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"
|
2017-03-24 08:15:16 +08:00
|
|
|
#include "Filesystem.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"
|
2017-04-17 16:58:12 +08:00
|
|
|
#include "Threads.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-02-06 08:06:26 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-09-29 09:45:22 +08:00
|
|
|
#include <climits>
|
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::Shdr Elf_Shdr;
|
|
|
|
typedef typename ELFT::Ehdr Elf_Ehdr;
|
|
|
|
typedef typename ELFT::Phdr Elf_Phdr;
|
2017-04-06 05:46:06 +08:00
|
|
|
|
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);
|
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);
|
2017-04-06 05:37:09 +08:00
|
|
|
uint64_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
|
|
|
|
2017-04-06 05:37:09 +08:00
|
|
|
uint64_t FileSize;
|
|
|
|
uint64_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));
|
2017-04-27 12:01:14 +08:00
|
|
|
return Saver.save(V.drop_back() + Inner);
|
2017-02-17 08:28:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 03:59:21 +08:00
|
|
|
for (StringRef V :
|
2017-03-17 18:14:53 +08:00
|
|
|
{".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
|
|
|
|
".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
|
2016-09-20 03:59:21 +08:00
|
|
|
".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_"))
|
2017-04-27 12:01:14 +08:00
|
|
|
return Saver.save("." + 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() &&
|
2017-03-20 18:09:58 +08:00
|
|
|
!Config->DynamicLinker.empty() && !Script->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;
|
2017-04-06 05:37:09 +08:00
|
|
|
uint64_t Size = P.Last->Addr + P.Last->Size - P.First->Addr;
|
2016-12-06 21:43:34 +08:00
|
|
|
return Size == 0;
|
|
|
|
});
|
|
|
|
Phdrs.erase(I, Phdrs.end());
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:06:18 +08:00
|
|
|
// 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.
|
2017-04-06 05:37:09 +08:00
|
|
|
static void combineMergableSections() {
|
2017-03-07 04:23:56 +08:00
|
|
|
std::vector<MergeSyntheticSection *> MergeSections;
|
2017-02-27 10:32:08 +08:00
|
|
|
for (InputSectionBase *&S : InputSections) {
|
2017-03-07 04:23:56 +08:00
|
|
|
MergeInputSection *MS = dyn_cast<MergeInputSection>(S);
|
2017-02-03 21:06:18 +08:00
|
|
|
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-04-06 05:45:47 +08:00
|
|
|
uint64_t Flags = MS->Flags & ~(uint64_t)(SHF_GROUP | SHF_COMPRESSED);
|
2017-04-06 05:37:09 +08:00
|
|
|
uint32_t Alignment = std::max<uint32_t>(MS->Alignment, MS->Entsize);
|
2017-02-03 21:06:18 +08:00
|
|
|
|
|
|
|
auto I =
|
2017-03-07 04:23:56 +08:00
|
|
|
llvm::find_if(MergeSections, [=](MergeSyntheticSection *Sec) {
|
2017-02-03 21:06:18 +08:00
|
|
|
return Sec->Name == OutsecName && Sec->Flags == Flags &&
|
|
|
|
Sec->Alignment == Alignment;
|
|
|
|
});
|
|
|
|
if (I == MergeSections.end()) {
|
2017-03-07 04:23:56 +08:00
|
|
|
MergeSyntheticSection *Syn =
|
|
|
|
make<MergeSyntheticSection>(OutsecName, MS->Type, Flags, Alignment);
|
2017-02-03 21:06:18 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2017-03-11 04:00:42 +08:00
|
|
|
template <class ELFT> static void combineEhFrameSections() {
|
|
|
|
for (InputSectionBase *&S : InputSections) {
|
|
|
|
EhInputSection *ES = dyn_cast<EhInputSection>(S);
|
2017-03-15 20:31:54 +08:00
|
|
|
if (!ES || !ES->Live)
|
2017-03-11 04:00:42 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
In<ELFT>::EhFrame->addSection(ES);
|
|
|
|
S = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<InputSectionBase *> &V = InputSections;
|
|
|
|
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-04-06 05:37:09 +08:00
|
|
|
combineMergableSections();
|
2016-11-15 16:19:02 +08:00
|
|
|
|
2017-03-11 04:00:42 +08:00
|
|
|
if (!Config->Relocatable)
|
|
|
|
combineEhFrameSections<ELFT>();
|
|
|
|
|
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.
|
2017-03-20 18:09:58 +08:00
|
|
|
Script->OutputSections = &OutputSections;
|
2017-03-22 07:03:09 +08:00
|
|
|
if (Script->Opt.HasSections) {
|
2016-11-21 10:11:05 +08:00
|
|
|
// If linker script contains SECTIONS commands, let it create sections.
|
2017-03-20 18:09:58 +08:00
|
|
|
Script->processCommands(Factory);
|
2016-11-21 10:11:05 +08:00
|
|
|
|
|
|
|
// Linker scripts may have left some input sections unassigned.
|
|
|
|
// Assign such sections using the default rule.
|
2017-03-20 18:09:58 +08:00
|
|
|
Script->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();
|
2017-03-20 18:09:58 +08:00
|
|
|
Script->processCommands(Factory);
|
2016-09-16 23:30:47 +08:00
|
|
|
}
|
2016-07-30 00:18:47 +08:00
|
|
|
|
2016-09-23 21:17:16 +08:00
|
|
|
if (Config->Discard != DiscardPolicy::All)
|
|
|
|
copyLocalSymbols();
|
|
|
|
|
2017-03-18 07:29:01 +08:00
|
|
|
if (Config->CopyRelocs)
|
2017-02-11 09:40:49 +08:00
|
|
|
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 {
|
2017-04-19 20:46:32 +08:00
|
|
|
if (!Script->Opt.HasSections) {
|
2016-04-16 18:10:32 +08:00
|
|
|
fixSectionAlignments();
|
2017-04-19 20:46:32 +08:00
|
|
|
Script->fabricateDefaultCommands(Config->MaxPageSize);
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
2017-04-19 20:46:32 +08:00
|
|
|
Script->assignAddresses(Phdrs);
|
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
|
|
|
|
2017-03-15 17:32:36 +08:00
|
|
|
In<ELFT>::DynStrTab = make<StringTableSection>(".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>>(
|
2017-03-18 07:29:01 +08:00
|
|
|
Config->IsRela ? ".rela.dyn" : ".rel.dyn", Config->ZCombreloc);
|
2017-03-15 17:32:36 +08:00
|
|
|
In<ELFT>::ShStrTab = make<StringTableSection>(".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);
|
2017-04-06 05:37:09 +08:00
|
|
|
Out::ProgramHeaders->updateAlignment(Config->Wordsize);
|
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) {
|
2017-03-15 17:32:36 +08:00
|
|
|
In<ELFT>::StrTab = make<StringTableSection>(".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) {
|
2017-03-21 00:40:21 +08:00
|
|
|
In<ELFT>::BuildId = make<BuildIdSection>();
|
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-03-17 21:31:07 +08:00
|
|
|
In<ELFT>::Common = createCommonSection<ELFT>();
|
|
|
|
if (In<ELFT>::Common)
|
|
|
|
Add(InX::Common);
|
2016-11-10 05:36:56 +08:00
|
|
|
|
2017-03-17 18:14:53 +08:00
|
|
|
In<ELFT>::Bss = make<BssSection>(".bss");
|
|
|
|
Add(In<ELFT>::Bss);
|
|
|
|
In<ELFT>::BssRelRo = make<BssSection>(".bss.rel.ro");
|
|
|
|
Add(In<ELFT>::BssRelRo);
|
|
|
|
|
2016-11-22 12:28:39 +08:00
|
|
|
// Add MIPS-specific sections.
|
2017-03-18 07:29:01 +08:00
|
|
|
bool HasDynSymTab = !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) {
|
2017-03-15 20:02:31 +08:00
|
|
|
In<ELFT>::MipsRldMap = make<MipsRldMapSection>();
|
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) {
|
2017-03-21 00:44:28 +08:00
|
|
|
In<ELFT>::MipsGot = make<MipsGotSection>();
|
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
|
|
|
|
2017-03-15 17:12:56 +08:00
|
|
|
In<ELFT>::GotPlt = make<GotPltSection>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::GotPlt);
|
2017-03-15 17:12:56 +08:00
|
|
|
In<ELFT>::IgotPlt = make<IgotPltSection>();
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::IgotPlt);
|
2016-11-30 00:05:27 +08:00
|
|
|
|
|
|
|
if (Config->GdbIndex) {
|
2017-03-21 16:19:34 +08:00
|
|
|
In<ELFT>::GdbIndex = make<GdbIndexSection>();
|
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>>(
|
2017-03-18 07:29:01 +08:00
|
|
|
Config->IsRela ? ".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-03-17 19:01:57 +08:00
|
|
|
In<ELFT>::Plt = make<PltSection>(Target->PltHeaderSize);
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::Plt);
|
2017-03-17 19:01:57 +08:00
|
|
|
In<ELFT>::Iplt = make<PltSection>(0);
|
2017-02-05 13:18:58 +08:00
|
|
|
Add(In<ELFT>::Iplt);
|
2016-11-30 00:05:27 +08:00
|
|
|
|
2017-02-24 06:06:28 +08:00
|
|
|
if (!Config->Relocatable) {
|
2017-03-09 16:45:25 +08:00
|
|
|
if (Config->EhFrameHdr) {
|
|
|
|
In<ELFT>::EhFrameHdr = make<EhFrameHeader<ELFT>>();
|
|
|
|
Add(In<ELFT>::EhFrameHdr);
|
|
|
|
}
|
2017-02-24 06:06:28 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-09 06:36:28 +08:00
|
|
|
static bool shouldKeepInSymtab(SectionBase *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
|
|
|
}
|
|
|
|
|
2017-03-16 19:20:02 +08:00
|
|
|
static bool includeInSymtab(const SymbolBody &B) {
|
2016-05-06 00:40:28 +08:00
|
|
|
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.
|
2017-03-09 06:36:28 +08:00
|
|
|
SectionBase *Sec = D->Section;
|
|
|
|
if (!Sec)
|
2016-05-06 00:40:28 +08:00
|
|
|
return true;
|
2017-03-09 06:36:28 +08:00
|
|
|
if (auto *IS = dyn_cast<InputSectionBase>(Sec)) {
|
|
|
|
Sec = IS->Repl;
|
|
|
|
IS = cast<InputSectionBase>(Sec);
|
|
|
|
// Exclude symbols pointing to garbage-collected sections.
|
|
|
|
if (!IS->Live)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (auto *S = dyn_cast<MergeInputSection>(Sec))
|
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;
|
2017-03-16 19:20:02 +08:00
|
|
|
if (!includeInSymtab(*B))
|
2016-05-06 00:38:46 +08:00
|
|
|
continue;
|
2016-11-24 02:07:33 +08:00
|
|
|
|
2017-03-09 06:36:28 +08:00
|
|
|
SectionBase *Sec = DR->Section;
|
2017-03-16 19:20:02 +08:00
|
|
|
if (!shouldKeepInSymtab(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;
|
2017-04-13 13:40:07 +08:00
|
|
|
|
|
|
|
// Non-allocatable or non-writable sections don't need RELRO because
|
|
|
|
// they are not writable or not even mapped to memory in the first place.
|
|
|
|
// RELRO is for sections that are essentially read-only but need to
|
|
|
|
// be writable only at process startup to allow dynamic linker to
|
|
|
|
// apply relocations.
|
2015-11-24 18:15:50 +08:00
|
|
|
if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
|
|
|
|
return false;
|
2017-04-13 13:40:07 +08:00
|
|
|
|
|
|
|
// Once initialized, TLS data segments are used as data templates
|
|
|
|
// for a thread-local storage. For each new thread, runtime
|
|
|
|
// allocates memory for a TLS and copy templates there. No thread
|
|
|
|
// are supposed to use templates directly. Thus, it can be in RELRO.
|
2015-12-11 03:13:08 +08:00
|
|
|
if (Flags & SHF_TLS)
|
|
|
|
return true;
|
2017-02-16 12:51:46 +08:00
|
|
|
|
2017-04-13 13:40:07 +08:00
|
|
|
// .init_array, .preinit_array and .fini_array contain pointers to
|
|
|
|
// functions that are executed on process startup or exit. These
|
|
|
|
// pointers are set by the static linker, and they are not expected
|
|
|
|
// to change at runtime. But if you are an attacker, you could do
|
|
|
|
// interesting things by manipulating pointers in .fini_array, for
|
|
|
|
// example. So they are put into RELRO.
|
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
|
|
|
|
2017-04-13 13:40:07 +08:00
|
|
|
// .got contains pointers to external symbols. They are resolved by
|
|
|
|
// the dynamic linker when a module is loaded into memory, and after
|
|
|
|
// that they are not expected to change. So, it can be in RELRO.
|
|
|
|
if (In<ELFT>::Got && Sec == In<ELFT>::Got->OutSec)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// .got.plt contains pointers to external function symbols. They are
|
|
|
|
// by default resolved lazily, so we usually cannot put it into RELRO.
|
|
|
|
// However, if "-z now" is given, the lazy symbol resolution is
|
|
|
|
// disabled, which enables us to put it into RELRO.
|
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;
|
2017-04-13 13:40:07 +08:00
|
|
|
|
|
|
|
// .dynamic section contains data for the dynamic linker, and
|
|
|
|
// there's no need to write to it at runtime, so it's better to put
|
|
|
|
// it into RELRO.
|
2016-11-17 05:01:02 +08:00
|
|
|
if (Sec == In<ELFT>::Dynamic->OutSec)
|
|
|
|
return true;
|
2017-04-13 13:40:07 +08:00
|
|
|
|
|
|
|
// .bss.rel.ro is used for copy relocations for read-only symbols.
|
|
|
|
// Since the dynamic linker needs to process copy relocations, the
|
|
|
|
// section cannot be read-only, but once initialized, they shouldn't
|
|
|
|
// change.
|
2017-03-17 18:14:53 +08:00
|
|
|
if (Sec == In<ELFT>::BssRelRo->OutSec)
|
2017-01-10 09:21:50 +08:00
|
|
|
return true;
|
2017-02-16 12:51:46 +08:00
|
|
|
|
2017-04-13 13:40:07 +08:00
|
|
|
// Sections with some special names are put into RELRO. This is a
|
|
|
|
// bit unfortunate because section names shouldn't be significant in
|
|
|
|
// ELF in spirit. But in reality many linker features depend on
|
|
|
|
// magic section names.
|
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-03-20 18:09:58 +08:00
|
|
|
int AIndex = Script->getSectionIndex(A->Name);
|
|
|
|
int BIndex = Script->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;
|
2017-03-07 22:55:52 +08:00
|
|
|
p_align = std::max(p_align, Sec->Alignment);
|
2016-12-20 01:01:01 +08:00
|
|
|
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-03-09 06:36:28 +08:00
|
|
|
static Symbol *addRegular(StringRef Name, SectionBase *Sec, uint64_t Value,
|
|
|
|
uint8_t StOther = STV_HIDDEN,
|
|
|
|
uint8_t Binding = STB_WEAK) {
|
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.
|
2017-03-09 06:36:28 +08:00
|
|
|
return Symtab<ELFT>::X->addRegular(Name, StOther, STT_NOTYPE, Value,
|
|
|
|
/*Size=*/0, Binding, Sec,
|
2016-11-23 14:59:47 +08:00
|
|
|
/*File=*/nullptr);
|
2016-11-11 19:33:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2017-03-09 06:36:28 +08:00
|
|
|
static DefinedRegular *
|
|
|
|
addOptionalRegular(StringRef Name, SectionBase *Sec, uint64_t Val,
|
|
|
|
uint8_t StOther = STV_HIDDEN, uint8_t Binding = STB_GLOBAL) {
|
2016-11-11 19:33:32 +08:00
|
|
|
SymbolBody *S = Symtab<ELFT>::X->find(Name);
|
|
|
|
if (!S)
|
|
|
|
return nullptr;
|
2017-01-18 00:08:06 +08:00
|
|
|
if (S->isInCurrentDSO())
|
2017-03-09 06:36:28 +08:00
|
|
|
return nullptr;
|
|
|
|
return cast<DefinedRegular>(
|
|
|
|
addRegular<ELFT>(Name, Sec, Val, StOther, Binding)->body());
|
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;
|
2017-03-18 07:29:01 +08:00
|
|
|
StringRef S = Config->IsRela ? "__rela_iplt_start" : "__rel_iplt_start";
|
2017-03-09 06:36:28 +08:00
|
|
|
addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, 0, STV_HIDDEN, STB_WEAK);
|
2015-12-26 17:47:57 +08:00
|
|
|
|
2017-03-18 07:29:01 +08:00
|
|
|
S = Config->IsRela ? "__rela_iplt_end" : "__rel_iplt_end";
|
2017-03-09 06:36:28 +08:00
|
|
|
addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, -1, STV_HIDDEN, STB_WEAK);
|
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
|
2017-03-21 05:03:43 +08:00
|
|
|
// start of function and 'gp' pointer into GOT.
|
2016-12-08 14:19:47 +08:00
|
|
|
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
|
2017-04-25 12:44:54 +08:00
|
|
|
// to avoid the undefined symbol error.
|
|
|
|
if (!In<ELFT>::DynSymTab)
|
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.
|
2017-03-22 07:03:09 +08:00
|
|
|
if (Script->Opt.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-03-09 06:36:28 +08:00
|
|
|
addOptionalRegular<ELFT>("__ehdr_start", Out::ElfHeader, 0, STV_HIDDEN);
|
2016-08-23 02:44:04 +08:00
|
|
|
|
2017-04-14 05:23:03 +08:00
|
|
|
auto Add = [](StringRef S) {
|
|
|
|
return addOptionalRegular<ELFT>(S, Out::ElfHeader, 0, STV_DEFAULT);
|
2016-02-26 22:36:36 +08:00
|
|
|
};
|
|
|
|
|
2017-04-14 05:23:03 +08:00
|
|
|
ElfSym::Bss = Add("__bss_start");
|
2017-04-14 05:37:56 +08:00
|
|
|
ElfSym::End1 = Add("end");
|
|
|
|
ElfSym::End2 = Add("_end");
|
|
|
|
ElfSym::Etext1 = Add("etext");
|
|
|
|
ElfSym::Etext2 = Add("_etext");
|
|
|
|
ElfSym::Edata1 = Add("edata");
|
|
|
|
ElfSym::Edata2 = Add("_edata");
|
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-04-06 05:37:09 +08:00
|
|
|
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-04-06 05:37:09 +08:00
|
|
|
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-03-09 06:36:28 +08:00
|
|
|
DenseMap<SectionBase *, 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-03-07 05:17:18 +08:00
|
|
|
if (isa<InputSection>(IS) || isa<EhInputSection>(IS))
|
2016-11-10 22:53:24 +08:00
|
|
|
Fn(*IS);
|
2016-07-21 01:58:07 +08:00
|
|
|
}
|
2017-03-11 04:00:42 +08:00
|
|
|
|
|
|
|
if (!Config->Relocatable) {
|
|
|
|
for (EhInputSection *ES : In<ELFT>::EhFrame->Sections)
|
|
|
|
Fn(*ES);
|
|
|
|
}
|
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-03-14 17:30:25 +08:00
|
|
|
Factory.addInputSec(IS, getOutputSectionName(IS->Name));
|
2016-08-11 15:56:43 +08:00
|
|
|
|
2016-11-10 17:05:20 +08:00
|
|
|
sortBySymbolsOrder<ELFT>(OutputSections);
|
2017-04-06 05:37:09 +08:00
|
|
|
sortInitFini(findSection(".init_array"));
|
|
|
|
sortInitFini(findSection(".fini_array"));
|
|
|
|
sortCtorsDtors(findSection(".ctors"));
|
|
|
|
sortCtorsDtors(findSection(".dtors"));
|
2016-08-11 15:56:43 +08:00
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-03-16 18:24:54 +08:00
|
|
|
Sec->assignOffsets();
|
2016-07-20 22:43:20 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2017-03-22 07:03:09 +08:00
|
|
|
if (!Script->Opt.HasSections) {
|
2016-09-22 06:36:19 +08:00
|
|
|
std::stable_sort(OutputSections.begin(), OutputSections.end(),
|
|
|
|
compareSectionsNonScript<ELFT>);
|
|
|
|
return;
|
|
|
|
}
|
2017-03-20 18:09:58 +08:00
|
|
|
Script->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-03-20 18:09:58 +08:00
|
|
|
return Script->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) {
|
2017-03-16 19:20:02 +08:00
|
|
|
bool ACanSharePtLoad = canSharePtLoad(**NonScriptI, *A);
|
|
|
|
bool BCanSharePtLoad = canSharePtLoad(**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
|
|
|
|
2017-03-20 18:09:58 +08:00
|
|
|
Script->adjustSectionsAfterSorting();
|
2016-09-22 06:36:19 +08:00
|
|
|
}
|
|
|
|
|
2017-03-08 22:06:24 +08:00
|
|
|
static void applySynthetic(const std::vector<SyntheticSection *> &Sections,
|
|
|
|
std::function<void(SyntheticSection *)> Fn) {
|
2017-02-27 10:56:02 +08:00
|
|
|
for (SyntheticSection *SS : Sections)
|
2016-11-30 00:05:27 +08:00
|
|
|
if (SS && SS->OutSec && !SS->empty()) {
|
2017-03-08 22:06:24 +08:00
|
|
|
Fn(SS);
|
2017-03-16 18:24:54 +08:00
|
|
|
SS->OutSec->assignOffsets();
|
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
|
2017-04-06 17:40:03 +08:00
|
|
|
// sometimes. This function filters out such unused sections from the output.
|
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-04-06 17:40:03 +08:00
|
|
|
SS->OutSec->Sections.erase(std::find(SS->OutSec->Sections.begin(),
|
|
|
|
SS->OutSec->Sections.end(), SS));
|
|
|
|
// If there are no other sections in the output section, remove it from the
|
|
|
|
// output.
|
|
|
|
if (SS->OutSec->Sections.empty())
|
|
|
|
V.erase(std::find(V.begin(), V.end(), SS->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
|
2017-03-08 22:06:24 +08:00
|
|
|
// pieces. The relocation scan uses those pieces, so this has to be
|
2017-02-24 06:06:28 +08:00
|
|
|
// earlier.
|
2017-03-16 18:29:44 +08:00
|
|
|
applySynthetic({In<ELFT>::EhFrame},
|
|
|
|
[](SyntheticSection *SS) { SS->finalizeContents(); });
|
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
|
|
|
|
2017-03-16 19:20:02 +08:00
|
|
|
if (!includeInSymtab(*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();
|
2017-03-16 19:20:02 +08:00
|
|
|
removeUnusedSyntheticSections(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) {
|
2017-03-20 18:09:58 +08:00
|
|
|
Phdrs = Script->hasPhdrsCommands() ? Script->createPhdrs() : createPhdrs();
|
2016-12-20 05:21:07 +08:00
|
|
|
addPtArmExid(Phdrs);
|
|
|
|
fixHeaders();
|
|
|
|
}
|
|
|
|
|
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.
|
2017-03-17 18:14:53 +08:00
|
|
|
applySynthetic({In<ELFT>::DynSymTab, In<ELFT>::Bss, In<ELFT>::BssRelRo,
|
|
|
|
In<ELFT>::GnuHashTab, In<ELFT>::HashTab, 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},
|
2017-03-16 18:29:44 +08:00
|
|
|
[](SyntheticSection *SS) { SS->finalizeContents(); });
|
2017-03-08 22:06:24 +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) {
|
|
|
|
// FIXME: only ARM Interworking and Mips LA25 Thunks are implemented,
|
|
|
|
// these
|
|
|
|
// do not require address information. To support range extension Thunks
|
|
|
|
// we need to assign addresses so that we can tell if jump instructions
|
|
|
|
// are out of range. This will need to turn into a loop that converges
|
|
|
|
// when no more Thunks are added
|
2017-04-05 18:30:09 +08:00
|
|
|
ThunkCreator<ELFT> TC;
|
|
|
|
if (TC.createThunks(OutputSections))
|
2017-03-16 18:29:44 +08:00
|
|
|
applySynthetic({In<ELFT>::MipsGot},
|
|
|
|
[](SyntheticSection *SS) { SS->updateAllocSize(); });
|
2017-03-08 22:06:24 +08:00
|
|
|
}
|
|
|
|
// Fill other section headers. The dynamic table is finalized
|
|
|
|
// at the end because some tags like RELSZ depend on result
|
|
|
|
// of finalizing other sections.
|
|
|
|
for (OutputSection *Sec : OutputSections)
|
|
|
|
Sec->finalize<ELFT>();
|
|
|
|
|
2017-04-19 19:32:27 +08:00
|
|
|
// If -compressed-debug-sections is specified, we need to compress
|
|
|
|
// .debug_* sections. Do it right now because it changes the size of
|
|
|
|
// output sections.
|
2017-04-17 16:58:12 +08:00
|
|
|
parallelForEach(OutputSections.begin(), OutputSections.end(),
|
|
|
|
[](OutputSection *S) { S->maybeCompress<ELFT>(); });
|
|
|
|
|
2017-03-08 22:06:24 +08:00
|
|
|
// createThunks may have added local symbols to the static symbol table
|
2017-03-16 18:29:44 +08:00
|
|
|
applySynthetic({In<ELFT>::SymTab, In<ELFT>::ShStrTab, In<ELFT>::StrTab},
|
|
|
|
[](SyntheticSection *SS) { SS->postThunkContents(); });
|
2015-12-26 15:50:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
|
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)
|
2017-03-21 17:01:39 +08:00
|
|
|
OS->addSection(make<ARMExidxSentinelSection>());
|
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.
|
2017-03-14 00:40:20 +08:00
|
|
|
if (OS) {
|
|
|
|
addOptionalRegular<ELFT>(Start, OS, 0);
|
|
|
|
addOptionalRegular<ELFT>(End, OS, -1);
|
|
|
|
} else {
|
2017-03-18 07:29:01 +08:00
|
|
|
if (Config->Pic)
|
2017-03-14 00:40:20 +08:00
|
|
|
OS = Out::ElfHeader;
|
|
|
|
addOptionalRegular<ELFT>(Start, OS, 0);
|
|
|
|
addOptionalRegular<ELFT>(End, OS, 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;
|
2017-03-09 06:36:28 +08:00
|
|
|
addOptionalRegular<ELFT>(Saver.save("__start_" + S), Sec, 0, STV_DEFAULT);
|
|
|
|
addOptionalRegular<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-03-16 19:20:02 +08:00
|
|
|
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.
|
2017-04-06 05:37:09 +08:00
|
|
|
static uint64_t computeFlags(uint64_t Flags) {
|
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;
|
2017-04-06 05:37:09 +08:00
|
|
|
if (Config->SingleRoRx && !(Flags & PF_W))
|
|
|
|
return Flags | PF_X;
|
|
|
|
return Flags;
|
2016-09-20 23:22:27 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2017-04-06 05:37:09 +08:00
|
|
|
uint64_t Flags = computeFlags(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;
|
2017-03-16 19:20:02 +08:00
|
|
|
if (!needsPtLoad(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.
|
2017-04-06 05:37:09 +08:00
|
|
|
uint64_t NewFlags = computeFlags(Sec->getPhdrFlags());
|
2017-03-20 18:09:58 +08:00
|
|
|
if (Script->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-03-16 19:20:02 +08:00
|
|
|
if (needsPtLoad(Sec) && isRelroSection<ELFT>(Sec))
|
2017-02-02 06:42:17 +08:00
|
|
|
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-03-14 16:49:09 +08:00
|
|
|
if (!In<ELFT>::EhFrame->empty() && In<ELFT>::EhFrameHdr &&
|
|
|
|
In<ELFT>::EhFrame->OutSec && In<ELFT>::EhFrameHdr->OutSec)
|
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
|
|
|
|
2017-03-24 08:15:57 +08:00
|
|
|
// PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes
|
|
|
|
// the dynamic linker fill the segment with random data.
|
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-03-20 18:09:58 +08:00
|
|
|
if (!Note || Script->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);
|
2017-03-16 19:20:02 +08:00
|
|
|
if (needsPtLoad(Sec))
|
2016-03-31 03:41:51 +08:00
|
|
|
Sec->PageAlign = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2017-03-13 22:40:58 +08:00
|
|
|
uint64_t HeaderSize = getHeaderSize();
|
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);
|
|
|
|
|
2017-03-22 07:03:09 +08:00
|
|
|
if (!Script->Opt.HasSections)
|
2017-01-21 04:41:18 +08:00
|
|
|
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
|
|
|
|
2017-03-20 18:09:58 +08:00
|
|
|
if (Script->hasPhdrsCommands())
|
2017-01-21 04:41:18 +08:00
|
|
|
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.
|
2017-03-22 07:03:09 +08:00
|
|
|
if (Script->Opt.HasSections)
|
2016-11-22 03:59:33 +08:00
|
|
|
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-03-14 17:03:53 +08:00
|
|
|
AllocateHeader = allocateHeaders(Phdrs, OutputSections, Min);
|
2016-04-06 15:20:45 +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.
|
2017-04-06 05:37:09 +08:00
|
|
|
static uint64_t getFileAlignment(uint64_t Off, OutputSection *Sec) {
|
2017-02-24 23:07:30 +08:00
|
|
|
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)
|
2017-03-07 22:55:52 +08:00
|
|
|
return alignTo(Off, Sec->Alignment);
|
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
|
|
|
}
|
|
|
|
|
2017-04-06 05:37:09 +08:00
|
|
|
static uint64_t setOffset(OutputSection *Sec, uint64_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
|
|
|
}
|
|
|
|
|
2017-04-06 05:37:09 +08:00
|
|
|
Off = getFileAlignment(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() {
|
2017-04-06 05:37:09 +08:00
|
|
|
uint64_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-04-06 05:37:09 +08:00
|
|
|
Off = setOffset(Sec, Off);
|
|
|
|
FileSize = alignTo(Off, Config->Wordsize);
|
2016-08-25 17:05:47 +08:00
|
|
|
}
|
|
|
|
|
2016-04-02 01:07:17 +08:00
|
|
|
// Assign file offsets to output sections.
|
|
|
|
template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
|
2017-04-06 05:37:09 +08:00
|
|
|
uint64_t Off = 0;
|
|
|
|
Off = setOffset(Out::ElfHeader, Off);
|
|
|
|
Off = setOffset(Out::ProgramHeaders, Off);
|
2016-04-06 15:20:45 +08:00
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-04-06 05:37:09 +08:00
|
|
|
Off = setOffset(Sec, Off);
|
2016-07-01 18:27:36 +08:00
|
|
|
|
2017-04-06 05:37:09 +08:00
|
|
|
SectionHeaderOff = alignTo(Off, Config->Wordsize);
|
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.
|
2017-04-06 05:37:09 +08:00
|
|
|
template <class ELFT> uint64_t 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))
|
2017-03-17 19:56:54 +08:00
|
|
|
return B->getVA();
|
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
|
|
|
static uint16_t getELFType() {
|
2017-03-18 07:29:01 +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-03-09 06:36:28 +08:00
|
|
|
auto Set = [](DefinedRegular *S1, DefinedRegular *S2, OutputSection *Sec,
|
2017-02-24 23:07:30 +08:00
|
|
|
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-04-14 05:37:56 +08:00
|
|
|
Set(ElfSym::End1, ElfSym::End2, Last->First, Last->p_memsz);
|
2017-01-29 01:48:21 +08:00
|
|
|
if (LastRO)
|
2017-04-14 05:37:56 +08:00
|
|
|
Set(ElfSym::Etext1, ElfSym::Etext2, LastRO->First, LastRO->p_filesz);
|
2017-01-29 01:48:21 +08:00
|
|
|
if (LastRW)
|
2017-04-14 05:37:56 +08:00
|
|
|
Set(ElfSym::Edata1, ElfSym::Edata2, LastRW->First, LastRW->p_filesz);
|
2016-11-24 06:22:16 +08:00
|
|
|
|
2017-04-05 18:03:25 +08:00
|
|
|
if (ElfSym::Bss)
|
|
|
|
ElfSym::Bss->Section = findSection(".bss");
|
|
|
|
|
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.
|
2017-04-06 05:37:09 +08:00
|
|
|
uint64_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;
|
2017-04-06 05:37:09 +08:00
|
|
|
if (Gp != (uint64_t)-1)
|
2017-03-01 03:29:55 +08:00
|
|
|
ElfSym::MipsGp->Value = Gp + 0x7ff0;
|
2016-12-13 05:34:11 +08:00
|
|
|
}
|
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);
|
2017-04-06 05:08:47 +08:00
|
|
|
EHdr->e_ident[EI_CLASS] = Config->Is64 ? ELFCLASS64 : ELFCLASS32;
|
|
|
|
EHdr->e_ident[EI_DATA] = Config->IsLE ? ELFDATA2LSB : ELFDATA2MSB;
|
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
|
|
|
// Open a result file.
|
2016-04-02 01:24:19 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::openFile() {
|
2017-04-06 05:37:09 +08:00
|
|
|
if (!Config->Is64 && FileSize > UINT32_MAX) {
|
|
|
|
error("output file too large: " + Twine(FileSize) + " bytes");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-04-07 18:36:42 +08:00
|
|
|
if (EhFrameHdr && !EhFrameHdr->Sections.empty())
|
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-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 *);
|