Index: lld/trunk/ELF/GdbIndex.h =================================================================== --- lld/trunk/ELF/GdbIndex.h +++ lld/trunk/ELF/GdbIndex.h @@ -12,15 +12,48 @@ #include "InputFiles.h" #include "llvm/Object/ELF.h" +#include "llvm/DebugInfo/DWARF/DWARFContext.h" namespace lld { namespace elf { template class InputSection; -template -std::vector> -readCuList(InputSection *Sec); +// Struct represents single entry of address area of gdb index. +template struct AddressEntry { + InputSectionBase *Section; + uint64_t LowAddress; + uint64_t HighAddress; + uint32_t CuIndex; +}; + +// GdbIndexBuilder is a helper class used for extracting data required +// for building .gdb_index section from objects. +template class GdbIndexBuilder : public LoadedObjectInfo { + typedef typename ELFT::uint uintX_t; + + InputSection *DebugInfoSec; + + std::unique_ptr Dwarf; + +public: + GdbIndexBuilder(InputSection *DebugInfoSec); + + // Extracts the compilation units. Each first element of pair is a offset of a + // CU in the .debug_info section and second is the length of that CU. + std::vector> readCUList(); + + // Extracts the vector of address area entries. Accepts global index of last + // parsed CU. + std::vector> readAddressArea(size_t CurrentCU); + +private: + // Method returns section file offset as a load addres for DWARF parser. That + // allows to find the target section index for address ranges. + uint64_t + getSectionLoadAddress(const llvm::object::SectionRef &Sec) const override; + std::unique_ptr clone() const override; +}; } // namespace elf } // namespace lld Index: lld/trunk/ELF/GdbIndex.cpp =================================================================== --- lld/trunk/ELF/GdbIndex.cpp +++ lld/trunk/ELF/GdbIndex.cpp @@ -57,34 +57,33 @@ // hashtable in according to .gdb_index format specification. // 6) Constant pool is populated at the same time as symbol table. // -// Current version of implementation has 1, 2, 3 steps. So it writes .gdb_index -// header and list of compilation units. Since we so not plan to support types -// CU list area, it is also empty and so far is "implemented". +// Current version implements steps 1-4. So it writes .gdb_index +// header, list of compilation units and address area. Since we so not plan to +// support types CU list area, it is also empty and so far is "implemented". // Other data areas are not yet implemented. //===----------------------------------------------------------------------===// #include "GdbIndex.h" -#include "llvm/DebugInfo/DWARF/DWARFContext.h" +#include "llvm/Object/ELFObjectFile.h" using namespace llvm; using namespace llvm::object; +using namespace lld::elf; template -std::vector> -lld::elf::readCuList(InputSection *DebugInfoSec) { - typedef typename ELFT::uint uintX_t; - - std::unique_ptr Dwarf; +GdbIndexBuilder::GdbIndexBuilder(InputSection *DebugInfoSec) + : DebugInfoSec(DebugInfoSec) { if (Expected> Obj = object::ObjectFile::createObjectFile(DebugInfoSec->getFile()->MB)) - Dwarf.reset(new DWARFContextInMemory(*Obj.get())); - - if (!Dwarf) { + Dwarf.reset(new DWARFContextInMemory(*Obj.get(), this)); + else error(toString(DebugInfoSec->getFile()) + ": error creating DWARF context"); - return {}; - } +} +template +std::vector> +GdbIndexBuilder::readCUList() { std::vector> Ret; for (std::unique_ptr &CU : Dwarf->compile_units()) Ret.push_back( @@ -92,11 +91,52 @@ return Ret; } -template std::vector> -lld::elf::readCuList(InputSection *); -template std::vector> -lld::elf::readCuList(InputSection *); -template std::vector> -lld::elf::readCuList(InputSection *); -template std::vector> -lld::elf::readCuList(InputSection *); +template +static InputSectionBase * +findSection(ArrayRef *> Arr, uint64_t Offset) { + for (InputSectionBase *S : Arr) + if (S && S != &InputSection::Discarded) + if (Offset >= S->Offset && Offset < S->Offset + S->getSize()) + return S; + return nullptr; +} + +template +std::vector> +GdbIndexBuilder::readAddressArea(size_t CurrentCU) { + std::vector> Ret; + for (const auto &CU : Dwarf->compile_units()) { + DWARFAddressRangesVector Ranges; + CU->collectAddressRanges(Ranges); + + ArrayRef *> Sections = + DebugInfoSec->getFile()->getSections(); + + for (std::pair &R : Ranges) + if (InputSectionBase *S = findSection(Sections, R.first)) + Ret.push_back( + {S, R.first - S->Offset, R.second - S->Offset, CurrentCU}); + ++CurrentCU; + } + return Ret; +} + +template +uint64_t GdbIndexBuilder::getSectionLoadAddress( + const object::SectionRef &Sec) const { + return static_cast(Sec).getOffset(); +} + +template +std::unique_ptr GdbIndexBuilder::clone() const { + return {}; +} + +namespace lld { +namespace elf { +template class GdbIndexBuilder; +template class GdbIndexBuilder; +template class GdbIndexBuilder; +template class GdbIndexBuilder; +} +} Index: lld/trunk/ELF/SyntheticSections.h =================================================================== --- lld/trunk/ELF/SyntheticSections.h +++ lld/trunk/ELF/SyntheticSections.h @@ -479,17 +479,22 @@ GdbIndexSection(); void finalize() override; void writeTo(uint8_t *Buf) override; - size_t getSize() const override { return CuTypesOffset; } + size_t getSize() const override; bool empty() const override; // Pairs of [CU Offset, CU length]. std::vector> CompilationUnits; + std::vector> AddressArea; + private: void parseDebugSections(); void readDwarf(InputSection *I); uint32_t CuTypesOffset; + uint32_t SymTabOffset; + + bool Finalized = false; }; // --eh-frame-hdr option tells linker to construct a header for all the Index: lld/trunk/ELF/SyntheticSections.cpp =================================================================== --- lld/trunk/ELF/SyntheticSections.cpp +++ lld/trunk/ELF/SyntheticSections.cpp @@ -1460,34 +1460,51 @@ : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index") {} template void GdbIndexSection::parseDebugSections() { - std::vector *> &IS = - static_cast *>(Out::DebugInfo)->Sections; - - for (InputSection *I : IS) - readDwarf(I); + for (InputSectionBase *S : Symtab::X->Sections) + if (InputSection *IS = dyn_cast>(S)) + if (IS->OutSec && IS->Name == ".debug_info") + readDwarf(IS); } template void GdbIndexSection::readDwarf(InputSection *I) { - std::vector> CuList = readCuList(I); + GdbIndexBuilder Builder(I); + if (ErrorCount) + return; + + size_t CuId = CompilationUnits.size(); + std::vector> CuList = Builder.readCUList(); CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end()); + + std::vector> AddrArea = Builder.readAddressArea(CuId); + AddressArea.insert(AddressArea.end(), AddrArea.begin(), AddrArea.end()); } template void GdbIndexSection::finalize() { + if (Finalized) + return; + Finalized = true; + parseDebugSections(); // GdbIndex header consist from version fields // and 5 more fields with different kinds of offsets. CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize; + SymTabOffset = CuTypesOffset + AddressArea.size() * AddressEntrySize; +} + +template size_t GdbIndexSection::getSize() const { + const_cast *>(this)->finalize(); + return SymTabOffset; } template void GdbIndexSection::writeTo(uint8_t *Buf) { - write32le(Buf, 7); // Write Version - write32le(Buf + 4, CuListOffset); // CU list offset - write32le(Buf + 8, CuTypesOffset); // Types CU list offset - write32le(Buf + 12, CuTypesOffset); // Address area offset - write32le(Buf + 16, CuTypesOffset); // Symbol table offset - write32le(Buf + 20, CuTypesOffset); // Constant pool offset + write32le(Buf, 7); // Write version. + write32le(Buf + 4, CuListOffset); // CU list offset. + write32le(Buf + 8, CuTypesOffset); // Types CU list offset. + write32le(Buf + 12, CuTypesOffset); // Address area offset. + write32le(Buf + 16, SymTabOffset); // Symbol table offset. + write32le(Buf + 20, SymTabOffset); // Constant pool offset. Buf += 24; // Write the CU list. @@ -1496,6 +1513,15 @@ write64le(Buf + 8, CU.second); Buf += 16; } + + // Write the address area. + for (AddressEntry &E : AddressArea) { + uintX_t BaseAddr = E.Section->OutSec->Addr + E.Section->getOffset(0); + write64le(Buf, BaseAddr + E.LowAddress); + write64le(Buf + 8, BaseAddr + E.HighAddress); + write32le(Buf + 16, E.CuIndex); + Buf += 20; + } } template bool GdbIndexSection::empty() const { Index: lld/trunk/test/ELF/gdb-index.s =================================================================== --- lld/trunk/test/ELF/gdb-index.s +++ lld/trunk/test/ELF/gdb-index.s @@ -31,8 +31,10 @@ # CHECK: .gnu_index contents: # CHECK-NEXT: 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 0 entries: -# CHECK: Symbol table offset = 0x38, size = 0, filled slots: -# CHECK: Constant pool offset = 0x38, has 0 CU vectors: +# 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 = 0x201000, High address = 0x20100b, CU index = 0 +# CHECK-NEXT: Low address = 0x20100b, High address = 0x201016, CU index = 1 +# CHECK: Symbol table offset = 0x60, size = 0, filled slots: +# CHECK: Constant pool offset = 0x60, has 0 CU vectors: