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"
|
|
|
|
#include "Driver.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"
|
2017-02-14 12:47:05 +08:00
|
|
|
#include "ScriptLexer.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"
|
2016-07-01 18:27:36 +08:00
|
|
|
#include "Target.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"
|
2016-11-06 06:37:59 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2016-04-20 02:58:11 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2016-04-16 18:10:32 +08:00
|
|
|
#include "llvm/Support/ELF.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-10-01 01:23:26 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-11-05 09:00:56 +08:00
|
|
|
#include "llvm/Support/MathExtras.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 <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
|
|
|
#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-17 21:05:04 +08:00
|
|
|
uint64_t ExprValue::getValue() const {
|
|
|
|
if (Sec)
|
|
|
|
return Sec->getOffset(Val) + Sec->getOutputSection()->Addr;
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:55:36 +08:00
|
|
|
uint64_t ExprValue::getSecAddr() const {
|
|
|
|
if (Sec)
|
|
|
|
return Sec->getOffset(0) + Sec->getOutputSection()->Addr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some operations only support one non absolute value. Move the
|
|
|
|
// absolute one to the right hand side for convenience.
|
|
|
|
static void moveAbsRight(ExprValue &A, ExprValue &B) {
|
2017-03-17 21:45:36 +08:00
|
|
|
if (A.isAbsolute())
|
|
|
|
std::swap(A, B);
|
2017-03-17 22:51:07 +08:00
|
|
|
if (!B.isAbsolute())
|
|
|
|
error("At least one side of the expression must be absolute");
|
2017-03-17 22:55:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ExprValue add(ExprValue A, ExprValue B) {
|
|
|
|
moveAbsRight(A, B);
|
2017-03-17 21:05:04 +08:00
|
|
|
return {A.Sec, A.ForceAbsolute, A.Val + B.getValue()};
|
|
|
|
}
|
|
|
|
static ExprValue sub(ExprValue A, ExprValue B) {
|
|
|
|
return {A.Sec, A.Val - B.getValue()};
|
|
|
|
}
|
|
|
|
static ExprValue mul(ExprValue A, ExprValue B) {
|
|
|
|
return A.getValue() * B.getValue();
|
|
|
|
}
|
|
|
|
static ExprValue div(ExprValue A, ExprValue B) {
|
|
|
|
if (uint64_t BV = B.getValue())
|
|
|
|
return A.getValue() / BV;
|
|
|
|
error("division by zero");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static ExprValue leftShift(ExprValue A, ExprValue B) {
|
|
|
|
return A.getValue() << B.getValue();
|
|
|
|
}
|
|
|
|
static ExprValue rightShift(ExprValue A, ExprValue B) {
|
|
|
|
return A.getValue() >> B.getValue();
|
|
|
|
}
|
|
|
|
static ExprValue lessThan(ExprValue A, ExprValue B) {
|
|
|
|
return A.getValue() < B.getValue();
|
|
|
|
}
|
|
|
|
static ExprValue greaterThan(ExprValue A, ExprValue B) {
|
|
|
|
return A.getValue() > B.getValue();
|
|
|
|
}
|
|
|
|
static ExprValue greaterThanOrEqual(ExprValue A, ExprValue B) {
|
|
|
|
return A.getValue() >= B.getValue();
|
|
|
|
}
|
|
|
|
static ExprValue lessThanOrEqual(ExprValue A, ExprValue B) {
|
|
|
|
return A.getValue() <= B.getValue();
|
|
|
|
}
|
|
|
|
static ExprValue equal(ExprValue A, ExprValue B) {
|
|
|
|
return A.getValue() == B.getValue();
|
|
|
|
}
|
|
|
|
static ExprValue notEqual(ExprValue A, ExprValue B) {
|
|
|
|
return A.getValue() != B.getValue();
|
|
|
|
}
|
|
|
|
static ExprValue bitAnd(ExprValue A, ExprValue B) {
|
2017-03-17 22:55:36 +08:00
|
|
|
moveAbsRight(A, B);
|
|
|
|
return {A.Sec, A.ForceAbsolute,
|
|
|
|
(A.getValue() & B.getValue()) - A.getSecAddr()};
|
2017-03-17 21:05:04 +08:00
|
|
|
}
|
|
|
|
static ExprValue bitOr(ExprValue A, ExprValue B) {
|
2017-03-17 22:55:36 +08:00
|
|
|
moveAbsRight(A, B);
|
|
|
|
return {A.Sec, A.ForceAbsolute,
|
|
|
|
(A.getValue() | B.getValue()) - A.getSecAddr()};
|
2017-03-17 21:05:04 +08:00
|
|
|
}
|
|
|
|
static ExprValue bitNot(ExprValue A) { return ~A.getValue(); }
|
|
|
|
static ExprValue minus(ExprValue A) { return -A.getValue(); }
|
|
|
|
|
2016-09-08 16:19:13 +08:00
|
|
|
LinkerScriptBase *elf::ScriptBase;
|
2016-04-21 04:13:41 +08:00
|
|
|
ScriptConfiguration *elf::ScriptConfig;
|
2016-02-12 05:17:59 +08:00
|
|
|
|
2017-01-10 02:36:57 +08:00
|
|
|
template <class ELFT> 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-02-22 06:32:51 +08:00
|
|
|
std::tie(Sym, std::ignore) = Symtab<ELFT>::X->insert(
|
|
|
|
Cmd->Name, /*Type*/ 0, Visibility, /*CanOmitFromDynSym*/ false,
|
|
|
|
/*File*/ nullptr);
|
|
|
|
Sym->Binding = STB_GLOBAL;
|
2017-03-17 21:05:04 +08:00
|
|
|
ExprValue Value = Cmd->Expression();
|
|
|
|
SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
|
2017-03-01 03:29:55 +08:00
|
|
|
replaceBody<DefinedRegular>(Sym, Cmd->Name, /*IsLocal=*/false, Visibility,
|
2017-03-09 06:36:28 +08:00
|
|
|
STT_NOTYPE, 0, 0, Sec, nullptr);
|
2017-01-10 02:36:57 +08:00
|
|
|
return Sym->body();
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
|
2016-11-26 02:51:54 +08:00
|
|
|
static bool isUnderSysroot(StringRef Path) {
|
|
|
|
if (Config->Sysroot == "")
|
|
|
|
return false;
|
|
|
|
for (; !Path.empty(); Path = sys::path::parent_path(Path))
|
|
|
|
if (sys::fs::equivalent(Config->Sysroot, Path))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-14 18:15:53 +08:00
|
|
|
OutputSection *LinkerScriptBase::getOutputSection(const Twine &Loc,
|
|
|
|
StringRef Name) {
|
|
|
|
static OutputSection FakeSec("", 0, 0);
|
|
|
|
|
|
|
|
for (OutputSection *Sec : *OutputSections)
|
|
|
|
if (Sec->Name == Name)
|
|
|
|
return Sec;
|
|
|
|
|
2017-03-17 21:05:04 +08:00
|
|
|
if (ErrorOnMissingSection)
|
|
|
|
error(Loc + ": undefined section " + Name);
|
2017-03-14 18:15:53 +08:00
|
|
|
return &FakeSec;
|
|
|
|
}
|
|
|
|
|
2017-03-14 18:24:47 +08:00
|
|
|
// This function is essentially the same as getOutputSection(Name)->Size,
|
|
|
|
// but it won't print out an error message if a given section is not found.
|
|
|
|
//
|
|
|
|
// Linker script does not create an output section if its content is empty.
|
|
|
|
// We want to allow SIZEOF(.foo) where .foo is a section which happened to
|
|
|
|
// be empty. That is why this function is different from getOutputSection().
|
|
|
|
uint64_t LinkerScriptBase::getOutputSectionSize(StringRef Name) {
|
|
|
|
for (OutputSection *Sec : *OutputSections)
|
|
|
|
if (Sec->Name == Name)
|
|
|
|
return Sec->Size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::setDot(Expr E, const Twine &Loc, bool InSec) {
|
2017-03-17 21:05:04 +08:00
|
|
|
uint64_t Val = E().getValue();
|
2017-02-18 00:26:13 +08:00
|
|
|
if (Val < Dot) {
|
|
|
|
if (InSec)
|
2017-02-21 22:50:38 +08:00
|
|
|
error(Loc + ": unable to move location counter backward for: " +
|
|
|
|
CurOutSec->Name);
|
2017-02-18 00:26:13 +08:00
|
|
|
else
|
2017-02-21 22:50:38 +08:00
|
|
|
error(Loc + ": unable to move location counter backward");
|
2017-02-18 00:26:13 +08:00
|
|
|
}
|
|
|
|
Dot = Val;
|
|
|
|
// Update to location counter means update to section size.
|
|
|
|
if (InSec)
|
|
|
|
CurOutSec->Size = Dot - CurOutSec->Addr;
|
|
|
|
}
|
|
|
|
|
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-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::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;
|
|
|
|
if (Sym->Section->Flags & SHF_ALLOC)
|
|
|
|
Sym->Value = V.Val;
|
|
|
|
else
|
|
|
|
Sym->Value = V.getValue();
|
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-02-18 00:01:51 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void LinkerScript<ELFT>::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.
|
2016-08-12 07:22:52 +08:00
|
|
|
SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
|
2017-01-10 02:36:57 +08:00
|
|
|
if (Cmd->Provide && (!B || B->isDefined()))
|
|
|
|
return;
|
|
|
|
|
2017-03-09 06:36:28 +08:00
|
|
|
Cmd->Sym = addRegular<ELFT>(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OutputSectionCommand::classof(const BaseCommand *C) {
|
|
|
|
return C->Kind == OutputSectionKind;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-05 09:00:56 +08:00
|
|
|
template <class ELFT> LinkerScript<ELFT>::LinkerScript() = default;
|
|
|
|
template <class ELFT> LinkerScript<ELFT>::~LinkerScript() = default;
|
2016-08-12 09:24:53 +08:00
|
|
|
|
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-14 20:03:34 +08:00
|
|
|
bool LinkerScriptBase::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-02-23 10:32:18 +08:00
|
|
|
static bool comparePriority(InputSectionBase *A, InputSectionBase *B) {
|
2016-09-16 03:15:12 +08:00
|
|
|
return getPriority(A->Name) < getPriority(B->Name);
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:32:18 +08:00
|
|
|
static bool compareName(InputSectionBase *A, InputSectionBase *B) {
|
2016-09-08 22:06:08 +08:00
|
|
|
return A->Name < B->Name;
|
2016-08-05 06:27:00 +08:00
|
|
|
}
|
|
|
|
|
2017-02-23 10:32:18 +08:00
|
|
|
static bool compareAlignment(InputSectionBase *A, InputSectionBase *B) {
|
2016-08-05 06:27:00 +08:00
|
|
|
// ">" is not a mistake. Larger alignments are placed before smaller
|
|
|
|
// alignments in order to reduce the amount of padding necessary.
|
|
|
|
// This is compatible with GNU.
|
|
|
|
return A->Alignment > B->Alignment;
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
return compareAlignment;
|
|
|
|
case SortSectionPolicy::Name:
|
2016-09-09 04:47:52 +08:00
|
|
|
return compareName;
|
2016-09-17 04:21:55 +08:00
|
|
|
case SortSectionPolicy::Priority:
|
|
|
|
return comparePriority;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown sort policy");
|
|
|
|
}
|
2016-08-05 06:27:00 +08:00
|
|
|
}
|
2016-07-29 23:32:46 +08:00
|
|
|
|
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-02-23 10:32:18 +08:00
|
|
|
bool IsRW = llvm::any_of(Sections, [=](InputSectionBase *Sec2) {
|
2017-02-23 10:28:28 +08:00
|
|
|
auto *Sec = static_cast<InputSectionBase *>(Sec2);
|
2016-10-26 20:36:56 +08:00
|
|
|
return Sec->Flags & SHF_WRITE;
|
2016-08-12 17:07:57 +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-02-23 10:32:18 +08:00
|
|
|
static void sortSections(InputSectionBase **Begin, InputSectionBase **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
|
|
|
}
|
|
|
|
|
2016-09-16 23:10:23 +08:00
|
|
|
// Compute and remember which sections the InputSectionDescription matches.
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::computeInputSections(InputSectionDescription *I) {
|
2016-09-17 10:23:40 +08:00
|
|
|
// Collects all sections that satisfy constraints of I
|
|
|
|
// and attach them to I.
|
|
|
|
for (SectionPattern &Pat : I->SectionPatterns) {
|
2016-09-21 23:56:44 +08:00
|
|
|
size_t SizeBefore = I->Sections.size();
|
2016-11-06 06:37:59 +08:00
|
|
|
|
2017-02-27 10:32:08 +08:00
|
|
|
for (InputSectionBase *S : InputSections) {
|
2017-02-18 03:37:30 +08:00
|
|
|
if (S->Assigned)
|
2016-11-06 06:37:59 +08:00
|
|
|
continue;
|
2017-02-16 22:36:09 +08:00
|
|
|
// For -emit-relocs we have to ignore entries like
|
|
|
|
// .rela.dyn : { *(.rela.data) }
|
|
|
|
// which are common because they are in the default bfd script.
|
|
|
|
if (S->Type == SHT_REL || S->Type == SHT_RELA)
|
|
|
|
continue;
|
2016-11-06 06:37:59 +08:00
|
|
|
|
2016-11-21 10:10:12 +08:00
|
|
|
StringRef Filename = basename(S);
|
|
|
|
if (!I->FilePat.match(Filename) || Pat.ExcludedFilePat.match(Filename))
|
|
|
|
continue;
|
|
|
|
if (!Pat.SectionPat.match(S->Name))
|
|
|
|
continue;
|
|
|
|
I->Sections.push_back(S);
|
|
|
|
S->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-02-23 10:32:18 +08:00
|
|
|
InputSectionBase **Begin = I->Sections.data() + SizeBefore;
|
|
|
|
InputSectionBase **End = I->Sections.data() + I->Sections.size();
|
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
|
|
|
}
|
2016-09-14 22:32:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-15 23:42:44 +08:00
|
|
|
void LinkerScriptBase::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-03-15 23:42:44 +08:00
|
|
|
if (S == InX::ShStrTab)
|
2017-02-18 01:35:07 +08:00
|
|
|
error("discarding .shstrtab 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-03-14 20:03:34 +08:00
|
|
|
LinkerScriptBase::createInputSectionList(OutputSectionCommand &OutCmd) {
|
2017-02-23 10:28:28 +08:00
|
|
|
std::vector<InputSectionBase *> Ret;
|
2016-08-12 11:16:56 +08:00
|
|
|
|
2016-08-12 17:07:57 +08:00
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
|
2016-09-17 05:05:36 +08:00
|
|
|
auto *Cmd = dyn_cast<InputSectionDescription>(Base.get());
|
|
|
|
if (!Cmd)
|
2016-08-12 11:16:56 +08:00
|
|
|
continue;
|
2016-09-17 04:34:02 +08:00
|
|
|
computeInputSections(Cmd);
|
2017-02-23 10:32:18 +08:00
|
|
|
for (InputSectionBase *S : Cmd->Sections)
|
2017-02-23 10:28:28 +08:00
|
|
|
Ret.push_back(static_cast<InputSectionBase *>(S));
|
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
|
|
|
|
2016-09-16 23:30:47 +08:00
|
|
|
template <class ELFT>
|
2017-02-27 10:31:48 +08:00
|
|
|
void LinkerScript<ELFT>::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;
|
|
|
|
CurOutSec = Aether;
|
|
|
|
|
2016-09-17 05:05:36 +08:00
|
|
|
for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
|
|
|
|
auto Iter = Opt.Commands.begin() + I;
|
|
|
|
const std::unique_ptr<BaseCommand> &Base1 = *Iter;
|
2016-11-21 10:11:05 +08:00
|
|
|
|
|
|
|
// Handle symbol assignments outside of any output section.
|
2016-08-12 11:33:04 +08:00
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
|
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
|
|
|
|
2016-08-11 15:56:43 +08:00
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
|
2017-02-23 10:28:28 +08:00
|
|
|
std::vector<InputSectionBase *> V = createInputSectionList(*Cmd);
|
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.
|
2016-08-12 08:27:23 +08:00
|
|
|
if (Cmd->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
|
|
|
|
// as if it wasn't present from the beginning.
|
|
|
|
//
|
|
|
|
// Because we'll iterate over Commands many more times, the easiest
|
|
|
|
// way to "make it as if it wasn't present" is to just remove it.
|
2017-03-14 19:23:33 +08:00
|
|
|
if (!matchConstraints(V, Cmd->Constraint)) {
|
2017-02-23 10:28:28 +08:00
|
|
|
for (InputSectionBase *S : V)
|
2016-11-21 07:15:52 +08:00
|
|
|
S->Assigned = false;
|
2016-09-17 05:05:36 +08:00
|
|
|
Opt.Commands.erase(Iter);
|
2016-09-17 17:50:10 +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.
|
2016-09-17 05:05:36 +08:00
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
|
|
|
|
if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get()))
|
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.
|
|
|
|
if (Cmd->SubalignExpr) {
|
2017-03-17 21:05:04 +08:00
|
|
|
uint32_t Subalign = Cmd->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-02-23 10:28:28 +08:00
|
|
|
for (InputSectionBase *S : V)
|
2017-03-14 17:30:25 +08:00
|
|
|
Factory.addInputSec(S, Cmd->Name);
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
}
|
2017-03-09 06:36:28 +08:00
|
|
|
CurOutSec = nullptr;
|
2016-09-16 23:30:47 +08:00
|
|
|
}
|
2016-07-20 22:43:20 +08:00
|
|
|
|
2016-11-21 10:11:05 +08:00
|
|
|
// Add sections that didn't match any sections command.
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::addOrphanSections(OutputSectionFactory &Factory) {
|
2017-02-27 10:32:08 +08:00
|
|
|
for (InputSectionBase *S : InputSections)
|
2016-11-09 02:23:02 +08:00
|
|
|
if (S->Live && !S->OutSec)
|
2017-03-14 17:30:25 +08:00
|
|
|
Factory.addInputSec(S, getOutputSectionName(S->Name));
|
2016-07-20 22:43:20 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 19:23:33 +08:00
|
|
|
static bool isTbss(OutputSection *Sec) {
|
2016-11-09 09:42:41 +08:00
|
|
|
return (Sec->Flags & SHF_TLS) && Sec->Type == SHT_NOBITS;
|
2016-09-22 20:35:44 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::output(InputSection *S) {
|
2016-09-16 23:10:23 +08:00
|
|
|
if (!AlreadyOutputIS.insert(S).second)
|
|
|
|
return;
|
2017-03-14 19:23:33 +08:00
|
|
|
bool IsTbss = isTbss(CurOutSec);
|
2016-09-16 23:10:23 +08:00
|
|
|
|
2017-03-14 18:00:19 +08:00
|
|
|
uint64_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
|
2016-09-16 23:10:23 +08:00
|
|
|
Pos = alignTo(Pos, S->Alignment);
|
2016-11-09 09:42:41 +08:00
|
|
|
S->OutSecOff = Pos - CurOutSec->Addr;
|
2017-03-08 23:44:30 +08:00
|
|
|
Pos += S->getSize();
|
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) }
|
2016-11-09 09:42:41 +08:00
|
|
|
CurOutSec->Size = Pos - CurOutSec->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.
|
|
|
|
if (CurMemRegion) {
|
|
|
|
CurMemRegion->Offset += CurOutSec->Size;
|
|
|
|
uint64_t CurSize = CurMemRegion->Offset - CurMemRegion->Origin;
|
|
|
|
if (CurSize > CurMemRegion->Length) {
|
|
|
|
uint64_t OverflowAmt = CurSize - CurMemRegion->Length;
|
|
|
|
error("section '" + CurOutSec->Name + "' will not fit in region '" +
|
|
|
|
CurMemRegion->Name + "': overflowed by " + Twine(OverflowAmt) +
|
|
|
|
" bytes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 20:00:08 +08:00
|
|
|
if (IsTbss)
|
|
|
|
ThreadBssOffset = Pos - Dot;
|
|
|
|
else
|
2016-09-16 23:10:23 +08:00
|
|
|
Dot = Pos;
|
|
|
|
}
|
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::flush() {
|
2017-03-01 22:12:21 +08:00
|
|
|
assert(CurOutSec);
|
|
|
|
if (!AlreadyOutputOS.insert(CurOutSec).second)
|
2016-09-24 04:10:47 +08:00
|
|
|
return;
|
2017-02-24 23:07:30 +08:00
|
|
|
for (InputSection *I : CurOutSec->Sections)
|
|
|
|
output(I);
|
2016-08-31 16:13:33 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::switchTo(OutputSection *Sec) {
|
2016-09-16 23:10:23 +08:00
|
|
|
if (CurOutSec == Sec)
|
|
|
|
return;
|
|
|
|
if (AlreadyOutputOS.count(Sec))
|
2016-08-11 15:56:43 +08:00
|
|
|
return;
|
|
|
|
|
2016-09-16 23:10:23 +08:00
|
|
|
CurOutSec = Sec;
|
|
|
|
|
2017-03-07 22:55:52 +08:00
|
|
|
Dot = alignTo(Dot, CurOutSec->Alignment);
|
2017-03-14 19:23:33 +08:00
|
|
|
CurOutSec->Addr = isTbss(CurOutSec) ? Dot + ThreadBssOffset : Dot;
|
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-02-23 15:57:55 +08:00
|
|
|
if (LMAOffset)
|
2017-02-24 22:34:12 +08:00
|
|
|
CurOutSec->LMAOffset = LMAOffset();
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
2016-09-01 17:55:57 +08:00
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::process(BaseCommand &Base) {
|
2016-09-27 03:22:50 +08:00
|
|
|
// This handles the assignments to symbol or to a location counter (.)
|
2016-09-16 23:10:23 +08:00
|
|
|
if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
|
2017-02-18 00:01:51 +08:00
|
|
|
assignSymbol(AssignCmd, true);
|
2016-09-16 23:10:23 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-09-27 03:22:50 +08:00
|
|
|
|
|
|
|
// Handle BYTE(), SHORT(), LONG(), or QUAD().
|
|
|
|
if (auto *DataCmd = dyn_cast<BytesDataCommand>(&Base)) {
|
2016-11-09 09:42:41 +08:00
|
|
|
DataCmd->Offset = Dot - CurOutSec->Addr;
|
2016-09-27 03:22:50 +08:00
|
|
|
Dot += DataCmd->Size;
|
2016-11-09 09:42:41 +08:00
|
|
|
CurOutSec->Size = Dot - CurOutSec->Addr;
|
2016-09-27 03:22:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-23 02:01:50 +08:00
|
|
|
if (auto *AssertCmd = dyn_cast<AssertCommand>(&Base)) {
|
2017-03-11 00:04:26 +08:00
|
|
|
AssertCmd->Expression();
|
2016-11-23 02:01:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-27 03:22:50 +08:00
|
|
|
// It handles single input section description command,
|
|
|
|
// calculates and assigns the offsets for each section and also
|
|
|
|
// updates the output section size.
|
2016-09-16 23:10:23 +08:00
|
|
|
auto &ICmd = cast<InputSectionDescription>(Base);
|
2017-03-01 22:12:21 +08:00
|
|
|
for (InputSectionBase *IB : ICmd.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-03-01 22:12:21 +08:00
|
|
|
if (auto *Sec = dyn_cast<SyntheticSection>(IB))
|
2016-11-30 00:05:27 +08:00
|
|
|
if (Sec->empty())
|
|
|
|
continue;
|
|
|
|
|
2017-02-21 23:46:43 +08:00
|
|
|
if (!IB->Live)
|
|
|
|
continue;
|
2017-03-01 22:21:31 +08:00
|
|
|
assert(CurOutSec == IB->OutSec || AlreadyOutputOS.count(IB->OutSec));
|
2017-03-01 22:12:21 +08:00
|
|
|
output(cast<InputSection>(IB));
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
static OutputSection *
|
|
|
|
findSection(StringRef Name, const std::vector<OutputSection *> &Sections) {
|
2017-02-04 06:27:05 +08:00
|
|
|
auto End = Sections.end();
|
2017-02-24 23:07:30 +08:00
|
|
|
auto HasName = [=](OutputSection *Sec) { return Sec->Name == Name; };
|
2017-02-04 06:27:05 +08:00
|
|
|
auto I = std::find_if(Sections.begin(), End, HasName);
|
2017-02-24 23:07:30 +08:00
|
|
|
std::vector<OutputSection *> Ret;
|
2017-02-04 06:27:05 +08:00
|
|
|
if (I == End)
|
|
|
|
return nullptr;
|
|
|
|
assert(std::find_if(I + 1, End, HasName) == End);
|
|
|
|
return *I;
|
2016-08-13 04:38:20 +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-03-14 20:03:34 +08:00
|
|
|
MemoryRegion *LinkerScriptBase::findMemoryRegion(OutputSectionCommand *Cmd,
|
|
|
|
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.
|
|
|
|
if (!Cmd->MemoryRegionName.empty()) {
|
|
|
|
auto It = Opt.MemoryRegions.find(Cmd->MemoryRegionName);
|
|
|
|
if (It != Opt.MemoryRegions.end())
|
|
|
|
return &It->second;
|
|
|
|
error("memory region '" + Cmd->MemoryRegionName + "' not declared");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The memory region name is empty, thus a suitable region must be
|
|
|
|
// searched for in the region map. If the region map is empty, just
|
|
|
|
// return. Note that this check doesn't happen at the very beginning
|
|
|
|
// so that uses of undeclared regions can be caught.
|
|
|
|
if (!Opt.MemoryRegions.size())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// See if a region can be found by matching section flags.
|
|
|
|
for (auto &MRI : Opt.MemoryRegions) {
|
|
|
|
MemoryRegion &MR = MRI.second;
|
2017-01-26 10:58:59 +08:00
|
|
|
if ((MR.Flags & Sec->Flags) != 0 && (MR.NegFlags & Sec->Flags) == 0)
|
2017-01-24 10:34:00 +08:00
|
|
|
return &MR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::assignOffsets(OutputSectionCommand *Cmd) {
|
2017-03-14 19:31:28 +08:00
|
|
|
OutputSection *Sec = findSection(Cmd->Name, *OutputSections);
|
2017-02-04 06:27:05 +08:00
|
|
|
if (!Sec)
|
2016-09-16 23:10:23 +08:00
|
|
|
return;
|
2017-01-24 10:34:00 +08:00
|
|
|
|
2017-02-18 00:26:13 +08:00
|
|
|
if (Cmd->AddrExpr && Sec->Flags & SHF_ALLOC)
|
2017-02-21 22:50:38 +08:00
|
|
|
setDot(Cmd->AddrExpr, Cmd->Location);
|
2017-02-18 00:26:13 +08:00
|
|
|
|
2017-03-14 16:57:09 +08:00
|
|
|
if (Cmd->LMAExpr) {
|
2017-03-14 18:00:19 +08:00
|
|
|
uint64_t D = Dot;
|
2017-03-17 21:05:04 +08:00
|
|
|
LMAOffset = [=] { return Cmd->LMAExpr().getValue() - D; };
|
2017-03-14 16:57:09 +08:00
|
|
|
}
|
|
|
|
|
2017-02-08 07:42:31 +08:00
|
|
|
// Handle align (e.g. ".foo : ALIGN(16) { ... }").
|
|
|
|
if (Cmd->AlignExpr)
|
2017-03-17 21:05:04 +08:00
|
|
|
Sec->updateAlignment(Cmd->AlignExpr().getValue());
|
2017-02-08 07:42:31 +08:00
|
|
|
|
2017-01-24 10:34:00 +08:00
|
|
|
// Try and find an appropriate memory region to assign offsets in.
|
|
|
|
CurMemRegion = findMemoryRegion(Cmd, Sec);
|
|
|
|
if (CurMemRegion)
|
|
|
|
Dot = CurMemRegion->Offset;
|
|
|
|
switchTo(Sec);
|
2016-11-21 10:11:05 +08:00
|
|
|
|
2016-09-16 23:10:23 +08:00
|
|
|
// Find the last section output location. We will output orphan sections
|
|
|
|
// there so that end symbols point to the correct location.
|
|
|
|
auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
|
|
|
|
[](const std::unique_ptr<BaseCommand> &Cmd) {
|
|
|
|
return !isa<SymbolAssignment>(*Cmd);
|
|
|
|
})
|
|
|
|
.base();
|
|
|
|
for (auto I = Cmd->Commands.begin(); I != E; ++I)
|
|
|
|
process(**I);
|
2016-09-24 04:10:47 +08:00
|
|
|
flush();
|
2016-09-19 21:27:31 +08:00
|
|
|
std::for_each(E, Cmd->Commands.end(),
|
|
|
|
[this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); });
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::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.
|
|
|
|
auto Pos = std::remove_if(
|
|
|
|
Opt.Commands.begin(), Opt.Commands.end(),
|
|
|
|
[&](const std::unique_ptr<BaseCommand> &Base) {
|
2016-11-21 10:11:05 +08:00
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
|
2017-03-14 19:31:28 +08:00
|
|
|
return !findSection(Cmd->Name, *OutputSections);
|
2016-11-21 10:11:05 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-14 22:33:49 +08:00
|
|
|
static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
|
|
|
|
for (const std::unique_ptr<BaseCommand> &I : Cmd.Commands)
|
|
|
|
if (!isa<InputSectionDescription>(*I))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-20 21:12:07 +08:00
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::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;
|
2016-11-26 14:55:35 +08:00
|
|
|
uint32_t Type = SHT_NOBITS;
|
2016-09-22 22:40:50 +08:00
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
|
|
|
|
auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
|
|
|
|
if (!Cmd)
|
|
|
|
continue;
|
2017-03-14 19:31:28 +08:00
|
|
|
if (OutputSection *Sec = findSection(Cmd->Name, *OutputSections)) {
|
2017-02-04 06:27:05 +08:00
|
|
|
Flags = Sec->Flags;
|
|
|
|
Type = Sec->Type;
|
2016-09-22 22:40:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-14 22:33:49 +08:00
|
|
|
if (isAllSectionDescription(*Cmd))
|
|
|
|
continue;
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
auto *OutSec = make<OutputSection>(Cmd->Name, Type, Flags);
|
2016-09-22 22:40:50 +08:00
|
|
|
OutputSections->push_back(OutSec);
|
|
|
|
}
|
2016-11-14 23:39:38 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::adjustSectionsAfterSorting() {
|
2016-11-14 23:39:38 +08:00
|
|
|
placeOrphanSections();
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
|
|
|
|
auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
|
|
|
|
if (!Cmd)
|
|
|
|
continue;
|
|
|
|
if (Cmd->Phdrs.empty())
|
|
|
|
Cmd->Phdrs = DefPhdrs;
|
|
|
|
else
|
|
|
|
DefPhdrs = Cmd->Phdrs;
|
|
|
|
}
|
2016-11-14 22:33:49 +08:00
|
|
|
|
|
|
|
removeEmptyCommands();
|
2016-09-22 22:40:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-23 02:05:49 +08:00
|
|
|
// When placing orphan sections, we want to place them after symbol assignments
|
|
|
|
// so that an orphan after
|
|
|
|
// begin_foo = .;
|
|
|
|
// foo : { *(foo) }
|
|
|
|
// end_foo = .;
|
|
|
|
// doesn't break the intended meaning of the begin/end symbols.
|
|
|
|
// We don't want to go over sections since Writer<ELFT>::sortSections is the
|
|
|
|
// one in charge of deciding the order of the sections.
|
|
|
|
// We don't want to go over alignments, since doing so in
|
|
|
|
// rx_sec : { *(rx_sec) }
|
|
|
|
// . = ALIGN(0x1000);
|
|
|
|
// /* The RW PT_LOAD starts here*/
|
|
|
|
// rw_sec : { *(rw_sec) }
|
|
|
|
// would mean that the RW PT_LOAD would become unaligned.
|
2016-11-27 17:44:45 +08:00
|
|
|
static bool shouldSkip(const BaseCommand &Cmd) {
|
2016-09-23 02:05:49 +08:00
|
|
|
if (isa<OutputSectionCommand>(Cmd))
|
|
|
|
return false;
|
|
|
|
const auto *Assign = dyn_cast<SymbolAssignment>(&Cmd);
|
|
|
|
if (!Assign)
|
|
|
|
return true;
|
2016-11-27 17:44:45 +08:00
|
|
|
return Assign->Name != ".";
|
2016-09-23 02:05:49 +08:00
|
|
|
}
|
|
|
|
|
2017-02-03 07:26:12 +08:00
|
|
|
// Orphan sections are sections present in the input files which are
|
|
|
|
// not explicitly placed into the output file by the linker script.
|
|
|
|
//
|
|
|
|
// When the control reaches this function, Opt.Commands contains
|
|
|
|
// output section commands for non-orphan sections only. This function
|
|
|
|
// adds new elements for orphan sections to Opt.Commands so that all
|
|
|
|
// sections are explicitly handled by Opt.Commands.
|
|
|
|
//
|
|
|
|
// Writer<ELFT>::sortSections has already sorted output sections.
|
|
|
|
// What we need to do is to scan OutputSections vector and
|
|
|
|
// Opt.Commands in parallel to find orphan sections. If there is an
|
|
|
|
// output section that doesn't have a corresponding entry in
|
|
|
|
// Opt.Commands, we will insert a new entry to Opt.Commands.
|
|
|
|
//
|
|
|
|
// There is some ambiguity as to where exactly a new entry should be
|
|
|
|
// inserted, because Opt.Commands contains not only output section
|
|
|
|
// commands but other types of commands such as symbol assignment
|
|
|
|
// expressions. There's no correct answer here due to the lack of the
|
|
|
|
// formal specification of the linker script. We use heuristics to
|
|
|
|
// determine whether a new output command should be added before or
|
|
|
|
// after another commands. For the details, look at shouldSkip
|
|
|
|
// function.
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::placeOrphanSections() {
|
2016-09-17 05:29:07 +08:00
|
|
|
// The OutputSections are already in the correct order.
|
|
|
|
// This loops creates or moves commands as needed so that they are in the
|
|
|
|
// correct order.
|
|
|
|
int CmdIndex = 0;
|
2016-11-27 17:44:45 +08:00
|
|
|
|
|
|
|
// As a horrible special case, skip the first . assignment if it is before any
|
|
|
|
// section. We do this because it is common to set a load address by starting
|
|
|
|
// the script with ". = 0xabcd" and the expectation is that every section is
|
|
|
|
// after that.
|
|
|
|
auto FirstSectionOrDotAssignment =
|
|
|
|
std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
|
|
|
|
[](const std::unique_ptr<BaseCommand> &Cmd) {
|
|
|
|
if (isa<OutputSectionCommand>(*Cmd))
|
|
|
|
return true;
|
|
|
|
const auto *Assign = dyn_cast<SymbolAssignment>(Cmd.get());
|
|
|
|
if (!Assign)
|
|
|
|
return false;
|
|
|
|
return Assign->Name == ".";
|
|
|
|
});
|
|
|
|
if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
|
|
|
|
CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
|
|
|
|
if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
|
|
|
|
++CmdIndex;
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : *OutputSections) {
|
2017-02-24 22:28:00 +08:00
|
|
|
StringRef Name = Sec->Name;
|
2016-09-17 05:29:07 +08:00
|
|
|
|
|
|
|
// Find the last spot where we can insert a command and still get the
|
2016-09-23 02:05:49 +08:00
|
|
|
// correct result.
|
2016-09-17 05:29:07 +08:00
|
|
|
auto CmdIter = Opt.Commands.begin() + CmdIndex;
|
|
|
|
auto E = Opt.Commands.end();
|
2016-11-27 17:44:45 +08:00
|
|
|
while (CmdIter != E && shouldSkip(**CmdIter)) {
|
2016-09-17 05:29:07 +08:00
|
|
|
++CmdIter;
|
|
|
|
++CmdIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Pos =
|
|
|
|
std::find_if(CmdIter, E, [&](const std::unique_ptr<BaseCommand> &Base) {
|
|
|
|
auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
|
|
|
|
return Cmd && Cmd->Name == Name;
|
|
|
|
});
|
|
|
|
if (Pos == E) {
|
|
|
|
Opt.Commands.insert(CmdIter,
|
|
|
|
llvm::make_unique<OutputSectionCommand>(Name));
|
2016-09-23 02:05:49 +08:00
|
|
|
++CmdIndex;
|
|
|
|
continue;
|
2016-09-17 05:29:07 +08:00
|
|
|
}
|
2016-09-23 02:05:49 +08:00
|
|
|
|
|
|
|
// Continue from where we found it.
|
|
|
|
CmdIndex = (Pos - Opt.Commands.begin()) + 1;
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
2016-11-14 22:13:32 +08:00
|
|
|
}
|
|
|
|
|
2017-03-15 11:33:23 +08:00
|
|
|
void LinkerScriptBase::processNonSectionCommands() {
|
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
|
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()))
|
|
|
|
assignSymbol(Cmd);
|
|
|
|
else if (auto *Cmd = dyn_cast<AssertCommand>(Base.get()))
|
|
|
|
Cmd->Expression();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
void LinkerScriptBase::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
|
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-03-17 21:05:04 +08:00
|
|
|
ErrorOnMissingSection = true;
|
2017-02-07 06:21:46 +08:00
|
|
|
switchTo(Aether);
|
|
|
|
|
2016-07-21 14:43:01 +08:00
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
|
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
|
2017-02-18 00:01:51 +08:00
|
|
|
assignSymbol(Cmd);
|
2016-07-12 14:39:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-16 18:10:32 +08:00
|
|
|
|
2016-08-04 17:29:31 +08:00
|
|
|
if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
|
2017-03-11 00:04:26 +08:00
|
|
|
Cmd->Expression();
|
2016-08-04 17:29:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:43:01 +08:00
|
|
|
auto *Cmd = cast<OutputSectionCommand>(Base.get());
|
2016-09-16 23:10:23 +08:00
|
|
|
assignOffsets(Cmd);
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
2016-07-01 18:42:25 +08:00
|
|
|
|
2017-03-14 18:00:19 +08:00
|
|
|
uint64_t MinVA = std::numeric_limits<uint64_t>::max();
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : *OutputSections) {
|
2016-11-09 09:42:41 +08:00
|
|
|
if (Sec->Flags & SHF_ALLOC)
|
2016-11-10 07:23:45 +08:00
|
|
|
MinVA = std::min<uint64_t>(MinVA, Sec->Addr);
|
2017-02-08 23:19:03 +08:00
|
|
|
else
|
|
|
|
Sec->Addr = 0;
|
|
|
|
}
|
2016-09-17 05:29:07 +08:00
|
|
|
|
2017-03-14 17:03:53 +08:00
|
|
|
allocateHeaders(Phdrs, *OutputSections, MinVA);
|
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-03-14 20:03:34 +08:00
|
|
|
std::vector<PhdrEntry> LinkerScriptBase::createPhdrs() {
|
2016-12-20 01:01:01 +08:00
|
|
|
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) {
|
2016-07-25 07:47:31 +08:00
|
|
|
Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
|
2016-12-20 01:01:01 +08:00
|
|
|
PhdrEntry &Phdr = Ret.back();
|
2016-07-19 17:25:43 +08:00
|
|
|
|
|
|
|
if (Cmd.HasFilehdr)
|
2017-02-27 10:31:26 +08:00
|
|
|
Phdr.add(Out::ElfHeader);
|
2016-07-19 17:25:43 +08:00
|
|
|
if (Cmd.HasPhdrs)
|
2017-02-27 10:31:26 +08:00
|
|
|
Phdr.add(Out::ProgramHeaders);
|
2016-09-09 17:46:16 +08:00
|
|
|
|
|
|
|
if (Cmd.LMAExpr) {
|
2017-03-17 21:05:04 +08:00
|
|
|
Phdr.p_paddr = Cmd.LMAExpr().getValue();
|
2016-09-09 17:46:16 +08:00
|
|
|
Phdr.HasLMA = true;
|
|
|
|
}
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2016-08-22 12:55:20 +08:00
|
|
|
// Add output sections to program headers.
|
2017-02-24 23:07:30 +08:00
|
|
|
for (OutputSection *Sec : *OutputSections) {
|
2016-11-09 09:42:41 +08:00
|
|
|
if (!(Sec->Flags & SHF_ALLOC))
|
2016-07-19 17:25:43 +08:00
|
|
|
break;
|
|
|
|
|
2016-10-19 23:04:49 +08:00
|
|
|
// Assign headers specified by linker script
|
2017-02-24 22:28:00 +08:00
|
|
|
for (size_t Id : getPhdrIndices(Sec->Name)) {
|
2016-10-19 23:04:49 +08:00
|
|
|
Ret[Id].add(Sec);
|
|
|
|
if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
|
2016-12-20 01:01:01 +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-14 20:03:34 +08:00
|
|
|
bool LinkerScriptBase::ignoreInterpSection() {
|
2016-08-16 14:40:58 +08:00
|
|
|
// Ignore .interp section in case we have PHDRS specification
|
|
|
|
// and PT_INTERP isn't listed.
|
|
|
|
return !Opt.PhdrsCommands.empty() &&
|
|
|
|
llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
|
|
|
|
return Cmd.Type == PT_INTERP;
|
|
|
|
}) == Opt.PhdrsCommands.end();
|
|
|
|
}
|
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
uint32_t LinkerScriptBase::getFiller(StringRef Name) {
|
2016-07-21 15:48:54 +08:00
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
|
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
|
|
|
|
if (Cmd->Name == Name)
|
|
|
|
return Cmd->Filler;
|
2016-11-20 02:05:56 +08:00
|
|
|
return 0;
|
2016-02-26 22:48:31 +08:00
|
|
|
}
|
|
|
|
|
2016-09-27 03:22:50 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
|
|
|
|
const endianness E = ELFT::TargetEndianness;
|
|
|
|
|
|
|
|
switch (Size) {
|
|
|
|
case 1:
|
|
|
|
*Buf = (uint8_t)Data;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
write16<E>(Buf, Data);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
write32<E>(Buf, Data);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
write64<E>(Buf, Data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported Size argument");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void LinkerScript<ELFT>::writeDataBytes(StringRef Name, uint8_t *Buf) {
|
|
|
|
int I = getSectionIndex(Name);
|
|
|
|
if (I == INT_MAX)
|
|
|
|
return;
|
|
|
|
|
2016-11-20 02:05:58 +08:00
|
|
|
auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get());
|
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
|
|
|
|
if (auto *Data = dyn_cast<BytesDataCommand>(Base.get()))
|
2017-03-17 21:05:04 +08:00
|
|
|
writeInt<ELFT>(Buf + Data->Offset, Data->Expression().getValue(),
|
|
|
|
Data->Size);
|
2016-09-27 03:22:50 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
bool LinkerScriptBase::hasLMA(StringRef Name) {
|
2016-08-17 15:44:19 +08:00
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
|
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
|
2016-10-06 17:39:28 +08:00
|
|
|
if (Cmd->LMAExpr && Cmd->Name == Name)
|
|
|
|
return true;
|
|
|
|
return false;
|
2016-08-17 15:44:19 +08:00
|
|
|
}
|
|
|
|
|
2016-04-22 04:30:00 +08:00
|
|
|
// Returns the index of the given section name in linker script
|
|
|
|
// SECTIONS commands. Sections are laid out as the same order as they
|
|
|
|
// were in the script. If a given name did not appear in the script,
|
|
|
|
// it returns INT_MAX, so that it will be laid out at end of file.
|
2017-03-14 20:03:34 +08:00
|
|
|
int LinkerScriptBase::getSectionIndex(StringRef Name) {
|
2016-11-20 02:05:58 +08:00
|
|
|
for (int I = 0, E = Opt.Commands.size(); I != E; ++I)
|
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get()))
|
2016-07-26 08:21:15 +08:00
|
|
|
if (Cmd->Name == Name)
|
|
|
|
return I;
|
|
|
|
return INT_MAX;
|
2016-04-21 18:22:02 +08:00
|
|
|
}
|
|
|
|
|
2016-12-22 21:13:12 +08:00
|
|
|
template <class ELFT>
|
2017-03-17 21:05:04 +08:00
|
|
|
ExprValue LinkerScript<ELFT>::getSymbolValue(const Twine &Loc, StringRef S) {
|
2017-03-11 00:04:26 +08:00
|
|
|
if (S == ".")
|
2017-03-17 21:05:04 +08:00
|
|
|
return {CurOutSec, Dot - CurOutSec->Addr};
|
|
|
|
if (SymbolBody *B = Symtab<ELFT>::X->find(S)) {
|
|
|
|
if (auto *D = dyn_cast<DefinedRegular>(B))
|
|
|
|
return {D->Section, D->Value};
|
|
|
|
auto *C = cast<DefinedCommon>(B);
|
|
|
|
return {In<ELFT>::Common, C->Offset};
|
|
|
|
}
|
2016-12-22 21:13:12 +08:00
|
|
|
error(Loc + ": symbol not found: " + S);
|
2016-09-08 16:19:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-23 21:17:23 +08:00
|
|
|
template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S) {
|
|
|
|
return Symtab<ELFT>::X->find(S) != nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
// Returns indices of ELF headers containing specific section, identified
|
|
|
|
// by Name. Each index is a zero based number of ELF header listed within
|
|
|
|
// PHDRS {} script block.
|
2017-03-14 20:03:34 +08:00
|
|
|
std::vector<size_t> LinkerScriptBase::getPhdrIndices(StringRef SectionName) {
|
2016-07-21 14:43:01 +08:00
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
|
|
|
|
auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
|
2016-07-25 07:47:31 +08:00
|
|
|
if (!Cmd || Cmd->Name != SectionName)
|
2016-07-21 00:43:03 +08:00
|
|
|
continue;
|
|
|
|
|
2016-07-26 08:27:36 +08:00
|
|
|
std::vector<size_t> Ret;
|
|
|
|
for (StringRef PhdrName : Cmd->Phdrs)
|
2016-12-06 00:38:32 +08:00
|
|
|
Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName));
|
2016-07-26 08:27:36 +08:00
|
|
|
return Ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
2016-07-21 00:43:03 +08:00
|
|
|
return {};
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 20:03:34 +08:00
|
|
|
size_t LinkerScriptBase::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;
|
|
|
|
}
|
2016-12-06 00:38:32 +08:00
|
|
|
error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
|
2016-07-26 08:27:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
class elf::ScriptParser final : public ScriptLexer {
|
2016-02-24 17:21:47 +08:00
|
|
|
typedef void (ScriptParser::*Handler)();
|
|
|
|
|
2015-10-01 01:23:26 +08:00
|
|
|
public:
|
2016-11-26 02:51:54 +08:00
|
|
|
ScriptParser(MemoryBufferRef MB)
|
2017-02-14 12:47:05 +08:00
|
|
|
: ScriptLexer(MB),
|
2016-11-26 02:51:54 +08:00
|
|
|
IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {}
|
2016-02-19 18:45:45 +08:00
|
|
|
|
2016-08-31 17:08:26 +08:00
|
|
|
void readLinkerScript();
|
|
|
|
void readVersionScript();
|
2016-12-09 01:54:26 +08:00
|
|
|
void readDynamicList();
|
2015-10-01 01:23:26 +08:00
|
|
|
|
|
|
|
private:
|
2015-10-11 11:28:42 +08:00
|
|
|
void addFile(StringRef Path);
|
|
|
|
|
2015-10-01 01:23:26 +08:00
|
|
|
void readAsNeeded();
|
2015-10-08 14:48:38 +08:00
|
|
|
void readEntry();
|
2015-10-20 01:35:12 +08:00
|
|
|
void readExtern();
|
2015-10-01 01:23:26 +08:00
|
|
|
void readGroup();
|
2015-10-11 09:31:55 +08:00
|
|
|
void readInclude();
|
2017-01-24 10:34:00 +08:00
|
|
|
void readMemory();
|
2015-10-07 08:25:09 +08:00
|
|
|
void readOutput();
|
2015-10-13 05:50:08 +08:00
|
|
|
void readOutputArch();
|
2015-10-01 01:23:26 +08:00
|
|
|
void readOutputFormat();
|
2016-07-19 17:25:43 +08:00
|
|
|
void readPhdrs();
|
2015-10-09 01:51:41 +08:00
|
|
|
void readSearchDir();
|
2015-11-12 17:52:08 +08:00
|
|
|
void readSections();
|
2016-09-01 04:03:54 +08:00
|
|
|
void readVersion();
|
|
|
|
void readVersionScriptCommand();
|
2015-11-12 17:52:08 +08:00
|
|
|
|
2016-07-25 07:05:57 +08:00
|
|
|
SymbolAssignment *readAssignment(StringRef Name);
|
2016-09-27 03:22:50 +08:00
|
|
|
BytesDataCommand *readBytesDataCommand(StringRef Tok);
|
2016-11-20 02:05:56 +08:00
|
|
|
uint32_t readFill();
|
2016-08-04 10:03:27 +08:00
|
|
|
OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
|
2016-11-20 02:05:56 +08:00
|
|
|
uint32_t readOutputSectionFiller(StringRef Tok);
|
2016-07-19 17:25:43 +08:00
|
|
|
std::vector<StringRef> readOutputSectionPhdrs();
|
2016-08-30 17:46:59 +08:00
|
|
|
InputSectionDescription *readInputSectionDescription(StringRef Tok);
|
2016-11-03 18:54:58 +08:00
|
|
|
StringMatcher readFilePatterns();
|
2016-09-21 23:56:44 +08:00
|
|
|
std::vector<SectionPattern> readInputSectionsList();
|
2016-08-30 17:46:59 +08:00
|
|
|
InputSectionDescription *readInputSectionRules(StringRef FilePattern);
|
2016-07-19 17:25:43 +08:00
|
|
|
unsigned readPhdrType();
|
2016-09-17 04:21:55 +08:00
|
|
|
SortSectionPolicy readSortKind();
|
2016-08-16 09:11:16 +08:00
|
|
|
SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
|
2016-11-01 19:30:45 +08:00
|
|
|
SymbolAssignment *readProvideOrAssignment(StringRef Tok);
|
2016-07-28 15:18:23 +08:00
|
|
|
void readSort();
|
2016-08-04 17:29:31 +08:00
|
|
|
Expr readAssert();
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
|
2017-01-26 10:58:19 +08:00
|
|
|
uint64_t readMemoryAssignment(StringRef, StringRef, StringRef);
|
|
|
|
std::pair<uint32_t, uint32_t> readMemoryAttributes();
|
|
|
|
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
Expr readExpr();
|
|
|
|
Expr readExpr1(Expr Lhs, int MinPrec);
|
2016-10-06 17:39:28 +08:00
|
|
|
StringRef readParenLiteral();
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
Expr readPrimary();
|
|
|
|
Expr readTernary(Expr Cond);
|
2016-08-18 02:59:16 +08:00
|
|
|
Expr readParenExpr();
|
2015-10-01 01:23:26 +08:00
|
|
|
|
2016-08-31 17:08:26 +08:00
|
|
|
// For parsing version script.
|
2016-11-18 14:30:09 +08:00
|
|
|
std::vector<SymbolVersion> readVersionExtern();
|
|
|
|
void readAnonymousDeclaration();
|
2016-09-01 04:03:54 +08:00
|
|
|
void readVersionDeclaration(StringRef VerStr);
|
2017-03-10 03:23:00 +08:00
|
|
|
|
|
|
|
std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
|
|
|
|
readSymbols();
|
2016-08-31 17:08:26 +08:00
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
ScriptConfiguration &Opt = *ScriptConfig;
|
2015-11-26 13:53:00 +08:00
|
|
|
bool IsUnderSysroot;
|
2015-10-01 01:23:26 +08:00
|
|
|
};
|
|
|
|
|
2016-12-09 01:54:26 +08:00
|
|
|
void ScriptParser::readDynamicList() {
|
|
|
|
expect("{");
|
|
|
|
readAnonymousDeclaration();
|
|
|
|
if (!atEOF())
|
|
|
|
setError("EOF expected, but got " + next());
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:08:26 +08:00
|
|
|
void ScriptParser::readVersionScript() {
|
2016-09-01 04:03:54 +08:00
|
|
|
readVersionScriptCommand();
|
|
|
|
if (!atEOF())
|
|
|
|
setError("EOF expected, but got " + next());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readVersionScriptCommand() {
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("{")) {
|
2016-11-18 14:30:09 +08:00
|
|
|
readAnonymousDeclaration();
|
2016-08-31 17:08:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-01 04:03:54 +08:00
|
|
|
while (!atEOF() && !Error && peek() != "}") {
|
2016-08-31 17:08:26 +08:00
|
|
|
StringRef VerStr = next();
|
|
|
|
if (VerStr == "{") {
|
2016-09-01 04:03:54 +08:00
|
|
|
setError("anonymous version definition is used in "
|
|
|
|
"combination with other version definitions");
|
2016-08-31 17:08:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
expect("{");
|
2016-09-01 04:03:54 +08:00
|
|
|
readVersionDeclaration(VerStr);
|
2016-08-31 17:08:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 04:03:54 +08:00
|
|
|
void ScriptParser::readVersion() {
|
|
|
|
expect("{");
|
|
|
|
readVersionScriptCommand();
|
|
|
|
expect("}");
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:08:26 +08:00
|
|
|
void ScriptParser::readLinkerScript() {
|
2015-10-01 01:23:26 +08:00
|
|
|
while (!atEOF()) {
|
|
|
|
StringRef Tok = next();
|
2016-09-03 02:52:41 +08:00
|
|
|
if (Tok == ";")
|
|
|
|
continue;
|
|
|
|
|
2016-09-16 23:30:47 +08:00
|
|
|
if (Tok == "ASSERT") {
|
|
|
|
Opt.Commands.emplace_back(new AssertCommand(readAssert()));
|
|
|
|
} else if (Tok == "ENTRY") {
|
2016-09-03 02:52:41 +08:00
|
|
|
readEntry();
|
|
|
|
} else if (Tok == "EXTERN") {
|
|
|
|
readExtern();
|
|
|
|
} else if (Tok == "GROUP" || Tok == "INPUT") {
|
|
|
|
readGroup();
|
|
|
|
} else if (Tok == "INCLUDE") {
|
|
|
|
readInclude();
|
2017-01-24 10:34:00 +08:00
|
|
|
} else if (Tok == "MEMORY") {
|
|
|
|
readMemory();
|
2016-09-03 02:52:41 +08:00
|
|
|
} else if (Tok == "OUTPUT") {
|
|
|
|
readOutput();
|
|
|
|
} else if (Tok == "OUTPUT_ARCH") {
|
|
|
|
readOutputArch();
|
|
|
|
} else if (Tok == "OUTPUT_FORMAT") {
|
|
|
|
readOutputFormat();
|
|
|
|
} else if (Tok == "PHDRS") {
|
|
|
|
readPhdrs();
|
|
|
|
} else if (Tok == "SEARCH_DIR") {
|
|
|
|
readSearchDir();
|
|
|
|
} else if (Tok == "SECTIONS") {
|
|
|
|
readSections();
|
|
|
|
} else if (Tok == "VERSION") {
|
|
|
|
readVersion();
|
2016-11-01 19:30:45 +08:00
|
|
|
} else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) {
|
2016-09-16 23:30:47 +08:00
|
|
|
Opt.Commands.emplace_back(Cmd);
|
2016-08-31 23:31:17 +08:00
|
|
|
} else {
|
2016-03-11 22:43:02 +08:00
|
|
|
setError("unknown directive: " + Tok);
|
2016-08-31 23:31:17 +08:00
|
|
|
}
|
2015-10-01 01:23:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::addFile(StringRef S) {
|
2015-11-26 13:53:00 +08:00
|
|
|
if (IsUnderSysroot && S.startswith("/")) {
|
2016-10-17 14:08:48 +08:00
|
|
|
SmallString<128> PathData;
|
|
|
|
StringRef Path = (Config->Sysroot + S).toStringRef(PathData);
|
2015-11-26 13:53:00 +08:00
|
|
|
if (sys::fs::exists(Path)) {
|
2016-10-17 14:08:48 +08:00
|
|
|
Driver->addFile(Saver.save(Path));
|
2015-11-26 13:53:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 08:09:21 +08:00
|
|
|
if (sys::path::is_absolute(S)) {
|
2015-10-11 11:28:42 +08:00
|
|
|
Driver->addFile(S);
|
|
|
|
} else if (S.startswith("=")) {
|
|
|
|
if (Config->Sysroot.empty())
|
|
|
|
Driver->addFile(S.substr(1));
|
|
|
|
else
|
|
|
|
Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
|
|
|
|
} else if (S.startswith("-l")) {
|
2016-02-03 05:13:09 +08:00
|
|
|
Driver->addLibrary(S.substr(2));
|
2015-11-27 04:23:46 +08:00
|
|
|
} else if (sys::fs::exists(S)) {
|
|
|
|
Driver->addFile(S);
|
2015-10-11 11:28:42 +08:00
|
|
|
} else {
|
2016-11-20 03:23:58 +08:00
|
|
|
if (Optional<std::string> Path = findFromSearchPaths(S))
|
|
|
|
Driver->addFile(Saver.save(*Path));
|
2016-02-03 04:27:59 +08:00
|
|
|
else
|
2016-11-20 03:23:58 +08:00
|
|
|
setError("unable to find " + S);
|
2015-10-11 11:28:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readAsNeeded() {
|
2015-10-01 01:23:26 +08:00
|
|
|
expect("(");
|
2015-10-12 04:59:12 +08:00
|
|
|
bool Orig = Config->AsNeeded;
|
|
|
|
Config->AsNeeded = true;
|
2016-10-18 00:01:53 +08:00
|
|
|
while (!Error && !consume(")"))
|
2016-09-09 22:35:36 +08:00
|
|
|
addFile(unquote(next()));
|
2015-10-12 04:59:12 +08:00
|
|
|
Config->AsNeeded = Orig;
|
2015-10-01 01:23:26 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readEntry() {
|
2015-10-08 14:48:38 +08:00
|
|
|
// -e <symbol> takes predecence over ENTRY(<symbol>).
|
|
|
|
expect("(");
|
|
|
|
StringRef Tok = next();
|
|
|
|
if (Config->Entry.empty())
|
|
|
|
Config->Entry = Tok;
|
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readExtern() {
|
2015-10-20 01:35:12 +08:00
|
|
|
expect("(");
|
2016-10-18 00:01:53 +08:00
|
|
|
while (!Error && !consume(")"))
|
2016-08-05 09:25:45 +08:00
|
|
|
Config->Undefined.push_back(next());
|
2015-10-20 01:35:12 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readGroup() {
|
2015-10-01 01:23:26 +08:00
|
|
|
expect("(");
|
2016-10-18 00:01:53 +08:00
|
|
|
while (!Error && !consume(")")) {
|
2015-10-01 01:23:26 +08:00
|
|
|
StringRef Tok = next();
|
2016-08-05 09:25:45 +08:00
|
|
|
if (Tok == "AS_NEEDED")
|
2015-10-01 01:23:26 +08:00
|
|
|
readAsNeeded();
|
2016-08-05 09:25:45 +08:00
|
|
|
else
|
2016-09-09 22:35:36 +08:00
|
|
|
addFile(unquote(Tok));
|
2015-10-01 01:23:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readInclude() {
|
2016-12-21 17:42:25 +08:00
|
|
|
StringRef Tok = unquote(next());
|
2017-01-09 09:42:02 +08:00
|
|
|
|
2016-12-21 17:42:25 +08:00
|
|
|
// https://sourceware.org/binutils/docs/ld/File-Commands.html:
|
|
|
|
// The file will be searched for in the current directory, and in any
|
|
|
|
// directory specified with the -L option.
|
2017-01-09 09:42:02 +08:00
|
|
|
if (sys::fs::exists(Tok)) {
|
|
|
|
if (Optional<MemoryBufferRef> MB = readFile(Tok))
|
|
|
|
tokenize(*MB);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Optional<std::string> Path = findFromSearchPaths(Tok)) {
|
|
|
|
if (Optional<MemoryBufferRef> MB = readFile(*Path))
|
|
|
|
tokenize(*MB);
|
2016-02-03 04:27:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-01-09 09:42:02 +08:00
|
|
|
setError("cannot open " + Tok);
|
2015-10-11 09:31:55 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readOutput() {
|
2015-10-07 08:25:09 +08:00
|
|
|
// -o <file> takes predecence over OUTPUT(<file>).
|
|
|
|
expect("(");
|
|
|
|
StringRef Tok = next();
|
|
|
|
if (Config->OutputFile.empty())
|
2016-09-09 22:35:36 +08:00
|
|
|
Config->OutputFile = unquote(Tok);
|
2015-10-07 08:25:09 +08:00
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readOutputArch() {
|
2017-02-08 17:59:06 +08:00
|
|
|
// OUTPUT_ARCH is ignored for now.
|
2015-10-13 05:50:08 +08:00
|
|
|
expect("(");
|
2017-02-08 17:59:06 +08:00
|
|
|
while (!Error && !consume(")"))
|
|
|
|
skip();
|
2015-10-13 05:50:08 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readOutputFormat() {
|
2015-10-01 01:23:26 +08:00
|
|
|
// Error checking only for now.
|
|
|
|
expect("(");
|
2016-10-17 14:21:13 +08:00
|
|
|
skip();
|
2015-10-13 05:08:41 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok == ")")
|
2016-09-08 16:20:30 +08:00
|
|
|
return;
|
2016-02-03 04:27:59 +08:00
|
|
|
if (Tok != ",") {
|
2016-03-11 22:43:02 +08:00
|
|
|
setError("unexpected token: " + Tok);
|
2016-02-03 04:27:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-10-17 14:21:13 +08:00
|
|
|
skip();
|
2015-10-13 05:08:41 +08:00
|
|
|
expect(",");
|
2016-10-17 14:21:13 +08:00
|
|
|
skip();
|
2015-10-01 01:23:26 +08:00
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
void ScriptParser::readPhdrs() {
|
|
|
|
expect("{");
|
2016-10-18 00:01:53 +08:00
|
|
|
while (!Error && !consume("}")) {
|
2016-07-19 17:25:43 +08:00
|
|
|
StringRef Tok = next();
|
2016-09-09 17:46:16 +08:00
|
|
|
Opt.PhdrsCommands.push_back(
|
|
|
|
{Tok, PT_NULL, false, false, UINT_MAX, nullptr});
|
2016-07-19 17:25:43 +08:00
|
|
|
PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
|
|
|
|
|
|
|
|
PhdrCmd.Type = readPhdrType();
|
|
|
|
do {
|
|
|
|
Tok = next();
|
|
|
|
if (Tok == ";")
|
|
|
|
break;
|
|
|
|
if (Tok == "FILEHDR")
|
|
|
|
PhdrCmd.HasFilehdr = true;
|
|
|
|
else if (Tok == "PHDRS")
|
|
|
|
PhdrCmd.HasPhdrs = true;
|
2016-09-09 17:46:16 +08:00
|
|
|
else if (Tok == "AT")
|
|
|
|
PhdrCmd.LMAExpr = readParenExpr();
|
2016-07-21 18:43:25 +08:00
|
|
|
else if (Tok == "FLAGS") {
|
|
|
|
expect("(");
|
2016-08-03 06:14:57 +08:00
|
|
|
// Passing 0 for the value of dot is a bit of a hack. It means that
|
|
|
|
// we accept expressions like ".|1".
|
2017-03-17 21:05:04 +08:00
|
|
|
PhdrCmd.Flags = readExpr()().getValue();
|
2016-07-21 18:43:25 +08:00
|
|
|
expect(")");
|
|
|
|
} else
|
2016-07-19 17:25:43 +08:00
|
|
|
setError("unexpected header attribute: " + Tok);
|
|
|
|
} while (!Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readSearchDir() {
|
2015-10-09 01:51:41 +08:00
|
|
|
expect("(");
|
2016-09-09 07:26:54 +08:00
|
|
|
StringRef Tok = next();
|
2016-09-03 03:20:33 +08:00
|
|
|
if (!Config->Nostdlib)
|
2016-09-09 22:35:36 +08:00
|
|
|
Config->SearchPaths.push_back(unquote(Tok));
|
2015-10-09 01:51:41 +08:00
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readSections() {
|
2016-09-14 16:32:36 +08:00
|
|
|
Opt.HasSections = true;
|
2016-11-28 18:11:10 +08:00
|
|
|
// -no-rosegment is used to avoid placing read only non-executable sections in
|
|
|
|
// their own segment. We do the same if SECTIONS command is present in linker
|
|
|
|
// script. See comment for computeFlags().
|
|
|
|
Config->SingleRoRx = true;
|
|
|
|
|
2015-11-12 17:52:08 +08:00
|
|
|
expect("{");
|
2016-10-18 00:01:53 +08:00
|
|
|
while (!Error && !consume("}")) {
|
2016-07-25 07:05:57 +08:00
|
|
|
StringRef Tok = next();
|
2016-11-01 19:30:45 +08:00
|
|
|
BaseCommand *Cmd = readProvideOrAssignment(Tok);
|
2016-08-11 15:56:43 +08:00
|
|
|
if (!Cmd) {
|
|
|
|
if (Tok == "ASSERT")
|
|
|
|
Cmd = new AssertCommand(readAssert());
|
|
|
|
else
|
|
|
|
Cmd = readOutputSectionDescription(Tok);
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
}
|
2016-08-04 10:03:27 +08:00
|
|
|
Opt.Commands.emplace_back(Cmd);
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
2015-11-12 17:52:08 +08:00
|
|
|
}
|
|
|
|
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
static int precedence(StringRef Op) {
|
|
|
|
return StringSwitch<int>(Op)
|
2016-09-24 02:06:51 +08:00
|
|
|
.Cases("*", "/", 5)
|
|
|
|
.Cases("+", "-", 4)
|
|
|
|
.Cases("<<", ">>", 3)
|
2016-09-24 06:22:34 +08:00
|
|
|
.Cases("<", "<=", ">", ">=", "==", "!=", 2)
|
2016-09-24 02:06:51 +08:00
|
|
|
.Cases("&", "|", 1)
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
.Default(-1);
|
|
|
|
}
|
|
|
|
|
2016-11-03 18:54:58 +08:00
|
|
|
StringMatcher ScriptParser::readFilePatterns() {
|
2016-08-04 10:03:27 +08:00
|
|
|
std::vector<StringRef> V;
|
2016-10-18 00:01:53 +08:00
|
|
|
while (!Error && !consume(")"))
|
2016-08-04 10:03:27 +08:00
|
|
|
V.push_back(next());
|
2016-11-04 01:57:38 +08:00
|
|
|
return StringMatcher(V);
|
2016-07-29 23:32:46 +08:00
|
|
|
}
|
|
|
|
|
2016-09-17 04:21:55 +08:00
|
|
|
SortSectionPolicy ScriptParser::readSortKind() {
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("SORT") || consume("SORT_BY_NAME"))
|
2016-09-17 04:21:55 +08:00
|
|
|
return SortSectionPolicy::Name;
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("SORT_BY_ALIGNMENT"))
|
2016-09-17 04:21:55 +08:00
|
|
|
return SortSectionPolicy::Alignment;
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("SORT_BY_INIT_PRIORITY"))
|
2016-09-17 04:21:55 +08:00
|
|
|
return SortSectionPolicy::Priority;
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("SORT_NONE"))
|
2016-09-17 05:14:55 +08:00
|
|
|
return SortSectionPolicy::None;
|
|
|
|
return SortSectionPolicy::Default;
|
2016-09-17 04:21:55 +08:00
|
|
|
}
|
|
|
|
|
2016-09-17 01:42:10 +08:00
|
|
|
// Method reads a list of sequence of excluded files and section globs given in
|
|
|
|
// a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+
|
|
|
|
// Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3)
|
2016-09-18 03:17:25 +08:00
|
|
|
// The semantics of that is next:
|
|
|
|
// * Include .foo.1 from every file.
|
|
|
|
// * Include .foo.2 from every file but a.o
|
|
|
|
// * Include .foo.3 from every file but b.o
|
2016-09-21 23:56:44 +08:00
|
|
|
std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
|
|
|
|
std::vector<SectionPattern> Ret;
|
2016-09-21 16:53:21 +08:00
|
|
|
while (!Error && peek() != ")") {
|
2016-11-04 01:57:38 +08:00
|
|
|
StringMatcher ExcludeFilePat;
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("EXCLUDE_FILE")) {
|
2016-09-17 01:42:10 +08:00
|
|
|
expect("(");
|
2016-11-04 01:57:38 +08:00
|
|
|
ExcludeFilePat = readFilePatterns();
|
2016-09-17 01:42:10 +08:00
|
|
|
}
|
|
|
|
|
2016-09-21 16:53:21 +08:00
|
|
|
std::vector<StringRef> V;
|
|
|
|
while (!Error && peek() != ")" && peek() != "EXCLUDE_FILE")
|
|
|
|
V.push_back(next());
|
|
|
|
|
|
|
|
if (!V.empty())
|
2016-11-04 01:57:38 +08:00
|
|
|
Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)});
|
2016-09-21 16:53:21 +08:00
|
|
|
else
|
|
|
|
setError("section pattern is expected");
|
2016-09-17 01:42:10 +08:00
|
|
|
}
|
2016-09-21 23:56:44 +08:00
|
|
|
return Ret;
|
2016-09-17 01:42:10 +08:00
|
|
|
}
|
|
|
|
|
2016-11-18 15:03:56 +08:00
|
|
|
// Reads contents of "SECTIONS" directive. That directive contains a
|
|
|
|
// list of glob patterns for input sections. The grammar is as follows.
|
|
|
|
//
|
|
|
|
// <patterns> ::= <section-list>
|
|
|
|
// | <sort> "(" <section-list> ")"
|
|
|
|
// | <sort> "(" <sort> "(" <section-list> ")" ")"
|
|
|
|
//
|
|
|
|
// <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
|
|
|
|
// | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
|
|
|
|
//
|
|
|
|
// <section-list> is parsed by readInputSectionsList().
|
2016-08-30 17:46:59 +08:00
|
|
|
InputSectionDescription *
|
|
|
|
ScriptParser::readInputSectionRules(StringRef FilePattern) {
|
2016-09-03 05:17:20 +08:00
|
|
|
auto *Cmd = new InputSectionDescription(FilePattern);
|
2016-07-26 05:47:13 +08:00
|
|
|
expect("(");
|
2016-11-24 09:43:21 +08:00
|
|
|
while (!Error && !consume(")")) {
|
2016-09-21 23:56:44 +08:00
|
|
|
SortSectionPolicy Outer = readSortKind();
|
|
|
|
SortSectionPolicy Inner = SortSectionPolicy::Default;
|
|
|
|
std::vector<SectionPattern> V;
|
|
|
|
if (Outer != SortSectionPolicy::Default) {
|
2016-08-03 16:35:59 +08:00
|
|
|
expect("(");
|
2016-09-21 23:56:44 +08:00
|
|
|
Inner = readSortKind();
|
|
|
|
if (Inner != SortSectionPolicy::Default) {
|
|
|
|
expect("(");
|
|
|
|
V = readInputSectionsList();
|
|
|
|
expect(")");
|
|
|
|
} else {
|
|
|
|
V = readInputSectionsList();
|
|
|
|
}
|
2016-08-03 16:35:59 +08:00
|
|
|
expect(")");
|
|
|
|
} else {
|
2016-09-21 23:56:44 +08:00
|
|
|
V = readInputSectionsList();
|
2016-08-03 16:35:59 +08:00
|
|
|
}
|
2016-07-29 23:32:46 +08:00
|
|
|
|
2016-09-21 23:56:44 +08:00
|
|
|
for (SectionPattern &Pat : V) {
|
|
|
|
Pat.SortInner = Inner;
|
|
|
|
Pat.SortOuter = Outer;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
|
|
|
|
}
|
2016-08-04 10:03:27 +08:00
|
|
|
return Cmd;
|
2016-07-29 05:51:30 +08:00
|
|
|
}
|
|
|
|
|
2016-08-30 17:46:59 +08:00
|
|
|
InputSectionDescription *
|
|
|
|
ScriptParser::readInputSectionDescription(StringRef Tok) {
|
2016-07-29 05:51:30 +08:00
|
|
|
// Input section wildcard can be surrounded by KEEP.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
|
2016-08-30 17:46:59 +08:00
|
|
|
if (Tok == "KEEP") {
|
2016-07-29 05:51:30 +08:00
|
|
|
expect("(");
|
2016-08-30 17:46:59 +08:00
|
|
|
StringRef FilePattern = next();
|
|
|
|
InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
|
2016-07-27 09:44:01 +08:00
|
|
|
expect(")");
|
2016-10-05 17:36:59 +08:00
|
|
|
Opt.KeptSections.push_back(Cmd);
|
2016-08-04 10:03:27 +08:00
|
|
|
return Cmd;
|
2016-07-26 05:47:13 +08:00
|
|
|
}
|
2016-08-30 17:46:59 +08:00
|
|
|
return readInputSectionRules(Tok);
|
2016-07-26 05:47:13 +08:00
|
|
|
}
|
|
|
|
|
2016-07-28 15:18:23 +08:00
|
|
|
void ScriptParser::readSort() {
|
|
|
|
expect("(");
|
|
|
|
expect("CONSTRUCTORS");
|
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
2016-08-04 17:29:31 +08:00
|
|
|
Expr ScriptParser::readAssert() {
|
|
|
|
expect("(");
|
|
|
|
Expr E = readExpr();
|
|
|
|
expect(",");
|
2016-09-09 22:35:36 +08:00
|
|
|
StringRef Msg = unquote(next());
|
2016-08-04 17:29:31 +08:00
|
|
|
expect(")");
|
2017-03-11 00:04:26 +08:00
|
|
|
return [=] {
|
2017-03-17 21:05:04 +08:00
|
|
|
if (!E().getValue())
|
2016-08-04 17:29:31 +08:00
|
|
|
error(Msg);
|
2017-03-17 05:50:30 +08:00
|
|
|
return ScriptBase->getDot();
|
2016-08-04 17:29:31 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-07 01:46:43 +08:00
|
|
|
// Reads a FILL(expr) command. We handle the FILL command as an
|
|
|
|
// alias for =fillexp section attribute, which is different from
|
|
|
|
// what GNU linkers do.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
|
2016-11-20 02:05:56 +08:00
|
|
|
uint32_t ScriptParser::readFill() {
|
2016-09-06 21:51:57 +08:00
|
|
|
expect("(");
|
2016-11-20 02:05:56 +08:00
|
|
|
uint32_t V = readOutputSectionFiller(next());
|
2016-09-06 21:51:57 +08:00
|
|
|
expect(")");
|
|
|
|
expect(";");
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2016-08-04 10:03:27 +08:00
|
|
|
OutputSectionCommand *
|
|
|
|
ScriptParser::readOutputSectionDescription(StringRef OutSec) {
|
2016-07-21 14:43:01 +08:00
|
|
|
OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
|
2016-12-06 00:38:32 +08:00
|
|
|
Cmd->Location = getCurrentLocation();
|
2016-07-25 16:29:46 +08:00
|
|
|
|
|
|
|
// Read an address expression.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
|
|
|
|
if (peek() != ":")
|
|
|
|
Cmd->AddrExpr = readExpr();
|
|
|
|
|
2015-11-12 17:52:08 +08:00
|
|
|
expect(":");
|
2016-07-22 11:36:24 +08:00
|
|
|
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("AT"))
|
2016-10-06 17:39:28 +08:00
|
|
|
Cmd->LMAExpr = readParenExpr();
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("ALIGN"))
|
2016-08-18 02:59:16 +08:00
|
|
|
Cmd->AlignExpr = readParenExpr();
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("SUBALIGN"))
|
2016-08-19 23:18:23 +08:00
|
|
|
Cmd->SubalignExpr = readParenExpr();
|
2016-07-27 02:06:29 +08:00
|
|
|
|
2016-07-22 11:36:24 +08:00
|
|
|
// Parse constraints.
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("ONLY_IF_RO"))
|
2016-07-26 06:00:10 +08:00
|
|
|
Cmd->Constraint = ConstraintKind::ReadOnly;
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("ONLY_IF_RW"))
|
2016-07-26 06:00:10 +08:00
|
|
|
Cmd->Constraint = ConstraintKind::ReadWrite;
|
2015-11-12 17:52:08 +08:00
|
|
|
expect("{");
|
2016-04-22 06:00:51 +08:00
|
|
|
|
2016-10-18 00:01:53 +08:00
|
|
|
while (!Error && !consume("}")) {
|
2016-08-11 15:56:43 +08:00
|
|
|
StringRef Tok = next();
|
2017-01-31 16:50:11 +08:00
|
|
|
if (Tok == ";") {
|
2017-02-01 17:14:22 +08:00
|
|
|
// Empty commands are allowed. Do nothing here.
|
2017-01-31 16:50:11 +08:00
|
|
|
} else if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok)) {
|
2016-08-11 15:56:43 +08:00
|
|
|
Cmd->Commands.emplace_back(Assignment);
|
2016-11-23 02:01:50 +08:00
|
|
|
} else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) {
|
2016-09-27 03:22:50 +08:00
|
|
|
Cmd->Commands.emplace_back(Data);
|
2016-11-23 02:01:50 +08:00
|
|
|
} else if (Tok == "ASSERT") {
|
|
|
|
Cmd->Commands.emplace_back(new AssertCommand(readAssert()));
|
|
|
|
expect(";");
|
2017-01-23 17:36:19 +08:00
|
|
|
} else if (Tok == "CONSTRUCTORS") {
|
|
|
|
// CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
|
|
|
|
// by name. This is for very old file formats such as ECOFF/XCOFF.
|
|
|
|
// For ELF, we should ignore.
|
2016-11-23 02:01:50 +08:00
|
|
|
} else if (Tok == "FILL") {
|
2016-09-06 21:51:57 +08:00
|
|
|
Cmd->Filler = readFill();
|
2016-11-23 02:01:50 +08:00
|
|
|
} else if (Tok == "SORT") {
|
2016-07-28 15:18:23 +08:00
|
|
|
readSort();
|
2016-11-23 02:01:50 +08:00
|
|
|
} else if (peek() == "(") {
|
2016-08-30 17:46:59 +08:00
|
|
|
Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
|
2016-11-23 02:01:50 +08:00
|
|
|
} else {
|
2016-08-11 15:56:43 +08:00
|
|
|
setError("unknown command " + Tok);
|
2016-11-23 02:01:50 +08:00
|
|
|
}
|
2015-11-12 17:52:08 +08:00
|
|
|
}
|
2017-01-24 10:34:00 +08:00
|
|
|
|
|
|
|
if (consume(">"))
|
|
|
|
Cmd->MemoryRegionName = next();
|
|
|
|
|
2016-07-21 14:43:01 +08:00
|
|
|
Cmd->Phdrs = readOutputSectionPhdrs();
|
2016-09-23 21:29:20 +08:00
|
|
|
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("="))
|
2016-09-23 21:29:20 +08:00
|
|
|
Cmd->Filler = readOutputSectionFiller(next());
|
|
|
|
else if (peek().startswith("="))
|
2016-09-06 21:51:57 +08:00
|
|
|
Cmd->Filler = readOutputSectionFiller(next().drop_front());
|
2016-09-23 21:29:20 +08:00
|
|
|
|
2017-01-17 23:32:12 +08:00
|
|
|
// Consume optional comma following output section command.
|
|
|
|
consume(",");
|
|
|
|
|
2016-08-04 10:03:27 +08:00
|
|
|
return Cmd;
|
2016-07-29 14:14:07 +08:00
|
|
|
}
|
2016-04-22 06:00:51 +08:00
|
|
|
|
2016-08-30 06:01:21 +08:00
|
|
|
// Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
|
|
|
|
//
|
|
|
|
// ld.gold is not fully compatible with ld.bfd. ld.bfd handles
|
|
|
|
// hexstrings as blobs of arbitrary sizes, while ld.gold handles them
|
|
|
|
// as 32-bit big-endian values. We will do the same as ld.gold does
|
|
|
|
// because it's simpler than what ld.bfd does.
|
2016-11-20 02:05:56 +08:00
|
|
|
uint32_t ScriptParser::readOutputSectionFiller(StringRef Tok) {
|
2016-08-04 07:25:15 +08:00
|
|
|
uint32_t V;
|
2016-11-20 02:05:56 +08:00
|
|
|
if (!Tok.getAsInteger(0, V))
|
|
|
|
return V;
|
|
|
|
setError("invalid filler expression: " + Tok);
|
|
|
|
return 0;
|
2015-11-12 17:52:08 +08:00
|
|
|
}
|
|
|
|
|
2016-08-16 09:11:16 +08:00
|
|
|
SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
|
2016-07-22 15:38:40 +08:00
|
|
|
expect("(");
|
2016-07-29 08:29:25 +08:00
|
|
|
SymbolAssignment *Cmd = readAssignment(next());
|
2016-08-16 09:11:16 +08:00
|
|
|
Cmd->Provide = Provide;
|
2016-07-29 08:29:25 +08:00
|
|
|
Cmd->Hidden = Hidden;
|
2016-07-22 15:38:40 +08:00
|
|
|
expect(")");
|
|
|
|
expect(";");
|
2016-08-04 10:03:27 +08:00
|
|
|
return Cmd;
|
2016-07-22 15:38:40 +08:00
|
|
|
}
|
|
|
|
|
2016-11-01 19:30:45 +08:00
|
|
|
SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) {
|
2016-08-11 15:56:43 +08:00
|
|
|
SymbolAssignment *Cmd = nullptr;
|
|
|
|
if (peek() == "=" || peek() == "+=") {
|
|
|
|
Cmd = readAssignment(Tok);
|
|
|
|
expect(";");
|
|
|
|
} else if (Tok == "PROVIDE") {
|
2016-08-16 09:11:16 +08:00
|
|
|
Cmd = readProvideHidden(true, false);
|
|
|
|
} else if (Tok == "HIDDEN") {
|
|
|
|
Cmd = readProvideHidden(false, true);
|
2016-08-11 15:56:43 +08:00
|
|
|
} else if (Tok == "PROVIDE_HIDDEN") {
|
2016-08-16 09:11:16 +08:00
|
|
|
Cmd = readProvideHidden(true, true);
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
|
2016-07-29 05:08:56 +08:00
|
|
|
SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
|
|
|
|
StringRef Op = next();
|
|
|
|
assert(Op == "=" || Op == "+=");
|
2017-03-15 11:33:23 +08:00
|
|
|
Expr E = readExpr();
|
2016-12-22 21:13:12 +08:00
|
|
|
if (Op == "+=") {
|
|
|
|
std::string Loc = getCurrentLocation();
|
2017-03-17 21:05:04 +08:00
|
|
|
E = [=] { return add(ScriptBase->getSymbolValue(Loc, Name), E()); };
|
2016-12-22 21:13:12 +08:00
|
|
|
}
|
2017-02-21 22:50:38 +08:00
|
|
|
return new SymbolAssignment(Name, E, getCurrentLocation());
|
2016-07-29 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is an operator-precedence parser to parse a linker
|
|
|
|
// script expression.
|
2017-02-16 03:58:17 +08:00
|
|
|
Expr ScriptParser::readExpr() {
|
|
|
|
// Our lexer is context-aware. Set the in-expression bit so that
|
|
|
|
// they apply different tokenization rules.
|
|
|
|
bool Orig = InExpr;
|
|
|
|
InExpr = true;
|
|
|
|
Expr E = readExpr1(readPrimary(), 0);
|
|
|
|
InExpr = Orig;
|
|
|
|
return E;
|
|
|
|
}
|
2016-07-29 05:08:56 +08:00
|
|
|
|
2016-08-05 09:04:59 +08:00
|
|
|
static Expr combine(StringRef Op, Expr L, Expr R) {
|
|
|
|
if (Op == "*")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return mul(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == "/") {
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return div(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
}
|
|
|
|
if (Op == "+")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return add(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == "-")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return sub(L(), R()); };
|
2016-09-23 21:13:55 +08:00
|
|
|
if (Op == "<<")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return leftShift(L(), R()); };
|
2016-09-23 21:13:55 +08:00
|
|
|
if (Op == ">>")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return rightShift(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == "<")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return lessThan(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == ">")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return greaterThan(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == ">=")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return greaterThanOrEqual(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == "<=")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return lessThanOrEqual(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == "==")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return ::equal(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == "!=")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return notEqual(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == "&")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return bitAnd(L(), R()); };
|
2016-08-23 05:33:35 +08:00
|
|
|
if (Op == "|")
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return bitOr(L(), R()); };
|
2016-08-05 09:04:59 +08:00
|
|
|
llvm_unreachable("invalid operator");
|
|
|
|
}
|
|
|
|
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
// This is a part of the operator-precedence parser. This function
|
|
|
|
// assumes that the remaining token stream starts with an operator.
|
|
|
|
Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
|
|
|
|
while (!atEOF() && !Error) {
|
|
|
|
// Read an operator and an expression.
|
2016-11-18 14:49:07 +08:00
|
|
|
if (consume("?"))
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
return readTernary(Lhs);
|
2016-11-18 14:49:07 +08:00
|
|
|
StringRef Op1 = peek();
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
if (precedence(Op1) < MinPrec)
|
|
|
|
break;
|
2016-10-17 14:21:13 +08:00
|
|
|
skip();
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
Expr Rhs = readPrimary();
|
|
|
|
|
|
|
|
// Evaluate the remaining part of the expression first if the
|
|
|
|
// next operator has greater precedence than the previous one.
|
|
|
|
// For example, if we have read "+" and "3", and if the next
|
|
|
|
// operator is "*", then we'll evaluate 3 * ... part first.
|
|
|
|
while (!atEOF()) {
|
|
|
|
StringRef Op2 = peek();
|
|
|
|
if (precedence(Op2) <= precedence(Op1))
|
2016-07-22 15:38:40 +08:00
|
|
|
break;
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
Rhs = readExpr1(Rhs, precedence(Op2));
|
|
|
|
}
|
2016-07-22 15:38:40 +08:00
|
|
|
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
Lhs = combine(Op1, Lhs, Rhs);
|
2016-07-12 14:39:48 +08:00
|
|
|
}
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
return Lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t static getConstant(StringRef S) {
|
2016-08-17 10:10:51 +08:00
|
|
|
if (S == "COMMONPAGESIZE")
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
return Target->PageSize;
|
2016-08-17 10:10:51 +08:00
|
|
|
if (S == "MAXPAGESIZE")
|
2016-09-28 23:20:47 +08:00
|
|
|
return Config->MaxPageSize;
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
error("unknown constant: " + S);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-03 02:19:00 +08:00
|
|
|
// Parses Tok as an integer. Returns true if successful.
|
|
|
|
// It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
|
|
|
|
// and decimal numbers. Decimal numbers may have "K" (kilo) or
|
|
|
|
// "M" (mega) prefixes.
|
2016-09-03 00:01:42 +08:00
|
|
|
static bool readInteger(StringRef Tok, uint64_t &Result) {
|
2016-11-18 14:49:07 +08:00
|
|
|
// Negative number
|
2016-09-03 05:54:35 +08:00
|
|
|
if (Tok.startswith("-")) {
|
|
|
|
if (!readInteger(Tok.substr(1), Result))
|
|
|
|
return false;
|
|
|
|
Result = -Result;
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-18 14:49:07 +08:00
|
|
|
|
|
|
|
// Hexadecimal
|
2016-09-03 00:01:42 +08:00
|
|
|
if (Tok.startswith_lower("0x"))
|
|
|
|
return !Tok.substr(2).getAsInteger(16, Result);
|
|
|
|
if (Tok.endswith_lower("H"))
|
|
|
|
return !Tok.drop_back().getAsInteger(16, Result);
|
|
|
|
|
2016-11-18 14:49:07 +08:00
|
|
|
// Decimal
|
2016-09-03 00:01:42 +08:00
|
|
|
int Suffix = 1;
|
|
|
|
if (Tok.endswith_lower("K")) {
|
|
|
|
Suffix = 1024;
|
|
|
|
Tok = Tok.drop_back();
|
|
|
|
} else if (Tok.endswith_lower("M")) {
|
|
|
|
Suffix = 1024 * 1024;
|
|
|
|
Tok = Tok.drop_back();
|
|
|
|
}
|
|
|
|
if (Tok.getAsInteger(10, Result))
|
|
|
|
return false;
|
|
|
|
Result *= Suffix;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-27 03:22:50 +08:00
|
|
|
BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) {
|
|
|
|
int Size = StringSwitch<unsigned>(Tok)
|
|
|
|
.Case("BYTE", 1)
|
|
|
|
.Case("SHORT", 2)
|
|
|
|
.Case("LONG", 4)
|
|
|
|
.Case("QUAD", 8)
|
|
|
|
.Default(-1);
|
|
|
|
if (Size == -1)
|
|
|
|
return nullptr;
|
|
|
|
|
2016-12-09 07:21:30 +08:00
|
|
|
return new BytesDataCommand(readParenExpr(), Size);
|
2016-09-27 03:22:50 +08:00
|
|
|
}
|
|
|
|
|
2016-10-06 17:39:28 +08:00
|
|
|
StringRef ScriptParser::readParenLiteral() {
|
|
|
|
expect("(");
|
|
|
|
StringRef Tok = next();
|
|
|
|
expect(")");
|
|
|
|
return Tok;
|
|
|
|
}
|
|
|
|
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
Expr ScriptParser::readPrimary() {
|
2016-08-18 02:59:16 +08:00
|
|
|
if (peek() == "(")
|
|
|
|
return readParenExpr();
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
|
2016-08-18 02:59:16 +08:00
|
|
|
StringRef Tok = next();
|
2016-12-01 12:36:49 +08:00
|
|
|
std::string Location = getCurrentLocation();
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
|
2016-09-03 05:54:35 +08:00
|
|
|
if (Tok == "~") {
|
|
|
|
Expr E = readPrimary();
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return bitNot(E()); };
|
2016-09-03 05:54:35 +08:00
|
|
|
}
|
|
|
|
if (Tok == "-") {
|
|
|
|
Expr E = readPrimary();
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return minus(E()); };
|
2016-09-03 05:54:35 +08:00
|
|
|
}
|
|
|
|
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
// Built-in functions are parsed here.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
|
2017-03-15 11:33:23 +08:00
|
|
|
if (Tok == "ABSOLUTE") {
|
2017-03-17 21:05:04 +08:00
|
|
|
Expr Inner = readParenExpr();
|
|
|
|
return [=] {
|
|
|
|
ExprValue I = Inner();
|
|
|
|
I.ForceAbsolute = true;
|
|
|
|
return I;
|
|
|
|
};
|
2017-03-15 11:33:23 +08:00
|
|
|
}
|
2016-08-30 17:54:01 +08:00
|
|
|
if (Tok == "ADDR") {
|
2016-10-06 17:39:28 +08:00
|
|
|
StringRef Name = readParenLiteral();
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=]() -> ExprValue {
|
|
|
|
return {ScriptBase->getOutputSection(Location, Name), 0};
|
|
|
|
};
|
2016-08-30 17:54:01 +08:00
|
|
|
}
|
2016-10-06 17:39:28 +08:00
|
|
|
if (Tok == "LOADADDR") {
|
|
|
|
StringRef Name = readParenLiteral();
|
2017-03-11 00:04:26 +08:00
|
|
|
return
|
|
|
|
[=] { return ScriptBase->getOutputSection(Location, Name)->getLMA(); };
|
2016-10-06 17:39:28 +08:00
|
|
|
}
|
2016-08-04 17:29:31 +08:00
|
|
|
if (Tok == "ASSERT")
|
|
|
|
return readAssert();
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
if (Tok == "ALIGN") {
|
2016-12-17 02:19:35 +08:00
|
|
|
expect("(");
|
|
|
|
Expr E = readExpr();
|
|
|
|
if (consume(",")) {
|
|
|
|
Expr E2 = readExpr();
|
|
|
|
expect(")");
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return alignTo(E().getValue(), E2().getValue()); };
|
2016-12-17 02:19:35 +08:00
|
|
|
}
|
|
|
|
expect(")");
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return alignTo(ScriptBase->getDot(), E().getValue()); };
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
}
|
|
|
|
if (Tok == "CONSTANT") {
|
2016-10-06 17:39:28 +08:00
|
|
|
StringRef Name = readParenLiteral();
|
2017-03-11 00:04:26 +08:00
|
|
|
return [=] { return getConstant(Name); };
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
}
|
2016-09-23 21:17:23 +08:00
|
|
|
if (Tok == "DEFINED") {
|
2016-11-17 11:52:14 +08:00
|
|
|
StringRef Name = readParenLiteral();
|
2017-03-11 00:04:26 +08:00
|
|
|
return [=] { return ScriptBase->isDefined(Name) ? 1 : 0; };
|
2016-09-23 21:17:23 +08:00
|
|
|
}
|
2016-07-29 02:16:24 +08:00
|
|
|
if (Tok == "SEGMENT_START") {
|
|
|
|
expect("(");
|
2016-10-17 14:21:13 +08:00
|
|
|
skip();
|
2016-07-29 02:16:24 +08:00
|
|
|
expect(",");
|
2016-09-18 02:14:56 +08:00
|
|
|
Expr E = readExpr();
|
2016-07-29 02:16:24 +08:00
|
|
|
expect(")");
|
2017-03-11 00:04:26 +08:00
|
|
|
return [=] { return E(); };
|
2016-07-29 02:16:24 +08:00
|
|
|
}
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
if (Tok == "DATA_SEGMENT_ALIGN") {
|
|
|
|
expect("(");
|
|
|
|
Expr E = readExpr();
|
|
|
|
expect(",");
|
|
|
|
readExpr();
|
|
|
|
expect(")");
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return alignTo(ScriptBase->getDot(), E().getValue()); };
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
}
|
|
|
|
if (Tok == "DATA_SEGMENT_END") {
|
|
|
|
expect("(");
|
|
|
|
expect(".");
|
|
|
|
expect(")");
|
2017-03-17 21:00:45 +08:00
|
|
|
return [] { return ScriptBase->getDot(); };
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
}
|
2016-07-27 01:58:44 +08:00
|
|
|
// GNU linkers implements more complicated logic to handle
|
|
|
|
// DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
|
|
|
|
// the next page boundary for simplicity.
|
|
|
|
if (Tok == "DATA_SEGMENT_RELRO_END") {
|
|
|
|
expect("(");
|
2016-09-15 03:14:01 +08:00
|
|
|
readExpr();
|
2016-07-27 01:58:44 +08:00
|
|
|
expect(",");
|
|
|
|
readExpr();
|
|
|
|
expect(")");
|
2017-03-17 21:00:45 +08:00
|
|
|
return [] { return alignTo(ScriptBase->getDot(), Target->PageSize); };
|
2016-07-27 01:58:44 +08:00
|
|
|
}
|
2016-07-30 00:18:47 +08:00
|
|
|
if (Tok == "SIZEOF") {
|
2016-10-06 17:39:28 +08:00
|
|
|
StringRef Name = readParenLiteral();
|
2017-03-11 00:04:26 +08:00
|
|
|
return [=] { return ScriptBase->getOutputSectionSize(Name); };
|
2016-07-30 00:18:47 +08:00
|
|
|
}
|
2016-09-08 17:08:30 +08:00
|
|
|
if (Tok == "ALIGNOF") {
|
2016-10-06 17:39:28 +08:00
|
|
|
StringRef Name = readParenLiteral();
|
2017-03-11 00:04:26 +08:00
|
|
|
return
|
|
|
|
[=] { return ScriptBase->getOutputSection(Location, Name)->Alignment; };
|
2016-09-08 17:08:30 +08:00
|
|
|
}
|
2016-08-10 15:59:34 +08:00
|
|
|
if (Tok == "SIZEOF_HEADERS")
|
2017-03-13 22:40:58 +08:00
|
|
|
return [=] { return elf::getHeaderSize(); };
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
|
2016-09-03 00:01:42 +08:00
|
|
|
// Tok is a literal number.
|
|
|
|
uint64_t V;
|
|
|
|
if (readInteger(Tok, V))
|
2017-03-11 00:04:26 +08:00
|
|
|
return [=] { return V; };
|
2016-09-03 00:01:42 +08:00
|
|
|
|
|
|
|
// Tok is a symbol name.
|
|
|
|
if (Tok != "." && !isValidCIdentifier(Tok))
|
|
|
|
setError("malformed number: " + Tok);
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return ScriptBase->getSymbolValue(Location, Tok); };
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Expr ScriptParser::readTernary(Expr Cond) {
|
|
|
|
Expr L = readExpr();
|
|
|
|
expect(":");
|
|
|
|
Expr R = readExpr();
|
2017-03-17 21:05:04 +08:00
|
|
|
return [=] { return Cond().getValue() ? L() : R(); };
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
}
|
|
|
|
|
2016-08-18 02:59:16 +08:00
|
|
|
Expr ScriptParser::readParenExpr() {
|
|
|
|
expect("(");
|
|
|
|
Expr E = readExpr();
|
|
|
|
expect(")");
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
|
|
|
|
std::vector<StringRef> Phdrs;
|
|
|
|
while (!Error && peek().startswith(":")) {
|
|
|
|
StringRef Tok = next();
|
2016-11-14 18:03:54 +08:00
|
|
|
Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1));
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
return Phdrs;
|
|
|
|
}
|
|
|
|
|
2016-10-18 18:49:50 +08:00
|
|
|
// Read a program header type name. The next token must be a
|
|
|
|
// name of a program header type or a constant (e.g. "0x3").
|
2016-07-19 17:25:43 +08:00
|
|
|
unsigned ScriptParser::readPhdrType() {
|
|
|
|
StringRef Tok = next();
|
2016-10-18 18:49:50 +08:00
|
|
|
uint64_t Val;
|
|
|
|
if (readInteger(Tok, Val))
|
|
|
|
return Val;
|
|
|
|
|
2016-07-21 03:36:38 +08:00
|
|
|
unsigned Ret = StringSwitch<unsigned>(Tok)
|
2016-09-08 16:20:30 +08:00
|
|
|
.Case("PT_NULL", PT_NULL)
|
|
|
|
.Case("PT_LOAD", PT_LOAD)
|
|
|
|
.Case("PT_DYNAMIC", PT_DYNAMIC)
|
|
|
|
.Case("PT_INTERP", PT_INTERP)
|
|
|
|
.Case("PT_NOTE", PT_NOTE)
|
|
|
|
.Case("PT_SHLIB", PT_SHLIB)
|
|
|
|
.Case("PT_PHDR", PT_PHDR)
|
|
|
|
.Case("PT_TLS", PT_TLS)
|
|
|
|
.Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
|
|
|
|
.Case("PT_GNU_STACK", PT_GNU_STACK)
|
|
|
|
.Case("PT_GNU_RELRO", PT_GNU_RELRO)
|
2016-10-14 21:02:22 +08:00
|
|
|
.Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
|
2016-10-14 18:34:36 +08:00
|
|
|
.Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
|
2016-12-07 01:57:42 +08:00
|
|
|
.Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
|
2016-09-08 16:20:30 +08:00
|
|
|
.Default(-1);
|
2016-07-19 17:25:43 +08:00
|
|
|
|
2016-07-21 03:36:38 +08:00
|
|
|
if (Ret == (unsigned)-1) {
|
|
|
|
setError("invalid program header type: " + Tok);
|
|
|
|
return PT_NULL;
|
|
|
|
}
|
|
|
|
return Ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2017-03-10 03:23:00 +08:00
|
|
|
// Reads an anonymous version declaration.
|
2016-11-18 14:30:09 +08:00
|
|
|
void ScriptParser::readAnonymousDeclaration() {
|
2017-03-10 03:23:00 +08:00
|
|
|
std::vector<SymbolVersion> Locals;
|
|
|
|
std::vector<SymbolVersion> Globals;
|
|
|
|
std::tie(Locals, Globals) = readSymbols();
|
2017-01-11 00:37:24 +08:00
|
|
|
|
|
|
|
for (SymbolVersion V : Locals) {
|
2017-03-10 03:23:00 +08:00
|
|
|
if (V.Name == "*")
|
2016-11-18 14:30:09 +08:00
|
|
|
Config->DefaultSymbolVersion = VER_NDX_LOCAL;
|
2017-03-10 03:23:00 +08:00
|
|
|
else
|
|
|
|
Config->VersionScriptLocals.push_back(V);
|
2016-11-18 14:30:09 +08:00
|
|
|
}
|
2017-03-10 03:23:00 +08:00
|
|
|
|
|
|
|
for (SymbolVersion V : Globals)
|
|
|
|
Config->VersionScriptGlobals.push_back(V);
|
|
|
|
|
|
|
|
expect(";");
|
2016-11-18 14:30:09 +08:00
|
|
|
}
|
|
|
|
|
2017-03-10 03:23:00 +08:00
|
|
|
// Reads a non-anonymous version definition,
|
|
|
|
// e.g. "VerStr { global: foo; bar; local: *; };".
|
2016-09-01 04:03:54 +08:00
|
|
|
void ScriptParser::readVersionDeclaration(StringRef VerStr) {
|
2017-03-10 03:23:00 +08:00
|
|
|
// Read a symbol list.
|
|
|
|
std::vector<SymbolVersion> Locals;
|
|
|
|
std::vector<SymbolVersion> Globals;
|
|
|
|
std::tie(Locals, Globals) = readSymbols();
|
|
|
|
|
|
|
|
for (SymbolVersion V : Locals) {
|
|
|
|
if (V.Name == "*")
|
|
|
|
Config->DefaultSymbolVersion = VER_NDX_LOCAL;
|
|
|
|
else
|
|
|
|
Config->VersionScriptLocals.push_back(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new version definition and add that to the global symbols.
|
|
|
|
VersionDefinition Ver;
|
|
|
|
Ver.Name = VerStr;
|
|
|
|
Ver.Globals = Globals;
|
|
|
|
|
|
|
|
// User-defined version number starts from 2 because 0 and 1 are
|
|
|
|
// reserved for VER_NDX_LOCAL and VER_NDX_GLOBAL, respectively.
|
|
|
|
Ver.Id = Config->VersionDefinitions.size() + 2;
|
|
|
|
Config->VersionDefinitions.push_back(Ver);
|
2016-08-31 17:08:26 +08:00
|
|
|
|
2016-11-18 14:30:09 +08:00
|
|
|
// Each version may have a parent version. For example, "Ver2"
|
|
|
|
// defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
|
|
|
|
// as a parent. This version hierarchy is, probably against your
|
|
|
|
// instinct, purely for hint; the runtime doesn't care about it
|
|
|
|
// at all. In LLD, we simply ignore it.
|
|
|
|
if (peek() != ";")
|
2016-10-17 14:21:13 +08:00
|
|
|
skip();
|
2016-08-31 17:08:26 +08:00
|
|
|
expect(";");
|
|
|
|
}
|
|
|
|
|
2017-03-10 03:23:00 +08:00
|
|
|
// Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
|
|
|
|
std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
|
|
|
|
ScriptParser::readSymbols() {
|
|
|
|
std::vector<SymbolVersion> Locals;
|
|
|
|
std::vector<SymbolVersion> Globals;
|
|
|
|
std::vector<SymbolVersion> *V = &Globals;
|
|
|
|
|
|
|
|
while (!Error) {
|
|
|
|
if (consume("}"))
|
|
|
|
break;
|
|
|
|
if (consumeLabel("local")) {
|
|
|
|
V = &Locals;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (consumeLabel("global")) {
|
|
|
|
V = &Globals;
|
2016-12-10 00:44:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-17 01:59:10 +08:00
|
|
|
|
2017-03-10 03:23:00 +08:00
|
|
|
if (consume("extern")) {
|
|
|
|
std::vector<SymbolVersion> Ext = readVersionExtern();
|
|
|
|
V->insert(V->end(), Ext.begin(), Ext.end());
|
|
|
|
} else {
|
|
|
|
StringRef Tok = next();
|
|
|
|
V->push_back({unquote(Tok), false, hasWildcard(Tok)});
|
|
|
|
}
|
2016-11-17 01:59:10 +08:00
|
|
|
expect(";");
|
|
|
|
}
|
2017-03-10 03:23:00 +08:00
|
|
|
return {Locals, Globals};
|
2016-11-17 01:59:10 +08:00
|
|
|
}
|
|
|
|
|
2016-11-18 14:30:09 +08:00
|
|
|
// Reads an "extern C++" directive, e.g.,
|
|
|
|
// "extern "C++" { ns::*; "f(int, double)"; };"
|
|
|
|
std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
|
2016-12-09 01:26:53 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
bool IsCXX = Tok == "\"C++\"";
|
|
|
|
if (!IsCXX && Tok != "\"C\"")
|
2016-12-09 01:54:26 +08:00
|
|
|
setError("Unknown language");
|
2016-08-31 17:08:26 +08:00
|
|
|
expect("{");
|
|
|
|
|
2016-11-18 14:30:09 +08:00
|
|
|
std::vector<SymbolVersion> Ret;
|
2016-11-17 11:52:14 +08:00
|
|
|
while (!Error && peek() != "}") {
|
|
|
|
StringRef Tok = next();
|
|
|
|
bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok);
|
2016-12-09 01:26:53 +08:00
|
|
|
Ret.push_back({unquote(Tok), IsCXX, HasWildcard});
|
2016-08-31 17:08:26 +08:00
|
|
|
expect(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
expect("}");
|
2016-11-18 14:30:09 +08:00
|
|
|
return Ret;
|
2016-08-31 17:08:26 +08:00
|
|
|
}
|
|
|
|
|
2017-03-20 17:51:18 +08:00
|
|
|
uint64_t ScriptParser::readMemoryAssignment(StringRef S1, StringRef S2,
|
|
|
|
StringRef S3) {
|
2017-01-26 10:58:19 +08:00
|
|
|
if (!(consume(S1) || consume(S2) || consume(S3))) {
|
|
|
|
setError("expected one of: " + S1 + ", " + S2 + ", or " + S3);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
expect("=");
|
|
|
|
|
|
|
|
// TODO: Fully support constant expressions.
|
|
|
|
uint64_t Val;
|
|
|
|
if (!readInteger(next(), Val))
|
2017-03-20 17:51:18 +08:00
|
|
|
setError("nonconstant expression for " + S1);
|
2017-01-26 10:58:19 +08:00
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the MEMORY command as specified in:
|
|
|
|
// https://sourceware.org/binutils/docs/ld/MEMORY.html
|
|
|
|
//
|
|
|
|
// MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
|
2017-01-24 10:34:00 +08:00
|
|
|
void ScriptParser::readMemory() {
|
|
|
|
expect("{");
|
|
|
|
while (!Error && !consume("}")) {
|
|
|
|
StringRef Name = next();
|
2017-01-26 10:58:19 +08:00
|
|
|
|
2017-01-24 10:34:00 +08:00
|
|
|
uint32_t Flags = 0;
|
2017-01-26 10:58:59 +08:00
|
|
|
uint32_t NegFlags = 0;
|
2017-01-24 10:34:00 +08:00
|
|
|
if (consume("(")) {
|
2017-01-26 10:58:59 +08:00
|
|
|
std::tie(Flags, NegFlags) = readMemoryAttributes();
|
2017-01-24 10:34:00 +08:00
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
expect(":");
|
|
|
|
|
2017-01-26 10:58:19 +08:00
|
|
|
uint64_t Origin = readMemoryAssignment("ORIGIN", "org", "o");
|
2017-01-24 10:34:00 +08:00
|
|
|
expect(",");
|
2017-01-26 10:58:19 +08:00
|
|
|
uint64_t Length = readMemoryAssignment("LENGTH", "len", "l");
|
2017-01-24 10:34:00 +08:00
|
|
|
|
|
|
|
// Add the memory region to the region map (if it doesn't already exist).
|
|
|
|
auto It = Opt.MemoryRegions.find(Name);
|
|
|
|
if (It != Opt.MemoryRegions.end())
|
|
|
|
setError("region '" + Name + "' already defined");
|
|
|
|
else
|
2017-01-26 10:58:59 +08:00
|
|
|
Opt.MemoryRegions[Name] = {Name, Origin, Length, Origin, Flags, NegFlags};
|
2017-01-24 10:34:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function parses the attributes used to match against section
|
|
|
|
// flags when placing output sections in a memory region. These flags
|
|
|
|
// are only used when an explicit memory region name is not used.
|
|
|
|
std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() {
|
|
|
|
uint32_t Flags = 0;
|
2017-01-26 10:58:59 +08:00
|
|
|
uint32_t NegFlags = 0;
|
2017-01-24 10:34:00 +08:00
|
|
|
bool Invert = false;
|
2017-01-26 10:58:39 +08:00
|
|
|
|
|
|
|
for (char C : next().lower()) {
|
2017-01-24 10:34:00 +08:00
|
|
|
uint32_t Flag = 0;
|
|
|
|
if (C == '!')
|
|
|
|
Invert = !Invert;
|
2017-01-26 10:58:39 +08:00
|
|
|
else if (C == 'w')
|
2017-01-24 10:34:00 +08:00
|
|
|
Flag = SHF_WRITE;
|
2017-01-26 10:58:39 +08:00
|
|
|
else if (C == 'x')
|
2017-01-24 10:34:00 +08:00
|
|
|
Flag = SHF_EXECINSTR;
|
2017-01-26 10:58:39 +08:00
|
|
|
else if (C == 'a')
|
2017-01-24 10:34:00 +08:00
|
|
|
Flag = SHF_ALLOC;
|
2017-01-26 10:58:39 +08:00
|
|
|
else if (C != 'r')
|
2017-01-24 10:34:00 +08:00
|
|
|
setError("invalid memory region attribute");
|
2017-01-26 10:58:39 +08:00
|
|
|
|
2017-01-24 10:34:00 +08:00
|
|
|
if (Invert)
|
2017-01-26 10:58:59 +08:00
|
|
|
NegFlags |= Flag;
|
2017-01-24 10:34:00 +08:00
|
|
|
else
|
|
|
|
Flags |= Flag;
|
|
|
|
}
|
2017-01-26 10:58:59 +08:00
|
|
|
return {Flags, NegFlags};
|
2017-01-24 10:34:00 +08:00
|
|
|
}
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
void elf::readLinkerScript(MemoryBufferRef MB) {
|
2016-11-26 02:51:54 +08:00
|
|
|
ScriptParser(MB).readLinkerScript();
|
2016-08-31 17:08:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void elf::readVersionScript(MemoryBufferRef MB) {
|
2016-11-26 02:51:54 +08:00
|
|
|
ScriptParser(MB).readVersionScript();
|
2016-04-21 04:13:41 +08:00
|
|
|
}
|
|
|
|
|
2016-12-09 01:54:26 +08:00
|
|
|
void elf::readDynamicList(MemoryBufferRef MB) {
|
|
|
|
ScriptParser(MB).readDynamicList();
|
|
|
|
}
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
template class elf::LinkerScript<ELF32LE>;
|
|
|
|
template class elf::LinkerScript<ELF32BE>;
|
|
|
|
template class elf::LinkerScript<ELF64LE>;
|
|
|
|
template class elf::LinkerScript<ELF64BE>;
|