blob: 0b69beac13826b599ca62c42c154b9df02cf5c74 [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
Rafael Espindolae0df00b2016-02-28 00:25:54 +000036LinkerScript *elf::Script;
Rui Ueyama717677a2016-02-11 21:17:59 +000037
George Rimar652852c2016-04-16 10:10:32 +000038static uint64_t getInteger(StringRef S) {
39 uint64_t V;
40 if (S.getAsInteger(0, V)) {
41 error("malformed number: " + S);
42 return 0;
43 }
44 return V;
45}
46
Rui Ueyama960504b2016-04-19 18:58:11 +000047static int precedence(StringRef Op) {
48 return StringSwitch<int>(Op)
49 .Case("*", 4)
50 .Case("/", 3)
51 .Case("+", 2)
52 .Case("-", 2)
53 .Case("&", 1)
54 .Default(-1);
55}
56
57static StringRef next(ArrayRef<StringRef> &Tokens) {
58 if (Tokens.empty()) {
59 error("no next token");
60 return "";
61 }
62 StringRef Tok = Tokens.front();
63 Tokens = Tokens.slice(1);
64 return Tok;
65}
66
67static uint64_t parseExpr(uint64_t Lhs, int MinPrec,
68 ArrayRef<StringRef> &Tokens, uint64_t Dot);
69
70// This is a part of the operator-precedence parser to evaluate
71// arithmetic expressions in SECTIONS command. This function evaluates an
72// integer literal, a parenthesized expression or the special variable ".".
73static uint64_t parsePrimary(ArrayRef<StringRef> &Tokens, uint64_t Dot) {
74 StringRef Tok = next(Tokens);
75 if (Tok == ".")
76 return Dot;
77 if (Tok == "(") {
78 uint64_t V = parsePrimary(Tokens, Dot);
79 V = parseExpr(V, 0, Tokens, Dot);
80 if (Tokens.empty()) {
81 error(") expected");
82 } else {
83 Tok = next(Tokens);
84 if (Tok != ")")
85 error(") expected, but got " + Tok);
86 }
87 return V;
88 }
89 return getInteger(Tok);
90}
91
92static uint64_t apply(StringRef Op, uint64_t L, uint64_t R) {
93 if (Op == "+")
94 return L + R;
95 if (Op == "-")
96 return L - R;
97 if (Op == "*")
98 return L * R;
99 if (Op == "/") {
100 if (R == 0) {
101 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000102 return 0;
103 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000104 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000105 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000106 if (Op == "&")
107 return L & R;
108 llvm_unreachable("unknown operator " + Op);
109 return 0;
110}
111
112// This is an operator-precedence parser to evaluate
113// arithmetic expressions in SECTIONS command.
114static uint64_t parseExpr(uint64_t Lhs, int MinPrec,
115 ArrayRef<StringRef> &Tokens, uint64_t Dot) {
116 while (!Tokens.empty()) {
117 // Read an operator and an expression.
118 StringRef Op1 = Tokens.front();
119 if (precedence(Op1) < MinPrec)
120 return Lhs;
121 next(Tokens);
122 uint64_t Rhs = parsePrimary(Tokens, Dot);
123
124 // Evaluate the remaining part of the expression first if the
125 // next operator has greater precedence than the previous one.
126 // For example, if we have read "+" and "3", and if the next
127 // operator is "*", then we'll evaluate 3 * ... part first.
128 while (!Tokens.empty()) {
129 StringRef Op2 = Tokens.front();
130 if (precedence(Op2) <= precedence(Op1))
131 break;
132 Rhs = parseExpr(Rhs, precedence(Op2), Tokens, Dot);
133 }
134
135 Lhs = apply(Op1, Lhs, Rhs);
136 }
137 return Lhs;
138}
139
140// Evaluates the expression given by list of tokens.
141uint64_t evaluate(ArrayRef<StringRef> Tokens, uint64_t Dot) {
142 uint64_t V = parsePrimary(Tokens, Dot);
143 V = parseExpr(V, 0, Tokens, Dot);
144 if (!Tokens.empty())
145 error("stray token: " + Tokens[0]);
146 return V;
George Rimar652852c2016-04-16 10:10:32 +0000147}
148
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000149template <class ELFT>
George Rimar481c2ce2016-02-23 07:47:54 +0000150SectionRule *LinkerScript::find(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000151 for (SectionRule &R : Sections)
152 if (R.match(S))
George Rimar481c2ce2016-02-23 07:47:54 +0000153 return &R;
154 return nullptr;
155}
156
157template <class ELFT>
158StringRef LinkerScript::getOutputSection(InputSectionBase<ELFT> *S) {
159 SectionRule *R = find(S);
160 return R ? R->Dest : "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000161}
162
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000163template <class ELFT>
164bool LinkerScript::isDiscarded(InputSectionBase<ELFT> *S) {
165 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000166}
167
George Rimar481c2ce2016-02-23 07:47:54 +0000168template <class ELFT> bool LinkerScript::shouldKeep(InputSectionBase<ELFT> *S) {
169 SectionRule *R = find(S);
170 return R && R->Keep;
171}
172
George Rimar652852c2016-04-16 10:10:32 +0000173template <class ELFT>
Rui Ueyama7c18c282016-04-18 21:00:40 +0000174static OutputSectionBase<ELFT> *
175findSection(std::vector<OutputSectionBase<ELFT> *> &V, StringRef Name) {
176 for (OutputSectionBase<ELFT> *Sec : V)
177 if (Sec->getName() == Name)
178 return Sec;
179 return nullptr;
180}
181
182template <class ELFT>
183void LinkerScript::assignAddresses(
184 std::vector<OutputSectionBase<ELFT> *> &Sections) {
185 typedef typename ELFT::uint uintX_t;
186
George Rimar652852c2016-04-16 10:10:32 +0000187 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000188 // are not explicitly placed into the output file by the linker script.
189 // We place orphan sections at end of file.
190 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000191 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000192 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000193 StringRef Name = Sec->getName();
194 auto I = std::find(SectionOrder.begin(), SectionOrder.end(), Name);
195 if (I == SectionOrder.end())
Rui Ueyama9e957a02016-04-18 21:00:45 +0000196 Commands.push_back({SectionKind, {}, Name});
George Rimar652852c2016-04-16 10:10:32 +0000197 }
George Rimar652852c2016-04-16 10:10:32 +0000198
Rui Ueyama7c18c282016-04-18 21:00:40 +0000199 // Assign addresses as instructed by linker script SECTIONS sub-commands.
George Rimar652852c2016-04-16 10:10:32 +0000200 uintX_t ThreadBssOffset = 0;
201 uintX_t VA =
202 Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
203
Rui Ueyama9e957a02016-04-18 21:00:45 +0000204 for (SectionsCommand &Cmd : Commands) {
205 if (Cmd.Kind == ExprKind) {
206 VA = evaluate(Cmd.Expr, VA);
George Rimar652852c2016-04-16 10:10:32 +0000207 continue;
208 }
209
Rui Ueyama9e957a02016-04-18 21:00:45 +0000210 OutputSectionBase<ELFT> *Sec = findSection(Sections, Cmd.SectionName);
Rui Ueyama7c18c282016-04-18 21:00:40 +0000211 if (!Sec)
George Rimar652852c2016-04-16 10:10:32 +0000212 continue;
213
George Rimar652852c2016-04-16 10:10:32 +0000214 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
215 uintX_t TVA = VA + ThreadBssOffset;
Rui Ueyama7c18c282016-04-18 21:00:40 +0000216 TVA = alignTo(TVA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000217 Sec->setVA(TVA);
218 ThreadBssOffset = TVA - VA + Sec->getSize();
219 continue;
220 }
221
222 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000223 VA = alignTo(VA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000224 Sec->setVA(VA);
225 VA += Sec->getSize();
226 continue;
227 }
228 }
229}
230
George Rimare2ee72b2016-02-26 14:48:31 +0000231ArrayRef<uint8_t> LinkerScript::getFiller(StringRef Name) {
Rui Ueyama3e808972016-02-28 05:09:11 +0000232 auto I = Filler.find(Name);
233 if (I == Filler.end())
234 return {};
235 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000236}
237
Rui Ueyamae9c58062016-02-12 20:41:43 +0000238// A compartor to sort output sections. Returns -1 or 1 if both
239// A and B are mentioned in linker scripts. Otherwise, returns 0
240// to use the default rule which is implemented in Writer.cpp.
Rui Ueyama717677a2016-02-11 21:17:59 +0000241int LinkerScript::compareSections(StringRef A, StringRef B) {
Rui Ueyama3e808972016-02-28 05:09:11 +0000242 auto E = SectionOrder.end();
243 auto I = std::find(SectionOrder.begin(), E, A);
244 auto J = std::find(SectionOrder.begin(), E, B);
245 if (I == E || J == E)
Rui Ueyama717677a2016-02-11 21:17:59 +0000246 return 0;
247 return I < J ? -1 : 1;
248}
249
George Rimarcb2aeb62016-02-24 08:49:50 +0000250// Returns true if S matches T. S can contain glob meta-characters.
251// The asterisk ('*') matches zero or more characacters, and the question
252// mark ('?') matches one character.
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000253static bool matchStr(StringRef S, StringRef T) {
254 for (;;) {
255 if (S.empty())
256 return T.empty();
257 if (S[0] == '*') {
258 S = S.substr(1);
259 if (S.empty())
260 // Fast path. If a pattern is '*', it matches anything.
261 return true;
262 for (size_t I = 0, E = T.size(); I < E; ++I)
263 if (matchStr(S, T.substr(I)))
264 return true;
265 return false;
266 }
George Rimarcb2aeb62016-02-24 08:49:50 +0000267 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000268 return false;
269 S = S.substr(1);
270 T = T.substr(1);
271 }
272}
273
274template <class ELFT> bool SectionRule::match(InputSectionBase<ELFT> *S) {
275 return matchStr(SectionPattern, S->getSectionName());
276}
277
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +0000278class elf::ScriptParser final : public elf::ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000279 typedef void (ScriptParser::*Handler)();
280
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000281public:
Rui Ueyama717677a2016-02-11 21:17:59 +0000282 ScriptParser(BumpPtrAllocator *A, StringRef S, bool B)
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +0000283 : ScriptParserBase(S), Saver(*A), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000284
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +0000285 void run() override;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000286
287private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000288 void addFile(StringRef Path);
289
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000290 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000291 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000292 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000293 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000294 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000295 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000296 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000297 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000298 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000299 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000300 void readSections();
301
George Rimar652852c2016-04-16 10:10:32 +0000302 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000303 void readOutputSectionDescription();
George Rimar481c2ce2016-02-23 07:47:54 +0000304 void readSectionPatterns(StringRef OutSec, bool Keep);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000305
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000306 StringSaver Saver;
George Rimarc3794e52016-02-24 09:21:47 +0000307 const static StringMap<Handler> Cmd;
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000308 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000309};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000310
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000311const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000312 {"ENTRY", &ScriptParser::readEntry},
313 {"EXTERN", &ScriptParser::readExtern},
314 {"GROUP", &ScriptParser::readGroup},
315 {"INCLUDE", &ScriptParser::readInclude},
316 {"INPUT", &ScriptParser::readGroup},
317 {"OUTPUT", &ScriptParser::readOutput},
318 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
319 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
320 {"SEARCH_DIR", &ScriptParser::readSearchDir},
321 {"SECTIONS", &ScriptParser::readSections},
322 {";", &ScriptParser::readNothing}};
323
Rui Ueyama717677a2016-02-11 21:17:59 +0000324void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000325 while (!atEOF()) {
326 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000327 if (Handler Fn = Cmd.lookup(Tok))
328 (this->*Fn)();
329 else
George Rimar57610422016-03-11 14:43:02 +0000330 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000331 }
332}
333
Rui Ueyama717677a2016-02-11 21:17:59 +0000334void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000335 if (IsUnderSysroot && S.startswith("/")) {
336 SmallString<128> Path;
337 (Config->Sysroot + S).toStringRef(Path);
338 if (sys::fs::exists(Path)) {
339 Driver->addFile(Saver.save(Path.str()));
340 return;
341 }
342 }
343
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000344 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000345 Driver->addFile(S);
346 } else if (S.startswith("=")) {
347 if (Config->Sysroot.empty())
348 Driver->addFile(S.substr(1));
349 else
350 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
351 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000352 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000353 } else if (sys::fs::exists(S)) {
354 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000355 } else {
356 std::string Path = findFromSearchPaths(S);
357 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000358 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000359 else
360 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000361 }
362}
363
Rui Ueyama717677a2016-02-11 21:17:59 +0000364void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000365 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000366 bool Orig = Config->AsNeeded;
367 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000368 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000369 StringRef Tok = next();
370 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000371 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000372 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000373 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000374 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000375}
376
Rui Ueyama717677a2016-02-11 21:17:59 +0000377void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000378 // -e <symbol> takes predecence over ENTRY(<symbol>).
379 expect("(");
380 StringRef Tok = next();
381 if (Config->Entry.empty())
382 Config->Entry = Tok;
383 expect(")");
384}
385
Rui Ueyama717677a2016-02-11 21:17:59 +0000386void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000387 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000388 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000389 StringRef Tok = next();
390 if (Tok == ")")
391 return;
392 Config->Undefined.push_back(Tok);
393 }
394}
395
Rui Ueyama717677a2016-02-11 21:17:59 +0000396void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000397 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000398 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000399 StringRef Tok = next();
400 if (Tok == ")")
401 return;
402 if (Tok == "AS_NEEDED") {
403 readAsNeeded();
404 continue;
405 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000406 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000407 }
408}
409
Rui Ueyama717677a2016-02-11 21:17:59 +0000410void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000411 StringRef Tok = next();
412 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000413 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000414 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000415 return;
416 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000417 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000418 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
419 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000420 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000421}
422
Rui Ueyama717677a2016-02-11 21:17:59 +0000423void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000424 // -o <file> takes predecence over OUTPUT(<file>).
425 expect("(");
426 StringRef Tok = next();
427 if (Config->OutputFile.empty())
428 Config->OutputFile = Tok;
429 expect(")");
430}
431
Rui Ueyama717677a2016-02-11 21:17:59 +0000432void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000433 // Error checking only for now.
434 expect("(");
435 next();
436 expect(")");
437}
438
Rui Ueyama717677a2016-02-11 21:17:59 +0000439void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000440 // Error checking only for now.
441 expect("(");
442 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000443 StringRef Tok = next();
444 if (Tok == ")")
445 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000446 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000447 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000448 return;
449 }
Davide Italiano6836c612015-10-12 21:08:41 +0000450 next();
451 expect(",");
452 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000453 expect(")");
454}
455
Rui Ueyama717677a2016-02-11 21:17:59 +0000456void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000457 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000458 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000459 expect(")");
460}
461
Rui Ueyama717677a2016-02-11 21:17:59 +0000462void ScriptParser::readSections() {
George Rimar652852c2016-04-16 10:10:32 +0000463 Script->DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000464 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000465 while (!Error && !skip("}")) {
466 StringRef Tok = peek();
467 if (Tok == ".")
468 readLocationCounterValue();
469 else
470 readOutputSectionDescription();
471 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000472}
473
George Rimar481c2ce2016-02-23 07:47:54 +0000474void ScriptParser::readSectionPatterns(StringRef OutSec, bool Keep) {
475 expect("(");
476 while (!Error && !skip(")"))
477 Script->Sections.emplace_back(OutSec, next(), Keep);
478}
479
George Rimar652852c2016-04-16 10:10:32 +0000480void ScriptParser::readLocationCounterValue() {
481 expect(".");
482 expect("=");
Rui Ueyama9e957a02016-04-18 21:00:45 +0000483 Script->Commands.push_back({ExprKind, {}, ""});
484 SectionsCommand &Cmd = Script->Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000485 while (!Error) {
486 StringRef Tok = next();
487 if (Tok == ";")
488 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000489 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000490 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000491 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000492 error("error in location counter expression");
493}
494
Rui Ueyama717677a2016-02-11 21:17:59 +0000495void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000496 StringRef OutSec = next();
497 Script->SectionOrder.push_back(OutSec);
Rui Ueyama9e957a02016-04-18 21:00:45 +0000498 Script->Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000499 expect(":");
500 expect("{");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000501 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000502 StringRef Tok = next();
503 if (Tok == "*") {
Rui Ueyama3e808972016-02-28 05:09:11 +0000504 readSectionPatterns(OutSec, false);
George Rimar481c2ce2016-02-23 07:47:54 +0000505 } else if (Tok == "KEEP") {
506 expect("(");
507 next(); // Skip *
Rui Ueyama3e808972016-02-28 05:09:11 +0000508 readSectionPatterns(OutSec, true);
George Rimar481c2ce2016-02-23 07:47:54 +0000509 expect(")");
510 } else {
George Rimar777f9632016-03-12 08:31:34 +0000511 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000512 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000513 }
George Rimare2ee72b2016-02-26 14:48:31 +0000514 StringRef Tok = peek();
515 if (Tok.startswith("=")) {
516 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000517 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000518 return;
519 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000520 Tok = Tok.substr(3);
521 Script->Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000522 next();
523 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000524}
525
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000526static bool isUnderSysroot(StringRef Path) {
527 if (Config->Sysroot == "")
528 return false;
529 for (; !Path.empty(); Path = sys::path::parent_path(Path))
530 if (sys::fs::equivalent(Config->Sysroot, Path))
531 return true;
532 return false;
533}
534
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000535// Entry point. The other functions or classes are private to this file.
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000536void LinkerScript::read(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000537 StringRef Path = MB.getBufferIdentifier();
Rui Ueyamaf9de0d62016-02-11 21:38:55 +0000538 ScriptParser(&Alloc, MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000539}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000540
541template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF32LE> *);
542template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF32BE> *);
543template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF64LE> *);
544template StringRef LinkerScript::getOutputSection(InputSectionBase<ELF64BE> *);
545
546template bool LinkerScript::isDiscarded(InputSectionBase<ELF32LE> *);
547template bool LinkerScript::isDiscarded(InputSectionBase<ELF32BE> *);
548template bool LinkerScript::isDiscarded(InputSectionBase<ELF64LE> *);
549template bool LinkerScript::isDiscarded(InputSectionBase<ELF64BE> *);
550
George Rimar481c2ce2016-02-23 07:47:54 +0000551template bool LinkerScript::shouldKeep(InputSectionBase<ELF32LE> *);
552template bool LinkerScript::shouldKeep(InputSectionBase<ELF32BE> *);
553template bool LinkerScript::shouldKeep(InputSectionBase<ELF64LE> *);
554template bool LinkerScript::shouldKeep(InputSectionBase<ELF64BE> *);
George Rimar652852c2016-04-16 10:10:32 +0000555
556template void
557LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF32LE> *> &);
558template void
559LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF32BE> *> &);
560template void
561LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF64LE> *> &);
562template void
563LinkerScript::assignAddresses(std::vector<OutputSectionBase<ELF64BE> *> &);