DWARFGdbIndex.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //===- DWARFGdbIndex.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/DWARFGdbIndex.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/Support/Format.h"
  12. #include "llvm/Support/FormatVariadic.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. #include <algorithm>
  15. #include <cassert>
  16. #include <cinttypes>
  17. #include <cstdint>
  18. #include <utility>
  19. using namespace llvm;
  20. // .gdb_index section format reference:
  21. // https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html
  22. void DWARFGdbIndex::dumpCUList(raw_ostream &OS) const {
  23. OS << format("\n CU list offset = 0x%x, has %" PRId64 " entries:",
  24. CuListOffset, (uint64_t)CuList.size())
  25. << '\n';
  26. uint32_t I = 0;
  27. for (const CompUnitEntry &CU : CuList)
  28. OS << format(" %d: Offset = 0x%llx, Length = 0x%llx\n", I++, CU.Offset,
  29. CU.Length);
  30. }
  31. void DWARFGdbIndex::dumpTUList(raw_ostream &OS) const {
  32. OS << formatv("\n Types CU list offset = {0:x}, has {1} entries:\n",
  33. TuListOffset, TuList.size());
  34. uint32_t I = 0;
  35. for (const TypeUnitEntry &TU : TuList)
  36. OS << formatv(" {0}: offset = {1:x8}, type_offset = {2:x8}, "
  37. "type_signature = {3:x16}\n",
  38. I++, TU.Offset, TU.TypeOffset, TU.TypeSignature);
  39. }
  40. void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const {
  41. OS << format("\n Address area offset = 0x%x, has %" PRId64 " entries:",
  42. AddressAreaOffset, (uint64_t)AddressArea.size())
  43. << '\n';
  44. for (const AddressEntry &Addr : AddressArea)
  45. OS << format(
  46. " Low/High address = [0x%llx, 0x%llx) (Size: 0x%llx), CU id = %d\n",
  47. Addr.LowAddress, Addr.HighAddress, Addr.HighAddress - Addr.LowAddress,
  48. Addr.CuIndex);
  49. }
  50. void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const {
  51. OS << format("\n Symbol table offset = 0x%x, size = %" PRId64
  52. ", filled slots:",
  53. SymbolTableOffset, (uint64_t)SymbolTable.size())
  54. << '\n';
  55. uint32_t I = -1;
  56. for (const SymTableEntry &E : SymbolTable) {
  57. ++I;
  58. if (!E.NameOffset && !E.VecOffset)
  59. continue;
  60. OS << format(" %d: Name offset = 0x%x, CU vector offset = 0x%x\n", I,
  61. E.NameOffset, E.VecOffset);
  62. StringRef Name = ConstantPoolStrings.substr(
  63. ConstantPoolOffset - StringPoolOffset + E.NameOffset);
  64. auto CuVector = llvm::find_if(
  65. ConstantPoolVectors,
  66. [&](const std::pair<uint32_t, SmallVector<uint32_t, 0>> &V) {
  67. return V.first == E.VecOffset;
  68. });
  69. assert(CuVector != ConstantPoolVectors.end() && "Invalid symbol table");
  70. uint32_t CuVectorId = CuVector - ConstantPoolVectors.begin();
  71. OS << format(" String name: %s, CU vector index: %d\n", Name.data(),
  72. CuVectorId);
  73. }
  74. }
  75. void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {
  76. OS << format("\n Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",
  77. ConstantPoolOffset, (uint64_t)ConstantPoolVectors.size());
  78. uint32_t I = 0;
  79. for (const auto &V : ConstantPoolVectors) {
  80. OS << format("\n %d(0x%x): ", I++, V.first);
  81. for (uint32_t Val : V.second)
  82. OS << format("0x%x ", Val);
  83. }
  84. OS << '\n';
  85. }
  86. void DWARFGdbIndex::dump(raw_ostream &OS) {
  87. if (HasError) {
  88. OS << "\n<error parsing>\n";
  89. return;
  90. }
  91. if (HasContent) {
  92. OS << " Version = " << Version << '\n';
  93. dumpCUList(OS);
  94. dumpTUList(OS);
  95. dumpAddressArea(OS);
  96. dumpSymbolTable(OS);
  97. dumpConstantPool(OS);
  98. }
  99. }
  100. bool DWARFGdbIndex::parseImpl(DataExtractor Data) {
  101. uint64_t Offset = 0;
  102. // Only version 7 is supported at this moment.
  103. Version = Data.getU32(&Offset);
  104. if (Version != 7)
  105. return false;
  106. CuListOffset = Data.getU32(&Offset);
  107. TuListOffset = Data.getU32(&Offset);
  108. AddressAreaOffset = Data.getU32(&Offset);
  109. SymbolTableOffset = Data.getU32(&Offset);
  110. ConstantPoolOffset = Data.getU32(&Offset);
  111. if (Offset != CuListOffset)
  112. return false;
  113. uint32_t CuListSize = (TuListOffset - CuListOffset) / 16;
  114. CuList.reserve(CuListSize);
  115. for (uint32_t i = 0; i < CuListSize; ++i) {
  116. uint64_t CuOffset = Data.getU64(&Offset);
  117. uint64_t CuLength = Data.getU64(&Offset);
  118. CuList.push_back({CuOffset, CuLength});
  119. }
  120. // CU Types are no longer needed as DWARF skeleton type units never made it
  121. // into the standard.
  122. uint32_t TuListSize = (AddressAreaOffset - TuListOffset) / 24;
  123. TuList.resize(TuListSize);
  124. for (uint32_t I = 0; I < TuListSize; ++I) {
  125. uint64_t CuOffset = Data.getU64(&Offset);
  126. uint64_t TypeOffset = Data.getU64(&Offset);
  127. uint64_t Signature = Data.getU64(&Offset);
  128. TuList[I] = {CuOffset, TypeOffset, Signature};
  129. }
  130. uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;
  131. AddressArea.reserve(AddressAreaSize);
  132. for (uint32_t i = 0; i < AddressAreaSize; ++i) {
  133. uint64_t LowAddress = Data.getU64(&Offset);
  134. uint64_t HighAddress = Data.getU64(&Offset);
  135. uint32_t CuIndex = Data.getU32(&Offset);
  136. AddressArea.push_back({LowAddress, HighAddress, CuIndex});
  137. }
  138. // The symbol table. This is an open addressed hash table. The size of the
  139. // hash table is always a power of 2.
  140. // Each slot in the hash table consists of a pair of offset_type values. The
  141. // first value is the offset of the symbol's name in the constant pool. The
  142. // second value is the offset of the CU vector in the constant pool.
  143. // If both values are 0, then this slot in the hash table is empty. This is ok
  144. // because while 0 is a valid constant pool index, it cannot be a valid index
  145. // for both a string and a CU vector.
  146. uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8;
  147. SymbolTable.reserve(SymTableSize);
  148. uint32_t CuVectorsTotal = 0;
  149. for (uint32_t i = 0; i < SymTableSize; ++i) {
  150. uint32_t NameOffset = Data.getU32(&Offset);
  151. uint32_t CuVecOffset = Data.getU32(&Offset);
  152. SymbolTable.push_back({NameOffset, CuVecOffset});
  153. if (NameOffset || CuVecOffset)
  154. ++CuVectorsTotal;
  155. }
  156. // The constant pool. CU vectors are stored first, followed by strings.
  157. // The first value is the number of CU indices in the vector. Each subsequent
  158. // value is the index and symbol attributes of a CU in the CU list.
  159. for (uint32_t i = 0; i < CuVectorsTotal; ++i) {
  160. ConstantPoolVectors.emplace_back(0, SmallVector<uint32_t, 0>());
  161. auto &Vec = ConstantPoolVectors.back();
  162. Vec.first = Offset - ConstantPoolOffset;
  163. uint32_t Num = Data.getU32(&Offset);
  164. for (uint32_t j = 0; j < Num; ++j)
  165. Vec.second.push_back(Data.getU32(&Offset));
  166. }
  167. ConstantPoolStrings = Data.getData().drop_front(Offset);
  168. StringPoolOffset = Offset;
  169. return true;
  170. }
  171. void DWARFGdbIndex::parse(DataExtractor Data) {
  172. HasContent = !Data.getData().empty();
  173. HasError = HasContent && !parseImpl(Data);
  174. }