2015-09-22 05:38:08 +08:00
|
|
|
//===- OutputSections.cpp -------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-09-22 05:38:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "OutputSections.h"
|
|
|
|
#include "Config.h"
|
2016-11-02 07:09:07 +08:00
|
|
|
#include "LinkerScript.h"
|
2015-09-22 05:38:08 +08:00
|
|
|
#include "SymbolTable.h"
|
2016-11-06 07:05:47 +08:00
|
|
|
#include "SyntheticSections.h"
|
2015-09-23 02:19:46 +08:00
|
|
|
#include "Target.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2018-03-01 01:38:19 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
2017-07-28 03:22:43 +08:00
|
|
|
#include "llvm/Support/Compression.h"
|
2016-04-08 06:49:21 +08:00
|
|
|
#include "llvm/Support/MD5.h"
|
2015-10-22 16:21:35 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
[Support] Move LLD's parallel algorithm wrappers to support
Essentially takes the lld/Common/Threads.h wrappers and moves them to
the llvm/Support/Paralle.h algorithm header.
The changes are:
- Remove policy parameter, since all clients use `par`.
- Rename the methods to `parallelSort` etc to match LLVM style, since
they are no longer C++17 pstl compatible.
- Move algorithms from llvm::parallel:: to llvm::, since they have
"parallel" in the name and are no longer overloads of the regular
algorithms.
- Add range overloads
- Use the sequential algorithm directly when 1 thread is requested
(skips task grouping)
- Fix the index type of parallelForEachN to size_t. Nobody in LLVM was
using any other parameter, and it made overload resolution hard for
for_each_n(par, 0, foo.size(), ...) because 0 is int, not size_t.
Remove Threads.h and update LLD for that.
This is a prerequisite for parallel public symbol processing in the PDB
library, which is in LLVM.
Reviewed By: MaskRay, aganea
Differential Revision: https://reviews.llvm.org/D79390
2020-05-05 11:03:19 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2016-09-28 00:43:49 +08:00
|
|
|
#include "llvm/Support/SHA1.h"
|
2020-11-03 22:41:09 +08:00
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2019-10-26 02:04:56 +08:00
|
|
|
#include <regex>
|
2020-07-21 23:49:04 +08:00
|
|
|
#include <unordered_set>
|
2015-09-22 05:38:08 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2016-02-10 05:46:11 +08:00
|
|
|
using namespace llvm::dwarf;
|
2015-09-22 05:38:08 +08:00
|
|
|
using namespace llvm::object;
|
2018-11-15 05:05:20 +08:00
|
|
|
using namespace llvm::support::endian;
|
2015-09-22 05:38:08 +08:00
|
|
|
using namespace llvm::ELF;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2015-09-22 05:38:08 +08:00
|
|
|
|
2019-03-01 07:11:35 +08:00
|
|
|
uint8_t *Out::bufferStart;
|
2017-02-27 10:31:26 +08:00
|
|
|
uint8_t Out::first;
|
|
|
|
PhdrEntry *Out::tlsPhdr;
|
|
|
|
OutputSection *Out::elfHeader;
|
|
|
|
OutputSection *Out::programHeaders;
|
|
|
|
OutputSection *Out::preinitArray;
|
|
|
|
OutputSection *Out::initArray;
|
|
|
|
OutputSection *Out::finiArray;
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
std::vector<OutputSection *> elf::outputSections;
|
2017-06-14 07:26:31 +08:00
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
uint32_t OutputSection::getPhdrFlags() const {
|
2018-02-28 00:55:25 +08:00
|
|
|
uint32_t ret = 0;
|
|
|
|
if (config->emachine != EM_ARM || !(flags & SHF_ARM_PURECODE))
|
|
|
|
ret |= PF_R;
|
2016-07-27 22:10:56 +08:00
|
|
|
if (flags & SHF_WRITE)
|
|
|
|
ret |= PF_W;
|
|
|
|
if (flags & SHF_EXECINSTR)
|
|
|
|
ret |= PF_X;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-13 14:50:33 +08:00
|
|
|
template <class ELFT>
|
2017-02-24 23:07:30 +08:00
|
|
|
void OutputSection::writeHeaderTo(typename ELFT::Shdr *shdr) {
|
2016-11-09 09:42:41 +08:00
|
|
|
shdr->sh_entsize = entsize;
|
2017-03-07 22:55:52 +08:00
|
|
|
shdr->sh_addralign = alignment;
|
2016-11-09 09:42:41 +08:00
|
|
|
shdr->sh_type = type;
|
|
|
|
shdr->sh_offset = offset;
|
|
|
|
shdr->sh_flags = flags;
|
|
|
|
shdr->sh_info = info;
|
|
|
|
shdr->sh_link = link;
|
2017-02-08 23:19:03 +08:00
|
|
|
shdr->sh_addr = addr;
|
2016-11-09 09:42:41 +08:00
|
|
|
shdr->sh_size = size;
|
|
|
|
shdr->sh_name = shName;
|
2016-03-13 14:50:33 +08:00
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
OutputSection::OutputSection(StringRef name, uint32_t type, uint64_t flags)
|
2017-07-28 03:22:43 +08:00
|
|
|
: BaseCommand(OutputSectionKind),
|
|
|
|
SectionBase(Output, name, flags, /*Entsize*/ 0, /*Alignment*/ 1, type,
|
2019-03-12 10:18:03 +08:00
|
|
|
/*Info*/ 0, /*Link*/ 0) {}
|
2016-02-25 16:23:37 +08:00
|
|
|
|
2017-10-07 08:43:31 +08:00
|
|
|
// We allow sections of types listed below to merged into a
|
|
|
|
// single progbits section. This is typically done by linker
|
|
|
|
// scripts. Merging nobits and progbits will force disk space
|
|
|
|
// to be allocated for nobits sections. Other ones don't require
|
|
|
|
// any special treatment on top of progbits, so there doesn't
|
|
|
|
// seem to be a harm in merging them.
|
2020-08-13 11:14:00 +08:00
|
|
|
//
|
|
|
|
// NOTE: clang since rL252300 emits SHT_X86_64_UNWIND .eh_frame sections. Allow
|
|
|
|
// them to be merged into SHT_PROGBITS .eh_frame (GNU as .cfi_*).
|
2017-10-07 08:43:31 +08:00
|
|
|
static bool canMergeToProgbits(unsigned type) {
|
|
|
|
return type == SHT_NOBITS || type == SHT_PROGBITS || type == SHT_INIT_ARRAY ||
|
|
|
|
type == SHT_PREINIT_ARRAY || type == SHT_FINI_ARRAY ||
|
2020-08-13 11:14:00 +08:00
|
|
|
type == SHT_NOTE ||
|
|
|
|
(type == SHT_X86_64_UNWIND && config->emachine == EM_X86_64);
|
2017-10-07 08:43:31 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
// Record that isec will be placed in the OutputSection. isec does not become
|
|
|
|
// permanent until finalizeInputSections() is called. The function should not be
|
|
|
|
// used after finalizeInputSections() is called. If you need to add an
|
|
|
|
// InputSection post finalizeInputSections(), then you must do the following:
|
|
|
|
//
|
|
|
|
// 1. Find or create an InputSectionDescription to hold InputSection.
|
2020-01-07 02:21:05 +08:00
|
|
|
// 2. Add the InputSection to the InputSectionDescription::sections.
|
2019-09-24 19:48:31 +08:00
|
|
|
// 3. Call commitSection(isec).
|
|
|
|
void OutputSection::recordSection(InputSectionBase *isec) {
|
|
|
|
partition = isec->partition;
|
|
|
|
isec->parent = this;
|
|
|
|
if (sectionCommands.empty() ||
|
|
|
|
!isa<InputSectionDescription>(sectionCommands.back()))
|
|
|
|
sectionCommands.push_back(make<InputSectionDescription>(""));
|
|
|
|
auto *isd = cast<InputSectionDescription>(sectionCommands.back());
|
|
|
|
isd->sectionBases.push_back(isec);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update fields (type, flags, alignment, etc) according to the InputSection
|
|
|
|
// isec. Also check whether the InputSection flags and type are consistent with
|
|
|
|
// other InputSections.
|
|
|
|
void OutputSection::commitSection(InputSection *isec) {
|
2019-06-04 04:14:25 +08:00
|
|
|
if (!hasInputSections) {
|
2017-10-07 08:58:19 +08:00
|
|
|
// If IS is the first section to be added to this section,
|
2019-09-24 19:48:31 +08:00
|
|
|
// initialize type, entsize and flags from isec.
|
2019-06-04 04:14:25 +08:00
|
|
|
hasInputSections = true;
|
2017-10-07 08:58:19 +08:00
|
|
|
type = isec->type;
|
2017-11-16 01:35:22 +08:00
|
|
|
entsize = isec->entsize;
|
2018-02-28 00:55:25 +08:00
|
|
|
flags = isec->flags;
|
2017-10-07 08:58:19 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, check if new type or flags are compatible with existing ones.
|
2020-03-29 14:12:04 +08:00
|
|
|
if ((flags ^ isec->flags) & SHF_TLS)
|
2017-10-07 08:43:31 +08:00
|
|
|
error("incompatible section flags for " + name + "\n>>> " + toString(isec) +
|
|
|
|
": 0x" + utohexstr(isec->flags) + "\n>>> output section " + name +
|
|
|
|
": 0x" + utohexstr(flags));
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2017-10-07 08:43:31 +08:00
|
|
|
if (type != isec->type) {
|
|
|
|
if (!canMergeToProgbits(type) || !canMergeToProgbits(isec->type))
|
|
|
|
error("section type mismatch for " + isec->name + "\n>>> " +
|
|
|
|
toString(isec) + ": " +
|
|
|
|
getELFSectionTypeName(config->emachine, isec->type) +
|
|
|
|
"\n>>> output section " + name + ": " +
|
|
|
|
getELFSectionTypeName(config->emachine, type));
|
|
|
|
type = SHT_PROGBITS;
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 19:48:31 +08:00
|
|
|
if (noload)
|
|
|
|
type = SHT_NOBITS;
|
2017-10-07 08:43:31 +08:00
|
|
|
|
|
|
|
isec->parent = this;
|
2018-02-28 01:11:10 +08:00
|
|
|
uint64_t andMask =
|
|
|
|
config->emachine == EM_ARM ? (uint64_t)SHF_ARM_PURECODE : 0;
|
2018-02-28 00:55:25 +08:00
|
|
|
uint64_t orMask = ~andMask;
|
|
|
|
uint64_t andFlags = (flags & isec->flags) & andMask;
|
|
|
|
uint64_t orFlags = (flags | isec->flags) & orMask;
|
|
|
|
flags = andFlags | orFlags;
|
2019-09-24 19:48:31 +08:00
|
|
|
if (nonAlloc)
|
|
|
|
flags &= ~(uint64_t)SHF_ALLOC;
|
2018-02-28 00:55:25 +08:00
|
|
|
|
2017-10-07 08:58:34 +08:00
|
|
|
alignment = std::max(alignment, isec->alignment);
|
2017-05-08 18:18:12 +08:00
|
|
|
|
2017-03-01 12:04:23 +08:00
|
|
|
// If this section contains a table of fixed-size entries, sh_entsize
|
2017-11-16 01:35:22 +08:00
|
|
|
// holds the element size. If it contains elements of different size we
|
|
|
|
// set sh_entsize to 0.
|
|
|
|
if (entsize != isec->entsize)
|
|
|
|
entsize = 0;
|
2019-09-24 19:48:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function scans over the InputSectionBase list sectionBases to create
|
|
|
|
// InputSectionDescription::sections.
|
|
|
|
//
|
|
|
|
// It removes MergeInputSections from the input section array and adds
|
|
|
|
// new synthetic sections at the location of the first input section
|
|
|
|
// that it replaces. It then finalizes each synthetic section in order
|
|
|
|
// to compute an output offset for each piece of each input section.
|
|
|
|
void OutputSection::finalizeInputSections() {
|
|
|
|
std::vector<MergeSyntheticSection *> mergeSections;
|
|
|
|
for (BaseCommand *base : sectionCommands) {
|
|
|
|
auto *cmd = dyn_cast<InputSectionDescription>(base);
|
|
|
|
if (!cmd)
|
|
|
|
continue;
|
|
|
|
cmd->sections.reserve(cmd->sectionBases.size());
|
|
|
|
for (InputSectionBase *s : cmd->sectionBases) {
|
|
|
|
MergeInputSection *ms = dyn_cast<MergeInputSection>(s);
|
|
|
|
if (!ms) {
|
|
|
|
cmd->sections.push_back(cast<InputSection>(s));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not want to handle sections that are not alive, so just remove
|
|
|
|
// them instead of trying to merge.
|
|
|
|
if (!ms->isLive())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto i = llvm::find_if(mergeSections, [=](MergeSyntheticSection *sec) {
|
|
|
|
// While we could create a single synthetic section for two different
|
|
|
|
// values of Entsize, it is better to take Entsize into consideration.
|
|
|
|
//
|
|
|
|
// With a single synthetic section no two pieces with different Entsize
|
|
|
|
// could be equal, so we may as well have two sections.
|
|
|
|
//
|
|
|
|
// Using Entsize in here also allows us to propagate it to the synthetic
|
|
|
|
// section.
|
|
|
|
//
|
|
|
|
// SHF_STRINGS section with different alignments should not be merged.
|
|
|
|
return sec->flags == ms->flags && sec->entsize == ms->entsize &&
|
|
|
|
(sec->alignment == ms->alignment || !(sec->flags & SHF_STRINGS));
|
|
|
|
});
|
|
|
|
if (i == mergeSections.end()) {
|
|
|
|
MergeSyntheticSection *syn =
|
|
|
|
createMergeSynthetic(name, ms->type, ms->flags, ms->alignment);
|
|
|
|
mergeSections.push_back(syn);
|
|
|
|
i = std::prev(mergeSections.end());
|
|
|
|
syn->entsize = ms->entsize;
|
|
|
|
cmd->sections.push_back(syn);
|
|
|
|
}
|
|
|
|
(*i)->addSection(ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
// sectionBases should not be used from this point onwards. Clear it to
|
|
|
|
// catch misuses.
|
|
|
|
cmd->sectionBases.clear();
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
// Some input sections may be removed from the list after ICF.
|
|
|
|
for (InputSection *s : cmd->sections)
|
|
|
|
commitSection(s);
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
2019-09-24 19:48:31 +08:00
|
|
|
for (auto *ms : mergeSections)
|
|
|
|
ms->finalizeContents();
|
2015-09-22 05:38:08 +08:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:20:08 +08:00
|
|
|
static void sortByOrder(MutableArrayRef<InputSection *> in,
|
2018-06-16 20:11:34 +08:00
|
|
|
llvm::function_ref<int(InputSectionBase *s)> order) {
|
2019-04-23 10:42:06 +08:00
|
|
|
std::vector<std::pair<int, InputSection *>> v;
|
2017-08-04 18:25:29 +08:00
|
|
|
for (InputSection *s : in)
|
|
|
|
v.push_back({order(s), s});
|
2019-04-23 10:42:06 +08:00
|
|
|
llvm::stable_sort(v, less_first());
|
2017-08-04 18:25:29 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < v.size(); ++i)
|
|
|
|
in[i] = v[i].second;
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
uint64_t elf::getHeaderSize() {
|
2017-03-13 22:40:58 +08:00
|
|
|
if (config->oFormatBinary)
|
|
|
|
return 0;
|
|
|
|
return Out::elfHeader->size + Out::programHeaders->size;
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
bool OutputSection::classof(const BaseCommand *c) {
|
|
|
|
return c->kind == OutputSectionKind;
|
|
|
|
}
|
|
|
|
|
2018-06-16 20:11:34 +08:00
|
|
|
void OutputSection::sort(llvm::function_ref<int(InputSectionBase *s)> order) {
|
2019-05-29 11:55:20 +08:00
|
|
|
assert(isLive());
|
2018-02-02 03:30:15 +08:00
|
|
|
for (BaseCommand *b : sectionCommands)
|
|
|
|
if (auto *isd = dyn_cast<InputSectionDescription>(b))
|
|
|
|
sortByOrder(isd->sections, order);
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 21:48:18 +08:00
|
|
|
static void nopInstrFill(uint8_t *buf, size_t size) {
|
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
unsigned i = 0;
|
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
std::vector<std::vector<uint8_t>> nopFiller = *target->nopInstrs;
|
|
|
|
unsigned num = size / nopFiller.back().size();
|
|
|
|
for (unsigned c = 0; c < num; ++c) {
|
|
|
|
memcpy(buf + i, nopFiller.back().data(), nopFiller.back().size());
|
|
|
|
i += nopFiller.back().size();
|
|
|
|
}
|
|
|
|
unsigned remaining = size - i;
|
|
|
|
if (!remaining)
|
|
|
|
return;
|
|
|
|
assert(nopFiller[remaining - 1].size() == remaining);
|
|
|
|
memcpy(buf + i, nopFiller[remaining - 1].data(), remaining);
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
// Fill [Buf, Buf + Size) with Filler.
|
|
|
|
// This is used for linker script "=fillexp" command.
|
2018-11-15 05:05:20 +08:00
|
|
|
static void fill(uint8_t *buf, size_t size,
|
|
|
|
const std::array<uint8_t, 4> &filler) {
|
2017-07-28 03:22:43 +08:00
|
|
|
size_t i = 0;
|
|
|
|
for (; i + 4 < size; i += 4)
|
2018-11-15 05:05:20 +08:00
|
|
|
memcpy(buf + i, filler.data(), 4);
|
|
|
|
memcpy(buf + i, filler.data(), size - i);
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compress section contents if this section contains debug info.
|
|
|
|
template <class ELFT> void OutputSection::maybeCompress() {
|
2019-04-01 08:11:24 +08:00
|
|
|
using Elf_Chdr = typename ELFT::Chdr;
|
2017-07-28 03:22:43 +08:00
|
|
|
|
|
|
|
// Compress only DWARF debug sections.
|
|
|
|
if (!config->compressDebugSections || (flags & SHF_ALLOC) ||
|
|
|
|
!name.startswith(".debug_"))
|
|
|
|
return;
|
|
|
|
|
2020-11-03 22:41:09 +08:00
|
|
|
llvm::TimeTraceScope timeScope("Compress debug sections");
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
// Create a section header.
|
|
|
|
zDebugHeader.resize(sizeof(Elf_Chdr));
|
|
|
|
auto *hdr = reinterpret_cast<Elf_Chdr *>(zDebugHeader.data());
|
|
|
|
hdr->ch_type = ELFCOMPRESS_ZLIB;
|
|
|
|
hdr->ch_size = size;
|
|
|
|
hdr->ch_addralign = alignment;
|
|
|
|
|
|
|
|
// Write section contents to a temporary buffer and compress it.
|
|
|
|
std::vector<uint8_t> buf(size);
|
|
|
|
writeTo<ELFT>(buf.data());
|
2019-11-25 17:39:03 +08:00
|
|
|
// We chose 1 as the default compression level because it is the fastest. If
|
|
|
|
// -O2 is given, we use level 6 to compress debug info more by ~15%. We found
|
|
|
|
// that level 7 to 9 doesn't make much difference (~1% more compression) while
|
|
|
|
// they take significant amount of time (~2x), so level 6 seems enough.
|
|
|
|
if (Error e = zlib::compress(toStringRef(buf), compressedData,
|
|
|
|
config->optimize >= 2 ? 6 : 1))
|
2017-07-28 03:22:43 +08:00
|
|
|
fatal("compress failed: " + llvm::toString(std::move(e)));
|
|
|
|
|
|
|
|
// Update section headers.
|
|
|
|
size = sizeof(Elf_Chdr) + compressedData.size();
|
|
|
|
flags |= SHF_COMPRESSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeInt(uint8_t *buf, uint64_t data, uint64_t size) {
|
|
|
|
if (size == 1)
|
|
|
|
*buf = data;
|
|
|
|
else if (size == 2)
|
2018-03-21 02:07:25 +08:00
|
|
|
write16(buf, data);
|
2017-07-28 03:22:43 +08:00
|
|
|
else if (size == 4)
|
2018-03-21 02:07:25 +08:00
|
|
|
write32(buf, data);
|
2017-07-28 03:22:43 +08:00
|
|
|
else if (size == 8)
|
2018-03-21 02:07:25 +08:00
|
|
|
write64(buf, data);
|
2017-07-28 03:22:43 +08:00
|
|
|
else
|
|
|
|
llvm_unreachable("unsupported Size argument");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void OutputSection::writeTo(uint8_t *buf) {
|
|
|
|
if (type == SHT_NOBITS)
|
|
|
|
return;
|
|
|
|
|
2019-10-29 09:41:38 +08:00
|
|
|
// If -compress-debug-section is specified and if this is a debug section,
|
2017-07-28 03:22:43 +08:00
|
|
|
// we've already compressed section contents. If that's the case,
|
|
|
|
// just write it down.
|
|
|
|
if (!compressedData.empty()) {
|
|
|
|
memcpy(buf, zDebugHeader.data(), zDebugHeader.size());
|
|
|
|
memcpy(buf + zDebugHeader.size(), compressedData.data(),
|
|
|
|
compressedData.size());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write leading padding.
|
2018-02-22 17:55:28 +08:00
|
|
|
std::vector<InputSection *> sections = getInputSections(this);
|
2018-11-15 05:05:20 +08:00
|
|
|
std::array<uint8_t, 4> filler = getFiller();
|
|
|
|
bool nonZeroFiller = read32(filler.data()) != 0;
|
|
|
|
if (nonZeroFiller)
|
2017-07-28 03:22:43 +08:00
|
|
|
fill(buf, sections.empty() ? size : sections[0]->outSecOff, filler);
|
|
|
|
|
2017-10-11 13:13:16 +08:00
|
|
|
parallelForEachN(0, sections.size(), [&](size_t i) {
|
2017-07-28 03:22:43 +08:00
|
|
|
InputSection *isec = sections[i];
|
|
|
|
isec->writeTo<ELFT>(buf);
|
|
|
|
|
|
|
|
// Fill gaps between sections.
|
2018-11-15 05:05:20 +08:00
|
|
|
if (nonZeroFiller) {
|
2017-07-28 03:22:43 +08:00
|
|
|
uint8_t *start = buf + isec->outSecOff + isec->getSize();
|
|
|
|
uint8_t *end;
|
|
|
|
if (i + 1 == sections.size())
|
|
|
|
end = buf + size;
|
|
|
|
else
|
|
|
|
end = buf + sections[i + 1]->outSecOff;
|
2020-04-07 21:48:18 +08:00
|
|
|
if (isec->nopFiller) {
|
|
|
|
assert(target->nopInstrs);
|
|
|
|
nopInstrFill(start, end - start);
|
|
|
|
} else
|
|
|
|
fill(start, end - start, filler);
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Linker scripts may have BYTE()-family commands with which you
|
|
|
|
// can write arbitrary bytes to the output. Process them if any.
|
2017-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *base : sectionCommands)
|
2017-10-11 12:22:09 +08:00
|
|
|
if (auto *data = dyn_cast<ByteCommand>(base))
|
2017-07-28 03:22:43 +08:00
|
|
|
writeInt(buf + data->offset, data->expression().getValue(), data->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void finalizeShtGroup(OutputSection *os,
|
2017-12-12 21:38:51 +08:00
|
|
|
InputSection *section) {
|
|
|
|
assert(config->relocatable);
|
2017-07-28 03:22:43 +08:00
|
|
|
|
|
|
|
// sh_link field for SHT_GROUP sections should contain the section index of
|
|
|
|
// the symbol table.
|
2018-09-26 03:26:58 +08:00
|
|
|
os->link = in.symTab->getParent()->sectionIndex;
|
2017-07-28 03:22:43 +08:00
|
|
|
|
|
|
|
// sh_info then contain index of an entry in symbol table section which
|
|
|
|
// provides signature of the section group.
|
2019-03-06 11:07:57 +08:00
|
|
|
ArrayRef<Symbol *> symbols = section->file->getSymbols();
|
2018-09-26 03:26:58 +08:00
|
|
|
os->info = in.symTab->getSymbolIndex(symbols[section->info]);
|
2020-07-21 23:49:04 +08:00
|
|
|
|
|
|
|
// Some group members may be combined or discarded, so we need to compute the
|
|
|
|
// new size. The content will be rewritten in InputSection::copyShtGroup.
|
|
|
|
std::unordered_set<uint32_t> seen;
|
|
|
|
ArrayRef<InputSectionBase *> sections = section->file->getSections();
|
|
|
|
for (const uint32_t &idx : section->getDataAs<uint32_t>().slice(1))
|
|
|
|
if (OutputSection *osec = sections[read32(&idx)]->getOutputSection())
|
|
|
|
seen.insert(osec->sectionIndex);
|
|
|
|
os->size = (1 + seen.size()) * sizeof(uint32_t);
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 11:07:57 +08:00
|
|
|
void OutputSection::finalize() {
|
2020-01-16 21:23:08 +08:00
|
|
|
InputSection *first = getFirstInputSection(this);
|
2017-07-28 03:22:43 +08:00
|
|
|
|
2017-09-16 06:14:59 +08:00
|
|
|
if (flags & SHF_LINK_ORDER) {
|
2017-07-28 03:22:43 +08:00
|
|
|
// We must preserve the link order dependency of sections with the
|
|
|
|
// SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
|
|
|
|
// need to translate the InputSection sh_link to the OutputSection sh_link,
|
|
|
|
// all InputSections in the OutputSection have the same dependency.
|
2019-03-28 19:10:20 +08:00
|
|
|
if (auto *ex = dyn_cast<ARMExidxSyntheticSection>(first))
|
|
|
|
link = ex->getLinkOrderDep()->getParent()->sectionIndex;
|
2020-03-29 14:12:04 +08:00
|
|
|
else if (first->flags & SHF_LINK_ORDER)
|
|
|
|
if (auto *d = first->getLinkOrderDep())
|
|
|
|
link = d->getParent()->sectionIndex;
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == SHT_GROUP) {
|
2019-03-06 11:07:57 +08:00
|
|
|
finalizeShtGroup(this, first);
|
2017-07-28 03:22:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config->copyRelocs || (type != SHT_RELA && type != SHT_REL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isa<SyntheticSection>(first))
|
|
|
|
return;
|
|
|
|
|
2018-09-26 03:26:58 +08:00
|
|
|
link = in.symTab->getParent()->sectionIndex;
|
2017-07-28 03:22:43 +08:00
|
|
|
// sh_info for SHT_REL[A] sections should contain the section header index of
|
|
|
|
// the section to which the relocation applies.
|
|
|
|
InputSectionBase *s = first->getRelocatedSection();
|
|
|
|
info = s->getOutputSection()->sectionIndex;
|
|
|
|
flags |= SHF_INFO_LINK;
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:04:56 +08:00
|
|
|
// Returns true if S is in one of the many forms the compiler driver may pass
|
|
|
|
// crtbegin files.
|
|
|
|
//
|
|
|
|
// Gcc uses any of crtbegin[<empty>|S|T].o.
|
|
|
|
// Clang uses Gcc's plus clang_rt.crtbegin[<empty>|S|T][-<arch>|<empty>].o.
|
|
|
|
|
|
|
|
static bool isCrtbegin(StringRef s) {
|
|
|
|
static std::regex re(R"((clang_rt\.)?crtbegin[ST]?(-.*)?\.o)");
|
|
|
|
s = sys::path::filename(s);
|
|
|
|
return std::regex_match(s.begin(), s.end(), re);
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
|
|
|
|
2019-10-26 02:04:56 +08:00
|
|
|
static bool isCrtend(StringRef s) {
|
|
|
|
static std::regex re(R"((clang_rt\.)?crtend[ST]?(-.*)?\.o)");
|
|
|
|
s = sys::path::filename(s);
|
|
|
|
return std::regex_match(s.begin(), s.end(), re);
|
|
|
|
}
|
2017-07-28 03:22:43 +08:00
|
|
|
|
2020-11-13 00:56:12 +08:00
|
|
|
// .ctors and .dtors are sorted by this order:
|
2017-07-28 03:22:43 +08:00
|
|
|
//
|
2020-11-13 00:56:12 +08:00
|
|
|
// 1. .ctors/.dtors in crtbegin (which contains a sentinel value -1).
|
|
|
|
// 2. The section is named ".ctors" or ".dtors" (priority: 65536).
|
|
|
|
// 3. The section has an optional priority value in the form of ".ctors.N" or
|
|
|
|
// ".dtors.N" where N is a number in the form of %05u (priority: 65535-N).
|
|
|
|
// 4. .ctors/.dtors in crtend (which contains a sentinel value 0).
|
2017-07-28 03:22:43 +08:00
|
|
|
//
|
2020-11-13 00:56:12 +08:00
|
|
|
// For 2 and 3, the sections are sorted by priority from high to low, e.g.
|
|
|
|
// .ctors (65536), .ctors.00100 (65436), .ctors.00200 (65336). In GNU ld's
|
|
|
|
// internal linker scripts, the sorting is by string comparison which can
|
|
|
|
// achieve the same goal given the optional priority values are of the same
|
|
|
|
// length.
|
2017-07-28 03:22:43 +08:00
|
|
|
//
|
|
|
|
// In an ideal world, we don't need this function because .init_array and
|
|
|
|
// .ctors are duplicate features (and .init_array is newer.) However, there
|
|
|
|
// are too many real-world use cases of .ctors, so we had no choice to
|
|
|
|
// support that with this rather ad-hoc semantics.
|
|
|
|
static bool compCtors(const InputSection *a, const InputSection *b) {
|
|
|
|
bool beginA = isCrtbegin(a->file->getName());
|
|
|
|
bool beginB = isCrtbegin(b->file->getName());
|
|
|
|
if (beginA != beginB)
|
|
|
|
return beginA;
|
|
|
|
bool endA = isCrtend(a->file->getName());
|
|
|
|
bool endB = isCrtend(b->file->getName());
|
|
|
|
if (endA != endB)
|
|
|
|
return endB;
|
2020-11-13 00:56:12 +08:00
|
|
|
return getPriority(a->name) > getPriority(b->name);
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sorts input sections by the special rules for .ctors and .dtors.
|
|
|
|
// Unfortunately, the rules are different from the one for .{init,fini}_array.
|
|
|
|
// Read the comment above.
|
|
|
|
void OutputSection::sortCtorsDtors() {
|
2017-10-11 09:50:56 +08:00
|
|
|
assert(sectionCommands.size() == 1);
|
|
|
|
auto *isd = cast<InputSectionDescription>(sectionCommands[0]);
|
2019-04-23 10:42:06 +08:00
|
|
|
llvm::stable_sort(isd->sections, compCtors);
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-13 00:56:12 +08:00
|
|
|
// If an input string is in the form of "foo.N" where N is a number, return N
|
|
|
|
// (65535-N if .ctors.N or .dtors.N). Otherwise, returns 65536, which is one
|
|
|
|
// greater than the lowest priority.
|
2020-05-15 13:18:58 +08:00
|
|
|
int elf::getPriority(StringRef s) {
|
2017-07-28 03:22:43 +08:00
|
|
|
size_t pos = s.rfind('.');
|
|
|
|
if (pos == StringRef::npos)
|
|
|
|
return 65536;
|
2020-11-13 00:56:12 +08:00
|
|
|
int v = 65536;
|
|
|
|
if (to_integer(s.substr(pos + 1), v, 10) &&
|
|
|
|
(pos == 6 && (s.startswith(".ctors") || s.startswith(".dtors"))))
|
|
|
|
v = 65535 - v;
|
2017-07-28 03:22:43 +08:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
InputSection *elf::getFirstInputSection(const OutputSection *os) {
|
2020-01-16 21:23:08 +08:00
|
|
|
for (BaseCommand *base : os->sectionCommands)
|
|
|
|
if (auto *isd = dyn_cast<InputSectionDescription>(base))
|
|
|
|
if (!isd->sections.empty())
|
|
|
|
return isd->sections[0];
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
std::vector<InputSection *> elf::getInputSections(const OutputSection *os) {
|
2018-02-22 17:55:28 +08:00
|
|
|
std::vector<InputSection *> ret;
|
|
|
|
for (BaseCommand *base : os->sectionCommands)
|
|
|
|
if (auto *isd = dyn_cast<InputSectionDescription>(base))
|
|
|
|
ret.insert(ret.end(), isd->sections.begin(), isd->sections.end());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
// Sorts input sections by section name suffixes, so that .foo.N comes
|
|
|
|
// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
|
|
|
|
// We want to keep the original order if the priorities are the same
|
|
|
|
// because the compiler keeps the original initialization order in a
|
|
|
|
// translation unit and we need to respect that.
|
|
|
|
// For more detail, read the section of the GCC's manual about init_priority.
|
|
|
|
void OutputSection::sortInitFini() {
|
|
|
|
// Sort sections by priority.
|
|
|
|
sort([](InputSectionBase *s) { return getPriority(s->name); });
|
|
|
|
}
|
|
|
|
|
2018-11-15 05:05:20 +08:00
|
|
|
std::array<uint8_t, 4> OutputSection::getFiller() {
|
2017-07-28 03:22:43 +08:00
|
|
|
if (filler)
|
|
|
|
return *filler;
|
|
|
|
if (flags & SHF_EXECINSTR)
|
|
|
|
return target->trapInstr;
|
2018-11-15 05:05:20 +08:00
|
|
|
return {0, 0, 0, 0};
|
2017-07-28 03:22:43 +08:00
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
|
|
|
|
template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
|
|
|
|
template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
|
|
|
|
template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
|
2017-07-28 03:22:43 +08:00
|
|
|
|
|
|
|
template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf);
|
|
|
|
template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf);
|
|
|
|
template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf);
|
|
|
|
template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf);
|
|
|
|
|
|
|
|
template void OutputSection::maybeCompress<ELF32LE>();
|
|
|
|
template void OutputSection::maybeCompress<ELF32BE>();
|
|
|
|
template void OutputSection::maybeCompress<ELF64LE>();
|
|
|
|
template void OutputSection::maybeCompress<ELF64BE>();
|