LLVM  15.0.0git
DWARFUnitIndex.h
Go to the documentation of this file.
1 //===- DWARFUnitIndex.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_DWARFUNITINDEX_H
10 #define LLVM_DEBUGINFO_DWARF_DWARFUNITINDEX_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/StringRef.h"
14 #include <cstdint>
15 #include <memory>
16 
17 namespace llvm {
18 
19 class raw_ostream;
20 class DataExtractor;
21 
22 /// The enum of section identifiers to be used in internal interfaces.
23 ///
24 /// Pre-standard implementation of package files defined a number of section
25 /// identifiers with values that clash definitions in the DWARFv5 standard.
26 /// See https://gcc.gnu.org/wiki/DebugFissionDWP and Section 7.3.5.3 in DWARFv5.
27 ///
28 /// The following identifiers are the same in the proposal and in DWARFv5:
29 /// - DW_SECT_INFO = 1 (.debug_info.dwo)
30 /// - DW_SECT_ABBREV = 3 (.debug_abbrev.dwo)
31 /// - DW_SECT_LINE = 4 (.debug_line.dwo)
32 /// - DW_SECT_STR_OFFSETS = 6 (.debug_str_offsets.dwo)
33 ///
34 /// The following identifiers are defined only in DWARFv5:
35 /// - DW_SECT_LOCLISTS = 5 (.debug_loclists.dwo)
36 /// - DW_SECT_RNGLISTS = 8 (.debug_rnglists.dwo)
37 ///
38 /// The following identifiers are defined only in the GNU proposal:
39 /// - DW_SECT_TYPES = 2 (.debug_types.dwo)
40 /// - DW_SECT_LOC = 5 (.debug_loc.dwo)
41 /// - DW_SECT_MACINFO = 7 (.debug_macinfo.dwo)
42 ///
43 /// DW_SECT_MACRO for the .debug_macro.dwo section is defined in both standards,
44 /// but with different values, 8 in GNU and 7 in DWARFv5.
45 ///
46 /// This enum defines constants to represent the identifiers of both sets.
47 /// For DWARFv5 ones, the values are the same as defined in the standard.
48 /// For pre-standard ones that correspond to sections being deprecated in
49 /// DWARFv5, the values are chosen arbitrary and a tag "_EXT_" is added to
50 /// the names.
51 ///
52 /// The enum is for internal use only. The user should not expect the values
53 /// to correspond to any input/output constants. Special conversion functions,
54 /// serializeSectionKind() and deserializeSectionKind(), should be used for
55 /// the translation.
57  /// Denotes a value read from an index section that does not correspond
58  /// to any of the supported standards.
60 #define HANDLE_DW_SECT(ID, NAME) DW_SECT_##NAME = ID,
61 #include "llvm/BinaryFormat/Dwarf.def"
65 };
66 
67 inline const char *toString(DWARFSectionKind Kind) {
68  switch (Kind) {
70  return "Unknown DW_SECT value 0";
71 #define STRINGIZE(X) #X
72 #define HANDLE_DW_SECT(ID, NAME) \
73  case DW_SECT_##NAME: \
74  return "DW_SECT_" STRINGIZE(NAME);
75 #include "llvm/BinaryFormat/Dwarf.def"
76  case DW_SECT_EXT_TYPES:
77  return "DW_SECT_TYPES";
78  case DW_SECT_EXT_LOC:
79  return "DW_SECT_LOC";
81  return "DW_SECT_MACINFO";
82  }
83  llvm_unreachable("unknown DWARFSectionKind");
84 }
85 
86 /// Convert the internal value for a section kind to an on-disk value.
87 ///
88 /// The conversion depends on the version of the index section.
89 /// IndexVersion is expected to be either 2 for pre-standard GNU proposal
90 /// or 5 for DWARFv5 package file.
91 uint32_t serializeSectionKind(DWARFSectionKind Kind, unsigned IndexVersion);
92 
93 /// Convert a value read from an index section to the internal representation.
94 ///
95 /// The conversion depends on the index section version, which is expected
96 /// to be either 2 for pre-standard GNU proposal or 5 for DWARFv5 package file.
98 
100  struct Header {
102  uint32_t NumColumns;
103  uint32_t NumUnits;
104  uint32_t NumBuckets = 0;
105 
106  bool parse(DataExtractor IndexData, uint64_t *OffsetPtr);
107  void dump(raw_ostream &OS) const;
108  };
109 
110 public:
111  class Entry {
112  public:
116  };
117 
118  private:
119  const DWARFUnitIndex *Index;
120  uint64_t Signature;
121  std::unique_ptr<SectionContribution[]> Contributions;
122  friend class DWARFUnitIndex;
123 
124  public:
126  const SectionContribution *getContribution() const;
127 
129  return Contributions.get();
130  }
131 
132  uint64_t getSignature() const { return Signature; }
133  };
134 
135 private:
136  struct Header Header;
137 
138  DWARFSectionKind InfoColumnKind;
139  int InfoColumn = -1;
140  std::unique_ptr<DWARFSectionKind[]> ColumnKinds;
141  // This is a parallel array of section identifiers as they read from the input
142  // file. The mapping from raw values to DWARFSectionKind is not revertable in
143  // case of unknown identifiers, so we keep them here.
144  std::unique_ptr<uint32_t[]> RawSectionIds;
145  std::unique_ptr<Entry[]> Rows;
146  mutable std::vector<Entry *> OffsetLookup;
147 
148  static StringRef getColumnHeader(DWARFSectionKind DS);
149 
150  bool parseImpl(DataExtractor IndexData);
151 
152 public:
154  : InfoColumnKind(InfoColumnKind) {}
155 
156  explicit operator bool() const { return Header.NumBuckets; }
157 
158  bool parse(DataExtractor IndexData);
159  void dump(raw_ostream &OS) const;
160 
161  uint32_t getVersion() const { return Header.Version; }
162 
163  const Entry *getFromOffset(uint32_t Offset) const;
164  const Entry *getFromHash(uint64_t Offset) const;
165 
167  return makeArrayRef(ColumnKinds.get(), Header.NumColumns);
168  }
169 
171  return makeArrayRef(Rows.get(), Header.NumBuckets);
172  }
173 };
174 
175 } // end namespace llvm
176 
177 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNITINDEX_H
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::DWARFUnitIndex::DWARFUnitIndex
DWARFUnitIndex(DWARFSectionKind InfoColumnKind)
Definition: DWARFUnitIndex.h:153
StringRef.h
llvm::DWARFUnitIndex::Entry::SectionContribution
Definition: DWARFUnitIndex.h:113
llvm::DW_SECT_EXT_unknown
@ DW_SECT_EXT_unknown
Denotes a value read from an index section that does not correspond to any of the supported standards...
Definition: DWARFUnitIndex.h:59
llvm::DWARFUnitIndex::Entry
Definition: DWARFUnitIndex.h:111
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:79
llvm::DWARFUnitIndex::dump
void dump(raw_ostream &OS) const
Definition: DWARFUnitIndex.cpp:215
llvm::serializeSectionKind
uint32_t serializeSectionKind(DWARFSectionKind Kind, unsigned IndexVersion)
Convert the internal value for a section kind to an on-disk value.
Definition: DWARFUnitIndex.cpp:42
llvm::DWARFUnitIndex::getFromHash
const Entry * getFromHash(uint64_t Offset) const
Definition: DWARFUnitIndex.cpp:285
llvm::DWARFSectionKind
DWARFSectionKind
The enum of section identifiers to be used in internal interfaces.
Definition: DWARFUnitIndex.h:56
llvm::DWARFUnitIndex
Definition: DWARFUnitIndex.h:99
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::DWARFUnitIndex::parse
bool parse(DataExtractor IndexData)
Definition: DWARFUnitIndex.cpp:119
llvm::DW_SECT_EXT_TYPES
@ DW_SECT_EXT_TYPES
Definition: DWARFUnitIndex.h:62
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::DWARFUnitIndex::getFromOffset
const Entry * getFromOffset(uint32_t Offset) const
Definition: DWARFUnitIndex.cpp:262
llvm::IndexedInstrProf::Version
const uint64_t Version
Definition: InstrProf.h:1027
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
uint64_t
llvm::DW_SECT_EXT_LOC
@ DW_SECT_EXT_LOC
Definition: DWARFUnitIndex.h:63
ArrayRef.h
llvm::DWARFUnitIndex::Entry::SectionContribution::Length
uint32_t Length
Definition: DWARFUnitIndex.h:115
llvm::SIInstrFlags::DS
@ DS
Definition: SIDefines.h:60
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::DWARFUnitIndex::Entry::getSignature
uint64_t getSignature() const
Definition: DWARFUnitIndex.h:132
uint32_t
llvm::DW_SECT_EXT_MACINFO
@ DW_SECT_EXT_MACINFO
Definition: DWARFUnitIndex.h:64
llvm::deserializeSectionKind
DWARFSectionKind deserializeSectionKind(uint32_t Value, unsigned IndexVersion)
Convert a value read from an index section to the internal representation.
Definition: DWARFUnitIndex.cpp:68
llvm::toString
const char * toString(DWARFSectionKind Kind)
Definition: DWARFUnitIndex.h:67
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::DWARFUnitIndex::getColumnKinds
ArrayRef< DWARFSectionKind > getColumnKinds() const
Definition: DWARFUnitIndex.h:166
llvm::DWARFUnitIndex::Entry::SectionContribution::Offset
uint32_t Offset
Definition: DWARFUnitIndex.h:114
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:475
llvm::DWARFUnitIndex::Entry::getContributions
const SectionContribution * getContributions() const
Definition: DWARFUnitIndex.h:128
llvm::DWARFUnitIndex::getRows
ArrayRef< Entry > getRows() const
Definition: DWARFUnitIndex.h:170
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DWARFUnitIndex::getVersion
uint32_t getVersion() const
Definition: DWARFUnitIndex.h:161
llvm::DWARFUnitIndex::Entry::getContribution
const SectionContribution * getContribution() const
Definition: DWARFUnitIndex.cpp:257