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#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(Fmt: "\n CU list offset = 0x%x, has %" PRId64 " entries:",
29 Vals: CuListOffset, Vals: (uint64_t)CuList.size())
30 << '\n';
31 uint32_t I = 0;
32 for (const CompUnitEntry &CU : CuList)
33 OS << format(Fmt: " %d: Offset = 0x%llx, Length = 0x%llx\n", Vals: I++, Vals: CU.Offset,
34 Vals: CU.Length);
35}
36
37void DWARFGdbIndex::dumpTUList(raw_ostream &OS) const {
38 OS << formatv(Fmt: "\n Types CU list offset = {0:x}, has {1} entries:\n",
39 Vals: TuListOffset, Vals: TuList.size());
40 uint32_t I = 0;
41 for (const TypeUnitEntry &TU : TuList)
42 OS << formatv(Fmt: " {0}: offset = {1:x8}, type_offset = {2:x8}, "
43 "type_signature = {3:x16}\n",
44 Vals: I++, Vals: TU.Offset, Vals: TU.TypeOffset, Vals: TU.TypeSignature);
45}
46
47void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const {
48 OS << format(Fmt: "\n Address area offset = 0x%x, has %" PRId64 " entries:",
49 Vals: AddressAreaOffset, Vals: (uint64_t)AddressArea.size())
50 << '\n';
51 for (const AddressEntry &Addr : AddressArea)
52 OS << format(
53 Fmt: " Low/High address = [0x%llx, 0x%llx) (Size: 0x%llx), CU id = %d\n",
54 Vals: Addr.LowAddress, Vals: Addr.HighAddress, Vals: Addr.HighAddress - Addr.LowAddress,
55 Vals: Addr.CuIndex);
56}
57
58void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const {
59 OS << format(Fmt: "\n Symbol table offset = 0x%x, size = %" PRId64
60 ", filled slots:",
61 Vals: SymbolTableOffset, Vals: (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(Fmt: " %d: Name offset = 0x%x, CU vector offset = 0x%x\n", Vals: I,
70 Vals: E.NameOffset, Vals: E.VecOffset);
71
72 StringRef Name = ConstantPoolStrings.substr(
73 Start: ConstantPoolOffset - StringPoolOffset + E.NameOffset);
74
75 auto CuVector = llvm::find_if(
76 Range: ConstantPoolVectors,
77 P: [&](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(Fmt: " String name: %s, CU vector index: %d\n", Vals: Name.data(),
83 Vals: CuVectorId);
84 }
85}
86
87void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {
88 OS << format(Fmt: "\n Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",
89 Vals: ConstantPoolOffset, Vals: (uint64_t)ConstantPoolVectors.size());
90 uint32_t I = 0;
91 for (const auto &V : ConstantPoolVectors) {
92 OS << format(Fmt: "\n %d(0x%x): ", Vals: I++, Vals: V.first);
93 for (uint32_t Val : V.second)
94 OS << format(Fmt: "0x%x ", Vals: Val);
95 }
96 OS << '\n';
97}
98
99void DWARFGdbIndex::dump(raw_ostream &OS) {
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_ptr: &Offset);
120 if (Version != 7 && Version != 8)
121 return false;
122
123 CuListOffset = Data.getU32(offset_ptr: &Offset);
124 TuListOffset = Data.getU32(offset_ptr: &Offset);
125 AddressAreaOffset = Data.getU32(offset_ptr: &Offset);
126 SymbolTableOffset = Data.getU32(offset_ptr: &Offset);
127 ConstantPoolOffset = Data.getU32(offset_ptr: &Offset);
128
129 if (Offset != CuListOffset)
130 return false;
131
132 uint32_t CuListSize = (TuListOffset - CuListOffset) / 16;
133 CuList.reserve(N: CuListSize);
134 for (uint32_t i = 0; i < CuListSize; ++i) {
135 uint64_t CuOffset = Data.getU64(offset_ptr: &Offset);
136 uint64_t CuLength = Data.getU64(offset_ptr: &Offset);
137 CuList.push_back(Elt: {.Offset: CuOffset, .Length: 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(N: TuListSize);
144 for (uint32_t I = 0; I < TuListSize; ++I) {
145 uint64_t CuOffset = Data.getU64(offset_ptr: &Offset);
146 uint64_t TypeOffset = Data.getU64(offset_ptr: &Offset);
147 uint64_t Signature = Data.getU64(offset_ptr: &Offset);
148 TuList[I] = {.Offset: CuOffset, .TypeOffset: TypeOffset, .TypeSignature: Signature};
149 }
150
151 uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;
152 AddressArea.reserve(N: AddressAreaSize);
153 for (uint32_t i = 0; i < AddressAreaSize; ++i) {
154 uint64_t LowAddress = Data.getU64(offset_ptr: &Offset);
155 uint64_t HighAddress = Data.getU64(offset_ptr: &Offset);
156 uint32_t CuIndex = Data.getU32(offset_ptr: &Offset);
157 AddressArea.push_back(Elt: {.LowAddress: LowAddress, .HighAddress: HighAddress, .CuIndex: 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(N: SymTableSize);
170 std::set<uint32_t> CUOffsets;
171 for (uint32_t i = 0; i < SymTableSize; ++i) {
172 uint32_t NameOffset = Data.getU32(offset_ptr: &Offset);
173 uint32_t CuVecOffset = Data.getU32(offset_ptr: &Offset);
174 SymbolTable.push_back(Elt: {.NameOffset: NameOffset, .VecOffset: CuVecOffset});
175 if (NameOffset || CuVecOffset)
176 CUOffsets.insert(x: 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(Args: 0, Args: SmallVector<uint32_t, 0>());
185 auto &Vec = ConstantPoolVectors.back();
186 Vec.first = Offset - ConstantPoolOffset;
187
188 uint32_t Num = Data.getU32(offset_ptr: &Offset);
189 for (uint32_t J = 0; J < Num; ++J)
190 Vec.second.push_back(Elt: Data.getU32(offset_ptr: &Offset));
191 }
192
193 ConstantPoolStrings = Data.getData().drop_front(N: Offset);
194 StringPoolOffset = Offset;
195 return true;
196}
197
198void DWARFGdbIndex::parse(DataExtractor Data) {
199 HasContent = !Data.getData().empty();
200 HasError = HasContent && !parseImpl(Data);
201}
202