Index: include/llvm/DebugInfo/DIContext.h =================================================================== --- include/llvm/DebugInfo/DIContext.h +++ include/llvm/DebugInfo/DIContext.h @@ -125,6 +125,7 @@ DIDT_AppleNamespaces, DIDT_AppleObjC, DIDT_CUIndex, + DIDT_GdbIndex, DIDT_TUIndex, }; Index: include/llvm/DebugInfo/DWARF/DWARFContext.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFContext.h +++ include/llvm/DebugInfo/DWARF/DWARFContext.h @@ -20,6 +20,7 @@ #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h" #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h" #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" +#include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h" #include "llvm/DebugInfo/DWARF/DWARFSection.h" #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h" @@ -41,6 +42,7 @@ DWARFUnitSection CUs; std::deque> TUs; std::unique_ptr CUIndex; + std::unique_ptr GdbIndex; std::unique_ptr TUIndex; std::unique_ptr Abbrev; std::unique_ptr Loc; @@ -149,6 +151,7 @@ } const DWARFUnitIndex &getCUIndex(); + const DWARFGdbIndex &getGdbIndex(); const DWARFUnitIndex &getTUIndex(); /// Get a pointer to the parsed DebugAbbrev object. @@ -220,6 +223,7 @@ virtual const DWARFSection& getAppleNamespacesSection() = 0; virtual const DWARFSection& getAppleObjCSection() = 0; virtual StringRef getCUIndexSection() = 0; + virtual StringRef getGdbIndexSection() = 0; virtual StringRef getTUIndexSection() = 0; static bool isSupportedVersion(unsigned version) { @@ -272,6 +276,7 @@ DWARFSection AppleNamespacesSection; DWARFSection AppleObjCSection; StringRef CUIndexSection; + StringRef GdbIndexSection; StringRef TUIndexSection; SmallVector, 4> UncompressedSections; @@ -318,6 +323,7 @@ return AddrSection; } StringRef getCUIndexSection() override { return CUIndexSection; } + StringRef getGdbIndexSection() override { return GdbIndexSection; } StringRef getTUIndexSection() override { return TUIndexSection; } }; Index: include/llvm/DebugInfo/DWARF/DWARFGdbIndex.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFGdbIndex.h +++ include/llvm/DebugInfo/DWARF/DWARFGdbIndex.h @@ -0,0 +1,62 @@ +//===-- DWARFGdbIndex.h -----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_DEBUGINFO_DWARFGDBINDEX_H +#define LLVM_LIB_DEBUGINFO_DWARFGDBINDEX_H + +#include "llvm/Support/DataExtractor.h" +#include "llvm/Support/raw_ostream.h" +#include + +namespace llvm { +class DWARFGdbIndex { + uint32_t Version; + + uint32_t CuListOffset; + uint32_t AddressAreaOffset; + uint32_t SymbolTableOffset; + uint32_t ConstantPoolOffset; + + struct CompUnit { + uint64_t Offset; // Offset of a CU in the .debug_info section + uint64_t Length; // Length of that CU + }; + std::vector CuList; + + struct AddressEntry { + uint64_t LowAddress; // The low address + uint64_t HighAddress; // The high address + uint32_t CuIndex; // The CU index + }; + std::vector AddressArea; + + struct SymTableEntry { + uint32_t NameOffset; // Offset of the symbol's name in the constant pool + uint32_t VecOffset; // Offset of the CU vector in the constant pool + }; + std::vector SymbolTable; + + // Each value is CU index + attributes + std::vector>> ConstantPoolVectors; + + StringRef ConstantPoolStrings; + uint32_t StringPoolOffset; + + void dumpCUList(raw_ostream &OS) const; + void dumpAddressArea(raw_ostream &OS) const; + void dumpSymbolTable(raw_ostream &OS) const; + void dumpConstantPool(raw_ostream &OS) const; + +public: + void dump(raw_ostream &OS) const; + void parse(DataExtractor Data); +}; +} + +#endif // LLVM_LIB_DEBUGINFO_DWARFGDBINDEX_H Index: lib/DebugInfo/DWARF/CMakeLists.txt =================================================================== --- lib/DebugInfo/DWARF/CMakeLists.txt +++ lib/DebugInfo/DWARF/CMakeLists.txt @@ -13,6 +13,7 @@ DWARFDebugMacro.cpp DWARFDebugRangeList.cpp DWARFFormValue.cpp + DWARFGdbIndex.cpp DWARFTypeUnit.cpp DWARFUnitIndex.cpp DWARFUnit.cpp Index: lib/DebugInfo/DWARF/DWARFContext.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFContext.cpp +++ lib/DebugInfo/DWARF/DWARFContext.cpp @@ -254,6 +254,11 @@ } } + if (DumpType == DIDT_All || DumpType == DIDT_GdbIndex) { + OS << "\n.gnu_index contents:\n"; + getGdbIndex().dump(OS); + } + if (DumpType == DIDT_All || DumpType == DIDT_AppleNames) dumpAccelSection(OS, "apple_names", getAppleNamesSection(), getStringSection(), isLittleEndian()); @@ -293,6 +298,16 @@ return *TUIndex; } +const DWARFGdbIndex &DWARFContext::getGdbIndex() { + if (GdbIndex) + return *GdbIndex; + + DataExtractor GdbIndexData(getGdbIndexSection(), true /*LE*/, 0); + GdbIndex = llvm::make_unique(); + GdbIndex->parse(GdbIndexData); + return *GdbIndex; +} + const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { if (Abbrev) return Abbrev.get(); @@ -716,6 +731,7 @@ .Case("apple_objc", &AppleObjCSection.Data) .Case("debug_cu_index", &CUIndexSection) .Case("debug_tu_index", &TUIndexSection) + .Case("gdb_index", &GdbIndexSection) // Any more debug info sections go here. .Default(nullptr); if (SectionData) { Index: lib/DebugInfo/DWARF/DWARFGdbIndex.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFGdbIndex.cpp +++ lib/DebugInfo/DWARF/DWARFGdbIndex.cpp @@ -0,0 +1,162 @@ +//===-- DWARFGdbIndex.cpp -------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/Format.h" + +using namespace llvm; + +// .gdb_index section format reference: +// https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html + +void DWARFGdbIndex::dumpCUList(raw_ostream &OS) const { + OS << format("\n CU list offset = 0x%x, has %zu entries:", CuListOffset, + CuList.size()) + << '\n'; + uint32_t I = 0; + for (const CompUnit &CU : CuList) + OS << format(" %d: Offset = 0x%llx, Length = 0x%llx\n", I++, CU.Offset, + CU.Length); +} + +void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const { + OS << format("\n Address area offset = 0x%x, has %zu entries:", + AddressAreaOffset, AddressArea.size()) + << '\n'; + for (const AddressEntry &Addr : AddressArea) + OS << format( + " Low address = 0x%llx, High address = 0x%llx, CU index = %d\n", + Addr.LowAddress, Addr.HighAddress, Addr.CuIndex); +} + +void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const { + OS << format("\n Symbol table offset = 0x%x, size = %zu, filled slots:", + SymbolTableOffset, SymbolTable.size()) + << '\n'; + uint32_t I = -1; + for (const SymTableEntry &E : SymbolTable) { + ++I; + if (!E.NameOffset && !E.VecOffset) + continue; + + OS << format(" %d: Name offset = 0x%x, CU vector offset = 0x%x\n", I, + E.NameOffset, E.VecOffset); + + StringRef Name = ConstantPoolStrings.substr( + ConstantPoolOffset - StringPoolOffset + E.NameOffset); + + auto CuVector = + std::find_if(ConstantPoolVectors.begin(), ConstantPoolVectors.end(), + [&](const std::pair> &V) { + return V.first == E.VecOffset; + }); + assert(CuVector != ConstantPoolVectors.end() && "Invalid symbol table"); + uint32_t CuVectorId = CuVector - ConstantPoolVectors.begin(); + OS << format(" String name: %s, CU vector index: %d\n", Name.data(), + CuVectorId); + } +} + +void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const { + OS << format("\n Constant pool offset = 0x%x, has %zu CU vectors:", + ConstantPoolOffset, ConstantPoolVectors.size()); + uint32_t I = 0; + for (const auto &V : ConstantPoolVectors) { + OS << format("\n %d(0x%x): ", I++, V.first); + for (uint32_t Val : V.second) + OS << format("0x%x ", Val); + } + OS << '\n'; +} + +void DWARFGdbIndex::dump(raw_ostream &OS) const { + OS << " Version = " << Version << '\n'; + + dumpCUList(OS); + dumpAddressArea(OS); + dumpSymbolTable(OS); + dumpConstantPool(OS); +} + +void DWARFGdbIndex::parse(DataExtractor Data) { + uint32_t Offset = 0; + + // Only version 7 is supported at this moment. + Version = Data.getU32(&Offset); + if (Version != 7) + report_fatal_error("only dumping of version 7 is supported"); + + CuListOffset = Data.getU32(&Offset); + uint32_t CuTypesOffset = Data.getU32(&Offset); + AddressAreaOffset = Data.getU32(&Offset); + SymbolTableOffset = Data.getU32(&Offset); + ConstantPoolOffset = Data.getU32(&Offset); + + assert(Offset == CuListOffset); + uint32_t CuListSize = (CuTypesOffset - CuListOffset) / 16; + CuList.reserve(CuListSize); + for (uint32_t I = 0; I < CuListSize; ++I) { + uint64_t CuOffset = Data.getU64(&Offset); + uint64_t CuLength = Data.getU64(&Offset); + CuList.push_back({CuOffset, CuLength}); + } + + // This is a sequence of triplets of 64-bit little-endian values. In a + // triplet, the first value is the CU offset, the second value is the type + // offset in the CU, and the third value is the type signature. + uint32_t CuTypesListSize = (AddressAreaOffset - CuTypesOffset) / 24; + if (CuTypesListSize != 0) + report_fatal_error("dumping of CU types list is not supported"); + + uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20; + AddressArea.reserve(AddressAreaSize); + for (uint32_t I = 0; I < AddressAreaSize; ++I) { + uint64_t LowAddress = Data.getU64(&Offset); + uint64_t HighAddress = Data.getU64(&Offset); + uint32_t CuIndex = Data.getU32(&Offset); + AddressArea.push_back({LowAddress, HighAddress, CuIndex}); + } + + // The symbol table. This is an open addressed hash table. The size of the + // hash table is always a power of 2. + // Each slot in the hash table consists of a pair of offset_type values. The + // first value is the offset of the symbol's name in the constant pool. The + // second value is the offset of the CU vector in the constant pool. + // If both values are 0, then this slot in the hash table is empty. This is ok + // because while 0 is a valid constant pool index, it cannot be a valid index + // for both a string and a CU vector. + uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8; + SymbolTable.reserve(SymTableSize); + uint32_t CuVectorsTotal = 0; + for (uint32_t I = 0; I < SymTableSize; ++I) { + uint32_t NameOffset = Data.getU32(&Offset); + uint32_t CuVecOffset = Data.getU32(&Offset); + SymbolTable.push_back({NameOffset, CuVecOffset}); + if (NameOffset || CuVecOffset) + ++CuVectorsTotal; + } + + // The constant pool. CU vectors are stored first, followed by strings. + // The first value is the number of CU indices in the vector. Each subsequent + // value is the index and symbol attributes of a CU in the CU list. + for (uint32_t I = 0; I < CuVectorsTotal; ++I) { + ConstantPoolVectors.push_back(std::pair>()); + std::pair> &Vec = + ConstantPoolVectors.back(); + Vec.first = Offset - ConstantPoolOffset; + + uint32_t Num = Data.getU32(&Offset); + while (Num--) + Vec.second.push_back(Data.getU32(&Offset)); + } + + ConstantPoolStrings = Data.getData().drop_front(Offset); + StringPoolOffset = Offset; +} Index: test/DebugInfo/dwarfdump-dump-gdbindex.test =================================================================== --- test/DebugInfo/dwarfdump-dump-gdbindex.test +++ test/DebugInfo/dwarfdump-dump-gdbindex.test @@ -0,0 +1,60 @@ +RUN: llvm-dwarfdump -debug-dump=gdb_index %p/Inputs/dwarfdump-gdbindex-v7.elf-x86-64 | FileCheck %s + +; test.cpp: +; double foo1; +; float bar1; +; void method1() {} +; int main() { return 0; } +; test2.cpp: +; double foo2; +; char method2() {} +; Compiled with: +; gcc -gsplit-dwarf -c test.cpp test2.cpp +; gold --gdb-index test.o test2.o -o dwarfdump-gdbindex-v7.elf-x86-64 +; gcc version 5.3.1 20160413, GNU gold (GNU Binutils for Ubuntu 2.26) 1.11 +; Info about gdb-index: https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html + +; CHECK-LABEL: .gnu_index contents: +; CHECK: Version = 7 + +; CHECK: CU list offset = 0x18, has 2 entries: +; CHECK-NEXT: 0: Offset = 0x0, Length = 0x34 +; CHECK-NEXT: 1: Offset = 0x34, Length = 0x34 + +; CHECK: Address area offset = 0x38, has 2 entries: +; CHECK-NEXT: Low address = 0x4000e8, High address = 0x4000fa, CU index = 0 +; CHECK-NEXT: Low address = 0x4000fa, High address = 0x400101, CU index = 1 + +; CHECK: Symbol table offset = 0x60, size = 1024, filled slots: +; CHECK-NEXT: 158: Name offset = 0x67, CU vector offset = 0x18 +; CHECK-NEXT: String name: bar1, CU vector index: 3 +; CHECK-NEXT: 191: Name offset = 0x62, CU vector offset = 0x10 +; CHECK-NEXT: String name: foo1, CU vector index: 2 +; CHECK-NEXT: 192: Name offset = 0x85, CU vector offset = 0x44 +; CHECK-NEXT: String name: foo2, CU vector index: 8 +; CHECK-NEXT: 447: Name offset = 0x55, CU vector offset = 0x0 +; CHECK-NEXT: String name: method1, CU vector index: 0 +; CHECK-NEXT: 448: Name offset = 0x7d, CU vector offset = 0x3c +; CHECK-NEXT: String name: method2, CU vector index: 7 +; CHECK-NEXT: 489: Name offset = 0x5d, CU vector offset = 0x8 +; CHECK-NEXT: String name: main, CU vector index: 1 +; CHECK-NEXT: 511: Name offset = 0x77, CU vector offset = 0x34 +; CHECK-NEXT: String name: float, CU vector index: 6 +; CHECK-NEXT: 518: Name offset = 0x8a, CU vector offset = 0x4c +; CHECK-NEXT: String name: char, CU vector index: 9 +; CHECK-NEXT: 754: Name offset = 0x6c, CU vector offset = 0x20 +; CHECK-NEXT: String name: int, CU vector index: 4 +; CHECK-NEXT: 977: Name offset = 0x70, CU vector offset = 0x28 +; CHECK-NEXT: String name: double, CU vector index: 5 + +; CHECK: Constant pool offset = 0x2060, has 10 CU vectors: +; CHECK-NEXT: 0(0x0): 0x30000000 +; CHECK-NEXT: 1(0x8): 0x30000000 +; CHECK-NEXT: 2(0x10): 0x20000000 +; CHECK-NEXT: 3(0x18): 0x20000000 +; CHECK-NEXT: 4(0x20): 0x90000000 +; CHECK-NEXT: 5(0x28): 0x90000000 0x90000001 +; CHECK-NEXT: 6(0x34): 0x90000000 +; CHECK-NEXT: 7(0x3c): 0x30000001 +; CHECK-NEXT: 8(0x44): 0x20000001 +; CHECK-NEXT: 9(0x4c): 0x90000001 Index: tools/llvm-dwarfdump/llvm-dwarfdump.cpp =================================================================== --- tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -71,6 +71,7 @@ clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo", ".debug_str_offsets.dwo"), clEnumValN(DIDT_CUIndex, "cu_index", ".debug_cu_index"), + clEnumValN(DIDT_GdbIndex, "gdb_index", ".gdb_index"), clEnumValN(DIDT_TUIndex, "tu_index", ".debug_tu_index"), clEnumValEnd)); static void error(StringRef Filename, std::error_code EC) {