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 "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/BinaryFormat/ELF.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/Endian.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/Parallel.h"
32 #include "llvm/Support/Path.h"
43 using namespace llvm::ELF;
44 using namespace llvm::object;
45 using namespace llvm::support::endian;
47 using namespace lld::elf;
49 LinkerScript *elf::script;
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 = std::string(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)().getValue();
107 uint64_t length = (memRegion->length)().getValue();
108 if (newSize > length)
109 error("section '" + secName + "' will not fit in region '" + regionName +
110 "': overflowed by " + Twine(newSize - length) + " bytes");
113 void LinkerScript::expandMemoryRegions(uint64_t size) {
115 expandMemoryRegion(ctx->memRegion, size, ctx->memRegion->name,
117 // Only expand the LMARegion if it is different from memRegion.
118 if (ctx->lmaRegion && ctx->memRegion != ctx->lmaRegion)
119 expandMemoryRegion(ctx->lmaRegion, size, ctx->lmaRegion->name,
123 void LinkerScript::expandOutputSection(uint64_t size) {
124 ctx->outSec->size += size;
125 expandMemoryRegions(size);
128 void LinkerScript::setDot(Expr e, const Twine &loc, bool inSec) {
129 uint64_t val = e().getValue();
130 if (val < dot && inSec)
131 error(loc + ": unable to move location counter backward for: " +
134 // Update to location counter means update to section size.
136 expandOutputSection(val - dot);
141 // Used for handling linker symbol assignments, for both finalizing
142 // their values and doing early declarations. Returns true if symbol
143 // should be defined from linker script.
144 static bool shouldDefineSym(SymbolAssignment *cmd) {
145 if (cmd->name == ".")
151 // If a symbol was in PROVIDE(), we need to define it only
152 // when it is a referenced undefined symbol.
153 Symbol *b = symtab->find(cmd->name);
154 if (b && !b->isDefined())
159 // Called by processSymbolAssignments() to assign definitions to
160 // linker-script-defined symbols.
161 void LinkerScript::addSymbol(SymbolAssignment *cmd) {
162 if (!shouldDefineSym(cmd))
166 ExprValue value = cmd->expression();
167 SectionBase *sec = value.isAbsolute() ? nullptr : value.sec;
168 uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
170 // When this function is called, section addresses have not been
171 // fixed yet. So, we may or may not know the value of the RHS
174 // For example, if an expression is `x = 42`, we know x is always 42.
175 // However, if an expression is `x = .`, there's no way to know its
176 // value at the moment.
178 // We want to set symbol values early if we can. This allows us to
179 // use symbols as variables in linker scripts. Doing so allows us to
180 // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
181 uint64_t symValue = value.sec ? 0 : value.getValue();
183 Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, value.type,
186 Symbol *sym = symtab->insert(cmd->name);
187 sym->mergeProperties(newSym);
188 sym->replace(newSym);
189 cmd->sym = cast<Defined>(sym);
192 // This function is called from LinkerScript::declareSymbols.
193 // It creates a placeholder symbol if needed.
194 static void declareSymbol(SymbolAssignment *cmd) {
195 if (!shouldDefineSym(cmd))
198 uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
199 Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE, 0, 0,
202 // We can't calculate final value right now.
203 Symbol *sym = symtab->insert(cmd->name);
204 sym->mergeProperties(newSym);
205 sym->replace(newSym);
207 cmd->sym = cast<Defined>(sym);
208 cmd->provide = false;
209 sym->scriptDefined = true;
212 using SymbolAssignmentMap =
213 DenseMap<const Defined *, std::pair<SectionBase *, uint64_t>>;
215 // Collect section/value pairs of linker-script-defined symbols. This is used to
216 // check whether symbol values converge.
217 static SymbolAssignmentMap
218 getSymbolAssignmentValues(const std::vector<BaseCommand *> §ionCommands) {
219 SymbolAssignmentMap ret;
220 for (BaseCommand *base : sectionCommands) {
221 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
222 if (cmd->sym) // sym is nullptr for dot.
223 ret.try_emplace(cmd->sym,
224 std::make_pair(cmd->sym->section, cmd->sym->value));
227 for (BaseCommand *sub_base : cast<OutputSection>(base)->sectionCommands)
228 if (auto *cmd = dyn_cast<SymbolAssignment>(sub_base))
230 ret.try_emplace(cmd->sym,
231 std::make_pair(cmd->sym->section, cmd->sym->value));
236 // Returns the lexicographical smallest (for determinism) Defined whose
237 // section/value has changed.
238 static const Defined *
239 getChangedSymbolAssignment(const SymbolAssignmentMap &oldValues) {
240 const Defined *changed = nullptr;
241 for (auto &it : oldValues) {
242 const Defined *sym = it.first;
243 if (std::make_pair(sym->section, sym->value) != it.second &&
244 (!changed || sym->getName() < changed->getName()))
250 // Process INSERT [AFTER|BEFORE] commands. For each command, we move the
251 // specified output section to the designated place.
252 void LinkerScript::processInsertCommands() {
253 for (const InsertCommand &cmd : insertCommands) {
254 // If cmd.os is empty, it may have been discarded by
255 // adjustSectionsBeforeSorting(). We do not handle such output sections.
256 auto from = llvm::find(sectionCommands, cmd.os);
257 if (from == sectionCommands.end())
259 sectionCommands.erase(from);
261 auto insertPos = llvm::find_if(sectionCommands, [&cmd](BaseCommand *base) {
262 auto *to = dyn_cast<OutputSection>(base);
263 return to != nullptr && to->name == cmd.where;
265 if (insertPos == sectionCommands.end()) {
266 error("unable to insert " + cmd.os->name +
267 (cmd.isAfter ? " after " : " before ") + cmd.where);
271 sectionCommands.insert(insertPos, cmd.os);
276 // Symbols defined in script should not be inlined by LTO. At the same time
277 // we don't know their final values until late stages of link. Here we scan
278 // over symbol assignment commands and create placeholder symbols if needed.
279 void LinkerScript::declareSymbols() {
281 for (BaseCommand *base : sectionCommands) {
282 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
287 // If the output section directive has constraints,
288 // we can't say for sure if it is going to be included or not.
289 // Skip such sections for now. Improve the checks if we ever
290 // need symbols from that sections to be declared early.
291 auto *sec = cast<OutputSection>(base);
292 if (sec->constraint != ConstraintKind::NoConstraint)
294 for (BaseCommand *base2 : sec->sectionCommands)
295 if (auto *cmd = dyn_cast<SymbolAssignment>(base2))
300 // This function is called from assignAddresses, while we are
301 // fixing the output section addresses. This function is supposed
302 // to set the final value for a given symbol assignment.
303 void LinkerScript::assignSymbol(SymbolAssignment *cmd, bool inSec) {
304 if (cmd->name == ".") {
305 setDot(cmd->expression, cmd->location, inSec);
312 ExprValue v = cmd->expression();
313 if (v.isAbsolute()) {
314 cmd->sym->section = nullptr;
315 cmd->sym->value = v.getValue();
317 cmd->sym->section = v.sec;
318 cmd->sym->value = v.getSectionOffset();
320 cmd->sym->type = v.type;
323 static std::string getFilename(InputFile *file) {
326 if (file->archiveName.empty())
327 return std::string(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) &&
339 (s->flags & id->withFlags) == id->withFlags &&
340 (s->flags & id->withoutFlags) == 0)
345 // A helper function for the SORT() command.
346 static bool matchConstraints(ArrayRef<InputSectionBase *> sections,
347 ConstraintKind kind) {
348 if (kind == ConstraintKind::NoConstraint)
351 bool isRW = llvm::any_of(
352 sections, [](InputSectionBase *sec) { return sec->flags & SHF_WRITE; });
354 return (isRW && kind == ConstraintKind::ReadWrite) ||
355 (!isRW && kind == ConstraintKind::ReadOnly);
358 static void sortSections(MutableArrayRef<InputSectionBase *> vec,
359 SortSectionPolicy k) {
360 auto alignmentComparator = [](InputSectionBase *a, InputSectionBase *b) {
361 // ">" is not a mistake. Sections with larger alignments are placed
362 // before sections with smaller alignments in order to reduce the
363 // amount of padding necessary. This is compatible with GNU.
364 return a->alignment > b->alignment;
366 auto nameComparator = [](InputSectionBase *a, InputSectionBase *b) {
367 return a->name < b->name;
369 auto priorityComparator = [](InputSectionBase *a, InputSectionBase *b) {
370 return getPriority(a->name) < getPriority(b->name);
374 case SortSectionPolicy::Default:
375 case SortSectionPolicy::None:
377 case SortSectionPolicy::Alignment:
378 return llvm::stable_sort(vec, alignmentComparator);
379 case SortSectionPolicy::Name:
380 return llvm::stable_sort(vec, nameComparator);
381 case SortSectionPolicy::Priority:
382 return llvm::stable_sort(vec, priorityComparator);
386 // Sort sections as instructed by SORT-family commands and --sort-section
387 // option. Because SORT-family commands can be nested at most two depth
388 // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
389 // line option is respected even if a SORT command is given, the exact
390 // behavior we have here is a bit complicated. Here are the rules.
392 // 1. If two SORT commands are given, --sort-section is ignored.
393 // 2. If one SORT command is given, and if it is not SORT_NONE,
394 // --sort-section is handled as an inner SORT command.
395 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
396 // 4. If no SORT command is given, sort according to --sort-section.
397 static void sortInputSections(MutableArrayRef<InputSectionBase *> vec,
398 const SectionPattern &pat) {
399 if (pat.sortOuter == SortSectionPolicy::None)
402 if (pat.sortInner == SortSectionPolicy::Default)
403 sortSections(vec, config->sortSection);
405 sortSections(vec, pat.sortInner);
406 sortSections(vec, pat.sortOuter);
409 // Compute and remember which sections the InputSectionDescription matches.
410 std::vector<InputSectionBase *>
411 LinkerScript::computeInputSections(const InputSectionDescription *cmd,
412 ArrayRef<InputSectionBase *> sections) {
413 std::vector<InputSectionBase *> ret;
415 // Collects all sections that satisfy constraints of Cmd.
416 for (const SectionPattern &pat : cmd->sectionPatterns) {
417 size_t sizeBefore = ret.size();
419 for (InputSectionBase *sec : sections) {
420 if (!sec->isLive() || sec->parent)
423 // For -emit-relocs we have to ignore entries like
424 // .rela.dyn : { *(.rela.data) }
425 // which are common because they are in the default bfd script.
426 // We do not ignore SHT_REL[A] linker-synthesized sections here because
427 // want to support scripts that do custom layout for them.
428 if (isa<InputSection>(sec) &&
429 cast<InputSection>(sec)->getRelocatedSection())
432 // Check the name early to improve performance in the common case.
433 if (!pat.sectionPat.match(sec->name))
436 std::string filename = getFilename(sec->file);
437 if (!cmd->filePat.match(filename) ||
438 pat.excludedFilePat.match(filename) ||
439 (sec->flags & cmd->withFlags) != cmd->withFlags ||
440 (sec->flags & cmd->withoutFlags) != 0)
447 MutableArrayRef<InputSectionBase *>(ret).slice(sizeBefore), pat);
452 void LinkerScript::discard(InputSectionBase *s) {
453 if (s == in.shStrTab || s == mainPart->relrDyn)
454 error("discarding " + s->name + " section is not allowed");
456 // You can discard .hash and .gnu.hash sections by linker scripts. Since
457 // they are synthesized sections, we need to handle them differently than
458 // other regular sections.
459 if (s == mainPart->gnuHashTab)
460 mainPart->gnuHashTab = nullptr;
461 if (s == mainPart->hashTab)
462 mainPart->hashTab = nullptr;
466 for (InputSection *ds : s->dependentSections)
470 void LinkerScript::discardSynthetic(OutputSection &outCmd) {
471 for (Partition &part : partitions) {
472 if (!part.armExidx || !part.armExidx->isLive())
474 std::vector<InputSectionBase *> secs(part.armExidx->exidxSections.begin(),
475 part.armExidx->exidxSections.end());
476 for (BaseCommand *base : outCmd.sectionCommands)
477 if (auto *cmd = dyn_cast<InputSectionDescription>(base)) {
478 std::vector<InputSectionBase *> matches =
479 computeInputSections(cmd, secs);
480 for (InputSectionBase *s : matches)
486 std::vector<InputSectionBase *>
487 LinkerScript::createInputSectionList(OutputSection &outCmd) {
488 std::vector<InputSectionBase *> ret;
490 for (BaseCommand *base : outCmd.sectionCommands) {
491 if (auto *cmd = dyn_cast<InputSectionDescription>(base)) {
492 cmd->sectionBases = computeInputSections(cmd, inputSections);
493 for (InputSectionBase *s : cmd->sectionBases)
495 ret.insert(ret.end(), cmd->sectionBases.begin(), cmd->sectionBases.end());
501 // Create output sections described by SECTIONS commands.
502 void LinkerScript::processSectionCommands() {
504 for (BaseCommand *base : sectionCommands) {
505 if (auto *sec = dyn_cast<OutputSection>(base)) {
506 std::vector<InputSectionBase *> v = createInputSectionList(*sec);
508 // The output section name `/DISCARD/' is special.
509 // Any input section assigned to it is discarded.
510 if (sec->name == "/DISCARD/") {
511 for (InputSectionBase *s : v)
513 discardSynthetic(*sec);
514 sec->sectionCommands.clear();
518 // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
519 // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
520 // sections satisfy a given constraint. If not, a directive is handled
521 // as if it wasn't present from the beginning.
523 // Because we'll iterate over SectionCommands many more times, the easy
524 // way to "make it as if it wasn't present" is to make it empty.
525 if (!matchConstraints(v, sec->constraint)) {
526 for (InputSectionBase *s : v)
528 sec->sectionCommands.clear();
532 // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
533 // is given, input sections are aligned to that value, whether the
534 // given value is larger or smaller than the original section alignment.
535 if (sec->subalignExpr) {
536 uint32_t subalign = sec->subalignExpr().getValue();
537 for (InputSectionBase *s : v)
538 s->alignment = subalign;
541 // Set the partition field the same way OutputSection::recordSection()
542 // does. Partitions cannot be used with the SECTIONS command, so this is
546 sec->sectionIndex = i++;
551 void LinkerScript::processSymbolAssignments() {
552 // Dot outside an output section still represents a relative address, whose
553 // sh_shndx should not be SHN_UNDEF or SHN_ABS. Create a dummy aether section
554 // that fills the void outside a section. It has an index of one, which is
555 // indistinguishable from any other regular section index.
556 aether = make<OutputSection>("", 0, SHF_ALLOC);
557 aether->sectionIndex = 1;
559 // ctx captures the local AddressState and makes it accessible deliberately.
560 // This is needed as there are some cases where we cannot just thread the
561 // current state through to a lambda function created by the script parser.
564 ctx->outSec = aether;
566 for (BaseCommand *base : sectionCommands) {
567 if (auto *cmd = dyn_cast<SymbolAssignment>(base))
570 for (BaseCommand *sub_base : cast<OutputSection>(base)->sectionCommands)
571 if (auto *cmd = dyn_cast<SymbolAssignment>(sub_base))
578 static OutputSection *findByName(ArrayRef<BaseCommand *> vec,
580 for (BaseCommand *base : vec)
581 if (auto *sec = dyn_cast<OutputSection>(base))
582 if (sec->name == name)
587 static OutputSection *createSection(InputSectionBase *isec,
588 StringRef outsecName) {
589 OutputSection *sec = script->createOutputSection(outsecName, "<internal>");
590 sec->recordSection(isec);
594 static OutputSection *
595 addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
596 InputSectionBase *isec, StringRef outsecName) {
597 // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
598 // option is given. A section with SHT_GROUP defines a "section group", and
599 // its members have SHF_GROUP attribute. Usually these flags have already been
600 // stripped by InputFiles.cpp as section groups are processed and uniquified.
601 // However, for the -r option, we want to pass through all section groups
602 // as-is because adding/removing members or merging them with other groups
603 // change their semantics.
604 if (isec->type == SHT_GROUP || (isec->flags & SHF_GROUP))
605 return createSection(isec, outsecName);
607 // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
608 // relocation sections .rela.foo and .rela.bar for example. Most tools do
609 // not allow multiple REL[A] sections for output section. Hence we
610 // should combine these relocation sections into single output.
611 // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
612 // other REL[A] sections created by linker itself.
613 if (!isa<SyntheticSection>(isec) &&
614 (isec->type == SHT_REL || isec->type == SHT_RELA)) {
615 auto *sec = cast<InputSection>(isec);
616 OutputSection *out = sec->getRelocatedSection()->getOutputSection();
618 if (out->relocationSection) {
619 out->relocationSection->recordSection(sec);
623 out->relocationSection = createSection(isec, outsecName);
624 return out->relocationSection;
627 // The ELF spec just says
628 // ----------------------------------------------------------------
629 // In the first phase, input sections that match in name, type and
630 // attribute flags should be concatenated into single sections.
631 // ----------------------------------------------------------------
633 // However, it is clear that at least some flags have to be ignored for
634 // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
635 // ignored. We should not have two output .text sections just because one was
636 // in a group and another was not for example.
638 // It also seems that wording was a late addition and didn't get the
639 // necessary scrutiny.
641 // Merging sections with different flags is expected by some users. One
642 // reason is that if one file has
644 // int *const bar __attribute__((section(".foo"))) = (int *)0;
646 // gcc with -fPIC will produce a read only .foo section. But if another
650 // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
652 // gcc with -fPIC will produce a read write section.
654 // Last but not least, when using linker script the merge rules are forced by
655 // the script. Unfortunately, linker scripts are name based. This means that
656 // expressions like *(.foo*) can refer to multiple input sections with
657 // different flags. We cannot put them in different output sections or we
658 // would produce wrong results for
660 // start = .; *(.foo.*) end = .; *(.bar)
662 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
663 // another. The problem is that there is no way to layout those output
664 // sections such that the .foo sections are the only thing between the start
667 // Given the above issues, we instead merge sections by name and error on
668 // incompatible types and flags.
669 TinyPtrVector<OutputSection *> &v = map[outsecName];
670 for (OutputSection *sec : v) {
671 if (sec->partition != isec->partition)
674 if (config->relocatable && (isec->flags & SHF_LINK_ORDER)) {
675 // Merging two SHF_LINK_ORDER sections with different sh_link fields will
676 // change their semantics, so we only merge them in -r links if they will
677 // end up being linked to the same output section. The casts are fine
678 // because everything in the map was created by the orphan placement code.
679 auto *firstIsec = cast<InputSectionBase>(
680 cast<InputSectionDescription>(sec->sectionCommands[0])
682 if (firstIsec->getLinkOrderDep()->getOutputSection() !=
683 isec->getLinkOrderDep()->getOutputSection())
687 sec->recordSection(isec);
691 OutputSection *sec = createSection(isec, outsecName);
696 // Add sections that didn't match any sections command.
697 void LinkerScript::addOrphanSections() {
698 StringMap<TinyPtrVector<OutputSection *>> map;
699 std::vector<OutputSection *> v;
701 std::function<void(InputSectionBase *)> add;
702 add = [&](InputSectionBase *s) {
703 if (s->isLive() && !s->parent) {
704 orphanSections.push_back(s);
706 StringRef name = getOutputSectionName(s);
707 if (config->unique) {
708 v.push_back(createSection(s, name));
709 } else if (OutputSection *sec = findByName(sectionCommands, name)) {
710 sec->recordSection(s);
712 if (OutputSection *os = addInputSec(map, s, name))
714 assert(isa<MergeInputSection>(s) ||
715 s->getOutputSection()->sectionIndex == UINT32_MAX);
719 if (config->relocatable)
720 for (InputSectionBase *depSec : s->dependentSections)
721 if (depSec->flags & SHF_LINK_ORDER)
725 // For futher --emit-reloc handling code we need target output section
726 // to be created before we create relocation output section, so we want
727 // to create target sections first. We do not want priority handling
728 // for synthetic sections because them are special.
729 for (InputSectionBase *isec : inputSections) {
730 // In -r links, SHF_LINK_ORDER sections are added while adding their parent
731 // sections because we need to know the parent's output section before we
732 // can select an output section for the SHF_LINK_ORDER section.
733 if (config->relocatable && (isec->flags & SHF_LINK_ORDER))
736 if (auto *sec = dyn_cast<InputSection>(isec))
737 if (InputSectionBase *rel = sec->getRelocatedSection())
738 if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
743 // If no SECTIONS command was given, we should insert sections commands
744 // before others, so that we can handle scripts which refers them,
745 // for example: "foo = ABSOLUTE(ADDR(.text)));".
746 // When SECTIONS command is present we just add all orphans to the end.
747 if (hasSectionsCommand)
748 sectionCommands.insert(sectionCommands.end(), v.begin(), v.end());
750 sectionCommands.insert(sectionCommands.begin(), v.begin(), v.end());
753 void LinkerScript::diagnoseOrphanHandling() const {
754 for (const InputSectionBase *sec : orphanSections) {
755 // Input SHT_REL[A] retained by --emit-relocs are ignored by
756 // computeInputSections(). Don't warn/error.
757 if (isa<InputSection>(sec) &&
758 cast<InputSection>(sec)->getRelocatedSection())
761 StringRef name = getOutputSectionName(sec);
762 if (config->orphanHandling == OrphanHandlingPolicy::Error)
763 error(toString(sec) + " is being placed in '" + name + "'");
764 else if (config->orphanHandling == OrphanHandlingPolicy::Warn)
765 warn(toString(sec) + " is being placed in '" + name + "'");
769 uint64_t LinkerScript::advance(uint64_t size, unsigned alignment) {
771 (ctx->outSec->flags & SHF_TLS) && ctx->outSec->type == SHT_NOBITS;
772 uint64_t start = isTbss ? dot + ctx->threadBssOffset : dot;
773 start = alignTo(start, alignment);
774 uint64_t end = start + size;
777 ctx->threadBssOffset = end - dot;
783 void LinkerScript::output(InputSection *s) {
784 assert(ctx->outSec == s->getParent());
785 uint64_t before = advance(0, 1);
786 uint64_t pos = advance(s->getSize(), s->alignment);
787 s->outSecOff = pos - s->getSize() - ctx->outSec->addr;
789 // Update output section size after adding each section. This is so that
790 // SIZEOF works correctly in the case below:
791 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
792 expandOutputSection(pos - before);
795 void LinkerScript::switchTo(OutputSection *sec) {
798 uint64_t pos = advance(0, 1);
799 if (sec->addrExpr && script->hasSectionsCommand) {
800 // The alignment is ignored.
801 ctx->outSec->addr = pos;
803 // ctx->outSec->alignment is the max of ALIGN and the maximum of input
804 // section alignments.
805 ctx->outSec->addr = advance(0, ctx->outSec->alignment);
806 expandMemoryRegions(ctx->outSec->addr - pos);
810 // This function searches for a memory region to place the given output
811 // section in. If found, a pointer to the appropriate memory region is
812 // returned. Otherwise, a nullptr is returned.
813 MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *sec) {
814 // If a memory region name was specified in the output section command,
815 // then try to find that region first.
816 if (!sec->memoryRegionName.empty()) {
817 if (MemoryRegion *m = memoryRegions.lookup(sec->memoryRegionName))
819 error("memory region '" + sec->memoryRegionName + "' not declared");
823 // If at least one memory region is defined, all sections must
824 // belong to some memory region. Otherwise, we don't need to do
825 // anything for memory regions.
826 if (memoryRegions.empty())
829 // See if a region can be found by matching section flags.
830 for (auto &pair : memoryRegions) {
831 MemoryRegion *m = pair.second;
832 if ((m->flags & sec->flags) && (m->negFlags & sec->flags) == 0)
836 // Otherwise, no suitable region was found.
837 if (sec->flags & SHF_ALLOC)
838 error("no memory region specified for section '" + sec->name + "'");
842 static OutputSection *findFirstSection(PhdrEntry *load) {
843 for (OutputSection *sec : outputSections)
844 if (sec->ptLoad == load)
849 // This function assigns offsets to input sections and an output section
850 // for a single sections command (e.g. ".text { *(.text); }").
851 void LinkerScript::assignOffsets(OutputSection *sec) {
852 if (!(sec->flags & SHF_ALLOC))
855 const bool sameMemRegion = ctx->memRegion == sec->memRegion;
856 const bool prevLMARegionIsDefault = ctx->lmaRegion == nullptr;
857 ctx->memRegion = sec->memRegion;
858 ctx->lmaRegion = sec->lmaRegion;
860 dot = ctx->memRegion->curPos;
862 if ((sec->flags & SHF_ALLOC) && sec->addrExpr)
863 setDot(sec->addrExpr, sec->location, false);
865 // If the address of the section has been moved forward by an explicit
866 // expression so that it now starts past the current curPos of the enclosing
867 // region, we need to expand the current region to account for the space
868 // between the previous section, if any, and the start of this section.
869 if (ctx->memRegion && ctx->memRegion->curPos < dot)
870 expandMemoryRegion(ctx->memRegion, dot - ctx->memRegion->curPos,
871 ctx->memRegion->name, sec->name);
875 // ctx->lmaOffset is LMA minus VMA. If LMA is explicitly specified via AT() or
876 // AT>, recompute ctx->lmaOffset; otherwise, if both previous/current LMA
877 // region is the default, and the two sections are in the same memory region,
878 // reuse previous lmaOffset; otherwise, reset lmaOffset to 0. This emulates
879 // heuristics described in
880 // https://sourceware.org/binutils/docs/ld/Output-Section-LMA.html
882 ctx->lmaOffset = sec->lmaExpr().getValue() - dot;
883 else if (MemoryRegion *mr = sec->lmaRegion)
884 ctx->lmaOffset = alignTo(mr->curPos, sec->alignment) - dot;
885 else if (!sameMemRegion || !prevLMARegionIsDefault)
888 // Propagate ctx->lmaOffset to the first "non-header" section.
889 if (PhdrEntry *l = ctx->outSec->ptLoad)
890 if (sec == findFirstSection(l))
891 l->lmaOffset = ctx->lmaOffset;
893 // We can call this method multiple times during the creation of
894 // thunks and want to start over calculation each time.
897 // We visited SectionsCommands from processSectionCommands to
898 // layout sections. Now, we visit SectionsCommands again to fix
900 for (BaseCommand *base : sec->sectionCommands) {
901 // This handles the assignments to symbol or to the dot.
902 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
904 assignSymbol(cmd, true);
905 cmd->size = dot - cmd->addr;
909 // Handle BYTE(), SHORT(), LONG(), or QUAD().
910 if (auto *cmd = dyn_cast<ByteCommand>(base)) {
911 cmd->offset = dot - ctx->outSec->addr;
913 expandOutputSection(cmd->size);
917 // Handle a single input section description command.
918 // It calculates and assigns the offsets for each section and also
919 // updates the output section size.
920 for (InputSection *sec : cast<InputSectionDescription>(base)->sections)
925 static bool isDiscardable(OutputSection &sec) {
926 if (sec.name == "/DISCARD/")
929 // We do not remove empty sections that are explicitly
930 // assigned to any segment.
931 if (!sec.phdrs.empty())
934 // We do not want to remove OutputSections with expressions that reference
935 // symbols even if the OutputSection is empty. We want to ensure that the
936 // expressions can be evaluated and report an error if they cannot.
937 if (sec.expressionsUseSymbols)
940 // OutputSections may be referenced by name in ADDR and LOADADDR expressions,
941 // as an empty Section can has a valid VMA and LMA we keep the OutputSection
942 // to maintain the integrity of the other Expression.
943 if (sec.usedInExpression)
946 for (BaseCommand *base : sec.sectionCommands) {
947 if (auto cmd = dyn_cast<SymbolAssignment>(base))
948 // Don't create empty output sections just for unreferenced PROVIDE
950 if (cmd->name != "." && !cmd->sym)
953 if (!isa<InputSectionDescription>(*base))
959 void LinkerScript::adjustSectionsBeforeSorting() {
960 // If the output section contains only symbol assignments, create a
961 // corresponding output section. The issue is what to do with linker script
962 // like ".foo : { symbol = 42; }". One option would be to convert it to
963 // "symbol = 42;". That is, move the symbol out of the empty section
964 // description. That seems to be what bfd does for this simple case. The
965 // problem is that this is not completely general. bfd will give up and
966 // create a dummy section too if there is a ". = . + 1" inside the section
968 // Given that we want to create the section, we have to worry what impact
969 // it will have on the link. For example, if we just create a section with
970 // 0 for flags, it would change which PT_LOADs are created.
971 // We could remember that particular section is dummy and ignore it in
972 // other parts of the linker, but unfortunately there are quite a few places
973 // that would need to change:
974 // * The program header creation.
975 // * The orphan section placement.
976 // * The address assignment.
977 // The other option is to pick flags that minimize the impact the section
978 // will have on the rest of the linker. That is why we copy the flags from
979 // the previous sections. Only a few flags are needed to keep the impact low.
980 uint64_t flags = SHF_ALLOC;
982 for (BaseCommand *&cmd : sectionCommands) {
983 auto *sec = dyn_cast<OutputSection>(cmd);
987 // Handle align (e.g. ".foo : ALIGN(16) { ... }").
990 std::max<uint32_t>(sec->alignment, sec->alignExpr().getValue());
992 // The input section might have been removed (if it was an empty synthetic
993 // section), but we at least know the flags.
994 if (sec->hasInputSections)
997 // We do not want to keep any special flags for output section
998 // in case it is empty.
999 bool isEmpty = (getFirstInputSection(sec) == nullptr);
1001 sec->flags = flags & ((sec->nonAlloc ? 0 : (uint64_t)SHF_ALLOC) |
1002 SHF_WRITE | SHF_EXECINSTR);
1004 if (isEmpty && isDiscardable(*sec)) {
1010 // It is common practice to use very generic linker scripts. So for any
1011 // given run some of the output sections in the script will be empty.
1012 // We could create corresponding empty output sections, but that would
1013 // clutter the output.
1014 // We instead remove trivially empty sections. The bfd linker seems even
1015 // more aggressive at removing them.
1016 llvm::erase_if(sectionCommands, [&](BaseCommand *base) { return !base; });
1019 void LinkerScript::adjustSectionsAfterSorting() {
1020 // Try and find an appropriate memory region to assign offsets in.
1021 for (BaseCommand *base : sectionCommands) {
1022 if (auto *sec = dyn_cast<OutputSection>(base)) {
1023 if (!sec->lmaRegionName.empty()) {
1024 if (MemoryRegion *m = memoryRegions.lookup(sec->lmaRegionName))
1027 error("memory region '" + sec->lmaRegionName + "' not declared");
1029 sec->memRegion = findMemoryRegion(sec);
1033 // If output section command doesn't specify any segments,
1034 // and we haven't previously assigned any section to segment,
1035 // then we simply assign section to the very first load segment.
1036 // Below is an example of such linker script:
1037 // PHDRS { seg PT_LOAD; }
1038 // SECTIONS { .aaa : { *(.aaa) } }
1039 std::vector<StringRef> defPhdrs;
1040 auto firstPtLoad = llvm::find_if(phdrsCommands, [](const PhdrsCommand &cmd) {
1041 return cmd.type == PT_LOAD;
1043 if (firstPtLoad != phdrsCommands.end())
1044 defPhdrs.push_back(firstPtLoad->name);
1046 // Walk the commands and propagate the program headers to commands that don't
1047 // explicitly specify them.
1048 for (BaseCommand *base : sectionCommands) {
1049 auto *sec = dyn_cast<OutputSection>(base);
1053 if (sec->phdrs.empty()) {
1054 // To match the bfd linker script behaviour, only propagate program
1055 // headers to sections that are allocated.
1056 if (sec->flags & SHF_ALLOC)
1057 sec->phdrs = defPhdrs;
1059 defPhdrs = sec->phdrs;
1064 static uint64_t computeBase(uint64_t min, bool allocateHeaders) {
1065 // If there is no SECTIONS or if the linkerscript is explicit about program
1066 // headers, do our best to allocate them.
1067 if (!script->hasSectionsCommand || allocateHeaders)
1069 // Otherwise only allocate program headers if that would not add a page.
1070 return alignDown(min, config->maxPageSize);
1073 // When the SECTIONS command is used, try to find an address for the file and
1074 // program headers output sections, which can be added to the first PT_LOAD
1075 // segment when program headers are created.
1077 // We check if the headers fit below the first allocated section. If there isn't
1078 // enough space for these sections, we'll remove them from the PT_LOAD segment,
1079 // and we'll also remove the PT_PHDR segment.
1080 void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &phdrs) {
1081 uint64_t min = std::numeric_limits<uint64_t>::max();
1082 for (OutputSection *sec : outputSections)
1083 if (sec->flags & SHF_ALLOC)
1084 min = std::min<uint64_t>(min, sec->addr);
1086 auto it = llvm::find_if(
1087 phdrs, [](const PhdrEntry *e) { return e->p_type == PT_LOAD; });
1088 if (it == phdrs.end())
1090 PhdrEntry *firstPTLoad = *it;
1092 bool hasExplicitHeaders =
1093 llvm::any_of(phdrsCommands, [](const PhdrsCommand &cmd) {
1094 return cmd.hasPhdrs || cmd.hasFilehdr;
1096 bool paged = !config->omagic && !config->nmagic;
1097 uint64_t headerSize = getHeaderSize();
1098 if ((paged || hasExplicitHeaders) &&
1099 headerSize <= min - computeBase(min, hasExplicitHeaders)) {
1100 min = alignDown(min - headerSize, config->maxPageSize);
1101 Out::elfHeader->addr = min;
1102 Out::programHeaders->addr = min + Out::elfHeader->size;
1106 // Error if we were explicitly asked to allocate headers.
1107 if (hasExplicitHeaders)
1108 error("could not allocate headers");
1110 Out::elfHeader->ptLoad = nullptr;
1111 Out::programHeaders->ptLoad = nullptr;
1112 firstPTLoad->firstSec = findFirstSection(firstPTLoad);
1114 llvm::erase_if(phdrs,
1115 [](const PhdrEntry *e) { return e->p_type == PT_PHDR; });
1118 LinkerScript::AddressState::AddressState() {
1119 for (auto &mri : script->memoryRegions) {
1120 MemoryRegion *mr = mri.second;
1121 mr->curPos = (mr->origin)().getValue();
1125 // Here we assign addresses as instructed by linker script SECTIONS
1126 // sub-commands. Doing that allows us to use final VA values, so here
1127 // we also handle rest commands like symbol assignments and ASSERTs.
1128 // Returns a symbol that has changed its section or value, or nullptr if no
1129 // symbol has changed.
1130 const Defined *LinkerScript::assignAddresses() {
1131 if (script->hasSectionsCommand) {
1132 // With a linker script, assignment of addresses to headers is covered by
1133 // allocateHeaders().
1134 dot = config->imageBase.getValueOr(0);
1136 // Assign addresses to headers right now.
1137 dot = target->getImageBase();
1138 Out::elfHeader->addr = dot;
1139 Out::programHeaders->addr = dot + Out::elfHeader->size;
1140 dot += getHeaderSize();
1143 auto deleter = std::make_unique<AddressState>();
1144 ctx = deleter.get();
1145 errorOnMissingSection = true;
1148 SymbolAssignmentMap oldValues = getSymbolAssignmentValues(sectionCommands);
1149 for (BaseCommand *base : sectionCommands) {
1150 if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
1152 assignSymbol(cmd, false);
1153 cmd->size = dot - cmd->addr;
1156 assignOffsets(cast<OutputSection>(base));
1160 return getChangedSymbolAssignment(oldValues);
1163 // Creates program headers as instructed by PHDRS linker script command.
1164 std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
1165 std::vector<PhdrEntry *> ret;
1167 // Process PHDRS and FILEHDR keywords because they are not
1168 // real output sections and cannot be added in the following loop.
1169 for (const PhdrsCommand &cmd : phdrsCommands) {
1170 PhdrEntry *phdr = make<PhdrEntry>(cmd.type, cmd.flags ? *cmd.flags : PF_R);
1173 phdr->add(Out::elfHeader);
1175 phdr->add(Out::programHeaders);
1178 phdr->p_paddr = cmd.lmaExpr().getValue();
1179 phdr->hasLMA = true;
1181 ret.push_back(phdr);
1184 // Add output sections to program headers.
1185 for (OutputSection *sec : outputSections) {
1186 // Assign headers specified by linker script
1187 for (size_t id : getPhdrIndices(sec)) {
1189 if (!phdrsCommands[id].flags.hasValue())
1190 ret[id]->p_flags |= sec->getPhdrFlags();
1196 // Returns true if we should emit an .interp section.
1198 // We usually do. But if PHDRS commands are given, and
1199 // no PT_INTERP is there, there's no place to emit an
1200 // .interp, so we don't do that in that case.
1201 bool LinkerScript::needsInterpSection() {
1202 if (phdrsCommands.empty())
1204 for (PhdrsCommand &cmd : phdrsCommands)
1205 if (cmd.type == PT_INTERP)
1210 ExprValue LinkerScript::getSymbolValue(StringRef name, const Twine &loc) {
1213 return {ctx->outSec, false, dot - ctx->outSec->addr, loc};
1214 error(loc + ": unable to get location counter value");
1218 if (Symbol *sym = symtab->find(name)) {
1219 if (auto *ds = dyn_cast<Defined>(sym)) {
1220 ExprValue v{ds->section, false, ds->value, loc};
1221 // Retain the original st_type, so that the alias will get the same
1222 // behavior in relocation processing. Any operation will reset st_type to
1227 if (isa<SharedSymbol>(sym))
1228 if (!errorOnMissingSection)
1229 return {nullptr, false, 0, loc};
1232 error(loc + ": symbol not found: " + name);
1236 // Returns the index of the segment named Name.
1237 static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> vec,
1239 for (size_t i = 0; i < vec.size(); ++i)
1240 if (vec[i].name == name)
1245 // Returns indices of ELF headers containing specific section. Each index is a
1246 // zero based number of ELF header listed within PHDRS {} script block.
1247 std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *cmd) {
1248 std::vector<size_t> ret;
1250 for (StringRef s : cmd->phdrs) {
1251 if (Optional<size_t> idx = getPhdrIndex(phdrsCommands, s))
1252 ret.push_back(*idx);
1253 else if (s != "NONE")
1254 error(cmd->location + ": program header '" + s +
1255 "' is not listed in PHDRS");