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"
2455b169bfSRafael Espindola #include "Threads.h"
25bbe38602SEugene Leviant #include "Writer.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.
222b4c9b81aSRafael Espindola static bool matchConstraints(ArrayRef<InputSectionBase *> Sections,
22306ae6836SGeorge Rimar                              ConstraintKind Kind) {
2248f66df92SGeorge Rimar   if (Kind == ConstraintKind::NoConstraint)
2258f66df92SGeorge Rimar     return true;
2262c7171bfSRui Ueyama 
227*b801441eSRui Ueyama   bool IsRW = llvm::any_of(
228*b801441eSRui Ueyama       Sections, [](InputSectionBase *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 
2346a1aa8d9SRafael Espindola static void sortSections(InputSection **Begin, InputSection **End,
235ee924709SRui Ueyama                          SortSectionPolicy K) {
236ee924709SRui Ueyama   if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
23707171f21SGeorge Rimar     std::stable_sort(Begin, End, getComparator(K));
238ee924709SRui Ueyama }
239ee924709SRui Ueyama 
240d6bcde38SGeorge Rimar static void sortBySymbolOrder(InputSection **Begin, InputSection **End) {
241d6bcde38SGeorge Rimar   if (Config->SymbolOrderingFile.empty())
242d6bcde38SGeorge Rimar     return;
243696a7f9aSGeorge Rimar   static llvm::DenseMap<SectionBase *, int> Order = buildSectionOrder();
244d6bcde38SGeorge Rimar   MutableArrayRef<InputSection *> In(Begin, End - Begin);
245d6bcde38SGeorge Rimar   sortByOrder(In, [&](InputSectionBase *S) { return Order.lookup(S); });
246d6bcde38SGeorge Rimar }
247d6bcde38SGeorge Rimar 
248d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches.
2496a1aa8d9SRafael Espindola std::vector<InputSection *>
25072e107f3SRui Ueyama LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
2516a1aa8d9SRafael Espindola   std::vector<InputSection *> Ret;
2528c6a5aafSRui Ueyama 
25372e107f3SRui Ueyama   // Collects all sections that satisfy constraints of Cmd.
25472e107f3SRui Ueyama   for (const SectionPattern &Pat : Cmd->SectionPatterns) {
25572e107f3SRui Ueyama     size_t SizeBefore = Ret.size();
25672e107f3SRui Ueyama 
25772e107f3SRui Ueyama     for (InputSectionBase *Sec : InputSections) {
25872e107f3SRui Ueyama       if (Sec->Assigned)
2598c6a5aafSRui Ueyama         continue;
26072e107f3SRui Ueyama 
261e39709b2SRafael Espindola       if (!Sec->Live) {
262e39709b2SRafael Espindola         reportDiscarded(Sec);
263e39709b2SRafael Espindola         continue;
264e39709b2SRafael Espindola       }
265e39709b2SRafael Espindola 
266908a3d34SRafael Espindola       // For -emit-relocs we have to ignore entries like
267908a3d34SRafael Espindola       //   .rela.dyn : { *(.rela.data) }
268908a3d34SRafael Espindola       // which are common because they are in the default bfd script.
26972e107f3SRui Ueyama       if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
270908a3d34SRafael Espindola         continue;
2718c6a5aafSRui Ueyama 
27204c9ca74SRui Ueyama       std::string Filename = getFilename(Sec->File);
27372e107f3SRui Ueyama       if (!Cmd->FilePat.match(Filename) ||
27472e107f3SRui Ueyama           Pat.ExcludedFilePat.match(Filename) ||
27572e107f3SRui Ueyama           !Pat.SectionPat.match(Sec->Name))
276e0be2901SRui Ueyama         continue;
27772e107f3SRui Ueyama 
2786a1aa8d9SRafael Espindola       Ret.push_back(cast<InputSection>(Sec));
27972e107f3SRui Ueyama       Sec->Assigned = true;
280f94efdddSRui Ueyama     }
281d3190795SRafael Espindola 
282ee924709SRui Ueyama     // Sort sections as instructed by SORT-family commands and --sort-section
283ee924709SRui Ueyama     // option. Because SORT-family commands can be nested at most two depth
284ee924709SRui Ueyama     // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
285ee924709SRui Ueyama     // line option is respected even if a SORT command is given, the exact
286ee924709SRui Ueyama     // behavior we have here is a bit complicated. Here are the rules.
287ee924709SRui Ueyama     //
288ee924709SRui Ueyama     // 1. If two SORT commands are given, --sort-section is ignored.
289ee924709SRui Ueyama     // 2. If one SORT command is given, and if it is not SORT_NONE,
290ee924709SRui Ueyama     //    --sort-section is handled as an inner SORT command.
291ee924709SRui Ueyama     // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
292ee924709SRui Ueyama     // 4. If no SORT command is given, sort according to --sort-section.
293d6bcde38SGeorge Rimar     // 5. If no SORT commands are given and --sort-section is not specified,
294d6bcde38SGeorge Rimar     //    apply sorting provided by --symbol-ordering-file if any exist.
2956a1aa8d9SRafael Espindola     InputSection **Begin = Ret.data() + SizeBefore;
2966a1aa8d9SRafael Espindola     InputSection **End = Ret.data() + Ret.size();
297d6bcde38SGeorge Rimar     if (Pat.SortOuter == SortSectionPolicy::Default &&
298d6bcde38SGeorge Rimar         Config->SortSection == SortSectionPolicy::Default) {
299d6bcde38SGeorge Rimar       sortBySymbolOrder(Begin, End);
300d6bcde38SGeorge Rimar       continue;
301d6bcde38SGeorge Rimar     }
30207171f21SGeorge Rimar     if (Pat.SortOuter != SortSectionPolicy::None) {
30307171f21SGeorge Rimar       if (Pat.SortInner == SortSectionPolicy::Default)
30407171f21SGeorge Rimar         sortSections(Begin, End, Config->SortSection);
305ee924709SRui Ueyama       else
30607171f21SGeorge Rimar         sortSections(Begin, End, Pat.SortInner);
30707171f21SGeorge Rimar       sortSections(Begin, End, Pat.SortOuter);
30807171f21SGeorge Rimar     }
309ee924709SRui Ueyama   }
31072e107f3SRui Ueyama   return Ret;
311be94e1b6SRafael Espindola }
312be94e1b6SRafael Espindola 
313b8dd23f5SRui Ueyama void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
314b4c9b81aSRafael Espindola   for (InputSectionBase *S : V) {
315be94e1b6SRafael Espindola     S->Live = false;
3161e30f07cSDmitry Mikulin     if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab ||
3171e30f07cSDmitry Mikulin         S == InX::DynStrTab)
3182af64b0bSRafael Espindola       error("discarding " + S->Name + " section is not allowed");
319647c1685SGeorge Rimar     discard(S->DependentSections);
320be94e1b6SRafael Espindola   }
321be94e1b6SRafael Espindola }
322be94e1b6SRafael Espindola 
323b4c9b81aSRafael Espindola std::vector<InputSectionBase *>
3248c022ca7SRafael Espindola LinkerScript::createInputSectionList(OutputSection &OutCmd) {
325b4c9b81aSRafael Espindola   std::vector<InputSectionBase *> Ret;
326e7f912cdSRui Ueyama 
3276b394caaSRui Ueyama   for (BaseCommand *Base : OutCmd.SectionCommands) {
3288f99f73cSRui Ueyama     auto *Cmd = dyn_cast<InputSectionDescription>(Base);
3297c3ff2ebSRafael Espindola     if (!Cmd)
3300b9ce6a4SRui Ueyama       continue;
33172e107f3SRui Ueyama 
33272e107f3SRui Ueyama     Cmd->Sections = computeInputSections(Cmd);
333e4c8b9b7SRafael Espindola     Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
3340b9ce6a4SRui Ueyama   }
335e71a3f8aSRafael Espindola 
3360b9ce6a4SRui Ueyama   return Ret;
3370b9ce6a4SRui Ueyama }
3380b9ce6a4SRui Ueyama 
3395908c2f8SRui Ueyama void LinkerScript::processSectionCommands(OutputSectionFactory &Factory) {
3405616adf6SRafael Espindola   // A symbol can be assigned before any section is mentioned in the linker
3415616adf6SRafael Espindola   // script. In an DSO, the symbol values are addresses, so the only important
3425616adf6SRafael Espindola   // section values are:
3435616adf6SRafael Espindola   // * SHN_UNDEF
3445616adf6SRafael Espindola   // * SHN_ABS
3455616adf6SRafael Espindola   // * Any value meaning a regular section.
3465616adf6SRafael Espindola   // To handle that, create a dummy aether section that fills the void before
3475616adf6SRafael Espindola   // the linker scripts switches to another section. It has an index of one
3485616adf6SRafael Espindola   // which will map to whatever the first actual section is.
3495616adf6SRafael Espindola   Aether = make<OutputSection>("", 0, SHF_ALLOC);
3505616adf6SRafael Espindola   Aether->SectionIndex = 1;
35118d19687SRui Ueyama 
35229b240c6SRui Ueyama   // Ctx captures the local AddressState and makes it accessible deliberately.
35329b240c6SRui Ueyama   // This is needed as there are some cases where we cannot just
354c1ace40bSPeter Smith   // thread the current state through to a lambda function created by the
355c1ace40bSPeter Smith   // script parser.
35629b240c6SRui Ueyama   Ctx = make_unique<AddressState>();
35729b240c6SRui Ueyama   Ctx->OutSec = Aether;
3585616adf6SRafael Espindola 
3596b394caaSRui Ueyama   for (size_t I = 0; I < SectionCommands.size(); ++I) {
3600b1b695aSRui Ueyama     // Handle symbol assignments outside of any output section.
3616b394caaSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(SectionCommands[I])) {
3624cd7352cSRafael Espindola       addSymbol(Cmd);
3632ab5f73dSRui Ueyama       continue;
3642ab5f73dSRui Ueyama     }
3650b1b695aSRui Ueyama 
3666b394caaSRui Ueyama     if (auto *Sec = dyn_cast<OutputSection>(SectionCommands[I])) {
3678c022ca7SRafael Espindola       std::vector<InputSectionBase *> V = createInputSectionList(*Sec);
3687bd37870SRafael Espindola 
3690b1b695aSRui Ueyama       // The output section name `/DISCARD/' is special.
3700b1b695aSRui Ueyama       // Any input section assigned to it is discarded.
3718c022ca7SRafael Espindola       if (Sec->Name == "/DISCARD/") {
3727bd37870SRafael Espindola         discard(V);
37348c3f1ceSRui Ueyama         continue;
37448c3f1ceSRui Ueyama       }
3750b9ce6a4SRui Ueyama 
3760b1b695aSRui Ueyama       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
3770b1b695aSRui Ueyama       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
3780b1b695aSRui Ueyama       // sections satisfy a given constraint. If not, a directive is handled
37907d7c42cSGeorge Rimar       // as if it wasn't present from the beginning.
3800b1b695aSRui Ueyama       //
3816b394caaSRui Ueyama       // Because we'll iterate over SectionCommands many more times, the easiest
38207d7c42cSGeorge Rimar       // way to "make it as if it wasn't present" is to just remove it.
3838c022ca7SRafael Espindola       if (!matchConstraints(V, Sec->Constraint)) {
384b4c9b81aSRafael Espindola         for (InputSectionBase *S : V)
385f94efdddSRui Ueyama           S->Assigned = false;
3866b394caaSRui Ueyama         SectionCommands.erase(SectionCommands.begin() + I);
38707d7c42cSGeorge Rimar         --I;
3887c3ff2ebSRafael Espindola         continue;
3897c3ff2ebSRafael Espindola       }
3907c3ff2ebSRafael Espindola 
3910b1b695aSRui Ueyama       // A directive may contain symbol definitions like this:
3920b1b695aSRui Ueyama       // ".foo : { ...; bar = .; }". Handle them.
3936b394caaSRui Ueyama       for (BaseCommand *Base : Sec->SectionCommands)
3948f99f73cSRui Ueyama         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
3954cd7352cSRafael Espindola           addSymbol(OutCmd);
3967c3ff2ebSRafael Espindola 
3970b1b695aSRui Ueyama       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
3980b1b695aSRui Ueyama       // is given, input sections are aligned to that value, whether the
3990b1b695aSRui Ueyama       // given value is larger or smaller than the original section alignment.
4008c022ca7SRafael Espindola       if (Sec->SubalignExpr) {
4018c022ca7SRafael Espindola         uint32_t Subalign = Sec->SubalignExpr().getValue();
402b4c9b81aSRafael Espindola         for (InputSectionBase *S : V)
4030b1b695aSRui Ueyama           S->Alignment = Subalign;
40420d03194SEugene Leviant       }
4050b1b695aSRui Ueyama 
4060b1b695aSRui Ueyama       // Add input sections to an output section.
407d86a4e50SGeorge Rimar       for (InputSectionBase *S : V)
4080e2bfb1eSRui Ueyama         Sec->addSection(cast<InputSection>(S));
409c54d5b16SRui Ueyama 
410660c9ab9SRafael Espindola       assert(Sec->SectionIndex == INT_MAX);
411660c9ab9SRafael Espindola       Sec->SectionIndex = I;
4128c022ca7SRafael Espindola       if (Sec->Noload)
413fbb0463fSGeorge Rimar         Sec->Type = SHT_NOBITS;
41448c3f1ceSRui Ueyama     }
4154aa2ef5bSRafael Espindola   }
41629b240c6SRui Ueyama   Ctx = nullptr;
417db24d9c3SGeorge Rimar }
418e63d81bdSEugene Leviant 
41902ed7575SRafael Espindola void LinkerScript::fabricateDefaultCommands() {
420cbfe9e94SPeter Smith   // Define start address
421761f0b66SRui Ueyama   uint64_t StartAddr = UINT64_MAX;
422cbfe9e94SPeter Smith 
423c60b4510SPeter Smith   // The Sections with -T<section> have been sorted in order of ascending
424c60b4510SPeter Smith   // address. We must lower StartAddr if the lowest -T<section address> as
425c60b4510SPeter Smith   // calls to setDot() must be monotonically increasing.
426c60b4510SPeter Smith   for (auto &KV : Config->SectionStartMap)
427c60b4510SPeter Smith     StartAddr = std::min(StartAddr, KV.second);
428c60b4510SPeter Smith 
429f5db0b36SRui Ueyama   auto Expr = [=] {
430b5ca92efSJames Henderson     return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize());
431f5db0b36SRui Ueyama   };
4326b394caaSRui Ueyama   SectionCommands.insert(SectionCommands.begin(),
4336b394caaSRui Ueyama                          make<SymbolAssignment>(".", Expr, ""));
434cbfe9e94SPeter Smith }
435cbfe9e94SPeter Smith 
436d2f225fdSRui Ueyama static OutputSection *findByName(ArrayRef<BaseCommand *> Vec,
437d2f225fdSRui Ueyama                                  StringRef Name) {
438d2f225fdSRui Ueyama   for (BaseCommand *Base : Vec)
439d2f225fdSRui Ueyama     if (auto *Sec = dyn_cast<OutputSection>(Base))
440d2f225fdSRui Ueyama       if (Sec->Name == Name)
441d2f225fdSRui Ueyama         return Sec;
442d2f225fdSRui Ueyama   return nullptr;
443d2f225fdSRui Ueyama }
444d2f225fdSRui Ueyama 
4450b1b695aSRui Ueyama // Add sections that didn't match any sections command.
446b8dd23f5SRui Ueyama void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
4476b394caaSRui Ueyama   unsigned End = SectionCommands.size();
448d2f225fdSRui Ueyama 
4494f013bb3SRafael Espindola   for (InputSectionBase *S : InputSections) {
450db5e56f7SRafael Espindola     if (!S->Live || S->Parent)
4514f013bb3SRafael Espindola       continue;
452d2f225fdSRui Ueyama 
4534f013bb3SRafael Espindola     StringRef Name = getOutputSectionName(S->Name);
454347c70d7SGeorge Rimar     log(toString(S) + " is being placed in '" + Name + "'");
455d2f225fdSRui Ueyama 
456ac27de9dSRui Ueyama     if (OutputSection *Sec =
4576b394caaSRui Ueyama             findByName(makeArrayRef(SectionCommands).slice(0, End), Name)) {
4580e2bfb1eSRui Ueyama       Sec->addSection(cast<InputSection>(S));
459c54d5b16SRui Ueyama       continue;
460660c9ab9SRafael Espindola     }
461c54d5b16SRui Ueyama 
462c54d5b16SRui Ueyama     if (OutputSection *OS = Factory.addInputSec(S, Name))
4636b394caaSRui Ueyama       SectionCommands.push_back(OS);
464c54d5b16SRui Ueyama     assert(S->getOutputSection()->SectionIndex == INT_MAX);
465d7faa916SRafael Espindola   }
4664f013bb3SRafael Espindola }
467e63d81bdSEugene Leviant 
46871f84067SRui Ueyama uint64_t LinkerScript::advance(uint64_t Size, unsigned Alignment) {
46929b240c6SRui Ueyama   bool IsTbss =
47029b240c6SRui Ueyama       (Ctx->OutSec->Flags & SHF_TLS) && Ctx->OutSec->Type == SHT_NOBITS;
47129b240c6SRui Ueyama   uint64_t Start = IsTbss ? Dot + Ctx->ThreadBssOffset : Dot;
47271f84067SRui Ueyama   Start = alignTo(Start, Alignment);
4737c4eafa3SRafael Espindola   uint64_t End = Start + Size;
4747c4eafa3SRafael Espindola 
4757c4eafa3SRafael Espindola   if (IsTbss)
47629b240c6SRui Ueyama     Ctx->ThreadBssOffset = End - Dot;
4777c4eafa3SRafael Espindola   else
4787c4eafa3SRafael Espindola     Dot = End;
4797c4eafa3SRafael Espindola   return End;
480a940e539SRafael Espindola }
481a940e539SRafael Espindola 
482b8dd23f5SRui Ueyama void LinkerScript::output(InputSection *S) {
483f694d33fSGeorge Rimar   uint64_t Before = advance(0, 1);
4847c4eafa3SRafael Espindola   uint64_t Pos = advance(S->getSize(), S->Alignment);
48529b240c6SRui Ueyama   S->OutSecOff = Pos - S->getSize() - Ctx->OutSec->Addr;
486d3190795SRafael Espindola 
487d3190795SRafael Espindola   // Update output section size after adding each section. This is so that
488d3190795SRafael Espindola   // SIZEOF works correctly in the case below:
489d3190795SRafael Espindola   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
49029b240c6SRui Ueyama   Ctx->OutSec->Size = Pos - Ctx->OutSec->Addr;
491d3190795SRafael Espindola 
492b889744eSMeador Inge   // If there is a memory region associated with this input section, then
493b889744eSMeador Inge   // place the section in that region and update the region index.
49429b240c6SRui Ueyama   if (Ctx->MemRegion) {
49529b240c6SRui Ueyama     uint64_t &CurOffset = Ctx->MemRegionOffset[Ctx->MemRegion];
496f694d33fSGeorge Rimar     CurOffset += Pos - Before;
49729b240c6SRui Ueyama     uint64_t CurSize = CurOffset - Ctx->MemRegion->Origin;
49829b240c6SRui Ueyama     if (CurSize > Ctx->MemRegion->Length) {
49929b240c6SRui Ueyama       uint64_t OverflowAmt = CurSize - Ctx->MemRegion->Length;
50029b240c6SRui Ueyama       error("section '" + Ctx->OutSec->Name + "' will not fit in region '" +
50129b240c6SRui Ueyama             Ctx->MemRegion->Name + "': overflowed by " + Twine(OverflowAmt) +
50229b240c6SRui Ueyama             " bytes");
503b889744eSMeador Inge     }
504b889744eSMeador Inge   }
5052de509c3SRui Ueyama }
506ceabe80eSEugene Leviant 
507b8dd23f5SRui Ueyama void LinkerScript::switchTo(OutputSection *Sec) {
50829b240c6SRui Ueyama   if (Ctx->OutSec == Sec)
509d3190795SRafael Espindola     return;
510d3190795SRafael Espindola 
51129b240c6SRui Ueyama   Ctx->OutSec = Sec;
51229b240c6SRui Ueyama   Ctx->OutSec->Addr = advance(0, Ctx->OutSec->Alignment);
513b71d6f7aSEugene Leviant 
514b71d6f7aSEugene Leviant   // If neither AT nor AT> is specified for an allocatable section, the linker
515b71d6f7aSEugene Leviant   // will set the LMA such that the difference between VMA and LMA for the
516b71d6f7aSEugene Leviant   // section is the same as the preceding output section in the same region
517b71d6f7aSEugene Leviant   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
51829b240c6SRui Ueyama   if (Ctx->LMAOffset)
51929b240c6SRui Ueyama     Ctx->OutSec->LMAOffset = Ctx->LMAOffset();
520d3190795SRafael Espindola }
521d3190795SRafael Espindola 
522b8dd23f5SRui Ueyama void LinkerScript::process(BaseCommand &Base) {
5232e081a4fSRui Ueyama   // This handles the assignments to symbol or to the dot.
5242e081a4fSRui Ueyama   if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) {
5252e081a4fSRui Ueyama     assignSymbol(Cmd, true);
526d3190795SRafael Espindola     return;
52797403d15SEugene Leviant   }
528e38cbab5SGeorge Rimar 
529e38cbab5SGeorge Rimar   // Handle BYTE(), SHORT(), LONG(), or QUAD().
5302e081a4fSRui Ueyama   if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) {
53129b240c6SRui Ueyama     Cmd->Offset = Dot - Ctx->OutSec->Addr;
5322e081a4fSRui Ueyama     Dot += Cmd->Size;
53329b240c6SRui Ueyama     Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr;
534e38cbab5SGeorge Rimar     return;
535e38cbab5SGeorge Rimar   }
536e38cbab5SGeorge Rimar 
5372e081a4fSRui Ueyama   // Handle ASSERT().
5382e081a4fSRui Ueyama   if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) {
5392e081a4fSRui Ueyama     Cmd->Expression();
540b2d99d6aSMeador Inge     return;
541b2d99d6aSMeador Inge   }
542b2d99d6aSMeador Inge 
5432e081a4fSRui Ueyama   // Handle a single input section description command.
5442e081a4fSRui Ueyama   // It calculates and assigns the offsets for each section and also
545e38cbab5SGeorge Rimar   // updates the output section size.
5462e081a4fSRui Ueyama   auto &Cmd = cast<InputSectionDescription>(Base);
547a85e8ddaSRafael Espindola   for (InputSection *Sec : Cmd.Sections) {
5483fb5a6dcSGeorge Rimar     // We tentatively added all synthetic sections at the beginning and removed
5493fb5a6dcSGeorge Rimar     // empty ones afterwards (because there is no way to know whether they were
5503fb5a6dcSGeorge Rimar     // going be empty or not other than actually running linker scripts.)
5513fb5a6dcSGeorge Rimar     // We need to ignore remains of empty sections.
5522e081a4fSRui Ueyama     if (auto *S = dyn_cast<SyntheticSection>(Sec))
5532e081a4fSRui Ueyama       if (S->empty())
5543fb5a6dcSGeorge Rimar         continue;
5553fb5a6dcSGeorge Rimar 
5562e081a4fSRui Ueyama     if (!Sec->Live)
55778ef645fSGeorge Rimar       continue;
55829b240c6SRui Ueyama     assert(Ctx->OutSec == Sec->getParent());
559a85e8ddaSRafael Espindola     output(Sec);
560ceabe80eSEugene Leviant   }
561ceabe80eSEugene Leviant }
562ceabe80eSEugene Leviant 
563b889744eSMeador Inge // This function searches for a memory region to place the given output
564b889744eSMeador Inge // section in. If found, a pointer to the appropriate memory region is
565b889744eSMeador Inge // returned. Otherwise, a nullptr is returned.
5668c022ca7SRafael Espindola MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) {
567b889744eSMeador Inge   // If a memory region name was specified in the output section command,
568b889744eSMeador Inge   // then try to find that region first.
5698c022ca7SRafael Espindola   if (!Sec->MemoryRegionName.empty()) {
570ac27de9dSRui Ueyama     auto It = MemoryRegions.find(Sec->MemoryRegionName);
571ac27de9dSRui Ueyama     if (It != MemoryRegions.end())
5725f37541cSGeorge Rimar       return It->second;
5738c022ca7SRafael Espindola     error("memory region '" + Sec->MemoryRegionName + "' not declared");
574b889744eSMeador Inge     return nullptr;
575b889744eSMeador Inge   }
576b889744eSMeador Inge 
577d7c5400fSRui Ueyama   // If at least one memory region is defined, all sections must
578d7c5400fSRui Ueyama   // belong to some memory region. Otherwise, we don't need to do
579d7c5400fSRui Ueyama   // anything for memory regions.
580ac27de9dSRui Ueyama   if (MemoryRegions.empty())
581b889744eSMeador Inge     return nullptr;
582b889744eSMeador Inge 
583b889744eSMeador Inge   // See if a region can be found by matching section flags.
584ac27de9dSRui Ueyama   for (auto &Pair : MemoryRegions) {
5855f37541cSGeorge Rimar     MemoryRegion *M = Pair.second;
5865f37541cSGeorge Rimar     if ((M->Flags & Sec->Flags) && (M->NegFlags & Sec->Flags) == 0)
5875f37541cSGeorge Rimar       return M;
588b889744eSMeador Inge   }
589b889744eSMeador Inge 
590b889744eSMeador Inge   // Otherwise, no suitable region was found.
591b889744eSMeador Inge   if (Sec->Flags & SHF_ALLOC)
592b889744eSMeador Inge     error("no memory region specified for section '" + Sec->Name + "'");
593b889744eSMeador Inge   return nullptr;
594b889744eSMeador Inge }
595b889744eSMeador Inge 
5960b1b695aSRui Ueyama // This function assigns offsets to input sections and an output section
5970b1b695aSRui Ueyama // for a single sections command (e.g. ".text { *(.text); }").
5988c022ca7SRafael Espindola void LinkerScript::assignOffsets(OutputSection *Sec) {
599dece2808SRafael Espindola   if (!(Sec->Flags & SHF_ALLOC))
600dece2808SRafael Espindola     Dot = 0;
6018c022ca7SRafael Espindola   else if (Sec->AddrExpr)
6028c022ca7SRafael Espindola     setDot(Sec->AddrExpr, Sec->Location, false);
603679828ffSRafael Espindola 
60429b240c6SRui Ueyama   Ctx->MemRegion = Sec->MemRegion;
60529b240c6SRui Ueyama   if (Ctx->MemRegion)
60629b240c6SRui Ueyama     Dot = Ctx->MemRegionOffset[Ctx->MemRegion];
607c2dffe3aSGeorge Rimar 
6088c022ca7SRafael Espindola   if (Sec->LMAExpr) {
6090c1c8085SGeorge Rimar     uint64_t D = Dot;
61029b240c6SRui Ueyama     Ctx->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; };
6115784e96fSEugene Leviant   }
6125784e96fSEugene Leviant 
613b889744eSMeador Inge   switchTo(Sec);
6140b1b695aSRui Ueyama 
615d86a4e50SGeorge Rimar   // We do not support custom layout for compressed debug sectons.
616d86a4e50SGeorge Rimar   // At this point we already know their size and have compressed content.
61729b240c6SRui Ueyama   if (Ctx->OutSec->Flags & SHF_COMPRESSED)
618d86a4e50SGeorge Rimar     return;
619d86a4e50SGeorge Rimar 
6206b394caaSRui Ueyama   for (BaseCommand *C : Sec->SectionCommands)
621de8d9897SRafael Espindola     process(*C);
622d3190795SRafael Espindola }
623d3190795SRafael Espindola 
624b8dd23f5SRui Ueyama void LinkerScript::removeEmptyCommands() {
6256d38e4dbSRafael Espindola   // It is common practice to use very generic linker scripts. So for any
6266d38e4dbSRafael Espindola   // given run some of the output sections in the script will be empty.
6276d38e4dbSRafael Espindola   // We could create corresponding empty output sections, but that would
6286d38e4dbSRafael Espindola   // clutter the output.
6296d38e4dbSRafael Espindola   // We instead remove trivially empty sections. The bfd linker seems even
6306d38e4dbSRafael Espindola   // more aggressive at removing them.
6316b394caaSRui Ueyama   llvm::erase_if(SectionCommands, [&](BaseCommand *Base) {
6328c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Base))
6338c022ca7SRafael Espindola       return !Sec->Live;
6340b1b695aSRui Ueyama     return false;
6356d38e4dbSRafael Espindola   });
63607fe6129SRafael Espindola }
63707fe6129SRafael Espindola 
6388c022ca7SRafael Espindola static bool isAllSectionDescription(const OutputSection &Cmd) {
6396b394caaSRui Ueyama   for (BaseCommand *Base : Cmd.SectionCommands)
6408f99f73cSRui Ueyama     if (!isa<InputSectionDescription>(*Base))
6416a53737cSRafael Espindola       return false;
6426a53737cSRafael Espindola   return true;
6436a53737cSRafael Espindola }
6446d38e4dbSRafael Espindola 
645b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsBeforeSorting() {
6469546fffbSRafael Espindola   // If the output section contains only symbol assignments, create a
6479546fffbSRafael Espindola   // corresponding output section. The bfd linker seems to only create them if
6489546fffbSRafael Espindola   // '.' is assigned to, but creating these section should not have any bad
6499546fffbSRafael Espindola   // consequeces and gives us a section to put the symbol in.
6500c1c8085SGeorge Rimar   uint64_t Flags = SHF_ALLOC;
651660c9ab9SRafael Espindola 
6526b394caaSRui Ueyama   for (BaseCommand *Cmd : SectionCommands) {
6538962db91SGeorge Rimar     auto *Sec = dyn_cast<OutputSection>(Cmd);
6548c022ca7SRafael Espindola     if (!Sec)
6559546fffbSRafael Espindola       continue;
6568c022ca7SRafael Espindola     if (Sec->Live) {
6572b074553SRafael Espindola       Flags = Sec->Flags;
6589546fffbSRafael Espindola       continue;
6599546fffbSRafael Espindola     }
6609546fffbSRafael Espindola 
6618c022ca7SRafael Espindola     if (isAllSectionDescription(*Sec))
6626a53737cSRafael Espindola       continue;
6636a53737cSRafael Espindola 
6648c022ca7SRafael Espindola     Sec->Live = true;
6658c022ca7SRafael Espindola     Sec->Flags = Flags;
6669546fffbSRafael Espindola   }
667f7a17448SRafael Espindola }
668f7a17448SRafael Espindola 
669b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsAfterSorting() {
670feed7506SRafael Espindola   // Try and find an appropriate memory region to assign offsets in.
6716b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
6728c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Base)) {
673ba45584cSGeorge Rimar       if (!Sec->Live)
674ba45584cSGeorge Rimar         continue;
6758c022ca7SRafael Espindola       Sec->MemRegion = findMemoryRegion(Sec);
676d1960dc0SRafael Espindola       // Handle align (e.g. ".foo : ALIGN(16) { ... }").
6778c022ca7SRafael Espindola       if (Sec->AlignExpr)
6788befefb2SRui Ueyama         Sec->Alignment =
6798befefb2SRui Ueyama             std::max<uint32_t>(Sec->Alignment, Sec->AlignExpr().getValue());
680d1960dc0SRafael Espindola     }
681d1960dc0SRafael Espindola   }
682feed7506SRafael Espindola 
683f7a17448SRafael Espindola   // If output section command doesn't specify any segments,
684f7a17448SRafael Espindola   // and we haven't previously assigned any section to segment,
685f7a17448SRafael Espindola   // then we simply assign section to the very first load segment.
686f7a17448SRafael Espindola   // Below is an example of such linker script:
687f7a17448SRafael Espindola   // PHDRS { seg PT_LOAD; }
688f7a17448SRafael Espindola   // SECTIONS { .aaa : { *(.aaa) } }
689f7a17448SRafael Espindola   std::vector<StringRef> DefPhdrs;
690f7a17448SRafael Espindola   auto FirstPtLoad =
691ac27de9dSRui Ueyama       std::find_if(PhdrsCommands.begin(), PhdrsCommands.end(),
692f7a17448SRafael Espindola                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
693ac27de9dSRui Ueyama   if (FirstPtLoad != PhdrsCommands.end())
694f7a17448SRafael Espindola     DefPhdrs.push_back(FirstPtLoad->Name);
695f7a17448SRafael Espindola 
696f7a17448SRafael Espindola   // Walk the commands and propagate the program headers to commands that don't
697f7a17448SRafael Espindola   // explicitly specify them.
6986b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
6998c022ca7SRafael Espindola     auto *Sec = dyn_cast<OutputSection>(Base);
7008c022ca7SRafael Espindola     if (!Sec)
701f7a17448SRafael Espindola       continue;
7028f99f73cSRui Ueyama 
7038c022ca7SRafael Espindola     if (Sec->Phdrs.empty()) {
704a020d348SAndrew Ng       // To match the bfd linker script behaviour, only propagate program
705a020d348SAndrew Ng       // headers to sections that are allocated.
7068c022ca7SRafael Espindola       if (Sec->Flags & SHF_ALLOC)
7078c022ca7SRafael Espindola         Sec->Phdrs = DefPhdrs;
708a020d348SAndrew Ng     } else {
7098c022ca7SRafael Espindola       DefPhdrs = Sec->Phdrs;
710f7a17448SRafael Espindola     }
711a020d348SAndrew Ng   }
7129546fffbSRafael Espindola }
7139546fffbSRafael Espindola 
714582ede89SGeorge Rimar static OutputSection *findFirstSection(PhdrEntry *Load) {
715582ede89SGeorge Rimar   for (OutputSection *Sec : OutputSections)
716582ede89SGeorge Rimar     if (Sec->PtLoad == Load)
717582ede89SGeorge Rimar       return Sec;
718582ede89SGeorge Rimar   return nullptr;
719582ede89SGeorge Rimar }
720582ede89SGeorge Rimar 
721b93c5b9fSPetr Hosek // Try to find an address for the file and program headers output sections,
722b93c5b9fSPetr Hosek // which were unconditionally added to the first PT_LOAD segment earlier.
723b93c5b9fSPetr Hosek //
724b93c5b9fSPetr Hosek // When using the default layout, we check if the headers fit below the first
725b93c5b9fSPetr Hosek // allocated section. When using a linker script, we also check if the headers
726b93c5b9fSPetr Hosek // are covered by the output section. This allows omitting the headers by not
727b93c5b9fSPetr Hosek // leaving enough space for them in the linker script; this pattern is common
728b93c5b9fSPetr Hosek // in embedded systems.
729b93c5b9fSPetr Hosek //
730b93c5b9fSPetr Hosek // If there isn't enough space for these sections, we'll remove them from the
731b93c5b9fSPetr Hosek // PT_LOAD segment, and we'll also remove the PT_PHDR segment.
732aa354187SGeorge Rimar void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) {
7335aedebffSPeter Smith   uint64_t Min = std::numeric_limits<uint64_t>::max();
7348c022ca7SRafael Espindola   for (OutputSection *Sec : OutputSections)
7355aedebffSPeter Smith     if (Sec->Flags & SHF_ALLOC)
7365aedebffSPeter Smith       Min = std::min<uint64_t>(Min, Sec->Addr);
7375aedebffSPeter Smith 
738aa354187SGeorge Rimar   auto It = llvm::find_if(
739aa354187SGeorge Rimar       Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; });
740aa354187SGeorge Rimar   if (It == Phdrs.end())
741d971e703SGeorge Rimar     return;
742aa354187SGeorge Rimar   PhdrEntry *FirstPTLoad = *It;
74302ed7575SRafael Espindola 
74402ed7575SRafael Espindola   uint64_t HeaderSize = getHeaderSize();
745b93c5b9fSPetr Hosek   // When linker script with SECTIONS is being used, don't output headers
746b93c5b9fSPetr Hosek   // unless there's a space for them.
747a323e2a7SRui Ueyama   uint64_t Base = HasSectionsCommand ? alignDown(Min, Config->MaxPageSize) : 0;
748b93c5b9fSPetr Hosek   if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) {
7491f0fe88aSRafael Espindola     Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
75002ed7575SRafael Espindola     Out::ElfHeader->Addr = Min;
75102ed7575SRafael Espindola     Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
752d971e703SGeorge Rimar     return;
75302ed7575SRafael Espindola   }
75402ed7575SRafael Espindola 
755582ede89SGeorge Rimar   Out::ElfHeader->PtLoad = nullptr;
756582ede89SGeorge Rimar   Out::ProgramHeaders->PtLoad = nullptr;
7576823c5f0SGeorge Rimar   FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad);
75802ed7575SRafael Espindola 
75960608a8aSGeorge Rimar   llvm::erase_if(Phdrs,
76060608a8aSGeorge Rimar                  [](const PhdrEntry *E) { return E->p_type == PT_PHDR; });
76102ed7575SRafael Espindola }
76202ed7575SRafael Espindola 
763ac27de9dSRui Ueyama LinkerScript::AddressState::AddressState() {
764ac27de9dSRui Ueyama   for (auto &MRI : Script->MemoryRegions) {
7655f37541cSGeorge Rimar     const MemoryRegion *MR = MRI.second;
766906e9a18SPeter Smith     MemRegionOffset[MR] = MR->Origin;
767906e9a18SPeter Smith   }
768906e9a18SPeter Smith }
769906e9a18SPeter Smith 
7707c18c28cSRui Ueyama // Assign addresses as instructed by linker script SECTIONS sub-commands.
771b5ca92efSJames Henderson void LinkerScript::assignAddresses() {
772b5ca92efSJames Henderson   // By default linker scripts use an initial value of 0 for '.', but prefer
773b5ca92efSJames Henderson   // -image-base if set.
774b5ca92efSJames Henderson   Dot = Config->ImageBase ? *Config->ImageBase : 0;
77518d19687SRui Ueyama 
77629b240c6SRui Ueyama   // Ctx captures the local AddressState and makes it accessible
777c1ace40bSPeter Smith   // deliberately. This is needed as there are some cases where we cannot just
778c1ace40bSPeter Smith   // thread the current state through to a lambda function created by the
779c1ace40bSPeter Smith   // script parser.
78029b240c6SRui Ueyama   Ctx = make_unique<AddressState>();
78172dc195dSRafael Espindola   ErrorOnMissingSection = true;
78206f4743aSRafael Espindola   switchTo(Aether);
78306f4743aSRafael Espindola 
7846b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
7858f99f73cSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
786d379f735SRui Ueyama       assignSymbol(Cmd, false);
78705ef4cffSRui Ueyama       continue;
788652852c5SGeorge Rimar     }
789652852c5SGeorge Rimar 
7908f99f73cSRui Ueyama     if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
7914595df94SRafael Espindola       Cmd->Expression();
792eefa758eSGeorge Rimar       continue;
793eefa758eSGeorge Rimar     }
794eefa758eSGeorge Rimar 
7958c022ca7SRafael Espindola     assignOffsets(cast<OutputSection>(Base));
796a14b13d8SGeorge Rimar   }
79729b240c6SRui Ueyama   Ctx = nullptr;
798fb8978fcSDima Stepanov }
799652852c5SGeorge Rimar 
800464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command.
801aa354187SGeorge Rimar std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
802aa354187SGeorge Rimar   std::vector<PhdrEntry *> Ret;
803bbe38602SEugene Leviant 
804464daadcSRui Ueyama   // Process PHDRS and FILEHDR keywords because they are not
805464daadcSRui Ueyama   // real output sections and cannot be added in the following loop.
806ac27de9dSRui Ueyama   for (const PhdrsCommand &Cmd : PhdrsCommands) {
8070ae2c24cSRui Ueyama     PhdrEntry *Phdr = make<PhdrEntry>(Cmd.Type, Cmd.Flags ? *Cmd.Flags : PF_R);
808bbe38602SEugene Leviant 
809bbe38602SEugene Leviant     if (Cmd.HasFilehdr)
810aa354187SGeorge Rimar       Phdr->add(Out::ElfHeader);
811bbe38602SEugene Leviant     if (Cmd.HasPhdrs)
812aa354187SGeorge Rimar       Phdr->add(Out::ProgramHeaders);
81356b21c86SEugene Leviant 
81456b21c86SEugene Leviant     if (Cmd.LMAExpr) {
815aa354187SGeorge Rimar       Phdr->p_paddr = Cmd.LMAExpr().getValue();
816aa354187SGeorge Rimar       Phdr->HasLMA = true;
81756b21c86SEugene Leviant     }
818aa354187SGeorge Rimar     Ret.push_back(Phdr);
819bbe38602SEugene Leviant   }
820bbe38602SEugene Leviant 
821464daadcSRui Ueyama   // Add output sections to program headers.
8228c022ca7SRafael Espindola   for (OutputSection *Sec : OutputSections) {
823bbe38602SEugene Leviant     // Assign headers specified by linker script
8248c022ca7SRafael Espindola     for (size_t Id : getPhdrIndices(Sec)) {
825aa354187SGeorge Rimar       Ret[Id]->add(Sec);
826ac27de9dSRui Ueyama       if (!PhdrsCommands[Id].Flags.hasValue())
827aa354187SGeorge Rimar         Ret[Id]->p_flags |= Sec->getPhdrFlags();
828bbe38602SEugene Leviant     }
829bbe38602SEugene Leviant   }
830edebbdf1SRui Ueyama   return Ret;
831bbe38602SEugene Leviant }
832bbe38602SEugene Leviant 
833e03ba023SRui Ueyama // Returns true if we should emit an .interp section.
834e03ba023SRui Ueyama //
835e03ba023SRui Ueyama // We usually do. But if PHDRS commands are given, and
836e03ba023SRui Ueyama // no PT_INTERP is there, there's no place to emit an
837e03ba023SRui Ueyama // .interp, so we don't do that in that case.
838e03ba023SRui Ueyama bool LinkerScript::needsInterpSection() {
839ac27de9dSRui Ueyama   if (PhdrsCommands.empty())
840e03ba023SRui Ueyama     return true;
841ac27de9dSRui Ueyama   for (PhdrsCommand &Cmd : PhdrsCommands)
842e31d9886SRui Ueyama     if (Cmd.Type == PT_INTERP)
843e31d9886SRui Ueyama       return true;
844e03ba023SRui Ueyama   return false;
845f9bc3bd2SEugene Leviant }
846f9bc3bd2SEugene Leviant 
847b8dd23f5SRui Ueyama ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
8487e5b0a59SGeorge Rimar   if (S == ".") {
84929b240c6SRui Ueyama     if (Ctx)
85029b240c6SRui Ueyama       return {Ctx->OutSec, false, Dot - Ctx->OutSec->Addr, Loc};
8517e5b0a59SGeorge Rimar     error(Loc + ": unable to get location counter value");
8527e5b0a59SGeorge Rimar     return 0;
8537e5b0a59SGeorge Rimar   }
854244ef981SRafael Espindola   if (SymbolBody *B = Symtab->find(S)) {
85572dc195dSRafael Espindola     if (auto *D = dyn_cast<DefinedRegular>(B))
8564fbe3518SRui Ueyama       return {D->Section, false, D->Value, Loc};
85730f16b23SPetr Hosek     if (auto *C = dyn_cast<DefinedCommon>(B))
8584fbe3518SRui Ueyama       return {C->Section, false, 0, Loc};
85972dc195dSRafael Espindola   }
860f6aeed36SEugene Leviant   error(Loc + ": symbol not found: " + S);
861884e786dSGeorge Rimar   return 0;
862884e786dSGeorge Rimar }
863884e786dSGeorge Rimar 
864656be311SRui Ueyama // Returns the index of the segment named Name.
865656be311SRui Ueyama static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> Vec,
866656be311SRui Ueyama                                      StringRef Name) {
867656be311SRui Ueyama   for (size_t I = 0; I < Vec.size(); ++I)
868656be311SRui Ueyama     if (Vec[I].Name == Name)
869656be311SRui Ueyama       return I;
870656be311SRui Ueyama   return None;
871656be311SRui Ueyama }
872656be311SRui Ueyama 
8732c923c2cSRafael Espindola // Returns indices of ELF headers containing specific section. Each index is a
8742c923c2cSRafael Espindola // zero based number of ELF header listed within PHDRS {} script block.
8758c022ca7SRafael Espindola std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) {
87629c5a2a9SRui Ueyama   std::vector<size_t> Ret;
877656be311SRui Ueyama 
878656be311SRui Ueyama   for (StringRef S : Cmd->Phdrs) {
879ac27de9dSRui Ueyama     if (Optional<size_t> Idx = getPhdrIndex(PhdrsCommands, S))
88087223574SRui Ueyama       Ret.push_back(*Idx);
881656be311SRui Ueyama     else if (S != "NONE")
882656be311SRui Ueyama       error(Cmd->Location + ": section header '" + S +
883656be311SRui Ueyama             "' is not listed in PHDRS");
884bbe38602SEugene Leviant   }
885656be311SRui Ueyama   return Ret;
88629c5a2a9SRui Ueyama }
887