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