Coverage Report

Created: 2017-09-19 22:28

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