10b57cec5SDimitry Andric //===- DWARFGdbIndex.cpp --------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
100b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
110b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
1281ad6265SDimitry Andric #include "llvm/Support/DataExtractor.h"
130b57cec5SDimitry Andric #include "llvm/Support/Format.h"
140b57cec5SDimitry Andric #include "llvm/Support/FormatVariadic.h"
150b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
160b57cec5SDimitry Andric #include <cassert>
170b57cec5SDimitry Andric #include <cinttypes>
180b57cec5SDimitry Andric #include <cstdint>
19*06c3fb27SDimitry Andric #include <set>
200b57cec5SDimitry Andric #include <utility>
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric // .gdb_index section format reference:
250b57cec5SDimitry Andric // https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html
260b57cec5SDimitry Andric
dumpCUList(raw_ostream & OS) const270b57cec5SDimitry Andric void DWARFGdbIndex::dumpCUList(raw_ostream &OS) const {
280b57cec5SDimitry Andric OS << format("\n CU list offset = 0x%x, has %" PRId64 " entries:",
290b57cec5SDimitry Andric CuListOffset, (uint64_t)CuList.size())
300b57cec5SDimitry Andric << '\n';
310b57cec5SDimitry Andric uint32_t I = 0;
320b57cec5SDimitry Andric for (const CompUnitEntry &CU : CuList)
330b57cec5SDimitry Andric OS << format(" %d: Offset = 0x%llx, Length = 0x%llx\n", I++, CU.Offset,
340b57cec5SDimitry Andric CU.Length);
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric
dumpTUList(raw_ostream & OS) const370b57cec5SDimitry Andric void DWARFGdbIndex::dumpTUList(raw_ostream &OS) const {
380b57cec5SDimitry Andric OS << formatv("\n Types CU list offset = {0:x}, has {1} entries:\n",
390b57cec5SDimitry Andric TuListOffset, TuList.size());
400b57cec5SDimitry Andric uint32_t I = 0;
410b57cec5SDimitry Andric for (const TypeUnitEntry &TU : TuList)
420b57cec5SDimitry Andric OS << formatv(" {0}: offset = {1:x8}, type_offset = {2:x8}, "
430b57cec5SDimitry Andric "type_signature = {3:x16}\n",
440b57cec5SDimitry Andric I++, TU.Offset, TU.TypeOffset, TU.TypeSignature);
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric
dumpAddressArea(raw_ostream & OS) const470b57cec5SDimitry Andric void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const {
480b57cec5SDimitry Andric OS << format("\n Address area offset = 0x%x, has %" PRId64 " entries:",
490b57cec5SDimitry Andric AddressAreaOffset, (uint64_t)AddressArea.size())
500b57cec5SDimitry Andric << '\n';
510b57cec5SDimitry Andric for (const AddressEntry &Addr : AddressArea)
520b57cec5SDimitry Andric OS << format(
530b57cec5SDimitry Andric " Low/High address = [0x%llx, 0x%llx) (Size: 0x%llx), CU id = %d\n",
540b57cec5SDimitry Andric Addr.LowAddress, Addr.HighAddress, Addr.HighAddress - Addr.LowAddress,
550b57cec5SDimitry Andric Addr.CuIndex);
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric
dumpSymbolTable(raw_ostream & OS) const580b57cec5SDimitry Andric void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const {
590b57cec5SDimitry Andric OS << format("\n Symbol table offset = 0x%x, size = %" PRId64
600b57cec5SDimitry Andric ", filled slots:",
610b57cec5SDimitry Andric SymbolTableOffset, (uint64_t)SymbolTable.size())
620b57cec5SDimitry Andric << '\n';
630b57cec5SDimitry Andric uint32_t I = -1;
640b57cec5SDimitry Andric for (const SymTableEntry &E : SymbolTable) {
650b57cec5SDimitry Andric ++I;
660b57cec5SDimitry Andric if (!E.NameOffset && !E.VecOffset)
670b57cec5SDimitry Andric continue;
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric OS << format(" %d: Name offset = 0x%x, CU vector offset = 0x%x\n", I,
700b57cec5SDimitry Andric E.NameOffset, E.VecOffset);
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric StringRef Name = ConstantPoolStrings.substr(
730b57cec5SDimitry Andric ConstantPoolOffset - StringPoolOffset + E.NameOffset);
740b57cec5SDimitry Andric
75e8d8bef9SDimitry Andric auto CuVector = llvm::find_if(
76e8d8bef9SDimitry Andric ConstantPoolVectors,
770b57cec5SDimitry Andric [&](const std::pair<uint32_t, SmallVector<uint32_t, 0>> &V) {
780b57cec5SDimitry Andric return V.first == E.VecOffset;
790b57cec5SDimitry Andric });
800b57cec5SDimitry Andric assert(CuVector != ConstantPoolVectors.end() && "Invalid symbol table");
810b57cec5SDimitry Andric uint32_t CuVectorId = CuVector - ConstantPoolVectors.begin();
820b57cec5SDimitry Andric OS << format(" String name: %s, CU vector index: %d\n", Name.data(),
830b57cec5SDimitry Andric CuVectorId);
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric
dumpConstantPool(raw_ostream & OS) const870b57cec5SDimitry Andric void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {
880b57cec5SDimitry Andric OS << format("\n Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",
890b57cec5SDimitry Andric ConstantPoolOffset, (uint64_t)ConstantPoolVectors.size());
900b57cec5SDimitry Andric uint32_t I = 0;
910b57cec5SDimitry Andric for (const auto &V : ConstantPoolVectors) {
920b57cec5SDimitry Andric OS << format("\n %d(0x%x): ", I++, V.first);
930b57cec5SDimitry Andric for (uint32_t Val : V.second)
940b57cec5SDimitry Andric OS << format("0x%x ", Val);
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric OS << '\n';
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
dump(raw_ostream & OS)990b57cec5SDimitry Andric void DWARFGdbIndex::dump(raw_ostream &OS) {
1000b57cec5SDimitry Andric if (HasError) {
1010b57cec5SDimitry Andric OS << "\n<error parsing>\n";
1020b57cec5SDimitry Andric return;
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric if (HasContent) {
1060b57cec5SDimitry Andric OS << " Version = " << Version << '\n';
1070b57cec5SDimitry Andric dumpCUList(OS);
1080b57cec5SDimitry Andric dumpTUList(OS);
1090b57cec5SDimitry Andric dumpAddressArea(OS);
1100b57cec5SDimitry Andric dumpSymbolTable(OS);
1110b57cec5SDimitry Andric dumpConstantPool(OS);
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric
parseImpl(DataExtractor Data)1150b57cec5SDimitry Andric bool DWARFGdbIndex::parseImpl(DataExtractor Data) {
1168bcb0991SDimitry Andric uint64_t Offset = 0;
1170b57cec5SDimitry Andric
118*06c3fb27SDimitry Andric // Only version 7 and 8 are supported at this moment.
1190b57cec5SDimitry Andric Version = Data.getU32(&Offset);
120*06c3fb27SDimitry Andric if (Version != 7 && Version != 8)
1210b57cec5SDimitry Andric return false;
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric CuListOffset = Data.getU32(&Offset);
1240b57cec5SDimitry Andric TuListOffset = Data.getU32(&Offset);
1250b57cec5SDimitry Andric AddressAreaOffset = Data.getU32(&Offset);
1260b57cec5SDimitry Andric SymbolTableOffset = Data.getU32(&Offset);
1270b57cec5SDimitry Andric ConstantPoolOffset = Data.getU32(&Offset);
1280b57cec5SDimitry Andric
1290b57cec5SDimitry Andric if (Offset != CuListOffset)
1300b57cec5SDimitry Andric return false;
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric uint32_t CuListSize = (TuListOffset - CuListOffset) / 16;
1330b57cec5SDimitry Andric CuList.reserve(CuListSize);
1340b57cec5SDimitry Andric for (uint32_t i = 0; i < CuListSize; ++i) {
1350b57cec5SDimitry Andric uint64_t CuOffset = Data.getU64(&Offset);
1360b57cec5SDimitry Andric uint64_t CuLength = Data.getU64(&Offset);
1370b57cec5SDimitry Andric CuList.push_back({CuOffset, CuLength});
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric // CU Types are no longer needed as DWARF skeleton type units never made it
1410b57cec5SDimitry Andric // into the standard.
1420b57cec5SDimitry Andric uint32_t TuListSize = (AddressAreaOffset - TuListOffset) / 24;
1430b57cec5SDimitry Andric TuList.resize(TuListSize);
1440b57cec5SDimitry Andric for (uint32_t I = 0; I < TuListSize; ++I) {
1450b57cec5SDimitry Andric uint64_t CuOffset = Data.getU64(&Offset);
1460b57cec5SDimitry Andric uint64_t TypeOffset = Data.getU64(&Offset);
1470b57cec5SDimitry Andric uint64_t Signature = Data.getU64(&Offset);
1480b57cec5SDimitry Andric TuList[I] = {CuOffset, TypeOffset, Signature};
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;
1520b57cec5SDimitry Andric AddressArea.reserve(AddressAreaSize);
1530b57cec5SDimitry Andric for (uint32_t i = 0; i < AddressAreaSize; ++i) {
1540b57cec5SDimitry Andric uint64_t LowAddress = Data.getU64(&Offset);
1550b57cec5SDimitry Andric uint64_t HighAddress = Data.getU64(&Offset);
1560b57cec5SDimitry Andric uint32_t CuIndex = Data.getU32(&Offset);
1570b57cec5SDimitry Andric AddressArea.push_back({LowAddress, HighAddress, CuIndex});
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric
1600b57cec5SDimitry Andric // The symbol table. This is an open addressed hash table. The size of the
1610b57cec5SDimitry Andric // hash table is always a power of 2.
1620b57cec5SDimitry Andric // Each slot in the hash table consists of a pair of offset_type values. The
1630b57cec5SDimitry Andric // first value is the offset of the symbol's name in the constant pool. The
1640b57cec5SDimitry Andric // second value is the offset of the CU vector in the constant pool.
1650b57cec5SDimitry Andric // If both values are 0, then this slot in the hash table is empty. This is ok
1660b57cec5SDimitry Andric // because while 0 is a valid constant pool index, it cannot be a valid index
1670b57cec5SDimitry Andric // for both a string and a CU vector.
1680b57cec5SDimitry Andric uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8;
1690b57cec5SDimitry Andric SymbolTable.reserve(SymTableSize);
170*06c3fb27SDimitry Andric std::set<uint32_t> CUOffsets;
1710b57cec5SDimitry Andric for (uint32_t i = 0; i < SymTableSize; ++i) {
1720b57cec5SDimitry Andric uint32_t NameOffset = Data.getU32(&Offset);
1730b57cec5SDimitry Andric uint32_t CuVecOffset = Data.getU32(&Offset);
1740b57cec5SDimitry Andric SymbolTable.push_back({NameOffset, CuVecOffset});
1750b57cec5SDimitry Andric if (NameOffset || CuVecOffset)
176*06c3fb27SDimitry Andric CUOffsets.insert(CuVecOffset);
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric // The constant pool. CU vectors are stored first, followed by strings.
1800b57cec5SDimitry Andric // The first value is the number of CU indices in the vector. Each subsequent
1810b57cec5SDimitry Andric // value is the index and symbol attributes of a CU in the CU list.
182*06c3fb27SDimitry Andric for (auto CUOffset : CUOffsets) {
183*06c3fb27SDimitry Andric Offset = ConstantPoolOffset + CUOffset;
1840b57cec5SDimitry Andric ConstantPoolVectors.emplace_back(0, SmallVector<uint32_t, 0>());
1850b57cec5SDimitry Andric auto &Vec = ConstantPoolVectors.back();
1860b57cec5SDimitry Andric Vec.first = Offset - ConstantPoolOffset;
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric uint32_t Num = Data.getU32(&Offset);
189*06c3fb27SDimitry Andric for (uint32_t J = 0; J < Num; ++J)
1900b57cec5SDimitry Andric Vec.second.push_back(Data.getU32(&Offset));
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric
1930b57cec5SDimitry Andric ConstantPoolStrings = Data.getData().drop_front(Offset);
1940b57cec5SDimitry Andric StringPoolOffset = Offset;
1950b57cec5SDimitry Andric return true;
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric
parse(DataExtractor Data)1980b57cec5SDimitry Andric void DWARFGdbIndex::parse(DataExtractor Data) {
1990b57cec5SDimitry Andric HasContent = !Data.getData().empty();
2000b57cec5SDimitry Andric HasError = HasContent && !parseImpl(Data);
2010b57cec5SDimitry Andric }
202