1#-------------------------------------------------------------------------------
2# elftools tests
3#
4# Eli Bendersky (eliben@gmail.com), Milton Miller <miltonm@us.ibm.com>
5# This code is in the public domain
6#-------------------------------------------------------------------------------
7import os
8import unittest
9
10from elftools.elf.elffile import ELFFile
11from elftools.common.py3compat import bytes2str
12
13class TestCacheLUTandDIEref(unittest.TestCase):
14    def dprint(self, list):
15        if False:
16            self.oprint(list)
17
18    def oprint(self, list):
19        if False:
20            print(list)
21
22    def test_die_from_LUTentry(self):
23        lines = ['']
24        with open(os.path.join('test', 'testfiles_for_unittests',
25                               'lambda.elf'), 'rb') as f:
26            elffile = ELFFile(f)
27            self.assertTrue(elffile.has_dwarf_info())
28
29            dwarf = elffile.get_dwarf_info()
30            pt = dwarf.get_pubnames()
31            for (k, v) in pt.items():
32                ndie = dwarf.get_DIE_from_lut_entry(v)
33                self.dprint(ndie)
34                if not 'DW_AT_type' in ndie.attributes:
35                    continue
36                if not 'DW_AT_name' in ndie.attributes:
37                    continue
38                name = bytes2str(ndie.attributes['DW_AT_name'].value)
39                tlist = []
40                tdie = ndie
41                while True:
42                    tdie = tdie.get_DIE_from_attribute('DW_AT_type')
43                    self.dprint(ndie)
44                    ttag = tdie.tag
45                    if isinstance(ttag, int):
46                        ttag = 'TAG(0x%x)' % ttag
47                    tlist.append(ttag)
48                    if 'DW_AT_name' in tdie.attributes:
49                        break
50                tlist.append(bytes2str(tdie.attributes['DW_AT_name'].value))
51                tname = ' '.join(tlist)
52                line = "%s DIE at %s is of type %s" % (
53                        ndie.tag, ndie.offset, tname)
54                lines.append(line)
55                self.dprint(line)
56
57        self.oprint('\n'.join(lines))
58        self.assertGreater(len(lines), 1)
59