Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/LinkerScript.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LinkerScript.cpp ---------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains the parser/evaluator of the linker script.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "LinkerScript.h"
14
#include "Config.h"
15
#include "InputSection.h"
16
#include "OutputSections.h"
17
#include "SymbolTable.h"
18
#include "Symbols.h"
19
#include "SyntheticSections.h"
20
#include "Target.h"
21
#include "Writer.h"
22
#include "lld/Common/Memory.h"
23
#include "lld/Common/Strings.h"
24
#include "lld/Common/Threads.h"
25
#include "llvm/ADT/STLExtras.h"
26
#include "llvm/ADT/StringRef.h"
27
#include "llvm/BinaryFormat/ELF.h"
28
#include "llvm/Support/Casting.h"
29
#include "llvm/Support/Endian.h"
30
#include "llvm/Support/ErrorHandling.h"
31
#include "llvm/Support/FileSystem.h"
32
#include "llvm/Support/Path.h"
33
#include <algorithm>
34
#include <cassert>
35
#include <cstddef>
36
#include <cstdint>
37
#include <iterator>
38
#include <limits>
39
#include <string>
40
#include <vector>
41
42
using namespace llvm;
43
using namespace llvm::ELF;
44
using namespace llvm::object;
45
using namespace llvm::support::endian;
46
using namespace lld;
47
using namespace lld::elf;
48
49
LinkerScript *elf::Script;
50
51
1.04k
static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) {
52
1.04k
  if (OutputSection *OS = InputSec->getOutputSection())
53
1.03k
    return OS->Addr;
54
2
  error(Loc + ": unable to evaluate expression: input section " +
55
2
        InputSec->Name + " has no output section assigned");
56
2
  return 0;
57
2
}
58
59
10.3k
uint64_t ExprValue::getValue() const {
60
10.3k
  if (Sec)
61
587
    return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc),
62
587
                   Alignment);
63
9.79k
  return alignTo(Val, Alignment);
64
9.79k
}
65
66
483
uint64_t ExprValue::getSecAddr() const {
67
483
  if (Sec)
68
454
    return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc);
69
29
  return 0;
70
29
}
71
72
559
uint64_t ExprValue::getSectionOffset() const {
73
559
  // If the alignment is trivial, we don't have to compute the full
74
559
  // value to know the offset. This allows this function to succeed in
75
559
  // cases where the output section is not yet known.
76
559
  if (Alignment == 1 && 
(534
!Sec534
||
!Sec->getOutputSection()421
))
77
117
    return Val;
78
442
  return getValue() - getSecAddr();
79
442
}
80
81
OutputSection *LinkerScript::createOutputSection(StringRef Name,
82
374k
                                                 StringRef Location) {
83
374k
  OutputSection *&SecRef = NameToOutputSection[Name];
84
374k
  OutputSection *Sec;
85
374k
  if (SecRef && 
SecRef->Location.empty()15
) {
86
6
    // There was a forward reference.
87
6
    Sec = SecRef;
88
374k
  } else {
89
374k
    Sec = make<OutputSection>(Name, SHT_PROGBITS, 0);
90
374k
    if (!SecRef)
91
374k
      SecRef = Sec;
92
374k
  }
93
374k
  Sec->Location = Location;
94
374k
  return Sec;
95
374k
}
96
97
54
OutputSection *LinkerScript::getOrCreateOutputSection(StringRef Name) {
98
54
  OutputSection *&CmdRef = NameToOutputSection[Name];
99
54
  if (!CmdRef)
100
9
    CmdRef = make<OutputSection>(Name, SHT_PROGBITS, 0);
101
54
  return CmdRef;
102
54
}
103
104
// Expands the memory region by the specified size.
105
static void expandMemoryRegion(MemoryRegion *MemRegion, uint64_t Size,
106
379
                               StringRef RegionName, StringRef SecName) {
107
379
  MemRegion->CurPos += Size;
108
379
  uint64_t NewSize = MemRegion->CurPos - MemRegion->Origin;
109
379
  if (NewSize > MemRegion->Length)
110
3
    error("section '" + SecName + "' will not fit in region '" + RegionName +
111
3
          "': overflowed by " + Twine(NewSize - MemRegion->Length) + " bytes");
112
379
}
113
114
1.42M
void LinkerScript::expandMemoryRegions(uint64_t Size) {
115
1.42M
  if (Ctx->MemRegion)
116
324
    expandMemoryRegion(Ctx->MemRegion, Size, Ctx->MemRegion->Name,
117
324
                       Ctx->OutSec->Name);
118
1.42M
  // Only expand the LMARegion if it is different from MemRegion.
119
1.42M
  if (Ctx->LMARegion && 
Ctx->MemRegion != Ctx->LMARegion59
)
120
55
    expandMemoryRegion(Ctx->LMARegion, Size, Ctx->LMARegion->Name,
121
55
                       Ctx->OutSec->Name);
122
1.42M
}
123
124
708k
void LinkerScript::expandOutputSection(uint64_t Size) {
125
708k
  Ctx->OutSec->Size += Size;
126
708k
  expandMemoryRegions(Size);
127
708k
}
128
129
8.17k
void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
130
8.17k
  uint64_t Val = E().getValue();
131
8.17k
  if (Val < Dot && 
InSec20
)
132
2
    error(Loc + ": unable to move location counter backward for: " +
133
2
          Ctx->OutSec->Name);
134
8.17k
135
8.17k
  // Update to location counter means update to section size.
136
8.17k
  if (InSec)
137
55
    expandOutputSection(Val - Dot);
138
8.12k
  else if (Val > Dot)
139
7.17k
    expandMemoryRegions(Val - Dot);
140
8.17k
141
8.17k
  Dot = Val;
142
8.17k
}
143
144
// Used for handling linker symbol assignments, for both finalizing
145
// their values and doing early declarations. Returns true if symbol
146
// should be defined from linker script.
147
1.08k
static bool shouldDefineSym(SymbolAssignment *Cmd) {
148
1.08k
  if (Cmd->Name == ".")
149
378
    return false;
150
711
151
711
  if (!Cmd->Provide)
152
652
    return true;
153
59
154
59
  // If a symbol was in PROVIDE(), we need to define it only
155
59
  // when it is a referenced undefined symbol.
156
59
  Symbol *B = Symtab->find(Cmd->Name);
157
59
  if (B && 
!B->isDefined()41
)
158
33
    return true;
159
26
  return false;
160
26
}
161
162
// This function is called from processSectionCommands,
163
// while we are fixing the output section layout.
164
545
void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
165
545
  if (!shouldDefineSym(Cmd))
166
202
    return;
167
343
168
343
  // Define a symbol.
169
343
  ExprValue Value = Cmd->Expression();
170
343
  SectionBase *Sec = Value.isAbsolute() ? 
nullptr205
:
Value.Sec138
;
171
343
  uint8_t Visibility = Cmd->Hidden ? 
STV_HIDDEN23
:
STV_DEFAULT320
;
172
343
173
343
  // When this function is called, section addresses have not been
174
343
  // fixed yet. So, we may or may not know the value of the RHS
175
343
  // expression.
176
343
  //
177
343
  // For example, if an expression is `x = 42`, we know x is always 42.
178
343
  // However, if an expression is `x = .`, there's no way to know its
179
343
  // value at the moment.
180
343
  //
181
343
  // We want to set symbol values early if we can. This allows us to
182
343
  // use symbols as variables in linker scripts. Doing so allows us to
183
343
  // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
184
343
  uint64_t SymValue = Value.Sec ? 
0147
:
Value.getValue()196
;
185
343
186
343
  Defined New(nullptr, Cmd->Name, STB_GLOBAL, Visibility, STT_NOTYPE, SymValue,
187
343
              0, Sec);
188
343
189
343
  Symbol *Sym = Symtab->insert(Cmd->Name);
190
343
  mergeSymbolProperties(Sym, New);
191
343
  replaceSymbol(Sym, New);
192
343
  Cmd->Sym = cast<Defined>(Sym);
193
343
}
194
195
// This function is called from LinkerScript::declareSymbols.
196
// It creates a placeholder symbol if needed.
197
544
static void declareSymbol(SymbolAssignment *Cmd) {
198
544
  if (!shouldDefineSym(Cmd))
199
202
    return;
200
342
201
342
  uint8_t Visibility = Cmd->Hidden ? 
STV_HIDDEN23
:
STV_DEFAULT319
;
202
342
  Defined New(nullptr, Cmd->Name, STB_GLOBAL, Visibility, STT_NOTYPE, 0, 0,
203
342
              nullptr);
204
342
205
342
  // We can't calculate final value right now.
206
342
  Symbol *Sym = Symtab->insert(Cmd->Name);
207
342
  mergeSymbolProperties(Sym, New);
208
342
  replaceSymbol(Sym, New);
209
342
210
342
  Cmd->Sym = cast<Defined>(Sym);
211
342
  Cmd->Provide = false;
212
342
  Sym->ScriptDefined = true;
213
342
}
214
215
// This method is used to handle INSERT AFTER statement. Here we rebuild
216
// the list of script commands to mix sections inserted into.
217
2.55k
void LinkerScript::processInsertCommands() {
218
2.55k
  std::vector<BaseCommand *> V;
219
2.55k
  auto Insert = [&](std::vector<BaseCommand *> &From) {
220
1.50k
    V.insert(V.end(), From.begin(), From.end());
221
1.50k
    From.clear();
222
1.50k
  };
223
2.55k
224
2.55k
  for (BaseCommand *Base : SectionCommands) {
225
1.16k
    if (auto *OS = dyn_cast<OutputSection>(Base)) {
226
751
      Insert(InsertBeforeCommands[OS->Name]);
227
751
      V.push_back(Base);
228
751
      Insert(InsertAfterCommands[OS->Name]);
229
751
      continue;
230
751
    }
231
417
    V.push_back(Base);
232
417
  }
233
2.55k
234
2.55k
  for (auto &Cmds : {InsertBeforeCommands, InsertAfterCommands})
235
5.11k
    for (const std::pair<StringRef, std::vector<BaseCommand *>> &P : Cmds)
236
1.49k
      if (!P.second.empty())
237
4
        error("unable to INSERT AFTER/BEFORE " + P.first +
238
4
              ": section not defined");
239
2.55k
240
2.55k
  SectionCommands = std::move(V);
241
2.55k
}
242
243
// Symbols defined in script should not be inlined by LTO. At the same time
244
// we don't know their final values until late stages of link. Here we scan
245
// over symbol assignment commands and create placeholder symbols if needed.
246
2.55k
void LinkerScript::declareSymbols() {
247
2.55k
  assert(!Ctx);
248
2.55k
  for (BaseCommand *Base : SectionCommands) {
249
1.17k
    if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
250
417
      declareSymbol(Cmd);
251
417
      continue;
252
417
    }
253
755
254
755
    // If the output section directive has constraints,
255
755
    // we can't say for sure if it is going to be included or not.
256
755
    // Skip such sections for now. Improve the checks if we ever
257
755
    // need symbols from that sections to be declared early.
258
755
    auto *Sec = cast<OutputSection>(Base);
259
755
    if (Sec->Constraint != ConstraintKind::NoConstraint)
260
14
      continue;
261
741
    for (BaseCommand *Base2 : Sec->SectionCommands)
262
955
      if (auto *Cmd = dyn_cast<SymbolAssignment>(Base2))
263
127
        declareSymbol(Cmd);
264
741
  }
265
2.55k
}
266
267
// This function is called from assignAddresses, while we are
268
// fixing the output section addresses. This function is supposed
269
// to set the final value for a given symbol assignment.
270
1.06k
void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
271
1.06k
  if (Cmd->Name == ".") {
272
376
    setDot(Cmd->Expression, Cmd->Location, InSec);
273
376
    return;
274
376
  }
275
693
276
693
  if (!Cmd->Sym)
277
20
    return;
278
673
279
673
  ExprValue V = Cmd->Expression();
280
673
  if (V.isAbsolute()) {
281
397
    Cmd->Sym->Section = nullptr;
282
397
    Cmd->Sym->Value = V.getValue();
283
397
  } else {
284
276
    Cmd->Sym->Section = V.Sec;
285
276
    Cmd->Sym->Value = V.getSectionOffset();
286
276
  }
287
673
}
288
289
147k
static std::string getFilename(InputFile *File) {
290
147k
  if (!File)
291
13.7k
    return "";
292
133k
  if (File->ArchiveName.empty())
293
133k
    return File->getName();
294
28
  return (File->ArchiveName + "(" + File->getName() + ")").str();
295
28
}
296
297
413
bool LinkerScript::shouldKeep(InputSectionBase *S) {
298
413
  if (KeptSections.empty())
299
395
    return false;
300
18
  std::string Filename = getFilename(S->File);
301
18
  for (InputSectionDescription *ID : KeptSections)
302
18
    if (ID->FilePat.match(Filename))
303
14
      for (SectionPattern &P : ID->SectionPatterns)
304
14
        if (P.SectionPat.match(S->Name))
305
4
          return true;
306
18
  
return false14
;
307
18
}
308
309
// A helper function for the SORT() command.
310
static std::function<bool(InputSectionBase *, InputSectionBase *)>
311
22
getComparator(SortSectionPolicy K) {
312
22
  switch (K) {
313
22
  case SortSectionPolicy::Alignment:
314
115
    return [](InputSectionBase *A, InputSectionBase *B) {
315
115
      // ">" is not a mistake. Sections with larger alignments are placed
316
115
      // before sections with smaller alignments in order to reduce the
317
115
      // amount of padding necessary. This is compatible with GNU.
318
115
      return A->Alignment > B->Alignment;
319
115
    };
320
22
  case SortSectionPolicy::Name:
321
698
    return [](InputSectionBase *A, InputSectionBase *B) {
322
698
      return A->Name < B->Name;
323
698
    };
324
22
  case SortSectionPolicy::Priority:
325
3
    return [](InputSectionBase *A, InputSectionBase *B) {
326
3
      return getPriority(A->Name) < getPriority(B->Name);
327
3
    };
328
22
  default:
329
0
    llvm_unreachable("unknown sort policy");
330
22
  }
331
22
}
332
333
// A helper function for the SORT() command.
334
static bool matchConstraints(ArrayRef<InputSection *> Sections,
335
735
                             ConstraintKind Kind) {
336
735
  if (Kind == ConstraintKind::NoConstraint)
337
721
    return true;
338
14
339
14
  bool IsRW = llvm::any_of(
340
15
      Sections, [](InputSection *Sec) { return Sec->Flags & SHF_WRITE; });
341
14
342
14
  return (IsRW && 
Kind == ConstraintKind::ReadWrite11
) ||
343
14
         
(9
!IsRW9
&&
Kind == ConstraintKind::ReadOnly3
);
344
14
}
345
346
static void sortSections(MutableArrayRef<InputSection *> Vec,
347
1.59k
                         SortSectionPolicy K) {
348
1.59k
  if (K != SortSectionPolicy::Default && 
K != SortSectionPolicy::None23
)
349
22
    llvm::stable_sort(Vec, getComparator(K));
350
1.59k
}
351
352
// Sort sections as instructed by SORT-family commands and --sort-section
353
// option. Because SORT-family commands can be nested at most two depth
354
// (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
355
// line option is respected even if a SORT command is given, the exact
356
// behavior we have here is a bit complicated. Here are the rules.
357
//
358
// 1. If two SORT commands are given, --sort-section is ignored.
359
// 2. If one SORT command is given, and if it is not SORT_NONE,
360
//    --sort-section is handled as an inner SORT command.
361
// 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
362
// 4. If no SORT command is given, sort according to --sort-section.
363
static void sortInputSections(MutableArrayRef<InputSection *> Vec,
364
801
                              const SectionPattern &Pat) {
365
801
  if (Pat.SortOuter == SortSectionPolicy::None)
366
2
    return;
367
799
368
799
  if (Pat.SortInner == SortSectionPolicy::Default)
369
796
    sortSections(Vec, Config->SortSection);
370
3
  else
371
3
    sortSections(Vec, Pat.SortInner);
372
799
  sortSections(Vec, Pat.SortOuter);
373
799
}
374
375
// Compute and remember which sections the InputSectionDescription matches.
376
std::vector<InputSection *>
377
796
LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
378
796
  std::vector<InputSection *> Ret;
379
796
380
796
  // Collects all sections that satisfy constraints of Cmd.
381
801
  for (const SectionPattern &Pat : Cmd->SectionPatterns) {
382
801
    size_t SizeBefore = Ret.size();
383
801
384
148k
    for (InputSectionBase *Sec : InputSections) {
385
148k
      if (!Sec->Live || 
Sec->Assigned148k
)
386
1.19k
        continue;
387
147k
388
147k
      // For -emit-relocs we have to ignore entries like
389
147k
      //   .rela.dyn : { *(.rela.data) }
390
147k
      // which are common because they are in the default bfd script.
391
147k
      // We do not ignore SHT_REL[A] linker-synthesized sections here because
392
147k
      // want to support scripts that do custom layout for them.
393
147k
      if (auto *IS = dyn_cast<InputSection>(Sec))
394
147k
        if (IS->getRelocatedSection())
395
14
          continue;
396
147k
397
147k
      std::string Filename = getFilename(Sec->File);
398
147k
      if (!Cmd->FilePat.match(Filename) ||
399
147k
          
Pat.ExcludedFilePat.match(Filename)146k
||
400
147k
          
!Pat.SectionPat.match(Sec->Name)146k
)
401
145k
        continue;
402
1.11k
403
1.11k
      // It is safe to assume that Sec is an InputSection
404
1.11k
      // because mergeable or EH input sections have already been
405
1.11k
      // handled and eliminated.
406
1.11k
      Ret.push_back(cast<InputSection>(Sec));
407
1.11k
      Sec->Assigned = true;
408
1.11k
    }
409
801
410
801
    sortInputSections(MutableArrayRef<InputSection *>(Ret).slice(SizeBefore),
411
801
                      Pat);
412
801
  }
413
796
  return Ret;
414
796
}
415
416
46
void LinkerScript::discard(ArrayRef<InputSection *> V) {
417
46
  for (InputSection *S : V) {
418
26
    if (S == In.ShStrTab || 
S == In.RelaDyn25
||
S == In.RelrDyn24
)
419
2
      error("discarding " + S->Name + " section is not allowed");
420
26
421
26
    // You can discard .hash and .gnu.hash sections by linker scripts. Since
422
26
    // they are synthesized sections, we need to handle them differently than
423
26
    // other regular sections.
424
26
    if (S == In.GnuHashTab)
425
1
      In.GnuHashTab = nullptr;
426
26
    if (S == In.HashTab)
427
1
      In.HashTab = nullptr;
428
26
429
26
    S->Assigned = false;
430
26
    S->Live = false;
431
26
    discard(S->DependentSections);
432
26
  }
433
46
}
434
435
std::vector<InputSection *>
436
755
LinkerScript::createInputSectionList(OutputSection &OutCmd) {
437
755
  std::vector<InputSection *> Ret;
438
755
439
973
  for (BaseCommand *Base : OutCmd.SectionCommands) {
440
973
    if (auto *Cmd = dyn_cast<InputSectionDescription>(Base)) {
441
796
      Cmd->Sections = computeInputSections(Cmd);
442
796
      Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
443
796
    }
444
973
  }
445
755
  return Ret;
446
755
}
447
448
2.52k
void LinkerScript::processSectionCommands() {
449
2.52k
  // A symbol can be assigned before any section is mentioned in the linker
450
2.52k
  // script. In an DSO, the symbol values are addresses, so the only important
451
2.52k
  // section values are:
452
2.52k
  // * SHN_UNDEF
453
2.52k
  // * SHN_ABS
454
2.52k
  // * Any value meaning a regular section.
455
2.52k
  // To handle that, create a dummy aether section that fills the void before
456
2.52k
  // the linker scripts switches to another section. It has an index of one
457
2.52k
  // which will map to whatever the first actual section is.
458
2.52k
  Aether = make<OutputSection>("", 0, SHF_ALLOC);
459
2.52k
  Aether->SectionIndex = 1;
460
2.52k
461
2.52k
  // Ctx captures the local AddressState and makes it accessible deliberately.
462
2.52k
  // This is needed as there are some cases where we cannot just
463
2.52k
  // thread the current state through to a lambda function created by the
464
2.52k
  // script parser.
465
2.52k
  auto Deleter = make_unique<AddressState>();
466
2.52k
  Ctx = Deleter.get();
467
2.52k
  Ctx->OutSec = Aether;
468
2.52k
469
2.52k
  size_t I = 0;
470
2.52k
  // Add input sections to output sections.
471
2.52k
  for (BaseCommand *Base : SectionCommands) {
472
1.17k
    // Handle symbol assignments outside of any output section.
473
1.17k
    if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
474
417
      addSymbol(Cmd);
475
417
      continue;
476
417
    }
477
755
478
755
    if (auto *Sec = dyn_cast<OutputSection>(Base)) {
479
755
      std::vector<InputSection *> V = createInputSectionList(*Sec);
480
755
481
755
      // The output section name `/DISCARD/' is special.
482
755
      // Any input section assigned to it is discarded.
483
755
      if (Sec->Name == "/DISCARD/") {
484
20
        discard(V);
485
20
        Sec->SectionCommands.clear();
486
20
        Sec->SectionIndex = 0; // Not an orphan.
487
20
        continue;
488
20
      }
489
735
490
735
      // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
491
735
      // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
492
735
      // sections satisfy a given constraint. If not, a directive is handled
493
735
      // as if it wasn't present from the beginning.
494
735
      //
495
735
      // Because we'll iterate over SectionCommands many more times, the easy
496
735
      // way to "make it as if it wasn't present" is to make it empty.
497
735
      if (!matchConstraints(V, Sec->Constraint)) {
498
6
        for (InputSectionBase *S : V)
499
10
          S->Assigned = false;
500
6
        Sec->SectionCommands.clear();
501
6
        continue;
502
6
      }
503
729
504
729
      // A directive may contain symbol definitions like this:
505
729
      // ".foo : { ...; bar = .; }". Handle them.
506
729
      for (BaseCommand *Base : Sec->SectionCommands)
507
942
        if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
508
128
          addSymbol(OutCmd);
509
729
510
729
      // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
511
729
      // is given, input sections are aligned to that value, whether the
512
729
      // given value is larger or smaller than the original section alignment.
513
729
      if (Sec->SubalignExpr) {
514
5
        uint32_t Subalign = Sec->SubalignExpr().getValue();
515
5
        for (InputSectionBase *S : V)
516
16
          S->Alignment = Subalign;
517
5
      }
518
729
519
729
      // Add input sections to an output section.
520
729
      for (InputSection *S : V)
521
1.08k
        Sec->addSection(S);
522
729
523
729
      Sec->SectionIndex = I++;
524
729
      if (Sec->Noload)
525
3
        Sec->Type = SHT_NOBITS;
526
729
      if (Sec->NonAlloc)
527
5
        Sec->Flags &= ~(uint64_t)SHF_ALLOC;
528
729
    }
529
755
  }
530
2.52k
  Ctx = nullptr;
531
2.52k
}
532
533
static OutputSection *findByName(ArrayRef<BaseCommand *> Vec,
534
383k
                                 StringRef Name) {
535
383k
  for (BaseCommand *Base : Vec)
536
282k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
537
144k
      if (Sec->Name == Name)
538
65
        return Sec;
539
383k
  
return nullptr382k
;
540
383k
}
541
542
static OutputSection *createSection(InputSectionBase *IS,
543
373k
                                    StringRef OutsecName) {
544
373k
  OutputSection *Sec = Script->createOutputSection(OutsecName, "<internal>");
545
373k
  Sec->addSection(cast<InputSection>(IS));
546
373k
  return Sec;
547
373k
}
548
549
static OutputSection *addInputSec(StringMap<OutputSection *> &Map,
550
382k
                                  InputSectionBase *IS, StringRef OutsecName) {
551
382k
  // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
552
382k
  // option is given. A section with SHT_GROUP defines a "section group", and
553
382k
  // its members have SHF_GROUP attribute. Usually these flags have already been
554
382k
  // stripped by InputFiles.cpp as section groups are processed and uniquified.
555
382k
  // However, for the -r option, we want to pass through all section groups
556
382k
  // as-is because adding/removing members or merging them with other groups
557
382k
  // change their semantics.
558
382k
  if (IS->Type == SHT_GROUP || 
(IS->Flags & SHF_GROUP)382k
)
559
25
    return createSection(IS, OutsecName);
560
382k
561
382k
  // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
562
382k
  // relocation sections .rela.foo and .rela.bar for example. Most tools do
563
382k
  // not allow multiple REL[A] sections for output section. Hence we
564
382k
  // should combine these relocation sections into single output.
565
382k
  // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
566
382k
  // other REL[A] sections created by linker itself.
567
382k
  if (!isa<SyntheticSection>(IS) &&
568
382k
      
(334k
IS->Type == SHT_REL334k
||
IS->Type == SHT_RELA334k
)) {
569
104
    auto *Sec = cast<InputSection>(IS);
570
104
    OutputSection *Out = Sec->getRelocatedSection()->getOutputSection();
571
104
572
104
    if (Out->RelocationSection) {
573
27
      Out->RelocationSection->addSection(Sec);
574
27
      return nullptr;
575
27
    }
576
77
577
77
    Out->RelocationSection = createSection(IS, OutsecName);
578
77
    return Out->RelocationSection;
579
77
  }
580
382k
581
382k
  // When control reaches here, mergeable sections have already been merged into
582
382k
  // synthetic sections. For relocatable case we want to create one output
583
382k
  // section per syntetic section so that they have a valid sh_entsize.
584
382k
  if (Config->Relocatable && 
(IS->Flags & SHF_MERGE)66.9k
)
585
8
    return createSection(IS, OutsecName);
586
382k
587
382k
  //  The ELF spec just says
588
382k
  // ----------------------------------------------------------------
589
382k
  // In the first phase, input sections that match in name, type and
590
382k
  // attribute flags should be concatenated into single sections.
591
382k
  // ----------------------------------------------------------------
592
382k
  //
593
382k
  // However, it is clear that at least some flags have to be ignored for
594
382k
  // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
595
382k
  // ignored. We should not have two output .text sections just because one was
596
382k
  // in a group and another was not for example.
597
382k
  //
598
382k
  // It also seems that wording was a late addition and didn't get the
599
382k
  // necessary scrutiny.
600
382k
  //
601
382k
  // Merging sections with different flags is expected by some users. One
602
382k
  // reason is that if one file has
603
382k
  //
604
382k
  // int *const bar __attribute__((section(".foo"))) = (int *)0;
605
382k
  //
606
382k
  // gcc with -fPIC will produce a read only .foo section. But if another
607
382k
  // file has
608
382k
  //
609
382k
  // int zed;
610
382k
  // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
611
382k
  //
612
382k
  // gcc with -fPIC will produce a read write section.
613
382k
  //
614
382k
  // Last but not least, when using linker script the merge rules are forced by
615
382k
  // the script. Unfortunately, linker scripts are name based. This means that
616
382k
  // expressions like *(.foo*) can refer to multiple input sections with
617
382k
  // different flags. We cannot put them in different output sections or we
618
382k
  // would produce wrong results for
619
382k
  //
620
382k
  // start = .; *(.foo.*) end = .; *(.bar)
621
382k
  //
622
382k
  // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
623
382k
  // another. The problem is that there is no way to layout those output
624
382k
  // sections such that the .foo sections are the only thing between the start
625
382k
  // and end symbols.
626
382k
  //
627
382k
  // Given the above issues, we instead merge sections by name and error on
628
382k
  // incompatible types and flags.
629
382k
  OutputSection *&Sec = Map[OutsecName];
630
382k
  if (Sec) {
631
9.54k
    Sec->addSection(cast<InputSection>(IS));
632
9.54k
    return nullptr;
633
9.54k
  }
634
373k
635
373k
  Sec = createSection(IS, OutsecName);
636
373k
  return Sec;
637
373k
}
638
639
// Add sections that didn't match any sections command.
640
2.52k
void LinkerScript::addOrphanSections() {
641
2.52k
  StringMap<OutputSection *> Map;
642
2.52k
  std::vector<OutputSection *> V;
643
2.52k
644
384k
  auto Add = [&](InputSectionBase *S) {
645
384k
    if (!S->Live || 
S->Parent384k
)
646
1.83k
      return;
647
383k
648
383k
    StringRef Name = getOutputSectionName(S);
649
383k
650
383k
    if (Config->OrphanHandling == OrphanHandlingPolicy::Error)
651
25
      error(toString(S) + " is being placed in '" + Name + "'");
652
383k
    else if (Config->OrphanHandling == OrphanHandlingPolicy::Warn)
653
25
      warn(toString(S) + " is being placed in '" + Name + "'");
654
383k
655
383k
    if (OutputSection *Sec = findByName(SectionCommands, Name)) {
656
65
      Sec->addSection(cast<InputSection>(S));
657
65
      return;
658
65
    }
659
382k
660
382k
    if (OutputSection *OS = addInputSec(Map, S, Name))
661
373k
      V.push_back(OS);
662
382k
    assert(S->getOutputSection()->SectionIndex == UINT32_MAX);
663
382k
  };
664
2.52k
665
2.52k
  // For futher --emit-reloc handling code we need target output section
666
2.52k
  // to be created before we create relocation output section, so we want
667
2.52k
  // to create target sections first. We do not want priority handling
668
2.52k
  // for synthetic sections because them are special.
669
384k
  for (InputSectionBase *IS : InputSections) {
670
384k
    if (auto *Sec = dyn_cast<InputSection>(IS))
671
384k
      if (InputSectionBase *Rel = Sec->getRelocatedSection())
672
114
        if (auto *RelIS = dyn_cast_or_null<InputSectionBase>(Rel->Parent))
673
3
          Add(RelIS);
674
384k
    Add(IS);
675
384k
  }
676
2.52k
677
2.52k
  // If no SECTIONS command was given, we should insert sections commands
678
2.52k
  // before others, so that we can handle scripts which refers them,
679
2.52k
  // for example: "foo = ABSOLUTE(ADDR(.text)));".
680
2.52k
  // When SECTIONS command is present we just add all orphans to the end.
681
2.52k
  if (HasSectionsCommand)
682
458
    SectionCommands.insert(SectionCommands.end(), V.begin(), V.end());
683
2.06k
  else
684
2.06k
    SectionCommands.insert(SectionCommands.begin(), V.begin(), V.end());
685
2.52k
}
686
687
2.83M
uint64_t LinkerScript::advance(uint64_t Size, unsigned Alignment) {
688
2.83M
  bool IsTbss =
689
2.83M
      (Ctx->OutSec->Flags & SHF_TLS) && 
Ctx->OutSec->Type == SHT_NOBITS1.12k
;
690
2.83M
  uint64_t Start = IsTbss ? 
Dot + Ctx->ThreadBssOffset520
:
Dot2.83M
;
691
2.83M
  Start = alignTo(Start, Alignment);
692
2.83M
  uint64_t End = Start + Size;
693
2.83M
694
2.83M
  if (IsTbss)
695
520
    Ctx->ThreadBssOffset = End - Dot;
696
2.83M
  else
697
2.83M
    Dot = End;
698
2.83M
  return End;
699
2.83M
}
700
701
708k
void LinkerScript::output(InputSection *S) {
702
708k
  assert(Ctx->OutSec == S->getParent());
703
708k
  uint64_t Before = advance(0, 1);
704
708k
  uint64_t Pos = advance(S->getSize(), S->Alignment);
705
708k
  S->OutSecOff = Pos - S->getSize() - Ctx->OutSec->Addr;
706
708k
707
708k
  // Update output section size after adding each section. This is so that
708
708k
  // SIZEOF works correctly in the case below:
709
708k
  // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
710
708k
  expandOutputSection(Pos - Before);
711
708k
}
712
713
708k
void LinkerScript::switchTo(OutputSection *Sec) {
714
708k
  Ctx->OutSec = Sec;
715
708k
716
708k
  uint64_t Before = advance(0, 1);
717
708k
  Ctx->OutSec->Addr = advance(0, Ctx->OutSec->Alignment);
718
708k
  expandMemoryRegions(Ctx->OutSec->Addr - Before);
719
708k
}
720
721
// This function searches for a memory region to place the given output
722
// section in. If found, a pointer to the appropriate memory region is
723
// returned. Otherwise, a nullptr is returned.
724
134k
MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) {
725
134k
  // If a memory region name was specified in the output section command,
726
134k
  // then try to find that region first.
727
134k
  if (!Sec->MemoryRegionName.empty()) {
728
58
    if (MemoryRegion *M = MemoryRegions.lookup(Sec->MemoryRegionName))
729
57
      return M;
730
1
    error("memory region '" + Sec->MemoryRegionName + "' not declared");
731
1
    return nullptr;
732
1
  }
733
134k
734
134k
  // If at least one memory region is defined, all sections must
735
134k
  // belong to some memory region. Otherwise, we don't need to do
736
134k
  // anything for memory regions.
737
134k
  if (MemoryRegions.empty())
738
133k
    return nullptr;
739
136
740
136
  // See if a region can be found by matching section flags.
741
249
  
for (auto &Pair : MemoryRegions)136
{
742
249
    MemoryRegion *M = Pair.second;
743
249
    if ((M->Flags & Sec->Flags) && 
(M->NegFlags & Sec->Flags) == 015
)
744
15
      return M;
745
249
  }
746
136
747
136
  // Otherwise, no suitable region was found.
748
136
  
if (121
Sec->Flags & SHF_ALLOC121
)
749
1
    error("no memory region specified for section '" + Sec->Name + "'");
750
121
  return nullptr;
751
136
}
752
753
159k
static OutputSection *findFirstSection(PhdrEntry *Load) {
754
159k
  for (OutputSection *Sec : OutputSections)
755
338k
    if (Sec->PtLoad == Load)
756
159k
      return Sec;
757
159k
  
return nullptr40
;
758
159k
}
759
760
// This function assigns offsets to input sections and an output section
761
// for a single sections command (e.g. ".text { *(.text); }").
762
703k
void LinkerScript::assignOffsets(OutputSection *Sec) {
763
703k
  if (!(Sec->Flags & SHF_ALLOC))
764
543k
    Dot = 0;
765
703k
766
703k
  Ctx->MemRegion = Sec->MemRegion;
767
703k
  Ctx->LMARegion = Sec->LMARegion;
768
703k
  if (Ctx->MemRegion)
769
137
    Dot = Ctx->MemRegion->CurPos;
770
703k
771
703k
  if ((Sec->Flags & SHF_ALLOC) && 
Sec->AddrExpr159k
)
772
7.80k
    setDot(Sec->AddrExpr, Sec->Location, false);
773
703k
774
703k
  switchTo(Sec);
775
703k
776
703k
  if (Sec->LMAExpr)
777
81
    Ctx->LMAOffset = Sec->LMAExpr().getValue() - Dot;
778
703k
779
703k
  if (MemoryRegion *MR = Sec->LMARegion)
780
27
    Ctx->LMAOffset = MR->CurPos - Dot;
781
703k
782
703k
  // If neither AT nor AT> is specified for an allocatable section, the linker
783
703k
  // will set the LMA such that the difference between VMA and LMA for the
784
703k
  // section is the same as the preceding output section in the same region
785
703k
  // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
786
703k
  // This, however, should only be done by the first "non-header" section
787
703k
  // in the segment.
788
703k
  if (PhdrEntry *L = Ctx->OutSec->PtLoad)
789
158k
    if (Sec == findFirstSection(L))
790
11.5k
      L->LMAOffset = Ctx->LMAOffset;
791
703k
792
703k
  // We can call this method multiple times during the creation of
793
703k
  // thunks and want to start over calculation each time.
794
703k
  Sec->Size = 0;
795
703k
796
703k
  // We visited SectionsCommands from processSectionCommands to
797
703k
  // layout sections. Now, we visit SectionsCommands again to fix
798
703k
  // section offsets.
799
703k
  for (BaseCommand *Base : Sec->SectionCommands) {
800
703k
    // This handles the assignments to symbol or to the dot.
801
703k
    if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
802
249
      Cmd->Addr = Dot;
803
249
      assignSymbol(Cmd, true);
804
249
      Cmd->Size = Dot - Cmd->Addr;
805
249
      continue;
806
249
    }
807
703k
808
703k
    // Handle BYTE(), SHORT(), LONG(), or QUAD().
809
703k
    if (auto *Cmd = dyn_cast<ByteCommand>(Base)) {
810
92
      Cmd->Offset = Dot - Ctx->OutSec->Addr;
811
92
      Dot += Cmd->Size;
812
92
      expandOutputSection(Cmd->Size);
813
92
      continue;
814
92
    }
815
703k
816
703k
    // Handle a single input section description command.
817
703k
    // It calculates and assigns the offsets for each section and also
818
703k
    // updates the output section size.
819
703k
    for (InputSection *Sec : cast<InputSectionDescription>(Base)->Sections)
820
708k
      output(Sec);
821
703k
  }
822
703k
}
823
824
20.9k
static bool isDiscardable(OutputSection &Sec) {
825
20.9k
  // We do not remove empty sections that are explicitly
826
20.9k
  // assigned to any segment.
827
20.9k
  if (!Sec.Phdrs.empty())
828
10
    return false;
829
20.9k
830
20.9k
  // We do not want to remove OutputSections with expressions that reference
831
20.9k
  // symbols even if the OutputSection is empty. We want to ensure that the
832
20.9k
  // expressions can be evaluated and report an error if they cannot.
833
20.9k
  if (Sec.ExpressionsUseSymbols)
834
4
    return false;
835
20.9k
836
20.9k
  // OutputSections may be referenced by name in ADDR and LOADADDR expressions,
837
20.9k
  // as an empty Section can has a valid VMA and LMA we keep the OutputSection
838
20.9k
  // to maintain the integrity of the other Expression.
839
20.9k
  if (Sec.UsedInExpression)
840
1
    return false;
841
20.9k
842
20.9k
  for (BaseCommand *Base : Sec.SectionCommands) {
843
20.9k
    if (auto Cmd = dyn_cast<SymbolAssignment>(Base))
844
15
      // Don't create empty output sections just for unreferenced PROVIDE
845
15
      // symbols.
846
15
      if (Cmd->Name != "." && 
!Cmd->Sym11
)
847
3
        continue;
848
20.9k
849
20.9k
    if (!isa<InputSectionDescription>(*Base))
850
21
      return false;
851
20.9k
  }
852
20.9k
  
return true20.9k
;
853
20.9k
}
854
855
2.41k
void LinkerScript::adjustSectionsBeforeSorting() {
856
2.41k
  // If the output section contains only symbol assignments, create a
857
2.41k
  // corresponding output section. The issue is what to do with linker script
858
2.41k
  // like ".foo : { symbol = 42; }". One option would be to convert it to
859
2.41k
  // "symbol = 42;". That is, move the symbol out of the empty section
860
2.41k
  // description. That seems to be what bfd does for this simple case. The
861
2.41k
  // problem is that this is not completely general. bfd will give up and
862
2.41k
  // create a dummy section too if there is a ". = . + 1" inside the section
863
2.41k
  // for example.
864
2.41k
  // Given that we want to create the section, we have to worry what impact
865
2.41k
  // it will have on the link. For example, if we just create a section with
866
2.41k
  // 0 for flags, it would change which PT_LOADs are created.
867
2.41k
  // We could remember that particular section is dummy and ignore it in
868
2.41k
  // other parts of the linker, but unfortunately there are quite a few places
869
2.41k
  // that would need to change:
870
2.41k
  //   * The program header creation.
871
2.41k
  //   * The orphan section placement.
872
2.41k
  //   * The address assignment.
873
2.41k
  // The other option is to pick flags that minimize the impact the section
874
2.41k
  // will have on the rest of the linker. That is why we copy the flags from
875
2.41k
  // the previous sections. Only a few flags are needed to keep the impact low.
876
2.41k
  uint64_t Flags = SHF_ALLOC;
877
2.41k
878
372k
  for (BaseCommand *&Cmd : SectionCommands) {
879
372k
    auto *Sec = dyn_cast<OutputSection>(Cmd);
880
372k
    if (!Sec)
881
410
      continue;
882
372k
883
372k
    // Handle align (e.g. ".foo : ALIGN(16) { ... }").
884
372k
    if (Sec->AlignExpr)
885
15
      Sec->Alignment =
886
15
          std::max<uint32_t>(Sec->Alignment, Sec->AlignExpr().getValue());
887
372k
888
372k
    // A live output section means that some input section was added to it. It
889
372k
    // might have been removed (if it was empty synthetic section), but we at
890
372k
    // least know the flags.
891
372k
    if (Sec->Live)
892
371k
      Flags = Sec->Flags;
893
372k
894
372k
    // We do not want to keep any special flags for output section
895
372k
    // in case it is empty.
896
372k
    bool IsEmpty = getInputSections(Sec).empty();
897
372k
    if (IsEmpty)
898
20.9k
      Sec->Flags = Flags & ((Sec->NonAlloc ? 
01
:
(uint64_t)SHF_ALLOC20.9k
) |
899
20.9k
                            SHF_WRITE | SHF_EXECINSTR);
900
372k
901
372k
    if (IsEmpty && 
isDiscardable(*Sec)20.9k
) {
902
20.9k
      Sec->Live = false;
903
20.9k
      Cmd = nullptr;
904
20.9k
    }
905
372k
  }
906
2.41k
907
2.41k
  // It is common practice to use very generic linker scripts. So for any
908
2.41k
  // given run some of the output sections in the script will be empty.
909
2.41k
  // We could create corresponding empty output sections, but that would
910
2.41k
  // clutter the output.
911
2.41k
  // We instead remove trivially empty sections. The bfd linker seems even
912
2.41k
  // more aggressive at removing them.
913
372k
  llvm::erase_if(SectionCommands, [&](BaseCommand *Base) { return !Base; });
914
2.41k
}
915
916
444
void LinkerScript::adjustSectionsAfterSorting() {
917
444
  // Try and find an appropriate memory region to assign offsets in.
918
134k
  for (BaseCommand *Base : SectionCommands) {
919
134k
    if (auto *Sec = dyn_cast<OutputSection>(Base)) {
920
134k
      if (!Sec->LMARegionName.empty()) {
921
14
        if (MemoryRegion *M = MemoryRegions.lookup(Sec->LMARegionName))
922
14
          Sec->LMARegion = M;
923
0
        else
924
0
          error("memory region '" + Sec->LMARegionName + "' not declared");
925
14
      }
926
134k
      Sec->MemRegion = findMemoryRegion(Sec);
927
134k
    }
928
134k
  }
929
444
930
444
  // If output section command doesn't specify any segments,
931
444
  // and we haven't previously assigned any section to segment,
932
444
  // then we simply assign section to the very first load segment.
933
444
  // Below is an example of such linker script:
934
444
  // PHDRS { seg PT_LOAD; }
935
444
  // SECTIONS { .aaa : { *(.aaa) } }
936
444
  std::vector<StringRef> DefPhdrs;
937
444
  auto FirstPtLoad = llvm::find_if(PhdrsCommands, [](const PhdrsCommand &Cmd) {
938
30
    return Cmd.Type == PT_LOAD;
939
30
  });
940
444
  if (FirstPtLoad != PhdrsCommands.end())
941
28
    DefPhdrs.push_back(FirstPtLoad->Name);
942
444
943
444
  // Walk the commands and propagate the program headers to commands that don't
944
444
  // explicitly specify them.
945
134k
  for (BaseCommand *Base : SectionCommands) {
946
134k
    auto *Sec = dyn_cast<OutputSection>(Base);
947
134k
    if (!Sec)
948
318
      continue;
949
134k
950
134k
    if (Sec->Phdrs.empty()) {
951
134k
      // To match the bfd linker script behaviour, only propagate program
952
134k
      // headers to sections that are allocated.
953
134k
      if (Sec->Flags & SHF_ALLOC)
954
66.9k
        Sec->Phdrs = DefPhdrs;
955
134k
    } else {
956
48
      DefPhdrs = Sec->Phdrs;
957
48
    }
958
134k
  }
959
444
}
960
961
2.28k
static uint64_t computeBase(uint64_t Min, bool AllocateHeaders) {
962
2.28k
  // If there is no SECTIONS or if the linkerscript is explicit about program
963
2.28k
  // headers, do our best to allocate them.
964
2.28k
  if (!Script->HasSectionsCommand || 
AllocateHeaders417
)
965
1.88k
    return 0;
966
402
  // Otherwise only allocate program headers if that would not add a page.
967
402
  return alignDown(Min, Config->MaxPageSize);
968
402
}
969
970
// Try to find an address for the file and program headers output sections,
971
// which were unconditionally added to the first PT_LOAD segment earlier.
972
//
973
// When using the default layout, we check if the headers fit below the first
974
// allocated section. When using a linker script, we also check if the headers
975
// are covered by the output section. This allows omitting the headers by not
976
// leaving enough space for them in the linker script; this pattern is common
977
// in embedded systems.
978
//
979
// If there isn't enough space for these sections, we'll remove them from the
980
// PT_LOAD segment, and we'll also remove the PT_PHDR segment.
981
2.38k
void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) {
982
2.38k
  uint64_t Min = std::numeric_limits<uint64_t>::max();
983
2.38k
  for (OutputSection *Sec : OutputSections)
984
350k
    if (Sec->Flags & SHF_ALLOC)
985
79.4k
      Min = std::min<uint64_t>(Min, Sec->Addr);
986
2.38k
987
2.38k
  auto It = llvm::find_if(
988
4.57k
      Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; });
989
2.38k
  if (It == Phdrs.end())
990
91
    return;
991
2.29k
  PhdrEntry *FirstPTLoad = *It;
992
2.29k
993
2.29k
  bool HasExplicitHeaders =
994
2.29k
      llvm::any_of(PhdrsCommands, [](const PhdrsCommand &Cmd) {
995
37
        return Cmd.HasPhdrs || 
Cmd.HasFilehdr21
;
996
37
      });
997
2.29k
  bool Paged = !Config->Omagic && 
!Config->Nmagic2.29k
;
998
2.29k
  uint64_t HeaderSize = getHeaderSize();
999
2.29k
  if ((Paged || 
HasExplicitHeaders10
) &&
1000
2.29k
      
HeaderSize <= Min - computeBase(Min, HasExplicitHeaders)2.28k
) {
1001
1.93k
    Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
1002
1.93k
    Out::ElfHeader->Addr = Min;
1003
1.93k
    Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
1004
1.93k
    return;
1005
1.93k
  }
1006
364
1007
364
  // Error if we were explicitly asked to allocate headers.
1008
364
  if (HasExplicitHeaders)
1009
1
    error("could not allocate headers");
1010
364
1011
364
  Out::ElfHeader->PtLoad = nullptr;
1012
364
  Out::ProgramHeaders->PtLoad = nullptr;
1013
364
  FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad);
1014
364
1015
364
  llvm::erase_if(Phdrs,
1016
1.58k
                 [](const PhdrEntry *E) { return E->p_type == PT_PHDR; });
1017
364
}
1018
1019
7.43k
LinkerScript::AddressState::AddressState() {
1020
7.43k
  for (auto &MRI : Script->MemoryRegions) {
1021
168
    MemoryRegion *MR = MRI.second;
1022
168
    MR->CurPos = MR->Origin;
1023
168
  }
1024
7.43k
}
1025
1026
4.91k
static uint64_t getInitialDot() {
1027
4.91k
  // By default linker scripts use an initial value of 0 for '.',
1028
4.91k
  // but prefer -image-base if set.
1029
4.91k
  if (Script->HasSectionsCommand)
1030
899
    return Config->ImageBase ? 
*Config->ImageBase2
:
0897
;
1031
4.01k
1032
4.01k
  uint64_t StartAddr = UINT64_MAX;
1033
4.01k
  // The Sections with -T<section> have been sorted in order of ascending
1034
4.01k
  // address. We must lower StartAddr if the lowest -T<section address> as
1035
4.01k
  // calls to setDot() must be monotonically increasing.
1036
4.01k
  for (auto &KV : Config->SectionStartMap)
1037
109
    StartAddr = std::min(StartAddr, KV.second);
1038
4.01k
  return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize());
1039
4.01k
}
1040
1041
// Here we assign addresses as instructed by linker script SECTIONS
1042
// sub-commands. Doing that allows us to use final VA values, so here
1043
// we also handle rest commands like symbol assignments and ASSERTs.
1044
4.91k
void LinkerScript::assignAddresses() {
1045
4.91k
  Dot = getInitialDot();
1046
4.91k
1047
4.91k
  auto Deleter = make_unique<AddressState>();
1048
4.91k
  Ctx = Deleter.get();
1049
4.91k
  ErrorOnMissingSection = true;
1050
4.91k
  switchTo(Aether);
1051
4.91k
1052
704k
  for (BaseCommand *Base : SectionCommands) {
1053
704k
    if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
1054
820
      Cmd->Addr = Dot;
1055
820
      assignSymbol(Cmd, false);
1056
820
      Cmd->Size = Dot - Cmd->Addr;
1057
820
      continue;
1058
820
    }
1059
703k
    assignOffsets(cast<OutputSection>(Base));
1060
703k
  }
1061
4.91k
  Ctx = nullptr;
1062
4.91k
}
1063
1064
// Creates program headers as instructed by PHDRS linker script command.
1065
34
std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
1066
34
  std::vector<PhdrEntry *> Ret;
1067
34
1068
34
  // Process PHDRS and FILEHDR keywords because they are not
1069
34
  // real output sections and cannot be added in the following loop.
1070
48
  for (const PhdrsCommand &Cmd : PhdrsCommands) {
1071
48
    PhdrEntry *Phdr = make<PhdrEntry>(Cmd.Type, Cmd.Flags ? 
*Cmd.Flags18
:
PF_R30
);
1072
48
1073
48
    if (Cmd.HasFilehdr)
1074
14
      Phdr->add(Out::ElfHeader);
1075
48
    if (Cmd.HasPhdrs)
1076
17
      Phdr->add(Out::ProgramHeaders);
1077
48
1078
48
    if (Cmd.LMAExpr) {
1079
1
      Phdr->p_paddr = Cmd.LMAExpr().getValue();
1080
1
      Phdr->HasLMA = true;
1081
1
    }
1082
48
    Ret.push_back(Phdr);
1083
48
  }
1084
34
1085
34
  // Add output sections to program headers.
1086
257
  for (OutputSection *Sec : OutputSections) {
1087
257
    // Assign headers specified by linker script
1088
257
    for (size_t Id : getPhdrIndices(Sec)) {
1089
100
      Ret[Id]->add(Sec);
1090
100
      if (!PhdrsCommands[Id].Flags.hasValue())
1091
50
        Ret[Id]->p_flags |= Sec->getPhdrFlags();
1092
100
    }
1093
257
  }
1094
34
  return Ret;
1095
34
}
1096
1097
// Returns true if we should emit an .interp section.
1098
//
1099
// We usually do. But if PHDRS commands are given, and
1100
// no PT_INTERP is there, there's no place to emit an
1101
// .interp, so we don't do that in that case.
1102
24
bool LinkerScript::needsInterpSection() {
1103
24
  if (PhdrsCommands.empty())
1104
18
    return true;
1105
6
  for (PhdrsCommand &Cmd : PhdrsCommands)
1106
6
    if (Cmd.Type == PT_INTERP)
1107
2
      return true;
1108
6
  
return false4
;
1109
6
}
1110
1111
573
ExprValue LinkerScript::getSymbolValue(StringRef Name, const Twine &Loc) {
1112
573
  if (Name == ".") {
1113
389
    if (Ctx)
1114
388
      return {Ctx->OutSec, false, Dot - Ctx->OutSec->Addr, Loc};
1115
1
    error(Loc + ": unable to get location counter value");
1116
1
    return 0;
1117
1
  }
1118
184
1119
184
  if (Symbol *Sym = Symtab->find(Name)) {
1120
184
    if (auto *DS = dyn_cast<Defined>(Sym))
1121
173
      return {DS->Section, false, DS->Value, Loc};
1122
11
    if (isa<SharedSymbol>(Sym))
1123
3
      if (!ErrorOnMissingSection)
1124
2
        return {nullptr, false, 0, Loc};
1125
9
  }
1126
9
1127
9
  error(Loc + ": symbol not found: " + Name);
1128
9
  return 0;
1129
9
}
1130
1131
// Returns the index of the segment named Name.
1132
static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> Vec,
1133
103
                                     StringRef Name) {
1134
127
  for (size_t I = 0; I < Vec.size(); 
++I24
)
1135
124
    if (Vec[I].Name == Name)
1136
100
      return I;
1137
103
  
return None3
;
1138
103
}
1139
1140
// Returns indices of ELF headers containing specific section. Each index is a
1141
// zero based number of ELF header listed within PHDRS {} script block.
1142
257
std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) {
1143
257
  std::vector<size_t> Ret;
1144
257
1145
257
  for (StringRef S : Cmd->Phdrs) {
1146
103
    if (Optional<size_t> Idx = getPhdrIndex(PhdrsCommands, S))
1147
100
      Ret.push_back(*Idx);
1148
3
    else if (S != "NONE")
1149
1
      error(Cmd->Location + ": section header '" + S +
1150
1
            "' is not listed in PHDRS");
1151
103
  }
1152
257
  return Ret;
1153
257
}