From 5854fa909a29cf4c87cd46894cb1839cff147348 Mon Sep 17 00:00:00 2001 From: Han Dai Date: Mon, 20 May 2024 03:42:34 -0400 Subject: [PATCH] add some symbols automatically --- .../components/compilers/llvm_recomp.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/patcherex2/components/compilers/llvm_recomp.py b/src/patcherex2/components/compilers/llvm_recomp.py index 5714005..5fc9072 100644 --- a/src/patcherex2/components/compilers/llvm_recomp.py +++ b/src/patcherex2/components/compilers/llvm_recomp.py @@ -152,13 +152,29 @@ def compile( _symbols.update(symbols) with open(os.path.join(td, "obj.o"), "rb") as f: + elf = ELFFile(f) linker_script_rodata_sections = " ".join( [ f". = ALIGN({section['sh_addralign']}); *({section.name})" - for section in ELFFile(f).iter_sections() + for section in elf.iter_sections() if section.name.startswith(".rodata") ] ) + + # automatically add symbols like off_deadbeef, dword_deadbeef, etc. + for sym in elf.get_section_by_name(".symtab").iter_symbols(): + if ( + sym.entry.st_shndx == "SHN_UNDEF" + and sym.name + and "_" in sym.name + ): + try: + _, addr = sym.name.split("_", 1) + addr = int(addr, 16) + if sym.name not in _symbols: + _symbols[sym.name] = addr + except ValueError: + pass linker_script_symbols = "".join( f"{name} = {hex(addr)};" for name, addr in _symbols.items() )