1f7c5fbb1SRui Ueyama //===- LinkerScript.cpp ---------------------------------------------------===//
2f7c5fbb1SRui Ueyama //
3f7c5fbb1SRui Ueyama //                             The LLVM Linker
4f7c5fbb1SRui Ueyama //
5f7c5fbb1SRui Ueyama // This file is distributed under the University of Illinois Open Source
6f7c5fbb1SRui Ueyama // License. See LICENSE.TXT for details.
7f7c5fbb1SRui Ueyama //
8f7c5fbb1SRui Ueyama //===----------------------------------------------------------------------===//
9f7c5fbb1SRui Ueyama //
10f7c5fbb1SRui Ueyama // This file contains the parser/evaluator of the linker script.
11f7c5fbb1SRui Ueyama //
12f7c5fbb1SRui Ueyama //===----------------------------------------------------------------------===//
13f7c5fbb1SRui Ueyama 
14717677afSRui Ueyama #include "LinkerScript.h"
15f7c5fbb1SRui Ueyama #include "Config.h"
161ebc8ed7SRui Ueyama #include "InputSection.h"
179381eb10SRui Ueyama #include "Memory.h"
18652852c5SGeorge Rimar #include "OutputSections.h"
1993c9af42SRui Ueyama #include "Strings.h"
20f7c5fbb1SRui Ueyama #include "SymbolTable.h"
2155518e7dSRui Ueyama #include "Symbols.h"
223fb5a6dcSGeorge Rimar #include "SyntheticSections.h"
2355b169bfSRafael Espindola #include "Target.h"
24bbe38602SEugene Leviant #include "Writer.h"
254f5c8c29SBob Haarman #include "lld/Common/Threads.h"
2622886a28SEugene Zelenko #include "llvm/ADT/STLExtras.h"
2722886a28SEugene Zelenko #include "llvm/ADT/StringRef.h"
28264b5d9eSZachary Turner #include "llvm/BinaryFormat/ELF.h"
2922886a28SEugene Zelenko #include "llvm/Support/Casting.h"
3022886a28SEugene Zelenko #include "llvm/Support/Endian.h"
3122886a28SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
32f7c5fbb1SRui Ueyama #include "llvm/Support/FileSystem.h"
33f03f3cc1SRui Ueyama #include "llvm/Support/Path.h"
3422886a28SEugene Zelenko #include <algorithm>
3522886a28SEugene Zelenko #include <cassert>
3622886a28SEugene Zelenko #include <cstddef>
3722886a28SEugene Zelenko #include <cstdint>
3822886a28SEugene Zelenko #include <iterator>
3922886a28SEugene Zelenko #include <limits>
4022886a28SEugene Zelenko #include <string>
4122886a28SEugene Zelenko #include <vector>
42f7c5fbb1SRui Ueyama 
43f7c5fbb1SRui Ueyama using namespace llvm;
44652852c5SGeorge Rimar using namespace llvm::ELF;
451ebc8ed7SRui Ueyama using namespace llvm::object;
46e38cbab5SGeorge Rimar using namespace llvm::support::endian;
47f7c5fbb1SRui Ueyama using namespace lld;
48e0df00b9SRafael Espindola using namespace lld::elf;
49f7c5fbb1SRui Ueyama 
50a34da938SRui Ueyama LinkerScript *elf::Script;
51a34da938SRui Ueyama 
525d0ea70aSGeorge Rimar static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) {
535d0ea70aSGeorge Rimar   if (OutputSection *OS = InputSec->getOutputSection())
545d0ea70aSGeorge Rimar     return OS->Addr;
555d0ea70aSGeorge Rimar   error(Loc + ": unable to evaluate expression: input section " +
565d0ea70aSGeorge Rimar         InputSec->Name + " has no output section assigned");
575d0ea70aSGeorge Rimar   return 0;
58608cf670SGeorge Rimar }
595d0ea70aSGeorge Rimar 
605d0ea70aSGeorge Rimar uint64_t ExprValue::getValue() const {
615d0ea70aSGeorge Rimar   if (Sec)
625d0ea70aSGeorge Rimar     return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc),
635d0ea70aSGeorge Rimar                    Alignment);
643c6de1a6SPetr Hosek   return alignTo(Val, Alignment);
6572dc195dSRafael Espindola }
6672dc195dSRafael Espindola 
677ba5f47eSRafael Espindola uint64_t ExprValue::getSecAddr() const {
687ba5f47eSRafael Espindola   if (Sec)
695d0ea70aSGeorge Rimar     return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc);
707ba5f47eSRafael Espindola   return 0;
717ba5f47eSRafael Espindola }
727ba5f47eSRafael Espindola 
73a6acd23cSRafael Espindola uint64_t ExprValue::getSectionOffset() const {
748b250344SRafael Espindola   // If the alignment is trivial, we don't have to compute the full
758b250344SRafael Espindola   // value to know the offset. This allows this function to succeed in
768b250344SRafael Espindola   // cases where the output section is not yet known.
778b250344SRafael Espindola   if (Alignment == 1)
788b250344SRafael Espindola     return Val;
79a6acd23cSRafael Espindola   return getValue() - getSecAddr();
80a6acd23cSRafael Espindola }
81a6acd23cSRafael Espindola 
828c022ca7SRafael Espindola OutputSection *LinkerScript::createOutputSection(StringRef Name,
838c022ca7SRafael Espindola                                                  StringRef Location) {
848c022ca7SRafael Espindola   OutputSection *&SecRef = NameToOutputSection[Name];
858c022ca7SRafael Espindola   OutputSection *Sec;
868c022ca7SRafael Espindola   if (SecRef && SecRef->Location.empty()) {
8705c4f67cSRafael Espindola     // There was a forward reference.
888c022ca7SRafael Espindola     Sec = SecRef;
8905c4f67cSRafael Espindola   } else {
908c022ca7SRafael Espindola     Sec = make<OutputSection>(Name, SHT_PROGBITS, 0);
918c022ca7SRafael Espindola     if (!SecRef)
928c022ca7SRafael Espindola       SecRef = Sec;
9305c4f67cSRafael Espindola   }
948c022ca7SRafael Espindola   Sec->Location = Location;
958c022ca7SRafael Espindola   return Sec;
96851dc1e8SGeorge Rimar }
97851dc1e8SGeorge Rimar 
988c022ca7SRafael Espindola OutputSection *LinkerScript::getOrCreateOutputSection(StringRef Name) {
998c022ca7SRafael Espindola   OutputSection *&CmdRef = NameToOutputSection[Name];
10005c4f67cSRafael Espindola   if (!CmdRef)
1018c022ca7SRafael Espindola     CmdRef = make<OutputSection>(Name, SHT_PROGBITS, 0);
10205c4f67cSRafael Espindola   return CmdRef;
103d83ce1b4SGeorge Rimar }
104d83ce1b4SGeorge Rimar 
105b8dd23f5SRui Ueyama void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
10672dc195dSRafael Espindola   uint64_t Val = E().getValue();
1078c804d97SGeorge Rimar   if (Val < Dot && InSec)
1082ee2d2dcSGeorge Rimar     error(Loc + ": unable to move location counter backward for: " +
10929b240c6SRui Ueyama           Ctx->OutSec->Name);
1104cd7352cSRafael Espindola   Dot = Val;
11118d19687SRui Ueyama 
1124cd7352cSRafael Espindola   // Update to location counter means update to section size.
1134cd7352cSRafael Espindola   if (InSec)
11429b240c6SRui Ueyama     Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr;
115679828ffSRafael Espindola }
116679828ffSRafael Espindola 
1175908c2f8SRui Ueyama // This function is called from processSectionCommands,
1185908c2f8SRui Ueyama // while we are fixing the output section layout.
119b8dd23f5SRui Ueyama void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
1201602421cSRui Ueyama   if (Cmd->Name == ".")
1218f1f3c40SMeador Inge     return;
1228f1f3c40SMeador Inge 
1238f1f3c40SMeador Inge   // If a symbol was in PROVIDE(), we need to define it only when
1248f1f3c40SMeador Inge   // it is a referenced undefined symbol.
125244ef981SRafael Espindola   SymbolBody *B = Symtab->find(Cmd->Name);
1268f1f3c40SMeador Inge   if (Cmd->Provide && (!B || B->isDefined()))
1278f1f3c40SMeador Inge     return;
1288f1f3c40SMeador Inge 
12918d19687SRui Ueyama   // Define a symbol.
13018d19687SRui Ueyama   Symbol *Sym;
13118d19687SRui Ueyama   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
13218d19687SRui Ueyama   std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, /*Type*/ 0, Visibility,
13318d19687SRui Ueyama                                               /*CanOmitFromDynSym*/ false,
13418d19687SRui Ueyama                                               /*File*/ nullptr);
13518d19687SRui Ueyama   Sym->Binding = STB_GLOBAL;
13618d19687SRui Ueyama   ExprValue Value = Cmd->Expression();
13718d19687SRui Ueyama   SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
13818d19687SRui Ueyama 
13918d19687SRui Ueyama   // When this function is called, section addresses have not been
14018d19687SRui Ueyama   // fixed yet. So, we may or may not know the value of the RHS
14118d19687SRui Ueyama   // expression.
14218d19687SRui Ueyama   //
14318d19687SRui Ueyama   // For example, if an expression is `x = 42`, we know x is always 42.
14418d19687SRui Ueyama   // However, if an expression is `x = .`, there's no way to know its
14518d19687SRui Ueyama   // value at the moment.
14618d19687SRui Ueyama   //
14718d19687SRui Ueyama   // We want to set symbol values early if we can. This allows us to
14818d19687SRui Ueyama   // use symbols as variables in linker scripts. Doing so allows us to
14918d19687SRui Ueyama   // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
15018d19687SRui Ueyama   uint64_t SymValue = Value.Sec ? 0 : Value.getValue();
15118d19687SRui Ueyama 
15218d19687SRui Ueyama   replaceBody<DefinedRegular>(Sym, nullptr, Cmd->Name, /*IsLocal=*/false,
15318d19687SRui Ueyama                               Visibility, STT_NOTYPE, SymValue, 0, Sec);
15418d19687SRui Ueyama   Cmd->Sym = cast<DefinedRegular>(Sym->body());
15518d19687SRui Ueyama }
15618d19687SRui Ueyama 
15718d19687SRui Ueyama // This function is called from assignAddresses, while we are
15818d19687SRui Ueyama // fixing the output section addresses. This function is supposed
15918d19687SRui Ueyama // to set the final value for a given symbol assignment.
16018d19687SRui Ueyama void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
16118d19687SRui Ueyama   if (Cmd->Name == ".") {
16218d19687SRui Ueyama     setDot(Cmd->Expression, Cmd->Location, InSec);
16318d19687SRui Ueyama     return;
16418d19687SRui Ueyama   }
16518d19687SRui Ueyama 
16618d19687SRui Ueyama   if (!Cmd->Sym)
16718d19687SRui Ueyama     return;
16818d19687SRui Ueyama 
16918d19687SRui Ueyama   ExprValue V = Cmd->Expression();
17018d19687SRui Ueyama   if (V.isAbsolute()) {
17118d19687SRui Ueyama     Cmd->Sym->Section = nullptr;
17218d19687SRui Ueyama     Cmd->Sym->Value = V.getValue();
17318d19687SRui Ueyama   } else {
17418d19687SRui Ueyama     Cmd->Sym->Section = V.Sec;
17518d19687SRui Ueyama     Cmd->Sym->Value = V.getSectionOffset();
17618d19687SRui Ueyama   }
177ceabe80eSEugene Leviant }
178ceabe80eSEugene Leviant 
17904c9ca74SRui Ueyama static std::string getFilename(InputFile *File) {
1801e30f07cSDmitry Mikulin   if (!File)
181e0be2901SRui Ueyama     return "";
1821e30f07cSDmitry Mikulin   if (File->ArchiveName.empty())
1831e30f07cSDmitry Mikulin     return File->getName();
1841e30f07cSDmitry Mikulin   return (File->ArchiveName + "(" + File->getName() + ")").str();
185e0be2901SRui Ueyama }
186e0be2901SRui Ueyama 
187b8dd23f5SRui Ueyama bool LinkerScript::shouldKeep(InputSectionBase *S) {
18804c9ca74SRui Ueyama   std::string Filename = getFilename(S->File);
1897f1c266eSRui Ueyama   for (InputSectionDescription *ID : KeptSections)
190f300ca21SDmitry Mikulin     if (ID->FilePat.match(Filename))
191cf43f179SEugene Leviant       for (SectionPattern &P : ID->SectionPatterns)
192f91282e1SRui Ueyama         if (P.SectionPat.match(S->Name))
193eea3114fSGeorge Rimar           return true;
194eea3114fSGeorge Rimar   return false;
195eea3114fSGeorge Rimar }
196eea3114fSGeorge Rimar 
197ea93fe00SRui Ueyama // A helper function for the SORT() command.
198c404d50dSRafael Espindola static std::function<bool(InputSectionBase *, InputSectionBase *)>
199be394db3SGeorge Rimar getComparator(SortSectionPolicy K) {
200be394db3SGeorge Rimar   switch (K) {
201be394db3SGeorge Rimar   case SortSectionPolicy::Alignment:
202ea93fe00SRui Ueyama     return [](InputSectionBase *A, InputSectionBase *B) {
203ea93fe00SRui Ueyama       // ">" is not a mistake. Sections with larger alignments are placed
204ea93fe00SRui Ueyama       // before sections with smaller alignments in order to reduce the
205ea93fe00SRui Ueyama       // amount of padding necessary. This is compatible with GNU.
206ea93fe00SRui Ueyama       return A->Alignment > B->Alignment;
207ea93fe00SRui Ueyama     };
208be394db3SGeorge Rimar   case SortSectionPolicy::Name:
209ea93fe00SRui Ueyama     return [](InputSectionBase *A, InputSectionBase *B) {
210ea93fe00SRui Ueyama       return A->Name < B->Name;
211ea93fe00SRui Ueyama     };
212be394db3SGeorge Rimar   case SortSectionPolicy::Priority:
213ea93fe00SRui Ueyama     return [](InputSectionBase *A, InputSectionBase *B) {
214ea93fe00SRui Ueyama       return getPriority(A->Name) < getPriority(B->Name);
215ea93fe00SRui Ueyama     };
216be394db3SGeorge Rimar   default:
217be394db3SGeorge Rimar     llvm_unreachable("unknown sort policy");
218be394db3SGeorge Rimar   }
219742c3836SRui Ueyama }
2200702c4e8SGeorge Rimar 
221ea93fe00SRui Ueyama // A helper function for the SORT() command.
22205433431SRui Ueyama static bool matchConstraints(ArrayRef<InputSection *> Sections,
22306ae6836SGeorge Rimar                              ConstraintKind Kind) {
2248f66df92SGeorge Rimar   if (Kind == ConstraintKind::NoConstraint)
2258f66df92SGeorge Rimar     return true;
2262c7171bfSRui Ueyama 
227b801441eSRui Ueyama   bool IsRW = llvm::any_of(
22805433431SRui Ueyama       Sections, [](InputSection *Sec) { return Sec->Flags & SHF_WRITE; });
2292c7171bfSRui Ueyama 
230e746e52cSRafael Espindola   return (IsRW && Kind == ConstraintKind::ReadWrite) ||
231e746e52cSRafael Espindola          (!IsRW && Kind == ConstraintKind::ReadOnly);
23206ae6836SGeorge Rimar }
23306ae6836SGeorge Rimar 
2341f4d7b56SRui Ueyama static void sortSections(MutableArrayRef<InputSection *> Vec,
235ee924709SRui Ueyama                          SortSectionPolicy K) {
236ee924709SRui Ueyama   if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
2371f4d7b56SRui Ueyama     std::stable_sort(Vec.begin(), Vec.end(), getComparator(K));
238ee924709SRui Ueyama }
239ee924709SRui Ueyama 
2407ad1e310SRui Ueyama // Sort sections as instructed by SORT-family commands and --sort-section
2417ad1e310SRui Ueyama // option. Because SORT-family commands can be nested at most two depth
2427ad1e310SRui Ueyama // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
2437ad1e310SRui Ueyama // line option is respected even if a SORT command is given, the exact
2447ad1e310SRui Ueyama // behavior we have here is a bit complicated. Here are the rules.
2457ad1e310SRui Ueyama //
2467ad1e310SRui Ueyama // 1. If two SORT commands are given, --sort-section is ignored.
2477ad1e310SRui Ueyama // 2. If one SORT command is given, and if it is not SORT_NONE,
2487ad1e310SRui Ueyama //    --sort-section is handled as an inner SORT command.
2497ad1e310SRui Ueyama // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
2507ad1e310SRui Ueyama // 4. If no SORT command is given, sort according to --sort-section.
2517ad1e310SRui Ueyama // 5. If no SORT commands are given and --sort-section is not specified,
2527ad1e310SRui Ueyama //    apply sorting provided by --symbol-ordering-file if any exist.
2537ad1e310SRui Ueyama static void sortInputSections(
2547ad1e310SRui Ueyama     MutableArrayRef<InputSection *> Vec, const SectionPattern &Pat,
2557ad1e310SRui Ueyama     const DenseMap<SectionBase *, int> &Order) {
2567ad1e310SRui Ueyama   if (Pat.SortOuter == SortSectionPolicy::None)
2577ad1e310SRui Ueyama     return;
2587ad1e310SRui Ueyama 
2597ad1e310SRui Ueyama   if (Pat.SortOuter == SortSectionPolicy::Default &&
2607ad1e310SRui Ueyama       Config->SortSection == SortSectionPolicy::Default) {
2617ad1e310SRui Ueyama     // If -symbol-ordering-file was given, sort accordingly.
2627ad1e310SRui Ueyama     // Usually, Order is empty.
2637ad1e310SRui Ueyama     if (!Order.empty())
2647ad1e310SRui Ueyama       sortByOrder(Vec, [&](InputSectionBase *S) { return Order.lookup(S); });
2657ad1e310SRui Ueyama     return;
2667ad1e310SRui Ueyama   }
2677ad1e310SRui Ueyama 
2687ad1e310SRui Ueyama   if (Pat.SortInner == SortSectionPolicy::Default)
2697ad1e310SRui Ueyama     sortSections(Vec, Config->SortSection);
2707ad1e310SRui Ueyama   else
2717ad1e310SRui Ueyama     sortSections(Vec, Pat.SortInner);
2727ad1e310SRui Ueyama   sortSections(Vec, Pat.SortOuter);
2737ad1e310SRui Ueyama }
2747ad1e310SRui Ueyama 
275d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches.
2766a1aa8d9SRafael Espindola std::vector<InputSection *>
277849d499eSRafael Espindola LinkerScript::computeInputSections(const InputSectionDescription *Cmd,
278849d499eSRafael Espindola                                    const DenseMap<SectionBase *, int> &Order) {
2796a1aa8d9SRafael Espindola   std::vector<InputSection *> Ret;
2808c6a5aafSRui Ueyama 
28172e107f3SRui Ueyama   // Collects all sections that satisfy constraints of Cmd.
28272e107f3SRui Ueyama   for (const SectionPattern &Pat : Cmd->SectionPatterns) {
28372e107f3SRui Ueyama     size_t SizeBefore = Ret.size();
28472e107f3SRui Ueyama 
28572e107f3SRui Ueyama     for (InputSectionBase *Sec : InputSections) {
28672e107f3SRui Ueyama       if (Sec->Assigned)
2878c6a5aafSRui Ueyama         continue;
28872e107f3SRui Ueyama 
289e39709b2SRafael Espindola       if (!Sec->Live) {
290e39709b2SRafael Espindola         reportDiscarded(Sec);
291e39709b2SRafael Espindola         continue;
292e39709b2SRafael Espindola       }
293e39709b2SRafael Espindola 
294908a3d34SRafael Espindola       // For -emit-relocs we have to ignore entries like
295908a3d34SRafael Espindola       //   .rela.dyn : { *(.rela.data) }
296908a3d34SRafael Espindola       // which are common because they are in the default bfd script.
29772e107f3SRui Ueyama       if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
298908a3d34SRafael Espindola         continue;
2998c6a5aafSRui Ueyama 
30004c9ca74SRui Ueyama       std::string Filename = getFilename(Sec->File);
30172e107f3SRui Ueyama       if (!Cmd->FilePat.match(Filename) ||
30272e107f3SRui Ueyama           Pat.ExcludedFilePat.match(Filename) ||
30372e107f3SRui Ueyama           !Pat.SectionPat.match(Sec->Name))
304e0be2901SRui Ueyama         continue;
30572e107f3SRui Ueyama 
306ec5c4adbSRui Ueyama       // It is safe to assume that Sec is an InputSection
307ec5c4adbSRui Ueyama       // because mergeable or EH input sections have already been
308ec5c4adbSRui Ueyama       // handled and eliminated.
3096a1aa8d9SRafael Espindola       Ret.push_back(cast<InputSection>(Sec));
31072e107f3SRui Ueyama       Sec->Assigned = true;
311f94efdddSRui Ueyama     }
312d3190795SRafael Espindola 
3137ad1e310SRui Ueyama     sortInputSections(MutableArrayRef<InputSection *>(Ret).slice(SizeBefore),
3147ad1e310SRui Ueyama                       Pat, Order);
315ee924709SRui Ueyama   }
31672e107f3SRui Ueyama   return Ret;
317be94e1b6SRafael Espindola }
318be94e1b6SRafael Espindola 
31905433431SRui Ueyama void LinkerScript::discard(ArrayRef<InputSection *> V) {
32005433431SRui Ueyama   for (InputSection *S : V) {
321be94e1b6SRafael Espindola     S->Live = false;
3221e30f07cSDmitry Mikulin     if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab ||
3231e30f07cSDmitry Mikulin         S == InX::DynStrTab)
3242af64b0bSRafael Espindola       error("discarding " + S->Name + " section is not allowed");
325647c1685SGeorge Rimar     discard(S->DependentSections);
326be94e1b6SRafael Espindola   }
327be94e1b6SRafael Espindola }
328be94e1b6SRafael Espindola 
329849d499eSRafael Espindola std::vector<InputSection *> LinkerScript::createInputSectionList(
330849d499eSRafael Espindola     OutputSection &OutCmd, const DenseMap<SectionBase *, int> &Order) {
33105433431SRui Ueyama   std::vector<InputSection *> Ret;
332e7f912cdSRui Ueyama 
3336b394caaSRui Ueyama   for (BaseCommand *Base : OutCmd.SectionCommands) {
33405433431SRui Ueyama     if (auto *Cmd = dyn_cast<InputSectionDescription>(Base)) {
335849d499eSRafael Espindola       Cmd->Sections = computeInputSections(Cmd, Order);
336e4c8b9b7SRafael Espindola       Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
3370b9ce6a4SRui Ueyama     }
33805433431SRui Ueyama   }
3390b9ce6a4SRui Ueyama   return Ret;
3400b9ce6a4SRui Ueyama }
3410b9ce6a4SRui Ueyama 
3423558e24aSRafael Espindola void LinkerScript::processSectionCommands() {
3435616adf6SRafael Espindola   // A symbol can be assigned before any section is mentioned in the linker
3445616adf6SRafael Espindola   // script. In an DSO, the symbol values are addresses, so the only important
3455616adf6SRafael Espindola   // section values are:
3465616adf6SRafael Espindola   // * SHN_UNDEF
3475616adf6SRafael Espindola   // * SHN_ABS
3485616adf6SRafael Espindola   // * Any value meaning a regular section.
3495616adf6SRafael Espindola   // To handle that, create a dummy aether section that fills the void before
3505616adf6SRafael Espindola   // the linker scripts switches to another section. It has an index of one
3515616adf6SRafael Espindola   // which will map to whatever the first actual section is.
3525616adf6SRafael Espindola   Aether = make<OutputSection>("", 0, SHF_ALLOC);
3535616adf6SRafael Espindola   Aether->SectionIndex = 1;
35418d19687SRui Ueyama 
35529b240c6SRui Ueyama   // Ctx captures the local AddressState and makes it accessible deliberately.
35629b240c6SRui Ueyama   // This is needed as there are some cases where we cannot just
357c1ace40bSPeter Smith   // thread the current state through to a lambda function created by the
358c1ace40bSPeter Smith   // script parser.
359*089dac7bSRafael Espindola   auto Deleter = make_unique<AddressState>();
360*089dac7bSRafael Espindola   Ctx = Deleter.get();
36129b240c6SRui Ueyama   Ctx->OutSec = Aether;
3625616adf6SRafael Espindola 
363849d499eSRafael Espindola   DenseMap<SectionBase *, int> Order = buildSectionOrder();
364355a8dd6SRui Ueyama   // Add input sections to output sections.
3656b394caaSRui Ueyama   for (size_t I = 0; I < SectionCommands.size(); ++I) {
3660b1b695aSRui Ueyama     // Handle symbol assignments outside of any output section.
3676b394caaSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(SectionCommands[I])) {
3684cd7352cSRafael Espindola       addSymbol(Cmd);
3692ab5f73dSRui Ueyama       continue;
3702ab5f73dSRui Ueyama     }
3710b1b695aSRui Ueyama 
3726b394caaSRui Ueyama     if (auto *Sec = dyn_cast<OutputSection>(SectionCommands[I])) {
373849d499eSRafael Espindola       std::vector<InputSection *> V = createInputSectionList(*Sec, Order);
3747bd37870SRafael Espindola 
3750b1b695aSRui Ueyama       // The output section name `/DISCARD/' is special.
3760b1b695aSRui Ueyama       // Any input section assigned to it is discarded.
3778c022ca7SRafael Espindola       if (Sec->Name == "/DISCARD/") {
3787bd37870SRafael Espindola         discard(V);
37948c3f1ceSRui Ueyama         continue;
38048c3f1ceSRui Ueyama       }
3810b9ce6a4SRui Ueyama 
3820b1b695aSRui Ueyama       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
3830b1b695aSRui Ueyama       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
3840b1b695aSRui Ueyama       // sections satisfy a given constraint. If not, a directive is handled
38507d7c42cSGeorge Rimar       // as if it wasn't present from the beginning.
3860b1b695aSRui Ueyama       //
3876b394caaSRui Ueyama       // Because we'll iterate over SectionCommands many more times, the easiest
38807d7c42cSGeorge Rimar       // way to "make it as if it wasn't present" is to just remove it.
3898c022ca7SRafael Espindola       if (!matchConstraints(V, Sec->Constraint)) {
390b4c9b81aSRafael Espindola         for (InputSectionBase *S : V)
391f94efdddSRui Ueyama           S->Assigned = false;
3926b394caaSRui Ueyama         SectionCommands.erase(SectionCommands.begin() + I);
39307d7c42cSGeorge Rimar         --I;
3947c3ff2ebSRafael Espindola         continue;
3957c3ff2ebSRafael Espindola       }
3967c3ff2ebSRafael Espindola 
3970b1b695aSRui Ueyama       // A directive may contain symbol definitions like this:
3980b1b695aSRui Ueyama       // ".foo : { ...; bar = .; }". Handle them.
3996b394caaSRui Ueyama       for (BaseCommand *Base : Sec->SectionCommands)
4008f99f73cSRui Ueyama         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
4014cd7352cSRafael Espindola           addSymbol(OutCmd);
4027c3ff2ebSRafael Espindola 
4030b1b695aSRui Ueyama       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
4040b1b695aSRui Ueyama       // is given, input sections are aligned to that value, whether the
4050b1b695aSRui Ueyama       // given value is larger or smaller than the original section alignment.
4068c022ca7SRafael Espindola       if (Sec->SubalignExpr) {
4078c022ca7SRafael Espindola         uint32_t Subalign = Sec->SubalignExpr().getValue();
408b4c9b81aSRafael Espindola         for (InputSectionBase *S : V)
4090b1b695aSRui Ueyama           S->Alignment = Subalign;
41020d03194SEugene Leviant       }
4110b1b695aSRui Ueyama 
4120b1b695aSRui Ueyama       // Add input sections to an output section.
41305433431SRui Ueyama       for (InputSection *S : V)
41405433431SRui Ueyama         Sec->addSection(S);
415355a8dd6SRui Ueyama     }
416355a8dd6SRui Ueyama   }
417355a8dd6SRui Ueyama   Ctx = nullptr;
418c54d5b16SRui Ueyama 
419355a8dd6SRui Ueyama   // Output sections are emitted in the exact same order as
420355a8dd6SRui Ueyama   // appeared in SECTIONS command, so we know their section indices.
421355a8dd6SRui Ueyama   for (size_t I = 0; I < SectionCommands.size(); ++I) {
422355a8dd6SRui Ueyama     auto *Sec = dyn_cast<OutputSection>(SectionCommands[I]);
423355a8dd6SRui Ueyama     if (!Sec)
424355a8dd6SRui Ueyama       continue;
425660c9ab9SRafael Espindola     assert(Sec->SectionIndex == INT_MAX);
426660c9ab9SRafael Espindola     Sec->SectionIndex = I;
4278c022ca7SRafael Espindola     if (Sec->Noload)
428fbb0463fSGeorge Rimar       Sec->Type = SHT_NOBITS;
42948c3f1ceSRui Ueyama   }
4304aa2ef5bSRafael Espindola }
431e63d81bdSEugene Leviant 
4322f4c121dSRui Ueyama // If no SECTIONS command was given, we create simple SectionCommands
4332f4c121dSRui Ueyama // as if a minimum SECTIONS command were given. This function does that.
43402ed7575SRafael Espindola void LinkerScript::fabricateDefaultCommands() {
435cbfe9e94SPeter Smith   // Define start address
436761f0b66SRui Ueyama   uint64_t StartAddr = UINT64_MAX;
437cbfe9e94SPeter Smith 
438c60b4510SPeter Smith   // The Sections with -T<section> have been sorted in order of ascending
439c60b4510SPeter Smith   // address. We must lower StartAddr if the lowest -T<section address> as
440c60b4510SPeter Smith   // calls to setDot() must be monotonically increasing.
441c60b4510SPeter Smith   for (auto &KV : Config->SectionStartMap)
442c60b4510SPeter Smith     StartAddr = std::min(StartAddr, KV.second);
443c60b4510SPeter Smith 
444f5db0b36SRui Ueyama   auto Expr = [=] {
445b5ca92efSJames Henderson     return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize());
446f5db0b36SRui Ueyama   };
4476b394caaSRui Ueyama   SectionCommands.insert(SectionCommands.begin(),
4486b394caaSRui Ueyama                          make<SymbolAssignment>(".", Expr, ""));
449cbfe9e94SPeter Smith }
450cbfe9e94SPeter Smith 
451d2f225fdSRui Ueyama static OutputSection *findByName(ArrayRef<BaseCommand *> Vec,
452d2f225fdSRui Ueyama                                  StringRef Name) {
453d2f225fdSRui Ueyama   for (BaseCommand *Base : Vec)
454d2f225fdSRui Ueyama     if (auto *Sec = dyn_cast<OutputSection>(Base))
455d2f225fdSRui Ueyama       if (Sec->Name == Name)
456d2f225fdSRui Ueyama         return Sec;
457d2f225fdSRui Ueyama   return nullptr;
458d2f225fdSRui Ueyama }
459d2f225fdSRui Ueyama 
4600b1b695aSRui Ueyama // Add sections that didn't match any sections command.
461b8dd23f5SRui Ueyama void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
4626b394caaSRui Ueyama   unsigned End = SectionCommands.size();
463d2f225fdSRui Ueyama 
4644f013bb3SRafael Espindola   for (InputSectionBase *S : InputSections) {
465db5e56f7SRafael Espindola     if (!S->Live || S->Parent)
4664f013bb3SRafael Espindola       continue;
467d2f225fdSRui Ueyama 
4684f013bb3SRafael Espindola     StringRef Name = getOutputSectionName(S->Name);
469347c70d7SGeorge Rimar     log(toString(S) + " is being placed in '" + Name + "'");
470d2f225fdSRui Ueyama 
471ac27de9dSRui Ueyama     if (OutputSection *Sec =
4726b394caaSRui Ueyama             findByName(makeArrayRef(SectionCommands).slice(0, End), Name)) {
4730e2bfb1eSRui Ueyama       Sec->addSection(cast<InputSection>(S));
474c54d5b16SRui Ueyama       continue;
475660c9ab9SRafael Espindola     }
476c54d5b16SRui Ueyama 
477c54d5b16SRui Ueyama     if (OutputSection *OS = Factory.addInputSec(S, Name))
4786b394caaSRui Ueyama       SectionCommands.push_back(OS);
479c54d5b16SRui Ueyama     assert(S->getOutputSection()->SectionIndex == INT_MAX);
480d7faa916SRafael Espindola   }
4814f013bb3SRafael Espindola }
482e63d81bdSEugene Leviant 
48371f84067SRui Ueyama uint64_t LinkerScript::advance(uint64_t Size, unsigned Alignment) {
48429b240c6SRui Ueyama   bool IsTbss =
48529b240c6SRui Ueyama       (Ctx->OutSec->Flags & SHF_TLS) && Ctx->OutSec->Type == SHT_NOBITS;
48629b240c6SRui Ueyama   uint64_t Start = IsTbss ? Dot + Ctx->ThreadBssOffset : Dot;
48771f84067SRui Ueyama   Start = alignTo(Start, Alignment);
4887c4eafa3SRafael Espindola   uint64_t End = Start + Size;
4897c4eafa3SRafael Espindola 
4907c4eafa3SRafael Espindola   if (IsTbss)
49129b240c6SRui Ueyama     Ctx->ThreadBssOffset = End - Dot;
4927c4eafa3SRafael Espindola   else
4937c4eafa3SRafael Espindola     Dot = End;
4947c4eafa3SRafael Espindola   return End;
495a940e539SRafael Espindola }
496a940e539SRafael Espindola 
497b8dd23f5SRui Ueyama void LinkerScript::output(InputSection *S) {
498f694d33fSGeorge Rimar   uint64_t Before = advance(0, 1);
4997c4eafa3SRafael Espindola   uint64_t Pos = advance(S->getSize(), S->Alignment);
50029b240c6SRui Ueyama   S->OutSecOff = Pos - S->getSize() - Ctx->OutSec->Addr;
501d3190795SRafael Espindola 
502d3190795SRafael Espindola   // Update output section size after adding each section. This is so that
503d3190795SRafael Espindola   // SIZEOF works correctly in the case below:
504d3190795SRafael Espindola   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
50529b240c6SRui Ueyama   Ctx->OutSec->Size = Pos - Ctx->OutSec->Addr;
506d3190795SRafael Espindola 
507b889744eSMeador Inge   // If there is a memory region associated with this input section, then
508b889744eSMeador Inge   // place the section in that region and update the region index.
50929b240c6SRui Ueyama   if (Ctx->MemRegion) {
51029b240c6SRui Ueyama     uint64_t &CurOffset = Ctx->MemRegionOffset[Ctx->MemRegion];
511f694d33fSGeorge Rimar     CurOffset += Pos - Before;
51229b240c6SRui Ueyama     uint64_t CurSize = CurOffset - Ctx->MemRegion->Origin;
51329b240c6SRui Ueyama     if (CurSize > Ctx->MemRegion->Length) {
51429b240c6SRui Ueyama       uint64_t OverflowAmt = CurSize - Ctx->MemRegion->Length;
51529b240c6SRui Ueyama       error("section '" + Ctx->OutSec->Name + "' will not fit in region '" +
51629b240c6SRui Ueyama             Ctx->MemRegion->Name + "': overflowed by " + Twine(OverflowAmt) +
51729b240c6SRui Ueyama             " bytes");
518b889744eSMeador Inge     }
519b889744eSMeador Inge   }
5202de509c3SRui Ueyama }
521ceabe80eSEugene Leviant 
522b8dd23f5SRui Ueyama void LinkerScript::switchTo(OutputSection *Sec) {
52329b240c6SRui Ueyama   if (Ctx->OutSec == Sec)
524d3190795SRafael Espindola     return;
525d3190795SRafael Espindola 
52629b240c6SRui Ueyama   Ctx->OutSec = Sec;
52729b240c6SRui Ueyama   Ctx->OutSec->Addr = advance(0, Ctx->OutSec->Alignment);
528b71d6f7aSEugene Leviant 
529b71d6f7aSEugene Leviant   // If neither AT nor AT> is specified for an allocatable section, the linker
530b71d6f7aSEugene Leviant   // will set the LMA such that the difference between VMA and LMA for the
531b71d6f7aSEugene Leviant   // section is the same as the preceding output section in the same region
532b71d6f7aSEugene Leviant   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
53329b240c6SRui Ueyama   if (Ctx->LMAOffset)
53429b240c6SRui Ueyama     Ctx->OutSec->LMAOffset = Ctx->LMAOffset();
535d3190795SRafael Espindola }
536d3190795SRafael Espindola 
537b889744eSMeador Inge // This function searches for a memory region to place the given output
538b889744eSMeador Inge // section in. If found, a pointer to the appropriate memory region is
539b889744eSMeador Inge // returned. Otherwise, a nullptr is returned.
5408c022ca7SRafael Espindola MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) {
541b889744eSMeador Inge   // If a memory region name was specified in the output section command,
542b889744eSMeador Inge   // then try to find that region first.
5438c022ca7SRafael Espindola   if (!Sec->MemoryRegionName.empty()) {
544ac27de9dSRui Ueyama     auto It = MemoryRegions.find(Sec->MemoryRegionName);
545ac27de9dSRui Ueyama     if (It != MemoryRegions.end())
5465f37541cSGeorge Rimar       return It->second;
5478c022ca7SRafael Espindola     error("memory region '" + Sec->MemoryRegionName + "' not declared");
548b889744eSMeador Inge     return nullptr;
549b889744eSMeador Inge   }
550b889744eSMeador Inge 
551d7c5400fSRui Ueyama   // If at least one memory region is defined, all sections must
552d7c5400fSRui Ueyama   // belong to some memory region. Otherwise, we don't need to do
553d7c5400fSRui Ueyama   // anything for memory regions.
554ac27de9dSRui Ueyama   if (MemoryRegions.empty())
555b889744eSMeador Inge     return nullptr;
556b889744eSMeador Inge 
557b889744eSMeador Inge   // See if a region can be found by matching section flags.
558ac27de9dSRui Ueyama   for (auto &Pair : MemoryRegions) {
5595f37541cSGeorge Rimar     MemoryRegion *M = Pair.second;
5605f37541cSGeorge Rimar     if ((M->Flags & Sec->Flags) && (M->NegFlags & Sec->Flags) == 0)
5615f37541cSGeorge Rimar       return M;
562b889744eSMeador Inge   }
563b889744eSMeador Inge 
564b889744eSMeador Inge   // Otherwise, no suitable region was found.
565b889744eSMeador Inge   if (Sec->Flags & SHF_ALLOC)
566b889744eSMeador Inge     error("no memory region specified for section '" + Sec->Name + "'");
567b889744eSMeador Inge   return nullptr;
568b889744eSMeador Inge }
569b889744eSMeador Inge 
5700b1b695aSRui Ueyama // This function assigns offsets to input sections and an output section
5710b1b695aSRui Ueyama // for a single sections command (e.g. ".text { *(.text); }").
5728c022ca7SRafael Espindola void LinkerScript::assignOffsets(OutputSection *Sec) {
573dece2808SRafael Espindola   if (!(Sec->Flags & SHF_ALLOC))
574dece2808SRafael Espindola     Dot = 0;
5758c022ca7SRafael Espindola   else if (Sec->AddrExpr)
5768c022ca7SRafael Espindola     setDot(Sec->AddrExpr, Sec->Location, false);
577679828ffSRafael Espindola 
57829b240c6SRui Ueyama   Ctx->MemRegion = Sec->MemRegion;
57929b240c6SRui Ueyama   if (Ctx->MemRegion)
58029b240c6SRui Ueyama     Dot = Ctx->MemRegionOffset[Ctx->MemRegion];
581c2dffe3aSGeorge Rimar 
5828c022ca7SRafael Espindola   if (Sec->LMAExpr) {
5830c1c8085SGeorge Rimar     uint64_t D = Dot;
58429b240c6SRui Ueyama     Ctx->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; };
5855784e96fSEugene Leviant   }
5865784e96fSEugene Leviant 
587b889744eSMeador Inge   switchTo(Sec);
5880b1b695aSRui Ueyama 
589d86a4e50SGeorge Rimar   // We do not support custom layout for compressed debug sectons.
590d86a4e50SGeorge Rimar   // At this point we already know their size and have compressed content.
59129b240c6SRui Ueyama   if (Ctx->OutSec->Flags & SHF_COMPRESSED)
592d86a4e50SGeorge Rimar     return;
593d86a4e50SGeorge Rimar 
5942f4c121dSRui Ueyama   // We visited SectionsCommands from processSectionCommands to
5952f4c121dSRui Ueyama   // layout sections. Now, we visit SectionsCommands again to fix
5962f4c121dSRui Ueyama   // section offsets.
5972f4c121dSRui Ueyama   for (BaseCommand *Base : Sec->SectionCommands) {
5982f4c121dSRui Ueyama     // This handles the assignments to symbol or to the dot.
5992f4c121dSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
6002f4c121dSRui Ueyama       assignSymbol(Cmd, true);
6012f4c121dSRui Ueyama       continue;
6022f4c121dSRui Ueyama     }
6032f4c121dSRui Ueyama 
6042f4c121dSRui Ueyama     // Handle BYTE(), SHORT(), LONG(), or QUAD().
605f0403c60SRui Ueyama     if (auto *Cmd = dyn_cast<ByteCommand>(Base)) {
6062f4c121dSRui Ueyama       Cmd->Offset = Dot - Ctx->OutSec->Addr;
6072f4c121dSRui Ueyama       Dot += Cmd->Size;
6082f4c121dSRui Ueyama       Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr;
6092f4c121dSRui Ueyama       continue;
6102f4c121dSRui Ueyama     }
6112f4c121dSRui Ueyama 
6122f4c121dSRui Ueyama     // Handle ASSERT().
6132f4c121dSRui Ueyama     if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
6142f4c121dSRui Ueyama       Cmd->Expression();
6152f4c121dSRui Ueyama       continue;
6162f4c121dSRui Ueyama     }
6172f4c121dSRui Ueyama 
6182f4c121dSRui Ueyama     // Handle a single input section description command.
6192f4c121dSRui Ueyama     // It calculates and assigns the offsets for each section and also
6202f4c121dSRui Ueyama     // updates the output section size.
6212f4c121dSRui Ueyama     auto *Cmd = cast<InputSectionDescription>(Base);
6222f4c121dSRui Ueyama     for (InputSection *Sec : Cmd->Sections) {
6232f4c121dSRui Ueyama       // We tentatively added all synthetic sections at the beginning and
6242f4c121dSRui Ueyama       // removed empty ones afterwards (because there is no way to know
6252f4c121dSRui Ueyama       // whether they were going be empty or not other than actually running
6262f4c121dSRui Ueyama       // linker scripts.) We need to ignore remains of empty sections.
6272f4c121dSRui Ueyama       if (auto *S = dyn_cast<SyntheticSection>(Sec))
6282f4c121dSRui Ueyama         if (S->empty())
6292f4c121dSRui Ueyama           continue;
6302f4c121dSRui Ueyama 
6312f4c121dSRui Ueyama       if (!Sec->Live)
6322f4c121dSRui Ueyama         continue;
6332f4c121dSRui Ueyama       assert(Ctx->OutSec == Sec->getParent());
6342f4c121dSRui Ueyama       output(Sec);
6352f4c121dSRui Ueyama     }
6362f4c121dSRui Ueyama   }
637d3190795SRafael Espindola }
638d3190795SRafael Espindola 
639b8dd23f5SRui Ueyama void LinkerScript::removeEmptyCommands() {
6406d38e4dbSRafael Espindola   // It is common practice to use very generic linker scripts. So for any
6416d38e4dbSRafael Espindola   // given run some of the output sections in the script will be empty.
6426d38e4dbSRafael Espindola   // We could create corresponding empty output sections, but that would
6436d38e4dbSRafael Espindola   // clutter the output.
6446d38e4dbSRafael Espindola   // We instead remove trivially empty sections. The bfd linker seems even
6456d38e4dbSRafael Espindola   // more aggressive at removing them.
6466b394caaSRui Ueyama   llvm::erase_if(SectionCommands, [&](BaseCommand *Base) {
6478c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Base))
6488c022ca7SRafael Espindola       return !Sec->Live;
6490b1b695aSRui Ueyama     return false;
6506d38e4dbSRafael Espindola   });
65107fe6129SRafael Espindola }
65207fe6129SRafael Espindola 
6538c022ca7SRafael Espindola static bool isAllSectionDescription(const OutputSection &Cmd) {
6546b394caaSRui Ueyama   for (BaseCommand *Base : Cmd.SectionCommands)
6558f99f73cSRui Ueyama     if (!isa<InputSectionDescription>(*Base))
6566a53737cSRafael Espindola       return false;
6576a53737cSRafael Espindola   return true;
6586a53737cSRafael Espindola }
6596d38e4dbSRafael Espindola 
660b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsBeforeSorting() {
6619546fffbSRafael Espindola   // If the output section contains only symbol assignments, create a
66226fa916dSGeorge Rimar   // corresponding output section. The issue is what to do with linker script
66326fa916dSGeorge Rimar   // like ".foo : { symbol = 42; }". One option would be to convert it to
66426fa916dSGeorge Rimar   // "symbol = 42;". That is, move the symbol out of the empty section
66526fa916dSGeorge Rimar   // description. That seems to be what bfd does for this simple case. The
66626fa916dSGeorge Rimar   // problem is that this is not completely general. bfd will give up and
66726fa916dSGeorge Rimar   // create a dummy section too if there is a ". = . + 1" inside the section
66826fa916dSGeorge Rimar   // for example.
66926fa916dSGeorge Rimar   // Given that we want to create the section, we have to worry what impact
67026fa916dSGeorge Rimar   // it will have on the link. For example, if we just create a section with
67126fa916dSGeorge Rimar   // 0 for flags, it would change which PT_LOADs are created.
67226fa916dSGeorge Rimar   // We could remember that that particular section is dummy and ignore it in
67326fa916dSGeorge Rimar   // other parts of the linker, but unfortunately there are quite a few places
67426fa916dSGeorge Rimar   // that would need to change:
67526fa916dSGeorge Rimar   //   * The program header creation.
67626fa916dSGeorge Rimar   //   * The orphan section placement.
67726fa916dSGeorge Rimar   //   * The address assignment.
67826fa916dSGeorge Rimar   // The other option is to pick flags that minimize the impact the section
67926fa916dSGeorge Rimar   // will have on the rest of the linker. That is why we copy the flags from
68026fa916dSGeorge Rimar   // the previous sections. Only a few flags are needed to keep the impact low.
6810c1c8085SGeorge Rimar   uint64_t Flags = SHF_ALLOC;
682660c9ab9SRafael Espindola 
6836b394caaSRui Ueyama   for (BaseCommand *Cmd : SectionCommands) {
6848962db91SGeorge Rimar     auto *Sec = dyn_cast<OutputSection>(Cmd);
6858c022ca7SRafael Espindola     if (!Sec)
6869546fffbSRafael Espindola       continue;
6878c022ca7SRafael Espindola     if (Sec->Live) {
68826fa916dSGeorge Rimar       Flags = Sec->Flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR);
6899546fffbSRafael Espindola       continue;
6909546fffbSRafael Espindola     }
6919546fffbSRafael Espindola 
6928c022ca7SRafael Espindola     if (isAllSectionDescription(*Sec))
6936a53737cSRafael Espindola       continue;
6946a53737cSRafael Espindola 
6958c022ca7SRafael Espindola     Sec->Live = true;
6968c022ca7SRafael Espindola     Sec->Flags = Flags;
6979546fffbSRafael Espindola   }
698f7a17448SRafael Espindola }
699f7a17448SRafael Espindola 
700b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsAfterSorting() {
701feed7506SRafael Espindola   // Try and find an appropriate memory region to assign offsets in.
7026b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
7038c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Base)) {
704ba45584cSGeorge Rimar       if (!Sec->Live)
705ba45584cSGeorge Rimar         continue;
7068c022ca7SRafael Espindola       Sec->MemRegion = findMemoryRegion(Sec);
707d1960dc0SRafael Espindola       // Handle align (e.g. ".foo : ALIGN(16) { ... }").
7088c022ca7SRafael Espindola       if (Sec->AlignExpr)
7098befefb2SRui Ueyama         Sec->Alignment =
7108befefb2SRui Ueyama             std::max<uint32_t>(Sec->Alignment, Sec->AlignExpr().getValue());
711d1960dc0SRafael Espindola     }
712d1960dc0SRafael Espindola   }
713feed7506SRafael Espindola 
714f7a17448SRafael Espindola   // If output section command doesn't specify any segments,
715f7a17448SRafael Espindola   // and we haven't previously assigned any section to segment,
716f7a17448SRafael Espindola   // then we simply assign section to the very first load segment.
717f7a17448SRafael Espindola   // Below is an example of such linker script:
718f7a17448SRafael Espindola   // PHDRS { seg PT_LOAD; }
719f7a17448SRafael Espindola   // SECTIONS { .aaa : { *(.aaa) } }
720f7a17448SRafael Espindola   std::vector<StringRef> DefPhdrs;
721f7a17448SRafael Espindola   auto FirstPtLoad =
722ac27de9dSRui Ueyama       std::find_if(PhdrsCommands.begin(), PhdrsCommands.end(),
723f7a17448SRafael Espindola                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
724ac27de9dSRui Ueyama   if (FirstPtLoad != PhdrsCommands.end())
725f7a17448SRafael Espindola     DefPhdrs.push_back(FirstPtLoad->Name);
726f7a17448SRafael Espindola 
727f7a17448SRafael Espindola   // Walk the commands and propagate the program headers to commands that don't
728f7a17448SRafael Espindola   // explicitly specify them.
7296b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
7308c022ca7SRafael Espindola     auto *Sec = dyn_cast<OutputSection>(Base);
7318c022ca7SRafael Espindola     if (!Sec)
732f7a17448SRafael Espindola       continue;
7338f99f73cSRui Ueyama 
7348c022ca7SRafael Espindola     if (Sec->Phdrs.empty()) {
735a020d348SAndrew Ng       // To match the bfd linker script behaviour, only propagate program
736a020d348SAndrew Ng       // headers to sections that are allocated.
7378c022ca7SRafael Espindola       if (Sec->Flags & SHF_ALLOC)
7388c022ca7SRafael Espindola         Sec->Phdrs = DefPhdrs;
739a020d348SAndrew Ng     } else {
7408c022ca7SRafael Espindola       DefPhdrs = Sec->Phdrs;
741f7a17448SRafael Espindola     }
742a020d348SAndrew Ng   }
7439546fffbSRafael Espindola }
7449546fffbSRafael Espindola 
745582ede89SGeorge Rimar static OutputSection *findFirstSection(PhdrEntry *Load) {
746582ede89SGeorge Rimar   for (OutputSection *Sec : OutputSections)
747582ede89SGeorge Rimar     if (Sec->PtLoad == Load)
748582ede89SGeorge Rimar       return Sec;
749582ede89SGeorge Rimar   return nullptr;
750582ede89SGeorge Rimar }
751582ede89SGeorge Rimar 
752b93c5b9fSPetr Hosek // Try to find an address for the file and program headers output sections,
753b93c5b9fSPetr Hosek // which were unconditionally added to the first PT_LOAD segment earlier.
754b93c5b9fSPetr Hosek //
755b93c5b9fSPetr Hosek // When using the default layout, we check if the headers fit below the first
756b93c5b9fSPetr Hosek // allocated section. When using a linker script, we also check if the headers
757b93c5b9fSPetr Hosek // are covered by the output section. This allows omitting the headers by not
758b93c5b9fSPetr Hosek // leaving enough space for them in the linker script; this pattern is common
759b93c5b9fSPetr Hosek // in embedded systems.
760b93c5b9fSPetr Hosek //
761b93c5b9fSPetr Hosek // If there isn't enough space for these sections, we'll remove them from the
762b93c5b9fSPetr Hosek // PT_LOAD segment, and we'll also remove the PT_PHDR segment.
763aa354187SGeorge Rimar void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) {
7645aedebffSPeter Smith   uint64_t Min = std::numeric_limits<uint64_t>::max();
7658c022ca7SRafael Espindola   for (OutputSection *Sec : OutputSections)
7665aedebffSPeter Smith     if (Sec->Flags & SHF_ALLOC)
7675aedebffSPeter Smith       Min = std::min<uint64_t>(Min, Sec->Addr);
7685aedebffSPeter Smith 
769aa354187SGeorge Rimar   auto It = llvm::find_if(
770aa354187SGeorge Rimar       Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; });
771aa354187SGeorge Rimar   if (It == Phdrs.end())
772d971e703SGeorge Rimar     return;
773aa354187SGeorge Rimar   PhdrEntry *FirstPTLoad = *It;
77402ed7575SRafael Espindola 
77502ed7575SRafael Espindola   uint64_t HeaderSize = getHeaderSize();
776b93c5b9fSPetr Hosek   // When linker script with SECTIONS is being used, don't output headers
777b93c5b9fSPetr Hosek   // unless there's a space for them.
778a323e2a7SRui Ueyama   uint64_t Base = HasSectionsCommand ? alignDown(Min, Config->MaxPageSize) : 0;
779b93c5b9fSPetr Hosek   if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) {
7801f0fe88aSRafael Espindola     Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
78102ed7575SRafael Espindola     Out::ElfHeader->Addr = Min;
78202ed7575SRafael Espindola     Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
783d971e703SGeorge Rimar     return;
78402ed7575SRafael Espindola   }
78502ed7575SRafael Espindola 
786582ede89SGeorge Rimar   Out::ElfHeader->PtLoad = nullptr;
787582ede89SGeorge Rimar   Out::ProgramHeaders->PtLoad = nullptr;
7886823c5f0SGeorge Rimar   FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad);
78902ed7575SRafael Espindola 
79060608a8aSGeorge Rimar   llvm::erase_if(Phdrs,
79160608a8aSGeorge Rimar                  [](const PhdrEntry *E) { return E->p_type == PT_PHDR; });
79202ed7575SRafael Espindola }
79302ed7575SRafael Espindola 
794ac27de9dSRui Ueyama LinkerScript::AddressState::AddressState() {
795ac27de9dSRui Ueyama   for (auto &MRI : Script->MemoryRegions) {
7965f37541cSGeorge Rimar     const MemoryRegion *MR = MRI.second;
797906e9a18SPeter Smith     MemRegionOffset[MR] = MR->Origin;
798906e9a18SPeter Smith   }
799906e9a18SPeter Smith }
800906e9a18SPeter Smith 
8017c18c28cSRui Ueyama // Assign addresses as instructed by linker script SECTIONS sub-commands.
802b5ca92efSJames Henderson void LinkerScript::assignAddresses() {
803b5ca92efSJames Henderson   // By default linker scripts use an initial value of 0 for '.', but prefer
804b5ca92efSJames Henderson   // -image-base if set.
805b5ca92efSJames Henderson   Dot = Config->ImageBase ? *Config->ImageBase : 0;
80618d19687SRui Ueyama 
807*089dac7bSRafael Espindola   auto Deleter = make_unique<AddressState>();
808*089dac7bSRafael Espindola   Ctx = Deleter.get();
80972dc195dSRafael Espindola   ErrorOnMissingSection = true;
81006f4743aSRafael Espindola   switchTo(Aether);
81106f4743aSRafael Espindola 
8126b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
8138f99f73cSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
814d379f735SRui Ueyama       assignSymbol(Cmd, false);
81505ef4cffSRui Ueyama       continue;
816652852c5SGeorge Rimar     }
817652852c5SGeorge Rimar 
8188f99f73cSRui Ueyama     if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
8194595df94SRafael Espindola       Cmd->Expression();
820eefa758eSGeorge Rimar       continue;
821eefa758eSGeorge Rimar     }
822eefa758eSGeorge Rimar 
8238c022ca7SRafael Espindola     assignOffsets(cast<OutputSection>(Base));
824a14b13d8SGeorge Rimar   }
82529b240c6SRui Ueyama   Ctx = nullptr;
826fb8978fcSDima Stepanov }
827652852c5SGeorge Rimar 
828464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command.
829aa354187SGeorge Rimar std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
830aa354187SGeorge Rimar   std::vector<PhdrEntry *> Ret;
831bbe38602SEugene Leviant 
832464daadcSRui Ueyama   // Process PHDRS and FILEHDR keywords because they are not
833464daadcSRui Ueyama   // real output sections and cannot be added in the following loop.
834ac27de9dSRui Ueyama   for (const PhdrsCommand &Cmd : PhdrsCommands) {
8350ae2c24cSRui Ueyama     PhdrEntry *Phdr = make<PhdrEntry>(Cmd.Type, Cmd.Flags ? *Cmd.Flags : PF_R);
836bbe38602SEugene Leviant 
837bbe38602SEugene Leviant     if (Cmd.HasFilehdr)
838aa354187SGeorge Rimar       Phdr->add(Out::ElfHeader);
839bbe38602SEugene Leviant     if (Cmd.HasPhdrs)
840aa354187SGeorge Rimar       Phdr->add(Out::ProgramHeaders);
84156b21c86SEugene Leviant 
84256b21c86SEugene Leviant     if (Cmd.LMAExpr) {
843aa354187SGeorge Rimar       Phdr->p_paddr = Cmd.LMAExpr().getValue();
844aa354187SGeorge Rimar       Phdr->HasLMA = true;
84556b21c86SEugene Leviant     }
846aa354187SGeorge Rimar     Ret.push_back(Phdr);
847bbe38602SEugene Leviant   }
848bbe38602SEugene Leviant 
849464daadcSRui Ueyama   // Add output sections to program headers.
8508c022ca7SRafael Espindola   for (OutputSection *Sec : OutputSections) {
851bbe38602SEugene Leviant     // Assign headers specified by linker script
8528c022ca7SRafael Espindola     for (size_t Id : getPhdrIndices(Sec)) {
853aa354187SGeorge Rimar       Ret[Id]->add(Sec);
854ac27de9dSRui Ueyama       if (!PhdrsCommands[Id].Flags.hasValue())
855aa354187SGeorge Rimar         Ret[Id]->p_flags |= Sec->getPhdrFlags();
856bbe38602SEugene Leviant     }
857bbe38602SEugene Leviant   }
858edebbdf1SRui Ueyama   return Ret;
859bbe38602SEugene Leviant }
860bbe38602SEugene Leviant 
861e03ba023SRui Ueyama // Returns true if we should emit an .interp section.
862e03ba023SRui Ueyama //
863e03ba023SRui Ueyama // We usually do. But if PHDRS commands are given, and
864e03ba023SRui Ueyama // no PT_INTERP is there, there's no place to emit an
865e03ba023SRui Ueyama // .interp, so we don't do that in that case.
866e03ba023SRui Ueyama bool LinkerScript::needsInterpSection() {
867ac27de9dSRui Ueyama   if (PhdrsCommands.empty())
868e03ba023SRui Ueyama     return true;
869ac27de9dSRui Ueyama   for (PhdrsCommand &Cmd : PhdrsCommands)
870e31d9886SRui Ueyama     if (Cmd.Type == PT_INTERP)
871e31d9886SRui Ueyama       return true;
872e03ba023SRui Ueyama   return false;
873f9bc3bd2SEugene Leviant }
874f9bc3bd2SEugene Leviant 
875722221f5SRui Ueyama ExprValue LinkerScript::getSymbolValue(StringRef Name, const Twine &Loc) {
876722221f5SRui Ueyama   if (Name == ".") {
87729b240c6SRui Ueyama     if (Ctx)
87829b240c6SRui Ueyama       return {Ctx->OutSec, false, Dot - Ctx->OutSec->Addr, Loc};
8797e5b0a59SGeorge Rimar     error(Loc + ": unable to get location counter value");
8807e5b0a59SGeorge Rimar     return 0;
8817e5b0a59SGeorge Rimar   }
882f5733500SRui Ueyama 
883722221f5SRui Ueyama   if (auto *Sym = dyn_cast_or_null<DefinedRegular>(Symtab->find(Name)))
884f5733500SRui Ueyama     return {Sym->Section, false, Sym->Value, Loc};
885f5733500SRui Ueyama 
886722221f5SRui Ueyama   error(Loc + ": symbol not found: " + Name);
887884e786dSGeorge Rimar   return 0;
888884e786dSGeorge Rimar }
889884e786dSGeorge Rimar 
890656be311SRui Ueyama // Returns the index of the segment named Name.
891656be311SRui Ueyama static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> Vec,
892656be311SRui Ueyama                                      StringRef Name) {
893656be311SRui Ueyama   for (size_t I = 0; I < Vec.size(); ++I)
894656be311SRui Ueyama     if (Vec[I].Name == Name)
895656be311SRui Ueyama       return I;
896656be311SRui Ueyama   return None;
897656be311SRui Ueyama }
898656be311SRui Ueyama 
8992c923c2cSRafael Espindola // Returns indices of ELF headers containing specific section. Each index is a
9002c923c2cSRafael Espindola // zero based number of ELF header listed within PHDRS {} script block.
9018c022ca7SRafael Espindola std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) {
90229c5a2a9SRui Ueyama   std::vector<size_t> Ret;
903656be311SRui Ueyama 
904656be311SRui Ueyama   for (StringRef S : Cmd->Phdrs) {
905ac27de9dSRui Ueyama     if (Optional<size_t> Idx = getPhdrIndex(PhdrsCommands, S))
90687223574SRui Ueyama       Ret.push_back(*Idx);
907656be311SRui Ueyama     else if (S != "NONE")
908656be311SRui Ueyama       error(Cmd->Location + ": section header '" + S +
909656be311SRui Ueyama             "' is not listed in PHDRS");
910bbe38602SEugene Leviant   }
911656be311SRui Ueyama   return Ret;
91229c5a2a9SRui Ueyama }
913