Coverage Report

Created: 2018-06-25 02:00

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