1 #-------------------------------------------------------------------------------
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):
9 # int retfunc(){ return 1; }
10 # int (*ptr1)() = retfunc;
11 # int (*ptr2)() = retfunc;
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
20 from elftools
.elf
.elffile
import ELFFile
22 class TestRelr(unittest
.TestCase
):
24 def test_num_relocations(self
):
25 """ Verify we can get the number of relocations in a RELR relocation
28 path
= os
.path
.join('test', 'testfiles_for_unittests',
30 with
open(path
, 'rb') as f
:
32 relr_section
= elf
.get_section_by_name('.relr.dyn')
33 self
.assertIsNotNone(relr_section
)
34 self
.assertEqual(relr_section
.num_relocations(), 100)
36 def test_get_relocation(self
):
37 """ Verify we can get a specific RELR relocation.
39 path
= os
.path
.join('test', 'testfiles_for_unittests',
41 with
open(path
, 'rb') as 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)
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)