1 //===- DWARFDebugArangeSet.cpp --------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
10 #include "llvm/BinaryFormat/Dwarf.h"
11 #include "llvm/Support/Errc.h"
12 #include "llvm/Support/Format.h"
13 #include "llvm/Support/raw_ostream.h"
21 void DWARFDebugArangeSet::Descriptor::dump(raw_ostream &OS,
22 uint32_t AddressSize) const {
23 OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2, Address)
24 << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2,
28 void DWARFDebugArangeSet::clear() {
30 std::memset(&HeaderData, 0, sizeof(Header));
31 ArangeDescriptors.clear();
34 Error DWARFDebugArangeSet::extract(DWARFDataExtractor data,
35 uint64_t *offset_ptr) {
36 assert(data.isValidOffset(*offset_ptr));
37 ArangeDescriptors.clear();
40 // 7.21 Address Range Table (extract)
41 // Each set of entries in the table of address ranges contained in
42 // the .debug_aranges section begins with a header containing:
43 // 1. unit_length (initial length)
44 // A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing
45 // the length of the set of entries for this compilation unit,
46 // not including the length field itself.
48 // The value in this field is 2.
49 // 3. debug_info_offset (section offset)
50 // A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the
51 // .debug_info section of the compilation unit header.
52 // 4. address_size (ubyte)
53 // 5. segment_selector_size (ubyte)
54 // This header is followed by a series of tuples. Each tuple consists of
55 // a segment, an address and a length. The segment selector size is given by
56 // the segment_selector_size field of the header; the address and length
57 // size are each given by the address_size field of the header. Each set of
58 // tuples is terminated by a 0 for the segment, a 0 for the address and 0
59 // for the length. If the segment_selector_size field in the header is zero,
60 // the segment selectors are omitted from all tuples, including
61 // the terminating tuple.
63 Error Err = Error::success();
64 std::tie(HeaderData.Length, HeaderData.Format) =
65 data.getInitialLength(offset_ptr, &Err);
66 HeaderData.Version = data.getU16(offset_ptr, &Err);
67 HeaderData.CuOffset = data.getUnsigned(
68 offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err);
69 HeaderData.AddrSize = data.getU8(offset_ptr, &Err);
70 HeaderData.SegSize = data.getU8(offset_ptr, &Err);
72 return createStringError(errc::invalid_argument,
73 "parsing address ranges table at offset 0x%" PRIx64
75 Offset, toString(std::move(Err)).c_str());
78 // Perform basic validation of the header fields.
79 uint64_t full_length =
80 dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length;
81 if (!data.isValidOffsetForDataOfSize(Offset, full_length))
82 return createStringError(errc::invalid_argument,
83 "the length of address range table at offset "
84 "0x%" PRIx64 " exceeds section size",
86 if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)
87 return createStringError(errc::invalid_argument,
88 "address range table at offset 0x%" PRIx64
89 " has unsupported address size: %d "
90 "(4 and 8 supported)",
91 Offset, HeaderData.AddrSize);
92 if (HeaderData.SegSize != 0)
93 return createStringError(errc::not_supported,
94 "non-zero segment selector size in address range "
95 "table at offset 0x%" PRIx64 " is not supported",
98 // The first tuple following the header in each set begins at an offset that
99 // is a multiple of the size of a single tuple (that is, twice the size of
100 // an address because we do not support non-zero segment selector sizes).
101 // Therefore, the full length should also be a multiple of the tuple size.
102 const uint32_t tuple_size = HeaderData.AddrSize * 2;
103 if (full_length % tuple_size != 0)
104 return createStringError(
105 errc::invalid_argument,
106 "address range table at offset 0x%" PRIx64
107 " has length that is not a multiple of the tuple size",
110 // The header is padded, if necessary, to the appropriate boundary.
111 const uint32_t header_size = *offset_ptr - Offset;
112 uint32_t first_tuple_offset = 0;
113 while (first_tuple_offset < header_size)
114 first_tuple_offset += tuple_size;
116 // There should be space for at least one tuple.
117 if (full_length <= first_tuple_offset)
118 return createStringError(
119 errc::invalid_argument,
120 "address range table at offset 0x%" PRIx64
121 " has an insufficient length to contain any entries",
124 *offset_ptr = Offset + first_tuple_offset;
126 Descriptor arangeDescriptor;
128 static_assert(sizeof(arangeDescriptor.Address) ==
129 sizeof(arangeDescriptor.Length),
130 "Different datatypes for addresses and sizes!");
131 assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
133 uint64_t end_offset = Offset + full_length;
134 while (*offset_ptr < end_offset) {
135 uint64_t EntryOffset = *offset_ptr;
136 arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
137 arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
139 // Each set of tuples is terminated by a 0 for the address and 0
141 if (arangeDescriptor.Length == 0 && arangeDescriptor.Address == 0) {
142 if (*offset_ptr == end_offset)
143 return ErrorSuccess();
144 return createStringError(
145 errc::invalid_argument,
146 "address range table at offset 0x%" PRIx64
147 " has a premature terminator entry at offset 0x%" PRIx64,
148 Offset, EntryOffset);
151 ArangeDescriptors.push_back(arangeDescriptor);
154 return createStringError(errc::invalid_argument,
155 "address range table at offset 0x%" PRIx64
156 " is not terminated by null entry",
160 void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
161 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format);
162 OS << "Address Range Header: "
163 << format("length = 0x%0*" PRIx64 ", ", OffsetDumpWidth, HeaderData.Length)
164 << "format = " << dwarf::FormatString(HeaderData.Format) << ", "
165 << format("version = 0x%4.4x, ", HeaderData.Version)
166 << format("cu_offset = 0x%0*" PRIx64 ", ", OffsetDumpWidth,
168 << format("addr_size = 0x%2.2x, ", HeaderData.AddrSize)
169 << format("seg_size = 0x%2.2x\n", HeaderData.SegSize);
171 for (const auto &Desc : ArangeDescriptors) {
172 Desc.dump(OS, HeaderData.AddrSize);