Improve DWARF 5 compatibility. (#400)
[pyelftools.git] / test / test_dwarf_v5_forms.py
1 # The dwarf_v5_forms.debug file was generated as follows, using gcc 11.2.0 on
2 # an x86_64 machine.
3 # $ cat dwarf_v5_forms.c
4 # int main();
5 # {
6 # char ** val;
7 # return 0;
8 # }
9 # $ gcc -O0 -gdwarf-5 dwarf_v5_forms.c -o dwarf_v5_forms.debug
10 # $ strip --only-keep-debug dwarf_v5_forms.debug
11 import unittest
12 import os
13
14
15 from elftools.elf.elffile import ELFFile
16
17 class TestDWARFV5_forms(unittest.TestCase):
18
19 def test_DW_FORM_implicit_const(self):
20 path = os.path.join('test', 'testfiles_for_unittests',
21 'dwarf_v5_forms.debug')
22 with open(path, 'rb') as f:
23 elffile = ELFFile(f)
24 dwarfinfo = elffile.get_dwarf_info()
25 # File is very small, so load all DIEs.
26 dies = []
27 for cu in dwarfinfo.iter_CUs():
28 dies.extend(cu.iter_DIEs())
29 # Locate the "var" DIE.
30 for die in dies:
31 # There should be only one
32 if (die.tag == "DW_TAG_variable" and
33 die.attributes["DW_AT_name"].value == b'val'):
34 # In the dwarfinfo, it's type is sized using a
35 # DW_FORM_implicit_const: check it is parsed correctly
36 break
37 dietype = cu.get_DIE_from_refaddr(die.attributes["DW_AT_type"].value)
38 byte_size_attr = dietype.attributes["DW_AT_byte_size"]
39 self.assertEqual(byte_size_attr.form, "DW_FORM_implicit_const")
40 self.assertEqual(byte_size_attr.value, 8)
41
42 def test_DW_FORM_linestrp(self):
43 path = os.path.join('test', 'testfiles_for_unittests',
44 'dwarf_v5_forms.debug')
45 with open(path, 'rb') as f:
46 elffile = ELFFile(f)
47 dwarfinfo = elffile.get_dwarf_info()
48 cu = next(dwarfinfo.iter_CUs())
49 top_die = cu.get_top_DIE()
50 name_attr = top_die.attributes["DW_AT_name"]
51 self.assertEqual(name_attr.form, "DW_FORM_line_strp")
52 self.assertEqual(name_attr.value, b"dwarf_v5_forms.c")