diff options
Diffstat (limited to 'lld/ELF/SyntheticSections.cpp')
| -rw-r--r-- | lld/ELF/SyntheticSections.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 2e2fc7331bd..f66f0a37048 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -25,12 +25,14 @@ #include "Target.h" #include "Writer.h" +#include "lld/Config/Version.h" #include "lld/Core/Parallel.h" #include "llvm/Support/Endian.h" #include "llvm/Support/MD5.h" #include "llvm/Support/RandomNumberGenerator.h" #include "llvm/Support/SHA1.h" #include "llvm/Support/xxhash.h" +#include <cstdlib> using namespace llvm; using namespace llvm::ELF; @@ -78,6 +80,36 @@ template <class ELFT> InputSection<ELFT> *elf::createCommonSection() { return Ret; } +// Returns an LLD version string. +static ArrayRef<uint8_t> getVersion() { + // Check LLD_VERSION first for ease of testing. + // You can get consitent output by using the environment variable. + // This is only for testing. + StringRef S = getenv("LLD_VERSION"); + if (S.empty()) + S = Saver.save(Twine("Linker: ") + getLLDVersion()); + + // +1 to include the terminating '\0'. + return {(const uint8_t *)S.data(), S.size() + 1}; +}; + +// Creates a .comment section containing LLD version info. +// With this feature, you can identify LLD-generated binaries easily +// by "objdump -s -j .comment <file>". +// The returned object is a mergeable string section. +template <class ELFT> MergeInputSection<ELFT> *elf::createCommentSection() { + typename ELFT::Shdr Hdr = {}; + Hdr.sh_flags = SHF_MERGE | SHF_STRINGS; + Hdr.sh_type = SHT_PROGBITS; + Hdr.sh_entsize = 1; + Hdr.sh_addralign = 1; + + auto *Ret = make<MergeInputSection<ELFT>>(/*file=*/nullptr, &Hdr, ".comment"); + Ret->Data = getVersion(); + Ret->splitIntoPieces(); + return Ret; +} + // Iterate over sections of the specified type. For each section call // provided function. After that "kill" the section by turning off // "Live" flag, so that they won't be included in the final output. @@ -352,6 +384,11 @@ template InputSection<ELF32BE> *elf::createInterpSection(); template InputSection<ELF64LE> *elf::createInterpSection(); template InputSection<ELF64BE> *elf::createInterpSection(); +template MergeInputSection<ELF32LE> *elf::createCommentSection(); +template MergeInputSection<ELF32BE> *elf::createCommentSection(); +template MergeInputSection<ELF64LE> *elf::createCommentSection(); +template MergeInputSection<ELF64BE> *elf::createCommentSection(); + template class elf::MipsAbiFlagsSection<ELF32LE>; template class elf::MipsAbiFlagsSection<ELF32BE>; template class elf::MipsAbiFlagsSection<ELF64LE>; |

