Index: lld/trunk/ELF/OutputSections.h =================================================================== --- lld/trunk/ELF/OutputSections.h +++ lld/trunk/ELF/OutputSections.h @@ -11,7 +11,6 @@ #define LLD_ELF_OUTPUT_SECTIONS_H #include "Config.h" -#include "GdbIndex.h" #include "Relocations.h" #include "lld/Core/LLVM.h" @@ -105,30 +104,6 @@ uint32_t Link = 0; }; -template class GdbIndexSection final : public OutputSectionBase { - typedef typename ELFT::uint uintX_t; - - const unsigned OffsetTypeSize = 4; - const unsigned CuListOffset = 6 * OffsetTypeSize; - const unsigned CompilationUnitSize = 16; - const unsigned AddressEntrySize = 16 + OffsetTypeSize; - const unsigned SymTabEntrySize = 2 * OffsetTypeSize; - -public: - GdbIndexSection(); - void finalize() override; - void writeTo(uint8_t *Buf) override; - - // Pairs of [CU Offset, CU length]. - std::vector> CompilationUnits; - -private: - void parseDebugSections(); - void readDwarf(InputSection *I); - - uint32_t CuTypesOffset; -}; - // For more information about .gnu.version and .gnu.version_r see: // https://www.akkadia.org/drepper/symbol-versioning @@ -338,7 +313,6 @@ static uint8_t First; static EhFrameHeader *EhFrameHdr; static EhOutputSection *EhFrame; - static GdbIndexSection *GdbIndex; static OutputSection *Bss; static OutputSection *MipsRldMap; static OutputSectionBase *Opd; @@ -391,7 +365,6 @@ template uint8_t Out::First; template EhFrameHeader *Out::EhFrameHdr; template EhOutputSection *Out::EhFrame; -template GdbIndexSection *Out::GdbIndex; template OutputSection *Out::Bss; template OutputSection *Out::MipsRldMap; template OutputSectionBase *Out::Opd; Index: lld/trunk/ELF/OutputSections.cpp =================================================================== --- lld/trunk/ELF/OutputSections.cpp +++ lld/trunk/ELF/OutputSections.cpp @@ -10,7 +10,6 @@ #include "OutputSections.h" #include "Config.h" #include "EhFrame.h" -#include "GdbIndex.h" #include "LinkerScript.h" #include "Memory.h" #include "Strings.h" @@ -63,50 +62,6 @@ Shdr->sh_name = ShName; } -template -GdbIndexSection::GdbIndexSection() - : OutputSectionBase(".gdb_index", SHT_PROGBITS, 0) {} - -template void GdbIndexSection::parseDebugSections() { - std::vector *> &IS = - static_cast *>(Out::DebugInfo)->Sections; - - for (InputSection *I : IS) - readDwarf(I); -} - -template -void GdbIndexSection::readDwarf(InputSection *I) { - std::vector> CuList = readCuList(I); - CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end()); -} - -template void GdbIndexSection::finalize() { - parseDebugSections(); - - // GdbIndex header consist from version fields - // and 5 more fields with different kinds of offsets. - CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize; - this->Size = CuTypesOffset; -} - -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 - Buf += 24; - - // Write the CU list. - for (std::pair CU : CompilationUnits) { - write64le(Buf, CU.first); - write64le(Buf + 8, CU.second); - Buf += 16; - } -} - // Returns the number of version definition entries. Because the first entry // is for the version definition itself, it is the number of versioned symbols // plus one. Note that we don't support multiple versions yet. @@ -908,11 +863,6 @@ template class VersionDefinitionSection; template class VersionDefinitionSection; -template class GdbIndexSection; -template class GdbIndexSection; -template class GdbIndexSection; -template class GdbIndexSection; - template class OutputSectionFactory; template class OutputSectionFactory; template class OutputSectionFactory; Index: lld/trunk/ELF/SyntheticSections.h =================================================================== --- lld/trunk/ELF/SyntheticSections.h +++ lld/trunk/ELF/SyntheticSections.h @@ -10,6 +10,7 @@ #ifndef LLD_ELF_SYNTHETIC_SECTION_H #define LLD_ELF_SYNTHETIC_SECTION_H +#include "GdbIndex.h" #include "InputSection.h" #include "llvm/ADT/SmallPtrSet.h" @@ -502,6 +503,32 @@ std::vector> Entries; }; +template +class GdbIndexSection final : public SyntheticSection { + typedef typename ELFT::uint uintX_t; + + const unsigned OffsetTypeSize = 4; + const unsigned CuListOffset = 6 * OffsetTypeSize; + const unsigned CompilationUnitSize = 16; + const unsigned AddressEntrySize = 16 + OffsetTypeSize; + const unsigned SymTabEntrySize = 2 * OffsetTypeSize; + +public: + GdbIndexSection(); + void finalize() override; + void writeTo(uint8_t *Buf) override; + size_t getSize() const override { return CuTypesOffset; } + + // Pairs of [CU Offset, CU length]. + std::vector> CompilationUnits; + +private: + void parseDebugSections(); + void readDwarf(InputSection *I); + + uint32_t CuTypesOffset; +}; + template InputSection *createCommonSection(); template InputSection *createInterpSection(); template MergeInputSection *createCommentSection(); @@ -514,6 +541,7 @@ static StringTableSection *DynStrTab; static SymbolTableSection *DynSymTab; static GnuHashTableSection *GnuHashTab; + static GdbIndexSection *GdbIndex; static GotSection *Got; static MipsGotSection *MipsGot; static GotPltSection *GotPlt; @@ -535,6 +563,7 @@ template DynamicSection *In::Dynamic; template StringTableSection *In::DynStrTab; template SymbolTableSection *In::DynSymTab; +template GdbIndexSection *In::GdbIndex; template GnuHashTableSection *In::GnuHashTab; template GotSection *In::Got; template MipsGotSection *In::MipsGot; Index: lld/trunk/ELF/SyntheticSections.cpp =================================================================== --- lld/trunk/ELF/SyntheticSections.cpp +++ lld/trunk/ELF/SyntheticSections.cpp @@ -1360,6 +1360,49 @@ return Target->PltHeaderSize + Entries.size() * Target->PltEntrySize; } +template +GdbIndexSection::GdbIndexSection() + : 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); +} + +template +void GdbIndexSection::readDwarf(InputSection *I) { + std::vector> CuList = readCuList(I); + CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end()); +} + +template void GdbIndexSection::finalize() { + parseDebugSections(); + + // GdbIndex header consist from version fields + // and 5 more fields with different kinds of offsets. + CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize; +} + +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 + Buf += 24; + + // Write the CU list. + for (std::pair CU : CompilationUnits) { + write64le(Buf, CU.first); + write64le(Buf + 8, CU.second); + Buf += 16; + } +} + template InputSection *elf::createCommonSection(); template InputSection *elf::createCommonSection(); template InputSection *elf::createCommonSection(); @@ -1469,3 +1512,8 @@ template class elf::PltSection; template class elf::PltSection; template class elf::PltSection; + +template class elf::GdbIndexSection; +template class elf::GdbIndexSection; +template class elf::GdbIndexSection; +template class elf::GdbIndexSection; Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -268,7 +268,7 @@ if (Config->SysvHash) In::HashTab = make>(); if (Config->GdbIndex) - Out::GdbIndex = make>(); + In::GdbIndex = make>(); In::RelaPlt = make>( Config->Rela ? ".rela.plt" : ".rel.plt", false /*Sort*/); @@ -979,8 +979,9 @@ finalizeSynthetic( {In::DynSymTab, In::GnuHashTab, In::HashTab, In::SymTab, In::ShStrTab, In::StrTab, - In::DynStrTab, In::Got, In::MipsGot, In::GotPlt, - In::RelaDyn, In::RelaPlt, In::Plt, In::Dynamic}); + In::DynStrTab, In::GdbIndex, In::Got, + In::MipsGot, In::GotPlt, In::RelaDyn, + In::RelaPlt, In::Plt, In::Dynamic}); } template bool Writer::needsGot() { @@ -1006,8 +1007,8 @@ // This order is not the same as the final output order // because we sort the sections using their attributes below. - if (Out::GdbIndex && Out::DebugInfo) - Add(Out::GdbIndex); + if (In::GdbIndex && Out::DebugInfo) + addInputSec(In::GdbIndex); addInputSec(In::SymTab); addInputSec(In::ShStrTab); addInputSec(In::StrTab);