Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Writer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Writer.cpp ---------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "Writer.h"
10
#include "AArch64ErrataFix.h"
11
#include "CallGraphSort.h"
12
#include "Config.h"
13
#include "LinkerScript.h"
14
#include "MapFile.h"
15
#include "OutputSections.h"
16
#include "Relocations.h"
17
#include "SymbolTable.h"
18
#include "Symbols.h"
19
#include "SyntheticSections.h"
20
#include "Target.h"
21
#include "lld/Common/Filesystem.h"
22
#include "lld/Common/Memory.h"
23
#include "lld/Common/Strings.h"
24
#include "lld/Common/Threads.h"
25
#include "llvm/ADT/StringMap.h"
26
#include "llvm/ADT/StringSwitch.h"
27
#include "llvm/Support/RandomNumberGenerator.h"
28
#include "llvm/Support/SHA1.h"
29
#include "llvm/Support/xxhash.h"
30
#include <climits>
31
32
using namespace llvm;
33
using namespace llvm::ELF;
34
using namespace llvm::object;
35
using namespace llvm::support;
36
using namespace llvm::support::endian;
37
38
using namespace lld;
39
using namespace lld::elf;
40
41
namespace {
42
// The writer writes a SymbolTable result to a file.
43
template <class ELFT> class Writer {
44
public:
45
2.75k
  Writer() : buffer(errorHandler().outputBuffer) {}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::Writer()
Line
Count
Source
45
376
  Writer() : buffer(errorHandler().outputBuffer) {}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::Writer()
Line
Count
Source
45
157
  Writer() : buffer(errorHandler().outputBuffer) {}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::Writer()
Line
Count
Source
45
2.11k
  Writer() : buffer(errorHandler().outputBuffer) {}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::Writer()
Line
Count
Source
45
108
  Writer() : buffer(errorHandler().outputBuffer) {}
46
  using Elf_Shdr = typename ELFT::Shdr;
47
  using Elf_Ehdr = typename ELFT::Ehdr;
48
  using Elf_Phdr = typename ELFT::Phdr;
49
50
  void run();
51
52
private:
53
  void copyLocalSymbols();
54
  void addSectionSymbols();
55
  void forEachRelSec(llvm::function_ref<void(InputSectionBase &)> fn);
56
  void sortSections();
57
  void resolveShfLinkOrder();
58
  void finalizeAddressDependentContent();
59
  void sortInputSections();
60
  void finalizeSections();
61
  void checkExecuteOnly();
62
  void setReservedSymbolSections();
63
64
  std::vector<PhdrEntry *> createPhdrs(Partition &part);
65
  void removeEmptyPTLoad(std::vector<PhdrEntry *> &phdrEntry);
66
  void addPhdrForSection(Partition &part, unsigned shType, unsigned pType,
67
                         unsigned pFlags);
68
  void assignFileOffsets();
69
  void assignFileOffsetsBinary();
70
  void setPhdrs(Partition &part);
71
  void checkSections();
72
  void fixSectionAlignments();
73
  void openFile();
74
  void writeTrapInstr();
75
  void writeHeader();
76
  void writeSections();
77
  void writeSectionsBinary();
78
  void writeBuildId();
79
80
  std::unique_ptr<FileOutputBuffer> &buffer;
81
82
  void addRelIpltSymbols();
83
  void addStartEndSymbols();
84
  void addStartStopSymbols(OutputSection *sec);
85
86
  uint64_t fileSize;
87
  uint64_t sectionHeaderOff;
88
};
89
} // anonymous namespace
90
91
67.6M
static bool isSectionPrefix(StringRef prefix, StringRef name) {
92
67.6M
  return name.startswith(prefix) || 
name == prefix.drop_back()67.6M
;
93
67.6M
}
94
95
4.58M
StringRef elf::getOutputSectionName(const InputSectionBase *s) {
96
4.58M
  if (config->relocatable)
97
67.1k
    return s->name;
98
4.52M
99
4.52M
  // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
100
4.52M
  // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
101
4.52M
  // technically required, but not doing it is odd). This code guarantees that.
102
4.52M
  if (auto *isec = dyn_cast<InputSection>(s)) {
103
4.51M
    if (InputSectionBase *rel = isec->getRelocatedSection()) {
104
27
      OutputSection *out = rel->getOutputSection();
105
27
      if (s->type == SHT_RELA)
106
26
        return saver.save(".rela" + out->name);
107
1
      return saver.save(".rel" + out->name);
108
1
    }
109
4.51M
  }
110
4.52M
111
4.52M
  // This check is for -z keep-text-section-prefix.  This option separates text
112
4.52M
  // sections with prefix ".text.hot", ".text.unlikely", ".text.startup" or
113
4.52M
  // ".text.exit".
114
4.52M
  // When enabled, this allows identifying the hot code region (.text.hot) in
115
4.52M
  // the final binary which can be selectively mapped to huge pages or mlocked,
116
4.52M
  // for instance.
117
4.52M
  if (config->zKeepTextSectionPrefix)
118
22
    for (StringRef v :
119
22
         {".text.hot.", ".text.unlikely.", ".text.startup.", ".text.exit."})
120
82
      if (isSectionPrefix(v, s->name))
121
4
        return v.drop_back();
122
4.52M
123
4.52M
  for (StringRef v :
124
4.52M
       {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
125
4.52M
        ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
126
4.52M
        ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."})
127
67.6M
    if (isSectionPrefix(v, s->name))
128
12.9k
      return v.drop_back();
129
4.52M
130
4.52M
  // CommonSection is identified as "COMMON" in linker scripts.
131
4.52M
  // By default, it should go to .bss section.
132
4.52M
  
if (4.50M
s->name == "COMMON"4.50M
)
133
82
    return ".bss";
134
4.50M
135
4.50M
  return s->name;
136
4.50M
}
137
138
5.43k
static bool needsInterpSection() {
139
5.43k
  return !sharedFiles.empty() && 
!config->dynamicLinker.empty()741
&&
140
5.43k
         
script->needsInterpSection()20
;
141
5.43k
}
142
143
2.75k
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
void lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
143
376
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
void lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
143
157
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
void lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
143
2.11k
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
void lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
143
108
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
144
145
template <class ELFT>
146
2.59k
void Writer<ELFT>::removeEmptyPTLoad(std::vector<PhdrEntry *> &phdrs) {
147
14.5k
  llvm::erase_if(phdrs, [&](const PhdrEntry *p) {
148
14.5k
    if (p->p_type != PT_LOAD)
149
7.73k
      return false;
150
6.85k
    if (!p->firstSec)
151
41
      return true;
152
6.81k
    uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
153
6.81k
    return size == 0;
154
6.81k
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::removeEmptyPTLoad(std::__1::vector<lld::elf::PhdrEntry*, std::__1::allocator<lld::elf::PhdrEntry*> >&)::'lambda'(lld::elf::PhdrEntry const*)::operator()(lld::elf::PhdrEntry const*) const
Line
Count
Source
147
2.13k
  llvm::erase_if(phdrs, [&](const PhdrEntry *p) {
148
2.13k
    if (p->p_type != PT_LOAD)
149
1.15k
      return false;
150
986
    if (!p->firstSec)
151
4
      return true;
152
982
    uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
153
982
    return size == 0;
154
982
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::removeEmptyPTLoad(std::__1::vector<lld::elf::PhdrEntry*, std::__1::allocator<lld::elf::PhdrEntry*> >&)::'lambda'(lld::elf::PhdrEntry const*)::operator()(lld::elf::PhdrEntry const*) const
Line
Count
Source
147
1.04k
  llvm::erase_if(phdrs, [&](const PhdrEntry *p) {
148
1.04k
    if (p->p_type != PT_LOAD)
149
623
      return false;
150
422
    if (!p->firstSec)
151
0
      return true;
152
422
    uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
153
422
    return size == 0;
154
422
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::removeEmptyPTLoad(std::__1::vector<lld::elf::PhdrEntry*, std::__1::allocator<lld::elf::PhdrEntry*> >&)::'lambda'(lld::elf::PhdrEntry const*)::operator()(lld::elf::PhdrEntry const*) const
Line
Count
Source
147
10.6k
  llvm::erase_if(phdrs, [&](const PhdrEntry *p) {
148
10.6k
    if (p->p_type != PT_LOAD)
149
5.57k
      return false;
150
5.09k
    if (!p->firstSec)
151
37
      return true;
152
5.06k
    uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
153
5.06k
    return size == 0;
154
5.06k
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::removeEmptyPTLoad(std::__1::vector<lld::elf::PhdrEntry*, std::__1::allocator<lld::elf::PhdrEntry*> >&)::'lambda'(lld::elf::PhdrEntry const*)::operator()(lld::elf::PhdrEntry const*) const
Line
Count
Source
147
733
  llvm::erase_if(phdrs, [&](const PhdrEntry *p) {
148
733
    if (p->p_type != PT_LOAD)
149
387
      return false;
150
346
    if (!p->firstSec)
151
0
      return true;
152
346
    uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
153
346
    return size == 0;
154
346
  });
155
2.59k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::removeEmptyPTLoad(std::__1::vector<lld::elf::PhdrEntry*, std::__1::allocator<lld::elf::PhdrEntry*> >&)
Line
Count
Source
146
370
void Writer<ELFT>::removeEmptyPTLoad(std::vector<PhdrEntry *> &phdrs) {
147
370
  llvm::erase_if(phdrs, [&](const PhdrEntry *p) {
148
370
    if (p->p_type != PT_LOAD)
149
370
      return false;
150
370
    if (!p->firstSec)
151
370
      return true;
152
370
    uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
153
370
    return size == 0;
154
370
  });
155
370
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::removeEmptyPTLoad(std::__1::vector<lld::elf::PhdrEntry*, std::__1::allocator<lld::elf::PhdrEntry*> >&)
Line
Count
Source
146
154
void Writer<ELFT>::removeEmptyPTLoad(std::vector<PhdrEntry *> &phdrs) {
147
154
  llvm::erase_if(phdrs, [&](const PhdrEntry *p) {
148
154
    if (p->p_type != PT_LOAD)
149
154
      return false;
150
154
    if (!p->firstSec)
151
154
      return true;
152
154
    uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
153
154
    return size == 0;
154
154
  });
155
154
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::removeEmptyPTLoad(std::__1::vector<lld::elf::PhdrEntry*, std::__1::allocator<lld::elf::PhdrEntry*> >&)
Line
Count
Source
146
1.97k
void Writer<ELFT>::removeEmptyPTLoad(std::vector<PhdrEntry *> &phdrs) {
147
1.97k
  llvm::erase_if(phdrs, [&](const PhdrEntry *p) {
148
1.97k
    if (p->p_type != PT_LOAD)
149
1.97k
      return false;
150
1.97k
    if (!p->firstSec)
151
1.97k
      return true;
152
1.97k
    uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
153
1.97k
    return size == 0;
154
1.97k
  });
155
1.97k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::removeEmptyPTLoad(std::__1::vector<lld::elf::PhdrEntry*, std::__1::allocator<lld::elf::PhdrEntry*> >&)
Line
Count
Source
146
101
void Writer<ELFT>::removeEmptyPTLoad(std::vector<PhdrEntry *> &phdrs) {
147
101
  llvm::erase_if(phdrs, [&](const PhdrEntry *p) {
148
101
    if (p->p_type != PT_LOAD)
149
101
      return false;
150
101
    if (!p->firstSec)
151
101
      return true;
152
101
    uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
153
101
    return size == 0;
154
101
  });
155
101
}
156
157
2.75k
template <class ELFT> static void copySectionsIntoPartitions() {
158
2.75k
  std::vector<InputSectionBase *> newSections;
159
2.77k
  for (unsigned part = 2; part != partitions.size() + 1; 
++part18
) {
160
93
    for (InputSectionBase *s : inputSections) {
161
93
      if (!(s->flags & SHF_ALLOC) || 
!s->isLive()73
)
162
33
        continue;
163
60
      InputSectionBase *copy;
164
60
      if (s->type == SHT_NOTE)
165
1
        copy = make<InputSection>(cast<InputSection>(*s));
166
59
      else if (auto *es = dyn_cast<EhInputSection>(s))
167
1
        copy = make<EhInputSection>(*es);
168
58
      else
169
58
        continue;
170
2
      copy->partition = part;
171
2
      newSections.push_back(copy);
172
2
    }
173
18
  }
174
2.75k
175
2.75k
  inputSections.insert(inputSections.end(), newSections.begin(),
176
2.75k
                       newSections.end());
177
2.75k
}
Writer.cpp:void copySectionsIntoPartitions<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
157
376
template <class ELFT> static void copySectionsIntoPartitions() {
158
376
  std::vector<InputSectionBase *> newSections;
159
380
  for (unsigned part = 2; part != partitions.size() + 1; 
++part4
) {
160
26
    for (InputSectionBase *s : inputSections) {
161
26
      if (!(s->flags & SHF_ALLOC) || 
!s->isLive()20
)
162
9
        continue;
163
17
      InputSectionBase *copy;
164
17
      if (s->type == SHT_NOTE)
165
0
        copy = make<InputSection>(cast<InputSection>(*s));
166
17
      else if (auto *es = dyn_cast<EhInputSection>(s))
167
0
        copy = make<EhInputSection>(*es);
168
17
      else
169
17
        continue;
170
0
      copy->partition = part;
171
0
      newSections.push_back(copy);
172
0
    }
173
4
  }
174
376
175
376
  inputSections.insert(inputSections.end(), newSections.begin(),
176
376
                       newSections.end());
177
376
}
Writer.cpp:void copySectionsIntoPartitions<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
157
157
template <class ELFT> static void copySectionsIntoPartitions() {
158
157
  std::vector<InputSectionBase *> newSections;
159
157
  for (unsigned part = 2; part != partitions.size() + 1; 
++part0
) {
160
0
    for (InputSectionBase *s : inputSections) {
161
0
      if (!(s->flags & SHF_ALLOC) || !s->isLive())
162
0
        continue;
163
0
      InputSectionBase *copy;
164
0
      if (s->type == SHT_NOTE)
165
0
        copy = make<InputSection>(cast<InputSection>(*s));
166
0
      else if (auto *es = dyn_cast<EhInputSection>(s))
167
0
        copy = make<EhInputSection>(*es);
168
0
      else
169
0
        continue;
170
0
      copy->partition = part;
171
0
      newSections.push_back(copy);
172
0
    }
173
0
  }
174
157
175
157
  inputSections.insert(inputSections.end(), newSections.begin(),
176
157
                       newSections.end());
177
157
}
Writer.cpp:void copySectionsIntoPartitions<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
157
2.11k
template <class ELFT> static void copySectionsIntoPartitions() {
158
2.11k
  std::vector<InputSectionBase *> newSections;
159
2.12k
  for (unsigned part = 2; part != partitions.size() + 1; 
++part14
) {
160
67
    for (InputSectionBase *s : inputSections) {
161
67
      if (!(s->flags & SHF_ALLOC) || 
!s->isLive()53
)
162
24
        continue;
163
43
      InputSectionBase *copy;
164
43
      if (s->type == SHT_NOTE)
165
1
        copy = make<InputSection>(cast<InputSection>(*s));
166
42
      else if (auto *es = dyn_cast<EhInputSection>(s))
167
1
        copy = make<EhInputSection>(*es);
168
41
      else
169
41
        continue;
170
2
      copy->partition = part;
171
2
      newSections.push_back(copy);
172
2
    }
173
14
  }
174
2.11k
175
2.11k
  inputSections.insert(inputSections.end(), newSections.begin(),
176
2.11k
                       newSections.end());
177
2.11k
}
Writer.cpp:void copySectionsIntoPartitions<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
157
108
template <class ELFT> static void copySectionsIntoPartitions() {
158
108
  std::vector<InputSectionBase *> newSections;
159
108
  for (unsigned part = 2; part != partitions.size() + 1; 
++part0
) {
160
0
    for (InputSectionBase *s : inputSections) {
161
0
      if (!(s->flags & SHF_ALLOC) || !s->isLive())
162
0
        continue;
163
0
      InputSectionBase *copy;
164
0
      if (s->type == SHT_NOTE)
165
0
        copy = make<InputSection>(cast<InputSection>(*s));
166
0
      else if (auto *es = dyn_cast<EhInputSection>(s))
167
0
        copy = make<EhInputSection>(*es);
168
0
      else
169
0
        continue;
170
0
      copy->partition = part;
171
0
      newSections.push_back(copy);
172
0
    }
173
0
  }
174
108
175
108
  inputSections.insert(inputSections.end(), newSections.begin(),
176
108
                       newSections.end());
177
108
}
178
179
2.66k
template <class ELFT> static void combineEhSections() {
180
322k
  for (InputSectionBase *&s : inputSections) {
181
322k
    // Ignore dead sections and the partition end marker (.part.end),
182
322k
    // whose partition number is out of bounds.
183
322k
    if (!s->isLive() || 
s->partition == 255322k
)
184
707
      continue;
185
322k
186
322k
    Partition &part = s->getPartition();
187
322k
    if (auto *es = dyn_cast<EhInputSection>(s)) {
188
117
      part.ehFrame->addSection<ELFT>(es);
189
117
      s = nullptr;
190
322k
    } else if (s->kind() == SectionBase::Regular && 
part.armExidx270k
&&
191
322k
               
part.armExidx->addSection(cast<InputSection>(s))846
) {
192
73
      s = nullptr;
193
73
    }
194
322k
  }
195
2.66k
196
2.66k
  std::vector<InputSectionBase *> &v = inputSections;
197
2.66k
  v.erase(std::remove(v.begin(), v.end(), nullptr), v.end());
198
2.66k
}
Writer.cpp:void combineEhSections<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
179
365
template <class ELFT> static void combineEhSections() {
180
8.49k
  for (InputSectionBase *&s : inputSections) {
181
8.49k
    // Ignore dead sections and the partition end marker (.part.end),
182
8.49k
    // whose partition number is out of bounds.
183
8.49k
    if (!s->isLive() || 
s->partition == 2558.42k
)
184
70
      continue;
185
8.42k
186
8.42k
    Partition &part = s->getPartition();
187
8.42k
    if (auto *es = dyn_cast<EhInputSection>(s)) {
188
4
      part.ehFrame->addSection<ELFT>(es);
189
4
      s = nullptr;
190
8.41k
    } else if (s->kind() == SectionBase::Regular && 
part.armExidx1.23k
&&
191
8.41k
               
part.armExidx->addSection(cast<InputSection>(s))846
) {
192
73
      s = nullptr;
193
73
    }
194
8.42k
  }
195
365
196
365
  std::vector<InputSectionBase *> &v = inputSections;
197
365
  v.erase(std::remove(v.begin(), v.end(), nullptr), v.end());
198
365
}
Writer.cpp:void combineEhSections<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
179
146
template <class ELFT> static void combineEhSections() {
180
3.95k
  for (InputSectionBase *&s : inputSections) {
181
3.95k
    // Ignore dead sections and the partition end marker (.part.end),
182
3.95k
    // whose partition number is out of bounds.
183
3.95k
    if (!s->isLive() || 
s->partition == 2553.64k
)
184
304
      continue;
185
3.64k
186
3.64k
    Partition &part = s->getPartition();
187
3.64k
    if (auto *es = dyn_cast<EhInputSection>(s)) {
188
0
      part.ehFrame->addSection<ELFT>(es);
189
0
      s = nullptr;
190
3.64k
    } else if (s->kind() == SectionBase::Regular && 
part.armExidx554
&&
191
3.64k
               
part.armExidx->addSection(cast<InputSection>(s))0
) {
192
0
      s = nullptr;
193
0
    }
194
3.64k
  }
195
146
196
146
  std::vector<InputSectionBase *> &v = inputSections;
197
146
  v.erase(std::remove(v.begin(), v.end(), nullptr), v.end());
198
146
}
Writer.cpp:void combineEhSections<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
179
2.05k
template <class ELFT> static void combineEhSections() {
180
307k
  for (InputSectionBase *&s : inputSections) {
181
307k
    // Ignore dead sections and the partition end marker (.part.end),
182
307k
    // whose partition number is out of bounds.
183
307k
    if (!s->isLive() || 
s->partition == 255307k
)
184
232
      continue;
185
307k
186
307k
    Partition &part = s->getPartition();
187
307k
    if (auto *es = dyn_cast<EhInputSection>(s)) {
188
109
      part.ehFrame->addSection<ELFT>(es);
189
109
      s = nullptr;
190
307k
    } else if (s->kind() == SectionBase::Regular && 
part.armExidx268k
&&
191
307k
               
part.armExidx->addSection(cast<InputSection>(s))0
) {
192
0
      s = nullptr;
193
0
    }
194
307k
  }
195
2.05k
196
2.05k
  std::vector<InputSectionBase *> &v = inputSections;
197
2.05k
  v.erase(std::remove(v.begin(), v.end(), nullptr), v.end());
198
2.05k
}
Writer.cpp:void combineEhSections<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
179
105
template <class ELFT> static void combineEhSections() {
180
2.53k
  for (InputSectionBase *&s : inputSections) {
181
2.53k
    // Ignore dead sections and the partition end marker (.part.end),
182
2.53k
    // whose partition number is out of bounds.
183
2.53k
    if (!s->isLive() || 
s->partition == 2552.43k
)
184
101
      continue;
185
2.43k
186
2.43k
    Partition &part = s->getPartition();
187
2.43k
    if (auto *es = dyn_cast<EhInputSection>(s)) {
188
4
      part.ehFrame->addSection<ELFT>(es);
189
4
      s = nullptr;
190
2.42k
    } else if (s->kind() == SectionBase::Regular && 
part.armExidx281
&&
191
2.42k
               
part.armExidx->addSection(cast<InputSection>(s))0
) {
192
0
      s = nullptr;
193
0
    }
194
2.43k
  }
195
105
196
105
  std::vector<InputSectionBase *> &v = inputSections;
197
105
  v.erase(std::remove(v.begin(), v.end(), nullptr), v.end());
198
105
}
199
200
static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
201
                                   uint64_t val, uint8_t stOther = STV_HIDDEN,
202
568k
                                   uint8_t binding = STB_GLOBAL) {
203
568k
  Symbol *s = symtab->find(name);
204
568k
  if (!s || 
s->isDefined()116
)
205
568k
    return nullptr;
206
108
207
108
  s->resolve(Defined{/*file=*/nullptr, name, binding, stOther, STT_NOTYPE, val,
208
108
                     /*size=*/0, sec});
209
108
  return cast<Defined>(s);
210
108
}
211
212
193
static Defined *addAbsolute(StringRef name) {
213
193
  Symbol *sym = symtab->addSymbol(Defined{nullptr, name, STB_GLOBAL, STV_HIDDEN,
214
193
                                          STT_NOTYPE, 0, 0, nullptr});
215
193
  return cast<Defined>(sym);
216
193
}
217
218
// The linker is expected to define some symbols depending on
219
// the linking result. This function defines such symbols.
220
2.69k
void elf::addReservedSymbols() {
221
2.69k
  if (config->emachine == EM_MIPS) {
222
184
    // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
223
184
    // so that it points to an absolute address which by default is relative
224
184
    // to GOT. Default offset is 0x7ff0.
225
184
    // See "Global Data Symbols" in Chapter 6 in the following document:
226
184
    // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
227
184
    ElfSym::mipsGp = addAbsolute("_gp");
228
184
229
184
    // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
230
184
    // start of function and 'gp' pointer into GOT.
231
184
    if (symtab->find("_gp_disp"))
232
8
      ElfSym::mipsGpDisp = addAbsolute("_gp_disp");
233
184
234
184
    // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
235
184
    // pointer. This symbol is used in the code generated by .cpload pseudo-op
236
184
    // in case of using -mno-shared option.
237
184
    // https://sourceware.org/ml/binutils/2004-12/msg00094.html
238
184
    if (symtab->find("__gnu_local_gp"))
239
1
      ElfSym::mipsLocalGp = addAbsolute("__gnu_local_gp");
240
2.50k
  } else if (config->emachine == EM_PPC) {
241
31
    // glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't
242
31
    // support Small Data Area, define it arbitrarily as 0.
243
31
    addOptionalRegular("_SDA_BASE_", nullptr, 0, STV_HIDDEN);
244
31
  }
245
2.69k
246
2.69k
  // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which
247
2.69k
  // combines the typical ELF GOT with the small data sections. It commonly
248
2.69k
  // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both
249
2.69k
  // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to
250
2.69k
  // represent the TOC base which is offset by 0x8000 bytes from the start of
251
2.69k
  // the .got section.
252
2.69k
  // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the
253
2.69k
  // correctness of some relocations depends on its value.
254
2.69k
  StringRef gotSymName =
255
2.69k
      (config->emachine == EM_PPC64) ? 
".TOC."150
:
"_GLOBAL_OFFSET_TABLE_"2.54k
;
256
2.69k
257
2.69k
  if (Symbol *s = symtab->find(gotSymName)) {
258
120
    if (s->isDefined()) {
259
1
      error(toString(s->file) + " cannot redefine linker defined symbol '" +
260
1
            gotSymName + "'");
261
1
      return;
262
1
    }
263
119
264
119
    uint64_t gotOff = 0;
265
119
    if (config->emachine == EM_PPC64)
266
75
      gotOff = 0x8000;
267
119
268
119
    s->resolve(Defined{/*file=*/nullptr, gotSymName, STB_GLOBAL, STV_HIDDEN,
269
119
                       STT_NOTYPE, gotOff, /*size=*/0, Out::elfHeader});
270
119
    ElfSym::globalOffsetTable = cast<Defined>(s);
271
119
  }
272
2.69k
273
2.69k
  // __ehdr_start is the location of ELF file headers. Note that we define
274
2.69k
  // this symbol unconditionally even when using a linker script, which
275
2.69k
  // differs from the behavior implemented by GNU linker which only define
276
2.69k
  // this symbol if ELF headers are in the memory mapped segment.
277
2.69k
  addOptionalRegular("__ehdr_start", Out::elfHeader, 0, STV_HIDDEN);
278
2.69k
279
2.69k
  // __executable_start is not documented, but the expectation of at
280
2.69k
  // least the Android libc is that it points to the ELF header.
281
2.69k
  addOptionalRegular("__executable_start", Out::elfHeader, 0, STV_HIDDEN);
282
2.69k
283
2.69k
  // __dso_handle symbol is passed to cxa_finalize as a marker to identify
284
2.69k
  // each DSO. The address of the symbol doesn't matter as long as they are
285
2.69k
  // different in different DSOs, so we chose the start address of the DSO.
286
2.69k
  addOptionalRegular("__dso_handle", Out::elfHeader, 0, STV_HIDDEN);
287
2.69k
288
2.69k
  // If linker script do layout we do not need to create any standart symbols.
289
2.69k
  if (script->hasSectionsCommand)
290
462
    return;
291
2.23k
292
15.6k
  
auto add = [](StringRef s, int64_t pos) 2.23k
{
293
15.6k
    return addOptionalRegular(s, Out::elfHeader, pos, STV_DEFAULT);
294
15.6k
  };
295
2.23k
296
2.23k
  ElfSym::bss = add("__bss_start", 0);
297
2.23k
  ElfSym::end1 = add("end", -1);
298
2.23k
  ElfSym::end2 = add("_end", -1);
299
2.23k
  ElfSym::etext1 = add("etext", -1);
300
2.23k
  ElfSym::etext2 = add("_etext", -1);
301
2.23k
  ElfSym::edata1 = add("edata", -1);
302
2.23k
  ElfSym::edata2 = add("_edata", -1);
303
2.23k
}
304
305
20.3k
static OutputSection *findSection(StringRef name, unsigned partition = 1) {
306
20.3k
  for (BaseCommand *base : script->sectionCommands)
307
2.03M
    if (auto *sec = dyn_cast<OutputSection>(base))
308
2.02M
      if (sec->name == name && 
sec->partition == partition4.07k
)
309
4.06k
        return sec;
310
20.3k
  
return nullptr16.2k
;
311
20.3k
}
312
313
// Initialize Out members.
314
2.75k
template <class ELFT> static void createSyntheticSections() {
315
2.75k
  // Initialize all pointers with NULL. This is needed because
316
2.75k
  // you can call lld::elf::main more than once as a library.
317
2.75k
  memset(&Out::first, 0, sizeof(Out));
318
2.75k
319
36.6k
  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
319
5.00k
  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
319
2.35k
  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
319
27.7k
  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
319
1.55k
  auto add = [](InputSectionBase *sec) { inputSections.push_back(sec); };
320
2.75k
321
2.75k
  in.shStrTab = make<StringTableSection>(".shstrtab", false);
322
2.75k
323
2.75k
  Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
324
2.75k
  Out::programHeaders->alignment = config->wordsize;
325
2.75k
326
2.75k
  if (config->strip != StripPolicy::All) {
327
2.74k
    in.strTab = make<StringTableSection>(".strtab", false);
328
2.74k
    in.symTab = make<SymbolTableSection<ELFT>>(*in.strTab);
329
2.74k
    in.symTabShndx = make<SymtabShndxSection>();
330
2.74k
  }
331
2.75k
332
2.75k
  in.bss = make<BssSection>(".bss", 0, 1);
333
2.75k
  add(in.bss);
334
2.75k
335
2.75k
  // If there is a SECTIONS command and a .data.rel.ro section name use name
336
2.75k
  // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
337
2.75k
  // This makes sure our relro is contiguous.
338
2.75k
  bool hasDataRelRo =
339
2.75k
      script->hasSectionsCommand && 
findSection(".data.rel.ro", 0)463
;
340
2.75k
  in.bssRelRo =
341
2.75k
      make<BssSection>(hasDataRelRo ? 
".data.rel.ro.bss"6
:
".bss.rel.ro"2.74k
, 0, 1);
342
2.75k
  add(in.bssRelRo);
343
2.75k
344
2.75k
  // Add MIPS-specific sections.
345
2.75k
  if (config->emachine == EM_MIPS) {
346
198
    if (!config->shared && 
config->hasDynSymTab119
) {
347
30
      in.mipsRldMap = make<MipsRldMapSection>();
348
30
      add(in.mipsRldMap);
349
30
    }
350
198
    if (auto *sec = MipsAbiFlagsSection<ELFT>::create())
351
192
      add(sec);
352
198
    if (auto *sec = MipsOptionsSection<ELFT>::create())
353
42
      add(sec);
354
198
    if (auto *sec = MipsReginfoSection<ELFT>::create())
355
146
      add(sec);
356
198
  }
357
2.75k
358
2.77k
  for (Partition &part : partitions) {
359
13.5k
    auto add = [&](InputSectionBase *sec) {
360
13.5k
      sec->partition = part.getNumber();
361
13.5k
      inputSections.push_back(sec);
362
13.5k
    };
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)1, false> >()::'lambda0'(lld::elf::InputSectionBase*)::operator()(lld::elf::InputSectionBase*) const
Line
Count
Source
359
1.96k
    auto add = [&](InputSectionBase *sec) {
360
1.96k
      sec->partition = part.getNumber();
361
1.96k
      inputSections.push_back(sec);
362
1.96k
    };
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)0, false> >()::'lambda0'(lld::elf::InputSectionBase*)::operator()(lld::elf::InputSectionBase*) const
Line
Count
Source
359
771
    auto add = [&](InputSectionBase *sec) {
360
771
      sec->partition = part.getNumber();
361
771
      inputSections.push_back(sec);
362
771
    };
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)1, true> >()::'lambda0'(lld::elf::InputSectionBase*)::operator()(lld::elf::InputSectionBase*) const
Line
Count
Source
359
10.3k
    auto add = [&](InputSectionBase *sec) {
360
10.3k
      sec->partition = part.getNumber();
361
10.3k
      inputSections.push_back(sec);
362
10.3k
    };
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)0, true> >()::'lambda0'(lld::elf::InputSectionBase*)::operator()(lld::elf::InputSectionBase*) const
Line
Count
Source
359
527
    auto add = [&](InputSectionBase *sec) {
360
527
      sec->partition = part.getNumber();
361
527
      inputSections.push_back(sec);
362
527
    };
363
2.77k
364
2.77k
    if (!part.name.empty()) {
365
18
      part.elfHeader = make<PartitionElfHeaderSection<ELFT>>();
366
18
      part.elfHeader->name = part.name;
367
18
      add(part.elfHeader);
368
18
369
18
      part.programHeaders = make<PartitionProgramHeadersSection<ELFT>>();
370
18
      add(part.programHeaders);
371
18
    }
372
2.77k
373
2.77k
    if (config->buildId != BuildIdKind::None) {
374
19
      part.buildId = make<BuildIdSection>();
375
19
      add(part.buildId);
376
19
    }
377
2.77k
378
2.77k
    part.dynStrTab = make<StringTableSection>(".dynstr", true);
379
2.77k
    part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
380
2.77k
    part.dynamic = make<DynamicSection<ELFT>>();
381
2.77k
    if (config->androidPackDynRelocs) {
382
7
      part.relaDyn = make<AndroidPackedRelocationSection<ELFT>>(
383
7
          config->isRela ? 
".rela.dyn"6
:
".rel.dyn"1
);
384
2.76k
    } else {
385
2.76k
      part.relaDyn = make<RelocationSection<ELFT>>(
386
2.76k
          config->isRela ? 
".rela.dyn"2.26k
:
".rel.dyn"501
, config->zCombreloc);
387
2.76k
    }
388
2.77k
389
2.77k
    if (needsInterpSection())
390
8
      add(createInterpSection());
391
2.77k
392
2.77k
    if (config->hasDynSymTab) {
393
1.35k
      part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
394
1.35k
      add(part.dynSymTab);
395
1.35k
396
1.35k
      part.verSym = make<VersionTableSection>();
397
1.35k
      add(part.verSym);
398
1.35k
399
1.35k
      if (!config->versionDefinitions.empty()) {
400
61
        part.verDef = make<VersionDefinitionSection>();
401
61
        add(part.verDef);
402
61
      }
403
1.35k
404
1.35k
      part.verNeed = make<VersionNeedSection<ELFT>>();
405
1.35k
      add(part.verNeed);
406
1.35k
407
1.35k
      if (config->gnuHash) {
408
1.11k
        part.gnuHashTab = make<GnuHashTableSection>();
409
1.11k
        add(part.gnuHashTab);
410
1.11k
      }
411
1.35k
412
1.35k
      if (config->sysvHash) {
413
1.34k
        part.hashTab = make<HashTableSection>();
414
1.34k
        add(part.hashTab);
415
1.34k
      }
416
1.35k
417
1.35k
      add(part.dynamic);
418
1.35k
      add(part.dynStrTab);
419
1.35k
      add(part.relaDyn);
420
1.35k
    }
421
2.77k
422
2.77k
    if (config->relrPackDynRelocs) {
423
6
      part.relrDyn = make<RelrSection<ELFT>>();
424
6
      add(part.relrDyn);
425
6
    }
426
2.77k
427
2.77k
    if (!config->relocatable) {
428
2.68k
      if (config->ehFrameHdr) {
429
32
        part.ehFrameHdr = make<EhFrameHeader>();
430
32
        add(part.ehFrameHdr);
431
32
      }
432
2.68k
      part.ehFrame = make<EhFrameSection>();
433
2.68k
      add(part.ehFrame);
434
2.68k
    }
435
2.77k
436
2.77k
    if (config->emachine == EM_ARM && 
!config->relocatable162
) {
437
157
      // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
438
157
      // InputSections.
439
157
      part.armExidx = make<ARMExidxSyntheticSection>();
440
157
      add(part.armExidx);
441
157
    }
442
2.77k
  }
443
2.75k
444
2.75k
  if (partitions.size() != 1) {
445
15
    // Create the partition end marker. This needs to be in partition number 255
446
15
    // so that it is sorted after all other partitions. It also has other
447
15
    // special handling (see createPhdrs() and combineEhSections()).
448
15
    in.partEnd = make<BssSection>(".part.end", config->maxPageSize, 1);
449
15
    in.partEnd->partition = 255;
450
15
    add(in.partEnd);
451
15
452
15
    in.partIndex = make<PartitionIndexSection>();
453
15
    addOptionalRegular("__part_index_begin", in.partIndex, 0);
454
15
    addOptionalRegular("__part_index_end", in.partIndex,
455
15
                       in.partIndex->getSize());
456
15
    add(in.partIndex);
457
15
  }
458
2.75k
459
2.75k
  // Add .got. MIPS' .got is so different from the other archs,
460
2.75k
  // it has its own class.
461
2.75k
  if (config->emachine == EM_MIPS) {
462
198
    in.mipsGot = make<MipsGotSection>();
463
198
    add(in.mipsGot);
464
2.55k
  } else {
465
2.55k
    in.got = make<GotSection>();
466
2.55k
    add(in.got);
467
2.55k
  }
468
2.75k
469
2.75k
  if (config->emachine == EM_PPC) {
470
31
    in.ppc32Got2 = make<PPC32Got2Section>();
471
31
    add(in.ppc32Got2);
472
31
  }
473
2.75k
474
2.75k
  if (config->emachine == EM_PPC64) {
475
152
    in.ppc64LongBranchTarget = make<PPC64LongBranchTargetSection>();
476
152
    add(in.ppc64LongBranchTarget);
477
152
  }
478
2.75k
479
2.75k
  if (config->emachine == EM_RISCV) {
480
90
    in.riscvSdata = make<RISCVSdataSection>();
481
90
    add(in.riscvSdata);
482
90
  }
483
2.75k
484
2.75k
  in.gotPlt = make<GotPltSection>();
485
2.75k
  add(in.gotPlt);
486
2.75k
  in.igotPlt = make<IgotPltSection>();
487
2.75k
  add(in.igotPlt);
488
2.75k
489
2.75k
  // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
490
2.75k
  // it as a relocation and ensure the referenced section is created.
491
2.75k
  if (ElfSym::globalOffsetTable && 
config->emachine != EM_MIPS119
) {
492
118
    if (target->gotBaseSymInGotPlt)
493
31
      in.gotPlt->hasGotPltOffRel = true;
494
87
    else
495
87
      in.got->hasGotOffRel = true;
496
118
  }
497
2.75k
498
2.75k
  if (config->gdbIndex)
499
19
    add(GdbIndexSection::create<ELFT>());
500
2.75k
501
2.75k
  // We always need to add rel[a].plt to output if it has entries.
502
2.75k
  // Even for static linking it can contain R_[*]_IRELATIVE relocations.
503
2.75k
  in.relaPlt = make<RelocationSection<ELFT>>(
504
2.75k
      config->isRela ? 
".rela.plt"2.25k
:
".rel.plt"498
, /*sort=*/false);
505
2.75k
  add(in.relaPlt);
506
2.75k
507
2.75k
  // The relaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
508
2.75k
  // that the IRelative relocations are processed last by the dynamic loader.
509
2.75k
  // We cannot place the iplt section in .rel.dyn when Android relocation
510
2.75k
  // packing is enabled because that would cause a section type mismatch.
511
2.75k
  // However, because the Android dynamic loader reads .rel.plt after .rel.dyn,
512
2.75k
  // we can get the desired behaviour by placing the iplt section in .rel.plt.
513
2.75k
  in.relaIplt = make<RelocationSection<ELFT>>(
514
2.75k
      (config->emachine == EM_ARM && 
!config->androidPackDynRelocs159
)
515
2.75k
          ? 
".rel.dyn"158
516
2.75k
          : 
in.relaPlt->name2.59k
,
517
2.75k
      /*sort=*/false);
518
2.75k
  add(in.relaIplt);
519
2.75k
520
2.75k
  in.plt = make<PltSection>(false);
521
2.75k
  add(in.plt);
522
2.75k
  in.iplt = make<PltSection>(true);
523
2.75k
  add(in.iplt);
524
2.75k
525
2.75k
  if (config->andFeatures)
526
24
    add(make<GnuPropertySection>());
527
2.75k
528
2.75k
  // .note.GNU-stack is always added when we are creating a re-linkable
529
2.75k
  // object file. Other linkers are using the presence of this marker
530
2.75k
  // section to control the executable-ness of the stack area, but that
531
2.75k
  // is irrelevant these days. Stack area should always be non-executable
532
2.75k
  // by default. So we emit this section unconditionally.
533
2.75k
  if (config->relocatable)
534
87
    add(make<GnuStackSection>());
535
2.75k
536
2.75k
  if (in.symTab)
537
2.74k
    add(in.symTab);
538
2.75k
  if (in.symTabShndx)
539
2.74k
    add(in.symTabShndx);
540
2.75k
  add(in.shStrTab);
541
2.75k
  if (in.strTab)
542
2.74k
    add(in.strTab);
543
2.75k
}
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
314
376
template <class ELFT> static void createSyntheticSections() {
315
376
  // Initialize all pointers with NULL. This is needed because
316
376
  // you can call lld::elf::main more than once as a library.
317
376
  memset(&Out::first, 0, sizeof(Out));
318
376
319
376
  auto add = [](InputSectionBase *sec) { inputSections.push_back(sec); };
320
376
321
376
  in.shStrTab = make<StringTableSection>(".shstrtab", false);
322
376
323
376
  Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
324
376
  Out::programHeaders->alignment = config->wordsize;
325
376
326
376
  if (config->strip != StripPolicy::All) {
327
376
    in.strTab = make<StringTableSection>(".strtab", false);
328
376
    in.symTab = make<SymbolTableSection<ELFT>>(*in.strTab);
329
376
    in.symTabShndx = make<SymtabShndxSection>();
330
376
  }
331
376
332
376
  in.bss = make<BssSection>(".bss", 0, 1);
333
376
  add(in.bss);
334
376
335
376
  // If there is a SECTIONS command and a .data.rel.ro section name use name
336
376
  // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
337
376
  // This makes sure our relro is contiguous.
338
376
  bool hasDataRelRo =
339
376
      script->hasSectionsCommand && 
findSection(".data.rel.ro", 0)43
;
340
376
  in.bssRelRo =
341
376
      make<BssSection>(hasDataRelRo ? 
".data.rel.ro.bss"0
: ".bss.rel.ro", 0, 1);
342
376
  add(in.bssRelRo);
343
376
344
376
  // Add MIPS-specific sections.
345
376
  if (config->emachine == EM_MIPS) {
346
24
    if (!config->shared && 
config->hasDynSymTab18
) {
347
3
      in.mipsRldMap = make<MipsRldMapSection>();
348
3
      add(in.mipsRldMap);
349
3
    }
350
24
    if (auto *sec = MipsAbiFlagsSection<ELFT>::create())
351
23
      add(sec);
352
24
    if (auto *sec = MipsOptionsSection<ELFT>::create())
353
0
      add(sec);
354
24
    if (auto *sec = MipsReginfoSection<ELFT>::create())
355
23
      add(sec);
356
24
  }
357
376
358
380
  for (Partition &part : partitions) {
359
380
    auto add = [&](InputSectionBase *sec) {
360
380
      sec->partition = part.getNumber();
361
380
      inputSections.push_back(sec);
362
380
    };
363
380
364
380
    if (!part.name.empty()) {
365
4
      part.elfHeader = make<PartitionElfHeaderSection<ELFT>>();
366
4
      part.elfHeader->name = part.name;
367
4
      add(part.elfHeader);
368
4
369
4
      part.programHeaders = make<PartitionProgramHeadersSection<ELFT>>();
370
4
      add(part.programHeaders);
371
4
    }
372
380
373
380
    if (config->buildId != BuildIdKind::None) {
374
0
      part.buildId = make<BuildIdSection>();
375
0
      add(part.buildId);
376
0
    }
377
380
378
380
    part.dynStrTab = make<StringTableSection>(".dynstr", true);
379
380
    part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
380
380
    part.dynamic = make<DynamicSection<ELFT>>();
381
380
    if (config->androidPackDynRelocs) {
382
1
      part.relaDyn = make<AndroidPackedRelocationSection<ELFT>>(
383
1
          config->isRela ? 
".rela.dyn"0
: ".rel.dyn");
384
379
    } else {
385
379
      part.relaDyn = make<RelocationSection<ELFT>>(
386
379
          config->isRela ? 
".rela.dyn"53
:
".rel.dyn"326
, config->zCombreloc);
387
379
    }
388
380
389
380
    if (needsInterpSection())
390
1
      add(createInterpSection());
391
380
392
380
    if (config->hasDynSymTab) {
393
185
      part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
394
185
      add(part.dynSymTab);
395
185
396
185
      part.verSym = make<VersionTableSection>();
397
185
      add(part.verSym);
398
185
399
185
      if (!config->versionDefinitions.empty()) {
400
0
        part.verDef = make<VersionDefinitionSection>();
401
0
        add(part.verDef);
402
0
      }
403
185
404
185
      part.verNeed = make<VersionNeedSection<ELFT>>();
405
185
      add(part.verNeed);
406
185
407
185
      if (config->gnuHash) {
408
139
        part.gnuHashTab = make<GnuHashTableSection>();
409
139
        add(part.gnuHashTab);
410
139
      }
411
185
412
185
      if (config->sysvHash) {
413
183
        part.hashTab = make<HashTableSection>();
414
183
        add(part.hashTab);
415
183
      }
416
185
417
185
      add(part.dynamic);
418
185
      add(part.dynStrTab);
419
185
      add(part.relaDyn);
420
185
    }
421
380
422
380
    if (config->relrPackDynRelocs) {
423
2
      part.relrDyn = make<RelrSection<ELFT>>();
424
2
      add(part.relrDyn);
425
2
    }
426
380
427
380
    if (!config->relocatable) {
428
369
      if (config->ehFrameHdr) {
429
0
        part.ehFrameHdr = make<EhFrameHeader>();
430
0
        add(part.ehFrameHdr);
431
0
      }
432
369
      part.ehFrame = make<EhFrameSection>();
433
369
      add(part.ehFrame);
434
369
    }
435
380
436
380
    if (config->emachine == EM_ARM && 
!config->relocatable162
) {
437
157
      // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
438
157
      // InputSections.
439
157
      part.armExidx = make<ARMExidxSyntheticSection>();
440
157
      add(part.armExidx);
441
157
    }
442
380
  }
443
376
444
376
  if (partitions.size() != 1) {
445
3
    // Create the partition end marker. This needs to be in partition number 255
446
3
    // so that it is sorted after all other partitions. It also has other
447
3
    // special handling (see createPhdrs() and combineEhSections()).
448
3
    in.partEnd = make<BssSection>(".part.end", config->maxPageSize, 1);
449
3
    in.partEnd->partition = 255;
450
3
    add(in.partEnd);
451
3
452
3
    in.partIndex = make<PartitionIndexSection>();
453
3
    addOptionalRegular("__part_index_begin", in.partIndex, 0);
454
3
    addOptionalRegular("__part_index_end", in.partIndex,
455
3
                       in.partIndex->getSize());
456
3
    add(in.partIndex);
457
3
  }
458
376
459
376
  // Add .got. MIPS' .got is so different from the other archs,
460
376
  // it has its own class.
461
376
  if (config->emachine == EM_MIPS) {
462
24
    in.mipsGot = make<MipsGotSection>();
463
24
    add(in.mipsGot);
464
352
  } else {
465
352
    in.got = make<GotSection>();
466
352
    add(in.got);
467
352
  }
468
376
469
376
  if (config->emachine == EM_PPC) {
470
0
    in.ppc32Got2 = make<PPC32Got2Section>();
471
0
    add(in.ppc32Got2);
472
0
  }
473
376
474
376
  if (config->emachine == EM_PPC64) {
475
0
    in.ppc64LongBranchTarget = make<PPC64LongBranchTargetSection>();
476
0
    add(in.ppc64LongBranchTarget);
477
0
  }
478
376
479
376
  if (config->emachine == EM_RISCV) {
480
44
    in.riscvSdata = make<RISCVSdataSection>();
481
44
    add(in.riscvSdata);
482
44
  }
483
376
484
376
  in.gotPlt = make<GotPltSection>();
485
376
  add(in.gotPlt);
486
376
  in.igotPlt = make<IgotPltSection>();
487
376
  add(in.igotPlt);
488
376
489
376
  // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
490
376
  // it as a relocation and ensure the referenced section is created.
491
376
  if (ElfSym::globalOffsetTable && 
config->emachine != EM_MIPS13
) {
492
13
    if (target->gotBaseSymInGotPlt)
493
8
      in.gotPlt->hasGotPltOffRel = true;
494
5
    else
495
5
      in.got->hasGotOffRel = true;
496
13
  }
497
376
498
376
  if (config->gdbIndex)
499
1
    add(GdbIndexSection::create<ELFT>());
500
376
501
376
  // We always need to add rel[a].plt to output if it has entries.
502
376
  // Even for static linking it can contain R_[*]_IRELATIVE relocations.
503
376
  in.relaPlt = make<RelocationSection<ELFT>>(
504
376
      config->isRela ? 
".rela.plt"53
:
".rel.plt"323
, /*sort=*/false);
505
376
  add(in.relaPlt);
506
376
507
376
  // The relaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
508
376
  // that the IRelative relocations are processed last by the dynamic loader.
509
376
  // We cannot place the iplt section in .rel.dyn when Android relocation
510
376
  // packing is enabled because that would cause a section type mismatch.
511
376
  // However, because the Android dynamic loader reads .rel.plt after .rel.dyn,
512
376
  // we can get the desired behaviour by placing the iplt section in .rel.plt.
513
376
  in.relaIplt = make<RelocationSection<ELFT>>(
514
376
      (config->emachine == EM_ARM && 
!config->androidPackDynRelocs159
)
515
376
          ? 
".rel.dyn"158
516
376
          : 
in.relaPlt->name218
,
517
376
      /*sort=*/false);
518
376
  add(in.relaIplt);
519
376
520
376
  in.plt = make<PltSection>(false);
521
376
  add(in.plt);
522
376
  in.iplt = make<PltSection>(true);
523
376
  add(in.iplt);
524
376
525
376
  if (config->andFeatures)
526
3
    add(make<GnuPropertySection>());
527
376
528
376
  // .note.GNU-stack is always added when we are creating a re-linkable
529
376
  // object file. Other linkers are using the presence of this marker
530
376
  // section to control the executable-ness of the stack area, but that
531
376
  // is irrelevant these days. Stack area should always be non-executable
532
376
  // by default. So we emit this section unconditionally.
533
376
  if (config->relocatable)
534
11
    add(make<GnuStackSection>());
535
376
536
376
  if (in.symTab)
537
376
    add(in.symTab);
538
376
  if (in.symTabShndx)
539
376
    add(in.symTabShndx);
540
376
  add(in.shStrTab);
541
376
  if (in.strTab)
542
376
    add(in.strTab);
543
376
}
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
314
157
template <class ELFT> static void createSyntheticSections() {
315
157
  // Initialize all pointers with NULL. This is needed because
316
157
  // you can call lld::elf::main more than once as a library.
317
157
  memset(&Out::first, 0, sizeof(Out));
318
157
319
157
  auto add = [](InputSectionBase *sec) { inputSections.push_back(sec); };
320
157
321
157
  in.shStrTab = make<StringTableSection>(".shstrtab", false);
322
157
323
157
  Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
324
157
  Out::programHeaders->alignment = config->wordsize;
325
157
326
157
  if (config->strip != StripPolicy::All) {
327
157
    in.strTab = make<StringTableSection>(".strtab", false);
328
157
    in.symTab = make<SymbolTableSection<ELFT>>(*in.strTab);
329
157
    in.symTabShndx = make<SymtabShndxSection>();
330
157
  }
331
157
332
157
  in.bss = make<BssSection>(".bss", 0, 1);
333
157
  add(in.bss);
334
157
335
157
  // If there is a SECTIONS command and a .data.rel.ro section name use name
336
157
  // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
337
157
  // This makes sure our relro is contiguous.
338
157
  bool hasDataRelRo =
339
157
      script->hasSectionsCommand && 
findSection(".data.rel.ro", 0)9
;
340
157
  in.bssRelRo =
341
157
      make<BssSection>(hasDataRelRo ? 
".data.rel.ro.bss"0
: ".bss.rel.ro", 0, 1);
342
157
  add(in.bssRelRo);
343
157
344
157
  // Add MIPS-specific sections.
345
157
  if (config->emachine == EM_MIPS) {
346
126
    if (!config->shared && 
config->hasDynSymTab76
) {
347
21
      in.mipsRldMap = make<MipsRldMapSection>();
348
21
      add(in.mipsRldMap);
349
21
    }
350
126
    if (auto *sec = MipsAbiFlagsSection<ELFT>::create())
351
123
      add(sec);
352
126
    if (auto *sec = MipsOptionsSection<ELFT>::create())
353
0
      add(sec);
354
126
    if (auto *sec = MipsReginfoSection<ELFT>::create())
355
123
      add(sec);
356
126
  }
357
157
358
157
  for (Partition &part : partitions) {
359
157
    auto add = [&](InputSectionBase *sec) {
360
157
      sec->partition = part.getNumber();
361
157
      inputSections.push_back(sec);
362
157
    };
363
157
364
157
    if (!part.name.empty()) {
365
0
      part.elfHeader = make<PartitionElfHeaderSection<ELFT>>();
366
0
      part.elfHeader->name = part.name;
367
0
      add(part.elfHeader);
368
0
369
0
      part.programHeaders = make<PartitionProgramHeadersSection<ELFT>>();
370
0
      add(part.programHeaders);
371
0
    }
372
157
373
157
    if (config->buildId != BuildIdKind::None) {
374
0
      part.buildId = make<BuildIdSection>();
375
0
      add(part.buildId);
376
0
    }
377
157
378
157
    part.dynStrTab = make<StringTableSection>(".dynstr", true);
379
157
    part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
380
157
    part.dynamic = make<DynamicSection<ELFT>>();
381
157
    if (config->androidPackDynRelocs) {
382
0
      part.relaDyn = make<AndroidPackedRelocationSection<ELFT>>(
383
0
          config->isRela ? ".rela.dyn" : ".rel.dyn");
384
157
    } else {
385
157
      part.relaDyn = make<RelocationSection<ELFT>>(
386
157
          config->isRela ? 
".rela.dyn"31
:
".rel.dyn"126
, config->zCombreloc);
387
157
    }
388
157
389
157
    if (needsInterpSection())
390
0
      add(createInterpSection());
391
157
392
157
    if (config->hasDynSymTab) {
393
87
      part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
394
87
      add(part.dynSymTab);
395
87
396
87
      part.verSym = make<VersionTableSection>();
397
87
      add(part.verSym);
398
87
399
87
      if (!config->versionDefinitions.empty()) {
400
0
        part.verDef = make<VersionDefinitionSection>();
401
0
        add(part.verDef);
402
0
      }
403
87
404
87
      part.verNeed = make<VersionNeedSection<ELFT>>();
405
87
      add(part.verNeed);
406
87
407
87
      if (config->gnuHash) {
408
16
        part.gnuHashTab = make<GnuHashTableSection>();
409
16
        add(part.gnuHashTab);
410
16
      }
411
87
412
87
      if (config->sysvHash) {
413
87
        part.hashTab = make<HashTableSection>();
414
87
        add(part.hashTab);
415
87
      }
416
87
417
87
      add(part.dynamic);
418
87
      add(part.dynStrTab);
419
87
      add(part.relaDyn);
420
87
    }
421
157
422
157
    if (config->relrPackDynRelocs) {
423
0
      part.relrDyn = make<RelrSection<ELFT>>();
424
0
      add(part.relrDyn);
425
0
    }
426
157
427
157
    if (!config->relocatable) {
428
146
      if (config->ehFrameHdr) {
429
0
        part.ehFrameHdr = make<EhFrameHeader>();
430
0
        add(part.ehFrameHdr);
431
0
      }
432
146
      part.ehFrame = make<EhFrameSection>();
433
146
      add(part.ehFrame);
434
146
    }
435
157
436
157
    if (config->emachine == EM_ARM && 
!config->relocatable0
) {
437
0
      // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
438
0
      // InputSections.
439
0
      part.armExidx = make<ARMExidxSyntheticSection>();
440
0
      add(part.armExidx);
441
0
    }
442
157
  }
443
157
444
157
  if (partitions.size() != 1) {
445
0
    // Create the partition end marker. This needs to be in partition number 255
446
0
    // so that it is sorted after all other partitions. It also has other
447
0
    // special handling (see createPhdrs() and combineEhSections()).
448
0
    in.partEnd = make<BssSection>(".part.end", config->maxPageSize, 1);
449
0
    in.partEnd->partition = 255;
450
0
    add(in.partEnd);
451
0
452
0
    in.partIndex = make<PartitionIndexSection>();
453
0
    addOptionalRegular("__part_index_begin", in.partIndex, 0);
454
0
    addOptionalRegular("__part_index_end", in.partIndex,
455
0
                       in.partIndex->getSize());
456
0
    add(in.partIndex);
457
0
  }
458
157
459
157
  // Add .got. MIPS' .got is so different from the other archs,
460
157
  // it has its own class.
461
157
  if (config->emachine == EM_MIPS) {
462
126
    in.mipsGot = make<MipsGotSection>();
463
126
    add(in.mipsGot);
464
126
  } else {
465
31
    in.got = make<GotSection>();
466
31
    add(in.got);
467
31
  }
468
157
469
157
  if (config->emachine == EM_PPC) {
470
31
    in.ppc32Got2 = make<PPC32Got2Section>();
471
31
    add(in.ppc32Got2);
472
31
  }
473
157
474
157
  if (config->emachine == EM_PPC64) {
475
0
    in.ppc64LongBranchTarget = make<PPC64LongBranchTargetSection>();
476
0
    add(in.ppc64LongBranchTarget);
477
0
  }
478
157
479
157
  if (config->emachine == EM_RISCV) {
480
0
    in.riscvSdata = make<RISCVSdataSection>();
481
0
    add(in.riscvSdata);
482
0
  }
483
157
484
157
  in.gotPlt = make<GotPltSection>();
485
157
  add(in.gotPlt);
486
157
  in.igotPlt = make<IgotPltSection>();
487
157
  add(in.igotPlt);
488
157
489
157
  // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
490
157
  // it as a relocation and ensure the referenced section is created.
491
157
  if (ElfSym::globalOffsetTable && 
config->emachine != EM_MIPS4
) {
492
4
    if (target->gotBaseSymInGotPlt)
493
0
      in.gotPlt->hasGotPltOffRel = true;
494
4
    else
495
4
      in.got->hasGotOffRel = true;
496
4
  }
497
157
498
157
  if (config->gdbIndex)
499
0
    add(GdbIndexSection::create<ELFT>());
500
157
501
157
  // We always need to add rel[a].plt to output if it has entries.
502
157
  // Even for static linking it can contain R_[*]_IRELATIVE relocations.
503
157
  in.relaPlt = make<RelocationSection<ELFT>>(
504
157
      config->isRela ? 
".rela.plt"31
:
".rel.plt"126
, /*sort=*/false);
505
157
  add(in.relaPlt);
506
157
507
157
  // The relaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
508
157
  // that the IRelative relocations are processed last by the dynamic loader.
509
157
  // We cannot place the iplt section in .rel.dyn when Android relocation
510
157
  // packing is enabled because that would cause a section type mismatch.
511
157
  // However, because the Android dynamic loader reads .rel.plt after .rel.dyn,
512
157
  // we can get the desired behaviour by placing the iplt section in .rel.plt.
513
157
  in.relaIplt = make<RelocationSection<ELFT>>(
514
157
      (config->emachine == EM_ARM && 
!config->androidPackDynRelocs0
)
515
157
          ? 
".rel.dyn"0
516
157
          : in.relaPlt->name,
517
157
      /*sort=*/false);
518
157
  add(in.relaIplt);
519
157
520
157
  in.plt = make<PltSection>(false);
521
157
  add(in.plt);
522
157
  in.iplt = make<PltSection>(true);
523
157
  add(in.iplt);
524
157
525
157
  if (config->andFeatures)
526
0
    add(make<GnuPropertySection>());
527
157
528
157
  // .note.GNU-stack is always added when we are creating a re-linkable
529
157
  // object file. Other linkers are using the presence of this marker
530
157
  // section to control the executable-ness of the stack area, but that
531
157
  // is irrelevant these days. Stack area should always be non-executable
532
157
  // by default. So we emit this section unconditionally.
533
157
  if (config->relocatable)
534
11
    add(make<GnuStackSection>());
535
157
536
157
  if (in.symTab)
537
157
    add(in.symTab);
538
157
  if (in.symTabShndx)
539
157
    add(in.symTabShndx);
540
157
  add(in.shStrTab);
541
157
  if (in.strTab)
542
157
    add(in.strTab);
543
157
}
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
314
2.11k
template <class ELFT> static void createSyntheticSections() {
315
2.11k
  // Initialize all pointers with NULL. This is needed because
316
2.11k
  // you can call lld::elf::main more than once as a library.
317
2.11k
  memset(&Out::first, 0, sizeof(Out));
318
2.11k
319
2.11k
  auto add = [](InputSectionBase *sec) { inputSections.push_back(sec); };
320
2.11k
321
2.11k
  in.shStrTab = make<StringTableSection>(".shstrtab", false);
322
2.11k
323
2.11k
  Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
324
2.11k
  Out::programHeaders->alignment = config->wordsize;
325
2.11k
326
2.11k
  if (config->strip != StripPolicy::All) {
327
2.10k
    in.strTab = make<StringTableSection>(".strtab", false);
328
2.10k
    in.symTab = make<SymbolTableSection<ELFT>>(*in.strTab);
329
2.10k
    in.symTabShndx = make<SymtabShndxSection>();
330
2.10k
  }
331
2.11k
332
2.11k
  in.bss = make<BssSection>(".bss", 0, 1);
333
2.11k
  add(in.bss);
334
2.11k
335
2.11k
  // If there is a SECTIONS command and a .data.rel.ro section name use name
336
2.11k
  // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
337
2.11k
  // This makes sure our relro is contiguous.
338
2.11k
  bool hasDataRelRo =
339
2.11k
      script->hasSectionsCommand && 
findSection(".data.rel.ro", 0)407
;
340
2.11k
  in.bssRelRo =
341
2.11k
      make<BssSection>(hasDataRelRo ? 
".data.rel.ro.bss"6
:
".bss.rel.ro"2.10k
, 0, 1);
342
2.11k
  add(in.bssRelRo);
343
2.11k
344
2.11k
  // Add MIPS-specific sections.
345
2.11k
  if (config->emachine == EM_MIPS) {
346
3
    if (!config->shared && config->hasDynSymTab) {
347
0
      in.mipsRldMap = make<MipsRldMapSection>();
348
0
      add(in.mipsRldMap);
349
0
    }
350
3
    if (auto *sec = MipsAbiFlagsSection<ELFT>::create())
351
3
      add(sec);
352
3
    if (auto *sec = MipsOptionsSection<ELFT>::create())
353
3
      add(sec);
354
3
    if (auto *sec = MipsReginfoSection<ELFT>::create())
355
0
      add(sec);
356
3
  }
357
2.11k
358
2.12k
  for (Partition &part : partitions) {
359
2.12k
    auto add = [&](InputSectionBase *sec) {
360
2.12k
      sec->partition = part.getNumber();
361
2.12k
      inputSections.push_back(sec);
362
2.12k
    };
363
2.12k
364
2.12k
    if (!part.name.empty()) {
365
14
      part.elfHeader = make<PartitionElfHeaderSection<ELFT>>();
366
14
      part.elfHeader->name = part.name;
367
14
      add(part.elfHeader);
368
14
369
14
      part.programHeaders = make<PartitionProgramHeadersSection<ELFT>>();
370
14
      add(part.programHeaders);
371
14
    }
372
2.12k
373
2.12k
    if (config->buildId != BuildIdKind::None) {
374
19
      part.buildId = make<BuildIdSection>();
375
19
      add(part.buildId);
376
19
    }
377
2.12k
378
2.12k
    part.dynStrTab = make<StringTableSection>(".dynstr", true);
379
2.12k
    part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
380
2.12k
    part.dynamic = make<DynamicSection<ELFT>>();
381
2.12k
    if (config->androidPackDynRelocs) {
382
6
      part.relaDyn = make<AndroidPackedRelocationSection<ELFT>>(
383
6
          config->isRela ? ".rela.dyn" : 
".rel.dyn"0
);
384
2.12k
    } else {
385
2.12k
      part.relaDyn = make<RelocationSection<ELFT>>(
386
2.12k
          config->isRela ? 
".rela.dyn"2.11k
:
".rel.dyn"3
, config->zCombreloc);
387
2.12k
    }
388
2.12k
389
2.12k
    if (needsInterpSection())
390
6
      add(createInterpSection());
391
2.12k
392
2.12k
    if (config->hasDynSymTab) {
393
1.02k
      part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
394
1.02k
      add(part.dynSymTab);
395
1.02k
396
1.02k
      part.verSym = make<VersionTableSection>();
397
1.02k
      add(part.verSym);
398
1.02k
399
1.02k
      if (!config->versionDefinitions.empty()) {
400
61
        part.verDef = make<VersionDefinitionSection>();
401
61
        add(part.verDef);
402
61
      }
403
1.02k
404
1.02k
      part.verNeed = make<VersionNeedSection<ELFT>>();
405
1.02k
      add(part.verNeed);
406
1.02k
407
1.02k
      if (config->gnuHash) {
408
929
        part.gnuHashTab = make<GnuHashTableSection>();
409
929
        add(part.gnuHashTab);
410
929
      }
411
1.02k
412
1.02k
      if (config->sysvHash) {
413
1.02k
        part.hashTab = make<HashTableSection>();
414
1.02k
        add(part.hashTab);
415
1.02k
      }
416
1.02k
417
1.02k
      add(part.dynamic);
418
1.02k
      add(part.dynStrTab);
419
1.02k
      add(part.relaDyn);
420
1.02k
    }
421
2.12k
422
2.12k
    if (config->relrPackDynRelocs) {
423
4
      part.relrDyn = make<RelrSection<ELFT>>();
424
4
      add(part.relrDyn);
425
4
    }
426
2.12k
427
2.12k
    if (!config->relocatable) {
428
2.06k
      if (config->ehFrameHdr) {
429
29
        part.ehFrameHdr = make<EhFrameHeader>();
430
29
        add(part.ehFrameHdr);
431
29
      }
432
2.06k
      part.ehFrame = make<EhFrameSection>();
433
2.06k
      add(part.ehFrame);
434
2.06k
    }
435
2.12k
436
2.12k
    if (config->emachine == EM_ARM && 
!config->relocatable0
) {
437
0
      // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
438
0
      // InputSections.
439
0
      part.armExidx = make<ARMExidxSyntheticSection>();
440
0
      add(part.armExidx);
441
0
    }
442
2.12k
  }
443
2.11k
444
2.11k
  if (partitions.size() != 1) {
445
12
    // Create the partition end marker. This needs to be in partition number 255
446
12
    // so that it is sorted after all other partitions. It also has other
447
12
    // special handling (see createPhdrs() and combineEhSections()).
448
12
    in.partEnd = make<BssSection>(".part.end", config->maxPageSize, 1);
449
12
    in.partEnd->partition = 255;
450
12
    add(in.partEnd);
451
12
452
12
    in.partIndex = make<PartitionIndexSection>();
453
12
    addOptionalRegular("__part_index_begin", in.partIndex, 0);
454
12
    addOptionalRegular("__part_index_end", in.partIndex,
455
12
                       in.partIndex->getSize());
456
12
    add(in.partIndex);
457
12
  }
458
2.11k
459
2.11k
  // Add .got. MIPS' .got is so different from the other archs,
460
2.11k
  // it has its own class.
461
2.11k
  if (config->emachine == EM_MIPS) {
462
3
    in.mipsGot = make<MipsGotSection>();
463
3
    add(in.mipsGot);
464
2.10k
  } else {
465
2.10k
    in.got = make<GotSection>();
466
2.10k
    add(in.got);
467
2.10k
  }
468
2.11k
469
2.11k
  if (config->emachine == EM_PPC) {
470
0
    in.ppc32Got2 = make<PPC32Got2Section>();
471
0
    add(in.ppc32Got2);
472
0
  }
473
2.11k
474
2.11k
  if (config->emachine == EM_PPC64) {
475
90
    in.ppc64LongBranchTarget = make<PPC64LongBranchTargetSection>();
476
90
    add(in.ppc64LongBranchTarget);
477
90
  }
478
2.11k
479
2.11k
  if (config->emachine == EM_RISCV) {
480
46
    in.riscvSdata = make<RISCVSdataSection>();
481
46
    add(in.riscvSdata);
482
46
  }
483
2.11k
484
2.11k
  in.gotPlt = make<GotPltSection>();
485
2.11k
  add(in.gotPlt);
486
2.11k
  in.igotPlt = make<IgotPltSection>();
487
2.11k
  add(in.igotPlt);
488
2.11k
489
2.11k
  // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
490
2.11k
  // it as a relocation and ensure the referenced section is created.
491
2.11k
  if (ElfSym::globalOffsetTable && 
config->emachine != EM_MIPS70
) {
492
70
    if (target->gotBaseSymInGotPlt)
493
23
      in.gotPlt->hasGotPltOffRel = true;
494
47
    else
495
47
      in.got->hasGotOffRel = true;
496
70
  }
497
2.11k
498
2.11k
  if (config->gdbIndex)
499
18
    add(GdbIndexSection::create<ELFT>());
500
2.11k
501
2.11k
  // We always need to add rel[a].plt to output if it has entries.
502
2.11k
  // Even for static linking it can contain R_[*]_IRELATIVE relocations.
503
2.11k
  in.relaPlt = make<RelocationSection<ELFT>>(
504
2.11k
      config->isRela ? 
".rela.plt"2.10k
:
".rel.plt"3
, /*sort=*/false);
505
2.11k
  add(in.relaPlt);
506
2.11k
507
2.11k
  // The relaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
508
2.11k
  // that the IRelative relocations are processed last by the dynamic loader.
509
2.11k
  // We cannot place the iplt section in .rel.dyn when Android relocation
510
2.11k
  // packing is enabled because that would cause a section type mismatch.
511
2.11k
  // However, because the Android dynamic loader reads .rel.plt after .rel.dyn,
512
2.11k
  // we can get the desired behaviour by placing the iplt section in .rel.plt.
513
2.11k
  in.relaIplt = make<RelocationSection<ELFT>>(
514
2.11k
      (config->emachine == EM_ARM && 
!config->androidPackDynRelocs0
)
515
2.11k
          ? 
".rel.dyn"0
516
2.11k
          : in.relaPlt->name,
517
2.11k
      /*sort=*/false);
518
2.11k
  add(in.relaIplt);
519
2.11k
520
2.11k
  in.plt = make<PltSection>(false);
521
2.11k
  add(in.plt);
522
2.11k
  in.iplt = make<PltSection>(true);
523
2.11k
  add(in.iplt);
524
2.11k
525
2.11k
  if (config->andFeatures)
526
21
    add(make<GnuPropertySection>());
527
2.11k
528
2.11k
  // .note.GNU-stack is always added when we are creating a re-linkable
529
2.11k
  // object file. Other linkers are using the presence of this marker
530
2.11k
  // section to control the executable-ness of the stack area, but that
531
2.11k
  // is irrelevant these days. Stack area should always be non-executable
532
2.11k
  // by default. So we emit this section unconditionally.
533
2.11k
  if (config->relocatable)
534
62
    add(make<GnuStackSection>());
535
2.11k
536
2.11k
  if (in.symTab)
537
2.10k
    add(in.symTab);
538
2.11k
  if (in.symTabShndx)
539
2.10k
    add(in.symTabShndx);
540
2.11k
  add(in.shStrTab);
541
2.11k
  if (in.strTab)
542
2.10k
    add(in.strTab);
543
2.11k
}
Writer.cpp:void createSyntheticSections<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
314
108
template <class ELFT> static void createSyntheticSections() {
315
108
  // Initialize all pointers with NULL. This is needed because
316
108
  // you can call lld::elf::main more than once as a library.
317
108
  memset(&Out::first, 0, sizeof(Out));
318
108
319
108
  auto add = [](InputSectionBase *sec) { inputSections.push_back(sec); };
320
108
321
108
  in.shStrTab = make<StringTableSection>(".shstrtab", false);
322
108
323
108
  Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
324
108
  Out::programHeaders->alignment = config->wordsize;
325
108
326
108
  if (config->strip != StripPolicy::All) {
327
108
    in.strTab = make<StringTableSection>(".strtab", false);
328
108
    in.symTab = make<SymbolTableSection<ELFT>>(*in.strTab);
329
108
    in.symTabShndx = make<SymtabShndxSection>();
330
108
  }
331
108
332
108
  in.bss = make<BssSection>(".bss", 0, 1);
333
108
  add(in.bss);
334
108
335
108
  // If there is a SECTIONS command and a .data.rel.ro section name use name
336
108
  // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
337
108
  // This makes sure our relro is contiguous.
338
108
  bool hasDataRelRo =
339
108
      script->hasSectionsCommand && 
findSection(".data.rel.ro", 0)4
;
340
108
  in.bssRelRo =
341
108
      make<BssSection>(hasDataRelRo ? 
".data.rel.ro.bss"0
: ".bss.rel.ro", 0, 1);
342
108
  add(in.bssRelRo);
343
108
344
108
  // Add MIPS-specific sections.
345
108
  if (config->emachine == EM_MIPS) {
346
45
    if (!config->shared && 
config->hasDynSymTab22
) {
347
6
      in.mipsRldMap = make<MipsRldMapSection>();
348
6
      add(in.mipsRldMap);
349
6
    }
350
45
    if (auto *sec = MipsAbiFlagsSection<ELFT>::create())
351
43
      add(sec);
352
45
    if (auto *sec = MipsOptionsSection<ELFT>::create())
353
39
      add(sec);
354
45
    if (auto *sec = MipsReginfoSection<ELFT>::create())
355
0
      add(sec);
356
45
  }
357
108
358
108
  for (Partition &part : partitions) {
359
108
    auto add = [&](InputSectionBase *sec) {
360
108
      sec->partition = part.getNumber();
361
108
      inputSections.push_back(sec);
362
108
    };
363
108
364
108
    if (!part.name.empty()) {
365
0
      part.elfHeader = make<PartitionElfHeaderSection<ELFT>>();
366
0
      part.elfHeader->name = part.name;
367
0
      add(part.elfHeader);
368
0
369
0
      part.programHeaders = make<PartitionProgramHeadersSection<ELFT>>();
370
0
      add(part.programHeaders);
371
0
    }
372
108
373
108
    if (config->buildId != BuildIdKind::None) {
374
0
      part.buildId = make<BuildIdSection>();
375
0
      add(part.buildId);
376
0
    }
377
108
378
108
    part.dynStrTab = make<StringTableSection>(".dynstr", true);
379
108
    part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
380
108
    part.dynamic = make<DynamicSection<ELFT>>();
381
108
    if (config->androidPackDynRelocs) {
382
0
      part.relaDyn = make<AndroidPackedRelocationSection<ELFT>>(
383
0
          config->isRela ? ".rela.dyn" : ".rel.dyn");
384
108
    } else {
385
108
      part.relaDyn = make<RelocationSection<ELFT>>(
386
108
          config->isRela ? 
".rela.dyn"62
:
".rel.dyn"46
, config->zCombreloc);
387
108
    }
388
108
389
108
    if (needsInterpSection())
390
1
      add(createInterpSection());
391
108
392
108
    if (config->hasDynSymTab) {
393
56
      part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
394
56
      add(part.dynSymTab);
395
56
396
56
      part.verSym = make<VersionTableSection>();
397
56
      add(part.verSym);
398
56
399
56
      if (!config->versionDefinitions.empty()) {
400
0
        part.verDef = make<VersionDefinitionSection>();
401
0
        add(part.verDef);
402
0
      }
403
56
404
56
      part.verNeed = make<VersionNeedSection<ELFT>>();
405
56
      add(part.verNeed);
406
56
407
56
      if (config->gnuHash) {
408
26
        part.gnuHashTab = make<GnuHashTableSection>();
409
26
        add(part.gnuHashTab);
410
26
      }
411
56
412
56
      if (config->sysvHash) {
413
56
        part.hashTab = make<HashTableSection>();
414
56
        add(part.hashTab);
415
56
      }
416
56
417
56
      add(part.dynamic);
418
56
      add(part.dynStrTab);
419
56
      add(part.relaDyn);
420
56
    }
421
108
422
108
    if (config->relrPackDynRelocs) {
423
0
      part.relrDyn = make<RelrSection<ELFT>>();
424
0
      add(part.relrDyn);
425
0
    }
426
108
427
108
    if (!config->relocatable) {
428
105
      if (config->ehFrameHdr) {
429
3
        part.ehFrameHdr = make<EhFrameHeader>();
430
3
        add(part.ehFrameHdr);
431
3
      }
432
105
      part.ehFrame = make<EhFrameSection>();
433
105
      add(part.ehFrame);
434
105
    }
435
108
436
108
    if (config->emachine == EM_ARM && 
!config->relocatable0
) {
437
0
      // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
438
0
      // InputSections.
439
0
      part.armExidx = make<ARMExidxSyntheticSection>();
440
0
      add(part.armExidx);
441
0
    }
442
108
  }
443
108
444
108
  if (partitions.size() != 1) {
445
0
    // Create the partition end marker. This needs to be in partition number 255
446
0
    // so that it is sorted after all other partitions. It also has other
447
0
    // special handling (see createPhdrs() and combineEhSections()).
448
0
    in.partEnd = make<BssSection>(".part.end", config->maxPageSize, 1);
449
0
    in.partEnd->partition = 255;
450
0
    add(in.partEnd);
451
0
452
0
    in.partIndex = make<PartitionIndexSection>();
453
0
    addOptionalRegular("__part_index_begin", in.partIndex, 0);
454
0
    addOptionalRegular("__part_index_end", in.partIndex,
455
0
                       in.partIndex->getSize());
456
0
    add(in.partIndex);
457
0
  }
458
108
459
108
  // Add .got. MIPS' .got is so different from the other archs,
460
108
  // it has its own class.
461
108
  if (config->emachine == EM_MIPS) {
462
45
    in.mipsGot = make<MipsGotSection>();
463
45
    add(in.mipsGot);
464
63
  } else {
465
63
    in.got = make<GotSection>();
466
63
    add(in.got);
467
63
  }
468
108
469
108
  if (config->emachine == EM_PPC) {
470
0
    in.ppc32Got2 = make<PPC32Got2Section>();
471
0
    add(in.ppc32Got2);
472
0
  }
473
108
474
108
  if (config->emachine == EM_PPC64) {
475
62
    in.ppc64LongBranchTarget = make<PPC64LongBranchTargetSection>();
476
62
    add(in.ppc64LongBranchTarget);
477
62
  }
478
108
479
108
  if (config->emachine == EM_RISCV) {
480
0
    in.riscvSdata = make<RISCVSdataSection>();
481
0
    add(in.riscvSdata);
482
0
  }
483
108
484
108
  in.gotPlt = make<GotPltSection>();
485
108
  add(in.gotPlt);
486
108
  in.igotPlt = make<IgotPltSection>();
487
108
  add(in.igotPlt);
488
108
489
108
  // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
490
108
  // it as a relocation and ensure the referenced section is created.
491
108
  if (ElfSym::globalOffsetTable && 
config->emachine != EM_MIPS32
) {
492
31
    if (target->gotBaseSymInGotPlt)
493
0
      in.gotPlt->hasGotPltOffRel = true;
494
31
    else
495
31
      in.got->hasGotOffRel = true;
496
31
  }
497
108
498
108
  if (config->gdbIndex)
499
0
    add(GdbIndexSection::create<ELFT>());
500
108
501
108
  // We always need to add rel[a].plt to output if it has entries.
502
108
  // Even for static linking it can contain R_[*]_IRELATIVE relocations.
503
108
  in.relaPlt = make<RelocationSection<ELFT>>(
504
108
      config->isRela ? 
".rela.plt"62
:
".rel.plt"46
, /*sort=*/false);
505
108
  add(in.relaPlt);
506
108
507
108
  // The relaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
508
108
  // that the IRelative relocations are processed last by the dynamic loader.
509
108
  // We cannot place the iplt section in .rel.dyn when Android relocation
510
108
  // packing is enabled because that would cause a section type mismatch.
511
108
  // However, because the Android dynamic loader reads .rel.plt after .rel.dyn,
512
108
  // we can get the desired behaviour by placing the iplt section in .rel.plt.
513
108
  in.relaIplt = make<RelocationSection<ELFT>>(
514
108
      (config->emachine == EM_ARM && 
!config->androidPackDynRelocs0
)
515
108
          ? 
".rel.dyn"0
516
108
          : in.relaPlt->name,
517
108
      /*sort=*/false);
518
108
  add(in.relaIplt);
519
108
520
108
  in.plt = make<PltSection>(false);
521
108
  add(in.plt);
522
108
  in.iplt = make<PltSection>(true);
523
108
  add(in.iplt);
524
108
525
108
  if (config->andFeatures)
526
0
    add(make<GnuPropertySection>());
527
108
528
108
  // .note.GNU-stack is always added when we are creating a re-linkable
529
108
  // object file. Other linkers are using the presence of this marker
530
108
  // section to control the executable-ness of the stack area, but that
531
108
  // is irrelevant these days. Stack area should always be non-executable
532
108
  // by default. So we emit this section unconditionally.
533
108
  if (config->relocatable)
534
3
    add(make<GnuStackSection>());
535
108
536
108
  if (in.symTab)
537
108
    add(in.symTab);
538
108
  if (in.symTabShndx)
539
108
    add(in.symTabShndx);
540
108
  add(in.shStrTab);
541
108
  if (in.strTab)
542
108
    add(in.strTab);
543
108
}
544
545
// The main function of the writer.
546
2.75k
template <class ELFT> void Writer<ELFT>::run() {
547
2.75k
  // Make copies of any input sections that need to be copied into each
548
2.75k
  // partition.
549
2.75k
  copySectionsIntoPartitions<ELFT>();
550
2.75k
551
2.75k
  // Create linker-synthesized sections such as .got or .plt.
552
2.75k
  // Such sections are of type input section.
553
2.75k
  createSyntheticSections<ELFT>();
554
2.75k
555
2.75k
  // Some input sections that are used for exception handling need to be moved
556
2.75k
  // into synthetic sections. Do that now so that they aren't assigned to
557
2.75k
  // output sections in the usual way.
558
2.75k
  if (!config->relocatable)
559
2.66k
    combineEhSections<ELFT>();
560
2.75k
561
2.75k
  // We want to process linker script commands. When SECTIONS command
562
2.75k
  // is given we let it create sections.
563
2.75k
  script->processSectionCommands();
564
2.75k
565
2.75k
  // Linker scripts controls how input sections are assigned to output sections.
566
2.75k
  // Input sections that were not handled by scripts are called "orphans", and
567
2.75k
  // they are assigned to output sections by the default rule. Process that.
568
2.75k
  script->addOrphanSections();
569
2.75k
570
2.75k
  if (config->discard != DiscardPolicy::All)
571
2.75k
    copyLocalSymbols();
572
2.75k
573
2.75k
  if (config->copyRelocs)
574
110
    addSectionSymbols();
575
2.75k
576
2.75k
  // Now that we have a complete set of output sections. This function
577
2.75k
  // completes section contents. For example, we need to add strings
578
2.75k
  // to the string table, and add entries to .got and .plt.
579
2.75k
  // finalizeSections does that.
580
2.75k
  finalizeSections();
581
2.75k
  checkExecuteOnly();
582
2.75k
  if (errorCount())
583
167
    return;
584
2.58k
585
2.58k
  script->assignAddresses();
586
2.58k
587
2.58k
  // If -compressed-debug-sections is specified, we need to compress
588
2.58k
  // .debug_* sections. Do it right now because it changes the size of
589
2.58k
  // output sections.
590
2.58k
  for (OutputSection *sec : outputSections)
591
352k
    sec->maybeCompress<ELFT>();
592
2.58k
593
2.58k
  script->allocateHeaders(mainPart->phdrs);
594
2.58k
595
2.58k
  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
596
2.58k
  // 0 sized region. This has to be done late since only after assignAddresses
597
2.58k
  // we know the size of the sections.
598
2.58k
  for (Partition &part : partitions)
599
2.59k
    removeEmptyPTLoad(part.phdrs);
600
2.58k
601
2.58k
  if (!config->oFormatBinary)
602
2.58k
    assignFileOffsets();
603
5
  else
604
5
    assignFileOffsetsBinary();
605
2.58k
606
2.58k
  for (Partition &part : partitions)
607
2.59k
    setPhdrs(part);
608
2.58k
609
2.58k
  if (config->relocatable)
610
87
    for (OutputSection *sec : outputSections)
611
66.2k
      sec->addr = 0;
612
2.58k
613
2.58k
  if (config->checkSections)
614
2.58k
    checkSections();
615
2.58k
616
2.58k
  // It does not make sense try to open the file if we have error already.
617
2.58k
  if (errorCount())
618
7
    return;
619
2.57k
  // Write the result down to a file.
620
2.57k
  openFile();
621
2.57k
  if (errorCount())
622
2
    return;
623
2.57k
624
2.57k
  if (!config->oFormatBinary) {
625
2.57k
    writeTrapInstr();
626
2.57k
    writeHeader();
627
2.57k
    writeSections();
628
2.57k
  } else {
629
5
    writeSectionsBinary();
630
5
  }
631
2.57k
632
2.57k
  // Backfill .note.gnu.build-id section content. This is done at last
633
2.57k
  // because the content is usually a hash value of the entire output file.
634
2.57k
  writeBuildId();
635
2.57k
  if (errorCount())
636
71
    return;
637
2.50k
638
2.50k
  // Handle -Map and -cref options.
639
2.50k
  writeMapFile();
640
2.50k
  writeCrossReferenceTable();
641
2.50k
  if (errorCount())
642
0
    return;
643
2.50k
644
2.50k
  if (auto e = buffer->commit())
645
0
    error("failed to write to the output file: " + toString(std::move(e)));
646
2.50k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::run()
Line
Count
Source
546
376
template <class ELFT> void Writer<ELFT>::run() {
547
376
  // Make copies of any input sections that need to be copied into each
548
376
  // partition.
549
376
  copySectionsIntoPartitions<ELFT>();
550
376
551
376
  // Create linker-synthesized sections such as .got or .plt.
552
376
  // Such sections are of type input section.
553
376
  createSyntheticSections<ELFT>();
554
376
555
376
  // Some input sections that are used for exception handling need to be moved
556
376
  // into synthetic sections. Do that now so that they aren't assigned to
557
376
  // output sections in the usual way.
558
376
  if (!config->relocatable)
559
365
    combineEhSections<ELFT>();
560
376
561
376
  // We want to process linker script commands. When SECTIONS command
562
376
  // is given we let it create sections.
563
376
  script->processSectionCommands();
564
376
565
376
  // Linker scripts controls how input sections are assigned to output sections.
566
376
  // Input sections that were not handled by scripts are called "orphans", and
567
376
  // they are assigned to output sections by the default rule. Process that.
568
376
  script->addOrphanSections();
569
376
570
376
  if (config->discard != DiscardPolicy::All)
571
376
    copyLocalSymbols();
572
376
573
376
  if (config->copyRelocs)
574
14
    addSectionSymbols();
575
376
576
376
  // Now that we have a complete set of output sections. This function
577
376
  // completes section contents. For example, we need to add strings
578
376
  // to the string table, and add entries to .got and .plt.
579
376
  // finalizeSections does that.
580
376
  finalizeSections();
581
376
  checkExecuteOnly();
582
376
  if (errorCount())
583
9
    return;
584
367
585
367
  script->assignAddresses();
586
367
587
367
  // If -compressed-debug-sections is specified, we need to compress
588
367
  // .debug_* sections. Do it right now because it changes the size of
589
367
  // output sections.
590
367
  for (OutputSection *sec : outputSections)
591
3.43k
    sec->maybeCompress<ELFT>();
592
367
593
367
  script->allocateHeaders(mainPart->phdrs);
594
367
595
367
  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
596
367
  // 0 sized region. This has to be done late since only after assignAddresses
597
367
  // we know the size of the sections.
598
367
  for (Partition &part : partitions)
599
370
    removeEmptyPTLoad(part.phdrs);
600
367
601
367
  if (!config->oFormatBinary)
602
367
    assignFileOffsets();
603
0
  else
604
0
    assignFileOffsetsBinary();
605
367
606
367
  for (Partition &part : partitions)
607
370
    setPhdrs(part);
608
367
609
367
  if (config->relocatable)
610
11
    for (OutputSection *sec : outputSections)
611
94
      sec->addr = 0;
612
367
613
367
  if (config->checkSections)
614
366
    checkSections();
615
367
616
367
  // It does not make sense try to open the file if we have error already.
617
367
  if (errorCount())
618
1
    return;
619
366
  // Write the result down to a file.
620
366
  openFile();
621
366
  if (errorCount())
622
1
    return;
623
365
624
365
  if (!config->oFormatBinary) {
625
365
    writeTrapInstr();
626
365
    writeHeader();
627
365
    writeSections();
628
365
  } else {
629
0
    writeSectionsBinary();
630
0
  }
631
365
632
365
  // Backfill .note.gnu.build-id section content. This is done at last
633
365
  // because the content is usually a hash value of the entire output file.
634
365
  writeBuildId();
635
365
  if (errorCount())
636
10
    return;
637
355
638
355
  // Handle -Map and -cref options.
639
355
  writeMapFile();
640
355
  writeCrossReferenceTable();
641
355
  if (errorCount())
642
0
    return;
643
355
644
355
  if (auto e = buffer->commit())
645
0
    error("failed to write to the output file: " + toString(std::move(e)));
646
355
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::run()
Line
Count
Source
546
157
template <class ELFT> void Writer<ELFT>::run() {
547
157
  // Make copies of any input sections that need to be copied into each
548
157
  // partition.
549
157
  copySectionsIntoPartitions<ELFT>();
550
157
551
157
  // Create linker-synthesized sections such as .got or .plt.
552
157
  // Such sections are of type input section.
553
157
  createSyntheticSections<ELFT>();
554
157
555
157
  // Some input sections that are used for exception handling need to be moved
556
157
  // into synthetic sections. Do that now so that they aren't assigned to
557
157
  // output sections in the usual way.
558
157
  if (!config->relocatable)
559
146
    combineEhSections<ELFT>();
560
157
561
157
  // We want to process linker script commands. When SECTIONS command
562
157
  // is given we let it create sections.
563
157
  script->processSectionCommands();
564
157
565
157
  // Linker scripts controls how input sections are assigned to output sections.
566
157
  // Input sections that were not handled by scripts are called "orphans", and
567
157
  // they are assigned to output sections by the default rule. Process that.
568
157
  script->addOrphanSections();
569
157
570
157
  if (config->discard != DiscardPolicy::All)
571
157
    copyLocalSymbols();
572
157
573
157
  if (config->copyRelocs)
574
11
    addSectionSymbols();
575
157
576
157
  // Now that we have a complete set of output sections. This function
577
157
  // completes section contents. For example, we need to add strings
578
157
  // to the string table, and add entries to .got and .plt.
579
157
  // finalizeSections does that.
580
157
  finalizeSections();
581
157
  checkExecuteOnly();
582
157
  if (errorCount())
583
3
    return;
584
154
585
154
  script->assignAddresses();
586
154
587
154
  // If -compressed-debug-sections is specified, we need to compress
588
154
  // .debug_* sections. Do it right now because it changes the size of
589
154
  // output sections.
590
154
  for (OutputSection *sec : outputSections)
591
1.88k
    sec->maybeCompress<ELFT>();
592
154
593
154
  script->allocateHeaders(mainPart->phdrs);
594
154
595
154
  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
596
154
  // 0 sized region. This has to be done late since only after assignAddresses
597
154
  // we know the size of the sections.
598
154
  for (Partition &part : partitions)
599
154
    removeEmptyPTLoad(part.phdrs);
600
154
601
154
  if (!config->oFormatBinary)
602
154
    assignFileOffsets();
603
0
  else
604
0
    assignFileOffsetsBinary();
605
154
606
154
  for (Partition &part : partitions)
607
154
    setPhdrs(part);
608
154
609
154
  if (config->relocatable)
610
11
    for (OutputSection *sec : outputSections)
611
112
      sec->addr = 0;
612
154
613
154
  if (config->checkSections)
614
154
    checkSections();
615
154
616
154
  // It does not make sense try to open the file if we have error already.
617
154
  if (errorCount())
618
0
    return;
619
154
  // Write the result down to a file.
620
154
  openFile();
621
154
  if (errorCount())
622
0
    return;
623
154
624
154
  if (!config->oFormatBinary) {
625
154
    writeTrapInstr();
626
154
    writeHeader();
627
154
    writeSections();
628
154
  } else {
629
0
    writeSectionsBinary();
630
0
  }
631
154
632
154
  // Backfill .note.gnu.build-id section content. This is done at last
633
154
  // because the content is usually a hash value of the entire output file.
634
154
  writeBuildId();
635
154
  if (errorCount())
636
2
    return;
637
152
638
152
  // Handle -Map and -cref options.
639
152
  writeMapFile();
640
152
  writeCrossReferenceTable();
641
152
  if (errorCount())
642
0
    return;
643
152
644
152
  if (auto e = buffer->commit())
645
0
    error("failed to write to the output file: " + toString(std::move(e)));
646
152
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::run()
Line
Count
Source
546
2.11k
template <class ELFT> void Writer<ELFT>::run() {
547
2.11k
  // Make copies of any input sections that need to be copied into each
548
2.11k
  // partition.
549
2.11k
  copySectionsIntoPartitions<ELFT>();
550
2.11k
551
2.11k
  // Create linker-synthesized sections such as .got or .plt.
552
2.11k
  // Such sections are of type input section.
553
2.11k
  createSyntheticSections<ELFT>();
554
2.11k
555
2.11k
  // Some input sections that are used for exception handling need to be moved
556
2.11k
  // into synthetic sections. Do that now so that they aren't assigned to
557
2.11k
  // output sections in the usual way.
558
2.11k
  if (!config->relocatable)
559
2.05k
    combineEhSections<ELFT>();
560
2.11k
561
2.11k
  // We want to process linker script commands. When SECTIONS command
562
2.11k
  // is given we let it create sections.
563
2.11k
  script->processSectionCommands();
564
2.11k
565
2.11k
  // Linker scripts controls how input sections are assigned to output sections.
566
2.11k
  // Input sections that were not handled by scripts are called "orphans", and
567
2.11k
  // they are assigned to output sections by the default rule. Process that.
568
2.11k
  script->addOrphanSections();
569
2.11k
570
2.11k
  if (config->discard != DiscardPolicy::All)
571
2.11k
    copyLocalSymbols();
572
2.11k
573
2.11k
  if (config->copyRelocs)
574
82
    addSectionSymbols();
575
2.11k
576
2.11k
  // Now that we have a complete set of output sections. This function
577
2.11k
  // completes section contents. For example, we need to add strings
578
2.11k
  // to the string table, and add entries to .got and .plt.
579
2.11k
  // finalizeSections does that.
580
2.11k
  finalizeSections();
581
2.11k
  checkExecuteOnly();
582
2.11k
  if (errorCount())
583
148
    return;
584
1.96k
585
1.96k
  script->assignAddresses();
586
1.96k
587
1.96k
  // If -compressed-debug-sections is specified, we need to compress
588
1.96k
  // .debug_* sections. Do it right now because it changes the size of
589
1.96k
  // output sections.
590
1.96k
  for (OutputSection *sec : outputSections)
591
346k
    sec->maybeCompress<ELFT>();
592
1.96k
593
1.96k
  script->allocateHeaders(mainPart->phdrs);
594
1.96k
595
1.96k
  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
596
1.96k
  // 0 sized region. This has to be done late since only after assignAddresses
597
1.96k
  // we know the size of the sections.
598
1.96k
  for (Partition &part : partitions)
599
1.97k
    removeEmptyPTLoad(part.phdrs);
600
1.96k
601
1.96k
  if (!config->oFormatBinary)
602
1.95k
    assignFileOffsets();
603
5
  else
604
5
    assignFileOffsetsBinary();
605
1.96k
606
1.96k
  for (Partition &part : partitions)
607
1.97k
    setPhdrs(part);
608
1.96k
609
1.96k
  if (config->relocatable)
610
62
    for (OutputSection *sec : outputSections)
611
65.9k
      sec->addr = 0;
612
1.96k
613
1.96k
  if (config->checkSections)
614
1.96k
    checkSections();
615
1.96k
616
1.96k
  // It does not make sense try to open the file if we have error already.
617
1.96k
  if (errorCount())
618
6
    return;
619
1.95k
  // Write the result down to a file.
620
1.95k
  openFile();
621
1.95k
  if (errorCount())
622
1
    return;
623
1.95k
624
1.95k
  if (!config->oFormatBinary) {
625
1.95k
    writeTrapInstr();
626
1.95k
    writeHeader();
627
1.95k
    writeSections();
628
1.95k
  } else {
629
5
    writeSectionsBinary();
630
5
  }
631
1.95k
632
1.95k
  // Backfill .note.gnu.build-id section content. This is done at last
633
1.95k
  // because the content is usually a hash value of the entire output file.
634
1.95k
  writeBuildId();
635
1.95k
  if (errorCount())
636
49
    return;
637
1.90k
638
1.90k
  // Handle -Map and -cref options.
639
1.90k
  writeMapFile();
640
1.90k
  writeCrossReferenceTable();
641
1.90k
  if (errorCount())
642
0
    return;
643
1.90k
644
1.90k
  if (auto e = buffer->commit())
645
0
    error("failed to write to the output file: " + toString(std::move(e)));
646
1.90k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::run()
Line
Count
Source
546
108
template <class ELFT> void Writer<ELFT>::run() {
547
108
  // Make copies of any input sections that need to be copied into each
548
108
  // partition.
549
108
  copySectionsIntoPartitions<ELFT>();
550
108
551
108
  // Create linker-synthesized sections such as .got or .plt.
552
108
  // Such sections are of type input section.
553
108
  createSyntheticSections<ELFT>();
554
108
555
108
  // Some input sections that are used for exception handling need to be moved
556
108
  // into synthetic sections. Do that now so that they aren't assigned to
557
108
  // output sections in the usual way.
558
108
  if (!config->relocatable)
559
105
    combineEhSections<ELFT>();
560
108
561
108
  // We want to process linker script commands. When SECTIONS command
562
108
  // is given we let it create sections.
563
108
  script->processSectionCommands();
564
108
565
108
  // Linker scripts controls how input sections are assigned to output sections.
566
108
  // Input sections that were not handled by scripts are called "orphans", and
567
108
  // they are assigned to output sections by the default rule. Process that.
568
108
  script->addOrphanSections();
569
108
570
108
  if (config->discard != DiscardPolicy::All)
571
107
    copyLocalSymbols();
572
108
573
108
  if (config->copyRelocs)
574
3
    addSectionSymbols();
575
108
576
108
  // Now that we have a complete set of output sections. This function
577
108
  // completes section contents. For example, we need to add strings
578
108
  // to the string table, and add entries to .got and .plt.
579
108
  // finalizeSections does that.
580
108
  finalizeSections();
581
108
  checkExecuteOnly();
582
108
  if (errorCount())
583
7
    return;
584
101
585
101
  script->assignAddresses();
586
101
587
101
  // If -compressed-debug-sections is specified, we need to compress
588
101
  // .debug_* sections. Do it right now because it changes the size of
589
101
  // output sections.
590
101
  for (OutputSection *sec : outputSections)
591
1.15k
    sec->maybeCompress<ELFT>();
592
101
593
101
  script->allocateHeaders(mainPart->phdrs);
594
101
595
101
  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
596
101
  // 0 sized region. This has to be done late since only after assignAddresses
597
101
  // we know the size of the sections.
598
101
  for (Partition &part : partitions)
599
101
    removeEmptyPTLoad(part.phdrs);
600
101
601
101
  if (!config->oFormatBinary)
602
101
    assignFileOffsets();
603
0
  else
604
0
    assignFileOffsetsBinary();
605
101
606
101
  for (Partition &part : partitions)
607
101
    setPhdrs(part);
608
101
609
101
  if (config->relocatable)
610
3
    for (OutputSection *sec : outputSections)
611
29
      sec->addr = 0;
612
101
613
101
  if (config->checkSections)
614
101
    checkSections();
615
101
616
101
  // It does not make sense try to open the file if we have error already.
617
101
  if (errorCount())
618
0
    return;
619
101
  // Write the result down to a file.
620
101
  openFile();
621
101
  if (errorCount())
622
0
    return;
623
101
624
101
  if (!config->oFormatBinary) {
625
101
    writeTrapInstr();
626
101
    writeHeader();
627
101
    writeSections();
628
101
  } else {
629
0
    writeSectionsBinary();
630
0
  }
631
101
632
101
  // Backfill .note.gnu.build-id section content. This is done at last
633
101
  // because the content is usually a hash value of the entire output file.
634
101
  writeBuildId();
635
101
  if (errorCount())
636
10
    return;
637
91
638
91
  // Handle -Map and -cref options.
639
91
  writeMapFile();
640
91
  writeCrossReferenceTable();
641
91
  if (errorCount())
642
0
    return;
643
91
644
91
  if (auto e = buffer->commit())
645
0
    error("failed to write to the output file: " + toString(std::move(e)));
646
91
}
647
648
68.2k
static bool shouldKeepInSymtab(const Defined &sym) {
649
68.2k
  if (sym.isSection())
650
66.1k
    return false;
651
2.06k
652
2.06k
  if (config->discard == DiscardPolicy::None)
653
58
    return true;
654
2.00k
655
2.00k
  // If -emit-reloc is given, all symbols including local ones need to be
656
2.00k
  // copied because they may be referenced by relocations.
657
2.00k
  if (config->emitRelocs)
658
35
    return true;
659
1.96k
660
1.96k
  // In ELF assembly .L symbols are normally discarded by the assembler.
661
1.96k
  // If the assembler fails to do so, the linker discards them if
662
1.96k
  // * --discard-locals is used.
663
1.96k
  // * The symbol is in a SHF_MERGE section, which is normally the reason for
664
1.96k
  //   the assembler keeping the .L symbol.
665
1.96k
  StringRef name = sym.getName();
666
1.96k
  bool isLocal = name.startswith(".L") || 
name.empty()1.89k
;
667
1.96k
  if (!isLocal)
668
1.89k
    return true;
669
77
670
77
  if (config->discard == DiscardPolicy::Locals)
671
2
    return false;
672
75
673
75
  SectionBase *sec = sym.section;
674
75
  return !sec || 
!(sec->flags & SHF_MERGE)73
;
675
75
}
676
677
86.6k
static bool includeInSymtab(const Symbol &b) {
678
86.6k
  if (!b.isLocal() && 
!b.isUsedInRegularObj18.3k
)
679
472
    return false;
680
86.1k
681
86.1k
  if (auto *d = dyn_cast<Defined>(&b)) {
682
85.3k
    // Always include absolute symbols.
683
85.3k
    SectionBase *sec = d->section;
684
85.3k
    if (!sec)
685
872
      return true;
686
84.5k
    sec = sec->repl;
687
84.5k
688
84.5k
    // Exclude symbols pointing to garbage-collected sections.
689
84.5k
    if (isa<InputSectionBase>(sec) && 
!sec->isLive()84.1k
)
690
86
      return false;
691
84.4k
692
84.4k
    if (auto *s = dyn_cast<MergeInputSection>(sec))
693
72
      if (!s->getSectionPiece(d->value)->live)
694
7
        return false;
695
84.4k
    return true;
696
84.4k
  }
697
798
  return b.used;
698
798
}
699
700
// Local symbols are not in the linker's symbol table. This function scans
701
// each object file's symbol table to copy local symbols to the output.
702
2.75k
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
703
2.75k
  if (!in.symTab)
704
4
    return;
705
3.36k
  
for (InputFile *file : objectFiles)2.74k
{
706
3.36k
    ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
707
68.3k
    for (Symbol *b : f->getLocalSymbols()) {
708
68.3k
      if (!b->isLocal())
709
0
        fatal(toString(f) +
710
0
              ": broken object: getLocalSymbols returns a non-local symbol");
711
68.3k
      auto *dr = dyn_cast<Defined>(b);
712
68.3k
713
68.3k
      // No reason to keep local undefined symbol in symtab.
714
68.3k
      if (!dr)
715
62
        continue;
716
68.3k
      if (!includeInSymtab(*b))
717
55
        continue;
718
68.2k
      if (!shouldKeepInSymtab(*dr))
719
66.1k
        continue;
720
2.05k
      in.symTab->addSymbol(b);
721
2.05k
    }
722
3.36k
  }
723
2.74k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::copyLocalSymbols()
Line
Count
Source
702
376
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
703
376
  if (!in.symTab)
704
0
    return;
705
436
  
for (InputFile *file : objectFiles)376
{
706
436
    ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
707
870
    for (Symbol *b : f->getLocalSymbols()) {
708
870
      if (!b->isLocal())
709
0
        fatal(toString(f) +
710
0
              ": broken object: getLocalSymbols returns a non-local symbol");
711
870
      auto *dr = dyn_cast<Defined>(b);
712
870
713
870
      // No reason to keep local undefined symbol in symtab.
714
870
      if (!dr)
715
0
        continue;
716
870
      if (!includeInSymtab(*b))
717
8
        continue;
718
862
      if (!shouldKeepInSymtab(*dr))
719
130
        continue;
720
732
      in.symTab->addSymbol(b);
721
732
    }
722
436
  }
723
376
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::copyLocalSymbols()
Line
Count
Source
702
157
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
703
157
  if (!in.symTab)
704
0
    return;
705
210
  
for (InputFile *file : objectFiles)157
{
706
210
    ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
707
210
    for (Symbol *b : f->getLocalSymbols()) {
708
135
      if (!b->isLocal())
709
0
        fatal(toString(f) +
710
0
              ": broken object: getLocalSymbols returns a non-local symbol");
711
135
      auto *dr = dyn_cast<Defined>(b);
712
135
713
135
      // No reason to keep local undefined symbol in symtab.
714
135
      if (!dr)
715
0
        continue;
716
135
      if (!includeInSymtab(*b))
717
0
        continue;
718
135
      if (!shouldKeepInSymtab(*dr))
719
62
        continue;
720
73
      in.symTab->addSymbol(b);
721
73
    }
722
210
  }
723
157
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::copyLocalSymbols()
Line
Count
Source
702
2.11k
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
703
2.11k
  if (!in.symTab)
704
4
    return;
705
2.58k
  
for (InputFile *file : objectFiles)2.10k
{
706
2.58k
    ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
707
67.2k
    for (Symbol *b : f->getLocalSymbols()) {
708
67.2k
      if (!b->isLocal())
709
0
        fatal(toString(f) +
710
0
              ": broken object: getLocalSymbols returns a non-local symbol");
711
67.2k
      auto *dr = dyn_cast<Defined>(b);
712
67.2k
713
67.2k
      // No reason to keep local undefined symbol in symtab.
714
67.2k
      if (!dr)
715
62
        continue;
716
67.2k
      if (!includeInSymtab(*b))
717
41
        continue;
718
67.1k
      if (!shouldKeepInSymtab(*dr))
719
65.9k
        continue;
720
1.18k
      in.symTab->addSymbol(b);
721
1.18k
    }
722
2.58k
  }
723
2.10k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::copyLocalSymbols()
Line
Count
Source
702
107
template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
703
107
  if (!in.symTab)
704
0
    return;
705
136
  
for (InputFile *file : objectFiles)107
{
706
136
    ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
707
136
    for (Symbol *b : f->getLocalSymbols()) {
708
98
      if (!b->isLocal())
709
0
        fatal(toString(f) +
710
0
              ": broken object: getLocalSymbols returns a non-local symbol");
711
98
      auto *dr = dyn_cast<Defined>(b);
712
98
713
98
      // No reason to keep local undefined symbol in symtab.
714
98
      if (!dr)
715
0
        continue;
716
98
      if (!includeInSymtab(*b))
717
6
        continue;
718
92
      if (!shouldKeepInSymtab(*dr))
719
35
        continue;
720
57
      in.symTab->addSymbol(b);
721
57
    }
722
136
  }
723
107
}
724
725
// Create a section symbol for each output section so that we can represent
726
// relocations that point to the section. If we know that no relocation is
727
// referring to a section (that happens if the section is a synthetic one), we
728
// don't create a section symbol for that section.
729
110
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
730
67.2k
  for (BaseCommand *base : script->sectionCommands) {
731
67.2k
    auto *sec = dyn_cast<OutputSection>(base);
732
67.2k
    if (!sec)
733
1
      continue;
734
67.2k
    auto i = llvm::find_if(sec->sectionCommands, [](BaseCommand *base) {
735
67.1k
      if (auto *isd = dyn_cast<InputSectionDescription>(base))
736
67.1k
        return !isd->sections.empty();
737
2
      return false;
738
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
734
230
    auto i = llvm::find_if(sec->sectionCommands, [](BaseCommand *base) {
735
230
      if (auto *isd = dyn_cast<InputSectionDescription>(base))
736
229
        return !isd->sections.empty();
737
1
      return false;
738
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
734
178
    auto i = llvm::find_if(sec->sectionCommands, [](BaseCommand *base) {
735
178
      if (auto *isd = dyn_cast<InputSectionDescription>(base))
736
178
        return !isd->sections.empty();
737
0
      return false;
738
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
734
66.7k
    auto i = llvm::find_if(sec->sectionCommands, [](BaseCommand *base) {
735
66.7k
      if (auto *isd = dyn_cast<InputSectionDescription>(base))
736
66.7k
        return !isd->sections.empty();
737
1
      return false;
738
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
734
47
    auto i = llvm::find_if(sec->sectionCommands, [](BaseCommand *base) {
735
47
      if (auto *isd = dyn_cast<InputSectionDescription>(base))
736
47
        return !isd->sections.empty();
737
0
      return false;
738
0
    });
739
67.2k
    if (i == sec->sectionCommands.end())
740
5
      continue;
741
67.1k
    InputSection *isec = cast<InputSectionDescription>(*i)->sections[0];
742
67.1k
743
67.1k
    // Relocations are not using REL[A] section symbols.
744
67.1k
    if (isec->type == SHT_REL || 
isec->type == SHT_RELA67.1k
)
745
207
      continue;
746
66.9k
747
66.9k
    // Unlike other synthetic sections, mergeable output sections contain data
748
66.9k
    // copied from input sections, and there may be a relocation pointing to its
749
66.9k
    // contents if -r or -emit-reloc are given.
750
66.9k
    if (isa<SyntheticSection>(isec) && 
!(isec->flags & SHF_MERGE)1.19k
)
751
1.16k
      continue;
752
65.8k
753
65.8k
    auto *sym =
754
65.8k
        make<Defined>(isec->file, "", STB_LOCAL, /*stOther=*/0, STT_SECTION,
755
65.8k
                      /*value=*/0, /*size=*/0, isec);
756
65.8k
    in.symTab->addSymbol(sym);
757
65.8k
  }
758
110
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addSectionSymbols()
Line
Count
Source
729
14
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
730
230
  for (BaseCommand *base : script->sectionCommands) {
731
230
    auto *sec = dyn_cast<OutputSection>(base);
732
230
    if (!sec)
733
0
      continue;
734
230
    auto i = llvm::find_if(sec->sectionCommands, [](BaseCommand *base) {
735
230
      if (auto *isd = dyn_cast<InputSectionDescription>(base))
736
230
        return !isd->sections.empty();
737
230
      return false;
738
230
    });
739
230
    if (i == sec->sectionCommands.end())
740
1
      continue;
741
229
    InputSection *isec = cast<InputSectionDescription>(*i)->sections[0];
742
229
743
229
    // Relocations are not using REL[A] section symbols.
744
229
    if (isec->type == SHT_REL || 
isec->type == SHT_RELA192
)
745
37
      continue;
746
192
747
192
    // Unlike other synthetic sections, mergeable output sections contain data
748
192
    // copied from input sections, and there may be a relocation pointing to its
749
192
    // contents if -r or -emit-reloc are given.
750
192
    if (isa<SyntheticSection>(isec) && 
!(isec->flags & SHF_MERGE)154
)
751
150
      continue;
752
42
753
42
    auto *sym =
754
42
        make<Defined>(isec->file, "", STB_LOCAL, /*stOther=*/0, STT_SECTION,
755
42
                      /*value=*/0, /*size=*/0, isec);
756
42
    in.symTab->addSymbol(sym);
757
42
  }
758
14
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addSectionSymbols()
Line
Count
Source
729
11
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
730
178
  for (BaseCommand *base : script->sectionCommands) {
731
178
    auto *sec = dyn_cast<OutputSection>(base);
732
178
    if (!sec)
733
0
      continue;
734
178
    auto i = llvm::find_if(sec->sectionCommands, [](BaseCommand *base) {
735
178
      if (auto *isd = dyn_cast<InputSectionDescription>(base))
736
178
        return !isd->sections.empty();
737
178
      return false;
738
178
    });
739
178
    if (i == sec->sectionCommands.end())
740
0
      continue;
741
178
    InputSection *isec = cast<InputSectionDescription>(*i)->sections[0];
742
178
743
178
    // Relocations are not using REL[A] section symbols.
744
178
    if (isec->type == SHT_REL || 
isec->type == SHT_RELA159
)
745
19
      continue;
746
159
747
159
    // Unlike other synthetic sections, mergeable output sections contain data
748
159
    // copied from input sections, and there may be a relocation pointing to its
749
159
    // contents if -r or -emit-reloc are given.
750
159
    if (isa<SyntheticSection>(isec) && 
!(isec->flags & SHF_MERGE)121
)
751
121
      continue;
752
38
753
38
    auto *sym =
754
38
        make<Defined>(isec->file, "", STB_LOCAL, /*stOther=*/0, STT_SECTION,
755
38
                      /*value=*/0, /*size=*/0, isec);
756
38
    in.symTab->addSymbol(sym);
757
38
  }
758
11
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addSectionSymbols()
Line
Count
Source
729
82
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
730
66.7k
  for (BaseCommand *base : script->sectionCommands) {
731
66.7k
    auto *sec = dyn_cast<OutputSection>(base);
732
66.7k
    if (!sec)
733
1
      continue;
734
66.7k
    auto i = llvm::find_if(sec->sectionCommands, [](BaseCommand *base) {
735
66.7k
      if (auto *isd = dyn_cast<InputSectionDescription>(base))
736
66.7k
        return !isd->sections.empty();
737
66.7k
      return false;
738
66.7k
    });
739
66.7k
    if (i == sec->sectionCommands.end())
740
4
      continue;
741
66.7k
    InputSection *isec = cast<InputSectionDescription>(*i)->sections[0];
742
66.7k
743
66.7k
    // Relocations are not using REL[A] section symbols.
744
66.7k
    if (isec->type == SHT_REL || isec->type == SHT_RELA)
745
146
      continue;
746
66.5k
747
66.5k
    // Unlike other synthetic sections, mergeable output sections contain data
748
66.5k
    // copied from input sections, and there may be a relocation pointing to its
749
66.5k
    // contents if -r or -emit-reloc are given.
750
66.5k
    if (isa<SyntheticSection>(isec) && 
!(isec->flags & SHF_MERGE)890
)
751
859
      continue;
752
65.7k
753
65.7k
    auto *sym =
754
65.7k
        make<Defined>(isec->file, "", STB_LOCAL, /*stOther=*/0, STT_SECTION,
755
65.7k
                      /*value=*/0, /*size=*/0, isec);
756
65.7k
    in.symTab->addSymbol(sym);
757
65.7k
  }
758
82
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSectionSymbols()
Line
Count
Source
729
3
template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
730
47
  for (BaseCommand *base : script->sectionCommands) {
731
47
    auto *sec = dyn_cast<OutputSection>(base);
732
47
    if (!sec)
733
0
      continue;
734
47
    auto i = llvm::find_if(sec->sectionCommands, [](BaseCommand *base) {
735
47
      if (auto *isd = dyn_cast<InputSectionDescription>(base))
736
47
        return !isd->sections.empty();
737
47
      return false;
738
47
    });
739
47
    if (i == sec->sectionCommands.end())
740
0
      continue;
741
47
    InputSection *isec = cast<InputSectionDescription>(*i)->sections[0];
742
47
743
47
    // Relocations are not using REL[A] section symbols.
744
47
    if (isec->type == SHT_REL || 
isec->type == SHT_RELA45
)
745
5
      continue;
746
42
747
42
    // Unlike other synthetic sections, mergeable output sections contain data
748
42
    // copied from input sections, and there may be a relocation pointing to its
749
42
    // contents if -r or -emit-reloc are given.
750
42
    if (isa<SyntheticSection>(isec) && 
!(isec->flags & SHF_MERGE)32
)
751
32
      continue;
752
10
753
10
    auto *sym =
754
10
        make<Defined>(isec->file, "", STB_LOCAL, /*stOther=*/0, STT_SECTION,
755
10
                      /*value=*/0, /*size=*/0, isec);
756
10
    in.symTab->addSymbol(sym);
757
10
  }
758
3
}
759
760
// Today's loaders have a feature to make segments read-only after
761
// processing dynamic relocations to enhance security. PT_GNU_RELRO
762
// is defined for that.
763
//
764
// This function returns true if a section needs to be put into a
765
// PT_GNU_RELRO segment.
766
160k
static bool isRelroSection(const OutputSection *sec) {
767
160k
  if (!config->zRelro)
768
214
    return false;
769
159k
770
159k
  uint64_t flags = sec->flags;
771
159k
772
159k
  // Non-allocatable or non-writable sections don't need RELRO because
773
159k
  // they are not writable or not even mapped to memory in the first place.
774
159k
  // RELRO is for sections that are essentially read-only but need to
775
159k
  // be writable only at process startup to allow dynamic linker to
776
159k
  // apply relocations.
777
159k
  if (!(flags & SHF_ALLOC) || !(flags & SHF_WRITE))
778
153k
    return false;
779
6.32k
780
6.32k
  // Once initialized, TLS data segments are used as data templates
781
6.32k
  // for a thread-local storage. For each new thread, runtime
782
6.32k
  // allocates memory for a TLS and copy templates there. No thread
783
6.32k
  // are supposed to use templates directly. Thus, it can be in RELRO.
784
6.32k
  if (flags & SHF_TLS)
785
235
    return true;
786
6.08k
787
6.08k
  // .init_array, .preinit_array and .fini_array contain pointers to
788
6.08k
  // functions that are executed on process startup or exit. These
789
6.08k
  // pointers are set by the static linker, and they are not expected
790
6.08k
  // to change at runtime. But if you are an attacker, you could do
791
6.08k
  // interesting things by manipulating pointers in .fini_array, for
792
6.08k
  // example. So they are put into RELRO.
793
6.08k
  uint32_t type = sec->type;
794
6.08k
  if (type == SHT_INIT_ARRAY || 
type == SHT_FINI_ARRAY6.06k
||
795
6.08k
      
type == SHT_PREINIT_ARRAY6.05k
)
796
46
    return true;
797
6.04k
798
6.04k
  // .got contains pointers to external symbols. They are resolved by
799
6.04k
  // the dynamic linker when a module is loaded into memory, and after
800
6.04k
  // that they are not expected to change. So, it can be in RELRO.
801
6.04k
  if (in.got && 
sec == in.got->getParent()4.92k
)
802
489
    return true;
803
5.55k
804
5.55k
  // .toc is a GOT-ish section for PowerPC64. Their contents are accessed
805
5.55k
  // through r2 register, which is reserved for that purpose. Since r2 is used
806
5.55k
  // for accessing .got as well, .got and .toc need to be close enough in the
807
5.55k
  // virtual address space. Usually, .toc comes just after .got. Since we place
808
5.55k
  // .got into RELRO, .toc needs to be placed into RELRO too.
809
5.55k
  if (sec->name.equals(".toc"))
810
44
    return true;
811
5.50k
812
5.50k
  // .got.plt contains pointers to external function symbols. They are
813
5.50k
  // by default resolved lazily, so we usually cannot put it into RELRO.
814
5.50k
  // However, if "-z now" is given, the lazy symbol resolution is
815
5.50k
  // disabled, which enables us to put it into RELRO.
816
5.50k
  if (sec == in.gotPlt->getParent())
817
661
    return config->zNow;
818
4.84k
819
4.84k
  // .dynamic section contains data for the dynamic linker, and
820
4.84k
  // there's no need to write to it at runtime, so it's better to put
821
4.84k
  // it into RELRO.
822
4.84k
  if (sec->name == ".dynamic")
823
2.29k
    return true;
824
2.55k
825
2.55k
  // Sections with some special names are put into RELRO. This is a
826
2.55k
  // bit unfortunate because section names shouldn't be significant in
827
2.55k
  // ELF in spirit. But in reality many linker features depend on
828
2.55k
  // magic section names.
829
2.55k
  StringRef s = sec->name;
830
2.55k
  return s == ".data.rel.ro" || 
s == ".bss.rel.ro"2.53k
||
s == ".ctors"2.49k
||
831
2.55k
         
s == ".dtors"2.48k
||
s == ".jcr"2.47k
||
s == ".eh_frame"2.47k
||
832
2.55k
         
s == ".openbsd.randomdata"2.47k
;
833
2.55k
}
834
835
// We compute a rank for each section. The rank indicates where the
836
// section should be placed in the file.  Instead of using simple
837
// numbers (0,1,2...), we use a series of flags. One for each decision
838
// point when placing the section.
839
// Using flags has two key properties:
840
// * It is easy to check if a give branch was taken.
841
// * It is easy two see how similar two ranks are (see getRankProximity).
842
enum RankFlags {
843
  RF_NOT_ADDR_SET = 1 << 27,
844
  RF_NOT_ALLOC = 1 << 26,
845
  RF_PARTITION = 1 << 18, // Partition number (8 bits)
846
  RF_NOT_PART_EHDR = 1 << 17,
847
  RF_NOT_PART_PHDR = 1 << 16,
848
  RF_NOT_INTERP = 1 << 15,
849
  RF_NOT_NOTE = 1 << 14,
850
  RF_WRITE = 1 << 13,
851
  RF_EXEC_WRITE = 1 << 12,
852
  RF_EXEC = 1 << 11,
853
  RF_RODATA = 1 << 10,
854
  RF_NOT_RELRO = 1 << 9,
855
  RF_NOT_TLS = 1 << 8,
856
  RF_BSS = 1 << 7,
857
  RF_PPC_NOT_TOCBSS = 1 << 6,
858
  RF_PPC_TOCL = 1 << 5,
859
  RF_PPC_TOC = 1 << 4,
860
  RF_PPC_GOT = 1 << 3,
861
  RF_PPC_BRANCH_LT = 1 << 2,
862
  RF_MIPS_GPREL = 1 << 1,
863
  RF_MIPS_NOT_GOT = 1 << 0
864
};
865
866
286k
static unsigned getSectionRank(const OutputSection *sec) {
867
286k
  unsigned rank = sec->partition * RF_PARTITION;
868
286k
869
286k
  // We want to put section specified by -T option first, so we
870
286k
  // can start assigning VA starting from them later.
871
286k
  if (config->sectionStartMap.count(sec->name))
872
59
    return rank;
873
286k
  rank |= RF_NOT_ADDR_SET;
874
286k
875
286k
  // Allocatable sections go first to reduce the total PT_LOAD size and
876
286k
  // so debug info doesn't change addresses in actual code.
877
286k
  if (!(sec->flags & SHF_ALLOC))
878
206k
    return rank | RF_NOT_ALLOC;
879
80.2k
880
80.2k
  if (sec->type == SHT_LLVM_PART_EHDR)
881
11
    return rank;
882
80.2k
  rank |= RF_NOT_PART_EHDR;
883
80.2k
884
80.2k
  if (sec->type == SHT_LLVM_PART_PHDR)
885
11
    return rank;
886
80.2k
  rank |= RF_NOT_PART_PHDR;
887
80.2k
888
80.2k
  // Put .interp first because some loaders want to see that section
889
80.2k
  // on the first page of the executable file when loaded into memory.
890
80.2k
  if (sec->name == ".interp")
891
8
    return rank;
892
80.2k
  rank |= RF_NOT_INTERP;
893
80.2k
894
80.2k
  // Put .note sections (which make up one PT_NOTE) at the beginning so that
895
80.2k
  // they are likely to be included in a core file even if core file size is
896
80.2k
  // limited. In particular, we want a .note.gnu.build-id and a .note.tag to be
897
80.2k
  // included in a core to match core files with executables.
898
80.2k
  if (sec->type == SHT_NOTE)
899
72
    return rank;
900
80.1k
  rank |= RF_NOT_NOTE;
901
80.1k
902
80.1k
  // Sort sections based on their access permission in the following
903
80.1k
  // order: R, RX, RWX, RW.  This order is based on the following
904
80.1k
  // considerations:
905
80.1k
  // * Read-only sections come first such that they go in the
906
80.1k
  //   PT_LOAD covering the program headers at the start of the file.
907
80.1k
  // * Read-only, executable sections come next.
908
80.1k
  // * Writable, executable sections follow such that .plt on
909
80.1k
  //   architectures where it needs to be writable will be placed
910
80.1k
  //   between .text and .data.
911
80.1k
  // * Writable sections come last, such that .bss lands at the very
912
80.1k
  //   end of the last PT_LOAD.
913
80.1k
  bool isExec = sec->flags & SHF_EXECINSTR;
914
80.1k
  bool isWrite = sec->flags & SHF_WRITE;
915
80.1k
916
80.1k
  if (isExec) {
917
5.11k
    if (isWrite)
918
21
      rank |= RF_EXEC_WRITE;
919
5.09k
    else
920
5.09k
      rank |= RF_EXEC;
921
75.0k
  } else if (isWrite) {
922
3.24k
    rank |= RF_WRITE;
923
71.7k
  } else if (sec->type == SHT_PROGBITS) {
924
65.7k
    // Make non-executable and non-writable PROGBITS sections (e.g .rodata
925
65.7k
    // .eh_frame) closer to .text. They likely contain PC or GOT relative
926
65.7k
    // relocations and there could be relocation overflow if other huge sections
927
65.7k
    // (.dynstr .dynsym) were placed in between.
928
65.7k
    rank |= RF_RODATA;
929
65.7k
  }
930
80.1k
931
80.1k
  // Place RelRo sections first. After considering SHT_NOBITS below, the
932
80.1k
  // ordering is PT_LOAD(PT_GNU_RELRO(.data.rel.ro .bss.rel.ro) | .data .bss),
933
80.1k
  // where | marks where page alignment happens. An alternative ordering is
934
80.1k
  // PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro .bss.rel.ro) | .bss), but it may
935
80.1k
  // waste more bytes due to 2 alignment places.
936
80.1k
  if (!isRelroSection(sec))
937
78.4k
    rank |= RF_NOT_RELRO;
938
80.1k
939
80.1k
  // If we got here we know that both A and B are in the same PT_LOAD.
940
80.1k
941
80.1k
  // The TLS initialization block needs to be a single contiguous block in a R/W
942
80.1k
  // PT_LOAD, so stick TLS sections directly before the other RelRo R/W
943
80.1k
  // sections. Since p_filesz can be less than p_memsz, place NOBITS sections
944
80.1k
  // after PROGBITS.
945
80.1k
  if (!(sec->flags & SHF_TLS))
946
79.9k
    rank |= RF_NOT_TLS;
947
80.1k
948
80.1k
  // Within TLS sections, or within other RelRo sections, or within non-RelRo
949
80.1k
  // sections, place non-NOBITS sections first.
950
80.1k
  if (sec->type == SHT_NOBITS)
951
530
    rank |= RF_BSS;
952
80.1k
953
80.1k
  // Some architectures have additional ordering restrictions for sections
954
80.1k
  // within the same PT_LOAD.
955
80.1k
  if (config->emachine == EM_PPC64) {
956
956
    // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
957
956
    // that we would like to make sure appear is a specific order to maximize
958
956
    // their coverage by a single signed 16-bit offset from the TOC base
959
956
    // pointer. Conversely, the special .tocbss section should be first among
960
956
    // all SHT_NOBITS sections. This will put it next to the loaded special
961
956
    // PPC64 sections (and, thus, within reach of the TOC base pointer).
962
956
    StringRef name = sec->name;
963
956
    if (name != ".tocbss")
964
956
      rank |= RF_PPC_NOT_TOCBSS;
965
956
966
956
    if (name == ".toc1")
967
0
      rank |= RF_PPC_TOCL;
968
956
969
956
    if (name == ".toc")
970
22
      rank |= RF_PPC_TOC;
971
956
972
956
    if (name == ".got")
973
92
      rank |= RF_PPC_GOT;
974
956
975
956
    if (name == ".branch_lt")
976
147
      rank |= RF_PPC_BRANCH_LT;
977
956
  }
978
80.1k
979
80.1k
  if (config->emachine == EM_MIPS) {
980
1.54k
    // All sections with SHF_MIPS_GPREL flag should be grouped together
981
1.54k
    // because data in these sections is addressable with a gp relative address.
982
1.54k
    if (sec->flags & SHF_MIPS_GPREL)
983
175
      rank |= RF_MIPS_GPREL;
984
1.54k
985
1.54k
    if (sec->name != ".got")
986
1.37k
      rank |= RF_MIPS_NOT_GOT;
987
1.54k
  }
988
80.1k
989
80.1k
  return rank;
990
80.1k
}
991
992
1.67M
static bool compareSections(const BaseCommand *aCmd, const BaseCommand *bCmd) {
993
1.67M
  const OutputSection *a = cast<OutputSection>(aCmd);
994
1.67M
  const OutputSection *b = cast<OutputSection>(bCmd);
995
1.67M
996
1.67M
  if (a->sortRank != b->sortRank)
997
94.1k
    return a->sortRank < b->sortRank;
998
1.58M
999
1.58M
  if (!(a->sortRank & RF_NOT_ADDR_SET))
1000
22
    return config->sectionStartMap.lookup(a->name) <
1001
22
           config->sectionStartMap.lookup(b->name);
1002
1.58M
  return false;
1003
1.58M
}
1004
1005
91.0k
void PhdrEntry::add(OutputSection *sec) {
1006
91.0k
  lastSec = sec;
1007
91.0k
  if (!firstSec)
1008
12.5k
    firstSec = sec;
1009
91.0k
  p_align = std::max(p_align, sec->alignment);
1010
91.0k
  if (p_type == PT_LOAD)
1011
85.1k
    sec->ptLoad = this;
1012
91.0k
}
1013
1014
// The beginning and the ending of .rel[a].plt section are marked
1015
// with __rel[a]_iplt_{start,end} symbols if it is a statically linked
1016
// executable. The runtime needs these symbols in order to resolve
1017
// all IRELATIVE relocs on startup. For dynamic executables, we don't
1018
// need these symbols, since IRELATIVE relocs are resolved through GOT
1019
// and PLT. For details, see http://www.airs.com/blog/archives/403.
1020
2.75k
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1021
2.75k
  if (config->relocatable || 
needsInterpSection()2.66k
)
1022
95
    return;
1023
2.65k
1024
2.65k
  // By default, __rela_iplt_{start,end} belong to a dummy section 0
1025
2.65k
  // because .rela.plt might be empty and thus removed from output.
1026
2.65k
  // We'll override Out::elfHeader with In.relaIplt later when we are
1027
2.65k
  // sure that .rela.plt exists in output.
1028
2.65k
  ElfSym::relaIpltStart = addOptionalRegular(
1029
2.65k
      config->isRela ? 
"__rela_iplt_start"2.18k
:
"__rel_iplt_start"473
,
1030
2.65k
      Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
1031
2.65k
1032
2.65k
  ElfSym::relaIpltEnd = addOptionalRegular(
1033
2.65k
      config->isRela ? 
"__rela_iplt_end"2.18k
:
"__rel_iplt_end"473
,
1034
2.65k
      Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
1035
2.65k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::addRelIpltSymbols()
Line
Count
Source
1020
376
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1021
376
  if (config->relocatable || 
needsInterpSection()365
)
1022
12
    return;
1023
364
1024
364
  // By default, __rela_iplt_{start,end} belong to a dummy section 0
1025
364
  // because .rela.plt might be empty and thus removed from output.
1026
364
  // We'll override Out::elfHeader with In.relaIplt later when we are
1027
364
  // sure that .rela.plt exists in output.
1028
364
  ElfSym::relaIpltStart = addOptionalRegular(
1029
364
      config->isRela ? 
"__rela_iplt_start"53
:
"__rel_iplt_start"311
,
1030
364
      Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
1031
364
1032
364
  ElfSym::relaIpltEnd = addOptionalRegular(
1033
364
      config->isRela ? 
"__rela_iplt_end"53
:
"__rel_iplt_end"311
,
1034
364
      Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
1035
364
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::addRelIpltSymbols()
Line
Count
Source
1020
157
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1021
157
  if (config->relocatable || 
needsInterpSection()146
)
1022
11
    return;
1023
146
1024
146
  // By default, __rela_iplt_{start,end} belong to a dummy section 0
1025
146
  // because .rela.plt might be empty and thus removed from output.
1026
146
  // We'll override Out::elfHeader with In.relaIplt later when we are
1027
146
  // sure that .rela.plt exists in output.
1028
146
  ElfSym::relaIpltStart = addOptionalRegular(
1029
146
      config->isRela ? 
"__rela_iplt_start"31
:
"__rel_iplt_start"115
,
1030
146
      Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
1031
146
1032
146
  ElfSym::relaIpltEnd = addOptionalRegular(
1033
146
      config->isRela ? 
"__rela_iplt_end"31
:
"__rel_iplt_end"115
,
1034
146
      Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
1035
146
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::addRelIpltSymbols()
Line
Count
Source
1020
2.11k
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1021
2.11k
  if (config->relocatable || 
needsInterpSection()2.05k
)
1022
68
    return;
1023
2.04k
1024
2.04k
  // By default, __rela_iplt_{start,end} belong to a dummy section 0
1025
2.04k
  // because .rela.plt might be empty and thus removed from output.
1026
2.04k
  // We'll override Out::elfHeader with In.relaIplt later when we are
1027
2.04k
  // sure that .rela.plt exists in output.
1028
2.04k
  ElfSym::relaIpltStart = addOptionalRegular(
1029
2.04k
      config->isRela ? 
"__rela_iplt_start"2.04k
:
"__rel_iplt_start"3
,
1030
2.04k
      Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
1031
2.04k
1032
2.04k
  ElfSym::relaIpltEnd = addOptionalRegular(
1033
2.04k
      config->isRela ? 
"__rela_iplt_end"2.04k
:
"__rel_iplt_end"3
,
1034
2.04k
      Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
1035
2.04k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addRelIpltSymbols()
Line
Count
Source
1020
108
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1021
108
  if (config->relocatable || 
needsInterpSection()105
)
1022
4
    return;
1023
104
1024
104
  // By default, __rela_iplt_{start,end} belong to a dummy section 0
1025
104
  // because .rela.plt might be empty and thus removed from output.
1026
104
  // We'll override Out::elfHeader with In.relaIplt later when we are
1027
104
  // sure that .rela.plt exists in output.
1028
104
  ElfSym::relaIpltStart = addOptionalRegular(
1029
104
      config->isRela ? 
"__rela_iplt_start"60
:
"__rel_iplt_start"44
,
1030
104
      Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
1031
104
1032
104
  ElfSym::relaIpltEnd = addOptionalRegular(
1033
104
      config->isRela ? 
"__rela_iplt_end"60
:
"__rel_iplt_end"44
,
1034
104
      Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
1035
104
}
1036
1037
template <class ELFT>
1038
void Writer<ELFT>::forEachRelSec(
1039
2.66k
    llvm::function_ref<void(InputSectionBase &)> fn) {
1040
2.66k
  // Scan all relocations. Each relocation goes through a series
1041
2.66k
  // of tests to determine if it needs special treatment, such as
1042
2.66k
  // creating GOT, PLT, copy relocations, etc.
1043
2.66k
  // Note that relocations for non-alloc sections are directly
1044
2.66k
  // processed by InputSection::relocateNonAlloc.
1045
2.66k
  for (InputSectionBase *isec : inputSections)
1046
322k
    if (isec->isLive() && 
isa<InputSection>(isec)322k
&&
(isec->flags & SHF_ALLOC)322k
)
1047
112k
      fn(*isec);
1048
2.68k
  for (Partition &part : partitions) {
1049
2.68k
    for (EhInputSection *es : part.ehFrame->sections)
1050
117
      fn(*es);
1051
2.68k
    if (part.armExidx && 
part.armExidx->isLive()157
)
1052
156
      for (InputSection *ex : part.armExidx->exidxSections)
1053
67
        fn(*ex);
1054
2.68k
  }
1055
2.66k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::forEachRelSec(llvm::function_ref<void (lld::elf::InputSectionBase&)>)
Line
Count
Source
1039
365
    llvm::function_ref<void(InputSectionBase &)> fn) {
1040
365
  // Scan all relocations. Each relocation goes through a series
1041
365
  // of tests to determine if it needs special treatment, such as
1042
365
  // creating GOT, PLT, copy relocations, etc.
1043
365
  // Note that relocations for non-alloc sections are directly
1044
365
  // processed by InputSection::relocateNonAlloc.
1045
365
  for (InputSectionBase *isec : inputSections)
1046
8.41k
    if (isec->isLive() && 
isa<InputSection>(isec)8.34k
&&
(isec->flags & SHF_ALLOC)8.34k
)
1047
6.44k
      fn(*isec);
1048
369
  for (Partition &part : partitions) {
1049
369
    for (EhInputSection *es : part.ehFrame->sections)
1050
4
      fn(*es);
1051
369
    if (part.armExidx && 
part.armExidx->isLive()157
)
1052
156
      for (InputSection *ex : part.armExidx->exidxSections)
1053
67
        fn(*ex);
1054
369
  }
1055
365
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::forEachRelSec(llvm::function_ref<void (lld::elf::InputSectionBase&)>)
Line
Count
Source
1039
146
    llvm::function_ref<void(InputSectionBase &)> fn) {
1040
146
  // Scan all relocations. Each relocation goes through a series
1041
146
  // of tests to determine if it needs special treatment, such as
1042
146
  // creating GOT, PLT, copy relocations, etc.
1043
146
  // Note that relocations for non-alloc sections are directly
1044
146
  // processed by InputSection::relocateNonAlloc.
1045
146
  for (InputSectionBase *isec : inputSections)
1046
3.95k
    if (isec->isLive() && 
isa<InputSection>(isec)3.64k
&&
(isec->flags & SHF_ALLOC)3.64k
)
1047
2.91k
      fn(*isec);
1048
146
  for (Partition &part : partitions) {
1049
146
    for (EhInputSection *es : part.ehFrame->sections)
1050
0
      fn(*es);
1051
146
    if (part.armExidx && 
part.armExidx->isLive()0
)
1052
0
      for (InputSection *ex : part.armExidx->exidxSections)
1053
0
        fn(*ex);
1054
146
  }
1055
146
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::forEachRelSec(llvm::function_ref<void (lld::elf::InputSectionBase&)>)
Line
Count
Source
1039
2.05k
    llvm::function_ref<void(InputSectionBase &)> fn) {
1040
2.05k
  // Scan all relocations. Each relocation goes through a series
1041
2.05k
  // of tests to determine if it needs special treatment, such as
1042
2.05k
  // creating GOT, PLT, copy relocations, etc.
1043
2.05k
  // Note that relocations for non-alloc sections are directly
1044
2.05k
  // processed by InputSection::relocateNonAlloc.
1045
2.05k
  for (InputSectionBase *isec : inputSections)
1046
307k
    if (isec->isLive() && 
isa<InputSection>(isec)307k
&&
(isec->flags & SHF_ALLOC)307k
)
1047
101k
      fn(*isec);
1048
2.06k
  for (Partition &part : partitions) {
1049
2.06k
    for (EhInputSection *es : part.ehFrame->sections)
1050
109
      fn(*es);
1051
2.06k
    if (part.armExidx && 
part.armExidx->isLive()0
)
1052
0
      for (InputSection *ex : part.armExidx->exidxSections)
1053
0
        fn(*ex);
1054
2.06k
  }
1055
2.05k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::forEachRelSec(llvm::function_ref<void (lld::elf::InputSectionBase&)>)
Line
Count
Source
1039
105
    llvm::function_ref<void(InputSectionBase &)> fn) {
1040
105
  // Scan all relocations. Each relocation goes through a series
1041
105
  // of tests to determine if it needs special treatment, such as
1042
105
  // creating GOT, PLT, copy relocations, etc.
1043
105
  // Note that relocations for non-alloc sections are directly
1044
105
  // processed by InputSection::relocateNonAlloc.
1045
105
  for (InputSectionBase *isec : inputSections)
1046
2.52k
    if (isec->isLive() && 
isa<InputSection>(isec)2.42k
&&
(isec->flags & SHF_ALLOC)2.42k
)
1047
1.90k
      fn(*isec);
1048
105
  for (Partition &part : partitions) {
1049
105
    for (EhInputSection *es : part.ehFrame->sections)
1050
4
      fn(*es);
1051
105
    if (part.armExidx && 
part.armExidx->isLive()0
)
1052
0
      for (InputSection *ex : part.armExidx->exidxSections)
1053
0
        fn(*ex);
1054
105
  }
1055
105
}
1056
1057
// This function generates assignments for predefined symbols (e.g. _end or
1058
// _etext) and inserts them into the commands sequence to be processed at the
1059
// appropriate time. This ensures that the value is going to be correct by the
1060
// time any references to these symbols are processed and is equivalent to
1061
// defining these symbols explicitly in the linker script.
1062
2.61k
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
1063
2.61k
  if (ElfSym::globalOffsetTable) {
1064
118
    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
1065
118
    // to the start of the .got or .got.plt section.
1066
118
    InputSection *gotSection = in.gotPlt;
1067
118
    if (!target->gotBaseSymInGotPlt)
1068
88
      gotSection = in.mipsGot ? 
cast<InputSection>(in.mipsGot)1
1069
88
                              : 
cast<InputSection>(in.got)87
;
1070
118
    ElfSym::globalOffsetTable->section = gotSection;
1071
118
  }
1072
2.61k
1073
2.61k
  // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
1074
2.61k
  if (ElfSym::relaIpltStart && 
in.relaIplt->isNeeded()8
) {
1075
7
    ElfSym::relaIpltStart->section = in.relaIplt;
1076
7
    ElfSym::relaIpltEnd->section = in.relaIplt;
1077
7
    ElfSym::relaIpltEnd->value = in.relaIplt->getSize();
1078
7
  }
1079
2.61k
1080
2.61k
  PhdrEntry *last = nullptr;
1081
2.61k
  PhdrEntry *lastRO = nullptr;
1082
2.61k
1083
2.62k
  for (Partition &part : partitions) {
1084
15.0k
    for (PhdrEntry *p : part.phdrs) {
1085
15.0k
      if (p->p_type != PT_LOAD)
1086
8.14k
        continue;
1087
6.89k
      last = p;
1088
6.89k
      if (!(p->p_flags & PF_W))
1089
4.61k
        lastRO = p;
1090
6.89k
    }
1091
2.62k
  }
1092
2.61k
1093
2.61k
  if (lastRO) {
1094
2.49k
    // _etext is the first location after the last read-only loadable segment.
1095
2.49k
    if (ElfSym::etext1)
1096
3
      ElfSym::etext1->section = lastRO->lastSec;
1097
2.49k
    if (ElfSym::etext2)
1098
1
      ElfSym::etext2->section = lastRO->lastSec;
1099
2.49k
  }
1100
2.61k
1101
2.61k
  if (last) {
1102
2.51k
    // _edata points to the end of the last mapped initialized section.
1103
2.51k
    OutputSection *edata = nullptr;
1104
80.3k
    for (OutputSection *os : outputSections) {
1105
80.3k
      if (os->type != SHT_NOBITS)
1106
79.8k
        edata = os;
1107
80.3k
      if (os == last->lastSec)
1108
2.50k
        break;
1109
80.3k
    }
1110
2.51k
1111
2.51k
    if (ElfSym::edata1)
1112
1
      ElfSym::edata1->section = edata;
1113
2.51k
    if (ElfSym::edata2)
1114
5
      ElfSym::edata2->section = edata;
1115
2.51k
1116
2.51k
    // _end is the first location after the uninitialized data region.
1117
2.51k
    if (ElfSym::end1)
1118
4
      ElfSym::end1->section = last->lastSec;
1119
2.51k
    if (ElfSym::end2)
1120
11
      ElfSym::end2->section = last->lastSec;
1121
2.51k
  }
1122
2.61k
1123
2.61k
  if (ElfSym::bss)
1124
4
    ElfSym::bss->section = findSection(".bss");
1125
2.61k
1126
2.61k
  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1127
2.61k
  // be equal to the _gp symbol's value.
1128
2.61k
  if (ElfSym::mipsGp) {
1129
173
    // Find GP-relative section with the lowest address
1130
173
    // and use this address to calculate default _gp value.
1131
1.38k
    for (OutputSection *os : outputSections) {
1132
1.38k
      if (os->flags & SHF_MIPS_GPREL) {
1133
173
        ElfSym::mipsGp->section = os;
1134
173
        ElfSym::mipsGp->value = 0x7ff0;
1135
173
        break;
1136
173
      }
1137
1.38k
    }
1138
173
  }
1139
2.61k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::setReservedSymbolSections()
Line
Count
Source
1062
367
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
1063
367
  if (ElfSym::globalOffsetTable) {
1064
12
    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
1065
12
    // to the start of the .got or .got.plt section.
1066
12
    InputSection *gotSection = in.gotPlt;
1067
12
    if (!target->gotBaseSymInGotPlt)
1068
5
      gotSection = in.mipsGot ? 
cast<InputSection>(in.mipsGot)0
1069
5
                              : cast<InputSection>(in.got);
1070
12
    ElfSym::globalOffsetTable->section = gotSection;
1071
12
  }
1072
367
1073
367
  // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
1074
367
  if (ElfSym::relaIpltStart && 
in.relaIplt->isNeeded()2
) {
1075
2
    ElfSym::relaIpltStart->section = in.relaIplt;
1076
2
    ElfSym::relaIpltEnd->section = in.relaIplt;
1077
2
    ElfSym::relaIpltEnd->value = in.relaIplt->getSize();
1078
2
  }
1079
367
1080
367
  PhdrEntry *last = nullptr;
1081
367
  PhdrEntry *lastRO = nullptr;
1082
367
1083
370
  for (Partition &part : partitions) {
1084
2.16k
    for (PhdrEntry *p : part.phdrs) {
1085
2.16k
      if (p->p_type != PT_LOAD)
1086
1.18k
        continue;
1087
986
      last = p;
1088
986
      if (!(p->p_flags & PF_W))
1089
673
        lastRO = p;
1090
986
    }
1091
370
  }
1092
367
1093
367
  if (lastRO) {
1094
353
    // _etext is the first location after the last read-only loadable segment.
1095
353
    if (ElfSym::etext1)
1096
0
      ElfSym::etext1->section = lastRO->lastSec;
1097
353
    if (ElfSym::etext2)
1098
0
      ElfSym::etext2->section = lastRO->lastSec;
1099
353
  }
1100
367
1101
367
  if (last) {
1102
355
    // _edata points to the end of the last mapped initialized section.
1103
355
    OutputSection *edata = nullptr;
1104
1.84k
    for (OutputSection *os : outputSections) {
1105
1.84k
      if (os->type != SHT_NOBITS)
1106
1.77k
        edata = os;
1107
1.84k
      if (os == last->lastSec)
1108
353
        break;
1109
1.84k
    }
1110
355
1111
355
    if (ElfSym::edata1)
1112
0
      ElfSym::edata1->section = edata;
1113
355
    if (ElfSym::edata2)
1114
0
      ElfSym::edata2->section = edata;
1115
355
1116
355
    // _end is the first location after the uninitialized data region.
1117
355
    if (ElfSym::end1)
1118
0
      ElfSym::end1->section = last->lastSec;
1119
355
    if (ElfSym::end2)
1120
0
      ElfSym::end2->section = last->lastSec;
1121
355
  }
1122
367
1123
367
  if (ElfSym::bss)
1124
0
    ElfSym::bss->section = findSection(".bss");
1125
367
1126
367
  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1127
367
  // be equal to the _gp symbol's value.
1128
367
  if (ElfSym::mipsGp) {
1129
22
    // Find GP-relative section with the lowest address
1130
22
    // and use this address to calculate default _gp value.
1131
148
    for (OutputSection *os : outputSections) {
1132
148
      if (os->flags & SHF_MIPS_GPREL) {
1133
22
        ElfSym::mipsGp->section = os;
1134
22
        ElfSym::mipsGp->value = 0x7ff0;
1135
22
        break;
1136
22
      }
1137
148
    }
1138
22
  }
1139
367
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::setReservedSymbolSections()
Line
Count
Source
1062
154
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
1063
154
  if (ElfSym::globalOffsetTable) {
1064
4
    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
1065
4
    // to the start of the .got or .got.plt section.
1066
4
    InputSection *gotSection = in.gotPlt;
1067
4
    if (!target->gotBaseSymInGotPlt)
1068
4
      gotSection = in.mipsGot ? 
cast<InputSection>(in.mipsGot)0
1069
4
                              : cast<InputSection>(in.got);
1070
4
    ElfSym::globalOffsetTable->section = gotSection;
1071
4
  }
1072
154
1073
154
  // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
1074
154
  if (ElfSym::relaIpltStart && 
in.relaIplt->isNeeded()1
) {
1075
1
    ElfSym::relaIpltStart->section = in.relaIplt;
1076
1
    ElfSym::relaIpltEnd->section = in.relaIplt;
1077
1
    ElfSym::relaIpltEnd->value = in.relaIplt->getSize();
1078
1
  }
1079
154
1080
154
  PhdrEntry *last = nullptr;
1081
154
  PhdrEntry *lastRO = nullptr;
1082
154
1083
154
  for (Partition &part : partitions) {
1084
1.05k
    for (PhdrEntry *p : part.phdrs) {
1085
1.05k
      if (p->p_type != PT_LOAD)
1086
632
        continue;
1087
422
      last = p;
1088
422
      if (!(p->p_flags & PF_W))
1089
276
        lastRO = p;
1090
422
    }
1091
154
  }
1092
154
1093
154
  if (lastRO) {
1094
143
    // _etext is the first location after the last read-only loadable segment.
1095
143
    if (ElfSym::etext1)
1096
0
      ElfSym::etext1->section = lastRO->lastSec;
1097
143
    if (ElfSym::etext2)
1098
0
      ElfSym::etext2->section = lastRO->lastSec;
1099
143
  }
1100
154
1101
154
  if (last) {
1102
143
    // _edata points to the end of the last mapped initialized section.
1103
143
    OutputSection *edata = nullptr;
1104
1.19k
    for (OutputSection *os : outputSections) {
1105
1.19k
      if (os->type != SHT_NOBITS)
1106
1.08k
        edata = os;
1107
1.19k
      if (os == last->lastSec)
1108
143
        break;
1109
1.19k
    }
1110
143
1111
143
    if (ElfSym::edata1)
1112
0
      ElfSym::edata1->section = edata;
1113
143
    if (ElfSym::edata2)
1114
1
      ElfSym::edata2->section = edata;
1115
143
1116
143
    // _end is the first location after the uninitialized data region.
1117
143
    if (ElfSym::end1)
1118
0
      ElfSym::end1->section = last->lastSec;
1119
143
    if (ElfSym::end2)
1120
1
      ElfSym::end2->section = last->lastSec;
1121
143
  }
1122
154
1123
154
  if (ElfSym::bss)
1124
1
    ElfSym::bss->section = findSection(".bss");
1125
154
1126
154
  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1127
154
  // be equal to the _gp symbol's value.
1128
154
  if (ElfSym::mipsGp) {
1129
112
    // Find GP-relative section with the lowest address
1130
112
    // and use this address to calculate default _gp value.
1131
910
    for (OutputSection *os : outputSections) {
1132
910
      if (os->flags & SHF_MIPS_GPREL) {
1133
112
        ElfSym::mipsGp->section = os;
1134
112
        ElfSym::mipsGp->value = 0x7ff0;
1135
112
        break;
1136
112
      }
1137
910
    }
1138
112
  }
1139
154
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::setReservedSymbolSections()
Line
Count
Source
1062
1.98k
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
1063
1.98k
  if (ElfSym::globalOffsetTable) {
1064
70
    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
1065
70
    // to the start of the .got or .got.plt section.
1066
70
    InputSection *gotSection = in.gotPlt;
1067
70
    if (!target->gotBaseSymInGotPlt)
1068
47
      gotSection = in.mipsGot ? 
cast<InputSection>(in.mipsGot)0
1069
47
                              : cast<InputSection>(in.got);
1070
70
    ElfSym::globalOffsetTable->section = gotSection;
1071
70
  }
1072
1.98k
1073
1.98k
  // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
1074
1.98k
  if (ElfSym::relaIpltStart && 
in.relaIplt->isNeeded()5
) {
1075
4
    ElfSym::relaIpltStart->section = in.relaIplt;
1076
4
    ElfSym::relaIpltEnd->section = in.relaIplt;
1077
4
    ElfSym::relaIpltEnd->value = in.relaIplt->getSize();
1078
4
  }
1079
1.98k
1080
1.98k
  PhdrEntry *last = nullptr;
1081
1.98k
  PhdrEntry *lastRO = nullptr;
1082
1.98k
1083
1.99k
  for (Partition &part : partitions) {
1084
11.0k
    for (PhdrEntry *p : part.phdrs) {
1085
11.0k
      if (p->p_type != PT_LOAD)
1086
5.94k
        continue;
1087
5.14k
      last = p;
1088
5.14k
      if (!(p->p_flags & PF_W))
1089
3.47k
        lastRO = p;
1090
5.14k
    }
1091
1.99k
  }
1092
1.98k
1093
1.98k
  if (lastRO) {
1094
1.90k
    // _etext is the first location after the last read-only loadable segment.
1095
1.90k
    if (ElfSym::etext1)
1096
3
      ElfSym::etext1->section = lastRO->lastSec;
1097
1.90k
    if (ElfSym::etext2)
1098
1
      ElfSym::etext2->section = lastRO->lastSec;
1099
1.90k
  }
1100
1.98k
1101
1.98k
  if (last) {
1102
1.91k
    // _edata points to the end of the last mapped initialized section.
1103
1.91k
    OutputSection *edata = nullptr;
1104
76.5k
    for (OutputSection *os : outputSections) {
1105
76.5k
      if (os->type != SHT_NOBITS)
1106
76.2k
        edata = os;
1107
76.5k
      if (os == last->lastSec)
1108
1.90k
        break;
1109
76.5k
    }
1110
1.91k
1111
1.91k
    if (ElfSym::edata1)
1112
1
      ElfSym::edata1->section = edata;
1113
1.91k
    if (ElfSym::edata2)
1114
4
      ElfSym::edata2->section = edata;
1115
1.91k
1116
1.91k
    // _end is the first location after the uninitialized data region.
1117
1.91k
    if (ElfSym::end1)
1118
4
      ElfSym::end1->section = last->lastSec;
1119
1.91k
    if (ElfSym::end2)
1120
10
      ElfSym::end2->section = last->lastSec;
1121
1.91k
  }
1122
1.98k
1123
1.98k
  if (ElfSym::bss)
1124
3
    ElfSym::bss->section = findSection(".bss");
1125
1.98k
1126
1.98k
  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1127
1.98k
  // be equal to the _gp symbol's value.
1128
1.98k
  if (ElfSym::mipsGp) {
1129
3
    // Find GP-relative section with the lowest address
1130
3
    // and use this address to calculate default _gp value.
1131
15
    for (OutputSection *os : outputSections) {
1132
15
      if (os->flags & SHF_MIPS_GPREL) {
1133
3
        ElfSym::mipsGp->section = os;
1134
3
        ElfSym::mipsGp->value = 0x7ff0;
1135
3
        break;
1136
3
      }
1137
15
    }
1138
3
  }
1139
1.98k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::setReservedSymbolSections()
Line
Count
Source
1062
101
template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
1063
101
  if (ElfSym::globalOffsetTable) {
1064
32
    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
1065
32
    // to the start of the .got or .got.plt section.
1066
32
    InputSection *gotSection = in.gotPlt;
1067
32
    if (!target->gotBaseSymInGotPlt)
1068
32
      gotSection = in.mipsGot ? 
cast<InputSection>(in.mipsGot)1
1069
32
                              : 
cast<InputSection>(in.got)31
;
1070
32
    ElfSym::globalOffsetTable->section = gotSection;
1071
32
  }
1072
101
1073
101
  // .rela_iplt_{start,end} mark the start and the end of .rela.plt section.
1074
101
  if (ElfSym::relaIpltStart && 
in.relaIplt->isNeeded()0
) {
1075
0
    ElfSym::relaIpltStart->section = in.relaIplt;
1076
0
    ElfSym::relaIpltEnd->section = in.relaIplt;
1077
0
    ElfSym::relaIpltEnd->value = in.relaIplt->getSize();
1078
0
  }
1079
101
1080
101
  PhdrEntry *last = nullptr;
1081
101
  PhdrEntry *lastRO = nullptr;
1082
101
1083
101
  for (Partition &part : partitions) {
1084
737
    for (PhdrEntry *p : part.phdrs) {
1085
737
      if (p->p_type != PT_LOAD)
1086
391
        continue;
1087
346
      last = p;
1088
346
      if (!(p->p_flags & PF_W))
1089
192
        lastRO = p;
1090
346
    }
1091
101
  }
1092
101
1093
101
  if (lastRO) {
1094
98
    // _etext is the first location after the last read-only loadable segment.
1095
98
    if (ElfSym::etext1)
1096
0
      ElfSym::etext1->section = lastRO->lastSec;
1097
98
    if (ElfSym::etext2)
1098
0
      ElfSym::etext2->section = lastRO->lastSec;
1099
98
  }
1100
101
1101
101
  if (last) {
1102
98
    // _edata points to the end of the last mapped initialized section.
1103
98
    OutputSection *edata = nullptr;
1104
730
    for (OutputSection *os : outputSections) {
1105
730
      if (os->type != SHT_NOBITS)
1106
655
        edata = os;
1107
730
      if (os == last->lastSec)
1108
98
        break;
1109
730
    }
1110
98
1111
98
    if (ElfSym::edata1)
1112
0
      ElfSym::edata1->section = edata;
1113
98
    if (ElfSym::edata2)
1114
0
      ElfSym::edata2->section = edata;
1115
98
1116
98
    // _end is the first location after the uninitialized data region.
1117
98
    if (ElfSym::end1)
1118
0
      ElfSym::end1->section = last->lastSec;
1119
98
    if (ElfSym::end2)
1120
0
      ElfSym::end2->section = last->lastSec;
1121
98
  }
1122
101
1123
101
  if (ElfSym::bss)
1124
0
    ElfSym::bss->section = findSection(".bss");
1125
101
1126
101
  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1127
101
  // be equal to the _gp symbol's value.
1128
101
  if (ElfSym::mipsGp) {
1129
36
    // Find GP-relative section with the lowest address
1130
36
    // and use this address to calculate default _gp value.
1131
307
    for (OutputSection *os : outputSections) {
1132
307
      if (os->flags & SHF_MIPS_GPREL) {
1133
36
        ElfSym::mipsGp->section = os;
1134
36
        ElfSym::mipsGp->value = 0x7ff0;
1135
36
        break;
1136
36
      }
1137
307
    }
1138
36
  }
1139
101
}
1140
1141
// We want to find how similar two ranks are.
1142
// The more branches in getSectionRank that match, the more similar they are.
1143
// Since each branch corresponds to a bit flag, we can just use
1144
// countLeadingZeros.
1145
401k
static int getRankProximityAux(OutputSection *a, OutputSection *b) {
1146
401k
  return countLeadingZeros(a->sortRank ^ b->sortRank);
1147
401k
}
1148
1149
401k
static int getRankProximity(OutputSection *a, BaseCommand *b) {
1150
401k
  auto *sec = dyn_cast<OutputSection>(b);
1151
401k
  return (sec && 
sec->hasInputSections401k
) ?
getRankProximityAux(a, sec)401k
:
-1346
;
1152
401k
}
1153
1154
// When placing orphan sections, we want to place them after symbol assignments
1155
// so that an orphan after
1156
//   begin_foo = .;
1157
//   foo : { *(foo) }
1158
//   end_foo = .;
1159
// doesn't break the intended meaning of the begin/end symbols.
1160
// We don't want to go over sections since findOrphanPos is the
1161
// one in charge of deciding the order of the sections.
1162
// We don't want to go over changes to '.', since doing so in
1163
//  rx_sec : { *(rx_sec) }
1164
//  . = ALIGN(0x1000);
1165
//  /* The RW PT_LOAD starts here*/
1166
//  rw_sec : { *(rw_sec) }
1167
// would mean that the RW PT_LOAD would become unaligned.
1168
736
static bool shouldSkip(BaseCommand *cmd) {
1169
736
  if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
1170
216
    return assign->name != ".";
1171
520
  return false;
1172
520
}
1173
1174
// We want to place orphan sections so that they share as much
1175
// characteristics with their neighbors as possible. For example, if
1176
// both are rw, or both are tls.
1177
static std::vector<BaseCommand *>::iterator
1178
findOrphanPos(std::vector<BaseCommand *>::iterator b,
1179
1.09k
              std::vector<BaseCommand *>::iterator e) {
1180
1.09k
  OutputSection *sec = cast<OutputSection>(*e);
1181
1.09k
1182
1.09k
  // Find the first element that has as close a rank as possible.
1183
133k
  auto i = std::max_element(b, e, [=](BaseCommand *a, BaseCommand *b) {
1184
133k
    return getRankProximity(sec, a) < getRankProximity(sec, b);
1185
133k
  });
1186
1.09k
  if (i == e)
1187
73
    return e;
1188
1.01k
1189
1.01k
  // Consider all existing sections with the same proximity.
1190
1.01k
  int proximity = getRankProximity(sec, *i);
1191
134k
  for (; i != e; 
++i133k
) {
1192
133k
    auto *curSec = dyn_cast<OutputSection>(*i);
1193
133k
    if (!curSec || 
!curSec->hasInputSections133k
)
1194
201
      continue;
1195
133k
    if (getRankProximity(sec, curSec) != proximity ||
1196
133k
        
sec->sortRank < curSec->sortRank133k
)
1197
190
      break;
1198
133k
  }
1199
1.01k
1200
1.29k
  auto isOutputSecWithInputSections = [](BaseCommand *cmd) {
1201
1.29k
    auto *os = dyn_cast<OutputSection>(cmd);
1202
1.29k
    return os && 
os->hasInputSections1.12k
;
1203
1.29k
  };
1204
1.01k
  auto j = std::find_if(llvm::make_reverse_iterator(i),
1205
1.01k
                        llvm::make_reverse_iterator(b),
1206
1.01k
                        isOutputSecWithInputSections);
1207
1.01k
  i = j.base();
1208
1.01k
1209
1.01k
  // As a special case, if the orphan section is the last section, put
1210
1.01k
  // it at the very end, past any other commands.
1211
1.01k
  // This matches bfd's behavior and is convenient when the linker script fully
1212
1.01k
  // specifies the start of the file, but doesn't care about the end (the non
1213
1.01k
  // alloc sections for example).
1214
1.01k
  auto nextSec = std::find_if(i, e, isOutputSecWithInputSections);
1215
1.01k
  if (nextSec == e)
1216
828
    return e;
1217
190
1218
196
  
while (190
i != e && shouldSkip(*i))
1219
6
    ++i;
1220
190
  return i;
1221
190
}
1222
1223
// Builds section order for handling --symbol-ordering-file.
1224
2.52k
static DenseMap<const InputSectionBase *, int> buildSectionOrder() {
1225
2.52k
  DenseMap<const InputSectionBase *, int> sectionOrder;
1226
2.52k
  // Use the rarely used option -call-graph-ordering-file to sort sections.
1227
2.52k
  if (!config->callGraphProfile.empty())
1228
11
    return computeCallGraphProfileOrder();
1229
2.51k
1230
2.51k
  if (config->symbolOrderingFile.empty())
1231
2.47k
    return sectionOrder;
1232
33
1233
33
  struct SymbolOrderEntry {
1234
33
    int priority;
1235
33
    bool present;
1236
33
  };
1237
33
1238
33
  // Build a map from symbols to their priorities. Symbols that didn't
1239
33
  // appear in the symbol ordering file have the lowest priority 0.
1240
33
  // All explicitly mentioned symbols have negative (higher) priorities.
1241
33
  DenseMap<StringRef, SymbolOrderEntry> symbolOrder;
1242
33
  int priority = -config->symbolOrderingFile.size();
1243
33
  for (StringRef s : config->symbolOrderingFile)
1244
72
    symbolOrder.insert({s, {priority++, false}});
1245
33
1246
33
  // Build a map from sections to their priorities.
1247
288
  auto addSym = [&](Symbol &sym) {
1248
288
    auto it = symbolOrder.find(sym.getName());
1249
288
    if (it == symbolOrder.end())
1250
219
      return;
1251
69
    SymbolOrderEntry &ent = it->second;
1252
69
    ent.present = true;
1253
69
1254
69
    maybeWarnUnorderableSymbol(&sym);
1255
69
1256
69
    if (auto *d = dyn_cast<Defined>(&sym)) {
1257
65
      if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section)) {
1258
58
        int &priority = sectionOrder[cast<InputSectionBase>(sec->repl)];
1259
58
        priority = std::min(priority, ent.priority);
1260
58
      }
1261
65
    }
1262
69
  };
1263
33
1264
33
  // We want both global and local symbols. We get the global ones from the
1265
33
  // symbol table and iterate the object files for the local ones.
1266
177
  symtab->forEachSymbol([&](Symbol *sym) {
1267
177
    if (!sym->isLazy())
1268
176
      addSym(*sym);
1269
177
  });
1270
33
1271
33
  for (InputFile *file : objectFiles)
1272
41
    for (Symbol *sym : file->getSymbols())
1273
299
      if (sym->isLocal())
1274
112
        addSym(*sym);
1275
33
1276
33
  if (config->warnSymbolOrdering)
1277
32
    for (auto orderEntry : symbolOrder)
1278
71
      if (!orderEntry.second.present)
1279
4
        warn("symbol ordering file: no such symbol: " + orderEntry.first);
1280
33
1281
33
  return sectionOrder;
1282
33
}
1283
1284
// Sorts the sections in ISD according to the provided section order.
1285
static void
1286
sortISDBySectionOrder(InputSectionDescription *isd,
1287
212
                      const DenseMap<const InputSectionBase *, int> &order) {
1288
212
  std::vector<InputSection *> unorderedSections;
1289
212
  std::vector<std::pair<InputSection *, int>> orderedSections;
1290
212
  uint64_t unorderedSize = 0;
1291
212
1292
360
  for (InputSection *isec : isd->sections) {
1293
360
    auto i = order.find(isec);
1294
360
    if (i == order.end()) {
1295
259
      unorderedSections.push_back(isec);
1296
259
      unorderedSize += isec->getSize();
1297
259
      continue;
1298
259
    }
1299
101
    orderedSections.push_back({isec, i->second});
1300
101
  }
1301
212
  llvm::sort(orderedSections, [&](std::pair<InputSection *, int> a,
1302
212
                                  std::pair<InputSection *, int> b) {
1303
130
    return a.second < b.second;
1304
130
  });
1305
212
1306
212
  // Find an insertion point for the ordered section list in the unordered
1307
212
  // section list. On targets with limited-range branches, this is the mid-point
1308
212
  // of the unordered section list. This decreases the likelihood that a range
1309
212
  // extension thunk will be needed to enter or exit the ordered region. If the
1310
212
  // ordered section list is a list of hot functions, we can generally expect
1311
212
  // the ordered functions to be called more often than the unordered functions,
1312
212
  // making it more likely that any particular call will be within range, and
1313
212
  // therefore reducing the number of thunks required.
1314
212
  //
1315
212
  // For example, imagine that you have 8MB of hot code and 32MB of cold code.
1316
212
  // If the layout is:
1317
212
  //
1318
212
  // 8MB hot
1319
212
  // 32MB cold
1320
212
  //
1321
212
  // only the first 8-16MB of the cold code (depending on which hot function it
1322
212
  // is actually calling) can call the hot code without a range extension thunk.
1323
212
  // However, if we use this layout:
1324
212
  //
1325
212
  // 16MB cold
1326
212
  // 8MB hot
1327
212
  // 16MB cold
1328
212
  //
1329
212
  // both the last 8-16MB of the first block of cold code and the first 8-16MB
1330
212
  // of the second block of cold code can call the hot code without a thunk. So
1331
212
  // we effectively double the amount of code that could potentially call into
1332
212
  // the hot code without a thunk.
1333
212
  size_t insPt = 0;
1334
212
  if (target->getThunkSectionSpacing() && 
!orderedSections.empty()6
) {
1335
1
    uint64_t unorderedPos = 0;
1336
4
    for (; insPt != unorderedSections.size(); 
++insPt3
) {
1337
4
      unorderedPos += unorderedSections[insPt]->getSize();
1338
4
      if (unorderedPos > unorderedSize / 2)
1339
1
        break;
1340
4
    }
1341
1
  }
1342
212
1343
212
  isd->sections.clear();
1344
212
  for (InputSection *isec : makeArrayRef(unorderedSections).slice(0, insPt))
1345
3
    isd->sections.push_back(isec);
1346
212
  for (std::pair<InputSection *, int> p : orderedSections)
1347
101
    isd->sections.push_back(p.first);
1348
212
  for (InputSection *isec : makeArrayRef(unorderedSections).slice(insPt))
1349
256
    isd->sections.push_back(isec);
1350
212
}
1351
1352
static void sortSection(OutputSection *sec,
1353
286k
                        const DenseMap<const InputSectionBase *, int> &order) {
1354
286k
  StringRef name = sec->name;
1355
286k
1356
286k
  // Sort input sections by section name suffixes for
1357
286k
  // __attribute__((init_priority(N))).
1358
286k
  if (name == ".init_array" || 
name == ".fini_array"286k
) {
1359
12
    if (!script->hasSectionsCommand)
1360
10
      sec->sortInitFini();
1361
12
    return;
1362
12
  }
1363
286k
1364
286k
  // Sort input sections by the special rule for .ctors and .dtors.
1365
286k
  if (name == ".ctors" || 
name == ".dtors"286k
) {
1366
8
    if (!script->hasSectionsCommand)
1367
8
      sec->sortCtorsDtors();
1368
8
    return;
1369
8
  }
1370
286k
1371
286k
  // Never sort these.
1372
286k
  if (name == ".init" || 
name == ".fini"286k
)
1373
15
    return;
1374
286k
1375
286k
  // .toc is allocated just after .got and is accessed using GOT-relative
1376
286k
  // relocations. Object files compiled with small code model have an
1377
286k
  // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations.
1378
286k
  // To reduce the risk of relocation overflow, .toc contents are sorted so that
1379
286k
  // sections having smaller relocation offsets are at beginning of .toc
1380
286k
  if (config->emachine == EM_PPC64 && 
name == ".toc"1.54k
) {
1381
22
    if (script->hasSectionsCommand)
1382
2
      return;
1383
20
    assert(sec->sectionCommands.size() == 1);
1384
20
    auto *isd = cast<InputSectionDescription>(sec->sectionCommands[0]);
1385
20
    llvm::stable_sort(isd->sections,
1386
20
                      [](const InputSection *a, const InputSection *b) -> bool {
1387
19
                        return a->file->ppc64SmallCodeModelTocRelocs &&
1388
19
                               
!b->file->ppc64SmallCodeModelTocRelocs13
;
1389
19
                      });
1390
20
    return;
1391
20
  }
1392
286k
1393
286k
  // Sort input sections by priority using the list provided
1394
286k
  // by --symbol-ordering-file.
1395
286k
  if (!order.empty())
1396
212
    for (BaseCommand *b : sec->sectionCommands)
1397
214
      if (auto *isd = dyn_cast<InputSectionDescription>(b))
1398
212
        sortISDBySectionOrder(isd, order);
1399
286k
}
1400
1401
// If no layout was provided by linker script, we want to apply default
1402
// sorting for special input sections. This also handles --symbol-ordering-file.
1403
2.52k
template <class ELFT> void Writer<ELFT>::sortInputSections() {
1404
2.52k
  // Build the order once since it is expensive.
1405
2.52k
  DenseMap<const InputSectionBase *, int> order = buildSectionOrder();
1406
2.52k
  for (BaseCommand *base : script->sectionCommands)
1407
287k
    if (auto *sec = dyn_cast<OutputSection>(base))
1408
286k
      sortSection(sec, order);
1409
2.52k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::sortInputSections()
Line
Count
Source
1403
356
template <class ELFT> void Writer<ELFT>::sortInputSections() {
1404
356
  // Build the order once since it is expensive.
1405
356
  DenseMap<const InputSectionBase *, int> order = buildSectionOrder();
1406
356
  for (BaseCommand *base : script->sectionCommands)
1407
3.40k
    if (auto *sec = dyn_cast<OutputSection>(base))
1408
3.34k
      sortSection(sec, order);
1409
356
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortInputSections()
Line
Count
Source
1403
143
template <class ELFT> void Writer<ELFT>::sortInputSections() {
1404
143
  // Build the order once since it is expensive.
1405
143
  DenseMap<const InputSectionBase *, int> order = buildSectionOrder();
1406
143
  for (BaseCommand *base : script->sectionCommands)
1407
1.77k
    if (auto *sec = dyn_cast<OutputSection>(base))
1408
1.77k
      sortSection(sec, order);
1409
143
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::sortInputSections()
Line
Count
Source
1403
1.92k
template <class ELFT> void Writer<ELFT>::sortInputSections() {
1404
1.92k
  // Build the order once since it is expensive.
1405
1.92k
  DenseMap<const InputSectionBase *, int> order = buildSectionOrder();
1406
1.92k
  for (BaseCommand *base : script->sectionCommands)
1407
280k
    if (auto *sec = dyn_cast<OutputSection>(base))
1408
280k
      sortSection(sec, order);
1409
1.92k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::sortInputSections()
Line
Count
Source
1403
98
template <class ELFT> void Writer<ELFT>::sortInputSections() {
1404
98
  // Build the order once since it is expensive.
1405
98
  DenseMap<const InputSectionBase *, int> order = buildSectionOrder();
1406
98
  for (BaseCommand *base : script->sectionCommands)
1407
1.14k
    if (auto *sec = dyn_cast<OutputSection>(base))
1408
1.12k
      sortSection(sec, order);
1409
98
}
1410
1411
2.61k
template <class ELFT> void Writer<ELFT>::sortSections() {
1412
2.61k
  script->adjustSectionsBeforeSorting();
1413
2.61k
1414
2.61k
  // Don't sort if using -r. It is not necessary and we want to preserve the
1415
2.61k
  // relative order for SHF_LINK_ORDER sections.
1416
2.61k
  if (config->relocatable)
1417
87
    return;
1418
2.52k
1419
2.52k
  sortInputSections();
1420
2.52k
1421
287k
  for (BaseCommand *base : script->sectionCommands) {
1422
287k
    auto *os = dyn_cast<OutputSection>(base);
1423
287k
    if (!os)
1424
493
      continue;
1425
286k
    os->sortRank = getSectionRank(os);
1426
286k
1427
286k
    // We want to assign rude approximation values to outSecOff fields
1428
286k
    // to know the relative order of the input sections. We use it for
1429
286k
    // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
1430
286k
    uint64_t i = 0;
1431
286k
    for (InputSection *sec : getInputSections(os))
1432
288k
      sec->outSecOff = i++;
1433
286k
  }
1434
2.52k
1435
2.52k
  if (!script->hasSectionsCommand) {
1436
2.07k
    // We know that all the OutputSections are contiguous in this case.
1437
4.32k
    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
1437
673
    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
1437
270
    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
1437
3.17k
    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
1437
206
    auto isSection = [](BaseCommand *base) { return isa<OutputSection>(base); };
1438
2.07k
    std::stable_sort(
1439
2.07k
        llvm::find_if(script->sectionCommands, isSection),
1440
2.07k
        llvm::find_if(llvm::reverse(script->sectionCommands), isSection).base(),
1441
2.07k
        compareSections);
1442
2.07k
    return;
1443
2.07k
  }
1444
448
1445
448
  // Orphan sections are sections present in the input files which are
1446
448
  // not explicitly placed into the output file by the linker script.
1447
448
  //
1448
448
  // The sections in the linker script are already in the correct
1449
448
  // order. We have to figuere out where to insert the orphan
1450
448
  // sections.
1451
448
  //
1452
448
  // The order of the sections in the script is arbitrary and may not agree with
1453
448
  // compareSections. This means that we cannot easily define a strict weak
1454
448
  // ordering. To see why, consider a comparison of a section in the script and
1455
448
  // one not in the script. We have a two simple options:
1456
448
  // * Make them equivalent (a is not less than b, and b is not less than a).
1457
448
  //   The problem is then that equivalence has to be transitive and we can
1458
448
  //   have sections a, b and c with only b in a script and a less than c
1459
448
  //   which breaks this property.
1460
448
  // * Use compareSectionsNonScript. Given that the script order doesn't have
1461
448
  //   to match, we can end up with sections a, b, c, d where b and c are in the
1462
448
  //   script and c is compareSectionsNonScript less than b. In which case d
1463
448
  //   can be equivalent to c, a to b and d < a. As a concrete example:
1464
448
  //   .a (rx) # not in script
1465
448
  //   .b (rx) # in script
1466
448
  //   .c (ro) # in script
1467
448
  //   .d (ro) # not in script
1468
448
  //
1469
448
  // The way we define an order then is:
1470
448
  // *  Sort only the orphan sections. They are in the end right now.
1471
448
  // *  Move each orphan section to its preferred position. We try
1472
448
  //    to put each section in the last position where it can share
1473
448
  //    a PT_LOAD.
1474
448
  //
1475
448
  // There is some ambiguity as to where exactly a new entry should be
1476
448
  // inserted, because Commands contains not only output section
1477
448
  // commands but also other types of commands such as symbol assignment
1478
448
  // expressions. There's no correct answer here due to the lack of the
1479
448
  // formal specification of the linker script. We use heuristics to
1480
448
  // determine whether a new output command should be added before or
1481
448
  // after another commands. For the details, look at shouldSkip
1482
448
  // function.
1483
448
1484
448
  auto i = script->sectionCommands.begin();
1485
448
  auto e = script->sectionCommands.end();
1486
1.46k
  auto nonScriptI = std::find_if(i, e, [](BaseCommand *base) {
1487
1.46k
    if (auto *sec = dyn_cast<OutputSection>(base))
1488
1.14k
      return sec->sectionIndex == UINT32_MAX;
1489
319
    return false;
1490
319
  });
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
1486
170
  auto nonScriptI = std::find_if(i, e, [](BaseCommand *base) {
1487
170
    if (auto *sec = dyn_cast<OutputSection>(base))
1488
152
      return sec->sectionIndex == UINT32_MAX;
1489
18
    return false;
1490
18
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortSections()::'lambda0'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1486
25
  auto nonScriptI = std::find_if(i, e, [](BaseCommand *base) {
1487
25
    if (auto *sec = dyn_cast<OutputSection>(base))
1488
22
      return sec->sectionIndex == UINT32_MAX;
1489
3
    return false;
1490
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
1486
1.25k
  auto nonScriptI = std::find_if(i, e, [](BaseCommand *base) {
1487
1.25k
    if (auto *sec = dyn_cast<OutputSection>(base))
1488
963
      return sec->sectionIndex == UINT32_MAX;
1489
293
    return false;
1490
293
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::sortSections()::'lambda0'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1486
14
  auto nonScriptI = std::find_if(i, e, [](BaseCommand *base) {
1487
14
    if (auto *sec = dyn_cast<OutputSection>(base))
1488
9
      return sec->sectionIndex == UINT32_MAX;
1489
5
    return false;
1490
5
  });
1491
448
1492
448
  // Sort the orphan sections.
1493
448
  std::stable_sort(nonScriptI, e, compareSections);
1494
448
1495
448
  // As a horrible special case, skip the first . assignment if it is before any
1496
448
  // section. We do this because it is common to set a load address by starting
1497
448
  // the script with ". = 0xabcd" and the expectation is that every section is
1498
448
  // after that.
1499
448
  auto firstSectionOrDotAssignment =
1500
540
      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
1500
43
      std::find_if(i, e, [](BaseCommand *cmd) { return !shouldSkip(cmd); });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortSections()::'lambda1'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1500
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
1500
480
      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
1500
8
      std::find_if(i, e, [](BaseCommand *cmd) { return !shouldSkip(cmd); });
1501
448
  if (firstSectionOrDotAssignment != e &&
1502
448
      isa<SymbolAssignment>(**firstSectionOrDotAssignment))
1503
110
    ++firstSectionOrDotAssignment;
1504
448
  i = firstSectionOrDotAssignment;
1505
448
1506
1.53k
  while (nonScriptI != e) {
1507
1.09k
    auto pos = findOrphanPos(i, nonScriptI);
1508
1.09k
    OutputSection *orphan = cast<OutputSection>(*nonScriptI);
1509
1.09k
1510
1.09k
    // As an optimization, find all sections with the same sort rank
1511
1.09k
    // and insert them with one rotate.
1512
1.09k
    unsigned rank = orphan->sortRank;
1513
133k
    auto end = std::find_if(nonScriptI + 1, e, [=](BaseCommand *cmd) {
1514
133k
      return cast<OutputSection>(cmd)->sortRank != rank;
1515
133k
    });
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
1513
281
    auto end = std::find_if(nonScriptI + 1, e, [=](BaseCommand *cmd) {
1514
281
      return cast<OutputSection>(cmd)->sortRank != rank;
1515
281
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortSections()::'lambda2'(lld::elf::BaseCommand*)::operator()(lld::elf::BaseCommand*) const
Line
Count
Source
1513
91
    auto end = std::find_if(nonScriptI + 1, e, [=](BaseCommand *cmd) {
1514
91
      return cast<OutputSection>(cmd)->sortRank != rank;
1515
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
1513
132k
    auto end = std::find_if(nonScriptI + 1, e, [=](BaseCommand *cmd) {
1514
132k
      return cast<OutputSection>(cmd)->sortRank != rank;
1515
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
1513
40
    auto end = std::find_if(nonScriptI + 1, e, [=](BaseCommand *cmd) {
1514
40
      return cast<OutputSection>(cmd)->sortRank != rank;
1515
40
    });
1516
1.09k
    std::rotate(pos, nonScriptI, end);
1517
1.09k
    nonScriptI = end;
1518
1.09k
  }
1519
448
1520
448
  script->adjustSectionsAfterSorting();
1521
448
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::sortSections()
Line
Count
Source
1411
367
template <class ELFT> void Writer<ELFT>::sortSections() {
1412
367
  script->adjustSectionsBeforeSorting();
1413
367
1414
367
  // Don't sort if using -r. It is not necessary and we want to preserve the
1415
367
  // relative order for SHF_LINK_ORDER sections.
1416
367
  if (config->relocatable)
1417
11
    return;
1418
356
1419
356
  sortInputSections();
1420
356
1421
3.40k
  for (BaseCommand *base : script->sectionCommands) {
1422
3.40k
    auto *os = dyn_cast<OutputSection>(base);
1423
3.40k
    if (!os)
1424
65
      continue;
1425
3.34k
    os->sortRank = getSectionRank(os);
1426
3.34k
1427
3.34k
    // We want to assign rude approximation values to outSecOff fields
1428
3.34k
    // to know the relative order of the input sections. We use it for
1429
3.34k
    // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
1430
3.34k
    uint64_t i = 0;
1431
3.34k
    for (InputSection *sec : getInputSections(os))
1432
3.81k
      sec->outSecOff = i++;
1433
3.34k
  }
1434
356
1435
356
  if (!script->hasSectionsCommand) {
1436
313
    // We know that all the OutputSections are contiguous in this case.
1437
313
    auto isSection = [](BaseCommand *base) { return isa<OutputSection>(base); };
1438
313
    std::stable_sort(
1439
313
        llvm::find_if(script->sectionCommands, isSection),
1440
313
        llvm::find_if(llvm::reverse(script->sectionCommands), isSection).base(),
1441
313
        compareSections);
1442
313
    return;
1443
313
  }
1444
43
1445
43
  // Orphan sections are sections present in the input files which are
1446
43
  // not explicitly placed into the output file by the linker script.
1447
43
  //
1448
43
  // The sections in the linker script are already in the correct
1449
43
  // order. We have to figuere out where to insert the orphan
1450
43
  // sections.
1451
43
  //
1452
43
  // The order of the sections in the script is arbitrary and may not agree with
1453
43
  // compareSections. This means that we cannot easily define a strict weak
1454
43
  // ordering. To see why, consider a comparison of a section in the script and
1455
43
  // one not in the script. We have a two simple options:
1456
43
  // * Make them equivalent (a is not less than b, and b is not less than a).
1457
43
  //   The problem is then that equivalence has to be transitive and we can
1458
43
  //   have sections a, b and c with only b in a script and a less than c
1459
43
  //   which breaks this property.
1460
43
  // * Use compareSectionsNonScript. Given that the script order doesn't have
1461
43
  //   to match, we can end up with sections a, b, c, d where b and c are in the
1462
43
  //   script and c is compareSectionsNonScript less than b. In which case d
1463
43
  //   can be equivalent to c, a to b and d < a. As a concrete example:
1464
43
  //   .a (rx) # not in script
1465
43
  //   .b (rx) # in script
1466
43
  //   .c (ro) # in script
1467
43
  //   .d (ro) # not in script
1468
43
  //
1469
43
  // The way we define an order then is:
1470
43
  // *  Sort only the orphan sections. They are in the end right now.
1471
43
  // *  Move each orphan section to its preferred position. We try
1472
43
  //    to put each section in the last position where it can share
1473
43
  //    a PT_LOAD.
1474
43
  //
1475
43
  // There is some ambiguity as to where exactly a new entry should be
1476
43
  // inserted, because Commands contains not only output section
1477
43
  // commands but also other types of commands such as symbol assignment
1478
43
  // expressions. There's no correct answer here due to the lack of the
1479
43
  // formal specification of the linker script. We use heuristics to
1480
43
  // determine whether a new output command should be added before or
1481
43
  // after another commands. For the details, look at shouldSkip
1482
43
  // function.
1483
43
1484
43
  auto i = script->sectionCommands.begin();
1485
43
  auto e = script->sectionCommands.end();
1486
43
  auto nonScriptI = std::find_if(i, e, [](BaseCommand *base) {
1487
43
    if (auto *sec = dyn_cast<OutputSection>(base))
1488
43
      return sec->sectionIndex == UINT32_MAX;
1489
43
    return false;
1490
43
  });
1491
43
1492
43
  // Sort the orphan sections.
1493
43
  std::stable_sort(nonScriptI, e, compareSections);
1494
43
1495
43
  // As a horrible special case, skip the first . assignment if it is before any
1496
43
  // section. We do this because it is common to set a load address by starting
1497
43
  // the script with ". = 0xabcd" and the expectation is that every section is
1498
43
  // after that.
1499
43
  auto firstSectionOrDotAssignment =
1500
43
      std::find_if(i, e, [](BaseCommand *cmd) { return !shouldSkip(cmd); });
1501
43
  if (firstSectionOrDotAssignment != e &&
1502
43
      isa<SymbolAssignment>(**firstSectionOrDotAssignment))
1503
18
    ++firstSectionOrDotAssignment;
1504
43
  i = firstSectionOrDotAssignment;
1505
43
1506
149
  while (nonScriptI != e) {
1507
106
    auto pos = findOrphanPos(i, nonScriptI);
1508
106
    OutputSection *orphan = cast<OutputSection>(*nonScriptI);
1509
106
1510
106
    // As an optimization, find all sections with the same sort rank
1511
106
    // and insert them with one rotate.
1512
106
    unsigned rank = orphan->sortRank;
1513
106
    auto end = std::find_if(nonScriptI + 1, e, [=](BaseCommand *cmd) {
1514
106
      return cast<OutputSection>(cmd)->sortRank != rank;
1515
106
    });
1516
106
    std::rotate(pos, nonScriptI, end);
1517
106
    nonScriptI = end;
1518
106
  }
1519
43
1520
43
  script->adjustSectionsAfterSorting();
1521
43
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::sortSections()
Line
Count
Source
1411
154
template <class ELFT> void Writer<ELFT>::sortSections() {
1412
154
  script->adjustSectionsBeforeSorting();
1413
154
1414
154
  // Don't sort if using -r. It is not necessary and we want to preserve the
1415
154
  // relative order for SHF_LINK_ORDER sections.
1416
154
  if (config->relocatable)
1417
11
    return;
1418
143
1419
143
  sortInputSections();
1420
143
1421
1.77k
  for (BaseCommand *base : script->sectionCommands) {
1422
1.77k
    auto *os = dyn_cast<OutputSection>(base);
1423
1.77k
    if (!os)
1424
5
      continue;
1425
1.77k
    os->sortRank = getSectionRank(os);
1426
1.77k
1427
1.77k
    // We want to assign rude approximation values to outSecOff fields
1428
1.77k
    // to know the relative order of the input sections. We use it for
1429
1.77k
    // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
1430
1.77k
    uint64_t i = 0;
1431
1.77k
    for (InputSection *sec : getInputSections(os))
1432
1.92k
      sec->outSecOff = i++;
1433
1.77k
  }
1434
143
1435
143
  if (!script->hasSectionsCommand) {
1436
134
    // We know that all the OutputSections are contiguous in this case.
1437
134
    auto isSection = [](BaseCommand *base) { return isa<OutputSection>(base); };
1438
134
    std::stable_sort(
1439
134
        llvm::find_if(script->sectionCommands, isSection),
1440
134
        llvm::find_if(llvm::reverse(script->sectionCommands), isSection).base(),
1441
134
        compareSections);
1442
134
    return;
1443
134
  }
1444
9
1445
9
  // Orphan sections are sections present in the input files which are
1446
9
  // not explicitly placed into the output file by the linker script.
1447
9
  //
1448
9
  // The sections in the linker script are already in the correct
1449
9
  // order. We have to figuere out where to insert the orphan
1450
9
  // sections.
1451
9
  //
1452
9
  // The order of the sections in the script is arbitrary and may not agree with
1453
9
  // compareSections. This means that we cannot easily define a strict weak
1454
9
  // ordering. To see why, consider a comparison of a section in the script and
1455
9
  // one not in the script. We have a two simple options:
1456
9
  // * Make them equivalent (a is not less than b, and b is not less than a).
1457
9
  //   The problem is then that equivalence has to be transitive and we can
1458
9
  //   have sections a, b and c with only b in a script and a less than c
1459
9
  //   which breaks this property.
1460
9
  // * Use compareSectionsNonScript. Given that the script order doesn't have
1461
9
  //   to match, we can end up with sections a, b, c, d where b and c are in the
1462
9
  //   script and c is compareSectionsNonScript less than b. In which case d
1463
9
  //   can be equivalent to c, a to b and d < a. As a concrete example:
1464
9
  //   .a (rx) # not in script
1465
9
  //   .b (rx) # in script
1466
9
  //   .c (ro) # in script
1467
9
  //   .d (ro) # not in script
1468
9
  //
1469
9
  // The way we define an order then is:
1470
9
  // *  Sort only the orphan sections. They are in the end right now.
1471
9
  // *  Move each orphan section to its preferred position. We try
1472
9
  //    to put each section in the last position where it can share
1473
9
  //    a PT_LOAD.
1474
9
  //
1475
9
  // There is some ambiguity as to where exactly a new entry should be
1476
9
  // inserted, because Commands contains not only output section
1477
9
  // commands but also other types of commands such as symbol assignment
1478
9
  // expressions. There's no correct answer here due to the lack of the
1479
9
  // formal specification of the linker script. We use heuristics to
1480
9
  // determine whether a new output command should be added before or
1481
9
  // after another commands. For the details, look at shouldSkip
1482
9
  // function.
1483
9
1484
9
  auto i = script->sectionCommands.begin();
1485
9
  auto e = script->sectionCommands.end();
1486
9
  auto nonScriptI = std::find_if(i, e, [](BaseCommand *base) {
1487
9
    if (auto *sec = dyn_cast<OutputSection>(base))
1488
9
      return sec->sectionIndex == UINT32_MAX;
1489
9
    return false;
1490
9
  });
1491
9
1492
9
  // Sort the orphan sections.
1493
9
  std::stable_sort(nonScriptI, e, compareSections);
1494
9
1495
9
  // As a horrible special case, skip the first . assignment if it is before any
1496
9
  // section. We do this because it is common to set a load address by starting
1497
9
  // the script with ". = 0xabcd" and the expectation is that every section is
1498
9
  // after that.
1499
9
  auto firstSectionOrDotAssignment =
1500
9
      std::find_if(i, e, [](BaseCommand *cmd) { return !shouldSkip(cmd); });
1501
9
  if (firstSectionOrDotAssignment != e &&
1502
9
      isa<SymbolAssignment>(**firstSectionOrDotAssignment))
1503
0
    ++firstSectionOrDotAssignment;
1504
9
  i = firstSectionOrDotAssignment;
1505
9
1506
52
  while (nonScriptI != e) {
1507
43
    auto pos = findOrphanPos(i, nonScriptI);
1508
43
    OutputSection *orphan = cast<OutputSection>(*nonScriptI);
1509
43
1510
43
    // As an optimization, find all sections with the same sort rank
1511
43
    // and insert them with one rotate.
1512
43
    unsigned rank = orphan->sortRank;
1513
43
    auto end = std::find_if(nonScriptI + 1, e, [=](BaseCommand *cmd) {
1514
43
      return cast<OutputSection>(cmd)->sortRank != rank;
1515
43
    });
1516
43
    std::rotate(pos, nonScriptI, end);
1517
43
    nonScriptI = end;
1518
43
  }
1519
9
1520
9
  script->adjustSectionsAfterSorting();
1521
9
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::sortSections()
Line
Count
Source
1411
1.98k
template <class ELFT> void Writer<ELFT>::sortSections() {
1412
1.98k
  script->adjustSectionsBeforeSorting();
1413
1.98k
1414
1.98k
  // Don't sort if using -r. It is not necessary and we want to preserve the
1415
1.98k
  // relative order for SHF_LINK_ORDER sections.
1416
1.98k
  if (config->relocatable)
1417
62
    return;
1418
1.92k
1419
1.92k
  sortInputSections();
1420
1.92k
1421
280k
  for (BaseCommand *base : script->sectionCommands) {
1422
280k
    auto *os = dyn_cast<OutputSection>(base);
1423
280k
    if (!os)
1424
400
      continue;
1425
280k
    os->sortRank = getSectionRank(os);
1426
280k
1427
280k
    // We want to assign rude approximation values to outSecOff fields
1428
280k
    // to know the relative order of the input sections. We use it for
1429
280k
    // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
1430
280k
    uint64_t i = 0;
1431
280k
    for (InputSection *sec : getInputSections(os))
1432
282k
      sec->outSecOff = i++;
1433
280k
  }
1434
1.92k
1435
1.92k
  if (!script->hasSectionsCommand) {
1436
1.53k
    // We know that all the OutputSections are contiguous in this case.
1437
1.53k
    auto isSection = [](BaseCommand *base) { return isa<OutputSection>(base); };
1438
1.53k
    std::stable_sort(
1439
1.53k
        llvm::find_if(script->sectionCommands, isSection),
1440
1.53k
        llvm::find_if(llvm::reverse(script->sectionCommands), isSection).base(),
1441
1.53k
        compareSections);
1442
1.53k
    return;
1443
1.53k
  }
1444
392
1445
392
  // Orphan sections are sections present in the input files which are
1446
392
  // not explicitly placed into the output file by the linker script.
1447
392
  //
1448
392
  // The sections in the linker script are already in the correct
1449
392
  // order. We have to figuere out where to insert the orphan
1450
392
  // sections.
1451
392
  //
1452
392
  // The order of the sections in the script is arbitrary and may not agree with
1453
392
  // compareSections. This means that we cannot easily define a strict weak
1454
392
  // ordering. To see why, consider a comparison of a section in the script and
1455
392
  // one not in the script. We have a two simple options:
1456
392
  // * Make them equivalent (a is not less than b, and b is not less than a).
1457
392
  //   The problem is then that equivalence has to be transitive and we can
1458
392
  //   have sections a, b and c with only b in a script and a less than c
1459
392
  //   which breaks this property.
1460
392
  // * Use compareSectionsNonScript. Given that the script order doesn't have
1461
392
  //   to match, we can end up with sections a, b, c, d where b and c are in the
1462
392
  //   script and c is compareSectionsNonScript less than b. In which case d
1463
392
  //   can be equivalent to c, a to b and d < a. As a concrete example:
1464
392
  //   .a (rx) # not in script
1465
392
  //   .b (rx) # in script
1466
392
  //   .c (ro) # in script
1467
392
  //   .d (ro) # not in script
1468
392
  //
1469
392
  // The way we define an order then is:
1470
392
  // *  Sort only the orphan sections. They are in the end right now.
1471
392
  // *  Move each orphan section to its preferred position. We try
1472
392
  //    to put each section in the last position where it can share
1473
392
  //    a PT_LOAD.
1474
392
  //
1475
392
  // There is some ambiguity as to where exactly a new entry should be
1476
392
  // inserted, because Commands contains not only output section
1477
392
  // commands but also other types of commands such as symbol assignment
1478
392
  // expressions. There's no correct answer here due to the lack of the
1479
392
  // formal specification of the linker script. We use heuristics to
1480
392
  // determine whether a new output command should be added before or
1481
392
  // after another commands. For the details, look at shouldSkip
1482
392
  // function.
1483
392
1484
392
  auto i = script->sectionCommands.begin();
1485
392
  auto e = script->sectionCommands.end();
1486
392
  auto nonScriptI = std::find_if(i, e, [](BaseCommand *base) {
1487
392
    if (auto *sec = dyn_cast<OutputSection>(base))
1488
392
      return sec->sectionIndex == UINT32_MAX;
1489
392
    return false;
1490
392
  });
1491
392
1492
392
  // Sort the orphan sections.
1493
392
  std::stable_sort(nonScriptI, e, compareSections);
1494
392
1495
392
  // As a horrible special case, skip the first . assignment if it is before any
1496
392
  // section. We do this because it is common to set a load address by starting
1497
392
  // the script with ". = 0xabcd" and the expectation is that every section is
1498
392
  // after that.
1499
392
  auto firstSectionOrDotAssignment =
1500
392
      std::find_if(i, e, [](BaseCommand *cmd) { return !shouldSkip(cmd); });
1501
392
  if (firstSectionOrDotAssignment != e &&
1502
392
      isa<SymbolAssignment>(**firstSectionOrDotAssignment))
1503
91
    ++firstSectionOrDotAssignment;
1504
392
  i = firstSectionOrDotAssignment;
1505
392
1506
1.31k
  while (nonScriptI != e) {
1507
922
    auto pos = findOrphanPos(i, nonScriptI);
1508
922
    OutputSection *orphan = cast<OutputSection>(*nonScriptI);
1509
922
1510
922
    // As an optimization, find all sections with the same sort rank
1511
922
    // and insert them with one rotate.
1512
922
    unsigned rank = orphan->sortRank;
1513
922
    auto end = std::find_if(nonScriptI + 1, e, [=](BaseCommand *cmd) {
1514
922
      return cast<OutputSection>(cmd)->sortRank != rank;
1515
922
    });
1516
922
    std::rotate(pos, nonScriptI, end);
1517
922
    nonScriptI = end;
1518
922
  }
1519
392
1520
392
  script->adjustSectionsAfterSorting();
1521
392
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::sortSections()
Line
Count
Source
1411
101
template <class ELFT> void Writer<ELFT>::sortSections() {
1412
101
  script->adjustSectionsBeforeSorting();
1413
101
1414
101
  // Don't sort if using -r. It is not necessary and we want to preserve the
1415
101
  // relative order for SHF_LINK_ORDER sections.
1416
101
  if (config->relocatable)
1417
3
    return;
1418
98
1419
98
  sortInputSections();
1420
98
1421
1.14k
  for (BaseCommand *base : script->sectionCommands) {
1422
1.14k
    auto *os = dyn_cast<OutputSection>(base);
1423
1.14k
    if (!os)
1424
23
      continue;
1425
1.12k
    os->sortRank = getSectionRank(os);
1426
1.12k
1427
1.12k
    // We want to assign rude approximation values to outSecOff fields
1428
1.12k
    // to know the relative order of the input sections. We use it for
1429
1.12k
    // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
1430
1.12k
    uint64_t i = 0;
1431
1.12k
    for (InputSection *sec : getInputSections(os))
1432
1.17k
      sec->outSecOff = i++;
1433
1.12k
  }
1434
98
1435
98
  if (!script->hasSectionsCommand) {
1436
94
    // We know that all the OutputSections are contiguous in this case.
1437
94
    auto isSection = [](BaseCommand *base) { return isa<OutputSection>(base); };
1438
94
    std::stable_sort(
1439
94
        llvm::find_if(script->sectionCommands, isSection),
1440
94
        llvm::find_if(llvm::reverse(script->sectionCommands), isSection).base(),
1441
94
        compareSections);
1442
94
    return;
1443
94
  }
1444
4
1445
4
  // Orphan sections are sections present in the input files which are
1446
4
  // not explicitly placed into the output file by the linker script.
1447
4
  //
1448
4
  // The sections in the linker script are already in the correct
1449
4
  // order. We have to figuere out where to insert the orphan
1450
4
  // sections.
1451
4
  //
1452
4
  // The order of the sections in the script is arbitrary and may not agree with
1453
4
  // compareSections. This means that we cannot easily define a strict weak
1454
4
  // ordering. To see why, consider a comparison of a section in the script and
1455
4
  // one not in the script. We have a two simple options:
1456
4
  // * Make them equivalent (a is not less than b, and b is not less than a).
1457
4
  //   The problem is then that equivalence has to be transitive and we can
1458
4
  //   have sections a, b and c with only b in a script and a less than c
1459
4
  //   which breaks this property.
1460
4
  // * Use compareSectionsNonScript. Given that the script order doesn't have
1461
4
  //   to match, we can end up with sections a, b, c, d where b and c are in the
1462
4
  //   script and c is compareSectionsNonScript less than b. In which case d
1463
4
  //   can be equivalent to c, a to b and d < a. As a concrete example:
1464
4
  //   .a (rx) # not in script
1465
4
  //   .b (rx) # in script
1466
4
  //   .c (ro) # in script
1467
4
  //   .d (ro) # not in script
1468
4
  //
1469
4
  // The way we define an order then is:
1470
4
  // *  Sort only the orphan sections. They are in the end right now.
1471
4
  // *  Move each orphan section to its preferred position. We try
1472
4
  //    to put each section in the last position where it can share
1473
4
  //    a PT_LOAD.
1474
4
  //
1475
4
  // There is some ambiguity as to where exactly a new entry should be
1476
4
  // inserted, because Commands contains not only output section
1477
4
  // commands but also other types of commands such as symbol assignment
1478
4
  // expressions. There's no correct answer here due to the lack of the
1479
4
  // formal specification of the linker script. We use heuristics to
1480
4
  // determine whether a new output command should be added before or
1481
4
  // after another commands. For the details, look at shouldSkip
1482
4
  // function.
1483
4
1484
4
  auto i = script->sectionCommands.begin();
1485
4
  auto e = script->sectionCommands.end();
1486
4
  auto nonScriptI = std::find_if(i, e, [](BaseCommand *base) {
1487
4
    if (auto *sec = dyn_cast<OutputSection>(base))
1488
4
      return sec->sectionIndex == UINT32_MAX;
1489
4
    return false;
1490
4
  });
1491
4
1492
4
  // Sort the orphan sections.
1493
4
  std::stable_sort(nonScriptI, e, compareSections);
1494
4
1495
4
  // As a horrible special case, skip the first . assignment if it is before any
1496
4
  // section. We do this because it is common to set a load address by starting
1497
4
  // the script with ". = 0xabcd" and the expectation is that every section is
1498
4
  // after that.
1499
4
  auto firstSectionOrDotAssignment =
1500
4
      std::find_if(i, e, [](BaseCommand *cmd) { return !shouldSkip(cmd); });
1501
4
  if (firstSectionOrDotAssignment != e &&
1502
4
      isa<SymbolAssignment>(**firstSectionOrDotAssignment))
1503
1
    ++firstSectionOrDotAssignment;
1504
4
  i = firstSectionOrDotAssignment;
1505
4
1506
24
  while (nonScriptI != e) {
1507
20
    auto pos = findOrphanPos(i, nonScriptI);
1508
20
    OutputSection *orphan = cast<OutputSection>(*nonScriptI);
1509
20
1510
20
    // As an optimization, find all sections with the same sort rank
1511
20
    // and insert them with one rotate.
1512
20
    unsigned rank = orphan->sortRank;
1513
20
    auto end = std::find_if(nonScriptI + 1, e, [=](BaseCommand *cmd) {
1514
20
      return cast<OutputSection>(cmd)->sortRank != rank;
1515
20
    });
1516
20
    std::rotate(pos, nonScriptI, end);
1517
20
    nonScriptI = end;
1518
20
  }
1519
4
1520
4
  script->adjustSectionsAfterSorting();
1521
4
}
1522
1523
6
static bool compareByFilePosition(InputSection *a, InputSection *b) {
1524
6
  InputSection *la = a->getLinkOrderDep();
1525
6
  InputSection *lb = b->getLinkOrderDep();
1526
6
  OutputSection *aOut = la->getParent();
1527
6
  OutputSection *bOut = lb->getParent();
1528
6
1529
6
  if (aOut != bOut)
1530
0
    return aOut->sectionIndex < bOut->sectionIndex;
1531
6
  return la->outSecOff < lb->outSecOff;
1532
6
}
1533
1534
2.61k
template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1535
352k
  for (OutputSection *sec : outputSections) {
1536
352k
    if (!(sec->flags & SHF_LINK_ORDER))
1537
352k
      continue;
1538
44
1539
44
    // Link order may be distributed across several InputSectionDescriptions
1540
44
    // but sort must consider them all at once.
1541
44
    std::vector<InputSection **> scriptSections;
1542
44
    std::vector<InputSection *> sections;
1543
45
    for (BaseCommand *base : sec->sectionCommands) {
1544
45
      if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
1545
50
        for (InputSection *&isec : isd->sections) {
1546
50
          scriptSections.push_back(&isec);
1547
50
          sections.push_back(isec);
1548
50
        }
1549
44
      }
1550
45
    }
1551
44
1552
44
    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1553
44
    // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1554
44
    if (!config->relocatable && 
config->emachine == EM_ARM36
&&
1555
44
        
sec->type == SHT_ARM_EXIDX26
)
1556
26
      continue;
1557
18
1558
18
    llvm::stable_sort(sections, compareByFilePosition);
1559
18
1560
42
    for (int i = 0, n = sections.size(); i < n; 
++i24
)
1561
24
      *scriptSections[i] = sections[i];
1562
18
  }
1563
2.61k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::resolveShfLinkOrder()
Line
Count
Source
1534
367
template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1535
3.43k
  for (OutputSection *sec : outputSections) {
1536
3.43k
    if (!(sec->flags & SHF_LINK_ORDER))
1537
3.40k
      continue;
1538
34
1539
34
    // Link order may be distributed across several InputSectionDescriptions
1540
34
    // but sort must consider them all at once.
1541
34
    std::vector<InputSection **> scriptSections;
1542
34
    std::vector<InputSection *> sections;
1543
35
    for (BaseCommand *base : sec->sectionCommands) {
1544
35
      if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
1545
35
        for (InputSection *&isec : isd->sections) {
1546
35
          scriptSections.push_back(&isec);
1547
35
          sections.push_back(isec);
1548
35
        }
1549
34
      }
1550
35
    }
1551
34
1552
34
    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1553
34
    // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1554
34
    if (!config->relocatable && 
config->emachine == EM_ARM26
&&
1555
34
        
sec->type == SHT_ARM_EXIDX26
)
1556
26
      continue;
1557
8
1558
8
    llvm::stable_sort(sections, compareByFilePosition);
1559
8
1560
17
    for (int i = 0, n = sections.size(); i < n; 
++i9
)
1561
9
      *scriptSections[i] = sections[i];
1562
8
  }
1563
367
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::resolveShfLinkOrder()
Line
Count
Source
1534
154
template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1535
1.88k
  for (OutputSection *sec : outputSections) {
1536
1.88k
    if (!(sec->flags & SHF_LINK_ORDER))
1537
1.88k
      continue;
1538
0
1539
0
    // Link order may be distributed across several InputSectionDescriptions
1540
0
    // but sort must consider them all at once.
1541
0
    std::vector<InputSection **> scriptSections;
1542
0
    std::vector<InputSection *> sections;
1543
0
    for (BaseCommand *base : sec->sectionCommands) {
1544
0
      if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
1545
0
        for (InputSection *&isec : isd->sections) {
1546
0
          scriptSections.push_back(&isec);
1547
0
          sections.push_back(isec);
1548
0
        }
1549
0
      }
1550
0
    }
1551
0
1552
0
    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1553
0
    // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1554
0
    if (!config->relocatable && config->emachine == EM_ARM &&
1555
0
        sec->type == SHT_ARM_EXIDX)
1556
0
      continue;
1557
0
1558
0
    llvm::stable_sort(sections, compareByFilePosition);
1559
0
1560
0
    for (int i = 0, n = sections.size(); i < n; ++i)
1561
0
      *scriptSections[i] = sections[i];
1562
0
  }
1563
154
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::resolveShfLinkOrder()
Line
Count
Source
1534
1.98k
template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1535
346k
  for (OutputSection *sec : outputSections) {
1536
346k
    if (!(sec->flags & SHF_LINK_ORDER))
1537
346k
      continue;
1538
10
1539
10
    // Link order may be distributed across several InputSectionDescriptions
1540
10
    // but sort must consider them all at once.
1541
10
    std::vector<InputSection **> scriptSections;
1542
10
    std::vector<InputSection *> sections;
1543
10
    for (BaseCommand *base : sec->sectionCommands) {
1544
10
      if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
1545
15
        for (InputSection *&isec : isd->sections) {
1546
15
          scriptSections.push_back(&isec);
1547
15
          sections.push_back(isec);
1548
15
        }
1549
10
      }
1550
10
    }
1551
10
1552
10
    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1553
10
    // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1554
10
    if (!config->relocatable && config->emachine == EM_ARM &&
1555
10
        
sec->type == SHT_ARM_EXIDX0
)
1556
0
      continue;
1557
10
1558
10
    llvm::stable_sort(sections, compareByFilePosition);
1559
10
1560
25
    for (int i = 0, n = sections.size(); i < n; 
++i15
)
1561
15
      *scriptSections[i] = sections[i];
1562
10
  }
1563
1.98k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::resolveShfLinkOrder()
Line
Count
Source
1534
101
template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1535
1.15k
  for (OutputSection *sec : outputSections) {
1536
1.15k
    if (!(sec->flags & SHF_LINK_ORDER))
1537
1.15k
      continue;
1538
0
1539
0
    // Link order may be distributed across several InputSectionDescriptions
1540
0
    // but sort must consider them all at once.
1541
0
    std::vector<InputSection **> scriptSections;
1542
0
    std::vector<InputSection *> sections;
1543
0
    for (BaseCommand *base : sec->sectionCommands) {
1544
0
      if (auto *isd = dyn_cast<InputSectionDescription>(base)) {
1545
0
        for (InputSection *&isec : isd->sections) {
1546
0
          scriptSections.push_back(&isec);
1547
0
          sections.push_back(isec);
1548
0
        }
1549
0
      }
1550
0
    }
1551
0
1552
0
    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
1553
0
    // this processing inside the ARMExidxsyntheticsection::finalizeContents().
1554
0
    if (!config->relocatable && config->emachine == EM_ARM &&
1555
0
        sec->type == SHT_ARM_EXIDX)
1556
0
      continue;
1557
0
1558
0
    llvm::stable_sort(sections, compareByFilePosition);
1559
0
1560
0
    for (int i = 0, n = sections.size(); i < n; ++i)
1561
0
      *scriptSections[i] = sections[i];
1562
0
  }
1563
101
}
1564
1565
// We need to generate and finalize the content that depends on the address of
1566
// InputSections. As the generation of the content may also alter InputSection
1567
// addresses we must converge to a fixed point. We do that here. See the comment
1568
// in Writer<ELFT>::finalizeSections().
1569
2.61k
template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1570
2.61k
  ThunkCreator tc;
1571
2.61k
  AArch64Err843419Patcher a64p;
1572
2.61k
1573
2.61k
  // For some targets, like x86, this loop iterates only once.
1574
2.72k
  for (;;) {
1575
2.72k
    bool changed = false;
1576
2.72k
1577
2.72k
    script->assignAddresses();
1578
2.72k
1579
2.72k
    if (target->needsThunks)
1580
752
      changed |= tc.createThunks(outputSections);
1581
2.72k
1582
2.72k
    if (config->fixCortexA53Errata843419) {
1583
16
      if (changed)
1584
2
        script->assignAddresses();
1585
16
      changed |= a64p.createFixes();
1586
16
    }
1587
2.72k
1588
2.72k
    if (in.mipsGot)
1589
202
      in.mipsGot->updateAllocSize();
1590
2.72k
1591
2.74k
    for (Partition &part : partitions) {
1592
2.74k
      changed |= part.relaDyn->updateAllocSize();
1593
2.74k
      if (part.relrDyn)
1594
11
        changed |= part.relrDyn->updateAllocSize();
1595
2.74k
    }
1596
2.72k
1597
2.72k
    if (!changed)
1598
2.61k
      return;
1599
2.72k
  }
1600
2.61k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeAddressDependentContent()
Line
Count
Source
1569
367
template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1570
367
  ThunkCreator tc;
1571
367
  AArch64Err843419Patcher a64p;
1572
367
1573
367
  // For some targets, like x86, this loop iterates only once.
1574
417
  for (;;) {
1575
417
    bool changed = false;
1576
417
1577
417
    script->assignAddresses();
1578
417
1579
417
    if (target->needsThunks)
1580
229
      changed |= tc.createThunks(outputSections);
1581
417
1582
417
    if (config->fixCortexA53Errata843419) {
1583
0
      if (changed)
1584
0
        script->assignAddresses();
1585
0
      changed |= a64p.createFixes();
1586
0
    }
1587
417
1588
417
    if (in.mipsGot)
1589
25
      in.mipsGot->updateAllocSize();
1590
417
1591
422
    for (Partition &part : partitions) {
1592
422
      changed |= part.relaDyn->updateAllocSize();
1593
422
      if (part.relrDyn)
1594
4
        changed |= part.relrDyn->updateAllocSize();
1595
422
    }
1596
417
1597
417
    if (!changed)
1598
367
      return;
1599
417
  }
1600
367
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeAddressDependentContent()
Line
Count
Source
1569
154
template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1570
154
  ThunkCreator tc;
1571
154
  AArch64Err843419Patcher a64p;
1572
154
1573
154
  // For some targets, like x86, this loop iterates only once.
1574
175
  for (;;) {
1575
175
    bool changed = false;
1576
175
1577
175
    script->assignAddresses();
1578
175
1579
175
    if (target->needsThunks)
1580
175
      changed |= tc.createThunks(outputSections);
1581
175
1582
175
    if (config->fixCortexA53Errata843419) {
1583
0
      if (changed)
1584
0
        script->assignAddresses();
1585
0
      changed |= a64p.createFixes();
1586
0
    }
1587
175
1588
175
    if (in.mipsGot)
1589
136
      in.mipsGot->updateAllocSize();
1590
175
1591
175
    for (Partition &part : partitions) {
1592
175
      changed |= part.relaDyn->updateAllocSize();
1593
175
      if (part.relrDyn)
1594
0
        changed |= part.relrDyn->updateAllocSize();
1595
175
    }
1596
175
1597
175
    if (!changed)
1598
154
      return;
1599
175
  }
1600
154
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeAddressDependentContent()
Line
Count
Source
1569
1.98k
template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1570
1.98k
  ThunkCreator tc;
1571
1.98k
  AArch64Err843419Patcher a64p;
1572
1.98k
1573
1.98k
  // For some targets, like x86, this loop iterates only once.
1574
2.02k
  for (;;) {
1575
2.02k
    bool changed = false;
1576
2.02k
1577
2.02k
    script->assignAddresses();
1578
2.02k
1579
2.02k
    if (target->needsThunks)
1580
235
      changed |= tc.createThunks(outputSections);
1581
2.02k
1582
2.02k
    if (config->fixCortexA53Errata843419) {
1583
16
      if (changed)
1584
2
        script->assignAddresses();
1585
16
      changed |= a64p.createFixes();
1586
16
    }
1587
2.02k
1588
2.02k
    if (in.mipsGot)
1589
3
      in.mipsGot->updateAllocSize();
1590
2.02k
1591
2.03k
    for (Partition &part : partitions) {
1592
2.03k
      changed |= part.relaDyn->updateAllocSize();
1593
2.03k
      if (part.relrDyn)
1594
7
        changed |= part.relrDyn->updateAllocSize();
1595
2.03k
    }
1596
2.02k
1597
2.02k
    if (!changed)
1598
1.98k
      return;
1599
2.02k
  }
1600
1.98k
}
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeAddressDependentContent()
Line
Count
Source
1569
101
template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1570
101
  ThunkCreator tc;
1571
101
  AArch64Err843419Patcher a64p;
1572
101
1573
101
  // For some targets, like x86, this loop iterates only once.
1574
114
  for (;;) {
1575
114
    bool changed = false;
1576
114
1577
114
    script->assignAddresses();
1578
114
1579
114
    if (target->needsThunks)
1580
113
      changed |= tc.createThunks(outputSections);
1581
114
1582
114
    if (config->fixCortexA53Errata843419) {
1583
0
      if (changed)
1584
0
        script->assignAddresses();
1585
0
      changed |= a64p.createFixes();
1586
0
    }
1587
114
1588
114
    if (in.mipsGot)
1589
38
      in.mipsGot->updateAllocSize();
1590
114
1591
114
    for (Partition &part : partitions) {
1592
114
      changed |= part.relaDyn->updateAllocSize();
1593
114
      if (part.relrDyn)
1594
0
        changed |= part.relrDyn->updateAllocSize();
1595
114
    }
1596
114
1597
114
    if (!changed)
1598
101
      return;
1599
114
  }
1600
101
}
1601
1602
78.5k
static void finalizeSynthetic(SyntheticSection *sec) {
1603
78.5k
  if (sec && 
sec->isNeeded()49.8k
&&
sec->getParent()17.4k
)
1604
14.7k
    sec->finalizeContents();
1605
78.5k
}
1606
1607
// In order to allow users to manipulate linker-synthesized sections,
1608
// we had to add synthetic sections to the input section list early,
1609
// even before we make decisions whether they are needed. This allows
1610
// users to write scripts like this: ".mygot : { .got }".
1611
//
1612
// Doing it has an unintended side effects. If it turns out that we
1613
// don't need a .got (for example) at all because there's no
1614
// relocation that needs a .got, we don't want to emit .got.
1615
//
1616
// To deal with the above problem, this function is called after
1617
// scanRelocations is called to remove synthetic sections that turn
1618
// out to be empty.
1619
2.61k
static void removeUnusedSyntheticSections() {
1620
2.61k
  // All input synthetic sections that can be empty are placed after
1621
2.61k
  // all regular ones. We iterate over them all and exit at first
1622
2.61k
  // non-synthetic.
1623
52.6k
  for (InputSectionBase *s : llvm::reverse(inputSections)) {
1624
52.6k
    SyntheticSection *ss = dyn_cast<SyntheticSection>(s);
1625
52.6k
    if (!ss)
1626
2.60k
      return;
1627
50.0k
    OutputSection *os = ss->getParent();
1628
50.0k
    if (!os || 
ss->isNeeded()50.0k
)
1629
19.2k
      continue;
1630
30.8k
1631
30.8k
    // If we reach here, then SS is an unused synthetic section and we want to
1632
30.8k
    // remove it from corresponding input section description of output section.
1633
30.8k
    for (BaseCommand *b : os->sectionCommands)
1634
30.8k
      if (auto *isd = dyn_cast<InputSectionDescription>(b))
1635
30.8k
        llvm::erase_if(isd->sections,
1636
38.9k
                       [=](InputSection *isec) { return isec == ss; });
1637
30.8k
  }
1638
2.61k
}
1639
1640
// Returns true if a symbol can be replaced at load-time by a symbol
1641
// with the same name defined in other ELF executable or DSO.
1642
18.3k
static bool computeIsPreemptible(const Symbol &b) {
1643
18.3k
  assert(!b.isLocal());
1644
18.3k
1645
18.3k
  // Only symbols that appear in dynsym can be preempted.
1646
18.3k
  if (!b.includeInDynsym())
1647
15.7k
    return false;
1648
2.63k
1649
2.63k
  // Only default visibility symbols can be preempted.
1650
2.63k
  if (b.visibility != STV_DEFAULT)
1651
34
    return false;
1652
2.59k
1653
2.59k
  // At this point copy relocations have not been created yet, so any
1654
2.59k
  // symbol that is not defined locally is preemptible.
1655
2.59k
  if (!b.isDefined())
1656
1.09k
    return true;
1657
1.50k
1658
1.50k
  // If we have a dynamic list it specifies which local symbols are preemptible.
1659
1.50k
  if (config->hasDynamicList)
1660
18
    return false;
1661
1.48k
1662
1.48k
  if (!config->shared)
1663
132
    return false;
1664
1.35k
1665
1.35k
  // -Bsymbolic means that definitions are not preempted.
1666
1.35k
  if (config->bsymbolic || 
(1.34k
config->bsymbolicFunctions1.34k
&&
b.isFunc()3
))
1667
16
    return false;
1668
1.33k
  return true;
1669
1.33k
}
1670
1671
// Create output section objects and add them to OutputSections.
1672
2.75k
template <class ELFT> void Writer<ELFT>::finalizeSections() {
1673
2.75k
  Out::preinitArray = findSection(".preinit_array");
1674
2.75k
  Out::initArray = findSection(".init_array");
1675
2.75k
  Out::finiArray = findSection(".fini_array");
1676
2.75k
1677
2.75k
  // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1678
2.75k
  // symbols for sections, so that the runtime can get the start and end
1679
2.75k
  // addresses of each section by section name. Add such symbols.
1680
2.75k
  if (!config->relocatable) {
1681
2.66k
    addStartEndSymbols();
1682
2.66k
    for (BaseCommand *base : script->sectionCommands)
1683
312k
      if (auto *sec = dyn_cast<OutputSection>(base))
1684
311k
        addStartStopSymbols(sec);
1685
2.66k
  }
1686
2.75k
1687
2.75k
  // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1688
2.75k
  // It should be okay as no one seems to care about the type.
1689
2.75k
  // Even the author of gold doesn't remember why gold behaves that way.
1690
2.75k
  // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1691
2.75k
  if (mainPart->dynamic->parent)
1692
1.33k
    symtab->addSymbol(Defined{/*file=*/nullptr, "_DYNAMIC", STB_WEAK,
1693
1.33k
                              STV_HIDDEN, STT_NOTYPE,
1694
1.33k
                              /*value=*/0, /*size=*/0, mainPart->dynamic});
1695
2.75k
1696
2.75k
  // Define __rel[a]_iplt_{start,end} symbols if needed.
1697
2.75k
  addRelIpltSymbols();
1698
2.75k
1699
2.75k
  // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800 if not defined.
1700
2.75k
  // This symbol should only be defined in an executable.
1701
2.75k
  if (config->emachine == EM_RISCV && 
!config->shared90
)
1702
70
    ElfSym::riscvGlobalPointer =
1703
70
        addOptionalRegular("__global_pointer$", findSection(".sdata"), 0x800,
1704
70
                           STV_DEFAULT, STB_GLOBAL);
1705
2.75k
1706
2.75k
  if (config->emachine == EM_X86_64) {
1707
1.84k
    // On targets that support TLSDESC, _TLS_MODULE_BASE_ is defined in such a
1708
1.84k
    // way that:
1709
1.84k
    //
1710
1.84k
    // 1) Without relaxation: it produces a dynamic TLSDESC relocation that
1711
1.84k
    // computes 0.
1712
1.84k
    // 2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address in
1713
1.84k
    // the TLS block).
1714
1.84k
    //
1715
1.84k
    // 2) is special cased in @tpoff computation. To satisfy 1), we define it as
1716
1.84k
    // an absolute symbol of zero. This is different from GNU linkers which
1717
1.84k
    // define _TLS_MODULE_BASE_ relative to the first TLS section.
1718
1.84k
    Symbol *s = symtab->find("_TLS_MODULE_BASE_");
1719
1.84k
    if (s && 
s->isUndefined()2
) {
1720
2
      s->resolve(Defined{/*file=*/nullptr, s->getName(), STB_GLOBAL, STV_HIDDEN,
1721
2
                         STT_TLS, /*value=*/0, 0,
1722
2
                         /*section=*/nullptr});
1723
2
      ElfSym::tlsModuleBase = cast<Defined>(s);
1724
2
    }
1725
1.84k
  }
1726
2.75k
1727
2.75k
  // This responsible for splitting up .eh_frame section into
1728
2.75k
  // pieces. The relocation scan uses those pieces, so this has to be
1729
2.75k
  // earlier.
1730
2.75k
  for (Partition &part : partitions)
1731
2.77k
    finalizeSynthetic(part.ehFrame);
1732
2.75k
1733
18.3k
  symtab->forEachSymbol([](Symbol *s) {
1734
18.3k
    if (!s->isPreemptible)
1735
18.3k
      s->isPreemptible = computeIsPreemptible(*s);
1736
18.3k
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeSections()::'lambda'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1733
1.41k
  symtab->forEachSymbol([](Symbol *s) {
1734
1.41k
    if (!s->isPreemptible)
1735
1.41k
      s->isPreemptible = computeIsPreemptible(*s);
1736
1.41k
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeSections()::'lambda'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1733
689
  symtab->forEachSymbol([](Symbol *s) {
1734
689
    if (!s->isPreemptible)
1735
689
      s->isPreemptible = computeIsPreemptible(*s);
1736
689
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeSections()::'lambda'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1733
5.85k
  symtab->forEachSymbol([](Symbol *s) {
1734
5.85k
    if (!s->isPreemptible)
1735
5.84k
      s->isPreemptible = computeIsPreemptible(*s);
1736
5.85k
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeSections()::'lambda'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1733
10.3k
  symtab->forEachSymbol([](Symbol *s) {
1734
10.3k
    if (!s->isPreemptible)
1735
10.3k
      s->isPreemptible = computeIsPreemptible(*s);
1736
10.3k
  });
1737
2.75k
1738
2.75k
  // Scan relocations. This must be done after every symbol is declared so that
1739
2.75k
  // we can correctly decide if a dynamic relocation is needed.
1740
2.75k
  if (!config->relocatable) {
1741
2.66k
    forEachRelSec(scanRelocations<ELFT>);
1742
2.66k
    reportUndefinedSymbols<ELFT>();
1743
2.66k
  }
1744
2.75k
1745
2.75k
  addIRelativeRelocs();
1746
2.75k
1747
2.75k
  if (in.plt && in.plt->isNeeded())
1748
263
    in.plt->addSymbols();
1749
2.75k
  if (in.iplt && in.iplt->isNeeded())
1750
46
    in.iplt->addSymbols();
1751
2.75k
1752
2.75k
  if (!config->allowShlibUndefined) {
1753
1.82k
    // Error on undefined symbols in a shared object, if all of its DT_NEEDED
1754
1.82k
    // entires are seen. These cases would otherwise lead to runtime errors
1755
1.82k
    // reported by the dynamic linker.
1756
1.82k
    //
1757
1.82k
    // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker to
1758
1.82k
    // catch more cases. That is too much for us. Our approach resembles the one
1759
1.82k
    // used in ld.gold, achieves a good balance to be useful but not too smart.
1760
1.82k
    for (SharedFile *file : sharedFiles)
1761
339
      file->allNeededIsKnown =
1762
339
          llvm::all_of(file->dtNeeded, [&](StringRef needed) {
1763
4
            return symtab->soNames.count(needed);
1764
4
          });
Unexecuted instantiation: Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeSections()::'lambda'(llvm::StringRef)::operator()(llvm::StringRef) const
Unexecuted instantiation: Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeSections()::'lambda'(llvm::StringRef)::operator()(llvm::StringRef) const
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeSections()::'lambda'(llvm::StringRef)::operator()(llvm::StringRef) const
Line
Count
Source
1762
4
          llvm::all_of(file->dtNeeded, [&](StringRef needed) {
1763
4
            return symtab->soNames.count(needed);
1764
4
          });
Unexecuted instantiation: Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeSections()::'lambda'(llvm::StringRef)::operator()(llvm::StringRef) const
1765
1.82k
1766
15.3k
    symtab->forEachSymbol([](Symbol *sym) {
1767
15.3k
      if (sym->isUndefined() && 
!sym->isWeak()304
)
1768
235
        if (auto *f = dyn_cast_or_null<SharedFile>(sym->file))
1769
3
          if (f->allNeededIsKnown)
1770
2
            error(toString(f) + ": undefined reference to " + toString(*sym));
1771
15.3k
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeSections()::'lambda0'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1766
1.00k
    symtab->forEachSymbol([](Symbol *sym) {
1767
1.00k
      if (sym->isUndefined() && 
!sym->isWeak()21
)
1768
7
        if (auto *f = dyn_cast_or_null<SharedFile>(sym->file))
1769
0
          if (f->allNeededIsKnown)
1770
0
            error(toString(f) + ": undefined reference to " + toString(*sym));
1771
1.00k
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeSections()::'lambda0'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1766
408
    symtab->forEachSymbol([](Symbol *sym) {
1767
408
      if (sym->isUndefined() && 
!sym->isWeak()3
)
1768
1
        if (auto *f = dyn_cast_or_null<SharedFile>(sym->file))
1769
0
          if (f->allNeededIsKnown)
1770
0
            error(toString(f) + ": undefined reference to " + toString(*sym));
1771
408
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeSections()::'lambda0'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1766
3.71k
    symtab->forEachSymbol([](Symbol *sym) {
1767
3.71k
      if (sym->isUndefined() && 
!sym->isWeak()276
)
1768
224
        if (auto *f = dyn_cast_or_null<SharedFile>(sym->file))
1769
3
          if (f->allNeededIsKnown)
1770
2
            error(toString(f) + ": undefined reference to " + toString(*sym));
1771
3.71k
    });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeSections()::'lambda0'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1766
10.2k
    symtab->forEachSymbol([](Symbol *sym) {
1767
10.2k
      if (sym->isUndefined() && 
!sym->isWeak()4
)
1768
3
        if (auto *f = dyn_cast_or_null<SharedFile>(sym->file))
1769
0
          if (f->allNeededIsKnown)
1770
0
            error(toString(f) + ": undefined reference to " + toString(*sym));
1771
10.2k
    });
1772
1.82k
  }
1773
2.75k
1774
2.75k
  // Now that we have defined all possible global symbols including linker-
1775
2.75k
  // synthesized ones. Visit all symbols to give the finishing touches.
1776
18.3k
  symtab->forEachSymbol([](Symbol *sym) {
1777
18.3k
    if (!includeInSymtab(*sym))
1778
518
      return;
1779
17.8k
    if (in.symTab)
1780
17.8k
      in.symTab->addSymbol(sym);
1781
17.8k
1782
17.8k
    if (sym->includeInDynsym()) {
1783
2.24k
      partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
1784
2.24k
      if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
1785
428
        if (file->isNeeded && !sym->isUndefined())
1786
427
          addVerneed(sym);
1787
2.24k
    }
1788
17.8k
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, false> >::finalizeSections()::'lambda1'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1776
1.41k
  symtab->forEachSymbol([](Symbol *sym) {
1777
1.41k
    if (!includeInSymtab(*sym))
1778
45
      return;
1779
1.37k
    if (in.symTab)
1780
1.37k
      in.symTab->addSymbol(sym);
1781
1.37k
1782
1.37k
    if (sym->includeInDynsym()) {
1783
354
      partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
1784
354
      if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
1785
79
        if (file->isNeeded && !sym->isUndefined())
1786
79
          addVerneed(sym);
1787
354
    }
1788
1.37k
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, false> >::finalizeSections()::'lambda1'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1776
689
  symtab->forEachSymbol([](Symbol *sym) {
1777
689
    if (!includeInSymtab(*sym))
1778
69
      return;
1779
620
    if (in.symTab)
1780
620
      in.symTab->addSymbol(sym);
1781
620
1782
620
    if (sym->includeInDynsym()) {
1783
194
      partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
1784
194
      if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
1785
38
        if (file->isNeeded && !sym->isUndefined())
1786
38
          addVerneed(sym);
1787
194
    }
1788
620
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)1, true> >::finalizeSections()::'lambda1'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1776
5.85k
  symtab->forEachSymbol([](Symbol *sym) {
1777
5.85k
    if (!includeInSymtab(*sym))
1778
391
      return;
1779
5.46k
    if (in.symTab)
1780
5.45k
      in.symTab->addSymbol(sym);
1781
5.46k
1782
5.46k
    if (sym->includeInDynsym()) {
1783
1.59k
      partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
1784
1.59k
      if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
1785
296
        if (file->isNeeded && !sym->isUndefined())
1786
295
          addVerneed(sym);
1787
1.59k
    }
1788
5.46k
  });
Writer.cpp:(anonymous namespace)::Writer<llvm::object::ELFType<(llvm::support::endianness)0, true> >::finalizeSections()::'lambda1'(lld::elf::Symbol*)::operator()(lld::elf::Symbol*) const
Line
Count
Source
1776
10.3k
  symtab->forEachSymbol([](Symbol *sym) {
1777
10.3k
    if (!includeInSymtab(*sym))
1778
13
      return;
1779
10.3k
    if (in.symTab)
1780
10.3k
      in.symTab->addSymbol(sym);
1781
10.3k
1782
10.3k
    if (sym->includeInDynsym()) {
1783
98
      partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
1784
98
      if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
1785
15
        if (file->isNeeded && !sym->isUndefined())
1786
15
          addVerneed(sym);
1787
98
    }
1788
10.3k
  });
1789
2.75k
1790
2.75k
  // We also need to scan the dynamic relocation tables of the other partitions
1791
2.75k
  // and add any referenced symbols to the partition's dynsym.
1792
2.75k
  for (Partition &part : MutableArrayRef<Partition>(partitions).slice(1)) {
1793
18
    DenseSet<Symbol *> syms;
1794
18
    for (const SymbolTableEntry &e : part.dynSymTab->getSymbols())
1795
21
      syms.insert(e.sym);
1796
18
    for (DynamicReloc &reloc : part.relaDyn->relocs)
1797
5
      if (reloc.sym && !reloc.useSymVA && 
syms.insert(reloc.sym).second4
)
1798
2
        part.dynSymTab->addSymbol(reloc.sym);
1799
18
  }
1800
2.75k
1801
2.75k
  // Do not proceed if there was an undefined symbol.
1802
2.75k
  if (errorCount())
1803
143
    return;
1804
2.61k
1805
2.61k
  if (in.mipsGot)
1806
187
    in.mipsGot->build();
1807
2.61k
1808
2.61k
  removeUnusedSyntheticSections();
1809
2.61k
1810
2.61k
  sortSections();
1811
2.61k
1812
2.61k
  // Now that we have the final list, create a list of all the
1813
2.61k
  // OutputSections for convenience.
1814
2.61k
  for (BaseCommand *base : script->sectionCommands)
1815
353k
    if (auto *sec = dyn_cast<OutputSection>(base))
1816
352k
      outputSections.push_back(sec);
1817
2.61k
1818
2.61k
  // Prefer command line supplied address over other constraints.
1819
352k
  for (OutputSection *sec : outputSections) {
1820
3