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-12-18 22:06:06 +08:00
|
|
|
#include "Memory.h"
|
2016-04-16 18:10:32 +08:00
|
|
|
#include "OutputSections.h"
|
2016-06-29 16:01:32 +08:00
|
|
|
#include "Strings.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"
|
|
|
|
#include "Threads.h"
|
2016-07-19 17:25:43 +08:00
|
|
|
#include "Writer.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-07-27 02:47:49 +08:00
|
|
|
static SymbolBody *addRegular(SymbolAssignment *Cmd) {
|
2017-02-22 06:32:51 +08:00
|
|
|
Symbol *Sym;
|
2016-10-31 21:14:53 +08:00
|
|
|
uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
|
2017-07-27 02:42:48 +08:00
|
|
|
std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, /*Type*/ 0, Visibility,
|
|
|
|
/*CanOmitFromDynSym*/ false,
|
|
|
|
/*File*/ nullptr);
|
2017-02-22 06:32:51 +08:00
|
|
|
Sym->Binding = STB_GLOBAL;
|
2017-03-17 21:05:04 +08:00
|
|
|
ExprValue Value = Cmd->Expression();
|
|
|
|
SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
|
2017-04-14 17:23:26 +08:00
|
|
|
|
|
|
|
// 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.isAbsolute() ? Value.getValue() : 0;
|
2017-08-05 06:31:42 +08:00
|
|
|
replaceBody<DefinedRegular>(Sym, nullptr, Cmd->Name, /*IsLocal=*/false,
|
|
|
|
Visibility, STT_NOTYPE, SymValue, 0, Sec);
|
2017-01-10 02:36:57 +08:00
|
|
|
return Sym->body();
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
|
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-07-28 03:22:43 +08:00
|
|
|
Sec = make<OutputSection>(Name, SHT_PROGBITS, 0);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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: " +
|
|
|
|
CurAddressState->OutSec->Name);
|
2017-02-18 00:26:13 +08:00
|
|
|
Dot = Val;
|
|
|
|
// Update to location counter means update to section size.
|
|
|
|
if (InSec)
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr;
|
2017-02-18 00:26:13 +08:00
|
|
|
}
|
|
|
|
|
2017-02-07 18:23:28 +08:00
|
|
|
// Sets value of a symbol. Two kinds of symbols are processed: synthetic
|
|
|
|
// symbols, whose value is an offset from beginning of section and regular
|
|
|
|
// symbols whose value is absolute.
|
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-09 06:36:28 +08:00
|
|
|
auto *Sym = cast<DefinedRegular>(Cmd->Sym);
|
2017-03-17 21:05:04 +08:00
|
|
|
ExprValue V = Cmd->Expression();
|
|
|
|
if (V.isAbsolute()) {
|
|
|
|
Sym->Value = V.getValue();
|
|
|
|
} else {
|
|
|
|
Sym->Section = V.Sec;
|
2017-06-14 04:57:43 +08:00
|
|
|
Sym->Value = alignTo(V.Val, V.Alignment);
|
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-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
|
2016-08-12 07:22:52 +08:00
|
|
|
if (Cmd->Name == ".")
|
2017-01-10 02:36:57 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// If a symbol was in PROVIDE(), we need to define it only when
|
|
|
|
// it is a referenced undefined symbol.
|
2017-07-27 02:42:48 +08:00
|
|
|
SymbolBody *B = Symtab->find(Cmd->Name);
|
2017-01-10 02:36:57 +08:00
|
|
|
if (Cmd->Provide && (!B || B->isDefined()))
|
|
|
|
return;
|
|
|
|
|
2017-07-27 02:47:49 +08:00
|
|
|
Cmd->Sym = addRegular(Cmd);
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 14:43:01 +08:00
|
|
|
bool SymbolAssignment::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == AssignmentKind;
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:26:59 +08:00
|
|
|
bool InputSectionDescription::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == InputSectionKind;
|
|
|
|
}
|
|
|
|
|
2016-08-04 17:29:31 +08:00
|
|
|
bool AssertCommand::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == AssertKind;
|
|
|
|
}
|
|
|
|
|
2016-09-27 03:22:50 +08:00
|
|
|
bool BytesDataCommand::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == BytesDataKind;
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
static StringRef basename(InputSectionBase *S) {
|
|
|
|
if (S->File)
|
|
|
|
return sys::path::filename(S->File->getName());
|
2016-11-21 10:10:12 +08:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
bool LinkerScript::shouldKeep(InputSectionBase *S) {
|
2016-11-21 10:10:12 +08:00
|
|
|
for (InputSectionDescription *ID : Opt.KeptSections)
|
|
|
|
if (ID->FilePat.match(basename(S)))
|
|
|
|
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-02-23 10:28:28 +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
|
|
|
|
|
|
|
bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) {
|
|
|
|
return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE;
|
2016-08-12 17:07:57 +08:00
|
|
|
});
|
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-05-24 06:47:31 +08:00
|
|
|
static void sortSections(InputSection **Begin, InputSection **End,
|
2016-09-21 03:42:41 +08:00
|
|
|
SortSectionPolicy K) {
|
|
|
|
if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
|
2016-09-21 23:56:44 +08:00
|
|
|
std::stable_sort(Begin, End, getComparator(K));
|
2016-09-21 03:42:41 +08:00
|
|
|
}
|
|
|
|
|
2017-08-04 18:25:29 +08:00
|
|
|
static llvm::DenseMap<SectionBase *, int> getSectionOrder() {
|
|
|
|
switch (Config->EKind) {
|
|
|
|
case ELF32LEKind:
|
|
|
|
return buildSectionOrder<ELF32LE>();
|
|
|
|
case ELF32BEKind:
|
|
|
|
return buildSectionOrder<ELF32BE>();
|
|
|
|
case ELF64LEKind:
|
|
|
|
return buildSectionOrder<ELF64LE>();
|
|
|
|
case ELF64BEKind:
|
|
|
|
return buildSectionOrder<ELF64BE>();
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown ELF type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sortBySymbolOrder(InputSection **Begin, InputSection **End) {
|
|
|
|
if (Config->SymbolOrderingFile.empty())
|
|
|
|
return;
|
|
|
|
static llvm::DenseMap<SectionBase *, int> Order = getSectionOrder();
|
|
|
|
MutableArrayRef<InputSection *> In(Begin, End - Begin);
|
|
|
|
sortByOrder(In, [&](InputSectionBase *S) { return Order.lookup(S); });
|
|
|
|
}
|
|
|
|
|
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 *>
|
2017-04-05 10:05:48 +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-05-31 04:40:03 +08:00
|
|
|
if (Sec->Assigned)
|
2017-05-30 13:17:58 +08:00
|
|
|
continue;
|
|
|
|
|
2017-05-31 04:40:03 +08:00
|
|
|
if (!Sec->Live) {
|
|
|
|
reportDiscarded(Sec);
|
2016-11-06 06:37:59 +08:00
|
|
|
continue;
|
2017-05-31 04:40:03 +08:00
|
|
|
}
|
2017-04-05 10:05:48 +08:00
|
|
|
|
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.
|
2017-04-05 10:05:48 +08:00
|
|
|
if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
|
2017-02-16 22:36:09 +08:00
|
|
|
continue;
|
2016-11-06 06:37:59 +08:00
|
|
|
|
2017-04-05 10:05:48 +08:00
|
|
|
StringRef Filename = basename(Sec);
|
|
|
|
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-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
|
|
|
|
2016-09-21 23:56:44 +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.
|
2017-08-04 18:25:29 +08:00
|
|
|
// 5. If no SORT commands are given and --sort-section is not specified,
|
|
|
|
// apply sorting provided by --symbol-ordering-file if any exist.
|
2017-05-24 06:47:31 +08:00
|
|
|
InputSection **Begin = Ret.data() + SizeBefore;
|
|
|
|
InputSection **End = Ret.data() + Ret.size();
|
2017-08-04 18:25:29 +08:00
|
|
|
if (Pat.SortOuter == SortSectionPolicy::Default &&
|
|
|
|
Config->SortSection == SortSectionPolicy::Default) {
|
|
|
|
sortBySymbolOrder(Begin, End);
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-21 23:56:44 +08:00
|
|
|
if (Pat.SortOuter != SortSectionPolicy::None) {
|
|
|
|
if (Pat.SortInner == SortSectionPolicy::Default)
|
|
|
|
sortSections(Begin, End, Config->SortSection);
|
|
|
|
else
|
|
|
|
sortSections(Begin, End, Pat.SortInner);
|
|
|
|
sortSections(Begin, End, Pat.SortOuter);
|
|
|
|
}
|
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-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
|
2017-02-23 10:28:28 +08:00
|
|
|
for (InputSectionBase *S : V) {
|
2016-09-14 22:32:08 +08:00
|
|
|
S->Live = false;
|
2017-08-10 16:07:05 +08:00
|
|
|
if (S == InX::ShStrTab || S == InX::Common || S == InX::Dynamic ||
|
|
|
|
S == InX::DynSymTab || S == InX::DynStrTab)
|
2017-06-17 07:45:35 +08:00
|
|
|
error("discarding " + S->Name + " section is not allowed");
|
2017-02-18 03:34:05 +08:00
|
|
|
discard(S->DependentSections);
|
2016-09-14 22:32:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
std::vector<InputSectionBase *>
|
2017-07-28 03:22:43 +08:00
|
|
|
LinkerScript::createInputSectionList(OutputSection &OutCmd) {
|
2017-02-23 10:28:28 +08:00
|
|
|
std::vector<InputSectionBase *> Ret;
|
2016-08-12 11:16:56 +08:00
|
|
|
|
2017-04-05 11:20:42 +08:00
|
|
|
for (BaseCommand *Base : OutCmd.Commands) {
|
|
|
|
auto *Cmd = dyn_cast<InputSectionDescription>(Base);
|
2016-09-17 05:05:36 +08:00
|
|
|
if (!Cmd)
|
2016-08-12 11:16:56 +08:00
|
|
|
continue;
|
2017-04-05 10:05:48 +08:00
|
|
|
|
|
|
|
Cmd->Sections = computeInputSections(Cmd);
|
2017-04-08 00:10:46 +08:00
|
|
|
Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
|
2016-08-12 11:16:56 +08:00
|
|
|
}
|
2016-09-17 04:34:02 +08:00
|
|
|
|
2016-08-12 11:16:56 +08:00
|
|
|
return Ret;
|
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::processCommands(OutputSectionFactory &Factory) {
|
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-07-07 17:11:27 +08:00
|
|
|
auto State = make_unique<AddressState>(Opt);
|
2017-07-11 17:28:27 +08:00
|
|
|
// CurAddressState 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.
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState = State.get();
|
|
|
|
CurAddressState->OutSec = Aether;
|
2017-03-20 22:33:33 +08:00
|
|
|
Dot = 0;
|
2017-03-09 06:36:28 +08:00
|
|
|
|
2017-04-06 00:07:44 +08:00
|
|
|
for (size_t I = 0; I < Opt.Commands.size(); ++I) {
|
2016-11-21 10:11:05 +08:00
|
|
|
// Handle symbol assignments outside of any output section.
|
2017-04-06 00:07:44 +08:00
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) {
|
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-07-28 03:22:43 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Opt.Commands[I])) {
|
|
|
|
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/") {
|
2016-09-13 00:05:16 +08:00
|
|
|
discard(V);
|
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
|
|
|
//
|
|
|
|
// Because we'll iterate over Commands many more times, the easiest
|
2017-04-05 17:19:29 +08:00
|
|
|
// way to "make it as if it wasn't present" is to just remove it.
|
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-04-06 00:07:44 +08:00
|
|
|
Opt.Commands.erase(Opt.Commands.begin() + I);
|
2017-04-05 17:19:29 +08:00
|
|
|
--I;
|
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-07-28 03:22:43 +08:00
|
|
|
for (BaseCommand *Base : Sec->Commands)
|
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-05-08 18:18:12 +08:00
|
|
|
for (InputSectionBase *S : V)
|
2017-07-28 03:22:43 +08:00
|
|
|
Factory.addInputSec(S, Sec->Name, Sec);
|
|
|
|
assert(Sec->SectionIndex == INT_MAX);
|
|
|
|
Sec->SectionIndex = I;
|
|
|
|
if (Sec->Noload)
|
|
|
|
Sec->Type = SHT_NOBITS;
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
}
|
2017-07-11 17:28:27 +08:00
|
|
|
CurAddressState = nullptr;
|
2016-09-16 23:30:47 +08:00
|
|
|
}
|
2016-07-20 22:43:20 +08:00
|
|
|
|
2017-05-05 03:34:17 +08:00
|
|
|
void LinkerScript::fabricateDefaultCommands() {
|
2017-04-19 20:46:32 +08:00
|
|
|
// Define start address
|
2017-06-08 12:17:04 +08:00
|
|
|
uint64_t StartAddr = -1;
|
2017-04-19 20:46:32 +08:00
|
|
|
|
2017-05-03 16:44:50 +08:00
|
|
|
// 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.
|
2017-07-18 19:55:35 +08:00
|
|
|
for (auto &KV : Config->SectionStartMap)
|
2017-05-03 16:44:50 +08:00
|
|
|
StartAddr = std::min(StartAddr, KV.second);
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
Opt.Commands.insert(Opt.Commands.begin(),
|
|
|
|
make<SymbolAssignment>(".",
|
|
|
|
[=] {
|
|
|
|
return std::min(
|
|
|
|
StartAddr,
|
|
|
|
Config->ImageBase +
|
|
|
|
elf::getHeaderSize());
|
|
|
|
},
|
|
|
|
""));
|
2017-04-19 20:46:32 +08:00
|
|
|
}
|
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// Add sections that didn't match any sections command.
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
|
2017-07-06 01:50:43 +08:00
|
|
|
unsigned NumCommands = Opt.Commands.size();
|
2017-04-27 06:30:15 +08:00
|
|
|
for (InputSectionBase *S : InputSections) {
|
2017-06-01 04:17:44 +08:00
|
|
|
if (!S->Live || S->Parent)
|
2017-04-27 06:30:15 +08:00
|
|
|
continue;
|
|
|
|
StringRef Name = getOutputSectionName(S->Name);
|
2017-07-06 01:50:43 +08:00
|
|
|
auto End = Opt.Commands.begin() + NumCommands;
|
|
|
|
auto I = std::find_if(Opt.Commands.begin(), End, [&](BaseCommand *Base) {
|
2017-07-28 03:22:43 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base))
|
|
|
|
return Sec->Name == Name;
|
2017-07-04 21:10:37 +08:00
|
|
|
return false;
|
|
|
|
});
|
2017-07-06 01:50:43 +08:00
|
|
|
if (I == End) {
|
2017-04-27 06:30:15 +08:00
|
|
|
Factory.addInputSec(S, Name);
|
2017-07-28 05:42:42 +08:00
|
|
|
assert(S->getOutputSection()->SectionIndex == INT_MAX);
|
2017-04-29 23:44:03 +08:00
|
|
|
} else {
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *Sec = cast<OutputSection>(*I);
|
|
|
|
Factory.addInputSec(S, Name, Sec);
|
|
|
|
unsigned Index = std::distance(Opt.Commands.begin(), I);
|
|
|
|
assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index);
|
|
|
|
Sec->SectionIndex = Index;
|
2017-04-29 23:44:03 +08:00
|
|
|
}
|
2017-04-27 06:30:15 +08:00
|
|
|
}
|
2016-07-20 22:43:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 11:00:27 +08:00
|
|
|
uint64_t LinkerScript::advance(uint64_t Size, unsigned Align) {
|
2017-07-07 17:11:27 +08:00
|
|
|
bool IsTbss = (CurAddressState->OutSec->Flags & SHF_TLS) &&
|
|
|
|
CurAddressState->OutSec->Type == SHT_NOBITS;
|
|
|
|
uint64_t Start = IsTbss ? Dot + CurAddressState->ThreadBssOffset : Dot;
|
2017-05-04 11:00:27 +08:00
|
|
|
Start = alignTo(Start, Align);
|
|
|
|
uint64_t End = Start + Size;
|
|
|
|
|
|
|
|
if (IsTbss)
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->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) {
|
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-07-07 17:11:27 +08:00
|
|
|
S->OutSecOff = Pos - S->getSize() - CurAddressState->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) }
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->OutSec->Size = Pos - CurAddressState->OutSec->Addr;
|
2016-09-16 23:10:23 +08:00
|
|
|
|
2017-01-24 10:34:00 +08:00
|
|
|
// If there is a memory region associated with this input section, then
|
|
|
|
// place the section in that region and update the region index.
|
2017-07-07 17:11:27 +08:00
|
|
|
if (CurAddressState->MemRegion) {
|
|
|
|
uint64_t &CurOffset =
|
|
|
|
CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
|
2017-07-25 16:29:29 +08:00
|
|
|
CurOffset += Pos - Before;
|
2017-07-07 17:11:27 +08:00
|
|
|
uint64_t CurSize = CurOffset - CurAddressState->MemRegion->Origin;
|
|
|
|
if (CurSize > CurAddressState->MemRegion->Length) {
|
|
|
|
uint64_t OverflowAmt = CurSize - CurAddressState->MemRegion->Length;
|
|
|
|
error("section '" + CurAddressState->OutSec->Name +
|
|
|
|
"' will not fit in region '" + CurAddressState->MemRegion->Name +
|
|
|
|
"': overflowed by " + Twine(OverflowAmt) + " bytes");
|
2017-01-24 10:34:00 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::switchTo(OutputSection *Sec) {
|
2017-07-07 17:11:27 +08:00
|
|
|
if (CurAddressState->OutSec == Sec)
|
2016-09-16 23:10:23 +08:00
|
|
|
return;
|
2016-08-11 15:56:43 +08:00
|
|
|
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->OutSec = Sec;
|
|
|
|
CurAddressState->OutSec->Addr =
|
|
|
|
advance(0, CurAddressState->OutSec->Alignment);
|
2016-10-06 17:39:28 +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
|
2017-07-07 17:11:27 +08:00
|
|
|
if (CurAddressState->LMAOffset)
|
|
|
|
CurAddressState->OutSec->LMAOffset = CurAddressState->LMAOffset();
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
2016-09-01 17:55:57 +08:00
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::process(BaseCommand &Base) {
|
2017-04-05 11:18:46 +08:00
|
|
|
// This handles the assignments to symbol or to the dot.
|
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) {
|
|
|
|
assignSymbol(Cmd, true);
|
2016-09-16 23:10:23 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-09-27 03:22:50 +08:00
|
|
|
|
|
|
|
// Handle BYTE(), SHORT(), LONG(), or QUAD().
|
2017-04-05 11:18:46 +08:00
|
|
|
if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) {
|
2017-07-07 17:11:27 +08:00
|
|
|
Cmd->Offset = Dot - CurAddressState->OutSec->Addr;
|
2017-04-05 11:18:46 +08:00
|
|
|
Dot += Cmd->Size;
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr;
|
2016-09-27 03:22:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-05 11:18:46 +08:00
|
|
|
// Handle ASSERT().
|
|
|
|
if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) {
|
|
|
|
Cmd->Expression();
|
2016-11-23 02:01:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-05 11:18:46 +08:00
|
|
|
// Handle a single input section description command.
|
|
|
|
// It calculates and assigns the offsets for each section and also
|
2016-09-27 03:22:50 +08:00
|
|
|
// updates the output section size.
|
2017-04-05 11:18:46 +08:00
|
|
|
auto &Cmd = cast<InputSectionDescription>(Base);
|
2017-05-31 04:24:52 +08:00
|
|
|
for (InputSection *Sec : Cmd.Sections) {
|
2016-11-30 00:05:27 +08:00
|
|
|
// We tentatively added all synthetic sections at the beginning and removed
|
|
|
|
// empty ones afterwards (because there is no way to know whether they were
|
|
|
|
// going be empty or not other than actually running linker scripts.)
|
|
|
|
// We need to ignore remains of empty sections.
|
2017-04-05 11:18:46 +08:00
|
|
|
if (auto *S = dyn_cast<SyntheticSection>(Sec))
|
|
|
|
if (S->empty())
|
2016-11-30 00:05:27 +08:00
|
|
|
continue;
|
|
|
|
|
2017-04-05 11:18:46 +08:00
|
|
|
if (!Sec->Live)
|
2017-02-21 23:46:43 +08:00
|
|
|
continue;
|
2017-07-07 17:11:27 +08:00
|
|
|
assert(CurAddressState->OutSec == Sec->getParent());
|
2017-05-31 04:24:52 +08:00
|
|
|
output(Sec);
|
2016-08-11 15:56:43 +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()) {
|
|
|
|
auto It = Opt.MemoryRegions.find(Sec->MemoryRegionName);
|
2017-01-24 10:34:00 +08:00
|
|
|
if (It != Opt.MemoryRegions.end())
|
|
|
|
return &It->second;
|
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-04-05 11:19:24 +08:00
|
|
|
if (Opt.MemoryRegions.empty())
|
2017-01-24 10:34:00 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// See if a region can be found by matching section flags.
|
2017-04-05 11:18:46 +08:00
|
|
|
for (auto &Pair : Opt.MemoryRegions) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-07-28 03:22:43 +08:00
|
|
|
if (Sec->LMAExpr) {
|
2017-03-14 18:00:19 +08:00
|
|
|
uint64_t D = Dot;
|
2017-07-28 03:22:43 +08:00
|
|
|
CurAddressState->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; };
|
2017-03-14 16:57:09 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
CurAddressState->MemRegion = Sec->MemRegion;
|
2017-07-07 17:11:27 +08:00
|
|
|
if (CurAddressState->MemRegion)
|
|
|
|
Dot = CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
|
2017-01-24 10:34:00 +08:00
|
|
|
switchTo(Sec);
|
2016-11-21 10:11:05 +08:00
|
|
|
|
2017-05-08 18:18:12 +08:00
|
|
|
// We do not support custom layout for compressed debug sectons.
|
|
|
|
// At this point we already know their size and have compressed content.
|
2017-07-07 17:11:27 +08:00
|
|
|
if (CurAddressState->OutSec->Flags & SHF_COMPRESSED)
|
2017-05-08 18:18:12 +08:00
|
|
|
return;
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
for (BaseCommand *C : Sec->Commands)
|
2017-04-29 23:44:03 +08:00
|
|
|
process(*C);
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
void LinkerScript::removeEmptyCommands() {
|
2016-09-20 21:12:07 +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.
|
2017-07-28 03:22:43 +08:00
|
|
|
auto Pos = std::remove_if(Opt.Commands.begin(), Opt.Commands.end(),
|
|
|
|
[&](BaseCommand *Base) {
|
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base))
|
|
|
|
return !Sec->Live;
|
|
|
|
return false;
|
|
|
|
});
|
2016-09-20 21:12:07 +08:00
|
|
|
Opt.Commands.erase(Pos, Opt.Commands.end());
|
2016-11-14 22:23:35 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
static bool isAllSectionDescription(const OutputSection &Cmd) {
|
2017-04-05 11:20:42 +08:00
|
|
|
for (BaseCommand *Base : Cmd.Commands)
|
|
|
|
if (!isa<InputSectionDescription>(*Base))
|
2016-11-14 22:33:49 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-20 21:12:07 +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
|
|
|
|
// corresponding output section. The bfd linker seems to only create them if
|
|
|
|
// '.' is assigned to, but creating these section should not have any bad
|
|
|
|
// consequeces and gives us a section to put the symbol in.
|
2017-03-14 18:00:19 +08:00
|
|
|
uint64_t Flags = SHF_ALLOC;
|
2017-05-06 05:34:26 +08:00
|
|
|
|
|
|
|
for (int I = 0, E = Opt.Commands.size(); I != E; ++I) {
|
2017-07-28 03:22:43 +08:00
|
|
|
auto *Sec = dyn_cast<OutputSection>(Opt.Commands[I]);
|
|
|
|
if (!Sec)
|
2016-09-22 22:40:50 +08:00
|
|
|
continue;
|
2017-07-28 03:22:43 +08:00
|
|
|
if (Sec->Live) {
|
2017-02-04 06:27:05 +08:00
|
|
|
Flags = Sec->Flags;
|
2016-09-22 22:40:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
if (isAllSectionDescription(*Sec))
|
2016-11-14 22:33:49 +08:00
|
|
|
continue;
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
Sec->Live = true;
|
|
|
|
Sec->SectionIndex = I;
|
|
|
|
Sec->Flags = Flags;
|
2016-09-22 22:40:50 +08:00
|
|
|
}
|
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-04-07 05:40:22 +08:00
|
|
|
for (BaseCommand *Base : Opt.Commands) {
|
2017-07-28 03:22:43 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base)) {
|
|
|
|
Sec->MemRegion = findMemoryRegion(Sec);
|
2017-04-07 05:40:22 +08:00
|
|
|
// Handle align (e.g. ".foo : ALIGN(16) { ... }").
|
2017-07-28 03:22:43 +08:00
|
|
|
if (Sec->AlignExpr)
|
|
|
|
Sec->updateAlignment(Sec->AlignExpr().getValue());
|
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;
|
|
|
|
auto FirstPtLoad =
|
|
|
|
std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
|
|
|
|
[](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
|
|
|
|
if (FirstPtLoad != Opt.PhdrsCommands.end())
|
|
|
|
DefPhdrs.push_back(FirstPtLoad->Name);
|
|
|
|
|
|
|
|
// Walk the commands and propagate the program headers to commands that don't
|
|
|
|
// explicitly specify them.
|
2017-04-05 11:20:42 +08:00
|
|
|
for (BaseCommand *Base : Opt.Commands) {
|
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-11-14 22:33:49 +08:00
|
|
|
|
|
|
|
removeEmptyCommands();
|
2016-09-22 22:40:50 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
uint64_t HeaderSize = getHeaderSize();
|
2017-08-24 02:44:34 +08:00
|
|
|
// When linker script with SECTIONS is being used, don't output headers
|
|
|
|
// unless there's a space for them.
|
|
|
|
uint64_t Base = Opt.HasSections ? alignDown(Min, Config->MaxPageSize) : 0;
|
|
|
|
if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) {
|
|
|
|
Min = Opt.HasSections ? Base
|
|
|
|
: 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
|
|
|
}
|
|
|
|
|
|
|
|
OutputSection *ActualFirst = nullptr;
|
2017-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections) {
|
2017-05-05 03:34:17 +08:00
|
|
|
if (Sec->FirstInPtLoad == Out::ElfHeader) {
|
|
|
|
ActualFirst = Sec;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ActualFirst) {
|
2017-07-28 03:22:43 +08:00
|
|
|
for (OutputSection *Sec : OutputSections)
|
2017-05-05 03:34:17 +08:00
|
|
|
if (Sec->FirstInPtLoad == Out::ElfHeader)
|
|
|
|
Sec->FirstInPtLoad = ActualFirst;
|
|
|
|
FirstPTLoad->First = ActualFirst;
|
|
|
|
} else {
|
2017-07-27 15:46:50 +08:00
|
|
|
Phdrs.erase(It);
|
2017-05-05 03:34:17 +08:00
|
|
|
}
|
|
|
|
|
2017-07-04 00:05:51 +08:00
|
|
|
auto PhdrI = llvm::find_if(
|
2017-07-27 15:46:50 +08:00
|
|
|
Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_PHDR; });
|
2017-05-05 03:34:17 +08:00
|
|
|
if (PhdrI != Phdrs.end())
|
|
|
|
Phdrs.erase(PhdrI);
|
|
|
|
}
|
|
|
|
|
2017-07-07 17:11:27 +08:00
|
|
|
LinkerScript::AddressState::AddressState(const ScriptConfiguration &Opt) {
|
|
|
|
for (auto &MRI : Opt.MemoryRegions) {
|
|
|
|
const MemoryRegion *MR = &MRI.second;
|
|
|
|
MemRegionOffset[MR] = MR->Origin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 17:12:54 +08:00
|
|
|
void LinkerScript::assignAddresses() {
|
2016-04-19 05:00:40 +08:00
|
|
|
// Assign addresses as instructed by linker script SECTIONS sub-commands.
|
2016-09-30 08:16:11 +08:00
|
|
|
Dot = 0;
|
2017-07-07 17:11:27 +08:00
|
|
|
auto State = make_unique<AddressState>(Opt);
|
2017-07-11 17:28:27 +08:00
|
|
|
// CurAddressState 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.
|
2017-07-07 17:11:27 +08:00
|
|
|
CurAddressState = State.get();
|
2017-03-17 21:05:04 +08:00
|
|
|
ErrorOnMissingSection = true;
|
2017-02-07 06:21:46 +08:00
|
|
|
switchTo(Aether);
|
|
|
|
|
2017-04-05 11:20:42 +08:00
|
|
|
for (BaseCommand *Base : Opt.Commands) {
|
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
|
2017-04-05 11:20:22 +08:00
|
|
|
assignSymbol(Cmd, false);
|
2016-07-12 14:39:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-16 18:10:32 +08:00
|
|
|
|
2017-04-05 11:20:42 +08:00
|
|
|
if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
|
2017-03-11 00:04:26 +08:00
|
|
|
Cmd->Expression();
|
2016-08-04 17:29:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
assignOffsets(cast<OutputSection>(Base));
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
2017-07-11 17:28:27 +08:00
|
|
|
CurAddressState = 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.
|
2016-07-19 17:25:43 +08:00
|
|
|
for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
|
2017-07-27 15:46:50 +08:00
|
|
|
PhdrEntry *Phdr =
|
|
|
|
make<PhdrEntry>(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
|
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);
|
2016-10-19 23:04:49 +08:00
|
|
|
if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
|
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-03-22 07:02:51 +08:00
|
|
|
bool LinkerScript::ignoreInterpSection() {
|
2016-08-16 14:40:58 +08:00
|
|
|
// Ignore .interp section in case we have PHDRS specification
|
|
|
|
// and PT_INTERP isn't listed.
|
2017-04-05 13:06:17 +08:00
|
|
|
if (Opt.PhdrsCommands.empty())
|
|
|
|
return false;
|
|
|
|
for (PhdrsCommand &Cmd : Opt.PhdrsCommands)
|
|
|
|
if (Cmd.Type == PT_INTERP)
|
|
|
|
return false;
|
|
|
|
return true;
|
2016-08-16 14:40:58 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 07:02:51 +08:00
|
|
|
ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
|
2017-08-17 16:47:21 +08:00
|
|
|
if (S == ".") {
|
|
|
|
if (CurAddressState)
|
|
|
|
return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr,
|
|
|
|
Loc};
|
|
|
|
error(Loc + ": unable to get location counter value");
|
|
|
|
return 0;
|
|
|
|
}
|
2017-07-27 02:42:48 +08:00
|
|
|
if (SymbolBody *B = Symtab->find(S)) {
|
2017-03-17 21:05:04 +08:00
|
|
|
if (auto *D = dyn_cast<DefinedRegular>(B))
|
2017-06-07 16:54:43 +08:00
|
|
|
return {D->Section, D->Value, Loc};
|
2017-03-23 11:52:34 +08:00
|
|
|
if (auto *C = dyn_cast<DefinedCommon>(B))
|
2017-06-07 16:54:43 +08:00
|
|
|
return {InX::Common, C->Offset, Loc};
|
2017-03-17 21:05:04 +08:00
|
|
|
}
|
2016-12-22 21:13:12 +08:00
|
|
|
error(Loc + ": symbol not found: " + S);
|
2016-09-08 16:19:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-27 02:42:48 +08:00
|
|
|
bool LinkerScript::isDefined(StringRef S) { return Symtab->find(S) != nullptr; }
|
2016-09-23 21:17:23 +08:00
|
|
|
|
2017-06-19 23:28:58 +08:00
|
|
|
static const size_t NoPhdr = -1;
|
|
|
|
|
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;
|
|
|
|
for (StringRef PhdrName : Cmd->Phdrs) {
|
|
|
|
size_t Index = getPhdrIndex(Cmd->Location, PhdrName);
|
|
|
|
if (Index != NoPhdr)
|
|
|
|
Ret.push_back(Index);
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
2017-07-06 06:30:04 +08:00
|
|
|
return Ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2017-06-19 23:28:58 +08:00
|
|
|
// Returns the index of the segment named PhdrName if found otherwise
|
|
|
|
// NoPhdr. When not found, if PhdrName is not the special case value 'NONE'
|
|
|
|
// (which can be used to explicitly specify that a section isn't assigned to a
|
|
|
|
// segment) then error.
|
2017-03-22 07:02:51 +08:00
|
|
|
size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
|
2016-07-26 08:27:36 +08:00
|
|
|
size_t I = 0;
|
|
|
|
for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
|
|
|
|
if (Cmd.Name == PhdrName)
|
|
|
|
return I;
|
|
|
|
++I;
|
|
|
|
}
|
2017-06-19 23:28:58 +08:00
|
|
|
if (PhdrName != "NONE")
|
|
|
|
error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
|
|
|
|
return NoPhdr;
|
2016-07-26 08:27:36 +08:00
|
|
|
}
|