From 58a7461134f43b3d71da2cac4e7b8acd38beb01d Mon Sep 17 00:00:00 2001 From: Han Dai Date: Tue, 21 May 2024 21:30:14 -0400 Subject: [PATCH] move cc stuff to archinfo --- src/patcherex2/components/archinfo/aarch64.py | 12 ++++++---- src/patcherex2/components/compilers/clang.py | 3 +-- src/patcherex2/patches/instruction_patches.py | 15 ++++++++---- src/patcherex2/targets/elf_aarch64_linux.py | 24 ------------------- src/patcherex2/targets/elf_amd64_linux.py | 19 --------------- src/patcherex2/targets/target.py | 17 ------------- 6 files changed, 19 insertions(+), 71 deletions(-) diff --git a/src/patcherex2/components/archinfo/aarch64.py b/src/patcherex2/components/archinfo/aarch64.py index b895af9..5d6d395 100644 --- a/src/patcherex2/components/archinfo/aarch64.py +++ b/src/patcherex2/components/archinfo/aarch64.py @@ -49,11 +49,13 @@ class Aarch64Info: """ cc = { - "default": ["x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7"], - "defaultPreserveNone": None, # TODO once aarch64 support lands in LLVM for preserve_none + "Linux": ["x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7"], + # TODO: update LinuxPreserveNone once aarch64 support lands + # in LLVM for preserve_none, currently defaults to Linux + "LinuxPreserveNone": ["x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7"], } callee_saved = { - "default": [ + "Linux": [ "x19", "x20", "x21", @@ -68,9 +70,9 @@ class Aarch64Info: "x30", ] } - cc_float = {"default": ["v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7"]} + cc_float = {"Linux": ["v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7"]} callee_saved_float = { - "default": ["v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15"] + "Linux": ["v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15"] } float_types = {32: "float", 64: "double", 128: "long double"} diff --git a/src/patcherex2/components/compilers/clang.py b/src/patcherex2/components/compilers/clang.py index 85aab08..edad90d 100644 --- a/src/patcherex2/components/compilers/clang.py +++ b/src/patcherex2/components/compilers/clang.py @@ -12,8 +12,7 @@ def __init__( self, p, clang_version=15, compiler_flags: list[str] | None = None ) -> None: super().__init__(p) - if clang_version >= 19: - self.preserve_none = True + self.preserve_none = clang_version >= 19 if compiler_flags is None: compiler_flags = [] self._compiler = f"clang-{clang_version}" diff --git a/src/patcherex2/patches/instruction_patches.py b/src/patcherex2/patches/instruction_patches.py index e2fd540..c49b6cb 100644 --- a/src/patcherex2/patches/instruction_patches.py +++ b/src/patcherex2/patches/instruction_patches.py @@ -253,7 +253,12 @@ def _apply_c(self, p) -> None: [] if self.c_config.regs_sort is None else list(self.c_config.regs_sort) ) - calling_convention = p.target.get_cc(preserve_none=p.compiler.preserve_none) + # TODO: make it os agnostic? + calling_convention = ( + p.archinfo.cc["LinuxPreserveNone"] + if p.compiler.preserve_none + else p.archinfo.cc["Linux"] + ) subregister_table = p.archinfo.subregisters subregister_float_table = p.archinfo.subregisters_float @@ -265,7 +270,9 @@ def _apply_c(self, p) -> None: # some registers via 'register uint64_t rbx asm("rbx");', the compiler will insert # push and pop instructions to save these registers. extra_saved = ( - extra_saved - set(calling_convention) - set(p.target.get_callee_saved()) + extra_saved + - set(calling_convention) + - set(p.archinfo.callee_saved["Linux"]) ) extra_saved_in = list(extra_saved) # We don't want to necessarily output registers that have been marked as scratch @@ -282,12 +289,12 @@ def uint_converter(size: int): extra_saved_out, subregister_table, c_regs_sort, uint_converter ) - calling_convention_float: list[str] = p.target.get_cc_float() + calling_convention_float: list[str] = p.archinfo.cc_float["Linux"] extra_saved_float = set(p.archinfo.regs_float) extra_saved_float = ( extra_saved_float - set(calling_convention_float) - - set(p.target.get_callee_saved_float()) + - set(p.archinfo.callee_saved_float["Linux"]) ) extra_saved_float_in = list(extra_saved_float) extra_saved_float_out = list(extra_saved_float - c_scratch_regs) diff --git a/src/patcherex2/targets/elf_aarch64_linux.py b/src/patcherex2/targets/elf_aarch64_linux.py index 8462bea..7ef5c72 100644 --- a/src/patcherex2/targets/elf_aarch64_linux.py +++ b/src/patcherex2/targets/elf_aarch64_linux.py @@ -79,27 +79,3 @@ def get_archinfo(self, archinfo): if archinfo == "default": return Aarch64Info() raise NotImplementedError() - - def get_cc(self, preserve_none=False, archinfo=None): - archinfo = self.get_archinfo(archinfo) - if preserve_none: - cc = archinfo.cc["defaultPreserveNone"] - if cc is None: - logger.warning("preserve_none for ARM64 is not implemented yet!") - return archinfo.cc["default"] - else: - return cc - else: - return archinfo.cc["default"] - - def get_cc_float(self, archinfo=None): - archinfo = self.get_archinfo(archinfo) - return archinfo.cc_float["default"] - - def get_callee_saved(self, archinfo=None): - archinfo = self.get_archinfo(archinfo) - return archinfo.callee_saved["default"] - - def get_callee_saved_float(self, archinfo=None): - archinfo = self.get_archinfo(archinfo) - return archinfo.callee_saved_float["default"] diff --git a/src/patcherex2/targets/elf_amd64_linux.py b/src/patcherex2/targets/elf_amd64_linux.py index 55b13d7..12efacd 100644 --- a/src/patcherex2/targets/elf_amd64_linux.py +++ b/src/patcherex2/targets/elf_amd64_linux.py @@ -76,22 +76,3 @@ def get_archinfo(self, archinfo): if archinfo == "default": return Amd64Info() raise NotImplementedError() - - def get_cc(self, preserve_none=False, archinfo=None): - archinfo = self.get_archinfo(archinfo) - if preserve_none: - return archinfo.cc["LinuxPreserveNone"] - else: - return archinfo.cc["Linux"] - - def get_cc_float(self, archinfo=None): - archinfo = self.get_archinfo(archinfo) - return archinfo.cc_float["Linux"] - - def get_callee_saved(self, archinfo=None): - archinfo = self.get_archinfo(archinfo) - return archinfo.callee_saved["Linux"] - - def get_callee_saved_float(self, archinfo=None): - archinfo = self.get_archinfo(archinfo) - return archinfo.callee_saved_float["Linux"] diff --git a/src/patcherex2/targets/target.py b/src/patcherex2/targets/target.py index 79b828b..a08da95 100644 --- a/src/patcherex2/targets/target.py +++ b/src/patcherex2/targets/target.py @@ -20,20 +20,3 @@ def get_component(self, component_type, component_name, component_opts=None): if component_opts is None: component_opts = {} return getattr(self, f"get_{component_type}")(component_name, **component_opts) - - def get_cc(self, archinfo=None, preserve_none=False): - raise NotImplementedError("The calling convention for this target is unknown") - - def get_cc_float(self, archinfo=None): - raise NotImplementedError( - "The floating point calling convention for this target is unknown" - ) - - def get_archinfo(self, archinfo): - raise NotImplementedError("get_archinfo not implemented") - - def get_callee_saved(self, archinfo=None): - raise NotImplementedError("get_callee_saved not implemented") - - def get_callee_saved_float(self, archinfo=None): - raise NotImplementedError("get_callee_saved_float not implemented")