1 //===- ScriptParser.cpp ---------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a recursive-descendent parser for linker scripts.
11 // Parsed results are stored to Config and Script global objects.
13 //===----------------------------------------------------------------------===//
15 #include "ScriptParser.h"
18 #include "InputSection.h"
19 #include "LinkerScript.h"
21 #include "OutputSections.h"
22 #include "ScriptLexer.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/BinaryFormat/ELF.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/Path.h"
38 using namespace llvm::ELF;
39 using namespace llvm::support::endian;
41 using namespace lld::elf;
43 static bool isUnderSysroot(StringRef Path);
46 class ScriptParser final : ScriptLexer {
48 ScriptParser(MemoryBufferRef MB)
50 IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {}
52 void readLinkerScript();
53 void readVersionScript();
54 void readDynamicList();
57 void addFile(StringRef Path);
58 OutputSection *checkSection(OutputSectionCommand *Cmd, StringRef Loccation);
67 void readOutputArch();
68 void readOutputFormat();
73 void readVersionScriptCommand();
75 SymbolAssignment *readAssignment(StringRef Name);
76 BytesDataCommand *readBytesDataCommand(StringRef Tok);
78 uint32_t parseFill(StringRef Tok);
79 void readSectionAddressType(OutputSectionCommand *Cmd);
80 OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
81 std::vector<StringRef> readOutputSectionPhdrs();
82 InputSectionDescription *readInputSectionDescription(StringRef Tok);
83 StringMatcher readFilePatterns();
84 std::vector<SectionPattern> readInputSectionsList();
85 InputSectionDescription *readInputSectionRules(StringRef FilePattern);
86 unsigned readPhdrType();
87 SortSectionPolicy readSortKind();
88 SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
89 SymbolAssignment *readProvideOrAssignment(StringRef Tok);
91 AssertCommand *readAssert();
92 Expr readAssertExpr();
94 uint64_t readMemoryAssignment(StringRef, StringRef, StringRef);
95 std::pair<uint32_t, uint32_t> readMemoryAttributes();
98 Expr readExpr1(Expr Lhs, int MinPrec);
99 StringRef readParenLiteral();
101 Expr readTernary(Expr Cond);
102 Expr readParenExpr();
104 // For parsing version script.
105 std::vector<SymbolVersion> readVersionExtern();
106 void readAnonymousDeclaration();
107 void readVersionDeclaration(StringRef VerStr);
109 std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
116 static StringRef unquote(StringRef S) {
117 if (S.startswith("\""))
118 return S.substr(1, S.size() - 2);
122 static bool isUnderSysroot(StringRef Path) {
123 if (Config->Sysroot == "")
125 for (; !Path.empty(); Path = sys::path::parent_path(Path))
126 if (sys::fs::equivalent(Config->Sysroot, Path))
131 // Some operations only support one non absolute value. Move the
132 // absolute one to the right hand side for convenience.
133 static void moveAbsRight(ExprValue &A, ExprValue &B) {
137 error(A.Loc + ": at least one side of the expression must be absolute");
140 static ExprValue add(ExprValue A, ExprValue B) {
142 return {A.Sec, A.ForceAbsolute, A.Val + B.getValue(), A.Loc};
145 static ExprValue sub(ExprValue A, ExprValue B) {
146 return {A.Sec, A.Val - B.getValue(), A.Loc};
149 static ExprValue mul(ExprValue A, ExprValue B) {
150 return A.getValue() * B.getValue();
153 static ExprValue div(ExprValue A, ExprValue B) {
154 if (uint64_t BV = B.getValue())
155 return A.getValue() / BV;
156 error("division by zero");
160 static ExprValue bitAnd(ExprValue A, ExprValue B) {
162 return {A.Sec, A.ForceAbsolute,
163 (A.getValue() & B.getValue()) - A.getSecAddr(), A.Loc};
166 static ExprValue bitOr(ExprValue A, ExprValue B) {
168 return {A.Sec, A.ForceAbsolute,
169 (A.getValue() | B.getValue()) - A.getSecAddr(), A.Loc};
172 void ScriptParser::readDynamicList() {
174 readAnonymousDeclaration();
176 setError("EOF expected, but got " + next());
179 void ScriptParser::readVersionScript() {
180 readVersionScriptCommand();
182 setError("EOF expected, but got " + next());
185 void ScriptParser::readVersionScriptCommand() {
187 readAnonymousDeclaration();
191 while (!atEOF() && !Error && peek() != "}") {
192 StringRef VerStr = next();
194 setError("anonymous version definition is used in "
195 "combination with other version definitions");
199 readVersionDeclaration(VerStr);
203 void ScriptParser::readVersion() {
205 readVersionScriptCommand();
209 void ScriptParser::readLinkerScript() {
211 StringRef Tok = next();
215 if (Tok == "ASSERT") {
216 Script->Opt.Commands.push_back(readAssert());
217 } else if (Tok == "ENTRY") {
219 } else if (Tok == "EXTERN") {
221 } else if (Tok == "GROUP" || Tok == "INPUT") {
223 } else if (Tok == "INCLUDE") {
225 } else if (Tok == "MEMORY") {
227 } else if (Tok == "OUTPUT") {
229 } else if (Tok == "OUTPUT_ARCH") {
231 } else if (Tok == "OUTPUT_FORMAT") {
233 } else if (Tok == "PHDRS") {
235 } else if (Tok == "SEARCH_DIR") {
237 } else if (Tok == "SECTIONS") {
239 } else if (Tok == "VERSION") {
241 } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) {
242 Script->Opt.Commands.push_back(Cmd);
244 setError("unknown directive: " + Tok);
249 void ScriptParser::addFile(StringRef S) {
250 if (IsUnderSysroot && S.startswith("/")) {
251 SmallString<128> PathData;
252 StringRef Path = (Config->Sysroot + S).toStringRef(PathData);
253 if (sys::fs::exists(Path)) {
254 Driver->addFile(Saver.save(Path), /*WithLOption=*/false);
259 if (sys::path::is_absolute(S)) {
260 Driver->addFile(S, /*WithLOption=*/false);
261 } else if (S.startswith("=")) {
262 if (Config->Sysroot.empty())
263 Driver->addFile(S.substr(1), /*WithLOption=*/false);
265 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)),
266 /*WithLOption=*/false);
267 } else if (S.startswith("-l")) {
268 Driver->addLibrary(S.substr(2));
269 } else if (sys::fs::exists(S)) {
270 Driver->addFile(S, /*WithLOption=*/false);
272 if (Optional<std::string> Path = findFromSearchPaths(S))
273 Driver->addFile(Saver.save(*Path), /*WithLOption=*/true);
275 setError("unable to find " + S);
279 void ScriptParser::readAsNeeded() {
281 bool Orig = Config->AsNeeded;
282 Config->AsNeeded = true;
283 while (!Error && !consume(")"))
284 addFile(unquote(next()));
285 Config->AsNeeded = Orig;
288 void ScriptParser::readEntry() {
289 // -e <symbol> takes predecence over ENTRY(<symbol>).
291 StringRef Tok = next();
292 if (Config->Entry.empty())
297 void ScriptParser::readExtern() {
299 while (!Error && !consume(")"))
300 Config->Undefined.push_back(next());
303 void ScriptParser::readGroup() {
305 while (!Error && !consume(")")) {
306 if (consume("AS_NEEDED"))
309 addFile(unquote(next()));
313 void ScriptParser::readInclude() {
314 StringRef Tok = unquote(next());
316 // https://sourceware.org/binutils/docs/ld/File-Commands.html:
317 // The file will be searched for in the current directory, and in any
318 // directory specified with the -L option.
319 if (sys::fs::exists(Tok)) {
320 if (Optional<MemoryBufferRef> MB = readFile(Tok))
324 if (Optional<std::string> Path = findFromSearchPaths(Tok)) {
325 if (Optional<MemoryBufferRef> MB = readFile(*Path))
329 setError("cannot open " + Tok);
332 void ScriptParser::readOutput() {
333 // -o <file> takes predecence over OUTPUT(<file>).
335 StringRef Tok = next();
336 if (Config->OutputFile.empty())
337 Config->OutputFile = unquote(Tok);
341 void ScriptParser::readOutputArch() {
342 // OUTPUT_ARCH is ignored for now.
344 while (!Error && !consume(")"))
348 void ScriptParser::readOutputFormat() {
349 // Error checking only for now.
361 void ScriptParser::readPhdrs() {
363 while (!Error && !consume("}")) {
364 Script->Opt.PhdrsCommands.push_back(
365 {next(), PT_NULL, false, false, UINT_MAX, nullptr});
367 PhdrsCommand &PhdrCmd = Script->Opt.PhdrsCommands.back();
368 PhdrCmd.Type = readPhdrType();
370 while (!Error && !consume(";")) {
371 if (consume("FILEHDR"))
372 PhdrCmd.HasFilehdr = true;
373 else if (consume("PHDRS"))
374 PhdrCmd.HasPhdrs = true;
375 else if (consume("AT"))
376 PhdrCmd.LMAExpr = readParenExpr();
377 else if (consume("FLAGS"))
378 PhdrCmd.Flags = readParenExpr()().getValue();
380 setError("unexpected header attribute: " + next());
385 void ScriptParser::readSearchDir() {
387 StringRef Tok = next();
388 if (!Config->Nostdlib)
389 Config->SearchPaths.push_back(unquote(Tok));
393 void ScriptParser::readSections() {
394 Script->Opt.HasSections = true;
396 // -no-rosegment is used to avoid placing read only non-executable sections in
397 // their own segment. We do the same if SECTIONS command is present in linker
398 // script. See comment for computeFlags().
399 Config->SingleRoRx = true;
402 while (!Error && !consume("}")) {
403 StringRef Tok = next();
404 BaseCommand *Cmd = readProvideOrAssignment(Tok);
409 Cmd = readOutputSectionDescription(Tok);
411 Script->Opt.Commands.push_back(Cmd);
415 static int precedence(StringRef Op) {
416 return StringSwitch<int>(Op)
419 .Cases("<<", ">>", 3)
420 .Cases("<", "<=", ">", ">=", "==", "!=", 2)
425 StringMatcher ScriptParser::readFilePatterns() {
426 std::vector<StringRef> V;
427 while (!Error && !consume(")"))
429 return StringMatcher(V);
432 SortSectionPolicy ScriptParser::readSortKind() {
433 if (consume("SORT") || consume("SORT_BY_NAME"))
434 return SortSectionPolicy::Name;
435 if (consume("SORT_BY_ALIGNMENT"))
436 return SortSectionPolicy::Alignment;
437 if (consume("SORT_BY_INIT_PRIORITY"))
438 return SortSectionPolicy::Priority;
439 if (consume("SORT_NONE"))
440 return SortSectionPolicy::None;
441 return SortSectionPolicy::Default;
444 // Reads SECTIONS command contents in the following form:
446 // <contents> ::= <elem>*
447 // <elem> ::= <exclude>? <glob-pattern>
448 // <exclude> ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
452 // *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
454 // is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
455 // The semantics of that is section .foo in any file, section .bar in
456 // any file but a.o, and section .baz in any file but b.o.
457 std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
458 std::vector<SectionPattern> Ret;
459 while (!Error && peek() != ")") {
460 StringMatcher ExcludeFilePat;
461 if (consume("EXCLUDE_FILE")) {
463 ExcludeFilePat = readFilePatterns();
466 std::vector<StringRef> V;
467 while (!Error && peek() != ")" && peek() != "EXCLUDE_FILE")
471 Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)});
473 setError("section pattern is expected");
478 // Reads contents of "SECTIONS" directive. That directive contains a
479 // list of glob patterns for input sections. The grammar is as follows.
481 // <patterns> ::= <section-list>
482 // | <sort> "(" <section-list> ")"
483 // | <sort> "(" <sort> "(" <section-list> ")" ")"
485 // <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
486 // | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
488 // <section-list> is parsed by readInputSectionsList().
489 InputSectionDescription *
490 ScriptParser::readInputSectionRules(StringRef FilePattern) {
491 auto *Cmd = make<InputSectionDescription>(FilePattern);
494 while (!Error && !consume(")")) {
495 SortSectionPolicy Outer = readSortKind();
496 SortSectionPolicy Inner = SortSectionPolicy::Default;
497 std::vector<SectionPattern> V;
498 if (Outer != SortSectionPolicy::Default) {
500 Inner = readSortKind();
501 if (Inner != SortSectionPolicy::Default) {
503 V = readInputSectionsList();
506 V = readInputSectionsList();
510 V = readInputSectionsList();
513 for (SectionPattern &Pat : V) {
514 Pat.SortInner = Inner;
515 Pat.SortOuter = Outer;
518 std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
523 InputSectionDescription *
524 ScriptParser::readInputSectionDescription(StringRef Tok) {
525 // Input section wildcard can be surrounded by KEEP.
526 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
529 StringRef FilePattern = next();
530 InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
532 Script->Opt.KeptSections.push_back(Cmd);
535 return readInputSectionRules(Tok);
538 void ScriptParser::readSort() {
540 expect("CONSTRUCTORS");
544 AssertCommand *ScriptParser::readAssert() {
545 return make<AssertCommand>(readAssertExpr());
548 Expr ScriptParser::readAssertExpr() {
552 StringRef Msg = unquote(next());
558 return Script->getDot();
562 // Reads a FILL(expr) command. We handle the FILL command as an
563 // alias for =fillexp section attribute, which is different from
564 // what GNU linkers do.
565 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
566 uint32_t ScriptParser::readFill() {
568 uint32_t V = parseFill(next());
573 // Reads an expression and/or the special directive "(NOLOAD)" for an
574 // output section definition.
576 // An output section name can be followed by an address expression
577 // and/or by "(NOLOAD)". This grammar is not LL(1) because "(" can be
578 // interpreted as either the beginning of some expression or "(NOLOAD)".
580 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
581 // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
582 void ScriptParser::readSectionAddressType(OutputSectionCommand *Cmd) {
584 if (consume("NOLOAD")) {
589 Cmd->AddrExpr = readExpr();
592 Cmd->AddrExpr = readExpr();
602 OutputSectionCommand *
603 ScriptParser::readOutputSectionDescription(StringRef OutSec) {
604 OutputSectionCommand *Cmd =
605 Script->createOutputSectionCommand(OutSec, getCurrentLocation());
608 readSectionAddressType(Cmd);
612 Cmd->LMAExpr = readParenExpr();
613 if (consume("ALIGN"))
614 Cmd->AlignExpr = readParenExpr();
615 if (consume("SUBALIGN"))
616 Cmd->SubalignExpr = readParenExpr();
618 // Parse constraints.
619 if (consume("ONLY_IF_RO"))
620 Cmd->Constraint = ConstraintKind::ReadOnly;
621 if (consume("ONLY_IF_RW"))
622 Cmd->Constraint = ConstraintKind::ReadWrite;
625 while (!Error && !consume("}")) {
626 StringRef Tok = next();
628 // Empty commands are allowed. Do nothing here.
629 } else if (SymbolAssignment *Assign = readProvideOrAssignment(Tok)) {
630 Cmd->Commands.push_back(Assign);
631 } else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) {
632 Cmd->Commands.push_back(Data);
633 } else if (Tok == "ASSERT") {
634 Cmd->Commands.push_back(readAssert());
636 } else if (Tok == "CONSTRUCTORS") {
637 // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
638 // by name. This is for very old file formats such as ECOFF/XCOFF.
639 // For ELF, we should ignore.
640 } else if (Tok == "FILL") {
641 Cmd->Filler = readFill();
642 } else if (Tok == "SORT") {
644 } else if (peek() == "(") {
645 Cmd->Commands.push_back(readInputSectionDescription(Tok));
647 setError("unknown command " + Tok);
652 Cmd->MemoryRegionName = next();
654 Cmd->Phdrs = readOutputSectionPhdrs();
657 Cmd->Filler = parseFill(next());
658 else if (peek().startswith("="))
659 Cmd->Filler = parseFill(next().drop_front());
661 // Consume optional comma following output section command.
667 // Parses a given string as a octal/decimal/hexadecimal number and
668 // returns it as a big-endian number. Used for `=<fillexp>`.
669 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
671 // When reading a hexstring, ld.bfd handles it as a blob of arbitrary
672 // size, while ld.gold always handles it as a 32-bit big-endian number.
673 // We are compatible with ld.gold because it's easier to implement.
674 uint32_t ScriptParser::parseFill(StringRef Tok) {
676 if (!to_integer(Tok, V))
677 setError("invalid filler expression: " + Tok);
684 SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
686 SymbolAssignment *Cmd = readAssignment(next());
687 Cmd->Provide = Provide;
688 Cmd->Hidden = Hidden;
694 SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) {
695 SymbolAssignment *Cmd = nullptr;
696 if (peek() == "=" || peek() == "+=") {
697 Cmd = readAssignment(Tok);
699 } else if (Tok == "PROVIDE") {
700 Cmd = readProvideHidden(true, false);
701 } else if (Tok == "HIDDEN") {
702 Cmd = readProvideHidden(false, true);
703 } else if (Tok == "PROVIDE_HIDDEN") {
704 Cmd = readProvideHidden(true, true);
709 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
710 StringRef Op = next();
711 assert(Op == "=" || Op == "+=");
714 std::string Loc = getCurrentLocation();
715 E = [=] { return add(Script->getSymbolValue(Loc, Name), E()); };
717 return make<SymbolAssignment>(Name, E, getCurrentLocation());
720 // This is an operator-precedence parser to parse a linker
721 // script expression.
722 Expr ScriptParser::readExpr() {
723 // Our lexer is context-aware. Set the in-expression bit so that
724 // they apply different tokenization rules.
727 Expr E = readExpr1(readPrimary(), 0);
732 static Expr combine(StringRef Op, Expr L, Expr R) {
734 return [=] { return add(L(), R()); };
736 return [=] { return sub(L(), R()); };
738 return [=] { return mul(L(), R()); };
740 return [=] { return div(L(), R()); };
742 return [=] { return L().getValue() << R().getValue(); };
744 return [=] { return L().getValue() >> R().getValue(); };
746 return [=] { return L().getValue() < R().getValue(); };
748 return [=] { return L().getValue() > R().getValue(); };
750 return [=] { return L().getValue() >= R().getValue(); };
752 return [=] { return L().getValue() <= R().getValue(); };
754 return [=] { return L().getValue() == R().getValue(); };
756 return [=] { return L().getValue() != R().getValue(); };
758 return [=] { return bitAnd(L(), R()); };
760 return [=] { return bitOr(L(), R()); };
761 llvm_unreachable("invalid operator");
764 // This is a part of the operator-precedence parser. This function
765 // assumes that the remaining token stream starts with an operator.
766 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
767 while (!atEOF() && !Error) {
768 // Read an operator and an expression.
770 return readTernary(Lhs);
771 StringRef Op1 = peek();
772 if (precedence(Op1) < MinPrec)
775 Expr Rhs = readPrimary();
777 // Evaluate the remaining part of the expression first if the
778 // next operator has greater precedence than the previous one.
779 // For example, if we have read "+" and "3", and if the next
780 // operator is "*", then we'll evaluate 3 * ... part first.
782 StringRef Op2 = peek();
783 if (precedence(Op2) <= precedence(Op1))
785 Rhs = readExpr1(Rhs, precedence(Op2));
788 Lhs = combine(Op1, Lhs, Rhs);
793 uint64_t static getConstant(StringRef S) {
794 if (S == "COMMONPAGESIZE")
795 return Target->PageSize;
796 if (S == "MAXPAGESIZE")
797 return Config->MaxPageSize;
798 error("unknown constant: " + S);
802 // Parses Tok as an integer. It recognizes hexadecimal (prefixed with
803 // "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
804 // have "K" (Ki) or "M" (Mi) suffixes.
805 static Optional<uint64_t> parseInt(StringRef Tok) {
807 if (Tok.startswith("-")) {
808 if (Optional<uint64_t> Val = parseInt(Tok.substr(1)))
815 if (Tok.startswith_lower("0x") && to_integer(Tok.substr(2), Val, 16))
817 if (Tok.endswith_lower("H") && to_integer(Tok.drop_back(), Val, 16))
821 if (Tok.endswith_lower("K")) {
822 if (!to_integer(Tok.drop_back(), Val, 10))
826 if (Tok.endswith_lower("M")) {
827 if (!to_integer(Tok.drop_back(), Val, 10))
829 return Val * 1024 * 1024;
831 if (!to_integer(Tok, Val, 10))
836 BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) {
837 int Size = StringSwitch<int>(Tok)
846 return make<BytesDataCommand>(readParenExpr(), Size);
849 StringRef ScriptParser::readParenLiteral() {
851 StringRef Tok = next();
856 OutputSection *ScriptParser::checkSection(OutputSectionCommand *Cmd,
857 StringRef Location) {
858 if (Cmd->Location.empty() && Script->ErrorOnMissingSection)
859 error(Location + ": undefined section " + Cmd->Name);
862 static OutputSection Dummy("", 0, 0);
866 Expr ScriptParser::readPrimary() {
868 return readParenExpr();
871 Expr E = readPrimary();
872 return [=] { return ~E().getValue(); };
875 Expr E = readPrimary();
876 return [=] { return -E().getValue(); };
879 StringRef Tok = next();
880 std::string Location = getCurrentLocation();
882 // Built-in functions are parsed here.
883 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
884 if (Tok == "ABSOLUTE") {
885 Expr Inner = readParenExpr();
887 ExprValue I = Inner();
888 I.ForceAbsolute = true;
893 StringRef Name = readParenLiteral();
894 OutputSectionCommand *Cmd = Script->getOrCreateOutputSectionCommand(Name);
895 return [=]() -> ExprValue {
896 return {checkSection(Cmd, Location), 0, Location};
899 if (Tok == "ALIGN") {
903 return [=] { return alignTo(Script->getDot(), E().getValue()); };
905 Expr E2 = readExpr();
909 V.Alignment = E2().getValue();
913 if (Tok == "ALIGNOF") {
914 StringRef Name = readParenLiteral();
915 OutputSectionCommand *Cmd = Script->getOrCreateOutputSectionCommand(Name);
916 return [=] { return checkSection(Cmd, Location)->Alignment; };
919 return readAssertExpr();
920 if (Tok == "CONSTANT") {
921 StringRef Name = readParenLiteral();
922 return [=] { return getConstant(Name); };
924 if (Tok == "DATA_SEGMENT_ALIGN") {
930 return [=] { return alignTo(Script->getDot(), E().getValue()); };
932 if (Tok == "DATA_SEGMENT_END") {
936 return [] { return Script->getDot(); };
938 if (Tok == "DATA_SEGMENT_RELRO_END") {
939 // GNU linkers implements more complicated logic to handle
940 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and
941 // just align to the next page boundary for simplicity.
947 return [] { return alignTo(Script->getDot(), Target->PageSize); };
949 if (Tok == "DEFINED") {
950 StringRef Name = readParenLiteral();
951 return [=] { return Script->isDefined(Name) ? 1 : 0; };
953 if (Tok == "LENGTH") {
954 StringRef Name = readParenLiteral();
955 if (Script->Opt.MemoryRegions.count(Name) == 0)
956 setError("memory region not defined: " + Name);
957 return [=] { return Script->Opt.MemoryRegions[Name].Length; };
959 if (Tok == "LOADADDR") {
960 StringRef Name = readParenLiteral();
961 OutputSectionCommand *Cmd = Script->getOrCreateOutputSectionCommand(Name);
962 return [=] { return checkSection(Cmd, Location)->getLMA(); };
964 if (Tok == "ORIGIN") {
965 StringRef Name = readParenLiteral();
966 if (Script->Opt.MemoryRegions.count(Name) == 0)
967 setError("memory region not defined: " + Name);
968 return [=] { return Script->Opt.MemoryRegions[Name].Origin; };
970 if (Tok == "SEGMENT_START") {
976 return [=] { return E(); };
978 if (Tok == "SIZEOF") {
979 StringRef Name = readParenLiteral();
980 OutputSectionCommand *Cmd = Script->getOrCreateOutputSectionCommand(Name);
981 // Linker script does not create an output section if its content is empty.
982 // We want to allow SIZEOF(.foo) where .foo is a section which happened to
984 return [=] { return Cmd->Sec ? Cmd->Sec->Size : 0; };
986 if (Tok == "SIZEOF_HEADERS")
987 return [=] { return elf::getHeaderSize(); };
991 return [=] { return Script->getSymbolValue(Location, Tok); };
993 // Tok is a literal number.
994 if (Optional<uint64_t> Val = parseInt(Tok))
995 return [=] { return *Val; };
997 // Tok is a symbol name.
998 if (!isValidCIdentifier(Tok))
999 setError("malformed number: " + Tok);
1000 Script->Opt.ReferencedSymbols.push_back(Tok);
1001 return [=] { return Script->getSymbolValue(Location, Tok); };
1004 Expr ScriptParser::readTernary(Expr Cond) {
1005 Expr L = readExpr();
1007 Expr R = readExpr();
1008 return [=] { return Cond().getValue() ? L() : R(); };
1011 Expr ScriptParser::readParenExpr() {
1013 Expr E = readExpr();
1018 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1019 std::vector<StringRef> Phdrs;
1020 while (!Error && peek().startswith(":")) {
1021 StringRef Tok = next();
1022 Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1));
1027 // Read a program header type name. The next token must be a
1028 // name of a program header type or a constant (e.g. "0x3").
1029 unsigned ScriptParser::readPhdrType() {
1030 StringRef Tok = next();
1031 if (Optional<uint64_t> Val = parseInt(Tok))
1034 unsigned Ret = StringSwitch<unsigned>(Tok)
1035 .Case("PT_NULL", PT_NULL)
1036 .Case("PT_LOAD", PT_LOAD)
1037 .Case("PT_DYNAMIC", PT_DYNAMIC)
1038 .Case("PT_INTERP", PT_INTERP)
1039 .Case("PT_NOTE", PT_NOTE)
1040 .Case("PT_SHLIB", PT_SHLIB)
1041 .Case("PT_PHDR", PT_PHDR)
1042 .Case("PT_TLS", PT_TLS)
1043 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1044 .Case("PT_GNU_STACK", PT_GNU_STACK)
1045 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1046 .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1047 .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1048 .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
1051 if (Ret == (unsigned)-1) {
1052 setError("invalid program header type: " + Tok);
1058 // Reads an anonymous version declaration.
1059 void ScriptParser::readAnonymousDeclaration() {
1060 std::vector<SymbolVersion> Locals;
1061 std::vector<SymbolVersion> Globals;
1062 std::tie(Locals, Globals) = readSymbols();
1064 for (SymbolVersion V : Locals) {
1066 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1068 Config->VersionScriptLocals.push_back(V);
1071 for (SymbolVersion V : Globals)
1072 Config->VersionScriptGlobals.push_back(V);
1077 // Reads a non-anonymous version definition,
1078 // e.g. "VerStr { global: foo; bar; local: *; };".
1079 void ScriptParser::readVersionDeclaration(StringRef VerStr) {
1080 // Read a symbol list.
1081 std::vector<SymbolVersion> Locals;
1082 std::vector<SymbolVersion> Globals;
1083 std::tie(Locals, Globals) = readSymbols();
1085 for (SymbolVersion V : Locals) {
1087 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1089 Config->VersionScriptLocals.push_back(V);
1092 // Create a new version definition and add that to the global symbols.
1093 VersionDefinition Ver;
1095 Ver.Globals = Globals;
1097 // User-defined version number starts from 2 because 0 and 1 are
1098 // reserved for VER_NDX_LOCAL and VER_NDX_GLOBAL, respectively.
1099 Ver.Id = Config->VersionDefinitions.size() + 2;
1100 Config->VersionDefinitions.push_back(Ver);
1102 // Each version may have a parent version. For example, "Ver2"
1103 // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1104 // as a parent. This version hierarchy is, probably against your
1105 // instinct, purely for hint; the runtime doesn't care about it
1106 // at all. In LLD, we simply ignore it.
1112 static bool hasWildcard(StringRef S) {
1113 return S.find_first_of("?*[") != StringRef::npos;
1116 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1117 std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
1118 ScriptParser::readSymbols() {
1119 std::vector<SymbolVersion> Locals;
1120 std::vector<SymbolVersion> Globals;
1121 std::vector<SymbolVersion> *V = &Globals;
1126 if (consumeLabel("local")) {
1130 if (consumeLabel("global")) {
1135 if (consume("extern")) {
1136 std::vector<SymbolVersion> Ext = readVersionExtern();
1137 V->insert(V->end(), Ext.begin(), Ext.end());
1139 StringRef Tok = next();
1140 V->push_back({unquote(Tok), false, hasWildcard(Tok)});
1144 return {Locals, Globals};
1147 // Reads an "extern C++" directive, e.g.,
1148 // "extern "C++" { ns::*; "f(int, double)"; };"
1149 std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
1150 StringRef Tok = next();
1151 bool IsCXX = Tok == "\"C++\"";
1152 if (!IsCXX && Tok != "\"C\"")
1153 setError("Unknown language");
1156 std::vector<SymbolVersion> Ret;
1157 while (!Error && peek() != "}") {
1158 StringRef Tok = next();
1159 bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok);
1160 Ret.push_back({unquote(Tok), IsCXX, HasWildcard});
1168 uint64_t ScriptParser::readMemoryAssignment(StringRef S1, StringRef S2,
1170 if (!consume(S1) && !consume(S2) && !consume(S3)) {
1171 setError("expected one of: " + S1 + ", " + S2 + ", or " + S3);
1175 return readExpr()().getValue();
1178 // Parse the MEMORY command as specified in:
1179 // https://sourceware.org/binutils/docs/ld/MEMORY.html
1181 // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
1182 void ScriptParser::readMemory() {
1184 while (!Error && !consume("}")) {
1185 StringRef Name = next();
1188 uint32_t NegFlags = 0;
1190 std::tie(Flags, NegFlags) = readMemoryAttributes();
1195 uint64_t Origin = readMemoryAssignment("ORIGIN", "org", "o");
1197 uint64_t Length = readMemoryAssignment("LENGTH", "len", "l");
1199 // Add the memory region to the region map (if it doesn't already exist).
1200 auto It = Script->Opt.MemoryRegions.find(Name);
1201 if (It != Script->Opt.MemoryRegions.end())
1202 setError("region '" + Name + "' already defined");
1204 Script->Opt.MemoryRegions[Name] = {Name, Origin, Length, Flags, NegFlags};
1208 // This function parses the attributes used to match against section
1209 // flags when placing output sections in a memory region. These flags
1210 // are only used when an explicit memory region name is not used.
1211 std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() {
1213 uint32_t NegFlags = 0;
1214 bool Invert = false;
1216 for (char C : next().lower()) {
1223 Flag = SHF_EXECINSTR;
1227 setError("invalid memory region attribute");
1234 return {Flags, NegFlags};
1237 void elf::readLinkerScript(MemoryBufferRef MB) {
1238 ScriptParser(MB).readLinkerScript();
1241 void elf::readVersionScript(MemoryBufferRef MB) {
1242 ScriptParser(MB).readVersionScript();
1245 void elf::readDynamicList(MemoryBufferRef MB) {
1246 ScriptParser(MB).readDynamicList();