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