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/Support/Casting.h"
29 #include "llvm/Support/ELF.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);
66 void readOutputArch();
67 void readOutputFormat();
72 void readVersionScriptCommand();
74 SymbolAssignment *readAssignment(StringRef Name);
75 BytesDataCommand *readBytesDataCommand(StringRef Tok);
77 uint32_t parseFill(StringRef Tok);
78 OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
79 std::vector<StringRef> readOutputSectionPhdrs();
80 InputSectionDescription *readInputSectionDescription(StringRef Tok);
81 StringMatcher readFilePatterns();
82 std::vector<SectionPattern> readInputSectionsList();
83 InputSectionDescription *readInputSectionRules(StringRef FilePattern);
84 unsigned readPhdrType();
85 SortSectionPolicy readSortKind();
86 SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
87 SymbolAssignment *readProvideOrAssignment(StringRef Tok);
89 AssertCommand *readAssert();
90 Expr readAssertExpr();
92 uint64_t readMemoryAssignment(StringRef, StringRef, StringRef);
93 std::pair<uint32_t, uint32_t> readMemoryAttributes();
96 Expr readExpr1(Expr Lhs, int MinPrec);
97 StringRef readParenLiteral();
99 Expr readTernary(Expr Cond);
100 Expr readParenExpr();
102 // For parsing version script.
103 std::vector<SymbolVersion> readVersionExtern();
104 void readAnonymousDeclaration();
105 void readVersionDeclaration(StringRef VerStr);
107 std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
114 static bool isUnderSysroot(StringRef Path) {
115 if (Config->Sysroot == "")
117 for (; !Path.empty(); Path = sys::path::parent_path(Path))
118 if (sys::fs::equivalent(Config->Sysroot, Path))
123 // Some operations only support one non absolute value. Move the
124 // absolute one to the right hand side for convenience.
125 static void moveAbsRight(ExprValue &A, ExprValue &B) {
129 error("At least one side of the expression must be absolute");
132 static ExprValue add(ExprValue A, ExprValue B) {
134 return {A.Sec, A.ForceAbsolute, A.Val + B.getValue()};
137 static ExprValue sub(ExprValue A, ExprValue B) {
138 return {A.Sec, A.Val - B.getValue()};
141 static ExprValue mul(ExprValue A, ExprValue B) {
142 return A.getValue() * B.getValue();
145 static ExprValue div(ExprValue A, ExprValue B) {
146 if (uint64_t BV = B.getValue())
147 return A.getValue() / BV;
148 error("division by zero");
152 static ExprValue bitAnd(ExprValue A, ExprValue B) {
154 return {A.Sec, A.ForceAbsolute,
155 (A.getValue() & B.getValue()) - A.getSecAddr()};
158 static ExprValue bitOr(ExprValue A, ExprValue B) {
160 return {A.Sec, A.ForceAbsolute,
161 (A.getValue() | B.getValue()) - A.getSecAddr()};
164 void ScriptParser::readDynamicList() {
166 readAnonymousDeclaration();
168 setError("EOF expected, but got " + next());
171 void ScriptParser::readVersionScript() {
172 readVersionScriptCommand();
174 setError("EOF expected, but got " + next());
177 void ScriptParser::readVersionScriptCommand() {
179 readAnonymousDeclaration();
183 while (!atEOF() && !Error && peek() != "}") {
184 StringRef VerStr = next();
186 setError("anonymous version definition is used in "
187 "combination with other version definitions");
191 readVersionDeclaration(VerStr);
195 void ScriptParser::readVersion() {
197 readVersionScriptCommand();
201 void ScriptParser::readLinkerScript() {
203 StringRef Tok = next();
207 if (Tok == "ASSERT") {
208 Script->Opt.Commands.push_back(readAssert());
209 } else if (Tok == "ENTRY") {
211 } else if (Tok == "EXTERN") {
213 } else if (Tok == "GROUP" || Tok == "INPUT") {
215 } else if (Tok == "INCLUDE") {
217 } else if (Tok == "MEMORY") {
219 } else if (Tok == "OUTPUT") {
221 } else if (Tok == "OUTPUT_ARCH") {
223 } else if (Tok == "OUTPUT_FORMAT") {
225 } else if (Tok == "PHDRS") {
227 } else if (Tok == "SEARCH_DIR") {
229 } else if (Tok == "SECTIONS") {
231 } else if (Tok == "VERSION") {
233 } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) {
234 Script->Opt.Commands.push_back(Cmd);
236 setError("unknown directive: " + Tok);
241 void ScriptParser::addFile(StringRef S) {
242 if (IsUnderSysroot && S.startswith("/")) {
243 SmallString<128> PathData;
244 StringRef Path = (Config->Sysroot + S).toStringRef(PathData);
245 if (sys::fs::exists(Path)) {
246 Driver->addFile(Saver.save(Path), /*WithLOption=*/false);
251 if (sys::path::is_absolute(S)) {
252 Driver->addFile(S, /*WithLOption=*/false);
253 } else if (S.startswith("=")) {
254 if (Config->Sysroot.empty())
255 Driver->addFile(S.substr(1), /*WithLOption=*/false);
257 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)),
258 /*WithLOption=*/false);
259 } else if (S.startswith("-l")) {
260 Driver->addLibrary(S.substr(2));
261 } else if (sys::fs::exists(S)) {
262 Driver->addFile(S, /*WithLOption=*/false);
264 if (Optional<std::string> Path = findFromSearchPaths(S))
265 Driver->addFile(Saver.save(*Path), /*WithLOption=*/true);
267 setError("unable to find " + S);
271 void ScriptParser::readAsNeeded() {
273 bool Orig = Config->AsNeeded;
274 Config->AsNeeded = true;
275 while (!Error && !consume(")"))
276 addFile(unquote(next()));
277 Config->AsNeeded = Orig;
280 void ScriptParser::readEntry() {
281 // -e <symbol> takes predecence over ENTRY(<symbol>).
283 StringRef Tok = next();
284 if (Config->Entry.empty())
289 void ScriptParser::readExtern() {
291 while (!Error && !consume(")"))
292 Config->Undefined.push_back(next());
295 void ScriptParser::readGroup() {
297 while (!Error && !consume(")")) {
298 if (consume("AS_NEEDED"))
301 addFile(unquote(next()));
305 void ScriptParser::readInclude() {
306 StringRef Tok = unquote(next());
308 // https://sourceware.org/binutils/docs/ld/File-Commands.html:
309 // The file will be searched for in the current directory, and in any
310 // directory specified with the -L option.
311 if (sys::fs::exists(Tok)) {
312 if (Optional<MemoryBufferRef> MB = readFile(Tok))
316 if (Optional<std::string> Path = findFromSearchPaths(Tok)) {
317 if (Optional<MemoryBufferRef> MB = readFile(*Path))
321 setError("cannot open " + Tok);
324 void ScriptParser::readOutput() {
325 // -o <file> takes predecence over OUTPUT(<file>).
327 StringRef Tok = next();
328 if (Config->OutputFile.empty())
329 Config->OutputFile = unquote(Tok);
333 void ScriptParser::readOutputArch() {
334 // OUTPUT_ARCH is ignored for now.
336 while (!Error && !consume(")"))
340 void ScriptParser::readOutputFormat() {
341 // Error checking only for now.
353 void ScriptParser::readPhdrs() {
355 while (!Error && !consume("}")) {
356 Script->Opt.PhdrsCommands.push_back(
357 {next(), PT_NULL, false, false, UINT_MAX, nullptr});
359 PhdrsCommand &PhdrCmd = Script->Opt.PhdrsCommands.back();
360 PhdrCmd.Type = readPhdrType();
362 while (!Error && !consume(";")) {
363 if (consume("FILEHDR"))
364 PhdrCmd.HasFilehdr = true;
365 else if (consume("PHDRS"))
366 PhdrCmd.HasPhdrs = true;
367 else if (consume("AT"))
368 PhdrCmd.LMAExpr = readParenExpr();
369 else if (consume("FLAGS"))
370 PhdrCmd.Flags = readParenExpr()().getValue();
372 setError("unexpected header attribute: " + next());
377 void ScriptParser::readSearchDir() {
379 StringRef Tok = next();
380 if (!Config->Nostdlib)
381 Config->SearchPaths.push_back(unquote(Tok));
385 void ScriptParser::readSections() {
386 Script->Opt.HasSections = true;
388 // -no-rosegment is used to avoid placing read only non-executable sections in
389 // their own segment. We do the same if SECTIONS command is present in linker
390 // script. See comment for computeFlags().
391 Config->SingleRoRx = true;
394 while (!Error && !consume("}")) {
395 StringRef Tok = next();
396 BaseCommand *Cmd = readProvideOrAssignment(Tok);
401 Cmd = readOutputSectionDescription(Tok);
403 Script->Opt.Commands.push_back(Cmd);
407 static int precedence(StringRef Op) {
408 return StringSwitch<int>(Op)
411 .Cases("<<", ">>", 3)
412 .Cases("<", "<=", ">", ">=", "==", "!=", 2)
417 StringMatcher ScriptParser::readFilePatterns() {
418 std::vector<StringRef> V;
419 while (!Error && !consume(")"))
421 return StringMatcher(V);
424 SortSectionPolicy ScriptParser::readSortKind() {
425 if (consume("SORT") || consume("SORT_BY_NAME"))
426 return SortSectionPolicy::Name;
427 if (consume("SORT_BY_ALIGNMENT"))
428 return SortSectionPolicy::Alignment;
429 if (consume("SORT_BY_INIT_PRIORITY"))
430 return SortSectionPolicy::Priority;
431 if (consume("SORT_NONE"))
432 return SortSectionPolicy::None;
433 return SortSectionPolicy::Default;
436 // Reads SECTIONS command contents in the following form:
438 // <contents> ::= <elem>*
439 // <elem> ::= <exclude>? <glob-pattern>
440 // <exclude> ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
444 // *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
446 // is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
447 // The semantics of that is section .foo in any file, section .bar in
448 // any file but a.o, and section .baz in any file but b.o.
449 std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
450 std::vector<SectionPattern> Ret;
451 while (!Error && peek() != ")") {
452 StringMatcher ExcludeFilePat;
453 if (consume("EXCLUDE_FILE")) {
455 ExcludeFilePat = readFilePatterns();
458 std::vector<StringRef> V;
459 while (!Error && peek() != ")" && peek() != "EXCLUDE_FILE")
463 Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)});
465 setError("section pattern is expected");
470 // Reads contents of "SECTIONS" directive. That directive contains a
471 // list of glob patterns for input sections. The grammar is as follows.
473 // <patterns> ::= <section-list>
474 // | <sort> "(" <section-list> ")"
475 // | <sort> "(" <sort> "(" <section-list> ")" ")"
477 // <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
478 // | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
480 // <section-list> is parsed by readInputSectionsList().
481 InputSectionDescription *
482 ScriptParser::readInputSectionRules(StringRef FilePattern) {
483 auto *Cmd = make<InputSectionDescription>(FilePattern);
486 while (!Error && !consume(")")) {
487 SortSectionPolicy Outer = readSortKind();
488 SortSectionPolicy Inner = SortSectionPolicy::Default;
489 std::vector<SectionPattern> V;
490 if (Outer != SortSectionPolicy::Default) {
492 Inner = readSortKind();
493 if (Inner != SortSectionPolicy::Default) {
495 V = readInputSectionsList();
498 V = readInputSectionsList();
502 V = readInputSectionsList();
505 for (SectionPattern &Pat : V) {
506 Pat.SortInner = Inner;
507 Pat.SortOuter = Outer;
510 std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
515 InputSectionDescription *
516 ScriptParser::readInputSectionDescription(StringRef Tok) {
517 // Input section wildcard can be surrounded by KEEP.
518 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
521 StringRef FilePattern = next();
522 InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
524 Script->Opt.KeptSections.push_back(Cmd);
527 return readInputSectionRules(Tok);
530 void ScriptParser::readSort() {
532 expect("CONSTRUCTORS");
536 AssertCommand *ScriptParser::readAssert() {
537 return make<AssertCommand>(readAssertExpr());
540 Expr ScriptParser::readAssertExpr() {
544 StringRef Msg = unquote(next());
550 return Script->getDot();
554 // Reads a FILL(expr) command. We handle the FILL command as an
555 // alias for =fillexp section attribute, which is different from
556 // what GNU linkers do.
557 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
558 uint32_t ScriptParser::readFill() {
560 uint32_t V = parseFill(next());
565 OutputSectionCommand *
566 ScriptParser::readOutputSectionDescription(StringRef OutSec) {
567 OutputSectionCommand *Cmd = make<OutputSectionCommand>(OutSec);
568 Cmd->Location = getCurrentLocation();
570 // Read an address expression.
571 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
573 Cmd->AddrExpr = readExpr();
578 Cmd->LMAExpr = readParenExpr();
579 if (consume("ALIGN"))
580 Cmd->AlignExpr = readParenExpr();
581 if (consume("SUBALIGN"))
582 Cmd->SubalignExpr = readParenExpr();
584 // Parse constraints.
585 if (consume("ONLY_IF_RO"))
586 Cmd->Constraint = ConstraintKind::ReadOnly;
587 if (consume("ONLY_IF_RW"))
588 Cmd->Constraint = ConstraintKind::ReadWrite;
591 while (!Error && !consume("}")) {
592 StringRef Tok = next();
594 // Empty commands are allowed. Do nothing here.
595 } else if (SymbolAssignment *Assign = readProvideOrAssignment(Tok)) {
596 Cmd->Commands.push_back(Assign);
597 } else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) {
598 Cmd->Commands.push_back(Data);
599 } else if (Tok == "ASSERT") {
600 Cmd->Commands.push_back(readAssert());
602 } else if (Tok == "CONSTRUCTORS") {
603 // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
604 // by name. This is for very old file formats such as ECOFF/XCOFF.
605 // For ELF, we should ignore.
606 } else if (Tok == "FILL") {
607 Cmd->Filler = readFill();
608 } else if (Tok == "SORT") {
610 } else if (peek() == "(") {
611 Cmd->Commands.push_back(readInputSectionDescription(Tok));
613 setError("unknown command " + Tok);
618 Cmd->MemoryRegionName = next();
620 Cmd->Phdrs = readOutputSectionPhdrs();
623 Cmd->Filler = parseFill(next());
624 else if (peek().startswith("="))
625 Cmd->Filler = parseFill(next().drop_front());
627 // Consume optional comma following output section command.
633 // Parses a given string as a octal/decimal/hexadecimal number and
634 // returns it as a big-endian number. Used for `=<fillexp>`.
635 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
637 // When reading a hexstring, ld.bfd handles it as a blob of arbitrary
638 // size, while ld.gold always handles it as a 32-bit big-endian number.
639 // We are compatible with ld.gold because it's easier to implement.
640 uint32_t ScriptParser::parseFill(StringRef Tok) {
642 if (Tok.getAsInteger(0, V))
643 setError("invalid filler expression: " + Tok);
650 SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
652 SymbolAssignment *Cmd = readAssignment(next());
653 Cmd->Provide = Provide;
654 Cmd->Hidden = Hidden;
660 SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) {
661 SymbolAssignment *Cmd = nullptr;
662 if (peek() == "=" || peek() == "+=") {
663 Cmd = readAssignment(Tok);
665 } else if (Tok == "PROVIDE") {
666 Cmd = readProvideHidden(true, false);
667 } else if (Tok == "HIDDEN") {
668 Cmd = readProvideHidden(false, true);
669 } else if (Tok == "PROVIDE_HIDDEN") {
670 Cmd = readProvideHidden(true, true);
675 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
676 StringRef Op = next();
677 assert(Op == "=" || Op == "+=");
680 std::string Loc = getCurrentLocation();
681 E = [=] { return add(Script->getSymbolValue(Loc, Name), E()); };
683 return make<SymbolAssignment>(Name, E, getCurrentLocation());
686 // This is an operator-precedence parser to parse a linker
687 // script expression.
688 Expr ScriptParser::readExpr() {
689 // Our lexer is context-aware. Set the in-expression bit so that
690 // they apply different tokenization rules.
693 Expr E = readExpr1(readPrimary(), 0);
698 static Expr combine(StringRef Op, Expr L, Expr R) {
700 return [=] { return add(L(), R()); };
702 return [=] { return sub(L(), R()); };
704 return [=] { return mul(L(), R()); };
706 return [=] { return div(L(), R()); };
708 return [=] { return L().getValue() << R().getValue(); };
710 return [=] { return L().getValue() >> R().getValue(); };
712 return [=] { return L().getValue() < R().getValue(); };
714 return [=] { return L().getValue() > R().getValue(); };
716 return [=] { return L().getValue() >= R().getValue(); };
718 return [=] { return L().getValue() <= R().getValue(); };
720 return [=] { return L().getValue() == R().getValue(); };
722 return [=] { return L().getValue() != R().getValue(); };
724 return [=] { return bitAnd(L(), R()); };
726 return [=] { return bitOr(L(), R()); };
727 llvm_unreachable("invalid operator");
730 // This is a part of the operator-precedence parser. This function
731 // assumes that the remaining token stream starts with an operator.
732 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
733 while (!atEOF() && !Error) {
734 // Read an operator and an expression.
736 return readTernary(Lhs);
737 StringRef Op1 = peek();
738 if (precedence(Op1) < MinPrec)
741 Expr Rhs = readPrimary();
743 // Evaluate the remaining part of the expression first if the
744 // next operator has greater precedence than the previous one.
745 // For example, if we have read "+" and "3", and if the next
746 // operator is "*", then we'll evaluate 3 * ... part first.
748 StringRef Op2 = peek();
749 if (precedence(Op2) <= precedence(Op1))
751 Rhs = readExpr1(Rhs, precedence(Op2));
754 Lhs = combine(Op1, Lhs, Rhs);
759 uint64_t static getConstant(StringRef S) {
760 if (S == "COMMONPAGESIZE")
761 return Target->PageSize;
762 if (S == "MAXPAGESIZE")
763 return Config->MaxPageSize;
764 error("unknown constant: " + S);
768 // Parses Tok as an integer. It recognizes hexadecimal (prefixed with
769 // "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
770 // have "K" (Ki) or "M" (Mi) suffixes.
771 static Optional<uint64_t> parseInt(StringRef Tok) {
773 if (Tok.startswith("-")) {
774 if (Optional<uint64_t> Val = parseInt(Tok.substr(1)))
781 if (Tok.startswith_lower("0x") && !Tok.substr(2).getAsInteger(16, Val))
783 if (Tok.endswith_lower("H") && !Tok.drop_back().getAsInteger(16, Val))
787 if (Tok.endswith_lower("K")) {
788 if (Tok.drop_back().getAsInteger(10, Val))
792 if (Tok.endswith_lower("M")) {
793 if (Tok.drop_back().getAsInteger(10, Val))
795 return Val * 1024 * 1024;
797 if (Tok.getAsInteger(10, Val))
802 BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) {
803 int Size = StringSwitch<int>(Tok)
812 return make<BytesDataCommand>(readParenExpr(), Size);
815 StringRef ScriptParser::readParenLiteral() {
817 StringRef Tok = next();
822 Expr ScriptParser::readPrimary() {
824 return readParenExpr();
827 Expr E = readPrimary();
828 return [=] { return ~E().getValue(); };
831 Expr E = readPrimary();
832 return [=] { return -E().getValue(); };
835 StringRef Tok = next();
836 std::string Location = getCurrentLocation();
838 // Built-in functions are parsed here.
839 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
840 if (Tok == "ABSOLUTE") {
841 Expr Inner = readParenExpr();
843 ExprValue I = Inner();
844 I.ForceAbsolute = true;
849 StringRef Name = readParenLiteral();
850 return [=]() -> ExprValue {
851 return {Script->getOutputSection(Location, Name), 0};
854 if (Tok == "ALIGN") {
858 return [=] { return alignTo(Script->getDot(), E().getValue()); };
860 Expr E2 = readExpr();
862 return [=] { return alignTo(E().getValue(), E2().getValue()); };
864 if (Tok == "ALIGNOF") {
865 StringRef Name = readParenLiteral();
866 return [=] { return Script->getOutputSection(Location, Name)->Alignment; };
869 return readAssertExpr();
870 if (Tok == "CONSTANT") {
871 StringRef Name = readParenLiteral();
872 return [=] { return getConstant(Name); };
874 if (Tok == "DATA_SEGMENT_ALIGN") {
880 return [=] { return alignTo(Script->getDot(), E().getValue()); };
882 if (Tok == "DATA_SEGMENT_END") {
886 return [] { return Script->getDot(); };
888 if (Tok == "DATA_SEGMENT_RELRO_END") {
889 // GNU linkers implements more complicated logic to handle
890 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and
891 // just align to the next page boundary for simplicity.
897 return [] { return alignTo(Script->getDot(), Target->PageSize); };
899 if (Tok == "DEFINED") {
900 StringRef Name = readParenLiteral();
901 return [=] { return Script->isDefined(Name) ? 1 : 0; };
903 if (Tok == "LOADADDR") {
904 StringRef Name = readParenLiteral();
905 return [=] { return Script->getOutputSection(Location, Name)->getLMA(); };
907 if (Tok == "SEGMENT_START") {
913 return [=] { return E(); };
915 if (Tok == "SIZEOF") {
916 StringRef Name = readParenLiteral();
917 return [=] { return Script->getOutputSectionSize(Name); };
919 if (Tok == "SIZEOF_HEADERS")
920 return [=] { return elf::getHeaderSize(); };
924 return [=] { return Script->getSymbolValue(Location, Tok); };
926 // Tok is a literal number.
927 if (Optional<uint64_t> Val = parseInt(Tok))
928 return [=] { return *Val; };
930 // Tok is a symbol name.
931 if (!isValidCIdentifier(Tok))
932 setError("malformed number: " + Tok);
933 Script->Opt.ReferencedSymbols.push_back(Tok);
934 return [=] { return Script->getSymbolValue(Location, Tok); };
937 Expr ScriptParser::readTernary(Expr Cond) {
941 return [=] { return Cond().getValue() ? L() : R(); };
944 Expr ScriptParser::readParenExpr() {
951 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
952 std::vector<StringRef> Phdrs;
953 while (!Error && peek().startswith(":")) {
954 StringRef Tok = next();
955 Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1));
960 // Read a program header type name. The next token must be a
961 // name of a program header type or a constant (e.g. "0x3").
962 unsigned ScriptParser::readPhdrType() {
963 StringRef Tok = next();
964 if (Optional<uint64_t> Val = parseInt(Tok))
967 unsigned Ret = StringSwitch<unsigned>(Tok)
968 .Case("PT_NULL", PT_NULL)
969 .Case("PT_LOAD", PT_LOAD)
970 .Case("PT_DYNAMIC", PT_DYNAMIC)
971 .Case("PT_INTERP", PT_INTERP)
972 .Case("PT_NOTE", PT_NOTE)
973 .Case("PT_SHLIB", PT_SHLIB)
974 .Case("PT_PHDR", PT_PHDR)
975 .Case("PT_TLS", PT_TLS)
976 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
977 .Case("PT_GNU_STACK", PT_GNU_STACK)
978 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
979 .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
980 .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
981 .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
984 if (Ret == (unsigned)-1) {
985 setError("invalid program header type: " + Tok);
991 // Reads an anonymous version declaration.
992 void ScriptParser::readAnonymousDeclaration() {
993 std::vector<SymbolVersion> Locals;
994 std::vector<SymbolVersion> Globals;
995 std::tie(Locals, Globals) = readSymbols();
997 for (SymbolVersion V : Locals) {
999 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1001 Config->VersionScriptLocals.push_back(V);
1004 for (SymbolVersion V : Globals)
1005 Config->VersionScriptGlobals.push_back(V);
1010 // Reads a non-anonymous version definition,
1011 // e.g. "VerStr { global: foo; bar; local: *; };".
1012 void ScriptParser::readVersionDeclaration(StringRef VerStr) {
1013 // Read a symbol list.
1014 std::vector<SymbolVersion> Locals;
1015 std::vector<SymbolVersion> Globals;
1016 std::tie(Locals, Globals) = readSymbols();
1018 for (SymbolVersion V : Locals) {
1020 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1022 Config->VersionScriptLocals.push_back(V);
1025 // Create a new version definition and add that to the global symbols.
1026 VersionDefinition Ver;
1028 Ver.Globals = Globals;
1030 // User-defined version number starts from 2 because 0 and 1 are
1031 // reserved for VER_NDX_LOCAL and VER_NDX_GLOBAL, respectively.
1032 Ver.Id = Config->VersionDefinitions.size() + 2;
1033 Config->VersionDefinitions.push_back(Ver);
1035 // Each version may have a parent version. For example, "Ver2"
1036 // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1037 // as a parent. This version hierarchy is, probably against your
1038 // instinct, purely for hint; the runtime doesn't care about it
1039 // at all. In LLD, we simply ignore it.
1045 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1046 std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
1047 ScriptParser::readSymbols() {
1048 std::vector<SymbolVersion> Locals;
1049 std::vector<SymbolVersion> Globals;
1050 std::vector<SymbolVersion> *V = &Globals;
1055 if (consumeLabel("local")) {
1059 if (consumeLabel("global")) {
1064 if (consume("extern")) {
1065 std::vector<SymbolVersion> Ext = readVersionExtern();
1066 V->insert(V->end(), Ext.begin(), Ext.end());
1068 StringRef Tok = next();
1069 V->push_back({unquote(Tok), false, hasWildcard(Tok)});
1073 return {Locals, Globals};
1076 // Reads an "extern C++" directive, e.g.,
1077 // "extern "C++" { ns::*; "f(int, double)"; };"
1078 std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
1079 StringRef Tok = next();
1080 bool IsCXX = Tok == "\"C++\"";
1081 if (!IsCXX && Tok != "\"C\"")
1082 setError("Unknown language");
1085 std::vector<SymbolVersion> Ret;
1086 while (!Error && peek() != "}") {
1087 StringRef Tok = next();
1088 bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok);
1089 Ret.push_back({unquote(Tok), IsCXX, HasWildcard});
1097 uint64_t ScriptParser::readMemoryAssignment(StringRef S1, StringRef S2,
1099 if (!consume(S1) && !consume(S2) && !consume(S3)) {
1100 setError("expected one of: " + S1 + ", " + S2 + ", or " + S3);
1104 return readExpr()().getValue();
1107 // Parse the MEMORY command as specified in:
1108 // https://sourceware.org/binutils/docs/ld/MEMORY.html
1110 // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
1111 void ScriptParser::readMemory() {
1113 while (!Error && !consume("}")) {
1114 StringRef Name = next();
1117 uint32_t NegFlags = 0;
1119 std::tie(Flags, NegFlags) = readMemoryAttributes();
1124 uint64_t Origin = readMemoryAssignment("ORIGIN", "org", "o");
1126 uint64_t Length = readMemoryAssignment("LENGTH", "len", "l");
1128 // Add the memory region to the region map (if it doesn't already exist).
1129 auto It = Script->Opt.MemoryRegions.find(Name);
1130 if (It != Script->Opt.MemoryRegions.end())
1131 setError("region '" + Name + "' already defined");
1133 Script->Opt.MemoryRegions[Name] = {Name, Origin, Length,
1134 Origin, Flags, NegFlags};
1138 // This function parses the attributes used to match against section
1139 // flags when placing output sections in a memory region. These flags
1140 // are only used when an explicit memory region name is not used.
1141 std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() {
1143 uint32_t NegFlags = 0;
1144 bool Invert = false;
1146 for (char C : next().lower()) {
1153 Flag = SHF_EXECINSTR;
1157 setError("invalid memory region attribute");
1164 return {Flags, NegFlags};
1167 void elf::readLinkerScript(MemoryBufferRef MB) {
1168 ScriptParser(MB).readLinkerScript();
1171 void elf::readVersionScript(MemoryBufferRef MB) {
1172 ScriptParser(MB).readVersionScript();
1175 void elf::readDynamicList(MemoryBufferRef MB) {
1176 ScriptParser(MB).readDynamicList();