blob: 5a9e4652b2291d683cf2d4a2c3ec1233a910466e [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 {
51 if (Sec)
52 return Sec->getOffset(Val) + Sec->getOutputSection()->Addr;
53 return Val;
54}
55
Rafael Espindola7ba5f472017-03-17 14:55:36 +000056uint64_t ExprValue::getSecAddr() const {
57 if (Sec)
58 return Sec->getOffset(0) + Sec->getOutputSection()->Addr;
59 return 0;
60}
61
Meador Inge8f1f3c42017-01-09 18:36:57 +000062template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) {
Petr Hosek5e51f7d2017-02-21 22:32:51 +000063 Symbol *Sym;
Rafael Espindola3dabfc62016-10-31 13:14:53 +000064 uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
Petr Hosek5e51f7d2017-02-21 22:32:51 +000065 std::tie(Sym, std::ignore) = Symtab<ELFT>::X->insert(
66 Cmd->Name, /*Type*/ 0, Visibility, /*CanOmitFromDynSym*/ false,
67 /*File*/ nullptr);
68 Sym->Binding = STB_GLOBAL;
Rafael Espindola72dc1952017-03-17 13:05:04 +000069 ExprValue Value = Cmd->Expression();
70 SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
George Rimar01aa7952017-04-14 09:23:26 +000071
72 // We want to set symbol values early if we can. This allows us to use symbols
73 // as variables in linker scripts. Doing so allows us to write expressions
74 // like this: `alignment = 16; . = ALIGN(., alignment)`
75 uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0;
Rui Ueyama80474a22017-02-28 19:29:55 +000076 replaceBody<DefinedRegular>(Sym, Cmd->Name, /*IsLocal=*/false, Visibility,
George Rimar01aa7952017-04-14 09:23:26 +000077 STT_NOTYPE, SymValue, 0, Sec, nullptr);
Meador Inge8f1f3c42017-01-09 18:36:57 +000078 return Sym->body();
Eugene Leviantceabe802016-08-11 07:56:43 +000079}
80
Rui Ueyamab8dd23f2017-03-21 23:02:51 +000081OutputSection *LinkerScript::getOutputSection(const Twine &Loc,
82 StringRef Name) {
George Rimar851dc1e2017-03-14 10:15:53 +000083 for (OutputSection *Sec : *OutputSections)
84 if (Sec->Name == Name)
85 return Sec;
86
Rui Ueyamaa08fa2e2017-04-05 00:42:45 +000087 static OutputSection Dummy("", 0, 0);
Rafael Espindola72dc1952017-03-17 13:05:04 +000088 if (ErrorOnMissingSection)
89 error(Loc + ": undefined section " + Name);
Rui Ueyamaa08fa2e2017-04-05 00:42:45 +000090 return &Dummy;
George Rimar851dc1e2017-03-14 10:15:53 +000091}
92
George Rimard83ce1b2017-03-14 10:24:47 +000093// This function is essentially the same as getOutputSection(Name)->Size,
94// but it won't print out an error message if a given section is not found.
95//
96// Linker script does not create an output section if its content is empty.
97// We want to allow SIZEOF(.foo) where .foo is a section which happened to
98// be empty. That is why this function is different from getOutputSection().
Rui Ueyamab8dd23f2017-03-21 23:02:51 +000099uint64_t LinkerScript::getOutputSectionSize(StringRef Name) {
George Rimard83ce1b2017-03-14 10:24:47 +0000100 for (OutputSection *Sec : *OutputSections)
101 if (Sec->Name == Name)
102 return Sec->Size;
103 return 0;
104}
105
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000106void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
Rafael Espindola72dc1952017-03-17 13:05:04 +0000107 uint64_t Val = E().getValue();
Rafael Espindola679828f2017-02-17 16:26:13 +0000108 if (Val < Dot) {
109 if (InSec)
George Rimar2ee2d2d2017-02-21 14:50:38 +0000110 error(Loc + ": unable to move location counter backward for: " +
111 CurOutSec->Name);
Rafael Espindola679828f2017-02-17 16:26:13 +0000112 else
George Rimar2ee2d2d2017-02-21 14:50:38 +0000113 error(Loc + ": unable to move location counter backward");
Rafael Espindola679828f2017-02-17 16:26:13 +0000114 }
115 Dot = Val;
116 // Update to location counter means update to section size.
117 if (InSec)
118 CurOutSec->Size = Dot - CurOutSec->Addr;
119}
120
George Rimarb2b70972017-02-07 10:23:28 +0000121// Sets value of a symbol. Two kinds of symbols are processed: synthetic
122// symbols, whose value is an offset from beginning of section and regular
123// symbols whose value is absolute.
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000124void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
Rafael Espindola4cd73522017-02-17 16:01:51 +0000125 if (Cmd->Name == ".") {
George Rimar2ee2d2d2017-02-21 14:50:38 +0000126 setDot(Cmd->Expression, Cmd->Location, InSec);
Rafael Espindola4cd73522017-02-17 16:01:51 +0000127 return;
128 }
129
George Rimarb2b70972017-02-07 10:23:28 +0000130 if (!Cmd->Sym)
Meador Inge8f1f3c42017-01-09 18:36:57 +0000131 return;
132
Rafael Espindola5616adf2017-03-08 22:36:28 +0000133 auto *Sym = cast<DefinedRegular>(Cmd->Sym);
Rafael Espindola72dc1952017-03-17 13:05:04 +0000134 ExprValue V = Cmd->Expression();
135 if (V.isAbsolute()) {
136 Sym->Value = V.getValue();
137 } else {
138 Sym->Section = V.Sec;
139 if (Sym->Section->Flags & SHF_ALLOC)
140 Sym->Value = V.Val;
141 else
142 Sym->Value = V.getValue();
Meador Inge8f1f3c42017-01-09 18:36:57 +0000143 }
Eugene Leviantdb741e72016-09-07 07:08:43 +0000144}
Meador Inge8f1f3c42017-01-09 18:36:57 +0000145
George Rimara8dba482017-03-20 10:09:58 +0000146static SymbolBody *findSymbol(StringRef S) {
147 switch (Config->EKind) {
148 case ELF32LEKind:
149 return Symtab<ELF32LE>::X->find(S);
150 case ELF32BEKind:
151 return Symtab<ELF32BE>::X->find(S);
152 case ELF64LEKind:
153 return Symtab<ELF64LE>::X->find(S);
154 case ELF64BEKind:
155 return Symtab<ELF64BE>::X->find(S);
156 default:
157 llvm_unreachable("unknown Config->EKind");
158 }
159}
160
161static SymbolBody *addRegularSymbol(SymbolAssignment *Cmd) {
162 switch (Config->EKind) {
163 case ELF32LEKind:
164 return addRegular<ELF32LE>(Cmd);
165 case ELF32BEKind:
166 return addRegular<ELF32BE>(Cmd);
167 case ELF64LEKind:
168 return addRegular<ELF64LE>(Cmd);
169 case ELF64BEKind:
170 return addRegular<ELF64BE>(Cmd);
171 default:
172 llvm_unreachable("unknown Config->EKind");
173 }
174}
175
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000176void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
Rui Ueyama16024212016-08-11 23:22:52 +0000177 if (Cmd->Name == ".")
Meador Inge8f1f3c42017-01-09 18:36:57 +0000178 return;
179
180 // If a symbol was in PROVIDE(), we need to define it only when
181 // it is a referenced undefined symbol.
George Rimara8dba482017-03-20 10:09:58 +0000182 SymbolBody *B = findSymbol(Cmd->Name);
Meador Inge8f1f3c42017-01-09 18:36:57 +0000183 if (Cmd->Provide && (!B || B->isDefined()))
184 return;
185
George Rimara8dba482017-03-20 10:09:58 +0000186 Cmd->Sym = addRegularSymbol(Cmd);
Eugene Leviantceabe802016-08-11 07:56:43 +0000187}
188
George Rimar076fe152016-07-21 06:43:01 +0000189bool SymbolAssignment::classof(const BaseCommand *C) {
190 return C->Kind == AssignmentKind;
191}
192
193bool OutputSectionCommand::classof(const BaseCommand *C) {
194 return C->Kind == OutputSectionKind;
195}
196
George Rimareea31142016-07-21 14:26:59 +0000197bool InputSectionDescription::classof(const BaseCommand *C) {
198 return C->Kind == InputSectionKind;
199}
200
George Rimareefa7582016-08-04 09:29:31 +0000201bool AssertCommand::classof(const BaseCommand *C) {
202 return C->Kind == AssertKind;
203}
204
George Rimare38cbab2016-09-26 19:22:50 +0000205bool BytesDataCommand::classof(const BaseCommand *C) {
206 return C->Kind == BytesDataKind;
207}
208
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000209static StringRef basename(InputSectionBase *S) {
210 if (S->File)
211 return sys::path::filename(S->File->getName());
Rui Ueyamae0be2902016-11-21 02:10:12 +0000212 return "";
213}
214
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000215bool LinkerScript::shouldKeep(InputSectionBase *S) {
Rui Ueyamae0be2902016-11-21 02:10:12 +0000216 for (InputSectionDescription *ID : Opt.KeptSections)
217 if (ID->FilePat.match(basename(S)))
218 for (SectionPattern &P : ID->SectionPatterns)
219 if (P.SectionPat.match(S->Name))
220 return true;
George Rimareea31142016-07-21 14:26:59 +0000221 return false;
222}
223
Rui Ueyamaea93fe02017-04-05 00:43:25 +0000224// A helper function for the SORT() command.
Rafael Espindolac404d502017-02-23 02:32:18 +0000225static std::function<bool(InputSectionBase *, InputSectionBase *)>
George Rimarbe394db2016-09-16 20:21:55 +0000226getComparator(SortSectionPolicy K) {
227 switch (K) {
228 case SortSectionPolicy::Alignment:
Rui Ueyamaea93fe02017-04-05 00:43:25 +0000229 return [](InputSectionBase *A, InputSectionBase *B) {
230 // ">" is not a mistake. Sections with larger alignments are placed
231 // before sections with smaller alignments in order to reduce the
232 // amount of padding necessary. This is compatible with GNU.
233 return A->Alignment > B->Alignment;
234 };
George Rimarbe394db2016-09-16 20:21:55 +0000235 case SortSectionPolicy::Name:
Rui Ueyamaea93fe02017-04-05 00:43:25 +0000236 return [](InputSectionBase *A, InputSectionBase *B) {
237 return A->Name < B->Name;
238 };
George Rimarbe394db2016-09-16 20:21:55 +0000239 case SortSectionPolicy::Priority:
Rui Ueyamaea93fe02017-04-05 00:43:25 +0000240 return [](InputSectionBase *A, InputSectionBase *B) {
241 return getPriority(A->Name) < getPriority(B->Name);
242 };
George Rimarbe394db2016-09-16 20:21:55 +0000243 default:
244 llvm_unreachable("unknown sort policy");
245 }
Rui Ueyama742c3832016-08-04 22:27:00 +0000246}
George Rimar0702c4e2016-07-29 15:32:46 +0000247
Rui Ueyamaea93fe02017-04-05 00:43:25 +0000248// A helper function for the SORT() command.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000249static bool matchConstraints(ArrayRef<InputSectionBase *> Sections,
George Rimar06ae6832016-08-12 09:07:57 +0000250 ConstraintKind Kind) {
George Rimar8f66df92016-08-12 20:38:20 +0000251 if (Kind == ConstraintKind::NoConstraint)
252 return true;
Rui Ueyama2c7171b2017-04-05 00:43:45 +0000253
254 bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) {
255 return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE;
George Rimar06ae6832016-08-12 09:07:57 +0000256 });
Rui Ueyama2c7171b2017-04-05 00:43:45 +0000257
Rafael Espindolae746e522016-09-21 18:33:44 +0000258 return (IsRW && Kind == ConstraintKind::ReadWrite) ||
259 (!IsRW && Kind == ConstraintKind::ReadOnly);
George Rimar06ae6832016-08-12 09:07:57 +0000260}
261
Rafael Espindolac404d502017-02-23 02:32:18 +0000262static void sortSections(InputSectionBase **Begin, InputSectionBase **End,
Rui Ueyamaee924702016-09-20 19:42:41 +0000263 SortSectionPolicy K) {
264 if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
George Rimar07171f22016-09-21 15:56:44 +0000265 std::stable_sort(Begin, End, getComparator(K));
Rui Ueyamaee924702016-09-20 19:42:41 +0000266}
267
Rafael Espindolad3190792016-09-16 15:10:23 +0000268// Compute and remember which sections the InputSectionDescription matches.
Rui Ueyama72e107f2017-04-05 02:05:48 +0000269std::vector<InputSectionBase *>
270LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
271 std::vector<InputSectionBase *> Ret;
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +0000272
Rui Ueyama72e107f2017-04-05 02:05:48 +0000273 // Collects all sections that satisfy constraints of Cmd.
274 for (const SectionPattern &Pat : Cmd->SectionPatterns) {
275 size_t SizeBefore = Ret.size();
276
277 for (InputSectionBase *Sec : InputSections) {
278 if (Sec->Assigned)
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +0000279 continue;
Rui Ueyama72e107f2017-04-05 02:05:48 +0000280
Rafael Espindola908a3d32017-02-16 14:36:09 +0000281 // For -emit-relocs we have to ignore entries like
282 // .rela.dyn : { *(.rela.data) }
283 // which are common because they are in the default bfd script.
Rui Ueyama72e107f2017-04-05 02:05:48 +0000284 if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
Rafael Espindola908a3d32017-02-16 14:36:09 +0000285 continue;
Rui Ueyama8c6a5aa2016-11-05 22:37:59 +0000286
Rui Ueyama72e107f2017-04-05 02:05:48 +0000287 StringRef Filename = basename(Sec);
288 if (!Cmd->FilePat.match(Filename) ||
289 Pat.ExcludedFilePat.match(Filename) ||
290 !Pat.SectionPat.match(Sec->Name))
Rui Ueyamae0be2902016-11-21 02:10:12 +0000291 continue;
Rui Ueyama72e107f2017-04-05 02:05:48 +0000292
293 Ret.push_back(Sec);
294 Sec->Assigned = true;
George Rimar395281c2016-09-16 17:42:10 +0000295 }
Rafael Espindolad3190792016-09-16 15:10:23 +0000296
George Rimar07171f22016-09-21 15:56:44 +0000297 // Sort sections as instructed by SORT-family commands and --sort-section
298 // option. Because SORT-family commands can be nested at most two depth
299 // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
300 // line option is respected even if a SORT command is given, the exact
301 // behavior we have here is a bit complicated. Here are the rules.
302 //
303 // 1. If two SORT commands are given, --sort-section is ignored.
304 // 2. If one SORT command is given, and if it is not SORT_NONE,
305 // --sort-section is handled as an inner SORT command.
306 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
307 // 4. If no SORT command is given, sort according to --sort-section.
Rui Ueyama72e107f2017-04-05 02:05:48 +0000308 InputSectionBase **Begin = Ret.data() + SizeBefore;
309 InputSectionBase **End = Ret.data() + Ret.size();
George Rimar07171f22016-09-21 15:56:44 +0000310 if (Pat.SortOuter != SortSectionPolicy::None) {
311 if (Pat.SortInner == SortSectionPolicy::Default)
312 sortSections(Begin, End, Config->SortSection);
313 else
314 sortSections(Begin, End, Pat.SortInner);
315 sortSections(Begin, End, Pat.SortOuter);
316 }
Rui Ueyamaee924702016-09-20 19:42:41 +0000317 }
Rui Ueyama72e107f2017-04-05 02:05:48 +0000318 return Ret;
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000319}
320
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000321void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000322 for (InputSectionBase *S : V) {
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000323 S->Live = false;
George Rimar503206c2017-03-15 15:42:44 +0000324 if (S == InX::ShStrTab)
Rafael Espindolaecbfd872017-02-17 17:35:07 +0000325 error("discarding .shstrtab section is not allowed");
George Rimar647c1682017-02-17 19:34:05 +0000326 discard(S->DependentSections);
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000327 }
328}
329
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000330std::vector<InputSectionBase *>
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000331LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000332 std::vector<InputSectionBase *> Ret;
Rui Ueyamae7f912c2016-08-03 21:12:09 +0000333
Rui Ueyama8f99f732017-04-05 03:20:42 +0000334 for (BaseCommand *Base : OutCmd.Commands) {
335 auto *Cmd = dyn_cast<InputSectionDescription>(Base);
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000336 if (!Cmd)
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000337 continue;
Rui Ueyama72e107f2017-04-05 02:05:48 +0000338
339 Cmd->Sections = computeInputSections(Cmd);
Rafael Espindolae4c8b9b2017-04-07 16:10:46 +0000340 Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000341 }
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000342
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000343 return Ret;
344}
345
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000346void LinkerScript::processCommands(OutputSectionFactory &Factory) {
Rafael Espindola5616adf2017-03-08 22:36:28 +0000347 // A symbol can be assigned before any section is mentioned in the linker
348 // script. In an DSO, the symbol values are addresses, so the only important
349 // section values are:
350 // * SHN_UNDEF
351 // * SHN_ABS
352 // * Any value meaning a regular section.
353 // To handle that, create a dummy aether section that fills the void before
354 // the linker scripts switches to another section. It has an index of one
355 // which will map to whatever the first actual section is.
356 Aether = make<OutputSection>("", 0, SHF_ALLOC);
357 Aether->SectionIndex = 1;
358 CurOutSec = Aether;
Rafael Espindola49592cf2017-03-20 14:33:33 +0000359 Dot = 0;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000360
Rui Ueyama92a5ba62017-04-05 16:07:44 +0000361 for (size_t I = 0; I < Opt.Commands.size(); ++I) {
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000362 // Handle symbol assignments outside of any output section.
Rui Ueyama92a5ba62017-04-05 16:07:44 +0000363 if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) {
Rafael Espindola4cd73522017-02-17 16:01:51 +0000364 addSymbol(Cmd);
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000365 continue;
366 }
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000367
Rui Ueyama92a5ba62017-04-05 16:07:44 +0000368 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I])) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000369 std::vector<InputSectionBase *> V = createInputSectionList(*Cmd);
Rafael Espindola7bd37872016-09-12 16:05:16 +0000370
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000371 // The output section name `/DISCARD/' is special.
372 // Any input section assigned to it is discarded.
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000373 if (Cmd->Name == "/DISCARD/") {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000374 discard(V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000375 continue;
376 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000377
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000378 // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
379 // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
380 // sections satisfy a given constraint. If not, a directive is handled
George Rimar07d7c422017-04-05 09:19:29 +0000381 // as if it wasn't present from the beginning.
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000382 //
383 // Because we'll iterate over Commands many more times, the easiest
George Rimar07d7c422017-04-05 09:19:29 +0000384 // way to "make it as if it wasn't present" is to just remove it.
George Rimarf7f0d082017-03-14 11:23:33 +0000385 if (!matchConstraints(V, Cmd->Constraint)) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000386 for (InputSectionBase *S : V)
Rui Ueyamaf94efdd2016-11-20 23:15:52 +0000387 S->Assigned = false;
Rui Ueyama92a5ba62017-04-05 16:07:44 +0000388 Opt.Commands.erase(Opt.Commands.begin() + I);
George Rimar07d7c422017-04-05 09:19:29 +0000389 --I;
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000390 continue;
391 }
392
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000393 // A directive may contain symbol definitions like this:
394 // ".foo : { ...; bar = .; }". Handle them.
Rui Ueyama8f99f732017-04-05 03:20:42 +0000395 for (BaseCommand *Base : Cmd->Commands)
396 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
Rafael Espindola4cd73522017-02-17 16:01:51 +0000397 addSymbol(OutCmd);
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000398
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000399 // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
400 // is given, input sections are aligned to that value, whether the
401 // given value is larger or smaller than the original section alignment.
402 if (Cmd->SubalignExpr) {
Rafael Espindola72dc1952017-03-17 13:05:04 +0000403 uint32_t Subalign = Cmd->SubalignExpr().getValue();
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000404 for (InputSectionBase *S : V)
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000405 S->Alignment = Subalign;
George Rimardb24d9c2016-08-19 15:18:23 +0000406 }
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000407
408 // Add input sections to an output section.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000409 for (InputSectionBase *S : V)
George Rimare21c3af2017-03-14 09:30:25 +0000410 Factory.addInputSec(S, Cmd->Name);
Eugene Leviantceabe802016-08-11 07:56:43 +0000411 }
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000412 }
Rafael Espindola5616adf2017-03-08 22:36:28 +0000413 CurOutSec = nullptr;
Eugene Leviant20d03192016-09-16 15:30:47 +0000414}
Eugene Leviante63d81b2016-07-20 14:43:20 +0000415
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000416// Add sections that didn't match any sections command.
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000417void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
Rui Ueyama536a2672017-02-27 02:32:08 +0000418 for (InputSectionBase *S : InputSections)
Rafael Espindola8f9026b2016-11-08 18:23:02 +0000419 if (S->Live && !S->OutSec)
George Rimare21c3af2017-03-14 09:30:25 +0000420 Factory.addInputSec(S, getOutputSectionName(S->Name));
Eugene Leviante63d81b2016-07-20 14:43:20 +0000421}
422
George Rimarf7f0d082017-03-14 11:23:33 +0000423static bool isTbss(OutputSection *Sec) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000424 return (Sec->Flags & SHF_TLS) && Sec->Type == SHT_NOBITS;
Rafael Espindolaa940e532016-09-22 12:35:44 +0000425}
426
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000427void LinkerScript::output(InputSection *S) {
Rafael Espindolad3190792016-09-16 15:10:23 +0000428 if (!AlreadyOutputIS.insert(S).second)
429 return;
George Rimarf7f0d082017-03-14 11:23:33 +0000430 bool IsTbss = isTbss(CurOutSec);
Eugene Leviant20889c52016-08-31 08:13:33 +0000431
George Rimar0c1c8082017-03-14 10:00:19 +0000432 uint64_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
Rafael Espindolad3190792016-09-16 15:10:23 +0000433 Pos = alignTo(Pos, S->Alignment);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000434 S->OutSecOff = Pos - CurOutSec->Addr;
Rafael Espindola76b6bd32017-03-08 15:44:30 +0000435 Pos += S->getSize();
Rafael Espindolad3190792016-09-16 15:10:23 +0000436
437 // Update output section size after adding each section. This is so that
438 // SIZEOF works correctly in the case below:
439 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000440 CurOutSec->Size = Pos - CurOutSec->Addr;
Rafael Espindolad3190792016-09-16 15:10:23 +0000441
Meador Ingeb8897442017-01-24 02:34:00 +0000442 // If there is a memory region associated with this input section, then
443 // place the section in that region and update the region index.
444 if (CurMemRegion) {
445 CurMemRegion->Offset += CurOutSec->Size;
446 uint64_t CurSize = CurMemRegion->Offset - CurMemRegion->Origin;
447 if (CurSize > CurMemRegion->Length) {
448 uint64_t OverflowAmt = CurSize - CurMemRegion->Length;
449 error("section '" + CurOutSec->Name + "' will not fit in region '" +
450 CurMemRegion->Name + "': overflowed by " + Twine(OverflowAmt) +
451 " bytes");
452 }
453 }
454
Rafael Espindola7252ae52016-09-22 12:00:08 +0000455 if (IsTbss)
456 ThreadBssOffset = Pos - Dot;
457 else
Rafael Espindolad3190792016-09-16 15:10:23 +0000458 Dot = Pos;
459}
460
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000461void LinkerScript::flush() {
Rafael Espindolabd12e2a2017-03-01 14:12:21 +0000462 assert(CurOutSec);
463 if (!AlreadyOutputOS.insert(CurOutSec).second)
Rafael Espindola65499b92016-09-23 20:10:47 +0000464 return;
Rafael Espindola24e6f362017-02-24 15:07:30 +0000465 for (InputSection *I : CurOutSec->Sections)
466 output(I);
Eugene Leviant20889c52016-08-31 08:13:33 +0000467}
468
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000469void LinkerScript::switchTo(OutputSection *Sec) {
Rafael Espindolad3190792016-09-16 15:10:23 +0000470 if (CurOutSec == Sec)
471 return;
472 if (AlreadyOutputOS.count(Sec))
473 return;
474
Rafael Espindolad3190792016-09-16 15:10:23 +0000475 CurOutSec = Sec;
476
Rafael Espindola37707632017-03-07 14:55:52 +0000477 Dot = alignTo(Dot, CurOutSec->Alignment);
George Rimarf7f0d082017-03-14 11:23:33 +0000478 CurOutSec->Addr = isTbss(CurOutSec) ? Dot + ThreadBssOffset : Dot;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000479
480 // If neither AT nor AT> is specified for an allocatable section, the linker
481 // will set the LMA such that the difference between VMA and LMA for the
482 // section is the same as the preceding output section in the same region
483 // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
George Rimar21467872017-02-23 07:57:55 +0000484 if (LMAOffset)
Rafael Espindola29c1afb2017-02-24 14:34:12 +0000485 CurOutSec->LMAOffset = LMAOffset();
Rafael Espindolad3190792016-09-16 15:10:23 +0000486}
487
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000488void LinkerScript::process(BaseCommand &Base) {
Rui Ueyama2e081a42017-04-05 03:18:46 +0000489 // This handles the assignments to symbol or to the dot.
490 if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) {
491 assignSymbol(Cmd, true);
Eugene Leviantceabe802016-08-11 07:56:43 +0000492 return;
Rui Ueyama2de509c2016-08-12 00:55:08 +0000493 }
George Rimare38cbab2016-09-26 19:22:50 +0000494
495 // Handle BYTE(), SHORT(), LONG(), or QUAD().
Rui Ueyama2e081a42017-04-05 03:18:46 +0000496 if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) {
497 Cmd->Offset = Dot - CurOutSec->Addr;
498 Dot += Cmd->Size;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000499 CurOutSec->Size = Dot - CurOutSec->Addr;
George Rimare38cbab2016-09-26 19:22:50 +0000500 return;
501 }
502
Rui Ueyama2e081a42017-04-05 03:18:46 +0000503 // Handle ASSERT().
504 if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) {
505 Cmd->Expression();
Meador Ingeb2d99d62016-11-22 18:01:50 +0000506 return;
507 }
508
Rui Ueyama2e081a42017-04-05 03:18:46 +0000509 // Handle a single input section description command.
510 // It calculates and assigns the offsets for each section and also
George Rimare38cbab2016-09-26 19:22:50 +0000511 // updates the output section size.
Rui Ueyama2e081a42017-04-05 03:18:46 +0000512 auto &Cmd = cast<InputSectionDescription>(Base);
513 for (InputSectionBase *Sec : Cmd.Sections) {
George Rimar3fb5a6d2016-11-29 16:05:27 +0000514 // We tentatively added all synthetic sections at the beginning and removed
515 // empty ones afterwards (because there is no way to know whether they were
516 // going be empty or not other than actually running linker scripts.)
517 // We need to ignore remains of empty sections.
Rui Ueyama2e081a42017-04-05 03:18:46 +0000518 if (auto *S = dyn_cast<SyntheticSection>(Sec))
519 if (S->empty())
George Rimar3fb5a6d2016-11-29 16:05:27 +0000520 continue;
521
Rui Ueyama2e081a42017-04-05 03:18:46 +0000522 if (!Sec->Live)
George Rimar78ef6452017-02-21 15:46:43 +0000523 continue;
Rui Ueyama2e081a42017-04-05 03:18:46 +0000524 assert(CurOutSec == Sec->OutSec || AlreadyOutputOS.count(Sec->OutSec));
525 output(cast<InputSection>(Sec));
Eugene Leviantceabe802016-08-11 07:56:43 +0000526 }
527}
528
Rafael Espindola24e6f362017-02-24 15:07:30 +0000529static OutputSection *
530findSection(StringRef Name, const std::vector<OutputSection *> &Sections) {
Rui Ueyama0b2381e2017-04-05 03:19:06 +0000531 for (OutputSection *Sec : Sections)
532 if (Sec->Name == Name)
533 return Sec;
534 return nullptr;
George Rimar8f66df92016-08-12 20:38:20 +0000535}
536
Meador Ingeb8897442017-01-24 02:34:00 +0000537// This function searches for a memory region to place the given output
538// section in. If found, a pointer to the appropriate memory region is
539// returned. Otherwise, a nullptr is returned.
Rafael Espindola1902b332017-04-06 21:26:03 +0000540MemoryRegion *LinkerScript::findMemoryRegion(OutputSectionCommand *Cmd) {
Meador Ingeb8897442017-01-24 02:34:00 +0000541 // If a memory region name was specified in the output section command,
542 // then try to find that region first.
543 if (!Cmd->MemoryRegionName.empty()) {
544 auto It = Opt.MemoryRegions.find(Cmd->MemoryRegionName);
545 if (It != Opt.MemoryRegions.end())
546 return &It->second;
547 error("memory region '" + Cmd->MemoryRegionName + "' not declared");
548 return nullptr;
549 }
550
Rui Ueyamad7c54002017-04-05 03:19:43 +0000551 // If at least one memory region is defined, all sections must
552 // belong to some memory region. Otherwise, we don't need to do
553 // anything for memory regions.
Rui Ueyamacc400cc2017-04-05 03:19:24 +0000554 if (Opt.MemoryRegions.empty())
Meador Ingeb8897442017-01-24 02:34:00 +0000555 return nullptr;
556
Rafael Espindola1902b332017-04-06 21:26:03 +0000557 OutputSection *Sec = Cmd->Sec;
Meador Ingeb8897442017-01-24 02:34:00 +0000558 // See if a region can be found by matching section flags.
Rui Ueyama2e081a42017-04-05 03:18:46 +0000559 for (auto &Pair : Opt.MemoryRegions) {
560 MemoryRegion &M = Pair.second;
561 if ((M.Flags & Sec->Flags) && (M.NegFlags & Sec->Flags) == 0)
562 return &M;
Meador Ingeb8897442017-01-24 02:34:00 +0000563 }
564
565 // Otherwise, no suitable region was found.
566 if (Sec->Flags & SHF_ALLOC)
567 error("no memory region specified for section '" + Sec->Name + "'");
568 return nullptr;
569}
570
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000571// This function assigns offsets to input sections and an output section
572// for a single sections command (e.g. ".text { *(.text); }").
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000573void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) {
Rafael Espindola9b980092017-04-06 21:05:39 +0000574 OutputSection *Sec = Cmd->Sec;
Rafael Espindola2b074552017-02-03 22:27:05 +0000575 if (!Sec)
Rafael Espindolad3190792016-09-16 15:10:23 +0000576 return;
Meador Ingeb8897442017-01-24 02:34:00 +0000577
Rui Ueyamacba41012017-04-05 03:20:03 +0000578 if (Cmd->AddrExpr && (Sec->Flags & SHF_ALLOC))
Rui Ueyamad379f732017-04-05 03:20:22 +0000579 setDot(Cmd->AddrExpr, Cmd->Location, false);
Rafael Espindola679828f2017-02-17 16:26:13 +0000580
Eugene Leviant5784e962017-03-14 08:57:09 +0000581 if (Cmd->LMAExpr) {
George Rimar0c1c8082017-03-14 10:00:19 +0000582 uint64_t D = Dot;
Rafael Espindola72dc1952017-03-17 13:05:04 +0000583 LMAOffset = [=] { return Cmd->LMAExpr().getValue() - D; };
Eugene Leviant5784e962017-03-14 08:57:09 +0000584 }
585
Rafael Espindolafeed7502017-04-06 21:31:24 +0000586 CurMemRegion = Cmd->MemRegion;
Meador Ingeb8897442017-01-24 02:34:00 +0000587 if (CurMemRegion)
588 Dot = CurMemRegion->Offset;
589 switchTo(Sec);
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000590
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000591 // flush() may add orphan sections, so the order of flush() and
592 // symbol assignments is important. We want to call flush() first so
593 // that symbols pointing the end of the current section points to
594 // the location after orphan sections.
595 auto Mid =
Rui Ueyama8f99f732017-04-05 03:20:42 +0000596 std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
597 [](BaseCommand *Cmd) { return !isa<SymbolAssignment>(Cmd); })
598 .base();
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000599 for (auto I = Cmd->Commands.begin(); I != Mid; ++I)
Rafael Espindolad3190792016-09-16 15:10:23 +0000600 process(**I);
Rafael Espindola65499b92016-09-23 20:10:47 +0000601 flush();
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000602 for (auto I = Mid, E = Cmd->Commands.end(); I != E; ++I)
603 process(**I);
Rafael Espindolad3190792016-09-16 15:10:23 +0000604}
605
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000606void LinkerScript::removeEmptyCommands() {
Rafael Espindola6d38e4d2016-09-20 13:12:07 +0000607 // It is common practice to use very generic linker scripts. So for any
608 // given run some of the output sections in the script will be empty.
609 // We could create corresponding empty output sections, but that would
610 // clutter the output.
611 // We instead remove trivially empty sections. The bfd linker seems even
612 // more aggressive at removing them.
613 auto Pos = std::remove_if(
Rui Ueyama8f99f732017-04-05 03:20:42 +0000614 Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) {
615 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
Rafael Espindola9b980092017-04-06 21:05:39 +0000616 return !Cmd->Sec;
Rui Ueyama0b1b6952016-11-21 02:11:05 +0000617 return false;
Rafael Espindola6d38e4d2016-09-20 13:12:07 +0000618 });
619 Opt.Commands.erase(Pos, Opt.Commands.end());
Rafael Espindola07fe6122016-11-14 14:23:35 +0000620}
621
Rafael Espindola6a537372016-11-14 14:33:49 +0000622static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
Rui Ueyama8f99f732017-04-05 03:20:42 +0000623 for (BaseCommand *Base : Cmd.Commands)
624 if (!isa<InputSectionDescription>(*Base))
Rafael Espindola6a537372016-11-14 14:33:49 +0000625 return false;
626 return true;
627}
Rafael Espindola6d38e4d2016-09-20 13:12:07 +0000628
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000629void LinkerScript::adjustSectionsBeforeSorting() {
Rafael Espindola9546fff2016-09-22 14:40:50 +0000630 // If the output section contains only symbol assignments, create a
631 // corresponding output section. The bfd linker seems to only create them if
632 // '.' is assigned to, but creating these section should not have any bad
633 // consequeces and gives us a section to put the symbol in.
George Rimar0c1c8082017-03-14 10:00:19 +0000634 uint64_t Flags = SHF_ALLOC;
Rafael Espindolaf93b8c22016-11-26 06:55:35 +0000635 uint32_t Type = SHT_NOBITS;
Rui Ueyama8f99f732017-04-05 03:20:42 +0000636 for (BaseCommand *Base : Opt.Commands) {
637 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
Rafael Espindola9546fff2016-09-22 14:40:50 +0000638 if (!Cmd)
639 continue;
George Rimar23e6a022017-03-14 11:31:28 +0000640 if (OutputSection *Sec = findSection(Cmd->Name, *OutputSections)) {
Rafael Espindola9b980092017-04-06 21:05:39 +0000641 Cmd->Sec = Sec;
Rafael Espindola2b074552017-02-03 22:27:05 +0000642 Flags = Sec->Flags;
643 Type = Sec->Type;
Rafael Espindola9546fff2016-09-22 14:40:50 +0000644 continue;
645 }
646
Rafael Espindola6a537372016-11-14 14:33:49 +0000647 if (isAllSectionDescription(*Cmd))
648 continue;
649
Rafael Espindola24e6f362017-02-24 15:07:30 +0000650 auto *OutSec = make<OutputSection>(Cmd->Name, Type, Flags);
Rafael Espindola9546fff2016-09-22 14:40:50 +0000651 OutputSections->push_back(OutSec);
Rafael Espindola9b980092017-04-06 21:05:39 +0000652 Cmd->Sec = OutSec;
Rafael Espindola9546fff2016-09-22 14:40:50 +0000653 }
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000654}
655
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000656void LinkerScript::adjustSectionsAfterSorting() {
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000657 placeOrphanSections();
658
Rafael Espindolafeed7502017-04-06 21:31:24 +0000659 // Try and find an appropriate memory region to assign offsets in.
Rafael Espindolad1960dc2017-04-06 21:40:22 +0000660 for (BaseCommand *Base : Opt.Commands) {
661 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) {
Rafael Espindolafeed7502017-04-06 21:31:24 +0000662 Cmd->MemRegion = findMemoryRegion(Cmd);
Rafael Espindolad1960dc2017-04-06 21:40:22 +0000663 // Handle align (e.g. ".foo : ALIGN(16) { ... }").
664 if (Cmd->AlignExpr)
665 Cmd->Sec->updateAlignment(Cmd->AlignExpr().getValue());
666 }
667 }
Rafael Espindolafeed7502017-04-06 21:31:24 +0000668
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000669 // If output section command doesn't specify any segments,
670 // and we haven't previously assigned any section to segment,
671 // then we simply assign section to the very first load segment.
672 // Below is an example of such linker script:
673 // PHDRS { seg PT_LOAD; }
674 // SECTIONS { .aaa : { *(.aaa) } }
675 std::vector<StringRef> DefPhdrs;
676 auto FirstPtLoad =
677 std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
678 [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
679 if (FirstPtLoad != Opt.PhdrsCommands.end())
680 DefPhdrs.push_back(FirstPtLoad->Name);
681
682 // Walk the commands and propagate the program headers to commands that don't
683 // explicitly specify them.
Rui Ueyama8f99f732017-04-05 03:20:42 +0000684 for (BaseCommand *Base : Opt.Commands) {
685 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000686 if (!Cmd)
687 continue;
Rui Ueyama8f99f732017-04-05 03:20:42 +0000688
Rafael Espindolaf7a17442016-11-14 15:39:38 +0000689 if (Cmd->Phdrs.empty())
690 Cmd->Phdrs = DefPhdrs;
691 else
692 DefPhdrs = Cmd->Phdrs;
693 }
Rafael Espindola6a537372016-11-14 14:33:49 +0000694
695 removeEmptyCommands();
Rafael Espindola9546fff2016-09-22 14:40:50 +0000696}
697
Rafael Espindola15c57952016-09-22 18:05:49 +0000698// When placing orphan sections, we want to place them after symbol assignments
699// so that an orphan after
700// begin_foo = .;
701// foo : { *(foo) }
702// end_foo = .;
703// doesn't break the intended meaning of the begin/end symbols.
704// We don't want to go over sections since Writer<ELFT>::sortSections is the
705// one in charge of deciding the order of the sections.
706// We don't want to go over alignments, since doing so in
707// rx_sec : { *(rx_sec) }
708// . = ALIGN(0x1000);
709// /* The RW PT_LOAD starts here*/
710// rw_sec : { *(rw_sec) }
711// would mean that the RW PT_LOAD would become unaligned.
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000712static bool shouldSkip(BaseCommand *Cmd) {
Rafael Espindola15c57952016-09-22 18:05:49 +0000713 if (isa<OutputSectionCommand>(Cmd))
714 return false;
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000715 if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd))
716 return Assign->Name != ".";
717 return true;
Rafael Espindola15c57952016-09-22 18:05:49 +0000718}
719
Rui Ueyama6697ec22017-02-02 23:26:12 +0000720// Orphan sections are sections present in the input files which are
721// not explicitly placed into the output file by the linker script.
722//
723// When the control reaches this function, Opt.Commands contains
724// output section commands for non-orphan sections only. This function
Rui Ueyama81cb7102017-03-24 00:15:57 +0000725// adds new elements for orphan sections so that all sections are
726// explicitly handled by Opt.Commands.
Rui Ueyama6697ec22017-02-02 23:26:12 +0000727//
728// Writer<ELFT>::sortSections has already sorted output sections.
729// What we need to do is to scan OutputSections vector and
730// Opt.Commands in parallel to find orphan sections. If there is an
731// output section that doesn't have a corresponding entry in
732// Opt.Commands, we will insert a new entry to Opt.Commands.
733//
734// There is some ambiguity as to where exactly a new entry should be
735// inserted, because Opt.Commands contains not only output section
Rui Ueyama81cb7102017-03-24 00:15:57 +0000736// commands but also other types of commands such as symbol assignment
Rui Ueyama6697ec22017-02-02 23:26:12 +0000737// expressions. There's no correct answer here due to the lack of the
738// formal specification of the linker script. We use heuristics to
739// determine whether a new output command should be added before or
740// after another commands. For the details, look at shouldSkip
741// function.
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000742void LinkerScript::placeOrphanSections() {
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000743 // The OutputSections are already in the correct order.
744 // This loops creates or moves commands as needed so that they are in the
745 // correct order.
746 int CmdIndex = 0;
Rafael Espindola5fcc99c2016-11-27 09:44:45 +0000747
748 // As a horrible special case, skip the first . assignment if it is before any
749 // section. We do this because it is common to set a load address by starting
750 // the script with ". = 0xabcd" and the expectation is that every section is
751 // after that.
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000752 auto FirstSectionOrDotAssignment =
753 std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
754 [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
Rafael Espindola5fcc99c2016-11-27 09:44:45 +0000755 if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
756 CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
757 if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
758 ++CmdIndex;
759 }
760
Rafael Espindola24e6f362017-02-24 15:07:30 +0000761 for (OutputSection *Sec : *OutputSections) {
Rafael Espindola40849412017-02-24 14:28:00 +0000762 StringRef Name = Sec->Name;
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000763
764 // Find the last spot where we can insert a command and still get the
Rafael Espindola15c57952016-09-22 18:05:49 +0000765 // correct result.
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000766 auto CmdIter = Opt.Commands.begin() + CmdIndex;
767 auto E = Opt.Commands.end();
Rui Ueyama4e1e88e2017-04-05 03:52:28 +0000768 while (CmdIter != E && shouldSkip(*CmdIter)) {
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000769 ++CmdIter;
770 ++CmdIndex;
771 }
772
Rui Ueyama8f99f732017-04-05 03:20:42 +0000773 auto Pos = std::find_if(CmdIter, E, [&](BaseCommand *Base) {
774 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
775 return Cmd && Cmd->Name == Name;
776 });
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000777 if (Pos == E) {
Rafael Espindola9b980092017-04-06 21:05:39 +0000778 auto *Cmd = make<OutputSectionCommand>(Name);
779 Cmd->Sec = Sec;
780 Opt.Commands.insert(CmdIter, Cmd);
Rafael Espindola15c57952016-09-22 18:05:49 +0000781 ++CmdIndex;
782 continue;
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000783 }
Rafael Espindola15c57952016-09-22 18:05:49 +0000784
785 // Continue from where we found it.
786 CmdIndex = (Pos - Opt.Commands.begin()) + 1;
George Rimar652852c2016-04-16 10:10:32 +0000787 }
Rafael Espindola337f9032016-11-14 14:13:32 +0000788}
789
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000790void LinkerScript::processNonSectionCommands() {
Rui Ueyama8f99f732017-04-05 03:20:42 +0000791 for (BaseCommand *Base : Opt.Commands) {
792 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base))
Rui Ueyamad379f732017-04-05 03:20:22 +0000793 assignSymbol(Cmd, false);
Rui Ueyama8f99f732017-04-05 03:20:42 +0000794 else if (auto *Cmd = dyn_cast<AssertCommand>(Base))
Petr Hosek02ad5162017-03-15 03:33:23 +0000795 Cmd->Expression();
796 }
797}
798
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000799void LinkerScript::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000800 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rafael Espindolabe607332016-09-30 00:16:11 +0000801 Dot = 0;
Rafael Espindola72dc1952017-03-17 13:05:04 +0000802 ErrorOnMissingSection = true;
Rafael Espindola06f47432017-02-06 22:21:46 +0000803 switchTo(Aether);
804
Rui Ueyama8f99f732017-04-05 03:20:42 +0000805 for (BaseCommand *Base : Opt.Commands) {
806 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
Rui Ueyamad379f732017-04-05 03:20:22 +0000807 assignSymbol(Cmd, false);
George Rimar652852c2016-04-16 10:10:32 +0000808 continue;
809 }
810
Rui Ueyama8f99f732017-04-05 03:20:42 +0000811 if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
Rafael Espindola4595df92017-03-10 16:04:26 +0000812 Cmd->Expression();
George Rimareefa7582016-08-04 09:29:31 +0000813 continue;
814 }
815
Rui Ueyama8f99f732017-04-05 03:20:42 +0000816 auto *Cmd = cast<OutputSectionCommand>(Base);
Rafael Espindolad3190792016-09-16 15:10:23 +0000817 assignOffsets(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000818 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000819
George Rimar0c1c8082017-03-14 10:00:19 +0000820 uint64_t MinVA = std::numeric_limits<uint64_t>::max();
Rafael Espindola24e6f362017-02-24 15:07:30 +0000821 for (OutputSection *Sec : *OutputSections) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000822 if (Sec->Flags & SHF_ALLOC)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000823 MinVA = std::min<uint64_t>(MinVA, Sec->Addr);
Rafael Espindolaea590d92017-02-08 15:19:03 +0000824 else
825 Sec->Addr = 0;
826 }
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000827
George Rimar2d262102017-03-14 09:03:53 +0000828 allocateHeaders(Phdrs, *OutputSections, MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000829}
830
Rui Ueyama464daad2016-08-22 04:55:20 +0000831// Creates program headers as instructed by PHDRS linker script command.
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000832std::vector<PhdrEntry> LinkerScript::createPhdrs() {
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000833 std::vector<PhdrEntry> Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000834
Rui Ueyama464daad2016-08-22 04:55:20 +0000835 // Process PHDRS and FILEHDR keywords because they are not
836 // real output sections and cannot be added in the following loop.
Eugene Leviantbbe38602016-07-19 09:25:43 +0000837 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000838 Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000839 PhdrEntry &Phdr = Ret.back();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000840
841 if (Cmd.HasFilehdr)
Rui Ueyama9d1bacb12017-02-27 02:31:26 +0000842 Phdr.add(Out::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000843 if (Cmd.HasPhdrs)
Rui Ueyama9d1bacb12017-02-27 02:31:26 +0000844 Phdr.add(Out::ProgramHeaders);
Eugene Leviant56b21c82016-09-09 09:46:16 +0000845
846 if (Cmd.LMAExpr) {
Rafael Espindola72dc1952017-03-17 13:05:04 +0000847 Phdr.p_paddr = Cmd.LMAExpr().getValue();
Eugene Leviant56b21c82016-09-09 09:46:16 +0000848 Phdr.HasLMA = true;
849 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000850 }
851
Rui Ueyama464daad2016-08-22 04:55:20 +0000852 // Add output sections to program headers.
Rafael Espindola24e6f362017-02-24 15:07:30 +0000853 for (OutputSection *Sec : *OutputSections) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000854 if (!(Sec->Flags & SHF_ALLOC))
Eugene Leviantbbe38602016-07-19 09:25:43 +0000855 break;
856
Eugene Leviantce30b1c2016-10-19 15:04:49 +0000857 // Assign headers specified by linker script
Rafael Espindola40849412017-02-24 14:28:00 +0000858 for (size_t Id : getPhdrIndices(Sec->Name)) {
Eugene Leviantce30b1c2016-10-19 15:04:49 +0000859 Ret[Id].add(Sec);
860 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000861 Ret[Id].p_flags |= Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000862 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000863 }
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000864 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000865}
866
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000867bool LinkerScript::ignoreInterpSection() {
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000868 // Ignore .interp section in case we have PHDRS specification
869 // and PT_INTERP isn't listed.
Rui Ueyamae31d9882017-04-05 05:06:17 +0000870 if (Opt.PhdrsCommands.empty())
871 return false;
872 for (PhdrsCommand &Cmd : Opt.PhdrsCommands)
873 if (Cmd.Type == PT_INTERP)
874 return false;
875 return true;
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000876}
877
James Henderson9d9a6632017-04-07 10:36:42 +0000878Optional<uint32_t> LinkerScript::getFiller(StringRef Name) {
Rui Ueyama8f99f732017-04-05 03:20:42 +0000879 for (BaseCommand *Base : Opt.Commands)
880 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
George Rimarf6c3cce2016-07-21 07:48:54 +0000881 if (Cmd->Name == Name)
882 return Cmd->Filler;
James Henderson9d9a6632017-04-07 10:36:42 +0000883 return None;
George Rimare2ee72b2016-02-26 14:48:31 +0000884}
885
George Rimare38cbab2016-09-26 19:22:50 +0000886static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
Rui Ueyamaf62d2602017-04-05 05:06:37 +0000887 if (Size == 1)
888 *Buf = Data;
889 else if (Size == 2)
Rui Ueyamaf93ed4d2017-03-21 21:40:08 +0000890 write16(Buf, Data, Config->Endianness);
Rui Ueyamaf62d2602017-04-05 05:06:37 +0000891 else if (Size == 4)
Rui Ueyamaf93ed4d2017-03-21 21:40:08 +0000892 write32(Buf, Data, Config->Endianness);
Rui Ueyamaf62d2602017-04-05 05:06:37 +0000893 else if (Size == 8)
Rui Ueyamaf93ed4d2017-03-21 21:40:08 +0000894 write64(Buf, Data, Config->Endianness);
Rui Ueyamaf62d2602017-04-05 05:06:37 +0000895 else
George Rimare38cbab2016-09-26 19:22:50 +0000896 llvm_unreachable("unsupported Size argument");
George Rimare38cbab2016-09-26 19:22:50 +0000897}
898
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000899void LinkerScript::writeDataBytes(StringRef Name, uint8_t *Buf) {
George Rimare38cbab2016-09-26 19:22:50 +0000900 int I = getSectionIndex(Name);
901 if (I == INT_MAX)
902 return;
903
Rui Ueyama8f99f732017-04-05 03:20:42 +0000904 auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]);
905 for (BaseCommand *Base : Cmd->Commands)
906 if (auto *Data = dyn_cast<BytesDataCommand>(Base))
George Rimara8dba482017-03-20 10:09:58 +0000907 writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
George Rimare38cbab2016-09-26 19:22:50 +0000908}
909
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000910bool LinkerScript::hasLMA(StringRef Name) {
Rui Ueyama8f99f732017-04-05 03:20:42 +0000911 for (BaseCommand *Base : Opt.Commands)
912 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000913 if (Cmd->LMAExpr && Cmd->Name == Name)
914 return true;
915 return false;
George Rimar8ceadb32016-08-17 07:44:19 +0000916}
917
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000918// Returns the index of the given section name in linker script
919// SECTIONS commands. Sections are laid out as the same order as they
920// were in the script. If a given name did not appear in the script,
921// it returns INT_MAX, so that it will be laid out at end of file.
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000922int LinkerScript::getSectionIndex(StringRef Name) {
Rui Ueyama6e68c5e2016-11-19 18:05:58 +0000923 for (int I = 0, E = Opt.Commands.size(); I != E; ++I)
Rui Ueyama8f99f732017-04-05 03:20:42 +0000924 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]))
Rui Ueyamaf510fa62016-07-26 00:21:15 +0000925 if (Cmd->Name == Name)
926 return I;
Rui Ueyamaf510fa62016-07-26 00:21:15 +0000927 return INT_MAX;
George Rimar71b26e92016-04-21 10:22:02 +0000928}
929
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000930ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
Rafael Espindola4595df92017-03-10 16:04:26 +0000931 if (S == ".")
Rafael Espindola72dc1952017-03-17 13:05:04 +0000932 return {CurOutSec, Dot - CurOutSec->Addr};
George Rimara8dba482017-03-20 10:09:58 +0000933 if (SymbolBody *B = findSymbol(S)) {
Rafael Espindola72dc1952017-03-17 13:05:04 +0000934 if (auto *D = dyn_cast<DefinedRegular>(B))
935 return {D->Section, D->Value};
Petr Hosek30f16b22017-03-23 03:52:34 +0000936 if (auto *C = dyn_cast<DefinedCommon>(B))
937 return {InX::Common, C->Offset};
Rafael Espindola72dc1952017-03-17 13:05:04 +0000938 }
Eugene Leviantf6aeed32016-12-22 13:13:12 +0000939 error(Loc + ": symbol not found: " + S);
George Rimar884e7862016-09-08 08:19:13 +0000940 return 0;
941}
942
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000943bool LinkerScript::isDefined(StringRef S) { return findSymbol(S) != nullptr; }
George Rimarf34f45f2016-09-23 13:17:23 +0000944
Eugene Leviantbbe38602016-07-19 09:25:43 +0000945// Returns indices of ELF headers containing specific section, identified
946// by Name. Each index is a zero based number of ELF header listed within
947// PHDRS {} script block.
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000948std::vector<size_t> LinkerScript::getPhdrIndices(StringRef SectionName) {
Rui Ueyama8f99f732017-04-05 03:20:42 +0000949 for (BaseCommand *Base : Opt.Commands) {
950 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000951 if (!Cmd || Cmd->Name != SectionName)
George Rimar31d842f2016-07-20 16:43:03 +0000952 continue;
953
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000954 std::vector<size_t> Ret;
955 for (StringRef PhdrName : Cmd->Phdrs)
Eugene Leviant2a942c42016-12-05 16:38:32 +0000956 Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName));
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000957 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000958 }
George Rimar31d842f2016-07-20 16:43:03 +0000959 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000960}
961
Rui Ueyamab8dd23f2017-03-21 23:02:51 +0000962size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000963 size_t I = 0;
964 for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
965 if (Cmd.Name == PhdrName)
966 return I;
967 ++I;
968 }
Eugene Leviant2a942c42016-12-05 16:38:32 +0000969 error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000970 return 0;
971}