5f99473ed28cea850710fdb00418da7ce1ba5416
1 #-------------------------------------------------------------------------------
2 # elftools: dwarf/ranges.py
4 # DWARF ranges section decoding (.debug_ranges)
6 # Eli Bendersky (eliben@gmail.com)
7 # This code is in the public domain
8 #-------------------------------------------------------------------------------
10 from collections
import namedtuple
12 from ..common
.utils
import struct_parse
15 RangeEntry
= namedtuple('RangeEntry', 'begin_offset end_offset')
16 BaseAddressEntry
= namedtuple('BaseAddressEntry', 'base_address')
19 class RangeLists(object):
20 """ A single range list is a Python list consisting of RangeEntry or
21 BaseAddressEntry objects.
23 def __init__(self
, stream
, structs
):
25 self
.structs
= structs
26 self
._max
_addr
= 2 ** (self
.structs
.address_size
* 8) - 1
28 def get_range_list_at_offset(self
, offset
):
29 """ Get a range list at the given offset in the section.
31 self
.stream
.seek(offset
, os
.SEEK_SET
)
32 return self
._parse
_range
_list
_from
_stream
()
34 def iter_range_lists(self
):
35 """ Yield all range lists found in the section.
37 # Just call _parse_range_list_from_stream until the stream ends
38 self
.stream
.seek(0, os
.SEEK_END
)
39 endpos
= self
.stream
.tell()
41 self
.stream
.seek(0, os
.SEEK_SET
)
42 while self
.stream
.tell() < endpos
:
43 yield self
._parse
_range
_list
_from
_stream
()
45 #------ PRIVATE ------#
47 def _parse_range_list_from_stream(self
):
50 begin_offset
= struct_parse(
51 self
.structs
.Dwarf_target_addr(''), self
.stream
)
52 end_offset
= struct_parse(
53 self
.structs
.Dwarf_target_addr(''), self
.stream
)
54 if begin_offset
== 0 and end_offset
== 0:
55 # End of list - we're done.
57 elif begin_offset
== self
._max
_addr
:
58 # Base address selection entry
59 lst
.append(BaseAddressEntry(base_address
=end_offset
))
62 lst
.append(RangeEntry(
63 begin_offset
=begin_offset
,
64 end_offset
=end_offset
))