LLVM 24.0.0git
DWARFDebugArangeSet.cpp
Go to the documentation of this file.
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
13#include "llvm/Support/Errc.h"
17#include <cassert>
18#include <cinttypes>
19#include <cstdint>
20#include <cstring>
21
22using namespace llvm;
23
25 uint32_t AddressSize) const {
26 OS << '[';
27 DWARFFormValue::dumpAddress(OS, AddressSize, Address);
28 OS << ", ";
30 OS << ')';
31}
32
34 Offset = -1ULL;
35 std::memset(&HeaderData, 0, sizeof(Header));
36 ArangeDescriptors.clear();
37}
38
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(HeaderData.Length, HeaderData.Format) =
71 data.getInitialLength(offset_ptr, &Err);
72 HeaderData.Version = data.getU16(offset_ptr, &Err);
73 HeaderData.CuOffset = data.getUnsigned(
74 offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err);
75 HeaderData.AddrSize = data.getU8(offset_ptr, &Err);
76 HeaderData.SegSize = data.getU8(offset_ptr, &Err);
77 if (Err) {
79 "parsing address ranges table at offset 0x%" PRIx64
80 ": %s",
81 Offset, toString(std::move(Err)).c_str());
82 }
83
84 // Perform basic validation of the header fields.
85 uint64_t full_length =
86 dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length;
87 if (!data.isValidOffsetForDataOfSize(Offset, full_length))
89 "the length of address range table at offset "
90 "0x%" PRIx64 " exceeds section size",
91 Offset);
93 HeaderData.AddrSize, errc::invalid_argument,
94 "address range table at offset 0x%" PRIx64, Offset))
95 return SizeErr;
96 if (HeaderData.SegSize != 0)
98 "non-zero segment selector size in address range "
99 "table at offset 0x%" PRIx64 " is not supported",
100 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(
110 "address range table at offset 0x%" PRIx64
111 " has length that is not a multiple of the tuple size",
112 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(
124 "address range table at offset 0x%" PRIx64
125 " has an insufficient length to contain any entries",
126 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, HeaderData.AddrSize);
141 arangeDescriptor.Length = data.getUnsigned(offset_ptr, 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(
151 "address range table at offset 0x%" PRIx64
152 " has a premature terminator entry at offset 0x%" PRIx64,
153 Offset, EntryOffset));
154 }
155 }
156
157 ArangeDescriptors.push_back(arangeDescriptor);
158 }
159
161 "address range table at offset 0x%" PRIx64
162 " is not terminated by null entry",
163 Offset);
164}
165
167 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format);
168 OS << "Address Range Header: "
169 << formatv("length = 0x{0:x-}, ",
170 fmt_align(HeaderData.Length, AlignStyle::Right, OffsetDumpWidth,
171 '0'))
172 << "format = " << dwarf::FormatString(HeaderData.Format) << ", "
173 << formatv("version = {0:x+4}, ", HeaderData.Version)
174 << formatv("cu_offset = 0x{0:x-}, ",
175 fmt_align(HeaderData.CuOffset, AlignStyle::Right,
176 OffsetDumpWidth, '0'))
177 << formatv("addr_size = {0:x+2}, ", HeaderData.AddrSize)
178 << formatv("seg_size = {0:x+2}\n", HeaderData.SegSize);
179
180 for (const auto &Desc : ArangeDescriptors) {
181 Desc.dump(OS, HeaderData.AddrSize);
182 OS << '\n';
183 }
184}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains constants used for implementing Dwarf debug support.
static Split data
static Error checkAddressSizeSupported(unsigned AddressSize, std::error_code EC, char const *Fmt, const Ts &...Vals)
A DWARFDataExtractor (typically for an in-memory copy of an object-file section) plus a relocation ma...
LLVM_ABI void dump(raw_ostream &OS) const
LLVM_ABI Error extract(DWARFDataExtractor data, uint64_t *offset_ptr, function_ref< void(Error)> WarningHandler=nullptr)
LLVM_ABI void dumpAddress(raw_ostream &OS, uint64_t Address) const
Subclass of Error for the sole purpose of identifying the success path in the type system.
Definition Error.h:334
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI StringRef FormatString(DwarfFormat Format)
Definition Dwarf.cpp:1062
uint8_t getUnitLengthFieldByteSize(DwarfFormat Format)
Get the byte size of the unit length field depending on the DWARF format.
Definition Dwarf.h:1228
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition Dwarf.h:1186
This is an optimization pass for GlobalISel generic memory operations.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
Op::Description Desc
@ not_supported
Definition Errc.h:69
@ invalid_argument
Definition Errc.h:56
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
support::detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
LLVM_ABI void dump(raw_ostream &OS, uint32_t AddressSize) const