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-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.
|
|
|
|
if (Alignment == 1)
|
|
|
|
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-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: " +
|
2017-10-11 10:45:54 +08:00
|
|
|
Ctx->OutSec->Name);
|
2017-02-18 00:26:13 +08:00
|
|
|
Dot = Val;
|
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)
|
2017-10-11 10:45:54 +08:00
|
|
|
Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr;
|
2017-02-18 00:26:13 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 10:28:28 +08:00
|
|
|
// This function is called from processSectionCommands,
|
|
|
|
// while we are fixing the output section layout.
|
2017-10-11 09:03:37 +08:00
|
|
|
void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
|
|
|
|
if (Cmd->Name == ".")
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If a symbol was in PROVIDE(), we need to define it only when
|
|
|
|
// it is a referenced undefined symbol.
|
|
|
|
SymbolBody *B = Symtab->find(Cmd->Name);
|
|
|
|
if (Cmd->Provide && (!B || B->isDefined()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Define a symbol.
|
|
|
|
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);
|
|
|
|
Sym->Binding = STB_GLOBAL;
|
|
|
|
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();
|
|
|
|
|
|
|
|
replaceBody<DefinedRegular>(Sym, nullptr, Cmd->Name, /*IsLocal=*/false,
|
|
|
|
Visibility, STT_NOTYPE, SymValue, 0, Sec);
|
|
|
|
Cmd->Sym = cast<DefinedRegular>(Sym->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-09 00:22:43 +08:00
|
|
|
static std::string filename(InputFile *File) {
|
|
|
|
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-10-11 09:26:22 +08:00
|
|
|
std::string Filename = filename(S->File);
|
|
|
|
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-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 void sortBySymbolOrder(InputSection **Begin, InputSection **End) {
|
|
|
|
if (Config->SymbolOrderingFile.empty())
|
|
|
|
return;
|
2017-09-19 17:20:54 +08:00
|
|
|
static llvm::DenseMap<SectionBase *, int> Order = buildSectionOrder();
|
2017-08-04 18:25:29 +08:00
|
|
|
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-09-09 00:22:43 +08:00
|
|
|
std::string Filename = filename(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-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-09-09 00:22:43 +08:00
|
|
|
if (S == InX::ShStrTab || 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-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *Base : OutCmd.SectionCommands) {
|
2017-04-05 11:20:42 +08:00
|
|
|
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-10-11 10:28:28 +08:00
|
|
|
void LinkerScript::processSectionCommands(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-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-11 10:45:54 +08:00
|
|
|
Ctx = make_unique<AddressState>();
|
|
|
|
Ctx->OutSec = Aether;
|
2017-03-09 06:36:28 +08:00
|
|
|
|
2017-10-11 09:50:56 +08:00
|
|
|
for (size_t I = 0; I < SectionCommands.size(); ++I) {
|
2016-11-21 10:11:05 +08:00
|
|
|
// Handle symbol assignments outside of any output section.
|
2017-10-11 09:50:56 +08:00
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(SectionCommands[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-10-11 09:50:56 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(SectionCommands[I])) {
|
2017-07-28 03:22:43 +08:00
|
|
|
std::vector<InputSectionBase *> V = createInputSectionList(*Sec);
|
2016-09-13 00:05:16 +08:00
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// The output section name `/DISCARD/' is special.
|
|
|
|
// Any input section assigned to it is discarded.
|
2017-07-28 03:22:43 +08:00
|
|
|
if (Sec->Name == "/DISCARD/") {
|
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
|
|
|
//
|
2017-10-11 09:50:56 +08:00
|
|
|
// Because we'll iterate over SectionCommands 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-10-11 09:50:56 +08:00
|
|
|
SectionCommands.erase(SectionCommands.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-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-05-08 18:18:12 +08:00
|
|
|
for (InputSectionBase *S : V)
|
2017-10-07 08:43:31 +08:00
|
|
|
Sec->addSection(cast<InputSection>(S));
|
2017-10-07 07:34:43 +08:00
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
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-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-05-05 03:34:17 +08:00
|
|
|
void LinkerScript::fabricateDefaultCommands() {
|
2017-04-19 20:46:32 +08:00
|
|
|
// Define start address
|
2017-09-26 01:40:21 +08:00
|
|
|
uint64_t StartAddr = UINT64_MAX;
|
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-09-26 01:19:17 +08:00
|
|
|
auto Expr = [=] {
|
2017-10-10 18:09:35 +08:00
|
|
|
return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize());
|
2017-09-26 01:19:17 +08:00
|
|
|
};
|
2017-10-11 09:50:56 +08:00
|
|
|
SectionCommands.insert(SectionCommands.begin(),
|
|
|
|
make<SymbolAssignment>(".", Expr, ""));
|
2017-04-19 20:46:32 +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;
|
|
|
|
}
|
|
|
|
|
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-10-11 09:50:56 +08:00
|
|
|
unsigned End = SectionCommands.size();
|
2017-10-07 07:06:55 +08:00
|
|
|
|
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;
|
2017-10-07 07:06:55 +08:00
|
|
|
|
2017-04-27 06:30:15 +08:00
|
|
|
StringRef Name = getOutputSectionName(S->Name);
|
2017-09-25 17:41:32 +08:00
|
|
|
log(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));
|
2017-10-07 07:34:43 +08:00
|
|
|
continue;
|
2017-04-29 23:44:03 +08:00
|
|
|
}
|
2017-10-07 07:34:43 +08:00
|
|
|
|
|
|
|
if (OutputSection *OS = Factory.addInputSec(S, Name))
|
2017-10-11 09:50:56 +08:00
|
|
|
SectionCommands.push_back(OS);
|
2017-10-07 07:34:43 +08:00
|
|
|
assert(S->getOutputSection()->SectionIndex == INT_MAX);
|
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-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-05-04 11:00:27 +08:00
|
|
|
Start = alignTo(Start, Align);
|
|
|
|
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) {
|
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) }
|
2017-10-11 10:45:54 +08:00
|
|
|
Ctx->OutSec->Size = Pos - Ctx->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-10-11 10:45:54 +08:00
|
|
|
if (Ctx->MemRegion) {
|
|
|
|
uint64_t &CurOffset = Ctx->MemRegionOffset[Ctx->MemRegion];
|
2017-07-25 16:29:29 +08:00
|
|
|
CurOffset += Pos - Before;
|
2017-10-11 10:45:54 +08:00
|
|
|
uint64_t CurSize = CurOffset - Ctx->MemRegion->Origin;
|
|
|
|
if (CurSize > Ctx->MemRegion->Length) {
|
|
|
|
uint64_t OverflowAmt = CurSize - Ctx->MemRegion->Length;
|
|
|
|
error("section '" + Ctx->OutSec->Name + "' will not fit in region '" +
|
|
|
|
Ctx->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-10-11 10:45:54 +08:00
|
|
|
if (Ctx->OutSec == Sec)
|
2016-09-16 23:10:23 +08:00
|
|
|
return;
|
2016-08-11 15:56:43 +08:00
|
|
|
|
2017-10-11 10:45:54 +08:00
|
|
|
Ctx->OutSec = Sec;
|
|
|
|
Ctx->OutSec->Addr = advance(0, Ctx->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-10-11 10:45:54 +08:00
|
|
|
if (Ctx->LMAOffset)
|
|
|
|
Ctx->OutSec->LMAOffset = Ctx->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-10-11 10:45:54 +08:00
|
|
|
Cmd->Offset = Dot - Ctx->OutSec->Addr;
|
2017-04-05 11:18:46 +08:00
|
|
|
Dot += Cmd->Size;
|
2017-10-11 10:45:54 +08:00
|
|
|
Ctx->OutSec->Size = Dot - Ctx->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-10-11 10:45:54 +08:00
|
|
|
assert(Ctx->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()) {
|
2017-10-11 09:19:33 +08:00
|
|
|
auto It = MemoryRegions.find(Sec->MemoryRegionName);
|
|
|
|
if (It != MemoryRegions.end())
|
2017-09-08 16:23:15 +08:00
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (Ctx->MemRegion)
|
|
|
|
Dot = Ctx->MemRegionOffset[Ctx->MemRegion];
|
2017-09-06 17:35:09 +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-10-11 10:45:54 +08:00
|
|
|
Ctx->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; };
|
2017-03-14 16:57:09 +08:00
|
|
|
}
|
|
|
|
|
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-10-11 10:45:54 +08:00
|
|
|
if (Ctx->OutSec->Flags & SHF_COMPRESSED)
|
2017-05-08 18:18:12 +08:00
|
|
|
return;
|
|
|
|
|
2017-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *C : Sec->SectionCommands)
|
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-10-11 09:50:56 +08:00
|
|
|
llvm::erase_if(SectionCommands, [&](BaseCommand *Base) {
|
2017-08-28 17:28:15 +08:00
|
|
|
if (auto *Sec = dyn_cast<OutputSection>(Base))
|
|
|
|
return !Sec->Live;
|
|
|
|
return false;
|
|
|
|
});
|
2016-11-14 22:23:35 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
static bool isAllSectionDescription(const OutputSection &Cmd) {
|
2017-10-11 09:50:56 +08:00
|
|
|
for (BaseCommand *Base : Cmd.SectionCommands)
|
2017-04-05 11:20:42 +08:00
|
|
|
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
|
|
|
|
2017-10-11 09:50:56 +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;
|
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->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-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)) {
|
2017-10-02 17:11:13 +08:00
|
|
|
if (!Sec->Live)
|
|
|
|
continue;
|
2017-07-28 03:22:43 +08:00
|
|
|
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)
|
2017-10-07 08:58:34 +08:00
|
|
|
Sec->Alignment =
|
|
|
|
std::max<uint32_t>(Sec->Alignment, 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 =
|
2017-10-11 09:19:33 +08:00
|
|
|
std::find_if(PhdrsCommands.begin(), PhdrsCommands.end(),
|
2016-11-14 23:39:38 +08:00
|
|
|
[](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
|
|
|
}
|
|
|
|
|
2017-09-07 18:53:07 +08:00
|
|
|
static OutputSection *findFirstSection(PhdrEntry *Load) {
|
|
|
|
for (OutputSection *Sec : OutputSections)
|
|
|
|
if (Sec->PtLoad == Load)
|
|
|
|
return Sec;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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.
|
2017-10-11 09:34:51 +08:00
|
|
|
uint64_t Base = HasSectionsCommand ? alignDown(Min, Config->MaxPageSize) : 0;
|
2017-08-24 02:44:34 +08:00
|
|
|
if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) {
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2017-09-08 16:23:15 +08:00
|
|
|
const MemoryRegion *MR = MRI.second;
|
2017-07-07 17:11:27 +08:00
|
|
|
MemRegionOffset[MR] = MR->Origin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 18:09:35 +08:00
|
|
|
// Assign addresses as instructed by linker script SECTIONS sub-commands.
|
2017-07-05 17:12:54 +08:00
|
|
|
void LinkerScript::assignAddresses() {
|
2017-10-10 18:09:35 +08:00
|
|
|
// By default linker scripts use an initial value of 0 for '.', but prefer
|
|
|
|
// -image-base if set.
|
|
|
|
Dot = Config->ImageBase ? *Config->ImageBase : 0;
|
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
|
2017-07-11 17:28:27 +08:00
|
|
|
// 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-10-11 10:45:54 +08:00
|
|
|
Ctx = make_unique<AddressState>();
|
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)) {
|
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-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-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 == ".") {
|
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-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-10-11 08:06:27 +08:00
|
|
|
return {D->Section, false, D->Value, Loc};
|
2017-03-23 11:52:34 +08:00
|
|
|
if (auto *C = dyn_cast<DefinedCommon>(B))
|
2017-10-11 08:06:27 +08:00
|
|
|
return {C->Section, false, 0, 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-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
|
|
|
}
|