2015-07-25 05:03:07 +08:00
|
|
|
//===- Writer.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-09-05 06:48:30 +08:00
|
|
|
#include "Writer.h"
|
2015-08-06 07:51:50 +08:00
|
|
|
#include "Config.h"
|
2016-02-12 05:17:59 +08:00
|
|
|
#include "LinkerScript.h"
|
2017-01-14 05:05:46 +08:00
|
|
|
#include "MapFile.h"
|
2016-12-18 22:06:06 +08:00
|
|
|
#include "Memory.h"
|
2015-09-22 05:38:08 +08:00
|
|
|
#include "OutputSections.h"
|
2016-05-25 04:24:43 +08:00
|
|
|
#include "Relocations.h"
|
2016-06-29 17:08:02 +08:00
|
|
|
#include "Strings.h"
|
2015-08-06 07:24:46 +08:00
|
|
|
#include "SymbolTable.h"
|
2016-11-02 04:28:21 +08:00
|
|
|
#include "SyntheticSections.h"
|
2015-09-23 02:19:46 +08:00
|
|
|
#include "Target.h"
|
2015-11-12 17:52:08 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2015-10-13 04:51:48 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2015-08-06 07:24:46 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2016-12-06 01:40:37 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-02-06 08:06:26 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-09-29 09:45:22 +08:00
|
|
|
#include <climits>
|
2016-12-06 01:40:37 +08:00
|
|
|
#include <thread>
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
using namespace llvm::object;
|
2016-10-10 17:39:26 +08:00
|
|
|
using namespace llvm::support;
|
|
|
|
using namespace llvm::support::endian;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
2016-02-28 08:25:54 +08:00
|
|
|
using namespace lld::elf;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-08-06 07:24:46 +08:00
|
|
|
namespace {
|
|
|
|
// The writer writes a SymbolTable result to a file.
|
|
|
|
template <class ELFT> class Writer {
|
|
|
|
public:
|
2016-03-15 07:16:09 +08:00
|
|
|
typedef typename ELFT::uint uintX_t;
|
|
|
|
typedef typename ELFT::Shdr Elf_Shdr;
|
|
|
|
typedef typename ELFT::Ehdr Elf_Ehdr;
|
|
|
|
typedef typename ELFT::Phdr Elf_Phdr;
|
|
|
|
typedef typename ELFT::Sym Elf_Sym;
|
|
|
|
typedef typename ELFT::SymRange Elf_Sym_Range;
|
|
|
|
typedef typename ELFT::Rela Elf_Rela;
|
2015-08-06 07:24:46 +08:00
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
2016-11-02 07:17:45 +08:00
|
|
|
void createSyntheticSections();
|
2015-10-09 07:49:30 +08:00
|
|
|
void copyLocalSymbols();
|
2015-12-26 15:50:39 +08:00
|
|
|
void addReservedSymbols();
|
2016-11-01 17:49:24 +08:00
|
|
|
void addInputSec(InputSectionBase<ELFT> *S);
|
2016-07-30 00:18:47 +08:00
|
|
|
void createSections();
|
2016-11-10 22:53:24 +08:00
|
|
|
void forEachRelSec(std::function<void(InputSectionBase<ELFT> &)> Fn);
|
2016-09-22 06:36:19 +08:00
|
|
|
void sortSections();
|
2016-07-20 22:43:20 +08:00
|
|
|
void finalizeSections();
|
2015-12-26 15:50:41 +08:00
|
|
|
void addPredefinedSections();
|
2015-12-25 15:38:58 +08:00
|
|
|
|
2016-12-20 01:01:01 +08:00
|
|
|
std::vector<PhdrEntry> createPhdrs();
|
2016-12-06 21:43:34 +08:00
|
|
|
void removeEmptyPTLoad();
|
2016-12-20 01:01:01 +08:00
|
|
|
void addPtArmExid(std::vector<PhdrEntry> &Phdrs);
|
2015-08-06 07:24:46 +08:00
|
|
|
void assignAddresses();
|
2016-04-02 01:07:17 +08:00
|
|
|
void assignFileOffsets();
|
2016-08-25 17:05:47 +08:00
|
|
|
void assignFileOffsetsBinary();
|
2016-04-02 01:07:17 +08:00
|
|
|
void setPhdrs();
|
2016-04-06 15:20:45 +08:00
|
|
|
void fixHeaders();
|
2016-03-31 03:41:51 +08:00
|
|
|
void fixSectionAlignments();
|
2015-12-26 18:52:26 +08:00
|
|
|
void fixAbsoluteSymbols();
|
2016-04-02 01:24:19 +08:00
|
|
|
void openFile();
|
2015-08-06 07:24:46 +08:00
|
|
|
void writeHeader();
|
|
|
|
void writeSections();
|
2016-08-25 17:05:47 +08:00
|
|
|
void writeSectionsBinary();
|
ELF: Implement --build-id.
This patch implements --build-id. After the linker creates an output file
in the memory buffer, it computes the FNV1 hash of the resulting file
and set the hash to the .note section as a build-id.
GNU ld and gold have the same feature, but their default choice of the
hash function is different. Their default is SHA1.
We made a deliberate choice to not use a secure hash function for the
sake of performance. Computing a secure hash is slow -- for example,
MD5 throughput is usually 400 MB/s or so. SHA1 is slower than that.
As a result, if you pass --build-id to gold, then the linker becomes about
10% slower than that without the option. We observed a similar degradation
in an experimental implementation of build-id for LLD. On the other hand,
we observed only 1-2% performance degradation with the FNV hash.
Since build-id is not for digital certificate or anything, we think that
a very small probability of collision is acceptable.
We considered using other signals such as using input file timestamps as
inputs to a secure hash function. But such signals would have an issue
with build reproducibility (if you build a binary from the same source
tree using the same toolchain, the build id should become the same.)
GNU linkers accepts --build-id=<style> option where style is one of
"MD5", "SHA1", or an arbitrary hex string. That option is out of scope
of this patch.
http://reviews.llvm.org/D18091
llvm-svn: 263292
2016-03-12 04:51:53 +08:00
|
|
|
void writeBuildId();
|
2015-08-06 07:24:46 +08:00
|
|
|
|
2016-07-17 02:55:47 +08:00
|
|
|
std::unique_ptr<FileOutputBuffer> Buffer;
|
2015-09-18 03:58:07 +08:00
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
std::vector<OutputSectionBase *> OutputSections;
|
2016-07-20 22:43:20 +08:00
|
|
|
OutputSectionFactory<ELFT> Factory;
|
2016-02-11 06:43:13 +08:00
|
|
|
|
2015-12-26 17:47:57 +08:00
|
|
|
void addRelIpltSymbols();
|
2015-12-26 17:48:00 +08:00
|
|
|
void addStartEndSymbols();
|
2016-11-10 07:23:45 +08:00
|
|
|
void addStartStopSymbols(OutputSectionBase *Sec);
|
2016-11-24 06:41:00 +08:00
|
|
|
uintX_t getEntryAddr();
|
2016-11-10 07:23:45 +08:00
|
|
|
OutputSectionBase *findSection(StringRef Name);
|
2015-10-11 06:34:30 +08:00
|
|
|
|
2016-12-20 01:01:01 +08:00
|
|
|
std::vector<PhdrEntry> Phdrs;
|
2015-09-18 03:58:07 +08:00
|
|
|
|
2015-08-12 07:14:13 +08:00
|
|
|
uintX_t FileSize;
|
2015-08-06 07:24:46 +08:00
|
|
|
uintX_t SectionHeaderOff;
|
2016-12-20 05:21:07 +08:00
|
|
|
bool AllocateHeader = true;
|
2015-08-06 07:24:46 +08:00
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-10-29 04:57:25 +08:00
|
|
|
StringRef elf::getOutputSectionName(StringRef Name) {
|
2016-10-05 18:10:45 +08:00
|
|
|
if (Config->Relocatable)
|
|
|
|
return Name;
|
|
|
|
|
2016-09-20 03:59:21 +08:00
|
|
|
for (StringRef V :
|
|
|
|
{".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.",
|
|
|
|
".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
|
|
|
|
".gcc_except_table.", ".tdata.", ".ARM.exidx."}) {
|
|
|
|
StringRef Prefix = V.drop_back();
|
|
|
|
if (Name.startswith(V) || Name == Prefix)
|
|
|
|
return Prefix;
|
|
|
|
}
|
2016-10-13 06:36:31 +08:00
|
|
|
|
2016-11-06 07:05:47 +08:00
|
|
|
// CommonSection is identified as "COMMON" in linker scripts.
|
|
|
|
// By default, it should go to .bss section.
|
|
|
|
if (Name == "COMMON")
|
|
|
|
return ".bss";
|
|
|
|
|
2016-10-13 06:36:31 +08:00
|
|
|
// ".zdebug_" is a prefix for ZLIB-compressed sections.
|
|
|
|
// Because we decompressed input sections, we want to remove 'z'.
|
|
|
|
if (Name.startswith(".zdebug_"))
|
2016-10-29 04:57:25 +08:00
|
|
|
return Saver.save(Twine(".") + Name.substr(2));
|
2016-07-12 16:50:42 +08:00
|
|
|
return Name;
|
|
|
|
}
|
|
|
|
|
2016-07-26 06:26:28 +08:00
|
|
|
template <class ELFT> void elf::reportDiscarded(InputSectionBase<ELFT> *IS) {
|
2017-01-14 00:18:15 +08:00
|
|
|
if (IS == In<ELFT>::ShStrTab)
|
|
|
|
error("discarding .shstrtab section is not allowed");
|
2016-11-09 02:23:02 +08:00
|
|
|
if (!Config->PrintGcSections)
|
2016-07-12 16:50:42 +08:00
|
|
|
return;
|
2016-09-08 22:06:08 +08:00
|
|
|
errs() << "removing unused section from '" << IS->Name << "' in file '"
|
|
|
|
<< IS->getFile()->getName() << "'\n";
|
2016-07-12 16:50:42 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 19:01:23 +08:00
|
|
|
template <class ELFT> static bool needsInterpSection() {
|
|
|
|
return !Symtab<ELFT>::X->getSharedFiles().empty() &&
|
2016-08-16 14:40:58 +08:00
|
|
|
!Config->DynamicLinker.empty() &&
|
|
|
|
!Script<ELFT>::X->ignoreInterpSection();
|
2016-07-21 19:01:23 +08:00
|
|
|
}
|
|
|
|
|
2016-11-14 18:14:18 +08:00
|
|
|
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
|
2015-10-08 03:18:16 +08:00
|
|
|
|
2016-12-06 21:43:34 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() {
|
2016-12-20 01:01:01 +08:00
|
|
|
auto I = std::remove_if(Phdrs.begin(), Phdrs.end(), [&](const PhdrEntry &P) {
|
|
|
|
if (P.p_type != PT_LOAD)
|
2016-12-06 21:43:34 +08:00
|
|
|
return false;
|
2016-12-08 11:17:05 +08:00
|
|
|
if (!P.First)
|
|
|
|
return true;
|
2016-12-06 21:43:34 +08:00
|
|
|
uintX_t Size = P.Last->Addr + P.Last->Size - P.First->Addr;
|
|
|
|
return Size == 0;
|
|
|
|
});
|
|
|
|
Phdrs.erase(I, Phdrs.end());
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// The main function of the writer.
|
2015-08-06 07:24:46 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::run() {
|
2016-11-21 10:11:05 +08:00
|
|
|
// Create linker-synthesized sections such as .got or .plt.
|
|
|
|
// Such sections are of type input section.
|
2016-11-02 07:17:45 +08:00
|
|
|
createSyntheticSections();
|
2016-11-15 16:19:02 +08:00
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// We need to create some reserved symbols such as _end. Create them.
|
2016-11-15 16:19:02 +08:00
|
|
|
if (!Config->Relocatable)
|
|
|
|
addReservedSymbols();
|
2016-07-20 22:43:20 +08:00
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// Some architectures use small displacements for jump instructions.
|
|
|
|
// It is linker's responsibility to create thunks containing long
|
|
|
|
// jump instructions if jump targets are too far. Create thunks.
|
2016-08-11 15:56:43 +08:00
|
|
|
if (Target->NeedsThunks)
|
|
|
|
forEachRelSec(createThunks<ELFT>);
|
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// Create output sections.
|
2016-08-04 20:13:05 +08:00
|
|
|
Script<ELFT>::X->OutputSections = &OutputSections;
|
2016-09-16 23:30:47 +08:00
|
|
|
if (ScriptConfig->HasSections) {
|
2016-11-21 10:11:05 +08:00
|
|
|
// If linker script contains SECTIONS commands, let it create sections.
|
|
|
|
Script<ELFT>::X->processCommands(Factory);
|
|
|
|
|
|
|
|
// Linker scripts may have left some input sections unassigned.
|
|
|
|
// Assign such sections using the default rule.
|
|
|
|
Script<ELFT>::X->addOrphanSections(Factory);
|
2016-09-16 23:30:47 +08:00
|
|
|
} else {
|
2016-11-21 10:11:05 +08:00
|
|
|
// If linker script does not contain SECTIONS commands, create
|
|
|
|
// output sections by default rules. We still need to give the
|
|
|
|
// linker script a chance to run, because it might contain
|
|
|
|
// non-SECTIONS commands such as ASSERT.
|
2016-07-30 00:18:47 +08:00
|
|
|
createSections();
|
2016-09-16 23:30:47 +08:00
|
|
|
Script<ELFT>::X->processCommands(Factory);
|
|
|
|
}
|
2016-07-30 00:18:47 +08:00
|
|
|
|
2016-09-23 21:17:16 +08:00
|
|
|
if (Config->Discard != DiscardPolicy::All)
|
|
|
|
copyLocalSymbols();
|
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// Now that we have a complete set of output sections. This function
|
|
|
|
// completes section contents. For example, we need to add strings
|
|
|
|
// to the string table, and add entries to .got and .plt.
|
|
|
|
// finalizeSections does that.
|
2016-07-20 22:43:20 +08:00
|
|
|
finalizeSections();
|
2016-11-24 09:43:21 +08:00
|
|
|
if (ErrorCount)
|
2016-01-29 06:56:29 +08:00
|
|
|
return;
|
2016-04-02 01:07:17 +08:00
|
|
|
|
|
|
|
if (Config->Relocatable) {
|
|
|
|
assignFileOffsets();
|
|
|
|
} else {
|
2016-09-14 16:32:36 +08:00
|
|
|
if (ScriptConfig->HasSections) {
|
2016-09-30 02:50:34 +08:00
|
|
|
Script<ELFT>::X->assignAddresses(Phdrs);
|
2016-04-16 18:10:32 +08:00
|
|
|
} else {
|
|
|
|
fixSectionAlignments();
|
|
|
|
assignAddresses();
|
|
|
|
}
|
2016-08-25 17:05:47 +08:00
|
|
|
|
2016-12-06 21:43:34 +08:00
|
|
|
// Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
|
|
|
|
// 0 sized region. This has to be done late since only after assignAddresses
|
|
|
|
// we know the size of the sections.
|
|
|
|
removeEmptyPTLoad();
|
|
|
|
|
2016-08-25 17:05:47 +08:00
|
|
|
if (!Config->OFormatBinary)
|
|
|
|
assignFileOffsets();
|
|
|
|
else
|
|
|
|
assignFileOffsetsBinary();
|
|
|
|
|
2016-04-02 01:07:17 +08:00
|
|
|
setPhdrs();
|
2016-04-02 01:11:42 +08:00
|
|
|
fixAbsoluteSymbols();
|
2016-02-25 16:23:37 +08:00
|
|
|
}
|
2016-04-02 01:07:17 +08:00
|
|
|
|
2017-01-17 21:50:34 +08:00
|
|
|
// It does not make sense try to open the file if we have error already.
|
|
|
|
if (ErrorCount)
|
|
|
|
return;
|
2016-11-21 10:11:05 +08:00
|
|
|
// Write the result down to a file.
|
2016-04-02 01:24:19 +08:00
|
|
|
openFile();
|
2016-11-24 09:43:21 +08:00
|
|
|
if (ErrorCount)
|
2016-02-03 06:48:04 +08:00
|
|
|
return;
|
2016-08-25 17:05:47 +08:00
|
|
|
if (!Config->OFormatBinary) {
|
|
|
|
writeHeader();
|
|
|
|
writeSections();
|
|
|
|
} else {
|
|
|
|
writeSectionsBinary();
|
|
|
|
}
|
2016-11-21 10:11:05 +08:00
|
|
|
|
|
|
|
// Backfill .note.gnu.build-id section content. This is done at last
|
|
|
|
// because the content is usually a hash value of the entire output file.
|
ELF: Implement --build-id.
This patch implements --build-id. After the linker creates an output file
in the memory buffer, it computes the FNV1 hash of the resulting file
and set the hash to the .note section as a build-id.
GNU ld and gold have the same feature, but their default choice of the
hash function is different. Their default is SHA1.
We made a deliberate choice to not use a secure hash function for the
sake of performance. Computing a secure hash is slow -- for example,
MD5 throughput is usually 400 MB/s or so. SHA1 is slower than that.
As a result, if you pass --build-id to gold, then the linker becomes about
10% slower than that without the option. We observed a similar degradation
in an experimental implementation of build-id for LLD. On the other hand,
we observed only 1-2% performance degradation with the FNV hash.
Since build-id is not for digital certificate or anything, we think that
a very small probability of collision is acceptable.
We considered using other signals such as using input file timestamps as
inputs to a secure hash function. But such signals would have an issue
with build reproducibility (if you build a binary from the same source
tree using the same toolchain, the build id should become the same.)
GNU linkers accepts --build-id=<style> option where style is one of
"MD5", "SHA1", or an arbitrary hex string. That option is out of scope
of this patch.
http://reviews.llvm.org/D18091
llvm-svn: 263292
2016-03-12 04:51:53 +08:00
|
|
|
writeBuildId();
|
2016-11-24 09:43:21 +08:00
|
|
|
if (ErrorCount)
|
2016-02-02 07:28:21 +08:00
|
|
|
return;
|
2016-11-21 10:11:05 +08:00
|
|
|
|
2017-01-18 11:34:38 +08:00
|
|
|
// Handle -Map option.
|
2017-01-14 05:05:46 +08:00
|
|
|
writeMapFile<ELFT>(OutputSections);
|
2017-01-18 11:34:38 +08:00
|
|
|
if (ErrorCount)
|
|
|
|
return;
|
|
|
|
|
2016-07-15 10:01:03 +08:00
|
|
|
if (auto EC = Buffer->commit())
|
2017-01-13 06:18:04 +08:00
|
|
|
error("failed to write to the output file: " + EC.message());
|
2016-11-21 10:11:05 +08:00
|
|
|
|
|
|
|
// Flush the output streams and exit immediately. A full shutdown
|
|
|
|
// is a good test that we are keeping track of all allocated memory,
|
|
|
|
// but actually freeing it is a waste of time in a regular linker run.
|
|
|
|
if (Config->ExitEarly)
|
2016-10-27 21:32:32 +08:00
|
|
|
exitLld(0);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:17:45 +08:00
|
|
|
// Initialize Out<ELFT> members.
|
|
|
|
template <class ELFT> void Writer<ELFT>::createSyntheticSections() {
|
|
|
|
// Initialize all pointers with NULL. This is needed because
|
|
|
|
// you can call lld::elf::main more than once as a library.
|
|
|
|
memset(&Out<ELFT>::First, 0, sizeof(Out<ELFT>));
|
|
|
|
|
|
|
|
// Create singleton output sections.
|
|
|
|
Out<ELFT>::Bss =
|
|
|
|
make<OutputSection<ELFT>>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
|
2017-01-10 09:21:50 +08:00
|
|
|
Out<ELFT>::BssRelRo = make<OutputSection<ELFT>>(".bss.rel.ro", SHT_NOBITS,
|
|
|
|
SHF_ALLOC | SHF_WRITE);
|
2016-11-14 17:16:00 +08:00
|
|
|
In<ELFT>::DynStrTab = make<StringTableSection<ELFT>>(".dynstr", true);
|
2016-11-15 20:26:55 +08:00
|
|
|
In<ELFT>::Dynamic = make<DynamicSection<ELFT>>();
|
2016-11-02 07:17:45 +08:00
|
|
|
Out<ELFT>::EhFrame = make<EhOutputSection<ELFT>>();
|
2016-11-16 18:02:27 +08:00
|
|
|
In<ELFT>::RelaDyn = make<RelocationSection<ELFT>>(
|
2016-11-02 07:17:45 +08:00
|
|
|
Config->Rela ? ".rela.dyn" : ".rel.dyn", Config->ZCombreloc);
|
2016-11-14 17:16:00 +08:00
|
|
|
In<ELFT>::ShStrTab = make<StringTableSection<ELFT>>(".shstrtab", false);
|
2016-11-02 07:17:45 +08:00
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
Out<ELFT>::ElfHeader = make<OutputSectionBase>("", 0, SHF_ALLOC);
|
2016-11-09 09:42:41 +08:00
|
|
|
Out<ELFT>::ElfHeader->Size = sizeof(Elf_Ehdr);
|
2016-11-10 07:23:45 +08:00
|
|
|
Out<ELFT>::ProgramHeaders = make<OutputSectionBase>("", 0, SHF_ALLOC);
|
2016-11-02 07:17:45 +08:00
|
|
|
Out<ELFT>::ProgramHeaders->updateAlignment(sizeof(uintX_t));
|
|
|
|
|
2016-11-06 07:05:47 +08:00
|
|
|
if (needsInterpSection<ELFT>()) {
|
2016-11-08 22:56:27 +08:00
|
|
|
In<ELFT>::Interp = createInterpSection<ELFT>();
|
2016-11-06 07:05:47 +08:00
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::Interp);
|
|
|
|
} else {
|
|
|
|
In<ELFT>::Interp = nullptr;
|
|
|
|
}
|
2016-11-02 07:17:45 +08:00
|
|
|
|
2016-11-30 00:05:27 +08:00
|
|
|
if (!Config->Relocatable)
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(createCommentSection<ELFT>());
|
2016-11-02 07:17:45 +08:00
|
|
|
|
|
|
|
if (Config->Strip != StripPolicy::All) {
|
2016-11-14 17:16:00 +08:00
|
|
|
In<ELFT>::StrTab = make<StringTableSection<ELFT>>(".strtab", false);
|
2016-11-17 17:16:34 +08:00
|
|
|
In<ELFT>::SymTab = make<SymbolTableSection<ELFT>>(*In<ELFT>::StrTab);
|
2016-11-02 07:17:45 +08:00
|
|
|
}
|
|
|
|
|
2016-11-22 08:54:15 +08:00
|
|
|
if (Config->BuildId != BuildIdKind::None) {
|
|
|
|
In<ELFT>::BuildId = make<BuildIdSection<ELFT>>();
|
2016-11-06 07:05:47 +08:00
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::BuildId);
|
2016-11-22 08:54:15 +08:00
|
|
|
}
|
2016-11-06 07:05:47 +08:00
|
|
|
|
2016-11-08 22:42:34 +08:00
|
|
|
InputSection<ELFT> *Common = createCommonSection<ELFT>();
|
2016-11-06 07:05:47 +08:00
|
|
|
if (!Common->Data.empty()) {
|
|
|
|
In<ELFT>::Common = Common;
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(Common);
|
|
|
|
}
|
2016-11-10 05:36:56 +08:00
|
|
|
|
2016-11-22 12:28:39 +08:00
|
|
|
// Add MIPS-specific sections.
|
2016-11-25 16:05:41 +08:00
|
|
|
bool HasDynSymTab = !Symtab<ELFT>::X->getSharedFiles().empty() || Config->Pic;
|
2016-11-10 05:36:56 +08:00
|
|
|
if (Config->EMachine == EM_MIPS) {
|
2016-11-25 16:05:41 +08:00
|
|
|
if (!Config->Shared && HasDynSymTab) {
|
2016-11-23 03:24:52 +08:00
|
|
|
In<ELFT>::MipsRldMap = make<MipsRldMapSection<ELFT>>();
|
2016-11-23 01:49:14 +08:00
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::MipsRldMap);
|
|
|
|
}
|
2016-11-22 12:28:39 +08:00
|
|
|
if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
|
2016-11-22 11:57:06 +08:00
|
|
|
Symtab<ELFT>::X->Sections.push_back(Sec);
|
2016-11-22 12:28:39 +08:00
|
|
|
if (auto *Sec = MipsOptionsSection<ELFT>::create())
|
2016-11-22 12:13:09 +08:00
|
|
|
Symtab<ELFT>::X->Sections.push_back(Sec);
|
2016-11-22 12:28:39 +08:00
|
|
|
if (auto *Sec = MipsReginfoSection<ELFT>::create())
|
2016-11-22 11:57:08 +08:00
|
|
|
Symtab<ELFT>::X->Sections.push_back(Sec);
|
2016-11-10 05:36:56 +08:00
|
|
|
}
|
2016-11-10 17:48:29 +08:00
|
|
|
|
2016-11-25 16:05:41 +08:00
|
|
|
if (HasDynSymTab) {
|
|
|
|
In<ELFT>::DynSymTab = make<SymbolTableSection<ELFT>>(*In<ELFT>::DynStrTab);
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::DynSymTab);
|
|
|
|
|
|
|
|
In<ELFT>::VerSym = make<VersionTableSection<ELFT>>();
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::VerSym);
|
|
|
|
|
|
|
|
if (!Config->VersionDefinitions.empty()) {
|
|
|
|
In<ELFT>::VerDef = make<VersionDefinitionSection<ELFT>>();
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::VerDef);
|
|
|
|
}
|
|
|
|
|
|
|
|
In<ELFT>::VerNeed = make<VersionNeedSection<ELFT>>();
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::VerNeed);
|
|
|
|
|
|
|
|
if (Config->GnuHash) {
|
|
|
|
In<ELFT>::GnuHashTab = make<GnuHashTableSection<ELFT>>();
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::GnuHashTab);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config->SysvHash) {
|
|
|
|
In<ELFT>::HashTab = make<HashTableSection<ELFT>>();
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::HashTab);
|
|
|
|
}
|
|
|
|
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::Dynamic);
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::DynStrTab);
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::RelaDyn);
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:28:39 +08:00
|
|
|
// Add .got. MIPS' .got is so different from the other archs,
|
|
|
|
// it has its own class.
|
2016-11-25 16:05:41 +08:00
|
|
|
if (Config->EMachine == EM_MIPS) {
|
2016-11-17 05:01:02 +08:00
|
|
|
In<ELFT>::MipsGot = make<MipsGotSection<ELFT>>();
|
2016-11-25 16:05:41 +08:00
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::MipsGot);
|
|
|
|
} else {
|
2016-11-17 05:01:02 +08:00
|
|
|
In<ELFT>::Got = make<GotSection<ELFT>>();
|
2016-11-25 16:05:41 +08:00
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::Got);
|
|
|
|
}
|
2016-11-17 05:01:02 +08:00
|
|
|
|
2016-11-10 17:48:29 +08:00
|
|
|
In<ELFT>::GotPlt = make<GotPltSection<ELFT>>();
|
2016-11-25 16:05:41 +08:00
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::GotPlt);
|
2016-12-08 20:58:55 +08:00
|
|
|
In<ELFT>::IgotPlt = make<IgotPltSection<ELFT>>();
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::IgotPlt);
|
2016-11-30 00:05:27 +08:00
|
|
|
|
|
|
|
if (Config->GdbIndex) {
|
|
|
|
In<ELFT>::GdbIndex = make<GdbIndexSection<ELFT>>();
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::GdbIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We always need to add rel[a].plt to output if it has entries.
|
|
|
|
// Even for static linking it can contain R_[*]_IRELATIVE relocations.
|
|
|
|
In<ELFT>::RelaPlt = make<RelocationSection<ELFT>>(
|
|
|
|
Config->Rela ? ".rela.plt" : ".rel.plt", false /*Sort*/);
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::RelaPlt);
|
|
|
|
|
2016-12-08 20:58:55 +08:00
|
|
|
// The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
|
|
|
|
// that the IRelative relocations are processed last by the dynamic loader
|
|
|
|
In<ELFT>::RelaIplt = make<RelocationSection<ELFT>>(
|
|
|
|
(Config->EMachine == EM_ARM) ? ".rel.dyn" : In<ELFT>::RelaPlt->Name,
|
|
|
|
false /*Sort*/);
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::RelaIplt);
|
|
|
|
|
2016-11-30 00:05:27 +08:00
|
|
|
In<ELFT>::Plt = make<PltSection<ELFT>>();
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::Plt);
|
2016-12-08 20:58:55 +08:00
|
|
|
In<ELFT>::Iplt = make<IpltSection<ELFT>>();
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::Iplt);
|
2016-11-30 00:05:27 +08:00
|
|
|
|
|
|
|
if (Config->EhFrameHdr) {
|
|
|
|
In<ELFT>::EhFrameHdr = make<EhFrameHeader<ELFT>>();
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::EhFrameHdr);
|
|
|
|
}
|
2017-01-14 00:18:15 +08:00
|
|
|
|
|
|
|
if (In<ELFT>::SymTab)
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::SymTab);
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::ShStrTab);
|
|
|
|
if (In<ELFT>::StrTab)
|
|
|
|
Symtab<ELFT>::X->Sections.push_back(In<ELFT>::StrTab);
|
2016-11-02 07:17:45 +08:00
|
|
|
}
|
|
|
|
|
2016-01-28 02:04:26 +08:00
|
|
|
template <class ELFT>
|
2016-04-04 22:04:16 +08:00
|
|
|
static bool shouldKeepInSymtab(InputSectionBase<ELFT> *Sec, StringRef SymName,
|
|
|
|
const SymbolBody &B) {
|
|
|
|
if (B.isFile())
|
2016-01-28 02:04:26 +08:00
|
|
|
return false;
|
|
|
|
|
2016-03-03 15:49:35 +08:00
|
|
|
// We keep sections in symtab for relocatable output.
|
2016-04-04 22:04:16 +08:00
|
|
|
if (B.isSection())
|
2016-03-03 15:49:35 +08:00
|
|
|
return Config->Relocatable;
|
|
|
|
|
2016-01-28 02:04:26 +08:00
|
|
|
// If sym references a section in a discarded group, don't keep it.
|
2016-04-04 22:04:16 +08:00
|
|
|
if (Sec == &InputSection<ELFT>::Discarded)
|
2016-01-28 02:04:26 +08:00
|
|
|
return false;
|
|
|
|
|
2016-08-31 16:46:30 +08:00
|
|
|
if (Config->Discard == DiscardPolicy::None)
|
2016-01-28 02:04:26 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// In ELF assembly .L symbols are normally discarded by the assembler.
|
|
|
|
// If the assembler fails to do so, the linker discards them if
|
|
|
|
// * --discard-locals is used.
|
|
|
|
// * The symbol is in a SHF_MERGE section, which is normally the reason for
|
|
|
|
// the assembler keeping the .L symbol.
|
|
|
|
if (!SymName.startswith(".L") && !SymName.empty())
|
|
|
|
return true;
|
|
|
|
|
2016-08-31 16:46:30 +08:00
|
|
|
if (Config->Discard == DiscardPolicy::Locals)
|
2016-01-28 02:04:26 +08:00
|
|
|
return false;
|
|
|
|
|
2016-10-26 20:36:56 +08:00
|
|
|
return !Sec || !(Sec->Flags & SHF_MERGE);
|
2016-01-28 02:04:26 +08:00
|
|
|
}
|
|
|
|
|
2016-05-06 00:40:28 +08:00
|
|
|
template <class ELFT> static bool includeInSymtab(const SymbolBody &B) {
|
|
|
|
if (!B.isLocal() && !B.symbol()->IsUsedInRegularObj)
|
|
|
|
return false;
|
|
|
|
|
2016-12-20 02:00:52 +08:00
|
|
|
// If --retain-symbols-file is given, we'll keep only symbols listed in that
|
|
|
|
// file.
|
|
|
|
if (Config->Discard == DiscardPolicy::RetainFile &&
|
|
|
|
!Config->RetainSymbolsFile.count(B.getName()))
|
|
|
|
return false;
|
|
|
|
|
2016-05-06 00:40:28 +08:00
|
|
|
if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B)) {
|
|
|
|
// Always include absolute symbols.
|
|
|
|
if (!D->Section)
|
|
|
|
return true;
|
|
|
|
// Exclude symbols pointing to garbage-collected sections.
|
|
|
|
if (!D->Section->Live)
|
|
|
|
return false;
|
|
|
|
if (auto *S = dyn_cast<MergeInputSection<ELFT>>(D->Section))
|
2016-05-22 08:41:38 +08:00
|
|
|
if (!S->getSectionPiece(D->Value)->Live)
|
2016-05-06 00:40:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-06 00:38:46 +08:00
|
|
|
|
2015-10-09 07:49:30 +08:00
|
|
|
// Local symbols are not in the linker's symbol table. This function scans
|
|
|
|
// each object file's symbol table to copy local symbols to the output.
|
|
|
|
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
|
2016-11-17 17:16:34 +08:00
|
|
|
if (!In<ELFT>::SymTab)
|
2016-01-21 11:07:38 +08:00
|
|
|
return;
|
2016-09-14 08:05:51 +08:00
|
|
|
for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
|
2016-03-11 20:06:30 +08:00
|
|
|
for (SymbolBody *B : F->getLocalSymbols()) {
|
2016-10-11 17:07:14 +08:00
|
|
|
if (!B->IsLocal)
|
2016-11-24 02:07:33 +08:00
|
|
|
fatal(toString(F) +
|
2016-10-11 17:07:14 +08:00
|
|
|
": broken object: getLocalSymbols returns a non-local symbol");
|
2016-04-04 22:04:16 +08:00
|
|
|
auto *DR = dyn_cast<DefinedRegular<ELFT>>(B);
|
2016-11-24 02:07:33 +08:00
|
|
|
|
2016-04-04 22:04:16 +08:00
|
|
|
// No reason to keep local undefined symbol in symtab.
|
|
|
|
if (!DR)
|
|
|
|
continue;
|
2016-05-06 00:38:46 +08:00
|
|
|
if (!includeInSymtab<ELFT>(*B))
|
|
|
|
continue;
|
2016-11-24 02:07:33 +08:00
|
|
|
|
2016-04-04 22:04:16 +08:00
|
|
|
InputSectionBase<ELFT> *Sec = DR->Section;
|
2016-11-23 12:57:25 +08:00
|
|
|
if (!shouldKeepInSymtab<ELFT>(Sec, B->getName(), *B))
|
2015-10-10 03:25:07 +08:00
|
|
|
continue;
|
2016-11-17 17:16:34 +08:00
|
|
|
++In<ELFT>::SymTab->NumLocals;
|
2016-03-03 15:49:35 +08:00
|
|
|
if (Config->Relocatable)
|
2016-11-17 17:16:34 +08:00
|
|
|
B->DynsymIndex = In<ELFT>::SymTab->NumLocals;
|
2016-11-23 12:57:25 +08:00
|
|
|
F->KeptLocalSyms.push_back(std::make_pair(
|
|
|
|
DR, In<ELFT>::SymTab->StrTabSec.addString(B->getName())));
|
2015-10-09 07:49:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 04:51:48 +08:00
|
|
|
// PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
|
|
|
|
// we would like to make sure appear is a specific order to maximize their
|
|
|
|
// coverage by a single signed 16-bit offset from the TOC base pointer.
|
|
|
|
// Conversely, the special .tocbss section should be first among all SHT_NOBITS
|
|
|
|
// sections. This will put it next to the loaded special PPC64 sections (and,
|
|
|
|
// thus, within reach of the TOC base pointer).
|
|
|
|
static int getPPC64SectionRank(StringRef SectionName) {
|
|
|
|
return StringSwitch<int>(SectionName)
|
2016-04-14 21:23:02 +08:00
|
|
|
.Case(".tocbss", 0)
|
|
|
|
.Case(".branch_lt", 2)
|
|
|
|
.Case(".toc", 3)
|
|
|
|
.Case(".toc1", 4)
|
|
|
|
.Case(".opd", 5)
|
|
|
|
.Default(1);
|
2015-10-13 04:51:48 +08:00
|
|
|
}
|
|
|
|
|
2017-01-17 05:17:23 +08:00
|
|
|
// All sections with SHF_MIPS_GPREL flag should be grouped together
|
|
|
|
// because data in these sections is addressable with a gp relative address.
|
|
|
|
static int getMipsSectionRank(const OutputSectionBase *S) {
|
|
|
|
if ((S->Flags & SHF_MIPS_GPREL) == 0)
|
|
|
|
return 0;
|
|
|
|
if (S->getName() == ".got")
|
|
|
|
return 1;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
template <class ELFT> bool elf::isRelroSection(const OutputSectionBase *Sec) {
|
2016-02-11 06:43:13 +08:00
|
|
|
if (!Config->ZRelro)
|
|
|
|
return false;
|
2016-11-10 07:23:45 +08:00
|
|
|
uint64_t Flags = Sec->Flags;
|
2015-11-24 18:15:50 +08:00
|
|
|
if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
|
|
|
|
return false;
|
2015-12-11 03:13:08 +08:00
|
|
|
if (Flags & SHF_TLS)
|
|
|
|
return true;
|
2016-11-09 09:42:41 +08:00
|
|
|
uint32_t Type = Sec->Type;
|
2015-12-11 03:13:08 +08:00
|
|
|
if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
|
|
|
|
Type == SHT_PREINIT_ARRAY)
|
2015-11-24 18:15:50 +08:00
|
|
|
return true;
|
2016-11-10 17:48:29 +08:00
|
|
|
if (Sec == In<ELFT>::GotPlt->OutSec)
|
2015-11-24 18:15:50 +08:00
|
|
|
return Config->ZNow;
|
2016-11-17 05:01:02 +08:00
|
|
|
if (Sec == In<ELFT>::Dynamic->OutSec)
|
|
|
|
return true;
|
|
|
|
if (In<ELFT>::Got && Sec == In<ELFT>::Got->OutSec)
|
|
|
|
return true;
|
2017-01-10 09:21:50 +08:00
|
|
|
if (Sec == Out<ELFT>::BssRelRo)
|
|
|
|
return true;
|
2015-12-11 03:19:04 +08:00
|
|
|
StringRef S = Sec->getName();
|
|
|
|
return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
|
2016-12-20 19:28:54 +08:00
|
|
|
S == ".eh_frame" || S == ".openbsd.randomdata";
|
2015-11-24 18:15:50 +08:00
|
|
|
}
|
|
|
|
|
2015-10-09 07:49:30 +08:00
|
|
|
template <class ELFT>
|
2016-11-10 07:23:45 +08:00
|
|
|
static bool compareSectionsNonScript(const OutputSectionBase *A,
|
|
|
|
const OutputSectionBase *B) {
|
2016-11-03 02:58:44 +08:00
|
|
|
// Put .interp first because some loaders want to see that section
|
|
|
|
// on the first page of the executable file when loaded into memory.
|
|
|
|
bool AIsInterp = A->getName() == ".interp";
|
|
|
|
bool BIsInterp = B->getName() == ".interp";
|
|
|
|
if (AIsInterp != BIsInterp)
|
|
|
|
return AIsInterp;
|
|
|
|
|
2015-10-09 07:49:30 +08:00
|
|
|
// Allocatable sections go first to reduce the total PT_LOAD size and
|
|
|
|
// so debug info doesn't change addresses in actual code.
|
2016-11-09 09:42:41 +08:00
|
|
|
bool AIsAlloc = A->Flags & SHF_ALLOC;
|
|
|
|
bool BIsAlloc = B->Flags & SHF_ALLOC;
|
2015-10-09 07:49:30 +08:00
|
|
|
if (AIsAlloc != BIsAlloc)
|
|
|
|
return AIsAlloc;
|
|
|
|
|
2016-09-21 06:43:15 +08:00
|
|
|
// We don't have any special requirements for the relative order of two non
|
|
|
|
// allocatable sections.
|
2015-10-09 07:49:30 +08:00
|
|
|
if (!AIsAlloc)
|
2016-09-22 06:36:19 +08:00
|
|
|
return false;
|
2015-10-09 07:49:30 +08:00
|
|
|
|
2016-12-20 05:21:07 +08:00
|
|
|
// We want to put section specified by -T option first, so we
|
|
|
|
// can start assigning VA starting from them later.
|
|
|
|
auto AAddrSetI = Config->SectionStartMap.find(A->getName());
|
|
|
|
auto BAddrSetI = Config->SectionStartMap.find(B->getName());
|
|
|
|
bool AHasAddrSet = AAddrSetI != Config->SectionStartMap.end();
|
|
|
|
bool BHasAddrSet = BAddrSetI != Config->SectionStartMap.end();
|
|
|
|
if (AHasAddrSet != BHasAddrSet)
|
|
|
|
return AHasAddrSet;
|
|
|
|
if (AHasAddrSet)
|
|
|
|
return AAddrSetI->second < BAddrSetI->second;
|
|
|
|
|
2015-10-09 07:49:30 +08:00
|
|
|
// We want the read only sections first so that they go in the PT_LOAD
|
|
|
|
// covering the program headers at the start of the file.
|
2016-11-09 09:42:41 +08:00
|
|
|
bool AIsWritable = A->Flags & SHF_WRITE;
|
|
|
|
bool BIsWritable = B->Flags & SHF_WRITE;
|
2015-10-09 07:49:30 +08:00
|
|
|
if (AIsWritable != BIsWritable)
|
|
|
|
return BIsWritable;
|
|
|
|
|
2016-11-28 18:26:21 +08:00
|
|
|
if (!Config->SingleRoRx) {
|
2016-09-30 06:48:55 +08:00
|
|
|
// For a corresponding reason, put non exec sections first (the program
|
|
|
|
// header PT_LOAD is not executable).
|
|
|
|
// We only do that if we are not using linker scripts, since with linker
|
|
|
|
// scripts ro and rx sections are in the same PT_LOAD, so their relative
|
2016-11-28 18:26:21 +08:00
|
|
|
// order is not important. The same applies for -no-rosegment.
|
2016-11-09 09:42:41 +08:00
|
|
|
bool AIsExec = A->Flags & SHF_EXECINSTR;
|
|
|
|
bool BIsExec = B->Flags & SHF_EXECINSTR;
|
2016-09-30 06:48:55 +08:00
|
|
|
if (AIsExec != BIsExec)
|
|
|
|
return BIsExec;
|
|
|
|
}
|
2015-10-09 07:49:30 +08:00
|
|
|
|
2015-10-14 01:57:46 +08:00
|
|
|
// If we got here we know that both A and B are in the same PT_LOAD.
|
2015-10-17 07:11:07 +08:00
|
|
|
|
2016-11-09 09:42:41 +08:00
|
|
|
bool AIsTls = A->Flags & SHF_TLS;
|
|
|
|
bool BIsTls = B->Flags & SHF_TLS;
|
|
|
|
bool AIsNoBits = A->Type == SHT_NOBITS;
|
|
|
|
bool BIsNoBits = B->Type == SHT_NOBITS;
|
2015-10-13 04:51:48 +08:00
|
|
|
|
2017-01-10 09:21:30 +08:00
|
|
|
// The first requirement we have is to put (non-TLS) nobits sections last. The
|
|
|
|
// reason is that the only thing the dynamic linker will see about them is a
|
|
|
|
// p_memsz that is larger than p_filesz. Seeing that it zeros the end of the
|
|
|
|
// PT_LOAD, so that has to correspond to the nobits sections.
|
|
|
|
bool AIsNonTlsNoBits = AIsNoBits && !AIsTls;
|
|
|
|
bool BIsNonTlsNoBits = BIsNoBits && !BIsTls;
|
|
|
|
if (AIsNonTlsNoBits != BIsNonTlsNoBits)
|
|
|
|
return BIsNonTlsNoBits;
|
|
|
|
|
|
|
|
// We place nobits RelRo sections before plain r/w ones, and non-nobits RelRo
|
|
|
|
// sections after r/w ones, so that the RelRo sections are contiguous.
|
2016-11-10 07:23:45 +08:00
|
|
|
bool AIsRelRo = isRelroSection<ELFT>(A);
|
|
|
|
bool BIsRelRo = isRelroSection<ELFT>(B);
|
2015-11-24 18:15:50 +08:00
|
|
|
if (AIsRelRo != BIsRelRo)
|
2017-01-10 09:21:30 +08:00
|
|
|
return AIsNonTlsNoBits ? AIsRelRo : BIsRelRo;
|
|
|
|
|
|
|
|
// The TLS initialization block needs to be a single contiguous block in a R/W
|
|
|
|
// PT_LOAD, so stick TLS sections directly before the other RelRo R/W
|
|
|
|
// sections. The TLS NOBITS sections are placed here as they don't take up
|
|
|
|
// virtual address space in the PT_LOAD.
|
|
|
|
if (AIsTls != BIsTls)
|
|
|
|
return AIsTls;
|
|
|
|
|
|
|
|
// Within the TLS initialization block, the non-nobits sections need to appear
|
|
|
|
// first.
|
|
|
|
if (AIsNoBits != BIsNoBits)
|
|
|
|
return BIsNoBits;
|
2015-11-24 18:15:50 +08:00
|
|
|
|
2015-10-14 03:07:29 +08:00
|
|
|
// Some architectures have additional ordering restrictions for sections
|
|
|
|
// within the same PT_LOAD.
|
|
|
|
if (Config->EMachine == EM_PPC64)
|
|
|
|
return getPPC64SectionRank(A->getName()) <
|
|
|
|
getPPC64SectionRank(B->getName());
|
2017-01-17 05:17:23 +08:00
|
|
|
if (Config->EMachine == EM_MIPS)
|
|
|
|
return getMipsSectionRank(A) < getMipsSectionRank(B);
|
2015-10-14 03:07:29 +08:00
|
|
|
|
2016-09-22 06:36:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output section ordering is determined by this function.
|
|
|
|
template <class ELFT>
|
2016-11-10 07:23:45 +08:00
|
|
|
static bool compareSections(const OutputSectionBase *A,
|
|
|
|
const OutputSectionBase *B) {
|
2016-09-22 06:36:19 +08:00
|
|
|
// For now, put sections mentioned in a linker script first.
|
|
|
|
int AIndex = Script<ELFT>::X->getSectionIndex(A->getName());
|
|
|
|
int BIndex = Script<ELFT>::X->getSectionIndex(B->getName());
|
|
|
|
bool AInScript = AIndex != INT_MAX;
|
|
|
|
bool BInScript = BIndex != INT_MAX;
|
|
|
|
if (AInScript != BInScript)
|
|
|
|
return AInScript;
|
|
|
|
// If both are in the script, use that order.
|
|
|
|
if (AInScript)
|
|
|
|
return AIndex < BIndex;
|
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
return compareSectionsNonScript<ELFT>(A, B);
|
2015-10-09 07:49:30 +08:00
|
|
|
}
|
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
// Program header entry
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry::PhdrEntry(unsigned Type, unsigned Flags) {
|
|
|
|
p_type = Type;
|
|
|
|
p_flags = Flags;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2016-12-20 01:01:01 +08:00
|
|
|
void PhdrEntry::add(OutputSectionBase *Sec) {
|
2016-07-19 17:25:43 +08:00
|
|
|
Last = Sec;
|
|
|
|
if (!First)
|
|
|
|
First = Sec;
|
2016-12-20 01:01:01 +08:00
|
|
|
p_align = std::max(p_align, Sec->Addralign);
|
|
|
|
if (p_type == PT_LOAD)
|
2016-09-29 17:20:33 +08:00
|
|
|
Sec->FirstInPtLoad = First;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2016-04-12 21:26:51 +08:00
|
|
|
template <class ELFT>
|
2017-01-04 10:43:01 +08:00
|
|
|
static void addOptionalSynthetic(StringRef Name, OutputSectionBase *Sec,
|
|
|
|
typename ELFT::uint Val,
|
|
|
|
uint8_t StOther = STV_HIDDEN) {
|
|
|
|
if (SymbolBody *S = Symtab<ELFT>::X->find(Name))
|
2017-01-18 00:08:06 +08:00
|
|
|
if (!S->isInCurrentDSO())
|
2017-01-04 10:43:01 +08:00
|
|
|
Symtab<ELFT>::X->addSynthetic(Name, Sec, Val, StOther);
|
2016-04-12 21:26:51 +08:00
|
|
|
}
|
|
|
|
|
2016-11-11 19:33:32 +08:00
|
|
|
template <class ELFT>
|
2016-11-23 14:59:47 +08:00
|
|
|
static Symbol *addRegular(StringRef Name, InputSectionBase<ELFT> *Sec,
|
2016-11-11 19:33:32 +08:00
|
|
|
typename ELFT::uint Value) {
|
2016-11-18 05:20:16 +08:00
|
|
|
// The linker generated symbols are added as STB_WEAK to allow user defined
|
|
|
|
// ones to override them.
|
2016-11-23 14:59:47 +08:00
|
|
|
return Symtab<ELFT>::X->addRegular(Name, STV_HIDDEN, STT_NOTYPE, Value,
|
|
|
|
/*Size=*/0, STB_WEAK, Sec,
|
|
|
|
/*File=*/nullptr);
|
2016-11-11 19:33:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
static Symbol *addOptionalRegular(StringRef Name, InputSectionBase<ELFT> *IS,
|
|
|
|
typename ELFT::uint Value) {
|
|
|
|
SymbolBody *S = Symtab<ELFT>::X->find(Name);
|
|
|
|
if (!S)
|
|
|
|
return nullptr;
|
2017-01-18 00:08:06 +08:00
|
|
|
if (S->isInCurrentDSO())
|
2016-11-11 19:33:32 +08:00
|
|
|
return S->symbol();
|
|
|
|
return addRegular(Name, IS, Value);
|
|
|
|
}
|
|
|
|
|
2015-12-26 17:47:57 +08:00
|
|
|
// The beginning and the ending of .rel[a].plt section are marked
|
|
|
|
// with __rel[a]_iplt_{start,end} symbols if it is a statically linked
|
|
|
|
// executable. The runtime needs these symbols in order to resolve
|
|
|
|
// all IRELATIVE relocs on startup. For dynamic executables, we don't
|
|
|
|
// need these symbols, since IRELATIVE relocs are resolved through GOT
|
|
|
|
// and PLT. For details, see http://www.airs.com/blog/archives/403.
|
2016-04-14 21:23:02 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
|
2016-11-17 17:16:34 +08:00
|
|
|
if (In<ELFT>::DynSymTab)
|
2015-12-21 18:12:06 +08:00
|
|
|
return;
|
2016-03-14 04:10:20 +08:00
|
|
|
StringRef S = Config->Rela ? "__rela_iplt_start" : "__rel_iplt_start";
|
2016-12-08 20:58:55 +08:00
|
|
|
addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, 0);
|
2015-12-26 17:47:57 +08:00
|
|
|
|
2016-03-14 04:10:20 +08:00
|
|
|
S = Config->Rela ? "__rela_iplt_end" : "__rel_iplt_end";
|
2016-12-08 20:58:55 +08:00
|
|
|
addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, -1);
|
2015-12-21 18:12:06 +08:00
|
|
|
}
|
|
|
|
|
2015-12-26 15:50:39 +08:00
|
|
|
// The linker is expected to define some symbols depending on
|
|
|
|
// the linking result. This function defines such symbols.
|
|
|
|
template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
|
2016-11-15 16:19:02 +08:00
|
|
|
if (Config->EMachine == EM_MIPS) {
|
2016-04-12 10:24:43 +08:00
|
|
|
// Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
|
2016-12-08 14:19:47 +08:00
|
|
|
// so that it points to an absolute address which by default is relative
|
|
|
|
// to GOT. Default offset is 0x7ff0.
|
2016-04-12 10:24:43 +08:00
|
|
|
// See "Global Data Symbols" in Chapter 6 in the following document:
|
|
|
|
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
|
2016-12-08 14:19:47 +08:00
|
|
|
ElfSym<ELFT>::MipsGp =
|
|
|
|
Symtab<ELFT>::X->addAbsolute("_gp", STV_HIDDEN, STB_LOCAL);
|
2016-04-12 10:24:43 +08:00
|
|
|
|
2016-04-04 22:04:16 +08:00
|
|
|
// On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
|
2016-12-08 14:19:47 +08:00
|
|
|
// start of function and 'gp' pointer into GOT. To simplify relocation
|
|
|
|
// calculation we assign _gp value to it and calculate corresponding
|
|
|
|
// relocations as relative to this value.
|
|
|
|
if (Symtab<ELFT>::X->find("_gp_disp"))
|
|
|
|
ElfSym<ELFT>::MipsGpDisp =
|
|
|
|
Symtab<ELFT>::X->addAbsolute("_gp_disp", STV_HIDDEN, STB_LOCAL);
|
2016-05-04 02:03:45 +08:00
|
|
|
|
2016-04-04 22:04:16 +08:00
|
|
|
// The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
|
|
|
|
// pointer. This symbol is used in the code generated by .cpload pseudo-op
|
|
|
|
// in case of using -mno-shared option.
|
|
|
|
// https://sourceware.org/ml/binutils/2004-12/msg00094.html
|
2016-12-08 14:19:47 +08:00
|
|
|
if (Symtab<ELFT>::X->find("__gnu_local_gp"))
|
|
|
|
ElfSym<ELFT>::MipsLocalGp =
|
|
|
|
Symtab<ELFT>::X->addAbsolute("__gnu_local_gp", STV_HIDDEN, STB_LOCAL);
|
2016-04-04 22:04:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
|
|
|
|
// is magical and is used to produce a R_386_GOTPC relocation.
|
|
|
|
// The R_386_GOTPC relocation value doesn't actually depend on the
|
|
|
|
// symbol value, so it could use an index of STN_UNDEF which, according
|
|
|
|
// to the spec, means the symbol value is 0.
|
|
|
|
// Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
|
|
|
|
// the object file.
|
|
|
|
// The situation is even stranger on x86_64 where the assembly doesn't
|
|
|
|
// need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
|
|
|
|
// an undefined symbol in the .o files.
|
|
|
|
// Given that the symbol is effectively unused, we just create a dummy
|
|
|
|
// hidden one to avoid the undefined symbol error.
|
2016-11-15 16:19:02 +08:00
|
|
|
Symtab<ELFT>::X->addIgnored("_GLOBAL_OFFSET_TABLE_");
|
2016-04-04 22:04:16 +08:00
|
|
|
|
2015-12-26 15:50:39 +08:00
|
|
|
// __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
|
|
|
|
// static linking the linker is required to optimize away any references to
|
|
|
|
// __tls_get_addr, so it's not defined anywhere. Create a hidden definition
|
2016-10-27 18:28:53 +08:00
|
|
|
// to avoid the undefined symbol error. As usual special cases are ARM and
|
|
|
|
// MIPS - the libc for these targets defines __tls_get_addr itself because
|
|
|
|
// there are no TLS optimizations for these targets.
|
2016-11-17 17:16:34 +08:00
|
|
|
if (!In<ELFT>::DynSymTab &&
|
2016-10-27 18:28:53 +08:00
|
|
|
(Config->EMachine != EM_MIPS && Config->EMachine != EM_ARM))
|
2016-08-09 11:38:23 +08:00
|
|
|
Symtab<ELFT>::X->addIgnored("__tls_get_addr");
|
2015-12-26 15:50:39 +08:00
|
|
|
|
2016-08-08 16:42:48 +08:00
|
|
|
// If linker script do layout we do not need to create any standart symbols.
|
2016-09-14 16:32:36 +08:00
|
|
|
if (ScriptConfig->HasSections)
|
2016-08-08 16:42:48 +08:00
|
|
|
return;
|
|
|
|
|
2016-08-23 02:44:04 +08:00
|
|
|
ElfSym<ELFT>::EhdrStart = Symtab<ELFT>::X->addIgnored("__ehdr_start");
|
|
|
|
|
2016-04-22 04:50:15 +08:00
|
|
|
auto Define = [this](StringRef S, DefinedRegular<ELFT> *&Sym1,
|
|
|
|
DefinedRegular<ELFT> *&Sym2) {
|
2016-08-09 11:38:23 +08:00
|
|
|
Sym1 = Symtab<ELFT>::X->addIgnored(S, STV_DEFAULT);
|
2016-02-27 00:38:39 +08:00
|
|
|
|
|
|
|
// The name without the underscore is not a reserved name,
|
|
|
|
// so it is defined only when there is a reference against it.
|
2016-02-27 00:49:54 +08:00
|
|
|
assert(S.startswith("_"));
|
2016-02-27 00:38:39 +08:00
|
|
|
S = S.substr(1);
|
2016-08-09 11:38:23 +08:00
|
|
|
if (SymbolBody *B = Symtab<ELFT>::X->find(S))
|
2016-02-26 22:36:36 +08:00
|
|
|
if (B->isUndefined())
|
2016-08-09 11:38:23 +08:00
|
|
|
Sym2 = Symtab<ELFT>::X->addAbsolute(S, STV_DEFAULT);
|
2016-02-26 22:36:36 +08:00
|
|
|
};
|
|
|
|
|
2016-04-22 04:50:15 +08:00
|
|
|
Define("_end", ElfSym<ELFT>::End, ElfSym<ELFT>::End2);
|
|
|
|
Define("_etext", ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2);
|
|
|
|
Define("_edata", ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2);
|
2015-12-26 15:50:39 +08:00
|
|
|
}
|
|
|
|
|
2016-02-11 07:20:42 +08:00
|
|
|
// Sort input sections by section name suffixes for
|
|
|
|
// __attribute__((init_priority(N))).
|
2016-11-10 07:23:45 +08:00
|
|
|
template <class ELFT> static void sortInitFini(OutputSectionBase *S) {
|
2016-02-11 07:20:42 +08:00
|
|
|
if (S)
|
2016-02-12 07:41:38 +08:00
|
|
|
reinterpret_cast<OutputSection<ELFT> *>(S)->sortInitFini();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort input sections by the special rule for .ctors and .dtors.
|
2016-11-10 07:23:45 +08:00
|
|
|
template <class ELFT> static void sortCtorsDtors(OutputSectionBase *S) {
|
2016-02-12 07:41:38 +08:00
|
|
|
if (S)
|
|
|
|
reinterpret_cast<OutputSection<ELFT> *>(S)->sortCtorsDtors();
|
2016-02-11 07:20:42 +08:00
|
|
|
}
|
|
|
|
|
2016-11-10 17:05:20 +08:00
|
|
|
// Sort input sections using the list provided by --symbol-ordering-file.
|
|
|
|
template <class ELFT>
|
2016-12-20 09:51:08 +08:00
|
|
|
static void sortBySymbolsOrder(ArrayRef<OutputSectionBase *> OutputSections) {
|
2016-11-10 17:05:20 +08:00
|
|
|
if (Config->SymbolOrderingFile.empty())
|
|
|
|
return;
|
|
|
|
|
2016-12-20 09:51:08 +08:00
|
|
|
// Build a map from symbols to their priorities. Symbols that didn't
|
|
|
|
// appear in the symbol ordering file have the lowest priority 0.
|
|
|
|
// All explicitly mentioned symbols have negative (higher) priorities.
|
|
|
|
DenseMap<StringRef, int> SymbolOrder;
|
|
|
|
int Priority = -Config->SymbolOrderingFile.size();
|
|
|
|
for (StringRef S : Config->SymbolOrderingFile)
|
|
|
|
SymbolOrder.insert({S, Priority++});
|
|
|
|
|
|
|
|
// Build a map from sections to their priorities.
|
|
|
|
DenseMap<InputSectionBase<ELFT> *, int> SectionOrder;
|
2016-11-10 17:05:20 +08:00
|
|
|
for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
|
|
|
|
for (SymbolBody *Body : File->getSymbols()) {
|
|
|
|
auto *D = dyn_cast<DefinedRegular<ELFT>>(Body);
|
|
|
|
if (!D || !D->Section)
|
|
|
|
continue;
|
2016-12-20 09:51:08 +08:00
|
|
|
int &Priority = SectionOrder[D->Section];
|
|
|
|
Priority = std::min(Priority, SymbolOrder.lookup(D->getName()));
|
2016-11-10 17:05:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 09:51:08 +08:00
|
|
|
// Sort sections by priority.
|
|
|
|
for (OutputSectionBase *Base : OutputSections)
|
2016-12-19 11:14:16 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection<ELFT>>(Base))
|
2016-12-20 09:51:08 +08:00
|
|
|
Sec->sort([&](InputSection<ELFT> *S) { return SectionOrder.lookup(S); });
|
2016-11-10 17:05:20 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 01:58:07 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void Writer<ELFT>::forEachRelSec(
|
2016-11-10 22:53:24 +08:00
|
|
|
std::function<void(InputSectionBase<ELFT> &)> Fn) {
|
2016-11-06 06:37:59 +08:00
|
|
|
for (InputSectionBase<ELFT> *IS : Symtab<ELFT>::X->Sections) {
|
2016-11-09 02:23:02 +08:00
|
|
|
if (!IS->Live)
|
2016-11-06 06:37:59 +08:00
|
|
|
continue;
|
|
|
|
// Scan all relocations. Each relocation goes through a series
|
|
|
|
// of tests to determine if it needs special treatment, such as
|
|
|
|
// creating GOT, PLT, copy relocations, etc.
|
|
|
|
// Note that relocations for non-alloc sections are directly
|
|
|
|
// processed by InputSection::relocateNonAlloc.
|
|
|
|
if (!(IS->Flags & SHF_ALLOC))
|
|
|
|
continue;
|
2016-11-10 22:53:24 +08:00
|
|
|
if (isa<InputSection<ELFT>>(IS) || isa<EhInputSection<ELFT>>(IS))
|
|
|
|
Fn(*IS);
|
2016-07-21 01:58:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 17:49:24 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void Writer<ELFT>::addInputSec(InputSectionBase<ELFT> *IS) {
|
2016-11-14 17:16:00 +08:00
|
|
|
if (!IS)
|
|
|
|
return;
|
|
|
|
|
2016-11-09 02:23:02 +08:00
|
|
|
if (!IS->Live) {
|
2016-11-01 17:49:24 +08:00
|
|
|
reportDiscarded(IS);
|
|
|
|
return;
|
|
|
|
}
|
2016-11-10 07:23:45 +08:00
|
|
|
OutputSectionBase *Sec;
|
2016-11-01 17:49:24 +08:00
|
|
|
bool IsNew;
|
|
|
|
StringRef OutsecName = getOutputSectionName(IS->Name);
|
|
|
|
std::tie(Sec, IsNew) = Factory.create(IS, OutsecName);
|
|
|
|
if (IsNew)
|
|
|
|
OutputSections.push_back(Sec);
|
|
|
|
Sec->addSection(IS);
|
|
|
|
}
|
2016-10-28 01:45:40 +08:00
|
|
|
|
2016-11-01 17:49:24 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::createSections() {
|
2016-11-06 06:37:59 +08:00
|
|
|
for (InputSectionBase<ELFT> *IS : Symtab<ELFT>::X->Sections)
|
|
|
|
addInputSec(IS);
|
2016-08-11 15:56:43 +08:00
|
|
|
|
2016-11-10 17:05:20 +08:00
|
|
|
sortBySymbolsOrder<ELFT>(OutputSections);
|
2016-11-10 07:23:45 +08:00
|
|
|
sortInitFini<ELFT>(findSection(".init_array"));
|
|
|
|
sortInitFini<ELFT>(findSection(".fini_array"));
|
|
|
|
sortCtorsDtors<ELFT>(findSection(".ctors"));
|
|
|
|
sortCtorsDtors<ELFT>(findSection(".dtors"));
|
2016-08-11 15:56:43 +08:00
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections)
|
2016-08-11 15:56:43 +08:00
|
|
|
Sec->assignOffsets();
|
2016-07-20 22:43:20 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:44:48 +08:00
|
|
|
template <class ELFT>
|
2016-11-10 07:23:45 +08:00
|
|
|
static bool canSharePtLoad(const OutputSectionBase &S1,
|
|
|
|
const OutputSectionBase &S2) {
|
2016-11-09 09:42:41 +08:00
|
|
|
if (!(S1.Flags & SHF_ALLOC) || !(S2.Flags & SHF_ALLOC))
|
2016-11-08 18:44:48 +08:00
|
|
|
return false;
|
|
|
|
|
2016-11-09 09:42:41 +08:00
|
|
|
bool S1IsWrite = S1.Flags & SHF_WRITE;
|
|
|
|
bool S2IsWrite = S2.Flags & SHF_WRITE;
|
2016-11-08 18:44:48 +08:00
|
|
|
if (S1IsWrite != S2IsWrite)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!S1IsWrite)
|
|
|
|
return true; // RO and RX share a PT_LOAD with linker scripts.
|
2016-11-09 09:42:41 +08:00
|
|
|
return (S1.Flags & SHF_EXECINSTR) == (S2.Flags & SHF_EXECINSTR);
|
2016-11-08 18:44:48 +08:00
|
|
|
}
|
|
|
|
|
2016-09-22 06:36:19 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::sortSections() {
|
2016-11-12 06:43:27 +08:00
|
|
|
// Don't sort if using -r. It is not necessary and we want to preserve the
|
|
|
|
// relative order for SHF_LINK_ORDER sections.
|
|
|
|
if (Config->Relocatable)
|
|
|
|
return;
|
2016-09-22 06:36:19 +08:00
|
|
|
if (!ScriptConfig->HasSections) {
|
|
|
|
std::stable_sort(OutputSections.begin(), OutputSections.end(),
|
|
|
|
compareSectionsNonScript<ELFT>);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-22 22:40:50 +08:00
|
|
|
Script<ELFT>::X->adjustSectionsBeforeSorting();
|
2016-09-22 06:36:19 +08:00
|
|
|
|
|
|
|
// The order of the sections in the script is arbitrary and may not agree with
|
|
|
|
// compareSectionsNonScript. This means that we cannot easily define a
|
|
|
|
// strict weak ordering. To see why, consider a comparison of a section in the
|
|
|
|
// script and one not in the script. We have a two simple options:
|
|
|
|
// * Make them equivalent (a is not less than b, and b is not less than a).
|
|
|
|
// The problem is then that equivalence has to be transitive and we can
|
|
|
|
// have sections a, b and c with only b in a script and a less than c
|
|
|
|
// which breaks this property.
|
|
|
|
// * Use compareSectionsNonScript. Given that the script order doesn't have
|
|
|
|
// to match, we can end up with sections a, b, c, d where b and c are in the
|
|
|
|
// script and c is compareSectionsNonScript less than b. In which case d
|
|
|
|
// can be equivalent to c, a to b and d < a. As a concrete example:
|
|
|
|
// .a (rx) # not in script
|
|
|
|
// .b (rx) # in script
|
|
|
|
// .c (ro) # in script
|
|
|
|
// .d (ro) # not in script
|
|
|
|
//
|
|
|
|
// The way we define an order then is:
|
|
|
|
// * First put script sections at the start and sort the script and
|
|
|
|
// non-script sections independently.
|
2016-11-08 18:44:48 +08:00
|
|
|
// * Move each non-script section to its preferred position. We try
|
|
|
|
// to put each section in the last position where it it can share
|
|
|
|
// a PT_LOAD.
|
2016-09-22 06:36:19 +08:00
|
|
|
|
|
|
|
std::stable_sort(OutputSections.begin(), OutputSections.end(),
|
|
|
|
compareSections<ELFT>);
|
|
|
|
|
|
|
|
auto I = OutputSections.begin();
|
|
|
|
auto E = OutputSections.end();
|
2016-11-08 18:44:48 +08:00
|
|
|
auto NonScriptI =
|
2016-11-10 07:23:45 +08:00
|
|
|
std::find_if(OutputSections.begin(), E, [](OutputSectionBase *S) {
|
2016-11-08 18:44:48 +08:00
|
|
|
return Script<ELFT>::X->getSectionIndex(S->getName()) == INT_MAX;
|
|
|
|
});
|
2016-09-22 06:36:19 +08:00
|
|
|
while (NonScriptI != E) {
|
2016-11-10 07:23:45 +08:00
|
|
|
auto BestPos = std::max_element(
|
|
|
|
I, NonScriptI, [&](OutputSectionBase *&A, OutputSectionBase *&B) {
|
|
|
|
bool ACanSharePtLoad = canSharePtLoad<ELFT>(**NonScriptI, *A);
|
|
|
|
bool BCanSharePtLoad = canSharePtLoad<ELFT>(**NonScriptI, *B);
|
2016-11-08 18:44:48 +08:00
|
|
|
if (ACanSharePtLoad != BCanSharePtLoad)
|
|
|
|
return BCanSharePtLoad;
|
|
|
|
|
|
|
|
bool ACmp = compareSectionsNonScript<ELFT>(*NonScriptI, A);
|
|
|
|
bool BCmp = compareSectionsNonScript<ELFT>(*NonScriptI, B);
|
|
|
|
if (ACmp != BCmp)
|
|
|
|
return BCmp; // FIXME: missing test
|
|
|
|
|
|
|
|
size_t PosA = &A - &OutputSections[0];
|
|
|
|
size_t PosB = &B - &OutputSections[0];
|
|
|
|
return ACmp ? PosA > PosB : PosA < PosB;
|
2016-09-22 06:36:19 +08:00
|
|
|
});
|
2016-11-08 18:44:48 +08:00
|
|
|
|
|
|
|
// max_element only returns NonScriptI if the range is empty. If the range
|
|
|
|
// is not empty we should consider moving the the element forward one
|
|
|
|
// position.
|
|
|
|
if (BestPos != NonScriptI &&
|
|
|
|
!compareSectionsNonScript<ELFT>(*NonScriptI, *BestPos))
|
|
|
|
++BestPos;
|
|
|
|
std::rotate(BestPos, NonScriptI, NonScriptI + 1);
|
2016-09-22 06:36:19 +08:00
|
|
|
++NonScriptI;
|
|
|
|
}
|
2016-11-14 23:39:38 +08:00
|
|
|
|
|
|
|
Script<ELFT>::X->adjustSectionsAfterSorting();
|
2016-09-22 06:36:19 +08:00
|
|
|
}
|
|
|
|
|
2016-11-15 20:26:55 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static void
|
|
|
|
finalizeSynthetic(const std::vector<SyntheticSection<ELFT> *> &Sections) {
|
|
|
|
for (SyntheticSection<ELFT> *SS : Sections)
|
2016-11-30 00:05:27 +08:00
|
|
|
if (SS && SS->OutSec && !SS->empty()) {
|
2016-11-15 20:26:55 +08:00
|
|
|
SS->finalize();
|
|
|
|
SS->OutSec->Size = 0;
|
|
|
|
SS->OutSec->assignOffsets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 16:05:41 +08:00
|
|
|
// We need to add input synthetic sections early in createSyntheticSections()
|
2016-12-06 05:39:35 +08:00
|
|
|
// to make them visible from linkescript side. But not all sections are always
|
|
|
|
// required to be in output. For example we don't need dynamic section content
|
|
|
|
// sometimes. This function filters out such unused sections from output.
|
|
|
|
template <class ELFT>
|
|
|
|
static void removeUnusedSyntheticSections(std::vector<OutputSectionBase *> &V) {
|
2016-11-25 16:05:41 +08:00
|
|
|
// Input synthetic sections are placed after all regular ones. We iterate over
|
|
|
|
// them all and exit at first non-synthetic.
|
2016-12-06 05:39:35 +08:00
|
|
|
for (InputSectionBase<ELFT> *S : llvm::reverse(Symtab<ELFT>::X->Sections)) {
|
|
|
|
SyntheticSection<ELFT> *SS = dyn_cast<SyntheticSection<ELFT>>(S);
|
|
|
|
if (!SS)
|
2016-11-25 16:05:41 +08:00
|
|
|
return;
|
2016-12-06 05:39:35 +08:00
|
|
|
if (!SS->empty() || !SS->OutSec)
|
2016-11-25 16:05:41 +08:00
|
|
|
continue;
|
|
|
|
|
2016-12-06 05:39:35 +08:00
|
|
|
OutputSection<ELFT> *OutSec = cast<OutputSection<ELFT>>(SS->OutSec);
|
|
|
|
OutSec->Sections.erase(
|
|
|
|
std::find(OutSec->Sections.begin(), OutSec->Sections.end(), SS));
|
|
|
|
// If there is no other sections in output section, remove it from output.
|
|
|
|
if (OutSec->Sections.empty())
|
|
|
|
V.erase(std::find(V.begin(), V.end(), OutSec));
|
2016-11-25 16:05:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 22:43:20 +08:00
|
|
|
// Create output section objects and add them to OutputSections.
|
|
|
|
template <class ELFT> void Writer<ELFT>::finalizeSections() {
|
2016-10-20 17:19:48 +08:00
|
|
|
Out<ELFT>::DebugInfo = findSection(".debug_info");
|
2016-08-09 12:25:20 +08:00
|
|
|
Out<ELFT>::PreinitArray = findSection(".preinit_array");
|
|
|
|
Out<ELFT>::InitArray = findSection(".init_array");
|
|
|
|
Out<ELFT>::FiniArray = findSection(".fini_array");
|
2015-10-03 03:37:55 +08:00
|
|
|
|
2015-12-26 17:48:00 +08:00
|
|
|
// The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
|
|
|
|
// symbols for sections, so that the runtime can get the start and end
|
|
|
|
// addresses of each section by section name. Add such symbols.
|
2016-03-02 03:12:35 +08:00
|
|
|
if (!Config->Relocatable) {
|
|
|
|
addStartEndSymbols();
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections)
|
2016-03-02 03:12:35 +08:00
|
|
|
addStartStopSymbols(Sec);
|
|
|
|
}
|
2016-03-05 02:34:14 +08:00
|
|
|
|
|
|
|
// Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
|
|
|
|
// It should be okay as no one seems to care about the type.
|
|
|
|
// Even the author of gold doesn't remember why gold behaves that way.
|
|
|
|
// https://sourceware.org/ml/binutils/2002-03/msg00360.html
|
2016-11-17 17:16:34 +08:00
|
|
|
if (In<ELFT>::DynSymTab)
|
2016-11-15 20:26:55 +08:00
|
|
|
addRegular("_DYNAMIC", In<ELFT>::Dynamic, 0);
|
2015-10-19 23:21:42 +08:00
|
|
|
|
2016-02-05 05:33:05 +08:00
|
|
|
// Define __rel[a]_iplt_{start,end} symbols if needed.
|
|
|
|
addRelIpltSymbols();
|
|
|
|
|
2016-05-24 00:24:16 +08:00
|
|
|
if (!Out<ELFT>::EhFrame->empty()) {
|
|
|
|
OutputSections.push_back(Out<ELFT>::EhFrame);
|
|
|
|
Out<ELFT>::EhFrame->finalize();
|
|
|
|
}
|
2016-04-07 22:22:09 +08:00
|
|
|
|
2016-07-21 01:58:07 +08:00
|
|
|
// Scan relocations. This must be done after every symbol is declared so that
|
|
|
|
// we can correctly decide if a dynamic relocation is needed.
|
|
|
|
forEachRelSec(scanRelocations<ELFT>);
|
|
|
|
|
2015-12-26 18:22:16 +08:00
|
|
|
// Now that we have defined all possible symbols including linker-
|
|
|
|
// synthesized ones. Visit all symbols to give the finishing touches.
|
2016-08-09 11:38:23 +08:00
|
|
|
for (Symbol *S : Symtab<ELFT>::X->getSymbols()) {
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
SymbolBody *Body = S->body();
|
2016-04-26 21:56:26 +08:00
|
|
|
|
2015-10-05 23:24:04 +08:00
|
|
|
if (!includeInSymtab<ELFT>(*Body))
|
2015-09-23 07:38:23 +08:00
|
|
|
continue;
|
2016-11-17 17:16:34 +08:00
|
|
|
if (In<ELFT>::SymTab)
|
|
|
|
In<ELFT>::SymTab->addSymbol(Body);
|
2015-09-23 07:38:23 +08:00
|
|
|
|
2016-11-17 17:16:34 +08:00
|
|
|
if (In<ELFT>::DynSymTab && S->includeInDynsym()) {
|
|
|
|
In<ELFT>::DynSymTab->addSymbol(Body);
|
2016-04-28 04:22:31 +08:00
|
|
|
if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(Body))
|
2016-07-17 11:11:46 +08:00
|
|
|
if (SS->file()->isNeeded())
|
2016-11-22 00:59:33 +08:00
|
|
|
In<ELFT>::VerNeed->addSymbol(SS);
|
2016-04-28 04:22:31 +08:00
|
|
|
}
|
2015-09-23 07:38:23 +08:00
|
|
|
}
|
2016-01-29 06:56:29 +08:00
|
|
|
|
|
|
|
// Do not proceed if there was an undefined symbol.
|
2016-11-24 09:43:21 +08:00
|
|
|
if (ErrorCount)
|
2016-04-02 01:24:19 +08:00
|
|
|
return;
|
2016-01-29 06:56:29 +08:00
|
|
|
|
2015-12-26 15:50:41 +08:00
|
|
|
// So far we have added sections from input object files.
|
|
|
|
// This function adds linker-created Out<ELFT>::* sections.
|
|
|
|
addPredefinedSections();
|
2016-12-06 05:39:35 +08:00
|
|
|
removeUnusedSyntheticSections<ELFT>(OutputSections);
|
2015-12-26 15:50:41 +08:00
|
|
|
|
2016-09-22 06:36:19 +08:00
|
|
|
sortSections();
|
2015-12-26 15:50:41 +08:00
|
|
|
|
2016-04-06 15:20:45 +08:00
|
|
|
unsigned I = 1;
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections) {
|
2016-04-06 15:20:45 +08:00
|
|
|
Sec->SectionIndex = I++;
|
2016-11-14 17:16:00 +08:00
|
|
|
Sec->ShName = In<ELFT>::ShStrTab->addString(Sec->getName());
|
2016-04-06 15:20:45 +08:00
|
|
|
}
|
2015-12-26 15:50:41 +08:00
|
|
|
|
2016-12-20 05:21:07 +08:00
|
|
|
// Binary and relocatable output does not have PHDRS.
|
|
|
|
// The headers have to be created before finalize as that can influence the
|
|
|
|
// image base and the dynamic section on mips includes the image base.
|
|
|
|
if (!Config->Relocatable && !Config->OFormatBinary) {
|
|
|
|
Phdrs = Script<ELFT>::X->hasPhdrsCommands() ? Script<ELFT>::X->createPhdrs()
|
|
|
|
: createPhdrs();
|
|
|
|
addPtArmExid(Phdrs);
|
|
|
|
fixHeaders();
|
|
|
|
}
|
|
|
|
|
2016-02-02 17:07:47 +08:00
|
|
|
// Fill other section headers. The dynamic table is finalized
|
|
|
|
// at the end because some tags like RELSZ depend on result
|
2016-11-02 10:18:01 +08:00
|
|
|
// of finalizing other sections.
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections)
|
2016-11-15 20:26:55 +08:00
|
|
|
Sec->finalize();
|
2016-02-02 17:07:47 +08:00
|
|
|
|
2016-11-18 14:44:18 +08:00
|
|
|
// Dynamic section must be the last one in this list and dynamic
|
|
|
|
// symbol table section (DynSymTab) must be the first one.
|
2016-11-22 00:59:33 +08:00
|
|
|
finalizeSynthetic<ELFT>(
|
|
|
|
{In<ELFT>::DynSymTab, In<ELFT>::GnuHashTab, In<ELFT>::HashTab,
|
2016-12-08 20:58:55 +08:00
|
|
|
In<ELFT>::SymTab, In<ELFT>::ShStrTab, In<ELFT>::StrTab,
|
|
|
|
In<ELFT>::VerDef, In<ELFT>::DynStrTab, In<ELFT>::GdbIndex,
|
|
|
|
In<ELFT>::Got, In<ELFT>::MipsGot, In<ELFT>::IgotPlt,
|
|
|
|
In<ELFT>::GotPlt, In<ELFT>::RelaDyn, In<ELFT>::RelaIplt,
|
|
|
|
In<ELFT>::RelaPlt, In<ELFT>::Plt, In<ELFT>::Iplt,
|
|
|
|
In<ELFT>::Plt, In<ELFT>::EhFrameHdr, In<ELFT>::VerSym,
|
|
|
|
In<ELFT>::VerNeed, In<ELFT>::Dynamic});
|
2015-12-26 15:50:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Out<ELFT>::Bss->Size > 0)
|
2016-11-30 00:05:27 +08:00
|
|
|
OutputSections.push_back(Out<ELFT>::Bss);
|
2017-01-10 09:21:50 +08:00
|
|
|
if (Out<ELFT>::BssRelRo->Size > 0)
|
|
|
|
OutputSections.push_back(Out<ELFT>::BssRelRo);
|
2016-11-24 19:43:55 +08:00
|
|
|
|
|
|
|
auto OS = dyn_cast_or_null<OutputSection<ELFT>>(findSection(".ARM.exidx"));
|
|
|
|
if (OS && !OS->Sections.empty() && !Config->Relocatable)
|
|
|
|
OS->addSection(make<ARMExidxSentinelSection<ELFT>>());
|
2015-08-13 23:23:46 +08:00
|
|
|
}
|
|
|
|
|
2015-12-26 17:48:00 +08:00
|
|
|
// The linker is expected to define SECNAME_start and SECNAME_end
|
|
|
|
// symbols for a few sections. This function defines them.
|
|
|
|
template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
|
2016-11-10 07:23:45 +08:00
|
|
|
auto Define = [&](StringRef Start, StringRef End, OutputSectionBase *OS) {
|
2016-10-25 04:46:21 +08:00
|
|
|
// These symbols resolve to the image base if the section does not exist.
|
2016-12-21 16:40:09 +08:00
|
|
|
// A special value -1 indicates end of the section.
|
2016-11-10 07:23:45 +08:00
|
|
|
addOptionalSynthetic<ELFT>(Start, OS, 0);
|
2016-12-21 16:40:09 +08:00
|
|
|
addOptionalSynthetic<ELFT>(End, OS, OS ? -1 : 0);
|
2015-12-26 17:48:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Define("__preinit_array_start", "__preinit_array_end",
|
2016-08-09 12:25:20 +08:00
|
|
|
Out<ELFT>::PreinitArray);
|
|
|
|
Define("__init_array_start", "__init_array_end", Out<ELFT>::InitArray);
|
|
|
|
Define("__fini_array_start", "__fini_array_end", Out<ELFT>::FiniArray);
|
2016-10-27 18:28:53 +08:00
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
if (OutputSectionBase *Sec = findSection(".ARM.exidx"))
|
2016-10-27 18:28:53 +08:00
|
|
|
Define("__exidx_start", "__exidx_end", Sec);
|
2015-12-26 17:48:00 +08:00
|
|
|
}
|
|
|
|
|
2015-10-16 01:11:03 +08:00
|
|
|
// If a section name is valid as a C identifier (which is rare because of
|
|
|
|
// the leading '.'), linkers are expected to define __start_<secname> and
|
|
|
|
// __stop_<secname> symbols. They are at beginning and end of the section,
|
|
|
|
// respectively. This is not requested by the ELF standard, but GNU ld and
|
|
|
|
// gold provide the feature, and used by many programs.
|
|
|
|
template <class ELFT>
|
2016-11-10 07:23:45 +08:00
|
|
|
void Writer<ELFT>::addStartStopSymbols(OutputSectionBase *Sec) {
|
2015-10-16 01:11:03 +08:00
|
|
|
StringRef S = Sec->getName();
|
|
|
|
if (!isValidCIdentifier(S))
|
|
|
|
return;
|
2016-11-10 07:23:45 +08:00
|
|
|
addOptionalSynthetic<ELFT>(Saver.save("__start_" + S), Sec, 0, STV_DEFAULT);
|
2016-12-21 16:40:09 +08:00
|
|
|
addOptionalSynthetic<ELFT>(Saver.save("__stop_" + S), Sec, -1, STV_DEFAULT);
|
2015-10-16 01:11:03 +08:00
|
|
|
}
|
|
|
|
|
2016-08-09 09:35:38 +08:00
|
|
|
template <class ELFT>
|
2016-11-10 07:23:45 +08:00
|
|
|
OutputSectionBase *Writer<ELFT>::findSection(StringRef Name) {
|
|
|
|
for (OutputSectionBase *Sec : OutputSections)
|
2016-08-09 09:35:38 +08:00
|
|
|
if (Sec->getName() == Name)
|
|
|
|
return Sec;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
template <class ELFT> static bool needsPtLoad(OutputSectionBase *Sec) {
|
2016-11-09 09:42:41 +08:00
|
|
|
if (!(Sec->Flags & SHF_ALLOC))
|
2016-02-11 07:29:38 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
|
|
|
|
// responsible for allocating space for them, not the PT_LOAD that
|
|
|
|
// contains the TLS initialization image.
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Flags & SHF_TLS && Sec->Type == SHT_NOBITS)
|
2016-02-11 07:29:38 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
2015-09-10 04:48:09 +08:00
|
|
|
}
|
|
|
|
|
2016-09-20 23:22:27 +08:00
|
|
|
// Linker scripts are responsible for aligning addresses. Unfortunately, most
|
|
|
|
// linker scripts are designed for creating two PT_LOADs only, one RX and one
|
|
|
|
// RW. This means that there is no alignment in the RO to RX transition and we
|
|
|
|
// cannot create a PT_LOAD there.
|
|
|
|
template <class ELFT>
|
|
|
|
static typename ELFT::uint computeFlags(typename ELFT::uint F) {
|
2016-11-29 17:43:51 +08:00
|
|
|
if (Config->OMagic)
|
|
|
|
return PF_R | PF_W | PF_X;
|
2016-11-28 18:05:20 +08:00
|
|
|
if (Config->SingleRoRx && !(F & PF_W))
|
2016-09-20 23:22:27 +08:00
|
|
|
return F | PF_X;
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
2016-02-11 06:43:13 +08:00
|
|
|
// Decide which program headers to create and which sections to include in each
|
|
|
|
// one.
|
2016-12-20 01:01:01 +08:00
|
|
|
template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() {
|
|
|
|
std::vector<PhdrEntry> Ret;
|
|
|
|
auto AddHdr = [&](unsigned Type, unsigned Flags) -> PhdrEntry * {
|
2016-07-21 03:36:39 +08:00
|
|
|
Ret.emplace_back(Type, Flags);
|
|
|
|
return &Ret.back();
|
2016-02-11 06:43:13 +08:00
|
|
|
};
|
2015-09-10 04:48:09 +08:00
|
|
|
|
2015-10-24 05:45:59 +08:00
|
|
|
// The first phdr entry is PT_PHDR which describes the program header itself.
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry &Hdr = *AddHdr(PT_PHDR, PF_R);
|
2016-07-21 03:36:41 +08:00
|
|
|
Hdr.add(Out<ELFT>::ProgramHeaders);
|
2015-08-13 23:23:46 +08:00
|
|
|
|
2015-10-24 05:45:59 +08:00
|
|
|
// PT_INTERP must be the second entry if exists.
|
2016-11-10 07:23:45 +08:00
|
|
|
if (OutputSectionBase *Sec = findSection(".interp")) {
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry &Hdr = *AddHdr(PT_INTERP, Sec->getPhdrFlags());
|
2016-11-03 02:58:44 +08:00
|
|
|
Hdr.add(Sec);
|
2016-02-11 06:43:13 +08:00
|
|
|
}
|
2015-09-12 02:49:42 +08:00
|
|
|
|
2015-10-24 05:45:59 +08:00
|
|
|
// Add the first PT_LOAD segment for regular output sections.
|
2016-09-20 23:22:27 +08:00
|
|
|
uintX_t Flags = computeFlags<ELFT>(PF_R);
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry *Load = AddHdr(PT_LOAD, Flags);
|
2015-09-10 23:41:34 +08:00
|
|
|
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry TlsHdr(PT_TLS, PF_R);
|
|
|
|
PhdrEntry RelRo(PT_GNU_RELRO, PF_R);
|
|
|
|
PhdrEntry Note(PT_NOTE, PF_R);
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections) {
|
2016-11-09 09:42:41 +08:00
|
|
|
if (!(Sec->Flags & SHF_ALLOC))
|
2016-09-17 05:29:07 +08:00
|
|
|
break;
|
2016-02-11 06:43:13 +08:00
|
|
|
|
2016-02-11 07:29:38 +08:00
|
|
|
// If we meet TLS section then we create TLS header
|
2016-09-21 00:57:02 +08:00
|
|
|
// and put all TLS sections inside for further use when
|
2016-02-11 07:29:38 +08:00
|
|
|
// assign addresses.
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Flags & SHF_TLS)
|
2016-07-21 03:36:41 +08:00
|
|
|
TlsHdr.add(Sec);
|
2016-02-11 07:29:38 +08:00
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
if (!needsPtLoad<ELFT>(Sec))
|
2016-02-11 07:29:38 +08:00
|
|
|
continue;
|
|
|
|
|
2016-08-17 15:44:19 +08:00
|
|
|
// Segments are contiguous memory regions that has the same attributes
|
|
|
|
// (e.g. executable or writable). There is one phdr for each segment.
|
|
|
|
// Therefore, we need to create a new phdr when the next section has
|
|
|
|
// different flags or is loaded at a discontiguous address using AT linker
|
|
|
|
// script command.
|
2016-09-20 23:22:27 +08:00
|
|
|
uintX_t NewFlags = computeFlags<ELFT>(Sec->getPhdrFlags());
|
2016-10-06 17:39:28 +08:00
|
|
|
if (Script<ELFT>::X->hasLMA(Sec->getName()) || Flags != NewFlags) {
|
2016-03-10 05:37:22 +08:00
|
|
|
Load = AddHdr(PT_LOAD, NewFlags);
|
2016-02-11 06:43:13 +08:00
|
|
|
Flags = NewFlags;
|
2015-08-13 23:31:17 +08:00
|
|
|
}
|
2015-08-12 08:00:24 +08:00
|
|
|
|
2016-07-21 03:36:41 +08:00
|
|
|
Load->add(Sec);
|
2016-02-11 06:43:13 +08:00
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
if (isRelroSection<ELFT>(Sec))
|
2016-07-21 03:36:41 +08:00
|
|
|
RelRo.add(Sec);
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Type == SHT_NOTE)
|
2016-07-21 03:36:41 +08:00
|
|
|
Note.add(Sec);
|
2015-11-05 10:00:35 +08:00
|
|
|
}
|
2015-11-03 08:34:39 +08:00
|
|
|
|
2016-02-11 06:43:13 +08:00
|
|
|
// Add the TLS segment unless it's empty.
|
|
|
|
if (TlsHdr.First)
|
2016-07-21 03:36:39 +08:00
|
|
|
Ret.push_back(std::move(TlsHdr));
|
2016-02-11 06:43:13 +08:00
|
|
|
|
2015-10-24 05:45:59 +08:00
|
|
|
// Add an entry for .dynamic.
|
2016-11-17 17:16:34 +08:00
|
|
|
if (In<ELFT>::DynSymTab) {
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry &H =
|
|
|
|
*AddHdr(PT_DYNAMIC, In<ELFT>::Dynamic->OutSec->getPhdrFlags());
|
2016-11-15 20:26:55 +08:00
|
|
|
H.add(In<ELFT>::Dynamic->OutSec);
|
2015-10-11 06:34:30 +08:00
|
|
|
}
|
2015-08-12 09:45:28 +08:00
|
|
|
|
2016-02-11 06:43:13 +08:00
|
|
|
// PT_GNU_RELRO includes all sections that should be marked as
|
|
|
|
// read-only by dynamic linker after proccessing relocations.
|
|
|
|
if (RelRo.First)
|
2016-07-21 03:36:39 +08:00
|
|
|
Ret.push_back(std::move(RelRo));
|
2015-11-24 18:15:50 +08:00
|
|
|
|
2016-02-11 06:43:13 +08:00
|
|
|
// PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
|
2016-11-21 23:52:10 +08:00
|
|
|
if (!Out<ELFT>::EhFrame->empty() && In<ELFT>::EhFrameHdr) {
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry &Hdr =
|
2016-11-21 23:52:10 +08:00
|
|
|
*AddHdr(PT_GNU_EH_FRAME, In<ELFT>::EhFrameHdr->OutSec->getPhdrFlags());
|
|
|
|
Hdr.add(In<ELFT>::EhFrameHdr->OutSec);
|
2016-01-15 21:34:52 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 21:02:22 +08:00
|
|
|
// PT_OPENBSD_RANDOMIZE specifies the location and size of a part of the
|
|
|
|
// memory image of the program that must be filled with random data before any
|
|
|
|
// code in the object is executed.
|
2016-11-10 07:23:45 +08:00
|
|
|
if (OutputSectionBase *Sec = findSection(".openbsd.randomdata")) {
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry &Hdr = *AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags());
|
2016-10-14 21:02:22 +08:00
|
|
|
Hdr.add(Sec);
|
|
|
|
}
|
|
|
|
|
2015-11-22 06:19:32 +08:00
|
|
|
// PT_GNU_STACK is a special section to tell the loader to make the
|
|
|
|
// pages for the stack non-executable.
|
2016-10-12 01:46:48 +08:00
|
|
|
if (!Config->ZExecstack) {
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry &Hdr = *AddHdr(PT_GNU_STACK, PF_R | PF_W);
|
2016-08-18 07:59:58 +08:00
|
|
|
if (Config->ZStackSize != uint64_t(-1))
|
2016-12-20 01:01:01 +08:00
|
|
|
Hdr.p_memsz = Config->ZStackSize;
|
2016-08-18 07:59:58 +08:00
|
|
|
}
|
2016-03-01 21:23:29 +08:00
|
|
|
|
2016-10-14 18:34:36 +08:00
|
|
|
// PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
|
|
|
|
// is expected to perform W^X violations, such as calling mprotect(2) or
|
|
|
|
// mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
|
|
|
|
// OpenBSD.
|
|
|
|
if (Config->ZWxneeded)
|
|
|
|
AddHdr(PT_OPENBSD_WXNEEDED, PF_X);
|
|
|
|
|
2016-03-01 21:23:29 +08:00
|
|
|
if (Note.First)
|
2016-07-21 03:36:39 +08:00
|
|
|
Ret.push_back(std::move(Note));
|
|
|
|
return Ret;
|
2016-02-11 06:43:13 +08:00
|
|
|
}
|
|
|
|
|
2016-11-28 08:40:21 +08:00
|
|
|
template <class ELFT>
|
2016-12-20 01:01:01 +08:00
|
|
|
void Writer<ELFT>::addPtArmExid(std::vector<PhdrEntry> &Phdrs) {
|
2016-11-28 08:40:21 +08:00
|
|
|
if (Config->EMachine != EM_ARM)
|
|
|
|
return;
|
|
|
|
auto I = std::find_if(
|
|
|
|
OutputSections.begin(), OutputSections.end(),
|
|
|
|
[](OutputSectionBase *Sec) { return Sec->Type == SHT_ARM_EXIDX; });
|
|
|
|
if (I == OutputSections.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry ARMExidx(PT_ARM_EXIDX, PF_R);
|
2016-11-28 08:40:21 +08:00
|
|
|
ARMExidx.add(*I);
|
|
|
|
Phdrs.push_back(ARMExidx);
|
|
|
|
}
|
|
|
|
|
2017-01-10 09:21:30 +08:00
|
|
|
// The first section of each PT_LOAD, the first section in PT_GNU_RELRO and the
|
|
|
|
// first section after PT_GNU_RELRO have to be page aligned so that the dynamic
|
|
|
|
// linker can set the permissions.
|
2016-03-31 03:41:51 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
|
2016-12-20 01:01:01 +08:00
|
|
|
for (const PhdrEntry &P : Phdrs)
|
2016-12-20 05:21:07 +08:00
|
|
|
if (P.p_type == PT_LOAD && P.First)
|
2016-03-31 03:41:51 +08:00
|
|
|
P.First->PageAlign = true;
|
|
|
|
|
2016-12-20 01:01:01 +08:00
|
|
|
for (const PhdrEntry &P : Phdrs) {
|
|
|
|
if (P.p_type != PT_GNU_RELRO)
|
2016-03-31 03:41:51 +08:00
|
|
|
continue;
|
2017-01-10 09:21:30 +08:00
|
|
|
if (P.First)
|
|
|
|
P.First->PageAlign = true;
|
2016-03-31 03:41:51 +08:00
|
|
|
// Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we
|
|
|
|
// have to align it to a page.
|
|
|
|
auto End = OutputSections.end();
|
|
|
|
auto I = std::find(OutputSections.begin(), End, P.Last);
|
|
|
|
if (I == End || (I + 1) == End)
|
|
|
|
continue;
|
2016-11-10 07:23:45 +08:00
|
|
|
OutputSectionBase *Sec = *(I + 1);
|
|
|
|
if (needsPtLoad<ELFT>(Sec))
|
2016-03-31 03:41:51 +08:00
|
|
|
Sec->PageAlign = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 05:21:07 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void elf::allocateHeaders(MutableArrayRef<PhdrEntry> Phdrs,
|
|
|
|
ArrayRef<OutputSectionBase *> OutputSections) {
|
|
|
|
auto FirstPTLoad =
|
|
|
|
std::find_if(Phdrs.begin(), Phdrs.end(),
|
|
|
|
[](const PhdrEntry &E) { return E.p_type == PT_LOAD; });
|
|
|
|
if (FirstPTLoad == Phdrs.end())
|
|
|
|
return;
|
|
|
|
if (FirstPTLoad->First)
|
|
|
|
for (OutputSectionBase *Sec : OutputSections)
|
|
|
|
if (Sec->FirstInPtLoad == FirstPTLoad->First)
|
|
|
|
Sec->FirstInPtLoad = Out<ELFT>::ElfHeader;
|
|
|
|
FirstPTLoad->First = Out<ELFT>::ElfHeader;
|
|
|
|
if (!FirstPTLoad->Last)
|
|
|
|
FirstPTLoad->Last = Out<ELFT>::ProgramHeaders;
|
|
|
|
}
|
|
|
|
|
2016-04-06 15:20:45 +08:00
|
|
|
// We should set file offsets and VAs for elf header and program headers
|
|
|
|
// sections. These are special, we do not include them into output sections
|
|
|
|
// list, but have them to simplify the code.
|
|
|
|
template <class ELFT> void Writer<ELFT>::fixHeaders() {
|
2016-11-09 09:42:41 +08:00
|
|
|
Out<ELFT>::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
|
2016-11-22 03:59:33 +08:00
|
|
|
// If the script has SECTIONS, assignAddresses will compute the values.
|
|
|
|
if (ScriptConfig->HasSections)
|
|
|
|
return;
|
2016-12-20 05:21:07 +08:00
|
|
|
|
|
|
|
uintX_t HeaderSize = getHeaderSize<ELFT>();
|
|
|
|
// When -T<section> option is specified, lower the base to make room for those
|
|
|
|
// sections.
|
|
|
|
if (!Config->SectionStartMap.empty()) {
|
|
|
|
uint64_t Min = -1;
|
|
|
|
for (const auto &P : Config->SectionStartMap)
|
|
|
|
Min = std::min(Min, P.second);
|
|
|
|
if (HeaderSize < Min)
|
|
|
|
Min -= HeaderSize;
|
|
|
|
else
|
|
|
|
AllocateHeader = false;
|
|
|
|
if (Min < Config->ImageBase)
|
|
|
|
Config->ImageBase = alignDown(Min, Config->MaxPageSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AllocateHeader)
|
|
|
|
allocateHeaders<ELFT>(Phdrs, OutputSections);
|
|
|
|
|
2016-11-22 03:59:33 +08:00
|
|
|
uintX_t BaseVA = Config->ImageBase;
|
|
|
|
Out<ELFT>::ElfHeader->Addr = BaseVA;
|
|
|
|
Out<ELFT>::ProgramHeaders->Addr = BaseVA + Out<ELFT>::ElfHeader->Size;
|
2016-04-06 15:20:45 +08:00
|
|
|
}
|
|
|
|
|
2016-04-02 01:07:17 +08:00
|
|
|
// Assign VAs (addresses at run-time) to output sections.
|
2016-02-11 06:43:13 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::assignAddresses() {
|
2016-12-20 05:21:07 +08:00
|
|
|
uintX_t VA = Config->ImageBase;
|
|
|
|
if (AllocateHeader)
|
|
|
|
VA += getHeaderSize<ELFT>();
|
2016-04-06 15:20:45 +08:00
|
|
|
uintX_t ThreadBssOffset = 0;
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections) {
|
2016-11-09 09:42:41 +08:00
|
|
|
uintX_t Alignment = Sec->Addralign;
|
2016-03-31 03:41:51 +08:00
|
|
|
if (Sec->PageAlign)
|
2016-09-28 23:20:47 +08:00
|
|
|
Alignment = std::max<uintX_t>(Alignment, Config->MaxPageSize);
|
2016-02-11 06:43:13 +08:00
|
|
|
|
2016-09-14 21:07:13 +08:00
|
|
|
auto I = Config->SectionStartMap.find(Sec->getName());
|
|
|
|
if (I != Config->SectionStartMap.end())
|
|
|
|
VA = I->second;
|
|
|
|
|
2016-02-11 06:43:13 +08:00
|
|
|
// We only assign VAs to allocated sections.
|
2016-11-10 07:23:45 +08:00
|
|
|
if (needsPtLoad<ELFT>(Sec)) {
|
2016-06-17 09:18:46 +08:00
|
|
|
VA = alignTo(VA, Alignment);
|
2016-11-09 09:42:41 +08:00
|
|
|
Sec->Addr = VA;
|
|
|
|
VA += Sec->Size;
|
|
|
|
} else if (Sec->Flags & SHF_TLS && Sec->Type == SHT_NOBITS) {
|
2016-02-11 07:29:38 +08:00
|
|
|
uintX_t TVA = VA + ThreadBssOffset;
|
2016-06-17 09:18:46 +08:00
|
|
|
TVA = alignTo(TVA, Alignment);
|
2016-11-09 09:42:41 +08:00
|
|
|
Sec->Addr = TVA;
|
|
|
|
ThreadBssOffset = TVA - VA + Sec->Size;
|
2016-02-11 06:43:13 +08:00
|
|
|
}
|
2015-10-11 07:25:39 +08:00
|
|
|
}
|
2016-04-02 01:07:17 +08:00
|
|
|
}
|
2015-10-11 07:25:39 +08:00
|
|
|
|
2016-04-27 17:16:28 +08:00
|
|
|
// Adjusts the file alignment for a given output section and returns
|
|
|
|
// its new file offset. The file offset must be the same with its
|
|
|
|
// virtual address (modulo the page size) so that the loader can load
|
|
|
|
// executables without any address adjustment.
|
|
|
|
template <class ELFT, class uintX_t>
|
2016-11-10 07:23:45 +08:00
|
|
|
static uintX_t getFileAlignment(uintX_t Off, OutputSectionBase *Sec) {
|
|
|
|
OutputSectionBase *First = Sec->FirstInPtLoad;
|
2016-12-08 04:20:39 +08:00
|
|
|
// If the section is not in a PT_LOAD, we just have to align it.
|
2016-09-30 00:29:55 +08:00
|
|
|
if (!First)
|
2016-12-08 04:20:39 +08:00
|
|
|
return alignTo(Off, Sec->Addralign);
|
2016-09-29 17:20:33 +08:00
|
|
|
|
2016-12-08 04:20:39 +08:00
|
|
|
// The first section in a PT_LOAD has to have congruent offset and address
|
|
|
|
// module the page size.
|
2016-09-30 00:29:55 +08:00
|
|
|
if (Sec == First)
|
2016-12-08 04:10:43 +08:00
|
|
|
return alignTo(Off, Config->MaxPageSize, Sec->Addr);
|
2016-12-08 04:20:39 +08:00
|
|
|
|
|
|
|
// If two sections share the same PT_LOAD the file offset is calculated
|
|
|
|
// using this formula: Off2 = Off1 + (VA2 - VA1).
|
2016-11-09 09:42:41 +08:00
|
|
|
return First->Offset + Sec->Addr - First->Addr;
|
2016-04-27 17:16:28 +08:00
|
|
|
}
|
|
|
|
|
2016-08-25 17:05:47 +08:00
|
|
|
template <class ELFT, class uintX_t>
|
2016-11-10 07:23:45 +08:00
|
|
|
void setOffset(OutputSectionBase *Sec, uintX_t &Off) {
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Type == SHT_NOBITS) {
|
|
|
|
Sec->Offset = Off;
|
2016-08-25 17:05:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Off = getFileAlignment<ELFT>(Off, Sec);
|
2016-11-09 09:42:41 +08:00
|
|
|
Sec->Offset = Off;
|
|
|
|
Off += Sec->Size;
|
2016-08-25 17:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
|
|
|
|
uintX_t Off = 0;
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections)
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Flags & SHF_ALLOC)
|
2016-11-10 07:23:45 +08:00
|
|
|
setOffset<ELFT>(Sec, Off);
|
2016-08-25 17:05:47 +08:00
|
|
|
FileSize = alignTo(Off, sizeof(uintX_t));
|
|
|
|
}
|
|
|
|
|
2016-04-02 01:07:17 +08:00
|
|
|
// Assign file offsets to output sections.
|
|
|
|
template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
|
2016-07-01 18:27:36 +08:00
|
|
|
uintX_t Off = 0;
|
2016-11-10 07:23:45 +08:00
|
|
|
setOffset<ELFT>(Out<ELFT>::ElfHeader, Off);
|
|
|
|
setOffset<ELFT>(Out<ELFT>::ProgramHeaders, Off);
|
2016-04-06 15:20:45 +08:00
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections)
|
|
|
|
setOffset<ELFT>(Sec, Off);
|
2016-07-01 18:27:36 +08:00
|
|
|
|
2016-04-02 01:07:17 +08:00
|
|
|
SectionHeaderOff = alignTo(Off, sizeof(uintX_t));
|
2016-04-06 15:20:45 +08:00
|
|
|
FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr);
|
2016-04-01 18:49:14 +08:00
|
|
|
}
|
|
|
|
|
2016-04-02 01:07:17 +08:00
|
|
|
// Finalize the program headers. We call this function after we assign
|
|
|
|
// file offsets and VAs to all sections.
|
|
|
|
template <class ELFT> void Writer<ELFT>::setPhdrs() {
|
2016-12-20 01:01:01 +08:00
|
|
|
for (PhdrEntry &P : Phdrs) {
|
2016-11-10 07:23:45 +08:00
|
|
|
OutputSectionBase *First = P.First;
|
|
|
|
OutputSectionBase *Last = P.Last;
|
2016-04-02 06:42:04 +08:00
|
|
|
if (First) {
|
2016-12-20 01:01:01 +08:00
|
|
|
P.p_filesz = Last->Offset - First->Offset;
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Last->Type != SHT_NOBITS)
|
2016-12-20 01:01:01 +08:00
|
|
|
P.p_filesz += Last->Size;
|
|
|
|
P.p_memsz = Last->Addr + Last->Size - First->Addr;
|
|
|
|
P.p_offset = First->Offset;
|
|
|
|
P.p_vaddr = First->Addr;
|
2016-10-06 17:39:28 +08:00
|
|
|
if (!P.HasLMA)
|
2016-12-20 01:01:01 +08:00
|
|
|
P.p_paddr = First->getLMA();
|
2016-02-11 06:43:13 +08:00
|
|
|
}
|
2016-12-20 01:01:01 +08:00
|
|
|
if (P.p_type == PT_LOAD)
|
|
|
|
P.p_align = Config->MaxPageSize;
|
2017-01-05 02:56:15 +08:00
|
|
|
else if (P.p_type == PT_GNU_RELRO) {
|
2016-12-20 01:01:01 +08:00
|
|
|
P.p_align = 1;
|
2017-01-05 02:56:15 +08:00
|
|
|
// The glibc dynamic loader rounds the size down, so we need to round up
|
|
|
|
// to protect the last page. This is a no-op on FreeBSD which always
|
|
|
|
// rounds up.
|
|
|
|
P.p_memsz = alignTo(P.p_memsz, Config->MaxPageSize);
|
|
|
|
}
|
2016-08-17 15:44:19 +08:00
|
|
|
|
2016-02-11 06:43:13 +08:00
|
|
|
// The TLS pointer goes after PT_TLS. At least glibc will align it,
|
|
|
|
// so round up the size to make sure the offsets are correct.
|
2016-12-20 01:01:01 +08:00
|
|
|
if (P.p_type == PT_TLS) {
|
|
|
|
Out<ELFT>::TlsPhdr = &P;
|
|
|
|
if (P.p_memsz)
|
|
|
|
P.p_memsz = alignTo(P.p_memsz, P.p_align);
|
2015-10-24 05:45:59 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 06:41:00 +08:00
|
|
|
// The entry point address is chosen in the following ways.
|
|
|
|
//
|
|
|
|
// 1. the '-e' entry command-line option;
|
|
|
|
// 2. the ENTRY(symbol) command in a linker control script;
|
|
|
|
// 3. the value of the symbol start, if present;
|
|
|
|
// 4. the address of the first byte of the .text section, if present;
|
|
|
|
// 5. the address 0.
|
|
|
|
template <class ELFT> typename ELFT::uint Writer<ELFT>::getEntryAddr() {
|
2016-12-07 11:23:06 +08:00
|
|
|
// Case 1, 2 or 3. As a special case, if the symbol is actually
|
|
|
|
// a number, we'll use that number as an address.
|
2016-10-20 08:07:36 +08:00
|
|
|
if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Entry))
|
|
|
|
return B->getVA<ELFT>();
|
2016-12-07 11:23:06 +08:00
|
|
|
uint64_t Addr;
|
|
|
|
if (!Config->Entry.getAsInteger(0, Addr))
|
|
|
|
return Addr;
|
2016-11-24 06:41:00 +08:00
|
|
|
|
|
|
|
// Case 4
|
|
|
|
if (OutputSectionBase *Sec = findSection(".text")) {
|
2016-12-07 12:06:21 +08:00
|
|
|
if (Config->WarnMissingEntry)
|
2016-12-07 10:26:16 +08:00
|
|
|
warn("cannot find entry symbol " + Config->Entry + "; defaulting to 0x" +
|
|
|
|
utohexstr(Sec->Addr));
|
2016-11-24 06:41:00 +08:00
|
|
|
return Sec->Addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 5
|
2016-12-07 12:06:21 +08:00
|
|
|
if (Config->WarnMissingEntry)
|
2016-12-07 10:26:16 +08:00
|
|
|
warn("cannot find entry symbol " + Config->Entry +
|
|
|
|
"; not setting start address");
|
2016-10-20 08:07:36 +08:00
|
|
|
return 0;
|
2015-12-24 16:37:34 +08:00
|
|
|
}
|
|
|
|
|
2016-02-26 03:28:37 +08:00
|
|
|
template <class ELFT> static uint8_t getELFEncoding() {
|
|
|
|
if (ELFT::TargetEndianness == llvm::support::little)
|
|
|
|
return ELFDATA2LSB;
|
|
|
|
return ELFDATA2MSB;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t getELFType() {
|
2016-03-17 13:57:33 +08:00
|
|
|
if (Config->Pic)
|
2016-02-26 03:28:37 +08:00
|
|
|
return ET_DYN;
|
|
|
|
if (Config->Relocatable)
|
|
|
|
return ET_REL;
|
|
|
|
return ET_EXEC;
|
|
|
|
}
|
|
|
|
|
2015-12-26 18:52:26 +08:00
|
|
|
// This function is called after we have assigned address and size
|
|
|
|
// to each section. This function fixes some predefined absolute
|
|
|
|
// symbol values that depend on section address and size.
|
|
|
|
template <class ELFT> void Writer<ELFT>::fixAbsoluteSymbols() {
|
2016-08-23 02:44:04 +08:00
|
|
|
// __ehdr_start is the location of program headers.
|
|
|
|
if (ElfSym<ELFT>::EhdrStart)
|
2016-11-09 09:42:41 +08:00
|
|
|
ElfSym<ELFT>::EhdrStart->Value = Out<ELFT>::ProgramHeaders->Addr;
|
2016-08-23 02:44:04 +08:00
|
|
|
|
2016-06-20 21:30:37 +08:00
|
|
|
auto Set = [](DefinedRegular<ELFT> *S1, DefinedRegular<ELFT> *S2, uintX_t V) {
|
2016-04-22 04:50:15 +08:00
|
|
|
if (S1)
|
|
|
|
S1->Value = V;
|
|
|
|
if (S2)
|
|
|
|
S2->Value = V;
|
|
|
|
};
|
|
|
|
|
2016-03-02 03:18:07 +08:00
|
|
|
// _etext is the first location after the last read-only loadable segment.
|
|
|
|
// _edata is the first location after the last read-write loadable segment.
|
2016-04-01 18:23:32 +08:00
|
|
|
// _end is the first location after the uninitialized data region.
|
2016-12-20 01:01:01 +08:00
|
|
|
for (PhdrEntry &P : Phdrs) {
|
|
|
|
if (P.p_type != PT_LOAD)
|
2016-02-26 22:36:36 +08:00
|
|
|
continue;
|
2016-12-20 01:01:01 +08:00
|
|
|
Set(ElfSym<ELFT>::End, ElfSym<ELFT>::End2, P.p_vaddr + P.p_memsz);
|
2016-04-14 22:37:59 +08:00
|
|
|
|
2016-12-20 01:01:01 +08:00
|
|
|
uintX_t Val = P.p_vaddr + P.p_filesz;
|
|
|
|
if (P.p_flags & PF_W)
|
2016-04-22 04:50:15 +08:00
|
|
|
Set(ElfSym<ELFT>::Edata, ElfSym<ELFT>::Edata2, Val);
|
2016-04-14 22:37:59 +08:00
|
|
|
else
|
2016-04-22 04:50:15 +08:00
|
|
|
Set(ElfSym<ELFT>::Etext, ElfSym<ELFT>::Etext2, Val);
|
2016-02-26 22:36:36 +08:00
|
|
|
}
|
2016-11-24 06:22:16 +08:00
|
|
|
|
|
|
|
// Setup MIPS _gp_disp/__gnu_local_gp symbols which should
|
|
|
|
// be equal to the _gp symbol's value.
|
|
|
|
if (Config->EMachine == EM_MIPS) {
|
2016-12-13 05:34:11 +08:00
|
|
|
if (!ElfSym<ELFT>::MipsGp->Value) {
|
|
|
|
// Find GP-relative section with the lowest address
|
|
|
|
// and use this address to calculate default _gp value.
|
|
|
|
uintX_t Gp = -1;
|
|
|
|
for (const OutputSectionBase * OS : OutputSections)
|
|
|
|
if ((OS->Flags & SHF_MIPS_GPREL) && OS->Addr < Gp)
|
|
|
|
Gp = OS->Addr;
|
|
|
|
if (Gp != (uintX_t)-1)
|
|
|
|
ElfSym<ELFT>::MipsGp->Value = Gp + 0x7ff0;
|
|
|
|
}
|
2016-11-24 06:22:16 +08:00
|
|
|
if (ElfSym<ELFT>::MipsGpDisp)
|
2016-12-08 14:19:47 +08:00
|
|
|
ElfSym<ELFT>::MipsGpDisp->Value = ElfSym<ELFT>::MipsGp->Value;
|
2016-11-24 06:22:16 +08:00
|
|
|
if (ElfSym<ELFT>::MipsLocalGp)
|
2016-12-08 14:19:47 +08:00
|
|
|
ElfSym<ELFT>::MipsLocalGp->Value = ElfSym<ELFT>::MipsGp->Value;
|
2016-11-24 06:22:16 +08:00
|
|
|
}
|
2015-12-26 18:52:26 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::writeHeader() {
|
|
|
|
uint8_t *Buf = Buffer->getBufferStart();
|
2015-10-24 06:44:39 +08:00
|
|
|
memcpy(Buf, "\177ELF", 4);
|
|
|
|
|
2015-10-25 01:57:40 +08:00
|
|
|
// Write the ELF header.
|
2015-09-09 05:57:31 +08:00
|
|
|
auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
|
2015-08-05 23:08:40 +08:00
|
|
|
EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
|
2016-02-26 03:28:37 +08:00
|
|
|
EHdr->e_ident[EI_DATA] = getELFEncoding<ELFT>();
|
2015-07-25 05:03:07 +08:00
|
|
|
EHdr->e_ident[EI_VERSION] = EV_CURRENT;
|
2016-10-27 22:00:51 +08:00
|
|
|
EHdr->e_ident[EI_OSABI] = Config->OSABI;
|
2016-02-26 03:28:37 +08:00
|
|
|
EHdr->e_type = getELFType();
|
2016-10-27 22:00:51 +08:00
|
|
|
EHdr->e_machine = Config->EMachine;
|
2015-07-25 05:03:07 +08:00
|
|
|
EHdr->e_version = EV_CURRENT;
|
2016-11-24 06:41:00 +08:00
|
|
|
EHdr->e_entry = getEntryAddr();
|
2015-07-29 08:30:10 +08:00
|
|
|
EHdr->e_shoff = SectionHeaderOff;
|
2015-09-09 05:57:31 +08:00
|
|
|
EHdr->e_ehsize = sizeof(Elf_Ehdr);
|
2015-10-11 06:34:30 +08:00
|
|
|
EHdr->e_phnum = Phdrs.size();
|
2015-09-09 05:57:31 +08:00
|
|
|
EHdr->e_shentsize = sizeof(Elf_Shdr);
|
2016-04-06 15:20:45 +08:00
|
|
|
EHdr->e_shnum = OutputSections.size() + 1;
|
2016-11-14 17:16:00 +08:00
|
|
|
EHdr->e_shstrndx = In<ELFT>::ShStrTab->OutSec->SectionIndex;
|
2015-10-25 01:57:40 +08:00
|
|
|
|
2016-09-14 04:51:32 +08:00
|
|
|
if (Config->EMachine == EM_ARM)
|
|
|
|
// We don't currently use any features incompatible with EF_ARM_EABI_VER5,
|
|
|
|
// but we don't have any firm guarantees of conformance. Linux AArch64
|
|
|
|
// kernels (as of 2016) require an EABI version to be set.
|
|
|
|
EHdr->e_flags = EF_ARM_EABI_VER5;
|
|
|
|
else if (Config->EMachine == EM_MIPS)
|
2016-07-21 04:30:41 +08:00
|
|
|
EHdr->e_flags = getMipsEFlags<ELFT>();
|
2016-03-14 03:29:17 +08:00
|
|
|
|
2016-02-25 16:23:37 +08:00
|
|
|
if (!Config->Relocatable) {
|
|
|
|
EHdr->e_phoff = sizeof(Elf_Ehdr);
|
|
|
|
EHdr->e_phentsize = sizeof(Elf_Phdr);
|
|
|
|
}
|
|
|
|
|
2015-10-25 01:57:40 +08:00
|
|
|
// Write the program header table.
|
2016-02-11 06:43:13 +08:00
|
|
|
auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
|
2016-12-20 01:01:01 +08:00
|
|
|
for (PhdrEntry &P : Phdrs) {
|
|
|
|
HBuf->p_type = P.p_type;
|
|
|
|
HBuf->p_flags = P.p_flags;
|
|
|
|
HBuf->p_offset = P.p_offset;
|
|
|
|
HBuf->p_vaddr = P.p_vaddr;
|
|
|
|
HBuf->p_paddr = P.p_paddr;
|
|
|
|
HBuf->p_filesz = P.p_filesz;
|
|
|
|
HBuf->p_memsz = P.p_memsz;
|
|
|
|
HBuf->p_align = P.p_align;
|
|
|
|
++HBuf;
|
|
|
|
}
|
2015-09-09 06:55:28 +08:00
|
|
|
|
2015-10-25 01:57:40 +08:00
|
|
|
// Write the section header table. Note that the first table entry is null.
|
2016-02-26 07:58:21 +08:00
|
|
|
auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections)
|
|
|
|
Sec->writeHeaderTo<ELFT>(++SHdrs);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2016-12-06 01:40:37 +08:00
|
|
|
// Removes a given file asynchronously. This is a performance hack,
|
|
|
|
// so remove this when operating systems are improved.
|
|
|
|
//
|
|
|
|
// On Linux (and probably on other Unix-like systems), unlink(2) is a
|
|
|
|
// noticeably slow system call. As of 2016, unlink takes 250
|
|
|
|
// milliseconds to remove a 1 GB file on ext4 filesystem on my machine.
|
|
|
|
//
|
|
|
|
// To create a new result file, we first remove existing file. So, if
|
|
|
|
// you repeatedly link a 1 GB program in a regular compile-link-debug
|
|
|
|
// cycle, every cycle wastes 250 milliseconds only to remove a file.
|
|
|
|
// Since LLD can link a 1 GB binary in about 5 seconds, that waste
|
|
|
|
// actually counts.
|
|
|
|
//
|
|
|
|
// This function spawns a background thread to call unlink.
|
|
|
|
// The calling thread returns almost immediately.
|
|
|
|
static void unlinkAsync(StringRef Path) {
|
|
|
|
if (!Config->Threads || !sys::fs::exists(Config->OutputFile))
|
|
|
|
return;
|
|
|
|
|
2016-12-25 14:49:17 +08:00
|
|
|
// First, rename Path to avoid race condition. We cannot remove
|
2016-12-06 01:40:37 +08:00
|
|
|
// Path from a different thread because we are now going to create
|
|
|
|
// Path as a new file. If we do that in a different thread, the new
|
|
|
|
// thread can remove the new file.
|
|
|
|
SmallString<128> TempPath;
|
2017-01-10 07:07:05 +08:00
|
|
|
if (sys::fs::createUniqueFile(Path + "tmp%%%%%%%%", TempPath))
|
|
|
|
return;
|
|
|
|
if (sys::fs::rename(Path, TempPath)) {
|
|
|
|
sys::fs::remove(TempPath);
|
|
|
|
return;
|
|
|
|
}
|
2016-12-06 01:40:37 +08:00
|
|
|
|
|
|
|
// Remove TempPath in background.
|
|
|
|
std::thread([=] { ::remove(TempPath.str().str().c_str()); }).detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a result file.
|
2016-04-02 01:24:19 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::openFile() {
|
2016-12-06 01:40:37 +08:00
|
|
|
unlinkAsync(Config->OutputFile);
|
2015-08-13 08:31:46 +08:00
|
|
|
ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
2016-02-03 06:48:04 +08:00
|
|
|
FileOutputBuffer::create(Config->OutputFile, FileSize,
|
|
|
|
FileOutputBuffer::F_executable);
|
2016-12-06 01:40:37 +08:00
|
|
|
|
2016-07-15 09:38:54 +08:00
|
|
|
if (auto EC = BufferOrErr.getError())
|
2017-01-13 06:18:04 +08:00
|
|
|
error("failed to open " + Config->OutputFile + ": " + EC.message());
|
2016-04-02 01:24:19 +08:00
|
|
|
else
|
2016-07-15 09:38:54 +08:00
|
|
|
Buffer = std::move(*BufferOrErr);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2016-08-25 17:05:47 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
|
|
|
|
uint8_t *Buf = Buffer->getBufferStart();
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections)
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Flags & SHF_ALLOC)
|
|
|
|
Sec->writeTo(Buf + Sec->Offset);
|
2016-08-25 17:05:47 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// Write section contents to a mmap'ed file.
|
|
|
|
template <class ELFT> void Writer<ELFT>::writeSections() {
|
|
|
|
uint8_t *Buf = Buffer->getBufferStart();
|
[ELF2/PPC64] Resolve local-call relocations using the correct function-descriptor values
Under PPC64 ELF v1 ABI, the symbols associated with each function name don't
point directly to the code in the .text section (or similar), but rather to a
function descriptor structure in a special data section named .opd. The
elements in the .opd structure include a pointer to the actual code, and a the
relevant TOC base value. Both of these are themselves set by relocations.
When we have a local call, we need the relevant relocation to refer directly to
the target code, not to the function-descriptor in the .opd section. Only when
we have a .plt stub do we care about the address of the .opd function
descriptor itself.
So we make a few changes here:
1. Always write .opd first, so that its relocated data values are available
for later use when writing the text sections. Record a pointer to the .opd
structure, and its corresponding buffer.
2. When processing a relative branch relocation under ppc64, if the
destination points into the .opd section, read the code pointer out of the
function descriptor structure and use that instead.
This this, I can link, and run, a dynamically-compiled "hello world"
application on big-Endian PPC64/Linux (ELF v1 ABI) using lld.
llvm-svn: 250122
2015-10-13 07:16:53 +08:00
|
|
|
|
2016-08-09 09:35:37 +08:00
|
|
|
// PPC64 needs to process relocations in the .opd section
|
|
|
|
// before processing relocations in code-containing sections.
|
2016-08-09 09:35:38 +08:00
|
|
|
Out<ELFT>::Opd = findSection(".opd");
|
2016-08-09 09:35:37 +08:00
|
|
|
if (Out<ELFT>::Opd) {
|
2016-11-09 09:42:41 +08:00
|
|
|
Out<ELFT>::OpdBuf = Buf + Out<ELFT>::Opd->Offset;
|
|
|
|
Out<ELFT>::Opd->writeTo(Buf + Out<ELFT>::Opd->Offset);
|
2015-10-13 22:45:51 +08:00
|
|
|
}
|
[ELF2/PPC64] Resolve local-call relocations using the correct function-descriptor values
Under PPC64 ELF v1 ABI, the symbols associated with each function name don't
point directly to the code in the .text section (or similar), but rather to a
function descriptor structure in a special data section named .opd. The
elements in the .opd structure include a pointer to the actual code, and a the
relevant TOC base value. Both of these are themselves set by relocations.
When we have a local call, we need the relevant relocation to refer directly to
the target code, not to the function-descriptor in the .opd section. Only when
we have a .plt stub do we care about the address of the .opd function
descriptor itself.
So we make a few changes here:
1. Always write .opd first, so that its relocated data values are available
for later use when writing the text sections. Record a pointer to the .opd
structure, and its corresponding buffer.
2. When processing a relative branch relocation under ppc64, if the
destination points into the .opd section, read the code pointer out of the
function descriptor structure and use that instead.
This this, I can link, and run, a dynamically-compiled "hello world"
application on big-Endian PPC64/Linux (ELF v1 ABI) using lld.
llvm-svn: 250122
2015-10-13 07:16:53 +08:00
|
|
|
|
2016-11-21 23:52:10 +08:00
|
|
|
OutputSectionBase *EhFrameHdr =
|
|
|
|
In<ELFT>::EhFrameHdr ? In<ELFT>::EhFrameHdr->OutSec : nullptr;
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : OutputSections)
|
2016-11-21 23:52:10 +08:00
|
|
|
if (Sec != Out<ELFT>::Opd && Sec != EhFrameHdr)
|
2016-11-09 09:42:41 +08:00
|
|
|
Sec->writeTo(Buf + Sec->Offset);
|
2016-08-31 15:43:50 +08:00
|
|
|
|
|
|
|
// The .eh_frame_hdr depends on .eh_frame section contents, therefore
|
|
|
|
// it should be written after .eh_frame is written.
|
2016-11-21 23:52:10 +08:00
|
|
|
if (!Out<ELFT>::EhFrame->empty() && EhFrameHdr)
|
|
|
|
EhFrameHdr->writeTo(Buf + EhFrameHdr->Offset);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
2015-10-10 05:07:25 +08:00
|
|
|
|
ELF: Implement --build-id.
This patch implements --build-id. After the linker creates an output file
in the memory buffer, it computes the FNV1 hash of the resulting file
and set the hash to the .note section as a build-id.
GNU ld and gold have the same feature, but their default choice of the
hash function is different. Their default is SHA1.
We made a deliberate choice to not use a secure hash function for the
sake of performance. Computing a secure hash is slow -- for example,
MD5 throughput is usually 400 MB/s or so. SHA1 is slower than that.
As a result, if you pass --build-id to gold, then the linker becomes about
10% slower than that without the option. We observed a similar degradation
in an experimental implementation of build-id for LLD. On the other hand,
we observed only 1-2% performance degradation with the FNV hash.
Since build-id is not for digital certificate or anything, we think that
a very small probability of collision is acceptable.
We considered using other signals such as using input file timestamps as
inputs to a secure hash function. But such signals would have an issue
with build reproducibility (if you build a binary from the same source
tree using the same toolchain, the build id should become the same.)
GNU linkers accepts --build-id=<style> option where style is one of
"MD5", "SHA1", or an arbitrary hex string. That option is out of scope
of this patch.
http://reviews.llvm.org/D18091
llvm-svn: 263292
2016-03-12 04:51:53 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::writeBuildId() {
|
2016-11-01 17:49:24 +08:00
|
|
|
if (!In<ELFT>::BuildId || !In<ELFT>::BuildId->OutSec)
|
ELF: Implement --build-id.
This patch implements --build-id. After the linker creates an output file
in the memory buffer, it computes the FNV1 hash of the resulting file
and set the hash to the .note section as a build-id.
GNU ld and gold have the same feature, but their default choice of the
hash function is different. Their default is SHA1.
We made a deliberate choice to not use a secure hash function for the
sake of performance. Computing a secure hash is slow -- for example,
MD5 throughput is usually 400 MB/s or so. SHA1 is slower than that.
As a result, if you pass --build-id to gold, then the linker becomes about
10% slower than that without the option. We observed a similar degradation
in an experimental implementation of build-id for LLD. On the other hand,
we observed only 1-2% performance degradation with the FNV hash.
Since build-id is not for digital certificate or anything, we think that
a very small probability of collision is acceptable.
We considered using other signals such as using input file timestamps as
inputs to a secure hash function. But such signals would have an issue
with build reproducibility (if you build a binary from the same source
tree using the same toolchain, the build id should become the same.)
GNU linkers accepts --build-id=<style> option where style is one of
"MD5", "SHA1", or an arbitrary hex string. That option is out of scope
of this patch.
http://reviews.llvm.org/D18091
llvm-svn: 263292
2016-03-12 04:51:53 +08:00
|
|
|
return;
|
|
|
|
|
2016-09-02 06:43:03 +08:00
|
|
|
// Compute a hash of all sections of the output file.
|
ELF: Implement --build-id.
This patch implements --build-id. After the linker creates an output file
in the memory buffer, it computes the FNV1 hash of the resulting file
and set the hash to the .note section as a build-id.
GNU ld and gold have the same feature, but their default choice of the
hash function is different. Their default is SHA1.
We made a deliberate choice to not use a secure hash function for the
sake of performance. Computing a secure hash is slow -- for example,
MD5 throughput is usually 400 MB/s or so. SHA1 is slower than that.
As a result, if you pass --build-id to gold, then the linker becomes about
10% slower than that without the option. We observed a similar degradation
in an experimental implementation of build-id for LLD. On the other hand,
we observed only 1-2% performance degradation with the FNV hash.
Since build-id is not for digital certificate or anything, we think that
a very small probability of collision is acceptable.
We considered using other signals such as using input file timestamps as
inputs to a secure hash function. But such signals would have an issue
with build reproducibility (if you build a binary from the same source
tree using the same toolchain, the build id should become the same.)
GNU linkers accepts --build-id=<style> option where style is one of
"MD5", "SHA1", or an arbitrary hex string. That option is out of scope
of this patch.
http://reviews.llvm.org/D18091
llvm-svn: 263292
2016-03-12 04:51:53 +08:00
|
|
|
uint8_t *Start = Buffer->getBufferStart();
|
2016-09-02 06:43:03 +08:00
|
|
|
uint8_t *End = Start + FileSize;
|
2016-11-01 17:49:24 +08:00
|
|
|
In<ELFT>::BuildId->writeBuildId({Start, End});
|
ELF: Implement --build-id.
This patch implements --build-id. After the linker creates an output file
in the memory buffer, it computes the FNV1 hash of the resulting file
and set the hash to the .note section as a build-id.
GNU ld and gold have the same feature, but their default choice of the
hash function is different. Their default is SHA1.
We made a deliberate choice to not use a secure hash function for the
sake of performance. Computing a secure hash is slow -- for example,
MD5 throughput is usually 400 MB/s or so. SHA1 is slower than that.
As a result, if you pass --build-id to gold, then the linker becomes about
10% slower than that without the option. We observed a similar degradation
in an experimental implementation of build-id for LLD. On the other hand,
we observed only 1-2% performance degradation with the FNV hash.
Since build-id is not for digital certificate or anything, we think that
a very small probability of collision is acceptable.
We considered using other signals such as using input file timestamps as
inputs to a secure hash function. But such signals would have an issue
with build reproducibility (if you build a binary from the same source
tree using the same toolchain, the build id should become the same.)
GNU linkers accepts --build-id=<style> option where style is one of
"MD5", "SHA1", or an arbitrary hex string. That option is out of scope
of this patch.
http://reviews.llvm.org/D18091
llvm-svn: 263292
2016-03-12 04:51:53 +08:00
|
|
|
}
|
|
|
|
|
2016-08-09 11:38:23 +08:00
|
|
|
template void elf::writeResult<ELF32LE>();
|
|
|
|
template void elf::writeResult<ELF32BE>();
|
|
|
|
template void elf::writeResult<ELF64LE>();
|
|
|
|
template void elf::writeResult<ELF64BE>();
|
2016-07-19 17:25:43 +08:00
|
|
|
|
2016-12-20 05:21:07 +08:00
|
|
|
template void elf::allocateHeaders<ELF32LE>(MutableArrayRef<PhdrEntry>,
|
|
|
|
ArrayRef<OutputSectionBase *>);
|
|
|
|
template void elf::allocateHeaders<ELF32BE>(MutableArrayRef<PhdrEntry>,
|
|
|
|
ArrayRef<OutputSectionBase *>);
|
|
|
|
template void elf::allocateHeaders<ELF64LE>(MutableArrayRef<PhdrEntry>,
|
|
|
|
ArrayRef<OutputSectionBase *>);
|
|
|
|
template void elf::allocateHeaders<ELF64BE>(MutableArrayRef<PhdrEntry>,
|
|
|
|
ArrayRef<OutputSectionBase *>);
|
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
template bool elf::isRelroSection<ELF32LE>(const OutputSectionBase *);
|
|
|
|
template bool elf::isRelroSection<ELF32BE>(const OutputSectionBase *);
|
|
|
|
template bool elf::isRelroSection<ELF64LE>(const OutputSectionBase *);
|
|
|
|
template bool elf::isRelroSection<ELF64BE>(const OutputSectionBase *);
|
2016-07-19 17:25:43 +08:00
|
|
|
|
2016-07-26 06:26:28 +08:00
|
|
|
template void elf::reportDiscarded<ELF32LE>(InputSectionBase<ELF32LE> *);
|
|
|
|
template void elf::reportDiscarded<ELF32BE>(InputSectionBase<ELF32BE> *);
|
|
|
|
template void elf::reportDiscarded<ELF64LE>(InputSectionBase<ELF64LE> *);
|
|
|
|
template void elf::reportDiscarded<ELF64BE>(InputSectionBase<ELF64BE> *);
|