1 //===- LinkerScript.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 the parser/evaluator of the linker script.
11 //===----------------------------------------------------------------------===//
13 #include "LinkerScript.h"
15 #include "InputSection.h"
16 #include "OutputSections.h"
17 #include "SymbolTable.h"
19 #include "SyntheticSections.h"
22 #include "lld/Common/Memory.h"
23 #include "lld/Common/Strings.h"
24 #include "lld/Common/Threads.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/BinaryFormat/ELF.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Endian.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/Path.h"
43 using namespace llvm::ELF;
44 using namespace llvm::object;
45 using namespace llvm::support::endian;
51 static uint64_t getOutputSectionVA(SectionBase *sec) {
52 OutputSection *os = sec->getOutputSection();
53 assert(os && "input section has no output section assigned");
54 return os ? os->addr : 0;
57 uint64_t ExprValue::getValue() const {
59 return alignTo(sec->getOffset(val) + getOutputSectionVA(sec),
61 return alignTo(val, alignment);
64 uint64_t ExprValue::getSecAddr() const {
66 return sec->getOffset(0) + getOutputSectionVA(sec);
70 uint64_t ExprValue::getSectionOffset() const {
71 // If the alignment is trivial, we don't have to compute the full
72 // value to know the offset. This allows this function to succeed in
73 // cases where the output section is not yet known.
74 if (alignment == 1 && !sec)
76 return getValue() - getSecAddr();
79 OutputSection *LinkerScript::createOutputSection(StringRef name,
81 OutputSection *&secRef = nameToOutputSection[name];
83 if (secRef && secRef->location.empty()) {
84 // There was a forward reference.
87 sec = make<OutputSection>(name, SHT_PROGBITS, 0);
91 sec->location = location;
95 OutputSection *LinkerScript::getOrCreateOutputSection(StringRef name) {
96 OutputSection *&cmdRef = nameToOutputSection[name];
98 cmdRef = make<OutputSection>(name, SHT_PROGBITS, 0);
102 // Expands the memory region by the specified size.
103 static void expandMemoryRegion(MemoryRegion *memRegion, uint64_t size,
104 StringRef regionName, StringRef secName) {
105 memRegion->curPos += size;
106 uint64_t newSize = memRegion->curPos - memRegion->origin;
107 if (newSize > memRegion->length)
108 error("section '" + secName + "' will not fit in region '" + regionName +
109 "': overflowed by " + Twine(newSize - memRegion->length) + " bytes");
112 void LinkerScript::expandMemoryRegions(uint64_t size) {
114 expandMemoryRegion(ctx->memRegion, size, ctx->memRegion->name,
116 // Only expand the LMARegion if it is different from memRegion.
117 if (ctx->lmaRegion && ctx->memRegion != ctx->lmaRegion)
118 expandMemoryRegion(ctx->lmaRegion, size, ctx->lmaRegion->name,
122 void LinkerScript::expandOutputSection(uint64_t size) {
123 ctx->outSec->size += size;
124 expandMemoryRegions(size);
127 void LinkerScript::setDot(Expr e, const Twine &loc, bool inSec) {
128 uint64_t val = e().getValue();
129 if (val < dot && inSec)
130 error(loc + ": unable to move location counter backward for: " +
133 // Update to location counter means update to section size.
135 expandOutputSection(val - dot);
140 // Used for handling linker symbol assignments, for both finalizing
141 // their values and doing early declarations. Returns true if symbol
142 // should be defined from linker script.
143 static bool shouldDefineSym(SymbolAssignment *cmd) {
144 if (cmd->name == ".")
150 // If a symbol was in PROVIDE(), we need to define it only
151 // when it is a referenced undefined symbol.
152 Symbol *b = symtab->find(cmd->name);
153 if (b && !b->isDefined())
158 // Called by processSymbolAssignments() to assign definitions to
159 // linker-script-defined symbols.
160 void LinkerScript::addSymbol(SymbolAssignment *cmd) {
161 if (!shouldDefineSym(cmd))
165 ExprValue value = cmd->expression();
166 SectionBase *sec = value.isAbsolute() ? nullptr : value.sec;
167 uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
169 // When this function is called, section addresses have not been
170 // fixed yet. So, we may or may not know the value of the RHS
173 // For example, if an expression is `x = 42`, we know x is always 42.
174 // However, if an expression is `x = .`, there's no way to know its
175 // value at the moment.
177 // We want to set symbol values early if we can. This allows us to
178 // use symbols as variables in linker scripts. Doing so allows us to
179 // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
180 uint64_t symValue = value.sec ? 0 : value.getValue();
182 Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE,
185 Symbol *sym = symtab->insert(cmd->name);
186 sym->mergeProperties(newSym);
187 sym->replace(newSym);
188 cmd->sym = cast<Defined>(sym);
191 // This function is called from LinkerScript::declareSymbols.
192 // It creates a placeholder symbol if needed.
193 static void declareSymbol(SymbolAssignment *cmd) {
194 if (!shouldDefineSym(cmd))
197 uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
198 Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE, 0, 0,
201 // We can't calculate final value right now.
202 Symbol *sym = symtab->insert(cmd->name);
203 sym->mergeProperties(newSym);
204 sym->replace(newSym);
206 cmd->sym = cast<Defined>(sym);
207 cmd->provide = false;
208 sym->scriptDefined = true;
211 using SymbolAssignmentMap =
212 DenseMap<const Defined *, std::pair<SectionBase *, uint64_t>>;
214 // Collect section/value pairs of linker-script-defined symbols. This is used to
215 // check whether symbol values converge.
216 static SymbolAssignmentMap
217 getSymbolAssignmentValues(const std::vector<BaseCommand *> §ionCommands) {
218 SymbolAssignmentMap ret;
219 for (BaseCommand *base : sectionCommands) {
220 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
221 if (cmd->sym) // sym is nullptr for dot.
222 ret.try_emplace(cmd->sym,
223 std::make_pair(cmd->sym->section, cmd->sym->value));
226 for (BaseCommand *sub_base : cast<OutputSection>(base)->sectionCommands)
227 if (auto *cmd = dyn_cast<SymbolAssignment>(sub_base))
229 ret.try_emplace(cmd->sym,
230 std::make_pair(cmd->sym->section, cmd->sym->value));
235 // Returns the lexicographical smallest (for determinism) Defined whose
236 // section/value has changed.
237 static const Defined *
238 getChangedSymbolAssignment(const SymbolAssignmentMap &oldValues) {
239 const Defined *changed = nullptr;
240 for (auto &it : oldValues) {
241 const Defined *sym = it.first;
242 if (std::make_pair(sym->section, sym->value) != it.second &&
243 (!changed || sym->getName() < changed->getName()))
249 // This method is used to handle INSERT AFTER statement. Here we rebuild
250 // the list of script commands to mix sections inserted into.
251 void LinkerScript::processInsertCommands() {
252 std::vector<BaseCommand *> v;
253 auto insert = [&](std::vector<BaseCommand *> &from) {
254 v.insert(v.end(), from.begin(), from.end());
258 for (BaseCommand *base : sectionCommands) {
259 if (auto *os = dyn_cast<OutputSection>(base)) {
260 insert(insertBeforeCommands[os->name]);
262 insert(insertAfterCommands[os->name]);
268 for (auto &cmds : {insertBeforeCommands, insertAfterCommands})
269 for (const std::pair<StringRef, std::vector<BaseCommand *>> &p : cmds)
270 if (!p.second.empty())
271 error("unable to INSERT AFTER/BEFORE " + p.first +
272 ": section not defined");
274 sectionCommands = std::move(v);
277 // Symbols defined in script should not be inlined by LTO. At the same time
278 // we don't know their final values until late stages of link. Here we scan
279 // over symbol assignment commands and create placeholder symbols if needed.
280 void LinkerScript::declareSymbols() {
282 for (BaseCommand *base : sectionCommands) {
283 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
288 // If the output section directive has constraints,
289 // we can't say for sure if it is going to be included or not.
290 // Skip such sections for now. Improve the checks if we ever
291 // need symbols from that sections to be declared early.
292 auto *sec = cast<OutputSection>(base);
293 if (sec->constraint != ConstraintKind::NoConstraint)
295 for (BaseCommand *base2 : sec->sectionCommands)
296 if (auto *cmd = dyn_cast<SymbolAssignment>(base2))
301 // This function is called from assignAddresses, while we are
302 // fixing the output section addresses. This function is supposed
303 // to set the final value for a given symbol assignment.
304 void LinkerScript::assignSymbol(SymbolAssignment *cmd, bool inSec) {
305 if (cmd->name == ".") {
306 setDot(cmd->expression, cmd->location, inSec);
313 ExprValue v = cmd->expression();
314 if (v.isAbsolute()) {
315 cmd->sym->section = nullptr;
316 cmd->sym->value = v.getValue();
318 cmd->sym->section = v.sec;
319 cmd->sym->value = v.getSectionOffset();
323 static std::string getFilename(InputFile *file) {
326 if (file->archiveName.empty())
327 return file->getName();
328 return (file->archiveName + "(" + file->getName() + ")").str();
331 bool LinkerScript::shouldKeep(InputSectionBase *s) {
332 if (keptSections.empty())
334 std::string filename = getFilename(s->file);
335 for (InputSectionDescription *id : keptSections)
336 if (id->filePat.match(filename))
337 for (SectionPattern &p : id->sectionPatterns)
338 if (p.sectionPat.match(s->name))
343 // A helper function for the SORT() command.
344 static bool matchConstraints(ArrayRef<InputSectionBase *> sections,
345 ConstraintKind kind) {
346 if (kind == ConstraintKind::NoConstraint)
349 bool isRW = llvm::any_of(
350 sections, [](InputSectionBase *sec) { return sec->flags & SHF_WRITE; });
352 return (isRW && kind == ConstraintKind::ReadWrite) ||
353 (!isRW && kind == ConstraintKind::ReadOnly);
356 static void sortSections(MutableArrayRef<InputSectionBase *> vec,
357 SortSectionPolicy k) {
358 auto alignmentComparator = [](InputSectionBase *a, InputSectionBase *b) {
359 // ">" is not a mistake. Sections with larger alignments are placed
360 // before sections with smaller alignments in order to reduce the
361 // amount of padding necessary. This is compatible with GNU.
362 return a->alignment > b->alignment;
364 auto nameComparator = [](InputSectionBase *a, InputSectionBase *b) {
365 return a->name < b->name;
367 auto priorityComparator = [](InputSectionBase *a, InputSectionBase *b) {
368 return getPriority(a->name) < getPriority(b->name);
372 case SortSectionPolicy::Default:
373 case SortSectionPolicy::None:
375 case SortSectionPolicy::Alignment:
376 return llvm::stable_sort(vec, alignmentComparator);
377 case SortSectionPolicy::Name:
378 return llvm::stable_sort(vec, nameComparator);
379 case SortSectionPolicy::Priority:
380 return llvm::stable_sort(vec, priorityComparator);
384 // Sort sections as instructed by SORT-family commands and --sort-section
385 // option. Because SORT-family commands can be nested at most two depth
386 // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
387 // line option is respected even if a SORT command is given, the exact
388 // behavior we have here is a bit complicated. Here are the rules.
390 // 1. If two SORT commands are given, --sort-section is ignored.
391 // 2. If one SORT command is given, and if it is not SORT_NONE,
392 // --sort-section is handled as an inner SORT command.
393 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
394 // 4. If no SORT command is given, sort according to --sort-section.
395 static void sortInputSections(MutableArrayRef<InputSectionBase *> vec,
396 const SectionPattern &pat) {
397 if (pat.sortOuter == SortSectionPolicy::None)
400 if (pat.sortInner == SortSectionPolicy::Default)
401 sortSections(vec, config->sortSection);
403 sortSections(vec, pat.sortInner);
404 sortSections(vec, pat.sortOuter);
407 // Compute and remember which sections the InputSectionDescription matches.
408 std::vector<InputSectionBase *>
409 LinkerScript::computeInputSections(const InputSectionDescription *cmd) {
410 std::vector<InputSectionBase *> ret;
412 // Collects all sections that satisfy constraints of Cmd.
413 for (const SectionPattern &pat : cmd->sectionPatterns) {
414 size_t sizeBefore = ret.size();
416 for (InputSectionBase *sec : inputSections) {
417 if (!sec->isLive() || sec->parent)
420 // For -emit-relocs we have to ignore entries like
421 // .rela.dyn : { *(.rela.data) }
422 // which are common because they are in the default bfd script.
423 // We do not ignore SHT_REL[A] linker-synthesized sections here because
424 // want to support scripts that do custom layout for them.
425 if (isa<InputSection>(sec) &&
426 cast<InputSection>(sec)->getRelocatedSection())
429 std::string filename = getFilename(sec->file);
430 if (!cmd->filePat.match(filename) ||
431 pat.excludedFilePat.match(filename) ||
432 !pat.sectionPat.match(sec->name))
439 MutableArrayRef<InputSectionBase *>(ret).slice(sizeBefore), pat);
444 void LinkerScript::discard(InputSectionBase *s) {
445 if (s == in.shStrTab || s == mainPart->relrDyn)
446 error("discarding " + s->name + " section is not allowed");
448 // You can discard .hash and .gnu.hash sections by linker scripts. Since
449 // they are synthesized sections, we need to handle them differently than
450 // other regular sections.
451 if (s == mainPart->gnuHashTab)
452 mainPart->gnuHashTab = nullptr;
453 if (s == mainPart->hashTab)
454 mainPart->hashTab = nullptr;
458 for (InputSection *ds : s->dependentSections)
462 std::vector<InputSectionBase *>
463 LinkerScript::createInputSectionList(OutputSection &outCmd) {
464 std::vector<InputSectionBase *> ret;
466 for (BaseCommand *base : outCmd.sectionCommands) {
467 if (auto *cmd = dyn_cast<InputSectionDescription>(base)) {
468 cmd->sectionBases = computeInputSections(cmd);
469 for (InputSectionBase *s : cmd->sectionBases)
471 ret.insert(ret.end(), cmd->sectionBases.begin(), cmd->sectionBases.end());
477 // Create output sections described by SECTIONS commands.
478 void LinkerScript::processSectionCommands() {
480 for (BaseCommand *base : sectionCommands) {
481 if (auto *sec = dyn_cast<OutputSection>(base)) {
482 std::vector<InputSectionBase *> v = createInputSectionList(*sec);
484 // The output section name `/DISCARD/' is special.
485 // Any input section assigned to it is discarded.
486 if (sec->name == "/DISCARD/") {
487 for (InputSectionBase *s : v)
489 sec->sectionCommands.clear();
493 // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
494 // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
495 // sections satisfy a given constraint. If not, a directive is handled
496 // as if it wasn't present from the beginning.
498 // Because we'll iterate over SectionCommands many more times, the easy
499 // way to "make it as if it wasn't present" is to make it empty.
500 if (!matchConstraints(v, sec->constraint)) {
501 for (InputSectionBase *s : v)
503 sec->sectionCommands.clear();
507 // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
508 // is given, input sections are aligned to that value, whether the
509 // given value is larger or smaller than the original section alignment.
510 if (sec->subalignExpr) {
511 uint32_t subalign = sec->subalignExpr().getValue();
512 for (InputSectionBase *s : v)
513 s->alignment = subalign;
516 // Set the partition field the same way OutputSection::recordSection()
517 // does. Partitions cannot be used with the SECTIONS command, so this is
521 sec->sectionIndex = i++;
526 void LinkerScript::processSymbolAssignments() {
527 // Dot outside an output section still represents a relative address, whose
528 // sh_shndx should not be SHN_UNDEF or SHN_ABS. Create a dummy aether section
529 // that fills the void outside a section. It has an index of one, which is
530 // indistinguishable from any other regular section index.
531 aether = make<OutputSection>("", 0, SHF_ALLOC);
532 aether->sectionIndex = 1;
534 // ctx captures the local AddressState and makes it accessible deliberately.
535 // This is needed as there are some cases where we cannot just thread the
536 // current state through to a lambda function created by the script parser.
539 ctx->outSec = aether;
541 for (BaseCommand *base : sectionCommands) {
542 if (auto *cmd = dyn_cast<SymbolAssignment>(base))
545 for (BaseCommand *sub_base : cast<OutputSection>(base)->sectionCommands)
546 if (auto *cmd = dyn_cast<SymbolAssignment>(sub_base))
553 static OutputSection *findByName(ArrayRef<BaseCommand *> vec,
555 for (BaseCommand *base : vec)
556 if (auto *sec = dyn_cast<OutputSection>(base))
557 if (sec->name == name)
562 static OutputSection *createSection(InputSectionBase *isec,
563 StringRef outsecName) {
564 OutputSection *sec = script->createOutputSection(outsecName, "<internal>");
565 sec->recordSection(isec);
569 static OutputSection *
570 addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
571 InputSectionBase *isec, StringRef outsecName) {
572 // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
573 // option is given. A section with SHT_GROUP defines a "section group", and
574 // its members have SHF_GROUP attribute. Usually these flags have already been
575 // stripped by InputFiles.cpp as section groups are processed and uniquified.
576 // However, for the -r option, we want to pass through all section groups
577 // as-is because adding/removing members or merging them with other groups
578 // change their semantics.
579 if (isec->type == SHT_GROUP || (isec->flags & SHF_GROUP))
580 return createSection(isec, outsecName);
582 // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
583 // relocation sections .rela.foo and .rela.bar for example. Most tools do
584 // not allow multiple REL[A] sections for output section. Hence we
585 // should combine these relocation sections into single output.
586 // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
587 // other REL[A] sections created by linker itself.
588 if (!isa<SyntheticSection>(isec) &&
589 (isec->type == SHT_REL || isec->type == SHT_RELA)) {
590 auto *sec = cast<InputSection>(isec);
591 OutputSection *out = sec->getRelocatedSection()->getOutputSection();
593 if (out->relocationSection) {
594 out->relocationSection->recordSection(sec);
598 out->relocationSection = createSection(isec, outsecName);
599 return out->relocationSection;
602 // The ELF spec just says
603 // ----------------------------------------------------------------
604 // In the first phase, input sections that match in name, type and
605 // attribute flags should be concatenated into single sections.
606 // ----------------------------------------------------------------
608 // However, it is clear that at least some flags have to be ignored for
609 // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
610 // ignored. We should not have two output .text sections just because one was
611 // in a group and another was not for example.
613 // It also seems that wording was a late addition and didn't get the
614 // necessary scrutiny.
616 // Merging sections with different flags is expected by some users. One
617 // reason is that if one file has
619 // int *const bar __attribute__((section(".foo"))) = (int *)0;
621 // gcc with -fPIC will produce a read only .foo section. But if another
625 // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
627 // gcc with -fPIC will produce a read write section.
629 // Last but not least, when using linker script the merge rules are forced by
630 // the script. Unfortunately, linker scripts are name based. This means that
631 // expressions like *(.foo*) can refer to multiple input sections with
632 // different flags. We cannot put them in different output sections or we
633 // would produce wrong results for
635 // start = .; *(.foo.*) end = .; *(.bar)
637 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
638 // another. The problem is that there is no way to layout those output
639 // sections such that the .foo sections are the only thing between the start
642 // Given the above issues, we instead merge sections by name and error on
643 // incompatible types and flags.
644 TinyPtrVector<OutputSection *> &v = map[outsecName];
645 for (OutputSection *sec : v) {
646 if (sec->partition != isec->partition)
649 if (config->relocatable && (isec->flags & SHF_LINK_ORDER)) {
650 // Merging two SHF_LINK_ORDER sections with different sh_link fields will
651 // change their semantics, so we only merge them in -r links if they will
652 // end up being linked to the same output section. The casts are fine
653 // because everything in the map was created by the orphan placement code.
654 auto *firstIsec = cast<InputSectionBase>(
655 cast<InputSectionDescription>(sec->sectionCommands[0])
657 if (firstIsec->getLinkOrderDep()->getOutputSection() !=
658 isec->getLinkOrderDep()->getOutputSection())
662 sec->recordSection(isec);
666 OutputSection *sec = createSection(isec, outsecName);
671 // Add sections that didn't match any sections command.
672 void LinkerScript::addOrphanSections() {
673 StringMap<TinyPtrVector<OutputSection *>> map;
674 std::vector<OutputSection *> v;
676 std::function<void(InputSectionBase *)> add;
677 add = [&](InputSectionBase *s) {
678 if (s->isLive() && !s->parent) {
679 StringRef name = getOutputSectionName(s);
681 if (config->orphanHandling == OrphanHandlingPolicy::Error)
682 error(toString(s) + " is being placed in '" + name + "'");
683 else if (config->orphanHandling == OrphanHandlingPolicy::Warn)
684 warn(toString(s) + " is being placed in '" + name + "'");
686 if (OutputSection *sec = findByName(sectionCommands, name)) {
687 sec->recordSection(s);
689 if (OutputSection *os = addInputSec(map, s, name))
691 assert(isa<MergeInputSection>(s) ||
692 s->getOutputSection()->sectionIndex == UINT32_MAX);
696 if (config->relocatable)
697 for (InputSectionBase *depSec : s->dependentSections)
698 if (depSec->flags & SHF_LINK_ORDER)
702 // For futher --emit-reloc handling code we need target output section
703 // to be created before we create relocation output section, so we want
704 // to create target sections first. We do not want priority handling
705 // for synthetic sections because them are special.
706 for (InputSectionBase *isec : inputSections) {
707 // In -r links, SHF_LINK_ORDER sections are added while adding their parent
708 // sections because we need to know the parent's output section before we
709 // can select an output section for the SHF_LINK_ORDER section.
710 if (config->relocatable && (isec->flags & SHF_LINK_ORDER))
713 if (auto *sec = dyn_cast<InputSection>(isec))
714 if (InputSectionBase *rel = sec->getRelocatedSection())
715 if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
720 // If no SECTIONS command was given, we should insert sections commands
721 // before others, so that we can handle scripts which refers them,
722 // for example: "foo = ABSOLUTE(ADDR(.text)));".
723 // When SECTIONS command is present we just add all orphans to the end.
724 if (hasSectionsCommand)
725 sectionCommands.insert(sectionCommands.end(), v.begin(), v.end());
727 sectionCommands.insert(sectionCommands.begin(), v.begin(), v.end());
730 uint64_t LinkerScript::advance(uint64_t size, unsigned alignment) {
732 (ctx->outSec->flags & SHF_TLS) && ctx->outSec->type == SHT_NOBITS;
733 uint64_t start = isTbss ? dot + ctx->threadBssOffset : dot;
734 start = alignTo(start, alignment);
735 uint64_t end = start + size;
738 ctx->threadBssOffset = end - dot;
744 void LinkerScript::output(InputSection *s) {
745 assert(ctx->outSec == s->getParent());
746 uint64_t before = advance(0, 1);
747 uint64_t pos = advance(s->getSize(), s->alignment);
748 s->outSecOff = pos - s->getSize() - ctx->outSec->addr;
750 // Update output section size after adding each section. This is so that
751 // SIZEOF works correctly in the case below:
752 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
753 expandOutputSection(pos - before);
756 void LinkerScript::switchTo(OutputSection *sec) {
759 uint64_t before = advance(0, 1);
760 ctx->outSec->addr = advance(0, ctx->outSec->alignment);
761 expandMemoryRegions(ctx->outSec->addr - before);
764 // This function searches for a memory region to place the given output
765 // section in. If found, a pointer to the appropriate memory region is
766 // returned. Otherwise, a nullptr is returned.
767 MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *sec) {
768 // If a memory region name was specified in the output section command,
769 // then try to find that region first.
770 if (!sec->memoryRegionName.empty()) {
771 if (MemoryRegion *m = memoryRegions.lookup(sec->memoryRegionName))
773 error("memory region '" + sec->memoryRegionName + "' not declared");
777 // If at least one memory region is defined, all sections must
778 // belong to some memory region. Otherwise, we don't need to do
779 // anything for memory regions.
780 if (memoryRegions.empty())
783 // See if a region can be found by matching section flags.
784 for (auto &pair : memoryRegions) {
785 MemoryRegion *m = pair.second;
786 if ((m->flags & sec->flags) && (m->negFlags & sec->flags) == 0)
790 // Otherwise, no suitable region was found.
791 if (sec->flags & SHF_ALLOC)
792 error("no memory region specified for section '" + sec->name + "'");
796 static OutputSection *findFirstSection(PhdrEntry *load) {
797 for (OutputSection *sec : outputSections)
798 if (sec->ptLoad == load)
803 // This function assigns offsets to input sections and an output section
804 // for a single sections command (e.g. ".text { *(.text); }").
805 void LinkerScript::assignOffsets(OutputSection *sec) {
806 if (!(sec->flags & SHF_ALLOC))
809 ctx->memRegion = sec->memRegion;
810 ctx->lmaRegion = sec->lmaRegion;
812 dot = ctx->memRegion->curPos;
814 if ((sec->flags & SHF_ALLOC) && sec->addrExpr)
815 setDot(sec->addrExpr, sec->location, false);
817 // If the address of the section has been moved forward by an explicit
818 // expression so that it now starts past the current curPos of the enclosing
819 // region, we need to expand the current region to account for the space
820 // between the previous section, if any, and the start of this section.
821 if (ctx->memRegion && ctx->memRegion->curPos < dot)
822 expandMemoryRegion(ctx->memRegion, dot - ctx->memRegion->curPos,
823 ctx->memRegion->name, sec->name);
828 ctx->lmaOffset = sec->lmaExpr().getValue() - dot;
830 if (MemoryRegion *mr = sec->lmaRegion)
831 ctx->lmaOffset = mr->curPos - dot;
833 // If neither AT nor AT> is specified for an allocatable section, the linker
834 // will set the LMA such that the difference between VMA and LMA for the
835 // section is the same as the preceding output section in the same region
836 // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
837 // This, however, should only be done by the first "non-header" section
839 if (PhdrEntry *l = ctx->outSec->ptLoad)
840 if (sec == findFirstSection(l))
841 l->lmaOffset = ctx->lmaOffset;
843 // We can call this method multiple times during the creation of
844 // thunks and want to start over calculation each time.
847 // We visited SectionsCommands from processSectionCommands to
848 // layout sections. Now, we visit SectionsCommands again to fix
850 for (BaseCommand *base : sec->sectionCommands) {
851 // This handles the assignments to symbol or to the dot.
852 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
854 assignSymbol(cmd, true);
855 cmd->size = dot - cmd->addr;
859 // Handle BYTE(), SHORT(), LONG(), or QUAD().
860 if (auto *cmd = dyn_cast<ByteCommand>(base)) {
861 cmd->offset = dot - ctx->outSec->addr;
863 expandOutputSection(cmd->size);
867 // Handle a single input section description command.
868 // It calculates and assigns the offsets for each section and also
869 // updates the output section size.
870 for (InputSection *sec : cast<InputSectionDescription>(base)->sections)
875 static bool isDiscardable(OutputSection &sec) {
876 if (sec.name == "/DISCARD/")
879 // We do not remove empty sections that are explicitly
880 // assigned to any segment.
881 if (!sec.phdrs.empty())
884 // We do not want to remove OutputSections with expressions that reference
885 // symbols even if the OutputSection is empty. We want to ensure that the
886 // expressions can be evaluated and report an error if they cannot.
887 if (sec.expressionsUseSymbols)
890 // OutputSections may be referenced by name in ADDR and LOADADDR expressions,
891 // as an empty Section can has a valid VMA and LMA we keep the OutputSection
892 // to maintain the integrity of the other Expression.
893 if (sec.usedInExpression)
896 for (BaseCommand *base : sec.sectionCommands) {
897 if (auto cmd = dyn_cast<SymbolAssignment>(base))
898 // Don't create empty output sections just for unreferenced PROVIDE
900 if (cmd->name != "." && !cmd->sym)
903 if (!isa<InputSectionDescription>(*base))
909 void LinkerScript::adjustSectionsBeforeSorting() {
910 // If the output section contains only symbol assignments, create a
911 // corresponding output section. The issue is what to do with linker script
912 // like ".foo : { symbol = 42; }". One option would be to convert it to
913 // "symbol = 42;". That is, move the symbol out of the empty section
914 // description. That seems to be what bfd does for this simple case. The
915 // problem is that this is not completely general. bfd will give up and
916 // create a dummy section too if there is a ". = . + 1" inside the section
918 // Given that we want to create the section, we have to worry what impact
919 // it will have on the link. For example, if we just create a section with
920 // 0 for flags, it would change which PT_LOADs are created.
921 // We could remember that particular section is dummy and ignore it in
922 // other parts of the linker, but unfortunately there are quite a few places
923 // that would need to change:
924 // * The program header creation.
925 // * The orphan section placement.
926 // * The address assignment.
927 // The other option is to pick flags that minimize the impact the section
928 // will have on the rest of the linker. That is why we copy the flags from
929 // the previous sections. Only a few flags are needed to keep the impact low.
930 uint64_t flags = SHF_ALLOC;
932 for (BaseCommand *&cmd : sectionCommands) {
933 auto *sec = dyn_cast<OutputSection>(cmd);
937 // Handle align (e.g. ".foo : ALIGN(16) { ... }").
940 std::max<uint32_t>(sec->alignment, sec->alignExpr().getValue());
942 // The input section might have been removed (if it was an empty synthetic
943 // section), but we at least know the flags.
944 if (sec->hasInputSections)
947 // We do not want to keep any special flags for output section
948 // in case it is empty.
949 bool isEmpty = getInputSections(sec).empty();
951 sec->flags = flags & ((sec->nonAlloc ? 0 : (uint64_t)SHF_ALLOC) |
952 SHF_WRITE | SHF_EXECINSTR);
954 if (isEmpty && isDiscardable(*sec)) {
960 // It is common practice to use very generic linker scripts. So for any
961 // given run some of the output sections in the script will be empty.
962 // We could create corresponding empty output sections, but that would
963 // clutter the output.
964 // We instead remove trivially empty sections. The bfd linker seems even
965 // more aggressive at removing them.
966 llvm::erase_if(sectionCommands, [&](BaseCommand *base) { return !base; });
969 void LinkerScript::adjustSectionsAfterSorting() {
970 // Try and find an appropriate memory region to assign offsets in.
971 for (BaseCommand *base : sectionCommands) {
972 if (auto *sec = dyn_cast<OutputSection>(base)) {
973 if (!sec->lmaRegionName.empty()) {
974 if (MemoryRegion *m = memoryRegions.lookup(sec->lmaRegionName))
977 error("memory region '" + sec->lmaRegionName + "' not declared");
979 sec->memRegion = findMemoryRegion(sec);
983 // If output section command doesn't specify any segments,
984 // and we haven't previously assigned any section to segment,
985 // then we simply assign section to the very first load segment.
986 // Below is an example of such linker script:
987 // PHDRS { seg PT_LOAD; }
988 // SECTIONS { .aaa : { *(.aaa) } }
989 std::vector<StringRef> defPhdrs;
990 auto firstPtLoad = llvm::find_if(phdrsCommands, [](const PhdrsCommand &cmd) {
991 return cmd.type == PT_LOAD;
993 if (firstPtLoad != phdrsCommands.end())
994 defPhdrs.push_back(firstPtLoad->name);
996 // Walk the commands and propagate the program headers to commands that don't
997 // explicitly specify them.
998 for (BaseCommand *base : sectionCommands) {
999 auto *sec = dyn_cast<OutputSection>(base);
1003 if (sec->phdrs.empty()) {
1004 // To match the bfd linker script behaviour, only propagate program
1005 // headers to sections that are allocated.
1006 if (sec->flags & SHF_ALLOC)
1007 sec->phdrs = defPhdrs;
1009 defPhdrs = sec->phdrs;
1014 static uint64_t computeBase(uint64_t min, bool allocateHeaders) {
1015 // If there is no SECTIONS or if the linkerscript is explicit about program
1016 // headers, do our best to allocate them.
1017 if (!script->hasSectionsCommand || allocateHeaders)
1019 // Otherwise only allocate program headers if that would not add a page.
1020 return alignDown(min, config->maxPageSize);
1023 // When the SECTIONS command is used, try to find an address for the file and
1024 // program headers output sections, which can be added to the first PT_LOAD
1025 // segment when program headers are created.
1027 // We check if the headers fit below the first allocated section. If there isn't
1028 // enough space for these sections, we'll remove them from the PT_LOAD segment,
1029 // and we'll also remove the PT_PHDR segment.
1030 void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &phdrs) {
1031 uint64_t min = std::numeric_limits<uint64_t>::max();
1032 for (OutputSection *sec : outputSections)
1033 if (sec->flags & SHF_ALLOC)
1034 min = std::min<uint64_t>(min, sec->addr);
1036 auto it = llvm::find_if(
1037 phdrs, [](const PhdrEntry *e) { return e->p_type == PT_LOAD; });
1038 if (it == phdrs.end())
1040 PhdrEntry *firstPTLoad = *it;
1042 bool hasExplicitHeaders =
1043 llvm::any_of(phdrsCommands, [](const PhdrsCommand &cmd) {
1044 return cmd.hasPhdrs || cmd.hasFilehdr;
1046 bool paged = !config->omagic && !config->nmagic;
1047 uint64_t headerSize = getHeaderSize();
1048 if ((paged || hasExplicitHeaders) &&
1049 headerSize <= min - computeBase(min, hasExplicitHeaders)) {
1050 min = alignDown(min - headerSize, config->maxPageSize);
1051 Out::elfHeader->addr = min;
1052 Out::programHeaders->addr = min + Out::elfHeader->size;
1056 // Error if we were explicitly asked to allocate headers.
1057 if (hasExplicitHeaders)
1058 error("could not allocate headers");
1060 Out::elfHeader->ptLoad = nullptr;
1061 Out::programHeaders->ptLoad = nullptr;
1062 firstPTLoad->firstSec = findFirstSection(firstPTLoad);
1064 llvm::erase_if(phdrs,
1065 [](const PhdrEntry *e) { return e->p_type == PT_PHDR; });
1068 LinkerScript::AddressState::AddressState() {
1069 for (auto &mri : script->memoryRegions) {
1070 MemoryRegion *mr = mri.second;
1071 mr->curPos = mr->origin;
1075 // Here we assign addresses as instructed by linker script SECTIONS
1076 // sub-commands. Doing that allows us to use final VA values, so here
1077 // we also handle rest commands like symbol assignments and ASSERTs.
1078 // Returns a symbol that has changed its section or value, or nullptr if no
1079 // symbol has changed.
1080 const Defined *LinkerScript::assignAddresses() {
1081 if (script->hasSectionsCommand) {
1082 // With a linker script, assignment of addresses to headers is covered by
1083 // allocateHeaders().
1084 dot = config->imageBase.getValueOr(0);
1086 // Assign addresses to headers right now.
1087 dot = target->getImageBase();
1088 Out::elfHeader->addr = dot;
1089 Out::programHeaders->addr = dot + Out::elfHeader->size;
1090 dot += getHeaderSize();
1093 auto deleter = std::make_unique<AddressState>();
1094 ctx = deleter.get();
1095 errorOnMissingSection = true;
1098 SymbolAssignmentMap oldValues = getSymbolAssignmentValues(sectionCommands);
1099 for (BaseCommand *base : sectionCommands) {
1100 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
1102 assignSymbol(cmd, false);
1103 cmd->size = dot - cmd->addr;
1106 assignOffsets(cast<OutputSection>(base));
1110 return getChangedSymbolAssignment(oldValues);
1113 // Creates program headers as instructed by PHDRS linker script command.
1114 std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
1115 std::vector<PhdrEntry *> ret;
1117 // Process PHDRS and FILEHDR keywords because they are not
1118 // real output sections and cannot be added in the following loop.
1119 for (const PhdrsCommand &cmd : phdrsCommands) {
1120 PhdrEntry *phdr = make<PhdrEntry>(cmd.type, cmd.flags ? *cmd.flags : PF_R);
1123 phdr->add(Out::elfHeader);
1125 phdr->add(Out::programHeaders);
1128 phdr->p_paddr = cmd.lmaExpr().getValue();
1129 phdr->hasLMA = true;
1131 ret.push_back(phdr);
1134 // Add output sections to program headers.
1135 for (OutputSection *sec : outputSections) {
1136 // Assign headers specified by linker script
1137 for (size_t id : getPhdrIndices(sec)) {
1139 if (!phdrsCommands[id].flags.hasValue())
1140 ret[id]->p_flags |= sec->getPhdrFlags();
1146 // Returns true if we should emit an .interp section.
1148 // We usually do. But if PHDRS commands are given, and
1149 // no PT_INTERP is there, there's no place to emit an
1150 // .interp, so we don't do that in that case.
1151 bool LinkerScript::needsInterpSection() {
1152 if (phdrsCommands.empty())
1154 for (PhdrsCommand &cmd : phdrsCommands)
1155 if (cmd.type == PT_INTERP)
1160 ExprValue LinkerScript::getSymbolValue(StringRef name, const Twine &loc) {
1163 return {ctx->outSec, false, dot - ctx->outSec->addr, loc};
1164 error(loc + ": unable to get location counter value");
1168 if (Symbol *sym = symtab->find(name)) {
1169 if (auto *ds = dyn_cast<Defined>(sym))
1170 return {ds->section, false, ds->value, loc};
1171 if (isa<SharedSymbol>(sym))
1172 if (!errorOnMissingSection)
1173 return {nullptr, false, 0, loc};
1176 error(loc + ": symbol not found: " + name);
1180 // Returns the index of the segment named Name.
1181 static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> vec,
1183 for (size_t i = 0; i < vec.size(); ++i)
1184 if (vec[i].name == name)
1189 // Returns indices of ELF headers containing specific section. Each index is a
1190 // zero based number of ELF header listed within PHDRS {} script block.
1191 std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *cmd) {
1192 std::vector<size_t> ret;
1194 for (StringRef s : cmd->phdrs) {
1195 if (Optional<size_t> idx = getPhdrIndex(phdrsCommands, s))
1196 ret.push_back(*idx);
1197 else if (s != "NONE")
1198 error(cmd->location + ": section header '" + s +
1199 "' is not listed in PHDRS");