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