Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Writer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Writer.cpp ---------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "Writer.h"
10
#include "AArch64ErrataFix.h"
11
#include "CallGraphSort.h"
12
#include "Config.h"
13
#include "LinkerScript.h"
14
#include "MapFile.h"
15
#include "OutputSections.h"
16
#include "Relocations.h"
17
#include "SymbolTable.h"
18
#include "Symbols.h"
19
#include "SyntheticSections.h"
20
#include "Target.h"
21
#include "lld/Common/Filesystem.h"
22
#include "lld/Common/Memory.h"
23
#include "lld/Common/Strings.h"
24
#include "lld/Common/Threads.h"
25
#include "llvm/ADT/StringMap.h"
26
#include "llvm/ADT/StringSwitch.h"
27
#include "llvm/Support/RandomNumberGenerator.h"
28
#include "llvm/Support/SHA1.h"
29
#include "llvm/Support/xxhash.h"
30
#include <climits>
31
32
using namespace llvm;
33
using namespace llvm::ELF;
34
using namespace llvm::object;
35
using namespace llvm::support;
36
using namespace llvm::support::endian;
37
38
using namespace lld;
39
using namespace lld::elf;
40
41
namespace {
42
// The writer writes a SymbolTable result to a file.
43
template <class ELFT> class Writer {
44
public:
45
2.52k
  Writer() : Buffer(errorHandler().OutputBuffer) {}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::Writer()
Line
Count
Source
45
319
  Writer() : Buffer(errorHandler().OutputBuffer) {}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::Writer()
Line
Count
Source
45
133
  Writer() : Buffer(errorHandler().OutputBuffer) {}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::Writer()
Line
Count
Source
45
1.96k
  Writer() : Buffer(errorHandler().OutputBuffer) {}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::Writer()
Line
Count
Source
45
115
  Writer() : Buffer(errorHandler().OutputBuffer) {}
46
  using Elf_Shdr = typename ELFT::Shdr;
47
  using Elf_Ehdr = typename ELFT::Ehdr;
48
  using Elf_Phdr = typename ELFT::Phdr;
49
50
  void run();
51
52
private:
53
  void copyLocalSymbols();
54
  void addSectionSymbols();
55
  void forEachRelSec(llvm::function_ref<void(InputSectionBase &)> Fn);
56
  void sortSections();
57
  void resolveShfLinkOrder();
58
  void finalizeAddressDependentContent();
59
  void sortInputSections();
60
  void finalizeSections();
61
  void checkExecuteOnly();
62
  void setReservedSymbolSections();
63
64
  std::vector<PhdrEntry *> createPhdrs();
65
  void removeEmptyPTLoad();
66
  void addPhdrForSection(std::vector<PhdrEntry *> &Phdrs, unsigned ShType,
67
                         unsigned PType, unsigned PFlags);
68
  void assignFileOffsets();
69
  void assignFileOffsetsBinary();
70
  void setPhdrs();
71
  void checkSections();
72
  void fixSectionAlignments();
73
  void openFile();
74
  void writeTrapInstr();
75
  void writeHeader();
76
  void writeSections();
77
  void writeSectionsBinary();
78
  void writeBuildId();
79
80
  std::unique_ptr<FileOutputBuffer> &Buffer;
81
82
  void addRelIpltSymbols();
83
  void addStartEndSymbols();
84
  void addStartStopSymbols(OutputSection *Sec);
85
86
  std::vector<PhdrEntry *> Phdrs;
87
88
  uint64_t FileSize;
89
  uint64_t SectionHeaderOff;
90
};
91
} // anonymous namespace
92
93
67.5M
static bool isSectionPrefix(StringRef Prefix, StringRef Name) {
94
67.5M
  return Name.startswith(Prefix) || 
Name == Prefix.drop_back()67.5M
;
95
67.5M
}
96
97
4.58M
StringRef elf::getOutputSectionName(const InputSectionBase *S) {
98
4.58M
  if (Config->Relocatable)
99
67.0k
    return S->Name;
100
4.51M
101
4.51M
  // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
102
4.51M
  // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
103
4.51M
  // technically required, but not doing it is odd). This code guarantees that.
104
4.51M
  if (auto *IS = dyn_cast<InputSection>(S)) {
105
4.51M
    if (InputSectionBase *Rel = IS->getRelocatedSection()) {
106
27
      OutputSection *Out = Rel->getOutputSection();
107
27
      if (S->Type == SHT_RELA)
108
26
        return Saver.save(".rela" + Out->Name);
109
1
      return Saver.save(".rel" + Out->Name);
110
1
    }
111
4.51M
  }
112
4.51M
113
4.51M
  // This check is for -z keep-text-section-prefix.  This option separates text
114
4.51M
  // sections with prefix ".text.hot", ".text.unlikely", ".text.startup" or
115
4.51M
  // ".text.exit".
116
4.51M
  // When enabled, this allows identifying the hot code region (.text.hot) in
117
4.51M
  // the final binary which can be selectively mapped to huge pages or mlocked,
118
4.51M
  // for instance.
119
4.51M
  if (Config->ZKeepTextSectionPrefix)
120
22
    for (StringRef V :
121
22
         {".text.hot.", ".text.unlikely.", ".text.startup.", ".text.exit."})
122
82
      if (isSectionPrefix(V, S->Name))
123
4
        return V.drop_back();
124
4.51M
125
4.51M
  for (StringRef V :
126
4.51M
       {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
127
4.51M
        ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
128
4.51M
        ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."})
129
67.5M
    if (isSectionPrefix(V, S->Name))
130
12.0k
      return V.drop_back();
131
4.51M
132
4.51M
  // CommonSection is identified as "COMMON" in linker scripts.
133
4.51M
  // By default, it should go to .bss section.
134
4.51M
  
if (4.50M
S->Name == "COMMON"4.50M
)
135
82
    return ".bss";
136
4.50M
137
4.50M
  return S->Name;
138
4.50M
}
139
140
4.97k
static bool needsInterpSection() {
141
4.97k
  return !SharedFiles.empty() && 
!Config->DynamicLinker.empty()680
&&
142
4.97k
         
Script->needsInterpSection()24
;
143
4.97k
}
144
145
2.52k
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
void lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
145
319
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
void lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
145
133
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
void lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
145
1.96k
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
void lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
145
115
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
146
147
2.38k
template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() {
148
13.3k
  llvm::erase_if(Phdrs, [&](const PhdrEntry *P) {
149
13.3k
    if (P->p_type != PT_LOAD)
150
7.06k
      return false;
151
6.27k
    if (!P->FirstSec)
152
41
      return true;
153
6.23k
    uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr;
154
6.23k
    return Size == 0;
155
6.23k
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::removeEmptyPTLoad()::'lambda'(lld::elf::PhdrEntry const*)::operator()(lld::elf::PhdrEntry const*) const
Line
Count
Source
148
1.81k
  llvm::erase_if(Phdrs, [&](const PhdrEntry *P) {
149
1.81k
    if (P->p_type != PT_LOAD)
150
977
      return false;
151
838
    if (!P->FirstSec)
152
4
      return true;
153
834
    uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr;
154
834
    return Size == 0;
155
834
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::removeEmptyPTLoad()::'lambda'(lld::elf::PhdrEntry const*)::operator()(lld::elf::PhdrEntry const*) const
Line
Count
Source
148
888
  llvm::erase_if(Phdrs, [&](const PhdrEntry *P) {
149
888
    if (P->p_type != PT_LOAD)
150
538
      return false;
151
350
    if (!P->FirstSec)
152
0
      return true;
153
350
    uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr;
154
350
    return Size == 0;
155
350
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::removeEmptyPTLoad()::'lambda'(lld::elf::PhdrEntry const*)::operator()(lld::elf::PhdrEntry const*) const
Line
Count
Source
148
9.85k
  llvm::erase_if(Phdrs, [&](const PhdrEntry *P) {
149
9.85k
    if (P->p_type != PT_LOAD)
150
5.13k
      return false;
151
4.71k
    if (!P->FirstSec)
152
37
      return true;
153
4.67k
    uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr;
154
4.67k
    return Size == 0;
155
4.67k
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::removeEmptyPTLoad()::'lambda'(lld::elf::PhdrEntry const*)::operator()(lld::elf::PhdrEntry const*) const
Line
Count
Source
148
787
  llvm::erase_if(Phdrs, [&](const PhdrEntry *P) {
149
787
    if (P->p_type != PT_LOAD)
150
414
      return false;
151
373
    if (!P->FirstSec)
152
0
      return true;
153
373
    uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr;
154
373
    return Size == 0;
155
373
  });
156
2.38k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::removeEmptyPTLoad()
Line
Count
Source
147
313
template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() {
148
313
  llvm::erase_if(Phdrs, [&](const PhdrEntry *P) {
149
313
    if (P->p_type != PT_LOAD)
150
313
      return false;
151
313
    if (!P->FirstSec)
152
313
      return true;
153
313
    uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr;
154
313
    return Size == 0;
155
313
  });
156
313
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::removeEmptyPTLoad()
Line
Count
Source
147
130
template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() {
148
130
  llvm::erase_if(Phdrs, [&](const PhdrEntry *P) {
149
130
    if (P->p_type != PT_LOAD)
150
130
      return false;
151
130
    if (!P->FirstSec)
152
130
      return true;
153
130
    uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr;
154
130
    return Size == 0;
155
130
  });
156
130
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::removeEmptyPTLoad()
Line
Count
Source
147
1.83k
template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() {
148
1.83k
  llvm::erase_if(Phdrs, [&](const PhdrEntry *P) {
149
1.83k
    if (P->p_type != PT_LOAD)
150
1.83k
      return false;
151
1.83k
    if (!P->FirstSec)
152
1.83k
      return true;
153
1.83k
    uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr;
154
1.83k
    return Size == 0;
155
1.83k
  });
156
1.83k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::removeEmptyPTLoad()
Line
Count
Source
147
108
template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() {
148
108
  llvm::erase_if(Phdrs, [&](const PhdrEntry *P) {
149
108
    if (P->p_type != PT_LOAD)
150
108
      return false;
151
108
    if (!P->FirstSec)
152
108
      return true;
153
108
    uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr;
154
108
    return Size == 0;
155
108
  });
156
108
}
157
158
2.44k
template <class ELFT> static void combineEhSections() {
159
317k
  for (InputSectionBase *&S : InputSections) {
160
317k
    if (!S->Live)
161
678
      continue;
162
317k
163
317k
    if (auto *ES = dyn_cast<EhInputSection>(S)) {
164
115
      In.EhFrame->addSection<ELFT>(ES);
165
115
      S = nullptr;
166
317k
    } else if (S->kind() == SectionBase::Regular && 
In.ARMExidx269k
&&
167
317k
               
In.ARMExidx->addSection(cast<InputSection>(S))833
) {
168
71
      S = nullptr;
169
71
    }
170
317k
  }
171
2.44k
172
2.44k
  std::vector<InputSectionBase *> &V = InputSections;
173
2.44k
  V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
174
2.44k
}
Writer.cpp:void combineEhSections<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
158
308
template <class ELFT> static void combineEhSections() {
159
7.27k
  for (InputSectionBase *&S : InputSections) {
160
7.27k
    if (!S->Live)
161
63
      continue;
162
7.21k
163
7.21k
    if (auto *ES = dyn_cast<EhInputSection>(S)) {
164
4
      In.EhFrame->addSection<ELFT>(ES);
165
4
      S = nullptr;
166
7.21k
    } else if (S->kind() == SectionBase::Regular && 
In.ARMExidx1.13k
&&
167
7.21k
               
In.ARMExidx->addSection(cast<InputSection>(S))833
) {
168
71
      S = nullptr;
169
71
    }
170
7.21k
  }
171
308
172
308
  std::vector<InputSectionBase *> &V = InputSections;
173
308
  V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
174
308
}
Writer.cpp:void combineEhSections<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
158
122
template <class ELFT> static void combineEhSections() {
159
3.40k
  for (InputSectionBase *&S : InputSections) {
160
3.40k
    if (!S->Live)
161
304
      continue;
162
3.09k
163
3.09k
    if (auto *ES = dyn_cast<EhInputSection>(S)) {
164
0
      In.EhFrame->addSection<ELFT>(ES);
165
0
      S = nullptr;
166
3.09k
    } else if (S->kind() == SectionBase::Regular && 
In.ARMExidx510
&&
167
3.09k
               
In.ARMExidx->addSection(cast<InputSection>(S))0
) {
168
0
      S = nullptr;
169
0
    }
170
3.09k
  }
171
122
172
122
  std::vector<InputSectionBase *> &V = InputSections;
173
122
  V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
174
122
}
Writer.cpp:void combineEhSections<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
158
1.90k
template <class ELFT> static void combineEhSections() {
159
304k
  for (InputSectionBase *&S : InputSections) {
160
304k
    if (!S->Live)
161
210
      continue;
162
304k
163
304k
    if (auto *ES = dyn_cast<EhInputSection>(S)) {
164
107
      In.EhFrame->addSection<ELFT>(ES);
165
107
      S = nullptr;
166
304k
    } else if (S->kind() == SectionBase::Regular && 
In.ARMExidx267k
&&
167
304k
               
In.ARMExidx->addSection(cast<InputSection>(S))0
) {
168
0
      S = nullptr;
169
0
    }
170
304k
  }
171
1.90k
172
1.90k
  std::vector<InputSectionBase *> &V = InputSections;
173
1.90k
  V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
174
1.90k
}
Writer.cpp:void combineEhSections<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
158
112
template <class ELFT> static void combineEhSections() {
159
2.70k
  for (InputSectionBase *&S : InputSections) {
160
2.70k
    if (!S->Live)
161
101
      continue;
162
2.59k
163
2.59k
    if (auto *ES = dyn_cast<EhInputSection>(S)) {
164
4
      In.EhFrame->addSection<ELFT>(ES);
165
4
      S = nullptr;
166
2.59k
    } else if (S->kind() == SectionBase::Regular && 
In.ARMExidx294
&&
167
2.59k
               
In.ARMExidx->addSection(cast<InputSection>(S))0
) {
168
0
      S = nullptr;
169
0
    }
170
2.59k
  }
171
112
172
112
  std::vector<InputSectionBase *> &V = InputSections;
173
112
  V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
174
112
}
175
176
static Defined *addOptionalRegular(StringRef Name, SectionBase *Sec,
177
                                   uint64_t Val, uint8_t StOther = STV_HIDDEN,
178
564k
                                   uint8_t Binding = STB_GLOBAL) {
179
564k
  Symbol *S = Symtab->find(Name);
180
564k
  if (!S || 
S->isDefined()107
)
181
564k
    return nullptr;
182
99
183
99
  return cast<Defined>(Symtab->addSymbol(
184
99
      Defined{/*File=*/nullptr, Name, Binding, StOther, STT_NOTYPE, Val,
185
99
              /*Size=*/0, Sec}));
186
99
}
187
188
192
static Defined *addAbsolute(StringRef Name) {
189
192
  Symbol *Sym = Symtab->addSymbol(Defined{nullptr, Name, STB_GLOBAL, STV_HIDDEN,
190
192
                                          STT_NOTYPE, 0, 0, nullptr});
191
192
  if (!Sym->isDefined())
192
0
    error("duplicate symbol: " + toString(*Sym));
193
192
  return cast<Defined>(Sym);
194
192
}
195
196
// The linker is expected to define some symbols depending on
197
// the linking result. This function defines such symbols.
198
2.47k
void elf::addReservedSymbols() {
199
2.47k
  if (Config->EMachine == EM_MIPS) {
200
183
    // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
201
183
    // so that it points to an absolute address which by default is relative
202
183
    // to GOT. Default offset is 0x7ff0.
203
183
    // See "Global Data Symbols" in Chapter 6 in the following document:
204
183
    // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
205
183
    ElfSym::MipsGp = addAbsolute("_gp");
206
183
207
183
    // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
208
183
    // start of function and 'gp' pointer into GOT.
209
183
    if (Symtab->find("_gp_disp"))
210
8
      ElfSym::MipsGpDisp = addAbsolute("_gp_disp");
211
183
212
183
    // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
213
183
    // pointer. This symbol is used in the code generated by .cpload pseudo-op
214
183
    // in case of using -mno-shared option.
215
183
    // https://sourceware.org/ml/binutils/2004-12/msg00094.html
216
183
    if (Symtab->find("__gnu_local_gp"))
217
1
      ElfSym::MipsLocalGp = addAbsolute("__gnu_local_gp");
218
183
  }
219
2.47k
220
2.47k
  // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which
221
2.47k
  // combines the typical ELF GOT with the small data sections. It commonly
222
2.47k
  // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both
223
2.47k
  // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to
224
2.47k
  // represent the TOC base which is offset by 0x8000 bytes from the start of
225
2.47k
  // the .got section.
226
2.47k
  // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the
227
2.47k
  // correctness of some relocations depends on its value.
228
2.47k
  StringRef GotSymName =
229
2.47k
      (Config->EMachine == EM_PPC64) ? 
".TOC."155
:
"_GLOBAL_OFFSET_TABLE_"2.31k
;
230
2.47k
231
2.47k
  if (Symbol *S = Symtab->find(GotSymName)) {
232
122
    if (S->isDefined()) {
233
1
      error(toString(S->File) + " cannot redefine linker defined symbol '" +
234
1
            GotSymName + "'");
235
1
      return;
236
1
    }
237
121
238
121
    uint64_t GotOff = 0;
239
121
    if (Config->EMachine == EM_PPC || Config->EMachine == EM_PPC64)
240
87
      GotOff = 0x8000;
241
121
242
121
    Symtab->addSymbol(Defined{/*File=*/nullptr, GotSymName, STB_GLOBAL,
243
121
                              STV_HIDDEN, STT_NOTYPE, GotOff, /*Size=*/0,
244
121
                              Out::ElfHeader});
245
121
    ElfSym::GlobalOffsetTable = cast<Defined>(S);
246
121
  }
247
2.47k
248
2.47k
  // __ehdr_start is the location of ELF file headers. Note that we define
249
2.47k
  // this symbol unconditionally even when using a linker script, which
250
2.47k
  // differs from the behavior implemented by GNU linker which only define
251
2.47k
  // this symbol if ELF headers are in the memory mapped segment.
252
2.47k
  addOptionalRegular("__ehdr_start", Out::ElfHeader, 0, STV_HIDDEN);
253
2.47k
254
2.47k
  // __executable_start is not documented, but the expectation of at
255
2.47k
  // least the Android libc is that it points to the ELF header.
256
2.47k
  addOptionalRegular("__executable_start", Out::ElfHeader, 0, STV_HIDDEN);
257
2.47k
258
2.47k
  // __dso_handle symbol is passed to cxa_finalize as a marker to identify
259
2.47k
  // each DSO. The address of the symbol doesn't matter as long as they are
260
2.47k
  // different in different DSOs, so we chose the start address of the DSO.
261
2.47k
  addOptionalRegular("__dso_handle", Out::ElfHeader, 0, STV_HIDDEN);
262
2.47k
263
2.47k
  // If linker script do layout we do not need to create any standart symbols.
264
2.47k
  if (Script->HasSectionsCommand)
265
457
    return;
266
2.01k
267
14.1k
  
auto Add = [](StringRef S, int64_t Pos) 2.01k
{
268
14.1k
    return addOptionalRegular(S, Out::ElfHeader, Pos, STV_DEFAULT);
269
14.1k
  };
270
2.01k
271
2.01k
  ElfSym::Bss = Add("__bss_start", 0);
272
2.01k
  ElfSym::End1 = Add("end", -1);
273
2.01k
  ElfSym::End2 = Add("_end", -1);
274
2.01k
  ElfSym::Etext1 = Add("etext", -1);
275
2.01k
  ElfSym::Etext2 = Add("_etext", -1);
276
2.01k
  ElfSym::Edata1 = Add("edata", -1);
277
2.01k
  ElfSym::Edata2 = Add("_edata", -1);
278
2.01k
}
279
280
18.6k
static OutputSection *findSection(StringRef Name) {
281
18.6k
  for (BaseCommand *Base : Script->SectionCommands)
282
2.01M
    if (auto *Sec = dyn_cast<OutputSection>(Base))
283
2.00M
      if (Sec->Name == Name)
284
3.66k
        return Sec;
285
18.6k
  
return nullptr14.9k
;
286
18.6k
}
287
288
// Initialize Out members.
289
2.52k
template <class ELFT> static void createSyntheticSections() {
290
2.52k
  // Initialize all pointers with NULL. This is needed because
291
2.52k
  // you can call lld::elf::main more than once as a library.
292
2.52k
  memset(&Out::First, 0, sizeof(Out));
293
2.52k
294
45.8k
  auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)1, false> >()::'lambda'(lld::elf::InputSectionBase*)::operator()(lld::elf::InputSectionBase*) const
Line
Count
Source
294
5.91k
  auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda'(lld::elf::InputSectionBase*)::operator()(lld::elf::InputSectionBase*) const
Line
Count
Source
294
2.64k
  auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)1, true> >()::'lambda'(lld::elf::InputSectionBase*)::operator()(lld::elf::InputSectionBase*) const
Line
Count
Source
294
35.0k
  auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda'(lld::elf::InputSectionBase*)::operator()(lld::elf::InputSectionBase*) const
Line
Count
Source
294
2.23k
  auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
295
2.52k
296
2.52k
  In.DynStrTab = make<StringTableSection>(".dynstr", true);
297
2.52k
  In.Dynamic = make<DynamicSection<ELFT>>();
298
2.52k
  if (Config->AndroidPackDynRelocs) {
299
5
    In.RelaDyn = make<AndroidPackedRelocationSection<ELFT>>(
300
5
        Config->IsRela ? 
".rela.dyn"4
:
".rel.dyn"1
);
301
2.52k
  } else {
302
2.52k
    In.RelaDyn = make<RelocationSection<ELFT>>(
303
2.52k
        Config->IsRela ? 
".rela.dyn"2.03k
:
".rel.dyn"484
, Config->ZCombreloc);
304
2.52k
  }
305
2.52k
  In.ShStrTab = make<StringTableSection>(".shstrtab", false);
306
2.52k
307
2.52k
  Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
308
2.52k
  Out::ProgramHeaders->Alignment = Config->Wordsize;
309
2.52k
310
2.52k
  if (needsInterpSection())
311
10
    Add(createInterpSection());
312
2.52k
313
2.52k
  if (Config->Strip != StripPolicy::All) {
314
2.52k
    In.StrTab = make<StringTableSection>(".strtab", false);
315
2.52k
    In.SymTab = make<SymbolTableSection<ELFT>>(*In.StrTab);
316
2.52k
    In.SymTabShndx = make<SymtabShndxSection>();
317
2.52k
  }
318
2.52k
319
2.52k
  if (Config->BuildId != BuildIdKind::None) {
320
17
    In.BuildId = make<BuildIdSection>();
321
17
    Add(In.BuildId);
322
17
  }
323
2.52k
324
2.52k
  In.Bss = make<BssSection>(".bss", 0, 1);
325
2.52k
  Add(In.Bss);
326
2.52k
327
2.52k
  // If there is a SECTIONS command and a .data.rel.ro section name use name
328
2.52k
  // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
329
2.52k
  // This makes sure our relro is contiguous.
330
2.52k
  bool HasDataRelRo = Script->HasSectionsCommand && 
findSection(".data.rel.ro")458
;
331
2.52k
  In.BssRelRo =
332
2.52k
      make<BssSection>(HasDataRelRo ? 
".data.rel.ro.bss"6
:
".bss.rel.ro"2.52k
, 0, 1);
333
2.52k
  Add(In.BssRelRo);
334
2.52k
335
2.52k
  // Add MIPS-specific sections.
336
2.52k
  if (Config->EMachine == EM_MIPS) {
337
197
    if (!Config->Shared && 
Config->HasDynSymTab118
) {
338
29
      In.MipsRldMap = make<MipsRldMapSection>();
339
29
      Add(In.MipsRldMap);
340
29
    }
341
197
    if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
342
191
      Add(Sec);
343
197
    if (auto *Sec = MipsOptionsSection<ELFT>::create())
344
42
      Add(Sec);
345
197
    if (auto *Sec = MipsReginfoSection<ELFT>::create())
346
145
      Add(Sec);
347
197
  }
348
2.52k
349
2.52k
  if (Config->HasDynSymTab) {
350
1.23k
    In.DynSymTab = make<SymbolTableSection<ELFT>>(*In.DynStrTab);
351
1.23k
    Add(In.DynSymTab);
352
1.23k
353
1.23k
    In.VerSym = make<VersionTableSection>();
354
1.23k
    Add(In.VerSym);
355
1.23k
356
1.23k
    if (!Config->VersionDefinitions.empty()) {
357
53
      In.VerDef = make<VersionDefinitionSection>();
358
53
      Add(In.VerDef);
359
53
    }
360
1.23k
361
1.23k
    In.VerNeed = make<VersionNeedSection<ELFT>>();
362
1.23k
    Add(In.VerNeed);
363
1.23k
364
1.23k
    if (Config->GnuHash) {
365
990
      In.GnuHashTab = make<GnuHashTableSection>();
366
990
      Add(In.GnuHashTab);
367
990
    }
368
1.23k
369
1.23k
    if (Config->SysvHash) {
370
1.22k
      In.HashTab = make<HashTableSection>();
371
1.22k
      Add(In.HashTab);
372
1.22k
    }
373
1.23k
374
1.23k
    Add(In.Dynamic);
375
1.23k
    Add(In.DynStrTab);
376
1.23k
    Add(In.RelaDyn);
377
1.23k
  }
378
2.52k
379
2.52k
  if (Config->RelrPackDynRelocs) {
380
4
    In.RelrDyn = make<RelrSection<ELFT>>();
381
4
    Add(In.RelrDyn);
382
4
  }
383
2.52k
384
2.52k
  // Add .got. MIPS' .got is so different from the other archs,
385
2.52k
  // it has its own class.
386
2.52k
  if (Config->EMachine == EM_MIPS) {
387
197
    In.MipsGot = make<MipsGotSection>();
388
197
    Add(In.MipsGot);
389
2.33k
  } else {
390
2.33k
    In.Got = make<GotSection>();
391
2.33k
    Add(In.Got);
392
2.33k
  }
393
2.52k
394
2.52k
  if (Config->EMachine == EM_PPC64) {
395
156
    In.PPC64LongBranchTarget = make<PPC64LongBranchTargetSection>();
396
156
    Add(In.PPC64LongBranchTarget);
397
156
  }
398
2.52k
399
2.52k
  In.GotPlt = make<GotPltSection>();
400
2.52k
  Add(In.GotPlt);
401
2.52k
  In.IgotPlt = make<IgotPltSection>();
402
2.52k
  Add(In.IgotPlt);
403
2.52k
404
2.52k
  // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
405
2.52k
  // it as a relocation and ensure the referenced section is created.
406
2.52k
  if (ElfSym::GlobalOffsetTable && 
Config->EMachine != EM_MIPS121
) {
407
120
    if (Target->GotBaseSymInGotPlt)
408
31
      In.GotPlt->HasGotPltOffRel = true;
409
89
    else
410
89
      In.Got->HasGotOffRel = true;
411
120
  }
412
2.52k
413
2.52k
  if (Config->GdbIndex)
414
17
    Add(GdbIndexSection::create<ELFT>());
415
2.52k
416
2.52k
  // We always need to add rel[a].plt to output if it has entries.
417
2.52k
  // Even for static linking it can contain R_[*]_IRELATIVE relocations.
418
2.52k
  In.RelaPlt = make<RelocationSection<ELFT>>(
419
2.52k
      Config->IsRela ? 
".rela.plt"2.04k
:
".rel.plt"485
, false /*Sort*/);
420
2.52k
  Add(In.RelaPlt);
421
2.52k
422
2.52k
  // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
423
2.52k
  // that the IRelative relocations are processed last by the dynamic loader.
424
2.52k
  // We cannot place the iplt section in .rel.dyn when Android relocation
425
2.52k
  // packing is enabled because that would cause a section type mismatch.
426
2.52k
  // However, because the Android dynamic loader reads .rel.plt after .rel.dyn,
427
2.52k
  // we can get the desired behaviour by placing the iplt section in .rel.plt.
428
2.52k
  In.RelaIplt = make<RelocationSection<ELFT>>(
429
2.52k
      (Config->EMachine == EM_ARM && 
!Config->AndroidPackDynRelocs155
)
430
2.52k
          ? 
".rel.dyn"154
431
2.52k
          : 
In.RelaPlt->Name2.37k
,
432
2.52k
      false /*Sort*/);
433
2.52k
  Add(In.RelaIplt);
434
2.52k
435
2.52k
  In.Plt = make<PltSection>(false);
436
2.52k
  Add(In.Plt);
437
2.52k
  In.Iplt = make<PltSection>(true);
438
2.52k
  Add(In.Iplt);
439
2.52k
440
2.52k
  // .note.GNU-stack is always added when we are creating a re-linkable
441
2.52k
  // object file. Other linkers are using the presence of this marker
442
2.52k
  // section to control the executable-ness of the stack area, but that
443
2.52k
  // is irrelevant these days. Stack area should always be non-executable
444
2.52k
  // by default. So we emit this section unconditionally.
445
2.52k
  if (Config->Relocatable)
446
82
    Add(make<GnuStackSection>());
447
2.52k
448
2.52k
  if (!Config->Relocatable) {
449
2.44k
    if (Config->EhFrameHdr) {
450
30
      In.EhFrameHdr = make<EhFrameHeader>();
451
30
      Add(In.EhFrameHdr);
452
30
    }
453
2.44k
    In.EhFrame = make<EhFrameSection>();
454
2.44k
    Add(In.EhFrame);
455
2.44k
  }
456
2.52k
457
2.52k
  if (In.SymTab)
458
2.52k
    Add(In.SymTab);
459
2.52k
  if (In.SymTabShndx)
460
2.52k
    Add(In.SymTabShndx);
461
2.52k
  Add(In.ShStrTab);
462
2.52k
  if (In.StrTab)
463
2.52k
    Add(In.StrTab);
464
2.52k
465
2.52k
  if (Config->EMachine == EM_ARM && 
!Config->Relocatable155
) {
466
150
    // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
467
150
    // InputSections.
468
150
    In.ARMExidx = make<ARMExidxSyntheticSection>();
469
150
    Add(In.ARMExidx);
470
150
  }
471
2.52k
}
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
289
319
template <class ELFT> static void createSyntheticSections() {
290
319
  // Initialize all pointers with NULL. This is needed because
291
319
  // you can call lld::elf::main more than once as a library.
292
319
  memset(&Out::First, 0, sizeof(Out));
293
319
294
319
  auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
295
319
296
319
  In.DynStrTab = make<StringTableSection>(".dynstr", true);
297
319
  In.Dynamic = make<DynamicSection<ELFT>>();
298
319
  if (Config->AndroidPackDynRelocs) {
299
1
    In.RelaDyn = make<AndroidPackedRelocationSection<ELFT>>(
300
1
        Config->IsRela ? 
".rela.dyn"0
: ".rel.dyn");
301
318
  } else {
302
318
    In.RelaDyn = make<RelocationSection<ELFT>>(
303
318
        Config->IsRela ? 
".rela.dyn"9
:
".rel.dyn"309
, Config->ZCombreloc);
304
318
  }
305
319
  In.ShStrTab = make<StringTableSection>(".shstrtab", false);
306
319
307
319
  Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
308
319
  Out::ProgramHeaders->Alignment = Config->Wordsize;
309
319
310
319
  if (needsInterpSection())
311
1
    Add(createInterpSection());
312
319
313
319
  if (Config->Strip != StripPolicy::All) {
314
319
    In.StrTab = make<StringTableSection>(".strtab", false);
315
319
    In.SymTab = make<SymbolTableSection<ELFT>>(*In.StrTab);
316
319
    In.SymTabShndx = make<SymtabShndxSection>();
317
319
  }
318
319
319
319
  if (Config->BuildId != BuildIdKind::None) {
320
0
    In.BuildId = make<BuildIdSection>();
321
0
    Add(In.BuildId);
322
0
  }
323
319
324
319
  In.Bss = make<BssSection>(".bss", 0, 1);
325
319
  Add(In.Bss);
326
319
327
319
  // If there is a SECTIONS command and a .data.rel.ro section name use name
328
319
  // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
329
319
  // This makes sure our relro is contiguous.
330
319
  bool HasDataRelRo = Script->HasSectionsCommand && 
findSection(".data.rel.ro")43
;
331
319
  In.BssRelRo =
332
319
      make<BssSection>(HasDataRelRo ? 
".data.rel.ro.bss"0
: ".bss.rel.ro", 0, 1);
333
319
  Add(In.BssRelRo);
334
319
335
319
  // Add MIPS-specific sections.
336
319
  if (Config->EMachine == EM_MIPS) {
337
23
    if (!Config->Shared && 
Config->HasDynSymTab17
) {
338
2
      In.MipsRldMap = make<MipsRldMapSection>();
339
2
      Add(In.MipsRldMap);
340
2
    }
341
23
    if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
342
22
      Add(Sec);
343
23
    if (auto *Sec = MipsOptionsSection<ELFT>::create())
344
0
      Add(Sec);
345
23
    if (auto *Sec = MipsReginfoSection<ELFT>::create())
346
22
      Add(Sec);
347
23
  }
348
319
349
319
  if (Config->HasDynSymTab) {
350
162
    In.DynSymTab = make<SymbolTableSection<ELFT>>(*In.DynStrTab);
351
162
    Add(In.DynSymTab);
352
162
353
162
    In.VerSym = make<VersionTableSection>();
354
162
    Add(In.VerSym);
355
162
356
162
    if (!Config->VersionDefinitions.empty()) {
357
0
      In.VerDef = make<VersionDefinitionSection>();
358
0
      Add(In.VerDef);
359
0
    }
360
162
361
162
    In.VerNeed = make<VersionNeedSection<ELFT>>();
362
162
    Add(In.VerNeed);
363
162
364
162
    if (Config->GnuHash) {
365
118
      In.GnuHashTab = make<GnuHashTableSection>();
366
118
      Add(In.GnuHashTab);
367
118
    }
368
162
369
162
    if (Config->SysvHash) {
370
160
      In.HashTab = make<HashTableSection>();
371
160
      Add(In.HashTab);
372
160
    }
373
162
374
162
    Add(In.Dynamic);
375
162
    Add(In.DynStrTab);
376
162
    Add(In.RelaDyn);
377
162
  }
378
319
379
319
  if (Config->RelrPackDynRelocs) {
380
2
    In.RelrDyn = make<RelrSection<ELFT>>();
381
2
    Add(In.RelrDyn);
382
2
  }
383
319
384
319
  // Add .got. MIPS' .got is so different from the other archs,
385
319
  // it has its own class.
386
319
  if (Config->EMachine == EM_MIPS) {
387
23
    In.MipsGot = make<MipsGotSection>();
388
23
    Add(In.MipsGot);
389
296
  } else {
390
296
    In.Got = make<GotSection>();
391
296
    Add(In.Got);
392
296
  }
393
319
394
319
  if (Config->EMachine == EM_PPC64) {
395
0
    In.PPC64LongBranchTarget = make<PPC64LongBranchTargetSection>();
396
0
    Add(In.PPC64LongBranchTarget);
397
0
  }
398
319
399
319
  In.GotPlt = make<GotPltSection>();
400
319
  Add(In.GotPlt);
401
319
  In.IgotPlt = make<IgotPltSection>();
402
319
  Add(In.IgotPlt);
403
319
404
319
  // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
405
319
  // it as a relocation and ensure the referenced section is created.
406
319
  if (ElfSym::GlobalOffsetTable && 
Config->EMachine != EM_MIPS10
) {
407
10
    if (Target->GotBaseSymInGotPlt)
408
8
      In.GotPlt->HasGotPltOffRel = true;
409
2
    else
410
2
      In.Got->HasGotOffRel = true;
411
10
  }
412
319
413
319
  if (Config->GdbIndex)
414
1
    Add(GdbIndexSection::create<ELFT>());
415
319
416
319
  // We always need to add rel[a].plt to output if it has entries.
417
319
  // Even for static linking it can contain R_[*]_IRELATIVE relocations.
418
319
  In.RelaPlt = make<RelocationSection<ELFT>>(
419
319
      Config->IsRela ? 
".rela.plt"9
:
".rel.plt"310
, false /*Sort*/);
420
319
  Add(In.RelaPlt);
421
319
422
319
  // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
423
319
  // that the IRelative relocations are processed last by the dynamic loader.
424
319
  // We cannot place the iplt section in .rel.dyn when Android relocation
425
319
  // packing is enabled because that would cause a section type mismatch.
426
319
  // However, because the Android dynamic loader reads .rel.plt after .rel.dyn,
427
319
  // we can get the desired behaviour by placing the iplt section in .rel.plt.
428
319
  In.RelaIplt = make<RelocationSection<ELFT>>(
429
319
      (Config->EMachine == EM_ARM && 
!Config->AndroidPackDynRelocs155
)
430
319
          ? 
".rel.dyn"154
431
319
          : 
In.RelaPlt->Name165
,
432
319
      false /*Sort*/);
433
319
  Add(In.RelaIplt);
434
319
435
319
  In.Plt = make<PltSection>(false);
436
319
  Add(In.Plt);
437
319
  In.Iplt = make<PltSection>(true);
438
319
  Add(In.Iplt);
439
319
440
319
  // .note.GNU-stack is always added when we are creating a re-linkable
441
319
  // object file. Other linkers are using the presence of this marker
442
319
  // section to control the executable-ness of the stack area, but that
443
319
  // is irrelevant these days. Stack area should always be non-executable
444
319
  // by default. So we emit this section unconditionally.
445
319
  if (Config->Relocatable)
446
11
    Add(make<GnuStackSection>());
447
319
448
319
  if (!Config->Relocatable) {
449
308
    if (Config->EhFrameHdr) {
450
0
      In.EhFrameHdr = make<EhFrameHeader>();
451
0
      Add(In.EhFrameHdr);
452
0
    }
453
308
    In.EhFrame = make<EhFrameSection>();
454
308
    Add(In.EhFrame);
455
308
  }
456
319
457
319
  if (In.SymTab)
458
319
    Add(In.SymTab);
459
319
  if (In.SymTabShndx)
460
319
    Add(In.SymTabShndx);
461
319
  Add(In.ShStrTab);
462
319
  if (In.StrTab)
463
319
    Add(In.StrTab);
464
319
465
319
  if (Config->EMachine == EM_ARM && 
!Config->Relocatable155
) {
466
150
    // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
467
150
    // InputSections.
468
150
    In.ARMExidx = make<ARMExidxSyntheticSection>();
469
150
    Add(In.ARMExidx);
470
150
  }
471
319
}
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
289
133
template <class ELFT> static void createSyntheticSections() {
290
133
  // Initialize all pointers with NULL. This is needed because
291
133
  // you can call lld::elf::main more than once as a library.
292
133
  memset(&Out::First, 0, sizeof(Out));
293
133
294
133
  auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
295
133
296
133
  In.DynStrTab = make<StringTableSection>(".dynstr", true);
297
133
  In.Dynamic = make<DynamicSection<ELFT>>();
298
133
  if (Config->AndroidPackDynRelocs) {
299
0
    In.RelaDyn = make<AndroidPackedRelocationSection<ELFT>>(
300
0
        Config->IsRela ? ".rela.dyn" : ".rel.dyn");
301
133
  } else {
302
133
    In.RelaDyn = make<RelocationSection<ELFT>>(
303
133
        Config->IsRela ? 
".rela.dyn"7
:
".rel.dyn"126
, Config->ZCombreloc);
304
133
  }
305
133
  In.ShStrTab = make<StringTableSection>(".shstrtab", false);
306
133
307
133
  Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
308
133
  Out::ProgramHeaders->Alignment = Config->Wordsize;
309
133
310
133
  if (needsInterpSection())
311
0
    Add(createInterpSection());
312
133
313
133
  if (Config->Strip != StripPolicy::All) {
314
133
    In.StrTab = make<StringTableSection>(".strtab", false);
315
133
    In.SymTab = make<SymbolTableSection<ELFT>>(*In.StrTab);
316
133
    In.SymTabShndx = make<SymtabShndxSection>();
317
133
  }
318
133
319
133
  if (Config->BuildId != BuildIdKind::None) {
320
0
    In.BuildId = make<BuildIdSection>();
321
0
    Add(In.BuildId);
322
0
  }
323
133
324
133
  In.Bss = make<BssSection>(".bss", 0, 1);
325
133
  Add(In.Bss);
326
133
327
133
  // If there is a SECTIONS command and a .data.rel.ro section name use name
328
133
  // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
329
133
  // This makes sure our relro is contiguous.
330
133
  bool HasDataRelRo = Script->HasSectionsCommand && 
findSection(".data.rel.ro")9
;
331
133
  In.BssRelRo =
332
133
      make<BssSection>(HasDataRelRo ? 
".data.rel.ro.bss"0
: ".bss.rel.ro", 0, 1);
333
133
  Add(In.BssRelRo);
334
133
335
133
  // Add MIPS-specific sections.
336
133
  if (Config->EMachine == EM_MIPS) {
337
126
    if (!Config->Shared && 
Config->HasDynSymTab76
) {
338
21
      In.MipsRldMap = make<MipsRldMapSection>();
339
21
      Add(In.MipsRldMap);
340
21
    }
341
126
    if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
342
123
      Add(Sec);
343
126
    if (auto *Sec = MipsOptionsSection<ELFT>::create())
344
0
      Add(Sec);
345
126
    if (auto *Sec = MipsReginfoSection<ELFT>::create())
346
123
      Add(Sec);
347
126
  }
348
133
349
133
  if (Config->HasDynSymTab) {
350
73
    In.DynSymTab = make<SymbolTableSection<ELFT>>(*In.DynStrTab);
351
73
    Add(In.DynSymTab);
352
73
353
73
    In.VerSym = make<VersionTableSection>();
354
73
    Add(In.VerSym);
355
73
356
73
    if (!Config->VersionDefinitions.empty()) {
357
0
      In.VerDef = make<VersionDefinitionSection>();
358
0
      Add(In.VerDef);
359
0
    }
360
73
361
73
    In.VerNeed = make<VersionNeedSection<ELFT>>();
362
73
    Add(In.VerNeed);
363
73
364
73
    if (Config->GnuHash) {
365
1
      In.GnuHashTab = make<GnuHashTableSection>();
366
1
      Add(In.GnuHashTab);
367
1
    }
368
73
369
73
    if (Config->SysvHash) {
370
73
      In.HashTab = make<HashTableSection>();
371
73
      Add(In.HashTab);
372
73
    }
373
73
374
73
    Add(In.Dynamic);
375
73
    Add(In.DynStrTab);
376
73
    Add(In.RelaDyn);
377
73
  }
378
133
379
133
  if (Config->RelrPackDynRelocs) {
380
0
    In.RelrDyn = make<RelrSection<ELFT>>();
381
0
    Add(In.RelrDyn);
382
0
  }
383
133
384
133
  // Add .got. MIPS' .got is so different from the other archs,
385
133
  // it has its own class.
386
133
  if (Config->EMachine == EM_MIPS) {
387
126
    In.MipsGot = make<MipsGotSection>();
388
126
    Add(In.MipsGot);
389
126
  } else {
390
7
    In.Got = make<GotSection>();
391
7
    Add(In.Got);
392
7
  }
393
133
394
133
  if (Config->EMachine == EM_PPC64) {
395
0
    In.PPC64LongBranchTarget = make<PPC64LongBranchTargetSection>();
396
0
    Add(In.PPC64LongBranchTarget);
397
0
  }
398
133
399
133
  In.GotPlt = make<GotPltSection>();
400
133
  Add(In.GotPlt);
401
133
  In.IgotPlt = make<IgotPltSection>();
402
133
  Add(In.IgotPlt);
403
133
404
133
  // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
405
133
  // it as a relocation and ensure the referenced section is created.
406
133
  if (ElfSym::GlobalOffsetTable && 
Config->EMachine != EM_MIPS0
) {
407
0
    if (Target->GotBaseSymInGotPlt)
408
0
      In.GotPlt->HasGotPltOffRel = true;
409
0
    else
410
0
      In.Got->HasGotOffRel = true;
411
0
  }
412
133
413
133
  if (Config->GdbIndex)
414
0
    Add(GdbIndexSection::create<ELFT>());
415
133
416
133
  // We always need to add rel[a].plt to output if it has entries.
417
133
  // Even for static linking it can contain R_[*]_IRELATIVE relocations.
418
133
  In.RelaPlt = make<RelocationSection<ELFT>>(
419
133
      Config->IsRela ? 
".rela.plt"7
:
".rel.plt"126
, false /*Sort*/);
420
133
  Add(In.RelaPlt);
421
133
422
133
  // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
423
133
  // that the IRelative relocations are processed last by the dynamic loader.
424
133
  // We cannot place the iplt section in .rel.dyn when Android relocation
425
133
  // packing is enabled because that would cause a section type mismatch.
426
133
  // However, because the Android dynamic loader reads .rel.plt after .rel.dyn,
427
133
  // we can get the desired behaviour by placing the iplt section in .rel.plt.
428
133
  In.RelaIplt = make<RelocationSection<ELFT>>(
429
133
      (Config->EMachine == EM_ARM && 
!Config->AndroidPackDynRelocs0
)
430
133
          ? 
".rel.dyn"0
431
133
          : In.RelaPlt->Name,
432
133
      false /*Sort*/);
433
133
  Add(In.RelaIplt);
434
133
435
133
  In.Plt = make<PltSection>(false);
436
133
  Add(In.Plt);
437
133
  In.Iplt = make<PltSection>(true);
438
133
  Add(In.Iplt);
439
133
440
133
  // .note.GNU-stack is always added when we are creating a re-linkable
441
133
  // object file. Other linkers are using the presence of this marker
442
133
  // section to control the executable-ness of the stack area, but that
443
133
  // is irrelevant these days. Stack area should always be non-executable
444
133
  // by default. So we emit this section unconditionally.
445
133
  if (Config->Relocatable)
446
11
    Add(make<GnuStackSection>());
447
133
448
133
  if (!Config->Relocatable) {
449
122
    if (Config->EhFrameHdr) {
450
0
      In.EhFrameHdr = make<EhFrameHeader>();
451
0
      Add(In.EhFrameHdr);
452
0
    }
453
122
    In.EhFrame = make<EhFrameSection>();
454
122
    Add(In.EhFrame);
455
122
  }
456
133
457
133
  if (In.SymTab)
458
133
    Add(In.SymTab);
459
133
  if (In.SymTabShndx)
460
133
    Add(In.SymTabShndx);
461
133
  Add(In.ShStrTab);
462
133
  if (In.StrTab)
463
133
    Add(In.StrTab);
464
133
465
133
  if (Config->EMachine == EM_ARM && 
!Config->Relocatable0
) {
466
0
    // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
467
0
    // InputSections.
468
0
    In.ARMExidx = make<ARMExidxSyntheticSection>();
469
0
    Add(In.ARMExidx);
470
0
  }
471
133
}
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
289
1.96k
template <class ELFT> static void createSyntheticSections() {
290
1.96k
  // Initialize all pointers with NULL. This is needed because
291
1.96k
  // you can call lld::elf::main more than once as a library.
292
1.96k
  memset(&Out::First, 0, sizeof(Out));
293
1.96k
294
1.96k
  auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
295
1.96k
296
1.96k
  In.DynStrTab = make<StringTableSection>(".dynstr", true);
297
1.96k
  In.Dynamic = make<DynamicSection<ELFT>>();
298
1.96k
  if (Config->AndroidPackDynRelocs) {
299
4
    In.RelaDyn = make<AndroidPackedRelocationSection<ELFT>>(
300
4
        Config->IsRela ? ".rela.dyn" : 
".rel.dyn"0
);
301
1.95k
  } else {
302
1.95k
    In.RelaDyn = make<RelocationSection<ELFT>>(
303
1.95k
        Config->IsRela ? 
".rela.dyn"1.95k
:
".rel.dyn"3
, Config->ZCombreloc);
304
1.95k
  }
305
1.96k
  In.ShStrTab = make<StringTableSection>(".shstrtab", false);
306
1.96k
307
1.96k
  Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
308
1.96k
  Out::ProgramHeaders->Alignment = Config->Wordsize;
309
1.96k
310
1.96k
  if (needsInterpSection())
311
7
    Add(createInterpSection());
312
1.96k
313
1.96k
  if (Config->Strip != StripPolicy::All) {
314
1.95k
    In.StrTab = make<StringTableSection>(".strtab", false);
315
1.95k
    In.SymTab = make<SymbolTableSection<ELFT>>(*In.StrTab);
316
1.95k
    In.SymTabShndx = make<SymtabShndxSection>();
317
1.95k
  }
318
1.96k
319
1.96k
  if (Config->BuildId != BuildIdKind::None) {
320
17
    In.BuildId = make<BuildIdSection>();
321
17
    Add(In.BuildId);
322
17
  }
323
1.96k
324
1.96k
  In.Bss = make<BssSection>(".bss", 0, 1);
325
1.96k
  Add(In.Bss);
326
1.96k
327
1.96k
  // If there is a SECTIONS command and a .data.rel.ro section name use name
328
1.96k
  // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
329
1.96k
  // This makes sure our relro is contiguous.
330
1.96k
  bool HasDataRelRo = Script->HasSectionsCommand && 
findSection(".data.rel.ro")402
;
331
1.96k
  In.BssRelRo =
332
1.96k
      make<BssSection>(HasDataRelRo ? 
".data.rel.ro.bss"6
:
".bss.rel.ro"1.95k
, 0, 1);
333
1.96k
  Add(In.BssRelRo);
334
1.96k
335
1.96k
  // Add MIPS-specific sections.
336
1.96k
  if (Config->EMachine == EM_MIPS) {
337
3
    if (!Config->Shared && Config->HasDynSymTab) {
338
0
      In.MipsRldMap = make<MipsRldMapSection>();
339
0
      Add(In.MipsRldMap);
340
0
    }
341
3
    if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
342
3
      Add(Sec);
343
3
    if (auto *Sec = MipsOptionsSection<ELFT>::create())
344
3
      Add(Sec);
345
3
    if (auto *Sec = MipsReginfoSection<ELFT>::create())
346
0
      Add(Sec);
347
3
  }
348
1.96k
349
1.96k
  if (Config->HasDynSymTab) {
350
937
    In.DynSymTab = make<SymbolTableSection<ELFT>>(*In.DynStrTab);
351
937
    Add(In.DynSymTab);
352
937
353
937
    In.VerSym = make<VersionTableSection>();
354
937
    Add(In.VerSym);
355
937
356
937
    if (!Config->VersionDefinitions.empty()) {
357
53
      In.VerDef = make<VersionDefinitionSection>();
358
53
      Add(In.VerDef);
359
53
    }
360
937
361
937
    In.VerNeed = make<VersionNeedSection<ELFT>>();
362
937
    Add(In.VerNeed);
363
937
364
937
    if (Config->GnuHash) {
365
839
      In.GnuHashTab = make<GnuHashTableSection>();
366
839
      Add(In.GnuHashTab);
367
839
    }
368
937
369
937
    if (Config->SysvHash) {
370
931
      In.HashTab = make<HashTableSection>();
371
931
      Add(In.HashTab);
372
931
    }
373
937
374
937
    Add(In.Dynamic);
375
937
    Add(In.DynStrTab);
376
937
    Add(In.RelaDyn);
377
937
  }
378
1.96k
379
1.96k
  if (Config->RelrPackDynRelocs) {
380
2
    In.RelrDyn = make<RelrSection<ELFT>>();
381
2
    Add(In.RelrDyn);
382
2
  }
383
1.96k
384
1.96k
  // Add .got. MIPS' .got is so different from the other archs,
385
1.96k
  // it has its own class.
386
1.96k
  if (Config->EMachine == EM_MIPS) {
387
3
    In.MipsGot = make<MipsGotSection>();
388
3
    Add(In.MipsGot);
389
1.95k
  } else {
390
1.95k
    In.Got = make<GotSection>();
391
1.95k
    Add(In.Got);
392
1.95k
  }
393
1.96k
394
1.96k
  if (Config->EMachine == EM_PPC64) {
395
87
    In.PPC64LongBranchTarget = make<PPC64LongBranchTargetSection>();
396
87
    Add(In.PPC64LongBranchTarget);
397
87
  }
398
1.96k
399
1.96k
  In.GotPlt = make<GotPltSection>();
400
1.96k
  Add(In.GotPlt);
401
1.96k
  In.IgotPlt = make<IgotPltSection>();
402
1.96k
  Add(In.IgotPlt);
403
1.96k
404
1.96k
  // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
405
1.96k
  // it as a relocation and ensure the referenced section is created.
406
1.96k
  if (ElfSym::GlobalOffsetTable && 
Config->EMachine != EM_MIPS73
) {
407
73
    if (Target->GotBaseSymInGotPlt)
408
23
      In.GotPlt->HasGotPltOffRel = true;
409
50
    else
410
50
      In.Got->HasGotOffRel = true;
411
73
  }
412
1.96k
413
1.96k
  if (Config->GdbIndex)
414
16
    Add(GdbIndexSection::create<ELFT>());
415
1.96k
416
1.96k
  // We always need to add rel[a].plt to output if it has entries.
417
1.96k
  // Even for static linking it can contain R_[*]_IRELATIVE relocations.
418
1.96k
  In.RelaPlt = make<RelocationSection<ELFT>>(
419
1.96k
      Config->IsRela ? 
".rela.plt"1.95k
:
".rel.plt"3
, false /*Sort*/);
420
1.96k
  Add(In.RelaPlt);
421
1.96k
422
1.96k
  // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
423
1.96k
  // that the IRelative relocations are processed last by the dynamic loader.
424
1.96k
  // We cannot place the iplt section in .rel.dyn when Android relocation
425
1.96k
  // packing is enabled because that would cause a section type mismatch.
426
1.96k
  // However, because the Android dynamic loader reads .rel.plt after .rel.dyn,
427
1.96k
  // we can get the desired behaviour by placing the iplt section in .rel.plt.
428
1.96k
  In.RelaIplt = make<RelocationSection<ELFT>>(
429
1.96k
      (Config->EMachine == EM_ARM && 
!Config->AndroidPackDynRelocs0
)
430
1.96k
          ? 
".rel.dyn"0
431
1.96k
          : In.RelaPlt->Name,
432
1.96k
      false /*Sort*/);
433
1.96k
  Add(In.RelaIplt);
434
1.96k
435
1.96k
  In.Plt = make<PltSection>(false);
436
1.96k
  Add(In.Plt);
437
1.96k
  In.Iplt = make<PltSection>(true);
438
1.96k
  Add(In.Iplt);
439
1.96k
440
1.96k
  // .note.GNU-stack is always added when we are creating a re-linkable
441
1.96k
  // object file. Other linkers are using the presence of this marker
442
1.96k
  // section to control the executable-ness of the stack area, but that
443
1.96k
  // is irrelevant these days. Stack area should always be non-executable
444
1.96k
  // by default. So we emit this section unconditionally.
445
1.96k
  if (Config->Relocatable)
446
57
    Add(make<GnuStackSection>());
447
1.96k
448
1.96k
  if (!Config->Relocatable) {
449
1.90k
    if (Config->EhFrameHdr) {
450
27
      In.EhFrameHdr = make<EhFrameHeader>();
451
27
      Add(In.EhFrameHdr);
452
27
    }
453
1.90k
    In.EhFrame = make<EhFrameSection>();
454
1.90k
    Add(In.EhFrame);
455
1.90k
  }
456
1.96k
457
1.96k
  if (In.SymTab)
458
1.95k
    Add(In.SymTab);
459
1.96k
  if (In.SymTabShndx)
460
1.95k
    Add(In.SymTabShndx);
461
1.96k
  Add(In.ShStrTab);
462
1.96k
  if (In.StrTab)
463
1.95k
    Add(In.StrTab);
464
1.96k
465
1.96k
  if (Config->EMachine == EM_ARM && 
!Config->Relocatable0
) {
466
0
    // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
467
0
    // InputSections.
468
0
    In.ARMExidx = make<ARMExidxSyntheticSection>();
469
0
    Add(In.ARMExidx);
470
0
  }
471
1.96k
}
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
289
115
template <class ELFT> static void createSyntheticSections() {
290
115
  // Initialize all pointers with NULL. This is needed because
291
115
  // you can call lld::elf::main more than once as a library.
292
115
  memset(&Out::First, 0, sizeof(Out));
293
115
294
115
  auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
295
115
296
115
  In.DynStrTab = make<StringTableSection>(".dynstr", true);
297
115
  In.Dynamic = make<DynamicSection<ELFT>>();
298
115
  if (Config->AndroidPackDynRelocs) {
299
0
    In.RelaDyn = make<AndroidPackedRelocationSection<ELFT>>(
300
0
        Config->IsRela ? ".rela.dyn" : ".rel.dyn");
301
115
  } else {
302
115
    In.RelaDyn = make<RelocationSection<ELFT>>(
303
115
        Config->IsRela ? 
".rela.dyn"69
:
".rel.dyn"46
, Config->ZCombreloc);
304
115
  }
305
115
  In.ShStrTab = make<StringTableSection>(".shstrtab", false);
306
115
307
115
  Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
308
115
  Out::ProgramHeaders->Alignment = Config->Wordsize;
309
115
310
115
  if (needsInterpSection())
311
2
    Add(createInterpSection());
312
115
313
115
  if (Config->Strip != StripPolicy::All) {
314
115
    In.StrTab = make<StringTableSection>(".strtab", false);
315
115
    In.SymTab = make<SymbolTableSection<ELFT>>(*In.StrTab);
316
115
    In.SymTabShndx = make<SymtabShndxSection>();
317
115
  }
318
115
319
115
  if (Config->BuildId != BuildIdKind::None) {
320
0
    In.BuildId = make<BuildIdSection>();
321
0
    Add(In.BuildId);
322
0
  }
323
115
324
115
  In.Bss = make<BssSection>(".bss", 0, 1);
325
115
  Add(In.Bss);
326
115
327
115
  // If there is a SECTIONS command and a .data.rel.ro section name use name
328
115
  // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
329
115
  // This makes sure our relro is contiguous.
330
115
  bool HasDataRelRo = Script->HasSectionsCommand && 
findSection(".data.rel.ro")4
;
331
115
  In.BssRelRo =
332
115
      make<BssSection>(HasDataRelRo ? 
".data.rel.ro.bss"0
: ".bss.rel.ro", 0, 1);
333
115
  Add(In.BssRelRo);
334
115
335
115
  // Add MIPS-specific sections.
336
115
  if (Config->EMachine == EM_MIPS) {
337
45
    if (!Config->Shared && 
Config->HasDynSymTab22
) {
338
6
      In.MipsRldMap = make<MipsRldMapSection>();
339
6
      Add(In.MipsRldMap);
340
6
    }
341
45
    if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
342
43
      Add(Sec);
343
45
    if (auto *Sec = MipsOptionsSection<ELFT>::create())
344
39
      Add(Sec);
345
45
    if (auto *Sec = MipsReginfoSection<ELFT>::create())
346
0
      Add(Sec);
347
45
  }
348
115
349
115
  if (Config->HasDynSymTab) {
350
61
    In.DynSymTab = make<SymbolTableSection<ELFT>>(*In.DynStrTab);
351
61
    Add(In.DynSymTab);
352
61
353
61
    In.VerSym = make<VersionTableSection>();
354
61
    Add(In.VerSym);
355
61
356
61
    if (!Config->VersionDefinitions.empty()) {
357
0
      In.VerDef = make<VersionDefinitionSection>();
358
0
      Add(In.VerDef);
359
0
    }
360
61
361
61
    In.VerNeed = make<VersionNeedSection<ELFT>>();
362
61
    Add(In.VerNeed);
363
61
364
61
    if (Config->GnuHash) {
365
32
      In.GnuHashTab = make<GnuHashTableSection>();
366
32
      Add(In.GnuHashTab);
367
32
    }
368
61
369
61
    if (Config->SysvHash) {
370
61
      In.HashTab = make<HashTableSection>();
371
61
      Add(In.HashTab);
372
61
    }
373
61
374
61
    Add(In.Dynamic);
375
61
    Add(In.DynStrTab);
376
61
    Add(In.RelaDyn);
377
61
  }
378
115
379
115
  if (Config->RelrPackDynRelocs) {
380
0
    In.RelrDyn = make<RelrSection<ELFT>>();
381
0
    Add(In.RelrDyn);
382
0
  }
383
115
384
115
  // Add .got. MIPS' .got is so different from the other archs,
385
115
  // it has its own class.
386
115
  if (Config->EMachine == EM_MIPS) {
387
45
    In.MipsGot = make<MipsGotSection>();
388
45
    Add(In.MipsGot);
389
70
  } else {
390
70
    In.Got = make<GotSection>();
391
70
    Add(In.Got);
392
70
  }
393
115
394
115
  if (Config->EMachine == EM_PPC64) {
395
69
    In.PPC64LongBranchTarget = make<PPC64LongBranchTargetSection>();
396
69
    Add(In.PPC64LongBranchTarget);
397
69
  }
398
115
399
115
  In.GotPlt = make<GotPltSection>();
400
115
  Add(In.GotPlt);
401
115
  In.IgotPlt = make<IgotPltSection>();
402
115
  Add(In.IgotPlt);
403
115
404
115
  // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
405
115
  // it as a relocation and ensure the referenced section is created.
406
115
  if (ElfSym::GlobalOffsetTable && 
Config->EMachine != EM_MIPS38
) {
407
37
    if (Target->GotBaseSymInGotPlt)
408
0
      In.GotPlt->HasGotPltOffRel = true;
409
37
    else
410
37
      In.Got->HasGotOffRel = true;
411
37
  }
412
115
413
115
  if (Config->GdbIndex)
414
0
    Add(GdbIndexSection::create<ELFT>());
415
115
416
115
  // We always need to add rel[a].plt to output if it has entries.
417
115
  // Even for static linking it can contain R_[*]_IRELATIVE relocations.
418
115
  In.RelaPlt = make<RelocationSection<ELFT>>(
419
115
      Config->IsRela ? 
".rela.plt"69
:
".rel.plt"46
, false /*Sort*/);
420
115
  Add(In.RelaPlt);
421
115
422
115
  // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
423
115
  // that the IRelative relocations are processed last by the dynamic loader.
424
115
  // We cannot place the iplt section in .rel.dyn when Android relocation
425
115
  // packing is enabled because that would cause a section type mismatch.
426
115
  // However, because the Android dynamic loader reads .rel.plt after .rel.dyn,
427
115
  // we can get the desired behaviour by placing the iplt section in .rel.plt.
428
115
  In.RelaIplt = make<RelocationSection<ELFT>>(
429
115
      (Config->EMachine == EM_ARM && 
!Config->AndroidPackDynRelocs0
)
430
115
          ? 
".rel.dyn"0
431
115
          : In.RelaPlt->Name,
432
115
      false /*Sort*/);
433
115
  Add(In.RelaIplt);
434
115
435
115
  In.Plt = make<PltSection>(false);
436
115
  Add(In.Plt);
437
115
  In.Iplt = make<PltSection>(true);
438
115
  Add(In.Iplt);
439
115
440
115
  // .note.GNU-stack is always added when we are creating a re-linkable
441
115
  // object file. Other linkers are using the presence of this marker
442
115
  // section to control the executable-ness of the stack area, but that
443
115
  // is irrelevant these days. Stack area should always be non-executable
444
115
  // by default. So we emit this section unconditionally.
445
115
  if (Config->Relocatable)
446
3
    Add(make<GnuStackSection>());
447
115
448
115
  if (!Config->Relocatable) {
449
112
    if (Config->EhFrameHdr) {
450
3
      In.EhFrameHdr = make<EhFrameHeader>();
451
3
      Add(In.EhFrameHdr);
452
3
    }
453
112
    In.EhFrame = make<EhFrameSection>();
454
112
    Add(In.EhFrame);
455
112
  }
456
115
457
115
  if (In.SymTab)
458
115
    Add(In.SymTab);
459
115
  if (In.SymTabShndx)
460
115
    Add(In.SymTabShndx);
461
115
  Add(In.ShStrTab);
462
115
  if (In.StrTab)
463
115
    Add(In.StrTab);
464
115
465
115
  if (Config->EMachine == EM_ARM && 
!Config->Relocatable0
) {
466
0
    // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
467
0
    // InputSections.
468
0
    In.ARMExidx = make<ARMExidxSyntheticSection>();
469
0
    Add(In.ARMExidx);
470
0
  }
471
115
}
472
473
// The main function of the writer.
474
2.52k
template <class ELFT> void Writer<ELFT>::run() {
475
2.52k
  // Create linker-synthesized sections such as .got or .plt.
476
2.52k
  // Such sections are of type input section.
477
2.52k
  createSyntheticSections<ELFT>();
478
2.52k
479
2.52k
  // Some input sections that are used for exception handling need to be moved
480
2.52k
  // into synthetic sections. Do that now so that they aren't assigned to
481
2.52k
  // output sections in the usual way.
482
2.52k
  if (!Config->Relocatable)
483
2.44k
    combineEhSections<ELFT>();
484
2.52k
485
2.52k
  // We want to process linker script commands. When SECTIONS command
486
2.52k
  // is given we let it create sections.
487
2.52k
  Script->processSectionCommands();
488
2.52k
489
2.52k
  // Linker scripts controls how input sections are assigned to output sections.
490
2.52k
  // Input sections that were not handled by scripts are called "orphans", and
491
2.52k
  // they are assigned to output sections by the default rule. Process that.
492
2.52k
  Script->addOrphanSections();
493
2.52k
494
2.52k
  if (Config->Discard != DiscardPolicy::All)
495
2.52k
    copyLocalSymbols();
496
2.52k
497
2.52k
  if (Config->CopyRelocs)
498
105
    addSectionSymbols();
499
2.52k
500
2.52k
  // Now that we have a complete set of output sections. This function
501
2.52k
  // completes section contents. For example, we need to add strings
502
2.52k
  // to the string table, and add entries to .got and .plt.
503
2.52k
  // finalizeSections does that.
504
2.52k
  finalizeSections();
505
2.52k
  checkExecuteOnly();
506
2.52k
  if (errorCount())
507
141
    return;
508
2.38k
509
2.38k
  Script->assignAddresses();
510
2.38k
511
2.38k
  // If -compressed-debug-sections is specified, we need to compress
512
2.38k
  // .debug_* sections. Do it right now because it changes the size of
513
2.38k
  // output sections.
514
2.38k
  for (OutputSection *Sec : OutputSections)
515
350k
    Sec->maybeCompress<ELFT>();
516
2.38k
517
2.38k
  Script->allocateHeaders(Phdrs);
518
2.38k
519
2.38k
  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
520
2.38k
  // 0 sized region. This has to be done late since only after assignAddresses
521
2.38k
  // we know the size of the sections.
522
2.38k
  removeEmptyPTLoad();
523
2.38k
524
2.38k
  if (!Config->OFormatBinary)
525
2.38k
    assignFileOffsets();
526
5
  else
527
5
    assignFileOffsetsBinary();
528
2.38k
529
2.38k
  setPhdrs();
530
2.38k
531
2.38k
  if (Config->Relocatable)
532
82
    for (OutputSection *Sec : OutputSections)
533
66.1k
      Sec->Addr = 0;
534
2.38k
535
2.38k
  if (Config->CheckSections)
536
2.38k
    checkSections();
537
2.38k
538
2.38k
  // It does not make sense try to open the file if we have error already.
539
2.38k
  if (errorCount())
540
7
    return;
541
2.37k
  // Write the result down to a file.
542
2.37k
  openFile();
543
2.37k
  if (errorCount())
544
2
    return;
545
2.37k
546
2.37k
  if (!Config->OFormatBinary) {
547
2.37k
    writeTrapInstr();
548
2.37k
    writeHeader();
549
2.37k
    writeSections();
550
2.37k
  } else {
551
5
    writeSectionsBinary();
552
5
  }
553
2.37k
554
2.37k
  // Backfill .note.gnu.build-id section content. This is done at last
555
2.37k
  // because the content is usually a hash value of the entire output file.
556
2.37k
  writeBuildId();
557
2.37k
  if (errorCount())
558
55
    return;
559
2.32k
560
2.32k
  // Handle -Map and -cref options.
561
2.32k
  writeMapFile();
562
2.32k
  writeCrossReferenceTable();
563
2.32k
  if (errorCount())
564
0
    return;
565
2.32k
566
2.32k
  if (auto E = Buffer->commit())
567
0
    error("failed to write to the output file: " + toString(std::move(E)));
568
2.32k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::run()
Line
Count
Source
474
319
template <class ELFT> void Writer<ELFT>::run() {
475
319
  // Create linker-synthesized sections such as .got or .plt.
476
319
  // Such sections are of type input section.
477
319
  createSyntheticSections<ELFT>();
478
319
479
319
  // Some input sections that are used for exception handling need to be moved
480
319
  // into synthetic sections. Do that now so that they aren't assigned to
481
319
  // output sections in the usual way.
482
319
  if (!Config->Relocatable)
483
308
    combineEhSections<ELFT>();
484
319
485
319
  // We want to process linker script commands. When SECTIONS command
486
319
  // is given we let it create sections.
487
319
  Script->processSectionCommands();
488
319
489
319
  // Linker scripts controls how input sections are assigned to output sections.
490
319
  // Input sections that were not handled by scripts are called "orphans", and
491
319
  // they are assigned to output sections by the default rule. Process that.
492
319
  Script->addOrphanSections();
493
319
494
319
  if (Config->Discard != DiscardPolicy::All)
495
319
    copyLocalSymbols();
496
319
497
319
  if (Config->CopyRelocs)
498
14
    addSectionSymbols();
499
319
500
319
  // Now that we have a complete set of output sections. This function
501
319
  // completes section contents. For example, we need to add strings
502
319
  // to the string table, and add entries to .got and .plt.
503
319
  // finalizeSections does that.
504
319
  finalizeSections();
505
319
  checkExecuteOnly();
506
319
  if (errorCount())
507
6
    return;
508
313
509
313
  Script->assignAddresses();
510
313
511
313
  // If -compressed-debug-sections is specified, we need to compress
512
313
  // .debug_* sections. Do it right now because it changes the size of
513
313
  // output sections.
514
313
  for (OutputSection *Sec : OutputSections)
515
2.99k
    Sec->maybeCompress<ELFT>();
516
313
517
313
  Script->allocateHeaders(Phdrs);
518
313
519
313
  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
520
313
  // 0 sized region. This has to be done late since only after assignAddresses
521
313
  // we know the size of the sections.
522
313
  removeEmptyPTLoad();
523
313
524
313
  if (!Config->OFormatBinary)
525
313
    assignFileOffsets();
526
0
  else
527
0
    assignFileOffsetsBinary();
528
313
529
313
  setPhdrs();
530
313
531
313
  if (Config->Relocatable)
532
11
    for (OutputSection *Sec : OutputSections)
533
94
      Sec->Addr = 0;
534
313
535
313
  if (Config->CheckSections)
536
312
    checkSections();
537
313
538
313
  // It does not make sense try to open the file if we have error already.
539
313
  if (errorCount())
540
1
    return;
541
312
  // Write the result down to a file.
542
312
  openFile();
543
312
  if (errorCount())
544
1
    return;
545
311
546
311
  if (!Config->OFormatBinary) {
547
311
    writeTrapInstr();
548
311
    writeHeader();
549
311
    writeSections();
550
311
  } else {
551
0
    writeSectionsBinary();
552
0
  }
553
311
554
311
  // Backfill .note.gnu.build-id section content. This is done at last
555
311
  // because the content is usually a hash value of the entire output file.
556
311
  writeBuildId();
557
311
  if (errorCount())
558
4
    return;
559
307
560
307
  // Handle -Map and -cref options.
561
307
  writeMapFile();
562
307
  writeCrossReferenceTable();
563
307
  if (errorCount())
564
0
    return;
565
307
566
307
  if (auto E = Buffer->commit())
567
0
    error("failed to write to the output file: " + toString(std::move(E)));
568
307
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::run()
Line
Count
Source
474
133
template <class ELFT> void Writer<ELFT>::run() {
475
133
  // Create linker-synthesized sections such as .got or .plt.
476
133
  // Such sections are of type input section.
477
133
  createSyntheticSections<ELFT>();
478
133
479
133
  // Some input sections that are used for exception handling need to be moved
480
133
  // into synthetic sections. Do that now so that they aren't assigned to
481
133
  // output sections in the usual way.
482
133
  if (!Config->Relocatable)
483
122
    combineEhSections<ELFT>();
484
133
485
133
  // We want to process linker script commands. When SECTIONS command
486
133
  // is given we let it create sections.
487
133
  Script->processSectionCommands();
488
133
489
133
  // Linker scripts controls how input sections are assigned to output sections.
490
133
  // Input sections that were not handled by scripts are called "orphans", and
491
133
  // they are assigned to output sections by the default rule. Process that.
492
133
  Script->addOrphanSections();
493
133
494
133
  if (Config->Discard != DiscardPolicy::All)
495
132
    copyLocalSymbols();
496
133
497
133
  if (Config->CopyRelocs)
498
11
    addSectionSymbols();
499
133
500
133
  // Now that we have a complete set of output sections. This function
501
133
  // completes section contents. For example, we need to add strings
502
133
  // to the string table, and add entries to .got and .plt.
503
133
  // finalizeSections does that.
504
133
  finalizeSections();
505
133
  checkExecuteOnly();
506
133
  if (errorCount())
507
3
    return;
508
130
509
130
  Script->assignAddresses();
510
130
511
130
  // If -compressed-debug-sections is specified, we need to compress
512
130
  // .debug_* sections. Do it right now because it changes the size of
513
130
  // output sections.
514
130
  for (OutputSection *Sec : OutputSections)
515
1.64k
    Sec->maybeCompress<ELFT>();
516
130
517
130
  Script->allocateHeaders(Phdrs);
518
130
519
130
  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
520
130
  // 0 sized region. This has to be done late since only after assignAddresses
521
130
  // we know the size of the sections.
522
130
  removeEmptyPTLoad();
523
130
524
130
  if (!Config->OFormatBinary)
525
130
    assignFileOffsets();
526
0
  else
527
0
    assignFileOffsetsBinary();
528
130
529
130
  setPhdrs();
530
130
531
130
  if (Config->Relocatable)
532
11
    for (OutputSection *Sec : OutputSections)
533
112
      Sec->Addr = 0;
534
130
535
130
  if (Config->CheckSections)
536
130
    checkSections();
537
130
538
130
  // It does not make sense try to open the file if we have error already.
539
130
  if (errorCount())
540
0
    return;
541
130
  // Write the result down to a file.
542
130
  openFile();
543
130
  if (errorCount())
544
0
    return;
545
130
546
130
  if (!Config->OFormatBinary) {
547
130
    writeTrapInstr();
548
130
    writeHeader();
549
130
    writeSections();
550
130
  } else {
551
0
    writeSectionsBinary();
552
0
  }
553
130
554
130
  // Backfill .note.gnu.build-id section content. This is done at last
555
130
  // because the content is usually a hash value of the entire output file.
556
130
  writeBuildId();
557
130
  if (errorCount())
558
2
    return;
559
128
560
128
  // Handle -Map and -cref options.
561
128
  writeMapFile();
562
128
  writeCrossReferenceTable();
563
128
  if (errorCount())
564
0
    return;
565
128
566
128
  if (auto E = Buffer->commit())
567
0
    error("failed to write to the output file: " + toString(std::move(E)));
568
128
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::run()
Line
Count
Source
474
1.96k
template <class ELFT> void Writer<ELFT>::run() {
475
1.96k
  // Create linker-synthesized sections such as .got or .plt.
476
1.96k
  // Such sections are of type input section.
477
1.96k
  createSyntheticSections<ELFT>();
478
1.96k
479
1.96k
  // Some input sections that are used for exception handling need to be moved
480
1.96k
  // into synthetic sections. Do that now so that they aren't assigned to
481
1.96k
  // output sections in the usual way.
482
1.96k
  if (!Config->Relocatable)
483
1.90k
    combineEhSections<ELFT>();
484
1.96k
485
1.96k
  // We want to process linker script commands. When SECTIONS command
486
1.96k
  // is given we let it create sections.
487
1.96k
  Script->processSectionCommands();
488
1.96k
489
1.96k
  // Linker scripts controls how input sections are assigned to output sections.
490
1.96k
  // Input sections that were not handled by scripts are called "orphans", and
491
1.96k
  // they are assigned to output sections by the default rule. Process that.
492
1.96k
  Script->addOrphanSections();
493
1.96k
494
1.96k
  if (Config->Discard != DiscardPolicy::All)
495
1.95k
    copyLocalSymbols();
496
1.96k
497
1.96k
  if (Config->CopyRelocs)
498
77
    addSectionSymbols();
499
1.96k
500
1.96k
  // Now that we have a complete set of output sections. This function
501
1.96k
  // completes section contents. For example, we need to add strings
502
1.96k
  // to the string table, and add entries to .got and .plt.
503
1.96k
  // finalizeSections does that.
504
1.96k
  finalizeSections();
505
1.96k
  checkExecuteOnly();
506
1.96k
  if (errorCount())
507
125
    return;
508
1.83k
509
1.83k
  Script->assignAddresses();
510
1.83k
511
1.83k
  // If -compressed-debug-sections is specified, we need to compress
512
1.83k
  // .debug_* sections. Do it right now because it changes the size of
513
1.83k
  // output sections.
514
1.83k
  for (OutputSection *Sec : OutputSections)
515
345k
    Sec->maybeCompress<ELFT>();
516
1.83k
517
1.83k
  Script->allocateHeaders(Phdrs);
518
1.83k
519
1.83k
  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
520
1.83k
  // 0 sized region. This has to be done late since only after assignAddresses
521
1.83k
  // we know the size of the sections.
522
1.83k
  removeEmptyPTLoad();
523
1.83k
524
1.83k
  if (!Config->OFormatBinary)
525
1.83k
    assignFileOffsets();
526
5
  else
527
5
    assignFileOffsetsBinary();
528
1.83k
529
1.83k
  setPhdrs();
530
1.83k
531
1.83k
  if (Config->Relocatable)
532
57
    for (OutputSection *Sec : OutputSections)
533
65.9k
      Sec->Addr = 0;
534
1.83k
535
1.83k
  if (Config->CheckSections)
536
1.83k
    checkSections();
537
1.83k
538
1.83k
  // It does not make sense try to open the file if we have error already.
539
1.83k
  if (errorCount())
540
6
    return;
541
1.82k
  // Write the result down to a file.
542
1.82k
  openFile();
543
1.82k
  if (errorCount())
544
1
    return;
545
1.82k
546
1.82k
  if (!Config->OFormatBinary) {
547
1.82k
    writeTrapInstr();
548
1.82k
    writeHeader();
549
1.82k
    writeSections();
550
1.82k
  } else {
551
5
    writeSectionsBinary();
552
5
  }
553
1.82k
554
1.82k
  // Backfill .note.gnu.build-id section content. This is done at last
555
1.82k
  // because the content is usually a hash value of the entire output file.
556
1.82k
  writeBuildId();
557
1.82k
  if (errorCount())
558
38
    return;
559
1.79k
560
1.79k
  // Handle -Map and -cref options.
561
1.79k
  writeMapFile();
562
1.79k
  writeCrossReferenceTable();
563
1.79k
  if (errorCount())
564
0
    return;
565
1.79k
566
1.79k
  if (auto E = Buffer->commit())
567
0
    error("failed to write to the output file: " + toString(std::move(E)));
568
1.79k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::run()
Line
Count
Source
474
115
template <class ELFT> void Writer<ELFT>::run() {
475
115
  // Create linker-synthesized sections such as .got or .plt.
476
115
  // Such sections are of type input section.
477
115
  createSyntheticSections<ELFT>();
478
115
479
115
  // Some input sections that are used for exception handling need to be moved
480
115
  // into synthetic sections. Do that now so that they aren't assigned to
481
115
  // output sections in the usual way.
482
115
  if (!Config->Relocatable)
483
112
    combineEhSections<ELFT>();
484
115
485
115
  // We want to process linker script commands. When SECTIONS command
486
115
  // is given we let it create sections.
487
115
  Script->processSectionCommands();
488
115
489
115
  // Linker scripts controls how input sections are assigned to output sections.
490
115
  // Input sections that were not handled by scripts are called "orphans", and
491
115
  // they are assigned to output sections by the default rule. Process that.
492
115
  Script->addOrphanSections();
493
115
494
115
  if (Config->Discard != DiscardPolicy::All)
495
114
    copyLocalSymbols();
496
115
497
115
  if (Config->CopyRelocs)
498
3
    addSectionSymbols();
499
115
500
115
  // Now that we have a complete set of output sections. This function
501
115
  // completes section contents. For example, we need to add strings
502
115
  // to the string table, and add entries to .got and .plt.
503
115
  // finalizeSections does that.
504
115
  finalizeSections();
505
115
  checkExecuteOnly();
506
115
  if (errorCount())
507
7
    return;
508
108
509
108
  Script->assignAddresses();
510
108
511
108
  // If -compressed-debug-sections is specified, we need to compress
512
108
  // .debug_* sections. Do it right now because it changes the size of
513
108
  // output sections.
514
108
  for (OutputSection *Sec : OutputSections)
515
1.23k
    Sec->maybeCompress<ELFT>();
516
108
517
108
  Script->allocateHeaders(Phdrs);
518
108
519
108
  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
520
108
  // 0 sized region. This has to be done late since only after assignAddresses
521
108
  // we know the size of the sections.
522
108
  removeEmptyPTLoad();
523
108
524
108
  if (!Config->OFormatBinary)
525
108
    assignFileOffsets();
526
0
  else
527
0
    assignFileOffsetsBinary();
528
108
529
108
  setPhdrs();
530
108
531
108
  if (Config->Relocatable)
532
3
    for (OutputSection *Sec : OutputSections)
533
29
      Sec->Addr = 0;
534
108
535
108
  if (Config->CheckSections)
536
108
    checkSections();
537
108
538
108
  // It does not make sense try to open the file if we have error already.
539
108
  if (errorCount())
540
0
    return;
541
108
  // Write the result down to a file.
542
108
  openFile();
543
108
  if (errorCount())
544
0
    return;
545
108
546
108
  if (!Config->OFormatBinary) {
547
108
    writeTrapInstr();
548
108
    writeHeader();
549
108
    writeSections();
550
108
  } else {
551
0
    writeSectionsBinary();
552
0
  }
553
108
554
108
  // Backfill .note.gnu.build-id section content. This is done at last
555
108
  // because the content is usually a hash value of the entire output file.
556
108
  writeBuildId();
557
108
  if (errorCount())
558
11
    return;
559
97
560
97
  // Handle -Map and -cref options.
561
97
  writeMapFile();
562
97
  writeCrossReferenceTable();
563
97
  if (errorCount())
564
0
    return;
565
97
566
97
  if (auto E = Buffer->commit())
567
0
    error("failed to write to the output file: " + toString(std::move(E)));
568
97
}
569
570
68.0k
static bool shouldKeepInSymtab(const Defined &Sym) {
571
68.0k
  if (Sym.isSection())
572
66.1k
    return false;
573
1.88k
574
1.88k
  if (Config->Discard == DiscardPolicy::None)
575
55
    return true;
576
1.82k
577
1.82k
  // If -emit-reloc is given, all symbols including local ones need to be
578
1.82k
  // copied because they may be referenced by relocations.
579
1.82k
  if (Config->EmitRelocs)
580
35
    return true;
581
1.79k
582
1.79k
  // In ELF assembly .L symbols are normally discarded by the assembler.
583
1.79k
  // If the assembler fails to do so, the linker discards them if
584
1.79k
  // * --discard-locals is used.
585
1.79k
  // * The symbol is in a SHF_MERGE section, which is normally the reason for
586
1.79k
  //   the assembler keeping the .L symbol.
587
1.79k
  StringRef Name = Sym.getName();
588
1.79k
  bool IsLocal = Name.startswith(".L") || 
Name.empty()1.78k
;
589
1.79k
  if (!IsLocal)
590
1.78k
    return true;
591
13
592
13
  if (Config->Discard == DiscardPolicy::Locals)
593
2
    return false;
594
11
595
11
  SectionBase *Sec = Sym.Section;
596
11
  return !Sec || 
!(Sec->Flags & SHF_MERGE)9
;
597
11
}
598
599
85.9k
static bool includeInSymtab(const Symbol &B) {
600
85.9k
  if (!B.isLocal() && 
!B.IsUsedInRegularObj17.8k
)
601
445
    return false;
602
85.5k
603
85.5k
  if (auto *D = dyn_cast<Defined>(&B)) {
604
84.7k
    // Always include absolute symbols.
605
84.7k
    SectionBase *Sec = D->Section;
606
84.7k
    if (!Sec)
607
842
      return true;
608
83.9k
    Sec = Sec->Repl;
609
83.9k
610
83.9k
    // Exclude symbols pointing to garbage-collected sections.
611
83.9k
    if (isa<InputSectionBase>(Sec) && 
!Sec->Live83.5k
)
612
126
      return false;
613
83.8k
614
83.8k
    if (auto *S = dyn_cast<MergeInputSection>(Sec))
615
72
      if (!S->getSectionPiece(D->Value)->Live)
616
7
        return false;
617
83.8k
    return true;
618
83.8k
  }
619
734
  return B.Used;
620
734
}
621
622
// Local symbols are not in the linker's symbol table. This function scans
623
// each object file's symbol table to copy local symbols to the output.
624
2.52k
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
625
2.52k
  if (!In.SymTab)
626
4
    return;
627
3.06k
  
for (InputFile *File : ObjectFiles)2.51k
{
628
3.06k
    ObjFile<ELFT> *F = cast<ObjFile<ELFT>>(File);
629
68.1k
    for (Symbol *B : F->getLocalSymbols()) {
630
68.1k
      if (!B->isLocal())
631
0
        fatal(toString(F) +
632
0
              ": broken object: getLocalSymbols returns a non-local symbol");
633
68.1k
      auto *DR = dyn_cast<Defined>(B);
634
68.1k
635
68.1k
      // No reason to keep local undefined symbol in symtab.
636
68.1k
      if (!DR)
637
2
        continue;
638
68.1k
      if (!includeInSymtab(*B))
639
95
        continue;
640
68.0k
      if (!shouldKeepInSymtab(*DR))
641
66.1k
        continue;
642
1.87k
      In.SymTab->addSymbol(B);
643
1.87k
    }
644
3.06k
  }
645
2.51k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::copyLocalSymbols()
Line
Count
Source
624
319
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
625
319
  if (!In.SymTab)
626
0
    return;
627
371
  
for (InputFile *File : ObjectFiles)319
{
628
371
    ObjFile<ELFT> *F = cast<ObjFile<ELFT>>(File);
629
824
    for (Symbol *B : F->getLocalSymbols()) {
630
824
      if (!B->isLocal())
631
0
        fatal(toString(F) +
632
0
              ": broken object: getLocalSymbols returns a non-local symbol");
633
824
      auto *DR = dyn_cast<Defined>(B);
634
824
635
824
      // No reason to keep local undefined symbol in symtab.
636
824
      if (!DR)
637
0
        continue;
638
824
      if (!includeInSymtab(*B))
639
8
        continue;
640
816
      if (!shouldKeepInSymtab(*DR))
641
128
        continue;
642
688
      In.SymTab->addSymbol(B);
643
688
    }
644
371
  }
645
319
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::copyLocalSymbols()
Line
Count
Source
624
132
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
625
132
  if (!In.SymTab)
626
0
    return;
627
178
  
for (InputFile *File : ObjectFiles)132
{
628
178
    ObjFile<ELFT> *F = cast<ObjFile<ELFT>>(File);
629
178
    for (Symbol *B : F->getLocalSymbols()) {
630
130
      if (!B->isLocal())
631
0
        fatal(toString(F) +
632
0
              ": broken object: getLocalSymbols returns a non-local symbol");
633
130
      auto *DR = dyn_cast<Defined>(B);
634
130
635
130
      // No reason to keep local undefined symbol in symtab.
636
130
      if (!DR)
637
0
        continue;
638
130
      if (!includeInSymtab(*B))
639
0
        continue;
640
130
      if (!shouldKeepInSymtab(*DR))
641
65
        continue;
642
65
      In.SymTab->addSymbol(B);
643
65
    }
644
178
  }
645
132
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::copyLocalSymbols()
Line
Count
Source
624
1.95k
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
625
1.95k
  if (!In.SymTab)
626
4
    return;
627
2.37k
  
for (InputFile *File : ObjectFiles)1.95k
{
628
2.37k
    ObjFile<ELFT> *F = cast<ObjFile<ELFT>>(File);
629
67.1k
    for (Symbol *B : F->getLocalSymbols()) {
630
67.1k
      if (!B->isLocal())
631
0
        fatal(toString(F) +
632
0
              ": broken object: getLocalSymbols returns a non-local symbol");
633
67.1k
      auto *DR = dyn_cast<Defined>(B);
634
67.1k
635
67.1k
      // No reason to keep local undefined symbol in symtab.
636
67.1k
      if (!DR)
637
2
        continue;
638
67.0k
      if (!includeInSymtab(*B))
639
81
        continue;
640
67.0k
      if (!shouldKeepInSymtab(*DR))
641
65.9k
        continue;
642
1.07k
      In.SymTab->addSymbol(B);
643
1.07k
    }
644
2.37k
  }
645
1.95k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::copyLocalSymbols()
Line
Count
Source
624
114
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
625
114
  if (!In.SymTab)
626
0
    return;
627
145
  
for (InputFile *File : ObjectFiles)114
{
628
145
    ObjFile<ELFT> *F = cast<ObjFile<ELFT>>(File);
629
145
    for (Symbol *B : F->getLocalSymbols()) {
630
91
      if (!B->isLocal())
631
0
        fatal(toString(F) +
632
0
              ": broken object: getLocalSymbols returns a non-local symbol");
633
91
      auto *DR = dyn_cast<Defined>(B);
634
91
635
91
      // No reason to keep local undefined symbol in symtab.
636
91
      if (!DR)
637
0
        continue;
638
91
      if (!includeInSymtab(*B))
639
6
        continue;
640
85
      if (!shouldKeepInSymtab(*DR))
641
37
        continue;
642
48
      In.SymTab->addSymbol(B);
643
48
    }
644
145
  }
645
114
}
646
647
// Create a section symbol for each output section so that we can represent
648
// relocations that point to the section. If we know that no relocation is
649
// referring to a section (that happens if the section is a synthetic one), we
650
// don't create a section symbol for that section.
651
105
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
652
67.1k
  for (BaseCommand *Base : Script->SectionCommands) {
653
67.1k
    auto *Sec = dyn_cast<OutputSection>(Base);
654
67.1k
    if (!Sec)
655
1
      continue;
656
67.1k
    auto I = llvm::find_if(Sec->SectionCommands, [](BaseCommand *Base) {
657
67.1k
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
658
67.1k
        return !ISD->Sections.empty();
659
2
      return false;
660
2
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSectionSymbols()::'lambda'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
656
230
    auto I = llvm::find_if(Sec->SectionCommands, [](BaseCommand *Base) {
657
230
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
658
229
        return !ISD->Sections.empty();
659
1
      return false;
660
1
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSectionSymbols()::'lambda'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
656
178
    auto I = llvm::find_if(Sec->SectionCommands, [](BaseCommand *Base) {
657
178
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
658
178
        return !ISD->Sections.empty();
659
0
      return false;
660
0
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSectionSymbols()::'lambda'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
656
66.6k
    auto I = llvm::find_if(Sec->SectionCommands, [](BaseCommand *Base) {
657
66.6k
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
658
66.6k
        return !ISD->Sections.empty();
659
1
      return false;
660
1
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSectionSymbols()::'lambda'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
656
47
    auto I = llvm::find_if(Sec->SectionCommands, [](BaseCommand *Base) {
657
47
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
658
47
        return !ISD->Sections.empty();
659
0
      return false;
660
0
    });
661
67.1k
    if (I == Sec->SectionCommands.end())
662
5
      continue;
663
67.1k
    InputSection *IS = cast<InputSectionDescription>(*I)->Sections[0];
664
67.1k
665
67.1k
    // Relocations are not using REL[A] section symbols.
666
67.1k
    if (IS->Type == SHT_REL || 
IS->Type == SHT_RELA67.0k
)
667
196
      continue;
668
66.9k
669
66.9k
    // Unlike other synthetic sections, mergeable output sections contain data
670
66.9k
    // copied from input sections, and there may be a relocation pointing to its
671
66.9k
    // contents if -r or -emit-reloc are given.
672
66.9k
    if (isa<SyntheticSection>(IS) && 
!(IS->Flags & SHF_MERGE)1.14k
)
673
1.10k
      continue;
674
65.8k
675
65.8k
    auto *Sym =
676
65.8k
        make<Defined>(IS->File, "", STB_LOCAL, /*StOther=*/0, STT_SECTION,
677
65.8k
                      /*Value=*/0, /*Size=*/0, IS);
678
65.8k
    In.SymTab->addSymbol(Sym);
679
65.8k
  }
680
105
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSectionSymbols()
Line
Count
Source
651
14
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
652
230
  for (BaseCommand *Base : Script->SectionCommands) {
653
230
    auto *Sec = dyn_cast<OutputSection>(Base);
654
230
    if (!Sec)
655
0
      continue;
656
230
    auto I = llvm::find_if(Sec->SectionCommands, [](BaseCommand *Base) {
657
230
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
658
230
        return !ISD->Sections.empty();
659
230
      return false;
660
230
    });
661
230
    if (I == Sec->SectionCommands.end())
662
1
      continue;
663
229
    InputSection *IS = cast<InputSectionDescription>(*I)->Sections[0];
664
229
665
229
    // Relocations are not using REL[A] section symbols.
666
229
    if (IS->Type == SHT_REL || 
IS->Type == SHT_RELA192
)
667
37
      continue;
668
192
669
192
    // Unlike other synthetic sections, mergeable output sections contain data
670
192
    // copied from input sections, and there may be a relocation pointing to its
671
192
    // contents if -r or -emit-reloc are given.
672
192
    if (isa<SyntheticSection>(IS) && 
!(IS->Flags & SHF_MERGE)154
)
673
150
      continue;
674
42
675
42
    auto *Sym =
676
42
        make<Defined>(IS->File, "", STB_LOCAL, /*StOther=*/0, STT_SECTION,
677
42
                      /*Value=*/0, /*Size=*/0, IS);
678
42
    In.SymTab->addSymbol(Sym);
679
42
  }
680
14
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSectionSymbols()
Line
Count
Source
651
11
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
652
178
  for (BaseCommand *Base : Script->SectionCommands) {
653
178
    auto *Sec = dyn_cast<OutputSection>(Base);
654
178
    if (!Sec)
655
0
      continue;
656
178
    auto I = llvm::find_if(Sec->SectionCommands, [](BaseCommand *Base) {
657
178
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
658
178
        return !ISD->Sections.empty();
659
178
      return false;
660
178
    });
661
178
    if (I == Sec->SectionCommands.end())
662
0
      continue;
663
178
    InputSection *IS = cast<InputSectionDescription>(*I)->Sections[0];
664
178
665
178
    // Relocations are not using REL[A] section symbols.
666
178
    if (IS->Type == SHT_REL || 
IS->Type == SHT_RELA159
)
667
19
      continue;
668
159
669
159
    // Unlike other synthetic sections, mergeable output sections contain data
670
159
    // copied from input sections, and there may be a relocation pointing to its
671
159
    // contents if -r or -emit-reloc are given.
672
159
    if (isa<SyntheticSection>(IS) && 
!(IS->Flags & SHF_MERGE)121
)
673
121
      continue;
674
38
675
38
    auto *Sym =
676
38
        make<Defined>(IS->File, "", STB_LOCAL, /*StOther=*/0, STT_SECTION,
677
38
                      /*Value=*/0, /*Size=*/0, IS);
678
38
    In.SymTab->addSymbol(Sym);
679
38
  }
680
11
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSectionSymbols()
Line
Count
Source
651
77
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
652
66.6k
  for (BaseCommand *Base : Script->SectionCommands) {
653
66.6k
    auto *Sec = dyn_cast<OutputSection>(Base);
654
66.6k
    if (!Sec)
655
1
      continue;
656
66.6k
    auto I = llvm::find_if(Sec->SectionCommands, [](BaseCommand *Base) {
657
66.6k
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
658
66.6k
        return !ISD->Sections.empty();
659
66.6k
      return false;
660
66.6k
    });
661
66.6k
    if (I == Sec->SectionCommands.end())
662
4
      continue;
663
66.6k
    InputSection *IS = cast<InputSectionDescription>(*I)->Sections[0];
664
66.6k
665
66.6k
    // Relocations are not using REL[A] section symbols.
666
66.6k
    if (IS->Type == SHT_REL || IS->Type == SHT_RELA)
667
135
      continue;
668
66.5k
669
66.5k
    // Unlike other synthetic sections, mergeable output sections contain data
670
66.5k
    // copied from input sections, and there may be a relocation pointing to its
671
66.5k
    // contents if -r or -emit-reloc are given.
672
66.5k
    if (isa<SyntheticSection>(IS) && 
!(IS->Flags & SHF_MERGE)834
)
673
804
      continue;
674
65.7k
675
65.7k
    auto *Sym =
676
65.7k
        make<Defined>(IS->File, "", STB_LOCAL, /*StOther=*/0, STT_SECTION,
677
65.7k
                      /*Value=*/0, /*Size=*/0, IS);
678
65.7k
    In.SymTab->addSymbol(Sym);
679
65.7k
  }
680
77
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSectionSymbols()
Line
Count
Source
651
3
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
652
47
  for (BaseCommand *Base : Script->SectionCommands) {
653
47
    auto *Sec = dyn_cast<OutputSection>(Base);
654
47
    if (!Sec)
655
0
      continue;
656
47
    auto I = llvm::find_if(Sec->SectionCommands, [](BaseCommand *Base) {
657
47
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
658
47
        return !ISD->Sections.empty();
659
47
      return false;
660
47
    });
661
47
    if (I == Sec->SectionCommands.end())
662
0
      continue;
663
47
    InputSection *IS = cast<InputSectionDescription>(*I)->Sections[0];
664
47
665
47
    // Relocations are not using REL[A] section symbols.
666
47
    if (IS->Type == SHT_REL || 
IS->Type == SHT_RELA45
)
667
5
      continue;
668
42
669
42
    // Unlike other synthetic sections, mergeable output sections contain data
670
42
    // copied from input sections, and there may be a relocation pointing to its
671
42
    // contents if -r or -emit-reloc are given.
672
42
    if (isa<SyntheticSection>(IS) && 
!(IS->Flags & SHF_MERGE)32
)
673
32
      continue;
674
10
675
10
    auto *Sym =
676
10
        make<Defined>(IS->File, "", STB_LOCAL, /*StOther=*/0, STT_SECTION,
677
10
                      /*Value=*/0, /*Size=*/0, IS);
678
10
    In.SymTab->addSymbol(Sym);
679
10
  }
680
3
}
681
682
// Today's loaders have a feature to make segments read-only after
683
// processing dynamic relocations to enhance security. PT_GNU_RELRO
684
// is defined for that.
685
//
686
// This function returns true if a section needs to be put into a
687
// PT_GNU_RELRO segment.
688
158k
static bool isRelroSection(const OutputSection *Sec) {
689
158k
  if (!Config->ZRelro)
690
214
    return false;
691
158k
692
158k
  uint64_t Flags = Sec->Flags;
693
158k
694
158k
  // Non-allocatable or non-writable sections don't need RELRO because
695
158k
  // they are not writable or not even mapped to memory in the first place.
696
158k
  // RELRO is for sections that are essentially read-only but need to
697
158k
  // be writable only at process startup to allow dynamic linker to
698
158k
  // apply relocations.
699
158k
  if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
700
152k
    return false;
701
5.86k
702
5.86k
  // Once initialized, TLS data segments are used as data templates
703
5.86k
  // for a thread-local storage. For each new thread, runtime
704
5.86k
  // allocates memory for a TLS and copy templates there. No thread
705
5.86k
  // are supposed to use templates directly. Thus, it can be in RELRO.
706
5.86k
  if (Flags & SHF_TLS)
707
201
    return true;
708
5.66k
709
5.66k
  // .init_array, .preinit_array and .fini_array contain pointers to
710
5.66k
  // functions that are executed on process startup or exit. These
711
5.66k
  // pointers are set by the static linker, and they are not expected
712
5.66k
  // to change at runtime. But if you are an attacker, you could do
713
5.66k
  // interesting things by manipulating pointers in .fini_array, for
714
5.66k
  // example. So they are put into RELRO.
715
5.66k
  uint32_t Type = Sec->Type;
716
5.66k
  if (Type == SHT_INIT_ARRAY || 
Type == SHT_FINI_ARRAY5.64k
||
717
5.66k
      
Type == SHT_PREINIT_ARRAY5.62k
)
718
44
    return true;
719
5.61k
720
5.61k
  // .got contains pointers to external symbols. They are resolved by
721
5.61k
  // the dynamic linker when a module is loaded into memory, and after
722
5.61k
  // that they are not expected to change. So, it can be in RELRO.
723
5.61k
  if (In.Got && 
Sec == In.Got->getParent()4.50k
)
724
427
    return true;
725
5.19k
726
5.19k
  // .toc is a GOT-ish section for PowerPC64. Their contents are accessed
727
5.19k
  // through r2 register, which is reserved for that purpose. Since r2 is used
728
5.19k
  // for accessing .got as well, .got and .toc need to be close enough in the
729
5.19k
  // virtual address space. Usually, .toc comes just after .got. Since we place
730
5.19k
  // .got into RELRO, .toc needs to be placed into RELRO too.
731
5.19k
  if (Sec->Name.equals(".toc"))
732
42
    return true;
733
5.15k
734
5.15k
  // .got.plt contains pointers to external function symbols. They are
735
5.15k
  // by default resolved lazily, so we usually cannot put it into RELRO.
736
5.15k
  // However, if "-z now" is given, the lazy symbol resolution is
737
5.15k
  // disabled, which enables us to put it into RELRO.
738
5.15k
  if (Sec == In.GotPlt->getParent())
739
606
    return Config->ZNow;
740
4.54k
741
4.54k
  // .dynamic section contains data for the dynamic linker, and
742
4.54k
  // there's no need to write to it at runtime, so it's better to put
743
4.54k
  // it into RELRO.
744
4.54k
  if (Sec == In.Dynamic->getParent())
745
2.09k
    return true;
746
2.44k
747
2.44k
  // Sections with some special names are put into RELRO. This is a
748
2.44k
  // bit unfortunate because section names shouldn't be significant in
749
2.44k
  // ELF in spirit. But in reality many linker features depend on
750
2.44k
  // magic section names.
751
2.44k
  StringRef S = Sec->Name;
752
2.44k
  return S == ".data.rel.ro" || 
S == ".bss.rel.ro"2.42k
||
S == ".ctors"2.40k
||
753
2.44k
         
S == ".dtors"2.39k
||
S == ".jcr"2.38k
||
S == ".eh_frame"2.37k
||
754
2.44k
         
S == ".openbsd.randomdata"2.37k
;
755
2.44k
}
756
757
// We compute a rank for each section. The rank indicates where the
758
// section should be placed in the file.  Instead of using simple
759
// numbers (0,1,2...), we use a series of flags. One for each decision
760
// point when placing the section.
761
// Using flags has two key properties:
762
// * It is easy to check if a give branch was taken.
763
// * It is easy two see how similar two ranks are (see getRankProximity).
764
enum RankFlags {
765
  RF_NOT_ADDR_SET = 1 << 17,
766
  RF_NOT_ALLOC = 1 << 16,
767
  RF_NOT_INTERP = 1 << 15,
768
  RF_NOT_NOTE = 1 << 14,
769
  RF_WRITE = 1 << 13,
770
  RF_EXEC_WRITE = 1 << 12,
771
  RF_EXEC = 1 << 11,
772
  RF_RODATA = 1 << 10,
773
  RF_NOT_RELRO = 1 << 9,
774
  RF_NOT_TLS = 1 << 8,
775
  RF_BSS = 1 << 7,
776
  RF_PPC_NOT_TOCBSS = 1 << 6,
777
  RF_PPC_TOCL = 1 << 5,
778
  RF_PPC_TOC = 1 << 4,
779
  RF_PPC_GOT = 1 << 3,
780
  RF_PPC_BRANCH_LT = 1 << 2,
781
  RF_MIPS_GPREL = 1 << 1,
782
  RF_MIPS_NOT_GOT = 1 << 0
783
};
784
785
284k
static unsigned getSectionRank(const OutputSection *Sec) {
786
284k
  unsigned Rank = 0;
787
284k
788
284k
  // We want to put section specified by -T option first, so we
789
284k
  // can start assigning VA starting from them later.
790
284k
  if (Config->SectionStartMap.count(Sec->Name))
791
59
    return Rank;
792
284k
  Rank |= RF_NOT_ADDR_SET;
793
284k
794
284k
  // Allocatable sections go first to reduce the total PT_LOAD size and
795
284k
  // so debug info doesn't change addresses in actual code.
796
284k
  if (!(Sec->Flags & SHF_ALLOC))
797
205k
    return Rank | RF_NOT_ALLOC;
798
79.2k
799
79.2k
  // Put .interp first because some loaders want to see that section
800
79.2k
  // on the first page of the executable file when loaded into memory.
801
79.2k
  if (Sec->Name == ".interp")
802
10
    return Rank;
803
79.2k
  Rank |= RF_NOT_INTERP;
804
79.2k
805
79.2k
  // Put .note sections (which make up one PT_NOTE) at the beginning so that
806
79.2k
  // they are likely to be included in a core file even if core file size is
807
79.2k
  // limited. In particular, we want a .note.gnu.build-id and a .note.tag to be
808
79.2k
  // included in a core to match core files with executables.
809
79.2k
  if (Sec->Type == SHT_NOTE)
810
48
    return Rank;
811
79.1k
  Rank |= RF_NOT_NOTE;
812
79.1k
813
79.1k
  // Sort sections based on their access permission in the following
814
79.1k
  // order: R, RX, RWX, RW.  This order is based on the following
815
79.1k
  // considerations:
816
79.1k
  // * Read-only sections come first such that they go in the
817
79.1k
  //   PT_LOAD covering the program headers at the start of the file.
818
79.1k
  // * Read-only, executable sections come next.
819
79.1k
  // * Writable, executable sections follow such that .plt on
820
79.1k
  //   architectures where it needs to be writable will be placed
821
79.1k
  //   between .text and .data.
822
79.1k
  // * Writable sections come last, such that .bss lands at the very
823
79.1k
  //   end of the last PT_LOAD.
824
79.1k
  bool IsExec = Sec->Flags & SHF_EXECINSTR;
825
79.1k
  bool IsWrite = Sec->Flags & SHF_WRITE;
826
79.1k
827
79.1k
  if (IsExec) {
828
4.89k
    if (IsWrite)
829
20
      Rank |= RF_EXEC_WRITE;
830
4.87k
    else
831
4.87k
      Rank |= RF_EXEC;
832
74.2k
  } else if (IsWrite) {
833
2.98k
    Rank |= RF_WRITE;
834
71.2k
  } else if (Sec->Type == SHT_PROGBITS) {
835
65.7k
    // Make non-executable and non-writable PROGBITS sections (e.g .rodata
836
65.7k
    // .eh_frame) closer to .text. They likely contain PC or GOT relative
837
65.7k
    // relocations and there could be relocation overflow if other huge sections
838
65.7k
    // (.dynstr .dynsym) were placed in between.
839
65.7k
    Rank |= RF_RODATA;
840
65.7k
  }
841
79.1k
842
79.1k
  // Place RelRo sections first. After considering SHT_NOBITS below, the
843
79.1k
  // ordering is PT_LOAD(PT_GNU_RELRO(.data.rel.ro .bss.rel.ro) | .data .bss),
844
79.1k
  // where | marks where page alignment happens. An alternative ordering is
845
79.1k
  // PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro .bss.rel.ro) | .bss), but it may
846
79.1k
  // waste more bytes due to 2 alignment places.
847
79.1k
  if (!isRelroSection(Sec))
848
77.6k
    Rank |= RF_NOT_RELRO;
849
79.1k
850
79.1k
  // If we got here we know that both A and B are in the same PT_LOAD.
851
79.1k
852
79.1k
  // The TLS initialization block needs to be a single contiguous block in a R/W
853
79.1k
  // PT_LOAD, so stick TLS sections directly before the other RelRo R/W
854
79.1k
  // sections. Since p_filesz can be less than p_memsz, place NOBITS sections
855
79.1k
  // after PROGBITS.
856
79.1k
  if (!(Sec->Flags & SHF_TLS))
857
79.0k
    Rank |= RF_NOT_TLS;
858
79.1k
859
79.1k
  // Within TLS sections, or within other RelRo sections, or within non-RelRo
860
79.1k
  // sections, place non-NOBITS sections first.
861
79.1k
  if (Sec->Type == SHT_NOBITS)
862
462
    Rank |= RF_BSS;
863
79.1k
864
79.1k
  // Some architectures have additional ordering restrictions for sections
865
79.1k
  // within the same PT_LOAD.
866
79.1k
  if (Config->EMachine == EM_PPC64) {
867
1.01k
    // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
868
1.01k
    // that we would like to make sure appear is a specific order to maximize
869
1.01k
    // their coverage by a single signed 16-bit offset from the TOC base
870
1.01k
    // pointer. Conversely, the special .tocbss section should be first among
871
1.01k
    // all SHT_NOBITS sections. This will put it next to the loaded special
872
1.01k
    // PPC64 sections (and, thus, within reach of the TOC base pointer).
873
1.01k
    StringRef Name = Sec->Name;
874
1.01k
    if (Name != ".tocbss")
875
1.01k
      Rank |= RF_PPC_NOT_TOCBSS;
876
1.01k
877
1.01k
    if (Name == ".toc1")
878
0
      Rank |= RF_PPC_TOCL;
879
1.01k
880
1.01k
    if (Name == ".toc")
881
21
      Rank |= RF_PPC_TOC;
882
1.01k
883
1.01k
    if (Name == ".got")
884
94
      Rank |= RF_PPC_GOT;
885
1.01k
886
1.01k
    if (Name == ".branch_lt")
887
153
      Rank |= RF_PPC_BRANCH_LT;
888
1.01k
  }
889
79.1k
890
79.1k
  if (Config->EMachine == EM_MIPS) {
891
1.54k
    // All sections with SHF_MIPS_GPREL flag should be grouped together
892
1.54k
    // because data in these sections is addressable with a gp relative address.
893
1.54k
    if (Sec->Flags & SHF_MIPS_GPREL)
894
175
      Rank |= RF_MIPS_GPREL;
895
1.54k
896
1.54k
    if (Sec->Name != ".got")
897
1.37k
      Rank |= RF_MIPS_NOT_GOT;
898
1.54k
  }
899
79.1k
900
79.1k
  return Rank;
901
79.1k
}
902
903
1.67M
static bool compareSections(const BaseCommand *ACmd, const BaseCommand *BCmd) {
904
1.67M
  const OutputSection *A = cast<OutputSection>(ACmd);
905
1.67M
  const OutputSection *B = cast<OutputSection>(BCmd);
906
1.67M
907
1.67M
  if (A->SortRank != B->SortRank)
908
91.3k
    return A->SortRank < B->SortRank;
909
1.58M
910
1.58M
  if (!(A->SortRank & RF_NOT_ADDR_SET))
911
22
    return Config->SectionStartMap.lookup(A->Name) <
912
22
           Config->SectionStartMap.lookup(B->Name);
913
1.58M
  return false;
914
1.58M
}
915
916
89.2k
void PhdrEntry::add(OutputSection *Sec) {
917
89.2k
  LastSec = Sec;
918
89.2k
  if (!FirstSec)
919
11.5k
    FirstSec = Sec;
920
89.2k
  p_align = std::max(p_align, Sec->Alignment);
921
89.2k
  if (p_type == PT_LOAD)
922
83.7k
    Sec->PtLoad = this;
923
89.2k
}
924
925
// The beginning and the ending of .rel[a].plt section are marked
926
// with __rel[a]_iplt_{start,end} symbols if it is a statically linked
927
// executable. The runtime needs these symbols in order to resolve
928
// all IRELATIVE relocs on startup. For dynamic executables, we don't
929
// need these symbols, since IRELATIVE relocs are resolved through GOT
930
// and PLT. For details, see http://www.airs.com/blog/archives/403.
931
2.52k
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
932
2.52k
  if (Config->Relocatable || 
needsInterpSection()2.44k
)
933
92
    return;
934
2.43k
935
2.43k
  // By default, __rela_iplt_{start,end} belong to a dummy section 0
936
2.43k
  // because .rela.plt might be empty and thus removed from output.
937
2.43k
  // We'll override Out::ElfHeader with In.RelaIplt later when we are
938
2.43k
  // sure that .rela.plt exists in output.
939
2.43k
  ElfSym::RelaIpltStart = addOptionalRegular(
940
2.43k
      Config->IsRela ? 
"__rela_iplt_start"1.97k
:
"__rel_iplt_start"460
,
941
2.43k
      Out::ElfHeader, 0, STV_HIDDEN, STB_WEAK);
942
2.43k
943
2.43k
  ElfSym::RelaIpltEnd = addOptionalRegular(
944
2.43k
      Config->IsRela ? 
"__rela_iplt_end"1.97k
:
"__rel_iplt_end"460
,
945
2.43k
      Out::ElfHeader, 0, STV_HIDDEN, STB_WEAK);
946
2.43k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addRelIpltSymbols()
Line
Count
Source
931
319
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
932
319
  if (Config->Relocatable || 
needsInterpSection()308
)
933
12
    return;
934
307
935
307
  // By default, __rela_iplt_{start,end} belong to a dummy section 0
936
307
  // because .rela.plt might be empty and thus removed from output.
937
307
  // We'll override Out::ElfHeader with In.RelaIplt later when we are
938
307
  // sure that .rela.plt exists in output.
939
307
  ElfSym::RelaIpltStart = addOptionalRegular(
940
307
      Config->IsRela ? 
"__rela_iplt_start"9
:
"__rel_iplt_start"298
,
941
307
      Out::ElfHeader, 0, STV_HIDDEN, STB_WEAK);
942
307
943
307
  ElfSym::RelaIpltEnd = addOptionalRegular(
944
307
      Config->IsRela ? 
"__rela_iplt_end"9
:
"__rel_iplt_end"298
,
945
307
      Out::ElfHeader, 0, STV_HIDDEN, STB_WEAK);
946
307
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addRelIpltSymbols()
Line
Count
Source
931
133
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
932
133
  if (Config->Relocatable || 
needsInterpSection()122
)
933
11
    return;
934
122
935
122
  // By default, __rela_iplt_{start,end} belong to a dummy section 0
936
122
  // because .rela.plt might be empty and thus removed from output.
937
122
  // We'll override Out::ElfHeader with In.RelaIplt later when we are
938
122
  // sure that .rela.plt exists in output.
939
122
  ElfSym::RelaIpltStart = addOptionalRegular(
940
122
      Config->IsRela ? 
"__rela_iplt_start"7
:
"__rel_iplt_start"115
,
941
122
      Out::ElfHeader, 0, STV_HIDDEN, STB_WEAK);
942
122
943
122
  ElfSym::RelaIpltEnd = addOptionalRegular(
944
122
      Config->IsRela ? 
"__rela_iplt_end"7
:
"__rel_iplt_end"115
,
945
122
      Out::ElfHeader, 0, STV_HIDDEN, STB_WEAK);
946
122
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addRelIpltSymbols()
Line
Count
Source
931
1.96k
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
932
1.96k
  if (Config->Relocatable || 
needsInterpSection()1.90k
)
933
64
    return;
934
1.89k
935
1.89k
  // By default, __rela_iplt_{start,end} belong to a dummy section 0
936
1.89k
  // because .rela.plt might be empty and thus removed from output.
937
1.89k
  // We'll override Out::ElfHeader with In.RelaIplt later when we are
938
1.89k
  // sure that .rela.plt exists in output.
939
1.89k
  ElfSym::RelaIpltStart = addOptionalRegular(
940
1.89k
      Config->IsRela ? 
"__rela_iplt_start"1.89k
:
"__rel_iplt_start"3
,
941
1.89k
      Out::ElfHeader, 0, STV_HIDDEN, STB_WEAK);
942
1.89k
943
1.89k
  ElfSym::RelaIpltEnd = addOptionalRegular(
944
1.89k
      Config->IsRela ? 
"__rela_iplt_end"1.89k
:
"__rel_iplt_end"3
,
945
1.89k
      Out::ElfHeader, 0, STV_HIDDEN, STB_WEAK);
946
1.89k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addRelIpltSymbols()
Line
Count
Source
931
115
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
932
115
  if (Config->Relocatable || 
needsInterpSection()112
)
933
5
    return;
934
110
935
110
  // By default, __rela_iplt_{start,end} belong to a dummy section 0
936
110
  // because .rela.plt might be empty and thus removed from output.
937
110
  // We'll override Out::ElfHeader with In.RelaIplt later when we are
938
110
  // sure that .rela.plt exists in output.
939
110
  ElfSym::RelaIpltStart = addOptionalRegular(
940
110
      Config->IsRela ? 
"__rela_iplt_start"66
:
"__rel_iplt_start"44
,
941
110
      Out::ElfHeader, 0, STV_HIDDEN, STB_WEAK);
942
110
943
110
  ElfSym::RelaIpltEnd = addOptionalRegular(
944
110
      Config->IsRela ? 
"__rela_iplt_end"66
:
"__rel_iplt_end"44
,
945
110
      Out::ElfHeader, 0, STV_HIDDEN, STB_WEAK);
946
110
}
947
948
template <class ELFT>
949
void Writer<ELFT>::forEachRelSec(
950
2.44k
    llvm::function_ref<void(InputSectionBase &)> Fn) {
951
2.44k
  // Scan all relocations. Each relocation goes through a series
952
2.44k
  // of tests to determine if it needs special treatment, such as
953
2.44k
  // creating GOT, PLT, copy relocations, etc.
954
2.44k
  // Note that relocations for non-alloc sections are directly
955
2.44k
  // processed by InputSection::relocateNonAlloc.
956
2.44k
  for (InputSectionBase *IS : InputSections)
957
317k
    if (IS->Live && 
isa<InputSection>(IS)317k
&&
(IS->Flags & SHF_ALLOC)317k
)
958
108k
      Fn(*IS);
959
2.44k
  for (EhInputSection *ES : In.EhFrame->Sections)
960
115
    Fn(*ES);
961
2.44k
  if (In.ARMExidx && 
In.ARMExidx->Live150
)
962
149
    for (InputSection *Ex : In.ARMExidx->ExidxSections)
963
65
      Fn(*Ex);
964
2.44k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::forEachRelSec(llvm::function_ref<void (lld::elf::InputSectionBase&)>)
Line
Count
Source
950
308
    llvm::function_ref<void(InputSectionBase &)> Fn) {
951
308
  // Scan all relocations. Each relocation goes through a series
952
308
  // of tests to determine if it needs special treatment, such as
953
308
  // creating GOT, PLT, copy relocations, etc.
954
308
  // Note that relocations for non-alloc sections are directly
955
308
  // processed by InputSection::relocateNonAlloc.
956
308
  for (InputSectionBase *IS : InputSections)
957
7.20k
    if (IS->Live && 
isa<InputSection>(IS)7.13k
&&
(IS->Flags & SHF_ALLOC)7.13k
)
958
5.52k
      Fn(*IS);
959
308
  for (EhInputSection *ES : In.EhFrame->Sections)
960
4
    Fn(*ES);
961
308
  if (In.ARMExidx && 
In.ARMExidx->Live150
)
962
149
    for (InputSection *Ex : In.ARMExidx->ExidxSections)
963
65
      Fn(*Ex);
964
308
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::forEachRelSec(llvm::function_ref<void (lld::elf::InputSectionBase&)>)
Line
Count
Source
950
122
    llvm::function_ref<void(InputSectionBase &)> Fn) {
951
122
  // Scan all relocations. Each relocation goes through a series
952
122
  // of tests to determine if it needs special treatment, such as
953
122
  // creating GOT, PLT, copy relocations, etc.
954
122
  // Note that relocations for non-alloc sections are directly
955
122
  // processed by InputSection::relocateNonAlloc.
956
122
  for (InputSectionBase *IS : InputSections)
957
3.40k
    if (IS->Live && 
isa<InputSection>(IS)3.09k
&&
(IS->Flags & SHF_ALLOC)3.09k
)
958
2.48k
      Fn(*IS);
959
122
  for (EhInputSection *ES : In.EhFrame->Sections)
960
0
    Fn(*ES);
961
122
  if (In.ARMExidx && 
In.ARMExidx->Live0
)
962
0
    for (InputSection *Ex : In.ARMExidx->ExidxSections)
963
0
      Fn(*Ex);
964
122
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::forEachRelSec(llvm::function_ref<void (lld::elf::InputSectionBase&)>)
Line
Count
Source
950
1.90k
    llvm::function_ref<void(InputSectionBase &)> Fn) {
951
1.90k
  // Scan all relocations. Each relocation goes through a series
952
1.90k
  // of tests to determine if it needs special treatment, such as
953
1.90k
  // creating GOT, PLT, copy relocations, etc.
954
1.90k
  // Note that relocations for non-alloc sections are directly
955
1.90k
  // processed by InputSection::relocateNonAlloc.
956
1.90k
  for (InputSectionBase *IS : InputSections)
957
304k
    if (IS->Live && 
isa<InputSection>(IS)304k
&&
(IS->Flags & SHF_ALLOC)304k
)
958
98.4k
      Fn(*IS);
959
1.90k
  for (EhInputSection *ES : In.EhFrame->Sections)
960
107
    Fn(*ES);
961
1.90k
  if (In.ARMExidx && 
In.ARMExidx->Live0
)
962
0
    for (InputSection *Ex : In.ARMExidx->ExidxSections)
963
0
      Fn(*Ex);
964
1.90k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::forEachRelSec(llvm::function_ref<void (lld::elf::InputSectionBase&)>)
Line
Count
Source
950
112
    llvm::function_ref<void(InputSectionBase &)> Fn) {
951
112
  // Scan all relocations. Each relocation goes through a series
952
112
  // of tests to determine if it needs special treatment, such as
953
112
  // creating GOT, PLT, copy relocations, etc.
954
112
  // Note that relocations for non-alloc sections are directly
955
112
  // processed by InputSection::relocateNonAlloc.
956
112
  for (InputSectionBase *IS : InputSections)
957
2.69k
    if (IS->Live && 
isa<InputSection>(IS)2.59k
&&
(IS->Flags & SHF_ALLOC)2.59k
)
958
2.03k
      Fn(*IS);
959
112
  for (EhInputSection *ES : In.EhFrame->Sections)
960
4
    Fn(*ES);
961
112
  if (In.ARMExidx && 
In.ARMExidx->Live0
)
962
0
    for (InputSection *Ex : In.ARMExidx->ExidxSections)
963
0
      Fn(*Ex);
964
112
}
965
966
// This function generates assignments for predefined symbols (e.g. _end or
967
// _etext) and inserts them into the commands sequence to be processed at the
968
// appropriate time. This ensures that the value is going to be correct by the
969
// time any references to these symbols are processed and is equivalent to
970
// defining these symbols explicitly in the linker script.
971
2.41k
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
972
2.41k
  if (ElfSym::GlobalOffsetTable) {
973
120
    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
974
120
    // to the start of the .got or .got.plt section.
975
120
    InputSection *GotSection = In.GotPlt;
976
120
    if (!Target->GotBaseSymInGotPlt)
977
90
      GotSection = In.MipsGot ? 
cast<InputSection>(In.MipsGot)1
978
90
                              : 
cast<InputSection>(In.Got)89
;
979
120
    ElfSym::GlobalOffsetTable->Section = GotSection;
980
120
  }
981
2.41k
982
2.41k
  // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
983
2.41k
  if (ElfSym::RelaIpltStart && 
In.RelaIplt->isNeeded()7
) {
984
6
    ElfSym::RelaIpltStart->Section = In.RelaIplt;
985
6
    ElfSym::RelaIpltEnd->Section = In.RelaIplt;
986
6
    ElfSym::RelaIpltEnd->Value = In.RelaIplt->getSize();
987
6
  }
988
2.41k
989
2.41k
  PhdrEntry *Last = nullptr;
990
2.41k
  PhdrEntry *LastRO = nullptr;
991
2.41k
992
13.7k
  for (PhdrEntry *P : Phdrs) {
993
13.7k
    if (P->p_type != PT_LOAD)
994
7.47k
      continue;
995
6.32k
    Last = P;
996
6.32k
    if (!(P->p_flags & PF_W))
997
4.21k
      LastRO = P;
998
6.32k
  }
999
2.41k
1000
2.41k
  if (LastRO) {
1001
2.30k
    // _etext is the first location after the last read-only loadable segment.
1002
2.30k
    if (ElfSym::Etext1)
1003
3
      ElfSym::Etext1->Section = LastRO->LastSec;
1004
2.30k
    if (ElfSym::Etext2)
1005
1
      ElfSym::Etext2->Section = LastRO->LastSec;
1006
2.30k
  }
1007
2.41k
1008
2.41k
  if (Last) {
1009
2.31k
    // _edata points to the end of the last mapped initialized section.
1010
2.31k
    OutputSection *Edata = nullptr;
1011
79.2k
    for (OutputSection *OS : OutputSections) {
1012
79.2k
      if (OS->Type != SHT_NOBITS)
1013
78.8k
        Edata = OS;
1014
79.2k
      if (OS == Last->LastSec)
1015
2.30k
        break;
1016
79.2k
    }
1017
2.31k
1018
2.31k
    if (ElfSym::Edata1)
1019
1
      ElfSym::Edata1->Section = Edata;
1020
2.31k
    if (ElfSym::Edata2)
1021
5
      ElfSym::Edata2->Section = Edata;
1022
2.31k
1023
2.31k
    // _end is the first location after the uninitialized data region.
1024
2.31k
    if (ElfSym::End1)
1025
4
      ElfSym::End1->Section = Last->LastSec;
1026
2.31k
    if (ElfSym::End2)
1027
11
      ElfSym::End2->Section = Last->LastSec;
1028
2.31k
  }
1029
2.41k
1030
2.41k
  if (ElfSym::Bss)
1031
4
    ElfSym::Bss->Section = findSection(".bss");
1032
2.41k
1033
2.41k
  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1034
2.41k
  // be equal to the _gp symbol's value.
1035
2.41k
  if (ElfSym::MipsGp) {
1036
173
    // Find GP-relative section with the lowest address
1037
173
    // and use this address to calculate default _gp value.
1038
1.38k
    for (OutputSection *OS : OutputSections) {
1039
1.38k
      if (OS->Flags & SHF_MIPS_GPREL) {
1040
173
        ElfSym::MipsGp->Section = OS;
1041
173
        ElfSym::MipsGp->Value = 0x7ff0;
1042
173
        break;
1043
173
      }
1044
1.38k
    }
1045
173
  }
1046
2.41k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::setReservedSymbolSections()
Line
Count
Source
971
313
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
972
313
  if (ElfSym::GlobalOffsetTable) {
973
9
    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
974
9
    // to the start of the .got or .got.plt section.
975
9
    InputSection *GotSection = In.GotPlt;
976
9
    if (!Target->GotBaseSymInGotPlt)
977
2
      GotSection = In.MipsGot ? 
cast<InputSection>(In.MipsGot)0
978
2
                              : cast<InputSection>(In.Got);
979
9
    ElfSym::GlobalOffsetTable->Section = GotSection;
980
9
  }
981
313
982
313
  // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
983
313
  if (ElfSym::RelaIpltStart && 
In.RelaIplt->isNeeded()2
) {
984
2
    ElfSym::RelaIpltStart->Section = In.RelaIplt;
985
2
    ElfSym::RelaIpltEnd->Section = In.RelaIplt;
986
2
    ElfSym::RelaIpltEnd->Value = In.RelaIplt->getSize();
987
2
  }
988
313
989
313
  PhdrEntry *Last = nullptr;
990
313
  PhdrEntry *LastRO = nullptr;
991
313
992
1.84k
  for (PhdrEntry *P : Phdrs) {
993
1.84k
    if (P->p_type != PT_LOAD)
994
1.00k
      continue;
995
838
    Last = P;
996
838
    if (!(P->p_flags & PF_W))
997
560
      LastRO = P;
998
838
  }
999
313
1000
313
  if (LastRO) {
1001
299
    // _etext is the first location after the last read-only loadable segment.
1002
299
    if (ElfSym::Etext1)
1003
0
      ElfSym::Etext1->Section = LastRO->LastSec;
1004
299
    if (ElfSym::Etext2)
1005
0
      ElfSym::Etext2->Section = LastRO->LastSec;
1006
299
  }
1007
313
1008
313
  if (Last) {
1009
301
    // _edata points to the end of the last mapped initialized section.
1010
301
    OutputSection *Edata = nullptr;
1011
1.63k
    for (OutputSection *OS : OutputSections) {
1012
1.63k
      if (OS->Type != SHT_NOBITS)
1013
1.57k
        Edata = OS;
1014
1.63k
      if (OS == Last->LastSec)
1015
299
        break;
1016
1.63k
    }
1017
301
1018
301
    if (ElfSym::Edata1)
1019
0
      ElfSym::Edata1->Section = Edata;
1020
301
    if (ElfSym::Edata2)
1021
0
      ElfSym::Edata2->Section = Edata;
1022
301
1023
301
    // _end is the first location after the uninitialized data region.
1024
301
    if (ElfSym::End1)
1025
0
      ElfSym::End1->Section = Last->LastSec;
1026
301
    if (ElfSym::End2)
1027
0
      ElfSym::End2->Section = Last->LastSec;
1028
301
  }
1029
313
1030
313
  if (ElfSym::Bss)
1031
0
    ElfSym::Bss->Section = findSection(".bss");
1032
313
1033
313
  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1034
313
  // be equal to the _gp symbol's value.
1035
313
  if (ElfSym::MipsGp) {
1036
22
    // Find GP-relative section with the lowest address
1037
22
    // and use this address to calculate default _gp value.
1038
148
    for (OutputSection *OS : OutputSections) {
1039
148
      if (OS->Flags & SHF_MIPS_GPREL) {
1040
22
        ElfSym::MipsGp->Section = OS;
1041
22
        ElfSym::MipsGp->Value = 0x7ff0;
1042
22
        break;
1043
22
      }
1044
148
    }
1045
22
  }
1046
313
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::setReservedSymbolSections()
Line
Count
Source
971
130
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
972
130
  if (ElfSym::GlobalOffsetTable) {
973
0
    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
974
0
    // to the start of the .got or .got.plt section.
975
0
    InputSection *GotSection = In.GotPlt;
976
0
    if (!Target->GotBaseSymInGotPlt)
977
0
      GotSection = In.MipsGot ? cast<InputSection>(In.MipsGot)
978
0
                              : cast<InputSection>(In.Got);
979
0
    ElfSym::GlobalOffsetTable->Section = GotSection;
980
0
  }
981
130
982
130
  // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
983
130
  if (ElfSym::RelaIpltStart && 
In.RelaIplt->isNeeded()0
) {
984
0
    ElfSym::RelaIpltStart->Section = In.RelaIplt;
985
0
    ElfSym::RelaIpltEnd->Section = In.RelaIplt;
986
0
    ElfSym::RelaIpltEnd->Value = In.RelaIplt->getSize();
987
0
  }
988
130
989
130
  PhdrEntry *Last = nullptr;
990
130
  PhdrEntry *LastRO = nullptr;
991
130
992
897
  for (PhdrEntry *P : Phdrs) {
993
897
    if (P->p_type != PT_LOAD)
994
547
      continue;
995
350
    Last = P;
996
350
    if (!(P->p_flags & PF_W))
997
228
      LastRO = P;
998
350
  }
999
130
1000
130
  if (LastRO) {
1001
119
    // _etext is the first location after the last read-only loadable segment.
1002
119
    if (ElfSym::Etext1)
1003
0
      ElfSym::Etext1->Section = LastRO->LastSec;
1004
119
    if (ElfSym::Etext2)
1005
0
      ElfSym::Etext2->Section = LastRO->LastSec;
1006
119
  }
1007
130
1008
130
  if (Last) {
1009
119
    // _edata points to the end of the last mapped initialized section.
1010
119
    OutputSection *Edata = nullptr;
1011
1.05k
    for (OutputSection *OS : OutputSections) {
1012
1.05k
      if (OS->Type != SHT_NOBITS)
1013
943
        Edata = OS;
1014
1.05k
      if (OS == Last->LastSec)
1015
119
        break;
1016
1.05k
    }
1017
119
1018
119
    if (ElfSym::Edata1)
1019
0
      ElfSym::Edata1->Section = Edata;
1020
119
    if (ElfSym::Edata2)
1021
1
      ElfSym::Edata2->Section = Edata;
1022
119
1023
119
    // _end is the first location after the uninitialized data region.
1024
119
    if (ElfSym::End1)
1025
0
      ElfSym::End1->Section = Last->LastSec;
1026
119
    if (ElfSym::End2)
1027
1
      ElfSym::End2->Section = Last->LastSec;
1028
119
  }
1029
130
1030
130
  if (ElfSym::Bss)
1031
1
    ElfSym::Bss->Section = findSection(".bss");
1032
130
1033
130
  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1034
130
  // be equal to the _gp symbol's value.
1035
130
  if (ElfSym::MipsGp) {
1036
112
    // Find GP-relative section with the lowest address
1037
112
    // and use this address to calculate default _gp value.
1038
910
    for (OutputSection *OS : OutputSections) {
1039
910
      if (OS->Flags & SHF_MIPS_GPREL) {
1040
112
        ElfSym::MipsGp->Section = OS;
1041
112
        ElfSym::MipsGp->Value = 0x7ff0;
1042
112
        break;
1043
112
      }
1044
910
    }
1045
112
  }
1046
130
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::setReservedSymbolSections()
Line
Count
Source
971
1.85k
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
972
1.85k
  if (ElfSym::GlobalOffsetTable) {
973
73
    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
974
73
    // to the start of the .got or .got.plt section.
975
73
    InputSection *GotSection = In.GotPlt;
976
73
    if (!Target->GotBaseSymInGotPlt)
977
50
      GotSection = In.MipsGot ? 
cast<InputSection>(In.MipsGot)0
978
50
                              : cast<InputSection>(In.Got);
979
73
    ElfSym::GlobalOffsetTable->Section = GotSection;
980
73
  }
981
1.85k
982
1.85k
  // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
983
1.85k
  if (ElfSym::RelaIpltStart && 
In.RelaIplt->isNeeded()5
) {
984
4
    ElfSym::RelaIpltStart->Section = In.RelaIplt;
985
4
    ElfSym::RelaIpltEnd->Section = In.RelaIplt;
986
4
    ElfSym::RelaIpltEnd->Value = In.RelaIplt->getSize();
987
4
  }
988
1.85k
989
1.85k
  PhdrEntry *Last = nullptr;
990
1.85k
  PhdrEntry *LastRO = nullptr;
991
1.85k
992
10.2k
  for (PhdrEntry *P : Phdrs) {
993
10.2k
    if (P->p_type != PT_LOAD)
994
5.50k
      continue;
995
4.75k
    Last = P;
996
4.75k
    if (!(P->p_flags & PF_W))
997
3.21k
      LastRO = P;
998
4.75k
  }
999
1.85k
1000
1.85k
  if (LastRO) {
1001
1.78k
    // _etext is the first location after the last read-only loadable segment.
1002
1.78k
    if (ElfSym::Etext1)
1003
3
      ElfSym::Etext1->Section = LastRO->LastSec;
1004
1.78k
    if (ElfSym::Etext2)
1005
1
      ElfSym::Etext2->Section = LastRO->LastSec;
1006
1.78k
  }
1007
1.85k
1008
1.85k
  if (Last) {
1009
1.79k
    // _edata points to the end of the last mapped initialized section.
1010
1.79k
    OutputSection *Edata = nullptr;
1011
75.8k
    for (OutputSection *OS : OutputSections) {
1012
75.8k
      if (OS->Type != SHT_NOBITS)
1013
75.6k
        Edata = OS;
1014
75.8k
      if (OS == Last->LastSec)
1015
1.78k
        break;
1016
75.8k
    }
1017
1.79k
1018
1.79k
    if (ElfSym::Edata1)
1019
1
      ElfSym::Edata1->Section = Edata;
1020
1.79k
    if (ElfSym::Edata2)
1021
4
      ElfSym::Edata2->Section = Edata;
1022
1.79k
1023
1.79k
    // _end is the first location after the uninitialized data region.
1024
1.79k
    if (ElfSym::End1)
1025
4
      ElfSym::End1->Section = Last->LastSec;
1026
1.79k
    if (ElfSym::End2)
1027
10
      ElfSym::End2->Section = Last->LastSec;
1028
1.79k
  }
1029
1.85k
1030
1.85k
  if (ElfSym::Bss)
1031
3
    ElfSym::Bss->Section = findSection(".bss");
1032
1.85k
1033
1.85k
  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1034
1.85k
  // be equal to the _gp symbol's value.
1035
1.85k
  if (ElfSym::MipsGp) {
1036
3
    // Find GP-relative section with the lowest address
1037
3
    // and use this address to calculate default _gp value.
1038
15
    for (OutputSection *OS : OutputSections) {
1039
15
      if (OS->Flags & SHF_MIPS_GPREL) {
1040
3
        ElfSym::MipsGp->Section = OS;
1041
3
        ElfSym::MipsGp->Value = 0x7ff0;
1042
3
        break;
1043
3
      }
1044
15
    }
1045
3
  }
1046
1.85k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::setReservedSymbolSections()
Line
Count
Source
971
108
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
972
108
  if (ElfSym::GlobalOffsetTable) {
973
38
    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
974
38
    // to the start of the .got or .got.plt section.
975
38
    InputSection *GotSection = In.GotPlt;
976
38
    if (!Target->GotBaseSymInGotPlt)
977
38
      GotSection = In.MipsGot ? 
cast<InputSection>(In.MipsGot)1
978
38
                              : 
cast<InputSection>(In.Got)37
;
979
38
    ElfSym::GlobalOffsetTable->Section = GotSection;
980
38
  }
981
108
982
108
  // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
983
108
  if (ElfSym::RelaIpltStart && 
In.RelaIplt->isNeeded()0
) {
984
0
    ElfSym::RelaIpltStart->Section = In.RelaIplt;
985
0
    ElfSym::RelaIpltEnd->Section = In.RelaIplt;
986
0
    ElfSym::RelaIpltEnd->Value = In.RelaIplt->getSize();
987
0
  }
988
108
989
108
  PhdrEntry *Last = nullptr;
990
108
  PhdrEntry *LastRO = nullptr;
991
108
992
791
  for (PhdrEntry *P : Phdrs) {
993
791
    if (P->p_type != PT_LOAD)
994
418
      continue;
995
373
    Last = P;
996
373
    if (!(P->p_flags & PF_W))
997
206
      LastRO = P;
998
373
  }
999
108
1000
108
  if (LastRO) {
1001
105
    // _etext is the first location after the last read-only loadable segment.
1002
105
    if (ElfSym::Etext1)
1003
0
      ElfSym::Etext1->Section = LastRO->LastSec;
1004
105
    if (ElfSym::Etext2)
1005
0
      ElfSym::Etext2->Section = LastRO->LastSec;
1006
105
  }
1007
108
1008
108
  if (Last) {
1009
105
    // _edata points to the end of the last mapped initialized section.
1010
105
    OutputSection *Edata = nullptr;
1011
783
    for (OutputSection *OS : OutputSections) {
1012
783
      if (OS->Type != SHT_NOBITS)
1013
707
        Edata = OS;
1014
783
      if (OS == Last->LastSec)
1015
105
        break;
1016
783
    }
1017
105
1018
105
    if (ElfSym::Edata1)
1019
0
      ElfSym::Edata1->Section = Edata;
1020
105
    if (ElfSym::Edata2)
1021
0
      ElfSym::Edata2->Section = Edata;
1022
105
1023
105
    // _end is the first location after the uninitialized data region.
1024
105
    if (ElfSym::End1)
1025
0
      ElfSym::End1->Section = Last->LastSec;
1026
105
    if (ElfSym::End2)
1027
0
      ElfSym::End2->Section = Last->LastSec;
1028
105
  }
1029
108
1030
108
  if (ElfSym::Bss)
1031
0
    ElfSym::Bss->Section = findSection(".bss");
1032
108
1033
108
  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1034
108
  // be equal to the _gp symbol's value.
1035
108
  if (ElfSym::MipsGp) {
1036
36
    // Find GP-relative section with the lowest address
1037
36
    // and use this address to calculate default _gp value.
1038
307
    for (OutputSection *OS : OutputSections) {
1039
307
      if (OS->Flags & SHF_MIPS_GPREL) {
1040
36
        ElfSym::MipsGp->Section = OS;
1041
36
        ElfSym::MipsGp->Value = 0x7ff0;
1042
36
        break;
1043
36
      }
1044
307
    }
1045
36
  }
1046
108
}
1047
1048
// We want to find how similar two ranks are.
1049
// The more branches in getSectionRank that match, the more similar they are.
1050
// Since each branch corresponds to a bit flag, we can just use
1051
// countLeadingZeros.
1052
401k
static int getRankProximityAux(OutputSection *A, OutputSection *B) {
1053
401k
  return countLeadingZeros(A->SortRank ^ B->SortRank);
1054
401k
}
1055
1056
401k
static int getRankProximity(OutputSection *A, BaseCommand *B) {
1057
401k
  auto *Sec = dyn_cast<OutputSection>(B);
1058
401k
  return (Sec && 
Sec->Live401k
) ?
getRankProximityAux(A, Sec)401k
:
-1347
;
1059
401k
}
1060
1061
// When placing orphan sections, we want to place them after symbol assignments
1062
// so that an orphan after
1063
//   begin_foo = .;
1064
//   foo : { *(foo) }
1065
//   end_foo = .;
1066
// doesn't break the intended meaning of the begin/end symbols.
1067
// We don't want to go over sections since findOrphanPos is the
1068
// one in charge of deciding the order of the sections.
1069
// We don't want to go over changes to '.', since doing so in
1070
//  rx_sec : { *(rx_sec) }
1071
//  . = ALIGN(0x1000);
1072
//  /* The RW PT_LOAD starts here*/
1073
//  rw_sec : { *(rw_sec) }
1074
// would mean that the RW PT_LOAD would become unaligned.
1075
729
static bool shouldSkip(BaseCommand *Cmd) {
1076
729
  if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd))
1077
215
    return Assign->Name != ".";
1078
514
  return false;
1079
514
}
1080
1081
// We want to place orphan sections so that they share as much
1082
// characteristics with their neighbors as possible. For example, if
1083
// both are rw, or both are tls.
1084
static std::vector<BaseCommand *>::iterator
1085
findOrphanPos(std::vector<BaseCommand *>::iterator B,
1086
1.08k
              std::vector<BaseCommand *>::iterator E) {
1087
1.08k
  OutputSection *Sec = cast<OutputSection>(*E);
1088
1.08k
1089
1.08k
  // Find the first element that has as close a rank as possible.
1090
133k
  auto I = std::max_element(B, E, [=](BaseCommand *A, BaseCommand *B) {
1091
133k
    return getRankProximity(Sec, A) < getRankProximity(Sec, B);
1092
133k
  });
1093
1.08k
  if (I == E)
1094
73
    return E;
1095
1.00k
1096
1.00k
  // Consider all existing sections with the same proximity.
1097
1.00k
  int Proximity = getRankProximity(Sec, *I);
1098
134k
  for (; I != E; 
++I133k
) {
1099
133k
    auto *CurSec = dyn_cast<OutputSection>(*I);
1100
133k
    if (!CurSec || 
!CurSec->Live133k
)
1101
202
      continue;
1102
133k
    if (getRankProximity(Sec, CurSec) != Proximity ||
1103
133k
        
Sec->SortRank < CurSec->SortRank133k
)
1104
188
      break;
1105
133k
  }
1106
1.00k
1107
1.28k
  auto IsLiveOutputSec = [](BaseCommand *Cmd) {
1108
1.28k
    auto *OS = dyn_cast<OutputSection>(Cmd);
1109
1.28k
    return OS && 
OS->Live1.12k
;
1110
1.28k
  };
1111
1.00k
  auto J = std::find_if(llvm::make_reverse_iterator(I),
1112
1.00k
                        llvm::make_reverse_iterator(B), IsLiveOutputSec);
1113
1.00k
  I = J.base();
1114
1.00k
1115
1.00k
  // As a special case, if the orphan section is the last section, put
1116
1.00k
  // it at the very end, past any other commands.
1117
1.00k
  // This matches bfd's behavior and is convenient when the linker script fully
1118
1.00k
  // specifies the start of the file, but doesn't care about the end (the non
1119
1.00k
  // alloc sections for example).
1120
1.00k
  auto NextSec = std::find_if(I, E, IsLiveOutputSec);
1121
1.00k
  if (NextSec == E)
1122
821
    return E;
1123
188
1124
194
  
while (188
I != E && shouldSkip(*I))
1125
6
    ++I;
1126
188
  return I;
1127
188
}
1128
1129
// Builds section order for handling --symbol-ordering-file.
1130
2.32k
static DenseMap<const InputSectionBase *, int> buildSectionOrder() {
1131
2.32k
  DenseMap<const InputSectionBase *, int> SectionOrder;
1132
2.32k
  // Use the rarely used option -call-graph-ordering-file to sort sections.
1133
2.32k
  if (!Config->CallGraphProfile.empty())
1134
10
    return computeCallGraphProfileOrder();
1135
2.31k
1136
2.31k
  if (Config->SymbolOrderingFile.empty())
1137
2.28k
    return SectionOrder;
1138
32
1139
32
  struct SymbolOrderEntry {
1140
32
    int Priority;
1141
32
    bool Present;
1142
32
  };
1143
32
1144
32
  // Build a map from symbols to their priorities. Symbols that didn't
1145
32
  // appear in the symbol ordering file have the lowest priority 0.
1146
32
  // All explicitly mentioned symbols have negative (higher) priorities.
1147
32
  DenseMap<StringRef, SymbolOrderEntry> SymbolOrder;
1148
32
  int Priority = -Config->SymbolOrderingFile.size();
1149
32
  for (StringRef S : Config->SymbolOrderingFile)
1150
68
    SymbolOrder.insert({S, {Priority++, false}});
1151
32
1152
32
  // Build a map from sections to their priorities.
1153
282
  auto AddSym = [&](Symbol &Sym) {
1154
282
    auto It = SymbolOrder.find(Sym.getName());
1155
282
    if (It == SymbolOrder.end())
1156
217
      return;
1157
65
    SymbolOrderEntry &Ent = It->second;
1158
65
    Ent.Present = true;
1159
65
1160
65
    maybeWarnUnorderableSymbol(&Sym);
1161
65
1162
65
    if (auto *D = dyn_cast<Defined>(&Sym)) {
1163
61
      if (auto *Sec = dyn_cast_or_null<InputSectionBase>(D->Section)) {
1164
54
        int &Priority = SectionOrder[cast<InputSectionBase>(Sec->Repl)];
1165
54
        Priority = std::min(Priority, Ent.Priority);
1166
54
      }
1167
61
    }
1168
65
  };
1169
32
1170
32
  // We want both global and local symbols. We get the global ones from the
1171
32
  // symbol table and iterate the object files for the local ones.
1172
32
  for (Symbol *Sym : Symtab->getSymbols())
1173
174
    if (!Sym->isLazy())
1174
173
      AddSym(*Sym);
1175
32
  for (InputFile *File : ObjectFiles)
1176
40
    for (Symbol *Sym : File->getSymbols())
1177
293
      if (Sym->isLocal())
1178
109
        AddSym(*Sym);
1179
32
1180
32
  if (Config->WarnSymbolOrdering)
1181
31
    for (auto OrderEntry : SymbolOrder)
1182
67
      if (!OrderEntry.second.Present)
1183
4
        warn("symbol ordering file: no such symbol: " + OrderEntry.first);
1184
32
1185
32
  return SectionOrder;
1186
32
}
1187
1188
// Sorts the sections in ISD according to the provided section order.
1189
static void
1190
sortISDBySectionOrder(InputSectionDescription *ISD,
1191
202
                      const DenseMap<const InputSectionBase *, int> &Order) {
1192
202
  std::vector<InputSection *> UnorderedSections;
1193
202
  std::vector<std::pair<InputSection *, int>> OrderedSections;
1194
202
  uint64_t UnorderedSize = 0;
1195
202
1196
342
  for (InputSection *IS : ISD->Sections) {
1197
342
    auto I = Order.find(IS);
1198
342
    if (I == Order.end()) {
1199
249
      UnorderedSections.push_back(IS);
1200
249
      UnorderedSize += IS->getSize();
1201
249
      continue;
1202
249
    }
1203
93
    OrderedSections.push_back({IS, I->second});
1204
93
  }
1205
202
  llvm::sort(OrderedSections, [&](std::pair<InputSection *, int> A,
1206
202
                                  std::pair<InputSection *, int> B) {
1207
121
    return A.second < B.second;
1208
121
  });
1209
202
1210
202
  // Find an insertion point for the ordered section list in the unordered
1211
202
  // section list. On targets with limited-range branches, this is the mid-point
1212
202
  // of the unordered section list. This decreases the likelihood that a range
1213
202
  // extension thunk will be needed to enter or exit the ordered region. If the
1214
202
  // ordered section list is a list of hot functions, we can generally expect
1215
202
  // the ordered functions to be called more often than the unordered functions,
1216
202
  // making it more likely that any particular call will be within range, and
1217
202
  // therefore reducing the number of thunks required.
1218
202
  //
1219
202
  // For example, imagine that you have 8MB of hot code and 32MB of cold code.
1220
202
  // If the layout is:
1221
202
  //
1222
202
  // 8MB hot
1223
202
  // 32MB cold
1224
202
  //
1225
202
  // only the first 8-16MB of the cold code (depending on which hot function it
1226
202
  // is actually calling) can call the hot code without a range extension thunk.
1227
202
  // However, if we use this layout:
1228
202
  //
1229
202
  // 16MB cold
1230
202
  // 8MB hot
1231
202
  // 16MB cold
1232
202
  //
1233
202
  // both the last 8-16MB of the first block of cold code and the first 8-16MB
1234
202
  // of the second block of cold code can call the hot code without a thunk. So
1235
202
  // we effectively double the amount of code that could potentially call into
1236
202
  // the hot code without a thunk.
1237
202
  size_t InsPt = 0;
1238
202
  if (Target->getThunkSectionSpacing() && 
!OrderedSections.empty()6
) {
1239
1
    uint64_t UnorderedPos = 0;
1240
4
    for (; InsPt != UnorderedSections.size(); 
++InsPt3
) {
1241
4
      UnorderedPos += UnorderedSections[InsPt]->getSize();
1242
4
      if (UnorderedPos > UnorderedSize / 2)
1243
1
        break;
1244
4
    }
1245
1
  }
1246
202
1247
202
  ISD->Sections.clear();
1248
202
  for (InputSection *IS : makeArrayRef(UnorderedSections).slice(0, InsPt))
1249
3
    ISD->Sections.push_back(IS);
1250
202
  for (std::pair<InputSection *, int> P : OrderedSections)
1251
93
    ISD->Sections.push_back(P.first);
1252
202
  for (InputSection *IS : makeArrayRef(UnorderedSections).slice(InsPt))
1253
246
    ISD->Sections.push_back(IS);
1254
202
}
1255
1256
static void sortSection(OutputSection *Sec,
1257
284k
                        const DenseMap<const InputSectionBase *, int> &Order) {
1258
284k
  StringRef Name = Sec->Name;
1259
284k
1260
284k
  // Sort input sections by section name suffixes for
1261
284k
  // __attribute__((init_priority(N))).
1262
284k
  if (Name == ".init_array" || 
Name == ".fini_array"284k
) {
1263
11
    if (!Script->HasSectionsCommand)
1264
9
      Sec->sortInitFini();
1265
11
    return;
1266
11
  }
1267
284k
1268
284k
  // Sort input sections by the special rule for .ctors and .dtors.
1269
284k
  if (Name == ".ctors" || 
Name == ".dtors"284k
) {
1270
8
    if (!Script->HasSectionsCommand)
1271
8
      Sec->sortCtorsDtors();
1272
8
    return;
1273
8
  }
1274
284k
1275
284k
  // Never sort these.
1276
284k
  if (Name == ".init" || 
Name == ".fini"284k
)
1277
14
    return;
1278
284k
1279
284k
  // .toc is allocated just after .got and is accessed using GOT-relative
1280
284k
  // relocations. Object files compiled with small code model have an
1281
284k
  // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations.
1282
284k
  // To reduce the risk of relocation overflow, .toc contents are sorted so that
1283
284k
  // sections having smaller relocation offsets are at beginning of .toc
1284
284k
  if (Config->EMachine == EM_PPC64 && 
Name == ".toc"1.63k
) {
1285
21
    if (Script->HasSectionsCommand)
1286
2
      return;
1287
19
    assert(Sec->SectionCommands.size() == 1);
1288
19
    auto *ISD = cast<InputSectionDescription>(Sec->SectionCommands[0]);
1289
19
    llvm::stable_sort(ISD->Sections,
1290
19
                      [](const InputSection *A, const InputSection *B) -> bool {
1291
18
                        return A->File->PPC64SmallCodeModelTocRelocs &&
1292
18
                               
!B->File->PPC64SmallCodeModelTocRelocs13
;
1293
18
                      });
1294
19
    return;
1295
19
  }
1296
284k
1297
284k
  // Sort input sections by priority using the list provided
1298
284k
  // by --symbol-ordering-file.
1299
284k
  if (!Order.empty())
1300
202
    for (BaseCommand *B : Sec->SectionCommands)
1301
204
      if (auto *ISD = dyn_cast<InputSectionDescription>(B))
1302
202
        sortISDBySectionOrder(ISD, Order);
1303
284k
}
1304
1305
// If no layout was provided by linker script, we want to apply default
1306
// sorting for special input sections. This also handles --symbol-ordering-file.
1307
2.32k
template <class ELFT> void Writer<ELFT>::sortInputSections() {
1308
2.32k
  // Build the order once since it is expensive.
1309
2.32k
  DenseMap<const InputSectionBase *, int> Order = buildSectionOrder();
1310
2.32k
  for (BaseCommand *Base : Script->SectionCommands)
1311
285k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1312
284k
      sortSection(Sec, Order);
1313
2.32k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::sortInputSections()
Line
Count
Source
1307
302
template <class ELFT> void Writer<ELFT>::sortInputSections() {
1308
302
  // Build the order once since it is expensive.
1309
302
  DenseMap<const InputSectionBase *, int> Order = buildSectionOrder();
1310
302
  for (BaseCommand *Base : Script->SectionCommands)
1311
2.92k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1312
2.90k
      sortSection(Sec, Order);
1313
302
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortInputSections()
Line
Count
Source
1307
119
template <class ELFT> void Writer<ELFT>::sortInputSections() {
1308
119
  // Build the order once since it is expensive.
1309
119
  DenseMap<const InputSectionBase *, int> Order = buildSectionOrder();
1310
119
  for (BaseCommand *Base : Script->SectionCommands)
1311
1.53k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1312
1.53k
      sortSection(Sec, Order);
1313
119
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::sortInputSections()
Line
Count
Source
1307
1.80k
template <class ELFT> void Writer<ELFT>::sortInputSections() {
1308
1.80k
  // Build the order once since it is expensive.
1309
1.80k
  DenseMap<const InputSectionBase *, int> Order = buildSectionOrder();
1310
1.80k
  for (BaseCommand *Base : Script->SectionCommands)
1311
279k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1312
279k
      sortSection(Sec, Order);
1313
1.80k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::sortInputSections()
Line
Count
Source
1307
105
template <class ELFT> void Writer<ELFT>::sortInputSections() {
1308
105
  // Build the order once since it is expensive.
1309
105
  DenseMap<const InputSectionBase *, int> Order = buildSectionOrder();
1310
105
  for (BaseCommand *Base : Script->SectionCommands)
1311
1.22k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1312
1.20k
      sortSection(Sec, Order);
1313
105
}
1314
1315
2.41k
template <class ELFT> void Writer<ELFT>::sortSections() {
1316
2.41k
  Script->adjustSectionsBeforeSorting();
1317
2.41k
1318
2.41k
  // Don't sort if using -r. It is not necessary and we want to preserve the
1319
2.41k
  // relative order for SHF_LINK_ORDER sections.
1320
2.41k
  if (Config->Relocatable)
1321
82
    return;
1322
2.32k
1323
2.32k
  sortInputSections();
1324
2.32k
1325
285k
  for (BaseCommand *Base : Script->SectionCommands) {
1326
285k
    auto *OS = dyn_cast<OutputSection>(Base);
1327
285k
    if (!OS)
1328
409
      continue;
1329
284k
    OS->SortRank = getSectionRank(OS);
1330
284k
1331
284k
    // We want to assign rude approximation values to OutSecOff fields
1332
284k
    // to know the relative order of the input sections. We use it for
1333
284k
    // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
1334
284k
    uint64_t I = 0;
1335
284k
    for (InputSection *Sec : getInputSections(OS))
1336
287k
      Sec->OutSecOff = I++;
1337
284k
  }
1338
2.32k
1339
2.32k
  if (!Script->HasSectionsCommand) {
1340
1.88k
    // We know that all the OutputSections are contiguous in this case.
1341
3.85k
    auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::sortSections()::'lambda'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1341
519
    auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortSections()::'lambda'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1341
220
    auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::sortSections()::'lambda'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1341
2.89k
    auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::sortSections()::'lambda'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1341
221
    auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
1342
1.88k
    std::stable_sort(
1343
1.88k
        llvm::find_if(Script->SectionCommands, IsSection),
1344
1.88k
        llvm::find_if(llvm::reverse(Script->SectionCommands), IsSection).base(),
1345
1.88k
        compareSections);
1346
1.88k
    return;
1347
1.88k
  }
1348
444
1349
444
  // Orphan sections are sections present in the input files which are
1350
444
  // not explicitly placed into the output file by the linker script.
1351
444
  //
1352
444
  // The sections in the linker script are already in the correct
1353
444
  // order. We have to figuere out where to insert the orphan
1354
444
  // sections.
1355
444
  //
1356
444
  // The order of the sections in the script is arbitrary and may not agree with
1357
444
  // compareSections. This means that we cannot easily define a strict weak
1358
444
  // ordering. To see why, consider a comparison of a section in the script and
1359
444
  // one not in the script. We have a two simple options:
1360
444
  // * Make them equivalent (a is not less than b, and b is not less than a).
1361
444
  //   The problem is then that equivalence has to be transitive and we can
1362
444
  //   have sections a, b and c with only b in a script and a less than c
1363
444
  //   which breaks this property.
1364
444
  // * Use compareSectionsNonScript. Given that the script order doesn't have
1365
444
  //   to match, we can end up with sections a, b, c, d where b and c are in the
1366
444
  //   script and c is compareSectionsNonScript less than b. In which case d
1367
444
  //   can be equivalent to c, a to b and d < a. As a concrete example:
1368
444
  //   .a (rx) # not in script
1369
444
  //   .b (rx) # in script
1370
444
  //   .c (ro) # in script
1371
444
  //   .d (ro) # not in script
1372
444
  //
1373
444
  // The way we define an order then is:
1374
444
  // *  Sort only the orphan sections. They are in the end right now.
1375
444
  // *  Move each orphan section to its preferred position. We try
1376
444
  //    to put each section in the last position where it can share
1377
444
  //    a PT_LOAD.
1378
444
  //
1379
444
  // There is some ambiguity as to where exactly a new entry should be
1380
444
  // inserted, because Commands contains not only output section
1381
444
  // commands but also other types of commands such as symbol assignment
1382
444
  // expressions. There's no correct answer here due to the lack of the
1383
444
  // formal specification of the linker script. We use heuristics to
1384
444
  // determine whether a new output command should be added before or
1385
444
  // after another commands. For the details, look at shouldSkip
1386
444
  // function.
1387
444
1388
444
  auto I = Script->SectionCommands.begin();
1389
444
  auto E = Script->SectionCommands.end();
1390
1.45k
  auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
1391
1.45k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1392
1.13k
      return Sec->SectionIndex == UINT32_MAX;
1393
318
    return false;
1394
318
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::sortSections()::'lambda0'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1390
170
  auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
1391
170
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1392
152
      return Sec->SectionIndex == UINT32_MAX;
1393
18
    return false;
1394
18
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortSections()::'lambda0'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1390
25
  auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
1391
25
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1392
22
      return Sec->SectionIndex == UINT32_MAX;
1393
3
    return false;
1394
3
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::sortSections()::'lambda0'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1390
1.24k
  auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
1391
1.24k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1392
954
      return Sec->SectionIndex == UINT32_MAX;
1393
293
    return false;
1394
293
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::sortSections()::'lambda0'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1390
13
  auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
1391
13
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1392
9
      return Sec->SectionIndex == UINT32_MAX;
1393
4
    return false;
1394
4
  });
1395
444
1396
444
  // Sort the orphan sections.
1397
444
  std::stable_sort(NonScriptI, E, compareSections);
1398
444
1399
444
  // As a horrible special case, skip the first . assignment if it is before any
1400
444
  // section. We do this because it is common to set a load address by starting
1401
444
  // the script with ". = 0xabcd" and the expectation is that every section is
1402
444
  // after that.
1403
444
  auto FirstSectionOrDotAssignment =
1404
535
      std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::sortSections()::'lambda1'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1404
43
      std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortSections()::'lambda1'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1404
9
      std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::sortSections()::'lambda1'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1404
476
      std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::sortSections()::'lambda1'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1404
7
      std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
1405
444
  if (FirstSectionOrDotAssignment != E &&
1406
444
      isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
1407
110
    ++FirstSectionOrDotAssignment;
1408
444
  I = FirstSectionOrDotAssignment;
1409
444
1410
1.52k
  while (NonScriptI != E) {
1411
1.08k
    auto Pos = findOrphanPos(I, NonScriptI);
1412
1.08k
    OutputSection *Orphan = cast<OutputSection>(*NonScriptI);
1413
1.08k
1414
1.08k
    // As an optimization, find all sections with the same sort rank
1415
1.08k
    // and insert them with one rotate.
1416
1.08k
    unsigned Rank = Orphan->SortRank;
1417
132k
    auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
1418
132k
      return cast<OutputSection>(Cmd)->SortRank != Rank;
1419
132k
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::sortSections()::'lambda2'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1417
281
    auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
1418
281
      return cast<OutputSection>(Cmd)->SortRank != Rank;
1419
281
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortSections()::'lambda2'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1417
91
    auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
1418
91
      return cast<OutputSection>(Cmd)->SortRank != Rank;
1419
91
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::sortSections()::'lambda2'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1417
132k
    auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
1418
132k
      return cast<OutputSection>(Cmd)->SortRank != Rank;
1419
132k
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::sortSections()::'lambda2'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1417
40
    auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
1418
40
      return cast<OutputSection>(Cmd)->SortRank != Rank;
1419
40
    });
1420
1.08k
    std::rotate(Pos, NonScriptI, End);
1421
1.08k
    NonScriptI = End;
1422
1.08k
  }
1423
444
1424
444
  Script->adjustSectionsAfterSorting();
1425
444
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::sortSections()
Line
Count
Source
1315
313
template <class ELFT> void Writer<ELFT>::sortSections() {
1316
313
  Script->adjustSectionsBeforeSorting();
1317
313
1318
313
  // Don't sort if using -r. It is not necessary and we want to preserve the
1319
313
  // relative order for SHF_LINK_ORDER sections.
1320
313
  if (Config->Relocatable)
1321
11
    return;
1322
302
1323
302
  sortInputSections();
1324
302
1325
2.92k
  for (BaseCommand *Base : Script->SectionCommands) {
1326
2.92k
    auto *OS = dyn_cast<OutputSection>(Base);
1327
2.92k
    if (!OS)
1328
19
      continue;
1329
2.90k
    OS->SortRank = getSectionRank(OS);
1330
2.90k
1331
2.90k
    // We want to assign rude approximation values to OutSecOff fields
1332
2.90k
    // to know the relative order of the input sections. We use it for
1333
2.90k
    // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
1334
2.90k
    uint64_t I = 0;
1335
2.90k
    for (InputSection *Sec : getInputSections(OS))
1336
3.36k
      Sec->OutSecOff = I++;
1337
2.90k
  }
1338
302
1339
302
  if (!Script->HasSectionsCommand) {
1340
259
    // We know that all the OutputSections are contiguous in this case.
1341
259
    auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
1342
259
    std::stable_sort(
1343
259
        llvm::find_if(Script->SectionCommands, IsSection),
1344
259
        llvm::find_if(llvm::reverse(Script->SectionCommands), IsSection).base(),
1345
259
        compareSections);
1346
259
    return;
1347
259
  }
1348
43
1349
43
  // Orphan sections are sections present in the input files which are
1350
43
  // not explicitly placed into the output file by the linker script.
1351
43
  //
1352
43
  // The sections in the linker script are already in the correct
1353
43
  // order. We have to figuere out where to insert the orphan
1354
43
  // sections.
1355
43
  //
1356
43
  // The order of the sections in the script is arbitrary and may not agree with
1357
43
  // compareSections. This means that we cannot easily define a strict weak
1358
43
  // ordering. To see why, consider a comparison of a section in the script and
1359
43
  // one not in the script. We have a two simple options:
1360
43
  // * Make them equivalent (a is not less than b, and b is not less than a).
1361
43
  //   The problem is then that equivalence has to be transitive and we can
1362
43
  //   have sections a, b and c with only b in a script and a less than c
1363
43
  //   which breaks this property.
1364
43
  // * Use compareSectionsNonScript. Given that the script order doesn't have
1365
43
  //   to match, we can end up with sections a, b, c, d where b and c are in the
1366
43
  //   script and c is compareSectionsNonScript less than b. In which case d
1367
43
  //   can be equivalent to c, a to b and d < a. As a concrete example:
1368
43
  //   .a (rx) # not in script
1369
43
  //   .b (rx) # in script
1370
43
  //   .c (ro) # in script
1371
43
  //   .d (ro) # not in script
1372
43
  //
1373
43
  // The way we define an order then is:
1374
43
  // *  Sort only the orphan sections. They are in the end right now.
1375
43
  // *  Move each orphan section to its preferred position. We try
1376
43
  //    to put each section in the last position where it can share
1377
43
  //    a PT_LOAD.
1378
43
  //
1379
43
  // There is some ambiguity as to where exactly a new entry should be
1380
43
  // inserted, because Commands contains not only output section
1381
43
  // commands but also other types of commands such as symbol assignment
1382
43
  // expressions. There's no correct answer here due to the lack of the
1383
43
  // formal specification of the linker script. We use heuristics to
1384
43
  // determine whether a new output command should be added before or
1385
43
  // after another commands. For the details, look at shouldSkip
1386
43
  // function.
1387
43
1388
43
  auto I = Script->SectionCommands.begin();
1389
43
  auto E = Script->SectionCommands.end();
1390
43
  auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
1391
43
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1392
43
      return Sec->SectionIndex == UINT32_MAX;
1393
43
    return false;
1394
43
  });
1395
43
1396
43
  // Sort the orphan sections.
1397
43
  std::stable_sort(NonScriptI, E, compareSections);
1398
43
1399
43
  // As a horrible special case, skip the first . assignment if it is before any
1400
43
  // section. We do this because it is common to set a load address by starting
1401
43
  // the script with ". = 0xabcd" and the expectation is that every section is
1402
43
  // after that.
1403
43
  auto FirstSectionOrDotAssignment =
1404
43
      std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
1405
43
  if (FirstSectionOrDotAssignment != E &&
1406
43
      isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
1407
18
    ++FirstSectionOrDotAssignment;
1408
43
  I = FirstSectionOrDotAssignment;
1409
43
1410
149
  while (NonScriptI != E) {
1411
106
    auto Pos = findOrphanPos(I, NonScriptI);
1412
106
    OutputSection *Orphan = cast<OutputSection>(*NonScriptI);
1413
106
1414
106
    // As an optimization, find all sections with the same sort rank
1415
106
    // and insert them with one rotate.
1416
106
    unsigned Rank = Orphan->SortRank;
1417
106
    auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
1418
106
      return cast<OutputSection>(Cmd)->SortRank != Rank;
1419
106
    });
1420
106
    std::rotate(Pos, NonScriptI, End);
1421
106
    NonScriptI = End;
1422
106
  }
1423
43
1424
43
  Script->adjustSectionsAfterSorting();
1425
43
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortSections()
Line
Count
Source
1315
130
template <class ELFT> void Writer<ELFT>::sortSections() {
1316
130
  Script->adjustSectionsBeforeSorting();
1317
130
1318
130
  // Don't sort if using -r. It is not necessary and we want to preserve the
1319
130
  // relative order for SHF_LINK_ORDER sections.
1320
130
  if (Config->Relocatable)
1321
11
    return;
1322
119
1323
119
  sortInputSections();
1324
119
1325
1.53k
  for (BaseCommand *Base : Script->SectionCommands) {
1326
1.53k
    auto *OS = dyn_cast<OutputSection>(Base);
1327
1.53k
    if (!OS)
1328
3
      continue;
1329
1.53k
    OS->SortRank = getSectionRank(OS);
1330
1.53k
1331
1.53k
    // We want to assign rude approximation values to OutSecOff fields
1332
1.53k
    // to know the relative order of the input sections. We use it for
1333
1.53k
    // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
1334
1.53k
    uint64_t I = 0;
1335
1.53k
    for (InputSection *Sec : getInputSections(OS))
1336
1.66k
      Sec->OutSecOff = I++;
1337
1.53k
  }
1338
119
1339
119
  if (!Script->HasSectionsCommand) {
1340
110
    // We know that all the OutputSections are contiguous in this case.
1341
110
    auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
1342
110
    std::stable_sort(
1343
110
        llvm::find_if(Script->SectionCommands, IsSection),
1344
110
        llvm::find_if(llvm::reverse(Script->SectionCommands), IsSection).base(),
1345
110
        compareSections);
1346
110
    return;
1347
110
  }
1348
9
1349
9
  // Orphan sections are sections present in the input files which are
1350
9
  // not explicitly placed into the output file by the linker script.
1351
9
  //
1352
9
  // The sections in the linker script are already in the correct
1353
9
  // order. We have to figuere out where to insert the orphan
1354
9
  // sections.
1355
9
  //
1356
9
  // The order of the sections in the script is arbitrary and may not agree with
1357
9
  // compareSections. This means that we cannot easily define a strict weak
1358
9
  // ordering. To see why, consider a comparison of a section in the script and
1359
9
  // one not in the script. We have a two simple options:
1360
9
  // * Make them equivalent (a is not less than b, and b is not less than a).
1361
9
  //   The problem is then that equivalence has to be transitive and we can
1362
9
  //   have sections a, b and c with only b in a script and a less than c
1363
9
  //   which breaks this property.
1364
9
  // * Use compareSectionsNonScript. Given that the script order doesn't have
1365
9
  //   to match, we can end up with sections a, b, c, d where b and c are in the
1366
9
  //   script and c is compareSectionsNonScript less than b. In which case d
1367
9
  //   can be equivalent to c, a to b and d < a. As a concrete example:
1368
9
  //   .a (rx) # not in script
1369
9
  //   .b (rx) # in script
1370
9
  //   .c (ro) # in script
1371
9
  //   .d (ro) # not in script
1372
9
  //
1373
9
  // The way we define an order then is:
1374
9
  // *  Sort only the orphan sections. They are in the end right now.
1375
9
  // *  Move each orphan section to its preferred position. We try
1376
9
  //    to put each section in the last position where it can share
1377
9
  //    a PT_LOAD.
1378
9
  //
1379
9
  // There is some ambiguity as to where exactly a new entry should be
1380
9
  // inserted, because Commands contains not only output section
1381
9
  // commands but also other types of commands such as symbol assignment
1382
9
  // expressions. There's no correct answer here due to the lack of the
1383
9
  // formal specification of the linker script. We use heuristics to
1384
9
  // determine whether a new output command should be added before or
1385
9
  // after another commands. For the details, look at shouldSkip
1386
9
  // function.
1387
9
1388
9
  auto I = Script->SectionCommands.begin();
1389
9
  auto E = Script->SectionCommands.end();
1390
9
  auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
1391
9
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1392
9
      return Sec->SectionIndex == UINT32_MAX;
1393
9
    return false;
1394
9
  });
1395
9
1396
9
  // Sort the orphan sections.
1397
9
  std::stable_sort(NonScriptI, E, compareSections);
1398
9
1399
9
  // As a horrible special case, skip the first . assignment if it is before any
1400
9
  // section. We do this because it is common to set a load address by starting
1401
9
  // the script with ". = 0xabcd" and the expectation is that every section is
1402
9
  // after that.
1403
9
  auto FirstSectionOrDotAssignment =
1404
9
      std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
1405
9
  if (FirstSectionOrDotAssignment != E &&
1406
9
      isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
1407
0
    ++FirstSectionOrDotAssignment;
1408
9
  I = FirstSectionOrDotAssignment;
1409
9
1410
52
  while (NonScriptI != E) {
1411
43
    auto Pos = findOrphanPos(I, NonScriptI);
1412
43
    OutputSection *Orphan = cast<OutputSection>(*NonScriptI);
1413
43
1414
43
    // As an optimization, find all sections with the same sort rank
1415
43
    // and insert them with one rotate.
1416
43
    unsigned Rank = Orphan->SortRank;
1417
43
    auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
1418
43
      return cast<OutputSection>(Cmd)->SortRank != Rank;
1419
43
    });
1420
43
    std::rotate(Pos, NonScriptI, End);
1421
43
    NonScriptI = End;
1422
43
  }
1423
9
1424
9
  Script->adjustSectionsAfterSorting();
1425
9
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::sortSections()
Line
Count
Source
1315
1.85k
template <class ELFT> void Writer<ELFT>::sortSections() {
1316
1.85k
  Script->adjustSectionsBeforeSorting();
1317
1.85k
1318
1.85k
  // Don't sort if using -r. It is not necessary and we want to preserve the
1319
1.85k
  // relative order for SHF_LINK_ORDER sections.
1320
1.85k
  if (Config->Relocatable)
1321
57
    return;
1322
1.80k
1323
1.80k
  sortInputSections();
1324
1.80k
1325
279k
  for (BaseCommand *Base : Script->SectionCommands) {
1326
279k
    auto *OS = dyn_cast<OutputSection>(Base);
1327
279k
    if (!OS)
1328
364
      continue;
1329
279k
    OS->SortRank = getSectionRank(OS);
1330
279k
1331
279k
    // We want to assign rude approximation values to OutSecOff fields
1332
279k
    // to know the relative order of the input sections. We use it for
1333
279k
    // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
1334
279k
    uint64_t I = 0;
1335
279k
    for (InputSection *Sec : getInputSections(OS))
1336
280k
      Sec->OutSecOff = I++;
1337
279k
  }
1338
1.80k
1339
1.80k
  if (!Script->HasSectionsCommand) {
1340
1.41k
    // We know that all the OutputSections are contiguous in this case.
1341
1.41k
    auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
1342
1.41k
    std::stable_sort(
1343
1.41k
        llvm::find_if(Script->SectionCommands, IsSection),
1344
1.41k
        llvm::find_if(llvm::reverse(Script->SectionCommands), IsSection).base(),
1345
1.41k
        compareSections);
1346
1.41k
    return;
1347
1.41k
  }
1348
388
1349
388
  // Orphan sections are sections present in the input files which are
1350
388
  // not explicitly placed into the output file by the linker script.
1351
388
  //
1352
388
  // The sections in the linker script are already in the correct
1353
388
  // order. We have to figuere out where to insert the orphan
1354
388
  // sections.
1355
388
  //
1356
388
  // The order of the sections in the script is arbitrary and may not agree with
1357
388
  // compareSections. This means that we cannot easily define a strict weak
1358
388
  // ordering. To see why, consider a comparison of a section in the script and
1359
388
  // one not in the script. We have a two simple options:
1360
388
  // * Make them equivalent (a is not less than b, and b is not less than a).
1361
388
  //   The problem is then that equivalence has to be transitive and we can
1362
388
  //   have sections a, b and c with only b in a script and a less than c
1363
388
  //   which breaks this property.
1364
388
  // * Use compareSectionsNonScript. Given that the script order doesn't have
1365
388
  //   to match, we can end up with sections a, b, c, d where b and c are in the
1366
388
  //   script and c is compareSectionsNonScript less than b. In which case d
1367
388
  //   can be equivalent to c, a to b and d < a. As a concrete example:
1368
388
  //   .a (rx) # not in script
1369
388
  //   .b (rx) # in script
1370
388
  //   .c (ro) # in script
1371
388
  //   .d (ro) # not in script
1372
388
  //
1373
388
  // The way we define an order then is:
1374
388
  // *  Sort only the orphan sections. They are in the end right now.
1375
388
  // *  Move each orphan section to its preferred position. We try
1376
388
  //    to put each section in the last position where it can share
1377
388
  //    a PT_LOAD.
1378
388
  //
1379
388
  // There is some ambiguity as to where exactly a new entry should be
1380
388
  // inserted, because Commands contains not only output section
1381
388
  // commands but also other types of commands such as symbol assignment
1382
388
  // expressions. There's no correct answer here due to the lack of the
1383
388
  // formal specification of the linker script. We use heuristics to
1384
388
  // determine whether a new output command should be added before or
1385
388
  // after another commands. For the details, look at shouldSkip
1386
388
  // function.
1387
388
1388
388
  auto I = Script->SectionCommands.begin();
1389
388
  auto E = Script->SectionCommands.end();
1390
388
  auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
1391
388
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1392
388
      return Sec->SectionIndex == UINT32_MAX;
1393
388
    return false;
1394
388
  });
1395
388
1396
388
  // Sort the orphan sections.
1397
388
  std::stable_sort(NonScriptI, E, compareSections);
1398
388
1399
388
  // As a horrible special case, skip the first . assignment if it is before any
1400
388
  // section. We do this because it is common to set a load address by starting
1401
388
  // the script with ". = 0xabcd" and the expectation is that every section is
1402
388
  // after that.
1403
388
  auto FirstSectionOrDotAssignment =
1404
388
      std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
1405
388
  if (FirstSectionOrDotAssignment != E &&
1406
388
      isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
1407
91
    ++FirstSectionOrDotAssignment;
1408
388
  I = FirstSectionOrDotAssignment;
1409
388
1410
1.30k
  while (NonScriptI != E) {
1411
913
    auto Pos = findOrphanPos(I, NonScriptI);
1412
913
    OutputSection *Orphan = cast<OutputSection>(*NonScriptI);
1413
913
1414
913
    // As an optimization, find all sections with the same sort rank
1415
913
    // and insert them with one rotate.
1416
913
    unsigned Rank = Orphan->SortRank;
1417
913
    auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
1418
913
      return cast<OutputSection>(Cmd)->SortRank != Rank;
1419
913
    });
1420
913
    std::rotate(Pos, NonScriptI, End);
1421
913
    NonScriptI = End;
1422
913
  }
1423
388
1424
388
  Script->adjustSectionsAfterSorting();
1425
388
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::sortSections()
Line
Count
Source
1315
108
template <class ELFT> void Writer<ELFT>::sortSections() {
1316
108
  Script->adjustSectionsBeforeSorting();
1317
108
1318
108
  // Don't sort if using -r. It is not necessary and we want to preserve the
1319
108
  // relative order for SHF_LINK_ORDER sections.
1320
108
  if (Config->Relocatable)
1321
3
    return;
1322
105
1323
105
  sortInputSections();
1324
105
1325
1.22k
  for (BaseCommand *Base : Script->SectionCommands) {
1326
1.22k
    auto *OS = dyn_cast<OutputSection>(Base);
1327
1.22k
    if (!OS)
1328
23
      continue;
1329
1.20k
    OS->SortRank = getSectionRank(OS);
1330
1.20k
1331
1.20k
    // We want to assign rude approximation values to OutSecOff fields
1332
1.20k
    // to know the relative order of the input sections. We use it for
1333
1.20k
    // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
1334
1.20k
    uint64_t I = 0;
1335
1.20k
    for (InputSection *Sec : getInputSections(OS))
1336
1.25k
      Sec->OutSecOff = I++;
1337
1.20k
  }
1338
105
1339
105
  if (!Script->HasSectionsCommand) {
1340
101
    // We know that all the OutputSections are contiguous in this case.
1341
101
    auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
1342
101
    std::stable_sort(
1343
101
        llvm::find_if(Script->SectionCommands, IsSection),
1344
101
        llvm::find_if(llvm::reverse(Script->SectionCommands), IsSection).base(),
1345
101
        compareSections);
1346
101
    return;
1347
101
  }
1348
4
1349
4
  // Orphan sections are sections present in the input files which are
1350
4
  // not explicitly placed into the output file by the linker script.
1351
4
  //
1352
4
  // The sections in the linker script are already in the correct
1353
4
  // order. We have to figuere out where to insert the orphan
1354
4
  // sections.
1355
4
  //
1356
4
  // The order of the sections in the script is arbitrary and may not agree with
1357
4
  // compareSections. This means that we cannot easily define a strict weak
1358
4
  // ordering. To see why, consider a comparison of a section in the script and
1359
4
  // one not in the script. We have a two simple options:
1360
4
  // * Make them equivalent (a is not less than b, and b is not less than a).
1361
4
  //   The problem is then that equivalence has to be transitive and we can
1362
4
  //   have sections a, b and c with only b in a script and a less than c
1363
4
  //   which breaks this property.
1364
4
  // * Use compareSectionsNonScript. Given that the script order doesn't have
1365
4
  //   to match, we can end up with sections a, b, c, d where b and c are in the
1366
4
  //   script and c is compareSectionsNonScript less than b. In which case d
1367
4
  //   can be equivalent to c, a to b and d < a. As a concrete example:
1368
4
  //   .a (rx) # not in script
1369
4
  //   .b (rx) # in script
1370
4
  //   .c (ro) # in script
1371
4
  //   .d (ro) # not in script
1372
4
  //
1373
4
  // The way we define an order then is:
1374
4
  // *  Sort only the orphan sections. They are in the end right now.
1375
4
  // *  Move each orphan section to its preferred position. We try
1376
4
  //    to put each section in the last position where it can share
1377
4
  //    a PT_LOAD.
1378
4
  //
1379
4
  // There is some ambiguity as to where exactly a new entry should be
1380
4
  // inserted, because Commands contains not only output section
1381
4
  // commands but also other types of commands such as symbol assignment
1382
4
  // expressions. There's no correct answer here due to the lack of the
1383
4
  // formal specification of the linker script. We use heuristics to
1384
4
  // determine whether a new output command should be added before or
1385
4
  // after another commands. For the details, look at shouldSkip
1386
4
  // function.
1387
4
1388
4
  auto I = Script->SectionCommands.begin();
1389
4
  auto E = Script->SectionCommands.end();
1390
4
  auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
1391
4
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1392
4
      return Sec->SectionIndex == UINT32_MAX;
1393
4
    return false;
1394
4
  });
1395
4
1396
4
  // Sort the orphan sections.
1397
4
  std::stable_sort(NonScriptI, E, compareSections);
1398
4
1399
4
  // As a horrible special case, skip the first . assignment if it is before any
1400
4
  // section. We do this because it is common to set a load address by starting
1401
4
  // the script with ". = 0xabcd" and the expectation is that every section is
1402
4
  // after that.
1403
4
  auto FirstSectionOrDotAssignment =
1404
4
      std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
1405
4
  if (FirstSectionOrDotAssignment != E &&
1406
4
      isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
1407
1
    ++FirstSectionOrDotAssignment;
1408
4
  I = FirstSectionOrDotAssignment;
1409
4
1410
24
  while (NonScriptI != E) {
1411
20
    auto Pos = findOrphanPos(I, NonScriptI);
1412
20
    OutputSection *Orphan = cast<OutputSection>(*NonScriptI);
1413
20
1414
20
    // As an optimization, find all sections with the same sort rank
1415
20
    // and insert them with one rotate.
1416
20
    unsigned Rank = Orphan->SortRank;
1417
20
    auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
1418
20
      return cast<OutputSection>(Cmd)->SortRank != Rank;
1419
20
    });
1420
20
    std::rotate(Pos, NonScriptI, End);
1421
20
    NonScriptI = End;
1422
20
  }
1423
4
1424
4
  Script->adjustSectionsAfterSorting();
1425
4
}
1426
1427
6
static bool compareByFilePosition(InputSection *A, InputSection *B) {
1428
6
  InputSection *LA = A->getLinkOrderDep();
1429
6
  InputSection *LB = B->getLinkOrderDep();
1430
6
  OutputSection *AOut = LA->getParent();
1431
6
  OutputSection *BOut = LB->getParent();
1432
6
1433
6
  if (AOut != BOut)
1434
0
    return AOut->SectionIndex < BOut->SectionIndex;
1435
6
  return LA->OutSecOff < LB->OutSecOff;
1436
6
}
1437
1438
2.41k
template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1439
351k
  for (OutputSection *Sec : OutputSections) {
1440
351k
    if (!(Sec->Flags & SHF_LINK_ORDER))
1441
351k
      continue;
1442
41
1443
41
    // Link order may be distributed across several InputSectionDescriptions
1444
41
    // but sort must consider them all at once.
1445
41
    std::vector<InputSection **> ScriptSections;
1446
41
    std::vector<InputSection *> Sections;
1447
42
    for (BaseCommand *Base : Sec->SectionCommands) {
1448
42
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
1449
47
        for (InputSection *&IS : ISD->Sections) {
1450
47
          ScriptSections.push_back(&IS);
1451
47
          Sections.push_back(IS);
1452
47
        }
1453
41
      }
1454
42
    }
1455
41
1456
41
    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1457
41
    // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1458
41
    if (!Config->Relocatable && 
Config->EMachine == EM_ARM33
&&
1459
41
        
Sec->Type == SHT_ARM_EXIDX24
)
1460
24
      continue;
1461
17
1462
17
    llvm::stable_sort(Sections, compareByFilePosition);
1463
17
1464
40
    for (int I = 0, N = Sections.size(); I < N; 
++I23
)
1465
23
      *ScriptSections[I] = Sections[I];
1466
17
  }
1467
2.41k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::resolveShfLinkOrder()
Line
Count
Source
1438
313
template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1439
2.99k
  for (OutputSection *Sec : OutputSections) {
1440
2.99k
    if (!(Sec->Flags & SHF_LINK_ORDER))
1441
2.96k
      continue;
1442
32
1443
32
    // Link order may be distributed across several InputSectionDescriptions
1444
32
    // but sort must consider them all at once.
1445
32
    std::vector<InputSection **> ScriptSections;
1446
32
    std::vector<InputSection *> Sections;
1447
33
    for (BaseCommand *Base : Sec->SectionCommands) {
1448
33
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
1449
33
        for (InputSection *&IS : ISD->Sections) {
1450
33
          ScriptSections.push_back(&IS);
1451
33
          Sections.push_back(IS);
1452
33
        }
1453
32
      }
1454
33
    }
1455
32
1456
32
    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1457
32
    // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1458
32
    if (!Config->Relocatable && 
Config->EMachine == EM_ARM24
&&
1459
32
        
Sec->Type == SHT_ARM_EXIDX24
)
1460
24
      continue;
1461
8
1462
8
    llvm::stable_sort(Sections, compareByFilePosition);
1463
8
1464
17
    for (int I = 0, N = Sections.size(); I < N; 
++I9
)
1465
9
      *ScriptSections[I] = Sections[I];
1466
8
  }
1467
313
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::resolveShfLinkOrder()
Line
Count
Source
1438
130
template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1439
1.64k
  for (OutputSection *Sec : OutputSections) {
1440
1.64k
    if (!(Sec->Flags & SHF_LINK_ORDER))
1441
1.64k
      continue;
1442
0
1443
0
    // Link order may be distributed across several InputSectionDescriptions
1444
0
    // but sort must consider them all at once.
1445
0
    std::vector<InputSection **> ScriptSections;
1446
0
    std::vector<InputSection *> Sections;
1447
0
    for (BaseCommand *Base : Sec->SectionCommands) {
1448
0
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
1449
0
        for (InputSection *&IS : ISD->Sections) {
1450
0
          ScriptSections.push_back(&IS);
1451
0
          Sections.push_back(IS);
1452
0
        }
1453
0
      }
1454
0
    }
1455
0
1456
0
    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1457
0
    // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1458
0
    if (!Config->Relocatable && Config->EMachine == EM_ARM &&
1459
0
        Sec->Type == SHT_ARM_EXIDX)
1460
0
      continue;
1461
0
1462
0
    llvm::stable_sort(Sections, compareByFilePosition);
1463
0
1464
0
    for (int I = 0, N = Sections.size(); I < N; ++I)
1465
0
      *ScriptSections[I] = Sections[I];
1466
0
  }
1467
130
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::resolveShfLinkOrder()
Line
Count
Source
1438
1.85k
template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1439
345k
  for (OutputSection *Sec : OutputSections) {
1440
345k
    if (!(Sec->Flags & SHF_LINK_ORDER))
1441
345k
      continue;
1442
9
1443
9
    // Link order may be distributed across several InputSectionDescriptions
1444
9
    // but sort must consider them all at once.
1445
9
    std::vector<InputSection **> ScriptSections;
1446
9
    std::vector<InputSection *> Sections;
1447
9
    for (BaseCommand *Base : Sec->SectionCommands) {
1448
9
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
1449
14
        for (InputSection *&IS : ISD->Sections) {
1450
14
          ScriptSections.push_back(&IS);
1451
14
          Sections.push_back(IS);
1452
14
        }
1453
9
      }
1454
9
    }
1455
9
1456
9
    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1457
9
    // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1458
9
    if (!Config->Relocatable && Config->EMachine == EM_ARM &&
1459
9
        
Sec->Type == SHT_ARM_EXIDX0
)
1460
0
      continue;
1461
9
1462
9
    llvm::stable_sort(Sections, compareByFilePosition);
1463
9
1464
23
    for (int I = 0, N = Sections.size(); I < N; 
++I14
)
1465
14
      *ScriptSections[I] = Sections[I];
1466
9
  }
1467
1.85k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::resolveShfLinkOrder()
Line
Count
Source
1438
108
template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1439
1.23k
  for (OutputSection *Sec : OutputSections) {
1440
1.23k
    if (!(Sec->Flags & SHF_LINK_ORDER))
1441
1.23k
      continue;
1442
0
1443
0
    // Link order may be distributed across several InputSectionDescriptions
1444
0
    // but sort must consider them all at once.
1445
0
    std::vector<InputSection **> ScriptSections;
1446
0
    std::vector<InputSection *> Sections;
1447
0
    for (BaseCommand *Base : Sec->SectionCommands) {
1448
0
      if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) {
1449
0
        for (InputSection *&IS : ISD->Sections) {
1450
0
          ScriptSections.push_back(&IS);
1451
0
          Sections.push_back(IS);
1452
0
        }
1453
0
      }
1454
0
    }
1455
0
1456
0
    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1457
0
    // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1458
0
    if (!Config->Relocatable && Config->EMachine == EM_ARM &&
1459
0
        Sec->Type == SHT_ARM_EXIDX)
1460
0
      continue;
1461
0
1462
0
    llvm::stable_sort(Sections, compareByFilePosition);
1463
0
1464
0
    for (int I = 0, N = Sections.size(); I < N; ++I)
1465
0
      *ScriptSections[I] = Sections[I];
1466
0
  }
1467
108
}
1468
1469
// We need to generate and finalize the content that depends on the address of
1470
// InputSections. As the generation of the content may also alter InputSection
1471
// addresses we must converge to a fixed point. We do that here. See the comment
1472
// in Writer<ELFT>::finalizeSections().
1473
2.41k
template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1474
2.41k
  ThunkCreator TC;
1475
2.41k
  AArch64Err843419Patcher A64P;
1476
2.41k
1477
2.41k
  // For some targets, like x86, this loop iterates only once.
1478
2.52k
  for (;;) {
1479
2.52k
    bool Changed = false;
1480
2.52k
1481
2.52k
    Script->assignAddresses();
1482
2.52k
1483
2.52k
    if (Target->NeedsThunks)
1484
700
      Changed |= TC.createThunks(OutputSections);
1485
2.52k
1486
2.52k
    if (Config->FixCortexA53Errata843419) {
1487
16
      if (Changed)
1488
2
        Script->assignAddresses();
1489
16
      Changed |= A64P.createFixes();
1490
16
    }
1491
2.52k
1492
2.52k
    if (In.MipsGot)
1493
202
      In.MipsGot->updateAllocSize();
1494
2.52k
1495
2.52k
    Changed |= In.RelaDyn->updateAllocSize();
1496
2.52k
1497
2.52k
    if (In.RelrDyn)
1498
7
      Changed |= In.RelrDyn->updateAllocSize();
1499
2.52k
1500
2.52k
    if (!Changed)
1501
2.41k
      return;
1502
2.52k
  }
1503
2.41k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeAddressDependentContent()
Line
Count
Source
1473
313
template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1474
313
  ThunkCreator TC;
1475
313
  AArch64Err843419Patcher A64P;
1476
313
1477
313
  // For some targets, like x86, this loop iterates only once.
1478
362
  for (;;) {
1479
362
    bool Changed = false;
1480
362
1481
362
    Script->assignAddresses();
1482
362
1483
362
    if (Target->NeedsThunks)
1484
224
      Changed |= TC.createThunks(OutputSections);
1485
362
1486
362
    if (Config->FixCortexA53Errata843419) {
1487
0
      if (Changed)
1488
0
        Script->assignAddresses();
1489
0
      Changed |= A64P.createFixes();
1490
0
    }
1491
362
1492
362
    if (In.MipsGot)
1493
25
      In.MipsGot->updateAllocSize();
1494
362
1495
362
    Changed |= In.RelaDyn->updateAllocSize();
1496
362
1497
362
    if (In.RelrDyn)
1498
4
      Changed |= In.RelrDyn->updateAllocSize();
1499
362
1500
362
    if (!Changed)
1501
313
      return;
1502
362
  }
1503
313
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeAddressDependentContent()
Line
Count
Source
1473
130
template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1474
130
  ThunkCreator TC;
1475
130
  AArch64Err843419Patcher A64P;
1476
130
1477
130
  // For some targets, like x86, this loop iterates only once.
1478
143
  for (;;) {
1479
143
    bool Changed = false;
1480
143
1481
143
    Script->assignAddresses();
1482
143
1483
143
    if (Target->NeedsThunks)
1484
136
      Changed |= TC.createThunks(OutputSections);
1485
143
1486
143
    if (Config->FixCortexA53Errata843419) {
1487
0
      if (Changed)
1488
0
        Script->assignAddresses();
1489
0
      Changed |= A64P.createFixes();
1490
0
    }
1491
143
1492
143
    if (In.MipsGot)
1493
136
      In.MipsGot->updateAllocSize();
1494
143
1495
143
    Changed |= In.RelaDyn->updateAllocSize();
1496
143
1497
143
    if (In.RelrDyn)
1498
0
      Changed |= In.RelrDyn->updateAllocSize();
1499
143
1500
143
    if (!Changed)
1501
130
      return;
1502
143
  }
1503
130
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeAddressDependentContent()
Line
Count
Source
1473
1.85k
template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1474
1.85k
  ThunkCreator TC;
1475
1.85k
  AArch64Err843419Patcher A64P;
1476
1.85k
1477
1.85k
  // For some targets, like x86, this loop iterates only once.
1478
1.89k
  for (;;) {
1479
1.89k
    bool Changed = false;
1480
1.89k
1481
1.89k
    Script->assignAddresses();
1482
1.89k
1483
1.89k
    if (Target->NeedsThunks)
1484
218
      Changed |= TC.createThunks(OutputSections);
1485
1.89k
1486
1.89k
    if (Config->FixCortexA53Errata843419) {
1487
16
      if (Changed)
1488
2
        Script->assignAddresses();
1489
16
      Changed |= A64P.createFixes();
1490
16
    }
1491
1.89k
1492
1.89k
    if (In.MipsGot)
1493
3
      In.MipsGot->updateAllocSize();
1494
1.89k
1495
1.89k
    Changed |= In.RelaDyn->updateAllocSize();
1496
1.89k
1497
1.89k
    if (In.RelrDyn)
1498
3
      Changed |= In.RelrDyn->updateAllocSize();
1499
1.89k
1500
1.89k
    if (!Changed)
1501
1.85k
      return;
1502
1.89k
  }
1503
1.85k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeAddressDependentContent()
Line
Count
Source
1473
108
template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1474
108
  ThunkCreator TC;
1475
108
  AArch64Err843419Patcher A64P;
1476
108
1477
108
  // For some targets, like x86, this loop iterates only once.
1478
123
  for (;;) {
1479
123
    bool Changed = false;
1480
123
1481
123
    Script->assignAddresses();
1482
123
1483
123
    if (Target->NeedsThunks)
1484
122
      Changed |= TC.createThunks(OutputSections);
1485
123
1486
123
    if (Config->FixCortexA53Errata843419) {
1487
0
      if (Changed)
1488
0
        Script->assignAddresses();
1489
0
      Changed |= A64P.createFixes();
1490
0
    }
1491
123
1492
123
    if (In.MipsGot)
1493
38
      In.MipsGot->updateAllocSize();
1494
123
1495
123
    Changed |= In.RelaDyn->updateAllocSize();
1496
123
1497
123
    if (In.RelrDyn)
1498
0
      Changed |= In.RelrDyn->updateAllocSize();
1499
123
1500
123
    if (!Changed)
1501
108
      return;
1502
123
  }
1503
108
}
1504
1505
65.1k
static void finalizeSynthetic(SyntheticSection *Sec) {
1506
65.1k
  if (Sec && 
Sec->isNeeded()44.5k
&&
Sec->getParent()14.8k
)
1507
13.5k
    Sec->finalizeContents();
1508
65.1k
}
1509
1510
// In order to allow users to manipulate linker-synthesized sections,
1511
// we had to add synthetic sections to the input section list early,
1512
// even before we make decisions whether they are needed. This allows
1513
// users to write scripts like this: ".mygot : { .got }".
1514
//
1515
// Doing it has an unintended side effects. If it turns out that we
1516
// don't need a .got (for example) at all because there's no
1517
// relocation that needs a .got, we don't want to emit .got.
1518
//
1519
// To deal with the above problem, this function is called after
1520
// scanRelocations is called to remove synthetic sections that turn
1521
// out to be empty.
1522
2.41k
static void removeUnusedSyntheticSections() {
1523
2.41k
  // All input synthetic sections that can be empty are placed after
1524
2.41k
  // all regular ones. We iterate over them all and exit at first
1525
2.41k
  // non-synthetic.
1526
48.4k
  for (InputSectionBase *S : llvm::reverse(InputSections)) {
1527
48.4k
    SyntheticSection *SS = dyn_cast<SyntheticSection>(S);
1528
48.4k
    if (!SS)
1529
2.40k
      return;
1530
46.0k
    OutputSection *OS = SS->getParent();
1531
46.0k
    if (!OS || 
SS->isNeeded()46.0k
)
1532
17.7k
      continue;
1533
28.3k
1534
28.3k
    // If we reach here, then SS is an unused synthetic section and we want to
1535
28.3k
    // remove it from corresponding input section description of output section.
1536
28.3k
    for (BaseCommand *B : OS->SectionCommands)
1537
28.4k
      if (auto *ISD = dyn_cast<InputSectionDescription>(B))
1538
28.3k
        llvm::erase_if(ISD->Sections,
1539
35.9k
                       [=](InputSection *IS) { return IS == SS; });
1540
28.3k
  }
1541
2.41k
}
1542
1543
// Returns true if a symbol can be replaced at load-time by a symbol
1544
// with the same name defined in other ELF executable or DSO.
1545
17.8k
static bool computeIsPreemptible(const Symbol &B) {
1546
17.8k
  assert(!B.isLocal());
1547
17.8k
1548
17.8k
  // Only symbols that appear in dynsym can be preempted.
1549
17.8k
  if (!B.includeInDynsym())
1550
15.3k
    return false;
1551
2.46k
1552
2.46k
  // Only default visibility symbols can be preempted.
1553
2.46k
  if (B.Visibility != STV_DEFAULT)
1554
34
    return false;
1555
2.43k
1556
2.43k
  // At this point copy relocations have not been created yet, so any
1557
2.43k
  // symbol that is not defined locally is preemptible.
1558
2.43k
  if (!B.isDefined())
1559
1.03k
    return true;
1560
1.40k
1561
1.40k
  // If we have a dynamic list it specifies which local symbols are preemptible.
1562
1.40k
  if (Config->HasDynamicList)
1563
17
    return false;
1564
1.38k
1565
1.38k
  if (!Config->Shared)
1566
104
    return false;
1567
1.28k
1568
1.28k
  // -Bsymbolic means that definitions are not preempted.
1569
1.28k
  if (Config->Bsymbolic || 
(1.26k
Config->BsymbolicFunctions1.26k
&&
B.isFunc()3
))
1570
16
    return false;
1571
1.26k
  return true;
1572
1.26k
}
1573
1574
// Create output section objects and add them to OutputSections.
1575
2.52k
template <class ELFT> void Writer<ELFT>::finalizeSections() {
1576
2.52k
  Out::PreinitArray = findSection(".preinit_array");
1577
2.52k
  Out::InitArray = findSection(".init_array");
1578
2.52k
  Out::FiniArray = findSection(".fini_array");
1579
2.52k
1580
2.52k
  // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1581
2.52k
  // symbols for sections, so that the runtime can get the start and end
1582
2.52k
  // addresses of each section by section name. Add such symbols.
1583
2.52k
  if (!Config->Relocatable) {
1584
2.44k
    addStartEndSymbols();
1585
2.44k
    for (BaseCommand *Base : Script->SectionCommands)
1586
307k
      if (auto *Sec = dyn_cast<OutputSection>(Base))
1587
307k
        addStartStopSymbols(Sec);
1588
2.44k
  }
1589
2.52k
1590
2.52k
  // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1591
2.52k
  // It should be okay as no one seems to care about the type.
1592
2.52k
  // Even the author of gold doesn't remember why gold behaves that way.
1593
2.52k
  // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1594
2.52k
  if (In.Dynamic->Parent)
1595
1.23k
    Symtab->addSymbol(Defined{/*File=*/nullptr, "_DYNAMIC", STB_WEAK,
1596
1.23k
                              STV_HIDDEN, STT_NOTYPE,
1597
1.23k
                              /*Value=*/0, /*Size=*/0, In.Dynamic});
1598
2.52k
1599
2.52k
  // Define __rel[a]_iplt_{start,end} symbols if needed.
1600
2.52k
  addRelIpltSymbols();
1601
2.52k
1602
2.52k
  // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800 if not defined.
1603
2.52k
  if (Config->EMachine == EM_RISCV)
1604
0
    if (!dyn_cast_or_null<Defined>(Symtab->find("__global_pointer$")))
1605
0
      addOptionalRegular("__global_pointer$", findSection(".sdata"), 0x800);
1606
2.52k
1607
2.52k
  // This responsible for splitting up .eh_frame section into
1608
2.52k
  // pieces. The relocation scan uses those pieces, so this has to be
1609
2.52k
  // earlier.
1610
2.52k
  finalizeSynthetic(In.EhFrame);
1611
2.52k
1612
2.52k
  for (Symbol *S : Symtab->getSymbols())
1613
17.8k
    if (!S->IsPreemptible)
1614
17.8k
      S->IsPreemptible = computeIsPreemptible(*S);
1615
2.52k
1616
2.52k
  // Scan relocations. This must be done after every symbol is declared so that
1617
2.52k
  // we can correctly decide if a dynamic relocation is needed.
1618
2.52k
  if (!Config->Relocatable)
1619
2.44k
    forEachRelSec(scanRelocations<ELFT>);
1620
2.52k
1621
2.52k
  addIRelativeRelocs();
1622
2.52k
1623
2.52k
  if (In.Plt && In.Plt->isNeeded())
1624
237
    In.Plt->addSymbols();
1625
2.52k
  if (In.Iplt && In.Iplt->isNeeded())
1626
42
    In.Iplt->addSymbols();
1627
2.52k
1628
2.52k
  if (!Config->AllowShlibUndefined) {
1629
1.64k
    // Error on undefined symbols in a shared object, if all of its DT_NEEDED
1630
1.64k
    // entires are seen. These cases would otherwise lead to runtime errors
1631
1.64k
    // reported by the dynamic linker.
1632
1.64k
    //
1633
1.64k
    // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker to
1634
1.64k
    // catch more cases. That is too much for us. Our approach resembles the one
1635
1.64k
    // used in ld.gold, achieves a good balance to be useful but not too smart.
1636
1.64k
    for (SharedFile *File : SharedFiles)
1637
306
      File->AllNeededIsKnown =
1638
306
          llvm::all_of(File->DtNeeded, [&](StringRef Needed) {
1639
4
            return Symtab->SoNames.count(Needed);
1640
4
          });
Unexecuted instantiation: Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeSections()::'lambda'(llvm::StringRef)::operator()(llvm::StringRef) const
Unexecuted instantiation: Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeSections()::'lambda'(llvm::StringRef)::operator()(llvm::StringRef) const
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeSections()::'lambda'(llvm::StringRef)::operator()(llvm::StringRef) const
Line
Count
Source
1638
4
          llvm::all_of(File->DtNeeded, [&](StringRef Needed) {
1639
4
            return Symtab->SoNames.count(Needed);
1640
4
          });
Unexecuted instantiation: Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeSections()::'lambda'(llvm::StringRef)::operator()(llvm::StringRef) const
1641
1.64k
    for (Symbol *Sym : Symtab->getSymbols())
1642
14.9k
      if (Sym->isUndefined() && 
!Sym->isWeak()274
)
1643
210
        if (auto *F = dyn_cast_or_null<SharedFile>(Sym->File))
1644
3
          if (F->AllNeededIsKnown)
1645
2
            error(toString(F) + ": undefined reference to " + toString(*Sym));
1646
1.64k
  }
1647
2.52k
1648
2.52k
  // Now that we have defined all possible global symbols including linker-
1649
2.52k
  // synthesized ones. Visit all symbols to give the finishing touches.
1650
17.8k
  for (Symbol *Sym : Symtab->getSymbols()) {
1651
17.8k
    if (!includeInSymtab(*Sym))
1652
491
      continue;
1653
17.3k
    if (In.SymTab)
1654
17.3k
      In.SymTab->addSymbol(Sym);
1655
17.3k
1656
17.3k
    if (Sym->includeInDynsym()) {
1657
2.09k
      In.DynSymTab->addSymbol(Sym);
1658
2.09k
      if (auto *File = dyn_cast_or_null<SharedFile>(Sym->File))
1659
396
        if (File->IsNeeded && !Sym->isUndefined())
1660
395
          addVerneed(Sym);
1661
2.09k
    }
1662
17.3k
  }
1663
2.52k
1664
2.52k
  // Do not proceed if there was an undefined symbol.
1665
2.52k
  if (errorCount())
1666
117
    return;
1667
2.41k
1668
2.41k
  if (In.MipsGot)
1669
187
    In.MipsGot->build();
1670
2.41k
1671
2.41k
  removeUnusedSyntheticSections();
1672
2.41k
1673
2.41k
  sortSections();
1674
2.41k
1675
2.41k
  // Now that we have the final list, create a list of all the
1676
2.41k
  // OutputSections for convenience.
1677
2.41k
  for (BaseCommand *Base : Script->SectionCommands)
1678
351k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1679
351k
      OutputSections.push_back(Sec);
1680
2.41k
1681
2.41k
  // Prefer command line supplied address over other constraints.
1682
351k
  for (OutputSection *Sec : OutputSections) {
1683
351k
    auto I = Config->SectionStartMap.find(Sec->Name);
1684
351k
    if (I != Config->SectionStartMap.end())
1685
119
      
Sec->AddrExpr = [=] 59
{ return I->second; };
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeSections()::'lambda'()::operator()() const
Line
Count
Source
1685
24
      Sec->AddrExpr = [=] { return I->second; };
Unexecuted instantiation: Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeSections()::'lambda'()::operator()() const
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeSections()::'lambda'()::operator()() const
Line
Count
Source
1685
93
      Sec->AddrExpr = [=] { return I->second; };
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeSections()::'lambda'()::operator()() const
Line
Count
Source
1685
2
      Sec->AddrExpr = [=] { return I->second; };
1686
351k
  }
1687
2.41k
1688
2.41k
  // This is a bit of a hack. A value of 0 means undef, so we set it
1689
2.41k
  // to 1 to make __ehdr_start defined. The section number is not
1690
2.41k
  // particularly relevant.
1691
2.41k
  Out::ElfHeader->SectionIndex = 1;
1692
2.41k
1693
353k
  for (size_t I = 0, E = OutputSections.size(); I != E; 
++I351k
) {
1694
351k
    OutputSection *Sec = OutputSections[I];
1695
351k
    Sec->SectionIndex = I + 1;
1696
351k
    Sec->ShName = In.ShStrTab->addString(Sec->Name);
1697
351k
  }
1698
2.41k
1699
2.41k
  // Binary and relocatable output does not have PHDRS.
1700
2.41k
  // The headers have to be created before finalize as that can influence the
1701
2.41k
  // image base and the dynamic section on mips includes the image base.
1702
2.41k
  if (!Config->Relocatable && 
!Config->OFormatBinary2.32k
) {
1703
2.32k
    Phdrs = Script->hasPhdrsCommands() ? 
Script->createPhdrs()34
:
createPhdrs()2.28k
;
1704
2.32k
    if (Config->EMachine == EM_ARM) {
1705
147
      // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
1706
147
      addPhdrForSection(Phdrs, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);
1707
147
    }
1708
2.32k
    if (Config->EMachine == EM_MIPS) {
1709
173
      // Add separate segments for MIPS-specific sections.
1710
173
      addPhdrForSection(Phdrs, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);
1711
173
      addPhdrForSection(Phdrs, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);
1712
173
      addPhdrForSection(Phdrs, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);
1713
173
    }
1714
2.32k
    Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
1715
2.32k
1716
2.32k
    // Find the TLS segment. This happens before the section layout loop so that
1717
2.32k
    // Android relocation packing can look up TLS symbol addresses.
1718
2.32k
    for (PhdrEntry *P : Phdrs)
1719
13.7k
      if (P->p_type == PT_TLS)
1720
117
        Out::TlsPhdr = P;
1721
2.32k
  }
1722
2.41k
1723
2.41k
  // Some symbols are defined in term of program headers. Now that we
1724
2.41k
  // have the headers, we can find out which sections they point to.
1725
2.41k
  setReservedSymbolSections();
1726
2.41k
1727
2.41k
  // Dynamic section must be the last one in this list and dynamic
1728
2.41k
  // symbol table section (DynSymTab) must be the first one.
1729
2.41k
  finalizeSynthetic(In.DynSymTab);
1730
2.41k
  finalizeSynthetic(In.ARMExidx);
1731
2.41k
  finalizeSynthetic(In.Bss);
1732
2.41k
  finalizeSynthetic(In.BssRelRo);
1733
2.41k
  finalizeSynthetic(In.GnuHashTab);
1734
2.41k
  finalizeSynthetic(In.HashTab);
1735
2.41k
  finalizeSynthetic(In.SymTabShndx);
1736
2.41k
  finalizeSynthetic(In.ShStrTab);
1737
2.41k
  finalizeSynthetic(In.StrTab);
1738
2.41k
  finalizeSynthetic(In.VerDef);
1739
2.41k
  finalizeSynthetic(In.Got);
1740
2.41k
  finalizeSynthetic(In.MipsGot);
1741
2.41k
  finalizeSynthetic(In.IgotPlt);
1742
2.41k
  finalizeSynthetic(In.GotPlt);
1743
2.41k
  finalizeSynthetic(In.RelaDyn);
1744
2.41k
  finalizeSynthetic(In.RelrDyn);
1745
2.41k
  finalizeSynthetic(In.RelaIplt);
1746
2.41k
  finalizeSynthetic(In.RelaPlt);
1747
2.41k
  finalizeSynthetic(In.Plt);
1748
2.41k
  finalizeSynthetic(In.Iplt);
1749
2.41k
  finalizeSynthetic(In.EhFrameHdr);
1750
2.41k
  finalizeSynthetic(In.VerSym);
1751
2.41k
  finalizeSynthetic(In.VerNeed);
1752
2.41k
  finalizeSynthetic(In.Dynamic);
1753
2.41k
1754
2.41k
  if (!Script->HasSectionsCommand && 
!Config->Relocatable1.96k
)
1755
1.88k
    fixSectionAlignments();
1756
2.41k
1757
2.41k
  // SHFLinkOrder processing must be processed after relative section placements are
1758
2.41k
  // known but before addresses are allocated.
1759
2.41k
  resolveShfLinkOrder();
1760
2.41k
1761
2.41k
  // This is used to:
1762
2.41k
  // 1) Create "thunks":
1763
2.41k
  //    Jump instructions in many ISAs have small displacements, and therefore
1764
2.41k
  //    they cannot jump to arbitrary addresses in memory. For example, RISC-V
1765
2.41k
  //    JAL instruction can target only +-1 MiB from PC. It is a linker's
1766
2.41k
  //    responsibility to create and insert small pieces of code between
1767
2.41k
  //    sections to extend the ranges if jump targets are out of range. Such
1768
2.41k
  //    code pieces are called "thunks".
1769
2.41k
  //
1770
2.41k
  //    We add thunks at this stage. We couldn't do this before this point
1771
2.41k
  //    because this is the earliest point where we know sizes of sections and
1772
2.41k
  //    their layouts (that are needed to determine if jump targets are in
1773
2.41k
  //    range).
1774
2.41k
  //
1775
2.41k
  // 2) Update the sections. We need to generate content that depends on the
1776
2.41k
  //    address of InputSections. For example, MIPS GOT section content or
1777
2.41k
  //    android packed relocations sections content.
1778
2.41k
  //
1779
2.41k
  // 3) Assign the final values for the linker script symbols. Linker scripts
1780
2.41k
  //    sometimes using forward symbol declarations. We want to set the correct
1781
2.41k
  //    values. They also might change after adding the thunks.
1782
2.41k
  finalizeAddressDependentContent();
1783
2.41k
1784
2.41k
  // finalizeAddressDependentContent may have added local symbols to the static symbol table.
1785
2.41k
  finalizeSynthetic(In.SymTab);
1786
2.41k
  finalizeSynthetic(In.PPC64LongBranchTarget);
1787
2.41k
1788
2.41k
  // Fill other section headers. The dynamic table is finalized
1789
2.41k
  // at the end because some tags like RELSZ depend on result
1790
2.41k
  // of finalizing other sections.
1791
2.41k
  for (OutputSection *Sec : OutputSections)
1792
351k
    Sec->finalize();
1793
2.41k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeSections()
Line
Count
Source
1575
319
template <class ELFT> void Writer<ELFT>::finalizeSections() {
1576
319
  Out::PreinitArray = findSection(".preinit_array");
1577
319
  Out::InitArray = findSection(".init_array");
1578
319
  Out::FiniArray = findSection(".fini_array");
1579
319
1580
319
  // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1581
319
  // symbols for sections, so that the runtime can get the start and end
1582
319
  // addresses of each section by section name. Add such symbols.
1583
319
  if (!Config->Relocatable) {
1584
308
    addStartEndSymbols();
1585
308
    for (BaseCommand *Base : Script->SectionCommands)
1586
5.83k
      if (auto *Sec = dyn_cast<OutputSection>(Base))
1587
5.81k
        addStartStopSymbols(Sec);
1588
308
  }
1589
319
1590
319
  // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1591
319
  // It should be okay as no one seems to care about the type.
1592
319
  // Even the author of gold doesn't remember why gold behaves that way.
1593
319
  // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1594
319
  if (In.Dynamic->Parent)
1595
162
    Symtab->addSymbol(Defined{/*File=*/nullptr, "_DYNAMIC", STB_WEAK,
1596
162
                              STV_HIDDEN, STT_NOTYPE,
1597
162
                              /*Value=*/0, /*Size=*/0, In.Dynamic});
1598
319
1599
319
  // Define __rel[a]_iplt_{start,end} symbols if needed.
1600
319
  addRelIpltSymbols();
1601
319
1602
319
  // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800 if not defined.
1603
319
  if (Config->EMachine == EM_RISCV)
1604
0
    if (!dyn_cast_or_null<Defined>(Symtab->find("__global_pointer$")))
1605
0
      addOptionalRegular("__global_pointer$", findSection(".sdata"), 0x800);
1606
319
1607
319
  // This responsible for splitting up .eh_frame section into
1608
319
  // pieces. The relocation scan uses those pieces, so this has to be
1609
319
  // earlier.
1610
319
  finalizeSynthetic(In.EhFrame);
1611
319
1612
319
  for (Symbol *S : Symtab->getSymbols())
1613
1.26k
    if (!S->IsPreemptible)
1614
1.26k
      S->IsPreemptible = computeIsPreemptible(*S);
1615
319
1616
319
  // Scan relocations. This must be done after every symbol is declared so that
1617
319
  // we can correctly decide if a dynamic relocation is needed.
1618
319
  if (!Config->Relocatable)
1619
308
    forEachRelSec(scanRelocations<ELFT>);
1620
319
1621
319
  addIRelativeRelocs();
1622
319
1623
319
  if (In.Plt && In.Plt->isNeeded())
1624
41
    In.Plt->addSymbols();
1625
319
  if (In.Iplt && In.Iplt->isNeeded())
1626
7
    In.Iplt->addSymbols();
1627
319
1628
319
  if (!Config->AllowShlibUndefined) {
1629
206
    // Error on undefined symbols in a shared object, if all of its DT_NEEDED
1630
206
    // entires are seen. These cases would otherwise lead to runtime errors
1631
206
    // reported by the dynamic linker.
1632
206
    //
1633
206
    // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker to
1634
206
    // catch more cases. That is too much for us. Our approach resembles the one
1635
206
    // used in ld.gold, achieves a good balance to be useful but not too smart.
1636
206
    for (SharedFile *File : SharedFiles)
1637
52
      File->AllNeededIsKnown =
1638
52
          llvm::all_of(File->DtNeeded, [&](StringRef Needed) {
1639
52
            return Symtab->SoNames.count(Needed);
1640
52
          });
1641
206
    for (Symbol *Sym : Symtab->getSymbols())
1642
875
      if (Sym->isUndefined() && 
!Sym->isWeak()18
)
1643
5
        if (auto *F = dyn_cast_or_null<SharedFile>(Sym->File))
1644
0
          if (F->AllNeededIsKnown)
1645
0
            error(toString(F) + ": undefined reference to " + toString(*Sym));
1646
206
  }
1647
319
1648
319
  // Now that we have defined all possible global symbols including linker-
1649
319
  // synthesized ones. Visit all symbols to give the finishing touches.
1650
1.26k
  for (Symbol *Sym : Symtab->getSymbols()) {
1651
1.26k
    if (!includeInSymtab(*Sym))
1652
41
      continue;
1653
1.22k
    if (In.SymTab)
1654
1.22k
      In.SymTab->addSymbol(Sym);
1655
1.22k
1656
1.22k
    if (Sym->includeInDynsym()) {
1657
326
      In.DynSymTab->addSymbol(Sym);
1658
326
      if (auto *File = dyn_cast_or_null<SharedFile>(Sym->File))
1659
74
        if (File->IsNeeded && !Sym->isUndefined())
1660
74
          addVerneed(Sym);
1661
326
    }
1662
1.22k
  }
1663
319
1664
319
  // Do not proceed if there was an undefined symbol.
1665
319
  if (errorCount())
1666
6
    return;
1667
313
1668
313
  if (In.MipsGot)
1669
23
    In.MipsGot->build();
1670
313
1671
313
  removeUnusedSyntheticSections();
1672
313
1673
313
  sortSections();
1674
313
1675
313
  // Now that we have the final list, create a list of all the
1676
313
  // OutputSections for convenience.
1677
313
  for (BaseCommand *Base : Script->SectionCommands)
1678
3.01k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1679
2.99k
      OutputSections.push_back(Sec);
1680
313
1681
313
  // Prefer command line supplied address over other constraints.
1682
2.99k
  for (OutputSection *Sec : OutputSections) {
1683
2.99k
    auto I = Config->SectionStartMap.find(Sec->Name);
1684
2.99k
    if (I != Config->SectionStartMap.end())
1685
12
      Sec->AddrExpr = [=] { return I->second; };
1686
2.99k
  }
1687
313
1688
313
  // This is a bit of a hack. A value of 0 means undef, so we set it
1689
313
  // to 1 to make __ehdr_start defined. The section number is not
1690
313
  // particularly relevant.
1691
313
  Out::ElfHeader->SectionIndex = 1;
1692
313
1693
3.31k
  for (size_t I = 0, E = OutputSections.size(); I != E; 
++I2.99k
) {
1694
2.99k
    OutputSection *Sec = OutputSections[I];
1695
2.99k
    Sec->SectionIndex = I + 1;
1696
2.99k
    Sec->ShName = In.ShStrTab->addString(Sec->Name);
1697
2.99k
  }
1698
313
1699
313
  // Binary and relocatable output does not have PHDRS.
1700
313
  // The headers have to be created before finalize as that can influence the
1701
313
  // image base and the dynamic section on mips includes the image base.
1702
313
  if (!Config->Relocatable && 
!Config->OFormatBinary302
) {
1703
302
    Phdrs = Script->hasPhdrsCommands() ? 
Script->createPhdrs()5
:
createPhdrs()297
;
1704
302
    if (Config->EMachine == EM_ARM) {
1705
147
      // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
1706
147
      addPhdrForSection(Phdrs, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);
1707
147
    }
1708
302
    if (Config->EMachine == EM_MIPS) {
1709
22
      // Add separate segments for MIPS-specific sections.
1710
22
      addPhdrForSection(Phdrs, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);
1711
22
      addPhdrForSection(Phdrs, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);
1712
22
      addPhdrForSection(Phdrs, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);
1713
22
    }
1714
302
    Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
1715
302
1716
302
    // Find the TLS segment. This happens before the section layout loop so that
1717
302
    // Android relocation packing can look up TLS symbol addresses.
1718
302
    for (PhdrEntry *P : Phdrs)
1719
1.84k
      if (P->p_type == PT_TLS)
1720
32
        Out::TlsPhdr = P;
1721
302
  }
1722
313
1723
313
  // Some symbols are defined in term of program headers. Now that we
1724
313
  // have the headers, we can find out which sections they point to.
1725
313
  setReservedSymbolSections();
1726
313
1727
313
  // Dynamic section must be the last one in this list and dynamic
1728
313
  // symbol table section (DynSymTab) must be the first one.
1729
313
  finalizeSynthetic(In.DynSymTab);
1730
313
  finalizeSynthetic(In.ARMExidx);
1731
313
  finalizeSynthetic(In.Bss);
1732
313
  finalizeSynthetic(In.BssRelRo);
1733
313
  finalizeSynthetic(In.GnuHashTab);
1734
313
  finalizeSynthetic(In.HashTab);
1735
313
  finalizeSynthetic(In.SymTabShndx);
1736
313
  finalizeSynthetic(In.ShStrTab);
1737
313
  finalizeSynthetic(In.StrTab);
1738
313
  finalizeSynthetic(In.VerDef);
1739
313
  finalizeSynthetic(In.Got);
1740
313
  finalizeSynthetic(In.MipsGot);
1741
313
  finalizeSynthetic(In.IgotPlt);
1742
313
  finalizeSynthetic(In.GotPlt);
1743
313
  finalizeSynthetic(In.RelaDyn);
1744
313
  finalizeSynthetic(In.RelrDyn);
1745
313
  finalizeSynthetic(In.RelaIplt);
1746
313
  finalizeSynthetic(In.RelaPlt);
1747
313
  finalizeSynthetic(In.Plt);
1748
313
  finalizeSynthetic(In.Iplt);
1749
313
  finalizeSynthetic(In.EhFrameHdr);
1750
313
  finalizeSynthetic(In.VerSym);
1751
313
  finalizeSynthetic(In.VerNeed);
1752
313
  finalizeSynthetic(In.Dynamic);
1753
313
1754
313
  if (!Script->HasSectionsCommand && 
!Config->Relocatable270
)
1755
259
    fixSectionAlignments();
1756
313
1757
313
  // SHFLinkOrder processing must be processed after relative section placements are
1758
313
  // known but before addresses are allocated.
1759
313
  resolveShfLinkOrder();
1760
313
1761
313
  // This is used to:
1762
313
  // 1) Create "thunks":
1763
313
  //    Jump instructions in many ISAs have small displacements, and therefore
1764
313
  //    they cannot jump to arbitrary addresses in memory. For example, RISC-V
1765
313
  //    JAL instruction can target only +-1 MiB from PC. It is a linker's
1766
313
  //    responsibility to create and insert small pieces of code between
1767
313
  //    sections to extend the ranges if jump targets are out of range. Such
1768
313
  //    code pieces are called "thunks".
1769
313
  //
1770
313
  //    We add thunks at this stage. We couldn't do this before this point
1771
313
  //    because this is the earliest point where we know sizes of sections and
1772
313
  //    their layouts (that are needed to determine if jump targets are in
1773
313
  //    range).
1774
313
  //
1775
313
  // 2) Update the sections. We need to generate content that depends on the
1776
313
  //    address of InputSections. For example, MIPS GOT section content or
1777
313
  //    android packed relocations sections content.
1778
313
  //
1779
313
  // 3) Assign the final values for the linker script symbols. Linker scripts
1780
313
  //    sometimes using forward symbol declarations. We want to set the correct
1781
313
  //    values. They also might change after adding the thunks.
1782
313
  finalizeAddressDependentContent();
1783
313
1784
313
  // finalizeAddressDependentContent may have added local symbols to the static symbol table.
1785
313
  finalizeSynthetic(In.SymTab);
1786
313
  finalizeSynthetic(In.PPC64LongBranchTarget);
1787
313
1788
313
  // Fill other section headers. The dynamic table is finalized
1789
313
  // at the end because some tags like RELSZ depend on result
1790
313
  // of finalizing other sections.
1791
313
  for (OutputSection *Sec : OutputSections)
1792
2.99k
    Sec->finalize();
1793
313
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeSections()
Line
Count
Source
1575
133
template <class ELFT> void Writer<ELFT>::finalizeSections() {
1576
133
  Out::PreinitArray = findSection(".preinit_array");
1577
133
  Out::InitArray = findSection(".init_array");
1578
133
  Out::FiniArray = findSection(".fini_array");
1579
133
1580
133
  // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1581
133
  // symbols for sections, so that the runtime can get the start and end
1582
133
  // addresses of each section by section name. Add such symbols.
1583
133
  if (!Config->Relocatable) {
1584
122
    addStartEndSymbols();
1585
122
    for (BaseCommand *Base : Script->SectionCommands)
1586
2.48k
      if (auto *Sec = dyn_cast<OutputSection>(Base))
1587
2.48k
        addStartStopSymbols(Sec);
1588
122
  }
1589
133
1590
133
  // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1591
133
  // It should be okay as no one seems to care about the type.
1592
133
  // Even the author of gold doesn't remember why gold behaves that way.
1593
133
  // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1594
133
  if (In.Dynamic->Parent)
1595
73
    Symtab->addSymbol(Defined{/*File=*/nullptr, "_DYNAMIC", STB_WEAK,
1596
73
                              STV_HIDDEN, STT_NOTYPE,
1597
73
                              /*Value=*/0, /*Size=*/0, In.Dynamic});
1598
133
1599
133
  // Define __rel[a]_iplt_{start,end} symbols if needed.
1600
133
  addRelIpltSymbols();
1601
133
1602
133
  // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800 if not defined.
1603
133
  if (Config->EMachine == EM_RISCV)
1604
0
    if (!dyn_cast_or_null<Defined>(Symtab->find("__global_pointer$")))
1605
0
      addOptionalRegular("__global_pointer$", findSection(".sdata"), 0x800);
1606
133
1607
133
  // This responsible for splitting up .eh_frame section into
1608
133
  // pieces. The relocation scan uses those pieces, so this has to be
1609
133
  // earlier.
1610
133
  finalizeSynthetic(In.EhFrame);
1611
133
1612
133
  for (Symbol *S : Symtab->getSymbols())
1613
622
    if (!S->IsPreemptible)
1614
622
      S->IsPreemptible = computeIsPreemptible(*S);
1615
133
1616
133
  // Scan relocations. This must be done after every symbol is declared so that
1617
133
  // we can correctly decide if a dynamic relocation is needed.
1618
133
  if (!Config->Relocatable)
1619
122
    forEachRelSec(scanRelocations<ELFT>);
1620
133
1621
133
  addIRelativeRelocs();
1622
133
1623
133
  if (In.Plt && In.Plt->isNeeded())
1624
11
    In.Plt->addSymbols();
1625
133
  if (In.Iplt && In.Iplt->isNeeded())
1626
0
    In.Iplt->addSymbols();
1627
133
1628
133
  if (!Config->AllowShlibUndefined) {
1629
81
    // Error on undefined symbols in a shared object, if all of its DT_NEEDED
1630
81
    // entires are seen. These cases would otherwise lead to runtime errors
1631
81
    // reported by the dynamic linker.
1632
81
    //
1633
81
    // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker to
1634
81
    // catch more cases. That is too much for us. Our approach resembles the one
1635
81
    // used in ld.gold, achieves a good balance to be useful but not too smart.
1636
81
    for (SharedFile *File : SharedFiles)
1637
20
      File->AllNeededIsKnown =
1638
20
          llvm::all_of(File->DtNeeded, [&](StringRef Needed) {
1639
20
            return Symtab->SoNames.count(Needed);
1640
20
          });
1641
81
    for (Symbol *Sym : Symtab->getSymbols())
1642
373
      if (Sym->isUndefined() && 
!Sym->isWeak()0
)
1643
0
        if (auto *F = dyn_cast_or_null<SharedFile>(Sym->File))
1644
0
          if (F->AllNeededIsKnown)
1645
0
            error(toString(F) + ": undefined reference to " + toString(*Sym));
1646
81
  }
1647
133
1648
133
  // Now that we have defined all possible global symbols including linker-
1649
133
  // synthesized ones. Visit all symbols to give the finishing touches.
1650
622
  for (Symbol *Sym : Symtab->getSymbols()) {
1651
622
    if (!includeInSymtab(*Sym))
1652
68
      continue;
1653
554
    if (In.SymTab)
1654
554
      In.SymTab->addSymbol(Sym);
1655
554
1656
554
    if (Sym->includeInDynsym()) {
1657
164
      In.DynSymTab->addSymbol(Sym);
1658
164
      if (auto *File = dyn_cast_or_null<SharedFile>(Sym->File))
1659
27
        if (File->IsNeeded && !Sym->isUndefined())
1660
27
          addVerneed(Sym);
1661
164
    }
1662
554
  }
1663
133
1664
133
  // Do not proceed if there was an undefined symbol.
1665
133
  if (errorCount())
1666
3
    return;
1667
130
1668
130
  if (In.MipsGot)
1669
123
    In.MipsGot->build();
1670
130
1671
130
  removeUnusedSyntheticSections();
1672
130
1673
130
  sortSections();
1674
130
1675
130
  // Now that we have the final list, create a list of all the
1676
130
  // OutputSections for convenience.
1677
130
  for (BaseCommand *Base : Script->SectionCommands)
1678
1.64k
    if (auto *Sec = dyn_cast<OutputSection>(Base))
1679
1.64k
      OutputSections.push_back(Sec);
1680
130
1681
130
  // Prefer command line supplied address over other constraints.
1682
1.64k
  for (OutputSection *Sec : OutputSections) {
1683
1.64k
    auto I = Config->SectionStartMap.find(Sec->Name);
1684
1.64k
    if (I != Config->SectionStartMap.end())
1685
0
      Sec->AddrExpr = [=] { return I->second; };
1686
1.64k
  }
1687
130
1688
130
  // This is a bit of a hack. A value of 0 means undef, so we set it
1689
130
  // to 1 to make __ehdr_start defined. The section number is not
1690
130
  // particularly relevant.
1691
130
  Out::ElfHeader->SectionIndex = 1;
1692
130
1693
1.77k
  for (size_t I = 0, E = OutputSections.size(); I != E; 
++I1.64k
) {
1694
1.64k
    OutputSection *Sec = OutputSections[I];
1695
1.64k
    Sec->SectionIndex = I + 1;
1696
1.64k
    Sec->ShName = In.ShStrTab->addString(Sec->Name);
1697
1.64k
  }
1698
130
1699
130
  // Binary and relocatable output does not have PHDRS.
1700
130
  // The headers have to be created before finalize as that can influence the
1701
130
  // image base and the dynamic section on mips includes the image base.
1702
130
  if (!Config->Relocatable && 
!Config->OFormatBinary119
) {
1703
119
    Phdrs = Script->hasPhdrsCommands() ? 
Script->createPhdrs()0
: createPhdrs();
1704
119
    if (Config->EMachine == EM_ARM) {
1705
0
      // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
1706
0
      addPhdrForSection(Phdrs, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);
1707
0
    }
1708
119
    if (Config->EMachine == EM_MIPS) {
1709
112
      // Add separate segments for MIPS-specific sections.
1710
112
      addPhdrForSection(Phdrs, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);
1711
112
      addPhdrForSection(Phdrs, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);
1712
112
      addPhdrForSection(Phdrs, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);
1713
112
    }
1714
119
    Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
1715
119
1716
119
    // Find the TLS segment. This happens before the section layout loop so that
1717
119
    // Android relocation packing can look up TLS symbol addresses.
1718
119
    for (PhdrEntry *P : Phdrs)
1719
897
      if (P->p_type == PT_TLS)
1720
7
        Out::TlsPhdr = P;
1721
119
  }
1722
130
1723
130
  // Some symbols are defined in term of program headers. Now that we
1724
130
  // have the headers, we can find out which sections they point to.
1725
130
  setReservedSymbolSections();
1726
130
1727
130
  // Dynamic section must be the last one in this list and dynamic
1728
130
  // symbol table section (DynSymTab) must be the first one.
1729
130
  finalizeSynthetic(In.DynSymTab);
1730
130
  finalizeSynthetic(In.ARMExidx);
1731
130
  finalizeSynthetic(In.Bss);
1732
130
  finalizeSynthetic(In.BssRelRo);
1733
130
  finalizeSynthetic(In.GnuHashTab);
1734
130
  finalizeSynthetic(In.HashTab);
1735
130
  finalizeSynthetic(In.SymTabShndx);
1736
130
  finalizeSynthetic(In.ShStrTab);
1737
130
  finalizeSynthetic(In.StrTab);
1738
130
  finalizeSynthetic(In.VerDef);
1739
130
  finalizeSynthetic(In.Got);
1740
130
  finalizeSynthetic(In.MipsGot);
1741
130
  finalizeSynthetic(In.IgotPlt);
1742
130
  finalizeSynthetic(In.GotPlt);
1743
130
  finalizeSynthetic(In.RelaDyn);
1744
130
  finalizeSynthetic(In.RelrDyn);
1745
130
  finalizeSynthetic(In.RelaIplt);
1746
130
  finalizeSynthetic(In.RelaPlt);
1747
130
  finalizeSynthetic(In.Plt);
1748
130
  finalizeSynthetic(In.Iplt);
1749
130
  finalizeSynthetic(In.EhFrameHdr);
1750
130
  finalizeSynthetic(In.VerSym);
1751
130
  finalizeSynthetic(In.VerNeed);
1752
130
  finalizeSynthetic(In.Dynamic);
1753
130
1754
130
  if (!Script->HasSectionsCommand && 
!Config->Relocatable121
)
1755
110
    fixSectionAlignments();
1756
130
1757
130
  // SHFLinkOrder processing must be processed after relative section placements are
1758
130
  // known but before addresses are allocated.
1759
130
  resolveShfLinkOrder();
1760
130
1761
130
  // This is used to:
1762
130
  // 1) Create "thunks":
1763
130
  //    Jump instructions in many ISAs have small displacements, and therefore
1764
130
  //    they cannot jump to arbitrary addresses in memory. For example, RISC-V
1765
130
  //    JAL instruction can target only +-1 MiB from PC. It is a linker's
1766
130
  //    responsibility to create and insert small pieces of code between
1767
130
  //    sections to extend the ranges if jump targets are out of range. Such
1768
130
  //    code pieces are called "thunks".
1769
130
  //
1770
130
  //    We add thunks at this stage. We couldn't do this before this point
1771
130
  //    because this is the earliest point where we know sizes of sections and
1772
130
  //    their layouts (that are needed to determine if jump targets are in
1773
130
  //    range).
1774
130
  //
1775
130
  // 2) Update the sections. We need to generate content that depends on the
1776
130
  //    address of InputSections. For example, MIPS GOT section content or
1777
130
  //    android packed relocations sections content.
1778
130
  //
1779
130
  // 3) Assign the final values for the linker script symbols. Linker scripts
1780
130
  //    sometimes using forward symbol declarations. We want to set the correct
1781
130
  //    values. They also might change after adding the thunks.
1782
130
  finalizeAddressDependentContent();
1783
130
1784
130
  // finalizeAddressDependentContent may have added local symbols to the static symbol table.
1785
130
  finalizeSynthetic(In.SymTab);
1786
130
  finalizeSynthetic(In.PPC64LongBranchTarget);
1787
130
1788
130
  // Fill other section headers. The dynamic table is finalized
1789
130
  // at the end because some tags like RELSZ depend on result
1790
130
  // of finalizing other sections.
1791
130
  for (OutputSection *Sec : OutputSections)
1792
1.64k
    Sec->finalize();
1793
130
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeSections()
Line
Count
Source
1575
1.96k
template <class ELFT> void Writer<ELFT>::finalizeSections() {
1576
1.96k
  Out::PreinitArray = findSection(".preinit_array");
1577
1.96k
  Out::InitArray = findSection(".init_array");
1578
1.96k
  Out::FiniArray = findSection(".fini_array");
1579
1.96k
1580
1.96k
  // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1581
1.96k
  // symbols for sections, so that the runtime can get the start and end
1582
1.96k
  // addresses of each section by section name. Add such symbols.
1583
1.96k
  if (!Config->Relocatable) {
1584
1.90k
    addStartEndSymbols();
1585
1.90k
    for (BaseCommand *Base : Script->SectionCommands)
1586
297k
      if (auto *Sec = dyn_cast<OutputSection>(Base))
1587
296k
        addStartStopSymbols(Sec);
1588
1.90k
  }
1589
1.96k
1590
1.96k
  // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1591
1.96k
  // It should be okay as no one seems to care about the type.
1592
1.96k
  // Even the author of gold doesn't remember why gold behaves that way.
1593
1.96k
  // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1594
1.96k
  if (In.Dynamic->Parent)
1595
936
    Symtab->addSymbol(Defined{/*File=*/nullptr, "_DYNAMIC", STB_WEAK,
1596
936
                              STV_HIDDEN, STT_NOTYPE,
1597
936
                              /*Value=*/0, /*Size=*/0, In.Dynamic});
1598
1.96k
1599
1.96k
  // Define __rel[a]_iplt_{start,end} symbols if needed.
1600
1.96k
  addRelIpltSymbols();
1601
1.96k
1602
1.96k
  // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800 if not defined.
1603
1.96k
  if (Config->EMachine == EM_RISCV)
1604
0
    if (!dyn_cast_or_null<Defined>(Symtab->find("__global_pointer$")))
1605
0
      addOptionalRegular("__global_pointer$", findSection(".sdata"), 0x800);
1606
1.96k
1607
1.96k
  // This responsible for splitting up .eh_frame section into
1608
1.96k
  // pieces. The relocation scan uses those pieces, so this has to be
1609
1.96k
  // earlier.
1610
1.96k
  finalizeSynthetic(In.EhFrame);
1611
1.96k
1612
1.96k
  for (Symbol *S : Symtab->getSymbols())
1613
5.48k
    if (!S->IsPreemptible)
1614
5.47k
      S->IsPreemptible = computeIsPreemptible(*S);
1615
1.96k
1616
1.96k
  // Scan relocations. This must be done after every symbol is declared so that
1617
1.96k
  // we can correctly decide if a dynamic relocation is needed.
1618
1.96k
  if (!Config->Relocatable)
1619
1.90k
    forEachRelSec(scanRelocations<ELFT>);
1620
1.96k
1621
1.96k
  addIRelativeRelocs();
1622
1.96k
1623
1.96k
  if (In.Plt && In.Plt->isNeeded())
1624
172
    In.Plt->addSymbols();
1625
1.96k
  if (In.Iplt && In.Iplt->isNeeded())
1626
34
    In.Iplt->addSymbols();
1627
1.96k
1628
1.96k
  if (!Config->AllowShlibUndefined) {