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-07-22 03:45:22 +08:00
|
|
|
// It parses a linker script and write the result to Config or ScriptConfig
|
|
|
|
// objects.
|
|
|
|
//
|
|
|
|
// If SECTIONS command is used, a ScriptConfig contains an AST
|
|
|
|
// of the command which will later be consumed by createSections() and
|
|
|
|
// assignAddresses().
|
2015-10-01 01:23:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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-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"
|
2016-07-12 14:39:48 +08:00
|
|
|
#include "Symbols.h"
|
2015-10-01 01:23:26 +08:00
|
|
|
#include "SymbolTable.h"
|
2016-07-01 18:27:36 +08:00
|
|
|
#include "Target.h"
|
2016-07-19 17:25:43 +08:00
|
|
|
#include "Writer.h"
|
2016-04-20 02:58:11 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2016-04-16 18:10:32 +08:00
|
|
|
#include "llvm/Support/ELF.h"
|
2015-10-01 01:23:26 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2015-10-13 08:09:21 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2015-10-11 09:53:04 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
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;
|
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-04-21 04:13:41 +08:00
|
|
|
ScriptConfiguration *elf::ScriptConfig;
|
2016-02-12 05:17:59 +08:00
|
|
|
|
2016-08-11 15:56:43 +08:00
|
|
|
template <class ELFT>
|
2016-08-12 07:22:52 +08:00
|
|
|
static void addRegular(SymbolAssignment *Cmd) {
|
|
|
|
Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, STB_GLOBAL, STV_DEFAULT);
|
|
|
|
Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
|
|
|
|
Cmd->Sym = Sym->body();
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-08-12 07:22:52 +08:00
|
|
|
static void addSynthetic(SymbolAssignment *Cmd,
|
|
|
|
OutputSectionBase<ELFT> *Section) {
|
|
|
|
Symbol *Sym = Symtab<ELFT>::X->addSynthetic(Cmd->Name, Section, 0);
|
|
|
|
Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
|
|
|
|
Cmd->Sym = Sym->body();
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 07:22:52 +08:00
|
|
|
// If a symbol was in PROVIDE(), we need to define it only when
|
|
|
|
// it is an undefined symbol.
|
|
|
|
template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) {
|
|
|
|
if (Cmd->Name == ".")
|
2016-08-11 15:56:43 +08:00
|
|
|
return false;
|
2016-08-12 07:22:52 +08:00
|
|
|
if (!Cmd->Provide)
|
|
|
|
return true;
|
|
|
|
SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
|
|
|
|
return B && B->isUndefined();
|
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-07-23 22:09:58 +08:00
|
|
|
template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
|
2016-07-21 22:26:59 +08:00
|
|
|
return !S || !S->Live;
|
2016-02-12 05:17:59 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 09:24:53 +08:00
|
|
|
template <class ELFT> LinkerScript<ELFT>::LinkerScript() {}
|
|
|
|
template <class ELFT> LinkerScript<ELFT>::~LinkerScript() {}
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
|
2016-04-22 06:00:51 +08:00
|
|
|
for (StringRef Pat : Opt.KeptSections)
|
2016-06-29 13:32:09 +08:00
|
|
|
if (globMatch(Pat, S->getSectionName()))
|
2016-04-22 06:00:51 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
2016-02-23 15:47:54 +08:00
|
|
|
}
|
|
|
|
|
2016-07-26 06:41:42 +08:00
|
|
|
static bool match(ArrayRef<StringRef> Patterns, StringRef S) {
|
|
|
|
for (StringRef Pat : Patterns)
|
|
|
|
if (globMatch(Pat, S))
|
2016-07-21 22:26:59 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-29 05:51:30 +08:00
|
|
|
static bool fileMatches(const InputSectionDescription *Desc,
|
|
|
|
StringRef Filename) {
|
|
|
|
if (!globMatch(Desc->FilePattern, Filename))
|
|
|
|
return false;
|
|
|
|
return Desc->ExcludedFiles.empty() || !match(Desc->ExcludedFiles, Filename);
|
|
|
|
}
|
|
|
|
|
2016-07-26 06:51:07 +08:00
|
|
|
// Returns input sections filtered by given glob patterns.
|
|
|
|
template <class ELFT>
|
|
|
|
std::vector<InputSectionBase<ELFT> *>
|
2016-07-29 05:05:04 +08:00
|
|
|
LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
|
2016-07-29 05:51:30 +08:00
|
|
|
ArrayRef<StringRef> Patterns = I->SectionPatterns;
|
2016-07-26 06:51:07 +08:00
|
|
|
std::vector<InputSectionBase<ELFT> *> Ret;
|
|
|
|
for (const std::unique_ptr<ObjectFile<ELFT>> &F :
|
2016-07-29 05:51:30 +08:00
|
|
|
Symtab<ELFT>::X->getObjectFiles()) {
|
|
|
|
if (fileMatches(I, sys::path::filename(F->getName())))
|
|
|
|
for (InputSectionBase<ELFT> *S : F->getSections())
|
|
|
|
if (!isDiscarded(S) && !S->OutSec &&
|
|
|
|
match(Patterns, S->getSectionName()))
|
2016-07-27 09:44:01 +08:00
|
|
|
Ret.push_back(S);
|
2016-07-29 05:51:30 +08:00
|
|
|
}
|
2016-07-29 03:24:13 +08:00
|
|
|
|
|
|
|
if ((llvm::find(Patterns, "COMMON") != Patterns.end()))
|
2016-07-29 05:05:04 +08:00
|
|
|
Ret.push_back(CommonInputSection<ELFT>::X);
|
2016-07-29 03:24:13 +08:00
|
|
|
|
2016-07-26 06:51:07 +08:00
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2016-08-12 05:00:02 +08:00
|
|
|
// You can define new symbols using linker scripts. For example,
|
|
|
|
// ".text { abc.o(.text); foo = .; def.o(.text); }" defines symbol
|
|
|
|
// foo just after abc.o's text section contents. This class is to
|
|
|
|
// handle such symbol definitions.
|
|
|
|
//
|
|
|
|
// In order to handle scripts like the above one, we want to
|
|
|
|
// keep symbol definitions in output sections. Because output sections
|
|
|
|
// can contain only input sections, we wrap symbol definitions
|
|
|
|
// with dummy input sections. This class serves that purpose.
|
2016-08-12 09:24:53 +08:00
|
|
|
template <class ELFT>
|
|
|
|
class elf::LayoutInputSection : public InputSectionBase<ELFT> {
|
2016-08-11 15:56:43 +08:00
|
|
|
public:
|
2016-08-12 09:24:53 +08:00
|
|
|
explicit LayoutInputSection(SymbolAssignment *Cmd);
|
2016-08-11 15:56:43 +08:00
|
|
|
static bool classof(const InputSectionBase<ELFT> *S);
|
|
|
|
SymbolAssignment *Cmd;
|
|
|
|
|
|
|
|
private:
|
|
|
|
typename ELFT::Shdr Hdr;
|
|
|
|
};
|
|
|
|
|
2016-08-12 11:16:56 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static InputSectionBase<ELFT> *
|
|
|
|
getNonLayoutSection(std::vector<InputSectionBase<ELFT> *> &Vec) {
|
|
|
|
for (InputSectionBase<ELFT> *S : Vec)
|
|
|
|
if (!isa<LayoutInputSection<ELFT>>(S))
|
|
|
|
return S;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-08-11 15:56:43 +08:00
|
|
|
|
|
|
|
template <class T> static T *zero(T *Val) {
|
|
|
|
memset(Val, 0, sizeof(*Val));
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
LayoutInputSection<ELFT>::LayoutInputSection(SymbolAssignment *Cmd)
|
2016-08-12 06:06:55 +08:00
|
|
|
: InputSectionBase<ELFT>(nullptr, zero(&Hdr),
|
|
|
|
InputSectionBase<ELFT>::Layout),
|
|
|
|
Cmd(Cmd) {
|
2016-08-11 15:56:43 +08:00
|
|
|
this->Live = true;
|
|
|
|
Hdr.sh_type = SHT_NOBITS;
|
|
|
|
}
|
|
|
|
|
2016-07-29 23:12:48 +08:00
|
|
|
template <class ELFT>
|
2016-08-11 15:56:43 +08:00
|
|
|
bool LayoutInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
|
|
|
|
return S->SectionKind == InputSectionBase<ELFT>::Layout;
|
|
|
|
}
|
|
|
|
|
2016-08-05 06:27:00 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static bool compareName(InputSectionBase<ELFT> *A, InputSectionBase<ELFT> *B) {
|
|
|
|
return A->getSectionName() < B->getSectionName();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
static bool compareAlignment(InputSectionBase<ELFT> *A,
|
|
|
|
InputSectionBase<ELFT> *B) {
|
|
|
|
// ">" 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
static std::function<bool(InputSectionBase<ELFT> *, InputSectionBase<ELFT> *)>
|
|
|
|
getComparator(SortKind K) {
|
|
|
|
if (K == SortByName)
|
|
|
|
return compareName<ELFT>;
|
|
|
|
return compareAlignment<ELFT>;
|
|
|
|
}
|
2016-07-29 23:32:46 +08:00
|
|
|
|
2016-07-20 22:43:20 +08:00
|
|
|
template <class ELFT>
|
2016-08-12 08:27:23 +08:00
|
|
|
void LinkerScript<ELFT>::discard(OutputSectionCommand &Cmd) {
|
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Cmd.Commands) {
|
|
|
|
if (auto *Cmd = dyn_cast<InputSectionDescription>(Base.get())) {
|
|
|
|
for (InputSectionBase<ELFT> *S : getInputSections(Cmd)) {
|
2016-07-26 06:51:07 +08:00
|
|
|
S->Live = false;
|
|
|
|
reportDiscarded(S);
|
2016-07-21 22:26:59 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-08-12 11:16:56 +08:00
|
|
|
std::vector<InputSectionBase<ELFT> *>
|
|
|
|
LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &Cmd) {
|
|
|
|
std::vector<InputSectionBase<ELFT> *> Ret;
|
|
|
|
|
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Cmd.Commands) {
|
|
|
|
if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
|
|
|
|
Ret.push_back(new (LAlloc.Allocate()) LayoutInputSection<ELFT>(Cmd));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *Cmd = cast<InputSectionDescription>(Base.get());
|
|
|
|
std::vector<InputSectionBase<ELFT> *> V = getInputSections(Cmd);
|
|
|
|
if (Cmd->SortInner)
|
|
|
|
std::stable_sort(V.begin(), V.end(), getComparator<ELFT>(Cmd->SortInner));
|
|
|
|
if (Cmd->SortOuter)
|
|
|
|
std::stable_sort(V.begin(), V.end(), getComparator<ELFT>(Cmd->SortOuter));
|
|
|
|
Ret.insert(Ret.end(), V.begin(), V.end());
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
|
2016-08-12 11:16:56 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
|
2016-08-12 08:27:23 +08:00
|
|
|
for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) {
|
2016-08-11 15:56:43 +08:00
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
|
2016-08-12 08:27:23 +08:00
|
|
|
if (Cmd->Name == "/DISCARD/") {
|
|
|
|
discard(*Cmd);
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-12 11:16:56 +08:00
|
|
|
|
|
|
|
std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
|
|
|
|
InputSectionBase<ELFT> *Head = getNonLayoutSection<ELFT>(V);
|
|
|
|
if (!Head)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
OutputSectionBase<ELFT> *OutSec;
|
|
|
|
bool IsNew;
|
|
|
|
std::tie(OutSec, IsNew) = Factory.create(Head, Cmd->Name);
|
|
|
|
if (IsNew)
|
|
|
|
OutputSections->push_back(OutSec);
|
|
|
|
|
|
|
|
for (InputSectionBase<ELFT> *Sec : V) {
|
|
|
|
if (auto *L = dyn_cast<LayoutInputSection<ELFT>>(Sec)) {
|
|
|
|
if (shouldDefine<ELFT>(L->Cmd))
|
|
|
|
addSynthetic<ELFT>(L->Cmd, OutSec);
|
|
|
|
else if (L->Cmd->Name != ".")
|
|
|
|
continue;
|
2016-08-12 08:27:23 +08:00
|
|
|
}
|
2016-08-12 11:16:56 +08:00
|
|
|
OutSec->addSection(Sec);
|
2016-08-12 08:27:23 +08:00
|
|
|
}
|
2016-08-11 15:56:43 +08:00
|
|
|
} else if (auto *Cmd2 = dyn_cast<SymbolAssignment>(Base1.get())) {
|
2016-08-12 07:22:52 +08:00
|
|
|
if (shouldDefine<ELFT>(Cmd2))
|
|
|
|
addRegular<ELFT>(Cmd2);
|
2016-08-11 15:56:43 +08:00
|
|
|
}
|
2016-08-12 08:27:23 +08:00
|
|
|
}
|
2016-07-20 22:43:20 +08:00
|
|
|
|
2016-08-12 11:16:56 +08:00
|
|
|
// Add orphan sections.
|
2016-07-26 06:51:07 +08:00
|
|
|
for (const std::unique_ptr<ObjectFile<ELFT>> &F :
|
2016-08-12 11:16:56 +08:00
|
|
|
Symtab<ELFT>::X->getObjectFiles()) {
|
|
|
|
for (InputSectionBase<ELFT> *S : F->getSections()) {
|
|
|
|
if (!isDiscarded(S) && !S->OutSec) {
|
|
|
|
OutputSectionBase<ELFT> *OutSec;
|
|
|
|
bool IsNew;
|
|
|
|
std::tie(OutSec, IsNew) = Factory.create(S, getOutputSectionName(S));
|
|
|
|
if (IsNew)
|
|
|
|
OutputSections->push_back(OutSec);
|
|
|
|
OutSec->addSection(S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 22:43:20 +08:00
|
|
|
|
2016-07-26 05:30:00 +08:00
|
|
|
// Remove from the output all the sections which did not meet
|
|
|
|
// the optional constraints.
|
2016-07-30 00:18:47 +08:00
|
|
|
filter();
|
2016-07-26 05:30:00 +08:00
|
|
|
}
|
|
|
|
|
2016-08-04 16:20:23 +08:00
|
|
|
template <class R, class T>
|
|
|
|
static inline void removeElementsIf(R &Range, const T &Pred) {
|
|
|
|
Range.erase(std::remove_if(Range.begin(), Range.end(), Pred), Range.end());
|
|
|
|
}
|
|
|
|
|
2016-07-26 05:30:00 +08:00
|
|
|
// Process ONLY_IF_RO and ONLY_IF_RW.
|
2016-07-30 00:18:47 +08:00
|
|
|
template <class ELFT> void LinkerScript<ELFT>::filter() {
|
2016-07-26 05:30:00 +08:00
|
|
|
// In this loop, we remove output sections if they don't satisfy
|
|
|
|
// requested properties.
|
|
|
|
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
|
|
|
|
auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
|
|
|
|
if (!Cmd || Cmd->Name == "/DISCARD/")
|
|
|
|
continue;
|
|
|
|
|
2016-07-26 18:47:09 +08:00
|
|
|
if (Cmd->Constraint == ConstraintKind::NoConstraint)
|
|
|
|
continue;
|
|
|
|
|
2016-08-05 09:05:01 +08:00
|
|
|
bool RO = (Cmd->Constraint == ConstraintKind::ReadOnly);
|
|
|
|
bool RW = (Cmd->Constraint == ConstraintKind::ReadWrite);
|
|
|
|
|
2016-08-04 16:20:23 +08:00
|
|
|
removeElementsIf(*OutputSections, [&](OutputSectionBase<ELFT> *S) {
|
|
|
|
bool Writable = (S->getFlags() & SHF_WRITE);
|
|
|
|
return S->getName() == Cmd->Name &&
|
|
|
|
((RO && Writable) || (RW && !Writable));
|
|
|
|
});
|
2016-07-26 05:30:00 +08:00
|
|
|
}
|
2016-07-20 22:43:20 +08:00
|
|
|
}
|
|
|
|
|
2016-08-11 15:56:43 +08:00
|
|
|
template <class ELFT> void assignOffsets(OutputSectionBase<ELFT> *Sec) {
|
|
|
|
auto *OutSec = dyn_cast<OutputSection<ELFT>>(Sec);
|
2016-08-12 08:55:08 +08:00
|
|
|
if (!OutSec) {
|
|
|
|
Sec->assignOffsets();
|
2016-08-11 15:56:43 +08:00
|
|
|
return;
|
2016-08-12 08:55:08 +08:00
|
|
|
}
|
2016-08-11 15:56:43 +08:00
|
|
|
|
|
|
|
typedef typename ELFT::uint uintX_t;
|
|
|
|
uintX_t Off = 0;
|
|
|
|
|
|
|
|
for (InputSection<ELFT> *I : OutSec->Sections) {
|
|
|
|
if (auto *L = dyn_cast<LayoutInputSection<ELFT>>(I)) {
|
|
|
|
uintX_t Value = L->Cmd->Expression(Sec->getVA() + Off) - Sec->getVA();
|
|
|
|
if (L->Cmd->Name == ".")
|
|
|
|
Off = Value;
|
|
|
|
else
|
|
|
|
cast<DefinedSynthetic<ELFT>>(L->Cmd->Sym)->Value = Value;
|
|
|
|
} else {
|
|
|
|
Off = alignTo(Off, I->Alignment);
|
|
|
|
I->OutSecOff = Off;
|
|
|
|
Off += I->getSize();
|
|
|
|
}
|
2016-08-12 05:30:42 +08:00
|
|
|
// Update section size inside for-loop, so that SIZEOF
|
2016-08-11 15:56:43 +08:00
|
|
|
// works correctly in the case below:
|
|
|
|
// .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
|
|
|
|
Sec->setSize(Off);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 20:13:05 +08:00
|
|
|
template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
|
2016-04-16 18:10:32 +08:00
|
|
|
// Orphan sections are sections present in the input files which
|
2016-04-19 05:00:40 +08:00
|
|
|
// are not explicitly placed into the output file by the linker script.
|
|
|
|
// We place orphan sections at end of file.
|
|
|
|
// Other linkers places them using some heuristics as described in
|
2016-04-16 18:10:32 +08:00
|
|
|
// https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
|
2016-08-12 08:36:56 +08:00
|
|
|
for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
|
2016-04-16 18:10:32 +08:00
|
|
|
StringRef Name = Sec->getName();
|
2016-04-22 04:30:00 +08:00
|
|
|
if (getSectionIndex(Name) == INT_MAX)
|
2016-07-21 14:43:01 +08:00
|
|
|
Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 05:00:40 +08:00
|
|
|
// Assign addresses as instructed by linker script SECTIONS sub-commands.
|
2016-08-10 15:59:34 +08:00
|
|
|
Dot = getSizeOfHeaders();
|
2016-07-01 18:27:36 +08:00
|
|
|
uintX_t MinVA = std::numeric_limits<uintX_t>::max();
|
2016-04-16 18:10:32 +08:00
|
|
|
uintX_t ThreadBssOffset = 0;
|
|
|
|
|
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) {
|
|
|
|
cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
|
|
|
|
}
|
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-05-20 02:15:54 +08:00
|
|
|
// Find all the sections with required name. There can be more than
|
2016-07-19 15:39:07 +08:00
|
|
|
// one section with such name, if the alignment, flags or type
|
2016-05-20 02:15:54 +08:00
|
|
|
// attribute differs.
|
2016-07-21 14:43:01 +08:00
|
|
|
auto *Cmd = cast<OutputSectionCommand>(Base.get());
|
2016-08-12 08:36:56 +08:00
|
|
|
for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
|
2016-07-21 14:43:01 +08:00
|
|
|
if (Sec->getName() != Cmd->Name)
|
2016-05-20 02:15:54 +08:00
|
|
|
continue;
|
2016-04-16 18:10:32 +08:00
|
|
|
|
2016-07-25 16:29:46 +08:00
|
|
|
if (Cmd->AddrExpr)
|
|
|
|
Dot = Cmd->AddrExpr(Dot);
|
|
|
|
|
2016-07-27 02:06:29 +08:00
|
|
|
if (Cmd->AlignExpr)
|
|
|
|
Sec->updateAlignment(Cmd->AlignExpr(Dot));
|
|
|
|
|
2016-05-20 02:15:54 +08:00
|
|
|
if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
|
|
|
|
uintX_t TVA = Dot + ThreadBssOffset;
|
2016-06-17 09:18:46 +08:00
|
|
|
TVA = alignTo(TVA, Sec->getAlignment());
|
2016-05-20 02:15:54 +08:00
|
|
|
Sec->setVA(TVA);
|
2016-08-11 15:56:43 +08:00
|
|
|
assignOffsets(Sec);
|
2016-05-20 02:15:54 +08:00
|
|
|
ThreadBssOffset = TVA - Dot + Sec->getSize();
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-16 18:10:32 +08:00
|
|
|
|
2016-05-20 02:15:54 +08:00
|
|
|
if (Sec->getFlags() & SHF_ALLOC) {
|
2016-06-17 09:18:46 +08:00
|
|
|
Dot = alignTo(Dot, Sec->getAlignment());
|
2016-05-20 02:15:54 +08:00
|
|
|
Sec->setVA(Dot);
|
2016-08-11 15:56:43 +08:00
|
|
|
assignOffsets(Sec);
|
2016-07-01 18:42:25 +08:00
|
|
|
MinVA = std::min(MinVA, Dot);
|
2016-05-20 02:15:54 +08:00
|
|
|
Dot += Sec->getSize();
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-12 08:55:08 +08:00
|
|
|
Sec->assignOffsets();
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-01 18:42:25 +08:00
|
|
|
|
2016-07-07 22:28:47 +08:00
|
|
|
// ELF and Program headers need to be right before the first section in
|
2016-07-19 15:42:07 +08:00
|
|
|
// memory. Set their addresses accordingly.
|
2016-07-01 18:27:36 +08:00
|
|
|
MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
|
|
|
|
Out<ELFT>::ProgramHeaders->getSize(),
|
|
|
|
Target->PageSize);
|
|
|
|
Out<ELFT>::ElfHeader->setVA(MinVA);
|
|
|
|
Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
|
2016-04-16 18:10:32 +08:00
|
|
|
}
|
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
template <class ELFT>
|
2016-08-04 20:13:05 +08:00
|
|
|
std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
|
|
|
|
ArrayRef<OutputSectionBase<ELFT> *> Sections = *OutputSections;
|
2016-07-25 07:47:31 +08:00
|
|
|
std::vector<PhdrEntry<ELFT>> Ret;
|
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);
|
|
|
|
PhdrEntry<ELFT> &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-07-19 17:25:43 +08:00
|
|
|
|
|
|
|
switch (Cmd.Type) {
|
|
|
|
case PT_INTERP:
|
2016-07-21 19:01:23 +08:00
|
|
|
if (Out<ELFT>::Interp)
|
2016-07-23 22:18:48 +08:00
|
|
|
Phdr.add(Out<ELFT>::Interp);
|
2016-07-19 17:25:43 +08:00
|
|
|
break;
|
|
|
|
case PT_DYNAMIC:
|
2016-08-09 12:42:01 +08:00
|
|
|
if (Out<ELFT>::DynSymTab) {
|
2016-07-27 22:10:56 +08:00
|
|
|
Phdr.H.p_flags = Out<ELFT>::Dynamic->getPhdrFlags();
|
2016-07-23 22:18:48 +08:00
|
|
|
Phdr.add(Out<ELFT>::Dynamic);
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PT_GNU_EH_FRAME:
|
|
|
|
if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) {
|
2016-07-27 22:10:56 +08:00
|
|
|
Phdr.H.p_flags = Out<ELFT>::EhFrameHdr->getPhdrFlags();
|
2016-07-23 22:18:48 +08:00
|
|
|
Phdr.add(Out<ELFT>::EhFrameHdr);
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 07:47:31 +08:00
|
|
|
PhdrEntry<ELFT> *Load = nullptr;
|
|
|
|
uintX_t Flags = PF_R;
|
2016-07-19 17:25:43 +08:00
|
|
|
for (OutputSectionBase<ELFT> *Sec : Sections) {
|
|
|
|
if (!(Sec->getFlags() & SHF_ALLOC))
|
|
|
|
break;
|
|
|
|
|
2016-07-25 07:47:31 +08:00
|
|
|
std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
|
2016-07-19 17:25:43 +08:00
|
|
|
if (!PhdrIds.empty()) {
|
|
|
|
// Assign headers specified by linker script
|
|
|
|
for (size_t Id : PhdrIds) {
|
2016-07-25 07:47:31 +08:00
|
|
|
Ret[Id].add(Sec);
|
2016-07-21 18:43:25 +08:00
|
|
|
if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
|
2016-07-27 22:10:56 +08:00
|
|
|
Ret[Id].H.p_flags |= Sec->getPhdrFlags();
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If we have no load segment or flags've changed then we want new load
|
|
|
|
// segment.
|
2016-07-27 22:10:56 +08:00
|
|
|
uintX_t NewFlags = Sec->getPhdrFlags();
|
2016-07-19 17:25:43 +08:00
|
|
|
if (Load == nullptr || Flags != NewFlags) {
|
2016-07-25 07:47:31 +08:00
|
|
|
Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
|
2016-07-19 17:25:43 +08:00
|
|
|
Flags = NewFlags;
|
|
|
|
}
|
2016-07-21 03:36:41 +08:00
|
|
|
Load->add(Sec);
|
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-04-21 04:13:41 +08:00
|
|
|
template <class ELFT>
|
|
|
|
ArrayRef<uint8_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;
|
|
|
|
return {};
|
2016-02-26 22:48:31 +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-07-26 08:21:15 +08:00
|
|
|
int I = 0;
|
|
|
|
for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
|
|
|
|
if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
|
|
|
|
if (Cmd->Name == Name)
|
|
|
|
return I;
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
return INT_MAX;
|
2016-04-21 18:22:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compartor to sort output sections. Returns -1 or 1 if
|
|
|
|
// A or B are mentioned in linker script. Otherwise, returns 0.
|
2016-04-21 04:13:41 +08:00
|
|
|
template <class ELFT>
|
|
|
|
int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
|
2016-04-22 04:30:00 +08:00
|
|
|
int I = getSectionIndex(A);
|
|
|
|
int J = getSectionIndex(B);
|
|
|
|
if (I == INT_MAX && J == INT_MAX)
|
2016-02-12 05:17:59 +08:00
|
|
|
return 0;
|
|
|
|
return I < J ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
|
|
|
|
return !Opt.PhdrsCommands.empty();
|
|
|
|
}
|
|
|
|
|
2016-07-30 00:18:47 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename ELFT::uint LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
|
|
|
|
for (OutputSectionBase<ELFT> *Sec : *OutputSections)
|
|
|
|
if (Sec->getName() == Name)
|
|
|
|
return Sec->getSize();
|
|
|
|
error("undefined section " + Name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-10 15:59:34 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename ELFT::uint LinkerScript<ELFT>::getSizeOfHeaders() {
|
|
|
|
return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
Ret.push_back(getPhdrIndex(PhdrName));
|
|
|
|
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>
|
|
|
|
size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
|
|
|
|
size_t I = 0;
|
|
|
|
for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
|
|
|
|
if (Cmd.Name == PhdrName)
|
|
|
|
return I;
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
error("section header '" + PhdrName + "' is not listed in PHDRS");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
class elf::ScriptParser : public ScriptParserBase {
|
2016-02-24 17:21:47 +08:00
|
|
|
typedef void (ScriptParser::*Handler)();
|
|
|
|
|
2015-10-01 01:23:26 +08:00
|
|
|
public:
|
2016-04-21 04:13:41 +08:00
|
|
|
ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
|
2016-02-19 18:45:45 +08:00
|
|
|
|
2016-04-23 06:59:24 +08:00
|
|
|
void run();
|
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();
|
2016-02-24 17:21:47 +08:00
|
|
|
void readNothing() {}
|
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-07-25 07:05:57 +08:00
|
|
|
SymbolAssignment *readAssignment(StringRef Name);
|
2016-08-04 10:03:27 +08:00
|
|
|
OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
|
2016-07-29 14:14:07 +08:00
|
|
|
std::vector<uint8_t> readOutputSectionFiller();
|
2016-07-19 17:25:43 +08:00
|
|
|
std::vector<StringRef> readOutputSectionPhdrs();
|
2016-08-04 10:03:27 +08:00
|
|
|
InputSectionDescription *readInputSectionDescription();
|
|
|
|
std::vector<StringRef> readInputFilePatterns();
|
|
|
|
InputSectionDescription *readInputSectionRules();
|
2016-07-19 17:25:43 +08:00
|
|
|
unsigned readPhdrType();
|
2016-08-05 06:27:00 +08:00
|
|
|
SortKind readSortKind();
|
2016-08-04 10:03:27 +08:00
|
|
|
SymbolAssignment *readProvide(bool Hidden);
|
2016-08-11 15:56:43 +08:00
|
|
|
SymbolAssignment *readProvideOrAssignment(StringRef Tok);
|
2016-08-04 10:03:27 +08:00
|
|
|
Expr readAlign();
|
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);
|
|
|
|
Expr readPrimary();
|
|
|
|
Expr readTernary(Expr Cond);
|
2015-10-01 01:23:26 +08:00
|
|
|
|
2016-02-24 17:21:47 +08:00
|
|
|
const static StringMap<Handler> Cmd;
|
2016-04-21 04:13:41 +08:00
|
|
|
ScriptConfiguration &Opt = *ScriptConfig;
|
|
|
|
StringSaver Saver = {ScriptConfig->Alloc};
|
2015-11-26 13:53:00 +08:00
|
|
|
bool IsUnderSysroot;
|
2015-10-01 01:23:26 +08:00
|
|
|
};
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
|
2016-02-24 17:21:47 +08:00
|
|
|
{"ENTRY", &ScriptParser::readEntry},
|
|
|
|
{"EXTERN", &ScriptParser::readExtern},
|
|
|
|
{"GROUP", &ScriptParser::readGroup},
|
|
|
|
{"INCLUDE", &ScriptParser::readInclude},
|
|
|
|
{"INPUT", &ScriptParser::readGroup},
|
|
|
|
{"OUTPUT", &ScriptParser::readOutput},
|
|
|
|
{"OUTPUT_ARCH", &ScriptParser::readOutputArch},
|
|
|
|
{"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
|
2016-07-19 17:25:43 +08:00
|
|
|
{"PHDRS", &ScriptParser::readPhdrs},
|
2016-02-24 17:21:47 +08:00
|
|
|
{"SEARCH_DIR", &ScriptParser::readSearchDir},
|
|
|
|
{"SECTIONS", &ScriptParser::readSections},
|
|
|
|
{";", &ScriptParser::readNothing}};
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::run() {
|
2015-10-01 01:23:26 +08:00
|
|
|
while (!atEOF()) {
|
|
|
|
StringRef Tok = next();
|
2016-02-24 17:21:47 +08:00
|
|
|
if (Handler Fn = Cmd.lookup(Tok))
|
|
|
|
(this->*Fn)();
|
|
|
|
else
|
2016-03-11 22:43:02 +08:00
|
|
|
setError("unknown directive: " + Tok);
|
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("/")) {
|
|
|
|
SmallString<128> Path;
|
|
|
|
(Config->Sysroot + S).toStringRef(Path);
|
|
|
|
if (sys::fs::exists(Path)) {
|
|
|
|
Driver->addFile(Saver.save(Path.str()));
|
|
|
|
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 {
|
|
|
|
std::string Path = findFromSearchPaths(S);
|
|
|
|
if (Path.empty())
|
2016-03-12 16:31:34 +08:00
|
|
|
setError("unable to find " + S);
|
2016-02-03 04:27:59 +08:00
|
|
|
else
|
|
|
|
Driver->addFile(Saver.save(Path));
|
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-08-05 09:25:45 +08:00
|
|
|
while (!Error && !skip(")"))
|
|
|
|
addFile(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-08-05 09:25:45 +08:00
|
|
|
while (!Error && !skip(")"))
|
|
|
|
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-08-05 09:25:45 +08:00
|
|
|
while (!Error && !skip(")")) {
|
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
|
|
|
|
addFile(Tok);
|
2015-10-01 01:23:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readInclude() {
|
2015-10-11 09:31:55 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
auto MBOrErr = MemoryBuffer::getFile(Tok);
|
2016-02-03 04:27:59 +08:00
|
|
|
if (!MBOrErr) {
|
2016-03-11 22:43:02 +08:00
|
|
|
setError("cannot open " + Tok);
|
2016-02-03 04:27:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-10-11 09:31:55 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
|
2015-10-11 09:53:04 +08:00
|
|
|
StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
|
|
|
|
std::vector<StringRef> V = tokenize(S);
|
2015-10-11 09:31:55 +08:00
|
|
|
Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
Config->OutputFile = Tok;
|
|
|
|
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("(");
|
|
|
|
next();
|
|
|
|
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("(");
|
|
|
|
next();
|
2015-10-13 05:08:41 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok == ")")
|
|
|
|
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;
|
|
|
|
}
|
2015-10-13 05:08:41 +08:00
|
|
|
next();
|
|
|
|
expect(",");
|
|
|
|
next();
|
2015-10-01 01:23:26 +08:00
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
void ScriptParser::readPhdrs() {
|
|
|
|
expect("{");
|
|
|
|
while (!Error && !skip("}")) {
|
|
|
|
StringRef Tok = next();
|
2016-07-21 18:43:25 +08:00
|
|
|
Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX});
|
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-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-03-09 01:13:12 +08:00
|
|
|
Config->SearchPaths.push_back(next());
|
2015-10-09 01:51:41 +08:00
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readSections() {
|
2016-07-29 11:31:09 +08:00
|
|
|
Opt.HasContents = true;
|
2015-11-12 17:52:08 +08:00
|
|
|
expect("{");
|
2016-04-16 18:10:32 +08:00
|
|
|
while (!Error && !skip("}")) {
|
2016-07-25 07:05:57 +08:00
|
|
|
StringRef Tok = next();
|
2016-08-11 15:56:43 +08:00
|
|
|
BaseCommand *Cmd = readProvideOrAssignment(Tok);
|
|
|
|
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)
|
|
|
|
.Case("*", 4)
|
|
|
|
.Case("/", 4)
|
|
|
|
.Case("+", 3)
|
|
|
|
.Case("-", 3)
|
|
|
|
.Case("<", 2)
|
|
|
|
.Case(">", 2)
|
|
|
|
.Case(">=", 2)
|
|
|
|
.Case("<=", 2)
|
|
|
|
.Case("==", 2)
|
|
|
|
.Case("!=", 2)
|
|
|
|
.Case("&", 1)
|
|
|
|
.Default(-1);
|
|
|
|
}
|
|
|
|
|
2016-08-04 10:03:27 +08:00
|
|
|
std::vector<StringRef> ScriptParser::readInputFilePatterns() {
|
|
|
|
std::vector<StringRef> V;
|
|
|
|
while (!Error && !skip(")"))
|
|
|
|
V.push_back(next());
|
|
|
|
return V;
|
2016-07-29 23:32:46 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 06:27:00 +08:00
|
|
|
SortKind ScriptParser::readSortKind() {
|
|
|
|
if (skip("SORT") || skip("SORT_BY_NAME"))
|
|
|
|
return SortByName;
|
|
|
|
if (skip("SORT_BY_ALIGNMENT"))
|
|
|
|
return SortByAlignment;
|
|
|
|
return SortNone;
|
|
|
|
}
|
|
|
|
|
2016-08-04 10:03:27 +08:00
|
|
|
InputSectionDescription *ScriptParser::readInputSectionRules() {
|
|
|
|
auto *Cmd = new InputSectionDescription;
|
|
|
|
Cmd->FilePattern = next();
|
2016-07-26 05:47:13 +08:00
|
|
|
expect("(");
|
2016-07-27 09:44:01 +08:00
|
|
|
|
2016-08-05 06:27:00 +08:00
|
|
|
// Read EXCLUDE_FILE().
|
2016-07-27 09:44:01 +08:00
|
|
|
if (skip("EXCLUDE_FILE")) {
|
|
|
|
expect("(");
|
|
|
|
while (!Error && !skip(")"))
|
2016-08-04 10:03:27 +08:00
|
|
|
Cmd->ExcludedFiles.push_back(next());
|
2016-07-29 05:51:30 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 06:27:00 +08:00
|
|
|
// Read SORT().
|
|
|
|
if (SortKind K1 = readSortKind()) {
|
|
|
|
Cmd->SortOuter = K1;
|
2016-07-29 23:32:46 +08:00
|
|
|
expect("(");
|
2016-08-05 06:27:00 +08:00
|
|
|
if (SortKind K2 = readSortKind()) {
|
|
|
|
Cmd->SortInner = K2;
|
2016-08-03 16:35:59 +08:00
|
|
|
expect("(");
|
2016-08-04 10:03:27 +08:00
|
|
|
Cmd->SectionPatterns = readInputFilePatterns();
|
2016-08-03 16:35:59 +08:00
|
|
|
expect(")");
|
|
|
|
} else {
|
2016-08-04 10:03:27 +08:00
|
|
|
Cmd->SectionPatterns = readInputFilePatterns();
|
2016-08-03 16:35:59 +08:00
|
|
|
}
|
2016-07-29 23:32:46 +08:00
|
|
|
expect(")");
|
2016-08-04 10:03:27 +08:00
|
|
|
return Cmd;
|
2016-07-29 05:51:30 +08:00
|
|
|
}
|
2016-07-29 23:32:46 +08:00
|
|
|
|
2016-08-04 10:03:27 +08:00
|
|
|
Cmd->SectionPatterns = readInputFilePatterns();
|
|
|
|
return Cmd;
|
2016-07-29 05:51:30 +08:00
|
|
|
}
|
|
|
|
|
2016-08-04 10:03:27 +08:00
|
|
|
InputSectionDescription *ScriptParser::readInputSectionDescription() {
|
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
|
|
|
|
if (skip("KEEP")) {
|
|
|
|
expect("(");
|
2016-08-04 10:03:27 +08:00
|
|
|
InputSectionDescription *Cmd = readInputSectionRules();
|
2016-07-27 09:44:01 +08:00
|
|
|
expect(")");
|
2016-08-04 10:03:27 +08:00
|
|
|
Opt.KeptSections.insert(Opt.KeptSections.end(),
|
|
|
|
Cmd->SectionPatterns.begin(),
|
|
|
|
Cmd->SectionPatterns.end());
|
|
|
|
return Cmd;
|
2016-07-26 05:47:13 +08:00
|
|
|
}
|
2016-08-04 10:03:27 +08:00
|
|
|
return readInputSectionRules();
|
2016-07-26 05:47:13 +08:00
|
|
|
}
|
|
|
|
|
2016-08-04 10:03:27 +08:00
|
|
|
Expr ScriptParser::readAlign() {
|
2016-07-27 02:06:29 +08:00
|
|
|
expect("(");
|
2016-08-04 10:03:27 +08:00
|
|
|
Expr E = readExpr();
|
2016-07-27 02:06:29 +08:00
|
|
|
expect(")");
|
2016-08-04 10:03:27 +08:00
|
|
|
return E;
|
2016-07-27 02:06:29 +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(",");
|
|
|
|
StringRef Msg = next();
|
|
|
|
expect(")");
|
|
|
|
return [=](uint64_t Dot) {
|
|
|
|
uint64_t V = E(Dot);
|
|
|
|
if (!V)
|
|
|
|
error(Msg);
|
|
|
|
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-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-07-27 02:06:29 +08:00
|
|
|
if (skip("ALIGN"))
|
2016-08-04 10:03:27 +08:00
|
|
|
Cmd->AlignExpr = readAlign();
|
2016-07-27 02:06:29 +08:00
|
|
|
|
2016-07-22 11:36:24 +08:00
|
|
|
// Parse constraints.
|
|
|
|
if (skip("ONLY_IF_RO"))
|
2016-07-26 06:00:10 +08:00
|
|
|
Cmd->Constraint = ConstraintKind::ReadOnly;
|
2016-07-22 11:36:24 +08:00
|
|
|
if (skip("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-02-03 04:27:59 +08:00
|
|
|
while (!Error && !skip("}")) {
|
2016-07-29 06:15:44 +08:00
|
|
|
if (peek().startswith("*") || peek() == "KEEP") {
|
2016-08-04 10:03:27 +08:00
|
|
|
Cmd->Commands.emplace_back(readInputSectionDescription());
|
2016-07-29 05:51:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-08-11 15:56:43 +08:00
|
|
|
|
|
|
|
StringRef Tok = next();
|
|
|
|
if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok))
|
|
|
|
Cmd->Commands.emplace_back(Assignment);
|
|
|
|
else if (Tok == "SORT")
|
2016-07-28 15:18:23 +08:00
|
|
|
readSort();
|
2016-08-11 15:56:43 +08:00
|
|
|
else
|
|
|
|
setError("unknown command " + Tok);
|
2015-11-12 17:52:08 +08:00
|
|
|
}
|
2016-07-21 14:43:01 +08:00
|
|
|
Cmd->Phdrs = readOutputSectionPhdrs();
|
2016-07-29 14:14:07 +08:00
|
|
|
Cmd->Filler = readOutputSectionFiller();
|
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-07-29 14:14:07 +08:00
|
|
|
std::vector<uint8_t> ScriptParser::readOutputSectionFiller() {
|
2016-02-26 22:48:31 +08:00
|
|
|
StringRef Tok = peek();
|
2016-07-29 14:14:07 +08:00
|
|
|
if (!Tok.startswith("="))
|
|
|
|
return {};
|
2016-07-30 06:21:28 +08:00
|
|
|
next();
|
2016-08-04 07:25:15 +08:00
|
|
|
|
|
|
|
// Read a hexstring of arbitrary length.
|
2016-07-30 06:21:28 +08:00
|
|
|
if (Tok.startswith("=0x"))
|
|
|
|
return parseHex(Tok.substr(3));
|
|
|
|
|
2016-08-04 07:25:15 +08:00
|
|
|
// Read a decimal or octal value as a big-endian 32 bit value.
|
|
|
|
// Why do this? I don't know, but that's what gold does.
|
|
|
|
uint32_t V;
|
|
|
|
if (Tok.substr(1).getAsInteger(0, V)) {
|
|
|
|
setError("invalid filler expression: " + Tok);
|
2016-07-29 14:14:07 +08:00
|
|
|
return {};
|
2016-02-26 22:48:31 +08:00
|
|
|
}
|
2016-08-04 07:25:15 +08:00
|
|
|
return { uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V) };
|
2015-11-12 17:52:08 +08:00
|
|
|
}
|
|
|
|
|
2016-08-04 10:03:27 +08:00
|
|
|
SymbolAssignment *ScriptParser::readProvide(bool Hidden) {
|
2016-07-22 15:38:40 +08:00
|
|
|
expect("(");
|
2016-07-29 08:29:25 +08:00
|
|
|
SymbolAssignment *Cmd = readAssignment(next());
|
|
|
|
Cmd->Provide = true;
|
|
|
|
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-08-11 15:56:43 +08:00
|
|
|
SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) {
|
|
|
|
SymbolAssignment *Cmd = nullptr;
|
|
|
|
if (peek() == "=" || peek() == "+=") {
|
|
|
|
Cmd = readAssignment(Tok);
|
|
|
|
expect(";");
|
|
|
|
} else if (Tok == "PROVIDE") {
|
|
|
|
Cmd = readProvide(false);
|
|
|
|
} else if (Tok == "PROVIDE_HIDDEN") {
|
|
|
|
Cmd = readProvide(true);
|
|
|
|
}
|
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
|
2016-07-29 05:08:56 +08:00
|
|
|
static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
|
|
|
|
if (S == ".")
|
|
|
|
return Dot;
|
2016-07-12 14:39:48 +08:00
|
|
|
|
2016-07-27 02:18:58 +08:00
|
|
|
switch (Config->EKind) {
|
|
|
|
case ELF32LEKind:
|
|
|
|
if (SymbolBody *B = Symtab<ELF32LE>::X->find(S))
|
|
|
|
return B->getVA<ELF32LE>();
|
|
|
|
break;
|
|
|
|
case ELF32BEKind:
|
|
|
|
if (SymbolBody *B = Symtab<ELF32BE>::X->find(S))
|
|
|
|
return B->getVA<ELF32BE>();
|
|
|
|
break;
|
|
|
|
case ELF64LEKind:
|
|
|
|
if (SymbolBody *B = Symtab<ELF64LE>::X->find(S))
|
|
|
|
return B->getVA<ELF64LE>();
|
|
|
|
break;
|
|
|
|
case ELF64BEKind:
|
|
|
|
if (SymbolBody *B = Symtab<ELF64BE>::X->find(S))
|
|
|
|
return B->getVA<ELF64BE>();
|
|
|
|
break;
|
2016-07-27 02:41:06 +08:00
|
|
|
default:
|
2016-07-27 02:46:13 +08:00
|
|
|
llvm_unreachable("unsupported target");
|
2016-07-27 02:18:58 +08:00
|
|
|
}
|
|
|
|
error("symbol not found: " + S);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-30 00:18:47 +08:00
|
|
|
static uint64_t getSectionSize(StringRef Name) {
|
|
|
|
switch (Config->EKind) {
|
|
|
|
case ELF32LEKind:
|
|
|
|
return Script<ELF32LE>::X->getOutputSectionSize(Name);
|
|
|
|
case ELF32BEKind:
|
|
|
|
return Script<ELF32BE>::X->getOutputSectionSize(Name);
|
|
|
|
case ELF64LEKind:
|
|
|
|
return Script<ELF64LE>::X->getOutputSectionSize(Name);
|
|
|
|
case ELF64BEKind:
|
|
|
|
return Script<ELF64BE>::X->getOutputSectionSize(Name);
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported target");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 15:59:34 +08:00
|
|
|
static uint64_t getSizeOfHeaders() {
|
|
|
|
switch (Config->EKind) {
|
|
|
|
case ELF32LEKind:
|
|
|
|
return Script<ELF32LE>::X->getSizeOfHeaders();
|
|
|
|
case ELF32BEKind:
|
|
|
|
return Script<ELF32BE>::X->getSizeOfHeaders();
|
|
|
|
case ELF64LEKind:
|
|
|
|
return Script<ELF64LE>::X->getSizeOfHeaders();
|
|
|
|
case ELF64BEKind:
|
|
|
|
return Script<ELF64BE>::X->getSizeOfHeaders();
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported target");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-29 05:08:56 +08:00
|
|
|
SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
|
|
|
|
StringRef Op = next();
|
|
|
|
assert(Op == "=" || Op == "+=");
|
|
|
|
Expr E = readExpr();
|
|
|
|
if (Op == "+=")
|
|
|
|
E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
|
2016-08-04 10:03:27 +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 == "+")
|
|
|
|
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); };
|
|
|
|
if (Op == "!=")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
|
|
|
|
if (Op == "&")
|
|
|
|
return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
|
|
|
|
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.
|
|
|
|
StringRef Op1 = peek();
|
|
|
|
if (Op1 == "?")
|
|
|
|
return readTernary(Lhs);
|
|
|
|
if (precedence(Op1) < MinPrec)
|
|
|
|
break;
|
|
|
|
next();
|
|
|
|
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) {
|
|
|
|
if (S == "COMMONPAGESIZE" || S == "MAXPAGESIZE")
|
|
|
|
return Target->PageSize;
|
|
|
|
error("unknown constant: " + S);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr ScriptParser::readPrimary() {
|
|
|
|
StringRef Tok = next();
|
|
|
|
|
|
|
|
if (Tok == "(") {
|
|
|
|
Expr E = readExpr();
|
|
|
|
expect(")");
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Built-in functions are parsed here.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
|
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") {
|
|
|
|
expect("(");
|
|
|
|
Expr E = readExpr();
|
|
|
|
expect(")");
|
|
|
|
return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
|
|
|
|
}
|
|
|
|
if (Tok == "CONSTANT") {
|
|
|
|
expect("(");
|
|
|
|
StringRef Tok = next();
|
|
|
|
expect(")");
|
|
|
|
return [=](uint64_t Dot) { return getConstant(Tok); };
|
|
|
|
}
|
2016-07-29 02:16:24 +08:00
|
|
|
if (Tok == "SEGMENT_START") {
|
|
|
|
expect("(");
|
|
|
|
next();
|
|
|
|
expect(",");
|
|
|
|
uint64_t Val;
|
|
|
|
next().getAsInteger(0, Val);
|
|
|
|
expect(")");
|
|
|
|
return [=](uint64_t Dot) { return Val; };
|
|
|
|
}
|
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("(");
|
|
|
|
next();
|
|
|
|
expect(",");
|
|
|
|
readExpr();
|
|
|
|
expect(")");
|
|
|
|
return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
|
|
|
|
}
|
2016-07-30 00:18:47 +08:00
|
|
|
if (Tok == "SIZEOF") {
|
|
|
|
expect("(");
|
|
|
|
StringRef Name = next();
|
|
|
|
expect(")");
|
|
|
|
return [=](uint64_t Dot) { return getSectionSize(Name); };
|
|
|
|
}
|
2016-08-10 15:59:34 +08:00
|
|
|
if (Tok == "SIZEOF_HEADERS")
|
|
|
|
return [=](uint64_t Dot) { return getSizeOfHeaders(); };
|
Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.
The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.
The above mechanism worked fairly well, but there were a few
drawbacks.
One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.
The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.
This patch fixes the issues with a new approach.
Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.
In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.
Differential Revision: https://reviews.llvm.org/D22728
llvm-svn: 276574
2016-07-25 02:19:40 +08:00
|
|
|
|
2016-07-27 02:18:58 +08:00
|
|
|
// Parse a symbol name or a number literal.
|
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
|
|
|
uint64_t V = 0;
|
2016-07-27 02:18:58 +08:00
|
|
|
if (Tok.getAsInteger(0, V)) {
|
2016-07-29 05:08:56 +08:00
|
|
|
if (Tok != "." && !isValidCIdentifier(Tok))
|
2016-07-27 02:18:58 +08:00
|
|
|
setError("malformed number: " + Tok);
|
2016-07-29 05:08:56 +08:00
|
|
|
return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
|
2016-07-27 02:18:58 +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 [=](uint64_t Dot) { return V; };
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr ScriptParser::readTernary(Expr Cond) {
|
|
|
|
next();
|
|
|
|
Expr L = readExpr();
|
|
|
|
expect(":");
|
|
|
|
Expr R = readExpr();
|
|
|
|
return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:25:43 +08:00
|
|
|
std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
|
|
|
|
std::vector<StringRef> Phdrs;
|
|
|
|
while (!Error && peek().startswith(":")) {
|
|
|
|
StringRef Tok = next();
|
|
|
|
Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
|
|
|
|
if (Tok.empty()) {
|
|
|
|
setError("section header name is empty");
|
|
|
|
break;
|
|
|
|
}
|
2016-07-21 03:36:36 +08:00
|
|
|
Phdrs.push_back(Tok);
|
2016-07-19 17:25:43 +08:00
|
|
|
}
|
|
|
|
return Phdrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ScriptParser::readPhdrType() {
|
|
|
|
StringRef Tok = next();
|
2016-07-21 03:36:38 +08:00
|
|
|
unsigned Ret = StringSwitch<unsigned>(Tok)
|
|
|
|
.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)
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
2015-11-26 13:53:00 +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;
|
|
|
|
}
|
|
|
|
|
2016-04-21 04:13:41 +08:00
|
|
|
// Entry point.
|
|
|
|
void elf::readLinkerScript(MemoryBufferRef MB) {
|
2015-11-26 13:53:00 +08:00
|
|
|
StringRef Path = MB.getBufferIdentifier();
|
2016-04-21 04:13:41 +08:00
|
|
|
ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
|
|
|
|
}
|
|
|
|
|
|
|
|
template class elf::LinkerScript<ELF32LE>;
|
|
|
|
template class elf::LinkerScript<ELF32BE>;
|
|
|
|
template class elf::LinkerScript<ELF64LE>;
|
|
|
|
template class elf::LinkerScript<ELF64BE>;
|