blob: ebd6104ab8785604365ca0a60500802dc6888828 [file] [log] [blame]
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001//===- DWARFGdbIndex.cpp --------------------------------------------------===//
George Rimar4f82df52016-09-23 11:01:53 +00002//
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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000011#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/StringRef.h"
George Rimar4f82df52016-09-23 11:01:53 +000013#include "llvm/Support/Format.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000014#include "llvm/Support/raw_ostream.h"
15#include <algorithm>
16#include <cassert>
17#include <cinttypes>
18#include <cstdint>
19#include <utility>
George Rimar4f82df52016-09-23 11:01:53 +000020
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("\n CU list offset = 0x%x, has %" PRId64 " entries:",
28 CuListOffset, (uint64_t)CuList.size())
29 << '\n';
30 uint32_t I = 0;
31 for (const CompUnitEntry &CU : CuList)
32 OS << format(" %d: Offset = 0x%llx, Length = 0x%llx\n", I++, CU.Offset,
33 CU.Length);
34}
35
36void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const {
37 OS << format("\n Address area offset = 0x%x, has %" PRId64 " entries:",
38 AddressAreaOffset, (uint64_t)AddressArea.size())
39 << '\n';
40 for (const AddressEntry &Addr : AddressArea)
41 OS << format(
George Rimare6ef4482017-04-27 10:00:13 +000042 " Low/High address = [0x%llx, 0x%llx) (Size: 0x%llx), CU id = %d\n",
43 Addr.LowAddress, Addr.HighAddress, Addr.HighAddress - Addr.LowAddress,
44 Addr.CuIndex);
George Rimar4f82df52016-09-23 11:01:53 +000045}
46
47void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const {
48 OS << format("\n Symbol table offset = 0x%x, size = %" PRId64
49 ", filled slots:",
50 SymbolTableOffset, (uint64_t)SymbolTable.size())
51 << '\n';
52 uint32_t I = -1;
53 for (const SymTableEntry &E : SymbolTable) {
54 ++I;
55 if (!E.NameOffset && !E.VecOffset)
56 continue;
57
58 OS << format(" %d: Name offset = 0x%x, CU vector offset = 0x%x\n", I,
59 E.NameOffset, E.VecOffset);
60
61 StringRef Name = ConstantPoolStrings.substr(
62 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 return V.first == E.VecOffset;
68 });
69 assert(CuVector != ConstantPoolVectors.end() && "Invalid symbol table");
70 uint32_t CuVectorId = CuVector - ConstantPoolVectors.begin();
71 OS << format(" String name: %s, CU vector index: %d\n", Name.data(),
72 CuVectorId);
73 }
74}
75
76void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {
77 OS << format("\n Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",
78 ConstantPoolOffset, (uint64_t)ConstantPoolVectors.size());
79 uint32_t I = 0;
80 for (const auto &V : ConstantPoolVectors) {
81 OS << format("\n %d(0x%x): ", I++, V.first);
82 for (uint32_t Val : V.second)
83 OS << format("0x%x ", Val);
84 }
85 OS << '\n';
86}
87
88void DWARFGdbIndex::dump(raw_ostream &OS) {
89 if (HasError) {
90 OS << "\n<error parsing>\n";
91 return;
92 }
93
94 if (HasContent) {
95 OS << " Version = " << Version << '\n';
96 dumpCUList(OS);
97 dumpAddressArea(OS);
98 dumpSymbolTable(OS);
99 dumpConstantPool(OS);
100 }
101}
102
103bool DWARFGdbIndex::parseImpl(DataExtractor Data) {
104 uint32_t Offset = 0;
105
106 // Only version 7 is supported at this moment.
107 Version = Data.getU32(&Offset);
108 if (Version != 7)
109 return false;
110
111 CuListOffset = Data.getU32(&Offset);
112 uint32_t CuTypesOffset = Data.getU32(&Offset);
113 AddressAreaOffset = Data.getU32(&Offset);
114 SymbolTableOffset = Data.getU32(&Offset);
115 ConstantPoolOffset = Data.getU32(&Offset);
116
117 if (Offset != CuListOffset)
118 return false;
119
120 uint32_t CuListSize = (CuTypesOffset - CuListOffset) / 16;
121 CuList.reserve(CuListSize);
122 for (uint32_t i = 0; i < CuListSize; ++i) {
123 uint64_t CuOffset = Data.getU64(&Offset);
124 uint64_t CuLength = Data.getU64(&Offset);
125 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 uint32_t CuTypesListSize = (AddressAreaOffset - CuTypesOffset) / 24;
131 if (CuTypesListSize != 0)
132 return false;
133
134 uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;
135 AddressArea.reserve(AddressAreaSize);
136 for (uint32_t i = 0; i < AddressAreaSize; ++i) {
137 uint64_t LowAddress = Data.getU64(&Offset);
138 uint64_t HighAddress = Data.getU64(&Offset);
139 uint32_t CuIndex = Data.getU32(&Offset);
140 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 uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8;
152 SymbolTable.reserve(SymTableSize);
153 uint32_t CuVectorsTotal = 0;
154 for (uint32_t i = 0; i < SymTableSize; ++i) {
155 uint32_t NameOffset = Data.getU32(&Offset);
156 uint32_t CuVecOffset = Data.getU32(&Offset);
157 SymbolTable.push_back({NameOffset, CuVecOffset});
158 if (NameOffset || CuVecOffset)
159 ++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 for (uint32_t i = 0; i < CuVectorsTotal; ++i) {
166 ConstantPoolVectors.emplace_back(0, SmallVector<uint32_t, 0>());
167 auto &Vec = ConstantPoolVectors.back();
168 Vec.first = Offset - ConstantPoolOffset;
169
170 uint32_t Num = Data.getU32(&Offset);
171 for (uint32_t j = 0; j < Num; ++j)
172 Vec.second.push_back(Data.getU32(&Offset));
173 }
174
175 ConstantPoolStrings = Data.getData().drop_front(Offset);
176 StringPoolOffset = Offset;
177 return true;
178}
179
180void DWARFGdbIndex::parse(DataExtractor Data) {
181 HasContent = !Data.getData().empty();
182 HasError = HasContent && !parseImpl(Data);
183}