pelfy/tests/test_example_elfs.py

64 lines
2.4 KiB
Python
Raw Normal View History

2025-07-28 14:04:19 +00:00
import pelfy._main as _main
import glob
2025-02-22 18:53:39 +00:00
def known_name(text: str) -> bool:
return not text.isnumeric() and not text.startswith('0x')
2025-02-24 22:28:02 +00:00
def test_simple_c() -> None:
file_list = glob.glob('tests/obj/*.o')
assert file_list, "No test object files found"
for path in file_list:
print(f'Open {path}...')
2025-07-28 14:04:19 +00:00
elf = _main.open_elf_file(path)
2025-02-23 22:13:21 +00:00
print(elf)
print(elf.sections)
print(elf.symbols)
print(elf.code_relocations)
print('\n')
2025-02-23 22:13:21 +00:00
section_count = 0
2025-04-02 19:20:36 +00:00
assert elf.sections
for section in elf.sections:
assert known_name(section.description), f"Section type {section.type} for {elf.architecture} in {path} is unknown."
for sym in section.symbols:
assert known_name(sym.info), f"Symbol info {sym.info} for {elf.architecture} in {path} is unknown."
section_count += 1
assert section_count > 2
2025-04-02 19:20:36 +00:00
assert elf.symbols
for sym in elf.symbols:
assert known_name(sym.info), f"Symbol info {sym.info} for {elf.architecture} in {path} is unknown."
2025-04-02 19:20:36 +00:00
assert elf.get_relocations()
for reloc in elf.get_relocations():
assert known_name(reloc.type), f"Relocation type {reloc.type} for {elf.architecture} in {path} is unknown."
2025-02-24 22:28:02 +00:00
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
2025-11-13 19:23:16 +00:00
assert 'leet456456456n4ghn4hf56n4f' not in elf.objects
assert 0 in elf.objects
assert 1000 not in elf.objects
assert elf.objects[0] in elf.symbols
assert elf.objects[0] not in elf.functions
2025-02-24 22:28:02 +00:00
if __name__ == '__main__':
test_simple_c()