Indirect encoding support (#430)
[pyelftools.git] / elftools / dwarf / dwarf_util.py
1 #-------------------------------------------------------------------------------
2 # elftools: dwarf/dwarf_utils.py
3 #
4 # Minor, shared DWARF helpers
5 #
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
9
10 import os
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
14
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.
19 """
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
24
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.
28
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.
31
32 This is used for translating DW_FORM_loclistx, DW_FORM_rnglistx
33 via the offset table in the respective section.
34 """
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.
38
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)
42
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.
45
46 get_parser is a lambda that takes structs, returns the parser
47 """
48 stream.seek(0, os.SEEK_END)
49 endpos = stream.tell()
50 stream.seek(0, os.SEEK_SET)
51
52 offset = 0
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)
58 else:
59 header['offsets'] = False
60 yield header
61 offset = header.offset_after_length + header.unit_length
62