1 #-------------------------------------------------------------------------------
2 # elftools: dwarf/dwarf_utils.py
4 # Minor, shared DWARF helpers
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
11 from ..construct
.macros
import UBInt32
, UBInt64
, ULInt32
, ULInt64
, Array
12 from ..common
.exceptions
import DWARFError
13 from ..common
.utils
import preserve_stream_pos
, struct_parse
15 def _get_base_offset(cu
, base_attribute_name
):
16 """Retrieves a required, base offset-type atribute
17 from the top DIE in the CU. Applies to several indirectly
18 encoded objects - range lists, location lists, strings, addresses.
20 cu_top_die
= cu
.get_top_DIE()
21 if not base_attribute_name
in cu_top_die
.attributes
:
22 raise DWARFError("The CU at offset 0x%x needs %s" % (cu
.cu_offset
, base_attribute_name
))
23 return cu_top_die
.attributes
[base_attribute_name
].value
25 def _resolve_via_offset_table(stream
, cu
, index
, base_attribute_name
):
26 """Given an index in the offset table and directions where to find it,
27 retrieves an offset. Works for loclists, rnglists.
29 The DWARF offset bitness of the CU block in the section matches that
30 of the CU record in dwarf_info. See DWARFv5 standard, section 7.4.
32 This is used for translating DW_FORM_loclistx, DW_FORM_rnglistx
33 via the offset table in the respective section.
35 base_offset
= _get_base_offset(cu
, base_attribute_name
)
36 # That's offset (within the rnglists/loclists/str_offsets section) of
37 # the offset table for this CU's block in that section, which in turn is indexed by the index.
39 offset_size
= 4 if cu
.structs
.dwarf_format
== 32 else 8
40 with
preserve_stream_pos(stream
):
41 return base_offset
+ struct_parse(cu
.structs
.Dwarf_offset(''), stream
, base_offset
+ index
*offset_size
)
43 def _iter_CUs_in_section(stream
, structs
, parser
):
44 """Iterates through the list of CU sections in loclists or rangelists. Almost identical structures there.
46 get_parser is a lambda that takes structs, returns the parser
48 stream
.seek(0, os
.SEEK_END
)
49 endpos
= stream
.tell()
50 stream
.seek(0, os
.SEEK_SET
)
53 while offset
< endpos
:
54 header
= struct_parse(parser
, stream
, offset
)
55 if header
.offset_count
> 0:
56 offset_parser
= structs
.Dwarf_uint64
if header
.is64
else structs
.Dwarf_uint32
57 header
['offsets'] = struct_parse(Array(header
.offset_count
, offset_parser('')), stream
)
59 header
['offsets'] = False
61 offset
= header
.offset_after_length
+ header
.unit_length