LLVM  13.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 
15 #include "llvm/Support/Errc.h"
16 #include "llvm/Support/Error.h"
17 #include "llvm/Support/Format.h"
19 #include <cstdint>
20 #include <map>
21 #include <vector>
22 
23 namespace 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.
29  uint64_t Offset;
30  /// The DWARF encoding (DW_RLE_* or DW_LLE_*).
31  uint8_t EntryKind;
32  /// The index of the section this entry belongs to.
33  uint64_t SectionIndex;
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.
38 template <typename ListEntryType> class DWARFListType {
39  using EntryType = ListEntryType;
40  using ListEntries = std::vector<EntryType>;
41 
42 protected:
43  ListEntries Entries;
44 
45 public:
46  const ListEntries &getEntries() const { return Entries; }
47  bool empty() const { return Entries.empty(); }
48  void clear() { Entries.clear(); }
49  Error extract(DWARFDataExtractor Data, uint64_t HeaderOffset,
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.
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 
86 public:
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  StringRef getSectionName() const { return SectionName; }
98  StringRef getListTypeString() const { return ListTypeString; }
99  dwarf::DwarfFormat getFormat() const { return Format; }
100 
101  /// Return the size of the table header including the length but not including
102  /// the offsets.
103  static uint8_t getHeaderSize(dwarf::DwarfFormat Format) {
104  switch (Format) {
106  return 12;
108  return 20;
109  }
110  llvm_unreachable("Invalid DWARF format (expected DWARF32 or DWARF64");
111  }
112 
114  DIDumpOptions DumpOpts = {}) const;
116  if (Index >= HeaderData.OffsetEntryCount)
117  return None;
118 
119  return getOffsetEntry(Data, getHeaderOffset() + getHeaderSize(Format), Format, Index);
120  }
121 
123  uint64_t OffsetTableOffset,
124  dwarf::DwarfFormat Format,
125  uint32_t Index) {
126  uint8_t OffsetByteSize = Format == dwarf::DWARF64 ? 8 : 4;
127  uint64_t Offset = OffsetTableOffset + OffsetByteSize * Index;
128  auto R = Data.getUnsigned(&Offset, OffsetByteSize);
129  return R;
130  }
131 
132  /// Extract the table header and the array of offsets.
133  Error extract(DWARFDataExtractor Data, uint64_t *OffsetPtr);
134 
135  /// Returns the length of the table, including the length field, or 0 if the
136  /// length has not been determined (e.g. because the table has not yet been
137  /// parsed, or there was a problem in parsing).
138  uint64_t length() const;
139 };
140 
141 /// A class representing a table of lists as specified in the DWARF v5
142 /// standard for location lists and range lists. The table consists of a header
143 /// followed by an array of offsets into a DWARF section, followed by zero or
144 /// more list entries. The list entries are kept in a map where the keys are
145 /// the lists' section offsets.
146 template <typename DWARFListType> class DWARFListTableBase {
147  DWARFListTableHeader Header;
148  /// A mapping between file offsets and lists. It is used to find a particular
149  /// list based on an offset (obtained from DW_AT_ranges, for example).
150  std::map<uint64_t, DWARFListType> ListMap;
151  /// This string is displayed as a heading before the list is dumped
152  /// (e.g. "ranges:").
153  StringRef HeaderString;
154 
155 protected:
157  StringRef ListTypeString)
158  : Header(SectionName, ListTypeString), HeaderString(HeaderString) {}
159 
160 public:
161  void clear() {
162  Header.clear();
163  ListMap.clear();
164  }
165  /// Extract the table header and the array of offsets.
167  return Header.extract(Data, OffsetPtr);
168  }
169  /// Extract an entire table, including all list entries.
170  Error extract(DWARFDataExtractor Data, uint64_t *OffsetPtr);
171  /// Look up a list based on a given offset. Extract it and enter it into the
172  /// list map if necessary.
174  uint64_t Offset) const;
175 
176  uint64_t getHeaderOffset() const { return Header.getHeaderOffset(); }
177  uint8_t getAddrSize() const { return Header.getAddrSize(); }
178  dwarf::DwarfFormat getFormat() const { return Header.getFormat(); }
179 
182  LookupPooledAddress,
183  DIDumpOptions DumpOpts = {}) const;
184 
185  /// Return the contents of the offset entry designated by a given index.
187  return Header.getOffsetEntry(Data, Index);
188  }
189  /// Return the size of the table header including the length but not including
190  /// the offsets. This is dependent on the table format, which is unambiguously
191  /// derived from parsing the table.
192  uint8_t getHeaderSize() const {
194  }
195 
196  uint64_t length() { return Header.length(); }
197 };
198 
199 template <typename DWARFListType>
201  uint64_t *OffsetPtr) {
202  clear();
203  if (Error E = extractHeaderAndOffsets(Data, OffsetPtr))
204  return E;
205 
206  Data.setAddressSize(Header.getAddrSize());
207  Data = DWARFDataExtractor(Data, getHeaderOffset() + Header.length());
208  while (Data.isValidOffset(*OffsetPtr)) {
209  DWARFListType CurrentList;
210  uint64_t Off = *OffsetPtr;
211  if (Error E = CurrentList.extract(Data, getHeaderOffset(), OffsetPtr,
212  Header.getSectionName(),
213  Header.getListTypeString()))
214  return E;
215  ListMap[Off] = CurrentList;
216  }
217 
218  assert(*OffsetPtr == Data.size() &&
219  "mismatch between expected length of table and length "
220  "of extracted data");
221  return Error::success();
222 }
223 
224 template <typename ListEntryType>
226  uint64_t HeaderOffset,
227  uint64_t *OffsetPtr,
229  StringRef ListTypeString) {
230  if (*OffsetPtr < HeaderOffset || *OffsetPtr >= Data.size())
232  "invalid %s list offset 0x%" PRIx64,
233  ListTypeString.data(), *OffsetPtr);
234  Entries.clear();
235  while (Data.isValidOffset(*OffsetPtr)) {
236  ListEntryType Entry;
237  if (Error E = Entry.extract(Data, OffsetPtr))
238  return E;
239  Entries.push_back(Entry);
240  if (Entry.isSentinel())
241  return Error::success();
242  }
244  "no end of list marker detected at end of %s table "
245  "starting at offset 0x%" PRIx64,
246  SectionName.data(), HeaderOffset);
247 }
248 
249 template <typename DWARFListType>
253  LookupPooledAddress,
254  DIDumpOptions DumpOpts) const {
255  Header.dump(Data, OS, DumpOpts);
256  OS << HeaderString << "\n";
257 
258  // Determine the length of the longest encoding string we have in the table,
259  // so we can align the output properly. We only need this in verbose mode.
260  size_t MaxEncodingStringLength = 0;
261  if (DumpOpts.Verbose) {
262  for (const auto &List : ListMap)
263  for (const auto &Entry : List.second.getEntries())
264  MaxEncodingStringLength =
265  std::max(MaxEncodingStringLength,
266  dwarf::RangeListEncodingString(Entry.EntryKind).size());
267  }
268 
269  uint64_t CurrentBase = 0;
270  for (const auto &List : ListMap)
271  for (const auto &Entry : List.second.getEntries())
272  Entry.dump(OS, getAddrSize(), MaxEncodingStringLength, CurrentBase,
273  DumpOpts, LookupPooledAddress);
274 }
275 
276 template <typename DWARFListType>
279  uint64_t Offset) const {
280  // Extract the list from the section and enter it into the list map.
282  if (Header.length())
283  Data = DWARFDataExtractor(Data, getHeaderOffset() + Header.length());
284  if (Error E =
285  List.extract(Data, Header.length() ? getHeaderOffset() : 0, &Offset,
286  Header.getSectionName(), Header.getListTypeString()))
287  return std::move(E);
288  return List;
289 }
290 
291 } // end namespace llvm
292 
293 #endif // LLVM_DEBUGINFO_DWARF_DWARFLISTTABLE_H
llvm::errc::invalid_argument
@ invalid_argument
llvm
Definition: AllocatorList.h:23
llvm::DWARFListEntryBase::Offset
uint64_t Offset
The offset at which the entry is located in the section.
Definition: DWARFListTable.h:29
llvm::DWARFListEntryBase::EntryKind
uint8_t EntryKind
The DWARF encoding (DW_RLE_* or DW_LLE_*).
Definition: DWARFListTable.h:31
llvm::DWARFListTableHeader::getListTypeString
StringRef getListTypeString() const
Definition: DWARFListTable.h:98
llvm::DWARFListType
A base class for lists of entries that are extracted from a particular section, such as range lists o...
Definition: DWARFListTable.h:38
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
Error.h
llvm::DWARFListTableHeader::getVersion
uint16_t getVersion() const
Definition: DWARFListTable.h:96
Errc.h
llvm::DWARFListTableBase::getAddrSize
uint8_t getAddrSize() const
Definition: DWARFListTable.h:177
llvm::dwarf::RangeListEncodingString
StringRef RangeListEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:514
llvm::DWARFListTableHeader::dump
void dump(DataExtractor Data, raw_ostream &OS, DIDumpOptions DumpOpts={}) const
Definition: DWARFListTable.cpp:78
llvm::Optional< uint64_t >
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::DWARFListEntryBase
A base class for DWARF list entries, such as range or location list entries.
Definition: DWARFListTable.h:27
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::DWARFListTableBase::dump
void dump(DWARFDataExtractor Data, raw_ostream &OS, llvm::function_ref< Optional< object::SectionedAddress >(uint32_t)> LookupPooledAddress, DIDumpOptions DumpOpts={}) const
Definition: DWARFListTable.h:250
llvm::DWARFDataExtractor
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Definition: DWARFDataExtractor.h:21
llvm::DWARFListType::Entries
ListEntries Entries
Definition: DWARFListTable.h:43
Format.h
llvm::DWARFListType::clear
void clear()
Definition: DWARFListTable.h:48
llvm::DWARFListTableHeader::length
uint64_t length() const
Returns the length of the table, including the length field, or 0 if the length has not been determin...
Definition: DWARFListTable.cpp:105
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::DWARFListType::getEntries
const ListEntries & getEntries() const
Definition: DWARFListTable.h:46
llvm::DWARFListType::extract
Error extract(DWARFDataExtractor Data, uint64_t HeaderOffset, uint64_t *OffsetPtr, StringRef SectionName, StringRef ListStringName)
Definition: DWARFListTable.h:225
clear
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:233
llvm::DWARFListTableHeader::getSectionName
StringRef getSectionName() const
Definition: DWARFListTable.h:97
llvm::DIDumpOptions::Verbose
bool Verbose
Definition: DIContext.h:198
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::DWARFListTableBase::extractHeaderAndOffsets
Error extractHeaderAndOffsets(DWARFDataExtractor Data, uint64_t *OffsetPtr)
Extract the table header and the array of offsets.
Definition: DWARFListTable.h:166
llvm::DWARFListTableBase::length
uint64_t length()
Definition: DWARFListTable.h:196
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::IndexedInstrProf::Version
const uint64_t Version
Definition: InstrProf.h:991
llvm::dwarf::DwarfFormat
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:92
llvm::None
const NoneType None
Definition: None.h:23
llvm::DWARFListType::empty
bool empty() const
Definition: DWARFListTable.h:47
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
llvm::errc::illegal_byte_sequence
@ illegal_byte_sequence
llvm::DWARFListTableBase::getHeaderOffset
uint64_t getHeaderOffset() const
Definition: DWARFListTable.h:176
llvm::DWARFListTableBase
A class representing a table of lists as specified in the DWARF v5 standard for location lists and ra...
Definition: DWARFListTable.h:146
llvm::DWARFListTableHeader::getHeaderSize
static uint8_t getHeaderSize(dwarf::DwarfFormat Format)
Return the size of the table header including the length but not including the offsets.
Definition: DWARFListTable.h:103
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::DWARFListTableBase::getHeaderSize
uint8_t getHeaderSize() const
Return the size of the table header including the length but not including the offsets.
Definition: DWARFListTable.h:192
llvm::DWARFListTableHeader::clear
void clear()
Definition: DWARFListTable.h:90
llvm::DWARFListTableHeader::getHeaderOffset
uint64_t getHeaderOffset() const
Definition: DWARFListTable.h:93
llvm::DWARFListTableHeader::getOffsetEntry
Optional< uint64_t > getOffsetEntry(DataExtractor Data, uint32_t Index) const
Definition: DWARFListTable.h:115
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DWARFListTableBase::getOffsetEntry
Optional< uint64_t > getOffsetEntry(DataExtractor Data, uint32_t Index) const
Return the contents of the offset entry designated by a given index.
Definition: DWARFListTable.h:186
llvm::DWARFListTableHeader::getLength
uint64_t getLength() const
Definition: DWARFListTable.h:95
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::DWARFListTableBase::DWARFListTableBase
DWARFListTableBase(StringRef SectionName, StringRef HeaderString, StringRef ListTypeString)
Definition: DWARFListTable.h:156
llvm::DWARFListTableHeader::getFormat
dwarf::DwarfFormat getFormat() const
Definition: DWARFListTable.h:99
llvm::DWARFListTableBase::getFormat
dwarf::DwarfFormat getFormat() const
Definition: DWARFListTable.h:178
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
Dwarf.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
uint32_t
llvm::DWARFListTableHeader
A class representing the header of a list table such as the range list table in the ....
Definition: DWARFListTable.h:56
DIContext.h
llvm::DWARFListTableBase::extract
Error extract(DWARFDataExtractor Data, uint64_t *OffsetPtr)
Extract an entire table, including all list entries.
Definition: DWARFListTable.h:200
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1202
llvm::DWARFListEntryBase::SectionIndex
uint64_t SectionIndex
The index of the section this entry belongs to.
Definition: DWARFListTable.h:33
uint16_t
llvm::SectionName
Definition: DWARFSection.h:21
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::DWARFListTableHeader::DWARFListTableHeader
DWARFListTableHeader(StringRef SectionName, StringRef ListTypeString)
Definition: DWARFListTable.h:87
llvm::DWARFListTableBase::findList
Expected< DWARFListType > findList(DWARFDataExtractor Data, uint64_t Offset) const
Look up a list based on a given offset.
Definition: DWARFListTable.h:278
llvm::DataExtractor
Definition: DataExtractor.h:41
List
const NodeList & List
Definition: RDFGraph.cpp:201
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:152
llvm::dwarf::DWARF64
@ DWARF64
Definition: Dwarf.h:92
llvm::DWARFListTableHeader::getOffsetEntry
static Optional< uint64_t > getOffsetEntry(DataExtractor Data, uint64_t OffsetTableOffset, dwarf::DwarfFormat Format, uint32_t Index)
Definition: DWARFListTable.h:122
raw_ostream.h
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:160
llvm::DWARFListTableHeader::getAddrSize
uint8_t getAddrSize() const
Definition: DWARFListTable.h:94
llvm::DWARFListTableBase::clear
void clear()
Definition: DWARFListTable.h:161
llvm::dwarf::DWARF32
@ DWARF32
Definition: Dwarf.h:92
llvm::DWARFListTableHeader::extract
Error extract(DWARFDataExtractor Data, uint64_t *OffsetPtr)
Extract the table header and the array of offsets.
Definition: DWARFListTable.cpp:18
DWARFDataExtractor.h
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:187