Coverage Report

Created: 2018-10-20 06:24

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