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"
|
2016-04-07 04:59:11 +08:00
|
|
|
#include "ScriptParser.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
|
|
|
|
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) {
|
2016-10-31 21:14:53 +08:00
|
|
|
uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
|
2017-01-10 02:36:57 +08:00
|
|
|
Symbol *Sym = Symtab<ELFT>::X->addUndefined(
|
|
|
|
Cmd->Name, /*IsLocal=*/false, STB_GLOBAL, Visibility,
|
|
|
|
/*Type*/ 0,
|
|
|
|
/*CanOmitFromDynSym*/ false, /*File*/ nullptr);
|
|
|
|
|
|
|
|
replaceBody<DefinedRegular<ELFT>>(Sym, Cmd->Name, /*IsLocal=*/false,
|
|
|
|
Visibility, STT_NOTYPE, 0, 0, nullptr,
|
|
|
|
nullptr);
|
|
|
|
return Sym->body();
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
|
2017-01-10 02:36:57 +08:00
|
|
|
template <class ELFT> static SymbolBody *addSynthetic(SymbolAssignment *Cmd) {
|
|
|
|
uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
|
2016-11-16 17:49:39 +08:00
|
|
|
const OutputSectionBase *Sec =
|
|
|
|
ScriptConfig->HasSections ? nullptr : Cmd->Expression.Section();
|
2017-01-10 02:36:57 +08:00
|
|
|
Symbol *Sym = Symtab<ELFT>::X->addUndefined(
|
|
|
|
Cmd->Name, /*IsLocal=*/false, STB_GLOBAL, Visibility,
|
|
|
|
/*Type*/ 0,
|
|
|
|
/*CanOmitFromDynSym*/ false, /*File*/ nullptr);
|
2016-11-16 17:49:39 +08:00
|
|
|
|
2017-01-10 02:36:57 +08:00
|
|
|
replaceBody<DefinedSynthetic>(Sym, Cmd->Name, 0, Sec);
|
|
|
|
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-01-10 02:36:57 +08:00
|
|
|
template <class ELFT> static void assignSymbol(SymbolAssignment *Cmd) {
|
|
|
|
// If there are sections, then let the value be assigned later in
|
|
|
|
// `assignAddresses`.
|
|
|
|
if (ScriptConfig->HasSections)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint64_t Value = Cmd->Expression(0);
|
|
|
|
if (Cmd->Expression.IsAbsolute()) {
|
|
|
|
cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Value;
|
|
|
|
} else {
|
|
|
|
const OutputSectionBase *Sec = Cmd->Expression.Section();
|
|
|
|
if (Sec)
|
|
|
|
cast<DefinedSynthetic>(Cmd->Sym)->Value = Value - Sec->Addr;
|
|
|
|
}
|
2016-09-07 15:08:43 +08:00
|
|
|
}
|
2017-01-10 02:36:57 +08:00
|
|
|
|
|
|
|
template <class ELFT> static void 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;
|
|
|
|
|
|
|
|
// Otherwise, create a new symbol if one does not exist or an
|
|
|
|
// undefined one does exist.
|
|
|
|
if (Cmd->Expression.IsAbsolute())
|
|
|
|
Cmd->Sym = addRegular<ELFT>(Cmd);
|
|
|
|
else
|
|
|
|
Cmd->Sym = addSynthetic<ELFT>(Cmd);
|
|
|
|
assignSymbol<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
|
|
|
|
2016-11-21 10:10:12 +08:00
|
|
|
template <class ELFT> static StringRef basename(InputSectionBase<ELFT> *S) {
|
|
|
|
if (S->getFile())
|
|
|
|
return sys::path::filename(S->getFile()->getName());
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *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;
|
|
|
|
}
|
|
|
|
|
2016-09-16 03:15:12 +08:00
|
|
|
static bool comparePriority(InputSectionData *A, InputSectionData *B) {
|
|
|
|
return getPriority(A->Name) < getPriority(B->Name);
|
|
|
|
}
|
|
|
|
|
2016-09-09 04:47:52 +08:00
|
|
|
static bool compareName(InputSectionData *A, InputSectionData *B) {
|
2016-09-08 22:06:08 +08:00
|
|
|
return A->Name < B->Name;
|
2016-08-05 06:27:00 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 04:47:52 +08:00
|
|
|
static bool compareAlignment(InputSectionData *A, InputSectionData *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;
|
|
|
|
}
|
|
|
|
|
2016-09-09 04:47:52 +08:00
|
|
|
static std::function<bool(InputSectionData *, InputSectionData *)>
|
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
|
|
|
|
2016-08-12 17:07:57 +08:00
|
|
|
template <class ELFT>
|
2016-09-17 04:34:02 +08:00
|
|
|
static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> 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;
|
2016-09-22 02:33:44 +08:00
|
|
|
bool IsRW = llvm::any_of(Sections, [=](InputSectionData *Sec2) {
|
2016-09-16 23:10:23 +08:00
|
|
|
auto *Sec = static_cast<InputSectionBase<ELFT> *>(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
|
|
|
}
|
|
|
|
|
2016-09-21 23:56:44 +08:00
|
|
|
static void sortSections(InputSectionData **Begin, InputSectionData **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.
|
2016-09-14 22:32:08 +08:00
|
|
|
template <class ELFT>
|
2016-09-17 04:34:02 +08:00
|
|
|
void LinkerScript<ELFT>::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
|
|
|
|
|
|
|
for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) {
|
2016-11-21 07:15:52 +08:00
|
|
|
if (!S->Live || S->Assigned)
|
2016-11-06 06:37:59 +08:00
|
|
|
continue;
|
|
|
|
|
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.
|
|
|
|
InputSectionData **Begin = I->Sections.data() + SizeBefore;
|
|
|
|
InputSectionData **End = I->Sections.data() + I->Sections.size();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
|
|
|
|
for (InputSectionBase<ELFT> *S : V) {
|
|
|
|
S->Live = false;
|
|
|
|
reportDiscarded(S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 08:27:23 +08:00
|
|
|
template <class ELFT>
|
2016-08-12 11:16:56 +08:00
|
|
|
std::vector<InputSectionBase<ELFT> *>
|
2016-08-12 17:07:57 +08:00
|
|
|
LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
|
2016-08-12 11:16:56 +08:00
|
|
|
std::vector<InputSectionBase<ELFT> *> Ret;
|
|
|
|
|
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);
|
2016-09-16 23:10:23 +08:00
|
|
|
for (InputSectionData *S : Cmd->Sections)
|
|
|
|
Ret.push_back(static_cast<InputSectionBase<ELFT> *>(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-08-12 11:16:56 +08:00
|
|
|
template <class ELFT>
|
2016-09-16 23:30:47 +08:00
|
|
|
void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory,
|
|
|
|
InputSectionBase<ELFT> *Sec,
|
|
|
|
StringRef Name) {
|
2016-11-10 07:23:45 +08:00
|
|
|
OutputSectionBase *OutSec;
|
2016-09-16 23:30:47 +08:00
|
|
|
bool IsNew;
|
2017-01-05 22:20:35 +08:00
|
|
|
std::tie(OutSec, IsNew) = Factory.create(Sec, Name);
|
2016-09-16 23:30:47 +08:00
|
|
|
if (IsNew)
|
|
|
|
OutputSections->push_back(OutSec);
|
|
|
|
OutSec->addSection(Sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
|
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-01-10 02:36:57 +08:00
|
|
|
addSymbol<ELFT>(Cmd);
|
2016-08-12 11:33:04 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-21 10:11:05 +08:00
|
|
|
|
2016-09-16 23:30:47 +08:00
|
|
|
if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) {
|
|
|
|
// If we don't have SECTIONS then output sections have already been
|
2016-09-18 03:21:05 +08:00
|
|
|
// created by Writer<ELFT>. The LinkerScript<ELFT>::assignAddresses
|
2016-09-16 23:30:47 +08:00
|
|
|
// will not be called, so ASSERT should be evaluated now.
|
|
|
|
if (!Opt.HasSections)
|
|
|
|
Cmd->Expression(0);
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-12 11:33:04 +08:00
|
|
|
|
2016-08-11 15:56:43 +08:00
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
|
2016-09-13 00:05:16 +08:00
|
|
|
std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
|
|
|
|
|
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.
|
2016-09-17 05:05:36 +08:00
|
|
|
if (!matchConstraints<ELFT>(V, Cmd->Constraint)) {
|
|
|
|
for (InputSectionBase<ELFT> *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-01-10 02:36:57 +08:00
|
|
|
addSymbol<ELFT>(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) {
|
|
|
|
uint32_t Subalign = Cmd->SubalignExpr(0);
|
|
|
|
for (InputSectionBase<ELFT> *S : V)
|
|
|
|
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.
|
|
|
|
for (InputSectionBase<ELFT> *S : V)
|
|
|
|
addSection(Factory, S, Cmd->Name);
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
}
|
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.
|
2016-09-16 23:30:47 +08:00
|
|
|
template <class ELFT>
|
2016-12-15 23:38:09 +08:00
|
|
|
void LinkerScript<ELFT>::addOrphanSections(
|
|
|
|
OutputSectionFactory<ELFT> &Factory) {
|
2016-11-06 06:37:59 +08:00
|
|
|
for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections)
|
2016-11-09 02:23:02 +08:00
|
|
|
if (S->Live && !S->OutSec)
|
2016-11-06 06:37:59 +08:00
|
|
|
addSection(Factory, S, getOutputSectionName(S->Name));
|
2016-07-20 22:43:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 15:08:43 +08:00
|
|
|
// Sets value of a section-defined 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.
|
|
|
|
template <class ELFT>
|
2016-11-16 17:49:39 +08:00
|
|
|
static void assignSectionSymbol(SymbolAssignment *Cmd,
|
2016-10-31 22:44:41 +08:00
|
|
|
typename ELFT::uint Value) {
|
2016-09-07 15:08:43 +08:00
|
|
|
if (!Cmd->Sym)
|
|
|
|
return;
|
|
|
|
|
2016-12-21 16:40:09 +08:00
|
|
|
if (auto *Body = dyn_cast<DefinedSynthetic>(Cmd->Sym)) {
|
2016-11-16 17:49:39 +08:00
|
|
|
Body->Section = Cmd->Expression.Section();
|
|
|
|
Body->Value = Cmd->Expression(Value) - Body->Section->Addr;
|
2016-09-07 15:08:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym);
|
2016-10-31 22:44:41 +08:00
|
|
|
Body->Value = Cmd->Expression(Value);
|
2016-09-07 15:08:43 +08:00
|
|
|
}
|
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
template <class ELFT> static bool isTbss(OutputSectionBase *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
|
|
|
}
|
|
|
|
|
2016-09-16 23:10:23 +08:00
|
|
|
template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) {
|
|
|
|
if (!AlreadyOutputIS.insert(S).second)
|
|
|
|
return;
|
2016-11-10 07:23:45 +08:00
|
|
|
bool IsTbss = isTbss<ELFT>(CurOutSec);
|
2016-09-16 23:10:23 +08:00
|
|
|
|
|
|
|
uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
|
|
|
|
Pos = alignTo(Pos, S->Alignment);
|
2016-11-09 09:42:41 +08:00
|
|
|
S->OutSecOff = Pos - CurOutSec->Addr;
|
2016-09-16 23:10:23 +08:00
|
|
|
Pos += S->getSize();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2016-09-22 20:00:08 +08:00
|
|
|
if (IsTbss)
|
|
|
|
ThreadBssOffset = Pos - Dot;
|
|
|
|
else
|
2016-09-16 23:10:23 +08:00
|
|
|
Dot = Pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void LinkerScript<ELFT>::flush() {
|
2016-09-24 04:10:47 +08:00
|
|
|
if (!CurOutSec || !AlreadyOutputOS.insert(CurOutSec).second)
|
|
|
|
return;
|
|
|
|
if (auto *OutSec = dyn_cast<OutputSection<ELFT>>(CurOutSec)) {
|
2016-09-16 23:10:23 +08:00
|
|
|
for (InputSection<ELFT> *I : OutSec->Sections)
|
|
|
|
output(I);
|
2016-09-24 04:10:47 +08:00
|
|
|
} else {
|
2016-11-09 09:42:41 +08:00
|
|
|
Dot += CurOutSec->Size;
|
2016-08-31 16:13:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-11-10 07:23:45 +08:00
|
|
|
void LinkerScript<ELFT>::switchTo(OutputSectionBase *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
|
|
|
flush();
|
|
|
|
CurOutSec = Sec;
|
|
|
|
|
2016-11-09 09:42:41 +08:00
|
|
|
Dot = alignTo(Dot, CurOutSec->Addralign);
|
2016-11-10 07:23:45 +08:00
|
|
|
CurOutSec->Addr = isTbss<ELFT>(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
|
|
|
|
CurOutSec->setLMAOffset(LMAOffset);
|
2016-09-16 23:10:23 +08:00
|
|
|
}
|
2016-09-01 17:55:57 +08:00
|
|
|
|
2016-09-16 23:10:23 +08:00
|
|
|
template <class ELFT> void LinkerScript<ELFT>::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)) {
|
|
|
|
if (AssignCmd->Name == ".") {
|
|
|
|
// Update to location counter means update to section size.
|
2016-12-15 15:27:28 +08:00
|
|
|
uintX_t Val = AssignCmd->Expression(Dot);
|
|
|
|
if (Val < Dot)
|
|
|
|
error("unable to move location counter backward for: " +
|
|
|
|
CurOutSec->Name);
|
|
|
|
Dot = Val;
|
2016-11-09 09:42:41 +08:00
|
|
|
CurOutSec->Size = Dot - CurOutSec->Addr;
|
2016-09-16 23:10:23 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-16 17:49:39 +08:00
|
|
|
assignSectionSymbol<ELFT>(AssignCmd, Dot);
|
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)) {
|
|
|
|
AssertCmd->Expression(Dot);
|
|
|
|
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);
|
|
|
|
for (InputSectionData *ID : 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.
|
|
|
|
if (auto *Sec = dyn_cast<SyntheticSection<ELFT>>(ID))
|
|
|
|
if (Sec->empty())
|
|
|
|
continue;
|
|
|
|
|
2016-09-16 23:10:23 +08:00
|
|
|
auto *IB = static_cast<InputSectionBase<ELFT> *>(ID);
|
|
|
|
switchTo(IB->OutSec);
|
|
|
|
if (auto *I = dyn_cast<InputSection<ELFT>>(IB))
|
|
|
|
output(I);
|
2016-09-24 04:10:47 +08:00
|
|
|
else
|
|
|
|
flush();
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 04:38:20 +08:00
|
|
|
template <class ELFT>
|
2016-11-10 07:23:45 +08:00
|
|
|
static std::vector<OutputSectionBase *>
|
|
|
|
findSections(StringRef Name, const std::vector<OutputSectionBase *> &Sections) {
|
|
|
|
std::vector<OutputSectionBase *> Ret;
|
|
|
|
for (OutputSectionBase *Sec : Sections)
|
2016-10-10 19:23:12 +08:00
|
|
|
if (Sec->getName() == Name)
|
2016-09-07 18:46:07 +08:00
|
|
|
Ret.push_back(Sec);
|
|
|
|
return Ret;
|
2016-08-13 04:38:20 +08:00
|
|
|
}
|
|
|
|
|
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); }").
|
2016-09-16 23:10:23 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
|
2016-10-06 17:39:28 +08:00
|
|
|
if (Cmd->LMAExpr)
|
|
|
|
LMAOffset = Cmd->LMAExpr(Dot) - Dot;
|
2016-11-10 07:23:45 +08:00
|
|
|
std::vector<OutputSectionBase *> Sections =
|
|
|
|
findSections<ELFT>(Cmd->Name, *OutputSections);
|
2016-09-16 23:10:23 +08:00
|
|
|
if (Sections.empty())
|
|
|
|
return;
|
|
|
|
switchTo(Sections[0]);
|
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-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Base : Sections)
|
2016-09-16 23:10:23 +08:00
|
|
|
switchTo(Base);
|
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
|
|
|
}
|
|
|
|
|
2016-11-14 22:23:35 +08:00
|
|
|
template <class ELFT> void LinkerScript<ELFT>::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()))
|
|
|
|
return findSections<ELFT>(Cmd->Name, *OutputSections).empty();
|
|
|
|
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
|
|
|
|
2016-11-14 22:33:49 +08:00
|
|
|
template <class ELFT> void LinkerScript<ELFT>::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.
|
|
|
|
uintX_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;
|
2016-11-10 07:23:45 +08:00
|
|
|
std::vector<OutputSectionBase *> Secs =
|
|
|
|
findSections<ELFT>(Cmd->Name, *OutputSections);
|
2016-09-22 22:40:50 +08:00
|
|
|
if (!Secs.empty()) {
|
2016-11-09 09:42:41 +08:00
|
|
|
Flags = Secs[0]->Flags;
|
|
|
|
Type = Secs[0]->Type;
|
2016-09-22 22:40:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-14 22:33:49 +08:00
|
|
|
if (isAllSectionDescription(*Cmd))
|
|
|
|
continue;
|
|
|
|
|
2016-11-02 07:09:07 +08:00
|
|
|
auto *OutSec = make<OutputSection<ELFT>>(Cmd->Name, Type, Flags);
|
2016-09-22 22:40:50 +08:00
|
|
|
OutputSections->push_back(OutSec);
|
|
|
|
}
|
2016-11-14 23:39:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void LinkerScript<ELFT>::adjustSectionsAfterSorting() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-14 22:13:32 +08:00
|
|
|
// Orphan sections are sections present in the input files which are not
|
|
|
|
// explicitly placed into the output file by the linker script. This just
|
|
|
|
// places them in the order already decided in OutputSections.
|
2016-12-15 23:38:09 +08:00
|
|
|
template <class ELFT> void LinkerScript<ELFT>::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;
|
|
|
|
}
|
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : *OutputSections) {
|
2016-04-16 18:10:32 +08:00
|
|
|
StringRef Name = Sec->getName();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-12-20 01:01:01 +08:00
|
|
|
void LinkerScript<ELFT>::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;
|
2016-04-16 18:10:32 +08:00
|
|
|
|
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())) {
|
2016-07-29 13:48:39 +08:00
|
|
|
if (Cmd->Name == ".") {
|
|
|
|
Dot = Cmd->Expression(Dot);
|
|
|
|
} else if (Cmd->Sym) {
|
2016-11-16 17:49:39 +08:00
|
|
|
assignSectionSymbol<ELFT>(Cmd, Dot);
|
2016-07-29 13:48:39 +08:00
|
|
|
}
|
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())) {
|
|
|
|
Cmd->Expression(Dot);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-21 14:43:01 +08:00
|
|
|
auto *Cmd = cast<OutputSectionCommand>(Base.get());
|
2016-09-16 23:10:23 +08:00
|
|
|
if (Cmd->AddrExpr)
|
|
|
|
Dot = Cmd->AddrExpr(Dot);
|
|
|
|
assignOffsets(Cmd);
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
2016-07-01 18:42:25 +08:00
|
|
|
|
2016-09-17 05:29:07 +08:00
|
|
|
uintX_t MinVA = std::numeric_limits<uintX_t>::max();
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *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);
|
2016-09-17 05:29:07 +08:00
|
|
|
else
|
2016-11-09 09:42:41 +08:00
|
|
|
Sec->Addr = 0;
|
2016-09-17 05:29:07 +08:00
|
|
|
}
|
|
|
|
|
2016-09-23 00:47:21 +08:00
|
|
|
uintX_t HeaderSize = getHeaderSize();
|
2016-11-22 03:59:33 +08:00
|
|
|
// If the linker script doesn't have PHDRS, add ElfHeader and ProgramHeaders
|
|
|
|
// now that we know we have space.
|
2016-12-20 05:21:07 +08:00
|
|
|
if (HeaderSize <= MinVA && !hasPhdrsCommands())
|
|
|
|
allocateHeaders<ELFT>(Phdrs, *OutputSections);
|
2016-11-22 03:59:33 +08:00
|
|
|
|
|
|
|
// ELF and Program headers need to be right before the first section in
|
|
|
|
// memory. Set their addresses accordingly.
|
2016-11-22 04:20:04 +08:00
|
|
|
MinVA = alignDown(MinVA - HeaderSize, Config->MaxPageSize);
|
2016-11-22 03:59:33 +08:00
|
|
|
Out<ELFT>::ElfHeader->Addr = MinVA;
|
|
|
|
Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + 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.
|
2016-12-20 01:01:01 +08:00
|
|
|
template <class ELFT> std::vector<PhdrEntry> LinkerScript<ELFT>::createPhdrs() {
|
|
|
|
std::vector<PhdrEntry> Ret;
|
2016-07-19 17:25:43 +08:00
|
|
|
|
2016-08-22 12:55:20 +08:00
|
|
|
// Process PHDRS and FILEHDR keywords because they are not
|
|
|
|
// real output sections and cannot be added in the following loop.
|
2016-07-19 17:25:43 +08:00
|
|
|
for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
|
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)
|
2016-07-23 22:18:48 +08:00
|
|
|
Phdr.add(Out<ELFT>::ElfHeader);
|
2016-07-19 17:25:43 +08:00
|
|
|
if (Cmd.HasPhdrs)
|
2016-07-23 22:18:48 +08:00
|
|
|
Phdr.add(Out<ELFT>::ProgramHeaders);
|
2016-09-09 17:46:16 +08:00
|
|
|
|
|
|
|
if (Cmd.LMAExpr) {
|
2016-12-20 01:01:01 +08:00
|
|
|
Phdr.p_paddr = Cmd.LMAExpr(0);
|
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.
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *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
|
2016-11-14 23:39:38 +08:00
|
|
|
for (size_t Id : getPhdrIndices(Sec->getName())) {
|
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
|
|
|
}
|
|
|
|
|
2016-08-16 14:40:58 +08:00
|
|
|
template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:38:09 +08:00
|
|
|
template <class ELFT> uint32_t LinkerScript<ELFT>::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()))
|
2016-12-09 07:21:30 +08:00
|
|
|
writeInt<ELFT>(Buf + Data->Offset, Data->Expression(0), Data->Size);
|
2016-09-27 03:22:50 +08:00
|
|
|
}
|
|
|
|
|
2016-10-06 17:39:28 +08:00
|
|
|
template <class ELFT> bool LinkerScript<ELFT>::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.
|
2016-07-21 14:43:01 +08:00
|
|
|
template <class ELFT> int LinkerScript<ELFT>::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-07-19 17:25:43 +08:00
|
|
|
template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
|
|
|
|
return !Opt.PhdrsCommands.empty();
|
|
|
|
}
|
|
|
|
|
2016-08-30 17:54:01 +08:00
|
|
|
template <class ELFT>
|
2016-11-28 17:58:04 +08:00
|
|
|
const OutputSectionBase *LinkerScript<ELFT>::getOutputSection(const Twine &Loc,
|
|
|
|
StringRef Name) {
|
2016-11-16 17:49:39 +08:00
|
|
|
static OutputSectionBase FakeSec("", 0, 0);
|
2016-07-30 00:18:47 +08:00
|
|
|
|
2016-11-10 07:23:45 +08:00
|
|
|
for (OutputSectionBase *Sec : *OutputSections)
|
2016-09-08 17:08:30 +08:00
|
|
|
if (Sec->getName() == Name)
|
2016-11-16 17:49:39 +08:00
|
|
|
return Sec;
|
2016-11-28 17:58:04 +08:00
|
|
|
|
|
|
|
error(Loc + ": undefined section " + Name);
|
2016-11-16 17:49:39 +08:00
|
|
|
return &FakeSec;
|
2016-09-08 17:08:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-18 04:27:10 +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().
|
|
|
|
template <class ELFT>
|
|
|
|
uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
|
|
|
|
for (OutputSectionBase *Sec : *OutputSections)
|
|
|
|
if (Sec->getName() == Name)
|
|
|
|
return Sec->Size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-08 16:19:13 +08:00
|
|
|
template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
|
2016-09-23 00:47:21 +08:00
|
|
|
return elf::getHeaderSize<ELFT>();
|
2016-08-10 15:59:34 +08:00
|
|
|
}
|
|
|
|
|
2016-12-22 21:13:12 +08:00
|
|
|
template <class ELFT>
|
|
|
|
uint64_t LinkerScript<ELFT>::getSymbolValue(const Twine &Loc, StringRef S) {
|
2016-09-08 16:19:13 +08:00
|
|
|
if (SymbolBody *B = Symtab<ELFT>::X->find(S))
|
|
|
|
return B->getVA<ELFT>();
|
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-11-01 03:56:37 +08:00
|
|
|
template <class ELFT> bool LinkerScript<ELFT>::isAbsolute(StringRef S) {
|
|
|
|
SymbolBody *Sym = Symtab<ELFT>::X->find(S);
|
|
|
|
auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym);
|
|
|
|
return DR && !DR->Section;
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:49:39 +08:00
|
|
|
// Gets section symbol belongs to. Symbol "." doesn't belong to any
|
|
|
|
// specific section but isn't absolute at the same time, so we try
|
|
|
|
// to find suitable section for it as well.
|
|
|
|
template <class ELFT>
|
|
|
|
const OutputSectionBase *LinkerScript<ELFT>::getSymbolSection(StringRef S) {
|
|
|
|
SymbolBody *Sym = Symtab<ELFT>::X->find(S);
|
|
|
|
if (!Sym) {
|
|
|
|
if (OutputSections->empty())
|
|
|
|
return nullptr;
|
|
|
|
return CurOutSec ? CurOutSec : (*OutputSections)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym))
|
|
|
|
return DR->Section ? DR->Section->OutSec : nullptr;
|
2016-12-21 16:40:09 +08:00
|
|
|
if (auto *DS = dyn_cast_or_null<DefinedSynthetic>(Sym))
|
2016-11-16 17:49:39 +08:00
|
|
|
return DS->Section;
|
|
|
|
|
|
|
|
return 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.
|
|
|
|
template <class ELFT>
|
2016-07-25 07:47:31 +08:00
|
|
|
std::vector<size_t> LinkerScript<ELFT>::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
|
|
|
}
|
|
|
|
|
2016-07-26 08:27:36 +08:00
|
|
|
template <class ELFT>
|
2016-12-06 00:38:32 +08:00
|
|
|
size_t LinkerScript<ELFT>::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;
|
|
|
|
}
|
|
|
|
|
2016-11-21 23:49:56 +08:00
|
|
|
class elf::ScriptParser final : public ScriptParserBase {
|
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)
|
|
|
|
: ScriptParserBase(MB),
|
|
|
|
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();
|
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
|
|
|
|
|
|
|
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);
|
2016-11-18 14:30:09 +08:00
|
|
|
std::vector<SymbolVersion> readSymbols();
|
2017-01-11 00:37:24 +08:00
|
|
|
void readLocals();
|
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();
|
|
|
|
} 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() {
|
2015-10-13 05:50:08 +08:00
|
|
|
// Error checking only for now.
|
|
|
|
expect("(");
|
2016-10-17 14:21:13 +08:00
|
|
|
skip();
|
2015-10-13 05:50:08 +08:00
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
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".
|
|
|
|
PhdrCmd.Flags = readExpr()(0);
|
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(")");
|
|
|
|
return [=](uint64_t Dot) {
|
|
|
|
uint64_t V = E(Dot);
|
|
|
|
if (!V)
|
|
|
|
error(Msg);
|
|
|
|
return V;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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();
|
2016-11-23 02:01:50 +08:00
|
|
|
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(";");
|
|
|
|
} 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
|
|
|
}
|
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-12-22 21:13:12 +08:00
|
|
|
static uint64_t getSymbolValue(const Twine &Loc, StringRef S, uint64_t Dot) {
|
2016-07-29 05:08:56 +08:00
|
|
|
if (S == ".")
|
|
|
|
return Dot;
|
2016-12-22 21:13:12 +08:00
|
|
|
return ScriptBase->getSymbolValue(Loc, S);
|
2016-08-10 15:59:34 +08:00
|
|
|
}
|
|
|
|
|
2016-11-01 03:56:37 +08:00
|
|
|
static bool isAbsolute(StringRef S) {
|
|
|
|
if (S == ".")
|
|
|
|
return false;
|
|
|
|
return ScriptBase->isAbsolute(S);
|
|
|
|
}
|
|
|
|
|
2016-07-29 05:08:56 +08:00
|
|
|
SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
|
|
|
|
StringRef Op = next();
|
2016-09-07 15:08:43 +08:00
|
|
|
Expr E;
|
2016-07-29 05:08:56 +08:00
|
|
|
assert(Op == "=" || Op == "+=");
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("ABSOLUTE")) {
|
2016-10-20 07:11:21 +08:00
|
|
|
// The RHS may be something like "ABSOLUTE(.) & 0xff".
|
|
|
|
// Call readExpr1 to read the whole expression.
|
|
|
|
E = readExpr1(readParenExpr(), 0);
|
2016-11-18 14:49:09 +08:00
|
|
|
E.IsAbsolute = [] { return true; };
|
2016-09-07 15:08:43 +08:00
|
|
|
} else {
|
|
|
|
E = readExpr();
|
|
|
|
}
|
2016-12-22 21:13:12 +08:00
|
|
|
if (Op == "+=") {
|
|
|
|
std::string Loc = getCurrentLocation();
|
|
|
|
E = [=](uint64_t Dot) {
|
|
|
|
return getSymbolValue(Loc, Name, Dot) + E(Dot);
|
|
|
|
};
|
|
|
|
}
|
2016-11-01 01:43:38 +08:00
|
|
|
return new SymbolAssignment(Name, E);
|
2016-07-29 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is an operator-precedence parser to parse a linker
|
|
|
|
// script expression.
|
|
|
|
Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
|
|
|
|
|
2016-08-05 09:04:59 +08:00
|
|
|
static Expr combine(StringRef Op, Expr L, Expr R) {
|
|
|
|
if (Op == "*")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
|
|
|
|
if (Op == "/") {
|
|
|
|
return [=](uint64_t Dot) -> uint64_t {
|
|
|
|
uint64_t RHS = R(Dot);
|
|
|
|
if (RHS == 0) {
|
|
|
|
error("division by zero");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return L(Dot) / RHS;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (Op == "+")
|
2016-11-01 03:56:37 +08:00
|
|
|
return {[=](uint64_t Dot) { return L(Dot) + R(Dot); },
|
2016-11-18 14:49:09 +08:00
|
|
|
[=] { return L.IsAbsolute() && R.IsAbsolute(); },
|
|
|
|
[=] {
|
2016-11-16 17:49:39 +08:00
|
|
|
const OutputSectionBase *S = L.Section();
|
|
|
|
return S ? S : R.Section();
|
|
|
|
}};
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == "-")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
|
2016-09-23 21:13:55 +08:00
|
|
|
if (Op == "<<")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) << R(Dot); };
|
|
|
|
if (Op == ">>")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) >> R(Dot); };
|
2016-08-05 09:04:59 +08:00
|
|
|
if (Op == "<")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
|
|
|
|
if (Op == ">")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
|
|
|
|
if (Op == ">=")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
|
|
|
|
if (Op == "<=")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
|
|
|
|
if (Op == "==")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
|
|
|
|
if (Op == "!=")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
|
|
|
|
if (Op == "&")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
|
2016-08-23 05:33:35 +08:00
|
|
|
if (Op == "|")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
|
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();
|
|
|
|
return [=](uint64_t Dot) { return ~E(Dot); };
|
|
|
|
}
|
|
|
|
if (Tok == "-") {
|
|
|
|
Expr E = readPrimary();
|
|
|
|
return [=](uint64_t Dot) { return -E(Dot); };
|
|
|
|
}
|
|
|
|
|
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.
|
2016-08-30 17:54:01 +08:00
|
|
|
if (Tok == "ADDR") {
|
2016-10-06 17:39:28 +08:00
|
|
|
StringRef Name = readParenLiteral();
|
2016-11-28 17:58:04 +08:00
|
|
|
return {[=](uint64_t Dot) {
|
|
|
|
return ScriptBase->getOutputSection(Location, Name)->Addr;
|
|
|
|
},
|
|
|
|
[=] { return false; },
|
|
|
|
[=] { return ScriptBase->getOutputSection(Location, Name); }};
|
2016-08-30 17:54:01 +08:00
|
|
|
}
|
2016-10-06 17:39:28 +08:00
|
|
|
if (Tok == "LOADADDR") {
|
|
|
|
StringRef Name = readParenLiteral();
|
2016-11-16 17:49:39 +08:00
|
|
|
return [=](uint64_t Dot) {
|
2016-11-28 17:58:04 +08:00
|
|
|
return ScriptBase->getOutputSection(Location, Name)->getLMA();
|
2016-11-16 17:49:39 +08:00
|
|
|
};
|
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(")");
|
|
|
|
return [=](uint64_t Dot) { return alignTo(E(Dot), E2(Dot)); };
|
|
|
|
}
|
|
|
|
expect(")");
|
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 [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
|
|
|
|
}
|
|
|
|
if (Tok == "CONSTANT") {
|
2016-10-06 17:39:28 +08:00
|
|
|
StringRef Name = readParenLiteral();
|
2016-11-01 05:36:23 +08:00
|
|
|
return [=](uint64_t Dot) { 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();
|
|
|
|
return [=](uint64_t Dot) { 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(")");
|
2016-09-18 02:14:56 +08:00
|
|
|
return [=](uint64_t Dot) { return E(Dot); };
|
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(")");
|
2016-07-27 03:34:10 +08:00
|
|
|
return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
|
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(")");
|
|
|
|
return [](uint64_t Dot) { return Dot; };
|
|
|
|
}
|
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(")");
|
|
|
|
return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
|
|
|
|
}
|
2016-07-30 00:18:47 +08:00
|
|
|
if (Tok == "SIZEOF") {
|
2016-10-06 17:39:28 +08:00
|
|
|
StringRef Name = readParenLiteral();
|
2016-11-18 04:27:10 +08:00
|
|
|
return [=](uint64_t Dot) { 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();
|
2016-11-16 17:49:39 +08:00
|
|
|
return [=](uint64_t Dot) {
|
2016-11-28 17:58:04 +08:00
|
|
|
return ScriptBase->getOutputSection(Location, Name)->Addralign;
|
2016-11-16 17:49:39 +08:00
|
|
|
};
|
2016-09-08 17:08:30 +08:00
|
|
|
}
|
2016-08-10 15:59:34 +08:00
|
|
|
if (Tok == "SIZEOF_HEADERS")
|
2016-11-01 05:36:23 +08:00
|
|
|
return [=](uint64_t Dot) { return ScriptBase->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))
|
2016-11-01 05:36:23 +08:00
|
|
|
return [=](uint64_t Dot) { return V; };
|
2016-09-03 00:01:42 +08:00
|
|
|
|
|
|
|
// Tok is a symbol name.
|
|
|
|
if (Tok != "." && !isValidCIdentifier(Tok))
|
|
|
|
setError("malformed number: " + Tok);
|
2016-12-22 21:13:12 +08:00
|
|
|
return {[=](uint64_t Dot) { return getSymbolValue(Location, Tok, Dot); },
|
2016-11-18 14:49:09 +08:00
|
|
|
[=] { return isAbsolute(Tok); },
|
|
|
|
[=] { return ScriptBase->getSymbolSection(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();
|
|
|
|
return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-18 14:30:09 +08:00
|
|
|
// Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
|
|
|
|
void ScriptParser::readAnonymousDeclaration() {
|
|
|
|
// Read global symbols first. "global:" is default, so if there's
|
|
|
|
// no label, we assume global symbols.
|
|
|
|
if (consume("global:") || peek() != "local:")
|
|
|
|
Config->VersionScriptGlobals = readSymbols();
|
|
|
|
|
2017-01-11 00:37:24 +08:00
|
|
|
readLocals();
|
|
|
|
expect("}");
|
|
|
|
expect(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readLocals() {
|
|
|
|
if (!consume("local:"))
|
|
|
|
return;
|
|
|
|
std::vector<SymbolVersion> Locals = readSymbols();
|
|
|
|
for (SymbolVersion V : Locals) {
|
|
|
|
if (V.Name == "*") {
|
2016-11-18 14:30:09 +08:00
|
|
|
Config->DefaultSymbolVersion = VER_NDX_LOCAL;
|
2017-01-11 00:37:24 +08:00
|
|
|
continue;
|
2016-11-18 14:30:09 +08:00
|
|
|
}
|
2017-01-11 00:37:24 +08:00
|
|
|
Config->VersionScriptLocals.push_back(V);
|
2016-11-18 14:30:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads a list of symbols, e.g. "VerStr { global: foo; bar; local: *; };".
|
2016-09-01 04:03:54 +08:00
|
|
|
void ScriptParser::readVersionDeclaration(StringRef VerStr) {
|
2016-08-31 17:08:26 +08:00
|
|
|
// Identifiers start at 2 because 0 and 1 are reserved
|
|
|
|
// for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
|
2016-11-17 11:39:21 +08:00
|
|
|
uint16_t VersionId = Config->VersionDefinitions.size() + 2;
|
2016-08-31 17:08:26 +08:00
|
|
|
Config->VersionDefinitions.push_back({VerStr, VersionId});
|
|
|
|
|
2016-11-18 14:30:09 +08:00
|
|
|
// Read global symbols.
|
2016-10-18 00:01:53 +08:00
|
|
|
if (consume("global:") || peek() != "local:")
|
2016-11-18 14:30:09 +08:00
|
|
|
Config->VersionDefinitions.back().Globals = readSymbols();
|
|
|
|
|
2017-01-11 00:37:24 +08:00
|
|
|
readLocals();
|
2016-08-31 17:08:26 +08:00
|
|
|
expect("}");
|
|
|
|
|
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(";");
|
|
|
|
}
|
|
|
|
|
2016-11-18 14:30:09 +08:00
|
|
|
// Reads a list of symbols for a versions cript.
|
|
|
|
std::vector<SymbolVersion> ScriptParser::readSymbols() {
|
|
|
|
std::vector<SymbolVersion> Ret;
|
2016-11-17 01:59:10 +08:00
|
|
|
for (;;) {
|
2016-12-10 00:44:05 +08:00
|
|
|
if (consume("extern")) {
|
2016-11-18 14:30:09 +08:00
|
|
|
for (SymbolVersion V : readVersionExtern())
|
|
|
|
Ret.push_back(V);
|
2016-12-10 00:44:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-17 01:59:10 +08:00
|
|
|
|
2016-11-17 11:52:14 +08:00
|
|
|
if (peek() == "}" || peek() == "local:" || Error)
|
2016-11-18 14:30:09 +08:00
|
|
|
break;
|
2016-11-17 11:52:14 +08:00
|
|
|
StringRef Tok = next();
|
2016-11-18 14:30:09 +08:00
|
|
|
Ret.push_back({unquote(Tok), false, hasWildcard(Tok)});
|
2016-11-17 01:59:10 +08:00
|
|
|
expect(";");
|
|
|
|
}
|
2016-11-18 14:30:09 +08:00
|
|
|
return Ret;
|
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("}");
|
|
|
|
expect(";");
|
2016-11-18 14:30:09 +08:00
|
|
|
return Ret;
|
2016-08-31 17:08:26 +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>;
|