Skip to content

Commit

Permalink
add arm32 recomp target
Browse files Browse the repository at this point in the history
  • Loading branch information
DennyDai committed Jan 21, 2024
1 parent d7b81f1 commit 75ba673
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/patcherex2/components/compilers/llvm_recomp_arm.py
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
2 changes: 2 additions & 0 deletions src/patcherex2/targets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .elf_aarch64_linux import ElfAArch64Linux
from .elf_arm_linux import ElfArmLinux
from .elf_arm_linux_recomp import ElfArmLinuxRecomp
from .elf_arm_mimxrt1052 import ElfArmMimxrt1052
from .elf_i386_linux import ElfI386Linux
from .elf_leon3_bare import ElfLeon3Bare
Expand All @@ -11,6 +12,7 @@
__all__ = [
"ElfAArch64Linux",
"ElfArmLinux",
"ElfArmLinuxRecomp",
"ElfArmMimxrt1052",
"ElfI386Linux",
"ElfLeon3Bare",
Expand Down
21 changes: 21 additions & 0 deletions src/patcherex2/targets/elf_arm_linux_recomp.py
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()

0 comments on commit 75ba673

Please sign in to comment.