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