LLVM 23.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"
14#include "llvm/Support/Format.h"
18#include <cassert>
19#include <cinttypes>
20#include <cstdint>
21#include <cstring>
22
23using namespace llvm;
24
26 uint32_t AddressSize) const {
27 OS << '[';
28 DWARFFormValue::dumpAddress(OS, AddressSize, Address);
29 OS << ", ";
31 OS << ')';
32}
33
35 Offset = -1ULL;
36 std::memset(&HeaderData, 0, sizeof(Header));
37 ArangeDescriptors.clear();
38}
39
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(HeaderData.Length, HeaderData.Format) =
72 data.getInitialLength(offset_ptr, &Err);
73 HeaderData.Version = data.getU16(offset_ptr, &Err);
74 HeaderData.CuOffset = data.getUnsigned(
75 offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err);
76 HeaderData.AddrSize = data.getU8(offset_ptr, &Err);
77 HeaderData.SegSize = data.getU8(offset_ptr, &Err);
78 if (Err) {
80 "parsing address ranges table at offset 0x%" PRIx64
81 ": %s",
82 Offset, toString(std::move(Err)).c_str());
83 }
84
85 // Perform basic validation of the header fields.
86 uint64_t full_length =
87 dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length;
88 if (!data.isValidOffsetForDataOfSize(Offset, full_length))
90 "the length of address range table at offset "
91 "0x%" PRIx64 " exceeds section size",
92 Offset);
94 HeaderData.AddrSize, errc::invalid_argument,
95 "address range table at offset 0x%" PRIx64, Offset))
96 return SizeErr;
97 if (HeaderData.SegSize != 0)
99 "non-zero segment selector size in address range "
100 "table at offset 0x%" PRIx64 " is not supported",
101 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(
111 "address range table at offset 0x%" PRIx64
112 " has length that is not a multiple of the tuple size",
113 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(
125 "address range table at offset 0x%" PRIx64
126 " has an insufficient length to contain any entries",
127 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, HeaderData.AddrSize);
142 arangeDescriptor.Length = data.getUnsigned(offset_ptr, 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(
152 "address range table at offset 0x%" PRIx64
153 " has a premature terminator entry at offset 0x%" PRIx64,
154 Offset, EntryOffset));
155 }
156 }
157
158 ArangeDescriptors.push_back(arangeDescriptor);
159 }
160
162 "address range table at offset 0x%" PRIx64
163 " is not terminated by null entry",
164 Offset);
165}
166
168 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format);
169 OS << "Address Range Header: "
170 << formatv("length = 0x{0:x-}, ",
171 fmt_align(HeaderData.Length, AlignStyle::Right, OffsetDumpWidth,
172 '0'))
173 << "format = " << dwarf::FormatString(HeaderData.Format) << ", "
174 << formatv("version = {0:x+4}, ", HeaderData.Version)
175 << formatv("cu_offset = 0x{0:x-}, ",
176 fmt_align(HeaderData.CuOffset, AlignStyle::Right,
177 OffsetDumpWidth, '0'))
178 << formatv("addr_size = {0:x+2}, ", HeaderData.AddrSize)
179 << formatv("seg_size = {0:x+2}\n", HeaderData.SegSize);
180
181 for (const auto &Desc : ArangeDescriptors) {
182 Desc.dump(OS, HeaderData.AddrSize);
183 OS << '\n';
184 }
185}
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:1023
uint8_t getUnitLengthFieldByteSize(DwarfFormat Format)
Get the byte size of the unit length field depending on the DWARF format.
Definition Dwarf.h:1139
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition Dwarf.h:1097
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:1305
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