From 7dd3eaf20cd9ed9bd06a19dd4fe6d6aa2ac69af3 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Fri, 23 Sep 2016 09:09:26 +0000 Subject: [PATCH] [llvm-dwarfdump] - Teach dwarfdump to dump gdb-index section. gold linker's --gdb-index option currently is able to create the .gdb_index section that allows GDB to locate and read the .dwo files as it needs them, this helps reduce the total size of the object files processed by the linker. More info about that: https://gcc.gnu.org/wiki/DebugFission https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html Patch teaches dwarfdump tool to dump this section. Differential revision: https://reviews.llvm.org/D21503 llvm-svn: 282235 --- include/llvm/DebugInfo/DIContext.h | 1 + include/llvm/DebugInfo/DWARF/DWARFContext.h | 6 + include/llvm/DebugInfo/DWARF/DWARFGdbIndex.h | 68 +++++++ lib/DebugInfo/DWARF/CMakeLists.txt | 1 + lib/DebugInfo/DWARF/DWARFContext.cpp | 17 ++ lib/DebugInfo/DWARF/DWARFGdbIndex.cpp | 175 ++++++++++++++++++ .../Inputs/dwarfdump-gdbindex-v7.elf-x86-64 | Bin 0 -> 14292 bytes test/DebugInfo/dwarfdump-dump-gdbindex.test | 35 ++++ tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 1 + 9 files changed, 304 insertions(+) create mode 100644 include/llvm/DebugInfo/DWARF/DWARFGdbIndex.h create mode 100644 lib/DebugInfo/DWARF/DWARFGdbIndex.cpp create mode 100644 test/DebugInfo/Inputs/dwarfdump-gdbindex-v7.elf-x86-64 create mode 100644 test/DebugInfo/dwarfdump-dump-gdbindex.test diff --git a/include/llvm/DebugInfo/DIContext.h b/include/llvm/DebugInfo/DIContext.h index 2f88371979e..7fe5eabc4e6 100644 --- a/include/llvm/DebugInfo/DIContext.h +++ b/include/llvm/DebugInfo/DIContext.h @@ -124,6 +124,7 @@ enum DIDumpType { DIDT_AppleNamespaces, DIDT_AppleObjC, DIDT_CUIndex, + DIDT_GdbIndex, DIDT_TUIndex, }; diff --git a/include/llvm/DebugInfo/DWARF/DWARFContext.h b/include/llvm/DebugInfo/DWARF/DWARFContext.h index 741a31cb582..72205ef35ed 100644 --- a/include/llvm/DebugInfo/DWARF/DWARFContext.h +++ b/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 @@ class DWARFContext : public DIContext { 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 @@ public: } const DWARFUnitIndex &getCUIndex(); + DWARFGdbIndex &getGdbIndex(); const DWARFUnitIndex &getTUIndex(); /// Get a pointer to the parsed DebugAbbrev object. @@ -220,6 +223,7 @@ public: 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 @@ class DWARFContextInMemory : public DWARFContext { DWARFSection AppleNamespacesSection; DWARFSection AppleObjCSection; StringRef CUIndexSection; + StringRef GdbIndexSection; StringRef TUIndexSection; SmallVector, 4> UncompressedSections; @@ -318,6 +323,7 @@ public: return AddrSection; } StringRef getCUIndexSection() override { return CUIndexSection; } + StringRef getGdbIndexSection() override { return GdbIndexSection; } StringRef getTUIndexSection() override { return TUIndexSection; } }; diff --git a/include/llvm/DebugInfo/DWARF/DWARFGdbIndex.h b/include/llvm/DebugInfo/DWARF/DWARFGdbIndex.h new file mode 100644 index 00000000000..66041be9656 --- /dev/null +++ b/include/llvm/DebugInfo/DWARF/DWARFGdbIndex.h @@ -0,0 +1,68 @@ +//===-- 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/Error.h" +#include "llvm/Support/raw_ostream.h" + +namespace llvm { +class DWARFGdbIndex { + uint32_t Version; + + uint32_t CuListOffset; + uint32_t AddressAreaOffset; + uint32_t SymbolTableOffset; + uint32_t ConstantPoolOffset; + + struct CompUnitEntry { + uint64_t Offset; // Offset of a CU in the .debug_info section. + uint64_t Length; // Length of that CU. + }; + SmallVector CuList; + + struct AddressEntry { + uint64_t LowAddress; // The low address. + uint64_t HighAddress; // The high address. + uint32_t CuIndex; // The CU index. + }; + SmallVector 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. + }; + SmallVector SymbolTable; + + // Each value is CU index + attributes. + SmallVector>, 0> + 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; + + bool parseImpl(DataExtractor Data); + +public: + void dump(raw_ostream &OS); + void parse(DataExtractor Data); + + bool HasContent = false; + bool HasError = false; +}; +} + +#endif // LLVM_LIB_DEBUGINFO_DWARFGDBINDEX_H diff --git a/lib/DebugInfo/DWARF/CMakeLists.txt b/lib/DebugInfo/DWARF/CMakeLists.txt index 7104c5f1039..d54c001d7f7 100644 --- a/lib/DebugInfo/DWARF/CMakeLists.txt +++ b/lib/DebugInfo/DWARF/CMakeLists.txt @@ -13,6 +13,7 @@ add_llvm_library(LLVMDebugInfoDWARF DWARFDebugMacro.cpp DWARFDebugRangeList.cpp DWARFFormValue.cpp + DWARFGdbIndex.cpp DWARFTypeUnit.cpp DWARFUnitIndex.cpp DWARFUnit.cpp diff --git a/lib/DebugInfo/DWARF/DWARFContext.cpp b/lib/DebugInfo/DWARF/DWARFContext.cpp index e8ea71b325a..1a0b7e568de 100644 --- a/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -256,6 +256,12 @@ void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH) { } } + if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) && + !getGdbIndexSection().empty()) { + OS << "\n.gnu_index contents:\n"; + getGdbIndex().dump(OS); + } + if (DumpType == DIDT_All || DumpType == DIDT_AppleNames) dumpAccelSection(OS, "apple_names", getAppleNamesSection(), getStringSection(), isLittleEndian()); @@ -295,6 +301,16 @@ const DWARFUnitIndex &DWARFContext::getTUIndex() { return *TUIndex; } +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(); @@ -718,6 +734,7 @@ DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj, .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) { diff --git a/lib/DebugInfo/DWARF/DWARFGdbIndex.cpp b/lib/DebugInfo/DWARF/DWARFGdbIndex.cpp new file mode 100644 index 00000000000..a3009684dfe --- /dev/null +++ b/lib/DebugInfo/DWARF/DWARFGdbIndex.cpp @@ -0,0 +1,175 @@ +//===-- 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/ADT/Twine.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 CompUnitEntry &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) { + if (HasError) { + OS << "\n\n"; + return; + } + + if (HasContent) { + OS << " Version = " << Version << '\n'; + dumpCUList(OS); + dumpAddressArea(OS); + dumpSymbolTable(OS); + dumpConstantPool(OS); + } +} + +bool DWARFGdbIndex::parseImpl(DataExtractor Data) { + uint32_t Offset = 0; + + // Only version 7 is supported at this moment. + Version = Data.getU32(&Offset); + if (Version != 7) + return false; + + CuListOffset = Data.getU32(&Offset); + uint32_t CuTypesOffset = Data.getU32(&Offset); + AddressAreaOffset = Data.getU32(&Offset); + SymbolTableOffset = Data.getU32(&Offset); + ConstantPoolOffset = Data.getU32(&Offset); + + if (Offset != CuListOffset) + return false; + + 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}); + } + + // CU Types are no longer needed as DWARF skeleton type units never made it + // into the standard. + uint32_t CuTypesListSize = (AddressAreaOffset - CuTypesOffset) / 24; + if (CuTypesListSize != 0) + return false; + + 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.emplace_back(0, SmallVector()); + auto &Vec = ConstantPoolVectors.back(); + Vec.first = Offset - ConstantPoolOffset; + + uint32_t Num = Data.getU32(&Offset); + for (uint32_t j = 0; j < Num; ++j) + Vec.second.push_back(Data.getU32(&Offset)); + } + + ConstantPoolStrings = Data.getData().drop_front(Offset); + StringPoolOffset = Offset; + return true; +} + +void DWARFGdbIndex::parse(DataExtractor Data) { + HasContent = !Data.getData().empty(); + HasError = HasContent && !parseImpl(Data); +} diff --git a/test/DebugInfo/Inputs/dwarfdump-gdbindex-v7.elf-x86-64 b/test/DebugInfo/Inputs/dwarfdump-gdbindex-v7.elf-x86-64 new file mode 100644 index 0000000000000000000000000000000000000000..c6a064f3dc64b68d3253c03b2ca4db9516fe067e GIT binary patch literal 14292 zcmeHO&ui3L6uudMwADIqTE+Sk2Ze_UW-?0AbLub6T^DyjR-slDFVbKfL8J@9>oKFY%2luh z?TQZHRm400K6JJ2JHn5Fnd#f_Um)eu>qM&57Lr-lE+}g`XYCy9>%R|^+XXmoZ&p@T zHX`SrZ0=@eYSa2tK%l?qeO~0AP$HF;($0H4u}KdfH)uyhy&t)IPj-9@PXdf zKT&jGVr%u}`YrC$@4tx&4^Uosd1vxPZ5HONCw#+E*49 zpcBeKhUadb$@Bqn9KejqL%q7Rl+^MukX8@vF11ym(QbK#ZmV8EC@4%E4i@H{q3hMV zb9w?L8)@qF)YQ@Z{+W8$54-s>d(?IgmAkrCyk0EZ&Zu2F5aIcfQ!G1WXF_|99-;TA z&&{a0_Pm=f+QlNYe^hWCWv%+SVsbX5;JDr~41Tdce4e%w&d1L9KEQK0zOgQIFaqR_j4f4Z>O{1ju)lvgh1-wduRwva+?=)mpvY z@fMXGdds1*ue4h&&xZtr%m~5`r#08@(wbA86i(V&awtc40H+@gp zUZXnOskI;r8ZcH^t3$@|YD>5Ebm4e^N5Q)X&n2&I@%i~cgKsrYp@CS2L8}sF zM7+p2MZ6c24Ddn8DvN21o6o0+Va*OtlYpKZ+J!4|zKBT>CH-^;sAXE;LtY`_%CX>;dcm* P(jQVa;R`7JC6)RHQt8=q literal 0 HcmV?d00001 diff --git a/test/DebugInfo/dwarfdump-dump-gdbindex.test b/test/DebugInfo/dwarfdump-dump-gdbindex.test new file mode 100644 index 00000000000..7bdf6ed0c5d --- /dev/null +++ b/test/DebugInfo/dwarfdump-dump-gdbindex.test @@ -0,0 +1,35 @@ +RUN: llvm-dwarfdump -debug-dump=gdb_index %p/Inputs/dwarfdump-gdbindex-v7.elf-x86-64 | FileCheck %s + +; test.cpp: +; int main() { return 0; } +; test2.cpp: +; int main2() { return 0; } +; 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 = 0x4000f3, CU index = 0 +; CHECK-NEXT: Low address = 0x4000f3, High address = 0x4000fe, CU index = 1 + +; CHECK: Symbol table offset = 0x60, size = 1024, filled slots: +; CHECK-NEXT: 489: Name offset = 0x1d, CU vector offset = 0x0 +; CHECK-NEXT: String name: main, CU vector index: 0 +; CHECK-NEXT: 754: Name offset = 0x22, CU vector offset = 0x8 +; CHECK-NEXT: String name: int, CU vector index: 1 +; CHECK-NEXT: 956: Name offset = 0x26, CU vector offset = 0x14 +; CHECK-NEXT: String name: main2, CU vector index: 2 + +; CHECK: Constant pool offset = 0x2060, has 3 CU vectors: +; CHECK-NEXT: 0(0x0): 0x30000000 +; CHECK-NEXT: 1(0x8): 0x90000000 0x90000001 +; CHECK-NEXT: 2(0x14): 0x30000001 diff --git a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp index 4b3a011f861..bbf36793e6d 100644 --- a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -71,6 +71,7 @@ static cl::opt DumpType( 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) {