2015-10-01 01:23:26 +08:00
|
|
|
//===- LinkerScript.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-10-01 01:23:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the parser/evaluator of the linker script.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
#include "LinkerScript.h"
|
2015-10-01 01:23:26 +08:00
|
|
|
#include "Config.h"
|
2016-02-13 05:47:28 +08:00
|
|
|
#include "InputSection.h"
|
2016-04-16 18:10:32 +08:00
|
|
|
#include "OutputSections.h"
|
2015-10-01 01:23:26 +08:00
|
|
|
#include "SymbolTable.h"
|
2016-10-29 04:57:25 +08:00
|
|
|
#include "Symbols.h"
|
2016-11-30 00:05:27 +08:00
|
|
|
#include "SyntheticSections.h"
|
2017-05-25 02:08:04 +08:00
|
|
|
#include "Target.h"
|
2016-07-19 17:25:43 +08:00
|
|
|
#include "Writer.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"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-10-01 01:23:26 +08:00
|
|
|
#include "llvm/Support/FileSystem.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"
|
2015-10-13 08:09:21 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2020-11-03 22:41:09 +08:00
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-10-01 01:23:26 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2016-04-16 18:10:32 +08:00
|
|
|
using namespace llvm::ELF;
|
2016-02-13 05:47:28 +08:00
|
|
|
using namespace llvm::object;
|
2016-09-27 03:22:50 +08:00
|
|
|
using namespace llvm::support::endian;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2015-10-01 01:23:26 +08:00
|
|
|
|
2021-12-23 06:36:14 +08:00
|
|
|
std::unique_ptr<LinkerScript> elf::script;
|
2017-03-22 07:03:09 +08:00
|
|
|
|
2021-11-21 14:18:09 +08:00
|
|
|
static bool isSectionPrefix(StringRef prefix, StringRef name) {
|
2021-12-15 15:43:00 +08:00
|
|
|
return name.consume_front(prefix) && (name.empty() || name[0] == '.');
|
2021-11-21 14:18:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef getOutputSectionName(const InputSectionBase *s) {
|
|
|
|
if (config->relocatable)
|
|
|
|
return s->name;
|
|
|
|
|
|
|
|
// This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
|
|
|
|
// to emit .rela.text.foo as .rela.text.bar for consistency (this is not
|
|
|
|
// technically required, but not doing it is odd). This code guarantees that.
|
|
|
|
if (auto *isec = dyn_cast<InputSection>(s)) {
|
|
|
|
if (InputSectionBase *rel = isec->getRelocatedSection()) {
|
|
|
|
OutputSection *out = rel->getOutputSection();
|
|
|
|
if (s->type == SHT_RELA)
|
|
|
|
return saver.save(".rela" + out->name);
|
|
|
|
return saver.save(".rel" + out->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A BssSection created for a common symbol is identified as "COMMON" in
|
|
|
|
// linker scripts. It should go to .bss section.
|
|
|
|
if (s->name == "COMMON")
|
|
|
|
return ".bss";
|
|
|
|
|
|
|
|
if (script->hasSectionsCommand)
|
|
|
|
return s->name;
|
|
|
|
|
|
|
|
// When no SECTIONS is specified, emulate GNU ld's internal linker scripts
|
|
|
|
// by grouping sections with certain prefixes.
|
|
|
|
|
|
|
|
// GNU ld places text sections with prefix ".text.hot.", ".text.unknown.",
|
|
|
|
// ".text.unlikely.", ".text.startup." or ".text.exit." before others.
|
|
|
|
// We provide an option -z keep-text-section-prefix to group such sections
|
|
|
|
// into separate output sections. This is more flexible. See also
|
|
|
|
// sortISDBySectionOrder().
|
|
|
|
// ".text.unknown" means the hotness of the section is unknown. When
|
|
|
|
// SampleFDO is used, if a function doesn't have sample, it could be very
|
|
|
|
// cold or it could be a new function never being sampled. Those functions
|
|
|
|
// will be kept in the ".text.unknown" section.
|
|
|
|
// ".text.split." holds symbols which are split out from functions in other
|
|
|
|
// input sections. For example, with -fsplit-machine-functions, placing the
|
|
|
|
// cold parts in .text.split instead of .text.unlikely mitigates against poor
|
|
|
|
// profile inaccuracy. Techniques such as hugepage remapping can make
|
|
|
|
// conservative decisions at the section granularity.
|
2021-12-16 02:20:10 +08:00
|
|
|
if (isSectionPrefix(".text", s->name)) {
|
|
|
|
if (config->zKeepTextSectionPrefix)
|
|
|
|
for (StringRef v : {".text.hot", ".text.unknown", ".text.unlikely",
|
|
|
|
".text.startup", ".text.exit", ".text.split"})
|
|
|
|
if (isSectionPrefix(v.substr(5), s->name.substr(5)))
|
|
|
|
return v;
|
|
|
|
return ".text";
|
|
|
|
}
|
2021-11-21 14:18:09 +08:00
|
|
|
|
|
|
|
for (StringRef v :
|
2021-12-16 02:20:10 +08:00
|
|
|
{".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
|
2021-12-15 16:18:58 +08:00
|
|
|
".gcc_except_table", ".init_array", ".fini_array", ".tbss", ".tdata",
|
|
|
|
".ARM.exidx", ".ARM.extab", ".ctors", ".dtors"})
|
2021-11-21 14:18:09 +08:00
|
|
|
if (isSectionPrefix(v, s->name))
|
2021-12-15 15:43:00 +08:00
|
|
|
return v;
|
2021-11-21 14:18:09 +08:00
|
|
|
|
|
|
|
return s->name;
|
|
|
|
}
|
|
|
|
|
2017-03-17 21:05:04 +08:00
|
|
|
uint64_t ExprValue::getValue() const {
|
2017-08-21 15:57:12 +08:00
|
|
|
if (sec)
|
2021-11-29 08:17:10 +08:00
|
|
|
return alignTo(sec->getOutputSection()->addr + sec->getOffset(val),
|
2017-08-21 15:57:12 +08:00
|
|
|
alignment);
|
2017-05-30 11:18:28 +08:00
|
|
|
return alignTo(val, alignment);
|
2017-03-17 21:05:04 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 22:55:36 +08:00
|
|
|
uint64_t ExprValue::getSecAddr() const {
|
2021-11-29 08:17:10 +08:00
|
|
|
return sec ? sec->getOutputSection()->addr + sec->getOffset(0) : 0;
|
2017-03-17 22:55:36 +08:00
|
|
|
}
|
|
|
|
|
2017-09-12 08:06:00 +08:00
|
|
|
uint64_t ExprValue::getSectionOffset() const {
|
2017-09-21 01:43:44 +08:00
|
|
|
// If the alignment is trivial, we don't have to compute the full
|
|
|
|
// value to know the offset. This allows this function to succeed in
|
|
|
|
// cases where the output section is not yet known.
|
2019-09-06 23:57:24 +08:00
|
|
|
if (alignment == 1 && !sec)
|
2017-09-21 01:43:44 +08:00
|
|
|
return val;
|
2017-09-12 08:06:00 +08:00
|
|
|
return getValue() - getSecAddr();
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *LinkerScript::createOutputSection(StringRef name,
|
|
|
|
StringRef location) {
|
|
|
|
OutputSection *&secRef = nameToOutputSection[name];
|
|
|
|
OutputSection *sec;
|
|
|
|
if (secRef && secRef->location.empty()) {
|
2017-06-01 09:16:50 +08:00
|
|
|
// There was a forward reference.
|
2017-07-28 03:22:43 +08:00
|
|
|
sec = secRef;
|
2017-06-01 09:16:50 +08:00
|
|
|
} else {
|
2019-04-23 20:38:52 +08:00
|
|
|
sec = make<OutputSection>(name, SHT_PROGBITS, 0);
|
2017-07-28 03:22:43 +08:00
|
|
|
if (!secRef)
|
|
|
|
secRef = sec;
|
2017-06-01 09:16:50 +08:00
|
|
|
}
|
2020-01-29 03:23:46 +08:00
|
|
|
sec->location = std::string(location);
|
2017-07-28 03:22:43 +08:00
|
|
|
return sec;
|
2017-03-14 18:15:53 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *LinkerScript::getOrCreateOutputSection(StringRef name) {
|
|
|
|
OutputSection *&cmdRef = nameToOutputSection[name];
|
2017-06-01 09:16:50 +08:00
|
|
|
if (!cmdRef)
|
2017-07-28 03:22:43 +08:00
|
|
|
cmdRef = make<OutputSection>(name, SHT_PROGBITS, 0);
|
2017-06-01 09:16:50 +08:00
|
|
|
return cmdRef;
|
2017-03-14 18:24:47 +08:00
|
|
|
}
|
|
|
|
|
2018-03-05 18:54:03 +08:00
|
|
|
// Expands the memory region by the specified size.
|
|
|
|
static void expandMemoryRegion(MemoryRegion *memRegion, uint64_t size,
|
2021-11-22 15:19:07 +08:00
|
|
|
StringRef secName) {
|
2018-03-05 18:54:03 +08:00
|
|
|
memRegion->curPos += size;
|
2020-03-07 03:49:58 +08:00
|
|
|
uint64_t newSize = memRegion->curPos - (memRegion->origin)().getValue();
|
|
|
|
uint64_t length = (memRegion->length)().getValue();
|
|
|
|
if (newSize > length)
|
2021-11-22 15:19:07 +08:00
|
|
|
error("section '" + secName + "' will not fit in region '" +
|
|
|
|
memRegion->name + "': overflowed by " + Twine(newSize - length) +
|
|
|
|
" bytes");
|
2018-03-05 18:54:03 +08:00
|
|
|
}
|
|
|
|
|
2018-03-26 16:58:16 +08:00
|
|
|
void LinkerScript::expandMemoryRegions(uint64_t size) {
|
2018-03-05 18:54:03 +08:00
|
|
|
if (ctx->memRegion)
|
2021-11-22 15:19:07 +08:00
|
|
|
expandMemoryRegion(ctx->memRegion, size, ctx->outSec->name);
|
2019-07-16 13:50:45 +08:00
|
|
|
// Only expand the LMARegion if it is different from memRegion.
|
2018-08-02 16:13:56 +08:00
|
|
|
if (ctx->lmaRegion && ctx->memRegion != ctx->lmaRegion)
|
2021-11-22 15:19:07 +08:00
|
|
|
expandMemoryRegion(ctx->lmaRegion, size, ctx->outSec->name);
|
2018-03-05 18:54:03 +08:00
|
|
|
}
|
|
|
|
|
2018-03-26 16:58:16 +08:00
|
|
|
void LinkerScript::expandOutputSection(uint64_t size) {
|
|
|
|
ctx->outSec->size += size;
|
|
|
|
expandMemoryRegions(size);
|
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::setDot(Expr e, const Twine &loc, bool inSec) {
|
2017-03-17 21:05:04 +08:00
|
|
|
uint64_t val = e().getValue();
|
2017-07-12 22:50:25 +08:00
|
|
|
if (val < dot && inSec)
|
|
|
|
error(loc + ": unable to move location counter backward for: " +
|
2017-10-11 10:45:54 +08:00
|
|
|
ctx->outSec->name);
|
2017-10-11 09:03:37 +08:00
|
|
|
|
2017-02-18 00:26:13 +08:00
|
|
|
// Update to location counter means update to section size.
|
|
|
|
if (inSec)
|
2018-03-05 18:54:03 +08:00
|
|
|
expandOutputSection(val - dot);
|
2018-07-05 18:44:17 +08:00
|
|
|
|
2018-03-05 18:54:03 +08:00
|
|
|
dot = val;
|
2017-02-18 00:26:13 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 17:04:27 +08:00
|
|
|
// Used for handling linker symbol assignments, for both finalizing
|
|
|
|
// their values and doing early declarations. Returns true if symbol
|
|
|
|
// should be defined from linker script.
|
|
|
|
static bool shouldDefineSym(SymbolAssignment *cmd) {
|
2017-10-11 09:03:37 +08:00
|
|
|
if (cmd->name == ".")
|
2018-01-30 17:04:27 +08:00
|
|
|
return false;
|
2017-10-11 09:03:37 +08:00
|
|
|
|
2018-01-30 17:04:27 +08:00
|
|
|
if (!cmd->provide)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If a symbol was in PROVIDE(), we need to define it only
|
|
|
|
// when it is a referenced undefined symbol.
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *b = symtab->find(cmd->name);
|
2018-02-27 15:18:07 +08:00
|
|
|
if (b && !b->isDefined())
|
|
|
|
return true;
|
|
|
|
return false;
|
2018-01-30 17:04:27 +08:00
|
|
|
}
|
|
|
|
|
2019-09-06 23:57:24 +08:00
|
|
|
// Called by processSymbolAssignments() to assign definitions to
|
|
|
|
// linker-script-defined symbols.
|
2018-01-30 17:04:27 +08:00
|
|
|
void LinkerScript::addSymbol(SymbolAssignment *cmd) {
|
|
|
|
if (!shouldDefineSym(cmd))
|
2017-10-11 09:03:37 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Define a symbol.
|
|
|
|
ExprValue value = cmd->expression();
|
|
|
|
SectionBase *sec = value.isAbsolute() ? nullptr : value.sec;
|
2019-05-16 10:14:00 +08:00
|
|
|
uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
|
2017-10-11 09:03:37 +08:00
|
|
|
|
|
|
|
// When this function is called, section addresses have not been
|
|
|
|
// fixed yet. So, we may or may not know the value of the RHS
|
|
|
|
// expression.
|
|
|
|
//
|
|
|
|
// For example, if an expression is `x = 42`, we know x is always 42.
|
|
|
|
// However, if an expression is `x = .`, there's no way to know its
|
|
|
|
// value at the moment.
|
|
|
|
//
|
|
|
|
// We want to set symbol values early if we can. This allows us to
|
|
|
|
// use symbols as variables in linker scripts. Doing so allows us to
|
|
|
|
// write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
|
|
|
|
uint64_t symValue = value.sec ? 0 : value.getValue();
|
|
|
|
|
2020-08-21 07:05:27 +08:00
|
|
|
Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, value.type,
|
2019-08-13 14:19:39 +08:00
|
|
|
symValue, 0, sec);
|
2019-05-16 10:14:00 +08:00
|
|
|
|
2019-05-17 09:55:20 +08:00
|
|
|
Symbol *sym = symtab->insert(cmd->name);
|
2019-08-13 14:19:39 +08:00
|
|
|
sym->mergeProperties(newSym);
|
|
|
|
sym->replace(newSym);
|
2017-11-06 12:35:31 +08:00
|
|
|
cmd->sym = cast<Defined>(sym);
|
2017-10-11 09:03:37 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 13:55:56 +08:00
|
|
|
// This function is called from LinkerScript::declareSymbols.
|
|
|
|
// It creates a placeholder symbol if needed.
|
|
|
|
static void declareSymbol(SymbolAssignment *cmd) {
|
|
|
|
if (!shouldDefineSym(cmd))
|
|
|
|
return;
|
|
|
|
|
2019-05-16 10:14:00 +08:00
|
|
|
uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
|
2019-08-13 14:19:39 +08:00
|
|
|
Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE, 0, 0,
|
|
|
|
nullptr);
|
2019-05-16 10:14:00 +08:00
|
|
|
|
2018-02-28 13:55:56 +08:00
|
|
|
// We can't calculate final value right now.
|
2019-05-17 09:55:20 +08:00
|
|
|
Symbol *sym = symtab->insert(cmd->name);
|
2019-08-13 14:19:39 +08:00
|
|
|
sym->mergeProperties(newSym);
|
|
|
|
sym->replace(newSym);
|
2019-05-16 10:14:00 +08:00
|
|
|
|
2018-02-28 13:55:56 +08:00
|
|
|
cmd->sym = cast<Defined>(sym);
|
|
|
|
cmd->provide = false;
|
2018-08-30 07:43:38 +08:00
|
|
|
sym->scriptDefined = true;
|
2018-02-28 13:55:56 +08:00
|
|
|
}
|
|
|
|
|
2019-08-26 18:23:31 +08:00
|
|
|
using SymbolAssignmentMap =
|
|
|
|
DenseMap<const Defined *, std::pair<SectionBase *, uint64_t>>;
|
|
|
|
|
|
|
|
// Collect section/value pairs of linker-script-defined symbols. This is used to
|
|
|
|
// check whether symbol values converge.
|
2021-12-27 05:06:54 +08:00
|
|
|
static SymbolAssignmentMap
|
|
|
|
getSymbolAssignmentValues(ArrayRef<SectionCommand *> sectionCommands) {
|
2019-08-26 18:23:31 +08:00
|
|
|
SymbolAssignmentMap ret;
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : sectionCommands) {
|
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
|
|
|
|
if (assign->sym) // sym is nullptr for dot.
|
|
|
|
ret.try_emplace(assign->sym, std::make_pair(assign->sym->section,
|
|
|
|
assign->sym->value));
|
2019-08-26 18:23:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *subCmd : cast<OutputSection>(cmd)->commands)
|
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(subCmd))
|
|
|
|
if (assign->sym)
|
|
|
|
ret.try_emplace(assign->sym, std::make_pair(assign->sym->section,
|
|
|
|
assign->sym->value));
|
2019-08-26 18:23:31 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the lexicographical smallest (for determinism) Defined whose
|
|
|
|
// section/value has changed.
|
|
|
|
static const Defined *
|
|
|
|
getChangedSymbolAssignment(const SymbolAssignmentMap &oldValues) {
|
|
|
|
const Defined *changed = nullptr;
|
|
|
|
for (auto &it : oldValues) {
|
|
|
|
const Defined *sym = it.first;
|
|
|
|
if (std::make_pair(sym->section, sym->value) != it.second &&
|
|
|
|
(!changed || sym->getName() < changed->getName()))
|
|
|
|
changed = sym;
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2020-02-11 07:58:29 +08:00
|
|
|
// Process INSERT [AFTER|BEFORE] commands. For each command, we move the
|
|
|
|
// specified output section to the designated place.
|
2018-03-08 22:54:38 +08:00
|
|
|
void LinkerScript::processInsertCommands() {
|
2021-12-27 05:53:47 +08:00
|
|
|
SmallVector<OutputSection *, 0> moves;
|
2020-02-11 07:58:29 +08:00
|
|
|
for (const InsertCommand &cmd : insertCommands) {
|
2021-07-01 02:35:50 +08:00
|
|
|
for (StringRef name : cmd.names) {
|
|
|
|
// If base is empty, it may have been discarded by
|
|
|
|
// adjustSectionsBeforeSorting(). We do not handle such output sections.
|
2021-11-26 12:24:23 +08:00
|
|
|
auto from = llvm::find_if(sectionCommands, [&](SectionCommand *subCmd) {
|
|
|
|
return isa<OutputSection>(subCmd) &&
|
|
|
|
cast<OutputSection>(subCmd)->name == name;
|
2021-07-01 02:35:50 +08:00
|
|
|
});
|
|
|
|
if (from == sectionCommands.end())
|
|
|
|
continue;
|
|
|
|
moves.push_back(cast<OutputSection>(*from));
|
|
|
|
sectionCommands.erase(from);
|
|
|
|
}
|
2020-02-11 07:58:29 +08:00
|
|
|
|
2021-11-26 12:24:23 +08:00
|
|
|
auto insertPos =
|
|
|
|
llvm::find_if(sectionCommands, [&cmd](SectionCommand *subCmd) {
|
|
|
|
auto *to = dyn_cast<OutputSection>(subCmd);
|
|
|
|
return to != nullptr && to->name == cmd.where;
|
|
|
|
});
|
2020-02-11 07:58:29 +08:00
|
|
|
if (insertPos == sectionCommands.end()) {
|
2021-07-01 02:35:50 +08:00
|
|
|
error("unable to insert " + cmd.names[0] +
|
2020-02-11 07:58:29 +08:00
|
|
|
(cmd.isAfter ? " after " : " before ") + cmd.where);
|
|
|
|
} else {
|
|
|
|
if (cmd.isAfter)
|
|
|
|
++insertPos;
|
2021-07-01 02:35:50 +08:00
|
|
|
sectionCommands.insert(insertPos, moves.begin(), moves.end());
|
2018-03-08 22:54:38 +08:00
|
|
|
}
|
2021-07-01 02:35:50 +08:00
|
|
|
moves.clear();
|
2018-03-08 22:54:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:04:27 +08:00
|
|
|
// Symbols defined in script should not be inlined by LTO. At the same time
|
|
|
|
// we don't know their final values until late stages of link. Here we scan
|
|
|
|
// over symbol assignment commands and create placeholder symbols if needed.
|
|
|
|
void LinkerScript::declareSymbols() {
|
|
|
|
assert(!ctx);
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : sectionCommands) {
|
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
|
|
|
|
declareSymbol(assign);
|
2018-01-30 17:04:27 +08:00
|
|
|
continue;
|
2018-02-28 13:55:56 +08:00
|
|
|
}
|
2018-07-05 22:09:47 +08:00
|
|
|
|
2018-02-28 13:55:56 +08:00
|
|
|
// If the output section directive has constraints,
|
|
|
|
// we can't say for sure if it is going to be included or not.
|
|
|
|
// Skip such sections for now. Improve the checks if we ever
|
|
|
|
// need symbols from that sections to be declared early.
|
2021-11-26 12:24:23 +08:00
|
|
|
auto *sec = cast<OutputSection>(cmd);
|
2018-02-28 13:55:56 +08:00
|
|
|
if (sec->constraint != ConstraintKind::NoConstraint)
|
|
|
|
continue;
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : sec->commands)
|
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
|
|
|
|
declareSymbol(assign);
|
2018-01-30 17:04:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 09:03:37 +08:00
|
|
|
// This function is called from assignAddresses, while we are
|
|
|
|
// fixing the output section addresses. This function is supposed
|
|
|
|
// to set the final value for a given symbol assignment.
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::assignSymbol(SymbolAssignment *cmd, bool inSec) {
|
2017-02-18 00:01:51 +08:00
|
|
|
if (cmd->name == ".") {
|
2017-02-21 22:50:38 +08:00
|
|
|
setDot(cmd->expression, cmd->location, inSec);
|
2017-02-18 00:01:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-07 18:23:28 +08:00
|
|
|
if (!cmd->sym)
|
2017-01-10 02:36:57 +08:00
|
|
|
return;
|
|
|
|
|
2017-03-17 21:05:04 +08:00
|
|
|
ExprValue v = cmd->expression();
|
|
|
|
if (v.isAbsolute()) {
|
2017-10-11 09:03:37 +08:00
|
|
|
cmd->sym->section = nullptr;
|
|
|
|
cmd->sym->value = v.getValue();
|
2017-03-17 21:05:04 +08:00
|
|
|
} else {
|
2017-10-11 09:03:37 +08:00
|
|
|
cmd->sym->section = v.sec;
|
|
|
|
cmd->sym->value = v.getSectionOffset();
|
2017-01-10 02:36:57 +08:00
|
|
|
}
|
2020-08-21 07:05:27 +08:00
|
|
|
cmd->sym->type = v.type;
|
2016-09-07 15:08:43 +08:00
|
|
|
}
|
2017-01-10 02:36:57 +08:00
|
|
|
|
2020-09-09 17:48:21 +08:00
|
|
|
static inline StringRef getFilename(const InputFile *file) {
|
|
|
|
return file ? file->getNameForScript() : StringRef();
|
2016-11-21 10:10:12 +08:00
|
|
|
}
|
|
|
|
|
2020-09-09 17:48:21 +08:00
|
|
|
bool InputSectionDescription::matchesFile(const InputFile *file) const {
|
|
|
|
if (filePat.isTrivialMatchAll())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!matchesFileCache || matchesFileCache->first != file)
|
|
|
|
matchesFileCache.emplace(file, filePat.match(getFilename(file)));
|
|
|
|
|
|
|
|
return matchesFileCache->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SectionPattern::excludesFile(const InputFile *file) const {
|
|
|
|
if (excludedFilePat.empty())
|
2017-12-07 03:13:23 +08:00
|
|
|
return false;
|
2020-09-09 17:48:21 +08:00
|
|
|
|
|
|
|
if (!excludesFileCache || excludesFileCache->first != file)
|
|
|
|
excludesFileCache.emplace(file, excludedFilePat.match(getFilename(file)));
|
|
|
|
|
|
|
|
return excludesFileCache->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinkerScript::shouldKeep(InputSectionBase *s) {
|
2017-10-11 09:26:22 +08:00
|
|
|
for (InputSectionDescription *id : keptSections)
|
2020-09-09 17:48:21 +08:00
|
|
|
if (id->matchesFile(s->file))
|
2016-11-21 10:10:12 +08:00
|
|
|
for (SectionPattern &p : id->sectionPatterns)
|
2020-01-15 17:38:00 +08:00
|
|
|
if (p.sectionPat.match(s->name) &&
|
|
|
|
(s->flags & id->withFlags) == id->withFlags &&
|
|
|
|
(s->flags & id->withoutFlags) == 0)
|
2016-11-21 10:10:12 +08:00
|
|
|
return true;
|
2016-07-21 22:26:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:43:25 +08:00
|
|
|
// A helper function for the SORT() command.
|
2019-09-24 19:48:31 +08:00
|
|
|
static bool matchConstraints(ArrayRef<InputSectionBase *> sections,
|
2016-08-12 17:07:57 +08:00
|
|
|
ConstraintKind kind) {
|
2016-08-13 04:38:20 +08:00
|
|
|
if (kind == ConstraintKind::NoConstraint)
|
|
|
|
return true;
|
2017-04-05 08:43:45 +08:00
|
|
|
|
2017-10-11 10:55:05 +08:00
|
|
|
bool isRW = llvm::any_of(
|
2019-09-24 19:48:31 +08:00
|
|
|
sections, [](InputSectionBase *sec) { return sec->flags & SHF_WRITE; });
|
2017-04-05 08:43:45 +08:00
|
|
|
|
2016-09-22 02:33:44 +08:00
|
|
|
return (isRW && kind == ConstraintKind::ReadWrite) ||
|
|
|
|
(!isRW && kind == ConstraintKind::ReadOnly);
|
2016-08-12 17:07:57 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
static void sortSections(MutableArrayRef<InputSectionBase *> vec,
|
2016-09-21 03:42:41 +08:00
|
|
|
SortSectionPolicy k) {
|
2019-08-16 01:47:19 +08:00
|
|
|
auto alignmentComparator = [](InputSectionBase *a, InputSectionBase *b) {
|
|
|
|
// ">" is not a mistake. Sections with larger alignments are placed
|
|
|
|
// before sections with smaller alignments in order to reduce the
|
|
|
|
// amount of padding necessary. This is compatible with GNU.
|
|
|
|
return a->alignment > b->alignment;
|
|
|
|
};
|
|
|
|
auto nameComparator = [](InputSectionBase *a, InputSectionBase *b) {
|
|
|
|
return a->name < b->name;
|
|
|
|
};
|
|
|
|
auto priorityComparator = [](InputSectionBase *a, InputSectionBase *b) {
|
|
|
|
return getPriority(a->name) < getPriority(b->name);
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (k) {
|
|
|
|
case SortSectionPolicy::Default:
|
|
|
|
case SortSectionPolicy::None:
|
|
|
|
return;
|
|
|
|
case SortSectionPolicy::Alignment:
|
|
|
|
return llvm::stable_sort(vec, alignmentComparator);
|
|
|
|
case SortSectionPolicy::Name:
|
|
|
|
return llvm::stable_sort(vec, nameComparator);
|
|
|
|
case SortSectionPolicy::Priority:
|
|
|
|
return llvm::stable_sort(vec, priorityComparator);
|
|
|
|
}
|
2016-09-21 03:42:41 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 12:50:30 +08:00
|
|
|
// Sort sections as instructed by SORT-family commands and --sort-section
|
|
|
|
// option. Because SORT-family commands can be nested at most two depth
|
|
|
|
// (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
|
|
|
|
// line option is respected even if a SORT command is given, the exact
|
|
|
|
// behavior we have here is a bit complicated. Here are the rules.
|
|
|
|
//
|
|
|
|
// 1. If two SORT commands are given, --sort-section is ignored.
|
|
|
|
// 2. If one SORT command is given, and if it is not SORT_NONE,
|
|
|
|
// --sort-section is handled as an inner SORT command.
|
|
|
|
// 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
|
|
|
|
// 4. If no SORT command is given, sort according to --sort-section.
|
2019-09-24 19:48:31 +08:00
|
|
|
static void sortInputSections(MutableArrayRef<InputSectionBase *> vec,
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
SortSectionPolicy outer,
|
|
|
|
SortSectionPolicy inner) {
|
|
|
|
if (outer == SortSectionPolicy::None)
|
2017-10-11 12:50:30 +08:00
|
|
|
return;
|
|
|
|
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
if (inner == SortSectionPolicy::Default)
|
2017-10-11 12:50:30 +08:00
|
|
|
sortSections(vec, config->sortSection);
|
|
|
|
else
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
sortSections(vec, inner);
|
|
|
|
sortSections(vec, outer);
|
2017-10-11 12:50:30 +08:00
|
|
|
}
|
|
|
|
|
2016-09-16 23:10:23 +08:00
|
|
|
// Compute and remember which sections the InputSectionDescription matches.
|
2021-12-27 05:06:54 +08:00
|
|
|
SmallVector<InputSectionBase *, 0>
|
2020-05-08 20:19:12 +08:00
|
|
|
LinkerScript::computeInputSections(const InputSectionDescription *cmd,
|
|
|
|
ArrayRef<InputSectionBase *> sections) {
|
2021-12-27 05:06:54 +08:00
|
|
|
SmallVector<InputSectionBase *, 0> ret;
|
2021-12-27 05:53:47 +08:00
|
|
|
SmallVector<size_t, 0> indexes;
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
DenseSet<size_t> seen;
|
|
|
|
auto sortByPositionThenCommandLine = [&](size_t begin, size_t end) {
|
|
|
|
llvm::sort(MutableArrayRef<size_t>(indexes).slice(begin, end - begin));
|
|
|
|
for (size_t i = begin; i != end; ++i)
|
|
|
|
ret[i] = sections[indexes[i]];
|
|
|
|
sortInputSections(
|
|
|
|
MutableArrayRef<InputSectionBase *>(ret).slice(begin, end - begin),
|
|
|
|
config->sortSection, SortSectionPolicy::None);
|
|
|
|
};
|
2017-04-05 10:05:48 +08:00
|
|
|
|
|
|
|
// Collects all sections that satisfy constraints of Cmd.
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
size_t sizeAfterPrevSort = 0;
|
2017-04-05 10:05:48 +08:00
|
|
|
for (const SectionPattern &pat : cmd->sectionPatterns) {
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
size_t sizeBeforeCurrPat = ret.size();
|
2017-04-05 10:05:48 +08:00
|
|
|
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
for (size_t i = 0, e = sections.size(); i != e; ++i) {
|
|
|
|
// Skip if the section is dead or has been matched by a previous input
|
|
|
|
// section description or a previous pattern.
|
|
|
|
InputSectionBase *sec = sections[i];
|
|
|
|
if (!sec->isLive() || sec->parent || seen.contains(i))
|
2017-05-30 13:17:58 +08:00
|
|
|
continue;
|
|
|
|
|
2021-10-26 03:52:06 +08:00
|
|
|
// For --emit-relocs we have to ignore entries like
|
2017-02-16 22:36:09 +08:00
|
|
|
// .rela.dyn : { *(.rela.data) }
|
|
|
|
// which are common because they are in the default bfd script.
|
2018-03-24 21:10:19 +08:00
|
|
|
// We do not ignore SHT_REL[A] linker-synthesized sections here because
|
|
|
|
// want to support scripts that do custom layout for them.
|
2019-09-24 19:48:31 +08:00
|
|
|
if (isa<InputSection>(sec) &&
|
|
|
|
cast<InputSection>(sec)->getRelocatedSection())
|
2019-08-25 22:41:18 +08:00
|
|
|
continue;
|
2016-11-06 06:37:59 +08:00
|
|
|
|
2020-01-15 19:48:37 +08:00
|
|
|
// Check the name early to improve performance in the common case.
|
|
|
|
if (!pat.sectionPat.match(sec->name))
|
|
|
|
continue;
|
|
|
|
|
2020-09-09 17:48:21 +08:00
|
|
|
if (!cmd->matchesFile(sec->file) || pat.excludesFile(sec->file) ||
|
2020-01-15 17:38:00 +08:00
|
|
|
(sec->flags & cmd->withFlags) != cmd->withFlags ||
|
|
|
|
(sec->flags & cmd->withoutFlags) != 0)
|
2016-11-21 10:10:12 +08:00
|
|
|
continue;
|
2017-04-05 10:05:48 +08:00
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
ret.push_back(sec);
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
indexes.push_back(i);
|
|
|
|
seen.insert(i);
|
2016-09-17 01:42:10 +08:00
|
|
|
}
|
2016-09-16 23:10:23 +08:00
|
|
|
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
if (pat.sortOuter == SortSectionPolicy::Default)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Matched sections are ordered by radix sort with the keys being (SORT*,
|
|
|
|
// --sort-section, input order), where SORT* (if present) is most
|
|
|
|
// significant.
|
|
|
|
//
|
|
|
|
// Matched sections between the previous SORT* and this SORT* are sorted by
|
|
|
|
// (--sort-alignment, input order).
|
|
|
|
sortByPositionThenCommandLine(sizeAfterPrevSort, sizeBeforeCurrPat);
|
|
|
|
// Matched sections by this SORT* pattern are sorted using all 3 keys.
|
|
|
|
// ret[sizeBeforeCurrPat,ret.size()) are already in the input order, so we
|
|
|
|
// just sort by sortOuter and sortInner.
|
2019-09-24 19:48:31 +08:00
|
|
|
sortInputSections(
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
MutableArrayRef<InputSectionBase *>(ret).slice(sizeBeforeCurrPat),
|
|
|
|
pat.sortOuter, pat.sortInner);
|
|
|
|
sizeAfterPrevSort = ret.size();
|
2016-09-21 03:42:41 +08:00
|
|
|
}
|
[ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:
* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order
This patch implements the behavior. The interaction with `SORT*` and --sort-section is:
Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.
> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.
As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order. If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.
This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.
The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096
Reviewed By: grimar, psmith
Differential Revision: https://reviews.llvm.org/D91127
2020-11-13 00:53:11 +08:00
|
|
|
// Matched sections after the last SORT* are sorted by (--sort-alignment,
|
|
|
|
// input order).
|
|
|
|
sortByPositionThenCommandLine(sizeAfterPrevSort, ret.size());
|
2017-04-05 10:05:48 +08:00
|
|
|
return ret;
|
2016-09-14 22:32:08 +08:00
|
|
|
}
|
|
|
|
|
2021-12-23 12:51:11 +08:00
|
|
|
void LinkerScript::discard(InputSectionBase &s) {
|
2021-12-23 15:55:11 +08:00
|
|
|
if (&s == in.shStrTab || &s == mainPart->relrDyn)
|
2021-12-23 12:51:11 +08:00
|
|
|
error("discarding " + s.name + " section is not allowed");
|
2019-09-24 19:48:31 +08:00
|
|
|
|
|
|
|
// You can discard .hash and .gnu.hash sections by linker scripts. Since
|
|
|
|
// they are synthesized sections, we need to handle them differently than
|
|
|
|
// other regular sections.
|
2021-12-23 15:55:11 +08:00
|
|
|
if (&s == mainPart->gnuHashTab)
|
2019-09-24 19:48:31 +08:00
|
|
|
mainPart->gnuHashTab = nullptr;
|
2021-12-23 15:55:11 +08:00
|
|
|
if (&s == mainPart->hashTab)
|
2019-09-24 19:48:31 +08:00
|
|
|
mainPart->hashTab = nullptr;
|
|
|
|
|
2021-12-23 12:51:11 +08:00
|
|
|
s.markDead();
|
|
|
|
s.parent = nullptr;
|
|
|
|
for (InputSection *sec : s.dependentSections)
|
|
|
|
discard(*sec);
|
2016-09-14 22:32:08 +08:00
|
|
|
}
|
|
|
|
|
2020-05-08 20:19:12 +08:00
|
|
|
void LinkerScript::discardSynthetic(OutputSection &outCmd) {
|
|
|
|
for (Partition &part : partitions) {
|
|
|
|
if (!part.armExidx || !part.armExidx->isLive())
|
|
|
|
continue;
|
|
|
|
std::vector<InputSectionBase *> secs(part.armExidx->exidxSections.begin(),
|
|
|
|
part.armExidx->exidxSections.end());
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : outCmd.commands)
|
2021-12-27 05:06:54 +08:00
|
|
|
if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
|
|
|
|
for (InputSectionBase *s : computeInputSections(isd, secs))
|
2021-12-23 12:51:11 +08:00
|
|
|
discard(*s);
|
2020-05-08 20:19:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 05:06:54 +08:00
|
|
|
SmallVector<InputSectionBase *, 0>
|
2018-01-31 00:20:08 +08:00
|
|
|
LinkerScript::createInputSectionList(OutputSection &outCmd) {
|
2021-12-27 05:06:54 +08:00
|
|
|
SmallVector<InputSectionBase *, 0> ret;
|
2016-08-12 11:16:56 +08:00
|
|
|
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : outCmd.commands) {
|
|
|
|
if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) {
|
|
|
|
isd->sectionBases = computeInputSections(isd, inputSections);
|
|
|
|
for (InputSectionBase *s : isd->sectionBases)
|
2019-09-24 19:48:46 +08:00
|
|
|
s->parent = &outCmd;
|
2021-11-26 12:24:23 +08:00
|
|
|
ret.insert(ret.end(), isd->sectionBases.begin(), isd->sectionBases.end());
|
2017-10-11 12:01:13 +08:00
|
|
|
}
|
2016-08-12 11:16:56 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
|
2019-09-06 23:57:24 +08:00
|
|
|
// Create output sections described by SECTIONS commands.
|
2017-10-21 07:28:19 +08:00
|
|
|
void LinkerScript::processSectionCommands() {
|
2021-06-14 03:41:11 +08:00
|
|
|
auto process = [this](OutputSection *osec) {
|
2021-12-27 05:06:54 +08:00
|
|
|
SmallVector<InputSectionBase *, 0> v = createInputSectionList(*osec);
|
2021-06-14 03:41:11 +08:00
|
|
|
|
|
|
|
// The output section name `/DISCARD/' is special.
|
|
|
|
// Any input section assigned to it is discarded.
|
|
|
|
if (osec->name == "/DISCARD/") {
|
|
|
|
for (InputSectionBase *s : v)
|
2021-12-23 12:51:11 +08:00
|
|
|
discard(*s);
|
2021-06-14 03:41:11 +08:00
|
|
|
discardSynthetic(*osec);
|
2021-11-26 08:47:07 +08:00
|
|
|
osec->commands.clear();
|
2021-06-14 03:41:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-12 11:16:56 +08:00
|
|
|
|
2021-06-14 03:41:11 +08:00
|
|
|
// This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
|
|
|
|
// ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
|
|
|
|
// sections satisfy a given constraint. If not, a directive is handled
|
|
|
|
// as if it wasn't present from the beginning.
|
|
|
|
//
|
|
|
|
// Because we'll iterate over SectionCommands many more times, the easy
|
|
|
|
// way to "make it as if it wasn't present" is to make it empty.
|
|
|
|
if (!matchConstraints(v, osec->constraint)) {
|
|
|
|
for (InputSectionBase *s : v)
|
|
|
|
s->parent = nullptr;
|
2021-11-26 08:47:07 +08:00
|
|
|
osec->commands.clear();
|
2021-06-14 03:41:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-17 05:05:36 +08:00
|
|
|
|
2021-06-14 03:41:11 +08:00
|
|
|
// Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
|
|
|
|
// is given, input sections are aligned to that value, whether the
|
|
|
|
// given value is larger or smaller than the original section alignment.
|
|
|
|
if (osec->subalignExpr) {
|
|
|
|
uint32_t subalign = osec->subalignExpr().getValue();
|
|
|
|
for (InputSectionBase *s : v)
|
|
|
|
s->alignment = subalign;
|
|
|
|
}
|
2016-11-21 10:11:05 +08:00
|
|
|
|
2021-06-14 03:41:11 +08:00
|
|
|
// Set the partition field the same way OutputSection::recordSection()
|
|
|
|
// does. Partitions cannot be used with the SECTIONS command, so this is
|
|
|
|
// always 1.
|
|
|
|
osec->partition = 1;
|
|
|
|
return true;
|
|
|
|
};
|
2019-09-27 01:10:09 +08:00
|
|
|
|
2021-06-14 03:41:11 +08:00
|
|
|
// Process OVERWRITE_SECTIONS first so that it can overwrite the main script
|
|
|
|
// or orphans.
|
|
|
|
DenseMap<StringRef, OutputSection *> map;
|
|
|
|
size_t i = 0;
|
|
|
|
for (OutputSection *osec : overwriteSections)
|
|
|
|
if (process(osec) && !map.try_emplace(osec->name, osec).second)
|
|
|
|
warn("OVERWRITE_SECTIONS specifies duplicate " + osec->name);
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *&base : sectionCommands)
|
2021-06-14 03:41:11 +08:00
|
|
|
if (auto *osec = dyn_cast<OutputSection>(base)) {
|
|
|
|
if (OutputSection *overwrite = map.lookup(osec->name)) {
|
|
|
|
log(overwrite->location + " overwrites " + osec->name);
|
|
|
|
overwrite->sectionIndex = i++;
|
|
|
|
base = overwrite;
|
|
|
|
} else if (process(osec)) {
|
|
|
|
osec->sectionIndex = i++;
|
|
|
|
}
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
2021-06-14 03:41:11 +08:00
|
|
|
|
|
|
|
// If an OVERWRITE_SECTIONS specified output section is not in
|
|
|
|
// sectionCommands, append it to the end. The section will be inserted by
|
|
|
|
// orphan placement.
|
|
|
|
for (OutputSection *osec : overwriteSections)
|
|
|
|
if (osec->partition == 1 && osec->sectionIndex == UINT32_MAX)
|
|
|
|
sectionCommands.push_back(osec);
|
2019-09-06 23:57:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinkerScript::processSymbolAssignments() {
|
|
|
|
// Dot outside an output section still represents a relative address, whose
|
|
|
|
// sh_shndx should not be SHN_UNDEF or SHN_ABS. Create a dummy aether section
|
|
|
|
// that fills the void outside a section. It has an index of one, which is
|
|
|
|
// indistinguishable from any other regular section index.
|
|
|
|
aether = make<OutputSection>("", 0, SHF_ALLOC);
|
|
|
|
aether->sectionIndex = 1;
|
|
|
|
|
|
|
|
// ctx captures the local AddressState and makes it accessible deliberately.
|
|
|
|
// This is needed as there are some cases where we cannot just thread the
|
|
|
|
// current state through to a lambda function created by the script parser.
|
|
|
|
AddressState state;
|
|
|
|
ctx = &state;
|
|
|
|
ctx->outSec = aether;
|
|
|
|
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : sectionCommands) {
|
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
|
|
|
|
addSymbol(assign);
|
2019-09-06 23:57:24 +08:00
|
|
|
else
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *subCmd : cast<OutputSection>(cmd)->commands)
|
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(subCmd))
|
|
|
|
addSymbol(assign);
|
2019-09-06 23:57:24 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 10:45:54 +08:00
|
|
|
ctx = nullptr;
|
2016-09-16 23:30:47 +08:00
|
|
|
}
|
2016-07-20 22:43:20 +08:00
|
|
|
|
2021-11-26 12:24:23 +08:00
|
|
|
static OutputSection *findByName(ArrayRef<SectionCommand *> vec,
|
2017-10-07 07:06:55 +08:00
|
|
|
StringRef name) {
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : vec)
|
|
|
|
if (auto *sec = dyn_cast<OutputSection>(cmd))
|
2017-10-07 07:06:55 +08:00
|
|
|
if (sec->name == name)
|
|
|
|
return sec;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-05 07:54:25 +08:00
|
|
|
static OutputSection *createSection(InputSectionBase *isec,
|
|
|
|
StringRef outsecName) {
|
|
|
|
OutputSection *sec = script->createOutputSection(outsecName, "<internal>");
|
2019-09-24 19:48:31 +08:00
|
|
|
sec->recordSection(isec);
|
2017-11-05 07:54:25 +08:00
|
|
|
return sec;
|
|
|
|
}
|
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
static OutputSection *
|
|
|
|
addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
|
|
|
|
InputSectionBase *isec, StringRef outsecName) {
|
2017-11-05 07:54:25 +08:00
|
|
|
// Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
|
|
|
|
// option is given. A section with SHT_GROUP defines a "section group", and
|
|
|
|
// its members have SHF_GROUP attribute. Usually these flags have already been
|
|
|
|
// stripped by InputFiles.cpp as section groups are processed and uniquified.
|
|
|
|
// However, for the -r option, we want to pass through all section groups
|
|
|
|
// as-is because adding/removing members or merging them with other groups
|
|
|
|
// change their semantics.
|
|
|
|
if (isec->type == SHT_GROUP || (isec->flags & SHF_GROUP))
|
|
|
|
return createSection(isec, outsecName);
|
|
|
|
|
|
|
|
// Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
|
|
|
|
// relocation sections .rela.foo and .rela.bar for example. Most tools do
|
|
|
|
// not allow multiple REL[A] sections for output section. Hence we
|
|
|
|
// should combine these relocation sections into single output.
|
|
|
|
// We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
|
|
|
|
// other REL[A] sections created by linker itself.
|
|
|
|
if (!isa<SyntheticSection>(isec) &&
|
|
|
|
(isec->type == SHT_REL || isec->type == SHT_RELA)) {
|
|
|
|
auto *sec = cast<InputSection>(isec);
|
|
|
|
OutputSection *out = sec->getRelocatedSection()->getOutputSection();
|
|
|
|
|
|
|
|
if (out->relocationSection) {
|
2019-09-24 19:48:31 +08:00
|
|
|
out->relocationSection->recordSection(sec);
|
2017-11-05 07:54:25 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
out->relocationSection = createSection(isec, outsecName);
|
|
|
|
return out->relocationSection;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The ELF spec just says
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// In the first phase, input sections that match in name, type and
|
|
|
|
// attribute flags should be concatenated into single sections.
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// However, it is clear that at least some flags have to be ignored for
|
|
|
|
// section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
|
|
|
|
// ignored. We should not have two output .text sections just because one was
|
|
|
|
// in a group and another was not for example.
|
|
|
|
//
|
2018-02-23 10:05:48 +08:00
|
|
|
// It also seems that wording was a late addition and didn't get the
|
2017-11-05 07:54:25 +08:00
|
|
|
// necessary scrutiny.
|
|
|
|
//
|
|
|
|
// Merging sections with different flags is expected by some users. One
|
|
|
|
// reason is that if one file has
|
|
|
|
//
|
|
|
|
// int *const bar __attribute__((section(".foo"))) = (int *)0;
|
|
|
|
//
|
|
|
|
// gcc with -fPIC will produce a read only .foo section. But if another
|
|
|
|
// file has
|
|
|
|
//
|
|
|
|
// int zed;
|
|
|
|
// int *const bar __attribute__((section(".foo"))) = (int *)&zed;
|
|
|
|
//
|
|
|
|
// gcc with -fPIC will produce a read write section.
|
|
|
|
//
|
|
|
|
// Last but not least, when using linker script the merge rules are forced by
|
|
|
|
// the script. Unfortunately, linker scripts are name based. This means that
|
|
|
|
// expressions like *(.foo*) can refer to multiple input sections with
|
|
|
|
// different flags. We cannot put them in different output sections or we
|
|
|
|
// would produce wrong results for
|
|
|
|
//
|
|
|
|
// start = .; *(.foo.*) end = .; *(.bar)
|
|
|
|
//
|
|
|
|
// and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
|
|
|
|
// another. The problem is that there is no way to layout those output
|
|
|
|
// sections such that the .foo sections are the only thing between the start
|
|
|
|
// and end symbols.
|
|
|
|
//
|
|
|
|
// Given the above issues, we instead merge sections by name and error on
|
|
|
|
// incompatible types and flags.
|
2019-05-29 11:55:20 +08:00
|
|
|
TinyPtrVector<OutputSection *> &v = map[outsecName];
|
|
|
|
for (OutputSection *sec : v) {
|
|
|
|
if (sec->partition != isec->partition)
|
|
|
|
continue;
|
2019-10-01 04:23:00 +08:00
|
|
|
|
|
|
|
if (config->relocatable && (isec->flags & SHF_LINK_ORDER)) {
|
|
|
|
// Merging two SHF_LINK_ORDER sections with different sh_link fields will
|
|
|
|
// change their semantics, so we only merge them in -r links if they will
|
|
|
|
// end up being linked to the same output section. The casts are fine
|
|
|
|
// because everything in the map was created by the orphan placement code.
|
|
|
|
auto *firstIsec = cast<InputSectionBase>(
|
2021-11-26 08:47:07 +08:00
|
|
|
cast<InputSectionDescription>(sec->commands[0])->sectionBases[0]);
|
2020-10-28 00:30:10 +08:00
|
|
|
OutputSection *firstIsecOut =
|
|
|
|
firstIsec->flags & SHF_LINK_ORDER
|
|
|
|
? firstIsec->getLinkOrderDep()->getOutputSection()
|
|
|
|
: nullptr;
|
|
|
|
if (firstIsecOut != isec->getLinkOrderDep()->getOutputSection())
|
2019-10-01 04:23:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
sec->recordSection(isec);
|
2017-11-05 07:54:25 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
OutputSection *sec = createSection(isec, outsecName);
|
|
|
|
v.push_back(sec);
|
2017-11-05 07:54:25 +08:00
|
|
|
return sec;
|
|
|
|
}
|
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// Add sections that didn't match any sections command.
|
2017-11-05 07:54:25 +08:00
|
|
|
void LinkerScript::addOrphanSections() {
|
2019-05-29 11:55:20 +08:00
|
|
|
StringMap<TinyPtrVector<OutputSection *>> map;
|
2021-12-27 05:53:47 +08:00
|
|
|
SmallVector<OutputSection *, 0> v;
|
2018-03-23 17:18:31 +08:00
|
|
|
|
2019-10-01 04:23:00 +08:00
|
|
|
std::function<void(InputSectionBase *)> add;
|
|
|
|
add = [&](InputSectionBase *s) {
|
|
|
|
if (s->isLive() && !s->parent) {
|
2020-02-26 07:02:04 +08:00
|
|
|
orphanSections.push_back(s);
|
2019-10-01 04:23:00 +08:00
|
|
|
|
2020-02-26 07:02:04 +08:00
|
|
|
StringRef name = getOutputSectionName(s);
|
[LLD] Add support for --unique option
Summary:
Places orphan sections into a unique output section. This prevents the merging of orphan sections of the same name.
Matches behaviour of GNU ld --unique. --unique=pattern is not implemented.
Motivated user case shown in the test has 2 local symbols as they would appear if C++ source has been compiled with -ffunction-sections. The merging of these sections in the case of a partial link (-r) may limit the effectiveness of -gc-sections of a subsequent link.
Reviewers: espindola, jhenderson, bd1976llvm, edd, andrewng, JonChesterfield, MaskRay, grimar, ruiu, psmith
Reviewed By: MaskRay, grimar
Subscribers: emaste, arichardson, MaskRay, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D75536
2020-03-09 23:43:20 +08:00
|
|
|
if (config->unique) {
|
|
|
|
v.push_back(createSection(s, name));
|
|
|
|
} else if (OutputSection *sec = findByName(sectionCommands, name)) {
|
2019-10-01 04:23:00 +08:00
|
|
|
sec->recordSection(s);
|
|
|
|
} else {
|
|
|
|
if (OutputSection *os = addInputSec(map, s, name))
|
|
|
|
v.push_back(os);
|
|
|
|
assert(isa<MergeInputSection>(s) ||
|
|
|
|
s->getOutputSection()->sectionIndex == UINT32_MAX);
|
|
|
|
}
|
2017-04-29 23:44:03 +08:00
|
|
|
}
|
2017-10-07 07:34:43 +08:00
|
|
|
|
2019-10-01 04:23:00 +08:00
|
|
|
if (config->relocatable)
|
|
|
|
for (InputSectionBase *depSec : s->dependentSections)
|
|
|
|
if (depSec->flags & SHF_LINK_ORDER)
|
|
|
|
add(depSec);
|
2018-03-23 17:18:31 +08:00
|
|
|
};
|
|
|
|
|
2021-04-15 02:52:41 +08:00
|
|
|
// For further --emit-reloc handling code we need target output section
|
2018-03-23 17:18:31 +08:00
|
|
|
// to be created before we create relocation output section, so we want
|
|
|
|
// to create target sections first. We do not want priority handling
|
|
|
|
// for synthetic sections because them are special.
|
|
|
|
for (InputSectionBase *isec : inputSections) {
|
2019-10-01 04:23:00 +08:00
|
|
|
// In -r links, SHF_LINK_ORDER sections are added while adding their parent
|
|
|
|
// sections because we need to know the parent's output section before we
|
|
|
|
// can select an output section for the SHF_LINK_ORDER section.
|
|
|
|
if (config->relocatable && (isec->flags & SHF_LINK_ORDER))
|
|
|
|
continue;
|
|
|
|
|
2018-03-27 02:49:31 +08:00
|
|
|
if (auto *sec = dyn_cast<InputSection>(isec))
|
|
|
|
if (InputSectionBase *rel = sec->getRelocatedSection())
|
2018-03-23 17:18:31 +08:00
|
|
|
if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
|
|
|
|
add(relIS);
|
|
|
|
add(isec);
|
2017-04-27 06:30:15 +08:00
|
|
|
}
|
2017-10-31 18:31:58 +08:00
|
|
|
|
|
|
|
// If no SECTIONS command was given, we should insert sections commands
|
|
|
|
// before others, so that we can handle scripts which refers them,
|
|
|
|
// for example: "foo = ABSOLUTE(ADDR(.text)));".
|
|
|
|
// When SECTIONS command is present we just add all orphans to the end.
|
|
|
|
if (hasSectionsCommand)
|
|
|
|
sectionCommands.insert(sectionCommands.end(), v.begin(), v.end());
|
|
|
|
else
|
|
|
|
sectionCommands.insert(sectionCommands.begin(), v.begin(), v.end());
|
2016-07-20 22:43:20 +08:00
|
|
|
}
|
|
|
|
|
2020-02-26 07:02:04 +08:00
|
|
|
void LinkerScript::diagnoseOrphanHandling() const {
|
2020-11-03 22:41:09 +08:00
|
|
|
llvm::TimeTraceScope timeScope("Diagnose orphan sections");
|
2020-11-30 04:36:26 +08:00
|
|
|
if (config->orphanHandling == OrphanHandlingPolicy::Place)
|
|
|
|
return;
|
2020-02-26 07:02:04 +08:00
|
|
|
for (const InputSectionBase *sec : orphanSections) {
|
2020-02-26 08:39:31 +08:00
|
|
|
// Input SHT_REL[A] retained by --emit-relocs are ignored by
|
|
|
|
// computeInputSections(). Don't warn/error.
|
|
|
|
if (isa<InputSection>(sec) &&
|
|
|
|
cast<InputSection>(sec)->getRelocatedSection())
|
|
|
|
continue;
|
|
|
|
|
2020-02-26 07:02:04 +08:00
|
|
|
StringRef name = getOutputSectionName(sec);
|
|
|
|
if (config->orphanHandling == OrphanHandlingPolicy::Error)
|
|
|
|
error(toString(sec) + " is being placed in '" + name + "'");
|
2020-11-30 04:36:26 +08:00
|
|
|
else
|
2020-02-26 07:02:04 +08:00
|
|
|
warn(toString(sec) + " is being placed in '" + name + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 10:34:00 +08:00
|
|
|
// This function searches for a memory region to place the given output
|
|
|
|
// section in. If found, a pointer to the appropriate memory region is
|
2021-11-11 16:07:38 +08:00
|
|
|
// returned in the first member of the pair. Otherwise, a nullptr is returned.
|
|
|
|
// The second member of the pair is a hint that should be passed to the
|
|
|
|
// subsequent call of this method.
|
|
|
|
std::pair<MemoryRegion *, MemoryRegion *>
|
|
|
|
LinkerScript::findMemoryRegion(OutputSection *sec, MemoryRegion *hint) {
|
2021-11-15 16:59:39 +08:00
|
|
|
// Non-allocatable sections are not part of the process image.
|
|
|
|
if (!(sec->flags & SHF_ALLOC)) {
|
|
|
|
if (!sec->memoryRegionName.empty())
|
|
|
|
warn("ignoring memory region assignment for non-allocatable section '" +
|
|
|
|
sec->name + "'");
|
|
|
|
return {nullptr, nullptr};
|
|
|
|
}
|
|
|
|
|
2017-01-24 10:34:00 +08:00
|
|
|
// If a memory region name was specified in the output section command,
|
|
|
|
// then try to find that region first.
|
2017-07-28 03:22:43 +08:00
|
|
|
if (!sec->memoryRegionName.empty()) {
|
2018-01-25 09:29:15 +08:00
|
|
|
if (MemoryRegion *m = memoryRegions.lookup(sec->memoryRegionName))
|
2021-11-11 16:07:38 +08:00
|
|
|
return {m, m};
|
2017-07-28 03:22:43 +08:00
|
|
|
error("memory region '" + sec->memoryRegionName + "' not declared");
|
2021-11-11 16:07:38 +08:00
|
|
|
return {nullptr, nullptr};
|
2017-01-24 10:34:00 +08:00
|
|
|
}
|
|
|
|
|
2017-04-05 11:19:43 +08:00
|
|
|
// If at least one memory region is defined, all sections must
|
|
|
|
// belong to some memory region. Otherwise, we don't need to do
|
|
|
|
// anything for memory regions.
|
2017-10-11 09:19:33 +08:00
|
|
|
if (memoryRegions.empty())
|
2021-11-11 16:07:38 +08:00
|
|
|
return {nullptr, nullptr};
|
|
|
|
|
2021-11-15 16:59:39 +08:00
|
|
|
// An orphan section should continue the previous memory region.
|
|
|
|
if (sec->sectionIndex == UINT32_MAX && hint)
|
2021-11-11 16:07:38 +08:00
|
|
|
return {hint, hint};
|
2017-01-24 10:34:00 +08:00
|
|
|
|
|
|
|
// See if a region can be found by matching section flags.
|
2017-10-11 09:19:33 +08:00
|
|
|
for (auto &pair : memoryRegions) {
|
2017-09-08 16:23:15 +08:00
|
|
|
MemoryRegion *m = pair.second;
|
2021-11-24 13:17:03 +08:00
|
|
|
if (m->compatibleWith(sec->flags))
|
2021-11-11 16:07:38 +08:00
|
|
|
return {m, nullptr};
|
2017-01-24 10:34:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, no suitable region was found.
|
2021-11-15 16:59:39 +08:00
|
|
|
error("no memory region specified for section '" + sec->name + "'");
|
2021-11-11 16:07:38 +08:00
|
|
|
return {nullptr, nullptr};
|
2017-01-24 10:34:00 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 18:45:46 +08:00
|
|
|
static OutputSection *findFirstSection(PhdrEntry *load) {
|
|
|
|
for (OutputSection *sec : outputSections)
|
|
|
|
if (sec->ptLoad == load)
|
|
|
|
return sec;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// This function assigns offsets to input sections and an output section
|
|
|
|
// for a single sections command (e.g. ".text { *(.text); }").
|
2017-07-28 03:22:43 +08:00
|
|
|
void LinkerScript::assignOffsets(OutputSection *sec) {
|
2021-08-04 23:58:50 +08:00
|
|
|
const bool isTbss = (sec->flags & SHF_TLS) && sec->type == SHT_NOBITS;
|
2020-06-20 00:07:48 +08:00
|
|
|
const bool sameMemRegion = ctx->memRegion == sec->memRegion;
|
|
|
|
const bool prevLMARegionIsDefault = ctx->lmaRegion == nullptr;
|
2020-08-14 00:00:26 +08:00
|
|
|
const uint64_t savedDot = dot;
|
2017-10-11 10:45:54 +08:00
|
|
|
ctx->memRegion = sec->memRegion;
|
2018-01-26 01:42:03 +08:00
|
|
|
ctx->lmaRegion = sec->lmaRegion;
|
2019-04-18 10:32:12 +08:00
|
|
|
|
2021-08-04 23:58:50 +08:00
|
|
|
if (!(sec->flags & SHF_ALLOC)) {
|
|
|
|
// Non-SHF_ALLOC sections have zero addresses.
|
|
|
|
dot = 0;
|
|
|
|
} else if (isTbss) {
|
|
|
|
// Allow consecutive SHF_TLS SHT_NOBITS output sections. The address range
|
|
|
|
// starts from the end address of the previous tbss section.
|
|
|
|
if (ctx->tbssAddr == 0)
|
|
|
|
ctx->tbssAddr = dot;
|
|
|
|
else
|
|
|
|
dot = ctx->tbssAddr;
|
|
|
|
} else {
|
2020-08-14 00:00:26 +08:00
|
|
|
if (ctx->memRegion)
|
|
|
|
dot = ctx->memRegion->curPos;
|
|
|
|
if (sec->addrExpr)
|
|
|
|
setDot(sec->addrExpr, sec->location, false);
|
|
|
|
|
|
|
|
// If the address of the section has been moved forward by an explicit
|
|
|
|
// expression so that it now starts past the current curPos of the enclosing
|
|
|
|
// region, we need to expand the current region to account for the space
|
|
|
|
// between the previous section, if any, and the start of this section.
|
|
|
|
if (ctx->memRegion && ctx->memRegion->curPos < dot)
|
|
|
|
expandMemoryRegion(ctx->memRegion, dot - ctx->memRegion->curPos,
|
2021-11-22 15:19:07 +08:00
|
|
|
sec->name);
|
2020-08-14 00:00:26 +08:00
|
|
|
}
|
2019-08-09 09:25:49 +08:00
|
|
|
|
2021-11-29 11:05:15 +08:00
|
|
|
ctx->outSec = sec;
|
|
|
|
if (sec->addrExpr && script->hasSectionsCommand) {
|
|
|
|
// The alignment is ignored.
|
2021-11-29 11:08:27 +08:00
|
|
|
sec->addr = dot;
|
2021-11-29 11:05:15 +08:00
|
|
|
} else {
|
2021-11-29 11:08:27 +08:00
|
|
|
// sec->alignment is the max of ALIGN and the maximum of input
|
2021-11-29 11:05:15 +08:00
|
|
|
// section alignments.
|
|
|
|
const uint64_t pos = dot;
|
2021-11-29 11:08:27 +08:00
|
|
|
dot = alignTo(dot, sec->alignment);
|
|
|
|
sec->addr = dot;
|
2021-11-29 11:05:15 +08:00
|
|
|
expandMemoryRegions(dot - pos);
|
|
|
|
}
|
2018-01-13 07:26:25 +08:00
|
|
|
|
2020-03-29 02:01:37 +08:00
|
|
|
// ctx->lmaOffset is LMA minus VMA. If LMA is explicitly specified via AT() or
|
|
|
|
// AT>, recompute ctx->lmaOffset; otherwise, if both previous/current LMA
|
2020-06-20 00:07:48 +08:00
|
|
|
// region is the default, and the two sections are in the same memory region,
|
|
|
|
// reuse previous lmaOffset; otherwise, reset lmaOffset to 0. This emulates
|
|
|
|
// heuristics described in
|
2020-03-29 02:01:37 +08:00
|
|
|
// https://sourceware.org/binutils/docs/ld/Output-Section-LMA.html
|
2021-11-18 22:21:21 +08:00
|
|
|
if (sec->lmaExpr) {
|
2018-01-26 00:43:49 +08:00
|
|
|
ctx->lmaOffset = sec->lmaExpr().getValue() - dot;
|
2021-11-18 22:21:21 +08:00
|
|
|
} else if (MemoryRegion *mr = sec->lmaRegion) {
|
|
|
|
uint64_t lmaStart = alignTo(mr->curPos, sec->alignment);
|
|
|
|
if (mr->curPos < lmaStart)
|
2021-11-22 15:19:07 +08:00
|
|
|
expandMemoryRegion(mr, lmaStart - mr->curPos, sec->name);
|
2021-11-18 22:21:21 +08:00
|
|
|
ctx->lmaOffset = lmaStart - dot;
|
|
|
|
} else if (!sameMemRegion || !prevLMARegionIsDefault) {
|
2020-03-29 02:01:37 +08:00
|
|
|
ctx->lmaOffset = 0;
|
2021-11-18 22:21:21 +08:00
|
|
|
}
|
2018-01-12 17:07:35 +08:00
|
|
|
|
2020-03-29 02:01:37 +08:00
|
|
|
// Propagate ctx->lmaOffset to the first "non-header" section.
|
2021-11-29 11:08:27 +08:00
|
|
|
if (PhdrEntry *l = sec->ptLoad)
|
2018-08-02 18:59:28 +08:00
|
|
|
if (sec == findFirstSection(l))
|
2018-08-02 18:45:46 +08:00
|
|
|
l->lmaOffset = ctx->lmaOffset;
|
2016-11-21 10:11:05 +08:00
|
|
|
|
2018-04-09 21:01:50 +08:00
|
|
|
// We can call this method multiple times during the creation of
|
|
|
|
// thunks and want to start over calculation each time.
|
[ELF] Reset OutputSection size prior to processing linker script commands
The size of an OutputSection is calculated early, to aid handling of compressed
debug sections. However, subsequent to this point, unused synthetic sections are
removed. In the event that an OutputSection, from which such an InputSection is
removed, is still required (e.g. because it has a symbol assignment), and no longer
has any InputSections, dot assignments, or BYTE()-family directives, the size
member is never updated when processing the commands. If the removed InputSection
had a non-zero size (such as a .got.plt section), the section ends up with the
wrong size in the output.
The fix is to reset the OutputSection size prior to processing the linker script
commands relating to that OutputSection. This ensures that the size is correct even
in the above situation.
Additionally, to reduce the risk of developers misusing OutputSection Size and
InputSection OutSecOff, they are set to simply the number of InputSections in an
OutputSection, and the corresponding index respectively. We cannot completely
stop using them, due to SHF_LINK_ORDER sections requiring them.
Compressed debug sections also require the full size. This is now calculated in
maybeCompress for these kinds of sections.
Reviewers: ruiu, rafael
Differential Revision: https://reviews.llvm.org/D38361
llvm-svn: 320472
2017-12-12 19:51:13 +08:00
|
|
|
sec->size = 0;
|
|
|
|
|
2017-10-11 12:21:55 +08:00
|
|
|
// We visited SectionsCommands from processSectionCommands to
|
|
|
|
// layout sections. Now, we visit SectionsCommands again to fix
|
|
|
|
// section offsets.
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : sec->commands) {
|
2017-10-11 12:21:55 +08:00
|
|
|
// This handles the assignments to symbol or to the dot.
|
2021-11-26 12:24:23 +08:00
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
|
|
|
|
assign->addr = dot;
|
|
|
|
assignSymbol(assign, true);
|
|
|
|
assign->size = dot - assign->addr;
|
2017-10-11 12:21:55 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle BYTE(), SHORT(), LONG(), or QUAD().
|
2021-11-26 12:24:23 +08:00
|
|
|
if (auto *data = dyn_cast<ByteCommand>(cmd)) {
|
2021-11-29 11:08:27 +08:00
|
|
|
data->offset = dot - sec->addr;
|
2021-11-26 12:24:23 +08:00
|
|
|
dot += data->size;
|
|
|
|
expandOutputSection(data->size);
|
2017-10-11 12:21:55 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a single input section description command.
|
|
|
|
// It calculates and assigns the offsets for each section and also
|
|
|
|
// updates the output section size.
|
2021-11-29 08:58:06 +08:00
|
|
|
for (InputSection *isec : cast<InputSectionDescription>(cmd)->sections) {
|
2021-11-29 11:08:27 +08:00
|
|
|
assert(isec->getParent() == sec);
|
2021-11-29 08:58:06 +08:00
|
|
|
const uint64_t pos = dot;
|
|
|
|
dot = alignTo(dot, isec->alignment);
|
2021-11-29 11:08:27 +08:00
|
|
|
isec->outSecOff = dot - sec->addr;
|
2021-11-29 08:58:06 +08:00
|
|
|
dot += isec->getSize();
|
|
|
|
|
|
|
|
// Update output section size after adding each section. This is so that
|
|
|
|
// SIZEOF works correctly in the case below:
|
|
|
|
// .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
|
|
|
|
expandOutputSection(dot - pos);
|
|
|
|
}
|
2017-10-11 12:21:55 +08:00
|
|
|
}
|
2020-08-14 00:00:26 +08:00
|
|
|
|
|
|
|
// Non-SHF_ALLOC sections do not affect the addresses of other OutputSections
|
|
|
|
// as they are not part of the process image.
|
2021-08-04 23:58:50 +08:00
|
|
|
if (!(sec->flags & SHF_ALLOC)) {
|
2020-08-14 00:00:26 +08:00
|
|
|
dot = savedDot;
|
2021-08-04 23:58:50 +08:00
|
|
|
} else if (isTbss) {
|
|
|
|
// NOBITS TLS sections are similar. Additionally save the end address.
|
|
|
|
ctx->tbssAddr = dot;
|
|
|
|
dot = savedDot;
|
|
|
|
}
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
|
|
|
|
2021-11-18 01:15:20 +08:00
|
|
|
static bool isDiscardable(const OutputSection &sec) {
|
2019-06-03 13:34:25 +08:00
|
|
|
if (sec.name == "/DISCARD/")
|
|
|
|
return true;
|
|
|
|
|
2019-04-26 14:59:30 +08:00
|
|
|
// We do not want to remove OutputSections with expressions that reference
|
|
|
|
// symbols even if the OutputSection is empty. We want to ensure that the
|
|
|
|
// expressions can be evaluated and report an error if they cannot.
|
2018-03-01 20:27:04 +08:00
|
|
|
if (sec.expressionsUseSymbols)
|
2018-03-01 09:08:00 +08:00
|
|
|
return false;
|
|
|
|
|
2019-04-26 14:59:30 +08:00
|
|
|
// OutputSections may be referenced by name in ADDR and LOADADDR expressions,
|
|
|
|
// as an empty Section can has a valid VMA and LMA we keep the OutputSection
|
|
|
|
// to maintain the integrity of the other Expression.
|
|
|
|
if (sec.usedInExpression)
|
|
|
|
return false;
|
|
|
|
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : sec.commands) {
|
|
|
|
if (auto assign = dyn_cast<SymbolAssignment>(cmd))
|
2018-07-03 17:23:25 +08:00
|
|
|
// Don't create empty output sections just for unreferenced PROVIDE
|
|
|
|
// symbols.
|
2021-11-26 12:24:23 +08:00
|
|
|
if (assign->name != "." && !assign->sym)
|
2018-07-03 17:23:25 +08:00
|
|
|
continue;
|
|
|
|
|
2021-11-26 12:24:23 +08:00
|
|
|
if (!isa<InputSectionDescription>(*cmd))
|
2018-03-01 09:08:00 +08:00
|
|
|
return false;
|
2018-07-03 17:23:25 +08:00
|
|
|
}
|
2018-03-13 16:32:56 +08:00
|
|
|
return true;
|
2018-03-01 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
2021-11-18 01:15:20 +08:00
|
|
|
bool LinkerScript::isDiscarded(const OutputSection *sec) const {
|
|
|
|
return hasSectionsCommand && (getFirstInputSection(sec) == nullptr) &&
|
|
|
|
isDiscardable(*sec);
|
|
|
|
}
|
|
|
|
|
2020-11-28 02:42:35 +08:00
|
|
|
static void maybePropagatePhdrs(OutputSection &sec,
|
2021-12-27 05:53:47 +08:00
|
|
|
SmallVector<StringRef, 0> &phdrs) {
|
2020-11-28 02:42:35 +08:00
|
|
|
if (sec.phdrs.empty()) {
|
|
|
|
// To match the bfd linker script behaviour, only propagate program
|
|
|
|
// headers to sections that are allocated.
|
|
|
|
if (sec.flags & SHF_ALLOC)
|
|
|
|
sec.phdrs = phdrs;
|
|
|
|
} else {
|
|
|
|
phdrs = sec.phdrs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::adjustSectionsBeforeSorting() {
|
2016-09-22 22:40:50 +08:00
|
|
|
// If the output section contains only symbol assignments, create a
|
2017-10-11 16:13:40 +08:00
|
|
|
// corresponding output section. The issue is what to do with linker script
|
|
|
|
// like ".foo : { symbol = 42; }". One option would be to convert it to
|
|
|
|
// "symbol = 42;". That is, move the symbol out of the empty section
|
|
|
|
// description. That seems to be what bfd does for this simple case. The
|
|
|
|
// problem is that this is not completely general. bfd will give up and
|
|
|
|
// create a dummy section too if there is a ". = . + 1" inside the section
|
|
|
|
// for example.
|
|
|
|
// Given that we want to create the section, we have to worry what impact
|
|
|
|
// it will have on the link. For example, if we just create a section with
|
|
|
|
// 0 for flags, it would change which PT_LOADs are created.
|
2018-02-24 05:57:49 +08:00
|
|
|
// We could remember that particular section is dummy and ignore it in
|
2017-10-11 16:13:40 +08:00
|
|
|
// other parts of the linker, but unfortunately there are quite a few places
|
|
|
|
// that would need to change:
|
|
|
|
// * The program header creation.
|
|
|
|
// * The orphan section placement.
|
|
|
|
// * The address assignment.
|
|
|
|
// The other option is to pick flags that minimize the impact the section
|
|
|
|
// will have on the rest of the linker. That is why we copy the flags from
|
|
|
|
// the previous sections. Only a few flags are needed to keep the impact low.
|
2017-03-14 18:00:19 +08:00
|
|
|
uint64_t flags = SHF_ALLOC;
|
2017-05-06 05:34:26 +08:00
|
|
|
|
2021-12-27 05:53:47 +08:00
|
|
|
SmallVector<StringRef, 0> defPhdrs;
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *&cmd : sectionCommands) {
|
2017-09-18 16:43:44 +08:00
|
|
|
auto *sec = dyn_cast<OutputSection>(cmd);
|
2017-07-28 03:22:43 +08:00
|
|
|
if (!sec)
|
2016-09-22 22:40:50 +08:00
|
|
|
continue;
|
|
|
|
|
2018-05-18 04:22:39 +08:00
|
|
|
// Handle align (e.g. ".foo : ALIGN(16) { ... }").
|
|
|
|
if (sec->alignExpr)
|
|
|
|
sec->alignment =
|
|
|
|
std::max<uint32_t>(sec->alignment, sec->alignExpr().getValue());
|
|
|
|
|
2019-06-04 04:14:25 +08:00
|
|
|
// The input section might have been removed (if it was an empty synthetic
|
|
|
|
// section), but we at least know the flags.
|
|
|
|
if (sec->hasInputSections)
|
2018-03-13 16:32:56 +08:00
|
|
|
flags = sec->flags;
|
2018-03-01 09:08:00 +08:00
|
|
|
|
2018-03-13 16:32:56 +08:00
|
|
|
// We do not want to keep any special flags for output section
|
|
|
|
// in case it is empty.
|
2020-01-16 21:23:08 +08:00
|
|
|
bool isEmpty = (getFirstInputSection(sec) == nullptr);
|
2018-03-13 16:32:56 +08:00
|
|
|
if (isEmpty)
|
2019-04-24 13:33:33 +08:00
|
|
|
sec->flags = flags & ((sec->nonAlloc ? 0 : (uint64_t)SHF_ALLOC) |
|
|
|
|
SHF_WRITE | SHF_EXECINSTR);
|
2018-03-13 16:32:56 +08:00
|
|
|
|
2020-11-28 02:42:35 +08:00
|
|
|
// The code below may remove empty output sections. We should save the
|
|
|
|
// specified program headers (if exist) and propagate them to subsequent
|
|
|
|
// sections which do not specify program headers.
|
|
|
|
// An example of such a linker script is:
|
|
|
|
// SECTIONS { .empty : { *(.empty) } :rw
|
|
|
|
// .foo : { *(.foo) } }
|
|
|
|
// Note: at this point the order of output sections has not been finalized,
|
|
|
|
// because orphans have not been inserted into their expected positions. We
|
|
|
|
// will handle them in adjustSectionsAfterSorting().
|
|
|
|
if (sec->sectionIndex != UINT32_MAX)
|
|
|
|
maybePropagatePhdrs(*sec, defPhdrs);
|
|
|
|
|
2018-03-13 16:32:56 +08:00
|
|
|
if (isEmpty && isDiscardable(*sec)) {
|
2019-05-29 11:55:20 +08:00
|
|
|
sec->markDead();
|
2018-02-23 18:53:04 +08:00
|
|
|
cmd = nullptr;
|
2018-03-01 09:08:00 +08:00
|
|
|
}
|
2016-09-22 22:40:50 +08:00
|
|
|
}
|
2018-02-23 18:53:04 +08:00
|
|
|
|
|
|
|
// It is common practice to use very generic linker scripts. So for any
|
|
|
|
// given run some of the output sections in the script will be empty.
|
|
|
|
// We could create corresponding empty output sections, but that would
|
|
|
|
// clutter the output.
|
|
|
|
// We instead remove trivially empty sections. The bfd linker seems even
|
|
|
|
// more aggressive at removing them.
|
2021-11-26 12:24:23 +08:00
|
|
|
llvm::erase_if(sectionCommands, [&](SectionCommand *cmd) { return !cmd; });
|
2016-11-14 23:39:38 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::adjustSectionsAfterSorting() {
|
2017-04-07 05:31:24 +08:00
|
|
|
// Try and find an appropriate memory region to assign offsets in.
|
2021-11-11 16:07:38 +08:00
|
|
|
MemoryRegion *hint = nullptr;
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : sectionCommands) {
|
|
|
|
if (auto *sec = dyn_cast<OutputSection>(cmd)) {
|
2018-01-25 09:36:36 +08:00
|
|
|
if (!sec->lmaRegionName.empty()) {
|
|
|
|
if (MemoryRegion *m = memoryRegions.lookup(sec->lmaRegionName))
|
|
|
|
sec->lmaRegion = m;
|
|
|
|
else
|
|
|
|
error("memory region '" + sec->lmaRegionName + "' not declared");
|
|
|
|
}
|
2021-11-11 16:07:38 +08:00
|
|
|
std::tie(sec->memRegion, hint) = findMemoryRegion(sec, hint);
|
2017-04-07 05:40:22 +08:00
|
|
|
}
|
|
|
|
}
|
2017-04-07 05:31:24 +08:00
|
|
|
|
2016-11-14 23:39:38 +08:00
|
|
|
// If output section command doesn't specify any segments,
|
|
|
|
// and we haven't previously assigned any section to segment,
|
|
|
|
// then we simply assign section to the very first load segment.
|
|
|
|
// Below is an example of such linker script:
|
|
|
|
// PHDRS { seg PT_LOAD; }
|
|
|
|
// SECTIONS { .aaa : { *(.aaa) } }
|
2021-12-27 05:53:47 +08:00
|
|
|
SmallVector<StringRef, 0> defPhdrs;
|
2018-03-01 02:05:37 +08:00
|
|
|
auto firstPtLoad = llvm::find_if(phdrsCommands, [](const PhdrsCommand &cmd) {
|
|
|
|
return cmd.type == PT_LOAD;
|
|
|
|
});
|
2017-10-11 09:19:33 +08:00
|
|
|
if (firstPtLoad != phdrsCommands.end())
|
2016-11-14 23:39:38 +08:00
|
|
|
defPhdrs.push_back(firstPtLoad->name);
|
|
|
|
|
|
|
|
// Walk the commands and propagate the program headers to commands that don't
|
|
|
|
// explicitly specify them.
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : sectionCommands)
|
|
|
|
if (auto *sec = dyn_cast<OutputSection>(cmd))
|
2020-11-28 02:42:35 +08:00
|
|
|
maybePropagatePhdrs(*sec, defPhdrs);
|
2016-09-22 22:40:50 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 23:25:46 +08:00
|
|
|
static uint64_t computeBase(uint64_t min, bool allocateHeaders) {
|
|
|
|
// If there is no SECTIONS or if the linkerscript is explicit about program
|
|
|
|
// headers, do our best to allocate them.
|
|
|
|
if (!script->hasSectionsCommand || allocateHeaders)
|
|
|
|
return 0;
|
|
|
|
// Otherwise only allocate program headers if that would not add a page.
|
|
|
|
return alignDown(min, config->maxPageSize);
|
|
|
|
}
|
|
|
|
|
[ELF] Map the ELF header at imageBase
If there is no readonly section, we map:
* The ELF header at imageBase+maxPageSize
* Program headers at imageBase+maxPageSize+sizeof(Ehdr)
* The first section .text at imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)
Due to the interaction between Writer<ELFT>::fixSectionAlignments and
LinkerScript::allocateHeaders,
`alignDown(p_vaddr(R PT_LOAD)) = alignDown(p_vaddr(RX PT_LOAD))`.
The RX PT_LOAD will override the R PT_LOAD at runtime, which is not ideal:
```
// PHDR at 0x401034, should be 0x400034
PHDR 0x000034 0x00401034 0x00401034 0x000a0 0x000a0 R 0x4
// R PT_LOAD contains just Ehdr and program headers.
// At 0x401000, should be 0x400000
LOAD 0x000000 0x00401000 0x00401000 0x000d4 0x000d4 R 0x1000
LOAD 0x0000d4 0x004010d4 0x004010d4 0x00001 0x00001 R E 0x1000
```
* createPhdrs allocates the headers to the R PT_LOAD.
* fixSectionAlignments assigns `imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)` (formula: `alignTo(dot, maxPageSize) + dot % config->maxPageSize`) to addrExpr of .text
* allocateHeaders computes the minimum address among SHF_ALLOC sections, i.e. addr(.text)
* allocateHeaders sets address of ELF header to `addr(.text)-sizeof(Ehdr)-sizeof(program headers) = imageBase+maxPageSize`
The main observation is that when the SECTIONS command is not used, we
don't have to call allocateHeaders. This requires an assumption that
the presence of PT_PHDR and addresses of headers can be decided
regardless of address information.
This may seem natural because dot is not manipulated by a linker script.
The other thing is that we have to drop the special rule for -T<section>
in `getInitialDot`. If -Ttext is smaller than the image base, the headers
will not be allocated with the old behavior (allocateHeaders is called)
but always allocated with the new behavior.
The behavior change is not a problem. Whether and where headers are
allocated can vary among linkers, or ld.bfd across different versions
(--enable-separate-code or not). It is thus advised to use a linker
script with the PHDRS command to have a consistent behavior across
linkers. If PT_PHDR is needed, an explicit --image-base can be a simpler
alternative.
Differential Revision: https://reviews.llvm.org/D67325
llvm-svn: 371957
2019-09-16 15:04:16 +08:00
|
|
|
// When the SECTIONS command is used, try to find an address for the file and
|
|
|
|
// program headers output sections, which can be added to the first PT_LOAD
|
|
|
|
// segment when program headers are created.
|
2017-08-24 02:44:34 +08:00
|
|
|
//
|
[ELF] Map the ELF header at imageBase
If there is no readonly section, we map:
* The ELF header at imageBase+maxPageSize
* Program headers at imageBase+maxPageSize+sizeof(Ehdr)
* The first section .text at imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)
Due to the interaction between Writer<ELFT>::fixSectionAlignments and
LinkerScript::allocateHeaders,
`alignDown(p_vaddr(R PT_LOAD)) = alignDown(p_vaddr(RX PT_LOAD))`.
The RX PT_LOAD will override the R PT_LOAD at runtime, which is not ideal:
```
// PHDR at 0x401034, should be 0x400034
PHDR 0x000034 0x00401034 0x00401034 0x000a0 0x000a0 R 0x4
// R PT_LOAD contains just Ehdr and program headers.
// At 0x401000, should be 0x400000
LOAD 0x000000 0x00401000 0x00401000 0x000d4 0x000d4 R 0x1000
LOAD 0x0000d4 0x004010d4 0x004010d4 0x00001 0x00001 R E 0x1000
```
* createPhdrs allocates the headers to the R PT_LOAD.
* fixSectionAlignments assigns `imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)` (formula: `alignTo(dot, maxPageSize) + dot % config->maxPageSize`) to addrExpr of .text
* allocateHeaders computes the minimum address among SHF_ALLOC sections, i.e. addr(.text)
* allocateHeaders sets address of ELF header to `addr(.text)-sizeof(Ehdr)-sizeof(program headers) = imageBase+maxPageSize`
The main observation is that when the SECTIONS command is not used, we
don't have to call allocateHeaders. This requires an assumption that
the presence of PT_PHDR and addresses of headers can be decided
regardless of address information.
This may seem natural because dot is not manipulated by a linker script.
The other thing is that we have to drop the special rule for -T<section>
in `getInitialDot`. If -Ttext is smaller than the image base, the headers
will not be allocated with the old behavior (allocateHeaders is called)
but always allocated with the new behavior.
The behavior change is not a problem. Whether and where headers are
allocated can vary among linkers, or ld.bfd across different versions
(--enable-separate-code or not). It is thus advised to use a linker
script with the PHDRS command to have a consistent behavior across
linkers. If PT_PHDR is needed, an explicit --image-base can be a simpler
alternative.
Differential Revision: https://reviews.llvm.org/D67325
llvm-svn: 371957
2019-09-16 15:04:16 +08:00
|
|
|
// We check if the headers fit below the first allocated section. If there isn't
|
|
|
|
// enough space for these sections, we'll remove them from the PT_LOAD segment,
|
|
|
|
// and we'll also remove the PT_PHDR segment.
|
2021-12-27 05:53:47 +08:00
|
|
|
void LinkerScript::allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs) {
|
2017-07-05 17:12:54 +08:00
|
|
|
uint64_t min = std::numeric_limits<uint64_t>::max();
|
2017-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *sec : outputSections)
|
2017-07-05 17:12:54 +08:00
|
|
|
if (sec->flags & SHF_ALLOC)
|
|
|
|
min = std::min<uint64_t>(min, sec->addr);
|
[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-07-27 15:46:50 +08:00
|
|
|
auto it = llvm::find_if(
|
|
|
|
phdrs, [](const PhdrEntry *e) { return e->p_type == PT_LOAD; });
|
|
|
|
if (it == phdrs.end())
|
2017-07-04 00:05:51 +08:00
|
|
|
return;
|
2017-07-27 15:46:50 +08:00
|
|
|
PhdrEntry *firstPTLoad = *it;
|
2017-05-05 03:34:17 +08:00
|
|
|
|
2018-03-01 23:25:46 +08:00
|
|
|
bool hasExplicitHeaders =
|
|
|
|
llvm::any_of(phdrsCommands, [](const PhdrsCommand &cmd) {
|
|
|
|
return cmd.hasPhdrs || cmd.hasFilehdr;
|
|
|
|
});
|
2019-05-14 00:01:26 +08:00
|
|
|
bool paged = !config->omagic && !config->nmagic;
|
2017-05-05 03:34:17 +08:00
|
|
|
uint64_t headerSize = getHeaderSize();
|
2019-05-14 00:01:26 +08:00
|
|
|
if ((paged || hasExplicitHeaders) &&
|
|
|
|
headerSize <= min - computeBase(min, hasExplicitHeaders)) {
|
2017-09-29 02:12:13 +08:00
|
|
|
min = alignDown(min - headerSize, config->maxPageSize);
|
2017-05-05 03:34:17 +08:00
|
|
|
Out::elfHeader->addr = min;
|
|
|
|
Out::programHeaders->addr = min + Out::elfHeader->size;
|
2017-07-04 00:05:51 +08:00
|
|
|
return;
|
2017-05-05 03:34:17 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 23:25:46 +08:00
|
|
|
// Error if we were explicitly asked to allocate headers.
|
|
|
|
if (hasExplicitHeaders)
|
|
|
|
error("could not allocate headers");
|
|
|
|
|
2017-09-07 18:53:07 +08:00
|
|
|
Out::elfHeader->ptLoad = nullptr;
|
|
|
|
Out::programHeaders->ptLoad = nullptr;
|
2017-09-07 19:01:10 +08:00
|
|
|
firstPTLoad->firstSec = findFirstSection(firstPTLoad);
|
2017-05-05 03:34:17 +08:00
|
|
|
|
2017-08-28 17:28:15 +08:00
|
|
|
llvm::erase_if(phdrs,
|
|
|
|
[](const PhdrEntry *e) { return e->p_type == PT_PHDR; });
|
2017-05-05 03:34:17 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 09:19:33 +08:00
|
|
|
LinkerScript::AddressState::AddressState() {
|
|
|
|
for (auto &mri : script->memoryRegions) {
|
2018-01-25 10:18:00 +08:00
|
|
|
MemoryRegion *mr = mri.second;
|
2020-03-07 03:49:58 +08:00
|
|
|
mr->curPos = (mr->origin)().getValue();
|
2017-07-07 17:11:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-31 18:31:58 +08:00
|
|
|
// Here we assign addresses as instructed by linker script SECTIONS
|
|
|
|
// sub-commands. Doing that allows us to use final VA values, so here
|
|
|
|
// we also handle rest commands like symbol assignments and ASSERTs.
|
2019-08-26 18:23:31 +08:00
|
|
|
// Returns a symbol that has changed its section or value, or nullptr if no
|
|
|
|
// symbol has changed.
|
|
|
|
const Defined *LinkerScript::assignAddresses() {
|
[ELF] Map the ELF header at imageBase
If there is no readonly section, we map:
* The ELF header at imageBase+maxPageSize
* Program headers at imageBase+maxPageSize+sizeof(Ehdr)
* The first section .text at imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)
Due to the interaction between Writer<ELFT>::fixSectionAlignments and
LinkerScript::allocateHeaders,
`alignDown(p_vaddr(R PT_LOAD)) = alignDown(p_vaddr(RX PT_LOAD))`.
The RX PT_LOAD will override the R PT_LOAD at runtime, which is not ideal:
```
// PHDR at 0x401034, should be 0x400034
PHDR 0x000034 0x00401034 0x00401034 0x000a0 0x000a0 R 0x4
// R PT_LOAD contains just Ehdr and program headers.
// At 0x401000, should be 0x400000
LOAD 0x000000 0x00401000 0x00401000 0x000d4 0x000d4 R 0x1000
LOAD 0x0000d4 0x004010d4 0x004010d4 0x00001 0x00001 R E 0x1000
```
* createPhdrs allocates the headers to the R PT_LOAD.
* fixSectionAlignments assigns `imageBase+maxPageSize+sizeof(Ehdr)+sizeof(program headers)` (formula: `alignTo(dot, maxPageSize) + dot % config->maxPageSize`) to addrExpr of .text
* allocateHeaders computes the minimum address among SHF_ALLOC sections, i.e. addr(.text)
* allocateHeaders sets address of ELF header to `addr(.text)-sizeof(Ehdr)-sizeof(program headers) = imageBase+maxPageSize`
The main observation is that when the SECTIONS command is not used, we
don't have to call allocateHeaders. This requires an assumption that
the presence of PT_PHDR and addresses of headers can be decided
regardless of address information.
This may seem natural because dot is not manipulated by a linker script.
The other thing is that we have to drop the special rule for -T<section>
in `getInitialDot`. If -Ttext is smaller than the image base, the headers
will not be allocated with the old behavior (allocateHeaders is called)
but always allocated with the new behavior.
The behavior change is not a problem. Whether and where headers are
allocated can vary among linkers, or ld.bfd across different versions
(--enable-separate-code or not). It is thus advised to use a linker
script with the PHDRS command to have a consistent behavior across
linkers. If PT_PHDR is needed, an explicit --image-base can be a simpler
alternative.
Differential Revision: https://reviews.llvm.org/D67325
llvm-svn: 371957
2019-09-16 15:04:16 +08:00
|
|
|
if (script->hasSectionsCommand) {
|
|
|
|
// With a linker script, assignment of addresses to headers is covered by
|
|
|
|
// allocateHeaders().
|
|
|
|
dot = config->imageBase.getValueOr(0);
|
|
|
|
} else {
|
|
|
|
// Assign addresses to headers right now.
|
|
|
|
dot = target->getImageBase();
|
|
|
|
Out::elfHeader->addr = dot;
|
|
|
|
Out::programHeaders->addr = dot + Out::elfHeader->size;
|
|
|
|
dot += getHeaderSize();
|
|
|
|
}
|
[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
|
|
|
|
2021-11-29 07:32:29 +08:00
|
|
|
AddressState state;
|
|
|
|
ctx = &state;
|
2017-03-17 21:05:04 +08:00
|
|
|
errorOnMissingSection = true;
|
2021-11-29 11:05:15 +08:00
|
|
|
ctx->outSec = aether;
|
[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-08-26 18:23:31 +08:00
|
|
|
SymbolAssignmentMap oldValues = getSymbolAssignmentValues(sectionCommands);
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : sectionCommands) {
|
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
|
|
|
|
assign->addr = dot;
|
|
|
|
assignSymbol(assign, false);
|
|
|
|
assign->size = dot - assign->addr;
|
2016-07-12 14:39:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-11-26 12:24:23 +08:00
|
|
|
assignOffsets(cast<OutputSection>(cmd));
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
2019-08-26 18:23:31 +08:00
|
|
|
|
2017-10-11 10:45:54 +08:00
|
|
|
ctx = nullptr;
|
2019-08-26 18:23:31 +08:00
|
|
|
return getChangedSymbolAssignment(oldValues);
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
|
|
|
|
2016-08-22 12:55:20 +08:00
|
|
|
// Creates program headers as instructed by PHDRS linker script command.
|
2021-12-27 05:53:47 +08:00
|
|
|
SmallVector<PhdrEntry *, 0> LinkerScript::createPhdrs() {
|
|
|
|
SmallVector<PhdrEntry *, 0> ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
|
2016-08-22 12:55:20 +08:00
|
|
|
// Process PHDRS and FILEHDR keywords because they are not
|
|
|
|
// real output sections and cannot be added in the following loop.
|
2017-10-11 09:19:33 +08:00
|
|
|
for (const PhdrsCommand &cmd : phdrsCommands) {
|
2021-12-25 12:57:40 +08:00
|
|
|
PhdrEntry *phdr = make<PhdrEntry>(cmd.type, cmd.flags.getValueOr(PF_R));
|
2016-07-19 17:25:43 +08:00
|
|
|
|
|
|
|
if (cmd.hasFilehdr)
|
2017-07-27 15:46:50 +08:00
|
|
|
phdr->add(Out::elfHeader);
|
2016-07-19 17:25:43 +08:00
|
|
|
if (cmd.hasPhdrs)
|
2017-07-27 15:46:50 +08:00
|
|
|
phdr->add(Out::programHeaders);
|
2016-09-09 17:46:16 +08:00
|
|
|
|
|
|
|
if (cmd.lmaExpr) {
|
2017-07-27 15:46:50 +08:00
|
|
|
phdr->p_paddr = cmd.lmaExpr().getValue();
|
|
|
|
phdr->hasLMA = true;
|
2016-09-09 17:46:16 +08:00
|
|
|
}
|
2017-07-27 15:46:50 +08:00
|
|
|
ret.push_back(phdr);
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2016-08-22 12:55:20 +08:00
|
|
|
// Add output sections to program headers.
|
2017-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *sec : outputSections) {
|
2016-10-19 23:04:49 +08:00
|
|
|
// Assign headers specified by linker script
|
2017-07-28 03:22:43 +08:00
|
|
|
for (size_t id : getPhdrIndices(sec)) {
|
2017-07-27 15:46:50 +08:00
|
|
|
ret[id]->add(sec);
|
2017-10-11 09:19:33 +08:00
|
|
|
if (!phdrsCommands[id].flags.hasValue())
|
2017-07-27 15:46:50 +08:00
|
|
|
ret[id]->p_flags |= sec->getPhdrFlags();
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-25 07:47:31 +08:00
|
|
|
return ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2017-10-08 11:52:15 +08:00
|
|
|
// Returns true if we should emit an .interp section.
|
|
|
|
//
|
|
|
|
// We usually do. But if PHDRS commands are given, and
|
|
|
|
// no PT_INTERP is there, there's no place to emit an
|
|
|
|
// .interp, so we don't do that in that case.
|
|
|
|
bool LinkerScript::needsInterpSection() {
|
2017-10-11 09:19:33 +08:00
|
|
|
if (phdrsCommands.empty())
|
2017-10-08 11:52:15 +08:00
|
|
|
return true;
|
2017-10-11 09:19:33 +08:00
|
|
|
for (PhdrsCommand &cmd : phdrsCommands)
|
2017-04-05 13:06:17 +08:00
|
|
|
if (cmd.type == PT_INTERP)
|
2017-10-08 11:52:15 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
2016-08-16 14:40:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 12:34:34 +08:00
|
|
|
ExprValue LinkerScript::getSymbolValue(StringRef name, const Twine &loc) {
|
|
|
|
if (name == ".") {
|
2017-10-11 10:45:54 +08:00
|
|
|
if (ctx)
|
|
|
|
return {ctx->outSec, false, dot - ctx->outSec->addr, loc};
|
2017-08-17 16:47:21 +08:00
|
|
|
error(loc + ": unable to get location counter value");
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-11 12:31:13 +08:00
|
|
|
|
2017-12-01 01:51:10 +08:00
|
|
|
if (Symbol *sym = symtab->find(name)) {
|
2020-08-21 07:05:27 +08:00
|
|
|
if (auto *ds = dyn_cast<Defined>(sym)) {
|
|
|
|
ExprValue v{ds->section, false, ds->value, loc};
|
|
|
|
// Retain the original st_type, so that the alias will get the same
|
|
|
|
// behavior in relocation processing. Any operation will reset st_type to
|
|
|
|
// STT_NOTYPE.
|
|
|
|
v.type = ds->type;
|
|
|
|
return v;
|
|
|
|
}
|
2018-04-27 03:21:07 +08:00
|
|
|
if (isa<SharedSymbol>(sym))
|
2018-04-27 01:58:58 +08:00
|
|
|
if (!errorOnMissingSection)
|
|
|
|
return {nullptr, false, 0, loc};
|
2017-12-01 01:51:10 +08:00
|
|
|
}
|
2017-10-11 12:31:13 +08:00
|
|
|
|
2017-10-11 12:34:34 +08:00
|
|
|
error(loc + ": symbol not found: " + name);
|
2016-09-08 16:19:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-08 10:54:42 +08:00
|
|
|
// Returns the index of the segment named Name.
|
|
|
|
static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> vec,
|
|
|
|
StringRef name) {
|
|
|
|
for (size_t i = 0; i < vec.size(); ++i)
|
|
|
|
if (vec[i].name == name)
|
|
|
|
return i;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2017-05-10 22:28:31 +08:00
|
|
|
// Returns indices of ELF headers containing specific section. Each index is a
|
|
|
|
// zero based number of ELF header listed within PHDRS {} script block.
|
2021-12-27 05:53:47 +08:00
|
|
|
SmallVector<size_t, 0> LinkerScript::getPhdrIndices(OutputSection *cmd) {
|
|
|
|
SmallVector<size_t, 0> ret;
|
[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-08 10:54:42 +08:00
|
|
|
for (StringRef s : cmd->phdrs) {
|
2017-10-11 09:19:33 +08:00
|
|
|
if (Optional<size_t> idx = getPhdrIndex(phdrsCommands, s))
|
2017-10-08 10:44:08 +08:00
|
|
|
ret.push_back(*idx);
|
2017-10-08 10:54:42 +08:00
|
|
|
else if (s != "NONE")
|
2020-03-26 22:14:50 +08:00
|
|
|
error(cmd->location + ": program header '" + s +
|
2017-10-08 10:54:42 +08:00
|
|
|
"' is not listed in PHDRS");
|
|
|
|
}
|
2017-07-06 06:30:04 +08:00
|
|
|
return ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|