1//===- DWARFDebugAbbrev.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
9#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
10#include "llvm/Support/FormatVariadic.h"
11#include "llvm/Support/raw_ostream.h"
12#include <cstdint>
13
14using namespace llvm;
15
16bool llvm::readAbbrevAttribute(const DataExtractor &AbbrevData,
17 uint64_t *Offset, dwarf::Attribute &Name,
18 dwarf::Form &Form,
19 std::optional<int64_t> &ImplicitConst) {
20 Name = static_cast<dwarf::Attribute>(AbbrevData.getULEB128(offset_ptr: Offset));
21 Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(offset_ptr: Offset));
22 ImplicitConst = std::nullopt;
23 if (Form == dwarf::DW_FORM_implicit_const)
24 ImplicitConst = AbbrevData.getSLEB128(OffsetPtr: Offset);
25 return Name != 0 || Form != 0;
26}
27
28DWARFAbbreviationDeclarationSet::DWARFAbbreviationDeclarationSet() {
29 clear();
30}
31
32void DWARFAbbreviationDeclarationSet::clear() {
33 Offset = 0;
34 FirstAbbrCode = 0;
35 Decls.clear();
36}
37
38Error DWARFAbbreviationDeclarationSet::extract(DataExtractor Data,
39 uint64_t *OffsetPtr) {
40 clear();
41 const uint64_t BeginOffset = *OffsetPtr;
42 Offset = BeginOffset;
43 DWARFAbbreviationDeclaration AbbrDecl;
44 uint32_t PrevAbbrCode = 0;
45 while (true) {
46 Expected<DWARFAbbreviationDeclaration::ExtractState> ES =
47 AbbrDecl.extract(Data, OffsetPtr);
48 if (!ES)
49 return ES.takeError();
50
51 if (*ES == DWARFAbbreviationDeclaration::ExtractState::Complete)
52 break;
53
54 if (FirstAbbrCode == 0) {
55 FirstAbbrCode = AbbrDecl.getCode();
56 } else if (PrevAbbrCode + 1 != AbbrDecl.getCode()) {
57 // Codes are not consecutive, can't do O(1) lookups.
58 FirstAbbrCode = UINT32_MAX;
59 }
60 PrevAbbrCode = AbbrDecl.getCode();
61 Decls.push_back(x: std::move(AbbrDecl));
62 }
63 Decls.shrink_to_fit();
64 return Error::success();
65}
66
67void DWARFAbbreviationDeclarationSet::dump(raw_ostream &OS) const {
68 for (const auto &Decl : Decls)
69 Decl.dump(OS);
70}
71
72const DWARFAbbreviationDeclaration *
73DWARFAbbreviationDeclarationSet::getAbbreviationDeclaration(
74 uint32_t AbbrCode) const {
75 if (FirstAbbrCode == UINT32_MAX) {
76 for (const auto &Decl : Decls) {
77 if (Decl.getCode() == AbbrCode)
78 return &Decl;
79 }
80 return nullptr;
81 }
82 if (AbbrCode < FirstAbbrCode || AbbrCode >= FirstAbbrCode + Decls.size())
83 return nullptr;
84 return &Decls[AbbrCode - FirstAbbrCode];
85}
86
87std::string DWARFAbbreviationDeclarationSet::getCodeRange() const {
88 // Create a sorted list of all abbrev codes.
89 std::vector<uint32_t> Codes;
90 Codes.reserve(n: Decls.size());
91 for (const auto &Decl : Decls)
92 Codes.push_back(x: Decl.getCode());
93
94 std::string Buffer;
95 raw_string_ostream Stream(Buffer);
96 // Each iteration through this loop represents a single contiguous range in
97 // the set of codes.
98 for (auto Current = Codes.begin(), End = Codes.end(); Current != End;) {
99 uint32_t RangeStart = *Current;
100 // Add the current range start.
101 Stream << *Current;
102 uint32_t RangeEnd = RangeStart;
103 // Find the end of the current range.
104 while (++Current != End && *Current == RangeEnd + 1)
105 ++RangeEnd;
106 // If there is more than one value in the range, add the range end too.
107 if (RangeStart != RangeEnd)
108 Stream << "-" << RangeEnd;
109 // If there is at least one more range, add a separator.
110 if (Current != End)
111 Stream << ", ";
112 }
113 return Buffer;
114}
115
116DWARFDebugAbbrev::DWARFDebugAbbrev(DataExtractor Data)
117 : AbbrDeclSets(), PrevAbbrOffsetPos(AbbrDeclSets.end()), Data(Data) {}
118
119Error DWARFDebugAbbrev::parse() const {
120 if (!Data)
121 return Error::success();
122 uint64_t Offset = 0;
123 auto I = AbbrDeclSets.begin();
124 while (Data->isValidOffset(offset: Offset)) {
125 while (I != AbbrDeclSets.end() && I->first < Offset)
126 ++I;
127 uint64_t CUAbbrOffset = Offset;
128 DWARFAbbreviationDeclarationSet AbbrDecls;
129 if (Error Err = AbbrDecls.extract(Data: *Data, OffsetPtr: &Offset)) {
130 Data = std::nullopt;
131 return Err;
132 }
133 AbbrDeclSets.insert(position: I, x: std::make_pair(x&: CUAbbrOffset, y: std::move(AbbrDecls)));
134 }
135 Data = std::nullopt;
136 return Error::success();
137}
138
139void DWARFDebugAbbrev::dump(raw_ostream &OS) const {
140 if (Error Err = parse())
141 // FIXME: We should propagate this error or otherwise display it.
142 llvm::consumeError(Err: std::move(Err));
143
144 if (AbbrDeclSets.empty()) {
145 OS << "< EMPTY >\n";
146 return;
147 }
148
149 for (const auto &I : AbbrDeclSets) {
150 OS << formatv(Fmt: "Abbrev table for offset: {0:x+8}\n", Vals: I.first);
151 I.second.dump(OS);
152 }
153}
154
155Expected<const DWARFAbbreviationDeclarationSet *>
156DWARFDebugAbbrev::getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const {
157 const auto End = AbbrDeclSets.end();
158 if (PrevAbbrOffsetPos != End && PrevAbbrOffsetPos->first == CUAbbrOffset) {
159 return &PrevAbbrOffsetPos->second;
160 }
161
162 const auto Pos = AbbrDeclSets.find(x: CUAbbrOffset);
163 if (Pos != End) {
164 PrevAbbrOffsetPos = Pos;
165 return &Pos->second;
166 }
167
168 if (!Data || CUAbbrOffset >= Data->getData().size())
169 return make_error<llvm::object::GenericBinaryError>(
170 Args: "the abbreviation offset into the .debug_abbrev section is not valid");
171
172 uint64_t Offset = CUAbbrOffset;
173 DWARFAbbreviationDeclarationSet AbbrDecls;
174 if (Error Err = AbbrDecls.extract(Data: *Data, OffsetPtr: &Offset))
175 return std::move(Err);
176
177 PrevAbbrOffsetPos =
178 AbbrDeclSets.insert(x: std::make_pair(x&: CUAbbrOffset, y: std::move(AbbrDecls)))
179 .first;
180 return &PrevAbbrOffsetPos->second;
181}
182