Skip to content

Commit

Permalink
update recomp
Browse files Browse the repository at this point in the history
  • Loading branch information
DennyDai committed Apr 26, 2024
1 parent d929b70 commit 84caa42
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/patcherex2/components/compilers/llvm_recomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import tempfile

import cle
from elftools.elf.elffile import ELFFile

from ..assets.assets import Assets
from .clang import Clang
Expand Down Expand Up @@ -40,22 +41,6 @@ def compile(
with open(os.path.join(td, "code.c"), "w") as f:
f.write(code)

# linker script
_symbols = {}
_symbols.update(self.p.symbols)
_symbols.update(self.p.binary_analyzer.get_all_symbols())
_symbols.update(symbols)
linker_script = (
"SECTIONS { .text : SUBALIGN(0) { . = "
+ hex(base)
+ "; *(.text) *(.rodata.*)"
)
for name, addr in _symbols.items():
linker_script += name + " = " + hex(addr) + ";"
linker_script += "} /DISCARD/ : { *(.eh_frame) } }"
with open(os.path.join(td, "linker.ld"), "w") as f:
f.write(linker_script)

librecomp_path = os.path.join(self._assets_path, "libRecompiler.so")

# c -> ll
Expand Down Expand Up @@ -160,6 +145,28 @@ def compile(
logger.error(e.stderr.decode("utf-8"))
raise e

# linker script
_symbols = {}
_symbols.update(self.p.symbols)
_symbols.update(self.p.binary_analyzer.get_all_symbols())
_symbols.update(symbols)

with open(os.path.join(td, "obj.o"), "rb") as f:
linker_script_rodata_sections = " ".join(
[
f". = ALIGN({section['sh_addralign']}); *({section.name})"
for section in ELFFile(f).iter_sections()
if section.name.startswith(".rodata")
]
)
linker_script_symbols = "".join(
f"{name} = {hex(addr)};" for name, addr in _symbols.items()
)

linker_script = f"SECTIONS {{ .patcherex2 : SUBALIGN(0) {{ . = {hex(base)}; *(.text) {linker_script_rodata_sections} {linker_script_symbols} }} }}"
with open(os.path.join(td, "linker.ld"), "w") as f:
f.write(linker_script)

# link object file
try:
args = [self._linker] + [
Expand Down

0 comments on commit 84caa42

Please sign in to comment.