2016-04-23 04:21:26 +08:00
|
|
|
//===- SymbolListFile.cpp -------------------------------------------------===//
|
2016-04-14 02:51:11 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the parser/evaluator of the linker script.
|
|
|
|
// It does not construct an AST but consume linker script directives directly.
|
|
|
|
// Results are written to Driver or Config object.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-04-23 04:21:26 +08:00
|
|
|
#include "SymbolListFile.h"
|
2016-04-14 02:51:11 +08:00
|
|
|
#include "Config.h"
|
|
|
|
#include "ScriptParser.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
|
|
|
|
|
|
|
// Parse the --dynamic-list argument. A dynamic list is in the form
|
|
|
|
//
|
|
|
|
// { symbol1; symbol2; [...]; symbolN };
|
|
|
|
//
|
|
|
|
// Multiple groups can be defined in the same file and they are merged
|
|
|
|
// in only one definition.
|
|
|
|
|
|
|
|
class DynamicListParser final : public ScriptParserBase {
|
|
|
|
public:
|
|
|
|
DynamicListParser(StringRef S) : ScriptParserBase(S) {}
|
|
|
|
|
2016-04-23 06:59:24 +08:00
|
|
|
void run();
|
2016-04-14 02:51:11 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void readGroup();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parse the default group definition using C language symbol name.
|
|
|
|
void DynamicListParser::readGroup() {
|
|
|
|
expect("{");
|
|
|
|
while (!Error) {
|
|
|
|
Config->DynamicList.push_back(next());
|
|
|
|
expect(";");
|
|
|
|
if (peek() == "}") {
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicListParser::run() {
|
|
|
|
while (!atEOF())
|
|
|
|
readGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void elf::parseDynamicList(MemoryBufferRef MB) {
|
|
|
|
DynamicListParser(MB.getBuffer()).run();
|
|
|
|
}
|
2016-04-23 04:21:26 +08:00
|
|
|
|
|
|
|
// Parse the --version-script argument. We currently only accept the following
|
|
|
|
// version script syntax:
|
|
|
|
//
|
|
|
|
// { [ global: symbol1; symbol2; [...]; symbolN; ] local: *; };
|
|
|
|
//
|
|
|
|
// No wildcards are supported, other than for the local entry. Symbol versioning
|
|
|
|
// is also not supported.
|
|
|
|
|
|
|
|
class VersionScriptParser final : public ScriptParserBase {
|
|
|
|
public:
|
|
|
|
VersionScriptParser(StringRef S) : ScriptParserBase(S) {}
|
|
|
|
|
2016-04-23 06:59:24 +08:00
|
|
|
void run();
|
2016-06-17 02:47:04 +08:00
|
|
|
|
|
|
|
private:
|
2016-06-20 19:55:12 +08:00
|
|
|
void parseVersion(StringRef Version);
|
2016-06-17 21:38:09 +08:00
|
|
|
void parseLocal();
|
2016-06-20 19:55:12 +08:00
|
|
|
void parseVersionSymbols(StringRef Version);
|
2016-04-23 04:21:26 +08:00
|
|
|
};
|
|
|
|
|
2016-06-20 19:55:12 +08:00
|
|
|
void VersionScriptParser::parseVersion(StringRef Version) {
|
2016-04-23 04:21:26 +08:00
|
|
|
expect("{");
|
2016-06-29 05:48:33 +08:00
|
|
|
Config->SymbolVersions.push_back(elf::Version(Version));
|
2016-04-23 04:21:26 +08:00
|
|
|
if (peek() == "global:") {
|
|
|
|
next();
|
2016-06-20 19:55:12 +08:00
|
|
|
parseVersionSymbols(Version);
|
2016-04-23 04:21:26 +08:00
|
|
|
}
|
2016-06-17 21:38:09 +08:00
|
|
|
if (peek() == "local:")
|
|
|
|
parseLocal();
|
2016-06-24 19:23:55 +08:00
|
|
|
else if (peek() != "}")
|
2016-06-20 19:55:12 +08:00
|
|
|
parseVersionSymbols(Version);
|
2016-06-17 21:38:09 +08:00
|
|
|
|
|
|
|
expect("}");
|
2016-07-01 19:45:10 +08:00
|
|
|
|
|
|
|
// Each version may have a parent version. For example, "Ver2" defined as
|
|
|
|
// "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This
|
|
|
|
// version hierarchy is, probably against your instinct, purely for human; the
|
|
|
|
// runtime doesn't care about them at all. In LLD, we simply skip the token.
|
2016-06-22 22:40:45 +08:00
|
|
|
if (!Version.empty() && peek() != ";")
|
2016-07-01 19:45:10 +08:00
|
|
|
next();
|
2016-06-17 21:38:09 +08:00
|
|
|
expect(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
void VersionScriptParser::parseLocal() {
|
2016-04-23 04:21:26 +08:00
|
|
|
expect("local:");
|
|
|
|
expect("*");
|
|
|
|
expect(";");
|
2016-06-17 21:38:09 +08:00
|
|
|
Config->VersionScriptGlobalByDefault = false;
|
|
|
|
}
|
|
|
|
|
2016-06-20 19:55:12 +08:00
|
|
|
void VersionScriptParser::parseVersionSymbols(StringRef Version) {
|
|
|
|
std::vector<StringRef> *Globals;
|
2016-06-29 05:48:33 +08:00
|
|
|
if (Version.empty())
|
2016-06-20 19:55:12 +08:00
|
|
|
Globals = &Config->VersionScriptGlobals;
|
2016-06-29 05:48:33 +08:00
|
|
|
else
|
2016-06-20 19:55:12 +08:00
|
|
|
Globals = &Config->SymbolVersions.back().Globals;
|
|
|
|
|
2016-06-17 21:38:09 +08:00
|
|
|
for (;;) {
|
2016-06-23 15:23:07 +08:00
|
|
|
StringRef Cur = peek();
|
2016-06-23 15:26:05 +08:00
|
|
|
if (Cur == "extern")
|
|
|
|
setError("extern keyword is not supported");
|
2016-06-23 15:23:07 +08:00
|
|
|
if (Cur == "}" || Cur == "local:" || Error)
|
2016-06-17 21:38:09 +08:00
|
|
|
return;
|
2016-06-23 15:23:07 +08:00
|
|
|
next();
|
|
|
|
Globals->push_back(Cur);
|
2016-06-17 21:38:09 +08:00
|
|
|
expect(";");
|
|
|
|
}
|
2016-06-17 02:47:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionScriptParser::run() {
|
|
|
|
StringRef Msg = "anonymous version definition is used in "
|
|
|
|
"combination with other version definitions";
|
|
|
|
if (peek() == "{") {
|
2016-06-20 19:55:12 +08:00
|
|
|
parseVersion("");
|
2016-06-17 02:47:04 +08:00
|
|
|
if (!atEOF())
|
|
|
|
setError(Msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!atEOF() && !Error) {
|
2016-06-20 19:55:12 +08:00
|
|
|
if (peek() == "{") {
|
2016-06-17 02:47:04 +08:00
|
|
|
setError(Msg);
|
|
|
|
return;
|
|
|
|
}
|
2016-06-20 19:55:12 +08:00
|
|
|
parseVersion(next());
|
2016-06-17 02:47:04 +08:00
|
|
|
}
|
2016-04-23 04:21:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void elf::parseVersionScript(MemoryBufferRef MB) {
|
|
|
|
VersionScriptParser(MB.getBuffer()).run();
|
|
|
|
}
|