blob: 50f48022940f567af2e5adc2fdfe73058713b444 [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
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000024template <class ELFT>
25OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t sh_type,
26 uintX_t sh_flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027 : Name(Name) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000028 memset(&Header, 0, sizeof(Elf_Shdr));
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029 Header.sh_type = sh_type;
30 Header.sh_flags = sh_flags;
31}
32
Rafael Espindola35c6af32015-09-25 17:19:10 +000033template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000034GotPltSection<ELFT>::GotPltSection()
35 : OutputSectionBase<ELFT>(".got.plt", llvm::ELF::SHT_PROGBITS,
36 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
37 this->Header.sh_addralign = sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000038}
39
40template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000041 Sym->GotPltIndex = Target->getGotPltHeaderEntriesNum() + Entries.size();
George Rimar648a2c32015-10-20 08:54:27 +000042 Entries.push_back(Sym);
43}
44
45template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +000046 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +000047}
48
49template <class ELFT>
50typename GotPltSection<ELFT>::uintX_t
51GotPltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
52 return this->getVA() + B.GotPltIndex * sizeof(uintX_t);
53}
54
55template <class ELFT> void GotPltSection<ELFT>::finalize() {
Igor Kudrin351b41d2015-11-16 17:44:08 +000056 this->Header.sh_size =
57 (Target->getGotPltHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000058}
59
60template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000061 Target->writeGotPltHeaderEntries(Buf);
62 Buf += Target->getGotPltHeaderEntriesNum() * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000063 for (const SymbolBody *B : Entries) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000064 Target->writeGotPltEntry(Buf, Out<ELFT>::Plt->getEntryAddr(*B));
George Rimar648a2c32015-10-20 08:54:27 +000065 Buf += sizeof(uintX_t);
66 }
67}
68
69template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000070GotSection<ELFT>::GotSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000071 : OutputSectionBase<ELFT>(".got", llvm::ELF::SHT_PROGBITS,
72 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000073 if (Config->EMachine == EM_MIPS)
74 this->Header.sh_flags |= llvm::ELF::SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000075 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000076}
77
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000079 Sym->GotIndex = Target->getGotHeaderEntriesNum() + Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080 Entries.push_back(Sym);
George Rimar687138c2015-11-13 16:28:53 +000081}
82
George Rimar90cd0a82015-12-01 19:20:26 +000083template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) {
84 if (Sym->hasGlobalDynIndex())
85 return false;
86 Sym->GlobalDynIndex = Target->getGotHeaderEntriesNum() + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +000087 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +000088 Entries.push_back(Sym);
89 Entries.push_back(nullptr);
George Rimar90cd0a82015-12-01 19:20:26 +000090 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000091}
92
George Rimar95c1a582015-12-07 08:02:20 +000093template <class ELFT> bool GotSection<ELFT>::addCurrentModuleTlsIndex() {
George Rimarb17f7392015-12-01 18:24:07 +000094 if (LocalTlsIndexOff != uint32_t(-1))
95 return false;
Michael J. Spencer1e225612015-11-11 01:00:24 +000096 Entries.push_back(nullptr);
97 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +000098 LocalTlsIndexOff = (Entries.size() - 2) * sizeof(uintX_t);
99 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000100}
101
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000102template <class ELFT>
103typename GotSection<ELFT>::uintX_t
104GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama5f551ae2015-10-14 14:02:06 +0000105 return this->getVA() + B.GotIndex * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106}
107
Igor Kudrin304860a2015-11-12 04:39:49 +0000108template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000109typename GotSection<ELFT>::uintX_t
110GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
111 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
112}
113
114template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000115const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
116 return Entries.empty() ? nullptr : Entries.front();
117}
118
119template <class ELFT>
120unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
121 // TODO: Update when the suppoort of GOT entries for local symbols is added.
122 return Target->getGotHeaderEntriesNum();
123}
124
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000125template <class ELFT> void GotSection<ELFT>::finalize() {
126 this->Header.sh_size =
127 (Target->getGotHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
128}
129
Rafael Espindolaa6627382015-10-06 23:56:53 +0000130template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000131 Target->writeGotHeaderEntries(Buf);
132 Buf += Target->getGotHeaderEntriesNum() * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000133 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000134 uint8_t *Entry = Buf;
135 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000136 if (!B)
137 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000138 // MIPS has special rules to fill up GOT entries.
139 // See "Global Offset Table" in Chapter 5 in the following document
140 // for detailed description:
141 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
142 // As the first approach, we can just store addresses for all symbols.
143 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000144 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000145 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +0000146 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000147 }
148}
149
Rafael Espindola35c6af32015-09-25 17:19:10 +0000150template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000151PltSection<ELFT>::PltSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000152 : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS,
153 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000154 this->Header.sh_addralign = 16;
155}
156
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000157template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000158 size_t Off = 0;
George Rimar648a2c32015-10-20 08:54:27 +0000159 bool LazyReloc = Target->supportsLazyRelocations();
160 if (LazyReloc) {
161 // First write PLT[0] entry which is special.
162 Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA());
163 Off += Target->getPltZeroEntrySize();
164 }
George Rimarfb5d7f22015-11-26 19:58:51 +0000165 for (auto &I : Entries) {
George Rimar77b77792015-11-25 22:15:01 +0000166 const SymbolBody *E = I.first;
167 unsigned RelOff = I.second;
168 uint64_t GotVA =
169 LazyReloc ? Out<ELFT>::GotPlt->getVA() : Out<ELFT>::Got->getVA();
170 uint64_t GotE = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
171 : Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000172 uint64_t Plt = this->getVA() + Off;
George Rimar77b77792015-11-25 22:15:01 +0000173 Target->writePltEntry(Buf + Off, GotVA, GotE, Plt, E->PltIndex, RelOff);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000174 Off += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000175 }
176}
177
178template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000179 Sym->PltIndex = Entries.size();
George Rimar77b77792015-11-25 22:15:01 +0000180 unsigned RelOff = Target->supportsLazyRelocations()
181 ? Out<ELFT>::RelaPlt->getRelocOffset()
182 : Out<ELFT>::RelaDyn->getRelocOffset();
183 Entries.push_back(std::make_pair(Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000184}
185
186template <class ELFT>
187typename PltSection<ELFT>::uintX_t
188PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
George Rimar648a2c32015-10-20 08:54:27 +0000189 return this->getVA() + Target->getPltZeroEntrySize() +
190 B.PltIndex * Target->getPltEntrySize();
191}
192
193template <class ELFT> void PltSection<ELFT>::finalize() {
194 this->Header.sh_size = Target->getPltZeroEntrySize() +
195 Entries.size() * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000196}
197
198template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000199RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
200 : OutputSectionBase<ELFT>(Name,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000201 IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
202 llvm::ELF::SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000203 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000204 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
205 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
206}
207
George Rimar5828c232015-11-30 17:49:19 +0000208// Applies corresponding symbol and type for dynamic tls relocation.
209// Returns true if relocation was handled.
210template <class ELFT>
211bool RelocationSection<ELFT>::applyTlsDynamicReloc(SymbolBody *Body,
212 uint32_t Type, Elf_Rel *P,
213 Elf_Rel *N) {
214 if (Target->isTlsLocalDynamicReloc(Type)) {
215 P->setSymbolAndType(0, Target->getTlsModuleIndexReloc(), Config->Mips64EL);
George Rimarb17f7392015-12-01 18:24:07 +0000216 P->r_offset = Out<ELFT>::Got->getLocalTlsIndexVA();
George Rimar5828c232015-11-30 17:49:19 +0000217 return true;
218 }
219
George Rimar25411f252015-12-04 11:20:13 +0000220 if (!Body || !Target->isTlsGlobalDynamicReloc(Type))
221 return false;
222
223 if (Target->isTlsOptimized(Type, Body)) {
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000224 P->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000225 Target->getTlsGotReloc(), Config->Mips64EL);
226 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimar5828c232015-11-30 17:49:19 +0000227 return true;
228 }
George Rimar25411f252015-12-04 11:20:13 +0000229
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000230 P->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000231 Target->getTlsModuleIndexReloc(), Config->Mips64EL);
232 P->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body);
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000233 N->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000234 Target->getTlsOffsetReloc(), Config->Mips64EL);
235 N->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body) + sizeof(uintX_t);
236 return true;
George Rimar5828c232015-11-30 17:49:19 +0000237}
238
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000239template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000240 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000242 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
243 Buf += EntrySize;
244
Michael J. Spencer627ae702015-11-13 00:28:34 +0000245 // Skip placeholder for global dynamic TLS relocation pair. It was already
246 // handled by the previous relocation.
247 if (!Rel.C || !Rel.RI)
248 continue;
249
250 InputSectionBase<ELFT> &C = *Rel.C;
251 const Elf_Rel &RI = *Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000252 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000253 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000254 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000255 if (Body)
256 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000257
Rui Ueyama64558522015-10-16 22:51:43 +0000258 uint32_t Type = RI.getType(Config->Mips64EL);
George Rimar5828c232015-11-30 17:49:19 +0000259 if (applyTlsDynamicReloc(Body, Type, P, reinterpret_cast<Elf_Rel *>(Buf)))
Michael J. Spencer1e225612015-11-11 01:00:24 +0000260 continue;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000261 bool NeedsCopy = Body && Target->needsCopyRel(Type, *Body);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000262 bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
263 bool CanBePreempted = canBePreempted(Body, NeedsGot);
George Rimar648a2c32015-10-20 08:54:27 +0000264 bool LazyReloc = Body && Target->supportsLazyRelocations() &&
265 Target->relocNeedsPlt(Type, *Body);
Rafael Espindola49757522015-10-19 19:58:18 +0000266
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000267 unsigned Sym = CanBePreempted ? Body->DynamicSymbolTableIndex : 0;
George Rimarc7dc0be2015-12-15 08:48:39 +0000268 unsigned Reloc;
269 if (!CanBePreempted)
270 Reloc = Target->getRelativeReloc();
271 else if (LazyReloc)
272 Reloc = Target->getPltReloc();
273 else if (NeedsGot)
Rui Ueyama62d0e322015-12-17 00:04:18 +0000274 Reloc = Body->isTls() ? Target->getTlsGotReloc() : Target->getGotReloc();
George Rimarc7dc0be2015-12-15 08:48:39 +0000275 else if (NeedsCopy)
276 Reloc = Target->getCopyReloc();
277 else
278 Reloc = Target->getDynReloc(Type);
279 P->setSymbolAndType(Sym, Reloc, Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000280
George Rimar648a2c32015-10-20 08:54:27 +0000281 if (NeedsGot) {
282 if (LazyReloc)
283 P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
284 else
285 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimarbc590fe2015-10-28 16:48:58 +0000286 } else if (NeedsCopy) {
287 P->r_offset = Out<ELFT>::Bss->getVA() +
288 dyn_cast<SharedSymbol<ELFT>>(Body)->OffsetInBSS;
George Rimar648a2c32015-10-20 08:54:27 +0000289 } else {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000290 P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA();
George Rimar648a2c32015-10-20 08:54:27 +0000291 }
Rafael Espindola49757522015-10-19 19:58:18 +0000292
Rafael Espindola932efcf2015-10-19 20:24:44 +0000293 uintX_t OrigAddend = 0;
Rafael Espindola49757522015-10-19 19:58:18 +0000294 if (IsRela && !NeedsGot)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000295 OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola49757522015-10-19 19:58:18 +0000296
Rafael Espindola932efcf2015-10-19 20:24:44 +0000297 uintX_t Addend;
George Rimarbc590fe2015-10-28 16:48:58 +0000298 if (NeedsCopy)
299 Addend = 0;
300 else if (CanBePreempted)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000301 Addend = OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000302 else if (Body)
303 Addend = getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body)) + OrigAddend;
304 else if (IsRela)
305 Addend = getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI));
306 else
307 Addend = getLocalRelTarget(File, RI);
Rafael Espindola52dca342015-10-07 00:58:20 +0000308
309 if (IsRela)
310 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000311 }
312}
313
George Rimar77b77792015-11-25 22:15:01 +0000314template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
315 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
316 return EntrySize * Relocs.size();
317}
318
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000319template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000320 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000321 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
322}
323
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000324template <class ELFT>
325InterpSection<ELFT>::InterpSection()
326 : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
327 llvm::ELF::SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000328 this->Header.sh_size = Config->DynamicLinker.size() + 1;
329 this->Header.sh_addralign = 1;
330}
331
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000332template <class ELFT>
333void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
Rui Ueyama157c4332015-10-24 17:57:39 +0000334 Header.sh_name = Out<ELFT>::ShStrTab->getOffset(Name);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000335 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000336}
337
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000338template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000339 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
340}
341
Rafael Espindola35c6af32015-09-25 17:19:10 +0000342template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000343HashTableSection<ELFT>::HashTableSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000344 : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
345 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000346 this->Header.sh_entsize = sizeof(Elf_Word);
347 this->Header.sh_addralign = sizeof(Elf_Word);
348}
349
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000350static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000351 uint32_t H = 0;
352 for (char C : Name) {
353 H = (H << 4) + C;
354 uint32_t G = H & 0xf0000000;
355 if (G)
356 H ^= G >> 24;
357 H &= ~G;
358 }
359 return H;
360}
361
Rui Ueyama0db335f2015-10-07 16:58:54 +0000362template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000363 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000364
Rui Ueyama0db335f2015-10-07 16:58:54 +0000365 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000366 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000367
368 // Create as many buckets as there are symbols.
369 // FIXME: This is simplistic. We can try to optimize it, but implementing
370 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000371 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000372 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
373}
374
375template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000376 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000377 auto *P = reinterpret_cast<Elf_Word *>(Buf);
378 *P++ = NumSymbols; // nbucket
379 *P++ = NumSymbols; // nchain
380
381 Elf_Word *Buckets = P;
382 Elf_Word *Chains = P + NumSymbols;
383
Igor Kudrinf1d60292015-10-28 07:05:56 +0000384 for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) {
Igor Kudrinab665fc2015-10-20 21:47:58 +0000385 StringRef Name = Body->getName();
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000386 unsigned I = Body->DynamicSymbolTableIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000387 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000388 Chains[I] = Buckets[Hash];
389 Buckets[Hash] = I;
390 }
391}
392
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000393static uint32_t hashGnu(StringRef Name) {
394 uint32_t H = 5381;
395 for (uint8_t C : Name)
396 H = (H << 5) + H + C;
397 return H;
398}
399
400template <class ELFT>
401GnuHashTableSection<ELFT>::GnuHashTableSection()
402 : OutputSectionBase<ELFT>(".gnu.hash", llvm::ELF::SHT_GNU_HASH,
403 llvm::ELF::SHF_ALLOC) {
404 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
405 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
406}
407
408template <class ELFT>
409unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
410 if (!NumHashed)
411 return 0;
412
413 // These values are prime numbers which are not greater than 2^(N-1) + 1.
414 // In result, for any particular NumHashed we return a prime number
415 // which is not greater than NumHashed.
416 static const unsigned Primes[] = {
417 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
418 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
419
420 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
421 array_lengthof(Primes) - 1)];
422}
423
424// Bloom filter estimation: at least 8 bits for each hashed symbol.
425// GNU Hash table requirement: it should be a power of 2,
426// the minimum value is 1, even for an empty table.
427// Expected results for a 32-bit target:
428// calcMaskWords(0..4) = 1
429// calcMaskWords(5..8) = 2
430// calcMaskWords(9..16) = 4
431// For a 64-bit target:
432// calcMaskWords(0..8) = 1
433// calcMaskWords(9..16) = 2
434// calcMaskWords(17..32) = 4
435template <class ELFT>
436unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
437 if (!NumHashed)
438 return 1;
439 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
440}
441
442template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000443 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000444 NBuckets = calcNBuckets(NumHashed);
445 MaskWords = calcMaskWords(NumHashed);
446 // Second hash shift estimation: just predefined values.
447 Shift2 = ELFT::Is64Bits ? 6 : 5;
448
449 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
450 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
451 + sizeof(Elf_Off) * MaskWords // Bloom Filter
452 + sizeof(Elf_Word) * NBuckets // Hash Buckets
453 + sizeof(Elf_Word) * NumHashed; // Hash Values
454}
455
456template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
457 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000458 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000459 return;
460 writeBloomFilter(Buf);
461 writeHashTable(Buf);
462}
463
464template <class ELFT>
465void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
466 auto *P = reinterpret_cast<Elf_Word *>(Buf);
467 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000468 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000469 *P++ = MaskWords;
470 *P++ = Shift2;
471 Buf = reinterpret_cast<uint8_t *>(P);
472}
473
474template <class ELFT>
475void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
476 unsigned C = sizeof(Elf_Off) * 8;
477
478 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000479 for (const HashedSymbolData &Item : HashedSymbols) {
480 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
481 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
482 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000483 Masks[Pos] |= V;
484 }
485 Buf += sizeof(Elf_Off) * MaskWords;
486}
487
488template <class ELFT>
489void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
490 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
491 Elf_Word *Values = Buckets + NBuckets;
492
493 int PrevBucket = -1;
494 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000495 for (const HashedSymbolData &Item : HashedSymbols) {
496 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000497 assert(PrevBucket <= Bucket);
498 if (Bucket != PrevBucket) {
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000499 Buckets[Bucket] = Item.Body->DynamicSymbolTableIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000500 PrevBucket = Bucket;
501 if (I > 0)
502 Values[I - 1] |= 1;
503 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000504 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000505 ++I;
506 }
507 if (I > 0)
508 Values[I - 1] |= 1;
509}
510
Rafael Espindola31f88882015-11-02 14:33:11 +0000511static bool includeInGnuHashTable(SymbolBody *B) {
512 // Assume that includeInDynamicSymtab() is already checked.
513 return !B->isUndefined();
514}
515
Rafael Espindola35c6af32015-09-25 17:19:10 +0000516template <class ELFT>
Igor Kudrinf1d60292015-10-28 07:05:56 +0000517void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) {
518 std::vector<SymbolBody *> NotHashed;
519 NotHashed.reserve(Symbols.size());
520 HashedSymbols.reserve(Symbols.size());
521 for (SymbolBody *B : Symbols) {
522 if (includeInGnuHashTable(B))
523 HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())});
524 else
525 NotHashed.push_back(B);
526 }
527 if (HashedSymbols.empty())
528 return;
529
530 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
531 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
532 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
533 return L.Hash % NBuckets < R.Hash % NBuckets;
534 });
535
536 Symbols = std::move(NotHashed);
537 for (const HashedSymbolData &Item : HashedSymbols)
538 Symbols.push_back(Item.Body);
539}
540
541template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000542DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000543 : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
544 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000545 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000546 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000547 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
548 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000549
550 // .dynamic section is not writable on MIPS.
551 // See "Special Section" in Chapter 4 in the following document:
552 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
553 if (Config->EMachine == EM_MIPS)
554 Header.sh_flags = llvm::ELF::SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000555}
556
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000557template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000558 if (this->Header.sh_size)
559 return; // Already finalized.
560
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000561 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000562 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000563
564 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000565 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000566 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000567 ++NumEntries; // DT_RELASZ / DT_RELSZ
568 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000569 }
George Rimar648a2c32015-10-20 08:54:27 +0000570 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
571 ++NumEntries; // DT_JMPREL
572 ++NumEntries; // DT_PLTRELSZ
Igor Kudrin304860a2015-11-12 04:39:49 +0000573 ++NumEntries; // DT_PLTGOT / DT_MIPS_PLTGOT
George Rimar648a2c32015-10-20 08:54:27 +0000574 ++NumEntries; // DT_PLTREL
575 }
576
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000577 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000578 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000579 ++NumEntries; // DT_STRTAB
580 ++NumEntries; // DT_STRSZ
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000581 if (Out<ELFT>::GnuHashTab)
582 ++NumEntries; // DT_GNU_HASH
583 if (Out<ELFT>::HashTab)
584 ++NumEntries; // DT_HASH
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000585
Rui Ueyama7de3f372015-10-01 19:36:04 +0000586 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000587 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000588 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000589 }
590
591 if (!Config->SoName.empty()) {
592 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000593 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000594 }
595
Rafael Espindola77572242015-10-02 19:37:55 +0000596 if (PreInitArraySec)
597 NumEntries += 2;
598 if (InitArraySec)
599 NumEntries += 2;
600 if (FiniArraySec)
601 NumEntries += 2;
602
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000603 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000604 if (!F->isNeeded())
605 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000606 Out<ELFT>::DynStrTab->add(F->getSoName());
607 ++NumEntries;
608 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000609
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000610 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
611 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
612 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
613 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000614 if (InitSym)
615 ++NumEntries; // DT_INIT
616 if (FiniSym)
617 ++NumEntries; // DT_FINI
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000618
619 if (Config->Bsymbolic)
620 DtFlags |= DF_SYMBOLIC;
621 if (Config->ZNodelete)
622 DtFlags1 |= DF_1_NODELETE;
623 if (Config->ZNow) {
624 DtFlags |= DF_BIND_NOW;
625 DtFlags1 |= DF_1_NOW;
626 }
627 if (Config->ZOrigin) {
628 DtFlags |= DF_ORIGIN;
629 DtFlags1 |= DF_1_ORIGIN;
630 }
631
632 if (DtFlags)
Davide Italiano58cbaf02015-10-19 23:32:16 +0000633 ++NumEntries; // DT_FLAGS
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000634 if (DtFlags1)
George Rimar97aad172015-10-07 15:00:21 +0000635 ++NumEntries; // DT_FLAGS_1
Igor Kudrin304860a2015-11-12 04:39:49 +0000636
637 if (Config->EMachine == EM_MIPS) {
638 ++NumEntries; // DT_MIPS_RLD_VERSION
639 ++NumEntries; // DT_MIPS_FLAGS
640 ++NumEntries; // DT_MIPS_BASE_ADDRESS
641 ++NumEntries; // DT_MIPS_SYMTABNO
642 ++NumEntries; // DT_MIPS_LOCAL_GOTNO
643 ++NumEntries; // DT_MIPS_GOTSYM;
644 ++NumEntries; // DT_PLTGOT
645 if (Out<ELFT>::MipsRldMap)
646 ++NumEntries; // DT_MIPS_RLD_MAP
647 }
648
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000649 ++NumEntries; // DT_NULL
650
651 Header.sh_size = NumEntries * Header.sh_entsize;
652}
653
654template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000655 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
656
Rui Ueyama8c205d52015-10-02 01:33:31 +0000657 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
658 P->d_tag = Tag;
659 P->d_un.d_ptr = Val;
660 ++P;
661 };
662
663 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
664 P->d_tag = Tag;
665 P->d_un.d_val = Val;
666 ++P;
667 };
668
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000669 if (Out<ELFT>::RelaDyn->hasRelocs()) {
670 bool IsRela = Out<ELFT>::RelaDyn->isRela();
671 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
672 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000673 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
674 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000675 }
George Rimar648a2c32015-10-20 08:54:27 +0000676 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
677 WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
678 WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
Igor Kudrin304860a2015-11-12 04:39:49 +0000679 // On MIPS, the address of the .got.plt section is stored in
680 // the DT_MIPS_PLTGOT entry because the DT_PLTGOT entry points to
681 // the .got section. See "Dynamic Section" in the following document:
682 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
683 WritePtr((Config->EMachine == EM_MIPS) ? DT_MIPS_PLTGOT : DT_PLTGOT,
684 Out<ELFT>::GotPlt->getVA());
George Rimar648a2c32015-10-20 08:54:27 +0000685 WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
686 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000687
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000688 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000689 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000690 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
691 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000692 if (Out<ELFT>::GnuHashTab)
693 WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA());
694 if (Out<ELFT>::HashTab)
695 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000696
Rui Ueyamadfa577b2015-11-19 23:30:10 +0000697 // If --enable-new-dtags is set, lld emits DT_RUNPATH
698 // instead of DT_RPATH. The two tags are functionally
699 // equivalent except for the following:
700 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
701 // DT_RPATH is searched before.
702 // - DT_RUNPATH is used only to search for direct
703 // dependencies of the object it's contained in, while
704 // DT_RPATH is used for indirect dependencies as well.
Rui Ueyama8c205d52015-10-02 01:33:31 +0000705 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000706 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000707 Out<ELFT>::DynStrTab->getOffset(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000708
Rui Ueyama8c205d52015-10-02 01:33:31 +0000709 if (!Config->SoName.empty())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000710 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000711
Rafael Espindola77572242015-10-02 19:37:55 +0000712 auto WriteArray = [&](int32_t T1, int32_t T2,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000713 const OutputSectionBase<ELFT> *Sec) {
Rafael Espindola77572242015-10-02 19:37:55 +0000714 if (!Sec)
715 return;
716 WritePtr(T1, Sec->getVA());
717 WriteVal(T2, Sec->getSize());
718 };
719 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
720 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
721 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
722
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000723 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000724 if (F->isNeeded())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000725 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000726
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000727 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000728 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000729 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000730 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000731 if (DtFlags)
732 WriteVal(DT_FLAGS, DtFlags);
733 if (DtFlags1)
734 WriteVal(DT_FLAGS_1, DtFlags1);
Igor Kudrin304860a2015-11-12 04:39:49 +0000735
736 // See "Dynamic Section" in Chapter 5 in the following document
737 // for detailed description:
738 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
739 if (Config->EMachine == EM_MIPS) {
740 WriteVal(DT_MIPS_RLD_VERSION, 1);
741 WriteVal(DT_MIPS_FLAGS, RHF_NOTPOT);
742 WritePtr(DT_MIPS_BASE_ADDRESS, Target->getVAStart());
743 WriteVal(DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols());
744 WriteVal(DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum());
745 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000746 WriteVal(DT_MIPS_GOTSYM, B->DynamicSymbolTableIndex);
Igor Kudrin304860a2015-11-12 04:39:49 +0000747 else
748 WriteVal(DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols());
749 WritePtr(DT_PLTGOT, Out<ELFT>::Got->getVA());
750 if (Out<ELFT>::MipsRldMap)
751 WritePtr(DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap->getVA());
752 }
753
Rui Ueyama8c205d52015-10-02 01:33:31 +0000754 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000755}
756
757template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000758OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000759 uintX_t sh_flags)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000760 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000761
762template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000763void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
764 Sections.push_back(C);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000765 C->OutSec = this;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000766 uint32_t Align = C->getAlign();
767 if (Align > this->Header.sh_addralign)
768 this->Header.sh_addralign = Align;
769
770 uintX_t Off = this->Header.sh_size;
771 Off = RoundUpToAlignment(Off, Align);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000772 C->OutSecOff = Off;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000773 Off += C->getSize();
774 this->Header.sh_size = Off;
775}
776
777template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000778typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000779 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000780 case SymbolBody::DefinedSyntheticKind: {
781 auto &D = cast<DefinedSynthetic<ELFT>>(S);
782 return D.Section.getVA() + D.Sym.st_value;
783 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000784 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000785 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000786 case SymbolBody::DefinedRegularKind: {
787 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindola48225b42015-10-23 19:55:11 +0000788 InputSectionBase<ELFT> &SC = DR.Section;
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000789 if (DR.Sym.getType() == STT_TLS)
790 return SC.OutSec->getVA() + SC.getOffset(DR.Sym) -
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000791 Out<ELFT>::TlsPhdr->p_vaddr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000792 return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
Rafael Espindolacd076f02015-09-25 18:19:03 +0000793 }
794 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000795 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
George Rimarbc590fe2015-10-28 16:48:58 +0000796 case SymbolBody::SharedKind: {
797 auto &SS = cast<SharedSymbol<ELFT>>(S);
Rui Ueyamabb936062015-12-17 01:14:23 +0000798 if (SS.NeedsCopy)
George Rimarbc590fe2015-10-28 16:48:58 +0000799 return Out<ELFT>::Bss->getVA() + SS.OffsetInBSS;
800 return 0;
801 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000802 case SymbolBody::UndefinedKind:
803 return 0;
804 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000805 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
806 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000807 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000808 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000809}
810
Rui Ueyama34f29242015-10-13 19:51:57 +0000811// Returns a VA which a relocatin RI refers to. Used only for local symbols.
812// For non-local symbols, use getSymVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000813template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000814typename ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000815lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
Rafael Espindola932efcf2015-10-19 20:24:44 +0000816 const Elf_Rel_Impl<ELFT, IsRela> &RI) {
817 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
818 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
819
820 uintX_t Addend = getAddend<ELFT>(RI);
821
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000822 // PPC64 has a special relocation representing the TOC base pointer
823 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000824 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000825 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000826
Rui Ueyama126d08f2015-10-12 20:28:22 +0000827 const Elf_Sym *Sym =
828 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000829
Rui Ueyama126d08f2015-10-12 20:28:22 +0000830 if (!Sym)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000831 error("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000832
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000833 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
834
835 if (Sym->getType() == STT_TLS)
836 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
George Rimarcc06a6f2015-11-29 14:14:20 +0000837 Out<ELFT>::TlsPhdr->p_vaddr;
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000838
Rafael Espindola444576d2015-10-09 19:25:07 +0000839 // According to the ELF spec reference to a local symbol from outside
840 // the group are not allowed. Unfortunately .eh_frame breaks that rule
841 // and must be treated specially. For now we just replace the symbol with
842 // 0.
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000843 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000844 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000845
Rafael Espindolac159c962015-10-19 21:00:02 +0000846 uintX_t VA = Section->OutSec->getVA();
847 if (isa<InputSection<ELFT>>(Section))
848 return VA + Section->getOffset(*Sym) + Addend;
849
850 uintX_t Offset = Sym->st_value;
851 if (Sym->getType() == STT_SECTION) {
852 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000853 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000854 }
855 return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
856 Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000857}
858
Rui Ueyama34f29242015-10-13 19:51:57 +0000859// Returns true if a symbol can be replaced at load-time by a symbol
860// with the same name defined in other ELF executable or DSO.
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000861bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000862 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000863 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000864 if (Body->isShared())
865 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000866
867 if (Body->isUndefined()) {
868 if (!Body->isWeak())
869 return true;
870
871 // This is an horrible corner case. Ideally we would like to say that any
872 // undefined symbol can be preempted so that the dynamic linker has a
873 // chance of finding it at runtime.
874 //
875 // The problem is that the code sequence used to test for weak undef
876 // functions looks like
877 // if (func) func()
878 // If the code is -fPIC the first reference is a load from the got and
879 // everything works.
880 // If the code is not -fPIC there is no reasonable way to solve it:
881 // * A relocation writing to the text segment will fail (it is ro).
882 // * A copy relocation doesn't work for functions.
883 // * The trick of using a plt entry as the address would fail here since
884 // the plt entry would have a non zero address.
885 // Since we cannot do anything better, we just resolve the symbol to 0 and
886 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000887 //
888 // As an extra hack, assume that if we are producing a shared library the
889 // user knows what he or she is doing and can handle a dynamic relocation.
890 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000891 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000892 if (!Config->Shared)
893 return false;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000894 return Body->getVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000895}
896
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000897template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000898 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000899 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000900}
901
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000902template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000903EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t sh_type,
904 uintX_t sh_flags)
905 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
906
907template <class ELFT>
908EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
909 : S(S), Index(Index) {}
910
911template <class ELFT> StringRef EHRegion<ELFT>::data() const {
912 ArrayRef<uint8_t> SecData = S->getSectionData();
913 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
914 size_t Start = Offsets[Index].first;
915 size_t End =
916 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
917 return StringRef((const char *)SecData.data() + Start, End - Start);
918}
919
920template <class ELFT>
921Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
922 : EHRegion<ELFT>(S, Index) {}
923
924template <class ELFT>
925template <bool IsRela>
926void EHOutputSection<ELFT>::addSectionAux(
927 EHInputSection<ELFT> *S,
928 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
929 const endianness E = ELFT::TargetEndianness;
930
931 S->OutSec = this;
932 uint32_t Align = S->getAlign();
933 if (Align > this->Header.sh_addralign)
934 this->Header.sh_addralign = Align;
935
936 Sections.push_back(S);
937
938 ArrayRef<uint8_t> SecData = S->getSectionData();
939 ArrayRef<uint8_t> D = SecData;
940 uintX_t Offset = 0;
941 auto RelI = Rels.begin();
942 auto RelE = Rels.end();
943
944 DenseMap<unsigned, unsigned> OffsetToIndex;
945 while (!D.empty()) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000946 unsigned Index = S->Offsets.size();
947 S->Offsets.push_back(std::make_pair(Offset, -1));
948
George Rimar003be4f2015-12-17 09:23:40 +0000949 uintX_t Length = readEntryLength(D);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000950 StringRef Entry((const char *)D.data(), Length);
951
952 while (RelI != RelE && RelI->r_offset < Offset)
953 ++RelI;
954 uintX_t NextOffset = Offset + Length;
955 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
956
957 uint32_t ID = read32<E>(D.data() + 4);
958 if (ID == 0) {
959 // CIE
960 Cie<ELFT> C(S, Index);
961
962 StringRef Personality;
963 if (HasReloc) {
964 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
965 SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl();
966 Personality = Body.getName();
967 }
968
969 std::pair<StringRef, StringRef> CieInfo(Entry, Personality);
970 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
971 if (P.second) {
972 Cies.push_back(C);
973 this->Header.sh_size += Length;
974 }
975 OffsetToIndex[Offset] = P.first->second;
976 } else {
977 if (!HasReloc)
978 error("FDE doesn't reference another section");
979 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
980 if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) {
981 uint32_t CieOffset = Offset + 4 - ID;
982 auto I = OffsetToIndex.find(CieOffset);
983 if (I == OffsetToIndex.end())
984 error("Invalid CIE reference");
985 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
986 this->Header.sh_size += Length;
987 }
988 }
989
990 Offset = NextOffset;
991 D = D.slice(Length);
992 }
993}
994
995template <class ELFT>
George Rimar003be4f2015-12-17 09:23:40 +0000996typename EHOutputSection<ELFT>::uintX_t
997EHOutputSection<ELFT>::readEntryLength(ArrayRef<uint8_t> D) {
998 const endianness E = ELFT::TargetEndianness;
999
1000 if (D.size() < 4)
1001 error("Truncated CIE/FDE length");
1002 uint64_t Len = read32<E>(D.data());
1003 if (Len < UINT32_MAX) {
1004 if (Len > (UINT32_MAX - 4))
1005 error("CIE/FIE size is too large");
1006 if (Len + 4 > D.size())
1007 error("CIE/FIE ends past the end of the section");
1008 return Len + 4;
1009 }
1010
1011 if (D.size() < 12)
1012 error("Truncated CIE/FDE length");
1013 Len = read64<E>(D.data() + 4);
1014 if (Len > (UINT64_MAX - 12))
1015 error("CIE/FIE size is too large");
1016 if (Len + 12 > D.size())
1017 error("CIE/FIE ends past the end of the section");
1018 return Len + 12;
1019}
1020
1021template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001022void EHOutputSection<ELFT>::addSection(EHInputSection<ELFT> *S) {
1023 const Elf_Shdr *RelSec = S->RelocSection;
1024 if (!RelSec)
1025 return addSectionAux(
1026 S, make_range((const Elf_Rela *)nullptr, (const Elf_Rela *)nullptr));
1027 ELFFile<ELFT> &Obj = S->getFile()->getObj();
1028 if (RelSec->sh_type == SHT_RELA)
1029 return addSectionAux(S, Obj.relas(RelSec));
1030 return addSectionAux(S, Obj.rels(RelSec));
1031}
1032
1033template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1034 const endianness E = ELFT::TargetEndianness;
1035 size_t Offset = 0;
1036 for (const Cie<ELFT> &C : Cies) {
1037 size_t CieOffset = Offset;
1038
1039 StringRef CieData = C.data();
1040 memcpy(Buf + Offset, CieData.data(), CieData.size());
1041 C.S->Offsets[C.Index].second = Offset;
1042 Offset += CieData.size();
1043
1044 for (const EHRegion<ELFT> &F : C.Fdes) {
1045 StringRef FdeData = F.data();
George Rimar5be170e2015-12-15 14:20:57 +00001046 memcpy(Buf + Offset, FdeData.data(), 4); // Length
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001047 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
1048 memcpy(Buf + Offset + 8, FdeData.data() + 8, FdeData.size() - 8);
1049 F.S->Offsets[F.Index].second = Offset;
1050 Offset += FdeData.size();
1051 }
1052 }
1053
1054 for (EHInputSection<ELFT> *S : Sections) {
1055 const Elf_Shdr *RelSec = S->RelocSection;
1056 if (!RelSec)
1057 continue;
1058 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1059 if (RelSec->sh_type == SHT_RELA)
1060 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1061 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001062 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001063 }
1064}
1065
1066template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +00001067MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
1068 uintX_t sh_flags)
1069 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
1070
1071template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001072 if (shouldTailMerge()) {
1073 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001074 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001075 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001076 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001077 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1078 StringRef Data = P.first;
1079 memcpy(Buf + P.second, Data.data(), Data.size());
1080 }
1081}
1082
1083static size_t findNull(StringRef S, size_t EntSize) {
1084 // Optimize the common case.
1085 if (EntSize == 1)
1086 return S.find(0);
1087
1088 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1089 const char *B = S.begin() + I;
1090 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1091 return I;
1092 }
1093 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001094}
1095
1096template <class ELFT>
1097void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
1098 S->OutSec = this;
1099 uint32_t Align = S->getAlign();
1100 if (Align > this->Header.sh_addralign)
1101 this->Header.sh_addralign = Align;
1102
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001103 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001104 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001105 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001106 uintX_t Offset = 0;
1107
1108 if (this->Header.sh_flags & SHF_STRINGS) {
1109 while (!Data.empty()) {
1110 size_t End = findNull(Data, EntSize);
1111 if (End == StringRef::npos)
1112 error("String is not null terminated");
1113 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001114 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001115 if (shouldTailMerge())
1116 OutputOffset = -1;
1117 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1118 uintX_t Size = End + EntSize;
1119 Data = Data.substr(Size);
1120 Offset += Size;
1121 }
1122 } else {
1123 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1124 StringRef Entry = Data.substr(I, EntSize);
1125 size_t OutputOffset = Builder.add(Entry);
1126 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1127 Offset += EntSize;
1128 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001129 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001130}
1131
1132template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001133unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1134 return Builder.getOffset(Val);
1135}
1136
1137template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1138 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1139}
1140
1141template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1142 if (shouldTailMerge())
1143 Builder.finalize();
1144 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001145}
1146
1147template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001148StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
1149 : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001150 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001151 Dynamic(Dynamic) {
1152 this->Header.sh_addralign = 1;
1153}
1154
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001155template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001156 StringRef Data = StrTabBuilder.data();
1157 memcpy(Buf, Data.data(), Data.size());
1158}
1159
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001160template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001161 if (!B.isUsedInRegularObj())
1162 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001163
1164 // Don't include synthetic symbols like __init_array_start in every output.
1165 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
1166 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
1167 return false;
1168
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001169 return true;
1170}
1171
Rafael Espindola05a3dd22015-09-22 23:38:23 +00001172bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001173 uint8_t V = B.getVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001174 if (V != STV_DEFAULT && V != STV_PROTECTED)
1175 return false;
1176
Rafael Espindola05a3dd22015-09-22 23:38:23 +00001177 if (Config->ExportDynamic || Config->Shared)
1178 return true;
1179 return B.isUsedInDynamicReloc();
1180}
1181
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001182template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +00001183bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
1184 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001185 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
1186 if (Sym.getType() == STT_SECTION)
1187 return false;
1188
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001189 InputSectionBase<ELFT> *Sec = File.getSection(Sym);
Rafael Espindola444576d2015-10-09 19:25:07 +00001190 // If sym references a section in a discarded group, don't keep it.
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001191 if (Sec == &InputSection<ELFT>::Discarded)
Rafael Espindola4cda5812015-10-16 15:29:48 +00001192 return false;
Rafael Espindola444576d2015-10-09 19:25:07 +00001193
Davide Italiano6993ba42015-09-26 00:47:56 +00001194 if (Config->DiscardNone)
1195 return true;
1196
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001197 // In ELF assembly .L symbols are normally discarded by the assembler.
1198 // If the assembler fails to do so, the linker discards them if
1199 // * --discard-locals is used.
1200 // * The symbol is in a SHF_MERGE section, which is normally the reason for
1201 // the assembler keeping the .L symbol.
Rafael Espindola29925632015-12-11 19:09:21 +00001202 if (!SymName.startswith(".L") && !SymName.empty())
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001203 return true;
1204
1205 if (Config->DiscardLocals)
1206 return false;
1207
1208 return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE);
Davide Italiano6993ba42015-09-26 00:47:56 +00001209}
1210
Rafael Espindola35c6af32015-09-25 17:19:10 +00001211template <class ELFT>
1212SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001213 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
1214 : OutputSectionBase<ELFT>(
Rafael Espindola35c6af32015-09-25 17:19:10 +00001215 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1216 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
1217 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001218 Table(Table), StrTabSec(StrTabSec) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001219 typedef OutputSectionBase<ELFT> Base;
1220 typename Base::Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +00001221
1222 Header.sh_entsize = sizeof(Elf_Sym);
1223 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
1224}
1225
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001226// Orders symbols according to their positions in the GOT,
1227// in compliance with MIPS ABI rules.
1228// See "Global Offset Table" in Chapter 5 in the following document
1229// for detailed description:
1230// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1231static bool sortMipsSymbols(SymbolBody *L, SymbolBody *R) {
1232 if (!L->isInGot() || !R->isInGot())
1233 return R->isInGot();
1234 return L->GotIndex < R->GotIndex;
1235}
1236
Rui Ueyama0db335f2015-10-07 16:58:54 +00001237template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001238 if (this->Header.sh_size)
1239 return; // Already finalized.
1240
Rui Ueyama0db335f2015-10-07 16:58:54 +00001241 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001242 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001243 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001244
1245 if (!StrTabSec.isDynamic()) {
1246 std::stable_sort(Symbols.begin(), Symbols.end(),
Igor Kudrinf1d60292015-10-28 07:05:56 +00001247 [](SymbolBody *L, SymbolBody *R) {
1248 return getSymbolBinding(L) == STB_LOCAL &&
1249 getSymbolBinding(R) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001250 });
1251 return;
1252 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001253 if (Out<ELFT>::GnuHashTab)
1254 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1255 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001256 else if (Config->EMachine == EM_MIPS)
1257 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001258 size_t I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +00001259 for (SymbolBody *B : Symbols)
Rui Ueyamaa02bba62015-12-17 00:12:04 +00001260 B->DynamicSymbolTableIndex = ++I;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001261}
1262
1263template <class ELFT>
Igor Kudrinab665fc2015-10-20 21:47:58 +00001264void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
Rui Ueyama0db335f2015-10-07 16:58:54 +00001265 StrTabSec.add(Name);
1266 ++NumVisible;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001267 ++NumLocals;
1268}
1269
1270template <class ELFT>
1271void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
1272 StrTabSec.add(Body->getName());
Igor Kudrinf1d60292015-10-28 07:05:56 +00001273 Symbols.push_back(Body);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001274 ++NumVisible;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001275}
1276
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001277template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001278 Buf += sizeof(Elf_Sym);
1279
1280 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1281 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001282 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1283 writeLocalSymbols(Buf);
1284
1285 writeGlobalSymbols(Buf);
1286}
1287
1288template <class ELFT>
1289void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1290 // Iterate over all input object files to copy their local symbols
1291 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001292 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
1293 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001294 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001295 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +00001296 error(SymNameOrErr);
1297 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001298 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001299 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +00001300
Rui Ueyamac55733e2015-09-30 00:54:29 +00001301 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001302 uintX_t VA = 0;
Rafael Espindola4cda5812015-10-16 15:29:48 +00001303 if (Sym.st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001304 ESym->st_shndx = SHN_ABS;
Rafael Espindolac159c962015-10-19 21:00:02 +00001305 VA = Sym.st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001306 } else {
Rafael Espindola48225b42015-10-23 19:55:11 +00001307 InputSectionBase<ELFT> *Section = File->getSection(Sym);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001308 if (!Section->isLive())
1309 continue;
Rafael Espindolac159c962015-10-19 21:00:02 +00001310 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1311 ESym->st_shndx = OutSec->SectionIndex;
1312 VA += OutSec->getVA() + Section->getOffset(Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001313 }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001314 ESym->st_name = StrTabSec.getOffset(SymName);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001315 ESym->st_size = Sym.st_size;
1316 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001317 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001318 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001319 }
1320 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001321}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001322
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001323template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001324void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001325 // Write the internal symbol table contents to the output symbol table
1326 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001327 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +00001328 for (SymbolBody *Body : Symbols) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001329 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001330
Rafael Espindola8614c562015-10-06 14:33:58 +00001331 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001332 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001333 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001334 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001335 case SymbolBody::DefinedRegularKind: {
1336 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
1337 if (!Sym->Section.isLive())
1338 continue;
1339 OutSec = Sym->Section.OutSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001340 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001341 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001342 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001343 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001344 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001345 case SymbolBody::SharedKind: {
Rui Ueyamabb936062015-12-17 01:14:23 +00001346 if (cast<SharedSymbol<ELFT>>(Body)->NeedsCopy)
George Rimarbc590fe2015-10-28 16:48:58 +00001347 OutSec = Out<ELFT>::Bss;
1348 break;
1349 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001350 case SymbolBody::UndefinedKind:
1351 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001352 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001353 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001354 }
1355
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001356 StringRef Name = Body->getName();
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001357 ESym->st_name = StrTabSec.getOffset(Name);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001358
Rafael Espindola8614c562015-10-06 14:33:58 +00001359 unsigned char Type = STT_NOTYPE;
1360 uintX_t Size = 0;
1361 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
1362 const Elf_Sym &InputSym = EBody->Sym;
Rafael Espindola8614c562015-10-06 14:33:58 +00001363 Type = InputSym.getType();
1364 Size = InputSym.st_size;
1365 }
1366
Igor Kudrin853b88d2015-10-20 20:52:14 +00001367 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001368 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001369 ESym->setVisibility(Body->getVisibility());
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001370 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001371
Rafael Espindola8614c562015-10-06 14:33:58 +00001372 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +00001373 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +00001374 else if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001375 ESym->st_shndx = OutSec->SectionIndex;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001376
1377 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001378 }
1379}
1380
Igor Kudrin853b88d2015-10-20 20:52:14 +00001381template <class ELFT>
1382uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001383 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001384 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1385 return STB_LOCAL;
1386 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body))
1387 return EBody->Sym.getBinding();
1388 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1389}
1390
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001391namespace lld {
1392namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001393template class OutputSectionBase<ELF32LE>;
1394template class OutputSectionBase<ELF32BE>;
1395template class OutputSectionBase<ELF64LE>;
1396template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001397
George Rimar648a2c32015-10-20 08:54:27 +00001398template class GotPltSection<ELF32LE>;
1399template class GotPltSection<ELF32BE>;
1400template class GotPltSection<ELF64LE>;
1401template class GotPltSection<ELF64BE>;
1402
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001403template class GotSection<ELF32LE>;
1404template class GotSection<ELF32BE>;
1405template class GotSection<ELF64LE>;
1406template class GotSection<ELF64BE>;
1407
1408template class PltSection<ELF32LE>;
1409template class PltSection<ELF32BE>;
1410template class PltSection<ELF64LE>;
1411template class PltSection<ELF64BE>;
1412
1413template class RelocationSection<ELF32LE>;
1414template class RelocationSection<ELF32BE>;
1415template class RelocationSection<ELF64LE>;
1416template class RelocationSection<ELF64BE>;
1417
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001418template class InterpSection<ELF32LE>;
1419template class InterpSection<ELF32BE>;
1420template class InterpSection<ELF64LE>;
1421template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001422
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001423template class GnuHashTableSection<ELF32LE>;
1424template class GnuHashTableSection<ELF32BE>;
1425template class GnuHashTableSection<ELF64LE>;
1426template class GnuHashTableSection<ELF64BE>;
1427
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001428template class HashTableSection<ELF32LE>;
1429template class HashTableSection<ELF32BE>;
1430template class HashTableSection<ELF64LE>;
1431template class HashTableSection<ELF64BE>;
1432
1433template class DynamicSection<ELF32LE>;
1434template class DynamicSection<ELF32BE>;
1435template class DynamicSection<ELF64LE>;
1436template class DynamicSection<ELF64BE>;
1437
1438template class OutputSection<ELF32LE>;
1439template class OutputSection<ELF32BE>;
1440template class OutputSection<ELF64LE>;
1441template class OutputSection<ELF64BE>;
1442
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001443template class EHOutputSection<ELF32LE>;
1444template class EHOutputSection<ELF32BE>;
1445template class EHOutputSection<ELF64LE>;
1446template class EHOutputSection<ELF64BE>;
1447
Rafael Espindolac159c962015-10-19 21:00:02 +00001448template class MergeOutputSection<ELF32LE>;
1449template class MergeOutputSection<ELF32BE>;
1450template class MergeOutputSection<ELF64LE>;
1451template class MergeOutputSection<ELF64BE>;
1452
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001453template class StringTableSection<ELF32LE>;
1454template class StringTableSection<ELF32BE>;
1455template class StringTableSection<ELF64LE>;
1456template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001457
1458template class SymbolTableSection<ELF32LE>;
1459template class SymbolTableSection<ELF32BE>;
1460template class SymbolTableSection<ELF64LE>;
1461template class SymbolTableSection<ELF64BE>;
1462
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001463template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1464template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1465template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1466template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +00001467
Rafael Espindola56f965f2015-09-21 22:48:12 +00001468template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001469getLocalRelTarget(const ObjectFile<ELF32LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001470 const ELFFile<ELF32LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001471template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001472getLocalRelTarget(const ObjectFile<ELF32BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001473 const ELFFile<ELF32BE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001474template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001475getLocalRelTarget(const ObjectFile<ELF64LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001476 const ELFFile<ELF64LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001477template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001478getLocalRelTarget(const ObjectFile<ELF64BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001479 const ELFFile<ELF64BE>::Elf_Rel &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001480
Rafael Espindolac159c962015-10-19 21:00:02 +00001481template ELFFile<ELF32LE>::uintX_t
1482getLocalRelTarget(const ObjectFile<ELF32LE> &,
1483 const ELFFile<ELF32LE>::Elf_Rela &);
1484template ELFFile<ELF32BE>::uintX_t
1485getLocalRelTarget(const ObjectFile<ELF32BE> &,
1486 const ELFFile<ELF32BE>::Elf_Rela &);
1487template ELFFile<ELF64LE>::uintX_t
1488getLocalRelTarget(const ObjectFile<ELF64LE> &,
1489 const ELFFile<ELF64LE>::Elf_Rela &);
1490template ELFFile<ELF64BE>::uintX_t
1491getLocalRelTarget(const ObjectFile<ELF64BE> &,
1492 const ELFFile<ELF64BE>::Elf_Rela &);
1493
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001494template bool includeInSymtab<ELF32LE>(const SymbolBody &);
1495template bool includeInSymtab<ELF32BE>(const SymbolBody &);
1496template bool includeInSymtab<ELF64LE>(const SymbolBody &);
1497template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001498
Rafael Espindola444576d2015-10-09 19:25:07 +00001499template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1500 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001501 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001502template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1503 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001504 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001505template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1506 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001507 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001508template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1509 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001510 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001511}
1512}