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