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"
|
2017-10-14 02:22:55 +08:00
|
|
|
#include "lld/Common/Threads.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"
|
2015-10-13 08:09:21 +08:00
|
|
|
#include "llvm/Support/Path.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;
|
2015-10-01 01:23:26 +08:00
|
|
|
|
2019-10-07 16:31:18 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace elf {
|
|
|
|
LinkerScript *script;
|
2017-03-22 07:03:09 +08:00
|
|
|
|
2019-09-06 23:57:24 +08:00
|
|
|
static uint64_t getOutputSectionVA(SectionBase *sec) {
|
|
|
|
OutputSection *os = sec->getOutputSection();
|
|
|
|
assert(os && "input section has no output section assigned");
|
|
|
|
return os ? os->addr : 0;
|
2017-08-21 15:57:12 +08:00
|
|
|
}
|
|
|
|
|
2017-03-17 21:05:04 +08:00
|
|
|
uint64_t ExprValue::getValue() const {
|
2017-08-21 15:57:12 +08:00
|
|
|
if (sec)
|
2019-09-06 23:57:24 +08:00
|
|
|
return alignTo(sec->getOffset(val) + getOutputSectionVA(sec),
|
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 {
|
|
|
|
if (sec)
|
2019-09-06 23:57:24 +08:00
|
|
|
return sec->getOffset(0) + getOutputSectionVA(sec);
|
2017-03-17 22:55:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-07-28 03:22:43 +08:00
|
|
|
sec->location = location;
|
|
|
|
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,
|
|
|
|
StringRef regionName, StringRef secName) {
|
|
|
|
memRegion->curPos += size;
|
|
|
|
uint64_t newSize = memRegion->curPos - memRegion->origin;
|
|
|
|
if (newSize > memRegion->length)
|
|
|
|
error("section '" + secName + "' will not fit in region '" + regionName +
|
|
|
|
"': overflowed by " + Twine(newSize - memRegion->length) + " bytes");
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
expandMemoryRegion(ctx->memRegion, size, ctx->memRegion->name,
|
|
|
|
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)
|
2018-03-07 20:44:18 +08:00
|
|
|
expandMemoryRegion(ctx->lmaRegion, size, ctx->lmaRegion->name,
|
|
|
|
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();
|
|
|
|
|
2019-08-13 14:19:39 +08:00
|
|
|
Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE,
|
|
|
|
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.
|
|
|
|
static SymbolAssignmentMap
|
|
|
|
getSymbolAssignmentValues(const std::vector<BaseCommand *> §ionCommands) {
|
|
|
|
SymbolAssignmentMap ret;
|
|
|
|
for (BaseCommand *base : sectionCommands) {
|
|
|
|
if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
|
|
|
|
if (cmd->sym) // sym is nullptr for dot.
|
|
|
|
ret.try_emplace(cmd->sym,
|
|
|
|
std::make_pair(cmd->sym->section, cmd->sym->value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (BaseCommand *sub_base : cast<OutputSection>(base)->sectionCommands)
|
|
|
|
if (auto *cmd = dyn_cast<SymbolAssignment>(sub_base))
|
|
|
|
if (cmd->sym)
|
|
|
|
ret.try_emplace(cmd->sym,
|
|
|
|
std::make_pair(cmd->sym->section, cmd->sym->value));
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-08 22:54:38 +08:00
|
|
|
// This method is used to handle INSERT AFTER statement. Here we rebuild
|
|
|
|
// the list of script commands to mix sections inserted into.
|
|
|
|
void LinkerScript::processInsertCommands() {
|
|
|
|
std::vector<BaseCommand *> v;
|
2018-03-13 17:18:11 +08:00
|
|
|
auto insert = [&](std::vector<BaseCommand *> &from) {
|
|
|
|
v.insert(v.end(), from.begin(), from.end());
|
|
|
|
from.clear();
|
|
|
|
};
|
|
|
|
|
2018-03-08 22:54:38 +08:00
|
|
|
for (BaseCommand *base : sectionCommands) {
|
2018-03-13 17:18:11 +08:00
|
|
|
if (auto *os = dyn_cast<OutputSection>(base)) {
|
|
|
|
insert(insertBeforeCommands[os->name]);
|
|
|
|
v.push_back(base);
|
|
|
|
insert(insertAfterCommands[os->name]);
|
|
|
|
continue;
|
2018-03-08 22:54:38 +08:00
|
|
|
}
|
2018-03-13 17:18:11 +08:00
|
|
|
v.push_back(base);
|
2018-03-08 22:54:38 +08:00
|
|
|
}
|
2018-03-13 17:18:11 +08:00
|
|
|
|
|
|
|
for (auto &cmds : {insertBeforeCommands, insertAfterCommands})
|
|
|
|
for (const std::pair<StringRef, std::vector<BaseCommand *>> &p : cmds)
|
|
|
|
if (!p.second.empty())
|
|
|
|
error("unable to INSERT AFTER/BEFORE " + p.first +
|
|
|
|
": section not defined");
|
2018-03-08 22:54:38 +08:00
|
|
|
|
|
|
|
sectionCommands = std::move(v);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
for (BaseCommand *base : sectionCommands) {
|
2018-02-28 13:55:56 +08:00
|
|
|
if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
|
|
|
|
declareSymbol(cmd);
|
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.
|
2018-07-05 22:09:47 +08:00
|
|
|
auto *sec = cast<OutputSection>(base);
|
2018-02-28 13:55:56 +08:00
|
|
|
if (sec->constraint != ConstraintKind::NoConstraint)
|
|
|
|
continue;
|
|
|
|
for (BaseCommand *base2 : sec->sectionCommands)
|
|
|
|
if (auto *cmd = dyn_cast<SymbolAssignment>(base2))
|
|
|
|
declareSymbol(cmd);
|
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
|
|
|
}
|
2016-09-07 15:08:43 +08:00
|
|
|
}
|
2017-01-10 02:36:57 +08:00
|
|
|
|
2017-10-11 10:54:52 +08:00
|
|
|
static std::string getFilename(InputFile *file) {
|
2017-09-09 00:22:43 +08:00
|
|
|
if (!file)
|
2017-08-25 06:01:40 +08:00
|
|
|
return "";
|
2017-09-09 00:22:43 +08:00
|
|
|
if (file->archiveName.empty())
|
|
|
|
return file->getName();
|
|
|
|
return (file->archiveName + "(" + file->getName() + ")").str();
|
2016-11-21 10:10:12 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
bool LinkerScript::shouldKeep(InputSectionBase *s) {
|
2017-12-07 03:13:23 +08:00
|
|
|
if (keptSections.empty())
|
|
|
|
return false;
|
2017-10-11 10:54:52 +08:00
|
|
|
std::string filename = getFilename(s->file);
|
2017-10-11 09:26:22 +08:00
|
|
|
for (InputSectionDescription *id : keptSections)
|
2017-08-25 06:01:40 +08:00
|
|
|
if (id->filePat.match(filename))
|
2016-11-21 10:10:12 +08:00
|
|
|
for (SectionPattern &p : id->sectionPatterns)
|
|
|
|
if (p.sectionPat.match(s->name))
|
|
|
|
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,
|
2018-01-31 00:20:08 +08:00
|
|
|
const SectionPattern &pat) {
|
2017-10-11 12:50:30 +08:00
|
|
|
if (pat.sortOuter == SortSectionPolicy::None)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pat.sortInner == SortSectionPolicy::Default)
|
|
|
|
sortSections(vec, config->sortSection);
|
|
|
|
else
|
|
|
|
sortSections(vec, pat.sortInner);
|
|
|
|
sortSections(vec, pat.sortOuter);
|
|
|
|
}
|
|
|
|
|
2016-09-16 23:10:23 +08:00
|
|
|
// Compute and remember which sections the InputSectionDescription matches.
|
2019-09-24 19:48:31 +08:00
|
|
|
std::vector<InputSectionBase *>
|
2018-01-31 00:20:08 +08:00
|
|
|
LinkerScript::computeInputSections(const InputSectionDescription *cmd) {
|
2019-09-24 19:48:31 +08:00
|
|
|
std::vector<InputSectionBase *> ret;
|
2017-04-05 10:05:48 +08:00
|
|
|
|
|
|
|
// Collects all sections that satisfy constraints of Cmd.
|
|
|
|
for (const SectionPattern &pat : cmd->sectionPatterns) {
|
|
|
|
size_t sizeBefore = ret.size();
|
|
|
|
|
|
|
|
for (InputSectionBase *sec : inputSections) {
|
2019-09-24 19:48:46 +08:00
|
|
|
if (!sec->isLive() || sec->parent)
|
2017-05-30 13:17:58 +08:00
|
|
|
continue;
|
|
|
|
|
2017-02-16 22:36:09 +08:00
|
|
|
// For -emit-relocs we have to ignore entries like
|
|
|
|
// .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
|
|
|
|
2017-10-11 10:54:52 +08:00
|
|
|
std::string filename = getFilename(sec->file);
|
2017-04-05 10:05:48 +08:00
|
|
|
if (!cmd->filePat.match(filename) ||
|
|
|
|
pat.excludedFilePat.match(filename) ||
|
|
|
|
!pat.sectionPat.match(sec->name))
|
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);
|
2016-09-17 01:42:10 +08:00
|
|
|
}
|
2016-09-16 23:10:23 +08:00
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
sortInputSections(
|
|
|
|
MutableArrayRef<InputSectionBase *>(ret).slice(sizeBefore), pat);
|
2016-09-21 03:42:41 +08:00
|
|
|
}
|
2017-04-05 10:05:48 +08:00
|
|
|
return ret;
|
2016-09-14 22:32:08 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
void LinkerScript::discard(InputSectionBase *s) {
|
2019-11-26 05:24:18 +08:00
|
|
|
if (s == in.shStrTab || s == mainPart->relrDyn)
|
2019-09-24 19:48:31 +08:00
|
|
|
error("discarding " + s->name + " section is not allowed");
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (s == mainPart->gnuHashTab)
|
|
|
|
mainPart->gnuHashTab = nullptr;
|
|
|
|
if (s == mainPart->hashTab)
|
|
|
|
mainPart->hashTab = nullptr;
|
|
|
|
|
|
|
|
s->markDead();
|
2019-09-24 19:48:46 +08:00
|
|
|
s->parent = nullptr;
|
2019-09-24 19:48:31 +08:00
|
|
|
for (InputSection *ds : s->dependentSections)
|
|
|
|
discard(ds);
|
2016-09-14 22:32:08 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 19:48:31 +08:00
|
|
|
std::vector<InputSectionBase *>
|
2018-01-31 00:20:08 +08:00
|
|
|
LinkerScript::createInputSectionList(OutputSection &outCmd) {
|
2019-09-24 19:48:31 +08:00
|
|
|
std::vector<InputSectionBase *> ret;
|
2016-08-12 11:16:56 +08:00
|
|
|
|
2017-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *base : outCmd.sectionCommands) {
|
2017-10-11 12:01:13 +08:00
|
|
|
if (auto *cmd = dyn_cast<InputSectionDescription>(base)) {
|
2019-09-24 19:48:31 +08:00
|
|
|
cmd->sectionBases = computeInputSections(cmd);
|
2019-09-24 19:48:46 +08:00
|
|
|
for (InputSectionBase *s : cmd->sectionBases)
|
|
|
|
s->parent = &outCmd;
|
2019-09-24 19:48:31 +08:00
|
|
|
ret.insert(ret.end(), cmd->sectionBases.begin(), cmd->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() {
|
2017-11-01 16:40:28 +08:00
|
|
|
size_t i = 0;
|
|
|
|
for (BaseCommand *base : sectionCommands) {
|
|
|
|
if (auto *sec = dyn_cast<OutputSection>(base)) {
|
2019-09-24 19:48:31 +08:00
|
|
|
std::vector<InputSectionBase *> v = createInputSectionList(*sec);
|
2016-09-13 00:05:16 +08:00
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// The output section name `/DISCARD/' is special.
|
|
|
|
// Any input section assigned to it is discarded.
|
2017-07-28 03:22:43 +08:00
|
|
|
if (sec->name == "/DISCARD/") {
|
2019-09-24 19:48:31 +08:00
|
|
|
for (InputSectionBase *s : v)
|
|
|
|
discard(s);
|
2018-03-01 09:08:00 +08:00
|
|
|
sec->sectionCommands.clear();
|
2016-08-12 08:27:23 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-12 11:16:56 +08:00
|
|
|
|
2016-11-21 10:11:05 +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
|
2017-04-05 17:19:29 +08:00
|
|
|
// as if it wasn't present from the beginning.
|
2016-11-21 10:11:05 +08:00
|
|
|
//
|
2017-11-01 16:40:28 +08:00
|
|
|
// 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.
|
2017-07-28 03:22:43 +08:00
|
|
|
if (!matchConstraints(v, sec->constraint)) {
|
2017-02-23 10:28:28 +08:00
|
|
|
for (InputSectionBase *s : v)
|
2019-09-24 19:48:46 +08:00
|
|
|
s->parent = nullptr;
|
2017-11-01 16:40:28 +08:00
|
|
|
sec->sectionCommands.clear();
|
2016-09-17 05:05:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-21 10:11:05 +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.
|
2017-07-28 03:22:43 +08:00
|
|
|
if (sec->subalignExpr) {
|
|
|
|
uint32_t subalign = sec->subalignExpr().getValue();
|
2017-02-23 10:28:28 +08:00
|
|
|
for (InputSectionBase *s : v)
|
2016-11-21 10:11:05 +08:00
|
|
|
s->alignment = subalign;
|
2016-08-19 23:18:23 +08:00
|
|
|
}
|
2016-11-21 10:11:05 +08:00
|
|
|
|
2019-09-27 01:10:09 +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.
|
|
|
|
sec->partition = 1;
|
|
|
|
|
2017-11-01 16:40:28 +08:00
|
|
|
sec->sectionIndex = i++;
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
for (BaseCommand *base : sectionCommands) {
|
|
|
|
if (auto *cmd = dyn_cast<SymbolAssignment>(base))
|
|
|
|
addSymbol(cmd);
|
|
|
|
else
|
|
|
|
for (BaseCommand *sub_base : cast<OutputSection>(base)->sectionCommands)
|
|
|
|
if (auto *cmd = dyn_cast<SymbolAssignment>(sub_base))
|
|
|
|
addSymbol(cmd);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-10-07 07:06:55 +08:00
|
|
|
static OutputSection *findByName(ArrayRef<BaseCommand *> vec,
|
|
|
|
StringRef name) {
|
|
|
|
for (BaseCommand *base : vec)
|
|
|
|
if (auto *sec = dyn_cast<OutputSection>(base))
|
|
|
|
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>(
|
|
|
|
cast<InputSectionDescription>(sec->sectionCommands[0])
|
|
|
|
->sectionBases[0]);
|
|
|
|
if (firstIsec->getLinkOrderDep()->getOutputSection() !=
|
|
|
|
isec->getLinkOrderDep()->getOutputSection())
|
|
|
|
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;
|
2017-10-31 18:31:58 +08:00
|
|
|
std::vector<OutputSection *> 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) {
|
|
|
|
StringRef name = getOutputSectionName(s);
|
|
|
|
|
|
|
|
if (config->orphanHandling == OrphanHandlingPolicy::Error)
|
|
|
|
error(toString(s) + " is being placed in '" + name + "'");
|
|
|
|
else if (config->orphanHandling == OrphanHandlingPolicy::Warn)
|
|
|
|
warn(toString(s) + " is being placed in '" + name + "'");
|
|
|
|
|
|
|
|
if (OutputSection *sec = findByName(sectionCommands, name)) {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
// For futher --emit-reloc handling code we need target output section
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-10-11 10:46:09 +08:00
|
|
|
uint64_t LinkerScript::advance(uint64_t size, unsigned alignment) {
|
2017-10-11 10:45:54 +08:00
|
|
|
bool isTbss =
|
|
|
|
(ctx->outSec->flags & SHF_TLS) && ctx->outSec->type == SHT_NOBITS;
|
|
|
|
uint64_t start = isTbss ? dot + ctx->threadBssOffset : dot;
|
2017-10-11 10:46:09 +08:00
|
|
|
start = alignTo(start, alignment);
|
2017-05-04 11:00:27 +08:00
|
|
|
uint64_t end = start + size;
|
|
|
|
|
|
|
|
if (isTbss)
|
2017-10-11 10:45:54 +08:00
|
|
|
ctx->threadBssOffset = end - dot;
|
2017-05-04 11:00:27 +08:00
|
|
|
else
|
|
|
|
dot = end;
|
|
|
|
return end;
|
2016-09-22 20:35:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::output(InputSection *s) {
|
2018-08-06 18:44:17 +08:00
|
|
|
assert(ctx->outSec == s->getParent());
|
2017-07-25 16:29:29 +08:00
|
|
|
uint64_t before = advance(0, 1);
|
2017-05-04 11:00:27 +08:00
|
|
|
uint64_t pos = advance(s->getSize(), s->alignment);
|
2017-10-11 10:45:54 +08:00
|
|
|
s->outSecOff = pos - s->getSize() - ctx->outSec->addr;
|
2016-09-16 23:10:23 +08:00
|
|
|
|
|
|
|
// 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) }
|
2018-03-05 18:54:03 +08:00
|
|
|
expandOutputSection(pos - before);
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::switchTo(OutputSection *sec) {
|
2017-10-11 10:45:54 +08:00
|
|
|
ctx->outSec = sec;
|
2018-03-26 16:58:16 +08:00
|
|
|
|
|
|
|
uint64_t before = advance(0, 1);
|
2017-10-11 10:45:54 +08:00
|
|
|
ctx->outSec->addr = advance(0, ctx->outSec->alignment);
|
2018-03-26 16:58:16 +08:00
|
|
|
expandMemoryRegions(ctx->outSec->addr - before);
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
2016-09-01 17:55:57 +08:00
|
|
|
|
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
|
|
|
|
// returned. Otherwise, a nullptr is returned.
|
2017-07-28 03:22:43 +08:00
|
|
|
MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *sec) {
|
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))
|
|
|
|
return m;
|
2017-07-28 03:22:43 +08:00
|
|
|
error("memory region '" + sec->memoryRegionName + "' not declared");
|
2017-01-24 10:34:00 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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())
|
2017-01-24 10:34:00 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
if ((m->flags & sec->flags) && (m->negFlags & sec->flags) == 0)
|
|
|
|
return m;
|
2017-01-24 10:34:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, no suitable region was found.
|
|
|
|
if (sec->flags & SHF_ALLOC)
|
|
|
|
error("no memory region specified for section '" + sec->name + "'");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-06-14 04:57:43 +08:00
|
|
|
if (!(sec->flags & SHF_ALLOC))
|
|
|
|
dot = 0;
|
2017-02-18 00:26:13 +08:00
|
|
|
|
2017-10-11 10:45:54 +08:00
|
|
|
ctx->memRegion = sec->memRegion;
|
2018-01-26 01:42:03 +08:00
|
|
|
ctx->lmaRegion = sec->lmaRegion;
|
2017-10-11 10:45:54 +08:00
|
|
|
if (ctx->memRegion)
|
2018-01-25 10:18:00 +08:00
|
|
|
dot = ctx->memRegion->curPos;
|
2017-09-06 17:35:09 +08:00
|
|
|
|
2019-04-18 10:32:12 +08:00
|
|
|
if ((sec->flags & SHF_ALLOC) && sec->addrExpr)
|
|
|
|
setDot(sec->addrExpr, sec->location, false);
|
|
|
|
|
2019-08-09 09:25:49 +08:00
|
|
|
// 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,
|
|
|
|
ctx->memRegion->name, sec->name);
|
|
|
|
|
2018-01-13 07:26:25 +08:00
|
|
|
switchTo(sec);
|
|
|
|
|
2018-01-26 00:43:49 +08:00
|
|
|
if (sec->lmaExpr)
|
|
|
|
ctx->lmaOffset = sec->lmaExpr().getValue() - dot;
|
2017-03-14 16:57:09 +08:00
|
|
|
|
2018-01-26 00:43:49 +08:00
|
|
|
if (MemoryRegion *mr = sec->lmaRegion)
|
2018-01-26 01:42:03 +08:00
|
|
|
ctx->lmaOffset = mr->curPos - dot;
|
2018-01-12 17:07:35 +08:00
|
|
|
|
2018-01-13 07:26:25 +08:00
|
|
|
// If neither AT nor AT> is specified for an allocatable section, the linker
|
|
|
|
// will set the LMA such that the difference between VMA and LMA for the
|
|
|
|
// section is the same as the preceding output section in the same region
|
|
|
|
// https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
|
2018-08-02 18:45:46 +08:00
|
|
|
// This, however, should only be done by the first "non-header" section
|
|
|
|
// in the segment.
|
2018-01-26 03:02:08 +08:00
|
|
|
if (PhdrEntry *l = ctx->outSec->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.
|
|
|
|
for (BaseCommand *base : sec->sectionCommands) {
|
|
|
|
// This handles the assignments to symbol or to the dot.
|
|
|
|
if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
|
2018-04-04 17:39:05 +08:00
|
|
|
cmd->addr = dot;
|
2017-10-11 12:21:55 +08:00
|
|
|
assignSymbol(cmd, true);
|
2018-04-04 17:39:05 +08:00
|
|
|
cmd->size = dot - cmd->addr;
|
2017-10-11 12:21:55 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle BYTE(), SHORT(), LONG(), or QUAD().
|
2017-10-11 12:22:09 +08:00
|
|
|
if (auto *cmd = dyn_cast<ByteCommand>(base)) {
|
2017-10-11 12:21:55 +08:00
|
|
|
cmd->offset = dot - ctx->outSec->addr;
|
|
|
|
dot += cmd->size;
|
2018-03-05 18:54:03 +08:00
|
|
|
expandOutputSection(cmd->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.
|
2018-08-06 18:44:17 +08:00
|
|
|
for (InputSection *sec : cast<InputSectionDescription>(base)->sections)
|
2017-10-11 12:21:55 +08:00
|
|
|
output(sec);
|
|
|
|
}
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 09:08:00 +08:00
|
|
|
static bool isDiscardable(OutputSection &sec) {
|
2019-06-03 13:34:25 +08:00
|
|
|
if (sec.name == "/DISCARD/")
|
|
|
|
return true;
|
|
|
|
|
2018-03-01 09:08:00 +08:00
|
|
|
// We do not remove empty sections that are explicitly
|
|
|
|
// assigned to any segment.
|
|
|
|
if (!sec.phdrs.empty())
|
|
|
|
return false;
|
|
|
|
|
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;
|
|
|
|
|
2018-07-03 17:23:25 +08:00
|
|
|
for (BaseCommand *base : sec.sectionCommands) {
|
|
|
|
if (auto cmd = dyn_cast<SymbolAssignment>(base))
|
|
|
|
// Don't create empty output sections just for unreferenced PROVIDE
|
|
|
|
// symbols.
|
|
|
|
if (cmd->name != "." && !cmd->sym)
|
|
|
|
continue;
|
|
|
|
|
2018-03-01 09:08:00 +08:00
|
|
|
if (!isa<InputSectionDescription>(*base))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-23 18:53:04 +08:00
|
|
|
for (BaseCommand *&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.
|
|
|
|
bool isEmpty = getInputSections(sec).empty();
|
|
|
|
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
|
|
|
|
|
|
|
if (isEmpty && isDiscardable(*sec)) {
|
2019-05-29 11:55:20 +08:00
|
|
|
sec->markDead();
|
2018-02-23 18:53:04 +08:00
|
|
|
cmd = nullptr;
|
2019-06-08 01:57:58 +08:00
|
|
|
} else if (!sec->isLive()) {
|
|
|
|
sec->markLive();
|
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.
|
|
|
|
llvm::erase_if(sectionCommands, [&](BaseCommand *base) { return !base; });
|
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.
|
2017-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *base : sectionCommands) {
|
2017-07-28 03:22:43 +08:00
|
|
|
if (auto *sec = dyn_cast<OutputSection>(base)) {
|
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");
|
|
|
|
}
|
2017-07-28 03:22:43 +08:00
|
|
|
sec->memRegion = findMemoryRegion(sec);
|
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) } }
|
|
|
|
std::vector<StringRef> 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.
|
2017-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *base : sectionCommands) {
|
2017-07-28 03:22:43 +08:00
|
|
|
auto *sec = dyn_cast<OutputSection>(base);
|
|
|
|
if (!sec)
|
2016-11-14 23:39:38 +08:00
|
|
|
continue;
|
2017-04-05 11:20:42 +08:00
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
if (sec->phdrs.empty()) {
|
2017-07-03 18:11:25 +08:00
|
|
|
// To match the bfd linker script behaviour, only propagate program
|
|
|
|
// headers to sections that are allocated.
|
2017-07-28 03:22:43 +08:00
|
|
|
if (sec->flags & SHF_ALLOC)
|
|
|
|
sec->phdrs = defPhdrs;
|
2017-07-03 18:11:25 +08:00
|
|
|
} else {
|
2017-07-28 03:22:43 +08:00
|
|
|
defPhdrs = sec->phdrs;
|
2017-07-03 18:11:25 +08:00
|
|
|
}
|
2016-11-14 23:39:38 +08:00
|
|
|
}
|
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.
|
2017-07-27 15:46:50 +08:00
|
|
|
void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &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;
|
|
|
|
mr->curPos = mr->origin;
|
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
|
|
|
|
2019-08-15 06:28:17 +08:00
|
|
|
auto deleter = std::make_unique<AddressState>();
|
2017-10-24 05:12:19 +08:00
|
|
|
ctx = deleter.get();
|
2017-03-17 21:05:04 +08:00
|
|
|
errorOnMissingSection = true;
|
2017-02-07 06:21:46 +08:00
|
|
|
switchTo(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);
|
2017-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *base : sectionCommands) {
|
2017-04-05 11:20:42 +08:00
|
|
|
if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
|
2018-04-05 19:25:58 +08:00
|
|
|
cmd->addr = dot;
|
2017-04-05 11:20:22 +08:00
|
|
|
assignSymbol(cmd, false);
|
2018-04-05 19:25:58 +08:00
|
|
|
cmd->size = dot - cmd->addr;
|
2016-07-12 14:39:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-07-28 03:22:43 +08:00
|
|
|
assignOffsets(cast<OutputSection>(base));
|
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.
|
2017-07-27 15:46:50 +08:00
|
|
|
std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
|
|
|
|
std::vector<PhdrEntry *> 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) {
|
2017-10-08 11:45:49 +08:00
|
|
|
PhdrEntry *phdr = make<PhdrEntry>(cmd.type, cmd.flags ? *cmd.flags : 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)) {
|
|
|
|
if (auto *ds = dyn_cast<Defined>(sym))
|
|
|
|
return {ds->section, false, ds->value, loc};
|
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.
|
2017-07-28 03:22:43 +08:00
|
|
|
std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *cmd) {
|
2017-07-06 06:30:04 +08:00
|
|
|
std::vector<size_t> 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")
|
|
|
|
error(cmd->location + ": section header '" + s +
|
|
|
|
"' is not listed in PHDRS");
|
|
|
|
}
|
2017-07-06 06:30:04 +08:00
|
|
|
return ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
2019-10-07 16:31:18 +08:00
|
|
|
|
|
|
|
} // namespace elf
|
|
|
|
} // namespace lld
|