2017-02-14 12:47:05 +08:00
|
|
|
//===- ScriptLexer.cpp ----------------------------------------------------===//
|
2016-04-07 04:59:11 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2017-02-14 12:47:24 +08:00
|
|
|
// This file defines a lexer for the linker script.
|
|
|
|
//
|
|
|
|
// The linker script's grammar is not complex but ambiguous due to the
|
|
|
|
// lack of the formal specification of the language. What we are trying to
|
|
|
|
// do in this and other files in LLD is to make a "reasonable" linker
|
|
|
|
// script processor.
|
|
|
|
//
|
|
|
|
// Among simplicity, compatibility and efficiency, we put the most
|
|
|
|
// emphasis on simplicity when we wrote this lexer. Compatibility with the
|
|
|
|
// GNU linkers is important, but we did not try to clone every tiny corner
|
|
|
|
// case of their lexers, as even ld.bfd and ld.gold are subtly different
|
|
|
|
// in various corner cases. We do not care much about efficiency because
|
|
|
|
// the time spent in parsing linker scripts is usually negligible.
|
|
|
|
//
|
|
|
|
// Our grammar of the linker script is LL(2), meaning that it needs at
|
|
|
|
// most two-token lookahead to parse. The only place we need two-token
|
|
|
|
// lookahead is labels in version scripts, where we need to parse "local :"
|
|
|
|
// as if "local:".
|
|
|
|
//
|
|
|
|
// Overall, this lexer works fine for most linker scripts. There's room
|
|
|
|
// for improving compatibility, but that's probably not at the top of our
|
|
|
|
// todo list.
|
|
|
|
//
|
|
|
|
// A caveat: This lexer splits an input string into tokens ahead of time,
|
|
|
|
// so the lexer is not context aware. There's one known corner case. Let's
|
|
|
|
// say the next string is "val*3" (without quotes). In the context where
|
|
|
|
// the parser is expecting an expression, that should be tokenizes to
|
|
|
|
// "val", "*" and "3". In other context, it should be just a single
|
|
|
|
// token. (If it is in a filename context, it'll be interpeted as a glob
|
|
|
|
// pattern, for example.) We want to fix this, but it probably needs a
|
|
|
|
// redesign of this lexer.
|
2016-04-07 04:59:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
#include "ScriptLexer.h"
|
2016-04-07 04:59:11 +08:00
|
|
|
#include "Error.h"
|
2016-04-16 18:58:45 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2016-04-07 04:59:11 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
|
|
|
|
2016-12-01 12:36:49 +08:00
|
|
|
// Returns a whole line containing the current token.
|
2017-02-14 12:47:05 +08:00
|
|
|
StringRef ScriptLexer::getLine() {
|
2016-12-01 12:36:54 +08:00
|
|
|
StringRef S = getCurrentMB().getBuffer();
|
2016-12-01 12:36:49 +08:00
|
|
|
StringRef Tok = Tokens[Pos - 1];
|
|
|
|
|
2016-12-01 11:56:27 +08:00
|
|
|
size_t Pos = S.rfind('\n', Tok.data() - S.data());
|
|
|
|
if (Pos != StringRef::npos)
|
|
|
|
S = S.substr(Pos + 1);
|
|
|
|
return S.substr(0, S.find_first_of("\r\n"));
|
2016-04-07 04:59:11 +08:00
|
|
|
}
|
|
|
|
|
2016-12-01 12:36:49 +08:00
|
|
|
// Returns 1-based line number of the current token.
|
2017-02-14 12:47:05 +08:00
|
|
|
size_t ScriptLexer::getLineNumber() {
|
2016-12-01 12:36:54 +08:00
|
|
|
StringRef S = getCurrentMB().getBuffer();
|
2016-12-01 12:36:49 +08:00
|
|
|
StringRef Tok = Tokens[Pos - 1];
|
2016-12-01 11:56:27 +08:00
|
|
|
return S.substr(0, Tok.data() - S.data()).count('\n') + 1;
|
|
|
|
}
|
|
|
|
|
2016-12-01 12:36:49 +08:00
|
|
|
// Returns 0-based column number of the current token.
|
2017-02-14 12:47:05 +08:00
|
|
|
size_t ScriptLexer::getColumnNumber() {
|
2016-12-01 12:36:49 +08:00
|
|
|
StringRef Tok = Tokens[Pos - 1];
|
|
|
|
return Tok.data() - getLine().data();
|
|
|
|
}
|
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
std::string ScriptLexer::getCurrentLocation() {
|
2016-12-01 12:36:54 +08:00
|
|
|
std::string Filename = getCurrentMB().getBufferIdentifier();
|
2016-12-01 12:36:49 +08:00
|
|
|
if (!Pos)
|
|
|
|
return Filename;
|
|
|
|
return (Filename + ":" + Twine(getLineNumber())).str();
|
2016-04-07 04:59:11 +08:00
|
|
|
}
|
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
ScriptLexer::ScriptLexer(MemoryBufferRef MB) { tokenize(MB); }
|
2016-11-21 23:49:56 +08:00
|
|
|
|
2016-04-07 04:59:11 +08:00
|
|
|
// We don't want to record cascading errors. Keep only the first one.
|
2017-02-14 12:47:05 +08:00
|
|
|
void ScriptLexer::setError(const Twine &Msg) {
|
2016-04-07 04:59:11 +08:00
|
|
|
if (Error)
|
|
|
|
return;
|
2016-12-01 11:56:27 +08:00
|
|
|
Error = true;
|
2016-11-21 23:49:56 +08:00
|
|
|
|
2016-12-01 11:56:27 +08:00
|
|
|
if (!Pos) {
|
2016-12-01 12:36:49 +08:00
|
|
|
error(getCurrentLocation() + ": " + Msg);
|
2016-12-01 11:56:27 +08:00
|
|
|
return;
|
2016-11-21 23:49:56 +08:00
|
|
|
}
|
|
|
|
|
2016-12-01 12:36:49 +08:00
|
|
|
std::string S = getCurrentLocation() + ": ";
|
|
|
|
error(S + Msg);
|
|
|
|
error(S + getLine());
|
|
|
|
error(S + std::string(getColumnNumber(), ' ') + "^");
|
2016-04-07 04:59:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split S into linker script tokens.
|
2017-02-14 12:47:05 +08:00
|
|
|
void ScriptLexer::tokenize(MemoryBufferRef MB) {
|
2016-12-01 12:36:51 +08:00
|
|
|
std::vector<StringRef> Vec;
|
2016-11-21 23:49:56 +08:00
|
|
|
MBs.push_back(MB);
|
|
|
|
StringRef S = MB.getBuffer();
|
|
|
|
StringRef Begin = S;
|
2016-12-01 12:36:49 +08:00
|
|
|
|
2016-04-07 04:59:11 +08:00
|
|
|
for (;;) {
|
|
|
|
S = skipSpace(S);
|
|
|
|
if (S.empty())
|
2016-11-21 23:49:56 +08:00
|
|
|
break;
|
2016-04-07 04:59:11 +08:00
|
|
|
|
2016-09-09 22:35:36 +08:00
|
|
|
// Quoted token. Note that double-quote characters are parts of a token
|
|
|
|
// because, in a glob match context, only unquoted tokens are interpreted
|
|
|
|
// as glob patterns. Double-quoted tokens are literal patterns in that
|
|
|
|
// context.
|
2016-04-07 04:59:11 +08:00
|
|
|
if (S.startswith("\"")) {
|
|
|
|
size_t E = S.find("\"", 1);
|
|
|
|
if (E == StringRef::npos) {
|
2016-12-01 12:36:49 +08:00
|
|
|
StringRef Filename = MB.getBufferIdentifier();
|
|
|
|
size_t Lineno = Begin.substr(0, S.data() - Begin.data()).count('\n');
|
2016-12-01 12:36:51 +08:00
|
|
|
error(Filename + ":" + Twine(Lineno + 1) + ": unclosed quote");
|
2016-11-21 23:49:56 +08:00
|
|
|
return;
|
2016-04-07 04:59:11 +08:00
|
|
|
}
|
2016-12-01 12:36:49 +08:00
|
|
|
|
2016-12-01 12:36:51 +08:00
|
|
|
Vec.push_back(S.take_front(E + 1));
|
2016-04-07 04:59:11 +08:00
|
|
|
S = S.substr(E + 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-02 06:48:05 +08:00
|
|
|
// Unquoted token. This is more relaxed than tokens in C-like language,
|
|
|
|
// so that you can write "file-name.cpp" as one bare token, for example.
|
2016-04-07 04:59:11 +08:00
|
|
|
size_t Pos = S.find_first_not_of(
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
2017-02-03 21:24:01 +08:00
|
|
|
"0123456789_.$/\\~=+[]*?-!<>^");
|
2016-09-02 06:48:05 +08:00
|
|
|
|
2016-04-07 04:59:11 +08:00
|
|
|
// A character that cannot start a word (which is usually a
|
|
|
|
// punctuation) forms a single character token.
|
|
|
|
if (Pos == 0)
|
|
|
|
Pos = 1;
|
2016-12-01 12:36:51 +08:00
|
|
|
Vec.push_back(S.substr(0, Pos));
|
2016-04-07 04:59:11 +08:00
|
|
|
S = S.substr(Pos);
|
|
|
|
}
|
2016-12-01 12:36:49 +08:00
|
|
|
|
2016-12-01 12:36:51 +08:00
|
|
|
Tokens.insert(Tokens.begin() + Pos, Vec.begin(), Vec.end());
|
2016-04-07 04:59:11 +08:00
|
|
|
}
|
|
|
|
|
2016-06-16 21:29:48 +08:00
|
|
|
// Skip leading whitespace characters or comments.
|
2017-02-14 12:47:05 +08:00
|
|
|
StringRef ScriptLexer::skipSpace(StringRef S) {
|
2016-04-07 04:59:11 +08:00
|
|
|
for (;;) {
|
|
|
|
if (S.startswith("/*")) {
|
|
|
|
size_t E = S.find("*/", 2);
|
|
|
|
if (E == StringRef::npos) {
|
|
|
|
error("unclosed comment in a linker script");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
S = S.substr(E + 2);
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-16 21:29:48 +08:00
|
|
|
if (S.startswith("#")) {
|
|
|
|
size_t E = S.find('\n', 1);
|
|
|
|
if (E == StringRef::npos)
|
|
|
|
E = S.size() - 1;
|
|
|
|
S = S.substr(E + 1);
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-07 04:59:11 +08:00
|
|
|
size_t Size = S.size();
|
|
|
|
S = S.ltrim();
|
|
|
|
if (S.size() == Size)
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// An erroneous token is handled as if it were the last token before EOF.
|
2017-02-14 12:47:05 +08:00
|
|
|
bool ScriptLexer::atEOF() { return Error || Tokens.size() == Pos; }
|
2016-04-07 04:59:11 +08:00
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
StringRef ScriptLexer::next() {
|
2016-04-07 04:59:11 +08:00
|
|
|
if (Error)
|
|
|
|
return "";
|
|
|
|
if (atEOF()) {
|
|
|
|
setError("unexpected EOF");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return Tokens[Pos++];
|
|
|
|
}
|
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
StringRef ScriptLexer::peek(unsigned N) {
|
2017-02-08 03:50:47 +08:00
|
|
|
StringRef Tok;
|
|
|
|
for (unsigned I = 0; I <= N; ++I) {
|
|
|
|
Tok = next();
|
|
|
|
if (Error)
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
Pos = Pos - N - 1;
|
2016-04-07 04:59:11 +08:00
|
|
|
return Tok;
|
|
|
|
}
|
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
bool ScriptLexer::consume(StringRef Tok) {
|
2016-10-21 12:52:13 +08:00
|
|
|
if (peek() == Tok) {
|
|
|
|
skip();
|
|
|
|
return true;
|
2016-04-07 04:59:11 +08:00
|
|
|
}
|
2016-10-21 12:52:13 +08:00
|
|
|
return false;
|
2016-04-07 04:59:11 +08:00
|
|
|
}
|
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
void ScriptLexer::skip() { (void)next(); }
|
2016-10-17 14:21:13 +08:00
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
void ScriptLexer::expect(StringRef Expect) {
|
2016-04-07 04:59:11 +08:00
|
|
|
if (Error)
|
|
|
|
return;
|
|
|
|
StringRef Tok = next();
|
|
|
|
if (Tok != Expect)
|
|
|
|
setError(Expect + " expected, but got " + Tok);
|
|
|
|
}
|
|
|
|
|
2016-12-01 12:36:54 +08:00
|
|
|
// Returns true if S encloses T.
|
|
|
|
static bool encloses(StringRef S, StringRef T) {
|
|
|
|
return S.bytes_begin() <= T.bytes_begin() && T.bytes_end() <= S.bytes_end();
|
2016-11-21 23:49:56 +08:00
|
|
|
}
|
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
MemoryBufferRef ScriptLexer::getCurrentMB() {
|
2016-11-21 23:49:56 +08:00
|
|
|
// Find input buffer containing the current token.
|
|
|
|
assert(!MBs.empty());
|
2016-12-01 12:36:54 +08:00
|
|
|
if (!Pos)
|
|
|
|
return MBs[0];
|
2016-11-21 23:49:56 +08:00
|
|
|
|
2016-12-01 12:36:54 +08:00
|
|
|
for (MemoryBufferRef MB : MBs)
|
|
|
|
if (encloses(MB.getBuffer(), Tokens[Pos - 1]))
|
|
|
|
return MB;
|
|
|
|
llvm_unreachable("getCurrentMB: failed to find a token");
|
2016-04-07 04:59:11 +08:00
|
|
|
}
|