1 #-------------------------------------------------------------------------------
2 # elftools: dwarf/dwarfinfo.py
4 # DWARFInfo - Main class for accessing DWARF debug information
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
9 from collections
import namedtuple
10 from bisect
import bisect_right
12 from ..common
.exceptions
import DWARFError
13 from ..common
.utils
import (struct_parse
, dwarf_assert
,
14 parse_cstring_from_stream
)
15 from .structs
import DWARFStructs
16 from .compileunit
import CompileUnit
17 from .abbrevtable
import AbbrevTable
18 from .lineprogram
import LineProgram
19 from .callframe
import CallFrameInfo
20 from .locationlists
import LocationLists
21 from .ranges
import RangeLists
22 from .aranges
import ARanges
23 from .namelut
import NameLUT
26 # Describes a debug section
28 # stream: a stream object containing the data of this section
29 # name: section name in the container file
30 # global_offset: the global offset of the section in its container file
31 # size: the size of the section's data, in bytes
32 # address: the virtual address for the section's data
34 # 'name' and 'global_offset' are for descriptional purposes only and
35 # aren't strictly required for the DWARF parsing to work. 'address' is required
36 # to properly decode the special '.eh_frame' format.
38 DebugSectionDescriptor
= namedtuple('DebugSectionDescriptor',
39 'stream name global_offset size address')
42 # Some configuration parameters for the DWARF reader. This exists to allow
43 # DWARFInfo to be independent from any specific file format/container.
46 # boolean flag specifying whether the data in the file is little endian
49 # Machine architecture as a string. For example 'x86' or 'x64'
51 # default_address_size:
52 # The default address size for the container file (sizeof pointer, in bytes)
54 DwarfConfig
= namedtuple('DwarfConfig',
55 'little_endian machine_arch default_address_size')
58 class DWARFInfo(object):
59 """ Acts also as a "context" to other major objects, bridging between
60 various parts of the debug infromation.
76 debug_str_offsets_sec
):
81 DebugSectionDescriptor for a section. Pass None for sections
82 that don't exist. These arguments are best given with
86 self
.debug_info_sec
= debug_info_sec
87 self
.debug_aranges_sec
= debug_aranges_sec
88 self
.debug_abbrev_sec
= debug_abbrev_sec
89 self
.debug_frame_sec
= debug_frame_sec
90 self
.eh_frame_sec
= eh_frame_sec
91 self
.debug_str_sec
= debug_str_sec
92 self
.debug_loc_sec
= debug_loc_sec
93 self
.debug_ranges_sec
= debug_ranges_sec
94 self
.debug_line_sec
= debug_line_sec
95 self
.debug_pubtypes_sec
= debug_pubtypes_sec
96 self
.debug_pubnames_sec
= debug_pubnames_sec
98 # This is the DWARFStructs the context uses, so it doesn't depend on
99 # DWARF format and address_size (these are determined per CU) - set them
101 self
.structs
= DWARFStructs(
102 little_endian
=self
.config
.little_endian
,
104 address_size
=self
.config
.default_address_size
)
106 # Cache for abbrev tables: a dict keyed by offset
107 self
._abbrevtable
_cache
= {}
109 # Cache of compile units and map of their offsets for bisect lookup.
110 # Access with .iter_CUs(), .get_CU_containing(), and/or .get_CU_at().
112 self
._cu
_offsets
_map
= []
115 def has_debug_info(self
):
116 """ Return whether this contains debug information.
118 It can be not the case when the ELF only contains .eh_frame, which is
119 encoded DWARF but not actually for debugging.
121 return bool(self
.debug_info_sec
)
123 def get_DIE_from_lut_entry(self
, lut_entry
):
124 """ Get the DIE from the pubnames or putbtypes lookup table entry.
127 A NameLUTEntry object from a NameLUT instance (see
128 .get_pubmames and .get_pubtypes methods).
130 cu
= self
.get_CU_at(lut_entry
.cu_ofs
)
131 return self
.get_DIE_from_refaddr(lut_entry
.die_ofs
, cu
)
133 def get_DIE_from_refaddr(self
, refaddr
, cu
=None):
134 """ Given a .debug_info section offset of a DIE, return the DIE.
137 The refaddr may come from a DW_FORM_ref_addr attribute.
140 The compile unit object, if known. If None a search
141 from the closest offset less than refaddr will be performed.
144 cu
= self
.get_CU_containing(refaddr
)
145 return cu
.get_DIE_from_refaddr(refaddr
)
147 def get_CU_containing(self
, refaddr
):
148 """ Find the CU that includes the given reference address in the
152 Either a refaddr of a DIE (possibly from a DW_FORM_ref_addr
153 attribute) or the section offset of a CU (possibly from an
156 This function will parse and cache CUs until the search criteria
157 is met, starting from the closest known offset lessthan or equal
158 to the given address.
162 'CU lookup but no debug info section')
164 0 <= refaddr
< self
.debug_info_sec
.size
,
165 "refaddr %s beyond .debug_info size" % refaddr
)
167 # The CU containing the DIE we desire will be to the right of the
168 # DIE insert point. If we have a CU address, then it will be a
169 # match but the right insert minus one will still be the item.
170 # The first CU starts at offset 0, so start there if cache is empty.
171 i
= bisect_right(self
._cu
_offsets
_map
, refaddr
)
172 start
= self
._cu
_offsets
_map
[i
- 1] if i
> 0 else 0
174 # parse CUs until we find one containing the desired address
175 for cu
in self
._parse
_CUs
_iter
(start
):
176 if cu
.cu_offset
<= refaddr
< cu
.cu_offset
+ cu
.size
:
179 raise ValueError("CU for reference address %s not found" % refaddr
)
181 def get_CU_at(self
, offset
):
182 """ Given a CU header offset, return the parsed CU.
185 The offset may be from an accelerated access table such as
186 the public names, public types, address range table, or
189 This function will directly parse the CU doing no validation of
190 the offset beyond checking the size of the .debug_info section.
194 'CU lookup but no debug info section')
196 0 <= offset
< self
.debug_info_sec
.size
,
197 "offset %s beyond .debug_info size" % offset
)
199 return self
._cached
_CU
_at
_offset
(offset
)
202 """ Yield all the compile units (CompileUnit objects) in the debug info
204 return self
._parse
_CUs
_iter
()
206 def get_abbrev_table(self
, offset
):
207 """ Get an AbbrevTable from the given offset in the debug_abbrev
210 The only verification done on the offset is that it's within the
211 bounds of the section (if not, an exception is raised).
212 It is the caller's responsibility to make sure the offset actually
213 points to a valid abbreviation table.
215 AbbrevTable objects are cached internally (two calls for the same
216 offset will return the same object).
219 offset
< self
.debug_abbrev_sec
.size
,
220 "Offset '0x%x' to abbrev table out of section bounds" % offset
)
221 if offset
not in self
._abbrevtable
_cache
:
222 self
._abbrevtable
_cache
[offset
] = AbbrevTable(
223 structs
=self
.structs
,
224 stream
=self
.debug_abbrev_sec
.stream
,
226 return self
._abbrevtable
_cache
[offset
]
228 def get_string_from_table(self
, offset
):
229 """ Obtain a string from the string table section, given an offset
230 relative to the section.
232 return parse_cstring_from_stream(self
.debug_str_sec
.stream
, offset
)
234 def line_program_for_CU(self
, CU
):
235 """ Given a CU object, fetch the line program it points to from the
237 If the CU doesn't point to a line program, return None.
239 # The line program is pointed to by the DW_AT_stmt_list attribute of
240 # the top DIE of a CU.
241 top_DIE
= CU
.get_top_DIE()
242 if 'DW_AT_stmt_list' in top_DIE
.attributes
:
243 return self
._parse
_line
_program
_at
_offset
(
244 top_DIE
.attributes
['DW_AT_stmt_list'].value
, CU
.structs
)
249 """ Does this dwarf info have a dwarf_frame CFI section?
251 return self
.debug_frame_sec
is not None
253 def CFI_entries(self
):
254 """ Get a list of dwarf_frame CFI entries from the .debug_frame section.
257 stream
=self
.debug_frame_sec
.stream
,
258 size
=self
.debug_frame_sec
.size
,
259 address
=self
.debug_frame_sec
.address
,
260 base_structs
=self
.structs
)
261 return cfi
.get_entries()
263 def has_EH_CFI(self
):
264 """ Does this dwarf info have a eh_frame CFI section?
266 return self
.eh_frame_sec
is not None
268 def EH_CFI_entries(self
):
269 """ Get a list of eh_frame CFI entries from the .eh_frame section.
272 stream
=self
.eh_frame_sec
.stream
,
273 size
=self
.eh_frame_sec
.size
,
274 address
=self
.eh_frame_sec
.address
,
275 base_structs
=self
.structs
,
277 return cfi
.get_entries()
279 def get_pubtypes(self
):
281 Returns a NameLUT object that contains information read from the
282 .debug_pubtypes section in the ELF file.
284 NameLUT is essentially a dictionary containing the CU/DIE offsets of
285 each symbol. See the NameLUT doc string for more details.
288 if self
.debug_pubtypes_sec
:
289 return NameLUT(self
.debug_pubtypes_sec
.stream
,
290 self
.debug_pubtypes_sec
.size
,
295 def get_pubnames(self
):
297 Returns a NameLUT object that contains information read from the
298 .debug_pubnames section in the ELF file.
300 NameLUT is essentially a dictionary containing the CU/DIE offsets of
301 each symbol. See the NameLUT doc string for more details.
304 if self
.debug_pubnames_sec
:
305 return NameLUT(self
.debug_pubnames_sec
.stream
,
306 self
.debug_pubnames_sec
.size
,
311 def get_aranges(self
):
312 """ Get an ARanges object representing the .debug_aranges section of
313 the DWARF data, or None if the section doesn't exist
315 if self
.debug_aranges_sec
:
316 return ARanges(self
.debug_aranges_sec
.stream
,
317 self
.debug_aranges_sec
.size
,
322 def location_lists(self
):
323 """ Get a LocationLists object representing the .debug_loc section of
324 the DWARF data, or None if this section doesn't exist.
326 if self
.debug_loc_sec
:
327 return LocationLists(self
.debug_loc_sec
.stream
, self
.structs
)
331 def range_lists(self
):
332 """ Get a RangeLists object representing the .debug_ranges section of
333 the DWARF data, or None if this section doesn't exist.
335 if self
.debug_ranges_sec
:
336 return RangeLists(self
.debug_ranges_sec
.stream
, self
.structs
)
340 #------ PRIVATE ------#
342 def _parse_CUs_iter(self
, offset
=0):
343 """ Iterate CU objects in order of appearance in the debug_info section.
346 The offset of the first CU to yield. Additional iterations
347 will return the sequential unit objects.
349 See .iter_CUs(), .get_CU_containing(), and .get_CU_at().
351 if self
.debug_info_sec
is None:
354 while offset
< self
.debug_info_sec
.size
:
355 cu
= self
._cached
_CU
_at
_offset
(offset
)
356 # Compute the offset of the next CU in the section. The unit_length
357 # field of the CU header contains its size not including the length
361 cu
.structs
.initial_length_field_size())
364 def _cached_CU_at_offset(self
, offset
):
365 """ Return the CU with unit header at the given offset into the
366 debug_info section from the cache. If not present, the unit is
367 header is parsed and the object is installed in the cache.
370 The offset of the unit header in the .debug_info section
371 to of the unit to fetch from the cache.
375 # Find the insert point for the requested offset. With bisect_right,
376 # if this entry is present in the cache it will be the prior entry.
377 i
= bisect_right(self
._cu
_offsets
_map
, offset
)
378 if i
>= 1 and offset
== self
._cu
_offsets
_map
[i
- 1]:
379 return self
._cu
_cache
[i
- 1]
381 # Parse the CU and insert the offset and object into the cache.
382 # The ._cu_offsets_map[] contains just the numeric offsets for the
383 # bisect_right search while the parallel indexed ._cu_cache[] holds
384 # the object references.
385 cu
= self
._parse
_CU
_at
_offset
(offset
)
386 self
._cu
_offsets
_map
.insert(i
, offset
)
387 self
._cu
_cache
.insert(i
, cu
)
390 def _parse_CU_at_offset(self
, offset
):
391 """ Parse and return a CU at the given offset in the debug_info stream.
393 # Section 7.4 (32-bit and 64-bit DWARF Formats) of the DWARF spec v3
394 # states that the first 32-bit word of the CU header determines
395 # whether the CU is represented with 32-bit or 64-bit DWARF format.
397 # So we peek at the first word in the CU header to determine its
398 # dwarf format. Based on it, we then create a new DWARFStructs
399 # instance suitable for this CU and use it to parse the rest.
401 initial_length
= struct_parse(
402 self
.structs
.Dwarf_uint32(''), self
.debug_info_sec
.stream
, offset
)
403 dwarf_format
= 64 if initial_length
== 0xFFFFFFFF else 32
406 # Temporary structs for parsing the header
407 # The structs for the rest of the CU depend on the header data.
409 cu_structs
= DWARFStructs(
410 little_endian
=self
.config
.little_endian
,
411 dwarf_format
=dwarf_format
,
415 cu_header
= struct_parse(
416 cu_structs
.Dwarf_CU_header
, self
.debug_info_sec
.stream
, offset
)
418 # structs for the rest of the CU, taking into account bitness and DWARF version
419 cu_structs
= DWARFStructs(
420 little_endian
=self
.config
.little_endian
,
421 dwarf_format
=dwarf_format
,
422 address_size
=cu_header
['address_size'],
423 dwarf_version
=cu_header
['version'])
425 cu_die_offset
= self
.debug_info_sec
.stream
.tell()
427 self
._is
_supported
_version
(cu_header
['version']),
428 "Expected supported DWARF version. Got '%s'" % cu_header
['version'])
434 cu_die_offset
=cu_die_offset
)
436 def _is_supported_version(self
, version
):
437 """ DWARF version supported by this parser
439 return 2 <= version
<= 5
441 def _parse_line_program_at_offset(self
, debug_line_offset
, structs
):
442 """ Given an offset to the .debug_line section, parse the line program
443 starting at this offset in the section and return it.
444 structs is the DWARFStructs object used to do this parsing.
446 lineprog_header
= struct_parse(
447 structs
.Dwarf_lineprog_header
,
448 self
.debug_line_sec
.stream
,
451 # Calculate the offset to the next line program (see DWARF 6.2.4)
452 end_offset
= ( debug_line_offset
+ lineprog_header
['unit_length'] +
453 structs
.initial_length_field_size())
456 header
=lineprog_header
,
457 stream
=self
.debug_line_sec
.stream
,
459 program_start_offset
=self
.debug_line_sec
.stream
.tell(),
460 program_end_offset
=end_offset
)