-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import logging | ||
|
||
from .llvm_recomp import LLVMRecomp | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LLVMRecompArm(LLVMRecomp): | ||
def compile( | ||
self, | ||
code, | ||
base=0, | ||
symbols=None, | ||
extra_compiler_flags=None, | ||
is_thumb=False, | ||
**kwargs, | ||
): | ||
if symbols is None: | ||
symbols = {} | ||
if extra_compiler_flags is None: | ||
extra_compiler_flags = [] | ||
if is_thumb: | ||
extra_compiler_flags += ["-mthumb"] | ||
else: | ||
extra_compiler_flags += ["-mno-thumb"] | ||
compiled = super().compile( | ||
code, | ||
base=base, | ||
symbols=symbols, | ||
extra_compiler_flags=extra_compiler_flags, | ||
**kwargs, | ||
) | ||
|
||
# FIXME: damn this is too hacky | ||
_symbols = {} | ||
_symbols.update(self.p.symbols) | ||
_symbols.update(self.p.binary_analyzer.get_all_symbols()) | ||
_symbols.update(symbols) | ||
symbols = _symbols | ||
disasm = self.p.disassembler.disassemble(compiled, base=base, is_thumb=is_thumb) | ||
reassembled = b"" | ||
for instr in disasm: | ||
if ( | ||
is_thumb | ||
and instr["mnemonic"] == "bl" | ||
and int(instr["op_str"][1:], 0) in symbols.values() | ||
): | ||
disasm_str = ( | ||
self.p.disassembler.to_asm_string(instr).replace("bl", "blx") + "\n" | ||
) | ||
reassembled += self.p.assembler.assemble( | ||
disasm_str, base=instr["address"], is_thumb=is_thumb | ||
) | ||
elif ( | ||
is_thumb | ||
and instr["mnemonic"] == "blx" | ||
and (int(instr["op_str"][1:], 0) + 1) in symbols.values() | ||
): | ||
disasm_str = ( | ||
self.p.disassembler.to_asm_string(instr).replace("blx", "bl") + "\n" | ||
) | ||
reassembled += self.p.assembler.assemble( | ||
disasm_str, base=instr["address"], is_thumb=is_thumb | ||
) | ||
elif ( | ||
not is_thumb | ||
and instr["mnemonic"] == "bl" | ||
and (int(instr["op_str"][1:], 0) + 1) in symbols.values() | ||
): | ||
disasm_str = ( | ||
self.p.disassembler.to_asm_string(instr).replace("bl", "blx") + "\n" | ||
) | ||
reassembled += self.p.assembler.assemble( | ||
disasm_str, base=instr["address"], is_thumb=is_thumb | ||
) | ||
elif ( | ||
not is_thumb | ||
and instr["mnemonic"] == "blx" | ||
and int(instr["op_str"][1:], 0) in symbols.values() | ||
): | ||
disasm_str = ( | ||
self.p.disassembler.to_asm_string(instr).replace("blx", "bl") + "\n" | ||
) | ||
reassembled += self.p.assembler.assemble( | ||
disasm_str, base=instr["address"], is_thumb=is_thumb | ||
) | ||
else: | ||
reassembled += compiled[ | ||
instr["address"] - base : instr["address"] - base + instr["size"] | ||
] | ||
compiled = reassembled + compiled[len(reassembled) :] | ||
if len(compiled) % 2 != 0: | ||
compiled += b"\x00" | ||
return compiled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from ..components.binary_analyzers.angr import Angr | ||
from ..components.compilers.llvm_recomp_arm import LLVMRecompArm | ||
from .elf_arm_linux import ElfArmLinux | ||
|
||
|
||
class ElfArmLinuxRecomp(ElfArmLinux): | ||
@staticmethod | ||
def detect_target(binary_path): | ||
return False | ||
|
||
def get_compiler(self, compiler): | ||
compiler = compiler or "llvm_recomp" | ||
if compiler == "llvm_recomp": | ||
return LLVMRecompArm(self.p) | ||
raise NotImplementedError() | ||
|
||
def get_binary_analyzer(self, binary_analyzer, **kwargs): | ||
binary_analyzer = binary_analyzer or "angr" | ||
if binary_analyzer == "angr": | ||
return Angr(self.binary_path, **kwargs) | ||
raise NotImplementedError() |