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"
17652852c5SGeorge Rimar #include "OutputSections.h"
1893c9af42SRui Ueyama #include "Strings.h"
19f7c5fbb1SRui Ueyama #include "SymbolTable.h"
2055518e7dSRui Ueyama #include "Symbols.h"
213fb5a6dcSGeorge Rimar #include "SyntheticSections.h"
2255b169bfSRafael Espindola #include "Target.h"
23bbe38602SEugene Leviant #include "Writer.h"
24*2017d52bSRui Ueyama #include "lld/Common/Memory.h"
254f5c8c29SBob Haarman #include "lld/Common/Threads.h"
2622886a28SEugene Zelenko #include "llvm/ADT/STLExtras.h"
2722886a28SEugene Zelenko #include "llvm/ADT/StringRef.h"
28264b5d9eSZachary Turner #include "llvm/BinaryFormat/ELF.h"
2922886a28SEugene Zelenko #include "llvm/Support/Casting.h"
3022886a28SEugene Zelenko #include "llvm/Support/Endian.h"
3122886a28SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
32f7c5fbb1SRui Ueyama #include "llvm/Support/FileSystem.h"
33f03f3cc1SRui Ueyama #include "llvm/Support/Path.h"
3422886a28SEugene Zelenko #include <algorithm>
3522886a28SEugene Zelenko #include <cassert>
3622886a28SEugene Zelenko #include <cstddef>
3722886a28SEugene Zelenko #include <cstdint>
3822886a28SEugene Zelenko #include <iterator>
3922886a28SEugene Zelenko #include <limits>
4022886a28SEugene Zelenko #include <string>
4122886a28SEugene Zelenko #include <vector>
42f7c5fbb1SRui Ueyama 
43f7c5fbb1SRui Ueyama using namespace llvm;
44652852c5SGeorge Rimar using namespace llvm::ELF;
451ebc8ed7SRui Ueyama using namespace llvm::object;
46e38cbab5SGeorge Rimar using namespace llvm::support::endian;
47f7c5fbb1SRui Ueyama using namespace lld;
48e0df00b9SRafael Espindola using namespace lld::elf;
49f7c5fbb1SRui Ueyama 
50a34da938SRui Ueyama LinkerScript *elf::Script;
51a34da938SRui Ueyama 
525d0ea70aSGeorge Rimar static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) {
535d0ea70aSGeorge Rimar   if (OutputSection *OS = InputSec->getOutputSection())
545d0ea70aSGeorge Rimar     return OS->Addr;
555d0ea70aSGeorge Rimar   error(Loc + ": unable to evaluate expression: input section " +
565d0ea70aSGeorge Rimar         InputSec->Name + " has no output section assigned");
575d0ea70aSGeorge Rimar   return 0;
58608cf670SGeorge Rimar }
595d0ea70aSGeorge Rimar 
605d0ea70aSGeorge Rimar uint64_t ExprValue::getValue() const {
615d0ea70aSGeorge Rimar   if (Sec)
625d0ea70aSGeorge Rimar     return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc),
635d0ea70aSGeorge Rimar                    Alignment);
643c6de1a6SPetr Hosek   return alignTo(Val, Alignment);
6572dc195dSRafael Espindola }
6672dc195dSRafael Espindola 
677ba5f47eSRafael Espindola uint64_t ExprValue::getSecAddr() const {
687ba5f47eSRafael Espindola   if (Sec)
695d0ea70aSGeorge Rimar     return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc);
707ba5f47eSRafael Espindola   return 0;
717ba5f47eSRafael Espindola }
727ba5f47eSRafael Espindola 
73a6acd23cSRafael Espindola uint64_t ExprValue::getSectionOffset() const {
748b250344SRafael Espindola   // If the alignment is trivial, we don't have to compute the full
758b250344SRafael Espindola   // value to know the offset. This allows this function to succeed in
768b250344SRafael Espindola   // cases where the output section is not yet known.
778b250344SRafael Espindola   if (Alignment == 1)
788b250344SRafael Espindola     return Val;
79a6acd23cSRafael Espindola   return getValue() - getSecAddr();
80a6acd23cSRafael Espindola }
81a6acd23cSRafael Espindola 
828c022ca7SRafael Espindola OutputSection *LinkerScript::createOutputSection(StringRef Name,
838c022ca7SRafael Espindola                                                  StringRef Location) {
848c022ca7SRafael Espindola   OutputSection *&SecRef = NameToOutputSection[Name];
858c022ca7SRafael Espindola   OutputSection *Sec;
868c022ca7SRafael Espindola   if (SecRef && SecRef->Location.empty()) {
8705c4f67cSRafael Espindola     // There was a forward reference.
888c022ca7SRafael Espindola     Sec = SecRef;
8905c4f67cSRafael Espindola   } else {
908c022ca7SRafael Espindola     Sec = make<OutputSection>(Name, SHT_PROGBITS, 0);
918c022ca7SRafael Espindola     if (!SecRef)
928c022ca7SRafael Espindola       SecRef = Sec;
9305c4f67cSRafael Espindola   }
948c022ca7SRafael Espindola   Sec->Location = Location;
958c022ca7SRafael Espindola   return Sec;
96851dc1e8SGeorge Rimar }
97851dc1e8SGeorge Rimar 
988c022ca7SRafael Espindola OutputSection *LinkerScript::getOrCreateOutputSection(StringRef Name) {
998c022ca7SRafael Espindola   OutputSection *&CmdRef = NameToOutputSection[Name];
10005c4f67cSRafael Espindola   if (!CmdRef)
1018c022ca7SRafael Espindola     CmdRef = make<OutputSection>(Name, SHT_PROGBITS, 0);
10205c4f67cSRafael Espindola   return CmdRef;
103d83ce1b4SGeorge Rimar }
104d83ce1b4SGeorge Rimar 
105b8dd23f5SRui Ueyama void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
10672dc195dSRafael Espindola   uint64_t Val = E().getValue();
1078c804d97SGeorge Rimar   if (Val < Dot && InSec)
1082ee2d2dcSGeorge Rimar     error(Loc + ": unable to move location counter backward for: " +
10929b240c6SRui Ueyama           Ctx->OutSec->Name);
1104cd7352cSRafael Espindola   Dot = Val;
11118d19687SRui Ueyama 
1124cd7352cSRafael Espindola   // Update to location counter means update to section size.
1134cd7352cSRafael Espindola   if (InSec)
11429b240c6SRui Ueyama     Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr;
115679828ffSRafael Espindola }
116679828ffSRafael Espindola 
1175908c2f8SRui Ueyama // This function is called from processSectionCommands,
1185908c2f8SRui Ueyama // while we are fixing the output section layout.
119b8dd23f5SRui Ueyama void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
1201602421cSRui Ueyama   if (Cmd->Name == ".")
1218f1f3c40SMeador Inge     return;
1228f1f3c40SMeador Inge 
1238f1f3c40SMeador Inge   // If a symbol was in PROVIDE(), we need to define it only when
1248f1f3c40SMeador Inge   // it is a referenced undefined symbol.
125f52496e1SRui Ueyama   Symbol *B = Symtab->find(Cmd->Name);
1268f1f3c40SMeador Inge   if (Cmd->Provide && (!B || B->isDefined()))
1278f1f3c40SMeador Inge     return;
1288f1f3c40SMeador Inge 
12918d19687SRui Ueyama   // Define a symbol.
130f52496e1SRui 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   ExprValue Value = Cmd->Expression();
13618d19687SRui Ueyama   SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
13718d19687SRui Ueyama 
13818d19687SRui Ueyama   // When this function is called, section addresses have not been
13918d19687SRui Ueyama   // fixed yet. So, we may or may not know the value of the RHS
14018d19687SRui Ueyama   // expression.
14118d19687SRui Ueyama   //
14218d19687SRui Ueyama   // For example, if an expression is `x = 42`, we know x is always 42.
14318d19687SRui Ueyama   // However, if an expression is `x = .`, there's no way to know its
14418d19687SRui Ueyama   // value at the moment.
14518d19687SRui Ueyama   //
14618d19687SRui Ueyama   // We want to set symbol values early if we can. This allows us to
14718d19687SRui Ueyama   // use symbols as variables in linker scripts. Doing so allows us to
14818d19687SRui Ueyama   // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
14918d19687SRui Ueyama   uint64_t SymValue = Value.Sec ? 0 : Value.getValue();
15018d19687SRui Ueyama 
151bec3765bSRafael Espindola   replaceSymbol<Defined>(Sym, nullptr, Cmd->Name, STB_GLOBAL, Visibility,
152e9a9e0a1SPeter Collingbourne                          STT_NOTYPE, SymValue, 0, Sec);
153e9a9e0a1SPeter Collingbourne   Cmd->Sym = cast<Defined>(Sym);
15418d19687SRui Ueyama }
15518d19687SRui Ueyama 
15618d19687SRui Ueyama // This function is called from assignAddresses, while we are
15718d19687SRui Ueyama // fixing the output section addresses. This function is supposed
15818d19687SRui Ueyama // to set the final value for a given symbol assignment.
15918d19687SRui Ueyama void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
16018d19687SRui Ueyama   if (Cmd->Name == ".") {
16118d19687SRui Ueyama     setDot(Cmd->Expression, Cmd->Location, InSec);
16218d19687SRui Ueyama     return;
16318d19687SRui Ueyama   }
16418d19687SRui Ueyama 
16518d19687SRui Ueyama   if (!Cmd->Sym)
16618d19687SRui Ueyama     return;
16718d19687SRui Ueyama 
16818d19687SRui Ueyama   ExprValue V = Cmd->Expression();
16918d19687SRui Ueyama   if (V.isAbsolute()) {
17018d19687SRui Ueyama     Cmd->Sym->Section = nullptr;
17118d19687SRui Ueyama     Cmd->Sym->Value = V.getValue();
17218d19687SRui Ueyama   } else {
17318d19687SRui Ueyama     Cmd->Sym->Section = V.Sec;
17418d19687SRui Ueyama     Cmd->Sym->Value = V.getSectionOffset();
17518d19687SRui Ueyama   }
176ceabe80eSEugene Leviant }
177ceabe80eSEugene Leviant 
17804c9ca74SRui Ueyama static std::string getFilename(InputFile *File) {
1791e30f07cSDmitry Mikulin   if (!File)
180e0be2901SRui Ueyama     return "";
1811e30f07cSDmitry Mikulin   if (File->ArchiveName.empty())
1821e30f07cSDmitry Mikulin     return File->getName();
1831e30f07cSDmitry Mikulin   return (File->ArchiveName + "(" + File->getName() + ")").str();
184e0be2901SRui Ueyama }
185e0be2901SRui Ueyama 
186b8dd23f5SRui Ueyama bool LinkerScript::shouldKeep(InputSectionBase *S) {
18704c9ca74SRui Ueyama   std::string Filename = getFilename(S->File);
1887f1c266eSRui Ueyama   for (InputSectionDescription *ID : KeptSections)
189f300ca21SDmitry Mikulin     if (ID->FilePat.match(Filename))
190cf43f179SEugene Leviant       for (SectionPattern &P : ID->SectionPatterns)
191f91282e1SRui Ueyama         if (P.SectionPat.match(S->Name))
192eea3114fSGeorge Rimar           return true;
193eea3114fSGeorge Rimar   return false;
194eea3114fSGeorge Rimar }
195eea3114fSGeorge Rimar 
196ea93fe00SRui Ueyama // A helper function for the SORT() command.
197c404d50dSRafael Espindola static std::function<bool(InputSectionBase *, InputSectionBase *)>
198be394db3SGeorge Rimar getComparator(SortSectionPolicy K) {
199be394db3SGeorge Rimar   switch (K) {
200be394db3SGeorge Rimar   case SortSectionPolicy::Alignment:
201ea93fe00SRui Ueyama     return [](InputSectionBase *A, InputSectionBase *B) {
202ea93fe00SRui Ueyama       // ">" is not a mistake. Sections with larger alignments are placed
203ea93fe00SRui Ueyama       // before sections with smaller alignments in order to reduce the
204ea93fe00SRui Ueyama       // amount of padding necessary. This is compatible with GNU.
205ea93fe00SRui Ueyama       return A->Alignment > B->Alignment;
206ea93fe00SRui Ueyama     };
207be394db3SGeorge Rimar   case SortSectionPolicy::Name:
208ea93fe00SRui Ueyama     return [](InputSectionBase *A, InputSectionBase *B) {
209ea93fe00SRui Ueyama       return A->Name < B->Name;
210ea93fe00SRui Ueyama     };
211be394db3SGeorge Rimar   case SortSectionPolicy::Priority:
212ea93fe00SRui Ueyama     return [](InputSectionBase *A, InputSectionBase *B) {
213ea93fe00SRui Ueyama       return getPriority(A->Name) < getPriority(B->Name);
214ea93fe00SRui Ueyama     };
215be394db3SGeorge Rimar   default:
216be394db3SGeorge Rimar     llvm_unreachable("unknown sort policy");
217be394db3SGeorge Rimar   }
218742c3836SRui Ueyama }
2190702c4e8SGeorge Rimar 
220ea93fe00SRui Ueyama // A helper function for the SORT() command.
22105433431SRui Ueyama static bool matchConstraints(ArrayRef<InputSection *> Sections,
22206ae6836SGeorge Rimar                              ConstraintKind Kind) {
2238f66df92SGeorge Rimar   if (Kind == ConstraintKind::NoConstraint)
2248f66df92SGeorge Rimar     return true;
2252c7171bfSRui Ueyama 
226b801441eSRui Ueyama   bool IsRW = llvm::any_of(
22705433431SRui Ueyama       Sections, [](InputSection *Sec) { return Sec->Flags & SHF_WRITE; });
2282c7171bfSRui Ueyama 
229e746e52cSRafael Espindola   return (IsRW && Kind == ConstraintKind::ReadWrite) ||
230e746e52cSRafael Espindola          (!IsRW && Kind == ConstraintKind::ReadOnly);
23106ae6836SGeorge Rimar }
23206ae6836SGeorge Rimar 
2331f4d7b56SRui Ueyama static void sortSections(MutableArrayRef<InputSection *> Vec,
234ee924709SRui Ueyama                          SortSectionPolicy K) {
235ee924709SRui Ueyama   if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
2361f4d7b56SRui Ueyama     std::stable_sort(Vec.begin(), Vec.end(), getComparator(K));
237ee924709SRui Ueyama }
238ee924709SRui Ueyama 
2397ad1e310SRui Ueyama // Sort sections as instructed by SORT-family commands and --sort-section
2407ad1e310SRui Ueyama // option. Because SORT-family commands can be nested at most two depth
2417ad1e310SRui Ueyama // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
2427ad1e310SRui Ueyama // line option is respected even if a SORT command is given, the exact
2437ad1e310SRui Ueyama // behavior we have here is a bit complicated. Here are the rules.
2447ad1e310SRui Ueyama //
2457ad1e310SRui Ueyama // 1. If two SORT commands are given, --sort-section is ignored.
2467ad1e310SRui Ueyama // 2. If one SORT command is given, and if it is not SORT_NONE,
2477ad1e310SRui Ueyama //    --sort-section is handled as an inner SORT command.
2487ad1e310SRui Ueyama // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
2497ad1e310SRui Ueyama // 4. If no SORT command is given, sort according to --sort-section.
2507ad1e310SRui Ueyama // 5. If no SORT commands are given and --sort-section is not specified,
2517ad1e310SRui Ueyama //    apply sorting provided by --symbol-ordering-file if any exist.
2527ad1e310SRui Ueyama static void sortInputSections(
2537ad1e310SRui Ueyama     MutableArrayRef<InputSection *> Vec, const SectionPattern &Pat,
2547ad1e310SRui Ueyama     const DenseMap<SectionBase *, int> &Order) {
2557ad1e310SRui Ueyama   if (Pat.SortOuter == SortSectionPolicy::None)
2567ad1e310SRui Ueyama     return;
2577ad1e310SRui Ueyama 
2587ad1e310SRui Ueyama   if (Pat.SortOuter == SortSectionPolicy::Default &&
2597ad1e310SRui Ueyama       Config->SortSection == SortSectionPolicy::Default) {
2607ad1e310SRui Ueyama     // If -symbol-ordering-file was given, sort accordingly.
2617ad1e310SRui Ueyama     // Usually, Order is empty.
2627ad1e310SRui Ueyama     if (!Order.empty())
2637ad1e310SRui Ueyama       sortByOrder(Vec, [&](InputSectionBase *S) { return Order.lookup(S); });
2647ad1e310SRui Ueyama     return;
2657ad1e310SRui Ueyama   }
2667ad1e310SRui Ueyama 
2677ad1e310SRui Ueyama   if (Pat.SortInner == SortSectionPolicy::Default)
2687ad1e310SRui Ueyama     sortSections(Vec, Config->SortSection);
2697ad1e310SRui Ueyama   else
2707ad1e310SRui Ueyama     sortSections(Vec, Pat.SortInner);
2717ad1e310SRui Ueyama   sortSections(Vec, Pat.SortOuter);
2727ad1e310SRui Ueyama }
2737ad1e310SRui Ueyama 
274d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches.
2756a1aa8d9SRafael Espindola std::vector<InputSection *>
276849d499eSRafael Espindola LinkerScript::computeInputSections(const InputSectionDescription *Cmd,
277849d499eSRafael Espindola                                    const DenseMap<SectionBase *, int> &Order) {
2786a1aa8d9SRafael Espindola   std::vector<InputSection *> Ret;
2798c6a5aafSRui Ueyama 
28072e107f3SRui Ueyama   // Collects all sections that satisfy constraints of Cmd.
28172e107f3SRui Ueyama   for (const SectionPattern &Pat : Cmd->SectionPatterns) {
28272e107f3SRui Ueyama     size_t SizeBefore = Ret.size();
28372e107f3SRui Ueyama 
28472e107f3SRui Ueyama     for (InputSectionBase *Sec : InputSections) {
28596b11578SGeorge Rimar       if (!Sec->Live || Sec->Assigned)
2868c6a5aafSRui Ueyama         continue;
28772e107f3SRui Ueyama 
288908a3d34SRafael Espindola       // For -emit-relocs we have to ignore entries like
289908a3d34SRafael Espindola       //   .rela.dyn : { *(.rela.data) }
290908a3d34SRafael Espindola       // which are common because they are in the default bfd script.
29172e107f3SRui Ueyama       if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
292908a3d34SRafael Espindola         continue;
2938c6a5aafSRui Ueyama 
29404c9ca74SRui Ueyama       std::string Filename = getFilename(Sec->File);
29572e107f3SRui Ueyama       if (!Cmd->FilePat.match(Filename) ||
29672e107f3SRui Ueyama           Pat.ExcludedFilePat.match(Filename) ||
29772e107f3SRui Ueyama           !Pat.SectionPat.match(Sec->Name))
298e0be2901SRui Ueyama         continue;
29972e107f3SRui Ueyama 
300ec5c4adbSRui Ueyama       // It is safe to assume that Sec is an InputSection
301ec5c4adbSRui Ueyama       // because mergeable or EH input sections have already been
302ec5c4adbSRui Ueyama       // handled and eliminated.
3036a1aa8d9SRafael Espindola       Ret.push_back(cast<InputSection>(Sec));
30472e107f3SRui Ueyama       Sec->Assigned = true;
305f94efdddSRui Ueyama     }
306d3190795SRafael Espindola 
3077ad1e310SRui Ueyama     sortInputSections(MutableArrayRef<InputSection *>(Ret).slice(SizeBefore),
3087ad1e310SRui Ueyama                       Pat, Order);
309ee924709SRui Ueyama   }
31072e107f3SRui Ueyama   return Ret;
311be94e1b6SRafael Espindola }
312be94e1b6SRafael Espindola 
31305433431SRui Ueyama void LinkerScript::discard(ArrayRef<InputSection *> V) {
31405433431SRui Ueyama   for (InputSection *S : V) {
3151e30f07cSDmitry Mikulin     if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab ||
3161e30f07cSDmitry Mikulin         S == InX::DynStrTab)
3172af64b0bSRafael Espindola       error("discarding " + S->Name + " section is not allowed");
3188e38ea8bSRui Ueyama 
3198e38ea8bSRui Ueyama     S->Assigned = false;
3208e38ea8bSRui Ueyama     S->Live = false;
321647c1685SGeorge Rimar     discard(S->DependentSections);
322be94e1b6SRafael Espindola   }
323be94e1b6SRafael Espindola }
324be94e1b6SRafael Espindola 
325849d499eSRafael Espindola std::vector<InputSection *> LinkerScript::createInputSectionList(
326849d499eSRafael Espindola     OutputSection &OutCmd, const DenseMap<SectionBase *, int> &Order) {
32705433431SRui Ueyama   std::vector<InputSection *> Ret;
328e7f912cdSRui Ueyama 
3296b394caaSRui Ueyama   for (BaseCommand *Base : OutCmd.SectionCommands) {
33005433431SRui Ueyama     if (auto *Cmd = dyn_cast<InputSectionDescription>(Base)) {
331849d499eSRafael Espindola       Cmd->Sections = computeInputSections(Cmd, Order);
332e4c8b9b7SRafael Espindola       Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
3330b9ce6a4SRui Ueyama     }
33405433431SRui Ueyama   }
3350b9ce6a4SRui Ueyama   return Ret;
3360b9ce6a4SRui Ueyama }
3370b9ce6a4SRui Ueyama 
3383558e24aSRafael Espindola void LinkerScript::processSectionCommands() {
3395616adf6SRafael Espindola   // A symbol can be assigned before any section is mentioned in the linker
3405616adf6SRafael Espindola   // script. In an DSO, the symbol values are addresses, so the only important
3415616adf6SRafael Espindola   // section values are:
3425616adf6SRafael Espindola   // * SHN_UNDEF
3435616adf6SRafael Espindola   // * SHN_ABS
3445616adf6SRafael Espindola   // * Any value meaning a regular section.
3455616adf6SRafael Espindola   // To handle that, create a dummy aether section that fills the void before
3465616adf6SRafael Espindola   // the linker scripts switches to another section. It has an index of one
3475616adf6SRafael Espindola   // which will map to whatever the first actual section is.
3485616adf6SRafael Espindola   Aether = make<OutputSection>("", 0, SHF_ALLOC);
3495616adf6SRafael Espindola   Aether->SectionIndex = 1;
35018d19687SRui Ueyama 
35129b240c6SRui Ueyama   // Ctx captures the local AddressState and makes it accessible deliberately.
35229b240c6SRui Ueyama   // This is needed as there are some cases where we cannot just
353c1ace40bSPeter Smith   // thread the current state through to a lambda function created by the
354c1ace40bSPeter Smith   // script parser.
355089dac7bSRafael Espindola   auto Deleter = make_unique<AddressState>();
356089dac7bSRafael Espindola   Ctx = Deleter.get();
35729b240c6SRui Ueyama   Ctx->OutSec = Aether;
3585616adf6SRafael Espindola 
3591b45d377SGeorge Rimar   size_t I = 0;
360849d499eSRafael Espindola   DenseMap<SectionBase *, int> Order = buildSectionOrder();
361355a8dd6SRui Ueyama   // Add input sections to output sections.
3621b45d377SGeorge Rimar   for (BaseCommand *Base : SectionCommands) {
3630b1b695aSRui Ueyama     // Handle symbol assignments outside of any output section.
3641b45d377SGeorge Rimar     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
3654cd7352cSRafael Espindola       addSymbol(Cmd);
3662ab5f73dSRui Ueyama       continue;
3672ab5f73dSRui Ueyama     }
3680b1b695aSRui Ueyama 
3691b45d377SGeorge Rimar     if (auto *Sec = dyn_cast<OutputSection>(Base)) {
370849d499eSRafael Espindola       std::vector<InputSection *> V = createInputSectionList(*Sec, Order);
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       //
3841b45d377SGeorge Rimar       // Because we'll iterate over SectionCommands many more times, the easy
3851b45d377SGeorge Rimar       // way to "make it as if it wasn't present" is to make it empty.
3868c022ca7SRafael Espindola       if (!matchConstraints(V, Sec->Constraint)) {
387b4c9b81aSRafael Espindola         for (InputSectionBase *S : V)
388f94efdddSRui Ueyama           S->Assigned = false;
3891b45d377SGeorge Rimar         Sec->SectionCommands.clear();
3907c3ff2ebSRafael Espindola         continue;
3917c3ff2ebSRafael Espindola       }
3927c3ff2ebSRafael Espindola 
3930b1b695aSRui Ueyama       // A directive may contain symbol definitions like this:
3940b1b695aSRui Ueyama       // ".foo : { ...; bar = .; }". Handle them.
3956b394caaSRui Ueyama       for (BaseCommand *Base : Sec->SectionCommands)
3968f99f73cSRui Ueyama         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
3974cd7352cSRafael Espindola           addSymbol(OutCmd);
3987c3ff2ebSRafael Espindola 
3990b1b695aSRui Ueyama       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
4000b1b695aSRui Ueyama       // is given, input sections are aligned to that value, whether the
4010b1b695aSRui Ueyama       // given value is larger or smaller than the original section alignment.
4028c022ca7SRafael Espindola       if (Sec->SubalignExpr) {
4038c022ca7SRafael Espindola         uint32_t Subalign = Sec->SubalignExpr().getValue();
404b4c9b81aSRafael Espindola         for (InputSectionBase *S : V)
4050b1b695aSRui Ueyama           S->Alignment = Subalign;
40620d03194SEugene Leviant       }
4070b1b695aSRui Ueyama 
4080b1b695aSRui Ueyama       // Add input sections to an output section.
40905433431SRui Ueyama       for (InputSection *S : V)
41005433431SRui Ueyama         Sec->addSection(S);
411c54d5b16SRui Ueyama 
4121b45d377SGeorge Rimar       Sec->SectionIndex = I++;
4138c022ca7SRafael Espindola       if (Sec->Noload)
414fbb0463fSGeorge Rimar         Sec->Type = SHT_NOBITS;
41548c3f1ceSRui Ueyama     }
4164aa2ef5bSRafael Espindola   }
4171b45d377SGeorge Rimar   Ctx = nullptr;
4181b45d377SGeorge Rimar }
419e63d81bdSEugene Leviant 
420d2f225fdSRui Ueyama static OutputSection *findByName(ArrayRef<BaseCommand *> Vec,
421d2f225fdSRui Ueyama                                  StringRef Name) {
422d2f225fdSRui Ueyama   for (BaseCommand *Base : Vec)
423d2f225fdSRui Ueyama     if (auto *Sec = dyn_cast<OutputSection>(Base))
424d2f225fdSRui Ueyama       if (Sec->Name == Name)
425d2f225fdSRui Ueyama         return Sec;
426d2f225fdSRui Ueyama   return nullptr;
427d2f225fdSRui Ueyama }
428d2f225fdSRui Ueyama 
429aa8523e4SRui Ueyama static OutputSection *createSection(InputSectionBase *IS,
430aa8523e4SRui Ueyama                                     StringRef OutsecName) {
431aa8523e4SRui Ueyama   OutputSection *Sec = Script->createOutputSection(OutsecName, "<internal>");
432aa8523e4SRui Ueyama   Sec->addSection(cast<InputSection>(IS));
433aa8523e4SRui Ueyama   return Sec;
434aa8523e4SRui Ueyama }
435aa8523e4SRui Ueyama 
436aa8523e4SRui Ueyama static OutputSection *addInputSec(StringMap<OutputSection *> &Map,
437aa8523e4SRui Ueyama                                   InputSectionBase *IS, StringRef OutsecName) {
438aa8523e4SRui Ueyama   // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
439aa8523e4SRui Ueyama   // option is given. A section with SHT_GROUP defines a "section group", and
440aa8523e4SRui Ueyama   // its members have SHF_GROUP attribute. Usually these flags have already been
441aa8523e4SRui Ueyama   // stripped by InputFiles.cpp as section groups are processed and uniquified.
442aa8523e4SRui Ueyama   // However, for the -r option, we want to pass through all section groups
443aa8523e4SRui Ueyama   // as-is because adding/removing members or merging them with other groups
444aa8523e4SRui Ueyama   // change their semantics.
445aa8523e4SRui Ueyama   if (IS->Type == SHT_GROUP || (IS->Flags & SHF_GROUP))
446aa8523e4SRui Ueyama     return createSection(IS, OutsecName);
447aa8523e4SRui Ueyama 
448aa8523e4SRui Ueyama   // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
449aa8523e4SRui Ueyama   // relocation sections .rela.foo and .rela.bar for example. Most tools do
450aa8523e4SRui Ueyama   // not allow multiple REL[A] sections for output section. Hence we
451aa8523e4SRui Ueyama   // should combine these relocation sections into single output.
452aa8523e4SRui Ueyama   // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
453aa8523e4SRui Ueyama   // other REL[A] sections created by linker itself.
454aa8523e4SRui Ueyama   if (!isa<SyntheticSection>(IS) &&
455aa8523e4SRui Ueyama       (IS->Type == SHT_REL || IS->Type == SHT_RELA)) {
456aa8523e4SRui Ueyama     auto *Sec = cast<InputSection>(IS);
457aa8523e4SRui Ueyama     OutputSection *Out = Sec->getRelocatedSection()->getOutputSection();
458aa8523e4SRui Ueyama 
459aa8523e4SRui Ueyama     if (Out->RelocationSection) {
460aa8523e4SRui Ueyama       Out->RelocationSection->addSection(Sec);
461aa8523e4SRui Ueyama       return nullptr;
462aa8523e4SRui Ueyama     }
463aa8523e4SRui Ueyama 
464aa8523e4SRui Ueyama     Out->RelocationSection = createSection(IS, OutsecName);
465aa8523e4SRui Ueyama     return Out->RelocationSection;
466aa8523e4SRui Ueyama   }
467aa8523e4SRui Ueyama 
46880355234SGeorge Rimar   // When control reaches here, mergeable sections have already been merged into
46980355234SGeorge Rimar   // synthetic sections. For relocatable case we want to create one output
47080355234SGeorge Rimar   // section per syntetic section so that they have a valid sh_entsize.
471aa8523e4SRui Ueyama   if (Config->Relocatable && (IS->Flags & SHF_MERGE))
472aa8523e4SRui Ueyama     return createSection(IS, OutsecName);
473aa8523e4SRui Ueyama 
474aa8523e4SRui Ueyama   //  The ELF spec just says
475aa8523e4SRui Ueyama   // ----------------------------------------------------------------
476aa8523e4SRui Ueyama   // In the first phase, input sections that match in name, type and
477aa8523e4SRui Ueyama   // attribute flags should be concatenated into single sections.
478aa8523e4SRui Ueyama   // ----------------------------------------------------------------
479aa8523e4SRui Ueyama   //
480aa8523e4SRui Ueyama   // However, it is clear that at least some flags have to be ignored for
481aa8523e4SRui Ueyama   // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
482aa8523e4SRui Ueyama   // ignored. We should not have two output .text sections just because one was
483aa8523e4SRui Ueyama   // in a group and another was not for example.
484aa8523e4SRui Ueyama   //
485aa8523e4SRui Ueyama   // It also seems that that wording was a late addition and didn't get the
486aa8523e4SRui Ueyama   // necessary scrutiny.
487aa8523e4SRui Ueyama   //
488aa8523e4SRui Ueyama   // Merging sections with different flags is expected by some users. One
489aa8523e4SRui Ueyama   // reason is that if one file has
490aa8523e4SRui Ueyama   //
491aa8523e4SRui Ueyama   // int *const bar __attribute__((section(".foo"))) = (int *)0;
492aa8523e4SRui Ueyama   //
493aa8523e4SRui Ueyama   // gcc with -fPIC will produce a read only .foo section. But if another
494aa8523e4SRui Ueyama   // file has
495aa8523e4SRui Ueyama   //
496aa8523e4SRui Ueyama   // int zed;
497aa8523e4SRui Ueyama   // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
498aa8523e4SRui Ueyama   //
499aa8523e4SRui Ueyama   // gcc with -fPIC will produce a read write section.
500aa8523e4SRui Ueyama   //
501aa8523e4SRui Ueyama   // Last but not least, when using linker script the merge rules are forced by
502aa8523e4SRui Ueyama   // the script. Unfortunately, linker scripts are name based. This means that
503aa8523e4SRui Ueyama   // expressions like *(.foo*) can refer to multiple input sections with
504aa8523e4SRui Ueyama   // different flags. We cannot put them in different output sections or we
505aa8523e4SRui Ueyama   // would produce wrong results for
506aa8523e4SRui Ueyama   //
507aa8523e4SRui Ueyama   // start = .; *(.foo.*) end = .; *(.bar)
508aa8523e4SRui Ueyama   //
509aa8523e4SRui Ueyama   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
510aa8523e4SRui Ueyama   // another. The problem is that there is no way to layout those output
511aa8523e4SRui Ueyama   // sections such that the .foo sections are the only thing between the start
512aa8523e4SRui Ueyama   // and end symbols.
513aa8523e4SRui Ueyama   //
514aa8523e4SRui Ueyama   // Given the above issues, we instead merge sections by name and error on
515aa8523e4SRui Ueyama   // incompatible types and flags.
516aa8523e4SRui Ueyama   OutputSection *&Sec = Map[OutsecName];
517aa8523e4SRui Ueyama   if (Sec) {
518aa8523e4SRui Ueyama     Sec->addSection(cast<InputSection>(IS));
519aa8523e4SRui Ueyama     return nullptr;
520aa8523e4SRui Ueyama   }
521aa8523e4SRui Ueyama 
522aa8523e4SRui Ueyama   Sec = createSection(IS, OutsecName);
523aa8523e4SRui Ueyama   return Sec;
524aa8523e4SRui Ueyama }
525aa8523e4SRui Ueyama 
5260b1b695aSRui Ueyama // Add sections that didn't match any sections command.
527aa8523e4SRui Ueyama void LinkerScript::addOrphanSections() {
5286b394caaSRui Ueyama   unsigned End = SectionCommands.size();
529aa8523e4SRui Ueyama   StringMap<OutputSection *> Map;
530d2f225fdSRui Ueyama 
531f9b04fd9SGeorge Rimar   std::vector<OutputSection *> V;
5324f013bb3SRafael Espindola   for (InputSectionBase *S : InputSections) {
533db5e56f7SRafael Espindola     if (!S->Live || S->Parent)
5344f013bb3SRafael Espindola       continue;
535d2f225fdSRui Ueyama 
5364f013bb3SRafael Espindola     StringRef Name = getOutputSectionName(S->Name);
5375c4cb8a9SRui Ueyama 
5385c4cb8a9SRui Ueyama     if (Config->OrphanHandling == OrphanHandlingPolicy::Error)
5395c4cb8a9SRui Ueyama       error(toString(S) + " is being placed in '" + Name + "'");
5405c4cb8a9SRui Ueyama     else if (Config->OrphanHandling == OrphanHandlingPolicy::Warn)
5415c4cb8a9SRui Ueyama       warn(toString(S) + " is being placed in '" + Name + "'");
542d2f225fdSRui Ueyama 
543ac27de9dSRui Ueyama     if (OutputSection *Sec =
5446b394caaSRui Ueyama             findByName(makeArrayRef(SectionCommands).slice(0, End), Name)) {
5450e2bfb1eSRui Ueyama       Sec->addSection(cast<InputSection>(S));
546c54d5b16SRui Ueyama       continue;
547660c9ab9SRafael Espindola     }
548c54d5b16SRui Ueyama 
549aa8523e4SRui Ueyama     if (OutputSection *OS = addInputSec(Map, S, Name))
550f9b04fd9SGeorge Rimar       V.push_back(OS);
551c54d5b16SRui Ueyama     assert(S->getOutputSection()->SectionIndex == INT_MAX);
552d7faa916SRafael Espindola   }
553f9b04fd9SGeorge Rimar 
554f9b04fd9SGeorge Rimar   // If no SECTIONS command was given, we should insert sections commands
555f9b04fd9SGeorge Rimar   // before others, so that we can handle scripts which refers them,
556f9b04fd9SGeorge Rimar   // for example: "foo = ABSOLUTE(ADDR(.text)));".
557f9b04fd9SGeorge Rimar   // When SECTIONS command is present we just add all orphans to the end.
558f9b04fd9SGeorge Rimar   if (HasSectionsCommand)
559f9b04fd9SGeorge Rimar     SectionCommands.insert(SectionCommands.end(), V.begin(), V.end());
560f9b04fd9SGeorge Rimar   else
561f9b04fd9SGeorge Rimar     SectionCommands.insert(SectionCommands.begin(), V.begin(), V.end());
5624f013bb3SRafael Espindola }
563e63d81bdSEugene Leviant 
56471f84067SRui Ueyama uint64_t LinkerScript::advance(uint64_t Size, unsigned Alignment) {
56529b240c6SRui Ueyama   bool IsTbss =
56629b240c6SRui Ueyama       (Ctx->OutSec->Flags & SHF_TLS) && Ctx->OutSec->Type == SHT_NOBITS;
56729b240c6SRui Ueyama   uint64_t Start = IsTbss ? Dot + Ctx->ThreadBssOffset : Dot;
56871f84067SRui Ueyama   Start = alignTo(Start, Alignment);
5697c4eafa3SRafael Espindola   uint64_t End = Start + Size;
5707c4eafa3SRafael Espindola 
5717c4eafa3SRafael Espindola   if (IsTbss)
57229b240c6SRui Ueyama     Ctx->ThreadBssOffset = End - Dot;
5737c4eafa3SRafael Espindola   else
5747c4eafa3SRafael Espindola     Dot = End;
5757c4eafa3SRafael Espindola   return End;
576a940e539SRafael Espindola }
577a940e539SRafael Espindola 
578b8dd23f5SRui Ueyama void LinkerScript::output(InputSection *S) {
579f694d33fSGeorge Rimar   uint64_t Before = advance(0, 1);
5807c4eafa3SRafael Espindola   uint64_t Pos = advance(S->getSize(), S->Alignment);
58129b240c6SRui Ueyama   S->OutSecOff = Pos - S->getSize() - Ctx->OutSec->Addr;
582d3190795SRafael Espindola 
583d3190795SRafael Espindola   // Update output section size after adding each section. This is so that
584d3190795SRafael Espindola   // SIZEOF works correctly in the case below:
585d3190795SRafael Espindola   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
58629b240c6SRui Ueyama   Ctx->OutSec->Size = Pos - Ctx->OutSec->Addr;
587d3190795SRafael Espindola 
588b889744eSMeador Inge   // If there is a memory region associated with this input section, then
589b889744eSMeador Inge   // place the section in that region and update the region index.
59029b240c6SRui Ueyama   if (Ctx->MemRegion) {
59129b240c6SRui Ueyama     uint64_t &CurOffset = Ctx->MemRegionOffset[Ctx->MemRegion];
592f694d33fSGeorge Rimar     CurOffset += Pos - Before;
59329b240c6SRui Ueyama     uint64_t CurSize = CurOffset - Ctx->MemRegion->Origin;
59429b240c6SRui Ueyama     if (CurSize > Ctx->MemRegion->Length) {
59529b240c6SRui Ueyama       uint64_t OverflowAmt = CurSize - Ctx->MemRegion->Length;
59629b240c6SRui Ueyama       error("section '" + Ctx->OutSec->Name + "' will not fit in region '" +
59729b240c6SRui Ueyama             Ctx->MemRegion->Name + "': overflowed by " + Twine(OverflowAmt) +
59829b240c6SRui Ueyama             " bytes");
599b889744eSMeador Inge     }
600b889744eSMeador Inge   }
6012de509c3SRui Ueyama }
602ceabe80eSEugene Leviant 
603b8dd23f5SRui Ueyama void LinkerScript::switchTo(OutputSection *Sec) {
60429b240c6SRui Ueyama   if (Ctx->OutSec == Sec)
605d3190795SRafael Espindola     return;
606d3190795SRafael Espindola 
60729b240c6SRui Ueyama   Ctx->OutSec = Sec;
60829b240c6SRui Ueyama   Ctx->OutSec->Addr = advance(0, Ctx->OutSec->Alignment);
609b71d6f7aSEugene Leviant 
610b71d6f7aSEugene Leviant   // If neither AT nor AT> is specified for an allocatable section, the linker
611b71d6f7aSEugene Leviant   // will set the LMA such that the difference between VMA and LMA for the
612b71d6f7aSEugene Leviant   // section is the same as the preceding output section in the same region
613b71d6f7aSEugene Leviant   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
61429b240c6SRui Ueyama   if (Ctx->LMAOffset)
61529b240c6SRui Ueyama     Ctx->OutSec->LMAOffset = Ctx->LMAOffset();
616d3190795SRafael Espindola }
617d3190795SRafael Espindola 
618b889744eSMeador Inge // This function searches for a memory region to place the given output
619b889744eSMeador Inge // section in. If found, a pointer to the appropriate memory region is
620b889744eSMeador Inge // returned. Otherwise, a nullptr is returned.
6218c022ca7SRafael Espindola MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) {
622b889744eSMeador Inge   // If a memory region name was specified in the output section command,
623b889744eSMeador Inge   // then try to find that region first.
6248c022ca7SRafael Espindola   if (!Sec->MemoryRegionName.empty()) {
625ac27de9dSRui Ueyama     auto It = MemoryRegions.find(Sec->MemoryRegionName);
626ac27de9dSRui Ueyama     if (It != MemoryRegions.end())
6275f37541cSGeorge Rimar       return It->second;
6288c022ca7SRafael Espindola     error("memory region '" + Sec->MemoryRegionName + "' not declared");
629b889744eSMeador Inge     return nullptr;
630b889744eSMeador Inge   }
631b889744eSMeador Inge 
632d7c5400fSRui Ueyama   // If at least one memory region is defined, all sections must
633d7c5400fSRui Ueyama   // belong to some memory region. Otherwise, we don't need to do
634d7c5400fSRui Ueyama   // anything for memory regions.
635ac27de9dSRui Ueyama   if (MemoryRegions.empty())
636b889744eSMeador Inge     return nullptr;
637b889744eSMeador Inge 
638b889744eSMeador Inge   // See if a region can be found by matching section flags.
639ac27de9dSRui Ueyama   for (auto &Pair : MemoryRegions) {
6405f37541cSGeorge Rimar     MemoryRegion *M = Pair.second;
6415f37541cSGeorge Rimar     if ((M->Flags & Sec->Flags) && (M->NegFlags & Sec->Flags) == 0)
6425f37541cSGeorge Rimar       return M;
643b889744eSMeador Inge   }
644b889744eSMeador Inge 
645b889744eSMeador Inge   // Otherwise, no suitable region was found.
646b889744eSMeador Inge   if (Sec->Flags & SHF_ALLOC)
647b889744eSMeador Inge     error("no memory region specified for section '" + Sec->Name + "'");
648b889744eSMeador Inge   return nullptr;
649b889744eSMeador Inge }
650b889744eSMeador Inge 
6510b1b695aSRui Ueyama // This function assigns offsets to input sections and an output section
6520b1b695aSRui Ueyama // for a single sections command (e.g. ".text { *(.text); }").
6538c022ca7SRafael Espindola void LinkerScript::assignOffsets(OutputSection *Sec) {
654dece2808SRafael Espindola   if (!(Sec->Flags & SHF_ALLOC))
655dece2808SRafael Espindola     Dot = 0;
6568c022ca7SRafael Espindola   else if (Sec->AddrExpr)
6578c022ca7SRafael Espindola     setDot(Sec->AddrExpr, Sec->Location, false);
658679828ffSRafael Espindola 
65929b240c6SRui Ueyama   Ctx->MemRegion = Sec->MemRegion;
66029b240c6SRui Ueyama   if (Ctx->MemRegion)
66129b240c6SRui Ueyama     Dot = Ctx->MemRegionOffset[Ctx->MemRegion];
662c2dffe3aSGeorge Rimar 
6638c022ca7SRafael Espindola   if (Sec->LMAExpr) {
6640c1c8085SGeorge Rimar     uint64_t D = Dot;
66529b240c6SRui Ueyama     Ctx->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; };
6665784e96fSEugene Leviant   }
6675784e96fSEugene Leviant 
668b889744eSMeador Inge   switchTo(Sec);
6690b1b695aSRui Ueyama 
670d86a4e50SGeorge Rimar   // We do not support custom layout for compressed debug sectons.
671d86a4e50SGeorge Rimar   // At this point we already know their size and have compressed content.
67229b240c6SRui Ueyama   if (Ctx->OutSec->Flags & SHF_COMPRESSED)
673d86a4e50SGeorge Rimar     return;
674d86a4e50SGeorge Rimar 
6752f4c121dSRui Ueyama   // We visited SectionsCommands from processSectionCommands to
6762f4c121dSRui Ueyama   // layout sections. Now, we visit SectionsCommands again to fix
6772f4c121dSRui Ueyama   // section offsets.
6782f4c121dSRui Ueyama   for (BaseCommand *Base : Sec->SectionCommands) {
6792f4c121dSRui Ueyama     // This handles the assignments to symbol or to the dot.
6802f4c121dSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
6812f4c121dSRui Ueyama       assignSymbol(Cmd, true);
6822f4c121dSRui Ueyama       continue;
6832f4c121dSRui Ueyama     }
6842f4c121dSRui Ueyama 
6852f4c121dSRui Ueyama     // Handle BYTE(), SHORT(), LONG(), or QUAD().
686f0403c60SRui Ueyama     if (auto *Cmd = dyn_cast<ByteCommand>(Base)) {
6872f4c121dSRui Ueyama       Cmd->Offset = Dot - Ctx->OutSec->Addr;
6882f4c121dSRui Ueyama       Dot += Cmd->Size;
6892f4c121dSRui Ueyama       Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr;
6902f4c121dSRui Ueyama       continue;
6912f4c121dSRui Ueyama     }
6922f4c121dSRui Ueyama 
6932f4c121dSRui Ueyama     // Handle ASSERT().
6942f4c121dSRui Ueyama     if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
6952f4c121dSRui Ueyama       Cmd->Expression();
6962f4c121dSRui Ueyama       continue;
6972f4c121dSRui Ueyama     }
6982f4c121dSRui Ueyama 
6992f4c121dSRui Ueyama     // Handle a single input section description command.
7002f4c121dSRui Ueyama     // It calculates and assigns the offsets for each section and also
7012f4c121dSRui Ueyama     // updates the output section size.
7022f4c121dSRui Ueyama     auto *Cmd = cast<InputSectionDescription>(Base);
7032f4c121dSRui Ueyama     for (InputSection *Sec : Cmd->Sections) {
7042f4c121dSRui Ueyama       // We tentatively added all synthetic sections at the beginning and
7052f4c121dSRui Ueyama       // removed empty ones afterwards (because there is no way to know
7062f4c121dSRui Ueyama       // whether they were going be empty or not other than actually running
7072f4c121dSRui Ueyama       // linker scripts.) We need to ignore remains of empty sections.
7082f4c121dSRui Ueyama       if (auto *S = dyn_cast<SyntheticSection>(Sec))
7092f4c121dSRui Ueyama         if (S->empty())
7102f4c121dSRui Ueyama           continue;
7112f4c121dSRui Ueyama 
7122f4c121dSRui Ueyama       if (!Sec->Live)
7132f4c121dSRui Ueyama         continue;
7142f4c121dSRui Ueyama       assert(Ctx->OutSec == Sec->getParent());
7152f4c121dSRui Ueyama       output(Sec);
7162f4c121dSRui Ueyama     }
7172f4c121dSRui Ueyama   }
718d3190795SRafael Espindola }
719d3190795SRafael Espindola 
720b8dd23f5SRui Ueyama void LinkerScript::removeEmptyCommands() {
7216d38e4dbSRafael Espindola   // It is common practice to use very generic linker scripts. So for any
7226d38e4dbSRafael Espindola   // given run some of the output sections in the script will be empty.
7236d38e4dbSRafael Espindola   // We could create corresponding empty output sections, but that would
7246d38e4dbSRafael Espindola   // clutter the output.
7256d38e4dbSRafael Espindola   // We instead remove trivially empty sections. The bfd linker seems even
7266d38e4dbSRafael Espindola   // more aggressive at removing them.
7276b394caaSRui Ueyama   llvm::erase_if(SectionCommands, [&](BaseCommand *Base) {
7288c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Base))
7298c022ca7SRafael Espindola       return !Sec->Live;
7300b1b695aSRui Ueyama     return false;
7316d38e4dbSRafael Espindola   });
73207fe6129SRafael Espindola }
73307fe6129SRafael Espindola 
7348c022ca7SRafael Espindola static bool isAllSectionDescription(const OutputSection &Cmd) {
7356b394caaSRui Ueyama   for (BaseCommand *Base : Cmd.SectionCommands)
7368f99f73cSRui Ueyama     if (!isa<InputSectionDescription>(*Base))
7376a53737cSRafael Espindola       return false;
7386a53737cSRafael Espindola   return true;
7396a53737cSRafael Espindola }
7406d38e4dbSRafael Espindola 
741b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsBeforeSorting() {
7429546fffbSRafael Espindola   // If the output section contains only symbol assignments, create a
74326fa916dSGeorge Rimar   // corresponding output section. The issue is what to do with linker script
74426fa916dSGeorge Rimar   // like ".foo : { symbol = 42; }". One option would be to convert it to
74526fa916dSGeorge Rimar   // "symbol = 42;". That is, move the symbol out of the empty section
74626fa916dSGeorge Rimar   // description. That seems to be what bfd does for this simple case. The
74726fa916dSGeorge Rimar   // problem is that this is not completely general. bfd will give up and
74826fa916dSGeorge Rimar   // create a dummy section too if there is a ". = . + 1" inside the section
74926fa916dSGeorge Rimar   // for example.
75026fa916dSGeorge Rimar   // Given that we want to create the section, we have to worry what impact
75126fa916dSGeorge Rimar   // it will have on the link. For example, if we just create a section with
75226fa916dSGeorge Rimar   // 0 for flags, it would change which PT_LOADs are created.
75326fa916dSGeorge Rimar   // We could remember that that particular section is dummy and ignore it in
75426fa916dSGeorge Rimar   // other parts of the linker, but unfortunately there are quite a few places
75526fa916dSGeorge Rimar   // that would need to change:
75626fa916dSGeorge Rimar   //   * The program header creation.
75726fa916dSGeorge Rimar   //   * The orphan section placement.
75826fa916dSGeorge Rimar   //   * The address assignment.
75926fa916dSGeorge Rimar   // The other option is to pick flags that minimize the impact the section
76026fa916dSGeorge Rimar   // will have on the rest of the linker. That is why we copy the flags from
76126fa916dSGeorge Rimar   // the previous sections. Only a few flags are needed to keep the impact low.
7620c1c8085SGeorge Rimar   uint64_t Flags = SHF_ALLOC;
763660c9ab9SRafael Espindola 
7646b394caaSRui Ueyama   for (BaseCommand *Cmd : SectionCommands) {
7658962db91SGeorge Rimar     auto *Sec = dyn_cast<OutputSection>(Cmd);
7668c022ca7SRafael Espindola     if (!Sec)
7679546fffbSRafael Espindola       continue;
7688c022ca7SRafael Espindola     if (Sec->Live) {
76926fa916dSGeorge Rimar       Flags = Sec->Flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR);
7709546fffbSRafael Espindola       continue;
7719546fffbSRafael Espindola     }
7729546fffbSRafael Espindola 
7738c022ca7SRafael Espindola     if (isAllSectionDescription(*Sec))
7746a53737cSRafael Espindola       continue;
7756a53737cSRafael Espindola 
7768c022ca7SRafael Espindola     Sec->Live = true;
7778c022ca7SRafael Espindola     Sec->Flags = Flags;
7789546fffbSRafael Espindola   }
779f7a17448SRafael Espindola }
780f7a17448SRafael Espindola 
781b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsAfterSorting() {
782feed7506SRafael Espindola   // Try and find an appropriate memory region to assign offsets in.
7836b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
7848c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Base)) {
785ba45584cSGeorge Rimar       if (!Sec->Live)
786ba45584cSGeorge Rimar         continue;
7878c022ca7SRafael Espindola       Sec->MemRegion = findMemoryRegion(Sec);
788d1960dc0SRafael Espindola       // Handle align (e.g. ".foo : ALIGN(16) { ... }").
7898c022ca7SRafael Espindola       if (Sec->AlignExpr)
7908befefb2SRui Ueyama         Sec->Alignment =
7918befefb2SRui Ueyama             std::max<uint32_t>(Sec->Alignment, Sec->AlignExpr().getValue());
792d1960dc0SRafael Espindola     }
793d1960dc0SRafael Espindola   }
794feed7506SRafael Espindola 
795f7a17448SRafael Espindola   // If output section command doesn't specify any segments,
796f7a17448SRafael Espindola   // and we haven't previously assigned any section to segment,
797f7a17448SRafael Espindola   // then we simply assign section to the very first load segment.
798f7a17448SRafael Espindola   // Below is an example of such linker script:
799f7a17448SRafael Espindola   // PHDRS { seg PT_LOAD; }
800f7a17448SRafael Espindola   // SECTIONS { .aaa : { *(.aaa) } }
801f7a17448SRafael Espindola   std::vector<StringRef> DefPhdrs;
802f7a17448SRafael Espindola   auto FirstPtLoad =
803ac27de9dSRui Ueyama       std::find_if(PhdrsCommands.begin(), PhdrsCommands.end(),
804f7a17448SRafael Espindola                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
805ac27de9dSRui Ueyama   if (FirstPtLoad != PhdrsCommands.end())
806f7a17448SRafael Espindola     DefPhdrs.push_back(FirstPtLoad->Name);
807f7a17448SRafael Espindola 
808f7a17448SRafael Espindola   // Walk the commands and propagate the program headers to commands that don't
809f7a17448SRafael Espindola   // explicitly specify them.
8106b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
8118c022ca7SRafael Espindola     auto *Sec = dyn_cast<OutputSection>(Base);
8128c022ca7SRafael Espindola     if (!Sec)
813f7a17448SRafael Espindola       continue;
8148f99f73cSRui Ueyama 
8158c022ca7SRafael Espindola     if (Sec->Phdrs.empty()) {
816a020d348SAndrew Ng       // To match the bfd linker script behaviour, only propagate program
817a020d348SAndrew Ng       // headers to sections that are allocated.
8188c022ca7SRafael Espindola       if (Sec->Flags & SHF_ALLOC)
8198c022ca7SRafael Espindola         Sec->Phdrs = DefPhdrs;
820a020d348SAndrew Ng     } else {
8218c022ca7SRafael Espindola       DefPhdrs = Sec->Phdrs;
822f7a17448SRafael Espindola     }
823a020d348SAndrew Ng   }
8249546fffbSRafael Espindola }
8259546fffbSRafael Espindola 
826582ede89SGeorge Rimar static OutputSection *findFirstSection(PhdrEntry *Load) {
827582ede89SGeorge Rimar   for (OutputSection *Sec : OutputSections)
828582ede89SGeorge Rimar     if (Sec->PtLoad == Load)
829582ede89SGeorge Rimar       return Sec;
830582ede89SGeorge Rimar   return nullptr;
831582ede89SGeorge Rimar }
832582ede89SGeorge Rimar 
833b93c5b9fSPetr Hosek // Try to find an address for the file and program headers output sections,
834b93c5b9fSPetr Hosek // which were unconditionally added to the first PT_LOAD segment earlier.
835b93c5b9fSPetr Hosek //
836b93c5b9fSPetr Hosek // When using the default layout, we check if the headers fit below the first
837b93c5b9fSPetr Hosek // allocated section. When using a linker script, we also check if the headers
838b93c5b9fSPetr Hosek // are covered by the output section. This allows omitting the headers by not
839b93c5b9fSPetr Hosek // leaving enough space for them in the linker script; this pattern is common
840b93c5b9fSPetr Hosek // in embedded systems.
841b93c5b9fSPetr Hosek //
842b93c5b9fSPetr Hosek // If there isn't enough space for these sections, we'll remove them from the
843b93c5b9fSPetr Hosek // PT_LOAD segment, and we'll also remove the PT_PHDR segment.
844aa354187SGeorge Rimar void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) {
8455aedebffSPeter Smith   uint64_t Min = std::numeric_limits<uint64_t>::max();
8468c022ca7SRafael Espindola   for (OutputSection *Sec : OutputSections)
8475aedebffSPeter Smith     if (Sec->Flags & SHF_ALLOC)
8485aedebffSPeter Smith       Min = std::min<uint64_t>(Min, Sec->Addr);
8495aedebffSPeter Smith 
850aa354187SGeorge Rimar   auto It = llvm::find_if(
851aa354187SGeorge Rimar       Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; });
852aa354187SGeorge Rimar   if (It == Phdrs.end())
853d971e703SGeorge Rimar     return;
854aa354187SGeorge Rimar   PhdrEntry *FirstPTLoad = *It;
85502ed7575SRafael Espindola 
85602ed7575SRafael Espindola   uint64_t HeaderSize = getHeaderSize();
857b93c5b9fSPetr Hosek   // When linker script with SECTIONS is being used, don't output headers
858b93c5b9fSPetr Hosek   // unless there's a space for them.
859a323e2a7SRui Ueyama   uint64_t Base = HasSectionsCommand ? alignDown(Min, Config->MaxPageSize) : 0;
860b93c5b9fSPetr Hosek   if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) {
8611f0fe88aSRafael Espindola     Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
86202ed7575SRafael Espindola     Out::ElfHeader->Addr = Min;
86302ed7575SRafael Espindola     Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
864d971e703SGeorge Rimar     return;
86502ed7575SRafael Espindola   }
86602ed7575SRafael Espindola 
867582ede89SGeorge Rimar   Out::ElfHeader->PtLoad = nullptr;
868582ede89SGeorge Rimar   Out::ProgramHeaders->PtLoad = nullptr;
8696823c5f0SGeorge Rimar   FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad);
87002ed7575SRafael Espindola 
87160608a8aSGeorge Rimar   llvm::erase_if(Phdrs,
87260608a8aSGeorge Rimar                  [](const PhdrEntry *E) { return E->p_type == PT_PHDR; });
87302ed7575SRafael Espindola }
87402ed7575SRafael Espindola 
875ac27de9dSRui Ueyama LinkerScript::AddressState::AddressState() {
876ac27de9dSRui Ueyama   for (auto &MRI : Script->MemoryRegions) {
8775f37541cSGeorge Rimar     const MemoryRegion *MR = MRI.second;
878906e9a18SPeter Smith     MemRegionOffset[MR] = MR->Origin;
879906e9a18SPeter Smith   }
880906e9a18SPeter Smith }
881906e9a18SPeter Smith 
882f9b04fd9SGeorge Rimar static uint64_t getInitialDot() {
883f9b04fd9SGeorge Rimar   // By default linker scripts use an initial value of 0 for '.',
884f9b04fd9SGeorge Rimar   // but prefer -image-base if set.
885f9b04fd9SGeorge Rimar   if (Script->HasSectionsCommand)
886f9b04fd9SGeorge Rimar     return Config->ImageBase ? *Config->ImageBase : 0;
887f9b04fd9SGeorge Rimar 
888f9b04fd9SGeorge Rimar   uint64_t StartAddr = UINT64_MAX;
889f9b04fd9SGeorge Rimar   // The Sections with -T<section> have been sorted in order of ascending
890f9b04fd9SGeorge Rimar   // address. We must lower StartAddr if the lowest -T<section address> as
891f9b04fd9SGeorge Rimar   // calls to setDot() must be monotonically increasing.
892f9b04fd9SGeorge Rimar   for (auto &KV : Config->SectionStartMap)
893f9b04fd9SGeorge Rimar     StartAddr = std::min(StartAddr, KV.second);
894f9b04fd9SGeorge Rimar   return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize());
895f9b04fd9SGeorge Rimar }
896f9b04fd9SGeorge Rimar 
897f9b04fd9SGeorge Rimar // Here we assign addresses as instructed by linker script SECTIONS
898f9b04fd9SGeorge Rimar // sub-commands. Doing that allows us to use final VA values, so here
899f9b04fd9SGeorge Rimar // we also handle rest commands like symbol assignments and ASSERTs.
900b5ca92efSJames Henderson void LinkerScript::assignAddresses() {
901f9b04fd9SGeorge Rimar   Dot = getInitialDot();
90218d19687SRui Ueyama 
903089dac7bSRafael Espindola   auto Deleter = make_unique<AddressState>();
904089dac7bSRafael Espindola   Ctx = Deleter.get();
90572dc195dSRafael Espindola   ErrorOnMissingSection = true;
90606f4743aSRafael Espindola   switchTo(Aether);
90706f4743aSRafael Espindola 
9086b394caaSRui Ueyama   for (BaseCommand *Base : SectionCommands) {
9098f99f73cSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
910d379f735SRui Ueyama       assignSymbol(Cmd, false);
91105ef4cffSRui Ueyama       continue;
912652852c5SGeorge Rimar     }
913652852c5SGeorge Rimar 
9148f99f73cSRui Ueyama     if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
9154595df94SRafael Espindola       Cmd->Expression();
916eefa758eSGeorge Rimar       continue;
917eefa758eSGeorge Rimar     }
918eefa758eSGeorge Rimar 
9198c022ca7SRafael Espindola     assignOffsets(cast<OutputSection>(Base));
920a14b13d8SGeorge Rimar   }
92129b240c6SRui Ueyama   Ctx = nullptr;
922fb8978fcSDima Stepanov }
923652852c5SGeorge Rimar 
924464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command.
925aa354187SGeorge Rimar std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
926aa354187SGeorge Rimar   std::vector<PhdrEntry *> Ret;
927bbe38602SEugene Leviant 
928464daadcSRui Ueyama   // Process PHDRS and FILEHDR keywords because they are not
929464daadcSRui Ueyama   // real output sections and cannot be added in the following loop.
930ac27de9dSRui Ueyama   for (const PhdrsCommand &Cmd : PhdrsCommands) {
9310ae2c24cSRui Ueyama     PhdrEntry *Phdr = make<PhdrEntry>(Cmd.Type, Cmd.Flags ? *Cmd.Flags : PF_R);
932bbe38602SEugene Leviant 
933bbe38602SEugene Leviant     if (Cmd.HasFilehdr)
934aa354187SGeorge Rimar       Phdr->add(Out::ElfHeader);
935bbe38602SEugene Leviant     if (Cmd.HasPhdrs)
936aa354187SGeorge Rimar       Phdr->add(Out::ProgramHeaders);
93756b21c86SEugene Leviant 
93856b21c86SEugene Leviant     if (Cmd.LMAExpr) {
939aa354187SGeorge Rimar       Phdr->p_paddr = Cmd.LMAExpr().getValue();
940aa354187SGeorge Rimar       Phdr->HasLMA = true;
94156b21c86SEugene Leviant     }
942aa354187SGeorge Rimar     Ret.push_back(Phdr);
943bbe38602SEugene Leviant   }
944bbe38602SEugene Leviant 
945464daadcSRui Ueyama   // Add output sections to program headers.
9468c022ca7SRafael Espindola   for (OutputSection *Sec : OutputSections) {
947bbe38602SEugene Leviant     // Assign headers specified by linker script
9488c022ca7SRafael Espindola     for (size_t Id : getPhdrIndices(Sec)) {
949aa354187SGeorge Rimar       Ret[Id]->add(Sec);
950ac27de9dSRui Ueyama       if (!PhdrsCommands[Id].Flags.hasValue())
951aa354187SGeorge Rimar         Ret[Id]->p_flags |= Sec->getPhdrFlags();
952bbe38602SEugene Leviant     }
953bbe38602SEugene Leviant   }
954edebbdf1SRui Ueyama   return Ret;
955bbe38602SEugene Leviant }
956bbe38602SEugene Leviant 
957e03ba023SRui Ueyama // Returns true if we should emit an .interp section.
958e03ba023SRui Ueyama //
959e03ba023SRui Ueyama // We usually do. But if PHDRS commands are given, and
960e03ba023SRui Ueyama // no PT_INTERP is there, there's no place to emit an
961e03ba023SRui Ueyama // .interp, so we don't do that in that case.
962e03ba023SRui Ueyama bool LinkerScript::needsInterpSection() {
963ac27de9dSRui Ueyama   if (PhdrsCommands.empty())
964e03ba023SRui Ueyama     return true;
965ac27de9dSRui Ueyama   for (PhdrsCommand &Cmd : PhdrsCommands)
966e31d9886SRui Ueyama     if (Cmd.Type == PT_INTERP)
967e31d9886SRui Ueyama       return true;
968e03ba023SRui Ueyama   return false;
969f9bc3bd2SEugene Leviant }
970f9bc3bd2SEugene Leviant 
971722221f5SRui Ueyama ExprValue LinkerScript::getSymbolValue(StringRef Name, const Twine &Loc) {
972722221f5SRui Ueyama   if (Name == ".") {
97329b240c6SRui Ueyama     if (Ctx)
97429b240c6SRui Ueyama       return {Ctx->OutSec, false, Dot - Ctx->OutSec->Addr, Loc};
9757e5b0a59SGeorge Rimar     error(Loc + ": unable to get location counter value");
9767e5b0a59SGeorge Rimar     return 0;
9777e5b0a59SGeorge Rimar   }
978f5733500SRui Ueyama 
979e9a9e0a1SPeter Collingbourne   if (auto *Sym = dyn_cast_or_null<Defined>(Symtab->find(Name)))
980f5733500SRui Ueyama     return {Sym->Section, false, Sym->Value, Loc};
981f5733500SRui Ueyama 
982722221f5SRui Ueyama   error(Loc + ": symbol not found: " + Name);
983884e786dSGeorge Rimar   return 0;
984884e786dSGeorge Rimar }
985884e786dSGeorge Rimar 
986656be311SRui Ueyama // Returns the index of the segment named Name.
987656be311SRui Ueyama static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> Vec,
988656be311SRui Ueyama                                      StringRef Name) {
989656be311SRui Ueyama   for (size_t I = 0; I < Vec.size(); ++I)
990656be311SRui Ueyama     if (Vec[I].Name == Name)
991656be311SRui Ueyama       return I;
992656be311SRui Ueyama   return None;
993656be311SRui Ueyama }
994656be311SRui Ueyama 
9952c923c2cSRafael Espindola // Returns indices of ELF headers containing specific section. Each index is a
9962c923c2cSRafael Espindola // zero based number of ELF header listed within PHDRS {} script block.
9978c022ca7SRafael Espindola std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) {
99829c5a2a9SRui Ueyama   std::vector<size_t> Ret;
999656be311SRui Ueyama 
1000656be311SRui Ueyama   for (StringRef S : Cmd->Phdrs) {
1001ac27de9dSRui Ueyama     if (Optional<size_t> Idx = getPhdrIndex(PhdrsCommands, S))
100287223574SRui Ueyama       Ret.push_back(*Idx);
1003656be311SRui Ueyama     else if (S != "NONE")
1004656be311SRui Ueyama       error(Cmd->Location + ": section header '" + S +
1005656be311SRui Ueyama             "' is not listed in PHDRS");
1006bbe38602SEugene Leviant   }
1007656be311SRui Ueyama   return Ret;
100829c5a2a9SRui Ueyama }
1009