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();
|
2017-02-11 09:40:49 +08:00
|
|
|
void addSectionSymbols();
|
2015-12-26 15:50:39 +08:00
|
|
|
void addReservedSymbols();
|
2016-07-30 00:18:47 +08:00
|
|
|
void createSections();
|
2017-02-23 10:28:28 +08:00
|
|
|
void forEachRelSec(std::function<void(InputSectionBase &)> 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();
|
2017-01-29 01:48:21 +08:00
|
|
|
void fixPredefinedSymbols();
|
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
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
std::vector<OutputSection *> OutputSections;
|
2017-02-27 10:31:48 +08:00
|
|
|
OutputSectionFactory Factory{OutputSections};
|
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();
|
2017-02-24 23:07:30 +08:00
|
|
|
void addStartStopSymbols(OutputSection *Sec);
|
2016-11-24 06:41:00 +08:00
|
|
|
uintX_t getEntryAddr();
|
2017-02-24 23:07:30 +08:00
|
|
|
OutputSection *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;
|
|
|
|
|
2017-02-17 08:40:44 +08:00
|
|
|
// If -emit-relocs is given (which is rare), we need to copy
|
|
|
|
// relocation sections to the output. If input section .foo is
|
|
|
|
// output as .bar, we want to rename .rel.foo .rel.bar as well.
|
2017-02-17 08:28:17 +08:00
|
|
|
if (Config->EmitRelocs) {
|
|
|
|
for (StringRef V : {".rel.", ".rela."}) {
|
|
|
|
if (Name.startswith(V)) {
|
|
|
|
StringRef Inner = getOutputSectionName(Name.substr(V.size() - 1));
|
|
|
|
return Saver.save(Twine(V.drop_back()) + Inner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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());
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:06:18 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
static typename ELFT::uint getOutFlags(InputSectionBase *S) {
|
2017-02-03 21:06:18 +08:00
|
|
|
return S->Flags & ~(typename ELFT::uint)(SHF_GROUP | SHF_COMPRESSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function scans over the input sections and creates mergeable
|
|
|
|
// synthetic sections. It removes MergeInputSections from array and
|
|
|
|
// adds new synthetic ones. Each synthetic section is added to the
|
|
|
|
// location of the first input section it replaces.
|
|
|
|
template <class ELFT> static void combineMergableSections() {
|
|
|
|
typedef typename ELFT::uint uintX_t;
|
|
|
|
|
|
|
|
std::vector<MergeSyntheticSection<ELFT> *> MergeSections;
|
2017-02-27 10:32:08 +08:00
|
|
|
for (InputSectionBase *&S : InputSections) {
|
2017-02-03 21:06:18 +08:00
|
|
|
MergeInputSection<ELFT> *MS = dyn_cast<MergeInputSection<ELFT>>(S);
|
|
|
|
if (!MS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// We do not want to handle sections that are not alive, so just remove
|
|
|
|
// them instead of trying to merge.
|
|
|
|
if (!MS->Live)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
StringRef OutsecName = getOutputSectionName(MS->Name);
|
2017-02-23 10:28:28 +08:00
|
|
|
uintX_t Flags = getOutFlags<ELFT>(MS);
|
2017-02-03 21:06:18 +08:00
|
|
|
uintX_t Alignment = std::max<uintX_t>(MS->Alignment, MS->Entsize);
|
|
|
|
|
|
|
|
auto I =
|
|
|
|
llvm::find_if(MergeSections, [=](MergeSyntheticSection<ELFT> *Sec) {
|
|
|
|
return Sec->Name == OutsecName && Sec->Flags == Flags &&
|
|
|
|
Sec->Alignment == Alignment;
|
|
|
|
});
|
|
|
|
if (I == MergeSections.end()) {
|
|
|
|
MergeSyntheticSection<ELFT> *Syn = make<MergeSyntheticSection<ELFT>>(
|
|
|
|
OutsecName, MS->Type, Flags, Alignment);
|
|
|
|
MergeSections.push_back(Syn);
|
|
|
|
I = std::prev(MergeSections.end());
|
|
|
|
S = Syn;
|
|
|
|
} else {
|
|
|
|
S = nullptr;
|
|
|
|
}
|
|
|
|
(*I)->addSection(MS);
|
|
|
|
}
|
|
|
|
|
2017-02-27 10:32:08 +08:00
|
|
|
std::vector<InputSectionBase *> &V = InputSections;
|
2017-02-03 21:06:18 +08:00
|
|
|
V.erase(std::remove(V.begin(), V.end(), nullptr), V.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();
|
2017-02-03 21:06:18 +08:00
|
|
|
combineMergableSections<ELFT>();
|
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
|
|
|
// 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();
|
|
|
|
|
2017-02-11 09:40:49 +08:00
|
|
|
if (Config->copyRelocs())
|
|
|
|
addSectionSymbols();
|
|
|
|
|
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();
|
2017-01-29 01:48:21 +08:00
|
|
|
fixPredefinedSymbols();
|
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
|
|
|
}
|
|
|
|
|
2017-02-27 10:31:26 +08:00
|
|
|
// Initialize Out members.
|
2016-11-02 07:17:45 +08:00
|
|
|
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.
|
2017-02-27 10:31:26 +08:00
|
|
|
memset(&Out::First, 0, sizeof(Out));
|
2016-11-02 07:17:45 +08:00
|
|
|
|
2017-02-27 10:32:08 +08:00
|
|
|
auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
|
2017-02-05 13:18:58 +08:00
|
|
|
|
2016-11-02 07:17:45 +08:00
|
|
|
// Create singleton output sections.
|
2017-02-27 10:31:26 +08:00
|
|
|
Out::Bss = make<OutputSection>(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
|
|
|
|
Out::BssRelRo =
|
2017-02-24 23:07:30 +08:00
|
|
|
make<OutputSection>(".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-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
|
|
|
|
2017-02-27 10:31:26 +08:00
|
|
|
Out::ElfHeader = make<OutputSection>("", 0, SHF_ALLOC);
|
|
|
|
Out::ElfHeader->Size = sizeof(Elf_Ehdr);
|
|
|
|
Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
|
|
|
|
Out::ProgramHeaders->updateAlignment(sizeof(uintX_t));
|
2016-11-02 07:17:45 +08:00
|
|
|
|
2016-11-06 07:05:47 +08:00
|
|
|
if (needsInterpSection<ELFT>()) {
|
2017-02-27 10:32:49 +08:00
|
|
|
In<ELFT>::Interp = createInterpSection();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::Interp);
|
2016-11-06 07:05:47 +08:00
|
|
|
} else {
|
|
|
|
In<ELFT>::Interp = nullptr;
|
|
|
|
}
|
2016-11-02 07:17:45 +08:00
|
|
|
|
2016-11-30 00:05:27 +08:00
|
|
|
if (!Config->Relocatable)
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(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>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::BuildId);
|
2016-11-22 08:54:15 +08:00
|
|
|
}
|
2016-11-06 07:05:47 +08:00
|
|
|
|
2017-02-24 00:49:07 +08:00
|
|
|
InputSection *Common = createCommonSection<ELFT>();
|
2016-11-06 07:05:47 +08:00
|
|
|
if (!Common->Data.empty()) {
|
|
|
|
In<ELFT>::Common = Common;
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(Common);
|
2016-11-06 07:05:47 +08:00
|
|
|
}
|
2016-11-10 05:36:56 +08:00
|
|
|
|
2016-11-22 12:28:39 +08:00
|
|
|
// Add MIPS-specific sections.
|
2017-02-14 13:45:47 +08:00
|
|
|
bool HasDynSymTab =
|
2017-02-16 05:51:25 +08:00
|
|
|
!Symtab<ELFT>::X->getSharedFiles().empty() || Config->pic() ||
|
|
|
|
Config->ExportDynamic;
|
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>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::MipsRldMap);
|
2016-11-23 01:49:14 +08:00
|
|
|
}
|
2016-11-22 12:28:39 +08:00
|
|
|
if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(Sec);
|
2016-11-22 12:28:39 +08:00
|
|
|
if (auto *Sec = MipsOptionsSection<ELFT>::create())
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(Sec);
|
2016-11-22 12:28:39 +08:00
|
|
|
if (auto *Sec = MipsReginfoSection<ELFT>::create())
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(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);
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::DynSymTab);
|
2016-11-25 16:05:41 +08:00
|
|
|
|
|
|
|
In<ELFT>::VerSym = make<VersionTableSection<ELFT>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::VerSym);
|
2016-11-25 16:05:41 +08:00
|
|
|
|
|
|
|
if (!Config->VersionDefinitions.empty()) {
|
|
|
|
In<ELFT>::VerDef = make<VersionDefinitionSection<ELFT>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::VerDef);
|
2016-11-25 16:05:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
In<ELFT>::VerNeed = make<VersionNeedSection<ELFT>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::VerNeed);
|
2016-11-25 16:05:41 +08:00
|
|
|
|
|
|
|
if (Config->GnuHash) {
|
|
|
|
In<ELFT>::GnuHashTab = make<GnuHashTableSection<ELFT>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::GnuHashTab);
|
2016-11-25 16:05:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Config->SysvHash) {
|
|
|
|
In<ELFT>::HashTab = make<HashTableSection<ELFT>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::HashTab);
|
2016-11-25 16:05:41 +08:00
|
|
|
}
|
|
|
|
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::Dynamic);
|
|
|
|
Add(In<ELFT>::DynStrTab);
|
|
|
|
Add(In<ELFT>::RelaDyn);
|
2016-11-25 16:05:41 +08:00
|
|
|
}
|
|
|
|
|
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>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::MipsGot);
|
2016-11-25 16:05:41 +08:00
|
|
|
} else {
|
2016-11-17 05:01:02 +08:00
|
|
|
In<ELFT>::Got = make<GotSection<ELFT>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::Got);
|
2016-11-25 16:05:41 +08:00
|
|
|
}
|
2016-11-17 05:01:02 +08:00
|
|
|
|
2016-11-10 17:48:29 +08:00
|
|
|
In<ELFT>::GotPlt = make<GotPltSection<ELFT>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::GotPlt);
|
2016-12-08 20:58:55 +08:00
|
|
|
In<ELFT>::IgotPlt = make<IgotPltSection<ELFT>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::IgotPlt);
|
2016-11-30 00:05:27 +08:00
|
|
|
|
|
|
|
if (Config->GdbIndex) {
|
|
|
|
In<ELFT>::GdbIndex = make<GdbIndexSection<ELFT>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::GdbIndex);
|
2016-11-30 00:05:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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*/);
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::RelaPlt);
|
2016-11-30 00:05:27 +08:00
|
|
|
|
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*/);
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::RelaIplt);
|
2016-12-08 20:58:55 +08:00
|
|
|
|
2017-02-09 18:56:15 +08:00
|
|
|
In<ELFT>::Plt = make<PltSection<ELFT>>(Target->PltHeaderSize);
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::Plt);
|
2017-02-09 18:56:15 +08:00
|
|
|
In<ELFT>::Iplt = make<PltSection<ELFT>>(0);
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::Iplt);
|
2016-11-30 00:05:27 +08:00
|
|
|
|
|
|
|
if (Config->EhFrameHdr) {
|
|
|
|
In<ELFT>::EhFrameHdr = make<EhFrameHeader<ELFT>>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::EhFrameHdr);
|
2016-11-30 00:05:27 +08:00
|
|
|
}
|
2017-01-14 00:18:15 +08:00
|
|
|
|
2017-02-24 06:06:28 +08:00
|
|
|
if (!Config->Relocatable) {
|
|
|
|
In<ELFT>::EhFrame = make<EhFrameSection<ELFT>>();
|
|
|
|
Add(In<ELFT>::EhFrame);
|
|
|
|
}
|
|
|
|
|
2017-01-14 00:18:15 +08:00
|
|
|
if (In<ELFT>::SymTab)
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::SymTab);
|
|
|
|
Add(In<ELFT>::ShStrTab);
|
2017-01-14 00:18:15 +08:00
|
|
|
if (In<ELFT>::StrTab)
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::StrTab);
|
2016-11-02 07:17:45 +08:00
|
|
|
}
|
|
|
|
|
2016-01-28 02:04:26 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
static bool shouldKeepInSymtab(InputSectionBase *Sec, StringRef SymName,
|
2016-04-04 22:04:16 +08:00
|
|
|
const SymbolBody &B) {
|
2017-02-11 09:40:49 +08:00
|
|
|
if (B.isFile() || B.isSection())
|
2016-01-28 02:04:26 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// If sym references a section in a discarded group, don't keep it.
|
2017-02-24 00:49:07 +08:00
|
|
|
if (Sec == &InputSection::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;
|
|
|
|
|
2017-03-01 03:29:55 +08:00
|
|
|
if (auto *D = dyn_cast<DefinedRegular>(&B)) {
|
2016-05-06 00:40:28 +08:00
|
|
|
// 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");
|
2017-03-01 03:29:55 +08:00
|
|
|
auto *DR = dyn_cast<DefinedRegular>(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
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
InputSectionBase *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;
|
2017-02-28 12:20:16 +08:00
|
|
|
In<ELFT>::SymTab->addSymbol(B);
|
2015-10-09 07:49:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 09:40:49 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
|
|
|
|
// Create one STT_SECTION symbol for each output section we might
|
|
|
|
// have a relocation with.
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2017-03-01 03:43:54 +08:00
|
|
|
if (Sec->Sections.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
InputSection *IS = Sec->Sections[0];
|
|
|
|
if (isa<SyntheticSection>(IS) || IS->Type == SHT_REL ||
|
2017-02-11 09:40:49 +08:00
|
|
|
IS->Type == SHT_RELA)
|
|
|
|
continue;
|
|
|
|
|
2017-03-01 03:36:30 +08:00
|
|
|
auto *Sym =
|
|
|
|
make<DefinedRegular>("", /*IsLocal=*/true, /*StOther=*/0, STT_SECTION,
|
|
|
|
/*Value=*/0, /*Size=*/0, IS, nullptr);
|
|
|
|
In<ELFT>::SymTab->addSymbol(Sym);
|
2017-02-11 09:40:49 +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.
|
2017-02-24 23:07:30 +08:00
|
|
|
static int getMipsSectionRank(const OutputSection *S) {
|
2017-01-17 05:17:23 +08:00
|
|
|
if ((S->Flags & SHF_MIPS_GPREL) == 0)
|
|
|
|
return 0;
|
2017-02-24 22:28:00 +08:00
|
|
|
if (S->Name == ".got")
|
2017-01-17 05:17:23 +08:00
|
|
|
return 1;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2017-02-16 12:51:46 +08:00
|
|
|
// Today's loaders have a feature to make segments read-only after
|
|
|
|
// processing dynamic relocations to enhance security. PT_GNU_RELRO
|
|
|
|
// is defined for that.
|
|
|
|
//
|
|
|
|
// This function returns true if a section needs to be put into a
|
|
|
|
// PT_GNU_RELRO segment.
|
2017-02-24 23:07:30 +08:00
|
|
|
template <class ELFT> bool elf::isRelroSection(const OutputSection *Sec) {
|
2016-02-11 06:43:13 +08:00
|
|
|
if (!Config->ZRelro)
|
|
|
|
return false;
|
2017-02-16 12:51:46 +08:00
|
|
|
|
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;
|
2017-02-16 12:51:46 +08:00
|
|
|
|
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;
|
2017-02-16 12:51:46 +08:00
|
|
|
|
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-02-27 10:31:26 +08:00
|
|
|
if (Sec == Out::BssRelRo)
|
2017-01-10 09:21:50 +08:00
|
|
|
return true;
|
2017-02-16 12:51:46 +08:00
|
|
|
|
2017-02-24 22:28:00 +08:00
|
|
|
StringRef S = Sec->Name;
|
2015-12-11 03:19:04 +08:00
|
|
|
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>
|
2017-02-24 23:07:30 +08:00
|
|
|
static bool compareSectionsNonScript(const OutputSection *A,
|
|
|
|
const OutputSection *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.
|
2017-02-24 22:28:00 +08:00
|
|
|
bool AIsInterp = A->Name == ".interp";
|
|
|
|
bool BIsInterp = B->Name == ".interp";
|
2016-11-03 02:58:44 +08:00
|
|
|
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.
|
2017-02-24 22:28:00 +08:00
|
|
|
auto AAddrSetI = Config->SectionStartMap.find(A->Name);
|
|
|
|
auto BAddrSetI = Config->SectionStartMap.find(B->Name);
|
2016-12-20 05:21:07 +08:00
|
|
|
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)
|
2017-02-24 22:28:00 +08:00
|
|
|
return getPPC64SectionRank(A->Name) < getPPC64SectionRank(B->Name);
|
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>
|
2017-02-24 23:07:30 +08:00
|
|
|
static bool compareSections(const OutputSection *A, const OutputSection *B) {
|
2016-09-22 06:36:19 +08:00
|
|
|
// For now, put sections mentioned in a linker script first.
|
2017-02-24 22:28:00 +08:00
|
|
|
int AIndex = Script<ELFT>::X->getSectionIndex(A->Name);
|
|
|
|
int BIndex = Script<ELFT>::X->getSectionIndex(B->Name);
|
2016-09-22 06:36:19 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
void PhdrEntry::add(OutputSection *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-29 01:48:21 +08:00
|
|
|
static DefinedSynthetic *
|
2017-02-24 23:07:30 +08:00
|
|
|
addOptionalSynthetic(StringRef Name, OutputSection *Sec,
|
2017-01-29 01:48:21 +08:00
|
|
|
typename ELFT::uint Val, uint8_t StOther = STV_HIDDEN) {
|
2017-01-04 10:43:01 +08:00
|
|
|
if (SymbolBody *S = Symtab<ELFT>::X->find(Name))
|
2017-01-18 00:08:06 +08:00
|
|
|
if (!S->isInCurrentDSO())
|
2017-01-29 01:48:21 +08:00
|
|
|
return cast<DefinedSynthetic>(
|
|
|
|
Symtab<ELFT>::X->addSynthetic(Name, Sec, Val, StOther)->body());
|
|
|
|
return nullptr;
|
2016-04-12 21:26:51 +08:00
|
|
|
}
|
|
|
|
|
2016-11-11 19:33:32 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
static Symbol *addRegular(StringRef Name, InputSectionBase *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>
|
2017-02-23 10:28:28 +08:00
|
|
|
static Symbol *addOptionalRegular(StringRef Name, InputSectionBase *IS,
|
2016-11-11 19:33:32 +08:00
|
|
|
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();
|
2017-02-23 10:28:28 +08:00
|
|
|
return addRegular<ELFT>(Name, IS, Value);
|
2016-11-11 19:33:32 +08:00
|
|
|
}
|
|
|
|
|
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
|
2017-03-01 03:29:55 +08:00
|
|
|
ElfSym::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"))
|
2017-03-01 03:29:55 +08:00
|
|
|
ElfSym::MipsGpDisp =
|
2016-12-08 14:19:47 +08:00
|
|
|
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"))
|
2017-03-01 03:29:55 +08:00
|
|
|
ElfSym::MipsLocalGp =
|
2016-12-08 14:19:47 +08:00
|
|
|
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;
|
|
|
|
|
2017-02-24 12:11:02 +08:00
|
|
|
// __ehdr_start is the location of ELF file headers.
|
2017-02-28 18:34:56 +08:00
|
|
|
addOptionalSynthetic<ELFT>("__ehdr_start", Out::ElfHeader, 0);
|
2016-08-23 02:44:04 +08:00
|
|
|
|
2017-01-29 03:07:33 +08:00
|
|
|
auto Define = [](StringRef S, DefinedSynthetic *&Sym1,
|
|
|
|
DefinedSynthetic *&Sym2) {
|
2017-01-29 01:48:21 +08:00
|
|
|
Sym1 = addOptionalSynthetic<ELFT>(S, nullptr, 0, STV_DEFAULT);
|
2016-02-27 00:49:54 +08:00
|
|
|
assert(S.startswith("_"));
|
2016-02-27 00:38:39 +08:00
|
|
|
S = S.substr(1);
|
2017-01-29 01:48:21 +08:00
|
|
|
Sym2 = addOptionalSynthetic<ELFT>(S, nullptr, 0, STV_DEFAULT);
|
2016-02-26 22:36:36 +08:00
|
|
|
};
|
|
|
|
|
2017-03-01 03:29:55 +08:00
|
|
|
Define("_end", ElfSym::End, ElfSym::End2);
|
|
|
|
Define("_etext", ElfSym::Etext, ElfSym::Etext2);
|
|
|
|
Define("_edata", ElfSym::Edata, ElfSym::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))).
|
2017-02-24 23:07:30 +08:00
|
|
|
template <class ELFT> static void sortInitFini(OutputSection *S) {
|
2016-02-11 07:20:42 +08:00
|
|
|
if (S)
|
2017-02-24 23:07:30 +08:00
|
|
|
reinterpret_cast<OutputSection *>(S)->sortInitFini();
|
2016-02-12 07:41:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort input sections by the special rule for .ctors and .dtors.
|
2017-02-24 23:07:30 +08:00
|
|
|
template <class ELFT> static void sortCtorsDtors(OutputSection *S) {
|
2016-02-12 07:41:38 +08:00
|
|
|
if (S)
|
2017-02-24 23:07:30 +08:00
|
|
|
reinterpret_cast<OutputSection *>(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>
|
2017-02-24 23:07:30 +08:00
|
|
|
static void sortBySymbolsOrder(ArrayRef<OutputSection *> 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.
|
2017-02-23 10:32:18 +08:00
|
|
|
DenseMap<InputSectionBase *, int> SectionOrder;
|
2016-11-10 17:05:20 +08:00
|
|
|
for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
|
|
|
|
for (SymbolBody *Body : File->getSymbols()) {
|
2017-03-01 03:29:55 +08:00
|
|
|
auto *D = dyn_cast<DefinedRegular>(Body);
|
2016-11-10 17:05:20 +08:00
|
|
|
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.
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Base : OutputSections)
|
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base))
|
2017-02-23 10:32:18 +08:00
|
|
|
Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); });
|
2016-11-10 17:05:20 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 01:58:07 +08:00
|
|
|
template <class ELFT>
|
2017-02-23 10:28:28 +08:00
|
|
|
void Writer<ELFT>::forEachRelSec(std::function<void(InputSectionBase &)> Fn) {
|
2017-02-27 10:32:08 +08:00
|
|
|
for (InputSectionBase *IS : InputSections) {
|
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;
|
2017-02-24 00:49:07 +08:00
|
|
|
if (isa<InputSection>(IS) || isa<EhInputSection<ELFT>>(IS))
|
2016-11-10 22:53:24 +08:00
|
|
|
Fn(*IS);
|
2016-07-21 01:58:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 17:49:24 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::createSections() {
|
2017-02-27 10:32:08 +08:00
|
|
|
for (InputSectionBase *IS : InputSections)
|
2017-02-17 01:32:26 +08:00
|
|
|
if (IS)
|
2017-02-27 10:31:48 +08:00
|
|
|
Factory.addInputSec<ELFT>(IS, getOutputSectionName(IS->Name));
|
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
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
|
|
|
Sec->assignOffsets<ELFT>();
|
2016-07-20 22:43:20 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:44:48 +08:00
|
|
|
template <class ELFT>
|
2017-02-24 23:07:30 +08:00
|
|
|
static bool canSharePtLoad(const OutputSection &S1, const OutputSection &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 =
|
2017-02-24 23:07:30 +08:00
|
|
|
std::find_if(OutputSections.begin(), E, [](OutputSection *S) {
|
2017-02-24 22:28:00 +08:00
|
|
|
return Script<ELFT>::X->getSectionIndex(S->Name) == INT_MAX;
|
2016-11-08 18:44:48 +08:00
|
|
|
});
|
2016-09-22 06:36:19 +08:00
|
|
|
while (NonScriptI != E) {
|
2016-11-10 07:23:45 +08:00
|
|
|
auto BestPos = std::max_element(
|
2017-02-24 23:07:30 +08:00
|
|
|
I, NonScriptI, [&](OutputSection *&A, OutputSection *&B) {
|
2016-11-10 07:23:45 +08:00
|
|
|
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>
|
2017-02-27 10:56:02 +08:00
|
|
|
static void finalizeSynthetic(const std::vector<SyntheticSection *> &Sections) {
|
|
|
|
for (SyntheticSection *SS : Sections)
|
2016-11-30 00:05:27 +08:00
|
|
|
if (SS && SS->OutSec && !SS->empty()) {
|
2017-02-27 11:07:41 +08:00
|
|
|
SS->finalizeContents();
|
2017-02-24 23:07:30 +08:00
|
|
|
SS->OutSec->template assignOffsets<ELFT>();
|
2016-11-15 20:26:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>
|
2017-02-24 23:07:30 +08:00
|
|
|
static void removeUnusedSyntheticSections(std::vector<OutputSection *> &V) {
|
2017-02-03 21:06:18 +08:00
|
|
|
// All input synthetic sections that can be empty are placed after
|
|
|
|
// all regular ones. We iterate over them all and exit at first
|
|
|
|
// non-synthetic.
|
2017-02-27 10:32:08 +08:00
|
|
|
for (InputSectionBase *S : llvm::reverse(InputSections)) {
|
2017-02-27 10:56:02 +08:00
|
|
|
SyntheticSection *SS = dyn_cast<SyntheticSection>(S);
|
2016-12-06 05:39:35 +08:00
|
|
|
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;
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
OutputSection *OutSec = cast<OutputSection>(SS->OutSec);
|
2016-12-06 05:39:35 +08:00
|
|
|
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() {
|
2017-02-27 10:31:26 +08:00
|
|
|
Out::DebugInfo = findSection(".debug_info");
|
|
|
|
Out::PreinitArray = findSection(".preinit_array");
|
|
|
|
Out::InitArray = findSection(".init_array");
|
|
|
|
Out::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();
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *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)
|
2017-02-23 10:28:28 +08:00
|
|
|
addRegular<ELFT>("_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();
|
|
|
|
|
2017-02-24 06:06:28 +08:00
|
|
|
// This responsible for splitting up .eh_frame section into
|
|
|
|
// pieces. The relocation scan uses those peaces, so this has to be
|
|
|
|
// earlier.
|
|
|
|
finalizeSynthetic<ELFT>({In<ELFT>::EhFrame});
|
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>);
|
|
|
|
|
2017-01-25 18:31:16 +08:00
|
|
|
if (In<ELFT>::Plt && !In<ELFT>::Plt->empty())
|
|
|
|
In<ELFT>::Plt->addSymbols();
|
|
|
|
if (In<ELFT>::Iplt && !In<ELFT>::Iplt->empty())
|
|
|
|
In<ELFT>::Iplt->addSymbols();
|
|
|
|
|
2017-02-20 19:12:33 +08:00
|
|
|
// Now that we have defined all possible global symbols including linker-
|
2015-12-26 18:22:16 +08:00
|
|
|
// 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)
|
2017-02-28 12:20:16 +08:00
|
|
|
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()) {
|
2017-02-28 12:20:16 +08:00
|
|
|
In<ELFT>::DynSymTab->addSymbol(Body);
|
2017-02-27 07:35:34 +08:00
|
|
|
if (auto *SS = dyn_cast<SharedSymbol>(Body))
|
|
|
|
if (cast<SharedFile<ELFT>>(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.
|
2017-02-27 10:31:26 +08:00
|
|
|
// This function adds linker-created Out::* sections.
|
2015-12-26 15:50:41 +08:00
|
|
|
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
|
|
|
|
2017-01-29 01:48:21 +08:00
|
|
|
// This is a bit of a hack. A value of 0 means undef, so we set it
|
|
|
|
// to 1 t make __ehdr_start defined. The section number is not
|
|
|
|
// particularly relevant.
|
2017-02-27 10:31:26 +08:00
|
|
|
Out::ElfHeader->SectionIndex = 1;
|
2017-01-29 01:48:21 +08:00
|
|
|
|
2016-04-06 15:20:45 +08:00
|
|
|
unsigned I = 1;
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2016-04-06 15:20:45 +08:00
|
|
|
Sec->SectionIndex = I++;
|
2017-02-24 22:28:00 +08:00
|
|
|
Sec->ShName = In<ELFT>::ShStrTab->addString(Sec->Name);
|
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();
|
|
|
|
}
|
|
|
|
|
2017-02-20 19:12:33 +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.
|
|
|
|
if (Target->NeedsThunks)
|
|
|
|
createThunks<ELFT>(OutputSections);
|
|
|
|
|
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.
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
|
|
|
Sec->finalize<ELFT>();
|
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() {
|
2017-02-16 12:19:03 +08:00
|
|
|
// Add BSS sections.
|
2017-02-24 23:07:30 +08:00
|
|
|
auto Add = [=](OutputSection *Sec) {
|
2017-02-16 12:12:19 +08:00
|
|
|
if (!Sec->Sections.empty()) {
|
2017-02-24 23:07:30 +08:00
|
|
|
Sec->assignOffsets<ELFT>();
|
2017-02-16 12:12:19 +08:00
|
|
|
OutputSections.push_back(Sec);
|
|
|
|
}
|
|
|
|
};
|
2017-02-27 10:31:26 +08:00
|
|
|
Add(Out::Bss);
|
|
|
|
Add(Out::BssRelRo);
|
2016-11-24 19:43:55 +08:00
|
|
|
|
2017-02-16 12:19:03 +08:00
|
|
|
// ARM ABI requires .ARM.exidx to be terminated by some piece of data.
|
|
|
|
// We have the terminater synthetic section class. Add that at the end.
|
2017-02-24 23:07:30 +08:00
|
|
|
auto *OS = dyn_cast_or_null<OutputSection>(findSection(".ARM.exidx"));
|
2016-11-24 19:43:55 +08:00
|
|
|
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() {
|
2017-02-24 23:07:30 +08:00
|
|
|
auto Define = [&](StringRef Start, StringRef End, OutputSection *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
|
|
|
};
|
|
|
|
|
2017-02-27 10:31:26 +08:00
|
|
|
Define("__preinit_array_start", "__preinit_array_end", Out::PreinitArray);
|
|
|
|
Define("__init_array_start", "__init_array_end", Out::InitArray);
|
|
|
|
Define("__fini_array_start", "__fini_array_end", Out::FiniArray);
|
2016-10-27 18:28:53 +08:00
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
if (OutputSection *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>
|
2017-02-24 23:07:30 +08:00
|
|
|
void Writer<ELFT>::addStartStopSymbols(OutputSection *Sec) {
|
2017-02-24 22:28:00 +08:00
|
|
|
StringRef S = Sec->Name;
|
2015-10-16 01:11:03 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
template <class ELFT> OutputSection *Writer<ELFT>::findSection(StringRef Name) {
|
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-02-24 22:28:00 +08:00
|
|
|
if (Sec->Name == Name)
|
2016-08-09 09:35:38 +08:00
|
|
|
return Sec;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
template <class ELFT> static bool needsPtLoad(OutputSection *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) {
|
2017-02-25 09:52:03 +08:00
|
|
|
if (Config->Omagic)
|
2016-11-29 17:43:51 +08:00
|
|
|
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.
|
2017-02-27 10:31:26 +08:00
|
|
|
AddHdr(PT_PHDR, PF_R)->add(Out::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.
|
2017-02-24 23:07:30 +08:00
|
|
|
if (OutputSection *Sec = findSection(".interp"))
|
2017-02-02 06:42:17 +08:00
|
|
|
AddHdr(PT_INTERP, Sec->getPhdrFlags())->add(Sec);
|
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);
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *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-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());
|
2017-02-24 22:28:00 +08:00
|
|
|
if (Script<ELFT>::X->hasLMA(Sec->Name) || 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);
|
2015-11-05 10:00:35 +08:00
|
|
|
}
|
2015-11-03 08:34:39 +08:00
|
|
|
|
2017-02-02 06:42:17 +08:00
|
|
|
// Add a TLS segment if any.
|
|
|
|
PhdrEntry TlsHdr(PT_TLS, PF_R);
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-02-02 06:42:17 +08:00
|
|
|
if (Sec->Flags & SHF_TLS)
|
|
|
|
TlsHdr.add(Sec);
|
2016-02-11 06:43:13 +08:00
|
|
|
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.
|
2017-02-02 06:42:17 +08:00
|
|
|
if (In<ELFT>::DynSymTab)
|
|
|
|
AddHdr(PT_DYNAMIC, In<ELFT>::Dynamic->OutSec->getPhdrFlags())
|
|
|
|
->add(In<ELFT>::Dynamic->OutSec);
|
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.
|
2017-02-02 06:42:17 +08:00
|
|
|
PhdrEntry RelRo(PT_GNU_RELRO, PF_R);
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-02-02 06:42:17 +08:00
|
|
|
if (needsPtLoad<ELFT>(Sec) && isRelroSection<ELFT>(Sec))
|
|
|
|
RelRo.add(Sec);
|
2016-02-11 06:43:13 +08:00
|
|
|
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.
|
2017-02-24 06:06:28 +08:00
|
|
|
if (!In<ELFT>::EhFrame->empty() && In<ELFT>::EhFrameHdr)
|
2017-02-02 06:42:17 +08:00
|
|
|
AddHdr(PT_GNU_EH_FRAME, In<ELFT>::EhFrameHdr->OutSec->getPhdrFlags())
|
|
|
|
->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.
|
2017-02-24 23:07:30 +08:00
|
|
|
if (OutputSection *Sec = findSection(".openbsd.randomdata"))
|
2017-02-02 06:42:17 +08:00
|
|
|
AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags())->add(Sec);
|
2016-10-14 21:02:22 +08:00
|
|
|
|
2015-11-22 06:19:32 +08:00
|
|
|
// PT_GNU_STACK is a special section to tell the loader to make the
|
2017-02-23 16:09:51 +08:00
|
|
|
// pages for the stack non-executable. If you really want an executable
|
|
|
|
// stack, you can pass -z execstack, but that's not recommended for
|
|
|
|
// security reasons.
|
|
|
|
unsigned Perm;
|
|
|
|
if (Config->ZExecstack)
|
|
|
|
Perm = PF_R | PF_W | PF_X;
|
|
|
|
else
|
|
|
|
Perm = PF_R | PF_W;
|
|
|
|
AddHdr(PT_GNU_STACK, Perm)->p_memsz = Config->ZStackSize;
|
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);
|
|
|
|
|
2017-02-02 04:58:41 +08:00
|
|
|
// Create one PT_NOTE per a group of contiguous .note sections.
|
|
|
|
PhdrEntry *Note = nullptr;
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2017-02-02 04:58:41 +08:00
|
|
|
if (Sec->Type == SHT_NOTE) {
|
2017-02-24 22:28:00 +08:00
|
|
|
if (!Note || Script<ELFT>::X->hasLMA(Sec->Name))
|
2017-02-02 04:58:41 +08:00
|
|
|
Note = AddHdr(PT_NOTE, PF_R);
|
|
|
|
Note->add(Sec);
|
|
|
|
} else {
|
|
|
|
Note = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2016-07-21 03:36:39 +08:00
|
|
|
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(),
|
2017-02-24 23:07:30 +08:00
|
|
|
[](OutputSection *Sec) { return Sec->Type == SHT_ARM_EXIDX; });
|
2016-11-28 08:40:21 +08:00
|
|
|
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;
|
2017-02-24 23:07:30 +08:00
|
|
|
OutputSection *Sec = *(I + 1);
|
2016-11-10 07:23:45 +08:00
|
|
|
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>
|
2017-01-21 04:46:15 +08:00
|
|
|
bool elf::allocateHeaders(std::vector<PhdrEntry> &Phdrs,
|
2017-02-24 23:07:30 +08:00
|
|
|
ArrayRef<OutputSection *> OutputSections,
|
2017-01-21 04:41:18 +08:00
|
|
|
uint64_t Min) {
|
2016-12-20 05:21:07 +08:00
|
|
|
auto FirstPTLoad =
|
|
|
|
std::find_if(Phdrs.begin(), Phdrs.end(),
|
|
|
|
[](const PhdrEntry &E) { return E.p_type == PT_LOAD; });
|
|
|
|
if (FirstPTLoad == Phdrs.end())
|
2017-01-21 04:41:18 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
uint64_t HeaderSize = getHeaderSize<ELFT>();
|
2017-01-21 04:46:15 +08:00
|
|
|
if (HeaderSize > Min) {
|
|
|
|
auto PhdrI =
|
|
|
|
std::find_if(Phdrs.begin(), Phdrs.end(),
|
|
|
|
[](const PhdrEntry &E) { return E.p_type == PT_PHDR; });
|
|
|
|
if (PhdrI != Phdrs.end())
|
|
|
|
Phdrs.erase(PhdrI);
|
2017-01-21 04:41:18 +08:00
|
|
|
return false;
|
2017-01-21 04:46:15 +08:00
|
|
|
}
|
2017-01-21 04:41:18 +08:00
|
|
|
Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
|
|
|
|
|
|
|
|
if (!ScriptConfig->HasSections)
|
|
|
|
Config->ImageBase = Min = std::min(Min, Config->ImageBase);
|
|
|
|
|
2017-02-27 10:31:26 +08:00
|
|
|
Out::ElfHeader->Addr = Min;
|
|
|
|
Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
|
2017-01-21 04:41:18 +08:00
|
|
|
|
|
|
|
if (Script<ELFT>::X->hasPhdrsCommands())
|
|
|
|
return true;
|
|
|
|
|
2016-12-20 05:21:07 +08:00
|
|
|
if (FirstPTLoad->First)
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2016-12-20 05:21:07 +08:00
|
|
|
if (Sec->FirstInPtLoad == FirstPTLoad->First)
|
2017-02-27 10:31:26 +08:00
|
|
|
Sec->FirstInPtLoad = Out::ElfHeader;
|
|
|
|
FirstPTLoad->First = Out::ElfHeader;
|
2016-12-20 05:21:07 +08:00
|
|
|
if (!FirstPTLoad->Last)
|
2017-02-27 10:31:26 +08:00
|
|
|
FirstPTLoad->Last = Out::ProgramHeaders;
|
2017-01-21 04:41:18 +08:00
|
|
|
return true;
|
2016-12-20 05:21:07 +08:00
|
|
|
}
|
|
|
|
|
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() {
|
2017-02-27 10:31:26 +08:00
|
|
|
Out::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
|
|
|
|
|
|
|
// When -T<section> option is specified, lower the base to make room for those
|
|
|
|
// sections.
|
2017-01-21 04:41:18 +08:00
|
|
|
uint64_t Min = -1;
|
|
|
|
if (!Config->SectionStartMap.empty())
|
2016-12-20 05:21:07 +08:00
|
|
|
for (const auto &P : Config->SectionStartMap)
|
|
|
|
Min = std::min(Min, P.second);
|
|
|
|
|
2017-01-21 04:41:18 +08:00
|
|
|
AllocateHeader = allocateHeaders<ELFT>(Phdrs, OutputSections, Min);
|
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;
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *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
|
|
|
|
2017-02-24 22:28:00 +08:00
|
|
|
auto I = Config->SectionStartMap.find(Sec->Name);
|
2016-09-14 21:07:13 +08:00
|
|
|
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>
|
2017-02-24 23:07:30 +08:00
|
|
|
static uintX_t getFileAlignment(uintX_t Off, OutputSection *Sec) {
|
|
|
|
OutputSection *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>
|
2017-02-24 23:07:30 +08:00
|
|
|
static uintX_t setOffset(OutputSection *Sec, uintX_t Off) {
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Type == SHT_NOBITS) {
|
|
|
|
Sec->Offset = Off;
|
2017-02-15 07:35:42 +08:00
|
|
|
return Off;
|
2016-08-25 17:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Off = getFileAlignment<ELFT>(Off, Sec);
|
2016-11-09 09:42:41 +08:00
|
|
|
Sec->Offset = Off;
|
2017-02-15 07:35:42 +08:00
|
|
|
return Off + Sec->Size;
|
2016-08-25 17:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
|
|
|
|
uintX_t Off = 0;
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Flags & SHF_ALLOC)
|
2017-02-15 07:35:42 +08:00
|
|
|
Off = 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;
|
2017-02-27 10:31:26 +08:00
|
|
|
Off = setOffset<ELFT>(Out::ElfHeader, Off);
|
|
|
|
Off = setOffset<ELFT>(Out::ProgramHeaders, Off);
|
2016-04-06 15:20:45 +08:00
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-02-15 07:35:42 +08:00
|
|
|
Off = 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) {
|
2017-02-24 23:07:30 +08:00
|
|
|
OutputSection *First = P.First;
|
|
|
|
OutputSection *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.
|
2017-01-31 02:20:07 +08:00
|
|
|
P.p_memsz = alignTo(P.p_memsz, Target->PageSize);
|
2017-01-05 02:56:15 +08:00
|
|
|
}
|
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) {
|
2017-02-27 10:31:26 +08:00
|
|
|
Out::TlsPhdr = &P;
|
2016-12-20 01:01:01 +08:00
|
|
|
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
|
2017-02-24 23:07:30 +08:00
|
|
|
if (OutputSection *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() {
|
2017-02-14 13:45:47 +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
|
2017-01-29 01:48:21 +08:00
|
|
|
// to each section. This function fixes some predefined
|
2015-12-26 18:52:26 +08:00
|
|
|
// symbol values that depend on section address and size.
|
2017-01-29 01:48:21 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::fixPredefinedSymbols() {
|
2017-02-24 23:07:30 +08:00
|
|
|
auto Set = [](DefinedSynthetic *S1, DefinedSynthetic *S2, OutputSection *Sec,
|
|
|
|
uint64_t Value) {
|
2017-01-29 01:48:21 +08:00
|
|
|
if (S1) {
|
|
|
|
S1->Section = Sec;
|
|
|
|
S1->Value = Value;
|
|
|
|
}
|
|
|
|
if (S2) {
|
|
|
|
S2->Section = Sec;
|
|
|
|
S2->Value = Value;
|
|
|
|
}
|
2016-04-22 04:50:15 +08:00
|
|
|
};
|
|
|
|
|
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.
|
2017-01-29 01:48:21 +08:00
|
|
|
PhdrEntry *Last = nullptr;
|
|
|
|
PhdrEntry *LastRO = nullptr;
|
|
|
|
PhdrEntry *LastRW = nullptr;
|
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;
|
2017-01-29 01:48:21 +08:00
|
|
|
Last = &P;
|
2016-12-20 01:01:01 +08:00
|
|
|
if (P.p_flags & PF_W)
|
2017-01-29 01:48:21 +08:00
|
|
|
LastRW = &P;
|
2016-04-14 22:37:59 +08:00
|
|
|
else
|
2017-01-29 01:48:21 +08:00
|
|
|
LastRO = &P;
|
2016-02-26 22:36:36 +08:00
|
|
|
}
|
2017-01-29 01:48:21 +08:00
|
|
|
if (Last)
|
2017-03-01 03:29:55 +08:00
|
|
|
Set(ElfSym::End, ElfSym::End2, Last->First, Last->p_memsz);
|
2017-01-29 01:48:21 +08:00
|
|
|
if (LastRO)
|
2017-03-01 03:29:55 +08:00
|
|
|
Set(ElfSym::Etext, ElfSym::Etext2, LastRO->First, LastRO->p_filesz);
|
2017-01-29 01:48:21 +08:00
|
|
|
if (LastRW)
|
2017-03-01 03:29:55 +08:00
|
|
|
Set(ElfSym::Edata, ElfSym::Edata2, LastRW->First, LastRW->p_filesz);
|
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) {
|
2017-03-01 03:29:55 +08:00
|
|
|
if (!ElfSym::MipsGp->Value) {
|
2016-12-13 05:34:11 +08:00
|
|
|
// Find GP-relative section with the lowest address
|
|
|
|
// and use this address to calculate default _gp value.
|
|
|
|
uintX_t Gp = -1;
|
2017-02-24 23:07:30 +08:00
|
|
|
for (const OutputSection *OS : OutputSections)
|
2016-12-13 05:34:11 +08:00
|
|
|
if ((OS->Flags & SHF_MIPS_GPREL) && OS->Addr < Gp)
|
|
|
|
Gp = OS->Addr;
|
|
|
|
if (Gp != (uintX_t)-1)
|
2017-03-01 03:29:55 +08:00
|
|
|
ElfSym::MipsGp->Value = Gp + 0x7ff0;
|
2016-12-13 05:34:11 +08:00
|
|
|
}
|
2017-03-01 03:29:55 +08:00
|
|
|
if (ElfSym::MipsGpDisp)
|
|
|
|
ElfSym::MipsGpDisp->Value = ElfSym::MipsGp->Value;
|
|
|
|
if (ElfSym::MipsLocalGp)
|
|
|
|
ElfSym::MipsLocalGp->Value = ElfSym::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);
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2016-11-10 07:23:45 +08:00
|
|
|
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();
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Flags & SHF_ALLOC)
|
2017-02-24 23:07:30 +08:00
|
|
|
Sec->writeTo<ELFT>(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.
|
2017-02-27 10:31:26 +08:00
|
|
|
Out::Opd = findSection(".opd");
|
|
|
|
if (Out::Opd) {
|
|
|
|
Out::OpdBuf = Buf + Out::Opd->Offset;
|
|
|
|
Out::Opd->template writeTo<ELFT>(Buf + Out::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
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
OutputSection *EhFrameHdr =
|
2016-11-21 23:52:10 +08:00
|
|
|
In<ELFT>::EhFrameHdr ? In<ELFT>::EhFrameHdr->OutSec : nullptr;
|
2017-02-11 09:40:49 +08:00
|
|
|
|
|
|
|
// In -r or -emit-relocs mode, write the relocation sections first as in
|
|
|
|
// ELf_Rel targets we might find out that we need to modify the relocated
|
|
|
|
// section while doing it.
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-02-11 09:40:49 +08:00
|
|
|
if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
|
2017-02-24 23:07:30 +08:00
|
|
|
Sec->writeTo<ELFT>(Buf + Sec->Offset);
|
2017-02-11 09:40:49 +08:00
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-02-27 10:31:26 +08:00
|
|
|
if (Sec != Out::Opd && Sec != EhFrameHdr && Sec->Type != SHT_REL &&
|
2017-02-11 09:40:49 +08:00
|
|
|
Sec->Type != SHT_RELA)
|
2017-02-24 23:07:30 +08:00
|
|
|
Sec->writeTo<ELFT>(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.
|
2017-02-24 06:06:28 +08:00
|
|
|
if (EhFrameHdr)
|
2017-02-24 23:07:30 +08:00
|
|
|
EhFrameHdr->writeTo<ELFT>(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
|
|
|
|
2017-01-21 04:46:15 +08:00
|
|
|
template bool elf::allocateHeaders<ELF32LE>(std::vector<PhdrEntry> &,
|
2017-02-24 23:07:30 +08:00
|
|
|
ArrayRef<OutputSection *>,
|
2017-01-21 04:41:18 +08:00
|
|
|
uint64_t);
|
2017-01-21 04:46:15 +08:00
|
|
|
template bool elf::allocateHeaders<ELF32BE>(std::vector<PhdrEntry> &,
|
2017-02-24 23:07:30 +08:00
|
|
|
ArrayRef<OutputSection *>,
|
2017-01-21 04:41:18 +08:00
|
|
|
uint64_t);
|
2017-01-21 04:46:15 +08:00
|
|
|
template bool elf::allocateHeaders<ELF64LE>(std::vector<PhdrEntry> &,
|
2017-02-24 23:07:30 +08:00
|
|
|
ArrayRef<OutputSection *>,
|
2017-01-21 04:41:18 +08:00
|
|
|
uint64_t);
|
2017-01-21 04:46:15 +08:00
|
|
|
template bool elf::allocateHeaders<ELF64BE>(std::vector<PhdrEntry> &,
|
2017-02-24 23:07:30 +08:00
|
|
|
ArrayRef<OutputSection *>,
|
2017-01-21 04:41:18 +08:00
|
|
|
uint64_t);
|
2016-12-20 05:21:07 +08:00
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
template bool elf::isRelroSection<ELF32LE>(const OutputSection *);
|
|
|
|
template bool elf::isRelroSection<ELF32BE>(const OutputSection *);
|
|
|
|
template bool elf::isRelroSection<ELF64LE>(const OutputSection *);
|
|
|
|
template bool elf::isRelroSection<ELF64BE>(const OutputSection *);
|