| Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 1 | //===- DWARFDebugAbbrev.cpp -----------------------------------------------===// |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 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 | |
| Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Format.h" |
| 12 | #include "llvm/Support/raw_ostream.h" |
| Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <cinttypes> |
| 15 | #include <cstdint> |
| 16 | |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 19 | DWARFAbbreviationDeclarationSet::DWARFAbbreviationDeclarationSet() { |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 20 | clear(); |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | void DWARFAbbreviationDeclarationSet::clear() { |
| 24 | Offset = 0; |
| 25 | FirstAbbrCode = 0; |
| 26 | Decls.clear(); |
| 27 | } |
| 28 | |
| 29 | bool DWARFAbbreviationDeclarationSet::extract(DataExtractor Data, |
| 30 | uint32_t *OffsetPtr) { |
| 31 | clear(); |
| 32 | const uint32_t BeginOffset = *OffsetPtr; |
| 33 | Offset = BeginOffset; |
| 34 | DWARFAbbreviationDeclaration AbbrDecl; |
| 35 | uint32_t PrevAbbrCode = 0; |
| 36 | while (AbbrDecl.extract(Data, OffsetPtr)) { |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 37 | if (FirstAbbrCode == 0) { |
| 38 | FirstAbbrCode = AbbrDecl.getCode(); |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 39 | } else { |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 40 | if (PrevAbbrCode + 1 != AbbrDecl.getCode()) { |
| 41 | // Codes are not consecutive, can't do O(1) lookups. |
| 42 | FirstAbbrCode = UINT32_MAX; |
| 43 | } |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 44 | } |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 45 | PrevAbbrCode = AbbrDecl.getCode(); |
| Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 46 | Decls.push_back(std::move(AbbrDecl)); |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 47 | } |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 48 | return BeginOffset != *OffsetPtr; |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 51 | void DWARFAbbreviationDeclarationSet::dump(raw_ostream &OS) const { |
| Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 52 | for (const auto &Decl : Decls) |
| 53 | Decl.dump(OS); |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 56 | const DWARFAbbreviationDeclaration * |
| 57 | DWARFAbbreviationDeclarationSet::getAbbreviationDeclaration( |
| 58 | uint32_t AbbrCode) const { |
| 59 | if (FirstAbbrCode == UINT32_MAX) { |
| Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 60 | for (const auto &Decl : Decls) { |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 61 | if (Decl.getCode() == AbbrCode) |
| Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 62 | return &Decl; |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 63 | } |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 64 | return nullptr; |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 65 | } |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 66 | if (AbbrCode < FirstAbbrCode || AbbrCode >= FirstAbbrCode + Decls.size()) |
| 67 | return nullptr; |
| 68 | return &Decls[AbbrCode - FirstAbbrCode]; |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 71 | DWARFDebugAbbrev::DWARFDebugAbbrev() { clear(); } |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 72 | |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 73 | void DWARFDebugAbbrev::clear() { |
| 74 | AbbrDeclSets.clear(); |
| 75 | PrevAbbrOffsetPos = AbbrDeclSets.end(); |
| 76 | } |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 77 | |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 78 | void DWARFDebugAbbrev::extract(DataExtractor Data) { |
| 79 | clear(); |
| David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 80 | this->Data = Data; |
| 81 | } |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 82 | |
| David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 83 | void DWARFDebugAbbrev::parse() const { |
| 84 | if (!Data) |
| 85 | return; |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 86 | uint32_t Offset = 0; |
| 87 | DWARFAbbreviationDeclarationSet AbbrDecls; |
| David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 88 | auto I = AbbrDeclSets.begin(); |
| 89 | while (Data->isValidOffset(Offset)) { |
| 90 | while (I != AbbrDeclSets.end() && I->first < Offset) |
| 91 | ++I; |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 92 | uint32_t CUAbbrOffset = Offset; |
| David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 93 | if (!AbbrDecls.extract(*Data, &Offset)) |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 94 | break; |
| David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 95 | AbbrDeclSets.insert(I, std::make_pair(CUAbbrOffset, std::move(AbbrDecls))); |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 96 | } |
| David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 97 | Data = None; |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void DWARFDebugAbbrev::dump(raw_ostream &OS) const { |
| David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 101 | parse(); |
| 102 | |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 103 | if (AbbrDeclSets.empty()) { |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 104 | OS << "< EMPTY >\n"; |
| 105 | return; |
| 106 | } |
| 107 | |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 108 | for (const auto &I : AbbrDeclSets) { |
| Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 109 | OS << format("Abbrev table for offset: 0x%8.8" PRIx64 "\n", I.first); |
| 110 | I.second.dump(OS); |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | const DWARFAbbreviationDeclarationSet* |
| Alexey Samsonov | 9a5c95a | 2014-04-24 22:41:09 +0000 | [diff] [blame] | 115 | DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const { |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 116 | const auto End = AbbrDeclSets.end(); |
| Alexey Samsonov | 9a5c95a | 2014-04-24 22:41:09 +0000 | [diff] [blame] | 117 | if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) { |
| Benjamin Kramer | eaa7433 | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 118 | return &(PrevAbbrOffsetPos->second); |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| Alexey Samsonov | 4316df5 | 2014-04-25 21:10:56 +0000 | [diff] [blame] | 121 | const auto Pos = AbbrDeclSets.find(CUAbbrOffset); |
| Alexey Samsonov | 9a5c95a | 2014-04-24 22:41:09 +0000 | [diff] [blame] | 122 | if (Pos != End) { |
| 123 | PrevAbbrOffsetPos = Pos; |
| 124 | return &(Pos->second); |
| 125 | } |
| 126 | |
| David Blaikie | 485e01b | 2017-09-19 15:13:55 +0000 | [diff] [blame] | 127 | if (Data && CUAbbrOffset < Data->getData().size()) { |
| 128 | uint32_t Offset = CUAbbrOffset; |
| 129 | DWARFAbbreviationDeclarationSet AbbrDecls; |
| 130 | if (!AbbrDecls.extract(*Data, &Offset)) |
| 131 | return nullptr; |
| 132 | PrevAbbrOffsetPos = |
| 133 | AbbrDeclSets.insert(std::make_pair(CUAbbrOffset, std::move(AbbrDecls))) |
| 134 | .first; |
| 135 | return &PrevAbbrOffsetPos->second; |
| 136 | } |
| 137 | |
| Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 138 | return nullptr; |
| Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 139 | } |