blob: 85bfae7e3d5a953bcf358462fd2a98f2b4fe728b [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) {
337 Phdrs.emplace_back(Cmd.Type, PF_R);
338 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 Leviantbbe38602016-07-19 09:25:43 +0000390 Phdrs[Id].H.p_flags |= toPhdrFlags(Sec->getFlags());
391 }
392 } else {
393 // If we have no load segment or flags've changed then we want new load
394 // segment.
395 uintX_t NewFlags = toPhdrFlags(Sec->getFlags());
396 if (Load == nullptr || Flags != NewFlags) {
397 Load = &*Phdrs.emplace(Phdrs.end(), PT_LOAD, NewFlags);
398 Flags = NewFlags;
399 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000400 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000401 }
402
403 if (RelroNum != -1 && isRelroSection(Sec))
Rui Ueyama18f084f2016-07-20 19:36:41 +0000404 Phdrs[RelroNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000405 if (NoteNum != -1 && Sec->getType() == SHT_NOTE)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000406 Phdrs[NoteNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000407 }
408 return Phdrs;
409}
410
411template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000412ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
413 auto I = Opt.Filler.find(Name);
414 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000415 return {};
416 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000417}
418
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000419// Returns the index of the given section name in linker script
420// SECTIONS commands. Sections are laid out as the same order as they
421// were in the script. If a given name did not appear in the script,
422// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000423template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000424 auto Begin = Opt.Commands.begin();
425 auto End = Opt.Commands.end();
George Rimar076fe152016-07-21 06:43:01 +0000426 auto I =
427 std::find_if(Begin, End, [&](const std::unique_ptr<BaseCommand> &Base) {
428 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
429 if (Cmd->Name == Name)
430 return true;
431 return false;
432 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000433 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000434}
435
436// A compartor to sort output sections. Returns -1 or 1 if
437// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000438template <class ELFT>
439int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000440 int I = getSectionIndex(A);
441 int J = getSectionIndex(B);
442 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000443 return 0;
444 return I < J ? -1 : 1;
445}
446
George Rimar076fe152016-07-21 06:43:01 +0000447template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() {
448 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
449 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()))
450 if (Cmd->Name != "." && Symtab<ELFT>::X->find(Cmd->Name) == nullptr)
451 Symtab<ELFT>::X->addAbsolute(Cmd->Name, STV_DEFAULT);
Eugene Levianteda81a12016-07-12 06:39:48 +0000452}
453
Eugene Leviantbbe38602016-07-19 09:25:43 +0000454template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
455 return !Opt.PhdrsCommands.empty();
456}
457
458// Returns indices of ELF headers containing specific section, identified
459// by Name. Each index is a zero based number of ELF header listed within
460// PHDRS {} script block.
461template <class ELFT>
462std::vector<size_t>
463LinkerScript<ELFT>::getPhdrIndicesForSection(StringRef Name) {
George Rimar076fe152016-07-21 06:43:01 +0000464 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
465 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
466 if (!Cmd || Cmd->Name != Name)
George Rimar31d842f2016-07-20 16:43:03 +0000467 continue;
468
469 std::vector<size_t> Indices;
George Rimar076fe152016-07-21 06:43:01 +0000470 for (StringRef PhdrName : Cmd->Phdrs) {
George Rimar31d842f2016-07-20 16:43:03 +0000471 auto ItPhdr =
472 std::find_if(Opt.PhdrsCommands.rbegin(), Opt.PhdrsCommands.rend(),
George Rimar076fe152016-07-21 06:43:01 +0000473 [&](PhdrsCommand &P) { return P.Name == PhdrName; });
Eugene Leviantbbe38602016-07-19 09:25:43 +0000474 if (ItPhdr == Opt.PhdrsCommands.rend())
475 error("section header '" + PhdrName + "' is not listed in PHDRS");
476 else
477 Indices.push_back(std::distance(ItPhdr, Opt.PhdrsCommands.rend()) - 1);
478 }
George Rimar31d842f2016-07-20 16:43:03 +0000479 return Indices;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000480 }
George Rimar31d842f2016-07-20 16:43:03 +0000481 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000482}
483
Rui Ueyama07320e42016-04-20 20:13:41 +0000484class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000485 typedef void (ScriptParser::*Handler)();
486
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000487public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000488 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000489
Rui Ueyama4a465392016-04-22 22:59:24 +0000490 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000491
492private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000493 void addFile(StringRef Path);
494
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000495 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000496 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000497 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000498 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000499 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000500 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000501 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000502 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000503 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000504 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000505 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000506 void readSections();
507
George Rimar652852c2016-04-16 10:10:32 +0000508 void readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000509 void readOutputSectionDescription(StringRef OutSec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000510 std::vector<StringRef> readOutputSectionPhdrs();
511 unsigned readPhdrType();
Eugene Levianteda81a12016-07-12 06:39:48 +0000512 void readSymbolAssignment(StringRef Name);
513 std::vector<StringRef> readSectionsCommandExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000514
George Rimarc3794e52016-02-24 09:21:47 +0000515 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000516 ScriptConfiguration &Opt = *ScriptConfig;
517 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000518 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000519};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000520
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000521const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000522 {"ENTRY", &ScriptParser::readEntry},
523 {"EXTERN", &ScriptParser::readExtern},
524 {"GROUP", &ScriptParser::readGroup},
525 {"INCLUDE", &ScriptParser::readInclude},
526 {"INPUT", &ScriptParser::readGroup},
527 {"OUTPUT", &ScriptParser::readOutput},
528 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
529 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
Eugene Leviantbbe38602016-07-19 09:25:43 +0000530 {"PHDRS", &ScriptParser::readPhdrs},
George Rimarc3794e52016-02-24 09:21:47 +0000531 {"SEARCH_DIR", &ScriptParser::readSearchDir},
532 {"SECTIONS", &ScriptParser::readSections},
533 {";", &ScriptParser::readNothing}};
534
Rui Ueyama717677a2016-02-11 21:17:59 +0000535void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000536 while (!atEOF()) {
537 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000538 if (Handler Fn = Cmd.lookup(Tok))
539 (this->*Fn)();
540 else
George Rimar57610422016-03-11 14:43:02 +0000541 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000542 }
543}
544
Rui Ueyama717677a2016-02-11 21:17:59 +0000545void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000546 if (IsUnderSysroot && S.startswith("/")) {
547 SmallString<128> Path;
548 (Config->Sysroot + S).toStringRef(Path);
549 if (sys::fs::exists(Path)) {
550 Driver->addFile(Saver.save(Path.str()));
551 return;
552 }
553 }
554
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000555 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000556 Driver->addFile(S);
557 } else if (S.startswith("=")) {
558 if (Config->Sysroot.empty())
559 Driver->addFile(S.substr(1));
560 else
561 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
562 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000563 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000564 } else if (sys::fs::exists(S)) {
565 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000566 } else {
567 std::string Path = findFromSearchPaths(S);
568 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000569 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000570 else
571 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000572 }
573}
574
Rui Ueyama717677a2016-02-11 21:17:59 +0000575void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000576 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000577 bool Orig = Config->AsNeeded;
578 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000579 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000580 StringRef Tok = next();
581 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000582 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000583 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000584 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000585 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000586}
587
Rui Ueyama717677a2016-02-11 21:17:59 +0000588void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000589 // -e <symbol> takes predecence over ENTRY(<symbol>).
590 expect("(");
591 StringRef Tok = next();
592 if (Config->Entry.empty())
593 Config->Entry = Tok;
594 expect(")");
595}
596
Rui Ueyama717677a2016-02-11 21:17:59 +0000597void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000598 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000599 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000600 StringRef Tok = next();
601 if (Tok == ")")
602 return;
603 Config->Undefined.push_back(Tok);
604 }
605}
606
Rui Ueyama717677a2016-02-11 21:17:59 +0000607void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000608 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000609 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000610 StringRef Tok = next();
611 if (Tok == ")")
612 return;
613 if (Tok == "AS_NEEDED") {
614 readAsNeeded();
615 continue;
616 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000617 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000618 }
619}
620
Rui Ueyama717677a2016-02-11 21:17:59 +0000621void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000622 StringRef Tok = next();
623 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000624 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000625 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000626 return;
627 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000628 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000629 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
630 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000631 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000632}
633
Rui Ueyama717677a2016-02-11 21:17:59 +0000634void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000635 // -o <file> takes predecence over OUTPUT(<file>).
636 expect("(");
637 StringRef Tok = next();
638 if (Config->OutputFile.empty())
639 Config->OutputFile = Tok;
640 expect(")");
641}
642
Rui Ueyama717677a2016-02-11 21:17:59 +0000643void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000644 // Error checking only for now.
645 expect("(");
646 next();
647 expect(")");
648}
649
Rui Ueyama717677a2016-02-11 21:17:59 +0000650void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000651 // Error checking only for now.
652 expect("(");
653 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000654 StringRef Tok = next();
655 if (Tok == ")")
656 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000657 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000658 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000659 return;
660 }
Davide Italiano6836c612015-10-12 21:08:41 +0000661 next();
662 expect(",");
663 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000664 expect(")");
665}
666
Eugene Leviantbbe38602016-07-19 09:25:43 +0000667void ScriptParser::readPhdrs() {
668 expect("{");
669 while (!Error && !skip("}")) {
670 StringRef Tok = next();
671 Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false});
672 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
673
674 PhdrCmd.Type = readPhdrType();
675 do {
676 Tok = next();
677 if (Tok == ";")
678 break;
679 if (Tok == "FILEHDR")
680 PhdrCmd.HasFilehdr = true;
681 else if (Tok == "PHDRS")
682 PhdrCmd.HasPhdrs = true;
683 else
684 setError("unexpected header attribute: " + Tok);
685 } while (!Error);
686 }
687}
688
Rui Ueyama717677a2016-02-11 21:17:59 +0000689void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000690 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000691 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000692 expect(")");
693}
694
Rui Ueyama717677a2016-02-11 21:17:59 +0000695void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000696 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000697 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000698 while (!Error && !skip("}")) {
699 StringRef Tok = peek();
Eugene Levianteda81a12016-07-12 06:39:48 +0000700 if (Tok == ".") {
George Rimar652852c2016-04-16 10:10:32 +0000701 readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000702 continue;
703 }
704 next();
705 if (peek() == "=")
706 readSymbolAssignment(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000707 else
Eugene Levianteda81a12016-07-12 06:39:48 +0000708 readOutputSectionDescription(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000709 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000710}
711
George Rimar652852c2016-04-16 10:10:32 +0000712void ScriptParser::readLocationCounterValue() {
713 expect(".");
714 expect("=");
Eugene Levianteda81a12016-07-12 06:39:48 +0000715 std::vector<StringRef> Expr = readSectionsCommandExpr();
716 if (Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000717 error("error in location counter expression");
Eugene Levianteda81a12016-07-12 06:39:48 +0000718 else
George Rimar076fe152016-07-21 06:43:01 +0000719 Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(".", Expr));
George Rimar652852c2016-04-16 10:10:32 +0000720}
721
Eugene Levianteda81a12016-07-12 06:39:48 +0000722void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +0000723 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
724 Opt.Commands.emplace_back(Cmd);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000725 expect(":");
726 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000727
Rui Ueyama025d59b2016-02-02 20:27:59 +0000728 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000729 StringRef Tok = next();
730 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000731 expect("(");
732 while (!Error && !skip(")"))
733 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000734 } else if (Tok == "KEEP") {
735 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000736 expect("*");
737 expect("(");
738 while (!Error && !skip(")")) {
739 StringRef Sec = next();
740 Opt.Sections.emplace_back(OutSec, Sec);
741 Opt.KeptSections.push_back(Sec);
742 }
George Rimar481c2ce2016-02-23 07:47:54 +0000743 expect(")");
744 } else {
George Rimar777f9632016-03-12 08:31:34 +0000745 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000746 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000747 }
George Rimar076fe152016-07-21 06:43:01 +0000748 Cmd->Phdrs = readOutputSectionPhdrs();
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000749
George Rimare2ee72b2016-02-26 14:48:31 +0000750 StringRef Tok = peek();
751 if (Tok.startswith("=")) {
752 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000753 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000754 return;
755 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000756 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000757 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000758 next();
759 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000760}
761
Eugene Levianteda81a12016-07-12 06:39:48 +0000762void ScriptParser::readSymbolAssignment(StringRef Name) {
763 expect("=");
764 std::vector<StringRef> Expr = readSectionsCommandExpr();
765 if (Expr.empty())
766 error("error in symbol assignment expression");
767 else
George Rimar076fe152016-07-21 06:43:01 +0000768 Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(Name, Expr));
Eugene Levianteda81a12016-07-12 06:39:48 +0000769}
770
771std::vector<StringRef> ScriptParser::readSectionsCommandExpr() {
772 std::vector<StringRef> Expr;
773 while (!Error) {
774 StringRef Tok = next();
775 if (Tok == ";")
776 break;
777 Expr.push_back(Tok);
778 }
779 return Expr;
780}
781
Eugene Leviantbbe38602016-07-19 09:25:43 +0000782std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
783 std::vector<StringRef> Phdrs;
784 while (!Error && peek().startswith(":")) {
785 StringRef Tok = next();
786 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
787 if (Tok.empty()) {
788 setError("section header name is empty");
789 break;
790 }
Rui Ueyama047404f2016-07-20 19:36:36 +0000791 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000792 }
793 return Phdrs;
794}
795
796unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000797 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +0000798 unsigned Ret = StringSwitch<unsigned>(Tok)
799 .Case("PT_NULL", PT_NULL)
800 .Case("PT_LOAD", PT_LOAD)
801 .Case("PT_DYNAMIC", PT_DYNAMIC)
802 .Case("PT_INTERP", PT_INTERP)
803 .Case("PT_NOTE", PT_NOTE)
804 .Case("PT_SHLIB", PT_SHLIB)
805 .Case("PT_PHDR", PT_PHDR)
806 .Case("PT_TLS", PT_TLS)
807 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
808 .Case("PT_GNU_STACK", PT_GNU_STACK)
809 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
810 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000811
Rui Ueyamab0f6c592016-07-20 19:36:38 +0000812 if (Ret == (unsigned)-1) {
813 setError("invalid program header type: " + Tok);
814 return PT_NULL;
815 }
816 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000817}
818
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000819static bool isUnderSysroot(StringRef Path) {
820 if (Config->Sysroot == "")
821 return false;
822 for (; !Path.empty(); Path = sys::path::parent_path(Path))
823 if (sys::fs::equivalent(Config->Sysroot, Path))
824 return true;
825 return false;
826}
827
Rui Ueyama07320e42016-04-20 20:13:41 +0000828// Entry point.
829void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000830 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000831 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000832}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000833
Rui Ueyama07320e42016-04-20 20:13:41 +0000834template class elf::LinkerScript<ELF32LE>;
835template class elf::LinkerScript<ELF32BE>;
836template class elf::LinkerScript<ELF64LE>;
837template class elf::LinkerScript<ELF64BE>;