Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / pyelftools / examples / dwarf_location_lists.py
1 #-------------------------------------------------------------------------------
2 # elftools example: dwarf_location_lists.py
3 #
4 # Examine DIE entries which have location list values, and decode these
5 # location lists.
6 #
7 # Eli Bendersky (eliben@gmail.com)
8 # This code is in the public domain
9 #-------------------------------------------------------------------------------
10 from __future__ import print_function
11 import sys
12
13 # If elftools is not installed, maybe we're running from the root or examples
14 # dir of the source distribution
15 try:
16     import elftools
17 except ImportError:
18     sys.path.extend(['.', '..'])
19
20 from elftools.common.py3compat import itervalues
21 from elftools.elf.elffile import ELFFile
22 from elftools.dwarf.descriptions import (
23     describe_DWARF_expr, set_global_machine_arch)
24 from elftools.dwarf.locationlists import LocationEntry
25
26
27 def process_file(filename):
28     print('Processing file:', filename)
29     with open(filename, 'rb') as f:
30         elffile = ELFFile(f)
31
32         if not elffile.has_dwarf_info():
33             print('  file has no DWARF info')
34             return
35
36         # get_dwarf_info returns a DWARFInfo context object, which is the
37         # starting point for all DWARF-based processing in pyelftools.
38         dwarfinfo = elffile.get_dwarf_info()
39
40         # The location lists are extracted by DWARFInfo from the .debug_loc
41         # section, and returned here as a LocationLists object.
42         location_lists = dwarfinfo.location_lists()
43
44         # This is required for the descriptions module to correctly decode
45         # register names contained in DWARF expressions.
46         set_global_machine_arch(elffile.get_machine_arch())
47
48         for CU in dwarfinfo.iter_CUs():
49             # DWARFInfo allows to iterate over the compile units contained in
50             # the .debug_info section. CU is a CompileUnit object, with some
51             # computed attributes (such as its offset in the section) and
52             # a header which conforms to the DWARF standard. The access to
53             # header elements is, as usual, via item-lookup.
54             print('  Found a compile unit at offset %s, length %s' % (
55                 CU.cu_offset, CU['unit_length']))
56
57             # A CU provides a simple API to iterate over all the DIEs in it.
58             for DIE in CU.iter_DIEs():
59                 # Go over all attributes of the DIE. Each attribute is an
60                 # AttributeValue object (from elftools.dwarf.die), which we
61                 # can examine.
62                 for attr in itervalues(DIE.attributes):
63                     if attribute_has_location_list(attr):
64                         # This is a location list. Its value is an offset into
65                         # the .debug_loc section, so we can use the location
66                         # lists object to decode it.
67                         loclist = location_lists.get_location_list_at_offset(
68                             attr.value)
69
70                         print('   DIE %s. attr %s.\n%s' % (
71                             DIE.tag,
72                             attr.name,
73                             show_loclist(loclist, dwarfinfo, indent='      ')))
74
75
76 def show_loclist(loclist, dwarfinfo, indent):
77     """ Display a location list nicely, decoding the DWARF expressions
78         contained within.
79     """
80     d = []
81     for loc_entity in loclist:
82         if isinstance(loc_entity, LocationEntry):
83             d.append('%s <<%s>>' % (
84                 loc_entity,
85                 describe_DWARF_expr(loc_entity.loc_expr, dwarfinfo.structs)))
86         else:
87             d.append(str(loc_entity))
88     return '\n'.join(indent + s for s in d)
89     
90
91 def attribute_has_location_list(attr):
92     """ Only some attributes can have location list values, if they have the
93         required DW_FORM (loclistptr "class" in DWARF spec v3)
94     """
95     if (attr.name in (  'DW_AT_location', 'DW_AT_string_length',
96                         'DW_AT_const_value', 'DW_AT_return_addr',
97                         'DW_AT_data_member_location', 'DW_AT_frame_base',
98                         'DW_AT_segment', 'DW_AT_static_link',
99                         'DW_AT_use_location', 'DW_AT_vtable_elem_location')):
100         if attr.form in ('DW_FORM_data4', 'DW_FORM_data8'):
101             return True
102     return False
103
104
105 if __name__ == '__main__':
106     for filename in sys.argv[1:]:
107         process_file(filename)
108
109
110
111
112
113