blob: 9fcb2f61188c8eaaca0878fc1c0b7793e3bb321d [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
100static uint64_t apply(StringRef Op, uint64_t L, uint64_t R) {
101 if (Op == "+")
102 return L + R;
103 if (Op == "-")
104 return L - R;
105 if (Op == "*")
106 return L * R;
107 if (Op == "/") {
108 if (R == 0) {
109 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000110 return 0;
111 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000112 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000113 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000114 if (Op == "&")
115 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000116 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000117 return 0;
118}
119
120// This is an operator-precedence parser to evaluate
121// arithmetic expressions in SECTIONS command.
Rui Ueyama99e519c2016-04-20 20:48:25 +0000122// Tokens should start with an operator.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000123template <class ELFT>
124uint64_t LinkerScript<ELFT>::parseExpr1(ArrayRef<StringRef> &Tokens,
125 uint64_t Lhs, int MinPrec) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000126 while (!Tokens.empty()) {
127 // Read an operator and an expression.
128 StringRef Op1 = Tokens.front();
129 if (precedence(Op1) < MinPrec)
130 return Lhs;
131 next(Tokens);
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000132 uint64_t Rhs = parsePrimary(Tokens);
Rui Ueyama960504b2016-04-19 18:58:11 +0000133
134 // Evaluate the remaining part of the expression first if the
135 // next operator has greater precedence than the previous one.
136 // For example, if we have read "+" and "3", and if the next
137 // operator is "*", then we'll evaluate 3 * ... part first.
138 while (!Tokens.empty()) {
139 StringRef Op2 = Tokens.front();
140 if (precedence(Op2) <= precedence(Op1))
141 break;
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000142 Rhs = parseExpr1(Tokens, Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000143 }
144
145 Lhs = apply(Op1, Lhs, Rhs);
146 }
147 return Lhs;
148}
149
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000150template <class ELFT>
151uint64_t LinkerScript<ELFT>::parseExpr(ArrayRef<StringRef> &Tokens) {
152 uint64_t V = parsePrimary(Tokens);
153 return parseExpr1(Tokens, V, 0);
Rui Ueyama99e519c2016-04-20 20:48:25 +0000154}
155
Rui Ueyama960504b2016-04-19 18:58:11 +0000156// Evaluates the expression given by list of tokens.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000157template <class ELFT>
158uint64_t LinkerScript<ELFT>::evaluate(ArrayRef<StringRef> Tokens) {
159 uint64_t V = parseExpr(Tokens);
Rui Ueyama960504b2016-04-19 18:58:11 +0000160 if (!Tokens.empty())
161 error("stray token: " + Tokens[0]);
162 return V;
George Rimar652852c2016-04-16 10:10:32 +0000163}
164
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000165template <class ELFT>
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000166StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000167 for (SectionRule &R : Opt.Sections)
Rui Ueyamac9f402e2016-04-22 00:23:52 +0000168 if (matchStr(R.SectionPattern, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000169 return R.Dest;
170 return "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000171}
172
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000173template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000174bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000175 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000176}
177
Rui Ueyama07320e42016-04-20 20:13:41 +0000178template <class ELFT>
179bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000180 for (StringRef Pat : Opt.KeptSections)
181 if (matchStr(Pat, S->getSectionName()))
182 return true;
183 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000184}
185
George Rimar652852c2016-04-16 10:10:32 +0000186template <class ELFT>
Rui Ueyama7c18c282016-04-18 21:00:40 +0000187static OutputSectionBase<ELFT> *
George Rimardbbd8b12016-04-21 11:21:48 +0000188findSection(ArrayRef<OutputSectionBase<ELFT> *> V, StringRef Name) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000189 for (OutputSectionBase<ELFT> *Sec : V)
190 if (Sec->getName() == Name)
191 return Sec;
192 return nullptr;
193}
194
195template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000196void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000197 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000198 typedef typename ELFT::uint uintX_t;
199
George Rimar652852c2016-04-16 10:10:32 +0000200 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000201 // are not explicitly placed into the output file by the linker script.
202 // We place orphan sections at end of file.
203 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000204 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000205 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000206 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000207 if (getSectionIndex(Name) == INT_MAX)
Rui Ueyama07320e42016-04-20 20:13:41 +0000208 Opt.Commands.push_back({SectionKind, {}, Name});
George Rimar652852c2016-04-16 10:10:32 +0000209 }
George Rimar652852c2016-04-16 10:10:32 +0000210
Rui Ueyama7c18c282016-04-18 21:00:40 +0000211 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000212 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000213 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000214
Rui Ueyama07320e42016-04-20 20:13:41 +0000215 for (SectionsCommand &Cmd : Opt.Commands) {
Rui Ueyama9e957a02016-04-18 21:00:45 +0000216 if (Cmd.Kind == ExprKind) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000217 Dot = evaluate(Cmd.Expr);
George Rimar652852c2016-04-16 10:10:32 +0000218 continue;
219 }
220
George Rimardbbd8b12016-04-21 11:21:48 +0000221 OutputSectionBase<ELFT> *Sec = findSection<ELFT>(Sections, Cmd.SectionName);
Rui Ueyama7c18c282016-04-18 21:00:40 +0000222 if (!Sec)
George Rimar652852c2016-04-16 10:10:32 +0000223 continue;
224
George Rimar652852c2016-04-16 10:10:32 +0000225 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000226 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama7c18c282016-04-18 21:00:40 +0000227 TVA = alignTo(TVA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000228 Sec->setVA(TVA);
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000229 ThreadBssOffset = TVA - Dot + Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000230 continue;
231 }
232
233 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000234 Dot = alignTo(Dot, Sec->getAlign());
235 Sec->setVA(Dot);
236 Dot += Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000237 continue;
238 }
239 }
240}
241
Rui Ueyama07320e42016-04-20 20:13:41 +0000242template <class ELFT>
243ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
244 auto I = Opt.Filler.find(Name);
245 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000246 return {};
247 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000248}
249
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000250// Returns the index of the given section name in linker script
251// SECTIONS commands. Sections are laid out as the same order as they
252// were in the script. If a given name did not appear in the script,
253// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar71b26e92016-04-21 10:22:02 +0000254template <class ELFT>
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000255int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000256 auto Begin = Opt.Commands.begin();
257 auto End = Opt.Commands.end();
258 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
259 return N.Kind == SectionKind && N.SectionName == Name;
260 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000261 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000262}
263
264// A compartor to sort output sections. Returns -1 or 1 if
265// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000266template <class ELFT>
267int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000268 int I = getSectionIndex(A);
269 int J = getSectionIndex(B);
270 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000271 return 0;
272 return I < J ? -1 : 1;
273}
274
George Rimarcb2aeb62016-02-24 08:49:50 +0000275// Returns true if S matches T. S can contain glob meta-characters.
276// The asterisk ('*') matches zero or more characacters, and the question
277// mark ('?') matches one character.
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000278static bool matchStr(StringRef S, StringRef T) {
279 for (;;) {
280 if (S.empty())
281 return T.empty();
282 if (S[0] == '*') {
283 S = S.substr(1);
284 if (S.empty())
285 // Fast path. If a pattern is '*', it matches anything.
286 return true;
287 for (size_t I = 0, E = T.size(); I < E; ++I)
288 if (matchStr(S, T.substr(I)))
289 return true;
290 return false;
291 }
George Rimarcb2aeb62016-02-24 08:49:50 +0000292 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000293 return false;
294 S = S.substr(1);
295 T = T.substr(1);
296 }
297}
298
Rui Ueyama07320e42016-04-20 20:13:41 +0000299class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000300 typedef void (ScriptParser::*Handler)();
301
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000302public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000303 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000304
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +0000305 void run() override;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000306
307private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000308 void addFile(StringRef Path);
309
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000310 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000311 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000312 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000313 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000314 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000315 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000316 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000317 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000318 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000319 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000320 void readSections();
321
George Rimar652852c2016-04-16 10:10:32 +0000322 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000323 void readOutputSectionDescription();
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000324 void readSectionPatterns(StringRef OutSec);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000325
George Rimarc3794e52016-02-24 09:21:47 +0000326 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000327 ScriptConfiguration &Opt = *ScriptConfig;
328 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000329 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000330};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000331
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000332const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000333 {"ENTRY", &ScriptParser::readEntry},
334 {"EXTERN", &ScriptParser::readExtern},
335 {"GROUP", &ScriptParser::readGroup},
336 {"INCLUDE", &ScriptParser::readInclude},
337 {"INPUT", &ScriptParser::readGroup},
338 {"OUTPUT", &ScriptParser::readOutput},
339 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
340 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
341 {"SEARCH_DIR", &ScriptParser::readSearchDir},
342 {"SECTIONS", &ScriptParser::readSections},
343 {";", &ScriptParser::readNothing}};
344
Rui Ueyama717677a2016-02-11 21:17:59 +0000345void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000346 while (!atEOF()) {
347 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000348 if (Handler Fn = Cmd.lookup(Tok))
349 (this->*Fn)();
350 else
George Rimar57610422016-03-11 14:43:02 +0000351 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000352 }
353}
354
Rui Ueyama717677a2016-02-11 21:17:59 +0000355void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000356 if (IsUnderSysroot && S.startswith("/")) {
357 SmallString<128> Path;
358 (Config->Sysroot + S).toStringRef(Path);
359 if (sys::fs::exists(Path)) {
360 Driver->addFile(Saver.save(Path.str()));
361 return;
362 }
363 }
364
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000365 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000366 Driver->addFile(S);
367 } else if (S.startswith("=")) {
368 if (Config->Sysroot.empty())
369 Driver->addFile(S.substr(1));
370 else
371 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
372 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000373 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000374 } else if (sys::fs::exists(S)) {
375 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000376 } else {
377 std::string Path = findFromSearchPaths(S);
378 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000379 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000380 else
381 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000382 }
383}
384
Rui Ueyama717677a2016-02-11 21:17:59 +0000385void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000386 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000387 bool Orig = Config->AsNeeded;
388 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000389 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000390 StringRef Tok = next();
391 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000392 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000393 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000394 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000395 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000396}
397
Rui Ueyama717677a2016-02-11 21:17:59 +0000398void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000399 // -e <symbol> takes predecence over ENTRY(<symbol>).
400 expect("(");
401 StringRef Tok = next();
402 if (Config->Entry.empty())
403 Config->Entry = Tok;
404 expect(")");
405}
406
Rui Ueyama717677a2016-02-11 21:17:59 +0000407void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000408 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000409 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000410 StringRef Tok = next();
411 if (Tok == ")")
412 return;
413 Config->Undefined.push_back(Tok);
414 }
415}
416
Rui Ueyama717677a2016-02-11 21:17:59 +0000417void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000418 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000419 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000420 StringRef Tok = next();
421 if (Tok == ")")
422 return;
423 if (Tok == "AS_NEEDED") {
424 readAsNeeded();
425 continue;
426 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000427 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000428 }
429}
430
Rui Ueyama717677a2016-02-11 21:17:59 +0000431void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000432 StringRef Tok = next();
433 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000434 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000435 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000436 return;
437 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000438 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000439 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
440 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000441 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000442}
443
Rui Ueyama717677a2016-02-11 21:17:59 +0000444void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000445 // -o <file> takes predecence over OUTPUT(<file>).
446 expect("(");
447 StringRef Tok = next();
448 if (Config->OutputFile.empty())
449 Config->OutputFile = Tok;
450 expect(")");
451}
452
Rui Ueyama717677a2016-02-11 21:17:59 +0000453void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000454 // Error checking only for now.
455 expect("(");
456 next();
457 expect(")");
458}
459
Rui Ueyama717677a2016-02-11 21:17:59 +0000460void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000461 // Error checking only for now.
462 expect("(");
463 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000464 StringRef Tok = next();
465 if (Tok == ")")
466 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000467 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000468 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000469 return;
470 }
Davide Italiano6836c612015-10-12 21:08:41 +0000471 next();
472 expect(",");
473 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000474 expect(")");
475}
476
Rui Ueyama717677a2016-02-11 21:17:59 +0000477void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000478 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000479 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000480 expect(")");
481}
482
Rui Ueyama717677a2016-02-11 21:17:59 +0000483void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000484 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000485 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000486 while (!Error && !skip("}")) {
487 StringRef Tok = peek();
488 if (Tok == ".")
489 readLocationCounterValue();
490 else
491 readOutputSectionDescription();
492 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000493}
494
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000495void ScriptParser::readSectionPatterns(StringRef OutSec) {
George Rimar481c2ce2016-02-23 07:47:54 +0000496 expect("(");
497 while (!Error && !skip(")"))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000498 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000499}
500
George Rimar652852c2016-04-16 10:10:32 +0000501void ScriptParser::readLocationCounterValue() {
502 expect(".");
503 expect("=");
Rui Ueyama07320e42016-04-20 20:13:41 +0000504 Opt.Commands.push_back({ExprKind, {}, ""});
505 SectionsCommand &Cmd = Opt.Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000506 while (!Error) {
507 StringRef Tok = next();
508 if (Tok == ";")
509 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000510 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000511 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000512 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000513 error("error in location counter expression");
514}
515
Rui Ueyama717677a2016-02-11 21:17:59 +0000516void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000517 StringRef OutSec = next();
Rui Ueyama07320e42016-04-20 20:13:41 +0000518 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000519 expect(":");
520 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000521
Rui Ueyama025d59b2016-02-02 20:27:59 +0000522 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000523 StringRef Tok = next();
524 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000525 expect("(");
526 while (!Error && !skip(")"))
527 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000528 } else if (Tok == "KEEP") {
529 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000530 expect("*");
531 expect("(");
532 while (!Error && !skip(")")) {
533 StringRef Sec = next();
534 Opt.Sections.emplace_back(OutSec, Sec);
535 Opt.KeptSections.push_back(Sec);
536 }
George Rimar481c2ce2016-02-23 07:47:54 +0000537 expect(")");
538 } else {
George Rimar777f9632016-03-12 08:31:34 +0000539 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000540 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000541 }
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000542
George Rimare2ee72b2016-02-26 14:48:31 +0000543 StringRef Tok = peek();
544 if (Tok.startswith("=")) {
545 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000546 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000547 return;
548 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000549 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000550 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000551 next();
552 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000553}
554
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000555static bool isUnderSysroot(StringRef Path) {
556 if (Config->Sysroot == "")
557 return false;
558 for (; !Path.empty(); Path = sys::path::parent_path(Path))
559 if (sys::fs::equivalent(Config->Sysroot, Path))
560 return true;
561 return false;
562}
563
Rui Ueyama07320e42016-04-20 20:13:41 +0000564// Entry point.
565void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000566 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000567 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000568}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000569
Rui Ueyama07320e42016-04-20 20:13:41 +0000570template class elf::LinkerScript<ELF32LE>;
571template class elf::LinkerScript<ELF32BE>;
572template class elf::LinkerScript<ELF64LE>;
573template class elf::LinkerScript<ELF64BE>;