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-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();
|
2017-09-01 10:23:31 +08:00
|
|
|
void addPredefinedSymbols();
|
2015-12-25 15:38:58 +08:00
|
|
|
|
2017-07-27 15:46:50 +08:00
|
|
|
std::vector<PhdrEntry *> createPhdrs();
|
2016-12-06 21:43:34 +08:00
|
|
|
void removeEmptyPTLoad();
|
2017-07-27 15:46:50 +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-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();
|
2017-08-03 00:35:00 +08:00
|
|
|
void writeTrapInstr();
|
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-07-07 00:40:44 +08:00
|
|
|
OutputSectionFactory Factory;
|
2016-02-11 06:43:13 +08:00
|
|
|
|
2015-12-26 17:47:57 +08:00
|
|
|
void addRelIpltSymbols();
|
2015-12-26 17:48:00 +08:00
|
|
|
void addStartEndSymbols();
|
2017-02-24 23:07:30 +08:00
|
|
|
void addStartStopSymbols(OutputSection *Sec);
|
2017-04-06 05:37:09 +08:00
|
|
|
uint64_t getEntryAddr();
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *findSection(StringRef Name);
|
2015-10-11 06:34:30 +08:00
|
|
|
|
2017-07-27 15:46:50 +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;
|
2017-06-26 18:22:17 +08:00
|
|
|
|
|
|
|
bool HasGotBaseSym = false;
|
2015-08-06 07:24:46 +08:00
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-10-29 04:57:25 +08:00
|
|
|
StringRef elf::getOutputSectionName(StringRef Name) {
|
2017-06-05 20:49:21 +08:00
|
|
|
// ".zdebug_" is a prefix for ZLIB-compressed sections.
|
|
|
|
// Because we decompressed input sections, we want to remove 'z'.
|
|
|
|
if (Name.startswith(".zdebug_"))
|
|
|
|
return Saver.save("." + Name.substr(2));
|
|
|
|
|
2016-10-05 18:10:45 +08:00
|
|
|
if (Config->Relocatable)
|
|
|
|
return Name;
|
|
|
|
|
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.",
|
2017-06-28 17:12:38 +08:00
|
|
|
".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) {
|
2016-09-20 03:59:21 +08:00
|
|
|
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-07-12 16:50:42 +08:00
|
|
|
return Name;
|
|
|
|
}
|
|
|
|
|
2016-07-21 19:01:23 +08:00
|
|
|
template <class ELFT> static bool needsInterpSection() {
|
2017-07-27 02:42:48 +08:00
|
|
|
return !SharedFile<ELFT>::Instances.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() {
|
2017-08-28 17:28:15 +08:00
|
|
|
llvm::erase_if(Phdrs, [&](const PhdrEntry *P) {
|
2017-07-27 15:46:50 +08:00
|
|
|
if (P->p_type != PT_LOAD)
|
2016-12-06 21:43:34 +08:00
|
|
|
return false;
|
2017-09-07 19:01:10 +08:00
|
|
|
if (!P->FirstSec)
|
2016-12-08 11:17:05 +08:00
|
|
|
return true;
|
2017-09-07 19:01:10 +08:00
|
|
|
uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr;
|
2016-12-06 21:43:34 +08:00
|
|
|
return Size == 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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();
|
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-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.
|
2017-03-20 18:09:58 +08:00
|
|
|
Script->processCommands(Factory);
|
2017-07-06 07:36:24 +08:00
|
|
|
createSections();
|
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
|
|
|
|
2017-05-25 02:08:04 +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-08-04 16:30:16 +08:00
|
|
|
parallelForEach(OutputSections,
|
2017-07-28 03:22:43 +08:00
|
|
|
[](OutputSection *Sec) { Sec->maybeCompress<ELFT>(); });
|
2017-06-02 00:30:12 +08:00
|
|
|
|
2017-09-01 10:23:31 +08:00
|
|
|
// Generate assignments for predefined symbols (e.g. _end or _etext)
|
|
|
|
// before assigning addresses. These symbols may be referred to from
|
|
|
|
// the linker script and we need to ensure they have the correct value
|
|
|
|
// prior evaluating any expressions using these symbols.
|
|
|
|
addPredefinedSymbols();
|
|
|
|
|
2017-07-05 17:12:54 +08:00
|
|
|
Script->assignAddresses();
|
|
|
|
Script->allocateHeaders(Phdrs);
|
2016-08-25 17:05:47 +08:00
|
|
|
|
2017-06-07 10:24:08 +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-12-06 21:43:34 +08:00
|
|
|
|
2017-06-07 10:24:08 +08:00
|
|
|
if (!Config->OFormatBinary)
|
|
|
|
assignFileOffsets();
|
|
|
|
else
|
|
|
|
assignFileOffsetsBinary();
|
2016-08-25 17:05:47 +08:00
|
|
|
|
2017-06-07 10:24:08 +08:00
|
|
|
setPhdrs();
|
|
|
|
|
|
|
|
if (Config->Relocatable) {
|
2017-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
|
|
|
Sec->Addr = 0;
|
2017-06-07 10:24:08 +08:00
|
|
|
} else {
|
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;
|
2017-06-02 00:32:58 +08:00
|
|
|
|
2016-08-25 17:05:47 +08:00
|
|
|
if (!Config->OFormatBinary) {
|
2017-08-03 00:35:00 +08:00
|
|
|
writeTrapInstr();
|
2016-08-25 17:05:47 +08:00
|
|
|
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-07-28 03:22:43 +08:00
|
|
|
writeMapFile<ELFT>();
|
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-05-12 06:02:41 +08:00
|
|
|
InX::DynStrTab = make<StringTableSection>(".dynstr", true);
|
2017-05-12 07:16:43 +08:00
|
|
|
InX::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-05-12 06:02:41 +08:00
|
|
|
InX::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-05-12 06:02:41 +08:00
|
|
|
InX::Interp = createInterpSection();
|
|
|
|
Add(InX::Interp);
|
2016-11-06 07:05:47 +08:00
|
|
|
} else {
|
2017-05-12 06:02:41 +08:00
|
|
|
InX::Interp = nullptr;
|
2016-11-06 07:05:47 +08:00
|
|
|
}
|
2016-11-02 07:17:45 +08:00
|
|
|
|
|
|
|
if (Config->Strip != StripPolicy::All) {
|
2017-05-12 06:02:41 +08:00
|
|
|
InX::StrTab = make<StringTableSection>(".strtab", false);
|
2017-05-16 16:53:30 +08:00
|
|
|
InX::SymTab = make<SymbolTableSection<ELFT>>(*InX::StrTab);
|
2016-11-02 07:17:45 +08:00
|
|
|
}
|
|
|
|
|
2016-11-22 08:54:15 +08:00
|
|
|
if (Config->BuildId != BuildIdKind::None) {
|
2017-05-12 06:02:41 +08:00
|
|
|
InX::BuildId = make<BuildIdSection>();
|
|
|
|
Add(InX::BuildId);
|
2016-11-22 08:54:15 +08:00
|
|
|
}
|
2016-11-06 07:05:47 +08:00
|
|
|
|
2017-05-12 06:02:41 +08:00
|
|
|
InX::Common = createCommonSection<ELFT>();
|
|
|
|
if (InX::Common)
|
2017-03-17 21:31:07 +08:00
|
|
|
Add(InX::Common);
|
2016-11-10 05:36:56 +08:00
|
|
|
|
2017-05-12 06:02:41 +08:00
|
|
|
InX::Bss = make<BssSection>(".bss");
|
|
|
|
Add(InX::Bss);
|
|
|
|
InX::BssRelRo = make<BssSection>(".bss.rel.ro");
|
|
|
|
Add(InX::BssRelRo);
|
2017-03-17 18:14:53 +08:00
|
|
|
|
2016-11-22 12:28:39 +08:00
|
|
|
// Add MIPS-specific sections.
|
2017-07-27 02:42:48 +08:00
|
|
|
bool HasDynSymTab = !SharedFile<ELFT>::Instances.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-05-12 06:02:41 +08:00
|
|
|
InX::MipsRldMap = make<MipsRldMapSection>();
|
|
|
|
Add(InX::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) {
|
2017-05-16 18:04:42 +08:00
|
|
|
InX::DynSymTab = make<SymbolTableSection<ELFT>>(*InX::DynStrTab);
|
|
|
|
Add(InX::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) {
|
2017-05-16 18:04:42 +08:00
|
|
|
InX::GnuHashTab = make<GnuHashTableSection>();
|
|
|
|
Add(InX::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-05-12 07:16:43 +08:00
|
|
|
Add(InX::Dynamic);
|
2017-05-12 06:02:41 +08:00
|
|
|
Add(InX::DynStrTab);
|
2017-02-05 13:18:58 +08:00
|
|
|
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-05-12 05:33:30 +08:00
|
|
|
InX::MipsGot = make<MipsGotSection>();
|
|
|
|
Add(InX::MipsGot);
|
2016-11-25 16:05:41 +08:00
|
|
|
} else {
|
2017-05-19 00:45:36 +08:00
|
|
|
InX::Got = make<GotSection>();
|
2017-05-12 07:26:03 +08:00
|
|
|
Add(InX::Got);
|
2016-11-25 16:05:41 +08:00
|
|
|
}
|
2016-11-17 05:01:02 +08:00
|
|
|
|
2017-05-12 05:23:38 +08:00
|
|
|
InX::GotPlt = make<GotPltSection>();
|
|
|
|
Add(InX::GotPlt);
|
2017-05-12 06:02:41 +08:00
|
|
|
InX::IgotPlt = make<IgotPltSection>();
|
|
|
|
Add(InX::IgotPlt);
|
2016-11-30 00:05:27 +08:00
|
|
|
|
|
|
|
if (Config->GdbIndex) {
|
2017-07-13 07:56:53 +08:00
|
|
|
InX::GdbIndex = createGdbIndex<ELFT>();
|
2017-05-12 06:02:41 +08:00
|
|
|
Add(InX::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-05-12 06:02:41 +08:00
|
|
|
InX::Plt = make<PltSection>(Target->PltHeaderSize);
|
|
|
|
Add(InX::Plt);
|
|
|
|
InX::Iplt = make<PltSection>(0);
|
|
|
|
Add(InX::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-05-16 18:04:42 +08:00
|
|
|
if (InX::SymTab)
|
|
|
|
Add(InX::SymTab);
|
2017-05-12 06:02:41 +08:00
|
|
|
Add(InX::ShStrTab);
|
|
|
|
if (InX::StrTab)
|
|
|
|
Add(InX::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;
|
2017-08-10 23:54:27 +08:00
|
|
|
return true;
|
2016-05-06 00:40:28 +08:00
|
|
|
}
|
2017-08-10 23:54:27 +08:00
|
|
|
|
|
|
|
if (auto *Sym = dyn_cast<DefinedCommon>(&B))
|
|
|
|
return Sym->Live;
|
2016-05-06 00:40:28 +08:00
|
|
|
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() {
|
2017-05-16 18:04:42 +08:00
|
|
|
if (!InX::SymTab)
|
2016-01-21 11:07:38 +08:00
|
|
|
return;
|
2017-07-27 06:13:32 +08:00
|
|
|
for (ObjFile<ELFT> *F : ObjFile<ELFT>::Instances) {
|
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-05-16 18:04:42 +08:00
|
|
|
InX::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-07-05 03:08:40 +08:00
|
|
|
for (BaseCommand *Base : Script->Opt.Commands) {
|
2017-07-28 03:22:43 +08:00
|
|
|
auto *Sec = dyn_cast<OutputSection>(Base);
|
|
|
|
if (!Sec)
|
2017-03-01 03:43:54 +08:00
|
|
|
continue;
|
2017-07-28 03:22:43 +08:00
|
|
|
auto I = llvm::find_if(Sec->Commands, [](BaseCommand *Base) {
|
2017-07-05 03:08:40 +08:00
|
|
|
if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
|
|
|
|
return !ISD->Sections.empty();
|
|
|
|
return false;
|
|
|
|
});
|
2017-07-28 03:22:43 +08:00
|
|
|
if (I == Sec->Commands.end())
|
2017-07-05 03:08:40 +08:00
|
|
|
continue;
|
|
|
|
InputSection *IS = cast<InputSectionDescription>(*I)->Sections[0];
|
2017-03-01 03:43:54 +08:00
|
|
|
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,
|
2017-08-05 06:31:42 +08:00
|
|
|
/*Value=*/0, /*Size=*/0, IS);
|
2017-05-16 18:04:42 +08:00
|
|
|
InX::SymTab->addSymbol(Sym);
|
2017-02-11 09:40:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-25 07:55:33 +08:00
|
|
|
static bool 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.
|
2017-06-01 04:17:44 +08:00
|
|
|
if (InX::Got && Sec == InX::Got->getParent())
|
2017-04-13 13:40:07 +08:00
|
|
|
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.
|
2017-06-01 04:17:44 +08:00
|
|
|
if (Sec == InX::GotPlt->getParent())
|
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.
|
2017-06-01 04:17:44 +08:00
|
|
|
if (Sec == InX::Dynamic->getParent())
|
2016-11-17 05:01:02 +08:00
|
|
|
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-06-01 04:17:44 +08:00
|
|
|
if (Sec == InX::BssRelRo->getParent())
|
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
|
|
|
}
|
|
|
|
|
2017-05-12 22:52:22 +08:00
|
|
|
// We compute a rank for each section. The rank indicates where the
|
|
|
|
// section should be placed in the file. Instead of using simple
|
|
|
|
// numbers (0,1,2...), we use a series of flags. One for each decision
|
|
|
|
// point when placing the section.
|
|
|
|
// Using flags has two key properties:
|
|
|
|
// * It is easy to check if a give branch was taken.
|
|
|
|
// * It is easy two see how similar two ranks are (see getRankProximity).
|
|
|
|
enum RankFlags {
|
2017-05-27 01:23:25 +08:00
|
|
|
RF_NOT_ADDR_SET = 1 << 16,
|
|
|
|
RF_NOT_INTERP = 1 << 15,
|
|
|
|
RF_NOT_ALLOC = 1 << 14,
|
|
|
|
RF_WRITE = 1 << 13,
|
|
|
|
RF_EXEC_WRITE = 1 << 12,
|
2017-05-19 00:20:12 +08:00
|
|
|
RF_EXEC = 1 << 11,
|
|
|
|
RF_NON_TLS_BSS = 1 << 10,
|
|
|
|
RF_NON_TLS_BSS_RO = 1 << 9,
|
|
|
|
RF_NOT_TLS = 1 << 8,
|
|
|
|
RF_BSS = 1 << 7,
|
|
|
|
RF_PPC_NOT_TOCBSS = 1 << 6,
|
|
|
|
RF_PPC_OPD = 1 << 5,
|
|
|
|
RF_PPC_TOCL = 1 << 4,
|
|
|
|
RF_PPC_TOC = 1 << 3,
|
|
|
|
RF_PPC_BRANCH_LT = 1 << 2,
|
|
|
|
RF_MIPS_GPREL = 1 << 1,
|
|
|
|
RF_MIPS_NOT_GOT = 1 << 0
|
2017-05-12 22:52:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned getSectionRank(const OutputSection *Sec) {
|
|
|
|
unsigned Rank = 0;
|
|
|
|
|
|
|
|
// We want to put section specified by -T option first, so we
|
|
|
|
// can start assigning VA starting from them later.
|
|
|
|
if (Config->SectionStartMap.count(Sec->Name))
|
|
|
|
return Rank;
|
|
|
|
Rank |= RF_NOT_ADDR_SET;
|
|
|
|
|
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-05-12 22:52:22 +08:00
|
|
|
if (Sec->Name == ".interp")
|
|
|
|
return Rank;
|
|
|
|
Rank |= RF_NOT_INTERP;
|
2016-11-03 02:58:44 +08:00
|
|
|
|
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.
|
2017-05-12 22:52:22 +08:00
|
|
|
if (!(Sec->Flags & SHF_ALLOC))
|
|
|
|
return Rank | RF_NOT_ALLOC;
|
2016-12-20 05:21:07 +08:00
|
|
|
|
2017-05-27 01:23:25 +08:00
|
|
|
// Sort sections based on their access permission in the following
|
|
|
|
// order: R, RX, RWX, RW. This order is based on the following
|
|
|
|
// considerations:
|
|
|
|
// * Read-only sections come first such that they go in the
|
|
|
|
// PT_LOAD covering the program headers at the start of the file.
|
|
|
|
// * Read-only, executable sections come next, unless the
|
|
|
|
// -no-rosegment option is used.
|
|
|
|
// * Writable, executable sections follow such that .plt on
|
|
|
|
// architectures where it needs to be writable will be placed
|
|
|
|
// between .text and .data.
|
|
|
|
// * Writable sections come last, such that .bss lands at the very
|
|
|
|
// end of the last PT_LOAD.
|
|
|
|
bool IsExec = Sec->Flags & SHF_EXECINSTR;
|
|
|
|
bool IsWrite = Sec->Flags & SHF_WRITE;
|
|
|
|
|
|
|
|
if (IsExec) {
|
|
|
|
if (IsWrite)
|
|
|
|
Rank |= RF_EXEC_WRITE;
|
|
|
|
else if (!Config->SingleRoRx)
|
2017-05-12 22:52:22 +08:00
|
|
|
Rank |= RF_EXEC;
|
2017-05-27 01:23:25 +08:00
|
|
|
} else {
|
|
|
|
if (IsWrite)
|
|
|
|
Rank |= RF_WRITE;
|
2016-09-30 06:48:55 +08:00
|
|
|
}
|
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
|
|
|
|
2017-05-12 22:52:22 +08:00
|
|
|
bool IsTls = Sec->Flags & SHF_TLS;
|
|
|
|
bool IsNoBits = Sec->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.
|
2017-05-12 22:52:22 +08:00
|
|
|
bool IsNonTlsNoBits = IsNoBits && !IsTls;
|
|
|
|
if (IsNonTlsNoBits)
|
|
|
|
Rank |= RF_NON_TLS_BSS;
|
2017-01-10 09:21:30 +08:00
|
|
|
|
|
|
|
// 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.
|
2017-05-12 22:52:22 +08:00
|
|
|
bool IsRelRo = isRelroSection(Sec);
|
|
|
|
if (IsNonTlsNoBits && !IsRelRo)
|
|
|
|
Rank |= RF_NON_TLS_BSS_RO;
|
|
|
|
if (!IsNonTlsNoBits && IsRelRo)
|
|
|
|
Rank |= RF_NON_TLS_BSS_RO;
|
2017-01-10 09:21:30 +08:00
|
|
|
|
|
|
|
// 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.
|
2017-05-12 22:52:22 +08:00
|
|
|
if (!IsTls)
|
|
|
|
Rank |= RF_NOT_TLS;
|
2017-01-10 09:21:30 +08:00
|
|
|
|
|
|
|
// Within the TLS initialization block, the non-nobits sections need to appear
|
|
|
|
// first.
|
2017-05-12 22:52:22 +08:00
|
|
|
if (IsNoBits)
|
|
|
|
Rank |= RF_BSS;
|
|
|
|
|
2017-08-19 00:15:36 +08:00
|
|
|
// Some architectures have additional ordering restrictions for sections
|
|
|
|
// within the same PT_LOAD.
|
2017-05-12 22:52:22 +08:00
|
|
|
if (Config->EMachine == EM_PPC64) {
|
|
|
|
// 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).
|
|
|
|
StringRef Name = Sec->Name;
|
|
|
|
if (Name != ".tocbss")
|
|
|
|
Rank |= RF_PPC_NOT_TOCBSS;
|
|
|
|
|
|
|
|
if (Name == ".opd")
|
|
|
|
Rank |= RF_PPC_OPD;
|
|
|
|
|
|
|
|
if (Name == ".toc1")
|
|
|
|
Rank |= RF_PPC_TOCL;
|
|
|
|
|
|
|
|
if (Name == ".toc")
|
|
|
|
Rank |= RF_PPC_TOC;
|
|
|
|
|
|
|
|
if (Name == ".branch_lt")
|
|
|
|
Rank |= RF_PPC_BRANCH_LT;
|
|
|
|
}
|
|
|
|
if (Config->EMachine == EM_MIPS) {
|
|
|
|
// All sections with SHF_MIPS_GPREL flag should be grouped together
|
|
|
|
// because data in these sections is addressable with a gp relative address.
|
|
|
|
if (Sec->Flags & SHF_MIPS_GPREL)
|
|
|
|
Rank |= RF_MIPS_GPREL;
|
2015-11-24 18:15:50 +08:00
|
|
|
|
2017-05-12 22:52:22 +08:00
|
|
|
if (Sec->Name != ".got")
|
|
|
|
Rank |= RF_MIPS_NOT_GOT;
|
|
|
|
}
|
2015-10-14 03:07:29 +08:00
|
|
|
|
2017-05-12 22:52:22 +08:00
|
|
|
return Rank;
|
|
|
|
}
|
|
|
|
|
2017-06-16 05:51:01 +08:00
|
|
|
static bool compareSections(const BaseCommand *ACmd, const BaseCommand *BCmd) {
|
2017-07-28 03:22:43 +08:00
|
|
|
const OutputSection *A = cast<OutputSection>(ACmd);
|
|
|
|
const OutputSection *B = cast<OutputSection>(BCmd);
|
2017-05-12 22:52:22 +08:00
|
|
|
if (A->SortRank != B->SortRank)
|
|
|
|
return A->SortRank < B->SortRank;
|
|
|
|
if (!(A->SortRank & RF_NOT_ADDR_SET))
|
|
|
|
return Config->SectionStartMap.lookup(A->Name) <
|
|
|
|
Config->SectionStartMap.lookup(B->Name);
|
2016-09-22 06:36:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
void PhdrEntry::add(OutputSection *Sec) {
|
2017-09-07 19:01:10 +08:00
|
|
|
LastSec = Sec;
|
|
|
|
if (!FirstSec)
|
|
|
|
FirstSec = 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)
|
2017-09-07 18:53:07 +08:00
|
|
|
Sec->PtLoad = this;
|
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-07-27 02:42:48 +08:00
|
|
|
return Symtab->addRegular<ELFT>(Name, StOther, STT_NOTYPE, Value,
|
|
|
|
/*Size=*/0, Binding, Sec,
|
|
|
|
/*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) {
|
2017-07-27 02:42:48 +08:00
|
|
|
SymbolBody *S = Symtab->find(Name);
|
2016-11-11 19:33:32 +08:00
|
|
|
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() {
|
2017-08-05 13:01:07 +08:00
|
|
|
if (!Config->Static)
|
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-07-27 02:42:48 +08:00
|
|
|
ElfSym::MipsGp = Symtab->addAbsolute<ELFT>("_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.
|
2017-07-27 02:42:48 +08:00
|
|
|
if (Symtab->find("_gp_disp"))
|
2017-03-01 03:29:55 +08:00
|
|
|
ElfSym::MipsGpDisp =
|
2017-07-27 02:42:48 +08:00
|
|
|
Symtab->addAbsolute<ELFT>("_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
|
2017-07-27 02:42:48 +08:00
|
|
|
if (Symtab->find("__gnu_local_gp"))
|
2017-03-01 03:29:55 +08:00
|
|
|
ElfSym::MipsLocalGp =
|
2017-07-27 02:42:48 +08:00
|
|
|
Symtab->addAbsolute<ELFT>("__gnu_local_gp", STV_HIDDEN, STB_LOCAL);
|
2016-04-04 22:04:16 +08:00
|
|
|
}
|
|
|
|
|
2017-06-26 18:22:17 +08:00
|
|
|
// The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
|
|
|
|
// be at some offset from the base of the .got section, usually 0 or the end
|
|
|
|
// of the .got
|
2017-06-26 23:11:24 +08:00
|
|
|
InputSection *GotSection = InX::MipsGot ? cast<InputSection>(InX::MipsGot)
|
|
|
|
: cast<InputSection>(InX::Got);
|
|
|
|
ElfSym::GlobalOffsetTable = addOptionalRegular<ELFT>(
|
|
|
|
"_GLOBAL_OFFSET_TABLE_", GotSection, Target->GotBaseSymOff);
|
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.
|
2017-05-16 18:04:42 +08:00
|
|
|
if (!InX::DynSymTab)
|
2017-07-27 02:42:48 +08:00
|
|
|
Symtab->addIgnored<ELFT>("__tls_get_addr");
|
2015-12-26 15:50:39 +08:00
|
|
|
|
2017-05-11 00:20:33 +08:00
|
|
|
// __ehdr_start is the location of ELF file headers. Note that we define
|
|
|
|
// this symbol unconditionally even when using a linker script, which
|
|
|
|
// differs from the behavior implemented by GNU linker which only define
|
|
|
|
// this symbol if ELF headers are in the memory mapped segment.
|
2017-06-07 00:18:48 +08:00
|
|
|
// __executable_start is not documented, but the expectation of at
|
|
|
|
// least the android libc is that it points to the elf header too.
|
|
|
|
// __dso_handle symbol is passed to cxa_finalize as a marker to identify
|
|
|
|
// each DSO. The address of the symbol doesn't matter as long as they are
|
|
|
|
// different in different DSOs, so we chose the start address of the DSO.
|
|
|
|
for (const char *Name :
|
|
|
|
{"__ehdr_start", "__executable_start", "__dso_handle"})
|
|
|
|
addOptionalRegular<ELFT>(Name, Out::ElfHeader, 0, STV_HIDDEN);
|
2017-05-11 00:20:33 +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-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-07-28 03:22:43 +08:00
|
|
|
static void sortInitFini(OutputSection *Cmd) {
|
2017-07-06 07:36:24 +08:00
|
|
|
if (Cmd)
|
|
|
|
Cmd->sortInitFini();
|
2016-02-12 07:41:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort input sections by the special rule for .ctors and .dtors.
|
2017-07-28 03:22:43 +08:00
|
|
|
static void sortCtorsDtors(OutputSection *Cmd) {
|
2017-07-06 07:36:24 +08:00
|
|
|
if (Cmd)
|
|
|
|
Cmd->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.
|
2017-07-06 07:36:24 +08:00
|
|
|
template <class ELFT> static void sortBySymbolsOrder() {
|
2016-11-10 17:05:20 +08:00
|
|
|
if (Config->SymbolOrderingFile.empty())
|
|
|
|
return;
|
|
|
|
|
2016-12-20 09:51:08 +08:00
|
|
|
// Sort sections by priority.
|
2017-08-04 18:25:29 +08:00
|
|
|
DenseMap<SectionBase *, int> SectionOrder = buildSectionOrder<ELFT>();
|
2017-07-06 07:36:24 +08:00
|
|
|
for (BaseCommand *Base : Script->Opt.Commands)
|
2017-07-28 03:22:43 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base))
|
|
|
|
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-07-28 03:22:43 +08:00
|
|
|
std::vector<BaseCommand *> Old = Script->Opt.Commands;
|
|
|
|
Script->Opt.Commands.clear();
|
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));
|
2017-07-28 03:22:43 +08:00
|
|
|
Script->Opt.Commands.insert(Script->Opt.Commands.end(), Old.begin(),
|
|
|
|
Old.end());
|
2016-08-11 15:56:43 +08:00
|
|
|
|
2017-07-06 07:36:24 +08:00
|
|
|
Script->fabricateDefaultCommands();
|
|
|
|
sortBySymbolsOrder<ELFT>();
|
2017-07-28 03:22:43 +08:00
|
|
|
sortInitFini(findSection(".init_array"));
|
|
|
|
sortInitFini(findSection(".fini_array"));
|
|
|
|
sortCtorsDtors(findSection(".ctors"));
|
|
|
|
sortCtorsDtors(findSection(".dtors"));
|
2016-07-20 22:43:20 +08:00
|
|
|
}
|
|
|
|
|
2017-09-01 10:23:31 +08:00
|
|
|
// This function generates assignments for predefined symbols (e.g. _end or
|
|
|
|
// _etext) and inserts them into the commands sequence to be processed at the
|
|
|
|
// appropriate time. This ensures that the value is going to be correct by the
|
|
|
|
// time any references to these symbols are processed and is equivalent to
|
|
|
|
// defining these symbols explicitly in the linker script.
|
|
|
|
template <class ELFT> void Writer<ELFT>::addPredefinedSymbols() {
|
|
|
|
PhdrEntry *Last = nullptr;
|
|
|
|
PhdrEntry *LastRO = nullptr;
|
|
|
|
PhdrEntry *LastRW = nullptr;
|
2017-09-06 04:17:37 +08:00
|
|
|
|
2017-09-01 10:23:31 +08:00
|
|
|
for (PhdrEntry *P : Phdrs) {
|
|
|
|
if (P->p_type != PT_LOAD)
|
|
|
|
continue;
|
|
|
|
Last = P;
|
|
|
|
if (P->p_flags & PF_W)
|
|
|
|
LastRW = P;
|
|
|
|
else
|
|
|
|
LastRO = P;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Make = [](DefinedRegular *S) {
|
|
|
|
auto *Cmd = make<SymbolAssignment>(
|
|
|
|
S->getName(), [=] { return Script->getSymbolValue("", "."); }, "");
|
|
|
|
Cmd->Sym = S;
|
|
|
|
return Cmd;
|
|
|
|
};
|
|
|
|
|
2017-09-06 04:17:37 +08:00
|
|
|
std::vector<BaseCommand *> &V = Script->Opt.Commands;
|
2017-09-01 10:23:31 +08:00
|
|
|
|
2017-09-06 04:17:37 +08:00
|
|
|
// _end is the first location after the uninitialized data region.
|
2017-09-01 10:23:31 +08:00
|
|
|
if (Last) {
|
2017-09-06 04:17:37 +08:00
|
|
|
for (size_t I = 0; I < V.size(); ++I) {
|
2017-09-07 19:01:10 +08:00
|
|
|
if (V[I] != Last->LastSec)
|
2017-09-06 04:17:37 +08:00
|
|
|
continue;
|
2017-09-01 10:23:31 +08:00
|
|
|
if (ElfSym::End2)
|
2017-09-06 04:17:37 +08:00
|
|
|
V.insert(V.begin() + I + 1, Make(ElfSym::End2));
|
|
|
|
if (ElfSym::End1)
|
|
|
|
V.insert(V.begin() + I + 1, Make(ElfSym::End1));
|
|
|
|
break;
|
2017-09-01 10:23:31 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-06 04:17:37 +08:00
|
|
|
|
|
|
|
// _etext is the first location after the last read-only loadable segment.
|
2017-09-01 10:23:31 +08:00
|
|
|
if (LastRO) {
|
2017-09-06 04:17:37 +08:00
|
|
|
for (size_t I = 0; I < V.size(); ++I) {
|
2017-09-07 19:01:10 +08:00
|
|
|
if (V[I] != LastRO->LastSec)
|
2017-09-06 04:17:37 +08:00
|
|
|
continue;
|
2017-09-01 10:23:31 +08:00
|
|
|
if (ElfSym::Etext2)
|
2017-09-06 04:17:37 +08:00
|
|
|
V.insert(V.begin() + I + 1, Make(ElfSym::Etext2));
|
|
|
|
if (ElfSym::Etext1)
|
|
|
|
V.insert(V.begin() + I + 1, Make(ElfSym::Etext1));
|
|
|
|
break;
|
2017-09-01 10:23:31 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-06 04:17:37 +08:00
|
|
|
|
|
|
|
// _edata points to the end of the last non SHT_NOBITS section.
|
2017-09-01 10:23:31 +08:00
|
|
|
if (LastRW) {
|
2017-09-06 04:17:37 +08:00
|
|
|
size_t I = 0;
|
|
|
|
for (; I < V.size(); ++I)
|
2017-09-07 19:01:10 +08:00
|
|
|
if (V[I] == LastRW->FirstSec)
|
2017-09-06 04:17:37 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
for (; I < V.size(); ++I) {
|
|
|
|
auto *Sec = dyn_cast<OutputSection>(V[I]);
|
|
|
|
if (!Sec || Sec->Type != SHT_NOBITS)
|
|
|
|
continue;
|
2017-09-01 10:23:31 +08:00
|
|
|
if (ElfSym::Edata2)
|
2017-09-06 04:17:37 +08:00
|
|
|
V.insert(V.begin() + I, Make(ElfSym::Edata2));
|
2017-09-01 10:23:31 +08:00
|
|
|
if (ElfSym::Edata1)
|
2017-09-06 04:17:37 +08:00
|
|
|
V.insert(V.begin() + I, Make(ElfSym::Edata1));
|
|
|
|
break;
|
2017-09-01 10:23:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 22:52:22 +08:00
|
|
|
// We want to find how similar two ranks are.
|
|
|
|
// The more branches in getSectionRank that match, the more similar they are.
|
|
|
|
// Since each branch corresponds to a bit flag, we can just use
|
|
|
|
// countLeadingZeros.
|
2017-07-28 03:22:43 +08:00
|
|
|
static int getRankProximityAux(OutputSection *A, OutputSection *B) {
|
2017-05-12 22:52:22 +08:00
|
|
|
return countLeadingZeros(A->SortRank ^ B->SortRank);
|
2016-11-08 18:44:48 +08:00
|
|
|
}
|
|
|
|
|
2017-06-16 05:51:01 +08:00
|
|
|
static int getRankProximity(OutputSection *A, BaseCommand *B) {
|
2017-07-28 03:22:43 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(B))
|
|
|
|
if (Sec->Live)
|
|
|
|
return getRankProximityAux(A, Sec);
|
2017-06-16 05:51:01 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When placing orphan sections, we want to place them after symbol assignments
|
|
|
|
// so that an orphan after
|
|
|
|
// begin_foo = .;
|
|
|
|
// foo : { *(foo) }
|
|
|
|
// end_foo = .;
|
|
|
|
// doesn't break the intended meaning of the begin/end symbols.
|
|
|
|
// We don't want to go over sections since findOrphanPos is the
|
|
|
|
// one in charge of deciding the order of the sections.
|
|
|
|
// We don't want to go over changes to '.', since doing so in
|
|
|
|
// rx_sec : { *(rx_sec) }
|
|
|
|
// . = ALIGN(0x1000);
|
|
|
|
// /* The RW PT_LOAD starts here*/
|
|
|
|
// rw_sec : { *(rw_sec) }
|
|
|
|
// would mean that the RW PT_LOAD would become unaligned.
|
|
|
|
static bool shouldSkip(BaseCommand *Cmd) {
|
2017-07-28 03:22:43 +08:00
|
|
|
if (isa<OutputSection>(Cmd))
|
2017-06-16 05:51:01 +08:00
|
|
|
return false;
|
|
|
|
if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd))
|
|
|
|
return Assign->Name != ".";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-12 22:52:22 +08:00
|
|
|
// We want to place orphan sections so that they share as much
|
|
|
|
// characteristics with their neighbors as possible. For example, if
|
|
|
|
// both are rw, or both are tls.
|
2017-05-09 21:58:46 +08:00
|
|
|
template <typename ELFT>
|
2017-06-16 05:51:01 +08:00
|
|
|
static std::vector<BaseCommand *>::iterator
|
|
|
|
findOrphanPos(std::vector<BaseCommand *>::iterator B,
|
|
|
|
std::vector<BaseCommand *>::iterator E) {
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *Sec = cast<OutputSection>(*E);
|
2017-05-09 21:58:46 +08:00
|
|
|
|
2017-05-12 22:52:22 +08:00
|
|
|
// Find the first element that has as close a rank as possible.
|
2017-06-16 05:51:01 +08:00
|
|
|
auto I = std::max_element(B, E, [=](BaseCommand *A, BaseCommand *B) {
|
2017-05-12 22:52:22 +08:00
|
|
|
return getRankProximity(Sec, A) < getRankProximity(Sec, B);
|
|
|
|
});
|
|
|
|
if (I == E)
|
2017-05-09 21:58:46 +08:00
|
|
|
return E;
|
|
|
|
|
2017-05-12 22:52:22 +08:00
|
|
|
// Consider all existing sections with the same proximity.
|
2017-06-16 05:51:01 +08:00
|
|
|
int Proximity = getRankProximity(Sec, *I);
|
|
|
|
for (; I != E; ++I) {
|
2017-07-28 03:22:43 +08:00
|
|
|
auto *CurSec = dyn_cast<OutputSection>(*I);
|
|
|
|
if (!CurSec || !CurSec->Live)
|
2017-06-16 05:51:01 +08:00
|
|
|
continue;
|
2017-07-28 03:22:43 +08:00
|
|
|
if (getRankProximity(Sec, CurSec) != Proximity ||
|
|
|
|
Sec->SortRank < CurSec->SortRank)
|
2017-06-16 05:51:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto J = std::find_if(
|
2017-06-16 06:03:06 +08:00
|
|
|
llvm::make_reverse_iterator(I), llvm::make_reverse_iterator(B),
|
2017-07-28 03:22:43 +08:00
|
|
|
[](BaseCommand *Cmd) { return isa<OutputSection>(Cmd); });
|
2017-06-16 05:51:01 +08:00
|
|
|
I = J.base();
|
|
|
|
while (I != E && shouldSkip(*I))
|
2017-05-12 22:52:22 +08:00
|
|
|
++I;
|
|
|
|
return I;
|
2017-05-09 21:58:46 +08:00
|
|
|
}
|
|
|
|
|
2016-09-22 06:36:19 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::sortSections() {
|
2017-06-28 17:59:34 +08:00
|
|
|
if (Script->Opt.HasSections)
|
|
|
|
Script->adjustSectionsBeforeSorting();
|
|
|
|
|
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)
|
2017-06-29 06:44:11 +08:00
|
|
|
return;
|
2017-05-12 22:52:22 +08:00
|
|
|
|
2017-06-16 05:51:01 +08:00
|
|
|
for (BaseCommand *Base : Script->Opt.Commands)
|
2017-07-28 03:22:43 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base))
|
|
|
|
Sec->SortRank = getSectionRank(Sec);
|
2017-05-12 22:52:22 +08:00
|
|
|
|
2017-03-22 07:03:09 +08:00
|
|
|
if (!Script->Opt.HasSections) {
|
2017-07-28 03:22:43 +08:00
|
|
|
// We know that all the OutputSections are contiguous in
|
2017-06-16 05:51:01 +08:00
|
|
|
// this case.
|
|
|
|
auto E = Script->Opt.Commands.end();
|
|
|
|
auto I = Script->Opt.Commands.begin();
|
2017-07-28 03:22:43 +08:00
|
|
|
auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
|
2017-06-16 05:51:01 +08:00
|
|
|
I = std::find_if(I, E, IsSection);
|
2017-06-16 06:03:06 +08:00
|
|
|
E = std::find_if(llvm::make_reverse_iterator(E),
|
|
|
|
llvm::make_reverse_iterator(I), IsSection)
|
2017-06-16 05:51:01 +08:00
|
|
|
.base();
|
|
|
|
std::stable_sort(I, E, compareSections);
|
2016-09-22 06:36:19 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-16 05:51:01 +08:00
|
|
|
// Orphan sections are sections present in the input files which are
|
|
|
|
// not explicitly placed into the output file by the linker script.
|
|
|
|
//
|
|
|
|
// The sections in the linker script are already in the correct
|
|
|
|
// order. We have to figuere out where to insert the orphan
|
|
|
|
// sections.
|
|
|
|
//
|
2016-09-22 06:36:19 +08:00
|
|
|
// The order of the sections in the script is arbitrary and may not agree with
|
2017-06-16 05:51:01 +08:00
|
|
|
// compareSections. 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:
|
2016-09-22 06:36:19 +08:00
|
|
|
// * 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:
|
2017-06-16 05:51:01 +08:00
|
|
|
// * Sort only the orphan sections. They are in the end right now.
|
|
|
|
// * Move each orphan section to its preferred position. We try
|
2016-11-08 18:44:48 +08:00
|
|
|
// to put each section in the last position where it it can share
|
|
|
|
// a PT_LOAD.
|
2017-06-16 05:51:01 +08:00
|
|
|
//
|
|
|
|
// There is some ambiguity as to where exactly a new entry should be
|
|
|
|
// inserted, because Opt.Commands contains not only output section
|
|
|
|
// commands but also other types of commands such as symbol assignment
|
|
|
|
// expressions. There's no correct answer here due to the lack of the
|
|
|
|
// formal specification of the linker script. We use heuristics to
|
|
|
|
// determine whether a new output command should be added before or
|
|
|
|
// after another commands. For the details, look at shouldSkip
|
|
|
|
// function.
|
|
|
|
|
|
|
|
auto I = Script->Opt.Commands.begin();
|
|
|
|
auto E = Script->Opt.Commands.end();
|
|
|
|
auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
|
2017-07-28 03:22:43 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base))
|
|
|
|
return Sec->Live && Sec->SectionIndex == INT_MAX;
|
2017-06-16 05:51:01 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Sort the orphan sections.
|
|
|
|
std::stable_sort(NonScriptI, E, compareSections);
|
2016-09-22 06:36:19 +08:00
|
|
|
|
2017-06-16 05:51:01 +08:00
|
|
|
// As a horrible special case, skip the first . assignment if it is before any
|
|
|
|
// section. We do this because it is common to set a load address by starting
|
|
|
|
// the script with ". = 0xabcd" and the expectation is that every section is
|
|
|
|
// after that.
|
|
|
|
auto FirstSectionOrDotAssignment =
|
|
|
|
std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
|
|
|
|
if (FirstSectionOrDotAssignment != E &&
|
|
|
|
isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
|
|
|
|
++FirstSectionOrDotAssignment;
|
|
|
|
I = FirstSectionOrDotAssignment;
|
2016-09-22 06:36:19 +08:00
|
|
|
|
2017-05-12 22:52:22 +08:00
|
|
|
while (NonScriptI != E) {
|
|
|
|
auto Pos = findOrphanPos<ELFT>(I, NonScriptI);
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *Orphan = cast<OutputSection>(*NonScriptI);
|
2017-05-12 22:52:22 +08:00
|
|
|
|
|
|
|
// As an optimization, find all sections with the same sort rank
|
|
|
|
// and insert them with one rotate.
|
2017-06-16 05:51:01 +08:00
|
|
|
unsigned Rank = Orphan->SortRank;
|
|
|
|
auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
|
2017-07-28 03:22:43 +08:00
|
|
|
return cast<OutputSection>(Cmd)->SortRank != Rank;
|
2017-05-12 22:52:22 +08:00
|
|
|
});
|
|
|
|
std::rotate(Pos, NonScriptI, End);
|
|
|
|
NonScriptI = End;
|
|
|
|
}
|
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)
|
2017-06-07 10:31:19 +08:00
|
|
|
if (SS && SS->getParent() && !SS->empty())
|
2017-03-08 22:06:24 +08:00
|
|
|
Fn(SS);
|
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-07-04 00:54:39 +08:00
|
|
|
static void removeUnusedSyntheticSections() {
|
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;
|
2017-06-01 04:17:44 +08:00
|
|
|
OutputSection *OS = SS->getParent();
|
|
|
|
if (!SS->empty() || !OS)
|
2016-11-25 16:05:41 +08:00
|
|
|
continue;
|
2017-06-26 23:11:24 +08:00
|
|
|
if ((SS == InX::Got || SS == InX::MipsGot) && ElfSym::GlobalOffsetTable)
|
2017-06-26 18:22:17 +08:00
|
|
|
continue;
|
2017-07-04 00:54:39 +08:00
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
std::vector<BaseCommand *>::iterator Empty = OS->Commands.end();
|
|
|
|
for (auto I = OS->Commands.begin(), E = OS->Commands.end(); I != E; ++I) {
|
2017-07-04 01:32:09 +08:00
|
|
|
BaseCommand *B = *I;
|
2017-07-04 00:54:39 +08:00
|
|
|
if (auto *ISD = dyn_cast<InputSectionDescription>(B)) {
|
2017-09-08 21:26:45 +08:00
|
|
|
llvm::erase_if(ISD->Sections,
|
|
|
|
[=](InputSection *IS) { return IS == SS; });
|
2017-07-04 00:54:39 +08:00
|
|
|
if (ISD->Sections.empty())
|
2017-07-04 01:32:09 +08:00
|
|
|
Empty = I;
|
2017-07-04 00:54:39 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 03:22:43 +08:00
|
|
|
if (Empty != OS->Commands.end())
|
|
|
|
OS->Commands.erase(Empty);
|
2017-07-04 00:54:39 +08:00
|
|
|
|
2017-04-06 17:40:03 +08:00
|
|
|
// If there are no other sections in the output section, remove it from the
|
|
|
|
// output.
|
2017-07-28 03:22:43 +08:00
|
|
|
if (OS->Commands.empty()) {
|
2017-07-03 23:49:25 +08:00
|
|
|
// Also remove script commands matching the output section.
|
2017-08-28 17:28:15 +08:00
|
|
|
llvm::erase_if(Script->Opt.Commands, [&](BaseCommand *Cmd) {
|
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Cmd))
|
2017-07-28 03:22:43 +08:00
|
|
|
return Sec == OS;
|
2017-07-03 23:49:25 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2016-11-25 16:05:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 23:05:37 +08:00
|
|
|
// Returns true if a symbol can be replaced at load-time by a symbol
|
|
|
|
// with the same name defined in other ELF executable or DSO.
|
|
|
|
static bool computeIsPreemptible(const SymbolBody &B) {
|
|
|
|
assert(!B.isLocal());
|
|
|
|
// Shared symbols resolve to the definition in the DSO. The exceptions are
|
|
|
|
// symbols with copy relocations (which resolve to .bss) or preempt plt
|
|
|
|
// entries (which resolve to that plt entry).
|
|
|
|
if (auto *SS = dyn_cast<SharedSymbol>(&B))
|
|
|
|
return !SS->CopyRelSec && !SS->NeedsPltAddr;
|
|
|
|
|
|
|
|
// Only symbols that appear in dynsym can be preempted.
|
|
|
|
if (!B.symbol()->includeInDynsym())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Only default visibility symbols can be preempted.
|
|
|
|
if (B.symbol()->Visibility != STV_DEFAULT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Undefined symbols in non-DSOs are usually just an error, so it
|
|
|
|
// doesn't matter whether we return true or false here. However, if
|
|
|
|
// -unresolved-symbols=ignore-all is specified, undefined symbols in
|
|
|
|
// executables are automatically exported so that the runtime linker
|
2017-09-07 05:16:51 +08:00
|
|
|
// can try to resolve them. In that case, they are preemptible. So, we
|
2017-08-10 23:05:37 +08:00
|
|
|
// return true for an undefined symbol in case the option is specified.
|
|
|
|
if (!Config->Shared)
|
|
|
|
return B.isUndefined();
|
|
|
|
|
|
|
|
// -Bsymbolic means that definitions are not preempted.
|
|
|
|
if (Config->Bsymbolic || (Config->BsymbolicFunctions && B.isFunc()))
|
|
|
|
return !B.isDefined();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-07-28 03:22:43 +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-07-05 03:05:03 +08:00
|
|
|
for (BaseCommand *Base : Script->Opt.Commands)
|
2017-07-28 03:22:43 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base))
|
|
|
|
addStartStopSymbols(Sec);
|
2016-03-02 03:12:35 +08:00
|
|
|
}
|
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
|
2017-05-16 18:04:42 +08:00
|
|
|
if (InX::DynSymTab)
|
2017-05-12 07:16:43 +08:00
|
|
|
addRegular<ELFT>("_DYNAMIC", InX::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
|
|
|
|
2017-09-08 09:09:52 +08:00
|
|
|
for (Symbol *S : Symtab->getSymbols())
|
|
|
|
S->body()->IsPreemptible = computeIsPreemptible(*S->body());
|
2017-08-10 23:05:37 +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-05-12 06:02:41 +08:00
|
|
|
if (InX::Plt && !InX::Plt->empty())
|
|
|
|
InX::Plt->addSymbols();
|
|
|
|
if (InX::Iplt && !InX::Iplt->empty())
|
|
|
|
InX::Iplt->addSymbols();
|
2017-01-25 18:31:16 +08:00
|
|
|
|
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.
|
2017-07-27 02:42:48 +08:00
|
|
|
for (Symbol *S : Symtab->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;
|
2017-05-16 18:04:42 +08:00
|
|
|
if (InX::SymTab)
|
|
|
|
InX::SymTab->addSymbol(Body);
|
2015-09-23 07:38:23 +08:00
|
|
|
|
2017-05-16 18:04:42 +08:00
|
|
|
if (InX::DynSymTab && S->includeInDynsym()) {
|
|
|
|
InX::DynSymTab->addSymbol(Body);
|
2017-02-27 07:35:34 +08:00
|
|
|
if (auto *SS = dyn_cast<SharedSymbol>(Body))
|
2017-08-05 06:31:42 +08:00
|
|
|
if (cast<SharedFile<ELFT>>(S->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
|
|
|
|
2017-07-05 02:26:21 +08:00
|
|
|
addPredefinedSections();
|
2017-07-04 00:54:39 +08:00
|
|
|
removeUnusedSyntheticSections();
|
|
|
|
|
2016-09-22 06:36:19 +08:00
|
|
|
sortSections();
|
2017-06-16 05:51:01 +08:00
|
|
|
|
|
|
|
// Now that we have the final list, create a list of all the
|
2017-07-28 03:22:43 +08:00
|
|
|
// OutputSections for convenience.
|
2017-06-14 06:36:20 +08:00
|
|
|
for (BaseCommand *Base : Script->Opt.Commands)
|
2017-07-28 03:22:43 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base))
|
|
|
|
OutputSections.push_back(Sec);
|
2015-12-26 15:50:41 +08:00
|
|
|
|
2017-06-20 09:51:50 +08:00
|
|
|
// Prefer command line supplied address over other constraints.
|
2017-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
|
|
|
auto I = Config->SectionStartMap.find(Sec->Name);
|
2017-06-20 09:51:50 +08:00
|
|
|
if (I != Config->SectionStartMap.end())
|
2017-07-28 03:22:43 +08:00
|
|
|
Sec->AddrExpr = [=] { return I->second; };
|
2017-06-20 09:51:50 +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-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2016-04-06 15:20:45 +08:00
|
|
|
Sec->SectionIndex = I++;
|
2017-05-12 06:02:41 +08:00
|
|
|
Sec->ShName = InX::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-06-14 07:26:31 +08:00
|
|
|
Phdrs = Script->hasPhdrsCommands() ? Script->createPhdrs() : createPhdrs();
|
2016-12-20 05:21:07 +08:00
|
|
|
addPtArmExid(Phdrs);
|
2017-05-05 03:34:17 +08:00
|
|
|
Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
|
2016-12-20 05:21:07 +08:00
|
|
|
}
|
|
|
|
|
2016-11-18 14:44:18 +08:00
|
|
|
// Dynamic section must be the last one in this list and dynamic
|
|
|
|
// symbol table section (DynSymTab) must be the first one.
|
2017-08-16 01:01:39 +08:00
|
|
|
applySynthetic({InX::DynSymTab, InX::Bss,
|
|
|
|
InX::BssRelRo, InX::GnuHashTab,
|
|
|
|
In<ELFT>::HashTab, InX::SymTab,
|
|
|
|
InX::ShStrTab, InX::StrTab,
|
|
|
|
In<ELFT>::VerDef, InX::DynStrTab,
|
|
|
|
InX::Got, InX::MipsGot,
|
|
|
|
InX::IgotPlt, InX::GotPlt,
|
|
|
|
In<ELFT>::RelaDyn, In<ELFT>::RelaIplt,
|
|
|
|
In<ELFT>::RelaPlt, InX::Plt,
|
|
|
|
InX::Iplt, In<ELFT>::EhFrameHdr,
|
|
|
|
In<ELFT>::VerSym, In<ELFT>::VerNeed,
|
|
|
|
InX::Dynamic},
|
2017-03-16 18:29:44 +08:00
|
|
|
[](SyntheticSection *SS) { SS->finalizeContents(); });
|
2017-03-08 22:06:24 +08:00
|
|
|
|
2017-09-06 22:02:14 +08:00
|
|
|
if (!Script->Opt.HasSections && !Config->Relocatable)
|
|
|
|
fixSectionAlignments();
|
|
|
|
|
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-05-17 15:10:59 +08:00
|
|
|
ThunkCreator TC;
|
2017-07-07 18:03:37 +08:00
|
|
|
Script->assignAddresses();
|
2017-07-28 03:22:43 +08:00
|
|
|
if (TC.createThunks(OutputSections)) {
|
2017-05-12 05:33:30 +08:00
|
|
|
applySynthetic({InX::MipsGot},
|
2017-03-16 18:29:44 +08:00
|
|
|
[](SyntheticSection *SS) { SS->updateAllocSize(); });
|
2017-07-28 03:22:43 +08:00
|
|
|
if (TC.createThunks(OutputSections))
|
2017-06-16 21:10:08 +08:00
|
|
|
fatal("All non-range thunks should be created in first call");
|
|
|
|
}
|
2017-03-08 22:06:24 +08:00
|
|
|
}
|
2017-06-05 16:51:15 +08:00
|
|
|
|
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.
|
2017-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
|
|
|
Sec->finalize<ELFT>();
|
2017-03-08 22:06:24 +08:00
|
|
|
|
|
|
|
// createThunks may have added local symbols to the static symbol table
|
2017-05-16 18:04:42 +08:00
|
|
|
applySynthetic({InX::SymTab, InX::ShStrTab, InX::StrTab},
|
2017-03-16 18:29:44 +08:00
|
|
|
[](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-07-28 03:22:43 +08:00
|
|
|
OutputSection *Cmd = findSection(".ARM.exidx");
|
|
|
|
if (!Cmd || !Cmd->Live || Config->Relocatable)
|
2017-05-30 19:51:02 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto *Sentinel = make<ARMExidxSentinelSection>();
|
2017-07-28 03:22:43 +08:00
|
|
|
Cmd->addSection(Sentinel);
|
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-07-28 03:22:43 +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-07-28 03:22:43 +08:00
|
|
|
template <class ELFT> OutputSection *Writer<ELFT>::findSection(StringRef Name) {
|
2017-07-05 02:26:21 +08:00
|
|
|
for (BaseCommand *Base : Script->Opt.Commands)
|
2017-07-28 03:22:43 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base))
|
|
|
|
if (Sec->Name == Name)
|
|
|
|
return Sec;
|
2017-05-27 01:48:27 +08:00
|
|
|
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.
|
2017-07-27 15:46:50 +08:00
|
|
|
template <class ELFT> std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs() {
|
|
|
|
std::vector<PhdrEntry *> Ret;
|
2016-12-20 01:01:01 +08:00
|
|
|
auto AddHdr = [&](unsigned Type, unsigned Flags) -> PhdrEntry * {
|
2017-07-27 15:46:50 +08:00
|
|
|
Ret.push_back(make<PhdrEntry>(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-07-28 03:22:43 +08:00
|
|
|
if (OutputSection *Cmd = findSection(".interp"))
|
|
|
|
AddHdr(PT_INTERP, Cmd->getPhdrFlags())->add(Cmd);
|
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-05-05 03:34:17 +08:00
|
|
|
|
|
|
|
// Add the headers. We will remove them if they don't fit.
|
|
|
|
Load->add(Out::ElfHeader);
|
|
|
|
Load->add(Out::ProgramHeaders);
|
|
|
|
|
2017-07-28 03:22:43 +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-07-28 03:22:43 +08:00
|
|
|
if (Sec->LMAExpr || 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.
|
2017-07-27 15:46:50 +08:00
|
|
|
PhdrEntry *TlsHdr = make<PhdrEntry>(PT_TLS, PF_R);
|
2017-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-02-02 06:42:17 +08:00
|
|
|
if (Sec->Flags & SHF_TLS)
|
2017-07-27 15:46:50 +08:00
|
|
|
TlsHdr->add(Sec);
|
2017-09-07 19:01:10 +08:00
|
|
|
if (TlsHdr->FirstSec)
|
2017-07-27 15:46:50 +08:00
|
|
|
Ret.push_back(TlsHdr);
|
2016-02-11 06:43:13 +08:00
|
|
|
|
2015-10-24 05:45:59 +08:00
|
|
|
// Add an entry for .dynamic.
|
2017-05-16 18:04:42 +08:00
|
|
|
if (InX::DynSymTab)
|
2017-06-01 04:17:44 +08:00
|
|
|
AddHdr(PT_DYNAMIC, InX::Dynamic->getParent()->getPhdrFlags())
|
|
|
|
->add(InX::Dynamic->getParent());
|
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-07-27 15:46:50 +08:00
|
|
|
PhdrEntry *RelRo = make<PhdrEntry>(PT_GNU_RELRO, PF_R);
|
2017-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-05-12 07:31:06 +08:00
|
|
|
if (needsPtLoad(Sec) && isRelroSection(Sec))
|
2017-07-27 15:46:50 +08:00
|
|
|
RelRo->add(Sec);
|
2017-09-07 19:01:10 +08:00
|
|
|
if (RelRo->FirstSec)
|
2017-07-27 15:46:50 +08:00
|
|
|
Ret.push_back(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 &&
|
2017-06-01 04:17:44 +08:00
|
|
|
In<ELFT>::EhFrame->getParent() && In<ELFT>::EhFrameHdr->getParent())
|
|
|
|
AddHdr(PT_GNU_EH_FRAME, In<ELFT>::EhFrameHdr->getParent()->getPhdrFlags())
|
|
|
|
->add(In<ELFT>::EhFrameHdr->getParent());
|
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-07-28 03:22:43 +08:00
|
|
|
if (OutputSection *Cmd = findSection(".openbsd.randomdata"))
|
|
|
|
AddHdr(PT_OPENBSD_RANDOMIZE, Cmd->getPhdrFlags())->add(Cmd);
|
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-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2017-02-02 04:58:41 +08:00
|
|
|
if (Sec->Type == SHT_NOTE) {
|
2017-07-28 03:22:43 +08:00
|
|
|
if (!Note || Sec->LMAExpr)
|
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>
|
2017-07-27 15:46:50 +08:00
|
|
|
void Writer<ELFT>::addPtArmExid(std::vector<PhdrEntry *> &Phdrs) {
|
2016-11-28 08:40:21 +08:00
|
|
|
if (Config->EMachine != EM_ARM)
|
|
|
|
return;
|
2017-07-28 03:22:43 +08:00
|
|
|
auto I = llvm::find_if(OutputSections, [](OutputSection *Cmd) {
|
|
|
|
return Cmd->Type == SHT_ARM_EXIDX;
|
2017-07-04 21:10:37 +08:00
|
|
|
});
|
2017-07-28 03:22:43 +08:00
|
|
|
if (I == OutputSections.end())
|
2016-11-28 08:40:21 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
|
2017-07-27 15:46:50 +08:00
|
|
|
PhdrEntry *ARMExidx = make<PhdrEntry>(PT_ARM_EXIDX, PF_R);
|
2017-07-28 03:22:43 +08:00
|
|
|
ARMExidx->add(*I);
|
2016-11-28 08:40:21 +08:00
|
|
|
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() {
|
2017-07-28 03:22:43 +08:00
|
|
|
auto PageAlign = [](OutputSection *Cmd) {
|
2017-06-02 09:37:58 +08:00
|
|
|
if (Cmd && !Cmd->AddrExpr)
|
|
|
|
Cmd->AddrExpr = [=] {
|
|
|
|
return alignTo(Script->getDot(), Config->MaxPageSize);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-07-27 15:46:50 +08:00
|
|
|
for (const PhdrEntry *P : Phdrs)
|
2017-09-07 19:01:10 +08:00
|
|
|
if (P->p_type == PT_LOAD && P->FirstSec)
|
|
|
|
PageAlign(P->FirstSec);
|
2016-03-31 03:41:51 +08:00
|
|
|
|
2017-07-27 15:46:50 +08:00
|
|
|
for (const PhdrEntry *P : Phdrs) {
|
|
|
|
if (P->p_type != PT_GNU_RELRO)
|
2016-03-31 03:41:51 +08:00
|
|
|
continue;
|
2017-09-07 19:01:10 +08:00
|
|
|
if (P->FirstSec)
|
|
|
|
PageAlign(P->FirstSec);
|
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.
|
2017-07-28 03:22:43 +08:00
|
|
|
auto End = OutputSections.end();
|
2017-09-07 19:01:10 +08:00
|
|
|
auto I = std::find(OutputSections.begin(), End, P->LastSec);
|
2016-03-31 03:41:51 +08:00
|
|
|
if (I == End || (I + 1) == End)
|
|
|
|
continue;
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *Cmd = (*(I + 1));
|
|
|
|
if (needsPtLoad(Cmd))
|
|
|
|
PageAlign(Cmd);
|
2016-03-31 03:41:51 +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-07-28 03:22:43 +08:00
|
|
|
static uint64_t getFileAlignment(uint64_t Off, OutputSection *Cmd) {
|
2016-12-08 04:20:39 +08:00
|
|
|
// If the section is not in a PT_LOAD, we just have to align it.
|
2017-09-07 18:53:07 +08:00
|
|
|
if (!Cmd->PtLoad)
|
2017-07-28 03:22:43 +08:00
|
|
|
return alignTo(Off, Cmd->Alignment);
|
2016-09-29 17:20:33 +08:00
|
|
|
|
2017-09-07 19:01:10 +08:00
|
|
|
OutputSection *First = Cmd->PtLoad->FirstSec;
|
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.
|
2017-07-28 03:22:43 +08:00
|
|
|
if (Cmd == First)
|
|
|
|
return alignTo(Off, std::max<uint64_t>(Cmd->Alignment, Config->MaxPageSize),
|
|
|
|
Cmd->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).
|
2017-07-28 03:22:43 +08:00
|
|
|
return First->Offset + Cmd->Addr - First->Addr;
|
2016-04-27 17:16:28 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
static uint64_t setOffset(OutputSection *Cmd, uint64_t Off) {
|
|
|
|
if (Cmd->Type == SHT_NOBITS) {
|
|
|
|
Cmd->Offset = Off;
|
2017-02-15 07:35:42 +08:00
|
|
|
return Off;
|
2016-08-25 17:05:47 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
Off = getFileAlignment(Off, Cmd);
|
|
|
|
Cmd->Offset = Off;
|
|
|
|
return Off + Cmd->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-07-28 03:22:43 +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-08-03 00:35:00 +08:00
|
|
|
PhdrEntry *LastRX = nullptr;
|
|
|
|
for (PhdrEntry *P : Phdrs)
|
|
|
|
if (P->p_type == PT_LOAD && (P->p_flags & PF_X))
|
|
|
|
LastRX = P;
|
|
|
|
|
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2017-07-28 03:22:43 +08:00
|
|
|
Off = setOffset(Sec, Off);
|
2017-08-03 00:35:00 +08:00
|
|
|
if (Script->Opt.HasSections)
|
|
|
|
continue;
|
|
|
|
// If this is a last section of the last executable segment and that
|
|
|
|
// segment is the last loadable segment, align the offset of the
|
|
|
|
// following section to avoid loading non-segments parts of the file.
|
2017-09-07 19:01:10 +08:00
|
|
|
if (LastRX && LastRX->LastSec == Sec)
|
2017-08-03 00:35:00 +08:00
|
|
|
Off = alignTo(Off, Target->PageSize);
|
|
|
|
}
|
2016-07-01 18:27:36 +08:00
|
|
|
|
2017-04-06 05:37:09 +08:00
|
|
|
SectionHeaderOff = alignTo(Off, Config->Wordsize);
|
2017-07-28 03:22:43 +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() {
|
2017-07-27 15:46:50 +08:00
|
|
|
for (PhdrEntry *P : Phdrs) {
|
2017-09-07 19:01:10 +08:00
|
|
|
OutputSection *First = P->FirstSec;
|
|
|
|
OutputSection *Last = P->LastSec;
|
2016-04-02 06:42:04 +08:00
|
|
|
if (First) {
|
2017-07-27 15:46:50 +08:00
|
|
|
P->p_filesz = Last->Offset - First->Offset;
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Last->Type != SHT_NOBITS)
|
2017-07-27 15:46:50 +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;
|
|
|
|
if (!P->HasLMA)
|
|
|
|
P->p_paddr = First->getLMA();
|
2016-02-11 06:43:13 +08:00
|
|
|
}
|
2017-07-27 15:46:50 +08:00
|
|
|
if (P->p_type == PT_LOAD)
|
|
|
|
P->p_align = std::max<uint64_t>(P->p_align, Config->MaxPageSize);
|
|
|
|
else if (P->p_type == PT_GNU_RELRO) {
|
|
|
|
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-07-27 15:46:50 +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.
|
2017-07-27 15:46:50 +08:00
|
|
|
if (P->p_type == PT_TLS) {
|
|
|
|
Out::TlsPhdr = P;
|
|
|
|
if (P->p_memsz)
|
|
|
|
P->p_memsz = alignTo(P->p_memsz, P->p_align);
|
2015-10-24 05:45:59 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 06:41:00 +08:00
|
|
|
// The entry point address is chosen in the following ways.
|
|
|
|
//
|
|
|
|
// 1. the '-e' entry command-line option;
|
|
|
|
// 2. the ENTRY(symbol) command in a linker control script;
|
|
|
|
// 3. the value of the symbol start, if present;
|
|
|
|
// 4. the address of the first byte of the .text section, if present;
|
|
|
|
// 5. the address 0.
|
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.
|
2017-07-27 02:42:48 +08:00
|
|
|
if (SymbolBody *B = Symtab->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;
|
2017-05-16 16:19:25 +08:00
|
|
|
if (to_integer(Config->Entry, Addr))
|
2016-12-07 11:23:06 +08:00
|
|
|
return Addr;
|
2016-11-24 06:41:00 +08:00
|
|
|
|
|
|
|
// Case 4
|
2017-07-28 03:22:43 +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-04-05 18:03:25 +08:00
|
|
|
if (ElfSym::Bss)
|
2017-07-28 03:22:43 +08:00
|
|
|
ElfSym::Bss->Section = findSection(".bss");
|
2017-04-05 18:03:25 +08:00
|
|
|
|
2016-11-24 06:22:16 +08:00
|
|
|
// Setup MIPS _gp_disp/__gnu_local_gp symbols which should
|
|
|
|
// be equal to the _gp symbol's value.
|
2017-06-01 06:46:19 +08:00
|
|
|
if (Config->EMachine == EM_MIPS && !ElfSym::MipsGp->Value) {
|
|
|
|
// Find GP-relative section with the lowest address
|
|
|
|
// and use this address to calculate default _gp value.
|
2017-07-28 03:22:43 +08:00
|
|
|
for (const OutputSection *Cmd : OutputSections) {
|
|
|
|
const OutputSection *OS = Cmd;
|
2017-06-01 06:49:50 +08:00
|
|
|
if (OS->Flags & SHF_MIPS_GPREL) {
|
|
|
|
ElfSym::MipsGp->Value = OS->Addr + 0x7ff0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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);
|
2017-07-28 03:22:43 +08:00
|
|
|
EHdr->e_shnum = OutputSections.size() + 1;
|
2017-06-01 04:17:44 +08:00
|
|
|
EHdr->e_shstrndx = InX::ShStrTab->getParent()->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);
|
2017-07-27 15:46:50 +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;
|
2016-12-20 01:01:01 +08:00
|
|
|
++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-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
|
|
|
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-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Flags & SHF_ALLOC)
|
2017-07-28 03:22:43 +08:00
|
|
|
Sec->writeTo<ELFT>(Buf + Sec->Offset);
|
2016-08-25 17:05:47 +08:00
|
|
|
}
|
|
|
|
|
2017-08-15 05:18:12 +08:00
|
|
|
static void fillTrap(uint8_t *I, uint8_t *End) {
|
2017-08-21 16:31:14 +08:00
|
|
|
for (; I + 4 <= End; I += 4)
|
2017-08-03 00:35:00 +08:00
|
|
|
memcpy(I, &Target->TrapInstr, 4);
|
|
|
|
}
|
|
|
|
|
2017-08-15 05:18:12 +08:00
|
|
|
// Fill the last page of executable segments with trap instructions
|
|
|
|
// instead of leaving them as zero. Even though it is not required by any
|
|
|
|
// standard, it is in general a good thing to do for security reasons.
|
|
|
|
//
|
|
|
|
// We'll leave other pages in segments as-is because the rest will be
|
|
|
|
// overwritten by output sections.
|
2017-08-03 00:35:00 +08:00
|
|
|
template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
|
|
|
|
if (Script->Opt.HasSections)
|
|
|
|
return;
|
|
|
|
|
2017-08-15 05:18:12 +08:00
|
|
|
// Fill the last page.
|
2017-08-03 00:35:00 +08:00
|
|
|
uint8_t *Buf = Buffer->getBufferStart();
|
2017-08-15 05:18:12 +08:00
|
|
|
for (PhdrEntry *P : Phdrs)
|
|
|
|
if (P->p_type == PT_LOAD && (P->p_flags & PF_X))
|
|
|
|
fillTrap(Buf + alignDown(P->p_offset + P->p_filesz, Target->PageSize),
|
|
|
|
Buf + alignTo(P->p_offset + P->p_filesz, Target->PageSize));
|
2017-08-03 00:35:00 +08:00
|
|
|
|
2017-08-15 05:18:12 +08:00
|
|
|
// Round up the file size of the last segment to the page boundary iff it is
|
|
|
|
// an executable segment to ensure that other other tools don't accidentally
|
|
|
|
// trim the instruction padding (e.g. when stripping the file).
|
2017-08-03 00:35:00 +08:00
|
|
|
PhdrEntry *LastRX = nullptr;
|
|
|
|
for (PhdrEntry *P : Phdrs) {
|
|
|
|
if (P->p_type != PT_LOAD)
|
|
|
|
continue;
|
|
|
|
if (P->p_flags & PF_X)
|
|
|
|
LastRX = P;
|
|
|
|
else
|
|
|
|
LastRX = nullptr;
|
|
|
|
}
|
|
|
|
if (LastRX)
|
2017-09-02 05:48:20 +08:00
|
|
|
LastRX->p_memsz = LastRX->p_filesz =
|
|
|
|
alignTo(LastRX->p_filesz, Target->PageSize);
|
2017-08-03 00:35:00 +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-07-28 03:22:43 +08:00
|
|
|
if (auto *OpdCmd = findSection(".opd")) {
|
|
|
|
Out::Opd = OpdCmd;
|
2017-02-27 10:31:26 +08:00
|
|
|
Out::OpdBuf = Buf + Out::Opd->Offset;
|
2017-05-25 02:08:04 +08:00
|
|
|
OpdCmd->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 =
|
2017-05-27 01:28:17 +08:00
|
|
|
(In<ELFT>::EhFrameHdr && !In<ELFT>::EhFrameHdr->empty())
|
2017-06-01 04:17:44 +08:00
|
|
|
? In<ELFT>::EhFrameHdr->getParent()
|
2017-05-27 01:28:17 +08:00
|
|
|
: 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-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-02-11 09:40:49 +08:00
|
|
|
if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
|
2017-07-28 03:22:43 +08:00
|
|
|
Sec->writeTo<ELFT>(Buf + Sec->Offset);
|
2017-02-11 09:40:49 +08:00
|
|
|
|
2017-07-28 03:22:43 +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-07-28 03:22:43 +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-07-28 03:22:43 +08:00
|
|
|
if (EhFrameHdr)
|
|
|
|
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() {
|
2017-06-01 04:17:44 +08:00
|
|
|
if (!InX::BuildId || !InX::BuildId->getParent())
|
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;
|
2017-05-12 06:02:41 +08:00
|
|
|
InX::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>();
|