1 //===-- DWARFDebugArangeSet.cpp -------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFDebugArangeSet.h"
11 #include "llvm/Support/Format.h"
12 #include "llvm/Support/raw_ostream.h"
16 void DWARFDebugArangeSet::clear() {
18 std::memset(&Header, 0, sizeof(Header));
19 ArangeDescriptors.clear();
22 void DWARFDebugArangeSet::compact() {
23 if (ArangeDescriptors.empty())
26 // Iterate through all arange descriptors and combine any ranges that
27 // overlap or have matching boundaries. The ArangeDescriptors are assumed
28 // to be in ascending order.
30 while (i + 1 < ArangeDescriptors.size()) {
31 if (ArangeDescriptors[i].getEndAddress() >= ArangeDescriptors[i+1].Address){
32 // The current range ends at or exceeds the start of the next address
33 // range. Compute the max end address between the two and use that to
34 // make the new length.
35 const uint64_t max_end_addr =
36 std::max(ArangeDescriptors[i].getEndAddress(),
37 ArangeDescriptors[i+1].getEndAddress());
38 ArangeDescriptors[i].Length = max_end_addr - ArangeDescriptors[i].Address;
39 // Now remove the next entry as it was just combined with the previous one
40 ArangeDescriptors.erase(ArangeDescriptors.begin()+i+1);
42 // Discontiguous address range, just proceed to the next one.
49 DWARFDebugArangeSet::extract(DataExtractor data, uint32_t *offset_ptr) {
50 if (data.isValidOffset(*offset_ptr)) {
51 ArangeDescriptors.clear();
54 // 7.20 Address Range Table
56 // Each set of entries in the table of address ranges contained in
57 // the .debug_aranges section begins with a header consisting of: a
58 // 4-byte length containing the length of the set of entries for this
59 // compilation unit, not including the length field itself; a 2-byte
60 // version identifier containing the value 2 for DWARF Version 2; a
61 // 4-byte offset into the.debug_infosection; a 1-byte unsigned integer
62 // containing the size in bytes of an address (or the offset portion of
63 // an address for segmented addressing) on the target system; and a
64 // 1-byte unsigned integer containing the size in bytes of a segment
65 // descriptor on the target system. This header is followed by a series
66 // of tuples. Each tuple consists of an address and a length, each in
67 // the size appropriate for an address on the target architecture.
68 Header.Length = data.getU32(offset_ptr);
69 Header.Version = data.getU16(offset_ptr);
70 Header.CuOffset = data.getU32(offset_ptr);
71 Header.AddrSize = data.getU8(offset_ptr);
72 Header.SegSize = data.getU8(offset_ptr);
74 // The first tuple following the header in each set begins at an offset
75 // that is a multiple of the size of a single tuple (that is, twice the
76 // size of an address). The header is padded, if necessary, to the
77 // appropriate boundary.
78 const uint32_t header_size = *offset_ptr - Offset;
79 const uint32_t tuple_size = Header.AddrSize * 2;
80 uint32_t first_tuple_offset = 0;
81 while (first_tuple_offset < header_size)
82 first_tuple_offset += tuple_size;
84 *offset_ptr = Offset + first_tuple_offset;
86 Descriptor arangeDescriptor;
88 assert(sizeof(arangeDescriptor.Address) == sizeof(arangeDescriptor.Length));
89 assert(sizeof(arangeDescriptor.Address) >= Header.AddrSize);
91 while (data.isValidOffset(*offset_ptr)) {
92 arangeDescriptor.Address = data.getUnsigned(offset_ptr, Header.AddrSize);
93 arangeDescriptor.Length = data.getUnsigned(offset_ptr, Header.AddrSize);
95 // Each set of tuples is terminated by a 0 for the address and 0
97 if (arangeDescriptor.Address || arangeDescriptor.Length)
98 ArangeDescriptors.push_back(arangeDescriptor);
100 break; // We are done if we get a zero address and length
103 return !ArangeDescriptors.empty();
108 void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
109 OS << format("Address Range Header: length = 0x%8.8x, version = 0x%4.4x, ",
110 Header.Length, Header.Version)
111 << format("cu_offset = 0x%8.8x, addr_size = 0x%2.2x, seg_size = 0x%2.2x\n",
112 Header.CuOffset, Header.AddrSize, Header.SegSize);
114 const uint32_t hex_width = Header.AddrSize * 2;
115 for (DescriptorConstIter pos = ArangeDescriptors.begin(),
116 end = ArangeDescriptors.end(); pos != end; ++pos)
117 OS << format("[0x%*.*llx -", hex_width, hex_width, pos->Address)
118 << format(" 0x%*.*llx)\n", hex_width, hex_width, pos->getEndAddress());
122 class DescriptorContainsAddress {
123 const uint64_t Address;
125 DescriptorContainsAddress(uint64_t address) : Address(address) {}
126 bool operator()(const DWARFDebugArangeSet::Descriptor &desc) const {
127 return Address >= desc.Address && Address < (desc.Address + desc.Length);
131 uint32_t DWARFDebugArangeSet::findAddress(uint64_t address) const {
132 DescriptorConstIter end = ArangeDescriptors.end();
133 DescriptorConstIter pos =
134 std::find_if(ArangeDescriptors.begin(), end, // Range
135 DescriptorContainsAddress(address)); // Predicate
137 return Header.CuOffset;