LLVM  5.0.0
DWARFAcceleratorTable.cpp
Go to the documentation of this file.
1 //===- DWARFAcceleratorTable.cpp ------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 
12 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Format.h"
20 #include <cstddef>
21 #include <cstdint>
22 #include <utility>
23 
24 using namespace llvm;
25 
27  uint32_t Offset = 0;
28 
29  // Check that we can at least read the header.
30  if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4))
31  return false;
32 
33  Hdr.Magic = AccelSection.getU32(&Offset);
34  Hdr.Version = AccelSection.getU16(&Offset);
35  Hdr.HashFunction = AccelSection.getU16(&Offset);
36  Hdr.NumBuckets = AccelSection.getU32(&Offset);
37  Hdr.NumHashes = AccelSection.getU32(&Offset);
38  Hdr.HeaderDataLength = AccelSection.getU32(&Offset);
39 
40  // Check that we can read all the hashes and offsets from the
41  // section (see SourceLevelDebugging.rst for the structure of the index).
42  if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength +
43  Hdr.NumBuckets*4 + Hdr.NumHashes*8))
44  return false;
45 
46  HdrData.DIEOffsetBase = AccelSection.getU32(&Offset);
47  uint32_t NumAtoms = AccelSection.getU32(&Offset);
48 
49  for (unsigned i = 0; i < NumAtoms; ++i) {
50  uint16_t AtomType = AccelSection.getU16(&Offset);
51  auto AtomForm = static_cast<dwarf::Form>(AccelSection.getU16(&Offset));
52  HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm));
53  }
54 
55  return true;
56 }
57 
58 uint32_t DWARFAcceleratorTable::getNumBuckets() { return Hdr.NumBuckets; }
59 uint32_t DWARFAcceleratorTable::getNumHashes() { return Hdr.NumHashes; }
62  return Hdr.HeaderDataLength;
63 }
64 
65 ArrayRef<std::pair<DWARFAcceleratorTable::HeaderData::AtomType,
68  return HdrData.Atoms;
69 }
70 
72  for (auto Atom : getAtomsDesc()) {
73  DWARFFormValue FormValue(Atom.second);
74  switch (Atom.first) {
76  if ((!FormValue.isFormClass(DWARFFormValue::FC_Constant) &&
77  !FormValue.isFormClass(DWARFFormValue::FC_Flag)) ||
78  FormValue.getForm() == dwarf::DW_FORM_sdata)
79  return false;
80  default:
81  break;
82  }
83  }
84  return true;
85 }
86 
89 
90  for (auto Atom : getAtomsDesc()) {
91  DWARFFormValue FormValue(Atom.second);
92  FormValue.extractValue(AccelSection, &HashDataOffset, NULL);
93  switch (Atom.first) {
95  DieOffset = *FormValue.getAsUnsignedConstant();
96  break;
97  default:
98  break;
99  }
100  }
101  return DieOffset;
102 }
103 
105  // Dump the header.
106  OS << "Magic = " << format("0x%08x", Hdr.Magic) << '\n'
107  << "Version = " << format("0x%04x", Hdr.Version) << '\n'
108  << "Hash function = " << format("0x%08x", Hdr.HashFunction) << '\n'
109  << "Bucket count = " << Hdr.NumBuckets << '\n'
110  << "Hashes count = " << Hdr.NumHashes << '\n'
111  << "HeaderData length = " << Hdr.HeaderDataLength << '\n'
112  << "DIE offset base = " << HdrData.DIEOffsetBase << '\n'
113  << "Number of atoms = " << HdrData.Atoms.size() << '\n';
114 
115  unsigned i = 0;
117  for (const auto &Atom: HdrData.Atoms) {
118  OS << format("Atom[%d] Type: ", i++);
119  auto TypeString = dwarf::AtomTypeString(Atom.first);
120  if (!TypeString.empty())
121  OS << TypeString;
122  else
123  OS << format("DW_ATOM_Unknown_0x%x", Atom.first);
124  OS << " Form: ";
125  auto FormString = dwarf::FormEncodingString(Atom.second);
126  if (!FormString.empty())
127  OS << FormString;
128  else
129  OS << format("DW_FORM_Unknown_0x%x", Atom.second);
130  OS << '\n';
131  AtomForms.push_back(DWARFFormValue(Atom.second));
132  }
133 
134  // Now go through the actual tables and dump them.
135  uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength;
136  unsigned HashesBase = Offset + Hdr.NumBuckets * 4;
137  unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4;
138 
139  for (unsigned Bucket = 0; Bucket < Hdr.NumBuckets; ++Bucket) {
140  unsigned Index = AccelSection.getU32(&Offset);
141 
142  OS << format("Bucket[%d]\n", Bucket);
143  if (Index == UINT32_MAX) {
144  OS << " EMPTY\n";
145  continue;
146  }
147 
148  for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) {
149  unsigned HashOffset = HashesBase + HashIdx*4;
150  unsigned OffsetsOffset = OffsetsBase + HashIdx*4;
151  uint32_t Hash = AccelSection.getU32(&HashOffset);
152 
153  if (Hash % Hdr.NumBuckets != Bucket)
154  break;
155 
156  unsigned DataOffset = AccelSection.getU32(&OffsetsOffset);
157  OS << format(" Hash = 0x%08x Offset = 0x%08x\n", Hash, DataOffset);
158  if (!AccelSection.isValidOffset(DataOffset)) {
159  OS << " Invalid section offset\n";
160  continue;
161  }
162  while (AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) {
163  unsigned StringOffset = AccelSection.getRelocatedValue(4, &DataOffset);
164  if (!StringOffset)
165  break;
166  OS << format(" Name: %08x \"%s\"\n", StringOffset,
167  StringSection.getCStr(&StringOffset));
168  unsigned NumData = AccelSection.getU32(&DataOffset);
169  for (unsigned Data = 0; Data < NumData; ++Data) {
170  OS << format(" Data[%d] => ", Data);
171  unsigned i = 0;
172  for (auto &Atom : AtomForms) {
173  OS << format("{Atom[%d]: ", i++);
174  if (Atom.extractValue(AccelSection, &DataOffset, nullptr))
175  Atom.dump(OS);
176  else
177  OS << "Error extracting the value";
178  OS << "} ";
179  }
180  OS << '\n';
181  }
182  }
183  }
184  }
185 }
StringRef AtomTypeString(unsigned Atom)
Definition: Dwarf.cpp:490
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
bool isValidOffset(uint32_t offset) const
Test the validity of offset.
uint32_t getU32(uint32_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
StringRef FormEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:105
bool isValidOffsetForDataOfSize(uint32_t offset, uint32_t length) const
Test the availability of length bytes of data from offset.
uint32_t readAtoms(uint32_t &HashDataOffset)
Return information related to the DWARF DIE we're looking for when performing a lookup by name...
uint64_t getRelocatedValue(uint32_t Size, uint32_t *Off, uint64_t *SectionIndex=nullptr) const
Extracts a value and applies a relocation to the result if one exists for the given offset...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, const DWARFUnit *U)
Extracts a value in Data at offset *OffsetPtr.
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
const char * getCStr(uint32_t *offset_ptr) const
Extract a C string from *offset_ptr.
const uint32_t DW_INVALID_OFFSET
Definition: Dwarf.h:66
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
Optional< uint64_t > getAsUnsignedConstant() const
bool isFormClass(FormClass FC) const
ArrayRef< std::pair< HeaderData::AtomType, HeaderData::Form > > getAtomsDesc()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
void dump(raw_ostream &OS) const
uint16_t getU16(uint32_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
dwarf::Form getForm() const
std::string Hash(const Unit &U)
Definition: FuzzerSHA1.cpp:216