1*0b57cec5SDimitry Andric //===- DWARFUnitIndex.cpp -------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
10*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
11*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
12*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
13*0b57cec5SDimitry Andric #include "llvm/Support/Format.h"
14*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
15*0b57cec5SDimitry Andric #include <cinttypes>
16*0b57cec5SDimitry Andric #include <cstdint>
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric using namespace llvm;
19*0b57cec5SDimitry Andric 
20*0b57cec5SDimitry Andric namespace {
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric enum class DWARFSectionKindV2 {
23*0b57cec5SDimitry Andric   DW_SECT_INFO = 1,
24*0b57cec5SDimitry Andric   DW_SECT_TYPES = 2,
25*0b57cec5SDimitry Andric   DW_SECT_ABBREV = 3,
26*0b57cec5SDimitry Andric   DW_SECT_LINE = 4,
27*0b57cec5SDimitry Andric   DW_SECT_LOC = 5,
28*0b57cec5SDimitry Andric   DW_SECT_STR_OFFSETS = 6,
29*0b57cec5SDimitry Andric   DW_SECT_MACINFO = 7,
30*0b57cec5SDimitry Andric   DW_SECT_MACRO = 8,
31*0b57cec5SDimitry Andric };
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric } // namespace
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric // Return true if the section identifier is defined in the DWARFv5 standard.
isKnownV5SectionID(uint32_t ID)36*0b57cec5SDimitry Andric constexpr bool isKnownV5SectionID(uint32_t ID) {
37*0b57cec5SDimitry Andric   return ID >= DW_SECT_INFO && ID <= DW_SECT_RNGLISTS &&
38*0b57cec5SDimitry Andric          ID != DW_SECT_EXT_TYPES;
39*0b57cec5SDimitry Andric }
40*0b57cec5SDimitry Andric 
serializeSectionKind(DWARFSectionKind Kind,unsigned IndexVersion)41*0b57cec5SDimitry Andric uint32_t llvm::serializeSectionKind(DWARFSectionKind Kind,
42*0b57cec5SDimitry Andric                                     unsigned IndexVersion) {
43*0b57cec5SDimitry Andric   if (IndexVersion == 5) {
44*0b57cec5SDimitry Andric     assert(isKnownV5SectionID(Kind));
45*0b57cec5SDimitry Andric     return static_cast<uint32_t>(Kind);
46*0b57cec5SDimitry Andric   }
47*0b57cec5SDimitry Andric   assert(IndexVersion == 2);
48*0b57cec5SDimitry Andric   switch (Kind) {
49*0b57cec5SDimitry Andric #define CASE(S,T) \
50*0b57cec5SDimitry Andric   case DW_SECT_##S: \
51*0b57cec5SDimitry Andric     return static_cast<uint32_t>(DWARFSectionKindV2::DW_SECT_##T)
52*0b57cec5SDimitry Andric   CASE(INFO, INFO);
53*0b57cec5SDimitry Andric   CASE(EXT_TYPES, TYPES);
54*0b57cec5SDimitry Andric   CASE(ABBREV, ABBREV);
55*0b57cec5SDimitry Andric   CASE(LINE, LINE);
56*0b57cec5SDimitry Andric   CASE(EXT_LOC, LOC);
57*0b57cec5SDimitry Andric   CASE(STR_OFFSETS, STR_OFFSETS);
58*0b57cec5SDimitry Andric   CASE(EXT_MACINFO, MACINFO);
59*0b57cec5SDimitry Andric   CASE(MACRO, MACRO);
60*0b57cec5SDimitry Andric #undef CASE
61*0b57cec5SDimitry Andric   default:
62*0b57cec5SDimitry Andric     // All other section kinds have no corresponding values in v2 indexes.
63*0b57cec5SDimitry Andric     llvm_unreachable("Invalid DWARFSectionKind");
64*0b57cec5SDimitry Andric   }
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric 
deserializeSectionKind(uint32_t Value,unsigned IndexVersion)67*0b57cec5SDimitry Andric DWARFSectionKind llvm::deserializeSectionKind(uint32_t Value,
68*0b57cec5SDimitry Andric                                               unsigned IndexVersion) {
69*0b57cec5SDimitry Andric   if (IndexVersion == 5)
70*0b57cec5SDimitry Andric     return isKnownV5SectionID(Value)
71*0b57cec5SDimitry Andric                ? static_cast<DWARFSectionKind>(Value)
72*0b57cec5SDimitry Andric                : DW_SECT_EXT_unknown;
73*0b57cec5SDimitry Andric   assert(IndexVersion == 2);
74*0b57cec5SDimitry Andric   switch (static_cast<DWARFSectionKindV2>(Value)) {
75*0b57cec5SDimitry Andric #define CASE(S,T) \
76*0b57cec5SDimitry Andric   case DWARFSectionKindV2::DW_SECT_##S: \
77*0b57cec5SDimitry Andric     return DW_SECT_##T
78*0b57cec5SDimitry Andric   CASE(INFO, INFO);
79*0b57cec5SDimitry Andric   CASE(TYPES, EXT_TYPES);
80*0b57cec5SDimitry Andric   CASE(ABBREV, ABBREV);
81*0b57cec5SDimitry Andric   CASE(LINE, LINE);
82*0b57cec5SDimitry Andric   CASE(LOC, EXT_LOC);
83*0b57cec5SDimitry Andric   CASE(STR_OFFSETS, STR_OFFSETS);
84*0b57cec5SDimitry Andric   CASE(MACINFO, EXT_MACINFO);
85*0b57cec5SDimitry Andric   CASE(MACRO, MACRO);
86*0b57cec5SDimitry Andric #undef CASE
87*0b57cec5SDimitry Andric   }
88*0b57cec5SDimitry Andric   return DW_SECT_EXT_unknown;
89*0b57cec5SDimitry Andric }
90*0b57cec5SDimitry Andric 
parse(DataExtractor IndexData,uint64_t * OffsetPtr)91*0b57cec5SDimitry Andric bool DWARFUnitIndex::Header::parse(DataExtractor IndexData,
92*0b57cec5SDimitry Andric                                    uint64_t *OffsetPtr) {
93*0b57cec5SDimitry Andric   const uint64_t BeginOffset = *OffsetPtr;
94*0b57cec5SDimitry Andric   if (!IndexData.isValidOffsetForDataOfSize(*OffsetPtr, 16))
95*0b57cec5SDimitry Andric     return false;
96*0b57cec5SDimitry Andric   // GCC Debug Fission defines the version as an unsigned 32-bit field
97*0b57cec5SDimitry Andric   // with value of 2, https://gcc.gnu.org/wiki/DebugFissionDWP.
98*0b57cec5SDimitry Andric   // DWARFv5 defines the same space as an uhalf version field with value of 5
99*0b57cec5SDimitry Andric   // and a 2 bytes long padding, see Section 7.3.5.3.
100*0b57cec5SDimitry Andric   Version = IndexData.getU32(OffsetPtr);
101*0b57cec5SDimitry Andric   if (Version != 2) {
102*0b57cec5SDimitry Andric     *OffsetPtr = BeginOffset;
103*0b57cec5SDimitry Andric     Version = IndexData.getU16(OffsetPtr);
104*0b57cec5SDimitry Andric     if (Version != 5)
105*0b57cec5SDimitry Andric       return false;
106*0b57cec5SDimitry Andric     *OffsetPtr += 2; // Skip padding.
107*0b57cec5SDimitry Andric   }
108*0b57cec5SDimitry Andric   NumColumns = IndexData.getU32(OffsetPtr);
109*0b57cec5SDimitry Andric   NumUnits = IndexData.getU32(OffsetPtr);
110*0b57cec5SDimitry Andric   NumBuckets = IndexData.getU32(OffsetPtr);
111*0b57cec5SDimitry Andric   return true;
112*0b57cec5SDimitry Andric }
113*0b57cec5SDimitry Andric 
dump(raw_ostream & OS) const114*0b57cec5SDimitry Andric void DWARFUnitIndex::Header::dump(raw_ostream &OS) const {
115*0b57cec5SDimitry Andric   OS << format("version = %u, units = %u, slots = %u\n\n", Version, NumUnits, NumBuckets);
116*0b57cec5SDimitry Andric }
117*0b57cec5SDimitry Andric 
parse(DataExtractor IndexData)118*0b57cec5SDimitry Andric bool DWARFUnitIndex::parse(DataExtractor IndexData) {
119*0b57cec5SDimitry Andric   bool b = parseImpl(IndexData);
120*0b57cec5SDimitry Andric   if (!b) {
121*0b57cec5SDimitry Andric     // Make sure we don't try to dump anything
122*0b57cec5SDimitry Andric     Header.NumBuckets = 0;
123*0b57cec5SDimitry Andric     // Release any partially initialized data.
124*0b57cec5SDimitry Andric     ColumnKinds.reset();
125*0b57cec5SDimitry Andric     Rows.reset();
126*0b57cec5SDimitry Andric   }
127*0b57cec5SDimitry Andric   return b;
128*0b57cec5SDimitry Andric }
129*0b57cec5SDimitry Andric 
parseImpl(DataExtractor IndexData)130*0b57cec5SDimitry Andric bool DWARFUnitIndex::parseImpl(DataExtractor IndexData) {
131*0b57cec5SDimitry Andric   uint64_t Offset = 0;
132*0b57cec5SDimitry Andric   if (!Header.parse(IndexData, &Offset))
133*0b57cec5SDimitry Andric     return false;
134*0b57cec5SDimitry Andric 
135*0b57cec5SDimitry Andric   // Fix InfoColumnKind: in DWARFv5, type units are in .debug_info.dwo.
136*0b57cec5SDimitry Andric   if (Header.Version == 5)
137*0b57cec5SDimitry Andric     InfoColumnKind = DW_SECT_INFO;
138*0b57cec5SDimitry Andric 
139*0b57cec5SDimitry Andric   if (!IndexData.isValidOffsetForDataOfSize(
140*0b57cec5SDimitry Andric           Offset, Header.NumBuckets * (8 + 4) +
141*0b57cec5SDimitry Andric                       (2 * Header.NumUnits + 1) * 4 * Header.NumColumns))
142*0b57cec5SDimitry Andric     return false;
143*0b57cec5SDimitry Andric 
144*0b57cec5SDimitry Andric   Rows = std::make_unique<Entry[]>(Header.NumBuckets);
145*0b57cec5SDimitry Andric   auto Contribs =
146*0b57cec5SDimitry Andric       std::make_unique<Entry::SectionContribution *[]>(Header.NumUnits);
147*0b57cec5SDimitry Andric   ColumnKinds = std::make_unique<DWARFSectionKind[]>(Header.NumColumns);
148*0b57cec5SDimitry Andric   RawSectionIds = std::make_unique<uint32_t[]>(Header.NumColumns);
149*0b57cec5SDimitry Andric 
150*0b57cec5SDimitry Andric   // Read Hash Table of Signatures
151*0b57cec5SDimitry Andric   for (unsigned i = 0; i != Header.NumBuckets; ++i)
152*0b57cec5SDimitry Andric     Rows[i].Signature = IndexData.getU64(&Offset);
153*0b57cec5SDimitry Andric 
154*0b57cec5SDimitry Andric   // Read Parallel Table of Indexes
155*0b57cec5SDimitry Andric   for (unsigned i = 0; i != Header.NumBuckets; ++i) {
156*0b57cec5SDimitry Andric     auto Index = IndexData.getU32(&Offset);
157*0b57cec5SDimitry Andric     if (!Index)
158*0b57cec5SDimitry Andric       continue;
159*0b57cec5SDimitry Andric     Rows[i].Index = this;
160*0b57cec5SDimitry Andric     Rows[i].Contributions =
161*0b57cec5SDimitry Andric         std::make_unique<Entry::SectionContribution[]>(Header.NumColumns);
162*0b57cec5SDimitry Andric     Contribs[Index - 1] = Rows[i].Contributions.get();
163*0b57cec5SDimitry Andric   }
164*0b57cec5SDimitry Andric 
165*0b57cec5SDimitry Andric   // Read the Column Headers
166*0b57cec5SDimitry Andric   for (unsigned i = 0; i != Header.NumColumns; ++i) {
167*0b57cec5SDimitry Andric     RawSectionIds[i] = IndexData.getU32(&Offset);
168*0b57cec5SDimitry Andric     ColumnKinds[i] = deserializeSectionKind(RawSectionIds[i], Header.Version);
169*0b57cec5SDimitry Andric     if (ColumnKinds[i] == InfoColumnKind) {
170*0b57cec5SDimitry Andric       if (InfoColumn != -1)
171*0b57cec5SDimitry Andric         return false;
172*0b57cec5SDimitry Andric       InfoColumn = i;
173*0b57cec5SDimitry Andric     }
174*0b57cec5SDimitry Andric   }
175*0b57cec5SDimitry Andric 
176*0b57cec5SDimitry Andric   if (InfoColumn == -1)
177*0b57cec5SDimitry Andric     return false;
178*0b57cec5SDimitry Andric 
179*0b57cec5SDimitry Andric   // Read Table of Section Offsets
180*0b57cec5SDimitry Andric   for (unsigned i = 0; i != Header.NumUnits; ++i) {
181*0b57cec5SDimitry Andric     auto *Contrib = Contribs[i];
182*0b57cec5SDimitry Andric     for (unsigned i = 0; i != Header.NumColumns; ++i)
183*0b57cec5SDimitry Andric       Contrib[i].Offset = IndexData.getU32(&Offset);
184*0b57cec5SDimitry Andric   }
185*0b57cec5SDimitry Andric 
186*0b57cec5SDimitry Andric   // Read Table of Section Sizes
187*0b57cec5SDimitry Andric   for (unsigned i = 0; i != Header.NumUnits; ++i) {
188*0b57cec5SDimitry Andric     auto *Contrib = Contribs[i];
189*0b57cec5SDimitry Andric     for (unsigned i = 0; i != Header.NumColumns; ++i)
190*0b57cec5SDimitry Andric       Contrib[i].Length = IndexData.getU32(&Offset);
191*0b57cec5SDimitry Andric   }
192*0b57cec5SDimitry Andric 
193*0b57cec5SDimitry Andric   return true;
194*0b57cec5SDimitry Andric }
195*0b57cec5SDimitry Andric 
getColumnHeader(DWARFSectionKind DS)196*0b57cec5SDimitry Andric StringRef DWARFUnitIndex::getColumnHeader(DWARFSectionKind DS) {
197*0b57cec5SDimitry Andric   switch (DS) {
198*0b57cec5SDimitry Andric #define HANDLE_DW_SECT(ID, NAME)                                               \
199*0b57cec5SDimitry Andric   case DW_SECT_##NAME:                                                         \
200*0b57cec5SDimitry Andric     return #NAME;
201 #include "llvm/BinaryFormat/Dwarf.def"
202   case DW_SECT_EXT_TYPES:
203     return "TYPES";
204   case DW_SECT_EXT_LOC:
205     return "LOC";
206   case DW_SECT_EXT_MACINFO:
207     return "MACINFO";
208   case DW_SECT_EXT_unknown:
209     return StringRef();
210   }
211   llvm_unreachable("Unknown DWARFSectionKind");
212 }
213 
dump(raw_ostream & OS) const214 void DWARFUnitIndex::dump(raw_ostream &OS) const {
215   if (!*this)
216     return;
217 
218   Header.dump(OS);
219   OS << "Index Signature         ";
220   for (unsigned i = 0; i != Header.NumColumns; ++i) {
221     DWARFSectionKind Kind = ColumnKinds[i];
222     StringRef Name = getColumnHeader(Kind);
223     if (!Name.empty())
224       OS << ' ' << left_justify(Name, 24);
225     else
226       OS << format(" Unknown: %-15" PRIu32, RawSectionIds[i]);
227   }
228   OS << "\n----- ------------------";
229   for (unsigned i = 0; i != Header.NumColumns; ++i)
230     OS << " ------------------------";
231   OS << '\n';
232   for (unsigned i = 0; i != Header.NumBuckets; ++i) {
233     auto &Row = Rows[i];
234     if (auto *Contribs = Row.Contributions.get()) {
235       OS << format("%5u 0x%016" PRIx64 " ", i + 1, Row.Signature);
236       for (unsigned i = 0; i != Header.NumColumns; ++i) {
237         auto &Contrib = Contribs[i];
238         OS << format("[0x%08x, 0x%08x) ", Contrib.Offset,
239                      Contrib.Offset + Contrib.Length);
240       }
241       OS << '\n';
242     }
243   }
244 }
245 
246 const DWARFUnitIndex::Entry::SectionContribution *
getContribution(DWARFSectionKind Sec) const247 DWARFUnitIndex::Entry::getContribution(DWARFSectionKind Sec) const {
248   uint32_t i = 0;
249   for (; i != Index->Header.NumColumns; ++i)
250     if (Index->ColumnKinds[i] == Sec)
251       return &Contributions[i];
252   return nullptr;
253 }
254 
255 const DWARFUnitIndex::Entry::SectionContribution *
getContribution() const256 DWARFUnitIndex::Entry::getContribution() const {
257   return &Contributions[Index->InfoColumn];
258 }
259 
260 const DWARFUnitIndex::Entry *
getFromOffset(uint32_t Offset) const261 DWARFUnitIndex::getFromOffset(uint32_t Offset) const {
262   if (OffsetLookup.empty()) {
263     for (uint32_t i = 0; i != Header.NumBuckets; ++i)
264       if (Rows[i].Contributions)
265         OffsetLookup.push_back(&Rows[i]);
266     llvm::sort(OffsetLookup, [&](Entry *E1, Entry *E2) {
267       return E1->Contributions[InfoColumn].Offset <
268              E2->Contributions[InfoColumn].Offset;
269     });
270   }
271   auto I = partition_point(OffsetLookup, [&](Entry *E2) {
272     return E2->Contributions[InfoColumn].Offset <= Offset;
273   });
274   if (I == OffsetLookup.begin())
275     return nullptr;
276   --I;
277   const auto *E = *I;
278   const auto &InfoContrib = E->Contributions[InfoColumn];
279   if ((InfoContrib.Offset + InfoContrib.Length) <= Offset)
280     return nullptr;
281   return E;
282 }
283 
getFromHash(uint64_t S) const284 const DWARFUnitIndex::Entry *DWARFUnitIndex::getFromHash(uint64_t S) const {
285   uint64_t Mask = Header.NumBuckets - 1;
286 
287   auto H = S & Mask;
288   auto HP = ((S >> 32) & Mask) | 1;
289   // The spec says "while 0 is a valid hash value, the row index in a used slot
290   // will always be non-zero". Loop until we find a match or an empty slot.
291   while (Rows[H].getSignature() != S && Rows[H].Index != nullptr)
292     H = (H + HP) & Mask;
293 
294   // If the slot is empty, we don't care whether the signature matches (it could
295   // be zero and still match the zeros in the empty slot).
296   if (Rows[H].Index == nullptr)
297     return nullptr;
298 
299   return &Rows[H];
300 }
301