More changes to port to Python 3
[pyelftools.git] / examples / dwarf_range_lists.py
1 #-------------------------------------------------------------------------------
2 # elftools example: dwarf_range_lists.py
3 #
4 # Examine DIE entries which have range list values, and decode these range
5 # 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.ranges import RangeEntry
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 range lists are extracted by DWARFInfo from the .debug_ranges
41 # section, and returned here as a RangeLists object.
42 range_lists = dwarfinfo.range_lists()
43
44 for CU in dwarfinfo.iter_CUs():
45 # DWARFInfo allows to iterate over the compile units contained in
46 # the .debug_info section. CU is a CompileUnit object, with some
47 # computed attributes (such as its offset in the section) and
48 # a header which conforms to the DWARF standard. The access to
49 # header elements is, as usual, via item-lookup.
50 print(' Found a compile unit at offset %s, length %s' % (
51 CU.cu_offset, CU['unit_length']))
52
53 # A CU provides a simple API to iterate over all the DIEs in it.
54 for DIE in CU.iter_DIEs():
55 # Go over all attributes of the DIE. Each attribute is an
56 # AttributeValue object (from elftools.dwarf.die), which we
57 # can examine.
58 for attr in itervalues(DIE.attributes):
59 if attribute_has_range_list(attr):
60 # This is a range list. Its value is an offset into
61 # the .debug_ranges section, so we can use the range
62 # lists object to decode it.
63 rangelist = range_lists.get_range_list_at_offset(
64 attr.value)
65
66 print(' DIE %s. attr %s.\n%s' % (
67 DIE.tag,
68 attr.name,
69 rangelist))
70
71
72 def attribute_has_range_list(attr):
73 """ Only some attributes can have range list values, if they have the
74 required DW_FORM (rangelistptr "class" in DWARF spec v3)
75 """
76 if attr.name == 'DW_AT_ranges':
77 if attr.form in ('DW_FORM_data4', 'DW_FORM_data8'):
78 return True
79 return False
80
81
82 if __name__ == '__main__':
83 for filename in sys.argv[1:]:
84 process_file(filename)
85
86
87
88
89
90
91