DWARFUnitIndex.cpp 9.1 KB

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