1 //===- DWARFUnitIndex.cpp -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
21 bool DWARFUnitIndex::Header::parse(DataExtractor IndexData,
22 uint32_t *OffsetPtr) {
23 if (!IndexData.isValidOffsetForDataOfSize(*OffsetPtr, 16))
25 Version = IndexData.getU32(OffsetPtr);
26 NumColumns = IndexData.getU32(OffsetPtr);
27 NumUnits = IndexData.getU32(OffsetPtr);
28 NumBuckets = IndexData.getU32(OffsetPtr);
32 void DWARFUnitIndex::Header::dump(raw_ostream &OS) const {
33 OS << format("version = %u slots = %u\n\n", Version, NumBuckets);
36 bool DWARFUnitIndex::parse(DataExtractor IndexData) {
37 bool b = parseImpl(IndexData);
39 // Make sure we don't try to dump anything
40 Header.NumBuckets = 0;
41 // Release any partially initialized data.
48 bool DWARFUnitIndex::parseImpl(DataExtractor IndexData) {
50 if (!Header.parse(IndexData, &Offset))
53 if (!IndexData.isValidOffsetForDataOfSize(
54 Offset, Header.NumBuckets * (8 + 4) +
55 (2 * Header.NumUnits + 1) * 4 * Header.NumColumns))
58 Rows = llvm::make_unique<Entry[]>(Header.NumBuckets);
60 llvm::make_unique<Entry::SectionContribution *[]>(Header.NumUnits);
61 ColumnKinds = llvm::make_unique<DWARFSectionKind[]>(Header.NumColumns);
63 // Read Hash Table of Signatures
64 for (unsigned i = 0; i != Header.NumBuckets; ++i)
65 Rows[i].Signature = IndexData.getU64(&Offset);
67 // Read Parallel Table of Indexes
68 for (unsigned i = 0; i != Header.NumBuckets; ++i) {
69 auto Index = IndexData.getU32(&Offset);
73 Rows[i].Contributions =
74 llvm::make_unique<Entry::SectionContribution[]>(Header.NumColumns);
75 Contribs[Index - 1] = Rows[i].Contributions.get();
78 // Read the Column Headers
79 for (unsigned i = 0; i != Header.NumColumns; ++i) {
80 ColumnKinds[i] = static_cast<DWARFSectionKind>(IndexData.getU32(&Offset));
81 if (ColumnKinds[i] == InfoColumnKind) {
91 // Read Table of Section Offsets
92 for (unsigned i = 0; i != Header.NumUnits; ++i) {
93 auto *Contrib = Contribs[i];
94 for (unsigned i = 0; i != Header.NumColumns; ++i)
95 Contrib[i].Offset = IndexData.getU32(&Offset);
98 // Read Table of Section Sizes
99 for (unsigned i = 0; i != Header.NumUnits; ++i) {
100 auto *Contrib = Contribs[i];
101 for (unsigned i = 0; i != Header.NumColumns; ++i)
102 Contrib[i].Length = IndexData.getU32(&Offset);
108 StringRef DWARFUnitIndex::getColumnHeader(DWARFSectionKind DS) {
122 llvm_unreachable("unknown DWARFSectionKind");
125 void DWARFUnitIndex::dump(raw_ostream &OS) const {
126 if (!Header.NumBuckets)
130 OS << "Index Signature ";
131 for (unsigned i = 0; i != Header.NumColumns; ++i)
132 OS << ' ' << left_justify(getColumnHeader(ColumnKinds[i]), 24);
133 OS << "\n----- ------------------";
134 for (unsigned i = 0; i != Header.NumColumns; ++i)
135 OS << " ------------------------";
137 for (unsigned i = 0; i != Header.NumBuckets; ++i) {
139 if (auto *Contribs = Row.Contributions.get()) {
140 OS << format("%5u 0x%016" PRIx64 " ", i + 1, Row.Signature);
141 for (unsigned i = 0; i != Header.NumColumns; ++i) {
142 auto &Contrib = Contribs[i];
143 OS << format("[0x%08x, 0x%08x) ", Contrib.Offset,
144 Contrib.Offset + Contrib.Length);
151 const DWARFUnitIndex::Entry::SectionContribution *
152 DWARFUnitIndex::Entry::getOffset(DWARFSectionKind Sec) const {
154 for (; i != Index->Header.NumColumns; ++i)
155 if (Index->ColumnKinds[i] == Sec)
156 return &Contributions[i];
160 const DWARFUnitIndex::Entry::SectionContribution *
161 DWARFUnitIndex::Entry::getOffset() const {
162 return &Contributions[Index->InfoColumn];
165 const DWARFUnitIndex::Entry *
166 DWARFUnitIndex::getFromOffset(uint32_t Offset) const {
167 for (uint32_t i = 0; i != Header.NumBuckets; ++i)
168 if (const auto &Contribs = Rows[i].Contributions)
169 if (Contribs[InfoColumn].Offset == Offset)