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.
|
|
|
|
// It does not construct an AST but consume linker script directives directly.
|
2015-10-14 03:51:57 +08:00
|
|
|
// Results are written to Driver or Config object.
|
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"
|
2015-10-01 01:23:26 +08:00
|
|
|
#include "SymbolTable.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-02-28 08:25:54 +08:00
|
|
|
LinkerScript *elf::Script;
|
2016-02-12 05:17:59 +08:00
|
|
|
|
2016-04-16 18:10:32 +08:00
|
|
|
static uint64_t getInteger(StringRef S) {
|
|
|
|
uint64_t V;
|
|
|
|
if (S.getAsInteger(0, V)) {
|
|
|
|
error("malformed number: " + S);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluates the expression given by list of tokens.
|
|
|
|
uint64_t LinkerScript::evaluate(std::vector<StringRef> &Tokens,
|
|
|
|
uint64_t LocCounter) {
|
|
|
|
uint64_t Result = 0;
|
|
|
|
for (size_t I = 0, E = Tokens.size(); I < E; ++I) {
|
|
|
|
// Each second token should be '+' as this is the
|
|
|
|
// only operator we support now.
|
|
|
|
if (I % 2 == 1) {
|
|
|
|
if (Tokens[I] == "+")
|
|
|
|
continue;
|
|
|
|
error("error in location counter expression");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef Tok = Tokens[I];
|
|
|
|
if (Tok == ".")
|
|
|
|
Result += LocCounter;
|
|
|
|
else
|
|
|
|
Result += getInteger(Tok);
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2016-02-13 05:47:28 +08:00
|
|
|
template <class ELFT>
|
2016-02-23 15:47:54 +08:00
|
|
|
SectionRule *LinkerScript::find(InputSectionBase<ELFT> *S) {
|
2016-02-13 05:47:28 +08:00
|
|
|
for (SectionRule &R : Sections)
|
|
|
|
if (R.match(S))
|
2016-02-23 15:47:54 +08:00
|
|
|
return &R;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
StringRef LinkerScript::getOutputSection(InputSectionBase<ELFT> *S) {
|
|
|
|
SectionRule *R = find(S);
|
|
|
|
return R ? R->Dest : "";
|
2016-02-12 05:17:59 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 05:47:28 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool LinkerScript::isDiscarded(InputSectionBase<ELFT> *S) {
|
|
|
|
return getOutputSection(S) == "/DISCARD/";
|
2016-02-12 05:17:59 +08:00
|
|
|
}
|
|
|
|
|
2016-02-23 15:47:54 +08:00
|
|
|
template <class ELFT> bool LinkerScript::shouldKeep(InputSectionBase<ELFT> *S) {
|
|
|
|
SectionRule *R = find(S);
|
|
|
|
return R && R->Keep;
|
|
|
|
}
|
|
|
|
|
2016-04-16 18:10:32 +08:00
|
|
|
// This method finalizes the Locations list. Adds neccesary locations for
|
|
|
|
// orphan sections, what prepares it for futher use without
|
|
|
|
// changes in LinkerScript::assignAddresses().
|
|
|
|
template <class ELFT>
|
|
|
|
void LinkerScript::fixupLocations(std::vector<OutputSectionBase<ELFT> *> &S) {
|
|
|
|
// Orphan sections are sections present in the input files which
|
|
|
|
// 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
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
|
|
|
|
for (OutputSectionBase<ELFT> *Sec : S) {
|
|
|
|
StringRef Name = Sec->getName();
|
|
|
|
auto I = std::find(SectionOrder.begin(), SectionOrder.end(), Name);
|
|
|
|
if (I == SectionOrder.end())
|
|
|
|
Locations.push_back({Command::Section, {}, {Name}});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELFT> *> &S) {
|
|
|
|
typedef typename ELFT::uint uintX_t;
|
|
|
|
|
|
|
|
Script->fixupLocations(S);
|
|
|
|
|
|
|
|
uintX_t ThreadBssOffset = 0;
|
|
|
|
uintX_t VA =
|
|
|
|
Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
|
|
|
|
|
|
|
|
for (LocationNode &Node : Locations) {
|
|
|
|
if (Node.Type == Command::Expr) {
|
|
|
|
VA = evaluate(Node.Expr, VA);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto I =
|
|
|
|
std::find_if(S.begin(), S.end(), [&](OutputSectionBase<ELFT> *Sec) {
|
|
|
|
return Sec->getName() == Node.SectionName;
|
|
|
|
});
|
|
|
|
if (I == S.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
OutputSectionBase<ELFT> *Sec = *I;
|
|
|
|
uintX_t Align = Sec->getAlign();
|
|
|
|
if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
|
|
|
|
uintX_t TVA = VA + ThreadBssOffset;
|
|
|
|
TVA = alignTo(TVA, Align);
|
|
|
|
Sec->setVA(TVA);
|
|
|
|
ThreadBssOffset = TVA - VA + Sec->getSize();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Sec->getFlags() & SHF_ALLOC) {
|
|
|
|
VA = alignTo(VA, Align);
|
|
|
|
Sec->setVA(VA);
|
|
|
|
VA += Sec->getSize();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:48:31 +08:00
|
|
|
ArrayRef<uint8_t> LinkerScript::getFiller(StringRef Name) {
|
2016-02-28 13:09:11 +08:00
|
|
|
auto I = Filler.find(Name);
|
|
|
|
if (I == Filler.end())
|
|
|
|
return {};
|
|
|
|
return I->second;
|
2016-02-26 22:48:31 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 04:41:43 +08:00
|
|
|
// A compartor to sort output sections. Returns -1 or 1 if both
|
|
|
|
// A and B are mentioned in linker scripts. Otherwise, returns 0
|
|
|
|
// to use the default rule which is implemented in Writer.cpp.
|
2016-02-12 05:17:59 +08:00
|
|
|
int LinkerScript::compareSections(StringRef A, StringRef B) {
|
2016-02-28 13:09:11 +08:00
|
|
|
auto E = SectionOrder.end();
|
|
|
|
auto I = std::find(SectionOrder.begin(), E, A);
|
|
|
|
auto J = std::find(SectionOrder.begin(), E, B);
|
|
|
|
if (I == E || J == E)
|
2016-02-12 05:17:59 +08:00
|
|
|
return 0;
|
|
|
|
return I < J ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
2016-02-24 16:49:50 +08:00
|
|
|
// Returns true if S matches T. S can contain glob meta-characters.
|
|
|
|
// The asterisk ('*') matches zero or more characacters, and the question
|
|
|
|
// mark ('?') matches one character.
|
2016-02-13 05:47:28 +08:00
|
|
|
static bool matchStr(StringRef S, StringRef T) {
|
|
|
|
for (;;) {
|
|
|
|
if (S.empty())
|
|
|
|
return T.empty();
|
|
|
|
if (S[0] == '*') {
|
|
|
|
S = S.substr(1);
|
|
|
|
if (S.empty())
|
|
|
|
// Fast path. If a pattern is '*', it matches anything.
|
|
|
|
return true;
|
|
|
|
for (size_t I = 0, E = T.size(); I < E; ++I)
|
|
|
|
if (matchStr(S, T.substr(I)))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-24 16:49:50 +08:00
|
|
|
if (T.empty() || (S[0] != T[0] && S[0] != '?'))
|
2016-02-13 05:47:28 +08:00
|
|
|
return false;
|
|
|
|
S = S.substr(1);
|
|
|
|
T = T.substr(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> bool SectionRule::match(InputSectionBase<ELFT> *S) {
|
|
|
|
return matchStr(SectionPattern, S->getSectionName());
|
|
|
|
}
|
|
|
|
|
2016-04-07 04:59:11 +08:00
|
|
|
class elf::ScriptParser final : public elf::ScriptParserBase {
|
2016-02-24 17:21:47 +08:00
|
|
|
typedef void (ScriptParser::*Handler)();
|
|
|
|
|
2015-10-01 01:23:26 +08:00
|
|
|
public:
|
2016-02-12 05:17:59 +08:00
|
|
|
ScriptParser(BumpPtrAllocator *A, StringRef S, bool B)
|
2016-04-07 04:59:11 +08:00
|
|
|
: ScriptParserBase(S), Saver(*A), IsUnderSysroot(B) {}
|
2016-02-19 18:45:45 +08:00
|
|
|
|
2016-04-07 04:59:11 +08:00
|
|
|
void run() override;
|
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();
|
2015-10-09 01:51:41 +08:00
|
|
|
void readSearchDir();
|
2015-11-12 17:52:08 +08:00
|
|
|
void readSections();
|
|
|
|
|
2016-04-16 18:10:32 +08:00
|
|
|
void readLocationCounterValue();
|
2015-11-12 17:52:08 +08:00
|
|
|
void readOutputSectionDescription();
|
2016-02-23 15:47:54 +08:00
|
|
|
void readSectionPatterns(StringRef OutSec, bool Keep);
|
2015-10-01 01:23:26 +08:00
|
|
|
|
2015-10-11 09:53:04 +08:00
|
|
|
StringSaver Saver;
|
2016-02-24 17:21:47 +08:00
|
|
|
const static StringMap<Handler> Cmd;
|
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},
|
|
|
|
{"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-02-03 04:27:59 +08:00
|
|
|
while (!Error) {
|
2015-10-01 01:23:26 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok == ")")
|
2015-10-12 04:59:12 +08:00
|
|
|
break;
|
2015-10-11 11:28:42 +08:00
|
|
|
addFile(Tok);
|
2015-10-01 01:23:26 +08:00
|
|
|
}
|
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-02-03 04:27:59 +08:00
|
|
|
while (!Error) {
|
2015-10-20 01:35:12 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok == ")")
|
|
|
|
return;
|
|
|
|
Config->Undefined.push_back(Tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readGroup() {
|
2015-10-01 01:23:26 +08:00
|
|
|
expect("(");
|
2016-02-03 04:27:59 +08:00
|
|
|
while (!Error) {
|
2015-10-01 01:23:26 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok == ")")
|
|
|
|
return;
|
|
|
|
if (Tok == "AS_NEEDED") {
|
|
|
|
readAsNeeded();
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-11 11:28:42 +08:00
|
|
|
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-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-04-16 18:10:32 +08:00
|
|
|
Script->DoLayout = true;
|
2015-11-12 17:52:08 +08:00
|
|
|
expect("{");
|
2016-04-16 18:10:32 +08:00
|
|
|
while (!Error && !skip("}")) {
|
|
|
|
StringRef Tok = peek();
|
|
|
|
if (Tok == ".")
|
|
|
|
readLocationCounterValue();
|
|
|
|
else
|
|
|
|
readOutputSectionDescription();
|
|
|
|
}
|
2015-11-12 17:52:08 +08:00
|
|
|
}
|
|
|
|
|
2016-02-23 15:47:54 +08:00
|
|
|
void ScriptParser::readSectionPatterns(StringRef OutSec, bool Keep) {
|
|
|
|
expect("(");
|
|
|
|
while (!Error && !skip(")"))
|
|
|
|
Script->Sections.emplace_back(OutSec, next(), Keep);
|
|
|
|
}
|
|
|
|
|
2016-04-16 18:10:32 +08:00
|
|
|
void ScriptParser::readLocationCounterValue() {
|
|
|
|
expect(".");
|
|
|
|
expect("=");
|
|
|
|
Script->Locations.push_back({Command::Expr, {}, {}});
|
|
|
|
LocationNode &Node = Script->Locations.back();
|
|
|
|
while (!Error) {
|
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok == ";")
|
|
|
|
break;
|
|
|
|
Node.Expr.push_back(Tok);
|
|
|
|
}
|
|
|
|
if (Node.Expr.empty())
|
|
|
|
error("error in location counter expression");
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:17:59 +08:00
|
|
|
void ScriptParser::readOutputSectionDescription() {
|
2016-02-28 13:09:11 +08:00
|
|
|
StringRef OutSec = next();
|
|
|
|
Script->SectionOrder.push_back(OutSec);
|
2016-04-16 18:10:32 +08:00
|
|
|
Script->Locations.push_back({Command::Section, {}, {OutSec}});
|
2015-11-12 17:52:08 +08:00
|
|
|
expect(":");
|
|
|
|
expect("{");
|
2016-02-03 04:27:59 +08:00
|
|
|
while (!Error && !skip("}")) {
|
2016-02-23 15:47:54 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok == "*") {
|
2016-02-28 13:09:11 +08:00
|
|
|
readSectionPatterns(OutSec, false);
|
2016-02-23 15:47:54 +08:00
|
|
|
} else if (Tok == "KEEP") {
|
|
|
|
expect("(");
|
|
|
|
next(); // Skip *
|
2016-02-28 13:09:11 +08:00
|
|
|
readSectionPatterns(OutSec, true);
|
2016-02-23 15:47:54 +08:00
|
|
|
expect(")");
|
|
|
|
} else {
|
2016-03-12 16:31:34 +08:00
|
|
|
setError("unknown command " + Tok);
|
2016-02-23 15:47:54 +08:00
|
|
|
}
|
2015-11-12 17:52:08 +08:00
|
|
|
}
|
2016-02-26 22:48:31 +08:00
|
|
|
StringRef Tok = peek();
|
|
|
|
if (Tok.startswith("=")) {
|
|
|
|
if (!Tok.startswith("=0x")) {
|
2016-03-13 11:17:44 +08:00
|
|
|
setError("filler should be a hexadecimal value");
|
2016-02-26 22:48:31 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-02-28 13:09:11 +08:00
|
|
|
Tok = Tok.substr(3);
|
|
|
|
Script->Filler[OutSec] = parseHex(Tok);
|
2016-02-26 22:48:31 +08:00
|
|
|
next();
|
|
|
|
}
|
2015-11-12 17:52:08 +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;
|
|
|
|
}
|
|
|
|
|
2015-10-01 01:23:26 +08:00
|
|
|
// Entry point. The other functions or classes are private to this file.
|
2016-02-12 05:38:55 +08:00
|
|
|
void LinkerScript::read(MemoryBufferRef MB) {
|
2015-11-26 13:53:00 +08:00
|
|
|
StringRef Path = MB.getBufferIdentifier();
|
2016-02-12 05:38:55 +08:00
|
|
|
ScriptParser(&Alloc, MB.getBuffer(), isUnderSysroot(Path)).run();
|
2015-10-01 01:23:26 +08:00
|
|
|
}
|
2016-02-13 05:47:28 +08:00
|
|
|
|
|
|
|
template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF32LE> *);
|
|
|
|
template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF32BE> *);
|
|
|
|
template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF64LE> *);
|
|
|
|
template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF64BE> *);
|
|
|
|
|
|
|
|
template bool LinkerScript::isDiscarded(InputSectionBase<ELF32LE> *);
|
|
|
|
template bool LinkerScript::isDiscarded(InputSectionBase<ELF32BE> *);
|
|
|
|
template bool LinkerScript::isDiscarded(InputSectionBase<ELF64LE> *);
|
|
|
|
template bool LinkerScript::isDiscarded(InputSectionBase<ELF64BE> *);
|
|
|
|
|
2016-02-23 15:47:54 +08:00
|
|
|
template bool LinkerScript::shouldKeep(InputSectionBase<ELF32LE> *);
|
|
|
|
template bool LinkerScript::shouldKeep(InputSectionBase<ELF32BE> *);
|
|
|
|
template bool LinkerScript::shouldKeep(InputSectionBase<ELF64LE> *);
|
|
|
|
template bool LinkerScript::shouldKeep(InputSectionBase<ELF64BE> *);
|
2016-04-16 18:10:32 +08:00
|
|
|
|
|
|
|
template void
|
|
|
|
LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF32LE> *> &);
|
|
|
|
template void
|
|
|
|
LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF32BE> *> &);
|
|
|
|
template void
|
|
|
|
LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF64LE> *> &);
|
|
|
|
template void
|
|
|
|
LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF64BE> *> &);
|