blob: aae750768d1606b908b4a3aa5c041b20e8af2c59 [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
Rui Ueyama717677a2016-02-11 21:17:59 +000016#include "LinkerScript.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000017#include "Config.h"
18#include "Driver.h"
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000019#include "InputSection.h"
George Rimar652852c2016-04-16 10:10:32 +000020#include "OutputSections.h"
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +000021#include "ScriptParser.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000022#include "SymbolTable.h"
Rui Ueyama960504b2016-04-19 18:58:11 +000023#include "llvm/ADT/StringSwitch.h"
George Rimar652852c2016-04-16 10:10:32 +000024#include "llvm/Support/ELF.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000025#include "llvm/Support/FileSystem.h"
26#include "llvm/Support/MemoryBuffer.h"
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +000027#include "llvm/Support/Path.h"
Rui Ueyamaa47ee682015-10-11 01:53:04 +000028#include "llvm/Support/StringSaver.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000029
30using namespace llvm;
George Rimar652852c2016-04-16 10:10:32 +000031using namespace llvm::ELF;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000032using namespace llvm::object;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000033using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034using namespace lld::elf;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000035
Rui Ueyama07320e42016-04-20 20:13:41 +000036ScriptConfiguration *elf::ScriptConfig;
Rui Ueyama717677a2016-02-11 21:17:59 +000037
Rui Ueyama8ec77e62016-04-21 22:00:51 +000038static bool matchStr(StringRef S, StringRef T);
39
George Rimar652852c2016-04-16 10:10:32 +000040static uint64_t getInteger(StringRef S) {
41 uint64_t V;
42 if (S.getAsInteger(0, V)) {
43 error("malformed number: " + S);
44 return 0;
45 }
46 return V;
47}
48
Rui Ueyama960504b2016-04-19 18:58:11 +000049static int precedence(StringRef Op) {
50 return StringSwitch<int>(Op)
51 .Case("*", 4)
52 .Case("/", 3)
53 .Case("+", 2)
54 .Case("-", 2)
55 .Case("&", 1)
56 .Default(-1);
57}
58
59static StringRef next(ArrayRef<StringRef> &Tokens) {
60 if (Tokens.empty()) {
61 error("no next token");
62 return "";
63 }
64 StringRef Tok = Tokens.front();
65 Tokens = Tokens.slice(1);
66 return Tok;
67}
68
Rui Ueyama60118112016-04-20 20:54:13 +000069static bool expect(ArrayRef<StringRef> &Tokens, StringRef S) {
70 if (Tokens.empty()) {
71 error(S + " expected");
72 return false;
73 }
74 StringRef Tok = Tokens.front();
75 if (Tok != S) {
76 error(S + " expected, but got " + Tok);
77 return false;
78 }
79 Tokens = Tokens.slice(1);
80 return true;
81}
82
Rui Ueyama960504b2016-04-19 18:58:11 +000083// This is a part of the operator-precedence parser to evaluate
84// arithmetic expressions in SECTIONS command. This function evaluates an
85// integer literal, a parenthesized expression or the special variable ".".
Rui Ueyamac998a8c2016-04-22 00:03:13 +000086template <class ELFT>
87uint64_t LinkerScript<ELFT>::parsePrimary(ArrayRef<StringRef> &Tokens) {
Rui Ueyama960504b2016-04-19 18:58:11 +000088 StringRef Tok = next(Tokens);
89 if (Tok == ".")
90 return Dot;
91 if (Tok == "(") {
Rui Ueyamac998a8c2016-04-22 00:03:13 +000092 uint64_t V = parseExpr(Tokens);
Rui Ueyama60118112016-04-20 20:54:13 +000093 if (!expect(Tokens, ")"))
94 return 0;
Rui Ueyama960504b2016-04-19 18:58:11 +000095 return V;
96 }
George Rimardffc1412016-04-22 11:40:53 +000097 if (Tok == "ALIGN") {
98 if (!expect(Tokens, "("))
99 return 0;
100 uint64_t V = parseExpr(Tokens);
101 if (!expect(Tokens, ")"))
102 return 0;
103 return alignTo(Dot, V);
104 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000105 return getInteger(Tok);
106}
107
George Rimarfba45c42016-04-22 11:28:54 +0000108template <class ELFT>
109uint64_t LinkerScript<ELFT>::parseTernary(ArrayRef<StringRef> &Tokens,
110 uint64_t Cond) {
111 next(Tokens);
112 uint64_t V = parseExpr(Tokens);
113 if (!expect(Tokens, ":"))
114 return 0;
115 uint64_t W = parseExpr(Tokens);
116 return Cond ? V : W;
117}
118
Rui Ueyama960504b2016-04-19 18:58:11 +0000119static uint64_t apply(StringRef Op, uint64_t L, uint64_t R) {
120 if (Op == "+")
121 return L + R;
122 if (Op == "-")
123 return L - R;
124 if (Op == "*")
125 return L * R;
126 if (Op == "/") {
127 if (R == 0) {
128 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000129 return 0;
130 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000131 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000132 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000133 if (Op == "&")
134 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000135 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000136 return 0;
137}
138
139// This is an operator-precedence parser to evaluate
140// arithmetic expressions in SECTIONS command.
Rui Ueyama99e519c2016-04-20 20:48:25 +0000141// Tokens should start with an operator.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000142template <class ELFT>
143uint64_t LinkerScript<ELFT>::parseExpr1(ArrayRef<StringRef> &Tokens,
144 uint64_t Lhs, int MinPrec) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000145 while (!Tokens.empty()) {
146 // Read an operator and an expression.
147 StringRef Op1 = Tokens.front();
George Rimarfba45c42016-04-22 11:28:54 +0000148 if (Op1 == "?")
149 return parseTernary(Tokens, Lhs);
150
Rui Ueyama960504b2016-04-19 18:58:11 +0000151 if (precedence(Op1) < MinPrec)
152 return Lhs;
153 next(Tokens);
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000154 uint64_t Rhs = parsePrimary(Tokens);
Rui Ueyama960504b2016-04-19 18:58:11 +0000155
156 // Evaluate the remaining part of the expression first if the
157 // next operator has greater precedence than the previous one.
158 // For example, if we have read "+" and "3", and if the next
159 // operator is "*", then we'll evaluate 3 * ... part first.
160 while (!Tokens.empty()) {
161 StringRef Op2 = Tokens.front();
162 if (precedence(Op2) <= precedence(Op1))
163 break;
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000164 Rhs = parseExpr1(Tokens, Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000165 }
166
167 Lhs = apply(Op1, Lhs, Rhs);
168 }
169 return Lhs;
170}
171
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000172template <class ELFT>
173uint64_t LinkerScript<ELFT>::parseExpr(ArrayRef<StringRef> &Tokens) {
174 uint64_t V = parsePrimary(Tokens);
175 return parseExpr1(Tokens, V, 0);
Rui Ueyama99e519c2016-04-20 20:48:25 +0000176}
177
Rui Ueyama960504b2016-04-19 18:58:11 +0000178// Evaluates the expression given by list of tokens.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000179template <class ELFT>
180uint64_t LinkerScript<ELFT>::evaluate(ArrayRef<StringRef> Tokens) {
181 uint64_t V = parseExpr(Tokens);
Rui Ueyama960504b2016-04-19 18:58:11 +0000182 if (!Tokens.empty())
183 error("stray token: " + Tokens[0]);
184 return V;
George Rimar652852c2016-04-16 10:10:32 +0000185}
186
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000187template <class ELFT>
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000188StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000189 for (SectionRule &R : Opt.Sections)
Rui Ueyamac9f402e2016-04-22 00:23:52 +0000190 if (matchStr(R.SectionPattern, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000191 return R.Dest;
192 return "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000193}
194
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000195template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000196bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000197 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000198}
199
Rui Ueyama07320e42016-04-20 20:13:41 +0000200template <class ELFT>
201bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000202 for (StringRef Pat : Opt.KeptSections)
203 if (matchStr(Pat, S->getSectionName()))
204 return true;
205 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000206}
207
George Rimar652852c2016-04-16 10:10:32 +0000208template <class ELFT>
Rui Ueyama7c18c282016-04-18 21:00:40 +0000209static OutputSectionBase<ELFT> *
George Rimardbbd8b12016-04-21 11:21:48 +0000210findSection(ArrayRef<OutputSectionBase<ELFT> *> V, StringRef Name) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000211 for (OutputSectionBase<ELFT> *Sec : V)
212 if (Sec->getName() == Name)
213 return Sec;
214 return nullptr;
215}
216
217template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000218void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000219 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000220 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000221 // are not explicitly placed into the output file by the linker script.
222 // We place orphan sections at end of file.
223 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000224 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000225 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000226 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000227 if (getSectionIndex(Name) == INT_MAX)
Rui Ueyama07320e42016-04-20 20:13:41 +0000228 Opt.Commands.push_back({SectionKind, {}, Name});
George Rimar652852c2016-04-16 10:10:32 +0000229 }
George Rimar652852c2016-04-16 10:10:32 +0000230
Rui Ueyama7c18c282016-04-18 21:00:40 +0000231 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000232 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000233 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000234
Rui Ueyama07320e42016-04-20 20:13:41 +0000235 for (SectionsCommand &Cmd : Opt.Commands) {
Rui Ueyama9e957a02016-04-18 21:00:45 +0000236 if (Cmd.Kind == ExprKind) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000237 Dot = evaluate(Cmd.Expr);
George Rimar652852c2016-04-16 10:10:32 +0000238 continue;
239 }
240
George Rimardbbd8b12016-04-21 11:21:48 +0000241 OutputSectionBase<ELFT> *Sec = findSection<ELFT>(Sections, Cmd.SectionName);
Rui Ueyama7c18c282016-04-18 21:00:40 +0000242 if (!Sec)
George Rimar652852c2016-04-16 10:10:32 +0000243 continue;
244
George Rimar652852c2016-04-16 10:10:32 +0000245 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000246 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama7c18c282016-04-18 21:00:40 +0000247 TVA = alignTo(TVA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000248 Sec->setVA(TVA);
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000249 ThreadBssOffset = TVA - Dot + Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000250 continue;
251 }
252
253 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000254 Dot = alignTo(Dot, Sec->getAlign());
255 Sec->setVA(Dot);
256 Dot += Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000257 continue;
258 }
259 }
260}
261
Rui Ueyama07320e42016-04-20 20:13:41 +0000262template <class ELFT>
263ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
264 auto I = Opt.Filler.find(Name);
265 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000266 return {};
267 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000268}
269
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000270// Returns the index of the given section name in linker script
271// SECTIONS commands. Sections are laid out as the same order as they
272// were in the script. If a given name did not appear in the script,
273// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar71b26e92016-04-21 10:22:02 +0000274template <class ELFT>
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000275int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000276 auto Begin = Opt.Commands.begin();
277 auto End = Opt.Commands.end();
278 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
279 return N.Kind == SectionKind && N.SectionName == Name;
280 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000281 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000282}
283
284// A compartor to sort output sections. Returns -1 or 1 if
285// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000286template <class ELFT>
287int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000288 int I = getSectionIndex(A);
289 int J = getSectionIndex(B);
290 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000291 return 0;
292 return I < J ? -1 : 1;
293}
294
George Rimarcb2aeb62016-02-24 08:49:50 +0000295// Returns true if S matches T. S can contain glob meta-characters.
296// The asterisk ('*') matches zero or more characacters, and the question
297// mark ('?') matches one character.
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000298static bool matchStr(StringRef S, StringRef T) {
299 for (;;) {
300 if (S.empty())
301 return T.empty();
302 if (S[0] == '*') {
303 S = S.substr(1);
304 if (S.empty())
305 // Fast path. If a pattern is '*', it matches anything.
306 return true;
307 for (size_t I = 0, E = T.size(); I < E; ++I)
308 if (matchStr(S, T.substr(I)))
309 return true;
310 return false;
311 }
George Rimarcb2aeb62016-02-24 08:49:50 +0000312 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000313 return false;
314 S = S.substr(1);
315 T = T.substr(1);
316 }
317}
318
Rui Ueyama07320e42016-04-20 20:13:41 +0000319class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000320 typedef void (ScriptParser::*Handler)();
321
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000322public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000323 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000324
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +0000325 void run() override;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000326
327private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000328 void addFile(StringRef Path);
329
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000330 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000331 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000332 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000333 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000334 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000335 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000336 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000337 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000338 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000339 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000340 void readSections();
341
George Rimar652852c2016-04-16 10:10:32 +0000342 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000343 void readOutputSectionDescription();
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000344 void readSectionPatterns(StringRef OutSec);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000345
George Rimarc3794e52016-02-24 09:21:47 +0000346 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000347 ScriptConfiguration &Opt = *ScriptConfig;
348 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000349 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000350};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000351
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000352const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000353 {"ENTRY", &ScriptParser::readEntry},
354 {"EXTERN", &ScriptParser::readExtern},
355 {"GROUP", &ScriptParser::readGroup},
356 {"INCLUDE", &ScriptParser::readInclude},
357 {"INPUT", &ScriptParser::readGroup},
358 {"OUTPUT", &ScriptParser::readOutput},
359 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
360 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
361 {"SEARCH_DIR", &ScriptParser::readSearchDir},
362 {"SECTIONS", &ScriptParser::readSections},
363 {";", &ScriptParser::readNothing}};
364
Rui Ueyama717677a2016-02-11 21:17:59 +0000365void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000366 while (!atEOF()) {
367 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000368 if (Handler Fn = Cmd.lookup(Tok))
369 (this->*Fn)();
370 else
George Rimar57610422016-03-11 14:43:02 +0000371 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000372 }
373}
374
Rui Ueyama717677a2016-02-11 21:17:59 +0000375void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000376 if (IsUnderSysroot && S.startswith("/")) {
377 SmallString<128> Path;
378 (Config->Sysroot + S).toStringRef(Path);
379 if (sys::fs::exists(Path)) {
380 Driver->addFile(Saver.save(Path.str()));
381 return;
382 }
383 }
384
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000385 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000386 Driver->addFile(S);
387 } else if (S.startswith("=")) {
388 if (Config->Sysroot.empty())
389 Driver->addFile(S.substr(1));
390 else
391 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
392 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000393 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000394 } else if (sys::fs::exists(S)) {
395 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000396 } else {
397 std::string Path = findFromSearchPaths(S);
398 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000399 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000400 else
401 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000402 }
403}
404
Rui Ueyama717677a2016-02-11 21:17:59 +0000405void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000406 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000407 bool Orig = Config->AsNeeded;
408 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000409 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000410 StringRef Tok = next();
411 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000412 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000413 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000414 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000415 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000416}
417
Rui Ueyama717677a2016-02-11 21:17:59 +0000418void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000419 // -e <symbol> takes predecence over ENTRY(<symbol>).
420 expect("(");
421 StringRef Tok = next();
422 if (Config->Entry.empty())
423 Config->Entry = Tok;
424 expect(")");
425}
426
Rui Ueyama717677a2016-02-11 21:17:59 +0000427void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000428 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000429 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000430 StringRef Tok = next();
431 if (Tok == ")")
432 return;
433 Config->Undefined.push_back(Tok);
434 }
435}
436
Rui Ueyama717677a2016-02-11 21:17:59 +0000437void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000438 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000439 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000440 StringRef Tok = next();
441 if (Tok == ")")
442 return;
443 if (Tok == "AS_NEEDED") {
444 readAsNeeded();
445 continue;
446 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000447 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000448 }
449}
450
Rui Ueyama717677a2016-02-11 21:17:59 +0000451void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000452 StringRef Tok = next();
453 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000454 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000455 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000456 return;
457 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000458 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000459 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
460 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000461 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000462}
463
Rui Ueyama717677a2016-02-11 21:17:59 +0000464void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000465 // -o <file> takes predecence over OUTPUT(<file>).
466 expect("(");
467 StringRef Tok = next();
468 if (Config->OutputFile.empty())
469 Config->OutputFile = Tok;
470 expect(")");
471}
472
Rui Ueyama717677a2016-02-11 21:17:59 +0000473void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000474 // Error checking only for now.
475 expect("(");
476 next();
477 expect(")");
478}
479
Rui Ueyama717677a2016-02-11 21:17:59 +0000480void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000481 // Error checking only for now.
482 expect("(");
483 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000484 StringRef Tok = next();
485 if (Tok == ")")
486 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000487 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000488 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000489 return;
490 }
Davide Italiano6836c612015-10-12 21:08:41 +0000491 next();
492 expect(",");
493 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000494 expect(")");
495}
496
Rui Ueyama717677a2016-02-11 21:17:59 +0000497void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000498 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000499 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000500 expect(")");
501}
502
Rui Ueyama717677a2016-02-11 21:17:59 +0000503void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000504 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000505 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000506 while (!Error && !skip("}")) {
507 StringRef Tok = peek();
508 if (Tok == ".")
509 readLocationCounterValue();
510 else
511 readOutputSectionDescription();
512 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000513}
514
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000515void ScriptParser::readSectionPatterns(StringRef OutSec) {
George Rimar481c2ce2016-02-23 07:47:54 +0000516 expect("(");
517 while (!Error && !skip(")"))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000518 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000519}
520
George Rimar652852c2016-04-16 10:10:32 +0000521void ScriptParser::readLocationCounterValue() {
522 expect(".");
523 expect("=");
Rui Ueyama07320e42016-04-20 20:13:41 +0000524 Opt.Commands.push_back({ExprKind, {}, ""});
525 SectionsCommand &Cmd = Opt.Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000526 while (!Error) {
527 StringRef Tok = next();
528 if (Tok == ";")
529 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000530 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000531 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000532 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000533 error("error in location counter expression");
534}
535
Rui Ueyama717677a2016-02-11 21:17:59 +0000536void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000537 StringRef OutSec = next();
Rui Ueyama07320e42016-04-20 20:13:41 +0000538 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000539 expect(":");
540 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000541
Rui Ueyama025d59b2016-02-02 20:27:59 +0000542 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000543 StringRef Tok = next();
544 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000545 expect("(");
546 while (!Error && !skip(")"))
547 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000548 } else if (Tok == "KEEP") {
549 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000550 expect("*");
551 expect("(");
552 while (!Error && !skip(")")) {
553 StringRef Sec = next();
554 Opt.Sections.emplace_back(OutSec, Sec);
555 Opt.KeptSections.push_back(Sec);
556 }
George Rimar481c2ce2016-02-23 07:47:54 +0000557 expect(")");
558 } else {
George Rimar777f9632016-03-12 08:31:34 +0000559 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000560 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000561 }
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000562
George Rimare2ee72b2016-02-26 14:48:31 +0000563 StringRef Tok = peek();
564 if (Tok.startswith("=")) {
565 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000566 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000567 return;
568 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000569 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000570 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000571 next();
572 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000573}
574
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000575static bool isUnderSysroot(StringRef Path) {
576 if (Config->Sysroot == "")
577 return false;
578 for (; !Path.empty(); Path = sys::path::parent_path(Path))
579 if (sys::fs::equivalent(Config->Sysroot, Path))
580 return true;
581 return false;
582}
583
Rui Ueyama07320e42016-04-20 20:13:41 +0000584// Entry point.
585void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000586 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000587 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000588}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000589
Rui Ueyama07320e42016-04-20 20:13:41 +0000590template class elf::LinkerScript<ELF32LE>;
591template class elf::LinkerScript<ELF32BE>;
592template class elf::LinkerScript<ELF64LE>;
593template class elf::LinkerScript<ELF64BE>;