Index: ELF/InputSection.h =================================================================== --- ELF/InputSection.h +++ ELF/InputSection.h @@ -222,10 +222,11 @@ public: InputSection(ObjectFile *F, const Elf_Shdr *Header, StringRef Name); + virtual ~InputSection() = default; // Write this section to a mmap'ed file, assuming Buf is pointing to // beginning of the output section. - void writeTo(uint8_t *Buf); + virtual void writeTo(uint8_t *Buf); // Relocation sections that refer to this one. llvm::TinyPtrVector RelocSections; @@ -327,6 +328,83 @@ static typename ELFT::Shdr Hdr; }; +template T *zero(T *Val) { + void *P = memset(Val, 0, sizeof(*Val)); + return static_cast(P); +} + +template class SyntheticInputSection : public InputSection { +public: + typedef typename ELFT::uint uintX_t; + + SyntheticInputSection(StringRef Name, uint32_t Type, uintX_t Flags) + : InputSection(nullptr, zero(&Header), Name) { + Header.sh_type = Type; + Header.sh_flags = Flags; + } + + virtual void finalize() {} + virtual bool needed() const { return true; } + +protected: + typename ELFT::Shdr Header; +}; + +template +class BuildIdSection : public SyntheticInputSection { + typedef OutputSectionBase Base; + +public: + void writeTo(uint8_t *Buf) override; + virtual void writeBuildId(ArrayRef Buf) = 0; + +protected: + BuildIdSection(size_t HashSize); + size_t HashSize; + uint8_t *HashBuf = nullptr; +}; + +template +class BuildIdFastHash final : public BuildIdSection { +public: + BuildIdFastHash() : BuildIdSection(8) {} + void writeBuildId(ArrayRef Buf) override; +}; + +template class BuildIdMd5 final : public BuildIdSection { +public: + BuildIdMd5() : BuildIdSection(16) {} + void writeBuildId(ArrayRef Buf) override; +}; + +template class BuildIdSha1 final : public BuildIdSection { +public: + BuildIdSha1() : BuildIdSection(20) {} + void writeBuildId(ArrayRef Buf) override; +}; + +template class BuildIdUuid final : public BuildIdSection { +public: + BuildIdUuid() : BuildIdSection(16) {} + void writeBuildId(ArrayRef Buf) override; +}; + +template +class BuildIdHexstring final : public BuildIdSection { +public: + BuildIdHexstring(); + void writeBuildId(ArrayRef) override; +}; + +// Linker generated sections which can be used as inputs. +template struct In { + static BuildIdSection *BuildId; + static std::vector *> Sections; +}; + +template BuildIdSection *In::BuildId; +template +std::vector *> In::Sections; template CommonInputSection *CommonInputSection::X; template typename ELFT::Shdr CommonInputSection::Hdr; Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -19,6 +19,8 @@ #include "llvm/Support/Compression.h" #include "llvm/Support/Endian.h" +#include "llvm/Support/RandomNumberGenerator.h" +#include "llvm/Support/xxhash.h" using namespace llvm; using namespace llvm::ELF; @@ -828,6 +830,64 @@ } } +template +BuildIdSection::BuildIdSection(size_t HashSize) + : SyntheticInputSection(".note.gnu.build-id", SHT_NOTE, SHF_ALLOC), + HashSize(HashSize) { + // 16 bytes for the note section header. + this->Header.sh_size = 16 + HashSize; +} + +template void BuildIdSection::writeTo(uint8_t *Buf) { + const endianness E = ELFT::TargetEndianness; + write32(Buf, 4); // Name size + write32(Buf + 4, HashSize); // Content size + write32(Buf + 8, NT_GNU_BUILD_ID); // Type + memcpy(Buf + 12, "GNU", 4); // Name string + HashBuf = Buf + 16; +} + +template +void BuildIdFastHash::writeBuildId(ArrayRef Buf) { + const endianness E = ELFT::TargetEndianness; + + // 64-bit xxhash + uint64_t Hash = xxHash64(toStringRef(Buf)); + write64(this->HashBuf, Hash); +} + +template +void BuildIdMd5::writeBuildId(ArrayRef Buf) { + MD5 Hash; + Hash.update(Buf); + MD5::MD5Result Res; + Hash.final(Res); + memcpy(this->HashBuf, Res, 16); +} + +template +void BuildIdSha1::writeBuildId(ArrayRef Buf) { + SHA1 Hash; + Hash.update(Buf); + memcpy(this->HashBuf, Hash.final().data(), 20); +} + +template +void BuildIdUuid::writeBuildId(ArrayRef Buf) { + if (getRandomBytes(this->HashBuf, 16)) + error("entropy source failure"); +} + +template +BuildIdHexstring::BuildIdHexstring() + : BuildIdSection(Config->BuildIdVector.size()) {} + +template +void BuildIdHexstring::writeBuildId(ArrayRef Buf) { + memcpy(this->HashBuf, Config->BuildIdVector.data(), + Config->BuildIdVector.size()); +} + template class elf::InputSectionBase; template class elf::InputSectionBase; template class elf::InputSectionBase; @@ -867,3 +927,33 @@ template class elf::CommonInputSection; template class elf::CommonInputSection; template class elf::CommonInputSection; + +template class elf::BuildIdSection; +template class elf::BuildIdSection; +template class elf::BuildIdSection; +template class elf::BuildIdSection; + +template class elf::BuildIdFastHash; +template class elf::BuildIdFastHash; +template class elf::BuildIdFastHash; +template class elf::BuildIdFastHash; + +template class elf::BuildIdMd5; +template class elf::BuildIdMd5; +template class elf::BuildIdMd5; +template class elf::BuildIdMd5; + +template class elf::BuildIdSha1; +template class elf::BuildIdSha1; +template class elf::BuildIdSha1; +template class elf::BuildIdSha1; + +template class elf::BuildIdUuid; +template class elf::BuildIdUuid; +template class elf::BuildIdUuid; +template class elf::BuildIdUuid; + +template class elf::BuildIdHexstring; +template class elf::BuildIdHexstring; +template class elf::BuildIdHexstring; +template class elf::BuildIdHexstring; Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -51,7 +51,6 @@ typedef typename ELFT::Shdr Elf_Shdr; enum Kind { Base, - BuildId, Dynamic, EHFrame, EHFrameHdr, @@ -738,60 +737,12 @@ std::vector Fdes; }; -template class BuildIdSection : public OutputSectionBase { - typedef OutputSectionBase Base; - -public: - void writeTo(uint8_t *Buf) override; - virtual void writeBuildId(ArrayRef Buf) = 0; - typename Base::Kind getKind() const override { return Base::BuildId; } - static bool classof(const Base *B) { return B->getKind() == Base::BuildId; } - -protected: - BuildIdSection(size_t HashSize); - size_t HashSize; - uint8_t *HashBuf = nullptr; -}; - -template -class BuildIdFastHash final : public BuildIdSection { -public: - BuildIdFastHash() : BuildIdSection(8) {} - void writeBuildId(ArrayRef Buf) override; -}; - -template class BuildIdMd5 final : public BuildIdSection { -public: - BuildIdMd5() : BuildIdSection(16) {} - void writeBuildId(ArrayRef Buf) override; -}; - -template class BuildIdSha1 final : public BuildIdSection { -public: - BuildIdSha1() : BuildIdSection(20) {} - void writeBuildId(ArrayRef Buf) override; -}; - -template class BuildIdUuid final : public BuildIdSection { -public: - BuildIdUuid() : BuildIdSection(16) {} - void writeBuildId(ArrayRef Buf) override; -}; - -template -class BuildIdHexstring final : public BuildIdSection { -public: - BuildIdHexstring(); - void writeBuildId(ArrayRef) override; -}; - // All output sections that are hadnled by the linker specially are // globally accessible. Writer initializes them, so don't use them // until Writer is initialized. template struct Out { typedef typename ELFT::uint uintX_t; typedef typename ELFT::Phdr Elf_Phdr; - static BuildIdSection *BuildId; static DynamicSection *Dynamic; static EhFrameHeader *EhFrameHdr; static EhOutputSection *EhFrame; @@ -862,7 +813,6 @@ return Out::ElfHeader->getSize() + Out::ProgramHeaders->getSize(); } -template BuildIdSection *Out::BuildId; template DynamicSection *Out::Dynamic; template EhFrameHeader *Out::EhFrameHdr; template EhOutputSection *Out::EhFrame; Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -19,9 +19,7 @@ #include "llvm/Support/Dwarf.h" #include "llvm/Support/MD5.h" #include "llvm/Support/MathExtras.h" -#include "llvm/Support/RandomNumberGenerator.h" #include "llvm/Support/SHA1.h" -#include "llvm/Support/xxhash.h" using namespace llvm; using namespace llvm::dwarf; @@ -1739,64 +1737,6 @@ } template -BuildIdSection::BuildIdSection(size_t HashSize) - : OutputSectionBase(".note.gnu.build-id", SHT_NOTE, SHF_ALLOC), - HashSize(HashSize) { - // 16 bytes for the note section header. - this->Header.sh_size = 16 + HashSize; -} - -template void BuildIdSection::writeTo(uint8_t *Buf) { - const endianness E = ELFT::TargetEndianness; - write32(Buf, 4); // Name size - write32(Buf + 4, HashSize); // Content size - write32(Buf + 8, NT_GNU_BUILD_ID); // Type - memcpy(Buf + 12, "GNU", 4); // Name string - HashBuf = Buf + 16; -} - -template -void BuildIdFastHash::writeBuildId(ArrayRef Buf) { - const endianness E = ELFT::TargetEndianness; - - // 64-bit xxhash - uint64_t Hash = xxHash64(toStringRef(Buf)); - write64(this->HashBuf, Hash); -} - -template -void BuildIdMd5::writeBuildId(ArrayRef Buf) { - MD5 Hash; - Hash.update(Buf); - MD5::MD5Result Res; - Hash.final(Res); - memcpy(this->HashBuf, Res, 16); -} - -template -void BuildIdSha1::writeBuildId(ArrayRef Buf) { - SHA1 Hash; - Hash.update(Buf); - memcpy(this->HashBuf, Hash.final().data(), 20); -} - -template -void BuildIdUuid::writeBuildId(ArrayRef Buf) { - if (getRandomBytes(this->HashBuf, 16)) - error("entropy source failure"); -} - -template -BuildIdHexstring::BuildIdHexstring() - : BuildIdSection(Config->BuildIdVector.size()) {} - -template -void BuildIdHexstring::writeBuildId(ArrayRef Buf) { - memcpy(this->HashBuf, Config->BuildIdVector.data(), - Config->BuildIdVector.size()); -} - -template MipsReginfoOutputSection::MipsReginfoOutputSection() : OutputSectionBase(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) { this->Header.sh_addralign = 4; @@ -2103,36 +2043,6 @@ template class VersionDefinitionSection; template class VersionDefinitionSection; -template class BuildIdSection; -template class BuildIdSection; -template class BuildIdSection; -template class BuildIdSection; - -template class BuildIdFastHash; -template class BuildIdFastHash; -template class BuildIdFastHash; -template class BuildIdFastHash; - -template class BuildIdMd5; -template class BuildIdMd5; -template class BuildIdMd5; -template class BuildIdMd5; - -template class BuildIdSha1; -template class BuildIdSha1; -template class BuildIdSha1; -template class BuildIdSha1; - -template class BuildIdUuid; -template class BuildIdUuid; -template class BuildIdUuid; -template class BuildIdUuid; - -template class BuildIdHexstring; -template class BuildIdHexstring; -template class BuildIdHexstring; -template class BuildIdHexstring; - template class GdbIndexSection; template class GdbIndexSection; template class GdbIndexSection; Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -50,10 +50,12 @@ void copyLocalSymbols(); void addReservedSymbols(); + void addInputSec(InputSectionBase *S); void createSections(); void forEachRelSec(std::function &, const typename ELFT::Shdr &)> Fn); void sortSections(); + void addLinkerGeneratedSections(); void finalizeSections(); void addPredefinedSections(); bool needsGot(); @@ -210,7 +212,6 @@ VerDef.reset(new VersionDefinitionSection()); Out::Bss = &Bss; - Out::BuildId = BuildId.get(); Out::DynStrTab = DynStrTab.get(); Out::DynSymTab = DynSymTab.get(); Out::Dynamic = &Dynamic; @@ -242,6 +243,10 @@ Out::InitArray = nullptr; Out::FiniArray = nullptr; + // Initialize linker generated sections + In::BuildId = BuildId.get(); + In::Sections = {BuildId.get()}; + Writer().run(); Out::Pool.clear(); } @@ -689,6 +694,17 @@ } } +template +void Writer::addInputSec(InputSectionBase *IS) { + OutputSectionBase *Sec; + bool IsNew; + StringRef OutsecName = getOutputSectionName(IS->Name, Alloc); + std::tie(Sec, IsNew) = Factory.create(IS, OutsecName); + if (IsNew) + OutputSections.push_back(Sec); + Sec->addSection(IS); +} + template void Writer::createSections() { for (elf::ObjectFile *F : Symtab::X->getObjectFiles()) { for (InputSectionBase *IS : F->getSections()) { @@ -696,13 +712,7 @@ reportDiscarded(IS); continue; } - OutputSectionBase *Sec; - bool IsNew; - StringRef OutsecName = getOutputSectionName(IS->Name, Alloc); - std::tie(Sec, IsNew) = Factory.create(IS, OutsecName); - if (IsNew) - OutputSections.push_back(Sec); - Sec->addSection(IS); + addInputSec(IS); } } @@ -765,6 +775,12 @@ } } +template void Writer::addLinkerGeneratedSections() { + for (SyntheticInputSection *S : In::Sections) + if (S && S->needed()) + addInputSec(S); +} + // Create output section objects and add them to OutputSections. template void Writer::finalizeSections() { Out::DebugInfo = findSection(".debug_info"); @@ -834,6 +850,10 @@ // This function adds linker-created Out::* sections. addPredefinedSections(); + // Adds linker generated input sections to + // corresponding output sections. + addLinkerGeneratedSections(); + sortSections(); unsigned I = 1; @@ -842,6 +862,13 @@ Sec->setSHName(Out::ShStrTab->addString(Sec->getName())); } + // Finalize linker generated sections. + for (SyntheticInputSection *S : In::Sections) + if (S && S->OutSec) { + S->finalize(); + S->OutSec->assignOffsets(); + } + // Finalizers fix each section's size. // .dynsym is finalized early since that may fill up .gnu.hash. if (Out::DynSymTab) @@ -886,12 +913,6 @@ OutputSections.push_back(OS); }; - // A core file does not usually contain unmodified segments except - // the first page of the executable. Add the build ID section to beginning of - // the file so that the section is included in the first page. - if (Out::BuildId) - OutputSections.insert(OutputSections.begin(), Out::BuildId); - // Add .interp at first because some loaders want to see that section // on the first page of the executable file when loaded into memory. if (Out::Interp) @@ -1471,13 +1492,13 @@ } template void Writer::writeBuildId() { - if (!Out::BuildId) + if (!In::BuildId || !In::BuildId->OutSec) return; // Compute a hash of all sections of the output file. uint8_t *Start = Buffer->getBufferStart(); uint8_t *End = Start + FileSize; - Out::BuildId->writeBuildId({Start, End}); + In::BuildId->writeBuildId({Start, End}); } template void elf::writeResult(); Index: test/ELF/build-id.s =================================================================== --- test/ELF/build-id.s +++ test/ELF/build-id.s @@ -23,9 +23,9 @@ .section .note.test, "a", @note .quad 42 +# DEFAULT: Contents of section .note.test: # DEFAULT: Contents of section .note.gnu.build-id: # DEFAULT-NEXT: 04000000 08000000 03000000 474e5500 ............GNU. -# DEFAULT: Contents of section .note.test: # MD5: Contents of section .note.gnu.build-id: # MD5-NEXT: 04000000 10000000 03000000 474e5500 ............GNU.