Compare commits

...

5 Commits
v1.0.5 ... main

Author SHA1 Message Date
Nicolas Kruse 4330c4015a
Merge pull request #2 from Nonannet/dev
Dev
2025-11-23 17:14:35 +01:00
Nicolas e5be50ba2e version bump to 1.0.7 2025-11-23 17:13:02 +01:00
Nicolas 16a9450100 addend extraction for ARM relocations added 2025-11-23 17:08:33 +01:00
Nicolas 36b0d1db5c version bumped to 1.0.6 2025-11-17 09:06:09 +01:00
Nicolas 05f78ed09e R_ARM_MOVW_ABS_NC and R_ARM_MOVT_ABS relocation added 2025-11-17 09:05:01 +01:00
3 changed files with 37 additions and 6 deletions

View File

@ -1,6 +1,6 @@
[project]
name = "pelfy"
version = "1.0.5"
version = "1.0.7"
authors = [
{ name="Nicolas Kruse", email="nicolas.kruse@nonan.net" },
]

View File

@ -406,6 +406,8 @@ relocation_table_types = {
14: ("R_ARM_THM_SWI8", 8, "S + A"),
15: ("R_ARM_XPC25", 25, ""),
16: ("R_ARM_THM_XPC22", 22, ""),
28: ("R_ARM_CALL", 24, "((S + A) - P) >> 2"),
29: ("R_ARM_JUMP24", 24, "((S + A) - P) >> 2"),
30: ("R_ARM_TLS_DESC", 0, ""),
32: ("R_ARM_ALU_PCREL_7_0", 7, "(S - P + A) & 0x000000FF"),
33: ("R_ARM_ALU_PCREL_15_8", 15, "(S - P + A) & 0x0000FF00"),
@ -418,6 +420,8 @@ relocation_table_types = {
40: ("R_ARM_V4BX", 0, ""),
41: ("R_ARM_STKCHK", 0, ""),
42: ("R_ARM_THM_STKCHK", 0, ""),
43: ("R_ARM_MOVW_ABS_NC", 16, "S + A"),
44: ("R_ARM_MOVT_ABS", 16, "S + A")
},
"EM_AARCH64": {
0: ("R_AARCH64_NONE", 0, ""),

View File

@ -501,14 +501,14 @@ class elf_file:
symbol_index = r_info >> 8
relocation_type = r_info & 0xFF
ret['r_addend'] = self.read_int(el_off + 8, 4, True) \
if sh.type == 'SHT_RELA' else self._get_rel_32bit_addend(r_offset, sh)
if sh.type == 'SHT_RELA' else self._get_rel_addend(relocation_type, r_offset, sh)
elif self.bit_width == 64:
r_offset = self.read_int(el_off, 8)
r_info = self.read_int(el_off + 8, 8)
symbol_index = r_info >> 32
relocation_type = r_info & 0xFFFFFFFF
ret['r_addend'] = self.read_int(el_off + 16, 8, True) \
if sh.type == 'SHT_RELA' else self._get_rel_32bit_addend(r_offset, sh)
if sh.type == 'SHT_RELA' else self._get_rel_addend(relocation_type, r_offset, sh)
else:
raise NotImplementedError(f"{self.bit_width} bit is not supported")
@ -516,9 +516,36 @@ class elf_file:
ret['r_info'] = r_info
yield elf_relocation(self, ret, symbol_index, relocation_type, sh['sh_info'], i)
def _get_rel_32bit_addend(self, r_offset: int, reloc_section: elf_section) -> int:
def _get_rel_addend(self, relocation_type: int, r_offset: int, reloc_section: elf_section) -> int:
reloc_types = fdat.relocation_table_types.get(self.architecture)
if reloc_types and 'A' in reloc_types[relocation_type][2]:
name = reloc_types[relocation_type][0]
sh = self.sections[reloc_section['sh_info']]
return self.read_int(r_offset + sh['sh_offset'], 4, True)
field = self.read_int(r_offset + sh['sh_offset'], 4, True)
if name in ('R_386_PC32', 'R_386_32', 'R_X86_64_PC32', 'R_X86_64_PLT32', 'R_ARM_REL32', 'R_ARM_ABS32'):
return field
if name == 'R_ARM_MOVW_ABS_NC':
imm4 = (field >> 16) & 0xF
imm12 = field & 0xFFF
return imm12 | (imm4 << 12)
if name == 'R_ARM_MOVT_ABS':
imm4 = (field >> 16) & 0xF
imm12 = field & 0xFFF
return (imm12 << 16) | (imm4 << 28) # Upper 16 bit
if name in ('R_ARM_JUMP24', 'R_ARM_CALL'):
imm24 = field & 0xFFFFFF
if imm24 & 0x800000:
imm24 |= ~0xFFFFFF
return imm24 << 2
if '_THM_' in name:
print('Warning: Thumb relocation addend extraction is not implemented')
return 0
if '_MIPS_' in name:
print('Warning: MIPS relocations addend extraction is not implemented')
return 0
raise NotImplementedError(f"Relocation addend extraction for {name} is not implemented")
return 0
def read_bytes(self, offset: int, num_bytes: int) -> bytes:
"""Read bytes from ELF file.