1//===- DWARFDebugArangeSet.cpp --------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
10#include "llvm/BinaryFormat/Dwarf.h"
11#include "llvm/DebugInfo/DWARF/DWARFContext.h"
12#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
13#include "llvm/Support/Errc.h"
14#include "llvm/Support/Format.h"
15#include "llvm/Support/FormatAdapters.h"
16#include "llvm/Support/FormatVariadic.h"
17#include "llvm/Support/raw_ostream.h"
18#include <cassert>
19#include <cinttypes>
20#include <cstdint>
21#include <cstring>
22
23using namespace llvm;
24
25void DWARFDebugArangeSet::Descriptor::dump(raw_ostream &OS,
26 uint32_t AddressSize) const {
27 OS << '[';
28 DWARFFormValue::dumpAddress(OS, AddressSize, Address);
29 OS << ", ";
30 DWARFFormValue::dumpAddress(OS, AddressSize, Address: getEndAddress());
31 OS << ')';
32}
33
34void DWARFDebugArangeSet::clear() {
35 Offset = -1ULL;
36 std::memset(s: &HeaderData, c: 0, n: sizeof(Header));
37 ArangeDescriptors.clear();
38}
39
40Error DWARFDebugArangeSet::extract(DWARFDataExtractor data,
41 uint64_t *offset_ptr,
42 function_ref<void(Error)> WarningHandler) {
43 assert(data.isValidOffset(*offset_ptr));
44 ArangeDescriptors.clear();
45 Offset = *offset_ptr;
46
47 // 7.21 Address Range Table (extract)
48 // Each set of entries in the table of address ranges contained in
49 // the .debug_aranges section begins with a header containing:
50 // 1. unit_length (initial length)
51 // A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing
52 // the length of the set of entries for this compilation unit,
53 // not including the length field itself.
54 // 2. version (uhalf)
55 // The value in this field is 2.
56 // 3. debug_info_offset (section offset)
57 // A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the
58 // .debug_info section of the compilation unit header.
59 // 4. address_size (ubyte)
60 // 5. segment_selector_size (ubyte)
61 // This header is followed by a series of tuples. Each tuple consists of
62 // a segment, an address and a length. The segment selector size is given by
63 // the segment_selector_size field of the header; the address and length
64 // size are each given by the address_size field of the header. Each set of
65 // tuples is terminated by a 0 for the segment, a 0 for the address and 0
66 // for the length. If the segment_selector_size field in the header is zero,
67 // the segment selectors are omitted from all tuples, including
68 // the terminating tuple.
69
70 Error Err = Error::success();
71 std::tie(args&: HeaderData.Length, args&: HeaderData.Format) =
72 data.getInitialLength(Off: offset_ptr, Err: &Err);
73 HeaderData.Version = data.getU16(offset_ptr, Err: &Err);
74 HeaderData.CuOffset = data.getUnsigned(
75 offset_ptr, byte_size: dwarf::getDwarfOffsetByteSize(Format: HeaderData.Format), Err: &Err);
76 HeaderData.AddrSize = data.getU8(offset_ptr, Err: &Err);
77 HeaderData.SegSize = data.getU8(offset_ptr, Err: &Err);
78 if (Err) {
79 return createStringError(EC: errc::invalid_argument,
80 Fmt: "parsing address ranges table at offset 0x%" PRIx64
81 ": %s",
82 Vals: Offset, Vals: toString(E: std::move(Err)).c_str());
83 }
84
85 // Perform basic validation of the header fields.
86 uint64_t full_length =
87 dwarf::getUnitLengthFieldByteSize(Format: HeaderData.Format) + HeaderData.Length;
88 if (!data.isValidOffsetForDataOfSize(offset: Offset, length: full_length))
89 return createStringError(EC: errc::invalid_argument,
90 Fmt: "the length of address range table at offset "
91 "0x%" PRIx64 " exceeds section size",
92 Vals: Offset);
93 if (Error SizeErr = DWARFContext::checkAddressSizeSupported(
94 AddressSize: HeaderData.AddrSize, EC: errc::invalid_argument,
95 Fmt: "address range table at offset 0x%" PRIx64, Vals: Offset))
96 return SizeErr;
97 if (HeaderData.SegSize != 0)
98 return createStringError(EC: errc::not_supported,
99 Fmt: "non-zero segment selector size in address range "
100 "table at offset 0x%" PRIx64 " is not supported",
101 Vals: Offset);
102
103 // The first tuple following the header in each set begins at an offset that
104 // is a multiple of the size of a single tuple (that is, twice the size of
105 // an address because we do not support non-zero segment selector sizes).
106 // Therefore, the full length should also be a multiple of the tuple size.
107 const uint32_t tuple_size = HeaderData.AddrSize * 2;
108 if (full_length % tuple_size != 0)
109 return createStringError(
110 EC: errc::invalid_argument,
111 Fmt: "address range table at offset 0x%" PRIx64
112 " has length that is not a multiple of the tuple size",
113 Vals: Offset);
114
115 // The header is padded, if necessary, to the appropriate boundary.
116 const uint32_t header_size = *offset_ptr - Offset;
117 uint32_t first_tuple_offset = 0;
118 while (first_tuple_offset < header_size)
119 first_tuple_offset += tuple_size;
120
121 // There should be space for at least one tuple.
122 if (full_length <= first_tuple_offset)
123 return createStringError(
124 EC: errc::invalid_argument,
125 Fmt: "address range table at offset 0x%" PRIx64
126 " has an insufficient length to contain any entries",
127 Vals: Offset);
128
129 *offset_ptr = Offset + first_tuple_offset;
130
131 Descriptor arangeDescriptor;
132
133 static_assert(sizeof(arangeDescriptor.Address) ==
134 sizeof(arangeDescriptor.Length),
135 "Different datatypes for addresses and sizes!");
136 assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
137
138 uint64_t end_offset = Offset + full_length;
139 while (*offset_ptr < end_offset) {
140 uint64_t EntryOffset = *offset_ptr;
141 arangeDescriptor.Address = data.getUnsigned(offset_ptr, byte_size: HeaderData.AddrSize);
142 arangeDescriptor.Length = data.getUnsigned(offset_ptr, byte_size: HeaderData.AddrSize);
143
144 // Each set of tuples is terminated by a 0 for the address and 0
145 // for the length.
146 if (arangeDescriptor.Length == 0 && arangeDescriptor.Address == 0) {
147 if (*offset_ptr == end_offset)
148 return ErrorSuccess();
149 if (WarningHandler) {
150 WarningHandler(createStringError(
151 EC: errc::invalid_argument,
152 Fmt: "address range table at offset 0x%" PRIx64
153 " has a premature terminator entry at offset 0x%" PRIx64,
154 Vals: Offset, Vals: EntryOffset));
155 }
156 }
157
158 ArangeDescriptors.push_back(x: arangeDescriptor);
159 }
160
161 return createStringError(EC: errc::invalid_argument,
162 Fmt: "address range table at offset 0x%" PRIx64
163 " is not terminated by null entry",
164 Vals: Offset);
165}
166
167void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
168 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format: HeaderData.Format);
169 OS << "Address Range Header: "
170 << formatv(Fmt: "length = 0x{0:x-}, ",
171 Vals: fmt_align(Item: HeaderData.Length, Where: AlignStyle::Right, Amount: OffsetDumpWidth,
172 Fill: '0'))
173 << "format = " << dwarf::FormatString(Format: HeaderData.Format) << ", "
174 << formatv(Fmt: "version = {0:x+4}, ", Vals: HeaderData.Version)
175 << formatv(Fmt: "cu_offset = 0x{0:x-}, ",
176 Vals: fmt_align(Item: HeaderData.CuOffset, Where: AlignStyle::Right,
177 Amount: OffsetDumpWidth, Fill: '0'))
178 << formatv(Fmt: "addr_size = {0:x+2}, ", Vals: HeaderData.AddrSize)
179 << formatv(Fmt: "seg_size = {0:x+2}\n", Vals: HeaderData.SegSize);
180
181 for (const auto &Desc : ArangeDescriptors) {
182 Desc.dump(OS, AddressSize: HeaderData.AddrSize);
183 OS << '\n';
184 }
185}
186