From 7ccd3852d1636ac68382977250e6173c2ce3c006 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 27 Jan 2026 13:38:24 +0100 Subject: [PATCH] Added thumb_mode, offset_in_section and offset_in_file property to elf_symbol class --- src/pelfy/_main.py | 7 +++++++ tests/test_example_elfs.py | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/pelfy/_main.py b/src/pelfy/_main.py index b9ec486..206a2aa 100644 --- a/src/pelfy/_main.py +++ b/src/pelfy/_main.py @@ -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: diff --git a/tests/test_example_elfs.py b/tests/test_example_elfs.py index 67c8b03..c0235ae 100644 --- a/tests/test_example_elfs.py +++ b/tests/test_example_elfs.py @@ -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