DWARFDebugAbbrev.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===-- DWARFDebugAbbrev.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/DWARF/DWARFDebugAbbrev.h"
  10. #include "llvm/Support/Format.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. using namespace llvm;
  13. DWARFAbbreviationDeclarationSet::DWARFAbbreviationDeclarationSet() {
  14. clear();
  15. }
  16. void DWARFAbbreviationDeclarationSet::clear() {
  17. Offset = 0;
  18. FirstAbbrCode = 0;
  19. Decls.clear();
  20. }
  21. bool DWARFAbbreviationDeclarationSet::extract(DataExtractor Data,
  22. uint32_t *OffsetPtr) {
  23. clear();
  24. const uint32_t BeginOffset = *OffsetPtr;
  25. Offset = BeginOffset;
  26. DWARFAbbreviationDeclaration AbbrDecl;
  27. uint32_t PrevAbbrCode = 0;
  28. while (AbbrDecl.extract(Data, OffsetPtr)) {
  29. if (FirstAbbrCode == 0) {
  30. FirstAbbrCode = AbbrDecl.getCode();
  31. } else {
  32. if (PrevAbbrCode + 1 != AbbrDecl.getCode()) {
  33. // Codes are not consecutive, can't do O(1) lookups.
  34. FirstAbbrCode = UINT32_MAX;
  35. }
  36. }
  37. PrevAbbrCode = AbbrDecl.getCode();
  38. Decls.push_back(std::move(AbbrDecl));
  39. }
  40. return BeginOffset != *OffsetPtr;
  41. }
  42. void DWARFAbbreviationDeclarationSet::dump(raw_ostream &OS) const {
  43. for (const auto &Decl : Decls)
  44. Decl.dump(OS);
  45. }
  46. const DWARFAbbreviationDeclaration *
  47. DWARFAbbreviationDeclarationSet::getAbbreviationDeclaration(
  48. uint32_t AbbrCode) const {
  49. if (FirstAbbrCode == UINT32_MAX) {
  50. for (const auto &Decl : Decls) {
  51. if (Decl.getCode() == AbbrCode)
  52. return &Decl;
  53. }
  54. return nullptr;
  55. }
  56. if (AbbrCode < FirstAbbrCode || AbbrCode >= FirstAbbrCode + Decls.size())
  57. return nullptr;
  58. return &Decls[AbbrCode - FirstAbbrCode];
  59. }
  60. DWARFDebugAbbrev::DWARFDebugAbbrev() {
  61. clear();
  62. }
  63. void DWARFDebugAbbrev::clear() {
  64. AbbrDeclSets.clear();
  65. PrevAbbrOffsetPos = AbbrDeclSets.end();
  66. }
  67. void DWARFDebugAbbrev::extract(DataExtractor Data) {
  68. clear();
  69. uint32_t Offset = 0;
  70. DWARFAbbreviationDeclarationSet AbbrDecls;
  71. while (Data.isValidOffset(Offset)) {
  72. uint32_t CUAbbrOffset = Offset;
  73. if (!AbbrDecls.extract(Data, &Offset))
  74. break;
  75. AbbrDeclSets[CUAbbrOffset] = std::move(AbbrDecls);
  76. }
  77. }
  78. void DWARFDebugAbbrev::dump(raw_ostream &OS) const {
  79. if (AbbrDeclSets.empty()) {
  80. OS << "< EMPTY >\n";
  81. return;
  82. }
  83. for (const auto &I : AbbrDeclSets) {
  84. OS << format("Abbrev table for offset: 0x%8.8" PRIx64 "\n", I.first);
  85. I.second.dump(OS);
  86. }
  87. }
  88. const DWARFAbbreviationDeclarationSet*
  89. DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const {
  90. const auto End = AbbrDeclSets.end();
  91. if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) {
  92. return &(PrevAbbrOffsetPos->second);
  93. }
  94. const auto Pos = AbbrDeclSets.find(CUAbbrOffset);
  95. if (Pos != End) {
  96. PrevAbbrOffsetPos = Pos;
  97. return &(Pos->second);
  98. }
  99. return nullptr;
  100. }