Fix for mixed version loclists, tests (#521)
[pyelftools.git] / test / test_relr.py
1 #-------------------------------------------------------------------------------
2 # elftools tests
3 #
4 # Andreas Ziegler (andreas.ziegler@fau.de)
5 # This code is in the public domain
6 #-------------------------------------------------------------------------------
7 # The lib_relro.so.elf file was generated as follows (on Debian 11):
8 # $ cat lib_relro.c
9 # int retfunc(){ return 1; }
10 # int (*ptr1)() = retfunc;
11 # int (*ptr2)() = retfunc;
12 # <...>
13 # int (*ptr100)() = retfunc;
14 # $ clang-12 -c -o lib_relro.o -fPIC lib_relro.c
15 # $ ld.lld-12 -o lib_relro.so.elf --pack-dyn-relocs=relr --shared -Bsymbolic-functions lib_relro.o
16
17 import unittest
18 import os
19
20 from elftools.elf.elffile import ELFFile
21
22 class TestRelr(unittest.TestCase):
23
24 def test_num_relocations(self):
25 """ Verify we can get the number of relocations in a RELR relocation
26 section.
27 """
28 path = os.path.join('test', 'testfiles_for_unittests',
29 'lib_relro.so.elf')
30 with open(path, 'rb') as f:
31 elf = ELFFile(f)
32 relr_section = elf.get_section_by_name('.relr.dyn')
33 self.assertIsNotNone(relr_section)
34 self.assertEqual(relr_section.num_relocations(), 100)
35
36 def test_get_relocation(self):
37 """ Verify we can get a specific RELR relocation.
38 """
39 path = os.path.join('test', 'testfiles_for_unittests',
40 'lib_relro.so.elf')
41 with open(path, 'rb') as f:
42 elf = ELFFile(f)
43 relr_section = elf.get_section_by_name('.relr.dyn')
44 self.assertIsNotNone(relr_section)
45 reloc = relr_section.get_relocation(n=0)
46 self.assertEqual(reloc['r_offset'], 0x4540)
47 reloc = relr_section.get_relocation(n=65)
48 self.assertEqual(reloc['r_offset'], 0x4748)
49
50 dynamic_section = elf.get_section_by_name('.dynamic')
51 self.assertIsNotNone(dynamic_section)
52 dynamic_relr = dynamic_section.get_relocation_tables()
53 self.assertIsNotNone(dynamic_relr)
54 self.assertIsNotNone(dynamic_relr['RELR'])
55 reloc = dynamic_relr['RELR'].get_relocation(n=0)
56 self.assertEqual(reloc['r_offset'], 0x4540)
57 reloc = dynamic_relr['RELR'].get_relocation(n=65)
58 self.assertEqual(reloc['r_offset'], 0x4748)