blob: 5c9edeaa5a3971a6a02c2283579a9db4987d8f41 [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
Rui Ueyama9c1112d2016-04-23 00:04:03 +000050// This is an operator-precedence parser to parse and evaluate
51// a linker script expression. For each linker script arithmetic
52// expression (e.g. ". = . + 0x1000"), a new instance of ExprParser
53// is created and ran.
54namespace {
55class ExprParser : public ScriptParserBase {
56public:
57 ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot)
58 : ScriptParserBase(Tokens), Dot(Dot) {}
59
60 uint64_t run();
61
62private:
63 uint64_t parsePrimary();
64 uint64_t parseTernary(uint64_t Cond);
65 uint64_t apply(StringRef Op, uint64_t L, uint64_t R);
66 uint64_t parseExpr1(uint64_t Lhs, int MinPrec);
67 uint64_t parseExpr();
68
69 uint64_t Dot;
70};
71}
72
Rui Ueyama960504b2016-04-19 18:58:11 +000073static int precedence(StringRef Op) {
74 return StringSwitch<int>(Op)
75 .Case("*", 4)
George Rimarab939062016-04-25 08:14:41 +000076 .Case("/", 4)
77 .Case("+", 3)
78 .Case("-", 3)
79 .Case("<", 2)
80 .Case(">", 2)
81 .Case(">=", 2)
82 .Case("<=", 2)
83 .Case("==", 2)
84 .Case("!=", 2)
Rui Ueyama960504b2016-04-19 18:58:11 +000085 .Case("&", 1)
86 .Default(-1);
87}
88
Rui Ueyama9c1112d2016-04-23 00:04:03 +000089static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) {
90 return ExprParser(Tokens, Dot).run();
Rui Ueyama960504b2016-04-19 18:58:11 +000091}
92
Rui Ueyama9c1112d2016-04-23 00:04:03 +000093uint64_t ExprParser::run() {
94 uint64_t V = parseExpr();
95 if (!atEOF() && !Error)
96 setError("stray token: " + peek());
97 return V;
Rui Ueyama60118112016-04-20 20:54:13 +000098}
99
Rui Ueyama960504b2016-04-19 18:58:11 +0000100// This is a part of the operator-precedence parser to evaluate
101// arithmetic expressions in SECTIONS command. This function evaluates an
Rui Ueyamae29a9752016-04-22 21:02:27 +0000102// integer literal, a parenthesized expression, the ALIGN function,
103// or the special variable ".".
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000104uint64_t ExprParser::parsePrimary() {
105 StringRef Tok = next();
Rui Ueyama960504b2016-04-19 18:58:11 +0000106 if (Tok == ".")
107 return Dot;
108 if (Tok == "(") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000109 uint64_t V = parseExpr();
110 expect(")");
Rui Ueyama960504b2016-04-19 18:58:11 +0000111 return V;
112 }
George Rimardffc1412016-04-22 11:40:53 +0000113 if (Tok == "ALIGN") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000114 expect("(");
115 uint64_t V = parseExpr();
116 expect(")");
George Rimardffc1412016-04-22 11:40:53 +0000117 return alignTo(Dot, V);
118 }
Rui Ueyama5fa60982016-04-22 21:05:04 +0000119 uint64_t V = 0;
120 if (Tok.getAsInteger(0, V))
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000121 setError("malformed number: " + Tok);
Rui Ueyama5fa60982016-04-22 21:05:04 +0000122 return V;
Rui Ueyama960504b2016-04-19 18:58:11 +0000123}
124
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000125uint64_t ExprParser::parseTernary(uint64_t Cond) {
126 next();
127 uint64_t V = parseExpr();
128 expect(":");
129 uint64_t W = parseExpr();
George Rimarfba45c42016-04-22 11:28:54 +0000130 return Cond ? V : W;
131}
132
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000133uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000134 if (Op == "*")
135 return L * R;
136 if (Op == "/") {
137 if (R == 0) {
138 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000139 return 0;
140 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000141 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000142 }
George Rimarab939062016-04-25 08:14:41 +0000143 if (Op == "+")
144 return L + R;
145 if (Op == "-")
146 return L - R;
147 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;
Rui Ueyama960504b2016-04-19 18:58:11 +0000159 if (Op == "&")
160 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000161 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000162}
163
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000164// This is a part of the operator-precedence parser.
165// This function assumes that the remaining token stream starts
166// with an operator.
167uint64_t ExprParser::parseExpr1(uint64_t Lhs, int MinPrec) {
168 while (!atEOF()) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000169 // Read an operator and an expression.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000170 StringRef Op1 = peek();
George Rimarfba45c42016-04-22 11:28:54 +0000171 if (Op1 == "?")
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000172 return parseTernary(Lhs);
Rui Ueyama960504b2016-04-19 18:58:11 +0000173 if (precedence(Op1) < MinPrec)
174 return Lhs;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000175 next();
176 uint64_t Rhs = parsePrimary();
Rui Ueyama960504b2016-04-19 18:58:11 +0000177
178 // Evaluate the remaining part of the expression first if the
179 // next operator has greater precedence than the previous one.
180 // For example, if we have read "+" and "3", and if the next
181 // operator is "*", then we'll evaluate 3 * ... part first.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000182 while (!atEOF()) {
183 StringRef Op2 = peek();
Rui Ueyama960504b2016-04-19 18:58:11 +0000184 if (precedence(Op2) <= precedence(Op1))
185 break;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000186 Rhs = parseExpr1(Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000187 }
188
189 Lhs = apply(Op1, Lhs, Rhs);
190 }
191 return Lhs;
192}
193
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000194// Reads and evaluates an arithmetic expression.
195uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); }
George Rimar652852c2016-04-16 10:10:32 +0000196
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000197template <class ELFT>
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000198StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000199 for (SectionRule &R : Opt.Sections)
Rui Ueyama722830a2016-06-29 05:32:09 +0000200 if (globMatch(R.SectionPattern, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000201 return R.Dest;
202 return "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000203}
204
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000205template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000206bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Eugene Leviante63d81b2016-07-20 14:43:20 +0000207 return !S || !S->Live || getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000208}
209
Rui Ueyama07320e42016-04-20 20:13:41 +0000210template <class ELFT>
211bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000212 for (StringRef Pat : Opt.KeptSections)
Rui Ueyama722830a2016-06-29 05:32:09 +0000213 if (globMatch(Pat, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000214 return true;
215 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000216}
217
George Rimar652852c2016-04-16 10:10:32 +0000218template <class ELFT>
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000219std::vector<OutputSectionBase<ELFT> *>
Eugene Leviante63d81b2016-07-20 14:43:20 +0000220LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000221 std::vector<OutputSectionBase<ELFT> *> Result;
222
Eugene Leviante63d81b2016-07-20 14:43:20 +0000223 // Add input section to output section. If there is no output section yet,
224 // then create it and add to output section list.
225 auto AddInputSec = [&](InputSectionBase<ELFT> *C, StringRef Name) {
226 OutputSectionBase<ELFT> *Sec;
227 bool IsNew;
228 std::tie(Sec, IsNew) = Factory.create(C, Name);
229 if (IsNew)
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000230 Result.push_back(Sec);
Eugene Leviante63d81b2016-07-20 14:43:20 +0000231 Sec->addSection(C);
232 };
233
234 // Select input sections matching rule and add them to corresponding
235 // output section. Section rules are processed in order they're listed
236 // in script, so correct input section order is maintained by design.
237 for (SectionRule &R : Opt.Sections)
238 for (const std::unique_ptr<ObjectFile<ELFT>> &F :
239 Symtab<ELFT>::X->getObjectFiles())
240 for (InputSectionBase<ELFT> *S : F->getSections())
241 if (!isDiscarded(S) && !S->OutSec &&
242 globMatch(R.SectionPattern, S->getSectionName()))
243 // Add single input section to output section.
244 AddInputSec(S, R.Dest);
245
246 // Add all other input sections, which are not listed in script.
247 for (const std::unique_ptr<ObjectFile<ELFT>> &F :
248 Symtab<ELFT>::X->getObjectFiles())
249 for (InputSectionBase<ELFT> *S : F->getSections())
250 if (!isDiscarded(S)) {
251 if (!S->OutSec)
252 AddInputSec(S, getOutputSectionName(S));
253 } else
254 reportDiscarded(S, F);
255
256 return Result;
257}
258
259template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000260void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000261 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000262 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000263 // are not explicitly placed into the output file by the linker script.
264 // We place orphan sections at end of file.
265 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000266 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000267 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000268 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000269 if (getSectionIndex(Name) == INT_MAX)
George Rimar076fe152016-07-21 06:43:01 +0000270 Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
George Rimar652852c2016-04-16 10:10:32 +0000271 }
George Rimar652852c2016-04-16 10:10:32 +0000272
Rui Ueyama7c18c282016-04-18 21:00:40 +0000273 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyama52c4e172016-07-01 10:42:25 +0000274 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000275 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
George Rimar652852c2016-04-16 10:10:32 +0000276 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000277
George Rimar076fe152016-07-21 06:43:01 +0000278 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
279 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
280 uint64_t Val = evalExpr(Cmd->Expr, Dot);
281 if (Cmd->Name == ".") {
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000282
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000283 Dot = Val;
284 } else {
George Rimar076fe152016-07-21 06:43:01 +0000285 auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd->Name));
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000286 D->Value = Val;
287 }
George Rimar652852c2016-04-16 10:10:32 +0000288 continue;
289 }
290
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000291 // Find all the sections with required name. There can be more than
George Rimar6ad330a2016-07-19 07:39:07 +0000292 // one section with such name, if the alignment, flags or type
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000293 // attribute differs.
George Rimar076fe152016-07-21 06:43:01 +0000294 auto *Cmd = cast<OutputSectionCommand>(Base.get());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000295 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar076fe152016-07-21 06:43:01 +0000296 if (Sec->getName() != Cmd->Name)
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000297 continue;
George Rimar652852c2016-04-16 10:10:32 +0000298
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000299 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
300 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama424b4082016-06-17 01:18:46 +0000301 TVA = alignTo(TVA, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000302 Sec->setVA(TVA);
303 ThreadBssOffset = TVA - Dot + Sec->getSize();
304 continue;
305 }
George Rimar652852c2016-04-16 10:10:32 +0000306
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000307 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama424b4082016-06-17 01:18:46 +0000308 Dot = alignTo(Dot, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000309 Sec->setVA(Dot);
Rui Ueyama52c4e172016-07-01 10:42:25 +0000310 MinVA = std::min(MinVA, Dot);
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000311 Dot += Sec->getSize();
312 continue;
313 }
George Rimar652852c2016-04-16 10:10:32 +0000314 }
315 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000316
Rafael Espindola64c32d62016-07-07 14:28:47 +0000317 // ELF and Program headers need to be right before the first section in
George Rimarb91e7112016-07-19 07:42:07 +0000318 // memory. Set their addresses accordingly.
Eugene Leviant467c4d52016-07-01 10:27:36 +0000319 MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
320 Out<ELFT>::ProgramHeaders->getSize(),
321 Target->PageSize);
322 Out<ELFT>::ElfHeader->setVA(MinVA);
323 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000324}
325
Rui Ueyama07320e42016-04-20 20:13:41 +0000326template <class ELFT>
Rafael Espindola74df5c72016-07-19 12:33:46 +0000327std::vector<PhdrEntry<ELFT>>
Eugene Leviantbbe38602016-07-19 09:25:43 +0000328LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) {
329 int TlsNum = -1;
330 int NoteNum = -1;
331 int RelroNum = -1;
332 Phdr *Load = nullptr;
333 uintX_t Flags = PF_R;
334 std::vector<Phdr> Phdrs;
335
336 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Eugene Leviant865bf862016-07-21 10:43:25 +0000337 Phdrs.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000338 Phdr &Added = Phdrs.back();
339
340 if (Cmd.HasFilehdr)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000341 Added.add(Out<ELFT>::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000342 if (Cmd.HasPhdrs)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000343 Added.add(Out<ELFT>::ProgramHeaders);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000344
345 switch (Cmd.Type) {
346 case PT_INTERP:
347 if (needsInterpSection<ELFT>())
Rui Ueyama18f084f2016-07-20 19:36:41 +0000348 Added.add(Out<ELFT>::Interp);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000349 break;
350 case PT_DYNAMIC:
351 if (isOutputDynamic<ELFT>()) {
352 Added.H.p_flags = toPhdrFlags(Out<ELFT>::Dynamic->getFlags());
Rui Ueyama18f084f2016-07-20 19:36:41 +0000353 Added.add(Out<ELFT>::Dynamic);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000354 }
355 break;
356 case PT_TLS:
357 TlsNum = Phdrs.size() - 1;
358 break;
359 case PT_NOTE:
360 NoteNum = Phdrs.size() - 1;
361 break;
362 case PT_GNU_RELRO:
363 RelroNum = Phdrs.size() - 1;
364 break;
365 case PT_GNU_EH_FRAME:
366 if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) {
367 Added.H.p_flags = toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags());
Rui Ueyama18f084f2016-07-20 19:36:41 +0000368 Added.add(Out<ELFT>::EhFrameHdr);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000369 }
370 break;
371 }
372 }
373
374 for (OutputSectionBase<ELFT> *Sec : Sections) {
375 if (!(Sec->getFlags() & SHF_ALLOC))
376 break;
377
378 if (TlsNum != -1 && (Sec->getFlags() & SHF_TLS))
Rui Ueyama18f084f2016-07-20 19:36:41 +0000379 Phdrs[TlsNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000380
381 if (!needsPtLoad<ELFT>(Sec))
382 continue;
383
384 const std::vector<size_t> &PhdrIds =
385 getPhdrIndicesForSection(Sec->getName());
386 if (!PhdrIds.empty()) {
387 // Assign headers specified by linker script
388 for (size_t Id : PhdrIds) {
Rui Ueyama18f084f2016-07-20 19:36:41 +0000389 Phdrs[Id].add(Sec);
Eugene Leviant865bf862016-07-21 10:43:25 +0000390 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
391 Phdrs[Id].H.p_flags |= toPhdrFlags(Sec->getFlags());
Eugene Leviantbbe38602016-07-19 09:25:43 +0000392 Phdrs[Id].H.p_flags |= toPhdrFlags(Sec->getFlags());
393 }
394 } else {
395 // If we have no load segment or flags've changed then we want new load
396 // segment.
397 uintX_t NewFlags = toPhdrFlags(Sec->getFlags());
398 if (Load == nullptr || Flags != NewFlags) {
399 Load = &*Phdrs.emplace(Phdrs.end(), PT_LOAD, NewFlags);
400 Flags = NewFlags;
401 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000402 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000403 }
404
405 if (RelroNum != -1 && isRelroSection(Sec))
Rui Ueyama18f084f2016-07-20 19:36:41 +0000406 Phdrs[RelroNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000407 if (NoteNum != -1 && Sec->getType() == SHT_NOTE)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000408 Phdrs[NoteNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000409 }
410 return Phdrs;
411}
412
413template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000414ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
George Rimarf6c3cce2016-07-21 07:48:54 +0000415 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
416 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
417 if (Cmd->Name == Name)
418 return Cmd->Filler;
419 return {};
George Rimare2ee72b2016-02-26 14:48:31 +0000420}
421
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000422// Returns the index of the given section name in linker script
423// SECTIONS commands. Sections are laid out as the same order as they
424// were in the script. If a given name did not appear in the script,
425// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000426template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000427 auto Begin = Opt.Commands.begin();
428 auto End = Opt.Commands.end();
George Rimar076fe152016-07-21 06:43:01 +0000429 auto I =
430 std::find_if(Begin, End, [&](const std::unique_ptr<BaseCommand> &Base) {
431 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
432 if (Cmd->Name == Name)
433 return true;
434 return false;
435 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000436 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000437}
438
439// A compartor to sort output sections. Returns -1 or 1 if
440// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000441template <class ELFT>
442int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000443 int I = getSectionIndex(A);
444 int J = getSectionIndex(B);
445 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000446 return 0;
447 return I < J ? -1 : 1;
448}
449
George Rimar076fe152016-07-21 06:43:01 +0000450template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() {
451 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
452 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()))
453 if (Cmd->Name != "." && Symtab<ELFT>::X->find(Cmd->Name) == nullptr)
454 Symtab<ELFT>::X->addAbsolute(Cmd->Name, STV_DEFAULT);
Eugene Levianteda81a12016-07-12 06:39:48 +0000455}
456
Eugene Leviantbbe38602016-07-19 09:25:43 +0000457template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
458 return !Opt.PhdrsCommands.empty();
459}
460
461// Returns indices of ELF headers containing specific section, identified
462// by Name. Each index is a zero based number of ELF header listed within
463// PHDRS {} script block.
464template <class ELFT>
465std::vector<size_t>
466LinkerScript<ELFT>::getPhdrIndicesForSection(StringRef Name) {
George Rimar076fe152016-07-21 06:43:01 +0000467 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
468 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
469 if (!Cmd || Cmd->Name != Name)
George Rimar31d842f2016-07-20 16:43:03 +0000470 continue;
471
472 std::vector<size_t> Indices;
George Rimar076fe152016-07-21 06:43:01 +0000473 for (StringRef PhdrName : Cmd->Phdrs) {
George Rimar31d842f2016-07-20 16:43:03 +0000474 auto ItPhdr =
475 std::find_if(Opt.PhdrsCommands.rbegin(), Opt.PhdrsCommands.rend(),
George Rimar076fe152016-07-21 06:43:01 +0000476 [&](PhdrsCommand &P) { return P.Name == PhdrName; });
Eugene Leviantbbe38602016-07-19 09:25:43 +0000477 if (ItPhdr == Opt.PhdrsCommands.rend())
478 error("section header '" + PhdrName + "' is not listed in PHDRS");
479 else
480 Indices.push_back(std::distance(ItPhdr, Opt.PhdrsCommands.rend()) - 1);
481 }
George Rimar31d842f2016-07-20 16:43:03 +0000482 return Indices;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000483 }
George Rimar31d842f2016-07-20 16:43:03 +0000484 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000485}
486
Rui Ueyama07320e42016-04-20 20:13:41 +0000487class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000488 typedef void (ScriptParser::*Handler)();
489
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000490public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000491 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000492
Rui Ueyama4a465392016-04-22 22:59:24 +0000493 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000494
495private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000496 void addFile(StringRef Path);
497
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000498 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000499 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000500 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000501 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000502 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000503 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000504 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000505 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000506 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000507 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000508 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000509 void readSections();
510
George Rimar652852c2016-04-16 10:10:32 +0000511 void readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000512 void readOutputSectionDescription(StringRef OutSec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000513 std::vector<StringRef> readOutputSectionPhdrs();
514 unsigned readPhdrType();
Eugene Levianteda81a12016-07-12 06:39:48 +0000515 void readSymbolAssignment(StringRef Name);
516 std::vector<StringRef> readSectionsCommandExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000517
George Rimarc3794e52016-02-24 09:21:47 +0000518 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000519 ScriptConfiguration &Opt = *ScriptConfig;
520 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000521 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000522};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000523
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000524const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000525 {"ENTRY", &ScriptParser::readEntry},
526 {"EXTERN", &ScriptParser::readExtern},
527 {"GROUP", &ScriptParser::readGroup},
528 {"INCLUDE", &ScriptParser::readInclude},
529 {"INPUT", &ScriptParser::readGroup},
530 {"OUTPUT", &ScriptParser::readOutput},
531 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
532 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
Eugene Leviantbbe38602016-07-19 09:25:43 +0000533 {"PHDRS", &ScriptParser::readPhdrs},
George Rimarc3794e52016-02-24 09:21:47 +0000534 {"SEARCH_DIR", &ScriptParser::readSearchDir},
535 {"SECTIONS", &ScriptParser::readSections},
536 {";", &ScriptParser::readNothing}};
537
Rui Ueyama717677a2016-02-11 21:17:59 +0000538void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000539 while (!atEOF()) {
540 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000541 if (Handler Fn = Cmd.lookup(Tok))
542 (this->*Fn)();
543 else
George Rimar57610422016-03-11 14:43:02 +0000544 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000545 }
546}
547
Rui Ueyama717677a2016-02-11 21:17:59 +0000548void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000549 if (IsUnderSysroot && S.startswith("/")) {
550 SmallString<128> Path;
551 (Config->Sysroot + S).toStringRef(Path);
552 if (sys::fs::exists(Path)) {
553 Driver->addFile(Saver.save(Path.str()));
554 return;
555 }
556 }
557
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000558 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000559 Driver->addFile(S);
560 } else if (S.startswith("=")) {
561 if (Config->Sysroot.empty())
562 Driver->addFile(S.substr(1));
563 else
564 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
565 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000566 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000567 } else if (sys::fs::exists(S)) {
568 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000569 } else {
570 std::string Path = findFromSearchPaths(S);
571 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000572 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000573 else
574 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000575 }
576}
577
Rui Ueyama717677a2016-02-11 21:17:59 +0000578void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000579 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000580 bool Orig = Config->AsNeeded;
581 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000582 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000583 StringRef Tok = next();
584 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000585 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000586 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000587 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000588 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000589}
590
Rui Ueyama717677a2016-02-11 21:17:59 +0000591void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000592 // -e <symbol> takes predecence over ENTRY(<symbol>).
593 expect("(");
594 StringRef Tok = next();
595 if (Config->Entry.empty())
596 Config->Entry = Tok;
597 expect(")");
598}
599
Rui Ueyama717677a2016-02-11 21:17:59 +0000600void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000601 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000602 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000603 StringRef Tok = next();
604 if (Tok == ")")
605 return;
606 Config->Undefined.push_back(Tok);
607 }
608}
609
Rui Ueyama717677a2016-02-11 21:17:59 +0000610void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000611 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000612 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000613 StringRef Tok = next();
614 if (Tok == ")")
615 return;
616 if (Tok == "AS_NEEDED") {
617 readAsNeeded();
618 continue;
619 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000620 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000621 }
622}
623
Rui Ueyama717677a2016-02-11 21:17:59 +0000624void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000625 StringRef Tok = next();
626 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000627 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000628 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000629 return;
630 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000631 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000632 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
633 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000634 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000635}
636
Rui Ueyama717677a2016-02-11 21:17:59 +0000637void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000638 // -o <file> takes predecence over OUTPUT(<file>).
639 expect("(");
640 StringRef Tok = next();
641 if (Config->OutputFile.empty())
642 Config->OutputFile = Tok;
643 expect(")");
644}
645
Rui Ueyama717677a2016-02-11 21:17:59 +0000646void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000647 // Error checking only for now.
648 expect("(");
649 next();
650 expect(")");
651}
652
Rui Ueyama717677a2016-02-11 21:17:59 +0000653void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000654 // Error checking only for now.
655 expect("(");
656 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000657 StringRef Tok = next();
658 if (Tok == ")")
659 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000660 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000661 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000662 return;
663 }
Davide Italiano6836c612015-10-12 21:08:41 +0000664 next();
665 expect(",");
666 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000667 expect(")");
668}
669
Eugene Leviantbbe38602016-07-19 09:25:43 +0000670void ScriptParser::readPhdrs() {
671 expect("{");
672 while (!Error && !skip("}")) {
673 StringRef Tok = next();
Eugene Leviant865bf862016-07-21 10:43:25 +0000674 Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX});
Eugene Leviantbbe38602016-07-19 09:25:43 +0000675 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
676
677 PhdrCmd.Type = readPhdrType();
678 do {
679 Tok = next();
680 if (Tok == ";")
681 break;
682 if (Tok == "FILEHDR")
683 PhdrCmd.HasFilehdr = true;
684 else if (Tok == "PHDRS")
685 PhdrCmd.HasPhdrs = true;
Eugene Leviant865bf862016-07-21 10:43:25 +0000686 else if (Tok == "FLAGS") {
687 expect("(");
688 next().getAsInteger(0, PhdrCmd.Flags);
689 expect(")");
690 } else
Eugene Leviantbbe38602016-07-19 09:25:43 +0000691 setError("unexpected header attribute: " + Tok);
692 } while (!Error);
693 }
694}
695
Rui Ueyama717677a2016-02-11 21:17:59 +0000696void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000697 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000698 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000699 expect(")");
700}
701
Rui Ueyama717677a2016-02-11 21:17:59 +0000702void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000703 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000704 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000705 while (!Error && !skip("}")) {
706 StringRef Tok = peek();
Eugene Levianteda81a12016-07-12 06:39:48 +0000707 if (Tok == ".") {
George Rimar652852c2016-04-16 10:10:32 +0000708 readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000709 continue;
710 }
711 next();
712 if (peek() == "=")
713 readSymbolAssignment(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000714 else
Eugene Levianteda81a12016-07-12 06:39:48 +0000715 readOutputSectionDescription(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000716 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000717}
718
George Rimar652852c2016-04-16 10:10:32 +0000719void ScriptParser::readLocationCounterValue() {
720 expect(".");
721 expect("=");
Eugene Levianteda81a12016-07-12 06:39:48 +0000722 std::vector<StringRef> Expr = readSectionsCommandExpr();
723 if (Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000724 error("error in location counter expression");
Eugene Levianteda81a12016-07-12 06:39:48 +0000725 else
George Rimar076fe152016-07-21 06:43:01 +0000726 Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(".", Expr));
George Rimar652852c2016-04-16 10:10:32 +0000727}
728
Eugene Levianteda81a12016-07-12 06:39:48 +0000729void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +0000730 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
731 Opt.Commands.emplace_back(Cmd);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000732 expect(":");
733 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000734
Rui Ueyama025d59b2016-02-02 20:27:59 +0000735 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000736 StringRef Tok = next();
737 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000738 expect("(");
739 while (!Error && !skip(")"))
740 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000741 } else if (Tok == "KEEP") {
742 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000743 expect("*");
744 expect("(");
745 while (!Error && !skip(")")) {
746 StringRef Sec = next();
747 Opt.Sections.emplace_back(OutSec, Sec);
748 Opt.KeptSections.push_back(Sec);
749 }
George Rimar481c2ce2016-02-23 07:47:54 +0000750 expect(")");
751 } else {
George Rimar777f9632016-03-12 08:31:34 +0000752 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000753 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000754 }
George Rimar076fe152016-07-21 06:43:01 +0000755 Cmd->Phdrs = readOutputSectionPhdrs();
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000756
George Rimare2ee72b2016-02-26 14:48:31 +0000757 StringRef Tok = peek();
758 if (Tok.startswith("=")) {
759 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000760 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000761 return;
762 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000763 Tok = Tok.substr(3);
George Rimarf6c3cce2016-07-21 07:48:54 +0000764 Cmd->Filler = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000765 next();
766 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000767}
768
Eugene Levianteda81a12016-07-12 06:39:48 +0000769void ScriptParser::readSymbolAssignment(StringRef Name) {
770 expect("=");
771 std::vector<StringRef> Expr = readSectionsCommandExpr();
772 if (Expr.empty())
773 error("error in symbol assignment expression");
774 else
George Rimar076fe152016-07-21 06:43:01 +0000775 Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(Name, Expr));
Eugene Levianteda81a12016-07-12 06:39:48 +0000776}
777
778std::vector<StringRef> ScriptParser::readSectionsCommandExpr() {
779 std::vector<StringRef> Expr;
780 while (!Error) {
781 StringRef Tok = next();
782 if (Tok == ";")
783 break;
784 Expr.push_back(Tok);
785 }
786 return Expr;
787}
788
Eugene Leviantbbe38602016-07-19 09:25:43 +0000789std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
790 std::vector<StringRef> Phdrs;
791 while (!Error && peek().startswith(":")) {
792 StringRef Tok = next();
793 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
794 if (Tok.empty()) {
795 setError("section header name is empty");
796 break;
797 }
Rui Ueyama047404f2016-07-20 19:36:36 +0000798 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000799 }
800 return Phdrs;
801}
802
803unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000804 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +0000805 unsigned Ret = StringSwitch<unsigned>(Tok)
806 .Case("PT_NULL", PT_NULL)
807 .Case("PT_LOAD", PT_LOAD)
808 .Case("PT_DYNAMIC", PT_DYNAMIC)
809 .Case("PT_INTERP", PT_INTERP)
810 .Case("PT_NOTE", PT_NOTE)
811 .Case("PT_SHLIB", PT_SHLIB)
812 .Case("PT_PHDR", PT_PHDR)
813 .Case("PT_TLS", PT_TLS)
814 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
815 .Case("PT_GNU_STACK", PT_GNU_STACK)
816 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
817 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000818
Rui Ueyamab0f6c592016-07-20 19:36:38 +0000819 if (Ret == (unsigned)-1) {
820 setError("invalid program header type: " + Tok);
821 return PT_NULL;
822 }
823 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000824}
825
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000826static bool isUnderSysroot(StringRef Path) {
827 if (Config->Sysroot == "")
828 return false;
829 for (; !Path.empty(); Path = sys::path::parent_path(Path))
830 if (sys::fs::equivalent(Config->Sysroot, Path))
831 return true;
832 return false;
833}
834
Rui Ueyama07320e42016-04-20 20:13:41 +0000835// Entry point.
836void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000837 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000838 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000839}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000840
Rui Ueyama07320e42016-04-20 20:13:41 +0000841template class elf::LinkerScript<ELF32LE>;
842template class elf::LinkerScript<ELF32BE>;
843template class elf::LinkerScript<ELF64LE>;
844template class elf::LinkerScript<ELF64BE>;