blob: f25d6d400f9f3455631e1e41ef800d06bb29560a [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 Ueyama93c9af42016-06-29 08:01:32 +000022#include "Strings.h"
Eugene Levianteda81a12016-07-12 06:39:48 +000023#include "Symbols.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000024#include "SymbolTable.h"
Eugene Leviant467c4d52016-07-01 10:27:36 +000025#include "Target.h"
Rui Ueyama960504b2016-04-19 18:58:11 +000026#include "llvm/ADT/StringSwitch.h"
George Rimar652852c2016-04-16 10:10:32 +000027#include "llvm/Support/ELF.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000028#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/MemoryBuffer.h"
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +000030#include "llvm/Support/Path.h"
Rui Ueyamaa47ee682015-10-11 01:53:04 +000031#include "llvm/Support/StringSaver.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000032
33using namespace llvm;
George Rimar652852c2016-04-16 10:10:32 +000034using namespace llvm::ELF;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000035using namespace llvm::object;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000036using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000037using namespace lld::elf;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000038
Rui Ueyama07320e42016-04-20 20:13:41 +000039ScriptConfiguration *elf::ScriptConfig;
Rui Ueyama717677a2016-02-11 21:17:59 +000040
Rui Ueyama9c1112d2016-04-23 00:04:03 +000041// This is an operator-precedence parser to parse and evaluate
42// a linker script expression. For each linker script arithmetic
43// expression (e.g. ". = . + 0x1000"), a new instance of ExprParser
44// is created and ran.
45namespace {
46class ExprParser : public ScriptParserBase {
47public:
48 ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot)
49 : ScriptParserBase(Tokens), Dot(Dot) {}
50
51 uint64_t run();
52
53private:
54 uint64_t parsePrimary();
55 uint64_t parseTernary(uint64_t Cond);
56 uint64_t apply(StringRef Op, uint64_t L, uint64_t R);
57 uint64_t parseExpr1(uint64_t Lhs, int MinPrec);
58 uint64_t parseExpr();
59
60 uint64_t Dot;
61};
62}
63
Rui Ueyama960504b2016-04-19 18:58:11 +000064static int precedence(StringRef Op) {
65 return StringSwitch<int>(Op)
66 .Case("*", 4)
George Rimarab939062016-04-25 08:14:41 +000067 .Case("/", 4)
68 .Case("+", 3)
69 .Case("-", 3)
70 .Case("<", 2)
71 .Case(">", 2)
72 .Case(">=", 2)
73 .Case("<=", 2)
74 .Case("==", 2)
75 .Case("!=", 2)
Rui Ueyama960504b2016-04-19 18:58:11 +000076 .Case("&", 1)
77 .Default(-1);
78}
79
Rui Ueyama9c1112d2016-04-23 00:04:03 +000080static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) {
81 return ExprParser(Tokens, Dot).run();
Rui Ueyama960504b2016-04-19 18:58:11 +000082}
83
Rui Ueyama9c1112d2016-04-23 00:04:03 +000084uint64_t ExprParser::run() {
85 uint64_t V = parseExpr();
86 if (!atEOF() && !Error)
87 setError("stray token: " + peek());
88 return V;
Rui Ueyama60118112016-04-20 20:54:13 +000089}
90
Rui Ueyama960504b2016-04-19 18:58:11 +000091// This is a part of the operator-precedence parser to evaluate
92// arithmetic expressions in SECTIONS command. This function evaluates an
Rui Ueyamae29a9752016-04-22 21:02:27 +000093// integer literal, a parenthesized expression, the ALIGN function,
94// or the special variable ".".
Rui Ueyama9c1112d2016-04-23 00:04:03 +000095uint64_t ExprParser::parsePrimary() {
96 StringRef Tok = next();
Rui Ueyama960504b2016-04-19 18:58:11 +000097 if (Tok == ".")
98 return Dot;
99 if (Tok == "(") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000100 uint64_t V = parseExpr();
101 expect(")");
Rui Ueyama960504b2016-04-19 18:58:11 +0000102 return V;
103 }
George Rimardffc1412016-04-22 11:40:53 +0000104 if (Tok == "ALIGN") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000105 expect("(");
106 uint64_t V = parseExpr();
107 expect(")");
George Rimardffc1412016-04-22 11:40:53 +0000108 return alignTo(Dot, V);
109 }
Rui Ueyama5fa60982016-04-22 21:05:04 +0000110 uint64_t V = 0;
111 if (Tok.getAsInteger(0, V))
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000112 setError("malformed number: " + Tok);
Rui Ueyama5fa60982016-04-22 21:05:04 +0000113 return V;
Rui Ueyama960504b2016-04-19 18:58:11 +0000114}
115
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000116uint64_t ExprParser::parseTernary(uint64_t Cond) {
117 next();
118 uint64_t V = parseExpr();
119 expect(":");
120 uint64_t W = parseExpr();
George Rimarfba45c42016-04-22 11:28:54 +0000121 return Cond ? V : W;
122}
123
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000124uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000125 if (Op == "*")
126 return L * R;
127 if (Op == "/") {
128 if (R == 0) {
129 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000130 return 0;
131 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000132 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000133 }
George Rimarab939062016-04-25 08:14:41 +0000134 if (Op == "+")
135 return L + R;
136 if (Op == "-")
137 return L - R;
138 if (Op == "<")
139 return L < R;
140 if (Op == ">")
141 return L > R;
142 if (Op == ">=")
143 return L >= R;
144 if (Op == "<=")
145 return L <= R;
146 if (Op == "==")
147 return L == R;
148 if (Op == "!=")
149 return L != R;
Rui Ueyama960504b2016-04-19 18:58:11 +0000150 if (Op == "&")
151 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000152 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000153 return 0;
154}
155
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000156// This is a part of the operator-precedence parser.
157// This function assumes that the remaining token stream starts
158// with an operator.
159uint64_t ExprParser::parseExpr1(uint64_t Lhs, int MinPrec) {
160 while (!atEOF()) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000161 // Read an operator and an expression.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000162 StringRef Op1 = peek();
George Rimarfba45c42016-04-22 11:28:54 +0000163 if (Op1 == "?")
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000164 return parseTernary(Lhs);
Rui Ueyama960504b2016-04-19 18:58:11 +0000165 if (precedence(Op1) < MinPrec)
166 return Lhs;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000167 next();
168 uint64_t Rhs = parsePrimary();
Rui Ueyama960504b2016-04-19 18:58:11 +0000169
170 // Evaluate the remaining part of the expression first if the
171 // next operator has greater precedence than the previous one.
172 // For example, if we have read "+" and "3", and if the next
173 // operator is "*", then we'll evaluate 3 * ... part first.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000174 while (!atEOF()) {
175 StringRef Op2 = peek();
Rui Ueyama960504b2016-04-19 18:58:11 +0000176 if (precedence(Op2) <= precedence(Op1))
177 break;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000178 Rhs = parseExpr1(Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000179 }
180
181 Lhs = apply(Op1, Lhs, Rhs);
182 }
183 return Lhs;
184}
185
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000186// Reads and evaluates an arithmetic expression.
187uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); }
George Rimar652852c2016-04-16 10:10:32 +0000188
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000189template <class ELFT>
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000190StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000191 for (SectionRule &R : Opt.Sections)
Rui Ueyama722830a2016-06-29 05:32:09 +0000192 if (globMatch(R.SectionPattern, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000193 return R.Dest;
194 return "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000195}
196
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000197template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000198bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000199 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000200}
201
Rui Ueyama07320e42016-04-20 20:13:41 +0000202template <class ELFT>
203bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000204 for (StringRef Pat : Opt.KeptSections)
Rui Ueyama722830a2016-06-29 05:32:09 +0000205 if (globMatch(Pat, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000206 return true;
207 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000208}
209
George Rimar652852c2016-04-16 10:10:32 +0000210template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000211void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000212 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000213 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000214 // are not explicitly placed into the output file by the linker script.
215 // We place orphan sections at end of file.
216 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000217 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000218 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000219 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000220 if (getSectionIndex(Name) == INT_MAX)
Rui Ueyama07320e42016-04-20 20:13:41 +0000221 Opt.Commands.push_back({SectionKind, {}, Name});
George Rimar652852c2016-04-16 10:10:32 +0000222 }
George Rimar652852c2016-04-16 10:10:32 +0000223
Rui Ueyama7c18c282016-04-18 21:00:40 +0000224 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyama52c4e172016-07-01 10:42:25 +0000225 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000226 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
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) {
Eugene Levianteda81a12016-07-12 06:39:48 +0000230 switch (Cmd.Kind) {
231 case ExprKind:
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000232 Dot = evalExpr(Cmd.Expr, Dot);
George Rimar652852c2016-04-16 10:10:32 +0000233 continue;
Eugene Levianteda81a12016-07-12 06:39:48 +0000234 case SymbolAssignmentKind: {
235 auto *D =
236 cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd.Name));
237 D->Value = evalExpr(Cmd.Expr, Dot);
238 continue;
239 }
240 default:
241 break;
George Rimar652852c2016-04-16 10:10:32 +0000242 }
243
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000244 // Find all the sections with required name. There can be more than
245 // ont section with such name, if the alignment, flags or type
246 // attribute differs.
247 for (OutputSectionBase<ELFT> *Sec : Sections) {
Eugene Levianteda81a12016-07-12 06:39:48 +0000248 if (Sec->getName() != Cmd.Name)
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000249 continue;
George Rimar652852c2016-04-16 10:10:32 +0000250
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000251 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
252 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama424b4082016-06-17 01:18:46 +0000253 TVA = alignTo(TVA, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000254 Sec->setVA(TVA);
255 ThreadBssOffset = TVA - Dot + Sec->getSize();
256 continue;
257 }
George Rimar652852c2016-04-16 10:10:32 +0000258
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000259 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama424b4082016-06-17 01:18:46 +0000260 Dot = alignTo(Dot, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000261 Sec->setVA(Dot);
Rui Ueyama52c4e172016-07-01 10:42:25 +0000262 MinVA = std::min(MinVA, Dot);
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000263 Dot += Sec->getSize();
264 continue;
265 }
George Rimar652852c2016-04-16 10:10:32 +0000266 }
267 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000268
Rafael Espindola64c32d62016-07-07 14:28:47 +0000269 // ELF and Program headers need to be right before the first section in
270 // memory.
Eugene Leviant467c4d52016-07-01 10:27:36 +0000271 // Set their addresses accordingly.
272 MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
273 Out<ELFT>::ProgramHeaders->getSize(),
274 Target->PageSize);
275 Out<ELFT>::ElfHeader->setVA(MinVA);
276 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000277}
278
Rui Ueyama07320e42016-04-20 20:13:41 +0000279template <class ELFT>
280ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
281 auto I = Opt.Filler.find(Name);
282 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000283 return {};
284 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000285}
286
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000287// Returns the index of the given section name in linker script
288// SECTIONS commands. Sections are laid out as the same order as they
289// were in the script. If a given name did not appear in the script,
290// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar71b26e92016-04-21 10:22:02 +0000291template <class ELFT>
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000292int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000293 auto Begin = Opt.Commands.begin();
294 auto End = Opt.Commands.end();
295 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
Eugene Levianteda81a12016-07-12 06:39:48 +0000296 return N.Kind == SectionKind && N.Name == Name;
George Rimar71b26e92016-04-21 10:22:02 +0000297 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000298 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000299}
300
301// A compartor to sort output sections. Returns -1 or 1 if
302// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000303template <class ELFT>
304int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000305 int I = getSectionIndex(A);
306 int J = getSectionIndex(B);
307 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000308 return 0;
309 return I < J ? -1 : 1;
310}
311
Eugene Levianteda81a12016-07-12 06:39:48 +0000312template <class ELFT>
313void LinkerScript<ELFT>::addScriptedSymbols() {
314 for (SectionsCommand &Cmd : Opt.Commands)
315 if (Cmd.Kind == SymbolAssignmentKind)
316 Symtab<ELFT>::X->addAbsolute(Cmd.Name, STV_DEFAULT);
317}
318
Rui Ueyama07320e42016-04-20 20:13:41 +0000319class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000320 typedef void (ScriptParser::*Handler)();
321
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000322public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000323 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000324
Rui Ueyama4a465392016-04-22 22:59:24 +0000325 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000326
327private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000328 void addFile(StringRef Path);
329
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000330 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000331 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000332 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000333 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000334 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000335 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000336 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000337 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000338 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000339 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000340 void readSections();
341
George Rimar652852c2016-04-16 10:10:32 +0000342 void readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000343 void readOutputSectionDescription(StringRef OutSec);
344 void readSymbolAssignment(StringRef Name);
345 std::vector<StringRef> readSectionsCommandExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000346
George Rimarc3794e52016-02-24 09:21:47 +0000347 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000348 ScriptConfiguration &Opt = *ScriptConfig;
349 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000350 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000351};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000352
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000353const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000354 {"ENTRY", &ScriptParser::readEntry},
355 {"EXTERN", &ScriptParser::readExtern},
356 {"GROUP", &ScriptParser::readGroup},
357 {"INCLUDE", &ScriptParser::readInclude},
358 {"INPUT", &ScriptParser::readGroup},
359 {"OUTPUT", &ScriptParser::readOutput},
360 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
361 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
362 {"SEARCH_DIR", &ScriptParser::readSearchDir},
363 {"SECTIONS", &ScriptParser::readSections},
364 {";", &ScriptParser::readNothing}};
365
Rui Ueyama717677a2016-02-11 21:17:59 +0000366void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000367 while (!atEOF()) {
368 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000369 if (Handler Fn = Cmd.lookup(Tok))
370 (this->*Fn)();
371 else
George Rimar57610422016-03-11 14:43:02 +0000372 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000373 }
374}
375
Rui Ueyama717677a2016-02-11 21:17:59 +0000376void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000377 if (IsUnderSysroot && S.startswith("/")) {
378 SmallString<128> Path;
379 (Config->Sysroot + S).toStringRef(Path);
380 if (sys::fs::exists(Path)) {
381 Driver->addFile(Saver.save(Path.str()));
382 return;
383 }
384 }
385
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000386 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000387 Driver->addFile(S);
388 } else if (S.startswith("=")) {
389 if (Config->Sysroot.empty())
390 Driver->addFile(S.substr(1));
391 else
392 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
393 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000394 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000395 } else if (sys::fs::exists(S)) {
396 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000397 } else {
398 std::string Path = findFromSearchPaths(S);
399 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000400 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000401 else
402 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000403 }
404}
405
Rui Ueyama717677a2016-02-11 21:17:59 +0000406void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000407 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000408 bool Orig = Config->AsNeeded;
409 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000410 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000411 StringRef Tok = next();
412 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000413 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000414 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000415 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000416 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000417}
418
Rui Ueyama717677a2016-02-11 21:17:59 +0000419void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000420 // -e <symbol> takes predecence over ENTRY(<symbol>).
421 expect("(");
422 StringRef Tok = next();
423 if (Config->Entry.empty())
424 Config->Entry = Tok;
425 expect(")");
426}
427
Rui Ueyama717677a2016-02-11 21:17:59 +0000428void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000429 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000430 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000431 StringRef Tok = next();
432 if (Tok == ")")
433 return;
434 Config->Undefined.push_back(Tok);
435 }
436}
437
Rui Ueyama717677a2016-02-11 21:17:59 +0000438void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000439 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000440 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000441 StringRef Tok = next();
442 if (Tok == ")")
443 return;
444 if (Tok == "AS_NEEDED") {
445 readAsNeeded();
446 continue;
447 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000448 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000449 }
450}
451
Rui Ueyama717677a2016-02-11 21:17:59 +0000452void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000453 StringRef Tok = next();
454 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000455 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000456 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000457 return;
458 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000459 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000460 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
461 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000462 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000463}
464
Rui Ueyama717677a2016-02-11 21:17:59 +0000465void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000466 // -o <file> takes predecence over OUTPUT(<file>).
467 expect("(");
468 StringRef Tok = next();
469 if (Config->OutputFile.empty())
470 Config->OutputFile = Tok;
471 expect(")");
472}
473
Rui Ueyama717677a2016-02-11 21:17:59 +0000474void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000475 // Error checking only for now.
476 expect("(");
477 next();
478 expect(")");
479}
480
Rui Ueyama717677a2016-02-11 21:17:59 +0000481void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000482 // Error checking only for now.
483 expect("(");
484 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000485 StringRef Tok = next();
486 if (Tok == ")")
487 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000488 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000489 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000490 return;
491 }
Davide Italiano6836c612015-10-12 21:08:41 +0000492 next();
493 expect(",");
494 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000495 expect(")");
496}
497
Rui Ueyama717677a2016-02-11 21:17:59 +0000498void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000499 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000500 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000501 expect(")");
502}
503
Rui Ueyama717677a2016-02-11 21:17:59 +0000504void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000505 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000506 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000507 while (!Error && !skip("}")) {
508 StringRef Tok = peek();
Eugene Levianteda81a12016-07-12 06:39:48 +0000509 if (Tok == ".") {
George Rimar652852c2016-04-16 10:10:32 +0000510 readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000511 continue;
512 }
513 next();
514 if (peek() == "=")
515 readSymbolAssignment(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000516 else
Eugene Levianteda81a12016-07-12 06:39:48 +0000517 readOutputSectionDescription(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000518 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000519}
520
George Rimar652852c2016-04-16 10:10:32 +0000521void ScriptParser::readLocationCounterValue() {
522 expect(".");
523 expect("=");
Eugene Levianteda81a12016-07-12 06:39:48 +0000524 std::vector<StringRef> Expr = readSectionsCommandExpr();
525 if (Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000526 error("error in location counter expression");
Eugene Levianteda81a12016-07-12 06:39:48 +0000527 else
528 Opt.Commands.push_back({ExprKind, std::move(Expr), ""});
George Rimar652852c2016-04-16 10:10:32 +0000529}
530
Eugene Levianteda81a12016-07-12 06:39:48 +0000531void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
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
Eugene Levianteda81a12016-07-12 06:39:48 +0000569void ScriptParser::readSymbolAssignment(StringRef Name) {
570 expect("=");
571 std::vector<StringRef> Expr = readSectionsCommandExpr();
572 if (Expr.empty())
573 error("error in symbol assignment expression");
574 else
575 Opt.Commands.push_back({SymbolAssignmentKind, std::move(Expr), Name});
576}
577
578std::vector<StringRef> ScriptParser::readSectionsCommandExpr() {
579 std::vector<StringRef> Expr;
580 while (!Error) {
581 StringRef Tok = next();
582 if (Tok == ";")
583 break;
584 Expr.push_back(Tok);
585 }
586 return Expr;
587}
588
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000589static bool isUnderSysroot(StringRef Path) {
590 if (Config->Sysroot == "")
591 return false;
592 for (; !Path.empty(); Path = sys::path::parent_path(Path))
593 if (sys::fs::equivalent(Config->Sysroot, Path))
594 return true;
595 return false;
596}
597
Rui Ueyama07320e42016-04-20 20:13:41 +0000598// Entry point.
599void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000600 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000601 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000602}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000603
Rui Ueyama07320e42016-04-20 20:13:41 +0000604template class elf::LinkerScript<ELF32LE>;
605template class elf::LinkerScript<ELF32BE>;
606template class elf::LinkerScript<ELF64LE>;
607template class elf::LinkerScript<ELF64BE>;