Files
portable_lce-shiggy/scripts/patch_relocations.py
2026-03-14 22:34:44 -05:00

44 lines
1.6 KiB
Python

import sys, struct, shutil, os
def patch_relocs(build_dir, out_dir, object_files):
patched = 0
for f in object_files:
f = os.path.join(build_dir, os.path.basename(f))
outfile = os.path.join(out_dir, os.path.basename(f).replace('.obj', '.o'))
shutil.copy(f, outfile)
with open(outfile, 'rb+') as file:
file.seek(0)
if file.read(4) != b'\x7fELF':
continue
file.seek(40)
hdr = struct.unpack('<Q I 6H', file.read(24))
e_shoff, e_shentsize, e_shnum = hdr[0], hdr[5], hdr[6]
for i in range(e_shnum):
file.seek(e_shoff + i * e_shentsize)
shdr = struct.unpack('<I I Q Q Q Q I I Q Q', file.read(64))
if shdr[1] == 4:
sh_offset, sh_size, sh_entsize = shdr[4], shdr[5], shdr[9]
for j in range(0, sh_size, sh_entsize):
file.seek(sh_offset + j + 8)
r_info = struct.unpack('<Q', file.read(8))[0]
if (r_info & 0xffffffff) == 40:
new_info = (r_info & 0xffffffff00000000) | 9
file.seek(sh_offset + j + 8)
file.write(struct.pack('<Q', new_info))
patched += 1
print(f"Patched {patched} 'type 40' relocations to standard GOTPCREL (9).")
if __name__ == '__main__':
build_dir = sys.argv[1]
out_dir = sys.argv[2]
object_files = sys.argv[3:]
patch_relocs(build_dir, out_dir, object_files)