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 {
74a6acd23cSRafael Espindola   return getValue() - getSecAddr();
75a6acd23cSRafael Espindola }
76a6acd23cSRafael Espindola 
77a5e8dd35SRafael Espindola static SymbolBody *addRegular(SymbolAssignment *Cmd) {
785e51f7d2SPetr Hosek   Symbol *Sym;
793dabfc6bSRafael Espindola   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
80244ef981SRafael Espindola   std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, /*Type*/ 0, Visibility,
81244ef981SRafael Espindola                                               /*CanOmitFromDynSym*/ false,
825e51f7d2SPetr Hosek                                               /*File*/ nullptr);
835e51f7d2SPetr Hosek   Sym->Binding = STB_GLOBAL;
8472dc195dSRafael Espindola   ExprValue Value = Cmd->Expression();
8572dc195dSRafael Espindola   SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
8601aa795fSGeorge Rimar 
8701aa795fSGeorge Rimar   // We want to set symbol values early if we can. This allows us to use symbols
8801aa795fSGeorge Rimar   // as variables in linker scripts. Doing so allows us to write expressions
8901aa795fSGeorge Rimar   // like this: `alignment = 16; . = ALIGN(., alignment)`
9001aa795fSGeorge Rimar   uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0;
916e93d054SRafael Espindola   replaceBody<DefinedRegular>(Sym, nullptr, Cmd->Name, /*IsLocal=*/false,
926e93d054SRafael Espindola                               Visibility, STT_NOTYPE, SymValue, 0, Sec);
938f1f3c40SMeador Inge   return Sym->body();
94ceabe80eSEugene Leviant }
95ceabe80eSEugene Leviant 
968c022ca7SRafael Espindola OutputSection *LinkerScript::createOutputSection(StringRef Name,
978c022ca7SRafael Espindola                                                  StringRef Location) {
988c022ca7SRafael Espindola   OutputSection *&SecRef = NameToOutputSection[Name];
998c022ca7SRafael Espindola   OutputSection *Sec;
1008c022ca7SRafael Espindola   if (SecRef && SecRef->Location.empty()) {
10105c4f67cSRafael Espindola     // There was a forward reference.
1028c022ca7SRafael Espindola     Sec = SecRef;
10305c4f67cSRafael Espindola   } else {
1048c022ca7SRafael Espindola     Sec = make<OutputSection>(Name, SHT_PROGBITS, 0);
1058c022ca7SRafael Espindola     if (!SecRef)
1068c022ca7SRafael Espindola       SecRef = Sec;
10705c4f67cSRafael Espindola   }
1088c022ca7SRafael Espindola   Sec->Location = Location;
1098c022ca7SRafael Espindola   return Sec;
110851dc1e8SGeorge Rimar }
111851dc1e8SGeorge Rimar 
1128c022ca7SRafael Espindola OutputSection *LinkerScript::getOrCreateOutputSection(StringRef Name) {
1138c022ca7SRafael Espindola   OutputSection *&CmdRef = NameToOutputSection[Name];
11405c4f67cSRafael Espindola   if (!CmdRef)
1158c022ca7SRafael Espindola     CmdRef = make<OutputSection>(Name, SHT_PROGBITS, 0);
11605c4f67cSRafael Espindola   return CmdRef;
117d83ce1b4SGeorge Rimar }
118d83ce1b4SGeorge Rimar 
119b8dd23f5SRui Ueyama void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
12072dc195dSRafael Espindola   uint64_t Val = E().getValue();
1218c804d97SGeorge Rimar   if (Val < Dot && InSec)
1222ee2d2dcSGeorge Rimar     error(Loc + ": unable to move location counter backward for: " +
123906e9a18SPeter Smith           CurAddressState->OutSec->Name);
1244cd7352cSRafael Espindola   Dot = Val;
1254cd7352cSRafael Espindola   // Update to location counter means update to section size.
1264cd7352cSRafael Espindola   if (InSec)
127906e9a18SPeter Smith     CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr;
128679828ffSRafael Espindola }
129679828ffSRafael Espindola 
130679828ffSRafael Espindola // Sets value of a symbol. Two kinds of symbols are processed: synthetic
131679828ffSRafael Espindola // symbols, whose value is an offset from beginning of section and regular
132679828ffSRafael Espindola // symbols whose value is absolute.
133b8dd23f5SRui Ueyama void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
134679828ffSRafael Espindola   if (Cmd->Name == ".") {
1352ee2d2dcSGeorge Rimar     setDot(Cmd->Expression, Cmd->Location, InSec);
1364cd7352cSRafael Espindola     return;
1374cd7352cSRafael Espindola   }
1384cd7352cSRafael Espindola 
139b2b70975SGeorge Rimar   if (!Cmd->Sym)
1408f1f3c40SMeador Inge     return;
1418f1f3c40SMeador Inge 
1425616adf6SRafael Espindola   auto *Sym = cast<DefinedRegular>(Cmd->Sym);
14372dc195dSRafael Espindola   ExprValue V = Cmd->Expression();
14472dc195dSRafael Espindola   if (V.isAbsolute()) {
14572dc195dSRafael Espindola     Sym->Value = V.getValue();
14672dc195dSRafael Espindola   } else {
14772dc195dSRafael Espindola     Sym->Section = V.Sec;
148a6acd23cSRafael Espindola     Sym->Value = V.getSectionOffset();
149ea590d91SRafael Espindola   }
1508f1f3c40SMeador Inge }
1518f1f3c40SMeador Inge 
152b8dd23f5SRui Ueyama void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
1531602421cSRui Ueyama   if (Cmd->Name == ".")
1548f1f3c40SMeador Inge     return;
1558f1f3c40SMeador Inge 
1568f1f3c40SMeador Inge   // If a symbol was in PROVIDE(), we need to define it only when
1578f1f3c40SMeador Inge   // it is a referenced undefined symbol.
158244ef981SRafael Espindola   SymbolBody *B = Symtab->find(Cmd->Name);
1598f1f3c40SMeador Inge   if (Cmd->Provide && (!B || B->isDefined()))
1608f1f3c40SMeador Inge     return;
1618f1f3c40SMeador Inge 
162a5e8dd35SRafael Espindola   Cmd->Sym = addRegular(Cmd);
163ceabe80eSEugene Leviant }
164ceabe80eSEugene Leviant 
165076fe157SGeorge Rimar bool SymbolAssignment::classof(const BaseCommand *C) {
166076fe157SGeorge Rimar   return C->Kind == AssignmentKind;
167076fe157SGeorge Rimar }
168076fe157SGeorge Rimar 
169eea3114fSGeorge Rimar bool InputSectionDescription::classof(const BaseCommand *C) {
170eea3114fSGeorge Rimar   return C->Kind == InputSectionKind;
171eea3114fSGeorge Rimar }
172eea3114fSGeorge Rimar 
173eefa758eSGeorge Rimar bool AssertCommand::classof(const BaseCommand *C) {
174eefa758eSGeorge Rimar   return C->Kind == AssertKind;
175eefa758eSGeorge Rimar }
176eefa758eSGeorge Rimar 
177e38cbab5SGeorge Rimar bool BytesDataCommand::classof(const BaseCommand *C) {
178e38cbab5SGeorge Rimar   return C->Kind == BytesDataKind;
179e38cbab5SGeorge Rimar }
180e38cbab5SGeorge Rimar 
1811e30f07cSDmitry Mikulin static std::string filename(InputFile *File) {
1821e30f07cSDmitry Mikulin   if (!File)
183e0be2901SRui Ueyama     return "";
1841e30f07cSDmitry Mikulin   if (File->ArchiveName.empty())
1851e30f07cSDmitry Mikulin     return File->getName();
1861e30f07cSDmitry Mikulin   return (File->ArchiveName + "(" + File->getName() + ")").str();
187e0be2901SRui Ueyama }
188e0be2901SRui Ueyama 
189b8dd23f5SRui Ueyama bool LinkerScript::shouldKeep(InputSectionBase *S) {
190f300ca21SDmitry Mikulin   for (InputSectionDescription *ID : Opt.KeptSections) {
1911e30f07cSDmitry Mikulin     std::string Filename = filename(S->File);
192f300ca21SDmitry Mikulin     if (ID->FilePat.match(Filename))
193cf43f179SEugene Leviant       for (SectionPattern &P : ID->SectionPatterns)
194f91282e1SRui Ueyama         if (P.SectionPat.match(S->Name))
195eea3114fSGeorge Rimar           return true;
196f300ca21SDmitry Mikulin   }
197eea3114fSGeorge Rimar   return false;
198eea3114fSGeorge Rimar }
199eea3114fSGeorge Rimar 
200ea93fe00SRui Ueyama // A helper function for the SORT() command.
201c404d50dSRafael Espindola static std::function<bool(InputSectionBase *, InputSectionBase *)>
202be394db3SGeorge Rimar getComparator(SortSectionPolicy K) {
203be394db3SGeorge Rimar   switch (K) {
204be394db3SGeorge Rimar   case SortSectionPolicy::Alignment:
205ea93fe00SRui Ueyama     return [](InputSectionBase *A, InputSectionBase *B) {
206ea93fe00SRui Ueyama       // ">" is not a mistake. Sections with larger alignments are placed
207ea93fe00SRui Ueyama       // before sections with smaller alignments in order to reduce the
208ea93fe00SRui Ueyama       // amount of padding necessary. This is compatible with GNU.
209ea93fe00SRui Ueyama       return A->Alignment > B->Alignment;
210ea93fe00SRui Ueyama     };
211be394db3SGeorge Rimar   case SortSectionPolicy::Name:
212ea93fe00SRui Ueyama     return [](InputSectionBase *A, InputSectionBase *B) {
213ea93fe00SRui Ueyama       return A->Name < B->Name;
214ea93fe00SRui Ueyama     };
215be394db3SGeorge Rimar   case SortSectionPolicy::Priority:
216ea93fe00SRui Ueyama     return [](InputSectionBase *A, InputSectionBase *B) {
217ea93fe00SRui Ueyama       return getPriority(A->Name) < getPriority(B->Name);
218ea93fe00SRui Ueyama     };
219be394db3SGeorge Rimar   default:
220be394db3SGeorge Rimar     llvm_unreachable("unknown sort policy");
221be394db3SGeorge Rimar   }
222742c3836SRui Ueyama }
2230702c4e8SGeorge Rimar 
224ea93fe00SRui Ueyama // A helper function for the SORT() command.
225b4c9b81aSRafael Espindola static bool matchConstraints(ArrayRef<InputSectionBase *> Sections,
22606ae6836SGeorge Rimar                              ConstraintKind Kind) {
2278f66df92SGeorge Rimar   if (Kind == ConstraintKind::NoConstraint)
2288f66df92SGeorge Rimar     return true;
2292c7171bfSRui Ueyama 
2302c7171bfSRui Ueyama   bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) {
2312c7171bfSRui Ueyama     return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE;
23206ae6836SGeorge Rimar   });
2332c7171bfSRui Ueyama 
234e746e52cSRafael Espindola   return (IsRW && Kind == ConstraintKind::ReadWrite) ||
235e746e52cSRafael Espindola          (!IsRW && Kind == ConstraintKind::ReadOnly);
23606ae6836SGeorge Rimar }
23706ae6836SGeorge Rimar 
2386a1aa8d9SRafael Espindola static void sortSections(InputSection **Begin, InputSection **End,
239ee924709SRui Ueyama                          SortSectionPolicy K) {
240ee924709SRui Ueyama   if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
24107171f21SGeorge Rimar     std::stable_sort(Begin, End, getComparator(K));
242ee924709SRui Ueyama }
243ee924709SRui Ueyama 
244d6bcde38SGeorge Rimar static llvm::DenseMap<SectionBase *, int> getSectionOrder() {
245d6bcde38SGeorge Rimar   switch (Config->EKind) {
246d6bcde38SGeorge Rimar   case ELF32LEKind:
247d6bcde38SGeorge Rimar     return buildSectionOrder<ELF32LE>();
248d6bcde38SGeorge Rimar   case ELF32BEKind:
249d6bcde38SGeorge Rimar     return buildSectionOrder<ELF32BE>();
250d6bcde38SGeorge Rimar   case ELF64LEKind:
251d6bcde38SGeorge Rimar     return buildSectionOrder<ELF64LE>();
252d6bcde38SGeorge Rimar   case ELF64BEKind:
253d6bcde38SGeorge Rimar     return buildSectionOrder<ELF64BE>();
254d6bcde38SGeorge Rimar   default:
255d6bcde38SGeorge Rimar     llvm_unreachable("unknown ELF type");
256d6bcde38SGeorge Rimar   }
257d6bcde38SGeorge Rimar }
258d6bcde38SGeorge Rimar 
259d6bcde38SGeorge Rimar static void sortBySymbolOrder(InputSection **Begin, InputSection **End) {
260d6bcde38SGeorge Rimar   if (Config->SymbolOrderingFile.empty())
261d6bcde38SGeorge Rimar     return;
262d6bcde38SGeorge Rimar   static llvm::DenseMap<SectionBase *, int> Order = getSectionOrder();
263d6bcde38SGeorge Rimar   MutableArrayRef<InputSection *> In(Begin, End - Begin);
264d6bcde38SGeorge Rimar   sortByOrder(In, [&](InputSectionBase *S) { return Order.lookup(S); });
265d6bcde38SGeorge Rimar }
266d6bcde38SGeorge Rimar 
267d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches.
2686a1aa8d9SRafael Espindola std::vector<InputSection *>
26972e107f3SRui Ueyama LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
2706a1aa8d9SRafael Espindola   std::vector<InputSection *> Ret;
2718c6a5aafSRui Ueyama 
27272e107f3SRui Ueyama   // Collects all sections that satisfy constraints of Cmd.
27372e107f3SRui Ueyama   for (const SectionPattern &Pat : Cmd->SectionPatterns) {
27472e107f3SRui Ueyama     size_t SizeBefore = Ret.size();
27572e107f3SRui Ueyama 
27672e107f3SRui Ueyama     for (InputSectionBase *Sec : InputSections) {
27772e107f3SRui Ueyama       if (Sec->Assigned)
2788c6a5aafSRui Ueyama         continue;
27972e107f3SRui Ueyama 
280e39709b2SRafael Espindola       if (!Sec->Live) {
281e39709b2SRafael Espindola         reportDiscarded(Sec);
282e39709b2SRafael Espindola         continue;
283e39709b2SRafael Espindola       }
284e39709b2SRafael Espindola 
285908a3d34SRafael Espindola       // For -emit-relocs we have to ignore entries like
286908a3d34SRafael Espindola       //   .rela.dyn : { *(.rela.data) }
287908a3d34SRafael Espindola       // which are common because they are in the default bfd script.
28872e107f3SRui Ueyama       if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
289908a3d34SRafael Espindola         continue;
2908c6a5aafSRui Ueyama 
2911e30f07cSDmitry Mikulin       std::string Filename = filename(Sec->File);
29272e107f3SRui Ueyama       if (!Cmd->FilePat.match(Filename) ||
29372e107f3SRui Ueyama           Pat.ExcludedFilePat.match(Filename) ||
29472e107f3SRui Ueyama           !Pat.SectionPat.match(Sec->Name))
295e0be2901SRui Ueyama         continue;
29672e107f3SRui Ueyama 
2976a1aa8d9SRafael Espindola       Ret.push_back(cast<InputSection>(Sec));
29872e107f3SRui Ueyama       Sec->Assigned = true;
299f94efdddSRui Ueyama     }
300d3190795SRafael Espindola 
301ee924709SRui Ueyama     // Sort sections as instructed by SORT-family commands and --sort-section
302ee924709SRui Ueyama     // option. Because SORT-family commands can be nested at most two depth
303ee924709SRui Ueyama     // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
304ee924709SRui Ueyama     // line option is respected even if a SORT command is given, the exact
305ee924709SRui Ueyama     // behavior we have here is a bit complicated. Here are the rules.
306ee924709SRui Ueyama     //
307ee924709SRui Ueyama     // 1. If two SORT commands are given, --sort-section is ignored.
308ee924709SRui Ueyama     // 2. If one SORT command is given, and if it is not SORT_NONE,
309ee924709SRui Ueyama     //    --sort-section is handled as an inner SORT command.
310ee924709SRui Ueyama     // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
311ee924709SRui Ueyama     // 4. If no SORT command is given, sort according to --sort-section.
312d6bcde38SGeorge Rimar     // 5. If no SORT commands are given and --sort-section is not specified,
313d6bcde38SGeorge Rimar     //    apply sorting provided by --symbol-ordering-file if any exist.
3146a1aa8d9SRafael Espindola     InputSection **Begin = Ret.data() + SizeBefore;
3156a1aa8d9SRafael Espindola     InputSection **End = Ret.data() + Ret.size();
316d6bcde38SGeorge Rimar     if (Pat.SortOuter == SortSectionPolicy::Default &&
317d6bcde38SGeorge Rimar         Config->SortSection == SortSectionPolicy::Default) {
318d6bcde38SGeorge Rimar       sortBySymbolOrder(Begin, End);
319d6bcde38SGeorge Rimar       continue;
320d6bcde38SGeorge Rimar     }
32107171f21SGeorge Rimar     if (Pat.SortOuter != SortSectionPolicy::None) {
32207171f21SGeorge Rimar       if (Pat.SortInner == SortSectionPolicy::Default)
32307171f21SGeorge Rimar         sortSections(Begin, End, Config->SortSection);
324ee924709SRui Ueyama       else
32507171f21SGeorge Rimar         sortSections(Begin, End, Pat.SortInner);
32607171f21SGeorge Rimar       sortSections(Begin, End, Pat.SortOuter);
32707171f21SGeorge Rimar     }
328ee924709SRui Ueyama   }
32972e107f3SRui Ueyama   return Ret;
330be94e1b6SRafael Espindola }
331be94e1b6SRafael Espindola 
332b8dd23f5SRui Ueyama void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
333b4c9b81aSRafael Espindola   for (InputSectionBase *S : V) {
334be94e1b6SRafael Espindola     S->Live = false;
3351e30f07cSDmitry Mikulin     if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab ||
3361e30f07cSDmitry Mikulin         S == InX::DynStrTab)
3372af64b0bSRafael Espindola       error("discarding " + S->Name + " section is not allowed");
338647c1685SGeorge Rimar     discard(S->DependentSections);
339be94e1b6SRafael Espindola   }
340be94e1b6SRafael Espindola }
341be94e1b6SRafael Espindola 
342b4c9b81aSRafael Espindola std::vector<InputSectionBase *>
3438c022ca7SRafael Espindola LinkerScript::createInputSectionList(OutputSection &OutCmd) {
344b4c9b81aSRafael Espindola   std::vector<InputSectionBase *> Ret;
345e7f912cdSRui Ueyama 
3468f99f73cSRui Ueyama   for (BaseCommand *Base : OutCmd.Commands) {
3478f99f73cSRui Ueyama     auto *Cmd = dyn_cast<InputSectionDescription>(Base);
3487c3ff2ebSRafael Espindola     if (!Cmd)
3490b9ce6a4SRui Ueyama       continue;
35072e107f3SRui Ueyama 
35172e107f3SRui Ueyama     Cmd->Sections = computeInputSections(Cmd);
352e4c8b9b7SRafael Espindola     Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
3530b9ce6a4SRui Ueyama   }
354e71a3f8aSRafael Espindola 
3550b9ce6a4SRui Ueyama   return Ret;
3560b9ce6a4SRui Ueyama }
3570b9ce6a4SRui Ueyama 
358b8dd23f5SRui Ueyama void LinkerScript::processCommands(OutputSectionFactory &Factory) {
3595616adf6SRafael Espindola   // A symbol can be assigned before any section is mentioned in the linker
3605616adf6SRafael Espindola   // script. In an DSO, the symbol values are addresses, so the only important
3615616adf6SRafael Espindola   // section values are:
3625616adf6SRafael Espindola   // * SHN_UNDEF
3635616adf6SRafael Espindola   // * SHN_ABS
3645616adf6SRafael Espindola   // * Any value meaning a regular section.
3655616adf6SRafael Espindola   // To handle that, create a dummy aether section that fills the void before
3665616adf6SRafael Espindola   // the linker scripts switches to another section. It has an index of one
3675616adf6SRafael Espindola   // which will map to whatever the first actual section is.
3685616adf6SRafael Espindola   Aether = make<OutputSection>("", 0, SHF_ALLOC);
3695616adf6SRafael Espindola   Aether->SectionIndex = 1;
370906e9a18SPeter Smith   auto State = make_unique<AddressState>(Opt);
371c1ace40bSPeter Smith   // CurAddressState captures the local AddressState and makes it accessible
372c1ace40bSPeter Smith   // deliberately. This is needed as there are some cases where we cannot just
373c1ace40bSPeter Smith   // thread the current state through to a lambda function created by the
374c1ace40bSPeter Smith   // script parser.
375906e9a18SPeter Smith   CurAddressState = State.get();
376906e9a18SPeter Smith   CurAddressState->OutSec = Aether;
37749592cf6SRafael Espindola   Dot = 0;
3785616adf6SRafael Espindola 
37992a5ba6dSRui Ueyama   for (size_t I = 0; I < Opt.Commands.size(); ++I) {
3800b1b695aSRui Ueyama     // Handle symbol assignments outside of any output section.
38192a5ba6dSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) {
3824cd7352cSRafael Espindola       addSymbol(Cmd);
3832ab5f73dSRui Ueyama       continue;
3842ab5f73dSRui Ueyama     }
3850b1b695aSRui Ueyama 
3868c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Opt.Commands[I])) {
3878c022ca7SRafael Espindola       std::vector<InputSectionBase *> V = createInputSectionList(*Sec);
3887bd37870SRafael Espindola 
3890b1b695aSRui Ueyama       // The output section name `/DISCARD/' is special.
3900b1b695aSRui Ueyama       // Any input section assigned to it is discarded.
3918c022ca7SRafael Espindola       if (Sec->Name == "/DISCARD/") {
3927bd37870SRafael Espindola         discard(V);
39348c3f1ceSRui Ueyama         continue;
39448c3f1ceSRui Ueyama       }
3950b9ce6a4SRui Ueyama 
3960b1b695aSRui Ueyama       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
3970b1b695aSRui Ueyama       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
3980b1b695aSRui Ueyama       // sections satisfy a given constraint. If not, a directive is handled
39907d7c42cSGeorge Rimar       // as if it wasn't present from the beginning.
4000b1b695aSRui Ueyama       //
4010b1b695aSRui Ueyama       // Because we'll iterate over Commands many more times, the easiest
40207d7c42cSGeorge Rimar       // way to "make it as if it wasn't present" is to just remove it.
4038c022ca7SRafael Espindola       if (!matchConstraints(V, Sec->Constraint)) {
404b4c9b81aSRafael Espindola         for (InputSectionBase *S : V)
405f94efdddSRui Ueyama           S->Assigned = false;
40692a5ba6dSRui Ueyama         Opt.Commands.erase(Opt.Commands.begin() + I);
40707d7c42cSGeorge Rimar         --I;
4087c3ff2ebSRafael Espindola         continue;
4097c3ff2ebSRafael Espindola       }
4107c3ff2ebSRafael Espindola 
4110b1b695aSRui Ueyama       // A directive may contain symbol definitions like this:
4120b1b695aSRui Ueyama       // ".foo : { ...; bar = .; }". Handle them.
4138c022ca7SRafael Espindola       for (BaseCommand *Base : Sec->Commands)
4148f99f73cSRui Ueyama         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
4154cd7352cSRafael Espindola           addSymbol(OutCmd);
4167c3ff2ebSRafael Espindola 
4170b1b695aSRui Ueyama       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
4180b1b695aSRui Ueyama       // is given, input sections are aligned to that value, whether the
4190b1b695aSRui Ueyama       // given value is larger or smaller than the original section alignment.
4208c022ca7SRafael Espindola       if (Sec->SubalignExpr) {
4218c022ca7SRafael Espindola         uint32_t Subalign = Sec->SubalignExpr().getValue();
422b4c9b81aSRafael Espindola         for (InputSectionBase *S : V)
4230b1b695aSRui Ueyama           S->Alignment = Subalign;
42420d03194SEugene Leviant       }
4250b1b695aSRui Ueyama 
4260b1b695aSRui Ueyama       // Add input sections to an output section.
427d86a4e50SGeorge Rimar       for (InputSectionBase *S : V)
4288c022ca7SRafael Espindola         Factory.addInputSec(S, Sec->Name, Sec);
429660c9ab9SRafael Espindola       assert(Sec->SectionIndex == INT_MAX);
430660c9ab9SRafael Espindola       Sec->SectionIndex = I;
4318c022ca7SRafael Espindola       if (Sec->Noload)
432fbb0463fSGeorge Rimar         Sec->Type = SHT_NOBITS;
43348c3f1ceSRui Ueyama     }
4344aa2ef5bSRafael Espindola   }
435c1ace40bSPeter Smith   CurAddressState = nullptr;
436db24d9c3SGeorge Rimar }
437e63d81bdSEugene Leviant 
43802ed7575SRafael Espindola void LinkerScript::fabricateDefaultCommands() {
439cbfe9e94SPeter Smith   // Define start address
440e76231b6SRafael Espindola   uint64_t StartAddr = -1;
441cbfe9e94SPeter Smith 
442c60b4510SPeter Smith   // The Sections with -T<section> have been sorted in order of ascending
443c60b4510SPeter Smith   // address. We must lower StartAddr if the lowest -T<section address> as
444c60b4510SPeter Smith   // calls to setDot() must be monotonically increasing.
445c60b4510SPeter Smith   for (auto &KV : Config->SectionStartMap)
446c60b4510SPeter Smith     StartAddr = std::min(StartAddr, KV.second);
447c60b4510SPeter Smith 
4488c022ca7SRafael Espindola   Opt.Commands.insert(Opt.Commands.begin(),
4498c022ca7SRafael Espindola                       make<SymbolAssignment>(".",
450e76231b6SRafael Espindola                                              [=] {
4518c022ca7SRafael Espindola                                                return std::min(
4528c022ca7SRafael Espindola                                                    StartAddr,
4538c022ca7SRafael Espindola                                                    Config->ImageBase +
4548c022ca7SRafael Espindola                                                        elf::getHeaderSize());
455e76231b6SRafael Espindola                                              },
456e76231b6SRafael Espindola                                              ""));
457cbfe9e94SPeter Smith }
458cbfe9e94SPeter Smith 
4590b1b695aSRui Ueyama // Add sections that didn't match any sections command.
460b8dd23f5SRui Ueyama void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
461d7faa916SRafael Espindola   unsigned NumCommands = Opt.Commands.size();
4624f013bb3SRafael Espindola   for (InputSectionBase *S : InputSections) {
463db5e56f7SRafael Espindola     if (!S->Live || S->Parent)
4644f013bb3SRafael Espindola       continue;
4654f013bb3SRafael Espindola     StringRef Name = getOutputSectionName(S->Name);
466d7faa916SRafael Espindola     auto End = Opt.Commands.begin() + NumCommands;
467d7faa916SRafael Espindola     auto I = std::find_if(Opt.Commands.begin(), End, [&](BaseCommand *Base) {
4688c022ca7SRafael Espindola       if (auto *Sec = dyn_cast<OutputSection>(Base))
4698c022ca7SRafael Espindola         return Sec->Name == Name;
4704f013bb3SRafael Espindola       return false;
4714f013bb3SRafael Espindola     });
472d7faa916SRafael Espindola     if (I == End) {
4734f013bb3SRafael Espindola       Factory.addInputSec(S, Name);
474a2df2f09SRafael Espindola       assert(S->getOutputSection()->SectionIndex == INT_MAX);
475de8d9897SRafael Espindola     } else {
4768c022ca7SRafael Espindola       OutputSection *Sec = cast<OutputSection>(*I);
4778c022ca7SRafael Espindola       Factory.addInputSec(S, Name, Sec);
478660c9ab9SRafael Espindola       unsigned Index = std::distance(Opt.Commands.begin(), I);
479660c9ab9SRafael Espindola       assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index);
480660c9ab9SRafael Espindola       Sec->SectionIndex = Index;
481660c9ab9SRafael Espindola     }
482d7faa916SRafael Espindola   }
4834f013bb3SRafael Espindola }
484e63d81bdSEugene Leviant 
4857c4eafa3SRafael Espindola uint64_t LinkerScript::advance(uint64_t Size, unsigned Align) {
486906e9a18SPeter Smith   bool IsTbss = (CurAddressState->OutSec->Flags & SHF_TLS) &&
487906e9a18SPeter Smith                 CurAddressState->OutSec->Type == SHT_NOBITS;
488906e9a18SPeter Smith   uint64_t Start = IsTbss ? Dot + CurAddressState->ThreadBssOffset : Dot;
4897c4eafa3SRafael Espindola   Start = alignTo(Start, Align);
4907c4eafa3SRafael Espindola   uint64_t End = Start + Size;
4917c4eafa3SRafael Espindola 
4927c4eafa3SRafael Espindola   if (IsTbss)
493906e9a18SPeter Smith     CurAddressState->ThreadBssOffset = End - Dot;
4947c4eafa3SRafael Espindola   else
4957c4eafa3SRafael Espindola     Dot = End;
4967c4eafa3SRafael Espindola   return End;
497a940e539SRafael Espindola }
498a940e539SRafael Espindola 
499b8dd23f5SRui Ueyama void LinkerScript::output(InputSection *S) {
500f694d33fSGeorge Rimar   uint64_t Before = advance(0, 1);
5017c4eafa3SRafael Espindola   uint64_t Pos = advance(S->getSize(), S->Alignment);
502906e9a18SPeter Smith   S->OutSecOff = Pos - S->getSize() - CurAddressState->OutSec->Addr;
503d3190795SRafael Espindola 
504d3190795SRafael Espindola   // Update output section size after adding each section. This is so that
505d3190795SRafael Espindola   // SIZEOF works correctly in the case below:
506d3190795SRafael Espindola   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
507906e9a18SPeter Smith   CurAddressState->OutSec->Size = Pos - CurAddressState->OutSec->Addr;
508d3190795SRafael Espindola 
509b889744eSMeador Inge   // If there is a memory region associated with this input section, then
510b889744eSMeador Inge   // place the section in that region and update the region index.
511906e9a18SPeter Smith   if (CurAddressState->MemRegion) {
512906e9a18SPeter Smith     uint64_t &CurOffset =
513906e9a18SPeter Smith         CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
514f694d33fSGeorge Rimar     CurOffset += Pos - Before;
515906e9a18SPeter Smith     uint64_t CurSize = CurOffset - CurAddressState->MemRegion->Origin;
516906e9a18SPeter Smith     if (CurSize > CurAddressState->MemRegion->Length) {
517906e9a18SPeter Smith       uint64_t OverflowAmt = CurSize - CurAddressState->MemRegion->Length;
518906e9a18SPeter Smith       error("section '" + CurAddressState->OutSec->Name +
519906e9a18SPeter Smith             "' will not fit in region '" + CurAddressState->MemRegion->Name +
520906e9a18SPeter Smith             "': overflowed by " + Twine(OverflowAmt) + " bytes");
521b889744eSMeador Inge     }
522b889744eSMeador Inge   }
5232de509c3SRui Ueyama }
524ceabe80eSEugene Leviant 
525b8dd23f5SRui Ueyama void LinkerScript::switchTo(OutputSection *Sec) {
526906e9a18SPeter Smith   if (CurAddressState->OutSec == Sec)
527d3190795SRafael Espindola     return;
528d3190795SRafael Espindola 
529906e9a18SPeter Smith   CurAddressState->OutSec = Sec;
530906e9a18SPeter Smith   CurAddressState->OutSec->Addr =
531906e9a18SPeter Smith       advance(0, CurAddressState->OutSec->Alignment);
532b71d6f7aSEugene Leviant 
533b71d6f7aSEugene Leviant   // If neither AT nor AT> is specified for an allocatable section, the linker
534b71d6f7aSEugene Leviant   // will set the LMA such that the difference between VMA and LMA for the
535b71d6f7aSEugene Leviant   // section is the same as the preceding output section in the same region
536b71d6f7aSEugene Leviant   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
537906e9a18SPeter Smith   if (CurAddressState->LMAOffset)
538906e9a18SPeter Smith     CurAddressState->OutSec->LMAOffset = CurAddressState->LMAOffset();
539d3190795SRafael Espindola }
540d3190795SRafael Espindola 
541b8dd23f5SRui Ueyama void LinkerScript::process(BaseCommand &Base) {
5422e081a4fSRui Ueyama   // This handles the assignments to symbol or to the dot.
5432e081a4fSRui Ueyama   if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) {
5442e081a4fSRui Ueyama     assignSymbol(Cmd, true);
545d3190795SRafael Espindola     return;
54697403d15SEugene Leviant   }
547e38cbab5SGeorge Rimar 
548e38cbab5SGeorge Rimar   // Handle BYTE(), SHORT(), LONG(), or QUAD().
5492e081a4fSRui Ueyama   if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) {
550906e9a18SPeter Smith     Cmd->Offset = Dot - CurAddressState->OutSec->Addr;
5512e081a4fSRui Ueyama     Dot += Cmd->Size;
552906e9a18SPeter Smith     CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr;
553e38cbab5SGeorge Rimar     return;
554e38cbab5SGeorge Rimar   }
555e38cbab5SGeorge Rimar 
5562e081a4fSRui Ueyama   // Handle ASSERT().
5572e081a4fSRui Ueyama   if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) {
5582e081a4fSRui Ueyama     Cmd->Expression();
559b2d99d6aSMeador Inge     return;
560b2d99d6aSMeador Inge   }
561b2d99d6aSMeador Inge 
5622e081a4fSRui Ueyama   // Handle a single input section description command.
5632e081a4fSRui Ueyama   // It calculates and assigns the offsets for each section and also
564e38cbab5SGeorge Rimar   // updates the output section size.
5652e081a4fSRui Ueyama   auto &Cmd = cast<InputSectionDescription>(Base);
566a85e8ddaSRafael Espindola   for (InputSection *Sec : Cmd.Sections) {
5673fb5a6dcSGeorge Rimar     // We tentatively added all synthetic sections at the beginning and removed
5683fb5a6dcSGeorge Rimar     // empty ones afterwards (because there is no way to know whether they were
5693fb5a6dcSGeorge Rimar     // going be empty or not other than actually running linker scripts.)
5703fb5a6dcSGeorge Rimar     // We need to ignore remains of empty sections.
5712e081a4fSRui Ueyama     if (auto *S = dyn_cast<SyntheticSection>(Sec))
5722e081a4fSRui Ueyama       if (S->empty())
5733fb5a6dcSGeorge Rimar         continue;
5743fb5a6dcSGeorge Rimar 
5752e081a4fSRui Ueyama     if (!Sec->Live)
57678ef645fSGeorge Rimar       continue;
577906e9a18SPeter Smith     assert(CurAddressState->OutSec == Sec->getParent());
578a85e8ddaSRafael Espindola     output(Sec);
579ceabe80eSEugene Leviant   }
580ceabe80eSEugene Leviant }
581ceabe80eSEugene Leviant 
582b889744eSMeador Inge // This function searches for a memory region to place the given output
583b889744eSMeador Inge // section in. If found, a pointer to the appropriate memory region is
584b889744eSMeador Inge // returned. Otherwise, a nullptr is returned.
5858c022ca7SRafael Espindola MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) {
586b889744eSMeador Inge   // If a memory region name was specified in the output section command,
587b889744eSMeador Inge   // then try to find that region first.
5888c022ca7SRafael Espindola   if (!Sec->MemoryRegionName.empty()) {
5898c022ca7SRafael Espindola     auto It = Opt.MemoryRegions.find(Sec->MemoryRegionName);
590b889744eSMeador Inge     if (It != Opt.MemoryRegions.end())
5915f37541cSGeorge Rimar       return It->second;
5928c022ca7SRafael Espindola     error("memory region '" + Sec->MemoryRegionName + "' not declared");
593b889744eSMeador Inge     return nullptr;
594b889744eSMeador Inge   }
595b889744eSMeador Inge 
596d7c5400fSRui Ueyama   // If at least one memory region is defined, all sections must
597d7c5400fSRui Ueyama   // belong to some memory region. Otherwise, we don't need to do
598d7c5400fSRui Ueyama   // anything for memory regions.
599cc400cc8SRui Ueyama   if (Opt.MemoryRegions.empty())
600b889744eSMeador Inge     return nullptr;
601b889744eSMeador Inge 
602b889744eSMeador Inge   // See if a region can be found by matching section flags.
6032e081a4fSRui Ueyama   for (auto &Pair : Opt.MemoryRegions) {
6045f37541cSGeorge Rimar     MemoryRegion *M = Pair.second;
6055f37541cSGeorge Rimar     if ((M->Flags & Sec->Flags) && (M->NegFlags & Sec->Flags) == 0)
6065f37541cSGeorge Rimar       return M;
607b889744eSMeador Inge   }
608b889744eSMeador Inge 
609b889744eSMeador Inge   // Otherwise, no suitable region was found.
610b889744eSMeador Inge   if (Sec->Flags & SHF_ALLOC)
611b889744eSMeador Inge     error("no memory region specified for section '" + Sec->Name + "'");
612b889744eSMeador Inge   return nullptr;
613b889744eSMeador Inge }
614b889744eSMeador Inge 
6150b1b695aSRui Ueyama // This function assigns offsets to input sections and an output section
6160b1b695aSRui Ueyama // for a single sections command (e.g. ".text { *(.text); }").
6178c022ca7SRafael Espindola void LinkerScript::assignOffsets(OutputSection *Sec) {
618dece2808SRafael Espindola   if (!(Sec->Flags & SHF_ALLOC))
619dece2808SRafael Espindola     Dot = 0;
6208c022ca7SRafael Espindola   else if (Sec->AddrExpr)
6218c022ca7SRafael Espindola     setDot(Sec->AddrExpr, Sec->Location, false);
622679828ffSRafael Espindola 
623c2dffe3aSGeorge Rimar   CurAddressState->MemRegion = Sec->MemRegion;
624c2dffe3aSGeorge Rimar   if (CurAddressState->MemRegion)
625c2dffe3aSGeorge Rimar     Dot = CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
626c2dffe3aSGeorge Rimar 
6278c022ca7SRafael Espindola   if (Sec->LMAExpr) {
6280c1c8085SGeorge Rimar     uint64_t D = Dot;
6298c022ca7SRafael Espindola     CurAddressState->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; };
6305784e96fSEugene Leviant   }
6315784e96fSEugene Leviant 
632b889744eSMeador Inge   switchTo(Sec);
6330b1b695aSRui Ueyama 
634d86a4e50SGeorge Rimar   // We do not support custom layout for compressed debug sectons.
635d86a4e50SGeorge Rimar   // At this point we already know their size and have compressed content.
636906e9a18SPeter Smith   if (CurAddressState->OutSec->Flags & SHF_COMPRESSED)
637d86a4e50SGeorge Rimar     return;
638d86a4e50SGeorge Rimar 
6398c022ca7SRafael Espindola   for (BaseCommand *C : Sec->Commands)
640de8d9897SRafael Espindola     process(*C);
641d3190795SRafael Espindola }
642d3190795SRafael Espindola 
643b8dd23f5SRui Ueyama void LinkerScript::removeEmptyCommands() {
6446d38e4dbSRafael Espindola   // It is common practice to use very generic linker scripts. So for any
6456d38e4dbSRafael Espindola   // given run some of the output sections in the script will be empty.
6466d38e4dbSRafael Espindola   // We could create corresponding empty output sections, but that would
6476d38e4dbSRafael Espindola   // clutter the output.
6486d38e4dbSRafael Espindola   // We instead remove trivially empty sections. The bfd linker seems even
6496d38e4dbSRafael Espindola   // more aggressive at removing them.
65060608a8aSGeorge Rimar   llvm::erase_if(Opt.Commands, [&](BaseCommand *Base) {
6518c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Base))
6528c022ca7SRafael Espindola       return !Sec->Live;
6530b1b695aSRui Ueyama     return false;
6546d38e4dbSRafael Espindola   });
65507fe6129SRafael Espindola }
65607fe6129SRafael Espindola 
6578c022ca7SRafael Espindola static bool isAllSectionDescription(const OutputSection &Cmd) {
6588f99f73cSRui Ueyama   for (BaseCommand *Base : Cmd.Commands)
6598f99f73cSRui Ueyama     if (!isa<InputSectionDescription>(*Base))
6606a53737cSRafael Espindola       return false;
6616a53737cSRafael Espindola   return true;
6626a53737cSRafael Espindola }
6636d38e4dbSRafael Espindola 
664b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsBeforeSorting() {
6659546fffbSRafael Espindola   // If the output section contains only symbol assignments, create a
6669546fffbSRafael Espindola   // corresponding output section. The bfd linker seems to only create them if
6679546fffbSRafael Espindola   // '.' is assigned to, but creating these section should not have any bad
6689546fffbSRafael Espindola   // consequeces and gives us a section to put the symbol in.
6690c1c8085SGeorge Rimar   uint64_t Flags = SHF_ALLOC;
670660c9ab9SRafael Espindola 
671660c9ab9SRafael Espindola   for (int I = 0, E = Opt.Commands.size(); I != E; ++I) {
6728c022ca7SRafael Espindola     auto *Sec = dyn_cast<OutputSection>(Opt.Commands[I]);
6738c022ca7SRafael Espindola     if (!Sec)
6749546fffbSRafael Espindola       continue;
6758c022ca7SRafael Espindola     if (Sec->Live) {
6762b074553SRafael Espindola       Flags = Sec->Flags;
6779546fffbSRafael Espindola       continue;
6789546fffbSRafael Espindola     }
6799546fffbSRafael Espindola 
6808c022ca7SRafael Espindola     if (isAllSectionDescription(*Sec))
6816a53737cSRafael Espindola       continue;
6826a53737cSRafael Espindola 
6838c022ca7SRafael Espindola     Sec->Live = true;
6848c022ca7SRafael Espindola     Sec->SectionIndex = I;
6858c022ca7SRafael Espindola     Sec->Flags = Flags;
6869546fffbSRafael Espindola   }
687f7a17448SRafael Espindola }
688f7a17448SRafael Espindola 
689b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsAfterSorting() {
690feed7506SRafael Espindola   // Try and find an appropriate memory region to assign offsets in.
691d1960dc0SRafael Espindola   for (BaseCommand *Base : Opt.Commands) {
6928c022ca7SRafael Espindola     if (auto *Sec = dyn_cast<OutputSection>(Base)) {
6938c022ca7SRafael Espindola       Sec->MemRegion = findMemoryRegion(Sec);
694d1960dc0SRafael Espindola       // Handle align (e.g. ".foo : ALIGN(16) { ... }").
6958c022ca7SRafael Espindola       if (Sec->AlignExpr)
6968c022ca7SRafael Espindola         Sec->updateAlignment(Sec->AlignExpr().getValue());
697d1960dc0SRafael Espindola     }
698d1960dc0SRafael Espindola   }
699feed7506SRafael Espindola 
700f7a17448SRafael Espindola   // If output section command doesn't specify any segments,
701f7a17448SRafael Espindola   // and we haven't previously assigned any section to segment,
702f7a17448SRafael Espindola   // then we simply assign section to the very first load segment.
703f7a17448SRafael Espindola   // Below is an example of such linker script:
704f7a17448SRafael Espindola   // PHDRS { seg PT_LOAD; }
705f7a17448SRafael Espindola   // SECTIONS { .aaa : { *(.aaa) } }
706f7a17448SRafael Espindola   std::vector<StringRef> DefPhdrs;
707f7a17448SRafael Espindola   auto FirstPtLoad =
708f7a17448SRafael Espindola       std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
709f7a17448SRafael Espindola                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
710f7a17448SRafael Espindola   if (FirstPtLoad != Opt.PhdrsCommands.end())
711f7a17448SRafael Espindola     DefPhdrs.push_back(FirstPtLoad->Name);
712f7a17448SRafael Espindola 
713f7a17448SRafael Espindola   // Walk the commands and propagate the program headers to commands that don't
714f7a17448SRafael Espindola   // explicitly specify them.
7158f99f73cSRui Ueyama   for (BaseCommand *Base : Opt.Commands) {
7168c022ca7SRafael Espindola     auto *Sec = dyn_cast<OutputSection>(Base);
7178c022ca7SRafael Espindola     if (!Sec)
718f7a17448SRafael Espindola       continue;
7198f99f73cSRui Ueyama 
7208c022ca7SRafael Espindola     if (Sec->Phdrs.empty()) {
721a020d348SAndrew Ng       // To match the bfd linker script behaviour, only propagate program
722a020d348SAndrew Ng       // headers to sections that are allocated.
7238c022ca7SRafael Espindola       if (Sec->Flags & SHF_ALLOC)
7248c022ca7SRafael Espindola         Sec->Phdrs = DefPhdrs;
725a020d348SAndrew Ng     } else {
7268c022ca7SRafael Espindola       DefPhdrs = Sec->Phdrs;
727f7a17448SRafael Espindola     }
728a020d348SAndrew Ng   }
7296a53737cSRafael Espindola 
7306a53737cSRafael Espindola   removeEmptyCommands();
7319546fffbSRafael Espindola }
7329546fffbSRafael Espindola 
733582ede89SGeorge Rimar static OutputSection *findFirstSection(PhdrEntry *Load) {
734582ede89SGeorge Rimar   for (OutputSection *Sec : OutputSections)
735582ede89SGeorge Rimar     if (Sec->PtLoad == Load)
736582ede89SGeorge Rimar       return Sec;
737582ede89SGeorge Rimar   return nullptr;
738582ede89SGeorge Rimar }
739582ede89SGeorge Rimar 
740b93c5b9fSPetr Hosek // Try to find an address for the file and program headers output sections,
741b93c5b9fSPetr Hosek // which were unconditionally added to the first PT_LOAD segment earlier.
742b93c5b9fSPetr Hosek //
743b93c5b9fSPetr Hosek // When using the default layout, we check if the headers fit below the first
744b93c5b9fSPetr Hosek // allocated section. When using a linker script, we also check if the headers
745b93c5b9fSPetr Hosek // are covered by the output section. This allows omitting the headers by not
746b93c5b9fSPetr Hosek // leaving enough space for them in the linker script; this pattern is common
747b93c5b9fSPetr Hosek // in embedded systems.
748b93c5b9fSPetr Hosek //
749b93c5b9fSPetr Hosek // If there isn't enough space for these sections, we'll remove them from the
750b93c5b9fSPetr Hosek // PT_LOAD segment, and we'll also remove the PT_PHDR segment.
751aa354187SGeorge Rimar void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) {
7525aedebffSPeter Smith   uint64_t Min = std::numeric_limits<uint64_t>::max();
7538c022ca7SRafael Espindola   for (OutputSection *Sec : OutputSections)
7545aedebffSPeter Smith     if (Sec->Flags & SHF_ALLOC)
7555aedebffSPeter Smith       Min = std::min<uint64_t>(Min, Sec->Addr);
7565aedebffSPeter Smith 
757aa354187SGeorge Rimar   auto It = llvm::find_if(
758aa354187SGeorge Rimar       Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; });
759aa354187SGeorge Rimar   if (It == Phdrs.end())
760d971e703SGeorge Rimar     return;
761aa354187SGeorge Rimar   PhdrEntry *FirstPTLoad = *It;
76202ed7575SRafael Espindola 
76302ed7575SRafael Espindola   uint64_t HeaderSize = getHeaderSize();
764b93c5b9fSPetr Hosek   // When linker script with SECTIONS is being used, don't output headers
765b93c5b9fSPetr Hosek   // unless there's a space for them.
766b93c5b9fSPetr Hosek   uint64_t Base = Opt.HasSections ? alignDown(Min, Config->MaxPageSize) : 0;
767b93c5b9fSPetr Hosek   if (HeaderSize <= Min - Base || Script->hasPhdrsCommands()) {
768b93c5b9fSPetr Hosek     Min = Opt.HasSections ? Base
769b93c5b9fSPetr Hosek                           : alignDown(Min - HeaderSize, Config->MaxPageSize);
77002ed7575SRafael Espindola     Out::ElfHeader->Addr = Min;
77102ed7575SRafael Espindola     Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
772d971e703SGeorge Rimar     return;
77302ed7575SRafael Espindola   }
77402ed7575SRafael Espindola 
775582ede89SGeorge Rimar   Out::ElfHeader->PtLoad = nullptr;
776582ede89SGeorge Rimar   Out::ProgramHeaders->PtLoad = nullptr;
7776823c5f0SGeorge Rimar   FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad);
77802ed7575SRafael Espindola 
77960608a8aSGeorge Rimar   llvm::erase_if(Phdrs,
78060608a8aSGeorge Rimar                  [](const PhdrEntry *E) { return E->p_type == PT_PHDR; });
78102ed7575SRafael Espindola }
78202ed7575SRafael Espindola 
783906e9a18SPeter Smith LinkerScript::AddressState::AddressState(const ScriptConfiguration &Opt) {
784906e9a18SPeter Smith   for (auto &MRI : Opt.MemoryRegions) {
7855f37541cSGeorge Rimar     const MemoryRegion *MR = MRI.second;
786906e9a18SPeter Smith     MemRegionOffset[MR] = MR->Origin;
787906e9a18SPeter Smith   }
788906e9a18SPeter Smith }
789906e9a18SPeter Smith 
7905aedebffSPeter Smith void LinkerScript::assignAddresses() {
7917c18c28cSRui Ueyama   // Assign addresses as instructed by linker script SECTIONS sub-commands.
792be607334SRafael Espindola   Dot = 0;
793906e9a18SPeter Smith   auto State = make_unique<AddressState>(Opt);
794c1ace40bSPeter Smith   // CurAddressState captures the local AddressState and makes it accessible
795c1ace40bSPeter Smith   // deliberately. This is needed as there are some cases where we cannot just
796c1ace40bSPeter Smith   // thread the current state through to a lambda function created by the
797c1ace40bSPeter Smith   // script parser.
798906e9a18SPeter Smith   CurAddressState = State.get();
79972dc195dSRafael Espindola   ErrorOnMissingSection = true;
80006f4743aSRafael Espindola   switchTo(Aether);
80106f4743aSRafael Espindola 
8028f99f73cSRui Ueyama   for (BaseCommand *Base : Opt.Commands) {
8038f99f73cSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
804d379f735SRui Ueyama       assignSymbol(Cmd, false);
80505ef4cffSRui Ueyama       continue;
806652852c5SGeorge Rimar     }
807652852c5SGeorge Rimar 
8088f99f73cSRui Ueyama     if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
8094595df94SRafael Espindola       Cmd->Expression();
810eefa758eSGeorge Rimar       continue;
811eefa758eSGeorge Rimar     }
812eefa758eSGeorge Rimar 
8138c022ca7SRafael Espindola     assignOffsets(cast<OutputSection>(Base));
814a14b13d8SGeorge Rimar   }
815c1ace40bSPeter Smith   CurAddressState = nullptr;
816fb8978fcSDima Stepanov }
817652852c5SGeorge Rimar 
818464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command.
819aa354187SGeorge Rimar std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
820aa354187SGeorge Rimar   std::vector<PhdrEntry *> Ret;
821bbe38602SEugene Leviant 
822464daadcSRui Ueyama   // Process PHDRS and FILEHDR keywords because they are not
823464daadcSRui Ueyama   // real output sections and cannot be added in the following loop.
824bbe38602SEugene Leviant   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
825aa354187SGeorge Rimar     PhdrEntry *Phdr =
826aa354187SGeorge Rimar         make<PhdrEntry>(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
827bbe38602SEugene Leviant 
828bbe38602SEugene Leviant     if (Cmd.HasFilehdr)
829aa354187SGeorge Rimar       Phdr->add(Out::ElfHeader);
830bbe38602SEugene Leviant     if (Cmd.HasPhdrs)
831aa354187SGeorge Rimar       Phdr->add(Out::ProgramHeaders);
83256b21c86SEugene Leviant 
83356b21c86SEugene Leviant     if (Cmd.LMAExpr) {
834aa354187SGeorge Rimar       Phdr->p_paddr = Cmd.LMAExpr().getValue();
835aa354187SGeorge Rimar       Phdr->HasLMA = true;
83656b21c86SEugene Leviant     }
837aa354187SGeorge Rimar     Ret.push_back(Phdr);
838bbe38602SEugene Leviant   }
839bbe38602SEugene Leviant 
840464daadcSRui Ueyama   // Add output sections to program headers.
8418c022ca7SRafael Espindola   for (OutputSection *Sec : OutputSections) {
842bbe38602SEugene Leviant     // Assign headers specified by linker script
8438c022ca7SRafael Espindola     for (size_t Id : getPhdrIndices(Sec)) {
844aa354187SGeorge Rimar       Ret[Id]->add(Sec);
845865bf863SEugene Leviant       if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
846aa354187SGeorge Rimar         Ret[Id]->p_flags |= Sec->getPhdrFlags();
847bbe38602SEugene Leviant     }
848bbe38602SEugene Leviant   }
849edebbdf1SRui Ueyama   return Ret;
850bbe38602SEugene Leviant }
851bbe38602SEugene Leviant 
852b8dd23f5SRui Ueyama bool LinkerScript::ignoreInterpSection() {
853f9bc3bd2SEugene Leviant   // Ignore .interp section in case we have PHDRS specification
854f9bc3bd2SEugene Leviant   // and PT_INTERP isn't listed.
855e31d9886SRui Ueyama   if (Opt.PhdrsCommands.empty())
856e31d9886SRui Ueyama     return false;
857e31d9886SRui Ueyama   for (PhdrsCommand &Cmd : Opt.PhdrsCommands)
858e31d9886SRui Ueyama     if (Cmd.Type == PT_INTERP)
859e31d9886SRui Ueyama       return false;
860e31d9886SRui Ueyama   return true;
861f9bc3bd2SEugene Leviant }
862f9bc3bd2SEugene Leviant 
863b8dd23f5SRui Ueyama ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
8647e5b0a59SGeorge Rimar   if (S == ".") {
8657e5b0a59SGeorge Rimar     if (CurAddressState)
8667e5b0a59SGeorge Rimar       return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr,
8677e5b0a59SGeorge Rimar               Loc};
8687e5b0a59SGeorge Rimar     error(Loc + ": unable to get location counter value");
8697e5b0a59SGeorge Rimar     return 0;
8707e5b0a59SGeorge Rimar   }
871244ef981SRafael Espindola   if (SymbolBody *B = Symtab->find(S)) {
87272dc195dSRafael Espindola     if (auto *D = dyn_cast<DefinedRegular>(B))
87341c7ab4aSGeorge Rimar       return {D->Section, D->Value, Loc};
87430f16b23SPetr Hosek     if (auto *C = dyn_cast<DefinedCommon>(B))
875*67df57a2SRafael Espindola       return {C->Section, 0, Loc};
87672dc195dSRafael Espindola   }
877f6aeed36SEugene Leviant   error(Loc + ": symbol not found: " + S);
878884e786dSGeorge Rimar   return 0;
879884e786dSGeorge Rimar }
880884e786dSGeorge Rimar 
881244ef981SRafael Espindola bool LinkerScript::isDefined(StringRef S) { return Symtab->find(S) != nullptr; }
882f34f45fdSGeorge Rimar 
8836e9f98c1SAndrew Ng static const size_t NoPhdr = -1;
8846e9f98c1SAndrew Ng 
8852c923c2cSRafael Espindola // Returns indices of ELF headers containing specific section. Each index is a
8862c923c2cSRafael Espindola // zero based number of ELF header listed within PHDRS {} script block.
8878c022ca7SRafael Espindola std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) {
88829c5a2a9SRui Ueyama   std::vector<size_t> Ret;
8896e9f98c1SAndrew Ng   for (StringRef PhdrName : Cmd->Phdrs) {
8906e9f98c1SAndrew Ng     size_t Index = getPhdrIndex(Cmd->Location, PhdrName);
8916e9f98c1SAndrew Ng     if (Index != NoPhdr)
8926e9f98c1SAndrew Ng       Ret.push_back(Index);
8936e9f98c1SAndrew Ng   }
89429c5a2a9SRui Ueyama   return Ret;
895bbe38602SEugene Leviant }
896bbe38602SEugene Leviant 
8976e9f98c1SAndrew Ng // Returns the index of the segment named PhdrName if found otherwise
8986e9f98c1SAndrew Ng // NoPhdr. When not found, if PhdrName is not the special case value 'NONE'
8996e9f98c1SAndrew Ng // (which can be used to explicitly specify that a section isn't assigned to a
9006e9f98c1SAndrew Ng // segment) then error.
901b8dd23f5SRui Ueyama size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
90229c5a2a9SRui Ueyama   size_t I = 0;
90329c5a2a9SRui Ueyama   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
90429c5a2a9SRui Ueyama     if (Cmd.Name == PhdrName)
90529c5a2a9SRui Ueyama       return I;
90629c5a2a9SRui Ueyama     ++I;
90729c5a2a9SRui Ueyama   }
9086e9f98c1SAndrew Ng   if (PhdrName != "NONE")
9092a942c4bSEugene Leviant     error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
9106e9f98c1SAndrew Ng   return NoPhdr;
91129c5a2a9SRui Ueyama }
912