2015-10-01 01:23:26 +08:00
|
|
|
//===- LinkerScript.cpp ---------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
using namespace lld;
|
2016-02-28 08:25:54 +08:00
|
|
|
using namespace lld::elf;
|
2015-10-01 01:23:26 +08:00
|
|
|
|
2017-03-22 07:03:09 +08:00
|
|
|
LinkerScript *elf::Script;
|
|
|
|
|
2017-08-21 15:57:12 +08:00
|
|
|
static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) {
|
|
|
|
if (OutputSection *OS = InputSec->getOutputSection())
|
|
|
|
return OS->Addr;
|
|
|
|
error(Loc + ": unable to evaluate expression: input section " +
|
|
|
|
InputSec->Name + " has no output section assigned");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-17 21:05:04 +08:00
|
|
|
uint64_t ExprValue::getValue() const {
|
2017-08-21 15:57:12 +08:00
|
|
|
if (Sec)
|
|
|
|
return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc),
|
|
|
|
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)
|
2017-08-21 15:57:12 +08:00
|
|
|
return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc);
|
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.
|
2018-02-07 17:00:34 +08:00
|
|
|
if (Alignment == 1 && (!Sec || !Sec->getOutputSection()))
|
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 {
|
2017-12-12 07:25:27 +08:00
|
|
|
Sec = make<OutputSection>(Name, SHT_NOBITS, 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);
|
2018-08-02 16:13:56 +08:00
|
|
|
// Only expand the LMARegion if it is different from MemRegion.
|
|
|
|
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
|
|
|
else
|
|
|
|
expandMemoryRegions(Val - Dot);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// This function is called from processSectionCommands,
|
|
|
|
// while we are fixing the output section layout.
|
|
|
|
void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
|
|
|
|
if (!shouldDefineSym(Cmd))
|
2017-10-11 09:03:37 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Define a symbol.
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *Sym;
|
2017-10-11 09:03:37 +08:00
|
|
|
uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
|
|
|
|
std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, /*Type*/ 0, Visibility,
|
|
|
|
/*CanOmitFromDynSym*/ false,
|
|
|
|
/*File*/ nullptr);
|
|
|
|
ExprValue Value = Cmd->Expression();
|
|
|
|
SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2017-11-17 09:37:50 +08:00
|
|
|
replaceSymbol<Defined>(Sym, nullptr, Cmd->Name, STB_GLOBAL, Visibility,
|
2017-11-06 12:35:31 +08:00
|
|
|
STT_NOTYPE, SymValue, 0, Sec);
|
|
|
|
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;
|
|
|
|
|
|
|
|
// We can't calculate final value right now.
|
|
|
|
Symbol *Sym;
|
|
|
|
uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
|
|
|
|
std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, /*Type*/ 0, Visibility,
|
|
|
|
/*CanOmitFromDynSym*/ false,
|
|
|
|
/*File*/ nullptr);
|
|
|
|
replaceSymbol<Defined>(Sym, nullptr, Cmd->Name, STB_GLOBAL, Visibility,
|
|
|
|
STT_NOTYPE, 0, 0, nullptr);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
2017-02-23 10:32:18 +08:00
|
|
|
static std::function<bool(InputSectionBase *, InputSectionBase *)>
|
2016-09-17 04:21:55 +08:00
|
|
|
getComparator(SortSectionPolicy K) {
|
|
|
|
switch (K) {
|
|
|
|
case SortSectionPolicy::Alignment:
|
2017-04-05 08:43:25 +08:00
|
|
|
return [](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;
|
|
|
|
};
|
2016-09-17 04:21:55 +08:00
|
|
|
case SortSectionPolicy::Name:
|
2017-04-05 08:43:25 +08:00
|
|
|
return [](InputSectionBase *A, InputSectionBase *B) {
|
|
|
|
return A->Name < B->Name;
|
|
|
|
};
|
2016-09-17 04:21:55 +08:00
|
|
|
case SortSectionPolicy::Priority:
|
2017-04-05 08:43:25 +08:00
|
|
|
return [](InputSectionBase *A, InputSectionBase *B) {
|
|
|
|
return getPriority(A->Name) < getPriority(B->Name);
|
|
|
|
};
|
2016-09-17 04:21:55 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown sort policy");
|
|
|
|
}
|
2016-08-05 06:27:00 +08:00
|
|
|
}
|
2016-07-29 23:32:46 +08:00
|
|
|
|
2017-04-05 08:43:25 +08:00
|
|
|
// A helper function for the SORT() command.
|
2017-10-11 12:01:13 +08:00
|
|
|
static bool matchConstraints(ArrayRef<InputSection *> 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(
|
2017-10-11 12:01:13 +08:00
|
|
|
Sections, [](InputSection *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
|
|
|
}
|
|
|
|
|
2017-10-11 11:23:29 +08:00
|
|
|
static void sortSections(MutableArrayRef<InputSection *> Vec,
|
2016-09-21 03:42:41 +08:00
|
|
|
SortSectionPolicy K) {
|
|
|
|
if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
|
2017-10-11 11:23:29 +08:00
|
|
|
std::stable_sort(Vec.begin(), Vec.end(), getComparator(K));
|
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.
|
2018-01-31 00:20:08 +08:00
|
|
|
static void sortInputSections(MutableArrayRef<InputSection *> Vec,
|
|
|
|
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.
|
2017-05-24 06:47:31 +08:00
|
|
|
std::vector<InputSection *>
|
2018-01-31 00:20:08 +08:00
|
|
|
LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
|
2017-05-24 06:47:31 +08:00
|
|
|
std::vector<InputSection *> 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) {
|
2017-10-27 19:32:22 +08:00
|
|
|
if (!Sec->Live || Sec->Assigned)
|
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.
|
2018-03-27 02:55:33 +08:00
|
|
|
if (auto *IS = dyn_cast<InputSection>(Sec))
|
|
|
|
if (IS->getRelocatedSection())
|
|
|
|
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
|
|
|
|
2017-10-11 11:23:17 +08:00
|
|
|
// It is safe to assume that Sec is an InputSection
|
|
|
|
// because mergeable or EH input sections have already been
|
|
|
|
// handled and eliminated.
|
2017-05-24 06:47:31 +08:00
|
|
|
Ret.push_back(cast<InputSection>(Sec));
|
2017-04-05 10:05:48 +08:00
|
|
|
Sec->Assigned = true;
|
2016-09-17 01:42:10 +08:00
|
|
|
}
|
2016-09-16 23:10:23 +08:00
|
|
|
|
2017-10-11 12:50:30 +08:00
|
|
|
sortInputSections(MutableArrayRef<InputSection *>(Ret).slice(SizeBefore),
|
2018-01-31 00:20:08 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-11 12:01:13 +08:00
|
|
|
void LinkerScript::discard(ArrayRef<InputSection *> V) {
|
|
|
|
for (InputSection *S : V) {
|
2018-09-26 03:26:58 +08:00
|
|
|
if (S == In.ShStrTab || S == In.Dynamic || S == In.DynSymTab ||
|
|
|
|
S == In.DynStrTab || S == In.RelaPlt || S == In.RelaDyn ||
|
|
|
|
S == In.RelrDyn)
|
2017-06-17 07:45:35 +08:00
|
|
|
error("discarding " + S->Name + " section is not allowed");
|
2017-10-26 06:03:21 +08:00
|
|
|
|
2018-03-07 19:47:15 +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.
|
2018-09-26 03:26:58 +08:00
|
|
|
if (S == In.GnuHashTab)
|
|
|
|
In.GnuHashTab = nullptr;
|
|
|
|
if (S == In.HashTab)
|
|
|
|
In.HashTab = nullptr;
|
2018-03-07 19:47:15 +08:00
|
|
|
|
2017-10-26 06:03:21 +08:00
|
|
|
S->Assigned = false;
|
|
|
|
S->Live = false;
|
2017-02-18 03:34:05 +08:00
|
|
|
discard(S->DependentSections);
|
2016-09-14 22:32:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:20:08 +08:00
|
|
|
std::vector<InputSection *>
|
|
|
|
LinkerScript::createInputSectionList(OutputSection &OutCmd) {
|
2017-10-11 12:01:13 +08:00
|
|
|
std::vector<InputSection *> 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)) {
|
2018-01-31 00:20:08 +08:00
|
|
|
Cmd->Sections = computeInputSections(Cmd);
|
2017-10-11 12:01:13 +08:00
|
|
|
Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
|
|
|
|
}
|
2016-08-12 11:16:56 +08:00
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
|
2017-10-21 07:28:19 +08:00
|
|
|
void LinkerScript::processSectionCommands() {
|
2017-03-09 06:36:28 +08:00
|
|
|
// A symbol can be assigned before any section is mentioned in the linker
|
|
|
|
// script. In an DSO, the symbol values are addresses, so the only important
|
|
|
|
// section values are:
|
|
|
|
// * SHN_UNDEF
|
|
|
|
// * SHN_ABS
|
|
|
|
// * Any value meaning a regular section.
|
|
|
|
// To handle that, create a dummy aether section that fills the void before
|
|
|
|
// the linker scripts switches to another section. It has an index of one
|
|
|
|
// which will map to whatever the first actual section is.
|
|
|
|
Aether = make<OutputSection>("", 0, SHF_ALLOC);
|
|
|
|
Aether->SectionIndex = 1;
|
2017-10-11 09:03:37 +08:00
|
|
|
|
2017-10-11 10:45:54 +08:00
|
|
|
// Ctx captures the local AddressState and makes it accessible deliberately.
|
|
|
|
// This is needed as there are some cases where we cannot just
|
2017-07-11 17:28:27 +08:00
|
|
|
// thread the current state through to a lambda function created by the
|
|
|
|
// script parser.
|
2017-10-24 05:12:19 +08:00
|
|
|
auto Deleter = make_unique<AddressState>();
|
|
|
|
Ctx = Deleter.get();
|
2017-10-11 10:45:54 +08:00
|
|
|
Ctx->OutSec = Aether;
|
2017-03-09 06:36:28 +08:00
|
|
|
|
2017-11-01 16:40:28 +08:00
|
|
|
size_t I = 0;
|
2017-10-11 12:01:24 +08:00
|
|
|
// Add input sections to output sections.
|
2017-11-01 16:40:28 +08:00
|
|
|
for (BaseCommand *Base : SectionCommands) {
|
2016-11-21 10:11:05 +08:00
|
|
|
// Handle symbol assignments outside of any output section.
|
2017-11-01 16:40:28 +08:00
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
|
2017-02-18 00:01:51 +08:00
|
|
|
addSymbol(Cmd);
|
2016-08-12 11:33:04 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-21 10:11:05 +08:00
|
|
|
|
2017-11-01 16:40:28 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base)) {
|
2018-01-31 00:20:08 +08:00
|
|
|
std::vector<InputSection *> 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/") {
|
2016-09-13 00:05:16 +08:00
|
|
|
discard(V);
|
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)
|
2016-11-21 07:15:52 +08:00
|
|
|
S->Assigned = false;
|
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
|
|
|
// A directive may contain symbol definitions like this:
|
|
|
|
// ".foo : { ...; bar = .; }". Handle them.
|
2017-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *Base : Sec->SectionCommands)
|
2017-04-05 11:20:42 +08:00
|
|
|
if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
|
2017-02-18 00:01:51 +08:00
|
|
|
addSymbol(OutCmd);
|
2016-09-17 05:05:36 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
// Add input sections to an output section.
|
2017-10-11 12:01:13 +08:00
|
|
|
for (InputSection *S : V)
|
|
|
|
Sec->addSection(S);
|
2017-11-01 16:40:28 +08:00
|
|
|
|
|
|
|
Sec->SectionIndex = I++;
|
|
|
|
if (Sec->Noload)
|
|
|
|
Sec->Type = SHT_NOBITS;
|
2018-02-16 18:42:58 +08:00
|
|
|
if (Sec->NonAlloc)
|
|
|
|
Sec->Flags &= ~(uint64_t)SHF_ALLOC;
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
2016-08-12 08:27:23 +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
|
|
|
|
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 *IS,
|
|
|
|
StringRef OutsecName) {
|
|
|
|
OutputSection *Sec = Script->createOutputSection(OutsecName, "<internal>");
|
|
|
|
Sec->addSection(cast<InputSection>(IS));
|
|
|
|
return Sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static OutputSection *addInputSec(StringMap<OutputSection *> &Map,
|
|
|
|
InputSectionBase *IS, StringRef OutsecName) {
|
|
|
|
// 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 (IS->Type == SHT_GROUP || (IS->Flags & SHF_GROUP))
|
|
|
|
return createSection(IS, 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>(IS) &&
|
|
|
|
(IS->Type == SHT_REL || IS->Type == SHT_RELA)) {
|
|
|
|
auto *Sec = cast<InputSection>(IS);
|
|
|
|
OutputSection *Out = Sec->getRelocatedSection()->getOutputSection();
|
|
|
|
|
|
|
|
if (Out->RelocationSection) {
|
|
|
|
Out->RelocationSection->addSection(Sec);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Out->RelocationSection = createSection(IS, OutsecName);
|
|
|
|
return Out->RelocationSection;
|
|
|
|
}
|
|
|
|
|
2017-11-17 19:27:57 +08:00
|
|
|
// When control reaches here, mergeable sections have already been merged into
|
|
|
|
// synthetic sections. For relocatable case we want to create one output
|
|
|
|
// section per syntetic section so that they have a valid sh_entsize.
|
2017-11-05 07:54:25 +08:00
|
|
|
if (Config->Relocatable && (IS->Flags & SHF_MERGE))
|
|
|
|
return createSection(IS, OutsecName);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
OutputSection *&Sec = Map[OutsecName];
|
|
|
|
if (Sec) {
|
|
|
|
Sec->addSection(cast<InputSection>(IS));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sec = createSection(IS, OutsecName);
|
|
|
|
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() {
|
2017-10-11 09:50:56 +08:00
|
|
|
unsigned End = SectionCommands.size();
|
2017-11-05 07:54:25 +08:00
|
|
|
StringMap<OutputSection *> Map;
|
2017-10-31 18:31:58 +08:00
|
|
|
std::vector<OutputSection *> V;
|
2018-03-23 17:18:31 +08:00
|
|
|
|
|
|
|
auto Add = [&](InputSectionBase *S) {
|
2017-06-01 04:17:44 +08:00
|
|
|
if (!S->Live || S->Parent)
|
2018-03-23 17:18:31 +08:00
|
|
|
return;
|
2017-10-07 07:06:55 +08:00
|
|
|
|
2017-12-01 17:04:52 +08:00
|
|
|
StringRef Name = getOutputSectionName(S);
|
2017-11-05 07:57:51 +08:00
|
|
|
|
|
|
|
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 + "'");
|
2017-10-07 07:06:55 +08:00
|
|
|
|
2017-10-11 09:19:33 +08:00
|
|
|
if (OutputSection *Sec =
|
2017-10-11 09:50:56 +08:00
|
|
|
findByName(makeArrayRef(SectionCommands).slice(0, End), Name)) {
|
2017-10-07 08:43:31 +08:00
|
|
|
Sec->addSection(cast<InputSection>(S));
|
2018-03-23 17:18:31 +08:00
|
|
|
return;
|
2017-04-29 23:44:03 +08:00
|
|
|
}
|
2017-10-07 07:34:43 +08:00
|
|
|
|
2017-11-05 07:54:25 +08:00
|
|
|
if (OutputSection *OS = addInputSec(Map, S, Name))
|
2017-10-31 18:31:58 +08:00
|
|
|
V.push_back(OS);
|
2018-03-08 03:25:36 +08:00
|
|
|
assert(S->getOutputSection()->SectionIndex == UINT32_MAX);
|
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 *IS : InputSections) {
|
2018-03-27 02:49:31 +08:00
|
|
|
if (auto *Sec = dyn_cast<InputSection>(IS))
|
|
|
|
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(IS);
|
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-07-28 03:22:43 +08:00
|
|
|
else if (Sec->AddrExpr)
|
|
|
|
setDot(Sec->AddrExpr, Sec->Location, false);
|
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
|
|
|
|
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) {
|
|
|
|
// We do not remove empty sections that are explicitly
|
|
|
|
// assigned to any segment.
|
|
|
|
if (!Sec.Phdrs.empty())
|
|
|
|
return false;
|
|
|
|
|
2018-03-01 20:27:04 +08:00
|
|
|
// We do not want to remove sections that reference symbols in address and
|
|
|
|
// other expressions. We add script symbols as undefined, and want to ensure
|
|
|
|
// all of them are defined in the output, hence have to keep them.
|
|
|
|
if (Sec.ExpressionsUseSymbols)
|
2018-03-01 09:08:00 +08:00
|
|
|
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());
|
|
|
|
|
2018-03-01 09:08:00 +08:00
|
|
|
// A live output section means that some input section was added to it. It
|
2018-03-13 16:32:56 +08:00
|
|
|
// might have been removed (if it was empty synthetic section), but we at
|
|
|
|
// least know the flags.
|
2018-03-01 09:08:00 +08:00
|
|
|
if (Sec->Live)
|
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)
|
|
|
|
Sec->Flags = Flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR);
|
|
|
|
|
|
|
|
if (IsEmpty && isDiscardable(*Sec)) {
|
2018-03-01 09:08:00 +08:00
|
|
|
Sec->Live = false;
|
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.
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-08-24 02:44:34 +08:00
|
|
|
// Try to find an address for the file and program headers output sections,
|
|
|
|
// which were unconditionally added to the first PT_LOAD segment earlier.
|
|
|
|
//
|
|
|
|
// When using the default layout, we check if the headers fit below the first
|
|
|
|
// allocated section. When using a linker script, we also check if the headers
|
|
|
|
// are covered by the output section. This allows omitting the headers by not
|
|
|
|
// leaving enough space for them in the linker script; this pattern is common
|
|
|
|
// in embedded systems.
|
|
|
|
//
|
|
|
|
// 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);
|
|
|
|
|
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;
|
|
|
|
});
|
2017-05-05 03:34:17 +08:00
|
|
|
uint64_t HeaderSize = getHeaderSize();
|
2018-03-01 23:25:46 +08:00
|
|
|
if (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
|
|
|
static uint64_t getInitialDot() {
|
|
|
|
// By default linker scripts use an initial value of 0 for '.',
|
|
|
|
// but prefer -image-base if set.
|
|
|
|
if (Script->HasSectionsCommand)
|
|
|
|
return Config->ImageBase ? *Config->ImageBase : 0;
|
|
|
|
|
|
|
|
uint64_t StartAddr = UINT64_MAX;
|
|
|
|
// The Sections with -T<section> have been sorted in order of ascending
|
|
|
|
// address. We must lower StartAddr if the lowest -T<section address> as
|
|
|
|
// calls to setDot() must be monotonically increasing.
|
|
|
|
for (auto &KV : Config->SectionStartMap)
|
|
|
|
StartAddr = std::min(StartAddr, KV.second);
|
|
|
|
return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2017-07-05 17:12:54 +08:00
|
|
|
void LinkerScript::assignAddresses() {
|
2017-10-31 18:31:58 +08:00
|
|
|
Dot = getInitialDot();
|
2017-10-11 09:03:37 +08:00
|
|
|
|
2017-10-24 05:12:19 +08:00
|
|
|
auto Deleter = make_unique<AddressState>();
|
|
|
|
Ctx = Deleter.get();
|
2017-03-17 21:05:04 +08:00
|
|
|
ErrorOnMissingSection = true;
|
2017-02-07 06:21:46 +08:00
|
|
|
switchTo(Aether);
|
|
|
|
|
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
|
|
|
}
|
2017-10-11 10:45:54 +08:00
|
|
|
Ctx = nullptr;
|
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;
|
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
|
|
|
}
|