Added thumb_mode, offset_in_section and offset_in_file property to

elf_symbol class
This commit is contained in:
Nicolas 2026-01-27 13:38:24 +01:00
parent 5dccaa30d6
commit 7ccd3852d1
2 changed files with 21 additions and 1 deletions

View File

@ -38,6 +38,10 @@ class elf_symbol():
description: Description of the symbol type
stb: visibility of the symbol (local, global, etc.)
stb_description: Description of the symbol visibility
thumb_mode: Indicate if symbol is a ARM thumb function
offset_in_section: Position of first symbol byte
relative to section start
offset_in_file: Position of first symbol byte in object file
fields: All symbol header fields as dict
"""
@ -64,6 +68,9 @@ class elf_symbol():
self.info, self.description = fdat.st_info_values[fields['st_info'] & 0x0F]
self.stb, self.stb_description = fdat.stb_values[fields['st_info'] >> 4]
self.thumb_mode = bool((file.architecture == 'EM_ARM') & fields['st_value'] & 1)
self.offset_in_section = fields['st_value'] & ~int(self.thumb_mode)
self.offset_in_file = self.section['sh_offset'] + self.offset_in_section if self.section else 0
@property
def data(self) -> bytes:

View File

@ -38,7 +38,20 @@ def test_simple_c() -> None:
for reloc in elf.get_relocations():
assert known_name(reloc.type), f"Relocation type {reloc.type} for {elf.architecture} in {path} is unknown."
assert 'imageWidth' in elf.objects or 'read_float_ret' in elf.objects, path
is_arm = 'arm' in path
test_function = elf.functions[-3]
assert test_function.section
assert is_arm == ('.ARM.attributes' in elf.sections), path # Is 32 bit ARM
assert is_arm == (elf.architecture == 'EM_ARM'), (path, elf.architecture)
assert test_function.thumb_mode == ('thumb' in path or 'arm-linux-gnueabihf-gcc-12-O' in path), path
if test_function.thumb_mode:
# In 32 Bit ARM mode the leased segnificant bit in st_value indicates thumb mode
assert test_function.offset_in_file == (test_function.fields['st_value'] & ~1) + test_function.section['sh_offset']
else:
assert test_function.offset_in_file == test_function.fields['st_value'] + test_function.section['sh_offset']
assert ('imageWidth' in elf.objects) or ('read_float_ret' in elf.objects) or ('pow_int_float' in elf.functions), path
assert 'leet456456456n4ghn4hf56n4f' not in elf.objects
assert 0 in elf.objects
assert 1000 not in elf.objects