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 does not construct an AST but consume linker script directives directly.
12 // Results are written to Driver or Config object.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "LinkerScript.h"
17 #include "Config.h"
18 #include "Driver.h"
19 #include "InputSection.h"
20 #include "OutputSections.h"
21 #include "ScriptParser.h"
22 #include "SymbolTable.h"
23 #include "llvm/ADT/StringSwitch.h"
24 #include "llvm/Support/ELF.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/StringSaver.h"
29 
30 using namespace llvm;
31 using namespace llvm::ELF;
32 using namespace llvm::object;
33 using namespace lld;
34 using namespace lld::elf;
35 
36 ScriptConfiguration *elf::ScriptConfig;
37 
38 static bool matchStr(StringRef S, StringRef T);
39 
40 // This is an operator-precedence parser to parse and evaluate
41 // a linker script expression. For each linker script arithmetic
42 // expression (e.g. ". = . + 0x1000"), a new instance of ExprParser
43 // is created and ran.
44 namespace {
45 class ExprParser : public ScriptParserBase {
46 public:
47   ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot)
48       : ScriptParserBase(Tokens), Dot(Dot) {}
49 
50   uint64_t run();
51 
52 private:
53   uint64_t parsePrimary();
54   uint64_t parseTernary(uint64_t Cond);
55   uint64_t apply(StringRef Op, uint64_t L, uint64_t R);
56   uint64_t parseExpr1(uint64_t Lhs, int MinPrec);
57   uint64_t parseExpr();
58 
59   uint64_t Dot;
60 };
61 }
62 
63 static int precedence(StringRef Op) {
64   return StringSwitch<int>(Op)
65       .Case("*", 4)
66       .Case("/", 4)
67       .Case("+", 3)
68       .Case("-", 3)
69       .Case("<", 2)
70       .Case(">", 2)
71       .Case(">=", 2)
72       .Case("<=", 2)
73       .Case("==", 2)
74       .Case("!=", 2)
75       .Case("&", 1)
76       .Default(-1);
77 }
78 
79 static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) {
80   return ExprParser(Tokens, Dot).run();
81 }
82 
83 uint64_t ExprParser::run() {
84   uint64_t V = parseExpr();
85   if (!atEOF() && !Error)
86     setError("stray token: " + peek());
87   return V;
88 }
89 
90 // This is a part of the operator-precedence parser to evaluate
91 // arithmetic expressions in SECTIONS command. This function evaluates an
92 // integer literal, a parenthesized expression, the ALIGN function,
93 // or the special variable ".".
94 uint64_t ExprParser::parsePrimary() {
95   StringRef Tok = next();
96   if (Tok == ".")
97     return Dot;
98   if (Tok == "(") {
99     uint64_t V = parseExpr();
100     expect(")");
101     return V;
102   }
103   if (Tok == "ALIGN") {
104     expect("(");
105     uint64_t V = parseExpr();
106     expect(")");
107     return alignTo(Dot, V);
108   }
109   uint64_t V = 0;
110   if (Tok.getAsInteger(0, V))
111     setError("malformed number: " + Tok);
112   return V;
113 }
114 
115 uint64_t ExprParser::parseTernary(uint64_t Cond) {
116   next();
117   uint64_t V = parseExpr();
118   expect(":");
119   uint64_t W = parseExpr();
120   return Cond ? V : W;
121 }
122 
123 uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) {
124   if (Op == "*")
125     return L * R;
126   if (Op == "/") {
127     if (R == 0) {
128       error("division by zero");
129       return 0;
130     }
131     return L / R;
132   }
133   if (Op == "+")
134     return L + R;
135   if (Op == "-")
136     return L - R;
137   if (Op == "<")
138     return L < R;
139   if (Op == ">")
140     return L > R;
141   if (Op == ">=")
142     return L >= R;
143   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   llvm_unreachable("invalid operator");
152   return 0;
153 }
154 
155 // This is a part of the operator-precedence parser.
156 // This function assumes that the remaining token stream starts
157 // with an operator.
158 uint64_t ExprParser::parseExpr1(uint64_t Lhs, int MinPrec) {
159   while (!atEOF()) {
160     // Read an operator and an expression.
161     StringRef Op1 = peek();
162     if (Op1 == "?")
163       return parseTernary(Lhs);
164     if (precedence(Op1) < MinPrec)
165       return Lhs;
166     next();
167     uint64_t Rhs = parsePrimary();
168 
169     // Evaluate the remaining part of the expression first if the
170     // next operator has greater precedence than the previous one.
171     // For example, if we have read "+" and "3", and if the next
172     // operator is "*", then we'll evaluate 3 * ... part first.
173     while (!atEOF()) {
174       StringRef Op2 = peek();
175       if (precedence(Op2) <= precedence(Op1))
176         break;
177       Rhs = parseExpr1(Rhs, precedence(Op2));
178     }
179 
180     Lhs = apply(Op1, Lhs, Rhs);
181   }
182   return Lhs;
183 }
184 
185 // Reads and evaluates an arithmetic expression.
186 uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); }
187 
188 template <class ELFT>
189 StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
190   for (SectionRule &R : Opt.Sections)
191     if (matchStr(R.SectionPattern, S->getSectionName()))
192       return R.Dest;
193   return "";
194 }
195 
196 template <class ELFT>
197 bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
198   return getOutputSection(S) == "/DISCARD/";
199 }
200 
201 template <class ELFT>
202 bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
203   for (StringRef Pat : Opt.KeptSections)
204     if (matchStr(Pat, S->getSectionName()))
205       return true;
206   return false;
207 }
208 
209 template <class ELFT>
210 static OutputSectionBase<ELFT> *
211 findSection(ArrayRef<OutputSectionBase<ELFT> *> V, StringRef Name) {
212   for (OutputSectionBase<ELFT> *Sec : V)
213     if (Sec->getName() == Name)
214       return Sec;
215   return nullptr;
216 }
217 
218 template <class ELFT>
219 void LinkerScript<ELFT>::assignAddresses(
220     ArrayRef<OutputSectionBase<ELFT> *> Sections) {
221   // Orphan sections are sections present in the input files which
222   // are not explicitly placed into the output file by the linker script.
223   // We place orphan sections at end of file.
224   // Other linkers places them using some heuristics as described in
225   // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
226   for (OutputSectionBase<ELFT> *Sec : Sections) {
227     StringRef Name = Sec->getName();
228     if (getSectionIndex(Name) == INT_MAX)
229       Opt.Commands.push_back({SectionKind, {}, Name});
230   }
231 
232   // Assign addresses as instructed by linker script SECTIONS sub-commands.
233   Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
234   uintX_t ThreadBssOffset = 0;
235 
236   for (SectionsCommand &Cmd : Opt.Commands) {
237     if (Cmd.Kind == ExprKind) {
238       Dot = evalExpr(Cmd.Expr, Dot);
239       continue;
240     }
241 
242     OutputSectionBase<ELFT> *Sec = findSection<ELFT>(Sections, Cmd.SectionName);
243     if (!Sec)
244       continue;
245 
246     if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
247       uintX_t TVA = Dot + ThreadBssOffset;
248       TVA = alignTo(TVA, Sec->getAlign());
249       Sec->setVA(TVA);
250       ThreadBssOffset = TVA - Dot + Sec->getSize();
251       continue;
252     }
253 
254     if (Sec->getFlags() & SHF_ALLOC) {
255       Dot = alignTo(Dot, Sec->getAlign());
256       Sec->setVA(Dot);
257       Dot += Sec->getSize();
258       continue;
259     }
260   }
261 }
262 
263 template <class ELFT>
264 ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
265   auto I = Opt.Filler.find(Name);
266   if (I == Opt.Filler.end())
267     return {};
268   return I->second;
269 }
270 
271 // Returns the index of the given section name in linker script
272 // SECTIONS commands. Sections are laid out as the same order as they
273 // were in the script. If a given name did not appear in the script,
274 // it returns INT_MAX, so that it will be laid out at end of file.
275 template <class ELFT>
276 int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
277   auto Begin = Opt.Commands.begin();
278   auto End = Opt.Commands.end();
279   auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
280     return N.Kind == SectionKind && N.SectionName == Name;
281   });
282   return I == End ? INT_MAX : (I - Begin);
283 }
284 
285 // A compartor to sort output sections. Returns -1 or 1 if
286 // A or B are mentioned in linker script. Otherwise, returns 0.
287 template <class ELFT>
288 int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
289   int I = getSectionIndex(A);
290   int J = getSectionIndex(B);
291   if (I == INT_MAX && J == INT_MAX)
292     return 0;
293   return I < J ? -1 : 1;
294 }
295 
296 // Returns true if S matches T. S can contain glob meta-characters.
297 // The asterisk ('*') matches zero or more characacters, and the question
298 // mark ('?') matches one character.
299 static bool matchStr(StringRef S, StringRef T) {
300   for (;;) {
301     if (S.empty())
302       return T.empty();
303     if (S[0] == '*') {
304       S = S.substr(1);
305       if (S.empty())
306         // Fast path. If a pattern is '*', it matches anything.
307         return true;
308       for (size_t I = 0, E = T.size(); I < E; ++I)
309         if (matchStr(S, T.substr(I)))
310           return true;
311       return false;
312     }
313     if (T.empty() || (S[0] != T[0] && S[0] != '?'))
314       return false;
315     S = S.substr(1);
316     T = T.substr(1);
317   }
318 }
319 
320 class elf::ScriptParser : public ScriptParserBase {
321   typedef void (ScriptParser::*Handler)();
322 
323 public:
324   ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
325 
326   void run();
327 
328 private:
329   void addFile(StringRef Path);
330 
331   void readAsNeeded();
332   void readEntry();
333   void readExtern();
334   void readGroup();
335   void readInclude();
336   void readNothing() {}
337   void readOutput();
338   void readOutputArch();
339   void readOutputFormat();
340   void readSearchDir();
341   void readSections();
342 
343   void readLocationCounterValue();
344   void readOutputSectionDescription();
345 
346   const static StringMap<Handler> Cmd;
347   ScriptConfiguration &Opt = *ScriptConfig;
348   StringSaver Saver = {ScriptConfig->Alloc};
349   bool IsUnderSysroot;
350 };
351 
352 const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
353     {"ENTRY", &ScriptParser::readEntry},
354     {"EXTERN", &ScriptParser::readExtern},
355     {"GROUP", &ScriptParser::readGroup},
356     {"INCLUDE", &ScriptParser::readInclude},
357     {"INPUT", &ScriptParser::readGroup},
358     {"OUTPUT", &ScriptParser::readOutput},
359     {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
360     {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
361     {"SEARCH_DIR", &ScriptParser::readSearchDir},
362     {"SECTIONS", &ScriptParser::readSections},
363     {";", &ScriptParser::readNothing}};
364 
365 void ScriptParser::run() {
366   while (!atEOF()) {
367     StringRef Tok = next();
368     if (Handler Fn = Cmd.lookup(Tok))
369       (this->*Fn)();
370     else
371       setError("unknown directive: " + Tok);
372   }
373 }
374 
375 void ScriptParser::addFile(StringRef S) {
376   if (IsUnderSysroot && S.startswith("/")) {
377     SmallString<128> Path;
378     (Config->Sysroot + S).toStringRef(Path);
379     if (sys::fs::exists(Path)) {
380       Driver->addFile(Saver.save(Path.str()));
381       return;
382     }
383   }
384 
385   if (sys::path::is_absolute(S)) {
386     Driver->addFile(S);
387   } else if (S.startswith("=")) {
388     if (Config->Sysroot.empty())
389       Driver->addFile(S.substr(1));
390     else
391       Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
392   } else if (S.startswith("-l")) {
393     Driver->addLibrary(S.substr(2));
394   } else if (sys::fs::exists(S)) {
395     Driver->addFile(S);
396   } else {
397     std::string Path = findFromSearchPaths(S);
398     if (Path.empty())
399       setError("unable to find " + S);
400     else
401       Driver->addFile(Saver.save(Path));
402   }
403 }
404 
405 void ScriptParser::readAsNeeded() {
406   expect("(");
407   bool Orig = Config->AsNeeded;
408   Config->AsNeeded = true;
409   while (!Error) {
410     StringRef Tok = next();
411     if (Tok == ")")
412       break;
413     addFile(Tok);
414   }
415   Config->AsNeeded = Orig;
416 }
417 
418 void ScriptParser::readEntry() {
419   // -e <symbol> takes predecence over ENTRY(<symbol>).
420   expect("(");
421   StringRef Tok = next();
422   if (Config->Entry.empty())
423     Config->Entry = Tok;
424   expect(")");
425 }
426 
427 void ScriptParser::readExtern() {
428   expect("(");
429   while (!Error) {
430     StringRef Tok = next();
431     if (Tok == ")")
432       return;
433     Config->Undefined.push_back(Tok);
434   }
435 }
436 
437 void ScriptParser::readGroup() {
438   expect("(");
439   while (!Error) {
440     StringRef Tok = next();
441     if (Tok == ")")
442       return;
443     if (Tok == "AS_NEEDED") {
444       readAsNeeded();
445       continue;
446     }
447     addFile(Tok);
448   }
449 }
450 
451 void ScriptParser::readInclude() {
452   StringRef Tok = next();
453   auto MBOrErr = MemoryBuffer::getFile(Tok);
454   if (!MBOrErr) {
455     setError("cannot open " + Tok);
456     return;
457   }
458   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
459   StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
460   std::vector<StringRef> V = tokenize(S);
461   Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
462 }
463 
464 void ScriptParser::readOutput() {
465   // -o <file> takes predecence over OUTPUT(<file>).
466   expect("(");
467   StringRef Tok = next();
468   if (Config->OutputFile.empty())
469     Config->OutputFile = Tok;
470   expect(")");
471 }
472 
473 void ScriptParser::readOutputArch() {
474   // Error checking only for now.
475   expect("(");
476   next();
477   expect(")");
478 }
479 
480 void ScriptParser::readOutputFormat() {
481   // Error checking only for now.
482   expect("(");
483   next();
484   StringRef Tok = next();
485   if (Tok == ")")
486    return;
487   if (Tok != ",") {
488     setError("unexpected token: " + Tok);
489     return;
490   }
491   next();
492   expect(",");
493   next();
494   expect(")");
495 }
496 
497 void ScriptParser::readSearchDir() {
498   expect("(");
499   Config->SearchPaths.push_back(next());
500   expect(")");
501 }
502 
503 void ScriptParser::readSections() {
504   Opt.DoLayout = true;
505   expect("{");
506   while (!Error && !skip("}")) {
507     StringRef Tok = peek();
508     if (Tok == ".")
509       readLocationCounterValue();
510     else
511       readOutputSectionDescription();
512   }
513 }
514 
515 void ScriptParser::readLocationCounterValue() {
516   expect(".");
517   expect("=");
518   Opt.Commands.push_back({ExprKind, {}, ""});
519   SectionsCommand &Cmd = Opt.Commands.back();
520   while (!Error) {
521     StringRef Tok = next();
522     if (Tok == ";")
523       break;
524     Cmd.Expr.push_back(Tok);
525   }
526   if (Cmd.Expr.empty())
527     error("error in location counter expression");
528 }
529 
530 void ScriptParser::readOutputSectionDescription() {
531   StringRef OutSec = next();
532   Opt.Commands.push_back({SectionKind, {}, OutSec});
533   expect(":");
534   expect("{");
535 
536   while (!Error && !skip("}")) {
537     StringRef Tok = next();
538     if (Tok == "*") {
539       expect("(");
540       while (!Error && !skip(")"))
541         Opt.Sections.emplace_back(OutSec, next());
542     } else if (Tok == "KEEP") {
543       expect("(");
544       expect("*");
545       expect("(");
546       while (!Error && !skip(")")) {
547         StringRef Sec = next();
548         Opt.Sections.emplace_back(OutSec, Sec);
549         Opt.KeptSections.push_back(Sec);
550       }
551       expect(")");
552     } else {
553       setError("unknown command " + Tok);
554     }
555   }
556 
557   StringRef Tok = peek();
558   if (Tok.startswith("=")) {
559     if (!Tok.startswith("=0x")) {
560       setError("filler should be a hexadecimal value");
561       return;
562     }
563     Tok = Tok.substr(3);
564     Opt.Filler[OutSec] = parseHex(Tok);
565     next();
566   }
567 }
568 
569 static bool isUnderSysroot(StringRef Path) {
570   if (Config->Sysroot == "")
571     return false;
572   for (; !Path.empty(); Path = sys::path::parent_path(Path))
573     if (sys::fs::equivalent(Config->Sysroot, Path))
574       return true;
575   return false;
576 }
577 
578 // Entry point.
579 void elf::readLinkerScript(MemoryBufferRef MB) {
580   StringRef Path = MB.getBufferIdentifier();
581   ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
582 }
583 
584 template class elf::LinkerScript<ELF32LE>;
585 template class elf::LinkerScript<ELF32BE>;
586 template class elf::LinkerScript<ELF64LE>;
587 template class elf::LinkerScript<ELF64BE>;
588