blob: 60169b51b80e363ea12e8cd6cca78df120cfc849 [file] [log] [blame]
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001//===- LinkerScript.cpp ---------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the parser/evaluator of the linker script.
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000011//
12//===----------------------------------------------------------------------===//
13
Rui Ueyama717677a2016-02-11 21:17:59 +000014#include "LinkerScript.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000015#include "Config.h"
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000016#include "InputSection.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000017#include "Memory.h"
George Rimar652852c2016-04-16 10:10:32 +000018#include "OutputSections.h"
Rui Ueyama93c9af42016-06-29 08:01:32 +000019#include "Strings.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000020#include "SymbolTable.h"
Rui Ueyama55518e72016-10-28 20:57:25 +000021#include "Symbols.h"
George Rimar3fb5a6d2016-11-29 16:05:27 +000022#include "SyntheticSections.h"
Eugene Leviantbbe38602016-07-19 09:25:43 +000023#include "Writer.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000024#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/StringRef.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000026#include "llvm/Support/Casting.h"
George Rimar652852c2016-04-16 10:10:32 +000027#include "llvm/Support/ELF.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000028#include "llvm/Support/Endian.h"
29#include "llvm/Support/ErrorHandling.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000030#include "llvm/Support/FileSystem.h"
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +000031#include "llvm/Support/Path.h"
Eugene Zelenko22886a22016-11-05 01:00:56 +000032#include <algorithm>
33#include <cassert>
34#include <cstddef>
35#include <cstdint>
36#include <iterator>
37#include <limits>
Eugene Zelenko22886a22016-11-05 01:00:56 +000038#include <string>
Eugene Zelenko22886a22016-11-05 01:00:56 +000039#include <vector>
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000040
41using namespace llvm;
George Rimar652852c2016-04-16 10:10:32 +000042using namespace llvm::ELF;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000043using namespace llvm::object;
George Rimare38cbab2016-09-26 19:22:50 +000044using namespace llvm::support::endian;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000045using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000046using namespace lld::elf;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000047
Rui Ueyamaa34da932017-03-21 23:03:09 +000048LinkerScript *elf::Script;
49
Rafael Espindola72dc1952017-03-17 13:05:04 +000050uint64_t ExprValue::getValue() const {
George Rimar608cf672017-05-10 14:23:33 +000051 if (Sec) {
52 if (Sec->getOutputSection())
53 return Sec->getOffset(Val) + Sec->getOutputSection()->Addr;
54 error("unable to evaluate expression: input section " + Sec->Name +
55 " has no output section assigned");
56 }
Rafael Espindola72dc1952017-03-17 13:05:04 +000057 return Val;
58}
59
Rafael Espindola7ba5f472017-03-17 14:55:36 +000060uint64_t ExprValue::getSecAddr() const {
61 if (Sec)
62 return Sec->getOffset(0) + Sec->getOutputSection()->Addr;
63 return 0;
64}
65
Meador Inge8f1f3c42017-01-09 18:36:57 +000066template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) {
Petr Hosek5e51f7d2017-02-21 22:32:51 +000067 Symbol *Sym;
Rafael Espindola3dabfc62016-10-31 13:14:53 +000068 uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
Petr Hosek5e51f7d2017-02-21 22:32:51 +000069 std::tie(Sym, std::ignore) = Symtab<ELFT>::X->insert(
70 Cmd->Name, /*Type*/ 0, Visibility, /*CanOmitFromDynSym*/ false,
71 /*File*/ nullptr);
72 Sym->Binding = STB_GLOBAL;
Rafael Espindola72dc1952017-03-17 13:05:04 +000073 ExprValue Value = Cmd->Expression();
74 SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
George Rimar01aa7952017-04-14 09:23:26 +000075
76 // We want to set symbol values early if we can. This allows us to use symbols
77 // as variables in linker scripts. Doing so allows us to write expressions
78 // like this: `alignment = 16; . = ALIGN(., alignment)`
79 uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0;
Rui Ueyama80474a22017-02-28 19:29:55 +000080 replaceBody<DefinedRegular>(Sym, Cmd->Name, /*IsLocal=*/false, Visibility,
George Rimar01aa7952017-04-14 09:23:26 +000081 STT_NOTYPE, SymValue, 0, Sec, nullptr);
Meador Inge8f1f3c42017-01-09 18:36:57 +000082 return Sym->body();
Eugene Leviantceabe802016-08-11 07:56:43 +000083}
84
Rui Ueyamab8dd23f2017-03-21 23:02:51 +000085OutputSection *LinkerScript::getOutputSection(const Twine &Loc,
86 StringRef Name) {
George Rimar851dc1e2017-03-14 10:15:53 +000087 for (OutputSection *Sec : *OutputSections)
88 if (Sec->Name == Name)
89 return Sec;
90
Rui Ueyamaa08fa2e2017-04-05 00:42:45 +000091 static OutputSection Dummy("", 0, 0);
Rafael Espindola72dc1952017-03-17 13:05:04 +000092 if (ErrorOnMissingSection)
93 error(Loc + ": undefined section " + Name);
Rui Ueyamaa08fa2e2017-04-05 00:42:45 +000094 return &Dummy;
George Rimar851dc1e2017-03-14 10:15:53 +000095}
96
George Rimard83ce1b2017-03-14 10:24:47 +000097// This function is essentially the same as getOutputSection(Name)->Size,
98// but it won't print out an error message if a given section is not found.
99//
100// Linker script does not create an output section if its content is empty.
101// We want to allow SIZEOF(.foo) where .foo is a section which happened to
102// be empty. That is why this function is different from getOutputSection().
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000103uint64_t LinkerScript::getOutputSectionSize(StringRef Name) {
George Rimard83ce1b2017-03-14 10:24:47 +0000104 for (OutputSection *Sec : *OutputSections)
105 if (Sec->Name == Name)
106 return Sec->Size;
107 return 0;
108}
109
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000110void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
Rafael Espindola72dc1952017-03-17 13:05:04 +0000111 uint64_t Val = E().getValue();
Rafael Espindola679828f2017-02-17 16:26:13 +0000112 if (Val < Dot) {
113 if (InSec)
George Rimar2ee2d2d2017-02-21 14:50:38 +0000114 error(Loc + ": unable to move location counter backward for: " +
115 CurOutSec->Name);
Rafael Espindola679828f2017-02-17 16:26:13 +0000116 else
George Rimar2ee2d2d2017-02-21 14:50:38 +0000117 error(Loc + ": unable to move location counter backward");
Rafael Espindola679828f2017-02-17 16:26:13 +0000118 }
119 Dot = Val;
120 // Update to location counter means update to section size.
121 if (InSec)
122 CurOutSec->Size = Dot - CurOutSec->Addr;
123}
124
George Rimarb2b70972017-02-07 10:23:28 +0000125// Sets value of a symbol. Two kinds of symbols are processed: synthetic
126// symbols, whose value is an offset from beginning of section and regular
127// symbols whose value is absolute.
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000128void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
Rafael Espindola4cd73522017-02-17 16:01:51 +0000129 if (Cmd->Name == ".") {
George Rimar2ee2d2d2017-02-21 14:50:38 +0000130 setDot(Cmd->Expression, Cmd->Location, InSec);
Rafael Espindola4cd73522017-02-17 16:01:51 +0000131 return;
132 }
133
George Rimarb2b70972017-02-07 10:23:28 +0000134 if (!Cmd->Sym)
Meador Inge8f1f3c42017-01-09 18:36:57 +0000135 return;
136
Rafael Espindola5616adf2017-03-08 22:36:28 +0000137 auto *Sym = cast<DefinedRegular>(Cmd->Sym);
Rafael Espindola72dc1952017-03-17 13:05:04 +0000138 ExprValue V = Cmd->Expression();
139 if (V.isAbsolute()) {
140 Sym->Value = V.getValue();
141 } else {
142 Sym->Section = V.Sec;
143 if (Sym->Section->Flags & SHF_ALLOC)
144 Sym->Value = V.Val;
145 else
146 Sym->Value = V.getValue();
Meador Inge8f1f3c42017-01-09 18:36:57 +0000147 }
Eugene Leviantdb741e72016-09-07 07:08:43 +0000148}
Meador Inge8f1f3c42017-01-09 18:36:57 +0000149
George Rimara8dba482017-03-20 10:09:58 +0000150static SymbolBody *findSymbol(StringRef S) {
151 switch (Config->EKind) {
152 case ELF32LEKind:
153 return Symtab<ELF32LE>::X->find(S);
154 case ELF32BEKind:
155 return Symtab<ELF32BE>::X->find(S);
156 case ELF64LEKind:
157 return Symtab<ELF64LE>::X->find(S);
158 case ELF64BEKind:
159 return Symtab<ELF64BE>::X->find(S);
160 default:
161 llvm_unreachable("unknown Config->EKind");
162 }
163}
164
165static SymbolBody *addRegularSymbol(SymbolAssignment *Cmd) {
166 switch (Config->EKind) {
167 case ELF32LEKind:
168 return addRegular<ELF32LE>(Cmd);
169 case ELF32BEKind:
170 return addRegular<ELF32BE>(Cmd);
171 case ELF64LEKind:
172 return addRegular<ELF64LE>(Cmd);
173 case ELF64BEKind:
174 return addRegular<ELF64BE>(Cmd);
175 default:
176 llvm_unreachable("unknown Config->EKind");
177 }
178}
179
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000180void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
Rui Ueyama16024212016-08-11 23:22:52 +0000181 if (Cmd->Name == ".")
Meador Inge8f1f3c42017-01-09 18:36:57 +0000182 return;
183
184 // If a symbol was in PROVIDE(), we need to define it only when
185 // it is a referenced undefined symbol.
George Rimara8dba482017-03-20 10:09:58 +0000186 SymbolBody *B = findSymbol(Cmd->Name);
Meador Inge8f1f3c42017-01-09 18:36:57 +0000187 if (Cmd->Provide && (!B || B->isDefined()))
188 return;
189
George Rimara8dba482017-03-20 10:09:58 +0000190 Cmd->Sym = addRegularSymbol(Cmd);
Eugene Leviantceabe802016-08-11 07:56:43 +0000191}
192
George Rimar076fe152016-07-21 06:43:01 +0000193bool SymbolAssignment::classof(const BaseCommand *C) {
194 return C->Kind == AssignmentKind;
195}
196
197bool OutputSectionCommand::classof(const BaseCommand *C) {
198 return C->Kind == OutputSectionKind;
199}
200
George Rimareea31142016-07-21 14:26:59 +0000201bool InputSectionDescription::classof(const BaseCommand *C) {
202 return C->Kind == InputSectionKind;
203}
204
George Rimareefa7582016-08-04 09:29:31 +0000205bool AssertCommand::classof(const BaseCommand *C) {
206 return C->Kind == AssertKind;
207}
208
George Rimare38cbab2016-09-26 19:22:50 +0000209bool BytesDataCommand::classof(const BaseCommand *C) {
210 return C->Kind == BytesDataKind;
211}
212
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000213static StringRef basename(InputSectionBase *S) {
214 if (S->File)
215 return sys::path::filename(S->File->getName());
Rui Ueyamae0be2902016-11-21 02:10:12 +0000216 return "";
217}
218
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000219bool LinkerScript::shouldKeep(InputSectionBase *S) {
Rui Ueyamae0be2902016-11-21 02:10:12 +0000220 for (InputSectionDescription *ID : Opt.KeptSections)
221 if (ID->FilePat.match(basename(S)))
222 for (SectionPattern &P : ID->SectionPatterns)
223 if (P.SectionPat.match(S->Name))
224 return true;
George Rimareea31142016-07-21 14:26:59 +0000225 return false;
226}
227
Rui Ueyamaea93fe02017-04-05 00:43:25 +0000228// A helper function for the SORT() command.
Rafael Espindolac404d502017-02-23 02:32:18 +0000229static std::function<bool(InputSectionBase *, InputSectionBase *)>
George Rimarbe394db2016-09-16 20:21:55 +0000230getComparator(SortSectionPolicy K) {
231 switch (K) {
232 case SortSectionPolicy::Alignment:
Rui Ueyamaea93fe02017-04-05 00:43:25 +0000233 return [](InputSectionBase *A, InputSectionBase *B) {
234 // ">" is not a mistake. Sections with larger alignments are placed
235 // before sections with smaller alignments in order to reduce the
236 // amount of padding necessary. This is compatible with GNU.
237 return A->Alignment > B->Alignment;
238 };
George Rimarbe394db2016-09-16 20:21:55 +0000239 case SortSectionPolicy::Name:
Rui Ueyamaea93fe02017-04-05 00:43:25 +0000240 return [](InputSectionBase *A, InputSectionBase *B) {
241 return A->Name < B->Name;
242 };
George Rimarbe394db2016-09-16 20:21:55 +0000243 case SortSectionPolicy::Priority:
Rui Ueyamaea93fe02017-04-05 00:43:25 +0000244 return [](InputSectionBase *A, InputSectionBase *B) {
245 return getPriority(A->Name) < getPriority(B->Name);
246 };
George Rimarbe394db2016-09-16 20:21:55 +0000247 default:
248 llvm_unreachable("unknown sort policy");
249 }
Rui Ueyama742c3832016-08-04 22:27:00 +0000250}
George Rimar0702c4e2016-07-29 15:32:46 +0000251
Rui Ueyamaea93fe02017-04-05 00:43:25 +0000252// A helper function for the SORT() command.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000253static bool matchConstraints(ArrayRef<InputSectionBase *> Sections,
George Rimar06ae6832016-08-12 09:07:57 +0000254 ConstraintKind Kind) {
George Rimar8f66df92016-08-12 20:38:20 +0000255 if (Kind == ConstraintKind::NoConstraint)
256 return true;
Rui Ueyama2c7171b2017-04-05 00:43:45 +0000257
258 bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) {
259 return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE;
George Rimar06ae6832016-08-12 09:07:57 +0000260 });
Rui Ueyama2c7171b2017-04-05 00:43:45 +0000261
Rafael Espindolae746e522016-09-21 18:33:44 +0000262 return (IsRW && Kind == ConstraintKind::ReadWrite) ||
263 (!IsRW && Kind == ConstraintKind::ReadOnly);
George Rimar06ae6832016-08-12 09:07:57 +0000264}
265
Rafael Espindolac404d502017-02-23 02:32:18 +0000266static void sortSections(InputSectionBase **Begin, InputSectionBase **End,
Rui Ueyamaee924702016-09-20 19:42:41 +0000267 SortSectionPolicy K) {
268 if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
George Rimar07171f22016-09-21 15:56:44 +0000269 std::stable_sort(Begin, End, getComparator(K));
Rui Ueyamaee924702016-09-20 19:42:41 +0000270}
271
Rafael Espindolad3190792016-09-16 15:10:23 +0000272// Compute and remember which sections the InputSectionDescription matches.
Rui Ueyama72e107f2017-04-05 02:05:48 +0000273std::vector<InputSectionBase *>
274LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
275 std::vector<InputSectionBase *> Ret;
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +0000276
Rui Ueyama72e107f2017-04-05 02:05:48 +0000277 // Collects all sections that satisfy constraints of Cmd.
278 for (const SectionPattern &Pat : Cmd->SectionPatterns) {
279 size_t SizeBefore = Ret.size();
280
281 for (InputSectionBase *Sec : InputSections) {
282 if (Sec->Assigned)
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +0000283 continue;
Rui Ueyama72e107f2017-04-05 02:05:48 +0000284
Rafael Espindola908a3d32017-02-16 14:36:09 +0000285 // For -emit-relocs we have to ignore entries like
286 // .rela.dyn : { *(.rela.data) }
287 // which are common because they are in the default bfd script.
Rui Ueyama72e107f2017-04-05 02:05:48 +0000288 if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
Rafael Espindola908a3d32017-02-16 14:36:09 +0000289 continue;
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +0000290
Rui Ueyama72e107f2017-04-05 02:05:48 +0000291 StringRef Filename = basename(Sec);
292 if (!Cmd->FilePat.match(Filename) ||
293 Pat.ExcludedFilePat.match(Filename) ||
294 !Pat.SectionPat.match(Sec->Name))
Rui Ueyamae0be2902016-11-21 02:10:12 +0000295 continue;
Rui Ueyama72e107f2017-04-05 02:05:48 +0000296
297 Ret.push_back(Sec);
298 Sec->Assigned = true;
George Rimar395281c2016-09-16 17:42:10 +0000299 }
Rafael Espindolad3190792016-09-16 15:10:23 +0000300
George Rimar07171f22016-09-21 15:56:44 +0000301 // Sort sections as instructed by SORT-family commands and --sort-section
302 // option. Because SORT-family commands can be nested at most two depth
303 // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
304 // line option is respected even if a SORT command is given, the exact
305 // behavior we have here is a bit complicated. Here are the rules.
306 //
307 // 1. If two SORT commands are given, --sort-section is ignored.
308 // 2. If one SORT command is given, and if it is not SORT_NONE,
309 // --sort-section is handled as an inner SORT command.
310 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
311 // 4. If no SORT command is given, sort according to --sort-section.
Rui Ueyama72e107f2017-04-05 02:05:48 +0000312 InputSectionBase **Begin = Ret.data() + SizeBefore;
313 InputSectionBase **End = Ret.data() + Ret.size();
George Rimar07171f22016-09-21 15:56:44 +0000314 if (Pat.SortOuter != SortSectionPolicy::None) {
315 if (Pat.SortInner == SortSectionPolicy::Default)
316 sortSections(Begin, End, Config->SortSection);
317 else
318 sortSections(Begin, End, Pat.SortInner);
319 sortSections(Begin, End, Pat.SortOuter);
320 }
Rui Ueyamaee924702016-09-20 19:42:41 +0000321 }
Rui Ueyama72e107f2017-04-05 02:05:48 +0000322 return Ret;
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000323}
324
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000325void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000326 for (InputSectionBase *S : V) {
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000327 S->Live = false;
George Rimar503206c2017-03-15 15:42:44 +0000328 if (S == InX::ShStrTab)
Rafael Espindolaecbfd872017-02-17 17:35:07 +0000329 error("discarding .shstrtab section is not allowed");
George Rimar647c1682017-02-17 19:34:05 +0000330 discard(S->DependentSections);
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000331 }
332}
333
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000334std::vector<InputSectionBase *>
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000335LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000336 std::vector<InputSectionBase *> Ret;
Rui Ueyamae7f912c2016-08-03 21:12:09 +0000337
Rui Ueyama8f99f732017-04-05 03:20:42 +0000338 for (BaseCommand *Base : OutCmd.Commands) {
339 auto *Cmd = dyn_cast<InputSectionDescription>(Base);
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000340 if (!Cmd)
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000341 continue;
Rui Ueyama72e107f2017-04-05 02:05:48 +0000342
343 Cmd->Sections = computeInputSections(Cmd);
Rafael Espindolae4c8b9b2017-04-07 16:10:46 +0000344 Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000345 }
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000346
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000347 return Ret;
348}
349
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000350void LinkerScript::processCommands(OutputSectionFactory &Factory) {
Rafael Espindola5616adf2017-03-08 22:36:28 +0000351 // A symbol can be assigned before any section is mentioned in the linker
352 // script. In an DSO, the symbol values are addresses, so the only important
353 // section values are:
354 // * SHN_UNDEF
355 // * SHN_ABS
356 // * Any value meaning a regular section.
357 // To handle that, create a dummy aether section that fills the void before
358 // the linker scripts switches to another section. It has an index of one
359 // which will map to whatever the first actual section is.
360 Aether = make<OutputSection>("", 0, SHF_ALLOC);
361 Aether->SectionIndex = 1;
362 CurOutSec = Aether;
Rafael Espindola49592cf2017-03-20 14:33:33 +0000363 Dot = 0;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000364
Rui Ueyama92a5ba62017-04-05 16:07:44 +0000365 for (size_t I = 0; I < Opt.Commands.size(); ++I) {
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000366 // Handle symbol assignments outside of any output section.
Rui Ueyama92a5ba62017-04-05 16:07:44 +0000367 if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) {
Rafael Espindola4cd73522017-02-17 16:01:51 +0000368 addSymbol(Cmd);
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000369 continue;
370 }
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000371
Rui Ueyama92a5ba62017-04-05 16:07:44 +0000372 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I])) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000373 std::vector<InputSectionBase *> V = createInputSectionList(*Cmd);
Rafael Espindola7bd37872016-09-12 16:05:16 +0000374
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000375 // The output section name `/DISCARD/' is special.
376 // Any input section assigned to it is discarded.
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000377 if (Cmd->Name == "/DISCARD/") {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000378 discard(V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000379 continue;
380 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000381
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000382 // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
383 // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
384 // sections satisfy a given constraint. If not, a directive is handled
George Rimar07d7c422017-04-05 09:19:29 +0000385 // as if it wasn't present from the beginning.
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000386 //
387 // Because we'll iterate over Commands many more times, the easiest
George Rimar07d7c422017-04-05 09:19:29 +0000388 // way to "make it as if it wasn't present" is to just remove it.
George Rimarf7f0d082017-03-14 11:23:33 +0000389 if (!matchConstraints(V, Cmd->Constraint)) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000390 for (InputSectionBase *S : V)
Rui Ueyamaf94efdd2016-11-20 23:15:52 +0000391 S->Assigned = false;
Rui Ueyama92a5ba62017-04-05 16:07:44 +0000392 Opt.Commands.erase(Opt.Commands.begin() + I);
George Rimar07d7c422017-04-05 09:19:29 +0000393 --I;
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000394 continue;
395 }
396
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000397 // A directive may contain symbol definitions like this:
398 // ".foo : { ...; bar = .; }". Handle them.
Rui Ueyama8f99f732017-04-05 03:20:42 +0000399 for (BaseCommand *Base : Cmd->Commands)
400 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
Rafael Espindola4cd73522017-02-17 16:01:51 +0000401 addSymbol(OutCmd);
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000402
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000403 // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
404 // is given, input sections are aligned to that value, whether the
405 // given value is larger or smaller than the original section alignment.
406 if (Cmd->SubalignExpr) {
Rafael Espindola72dc1952017-03-17 13:05:04 +0000407 uint32_t Subalign = Cmd->SubalignExpr().getValue();
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000408 for (InputSectionBase *S : V)
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000409 S->Alignment = Subalign;
George Rimardb24d9c2016-08-19 15:18:23 +0000410 }
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000411
412 // Add input sections to an output section.
George Rimard86a4e52017-05-08 10:18:12 +0000413 for (InputSectionBase *S : V)
Rafael Espindola4f013bb2017-04-26 22:30:15 +0000414 Factory.addInputSec(S, Cmd->Name, Cmd->Sec);
Rafael Espindola660c9ab2017-05-05 21:34:26 +0000415 if (OutputSection *Sec = Cmd->Sec) {
416 assert(Sec->SectionIndex == INT_MAX);
417 Sec->SectionIndex = I;
418 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000419 }
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000420 }
Rafael Espindola5616adf2017-03-08 22:36:28 +0000421 CurOutSec = nullptr;
Eugene Leviant20d03192016-09-16 15:30:47 +0000422}
Eugene Leviante63d81b2016-07-20 14:43:20 +0000423
Rafael Espindola02ed7572017-05-04 19:34:17 +0000424void LinkerScript::fabricateDefaultCommands() {
Peter Smithcbfe9e92017-04-19 12:46:32 +0000425 std::vector<BaseCommand *> Commands;
426
427 // Define start address
Rafael Espindola02ed7572017-05-04 19:34:17 +0000428 uint64_t StartAddr = Config->ImageBase + elf::getHeaderSize();
Peter Smithcbfe9e92017-04-19 12:46:32 +0000429
Peter Smithc60b4512017-05-03 08:44:50 +0000430 // The Sections with -T<section> have been sorted in order of ascending
431 // address. We must lower StartAddr if the lowest -T<section address> as
432 // calls to setDot() must be monotonically increasing.
433 for (auto& KV : Config->SectionStartMap)
434 StartAddr = std::min(StartAddr, KV.second);
435
Peter Smithcbfe9e92017-04-19 12:46:32 +0000436 Commands.push_back(
437 make<SymbolAssignment>(".", [=] { return StartAddr; }, ""));
438
439 // For each OutputSection that needs a VA fabricate an OutputSectionCommand
440 // with an InputSectionDescription describing the InputSections
441 for (OutputSection *Sec : *OutputSections) {
442 if (!(Sec->Flags & SHF_ALLOC))
443 continue;
444
Peter Smithc60b4512017-05-03 08:44:50 +0000445 auto *OSCmd = make<OutputSectionCommand>(Sec->Name);
446 OSCmd->Sec = Sec;
447
448 // Prefer user supplied address over additional alignment constraint
Peter Smithcbfe9e92017-04-19 12:46:32 +0000449 auto I = Config->SectionStartMap.find(Sec->Name);
450 if (I != Config->SectionStartMap.end())
451 Commands.push_back(
452 make<SymbolAssignment>(".", [=] { return I->second; }, ""));
Peter Smithc60b4512017-05-03 08:44:50 +0000453 else if (Sec->PageAlign)
Peter Smithcbfe9e92017-04-19 12:46:32 +0000454 OSCmd->AddrExpr = [=] {
455 return alignTo(Script->getDot(), Config->MaxPageSize);
456 };
Peter Smithc60b4512017-05-03 08:44:50 +0000457
Peter Smithcbfe9e92017-04-19 12:46:32 +0000458 Commands.push_back(OSCmd);
459 if (Sec->Sections.size()) {
460 auto *ISD = make<InputSectionDescription>("");
461 OSCmd->Commands.push_back(ISD);
462 for (InputSection *ISec : Sec->Sections) {
463 ISD->Sections.push_back(ISec);
464 ISec->Assigned = true;
465 }
466 }
467 }
468 // SECTIONS commands run before other non SECTIONS commands
469 Commands.insert(Commands.end(), Opt.Commands.begin(), Opt.Commands.end());
470 Opt.Commands = std::move(Commands);
471}
472
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000473// Add sections that didn't match any sections command.
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000474void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
Rafael Espindola4f013bb2017-04-26 22:30:15 +0000475 for (InputSectionBase *S : InputSections) {
476 if (!S->Live || S->OutSec)
477 continue;
478 StringRef Name = getOutputSectionName(S->Name);
479 auto I = std::find_if(
480 Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) {
481 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
482 return Cmd->Name == Name;
483 return false;
484 });
Rafael Espindolade8d9892017-04-29 15:44:03 +0000485 if (I == Opt.Commands.end()) {
Rafael Espindola4f013bb2017-04-26 22:30:15 +0000486 Factory.addInputSec(S, Name);
Rafael Espindolade8d9892017-04-29 15:44:03 +0000487 } else {
488 auto *Cmd = cast<OutputSectionCommand>(*I);
489 Factory.addInputSec(S, Name, Cmd->Sec);
Rafael Espindola660c9ab2017-05-05 21:34:26 +0000490 if (OutputSection *Sec = Cmd->Sec) {
491 unsigned Index = std::distance(Opt.Commands.begin(), I);
492 assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index);
493 Sec->SectionIndex = Index;
494 }
Rafael Espindolade8d9892017-04-29 15:44:03 +0000495 auto *ISD = make<InputSectionDescription>("");
496 ISD->Sections.push_back(S);
497 Cmd->Commands.push_back(ISD);
498 }
Rafael Espindola4f013bb2017-04-26 22:30:15 +0000499 }
Eugene Leviante63d81b2016-07-20 14:43:20 +0000500}
501
Rafael Espindola7c4eafa2017-05-04 03:00:27 +0000502uint64_t LinkerScript::advance(uint64_t Size, unsigned Align) {
503 bool IsTbss = (CurOutSec->Flags & SHF_TLS) && CurOutSec->Type == SHT_NOBITS;
504 uint64_t Start = IsTbss ? Dot + ThreadBssOffset : Dot;
505 Start = alignTo(Start, Align);
506 uint64_t End = Start + Size;
507
508 if (IsTbss)
509 ThreadBssOffset = End - Dot;
510 else
511 Dot = End;
512 return End;
Rafael Espindolaa940e532016-09-22 12:35:44 +0000513}
514
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000515void LinkerScript::output(InputSection *S) {
Rafael Espindola7c4eafa2017-05-04 03:00:27 +0000516 uint64_t Pos = advance(S->getSize(), S->Alignment);
517 S->OutSecOff = Pos - S->getSize() - CurOutSec->Addr;
Rafael Espindolad3190792016-09-16 15:10:23 +0000518
519 // Update output section size after adding each section. This is so that
520 // SIZEOF works correctly in the case below:
521 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000522 CurOutSec->Size = Pos - CurOutSec->Addr;
Rafael Espindolad3190792016-09-16 15:10:23 +0000523
Meador Ingeb8897442017-01-24 02:34:00 +0000524 // If there is a memory region associated with this input section, then
525 // place the section in that region and update the region index.
526 if (CurMemRegion) {
527 CurMemRegion->Offset += CurOutSec->Size;
528 uint64_t CurSize = CurMemRegion->Offset - CurMemRegion->Origin;
529 if (CurSize > CurMemRegion->Length) {
530 uint64_t OverflowAmt = CurSize - CurMemRegion->Length;
531 error("section '" + CurOutSec->Name + "' will not fit in region '" +
532 CurMemRegion->Name + "': overflowed by " + Twine(OverflowAmt) +
533 " bytes");
534 }
535 }
Rafael Espindolad3190792016-09-16 15:10:23 +0000536}
537
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000538void LinkerScript::switchTo(OutputSection *Sec) {
Rafael Espindolad3190792016-09-16 15:10:23 +0000539 if (CurOutSec == Sec)
540 return;
Rafael Espindolad3190792016-09-16 15:10:23 +0000541
Rafael Espindolad3190792016-09-16 15:10:23 +0000542 CurOutSec = Sec;
Rafael Espindola7c4eafa2017-05-04 03:00:27 +0000543 CurOutSec->Addr = advance(0, CurOutSec->Alignment);
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000544
545 // If neither AT nor AT> is specified for an allocatable section, the linker
546 // will set the LMA such that the difference between VMA and LMA for the
547 // section is the same as the preceding output section in the same region
548 // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
George Rimar21467872017-02-23 07:57:55 +0000549 if (LMAOffset)
Rafael Espindola29c1afb2017-02-24 14:34:12 +0000550 CurOutSec->LMAOffset = LMAOffset();
Rafael Espindolad3190792016-09-16 15:10:23 +0000551}
552
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000553void LinkerScript::process(BaseCommand &Base) {
Rui Ueyama2e081a42017-04-05 03:18:46 +0000554 // This handles the assignments to symbol or to the dot.
555 if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) {
556 assignSymbol(Cmd, true);
Eugene Leviantceabe802016-08-11 07:56:43 +0000557 return;
Rui Ueyama2de509c2016-08-12 00:55:08 +0000558 }
George Rimare38cbab2016-09-26 19:22:50 +0000559
560 // Handle BYTE(), SHORT(), LONG(), or QUAD().
Rui Ueyama2e081a42017-04-05 03:18:46 +0000561 if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) {
562 Cmd->Offset = Dot - CurOutSec->Addr;
563 Dot += Cmd->Size;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000564 CurOutSec->Size = Dot - CurOutSec->Addr;
George Rimare38cbab2016-09-26 19:22:50 +0000565 return;
566 }
567
Rui Ueyama2e081a42017-04-05 03:18:46 +0000568 // Handle ASSERT().
569 if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) {
570 Cmd->Expression();
Meador Ingeb2d99d62016-11-22 18:01:50 +0000571 return;
572 }
573
Rui Ueyama2e081a42017-04-05 03:18:46 +0000574 // Handle a single input section description command.
575 // It calculates and assigns the offsets for each section and also
George Rimare38cbab2016-09-26 19:22:50 +0000576 // updates the output section size.
Rui Ueyama2e081a42017-04-05 03:18:46 +0000577 auto &Cmd = cast<InputSectionDescription>(Base);
578 for (InputSectionBase *Sec : Cmd.Sections) {
George Rimar3fb5a6d2016-11-29 16:05:27 +0000579 // We tentatively added all synthetic sections at the beginning and removed
580 // empty ones afterwards (because there is no way to know whether they were
581 // going be empty or not other than actually running linker scripts.)
582 // We need to ignore remains of empty sections.
Rui Ueyama2e081a42017-04-05 03:18:46 +0000583 if (auto *S = dyn_cast<SyntheticSection>(Sec))
584 if (S->empty())
George Rimar3fb5a6d2016-11-29 16:05:27 +0000585 continue;
586
Rui Ueyama2e081a42017-04-05 03:18:46 +0000587 if (!Sec->Live)
George Rimar78ef6452017-02-21 15:46:43 +0000588 continue;
Rafael Espindolade8d9892017-04-29 15:44:03 +0000589 assert(CurOutSec == Sec->OutSec);
Rui Ueyama2e081a42017-04-05 03:18:46 +0000590 output(cast<InputSection>(Sec));
Eugene Leviantceabe802016-08-11 07:56:43 +0000591 }
592}
593
Meador Ingeb8897442017-01-24 02:34:00 +0000594// This function searches for a memory region to place the given output
595// section in. If found, a pointer to the appropriate memory region is
596// returned. Otherwise, a nullptr is returned.
Rafael Espindola1902b332017-04-06 21:26:03 +0000597MemoryRegion *LinkerScript::findMemoryRegion(OutputSectionCommand *Cmd) {
Meador Ingeb8897442017-01-24 02:34:00 +0000598 // If a memory region name was specified in the output section command,
599 // then try to find that region first.
600 if (!Cmd->MemoryRegionName.empty()) {
601 auto It = Opt.MemoryRegions.find(Cmd->MemoryRegionName);
602 if (It != Opt.MemoryRegions.end())
603 return &It->second;
604 error("memory region '" + Cmd->MemoryRegionName + "' not declared");
605 return nullptr;
606 }
607
Rui Ueyamad7c54002017-04-05 03:19:43 +0000608 // If at least one memory region is defined, all sections must
609 // belong to some memory region. Otherwise, we don't need to do
610 // anything for memory regions.
Rui Ueyamacc400cc2017-04-05 03:19:24 +0000611 if (Opt.MemoryRegions.empty())
Meador Ingeb8897442017-01-24 02:34:00 +0000612 return nullptr;
613
Rafael Espindola1902b332017-04-06 21:26:03 +0000614 OutputSection *Sec = Cmd->Sec;
Meador Ingeb8897442017-01-24 02:34:00 +0000615 // See if a region can be found by matching section flags.
Rui Ueyama2e081a42017-04-05 03:18:46 +0000616 for (auto &Pair : Opt.MemoryRegions) {
617 MemoryRegion &M = Pair.second;
618 if ((M.Flags & Sec->Flags) && (M.NegFlags & Sec->Flags) == 0)
619 return &M;
Meador Ingeb8897442017-01-24 02:34:00 +0000620 }
621
622 // Otherwise, no suitable region was found.
623 if (Sec->Flags & SHF_ALLOC)
624 error("no memory region specified for section '" + Sec->Name + "'");
625 return nullptr;
626}
627
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000628// This function assigns offsets to input sections and an output section
629// for a single sections command (e.g. ".text { *(.text); }").
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000630void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) {
Rafael Espindola9b980092017-04-06 21:05:39 +0000631 OutputSection *Sec = Cmd->Sec;
Rafael Espindola2b074552017-02-03 22:27:05 +0000632 if (!Sec)
Rafael Espindolad3190792016-09-16 15:10:23 +0000633 return;
Meador Ingeb8897442017-01-24 02:34:00 +0000634
Rui Ueyamacba41012017-04-05 03:20:03 +0000635 if (Cmd->AddrExpr && (Sec->Flags & SHF_ALLOC))
Rui Ueyamad379f732017-04-05 03:20:22 +0000636 setDot(Cmd->AddrExpr, Cmd->Location, false);
Rafael Espindola679828f2017-02-17 16:26:13 +0000637
Eugene Leviant5784e962017-03-14 08:57:09 +0000638 if (Cmd->LMAExpr) {
George Rimar0c1c8082017-03-14 10:00:19 +0000639 uint64_t D = Dot;
Rafael Espindola72dc1952017-03-17 13:05:04 +0000640 LMAOffset = [=] { return Cmd->LMAExpr().getValue() - D; };
Eugene Leviant5784e962017-03-14 08:57:09 +0000641 }
642
Rafael Espindolafeed7502017-04-06 21:31:24 +0000643 CurMemRegion = Cmd->MemRegion;
Meador Ingeb8897442017-01-24 02:34:00 +0000644 if (CurMemRegion)
645 Dot = CurMemRegion->Offset;
646 switchTo(Sec);
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000647
George Rimard86a4e52017-05-08 10:18:12 +0000648 // We do not support custom layout for compressed debug sectons.
649 // At this point we already know their size and have compressed content.
650 if (CurOutSec->Flags & SHF_COMPRESSED)
651 return;
652
Rafael Espindolade8d9892017-04-29 15:44:03 +0000653 for (BaseCommand *C : Cmd->Commands)
654 process(*C);
Rafael Espindolad3190792016-09-16 15:10:23 +0000655}
656
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000657void LinkerScript::removeEmptyCommands() {
Rafael Espindola6d38e4d2016-09-20 13:12:07 +0000658 // It is common practice to use very generic linker scripts. So for any
659 // given run some of the output sections in the script will be empty.
660 // We could create corresponding empty output sections, but that would
661 // clutter the output.
662 // We instead remove trivially empty sections. The bfd linker seems even
663 // more aggressive at removing them.
664 auto Pos = std::remove_if(
Rui Ueyama8f99f732017-04-05 03:20:42 +0000665 Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) {
666 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
Rafael Espindola4f013bb2017-04-26 22:30:15 +0000667 return std::find(OutputSections->begin(), OutputSections->end(),
668 Cmd->Sec) == OutputSections->end();
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000669 return false;
Rafael Espindola6d38e4d2016-09-20 13:12:07 +0000670 });
671 Opt.Commands.erase(Pos, Opt.Commands.end());
Rafael Espindola07fe6122016-11-14 14:23:35 +0000672}
673
Rafael Espindola6a537372016-11-14 14:33:49 +0000674static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
Rui Ueyama8f99f732017-04-05 03:20:42 +0000675 for (BaseCommand *Base : Cmd.Commands)
676 if (!isa<InputSectionDescription>(*Base))
Rafael Espindola6a537372016-11-14 14:33:49 +0000677 return false;
678 return true;
679}
Rafael Espindola6d38e4d2016-09-20 13:12:07 +0000680
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000681void LinkerScript::adjustSectionsBeforeSorting() {
Rafael Espindola9546fff2016-09-22 14:40:50 +0000682 // If the output section contains only symbol assignments, create a
683 // corresponding output section. The bfd linker seems to only create them if
684 // '.' is assigned to, but creating these section should not have any bad
685 // consequeces and gives us a section to put the symbol in.
George Rimar0c1c8082017-03-14 10:00:19 +0000686 uint64_t Flags = SHF_ALLOC;
George Rimar10221122017-04-14 09:37:00 +0000687 uint32_t Type = SHT_PROGBITS;
Rafael Espindola660c9ab2017-05-05 21:34:26 +0000688
689 for (int I = 0, E = Opt.Commands.size(); I != E; ++I) {
690 auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]);
Rafael Espindola9546fff2016-09-22 14:40:50 +0000691 if (!Cmd)
692 continue;
Rafael Espindola4f013bb2017-04-26 22:30:15 +0000693 if (OutputSection *Sec = Cmd->Sec) {
Rafael Espindola2b074552017-02-03 22:27:05 +0000694 Flags = Sec->Flags;
695 Type = Sec->Type;
Rafael Espindola9546fff2016-09-22 14:40:50 +0000696 continue;
697 }
698
Rafael Espindola6a537372016-11-14 14:33:49 +0000699 if (isAllSectionDescription(*Cmd))
700 continue;
701
Rafael Espindola24e6f362017-02-24 15:07:30 +0000702 auto *OutSec = make<OutputSection>(Cmd->Name, Type, Flags);
Rafael Espindola660c9ab2017-05-05 21:34:26 +0000703 OutSec->SectionIndex = I;
Rafael Espindola9546fff2016-09-22 14:40:50 +0000704 OutputSections->push_back(OutSec);
Rafael Espindola9b980092017-04-06 21:05:39 +0000705 Cmd->Sec = OutSec;
Rafael Espindola9546fff2016-09-22 14:40:50 +0000706 }
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000707}
708
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000709void LinkerScript::adjustSectionsAfterSorting() {
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000710 placeOrphanSections();
711
Rafael Espindolafeed7502017-04-06 21:31:24 +0000712 // Try and find an appropriate memory region to assign offsets in.
Rafael Espindolad1960dc2017-04-06 21:40:22 +0000713 for (BaseCommand *Base : Opt.Commands) {
714 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) {
Rafael Espindolafeed7502017-04-06 21:31:24 +0000715 Cmd->MemRegion = findMemoryRegion(Cmd);
Rafael Espindolad1960dc2017-04-06 21:40:22 +0000716 // Handle align (e.g. ".foo : ALIGN(16) { ... }").
717 if (Cmd->AlignExpr)
718 Cmd->Sec->updateAlignment(Cmd->AlignExpr().getValue());
719 }
720 }
Rafael Espindolafeed7502017-04-06 21:31:24 +0000721
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000722 // If output section command doesn't specify any segments,
723 // and we haven't previously assigned any section to segment,
724 // then we simply assign section to the very first load segment.
725 // Below is an example of such linker script:
726 // PHDRS { seg PT_LOAD; }
727 // SECTIONS { .aaa : { *(.aaa) } }
728 std::vector<StringRef> DefPhdrs;
729 auto FirstPtLoad =
730 std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
731 [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
732 if (FirstPtLoad != Opt.PhdrsCommands.end())
733 DefPhdrs.push_back(FirstPtLoad->Name);
734
735 // Walk the commands and propagate the program headers to commands that don't
736 // explicitly specify them.
Rui Ueyama8f99f732017-04-05 03:20:42 +0000737 for (BaseCommand *Base : Opt.Commands) {
738 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000739 if (!Cmd)
740 continue;
Rui Ueyama8f99f732017-04-05 03:20:42 +0000741
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000742 if (Cmd->Phdrs.empty())
743 Cmd->Phdrs = DefPhdrs;
744 else
745 DefPhdrs = Cmd->Phdrs;
746 }
Rafael Espindola6a537372016-11-14 14:33:49 +0000747
748 removeEmptyCommands();
Rafael Espindola9546fff2016-09-22 14:40:50 +0000749}
750
Rafael Espindola15c57952016-09-22 18:05:49 +0000751// When placing orphan sections, we want to place them after symbol assignments
752// so that an orphan after
753// begin_foo = .;
754// foo : { *(foo) }
755// end_foo = .;
756// doesn't break the intended meaning of the begin/end symbols.
757// We don't want to go over sections since Writer<ELFT>::sortSections is the
758// one in charge of deciding the order of the sections.
759// We don't want to go over alignments, since doing so in
760// rx_sec : { *(rx_sec) }
761// . = ALIGN(0x1000);
762// /* The RW PT_LOAD starts here*/
763// rw_sec : { *(rw_sec) }
764// would mean that the RW PT_LOAD would become unaligned.
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000765static bool shouldSkip(BaseCommand *Cmd) {
Rafael Espindola15c57952016-09-22 18:05:49 +0000766 if (isa<OutputSectionCommand>(Cmd))
767 return false;
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000768 if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd))
769 return Assign->Name != ".";
770 return true;
Rafael Espindola15c57952016-09-22 18:05:49 +0000771}
772
Rui Ueyama6697ec22017-02-02 23:26:12 +0000773// Orphan sections are sections present in the input files which are
774// not explicitly placed into the output file by the linker script.
775//
776// When the control reaches this function, Opt.Commands contains
777// output section commands for non-orphan sections only. This function
Rui Ueyama81cb7102017-03-24 00:15:57 +0000778// adds new elements for orphan sections so that all sections are
779// explicitly handled by Opt.Commands.
Rui Ueyama6697ec22017-02-02 23:26:12 +0000780//
781// Writer<ELFT>::sortSections has already sorted output sections.
782// What we need to do is to scan OutputSections vector and
783// Opt.Commands in parallel to find orphan sections. If there is an
784// output section that doesn't have a corresponding entry in
785// Opt.Commands, we will insert a new entry to Opt.Commands.
786//
787// There is some ambiguity as to where exactly a new entry should be
788// inserted, because Opt.Commands contains not only output section
Rui Ueyama81cb7102017-03-24 00:15:57 +0000789// commands but also other types of commands such as symbol assignment
Rui Ueyama6697ec22017-02-02 23:26:12 +0000790// expressions. There's no correct answer here due to the lack of the
791// formal specification of the linker script. We use heuristics to
792// determine whether a new output command should be added before or
793// after another commands. For the details, look at shouldSkip
794// function.
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000795void LinkerScript::placeOrphanSections() {
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000796 // The OutputSections are already in the correct order.
797 // This loops creates or moves commands as needed so that they are in the
798 // correct order.
799 int CmdIndex = 0;
Rafael Espindola5fcc99c2016-11-27 09:44:45 +0000800
801 // As a horrible special case, skip the first . assignment if it is before any
802 // section. We do this because it is common to set a load address by starting
803 // the script with ". = 0xabcd" and the expectation is that every section is
804 // after that.
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000805 auto FirstSectionOrDotAssignment =
806 std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
807 [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
Rafael Espindola5fcc99c2016-11-27 09:44:45 +0000808 if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
809 CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
810 if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
811 ++CmdIndex;
812 }
813
Rafael Espindola24e6f362017-02-24 15:07:30 +0000814 for (OutputSection *Sec : *OutputSections) {
Rafael Espindola40849412017-02-24 14:28:00 +0000815 StringRef Name = Sec->Name;
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000816
817 // Find the last spot where we can insert a command and still get the
Rafael Espindola15c57952016-09-22 18:05:49 +0000818 // correct result.
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000819 auto CmdIter = Opt.Commands.begin() + CmdIndex;
820 auto E = Opt.Commands.end();
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000821 while (CmdIter != E && shouldSkip(*CmdIter)) {
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000822 ++CmdIter;
823 ++CmdIndex;
824 }
825
Rafael Espindolade8d9892017-04-29 15:44:03 +0000826 // If there is no command corresponding to this output section,
827 // create one and put a InputSectionDescription in it so that both
828 // representations agree on which input sections to use.
Rui Ueyama8f99f732017-04-05 03:20:42 +0000829 auto Pos = std::find_if(CmdIter, E, [&](BaseCommand *Base) {
830 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
831 return Cmd && Cmd->Name == Name;
832 });
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000833 if (Pos == E) {
Rafael Espindola9b980092017-04-06 21:05:39 +0000834 auto *Cmd = make<OutputSectionCommand>(Name);
Rafael Espindola9b980092017-04-06 21:05:39 +0000835 Opt.Commands.insert(CmdIter, Cmd);
Rafael Espindola15c57952016-09-22 18:05:49 +0000836 ++CmdIndex;
Rafael Espindolade8d9892017-04-29 15:44:03 +0000837
838 Cmd->Sec = Sec;
839 auto *ISD = make<InputSectionDescription>("");
840 for (InputSection *IS : Sec->Sections)
841 ISD->Sections.push_back(IS);
842 Cmd->Commands.push_back(ISD);
843
Rafael Espindola15c57952016-09-22 18:05:49 +0000844 continue;
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000845 }
Rafael Espindola15c57952016-09-22 18:05:49 +0000846
847 // Continue from where we found it.
848 CmdIndex = (Pos - Opt.Commands.begin()) + 1;
George Rimar652852c2016-04-16 10:10:32 +0000849 }
Rafael Espindola337f9032016-11-14 14:13:32 +0000850}
851
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000852void LinkerScript::processNonSectionCommands() {
Rui Ueyama8f99f732017-04-05 03:20:42 +0000853 for (BaseCommand *Base : Opt.Commands) {
854 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base))
Rui Ueyamad379f732017-04-05 03:20:22 +0000855 assignSymbol(Cmd, false);
Rui Ueyama8f99f732017-04-05 03:20:42 +0000856 else if (auto *Cmd = dyn_cast<AssertCommand>(Base))
Petr Hosek02ad5162017-03-15 03:33:23 +0000857 Cmd->Expression();
858 }
859}
860
Rafael Espindolade8d9892017-04-29 15:44:03 +0000861// Do a last effort at synchronizing the linker script "AST" and the section
862// list. This is needed to account for last minute changes, like adding a
863// .ARM.exidx terminator and sorting SHF_LINK_ORDER sections.
864//
865// FIXME: We should instead create the "AST" earlier and the above changes would
866// be done directly in the "AST".
867//
868// This can only handle new sections being added and sections being reordered.
869void LinkerScript::synchronize() {
870 for (BaseCommand *Base : Opt.Commands) {
871 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
872 if (!Cmd)
873 continue;
874 ArrayRef<InputSection *> Sections = Cmd->Sec->Sections;
875 std::vector<InputSectionBase **> ScriptSections;
876 DenseSet<InputSectionBase *> ScriptSectionsSet;
877 for (BaseCommand *Base : Cmd->Commands) {
878 auto *ISD = dyn_cast<InputSectionDescription>(Base);
879 if (!ISD)
880 continue;
881 for (InputSectionBase *&IS : ISD->Sections) {
882 if (IS->Live) {
883 ScriptSections.push_back(&IS);
884 ScriptSectionsSet.insert(IS);
885 }
886 }
887 }
888 std::vector<InputSectionBase *> Missing;
889 for (InputSection *IS : Sections)
890 if (!ScriptSectionsSet.count(IS))
891 Missing.push_back(IS);
892 if (!Missing.empty()) {
893 auto ISD = make<InputSectionDescription>("");
894 ISD->Sections = Missing;
895 Cmd->Commands.push_back(ISD);
896 for (InputSectionBase *&IS : ISD->Sections)
897 if (IS->Live)
898 ScriptSections.push_back(&IS);
899 }
900 assert(ScriptSections.size() == Sections.size());
901 for (int I = 0, N = Sections.size(); I < N; ++I)
902 *ScriptSections[I] = Sections[I];
903 }
904}
905
Rafael Espindola02ed7572017-05-04 19:34:17 +0000906static bool allocateHeaders(std::vector<PhdrEntry> &Phdrs,
907 ArrayRef<OutputSection *> OutputSections,
908 uint64_t Min) {
909 auto FirstPTLoad =
910 std::find_if(Phdrs.begin(), Phdrs.end(),
911 [](const PhdrEntry &E) { return E.p_type == PT_LOAD; });
912 if (FirstPTLoad == Phdrs.end())
913 return false;
914
915 uint64_t HeaderSize = getHeaderSize();
916 if (HeaderSize <= Min || Script->hasPhdrsCommands()) {
917 Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
918 Out::ElfHeader->Addr = Min;
919 Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
920 return true;
921 }
922
923 assert(FirstPTLoad->First == Out::ElfHeader);
924 OutputSection *ActualFirst = nullptr;
925 for (OutputSection *Sec : OutputSections) {
926 if (Sec->FirstInPtLoad == Out::ElfHeader) {
927 ActualFirst = Sec;
928 break;
929 }
930 }
931 if (ActualFirst) {
932 for (OutputSection *Sec : OutputSections)
933 if (Sec->FirstInPtLoad == Out::ElfHeader)
934 Sec->FirstInPtLoad = ActualFirst;
935 FirstPTLoad->First = ActualFirst;
936 } else {
937 Phdrs.erase(FirstPTLoad);
938 }
939
940 auto PhdrI = std::find_if(Phdrs.begin(), Phdrs.end(), [](const PhdrEntry &E) {
941 return E.p_type == PT_PHDR;
942 });
943 if (PhdrI != Phdrs.end())
944 Phdrs.erase(PhdrI);
945 return false;
946}
947
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000948void LinkerScript::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000949 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rafael Espindolabe607332016-09-30 00:16:11 +0000950 Dot = 0;
Rafael Espindola72dc1952017-03-17 13:05:04 +0000951 ErrorOnMissingSection = true;
Rafael Espindola06f47432017-02-06 22:21:46 +0000952 switchTo(Aether);
953
Rui Ueyama8f99f732017-04-05 03:20:42 +0000954 for (BaseCommand *Base : Opt.Commands) {
955 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
Rui Ueyamad379f732017-04-05 03:20:22 +0000956 assignSymbol(Cmd, false);
George Rimar652852c2016-04-16 10:10:32 +0000957 continue;
958 }
959
Rui Ueyama8f99f732017-04-05 03:20:42 +0000960 if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
Rafael Espindola4595df92017-03-10 16:04:26 +0000961 Cmd->Expression();
George Rimareefa7582016-08-04 09:29:31 +0000962 continue;
963 }
964
Rui Ueyama8f99f732017-04-05 03:20:42 +0000965 auto *Cmd = cast<OutputSectionCommand>(Base);
Rafael Espindolad3190792016-09-16 15:10:23 +0000966 assignOffsets(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000967 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000968
George Rimar0c1c8082017-03-14 10:00:19 +0000969 uint64_t MinVA = std::numeric_limits<uint64_t>::max();
Rafael Espindola24e6f362017-02-24 15:07:30 +0000970 for (OutputSection *Sec : *OutputSections) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000971 if (Sec->Flags & SHF_ALLOC)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000972 MinVA = std::min<uint64_t>(MinVA, Sec->Addr);
Rafael Espindolaea590d92017-02-08 15:19:03 +0000973 else
974 Sec->Addr = 0;
975 }
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000976
George Rimar2d262102017-03-14 09:03:53 +0000977 allocateHeaders(Phdrs, *OutputSections, MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000978}
979
Rui Ueyama464daad2016-08-22 04:55:20 +0000980// Creates program headers as instructed by PHDRS linker script command.
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000981std::vector<PhdrEntry> LinkerScript::createPhdrs() {
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000982 std::vector<PhdrEntry> Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000983
Rui Ueyama464daad2016-08-22 04:55:20 +0000984 // Process PHDRS and FILEHDR keywords because they are not
985 // real output sections and cannot be added in the following loop.
Eugene Leviantbbe38602016-07-19 09:25:43 +0000986 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000987 Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000988 PhdrEntry &Phdr = Ret.back();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000989
990 if (Cmd.HasFilehdr)
Rui Ueyama9d1bacb12017-02-27 02:31:26 +0000991 Phdr.add(Out::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000992 if (Cmd.HasPhdrs)
Rui Ueyama9d1bacb12017-02-27 02:31:26 +0000993 Phdr.add(Out::ProgramHeaders);
Eugene Leviant56b21c82016-09-09 09:46:16 +0000994
995 if (Cmd.LMAExpr) {
Rafael Espindola72dc1952017-03-17 13:05:04 +0000996 Phdr.p_paddr = Cmd.LMAExpr().getValue();
Eugene Leviant56b21c82016-09-09 09:46:16 +0000997 Phdr.HasLMA = true;
998 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000999 }
1000
Rui Ueyama464daad2016-08-22 04:55:20 +00001001 // Add output sections to program headers.
Rafael Espindola24e6f362017-02-24 15:07:30 +00001002 for (OutputSection *Sec : *OutputSections) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001003 if (!(Sec->Flags & SHF_ALLOC))
Eugene Leviantbbe38602016-07-19 09:25:43 +00001004 break;
1005
Eugene Leviantce30b1c2016-10-19 15:04:49 +00001006 // Assign headers specified by linker script
Rafael Espindola2c923c22017-05-10 14:28:31 +00001007 for (size_t Id : getPhdrIndices(Sec)) {
Eugene Leviantce30b1c2016-10-19 15:04:49 +00001008 Ret[Id].add(Sec);
1009 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
Rafael Espindola17cb7c02016-12-19 17:01:01 +00001010 Ret[Id].p_flags |= Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +00001011 }
Eugene Leviantbbe38602016-07-19 09:25:43 +00001012 }
Rui Ueyamaedebbdf2016-07-24 23:47:31 +00001013 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +00001014}
1015
Rui Ueyamab8dd23f2017-03-21 23:02:51 +00001016bool LinkerScript::ignoreInterpSection() {
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +00001017 // Ignore .interp section in case we have PHDRS specification
1018 // and PT_INTERP isn't listed.
Rui Ueyamae31d9882017-04-05 05:06:17 +00001019 if (Opt.PhdrsCommands.empty())
1020 return false;
1021 for (PhdrsCommand &Cmd : Opt.PhdrsCommands)
1022 if (Cmd.Type == PT_INTERP)
1023 return false;
1024 return true;
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +00001025}
1026
Rafael Espindolac5b612b2017-05-10 14:01:13 +00001027Optional<uint32_t> LinkerScript::getFiller(OutputSection *Sec) {
Rui Ueyama8f99f732017-04-05 03:20:42 +00001028 for (BaseCommand *Base : Opt.Commands)
1029 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
Rafael Espindolac5b612b2017-05-10 14:01:13 +00001030 if (Cmd->Sec == Sec)
George Rimarf6c3cce2016-07-21 07:48:54 +00001031 return Cmd->Filler;
James Henderson9d9a6632017-04-07 10:36:42 +00001032 return None;
George Rimare2ee72b2016-02-26 14:48:31 +00001033}
1034
George Rimare38cbab2016-09-26 19:22:50 +00001035static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
Rui Ueyamaf62d2602017-04-05 05:06:37 +00001036 if (Size == 1)
1037 *Buf = Data;
1038 else if (Size == 2)
Rui Ueyamaf93ed4d2017-03-21 21:40:08 +00001039 write16(Buf, Data, Config->Endianness);
Rui Ueyamaf62d2602017-04-05 05:06:37 +00001040 else if (Size == 4)
Rui Ueyamaf93ed4d2017-03-21 21:40:08 +00001041 write32(Buf, Data, Config->Endianness);
Rui Ueyamaf62d2602017-04-05 05:06:37 +00001042 else if (Size == 8)
Rui Ueyamaf93ed4d2017-03-21 21:40:08 +00001043 write64(Buf, Data, Config->Endianness);
Rui Ueyamaf62d2602017-04-05 05:06:37 +00001044 else
George Rimare38cbab2016-09-26 19:22:50 +00001045 llvm_unreachable("unsupported Size argument");
George Rimare38cbab2016-09-26 19:22:50 +00001046}
1047
Rafael Espindola660c9ab2017-05-05 21:34:26 +00001048void LinkerScript::writeDataBytes(OutputSection *Sec, uint8_t *Buf) {
1049 auto I = std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
1050 [=](BaseCommand *Base) {
1051 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
1052 if (Cmd->Sec == Sec)
1053 return true;
1054 return false;
1055 });
1056 if (I == Opt.Commands.end())
George Rimare38cbab2016-09-26 19:22:50 +00001057 return;
Rafael Espindola660c9ab2017-05-05 21:34:26 +00001058 auto *Cmd = cast<OutputSectionCommand>(*I);
Rui Ueyama8f99f732017-04-05 03:20:42 +00001059 for (BaseCommand *Base : Cmd->Commands)
1060 if (auto *Data = dyn_cast<BytesDataCommand>(Base))
George Rimara8dba482017-03-20 10:09:58 +00001061 writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
George Rimare38cbab2016-09-26 19:22:50 +00001062}
1063
Rafael Espindoladc1ed122017-05-10 14:12:02 +00001064bool LinkerScript::hasLMA(OutputSection *Sec) {
Rui Ueyama8f99f732017-04-05 03:20:42 +00001065 for (BaseCommand *Base : Opt.Commands)
1066 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
Rafael Espindoladc1ed122017-05-10 14:12:02 +00001067 if (Cmd->LMAExpr && Cmd->Sec == Sec)
Eugene Leviantb71d6f72016-10-06 09:39:28 +00001068 return true;
1069 return false;
George Rimar8ceadb32016-08-17 07:44:19 +00001070}
1071
Rui Ueyamab8dd23f2017-03-21 23:02:51 +00001072ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
Rafael Espindola4595df92017-03-10 16:04:26 +00001073 if (S == ".")
Rafael Espindola72dc1952017-03-17 13:05:04 +00001074 return {CurOutSec, Dot - CurOutSec->Addr};
George Rimara8dba482017-03-20 10:09:58 +00001075 if (SymbolBody *B = findSymbol(S)) {
Rafael Espindola72dc1952017-03-17 13:05:04 +00001076 if (auto *D = dyn_cast<DefinedRegular>(B))
1077 return {D->Section, D->Value};
Petr Hosek30f16b22017-03-23 03:52:34 +00001078 if (auto *C = dyn_cast<DefinedCommon>(B))
1079 return {InX::Common, C->Offset};
Rafael Espindola72dc1952017-03-17 13:05:04 +00001080 }
Eugene Leviantf6aeed32016-12-22 13:13:12 +00001081 error(Loc + ": symbol not found: " + S);
George Rimar884e7862016-09-08 08:19:13 +00001082 return 0;
1083}
1084
Rui Ueyamab8dd23f2017-03-21 23:02:51 +00001085bool LinkerScript::isDefined(StringRef S) { return findSymbol(S) != nullptr; }
George Rimarf34f45f2016-09-23 13:17:23 +00001086
Rafael Espindola2c923c22017-05-10 14:28:31 +00001087// Returns indices of ELF headers containing specific section. Each index is a
1088// zero based number of ELF header listed within PHDRS {} script block.
1089std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Sec) {
Rui Ueyama8f99f732017-04-05 03:20:42 +00001090 for (BaseCommand *Base : Opt.Commands) {
1091 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
Rafael Espindola2c923c22017-05-10 14:28:31 +00001092 if (!Cmd || Cmd->Sec != Sec)
George Rimar31d842f2016-07-20 16:43:03 +00001093 continue;
1094
Rui Ueyama29c5a2a2016-07-26 00:27:36 +00001095 std::vector<size_t> Ret;
1096 for (StringRef PhdrName : Cmd->Phdrs)
Eugene Leviant2a942c42016-12-05 16:38:32 +00001097 Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName));
Rui Ueyama29c5a2a2016-07-26 00:27:36 +00001098 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +00001099 }
George Rimar31d842f2016-07-20 16:43:03 +00001100 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +00001101}
1102
Rui Ueyamab8dd23f2017-03-21 23:02:51 +00001103size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
Rui Ueyama29c5a2a2016-07-26 00:27:36 +00001104 size_t I = 0;
1105 for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
1106 if (Cmd.Name == PhdrName)
1107 return I;
1108 ++I;
1109 }
Eugene Leviant2a942c42016-12-05 16:38:32 +00001110 error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
Rui Ueyama29c5a2a2016-07-26 00:27:36 +00001111 return 0;
1112}