File: test_supplementary_object_files.py

package info (click to toggle)
python-pyelftools 0.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,964 kB
  • sloc: python: 15,903; ansic: 298; asm: 86; makefile: 24; cpp: 18; sh: 4
file content (101 lines) | stat: -rw-r--r-- 4,922 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# The test_gnudebugaltlink* and test_debugsup* files have been generated as
# follows:
# $ cat test_sup.c
# int main(int argc, char** argv)
# {
#        return argc;
# }
#
# $ gcc test_sup.c -o test_debugsup1
# $ gcc test_sup.c -o test_debugsup2
# $ dwz test_debugsup1 test_debugsup2 -m test_debugsup.common --dwarf-5
#
# $ gcc test_sup.c -o test_gnudebugaltlink1
# $ gcc test_sup.c -o test_gnudebugaltlink2
# $ dwz test_gnudebugaltlink1 test_gnudebugaltlink2 -m test_gnudebugaltlink.common

import unittest
import os

from elftools.elf.elffile import ELFFile

class TestDWARFSupplementaryObjects(unittest.TestCase):

    def test_gnudebugaltlink_no_followlinks(self):
        path = os.path.join('test', 'testfiles_for_unittests',
                            'test_gnudebugaltlink1.debug')
        with open(path, 'rb') as f:
            elffile = ELFFile(f)
            # Check that we don't have a supplementary_dwarfinfo
            dwarfinfo = elffile.get_dwarf_info(follow_links=False)
            self.assertIsNone(dwarfinfo.supplementary_dwarfinfo)
            # Check that imported units are present
            self.assertTrue(any(die.tag == 'DW_TAG_imported_unit'
                for cu in dwarfinfo.iter_CUs()
                for die in cu.iter_DIEs()))
            # Check that DW_FORM_GNU_strp_alt keep their raw_value.
            for cu in dwarfinfo.iter_CUs():
                for die in cu.iter_DIEs():
                    attrs = die.attributes
                    if ('DW_AT_name' in attrs and
                        attrs['DW_AT_name'].form == 'DW_FORM_GNU_strp_alt'):
                        self.assertEqual(attrs['DW_AT_name'].value,
                                         attrs['DW_AT_name'].raw_value)

    def test_gnudebugaltlink_followlinks(self):
        base_dir = os.path.join(b'test', b'testfiles_for_unittests')
        path = os.path.join(base_dir, b'test_gnudebugaltlink1.debug')
        with ELFFile.load_from_path(path) as elffile:
            # Check that we do have a supplementary_dwarfinfo
            dwarfinfo = elffile.get_dwarf_info()
            self.assertIsNotNone(dwarfinfo.supplementary_dwarfinfo)
            # Check that imported units are replaced by what they refer to.
            self.assertTrue(all(die.tag != 'DW_TAG_imported_unit'
                for cu in dwarfinfo.iter_CUs()
                for die in cu.iter_DIEs()))
            # Check that DW_FORM_GNU_strp_alt get a proper reference
            for cu in dwarfinfo.iter_CUs():
                for die in cu.iter_DIEs():
                    attrs = die.attributes
                    if ('DW_AT_name' in attrs and attrs['DW_AT_name'].form ==
                            'DW_FORM_GNU_strp_alt'):
                        self.assertIsInstance(attrs['DW_AT_name'].value, bytes)

    def test_debugsup_no_followlinks(self):
        path = os.path.join('test', 'testfiles_for_unittests',
                            'test_debugsup1.debug')
        with ELFFile.load_from_path(path) as elffile:
            # Check that we don't have a supplementary_dwarfinfo
            dwarfinfo = elffile.get_dwarf_info(follow_links=False)
            self.assertIsNone(dwarfinfo.supplementary_dwarfinfo)
            # Check that imported units are present
            self.assertTrue(any(die.tag == 'DW_TAG_imported_unit'
                for cu in dwarfinfo.iter_CUs()
                for die in cu.iter_DIEs()))
            # Check that DW_FORM_GNU_strp_alt keep their raw_value.
            for cu in dwarfinfo.iter_CUs():
                for die in cu.iter_DIEs():
                    attrs = die.attributes
                    if ('DW_AT_name' in attrs and
                        attrs['DW_AT_name'].form == 'DW_FORM_strp_sup'):
                        self.assertEqual(attrs['DW_AT_name'].value,
                                         attrs['DW_AT_name'].raw_value)

    def test_debugsup_followlinks(self):
        base_dir = os.path.join(b'test', b'testfiles_for_unittests')
        path = os.path.join(base_dir, b'test_debugsup1.debug')
        with ELFFile.load_from_path(path) as elffile:
            # Check that we do have a supplementary_dwarfinfo
            dwarfinfo = elffile.get_dwarf_info()
            self.assertIsNotNone(dwarfinfo.supplementary_dwarfinfo)
            # Check that imported units are replaced by what they refer to.
            self.assertTrue(all(die.tag != 'DW_TAG_imported_unit'
                for cu in dwarfinfo.iter_CUs()
                for die in cu.iter_DIEs()))
            # Check that DW_FORM_GNU_strp_alt get a proper reference
            for cu in dwarfinfo.iter_CUs():
                for die in cu.iter_DIEs():
                    attrs = die.attributes
                    if ('DW_AT_name' in attrs and attrs['DW_AT_name'].form ==
                            'DW_FORM_strp_sup'):
                        self.assertIsInstance(attrs['DW_AT_name'].value, bytes)