LLVM 22.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
64 const auto FindCuVectorId = [&](uint32_t VecOffset) {
65 // Entries in ConstantPoolVectors are sorted by their offset in constant
66 // pool, see how ConstantPoolVectors is populated in parseImpl.
67 const auto *It =
68 llvm::lower_bound(ConstantPoolVectors, VecOffset,
69 [](const auto &ConstantPoolEntry, uint32_t Offset) {
70 return ConstantPoolEntry.first < Offset;
71 });
72 assert(It != ConstantPoolVectors.end() && It->first == VecOffset &&
73 "Invalid symbol table");
74 return It - ConstantPoolVectors.begin();
75 };
76
77 uint32_t I = -1;
78 for (const SymTableEntry &E : SymbolTable) {
79 ++I;
80 if (!E.NameOffset && !E.VecOffset)
81 continue;
82
83 OS << format(" %d: Name offset = 0x%x, CU vector offset = 0x%x\n", I,
84 E.NameOffset, E.VecOffset);
85
86 StringRef Name = ConstantPoolStrings.substr(
87 ConstantPoolOffset - StringPoolOffset + E.NameOffset);
88
89 const uint32_t CuVectorId = FindCuVectorId(E.VecOffset);
90 OS << format(" String name: %s, CU vector index: %d\n", Name.data(),
91 CuVectorId);
92 }
93}
94
95void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {
96 OS << format("\n Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",
97 ConstantPoolOffset, (uint64_t)ConstantPoolVectors.size());
98 uint32_t I = 0;
99 for (const auto &V : ConstantPoolVectors) {
100 OS << format("\n %d(0x%x): ", I++, V.first);
101 for (uint32_t Val : V.second)
102 OS << format("0x%x ", Val);
103 }
104 OS << '\n';
105}
106
108 if (HasError) {
109 OS << "\n<error parsing>\n";
110 return;
111 }
112
113 if (HasContent) {
114 OS << " Version = " << Version << '\n';
115 dumpCUList(OS);
116 dumpTUList(OS);
117 dumpAddressArea(OS);
118 dumpSymbolTable(OS);
119 dumpConstantPool(OS);
120 }
121}
122
123bool DWARFGdbIndex::parseImpl(DataExtractor Data) {
124 uint64_t Offset = 0;
125
126 // Only version 7 and 8 are supported at this moment.
127 Version = Data.getU32(&Offset);
128 if (Version != 7 && Version != 8)
129 return false;
130
131 CuListOffset = Data.getU32(&Offset);
132 TuListOffset = Data.getU32(&Offset);
133 AddressAreaOffset = Data.getU32(&Offset);
134 SymbolTableOffset = Data.getU32(&Offset);
135 ConstantPoolOffset = Data.getU32(&Offset);
136
137 if (Offset != CuListOffset)
138 return false;
139
140 uint32_t CuListSize = (TuListOffset - CuListOffset) / 16;
141 CuList.reserve(CuListSize);
142 for (uint32_t i = 0; i < CuListSize; ++i) {
143 uint64_t CuOffset = Data.getU64(&Offset);
144 uint64_t CuLength = Data.getU64(&Offset);
145 CuList.push_back({CuOffset, CuLength});
146 }
147
148 // CU Types are no longer needed as DWARF skeleton type units never made it
149 // into the standard.
150 uint32_t TuListSize = (AddressAreaOffset - TuListOffset) / 24;
151 TuList.resize(TuListSize);
152 for (uint32_t I = 0; I < TuListSize; ++I) {
153 uint64_t CuOffset = Data.getU64(&Offset);
154 uint64_t TypeOffset = Data.getU64(&Offset);
155 uint64_t Signature = Data.getU64(&Offset);
156 TuList[I] = {CuOffset, TypeOffset, Signature};
157 }
158
159 uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;
160 AddressArea.reserve(AddressAreaSize);
161 for (uint32_t i = 0; i < AddressAreaSize; ++i) {
162 uint64_t LowAddress = Data.getU64(&Offset);
163 uint64_t HighAddress = Data.getU64(&Offset);
164 uint32_t CuIndex = Data.getU32(&Offset);
165 AddressArea.push_back({LowAddress, HighAddress, CuIndex});
166 }
167
168 // The symbol table. This is an open addressed hash table. The size of the
169 // hash table is always a power of 2.
170 // Each slot in the hash table consists of a pair of offset_type values. The
171 // first value is the offset of the symbol's name in the constant pool. The
172 // second value is the offset of the CU vector in the constant pool.
173 // If both values are 0, then this slot in the hash table is empty. This is ok
174 // because while 0 is a valid constant pool index, it cannot be a valid index
175 // for both a string and a CU vector.
176 uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8;
177 SymbolTable.reserve(SymTableSize);
178 std::set<uint32_t> CUOffsets;
179 for (uint32_t i = 0; i < SymTableSize; ++i) {
180 uint32_t NameOffset = Data.getU32(&Offset);
181 uint32_t CuVecOffset = Data.getU32(&Offset);
182 SymbolTable.push_back({NameOffset, CuVecOffset});
183 if (NameOffset || CuVecOffset)
184 CUOffsets.insert(CuVecOffset);
185 }
186
187 // The constant pool. CU vectors are stored first, followed by strings.
188 // The first value is the number of CU indices in the vector. Each subsequent
189 // value is the index and symbol attributes of a CU in the CU list.
190 for (auto CUOffset : CUOffsets) {
191 Offset = ConstantPoolOffset + CUOffset;
192 ConstantPoolVectors.emplace_back(0, SmallVector<uint32_t, 0>());
193 auto &Vec = ConstantPoolVectors.back();
194 Vec.first = Offset - ConstantPoolOffset;
195
196 uint32_t Num = Data.getU32(&Offset);
197 for (uint32_t J = 0; J < Num; ++J)
198 Vec.second.push_back(Data.getU32(&Offset));
199 }
200
201 ConstantPoolStrings = Data.getData().drop_front(Offset);
202 StringPoolOffset = Offset;
203 return true;
204}
205
207 HasContent = !Data.getData().empty();
208 HasError = HasContent && !parseImpl(Data);
209}
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:58
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:477
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:126
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
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:1976