Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -59,10 +59,11 @@ GotPlt, HashTable, Interp, + LinkerVersion, Merge, - MipsReginfo, - MipsOptions, MipsAbiFlags, + MipsOptions, + MipsReginfo, Plt, Regular, Reloc, @@ -500,6 +501,22 @@ static bool classof(const Base *B) { return B->getKind() == Base::Interp; } }; +// Represents a ".linker-version" section which contains +// a linker version string. +template +class LinkerVersionSection final : public OutputSectionBase { + typedef OutputSectionBase Base; + +public: + LinkerVersionSection(); + void writeTo(uint8_t *Buf) override; + typename Base::Kind getKind() const override { return Base::LinkerVersion; } + + static bool classof(const Base *B) { + return B->getKind() == Base::LinkerVersion; + } +}; + template class StringTableSection final : public OutputSectionBase { typedef OutputSectionBase Base; @@ -770,6 +787,7 @@ static GotSection *Got; static HashTableSection *HashTab; static InterpSection *Interp; + static LinkerVersionSection *LinkerVersion; static OutputSection *Bss; static OutputSection *MipsRldMap; static OutputSectionBase *Opd; @@ -839,6 +857,7 @@ template GotSection *Out::Got; template HashTableSection *Out::HashTab; template InterpSection *Out::Interp; +template LinkerVersionSection *Out::LinkerVersion; template OutputSection *Out::Bss; template OutputSection *Out::MipsRldMap; template OutputSectionBase *Out::Opd; Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -14,6 +14,7 @@ #include "Strings.h" #include "SymbolTable.h" #include "Target.h" +#include "lld/Config/Version.h" #include "lld/Core/Parallel.h" #include "llvm/Support/Dwarf.h" #include "llvm/Support/MD5.h" @@ -451,6 +452,17 @@ } template +LinkerVersionSection::LinkerVersionSection() + : OutputSectionBase(".linker-version", SHT_PROGBITS, 0) { + this->Header.sh_size = getLLDVersion().size() + 1; // +1 for NUL +} + +template void LinkerVersionSection::writeTo(uint8_t *Buf) { + StringRef S = getLLDVersion(); + memcpy(Buf, S.data(), S.size()); +} + +template HashTableSection::HashTableSection() : OutputSectionBase(".hash", SHT_HASH, SHF_ALLOC) { this->Header.sh_entsize = sizeof(Elf_Word); @@ -1978,6 +1990,11 @@ template class DynamicSection; template class DynamicSection; +template class LinkerVersionSection; +template class LinkerVersionSection; +template class LinkerVersionSection; +template class LinkerVersionSection; + template class OutputSection; template class OutputSection; template class OutputSection; Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -133,6 +133,7 @@ OutputSection Bss(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE); DynamicSection Dynamic; EhOutputSection EhFrame; + LinkerVersionSection LinkerVersion; GotSection Got; PltSection Plt; RelocationSection RelaDyn(Config->Rela ? ".rela.dyn" : ".rel.dyn", @@ -219,6 +220,7 @@ Out::GotPlt = GotPlt.get(); Out::HashTab = HashTab.get(); Out::Interp = Interp.get(); + Out::LinkerVersion = &LinkerVersion; Out::Plt = &Plt; Out::RelaDyn = &RelaDyn; Out::RelaPlt = RelaPlt.get(); @@ -904,6 +906,7 @@ // This order is not the same as the final output order // because we sort the sections using their attributes below. + Add(Out::LinkerVersion); Add(Out::SymTab); Add(Out::ShStrTab); Add(Out::StrTab);