Skip to content

Commit

Permalink
move cc stuff to archinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
DennyDai committed May 22, 2024
1 parent 524fd5d commit 58a7461
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 71 deletions.
12 changes: 7 additions & 5 deletions src/patcherex2/components/archinfo/aarch64.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"}
Expand Down
3 changes: 1 addition & 2 deletions src/patcherex2/components/compilers/clang.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
15 changes: 11 additions & 4 deletions src/patcherex2/patches/instruction_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand Down
24 changes: 0 additions & 24 deletions src/patcherex2/targets/elf_aarch64_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
19 changes: 0 additions & 19 deletions src/patcherex2/targets/elf_amd64_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
17 changes: 0 additions & 17 deletions src/patcherex2/targets/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 58a7461

Please sign in to comment.