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