Coverage Report

Created: 2018-10-20 06:24

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