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