1 //===- 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 parses a linker script and write the result to Config or ScriptConfig
12 // objects.
13 //
14 // If SECTIONS command is used, a ScriptConfig contains an AST
15 // of the command which will later be consumed by createSections() and
16 // assignAddresses().
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "LinkerScript.h"
21 #include "Config.h"
22 #include "Driver.h"
23 #include "InputSection.h"
24 #include "OutputSections.h"
25 #include "ScriptParser.h"
26 #include "Strings.h"
27 #include "Symbols.h"
28 #include "SymbolTable.h"
29 #include "Target.h"
30 #include "Writer.h"
31 #include "llvm/ADT/StringSwitch.h"
32 #include "llvm/Support/ELF.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/StringSaver.h"
37 
38 using namespace llvm;
39 using namespace llvm::ELF;
40 using namespace llvm::object;
41 using namespace lld;
42 using namespace lld::elf;
43 
44 ScriptConfiguration *elf::ScriptConfig;
45 
46 bool SymbolAssignment::classof(const BaseCommand *C) {
47   return C->Kind == AssignmentKind;
48 }
49 
50 bool OutputSectionCommand::classof(const BaseCommand *C) {
51   return C->Kind == OutputSectionKind;
52 }
53 
54 bool InputSectionDescription::classof(const BaseCommand *C) {
55   return C->Kind == InputSectionKind;
56 }
57 
58 template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
59   return !S || !S->Live;
60 }
61 
62 template <class ELFT>
63 bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
64   for (StringRef Pat : Opt.KeptSections)
65     if (globMatch(Pat, S->getSectionName()))
66       return true;
67   return false;
68 }
69 
70 static bool match(ArrayRef<StringRef> Patterns, StringRef S) {
71   for (StringRef Pat : Patterns)
72     if (globMatch(Pat, S))
73       return true;
74   return false;
75 }
76 
77 // Create a vector of (<output section name>, <input section name patterns>).
78 // For example, if a returned vector contains (".text" (".foo.*" ".bar.*")),
79 // input sections start with ".foo." or ".bar." should be added to
80 // ".text" section.
81 template <class ELFT>
82 std::vector<std::pair<StringRef, const InputSectionDescription *>>
83 LinkerScript<ELFT>::getSectionMap() {
84   std::vector<std::pair<StringRef, const InputSectionDescription *>> Ret;
85 
86   for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands)
87     if (auto *Cmd1 = dyn_cast<OutputSectionCommand>(Base1.get()))
88       for (const std::unique_ptr<BaseCommand> &Base2 : Cmd1->Commands)
89         if (auto *Cmd2 = dyn_cast<InputSectionDescription>(Base2.get()))
90           Ret.emplace_back(Cmd1->Name, Cmd2);
91 
92   return Ret;
93 }
94 
95 // Returns input sections filtered by given glob patterns.
96 template <class ELFT>
97 std::vector<InputSectionBase<ELFT> *>
98 LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
99   ArrayRef<StringRef> Patterns = I->Patterns;
100   ArrayRef<StringRef> ExcludedFiles = I->ExcludedFiles;
101   std::vector<InputSectionBase<ELFT> *> Ret;
102   for (const std::unique_ptr<ObjectFile<ELFT>> &F :
103        Symtab<ELFT>::X->getObjectFiles())
104     for (InputSectionBase<ELFT> *S : F->getSections())
105       if (!isDiscarded(S) && !S->OutSec && match(Patterns, S->getSectionName()))
106         if (ExcludedFiles.empty() ||
107             !match(ExcludedFiles, sys::path::filename(F->getName())))
108           Ret.push_back(S);
109   return Ret;
110 }
111 
112 template <class ELFT>
113 std::vector<OutputSectionBase<ELFT> *>
114 LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
115   std::vector<OutputSectionBase<ELFT> *> Ret;
116 
117   // Add input section to output section. If there is no output section yet,
118   // then create it and add to output section list.
119   auto Add = [&](InputSectionBase<ELFT> *C, StringRef Name) {
120     OutputSectionBase<ELFT> *Sec;
121     bool IsNew;
122     std::tie(Sec, IsNew) = Factory.create(C, Name);
123     if (IsNew)
124       Ret.push_back(Sec);
125     Sec->addSection(C);
126   };
127 
128   for (auto &P : getSectionMap()) {
129     StringRef OutputName = P.first;
130     const InputSectionDescription *I = P.second;
131     for (InputSectionBase<ELFT> *S : getInputSections(I)) {
132       if (OutputName == "/DISCARD/") {
133         S->Live = false;
134         reportDiscarded(S);
135         continue;
136       }
137       Add(S, OutputName);
138     }
139   }
140 
141   // Add all other input sections, which are not listed in script.
142   for (const std::unique_ptr<ObjectFile<ELFT>> &F :
143        Symtab<ELFT>::X->getObjectFiles())
144     for (InputSectionBase<ELFT> *S : F->getSections())
145       if (!isDiscarded(S) && !S->OutSec)
146         Add(S, getOutputSectionName(S));
147 
148   // Remove from the output all the sections which did not meet
149   // the optional constraints.
150   return filter(Ret);
151 }
152 
153 // Process ONLY_IF_RO and ONLY_IF_RW.
154 template <class ELFT>
155 std::vector<OutputSectionBase<ELFT> *>
156 LinkerScript<ELFT>::filter(std::vector<OutputSectionBase<ELFT> *> &Sections) {
157   // In this loop, we remove output sections if they don't satisfy
158   // requested properties.
159   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
160     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
161     if (!Cmd || Cmd->Name == "/DISCARD/")
162       continue;
163 
164     if (Cmd->Constraint == ConstraintKind::NoConstraint)
165       continue;
166 
167     auto It = llvm::find_if(Sections, [&](OutputSectionBase<ELFT> *S) {
168       return S->getName() == Cmd->Name;
169     });
170     if (It == Sections.end())
171       continue;
172 
173     OutputSectionBase<ELFT> *Sec = *It;
174     bool Writable = (Sec->getFlags() & SHF_WRITE);
175     bool RO = (Cmd->Constraint == ConstraintKind::ReadOnly);
176     bool RW = (Cmd->Constraint == ConstraintKind::ReadWrite);
177 
178     if ((RO && Writable) || (RW && !Writable)) {
179       Sections.erase(It);
180       continue;
181     }
182   }
183   return Sections;
184 }
185 
186 template <class ELFT>
187 void LinkerScript<ELFT>::dispatchAssignment(SymbolAssignment *Cmd) {
188   uint64_t Val = Cmd->Expression(Dot);
189   if (Cmd->Name == ".") {
190     Dot = Val;
191   } else if (!Cmd->Ignore) {
192     auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd->Name));
193     D->Value = Val;
194   }
195 }
196 
197 template <class ELFT>
198 void LinkerScript<ELFT>::assignAddresses(
199     ArrayRef<OutputSectionBase<ELFT> *> Sections) {
200   // Orphan sections are sections present in the input files which
201   // are not explicitly placed into the output file by the linker script.
202   // We place orphan sections at end of file.
203   // Other linkers places them using some heuristics as described in
204   // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
205   for (OutputSectionBase<ELFT> *Sec : Sections) {
206     StringRef Name = Sec->getName();
207     if (getSectionIndex(Name) == INT_MAX)
208       Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
209   }
210 
211   // Assign addresses as instructed by linker script SECTIONS sub-commands.
212   Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
213   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
214   uintX_t ThreadBssOffset = 0;
215 
216   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
217     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
218       dispatchAssignment(Cmd);
219       continue;
220     }
221 
222     // Find all the sections with required name. There can be more than
223     // one section with such name, if the alignment, flags or type
224     // attribute differs.
225     auto *Cmd = cast<OutputSectionCommand>(Base.get());
226     for (OutputSectionBase<ELFT> *Sec : Sections) {
227       if (Sec->getName() != Cmd->Name)
228         continue;
229 
230       if (Cmd->AddrExpr)
231         Dot = Cmd->AddrExpr(Dot);
232 
233       if (Cmd->AlignExpr)
234         Sec->updateAlignment(Cmd->AlignExpr(Dot));
235 
236       if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
237         uintX_t TVA = Dot + ThreadBssOffset;
238         TVA = alignTo(TVA, Sec->getAlignment());
239         Sec->setVA(TVA);
240         ThreadBssOffset = TVA - Dot + Sec->getSize();
241         continue;
242       }
243 
244       if (Sec->getFlags() & SHF_ALLOC) {
245         Dot = alignTo(Dot, Sec->getAlignment());
246         Sec->setVA(Dot);
247         MinVA = std::min(MinVA, Dot);
248         Dot += Sec->getSize();
249         continue;
250       }
251     }
252   }
253 
254   // ELF and Program headers need to be right before the first section in
255   // memory. Set their addresses accordingly.
256   MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
257                         Out<ELFT>::ProgramHeaders->getSize(),
258                     Target->PageSize);
259   Out<ELFT>::ElfHeader->setVA(MinVA);
260   Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
261 }
262 
263 template <class ELFT>
264 std::vector<PhdrEntry<ELFT>>
265 LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) {
266   std::vector<PhdrEntry<ELFT>> Ret;
267 
268   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
269     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
270     PhdrEntry<ELFT> &Phdr = Ret.back();
271 
272     if (Cmd.HasFilehdr)
273       Phdr.add(Out<ELFT>::ElfHeader);
274     if (Cmd.HasPhdrs)
275       Phdr.add(Out<ELFT>::ProgramHeaders);
276 
277     switch (Cmd.Type) {
278     case PT_INTERP:
279       if (Out<ELFT>::Interp)
280         Phdr.add(Out<ELFT>::Interp);
281       break;
282     case PT_DYNAMIC:
283       if (isOutputDynamic<ELFT>()) {
284         Phdr.H.p_flags = Out<ELFT>::Dynamic->getPhdrFlags();
285         Phdr.add(Out<ELFT>::Dynamic);
286       }
287       break;
288     case PT_GNU_EH_FRAME:
289       if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) {
290         Phdr.H.p_flags = Out<ELFT>::EhFrameHdr->getPhdrFlags();
291         Phdr.add(Out<ELFT>::EhFrameHdr);
292       }
293       break;
294     }
295   }
296 
297   PhdrEntry<ELFT> *Load = nullptr;
298   uintX_t Flags = PF_R;
299   for (OutputSectionBase<ELFT> *Sec : Sections) {
300     if (!(Sec->getFlags() & SHF_ALLOC))
301       break;
302 
303     std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
304     if (!PhdrIds.empty()) {
305       // Assign headers specified by linker script
306       for (size_t Id : PhdrIds) {
307         Ret[Id].add(Sec);
308         if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
309           Ret[Id].H.p_flags |= Sec->getPhdrFlags();
310       }
311     } else {
312       // If we have no load segment or flags've changed then we want new load
313       // segment.
314       uintX_t NewFlags = Sec->getPhdrFlags();
315       if (Load == nullptr || Flags != NewFlags) {
316         Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
317         Flags = NewFlags;
318       }
319       Load->add(Sec);
320     }
321   }
322   return Ret;
323 }
324 
325 template <class ELFT>
326 ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
327   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
328     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
329       if (Cmd->Name == Name)
330         return Cmd->Filler;
331   return {};
332 }
333 
334 // Returns the index of the given section name in linker script
335 // SECTIONS commands. Sections are laid out as the same order as they
336 // were in the script. If a given name did not appear in the script,
337 // it returns INT_MAX, so that it will be laid out at end of file.
338 template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
339   int I = 0;
340   for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
341     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
342       if (Cmd->Name == Name)
343         return I;
344     ++I;
345   }
346   return INT_MAX;
347 }
348 
349 // A compartor to sort output sections. Returns -1 or 1 if
350 // A or B are mentioned in linker script. Otherwise, returns 0.
351 template <class ELFT>
352 int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
353   int I = getSectionIndex(A);
354   int J = getSectionIndex(B);
355   if (I == INT_MAX && J == INT_MAX)
356     return 0;
357   return I < J ? -1 : 1;
358 }
359 
360 template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() {
361   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
362     auto *Cmd = dyn_cast<SymbolAssignment>(Base.get());
363     if (!Cmd || Cmd->Name == ".")
364       continue;
365 
366     SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
367     // The semantic of PROVIDE is that of introducing a symbol only if
368     // it's not defined and there's at least a reference to it.
369     if ((!B && !Cmd->Provide) || (B && B->isUndefined()))
370       Symtab<ELFT>::X->addAbsolute(Cmd->Name,
371                                    Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
372     else
373       // Symbol already exists in symbol table. If it is provided
374       // then we can't override its value.
375       Cmd->Ignore = Cmd->Provide;
376   }
377 }
378 
379 template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
380   return !Opt.PhdrsCommands.empty();
381 }
382 
383 // Returns indices of ELF headers containing specific section, identified
384 // by Name. Each index is a zero based number of ELF header listed within
385 // PHDRS {} script block.
386 template <class ELFT>
387 std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
388   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
389     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
390     if (!Cmd || Cmd->Name != SectionName)
391       continue;
392 
393     std::vector<size_t> Ret;
394     for (StringRef PhdrName : Cmd->Phdrs)
395       Ret.push_back(getPhdrIndex(PhdrName));
396     return Ret;
397   }
398   return {};
399 }
400 
401 template <class ELFT>
402 size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
403   size_t I = 0;
404   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
405     if (Cmd.Name == PhdrName)
406       return I;
407     ++I;
408   }
409   error("section header '" + PhdrName + "' is not listed in PHDRS");
410   return 0;
411 }
412 
413 class elf::ScriptParser : public ScriptParserBase {
414   typedef void (ScriptParser::*Handler)();
415 
416 public:
417   ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
418 
419   void run();
420 
421 private:
422   void addFile(StringRef Path);
423 
424   void readAsNeeded();
425   void readEntry();
426   void readExtern();
427   std::unique_ptr<InputSectionDescription> readFilePattern();
428   void readGroup();
429   void readKeep(OutputSectionCommand *Cmd);
430   void readInclude();
431   void readNothing() {}
432   void readOutput();
433   void readOutputArch();
434   void readOutputFormat();
435   void readPhdrs();
436   void readSearchDir();
437   void readSections();
438 
439   SymbolAssignment *readAssignment(StringRef Name);
440   void readOutputSectionDescription(StringRef OutSec);
441   std::vector<StringRef> readOutputSectionPhdrs();
442   unsigned readPhdrType();
443   void readProvide(bool Hidden);
444   void readAlign(OutputSectionCommand *Cmd);
445 
446   Expr readExpr();
447   Expr readExpr1(Expr Lhs, int MinPrec);
448   Expr readPrimary();
449   Expr readTernary(Expr Cond);
450   Expr combine(StringRef Op, Expr Lhs, Expr Rhs);
451 
452   const static StringMap<Handler> Cmd;
453   ScriptConfiguration &Opt = *ScriptConfig;
454   StringSaver Saver = {ScriptConfig->Alloc};
455   bool IsUnderSysroot;
456 };
457 
458 const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
459     {"ENTRY", &ScriptParser::readEntry},
460     {"EXTERN", &ScriptParser::readExtern},
461     {"GROUP", &ScriptParser::readGroup},
462     {"INCLUDE", &ScriptParser::readInclude},
463     {"INPUT", &ScriptParser::readGroup},
464     {"OUTPUT", &ScriptParser::readOutput},
465     {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
466     {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
467     {"PHDRS", &ScriptParser::readPhdrs},
468     {"SEARCH_DIR", &ScriptParser::readSearchDir},
469     {"SECTIONS", &ScriptParser::readSections},
470     {";", &ScriptParser::readNothing}};
471 
472 void ScriptParser::run() {
473   while (!atEOF()) {
474     StringRef Tok = next();
475     if (Handler Fn = Cmd.lookup(Tok))
476       (this->*Fn)();
477     else
478       setError("unknown directive: " + Tok);
479   }
480 }
481 
482 void ScriptParser::addFile(StringRef S) {
483   if (IsUnderSysroot && S.startswith("/")) {
484     SmallString<128> Path;
485     (Config->Sysroot + S).toStringRef(Path);
486     if (sys::fs::exists(Path)) {
487       Driver->addFile(Saver.save(Path.str()));
488       return;
489     }
490   }
491 
492   if (sys::path::is_absolute(S)) {
493     Driver->addFile(S);
494   } else if (S.startswith("=")) {
495     if (Config->Sysroot.empty())
496       Driver->addFile(S.substr(1));
497     else
498       Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
499   } else if (S.startswith("-l")) {
500     Driver->addLibrary(S.substr(2));
501   } else if (sys::fs::exists(S)) {
502     Driver->addFile(S);
503   } else {
504     std::string Path = findFromSearchPaths(S);
505     if (Path.empty())
506       setError("unable to find " + S);
507     else
508       Driver->addFile(Saver.save(Path));
509   }
510 }
511 
512 void ScriptParser::readAsNeeded() {
513   expect("(");
514   bool Orig = Config->AsNeeded;
515   Config->AsNeeded = true;
516   while (!Error) {
517     StringRef Tok = next();
518     if (Tok == ")")
519       break;
520     addFile(Tok);
521   }
522   Config->AsNeeded = Orig;
523 }
524 
525 void ScriptParser::readEntry() {
526   // -e <symbol> takes predecence over ENTRY(<symbol>).
527   expect("(");
528   StringRef Tok = next();
529   if (Config->Entry.empty())
530     Config->Entry = Tok;
531   expect(")");
532 }
533 
534 void ScriptParser::readExtern() {
535   expect("(");
536   while (!Error) {
537     StringRef Tok = next();
538     if (Tok == ")")
539       return;
540     Config->Undefined.push_back(Tok);
541   }
542 }
543 
544 void ScriptParser::readGroup() {
545   expect("(");
546   while (!Error) {
547     StringRef Tok = next();
548     if (Tok == ")")
549       return;
550     if (Tok == "AS_NEEDED") {
551       readAsNeeded();
552       continue;
553     }
554     addFile(Tok);
555   }
556 }
557 
558 void ScriptParser::readInclude() {
559   StringRef Tok = next();
560   auto MBOrErr = MemoryBuffer::getFile(Tok);
561   if (!MBOrErr) {
562     setError("cannot open " + Tok);
563     return;
564   }
565   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
566   StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
567   std::vector<StringRef> V = tokenize(S);
568   Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
569 }
570 
571 void ScriptParser::readOutput() {
572   // -o <file> takes predecence over OUTPUT(<file>).
573   expect("(");
574   StringRef Tok = next();
575   if (Config->OutputFile.empty())
576     Config->OutputFile = Tok;
577   expect(")");
578 }
579 
580 void ScriptParser::readOutputArch() {
581   // Error checking only for now.
582   expect("(");
583   next();
584   expect(")");
585 }
586 
587 void ScriptParser::readOutputFormat() {
588   // Error checking only for now.
589   expect("(");
590   next();
591   StringRef Tok = next();
592   if (Tok == ")")
593    return;
594   if (Tok != ",") {
595     setError("unexpected token: " + Tok);
596     return;
597   }
598   next();
599   expect(",");
600   next();
601   expect(")");
602 }
603 
604 void ScriptParser::readPhdrs() {
605   expect("{");
606   while (!Error && !skip("}")) {
607     StringRef Tok = next();
608     Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX});
609     PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
610 
611     PhdrCmd.Type = readPhdrType();
612     do {
613       Tok = next();
614       if (Tok == ";")
615         break;
616       if (Tok == "FILEHDR")
617         PhdrCmd.HasFilehdr = true;
618       else if (Tok == "PHDRS")
619         PhdrCmd.HasPhdrs = true;
620       else if (Tok == "FLAGS") {
621         expect("(");
622         next().getAsInteger(0, PhdrCmd.Flags);
623         expect(")");
624       } else
625         setError("unexpected header attribute: " + Tok);
626     } while (!Error);
627   }
628 }
629 
630 void ScriptParser::readSearchDir() {
631   expect("(");
632   Config->SearchPaths.push_back(next());
633   expect(")");
634 }
635 
636 void ScriptParser::readSections() {
637   Opt.DoLayout = true;
638   expect("{");
639   while (!Error && !skip("}")) {
640     StringRef Tok = next();
641     if (peek() == "=") {
642       readAssignment(Tok);
643       expect(";");
644     } else if (Tok == "PROVIDE") {
645       readProvide(false);
646     } else if (Tok == "PROVIDE_HIDDEN") {
647       readProvide(true);
648     } else {
649       readOutputSectionDescription(Tok);
650     }
651   }
652 }
653 
654 static int precedence(StringRef Op) {
655   return StringSwitch<int>(Op)
656       .Case("*", 4)
657       .Case("/", 4)
658       .Case("+", 3)
659       .Case("-", 3)
660       .Case("<", 2)
661       .Case(">", 2)
662       .Case(">=", 2)
663       .Case("<=", 2)
664       .Case("==", 2)
665       .Case("!=", 2)
666       .Case("&", 1)
667       .Default(-1);
668 }
669 
670 std::unique_ptr<InputSectionDescription> ScriptParser::readFilePattern() {
671   expect("*");
672   expect("(");
673 
674   auto InCmd = llvm::make_unique<InputSectionDescription>();
675 
676   if (skip("EXCLUDE_FILE")) {
677     expect("(");
678     while (!Error && !skip(")"))
679       InCmd->ExcludedFiles.push_back(next());
680     InCmd->Patterns.push_back(next());
681     expect(")");
682   } else {
683     while (!Error && !skip(")"))
684       InCmd->Patterns.push_back(next());
685   }
686   return InCmd;
687 }
688 
689 void ScriptParser::readKeep(OutputSectionCommand *Cmd) {
690   expect("(");
691   std::unique_ptr<InputSectionDescription> InCmd = readFilePattern();
692   Opt.KeptSections.insert(Opt.KeptSections.end(), InCmd->Patterns.begin(),
693                           InCmd->Patterns.end());
694   Cmd->Commands.push_back(std::move(InCmd));
695   expect(")");
696 }
697 
698 void ScriptParser::readAlign(OutputSectionCommand *Cmd) {
699   expect("(");
700   Cmd->AlignExpr = readExpr();
701   expect(")");
702 }
703 
704 void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
705   OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
706   Opt.Commands.emplace_back(Cmd);
707 
708   // Read an address expression.
709   // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
710   if (peek() != ":")
711     Cmd->AddrExpr = readExpr();
712 
713   expect(":");
714 
715   if (skip("ALIGN"))
716     readAlign(Cmd);
717 
718   // Parse constraints.
719   if (skip("ONLY_IF_RO"))
720     Cmd->Constraint = ConstraintKind::ReadOnly;
721   if (skip("ONLY_IF_RW"))
722     Cmd->Constraint = ConstraintKind::ReadWrite;
723   expect("{");
724 
725   while (!Error && !skip("}")) {
726     StringRef Tok = next();
727     if (Tok == "*") {
728       auto *InCmd = new InputSectionDescription();
729       Cmd->Commands.emplace_back(InCmd);
730       expect("(");
731       while (!Error && !skip(")"))
732         InCmd->Patterns.push_back(next());
733     } else if (Tok == "KEEP") {
734       readKeep(Cmd);
735     } else if (Tok == "PROVIDE") {
736       readProvide(false);
737     } else if (Tok == "PROVIDE_HIDDEN") {
738       readProvide(true);
739     } else {
740       setError("unknown command " + Tok);
741     }
742   }
743   Cmd->Phdrs = readOutputSectionPhdrs();
744 
745   StringRef Tok = peek();
746   if (Tok.startswith("=")) {
747     if (!Tok.startswith("=0x")) {
748       setError("filler should be a hexadecimal value");
749       return;
750     }
751     Tok = Tok.substr(3);
752     Cmd->Filler = parseHex(Tok);
753     next();
754   }
755 }
756 
757 void ScriptParser::readProvide(bool Hidden) {
758   expect("(");
759   if (SymbolAssignment *Assignment = readAssignment(next())) {
760     Assignment->Provide = true;
761     Assignment->Hidden = Hidden;
762   }
763   expect(")");
764   expect(";");
765 }
766 
767 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
768   expect("=");
769   Expr E = readExpr();
770   auto *Cmd = new SymbolAssignment(Name, E);
771   Opt.Commands.emplace_back(Cmd);
772   return Cmd;
773 }
774 
775 // This is an operator-precedence parser to parse a linker
776 // script expression.
777 Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
778 
779 static uint64_t getSymbolValue(StringRef S) {
780   switch (Config->EKind) {
781   case ELF32LEKind:
782     if (SymbolBody *B = Symtab<ELF32LE>::X->find(S))
783       return B->getVA<ELF32LE>();
784     break;
785   case ELF32BEKind:
786     if (SymbolBody *B = Symtab<ELF32BE>::X->find(S))
787       return B->getVA<ELF32BE>();
788     break;
789   case ELF64LEKind:
790     if (SymbolBody *B = Symtab<ELF64LE>::X->find(S))
791       return B->getVA<ELF64LE>();
792     break;
793   case ELF64BEKind:
794     if (SymbolBody *B = Symtab<ELF64BE>::X->find(S))
795       return B->getVA<ELF64BE>();
796     break;
797   default:
798     llvm_unreachable("unsupported target");
799   }
800   error("symbol not found: " + S);
801   return 0;
802 }
803 
804 // This is a part of the operator-precedence parser. This function
805 // assumes that the remaining token stream starts with an operator.
806 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
807   while (!atEOF() && !Error) {
808     // Read an operator and an expression.
809     StringRef Op1 = peek();
810     if (Op1 == "?")
811       return readTernary(Lhs);
812     if (precedence(Op1) < MinPrec)
813       break;
814     next();
815     Expr Rhs = readPrimary();
816 
817     // Evaluate the remaining part of the expression first if the
818     // next operator has greater precedence than the previous one.
819     // For example, if we have read "+" and "3", and if the next
820     // operator is "*", then we'll evaluate 3 * ... part first.
821     while (!atEOF()) {
822       StringRef Op2 = peek();
823       if (precedence(Op2) <= precedence(Op1))
824         break;
825       Rhs = readExpr1(Rhs, precedence(Op2));
826     }
827 
828     Lhs = combine(Op1, Lhs, Rhs);
829   }
830   return Lhs;
831 }
832 
833 uint64_t static getConstant(StringRef S) {
834   if (S == "COMMONPAGESIZE" || S == "MAXPAGESIZE")
835     return Target->PageSize;
836   error("unknown constant: " + S);
837   return 0;
838 }
839 
840 Expr ScriptParser::readPrimary() {
841   StringRef Tok = next();
842 
843   if (Tok == ".")
844     return [](uint64_t Dot) { return Dot; };
845 
846   if (Tok == "(") {
847     Expr E = readExpr();
848     expect(")");
849     return E;
850   }
851 
852   // Built-in functions are parsed here.
853   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
854   if (Tok == "ALIGN") {
855     expect("(");
856     Expr E = readExpr();
857     expect(")");
858     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
859   }
860   if (Tok == "CONSTANT") {
861     expect("(");
862     StringRef Tok = next();
863     expect(")");
864     return [=](uint64_t Dot) { return getConstant(Tok); };
865   }
866   if (Tok == "DATA_SEGMENT_ALIGN") {
867     expect("(");
868     Expr E = readExpr();
869     expect(",");
870     readExpr();
871     expect(")");
872     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
873   }
874   if (Tok == "DATA_SEGMENT_END") {
875     expect("(");
876     expect(".");
877     expect(")");
878     return [](uint64_t Dot) { return Dot; };
879   }
880   // GNU linkers implements more complicated logic to handle
881   // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
882   // the next page boundary for simplicity.
883   if (Tok == "DATA_SEGMENT_RELRO_END") {
884     expect("(");
885     next();
886     expect(",");
887     readExpr();
888     expect(")");
889     return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
890   }
891 
892   // Parse a symbol name or a number literal.
893   uint64_t V = 0;
894   if (Tok.getAsInteger(0, V)) {
895     if (!isValidCIdentifier(Tok))
896       setError("malformed number: " + Tok);
897     return [=](uint64_t Dot) { return getSymbolValue(Tok); };
898   }
899   return [=](uint64_t Dot) { return V; };
900 }
901 
902 Expr ScriptParser::readTernary(Expr Cond) {
903   next();
904   Expr L = readExpr();
905   expect(":");
906   Expr R = readExpr();
907   return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
908 }
909 
910 Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) {
911   if (Op == "*")
912     return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
913   if (Op == "/") {
914     return [=](uint64_t Dot) -> uint64_t {
915       uint64_t RHS = R(Dot);
916       if (RHS == 0) {
917         error("division by zero");
918         return 0;
919       }
920       return L(Dot) / RHS;
921     };
922   }
923   if (Op == "+")
924     return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
925   if (Op == "-")
926     return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
927   if (Op == "<")
928     return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
929   if (Op == ">")
930     return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
931   if (Op == ">=")
932     return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
933   if (Op == "<=")
934     return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
935   if (Op == "==")
936     return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
937   if (Op == "!=")
938     return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
939   if (Op == "&")
940     return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
941   llvm_unreachable("invalid operator");
942 }
943 
944 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
945   std::vector<StringRef> Phdrs;
946   while (!Error && peek().startswith(":")) {
947     StringRef Tok = next();
948     Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
949     if (Tok.empty()) {
950       setError("section header name is empty");
951       break;
952     }
953     Phdrs.push_back(Tok);
954   }
955   return Phdrs;
956 }
957 
958 unsigned ScriptParser::readPhdrType() {
959   StringRef Tok = next();
960   unsigned Ret = StringSwitch<unsigned>(Tok)
961       .Case("PT_NULL", PT_NULL)
962       .Case("PT_LOAD", PT_LOAD)
963       .Case("PT_DYNAMIC", PT_DYNAMIC)
964       .Case("PT_INTERP", PT_INTERP)
965       .Case("PT_NOTE", PT_NOTE)
966       .Case("PT_SHLIB", PT_SHLIB)
967       .Case("PT_PHDR", PT_PHDR)
968       .Case("PT_TLS", PT_TLS)
969       .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
970       .Case("PT_GNU_STACK", PT_GNU_STACK)
971       .Case("PT_GNU_RELRO", PT_GNU_RELRO)
972       .Default(-1);
973 
974   if (Ret == (unsigned)-1) {
975     setError("invalid program header type: " + Tok);
976     return PT_NULL;
977   }
978   return Ret;
979 }
980 
981 static bool isUnderSysroot(StringRef Path) {
982   if (Config->Sysroot == "")
983     return false;
984   for (; !Path.empty(); Path = sys::path::parent_path(Path))
985     if (sys::fs::equivalent(Config->Sysroot, Path))
986       return true;
987   return false;
988 }
989 
990 // Entry point.
991 void elf::readLinkerScript(MemoryBufferRef MB) {
992   StringRef Path = MB.getBufferIdentifier();
993   ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
994 }
995 
996 template class elf::LinkerScript<ELF32LE>;
997 template class elf::LinkerScript<ELF32BE>;
998 template class elf::LinkerScript<ELF64LE>;
999 template class elf::LinkerScript<ELF64BE>;
1000