DWARFAcceleratorTable.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //===--- DWARFAcceleratorTable.cpp ----------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/DebugInfo/DWARFAcceleratorTable.h"
  10. #include "llvm/Support/Dwarf.h"
  11. #include "llvm/Support/Format.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. namespace llvm {
  14. bool DWARFAcceleratorTable::extract() {
  15. uint32_t Offset = 0;
  16. // Check that we can at least read the header.
  17. if (!AccelSection.isValidOffset(offsetof(Header, HeaderDataLength)+4))
  18. return false;
  19. Hdr.Magic = AccelSection.getU32(&Offset);
  20. Hdr.Version = AccelSection.getU16(&Offset);
  21. Hdr.HashFunction = AccelSection.getU16(&Offset);
  22. Hdr.NumBuckets = AccelSection.getU32(&Offset);
  23. Hdr.NumHashes = AccelSection.getU32(&Offset);
  24. Hdr.HeaderDataLength = AccelSection.getU32(&Offset);
  25. // Check that we can read all the hashes and offsets from the
  26. // section (see SourceLevelDebugging.rst for the structure of the index).
  27. if (!AccelSection.isValidOffset(sizeof(Hdr) + Hdr.HeaderDataLength +
  28. Hdr.NumBuckets*4 + Hdr.NumHashes*8))
  29. return false;
  30. HdrData.DIEOffsetBase = AccelSection.getU32(&Offset);
  31. uint32_t NumAtoms = AccelSection.getU32(&Offset);
  32. for (unsigned i = 0; i < NumAtoms; ++i) {
  33. uint16_t AtomType = AccelSection.getU16(&Offset);
  34. uint16_t AtomForm = AccelSection.getU16(&Offset);
  35. HdrData.Atoms.push_back(std::make_pair(AtomType, AtomForm));
  36. }
  37. return true;
  38. }
  39. void DWARFAcceleratorTable::dump(raw_ostream &OS) const {
  40. // Dump the header.
  41. OS << "Magic = " << format("0x%08x", Hdr.Magic) << '\n'
  42. << "Version = " << format("0x%04x", Hdr.Version) << '\n'
  43. << "Hash function = " << format("0x%08x", Hdr.HashFunction) << '\n'
  44. << "Bucket count = " << Hdr.NumBuckets << '\n'
  45. << "Hashes count = " << Hdr.NumHashes << '\n'
  46. << "HeaderData length = " << Hdr.HeaderDataLength << '\n'
  47. << "DIE offset base = " << HdrData.DIEOffsetBase << '\n'
  48. << "Number of atoms = " << HdrData.Atoms.size() << '\n';
  49. unsigned i = 0;
  50. SmallVector<DWARFFormValue, 3> AtomForms;
  51. for (const auto &Atom: HdrData.Atoms) {
  52. OS << format("Atom[%d] Type: ", i++);
  53. if (const char *TypeString = dwarf::AtomTypeString(Atom.first))
  54. OS << TypeString;
  55. else
  56. OS << format("DW_ATOM_Unknown_0x%x", Atom.first);
  57. OS << " Form: ";
  58. if (const char *FormString = dwarf::FormEncodingString(Atom.second))
  59. OS << FormString;
  60. else
  61. OS << format("DW_FORM_Unknown_0x%x", Atom.second);
  62. OS << '\n';
  63. AtomForms.push_back(DWARFFormValue(Atom.second));
  64. }
  65. // Now go through the actual tables and dump them.
  66. uint32_t Offset = sizeof(Hdr) + Hdr.HeaderDataLength;
  67. unsigned HashesBase = Offset + Hdr.NumBuckets * 4;
  68. unsigned OffsetsBase = HashesBase + Hdr.NumHashes * 4;
  69. for (unsigned Bucket = 0; Bucket < Hdr.NumBuckets; ++Bucket) {
  70. unsigned Index = AccelSection.getU32(&Offset);
  71. OS << format("Bucket[%d]\n", Bucket);
  72. if (Index == UINT32_MAX) {
  73. OS << " EMPTY\n";
  74. continue;
  75. }
  76. for (unsigned HashIdx = Index; HashIdx < Hdr.NumHashes; ++HashIdx) {
  77. unsigned HashOffset = HashesBase + HashIdx*4;
  78. unsigned OffsetsOffset = OffsetsBase + HashIdx*4;
  79. uint32_t Hash = AccelSection.getU32(&HashOffset);
  80. if (Hash % Hdr.NumBuckets != Bucket)
  81. break;
  82. unsigned DataOffset = AccelSection.getU32(&OffsetsOffset);
  83. OS << format(" Hash = 0x%08x Offset = 0x%08x\n", Hash, DataOffset);
  84. if (!AccelSection.isValidOffset(DataOffset)) {
  85. OS << " Invalid section offset\n";
  86. continue;
  87. }
  88. while (AccelSection.isValidOffsetForDataOfSize(DataOffset, 4)) {
  89. unsigned StringOffset = AccelSection.getU32(&DataOffset);
  90. RelocAddrMap::const_iterator Reloc = Relocs.find(DataOffset-4);
  91. if (Reloc != Relocs.end())
  92. StringOffset += Reloc->second.second;
  93. if (!StringOffset)
  94. break;
  95. OS << format(" Name: %08x \"%s\"\n", StringOffset,
  96. StringSection.getCStr(&StringOffset));
  97. unsigned NumData = AccelSection.getU32(&DataOffset);
  98. for (unsigned Data = 0; Data < NumData; ++Data) {
  99. OS << format(" Data[%d] => ", Data);
  100. unsigned i = 0;
  101. for (auto &Atom : AtomForms) {
  102. OS << format("{Atom[%d]: ", i++);
  103. if (Atom.extractValue(AccelSection, &DataOffset, nullptr))
  104. Atom.dump(OS, nullptr);
  105. else
  106. OS << "Error extracting the value";
  107. OS << "} ";
  108. }
  109. OS << '\n';
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }