354c3a72138ee9041c204bfca281b0bf7fd7d639
[pyelftools.git] / test / test_arm_call_reloc.py
1 #-------------------------------------------------------------------------------
2 # elftools tests
3 #
4 # Test 'R_ARM_CALL' relocation type support.
5 # Compare the '.text' section data of ELF file that was relocated by elftools
6 # with an ELF file that was relocated by linker.
7 #
8 # Dmitry Koltunov (koltunov@ispras.ru)
9 # This code is in the public domain
10 #-------------------------------------------------------------------------------
11 import os
12 import sys
13 import unittest
14
15 from elftools.common.py3compat import BytesIO
16 from elftools.elf.elffile import ELFFile
17 from elftools.elf.relocation import RelocationHandler
18
19
20 def do_relocation(rel_elf):
21 data = rel_elf.get_section_by_name('.text').data()
22 rh = RelocationHandler(rel_elf)
23
24 stream = BytesIO()
25 stream.write(data)
26
27 rel = rel_elf.get_section_by_name('.rel.text')
28 rh.apply_section_relocations(stream, rel)
29 return stream.getvalue()
30
31
32 class TestARMRElocation(unittest.TestCase):
33 def test_reloc(self):
34 test_dir = os.path.join('test', 'testfiles_for_unittests')
35 with open(os.path.join(test_dir, 'arm_reloc_unrelocated.o'), 'rb') as rel_f, \
36 open(os.path.join(test_dir, 'arm_reloc_relocated.elf'), 'rb') as f:
37 rel_elf = ELFFile(rel_f)
38 elf = ELFFile(f)
39
40 # Comparison of '.text' section data
41 self.assertEquals(do_relocation(rel_elf),
42 elf.get_section_by_name('.text').data())
43
44 if __name__ == '__main__':
45 unittest.main()