2017-02-14 12:47:05 +08:00
|
|
|
//===- ScriptLexer.cpp ----------------------------------------------------===//
|
2016-04-07 04:59:11 +08:00
|
|
|
//
|
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
|
2016-04-07 04:59:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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:".
|
|
|
|
//
|
2017-02-16 03:58:17 +08:00
|
|
|
// Overall, this lexer works fine for most linker scripts. There might
|
|
|
|
// be room for improving compatibility, but that's probably not at the
|
|
|
|
// top of our todo list.
|
2016-04-07 04:59:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
#include "ScriptLexer.h"
|
[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
|
|
|
#include "lld/Common/ErrorHandler.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;
|
|
|
|
|
2019-10-07 16:31:18 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace 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
|
|
|
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) {
|
[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
|
|
|
if (errorCount())
|
2016-04-07 04:59:11 +08:00
|
|
|
return;
|
2016-11-21 23:49:56 +08:00
|
|
|
|
2017-08-23 16:48:39 +08:00
|
|
|
std::string s = (getCurrentLocation() + ": " + msg).str();
|
|
|
|
if (pos)
|
|
|
|
s += "\n>>> " + getLine().str() + "\n>>> " +
|
|
|
|
std::string(getColumnNumber(), ' ') + "^";
|
|
|
|
error(s);
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-26 18:13:10 +08:00
|
|
|
// ">foo" is parsed to ">" and "foo", but ">>" is parsed to ">>".
|
2018-07-03 22:16:19 +08:00
|
|
|
// "|", "||", "&" and "&&" are different operators.
|
2017-12-26 18:13:10 +08:00
|
|
|
if (s.startswith("<<") || s.startswith("<=") || s.startswith(">>") ||
|
2018-07-03 22:02:52 +08:00
|
|
|
s.startswith(">=") || s.startswith("||") || s.startswith("&&")) {
|
2017-12-26 18:13:10 +08:00
|
|
|
vec.push_back(s.substr(0, 2));
|
|
|
|
s = s.substr(2);
|
|
|
|
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-12-26 18:13:10 +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.
|
[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
|
|
|
bool ScriptLexer::atEOF() { return errorCount() || tokens.size() == pos; }
|
2016-04-07 04:59:11 +08:00
|
|
|
|
2017-02-16 03:58:17 +08:00
|
|
|
// Split a given string as an expression.
|
|
|
|
// This function returns "3", "*" and "5" for "3*5" for example.
|
|
|
|
static std::vector<StringRef> tokenizeExpr(StringRef s) {
|
2019-07-04 22:17:31 +08:00
|
|
|
StringRef ops = "+-*/:!~=<>"; // List of operators
|
2017-02-16 03:58:17 +08:00
|
|
|
|
|
|
|
// Quoted strings are literal strings, so we don't want to split it.
|
|
|
|
if (s.startswith("\""))
|
|
|
|
return {s};
|
|
|
|
|
2017-10-11 16:18:53 +08:00
|
|
|
// Split S with operators as separators.
|
2017-02-16 03:58:17 +08:00
|
|
|
std::vector<StringRef> ret;
|
|
|
|
while (!s.empty()) {
|
|
|
|
size_t e = s.find_first_of(ops);
|
|
|
|
|
|
|
|
// No need to split if there is no operator.
|
|
|
|
if (e == StringRef::npos) {
|
|
|
|
ret.push_back(s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a token before the opreator.
|
|
|
|
if (e != 0)
|
|
|
|
ret.push_back(s.substr(0, e));
|
|
|
|
|
2019-07-04 22:17:31 +08:00
|
|
|
// Get the operator as a token.
|
|
|
|
// Keep !=, ==, >=, <=, << and >> operators as a single tokens.
|
|
|
|
if (s.substr(e).startswith("!=") || s.substr(e).startswith("==") ||
|
|
|
|
s.substr(e).startswith(">=") || s.substr(e).startswith("<=") ||
|
|
|
|
s.substr(e).startswith("<<") || s.substr(e).startswith(">>")) {
|
2017-08-10 23:25:47 +08:00
|
|
|
ret.push_back(s.substr(e, 2));
|
|
|
|
s = s.substr(e + 2);
|
|
|
|
} else {
|
|
|
|
ret.push_back(s.substr(e, 1));
|
|
|
|
s = s.substr(e + 1);
|
|
|
|
}
|
2017-02-16 03:58:17 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In contexts where expressions are expected, the lexer should apply
|
|
|
|
// different tokenization rules than the default one. By default,
|
|
|
|
// arithmetic operator characters are regular characters, but in the
|
|
|
|
// expression context, they should be independent tokens.
|
|
|
|
//
|
|
|
|
// For example, "foo*3" should be tokenized to "foo", "*" and "3" only
|
|
|
|
// in the expression context.
|
|
|
|
//
|
|
|
|
// This function may split the current token into multiple tokens.
|
|
|
|
void ScriptLexer::maybeSplitExpr() {
|
[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
|
|
|
if (!inExpr || errorCount() || atEOF())
|
2017-02-16 03:58:17 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<StringRef> v = tokenizeExpr(tokens[pos]);
|
|
|
|
if (v.size() == 1)
|
|
|
|
return;
|
|
|
|
tokens.erase(tokens.begin() + pos);
|
|
|
|
tokens.insert(tokens.begin() + pos, v.begin(), v.end());
|
|
|
|
}
|
|
|
|
|
2017-02-14 12:47:05 +08:00
|
|
|
StringRef ScriptLexer::next() {
|
2017-02-16 03:58:17 +08:00
|
|
|
maybeSplitExpr();
|
|
|
|
|
[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
|
|
|
if (errorCount())
|
2016-04-07 04:59:11 +08:00
|
|
|
return "";
|
|
|
|
if (atEOF()) {
|
|
|
|
setError("unexpected EOF");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return tokens[pos++];
|
|
|
|
}
|
|
|
|
|
2017-03-10 03:23:00 +08:00
|
|
|
StringRef ScriptLexer::peek() {
|
|
|
|
StringRef tok = next();
|
[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
|
|
|
if (errorCount())
|
2017-03-10 03:23:00 +08:00
|
|
|
return "";
|
|
|
|
pos = pos - 1;
|
2016-04-07 04:59:11 +08:00
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2018-08-28 16:39:21 +08:00
|
|
|
StringRef ScriptLexer::peek2() {
|
|
|
|
skip();
|
|
|
|
StringRef tok = next();
|
|
|
|
if (errorCount())
|
|
|
|
return "";
|
|
|
|
pos = pos - 2;
|
|
|
|
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-03-10 03:23:00 +08:00
|
|
|
// Consumes Tok followed by ":". Space is allowed between Tok and ":".
|
|
|
|
bool ScriptLexer::consumeLabel(StringRef tok) {
|
|
|
|
if (consume((tok + ":").str()))
|
|
|
|
return true;
|
|
|
|
if (tokens.size() >= pos + 2 && tokens[pos] == tok &&
|
|
|
|
tokens[pos + 1] == ":") {
|
|
|
|
pos += 2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
[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
|
|
|
if (errorCount())
|
2016-04-07 04:59:11 +08:00
|
|
|
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.
|
2018-07-06 21:30:50 +08:00
|
|
|
assert(!mbs.empty() && pos > 0);
|
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
|
|
|
}
|
2019-10-07 16:31:18 +08:00
|
|
|
|
|
|
|
} // namespace elf
|
|
|
|
} // namespace lld
|