blob: 4d72f5e1b0d1a8c8c280714fd937a7cea7790879 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.cpp -------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "OutputSections.h"
11#include "Config.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000012#include "SymbolTable.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000013#include "Target.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000014#include "llvm/Support/MathExtras.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000015
Rafael Espindola5805c4f2015-09-21 21:38:08 +000016using namespace llvm;
17using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000018using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000019using namespace llvm::ELF;
20
21using namespace lld;
22using namespace lld::elf2;
23
George Rimarbfb7bf72015-12-21 10:00:12 +000024bool lld::elf2::HasGotOffRel = false;
25
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000026template <class ELFT>
27OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t sh_type,
28 uintX_t sh_flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029 : Name(Name) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000030 memset(&Header, 0, sizeof(Elf_Shdr));
Rafael Espindola5805c4f2015-09-21 21:38:08 +000031 Header.sh_type = sh_type;
32 Header.sh_flags = sh_flags;
33}
34
Rafael Espindola35c6af32015-09-25 17:19:10 +000035template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000036GotPltSection<ELFT>::GotPltSection()
37 : OutputSectionBase<ELFT>(".got.plt", llvm::ELF::SHT_PROGBITS,
38 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
39 this->Header.sh_addralign = sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000040}
41
42template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000043 Sym->GotPltIndex = Target->getGotPltHeaderEntriesNum() + Entries.size();
George Rimar648a2c32015-10-20 08:54:27 +000044 Entries.push_back(Sym);
45}
46
47template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +000048 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +000049}
50
51template <class ELFT>
52typename GotPltSection<ELFT>::uintX_t
53GotPltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
54 return this->getVA() + B.GotPltIndex * sizeof(uintX_t);
55}
56
57template <class ELFT> void GotPltSection<ELFT>::finalize() {
Igor Kudrin351b41d2015-11-16 17:44:08 +000058 this->Header.sh_size =
59 (Target->getGotPltHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000060}
61
62template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000063 Target->writeGotPltHeaderEntries(Buf);
64 Buf += Target->getGotPltHeaderEntriesNum() * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000065 for (const SymbolBody *B : Entries) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000066 Target->writeGotPltEntry(Buf, Out<ELFT>::Plt->getEntryAddr(*B));
George Rimar648a2c32015-10-20 08:54:27 +000067 Buf += sizeof(uintX_t);
68 }
69}
70
71template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000072GotSection<ELFT>::GotSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000073 : OutputSectionBase<ELFT>(".got", llvm::ELF::SHT_PROGBITS,
74 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000075 if (Config->EMachine == EM_MIPS)
76 this->Header.sh_flags |= llvm::ELF::SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000077 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000078}
79
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000081 Sym->GotIndex = Target->getGotHeaderEntriesNum() + Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 Entries.push_back(Sym);
George Rimar687138c2015-11-13 16:28:53 +000083}
84
George Rimar90cd0a82015-12-01 19:20:26 +000085template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) {
86 if (Sym->hasGlobalDynIndex())
87 return false;
88 Sym->GlobalDynIndex = Target->getGotHeaderEntriesNum() + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +000089 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +000090 Entries.push_back(Sym);
91 Entries.push_back(nullptr);
George Rimar90cd0a82015-12-01 19:20:26 +000092 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000093}
94
George Rimar95c1a582015-12-07 08:02:20 +000095template <class ELFT> bool GotSection<ELFT>::addCurrentModuleTlsIndex() {
George Rimarb17f7392015-12-01 18:24:07 +000096 if (LocalTlsIndexOff != uint32_t(-1))
97 return false;
Michael J. Spencer1e225612015-11-11 01:00:24 +000098 Entries.push_back(nullptr);
99 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +0000100 LocalTlsIndexOff = (Entries.size() - 2) * sizeof(uintX_t);
101 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000102}
103
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104template <class ELFT>
105typename GotSection<ELFT>::uintX_t
106GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama5f551ae2015-10-14 14:02:06 +0000107 return this->getVA() + B.GotIndex * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000108}
109
Igor Kudrin304860a2015-11-12 04:39:49 +0000110template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000111typename GotSection<ELFT>::uintX_t
112GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
113 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
114}
115
116template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000117const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
118 return Entries.empty() ? nullptr : Entries.front();
119}
120
121template <class ELFT>
122unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
123 // TODO: Update when the suppoort of GOT entries for local symbols is added.
124 return Target->getGotHeaderEntriesNum();
125}
126
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000127template <class ELFT> void GotSection<ELFT>::finalize() {
128 this->Header.sh_size =
129 (Target->getGotHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
130}
131
Rafael Espindolaa6627382015-10-06 23:56:53 +0000132template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000133 Target->writeGotHeaderEntries(Buf);
134 Buf += Target->getGotHeaderEntriesNum() * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000135 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000136 uint8_t *Entry = Buf;
137 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000138 if (!B)
139 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000140 // MIPS has special rules to fill up GOT entries.
141 // See "Global Offset Table" in Chapter 5 in the following document
142 // for detailed description:
143 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
144 // As the first approach, we can just store addresses for all symbols.
145 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000146 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000147 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +0000148 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000149 }
150}
151
Rafael Espindola35c6af32015-09-25 17:19:10 +0000152template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000153PltSection<ELFT>::PltSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000154 : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS,
155 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000156 this->Header.sh_addralign = 16;
157}
158
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000160 size_t Off = 0;
George Rimar648a2c32015-10-20 08:54:27 +0000161 bool LazyReloc = Target->supportsLazyRelocations();
162 if (LazyReloc) {
163 // First write PLT[0] entry which is special.
164 Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA());
165 Off += Target->getPltZeroEntrySize();
166 }
George Rimarfb5d7f22015-11-26 19:58:51 +0000167 for (auto &I : Entries) {
George Rimar77b77792015-11-25 22:15:01 +0000168 const SymbolBody *E = I.first;
169 unsigned RelOff = I.second;
170 uint64_t GotVA =
171 LazyReloc ? Out<ELFT>::GotPlt->getVA() : Out<ELFT>::Got->getVA();
172 uint64_t GotE = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
173 : Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000174 uint64_t Plt = this->getVA() + Off;
George Rimar77b77792015-11-25 22:15:01 +0000175 Target->writePltEntry(Buf + Off, GotVA, GotE, Plt, E->PltIndex, RelOff);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000176 Off += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000177 }
178}
179
180template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000181 Sym->PltIndex = Entries.size();
George Rimar77b77792015-11-25 22:15:01 +0000182 unsigned RelOff = Target->supportsLazyRelocations()
183 ? Out<ELFT>::RelaPlt->getRelocOffset()
184 : Out<ELFT>::RelaDyn->getRelocOffset();
185 Entries.push_back(std::make_pair(Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000186}
187
188template <class ELFT>
189typename PltSection<ELFT>::uintX_t
190PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
George Rimar648a2c32015-10-20 08:54:27 +0000191 return this->getVA() + Target->getPltZeroEntrySize() +
192 B.PltIndex * Target->getPltEntrySize();
193}
194
195template <class ELFT> void PltSection<ELFT>::finalize() {
196 this->Header.sh_size = Target->getPltZeroEntrySize() +
197 Entries.size() * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000198}
199
200template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000201RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
202 : OutputSectionBase<ELFT>(Name,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000203 IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
204 llvm::ELF::SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000205 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000206 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
207 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
208}
209
George Rimar5828c232015-11-30 17:49:19 +0000210// Applies corresponding symbol and type for dynamic tls relocation.
211// Returns true if relocation was handled.
212template <class ELFT>
213bool RelocationSection<ELFT>::applyTlsDynamicReloc(SymbolBody *Body,
214 uint32_t Type, Elf_Rel *P,
215 Elf_Rel *N) {
216 if (Target->isTlsLocalDynamicReloc(Type)) {
217 P->setSymbolAndType(0, Target->getTlsModuleIndexReloc(), Config->Mips64EL);
George Rimarb17f7392015-12-01 18:24:07 +0000218 P->r_offset = Out<ELFT>::Got->getLocalTlsIndexVA();
George Rimar5828c232015-11-30 17:49:19 +0000219 return true;
220 }
221
George Rimar25411f252015-12-04 11:20:13 +0000222 if (!Body || !Target->isTlsGlobalDynamicReloc(Type))
223 return false;
224
225 if (Target->isTlsOptimized(Type, Body)) {
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000226 P->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000227 Target->getTlsGotReloc(), Config->Mips64EL);
228 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimar5828c232015-11-30 17:49:19 +0000229 return true;
230 }
George Rimar25411f252015-12-04 11:20:13 +0000231
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000232 P->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000233 Target->getTlsModuleIndexReloc(), Config->Mips64EL);
234 P->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body);
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000235 N->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000236 Target->getTlsOffsetReloc(), Config->Mips64EL);
237 N->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body) + sizeof(uintX_t);
238 return true;
George Rimar5828c232015-11-30 17:49:19 +0000239}
240
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000242 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000244 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
245 Buf += EntrySize;
246
Michael J. Spencer627ae702015-11-13 00:28:34 +0000247 // Skip placeholder for global dynamic TLS relocation pair. It was already
248 // handled by the previous relocation.
249 if (!Rel.C || !Rel.RI)
250 continue;
251
252 InputSectionBase<ELFT> &C = *Rel.C;
253 const Elf_Rel &RI = *Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000254 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000255 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000256 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000257 if (Body)
258 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000259
Rui Ueyama64558522015-10-16 22:51:43 +0000260 uint32_t Type = RI.getType(Config->Mips64EL);
George Rimar5828c232015-11-30 17:49:19 +0000261 if (applyTlsDynamicReloc(Body, Type, P, reinterpret_cast<Elf_Rel *>(Buf)))
Michael J. Spencer1e225612015-11-11 01:00:24 +0000262 continue;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000263 bool NeedsCopy = Body && Target->needsCopyRel(Type, *Body);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000264 bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
265 bool CanBePreempted = canBePreempted(Body, NeedsGot);
George Rimar648a2c32015-10-20 08:54:27 +0000266 bool LazyReloc = Body && Target->supportsLazyRelocations() &&
267 Target->relocNeedsPlt(Type, *Body);
George Rimar6f17e092015-12-17 09:32:21 +0000268 bool IsDynRelative = Type == Target->getRelativeReloc();
Rafael Espindola49757522015-10-19 19:58:18 +0000269
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000270 unsigned Sym = CanBePreempted ? Body->DynamicSymbolTableIndex : 0;
George Rimarc7dc0be2015-12-15 08:48:39 +0000271 unsigned Reloc;
George Rimar6f17e092015-12-17 09:32:21 +0000272 if (!CanBePreempted || IsDynRelative)
George Rimarc7dc0be2015-12-15 08:48:39 +0000273 Reloc = Target->getRelativeReloc();
274 else if (LazyReloc)
275 Reloc = Target->getPltReloc();
276 else if (NeedsGot)
Rui Ueyama62d0e322015-12-17 00:04:18 +0000277 Reloc = Body->isTls() ? Target->getTlsGotReloc() : Target->getGotReloc();
George Rimarc7dc0be2015-12-15 08:48:39 +0000278 else if (NeedsCopy)
279 Reloc = Target->getCopyReloc();
280 else
281 Reloc = Target->getDynReloc(Type);
282 P->setSymbolAndType(Sym, Reloc, Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000283
George Rimar648a2c32015-10-20 08:54:27 +0000284 if (NeedsGot) {
285 if (LazyReloc)
286 P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
287 else
288 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimarbc590fe2015-10-28 16:48:58 +0000289 } else if (NeedsCopy) {
290 P->r_offset = Out<ELFT>::Bss->getVA() +
291 dyn_cast<SharedSymbol<ELFT>>(Body)->OffsetInBSS;
George Rimar648a2c32015-10-20 08:54:27 +0000292 } else {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000293 P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA();
George Rimar648a2c32015-10-20 08:54:27 +0000294 }
Rafael Espindola49757522015-10-19 19:58:18 +0000295
Rafael Espindola932efcf2015-10-19 20:24:44 +0000296 uintX_t OrigAddend = 0;
Rafael Espindola49757522015-10-19 19:58:18 +0000297 if (IsRela && !NeedsGot)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000298 OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola49757522015-10-19 19:58:18 +0000299
Rafael Espindola932efcf2015-10-19 20:24:44 +0000300 uintX_t Addend;
George Rimarbc590fe2015-10-28 16:48:58 +0000301 if (NeedsCopy)
302 Addend = 0;
George Rimar6f17e092015-12-17 09:32:21 +0000303 else if (CanBePreempted || IsDynRelative)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000304 Addend = OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000305 else if (Body)
306 Addend = getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body)) + OrigAddend;
307 else if (IsRela)
308 Addend = getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI));
309 else
310 Addend = getLocalRelTarget(File, RI);
Rafael Espindola52dca342015-10-07 00:58:20 +0000311
312 if (IsRela)
313 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000314 }
315}
316
George Rimar77b77792015-11-25 22:15:01 +0000317template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
318 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
319 return EntrySize * Relocs.size();
320}
321
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000322template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000323 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
325}
326
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000327template <class ELFT>
328InterpSection<ELFT>::InterpSection()
329 : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
330 llvm::ELF::SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000331 this->Header.sh_size = Config->DynamicLinker.size() + 1;
332 this->Header.sh_addralign = 1;
333}
334
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000335template <class ELFT>
336void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
Rui Ueyama157c4332015-10-24 17:57:39 +0000337 Header.sh_name = Out<ELFT>::ShStrTab->getOffset(Name);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000338 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000339}
340
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000341template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000342 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
343}
344
Rafael Espindola35c6af32015-09-25 17:19:10 +0000345template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000346HashTableSection<ELFT>::HashTableSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000347 : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
348 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000349 this->Header.sh_entsize = sizeof(Elf_Word);
350 this->Header.sh_addralign = sizeof(Elf_Word);
351}
352
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000353static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000354 uint32_t H = 0;
355 for (char C : Name) {
356 H = (H << 4) + C;
357 uint32_t G = H & 0xf0000000;
358 if (G)
359 H ^= G >> 24;
360 H &= ~G;
361 }
362 return H;
363}
364
Rui Ueyama0db335f2015-10-07 16:58:54 +0000365template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000366 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000367
Rui Ueyama0db335f2015-10-07 16:58:54 +0000368 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000369 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000370
371 // Create as many buckets as there are symbols.
372 // FIXME: This is simplistic. We can try to optimize it, but implementing
373 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000374 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000375 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
376}
377
378template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000379 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000380 auto *P = reinterpret_cast<Elf_Word *>(Buf);
381 *P++ = NumSymbols; // nbucket
382 *P++ = NumSymbols; // nchain
383
384 Elf_Word *Buckets = P;
385 Elf_Word *Chains = P + NumSymbols;
386
Igor Kudrinf1d60292015-10-28 07:05:56 +0000387 for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) {
Igor Kudrinab665fc2015-10-20 21:47:58 +0000388 StringRef Name = Body->getName();
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000389 unsigned I = Body->DynamicSymbolTableIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000390 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000391 Chains[I] = Buckets[Hash];
392 Buckets[Hash] = I;
393 }
394}
395
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000396static uint32_t hashGnu(StringRef Name) {
397 uint32_t H = 5381;
398 for (uint8_t C : Name)
399 H = (H << 5) + H + C;
400 return H;
401}
402
403template <class ELFT>
404GnuHashTableSection<ELFT>::GnuHashTableSection()
405 : OutputSectionBase<ELFT>(".gnu.hash", llvm::ELF::SHT_GNU_HASH,
406 llvm::ELF::SHF_ALLOC) {
407 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
408 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
409}
410
411template <class ELFT>
412unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
413 if (!NumHashed)
414 return 0;
415
416 // These values are prime numbers which are not greater than 2^(N-1) + 1.
417 // In result, for any particular NumHashed we return a prime number
418 // which is not greater than NumHashed.
419 static const unsigned Primes[] = {
420 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
421 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
422
423 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
424 array_lengthof(Primes) - 1)];
425}
426
427// Bloom filter estimation: at least 8 bits for each hashed symbol.
428// GNU Hash table requirement: it should be a power of 2,
429// the minimum value is 1, even for an empty table.
430// Expected results for a 32-bit target:
431// calcMaskWords(0..4) = 1
432// calcMaskWords(5..8) = 2
433// calcMaskWords(9..16) = 4
434// For a 64-bit target:
435// calcMaskWords(0..8) = 1
436// calcMaskWords(9..16) = 2
437// calcMaskWords(17..32) = 4
438template <class ELFT>
439unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
440 if (!NumHashed)
441 return 1;
442 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
443}
444
445template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000446 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000447 NBuckets = calcNBuckets(NumHashed);
448 MaskWords = calcMaskWords(NumHashed);
449 // Second hash shift estimation: just predefined values.
450 Shift2 = ELFT::Is64Bits ? 6 : 5;
451
452 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
453 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
454 + sizeof(Elf_Off) * MaskWords // Bloom Filter
455 + sizeof(Elf_Word) * NBuckets // Hash Buckets
456 + sizeof(Elf_Word) * NumHashed; // Hash Values
457}
458
459template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
460 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000461 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000462 return;
463 writeBloomFilter(Buf);
464 writeHashTable(Buf);
465}
466
467template <class ELFT>
468void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
469 auto *P = reinterpret_cast<Elf_Word *>(Buf);
470 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000471 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000472 *P++ = MaskWords;
473 *P++ = Shift2;
474 Buf = reinterpret_cast<uint8_t *>(P);
475}
476
477template <class ELFT>
478void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
479 unsigned C = sizeof(Elf_Off) * 8;
480
481 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000482 for (const HashedSymbolData &Item : HashedSymbols) {
483 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
484 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
485 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000486 Masks[Pos] |= V;
487 }
488 Buf += sizeof(Elf_Off) * MaskWords;
489}
490
491template <class ELFT>
492void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
493 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
494 Elf_Word *Values = Buckets + NBuckets;
495
496 int PrevBucket = -1;
497 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000498 for (const HashedSymbolData &Item : HashedSymbols) {
499 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000500 assert(PrevBucket <= Bucket);
501 if (Bucket != PrevBucket) {
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000502 Buckets[Bucket] = Item.Body->DynamicSymbolTableIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000503 PrevBucket = Bucket;
504 if (I > 0)
505 Values[I - 1] |= 1;
506 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000507 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000508 ++I;
509 }
510 if (I > 0)
511 Values[I - 1] |= 1;
512}
513
Rafael Espindola31f88882015-11-02 14:33:11 +0000514static bool includeInGnuHashTable(SymbolBody *B) {
515 // Assume that includeInDynamicSymtab() is already checked.
516 return !B->isUndefined();
517}
518
Rafael Espindola35c6af32015-09-25 17:19:10 +0000519template <class ELFT>
Igor Kudrinf1d60292015-10-28 07:05:56 +0000520void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) {
521 std::vector<SymbolBody *> NotHashed;
522 NotHashed.reserve(Symbols.size());
523 HashedSymbols.reserve(Symbols.size());
524 for (SymbolBody *B : Symbols) {
525 if (includeInGnuHashTable(B))
526 HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())});
527 else
528 NotHashed.push_back(B);
529 }
530 if (HashedSymbols.empty())
531 return;
532
533 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
534 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
535 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
536 return L.Hash % NBuckets < R.Hash % NBuckets;
537 });
538
539 Symbols = std::move(NotHashed);
540 for (const HashedSymbolData &Item : HashedSymbols)
541 Symbols.push_back(Item.Body);
542}
543
544template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000545DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000546 : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
547 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000548 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000549 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000550 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
551 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000552
553 // .dynamic section is not writable on MIPS.
554 // See "Special Section" in Chapter 4 in the following document:
555 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
556 if (Config->EMachine == EM_MIPS)
557 Header.sh_flags = llvm::ELF::SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000558}
559
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000560template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000561 if (this->Header.sh_size)
562 return; // Already finalized.
563
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000564 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000565 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000566
567 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000568 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000569 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000570 ++NumEntries; // DT_RELASZ / DT_RELSZ
571 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000572 }
George Rimar648a2c32015-10-20 08:54:27 +0000573 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
574 ++NumEntries; // DT_JMPREL
575 ++NumEntries; // DT_PLTRELSZ
Igor Kudrin304860a2015-11-12 04:39:49 +0000576 ++NumEntries; // DT_PLTGOT / DT_MIPS_PLTGOT
George Rimar648a2c32015-10-20 08:54:27 +0000577 ++NumEntries; // DT_PLTREL
578 }
579
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000580 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000581 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000582 ++NumEntries; // DT_STRTAB
583 ++NumEntries; // DT_STRSZ
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000584 if (Out<ELFT>::GnuHashTab)
585 ++NumEntries; // DT_GNU_HASH
586 if (Out<ELFT>::HashTab)
587 ++NumEntries; // DT_HASH
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000588
Rui Ueyama7de3f372015-10-01 19:36:04 +0000589 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000590 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000591 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000592 }
593
594 if (!Config->SoName.empty()) {
595 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000596 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000597 }
598
Rafael Espindola77572242015-10-02 19:37:55 +0000599 if (PreInitArraySec)
600 NumEntries += 2;
601 if (InitArraySec)
602 NumEntries += 2;
603 if (FiniArraySec)
604 NumEntries += 2;
605
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000606 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000607 if (!F->isNeeded())
608 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000609 Out<ELFT>::DynStrTab->add(F->getSoName());
610 ++NumEntries;
611 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000612
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000613 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
614 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
615 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
616 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000617 if (InitSym)
618 ++NumEntries; // DT_INIT
619 if (FiniSym)
620 ++NumEntries; // DT_FINI
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000621
622 if (Config->Bsymbolic)
623 DtFlags |= DF_SYMBOLIC;
624 if (Config->ZNodelete)
625 DtFlags1 |= DF_1_NODELETE;
626 if (Config->ZNow) {
627 DtFlags |= DF_BIND_NOW;
628 DtFlags1 |= DF_1_NOW;
629 }
630 if (Config->ZOrigin) {
631 DtFlags |= DF_ORIGIN;
632 DtFlags1 |= DF_1_ORIGIN;
633 }
634
635 if (DtFlags)
Davide Italiano58cbaf02015-10-19 23:32:16 +0000636 ++NumEntries; // DT_FLAGS
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000637 if (DtFlags1)
George Rimar97aad172015-10-07 15:00:21 +0000638 ++NumEntries; // DT_FLAGS_1
Igor Kudrin304860a2015-11-12 04:39:49 +0000639
640 if (Config->EMachine == EM_MIPS) {
641 ++NumEntries; // DT_MIPS_RLD_VERSION
642 ++NumEntries; // DT_MIPS_FLAGS
643 ++NumEntries; // DT_MIPS_BASE_ADDRESS
644 ++NumEntries; // DT_MIPS_SYMTABNO
645 ++NumEntries; // DT_MIPS_LOCAL_GOTNO
646 ++NumEntries; // DT_MIPS_GOTSYM;
647 ++NumEntries; // DT_PLTGOT
648 if (Out<ELFT>::MipsRldMap)
649 ++NumEntries; // DT_MIPS_RLD_MAP
650 }
651
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000652 ++NumEntries; // DT_NULL
653
654 Header.sh_size = NumEntries * Header.sh_entsize;
655}
656
657template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000658 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
659
Rui Ueyama8c205d52015-10-02 01:33:31 +0000660 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
661 P->d_tag = Tag;
662 P->d_un.d_ptr = Val;
663 ++P;
664 };
665
666 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
667 P->d_tag = Tag;
668 P->d_un.d_val = Val;
669 ++P;
670 };
671
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000672 if (Out<ELFT>::RelaDyn->hasRelocs()) {
673 bool IsRela = Out<ELFT>::RelaDyn->isRela();
674 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
675 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000676 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
677 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000678 }
George Rimar648a2c32015-10-20 08:54:27 +0000679 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
680 WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
681 WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
Igor Kudrin304860a2015-11-12 04:39:49 +0000682 // On MIPS, the address of the .got.plt section is stored in
683 // the DT_MIPS_PLTGOT entry because the DT_PLTGOT entry points to
684 // the .got section. See "Dynamic Section" in the following document:
685 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
686 WritePtr((Config->EMachine == EM_MIPS) ? DT_MIPS_PLTGOT : DT_PLTGOT,
687 Out<ELFT>::GotPlt->getVA());
George Rimar648a2c32015-10-20 08:54:27 +0000688 WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
689 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000690
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000691 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000692 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000693 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
694 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000695 if (Out<ELFT>::GnuHashTab)
696 WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA());
697 if (Out<ELFT>::HashTab)
698 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000699
Rui Ueyamadfa577b2015-11-19 23:30:10 +0000700 // If --enable-new-dtags is set, lld emits DT_RUNPATH
701 // instead of DT_RPATH. The two tags are functionally
702 // equivalent except for the following:
703 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
704 // DT_RPATH is searched before.
705 // - DT_RUNPATH is used only to search for direct
706 // dependencies of the object it's contained in, while
707 // DT_RPATH is used for indirect dependencies as well.
Rui Ueyama8c205d52015-10-02 01:33:31 +0000708 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000709 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000710 Out<ELFT>::DynStrTab->getOffset(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000711
Rui Ueyama8c205d52015-10-02 01:33:31 +0000712 if (!Config->SoName.empty())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000713 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000714
Rafael Espindola77572242015-10-02 19:37:55 +0000715 auto WriteArray = [&](int32_t T1, int32_t T2,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000716 const OutputSectionBase<ELFT> *Sec) {
Rafael Espindola77572242015-10-02 19:37:55 +0000717 if (!Sec)
718 return;
719 WritePtr(T1, Sec->getVA());
720 WriteVal(T2, Sec->getSize());
721 };
722 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
723 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
724 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
725
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000726 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000727 if (F->isNeeded())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000728 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000729
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000730 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000731 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000732 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000733 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000734 if (DtFlags)
735 WriteVal(DT_FLAGS, DtFlags);
736 if (DtFlags1)
737 WriteVal(DT_FLAGS_1, DtFlags1);
Igor Kudrin304860a2015-11-12 04:39:49 +0000738
739 // See "Dynamic Section" in Chapter 5 in the following document
740 // for detailed description:
741 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
742 if (Config->EMachine == EM_MIPS) {
743 WriteVal(DT_MIPS_RLD_VERSION, 1);
744 WriteVal(DT_MIPS_FLAGS, RHF_NOTPOT);
745 WritePtr(DT_MIPS_BASE_ADDRESS, Target->getVAStart());
746 WriteVal(DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols());
747 WriteVal(DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum());
748 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000749 WriteVal(DT_MIPS_GOTSYM, B->DynamicSymbolTableIndex);
Igor Kudrin304860a2015-11-12 04:39:49 +0000750 else
751 WriteVal(DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols());
752 WritePtr(DT_PLTGOT, Out<ELFT>::Got->getVA());
753 if (Out<ELFT>::MipsRldMap)
754 WritePtr(DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap->getVA());
755 }
756
Rui Ueyama8c205d52015-10-02 01:33:31 +0000757 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000758}
759
760template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000761OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000762 uintX_t sh_flags)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000763 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000764
765template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000766void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
767 Sections.push_back(C);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000768 C->OutSec = this;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000769 uint32_t Align = C->getAlign();
770 if (Align > this->Header.sh_addralign)
771 this->Header.sh_addralign = Align;
772
773 uintX_t Off = this->Header.sh_size;
774 Off = RoundUpToAlignment(Off, Align);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000775 C->OutSecOff = Off;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000776 Off += C->getSize();
777 this->Header.sh_size = Off;
778}
779
780template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000781typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000782 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000783 case SymbolBody::DefinedSyntheticKind: {
784 auto &D = cast<DefinedSynthetic<ELFT>>(S);
785 return D.Section.getVA() + D.Sym.st_value;
786 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000787 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000788 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000789 case SymbolBody::DefinedRegularKind: {
790 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindola48225b42015-10-23 19:55:11 +0000791 InputSectionBase<ELFT> &SC = DR.Section;
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000792 if (DR.Sym.getType() == STT_TLS)
793 return SC.OutSec->getVA() + SC.getOffset(DR.Sym) -
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000794 Out<ELFT>::TlsPhdr->p_vaddr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000795 return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
Rafael Espindolacd076f02015-09-25 18:19:03 +0000796 }
797 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000798 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
George Rimarbc590fe2015-10-28 16:48:58 +0000799 case SymbolBody::SharedKind: {
800 auto &SS = cast<SharedSymbol<ELFT>>(S);
Rui Ueyamabb936062015-12-17 01:14:23 +0000801 if (SS.NeedsCopy)
George Rimarbc590fe2015-10-28 16:48:58 +0000802 return Out<ELFT>::Bss->getVA() + SS.OffsetInBSS;
803 return 0;
804 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000805 case SymbolBody::UndefinedKind:
806 return 0;
807 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000808 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
809 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000810 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000811 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000812}
813
Rui Ueyama34f29242015-10-13 19:51:57 +0000814// Returns a VA which a relocatin RI refers to. Used only for local symbols.
815// For non-local symbols, use getSymVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000816template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000817typename ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000818lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
Rafael Espindola932efcf2015-10-19 20:24:44 +0000819 const Elf_Rel_Impl<ELFT, IsRela> &RI) {
820 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
821 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
822
823 uintX_t Addend = getAddend<ELFT>(RI);
824
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000825 // PPC64 has a special relocation representing the TOC base pointer
826 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000827 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000828 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000829
Rui Ueyama126d08f2015-10-12 20:28:22 +0000830 const Elf_Sym *Sym =
831 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000832
Rui Ueyama126d08f2015-10-12 20:28:22 +0000833 if (!Sym)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000834 error("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000835
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000836 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
837
838 if (Sym->getType() == STT_TLS)
839 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
George Rimarcc06a6f2015-11-29 14:14:20 +0000840 Out<ELFT>::TlsPhdr->p_vaddr;
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000841
Rafael Espindola444576d2015-10-09 19:25:07 +0000842 // According to the ELF spec reference to a local symbol from outside
843 // the group are not allowed. Unfortunately .eh_frame breaks that rule
844 // and must be treated specially. For now we just replace the symbol with
845 // 0.
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000846 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000847 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000848
Rafael Espindolac159c962015-10-19 21:00:02 +0000849 uintX_t VA = Section->OutSec->getVA();
850 if (isa<InputSection<ELFT>>(Section))
851 return VA + Section->getOffset(*Sym) + Addend;
852
853 uintX_t Offset = Sym->st_value;
854 if (Sym->getType() == STT_SECTION) {
855 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000856 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000857 }
858 return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
859 Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000860}
861
Rui Ueyama34f29242015-10-13 19:51:57 +0000862// Returns true if a symbol can be replaced at load-time by a symbol
863// with the same name defined in other ELF executable or DSO.
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000864bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000865 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000866 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000867 if (Body->isShared())
868 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000869
870 if (Body->isUndefined()) {
871 if (!Body->isWeak())
872 return true;
873
874 // This is an horrible corner case. Ideally we would like to say that any
875 // undefined symbol can be preempted so that the dynamic linker has a
876 // chance of finding it at runtime.
877 //
878 // The problem is that the code sequence used to test for weak undef
879 // functions looks like
880 // if (func) func()
881 // If the code is -fPIC the first reference is a load from the got and
882 // everything works.
883 // If the code is not -fPIC there is no reasonable way to solve it:
884 // * A relocation writing to the text segment will fail (it is ro).
885 // * A copy relocation doesn't work for functions.
886 // * The trick of using a plt entry as the address would fail here since
887 // the plt entry would have a non zero address.
888 // Since we cannot do anything better, we just resolve the symbol to 0 and
889 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000890 //
891 // As an extra hack, assume that if we are producing a shared library the
892 // user knows what he or she is doing and can handle a dynamic relocation.
893 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000894 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000895 if (!Config->Shared)
896 return false;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000897 return Body->getVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000898}
899
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000900template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000901 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000902 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000903}
904
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000905template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000906EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t sh_type,
907 uintX_t sh_flags)
908 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
909
910template <class ELFT>
911EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
912 : S(S), Index(Index) {}
913
914template <class ELFT> StringRef EHRegion<ELFT>::data() const {
915 ArrayRef<uint8_t> SecData = S->getSectionData();
916 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
917 size_t Start = Offsets[Index].first;
918 size_t End =
919 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
920 return StringRef((const char *)SecData.data() + Start, End - Start);
921}
922
923template <class ELFT>
924Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
925 : EHRegion<ELFT>(S, Index) {}
926
927template <class ELFT>
928template <bool IsRela>
929void EHOutputSection<ELFT>::addSectionAux(
930 EHInputSection<ELFT> *S,
931 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
932 const endianness E = ELFT::TargetEndianness;
933
934 S->OutSec = this;
935 uint32_t Align = S->getAlign();
936 if (Align > this->Header.sh_addralign)
937 this->Header.sh_addralign = Align;
938
939 Sections.push_back(S);
940
941 ArrayRef<uint8_t> SecData = S->getSectionData();
942 ArrayRef<uint8_t> D = SecData;
943 uintX_t Offset = 0;
944 auto RelI = Rels.begin();
945 auto RelE = Rels.end();
946
947 DenseMap<unsigned, unsigned> OffsetToIndex;
948 while (!D.empty()) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000949 unsigned Index = S->Offsets.size();
950 S->Offsets.push_back(std::make_pair(Offset, -1));
951
George Rimar003be4f2015-12-17 09:23:40 +0000952 uintX_t Length = readEntryLength(D);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000953 StringRef Entry((const char *)D.data(), Length);
954
955 while (RelI != RelE && RelI->r_offset < Offset)
956 ++RelI;
957 uintX_t NextOffset = Offset + Length;
958 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
959
960 uint32_t ID = read32<E>(D.data() + 4);
961 if (ID == 0) {
962 // CIE
963 Cie<ELFT> C(S, Index);
964
965 StringRef Personality;
966 if (HasReloc) {
967 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
968 SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl();
969 Personality = Body.getName();
970 }
971
972 std::pair<StringRef, StringRef> CieInfo(Entry, Personality);
973 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
974 if (P.second) {
975 Cies.push_back(C);
George Rimare72beba2015-12-21 09:38:59 +0000976 this->Header.sh_size += RoundUpToAlignment(Length, sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000977 }
978 OffsetToIndex[Offset] = P.first->second;
979 } else {
980 if (!HasReloc)
981 error("FDE doesn't reference another section");
982 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
983 if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) {
984 uint32_t CieOffset = Offset + 4 - ID;
985 auto I = OffsetToIndex.find(CieOffset);
986 if (I == OffsetToIndex.end())
987 error("Invalid CIE reference");
988 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
George Rimare72beba2015-12-21 09:38:59 +0000989 this->Header.sh_size += RoundUpToAlignment(Length, sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000990 }
991 }
992
993 Offset = NextOffset;
994 D = D.slice(Length);
995 }
996}
997
998template <class ELFT>
George Rimar003be4f2015-12-17 09:23:40 +0000999typename EHOutputSection<ELFT>::uintX_t
1000EHOutputSection<ELFT>::readEntryLength(ArrayRef<uint8_t> D) {
1001 const endianness E = ELFT::TargetEndianness;
1002
1003 if (D.size() < 4)
1004 error("Truncated CIE/FDE length");
1005 uint64_t Len = read32<E>(D.data());
1006 if (Len < UINT32_MAX) {
1007 if (Len > (UINT32_MAX - 4))
1008 error("CIE/FIE size is too large");
1009 if (Len + 4 > D.size())
1010 error("CIE/FIE ends past the end of the section");
1011 return Len + 4;
1012 }
1013
1014 if (D.size() < 12)
1015 error("Truncated CIE/FDE length");
1016 Len = read64<E>(D.data() + 4);
1017 if (Len > (UINT64_MAX - 12))
1018 error("CIE/FIE size is too large");
1019 if (Len + 12 > D.size())
1020 error("CIE/FIE ends past the end of the section");
1021 return Len + 12;
1022}
1023
1024template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001025void EHOutputSection<ELFT>::addSection(EHInputSection<ELFT> *S) {
1026 const Elf_Shdr *RelSec = S->RelocSection;
1027 if (!RelSec)
1028 return addSectionAux(
1029 S, make_range((const Elf_Rela *)nullptr, (const Elf_Rela *)nullptr));
1030 ELFFile<ELFT> &Obj = S->getFile()->getObj();
1031 if (RelSec->sh_type == SHT_RELA)
1032 return addSectionAux(S, Obj.relas(RelSec));
1033 return addSectionAux(S, Obj.rels(RelSec));
1034}
1035
1036template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1037 const endianness E = ELFT::TargetEndianness;
1038 size_t Offset = 0;
1039 for (const Cie<ELFT> &C : Cies) {
1040 size_t CieOffset = Offset;
1041
1042 StringRef CieData = C.data();
1043 memcpy(Buf + Offset, CieData.data(), CieData.size());
1044 C.S->Offsets[C.Index].second = Offset;
George Rimare72beba2015-12-21 09:38:59 +00001045 Offset += RoundUpToAlignment(CieData.size(), sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001046
1047 for (const EHRegion<ELFT> &F : C.Fdes) {
1048 StringRef FdeData = F.data();
George Rimare72beba2015-12-21 09:38:59 +00001049 uintX_t Len = RoundUpToAlignment(FdeData.size(), sizeof(uintX_t));
1050 write32<E>(Buf + Offset, Len - 4); // Length
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001051 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
1052 memcpy(Buf + Offset + 8, FdeData.data() + 8, FdeData.size() - 8);
1053 F.S->Offsets[F.Index].second = Offset;
George Rimare72beba2015-12-21 09:38:59 +00001054 Offset += Len;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001055 }
1056 }
1057
1058 for (EHInputSection<ELFT> *S : Sections) {
1059 const Elf_Shdr *RelSec = S->RelocSection;
1060 if (!RelSec)
1061 continue;
1062 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1063 if (RelSec->sh_type == SHT_RELA)
1064 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1065 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001066 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001067 }
1068}
1069
1070template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +00001071MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
1072 uintX_t sh_flags)
1073 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
1074
1075template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001076 if (shouldTailMerge()) {
1077 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001078 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001079 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001080 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001081 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1082 StringRef Data = P.first;
1083 memcpy(Buf + P.second, Data.data(), Data.size());
1084 }
1085}
1086
1087static size_t findNull(StringRef S, size_t EntSize) {
1088 // Optimize the common case.
1089 if (EntSize == 1)
1090 return S.find(0);
1091
1092 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1093 const char *B = S.begin() + I;
1094 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1095 return I;
1096 }
1097 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001098}
1099
1100template <class ELFT>
1101void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
1102 S->OutSec = this;
1103 uint32_t Align = S->getAlign();
1104 if (Align > this->Header.sh_addralign)
1105 this->Header.sh_addralign = Align;
1106
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001107 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001108 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001109 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001110 uintX_t Offset = 0;
1111
1112 if (this->Header.sh_flags & SHF_STRINGS) {
1113 while (!Data.empty()) {
1114 size_t End = findNull(Data, EntSize);
1115 if (End == StringRef::npos)
1116 error("String is not null terminated");
1117 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001118 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001119 if (shouldTailMerge())
1120 OutputOffset = -1;
1121 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1122 uintX_t Size = End + EntSize;
1123 Data = Data.substr(Size);
1124 Offset += Size;
1125 }
1126 } else {
1127 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1128 StringRef Entry = Data.substr(I, EntSize);
1129 size_t OutputOffset = Builder.add(Entry);
1130 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1131 Offset += EntSize;
1132 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001133 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001134}
1135
1136template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001137unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1138 return Builder.getOffset(Val);
1139}
1140
1141template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1142 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1143}
1144
1145template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1146 if (shouldTailMerge())
1147 Builder.finalize();
1148 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001149}
1150
1151template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001152StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
1153 : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001154 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001155 Dynamic(Dynamic) {
1156 this->Header.sh_addralign = 1;
1157}
1158
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001159template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001160 StringRef Data = StrTabBuilder.data();
1161 memcpy(Buf, Data.data(), Data.size());
1162}
1163
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001164template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001165 if (!B.isUsedInRegularObj())
1166 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001167
1168 // Don't include synthetic symbols like __init_array_start in every output.
1169 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
1170 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
1171 return false;
1172
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001173 return true;
1174}
1175
Rafael Espindola05a3dd22015-09-22 23:38:23 +00001176bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001177 uint8_t V = B.getVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001178 if (V != STV_DEFAULT && V != STV_PROTECTED)
1179 return false;
1180
Rafael Espindola05a3dd22015-09-22 23:38:23 +00001181 if (Config->ExportDynamic || Config->Shared)
1182 return true;
1183 return B.isUsedInDynamicReloc();
1184}
1185
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001186template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +00001187bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
1188 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001189 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
1190 if (Sym.getType() == STT_SECTION)
1191 return false;
1192
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001193 InputSectionBase<ELFT> *Sec = File.getSection(Sym);
Rafael Espindola444576d2015-10-09 19:25:07 +00001194 // If sym references a section in a discarded group, don't keep it.
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001195 if (Sec == &InputSection<ELFT>::Discarded)
Rafael Espindola4cda5812015-10-16 15:29:48 +00001196 return false;
Rafael Espindola444576d2015-10-09 19:25:07 +00001197
Davide Italiano6993ba42015-09-26 00:47:56 +00001198 if (Config->DiscardNone)
1199 return true;
1200
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001201 // In ELF assembly .L symbols are normally discarded by the assembler.
1202 // If the assembler fails to do so, the linker discards them if
1203 // * --discard-locals is used.
1204 // * The symbol is in a SHF_MERGE section, which is normally the reason for
1205 // the assembler keeping the .L symbol.
Rafael Espindola29925632015-12-11 19:09:21 +00001206 if (!SymName.startswith(".L") && !SymName.empty())
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001207 return true;
1208
1209 if (Config->DiscardLocals)
1210 return false;
1211
1212 return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE);
Davide Italiano6993ba42015-09-26 00:47:56 +00001213}
1214
Rafael Espindola35c6af32015-09-25 17:19:10 +00001215template <class ELFT>
1216SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001217 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
1218 : OutputSectionBase<ELFT>(
Rafael Espindola35c6af32015-09-25 17:19:10 +00001219 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1220 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
1221 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001222 Table(Table), StrTabSec(StrTabSec) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001223 typedef OutputSectionBase<ELFT> Base;
1224 typename Base::Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +00001225
1226 Header.sh_entsize = sizeof(Elf_Sym);
1227 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
1228}
1229
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001230// Orders symbols according to their positions in the GOT,
1231// in compliance with MIPS ABI rules.
1232// See "Global Offset Table" in Chapter 5 in the following document
1233// for detailed description:
1234// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1235static bool sortMipsSymbols(SymbolBody *L, SymbolBody *R) {
1236 if (!L->isInGot() || !R->isInGot())
1237 return R->isInGot();
1238 return L->GotIndex < R->GotIndex;
1239}
1240
Rui Ueyama0db335f2015-10-07 16:58:54 +00001241template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001242 if (this->Header.sh_size)
1243 return; // Already finalized.
1244
Rui Ueyama0db335f2015-10-07 16:58:54 +00001245 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001246 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001247 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001248
1249 if (!StrTabSec.isDynamic()) {
1250 std::stable_sort(Symbols.begin(), Symbols.end(),
Igor Kudrinf1d60292015-10-28 07:05:56 +00001251 [](SymbolBody *L, SymbolBody *R) {
1252 return getSymbolBinding(L) == STB_LOCAL &&
1253 getSymbolBinding(R) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001254 });
1255 return;
1256 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001257 if (Out<ELFT>::GnuHashTab)
1258 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1259 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001260 else if (Config->EMachine == EM_MIPS)
1261 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001262 size_t I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +00001263 for (SymbolBody *B : Symbols)
Rui Ueyamaa02bba62015-12-17 00:12:04 +00001264 B->DynamicSymbolTableIndex = ++I;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001265}
1266
1267template <class ELFT>
Igor Kudrinab665fc2015-10-20 21:47:58 +00001268void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
Rui Ueyama0db335f2015-10-07 16:58:54 +00001269 StrTabSec.add(Name);
1270 ++NumVisible;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001271 ++NumLocals;
1272}
1273
1274template <class ELFT>
1275void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
1276 StrTabSec.add(Body->getName());
Igor Kudrinf1d60292015-10-28 07:05:56 +00001277 Symbols.push_back(Body);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001278 ++NumVisible;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001279}
1280
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001281template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001282 Buf += sizeof(Elf_Sym);
1283
1284 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1285 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001286 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1287 writeLocalSymbols(Buf);
1288
1289 writeGlobalSymbols(Buf);
1290}
1291
1292template <class ELFT>
1293void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1294 // Iterate over all input object files to copy their local symbols
1295 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001296 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
1297 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001298 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001299 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +00001300 error(SymNameOrErr);
1301 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001302 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001303 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +00001304
Rui Ueyamac55733e2015-09-30 00:54:29 +00001305 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001306 uintX_t VA = 0;
Rafael Espindola4cda5812015-10-16 15:29:48 +00001307 if (Sym.st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001308 ESym->st_shndx = SHN_ABS;
Rafael Espindolac159c962015-10-19 21:00:02 +00001309 VA = Sym.st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001310 } else {
Rafael Espindola48225b42015-10-23 19:55:11 +00001311 InputSectionBase<ELFT> *Section = File->getSection(Sym);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001312 if (!Section->isLive())
1313 continue;
Rafael Espindolac159c962015-10-19 21:00:02 +00001314 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1315 ESym->st_shndx = OutSec->SectionIndex;
1316 VA += OutSec->getVA() + Section->getOffset(Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001317 }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001318 ESym->st_name = StrTabSec.getOffset(SymName);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001319 ESym->st_size = Sym.st_size;
1320 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001321 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001322 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001323 }
1324 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001325}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001326
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001327template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001328void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001329 // Write the internal symbol table contents to the output symbol table
1330 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001331 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +00001332 for (SymbolBody *Body : Symbols) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001333 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001334
Rafael Espindola8614c562015-10-06 14:33:58 +00001335 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001336 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001337 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001338 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001339 case SymbolBody::DefinedRegularKind: {
1340 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
1341 if (!Sym->Section.isLive())
1342 continue;
1343 OutSec = Sym->Section.OutSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001344 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001345 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001346 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001347 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001348 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001349 case SymbolBody::SharedKind: {
Rui Ueyamabb936062015-12-17 01:14:23 +00001350 if (cast<SharedSymbol<ELFT>>(Body)->NeedsCopy)
George Rimarbc590fe2015-10-28 16:48:58 +00001351 OutSec = Out<ELFT>::Bss;
1352 break;
1353 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001354 case SymbolBody::UndefinedKind:
1355 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001356 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001357 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001358 }
1359
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001360 StringRef Name = Body->getName();
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001361 ESym->st_name = StrTabSec.getOffset(Name);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001362
Rafael Espindola8614c562015-10-06 14:33:58 +00001363 unsigned char Type = STT_NOTYPE;
1364 uintX_t Size = 0;
1365 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
1366 const Elf_Sym &InputSym = EBody->Sym;
Rafael Espindola8614c562015-10-06 14:33:58 +00001367 Type = InputSym.getType();
1368 Size = InputSym.st_size;
1369 }
1370
Igor Kudrin853b88d2015-10-20 20:52:14 +00001371 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001372 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001373 ESym->setVisibility(Body->getVisibility());
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001374 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001375
Rafael Espindola8614c562015-10-06 14:33:58 +00001376 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +00001377 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +00001378 else if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001379 ESym->st_shndx = OutSec->SectionIndex;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001380
1381 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001382 }
1383}
1384
Igor Kudrin853b88d2015-10-20 20:52:14 +00001385template <class ELFT>
1386uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001387 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001388 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1389 return STB_LOCAL;
1390 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body))
1391 return EBody->Sym.getBinding();
1392 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1393}
1394
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001395template <class ELFT>
1396MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1397 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1398 this->Header.sh_addralign = 4;
1399 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1400 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1401}
1402
1403template <class ELFT>
1404void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1405 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
1406 R->ri_gp_value = getMipsGpAddr<ELFT>();
1407 R->ri_gprmask = GeneralMask;
1408}
1409
1410template <class ELFT>
1411void MipsReginfoOutputSection<ELFT>::addSection(
1412 MipsReginfoInputSection<ELFT> *S) {
1413 GeneralMask |= S->getGeneralMask();
1414}
1415
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001416namespace lld {
1417namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001418template class OutputSectionBase<ELF32LE>;
1419template class OutputSectionBase<ELF32BE>;
1420template class OutputSectionBase<ELF64LE>;
1421template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001422
George Rimar648a2c32015-10-20 08:54:27 +00001423template class GotPltSection<ELF32LE>;
1424template class GotPltSection<ELF32BE>;
1425template class GotPltSection<ELF64LE>;
1426template class GotPltSection<ELF64BE>;
1427
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001428template class GotSection<ELF32LE>;
1429template class GotSection<ELF32BE>;
1430template class GotSection<ELF64LE>;
1431template class GotSection<ELF64BE>;
1432
1433template class PltSection<ELF32LE>;
1434template class PltSection<ELF32BE>;
1435template class PltSection<ELF64LE>;
1436template class PltSection<ELF64BE>;
1437
1438template class RelocationSection<ELF32LE>;
1439template class RelocationSection<ELF32BE>;
1440template class RelocationSection<ELF64LE>;
1441template class RelocationSection<ELF64BE>;
1442
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001443template class InterpSection<ELF32LE>;
1444template class InterpSection<ELF32BE>;
1445template class InterpSection<ELF64LE>;
1446template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001447
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001448template class GnuHashTableSection<ELF32LE>;
1449template class GnuHashTableSection<ELF32BE>;
1450template class GnuHashTableSection<ELF64LE>;
1451template class GnuHashTableSection<ELF64BE>;
1452
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001453template class HashTableSection<ELF32LE>;
1454template class HashTableSection<ELF32BE>;
1455template class HashTableSection<ELF64LE>;
1456template class HashTableSection<ELF64BE>;
1457
1458template class DynamicSection<ELF32LE>;
1459template class DynamicSection<ELF32BE>;
1460template class DynamicSection<ELF64LE>;
1461template class DynamicSection<ELF64BE>;
1462
1463template class OutputSection<ELF32LE>;
1464template class OutputSection<ELF32BE>;
1465template class OutputSection<ELF64LE>;
1466template class OutputSection<ELF64BE>;
1467
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001468template class EHOutputSection<ELF32LE>;
1469template class EHOutputSection<ELF32BE>;
1470template class EHOutputSection<ELF64LE>;
1471template class EHOutputSection<ELF64BE>;
1472
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001473template class MipsReginfoOutputSection<ELF32LE>;
1474template class MipsReginfoOutputSection<ELF32BE>;
1475template class MipsReginfoOutputSection<ELF64LE>;
1476template class MipsReginfoOutputSection<ELF64BE>;
1477
Rafael Espindolac159c962015-10-19 21:00:02 +00001478template class MergeOutputSection<ELF32LE>;
1479template class MergeOutputSection<ELF32BE>;
1480template class MergeOutputSection<ELF64LE>;
1481template class MergeOutputSection<ELF64BE>;
1482
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001483template class StringTableSection<ELF32LE>;
1484template class StringTableSection<ELF32BE>;
1485template class StringTableSection<ELF64LE>;
1486template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001487
1488template class SymbolTableSection<ELF32LE>;
1489template class SymbolTableSection<ELF32BE>;
1490template class SymbolTableSection<ELF64LE>;
1491template class SymbolTableSection<ELF64BE>;
1492
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001493template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1494template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1495template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1496template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +00001497
Rafael Espindola56f965f2015-09-21 22:48:12 +00001498template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001499getLocalRelTarget(const ObjectFile<ELF32LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001500 const ELFFile<ELF32LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001501template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001502getLocalRelTarget(const ObjectFile<ELF32BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001503 const ELFFile<ELF32BE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001504template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001505getLocalRelTarget(const ObjectFile<ELF64LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001506 const ELFFile<ELF64LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001507template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001508getLocalRelTarget(const ObjectFile<ELF64BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001509 const ELFFile<ELF64BE>::Elf_Rel &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001510
Rafael Espindolac159c962015-10-19 21:00:02 +00001511template ELFFile<ELF32LE>::uintX_t
1512getLocalRelTarget(const ObjectFile<ELF32LE> &,
1513 const ELFFile<ELF32LE>::Elf_Rela &);
1514template ELFFile<ELF32BE>::uintX_t
1515getLocalRelTarget(const ObjectFile<ELF32BE> &,
1516 const ELFFile<ELF32BE>::Elf_Rela &);
1517template ELFFile<ELF64LE>::uintX_t
1518getLocalRelTarget(const ObjectFile<ELF64LE> &,
1519 const ELFFile<ELF64LE>::Elf_Rela &);
1520template ELFFile<ELF64BE>::uintX_t
1521getLocalRelTarget(const ObjectFile<ELF64BE> &,
1522 const ELFFile<ELF64BE>::Elf_Rela &);
1523
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001524template bool includeInSymtab<ELF32LE>(const SymbolBody &);
1525template bool includeInSymtab<ELF32BE>(const SymbolBody &);
1526template bool includeInSymtab<ELF64LE>(const SymbolBody &);
1527template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001528
Rafael Espindola444576d2015-10-09 19:25:07 +00001529template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1530 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001531 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001532template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1533 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001534 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001535template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1536 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001537 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001538template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1539 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001540 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001541}
1542}