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"
|
2020-07-27 16:49:24 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2017-04-05 13:07:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2020-01-15 17:38:00 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2017-04-05 13:07:39 +08:00
|
|
|
#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;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2017-04-05 13:07:39 +08:00
|
|
|
|
2017-04-05 13:08:01 +08:00
|
|
|
namespace {
|
|
|
|
class ScriptParser final : ScriptLexer {
|
2017-04-05 13:07:39 +08:00
|
|
|
public:
|
2019-07-03 14:11:50 +08:00
|
|
|
ScriptParser(MemoryBufferRef mb) : ScriptLexer(mb) {
|
|
|
|
// Initialize IsUnderSysroot
|
|
|
|
if (config->sysroot == "")
|
|
|
|
return;
|
|
|
|
StringRef path = mb.getBufferIdentifier();
|
|
|
|
for (; !path.empty(); path = sys::path::parent_path(path)) {
|
|
|
|
if (!sys::fs::equivalent(config->sysroot, path))
|
|
|
|
continue;
|
|
|
|
isUnderSysroot = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
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();
|
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();
|
2020-01-15 17:38:00 +08:00
|
|
|
std::pair<uint64_t, uint64_t> readInputSectionFlags();
|
2017-04-05 13:07:39 +08:00
|
|
|
InputSectionDescription *readInputSectionDescription(StringRef tok);
|
|
|
|
StringMatcher readFilePatterns();
|
|
|
|
std::vector<SectionPattern> readInputSectionsList();
|
2020-01-15 17:38:00 +08:00
|
|
|
InputSectionDescription *readInputSectionRules(StringRef filePattern,
|
|
|
|
uint64_t withFlags,
|
|
|
|
uint64_t withoutFlags);
|
2017-04-05 13:07:39 +08:00
|
|
|
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
|
|
|
|
2020-03-07 03:49:58 +08:00
|
|
|
Expr readMemoryAssignment(StringRef, StringRef, StringRef);
|
2017-04-05 13:07:39 +08:00
|
|
|
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.
|
2019-07-03 14:11:50 +08:00
|
|
|
bool isUnderSysroot = false;
|
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
|
|
|
// 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() {
|
|
|
|
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)) {
|
2019-07-16 12:46:31 +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("/")) {
|
2020-04-09 12:45:21 +08:00
|
|
|
// Case 1: s is an absolute path. Just open it.
|
2019-07-16 12:46:31 +08:00
|
|
|
driver->addFile(s, /*withLOption=*/false);
|
2017-04-05 13:07:39 +08:00
|
|
|
} else if (s.startswith("=")) {
|
2020-04-09 12:45:21 +08:00
|
|
|
// Case 2: relative to the sysroot.
|
2017-04-05 13:07:39 +08:00
|
|
|
if (config->sysroot.empty())
|
2019-07-16 12:46:31 +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)),
|
2019-07-16 12:46:31 +08:00
|
|
|
/*withLOption=*/false);
|
2017-04-05 13:07:39 +08:00
|
|
|
} else if (s.startswith("-l")) {
|
2020-04-09 12:45:21 +08:00
|
|
|
// Case 3: search in the list of library paths.
|
2017-04-05 13:07:39 +08:00
|
|
|
driver->addLibrary(s.substr(2));
|
|
|
|
} else {
|
2020-04-09 12:45:21 +08:00
|
|
|
// Case 4: s is a relative path. Search in the directory of the script file.
|
|
|
|
std::string filename = std::string(getCurrentMB().getBufferIdentifier());
|
|
|
|
StringRef directory = sys::path::parent_path(filename);
|
|
|
|
if (!directory.empty()) {
|
|
|
|
SmallString<0> path(directory);
|
|
|
|
sys::path::append(path, s);
|
|
|
|
if (sys::fs::exists(path)) {
|
|
|
|
driver->addFile(path, /*withLOption=*/false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Then search in the current working directory.
|
|
|
|
if (sys::fs::exists(s)) {
|
|
|
|
driver->addFile(s, /*withLOption=*/false);
|
|
|
|
} else {
|
|
|
|
// Finally, search in the list of library paths.
|
|
|
|
if (Optional<std::string> path = findFromSearchPaths(s))
|
|
|
|
driver->addFile(saver.save(*path), /*withLOption=*/true);
|
|
|
|
else
|
|
|
|
setError("unable to find " + s);
|
|
|
|
}
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(")"))
|
2019-02-12 06:01:32 +08:00
|
|
|
config->undefined.push_back(unquote(next()));
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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})
|
Recognize FreeBSD specific BFD names in OUTPUT_FORMAT
Summary:
After rLLD344952 ("Add OUTPUT_FORMAT linker script directive support"),
using BFD names such as `elf64-x86-64-freebsd` the `OUTPUT_FORMAT`
linker script command does not work anymore, resulting in errors like:
```
ld: error: /home/dim/src/clang800-import/stand/efi/loader/arch/amd64/ldscript.amd64:2: unknown output format name: elf64-x86-64-freebsd
>>> OUTPUT_FORMAT("elf64-x86-64-freebsd", "elf64-x86-64-freebsd", "elf64-x86-64-freebsd")
>>> ^
```
To fix this, recognize a `-freebsd` suffix in BFD names, and also set
`Configuration::OSABI` to `ELFOSABI_FREEBSD` for those cases.
Add and/or update several test cases to check for the correct results of
these new `OUTPUT_FORMAT` arguments.
Reviewers: ruiu, atanasyan, grimar, hokein, emaste, espindola
Reviewed By: ruiu
Subscribers: nemanjai, javed.absar, arichardson, krytarowski, kristof.beyls, kbarton, llvm-commits
Differential Revision: https://reviews.llvm.org/D57283
llvm-svn: 352606
2019-01-30 14:31:52 +08:00
|
|
|
.Case("elf64-aarch64", {ELF64LEKind, EM_AARCH64})
|
2019-01-29 03:11:52 +08:00
|
|
|
.Case("elf64-littleaarch64", {ELF64LEKind, EM_AARCH64})
|
2019-02-14 02:51:15 +08:00
|
|
|
.Case("elf32-powerpc", {ELF32BEKind, EM_PPC})
|
2019-01-29 03:11:52 +08:00
|
|
|
.Case("elf64-powerpc", {ELF64BEKind, EM_PPC64})
|
|
|
|
.Case("elf64-powerpcle", {ELF64LEKind, EM_PPC64})
|
|
|
|
.Case("elf64-x86-64", {ELF64LEKind, EM_X86_64})
|
2019-02-14 02:51:15 +08:00
|
|
|
.Cases("elf32-tradbigmips", "elf32-bigmips", {ELF32BEKind, EM_MIPS})
|
2019-01-29 03:11:52 +08:00
|
|
|
.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})
|
2019-06-10 16:09:55 +08:00
|
|
|
.Case("elf32-littleriscv", {ELF32LEKind, EM_RISCV})
|
|
|
|
.Case("elf64-littleriscv", {ELF64LEKind, EM_RISCV})
|
2020-04-17 22:58:15 +08:00
|
|
|
.Case("elf64-sparc", {ELF64BEKind, EM_SPARCV9})
|
2019-01-29 03:11:52 +08:00
|
|
|
.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
|
|
|
|
[ELF] Correct error message when OUTPUT_FORMAT is used
Any OUTPUT_FORMAT in a linker script overrides the emulation passed on
the command line, so record the passed bfdname and use that in the error
message about incompatible input files.
This prevents confusing error messages. For example, if you explicitly
pass `-m elf_x86_64` to LLD but accidentally include a linker script
which sets `OUTPUT_FORMAT(elf32-i386)`, LLD would previously complain
about your input files being compatible with elf_x86_64, which isn't the
actual issue, and is confusing because the input files are in fact
x86-64 ELF files.
Interestingly enough, this also prevents a segfault! When we don't pass
`-m` and we have an object file which is incompatible with the
`OUTPUT_FORMAT` set by a linker script, the object file is checked for
compatibility before it's added to the objectFiles vector.
config->emulation, objectFiles, and sharedFiles will all be empty, so
we'll attempt to access bitcodeFiles[0], but bitcodeFiles is also empty,
so we'll segfault. This commit prevents the segfault by adding
OUTPUT_FORMAT as a possible source of machine configuration, and it also
adds an llvm_unreachable to diagnose similar issues in the future.
Differential Revision: https://reviews.llvm.org/D76109
2020-03-13 06:25:36 +08:00
|
|
|
config->bfdname = unquote(next());
|
|
|
|
StringRef s = config->bfdname;
|
Recognize FreeBSD specific BFD names in OUTPUT_FORMAT
Summary:
After rLLD344952 ("Add OUTPUT_FORMAT linker script directive support"),
using BFD names such as `elf64-x86-64-freebsd` the `OUTPUT_FORMAT`
linker script command does not work anymore, resulting in errors like:
```
ld: error: /home/dim/src/clang800-import/stand/efi/loader/arch/amd64/ldscript.amd64:2: unknown output format name: elf64-x86-64-freebsd
>>> OUTPUT_FORMAT("elf64-x86-64-freebsd", "elf64-x86-64-freebsd", "elf64-x86-64-freebsd")
>>> ^
```
To fix this, recognize a `-freebsd` suffix in BFD names, and also set
`Configuration::OSABI` to `ELFOSABI_FREEBSD` for those cases.
Add and/or update several test cases to check for the correct results of
these new `OUTPUT_FORMAT` arguments.
Reviewers: ruiu, atanasyan, grimar, hokein, emaste, espindola
Reviewed By: ruiu
Subscribers: nemanjai, javed.absar, arichardson, krytarowski, kristof.beyls, kbarton, llvm-commits
Differential Revision: https://reviews.llvm.org/D57283
llvm-svn: 352606
2019-01-30 14:31:52 +08:00
|
|
|
if (s.consume_back("-freebsd"))
|
|
|
|
config->osabi = ELFOSABI_FREEBSD;
|
2019-01-29 03:11:52 +08:00
|
|
|
|
|
|
|
std::tie(config->ekind, config->emachine) = parseBfdName(s);
|
|
|
|
if (config->emachine == EM_NONE)
|
[ELF] Correct error message when OUTPUT_FORMAT is used
Any OUTPUT_FORMAT in a linker script overrides the emulation passed on
the command line, so record the passed bfdname and use that in the error
message about incompatible input files.
This prevents confusing error messages. For example, if you explicitly
pass `-m elf_x86_64` to LLD but accidentally include a linker script
which sets `OUTPUT_FORMAT(elf32-i386)`, LLD would previously complain
about your input files being compatible with elf_x86_64, which isn't the
actual issue, and is confusing because the input files are in fact
x86-64 ELF files.
Interestingly enough, this also prevents a segfault! When we don't pass
`-m` and we have an object file which is incompatible with the
`OUTPUT_FORMAT` set by a linker script, the object file is checked for
compatibility before it's added to the objectFiles vector.
config->emulation, objectFiles, and sharedFiles will all be empty, so
we'll attempt to access bitcodeFiles[0], but bitcodeFiles is also empty,
so we'll segfault. This commit prevents the segfault by adding
OUTPUT_FORMAT as a possible source of machine configuration, and it also
adds an llvm_unreachable to diagnose similar issues in the future.
Differential Revision: https://reviews.llvm.org/D76109
2020-03-13 06:25:36 +08:00
|
|
|
setError("unknown output format name: " + config->bfdname);
|
2019-01-29 03:11:52 +08:00
|
|
|
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() {
|
|
|
|
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
|
|
|
}
|
2020-02-11 07:58:29 +08:00
|
|
|
script->sectionCommands.insert(script->sectionCommands.end(), v.begin(),
|
|
|
|
v.end());
|
2018-03-08 22:54:38 +08:00
|
|
|
|
2020-02-11 07:58:29 +08:00
|
|
|
if (atEOF() || !consume("INSERT")) {
|
|
|
|
script->hasSectionsCommand = true;
|
2018-03-08 22:54:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-11 07:58:29 +08:00
|
|
|
bool isAfter = false;
|
|
|
|
if (consume("AFTER"))
|
|
|
|
isAfter = true;
|
|
|
|
else if (!consume("BEFORE"))
|
|
|
|
setError("expected AFTER/BEFORE, but got '" + next() + "'");
|
|
|
|
StringRef where = next();
|
|
|
|
for (BaseCommand *cmd : v)
|
|
|
|
if (auto *os = dyn_cast<OutputSection>(cmd))
|
|
|
|
script->insertCommands.push_back({os, isAfter, where});
|
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() {
|
[lld/ELF] PR44498: Support input filename in double quote
Summary:
Linker scripts allow filenames to be put in double quotes to prevent
characters in filenames that are part of the linker script syntax from
having their special meaning. Case in point the * wildcard character.
Availability of double quoting filenames also allows to fix a failure in
ELF/linkerscript/filename-spec.s when the path contain a @ which the
lexer consider as a special characters and thus break up a filename
containing it. This may happens under Jenkins which createspath such as
pipeline@2.
To avoid the need for escaping GlobPattern metacharacters in filename
in double quotes, GlobPattern::create is augmented with a new parameter
to request literal matching instead of relying on the presence of a
wildcard character in the pattern.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: peter.smith, grimar, ruiu, emaste, arichardson, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72517
2020-01-11 00:56:07 +08:00
|
|
|
StringMatcher Matcher;
|
|
|
|
|
[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(")"))
|
[lld/ELF] PR44498: Support input filename in double quote
Summary:
Linker scripts allow filenames to be put in double quotes to prevent
characters in filenames that are part of the linker script syntax from
having their special meaning. Case in point the * wildcard character.
Availability of double quoting filenames also allows to fix a failure in
ELF/linkerscript/filename-spec.s when the path contain a @ which the
lexer consider as a special characters and thus break up a filename
containing it. This may happens under Jenkins which createspath such as
pipeline@2.
To avoid the need for escaping GlobPattern metacharacters in filename
in double quotes, GlobPattern::create is augmented with a new parameter
to request literal matching instead of relying on the presence of a
wildcard character in the pattern.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: peter.smith, grimar, ruiu, emaste, arichardson, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72517
2020-01-11 00:56:07 +08:00
|
|
|
Matcher.addPattern(SingleStringMatcher(next()));
|
|
|
|
return Matcher;
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
[lld/ELF] PR44498: Support input filename in double quote
Summary:
Linker scripts allow filenames to be put in double quotes to prevent
characters in filenames that are part of the linker script syntax from
having their special meaning. Case in point the * wildcard character.
Availability of double quoting filenames also allows to fix a failure in
ELF/linkerscript/filename-spec.s when the path contain a @ which the
lexer consider as a special characters and thus break up a filename
containing it. This may happens under Jenkins which createspath such as
pipeline@2.
To avoid the need for escaping GlobPattern metacharacters in filename
in double quotes, GlobPattern::create is augmented with a new parameter
to request literal matching instead of relying on the presence of a
wildcard character in the pattern.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: peter.smith, grimar, ruiu, emaste, arichardson, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72517
2020-01-11 00:56:07 +08:00
|
|
|
StringMatcher SectionMatcher;
|
[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")
|
[lld/ELF] PR44498: Support input filename in double quote
Summary:
Linker scripts allow filenames to be put in double quotes to prevent
characters in filenames that are part of the linker script syntax from
having their special meaning. Case in point the * wildcard character.
Availability of double quoting filenames also allows to fix a failure in
ELF/linkerscript/filename-spec.s when the path contain a @ which the
lexer consider as a special characters and thus break up a filename
containing it. This may happens under Jenkins which createspath such as
pipeline@2.
To avoid the need for escaping GlobPattern metacharacters in filename
in double quotes, GlobPattern::create is augmented with a new parameter
to request literal matching instead of relying on the presence of a
wildcard character in the pattern.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: peter.smith, grimar, ruiu, emaste, arichardson, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72517
2020-01-11 00:56:07 +08:00
|
|
|
SectionMatcher.addPattern(unquote(next()));
|
2017-04-05 13:07:39 +08:00
|
|
|
|
[lld/ELF] PR44498: Support input filename in double quote
Summary:
Linker scripts allow filenames to be put in double quotes to prevent
characters in filenames that are part of the linker script syntax from
having their special meaning. Case in point the * wildcard character.
Availability of double quoting filenames also allows to fix a failure in
ELF/linkerscript/filename-spec.s when the path contain a @ which the
lexer consider as a special characters and thus break up a filename
containing it. This may happens under Jenkins which createspath such as
pipeline@2.
To avoid the need for escaping GlobPattern metacharacters in filename
in double quotes, GlobPattern::create is augmented with a new parameter
to request literal matching instead of relying on the presence of a
wildcard character in the pattern.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: peter.smith, grimar, ruiu, emaste, arichardson, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72517
2020-01-11 00:56:07 +08:00
|
|
|
if (!SectionMatcher.empty())
|
|
|
|
ret.push_back({std::move(excludeFilePat), std::move(SectionMatcher)});
|
2017-04-05 13:07:39 +08:00
|
|
|
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 *
|
2020-01-15 17:38:00 +08:00
|
|
|
ScriptParser::readInputSectionRules(StringRef filePattern, uint64_t withFlags,
|
|
|
|
uint64_t withoutFlags) {
|
|
|
|
auto *cmd =
|
|
|
|
make<InputSectionDescription>(filePattern, withFlags, withoutFlags);
|
2017-04-05 13:07:39 +08:00
|
|
|
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
|
2020-01-15 17:38:00 +08:00
|
|
|
uint64_t withFlags = 0;
|
|
|
|
uint64_t withoutFlags = 0;
|
2017-04-05 13:07:39 +08:00
|
|
|
if (tok == "KEEP") {
|
|
|
|
expect("(");
|
2020-01-15 17:38:00 +08:00
|
|
|
if (consume("INPUT_SECTION_FLAGS"))
|
|
|
|
std::tie(withFlags, withoutFlags) = readInputSectionFlags();
|
|
|
|
InputSectionDescription *cmd =
|
|
|
|
readInputSectionRules(next(), withFlags, withoutFlags);
|
2017-04-05 13:07:39 +08:00
|
|
|
expect(")");
|
2017-10-11 09:19:33 +08:00
|
|
|
script->keptSections.push_back(cmd);
|
2017-04-05 13:07:39 +08:00
|
|
|
return cmd;
|
|
|
|
}
|
2020-01-15 17:38:00 +08:00
|
|
|
if (tok == "INPUT_SECTION_FLAGS") {
|
|
|
|
std::tie(withFlags, withoutFlags) = readInputSectionFlags();
|
|
|
|
tok = next();
|
|
|
|
}
|
|
|
|
return readInputSectionRules(tok, withFlags, withoutFlags);
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2019-09-07 00:30:22 +08:00
|
|
|
errorOrWarn(msg);
|
2017-04-05 13:07:39 +08:00
|
|
|
return script->getDot();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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;
|
2020-03-29 00:54:06 +08:00
|
|
|
cmd->type = SHT_NOBITS;
|
2018-08-28 16:39:21 +08:00
|
|
|
} 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("{");
|
2020-01-15 17:38:00 +08:00
|
|
|
while (!errorCount() && !consume("}")) {
|
|
|
|
uint64_t withFlags = 0;
|
|
|
|
uint64_t withoutFlags = 0;
|
|
|
|
if (consume("INPUT_SECTION_FLAGS"))
|
|
|
|
std::tie(withFlags, withoutFlags) = readInputSectionFlags();
|
|
|
|
cmd->sectionCommands.push_back(
|
|
|
|
readInputSectionRules(next(), withFlags, withoutFlags));
|
|
|
|
}
|
2018-06-27 16:08:12 +08:00
|
|
|
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") {
|
2019-07-04 22:17:31 +08:00
|
|
|
// 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
|
2020-02-16 19:17:26 +08:00
|
|
|
if (peek() != "(")
|
|
|
|
setError("( expected, but got " + peek());
|
2017-04-05 13:07:39 +08:00
|
|
|
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.
|
2020-01-15 17:38:00 +08:00
|
|
|
// FIXME: GNU ld permits INPUT_SECTION_FLAGS to be used here. We do not
|
|
|
|
// handle this case here as it will already have been matched by the
|
|
|
|
// case above.
|
2018-12-06 18:56:11 +08:00
|
|
|
auto *isd = make<InputSectionDescription>(tok);
|
[lld/ELF] PR44498: Support input filename in double quote
Summary:
Linker scripts allow filenames to be put in double quotes to prevent
characters in filenames that are part of the linker script syntax from
having their special meaning. Case in point the * wildcard character.
Availability of double quoting filenames also allows to fix a failure in
ELF/linkerscript/filename-spec.s when the path contain a @ which the
lexer consider as a special characters and thus break up a filename
containing it. This may happens under Jenkins which createspath such as
pipeline@2.
To avoid the need for escaping GlobPattern metacharacters in filename
in double quotes, GlobPattern::create is augmented with a new parameter
to request literal matching instead of relying on the presence of a
wildcard character in the pattern.
Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap
Reviewed By: MaskRay
Subscribers: peter.smith, grimar, ruiu, emaste, arichardson, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D72517
2020-01-11 00:56:07 +08:00
|
|
|
isd->sectionPatterns.push_back({{}, StringMatcher("*")});
|
2018-12-06 16:34:52 +08:00
|
|
|
cmd->sectionCommands.push_back(isd);
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (consume(">"))
|
2020-01-29 03:23:46 +08:00
|
|
|
cmd->memoryRegionName = std::string(next());
|
2017-04-05 13:07:39 +08:00
|
|
|
|
2018-01-12 17:07:35 +08:00
|
|
|
if (consume("AT")) {
|
|
|
|
expect(">");
|
2020-01-29 03:23:46 +08:00
|
|
|
cmd->lmaRegionName = std::string(next());
|
2018-01-12 17:07:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2019-07-04 22:17:31 +08:00
|
|
|
if (peek() == "=" || peek().startswith("=")) {
|
|
|
|
inExpr = true;
|
|
|
|
consume("=");
|
|
|
|
cmd->filler = readFill();
|
|
|
|
inExpr = false;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-04 22:17:31 +08:00
|
|
|
// Reads a `=<fillexp>` expression and returns its value as a big-endian number.
|
2017-04-05 13:07:39 +08:00
|
|
|
// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
|
2019-07-04 22:17:31 +08:00
|
|
|
// We do not support using symbols in such expressions.
|
2017-04-05 13:07:39 +08:00
|
|
|
//
|
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.
|
2020-02-16 19:17:26 +08:00
|
|
|
// Also, we require that expressions with operators must be wrapped into
|
|
|
|
// round brackets. We did it to resolve the ambiguity when parsing scripts like:
|
|
|
|
// SECTIONS { .foo : { ... } =120+3 /DISCARD/ : { ... } }
|
2019-07-04 22:17:31 +08:00
|
|
|
std::array<uint8_t, 4> ScriptParser::readFill() {
|
2020-02-16 19:17:26 +08:00
|
|
|
uint64_t value = readPrimary()().val;
|
2019-07-04 22:17:31 +08:00
|
|
|
if (value > UINT32_MAX)
|
|
|
|
setError("filler expression result does not fit 32-bit: 0x" +
|
|
|
|
Twine::utohexstr(value));
|
2017-04-12 06:45:57 +08:00
|
|
|
|
2018-11-15 05:05:20 +08:00
|
|
|
std::array<uint8_t, 4> buf;
|
2019-07-04 22:17:31 +08:00
|
|
|
write32be(buf.data(), (uint32_t)value);
|
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);
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
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;
|
|
|
|
}
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
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)
|
2019-05-14 00:01:26 +08:00
|
|
|
return config->commonPageSize;
|
2017-08-04 00:05:08 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-15 17:38:00 +08:00
|
|
|
static llvm::Optional<uint64_t> parseFlag(StringRef tok) {
|
|
|
|
if (llvm::Optional<uint64_t> asInt = parseInt(tok))
|
|
|
|
return asInt;
|
|
|
|
#define CASE_ENT(enum) #enum, ELF::enum
|
|
|
|
return StringSwitch<llvm::Optional<uint64_t>>(tok)
|
|
|
|
.Case(CASE_ENT(SHF_WRITE))
|
|
|
|
.Case(CASE_ENT(SHF_ALLOC))
|
|
|
|
.Case(CASE_ENT(SHF_EXECINSTR))
|
|
|
|
.Case(CASE_ENT(SHF_MERGE))
|
|
|
|
.Case(CASE_ENT(SHF_STRINGS))
|
|
|
|
.Case(CASE_ENT(SHF_INFO_LINK))
|
|
|
|
.Case(CASE_ENT(SHF_LINK_ORDER))
|
|
|
|
.Case(CASE_ENT(SHF_OS_NONCONFORMING))
|
|
|
|
.Case(CASE_ENT(SHF_GROUP))
|
|
|
|
.Case(CASE_ENT(SHF_TLS))
|
|
|
|
.Case(CASE_ENT(SHF_COMPRESSED))
|
|
|
|
.Case(CASE_ENT(SHF_EXCLUDE))
|
|
|
|
.Case(CASE_ENT(SHF_ARM_PURECODE))
|
|
|
|
.Default(None);
|
|
|
|
#undef CASE_ENT
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads the '(' <flags> ')' list of section flags in
|
|
|
|
// INPUT_SECTION_FLAGS '(' <flags> ')' in the
|
|
|
|
// following form:
|
|
|
|
// <flags> ::= <flag>
|
|
|
|
// | <flags> & flag
|
|
|
|
// <flag> ::= Recognized Flag Name, or Integer value of flag.
|
|
|
|
// If the first character of <flag> is a ! then this means without flag,
|
|
|
|
// otherwise with flag.
|
|
|
|
// Example: SHF_EXECINSTR & !SHF_WRITE means with flag SHF_EXECINSTR and
|
|
|
|
// without flag SHF_WRITE.
|
|
|
|
std::pair<uint64_t, uint64_t> ScriptParser::readInputSectionFlags() {
|
|
|
|
uint64_t withFlags = 0;
|
|
|
|
uint64_t withoutFlags = 0;
|
|
|
|
expect("(");
|
|
|
|
while (!errorCount()) {
|
|
|
|
StringRef tok = unquote(next());
|
|
|
|
bool without = tok.consume_front("!");
|
|
|
|
if (llvm::Optional<uint64_t> flag = parseFlag(tok)) {
|
|
|
|
if (without)
|
|
|
|
withoutFlags |= *flag;
|
|
|
|
else
|
|
|
|
withFlags |= *flag;
|
|
|
|
} else {
|
|
|
|
setError("unrecognised flag: " + tok);
|
|
|
|
}
|
|
|
|
if (consume(")"))
|
|
|
|
break;
|
|
|
|
if (!consume("&")) {
|
|
|
|
next();
|
|
|
|
setError("expected & or )");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_pair(withFlags, withoutFlags);
|
|
|
|
}
|
|
|
|
|
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);
|
2019-04-26 14:59:30 +08:00
|
|
|
sec->usedInExpression = true;
|
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();
|
2020-07-17 04:40:31 +08:00
|
|
|
return [=] {
|
|
|
|
Symbol *b = symtab->find(name);
|
|
|
|
return (b && b->isDefined()) ? 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; };
|
|
|
|
}
|
2020-03-07 03:49:58 +08:00
|
|
|
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);
|
2019-04-26 14:59:30 +08:00
|
|
|
cmd->usedInExpression = true;
|
2017-10-08 10:27:02 +08:00
|
|
|
return [=] {
|
|
|
|
checkIfExists(cmd, location);
|
|
|
|
return cmd->getLMA();
|
|
|
|
};
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
2020-07-27 16:49:24 +08:00
|
|
|
if (tok == "LOG2CEIL") {
|
|
|
|
expect("(");
|
|
|
|
Expr a = readExpr();
|
|
|
|
expect(")");
|
|
|
|
return [=] {
|
|
|
|
// LOG2CEIL(0) is defined to be 0.
|
|
|
|
return llvm::Log2_64_Ceil(std::max(a().getValue(), UINT64_C(1)));
|
|
|
|
};
|
|
|
|
}
|
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; };
|
|
|
|
}
|
2020-03-07 03:49:58 +08:00
|
|
|
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")
|
2020-05-15 13:18:58 +08:00
|
|
|
return [=] { return elf::getHeaderSize(); };
|
2017-04-05 13:07:39 +08:00
|
|
|
|
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();
|
2019-08-05 22:31:39 +08:00
|
|
|
for (const SymbolVersion &pat : locals)
|
|
|
|
config->versionDefinitions[VER_NDX_LOCAL].patterns.push_back(pat);
|
|
|
|
for (const SymbolVersion &pat : globals)
|
|
|
|
config->versionDefinitions[VER_NDX_GLOBAL].patterns.push_back(pat);
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
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();
|
2019-08-05 22:31:39 +08:00
|
|
|
for (const SymbolVersion &pat : locals)
|
|
|
|
config->versionDefinitions[VER_NDX_LOCAL].patterns.push_back(pat);
|
2017-04-05 13:07:39 +08:00
|
|
|
|
|
|
|
// Create a new version definition and add that to the global symbols.
|
|
|
|
VersionDefinition ver;
|
|
|
|
ver.name = verStr;
|
2019-08-05 22:31:39 +08:00
|
|
|
ver.patterns = globals;
|
|
|
|
ver.id = config->versionDefinitions.size();
|
2017-04-05 13:07:39 +08:00
|
|
|
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.
|
2020-02-09 06:10:29 +08:00
|
|
|
if (next() != ";")
|
|
|
|
expect(";");
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 08:11:38 +08:00
|
|
|
bool elf::hasWildcard(StringRef s) {
|
2017-07-14 04:30:35 +08:00
|
|
|
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();
|
2019-07-03 14:11:50 +08:00
|
|
|
ret.push_back(
|
|
|
|
{unquote(tok), isCXX, !tok.startswith("\"") && hasWildcard(tok)});
|
2018-02-02 07:46:17 +08:00
|
|
|
if (consume("}"))
|
|
|
|
return ret;
|
2017-04-05 13:07:39 +08:00
|
|
|
expect(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
expect("}");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-07 03:49:58 +08:00
|
|
|
Expr 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);
|
2020-03-07 03:49:58 +08:00
|
|
|
return [] { return 0; };
|
2017-04-05 13:07:39 +08:00
|
|
|
}
|
|
|
|
expect("=");
|
2020-03-07 03:49:58 +08:00
|
|
|
return readExpr();
|
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(":");
|
|
|
|
|
2020-03-07 03:49:58 +08:00
|
|
|
Expr origin = readMemoryAssignment("ORIGIN", "org", "o");
|
2017-04-05 13:07:39 +08:00
|
|
|
expect(",");
|
2020-03-07 03:49:58 +08:00
|
|
|
Expr length = readMemoryAssignment("LENGTH", "len", "l");
|
2017-04-05 13:07:39 +08:00
|
|
|
|
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;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2017-04-05 13:07:39 +08:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::readLinkerScript(MemoryBufferRef mb) {
|
2017-04-05 13:07:39 +08:00
|
|
|
ScriptParser(mb).readLinkerScript();
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::readVersionScript(MemoryBufferRef mb) {
|
2017-04-05 13:07:39 +08:00
|
|
|
ScriptParser(mb).readVersionScript();
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::readDynamicList(MemoryBufferRef mb) {
|
|
|
|
ScriptParser(mb).readDynamicList();
|
|
|
|
}
|
2017-11-04 10:03:58 +08:00
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::readDefsym(StringRef name, MemoryBufferRef mb) {
|
2017-11-04 10:03:58 +08:00
|
|
|
ScriptParser(mb).readDefsym(name);
|
|
|
|
}
|