blob: 5fad6e1abf24311d2ada5aa7fb9c3c1dc14dd53e [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
Rui Ueyama960504b2016-04-19 18:58:11 +000040static int precedence(StringRef Op) {
41 return StringSwitch<int>(Op)
42 .Case("*", 4)
43 .Case("/", 3)
44 .Case("+", 2)
45 .Case("-", 2)
46 .Case("&", 1)
47 .Default(-1);
48}
49
50static StringRef next(ArrayRef<StringRef> &Tokens) {
51 if (Tokens.empty()) {
52 error("no next token");
53 return "";
54 }
55 StringRef Tok = Tokens.front();
56 Tokens = Tokens.slice(1);
57 return Tok;
58}
59
Rui Ueyama60118112016-04-20 20:54:13 +000060static bool expect(ArrayRef<StringRef> &Tokens, StringRef S) {
61 if (Tokens.empty()) {
62 error(S + " expected");
63 return false;
64 }
65 StringRef Tok = Tokens.front();
66 if (Tok != S) {
67 error(S + " expected, but got " + Tok);
68 return false;
69 }
70 Tokens = Tokens.slice(1);
71 return true;
72}
73
Rui Ueyama960504b2016-04-19 18:58:11 +000074// This is a part of the operator-precedence parser to evaluate
75// arithmetic expressions in SECTIONS command. This function evaluates an
Rui Ueyamae29a9752016-04-22 21:02:27 +000076// integer literal, a parenthesized expression, the ALIGN function,
77// or the special variable ".".
Rui Ueyamac998a8c2016-04-22 00:03:13 +000078template <class ELFT>
79uint64_t LinkerScript<ELFT>::parsePrimary(ArrayRef<StringRef> &Tokens) {
Rui Ueyama960504b2016-04-19 18:58:11 +000080 StringRef Tok = next(Tokens);
81 if (Tok == ".")
82 return Dot;
83 if (Tok == "(") {
Rui Ueyamac998a8c2016-04-22 00:03:13 +000084 uint64_t V = parseExpr(Tokens);
Rui Ueyama60118112016-04-20 20:54:13 +000085 if (!expect(Tokens, ")"))
86 return 0;
Rui Ueyama960504b2016-04-19 18:58:11 +000087 return V;
88 }
George Rimardffc1412016-04-22 11:40:53 +000089 if (Tok == "ALIGN") {
90 if (!expect(Tokens, "("))
91 return 0;
92 uint64_t V = parseExpr(Tokens);
93 if (!expect(Tokens, ")"))
94 return 0;
95 return alignTo(Dot, V);
96 }
Rui Ueyama5fa60982016-04-22 21:05:04 +000097 uint64_t V = 0;
98 if (Tok.getAsInteger(0, V))
99 error("malformed number: " + Tok);
100 return V;
Rui Ueyama960504b2016-04-19 18:58:11 +0000101}
102
George Rimarfba45c42016-04-22 11:28:54 +0000103template <class ELFT>
104uint64_t LinkerScript<ELFT>::parseTernary(ArrayRef<StringRef> &Tokens,
105 uint64_t Cond) {
106 next(Tokens);
107 uint64_t V = parseExpr(Tokens);
108 if (!expect(Tokens, ":"))
109 return 0;
110 uint64_t W = parseExpr(Tokens);
111 return Cond ? V : W;
112}
113
Rui Ueyama960504b2016-04-19 18:58:11 +0000114static uint64_t apply(StringRef Op, uint64_t L, uint64_t R) {
115 if (Op == "+")
116 return L + R;
117 if (Op == "-")
118 return L - R;
119 if (Op == "*")
120 return L * R;
121 if (Op == "/") {
122 if (R == 0) {
123 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000124 return 0;
125 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000126 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000127 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000128 if (Op == "&")
129 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000130 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000131 return 0;
132}
133
134// This is an operator-precedence parser to evaluate
135// arithmetic expressions in SECTIONS command.
Rui Ueyama99e519c2016-04-20 20:48:25 +0000136// Tokens should start with an operator.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000137template <class ELFT>
138uint64_t LinkerScript<ELFT>::parseExpr1(ArrayRef<StringRef> &Tokens,
139 uint64_t Lhs, int MinPrec) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000140 while (!Tokens.empty()) {
141 // Read an operator and an expression.
142 StringRef Op1 = Tokens.front();
George Rimarfba45c42016-04-22 11:28:54 +0000143 if (Op1 == "?")
144 return parseTernary(Tokens, Lhs);
145
Rui Ueyama960504b2016-04-19 18:58:11 +0000146 if (precedence(Op1) < MinPrec)
147 return Lhs;
148 next(Tokens);
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000149 uint64_t Rhs = parsePrimary(Tokens);
Rui Ueyama960504b2016-04-19 18:58:11 +0000150
151 // Evaluate the remaining part of the expression first if the
152 // next operator has greater precedence than the previous one.
153 // For example, if we have read "+" and "3", and if the next
154 // operator is "*", then we'll evaluate 3 * ... part first.
155 while (!Tokens.empty()) {
156 StringRef Op2 = Tokens.front();
157 if (precedence(Op2) <= precedence(Op1))
158 break;
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000159 Rhs = parseExpr1(Tokens, Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000160 }
161
162 Lhs = apply(Op1, Lhs, Rhs);
163 }
164 return Lhs;
165}
166
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000167template <class ELFT>
168uint64_t LinkerScript<ELFT>::parseExpr(ArrayRef<StringRef> &Tokens) {
169 uint64_t V = parsePrimary(Tokens);
170 return parseExpr1(Tokens, V, 0);
Rui Ueyama99e519c2016-04-20 20:48:25 +0000171}
172
Rui Ueyama960504b2016-04-19 18:58:11 +0000173// Evaluates the expression given by list of tokens.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000174template <class ELFT>
175uint64_t LinkerScript<ELFT>::evaluate(ArrayRef<StringRef> Tokens) {
176 uint64_t V = parseExpr(Tokens);
Rui Ueyama960504b2016-04-19 18:58:11 +0000177 if (!Tokens.empty())
178 error("stray token: " + Tokens[0]);
179 return V;
George Rimar652852c2016-04-16 10:10:32 +0000180}
181
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000182template <class ELFT>
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000183StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000184 for (SectionRule &R : Opt.Sections)
Rui Ueyamac9f402e2016-04-22 00:23:52 +0000185 if (matchStr(R.SectionPattern, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000186 return R.Dest;
187 return "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000188}
189
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000190template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000191bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000192 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000193}
194
Rui Ueyama07320e42016-04-20 20:13:41 +0000195template <class ELFT>
196bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000197 for (StringRef Pat : Opt.KeptSections)
198 if (matchStr(Pat, S->getSectionName()))
199 return true;
200 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000201}
202
George Rimar652852c2016-04-16 10:10:32 +0000203template <class ELFT>
Rui Ueyama7c18c282016-04-18 21:00:40 +0000204static OutputSectionBase<ELFT> *
George Rimardbbd8b12016-04-21 11:21:48 +0000205findSection(ArrayRef<OutputSectionBase<ELFT> *> V, StringRef Name) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000206 for (OutputSectionBase<ELFT> *Sec : V)
207 if (Sec->getName() == Name)
208 return Sec;
209 return nullptr;
210}
211
212template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000213void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000214 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000215 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000216 // are not explicitly placed into the output file by the linker script.
217 // We place orphan sections at end of file.
218 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000219 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000220 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000221 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000222 if (getSectionIndex(Name) == INT_MAX)
Rui Ueyama07320e42016-04-20 20:13:41 +0000223 Opt.Commands.push_back({SectionKind, {}, Name});
George Rimar652852c2016-04-16 10:10:32 +0000224 }
George Rimar652852c2016-04-16 10:10:32 +0000225
Rui Ueyama7c18c282016-04-18 21:00:40 +0000226 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000227 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000228 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000229
Rui Ueyama07320e42016-04-20 20:13:41 +0000230 for (SectionsCommand &Cmd : Opt.Commands) {
Rui Ueyama9e957a02016-04-18 21:00:45 +0000231 if (Cmd.Kind == ExprKind) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000232 Dot = evaluate(Cmd.Expr);
George Rimar652852c2016-04-16 10:10:32 +0000233 continue;
234 }
235
George Rimardbbd8b12016-04-21 11:21:48 +0000236 OutputSectionBase<ELFT> *Sec = findSection<ELFT>(Sections, Cmd.SectionName);
Rui Ueyama7c18c282016-04-18 21:00:40 +0000237 if (!Sec)
George Rimar652852c2016-04-16 10:10:32 +0000238 continue;
239
George Rimar652852c2016-04-16 10:10:32 +0000240 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000241 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama7c18c282016-04-18 21:00:40 +0000242 TVA = alignTo(TVA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000243 Sec->setVA(TVA);
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000244 ThreadBssOffset = TVA - Dot + Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000245 continue;
246 }
247
248 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000249 Dot = alignTo(Dot, Sec->getAlign());
250 Sec->setVA(Dot);
251 Dot += Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000252 continue;
253 }
254 }
255}
256
Rui Ueyama07320e42016-04-20 20:13:41 +0000257template <class ELFT>
258ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
259 auto I = Opt.Filler.find(Name);
260 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000261 return {};
262 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000263}
264
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000265// Returns the index of the given section name in linker script
266// SECTIONS commands. Sections are laid out as the same order as they
267// were in the script. If a given name did not appear in the script,
268// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar71b26e92016-04-21 10:22:02 +0000269template <class ELFT>
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000270int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000271 auto Begin = Opt.Commands.begin();
272 auto End = Opt.Commands.end();
273 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
274 return N.Kind == SectionKind && N.SectionName == Name;
275 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000276 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000277}
278
279// A compartor to sort output sections. Returns -1 or 1 if
280// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000281template <class ELFT>
282int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000283 int I = getSectionIndex(A);
284 int J = getSectionIndex(B);
285 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000286 return 0;
287 return I < J ? -1 : 1;
288}
289
George Rimarcb2aeb62016-02-24 08:49:50 +0000290// Returns true if S matches T. S can contain glob meta-characters.
291// The asterisk ('*') matches zero or more characacters, and the question
292// mark ('?') matches one character.
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000293static bool matchStr(StringRef S, StringRef T) {
294 for (;;) {
295 if (S.empty())
296 return T.empty();
297 if (S[0] == '*') {
298 S = S.substr(1);
299 if (S.empty())
300 // Fast path. If a pattern is '*', it matches anything.
301 return true;
302 for (size_t I = 0, E = T.size(); I < E; ++I)
303 if (matchStr(S, T.substr(I)))
304 return true;
305 return false;
306 }
George Rimarcb2aeb62016-02-24 08:49:50 +0000307 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000308 return false;
309 S = S.substr(1);
310 T = T.substr(1);
311 }
312}
313
Rui Ueyama07320e42016-04-20 20:13:41 +0000314class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000315 typedef void (ScriptParser::*Handler)();
316
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000317public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000318 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000319
Rui Ueyama4a465392016-04-22 22:59:24 +0000320 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000321
322private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000323 void addFile(StringRef Path);
324
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000325 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000326 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000327 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000328 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000329 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000330 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000331 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000332 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000333 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000334 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000335 void readSections();
336
George Rimar652852c2016-04-16 10:10:32 +0000337 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000338 void readOutputSectionDescription();
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000339 void readSectionPatterns(StringRef OutSec);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000340
George Rimarc3794e52016-02-24 09:21:47 +0000341 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000342 ScriptConfiguration &Opt = *ScriptConfig;
343 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000344 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000345};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000346
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000347const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000348 {"ENTRY", &ScriptParser::readEntry},
349 {"EXTERN", &ScriptParser::readExtern},
350 {"GROUP", &ScriptParser::readGroup},
351 {"INCLUDE", &ScriptParser::readInclude},
352 {"INPUT", &ScriptParser::readGroup},
353 {"OUTPUT", &ScriptParser::readOutput},
354 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
355 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
356 {"SEARCH_DIR", &ScriptParser::readSearchDir},
357 {"SECTIONS", &ScriptParser::readSections},
358 {";", &ScriptParser::readNothing}};
359
Rui Ueyama717677a2016-02-11 21:17:59 +0000360void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000361 while (!atEOF()) {
362 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000363 if (Handler Fn = Cmd.lookup(Tok))
364 (this->*Fn)();
365 else
George Rimar57610422016-03-11 14:43:02 +0000366 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000367 }
368}
369
Rui Ueyama717677a2016-02-11 21:17:59 +0000370void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000371 if (IsUnderSysroot && S.startswith("/")) {
372 SmallString<128> Path;
373 (Config->Sysroot + S).toStringRef(Path);
374 if (sys::fs::exists(Path)) {
375 Driver->addFile(Saver.save(Path.str()));
376 return;
377 }
378 }
379
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000380 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000381 Driver->addFile(S);
382 } else if (S.startswith("=")) {
383 if (Config->Sysroot.empty())
384 Driver->addFile(S.substr(1));
385 else
386 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
387 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000388 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000389 } else if (sys::fs::exists(S)) {
390 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000391 } else {
392 std::string Path = findFromSearchPaths(S);
393 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000394 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000395 else
396 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000397 }
398}
399
Rui Ueyama717677a2016-02-11 21:17:59 +0000400void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000401 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000402 bool Orig = Config->AsNeeded;
403 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000404 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000405 StringRef Tok = next();
406 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000407 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000408 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000409 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000410 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000411}
412
Rui Ueyama717677a2016-02-11 21:17:59 +0000413void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000414 // -e <symbol> takes predecence over ENTRY(<symbol>).
415 expect("(");
416 StringRef Tok = next();
417 if (Config->Entry.empty())
418 Config->Entry = Tok;
419 expect(")");
420}
421
Rui Ueyama717677a2016-02-11 21:17:59 +0000422void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000423 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000424 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000425 StringRef Tok = next();
426 if (Tok == ")")
427 return;
428 Config->Undefined.push_back(Tok);
429 }
430}
431
Rui Ueyama717677a2016-02-11 21:17:59 +0000432void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000433 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000434 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000435 StringRef Tok = next();
436 if (Tok == ")")
437 return;
438 if (Tok == "AS_NEEDED") {
439 readAsNeeded();
440 continue;
441 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000442 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000443 }
444}
445
Rui Ueyama717677a2016-02-11 21:17:59 +0000446void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000447 StringRef Tok = next();
448 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000449 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000450 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000451 return;
452 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000453 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000454 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
455 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000456 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000457}
458
Rui Ueyama717677a2016-02-11 21:17:59 +0000459void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000460 // -o <file> takes predecence over OUTPUT(<file>).
461 expect("(");
462 StringRef Tok = next();
463 if (Config->OutputFile.empty())
464 Config->OutputFile = Tok;
465 expect(")");
466}
467
Rui Ueyama717677a2016-02-11 21:17:59 +0000468void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000469 // Error checking only for now.
470 expect("(");
471 next();
472 expect(")");
473}
474
Rui Ueyama717677a2016-02-11 21:17:59 +0000475void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000476 // Error checking only for now.
477 expect("(");
478 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000479 StringRef Tok = next();
480 if (Tok == ")")
481 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000482 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000483 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000484 return;
485 }
Davide Italiano6836c612015-10-12 21:08:41 +0000486 next();
487 expect(",");
488 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000489 expect(")");
490}
491
Rui Ueyama717677a2016-02-11 21:17:59 +0000492void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000493 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000494 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000495 expect(")");
496}
497
Rui Ueyama717677a2016-02-11 21:17:59 +0000498void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000499 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000500 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000501 while (!Error && !skip("}")) {
502 StringRef Tok = peek();
503 if (Tok == ".")
504 readLocationCounterValue();
505 else
506 readOutputSectionDescription();
507 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000508}
509
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000510void ScriptParser::readSectionPatterns(StringRef OutSec) {
George Rimar481c2ce2016-02-23 07:47:54 +0000511 expect("(");
512 while (!Error && !skip(")"))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000513 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000514}
515
George Rimar652852c2016-04-16 10:10:32 +0000516void ScriptParser::readLocationCounterValue() {
517 expect(".");
518 expect("=");
Rui Ueyama07320e42016-04-20 20:13:41 +0000519 Opt.Commands.push_back({ExprKind, {}, ""});
520 SectionsCommand &Cmd = Opt.Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000521 while (!Error) {
522 StringRef Tok = next();
523 if (Tok == ";")
524 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000525 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000526 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000527 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000528 error("error in location counter expression");
529}
530
Rui Ueyama717677a2016-02-11 21:17:59 +0000531void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000532 StringRef OutSec = next();
Rui Ueyama07320e42016-04-20 20:13:41 +0000533 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000534 expect(":");
535 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000536
Rui Ueyama025d59b2016-02-02 20:27:59 +0000537 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000538 StringRef Tok = next();
539 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000540 expect("(");
541 while (!Error && !skip(")"))
542 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000543 } else if (Tok == "KEEP") {
544 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000545 expect("*");
546 expect("(");
547 while (!Error && !skip(")")) {
548 StringRef Sec = next();
549 Opt.Sections.emplace_back(OutSec, Sec);
550 Opt.KeptSections.push_back(Sec);
551 }
George Rimar481c2ce2016-02-23 07:47:54 +0000552 expect(")");
553 } else {
George Rimar777f9632016-03-12 08:31:34 +0000554 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000555 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000556 }
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000557
George Rimare2ee72b2016-02-26 14:48:31 +0000558 StringRef Tok = peek();
559 if (Tok.startswith("=")) {
560 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000561 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000562 return;
563 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000564 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000565 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000566 next();
567 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000568}
569
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000570static bool isUnderSysroot(StringRef Path) {
571 if (Config->Sysroot == "")
572 return false;
573 for (; !Path.empty(); Path = sys::path::parent_path(Path))
574 if (sys::fs::equivalent(Config->Sysroot, Path))
575 return true;
576 return false;
577}
578
Rui Ueyama07320e42016-04-20 20:13:41 +0000579// Entry point.
580void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000581 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000582 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000583}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000584
Rui Ueyama07320e42016-04-20 20:13:41 +0000585template class elf::LinkerScript<ELF32LE>;
586template class elf::LinkerScript<ELF32BE>;
587template class elf::LinkerScript<ELF64LE>;
588template class elf::LinkerScript<ELF64BE>;