Line data Source code
1 : //===- DWARFGdbIndex.cpp --------------------------------------------------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 :
10 : #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
11 : #include "llvm/ADT/SmallVector.h"
12 : #include "llvm/ADT/StringRef.h"
13 : #include "llvm/Support/Format.h"
14 : #include "llvm/Support/raw_ostream.h"
15 : #include <algorithm>
16 : #include <cassert>
17 : #include <cinttypes>
18 : #include <cstdint>
19 : #include <utility>
20 :
21 : using namespace llvm;
22 :
23 : // .gdb_index section format reference:
24 : // https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html
25 :
26 7 : void DWARFGdbIndex::dumpCUList(raw_ostream &OS) const {
27 14 : OS << format("\n CU list offset = 0x%x, has %" PRId64 " entries:",
28 14 : CuListOffset, (uint64_t)CuList.size())
29 : << '\n';
30 : uint32_t I = 0;
31 17 : for (const CompUnitEntry &CU : CuList)
32 10 : OS << format(" %d: Offset = 0x%llx, Length = 0x%llx\n", I++, CU.Offset,
33 10 : CU.Length);
34 7 : }
35 :
36 7 : void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const {
37 14 : OS << format("\n Address area offset = 0x%x, has %" PRId64 " entries:",
38 14 : AddressAreaOffset, (uint64_t)AddressArea.size())
39 : << '\n';
40 18 : for (const AddressEntry &Addr : AddressArea)
41 11 : OS << format(
42 : " Low/High address = [0x%llx, 0x%llx) (Size: 0x%llx), CU id = %d\n",
43 11 : Addr.LowAddress, Addr.HighAddress, Addr.HighAddress - Addr.LowAddress,
44 11 : Addr.CuIndex);
45 7 : }
46 :
47 7 : void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const {
48 14 : OS << format("\n Symbol table offset = 0x%x, size = %" PRId64
49 : ", filled slots:",
50 14 : SymbolTableOffset, (uint64_t)SymbolTable.size())
51 : << '\n';
52 : uint32_t I = -1;
53 7175 : for (const SymTableEntry &E : SymbolTable) {
54 7168 : ++I;
55 7168 : if (!E.NameOffset && !E.VecOffset)
56 : continue;
57 :
58 9 : OS << format(" %d: Name offset = 0x%x, CU vector offset = 0x%x\n", I,
59 9 : E.NameOffset, E.VecOffset);
60 :
61 : StringRef Name = ConstantPoolStrings.substr(
62 9 : ConstantPoolOffset - StringPoolOffset + E.NameOffset);
63 :
64 : auto CuVector = std::find_if(
65 : ConstantPoolVectors.begin(), ConstantPoolVectors.end(),
66 : [&](const std::pair<uint32_t, SmallVector<uint32_t, 0>> &V) {
67 0 : return V.first == E.VecOffset;
68 : });
69 : assert(CuVector != ConstantPoolVectors.end() && "Invalid symbol table");
70 9 : uint32_t CuVectorId = CuVector - ConstantPoolVectors.begin();
71 9 : OS << format(" String name: %s, CU vector index: %d\n", Name.data(),
72 9 : CuVectorId);
73 : }
74 7 : }
75 :
76 7 : void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {
77 7 : OS << format("\n Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",
78 14 : ConstantPoolOffset, (uint64_t)ConstantPoolVectors.size());
79 : uint32_t I = 0;
80 16 : for (const auto &V : ConstantPoolVectors) {
81 18 : OS << format("\n %d(0x%x): ", I++, V.first);
82 21 : for (uint32_t Val : V.second)
83 12 : OS << format("0x%x ", Val);
84 : }
85 : OS << '\n';
86 7 : }
87 :
88 7 : void DWARFGdbIndex::dump(raw_ostream &OS) {
89 7 : if (HasError) {
90 0 : OS << "\n<error parsing>\n";
91 0 : return;
92 : }
93 :
94 7 : if (HasContent) {
95 7 : OS << " Version = " << Version << '\n';
96 7 : dumpCUList(OS);
97 7 : dumpAddressArea(OS);
98 7 : dumpSymbolTable(OS);
99 7 : dumpConstantPool(OS);
100 : }
101 : }
102 :
103 7 : bool DWARFGdbIndex::parseImpl(DataExtractor Data) {
104 7 : uint32_t Offset = 0;
105 :
106 : // Only version 7 is supported at this moment.
107 7 : Version = Data.getU32(&Offset);
108 7 : if (Version != 7)
109 : return false;
110 :
111 7 : CuListOffset = Data.getU32(&Offset);
112 7 : uint32_t CuTypesOffset = Data.getU32(&Offset);
113 7 : AddressAreaOffset = Data.getU32(&Offset);
114 7 : SymbolTableOffset = Data.getU32(&Offset);
115 7 : ConstantPoolOffset = Data.getU32(&Offset);
116 :
117 7 : if (Offset != CuListOffset)
118 : return false;
119 :
120 7 : uint32_t CuListSize = (CuTypesOffset - CuListOffset) / 16;
121 7 : CuList.reserve(CuListSize);
122 17 : for (uint32_t i = 0; i < CuListSize; ++i) {
123 10 : uint64_t CuOffset = Data.getU64(&Offset);
124 10 : uint64_t CuLength = Data.getU64(&Offset);
125 10 : CuList.push_back({CuOffset, CuLength});
126 : }
127 :
128 : // CU Types are no longer needed as DWARF skeleton type units never made it
129 : // into the standard.
130 7 : uint32_t CuTypesListSize = (AddressAreaOffset - CuTypesOffset) / 24;
131 7 : if (CuTypesListSize != 0)
132 : return false;
133 :
134 7 : uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;
135 7 : AddressArea.reserve(AddressAreaSize);
136 18 : for (uint32_t i = 0; i < AddressAreaSize; ++i) {
137 11 : uint64_t LowAddress = Data.getU64(&Offset);
138 11 : uint64_t HighAddress = Data.getU64(&Offset);
139 11 : uint32_t CuIndex = Data.getU32(&Offset);
140 11 : AddressArea.push_back({LowAddress, HighAddress, CuIndex});
141 : }
142 :
143 : // The symbol table. This is an open addressed hash table. The size of the
144 : // hash table is always a power of 2.
145 : // Each slot in the hash table consists of a pair of offset_type values. The
146 : // first value is the offset of the symbol's name in the constant pool. The
147 : // second value is the offset of the CU vector in the constant pool.
148 : // If both values are 0, then this slot in the hash table is empty. This is ok
149 : // because while 0 is a valid constant pool index, it cannot be a valid index
150 : // for both a string and a CU vector.
151 7 : uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8;
152 7 : SymbolTable.reserve(SymTableSize);
153 : uint32_t CuVectorsTotal = 0;
154 7175 : for (uint32_t i = 0; i < SymTableSize; ++i) {
155 7168 : uint32_t NameOffset = Data.getU32(&Offset);
156 7168 : uint32_t CuVecOffset = Data.getU32(&Offset);
157 7168 : SymbolTable.push_back({NameOffset, CuVecOffset});
158 7168 : if (NameOffset || CuVecOffset)
159 9 : ++CuVectorsTotal;
160 : }
161 :
162 : // The constant pool. CU vectors are stored first, followed by strings.
163 : // The first value is the number of CU indices in the vector. Each subsequent
164 : // value is the index and symbol attributes of a CU in the CU list.
165 16 : for (uint32_t i = 0; i < CuVectorsTotal; ++i) {
166 18 : ConstantPoolVectors.emplace_back(0, SmallVector<uint32_t, 0>());
167 : auto &Vec = ConstantPoolVectors.back();
168 9 : Vec.first = Offset - ConstantPoolOffset;
169 :
170 9 : uint32_t Num = Data.getU32(&Offset);
171 21 : for (uint32_t j = 0; j < Num; ++j)
172 12 : Vec.second.push_back(Data.getU32(&Offset));
173 : }
174 :
175 7 : ConstantPoolStrings = Data.getData().drop_front(Offset);
176 7 : StringPoolOffset = Offset;
177 7 : return true;
178 : }
179 :
180 7 : void DWARFGdbIndex::parse(DataExtractor Data) {
181 7 : HasContent = !Data.getData().empty();
182 7 : HasError = HasContent && !parseImpl(Data);
183 7 : }
|