1 //===- ScriptLexer.cpp ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines a lexer for the linker script.
11 // The linker script's grammar is not complex but ambiguous due to the
12 // lack of the formal specification of the language. What we are trying to
13 // do in this and other files in LLD is to make a "reasonable" linker
16 // Among simplicity, compatibility and efficiency, we put the most
17 // emphasis on simplicity when we wrote this lexer. Compatibility with the
18 // GNU linkers is important, but we did not try to clone every tiny corner
19 // case of their lexers, as even ld.bfd and ld.gold are subtly different
20 // in various corner cases. We do not care much about efficiency because
21 // the time spent in parsing linker scripts is usually negligible.
23 // Overall, this lexer works fine for most linker scripts. There might
24 // be room for improving compatibility, but that's probably not at the
25 // top of our todo list.
27 //===----------------------------------------------------------------------===//
29 #include "ScriptLexer.h"
31 #include "lld/Common/ErrorHandler.h"
32 #include "llvm/ADT/Twine.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/Path.h"
40 using namespace lld::elf
;
42 ScriptLexer::Buffer::Buffer(Ctx
&ctx
, MemoryBufferRef mb
)
43 : s(mb
.getBuffer()), filename(mb
.getBufferIdentifier()),
44 begin(mb
.getBufferStart()) {
45 if (ctx
.arg
.sysroot
== "")
47 StringRef path
= filename
;
48 for (; !path
.empty(); path
= sys::path::parent_path(path
)) {
49 if (!sys::fs::equivalent(ctx
.arg
.sysroot
, path
))
51 isUnderSysroot
= true;
56 ScriptLexer::ScriptLexer(Ctx
&ctx
, MemoryBufferRef mb
)
57 : ctx(ctx
), curBuf(ctx
, mb
), mbs(1, mb
) {
58 activeFilenames
.insert(mb
.getBufferIdentifier());
61 // Returns a whole line containing the current token.
62 StringRef
ScriptLexer::getLine() {
63 StringRef s
= getCurrentMB().getBuffer();
65 size_t pos
= s
.rfind('\n', prevTok
.data() - s
.data());
66 if (pos
!= StringRef::npos
)
67 s
= s
.substr(pos
+ 1);
68 return s
.substr(0, s
.find_first_of("\r\n"));
71 // Returns 0-based column number of the current token.
72 size_t ScriptLexer::getColumnNumber() {
73 return prevTok
.data() - getLine().data();
76 std::string
ScriptLexer::getCurrentLocation() {
77 std::string filename
= std::string(getCurrentMB().getBufferIdentifier());
78 return (filename
+ ":" + Twine(prevTokLine
)).str();
81 // We don't want to record cascading errors. Keep only the first one.
82 void ScriptLexer::setError(const Twine
&msg
) {
86 std::string s
= (getCurrentLocation() + ": " + msg
).str();
88 s
+= "\n>>> " + getLine().str() + "\n>>> " +
89 std::string(getColumnNumber(), ' ') + "^";
93 void ScriptLexer::lex() {
95 StringRef
&s
= curBuf
.s
;
98 // If this buffer is from an INCLUDE command, switch to the "return
99 // value"; otherwise, mark EOF.
100 if (buffers
.empty()) {
104 activeFilenames
.erase(curBuf
.filename
);
105 curBuf
= buffers
.pop_back_val();
108 curTokState
= inExpr
;
110 // Quoted token. Note that double-quote characters are parts of a token
111 // because, in a glob match context, only unquoted tokens are interpreted
112 // as glob patterns. Double-quoted tokens are literal patterns in that
114 if (s
.starts_with("\"")) {
115 size_t e
= s
.find("\"", 1);
116 if (e
== StringRef::npos
) {
118 StringRef(curBuf
.begin
, s
.data() - curBuf
.begin
).count('\n');
119 ErrAlways(ctx
) << curBuf
.filename
<< ":" << (lineno
+ 1)
120 << ": unclosed quote";
124 curTok
= s
.take_front(e
+ 1);
129 // Some operators form separate tokens.
130 if (s
.starts_with("<<=") || s
.starts_with(">>=")) {
131 curTok
= s
.substr(0, 3);
135 if (s
.size() > 1 && (s
[1] == '=' && strchr("+-*/!&^|", s
[0]))) {
136 curTok
= s
.substr(0, 2);
141 // Unquoted token. The non-expression token is more relaxed than tokens in
142 // C-like languages, so that you can write "file-name.cpp" as one bare
146 pos
= s
.find_first_not_of(
147 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
149 if (pos
== 0 && s
.size() >= 2 &&
150 ((s
[0] == s
[1] && strchr("<>&|", s
[0])) ||
151 is_contained({"==", "!=", "<=", ">=", "<<", ">>"}, s
.substr(0, 2))))
154 pos
= s
.find_first_not_of(
155 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
156 "0123456789_.$/\\~=+[]*?-!^:");
161 curTok
= s
.substr(0, pos
);
167 // Skip leading whitespace characters or comments.
168 StringRef
ScriptLexer::skipSpace(StringRef s
) {
170 if (s
.starts_with("/*")) {
171 size_t e
= s
.find("*/", 2);
172 if (e
== StringRef::npos
) {
173 setError("unclosed comment in a linker script");
176 curBuf
.lineNumber
+= s
.substr(0, e
).count('\n');
180 if (s
.starts_with("#")) {
181 size_t e
= s
.find('\n', 1);
182 if (e
== StringRef::npos
)
191 auto len
= saved
.size() - s
.size();
194 curBuf
.lineNumber
+= saved
.substr(0, len
).count('\n');
198 // Used to determine whether to stop parsing. Treat errors like EOF.
199 bool ScriptLexer::atEOF() { return eof
|| errCount(ctx
); }
201 StringRef
ScriptLexer::next() {
203 // `prevTokLine` is not updated for EOF so that the line number in `setError`
204 // will be more useful.
206 prevTokLine
= curBuf
.lineNumber
;
207 return std::exchange(curTok
, StringRef(curBuf
.s
.data(), 0));
210 StringRef
ScriptLexer::peek() {
211 // curTok is invalid if curTokState and inExpr mismatch.
212 if (curTok
.size() && curTokState
!= inExpr
) {
213 curBuf
.s
= StringRef(curTok
.data(), curBuf
.s
.end() - curTok
.data());
221 bool ScriptLexer::consume(StringRef tok
) {
228 void ScriptLexer::skip() { (void)next(); }
230 void ScriptLexer::expect(StringRef expect
) {
233 StringRef tok
= next();
236 setError("unexpected EOF");
238 setError(expect
+ " expected, but got " + tok
);
242 ScriptLexer::Token
ScriptLexer::till(StringRef tok
) {
243 StringRef str
= next();
249 setError("unexpected EOF");
253 // Returns true if S encloses T.
254 static bool encloses(StringRef s
, StringRef t
) {
255 return s
.bytes_begin() <= t
.bytes_begin() && t
.bytes_end() <= s
.bytes_end();
258 MemoryBufferRef
ScriptLexer::getCurrentMB() {
259 // Find input buffer containing the current token.
260 assert(!mbs
.empty());
261 for (MemoryBufferRef mb
: mbs
)
262 if (encloses(mb
.getBuffer(), curBuf
.s
))
264 llvm_unreachable("getCurrentMB: failed to find a token");