blob: 380fd47c998c3cfbe9f0dde0d87ea0313f317b24 [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"
Eugene Leviantbbe38602016-07-19 09:25:43 +000026#include "Writer.h"
Rui Ueyama960504b2016-04-19 18:58:11 +000027#include "llvm/ADT/StringSwitch.h"
George Rimar652852c2016-04-16 10:10:32 +000028#include "llvm/Support/ELF.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000029#include "llvm/Support/FileSystem.h"
30#include "llvm/Support/MemoryBuffer.h"
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +000031#include "llvm/Support/Path.h"
Rui Ueyamaa47ee682015-10-11 01:53:04 +000032#include "llvm/Support/StringSaver.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000033
34using namespace llvm;
George Rimar652852c2016-04-16 10:10:32 +000035using namespace llvm::ELF;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000036using namespace llvm::object;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000037using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000038using namespace lld::elf;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000039
Rui Ueyama07320e42016-04-20 20:13:41 +000040ScriptConfiguration *elf::ScriptConfig;
Rui Ueyama717677a2016-02-11 21:17:59 +000041
George Rimar076fe152016-07-21 06:43:01 +000042bool SymbolAssignment::classof(const BaseCommand *C) {
43 return C->Kind == AssignmentKind;
44}
45
46bool OutputSectionCommand::classof(const BaseCommand *C) {
47 return C->Kind == OutputSectionKind;
48}
49
George Rimareea31142016-07-21 14:26:59 +000050bool InputSectionDescription::classof(const BaseCommand *C) {
51 return C->Kind == InputSectionKind;
52}
53
Rui Ueyama9c1112d2016-04-23 00:04:03 +000054// This is an operator-precedence parser to parse and evaluate
55// a linker script expression. For each linker script arithmetic
56// expression (e.g. ". = . + 0x1000"), a new instance of ExprParser
57// is created and ran.
58namespace {
59class ExprParser : public ScriptParserBase {
60public:
61 ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot)
62 : ScriptParserBase(Tokens), Dot(Dot) {}
63
64 uint64_t run();
65
66private:
67 uint64_t parsePrimary();
68 uint64_t parseTernary(uint64_t Cond);
69 uint64_t apply(StringRef Op, uint64_t L, uint64_t R);
70 uint64_t parseExpr1(uint64_t Lhs, int MinPrec);
71 uint64_t parseExpr();
72
73 uint64_t Dot;
74};
75}
76
Rui Ueyama960504b2016-04-19 18:58:11 +000077static int precedence(StringRef Op) {
78 return StringSwitch<int>(Op)
79 .Case("*", 4)
George Rimarab939062016-04-25 08:14:41 +000080 .Case("/", 4)
81 .Case("+", 3)
82 .Case("-", 3)
83 .Case("<", 2)
84 .Case(">", 2)
85 .Case(">=", 2)
86 .Case("<=", 2)
87 .Case("==", 2)
88 .Case("!=", 2)
Rui Ueyama960504b2016-04-19 18:58:11 +000089 .Case("&", 1)
90 .Default(-1);
91}
92
Rui Ueyama9c1112d2016-04-23 00:04:03 +000093static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) {
94 return ExprParser(Tokens, Dot).run();
Rui Ueyama960504b2016-04-19 18:58:11 +000095}
96
Rui Ueyama9c1112d2016-04-23 00:04:03 +000097uint64_t ExprParser::run() {
98 uint64_t V = parseExpr();
99 if (!atEOF() && !Error)
100 setError("stray token: " + peek());
101 return V;
Rui Ueyama60118112016-04-20 20:54:13 +0000102}
103
Rui Ueyama960504b2016-04-19 18:58:11 +0000104// This is a part of the operator-precedence parser to evaluate
105// arithmetic expressions in SECTIONS command. This function evaluates an
Rui Ueyamae29a9752016-04-22 21:02:27 +0000106// integer literal, a parenthesized expression, the ALIGN function,
107// or the special variable ".".
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000108uint64_t ExprParser::parsePrimary() {
109 StringRef Tok = next();
Rui Ueyama960504b2016-04-19 18:58:11 +0000110 if (Tok == ".")
111 return Dot;
112 if (Tok == "(") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000113 uint64_t V = parseExpr();
114 expect(")");
Rui Ueyama960504b2016-04-19 18:58:11 +0000115 return V;
116 }
George Rimardffc1412016-04-22 11:40:53 +0000117 if (Tok == "ALIGN") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000118 expect("(");
119 uint64_t V = parseExpr();
120 expect(")");
George Rimardffc1412016-04-22 11:40:53 +0000121 return alignTo(Dot, V);
122 }
Rui Ueyama5fa60982016-04-22 21:05:04 +0000123 uint64_t V = 0;
124 if (Tok.getAsInteger(0, V))
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000125 setError("malformed number: " + Tok);
Rui Ueyama5fa60982016-04-22 21:05:04 +0000126 return V;
Rui Ueyama960504b2016-04-19 18:58:11 +0000127}
128
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000129uint64_t ExprParser::parseTernary(uint64_t Cond) {
130 next();
131 uint64_t V = parseExpr();
132 expect(":");
133 uint64_t W = parseExpr();
George Rimarfba45c42016-04-22 11:28:54 +0000134 return Cond ? V : W;
135}
136
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000137uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000138 if (Op == "*")
139 return L * R;
140 if (Op == "/") {
141 if (R == 0) {
142 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000143 return 0;
144 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000145 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000146 }
George Rimarab939062016-04-25 08:14:41 +0000147 if (Op == "+")
148 return L + R;
149 if (Op == "-")
150 return L - R;
151 if (Op == "<")
152 return L < R;
153 if (Op == ">")
154 return L > R;
155 if (Op == ">=")
156 return L >= R;
157 if (Op == "<=")
158 return L <= R;
159 if (Op == "==")
160 return L == R;
161 if (Op == "!=")
162 return L != R;
Rui Ueyama960504b2016-04-19 18:58:11 +0000163 if (Op == "&")
164 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000165 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000166}
167
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000168// This is a part of the operator-precedence parser.
169// This function assumes that the remaining token stream starts
170// with an operator.
171uint64_t ExprParser::parseExpr1(uint64_t Lhs, int MinPrec) {
172 while (!atEOF()) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000173 // Read an operator and an expression.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000174 StringRef Op1 = peek();
George Rimarfba45c42016-04-22 11:28:54 +0000175 if (Op1 == "?")
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000176 return parseTernary(Lhs);
Rui Ueyama960504b2016-04-19 18:58:11 +0000177 if (precedence(Op1) < MinPrec)
178 return Lhs;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000179 next();
180 uint64_t Rhs = parsePrimary();
Rui Ueyama960504b2016-04-19 18:58:11 +0000181
182 // Evaluate the remaining part of the expression first if the
183 // next operator has greater precedence than the previous one.
184 // For example, if we have read "+" and "3", and if the next
185 // operator is "*", then we'll evaluate 3 * ... part first.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000186 while (!atEOF()) {
187 StringRef Op2 = peek();
Rui Ueyama960504b2016-04-19 18:58:11 +0000188 if (precedence(Op2) <= precedence(Op1))
189 break;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000190 Rhs = parseExpr1(Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000191 }
192
193 Lhs = apply(Op1, Lhs, Rhs);
194 }
195 return Lhs;
196}
197
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000198// Reads and evaluates an arithmetic expression.
199uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); }
George Rimar652852c2016-04-16 10:10:32 +0000200
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000201template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000202bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
George Rimareea31142016-07-21 14:26:59 +0000203 return !S || !S->Live;
Rui Ueyama717677a2016-02-11 21:17:59 +0000204}
205
Rui Ueyama07320e42016-04-20 20:13:41 +0000206template <class ELFT>
207bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000208 for (StringRef Pat : Opt.KeptSections)
Rui Ueyama722830a2016-06-29 05:32:09 +0000209 if (globMatch(Pat, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000210 return true;
211 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000212}
213
George Rimareea31142016-07-21 14:26:59 +0000214static bool match(StringRef Pattern, ArrayRef<StringRef> Arr) {
215 for (StringRef S : Arr)
216 if (globMatch(S, Pattern))
217 return true;
218 return false;
219}
220
George Rimar652852c2016-04-16 10:10:32 +0000221template <class ELFT>
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000222std::vector<OutputSectionBase<ELFT> *>
Eugene Leviante63d81b2016-07-20 14:43:20 +0000223LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
George Rimareea31142016-07-21 14:26:59 +0000224 typedef const std::unique_ptr<ObjectFile<ELFT>> ObjectFile;
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000225 std::vector<OutputSectionBase<ELFT> *> Result;
226
Eugene Leviante63d81b2016-07-20 14:43:20 +0000227 // Add input section to output section. If there is no output section yet,
228 // then create it and add to output section list.
229 auto AddInputSec = [&](InputSectionBase<ELFT> *C, StringRef Name) {
230 OutputSectionBase<ELFT> *Sec;
231 bool IsNew;
232 std::tie(Sec, IsNew) = Factory.create(C, Name);
233 if (IsNew)
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000234 Result.push_back(Sec);
Eugene Leviante63d81b2016-07-20 14:43:20 +0000235 Sec->addSection(C);
236 };
237
238 // Select input sections matching rule and add them to corresponding
239 // output section. Section rules are processed in order they're listed
240 // in script, so correct input section order is maintained by design.
George Rimareea31142016-07-21 14:26:59 +0000241 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
242 auto *OutCmd = dyn_cast<OutputSectionCommand>(Base.get());
243 if (!OutCmd)
244 continue;
245
246 for (const std::unique_ptr<BaseCommand> &Cmd : OutCmd->Commands) {
247 auto *InCmd = dyn_cast<InputSectionDescription>(Cmd.get());
248 if (!InCmd)
249 continue;
250
251 for (ObjectFile &F : Symtab<ELFT>::X->getObjectFiles()) {
252 for (InputSectionBase<ELFT> *S : F->getSections()) {
253 if (isDiscarded(S) || S->OutSec)
254 continue;
255
256 if (match(S->getSectionName(), InCmd->Patterns)) {
257 if (OutCmd->Name == "/DISCARD/")
258 S->Live = false;
259 else
260 AddInputSec(S, OutCmd->Name);
261 }
262 }
263 }
264 }
265 }
Eugene Leviante63d81b2016-07-20 14:43:20 +0000266
267 // Add all other input sections, which are not listed in script.
George Rimareea31142016-07-21 14:26:59 +0000268 for (ObjectFile &F : Symtab<ELFT>::X->getObjectFiles())
Reid Kleckner3c944ec2016-07-21 18:39:28 +0000269 for (InputSectionBase<ELFT> *S : F->getSections()) {
Eugene Leviante63d81b2016-07-20 14:43:20 +0000270 if (!isDiscarded(S)) {
271 if (!S->OutSec)
272 AddInputSec(S, getOutputSectionName(S));
273 } else
274 reportDiscarded(S, F);
Reid Kleckner3c944ec2016-07-21 18:39:28 +0000275 }
Eugene Leviante63d81b2016-07-20 14:43:20 +0000276
277 return Result;
278}
279
280template <class ELFT>
George Rimar10e576e2016-07-21 16:07:40 +0000281void LinkerScript<ELFT>::dispatchAssignment(SymbolAssignment *Cmd) {
282 uint64_t Val = evalExpr(Cmd->Expr, Dot);
283 if (Cmd->Name == ".") {
284 Dot = Val;
285 } else {
286 auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd->Name));
287 D->Value = Val;
288 }
289}
290
291template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000292void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000293 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000294 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000295 // are not explicitly placed into the output file by the linker script.
296 // We place orphan sections at end of file.
297 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000298 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000299 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000300 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000301 if (getSectionIndex(Name) == INT_MAX)
George Rimar076fe152016-07-21 06:43:01 +0000302 Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
George Rimar652852c2016-04-16 10:10:32 +0000303 }
George Rimar652852c2016-04-16 10:10:32 +0000304
Rui Ueyama7c18c282016-04-18 21:00:40 +0000305 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyama52c4e172016-07-01 10:42:25 +0000306 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000307 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
George Rimar652852c2016-04-16 10:10:32 +0000308 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000309
George Rimar076fe152016-07-21 06:43:01 +0000310 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
311 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
George Rimar10e576e2016-07-21 16:07:40 +0000312 dispatchAssignment(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000313 continue;
314 }
315
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000316 // Find all the sections with required name. There can be more than
George Rimar6ad330a2016-07-19 07:39:07 +0000317 // one section with such name, if the alignment, flags or type
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000318 // attribute differs.
George Rimar076fe152016-07-21 06:43:01 +0000319 auto *Cmd = cast<OutputSectionCommand>(Base.get());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000320 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar076fe152016-07-21 06:43:01 +0000321 if (Sec->getName() != Cmd->Name)
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000322 continue;
George Rimar652852c2016-04-16 10:10:32 +0000323
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000324 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
325 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama424b4082016-06-17 01:18:46 +0000326 TVA = alignTo(TVA, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000327 Sec->setVA(TVA);
328 ThreadBssOffset = TVA - Dot + Sec->getSize();
329 continue;
330 }
George Rimar652852c2016-04-16 10:10:32 +0000331
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000332 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama424b4082016-06-17 01:18:46 +0000333 Dot = alignTo(Dot, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000334 Sec->setVA(Dot);
Rui Ueyama52c4e172016-07-01 10:42:25 +0000335 MinVA = std::min(MinVA, Dot);
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000336 Dot += Sec->getSize();
337 continue;
338 }
George Rimar652852c2016-04-16 10:10:32 +0000339 }
340 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000341
Rafael Espindola64c32d62016-07-07 14:28:47 +0000342 // ELF and Program headers need to be right before the first section in
George Rimarb91e7112016-07-19 07:42:07 +0000343 // memory. Set their addresses accordingly.
Eugene Leviant467c4d52016-07-01 10:27:36 +0000344 MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
345 Out<ELFT>::ProgramHeaders->getSize(),
346 Target->PageSize);
347 Out<ELFT>::ElfHeader->setVA(MinVA);
348 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000349}
350
Rui Ueyama07320e42016-04-20 20:13:41 +0000351template <class ELFT>
Rafael Espindola74df5c72016-07-19 12:33:46 +0000352std::vector<PhdrEntry<ELFT>>
Eugene Leviantbbe38602016-07-19 09:25:43 +0000353LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) {
354 int TlsNum = -1;
355 int NoteNum = -1;
356 int RelroNum = -1;
357 Phdr *Load = nullptr;
358 uintX_t Flags = PF_R;
359 std::vector<Phdr> Phdrs;
360
361 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Eugene Leviant865bf862016-07-21 10:43:25 +0000362 Phdrs.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000363 Phdr &Added = Phdrs.back();
364
365 if (Cmd.HasFilehdr)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000366 Added.add(Out<ELFT>::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000367 if (Cmd.HasPhdrs)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000368 Added.add(Out<ELFT>::ProgramHeaders);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000369
370 switch (Cmd.Type) {
371 case PT_INTERP:
Rui Ueyamafd03cfd2016-07-21 11:01:23 +0000372 if (Out<ELFT>::Interp)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000373 Added.add(Out<ELFT>::Interp);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000374 break;
375 case PT_DYNAMIC:
376 if (isOutputDynamic<ELFT>()) {
377 Added.H.p_flags = toPhdrFlags(Out<ELFT>::Dynamic->getFlags());
Rui Ueyama18f084f2016-07-20 19:36:41 +0000378 Added.add(Out<ELFT>::Dynamic);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000379 }
380 break;
381 case PT_TLS:
382 TlsNum = Phdrs.size() - 1;
383 break;
384 case PT_NOTE:
385 NoteNum = Phdrs.size() - 1;
386 break;
387 case PT_GNU_RELRO:
388 RelroNum = Phdrs.size() - 1;
389 break;
390 case PT_GNU_EH_FRAME:
391 if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) {
392 Added.H.p_flags = toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags());
Rui Ueyama18f084f2016-07-20 19:36:41 +0000393 Added.add(Out<ELFT>::EhFrameHdr);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000394 }
395 break;
396 }
397 }
398
399 for (OutputSectionBase<ELFT> *Sec : Sections) {
400 if (!(Sec->getFlags() & SHF_ALLOC))
401 break;
402
403 if (TlsNum != -1 && (Sec->getFlags() & SHF_TLS))
Rui Ueyama18f084f2016-07-20 19:36:41 +0000404 Phdrs[TlsNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000405
406 if (!needsPtLoad<ELFT>(Sec))
407 continue;
408
409 const std::vector<size_t> &PhdrIds =
410 getPhdrIndicesForSection(Sec->getName());
411 if (!PhdrIds.empty()) {
412 // Assign headers specified by linker script
413 for (size_t Id : PhdrIds) {
Rui Ueyama18f084f2016-07-20 19:36:41 +0000414 Phdrs[Id].add(Sec);
Eugene Leviant865bf862016-07-21 10:43:25 +0000415 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
416 Phdrs[Id].H.p_flags |= toPhdrFlags(Sec->getFlags());
Eugene Leviantbbe38602016-07-19 09:25:43 +0000417 }
418 } else {
419 // If we have no load segment or flags've changed then we want new load
420 // segment.
421 uintX_t NewFlags = toPhdrFlags(Sec->getFlags());
422 if (Load == nullptr || Flags != NewFlags) {
423 Load = &*Phdrs.emplace(Phdrs.end(), PT_LOAD, NewFlags);
424 Flags = NewFlags;
425 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000426 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000427 }
428
429 if (RelroNum != -1 && isRelroSection(Sec))
Rui Ueyama18f084f2016-07-20 19:36:41 +0000430 Phdrs[RelroNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000431 if (NoteNum != -1 && Sec->getType() == SHT_NOTE)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000432 Phdrs[NoteNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000433 }
434 return Phdrs;
435}
436
437template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000438ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
George Rimarf6c3cce2016-07-21 07:48:54 +0000439 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
440 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
441 if (Cmd->Name == Name)
442 return Cmd->Filler;
443 return {};
George Rimare2ee72b2016-02-26 14:48:31 +0000444}
445
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000446// Returns the index of the given section name in linker script
447// SECTIONS commands. Sections are laid out as the same order as they
448// were in the script. If a given name did not appear in the script,
449// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000450template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000451 auto Begin = Opt.Commands.begin();
452 auto End = Opt.Commands.end();
George Rimar076fe152016-07-21 06:43:01 +0000453 auto I =
454 std::find_if(Begin, End, [&](const std::unique_ptr<BaseCommand> &Base) {
455 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
456 if (Cmd->Name == Name)
457 return true;
458 return false;
459 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000460 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000461}
462
463// A compartor to sort output sections. Returns -1 or 1 if
464// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000465template <class ELFT>
466int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000467 int I = getSectionIndex(A);
468 int J = getSectionIndex(B);
469 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000470 return 0;
471 return I < J ? -1 : 1;
472}
473
George Rimar076fe152016-07-21 06:43:01 +0000474template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() {
475 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
476 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()))
477 if (Cmd->Name != "." && Symtab<ELFT>::X->find(Cmd->Name) == nullptr)
478 Symtab<ELFT>::X->addAbsolute(Cmd->Name, STV_DEFAULT);
Eugene Levianteda81a12016-07-12 06:39:48 +0000479}
480
Eugene Leviantbbe38602016-07-19 09:25:43 +0000481template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
482 return !Opt.PhdrsCommands.empty();
483}
484
485// Returns indices of ELF headers containing specific section, identified
486// by Name. Each index is a zero based number of ELF header listed within
487// PHDRS {} script block.
488template <class ELFT>
489std::vector<size_t>
490LinkerScript<ELFT>::getPhdrIndicesForSection(StringRef Name) {
George Rimar076fe152016-07-21 06:43:01 +0000491 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
492 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
493 if (!Cmd || Cmd->Name != Name)
George Rimar31d842f2016-07-20 16:43:03 +0000494 continue;
495
496 std::vector<size_t> Indices;
George Rimar076fe152016-07-21 06:43:01 +0000497 for (StringRef PhdrName : Cmd->Phdrs) {
George Rimar31d842f2016-07-20 16:43:03 +0000498 auto ItPhdr =
499 std::find_if(Opt.PhdrsCommands.rbegin(), Opt.PhdrsCommands.rend(),
George Rimar076fe152016-07-21 06:43:01 +0000500 [&](PhdrsCommand &P) { return P.Name == PhdrName; });
Eugene Leviantbbe38602016-07-19 09:25:43 +0000501 if (ItPhdr == Opt.PhdrsCommands.rend())
502 error("section header '" + PhdrName + "' is not listed in PHDRS");
503 else
504 Indices.push_back(std::distance(ItPhdr, Opt.PhdrsCommands.rend()) - 1);
505 }
George Rimar31d842f2016-07-20 16:43:03 +0000506 return Indices;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000507 }
George Rimar31d842f2016-07-20 16:43:03 +0000508 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000509}
510
Rui Ueyama07320e42016-04-20 20:13:41 +0000511class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000512 typedef void (ScriptParser::*Handler)();
513
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000514public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000515 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000516
Rui Ueyama4a465392016-04-22 22:59:24 +0000517 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000518
519private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000520 void addFile(StringRef Path);
521
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000522 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000523 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000524 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000525 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000526 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000527 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000528 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000529 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000530 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000531 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000532 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000533 void readSections();
534
George Rimar652852c2016-04-16 10:10:32 +0000535 void readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000536 void readOutputSectionDescription(StringRef OutSec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000537 std::vector<StringRef> readOutputSectionPhdrs();
538 unsigned readPhdrType();
Eugene Levianteda81a12016-07-12 06:39:48 +0000539 void readSymbolAssignment(StringRef Name);
540 std::vector<StringRef> readSectionsCommandExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000541
George Rimarc3794e52016-02-24 09:21:47 +0000542 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000543 ScriptConfiguration &Opt = *ScriptConfig;
544 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000545 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000546};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000547
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000548const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000549 {"ENTRY", &ScriptParser::readEntry},
550 {"EXTERN", &ScriptParser::readExtern},
551 {"GROUP", &ScriptParser::readGroup},
552 {"INCLUDE", &ScriptParser::readInclude},
553 {"INPUT", &ScriptParser::readGroup},
554 {"OUTPUT", &ScriptParser::readOutput},
555 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
556 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
Eugene Leviantbbe38602016-07-19 09:25:43 +0000557 {"PHDRS", &ScriptParser::readPhdrs},
George Rimarc3794e52016-02-24 09:21:47 +0000558 {"SEARCH_DIR", &ScriptParser::readSearchDir},
559 {"SECTIONS", &ScriptParser::readSections},
560 {";", &ScriptParser::readNothing}};
561
Rui Ueyama717677a2016-02-11 21:17:59 +0000562void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000563 while (!atEOF()) {
564 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000565 if (Handler Fn = Cmd.lookup(Tok))
566 (this->*Fn)();
567 else
George Rimar57610422016-03-11 14:43:02 +0000568 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000569 }
570}
571
Rui Ueyama717677a2016-02-11 21:17:59 +0000572void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000573 if (IsUnderSysroot && S.startswith("/")) {
574 SmallString<128> Path;
575 (Config->Sysroot + S).toStringRef(Path);
576 if (sys::fs::exists(Path)) {
577 Driver->addFile(Saver.save(Path.str()));
578 return;
579 }
580 }
581
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000582 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000583 Driver->addFile(S);
584 } else if (S.startswith("=")) {
585 if (Config->Sysroot.empty())
586 Driver->addFile(S.substr(1));
587 else
588 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
589 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000590 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000591 } else if (sys::fs::exists(S)) {
592 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000593 } else {
594 std::string Path = findFromSearchPaths(S);
595 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000596 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000597 else
598 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000599 }
600}
601
Rui Ueyama717677a2016-02-11 21:17:59 +0000602void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000603 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000604 bool Orig = Config->AsNeeded;
605 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000606 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000607 StringRef Tok = next();
608 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000609 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000610 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000611 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000612 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000613}
614
Rui Ueyama717677a2016-02-11 21:17:59 +0000615void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000616 // -e <symbol> takes predecence over ENTRY(<symbol>).
617 expect("(");
618 StringRef Tok = next();
619 if (Config->Entry.empty())
620 Config->Entry = Tok;
621 expect(")");
622}
623
Rui Ueyama717677a2016-02-11 21:17:59 +0000624void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000625 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000626 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000627 StringRef Tok = next();
628 if (Tok == ")")
629 return;
630 Config->Undefined.push_back(Tok);
631 }
632}
633
Rui Ueyama717677a2016-02-11 21:17:59 +0000634void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000635 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000636 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000637 StringRef Tok = next();
638 if (Tok == ")")
639 return;
640 if (Tok == "AS_NEEDED") {
641 readAsNeeded();
642 continue;
643 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000644 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000645 }
646}
647
Rui Ueyama717677a2016-02-11 21:17:59 +0000648void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000649 StringRef Tok = next();
650 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000651 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000652 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000653 return;
654 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000655 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000656 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
657 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000658 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000659}
660
Rui Ueyama717677a2016-02-11 21:17:59 +0000661void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000662 // -o <file> takes predecence over OUTPUT(<file>).
663 expect("(");
664 StringRef Tok = next();
665 if (Config->OutputFile.empty())
666 Config->OutputFile = Tok;
667 expect(")");
668}
669
Rui Ueyama717677a2016-02-11 21:17:59 +0000670void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000671 // Error checking only for now.
672 expect("(");
673 next();
674 expect(")");
675}
676
Rui Ueyama717677a2016-02-11 21:17:59 +0000677void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000678 // Error checking only for now.
679 expect("(");
680 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000681 StringRef Tok = next();
682 if (Tok == ")")
683 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000684 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000685 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000686 return;
687 }
Davide Italiano6836c612015-10-12 21:08:41 +0000688 next();
689 expect(",");
690 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000691 expect(")");
692}
693
Eugene Leviantbbe38602016-07-19 09:25:43 +0000694void ScriptParser::readPhdrs() {
695 expect("{");
696 while (!Error && !skip("}")) {
697 StringRef Tok = next();
Eugene Leviant865bf862016-07-21 10:43:25 +0000698 Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX});
Eugene Leviantbbe38602016-07-19 09:25:43 +0000699 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
700
701 PhdrCmd.Type = readPhdrType();
702 do {
703 Tok = next();
704 if (Tok == ";")
705 break;
706 if (Tok == "FILEHDR")
707 PhdrCmd.HasFilehdr = true;
708 else if (Tok == "PHDRS")
709 PhdrCmd.HasPhdrs = true;
Eugene Leviant865bf862016-07-21 10:43:25 +0000710 else if (Tok == "FLAGS") {
711 expect("(");
712 next().getAsInteger(0, PhdrCmd.Flags);
713 expect(")");
714 } else
Eugene Leviantbbe38602016-07-19 09:25:43 +0000715 setError("unexpected header attribute: " + Tok);
716 } while (!Error);
717 }
718}
719
Rui Ueyama717677a2016-02-11 21:17:59 +0000720void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000721 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000722 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000723 expect(")");
724}
725
Rui Ueyama717677a2016-02-11 21:17:59 +0000726void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000727 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000728 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000729 while (!Error && !skip("}")) {
730 StringRef Tok = peek();
Eugene Levianteda81a12016-07-12 06:39:48 +0000731 if (Tok == ".") {
George Rimar652852c2016-04-16 10:10:32 +0000732 readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000733 continue;
734 }
735 next();
736 if (peek() == "=")
737 readSymbolAssignment(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000738 else
Eugene Levianteda81a12016-07-12 06:39:48 +0000739 readOutputSectionDescription(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000740 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000741}
742
George Rimar652852c2016-04-16 10:10:32 +0000743void ScriptParser::readLocationCounterValue() {
744 expect(".");
745 expect("=");
Eugene Levianteda81a12016-07-12 06:39:48 +0000746 std::vector<StringRef> Expr = readSectionsCommandExpr();
747 if (Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000748 error("error in location counter expression");
Eugene Levianteda81a12016-07-12 06:39:48 +0000749 else
George Rimar076fe152016-07-21 06:43:01 +0000750 Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(".", Expr));
George Rimar652852c2016-04-16 10:10:32 +0000751}
752
Eugene Levianteda81a12016-07-12 06:39:48 +0000753void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +0000754 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
755 Opt.Commands.emplace_back(Cmd);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000756 expect(":");
757 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000758
Rui Ueyama025d59b2016-02-02 20:27:59 +0000759 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000760 StringRef Tok = next();
761 if (Tok == "*") {
George Rimareea31142016-07-21 14:26:59 +0000762 auto *InCmd = new InputSectionDescription();
763 Cmd->Commands.emplace_back(InCmd);
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000764 expect("(");
765 while (!Error && !skip(")"))
George Rimareea31142016-07-21 14:26:59 +0000766 InCmd->Patterns.push_back(next());
George Rimar481c2ce2016-02-23 07:47:54 +0000767 } else if (Tok == "KEEP") {
768 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000769 expect("*");
770 expect("(");
George Rimareea31142016-07-21 14:26:59 +0000771 auto *InCmd = new InputSectionDescription();
772 Cmd->Commands.emplace_back(InCmd);
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000773 while (!Error && !skip(")")) {
George Rimareea31142016-07-21 14:26:59 +0000774 Opt.KeptSections.push_back(peek());
775 InCmd->Patterns.push_back(next());
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000776 }
George Rimar481c2ce2016-02-23 07:47:54 +0000777 expect(")");
778 } else {
George Rimar777f9632016-03-12 08:31:34 +0000779 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000780 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000781 }
George Rimar076fe152016-07-21 06:43:01 +0000782 Cmd->Phdrs = readOutputSectionPhdrs();
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000783
George Rimare2ee72b2016-02-26 14:48:31 +0000784 StringRef Tok = peek();
785 if (Tok.startswith("=")) {
786 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000787 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000788 return;
789 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000790 Tok = Tok.substr(3);
George Rimarf6c3cce2016-07-21 07:48:54 +0000791 Cmd->Filler = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000792 next();
793 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000794}
795
Eugene Levianteda81a12016-07-12 06:39:48 +0000796void ScriptParser::readSymbolAssignment(StringRef Name) {
797 expect("=");
798 std::vector<StringRef> Expr = readSectionsCommandExpr();
799 if (Expr.empty())
800 error("error in symbol assignment expression");
801 else
George Rimar076fe152016-07-21 06:43:01 +0000802 Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(Name, Expr));
Eugene Levianteda81a12016-07-12 06:39:48 +0000803}
804
805std::vector<StringRef> ScriptParser::readSectionsCommandExpr() {
806 std::vector<StringRef> Expr;
807 while (!Error) {
808 StringRef Tok = next();
809 if (Tok == ";")
810 break;
811 Expr.push_back(Tok);
812 }
813 return Expr;
814}
815
Eugene Leviantbbe38602016-07-19 09:25:43 +0000816std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
817 std::vector<StringRef> Phdrs;
818 while (!Error && peek().startswith(":")) {
819 StringRef Tok = next();
820 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
821 if (Tok.empty()) {
822 setError("section header name is empty");
823 break;
824 }
Rui Ueyama047404f2016-07-20 19:36:36 +0000825 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000826 }
827 return Phdrs;
828}
829
830unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000831 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +0000832 unsigned Ret = StringSwitch<unsigned>(Tok)
833 .Case("PT_NULL", PT_NULL)
834 .Case("PT_LOAD", PT_LOAD)
835 .Case("PT_DYNAMIC", PT_DYNAMIC)
836 .Case("PT_INTERP", PT_INTERP)
837 .Case("PT_NOTE", PT_NOTE)
838 .Case("PT_SHLIB", PT_SHLIB)
839 .Case("PT_PHDR", PT_PHDR)
840 .Case("PT_TLS", PT_TLS)
841 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
842 .Case("PT_GNU_STACK", PT_GNU_STACK)
843 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
844 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000845
Rui Ueyamab0f6c592016-07-20 19:36:38 +0000846 if (Ret == (unsigned)-1) {
847 setError("invalid program header type: " + Tok);
848 return PT_NULL;
849 }
850 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000851}
852
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000853static bool isUnderSysroot(StringRef Path) {
854 if (Config->Sysroot == "")
855 return false;
856 for (; !Path.empty(); Path = sys::path::parent_path(Path))
857 if (sys::fs::equivalent(Config->Sysroot, Path))
858 return true;
859 return false;
860}
861
Rui Ueyama07320e42016-04-20 20:13:41 +0000862// Entry point.
863void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000864 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000865 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000866}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000867
Rui Ueyama07320e42016-04-20 20:13:41 +0000868template class elf::LinkerScript<ELF32LE>;
869template class elf::LinkerScript<ELF32BE>;
870template class elf::LinkerScript<ELF64LE>;
871template class elf::LinkerScript<ELF64BE>;