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 "InputFiles.h"
18 #include "LinkerScript.h"
19 #include "OutputSections.h"
20 #include "ScriptLexer.h"
21 #include "SymbolTable.h"
24 #include "lld/Common/CommonLinkerContext.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/StringSet.h"
28 #include "llvm/ADT/StringSwitch.h"
29 #include "llvm/BinaryFormat/ELF.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/Path.h"
35 #include "llvm/Support/SaveAndRestore.h"
36 #include "llvm/Support/TimeProfiler.h"
43 using namespace llvm::ELF
;
44 using namespace llvm::support::endian
;
46 using namespace lld::elf
;
49 class ScriptParser final
: ScriptLexer
{
51 ScriptParser(MemoryBufferRef mb
) : ScriptLexer(mb
) {
52 // Initialize IsUnderSysroot
53 if (config
->sysroot
== "")
55 StringRef path
= mb
.getBufferIdentifier();
56 for (; !path
.empty(); path
= sys::path::parent_path(path
)) {
57 if (!sys::fs::equivalent(config
->sysroot
, path
))
59 isUnderSysroot
= true;
64 void readLinkerScript();
65 void readVersionScript();
66 void readDynamicList();
67 void readDefsym(StringRef name
);
70 void addFile(StringRef path
);
80 void readOutputArch();
81 void readOutputFormat();
82 void readOverwriteSections();
84 void readRegionAlias();
89 void readVersionScriptCommand();
90 void readNoCrossRefs(bool to
);
92 SymbolAssignment
*readSymbolAssignment(StringRef name
);
93 ByteCommand
*readByteCommand(StringRef tok
);
94 std::array
<uint8_t, 4> readFill();
95 bool readSectionDirective(OutputSection
*cmd
, StringRef tok
);
96 void readSectionAddressType(OutputSection
*cmd
);
97 OutputDesc
*readOverlaySectionDescription();
98 OutputDesc
*readOutputSectionDescription(StringRef outSec
);
99 SmallVector
<SectionCommand
*, 0> readOverlay();
100 SmallVector
<StringRef
, 0> readOutputSectionPhdrs();
101 std::pair
<uint64_t, uint64_t> readInputSectionFlags();
102 InputSectionDescription
*readInputSectionDescription(StringRef tok
);
103 StringMatcher
readFilePatterns();
104 SmallVector
<SectionPattern
, 0> readInputSectionsList();
105 InputSectionDescription
*readInputSectionRules(StringRef filePattern
,
107 uint64_t withoutFlags
);
108 unsigned readPhdrType();
109 SortSectionPolicy
peekSortKind();
110 SortSectionPolicy
readSortKind();
111 SymbolAssignment
*readProvideHidden(bool provide
, bool hidden
);
112 SymbolAssignment
*readAssignment(StringRef tok
);
118 Expr
readMemoryAssignment(StringRef
, StringRef
, StringRef
);
119 void readMemoryAttributes(uint32_t &flags
, uint32_t &invFlags
,
120 uint32_t &negFlags
, uint32_t &negInvFlags
);
122 Expr
combine(StringRef op
, Expr l
, Expr r
);
124 Expr
readExpr1(Expr lhs
, int minPrec
);
125 StringRef
readParenLiteral();
127 Expr
readTernary(Expr cond
);
128 Expr
readParenExpr();
130 // For parsing version script.
131 SmallVector
<SymbolVersion
, 0> readVersionExtern();
132 void readAnonymousDeclaration();
133 void readVersionDeclaration(StringRef verStr
);
135 std::pair
<SmallVector
<SymbolVersion
, 0>, SmallVector
<SymbolVersion
, 0>>
138 // True if a script being read is in the --sysroot directory.
139 bool isUnderSysroot
= false;
141 // A set to detect an INCLUDE() cycle.
144 // If we are currently parsing a PROVIDE|PROVIDE_HIDDEN command,
145 // then this member is set to the PROVIDE symbol name.
146 std::optional
<llvm::StringRef
> activeProvideSym
;
150 static StringRef
unquote(StringRef s
) {
151 if (s
.starts_with("\""))
152 return s
.substr(1, s
.size() - 2);
156 // Some operations only support one non absolute value. Move the
157 // absolute one to the right hand side for convenience.
158 static void moveAbsRight(ExprValue
&a
, ExprValue
&b
) {
159 if (a
.sec
== nullptr || (a
.forceAbsolute
&& !b
.isAbsolute()))
163 a
.loc
+ ": at least one side of the expression must be absolute");
166 static ExprValue
add(ExprValue a
, ExprValue b
) {
168 return {a
.sec
, a
.forceAbsolute
, a
.getSectionOffset() + b
.getValue(), a
.loc
};
171 static ExprValue
sub(ExprValue a
, ExprValue b
) {
172 // The distance between two symbols in sections is absolute.
173 if (!a
.isAbsolute() && !b
.isAbsolute())
174 return a
.getValue() - b
.getValue();
175 return {a
.sec
, false, a
.getSectionOffset() - b
.getValue(), a
.loc
};
178 static ExprValue
bitAnd(ExprValue a
, ExprValue b
) {
180 return {a
.sec
, a
.forceAbsolute
,
181 (a
.getValue() & b
.getValue()) - a
.getSecAddr(), a
.loc
};
184 static ExprValue
bitXor(ExprValue a
, ExprValue b
) {
186 return {a
.sec
, a
.forceAbsolute
,
187 (a
.getValue() ^ b
.getValue()) - a
.getSecAddr(), a
.loc
};
190 static ExprValue
bitOr(ExprValue a
, ExprValue b
) {
192 return {a
.sec
, a
.forceAbsolute
,
193 (a
.getValue() | b
.getValue()) - a
.getSecAddr(), a
.loc
};
196 void ScriptParser::readDynamicList() {
198 SmallVector
<SymbolVersion
, 0> locals
;
199 SmallVector
<SymbolVersion
, 0> globals
;
200 std::tie(locals
, globals
) = readSymbols();
204 setError("EOF expected, but got " + next());
207 if (!locals
.empty()) {
208 setError("\"local:\" scope not supported in --dynamic-list");
212 for (SymbolVersion v
: globals
)
213 config
->dynamicList
.push_back(v
);
216 void ScriptParser::readVersionScript() {
217 readVersionScriptCommand();
219 setError("EOF expected, but got " + next());
222 void ScriptParser::readVersionScriptCommand() {
224 readAnonymousDeclaration();
228 while (!atEOF() && !errorCount() && peek() != "}") {
229 StringRef verStr
= next();
231 setError("anonymous version definition is used in "
232 "combination with other version definitions");
236 readVersionDeclaration(verStr
);
240 void ScriptParser::readVersion() {
242 readVersionScriptCommand();
246 void ScriptParser::readLinkerScript() {
248 StringRef tok
= next();
252 if (tok
== "ENTRY") {
254 } else if (tok
== "EXTERN") {
256 } else if (tok
== "GROUP") {
258 } else if (tok
== "INCLUDE") {
260 } else if (tok
== "INPUT") {
262 } else if (tok
== "MEMORY") {
264 } else if (tok
== "OUTPUT") {
266 } else if (tok
== "OUTPUT_ARCH") {
268 } else if (tok
== "OUTPUT_FORMAT") {
270 } else if (tok
== "OVERWRITE_SECTIONS") {
271 readOverwriteSections();
272 } else if (tok
== "PHDRS") {
274 } else if (tok
== "REGION_ALIAS") {
276 } else if (tok
== "SEARCH_DIR") {
278 } else if (tok
== "SECTIONS") {
280 } else if (tok
== "TARGET") {
282 } else if (tok
== "VERSION") {
284 } else if (tok
== "NOCROSSREFS") {
285 readNoCrossRefs(/*to=*/false);
286 } else if (tok
== "NOCROSSREFS_TO") {
287 readNoCrossRefs(/*to=*/true);
288 } else if (SymbolAssignment
*cmd
= readAssignment(tok
)) {
289 script
->sectionCommands
.push_back(cmd
);
291 setError("unknown directive: " + tok
);
296 void ScriptParser::readDefsym(StringRef name
) {
301 setError("EOF expected, but got " + next());
302 auto *cmd
= make
<SymbolAssignment
>(
303 name
, e
, 0, getCurrentMB().getBufferIdentifier().str());
304 script
->sectionCommands
.push_back(cmd
);
307 void ScriptParser::readNoCrossRefs(bool to
) {
309 NoCrossRefCommand cmd
{{}, to
};
310 while (!errorCount() && !consume(")"))
311 cmd
.outputSections
.push_back(unquote(next()));
312 if (cmd
.outputSections
.size() < 2)
313 warn(getCurrentLocation() + ": ignored with fewer than 2 output sections");
315 script
->noCrossRefs
.push_back(std::move(cmd
));
318 void ScriptParser::addFile(StringRef s
) {
319 if (isUnderSysroot
&& s
.starts_with("/")) {
320 SmallString
<128> pathData
;
321 StringRef path
= (config
->sysroot
+ s
).toStringRef(pathData
);
322 if (sys::fs::exists(path
))
323 ctx
.driver
.addFile(saver().save(path
), /*withLOption=*/false);
325 setError("cannot find " + s
+ " inside " + config
->sysroot
);
329 if (s
.starts_with("/")) {
330 // Case 1: s is an absolute path. Just open it.
331 ctx
.driver
.addFile(s
, /*withLOption=*/false);
332 } else if (s
.starts_with("=")) {
333 // Case 2: relative to the sysroot.
334 if (config
->sysroot
.empty())
335 ctx
.driver
.addFile(s
.substr(1), /*withLOption=*/false);
337 ctx
.driver
.addFile(saver().save(config
->sysroot
+ "/" + s
.substr(1)),
338 /*withLOption=*/false);
339 } else if (s
.starts_with("-l")) {
340 // Case 3: search in the list of library paths.
341 ctx
.driver
.addLibrary(s
.substr(2));
343 // Case 4: s is a relative path. Search in the directory of the script file.
344 std::string filename
= std::string(getCurrentMB().getBufferIdentifier());
345 StringRef directory
= sys::path::parent_path(filename
);
346 if (!directory
.empty()) {
347 SmallString
<0> path(directory
);
348 sys::path::append(path
, s
);
349 if (sys::fs::exists(path
)) {
350 ctx
.driver
.addFile(path
, /*withLOption=*/false);
354 // Then search in the current working directory.
355 if (sys::fs::exists(s
)) {
356 ctx
.driver
.addFile(s
, /*withLOption=*/false);
358 // Finally, search in the list of library paths.
359 if (std::optional
<std::string
> path
= findFromSearchPaths(s
))
360 ctx
.driver
.addFile(saver().save(*path
), /*withLOption=*/true);
362 setError("unable to find " + s
);
367 void ScriptParser::readAsNeeded() {
369 bool orig
= config
->asNeeded
;
370 config
->asNeeded
= true;
371 while (!errorCount() && !consume(")"))
372 addFile(unquote(next()));
373 config
->asNeeded
= orig
;
376 void ScriptParser::readEntry() {
377 // -e <symbol> takes predecence over ENTRY(<symbol>).
379 StringRef tok
= next();
380 if (config
->entry
.empty())
381 config
->entry
= unquote(tok
);
385 void ScriptParser::readExtern() {
387 while (!errorCount() && !consume(")"))
388 config
->undefined
.push_back(unquote(next()));
391 void ScriptParser::readGroup() {
392 bool orig
= InputFile::isInGroup
;
393 InputFile::isInGroup
= true;
395 InputFile::isInGroup
= orig
;
397 ++InputFile::nextGroupId
;
400 void ScriptParser::readInclude() {
401 StringRef tok
= unquote(next());
403 if (!seen
.insert(tok
).second
) {
404 setError("there is a cycle in linker script INCLUDEs");
408 if (std::optional
<std::string
> path
= searchScript(tok
)) {
409 if (std::optional
<MemoryBufferRef
> mb
= readFile(*path
))
413 setError("cannot find linker script " + tok
);
416 void ScriptParser::readInput() {
418 while (!errorCount() && !consume(")")) {
419 if (consume("AS_NEEDED"))
422 addFile(unquote(next()));
426 void ScriptParser::readOutput() {
427 // -o <file> takes predecence over OUTPUT(<file>).
429 StringRef tok
= next();
430 if (config
->outputFile
.empty())
431 config
->outputFile
= unquote(tok
);
435 void ScriptParser::readOutputArch() {
436 // OUTPUT_ARCH is ignored for now.
438 while (!errorCount() && !consume(")"))
442 static std::pair
<ELFKind
, uint16_t> parseBfdName(StringRef s
) {
443 return StringSwitch
<std::pair
<ELFKind
, uint16_t>>(s
)
444 .Case("elf32-i386", {ELF32LEKind
, EM_386
})
445 .Case("elf32-avr", {ELF32LEKind
, EM_AVR
})
446 .Case("elf32-iamcu", {ELF32LEKind
, EM_IAMCU
})
447 .Case("elf32-littlearm", {ELF32LEKind
, EM_ARM
})
448 .Case("elf32-bigarm", {ELF32BEKind
, EM_ARM
})
449 .Case("elf32-x86-64", {ELF32LEKind
, EM_X86_64
})
450 .Case("elf64-aarch64", {ELF64LEKind
, EM_AARCH64
})
451 .Case("elf64-littleaarch64", {ELF64LEKind
, EM_AARCH64
})
452 .Case("elf64-bigaarch64", {ELF64BEKind
, EM_AARCH64
})
453 .Case("elf32-powerpc", {ELF32BEKind
, EM_PPC
})
454 .Case("elf32-powerpcle", {ELF32LEKind
, EM_PPC
})
455 .Case("elf64-powerpc", {ELF64BEKind
, EM_PPC64
})
456 .Case("elf64-powerpcle", {ELF64LEKind
, EM_PPC64
})
457 .Case("elf64-x86-64", {ELF64LEKind
, EM_X86_64
})
458 .Cases("elf32-tradbigmips", "elf32-bigmips", {ELF32BEKind
, EM_MIPS
})
459 .Case("elf32-ntradbigmips", {ELF32BEKind
, EM_MIPS
})
460 .Case("elf32-tradlittlemips", {ELF32LEKind
, EM_MIPS
})
461 .Case("elf32-ntradlittlemips", {ELF32LEKind
, EM_MIPS
})
462 .Case("elf64-tradbigmips", {ELF64BEKind
, EM_MIPS
})
463 .Case("elf64-tradlittlemips", {ELF64LEKind
, EM_MIPS
})
464 .Case("elf32-littleriscv", {ELF32LEKind
, EM_RISCV
})
465 .Case("elf64-littleriscv", {ELF64LEKind
, EM_RISCV
})
466 .Case("elf64-sparc", {ELF64BEKind
, EM_SPARCV9
})
467 .Case("elf32-msp430", {ELF32LEKind
, EM_MSP430
})
468 .Case("elf32-loongarch", {ELF32LEKind
, EM_LOONGARCH
})
469 .Case("elf64-loongarch", {ELF64LEKind
, EM_LOONGARCH
})
470 .Case("elf64-s390", {ELF64BEKind
, EM_S390
})
471 .Cases("elf32-hexagon", "elf32-littlehexagon", {ELF32LEKind
, EM_HEXAGON
})
472 .Default({ELFNoneKind
, EM_NONE
});
475 // Parse OUTPUT_FORMAT(bfdname) or OUTPUT_FORMAT(default, big, little). Choose
476 // big if -EB is specified, little if -EL is specified, or default if neither is
478 void ScriptParser::readOutputFormat() {
481 StringRef s
= unquote(next());
484 StringRef tmp
= unquote(next());
488 tmp
= unquote(next());
493 // If more than one OUTPUT_FORMAT is specified, only the first is checked.
494 if (!config
->bfdname
.empty())
499 config
->oFormatBinary
= true;
503 if (s
.consume_back("-freebsd"))
504 config
->osabi
= ELFOSABI_FREEBSD
;
506 std::tie(config
->ekind
, config
->emachine
) = parseBfdName(s
);
507 if (config
->emachine
== EM_NONE
)
508 setError("unknown output format name: " + config
->bfdname
);
509 if (s
== "elf32-ntradlittlemips" || s
== "elf32-ntradbigmips")
510 config
->mipsN32Abi
= true;
511 if (config
->emachine
== EM_MSP430
)
512 config
->osabi
= ELFOSABI_STANDALONE
;
515 void ScriptParser::readPhdrs() {
518 while (!errorCount() && !consume("}")) {
521 cmd
.type
= readPhdrType();
523 while (!errorCount() && !consume(";")) {
524 if (consume("FILEHDR"))
525 cmd
.hasFilehdr
= true;
526 else if (consume("PHDRS"))
528 else if (consume("AT"))
529 cmd
.lmaExpr
= readParenExpr();
530 else if (consume("FLAGS"))
531 cmd
.flags
= readParenExpr()().getValue();
533 setError("unexpected header attribute: " + next());
536 script
->phdrsCommands
.push_back(cmd
);
540 void ScriptParser::readRegionAlias() {
542 StringRef alias
= unquote(next());
544 StringRef name
= next();
547 if (script
->memoryRegions
.count(alias
))
548 setError("redefinition of memory region '" + alias
+ "'");
549 if (!script
->memoryRegions
.count(name
))
550 setError("memory region '" + name
+ "' is not defined");
551 script
->memoryRegions
.insert({alias
, script
->memoryRegions
[name
]});
554 void ScriptParser::readSearchDir() {
556 StringRef tok
= next();
557 if (!config
->nostdlib
)
558 config
->searchPaths
.push_back(unquote(tok
));
562 // This reads an overlay description. Overlays are used to describe output
563 // sections that use the same virtual memory range and normally would trigger
564 // linker's sections sanity check failures.
565 // https://sourceware.org/binutils/docs/ld/Overlay-Description.html#Overlay-Description
566 SmallVector
<SectionCommand
*, 0> ScriptParser::readOverlay() {
569 addrExpr
= [] { return script
->getDot(); };
571 addrExpr
= readExpr();
574 // When AT is omitted, LMA should equal VMA. script->getDot() when evaluating
575 // lmaExpr will ensure this, even if the start address is specified.
577 consume("AT") ? readParenExpr() : [] { return script
->getDot(); };
580 SmallVector
<SectionCommand
*, 0> v
;
581 OutputSection
*prev
= nullptr;
582 while (!errorCount() && !consume("}")) {
583 // VA is the same for all sections. The LMAs are consecutive in memory
584 // starting from the base load address specified.
585 OutputDesc
*osd
= readOverlaySectionDescription();
586 osd
->osec
.addrExpr
= addrExpr
;
588 osd
->osec
.lmaExpr
= [=] { return prev
->getLMA() + prev
->size
; };
590 osd
->osec
.lmaExpr
= lmaExpr
;
591 // Use first section address for subsequent sections as initial addrExpr
592 // can be DOT. Ensure the first section, even if empty, is not discarded.
593 osd
->osec
.usedInExpression
= true;
594 addrExpr
= [=]() -> ExprValue
{ return {&osd
->osec
, false, 0, ""}; };
600 // According to the specification, at the end of the overlay, the location
601 // counter should be equal to the overlay base address plus size of the
602 // largest section seen in the overlay.
603 // Here we want to create the Dot assignment command to achieve that.
606 for (SectionCommand
*cmd
: v
)
607 max
= std::max(max
, cast
<OutputDesc
>(cmd
)->osec
.size
);
608 return addrExpr().getValue() + max
;
610 v
.push_back(make
<SymbolAssignment
>(".", moveDot
, 0, getCurrentLocation()));
614 void ScriptParser::readOverwriteSections() {
616 while (!errorCount() && !consume("}"))
617 script
->overwriteSections
.push_back(readOutputSectionDescription(next()));
620 void ScriptParser::readSections() {
622 SmallVector
<SectionCommand
*, 0> v
;
623 while (!errorCount() && !consume("}")) {
624 StringRef tok
= next();
625 if (tok
== "OVERLAY") {
626 for (SectionCommand
*cmd
: readOverlay())
629 } else if (tok
== "INCLUDE") {
634 if (SectionCommand
*cmd
= readAssignment(tok
))
637 v
.push_back(readOutputSectionDescription(tok
));
640 // If DATA_SEGMENT_RELRO_END is absent, for sections after DATA_SEGMENT_ALIGN,
641 // the relro fields should be cleared.
642 if (!script
->seenRelroEnd
)
643 for (SectionCommand
*cmd
: v
)
644 if (auto *osd
= dyn_cast
<OutputDesc
>(cmd
))
645 osd
->osec
.relro
= false;
647 script
->sectionCommands
.insert(script
->sectionCommands
.end(), v
.begin(),
650 if (atEOF() || !consume("INSERT")) {
651 script
->hasSectionsCommand
= true;
655 bool isAfter
= false;
656 if (consume("AFTER"))
658 else if (!consume("BEFORE"))
659 setError("expected AFTER/BEFORE, but got '" + next() + "'");
660 StringRef where
= next();
661 SmallVector
<StringRef
, 0> names
;
662 for (SectionCommand
*cmd
: v
)
663 if (auto *os
= dyn_cast
<OutputDesc
>(cmd
))
664 names
.push_back(os
->osec
.name
);
666 script
->insertCommands
.push_back({std::move(names
), isAfter
, where
});
669 void ScriptParser::readTarget() {
670 // TARGET(foo) is an alias for "--format foo". Unlike GNU linkers,
671 // we accept only a limited set of BFD names (i.e. "elf" or "binary")
672 // for --format. We recognize only /^elf/ and "binary" in the linker
675 StringRef tok
= unquote(next());
678 if (tok
.starts_with("elf"))
679 config
->formatBinary
= false;
680 else if (tok
== "binary")
681 config
->formatBinary
= true;
683 setError("unknown target: " + tok
);
686 static int precedence(StringRef op
) {
687 return StringSwitch
<int>(op
)
688 .Cases("*", "/", "%", 11)
690 .Cases("<<", ">>", 9)
691 .Cases("<", "<=", ">", ">=", 8)
692 .Cases("==", "!=", 7)
702 StringMatcher
ScriptParser::readFilePatterns() {
703 StringMatcher Matcher
;
705 while (!errorCount() && !consume(")"))
706 Matcher
.addPattern(SingleStringMatcher(next()));
710 SortSectionPolicy
ScriptParser::peekSortKind() {
711 return StringSwitch
<SortSectionPolicy
>(peek())
712 .Case("REVERSE", SortSectionPolicy::Reverse
)
713 .Cases("SORT", "SORT_BY_NAME", SortSectionPolicy::Name
)
714 .Case("SORT_BY_ALIGNMENT", SortSectionPolicy::Alignment
)
715 .Case("SORT_BY_INIT_PRIORITY", SortSectionPolicy::Priority
)
716 .Case("SORT_NONE", SortSectionPolicy::None
)
717 .Default(SortSectionPolicy::Default
);
720 SortSectionPolicy
ScriptParser::readSortKind() {
721 SortSectionPolicy ret
= peekSortKind();
722 if (ret
!= SortSectionPolicy::Default
)
727 // Reads SECTIONS command contents in the following form:
729 // <contents> ::= <elem>*
730 // <elem> ::= <exclude>? <glob-pattern>
731 // <exclude> ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
735 // *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
737 // is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
738 // The semantics of that is section .foo in any file, section .bar in
739 // any file but a.o, and section .baz in any file but b.o.
740 SmallVector
<SectionPattern
, 0> ScriptParser::readInputSectionsList() {
741 SmallVector
<SectionPattern
, 0> ret
;
742 while (!errorCount() && peek() != ")") {
743 StringMatcher excludeFilePat
;
744 if (consume("EXCLUDE_FILE")) {
746 excludeFilePat
= readFilePatterns();
749 StringMatcher SectionMatcher
;
750 // Break if the next token is ), EXCLUDE_FILE, or SORT*.
751 while (!errorCount() && peekSortKind() == SortSectionPolicy::Default
) {
752 StringRef s
= peek();
753 if (s
== ")" || s
== "EXCLUDE_FILE")
755 // Detect common mistakes when certain non-wildcard meta characters are
756 // used without a closing ')'.
757 if (!s
.empty() && strchr("(){}", s
[0])) {
759 setError("section pattern is expected");
762 SectionMatcher
.addPattern(unquote(next()));
765 if (!SectionMatcher
.empty())
766 ret
.push_back({std::move(excludeFilePat
), std::move(SectionMatcher
)});
767 else if (excludeFilePat
.empty())
770 setError("section pattern is expected");
775 // Reads contents of "SECTIONS" directive. That directive contains a
776 // list of glob patterns for input sections. The grammar is as follows.
778 // <patterns> ::= <section-list>
779 // | <sort> "(" <section-list> ")"
780 // | <sort> "(" <sort> "(" <section-list> ")" ")"
782 // <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
783 // | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
785 // <section-list> is parsed by readInputSectionsList().
786 InputSectionDescription
*
787 ScriptParser::readInputSectionRules(StringRef filePattern
, uint64_t withFlags
,
788 uint64_t withoutFlags
) {
790 make
<InputSectionDescription
>(filePattern
, withFlags
, withoutFlags
);
793 while (!errorCount() && !consume(")")) {
794 SortSectionPolicy outer
= readSortKind();
795 SortSectionPolicy inner
= SortSectionPolicy::Default
;
796 SmallVector
<SectionPattern
, 0> v
;
797 if (outer
!= SortSectionPolicy::Default
) {
799 inner
= readSortKind();
800 if (inner
!= SortSectionPolicy::Default
) {
802 v
= readInputSectionsList();
805 v
= readInputSectionsList();
809 v
= readInputSectionsList();
812 for (SectionPattern
&pat
: v
) {
813 pat
.sortInner
= inner
;
814 pat
.sortOuter
= outer
;
817 std::move(v
.begin(), v
.end(), std::back_inserter(cmd
->sectionPatterns
));
822 InputSectionDescription
*
823 ScriptParser::readInputSectionDescription(StringRef tok
) {
824 // Input section wildcard can be surrounded by KEEP.
825 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
826 uint64_t withFlags
= 0;
827 uint64_t withoutFlags
= 0;
830 if (consume("INPUT_SECTION_FLAGS"))
831 std::tie(withFlags
, withoutFlags
) = readInputSectionFlags();
832 InputSectionDescription
*cmd
=
833 readInputSectionRules(next(), withFlags
, withoutFlags
);
835 script
->keptSections
.push_back(cmd
);
838 if (tok
== "INPUT_SECTION_FLAGS") {
839 std::tie(withFlags
, withoutFlags
) = readInputSectionFlags();
842 return readInputSectionRules(tok
, withFlags
, withoutFlags
);
845 void ScriptParser::readSort() {
847 expect("CONSTRUCTORS");
851 Expr
ScriptParser::readAssert() {
855 StringRef msg
= unquote(next());
861 return script
->getDot();
867 constexpr std::pair
<const char *, unsigned> typeMap
[] = {
868 ECase(SHT_PROGBITS
), ECase(SHT_NOTE
), ECase(SHT_NOBITS
),
869 ECase(SHT_INIT_ARRAY
), ECase(SHT_FINI_ARRAY
), ECase(SHT_PREINIT_ARRAY
),
873 // Tries to read the special directive for an output section definition which
874 // can be one of following: "(NOLOAD)", "(COPY)", "(INFO)", "(OVERLAY)", and
876 bool ScriptParser::readSectionDirective(OutputSection
*cmd
, StringRef tok
) {
877 if (tok
!= "NOLOAD" && tok
!= "COPY" && tok
!= "INFO" && tok
!= "OVERLAY" &&
881 if (consume("NOLOAD")) {
882 cmd
->type
= SHT_NOBITS
;
883 cmd
->typeIsSet
= true;
884 } else if (consume("TYPE")) {
886 StringRef value
= peek();
887 auto it
= llvm::find_if(typeMap
, [=](auto e
) { return e
.first
== value
; });
888 if (it
!= std::end(typeMap
)) {
889 // The value is a recognized literal SHT_*.
890 cmd
->type
= it
->second
;
892 } else if (value
.starts_with("SHT_")) {
893 setError("unknown section type " + value
);
895 // Otherwise, read an expression.
896 cmd
->type
= readExpr()().getValue();
898 cmd
->typeIsSet
= true;
900 skip(); // This is "COPY", "INFO" or "OVERLAY".
901 cmd
->nonAlloc
= true;
907 // Reads an expression and/or the special directive for an output
908 // section definition. Directive is one of following: "(NOLOAD)",
909 // "(COPY)", "(INFO)" or "(OVERLAY)".
911 // An output section name can be followed by an address expression
912 // and/or directive. This grammar is not LL(1) because "(" can be
913 // interpreted as either the beginning of some expression or beginning
916 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
917 // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
918 void ScriptParser::readSectionAddressType(OutputSection
*cmd
) {
920 // Temporarily set inExpr to support TYPE=<value> without spaces.
921 SaveAndRestore
saved(inExpr
, true);
922 if (readSectionDirective(cmd
, peek()))
924 cmd
->addrExpr
= readExpr();
927 cmd
->addrExpr
= readExpr();
931 SaveAndRestore
saved(inExpr
, true);
932 StringRef tok
= peek();
933 if (!readSectionDirective(cmd
, tok
))
934 setError("unknown section directive: " + tok
);
938 static Expr
checkAlignment(Expr e
, std::string
&loc
) {
940 uint64_t alignment
= std::max((uint64_t)1, e().getValue());
941 if (!isPowerOf2_64(alignment
)) {
942 error(loc
+ ": alignment must be power of 2");
943 return (uint64_t)1; // Return a dummy value.
949 OutputDesc
*ScriptParser::readOverlaySectionDescription() {
950 OutputDesc
*osd
= script
->createOutputSection(next(), getCurrentLocation());
951 osd
->osec
.inOverlay
= true;
953 while (!errorCount() && !consume("}")) {
954 uint64_t withFlags
= 0;
955 uint64_t withoutFlags
= 0;
956 if (consume("INPUT_SECTION_FLAGS"))
957 std::tie(withFlags
, withoutFlags
) = readInputSectionFlags();
958 osd
->osec
.commands
.push_back(
959 readInputSectionRules(next(), withFlags
, withoutFlags
));
961 osd
->osec
.phdrs
= readOutputSectionPhdrs();
965 OutputDesc
*ScriptParser::readOutputSectionDescription(StringRef outSec
) {
967 script
->createOutputSection(unquote(outSec
), getCurrentLocation());
968 OutputSection
*osec
= &cmd
->osec
;
969 // Maybe relro. Will reset to false if DATA_SEGMENT_RELRO_END is absent.
970 osec
->relro
= script
->seenDataAlign
&& !script
->seenRelroEnd
;
972 size_t symbolsReferenced
= script
->referencedSymbols
.size();
975 readSectionAddressType(osec
);
978 std::string location
= getCurrentLocation();
980 osec
->lmaExpr
= readParenExpr();
981 if (consume("ALIGN"))
982 osec
->alignExpr
= checkAlignment(readParenExpr(), location
);
983 if (consume("SUBALIGN"))
984 osec
->subalignExpr
= checkAlignment(readParenExpr(), location
);
986 // Parse constraints.
987 if (consume("ONLY_IF_RO"))
988 osec
->constraint
= ConstraintKind::ReadOnly
;
989 if (consume("ONLY_IF_RW"))
990 osec
->constraint
= ConstraintKind::ReadWrite
;
993 while (!errorCount() && !consume("}")) {
994 StringRef tok
= next();
996 // Empty commands are allowed. Do nothing here.
997 } else if (SymbolAssignment
*assign
= readAssignment(tok
)) {
998 osec
->commands
.push_back(assign
);
999 } else if (ByteCommand
*data
= readByteCommand(tok
)) {
1000 osec
->commands
.push_back(data
);
1001 } else if (tok
== "CONSTRUCTORS") {
1002 // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
1003 // by name. This is for very old file formats such as ECOFF/XCOFF.
1004 // For ELF, we should ignore.
1005 } else if (tok
== "FILL") {
1006 // We handle the FILL command as an alias for =fillexp section attribute,
1007 // which is different from what GNU linkers do.
1008 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
1010 setError("( expected, but got " + peek());
1011 osec
->filler
= readFill();
1012 } else if (tok
== "SORT") {
1014 } else if (tok
== "INCLUDE") {
1016 } else if (tok
== "(" || tok
== ")") {
1017 setError("expected filename pattern");
1018 } else if (peek() == "(") {
1019 osec
->commands
.push_back(readInputSectionDescription(tok
));
1021 // We have a file name and no input sections description. It is not a
1022 // commonly used syntax, but still acceptable. In that case, all sections
1023 // from the file will be included.
1024 // FIXME: GNU ld permits INPUT_SECTION_FLAGS to be used here. We do not
1025 // handle this case here as it will already have been matched by the
1027 auto *isd
= make
<InputSectionDescription
>(tok
);
1028 isd
->sectionPatterns
.push_back({{}, StringMatcher("*")});
1029 osec
->commands
.push_back(isd
);
1034 osec
->memoryRegionName
= std::string(next());
1036 if (consume("AT")) {
1038 osec
->lmaRegionName
= std::string(next());
1041 if (osec
->lmaExpr
&& !osec
->lmaRegionName
.empty())
1042 error("section can't have both LMA and a load region");
1044 osec
->phdrs
= readOutputSectionPhdrs();
1046 if (peek() == "=" || peek().starts_with("=")) {
1049 osec
->filler
= readFill();
1053 // Consume optional comma following output section command.
1056 if (script
->referencedSymbols
.size() > symbolsReferenced
)
1057 osec
->expressionsUseSymbols
= true;
1061 // Reads a `=<fillexp>` expression and returns its value as a big-endian number.
1062 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1063 // We do not support using symbols in such expressions.
1065 // When reading a hexstring, ld.bfd handles it as a blob of arbitrary
1066 // size, while ld.gold always handles it as a 32-bit big-endian number.
1067 // We are compatible with ld.gold because it's easier to implement.
1068 // Also, we require that expressions with operators must be wrapped into
1069 // round brackets. We did it to resolve the ambiguity when parsing scripts like:
1070 // SECTIONS { .foo : { ... } =120+3 /DISCARD/ : { ... } }
1071 std::array
<uint8_t, 4> ScriptParser::readFill() {
1072 uint64_t value
= readPrimary()().val
;
1073 if (value
> UINT32_MAX
)
1074 setError("filler expression result does not fit 32-bit: 0x" +
1075 Twine::utohexstr(value
));
1077 std::array
<uint8_t, 4> buf
;
1078 write32be(buf
.data(), (uint32_t)value
);
1082 SymbolAssignment
*ScriptParser::readProvideHidden(bool provide
, bool hidden
) {
1084 StringRef name
= next(), eq
= peek();
1086 setError("= expected, but got " + next());
1087 while (!atEOF() && next() != ")")
1091 llvm::SaveAndRestore
saveActiveProvideSym(activeProvideSym
);
1093 activeProvideSym
= name
;
1094 SymbolAssignment
*cmd
= readSymbolAssignment(name
);
1095 cmd
->provide
= provide
;
1096 cmd
->hidden
= hidden
;
1101 SymbolAssignment
*ScriptParser::readAssignment(StringRef tok
) {
1102 // Assert expression returns Dot, so this is equal to ".=."
1103 if (tok
== "ASSERT")
1104 return make
<SymbolAssignment
>(".", readAssert(), 0, getCurrentLocation());
1106 size_t oldPos
= pos
;
1107 SymbolAssignment
*cmd
= nullptr;
1108 bool savedSeenRelroEnd
= script
->seenRelroEnd
;
1109 const StringRef op
= peek();
1110 if (op
.starts_with("=")) {
1111 // Support = followed by an expression without whitespace.
1112 SaveAndRestore
saved(inExpr
, true);
1113 cmd
= readSymbolAssignment(tok
);
1114 } else if ((op
.size() == 2 && op
[1] == '=' && strchr("*/+-&^|", op
[0])) ||
1115 op
== "<<=" || op
== ">>=") {
1116 cmd
= readSymbolAssignment(tok
);
1117 } else if (tok
== "PROVIDE") {
1118 SaveAndRestore
saved(inExpr
, true);
1119 cmd
= readProvideHidden(true, false);
1120 } else if (tok
== "HIDDEN") {
1121 SaveAndRestore
saved(inExpr
, true);
1122 cmd
= readProvideHidden(false, true);
1123 } else if (tok
== "PROVIDE_HIDDEN") {
1124 SaveAndRestore
saved(inExpr
, true);
1125 cmd
= readProvideHidden(true, true);
1129 cmd
->dataSegmentRelroEnd
= !savedSeenRelroEnd
&& script
->seenRelroEnd
;
1130 cmd
->commandString
=
1132 llvm::join(tokens
.begin() + oldPos
, tokens
.begin() + pos
, " ");
1138 SymbolAssignment
*ScriptParser::readSymbolAssignment(StringRef name
) {
1139 name
= unquote(name
);
1140 StringRef op
= next();
1141 assert(op
== "=" || op
== "*=" || op
== "/=" || op
== "+=" || op
== "-=" ||
1142 op
== "&=" || op
== "^=" || op
== "|=" || op
== "<<=" || op
== ">>=");
1143 // Note: GNU ld does not support %=.
1144 Expr e
= readExpr();
1146 std::string loc
= getCurrentLocation();
1147 e
= [=, c
= op
[0]]() -> ExprValue
{
1148 ExprValue lhs
= script
->getSymbolValue(name
, loc
);
1151 return lhs
.getValue() * e().getValue();
1153 if (uint64_t rv
= e().getValue())
1154 return lhs
.getValue() / rv
;
1155 error(loc
+ ": division by zero");
1158 return add(lhs
, e());
1160 return sub(lhs
, e());
1162 return lhs
.getValue() << e().getValue() % 64;
1164 return lhs
.getValue() >> e().getValue() % 64;
1166 return lhs
.getValue() & e().getValue();
1168 return lhs
.getValue() ^ e().getValue();
1170 return lhs
.getValue() | e().getValue();
1172 llvm_unreachable("");
1176 return make
<SymbolAssignment
>(name
, e
, ctx
.scriptSymOrderCounter
++,
1177 getCurrentLocation());
1180 // This is an operator-precedence parser to parse a linker
1181 // script expression.
1182 Expr
ScriptParser::readExpr() {
1183 // Our lexer is context-aware. Set the in-expression bit so that
1184 // they apply different tokenization rules.
1185 SaveAndRestore
saved(inExpr
, true);
1186 Expr e
= readExpr1(readPrimary(), 0);
1190 Expr
ScriptParser::combine(StringRef op
, Expr l
, Expr r
) {
1192 return [=] { return add(l(), r()); };
1194 return [=] { return sub(l(), r()); };
1196 return [=] { return l().getValue() * r().getValue(); };
1198 std::string loc
= getCurrentLocation();
1199 return [=]() -> uint64_t {
1200 if (uint64_t rv
= r().getValue())
1201 return l().getValue() / rv
;
1202 error(loc
+ ": division by zero");
1207 std::string loc
= getCurrentLocation();
1208 return [=]() -> uint64_t {
1209 if (uint64_t rv
= r().getValue())
1210 return l().getValue() % rv
;
1211 error(loc
+ ": modulo by zero");
1216 return [=] { return l().getValue() << r().getValue() % 64; };
1218 return [=] { return l().getValue() >> r().getValue() % 64; };
1220 return [=] { return l().getValue() < r().getValue(); };
1222 return [=] { return l().getValue() > r().getValue(); };
1224 return [=] { return l().getValue() >= r().getValue(); };
1226 return [=] { return l().getValue() <= r().getValue(); };
1228 return [=] { return l().getValue() == r().getValue(); };
1230 return [=] { return l().getValue() != r().getValue(); };
1232 return [=] { return l().getValue() || r().getValue(); };
1234 return [=] { return l().getValue() && r().getValue(); };
1236 return [=] { return bitAnd(l(), r()); };
1238 return [=] { return bitXor(l(), r()); };
1240 return [=] { return bitOr(l(), r()); };
1241 llvm_unreachable("invalid operator");
1244 // This is a part of the operator-precedence parser. This function
1245 // assumes that the remaining token stream starts with an operator.
1246 Expr
ScriptParser::readExpr1(Expr lhs
, int minPrec
) {
1247 while (!atEOF() && !errorCount()) {
1248 // Read an operator and an expression.
1249 StringRef op1
= peek();
1250 if (precedence(op1
) < minPrec
)
1254 return readTernary(lhs
);
1255 Expr rhs
= readPrimary();
1257 // Evaluate the remaining part of the expression first if the
1258 // next operator has greater precedence than the previous one.
1259 // For example, if we have read "+" and "3", and if the next
1260 // operator is "*", then we'll evaluate 3 * ... part first.
1262 StringRef op2
= peek();
1263 if (precedence(op2
) <= precedence(op1
))
1265 rhs
= readExpr1(rhs
, precedence(op2
));
1268 lhs
= combine(op1
, lhs
, rhs
);
1273 Expr
ScriptParser::getPageSize() {
1274 std::string location
= getCurrentLocation();
1275 return [=]() -> uint64_t {
1277 return config
->commonPageSize
;
1278 error(location
+ ": unable to calculate page size");
1279 return 4096; // Return a dummy value.
1283 Expr
ScriptParser::readConstant() {
1284 StringRef s
= readParenLiteral();
1285 if (s
== "COMMONPAGESIZE")
1286 return getPageSize();
1287 if (s
== "MAXPAGESIZE")
1288 return [] { return config
->maxPageSize
; };
1289 setError("unknown constant: " + s
);
1290 return [] { return 0; };
1293 // Parses Tok as an integer. It recognizes hexadecimal (prefixed with
1294 // "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
1295 // have "K" (Ki) or "M" (Mi) suffixes.
1296 static std::optional
<uint64_t> parseInt(StringRef tok
) {
1299 if (tok
.starts_with_insensitive("0x")) {
1300 if (!to_integer(tok
.substr(2), val
, 16))
1301 return std::nullopt
;
1304 if (tok
.ends_with_insensitive("H")) {
1305 if (!to_integer(tok
.drop_back(), val
, 16))
1306 return std::nullopt
;
1311 if (tok
.ends_with_insensitive("K")) {
1312 if (!to_integer(tok
.drop_back(), val
, 10))
1313 return std::nullopt
;
1316 if (tok
.ends_with_insensitive("M")) {
1317 if (!to_integer(tok
.drop_back(), val
, 10))
1318 return std::nullopt
;
1319 return val
* 1024 * 1024;
1321 if (!to_integer(tok
, val
, 10))
1322 return std::nullopt
;
1326 ByteCommand
*ScriptParser::readByteCommand(StringRef tok
) {
1327 int size
= StringSwitch
<int>(tok
)
1336 size_t oldPos
= pos
;
1337 Expr e
= readParenExpr();
1338 std::string commandString
=
1340 llvm::join(tokens
.begin() + oldPos
, tokens
.begin() + pos
, " ");
1341 return make
<ByteCommand
>(e
, size
, commandString
);
1344 static std::optional
<uint64_t> parseFlag(StringRef tok
) {
1345 if (std::optional
<uint64_t> asInt
= parseInt(tok
))
1347 #define CASE_ENT(enum) #enum, ELF::enum
1348 return StringSwitch
<std::optional
<uint64_t>>(tok
)
1349 .Case(CASE_ENT(SHF_WRITE
))
1350 .Case(CASE_ENT(SHF_ALLOC
))
1351 .Case(CASE_ENT(SHF_EXECINSTR
))
1352 .Case(CASE_ENT(SHF_MERGE
))
1353 .Case(CASE_ENT(SHF_STRINGS
))
1354 .Case(CASE_ENT(SHF_INFO_LINK
))
1355 .Case(CASE_ENT(SHF_LINK_ORDER
))
1356 .Case(CASE_ENT(SHF_OS_NONCONFORMING
))
1357 .Case(CASE_ENT(SHF_GROUP
))
1358 .Case(CASE_ENT(SHF_TLS
))
1359 .Case(CASE_ENT(SHF_COMPRESSED
))
1360 .Case(CASE_ENT(SHF_EXCLUDE
))
1361 .Case(CASE_ENT(SHF_ARM_PURECODE
))
1362 .Default(std::nullopt
);
1366 // Reads the '(' <flags> ')' list of section flags in
1367 // INPUT_SECTION_FLAGS '(' <flags> ')' in the
1369 // <flags> ::= <flag>
1371 // <flag> ::= Recognized Flag Name, or Integer value of flag.
1372 // If the first character of <flag> is a ! then this means without flag,
1373 // otherwise with flag.
1374 // Example: SHF_EXECINSTR & !SHF_WRITE means with flag SHF_EXECINSTR and
1375 // without flag SHF_WRITE.
1376 std::pair
<uint64_t, uint64_t> ScriptParser::readInputSectionFlags() {
1377 uint64_t withFlags
= 0;
1378 uint64_t withoutFlags
= 0;
1380 while (!errorCount()) {
1381 StringRef tok
= unquote(next());
1382 bool without
= tok
.consume_front("!");
1383 if (std::optional
<uint64_t> flag
= parseFlag(tok
)) {
1385 withoutFlags
|= *flag
;
1389 setError("unrecognised flag: " + tok
);
1393 if (!consume("&")) {
1395 setError("expected & or )");
1398 return std::make_pair(withFlags
, withoutFlags
);
1401 StringRef
ScriptParser::readParenLiteral() {
1405 StringRef tok
= next();
1411 static void checkIfExists(const OutputSection
&osec
, StringRef location
) {
1412 if (osec
.location
.empty() && script
->errorOnMissingSection
)
1413 script
->recordError(location
+ ": undefined section " + osec
.name
);
1416 static bool isValidSymbolName(StringRef s
) {
1417 auto valid
= [](char c
) {
1418 return isAlnum(c
) || c
== '$' || c
== '.' || c
== '_';
1420 return !s
.empty() && !isDigit(s
[0]) && llvm::all_of(s
, valid
);
1423 Expr
ScriptParser::readPrimary() {
1425 return readParenExpr();
1428 Expr e
= readPrimary();
1429 return [=] { return ~e().getValue(); };
1432 Expr e
= readPrimary();
1433 return [=] { return !e().getValue(); };
1436 Expr e
= readPrimary();
1437 return [=] { return -e().getValue(); };
1440 StringRef tok
= next();
1441 std::string location
= getCurrentLocation();
1443 // Built-in functions are parsed here.
1444 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1445 if (tok
== "ABSOLUTE") {
1446 Expr inner
= readParenExpr();
1448 ExprValue i
= inner();
1449 i
.forceAbsolute
= true;
1453 if (tok
== "ADDR") {
1454 StringRef name
= unquote(readParenLiteral());
1455 OutputSection
*osec
= &script
->getOrCreateOutputSection(name
)->osec
;
1456 osec
->usedInExpression
= true;
1457 return [=]() -> ExprValue
{
1458 checkIfExists(*osec
, location
);
1459 return {osec
, false, 0, location
};
1462 if (tok
== "ALIGN") {
1464 Expr e
= readExpr();
1466 e
= checkAlignment(e
, location
);
1467 return [=] { return alignToPowerOf2(script
->getDot(), e().getValue()); };
1470 Expr e2
= checkAlignment(readExpr(), location
);
1474 v
.alignment
= e2().getValue();
1478 if (tok
== "ALIGNOF") {
1479 StringRef name
= unquote(readParenLiteral());
1480 OutputSection
*osec
= &script
->getOrCreateOutputSection(name
)->osec
;
1482 checkIfExists(*osec
, location
);
1483 return osec
->addralign
;
1486 if (tok
== "ASSERT")
1487 return readAssert();
1488 if (tok
== "CONSTANT")
1489 return readConstant();
1490 if (tok
== "DATA_SEGMENT_ALIGN") {
1492 Expr e
= readExpr();
1496 script
->seenDataAlign
= true;
1498 uint64_t align
= std::max(uint64_t(1), e().getValue());
1499 return (script
->getDot() + align
- 1) & -align
;
1502 if (tok
== "DATA_SEGMENT_END") {
1506 return [] { return script
->getDot(); };
1508 if (tok
== "DATA_SEGMENT_RELRO_END") {
1509 // GNU linkers implements more complicated logic to handle
1510 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and
1511 // just align to the next page boundary for simplicity.
1517 script
->seenRelroEnd
= true;
1518 return [=] { return alignToPowerOf2(script
->getDot(), config
->maxPageSize
); };
1520 if (tok
== "DEFINED") {
1521 StringRef name
= unquote(readParenLiteral());
1522 // Return 1 if s is defined. If the definition is only found in a linker
1523 // script, it must happen before this DEFINED.
1524 auto order
= ctx
.scriptSymOrderCounter
++;
1526 Symbol
*s
= symtab
.find(name
);
1527 return s
&& s
->isDefined() && ctx
.scriptSymOrder
.lookup(s
) < order
? 1
1531 if (tok
== "LENGTH") {
1532 StringRef name
= readParenLiteral();
1533 if (script
->memoryRegions
.count(name
) == 0) {
1534 setError("memory region not defined: " + name
);
1535 return [] { return 0; };
1537 return script
->memoryRegions
[name
]->length
;
1539 if (tok
== "LOADADDR") {
1540 StringRef name
= unquote(readParenLiteral());
1541 OutputSection
*osec
= &script
->getOrCreateOutputSection(name
)->osec
;
1542 osec
->usedInExpression
= true;
1544 checkIfExists(*osec
, location
);
1545 return osec
->getLMA();
1548 if (tok
== "LOG2CEIL") {
1550 Expr a
= readExpr();
1553 // LOG2CEIL(0) is defined to be 0.
1554 return llvm::Log2_64_Ceil(std::max(a().getValue(), UINT64_C(1)));
1557 if (tok
== "MAX" || tok
== "MIN") {
1559 Expr a
= readExpr();
1561 Expr b
= readExpr();
1564 return [=] { return std::min(a().getValue(), b().getValue()); };
1565 return [=] { return std::max(a().getValue(), b().getValue()); };
1567 if (tok
== "ORIGIN") {
1568 StringRef name
= readParenLiteral();
1569 if (script
->memoryRegions
.count(name
) == 0) {
1570 setError("memory region not defined: " + name
);
1571 return [] { return 0; };
1573 return script
->memoryRegions
[name
]->origin
;
1575 if (tok
== "SEGMENT_START") {
1579 Expr e
= readExpr();
1581 return [=] { return e(); };
1583 if (tok
== "SIZEOF") {
1584 StringRef name
= unquote(readParenLiteral());
1585 OutputSection
*cmd
= &script
->getOrCreateOutputSection(name
)->osec
;
1586 // Linker script does not create an output section if its content is empty.
1587 // We want to allow SIZEOF(.foo) where .foo is a section which happened to
1589 return [=] { return cmd
->size
; };
1591 if (tok
== "SIZEOF_HEADERS")
1592 return [=] { return elf::getHeaderSize(); };
1596 return [=] { return script
->getSymbolValue(tok
, location
); };
1598 // Tok is a literal number.
1599 if (std::optional
<uint64_t> val
= parseInt(tok
))
1600 return [=] { return *val
; };
1602 // Tok is a symbol name.
1603 if (tok
.starts_with("\""))
1605 else if (!isValidSymbolName(tok
))
1606 setError("malformed number: " + tok
);
1607 if (activeProvideSym
)
1608 script
->provideMap
[*activeProvideSym
].push_back(tok
);
1610 script
->referencedSymbols
.push_back(tok
);
1611 return [=] { return script
->getSymbolValue(tok
, location
); };
1614 Expr
ScriptParser::readTernary(Expr cond
) {
1615 Expr l
= readExpr();
1617 Expr r
= readExpr();
1618 return [=] { return cond().getValue() ? l() : r(); };
1621 Expr
ScriptParser::readParenExpr() {
1623 Expr e
= readExpr();
1628 SmallVector
<StringRef
, 0> ScriptParser::readOutputSectionPhdrs() {
1629 SmallVector
<StringRef
, 0> phdrs
;
1630 while (!errorCount() && peek().starts_with(":")) {
1631 StringRef tok
= next();
1632 phdrs
.push_back((tok
.size() == 1) ? next() : tok
.substr(1));
1637 // Read a program header type name. The next token must be a
1638 // name of a program header type or a constant (e.g. "0x3").
1639 unsigned ScriptParser::readPhdrType() {
1640 StringRef tok
= next();
1641 if (std::optional
<uint64_t> val
= parseInt(tok
))
1644 unsigned ret
= StringSwitch
<unsigned>(tok
)
1645 .Case("PT_NULL", PT_NULL
)
1646 .Case("PT_LOAD", PT_LOAD
)
1647 .Case("PT_DYNAMIC", PT_DYNAMIC
)
1648 .Case("PT_INTERP", PT_INTERP
)
1649 .Case("PT_NOTE", PT_NOTE
)
1650 .Case("PT_SHLIB", PT_SHLIB
)
1651 .Case("PT_PHDR", PT_PHDR
)
1652 .Case("PT_TLS", PT_TLS
)
1653 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME
)
1654 .Case("PT_GNU_STACK", PT_GNU_STACK
)
1655 .Case("PT_GNU_RELRO", PT_GNU_RELRO
)
1656 .Case("PT_OPENBSD_MUTABLE", PT_OPENBSD_MUTABLE
)
1657 .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE
)
1658 .Case("PT_OPENBSD_SYSCALLS", PT_OPENBSD_SYSCALLS
)
1659 .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED
)
1660 .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA
)
1663 if (ret
== (unsigned)-1) {
1664 setError("invalid program header type: " + tok
);
1670 // Reads an anonymous version declaration.
1671 void ScriptParser::readAnonymousDeclaration() {
1672 SmallVector
<SymbolVersion
, 0> locals
;
1673 SmallVector
<SymbolVersion
, 0> globals
;
1674 std::tie(locals
, globals
) = readSymbols();
1675 for (const SymbolVersion
&pat
: locals
)
1676 config
->versionDefinitions
[VER_NDX_LOCAL
].localPatterns
.push_back(pat
);
1677 for (const SymbolVersion
&pat
: globals
)
1678 config
->versionDefinitions
[VER_NDX_GLOBAL
].nonLocalPatterns
.push_back(pat
);
1683 // Reads a non-anonymous version definition,
1684 // e.g. "VerStr { global: foo; bar; local: *; };".
1685 void ScriptParser::readVersionDeclaration(StringRef verStr
) {
1686 // Read a symbol list.
1687 SmallVector
<SymbolVersion
, 0> locals
;
1688 SmallVector
<SymbolVersion
, 0> globals
;
1689 std::tie(locals
, globals
) = readSymbols();
1691 // Create a new version definition and add that to the global symbols.
1692 VersionDefinition ver
;
1694 ver
.nonLocalPatterns
= std::move(globals
);
1695 ver
.localPatterns
= std::move(locals
);
1696 ver
.id
= config
->versionDefinitions
.size();
1697 config
->versionDefinitions
.push_back(ver
);
1699 // Each version may have a parent version. For example, "Ver2"
1700 // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1701 // as a parent. This version hierarchy is, probably against your
1702 // instinct, purely for hint; the runtime doesn't care about it
1703 // at all. In LLD, we simply ignore it.
1708 bool elf::hasWildcard(StringRef s
) {
1709 return s
.find_first_of("?*[") != StringRef::npos
;
1712 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1713 std::pair
<SmallVector
<SymbolVersion
, 0>, SmallVector
<SymbolVersion
, 0>>
1714 ScriptParser::readSymbols() {
1715 SmallVector
<SymbolVersion
, 0> locals
;
1716 SmallVector
<SymbolVersion
, 0> globals
;
1717 SmallVector
<SymbolVersion
, 0> *v
= &globals
;
1719 while (!errorCount()) {
1722 if (consumeLabel("local")) {
1726 if (consumeLabel("global")) {
1731 if (consume("extern")) {
1732 SmallVector
<SymbolVersion
, 0> ext
= readVersionExtern();
1733 v
->insert(v
->end(), ext
.begin(), ext
.end());
1735 StringRef tok
= next();
1736 v
->push_back({unquote(tok
), false, hasWildcard(tok
)});
1740 return {locals
, globals
};
1743 // Reads an "extern C++" directive, e.g.,
1744 // "extern "C++" { ns::*; "f(int, double)"; };"
1746 // The last semicolon is optional. E.g. this is OK:
1747 // "extern "C++" { ns::*; "f(int, double)" };"
1748 SmallVector
<SymbolVersion
, 0> ScriptParser::readVersionExtern() {
1749 StringRef tok
= next();
1750 bool isCXX
= tok
== "\"C++\"";
1751 if (!isCXX
&& tok
!= "\"C\"")
1752 setError("Unknown language");
1755 SmallVector
<SymbolVersion
, 0> ret
;
1756 while (!errorCount() && peek() != "}") {
1757 StringRef tok
= next();
1759 {unquote(tok
), isCXX
, !tok
.starts_with("\"") && hasWildcard(tok
)});
1769 Expr
ScriptParser::readMemoryAssignment(StringRef s1
, StringRef s2
,
1771 if (!consume(s1
) && !consume(s2
) && !consume(s3
)) {
1772 setError("expected one of: " + s1
+ ", " + s2
+ ", or " + s3
);
1773 return [] { return 0; };
1779 // Parse the MEMORY command as specified in:
1780 // https://sourceware.org/binutils/docs/ld/MEMORY.html
1782 // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
1783 void ScriptParser::readMemory() {
1785 while (!errorCount() && !consume("}")) {
1786 StringRef tok
= next();
1787 if (tok
== "INCLUDE") {
1793 uint32_t invFlags
= 0;
1794 uint32_t negFlags
= 0;
1795 uint32_t negInvFlags
= 0;
1797 readMemoryAttributes(flags
, invFlags
, negFlags
, negInvFlags
);
1802 Expr origin
= readMemoryAssignment("ORIGIN", "org", "o");
1804 Expr length
= readMemoryAssignment("LENGTH", "len", "l");
1806 // Add the memory region to the region map.
1807 MemoryRegion
*mr
= make
<MemoryRegion
>(tok
, origin
, length
, flags
, invFlags
,
1808 negFlags
, negInvFlags
);
1809 if (!script
->memoryRegions
.insert({tok
, mr
}).second
)
1810 setError("region '" + tok
+ "' already defined");
1814 // This function parses the attributes used to match against section
1815 // flags when placing output sections in a memory region. These flags
1816 // are only used when an explicit memory region name is not used.
1817 void ScriptParser::readMemoryAttributes(uint32_t &flags
, uint32_t &invFlags
,
1819 uint32_t &negInvFlags
) {
1820 bool invert
= false;
1822 for (char c
: next().lower()) {
1825 std::swap(flags
, negFlags
);
1826 std::swap(invFlags
, negInvFlags
);
1832 flags
|= SHF_EXECINSTR
;
1836 invFlags
|= SHF_WRITE
;
1838 setError("invalid memory region attribute");
1842 std::swap(flags
, negFlags
);
1843 std::swap(invFlags
, negInvFlags
);
1847 void elf::readLinkerScript(MemoryBufferRef mb
) {
1848 llvm::TimeTraceScope
timeScope("Read linker script",
1849 mb
.getBufferIdentifier());
1850 ScriptParser(mb
).readLinkerScript();
1853 void elf::readVersionScript(MemoryBufferRef mb
) {
1854 llvm::TimeTraceScope
timeScope("Read version script",
1855 mb
.getBufferIdentifier());
1856 ScriptParser(mb
).readVersionScript();
1859 void elf::readDynamicList(MemoryBufferRef mb
) {
1860 llvm::TimeTraceScope
timeScope("Read dynamic list", mb
.getBufferIdentifier());
1861 ScriptParser(mb
).readDynamicList();
1864 void elf::readDefsym(StringRef name
, MemoryBufferRef mb
) {
1865 llvm::TimeTraceScope
timeScope("Read defsym input", name
);
1866 ScriptParser(mb
).readDefsym(name
);