1 //===- ScriptParser.cpp ---------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains a recursive-descendent parser for linker scripts.
10 // Parsed results are stored to Config and Script global objects.
12 //===----------------------------------------------------------------------===//
14 #include "ScriptParser.h"
17 #include "InputSection.h"
18 #include "LinkerScript.h"
19 #include "OutputSections.h"
20 #include "ScriptLexer.h"
23 #include "lld/Common/Memory.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/StringSet.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;
44 class ScriptParser final : ScriptLexer {
46 ScriptParser(MemoryBufferRef mb) : ScriptLexer(mb) {
47 // Initialize IsUnderSysroot
48 if (config->sysroot == "")
50 StringRef path = mb.getBufferIdentifier();
51 for (; !path.empty(); path = sys::path::parent_path(path)) {
52 if (!sys::fs::equivalent(config->sysroot, path))
54 isUnderSysroot = true;
59 void readLinkerScript();
60 void readVersionScript();
61 void readDynamicList();
62 void readDefsym(StringRef name);
65 void addFile(StringRef path);
75 void readOutputArch();
76 void readOutputFormat();
78 void readRegionAlias();
83 void readVersionScriptCommand();
85 SymbolAssignment *readSymbolAssignment(StringRef name);
86 ByteCommand *readByteCommand(StringRef tok);
87 std::array<uint8_t, 4> readFill();
88 bool readSectionDirective(OutputSection *cmd, StringRef tok1, StringRef tok2);
89 void readSectionAddressType(OutputSection *cmd);
90 OutputSection *readOverlaySectionDescription();
91 OutputSection *readOutputSectionDescription(StringRef outSec);
92 std::vector<BaseCommand *> readOverlay();
93 std::vector<StringRef> readOutputSectionPhdrs();
94 InputSectionDescription *readInputSectionDescription(StringRef tok);
95 StringMatcher readFilePatterns();
96 std::vector<SectionPattern> readInputSectionsList();
97 InputSectionDescription *readInputSectionRules(StringRef filePattern);
98 unsigned readPhdrType();
99 SortSectionPolicy readSortKind();
100 SymbolAssignment *readProvideHidden(bool provide, bool hidden);
101 SymbolAssignment *readAssignment(StringRef tok);
107 uint64_t readMemoryAssignment(StringRef, StringRef, StringRef);
108 std::pair<uint32_t, uint32_t> readMemoryAttributes();
110 Expr combine(StringRef op, Expr l, Expr r);
112 Expr readExpr1(Expr lhs, int minPrec);
113 StringRef readParenLiteral();
115 Expr readTernary(Expr cond);
116 Expr readParenExpr();
118 // For parsing version script.
119 std::vector<SymbolVersion> readVersionExtern();
120 void readAnonymousDeclaration();
121 void readVersionDeclaration(StringRef verStr);
123 std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
126 // True if a script being read is in a subdirectory specified by -sysroot.
127 bool isUnderSysroot = false;
129 // A set to detect an INCLUDE() cycle.
134 static StringRef unquote(StringRef s) {
135 if (s.startswith("\""))
136 return s.substr(1, s.size() - 2);
140 // Some operations only support one non absolute value. Move the
141 // absolute one to the right hand side for convenience.
142 static void moveAbsRight(ExprValue &a, ExprValue &b) {
143 if (a.sec == nullptr || (a.forceAbsolute && !b.isAbsolute()))
146 error(a.loc + ": at least one side of the expression must be absolute");
149 static ExprValue add(ExprValue a, ExprValue b) {
151 return {a.sec, a.forceAbsolute, a.getSectionOffset() + b.getValue(), a.loc};
154 static ExprValue sub(ExprValue a, ExprValue b) {
155 // The distance between two symbols in sections is absolute.
156 if (!a.isAbsolute() && !b.isAbsolute())
157 return a.getValue() - b.getValue();
158 return {a.sec, false, a.getSectionOffset() - b.getValue(), a.loc};
161 static ExprValue bitAnd(ExprValue a, ExprValue b) {
163 return {a.sec, a.forceAbsolute,
164 (a.getValue() & b.getValue()) - a.getSecAddr(), a.loc};
167 static ExprValue bitOr(ExprValue a, ExprValue b) {
169 return {a.sec, a.forceAbsolute,
170 (a.getValue() | b.getValue()) - a.getSecAddr(), a.loc};
173 void ScriptParser::readDynamicList() {
174 config->hasDynamicList = true;
176 std::vector<SymbolVersion> locals;
177 std::vector<SymbolVersion> globals;
178 std::tie(locals, globals) = readSymbols();
182 setError("EOF expected, but got " + next());
185 if (!locals.empty()) {
186 setError("\"local:\" scope not supported in --dynamic-list");
190 for (SymbolVersion v : globals)
191 config->dynamicList.push_back(v);
194 void ScriptParser::readVersionScript() {
195 readVersionScriptCommand();
197 setError("EOF expected, but got " + next());
200 void ScriptParser::readVersionScriptCommand() {
202 readAnonymousDeclaration();
206 while (!atEOF() && !errorCount() && peek() != "}") {
207 StringRef verStr = next();
209 setError("anonymous version definition is used in "
210 "combination with other version definitions");
214 readVersionDeclaration(verStr);
218 void ScriptParser::readVersion() {
220 readVersionScriptCommand();
224 void ScriptParser::readLinkerScript() {
226 StringRef tok = next();
230 if (tok == "ENTRY") {
232 } else if (tok == "EXTERN") {
234 } else if (tok == "GROUP") {
236 } else if (tok == "INCLUDE") {
238 } else if (tok == "INPUT") {
240 } else if (tok == "MEMORY") {
242 } else if (tok == "OUTPUT") {
244 } else if (tok == "OUTPUT_ARCH") {
246 } else if (tok == "OUTPUT_FORMAT") {
248 } else if (tok == "PHDRS") {
250 } else if (tok == "REGION_ALIAS") {
252 } else if (tok == "SEARCH_DIR") {
254 } else if (tok == "SECTIONS") {
256 } else if (tok == "TARGET") {
258 } else if (tok == "VERSION") {
260 } else if (SymbolAssignment *cmd = readAssignment(tok)) {
261 script->sectionCommands.push_back(cmd);
263 setError("unknown directive: " + tok);
268 void ScriptParser::readDefsym(StringRef name) {
273 setError("EOF expected, but got " + next());
274 SymbolAssignment *cmd = make<SymbolAssignment>(name, e, getCurrentLocation());
275 script->sectionCommands.push_back(cmd);
278 void ScriptParser::addFile(StringRef s) {
279 if (isUnderSysroot && s.startswith("/")) {
280 SmallString<128> pathData;
281 StringRef path = (config->sysroot + s).toStringRef(pathData);
282 if (sys::fs::exists(path)) {
283 driver->addFile(saver.save(path), /*withLOption=*/false);
288 if (s.startswith("/")) {
289 driver->addFile(s, /*withLOption=*/false);
290 } else if (s.startswith("=")) {
291 if (config->sysroot.empty())
292 driver->addFile(s.substr(1), /*withLOption=*/false);
294 driver->addFile(saver.save(config->sysroot + "/" + s.substr(1)),
295 /*withLOption=*/false);
296 } else if (s.startswith("-l")) {
297 driver->addLibrary(s.substr(2));
298 } else if (sys::fs::exists(s)) {
299 driver->addFile(s, /*withLOption=*/false);
301 if (Optional<std::string> path = findFromSearchPaths(s))
302 driver->addFile(saver.save(*path), /*withLOption=*/true);
304 setError("unable to find " + s);
308 void ScriptParser::readAsNeeded() {
310 bool orig = config->asNeeded;
311 config->asNeeded = true;
312 while (!errorCount() && !consume(")"))
313 addFile(unquote(next()));
314 config->asNeeded = orig;
317 void ScriptParser::readEntry() {
318 // -e <symbol> takes predecence over ENTRY(<symbol>).
320 StringRef tok = next();
321 if (config->entry.empty())
326 void ScriptParser::readExtern() {
328 while (!errorCount() && !consume(")"))
329 config->undefined.push_back(unquote(next()));
332 void ScriptParser::readGroup() {
333 bool orig = InputFile::isInGroup;
334 InputFile::isInGroup = true;
336 InputFile::isInGroup = orig;
338 ++InputFile::nextGroupId;
341 void ScriptParser::readInclude() {
342 StringRef tok = unquote(next());
344 if (!seen.insert(tok).second) {
345 setError("there is a cycle in linker script INCLUDEs");
349 if (Optional<std::string> path = searchScript(tok)) {
350 if (Optional<MemoryBufferRef> mb = readFile(*path))
354 setError("cannot find linker script " + tok);
357 void ScriptParser::readInput() {
359 while (!errorCount() && !consume(")")) {
360 if (consume("AS_NEEDED"))
363 addFile(unquote(next()));
367 void ScriptParser::readOutput() {
368 // -o <file> takes predecence over OUTPUT(<file>).
370 StringRef tok = next();
371 if (config->outputFile.empty())
372 config->outputFile = unquote(tok);
376 void ScriptParser::readOutputArch() {
377 // OUTPUT_ARCH is ignored for now.
379 while (!errorCount() && !consume(")"))
383 static std::pair<ELFKind, uint16_t> parseBfdName(StringRef s) {
384 return StringSwitch<std::pair<ELFKind, uint16_t>>(s)
385 .Case("elf32-i386", {ELF32LEKind, EM_386})
386 .Case("elf32-iamcu", {ELF32LEKind, EM_IAMCU})
387 .Case("elf32-littlearm", {ELF32LEKind, EM_ARM})
388 .Case("elf32-x86-64", {ELF32LEKind, EM_X86_64})
389 .Case("elf64-aarch64", {ELF64LEKind, EM_AARCH64})
390 .Case("elf64-littleaarch64", {ELF64LEKind, EM_AARCH64})
391 .Case("elf32-powerpc", {ELF32BEKind, EM_PPC})
392 .Case("elf64-powerpc", {ELF64BEKind, EM_PPC64})
393 .Case("elf64-powerpcle", {ELF64LEKind, EM_PPC64})
394 .Case("elf64-x86-64", {ELF64LEKind, EM_X86_64})
395 .Cases("elf32-tradbigmips", "elf32-bigmips", {ELF32BEKind, EM_MIPS})
396 .Case("elf32-ntradbigmips", {ELF32BEKind, EM_MIPS})
397 .Case("elf32-tradlittlemips", {ELF32LEKind, EM_MIPS})
398 .Case("elf32-ntradlittlemips", {ELF32LEKind, EM_MIPS})
399 .Case("elf64-tradbigmips", {ELF64BEKind, EM_MIPS})
400 .Case("elf64-tradlittlemips", {ELF64LEKind, EM_MIPS})
401 .Case("elf32-littleriscv", {ELF32LEKind, EM_RISCV})
402 .Case("elf64-littleriscv", {ELF64LEKind, EM_RISCV})
403 .Default({ELFNoneKind, EM_NONE});
406 // Parse OUTPUT_FORMAT(bfdname) or OUTPUT_FORMAT(bfdname, big, little).
407 // Currently we ignore big and little parameters.
408 void ScriptParser::readOutputFormat() {
411 StringRef name = unquote(next());
413 if (s.consume_back("-freebsd"))
414 config->osabi = ELFOSABI_FREEBSD;
416 std::tie(config->ekind, config->emachine) = parseBfdName(s);
417 if (config->emachine == EM_NONE)
418 setError("unknown output format name: " + name);
419 if (s == "elf32-ntradlittlemips" || s == "elf32-ntradbigmips")
420 config->mipsN32Abi = true;
431 void ScriptParser::readPhdrs() {
434 while (!errorCount() && !consume("}")) {
437 cmd.type = readPhdrType();
439 while (!errorCount() && !consume(";")) {
440 if (consume("FILEHDR"))
441 cmd.hasFilehdr = true;
442 else if (consume("PHDRS"))
444 else if (consume("AT"))
445 cmd.lmaExpr = readParenExpr();
446 else if (consume("FLAGS"))
447 cmd.flags = readParenExpr()().getValue();
449 setError("unexpected header attribute: " + next());
452 script->phdrsCommands.push_back(cmd);
456 void ScriptParser::readRegionAlias() {
458 StringRef alias = unquote(next());
460 StringRef name = next();
463 if (script->memoryRegions.count(alias))
464 setError("redefinition of memory region '" + alias + "'");
465 if (!script->memoryRegions.count(name))
466 setError("memory region '" + name + "' is not defined");
467 script->memoryRegions.insert({alias, script->memoryRegions[name]});
470 void ScriptParser::readSearchDir() {
472 StringRef tok = next();
473 if (!config->nostdlib)
474 config->searchPaths.push_back(unquote(tok));
478 // This reads an overlay description. Overlays are used to describe output
479 // sections that use the same virtual memory range and normally would trigger
480 // linker's sections sanity check failures.
481 // https://sourceware.org/binutils/docs/ld/Overlay-Description.html#Overlay-Description
482 std::vector<BaseCommand *> ScriptParser::readOverlay() {
483 // VA and LMA expressions are optional, though for simplicity of
484 // implementation we assume they are not. That is what OVERLAY was designed
485 // for first of all: to allow sections with overlapping VAs at different LMAs.
486 Expr addrExpr = readExpr();
489 Expr lmaExpr = readParenExpr();
492 std::vector<BaseCommand *> v;
493 OutputSection *prev = nullptr;
494 while (!errorCount() && !consume("}")) {
495 // VA is the same for all sections. The LMAs are consecutive in memory
496 // starting from the base load address specified.
497 OutputSection *os = readOverlaySectionDescription();
498 os->addrExpr = addrExpr;
500 os->lmaExpr = [=] { return prev->getLMA() + prev->size; };
502 os->lmaExpr = lmaExpr;
507 // According to the specification, at the end of the overlay, the location
508 // counter should be equal to the overlay base address plus size of the
509 // largest section seen in the overlay.
510 // Here we want to create the Dot assignment command to achieve that.
513 for (BaseCommand *cmd : v)
514 max = std::max(max, cast<OutputSection>(cmd)->size);
515 return addrExpr().getValue() + max;
517 v.push_back(make<SymbolAssignment>(".", moveDot, getCurrentLocation()));
521 void ScriptParser::readSections() {
522 script->hasSectionsCommand = true;
524 // -no-rosegment is used to avoid placing read only non-executable sections in
525 // their own segment. We do the same if SECTIONS command is present in linker
526 // script. See comment for computeFlags().
527 config->singleRoRx = true;
530 std::vector<BaseCommand *> v;
531 while (!errorCount() && !consume("}")) {
532 StringRef tok = next();
533 if (tok == "OVERLAY") {
534 for (BaseCommand *cmd : readOverlay())
537 } else if (tok == "INCLUDE") {
542 if (BaseCommand *cmd = readAssignment(tok))
545 v.push_back(readOutputSectionDescription(tok));
548 if (!atEOF() && consume("INSERT")) {
549 std::vector<BaseCommand *> *dest = nullptr;
550 if (consume("AFTER"))
551 dest = &script->insertAfterCommands[next()];
552 else if (consume("BEFORE"))
553 dest = &script->insertBeforeCommands[next()];
555 setError("expected AFTER/BEFORE, but got '" + next() + "'");
557 dest->insert(dest->end(), v.begin(), v.end());
561 script->sectionCommands.insert(script->sectionCommands.end(), v.begin(),
565 void ScriptParser::readTarget() {
566 // TARGET(foo) is an alias for "--format foo". Unlike GNU linkers,
567 // we accept only a limited set of BFD names (i.e. "elf" or "binary")
568 // for --format. We recognize only /^elf/ and "binary" in the linker
571 StringRef tok = next();
574 if (tok.startswith("elf"))
575 config->formatBinary = false;
576 else if (tok == "binary")
577 config->formatBinary = true;
579 setError("unknown target: " + tok);
582 static int precedence(StringRef op) {
583 return StringSwitch<int>(op)
584 .Cases("*", "/", "%", 8)
586 .Cases("<<", ">>", 6)
587 .Cases("<", "<=", ">", ">=", "==", "!=", 5)
595 StringMatcher ScriptParser::readFilePatterns() {
596 std::vector<StringRef> v;
597 while (!errorCount() && !consume(")"))
599 return StringMatcher(v);
602 SortSectionPolicy ScriptParser::readSortKind() {
603 if (consume("SORT") || consume("SORT_BY_NAME"))
604 return SortSectionPolicy::Name;
605 if (consume("SORT_BY_ALIGNMENT"))
606 return SortSectionPolicy::Alignment;
607 if (consume("SORT_BY_INIT_PRIORITY"))
608 return SortSectionPolicy::Priority;
609 if (consume("SORT_NONE"))
610 return SortSectionPolicy::None;
611 return SortSectionPolicy::Default;
614 // Reads SECTIONS command contents in the following form:
616 // <contents> ::= <elem>*
617 // <elem> ::= <exclude>? <glob-pattern>
618 // <exclude> ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
622 // *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
624 // is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
625 // The semantics of that is section .foo in any file, section .bar in
626 // any file but a.o, and section .baz in any file but b.o.
627 std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
628 std::vector<SectionPattern> ret;
629 while (!errorCount() && peek() != ")") {
630 StringMatcher excludeFilePat;
631 if (consume("EXCLUDE_FILE")) {
633 excludeFilePat = readFilePatterns();
636 std::vector<StringRef> v;
637 while (!errorCount() && peek() != ")" && peek() != "EXCLUDE_FILE")
638 v.push_back(unquote(next()));
641 ret.push_back({std::move(excludeFilePat), StringMatcher(v)});
643 setError("section pattern is expected");
648 // Reads contents of "SECTIONS" directive. That directive contains a
649 // list of glob patterns for input sections. The grammar is as follows.
651 // <patterns> ::= <section-list>
652 // | <sort> "(" <section-list> ")"
653 // | <sort> "(" <sort> "(" <section-list> ")" ")"
655 // <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
656 // | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
658 // <section-list> is parsed by readInputSectionsList().
659 InputSectionDescription *
660 ScriptParser::readInputSectionRules(StringRef filePattern) {
661 auto *cmd = make<InputSectionDescription>(filePattern);
664 while (!errorCount() && !consume(")")) {
665 SortSectionPolicy outer = readSortKind();
666 SortSectionPolicy inner = SortSectionPolicy::Default;
667 std::vector<SectionPattern> v;
668 if (outer != SortSectionPolicy::Default) {
670 inner = readSortKind();
671 if (inner != SortSectionPolicy::Default) {
673 v = readInputSectionsList();
676 v = readInputSectionsList();
680 v = readInputSectionsList();
683 for (SectionPattern &pat : v) {
684 pat.sortInner = inner;
685 pat.sortOuter = outer;
688 std::move(v.begin(), v.end(), std::back_inserter(cmd->sectionPatterns));
693 InputSectionDescription *
694 ScriptParser::readInputSectionDescription(StringRef tok) {
695 // Input section wildcard can be surrounded by KEEP.
696 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
699 StringRef filePattern = next();
700 InputSectionDescription *cmd = readInputSectionRules(filePattern);
702 script->keptSections.push_back(cmd);
705 return readInputSectionRules(tok);
708 void ScriptParser::readSort() {
710 expect("CONSTRUCTORS");
714 Expr ScriptParser::readAssert() {
718 StringRef msg = unquote(next());
724 return script->getDot();
728 // Tries to read the special directive for an output section definition which
729 // can be one of following: "(NOLOAD)", "(COPY)", "(INFO)" or "(OVERLAY)".
730 // Tok1 and Tok2 are next 2 tokens peeked. See comment for readSectionAddressType below.
731 bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok1, StringRef tok2) {
734 if (tok2 != "NOLOAD" && tok2 != "COPY" && tok2 != "INFO" && tok2 != "OVERLAY")
738 if (consume("NOLOAD")) {
740 cmd->type = SHT_NOBITS;
742 skip(); // This is "COPY", "INFO" or "OVERLAY".
743 cmd->nonAlloc = true;
749 // Reads an expression and/or the special directive for an output
750 // section definition. Directive is one of following: "(NOLOAD)",
751 // "(COPY)", "(INFO)" or "(OVERLAY)".
753 // An output section name can be followed by an address expression
754 // and/or directive. This grammar is not LL(1) because "(" can be
755 // interpreted as either the beginning of some expression or beginning
758 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
759 // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
760 void ScriptParser::readSectionAddressType(OutputSection *cmd) {
761 if (readSectionDirective(cmd, peek(), peek2()))
764 cmd->addrExpr = readExpr();
765 if (peek() == "(" && !readSectionDirective(cmd, "(", peek2()))
766 setError("unknown section directive: " + peek2());
769 static Expr checkAlignment(Expr e, std::string &loc) {
771 uint64_t alignment = std::max((uint64_t)1, e().getValue());
772 if (!isPowerOf2_64(alignment)) {
773 error(loc + ": alignment must be power of 2");
774 return (uint64_t)1; // Return a dummy value.
780 OutputSection *ScriptParser::readOverlaySectionDescription() {
782 script->createOutputSection(next(), getCurrentLocation());
783 cmd->inOverlay = true;
785 while (!errorCount() && !consume("}"))
786 cmd->sectionCommands.push_back(readInputSectionRules(next()));
787 cmd->phdrs = readOutputSectionPhdrs();
791 OutputSection *ScriptParser::readOutputSectionDescription(StringRef outSec) {
793 script->createOutputSection(outSec, getCurrentLocation());
795 size_t symbolsReferenced = script->referencedSymbols.size();
798 readSectionAddressType(cmd);
801 std::string location = getCurrentLocation();
803 cmd->lmaExpr = readParenExpr();
804 if (consume("ALIGN"))
805 cmd->alignExpr = checkAlignment(readParenExpr(), location);
806 if (consume("SUBALIGN"))
807 cmd->subalignExpr = checkAlignment(readParenExpr(), location);
809 // Parse constraints.
810 if (consume("ONLY_IF_RO"))
811 cmd->constraint = ConstraintKind::ReadOnly;
812 if (consume("ONLY_IF_RW"))
813 cmd->constraint = ConstraintKind::ReadWrite;
816 while (!errorCount() && !consume("}")) {
817 StringRef tok = next();
819 // Empty commands are allowed. Do nothing here.
820 } else if (SymbolAssignment *assign = readAssignment(tok)) {
821 cmd->sectionCommands.push_back(assign);
822 } else if (ByteCommand *data = readByteCommand(tok)) {
823 cmd->sectionCommands.push_back(data);
824 } else if (tok == "CONSTRUCTORS") {
825 // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
826 // by name. This is for very old file formats such as ECOFF/XCOFF.
827 // For ELF, we should ignore.
828 } else if (tok == "FILL") {
829 // We handle the FILL command as an alias for =fillexp section attribute,
830 // which is different from what GNU linkers do.
831 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
833 cmd->filler = readFill();
835 } else if (tok == "SORT") {
837 } else if (tok == "INCLUDE") {
839 } else if (peek() == "(") {
840 cmd->sectionCommands.push_back(readInputSectionDescription(tok));
842 // We have a file name and no input sections description. It is not a
843 // commonly used syntax, but still acceptable. In that case, all sections
844 // from the file will be included.
845 auto *isd = make<InputSectionDescription>(tok);
846 isd->sectionPatterns.push_back({{}, StringMatcher({"*"})});
847 cmd->sectionCommands.push_back(isd);
852 cmd->memoryRegionName = next();
856 cmd->lmaRegionName = next();
859 if (cmd->lmaExpr && !cmd->lmaRegionName.empty())
860 error("section can't have both LMA and a load region");
862 cmd->phdrs = readOutputSectionPhdrs();
864 if (peek() == "=" || peek().startswith("=")) {
867 cmd->filler = readFill();
871 // Consume optional comma following output section command.
874 if (script->referencedSymbols.size() > symbolsReferenced)
875 cmd->expressionsUseSymbols = true;
879 // Reads a `=<fillexp>` expression and returns its value as a big-endian number.
880 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
881 // We do not support using symbols in such expressions.
883 // When reading a hexstring, ld.bfd handles it as a blob of arbitrary
884 // size, while ld.gold always handles it as a 32-bit big-endian number.
885 // We are compatible with ld.gold because it's easier to implement.
886 std::array<uint8_t, 4> ScriptParser::readFill() {
887 uint64_t value = readExpr()().val;
888 if (value > UINT32_MAX)
889 setError("filler expression result does not fit 32-bit: 0x" +
890 Twine::utohexstr(value));
892 std::array<uint8_t, 4> buf;
893 write32be(buf.data(), (uint32_t)value);
897 SymbolAssignment *ScriptParser::readProvideHidden(bool provide, bool hidden) {
899 SymbolAssignment *cmd = readSymbolAssignment(next());
900 cmd->provide = provide;
901 cmd->hidden = hidden;
906 SymbolAssignment *ScriptParser::readAssignment(StringRef tok) {
907 // Assert expression returns Dot, so this is equal to ".=."
909 return make<SymbolAssignment>(".", readAssert(), getCurrentLocation());
912 SymbolAssignment *cmd = nullptr;
913 if (peek() == "=" || peek() == "+=")
914 cmd = readSymbolAssignment(tok);
915 else if (tok == "PROVIDE")
916 cmd = readProvideHidden(true, false);
917 else if (tok == "HIDDEN")
918 cmd = readProvideHidden(false, true);
919 else if (tok == "PROVIDE_HIDDEN")
920 cmd = readProvideHidden(true, true);
925 llvm::join(tokens.begin() + oldPos, tokens.begin() + pos, " ");
931 SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef name) {
932 StringRef op = next();
933 assert(op == "=" || op == "+=");
936 std::string loc = getCurrentLocation();
937 e = [=] { return add(script->getSymbolValue(name, loc), e()); };
939 return make<SymbolAssignment>(name, e, getCurrentLocation());
942 // This is an operator-precedence parser to parse a linker
943 // script expression.
944 Expr ScriptParser::readExpr() {
945 // Our lexer is context-aware. Set the in-expression bit so that
946 // they apply different tokenization rules.
949 Expr e = readExpr1(readPrimary(), 0);
954 Expr ScriptParser::combine(StringRef op, Expr l, Expr r) {
956 return [=] { return add(l(), r()); };
958 return [=] { return sub(l(), r()); };
960 return [=] { return l().getValue() * r().getValue(); };
962 std::string loc = getCurrentLocation();
963 return [=]() -> uint64_t {
964 if (uint64_t rv = r().getValue())
965 return l().getValue() / rv;
966 error(loc + ": division by zero");
971 std::string loc = getCurrentLocation();
972 return [=]() -> uint64_t {
973 if (uint64_t rv = r().getValue())
974 return l().getValue() % rv;
975 error(loc + ": modulo by zero");
980 return [=] { return l().getValue() << r().getValue(); };
982 return [=] { return l().getValue() >> r().getValue(); };
984 return [=] { return l().getValue() < r().getValue(); };
986 return [=] { return l().getValue() > r().getValue(); };
988 return [=] { return l().getValue() >= r().getValue(); };
990 return [=] { return l().getValue() <= r().getValue(); };
992 return [=] { return l().getValue() == r().getValue(); };
994 return [=] { return l().getValue() != r().getValue(); };
996 return [=] { return l().getValue() || r().getValue(); };
998 return [=] { return l().getValue() && r().getValue(); };
1000 return [=] { return bitAnd(l(), r()); };
1002 return [=] { return bitOr(l(), r()); };
1003 llvm_unreachable("invalid operator");
1006 // This is a part of the operator-precedence parser. This function
1007 // assumes that the remaining token stream starts with an operator.
1008 Expr ScriptParser::readExpr1(Expr lhs, int minPrec) {
1009 while (!atEOF() && !errorCount()) {
1010 // Read an operator and an expression.
1012 return readTernary(lhs);
1013 StringRef op1 = peek();
1014 if (precedence(op1) < minPrec)
1017 Expr rhs = readPrimary();
1019 // Evaluate the remaining part of the expression first if the
1020 // next operator has greater precedence than the previous one.
1021 // For example, if we have read "+" and "3", and if the next
1022 // operator is "*", then we'll evaluate 3 * ... part first.
1024 StringRef op2 = peek();
1025 if (precedence(op2) <= precedence(op1))
1027 rhs = readExpr1(rhs, precedence(op2));
1030 lhs = combine(op1, lhs, rhs);
1035 Expr ScriptParser::getPageSize() {
1036 std::string location = getCurrentLocation();
1037 return [=]() -> uint64_t {
1039 return config->commonPageSize;
1040 error(location + ": unable to calculate page size");
1041 return 4096; // Return a dummy value.
1045 Expr ScriptParser::readConstant() {
1046 StringRef s = readParenLiteral();
1047 if (s == "COMMONPAGESIZE")
1048 return getPageSize();
1049 if (s == "MAXPAGESIZE")
1050 return [] { return config->maxPageSize; };
1051 setError("unknown constant: " + s);
1052 return [] { return 0; };
1055 // Parses Tok as an integer. It recognizes hexadecimal (prefixed with
1056 // "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
1057 // have "K" (Ki) or "M" (Mi) suffixes.
1058 static Optional<uint64_t> parseInt(StringRef tok) {
1061 if (tok.startswith_lower("0x")) {
1062 if (!to_integer(tok.substr(2), val, 16))
1066 if (tok.endswith_lower("H")) {
1067 if (!to_integer(tok.drop_back(), val, 16))
1073 if (tok.endswith_lower("K")) {
1074 if (!to_integer(tok.drop_back(), val, 10))
1078 if (tok.endswith_lower("M")) {
1079 if (!to_integer(tok.drop_back(), val, 10))
1081 return val * 1024 * 1024;
1083 if (!to_integer(tok, val, 10))
1088 ByteCommand *ScriptParser::readByteCommand(StringRef tok) {
1089 int size = StringSwitch<int>(tok)
1098 size_t oldPos = pos;
1099 Expr e = readParenExpr();
1100 std::string commandString =
1102 llvm::join(tokens.begin() + oldPos, tokens.begin() + pos, " ");
1103 return make<ByteCommand>(e, size, commandString);
1106 StringRef ScriptParser::readParenLiteral() {
1110 StringRef tok = next();
1116 static void checkIfExists(OutputSection *cmd, StringRef location) {
1117 if (cmd->location.empty() && script->errorOnMissingSection)
1118 error(location + ": undefined section " + cmd->name);
1121 Expr ScriptParser::readPrimary() {
1123 return readParenExpr();
1126 Expr e = readPrimary();
1127 return [=] { return ~e().getValue(); };
1130 Expr e = readPrimary();
1131 return [=] { return !e().getValue(); };
1134 Expr e = readPrimary();
1135 return [=] { return -e().getValue(); };
1138 StringRef tok = next();
1139 std::string location = getCurrentLocation();
1141 // Built-in functions are parsed here.
1142 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1143 if (tok == "ABSOLUTE") {
1144 Expr inner = readParenExpr();
1146 ExprValue i = inner();
1147 i.forceAbsolute = true;
1151 if (tok == "ADDR") {
1152 StringRef name = readParenLiteral();
1153 OutputSection *sec = script->getOrCreateOutputSection(name);
1154 sec->usedInExpression = true;
1155 return [=]() -> ExprValue {
1156 checkIfExists(sec, location);
1157 return {sec, false, 0, location};
1160 if (tok == "ALIGN") {
1162 Expr e = readExpr();
1164 e = checkAlignment(e, location);
1165 return [=] { return alignTo(script->getDot(), e().getValue()); };
1168 Expr e2 = checkAlignment(readExpr(), location);
1172 v.alignment = e2().getValue();
1176 if (tok == "ALIGNOF") {
1177 StringRef name = readParenLiteral();
1178 OutputSection *cmd = script->getOrCreateOutputSection(name);
1180 checkIfExists(cmd, location);
1181 return cmd->alignment;
1184 if (tok == "ASSERT")
1185 return readAssert();
1186 if (tok == "CONSTANT")
1187 return readConstant();
1188 if (tok == "DATA_SEGMENT_ALIGN") {
1190 Expr e = readExpr();
1195 return alignTo(script->getDot(), std::max((uint64_t)1, e().getValue()));
1198 if (tok == "DATA_SEGMENT_END") {
1202 return [] { return script->getDot(); };
1204 if (tok == "DATA_SEGMENT_RELRO_END") {
1205 // GNU linkers implements more complicated logic to handle
1206 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and
1207 // just align to the next page boundary for simplicity.
1213 Expr e = getPageSize();
1214 return [=] { return alignTo(script->getDot(), e().getValue()); };
1216 if (tok == "DEFINED") {
1217 StringRef name = readParenLiteral();
1218 return [=] { return symtab->find(name) ? 1 : 0; };
1220 if (tok == "LENGTH") {
1221 StringRef name = readParenLiteral();
1222 if (script->memoryRegions.count(name) == 0) {
1223 setError("memory region not defined: " + name);
1224 return [] { return 0; };
1226 return [=] { return script->memoryRegions[name]->length; };
1228 if (tok == "LOADADDR") {
1229 StringRef name = readParenLiteral();
1230 OutputSection *cmd = script->getOrCreateOutputSection(name);
1231 cmd->usedInExpression = true;
1233 checkIfExists(cmd, location);
1234 return cmd->getLMA();
1237 if (tok == "MAX" || tok == "MIN") {
1239 Expr a = readExpr();
1241 Expr b = readExpr();
1244 return [=] { return std::min(a().getValue(), b().getValue()); };
1245 return [=] { return std::max(a().getValue(), b().getValue()); };
1247 if (tok == "ORIGIN") {
1248 StringRef name = readParenLiteral();
1249 if (script->memoryRegions.count(name) == 0) {
1250 setError("memory region not defined: " + name);
1251 return [] { return 0; };
1253 return [=] { return script->memoryRegions[name]->origin; };
1255 if (tok == "SEGMENT_START") {
1259 Expr e = readExpr();
1261 return [=] { return e(); };
1263 if (tok == "SIZEOF") {
1264 StringRef name = readParenLiteral();
1265 OutputSection *cmd = script->getOrCreateOutputSection(name);
1266 // Linker script does not create an output section if its content is empty.
1267 // We want to allow SIZEOF(.foo) where .foo is a section which happened to
1269 return [=] { return cmd->size; };
1271 if (tok == "SIZEOF_HEADERS")
1272 return [=] { return getHeaderSize(); };
1276 return [=] { return script->getSymbolValue(tok, location); };
1278 // Tok is a literal number.
1279 if (Optional<uint64_t> val = parseInt(tok))
1280 return [=] { return *val; };
1282 // Tok is a symbol name.
1283 if (!isValidCIdentifier(tok))
1284 setError("malformed number: " + tok);
1285 script->referencedSymbols.push_back(tok);
1286 return [=] { return script->getSymbolValue(tok, location); };
1289 Expr ScriptParser::readTernary(Expr cond) {
1290 Expr l = readExpr();
1292 Expr r = readExpr();
1293 return [=] { return cond().getValue() ? l() : r(); };
1296 Expr ScriptParser::readParenExpr() {
1298 Expr e = readExpr();
1303 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1304 std::vector<StringRef> phdrs;
1305 while (!errorCount() && peek().startswith(":")) {
1306 StringRef tok = next();
1307 phdrs.push_back((tok.size() == 1) ? next() : tok.substr(1));
1312 // Read a program header type name. The next token must be a
1313 // name of a program header type or a constant (e.g. "0x3").
1314 unsigned ScriptParser::readPhdrType() {
1315 StringRef tok = next();
1316 if (Optional<uint64_t> val = parseInt(tok))
1319 unsigned ret = StringSwitch<unsigned>(tok)
1320 .Case("PT_NULL", PT_NULL)
1321 .Case("PT_LOAD", PT_LOAD)
1322 .Case("PT_DYNAMIC", PT_DYNAMIC)
1323 .Case("PT_INTERP", PT_INTERP)
1324 .Case("PT_NOTE", PT_NOTE)
1325 .Case("PT_SHLIB", PT_SHLIB)
1326 .Case("PT_PHDR", PT_PHDR)
1327 .Case("PT_TLS", PT_TLS)
1328 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1329 .Case("PT_GNU_STACK", PT_GNU_STACK)
1330 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1331 .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1332 .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1333 .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
1336 if (ret == (unsigned)-1) {
1337 setError("invalid program header type: " + tok);
1343 // Reads an anonymous version declaration.
1344 void ScriptParser::readAnonymousDeclaration() {
1345 std::vector<SymbolVersion> locals;
1346 std::vector<SymbolVersion> globals;
1347 std::tie(locals, globals) = readSymbols();
1348 for (const SymbolVersion &pat : locals)
1349 config->versionDefinitions[VER_NDX_LOCAL].patterns.push_back(pat);
1350 for (const SymbolVersion &pat : globals)
1351 config->versionDefinitions[VER_NDX_GLOBAL].patterns.push_back(pat);
1356 // Reads a non-anonymous version definition,
1357 // e.g. "VerStr { global: foo; bar; local: *; };".
1358 void ScriptParser::readVersionDeclaration(StringRef verStr) {
1359 // Read a symbol list.
1360 std::vector<SymbolVersion> locals;
1361 std::vector<SymbolVersion> globals;
1362 std::tie(locals, globals) = readSymbols();
1363 for (const SymbolVersion &pat : locals)
1364 config->versionDefinitions[VER_NDX_LOCAL].patterns.push_back(pat);
1366 // Create a new version definition and add that to the global symbols.
1367 VersionDefinition ver;
1369 ver.patterns = globals;
1370 ver.id = config->versionDefinitions.size();
1371 config->versionDefinitions.push_back(ver);
1373 // Each version may have a parent version. For example, "Ver2"
1374 // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1375 // as a parent. This version hierarchy is, probably against your
1376 // instinct, purely for hint; the runtime doesn't care about it
1377 // at all. In LLD, we simply ignore it.
1383 static bool hasWildcard(StringRef s) {
1384 return s.find_first_of("?*[") != StringRef::npos;
1387 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1388 std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
1389 ScriptParser::readSymbols() {
1390 std::vector<SymbolVersion> locals;
1391 std::vector<SymbolVersion> globals;
1392 std::vector<SymbolVersion> *v = &globals;
1394 while (!errorCount()) {
1397 if (consumeLabel("local")) {
1401 if (consumeLabel("global")) {
1406 if (consume("extern")) {
1407 std::vector<SymbolVersion> ext = readVersionExtern();
1408 v->insert(v->end(), ext.begin(), ext.end());
1410 StringRef tok = next();
1411 v->push_back({unquote(tok), false, hasWildcard(tok)});
1415 return {locals, globals};
1418 // Reads an "extern C++" directive, e.g.,
1419 // "extern "C++" { ns::*; "f(int, double)"; };"
1421 // The last semicolon is optional. E.g. this is OK:
1422 // "extern "C++" { ns::*; "f(int, double)" };"
1423 std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
1424 StringRef tok = next();
1425 bool isCXX = tok == "\"C++\"";
1426 if (!isCXX && tok != "\"C\"")
1427 setError("Unknown language");
1430 std::vector<SymbolVersion> ret;
1431 while (!errorCount() && peek() != "}") {
1432 StringRef tok = next();
1434 {unquote(tok), isCXX, !tok.startswith("\"") && hasWildcard(tok)});
1444 uint64_t ScriptParser::readMemoryAssignment(StringRef s1, StringRef s2,
1446 if (!consume(s1) && !consume(s2) && !consume(s3)) {
1447 setError("expected one of: " + s1 + ", " + s2 + ", or " + s3);
1451 return readExpr()().getValue();
1454 // Parse the MEMORY command as specified in:
1455 // https://sourceware.org/binutils/docs/ld/MEMORY.html
1457 // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
1458 void ScriptParser::readMemory() {
1460 while (!errorCount() && !consume("}")) {
1461 StringRef tok = next();
1462 if (tok == "INCLUDE") {
1468 uint32_t negFlags = 0;
1470 std::tie(flags, negFlags) = readMemoryAttributes();
1475 uint64_t origin = readMemoryAssignment("ORIGIN", "org", "o");
1477 uint64_t length = readMemoryAssignment("LENGTH", "len", "l");
1479 // Add the memory region to the region map.
1480 MemoryRegion *mr = make<MemoryRegion>(tok, origin, length, flags, negFlags);
1481 if (!script->memoryRegions.insert({tok, mr}).second)
1482 setError("region '" + tok + "' already defined");
1486 // This function parses the attributes used to match against section
1487 // flags when placing output sections in a memory region. These flags
1488 // are only used when an explicit memory region name is not used.
1489 std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() {
1491 uint32_t negFlags = 0;
1492 bool invert = false;
1494 for (char c : next().lower()) {
1501 flag = SHF_EXECINSTR;
1505 setError("invalid memory region attribute");
1512 return {flags, negFlags};
1515 void readLinkerScript(MemoryBufferRef mb) {
1516 ScriptParser(mb).readLinkerScript();
1519 void readVersionScript(MemoryBufferRef mb) {
1520 ScriptParser(mb).readVersionScript();
1523 void readDynamicList(MemoryBufferRef mb) { ScriptParser(mb).readDynamicList(); }
1525 void readDefsym(StringRef name, MemoryBufferRef mb) {
1526 ScriptParser(mb).readDefsym(name);