Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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 "Memory.h"
18
#include "OutputSections.h"
19
#include "Strings.h"
20
#include "SymbolTable.h"
21
#include "Symbols.h"
22
#include "SyntheticSections.h"
23
#include "Target.h"
24
#include "Threads.h"
25
#include "Writer.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
97
static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) {
53
97
  if (OutputSection *OS = InputSec->getOutputSection())
54
95
    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
4.96k
uint64_t ExprValue::getValue() const {
61
4.96k
  if (Sec)
62
73
    return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc),
63
73
                   Alignment);
64
4.89k
  return alignTo(Val, Alignment);
65
4.89k
}
66
67
31
uint64_t ExprValue::getSecAddr() const {
68
31
  if (Sec)
69
24
    return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc);
70
7
  return 0;
71
7
}
72
73
217
uint64_t ExprValue::getSectionOffset() const {
74
217
  // If the alignment is trivial, we don't have to compute the full
75
217
  // value to know the offset. This allows this function to succeed in
76
217
  // cases where the output section is not yet known.
77
217
  if (Alignment == 1)
78
201
    return Val;
79
16
  return getValue() - getSecAddr();
80
16
}
81
82
185
static SymbolBody *addRegular(SymbolAssignment *Cmd) {
83
185
  Symbol *Sym;
84
185
  uint8_t Visibility = Cmd->Hidden ? 
STV_HIDDEN22
:
STV_DEFAULT163
;
85
185
  std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, /*Type*/ 0, Visibility,
86
185
                                              /*CanOmitFromDynSym*/ false,
87
185
                                              /*File*/ nullptr);
88
185
  Sym->Binding = STB_GLOBAL;
89
185
  ExprValue Value = Cmd->Expression();
90
185
  SectionBase *Sec = Value.isAbsolute() ? 
nullptr107
:
Value.Sec78
;
91
185
92
185
  // We want to set symbol values early if we can. This allows us to use symbols
93
185
  // as variables in linker scripts. Doing so allows us to write expressions
94
185
  // like this: `alignment = 16; . = ALIGN(., alignment)`
95
185
  uint64_t SymValue = Value.Sec ? 
087
:
Value.getValue()98
;
96
185
  replaceBody<DefinedRegular>(Sym, nullptr, Cmd->Name, /*IsLocal=*/false,
97
185
                              Visibility, STT_NOTYPE, SymValue, 0, Sec);
98
185
  return Sym->body();
99
185
}
100
101
OutputSection *LinkerScript::createOutputSection(StringRef Name,
102
222k
                                                 StringRef Location) {
103
222k
  OutputSection *&SecRef = NameToOutputSection[Name];
104
222k
  OutputSection *Sec;
105
222k
  if (
SecRef && 222k
SecRef->Location.empty()14
) {
106
5
    // There was a forward reference.
107
5
    Sec = SecRef;
108
222k
  } else {
109
222k
    Sec = make<OutputSection>(Name, SHT_PROGBITS, 0);
110
222k
    if (!SecRef)
111
222k
      SecRef = Sec;
112
222k
  }
113
222k
  Sec->Location = Location;
114
222k
  return Sec;
115
222k
}
116
117
45
OutputSection *LinkerScript::getOrCreateOutputSection(StringRef Name) {
118
45
  OutputSection *&CmdRef = NameToOutputSection[Name];
119
45
  if (!CmdRef)
120
8
    CmdRef = make<OutputSection>(Name, SHT_PROGBITS, 0);
121
45
  return CmdRef;
122
45
}
123
124
4.27k
void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
125
4.27k
  uint64_t Val = E().getValue();
126
4.27k
  if (
Val < Dot && 4.27k
InSec4
)
127
2
    error(Loc + ": unable to move location counter backward for: " +
128
2
          CurAddressState->OutSec->Name);
129
4.27k
  Dot = Val;
130
4.27k
  // Update to location counter means update to section size.
131
4.27k
  if (InSec)
132
11
    CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr;
133
4.27k
}
134
135
// Sets value of a symbol. Two kinds of symbols are processed: synthetic
136
// symbols, whose value is an offset from beginning of section and regular
137
// symbols whose value is absolute.
138
1.76k
void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
139
1.76k
  if (
Cmd->Name == "."1.76k
) {
140
1.57k
    setDot(Cmd->Expression, Cmd->Location, InSec);
141
1.57k
    return;
142
1.57k
  }
143
195
144
195
  
if (195
!Cmd->Sym195
)
145
10
    return;
146
185
147
185
  auto *Sym = cast<DefinedRegular>(Cmd->Sym);
148
185
  ExprValue V = Cmd->Expression();
149
185
  if (
V.isAbsolute()185
) {
150
109
    Sym->Value = V.getValue();
151
109
    Sym->Section = nullptr;
152
185
  } else {
153
76
    Sym->Section = V.Sec;
154
76
    Sym->Value = V.getSectionOffset();
155
76
  }
156
1.76k
}
157
158
319
void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
159
319
  if (Cmd->Name == ".")
160
125
    return;
161
194
162
194
  // If a symbol was in PROVIDE(), we need to define it only when
163
194
  // it is a referenced undefined symbol.
164
194
  SymbolBody *B = Symtab->find(Cmd->Name);
165
194
  if (
Cmd->Provide && 194
(!B || 36
B->isDefined()31
))
166
9
    return;
167
185
168
185
  Cmd->Sym = addRegular(Cmd);
169
185
}
170
171
426k
bool SymbolAssignment::classof(const BaseCommand *C) {
172
426k
  return C->Kind == AssignmentKind;
173
426k
}
174
175
442k
bool InputSectionDescription::classof(const BaseCommand *C) {
176
442k
  return C->Kind == InputSectionKind;
177
442k
}
178
179
423k
bool AssertCommand::classof(const BaseCommand *C) {
180
423k
  return C->Kind == AssertKind;
181
423k
}
182
183
420k
bool BytesDataCommand::classof(const BaseCommand *C) {
184
420k
  return C->Kind == BytesDataKind;
185
420k
}
186
187
138k
static std::string filename(InputFile *File) {
188
138k
  if (!File)
189
6.40k
    return "";
190
132k
  
if (132k
File->ArchiveName.empty()132k
)
191
132k
    return File->getName();
192
28
  return (File->ArchiveName + "(" + File->getName() + ")").str();
193
28
}
194
195
197
bool LinkerScript::shouldKeep(InputSectionBase *S) {
196
14
  for (InputSectionDescription *ID : Opt.KeptSections) {
197
14
    std::string Filename = filename(S->File);
198
14
    if (ID->FilePat.match(Filename))
199
11
      for (SectionPattern &P : ID->SectionPatterns)
200
11
        
if (11
P.SectionPat.match(S->Name)11
)
201
4
          return true;
202
193
  }
203
193
  return false;
204
193
}
205
206
// A helper function for the SORT() command.
207
static std::function<bool(InputSectionBase *, InputSectionBase *)>
208
21
getComparator(SortSectionPolicy K) {
209
21
  switch (K) {
210
7
  case SortSectionPolicy::Alignment:
211
115
    return [](InputSectionBase *A, InputSectionBase *B) {
212
115
      // ">" is not a mistake. Sections with larger alignments are placed
213
115
      // before sections with smaller alignments in order to reduce the
214
115
      // amount of padding necessary. This is compatible with GNU.
215
115
      return A->Alignment > B->Alignment;
216
115
    };
217
13
  case SortSectionPolicy::Name:
218
202
    return [](InputSectionBase *A, InputSectionBase *B) {
219
202
      return A->Name < B->Name;
220
202
    };
221
1
  case SortSectionPolicy::Priority:
222
1
    return [](InputSectionBase *A, InputSectionBase *B) {
223
1
      return getPriority(A->Name) < getPriority(B->Name);
224
1
    };
225
0
  default:
226
0
    llvm_unreachable("unknown sort policy");
227
0
  }
228
0
}
229
230
// A helper function for the SORT() command.
231
static bool matchConstraints(ArrayRef<InputSectionBase *> Sections,
232
449
                             ConstraintKind Kind) {
233
449
  if (Kind == ConstraintKind::NoConstraint)
234
435
    return true;
235
14
236
14
  
bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) 14
{
237
15
    return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE;
238
15
  });
239
14
240
11
  return (IsRW && Kind == ConstraintKind::ReadWrite) ||
241
9
         
(!IsRW && 9
Kind == ConstraintKind::ReadOnly3
);
242
449
}
243
244
static void sortSections(InputSection **Begin, InputSection **End,
245
34
                         SortSectionPolicy K) {
246
34
  if (
K != SortSectionPolicy::Default && 34
K != SortSectionPolicy::None22
)
247
21
    std::stable_sort(Begin, End, getComparator(K));
248
34
}
249
250
462
static void sortBySymbolOrder(InputSection **Begin, InputSection **End) {
251
462
  if (Config->SymbolOrderingFile.empty())
252
461
    return;
253
1
  static llvm::DenseMap<SectionBase *, int> Order = buildSectionOrder();
254
1
  MutableArrayRef<InputSection *> In(Begin, End - Begin);
255
2
  sortByOrder(In, [&](InputSectionBase *S) { return Order.lookup(S); });
256
462
}
257
258
// Compute and remember which sections the InputSectionDescription matches.
259
std::vector<InputSection *>
260
476
LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
261
476
  std::vector<InputSection *> Ret;
262
476
263
476
  // Collects all sections that satisfy constraints of Cmd.
264
481
  for (const SectionPattern &Pat : Cmd->SectionPatterns) {
265
481
    size_t SizeBefore = Ret.size();
266
481
267
140k
    for (InputSectionBase *Sec : InputSections) {
268
140k
      if (Sec->Assigned)
269
508
        continue;
270
139k
271
139k
      
if (139k
!Sec->Live139k
) {
272
88
        reportDiscarded(Sec);
273
88
        continue;
274
88
      }
275
139k
276
139k
      // For -emit-relocs we have to ignore entries like
277
139k
      //   .rela.dyn : { *(.rela.data) }
278
139k
      // which are common because they are in the default bfd script.
279
139k
      
if (139k
Sec->Type == SHT_REL || 139k
Sec->Type == SHT_RELA139k
)
280
1.07k
        continue;
281
138k
282
138k
      std::string Filename = filename(Sec->File);
283
138k
      if (!Cmd->FilePat.match(Filename) ||
284
138k
          Pat.ExcludedFilePat.match(Filename) ||
285
138k
          !Pat.SectionPat.match(Sec->Name))
286
137k
        continue;
287
683
288
683
      Ret.push_back(cast<InputSection>(Sec));
289
683
      Sec->Assigned = true;
290
683
    }
291
481
292
481
    // Sort sections as instructed by SORT-family commands and --sort-section
293
481
    // option. Because SORT-family commands can be nested at most two depth
294
481
    // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
295
481
    // line option is respected even if a SORT command is given, the exact
296
481
    // behavior we have here is a bit complicated. Here are the rules.
297
481
    //
298
481
    // 1. If two SORT commands are given, --sort-section is ignored.
299
481
    // 2. If one SORT command is given, and if it is not SORT_NONE,
300
481
    //    --sort-section is handled as an inner SORT command.
301
481
    // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
302
481
    // 4. If no SORT command is given, sort according to --sort-section.
303
481
    // 5. If no SORT commands are given and --sort-section is not specified,
304
481
    //    apply sorting provided by --symbol-ordering-file if any exist.
305
481
    InputSection **Begin = Ret.data() + SizeBefore;
306
481
    InputSection **End = Ret.data() + Ret.size();
307
481
    if (Pat.SortOuter == SortSectionPolicy::Default &&
308
481
        
Config->SortSection == SortSectionPolicy::Default465
) {
309
462
      sortBySymbolOrder(Begin, End);
310
462
      continue;
311
462
    }
312
19
    
if (19
Pat.SortOuter != SortSectionPolicy::None19
) {
313
17
      if (Pat.SortInner == SortSectionPolicy::Default)
314
14
        sortSections(Begin, End, Config->SortSection);
315
17
      else
316
3
        sortSections(Begin, End, Pat.SortInner);
317
17
      sortSections(Begin, End, Pat.SortOuter);
318
17
    }
319
481
  }
320
476
  return Ret;
321
476
}
322
323
42
void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
324
24
  for (InputSectionBase *S : V) {
325
24
    S->Live = false;
326
24
    if (
S == InX::ShStrTab || 24
S == InX::Dynamic23
||
S == InX::DynSymTab22
||
327
21
        S == InX::DynStrTab)
328
4
      error("discarding " + S->Name + " section is not allowed");
329
24
    discard(S->DependentSections);
330
24
  }
331
42
}
332
333
std::vector<InputSectionBase *>
334
460
LinkerScript::createInputSectionList(OutputSection &OutCmd) {
335
460
  std::vector<InputSectionBase *> Ret;
336
460
337
558
  for (BaseCommand *Base : OutCmd.Commands) {
338
558
    auto *Cmd = dyn_cast<InputSectionDescription>(Base);
339
558
    if (!Cmd)
340
82
      continue;
341
476
342
476
    Cmd->Sections = computeInputSections(Cmd);
343
476
    Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
344
476
  }
345
460
346
460
  return Ret;
347
460
}
348
349
1.59k
void LinkerScript::processCommands(OutputSectionFactory &Factory) {
350
1.59k
  // A symbol can be assigned before any section is mentioned in the linker
351
1.59k
  // script. In an DSO, the symbol values are addresses, so the only important
352
1.59k
  // section values are:
353
1.59k
  // * SHN_UNDEF
354
1.59k
  // * SHN_ABS
355
1.59k
  // * Any value meaning a regular section.
356
1.59k
  // To handle that, create a dummy aether section that fills the void before
357
1.59k
  // the linker scripts switches to another section. It has an index of one
358
1.59k
  // which will map to whatever the first actual section is.
359
1.59k
  Aether = make<OutputSection>("", 0, SHF_ALLOC);
360
1.59k
  Aether->SectionIndex = 1;
361
1.59k
  auto State = make_unique<AddressState>(Opt);
362
1.59k
  // CurAddressState captures the local AddressState and makes it accessible
363
1.59k
  // deliberately. This is needed as there are some cases where we cannot just
364
1.59k
  // thread the current state through to a lambda function created by the
365
1.59k
  // script parser.
366
1.59k
  CurAddressState = State.get();
367
1.59k
  CurAddressState->OutSec = Aether;
368
1.59k
  Dot = 0;
369
1.59k
370
2.32k
  for (size_t I = 0; 
I < Opt.Commands.size()2.32k
;
++I723
) {
371
723
    // Handle symbol assignments outside of any output section.
372
723
    if (auto *
Cmd723
= dyn_cast<SymbolAssignment>(Opt.Commands[I])) {
373
259
      addSymbol(Cmd);
374
259
      continue;
375
259
    }
376
464
377
464
    
if (auto *464
Sec464
= dyn_cast<OutputSection>(Opt.Commands[I])) {
378
460
      std::vector<InputSectionBase *> V = createInputSectionList(*Sec);
379
460
380
460
      // The output section name `/DISCARD/' is special.
381
460
      // Any input section assigned to it is discarded.
382
460
      if (
Sec->Name == "/DISCARD/"460
) {
383
11
        discard(V);
384
11
        continue;
385
11
      }
386
449
387
449
      // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
388
449
      // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
389
449
      // sections satisfy a given constraint. If not, a directive is handled
390
449
      // as if it wasn't present from the beginning.
391
449
      //
392
449
      // Because we'll iterate over Commands many more times, the easiest
393
449
      // way to "make it as if it wasn't present" is to just remove it.
394
449
      
if (449
!matchConstraints(V, Sec->Constraint)449
) {
395
6
        for (InputSectionBase *S : V)
396
10
          S->Assigned = false;
397
6
        Opt.Commands.erase(Opt.Commands.begin() + I);
398
6
        --I;
399
6
        continue;
400
6
      }
401
443
402
443
      // A directive may contain symbol definitions like this:
403
443
      // ".foo : { ...; bar = .; }". Handle them.
404
443
      for (BaseCommand *Base : Sec->Commands)
405
537
        
if (auto *537
OutCmd537
= dyn_cast<SymbolAssignment>(Base))
406
60
          addSymbol(OutCmd);
407
443
408
443
      // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
409
443
      // is given, input sections are aligned to that value, whether the
410
443
      // given value is larger or smaller than the original section alignment.
411
443
      if (
Sec->SubalignExpr443
) {
412
1
        uint32_t Subalign = Sec->SubalignExpr().getValue();
413
1
        for (InputSectionBase *S : V)
414
4
          S->Alignment = Subalign;
415
1
      }
416
443
417
443
      // Add input sections to an output section.
418
443
      for (InputSectionBase *S : V)
419
661
        Factory.addInputSec(S, Sec->Name, Sec);
420
443
      assert(Sec->SectionIndex == INT_MAX);
421
443
      Sec->SectionIndex = I;
422
443
      if (Sec->Noload)
423
2
        Sec->Type = SHT_NOBITS;
424
460
    }
425
723
  }
426
1.59k
  CurAddressState = nullptr;
427
1.59k
}
428
429
1.30k
void LinkerScript::fabricateDefaultCommands() {
430
1.30k
  // Define start address
431
1.30k
  uint64_t StartAddr = UINT64_MAX;
432
1.30k
433
1.30k
  // The Sections with -T<section> have been sorted in order of ascending
434
1.30k
  // address. We must lower StartAddr if the lowest -T<section address> as
435
1.30k
  // calls to setDot() must be monotonically increasing.
436
1.30k
  for (auto &KV : Config->SectionStartMap)
437
40
    StartAddr = std::min(StartAddr, KV.second);
438
1.30k
439
1.43k
  auto Expr = [=] {
440
1.43k
    return std::min(StartAddr, Config->ImageBase + elf::getHeaderSize());
441
1.43k
  };
442
1.30k
  Opt.Commands.insert(Opt.Commands.begin(),
443
1.30k
                      make<SymbolAssignment>(".", Expr, ""));
444
1.30k
}
445
446
// Add sections that didn't match any sections command.
447
295
void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
448
295
  unsigned NumCommands = Opt.Commands.size();
449
136k
  for (InputSectionBase *S : InputSections) {
450
136k
    if (
!S->Live || 136k
S->Parent136k
)
451
732
      continue;
452
135k
    StringRef Name = getOutputSectionName(S->Name);
453
135k
    auto End = Opt.Commands.begin() + NumCommands;
454
272k
    auto I = std::find_if(Opt.Commands.begin(), End, [&](BaseCommand *Base) {
455
272k
      if (auto *Sec = dyn_cast<OutputSection>(Base))
456
137k
        return Sec->Name == Name;
457
134k
      return false;
458
134k
    });
459
135k
    log(toString(S) + " is being placed in '" + Name + "'");
460
135k
    if (
I == End135k
) {
461
135k
      Factory.addInputSec(S, Name);
462
135k
      assert(S->getOutputSection()->SectionIndex == INT_MAX);
463
135k
    } else {
464
42
      OutputSection *Sec = cast<OutputSection>(*I);
465
42
      Factory.addInputSec(S, Name, Sec);
466
42
      unsigned Index = std::distance(Opt.Commands.begin(), I);
467
42
      assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index);
468
42
      Sec->SectionIndex = Index;
469
42
    }
470
136k
  }
471
295
}
472
473
639k
uint64_t LinkerScript::advance(uint64_t Size, unsigned Align) {
474
639k
  bool IsTbss = (CurAddressState->OutSec->Flags & SHF_TLS) &&
475
374
                CurAddressState->OutSec->Type == SHT_NOBITS;
476
639k
  uint64_t Start = IsTbss ? 
Dot + CurAddressState->ThreadBssOffset192
:
Dot639k
;
477
639k
  Start = alignTo(Start, Align);
478
639k
  uint64_t End = Start + Size;
479
639k
480
639k
  if (IsTbss)
481
192
    CurAddressState->ThreadBssOffset = End - Dot;
482
639k
  else
483
639k
    Dot = End;
484
639k
  return End;
485
639k
}
486
487
212k
void LinkerScript::output(InputSection *S) {
488
212k
  uint64_t Before = advance(0, 1);
489
212k
  uint64_t Pos = advance(S->getSize(), S->Alignment);
490
212k
  S->OutSecOff = Pos - S->getSize() - CurAddressState->OutSec->Addr;
491
212k
492
212k
  // Update output section size after adding each section. This is so that
493
212k
  // SIZEOF works correctly in the case below:
494
212k
  // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
495
212k
  CurAddressState->OutSec->Size = Pos - CurAddressState->OutSec->Addr;
496
212k
497
212k
  // If there is a memory region associated with this input section, then
498
212k
  // place the section in that region and update the region index.
499
212k
  if (
CurAddressState->MemRegion212k
) {
500
17
    uint64_t &CurOffset =
501
17
        CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
502
17
    CurOffset += Pos - Before;
503
17
    uint64_t CurSize = CurOffset - CurAddressState->MemRegion->Origin;
504
17
    if (
CurSize > CurAddressState->MemRegion->Length17
) {
505
1
      uint64_t OverflowAmt = CurSize - CurAddressState->MemRegion->Length;
506
1
      error("section '" + CurAddressState->OutSec->Name +
507
1
            "' will not fit in region '" + CurAddressState->MemRegion->Name +
508
1
            "': overflowed by " + Twine(OverflowAmt) + " bytes");
509
1
    }
510
17
  }
511
212k
}
512
513
213k
void LinkerScript::switchTo(OutputSection *Sec) {
514
213k
  if (CurAddressState->OutSec == Sec)
515
0
    return;
516
213k
517
213k
  CurAddressState->OutSec = Sec;
518
213k
  CurAddressState->OutSec->Addr =
519
213k
      advance(0, CurAddressState->OutSec->Alignment);
520
213k
521
213k
  // If neither AT nor AT> is specified for an allocatable section, the linker
522
213k
  // will set the LMA such that the difference between VMA and LMA for the
523
213k
  // section is the same as the preceding output section in the same region
524
213k
  // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
525
213k
  if (CurAddressState->LMAOffset)
526
37
    CurAddressState->OutSec->LMAOffset = CurAddressState->LMAOffset();
527
213k
}
528
529
211k
void LinkerScript::process(BaseCommand &Base) {
530
211k
  // This handles the assignments to symbol or to the dot.
531
211k
  if (auto *
Cmd211k
= dyn_cast<SymbolAssignment>(&Base)) {
532
62
    assignSymbol(Cmd, true);
533
62
    return;
534
62
  }
535
211k
536
211k
  // Handle BYTE(), SHORT(), LONG(), or QUAD().
537
211k
  
if (auto *211k
Cmd211k
= dyn_cast<BytesDataCommand>(&Base)) {
538
27
    Cmd->Offset = Dot - CurAddressState->OutSec->Addr;
539
27
    Dot += Cmd->Size;
540
27
    CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr;
541
27
    return;
542
27
  }
543
211k
544
211k
  // Handle ASSERT().
545
211k
  
if (auto *211k
Cmd211k
= dyn_cast<AssertCommand>(&Base)) {
546
1
    Cmd->Expression();
547
1
    return;
548
1
  }
549
211k
550
211k
  // Handle a single input section description command.
551
211k
  // It calculates and assigns the offsets for each section and also
552
211k
  // updates the output section size.
553
211k
  auto &Cmd = cast<InputSectionDescription>(Base);
554
212k
  for (InputSection *Sec : Cmd.Sections) {
555
212k
    // We tentatively added all synthetic sections at the beginning and removed
556
212k
    // empty ones afterwards (because there is no way to know whether they were
557
212k
    // going be empty or not other than actually running linker scripts.)
558
212k
    // We need to ignore remains of empty sections.
559
212k
    if (auto *S = dyn_cast<SyntheticSection>(Sec))
560
12.4k
      
if (12.4k
S->empty()12.4k
)
561
0
        continue;
562
212k
563
212k
    
if (212k
!Sec->Live212k
)
564
0
      continue;
565
212k
    assert(CurAddressState->OutSec == Sec->getParent());
566
212k
    output(Sec);
567
212k
  }
568
211k
}
569
570
// This function searches for a memory region to place the given output
571
// section in. If found, a pointer to the appropriate memory region is
572
// returned. Otherwise, a nullptr is returned.
573
132k
MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) {
574
132k
  // If a memory region name was specified in the output section command,
575
132k
  // then try to find that region first.
576
132k
  if (
!Sec->MemoryRegionName.empty()132k
) {
577
16
    auto It = Opt.MemoryRegions.find(Sec->MemoryRegionName);
578
16
    if (It != Opt.MemoryRegions.end())
579
15
      return It->second;
580
1
    error("memory region '" + Sec->MemoryRegionName + "' not declared");
581
1
    return nullptr;
582
1
  }
583
132k
584
132k
  // If at least one memory region is defined, all sections must
585
132k
  // belong to some memory region. Otherwise, we don't need to do
586
132k
  // anything for memory regions.
587
132k
  
if (132k
Opt.MemoryRegions.empty()132k
)
588
132k
    return nullptr;
589
43
590
43
  // See if a region can be found by matching section flags.
591
43
  
for (auto &Pair : Opt.MemoryRegions) 43
{
592
79
    MemoryRegion *M = Pair.second;
593
79
    if (
(M->Flags & Sec->Flags) && 79
(M->NegFlags & Sec->Flags) == 02
)
594
2
      return M;
595
41
  }
596
41
597
41
  // Otherwise, no suitable region was found.
598
41
  
if (41
Sec->Flags & SHF_ALLOC41
)
599
1
    error("no memory region specified for section '" + Sec->Name + "'");
600
132k
  return nullptr;
601
132k
}
602
603
// This function assigns offsets to input sections and an output section
604
// for a single sections command (e.g. ".text { *(.text); }").
605
211k
void LinkerScript::assignOffsets(OutputSection *Sec) {
606
211k
  if (!(Sec->Flags & SHF_ALLOC))
607
137k
    Dot = 0;
608
74.0k
  else 
if (74.0k
Sec->AddrExpr74.0k
)
609
2.70k
    setDot(Sec->AddrExpr, Sec->Location, false);
610
211k
611
211k
  CurAddressState->MemRegion = Sec->MemRegion;
612
211k
  if (CurAddressState->MemRegion)
613
16
    Dot = CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
614
211k
615
211k
  if (
Sec->LMAExpr211k
) {
616
12
    uint64_t D = Dot;
617
37
    CurAddressState->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; };
618
12
  }
619
211k
620
211k
  switchTo(Sec);
621
211k
622
211k
  // We do not support custom layout for compressed debug sectons.
623
211k
  // At this point we already know their size and have compressed content.
624
211k
  if (CurAddressState->OutSec->Flags & SHF_COMPRESSED)
625
4
    return;
626
211k
627
211k
  for (BaseCommand *C : Sec->Commands)
628
211k
    process(*C);
629
211k
}
630
631
286
void LinkerScript::removeEmptyCommands() {
632
286
  // It is common practice to use very generic linker scripts. So for any
633
286
  // given run some of the output sections in the script will be empty.
634
286
  // We could create corresponding empty output sections, but that would
635
286
  // clutter the output.
636
286
  // We instead remove trivially empty sections. The bfd linker seems even
637
286
  // more aggressive at removing them.
638
132k
  llvm::erase_if(Opt.Commands, [&](BaseCommand *Base) {
639
132k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
640
132k
      return !Sec->Live;
641
239
    return false;
642
239
  });
643
286
}
644
645
33
static bool isAllSectionDescription(const OutputSection &Cmd) {
646
33
  for (BaseCommand *Base : Cmd.Commands)
647
31
    
if (31
!isa<InputSectionDescription>(*Base)31
)
648
5
      return false;
649
28
  return true;
650
28
}
651
652
287
void LinkerScript::adjustSectionsBeforeSorting() {
653
287
  // If the output section contains only symbol assignments, create a
654
287
  // corresponding output section. The bfd linker seems to only create them if
655
287
  // '.' is assigned to, but creating these section should not have any bad
656
287
  // consequeces and gives us a section to put the symbol in.
657
287
  uint64_t Flags = SHF_ALLOC;
658
287
659
132k
  for (BaseCommand * Cmd : Opt.Commands) {
660
132k
    auto *Sec = dyn_cast<OutputSection>(Cmd);
661
132k
    if (!Sec)
662
239
      continue;
663
132k
    
if (132k
Sec->Live132k
) {
664
132k
      Flags = Sec->Flags;
665
132k
      continue;
666
132k
    }
667
33
668
33
    
if (33
isAllSectionDescription(*Sec)33
)
669
28
      continue;
670
5
671
5
    Sec->Live = true;
672
5
    Sec->Flags = Flags;
673
5
  }
674
287
}
675
676
286
void LinkerScript::adjustSectionsAfterSorting() {
677
286
  // Try and find an appropriate memory region to assign offsets in.
678
132k
  for (BaseCommand *Base : Opt.Commands) {
679
132k
    if (auto *
Sec132k
= dyn_cast<OutputSection>(Base)) {
680
132k
      Sec->MemRegion = findMemoryRegion(Sec);
681
132k
      // Handle align (e.g. ".foo : ALIGN(16) { ... }").
682
132k
      if (Sec->AlignExpr)
683
8
        Sec->updateAlignment(Sec->AlignExpr().getValue());
684
132k
    }
685
132k
  }
686
286
687
286
  // If output section command doesn't specify any segments,
688
286
  // and we haven't previously assigned any section to segment,
689
286
  // then we simply assign section to the very first load segment.
690
286
  // Below is an example of such linker script:
691
286
  // PHDRS { seg PT_LOAD; }
692
286
  // SECTIONS { .aaa : { *(.aaa) } }
693
286
  std::vector<StringRef> DefPhdrs;
694
286
  auto FirstPtLoad =
695
286
      std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
696
20
                   [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
697
286
  if (FirstPtLoad != Opt.PhdrsCommands.end())
698
19
    DefPhdrs.push_back(FirstPtLoad->Name);
699
286
700
286
  // Walk the commands and propagate the program headers to commands that don't
701
286
  // explicitly specify them.
702
132k
  for (BaseCommand *Base : Opt.Commands) {
703
132k
    auto *Sec = dyn_cast<OutputSection>(Base);
704
132k
    if (!Sec)
705
239
      continue;
706
132k
707
132k
    
if (132k
Sec->Phdrs.empty()132k
) {
708
132k
      // To match the bfd linker script behaviour, only propagate program
709
132k
      // headers to sections that are allocated.
710
132k
      if (Sec->Flags & SHF_ALLOC)
711
66.2k
        Sec->Phdrs = DefPhdrs;
712
132k
    } else {
713
32
      DefPhdrs = Sec->Phdrs;
714
32
    }
715
132k
  }
716
286
717
286
  removeEmptyCommands();
718
286
}
719
720
231
static OutputSection *findFirstSection(PhdrEntry *Load) {
721
231
  for (OutputSection *Sec : OutputSections)
722
324
    
if (324
Sec->PtLoad == Load324
)
723
217
      return Sec;
724
14
  return nullptr;
725
14
}
726
727
// Try to find an address for the file and program headers output sections,
728
// which were unconditionally added to the first PT_LOAD segment earlier.
729
//
730
// When using the default layout, we check if the headers fit below the first
731
// allocated section. When using a linker script, we also check if the headers
732
// are covered by the output section. This allows omitting the headers by not
733
// leaving enough space for them in the linker script; this pattern is common
734
// in embedded systems.
735
//
736
// If there isn't enough space for these sections, we'll remove them from the
737
// PT_LOAD segment, and we'll also remove the PT_PHDR segment.
738
1.52k
void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) {
739
1.52k
  uint64_t Min = std::numeric_limits<uint64_t>::max();
740
1.52k
  for (OutputSection *Sec : OutputSections)
741
209k
    
if (209k
Sec->Flags & SHF_ALLOC209k
)
742
72.4k
      Min = std::min<uint64_t>(Min, Sec->Addr);
743
1.52k
744
1.52k
  auto It = llvm::find_if(
745
2.91k
      Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; });
746
1.52k
  if (It == Phdrs.end())
747
67
    return;
748
1.46k
  PhdrEntry *FirstPTLoad = *It;
749
1.46k
750
1.46k
  uint64_t HeaderSize = getHeaderSize();
751
1.46k
  // When linker script with SECTIONS is being used, don't output headers
752
1.46k
  // unless there's a space for them.
753
1.46k
  uint64_t Base = Opt.HasSections ? 
alignDown(Min, Config->MaxPageSize)280
:
01.18k
;
754
1.46k
  if (
HeaderSize <= Min - Base || 1.46k
Script->hasPhdrsCommands()237
) {
755
1.23k
    Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
756
1.23k
    Out::ElfHeader->Addr = Min;
757
1.23k
    Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
758
1.23k
    return;
759
1.23k
  }
760
231
761
231
  Out::ElfHeader->PtLoad = nullptr;
762
231
  Out::ProgramHeaders->PtLoad = nullptr;
763
231
  FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad);
764
231
765
231
  llvm::erase_if(Phdrs,
766
922
                 [](const PhdrEntry *E) { return E->p_type == PT_PHDR; });
767
1.52k
}
768
769
3.34k
LinkerScript::AddressState::AddressState(const ScriptConfiguration &Opt) {
770
37
  for (auto &MRI : Opt.MemoryRegions) {
771
37
    const MemoryRegion *MR = MRI.second;
772
37
    MemRegionOffset[MR] = MR->Origin;
773
37
  }
774
3.34k
}
775
776
1.74k
void LinkerScript::assignAddresses() {
777
1.74k
  // Assign addresses as instructed by linker script SECTIONS sub-commands.
778
1.74k
  Dot = 0;
779
1.74k
  auto State = make_unique<AddressState>(Opt);
780
1.74k
  // CurAddressState captures the local AddressState and makes it accessible
781
1.74k
  // deliberately. This is needed as there are some cases where we cannot just
782
1.74k
  // thread the current state through to a lambda function created by the
783
1.74k
  // script parser.
784
1.74k
  CurAddressState = State.get();
785
1.74k
  ErrorOnMissingSection = true;
786
1.74k
  switchTo(Aether);
787
1.74k
788
213k
  for (BaseCommand *Base : Opt.Commands) {
789
213k
    if (auto *
Cmd213k
= dyn_cast<SymbolAssignment>(Base)) {
790
1.70k
      assignSymbol(Cmd, false);
791
1.70k
      continue;
792
1.70k
    }
793
211k
794
211k
    
if (auto *211k
Cmd211k
= dyn_cast<AssertCommand>(Base)) {
795
4
      Cmd->Expression();
796
4
      continue;
797
4
    }
798
211k
799
211k
    assignOffsets(cast<OutputSection>(Base));
800
211k
  }
801
1.74k
  CurAddressState = nullptr;
802
1.74k
}
803
804
// Creates program headers as instructed by PHDRS linker script command.
805
23
std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
806
23
  std::vector<PhdrEntry *> Ret;
807
23
808
23
  // Process PHDRS and FILEHDR keywords because they are not
809
23
  // real output sections and cannot be added in the following loop.
810
30
  for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
811
30
    PhdrEntry *Phdr =
812
30
        make<PhdrEntry>(Cmd.Type, Cmd.Flags == UINT_MAX ? 
PF_R26
:
Cmd.Flags4
);
813
30
814
30
    if (Cmd.HasFilehdr)
815
10
      Phdr->add(Out::ElfHeader);
816
30
    if (Cmd.HasPhdrs)
817
12
      Phdr->add(Out::ProgramHeaders);
818
30
819
30
    if (
Cmd.LMAExpr30
) {
820
1
      Phdr->p_paddr = Cmd.LMAExpr().getValue();
821
1
      Phdr->HasLMA = true;
822
1
    }
823
30
    Ret.push_back(Phdr);
824
30
  }
825
23
826
23
  // Add output sections to program headers.
827
154
  for (OutputSection *Sec : OutputSections) {
828
154
    // Assign headers specified by linker script
829
54
    for (size_t Id : getPhdrIndices(Sec)) {
830
54
      Ret[Id]->add(Sec);
831
54
      if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
832
44
        Ret[Id]->p_flags |= Sec->getPhdrFlags();
833
54
    }
834
154
  }
835
23
  return Ret;
836
23
}
837
838
5
bool LinkerScript::ignoreInterpSection() {
839
5
  // Ignore .interp section in case we have PHDRS specification
840
5
  // and PT_INTERP isn't listed.
841
5
  if (Opt.PhdrsCommands.empty())
842
4
    return false;
843
1
  for (PhdrsCommand &Cmd : Opt.PhdrsCommands)
844
1
    
if (1
Cmd.Type == PT_INTERP1
)
845
0
      return false;
846
1
  return true;
847
1
}
848
849
213
ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
850
213
  if (
S == "."213
) {
851
125
    if (CurAddressState)
852
124
      return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr,
853
124
              Loc};
854
1
    error(Loc + ": unable to get location counter value");
855
1
    return 0;
856
1
  }
857
88
  
if (SymbolBody *88
B88
= Symtab->find(S)) {
858
88
    if (auto *D = dyn_cast<DefinedRegular>(B))
859
82
      return {D->Section, D->Value, Loc};
860
6
    
if (auto *6
C6
= dyn_cast<DefinedCommon>(B))
861
4
      return {C->Section, 0, Loc};
862
2
  }
863
2
  error(Loc + ": symbol not found: " + S);
864
2
  return 0;
865
2
}
866
867
2
bool LinkerScript::isDefined(StringRef S) { return Symtab->find(S) != nullptr; }
868
869
static const size_t NoPhdr = -1;
870
871
// Returns indices of ELF headers containing specific section. Each index is a
872
// zero based number of ELF header listed within PHDRS {} script block.
873
154
std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) {
874
154
  std::vector<size_t> Ret;
875
56
  for (StringRef PhdrName : Cmd->Phdrs) {
876
56
    size_t Index = getPhdrIndex(Cmd->Location, PhdrName);
877
56
    if (Index != NoPhdr)
878
54
      Ret.push_back(Index);
879
56
  }
880
154
  return Ret;
881
154
}
882
883
// Returns the index of the segment named PhdrName if found otherwise
884
// NoPhdr. When not found, if PhdrName is not the special case value 'NONE'
885
// (which can be used to explicitly specify that a section isn't assigned to a
886
// segment) then error.
887
56
size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
888
56
  size_t I = 0;
889
63
  for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
890
63
    if (Cmd.Name == PhdrName)
891
54
      return I;
892
9
    ++I;
893
9
  }
894
2
  
if (2
PhdrName != "NONE"2
)
895
1
    error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
896
2
  return NoPhdr;
897
56
}