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 elftools is not installed, maybe we're running from the root or examples
14 # dir of the source distribution
18 sys
.path
.extend(['.', '..'])
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
27 def process_file(filename
):
28 print('Processing file:', filename
)
29 with
open(filename
, 'rb') as f
:
32 if not elffile
.has_dwarf_info():
33 print(' file has no DWARF info')
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()
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()
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())
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']))
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
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(
70 print(' DIE %s. attr %s.\n%s' % (
73 show_loclist(loclist
, dwarfinfo
, indent
=' ')))
76 def show_loclist(loclist
, dwarfinfo
, indent
):
77 """ Display a location list nicely, decoding the DWARF expressions
81 for loc_entity
in loclist
:
82 if isinstance(loc_entity
, LocationEntry
):
83 d
.append('%s <<%s>>' % (
85 describe_DWARF_expr(loc_entity
.loc_expr
, dwarfinfo
.structs
)))
87 d
.append(str(loc_entity
))
88 return '\n'.join(indent
+ s
for s
in d
)
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)
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'):
105 if __name__
== '__main__':
106 for filename
in sys
.argv
[1:]:
107 process_file(filename
)