1 //===- LinkerScript.cpp ---------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the parser/evaluator of the linker script.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "LinkerScript.h"
14 #include "Config.h"
15 #include "InputFiles.h"
16 #include "InputSection.h"
17 #include "OutputSections.h"
18 #include "SymbolTable.h"
19 #include "Symbols.h"
20 #include "SyntheticSections.h"
21 #include "Target.h"
22 #include "Writer.h"
23 #include "lld/Common/CommonLinkerContext.h"
24 #include "lld/Common/Strings.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/TimeProfiler.h"
32 #include <algorithm>
33 #include <cassert>
34 #include <cstddef>
35 #include <cstdint>
36 #include <limits>
37 #include <string>
38 #include <vector>
39
40 using namespace llvm;
41 using namespace llvm::ELF;
42 using namespace llvm::object;
43 using namespace llvm::support::endian;
44 using namespace lld;
45 using namespace lld::elf;
46
47 std::unique_ptr<LinkerScript> elf::script;
48
isSectionPrefix(StringRef prefix,StringRef name)49 static bool isSectionPrefix(StringRef prefix, StringRef name) {
50 return name.consume_front(prefix) && (name.empty() || name[0] == '.');
51 }
52
getOutputSectionName(const InputSectionBase * s)53 static StringRef getOutputSectionName(const InputSectionBase *s) {
54 if (config->relocatable)
55 return s->name;
56
57 // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
58 // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
59 // technically required, but not doing it is odd). This code guarantees that.
60 if (auto *isec = dyn_cast<InputSection>(s)) {
61 if (InputSectionBase *rel = isec->getRelocatedSection()) {
62 OutputSection *out = rel->getOutputSection();
63 if (s->type == SHT_RELA)
64 return saver().save(".rela" + out->name);
65 return saver().save(".rel" + out->name);
66 }
67 }
68
69 // A BssSection created for a common symbol is identified as "COMMON" in
70 // linker scripts. It should go to .bss section.
71 if (s->name == "COMMON")
72 return ".bss";
73
74 if (script->hasSectionsCommand)
75 return s->name;
76
77 // When no SECTIONS is specified, emulate GNU ld's internal linker scripts
78 // by grouping sections with certain prefixes.
79
80 // GNU ld places text sections with prefix ".text.hot.", ".text.unknown.",
81 // ".text.unlikely.", ".text.startup." or ".text.exit." before others.
82 // We provide an option -z keep-text-section-prefix to group such sections
83 // into separate output sections. This is more flexible. See also
84 // sortISDBySectionOrder().
85 // ".text.unknown" means the hotness of the section is unknown. When
86 // SampleFDO is used, if a function doesn't have sample, it could be very
87 // cold or it could be a new function never being sampled. Those functions
88 // will be kept in the ".text.unknown" section.
89 // ".text.split." holds symbols which are split out from functions in other
90 // input sections. For example, with -fsplit-machine-functions, placing the
91 // cold parts in .text.split instead of .text.unlikely mitigates against poor
92 // profile inaccuracy. Techniques such as hugepage remapping can make
93 // conservative decisions at the section granularity.
94 if (isSectionPrefix(".text", s->name)) {
95 if (config->zKeepTextSectionPrefix)
96 for (StringRef v : {".text.hot", ".text.unknown", ".text.unlikely",
97 ".text.startup", ".text.exit", ".text.split"})
98 if (isSectionPrefix(v.substr(5), s->name.substr(5)))
99 return v;
100 return ".text";
101 }
102
103 for (StringRef v :
104 {".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
105 ".gcc_except_table", ".init_array", ".fini_array", ".tbss", ".tdata",
106 ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors",
107 ".openbsd.randomdata", ".openbsd.mutable"})
108 if (isSectionPrefix(v, s->name))
109 return v;
110
111 return s->name;
112 }
113
getValue() const114 uint64_t ExprValue::getValue() const {
115 if (sec)
116 return alignToPowerOf2(sec->getOutputSection()->addr + sec->getOffset(val),
117 alignment);
118 return alignToPowerOf2(val, alignment);
119 }
120
getSecAddr() const121 uint64_t ExprValue::getSecAddr() const {
122 return sec ? sec->getOutputSection()->addr + sec->getOffset(0) : 0;
123 }
124
getSectionOffset() const125 uint64_t ExprValue::getSectionOffset() const {
126 // If the alignment is trivial, we don't have to compute the full
127 // value to know the offset. This allows this function to succeed in
128 // cases where the output section is not yet known.
129 if (alignment == 1 && !sec)
130 return val;
131 return getValue() - getSecAddr();
132 }
133
createOutputSection(StringRef name,StringRef location)134 OutputDesc *LinkerScript::createOutputSection(StringRef name,
135 StringRef location) {
136 OutputDesc *&secRef = nameToOutputSection[CachedHashStringRef(name)];
137 OutputDesc *sec;
138 if (secRef && secRef->osec.location.empty()) {
139 // There was a forward reference.
140 sec = secRef;
141 } else {
142 sec = make<OutputDesc>(name, SHT_PROGBITS, 0);
143 if (!secRef)
144 secRef = sec;
145 }
146 sec->osec.location = std::string(location);
147 return sec;
148 }
149
getOrCreateOutputSection(StringRef name)150 OutputDesc *LinkerScript::getOrCreateOutputSection(StringRef name) {
151 OutputDesc *&cmdRef = nameToOutputSection[CachedHashStringRef(name)];
152 if (!cmdRef)
153 cmdRef = make<OutputDesc>(name, SHT_PROGBITS, 0);
154 return cmdRef;
155 }
156
157 // Expands the memory region by the specified size.
expandMemoryRegion(MemoryRegion * memRegion,uint64_t size,StringRef secName)158 static void expandMemoryRegion(MemoryRegion *memRegion, uint64_t size,
159 StringRef secName) {
160 memRegion->curPos += size;
161 uint64_t newSize = memRegion->curPos - (memRegion->origin)().getValue();
162 uint64_t length = (memRegion->length)().getValue();
163 if (newSize > length)
164 error("section '" + secName + "' will not fit in region '" +
165 memRegion->name + "': overflowed by " + Twine(newSize - length) +
166 " bytes");
167 }
168
expandMemoryRegions(uint64_t size)169 void LinkerScript::expandMemoryRegions(uint64_t size) {
170 if (state->memRegion)
171 expandMemoryRegion(state->memRegion, size, state->outSec->name);
172 // Only expand the LMARegion if it is different from memRegion.
173 if (state->lmaRegion && state->memRegion != state->lmaRegion)
174 expandMemoryRegion(state->lmaRegion, size, state->outSec->name);
175 }
176
expandOutputSection(uint64_t size)177 void LinkerScript::expandOutputSection(uint64_t size) {
178 state->outSec->size += size;
179 expandMemoryRegions(size);
180 }
181
setDot(Expr e,const Twine & loc,bool inSec)182 void LinkerScript::setDot(Expr e, const Twine &loc, bool inSec) {
183 uint64_t val = e().getValue();
184 if (val < dot && inSec)
185 error(loc + ": unable to move location counter backward for: " +
186 state->outSec->name);
187
188 // Update to location counter means update to section size.
189 if (inSec)
190 expandOutputSection(val - dot);
191
192 dot = val;
193 }
194
195 // Used for handling linker symbol assignments, for both finalizing
196 // their values and doing early declarations. Returns true if symbol
197 // should be defined from linker script.
shouldDefineSym(SymbolAssignment * cmd)198 static bool shouldDefineSym(SymbolAssignment *cmd) {
199 if (cmd->name == ".")
200 return false;
201
202 if (!cmd->provide)
203 return true;
204
205 // If a symbol was in PROVIDE(), we need to define it only
206 // when it is a referenced undefined symbol.
207 Symbol *b = symtab.find(cmd->name);
208 if (b && !b->isDefined() && !b->isCommon())
209 return true;
210 return false;
211 }
212
213 // Called by processSymbolAssignments() to assign definitions to
214 // linker-script-defined symbols.
addSymbol(SymbolAssignment * cmd)215 void LinkerScript::addSymbol(SymbolAssignment *cmd) {
216 if (!shouldDefineSym(cmd))
217 return;
218
219 // Define a symbol.
220 ExprValue value = cmd->expression();
221 SectionBase *sec = value.isAbsolute() ? nullptr : value.sec;
222 uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
223
224 // When this function is called, section addresses have not been
225 // fixed yet. So, we may or may not know the value of the RHS
226 // expression.
227 //
228 // For example, if an expression is `x = 42`, we know x is always 42.
229 // However, if an expression is `x = .`, there's no way to know its
230 // value at the moment.
231 //
232 // We want to set symbol values early if we can. This allows us to
233 // use symbols as variables in linker scripts. Doing so allows us to
234 // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
235 uint64_t symValue = value.sec ? 0 : value.getValue();
236
237 Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, value.type,
238 symValue, 0, sec);
239
240 Symbol *sym = symtab.insert(cmd->name);
241 sym->mergeProperties(newSym);
242 newSym.overwrite(*sym);
243 sym->isUsedInRegularObj = true;
244 cmd->sym = cast<Defined>(sym);
245 }
246
247 // This function is called from LinkerScript::declareSymbols.
248 // It creates a placeholder symbol if needed.
declareSymbol(SymbolAssignment * cmd)249 static void declareSymbol(SymbolAssignment *cmd) {
250 if (!shouldDefineSym(cmd))
251 return;
252
253 uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
254 Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE, 0, 0,
255 nullptr);
256
257 // We can't calculate final value right now.
258 Symbol *sym = symtab.insert(cmd->name);
259 sym->mergeProperties(newSym);
260 newSym.overwrite(*sym);
261
262 cmd->sym = cast<Defined>(sym);
263 cmd->provide = false;
264 sym->isUsedInRegularObj = true;
265 sym->scriptDefined = true;
266 }
267
268 using SymbolAssignmentMap =
269 DenseMap<const Defined *, std::pair<SectionBase *, uint64_t>>;
270
271 // Collect section/value pairs of linker-script-defined symbols. This is used to
272 // check whether symbol values converge.
273 static SymbolAssignmentMap
getSymbolAssignmentValues(ArrayRef<SectionCommand * > sectionCommands)274 getSymbolAssignmentValues(ArrayRef<SectionCommand *> sectionCommands) {
275 SymbolAssignmentMap ret;
276 for (SectionCommand *cmd : sectionCommands) {
277 if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
278 if (assign->sym) // sym is nullptr for dot.
279 ret.try_emplace(assign->sym, std::make_pair(assign->sym->section,
280 assign->sym->value));
281 continue;
282 }
283 for (SectionCommand *subCmd : cast<OutputDesc>(cmd)->osec.commands)
284 if (auto *assign = dyn_cast<SymbolAssignment>(subCmd))
285 if (assign->sym)
286 ret.try_emplace(assign->sym, std::make_pair(assign->sym->section,
287 assign->sym->value));
288 }
289 return ret;
290 }
291
292 // Returns the lexicographical smallest (for determinism) Defined whose
293 // section/value has changed.
294 static const Defined *
getChangedSymbolAssignment(const SymbolAssignmentMap & oldValues)295 getChangedSymbolAssignment(const SymbolAssignmentMap &oldValues) {
296 const Defined *changed = nullptr;
297 for (auto &it : oldValues) {
298 const Defined *sym = it.first;
299 if (std::make_pair(sym->section, sym->value) != it.second &&
300 (!changed || sym->getName() < changed->getName()))
301 changed = sym;
302 }
303 return changed;
304 }
305
306 // Process INSERT [AFTER|BEFORE] commands. For each command, we move the
307 // specified output section to the designated place.
processInsertCommands()308 void LinkerScript::processInsertCommands() {
309 SmallVector<OutputDesc *, 0> moves;
310 for (const InsertCommand &cmd : insertCommands) {
311 for (StringRef name : cmd.names) {
312 // If base is empty, it may have been discarded by
313 // adjustOutputSections(). We do not handle such output sections.
314 auto from = llvm::find_if(sectionCommands, [&](SectionCommand *subCmd) {
315 return isa<OutputDesc>(subCmd) &&
316 cast<OutputDesc>(subCmd)->osec.name == name;
317 });
318 if (from == sectionCommands.end())
319 continue;
320 moves.push_back(cast<OutputDesc>(*from));
321 sectionCommands.erase(from);
322 }
323
324 auto insertPos =
325 llvm::find_if(sectionCommands, [&cmd](SectionCommand *subCmd) {
326 auto *to = dyn_cast<OutputDesc>(subCmd);
327 return to != nullptr && to->osec.name == cmd.where;
328 });
329 if (insertPos == sectionCommands.end()) {
330 error("unable to insert " + cmd.names[0] +
331 (cmd.isAfter ? " after " : " before ") + cmd.where);
332 } else {
333 if (cmd.isAfter)
334 ++insertPos;
335 sectionCommands.insert(insertPos, moves.begin(), moves.end());
336 }
337 moves.clear();
338 }
339 }
340
341 // Symbols defined in script should not be inlined by LTO. At the same time
342 // we don't know their final values until late stages of link. Here we scan
343 // over symbol assignment commands and create placeholder symbols if needed.
declareSymbols()344 void LinkerScript::declareSymbols() {
345 assert(!state);
346 for (SectionCommand *cmd : sectionCommands) {
347 if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
348 declareSymbol(assign);
349 continue;
350 }
351
352 // If the output section directive has constraints,
353 // we can't say for sure if it is going to be included or not.
354 // Skip such sections for now. Improve the checks if we ever
355 // need symbols from that sections to be declared early.
356 const OutputSection &sec = cast<OutputDesc>(cmd)->osec;
357 if (sec.constraint != ConstraintKind::NoConstraint)
358 continue;
359 for (SectionCommand *cmd : sec.commands)
360 if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
361 declareSymbol(assign);
362 }
363 }
364
365 // This function is called from assignAddresses, while we are
366 // fixing the output section addresses. This function is supposed
367 // to set the final value for a given symbol assignment.
assignSymbol(SymbolAssignment * cmd,bool inSec)368 void LinkerScript::assignSymbol(SymbolAssignment *cmd, bool inSec) {
369 if (cmd->name == ".") {
370 setDot(cmd->expression, cmd->location, inSec);
371 return;
372 }
373
374 if (!cmd->sym)
375 return;
376
377 ExprValue v = cmd->expression();
378 if (v.isAbsolute()) {
379 cmd->sym->section = nullptr;
380 cmd->sym->value = v.getValue();
381 } else {
382 cmd->sym->section = v.sec;
383 cmd->sym->value = v.getSectionOffset();
384 }
385 cmd->sym->type = v.type;
386 }
387
getFilename(const InputFile * file)388 static inline StringRef getFilename(const InputFile *file) {
389 return file ? file->getNameForScript() : StringRef();
390 }
391
matchesFile(const InputFile * file) const392 bool InputSectionDescription::matchesFile(const InputFile *file) const {
393 if (filePat.isTrivialMatchAll())
394 return true;
395
396 if (!matchesFileCache || matchesFileCache->first != file)
397 matchesFileCache.emplace(file, filePat.match(getFilename(file)));
398
399 return matchesFileCache->second;
400 }
401
excludesFile(const InputFile * file) const402 bool SectionPattern::excludesFile(const InputFile *file) const {
403 if (excludedFilePat.empty())
404 return false;
405
406 if (!excludesFileCache || excludesFileCache->first != file)
407 excludesFileCache.emplace(file, excludedFilePat.match(getFilename(file)));
408
409 return excludesFileCache->second;
410 }
411
shouldKeep(InputSectionBase * s)412 bool LinkerScript::shouldKeep(InputSectionBase *s) {
413 for (InputSectionDescription *id : keptSections)
414 if (id->matchesFile(s->file))
415 for (SectionPattern &p : id->sectionPatterns)
416 if (p.sectionPat.match(s->name) &&
417 (s->flags & id->withFlags) == id->withFlags &&
418 (s->flags & id->withoutFlags) == 0)
419 return true;
420 return false;
421 }
422
423 // A helper function for the SORT() command.
matchConstraints(ArrayRef<InputSectionBase * > sections,ConstraintKind kind)424 static bool matchConstraints(ArrayRef<InputSectionBase *> sections,
425 ConstraintKind kind) {
426 if (kind == ConstraintKind::NoConstraint)
427 return true;
428
429 bool isRW = llvm::any_of(
430 sections, [](InputSectionBase *sec) { return sec->flags & SHF_WRITE; });
431
432 return (isRW && kind == ConstraintKind::ReadWrite) ||
433 (!isRW && kind == ConstraintKind::ReadOnly);
434 }
435
sortSections(MutableArrayRef<InputSectionBase * > vec,SortSectionPolicy k)436 static void sortSections(MutableArrayRef<InputSectionBase *> vec,
437 SortSectionPolicy k) {
438 auto alignmentComparator = [](InputSectionBase *a, InputSectionBase *b) {
439 // ">" is not a mistake. Sections with larger alignments are placed
440 // before sections with smaller alignments in order to reduce the
441 // amount of padding necessary. This is compatible with GNU.
442 return a->addralign > b->addralign;
443 };
444 auto nameComparator = [](InputSectionBase *a, InputSectionBase *b) {
445 return a->name < b->name;
446 };
447 auto priorityComparator = [](InputSectionBase *a, InputSectionBase *b) {
448 return getPriority(a->name) < getPriority(b->name);
449 };
450
451 switch (k) {
452 case SortSectionPolicy::Default:
453 case SortSectionPolicy::None:
454 return;
455 case SortSectionPolicy::Alignment:
456 return llvm::stable_sort(vec, alignmentComparator);
457 case SortSectionPolicy::Name:
458 return llvm::stable_sort(vec, nameComparator);
459 case SortSectionPolicy::Priority:
460 return llvm::stable_sort(vec, priorityComparator);
461 }
462 }
463
464 // Sort sections as instructed by SORT-family commands and --sort-section
465 // option. Because SORT-family commands can be nested at most two depth
466 // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
467 // line option is respected even if a SORT command is given, the exact
468 // behavior we have here is a bit complicated. Here are the rules.
469 //
470 // 1. If two SORT commands are given, --sort-section is ignored.
471 // 2. If one SORT command is given, and if it is not SORT_NONE,
472 // --sort-section is handled as an inner SORT command.
473 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
474 // 4. If no SORT command is given, sort according to --sort-section.
sortInputSections(MutableArrayRef<InputSectionBase * > vec,SortSectionPolicy outer,SortSectionPolicy inner)475 static void sortInputSections(MutableArrayRef<InputSectionBase *> vec,
476 SortSectionPolicy outer,
477 SortSectionPolicy inner) {
478 if (outer == SortSectionPolicy::None)
479 return;
480
481 if (inner == SortSectionPolicy::Default)
482 sortSections(vec, config->sortSection);
483 else
484 sortSections(vec, inner);
485 sortSections(vec, outer);
486 }
487
488 // Compute and remember which sections the InputSectionDescription matches.
489 SmallVector<InputSectionBase *, 0>
computeInputSections(const InputSectionDescription * cmd,ArrayRef<InputSectionBase * > sections)490 LinkerScript::computeInputSections(const InputSectionDescription *cmd,
491 ArrayRef<InputSectionBase *> sections) {
492 SmallVector<InputSectionBase *, 0> ret;
493 SmallVector<size_t, 0> indexes;
494 DenseSet<size_t> seen;
495 auto sortByPositionThenCommandLine = [&](size_t begin, size_t end) {
496 llvm::sort(MutableArrayRef<size_t>(indexes).slice(begin, end - begin));
497 for (size_t i = begin; i != end; ++i)
498 ret[i] = sections[indexes[i]];
499 sortInputSections(
500 MutableArrayRef<InputSectionBase *>(ret).slice(begin, end - begin),
501 config->sortSection, SortSectionPolicy::None);
502 };
503
504 // Collects all sections that satisfy constraints of Cmd.
505 size_t sizeAfterPrevSort = 0;
506 for (const SectionPattern &pat : cmd->sectionPatterns) {
507 size_t sizeBeforeCurrPat = ret.size();
508
509 for (size_t i = 0, e = sections.size(); i != e; ++i) {
510 // Skip if the section is dead or has been matched by a previous input
511 // section description or a previous pattern.
512 InputSectionBase *sec = sections[i];
513 if (!sec->isLive() || sec->parent || seen.contains(i))
514 continue;
515
516 // For --emit-relocs we have to ignore entries like
517 // .rela.dyn : { *(.rela.data) }
518 // which are common because they are in the default bfd script.
519 // We do not ignore SHT_REL[A] linker-synthesized sections here because
520 // want to support scripts that do custom layout for them.
521 if (isa<InputSection>(sec) &&
522 cast<InputSection>(sec)->getRelocatedSection())
523 continue;
524
525 // Check the name early to improve performance in the common case.
526 if (!pat.sectionPat.match(sec->name))
527 continue;
528
529 if (!cmd->matchesFile(sec->file) || pat.excludesFile(sec->file) ||
530 (sec->flags & cmd->withFlags) != cmd->withFlags ||
531 (sec->flags & cmd->withoutFlags) != 0)
532 continue;
533
534 ret.push_back(sec);
535 indexes.push_back(i);
536 seen.insert(i);
537 }
538
539 if (pat.sortOuter == SortSectionPolicy::Default)
540 continue;
541
542 // Matched sections are ordered by radix sort with the keys being (SORT*,
543 // --sort-section, input order), where SORT* (if present) is most
544 // significant.
545 //
546 // Matched sections between the previous SORT* and this SORT* are sorted by
547 // (--sort-alignment, input order).
548 sortByPositionThenCommandLine(sizeAfterPrevSort, sizeBeforeCurrPat);
549 // Matched sections by this SORT* pattern are sorted using all 3 keys.
550 // ret[sizeBeforeCurrPat,ret.size()) are already in the input order, so we
551 // just sort by sortOuter and sortInner.
552 sortInputSections(
553 MutableArrayRef<InputSectionBase *>(ret).slice(sizeBeforeCurrPat),
554 pat.sortOuter, pat.sortInner);
555 sizeAfterPrevSort = ret.size();
556 }
557 // Matched sections after the last SORT* are sorted by (--sort-alignment,
558 // input order).
559 sortByPositionThenCommandLine(sizeAfterPrevSort, ret.size());
560 return ret;
561 }
562
discard(InputSectionBase & s)563 void LinkerScript::discard(InputSectionBase &s) {
564 if (&s == in.shStrTab.get())
565 error("discarding " + s.name + " section is not allowed");
566
567 s.markDead();
568 s.parent = nullptr;
569 for (InputSection *sec : s.dependentSections)
570 discard(*sec);
571 }
572
discardSynthetic(OutputSection & outCmd)573 void LinkerScript::discardSynthetic(OutputSection &outCmd) {
574 for (Partition &part : partitions) {
575 if (!part.armExidx || !part.armExidx->isLive())
576 continue;
577 SmallVector<InputSectionBase *, 0> secs(
578 part.armExidx->exidxSections.begin(),
579 part.armExidx->exidxSections.end());
580 for (SectionCommand *cmd : outCmd.commands)
581 if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
582 for (InputSectionBase *s : computeInputSections(isd, secs))
583 discard(*s);
584 }
585 }
586
587 SmallVector<InputSectionBase *, 0>
createInputSectionList(OutputSection & outCmd)588 LinkerScript::createInputSectionList(OutputSection &outCmd) {
589 SmallVector<InputSectionBase *, 0> ret;
590
591 for (SectionCommand *cmd : outCmd.commands) {
592 if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) {
593 isd->sectionBases = computeInputSections(isd, ctx.inputSections);
594 for (InputSectionBase *s : isd->sectionBases)
595 s->parent = &outCmd;
596 ret.insert(ret.end(), isd->sectionBases.begin(), isd->sectionBases.end());
597 }
598 }
599 return ret;
600 }
601
602 // Create output sections described by SECTIONS commands.
processSectionCommands()603 void LinkerScript::processSectionCommands() {
604 auto process = [this](OutputSection *osec) {
605 SmallVector<InputSectionBase *, 0> v = createInputSectionList(*osec);
606
607 // The output section name `/DISCARD/' is special.
608 // Any input section assigned to it is discarded.
609 if (osec->name == "/DISCARD/") {
610 for (InputSectionBase *s : v)
611 discard(*s);
612 discardSynthetic(*osec);
613 osec->commands.clear();
614 return false;
615 }
616
617 // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
618 // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
619 // sections satisfy a given constraint. If not, a directive is handled
620 // as if it wasn't present from the beginning.
621 //
622 // Because we'll iterate over SectionCommands many more times, the easy
623 // way to "make it as if it wasn't present" is to make it empty.
624 if (!matchConstraints(v, osec->constraint)) {
625 for (InputSectionBase *s : v)
626 s->parent = nullptr;
627 osec->commands.clear();
628 return false;
629 }
630
631 // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
632 // is given, input sections are aligned to that value, whether the
633 // given value is larger or smaller than the original section alignment.
634 if (osec->subalignExpr) {
635 uint32_t subalign = osec->subalignExpr().getValue();
636 for (InputSectionBase *s : v)
637 s->addralign = subalign;
638 }
639
640 // Set the partition field the same way OutputSection::recordSection()
641 // does. Partitions cannot be used with the SECTIONS command, so this is
642 // always 1.
643 osec->partition = 1;
644 return true;
645 };
646
647 // Process OVERWRITE_SECTIONS first so that it can overwrite the main script
648 // or orphans.
649 DenseMap<CachedHashStringRef, OutputDesc *> map;
650 size_t i = 0;
651 for (OutputDesc *osd : overwriteSections) {
652 OutputSection *osec = &osd->osec;
653 if (process(osec) &&
654 !map.try_emplace(CachedHashStringRef(osec->name), osd).second)
655 warn("OVERWRITE_SECTIONS specifies duplicate " + osec->name);
656 }
657 for (SectionCommand *&base : sectionCommands)
658 if (auto *osd = dyn_cast<OutputDesc>(base)) {
659 OutputSection *osec = &osd->osec;
660 if (OutputDesc *overwrite = map.lookup(CachedHashStringRef(osec->name))) {
661 log(overwrite->osec.location + " overwrites " + osec->name);
662 overwrite->osec.sectionIndex = i++;
663 base = overwrite;
664 } else if (process(osec)) {
665 osec->sectionIndex = i++;
666 }
667 }
668
669 // If an OVERWRITE_SECTIONS specified output section is not in
670 // sectionCommands, append it to the end. The section will be inserted by
671 // orphan placement.
672 for (OutputDesc *osd : overwriteSections)
673 if (osd->osec.partition == 1 && osd->osec.sectionIndex == UINT32_MAX)
674 sectionCommands.push_back(osd);
675 }
676
processSymbolAssignments()677 void LinkerScript::processSymbolAssignments() {
678 // Dot outside an output section still represents a relative address, whose
679 // sh_shndx should not be SHN_UNDEF or SHN_ABS. Create a dummy aether section
680 // that fills the void outside a section. It has an index of one, which is
681 // indistinguishable from any other regular section index.
682 aether = make<OutputSection>("", 0, SHF_ALLOC);
683 aether->sectionIndex = 1;
684
685 // `st` captures the local AddressState and makes it accessible deliberately.
686 // This is needed as there are some cases where we cannot just thread the
687 // current state through to a lambda function created by the script parser.
688 AddressState st;
689 state = &st;
690 st.outSec = aether;
691
692 for (SectionCommand *cmd : sectionCommands) {
693 if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
694 addSymbol(assign);
695 else
696 for (SectionCommand *subCmd : cast<OutputDesc>(cmd)->osec.commands)
697 if (auto *assign = dyn_cast<SymbolAssignment>(subCmd))
698 addSymbol(assign);
699 }
700
701 state = nullptr;
702 }
703
findByName(ArrayRef<SectionCommand * > vec,StringRef name)704 static OutputSection *findByName(ArrayRef<SectionCommand *> vec,
705 StringRef name) {
706 for (SectionCommand *cmd : vec)
707 if (auto *osd = dyn_cast<OutputDesc>(cmd))
708 if (osd->osec.name == name)
709 return &osd->osec;
710 return nullptr;
711 }
712
createSection(InputSectionBase * isec,StringRef outsecName)713 static OutputDesc *createSection(InputSectionBase *isec, StringRef outsecName) {
714 OutputDesc *osd = script->createOutputSection(outsecName, "<internal>");
715 osd->osec.recordSection(isec);
716 return osd;
717 }
718
addInputSec(StringMap<TinyPtrVector<OutputSection * >> & map,InputSectionBase * isec,StringRef outsecName)719 static OutputDesc *addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
720 InputSectionBase *isec, StringRef outsecName) {
721 // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
722 // option is given. A section with SHT_GROUP defines a "section group", and
723 // its members have SHF_GROUP attribute. Usually these flags have already been
724 // stripped by InputFiles.cpp as section groups are processed and uniquified.
725 // However, for the -r option, we want to pass through all section groups
726 // as-is because adding/removing members or merging them with other groups
727 // change their semantics.
728 if (isec->type == SHT_GROUP || (isec->flags & SHF_GROUP))
729 return createSection(isec, outsecName);
730
731 // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
732 // relocation sections .rela.foo and .rela.bar for example. Most tools do
733 // not allow multiple REL[A] sections for output section. Hence we
734 // should combine these relocation sections into single output.
735 // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
736 // other REL[A] sections created by linker itself.
737 if (!isa<SyntheticSection>(isec) &&
738 (isec->type == SHT_REL || isec->type == SHT_RELA)) {
739 auto *sec = cast<InputSection>(isec);
740 OutputSection *out = sec->getRelocatedSection()->getOutputSection();
741
742 if (out->relocationSection) {
743 out->relocationSection->recordSection(sec);
744 return nullptr;
745 }
746
747 OutputDesc *osd = createSection(isec, outsecName);
748 out->relocationSection = &osd->osec;
749 return osd;
750 }
751
752 // The ELF spec just says
753 // ----------------------------------------------------------------
754 // In the first phase, input sections that match in name, type and
755 // attribute flags should be concatenated into single sections.
756 // ----------------------------------------------------------------
757 //
758 // However, it is clear that at least some flags have to be ignored for
759 // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
760 // ignored. We should not have two output .text sections just because one was
761 // in a group and another was not for example.
762 //
763 // It also seems that wording was a late addition and didn't get the
764 // necessary scrutiny.
765 //
766 // Merging sections with different flags is expected by some users. One
767 // reason is that if one file has
768 //
769 // int *const bar __attribute__((section(".foo"))) = (int *)0;
770 //
771 // gcc with -fPIC will produce a read only .foo section. But if another
772 // file has
773 //
774 // int zed;
775 // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
776 //
777 // gcc with -fPIC will produce a read write section.
778 //
779 // Last but not least, when using linker script the merge rules are forced by
780 // the script. Unfortunately, linker scripts are name based. This means that
781 // expressions like *(.foo*) can refer to multiple input sections with
782 // different flags. We cannot put them in different output sections or we
783 // would produce wrong results for
784 //
785 // start = .; *(.foo.*) end = .; *(.bar)
786 //
787 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
788 // another. The problem is that there is no way to layout those output
789 // sections such that the .foo sections are the only thing between the start
790 // and end symbols.
791 //
792 // Given the above issues, we instead merge sections by name and error on
793 // incompatible types and flags.
794 TinyPtrVector<OutputSection *> &v = map[outsecName];
795 for (OutputSection *sec : v) {
796 if (sec->partition != isec->partition)
797 continue;
798
799 if (config->relocatable && (isec->flags & SHF_LINK_ORDER)) {
800 // Merging two SHF_LINK_ORDER sections with different sh_link fields will
801 // change their semantics, so we only merge them in -r links if they will
802 // end up being linked to the same output section. The casts are fine
803 // because everything in the map was created by the orphan placement code.
804 auto *firstIsec = cast<InputSectionBase>(
805 cast<InputSectionDescription>(sec->commands[0])->sectionBases[0]);
806 OutputSection *firstIsecOut =
807 firstIsec->flags & SHF_LINK_ORDER
808 ? firstIsec->getLinkOrderDep()->getOutputSection()
809 : nullptr;
810 if (firstIsecOut != isec->getLinkOrderDep()->getOutputSection())
811 continue;
812 }
813
814 sec->recordSection(isec);
815 return nullptr;
816 }
817
818 OutputDesc *osd = createSection(isec, outsecName);
819 v.push_back(&osd->osec);
820 return osd;
821 }
822
823 // Add sections that didn't match any sections command.
addOrphanSections()824 void LinkerScript::addOrphanSections() {
825 StringMap<TinyPtrVector<OutputSection *>> map;
826 SmallVector<OutputDesc *, 0> v;
827
828 auto add = [&](InputSectionBase *s) {
829 if (s->isLive() && !s->parent) {
830 orphanSections.push_back(s);
831
832 StringRef name = getOutputSectionName(s);
833 if (config->unique) {
834 v.push_back(createSection(s, name));
835 } else if (OutputSection *sec = findByName(sectionCommands, name)) {
836 sec->recordSection(s);
837 } else {
838 if (OutputDesc *osd = addInputSec(map, s, name))
839 v.push_back(osd);
840 assert(isa<MergeInputSection>(s) ||
841 s->getOutputSection()->sectionIndex == UINT32_MAX);
842 }
843 }
844 };
845
846 // For further --emit-reloc handling code we need target output section
847 // to be created before we create relocation output section, so we want
848 // to create target sections first. We do not want priority handling
849 // for synthetic sections because them are special.
850 size_t n = 0;
851 for (InputSectionBase *isec : ctx.inputSections) {
852 // Process InputSection and MergeInputSection.
853 if (LLVM_LIKELY(isa<InputSection>(isec)))
854 ctx.inputSections[n++] = isec;
855
856 // In -r links, SHF_LINK_ORDER sections are added while adding their parent
857 // sections because we need to know the parent's output section before we
858 // can select an output section for the SHF_LINK_ORDER section.
859 if (config->relocatable && (isec->flags & SHF_LINK_ORDER))
860 continue;
861
862 if (auto *sec = dyn_cast<InputSection>(isec))
863 if (InputSectionBase *rel = sec->getRelocatedSection())
864 if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
865 add(relIS);
866 add(isec);
867 if (config->relocatable)
868 for (InputSectionBase *depSec : isec->dependentSections)
869 if (depSec->flags & SHF_LINK_ORDER)
870 add(depSec);
871 }
872 // Keep just InputSection.
873 ctx.inputSections.resize(n);
874
875 // If no SECTIONS command was given, we should insert sections commands
876 // before others, so that we can handle scripts which refers them,
877 // for example: "foo = ABSOLUTE(ADDR(.text)));".
878 // When SECTIONS command is present we just add all orphans to the end.
879 if (hasSectionsCommand)
880 sectionCommands.insert(sectionCommands.end(), v.begin(), v.end());
881 else
882 sectionCommands.insert(sectionCommands.begin(), v.begin(), v.end());
883 }
884
diagnoseOrphanHandling() const885 void LinkerScript::diagnoseOrphanHandling() const {
886 llvm::TimeTraceScope timeScope("Diagnose orphan sections");
887 if (config->orphanHandling == OrphanHandlingPolicy::Place)
888 return;
889 for (const InputSectionBase *sec : orphanSections) {
890 // Input SHT_REL[A] retained by --emit-relocs are ignored by
891 // computeInputSections(). Don't warn/error.
892 if (isa<InputSection>(sec) &&
893 cast<InputSection>(sec)->getRelocatedSection())
894 continue;
895
896 StringRef name = getOutputSectionName(sec);
897 if (config->orphanHandling == OrphanHandlingPolicy::Error)
898 error(toString(sec) + " is being placed in '" + name + "'");
899 else
900 warn(toString(sec) + " is being placed in '" + name + "'");
901 }
902 }
903
904 // This function searches for a memory region to place the given output
905 // section in. If found, a pointer to the appropriate memory region is
906 // returned in the first member of the pair. Otherwise, a nullptr is returned.
907 // The second member of the pair is a hint that should be passed to the
908 // subsequent call of this method.
909 std::pair<MemoryRegion *, MemoryRegion *>
findMemoryRegion(OutputSection * sec,MemoryRegion * hint)910 LinkerScript::findMemoryRegion(OutputSection *sec, MemoryRegion *hint) {
911 // Non-allocatable sections are not part of the process image.
912 if (!(sec->flags & SHF_ALLOC)) {
913 if (!sec->memoryRegionName.empty())
914 warn("ignoring memory region assignment for non-allocatable section '" +
915 sec->name + "'");
916 return {nullptr, nullptr};
917 }
918
919 // If a memory region name was specified in the output section command,
920 // then try to find that region first.
921 if (!sec->memoryRegionName.empty()) {
922 if (MemoryRegion *m = memoryRegions.lookup(sec->memoryRegionName))
923 return {m, m};
924 error("memory region '" + sec->memoryRegionName + "' not declared");
925 return {nullptr, nullptr};
926 }
927
928 // If at least one memory region is defined, all sections must
929 // belong to some memory region. Otherwise, we don't need to do
930 // anything for memory regions.
931 if (memoryRegions.empty())
932 return {nullptr, nullptr};
933
934 // An orphan section should continue the previous memory region.
935 if (sec->sectionIndex == UINT32_MAX && hint)
936 return {hint, hint};
937
938 // See if a region can be found by matching section flags.
939 for (auto &pair : memoryRegions) {
940 MemoryRegion *m = pair.second;
941 if (m->compatibleWith(sec->flags))
942 return {m, nullptr};
943 }
944
945 // Otherwise, no suitable region was found.
946 error("no memory region specified for section '" + sec->name + "'");
947 return {nullptr, nullptr};
948 }
949
findFirstSection(PhdrEntry * load)950 static OutputSection *findFirstSection(PhdrEntry *load) {
951 for (OutputSection *sec : outputSections)
952 if (sec->ptLoad == load)
953 return sec;
954 return nullptr;
955 }
956
957 // This function assigns offsets to input sections and an output section
958 // for a single sections command (e.g. ".text { *(.text); }").
assignOffsets(OutputSection * sec)959 void LinkerScript::assignOffsets(OutputSection *sec) {
960 const bool isTbss = (sec->flags & SHF_TLS) && sec->type == SHT_NOBITS;
961 const bool sameMemRegion = state->memRegion == sec->memRegion;
962 const bool prevLMARegionIsDefault = state->lmaRegion == nullptr;
963 const uint64_t savedDot = dot;
964 state->memRegion = sec->memRegion;
965 state->lmaRegion = sec->lmaRegion;
966
967 if (!(sec->flags & SHF_ALLOC)) {
968 // Non-SHF_ALLOC sections have zero addresses.
969 dot = 0;
970 } else if (isTbss) {
971 // Allow consecutive SHF_TLS SHT_NOBITS output sections. The address range
972 // starts from the end address of the previous tbss section.
973 if (state->tbssAddr == 0)
974 state->tbssAddr = dot;
975 else
976 dot = state->tbssAddr;
977 } else {
978 if (state->memRegion)
979 dot = state->memRegion->curPos;
980 if (sec->addrExpr)
981 setDot(sec->addrExpr, sec->location, false);
982
983 // If the address of the section has been moved forward by an explicit
984 // expression so that it now starts past the current curPos of the enclosing
985 // region, we need to expand the current region to account for the space
986 // between the previous section, if any, and the start of this section.
987 if (state->memRegion && state->memRegion->curPos < dot)
988 expandMemoryRegion(state->memRegion, dot - state->memRegion->curPos,
989 sec->name);
990 }
991
992 // This section was previously a call to switchTo(), but switchTo()
993 // was unrolled here.
994 // On OpenBSD, we had consistently moved the call to switchTo()
995 // below the next section.
996 state->outSec = sec;
997 if (sec->addrExpr && script->hasSectionsCommand) {
998 // The alignment is ignored.
999 sec->addr = dot;
1000 } else {
1001 // sec->alignment is the max of ALIGN and the maximum of input
1002 // section alignments.
1003 const uint64_t pos = dot;
1004 dot = alignToPowerOf2(dot, sec->addralign);
1005 sec->addr = dot;
1006 expandMemoryRegions(dot - pos);
1007 }
1008
1009 // state->lmaOffset is LMA minus VMA. If LMA is explicitly specified via AT()
1010 // or AT>, recompute state->lmaOffset; otherwise, if both previous/current LMA
1011 // region is the default, and the two sections are in the same memory region,
1012 // reuse previous lmaOffset; otherwise, reset lmaOffset to 0. This emulates
1013 // heuristics described in
1014 // https://sourceware.org/binutils/docs/ld/Output-Section-LMA.html
1015 if (sec->lmaExpr) {
1016 state->lmaOffset = sec->lmaExpr().getValue() - dot;
1017 } else if (MemoryRegion *mr = sec->lmaRegion) {
1018 uint64_t lmaStart = alignToPowerOf2(mr->curPos, sec->addralign);
1019 if (mr->curPos < lmaStart)
1020 expandMemoryRegion(mr, lmaStart - mr->curPos, sec->name);
1021 state->lmaOffset = lmaStart - dot;
1022 } else if (!sameMemRegion || !prevLMARegionIsDefault) {
1023 state->lmaOffset = 0;
1024 }
1025
1026 // On OpenBSD, the switchTo() call was here.
1027
1028 // Propagate state->lmaOffset to the first "non-header" section.
1029 if (PhdrEntry *l = sec->ptLoad)
1030 if (sec == findFirstSection(l))
1031 l->lmaOffset = state->lmaOffset;
1032
1033 // We can call this method multiple times during the creation of
1034 // thunks and want to start over calculation each time.
1035 sec->size = 0;
1036
1037 // We visited SectionsCommands from processSectionCommands to
1038 // layout sections. Now, we visit SectionsCommands again to fix
1039 // section offsets.
1040 for (SectionCommand *cmd : sec->commands) {
1041 // This handles the assignments to symbol or to the dot.
1042 if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
1043 assign->addr = dot;
1044 assignSymbol(assign, true);
1045 assign->size = dot - assign->addr;
1046 continue;
1047 }
1048
1049 // Handle BYTE(), SHORT(), LONG(), or QUAD().
1050 if (auto *data = dyn_cast<ByteCommand>(cmd)) {
1051 data->offset = dot - sec->addr;
1052 dot += data->size;
1053 expandOutputSection(data->size);
1054 continue;
1055 }
1056
1057 // Handle a single input section description command.
1058 // It calculates and assigns the offsets for each section and also
1059 // updates the output section size.
1060 for (InputSection *isec : cast<InputSectionDescription>(cmd)->sections) {
1061 assert(isec->getParent() == sec);
1062 const uint64_t pos = dot;
1063 dot = alignToPowerOf2(dot, isec->addralign);
1064 isec->outSecOff = dot - sec->addr;
1065 dot += isec->getSize();
1066
1067 // Update output section size after adding each section. This is so that
1068 // SIZEOF works correctly in the case below:
1069 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
1070 expandOutputSection(dot - pos);
1071 }
1072 }
1073
1074 // Non-SHF_ALLOC sections do not affect the addresses of other OutputSections
1075 // as they are not part of the process image.
1076 if (!(sec->flags & SHF_ALLOC)) {
1077 dot = savedDot;
1078 } else if (isTbss) {
1079 // NOBITS TLS sections are similar. Additionally save the end address.
1080 state->tbssAddr = dot;
1081 dot = savedDot;
1082 }
1083 }
1084
isDiscardable(const OutputSection & sec)1085 static bool isDiscardable(const OutputSection &sec) {
1086 if (sec.name == "/DISCARD/")
1087 return true;
1088
1089 // We do not want to remove OutputSections with expressions that reference
1090 // symbols even if the OutputSection is empty. We want to ensure that the
1091 // expressions can be evaluated and report an error if they cannot.
1092 if (sec.expressionsUseSymbols)
1093 return false;
1094
1095 // OutputSections may be referenced by name in ADDR and LOADADDR expressions,
1096 // as an empty Section can has a valid VMA and LMA we keep the OutputSection
1097 // to maintain the integrity of the other Expression.
1098 if (sec.usedInExpression)
1099 return false;
1100
1101 for (SectionCommand *cmd : sec.commands) {
1102 if (auto assign = dyn_cast<SymbolAssignment>(cmd))
1103 // Don't create empty output sections just for unreferenced PROVIDE
1104 // symbols.
1105 if (assign->name != "." && !assign->sym)
1106 continue;
1107
1108 if (!isa<InputSectionDescription>(*cmd))
1109 return false;
1110 }
1111 return true;
1112 }
1113
isDiscarded(const OutputSection * sec) const1114 bool LinkerScript::isDiscarded(const OutputSection *sec) const {
1115 return hasSectionsCommand && (getFirstInputSection(sec) == nullptr) &&
1116 isDiscardable(*sec);
1117 }
1118
maybePropagatePhdrs(OutputSection & sec,SmallVector<StringRef,0> & phdrs)1119 static void maybePropagatePhdrs(OutputSection &sec,
1120 SmallVector<StringRef, 0> &phdrs) {
1121 if (sec.phdrs.empty()) {
1122 // To match the bfd linker script behaviour, only propagate program
1123 // headers to sections that are allocated.
1124 if (sec.flags & SHF_ALLOC)
1125 sec.phdrs = phdrs;
1126 } else {
1127 phdrs = sec.phdrs;
1128 }
1129 }
1130
adjustOutputSections()1131 void LinkerScript::adjustOutputSections() {
1132 // If the output section contains only symbol assignments, create a
1133 // corresponding output section. The issue is what to do with linker script
1134 // like ".foo : { symbol = 42; }". One option would be to convert it to
1135 // "symbol = 42;". That is, move the symbol out of the empty section
1136 // description. That seems to be what bfd does for this simple case. The
1137 // problem is that this is not completely general. bfd will give up and
1138 // create a dummy section too if there is a ". = . + 1" inside the section
1139 // for example.
1140 // Given that we want to create the section, we have to worry what impact
1141 // it will have on the link. For example, if we just create a section with
1142 // 0 for flags, it would change which PT_LOADs are created.
1143 // We could remember that particular section is dummy and ignore it in
1144 // other parts of the linker, but unfortunately there are quite a few places
1145 // that would need to change:
1146 // * The program header creation.
1147 // * The orphan section placement.
1148 // * The address assignment.
1149 // The other option is to pick flags that minimize the impact the section
1150 // will have on the rest of the linker. That is why we copy the flags from
1151 // the previous sections. Only a few flags are needed to keep the impact low.
1152 uint64_t flags = SHF_ALLOC;
1153
1154 SmallVector<StringRef, 0> defPhdrs;
1155 for (SectionCommand *&cmd : sectionCommands) {
1156 if (!isa<OutputDesc>(cmd))
1157 continue;
1158 auto *sec = &cast<OutputDesc>(cmd)->osec;
1159
1160 // Handle align (e.g. ".foo : ALIGN(16) { ... }").
1161 if (sec->alignExpr)
1162 sec->addralign =
1163 std::max<uint32_t>(sec->addralign, sec->alignExpr().getValue());
1164
1165 bool isEmpty = (getFirstInputSection(sec) == nullptr);
1166 bool discardable = isEmpty && isDiscardable(*sec);
1167 // If sec has at least one input section and not discarded, remember its
1168 // flags to be inherited by subsequent output sections. (sec may contain
1169 // just one empty synthetic section.)
1170 if (sec->hasInputSections && !discardable)
1171 flags = sec->flags;
1172
1173 // We do not want to keep any special flags for output section
1174 // in case it is empty.
1175 if (isEmpty)
1176 sec->flags = flags & ((sec->nonAlloc ? 0 : (uint64_t)SHF_ALLOC) |
1177 SHF_WRITE | SHF_EXECINSTR);
1178
1179 // The code below may remove empty output sections. We should save the
1180 // specified program headers (if exist) and propagate them to subsequent
1181 // sections which do not specify program headers.
1182 // An example of such a linker script is:
1183 // SECTIONS { .empty : { *(.empty) } :rw
1184 // .foo : { *(.foo) } }
1185 // Note: at this point the order of output sections has not been finalized,
1186 // because orphans have not been inserted into their expected positions. We
1187 // will handle them in adjustSectionsAfterSorting().
1188 if (sec->sectionIndex != UINT32_MAX)
1189 maybePropagatePhdrs(*sec, defPhdrs);
1190
1191 if (discardable) {
1192 sec->markDead();
1193 cmd = nullptr;
1194 }
1195 }
1196
1197 // It is common practice to use very generic linker scripts. So for any
1198 // given run some of the output sections in the script will be empty.
1199 // We could create corresponding empty output sections, but that would
1200 // clutter the output.
1201 // We instead remove trivially empty sections. The bfd linker seems even
1202 // more aggressive at removing them.
1203 llvm::erase_if(sectionCommands, [&](SectionCommand *cmd) { return !cmd; });
1204 }
1205
adjustSectionsAfterSorting()1206 void LinkerScript::adjustSectionsAfterSorting() {
1207 // Try and find an appropriate memory region to assign offsets in.
1208 MemoryRegion *hint = nullptr;
1209 for (SectionCommand *cmd : sectionCommands) {
1210 if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
1211 OutputSection *sec = &osd->osec;
1212 if (!sec->lmaRegionName.empty()) {
1213 if (MemoryRegion *m = memoryRegions.lookup(sec->lmaRegionName))
1214 sec->lmaRegion = m;
1215 else
1216 error("memory region '" + sec->lmaRegionName + "' not declared");
1217 }
1218 std::tie(sec->memRegion, hint) = findMemoryRegion(sec, hint);
1219 }
1220 }
1221
1222 // If output section command doesn't specify any segments,
1223 // and we haven't previously assigned any section to segment,
1224 // then we simply assign section to the very first load segment.
1225 // Below is an example of such linker script:
1226 // PHDRS { seg PT_LOAD; }
1227 // SECTIONS { .aaa : { *(.aaa) } }
1228 SmallVector<StringRef, 0> defPhdrs;
1229 auto firstPtLoad = llvm::find_if(phdrsCommands, [](const PhdrsCommand &cmd) {
1230 return cmd.type == PT_LOAD;
1231 });
1232 if (firstPtLoad != phdrsCommands.end())
1233 defPhdrs.push_back(firstPtLoad->name);
1234
1235 // Walk the commands and propagate the program headers to commands that don't
1236 // explicitly specify them.
1237 for (SectionCommand *cmd : sectionCommands)
1238 if (auto *osd = dyn_cast<OutputDesc>(cmd))
1239 maybePropagatePhdrs(osd->osec, defPhdrs);
1240 }
1241
computeBase(uint64_t min,bool allocateHeaders)1242 static uint64_t computeBase(uint64_t min, bool allocateHeaders) {
1243 // If there is no SECTIONS or if the linkerscript is explicit about program
1244 // headers, do our best to allocate them.
1245 if (!script->hasSectionsCommand || allocateHeaders)
1246 return 0;
1247 // Otherwise only allocate program headers if that would not add a page.
1248 return alignDown(min, config->maxPageSize);
1249 }
1250
1251 // When the SECTIONS command is used, try to find an address for the file and
1252 // program headers output sections, which can be added to the first PT_LOAD
1253 // segment when program headers are created.
1254 //
1255 // We check if the headers fit below the first allocated section. If there isn't
1256 // enough space for these sections, we'll remove them from the PT_LOAD segment,
1257 // and we'll also remove the PT_PHDR segment.
allocateHeaders(SmallVector<PhdrEntry *,0> & phdrs)1258 void LinkerScript::allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs) {
1259 uint64_t min = std::numeric_limits<uint64_t>::max();
1260 for (OutputSection *sec : outputSections)
1261 if (sec->flags & SHF_ALLOC)
1262 min = std::min<uint64_t>(min, sec->addr);
1263
1264 auto it = llvm::find_if(
1265 phdrs, [](const PhdrEntry *e) { return e->p_type == PT_LOAD; });
1266 if (it == phdrs.end())
1267 return;
1268 PhdrEntry *firstPTLoad = *it;
1269
1270 bool hasExplicitHeaders =
1271 llvm::any_of(phdrsCommands, [](const PhdrsCommand &cmd) {
1272 return cmd.hasPhdrs || cmd.hasFilehdr;
1273 });
1274 bool paged = !config->omagic && !config->nmagic;
1275 uint64_t headerSize = getHeaderSize();
1276 if ((paged || hasExplicitHeaders) &&
1277 headerSize <= min - computeBase(min, hasExplicitHeaders)) {
1278 min = alignDown(min - headerSize, config->maxPageSize);
1279 Out::elfHeader->addr = min;
1280 Out::programHeaders->addr = min + Out::elfHeader->size;
1281 return;
1282 }
1283
1284 // Error if we were explicitly asked to allocate headers.
1285 if (hasExplicitHeaders)
1286 error("could not allocate headers");
1287
1288 Out::elfHeader->ptLoad = nullptr;
1289 Out::programHeaders->ptLoad = nullptr;
1290 firstPTLoad->firstSec = findFirstSection(firstPTLoad);
1291
1292 llvm::erase_if(phdrs,
1293 [](const PhdrEntry *e) { return e->p_type == PT_PHDR; });
1294 }
1295
AddressState()1296 LinkerScript::AddressState::AddressState() {
1297 for (auto &mri : script->memoryRegions) {
1298 MemoryRegion *mr = mri.second;
1299 mr->curPos = (mr->origin)().getValue();
1300 }
1301 }
1302
1303 // Here we assign addresses as instructed by linker script SECTIONS
1304 // sub-commands. Doing that allows us to use final VA values, so here
1305 // we also handle rest commands like symbol assignments and ASSERTs.
1306 // Returns a symbol that has changed its section or value, or nullptr if no
1307 // symbol has changed.
assignAddresses()1308 const Defined *LinkerScript::assignAddresses() {
1309 if (script->hasSectionsCommand) {
1310 // With a linker script, assignment of addresses to headers is covered by
1311 // allocateHeaders().
1312 dot = config->imageBase.value_or(0);
1313 } else {
1314 // Assign addresses to headers right now.
1315 dot = target->getImageBase();
1316 Out::elfHeader->addr = dot;
1317 Out::programHeaders->addr = dot + Out::elfHeader->size;
1318 dot += getHeaderSize();
1319 }
1320
1321 AddressState st;
1322 state = &st;
1323 errorOnMissingSection = true;
1324 st.outSec = aether;
1325
1326 SymbolAssignmentMap oldValues = getSymbolAssignmentValues(sectionCommands);
1327 for (SectionCommand *cmd : sectionCommands) {
1328 if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
1329 assign->addr = dot;
1330 assignSymbol(assign, false);
1331 assign->size = dot - assign->addr;
1332 continue;
1333 }
1334 assignOffsets(&cast<OutputDesc>(cmd)->osec);
1335 }
1336
1337 state = nullptr;
1338 return getChangedSymbolAssignment(oldValues);
1339 }
1340
1341 // Creates program headers as instructed by PHDRS linker script command.
createPhdrs()1342 SmallVector<PhdrEntry *, 0> LinkerScript::createPhdrs() {
1343 SmallVector<PhdrEntry *, 0> ret;
1344
1345 // Process PHDRS and FILEHDR keywords because they are not
1346 // real output sections and cannot be added in the following loop.
1347 for (const PhdrsCommand &cmd : phdrsCommands) {
1348 PhdrEntry *phdr = make<PhdrEntry>(cmd.type, cmd.flags.value_or(PF_R));
1349
1350 if (cmd.hasFilehdr)
1351 phdr->add(Out::elfHeader);
1352 if (cmd.hasPhdrs)
1353 phdr->add(Out::programHeaders);
1354
1355 if (cmd.lmaExpr) {
1356 phdr->p_paddr = cmd.lmaExpr().getValue();
1357 phdr->hasLMA = true;
1358 }
1359 ret.push_back(phdr);
1360 }
1361
1362 // Add output sections to program headers.
1363 for (OutputSection *sec : outputSections) {
1364 // Assign headers specified by linker script
1365 for (size_t id : getPhdrIndices(sec)) {
1366 ret[id]->add(sec);
1367 if (!phdrsCommands[id].flags)
1368 ret[id]->p_flags |= sec->getPhdrFlags();
1369 }
1370 }
1371 return ret;
1372 }
1373
1374 // Returns true if we should emit an .interp section.
1375 //
1376 // We usually do. But if PHDRS commands are given, and
1377 // no PT_INTERP is there, there's no place to emit an
1378 // .interp, so we don't do that in that case.
needsInterpSection()1379 bool LinkerScript::needsInterpSection() {
1380 if (phdrsCommands.empty())
1381 return true;
1382 for (PhdrsCommand &cmd : phdrsCommands)
1383 if (cmd.type == PT_INTERP)
1384 return true;
1385 return false;
1386 }
1387
getSymbolValue(StringRef name,const Twine & loc)1388 ExprValue LinkerScript::getSymbolValue(StringRef name, const Twine &loc) {
1389 if (name == ".") {
1390 if (state)
1391 return {state->outSec, false, dot - state->outSec->addr, loc};
1392 error(loc + ": unable to get location counter value");
1393 return 0;
1394 }
1395
1396 if (Symbol *sym = symtab.find(name)) {
1397 if (auto *ds = dyn_cast<Defined>(sym)) {
1398 ExprValue v{ds->section, false, ds->value, loc};
1399 // Retain the original st_type, so that the alias will get the same
1400 // behavior in relocation processing. Any operation will reset st_type to
1401 // STT_NOTYPE.
1402 v.type = ds->type;
1403 return v;
1404 }
1405 if (isa<SharedSymbol>(sym))
1406 if (!errorOnMissingSection)
1407 return {nullptr, false, 0, loc};
1408 }
1409
1410 error(loc + ": symbol not found: " + name);
1411 return 0;
1412 }
1413
1414 // Returns the index of the segment named Name.
getPhdrIndex(ArrayRef<PhdrsCommand> vec,StringRef name)1415 static std::optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> vec,
1416 StringRef name) {
1417 for (size_t i = 0; i < vec.size(); ++i)
1418 if (vec[i].name == name)
1419 return i;
1420 return std::nullopt;
1421 }
1422
1423 // Returns indices of ELF headers containing specific section. Each index is a
1424 // zero based number of ELF header listed within PHDRS {} script block.
getPhdrIndices(OutputSection * cmd)1425 SmallVector<size_t, 0> LinkerScript::getPhdrIndices(OutputSection *cmd) {
1426 SmallVector<size_t, 0> ret;
1427
1428 for (StringRef s : cmd->phdrs) {
1429 if (std::optional<size_t> idx = getPhdrIndex(phdrsCommands, s))
1430 ret.push_back(*idx);
1431 else if (s != "NONE")
1432 error(cmd->location + ": program header '" + s +
1433 "' is not listed in PHDRS");
1434 }
1435 return ret;
1436 }
1437