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