128db7e65SEugene Zelenko //===- DWARFUnitIndex.cpp -------------------------------------------------===//
265a8efe4SDavid Blaikie //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
665a8efe4SDavid Blaikie //
765a8efe4SDavid Blaikie //===----------------------------------------------------------------------===//
865a8efe4SDavid Blaikie 
928db7e65SEugene Zelenko #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
106bda14b3SChandler Carruth #include "llvm/ADT/STLExtras.h"
116bda14b3SChandler Carruth #include "llvm/ADT/StringRef.h"
12*290e4823Sserge-sans-paille #include "llvm/Support/DataExtractor.h"
135b9bf49cSDavid Blaikie #include "llvm/Support/ErrorHandling.h"
1428db7e65SEugene Zelenko #include "llvm/Support/Format.h"
1528db7e65SEugene Zelenko #include "llvm/Support/raw_ostream.h"
1628db7e65SEugene Zelenko #include <cinttypes>
1728db7e65SEugene Zelenko #include <cstdint>
185b9bf49cSDavid Blaikie 
1928db7e65SEugene Zelenko using namespace llvm;
2065a8efe4SDavid Blaikie 
21714324b7SIgor Kudrin namespace {
22714324b7SIgor Kudrin 
23714324b7SIgor Kudrin enum class DWARFSectionKindV2 {
24714324b7SIgor Kudrin   DW_SECT_INFO = 1,
25714324b7SIgor Kudrin   DW_SECT_TYPES = 2,
26714324b7SIgor Kudrin   DW_SECT_ABBREV = 3,
27714324b7SIgor Kudrin   DW_SECT_LINE = 4,
28714324b7SIgor Kudrin   DW_SECT_LOC = 5,
29714324b7SIgor Kudrin   DW_SECT_STR_OFFSETS = 6,
30714324b7SIgor Kudrin   DW_SECT_MACINFO = 7,
31714324b7SIgor Kudrin   DW_SECT_MACRO = 8,
32714324b7SIgor Kudrin };
33714324b7SIgor Kudrin 
34714324b7SIgor Kudrin } // namespace
35714324b7SIgor Kudrin 
36714324b7SIgor Kudrin // Return true if the section identifier is defined in the DWARFv5 standard.
isKnownV5SectionID(uint32_t ID)37714324b7SIgor Kudrin constexpr bool isKnownV5SectionID(uint32_t ID) {
38714324b7SIgor Kudrin   return ID >= DW_SECT_INFO && ID <= DW_SECT_RNGLISTS &&
39714324b7SIgor Kudrin          ID != DW_SECT_EXT_TYPES;
40714324b7SIgor Kudrin }
41714324b7SIgor Kudrin 
serializeSectionKind(DWARFSectionKind Kind,unsigned IndexVersion)42714324b7SIgor Kudrin uint32_t llvm::serializeSectionKind(DWARFSectionKind Kind,
43714324b7SIgor Kudrin                                     unsigned IndexVersion) {
44714324b7SIgor Kudrin   if (IndexVersion == 5) {
45714324b7SIgor Kudrin     assert(isKnownV5SectionID(Kind));
46714324b7SIgor Kudrin     return static_cast<uint32_t>(Kind);
47714324b7SIgor Kudrin   }
48714324b7SIgor Kudrin   assert(IndexVersion == 2);
49714324b7SIgor Kudrin   switch (Kind) {
50714324b7SIgor Kudrin #define CASE(S,T) \
51714324b7SIgor Kudrin   case DW_SECT_##S: \
52714324b7SIgor Kudrin     return static_cast<uint32_t>(DWARFSectionKindV2::DW_SECT_##T)
53714324b7SIgor Kudrin   CASE(INFO, INFO);
54714324b7SIgor Kudrin   CASE(EXT_TYPES, TYPES);
55714324b7SIgor Kudrin   CASE(ABBREV, ABBREV);
56714324b7SIgor Kudrin   CASE(LINE, LINE);
57714324b7SIgor Kudrin   CASE(EXT_LOC, LOC);
58714324b7SIgor Kudrin   CASE(STR_OFFSETS, STR_OFFSETS);
59714324b7SIgor Kudrin   CASE(EXT_MACINFO, MACINFO);
60714324b7SIgor Kudrin   CASE(MACRO, MACRO);
61714324b7SIgor Kudrin #undef CASE
62714324b7SIgor Kudrin   default:
63714324b7SIgor Kudrin     // All other section kinds have no corresponding values in v2 indexes.
64714324b7SIgor Kudrin     llvm_unreachable("Invalid DWARFSectionKind");
65714324b7SIgor Kudrin   }
66714324b7SIgor Kudrin }
67714324b7SIgor Kudrin 
deserializeSectionKind(uint32_t Value,unsigned IndexVersion)68714324b7SIgor Kudrin DWARFSectionKind llvm::deserializeSectionKind(uint32_t Value,
69714324b7SIgor Kudrin                                               unsigned IndexVersion) {
70714324b7SIgor Kudrin   if (IndexVersion == 5)
71714324b7SIgor Kudrin     return isKnownV5SectionID(Value)
72714324b7SIgor Kudrin                ? static_cast<DWARFSectionKind>(Value)
73714324b7SIgor Kudrin                : DW_SECT_EXT_unknown;
74714324b7SIgor Kudrin   assert(IndexVersion == 2);
75714324b7SIgor Kudrin   switch (static_cast<DWARFSectionKindV2>(Value)) {
76714324b7SIgor Kudrin #define CASE(S,T) \
77714324b7SIgor Kudrin   case DWARFSectionKindV2::DW_SECT_##S: \
78714324b7SIgor Kudrin     return DW_SECT_##T
79714324b7SIgor Kudrin   CASE(INFO, INFO);
80714324b7SIgor Kudrin   CASE(TYPES, EXT_TYPES);
81714324b7SIgor Kudrin   CASE(ABBREV, ABBREV);
82714324b7SIgor Kudrin   CASE(LINE, LINE);
83714324b7SIgor Kudrin   CASE(LOC, EXT_LOC);
84714324b7SIgor Kudrin   CASE(STR_OFFSETS, STR_OFFSETS);
85714324b7SIgor Kudrin   CASE(MACINFO, EXT_MACINFO);
86714324b7SIgor Kudrin   CASE(MACRO, MACRO);
87714324b7SIgor Kudrin #undef CASE
88714324b7SIgor Kudrin   }
89714324b7SIgor Kudrin   return DW_SECT_EXT_unknown;
90714324b7SIgor Kudrin }
91714324b7SIgor Kudrin 
parse(DataExtractor IndexData,uint64_t * OffsetPtr)920b44dcc4SDavid Blaikie bool DWARFUnitIndex::Header::parse(DataExtractor IndexData,
93f26a70a5SIgor Kudrin                                    uint64_t *OffsetPtr) {
94714324b7SIgor Kudrin   const uint64_t BeginOffset = *OffsetPtr;
956e9c4f7fSDavid Blaikie   if (!IndexData.isValidOffsetForDataOfSize(*OffsetPtr, 16))
966e9c4f7fSDavid Blaikie     return false;
97714324b7SIgor Kudrin   // GCC Debug Fission defines the version as an unsigned 32-bit field
98714324b7SIgor Kudrin   // with value of 2, https://gcc.gnu.org/wiki/DebugFissionDWP.
99714324b7SIgor Kudrin   // DWARFv5 defines the same space as an uhalf version field with value of 5
100714324b7SIgor Kudrin   // and a 2 bytes long padding, see Section 7.3.5.3.
10165a8efe4SDavid Blaikie   Version = IndexData.getU32(OffsetPtr);
102714324b7SIgor Kudrin   if (Version != 2) {
103714324b7SIgor Kudrin     *OffsetPtr = BeginOffset;
104714324b7SIgor Kudrin     Version = IndexData.getU16(OffsetPtr);
105714324b7SIgor Kudrin     if (Version != 5)
106714324b7SIgor Kudrin       return false;
107714324b7SIgor Kudrin     *OffsetPtr += 2; // Skip padding.
108714324b7SIgor Kudrin   }
10965a8efe4SDavid Blaikie   NumColumns = IndexData.getU32(OffsetPtr);
11065a8efe4SDavid Blaikie   NumUnits = IndexData.getU32(OffsetPtr);
11165a8efe4SDavid Blaikie   NumBuckets = IndexData.getU32(OffsetPtr);
112714324b7SIgor Kudrin   return true;
11365a8efe4SDavid Blaikie }
11465a8efe4SDavid Blaikie 
dump(raw_ostream & OS) const11565a8efe4SDavid Blaikie void DWARFUnitIndex::Header::dump(raw_ostream &OS) const {
1165146fc15SDavid Blaikie   OS << format("version = %u, units = %u, slots = %u\n\n", Version, NumUnits, NumBuckets);
11765a8efe4SDavid Blaikie }
11865a8efe4SDavid Blaikie 
parse(DataExtractor IndexData)11965a8efe4SDavid Blaikie bool DWARFUnitIndex::parse(DataExtractor IndexData) {
120b073cb9bSDavid Blaikie   bool b = parseImpl(IndexData);
12120f52662SDavid Blaikie   if (!b) {
12220f52662SDavid Blaikie     // Make sure we don't try to dump anything
12320f52662SDavid Blaikie     Header.NumBuckets = 0;
12420f52662SDavid Blaikie     // Release any partially initialized data.
12520f52662SDavid Blaikie     ColumnKinds.reset();
12620f52662SDavid Blaikie     Rows.reset();
12720f52662SDavid Blaikie   }
128b073cb9bSDavid Blaikie   return b;
129b073cb9bSDavid Blaikie }
130b073cb9bSDavid Blaikie 
parseImpl(DataExtractor IndexData)131b073cb9bSDavid Blaikie bool DWARFUnitIndex::parseImpl(DataExtractor IndexData) {
132f26a70a5SIgor Kudrin   uint64_t Offset = 0;
13365a8efe4SDavid Blaikie   if (!Header.parse(IndexData, &Offset))
13465a8efe4SDavid Blaikie     return false;
13565a8efe4SDavid Blaikie 
136714324b7SIgor Kudrin   // Fix InfoColumnKind: in DWARFv5, type units are in .debug_info.dwo.
137714324b7SIgor Kudrin   if (Header.Version == 5)
138714324b7SIgor Kudrin     InfoColumnKind = DW_SECT_INFO;
139714324b7SIgor Kudrin 
1406e9c4f7fSDavid Blaikie   if (!IndexData.isValidOffsetForDataOfSize(
1416e9c4f7fSDavid Blaikie           Offset, Header.NumBuckets * (8 + 4) +
1426e9c4f7fSDavid Blaikie                       (2 * Header.NumUnits + 1) * 4 * Header.NumColumns))
1436e9c4f7fSDavid Blaikie     return false;
1446e9c4f7fSDavid Blaikie 
1450eaee545SJonas Devlieghere   Rows = std::make_unique<Entry[]>(Header.NumBuckets);
1468e8dd57eSDavid Blaikie   auto Contribs =
1470eaee545SJonas Devlieghere       std::make_unique<Entry::SectionContribution *[]>(Header.NumUnits);
1480eaee545SJonas Devlieghere   ColumnKinds = std::make_unique<DWARFSectionKind[]>(Header.NumColumns);
149714324b7SIgor Kudrin   RawSectionIds = std::make_unique<uint32_t[]>(Header.NumColumns);
1508e8dd57eSDavid Blaikie 
1518e8dd57eSDavid Blaikie   // Read Hash Table of Signatures
1528e8dd57eSDavid Blaikie   for (unsigned i = 0; i != Header.NumBuckets; ++i)
1538e8dd57eSDavid Blaikie     Rows[i].Signature = IndexData.getU64(&Offset);
1548e8dd57eSDavid Blaikie 
1558e8dd57eSDavid Blaikie   // Read Parallel Table of Indexes
1568e8dd57eSDavid Blaikie   for (unsigned i = 0; i != Header.NumBuckets; ++i) {
1578e8dd57eSDavid Blaikie     auto Index = IndexData.getU32(&Offset);
1588e8dd57eSDavid Blaikie     if (!Index)
1598e8dd57eSDavid Blaikie       continue;
16082641be4SDavid Blaikie     Rows[i].Index = this;
1618e8dd57eSDavid Blaikie     Rows[i].Contributions =
1620eaee545SJonas Devlieghere         std::make_unique<Entry::SectionContribution[]>(Header.NumColumns);
1638e8dd57eSDavid Blaikie     Contribs[Index - 1] = Rows[i].Contributions.get();
1648e8dd57eSDavid Blaikie   }
1658e8dd57eSDavid Blaikie 
1668e8dd57eSDavid Blaikie   // Read the Column Headers
16782641be4SDavid Blaikie   for (unsigned i = 0; i != Header.NumColumns; ++i) {
168714324b7SIgor Kudrin     RawSectionIds[i] = IndexData.getU32(&Offset);
169714324b7SIgor Kudrin     ColumnKinds[i] = deserializeSectionKind(RawSectionIds[i], Header.Version);
170b073cb9bSDavid Blaikie     if (ColumnKinds[i] == InfoColumnKind) {
17182641be4SDavid Blaikie       if (InfoColumn != -1)
17282641be4SDavid Blaikie         return false;
17382641be4SDavid Blaikie       InfoColumn = i;
17482641be4SDavid Blaikie     }
17582641be4SDavid Blaikie   }
17682641be4SDavid Blaikie 
17782641be4SDavid Blaikie   if (InfoColumn == -1)
17882641be4SDavid Blaikie     return false;
1798e8dd57eSDavid Blaikie 
1808e8dd57eSDavid Blaikie   // Read Table of Section Offsets
1818e8dd57eSDavid Blaikie   for (unsigned i = 0; i != Header.NumUnits; ++i) {
1828e8dd57eSDavid Blaikie     auto *Contrib = Contribs[i];
18382641be4SDavid Blaikie     for (unsigned i = 0; i != Header.NumColumns; ++i)
1848e8dd57eSDavid Blaikie       Contrib[i].Offset = IndexData.getU32(&Offset);
1858e8dd57eSDavid Blaikie   }
1868e8dd57eSDavid Blaikie 
1878e8dd57eSDavid Blaikie   // Read Table of Section Sizes
1888e8dd57eSDavid Blaikie   for (unsigned i = 0; i != Header.NumUnits; ++i) {
1898e8dd57eSDavid Blaikie     auto *Contrib = Contribs[i];
19082641be4SDavid Blaikie     for (unsigned i = 0; i != Header.NumColumns; ++i)
19182641be4SDavid Blaikie       Contrib[i].Length = IndexData.getU32(&Offset);
1928e8dd57eSDavid Blaikie   }
1938e8dd57eSDavid Blaikie 
19465a8efe4SDavid Blaikie   return true;
19565a8efe4SDavid Blaikie }
19665a8efe4SDavid Blaikie 
getColumnHeader(DWARFSectionKind DS)19782641be4SDavid Blaikie StringRef DWARFUnitIndex::getColumnHeader(DWARFSectionKind DS) {
1985b9bf49cSDavid Blaikie   switch (DS) {
199714324b7SIgor Kudrin #define HANDLE_DW_SECT(ID, NAME)                                               \
200714324b7SIgor Kudrin   case DW_SECT_##NAME:                                                         \
201714324b7SIgor Kudrin     return #NAME;
202714324b7SIgor Kudrin #include "llvm/BinaryFormat/Dwarf.def"
203a0249fe9SIgor Kudrin   case DW_SECT_EXT_TYPES:
204a0249fe9SIgor Kudrin     return "TYPES";
205a0249fe9SIgor Kudrin   case DW_SECT_EXT_LOC:
206a0249fe9SIgor Kudrin     return "LOC";
207a0249fe9SIgor Kudrin   case DW_SECT_EXT_MACINFO:
208a0249fe9SIgor Kudrin     return "MACINFO";
209714324b7SIgor Kudrin   case DW_SECT_EXT_unknown:
2103a1bc41aSIgor Kudrin     return StringRef();
2115b9bf49cSDavid Blaikie   }
212714324b7SIgor Kudrin   llvm_unreachable("Unknown DWARFSectionKind");
213714324b7SIgor Kudrin }
2145b9bf49cSDavid Blaikie 
dump(raw_ostream & OS) const2155b9bf49cSDavid Blaikie void DWARFUnitIndex::dump(raw_ostream &OS) const {
216ebac0b9cSDavid Blaikie   if (!*this)
217b073cb9bSDavid Blaikie     return;
218b073cb9bSDavid Blaikie 
2195b9bf49cSDavid Blaikie   Header.dump(OS);
2208e8dd57eSDavid Blaikie   OS << "Index Signature         ";
2213a1bc41aSIgor Kudrin   for (unsigned i = 0; i != Header.NumColumns; ++i) {
2223a1bc41aSIgor Kudrin     DWARFSectionKind Kind = ColumnKinds[i];
2233a1bc41aSIgor Kudrin     StringRef Name = getColumnHeader(Kind);
2243a1bc41aSIgor Kudrin     if (!Name.empty())
2253a1bc41aSIgor Kudrin       OS << ' ' << left_justify(Name, 24);
2263a1bc41aSIgor Kudrin     else
227714324b7SIgor Kudrin       OS << format(" Unknown: %-15" PRIu32, RawSectionIds[i]);
2283a1bc41aSIgor Kudrin   }
2298e8dd57eSDavid Blaikie   OS << "\n----- ------------------";
2308e8dd57eSDavid Blaikie   for (unsigned i = 0; i != Header.NumColumns; ++i)
2318e8dd57eSDavid Blaikie     OS << " ------------------------";
2328e8dd57eSDavid Blaikie   OS << '\n';
2338e8dd57eSDavid Blaikie   for (unsigned i = 0; i != Header.NumBuckets; ++i) {
2348e8dd57eSDavid Blaikie     auto &Row = Rows[i];
2358e8dd57eSDavid Blaikie     if (auto *Contribs = Row.Contributions.get()) {
236725c4f71SDavid Blaikie       OS << format("%5u 0x%016" PRIx64 " ", i + 1, Row.Signature);
2378e8dd57eSDavid Blaikie       for (unsigned i = 0; i != Header.NumColumns; ++i) {
2388e8dd57eSDavid Blaikie         auto &Contrib = Contribs[i];
23982641be4SDavid Blaikie         OS << format("[0x%08x, 0x%08x) ", Contrib.Offset,
24082641be4SDavid Blaikie                      Contrib.Offset + Contrib.Length);
2415b9bf49cSDavid Blaikie       }
2428e8dd57eSDavid Blaikie       OS << '\n';
2438e8dd57eSDavid Blaikie     }
2448e8dd57eSDavid Blaikie   }
2458e8dd57eSDavid Blaikie }
24682641be4SDavid Blaikie 
24782641be4SDavid Blaikie const DWARFUnitIndex::Entry::SectionContribution *
getContribution(DWARFSectionKind Sec) const248f13ce15dSIgor Kudrin DWARFUnitIndex::Entry::getContribution(DWARFSectionKind Sec) const {
24982641be4SDavid Blaikie   uint32_t i = 0;
25082641be4SDavid Blaikie   for (; i != Index->Header.NumColumns; ++i)
25182641be4SDavid Blaikie     if (Index->ColumnKinds[i] == Sec)
25282641be4SDavid Blaikie       return &Contributions[i];
25382641be4SDavid Blaikie   return nullptr;
25482641be4SDavid Blaikie }
25528db7e65SEugene Zelenko 
25682641be4SDavid Blaikie const DWARFUnitIndex::Entry::SectionContribution *
getContribution() const257f13ce15dSIgor Kudrin DWARFUnitIndex::Entry::getContribution() const {
25882641be4SDavid Blaikie   return &Contributions[Index->InfoColumn];
25982641be4SDavid Blaikie }
26082641be4SDavid Blaikie 
26182641be4SDavid Blaikie const DWARFUnitIndex::Entry *
getFromOffset(uint32_t Offset) const26282641be4SDavid Blaikie DWARFUnitIndex::getFromOffset(uint32_t Offset) const {
263da36f3f4SDavid Blaikie   if (OffsetLookup.empty()) {
26482641be4SDavid Blaikie     for (uint32_t i = 0; i != Header.NumBuckets; ++i)
2655b65e41aSSimon Pilgrim       if (Rows[i].Contributions)
266da36f3f4SDavid Blaikie         OffsetLookup.push_back(&Rows[i]);
267da36f3f4SDavid Blaikie     llvm::sort(OffsetLookup, [&](Entry *E1, Entry *E2) {
268da36f3f4SDavid Blaikie       return E1->Contributions[InfoColumn].Offset <
269da36f3f4SDavid Blaikie              E2->Contributions[InfoColumn].Offset;
270da36f3f4SDavid Blaikie     });
271e79dda31SDavid Blaikie   }
27278ee2fbfSFangrui Song   auto I = partition_point(OffsetLookup, [&](Entry *E2) {
27378ee2fbfSFangrui Song     return E2->Contributions[InfoColumn].Offset <= Offset;
274da36f3f4SDavid Blaikie   });
275da36f3f4SDavid Blaikie   if (I == OffsetLookup.begin())
27682641be4SDavid Blaikie     return nullptr;
277da36f3f4SDavid Blaikie   --I;
278da36f3f4SDavid Blaikie   const auto *E = *I;
279da36f3f4SDavid Blaikie   const auto &InfoContrib = E->Contributions[InfoColumn];
280da36f3f4SDavid Blaikie   if ((InfoContrib.Offset + InfoContrib.Length) <= Offset)
281da36f3f4SDavid Blaikie     return nullptr;
282da36f3f4SDavid Blaikie   return E;
28382641be4SDavid Blaikie }
284ebac0b9cSDavid Blaikie 
getFromHash(uint64_t S) const285ebac0b9cSDavid Blaikie const DWARFUnitIndex::Entry *DWARFUnitIndex::getFromHash(uint64_t S) const {
286ebac0b9cSDavid Blaikie   uint64_t Mask = Header.NumBuckets - 1;
287ebac0b9cSDavid Blaikie 
288ebac0b9cSDavid Blaikie   auto H = S & Mask;
289ebac0b9cSDavid Blaikie   auto HP = ((S >> 32) & Mask) | 1;
290314a0d73SJorge Gorbe Moya   // The spec says "while 0 is a valid hash value, the row index in a used slot
291314a0d73SJorge Gorbe Moya   // will always be non-zero". Loop until we find a match or an empty slot.
292314a0d73SJorge Gorbe Moya   while (Rows[H].getSignature() != S && Rows[H].Index != nullptr)
293ebac0b9cSDavid Blaikie     H = (H + HP) & Mask;
294ebac0b9cSDavid Blaikie 
295314a0d73SJorge Gorbe Moya   // If the slot is empty, we don't care whether the signature matches (it could
296314a0d73SJorge Gorbe Moya   // be zero and still match the zeros in the empty slot).
297314a0d73SJorge Gorbe Moya   if (Rows[H].Index == nullptr)
298ebac0b9cSDavid Blaikie     return nullptr;
299ebac0b9cSDavid Blaikie 
300ebac0b9cSDavid Blaikie   return &Rows[H];
301ebac0b9cSDavid Blaikie }
302