1 //===- LinkerScript.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 the parser/evaluator of the linker script.
12 //===----------------------------------------------------------------------===//
14 #include "LinkerScript.h"
17 #include "InputSection.h"
19 #include "OutputSections.h"
20 #include "ScriptParser.h"
22 #include "SymbolTable.h"
24 #include "SyntheticSections.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/ADT/StringSwitch.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/ELF.h"
33 #include "llvm/Support/Endian.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/Path.h"
50 using namespace llvm::ELF;
51 using namespace llvm::object;
52 using namespace llvm::support::endian;
54 using namespace lld::elf;
56 LinkerScriptBase *elf::ScriptBase;
57 ScriptConfiguration *elf::ScriptConfig;
59 template <class ELFT> static void addRegular(SymbolAssignment *Cmd) {
60 uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
61 Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, Visibility, STT_NOTYPE,
62 0, 0, STB_GLOBAL, nullptr, nullptr);
63 Cmd->Sym = Sym->body();
65 // If we have no SECTIONS then we don't have '.' and don't call
66 // assignAddresses(). We calculate symbol value immediately in this case.
67 if (!ScriptConfig->HasSections)
68 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(0);
71 template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) {
72 // If we have SECTIONS block then output sections haven't been created yet.
73 const OutputSectionBase *Sec =
74 ScriptConfig->HasSections ? nullptr : Cmd->Expression.Section();
75 Symbol *Sym = Symtab<ELFT>::X->addSynthetic(
76 Cmd->Name, Sec, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
77 Cmd->Sym = Sym->body();
79 // If we already know section then we can calculate symbol value immediately.
81 cast<DefinedSynthetic>(Cmd->Sym)->Value = Cmd->Expression(0) - Sec->Addr;
84 static bool isUnderSysroot(StringRef Path) {
85 if (Config->Sysroot == "")
87 for (; !Path.empty(); Path = sys::path::parent_path(Path))
88 if (sys::fs::equivalent(Config->Sysroot, Path))
93 template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) {
94 if (Cmd->Expression.IsAbsolute())
95 addRegular<ELFT>(Cmd);
97 addSynthetic<ELFT>(Cmd);
99 // If a symbol was in PROVIDE(), we need to define it only when
100 // it is an undefined symbol.
101 template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) {
102 if (Cmd->Name == ".")
106 SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
107 return B && B->isUndefined();
110 bool SymbolAssignment::classof(const BaseCommand *C) {
111 return C->Kind == AssignmentKind;
114 bool OutputSectionCommand::classof(const BaseCommand *C) {
115 return C->Kind == OutputSectionKind;
118 bool InputSectionDescription::classof(const BaseCommand *C) {
119 return C->Kind == InputSectionKind;
122 bool AssertCommand::classof(const BaseCommand *C) {
123 return C->Kind == AssertKind;
126 bool BytesDataCommand::classof(const BaseCommand *C) {
127 return C->Kind == BytesDataKind;
130 template <class ELFT> LinkerScript<ELFT>::LinkerScript() = default;
131 template <class ELFT> LinkerScript<ELFT>::~LinkerScript() = default;
133 template <class ELFT> static StringRef basename(InputSectionBase<ELFT> *S) {
135 return sys::path::filename(S->getFile()->getName());
139 template <class ELFT>
140 bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
141 for (InputSectionDescription *ID : Opt.KeptSections)
142 if (ID->FilePat.match(basename(S)))
143 for (SectionPattern &P : ID->SectionPatterns)
144 if (P.SectionPat.match(S->Name))
149 static bool comparePriority(InputSectionData *A, InputSectionData *B) {
150 return getPriority(A->Name) < getPriority(B->Name);
153 static bool compareName(InputSectionData *A, InputSectionData *B) {
154 return A->Name < B->Name;
157 static bool compareAlignment(InputSectionData *A, InputSectionData *B) {
158 // ">" is not a mistake. Larger alignments are placed before smaller
159 // alignments in order to reduce the amount of padding necessary.
160 // This is compatible with GNU.
161 return A->Alignment > B->Alignment;
164 static std::function<bool(InputSectionData *, InputSectionData *)>
165 getComparator(SortSectionPolicy K) {
167 case SortSectionPolicy::Alignment:
168 return compareAlignment;
169 case SortSectionPolicy::Name:
171 case SortSectionPolicy::Priority:
172 return comparePriority;
174 llvm_unreachable("unknown sort policy");
178 template <class ELFT>
179 static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections,
180 ConstraintKind Kind) {
181 if (Kind == ConstraintKind::NoConstraint)
183 bool IsRW = llvm::any_of(Sections, [=](InputSectionData *Sec2) {
184 auto *Sec = static_cast<InputSectionBase<ELFT> *>(Sec2);
185 return Sec->Flags & SHF_WRITE;
187 return (IsRW && Kind == ConstraintKind::ReadWrite) ||
188 (!IsRW && Kind == ConstraintKind::ReadOnly);
191 static void sortSections(InputSectionData **Begin, InputSectionData **End,
192 SortSectionPolicy K) {
193 if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
194 std::stable_sort(Begin, End, getComparator(K));
197 // Compute and remember which sections the InputSectionDescription matches.
198 template <class ELFT>
199 void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) {
200 // Collects all sections that satisfy constraints of I
201 // and attach them to I.
202 for (SectionPattern &Pat : I->SectionPatterns) {
203 size_t SizeBefore = I->Sections.size();
205 for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) {
206 if (!S->Live || S->Assigned)
209 StringRef Filename = basename(S);
210 if (!I->FilePat.match(Filename) || Pat.ExcludedFilePat.match(Filename))
212 if (!Pat.SectionPat.match(S->Name))
214 I->Sections.push_back(S);
218 // Sort sections as instructed by SORT-family commands and --sort-section
219 // option. Because SORT-family commands can be nested at most two depth
220 // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
221 // line option is respected even if a SORT command is given, the exact
222 // behavior we have here is a bit complicated. Here are the rules.
224 // 1. If two SORT commands are given, --sort-section is ignored.
225 // 2. If one SORT command is given, and if it is not SORT_NONE,
226 // --sort-section is handled as an inner SORT command.
227 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
228 // 4. If no SORT command is given, sort according to --sort-section.
229 InputSectionData **Begin = I->Sections.data() + SizeBefore;
230 InputSectionData **End = I->Sections.data() + I->Sections.size();
231 if (Pat.SortOuter != SortSectionPolicy::None) {
232 if (Pat.SortInner == SortSectionPolicy::Default)
233 sortSections(Begin, End, Config->SortSection);
235 sortSections(Begin, End, Pat.SortInner);
236 sortSections(Begin, End, Pat.SortOuter);
241 template <class ELFT>
242 void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
243 for (InputSectionBase<ELFT> *S : V) {
249 template <class ELFT>
250 std::vector<InputSectionBase<ELFT> *>
251 LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
252 std::vector<InputSectionBase<ELFT> *> Ret;
254 for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
255 auto *Cmd = dyn_cast<InputSectionDescription>(Base.get());
258 computeInputSections(Cmd);
259 for (InputSectionData *S : Cmd->Sections)
260 Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
266 template <class ELFT>
267 static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
268 StringRef OutsecName) {
269 // When using linker script the merge rules are different.
270 // Unfortunately, linker scripts are name based. This means that expressions
271 // like *(.foo*) can refer to multiple input sections that would normally be
272 // placed in different output sections. We cannot put them in different
273 // output sections or we would produce wrong results for
274 // start = .; *(.foo.*) end = .; *(.bar)
275 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
276 // another. The problem is that there is no way to layout those output
277 // sections such that the .foo sections are the only thing between the
278 // start and end symbols.
280 // An extra annoyance is that we cannot simply disable merging of the contents
281 // of SHF_MERGE sections, but our implementation requires one output section
282 // per "kind" (string or not, which size/aligment).
283 // Fortunately, creating symbols in the middle of a merge section is not
284 // supported by bfd or gold, so we can just create multiple section in that
286 typedef typename ELFT::uint uintX_t;
287 uintX_t Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
289 uintX_t Alignment = 0;
290 if (isa<MergeInputSection<ELFT>>(C))
291 Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
293 return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment};
296 template <class ELFT>
297 void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory,
298 InputSectionBase<ELFT> *Sec,
300 OutputSectionBase *OutSec;
302 std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec);
304 OutputSections->push_back(OutSec);
305 OutSec->addSection(Sec);
308 template <class ELFT>
309 void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
310 for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
311 auto Iter = Opt.Commands.begin() + I;
312 const std::unique_ptr<BaseCommand> &Base1 = *Iter;
314 // Handle symbol assignments outside of any output section.
315 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
316 if (shouldDefine<ELFT>(Cmd))
317 addSymbol<ELFT>(Cmd);
321 if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) {
322 // If we don't have SECTIONS then output sections have already been
323 // created by Writer<ELFT>. The LinkerScript<ELFT>::assignAddresses
324 // will not be called, so ASSERT should be evaluated now.
325 if (!Opt.HasSections)
330 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
331 std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
333 // The output section name `/DISCARD/' is special.
334 // Any input section assigned to it is discarded.
335 if (Cmd->Name == "/DISCARD/") {
340 // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
341 // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
342 // sections satisfy a given constraint. If not, a directive is handled
343 // as if it wasn't present from the beginning.
345 // Because we'll iterate over Commands many more times, the easiest
346 // way to "make it as if it wasn't present" is to just remove it.
347 if (!matchConstraints<ELFT>(V, Cmd->Constraint)) {
348 for (InputSectionBase<ELFT> *S : V)
350 Opt.Commands.erase(Iter);
355 // A directive may contain symbol definitions like this:
356 // ".foo : { ...; bar = .; }". Handle them.
357 for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
358 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get()))
359 if (shouldDefine<ELFT>(OutCmd))
360 addSymbol<ELFT>(OutCmd);
362 // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
363 // is given, input sections are aligned to that value, whether the
364 // given value is larger or smaller than the original section alignment.
365 if (Cmd->SubalignExpr) {
366 uint32_t Subalign = Cmd->SubalignExpr(0);
367 for (InputSectionBase<ELFT> *S : V)
368 S->Alignment = Subalign;
371 // Add input sections to an output section.
372 for (InputSectionBase<ELFT> *S : V)
373 addSection(Factory, S, Cmd->Name);
378 // Add sections that didn't match any sections command.
379 template <class ELFT>
380 void LinkerScript<ELFT>::addOrphanSections(
381 OutputSectionFactory<ELFT> &Factory) {
382 for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections)
383 if (S->Live && !S->OutSec)
384 addSection(Factory, S, getOutputSectionName(S->Name));
387 // Sets value of a section-defined symbol. Two kinds of
388 // symbols are processed: synthetic symbols, whose value
389 // is an offset from beginning of section and regular
390 // symbols whose value is absolute.
391 template <class ELFT>
392 static void assignSectionSymbol(SymbolAssignment *Cmd,
393 typename ELFT::uint Value) {
397 if (auto *Body = dyn_cast<DefinedSynthetic>(Cmd->Sym)) {
398 Body->Section = Cmd->Expression.Section();
399 Body->Value = Cmd->Expression(Value) - Body->Section->Addr;
402 auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym);
403 Body->Value = Cmd->Expression(Value);
406 template <class ELFT> static bool isTbss(OutputSectionBase *Sec) {
407 return (Sec->Flags & SHF_TLS) && Sec->Type == SHT_NOBITS;
410 template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) {
411 if (!AlreadyOutputIS.insert(S).second)
413 bool IsTbss = isTbss<ELFT>(CurOutSec);
415 uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
416 Pos = alignTo(Pos, S->Alignment);
417 S->OutSecOff = Pos - CurOutSec->Addr;
420 // Update output section size after adding each section. This is so that
421 // SIZEOF works correctly in the case below:
422 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
423 CurOutSec->Size = Pos - CurOutSec->Addr;
426 ThreadBssOffset = Pos - Dot;
431 template <class ELFT> void LinkerScript<ELFT>::flush() {
432 if (!CurOutSec || !AlreadyOutputOS.insert(CurOutSec).second)
434 if (auto *OutSec = dyn_cast<OutputSection<ELFT>>(CurOutSec)) {
435 for (InputSection<ELFT> *I : OutSec->Sections)
438 Dot += CurOutSec->Size;
442 template <class ELFT>
443 void LinkerScript<ELFT>::switchTo(OutputSectionBase *Sec) {
444 if (CurOutSec == Sec)
446 if (AlreadyOutputOS.count(Sec))
452 Dot = alignTo(Dot, CurOutSec->Addralign);
453 CurOutSec->Addr = isTbss<ELFT>(CurOutSec) ? Dot + ThreadBssOffset : Dot;
455 // If neither AT nor AT> is specified for an allocatable section, the linker
456 // will set the LMA such that the difference between VMA and LMA for the
457 // section is the same as the preceding output section in the same region
458 // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
459 CurOutSec->setLMAOffset(LMAOffset);
462 template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) {
463 // This handles the assignments to symbol or to a location counter (.)
464 if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
465 if (AssignCmd->Name == ".") {
466 // Update to location counter means update to section size.
467 uintX_t Val = AssignCmd->Expression(Dot);
469 error("unable to move location counter backward for: " +
472 CurOutSec->Size = Dot - CurOutSec->Addr;
475 assignSectionSymbol<ELFT>(AssignCmd, Dot);
479 // Handle BYTE(), SHORT(), LONG(), or QUAD().
480 if (auto *DataCmd = dyn_cast<BytesDataCommand>(&Base)) {
481 DataCmd->Offset = Dot - CurOutSec->Addr;
482 Dot += DataCmd->Size;
483 CurOutSec->Size = Dot - CurOutSec->Addr;
487 if (auto *AssertCmd = dyn_cast<AssertCommand>(&Base)) {
488 AssertCmd->Expression(Dot);
492 // It handles single input section description command,
493 // calculates and assigns the offsets for each section and also
494 // updates the output section size.
495 auto &ICmd = cast<InputSectionDescription>(Base);
496 for (InputSectionData *ID : ICmd.Sections) {
497 // We tentatively added all synthetic sections at the beginning and removed
498 // empty ones afterwards (because there is no way to know whether they were
499 // going be empty or not other than actually running linker scripts.)
500 // We need to ignore remains of empty sections.
501 if (auto *Sec = dyn_cast<SyntheticSection<ELFT>>(ID))
505 auto *IB = static_cast<InputSectionBase<ELFT> *>(ID);
506 switchTo(IB->OutSec);
507 if (auto *I = dyn_cast<InputSection<ELFT>>(IB))
514 template <class ELFT>
515 static std::vector<OutputSectionBase *>
516 findSections(StringRef Name, const std::vector<OutputSectionBase *> &Sections) {
517 std::vector<OutputSectionBase *> Ret;
518 for (OutputSectionBase *Sec : Sections)
519 if (Sec->getName() == Name)
524 // This function assigns offsets to input sections and an output section
525 // for a single sections command (e.g. ".text { *(.text); }").
526 template <class ELFT>
527 void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
529 LMAOffset = Cmd->LMAExpr(Dot) - Dot;
530 std::vector<OutputSectionBase *> Sections =
531 findSections<ELFT>(Cmd->Name, *OutputSections);
532 if (Sections.empty())
534 switchTo(Sections[0]);
536 // Find the last section output location. We will output orphan sections
537 // there so that end symbols point to the correct location.
538 auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
539 [](const std::unique_ptr<BaseCommand> &Cmd) {
540 return !isa<SymbolAssignment>(*Cmd);
543 for (auto I = Cmd->Commands.begin(); I != E; ++I)
545 for (OutputSectionBase *Base : Sections)
548 std::for_each(E, Cmd->Commands.end(),
549 [this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); });
552 template <class ELFT> void LinkerScript<ELFT>::removeEmptyCommands() {
553 // It is common practice to use very generic linker scripts. So for any
554 // given run some of the output sections in the script will be empty.
555 // We could create corresponding empty output sections, but that would
556 // clutter the output.
557 // We instead remove trivially empty sections. The bfd linker seems even
558 // more aggressive at removing them.
559 auto Pos = std::remove_if(
560 Opt.Commands.begin(), Opt.Commands.end(),
561 [&](const std::unique_ptr<BaseCommand> &Base) {
562 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
563 return findSections<ELFT>(Cmd->Name, *OutputSections).empty();
566 Opt.Commands.erase(Pos, Opt.Commands.end());
569 static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
570 for (const std::unique_ptr<BaseCommand> &I : Cmd.Commands)
571 if (!isa<InputSectionDescription>(*I))
576 template <class ELFT> void LinkerScript<ELFT>::adjustSectionsBeforeSorting() {
577 // If the output section contains only symbol assignments, create a
578 // corresponding output section. The bfd linker seems to only create them if
579 // '.' is assigned to, but creating these section should not have any bad
580 // consequeces and gives us a section to put the symbol in.
581 uintX_t Flags = SHF_ALLOC;
582 uint32_t Type = SHT_NOBITS;
583 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
584 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
587 std::vector<OutputSectionBase *> Secs =
588 findSections<ELFT>(Cmd->Name, *OutputSections);
590 Flags = Secs[0]->Flags;
591 Type = Secs[0]->Type;
595 if (isAllSectionDescription(*Cmd))
598 auto *OutSec = make<OutputSection<ELFT>>(Cmd->Name, Type, Flags);
599 OutputSections->push_back(OutSec);
603 template <class ELFT> void LinkerScript<ELFT>::adjustSectionsAfterSorting() {
604 placeOrphanSections();
606 // If output section command doesn't specify any segments,
607 // and we haven't previously assigned any section to segment,
608 // then we simply assign section to the very first load segment.
609 // Below is an example of such linker script:
610 // PHDRS { seg PT_LOAD; }
611 // SECTIONS { .aaa : { *(.aaa) } }
612 std::vector<StringRef> DefPhdrs;
614 std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
615 [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
616 if (FirstPtLoad != Opt.PhdrsCommands.end())
617 DefPhdrs.push_back(FirstPtLoad->Name);
619 // Walk the commands and propagate the program headers to commands that don't
620 // explicitly specify them.
621 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
622 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
625 if (Cmd->Phdrs.empty())
626 Cmd->Phdrs = DefPhdrs;
628 DefPhdrs = Cmd->Phdrs;
631 removeEmptyCommands();
634 // When placing orphan sections, we want to place them after symbol assignments
635 // so that an orphan after
639 // doesn't break the intended meaning of the begin/end symbols.
640 // We don't want to go over sections since Writer<ELFT>::sortSections is the
641 // one in charge of deciding the order of the sections.
642 // We don't want to go over alignments, since doing so in
643 // rx_sec : { *(rx_sec) }
644 // . = ALIGN(0x1000);
645 // /* The RW PT_LOAD starts here*/
646 // rw_sec : { *(rw_sec) }
647 // would mean that the RW PT_LOAD would become unaligned.
648 static bool shouldSkip(const BaseCommand &Cmd) {
649 if (isa<OutputSectionCommand>(Cmd))
651 const auto *Assign = dyn_cast<SymbolAssignment>(&Cmd);
654 return Assign->Name != ".";
657 // Orphan sections are sections present in the input files which are not
658 // explicitly placed into the output file by the linker script. This just
659 // places them in the order already decided in OutputSections.
660 template <class ELFT> void LinkerScript<ELFT>::placeOrphanSections() {
661 // The OutputSections are already in the correct order.
662 // This loops creates or moves commands as needed so that they are in the
666 // As a horrible special case, skip the first . assignment if it is before any
667 // section. We do this because it is common to set a load address by starting
668 // the script with ". = 0xabcd" and the expectation is that every section is
670 auto FirstSectionOrDotAssignment =
671 std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
672 [](const std::unique_ptr<BaseCommand> &Cmd) {
673 if (isa<OutputSectionCommand>(*Cmd))
675 const auto *Assign = dyn_cast<SymbolAssignment>(Cmd.get());
678 return Assign->Name == ".";
680 if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
681 CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
682 if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
686 for (OutputSectionBase *Sec : *OutputSections) {
687 StringRef Name = Sec->getName();
689 // Find the last spot where we can insert a command and still get the
691 auto CmdIter = Opt.Commands.begin() + CmdIndex;
692 auto E = Opt.Commands.end();
693 while (CmdIter != E && shouldSkip(**CmdIter)) {
699 std::find_if(CmdIter, E, [&](const std::unique_ptr<BaseCommand> &Base) {
700 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
701 return Cmd && Cmd->Name == Name;
704 Opt.Commands.insert(CmdIter,
705 llvm::make_unique<OutputSectionCommand>(Name));
710 // Continue from where we found it.
711 CmdIndex = (Pos - Opt.Commands.begin()) + 1;
715 template <class ELFT>
716 void LinkerScript<ELFT>::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
717 // Assign addresses as instructed by linker script SECTIONS sub-commands.
720 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
721 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
722 if (Cmd->Name == ".") {
723 Dot = Cmd->Expression(Dot);
724 } else if (Cmd->Sym) {
725 assignSectionSymbol<ELFT>(Cmd, Dot);
730 if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
731 Cmd->Expression(Dot);
735 auto *Cmd = cast<OutputSectionCommand>(Base.get());
737 Dot = Cmd->AddrExpr(Dot);
741 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
742 for (OutputSectionBase *Sec : *OutputSections) {
743 if (Sec->Flags & SHF_ALLOC)
744 MinVA = std::min<uint64_t>(MinVA, Sec->Addr);
749 uintX_t HeaderSize = getHeaderSize();
750 // If the linker script doesn't have PHDRS, add ElfHeader and ProgramHeaders
751 // now that we know we have space.
752 if (HeaderSize <= MinVA && !hasPhdrsCommands())
753 allocateHeaders<ELFT>(Phdrs, *OutputSections);
755 // ELF and Program headers need to be right before the first section in
756 // memory. Set their addresses accordingly.
757 MinVA = alignDown(MinVA - HeaderSize, Config->MaxPageSize);
758 Out<ELFT>::ElfHeader->Addr = MinVA;
759 Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + MinVA;
762 // Creates program headers as instructed by PHDRS linker script command.
763 template <class ELFT> std::vector<PhdrEntry> LinkerScript<ELFT>::createPhdrs() {
764 std::vector<PhdrEntry> Ret;
766 // Process PHDRS and FILEHDR keywords because they are not
767 // real output sections and cannot be added in the following loop.
768 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
769 Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
770 PhdrEntry &Phdr = Ret.back();
773 Phdr.add(Out<ELFT>::ElfHeader);
775 Phdr.add(Out<ELFT>::ProgramHeaders);
778 Phdr.p_paddr = Cmd.LMAExpr(0);
783 // Add output sections to program headers.
784 for (OutputSectionBase *Sec : *OutputSections) {
785 if (!(Sec->Flags & SHF_ALLOC))
788 // Assign headers specified by linker script
789 for (size_t Id : getPhdrIndices(Sec->getName())) {
791 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
792 Ret[Id].p_flags |= Sec->getPhdrFlags();
798 template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
799 // Ignore .interp section in case we have PHDRS specification
800 // and PT_INTERP isn't listed.
801 return !Opt.PhdrsCommands.empty() &&
802 llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
803 return Cmd.Type == PT_INTERP;
804 }) == Opt.PhdrsCommands.end();
807 template <class ELFT> uint32_t LinkerScript<ELFT>::getFiller(StringRef Name) {
808 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
809 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
810 if (Cmd->Name == Name)
815 template <class ELFT>
816 static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
817 const endianness E = ELFT::TargetEndianness;
821 *Buf = (uint8_t)Data;
824 write16<E>(Buf, Data);
827 write32<E>(Buf, Data);
830 write64<E>(Buf, Data);
833 llvm_unreachable("unsupported Size argument");
837 template <class ELFT>
838 void LinkerScript<ELFT>::writeDataBytes(StringRef Name, uint8_t *Buf) {
839 int I = getSectionIndex(Name);
843 auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get());
844 for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
845 if (auto *Data = dyn_cast<BytesDataCommand>(Base.get()))
846 writeInt<ELFT>(Buf + Data->Offset, Data->Expression(0), Data->Size);
849 template <class ELFT> bool LinkerScript<ELFT>::hasLMA(StringRef Name) {
850 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
851 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
852 if (Cmd->LMAExpr && Cmd->Name == Name)
857 // Returns the index of the given section name in linker script
858 // SECTIONS commands. Sections are laid out as the same order as they
859 // were in the script. If a given name did not appear in the script,
860 // it returns INT_MAX, so that it will be laid out at end of file.
861 template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
862 for (int I = 0, E = Opt.Commands.size(); I != E; ++I)
863 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get()))
864 if (Cmd->Name == Name)
869 template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
870 return !Opt.PhdrsCommands.empty();
873 template <class ELFT>
874 const OutputSectionBase *LinkerScript<ELFT>::getOutputSection(const Twine &Loc,
876 static OutputSectionBase FakeSec("", 0, 0);
878 for (OutputSectionBase *Sec : *OutputSections)
879 if (Sec->getName() == Name)
882 error(Loc + ": undefined section " + Name);
886 // This function is essentially the same as getOutputSection(Name)->Size,
887 // but it won't print out an error message if a given section is not found.
889 // Linker script does not create an output section if its content is empty.
890 // We want to allow SIZEOF(.foo) where .foo is a section which happened to
891 // be empty. That is why this function is different from getOutputSection().
892 template <class ELFT>
893 uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
894 for (OutputSectionBase *Sec : *OutputSections)
895 if (Sec->getName() == Name)
900 template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
901 return elf::getHeaderSize<ELFT>();
904 template <class ELFT>
905 uint64_t LinkerScript<ELFT>::getSymbolValue(const Twine &Loc, StringRef S) {
906 if (SymbolBody *B = Symtab<ELFT>::X->find(S))
907 return B->getVA<ELFT>();
908 error(Loc + ": symbol not found: " + S);
912 template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S) {
913 return Symtab<ELFT>::X->find(S) != nullptr;
916 template <class ELFT> bool LinkerScript<ELFT>::isAbsolute(StringRef S) {
917 SymbolBody *Sym = Symtab<ELFT>::X->find(S);
918 auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym);
919 return DR && !DR->Section;
922 // Gets section symbol belongs to. Symbol "." doesn't belong to any
923 // specific section but isn't absolute at the same time, so we try
924 // to find suitable section for it as well.
925 template <class ELFT>
926 const OutputSectionBase *LinkerScript<ELFT>::getSymbolSection(StringRef S) {
927 SymbolBody *Sym = Symtab<ELFT>::X->find(S);
929 if (OutputSections->empty())
931 return CurOutSec ? CurOutSec : (*OutputSections)[0];
934 if (auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym))
935 return DR->Section ? DR->Section->OutSec : nullptr;
936 if (auto *DS = dyn_cast_or_null<DefinedSynthetic>(Sym))
942 // Returns indices of ELF headers containing specific section, identified
943 // by Name. Each index is a zero based number of ELF header listed within
944 // PHDRS {} script block.
945 template <class ELFT>
946 std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
947 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
948 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
949 if (!Cmd || Cmd->Name != SectionName)
952 std::vector<size_t> Ret;
953 for (StringRef PhdrName : Cmd->Phdrs)
954 Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName));
960 template <class ELFT>
961 size_t LinkerScript<ELFT>::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
963 for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
964 if (Cmd.Name == PhdrName)
968 error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
972 class elf::ScriptParser final : public ScriptParserBase {
973 typedef void (ScriptParser::*Handler)();
976 ScriptParser(MemoryBufferRef MB)
977 : ScriptParserBase(MB),
978 IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {}
980 void readLinkerScript();
981 void readVersionScript();
982 void readDynamicList();
985 void addFile(StringRef Path);
993 void readOutputArch();
994 void readOutputFormat();
996 void readSearchDir();
999 void readVersionScriptCommand();
1001 SymbolAssignment *readAssignment(StringRef Name);
1002 BytesDataCommand *readBytesDataCommand(StringRef Tok);
1003 uint32_t readFill();
1004 OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
1005 uint32_t readOutputSectionFiller(StringRef Tok);
1006 std::vector<StringRef> readOutputSectionPhdrs();
1007 InputSectionDescription *readInputSectionDescription(StringRef Tok);
1008 StringMatcher readFilePatterns();
1009 std::vector<SectionPattern> readInputSectionsList();
1010 InputSectionDescription *readInputSectionRules(StringRef FilePattern);
1011 unsigned readPhdrType();
1012 SortSectionPolicy readSortKind();
1013 SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
1014 SymbolAssignment *readProvideOrAssignment(StringRef Tok);
1019 Expr readExpr1(Expr Lhs, int MinPrec);
1020 StringRef readParenLiteral();
1022 Expr readTernary(Expr Cond);
1023 Expr readParenExpr();
1025 // For parsing version script.
1026 std::vector<SymbolVersion> readVersionExtern();
1027 void readAnonymousDeclaration();
1028 void readVersionDeclaration(StringRef VerStr);
1029 std::vector<SymbolVersion> readSymbols();
1031 ScriptConfiguration &Opt = *ScriptConfig;
1032 bool IsUnderSysroot;
1035 void ScriptParser::readDynamicList() {
1037 readAnonymousDeclaration();
1039 setError("EOF expected, but got " + next());
1042 void ScriptParser::readVersionScript() {
1043 readVersionScriptCommand();
1045 setError("EOF expected, but got " + next());
1048 void ScriptParser::readVersionScriptCommand() {
1050 readAnonymousDeclaration();
1054 while (!atEOF() && !Error && peek() != "}") {
1055 StringRef VerStr = next();
1056 if (VerStr == "{") {
1057 setError("anonymous version definition is used in "
1058 "combination with other version definitions");
1062 readVersionDeclaration(VerStr);
1066 void ScriptParser::readVersion() {
1068 readVersionScriptCommand();
1072 void ScriptParser::readLinkerScript() {
1074 StringRef Tok = next();
1078 if (Tok == "ASSERT") {
1079 Opt.Commands.emplace_back(new AssertCommand(readAssert()));
1080 } else if (Tok == "ENTRY") {
1082 } else if (Tok == "EXTERN") {
1084 } else if (Tok == "GROUP" || Tok == "INPUT") {
1086 } else if (Tok == "INCLUDE") {
1088 } else if (Tok == "OUTPUT") {
1090 } else if (Tok == "OUTPUT_ARCH") {
1092 } else if (Tok == "OUTPUT_FORMAT") {
1094 } else if (Tok == "PHDRS") {
1096 } else if (Tok == "SEARCH_DIR") {
1098 } else if (Tok == "SECTIONS") {
1100 } else if (Tok == "VERSION") {
1102 } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) {
1103 Opt.Commands.emplace_back(Cmd);
1105 setError("unknown directive: " + Tok);
1110 void ScriptParser::addFile(StringRef S) {
1111 if (IsUnderSysroot && S.startswith("/")) {
1112 SmallString<128> PathData;
1113 StringRef Path = (Config->Sysroot + S).toStringRef(PathData);
1114 if (sys::fs::exists(Path)) {
1115 Driver->addFile(Saver.save(Path));
1120 if (sys::path::is_absolute(S)) {
1122 } else if (S.startswith("=")) {
1123 if (Config->Sysroot.empty())
1124 Driver->addFile(S.substr(1));
1126 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
1127 } else if (S.startswith("-l")) {
1128 Driver->addLibrary(S.substr(2));
1129 } else if (sys::fs::exists(S)) {
1132 if (Optional<std::string> Path = findFromSearchPaths(S))
1133 Driver->addFile(Saver.save(*Path));
1135 setError("unable to find " + S);
1139 void ScriptParser::readAsNeeded() {
1141 bool Orig = Config->AsNeeded;
1142 Config->AsNeeded = true;
1143 while (!Error && !consume(")"))
1144 addFile(unquote(next()));
1145 Config->AsNeeded = Orig;
1148 void ScriptParser::readEntry() {
1149 // -e <symbol> takes predecence over ENTRY(<symbol>).
1151 StringRef Tok = next();
1152 if (Config->Entry.empty())
1153 Config->Entry = Tok;
1157 void ScriptParser::readExtern() {
1159 while (!Error && !consume(")"))
1160 Config->Undefined.push_back(next());
1163 void ScriptParser::readGroup() {
1165 while (!Error && !consume(")")) {
1166 StringRef Tok = next();
1167 if (Tok == "AS_NEEDED")
1170 addFile(unquote(Tok));
1174 void ScriptParser::readInclude() {
1175 StringRef Tok = unquote(next());
1176 // https://sourceware.org/binutils/docs/ld/File-Commands.html:
1177 // The file will be searched for in the current directory, and in any
1178 // directory specified with the -L option.
1179 auto MBOrErr = MemoryBuffer::getFile(Tok);
1181 if (Optional<std::string> Path = findFromSearchPaths(Tok))
1182 MBOrErr = MemoryBuffer::getFile(*Path);
1184 setError("cannot open " + Tok);
1187 MemoryBufferRef MBRef = (*MBOrErr)->getMemBufferRef();
1188 make<std::unique_ptr<MemoryBuffer>>(std::move(*MBOrErr)); // take MB ownership
1192 void ScriptParser::readOutput() {
1193 // -o <file> takes predecence over OUTPUT(<file>).
1195 StringRef Tok = next();
1196 if (Config->OutputFile.empty())
1197 Config->OutputFile = unquote(Tok);
1201 void ScriptParser::readOutputArch() {
1202 // Error checking only for now.
1208 void ScriptParser::readOutputFormat() {
1209 // Error checking only for now.
1212 StringRef Tok = next();
1216 setError("unexpected token: " + Tok);
1225 void ScriptParser::readPhdrs() {
1227 while (!Error && !consume("}")) {
1228 StringRef Tok = next();
1229 Opt.PhdrsCommands.push_back(
1230 {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
1231 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
1233 PhdrCmd.Type = readPhdrType();
1238 if (Tok == "FILEHDR")
1239 PhdrCmd.HasFilehdr = true;
1240 else if (Tok == "PHDRS")
1241 PhdrCmd.HasPhdrs = true;
1242 else if (Tok == "AT")
1243 PhdrCmd.LMAExpr = readParenExpr();
1244 else if (Tok == "FLAGS") {
1246 // Passing 0 for the value of dot is a bit of a hack. It means that
1247 // we accept expressions like ".|1".
1248 PhdrCmd.Flags = readExpr()(0);
1251 setError("unexpected header attribute: " + Tok);
1256 void ScriptParser::readSearchDir() {
1258 StringRef Tok = next();
1259 if (!Config->Nostdlib)
1260 Config->SearchPaths.push_back(unquote(Tok));
1264 void ScriptParser::readSections() {
1265 Opt.HasSections = true;
1266 // -no-rosegment is used to avoid placing read only non-executable sections in
1267 // their own segment. We do the same if SECTIONS command is present in linker
1268 // script. See comment for computeFlags().
1269 Config->SingleRoRx = true;
1272 while (!Error && !consume("}")) {
1273 StringRef Tok = next();
1274 BaseCommand *Cmd = readProvideOrAssignment(Tok);
1276 if (Tok == "ASSERT")
1277 Cmd = new AssertCommand(readAssert());
1279 Cmd = readOutputSectionDescription(Tok);
1281 Opt.Commands.emplace_back(Cmd);
1285 static int precedence(StringRef Op) {
1286 return StringSwitch<int>(Op)
1289 .Cases("<<", ">>", 3)
1290 .Cases("<", "<=", ">", ">=", "==", "!=", 2)
1295 StringMatcher ScriptParser::readFilePatterns() {
1296 std::vector<StringRef> V;
1297 while (!Error && !consume(")"))
1298 V.push_back(next());
1299 return StringMatcher(V);
1302 SortSectionPolicy ScriptParser::readSortKind() {
1303 if (consume("SORT") || consume("SORT_BY_NAME"))
1304 return SortSectionPolicy::Name;
1305 if (consume("SORT_BY_ALIGNMENT"))
1306 return SortSectionPolicy::Alignment;
1307 if (consume("SORT_BY_INIT_PRIORITY"))
1308 return SortSectionPolicy::Priority;
1309 if (consume("SORT_NONE"))
1310 return SortSectionPolicy::None;
1311 return SortSectionPolicy::Default;
1314 // Method reads a list of sequence of excluded files and section globs given in
1315 // a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+
1316 // Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3)
1317 // The semantics of that is next:
1318 // * Include .foo.1 from every file.
1319 // * Include .foo.2 from every file but a.o
1320 // * Include .foo.3 from every file but b.o
1321 std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
1322 std::vector<SectionPattern> Ret;
1323 while (!Error && peek() != ")") {
1324 StringMatcher ExcludeFilePat;
1325 if (consume("EXCLUDE_FILE")) {
1327 ExcludeFilePat = readFilePatterns();
1330 std::vector<StringRef> V;
1331 while (!Error && peek() != ")" && peek() != "EXCLUDE_FILE")
1332 V.push_back(next());
1335 Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)});
1337 setError("section pattern is expected");
1342 // Reads contents of "SECTIONS" directive. That directive contains a
1343 // list of glob patterns for input sections. The grammar is as follows.
1345 // <patterns> ::= <section-list>
1346 // | <sort> "(" <section-list> ")"
1347 // | <sort> "(" <sort> "(" <section-list> ")" ")"
1349 // <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
1350 // | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
1352 // <section-list> is parsed by readInputSectionsList().
1353 InputSectionDescription *
1354 ScriptParser::readInputSectionRules(StringRef FilePattern) {
1355 auto *Cmd = new InputSectionDescription(FilePattern);
1357 while (!Error && !consume(")")) {
1358 SortSectionPolicy Outer = readSortKind();
1359 SortSectionPolicy Inner = SortSectionPolicy::Default;
1360 std::vector<SectionPattern> V;
1361 if (Outer != SortSectionPolicy::Default) {
1363 Inner = readSortKind();
1364 if (Inner != SortSectionPolicy::Default) {
1366 V = readInputSectionsList();
1369 V = readInputSectionsList();
1373 V = readInputSectionsList();
1376 for (SectionPattern &Pat : V) {
1377 Pat.SortInner = Inner;
1378 Pat.SortOuter = Outer;
1381 std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
1386 InputSectionDescription *
1387 ScriptParser::readInputSectionDescription(StringRef Tok) {
1388 // Input section wildcard can be surrounded by KEEP.
1389 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
1390 if (Tok == "KEEP") {
1392 StringRef FilePattern = next();
1393 InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
1395 Opt.KeptSections.push_back(Cmd);
1398 return readInputSectionRules(Tok);
1401 void ScriptParser::readSort() {
1403 expect("CONSTRUCTORS");
1407 Expr ScriptParser::readAssert() {
1409 Expr E = readExpr();
1411 StringRef Msg = unquote(next());
1413 return [=](uint64_t Dot) {
1414 uint64_t V = E(Dot);
1421 // Reads a FILL(expr) command. We handle the FILL command as an
1422 // alias for =fillexp section attribute, which is different from
1423 // what GNU linkers do.
1424 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
1425 uint32_t ScriptParser::readFill() {
1427 uint32_t V = readOutputSectionFiller(next());
1433 OutputSectionCommand *
1434 ScriptParser::readOutputSectionDescription(StringRef OutSec) {
1435 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
1436 Cmd->Location = getCurrentLocation();
1438 // Read an address expression.
1439 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
1441 Cmd->AddrExpr = readExpr();
1446 Cmd->LMAExpr = readParenExpr();
1447 if (consume("ALIGN"))
1448 Cmd->AlignExpr = readParenExpr();
1449 if (consume("SUBALIGN"))
1450 Cmd->SubalignExpr = readParenExpr();
1452 // Parse constraints.
1453 if (consume("ONLY_IF_RO"))
1454 Cmd->Constraint = ConstraintKind::ReadOnly;
1455 if (consume("ONLY_IF_RW"))
1456 Cmd->Constraint = ConstraintKind::ReadWrite;
1459 while (!Error && !consume("}")) {
1460 StringRef Tok = next();
1461 if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok)) {
1462 Cmd->Commands.emplace_back(Assignment);
1463 } else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) {
1464 Cmd->Commands.emplace_back(Data);
1465 } else if (Tok == "ASSERT") {
1466 Cmd->Commands.emplace_back(new AssertCommand(readAssert()));
1468 } else if (Tok == "FILL") {
1469 Cmd->Filler = readFill();
1470 } else if (Tok == "SORT") {
1472 } else if (peek() == "(") {
1473 Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
1475 setError("unknown command " + Tok);
1478 Cmd->Phdrs = readOutputSectionPhdrs();
1481 Cmd->Filler = readOutputSectionFiller(next());
1482 else if (peek().startswith("="))
1483 Cmd->Filler = readOutputSectionFiller(next().drop_front());
1488 // Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
1489 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1491 // ld.gold is not fully compatible with ld.bfd. ld.bfd handles
1492 // hexstrings as blobs of arbitrary sizes, while ld.gold handles them
1493 // as 32-bit big-endian values. We will do the same as ld.gold does
1494 // because it's simpler than what ld.bfd does.
1495 uint32_t ScriptParser::readOutputSectionFiller(StringRef Tok) {
1497 if (!Tok.getAsInteger(0, V))
1499 setError("invalid filler expression: " + Tok);
1503 SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
1505 SymbolAssignment *Cmd = readAssignment(next());
1506 Cmd->Provide = Provide;
1507 Cmd->Hidden = Hidden;
1513 SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) {
1514 SymbolAssignment *Cmd = nullptr;
1515 if (peek() == "=" || peek() == "+=") {
1516 Cmd = readAssignment(Tok);
1518 } else if (Tok == "PROVIDE") {
1519 Cmd = readProvideHidden(true, false);
1520 } else if (Tok == "HIDDEN") {
1521 Cmd = readProvideHidden(false, true);
1522 } else if (Tok == "PROVIDE_HIDDEN") {
1523 Cmd = readProvideHidden(true, true);
1528 static uint64_t getSymbolValue(const Twine &Loc, StringRef S, uint64_t Dot) {
1531 return ScriptBase->getSymbolValue(Loc, S);
1534 static bool isAbsolute(StringRef S) {
1537 return ScriptBase->isAbsolute(S);
1540 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1541 StringRef Op = next();
1543 assert(Op == "=" || Op == "+=");
1544 if (consume("ABSOLUTE")) {
1545 // The RHS may be something like "ABSOLUTE(.) & 0xff".
1546 // Call readExpr1 to read the whole expression.
1547 E = readExpr1(readParenExpr(), 0);
1548 E.IsAbsolute = [] { return true; };
1553 std::string Loc = getCurrentLocation();
1554 E = [=](uint64_t Dot) {
1555 return getSymbolValue(Loc, Name, Dot) + E(Dot);
1558 return new SymbolAssignment(Name, E);
1561 // This is an operator-precedence parser to parse a linker
1562 // script expression.
1563 Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1565 static Expr combine(StringRef Op, Expr L, Expr R) {
1567 return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1569 return [=](uint64_t Dot) -> uint64_t {
1570 uint64_t RHS = R(Dot);
1572 error("division by zero");
1575 return L(Dot) / RHS;
1579 return {[=](uint64_t Dot) { return L(Dot) + R(Dot); },
1580 [=] { return L.IsAbsolute() && R.IsAbsolute(); },
1582 const OutputSectionBase *S = L.Section();
1583 return S ? S : R.Section();
1586 return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
1588 return [=](uint64_t Dot) { return L(Dot) << R(Dot); };
1590 return [=](uint64_t Dot) { return L(Dot) >> R(Dot); };
1592 return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
1594 return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
1596 return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
1598 return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
1600 return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
1602 return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
1604 return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
1606 return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
1607 llvm_unreachable("invalid operator");
1610 // This is a part of the operator-precedence parser. This function
1611 // assumes that the remaining token stream starts with an operator.
1612 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1613 while (!atEOF() && !Error) {
1614 // Read an operator and an expression.
1616 return readTernary(Lhs);
1617 StringRef Op1 = peek();
1618 if (precedence(Op1) < MinPrec)
1621 Expr Rhs = readPrimary();
1623 // Evaluate the remaining part of the expression first if the
1624 // next operator has greater precedence than the previous one.
1625 // For example, if we have read "+" and "3", and if the next
1626 // operator is "*", then we'll evaluate 3 * ... part first.
1628 StringRef Op2 = peek();
1629 if (precedence(Op2) <= precedence(Op1))
1631 Rhs = readExpr1(Rhs, precedence(Op2));
1634 Lhs = combine(Op1, Lhs, Rhs);
1639 uint64_t static getConstant(StringRef S) {
1640 if (S == "COMMONPAGESIZE")
1641 return Target->PageSize;
1642 if (S == "MAXPAGESIZE")
1643 return Config->MaxPageSize;
1644 error("unknown constant: " + S);
1648 // Parses Tok as an integer. Returns true if successful.
1649 // It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1650 // and decimal numbers. Decimal numbers may have "K" (kilo) or
1651 // "M" (mega) prefixes.
1652 static bool readInteger(StringRef Tok, uint64_t &Result) {
1654 if (Tok.startswith("-")) {
1655 if (!readInteger(Tok.substr(1), Result))
1662 if (Tok.startswith_lower("0x"))
1663 return !Tok.substr(2).getAsInteger(16, Result);
1664 if (Tok.endswith_lower("H"))
1665 return !Tok.drop_back().getAsInteger(16, Result);
1669 if (Tok.endswith_lower("K")) {
1671 Tok = Tok.drop_back();
1672 } else if (Tok.endswith_lower("M")) {
1673 Suffix = 1024 * 1024;
1674 Tok = Tok.drop_back();
1676 if (Tok.getAsInteger(10, Result))
1682 BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) {
1683 int Size = StringSwitch<unsigned>(Tok)
1692 return new BytesDataCommand(readParenExpr(), Size);
1695 StringRef ScriptParser::readParenLiteral() {
1697 StringRef Tok = next();
1702 Expr ScriptParser::readPrimary() {
1704 return readParenExpr();
1706 StringRef Tok = next();
1707 std::string Location = getCurrentLocation();
1710 Expr E = readPrimary();
1711 return [=](uint64_t Dot) { return ~E(Dot); };
1714 Expr E = readPrimary();
1715 return [=](uint64_t Dot) { return -E(Dot); };
1718 // Built-in functions are parsed here.
1719 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1720 if (Tok == "ADDR") {
1721 StringRef Name = readParenLiteral();
1722 return {[=](uint64_t Dot) {
1723 return ScriptBase->getOutputSection(Location, Name)->Addr;
1725 [=] { return false; },
1726 [=] { return ScriptBase->getOutputSection(Location, Name); }};
1728 if (Tok == "LOADADDR") {
1729 StringRef Name = readParenLiteral();
1730 return [=](uint64_t Dot) {
1731 return ScriptBase->getOutputSection(Location, Name)->getLMA();
1734 if (Tok == "ASSERT")
1735 return readAssert();
1736 if (Tok == "ALIGN") {
1738 Expr E = readExpr();
1740 Expr E2 = readExpr();
1742 return [=](uint64_t Dot) { return alignTo(E(Dot), E2(Dot)); };
1745 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1747 if (Tok == "CONSTANT") {
1748 StringRef Name = readParenLiteral();
1749 return [=](uint64_t Dot) { return getConstant(Name); };
1751 if (Tok == "DEFINED") {
1752 StringRef Name = readParenLiteral();
1753 return [=](uint64_t Dot) { return ScriptBase->isDefined(Name) ? 1 : 0; };
1755 if (Tok == "SEGMENT_START") {
1759 Expr E = readExpr();
1761 return [=](uint64_t Dot) { return E(Dot); };
1763 if (Tok == "DATA_SEGMENT_ALIGN") {
1765 Expr E = readExpr();
1769 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1771 if (Tok == "DATA_SEGMENT_END") {
1775 return [](uint64_t Dot) { return Dot; };
1777 // GNU linkers implements more complicated logic to handle
1778 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1779 // the next page boundary for simplicity.
1780 if (Tok == "DATA_SEGMENT_RELRO_END") {
1786 return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1788 if (Tok == "SIZEOF") {
1789 StringRef Name = readParenLiteral();
1790 return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
1792 if (Tok == "ALIGNOF") {
1793 StringRef Name = readParenLiteral();
1794 return [=](uint64_t Dot) {
1795 return ScriptBase->getOutputSection(Location, Name)->Addralign;
1798 if (Tok == "SIZEOF_HEADERS")
1799 return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
1801 // Tok is a literal number.
1803 if (readInteger(Tok, V))
1804 return [=](uint64_t Dot) { return V; };
1806 // Tok is a symbol name.
1807 if (Tok != "." && !isValidCIdentifier(Tok))
1808 setError("malformed number: " + Tok);
1809 return {[=](uint64_t Dot) { return getSymbolValue(Location, Tok, Dot); },
1810 [=] { return isAbsolute(Tok); },
1811 [=] { return ScriptBase->getSymbolSection(Tok); }};
1814 Expr ScriptParser::readTernary(Expr Cond) {
1815 Expr L = readExpr();
1817 Expr R = readExpr();
1818 return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1821 Expr ScriptParser::readParenExpr() {
1823 Expr E = readExpr();
1828 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1829 std::vector<StringRef> Phdrs;
1830 while (!Error && peek().startswith(":")) {
1831 StringRef Tok = next();
1832 Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1));
1837 // Read a program header type name. The next token must be a
1838 // name of a program header type or a constant (e.g. "0x3").
1839 unsigned ScriptParser::readPhdrType() {
1840 StringRef Tok = next();
1842 if (readInteger(Tok, Val))
1845 unsigned Ret = StringSwitch<unsigned>(Tok)
1846 .Case("PT_NULL", PT_NULL)
1847 .Case("PT_LOAD", PT_LOAD)
1848 .Case("PT_DYNAMIC", PT_DYNAMIC)
1849 .Case("PT_INTERP", PT_INTERP)
1850 .Case("PT_NOTE", PT_NOTE)
1851 .Case("PT_SHLIB", PT_SHLIB)
1852 .Case("PT_PHDR", PT_PHDR)
1853 .Case("PT_TLS", PT_TLS)
1854 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1855 .Case("PT_GNU_STACK", PT_GNU_STACK)
1856 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1857 .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1858 .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1859 .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
1862 if (Ret == (unsigned)-1) {
1863 setError("invalid program header type: " + Tok);
1869 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1870 void ScriptParser::readAnonymousDeclaration() {
1871 // Read global symbols first. "global:" is default, so if there's
1872 // no label, we assume global symbols.
1873 if (consume("global:") || peek() != "local:")
1874 Config->VersionScriptGlobals = readSymbols();
1876 // Next, read local symbols.
1877 if (consume("local:")) {
1879 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1882 setError("local symbol list for anonymous version is not supported");
1889 // Reads a list of symbols, e.g. "VerStr { global: foo; bar; local: *; };".
1890 void ScriptParser::readVersionDeclaration(StringRef VerStr) {
1891 // Identifiers start at 2 because 0 and 1 are reserved
1892 // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1893 uint16_t VersionId = Config->VersionDefinitions.size() + 2;
1894 Config->VersionDefinitions.push_back({VerStr, VersionId});
1896 // Read global symbols.
1897 if (consume("global:") || peek() != "local:")
1898 Config->VersionDefinitions.back().Globals = readSymbols();
1900 // Read local symbols.
1901 if (consume("local:")) {
1903 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1906 for (SymbolVersion V : readSymbols())
1907 Config->VersionScriptLocals.push_back(V);
1912 // Each version may have a parent version. For example, "Ver2"
1913 // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1914 // as a parent. This version hierarchy is, probably against your
1915 // instinct, purely for hint; the runtime doesn't care about it
1916 // at all. In LLD, we simply ignore it.
1922 // Reads a list of symbols for a versions cript.
1923 std::vector<SymbolVersion> ScriptParser::readSymbols() {
1924 std::vector<SymbolVersion> Ret;
1926 if (consume("extern")) {
1927 for (SymbolVersion V : readVersionExtern())
1932 if (peek() == "}" || peek() == "local:" || Error)
1934 StringRef Tok = next();
1935 Ret.push_back({unquote(Tok), false, hasWildcard(Tok)});
1941 // Reads an "extern C++" directive, e.g.,
1942 // "extern "C++" { ns::*; "f(int, double)"; };"
1943 std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
1944 StringRef Tok = next();
1945 bool IsCXX = Tok == "\"C++\"";
1946 if (!IsCXX && Tok != "\"C\"")
1947 setError("Unknown language");
1950 std::vector<SymbolVersion> Ret;
1951 while (!Error && peek() != "}") {
1952 StringRef Tok = next();
1953 bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok);
1954 Ret.push_back({unquote(Tok), IsCXX, HasWildcard});
1963 void elf::readLinkerScript(MemoryBufferRef MB) {
1964 ScriptParser(MB).readLinkerScript();
1967 void elf::readVersionScript(MemoryBufferRef MB) {
1968 ScriptParser(MB).readVersionScript();
1971 void elf::readDynamicList(MemoryBufferRef MB) {
1972 ScriptParser(MB).readDynamicList();
1975 template class elf::LinkerScript<ELF32LE>;
1976 template class elf::LinkerScript<ELF32BE>;
1977 template class elf::LinkerScript<ELF64LE>;
1978 template class elf::LinkerScript<ELF64BE>;