LLVM 22.0.0git
DWARFListTable.h
Go to the documentation of this file.
1//===- DWARFListTable.h -----------------------------------------*- C++ -*-===//
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#ifndef LLVM_DEBUGINFO_DWARF_DWARFLISTTABLE_H
10#define LLVM_DEBUGINFO_DWARF_DWARFLISTTABLE_H
11
16#include "llvm/Support/Errc.h"
17#include "llvm/Support/Error.h"
19#include <cstdint>
20#include <map>
21#include <vector>
22
23namespace llvm {
24
25/// A base class for DWARF list entries, such as range or location list
26/// entries.
28 /// The offset at which the entry is located in the section.
30 /// The DWARF encoding (DW_RLE_* or DW_LLE_*).
32 /// The index of the section this entry belongs to.
34};
35
36/// A base class for lists of entries that are extracted from a particular
37/// section, such as range lists or location lists.
38template <typename ListEntryType> class DWARFListType {
39 using EntryType = ListEntryType;
40 using ListEntries = std::vector<EntryType>;
41
42protected:
43 ListEntries Entries;
44
45public:
46 const ListEntries &getEntries() const { return Entries; }
47 bool empty() const { return Entries.empty(); }
48 void clear() { Entries.clear(); }
50 uint64_t *OffsetPtr, StringRef SectionName,
51 StringRef ListStringName);
52};
53
54/// A class representing the header of a list table such as the range list
55/// table in the .debug_rnglists section.
57 struct Header {
58 /// The total length of the entries for this table, not including the length
59 /// field itself.
60 uint64_t Length = 0;
61 /// The DWARF version number.
62 uint16_t Version;
63 /// The size in bytes of an address on the target architecture. For
64 /// segmented addressing, this is the size of the offset portion of the
65 /// address.
66 uint8_t AddrSize;
67 /// The size in bytes of a segment selector on the target architecture.
68 /// If the target system uses a flat address space, this value is 0.
69 uint8_t SegSize;
70 /// The number of offsets that follow the header before the range lists.
71 uint32_t OffsetEntryCount;
72 };
73
74 Header HeaderData;
75 /// The table's format, either DWARF32 or DWARF64.
76 dwarf::DwarfFormat Format;
77 /// The offset at which the header (and hence the table) is located within
78 /// its section.
79 uint64_t HeaderOffset;
80 /// The name of the section the list is located in.
82 /// A characterization of the list for dumping purposes, e.g. "range" or
83 /// "location".
84 StringRef ListTypeString;
85
86public:
88 : SectionName(SectionName), ListTypeString(ListTypeString) {}
89
90 void clear() {
91 HeaderData = {};
92 }
93 uint64_t getHeaderOffset() const { return HeaderOffset; }
94 uint8_t getAddrSize() const { return HeaderData.AddrSize; }
95 uint64_t getLength() const { return HeaderData.Length; }
96 uint16_t getVersion() const { return HeaderData.Version; }
97 uint32_t getOffsetEntryCount() const { return HeaderData.OffsetEntryCount; }
99 StringRef getListTypeString() const { return ListTypeString; }
100 dwarf::DwarfFormat getFormat() const { return Format; }
101
102 /// Return the size of the table header including the length but not including
103 /// the offsets.
105 switch (Format) {
107 return 12;
109 return 20;
110 }
111 llvm_unreachable("Invalid DWARF format (expected DWARF32 or DWARF64");
112 }
113
115 DIDumpOptions DumpOpts = {}) const;
116 std::optional<uint64_t> getOffsetEntry(DataExtractor Data,
117 uint32_t Index) const {
118 if (Index >= HeaderData.OffsetEntryCount)
119 return std::nullopt;
120
121 return getOffsetEntry(Data, getHeaderOffset() + getHeaderSize(Format), Format, Index);
122 }
123
124 static std::optional<uint64_t> getOffsetEntry(DataExtractor Data,
125 uint64_t OffsetTableOffset,
126 dwarf::DwarfFormat Format,
127 uint32_t Index) {
128 uint8_t OffsetByteSize = Format == dwarf::DWARF64 ? 8 : 4;
129 uint64_t Offset = OffsetTableOffset + OffsetByteSize * Index;
130 auto R = Data.getUnsigned(&Offset, OffsetByteSize);
131 return R;
132 }
133
134 /// Extract the table header and the array of offsets.
136
137 /// Returns the length of the table, including the length field, or 0 if the
138 /// length has not been determined (e.g. because the table has not yet been
139 /// parsed, or there was a problem in parsing).
140 LLVM_ABI uint64_t length() const;
141};
142
143/// A class representing a table of lists as specified in the DWARF v5
144/// standard for location lists and range lists. The table consists of a header
145/// followed by an array of offsets into a DWARF section, followed by zero or
146/// more list entries. The list entries are kept in a map where the keys are
147/// the lists' section offsets.
148template <typename DWARFListType> class DWARFListTableBase {
150 /// A mapping between file offsets and lists. It is used to find a particular
151 /// list based on an offset (obtained from DW_AT_ranges, for example).
152 std::map<uint64_t, DWARFListType> ListMap;
153 /// This string is displayed as a heading before the list is dumped
154 /// (e.g. "ranges:").
155 StringRef HeaderString;
156
157protected:
159 StringRef ListTypeString)
160 : Header(SectionName, ListTypeString), HeaderString(HeaderString) {}
161
162public:
163 void clear() {
164 Header.clear();
165 ListMap.clear();
166 }
167 /// Extract the table header and the array of offsets.
169 return Header.extract(Data, OffsetPtr);
170 }
171 /// Extract an entire table, including all list entries.
173 /// Look up a list based on a given offset. Extract it and enter it into the
174 /// list map if necessary.
176 uint64_t Offset) const;
177
178 uint64_t getHeaderOffset() const { return Header.getHeaderOffset(); }
179 uint8_t getAddrSize() const { return Header.getAddrSize(); }
180 uint32_t getOffsetEntryCount() const { return Header.getOffsetEntryCount(); }
181 dwarf::DwarfFormat getFormat() const { return Header.getFormat(); }
182
183 void
185 llvm::function_ref<std::optional<object::SectionedAddress>(uint32_t)>
186 LookupPooledAddress,
187 DIDumpOptions DumpOpts = {}) const;
188
189 /// Return the contents of the offset entry designated by a given index.
190 std::optional<uint64_t> getOffsetEntry(DataExtractor Data,
191 uint32_t Index) const {
192 return Header.getOffsetEntry(Data, Index);
193 }
194 /// Return the size of the table header including the length but not including
195 /// the offsets. This is dependent on the table format, which is unambiguously
196 /// derived from parsing the table.
199 }
200
201 uint64_t length() { return Header.length(); }
202};
203
204template <typename DWARFListType>
206 uint64_t *OffsetPtr) {
207 clear();
208 if (Error E = extractHeaderAndOffsets(Data, OffsetPtr))
209 return E;
210
211 Data.setAddressSize(Header.getAddrSize());
212 Data = DWARFDataExtractor(Data, getHeaderOffset() + Header.length());
213 while (Data.isValidOffset(*OffsetPtr)) {
214 DWARFListType CurrentList;
215 uint64_t Off = *OffsetPtr;
216 if (Error E = CurrentList.extract(Data, getHeaderOffset(), OffsetPtr,
217 Header.getSectionName(),
218 Header.getListTypeString()))
219 return E;
220 ListMap[Off] = CurrentList;
221 }
222
223 assert(*OffsetPtr == Data.size() &&
224 "mismatch between expected length of table and length "
225 "of extracted data");
226 return Error::success();
227}
228
229template <typename ListEntryType>
231 uint64_t HeaderOffset,
232 uint64_t *OffsetPtr,
234 StringRef ListTypeString) {
235 if (*OffsetPtr < HeaderOffset || *OffsetPtr >= Data.size())
237 "invalid %s list offset 0x%" PRIx64,
238 ListTypeString.data(), *OffsetPtr);
239 Entries.clear();
240 while (Data.isValidOffset(*OffsetPtr)) {
241 ListEntryType Entry;
242 if (Error E = Entry.extract(Data, OffsetPtr))
243 return E;
244 Entries.push_back(Entry);
245 if (Entry.isSentinel())
246 return Error::success();
247 }
249 "no end of list marker detected at end of %s table "
250 "starting at offset 0x%" PRIx64,
251 SectionName.data(), HeaderOffset);
252}
253
254template <typename DWARFListType>
257 llvm::function_ref<std::optional<object::SectionedAddress>(uint32_t)>
258 LookupPooledAddress,
259 DIDumpOptions DumpOpts) const {
260 Header.dump(Data, OS, DumpOpts);
261 OS << HeaderString << "\n";
262
263 // Determine the length of the longest encoding string we have in the table,
264 // so we can align the output properly. We only need this in verbose mode.
265 size_t MaxEncodingStringLength = 0;
266 if (DumpOpts.Verbose) {
267 for (const auto &List : ListMap)
268 for (const auto &Entry : List.second.getEntries())
269 MaxEncodingStringLength =
270 std::max(MaxEncodingStringLength,
271 dwarf::RangeListEncodingString(Entry.EntryKind).size());
272 }
273
274 uint64_t CurrentBase = 0;
275 for (const auto &List : ListMap)
276 for (const auto &Entry : List.second.getEntries())
277 Entry.dump(OS, getAddrSize(), MaxEncodingStringLength, CurrentBase,
278 DumpOpts, LookupPooledAddress);
279}
280
281template <typename DWARFListType>
284 uint64_t Offset) const {
285 // Extract the list from the section and enter it into the list map.
287 if (Header.length())
288 Data = DWARFDataExtractor(Data, getHeaderOffset() + Header.length());
289 if (Error E =
290 List.extract(Data, Header.length() ? getHeaderOffset() : 0, &Offset,
291 Header.getSectionName(), Header.getListTypeString()))
292 return std::move(E);
293 return List;
294}
295
296} // end namespace llvm
297
298#endif // LLVM_DEBUGINFO_DWARF_DWARFLISTTABLE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition: Compiler.h:213
This file contains constants used for implementing Dwarf debug support.
uint32_t Index
loop extract
raw_pwrite_stream & OS
A DWARFDataExtractor (typically for an in-memory copy of an object-file section) plus a relocation ma...
A class representing a table of lists as specified in the DWARF v5 standard for location lists and ra...
uint8_t getHeaderSize() const
Return the size of the table header including the length but not including the offsets.
std::optional< uint64_t > getOffsetEntry(DataExtractor Data, uint32_t Index) const
Return the contents of the offset entry designated by a given index.
uint32_t getOffsetEntryCount() const
uint64_t getHeaderOffset() const
uint8_t getAddrSize() const
Expected< DWARFListType > findList(DWARFDataExtractor Data, uint64_t Offset) const
Look up a list based on a given offset.
dwarf::DwarfFormat getFormat() const
Error extractHeaderAndOffsets(DWARFDataExtractor Data, uint64_t *OffsetPtr)
Extract the table header and the array of offsets.
Error extract(DWARFDataExtractor Data, uint64_t *OffsetPtr)
Extract an entire table, including all list entries.
void dump(DWARFDataExtractor Data, raw_ostream &OS, llvm::function_ref< std::optional< object::SectionedAddress >(uint32_t)> LookupPooledAddress, DIDumpOptions DumpOpts={}) const
DWARFListTableBase(StringRef SectionName, StringRef HeaderString, StringRef ListTypeString)
A class representing the header of a list table such as the range list table in the ....
static std::optional< uint64_t > getOffsetEntry(DataExtractor Data, uint64_t OffsetTableOffset, dwarf::DwarfFormat Format, uint32_t Index)
uint64_t getLength() const
DWARFListTableHeader(StringRef SectionName, StringRef ListTypeString)
static uint8_t getHeaderSize(dwarf::DwarfFormat Format)
Return the size of the table header including the length but not including the offsets.
std::optional< uint64_t > getOffsetEntry(DataExtractor Data, uint32_t Index) const
LLVM_ABI void dump(DataExtractor Data, raw_ostream &OS, DIDumpOptions DumpOpts={}) const
dwarf::DwarfFormat getFormat() const
uint32_t getOffsetEntryCount() const
StringRef getSectionName() const
uint64_t getHeaderOffset() const
LLVM_ABI uint64_t length() const
Returns the length of the table, including the length field, or 0 if the length has not been determin...
StringRef getListTypeString() const
uint8_t getAddrSize() const
uint16_t getVersion() const
A base class for lists of entries that are extracted from a particular section, such as range lists o...
const ListEntries & getEntries() const
Error extract(DWARFDataExtractor Data, uint64_t HeaderOffset, uint64_t *OffsetPtr, StringRef SectionName, StringRef ListStringName)
bool empty() const
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
Tagged union holding either a T or a Error.
Definition: Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:154
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:148
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 RangeListEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:610
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:92
@ DWARF64
Definition: Dwarf.h:92
@ DWARF32
Definition: Dwarf.h:92
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
@ Length
Definition: DWP.cpp:477
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1305
@ illegal_byte_sequence
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:196
A base class for DWARF list entries, such as range or location list entries.
uint64_t SectionIndex
The index of the section this entry belongs to.
uint8_t EntryKind
The DWARF encoding (DW_RLE_* or DW_LLE_*).
uint64_t Offset
The offset at which the entry is located in the section.