blob: 1939424eb8b66d82f1b52051412937004b1c7d27 [file] [log] [blame]
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001//===- LinkerScript.cpp ---------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the parser/evaluator of the linker script.
11// It does not construct an AST but consume linker script directives directly.
12// Results are written to Symtab or Config object.
13//
14//===----------------------------------------------------------------------===//
15
16#include "Config.h"
17#include "Driver.h"
18#include "SymbolTable.h"
19#include "llvm/Support/FileSystem.h"
20#include "llvm/Support/MemoryBuffer.h"
Rui Ueyamaa47ee682015-10-11 01:53:04 +000021#include "llvm/Support/StringSaver.h"
Rui Ueyama63d88802015-10-12 23:49:04 +000022#include <cctype>
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000023
24using namespace llvm;
25using namespace lld;
26using namespace lld::elf2;
27
28namespace {
29class LinkerScript {
30public:
Rui Ueyamaa47ee682015-10-11 01:53:04 +000031 LinkerScript(BumpPtrAllocator *A, StringRef S)
32 : Saver(*A), Tokens(tokenize(S)) {}
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000033 void run();
34
35private:
36 static std::vector<StringRef> tokenize(StringRef S);
37 static StringRef skipSpace(StringRef S);
38 StringRef next();
39 bool atEOF() { return Tokens.size() == Pos; }
40 void expect(StringRef Expect);
41
Rui Ueyama52a15092015-10-11 03:28:42 +000042 void addFile(StringRef Path);
43
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000044 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +000045 void readEntry();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000046 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +000047 void readInclude();
Rui Ueyamaee592822015-10-07 00:25:09 +000048 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +000049 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000050 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +000051 void readSearchDir();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000052
Rui Ueyamaa47ee682015-10-11 01:53:04 +000053 StringSaver Saver;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000054 std::vector<StringRef> Tokens;
55 size_t Pos = 0;
56};
57}
58
59void LinkerScript::run() {
60 while (!atEOF()) {
61 StringRef Tok = next();
Denis Protivensky90c50992015-10-08 06:48:38 +000062 if (Tok == "ENTRY") {
63 readEntry();
Rui Ueyama00f97272015-10-11 01:31:57 +000064 } else if (Tok == "GROUP" || Tok == "INPUT") {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000065 readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +000066 } else if (Tok == "INCLUDE") {
67 readInclude();
Rui Ueyamaee592822015-10-07 00:25:09 +000068 } else if (Tok == "OUTPUT") {
69 readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +000070 } else if (Tok == "OUTPUT_ARCH") {
71 readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000072 } else if (Tok == "OUTPUT_FORMAT") {
73 readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +000074 } else if (Tok == "SEARCH_DIR") {
75 readSearchDir();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000076 } else {
77 error("unknown directive: " + Tok);
78 }
79 }
80}
81
82// Split S into linker script tokens.
83std::vector<StringRef> LinkerScript::tokenize(StringRef S) {
84 std::vector<StringRef> Ret;
85 for (;;) {
86 S = skipSpace(S);
87 if (S.empty())
88 return Ret;
89
90 // Quoted token
91 if (S.startswith("\"")) {
92 size_t E = S.find("\"", 1);
93 if (E == StringRef::npos)
94 error("unclosed quote");
95 Ret.push_back(S.substr(1, E));
96 S = S.substr(E + 1);
97 continue;
98 }
99
100 // Unquoted token
101 size_t Pos = S.find_first_not_of(
102 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
103 "0123456789_.$/\\~=+[]*?-:");
104 // A character that cannot start a word (which is usually a
105 // punctuation) forms a single character token.
106 if (Pos == 0)
107 Pos = 1;
108 Ret.push_back(S.substr(0, Pos));
109 S = S.substr(Pos);
110 }
111}
112
113// Skip leading whitespace characters or /**/-style comments.
114StringRef LinkerScript::skipSpace(StringRef S) {
115 for (;;) {
116 if (S.startswith("/*")) {
117 size_t E = S.find("*/", 2);
118 if (E == StringRef::npos)
119 error("unclosed comment in a linker script");
120 S = S.substr(E + 2);
121 continue;
122 }
123 size_t Size = S.size();
124 S = S.ltrim();
125 if (S.size() == Size)
126 return S;
127 }
128}
129
130StringRef LinkerScript::next() {
131 if (Pos == Tokens.size())
132 error("unexpected EOF");
133 return Tokens[Pos++];
134}
135
136void LinkerScript::expect(StringRef Expect) {
137 StringRef Tok = next();
138 if (Tok != Expect)
139 error(Expect + " expected, but got " + Tok);
140}
141
Rui Ueyama63d88802015-10-12 23:49:04 +0000142static bool isDOSAbsolutePath(StringRef S) {
143 return isalpha(S[0]) && S.substr(1).startswith(":\\");
144}
145
Rui Ueyama52a15092015-10-11 03:28:42 +0000146void LinkerScript::addFile(StringRef S) {
Rui Ueyama63d88802015-10-12 23:49:04 +0000147 if (S.startswith("/") || isDOSAbsolutePath(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000148 Driver->addFile(S);
149 } else if (S.startswith("=")) {
150 if (Config->Sysroot.empty())
151 Driver->addFile(S.substr(1));
152 else
153 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
154 } else if (S.startswith("-l")) {
155 Driver->addFile(searchLibrary(S.substr(2)));
156 } else {
157 std::string Path = findFromSearchPaths(S);
158 if (Path.empty())
159 error("Unable to find " + S);
160 Driver->addFile(Saver.save(Path));
161 }
162}
163
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000164void LinkerScript::readAsNeeded() {
165 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000166 bool Orig = Config->AsNeeded;
167 Config->AsNeeded = true;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000168 for (;;) {
169 StringRef Tok = next();
170 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000171 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000172 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000173 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000174 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000175}
176
Denis Protivensky90c50992015-10-08 06:48:38 +0000177void LinkerScript::readEntry() {
178 // -e <symbol> takes predecence over ENTRY(<symbol>).
179 expect("(");
180 StringRef Tok = next();
181 if (Config->Entry.empty())
182 Config->Entry = Tok;
183 expect(")");
184}
185
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000186void LinkerScript::readGroup() {
187 expect("(");
188 for (;;) {
189 StringRef Tok = next();
190 if (Tok == ")")
191 return;
192 if (Tok == "AS_NEEDED") {
193 readAsNeeded();
194 continue;
195 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000196 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000197 }
198}
199
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000200void LinkerScript::readInclude() {
201 StringRef Tok = next();
202 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000203 error(MBOrErr, "cannot open " + Tok);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000204 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000205 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
206 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000207 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000208}
209
Rui Ueyamaee592822015-10-07 00:25:09 +0000210void LinkerScript::readOutput() {
211 // -o <file> takes predecence over OUTPUT(<file>).
212 expect("(");
213 StringRef Tok = next();
214 if (Config->OutputFile.empty())
215 Config->OutputFile = Tok;
216 expect(")");
217}
218
Davide Italiano9159ce92015-10-12 21:50:08 +0000219void LinkerScript::readOutputArch() {
220 // Error checking only for now.
221 expect("(");
222 next();
223 expect(")");
224}
225
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000226void LinkerScript::readOutputFormat() {
227 // Error checking only for now.
228 expect("(");
229 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000230 StringRef Tok = next();
231 if (Tok == ")")
232 return;
233 if (Tok != ",")
234 error("unexpected token: " + Tok);
235 next();
236 expect(",");
237 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000238 expect(")");
239}
240
Davide Italiano68a39a62015-10-08 17:51:41 +0000241void LinkerScript::readSearchDir() {
242 expect("(");
Rui Ueyama52a15092015-10-11 03:28:42 +0000243 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000244 expect(")");
245}
246
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000247// Entry point. The other functions or classes are private to this file.
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000248void lld::elf2::readLinkerScript(BumpPtrAllocator *A, MemoryBufferRef MB) {
249 LinkerScript(A, MB.getBuffer()).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000250}