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 
227b801441eSRui Ueyama   bool IsRW = llvm::any_of(
228b801441eSRui 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 
278*ec5c4adbSRui Ueyama       // It is safe to assume that Sec is an InputSection
279*ec5c4adbSRui Ueyama       // because mergeable or EH input sections have already been
280*ec5c4adbSRui Ueyama       // handled and eliminated.
2816a1aa8d9SRafael Espindola       Ret.push_back(cast<InputSection>(Sec));
28272e107f3SRui Ueyama       Sec->Assigned = true;
283f94efdddSRui Ueyama     }
284d3190795SRafael Espindola 
285ee924709SRui Ueyama     // Sort sections as instructed by SORT-family commands and --sort-section
286ee924709SRui Ueyama     // option. Because SORT-family commands can be nested at most two depth
287ee924709SRui Ueyama     // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
288ee924709SRui Ueyama     // line option is respected even if a SORT command is given, the exact
289ee924709SRui Ueyama     // behavior we have here is a bit complicated. Here are the rules.
290ee924709SRui Ueyama     //
291ee924709SRui Ueyama     // 1. If two SORT commands are given, --sort-section is ignored.
292ee924709SRui Ueyama     // 2. If one SORT command is given, and if it is not SORT_NONE,
293ee924709SRui Ueyama     //    --sort-section is handled as an inner SORT command.
294ee924709SRui Ueyama     // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
295ee924709SRui Ueyama     // 4. If no SORT command is given, sort according to --sort-section.
296d6bcde38SGeorge Rimar     // 5. If no SORT commands are given and --sort-section is not specified,
297d6bcde38SGeorge Rimar     //    apply sorting provided by --symbol-ordering-file if any exist.
2986a1aa8d9SRafael Espindola     InputSection **Begin = Ret.data() + SizeBefore;
2996a1aa8d9SRafael Espindola     InputSection **End = Ret.data() + Ret.size();
300d6bcde38SGeorge Rimar     if (Pat.SortOuter == SortSectionPolicy::Default &&
301d6bcde38SGeorge Rimar         Config->SortSection == SortSectionPolicy::Default) {
302d6bcde38SGeorge Rimar       sortBySymbolOrder(Begin, End);
303d6bcde38SGeorge Rimar       continue;
304d6bcde38SGeorge Rimar     }
30507171f21SGeorge Rimar     if (Pat.SortOuter != SortSectionPolicy::None) {
30607171f21SGeorge Rimar       if (Pat.SortInner == SortSectionPolicy::Default)
30707171f21SGeorge Rimar         sortSections(Begin, End, Config->SortSection);
308ee924709SRui Ueyama       else
30907171f21SGeorge Rimar         sortSections(Begin, End, Pat.SortInner);
31007171f21SGeorge Rimar       sortSections(Begin, End, Pat.SortOuter);
31107171f21SGeorge Rimar     }
312ee924709SRui Ueyama   }
31372e107f3SRui Ueyama   return Ret;
314be94e1b6SRafael Espindola }
315be94e1b6SRafael Espindola 
316b8dd23f5SRui Ueyama void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
317b4c9b81aSRafael Espindola   for (InputSectionBase *S : V) {
318be94e1b6SRafael Espindola     S->Live = false;
3191e30f07cSDmitry Mikulin     if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab ||
3201e30f07cSDmitry Mikulin         S == InX::DynStrTab)
3212af64b0bSRafael Espindola       error("discarding " + S->Name + " section is not allowed");
322647c1685SGeorge Rimar     discard(S->DependentSections);
323be94e1b6SRafael Espindola   }
324be94e1b6SRafael Espindola }
325be94e1b6SRafael Espindola 
326b4c9b81aSRafael Espindola std::vector<InputSectionBase *>
3278c022ca7SRafael Espindola LinkerScript::createInputSectionList(OutputSection &OutCmd) {
328b4c9b81aSRafael Espindola   std::vector<InputSectionBase *> Ret;
329e7f912cdSRui Ueyama 
3306b394caaSRui Ueyama   for (BaseCommand *Base : OutCmd.SectionCommands) {
3318f99f73cSRui Ueyama     auto *Cmd = dyn_cast<InputSectionDescription>(Base);
3327c3ff2ebSRafael Espindola     if (!Cmd)
3330b9ce6a4SRui Ueyama       continue;
33472e107f3SRui Ueyama 
33572e107f3SRui Ueyama     Cmd->Sections = computeInputSections(Cmd);
336e4c8b9b7SRafael Espindola     Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
3370b9ce6a4SRui Ueyama   }
338e71a3f8aSRafael Espindola 
3390b9ce6a4SRui Ueyama   return Ret;
3400b9ce6a4SRui Ueyama }
3410b9ce6a4SRui Ueyama 
3425908c2f8SRui Ueyama void LinkerScript::processSectionCommands(OutputSectionFactory &Factory) {
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.
35929b240c6SRui Ueyama   Ctx = make_unique<AddressState>();
36029b240c6SRui Ueyama   Ctx->OutSec = Aether;
3615616adf6SRafael Espindola 
3626b394caaSRui Ueyama   for (size_t I = 0; I < SectionCommands.size(); ++I) {
3630b1b695aSRui Ueyama     // Handle symbol assignments outside of any output section.
3646b394caaSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(SectionCommands[I])) {
3654cd7352cSRafael Espindola       addSymbol(Cmd);
3662ab5f73dSRui Ueyama       continue;
3672ab5f73dSRui Ueyama     }
3680b1b695aSRui Ueyama 
3696b394caaSRui Ueyama     if (auto *Sec = dyn_cast<OutputSection>(SectionCommands[I])) {
3708c022ca7SRafael Espindola       std::vector<InputSectionBase *> V = createInputSectionList(*Sec);
3717bd37870SRafael Espindola 
3720b1b695aSRui Ueyama       // The output section name `/DISCARD/' is special.
3730b1b695aSRui Ueyama       // Any input section assigned to it is discarded.
3748c022ca7SRafael Espindola       if (Sec->Name == "/DISCARD/") {
3757bd37870SRafael Espindola         discard(V);
37648c3f1ceSRui Ueyama         continue;
37748c3f1ceSRui Ueyama       }
3780b9ce6a4SRui Ueyama 
3790b1b695aSRui Ueyama       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
3800b1b695aSRui Ueyama       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
3810b1b695aSRui Ueyama       // sections satisfy a given constraint. If not, a directive is handled
38207d7c42cSGeorge Rimar       // as if it wasn't present from the beginning.
3830b1b695aSRui Ueyama       //
3846b394caaSRui Ueyama       // Because we'll iterate over SectionCommands many more times, the easiest
38507d7c42cSGeorge Rimar       // way to "make it as if it wasn't present" is to just remove it.
3868c022ca7SRafael Espindola       if (!matchConstraints(V, Sec->Constraint)) {
387b4c9b81aSRafael Espindola         for (InputSectionBase *S : V)
388f94efdddSRui Ueyama           S->Assigned = false;
3896b394caaSRui Ueyama         SectionCommands.erase(SectionCommands.begin() + I);
39007d7c42cSGeorge Rimar         --I;
3917c3ff2ebSRafael Espindola         continue;
3927c3ff2ebSRafael Espindola       }
3937c3ff2ebSRafael Espindola 
3940b1b695aSRui Ueyama       // A directive may contain symbol definitions like this:
3950b1b695aSRui Ueyama       // ".foo : { ...; bar = .; }". Handle them.
3966b394caaSRui Ueyama       for (BaseCommand *Base : Sec->SectionCommands)
3978f99f73cSRui Ueyama         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
3984cd7352cSRafael Espindola           addSymbol(OutCmd);
3997c3ff2ebSRafael Espindola 
4000b1b695aSRui Ueyama       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
4010b1b695aSRui Ueyama       // is given, input sections are aligned to that value, whether the
4020b1b695aSRui Ueyama       // given value is larger or smaller than the original section alignment.
4038c022ca7SRafael Espindola       if (Sec->SubalignExpr) {
4048c022ca7SRafael Espindola         uint32_t Subalign = Sec->SubalignExpr().getValue();
405b4c9b81aSRafael Espindola         for (InputSectionBase *S : V)
4060b1b695aSRui Ueyama           S->Alignment = Subalign;
40720d03194SEugene Leviant       }
4080b1b695aSRui Ueyama 
4090b1b695aSRui Ueyama       // Add input sections to an output section.
410d86a4e50SGeorge Rimar       for (InputSectionBase *S : V)
4110e2bfb1eSRui Ueyama         Sec->addSection(cast<InputSection>(S));
412c54d5b16SRui Ueyama 
413660c9ab9SRafael Espindola       assert(Sec->SectionIndex == INT_MAX);
414660c9ab9SRafael Espindola       Sec->SectionIndex = I;
4158c022ca7SRafael Espindola       if (Sec->Noload)
416fbb0463fSGeorge Rimar         Sec->Type = SHT_NOBITS;
41748c3f1ceSRui Ueyama     }
4184aa2ef5bSRafael Espindola   }
41929b240c6SRui Ueyama   Ctx = nullptr;
420db24d9c3SGeorge Rimar }
421e63d81bdSEugene Leviant 
42202ed7575SRafael Espindola void LinkerScript::fabricateDefaultCommands() {
423cbfe9e94SPeter Smith   // Define start address
424761f0b66SRui Ueyama   uint64_t StartAddr = UINT64_MAX;
425cbfe9e94SPeter Smith 
426c60b4510SPeter Smith   // The Sections with -T<section> have been sorted in order of ascending
427c60b4510SPeter Smith   // address. We must lower StartAddr if the lowest -T<section address> as
428c60b4510SPeter Smith   // calls to setDot() must be monotonically increasing.
429c60b4510SPeter Smith   for (auto &KV : Config->SectionStartMap)
430c60b4510SPeter Smith     StartAddr = std::min(StartAddr, KV.second);
431c60b4510SPeter Smith 
432f5db0b36SRui Ueyama   auto Expr = [=] {
433b5ca92efSJames Henderson     return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize());
434f5db0b36SRui Ueyama   };
4356b394caaSRui Ueyama   SectionCommands.insert(SectionCommands.begin(),
4366b394caaSRui Ueyama                          make<SymbolAssignment>(".", Expr, ""));
437cbfe9e94SPeter Smith }
438cbfe9e94SPeter Smith 
439d2f225fdSRui Ueyama static OutputSection *findByName(ArrayRef<BaseCommand *> Vec,
440d2f225fdSRui Ueyama                                  StringRef Name) {
441d2f225fdSRui Ueyama   for (BaseCommand *Base : Vec)
442d2f225fdSRui Ueyama     if (auto *Sec = dyn_cast<OutputSection>(Base))
443d2f225fdSRui Ueyama       if (Sec->Name == Name)
444d2f225fdSRui Ueyama         return Sec;
445d2f225fdSRui Ueyama   return nullptr;
446d2f225fdSRui Ueyama }
447d2f225fdSRui Ueyama 
4480b1b695aSRui Ueyama // Add sections that didn't match any sections command.
449b8dd23f5SRui Ueyama void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
4506b394caaSRui Ueyama   unsigned End = SectionCommands.size();
451d2f225fdSRui Ueyama 
4524f013bb3SRafael Espindola   for (InputSectionBase *S : InputSections) {
453db5e56f7SRafael Espindola     if (!S->Live || S->Parent)
4544f013bb3SRafael Espindola       continue;
455d2f225fdSRui Ueyama 
4564f013bb3SRafael Espindola     StringRef Name = getOutputSectionName(S->Name);
457347c70d7SGeorge Rimar     log(toString(S) + " is being placed in '" + Name + "'");
458d2f225fdSRui Ueyama 
459ac27de9dSRui Ueyama     if (OutputSection *Sec =
4606b394caaSRui Ueyama             findByName(makeArrayRef(SectionCommands).slice(0, End), Name)) {
4610e2bfb1eSRui Ueyama       Sec->addSection(cast<InputSection>(S));
462c54d5b16SRui Ueyama       continue;
463660c9ab9SRafael Espindola     }
464c54d5b16SRui Ueyama 
465c54d5b16SRui Ueyama     if (OutputSection *OS = Factory.addInputSec(S, Name))
4666b394caaSRui Ueyama       SectionCommands.push_back(OS);
467c54d5b16SRui Ueyama     assert(S->getOutputSection()->SectionIndex == INT_MAX);
468d7faa916SRafael Espindola   }
4694f013bb3SRafael Espindola }
470e63d81bdSEugene Leviant 
47171f84067SRui Ueyama uint64_t LinkerScript::advance(uint64_t Size, unsigned Alignment) {
47229b240c6SRui Ueyama   bool IsTbss =
47329b240c6SRui Ueyama       (Ctx->OutSec->Flags & SHF_TLS) && Ctx->OutSec->Type == SHT_NOBITS;
47429b240c6SRui Ueyama   uint64_t Start = IsTbss ? Dot + Ctx->ThreadBssOffset : Dot;
47571f84067SRui Ueyama   Start = alignTo(Start, Alignment);
4767c4eafa3SRafael Espindola   uint64_t End = Start + Size;
4777c4eafa3SRafael Espindola 
4787c4eafa3SRafael Espindola   if (IsTbss)
47929b240c6SRui Ueyama     Ctx->ThreadBssOffset = End - Dot;
4807c4eafa3SRafael Espindola   else
4817c4eafa3SRafael Espindola     Dot = End;
4827c4eafa3SRafael Espindola   return End;
483a940e539SRafael Espindola }
484a940e539SRafael Espindola 
485b8dd23f5SRui Ueyama void LinkerScript::output(InputSection *S) {
486f694d33fSGeorge Rimar   uint64_t Before = advance(0, 1);
4877c4eafa3SRafael Espindola   uint64_t Pos = advance(S->getSize(), S->Alignment);
48829b240c6SRui Ueyama   S->OutSecOff = Pos - S->getSize() - Ctx->OutSec->Addr;
489d3190795SRafael Espindola 
490d3190795SRafael Espindola   // Update output section size after adding each section. This is so that
491d3190795SRafael Espindola   // SIZEOF works correctly in the case below:
492d3190795SRafael Espindola   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
49329b240c6SRui Ueyama   Ctx->OutSec->Size = Pos - Ctx->OutSec->Addr;
494d3190795SRafael Espindola 
495b889744eSMeador Inge   // If there is a memory region associated with this input section, then
496b889744eSMeador Inge   // place the section in that region and update the region index.
49729b240c6SRui Ueyama   if (Ctx->MemRegion) {
49829b240c6SRui Ueyama     uint64_t &CurOffset = Ctx->MemRegionOffset[Ctx->MemRegion];
499f694d33fSGeorge Rimar     CurOffset += Pos - Before;
50029b240c6SRui Ueyama     uint64_t CurSize = CurOffset - Ctx->MemRegion->Origin;
50129b240c6SRui Ueyama     if (CurSize > Ctx->MemRegion->Length) {
50229b240c6SRui Ueyama       uint64_t OverflowAmt = CurSize - Ctx->MemRegion->Length;
50329b240c6SRui Ueyama       error("section '" + Ctx->OutSec->Name + "' will not fit in region '" +
50429b240c6SRui Ueyama             Ctx->MemRegion->Name + "': overflowed by " + Twine(OverflowAmt) +
50529b240c6SRui Ueyama             " bytes");
506b889744eSMeador Inge     }
507b889744eSMeador Inge   }
5082de509c3SRui Ueyama }
509ceabe80eSEugene Leviant 
510b8dd23f5SRui Ueyama void LinkerScript::switchTo(OutputSection *Sec) {
51129b240c6SRui Ueyama   if (Ctx->OutSec == Sec)
512d3190795SRafael Espindola     return;
513d3190795SRafael Espindola 
51429b240c6SRui Ueyama   Ctx->OutSec = Sec;
51529b240c6SRui Ueyama   Ctx->OutSec->Addr = advance(0, Ctx->OutSec->Alignment);
516b71d6f7aSEugene Leviant 
517b71d6f7aSEugene Leviant   // If neither AT nor AT> is specified for an allocatable section, the linker
518b71d6f7aSEugene Leviant   // will set the LMA such that the difference between VMA and LMA for the
519b71d6f7aSEugene Leviant   // section is the same as the preceding output section in the same region
520b71d6f7aSEugene Leviant   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
52129b240c6SRui Ueyama   if (Ctx->LMAOffset)
52229b240c6SRui Ueyama     Ctx->OutSec->LMAOffset = Ctx->LMAOffset();
523d3190795SRafael Espindola }
524d3190795SRafael Espindola 
525b8dd23f5SRui Ueyama void LinkerScript::process(BaseCommand &Base) {
5262e081a4fSRui Ueyama   // This handles the assignments to symbol or to the dot.
5272e081a4fSRui Ueyama   if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) {
5282e081a4fSRui Ueyama     assignSymbol(Cmd, true);
529d3190795SRafael Espindola     return;
53097403d15SEugene Leviant   }
531e38cbab5SGeorge Rimar 
532e38cbab5SGeorge Rimar   // Handle BYTE(), SHORT(), LONG(), or QUAD().
5332e081a4fSRui Ueyama   if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) {
53429b240c6SRui Ueyama     Cmd->Offset = Dot - Ctx->OutSec->Addr;
5352e081a4fSRui Ueyama     Dot += Cmd->Size;
53629b240c6SRui Ueyama     Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr;
537e38cbab5SGeorge Rimar     return;
538e38cbab5SGeorge Rimar   }
539e38cbab5SGeorge Rimar 
5402e081a4fSRui Ueyama   // Handle ASSERT().
5412e081a4fSRui Ueyama   if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) {
5422e081a4fSRui Ueyama     Cmd->Expression();
543b2d99d6aSMeador Inge     return;
544b2d99d6aSMeador Inge   }
545b2d99d6aSMeador Inge 
5462e081a4fSRui Ueyama   // Handle a single input section description command.
5472e081a4fSRui Ueyama   // It calculates and assigns the offsets for each section and also
548e38cbab5SGeorge Rimar   // updates the output section size.
5492e081a4fSRui Ueyama   auto &Cmd = cast<InputSectionDescription>(Base);
550a85e8ddaSRafael Espindola   for (InputSection *Sec : Cmd.Sections) {
5513fb5a6dcSGeorge Rimar     // We tentatively added all synthetic sections at the beginning and removed
5523fb5a6dcSGeorge Rimar     // empty ones afterwards (because there is no way to know whether they were
5533fb5a6dcSGeorge Rimar     // going be empty or not other than actually running linker scripts.)
5543fb5a6dcSGeorge Rimar     // We need to ignore remains of empty sections.
5552e081a4fSRui Ueyama     if (auto *S = dyn_cast<SyntheticSection>(Sec))
5562e081a4fSRui Ueyama       if (S->empty())
5573fb5a6dcSGeorge Rimar         continue;
5583fb5a6dcSGeorge Rimar 
5592e081a4fSRui Ueyama     if (!Sec->Live)
56078ef645fSGeorge Rimar       continue;
56129b240c6SRui Ueyama     assert(Ctx->OutSec == Sec->getParent());
562a85e8ddaSRafael Espindola     output(Sec);
563ceabe80eSEugene Leviant   }
564ceabe80eSEugene Leviant }
565ceabe80eSEugene Leviant 
566b889744eSMeador Inge // This function searches for a memory region to place the given output
567b889744eSMeador Inge // section in. If found, a pointer to the appropriate memory region is
568b889744eSMeador Inge // returned. Otherwise, a nullptr is returned.
5698c022ca7SRafael Espindola MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) {
570b889744eSMeador Inge   // If a memory region name was specified in the output section command,
571b889744eSMeador Inge   // then try to find that region first.
5728c022ca7SRafael Espindola   if (!Sec->MemoryRegionName.empty()) {
573ac27de9dSRui Ueyama     auto It = MemoryRegions.find(Sec->MemoryRegionName);
574ac27de9dSRui Ueyama     if (It != MemoryRegions.end())
5755f37541cSGeorge Rimar       return It->second;
5768c022ca7SRafael Espindola     error("memory region '" + Sec->MemoryRegionName + "' not declared");
577b889744eSMeador Inge     return nullptr;
578b889744eSMeador Inge   }
579b889744eSMeador Inge 
580d7c5400fSRui Ueyama   // If at least one memory region is defined, all sections must
581d7c5400fSRui Ueyama   // belong to some memory region. Otherwise, we don't need to do
582d7c5400fSRui Ueyama   // anything for memory regions.
583ac27de9dSRui Ueyama   if (MemoryRegions.empty())
584b889744eSMeador Inge     return nullptr;
585b889744eSMeador Inge 
586b889744eSMeador Inge   // See if a region can be found by matching section flags.
587ac27de9dSRui Ueyama   for (auto &Pair : MemoryRegions) {
5885f37541cSGeorge Rimar     MemoryRegion *M = Pair.second;
5895f37541cSGeorge Rimar     if ((M->Flags & Sec->Flags) && (M->NegFlags & Sec->Flags) == 0)
5905f37541cSGeorge Rimar       return M;
591b889744eSMeador Inge   }
592b889744eSMeador Inge 
593b889744eSMeador Inge   // Otherwise, no suitable region was found.
594b889744eSMeador Inge   if (Sec->Flags & SHF_ALLOC)
595b889744eSMeador Inge     error("no memory region specified for section '" + Sec->Name + "'");
596b889744eSMeador Inge   return nullptr;
597b889744eSMeador Inge }
598b889744eSMeador Inge 
5990b1b695aSRui Ueyama // This function assigns offsets to input sections and an output section
6000b1b695aSRui Ueyama // for a single sections command (e.g. ".text { *(.text); }").
6018c022ca7SRafael Espindola void LinkerScript::assignOffsets(OutputSection *Sec) {
602dece2808SRafael Espindola   if (!(Sec->Flags & SHF_ALLOC))
603dece2808SRafael Espindola     Dot = 0;
6048c022ca7SRafael Espindola   else if (Sec->AddrExpr)
6058c022ca7SRafael Espindola     setDot(Sec->AddrExpr, Sec->Location, false);
606679828ffSRafael Espindola 
60729b240c6SRui Ueyama   Ctx->MemRegion = Sec->MemRegion;
60829b240c6SRui Ueyama   if (Ctx->MemRegion)
60929b240c6SRui Ueyama     Dot = Ctx->MemRegionOffset[Ctx->MemRegion];
610c2dffe3aSGeorge Rimar 
6118c022ca7SRafael Espindola   if (Sec->LMAExpr) {
6120c1c8085SGeorge Rimar     uint64_t D = Dot;
61329b240c6SRui Ueyama     Ctx->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; };
6145784e96fSEugene Leviant   }
6155784e96fSEugene Leviant 
616b889744eSMeador Inge   switchTo(Sec);
6170b1b695aSRui Ueyama 
618d86a4e50SGeorge Rimar   // We do not support custom layout for compressed debug sectons.
619d86a4e50SGeorge Rimar   // At this point we already know their size and have compressed content.
62029b240c6SRui Ueyama   if (Ctx->OutSec->Flags & SHF_COMPRESSED)
621d86a4e50SGeorge Rimar     return;
622d86a4e50SGeorge Rimar 
6236b394caaSRui Ueyama   for (BaseCommand *C : Sec->SectionCommands)
624de8d9897SRafael Espindola     process(*C);
625d3190795SRafael Espindola }
626d3190795SRafael Espindola 
627b8dd23f5SRui Ueyama void LinkerScript::removeEmptyCommands() {
6286d38e4dbSRafael Espindola   // It is common practice to use very generic linker scripts. So for any
6296d38e4dbSRafael Espindola   // given run some of the output sections in the script will be empty.
6306d38e4dbSRafael Espindola   // We could create corresponding empty output sections, but that would
6316d38e4dbSRafael Espindola   // clutter the output.
6326d38e4dbSRafael Espindola   // We instead remove trivially empty sections. The bfd linker seems even
6336d38e4dbSRafael Espindola   // more aggressive at removing them.
6346b394caaSRui Ueyama   llvm::erase_if(SectionCommands, [&](BaseCommand *Base) {
6358c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Base))
6368c022ca7SRafael Espindola       return !Sec->Live;
6370b1b695aSRui Ueyama     return false;
6386d38e4dbSRafael Espindola   });
63907fe6129SRafael Espindola }
64007fe6129SRafael Espindola 
6418c022ca7SRafael Espindola static bool isAllSectionDescription(const OutputSection &Cmd) {
6426b394caaSRui Ueyama   for (BaseCommand *Base : Cmd.SectionCommands)
6438f99f73cSRui Ueyama     if (!isa<InputSectionDescription>(*Base))
6446a53737cSRafael Espindola       return false;
6456a53737cSRafael Espindola   return true;
6466a53737cSRafael Espindola }
6476d38e4dbSRafael Espindola 
648b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsBeforeSorting() {
6499546fffbSRafael Espindola   // If the output section contains only symbol assignments, create a
6509546fffbSRafael Espindola   // corresponding output section. The bfd linker seems to only create them if
6519546fffbSRafael Espindola   // '.' is assigned to, but creating these section should not have any bad
6529546fffbSRafael Espindola   // consequeces and gives us a section to put the symbol in.
6530c1c8085SGeorge Rimar   uint64_t Flags = SHF_ALLOC;
654660c9ab9SRafael Espindola 
6556b394caaSRui Ueyama   for (BaseCommand *Cmd : SectionCommands) {
6568962db91SGeorge Rimar     auto *Sec = dyn_cast<OutputSection>(Cmd);
6578c022ca7SRafael Espindola     if (!Sec)
6589546fffbSRafael Espindola       continue;
6598c022ca7SRafael Espindola     if (Sec->Live) {
6602b074553SRafael Espindola       Flags = Sec->Flags;
6619546fffbSRafael Espindola       continue;
6629546fffbSRafael Espindola     }
6639546fffbSRafael Espindola 
6648c022ca7SRafael Espindola     if (isAllSectionDescription(*Sec))
6656a53737cSRafael Espindola       continue;
6666a53737cSRafael Espindola 
6678c022ca7SRafael Espindola     Sec->Live = true;
6688c022ca7SRafael Espindola     Sec->Flags = Flags;
6699546fffbSRafael Espindola   }
670f7a17448SRafael Espindola }
671f7a17448SRafael Espindola 
672b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsAfterSorting() {
673feed7506SRafael Espindola   // Try and find an appropriate memory region to assign offsets in.
6746b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
6758c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Base)) {
676ba45584cSGeorge Rimar       if (!Sec->Live)
677ba45584cSGeorge Rimar         continue;
6788c022ca7SRafael Espindola       Sec->MemRegion = findMemoryRegion(Sec);
679d1960dc0SRafael Espindola       // Handle align (e.g. ".foo : ALIGN(16) { ... }").
6808c022ca7SRafael Espindola       if (Sec->AlignExpr)
6818befefb2SRui Ueyama         Sec->Alignment =
6828befefb2SRui Ueyama             std::max<uint32_t>(Sec->Alignment, Sec->AlignExpr().getValue());
683d1960dc0SRafael Espindola     }
684d1960dc0SRafael Espindola   }
685feed7506SRafael Espindola 
686f7a17448SRafael Espindola   // If output section command doesn't specify any segments,
687f7a17448SRafael Espindola   // and we haven't previously assigned any section to segment,
688f7a17448SRafael Espindola   // then we simply assign section to the very first load segment.
689f7a17448SRafael Espindola   // Below is an example of such linker script:
690f7a17448SRafael Espindola   // PHDRS { seg PT_LOAD; }
691f7a17448SRafael Espindola   // SECTIONS { .aaa : { *(.aaa) } }
692f7a17448SRafael Espindola   std::vector<StringRef> DefPhdrs;
693f7a17448SRafael Espindola   auto FirstPtLoad =
694ac27de9dSRui Ueyama       std::find_if(PhdrsCommands.begin(), PhdrsCommands.end(),
695f7a17448SRafael Espindola                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
696ac27de9dSRui Ueyama   if (FirstPtLoad != PhdrsCommands.end())
697f7a17448SRafael Espindola     DefPhdrs.push_back(FirstPtLoad->Name);
698f7a17448SRafael Espindola 
699f7a17448SRafael Espindola   // Walk the commands and propagate the program headers to commands that don't
700f7a17448SRafael Espindola   // explicitly specify them.
7016b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
7028c022ca7SRafael Espindola     auto *Sec = dyn_cast<OutputSection>(Base);
7038c022ca7SRafael Espindola     if (!Sec)
704f7a17448SRafael Espindola       continue;
7058f99f73cSRui Ueyama 
7068c022ca7SRafael Espindola     if (Sec->Phdrs.empty()) {
707a020d348SAndrew Ng       // To match the bfd linker script behaviour, only propagate program
708a020d348SAndrew Ng       // headers to sections that are allocated.
7098c022ca7SRafael Espindola       if (Sec->Flags & SHF_ALLOC)
7108c022ca7SRafael Espindola         Sec->Phdrs = DefPhdrs;
711a020d348SAndrew Ng     } else {
7128c022ca7SRafael Espindola       DefPhdrs = Sec->Phdrs;
713f7a17448SRafael Espindola     }
714a020d348SAndrew Ng   }
7159546fffbSRafael Espindola }
7169546fffbSRafael Espindola 
717582ede89SGeorge Rimar static OutputSection *findFirstSection(PhdrEntry *Load) {
718582ede89SGeorge Rimar   for (OutputSection *Sec : OutputSections)
719582ede89SGeorge Rimar     if (Sec->PtLoad == Load)
720582ede89SGeorge Rimar       return Sec;
721582ede89SGeorge Rimar   return nullptr;
722582ede89SGeorge Rimar }
723582ede89SGeorge Rimar 
724b93c5b9fSPetr Hosek // Try to find an address for the file and program headers output sections,
725b93c5b9fSPetr Hosek // which were unconditionally added to the first PT_LOAD segment earlier.
726b93c5b9fSPetr Hosek //
727b93c5b9fSPetr Hosek // When using the default layout, we check if the headers fit below the first
728b93c5b9fSPetr Hosek // allocated section. When using a linker script, we also check if the headers
729b93c5b9fSPetr Hosek // are covered by the output section. This allows omitting the headers by not
730b93c5b9fSPetr Hosek // leaving enough space for them in the linker script; this pattern is common
731b93c5b9fSPetr Hosek // in embedded systems.
732b93c5b9fSPetr Hosek //
733b93c5b9fSPetr Hosek // If there isn't enough space for these sections, we'll remove them from the
734b93c5b9fSPetr Hosek // PT_LOAD segment, and we'll also remove the PT_PHDR segment.
735aa354187SGeorge Rimar void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) {
7365aedebffSPeter Smith   uint64_t Min = std::numeric_limits<uint64_t>::max();
7378c022ca7SRafael Espindola   for (OutputSection *Sec : OutputSections)
7385aedebffSPeter Smith     if (Sec->Flags & SHF_ALLOC)
7395aedebffSPeter Smith       Min = std::min<uint64_t>(Min, Sec->Addr);
7405aedebffSPeter Smith 
741aa354187SGeorge Rimar   auto It = llvm::find_if(
742aa354187SGeorge Rimar       Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; });
743aa354187SGeorge Rimar   if (It == Phdrs.end())
744d971e703SGeorge Rimar     return;
745aa354187SGeorge Rimar   PhdrEntry *FirstPTLoad = *It;
74602ed7575SRafael Espindola 
74702ed7575SRafael Espindola   uint64_t HeaderSize = getHeaderSize();
748b93c5b9fSPetr Hosek   // When linker script with SECTIONS is being used, don't output headers
749b93c5b9fSPetr Hosek   // unless there's a space for them.
750a323e2a7SRui Ueyama   uint64_t Base = HasSectionsCommand ? alignDown(Min, Config->MaxPageSize) : 0;
751b93c5b9fSPetr Hosek   if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) {
7521f0fe88aSRafael Espindola     Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
75302ed7575SRafael Espindola     Out::ElfHeader->Addr = Min;
75402ed7575SRafael Espindola     Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
755d971e703SGeorge Rimar     return;
75602ed7575SRafael Espindola   }
75702ed7575SRafael Espindola 
758582ede89SGeorge Rimar   Out::ElfHeader->PtLoad = nullptr;
759582ede89SGeorge Rimar   Out::ProgramHeaders->PtLoad = nullptr;
7606823c5f0SGeorge Rimar   FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad);
76102ed7575SRafael Espindola 
76260608a8aSGeorge Rimar   llvm::erase_if(Phdrs,
76360608a8aSGeorge Rimar                  [](const PhdrEntry *E) { return E->p_type == PT_PHDR; });
76402ed7575SRafael Espindola }
76502ed7575SRafael Espindola 
766ac27de9dSRui Ueyama LinkerScript::AddressState::AddressState() {
767ac27de9dSRui Ueyama   for (auto &MRI : Script->MemoryRegions) {
7685f37541cSGeorge Rimar     const MemoryRegion *MR = MRI.second;
769906e9a18SPeter Smith     MemRegionOffset[MR] = MR->Origin;
770906e9a18SPeter Smith   }
771906e9a18SPeter Smith }
772906e9a18SPeter Smith 
7737c18c28cSRui Ueyama // Assign addresses as instructed by linker script SECTIONS sub-commands.
774b5ca92efSJames Henderson void LinkerScript::assignAddresses() {
775b5ca92efSJames Henderson   // By default linker scripts use an initial value of 0 for '.', but prefer
776b5ca92efSJames Henderson   // -image-base if set.
777b5ca92efSJames Henderson   Dot = Config->ImageBase ? *Config->ImageBase : 0;
77818d19687SRui Ueyama 
77929b240c6SRui Ueyama   // Ctx captures the local AddressState and makes it accessible
780c1ace40bSPeter Smith   // deliberately. This is needed as there are some cases where we cannot just
781c1ace40bSPeter Smith   // thread the current state through to a lambda function created by the
782c1ace40bSPeter Smith   // script parser.
78329b240c6SRui Ueyama   Ctx = make_unique<AddressState>();
78472dc195dSRafael Espindola   ErrorOnMissingSection = true;
78506f4743aSRafael Espindola   switchTo(Aether);
78606f4743aSRafael Espindola 
7876b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
7888f99f73cSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
789d379f735SRui Ueyama       assignSymbol(Cmd, false);
79005ef4cffSRui Ueyama       continue;
791652852c5SGeorge Rimar     }
792652852c5SGeorge Rimar 
7938f99f73cSRui Ueyama     if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
7944595df94SRafael Espindola       Cmd->Expression();
795eefa758eSGeorge Rimar       continue;
796eefa758eSGeorge Rimar     }
797eefa758eSGeorge Rimar 
7988c022ca7SRafael Espindola     assignOffsets(cast<OutputSection>(Base));
799a14b13d8SGeorge Rimar   }
80029b240c6SRui Ueyama   Ctx = nullptr;
801fb8978fcSDima Stepanov }
802652852c5SGeorge Rimar 
803464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command.
804aa354187SGeorge Rimar std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
805aa354187SGeorge Rimar   std::vector<PhdrEntry *> Ret;
806bbe38602SEugene Leviant 
807464daadcSRui Ueyama   // Process PHDRS and FILEHDR keywords because they are not
808464daadcSRui Ueyama   // real output sections and cannot be added in the following loop.
809ac27de9dSRui Ueyama   for (const PhdrsCommand &Cmd : PhdrsCommands) {
8100ae2c24cSRui Ueyama     PhdrEntry *Phdr = make<PhdrEntry>(Cmd.Type, Cmd.Flags ? *Cmd.Flags : PF_R);
811bbe38602SEugene Leviant 
812bbe38602SEugene Leviant     if (Cmd.HasFilehdr)
813aa354187SGeorge Rimar       Phdr->add(Out::ElfHeader);
814bbe38602SEugene Leviant     if (Cmd.HasPhdrs)
815aa354187SGeorge Rimar       Phdr->add(Out::ProgramHeaders);
81656b21c86SEugene Leviant 
81756b21c86SEugene Leviant     if (Cmd.LMAExpr) {
818aa354187SGeorge Rimar       Phdr->p_paddr = Cmd.LMAExpr().getValue();
819aa354187SGeorge Rimar       Phdr->HasLMA = true;
82056b21c86SEugene Leviant     }
821aa354187SGeorge Rimar     Ret.push_back(Phdr);
822bbe38602SEugene Leviant   }
823bbe38602SEugene Leviant 
824464daadcSRui Ueyama   // Add output sections to program headers.
8258c022ca7SRafael Espindola   for (OutputSection *Sec : OutputSections) {
826bbe38602SEugene Leviant     // Assign headers specified by linker script
8278c022ca7SRafael Espindola     for (size_t Id : getPhdrIndices(Sec)) {
828aa354187SGeorge Rimar       Ret[Id]->add(Sec);
829ac27de9dSRui Ueyama       if (!PhdrsCommands[Id].Flags.hasValue())
830aa354187SGeorge Rimar         Ret[Id]->p_flags |= Sec->getPhdrFlags();
831bbe38602SEugene Leviant     }
832bbe38602SEugene Leviant   }
833edebbdf1SRui Ueyama   return Ret;
834bbe38602SEugene Leviant }
835bbe38602SEugene Leviant 
836e03ba023SRui Ueyama // Returns true if we should emit an .interp section.
837e03ba023SRui Ueyama //
838e03ba023SRui Ueyama // We usually do. But if PHDRS commands are given, and
839e03ba023SRui Ueyama // no PT_INTERP is there, there's no place to emit an
840e03ba023SRui Ueyama // .interp, so we don't do that in that case.
841e03ba023SRui Ueyama bool LinkerScript::needsInterpSection() {
842ac27de9dSRui Ueyama   if (PhdrsCommands.empty())
843e03ba023SRui Ueyama     return true;
844ac27de9dSRui Ueyama   for (PhdrsCommand &Cmd : PhdrsCommands)
845e31d9886SRui Ueyama     if (Cmd.Type == PT_INTERP)
846e31d9886SRui Ueyama       return true;
847e03ba023SRui Ueyama   return false;
848f9bc3bd2SEugene Leviant }
849f9bc3bd2SEugene Leviant 
850b8dd23f5SRui Ueyama ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
8517e5b0a59SGeorge Rimar   if (S == ".") {
85229b240c6SRui Ueyama     if (Ctx)
85329b240c6SRui Ueyama       return {Ctx->OutSec, false, Dot - Ctx->OutSec->Addr, Loc};
8547e5b0a59SGeorge Rimar     error(Loc + ": unable to get location counter value");
8557e5b0a59SGeorge Rimar     return 0;
8567e5b0a59SGeorge Rimar   }
857244ef981SRafael Espindola   if (SymbolBody *B = Symtab->find(S)) {
85872dc195dSRafael Espindola     if (auto *D = dyn_cast<DefinedRegular>(B))
8594fbe3518SRui Ueyama       return {D->Section, false, D->Value, Loc};
86030f16b23SPetr Hosek     if (auto *C = dyn_cast<DefinedCommon>(B))
8614fbe3518SRui Ueyama       return {C->Section, false, 0, Loc};
86272dc195dSRafael Espindola   }
863f6aeed36SEugene Leviant   error(Loc + ": symbol not found: " + S);
864884e786dSGeorge Rimar   return 0;
865884e786dSGeorge Rimar }
866884e786dSGeorge Rimar 
867656be311SRui Ueyama // Returns the index of the segment named Name.
868656be311SRui Ueyama static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> Vec,
869656be311SRui Ueyama                                      StringRef Name) {
870656be311SRui Ueyama   for (size_t I = 0; I < Vec.size(); ++I)
871656be311SRui Ueyama     if (Vec[I].Name == Name)
872656be311SRui Ueyama       return I;
873656be311SRui Ueyama   return None;
874656be311SRui Ueyama }
875656be311SRui Ueyama 
8762c923c2cSRafael Espindola // Returns indices of ELF headers containing specific section. Each index is a
8772c923c2cSRafael Espindola // zero based number of ELF header listed within PHDRS {} script block.
8788c022ca7SRafael Espindola std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) {
87929c5a2a9SRui Ueyama   std::vector<size_t> Ret;
880656be311SRui Ueyama 
881656be311SRui Ueyama   for (StringRef S : Cmd->Phdrs) {
882ac27de9dSRui Ueyama     if (Optional<size_t> Idx = getPhdrIndex(PhdrsCommands, S))
88387223574SRui Ueyama       Ret.push_back(*Idx);
884656be311SRui Ueyama     else if (S != "NONE")
885656be311SRui Ueyama       error(Cmd->Location + ": section header '" + S +
886656be311SRui Ueyama             "' is not listed in PHDRS");
887bbe38602SEugene Leviant   }
888656be311SRui Ueyama   return Ret;
88929c5a2a9SRui Ueyama }
890