blob: 77ba023b0496c814738eb38ee81e2f15257c9c6f [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 }
97 return getInteger(Tok);
98}
99
George Rimarfba45c42016-04-22 11:28:54 +0000100template <class ELFT>
101uint64_t LinkerScript<ELFT>::parseTernary(ArrayRef<StringRef> &Tokens,
102 uint64_t Cond) {
103 next(Tokens);
104 uint64_t V = parseExpr(Tokens);
105 if (!expect(Tokens, ":"))
106 return 0;
107 uint64_t W = parseExpr(Tokens);
108 return Cond ? V : W;
109}
110
Rui Ueyama960504b2016-04-19 18:58:11 +0000111static uint64_t apply(StringRef Op, uint64_t L, uint64_t R) {
112 if (Op == "+")
113 return L + R;
114 if (Op == "-")
115 return L - R;
116 if (Op == "*")
117 return L * R;
118 if (Op == "/") {
119 if (R == 0) {
120 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000121 return 0;
122 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000123 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000124 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000125 if (Op == "&")
126 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000127 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000128 return 0;
129}
130
131// This is an operator-precedence parser to evaluate
132// arithmetic expressions in SECTIONS command.
Rui Ueyama99e519c2016-04-20 20:48:25 +0000133// Tokens should start with an operator.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000134template <class ELFT>
135uint64_t LinkerScript<ELFT>::parseExpr1(ArrayRef<StringRef> &Tokens,
136 uint64_t Lhs, int MinPrec) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000137 while (!Tokens.empty()) {
138 // Read an operator and an expression.
139 StringRef Op1 = Tokens.front();
George Rimarfba45c42016-04-22 11:28:54 +0000140 if (Op1 == "?")
141 return parseTernary(Tokens, Lhs);
142
Rui Ueyama960504b2016-04-19 18:58:11 +0000143 if (precedence(Op1) < MinPrec)
144 return Lhs;
145 next(Tokens);
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000146 uint64_t Rhs = parsePrimary(Tokens);
Rui Ueyama960504b2016-04-19 18:58:11 +0000147
148 // Evaluate the remaining part of the expression first if the
149 // next operator has greater precedence than the previous one.
150 // For example, if we have read "+" and "3", and if the next
151 // operator is "*", then we'll evaluate 3 * ... part first.
152 while (!Tokens.empty()) {
153 StringRef Op2 = Tokens.front();
154 if (precedence(Op2) <= precedence(Op1))
155 break;
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000156 Rhs = parseExpr1(Tokens, Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000157 }
158
159 Lhs = apply(Op1, Lhs, Rhs);
160 }
161 return Lhs;
162}
163
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000164template <class ELFT>
165uint64_t LinkerScript<ELFT>::parseExpr(ArrayRef<StringRef> &Tokens) {
166 uint64_t V = parsePrimary(Tokens);
167 return parseExpr1(Tokens, V, 0);
Rui Ueyama99e519c2016-04-20 20:48:25 +0000168}
169
Rui Ueyama960504b2016-04-19 18:58:11 +0000170// Evaluates the expression given by list of tokens.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000171template <class ELFT>
172uint64_t LinkerScript<ELFT>::evaluate(ArrayRef<StringRef> Tokens) {
173 uint64_t V = parseExpr(Tokens);
Rui Ueyama960504b2016-04-19 18:58:11 +0000174 if (!Tokens.empty())
175 error("stray token: " + Tokens[0]);
176 return V;
George Rimar652852c2016-04-16 10:10:32 +0000177}
178
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000179template <class ELFT>
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000180StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000181 for (SectionRule &R : Opt.Sections)
Rui Ueyamac9f402e2016-04-22 00:23:52 +0000182 if (matchStr(R.SectionPattern, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000183 return R.Dest;
184 return "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000185}
186
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000187template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000188bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000189 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000190}
191
Rui Ueyama07320e42016-04-20 20:13:41 +0000192template <class ELFT>
193bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000194 for (StringRef Pat : Opt.KeptSections)
195 if (matchStr(Pat, S->getSectionName()))
196 return true;
197 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000198}
199
George Rimar652852c2016-04-16 10:10:32 +0000200template <class ELFT>
Rui Ueyama7c18c282016-04-18 21:00:40 +0000201static OutputSectionBase<ELFT> *
George Rimardbbd8b12016-04-21 11:21:48 +0000202findSection(ArrayRef<OutputSectionBase<ELFT> *> V, StringRef Name) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000203 for (OutputSectionBase<ELFT> *Sec : V)
204 if (Sec->getName() == Name)
205 return Sec;
206 return nullptr;
207}
208
209template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000210void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000211 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000212 typedef typename ELFT::uint uintX_t;
213
George Rimar652852c2016-04-16 10:10:32 +0000214 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000215 // are not explicitly placed into the output file by the linker script.
216 // We place orphan sections at end of file.
217 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000218 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000219 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000220 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000221 if (getSectionIndex(Name) == INT_MAX)
Rui Ueyama07320e42016-04-20 20:13:41 +0000222 Opt.Commands.push_back({SectionKind, {}, Name});
George Rimar652852c2016-04-16 10:10:32 +0000223 }
George Rimar652852c2016-04-16 10:10:32 +0000224
Rui Ueyama7c18c282016-04-18 21:00:40 +0000225 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000226 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000227 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000228
Rui Ueyama07320e42016-04-20 20:13:41 +0000229 for (SectionsCommand &Cmd : Opt.Commands) {
Rui Ueyama9e957a02016-04-18 21:00:45 +0000230 if (Cmd.Kind == ExprKind) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000231 Dot = evaluate(Cmd.Expr);
George Rimar652852c2016-04-16 10:10:32 +0000232 continue;
233 }
234
George Rimardbbd8b12016-04-21 11:21:48 +0000235 OutputSectionBase<ELFT> *Sec = findSection<ELFT>(Sections, Cmd.SectionName);
Rui Ueyama7c18c282016-04-18 21:00:40 +0000236 if (!Sec)
George Rimar652852c2016-04-16 10:10:32 +0000237 continue;
238
George Rimar652852c2016-04-16 10:10:32 +0000239 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000240 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama7c18c282016-04-18 21:00:40 +0000241 TVA = alignTo(TVA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000242 Sec->setVA(TVA);
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000243 ThreadBssOffset = TVA - Dot + Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000244 continue;
245 }
246
247 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000248 Dot = alignTo(Dot, Sec->getAlign());
249 Sec->setVA(Dot);
250 Dot += Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000251 continue;
252 }
253 }
254}
255
Rui Ueyama07320e42016-04-20 20:13:41 +0000256template <class ELFT>
257ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
258 auto I = Opt.Filler.find(Name);
259 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000260 return {};
261 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000262}
263
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000264// Returns the index of the given section name in linker script
265// SECTIONS commands. Sections are laid out as the same order as they
266// were in the script. If a given name did not appear in the script,
267// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar71b26e92016-04-21 10:22:02 +0000268template <class ELFT>
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000269int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000270 auto Begin = Opt.Commands.begin();
271 auto End = Opt.Commands.end();
272 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
273 return N.Kind == SectionKind && N.SectionName == Name;
274 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000275 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000276}
277
278// A compartor to sort output sections. Returns -1 or 1 if
279// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000280template <class ELFT>
281int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000282 int I = getSectionIndex(A);
283 int J = getSectionIndex(B);
284 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000285 return 0;
286 return I < J ? -1 : 1;
287}
288
George Rimarcb2aeb62016-02-24 08:49:50 +0000289// Returns true if S matches T. S can contain glob meta-characters.
290// The asterisk ('*') matches zero or more characacters, and the question
291// mark ('?') matches one character.
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000292static bool matchStr(StringRef S, StringRef T) {
293 for (;;) {
294 if (S.empty())
295 return T.empty();
296 if (S[0] == '*') {
297 S = S.substr(1);
298 if (S.empty())
299 // Fast path. If a pattern is '*', it matches anything.
300 return true;
301 for (size_t I = 0, E = T.size(); I < E; ++I)
302 if (matchStr(S, T.substr(I)))
303 return true;
304 return false;
305 }
George Rimarcb2aeb62016-02-24 08:49:50 +0000306 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000307 return false;
308 S = S.substr(1);
309 T = T.substr(1);
310 }
311}
312
Rui Ueyama07320e42016-04-20 20:13:41 +0000313class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000314 typedef void (ScriptParser::*Handler)();
315
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000316public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000317 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000318
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +0000319 void run() override;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000320
321private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000322 void addFile(StringRef Path);
323
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000324 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000325 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000326 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000327 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000328 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000329 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000330 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000331 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000332 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000333 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000334 void readSections();
335
George Rimar652852c2016-04-16 10:10:32 +0000336 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000337 void readOutputSectionDescription();
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000338 void readSectionPatterns(StringRef OutSec);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000339
George Rimarc3794e52016-02-24 09:21:47 +0000340 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000341 ScriptConfiguration &Opt = *ScriptConfig;
342 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000343 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000344};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000345
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000346const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000347 {"ENTRY", &ScriptParser::readEntry},
348 {"EXTERN", &ScriptParser::readExtern},
349 {"GROUP", &ScriptParser::readGroup},
350 {"INCLUDE", &ScriptParser::readInclude},
351 {"INPUT", &ScriptParser::readGroup},
352 {"OUTPUT", &ScriptParser::readOutput},
353 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
354 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
355 {"SEARCH_DIR", &ScriptParser::readSearchDir},
356 {"SECTIONS", &ScriptParser::readSections},
357 {";", &ScriptParser::readNothing}};
358
Rui Ueyama717677a2016-02-11 21:17:59 +0000359void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000360 while (!atEOF()) {
361 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000362 if (Handler Fn = Cmd.lookup(Tok))
363 (this->*Fn)();
364 else
George Rimar57610422016-03-11 14:43:02 +0000365 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000366 }
367}
368
Rui Ueyama717677a2016-02-11 21:17:59 +0000369void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000370 if (IsUnderSysroot && S.startswith("/")) {
371 SmallString<128> Path;
372 (Config->Sysroot + S).toStringRef(Path);
373 if (sys::fs::exists(Path)) {
374 Driver->addFile(Saver.save(Path.str()));
375 return;
376 }
377 }
378
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000379 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000380 Driver->addFile(S);
381 } else if (S.startswith("=")) {
382 if (Config->Sysroot.empty())
383 Driver->addFile(S.substr(1));
384 else
385 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
386 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000387 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000388 } else if (sys::fs::exists(S)) {
389 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000390 } else {
391 std::string Path = findFromSearchPaths(S);
392 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000393 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000394 else
395 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000396 }
397}
398
Rui Ueyama717677a2016-02-11 21:17:59 +0000399void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000400 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000401 bool Orig = Config->AsNeeded;
402 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000403 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000404 StringRef Tok = next();
405 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000406 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000407 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000408 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000409 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000410}
411
Rui Ueyama717677a2016-02-11 21:17:59 +0000412void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000413 // -e <symbol> takes predecence over ENTRY(<symbol>).
414 expect("(");
415 StringRef Tok = next();
416 if (Config->Entry.empty())
417 Config->Entry = Tok;
418 expect(")");
419}
420
Rui Ueyama717677a2016-02-11 21:17:59 +0000421void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000422 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000423 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000424 StringRef Tok = next();
425 if (Tok == ")")
426 return;
427 Config->Undefined.push_back(Tok);
428 }
429}
430
Rui Ueyama717677a2016-02-11 21:17:59 +0000431void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000432 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000433 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000434 StringRef Tok = next();
435 if (Tok == ")")
436 return;
437 if (Tok == "AS_NEEDED") {
438 readAsNeeded();
439 continue;
440 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000441 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000442 }
443}
444
Rui Ueyama717677a2016-02-11 21:17:59 +0000445void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000446 StringRef Tok = next();
447 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000448 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000449 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000450 return;
451 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000452 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000453 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
454 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000455 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000456}
457
Rui Ueyama717677a2016-02-11 21:17:59 +0000458void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000459 // -o <file> takes predecence over OUTPUT(<file>).
460 expect("(");
461 StringRef Tok = next();
462 if (Config->OutputFile.empty())
463 Config->OutputFile = Tok;
464 expect(")");
465}
466
Rui Ueyama717677a2016-02-11 21:17:59 +0000467void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000468 // Error checking only for now.
469 expect("(");
470 next();
471 expect(")");
472}
473
Rui Ueyama717677a2016-02-11 21:17:59 +0000474void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000475 // Error checking only for now.
476 expect("(");
477 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000478 StringRef Tok = next();
479 if (Tok == ")")
480 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000481 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000482 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000483 return;
484 }
Davide Italiano6836c612015-10-12 21:08:41 +0000485 next();
486 expect(",");
487 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000488 expect(")");
489}
490
Rui Ueyama717677a2016-02-11 21:17:59 +0000491void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000492 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000493 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000494 expect(")");
495}
496
Rui Ueyama717677a2016-02-11 21:17:59 +0000497void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000498 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000499 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000500 while (!Error && !skip("}")) {
501 StringRef Tok = peek();
502 if (Tok == ".")
503 readLocationCounterValue();
504 else
505 readOutputSectionDescription();
506 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000507}
508
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000509void ScriptParser::readSectionPatterns(StringRef OutSec) {
George Rimar481c2ce2016-02-23 07:47:54 +0000510 expect("(");
511 while (!Error && !skip(")"))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000512 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000513}
514
George Rimar652852c2016-04-16 10:10:32 +0000515void ScriptParser::readLocationCounterValue() {
516 expect(".");
517 expect("=");
Rui Ueyama07320e42016-04-20 20:13:41 +0000518 Opt.Commands.push_back({ExprKind, {}, ""});
519 SectionsCommand &Cmd = Opt.Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000520 while (!Error) {
521 StringRef Tok = next();
522 if (Tok == ";")
523 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000524 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000525 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000526 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000527 error("error in location counter expression");
528}
529
Rui Ueyama717677a2016-02-11 21:17:59 +0000530void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000531 StringRef OutSec = next();
Rui Ueyama07320e42016-04-20 20:13:41 +0000532 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000533 expect(":");
534 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000535
Rui Ueyama025d59b2016-02-02 20:27:59 +0000536 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000537 StringRef Tok = next();
538 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000539 expect("(");
540 while (!Error && !skip(")"))
541 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000542 } else if (Tok == "KEEP") {
543 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000544 expect("*");
545 expect("(");
546 while (!Error && !skip(")")) {
547 StringRef Sec = next();
548 Opt.Sections.emplace_back(OutSec, Sec);
549 Opt.KeptSections.push_back(Sec);
550 }
George Rimar481c2ce2016-02-23 07:47:54 +0000551 expect(")");
552 } else {
George Rimar777f9632016-03-12 08:31:34 +0000553 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000554 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000555 }
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000556
George Rimare2ee72b2016-02-26 14:48:31 +0000557 StringRef Tok = peek();
558 if (Tok.startswith("=")) {
559 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000560 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000561 return;
562 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000563 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000564 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000565 next();
566 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000567}
568
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000569static bool isUnderSysroot(StringRef Path) {
570 if (Config->Sysroot == "")
571 return false;
572 for (; !Path.empty(); Path = sys::path::parent_path(Path))
573 if (sys::fs::equivalent(Config->Sysroot, Path))
574 return true;
575 return false;
576}
577
Rui Ueyama07320e42016-04-20 20:13:41 +0000578// Entry point.
579void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000580 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000581 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000582}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000583
Rui Ueyama07320e42016-04-20 20:13:41 +0000584template class elf::LinkerScript<ELF32LE>;
585template class elf::LinkerScript<ELF32BE>;
586template class elf::LinkerScript<ELF64LE>;
587template class elf::LinkerScript<ELF64BE>;