LLVM 19.0.0git
DWARFGdbIndex.cpp
Go to the documentation of this file.
1//===- DWARFGdbIndex.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
11#include "llvm/ADT/StringRef.h"
13#include "llvm/Support/Format.h"
16#include <cassert>
17#include <cinttypes>
18#include <cstdint>
19#include <set>
20#include <utility>
21
22using namespace llvm;
23
24// .gdb_index section format reference:
25// https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html
26
27void DWARFGdbIndex::dumpCUList(raw_ostream &OS) const {
28 OS << format("\n CU list offset = 0x%x, has %" PRId64 " entries:",
29 CuListOffset, (uint64_t)CuList.size())
30 << '\n';
31 uint32_t I = 0;
32 for (const CompUnitEntry &CU : CuList)
33 OS << format(" %d: Offset = 0x%llx, Length = 0x%llx\n", I++, CU.Offset,
34 CU.Length);
35}
36
37void DWARFGdbIndex::dumpTUList(raw_ostream &OS) const {
38 OS << formatv("\n Types CU list offset = {0:x}, has {1} entries:\n",
39 TuListOffset, TuList.size());
40 uint32_t I = 0;
41 for (const TypeUnitEntry &TU : TuList)
42 OS << formatv(" {0}: offset = {1:x8}, type_offset = {2:x8}, "
43 "type_signature = {3:x16}\n",
44 I++, TU.Offset, TU.TypeOffset, TU.TypeSignature);
45}
46
47void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const {
48 OS << format("\n Address area offset = 0x%x, has %" PRId64 " entries:",
49 AddressAreaOffset, (uint64_t)AddressArea.size())
50 << '\n';
51 for (const AddressEntry &Addr : AddressArea)
52 OS << format(
53 " Low/High address = [0x%llx, 0x%llx) (Size: 0x%llx), CU id = %d\n",
54 Addr.LowAddress, Addr.HighAddress, Addr.HighAddress - Addr.LowAddress,
55 Addr.CuIndex);
56}
57
58void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const {
59 OS << format("\n Symbol table offset = 0x%x, size = %" PRId64
60 ", filled slots:",
61 SymbolTableOffset, (uint64_t)SymbolTable.size())
62 << '\n';
63 uint32_t I = -1;
64 for (const SymTableEntry &E : SymbolTable) {
65 ++I;
66 if (!E.NameOffset && !E.VecOffset)
67 continue;
68
69 OS << format(" %d: Name offset = 0x%x, CU vector offset = 0x%x\n", I,
70 E.NameOffset, E.VecOffset);
71
72 StringRef Name = ConstantPoolStrings.substr(
73 ConstantPoolOffset - StringPoolOffset + E.NameOffset);
74
75 auto CuVector = llvm::find_if(
76 ConstantPoolVectors,
77 [&](const std::pair<uint32_t, SmallVector<uint32_t, 0>> &V) {
78 return V.first == E.VecOffset;
79 });
80 assert(CuVector != ConstantPoolVectors.end() && "Invalid symbol table");
81 uint32_t CuVectorId = CuVector - ConstantPoolVectors.begin();
82 OS << format(" String name: %s, CU vector index: %d\n", Name.data(),
83 CuVectorId);
84 }
85}
86
87void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {
88 OS << format("\n Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",
89 ConstantPoolOffset, (uint64_t)ConstantPoolVectors.size());
90 uint32_t I = 0;
91 for (const auto &V : ConstantPoolVectors) {
92 OS << format("\n %d(0x%x): ", I++, V.first);
93 for (uint32_t Val : V.second)
94 OS << format("0x%x ", Val);
95 }
96 OS << '\n';
97}
98
100 if (HasError) {
101 OS << "\n<error parsing>\n";
102 return;
103 }
104
105 if (HasContent) {
106 OS << " Version = " << Version << '\n';
107 dumpCUList(OS);
108 dumpTUList(OS);
109 dumpAddressArea(OS);
110 dumpSymbolTable(OS);
111 dumpConstantPool(OS);
112 }
113}
114
115bool DWARFGdbIndex::parseImpl(DataExtractor Data) {
116 uint64_t Offset = 0;
117
118 // Only version 7 and 8 are supported at this moment.
119 Version = Data.getU32(&Offset);
120 if (Version != 7 && Version != 8)
121 return false;
122
123 CuListOffset = Data.getU32(&Offset);
124 TuListOffset = Data.getU32(&Offset);
125 AddressAreaOffset = Data.getU32(&Offset);
126 SymbolTableOffset = Data.getU32(&Offset);
127 ConstantPoolOffset = Data.getU32(&Offset);
128
129 if (Offset != CuListOffset)
130 return false;
131
132 uint32_t CuListSize = (TuListOffset - CuListOffset) / 16;
133 CuList.reserve(CuListSize);
134 for (uint32_t i = 0; i < CuListSize; ++i) {
135 uint64_t CuOffset = Data.getU64(&Offset);
136 uint64_t CuLength = Data.getU64(&Offset);
137 CuList.push_back({CuOffset, CuLength});
138 }
139
140 // CU Types are no longer needed as DWARF skeleton type units never made it
141 // into the standard.
142 uint32_t TuListSize = (AddressAreaOffset - TuListOffset) / 24;
143 TuList.resize(TuListSize);
144 for (uint32_t I = 0; I < TuListSize; ++I) {
145 uint64_t CuOffset = Data.getU64(&Offset);
146 uint64_t TypeOffset = Data.getU64(&Offset);
147 uint64_t Signature = Data.getU64(&Offset);
148 TuList[I] = {CuOffset, TypeOffset, Signature};
149 }
150
151 uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;
152 AddressArea.reserve(AddressAreaSize);
153 for (uint32_t i = 0; i < AddressAreaSize; ++i) {
154 uint64_t LowAddress = Data.getU64(&Offset);
155 uint64_t HighAddress = Data.getU64(&Offset);
156 uint32_t CuIndex = Data.getU32(&Offset);
157 AddressArea.push_back({LowAddress, HighAddress, CuIndex});
158 }
159
160 // The symbol table. This is an open addressed hash table. The size of the
161 // hash table is always a power of 2.
162 // Each slot in the hash table consists of a pair of offset_type values. The
163 // first value is the offset of the symbol's name in the constant pool. The
164 // second value is the offset of the CU vector in the constant pool.
165 // If both values are 0, then this slot in the hash table is empty. This is ok
166 // because while 0 is a valid constant pool index, it cannot be a valid index
167 // for both a string and a CU vector.
168 uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8;
169 SymbolTable.reserve(SymTableSize);
170 std::set<uint32_t> CUOffsets;
171 for (uint32_t i = 0; i < SymTableSize; ++i) {
172 uint32_t NameOffset = Data.getU32(&Offset);
173 uint32_t CuVecOffset = Data.getU32(&Offset);
174 SymbolTable.push_back({NameOffset, CuVecOffset});
175 if (NameOffset || CuVecOffset)
176 CUOffsets.insert(CuVecOffset);
177 }
178
179 // The constant pool. CU vectors are stored first, followed by strings.
180 // The first value is the number of CU indices in the vector. Each subsequent
181 // value is the index and symbol attributes of a CU in the CU list.
182 for (auto CUOffset : CUOffsets) {
183 Offset = ConstantPoolOffset + CUOffset;
184 ConstantPoolVectors.emplace_back(0, SmallVector<uint32_t, 0>());
185 auto &Vec = ConstantPoolVectors.back();
186 Vec.first = Offset - ConstantPoolOffset;
187
188 uint32_t Num = Data.getU32(&Offset);
189 for (uint32_t J = 0; J < Num; ++J)
190 Vec.second.push_back(Data.getU32(&Offset));
191 }
192
193 ConstantPoolStrings = Data.getData().drop_front(Offset);
194 StringPoolOffset = Offset;
195 return true;
196}
197
199 HasContent = !Data.getData().empty();
200 HasError = HasContent && !parseImpl(Data);
201}
uint64_t Addr
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallVector class.
void dump(raw_ostream &OS)
void parse(DataExtractor Data)
size_t size() const
Definition: SmallVector.h:91
void reserve(size_type N)
Definition: SmallVector.h:676
void resize(size_type N)
Definition: SmallVector.h:651
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:567
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1758