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