LLVM 20.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"
16#include <cassert>
17#include <cinttypes>
18#include <cstdint>
19#include <cstring>
20
21using namespace llvm;
22
24 uint32_t AddressSize) const {
25 OS << '[';
27 OS << ", ";
29 OS << ')';
30}
31
33 Offset = -1ULL;
34 std::memset(&HeaderData, 0, sizeof(Header));
35 ArangeDescriptors.clear();
36}
37
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(HeaderData.Length, HeaderData.Format) =
70 data.getInitialLength(offset_ptr, &Err);
71 HeaderData.Version = data.getU16(offset_ptr, &Err);
72 HeaderData.CuOffset = data.getUnsigned(
73 offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err);
74 HeaderData.AddrSize = data.getU8(offset_ptr, &Err);
75 HeaderData.SegSize = data.getU8(offset_ptr, &Err);
76 if (Err) {
78 "parsing address ranges table at offset 0x%" PRIx64
79 ": %s",
80 Offset, toString(std::move(Err)).c_str());
81 }
82
83 // Perform basic validation of the header fields.
84 uint64_t full_length =
85 dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length;
86 if (!data.isValidOffsetForDataOfSize(Offset, full_length))
88 "the length of address range table at offset "
89 "0x%" PRIx64 " exceeds section size",
90 Offset);
93 "address range table at offset 0x%" PRIx64, Offset))
94 return SizeErr;
95 if (HeaderData.SegSize != 0)
97 "non-zero segment selector size in address range "
98 "table at offset 0x%" PRIx64 " is not supported",
99 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(
109 "address range table at offset 0x%" PRIx64
110 " has length that is not a multiple of the tuple size",
111 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(
123 "address range table at offset 0x%" PRIx64
124 " has an insufficient length to contain any entries",
125 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, HeaderData.AddrSize);
140 arangeDescriptor.Length = data.getUnsigned(offset_ptr, 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 if (WarningHandler) {
148 WarningHandler(createStringError(
150 "address range table at offset 0x%" PRIx64
151 " has a premature terminator entry at offset 0x%" PRIx64,
152 Offset, EntryOffset));
153 }
154 }
155
156 ArangeDescriptors.push_back(arangeDescriptor);
157 }
158
160 "address range table at offset 0x%" PRIx64
161 " is not terminated by null entry",
162 Offset);
163}
164
166 int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format);
167 OS << "Address Range Header: "
168 << format("length = 0x%0*" PRIx64 ", ", OffsetDumpWidth, HeaderData.Length)
169 << "format = " << dwarf::FormatString(HeaderData.Format) << ", "
170 << format("version = 0x%4.4x, ", HeaderData.Version)
171 << format("cu_offset = 0x%0*" PRIx64 ", ", OffsetDumpWidth,
172 HeaderData.CuOffset)
173 << format("addr_size = 0x%2.2x, ", HeaderData.AddrSize)
174 << format("seg_size = 0x%2.2x\n", HeaderData.SegSize);
175
176 for (const auto &Desc : ArangeDescriptors) {
177 Desc.dump(OS, HeaderData.AddrSize);
178 OS << '\n';
179 }
180}
This file contains constants used for implementing Dwarf debug support.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
static Error checkAddressSizeSupported(unsigned AddressSize, std::error_code EC, char const *Fmt, const Ts &...Vals)
Definition: DWARFContext.h:417
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
std::pair< uint64_t, dwarf::DwarfFormat > getInitialLength(uint64_t *Off, Error *Err=nullptr) const
Extracts the DWARF "initial length" field, which can either be a 32-bit value smaller than 0xfffffff0...
void dump(raw_ostream &OS) const
Error extract(DWARFDataExtractor data, uint64_t *offset_ptr, function_ref< void(Error)> WarningHandler=nullptr)
void dumpAddress(raw_ostream &OS, uint64_t Address) const
uint64_t getUnsigned(uint64_t *offset_ptr, uint32_t byte_size, Error *Err=nullptr) const
Extract an unsigned integer of size byte_size from *offset_ptr.
uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
bool isValidOffsetForDataOfSize(uint64_t offset, uint64_t length) const
Test the availability of length bytes of data from offset.
Subclass of Error for the sole purpose of identifying the success path in the type system.
Definition: Error.h:335
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
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:52
StringRef FormatString(DwarfFormat Format)
Definition: Dwarf.cpp:868
uint8_t getUnitLengthFieldByteSize(DwarfFormat Format)
Get the byte size of the unit length field depending on the DWARF format.
Definition: Dwarf.h:1110
uint8_t getDwarfOffsetByteSize(DwarfFormat Format)
The size of a reference determined by the DWARF 32/64-bit format.
Definition: Dwarf.h:1071
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1291
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
const char * toString(DWARFSectionKind Kind)
void dump(raw_ostream &OS, uint32_t AddressSize) const
uint16_t Version
The DWARF version number.
uint64_t CuOffset
The offset from the beginning of the .debug_info section of the compilation unit entry referenced by ...
uint8_t AddrSize
The size in bytes of an address on the target architecture.
uint64_t Length
The total length of the entries for that set, not including the length field itself.
uint8_t SegSize
The size in bytes of a segment descriptor on the target architecture.
dwarf::DwarfFormat Format
The DWARF format of the set.
Description of the encoding of one expression Op.