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