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