2017-04-05 13:07:39 +08:00
|
|
|
//===- ScriptParser.cpp ---------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-04-05 13:07:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-04-05 13:50:08 +08:00
|
|
|
//
|
|
|
|
// This file contains a recursive-descendent parser for linker scripts.
|
|
|
|
// Parsed results are stored to Config and Script global objects.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
#include "ScriptParser.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "Driver.h"
|
|
|
|
#include "InputSection.h"
|
|
|
|
#include "LinkerScript.h"
|
|
|
|
#include "OutputSections.h"
|
|
|
|
#include "ScriptLexer.h"
|
|
|
|
#include "Symbols.h"
|
|
|
|
#include "Target.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2017-04-05 13:07:39 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-09-07 02:14:08 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2017-04-05 13:07:39 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2017-04-05 13:07:39 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <limits>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
2017-04-12 06:45:57 +08:00
|
|
|
using namespace llvm::support::endian;
|
2017-04-05 13:07:39 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
|
|
|
|
|
|
|
static bool isUnderSysroot(StringRef Path);
|
|
|
|
|
2017-04-05 13:08:01 +08:00
|
|
|
namespace {
|
|
|
|
class ScriptParser final : ScriptLexer {
|
2017-04-05 13:07:39 +08:00
|
|
|
public:
|
|
|
|
ScriptParser(MemoryBufferRef MB)
|
|
|
|
: ScriptLexer(MB),
|
|
|
|
IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {}
|
|
|
|
|
|
|
|
void readLinkerScript();
|
|
|
|
void readVersionScript();
|
|
|
|
void readDynamicList();
|
2017-11-04 10:03:58 +08:00
|
|
|
void readDefsym(StringRef Name);
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void addFile(StringRef Path);
|
|
|
|
|
|
|
|
void readAsNeeded();
|
|
|
|
void readEntry();
|
|
|
|
void readExtern();
|
|
|
|
void readGroup();
|
|
|
|
void readInclude();
|
Add --warn-backrefs to maintain compatibility with other linkers
I'm proposing a new command line flag, --warn-backrefs in this patch.
The flag and the feature proposed below don't exist in GNU linkers
nor the current lld.
--warn-backrefs is an option to detect reverse or cyclic dependencies
between static archives, and it can be used to keep your program
compatible with GNU linkers after you switch to lld. I'll explain the
feature and why you may find it useful below.
lld's symbol resolution semantics is more relaxed than traditional
Unix linkers. Therefore,
ld.lld foo.a bar.o
succeeds even if bar.o contains an undefined symbol that have to be
resolved by some object file in foo.a. Traditional Unix linkers
don't allow this kind of backward reference, as they visit each
file only once from left to right in the command line while
resolving all undefined symbol at the moment of visiting.
In the above case, since there's no undefined symbol when a linker
visits foo.a, no files are pulled out from foo.a, and because the
linker forgets about foo.a after visiting, it can't resolve
undefined symbols that could have been resolved otherwise.
That lld accepts more relaxed form means (besides it makes more
sense) that you can accidentally write a command line or a build
file that works only with lld, even if you have a plan to
distribute it to wider users who may be using GNU linkers. With
--check-library-dependency, you can detect a library order that
doesn't work with other Unix linkers.
The option is also useful to detect cyclic dependencies between
static archives. Again, lld accepts
ld.lld foo.a bar.a
even if foo.a and bar.a depend on each other. With --warn-backrefs
it is handled as an error.
Here is how the option works. We assign a group ID to each file. A
file with a smaller group ID can pull out object files from an
archive file with an equal or greater group ID. Otherwise, it is a
reverse dependency and an error.
A file outside --{start,end}-group gets a fresh ID when
instantiated. All files within the same --{start,end}-group get the
same group ID. E.g.
ld.lld A B --start-group C D --end-group E
A and B form group 0, C, D and their member object files form group
1, and E forms group 2. I think that you can see how this group
assignment rule simulates the traditional linker's semantics.
Differential Revision: https://reviews.llvm.org/D45195
llvm-svn: 329636
2018-04-10 07:05:48 +08:00
|
|
|
void readInput();
|
2017-04-05 13:07:39 +08:00
|
|
|
void readMemory();
|
|
|
|
void readOutput();
|
|
|
|
void readOutputArch();
|
|
|
|
void readOutputFormat();
|
|
|
|
void readPhdrs();
|
2017-09-08 16:23:15 +08:00
|
|
|
void readRegionAlias();
|
2017-04-05 13:07:39 +08:00
|
|
|
void readSearchDir();
|
|
|
|
void readSections();
|
2018-08-07 05:29:41 +08:00
|
|
|
void readTarget();
|
2017-04-05 13:07:39 +08:00
|
|
|
void readVersion();
|
|
|
|
void readVersionScriptCommand();
|
|
|
|
|
2018-04-25 19:16:31 +08:00
|
|
|
SymbolAssignment *readSymbolAssignment(StringRef Name);
|
2017-10-11 12:22:09 +08:00
|
|
|
ByteCommand *readByteCommand(StringRef Tok);
|
2018-11-15 05:05:20 +08:00
|
|
|
std::array<uint8_t, 4> readFill();
|
|
|
|
std::array<uint8_t, 4> parseFill(StringRef Tok);
|
2018-08-28 16:39:21 +08:00
|
|
|
bool readSectionDirective(OutputSection *Cmd, StringRef Tok1, StringRef Tok2);
|
2017-07-28 03:22:43 +08:00
|
|
|
void readSectionAddressType(OutputSection *Cmd);
|
2018-06-27 16:08:12 +08:00
|
|
|
OutputSection *readOverlaySectionDescription();
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *readOutputSectionDescription(StringRef OutSec);
|
2018-06-27 16:08:12 +08:00
|
|
|
std::vector<BaseCommand *> readOverlay();
|
2017-04-05 13:07:39 +08:00
|
|
|
std::vector<StringRef> readOutputSectionPhdrs();
|
|
|
|
InputSectionDescription *readInputSectionDescription(StringRef Tok);
|
|
|
|
StringMatcher readFilePatterns();
|
|
|
|
std::vector<SectionPattern> readInputSectionsList();
|
|
|
|
InputSectionDescription *readInputSectionRules(StringRef FilePattern);
|
|
|
|
unsigned readPhdrType();
|
|
|
|
SortSectionPolicy readSortKind();
|
|
|
|
SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
|
2018-04-25 19:16:31 +08:00
|
|
|
SymbolAssignment *readAssignment(StringRef Tok);
|
2017-04-05 13:07:39 +08:00
|
|
|
void readSort();
|
2018-04-25 19:16:31 +08:00
|
|
|
Expr readAssert();
|
2017-08-04 00:05:08 +08:00
|
|
|
Expr readConstant();
|
|
|
|
Expr getPageSize();
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
uint64_t readMemoryAssignment(StringRef, StringRef, StringRef);
|
|
|
|
std::pair<uint32_t, uint32_t> readMemoryAttributes();
|
|
|
|
|
2018-03-05 18:02:44 +08:00
|
|
|
Expr combine(StringRef Op, Expr L, Expr R);
|
2017-04-05 13:07:39 +08:00
|
|
|
Expr readExpr();
|
|
|
|
Expr readExpr1(Expr Lhs, int MinPrec);
|
|
|
|
StringRef readParenLiteral();
|
|
|
|
Expr readPrimary();
|
|
|
|
Expr readTernary(Expr Cond);
|
|
|
|
Expr readParenExpr();
|
|
|
|
|
|
|
|
// For parsing version script.
|
|
|
|
std::vector<SymbolVersion> readVersionExtern();
|
|
|
|
void readAnonymousDeclaration();
|
|
|
|
void readVersionDeclaration(StringRef VerStr);
|
|
|
|
|
|
|
|
std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
|
|
|
|
readSymbols();
|
|
|
|
|
2017-09-07 02:09:06 +08:00
|
|
|
// True if a script being read is in a subdirectory specified by -sysroot.
|
2017-04-05 13:07:39 +08:00
|
|
|
bool IsUnderSysroot;
|
2017-09-07 02:14:08 +08:00
|
|
|
|
|
|
|
// A set to detect an INCLUDE() cycle.
|
|
|
|
StringSet<> Seen;
|
2017-04-05 13:07:39 +08:00
|
|
|
};
|
2017-04-05 13:08:01 +08:00
|
|
|
} // namespace
|
2017-04-05 13:07:39 +08:00
|
|
|
|
2017-07-14 04:30:35 +08:00
|
|
|
static StringRef unquote(StringRef S) {
|
|
|
|
if (S.startswith("\""))
|
|
|
|
return S.substr(1, S.size() - 2);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2017-04-05 13:07:39 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some operations only support one non absolute value. Move the
|
|
|
|
// absolute one to the right hand side for convenience.
|
|
|
|
static void moveAbsRight(ExprValue &A, ExprValue &B) {
|
2017-09-21 03:24:57 +08:00
|
|
|
if (A.Sec == nullptr || (A.ForceAbsolute && !B.isAbsolute()))
|
2017-04-05 13:07:39 +08:00
|
|
|
std::swap(A, B);
|
|
|
|
if (!B.isAbsolute())
|
2017-06-07 16:54:43 +08:00
|
|
|
error(A.Loc + ": at least one side of the expression must be absolute");
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ExprValue add(ExprValue A, ExprValue B) {
|
|
|
|
moveAbsRight(A, B);
|
2017-09-12 08:06:00 +08:00
|
|
|
return {A.Sec, A.ForceAbsolute, A.getSectionOffset() + B.getValue(), A.Loc};
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ExprValue sub(ExprValue A, ExprValue B) {
|
2017-12-27 02:11:14 +08:00
|
|
|
// The distance between two symbols in sections is absolute.
|
2017-12-23 05:55:28 +08:00
|
|
|
if (!A.isAbsolute() && !B.isAbsolute())
|
|
|
|
return A.getValue() - B.getValue();
|
2017-10-11 08:06:27 +08:00
|
|
|
return {A.Sec, false, A.getSectionOffset() - B.getValue(), A.Loc};
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ExprValue bitAnd(ExprValue A, ExprValue B) {
|
|
|
|
moveAbsRight(A, B);
|
|
|
|
return {A.Sec, A.ForceAbsolute,
|
2017-06-07 16:54:43 +08:00
|
|
|
(A.getValue() & B.getValue()) - A.getSecAddr(), A.Loc};
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ExprValue bitOr(ExprValue A, ExprValue B) {
|
|
|
|
moveAbsRight(A, B);
|
|
|
|
return {A.Sec, A.ForceAbsolute,
|
2017-06-07 16:54:43 +08:00
|
|
|
(A.getValue() | B.getValue()) - A.getSecAddr(), A.Loc};
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readDynamicList() {
|
2017-09-09 02:53:43 +08:00
|
|
|
Config->HasDynamicList = true;
|
2017-04-05 13:07:39 +08:00
|
|
|
expect("{");
|
2017-09-09 02:16:59 +08:00
|
|
|
std::vector<SymbolVersion> Locals;
|
|
|
|
std::vector<SymbolVersion> Globals;
|
|
|
|
std::tie(Locals, Globals) = readSymbols();
|
|
|
|
expect(";");
|
|
|
|
|
|
|
|
if (!atEOF()) {
|
2017-04-05 13:07:39 +08:00
|
|
|
setError("EOF expected, but got " + next());
|
2017-09-09 02:16:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!Locals.empty()) {
|
|
|
|
setError("\"local:\" scope not supported in --dynamic-list");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (SymbolVersion V : Globals)
|
|
|
|
Config->DynamicList.push_back(V);
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readVersionScript() {
|
|
|
|
readVersionScriptCommand();
|
|
|
|
if (!atEOF())
|
|
|
|
setError("EOF expected, but got " + next());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readVersionScriptCommand() {
|
|
|
|
if (consume("{")) {
|
|
|
|
readAnonymousDeclaration();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!atEOF() && !errorCount() && peek() != "}") {
|
2017-04-05 13:07:39 +08:00
|
|
|
StringRef VerStr = next();
|
|
|
|
if (VerStr == "{") {
|
|
|
|
setError("anonymous version definition is used in "
|
|
|
|
"combination with other version definitions");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
expect("{");
|
|
|
|
readVersionDeclaration(VerStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readVersion() {
|
|
|
|
expect("{");
|
|
|
|
readVersionScriptCommand();
|
|
|
|
expect("}");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readLinkerScript() {
|
|
|
|
while (!atEOF()) {
|
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok == ";")
|
|
|
|
continue;
|
|
|
|
|
2018-04-25 19:16:31 +08:00
|
|
|
if (Tok == "ENTRY") {
|
2017-04-05 13:07:39 +08:00
|
|
|
readEntry();
|
|
|
|
} else if (Tok == "EXTERN") {
|
|
|
|
readExtern();
|
Add --warn-backrefs to maintain compatibility with other linkers
I'm proposing a new command line flag, --warn-backrefs in this patch.
The flag and the feature proposed below don't exist in GNU linkers
nor the current lld.
--warn-backrefs is an option to detect reverse or cyclic dependencies
between static archives, and it can be used to keep your program
compatible with GNU linkers after you switch to lld. I'll explain the
feature and why you may find it useful below.
lld's symbol resolution semantics is more relaxed than traditional
Unix linkers. Therefore,
ld.lld foo.a bar.o
succeeds even if bar.o contains an undefined symbol that have to be
resolved by some object file in foo.a. Traditional Unix linkers
don't allow this kind of backward reference, as they visit each
file only once from left to right in the command line while
resolving all undefined symbol at the moment of visiting.
In the above case, since there's no undefined symbol when a linker
visits foo.a, no files are pulled out from foo.a, and because the
linker forgets about foo.a after visiting, it can't resolve
undefined symbols that could have been resolved otherwise.
That lld accepts more relaxed form means (besides it makes more
sense) that you can accidentally write a command line or a build
file that works only with lld, even if you have a plan to
distribute it to wider users who may be using GNU linkers. With
--check-library-dependency, you can detect a library order that
doesn't work with other Unix linkers.
The option is also useful to detect cyclic dependencies between
static archives. Again, lld accepts
ld.lld foo.a bar.a
even if foo.a and bar.a depend on each other. With --warn-backrefs
it is handled as an error.
Here is how the option works. We assign a group ID to each file. A
file with a smaller group ID can pull out object files from an
archive file with an equal or greater group ID. Otherwise, it is a
reverse dependency and an error.
A file outside --{start,end}-group gets a fresh ID when
instantiated. All files within the same --{start,end}-group get the
same group ID. E.g.
ld.lld A B --start-group C D --end-group E
A and B form group 0, C, D and their member object files form group
1, and E forms group 2. I think that you can see how this group
assignment rule simulates the traditional linker's semantics.
Differential Revision: https://reviews.llvm.org/D45195
llvm-svn: 329636
2018-04-10 07:05:48 +08:00
|
|
|
} else if (Tok == "GROUP") {
|
2017-04-05 13:07:39 +08:00
|
|
|
readGroup();
|
|
|
|
} else if (Tok == "INCLUDE") {
|
|
|
|
readInclude();
|
Add --warn-backrefs to maintain compatibility with other linkers
I'm proposing a new command line flag, --warn-backrefs in this patch.
The flag and the feature proposed below don't exist in GNU linkers
nor the current lld.
--warn-backrefs is an option to detect reverse or cyclic dependencies
between static archives, and it can be used to keep your program
compatible with GNU linkers after you switch to lld. I'll explain the
feature and why you may find it useful below.
lld's symbol resolution semantics is more relaxed than traditional
Unix linkers. Therefore,
ld.lld foo.a bar.o
succeeds even if bar.o contains an undefined symbol that have to be
resolved by some object file in foo.a. Traditional Unix linkers
don't allow this kind of backward reference, as they visit each
file only once from left to right in the command line while
resolving all undefined symbol at the moment of visiting.
In the above case, since there's no undefined symbol when a linker
visits foo.a, no files are pulled out from foo.a, and because the
linker forgets about foo.a after visiting, it can't resolve
undefined symbols that could have been resolved otherwise.
That lld accepts more relaxed form means (besides it makes more
sense) that you can accidentally write a command line or a build
file that works only with lld, even if you have a plan to
distribute it to wider users who may be using GNU linkers. With
--check-library-dependency, you can detect a library order that
doesn't work with other Unix linkers.
The option is also useful to detect cyclic dependencies between
static archives. Again, lld accepts
ld.lld foo.a bar.a
even if foo.a and bar.a depend on each other. With --warn-backrefs
it is handled as an error.
Here is how the option works. We assign a group ID to each file. A
file with a smaller group ID can pull out object files from an
archive file with an equal or greater group ID. Otherwise, it is a
reverse dependency and an error.
A file outside --{start,end}-group gets a fresh ID when
instantiated. All files within the same --{start,end}-group get the
same group ID. E.g.
ld.lld A B --start-group C D --end-group E
A and B form group 0, C, D and their member object files form group
1, and E forms group 2. I think that you can see how this group
assignment rule simulates the traditional linker's semantics.
Differential Revision: https://reviews.llvm.org/D45195
llvm-svn: 329636
2018-04-10 07:05:48 +08:00
|
|
|
} else if (Tok == "INPUT") {
|
|
|
|
readInput();
|
2017-04-05 13:07:39 +08:00
|
|
|
} else if (Tok == "MEMORY") {
|
|
|
|
readMemory();
|
|
|
|
} else if (Tok == "OUTPUT") {
|
|
|
|
readOutput();
|
|
|
|
} else if (Tok == "OUTPUT_ARCH") {
|
|
|
|
readOutputArch();
|
|
|
|
} else if (Tok == "OUTPUT_FORMAT") {
|
|
|
|
readOutputFormat();
|
|
|
|
} else if (Tok == "PHDRS") {
|
|
|
|
readPhdrs();
|
2017-09-08 16:23:15 +08:00
|
|
|
} else if (Tok == "REGION_ALIAS") {
|
|
|
|
readRegionAlias();
|
2017-04-05 13:07:39 +08:00
|
|
|
} else if (Tok == "SEARCH_DIR") {
|
|
|
|
readSearchDir();
|
|
|
|
} else if (Tok == "SECTIONS") {
|
|
|
|
readSections();
|
2018-08-07 05:29:41 +08:00
|
|
|
} else if (Tok == "TARGET") {
|
|
|
|
readTarget();
|
2017-04-05 13:07:39 +08:00
|
|
|
} else if (Tok == "VERSION") {
|
|
|
|
readVersion();
|
2018-04-25 19:16:31 +08:00
|
|
|
} else if (SymbolAssignment *Cmd = readAssignment(Tok)) {
|
2017-10-11 09:50:56 +08:00
|
|
|
Script->SectionCommands.push_back(Cmd);
|
2017-04-05 13:07:39 +08:00
|
|
|
} else {
|
|
|
|
setError("unknown directive: " + Tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 10:03:58 +08:00
|
|
|
void ScriptParser::readDefsym(StringRef Name) {
|
2018-11-26 20:29:56 +08:00
|
|
|
if (errorCount())
|
|
|
|
return;
|
2017-11-04 10:03:58 +08:00
|
|
|
Expr E = readExpr();
|
|
|
|
if (!atEOF())
|
|
|
|
setError("EOF expected, but got " + next());
|
2018-04-05 19:25:58 +08:00
|
|
|
SymbolAssignment *Cmd = make<SymbolAssignment>(Name, E, getCurrentLocation());
|
2017-11-04 10:03:58 +08:00
|
|
|
Script->SectionCommands.push_back(Cmd);
|
|
|
|
}
|
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
void ScriptParser::addFile(StringRef S) {
|
|
|
|
if (IsUnderSysroot && S.startswith("/")) {
|
|
|
|
SmallString<128> PathData;
|
|
|
|
StringRef Path = (Config->Sysroot + S).toStringRef(PathData);
|
|
|
|
if (sys::fs::exists(Path)) {
|
2017-04-12 08:13:48 +08:00
|
|
|
Driver->addFile(Saver.save(Path), /*WithLOption=*/false);
|
2017-04-05 13:07:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 02:17:55 +08:00
|
|
|
if (S.startswith("/")) {
|
2017-04-12 08:13:48 +08:00
|
|
|
Driver->addFile(S, /*WithLOption=*/false);
|
2017-04-05 13:07:39 +08:00
|
|
|
} else if (S.startswith("=")) {
|
|
|
|
if (Config->Sysroot.empty())
|
2017-04-12 08:13:48 +08:00
|
|
|
Driver->addFile(S.substr(1), /*WithLOption=*/false);
|
2017-04-05 13:07:39 +08:00
|
|
|
else
|
2017-04-12 08:13:48 +08:00
|
|
|
Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)),
|
|
|
|
/*WithLOption=*/false);
|
2017-04-05 13:07:39 +08:00
|
|
|
} else if (S.startswith("-l")) {
|
|
|
|
Driver->addLibrary(S.substr(2));
|
|
|
|
} else if (sys::fs::exists(S)) {
|
2017-04-12 08:13:48 +08:00
|
|
|
Driver->addFile(S, /*WithLOption=*/false);
|
2017-04-05 13:07:39 +08:00
|
|
|
} else {
|
|
|
|
if (Optional<std::string> Path = findFromSearchPaths(S))
|
2017-04-12 08:13:48 +08:00
|
|
|
Driver->addFile(Saver.save(*Path), /*WithLOption=*/true);
|
2017-04-05 13:07:39 +08:00
|
|
|
else
|
|
|
|
setError("unable to find " + S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readAsNeeded() {
|
|
|
|
expect("(");
|
|
|
|
bool Orig = Config->AsNeeded;
|
|
|
|
Config->AsNeeded = true;
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && !consume(")"))
|
2017-04-05 13:07:39 +08:00
|
|
|
addFile(unquote(next()));
|
|
|
|
Config->AsNeeded = Orig;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readEntry() {
|
|
|
|
// -e <symbol> takes predecence over ENTRY(<symbol>).
|
|
|
|
expect("(");
|
|
|
|
StringRef Tok = next();
|
|
|
|
if (Config->Entry.empty())
|
|
|
|
Config->Entry = Tok;
|
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readExtern() {
|
|
|
|
expect("(");
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && !consume(")"))
|
2017-04-05 13:07:39 +08:00
|
|
|
Config->Undefined.push_back(next());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readGroup() {
|
Add --warn-backrefs to maintain compatibility with other linkers
I'm proposing a new command line flag, --warn-backrefs in this patch.
The flag and the feature proposed below don't exist in GNU linkers
nor the current lld.
--warn-backrefs is an option to detect reverse or cyclic dependencies
between static archives, and it can be used to keep your program
compatible with GNU linkers after you switch to lld. I'll explain the
feature and why you may find it useful below.
lld's symbol resolution semantics is more relaxed than traditional
Unix linkers. Therefore,
ld.lld foo.a bar.o
succeeds even if bar.o contains an undefined symbol that have to be
resolved by some object file in foo.a. Traditional Unix linkers
don't allow this kind of backward reference, as they visit each
file only once from left to right in the command line while
resolving all undefined symbol at the moment of visiting.
In the above case, since there's no undefined symbol when a linker
visits foo.a, no files are pulled out from foo.a, and because the
linker forgets about foo.a after visiting, it can't resolve
undefined symbols that could have been resolved otherwise.
That lld accepts more relaxed form means (besides it makes more
sense) that you can accidentally write a command line or a build
file that works only with lld, even if you have a plan to
distribute it to wider users who may be using GNU linkers. With
--check-library-dependency, you can detect a library order that
doesn't work with other Unix linkers.
The option is also useful to detect cyclic dependencies between
static archives. Again, lld accepts
ld.lld foo.a bar.a
even if foo.a and bar.a depend on each other. With --warn-backrefs
it is handled as an error.
Here is how the option works. We assign a group ID to each file. A
file with a smaller group ID can pull out object files from an
archive file with an equal or greater group ID. Otherwise, it is a
reverse dependency and an error.
A file outside --{start,end}-group gets a fresh ID when
instantiated. All files within the same --{start,end}-group get the
same group ID. E.g.
ld.lld A B --start-group C D --end-group E
A and B form group 0, C, D and their member object files form group
1, and E forms group 2. I think that you can see how this group
assignment rule simulates the traditional linker's semantics.
Differential Revision: https://reviews.llvm.org/D45195
llvm-svn: 329636
2018-04-10 07:05:48 +08:00
|
|
|
bool Orig = InputFile::IsInGroup;
|
|
|
|
InputFile::IsInGroup = true;
|
|
|
|
readInput();
|
|
|
|
InputFile::IsInGroup = Orig;
|
2018-04-20 07:23:23 +08:00
|
|
|
if (!Orig)
|
|
|
|
++InputFile::NextGroupId;
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readInclude() {
|
|
|
|
StringRef Tok = unquote(next());
|
|
|
|
|
2017-09-07 02:14:08 +08:00
|
|
|
if (!Seen.insert(Tok).second) {
|
|
|
|
setError("there is a cycle in linker script INCLUDEs");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-26 05:53:18 +08:00
|
|
|
if (Optional<std::string> Path = searchScript(Tok)) {
|
2017-04-05 13:07:39 +08:00
|
|
|
if (Optional<MemoryBufferRef> MB = readFile(*Path))
|
|
|
|
tokenize(*MB);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-20 23:43:20 +08:00
|
|
|
setError("cannot find linker script " + Tok);
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
Add --warn-backrefs to maintain compatibility with other linkers
I'm proposing a new command line flag, --warn-backrefs in this patch.
The flag and the feature proposed below don't exist in GNU linkers
nor the current lld.
--warn-backrefs is an option to detect reverse or cyclic dependencies
between static archives, and it can be used to keep your program
compatible with GNU linkers after you switch to lld. I'll explain the
feature and why you may find it useful below.
lld's symbol resolution semantics is more relaxed than traditional
Unix linkers. Therefore,
ld.lld foo.a bar.o
succeeds even if bar.o contains an undefined symbol that have to be
resolved by some object file in foo.a. Traditional Unix linkers
don't allow this kind of backward reference, as they visit each
file only once from left to right in the command line while
resolving all undefined symbol at the moment of visiting.
In the above case, since there's no undefined symbol when a linker
visits foo.a, no files are pulled out from foo.a, and because the
linker forgets about foo.a after visiting, it can't resolve
undefined symbols that could have been resolved otherwise.
That lld accepts more relaxed form means (besides it makes more
sense) that you can accidentally write a command line or a build
file that works only with lld, even if you have a plan to
distribute it to wider users who may be using GNU linkers. With
--check-library-dependency, you can detect a library order that
doesn't work with other Unix linkers.
The option is also useful to detect cyclic dependencies between
static archives. Again, lld accepts
ld.lld foo.a bar.a
even if foo.a and bar.a depend on each other. With --warn-backrefs
it is handled as an error.
Here is how the option works. We assign a group ID to each file. A
file with a smaller group ID can pull out object files from an
archive file with an equal or greater group ID. Otherwise, it is a
reverse dependency and an error.
A file outside --{start,end}-group gets a fresh ID when
instantiated. All files within the same --{start,end}-group get the
same group ID. E.g.
ld.lld A B --start-group C D --end-group E
A and B form group 0, C, D and their member object files form group
1, and E forms group 2. I think that you can see how this group
assignment rule simulates the traditional linker's semantics.
Differential Revision: https://reviews.llvm.org/D45195
llvm-svn: 329636
2018-04-10 07:05:48 +08:00
|
|
|
void ScriptParser::readInput() {
|
|
|
|
expect("(");
|
|
|
|
while (!errorCount() && !consume(")")) {
|
|
|
|
if (consume("AS_NEEDED"))
|
|
|
|
readAsNeeded();
|
|
|
|
else
|
|
|
|
addFile(unquote(next()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
void ScriptParser::readOutput() {
|
|
|
|
// -o <file> takes predecence over OUTPUT(<file>).
|
|
|
|
expect("(");
|
|
|
|
StringRef Tok = next();
|
|
|
|
if (Config->OutputFile.empty())
|
|
|
|
Config->OutputFile = unquote(Tok);
|
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readOutputArch() {
|
|
|
|
// OUTPUT_ARCH is ignored for now.
|
|
|
|
expect("(");
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && !consume(")"))
|
2017-04-05 13:07:39 +08:00
|
|
|
skip();
|
|
|
|
}
|
|
|
|
|
2019-01-29 05:45:50 +08:00
|
|
|
static std::pair<ELFKind, uint16_t> parseBfdName(StringRef S) {
|
|
|
|
return StringSwitch<std::pair<ELFKind, uint16_t>>(S)
|
2019-01-29 03:11:52 +08:00
|
|
|
.Case("elf32-i386", {ELF32LEKind, EM_386})
|
|
|
|
.Case("elf32-iamcu", {ELF32LEKind, EM_IAMCU})
|
|
|
|
.Case("elf32-littlearm", {ELF32LEKind, EM_ARM})
|
|
|
|
.Case("elf32-x86-64", {ELF32LEKind, EM_X86_64})
|
|
|
|
.Case("elf64-littleaarch64", {ELF64LEKind, EM_AARCH64})
|
|
|
|
.Case("elf64-powerpc", {ELF64BEKind, EM_PPC64})
|
|
|
|
.Case("elf64-powerpcle", {ELF64LEKind, EM_PPC64})
|
|
|
|
.Case("elf64-x86-64", {ELF64LEKind, EM_X86_64})
|
|
|
|
.Case("elf32-tradbigmips", {ELF32BEKind, EM_MIPS})
|
|
|
|
.Case("elf32-ntradbigmips", {ELF32BEKind, EM_MIPS})
|
|
|
|
.Case("elf32-tradlittlemips", {ELF32LEKind, EM_MIPS})
|
|
|
|
.Case("elf32-ntradlittlemips", {ELF32LEKind, EM_MIPS})
|
|
|
|
.Case("elf64-tradbigmips", {ELF64BEKind, EM_MIPS})
|
|
|
|
.Case("elf64-tradlittlemips", {ELF64LEKind, EM_MIPS})
|
|
|
|
.Default({ELFNoneKind, EM_NONE});
|
Add OUTPUT_FORMAT linker script directive support.
This patch adds a support for OUTPUT_FORMAT linker script directive.
Since I'm not 100% confident with BFD names you can use in the directive
for all architectures, I added only a few in this patch. We can add
other names for other archtiectures later.
We still do not support triple-style OUTPUT_FORMAT directive, namely,
OUTPUT_FORMAT(bfdname, big, little). If you pass -EL (little endian)
or -EB (big endian) to the linker, GNU linkers pick up big or little
as a BFD name, correspondingly, so that you can use a single linker
script for bi-endian processor. I'm not sure if we really need to
support that, so I'll leave it alone for now.
Note that -m takes precedence over OUTPUT_FORAMT, but we always parse
a BFD name given to OUTPUT_FORMAT for error checking. You cannot write
an invalid name in the OUTPUT_FORMAT directive.
Differential Revision: https://reviews.llvm.org/D53495
llvm-svn: 344952
2018-10-23 04:50:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse OUTPUT_FORMAT(bfdname) or OUTPUT_FORMAT(bfdname, big, little).
|
|
|
|
// Currently we ignore big and little parameters.
|
2017-04-05 13:07:39 +08:00
|
|
|
void ScriptParser::readOutputFormat() {
|
|
|
|
expect("(");
|
Add OUTPUT_FORMAT linker script directive support.
This patch adds a support for OUTPUT_FORMAT linker script directive.
Since I'm not 100% confident with BFD names you can use in the directive
for all architectures, I added only a few in this patch. We can add
other names for other archtiectures later.
We still do not support triple-style OUTPUT_FORMAT directive, namely,
OUTPUT_FORMAT(bfdname, big, little). If you pass -EL (little endian)
or -EB (big endian) to the linker, GNU linkers pick up big or little
as a BFD name, correspondingly, so that you can use a single linker
script for bi-endian processor. I'm not sure if we really need to
support that, so I'll leave it alone for now.
Note that -m takes precedence over OUTPUT_FORAMT, but we always parse
a BFD name given to OUTPUT_FORMAT for error checking. You cannot write
an invalid name in the OUTPUT_FORMAT directive.
Differential Revision: https://reviews.llvm.org/D53495
llvm-svn: 344952
2018-10-23 04:50:01 +08:00
|
|
|
|
2019-01-29 03:11:52 +08:00
|
|
|
StringRef S = unquote(next());
|
|
|
|
|
|
|
|
std::tie(Config->EKind, Config->EMachine) = parseBfdName(S);
|
|
|
|
if (Config->EMachine == EM_NONE)
|
|
|
|
setError("unknown output format name: " + S);
|
|
|
|
if (S == "elf32-ntradlittlemips" || S == "elf32-ntradbigmips")
|
|
|
|
Config->MipsN32Abi = true;
|
Add OUTPUT_FORMAT linker script directive support.
This patch adds a support for OUTPUT_FORMAT linker script directive.
Since I'm not 100% confident with BFD names you can use in the directive
for all architectures, I added only a few in this patch. We can add
other names for other archtiectures later.
We still do not support triple-style OUTPUT_FORMAT directive, namely,
OUTPUT_FORMAT(bfdname, big, little). If you pass -EL (little endian)
or -EB (big endian) to the linker, GNU linkers pick up big or little
as a BFD name, correspondingly, so that you can use a single linker
script for bi-endian processor. I'm not sure if we really need to
support that, so I'll leave it alone for now.
Note that -m takes precedence over OUTPUT_FORAMT, but we always parse
a BFD name given to OUTPUT_FORMAT for error checking. You cannot write
an invalid name in the OUTPUT_FORMAT directive.
Differential Revision: https://reviews.llvm.org/D53495
llvm-svn: 344952
2018-10-23 04:50:01 +08:00
|
|
|
|
2017-04-05 13:40:21 +08:00
|
|
|
if (consume(")"))
|
2017-04-05 13:07:39 +08:00
|
|
|
return;
|
2017-04-05 13:40:21 +08:00
|
|
|
expect(",");
|
2017-04-05 13:07:39 +08:00
|
|
|
skip();
|
|
|
|
expect(",");
|
|
|
|
skip();
|
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readPhdrs() {
|
|
|
|
expect("{");
|
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && !consume("}")) {
|
2017-10-08 11:45:49 +08:00
|
|
|
PhdrsCommand Cmd;
|
|
|
|
Cmd.Name = next();
|
|
|
|
Cmd.Type = readPhdrType();
|
2017-04-05 13:40:21 +08:00
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && !consume(";")) {
|
2017-04-05 13:40:21 +08:00
|
|
|
if (consume("FILEHDR"))
|
2017-10-08 11:45:49 +08:00
|
|
|
Cmd.HasFilehdr = true;
|
2017-04-05 13:40:21 +08:00
|
|
|
else if (consume("PHDRS"))
|
2017-10-08 11:45:49 +08:00
|
|
|
Cmd.HasPhdrs = true;
|
2017-04-05 13:40:21 +08:00
|
|
|
else if (consume("AT"))
|
2017-10-08 11:45:49 +08:00
|
|
|
Cmd.LMAExpr = readParenExpr();
|
2017-04-05 13:40:21 +08:00
|
|
|
else if (consume("FLAGS"))
|
2017-10-08 11:45:49 +08:00
|
|
|
Cmd.Flags = readParenExpr()().getValue();
|
2017-04-05 13:40:21 +08:00
|
|
|
else
|
|
|
|
setError("unexpected header attribute: " + next());
|
|
|
|
}
|
2017-10-08 11:45:49 +08:00
|
|
|
|
2017-10-11 09:19:33 +08:00
|
|
|
Script->PhdrsCommands.push_back(Cmd);
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 16:23:15 +08:00
|
|
|
void ScriptParser::readRegionAlias() {
|
|
|
|
expect("(");
|
|
|
|
StringRef Alias = unquote(next());
|
|
|
|
expect(",");
|
|
|
|
StringRef Name = next();
|
|
|
|
expect(")");
|
|
|
|
|
2017-10-11 09:19:33 +08:00
|
|
|
if (Script->MemoryRegions.count(Alias))
|
2017-09-08 16:23:15 +08:00
|
|
|
setError("redefinition of memory region '" + Alias + "'");
|
2017-10-11 09:19:33 +08:00
|
|
|
if (!Script->MemoryRegions.count(Name))
|
2017-09-08 16:23:15 +08:00
|
|
|
setError("memory region '" + Name + "' is not defined");
|
2017-11-03 16:21:51 +08:00
|
|
|
Script->MemoryRegions.insert({Alias, Script->MemoryRegions[Name]});
|
2017-09-08 16:23:15 +08:00
|
|
|
}
|
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
void ScriptParser::readSearchDir() {
|
|
|
|
expect("(");
|
|
|
|
StringRef Tok = next();
|
|
|
|
if (!Config->Nostdlib)
|
|
|
|
Config->SearchPaths.push_back(unquote(Tok));
|
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
2018-06-27 16:08:12 +08:00
|
|
|
// This reads an overlay description. Overlays are used to describe output
|
|
|
|
// sections that use the same virtual memory range and normally would trigger
|
|
|
|
// linker's sections sanity check failures.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Overlay-Description.html#Overlay-Description
|
|
|
|
std::vector<BaseCommand *> ScriptParser::readOverlay() {
|
|
|
|
// VA and LMA expressions are optional, though for simplicity of
|
|
|
|
// implementation we assume they are not. That is what OVERLAY was designed
|
|
|
|
// for first of all: to allow sections with overlapping VAs at different LMAs.
|
|
|
|
Expr AddrExpr = readExpr();
|
|
|
|
expect(":");
|
|
|
|
expect("AT");
|
|
|
|
Expr LMAExpr = readParenExpr();
|
|
|
|
expect("{");
|
|
|
|
|
|
|
|
std::vector<BaseCommand *> V;
|
|
|
|
OutputSection *Prev = nullptr;
|
|
|
|
while (!errorCount() && !consume("}")) {
|
|
|
|
// VA is the same for all sections. The LMAs are consecutive in memory
|
|
|
|
// starting from the base load address specified.
|
|
|
|
OutputSection *OS = readOverlaySectionDescription();
|
|
|
|
OS->AddrExpr = AddrExpr;
|
|
|
|
if (Prev)
|
|
|
|
OS->LMAExpr = [=] { return Prev->getLMA() + Prev->Size; };
|
|
|
|
else
|
|
|
|
OS->LMAExpr = LMAExpr;
|
|
|
|
V.push_back(OS);
|
|
|
|
Prev = OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// According to the specification, at the end of the overlay, the location
|
|
|
|
// counter should be equal to the overlay base address plus size of the
|
|
|
|
// largest section seen in the overlay.
|
|
|
|
// Here we want to create the Dot assignment command to achieve that.
|
|
|
|
Expr MoveDot = [=] {
|
|
|
|
uint64_t Max = 0;
|
|
|
|
for (BaseCommand *Cmd : V)
|
|
|
|
Max = std::max(Max, cast<OutputSection>(Cmd)->Size);
|
|
|
|
return AddrExpr().getValue() + Max;
|
|
|
|
};
|
|
|
|
V.push_back(make<SymbolAssignment>(".", MoveDot, getCurrentLocation()));
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
void ScriptParser::readSections() {
|
2017-10-11 09:34:51 +08:00
|
|
|
Script->HasSectionsCommand = true;
|
2017-04-05 13:40:21 +08:00
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
// -no-rosegment is used to avoid placing read only non-executable sections in
|
|
|
|
// their own segment. We do the same if SECTIONS command is present in linker
|
|
|
|
// script. See comment for computeFlags().
|
|
|
|
Config->SingleRoRx = true;
|
|
|
|
|
|
|
|
expect("{");
|
2018-03-08 22:54:38 +08:00
|
|
|
std::vector<BaseCommand *> V;
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && !consume("}")) {
|
2017-04-05 13:07:39 +08:00
|
|
|
StringRef Tok = next();
|
2018-06-27 16:08:12 +08:00
|
|
|
if (Tok == "OVERLAY") {
|
|
|
|
for (BaseCommand *Cmd : readOverlay())
|
|
|
|
V.push_back(Cmd);
|
|
|
|
continue;
|
2018-10-13 01:07:32 +08:00
|
|
|
} else if (Tok == "INCLUDE") {
|
|
|
|
readInclude();
|
|
|
|
continue;
|
2018-06-27 16:08:12 +08:00
|
|
|
}
|
|
|
|
|
2018-04-25 19:16:31 +08:00
|
|
|
if (BaseCommand *Cmd = readAssignment(Tok))
|
|
|
|
V.push_back(Cmd);
|
|
|
|
else
|
|
|
|
V.push_back(readOutputSectionDescription(Tok));
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
2018-03-08 22:54:38 +08:00
|
|
|
|
|
|
|
if (!atEOF() && consume("INSERT")) {
|
2018-03-13 17:18:11 +08:00
|
|
|
std::vector<BaseCommand *> *Dest = nullptr;
|
|
|
|
if (consume("AFTER"))
|
|
|
|
Dest = &Script->InsertAfterCommands[next()];
|
|
|
|
else if (consume("BEFORE"))
|
|
|
|
Dest = &Script->InsertBeforeCommands[next()];
|
|
|
|
else
|
|
|
|
setError("expected AFTER/BEFORE, but got '" + next() + "'");
|
|
|
|
if (Dest)
|
|
|
|
Dest->insert(Dest->end(), V.begin(), V.end());
|
2018-03-08 22:54:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Script->SectionCommands.insert(Script->SectionCommands.end(), V.begin(),
|
|
|
|
V.end());
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
2018-08-07 05:29:41 +08:00
|
|
|
void ScriptParser::readTarget() {
|
|
|
|
// TARGET(foo) is an alias for "--format foo". Unlike GNU linkers,
|
|
|
|
// we accept only a limited set of BFD names (i.e. "elf" or "binary")
|
|
|
|
// for --format. We recognize only /^elf/ and "binary" in the linker
|
|
|
|
// script as well.
|
|
|
|
expect("(");
|
|
|
|
StringRef Tok = next();
|
|
|
|
expect(")");
|
|
|
|
|
|
|
|
if (Tok.startswith("elf"))
|
|
|
|
Config->FormatBinary = false;
|
|
|
|
else if (Tok == "binary")
|
|
|
|
Config->FormatBinary = true;
|
|
|
|
else
|
|
|
|
setError("unknown target: " + Tok);
|
|
|
|
}
|
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
static int precedence(StringRef Op) {
|
|
|
|
return StringSwitch<int>(Op)
|
2018-07-03 22:02:52 +08:00
|
|
|
.Cases("*", "/", "%", 8)
|
|
|
|
.Cases("+", "-", 7)
|
|
|
|
.Cases("<<", ">>", 6)
|
|
|
|
.Cases("<", "<=", ">", ">=", "==", "!=", 5)
|
|
|
|
.Case("&", 4)
|
|
|
|
.Case("|", 3)
|
|
|
|
.Case("&&", 2)
|
|
|
|
.Case("||", 1)
|
2017-04-05 13:07:39 +08:00
|
|
|
.Default(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringMatcher ScriptParser::readFilePatterns() {
|
|
|
|
std::vector<StringRef> V;
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && !consume(")"))
|
2017-04-05 13:07:39 +08:00
|
|
|
V.push_back(next());
|
|
|
|
return StringMatcher(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
SortSectionPolicy ScriptParser::readSortKind() {
|
|
|
|
if (consume("SORT") || consume("SORT_BY_NAME"))
|
|
|
|
return SortSectionPolicy::Name;
|
|
|
|
if (consume("SORT_BY_ALIGNMENT"))
|
|
|
|
return SortSectionPolicy::Alignment;
|
|
|
|
if (consume("SORT_BY_INIT_PRIORITY"))
|
|
|
|
return SortSectionPolicy::Priority;
|
|
|
|
if (consume("SORT_NONE"))
|
|
|
|
return SortSectionPolicy::None;
|
|
|
|
return SortSectionPolicy::Default;
|
|
|
|
}
|
|
|
|
|
2017-04-06 03:20:54 +08:00
|
|
|
// Reads SECTIONS command contents in the following form:
|
|
|
|
//
|
|
|
|
// <contents> ::= <elem>*
|
|
|
|
// <elem> ::= <exclude>? <glob-pattern>
|
|
|
|
// <exclude> ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
|
|
|
|
//
|
|
|
|
// For example,
|
|
|
|
//
|
|
|
|
// *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
|
|
|
|
//
|
|
|
|
// is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
|
|
|
|
// The semantics of that is section .foo in any file, section .bar in
|
|
|
|
// any file but a.o, and section .baz in any file but b.o.
|
2017-04-05 13:07:39 +08:00
|
|
|
std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
|
|
|
|
std::vector<SectionPattern> Ret;
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && peek() != ")") {
|
2017-04-05 13:07:39 +08:00
|
|
|
StringMatcher ExcludeFilePat;
|
|
|
|
if (consume("EXCLUDE_FILE")) {
|
|
|
|
expect("(");
|
|
|
|
ExcludeFilePat = readFilePatterns();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StringRef> V;
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && peek() != ")" && peek() != "EXCLUDE_FILE")
|
2017-04-05 13:07:39 +08:00
|
|
|
V.push_back(next());
|
|
|
|
|
|
|
|
if (!V.empty())
|
|
|
|
Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)});
|
|
|
|
else
|
|
|
|
setError("section pattern is expected");
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads contents of "SECTIONS" directive. That directive contains a
|
|
|
|
// list of glob patterns for input sections. The grammar is as follows.
|
|
|
|
//
|
|
|
|
// <patterns> ::= <section-list>
|
|
|
|
// | <sort> "(" <section-list> ")"
|
|
|
|
// | <sort> "(" <sort> "(" <section-list> ")" ")"
|
|
|
|
//
|
|
|
|
// <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
|
|
|
|
// | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
|
|
|
|
//
|
|
|
|
// <section-list> is parsed by readInputSectionsList().
|
|
|
|
InputSectionDescription *
|
|
|
|
ScriptParser::readInputSectionRules(StringRef FilePattern) {
|
|
|
|
auto *Cmd = make<InputSectionDescription>(FilePattern);
|
|
|
|
expect("(");
|
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && !consume(")")) {
|
2017-04-05 13:07:39 +08:00
|
|
|
SortSectionPolicy Outer = readSortKind();
|
|
|
|
SortSectionPolicy Inner = SortSectionPolicy::Default;
|
|
|
|
std::vector<SectionPattern> V;
|
|
|
|
if (Outer != SortSectionPolicy::Default) {
|
|
|
|
expect("(");
|
|
|
|
Inner = readSortKind();
|
|
|
|
if (Inner != SortSectionPolicy::Default) {
|
|
|
|
expect("(");
|
|
|
|
V = readInputSectionsList();
|
|
|
|
expect(")");
|
|
|
|
} else {
|
|
|
|
V = readInputSectionsList();
|
|
|
|
}
|
|
|
|
expect(")");
|
|
|
|
} else {
|
|
|
|
V = readInputSectionsList();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (SectionPattern &Pat : V) {
|
|
|
|
Pat.SortInner = Inner;
|
|
|
|
Pat.SortOuter = Outer;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
|
|
|
|
}
|
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
InputSectionDescription *
|
|
|
|
ScriptParser::readInputSectionDescription(StringRef Tok) {
|
|
|
|
// Input section wildcard can be surrounded by KEEP.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
|
|
|
|
if (Tok == "KEEP") {
|
|
|
|
expect("(");
|
|
|
|
StringRef FilePattern = next();
|
|
|
|
InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
|
|
|
|
expect(")");
|
2017-10-11 09:19:33 +08:00
|
|
|
Script->KeptSections.push_back(Cmd);
|
2017-04-05 13:07:39 +08:00
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
return readInputSectionRules(Tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptParser::readSort() {
|
|
|
|
expect("(");
|
|
|
|
expect("CONSTRUCTORS");
|
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:16:31 +08:00
|
|
|
Expr ScriptParser::readAssert() {
|
2017-04-05 13:07:39 +08:00
|
|
|
expect("(");
|
|
|
|
Expr E = readExpr();
|
|
|
|
expect(",");
|
|
|
|
StringRef Msg = unquote(next());
|
|
|
|
expect(")");
|
2017-04-05 13:40:21 +08:00
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
return [=] {
|
|
|
|
if (!E().getValue())
|
|
|
|
error(Msg);
|
|
|
|
return Script->getDot();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads a FILL(expr) command. We handle the FILL command as an
|
|
|
|
// alias for =fillexp section attribute, which is different from
|
|
|
|
// what GNU linkers do.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
|
2018-11-15 05:05:20 +08:00
|
|
|
std::array<uint8_t, 4> ScriptParser::readFill() {
|
2017-04-05 13:07:39 +08:00
|
|
|
expect("(");
|
2018-11-15 05:05:20 +08:00
|
|
|
std::array<uint8_t, 4> V = parseFill(next());
|
2017-04-05 13:07:39 +08:00
|
|
|
expect(")");
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2018-08-28 16:39:21 +08:00
|
|
|
// Tries to read the special directive for an output section definition which
|
|
|
|
// can be one of following: "(NOLOAD)", "(COPY)", "(INFO)" or "(OVERLAY)".
|
|
|
|
// Tok1 and Tok2 are next 2 tokens peeked. See comment for readSectionAddressType below.
|
|
|
|
bool ScriptParser::readSectionDirective(OutputSection *Cmd, StringRef Tok1, StringRef Tok2) {
|
|
|
|
if (Tok1 != "(")
|
|
|
|
return false;
|
|
|
|
if (Tok2 != "NOLOAD" && Tok2 != "COPY" && Tok2 != "INFO" && Tok2 != "OVERLAY")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
expect("(");
|
|
|
|
if (consume("NOLOAD")) {
|
|
|
|
Cmd->Noload = true;
|
|
|
|
} else {
|
|
|
|
skip(); // This is "COPY", "INFO" or "OVERLAY".
|
|
|
|
Cmd->NonAlloc = true;
|
|
|
|
}
|
|
|
|
expect(")");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-16 18:42:58 +08:00
|
|
|
// Reads an expression and/or the special directive for an output
|
|
|
|
// section definition. Directive is one of following: "(NOLOAD)",
|
|
|
|
// "(COPY)", "(INFO)" or "(OVERLAY)".
|
2017-06-09 03:47:16 +08:00
|
|
|
//
|
|
|
|
// An output section name can be followed by an address expression
|
2018-02-16 18:42:58 +08:00
|
|
|
// and/or directive. This grammar is not LL(1) because "(" can be
|
2018-02-16 18:46:50 +08:00
|
|
|
// interpreted as either the beginning of some expression or beginning
|
2018-02-16 18:42:58 +08:00
|
|
|
// of directive.
|
2017-06-09 03:47:16 +08:00
|
|
|
//
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
|
2017-07-28 03:22:43 +08:00
|
|
|
void ScriptParser::readSectionAddressType(OutputSection *Cmd) {
|
2018-08-28 16:39:21 +08:00
|
|
|
if (readSectionDirective(Cmd, peek(), peek2()))
|
|
|
|
return;
|
2017-04-05 13:07:39 +08:00
|
|
|
|
2018-08-28 16:39:21 +08:00
|
|
|
Cmd->AddrExpr = readExpr();
|
|
|
|
if (peek() == "(" && !readSectionDirective(Cmd, "(", peek2()))
|
|
|
|
setError("unknown section directive: " + peek2());
|
2017-06-09 03:47:16 +08:00
|
|
|
}
|
|
|
|
|
2017-10-25 22:50:51 +08:00
|
|
|
static Expr checkAlignment(Expr E, std::string &Loc) {
|
|
|
|
return [=] {
|
|
|
|
uint64_t Alignment = std::max((uint64_t)1, E().getValue());
|
|
|
|
if (!isPowerOf2_64(Alignment)) {
|
|
|
|
error(Loc + ": alignment must be power of 2");
|
|
|
|
return (uint64_t)1; // Return a dummy value.
|
|
|
|
}
|
|
|
|
return Alignment;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-27 16:08:12 +08:00
|
|
|
OutputSection *ScriptParser::readOverlaySectionDescription() {
|
|
|
|
OutputSection *Cmd =
|
|
|
|
Script->createOutputSection(next(), getCurrentLocation());
|
|
|
|
Cmd->InOverlay = true;
|
|
|
|
expect("{");
|
|
|
|
while (!errorCount() && !consume("}"))
|
|
|
|
Cmd->SectionCommands.push_back(readInputSectionRules(next()));
|
|
|
|
Cmd->Phdrs = readOutputSectionPhdrs();
|
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *ScriptParser::readOutputSectionDescription(StringRef OutSec) {
|
|
|
|
OutputSection *Cmd =
|
|
|
|
Script->createOutputSection(OutSec, getCurrentLocation());
|
2017-06-09 03:47:16 +08:00
|
|
|
|
2018-03-01 20:27:04 +08:00
|
|
|
size_t SymbolsReferenced = Script->ReferencedSymbols.size();
|
|
|
|
|
2017-06-09 03:47:16 +08:00
|
|
|
if (peek() != ":")
|
|
|
|
readSectionAddressType(Cmd);
|
2017-04-05 13:07:39 +08:00
|
|
|
expect(":");
|
|
|
|
|
2017-10-25 22:50:51 +08:00
|
|
|
std::string Location = getCurrentLocation();
|
2017-04-05 13:07:39 +08:00
|
|
|
if (consume("AT"))
|
|
|
|
Cmd->LMAExpr = readParenExpr();
|
|
|
|
if (consume("ALIGN"))
|
2017-10-25 22:50:51 +08:00
|
|
|
Cmd->AlignExpr = checkAlignment(readParenExpr(), Location);
|
2017-04-05 13:07:39 +08:00
|
|
|
if (consume("SUBALIGN"))
|
2017-10-25 22:50:51 +08:00
|
|
|
Cmd->SubalignExpr = checkAlignment(readParenExpr(), Location);
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
// Parse constraints.
|
|
|
|
if (consume("ONLY_IF_RO"))
|
|
|
|
Cmd->Constraint = ConstraintKind::ReadOnly;
|
|
|
|
if (consume("ONLY_IF_RW"))
|
|
|
|
Cmd->Constraint = ConstraintKind::ReadWrite;
|
|
|
|
expect("{");
|
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && !consume("}")) {
|
2017-04-05 13:07:39 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok == ";") {
|
|
|
|
// Empty commands are allowed. Do nothing here.
|
2018-04-25 19:16:31 +08:00
|
|
|
} else if (SymbolAssignment *Assign = readAssignment(Tok)) {
|
2017-10-11 09:50:56 +08:00
|
|
|
Cmd->SectionCommands.push_back(Assign);
|
2017-10-11 12:22:09 +08:00
|
|
|
} else if (ByteCommand *Data = readByteCommand(Tok)) {
|
2017-10-11 09:50:56 +08:00
|
|
|
Cmd->SectionCommands.push_back(Data);
|
2017-04-05 13:07:39 +08:00
|
|
|
} else if (Tok == "CONSTRUCTORS") {
|
|
|
|
// CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
|
|
|
|
// by name. This is for very old file formats such as ECOFF/XCOFF.
|
|
|
|
// For ELF, we should ignore.
|
|
|
|
} else if (Tok == "FILL") {
|
|
|
|
Cmd->Filler = readFill();
|
|
|
|
} else if (Tok == "SORT") {
|
|
|
|
readSort();
|
2018-10-13 01:07:32 +08:00
|
|
|
} else if (Tok == "INCLUDE") {
|
|
|
|
readInclude();
|
2017-04-05 13:07:39 +08:00
|
|
|
} else if (peek() == "(") {
|
2017-10-11 09:50:56 +08:00
|
|
|
Cmd->SectionCommands.push_back(readInputSectionDescription(Tok));
|
2017-04-05 13:07:39 +08:00
|
|
|
} else {
|
2018-12-06 16:34:52 +08:00
|
|
|
// We have a file name and no input sections description. It is not a
|
|
|
|
// commonly used syntax, but still acceptable. In that case, all sections
|
|
|
|
// from the file will be included.
|
2018-12-06 18:56:11 +08:00
|
|
|
auto *ISD = make<InputSectionDescription>(Tok);
|
2018-12-06 16:34:52 +08:00
|
|
|
ISD->SectionPatterns.push_back({{}, StringMatcher({"*"})});
|
|
|
|
Cmd->SectionCommands.push_back(ISD);
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (consume(">"))
|
|
|
|
Cmd->MemoryRegionName = next();
|
|
|
|
|
2018-01-12 17:07:35 +08:00
|
|
|
if (consume("AT")) {
|
|
|
|
expect(">");
|
|
|
|
Cmd->LMARegionName = next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Cmd->LMAExpr && !Cmd->LMARegionName.empty())
|
|
|
|
error("section can't have both LMA and a load region");
|
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
Cmd->Phdrs = readOutputSectionPhdrs();
|
|
|
|
|
|
|
|
if (consume("="))
|
2017-04-14 07:40:19 +08:00
|
|
|
Cmd->Filler = parseFill(next());
|
2017-04-05 13:07:39 +08:00
|
|
|
else if (peek().startswith("="))
|
2017-04-14 07:40:19 +08:00
|
|
|
Cmd->Filler = parseFill(next().drop_front());
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
// Consume optional comma following output section command.
|
|
|
|
consume(",");
|
|
|
|
|
2018-03-01 20:27:04 +08:00
|
|
|
if (Script->ReferencedSymbols.size() > SymbolsReferenced)
|
|
|
|
Cmd->ExpressionsUseSymbols = true;
|
2017-04-05 13:07:39 +08:00
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
|
2017-04-14 07:40:19 +08:00
|
|
|
// Parses a given string as a octal/decimal/hexadecimal number and
|
|
|
|
// returns it as a big-endian number. Used for `=<fillexp>`.
|
2017-04-05 13:07:39 +08:00
|
|
|
// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
|
|
|
|
//
|
2017-04-14 07:40:19 +08:00
|
|
|
// When reading a hexstring, ld.bfd handles it as a blob of arbitrary
|
|
|
|
// size, while ld.gold always handles it as a 32-bit big-endian number.
|
|
|
|
// We are compatible with ld.gold because it's easier to implement.
|
2018-11-15 05:05:20 +08:00
|
|
|
std::array<uint8_t, 4> ScriptParser::parseFill(StringRef Tok) {
|
2017-04-12 06:45:57 +08:00
|
|
|
uint32_t V = 0;
|
2017-05-16 16:19:25 +08:00
|
|
|
if (!to_integer(Tok, V))
|
2017-04-12 06:45:57 +08:00
|
|
|
setError("invalid filler expression: " + Tok);
|
|
|
|
|
2018-11-15 05:05:20 +08:00
|
|
|
std::array<uint8_t, 4> Buf;
|
|
|
|
write32be(Buf.data(), V);
|
2017-04-12 06:45:57 +08:00
|
|
|
return Buf;
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
|
|
|
|
expect("(");
|
2018-04-25 19:16:31 +08:00
|
|
|
SymbolAssignment *Cmd = readSymbolAssignment(next());
|
2017-04-05 13:07:39 +08:00
|
|
|
Cmd->Provide = Provide;
|
|
|
|
Cmd->Hidden = Hidden;
|
|
|
|
expect(")");
|
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:16:31 +08:00
|
|
|
SymbolAssignment *ScriptParser::readAssignment(StringRef Tok) {
|
|
|
|
// Assert expression returns Dot, so this is equal to ".=."
|
|
|
|
if (Tok == "ASSERT")
|
|
|
|
return make<SymbolAssignment>(".", readAssert(), getCurrentLocation());
|
|
|
|
|
2018-04-05 19:25:58 +08:00
|
|
|
size_t OldPos = Pos;
|
2017-04-05 13:07:39 +08:00
|
|
|
SymbolAssignment *Cmd = nullptr;
|
2018-04-05 19:25:58 +08:00
|
|
|
if (peek() == "=" || peek() == "+=")
|
2018-04-25 19:16:31 +08:00
|
|
|
Cmd = readSymbolAssignment(Tok);
|
2018-04-05 19:25:58 +08:00
|
|
|
else if (Tok == "PROVIDE")
|
2017-04-05 13:07:39 +08:00
|
|
|
Cmd = readProvideHidden(true, false);
|
2018-04-05 19:25:58 +08:00
|
|
|
else if (Tok == "HIDDEN")
|
2017-04-05 13:07:39 +08:00
|
|
|
Cmd = readProvideHidden(false, true);
|
2018-04-05 19:25:58 +08:00
|
|
|
else if (Tok == "PROVIDE_HIDDEN")
|
2017-04-05 13:07:39 +08:00
|
|
|
Cmd = readProvideHidden(true, true);
|
2018-04-05 19:25:58 +08:00
|
|
|
|
|
|
|
if (Cmd) {
|
|
|
|
Cmd->CommandString =
|
|
|
|
Tok.str() + " " +
|
|
|
|
llvm::join(Tokens.begin() + OldPos, Tokens.begin() + Pos, " ");
|
|
|
|
expect(";");
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
return Cmd;
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:16:31 +08:00
|
|
|
SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef Name) {
|
2017-04-05 13:07:39 +08:00
|
|
|
StringRef Op = next();
|
|
|
|
assert(Op == "=" || Op == "+=");
|
|
|
|
Expr E = readExpr();
|
|
|
|
if (Op == "+=") {
|
|
|
|
std::string Loc = getCurrentLocation();
|
2017-10-11 12:34:34 +08:00
|
|
|
E = [=] { return add(Script->getSymbolValue(Name, Loc), E()); };
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
2018-04-05 19:25:58 +08:00
|
|
|
return make<SymbolAssignment>(Name, E, getCurrentLocation());
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is an operator-precedence parser to parse a linker
|
|
|
|
// script expression.
|
|
|
|
Expr ScriptParser::readExpr() {
|
|
|
|
// Our lexer is context-aware. Set the in-expression bit so that
|
|
|
|
// they apply different tokenization rules.
|
|
|
|
bool Orig = InExpr;
|
|
|
|
InExpr = true;
|
|
|
|
Expr E = readExpr1(readPrimary(), 0);
|
|
|
|
InExpr = Orig;
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
2018-03-05 18:02:44 +08:00
|
|
|
Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) {
|
2017-04-05 13:07:39 +08:00
|
|
|
if (Op == "+")
|
|
|
|
return [=] { return add(L(), R()); };
|
|
|
|
if (Op == "-")
|
|
|
|
return [=] { return sub(L(), R()); };
|
2017-04-05 13:40:21 +08:00
|
|
|
if (Op == "*")
|
2018-03-01 02:25:00 +08:00
|
|
|
return [=] { return L().getValue() * R().getValue(); };
|
2018-03-05 18:02:44 +08:00
|
|
|
if (Op == "/") {
|
|
|
|
std::string Loc = getCurrentLocation();
|
|
|
|
return [=]() -> uint64_t {
|
|
|
|
if (uint64_t RV = R().getValue())
|
|
|
|
return L().getValue() / RV;
|
|
|
|
error(Loc + ": division by zero");
|
2018-03-06 07:50:45 +08:00
|
|
|
return 0;
|
2018-03-05 18:02:44 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (Op == "%") {
|
|
|
|
std::string Loc = getCurrentLocation();
|
|
|
|
return [=]() -> uint64_t {
|
|
|
|
if (uint64_t RV = R().getValue())
|
|
|
|
return L().getValue() % RV;
|
|
|
|
error(Loc + ": modulo by zero");
|
2018-03-06 07:50:45 +08:00
|
|
|
return 0;
|
2018-03-05 18:02:44 +08:00
|
|
|
};
|
|
|
|
}
|
2017-04-05 13:07:39 +08:00
|
|
|
if (Op == "<<")
|
2017-04-05 13:40:01 +08:00
|
|
|
return [=] { return L().getValue() << R().getValue(); };
|
2017-04-05 13:07:39 +08:00
|
|
|
if (Op == ">>")
|
2017-04-05 13:40:01 +08:00
|
|
|
return [=] { return L().getValue() >> R().getValue(); };
|
2017-04-05 13:07:39 +08:00
|
|
|
if (Op == "<")
|
|
|
|
return [=] { return L().getValue() < R().getValue(); };
|
|
|
|
if (Op == ">")
|
|
|
|
return [=] { return L().getValue() > R().getValue(); };
|
|
|
|
if (Op == ">=")
|
|
|
|
return [=] { return L().getValue() >= R().getValue(); };
|
|
|
|
if (Op == "<=")
|
|
|
|
return [=] { return L().getValue() <= R().getValue(); };
|
|
|
|
if (Op == "==")
|
|
|
|
return [=] { return L().getValue() == R().getValue(); };
|
|
|
|
if (Op == "!=")
|
|
|
|
return [=] { return L().getValue() != R().getValue(); };
|
2018-07-03 22:02:52 +08:00
|
|
|
if (Op == "||")
|
|
|
|
return [=] { return L().getValue() || R().getValue(); };
|
|
|
|
if (Op == "&&")
|
|
|
|
return [=] { return L().getValue() && R().getValue(); };
|
2017-04-05 13:07:39 +08:00
|
|
|
if (Op == "&")
|
|
|
|
return [=] { return bitAnd(L(), R()); };
|
|
|
|
if (Op == "|")
|
|
|
|
return [=] { return bitOr(L(), R()); };
|
|
|
|
llvm_unreachable("invalid operator");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!atEOF() && !errorCount()) {
|
2017-04-05 13:07:39 +08:00
|
|
|
// Read an operator and an expression.
|
|
|
|
if (consume("?"))
|
|
|
|
return readTernary(Lhs);
|
|
|
|
StringRef Op1 = peek();
|
|
|
|
if (precedence(Op1) < MinPrec)
|
|
|
|
break;
|
|
|
|
skip();
|
|
|
|
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))
|
|
|
|
break;
|
|
|
|
Rhs = readExpr1(Rhs, precedence(Op2));
|
|
|
|
}
|
|
|
|
|
|
|
|
Lhs = combine(Op1, Lhs, Rhs);
|
|
|
|
}
|
|
|
|
return Lhs;
|
|
|
|
}
|
|
|
|
|
2017-08-04 00:05:08 +08:00
|
|
|
Expr ScriptParser::getPageSize() {
|
|
|
|
std::string Location = getCurrentLocation();
|
|
|
|
return [=]() -> uint64_t {
|
|
|
|
if (Target)
|
|
|
|
return Target->PageSize;
|
|
|
|
error(Location + ": unable to calculate page size");
|
|
|
|
return 4096; // Return a dummy value.
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr ScriptParser::readConstant() {
|
|
|
|
StringRef S = readParenLiteral();
|
2017-04-05 13:07:39 +08:00
|
|
|
if (S == "COMMONPAGESIZE")
|
2017-08-04 00:05:08 +08:00
|
|
|
return getPageSize();
|
2017-04-05 13:07:39 +08:00
|
|
|
if (S == "MAXPAGESIZE")
|
2017-08-04 00:05:08 +08:00
|
|
|
return [] { return Config->MaxPageSize; };
|
|
|
|
setError("unknown constant: " + S);
|
2018-03-01 20:36:01 +08:00
|
|
|
return [] { return 0; };
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
2017-04-06 07:22:11 +08:00
|
|
|
// Parses Tok as an integer. It recognizes hexadecimal (prefixed with
|
|
|
|
// "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
|
|
|
|
// have "K" (Ki) or "M" (Mi) suffixes.
|
|
|
|
static Optional<uint64_t> parseInt(StringRef Tok) {
|
2017-04-05 13:07:39 +08:00
|
|
|
// Hexadecimal
|
2017-04-06 07:22:11 +08:00
|
|
|
uint64_t Val;
|
2017-10-12 03:30:39 +08:00
|
|
|
if (Tok.startswith_lower("0x")) {
|
|
|
|
if (!to_integer(Tok.substr(2), Val, 16))
|
|
|
|
return None;
|
2017-04-06 07:22:11 +08:00
|
|
|
return Val;
|
2017-10-12 03:30:39 +08:00
|
|
|
}
|
|
|
|
if (Tok.endswith_lower("H")) {
|
|
|
|
if (!to_integer(Tok.drop_back(), Val, 16))
|
|
|
|
return None;
|
2017-04-06 07:22:11 +08:00
|
|
|
return Val;
|
2017-10-12 03:30:39 +08:00
|
|
|
}
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
// Decimal
|
|
|
|
if (Tok.endswith_lower("K")) {
|
2017-05-16 16:19:25 +08:00
|
|
|
if (!to_integer(Tok.drop_back(), Val, 10))
|
2017-04-06 07:22:11 +08:00
|
|
|
return None;
|
|
|
|
return Val * 1024;
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
2017-04-06 07:22:11 +08:00
|
|
|
if (Tok.endswith_lower("M")) {
|
2017-05-16 16:19:25 +08:00
|
|
|
if (!to_integer(Tok.drop_back(), Val, 10))
|
2017-04-06 07:22:11 +08:00
|
|
|
return None;
|
|
|
|
return Val * 1024 * 1024;
|
|
|
|
}
|
2017-05-16 16:19:25 +08:00
|
|
|
if (!to_integer(Tok, Val, 10))
|
2017-04-06 07:22:11 +08:00
|
|
|
return None;
|
|
|
|
return Val;
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 12:22:09 +08:00
|
|
|
ByteCommand *ScriptParser::readByteCommand(StringRef Tok) {
|
2017-04-05 13:40:21 +08:00
|
|
|
int Size = StringSwitch<int>(Tok)
|
2017-04-05 13:07:39 +08:00
|
|
|
.Case("BYTE", 1)
|
|
|
|
.Case("SHORT", 2)
|
|
|
|
.Case("LONG", 4)
|
|
|
|
.Case("QUAD", 8)
|
|
|
|
.Default(-1);
|
|
|
|
if (Size == -1)
|
|
|
|
return nullptr;
|
2018-03-15 17:16:40 +08:00
|
|
|
|
|
|
|
size_t OldPos = Pos;
|
|
|
|
Expr E = readParenExpr();
|
|
|
|
std::string CommandString =
|
|
|
|
Tok.str() + " " +
|
|
|
|
llvm::join(Tokens.begin() + OldPos, Tokens.begin() + Pos, " ");
|
|
|
|
return make<ByteCommand>(E, Size, CommandString);
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StringRef ScriptParser::readParenLiteral() {
|
|
|
|
expect("(");
|
2018-01-18 09:14:57 +08:00
|
|
|
bool Orig = InExpr;
|
|
|
|
InExpr = false;
|
2017-04-05 13:07:39 +08:00
|
|
|
StringRef Tok = next();
|
2018-01-18 09:14:57 +08:00
|
|
|
InExpr = Orig;
|
2017-04-05 13:07:39 +08:00
|
|
|
expect(")");
|
|
|
|
return Tok;
|
|
|
|
}
|
|
|
|
|
2017-10-08 10:27:02 +08:00
|
|
|
static void checkIfExists(OutputSection *Cmd, StringRef Location) {
|
2017-06-01 09:16:50 +08:00
|
|
|
if (Cmd->Location.empty() && Script->ErrorOnMissingSection)
|
|
|
|
error(Location + ": undefined section " + Cmd->Name);
|
|
|
|
}
|
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
Expr ScriptParser::readPrimary() {
|
|
|
|
if (peek() == "(")
|
|
|
|
return readParenExpr();
|
|
|
|
|
2017-04-06 07:22:11 +08:00
|
|
|
if (consume("~")) {
|
2017-04-05 13:07:39 +08:00
|
|
|
Expr E = readPrimary();
|
2017-04-06 03:21:15 +08:00
|
|
|
return [=] { return ~E().getValue(); };
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
2017-08-10 23:25:47 +08:00
|
|
|
if (consume("!")) {
|
|
|
|
Expr E = readPrimary();
|
|
|
|
return [=] { return !E().getValue(); };
|
|
|
|
}
|
2017-04-06 07:22:11 +08:00
|
|
|
if (consume("-")) {
|
2017-04-05 13:07:39 +08:00
|
|
|
Expr E = readPrimary();
|
2017-04-06 03:21:15 +08:00
|
|
|
return [=] { return -E().getValue(); };
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
2017-04-06 07:22:11 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
std::string Location = getCurrentLocation();
|
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
// Built-in functions are parsed here.
|
|
|
|
// https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
|
|
|
|
if (Tok == "ABSOLUTE") {
|
|
|
|
Expr Inner = readParenExpr();
|
|
|
|
return [=] {
|
|
|
|
ExprValue I = Inner();
|
|
|
|
I.ForceAbsolute = true;
|
|
|
|
return I;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (Tok == "ADDR") {
|
|
|
|
StringRef Name = readParenLiteral();
|
2017-10-11 08:06:27 +08:00
|
|
|
OutputSection *Sec = Script->getOrCreateOutputSection(Name);
|
2017-06-07 16:54:43 +08:00
|
|
|
return [=]() -> ExprValue {
|
2017-10-11 08:06:27 +08:00
|
|
|
checkIfExists(Sec, Location);
|
|
|
|
return {Sec, false, 0, Location};
|
2017-06-07 16:54:43 +08:00
|
|
|
};
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
if (Tok == "ALIGN") {
|
|
|
|
expect("(");
|
|
|
|
Expr E = readExpr();
|
2017-10-25 22:50:51 +08:00
|
|
|
if (consume(")")) {
|
|
|
|
E = checkAlignment(E, Location);
|
|
|
|
return [=] { return alignTo(Script->getDot(), E().getValue()); };
|
|
|
|
}
|
2017-04-05 13:40:21 +08:00
|
|
|
expect(",");
|
2017-10-25 22:50:51 +08:00
|
|
|
Expr E2 = checkAlignment(readExpr(), Location);
|
2017-04-05 13:07:39 +08:00
|
|
|
expect(")");
|
2017-05-30 11:18:28 +08:00
|
|
|
return [=] {
|
|
|
|
ExprValue V = E();
|
2017-10-25 22:50:51 +08:00
|
|
|
V.Alignment = E2().getValue();
|
2017-05-30 11:18:28 +08:00
|
|
|
return V;
|
|
|
|
};
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
if (Tok == "ALIGNOF") {
|
|
|
|
StringRef Name = readParenLiteral();
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
|
2017-10-08 10:27:02 +08:00
|
|
|
return [=] {
|
|
|
|
checkIfExists(Cmd, Location);
|
|
|
|
return Cmd->Alignment;
|
|
|
|
};
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
if (Tok == "ASSERT")
|
2018-04-25 19:16:31 +08:00
|
|
|
return readAssert();
|
2017-08-04 00:05:08 +08:00
|
|
|
if (Tok == "CONSTANT")
|
|
|
|
return readConstant();
|
2017-04-05 13:07:39 +08:00
|
|
|
if (Tok == "DATA_SEGMENT_ALIGN") {
|
|
|
|
expect("(");
|
|
|
|
Expr E = readExpr();
|
|
|
|
expect(",");
|
|
|
|
readExpr();
|
|
|
|
expect(")");
|
2017-07-28 17:27:49 +08:00
|
|
|
return [=] {
|
|
|
|
return alignTo(Script->getDot(), std::max((uint64_t)1, E().getValue()));
|
|
|
|
};
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
if (Tok == "DATA_SEGMENT_END") {
|
|
|
|
expect("(");
|
|
|
|
expect(".");
|
|
|
|
expect(")");
|
|
|
|
return [] { return Script->getDot(); };
|
|
|
|
}
|
|
|
|
if (Tok == "DATA_SEGMENT_RELRO_END") {
|
|
|
|
// 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.
|
|
|
|
expect("(");
|
|
|
|
readExpr();
|
|
|
|
expect(",");
|
|
|
|
readExpr();
|
|
|
|
expect(")");
|
2017-08-04 00:05:08 +08:00
|
|
|
Expr E = getPageSize();
|
|
|
|
return [=] { return alignTo(Script->getDot(), E().getValue()); };
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
if (Tok == "DEFINED") {
|
|
|
|
StringRef Name = readParenLiteral();
|
2017-10-08 10:06:03 +08:00
|
|
|
return [=] { return Symtab->find(Name) ? 1 : 0; };
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
2017-05-10 02:24:38 +08:00
|
|
|
if (Tok == "LENGTH") {
|
|
|
|
StringRef Name = readParenLiteral();
|
2018-03-01 20:36:01 +08:00
|
|
|
if (Script->MemoryRegions.count(Name) == 0) {
|
2017-05-10 02:24:38 +08:00
|
|
|
setError("memory region not defined: " + Name);
|
2018-03-01 20:36:01 +08:00
|
|
|
return [] { return 0; };
|
|
|
|
}
|
2017-10-11 09:19:33 +08:00
|
|
|
return [=] { return Script->MemoryRegions[Name]->Length; };
|
2017-05-10 02:24:38 +08:00
|
|
|
}
|
2017-04-05 13:07:39 +08:00
|
|
|
if (Tok == "LOADADDR") {
|
|
|
|
StringRef Name = readParenLiteral();
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
|
2017-10-08 10:27:02 +08:00
|
|
|
return [=] {
|
|
|
|
checkIfExists(Cmd, Location);
|
|
|
|
return Cmd->getLMA();
|
|
|
|
};
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
2018-03-28 19:33:00 +08:00
|
|
|
if (Tok == "MAX" || Tok == "MIN") {
|
|
|
|
expect("(");
|
|
|
|
Expr A = readExpr();
|
|
|
|
expect(",");
|
|
|
|
Expr B = readExpr();
|
|
|
|
expect(")");
|
|
|
|
if (Tok == "MIN")
|
|
|
|
return [=] { return std::min(A().getValue(), B().getValue()); };
|
|
|
|
return [=] { return std::max(A().getValue(), B().getValue()); };
|
|
|
|
}
|
2017-05-10 02:24:38 +08:00
|
|
|
if (Tok == "ORIGIN") {
|
|
|
|
StringRef Name = readParenLiteral();
|
2018-03-01 20:36:01 +08:00
|
|
|
if (Script->MemoryRegions.count(Name) == 0) {
|
2017-05-10 02:24:38 +08:00
|
|
|
setError("memory region not defined: " + Name);
|
2018-03-01 20:36:01 +08:00
|
|
|
return [] { return 0; };
|
|
|
|
}
|
2017-10-11 09:19:33 +08:00
|
|
|
return [=] { return Script->MemoryRegions[Name]->Origin; };
|
2017-05-10 02:24:38 +08:00
|
|
|
}
|
2017-04-05 13:07:39 +08:00
|
|
|
if (Tok == "SEGMENT_START") {
|
|
|
|
expect("(");
|
|
|
|
skip();
|
|
|
|
expect(",");
|
|
|
|
Expr E = readExpr();
|
|
|
|
expect(")");
|
|
|
|
return [=] { return E(); };
|
|
|
|
}
|
|
|
|
if (Tok == "SIZEOF") {
|
|
|
|
StringRef Name = readParenLiteral();
|
2017-07-28 03:22:43 +08:00
|
|
|
OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
|
2017-06-01 09:16:50 +08:00
|
|
|
// Linker script does not create an output section if its content is empty.
|
|
|
|
// We want to allow SIZEOF(.foo) where .foo is a section which happened to
|
|
|
|
// be empty.
|
2017-07-28 03:22:43 +08:00
|
|
|
return [=] { return Cmd->Size; };
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
if (Tok == "SIZEOF_HEADERS")
|
|
|
|
return [=] { return elf::getHeaderSize(); };
|
|
|
|
|
2017-04-06 02:02:30 +08:00
|
|
|
// Tok is the dot.
|
|
|
|
if (Tok == ".")
|
2017-10-11 12:34:34 +08:00
|
|
|
return [=] { return Script->getSymbolValue(Tok, Location); };
|
2017-04-06 02:02:30 +08:00
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
// Tok is a literal number.
|
2017-04-06 07:22:11 +08:00
|
|
|
if (Optional<uint64_t> Val = parseInt(Tok))
|
|
|
|
return [=] { return *Val; };
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
// Tok is a symbol name.
|
2017-04-06 02:02:30 +08:00
|
|
|
if (!isValidCIdentifier(Tok))
|
|
|
|
setError("malformed number: " + Tok);
|
2017-10-11 09:19:33 +08:00
|
|
|
Script->ReferencedSymbols.push_back(Tok);
|
2017-10-11 12:34:34 +08:00
|
|
|
return [=] { return Script->getSymbolValue(Tok, Location); };
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Expr ScriptParser::readTernary(Expr Cond) {
|
|
|
|
Expr L = readExpr();
|
|
|
|
expect(":");
|
|
|
|
Expr R = readExpr();
|
|
|
|
return [=] { return Cond().getValue() ? L() : R(); };
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr ScriptParser::readParenExpr() {
|
|
|
|
expect("(");
|
|
|
|
Expr E = readExpr();
|
|
|
|
expect(")");
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
|
|
|
|
std::vector<StringRef> Phdrs;
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && peek().startswith(":")) {
|
2017-04-05 13:07:39 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1));
|
|
|
|
}
|
|
|
|
return Phdrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a program header type name. The next token must be a
|
|
|
|
// name of a program header type or a constant (e.g. "0x3").
|
|
|
|
unsigned ScriptParser::readPhdrType() {
|
|
|
|
StringRef Tok = next();
|
2017-04-06 07:22:11 +08:00
|
|
|
if (Optional<uint64_t> Val = parseInt(Tok))
|
|
|
|
return *Val;
|
2017-04-05 13:07:39 +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)
|
|
|
|
.Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
|
|
|
|
.Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
|
|
|
|
.Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
|
|
|
|
.Default(-1);
|
|
|
|
|
|
|
|
if (Ret == (unsigned)-1) {
|
|
|
|
setError("invalid program header type: " + Tok);
|
|
|
|
return PT_NULL;
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads an anonymous version declaration.
|
|
|
|
void ScriptParser::readAnonymousDeclaration() {
|
|
|
|
std::vector<SymbolVersion> Locals;
|
|
|
|
std::vector<SymbolVersion> Globals;
|
|
|
|
std::tie(Locals, Globals) = readSymbols();
|
|
|
|
|
|
|
|
for (SymbolVersion V : Locals) {
|
|
|
|
if (V.Name == "*")
|
|
|
|
Config->DefaultSymbolVersion = VER_NDX_LOCAL;
|
|
|
|
else
|
|
|
|
Config->VersionScriptLocals.push_back(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (SymbolVersion V : Globals)
|
|
|
|
Config->VersionScriptGlobals.push_back(V);
|
|
|
|
|
|
|
|
expect(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads a non-anonymous version definition,
|
|
|
|
// e.g. "VerStr { global: foo; bar; local: *; };".
|
|
|
|
void ScriptParser::readVersionDeclaration(StringRef VerStr) {
|
|
|
|
// Read a symbol list.
|
|
|
|
std::vector<SymbolVersion> Locals;
|
|
|
|
std::vector<SymbolVersion> Globals;
|
|
|
|
std::tie(Locals, Globals) = readSymbols();
|
|
|
|
|
|
|
|
for (SymbolVersion V : Locals) {
|
|
|
|
if (V.Name == "*")
|
|
|
|
Config->DefaultSymbolVersion = VER_NDX_LOCAL;
|
|
|
|
else
|
|
|
|
Config->VersionScriptLocals.push_back(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new version definition and add that to the global symbols.
|
|
|
|
VersionDefinition Ver;
|
|
|
|
Ver.Name = VerStr;
|
|
|
|
Ver.Globals = Globals;
|
|
|
|
|
|
|
|
// User-defined version number starts from 2 because 0 and 1 are
|
|
|
|
// reserved for VER_NDX_LOCAL and VER_NDX_GLOBAL, respectively.
|
|
|
|
Ver.Id = Config->VersionDefinitions.size() + 2;
|
|
|
|
Config->VersionDefinitions.push_back(Ver);
|
|
|
|
|
|
|
|
// Each version may have a parent version. For example, "Ver2"
|
|
|
|
// defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
|
|
|
|
// as a parent. This version hierarchy is, probably against your
|
|
|
|
// instinct, purely for hint; the runtime doesn't care about it
|
|
|
|
// at all. In LLD, we simply ignore it.
|
|
|
|
if (peek() != ";")
|
|
|
|
skip();
|
|
|
|
expect(";");
|
|
|
|
}
|
|
|
|
|
2017-07-14 04:30:35 +08:00
|
|
|
static bool hasWildcard(StringRef S) {
|
|
|
|
return S.find_first_of("?*[") != StringRef::npos;
|
|
|
|
}
|
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
// Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
|
|
|
|
std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
|
|
|
|
ScriptParser::readSymbols() {
|
|
|
|
std::vector<SymbolVersion> Locals;
|
|
|
|
std::vector<SymbolVersion> Globals;
|
|
|
|
std::vector<SymbolVersion> *V = &Globals;
|
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount()) {
|
2017-04-05 13:07:39 +08:00
|
|
|
if (consume("}"))
|
|
|
|
break;
|
|
|
|
if (consumeLabel("local")) {
|
|
|
|
V = &Locals;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (consumeLabel("global")) {
|
|
|
|
V = &Globals;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (consume("extern")) {
|
|
|
|
std::vector<SymbolVersion> Ext = readVersionExtern();
|
|
|
|
V->insert(V->end(), Ext.begin(), Ext.end());
|
|
|
|
} else {
|
|
|
|
StringRef Tok = next();
|
|
|
|
V->push_back({unquote(Tok), false, hasWildcard(Tok)});
|
|
|
|
}
|
|
|
|
expect(";");
|
|
|
|
}
|
|
|
|
return {Locals, Globals};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads an "extern C++" directive, e.g.,
|
|
|
|
// "extern "C++" { ns::*; "f(int, double)"; };"
|
2018-02-02 07:46:17 +08:00
|
|
|
//
|
|
|
|
// The last semicolon is optional. E.g. this is OK:
|
|
|
|
// "extern "C++" { ns::*; "f(int, double)" };"
|
2017-04-05 13:07:39 +08:00
|
|
|
std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
|
|
|
|
StringRef Tok = next();
|
|
|
|
bool IsCXX = Tok == "\"C++\"";
|
|
|
|
if (!IsCXX && Tok != "\"C\"")
|
|
|
|
setError("Unknown language");
|
|
|
|
expect("{");
|
|
|
|
|
|
|
|
std::vector<SymbolVersion> Ret;
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && peek() != "}") {
|
2017-04-05 13:07:39 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok);
|
|
|
|
Ret.push_back({unquote(Tok), IsCXX, HasWildcard});
|
2018-02-02 07:46:17 +08:00
|
|
|
if (consume("}"))
|
|
|
|
return Ret;
|
2017-04-05 13:07:39 +08:00
|
|
|
expect(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
expect("}");
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ScriptParser::readMemoryAssignment(StringRef S1, StringRef S2,
|
|
|
|
StringRef S3) {
|
2017-04-05 13:40:21 +08:00
|
|
|
if (!consume(S1) && !consume(S2) && !consume(S3)) {
|
2017-04-05 13:07:39 +08:00
|
|
|
setError("expected one of: " + S1 + ", " + S2 + ", or " + S3);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
expect("=");
|
2017-04-13 07:16:52 +08:00
|
|
|
return readExpr()().getValue();
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the MEMORY command as specified in:
|
|
|
|
// https://sourceware.org/binutils/docs/ld/MEMORY.html
|
|
|
|
//
|
|
|
|
// MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
|
|
|
|
void ScriptParser::readMemory() {
|
|
|
|
expect("{");
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
while (!errorCount() && !consume("}")) {
|
2018-10-13 01:07:32 +08:00
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok == "INCLUDE") {
|
|
|
|
readInclude();
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
uint32_t Flags = 0;
|
|
|
|
uint32_t NegFlags = 0;
|
|
|
|
if (consume("(")) {
|
|
|
|
std::tie(Flags, NegFlags) = readMemoryAttributes();
|
|
|
|
expect(")");
|
|
|
|
}
|
|
|
|
expect(":");
|
|
|
|
|
|
|
|
uint64_t Origin = readMemoryAssignment("ORIGIN", "org", "o");
|
|
|
|
expect(",");
|
|
|
|
uint64_t Length = readMemoryAssignment("LENGTH", "len", "l");
|
|
|
|
|
2017-09-08 16:23:15 +08:00
|
|
|
// Add the memory region to the region map.
|
2018-10-13 01:07:32 +08:00
|
|
|
MemoryRegion *MR = make<MemoryRegion>(Tok, Origin, Length, Flags, NegFlags);
|
|
|
|
if (!Script->MemoryRegions.insert({Tok, MR}).second)
|
|
|
|
setError("region '" + Tok + "' already defined");
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function parses the attributes used to match against section
|
|
|
|
// flags when placing output sections in a memory region. These flags
|
|
|
|
// are only used when an explicit memory region name is not used.
|
|
|
|
std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() {
|
|
|
|
uint32_t Flags = 0;
|
|
|
|
uint32_t NegFlags = 0;
|
|
|
|
bool Invert = false;
|
|
|
|
|
|
|
|
for (char C : next().lower()) {
|
|
|
|
uint32_t Flag = 0;
|
|
|
|
if (C == '!')
|
|
|
|
Invert = !Invert;
|
|
|
|
else if (C == 'w')
|
|
|
|
Flag = SHF_WRITE;
|
|
|
|
else if (C == 'x')
|
|
|
|
Flag = SHF_EXECINSTR;
|
|
|
|
else if (C == 'a')
|
|
|
|
Flag = SHF_ALLOC;
|
|
|
|
else if (C != 'r')
|
|
|
|
setError("invalid memory region attribute");
|
|
|
|
|
|
|
|
if (Invert)
|
|
|
|
NegFlags |= Flag;
|
|
|
|
else
|
|
|
|
Flags |= Flag;
|
|
|
|
}
|
|
|
|
return {Flags, NegFlags};
|
|
|
|
}
|
|
|
|
|
|
|
|
void elf::readLinkerScript(MemoryBufferRef MB) {
|
|
|
|
ScriptParser(MB).readLinkerScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
void elf::readVersionScript(MemoryBufferRef MB) {
|
|
|
|
ScriptParser(MB).readVersionScript();
|
|
|
|
}
|
|
|
|
|
|
|
|
void elf::readDynamicList(MemoryBufferRef MB) {
|
|
|
|
ScriptParser(MB).readDynamicList();
|
|
|
|
}
|
2017-11-04 10:03:58 +08:00
|
|
|
|
|
|
|
void elf::readDefsym(StringRef Name, MemoryBufferRef MB) {
|
|
|
|
ScriptParser(MB).readDefsym(Name);
|
|
|
|
}
|