blob: fcafc66407dc3b6032f4ae8502f242f5d7fd2fa8 [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.
Rui Ueyama34f29242015-10-13 19:51:57 +000012// Results are written to Driver or Config object.
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000013//
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 Ueyamaf03f3cc2015-10-13 00:09:21 +000021#include "llvm/Support/Path.h"
Rui Ueyamaa47ee682015-10-11 01:53:04 +000022#include "llvm/Support/StringSaver.h"
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 Ueyama52a15092015-10-11 03:28:42 +0000142void LinkerScript::addFile(StringRef S) {
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000143 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000144 Driver->addFile(S);
145 } else if (S.startswith("=")) {
146 if (Config->Sysroot.empty())
147 Driver->addFile(S.substr(1));
148 else
149 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
150 } else if (S.startswith("-l")) {
151 Driver->addFile(searchLibrary(S.substr(2)));
152 } else {
153 std::string Path = findFromSearchPaths(S);
154 if (Path.empty())
155 error("Unable to find " + S);
156 Driver->addFile(Saver.save(Path));
157 }
158}
159
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000160void LinkerScript::readAsNeeded() {
161 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000162 bool Orig = Config->AsNeeded;
163 Config->AsNeeded = true;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000164 for (;;) {
165 StringRef Tok = next();
166 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000167 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000168 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000169 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000170 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000171}
172
Denis Protivensky90c50992015-10-08 06:48:38 +0000173void LinkerScript::readEntry() {
174 // -e <symbol> takes predecence over ENTRY(<symbol>).
175 expect("(");
176 StringRef Tok = next();
177 if (Config->Entry.empty())
178 Config->Entry = Tok;
179 expect(")");
180}
181
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000182void LinkerScript::readGroup() {
183 expect("(");
184 for (;;) {
185 StringRef Tok = next();
186 if (Tok == ")")
187 return;
188 if (Tok == "AS_NEEDED") {
189 readAsNeeded();
190 continue;
191 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000192 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000193 }
194}
195
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000196void LinkerScript::readInclude() {
197 StringRef Tok = next();
198 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000199 error(MBOrErr, "cannot open " + Tok);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000200 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000201 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
202 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000203 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000204}
205
Rui Ueyamaee592822015-10-07 00:25:09 +0000206void LinkerScript::readOutput() {
207 // -o <file> takes predecence over OUTPUT(<file>).
208 expect("(");
209 StringRef Tok = next();
210 if (Config->OutputFile.empty())
211 Config->OutputFile = Tok;
212 expect(")");
213}
214
Davide Italiano9159ce92015-10-12 21:50:08 +0000215void LinkerScript::readOutputArch() {
216 // Error checking only for now.
217 expect("(");
218 next();
219 expect(")");
220}
221
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000222void LinkerScript::readOutputFormat() {
223 // Error checking only for now.
224 expect("(");
225 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000226 StringRef Tok = next();
227 if (Tok == ")")
228 return;
229 if (Tok != ",")
230 error("unexpected token: " + Tok);
231 next();
232 expect(",");
233 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000234 expect(")");
235}
236
Davide Italiano68a39a62015-10-08 17:51:41 +0000237void LinkerScript::readSearchDir() {
238 expect("(");
Rui Ueyama52a15092015-10-11 03:28:42 +0000239 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000240 expect(")");
241}
242
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000243// Entry point. The other functions or classes are private to this file.
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000244void lld::elf2::readLinkerScript(BumpPtrAllocator *A, MemoryBufferRef MB) {
245 LinkerScript(A, MB.getBuffer()).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000246}