blob: 0006ccf3c08a58737ecb09e3c82e7228e507d9f5 [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
83template <class ELFT> void GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) {
84 Sym->GotIndex = Target->getGotHeaderEntriesNum() + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +000085 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +000086 Entries.push_back(Sym);
87 Entries.push_back(nullptr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000088}
89
Michael J. Spencer1e225612015-11-11 01:00:24 +000090template <class ELFT> uint32_t GotSection<ELFT>::addLocalModuleTlsIndex() {
91 Entries.push_back(nullptr);
92 Entries.push_back(nullptr);
93 return (Entries.size() - 2) * sizeof(uintX_t);
94}
95
Rafael Espindola5805c4f2015-09-21 21:38:08 +000096template <class ELFT>
97typename GotSection<ELFT>::uintX_t
98GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama5f551ae2015-10-14 14:02:06 +000099 return this->getVA() + B.GotIndex * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000100}
101
Igor Kudrin304860a2015-11-12 04:39:49 +0000102template <class ELFT>
103const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
104 return Entries.empty() ? nullptr : Entries.front();
105}
106
107template <class ELFT>
108unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
109 // TODO: Update when the suppoort of GOT entries for local symbols is added.
110 return Target->getGotHeaderEntriesNum();
111}
112
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000113template <class ELFT> void GotSection<ELFT>::finalize() {
114 this->Header.sh_size =
115 (Target->getGotHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
116}
117
Rafael Espindolaa6627382015-10-06 23:56:53 +0000118template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000119 Target->writeGotHeaderEntries(Buf);
120 Buf += Target->getGotHeaderEntriesNum() * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000121 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000122 uint8_t *Entry = Buf;
123 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000124 if (!B)
125 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000126 // MIPS has special rules to fill up GOT entries.
127 // See "Global Offset Table" in Chapter 5 in the following document
128 // for detailed description:
129 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
130 // As the first approach, we can just store addresses for all symbols.
131 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000132 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000133 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +0000134 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000135 }
136}
137
Rafael Espindola35c6af32015-09-25 17:19:10 +0000138template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000139PltSection<ELFT>::PltSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000140 : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS,
141 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000142 this->Header.sh_addralign = 16;
143}
144
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000145template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000146 size_t Off = 0;
George Rimar648a2c32015-10-20 08:54:27 +0000147 bool LazyReloc = Target->supportsLazyRelocations();
148 if (LazyReloc) {
149 // First write PLT[0] entry which is special.
150 Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA());
151 Off += Target->getPltZeroEntrySize();
152 }
George Rimar77b77792015-11-25 22:15:01 +0000153 for (std::pair<const SymbolBody *, unsigned> &I : Entries) {
154 const SymbolBody *E = I.first;
155 unsigned RelOff = I.second;
156 uint64_t GotVA =
157 LazyReloc ? Out<ELFT>::GotPlt->getVA() : Out<ELFT>::Got->getVA();
158 uint64_t GotE = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
159 : Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000160 uint64_t Plt = this->getVA() + Off;
George Rimar77b77792015-11-25 22:15:01 +0000161 Target->writePltEntry(Buf + Off, GotVA, GotE, Plt, E->PltIndex, RelOff);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000162 Off += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163 }
164}
165
166template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000167 Sym->PltIndex = Entries.size();
George Rimar77b77792015-11-25 22:15:01 +0000168 unsigned RelOff = Target->supportsLazyRelocations()
169 ? Out<ELFT>::RelaPlt->getRelocOffset()
170 : Out<ELFT>::RelaDyn->getRelocOffset();
171 Entries.push_back(std::make_pair(Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172}
173
174template <class ELFT>
175typename PltSection<ELFT>::uintX_t
176PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
George Rimar648a2c32015-10-20 08:54:27 +0000177 return this->getVA() + Target->getPltZeroEntrySize() +
178 B.PltIndex * Target->getPltEntrySize();
179}
180
181template <class ELFT> void PltSection<ELFT>::finalize() {
182 this->Header.sh_size = Target->getPltZeroEntrySize() +
183 Entries.size() * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000184}
185
186template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000187RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
188 : OutputSectionBase<ELFT>(Name,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000189 IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
190 llvm::ELF::SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000191 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000192 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
193 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
194}
195
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000196template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000197 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000198 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000199 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
200 Buf += EntrySize;
201
Michael J. Spencer627ae702015-11-13 00:28:34 +0000202 // Skip placeholder for global dynamic TLS relocation pair. It was already
203 // handled by the previous relocation.
204 if (!Rel.C || !Rel.RI)
205 continue;
206
207 InputSectionBase<ELFT> &C = *Rel.C;
208 const Elf_Rel &RI = *Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000209 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000210 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000211 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000212 if (Body)
213 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000214
Rui Ueyama64558522015-10-16 22:51:43 +0000215 uint32_t Type = RI.getType(Config->Mips64EL);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000216
Michael J. Spencerecd7f372015-11-13 00:32:58 +0000217 if (Target->isTlsLocalDynamicReloc(Type)) {
Michael J. Spencer1e225612015-11-11 01:00:24 +0000218 P->setSymbolAndType(0, Target->getTlsModuleIndexReloc(),
219 Config->Mips64EL);
220 P->r_offset =
221 Out<ELFT>::Got->getVA() + Out<ELFT>::LocalModuleTlsIndexOffset;
222 continue;
223 }
224
Michael J. Spencerecd7f372015-11-13 00:32:58 +0000225 if (Body && Target->isTlsGlobalDynamicReloc(Type)) {
Michael J. Spencer627ae702015-11-13 00:28:34 +0000226 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
227 Target->getTlsModuleIndexReloc(), Config->Mips64EL);
228 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
229 auto *PNext = reinterpret_cast<Elf_Rel *>(Buf);
230 PNext->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
231 Target->getTlsOffsetReloc(), Config->Mips64EL);
232 PNext->r_offset = Out<ELFT>::Got->getEntryAddr(*Body) + sizeof(uintX_t);
233 continue;
234 }
235
George Rimarbc590fe2015-10-28 16:48:58 +0000236 bool NeedsCopy = Body && Target->relocNeedsCopy(Type, *Body);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000237 bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
238 bool CanBePreempted = canBePreempted(Body, NeedsGot);
George Rimar648a2c32015-10-20 08:54:27 +0000239 bool LazyReloc = Body && Target->supportsLazyRelocations() &&
240 Target->relocNeedsPlt(Type, *Body);
Rafael Espindola49757522015-10-19 19:58:18 +0000241
242 if (CanBePreempted) {
George Rimar687138c2015-11-13 16:28:53 +0000243 unsigned GotReloc =
244 Body->isTLS() ? Target->getTlsGotReloc() : Target->getGotReloc();
Rafael Espindola49757522015-10-19 19:58:18 +0000245 if (NeedsGot)
246 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
George Rimar687138c2015-11-13 16:28:53 +0000247 LazyReloc ? Target->getPltReloc() : GotReloc,
George Rimar648a2c32015-10-20 08:54:27 +0000248 Config->Mips64EL);
Rafael Espindola49757522015-10-19 19:58:18 +0000249 else
George Rimarbc590fe2015-10-28 16:48:58 +0000250 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
George Rimard23970f2015-11-25 20:41:53 +0000251 NeedsCopy ? Target->getCopyReloc()
252 : Target->getDynReloc(Type),
Rafael Espindola49757522015-10-19 19:58:18 +0000253 Config->Mips64EL);
254 } else {
Rui Ueyama64558522015-10-16 22:51:43 +0000255 P->setSymbolAndType(0, Target->getRelativeReloc(), Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000256 }
257
George Rimar648a2c32015-10-20 08:54:27 +0000258 if (NeedsGot) {
259 if (LazyReloc)
260 P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
261 else
262 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimarbc590fe2015-10-28 16:48:58 +0000263 } else if (NeedsCopy) {
264 P->r_offset = Out<ELFT>::Bss->getVA() +
265 dyn_cast<SharedSymbol<ELFT>>(Body)->OffsetInBSS;
George Rimar648a2c32015-10-20 08:54:27 +0000266 } else {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000267 P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA();
George Rimar648a2c32015-10-20 08:54:27 +0000268 }
Rafael Espindola49757522015-10-19 19:58:18 +0000269
Rafael Espindola932efcf2015-10-19 20:24:44 +0000270 uintX_t OrigAddend = 0;
Rafael Espindola49757522015-10-19 19:58:18 +0000271 if (IsRela && !NeedsGot)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000272 OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola49757522015-10-19 19:58:18 +0000273
Rafael Espindola932efcf2015-10-19 20:24:44 +0000274 uintX_t Addend;
George Rimarbc590fe2015-10-28 16:48:58 +0000275 if (NeedsCopy)
276 Addend = 0;
277 else if (CanBePreempted)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000278 Addend = OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000279 else if (Body)
280 Addend = getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body)) + OrigAddend;
281 else if (IsRela)
282 Addend = getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI));
283 else
284 Addend = getLocalRelTarget(File, RI);
Rafael Espindola52dca342015-10-07 00:58:20 +0000285
286 if (IsRela)
287 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000288 }
289}
290
George Rimar77b77792015-11-25 22:15:01 +0000291template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
292 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
293 return EntrySize * Relocs.size();
294}
295
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000296template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000297 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000298 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
299}
300
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000301template <class ELFT>
302InterpSection<ELFT>::InterpSection()
303 : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
304 llvm::ELF::SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000305 this->Header.sh_size = Config->DynamicLinker.size() + 1;
306 this->Header.sh_addralign = 1;
307}
308
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000309template <class ELFT>
310void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
Rui Ueyama157c4332015-10-24 17:57:39 +0000311 Header.sh_name = Out<ELFT>::ShStrTab->getOffset(Name);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000312 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000313}
314
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000315template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000316 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
317}
318
Rafael Espindola35c6af32015-09-25 17:19:10 +0000319template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000320HashTableSection<ELFT>::HashTableSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000321 : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
322 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000323 this->Header.sh_entsize = sizeof(Elf_Word);
324 this->Header.sh_addralign = sizeof(Elf_Word);
325}
326
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000327static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000328 uint32_t H = 0;
329 for (char C : Name) {
330 H = (H << 4) + C;
331 uint32_t G = H & 0xf0000000;
332 if (G)
333 H ^= G >> 24;
334 H &= ~G;
335 }
336 return H;
337}
338
Rui Ueyama0db335f2015-10-07 16:58:54 +0000339template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000340 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000341
Rui Ueyama0db335f2015-10-07 16:58:54 +0000342 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000343 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000344
345 // Create as many buckets as there are symbols.
346 // FIXME: This is simplistic. We can try to optimize it, but implementing
347 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000348 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000349 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
350}
351
352template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000353 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000354 auto *P = reinterpret_cast<Elf_Word *>(Buf);
355 *P++ = NumSymbols; // nbucket
356 *P++ = NumSymbols; // nchain
357
358 Elf_Word *Buckets = P;
359 Elf_Word *Chains = P + NumSymbols;
360
Igor Kudrinf1d60292015-10-28 07:05:56 +0000361 for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) {
Igor Kudrinab665fc2015-10-20 21:47:58 +0000362 StringRef Name = Body->getName();
363 unsigned I = Body->getDynamicSymbolTableIndex();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000364 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000365 Chains[I] = Buckets[Hash];
366 Buckets[Hash] = I;
367 }
368}
369
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000370static uint32_t hashGnu(StringRef Name) {
371 uint32_t H = 5381;
372 for (uint8_t C : Name)
373 H = (H << 5) + H + C;
374 return H;
375}
376
377template <class ELFT>
378GnuHashTableSection<ELFT>::GnuHashTableSection()
379 : OutputSectionBase<ELFT>(".gnu.hash", llvm::ELF::SHT_GNU_HASH,
380 llvm::ELF::SHF_ALLOC) {
381 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
382 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
383}
384
385template <class ELFT>
386unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
387 if (!NumHashed)
388 return 0;
389
390 // These values are prime numbers which are not greater than 2^(N-1) + 1.
391 // In result, for any particular NumHashed we return a prime number
392 // which is not greater than NumHashed.
393 static const unsigned Primes[] = {
394 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
395 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
396
397 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
398 array_lengthof(Primes) - 1)];
399}
400
401// Bloom filter estimation: at least 8 bits for each hashed symbol.
402// GNU Hash table requirement: it should be a power of 2,
403// the minimum value is 1, even for an empty table.
404// Expected results for a 32-bit target:
405// calcMaskWords(0..4) = 1
406// calcMaskWords(5..8) = 2
407// calcMaskWords(9..16) = 4
408// For a 64-bit target:
409// calcMaskWords(0..8) = 1
410// calcMaskWords(9..16) = 2
411// calcMaskWords(17..32) = 4
412template <class ELFT>
413unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
414 if (!NumHashed)
415 return 1;
416 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
417}
418
419template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000420 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000421 NBuckets = calcNBuckets(NumHashed);
422 MaskWords = calcMaskWords(NumHashed);
423 // Second hash shift estimation: just predefined values.
424 Shift2 = ELFT::Is64Bits ? 6 : 5;
425
426 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
427 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
428 + sizeof(Elf_Off) * MaskWords // Bloom Filter
429 + sizeof(Elf_Word) * NBuckets // Hash Buckets
430 + sizeof(Elf_Word) * NumHashed; // Hash Values
431}
432
433template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
434 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000435 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000436 return;
437 writeBloomFilter(Buf);
438 writeHashTable(Buf);
439}
440
441template <class ELFT>
442void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
443 auto *P = reinterpret_cast<Elf_Word *>(Buf);
444 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000445 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000446 *P++ = MaskWords;
447 *P++ = Shift2;
448 Buf = reinterpret_cast<uint8_t *>(P);
449}
450
451template <class ELFT>
452void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
453 unsigned C = sizeof(Elf_Off) * 8;
454
455 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000456 for (const HashedSymbolData &Item : HashedSymbols) {
457 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
458 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
459 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000460 Masks[Pos] |= V;
461 }
462 Buf += sizeof(Elf_Off) * MaskWords;
463}
464
465template <class ELFT>
466void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
467 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
468 Elf_Word *Values = Buckets + NBuckets;
469
470 int PrevBucket = -1;
471 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000472 for (const HashedSymbolData &Item : HashedSymbols) {
473 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000474 assert(PrevBucket <= Bucket);
475 if (Bucket != PrevBucket) {
476 Buckets[Bucket] = Item.Body->getDynamicSymbolTableIndex();
477 PrevBucket = Bucket;
478 if (I > 0)
479 Values[I - 1] |= 1;
480 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000481 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000482 ++I;
483 }
484 if (I > 0)
485 Values[I - 1] |= 1;
486}
487
Rafael Espindola31f88882015-11-02 14:33:11 +0000488static bool includeInGnuHashTable(SymbolBody *B) {
489 // Assume that includeInDynamicSymtab() is already checked.
490 return !B->isUndefined();
491}
492
Rafael Espindola35c6af32015-09-25 17:19:10 +0000493template <class ELFT>
Igor Kudrinf1d60292015-10-28 07:05:56 +0000494void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) {
495 std::vector<SymbolBody *> NotHashed;
496 NotHashed.reserve(Symbols.size());
497 HashedSymbols.reserve(Symbols.size());
498 for (SymbolBody *B : Symbols) {
499 if (includeInGnuHashTable(B))
500 HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())});
501 else
502 NotHashed.push_back(B);
503 }
504 if (HashedSymbols.empty())
505 return;
506
507 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
508 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
509 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
510 return L.Hash % NBuckets < R.Hash % NBuckets;
511 });
512
513 Symbols = std::move(NotHashed);
514 for (const HashedSymbolData &Item : HashedSymbols)
515 Symbols.push_back(Item.Body);
516}
517
518template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000519DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000520 : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
521 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000522 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000523 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000524 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
525 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000526
527 // .dynamic section is not writable on MIPS.
528 // See "Special Section" in Chapter 4 in the following document:
529 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
530 if (Config->EMachine == EM_MIPS)
531 Header.sh_flags = llvm::ELF::SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000532}
533
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000534template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000535 if (this->Header.sh_size)
536 return; // Already finalized.
537
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000538 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000539 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000540
541 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000542 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000543 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000544 ++NumEntries; // DT_RELASZ / DT_RELSZ
545 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000546 }
George Rimar648a2c32015-10-20 08:54:27 +0000547 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
548 ++NumEntries; // DT_JMPREL
549 ++NumEntries; // DT_PLTRELSZ
Igor Kudrin304860a2015-11-12 04:39:49 +0000550 ++NumEntries; // DT_PLTGOT / DT_MIPS_PLTGOT
George Rimar648a2c32015-10-20 08:54:27 +0000551 ++NumEntries; // DT_PLTREL
552 }
553
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000554 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000555 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000556 ++NumEntries; // DT_STRTAB
557 ++NumEntries; // DT_STRSZ
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000558 if (Out<ELFT>::GnuHashTab)
559 ++NumEntries; // DT_GNU_HASH
560 if (Out<ELFT>::HashTab)
561 ++NumEntries; // DT_HASH
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000562
Rui Ueyama7de3f372015-10-01 19:36:04 +0000563 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000564 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000565 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000566 }
567
568 if (!Config->SoName.empty()) {
569 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000570 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000571 }
572
Rafael Espindola77572242015-10-02 19:37:55 +0000573 if (PreInitArraySec)
574 NumEntries += 2;
575 if (InitArraySec)
576 NumEntries += 2;
577 if (FiniArraySec)
578 NumEntries += 2;
579
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000580 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000581 if (!F->isNeeded())
582 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000583 Out<ELFT>::DynStrTab->add(F->getSoName());
584 ++NumEntries;
585 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000586
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000587 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
588 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
589 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
590 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000591 if (InitSym)
592 ++NumEntries; // DT_INIT
593 if (FiniSym)
594 ++NumEntries; // DT_FINI
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000595
596 if (Config->Bsymbolic)
597 DtFlags |= DF_SYMBOLIC;
598 if (Config->ZNodelete)
599 DtFlags1 |= DF_1_NODELETE;
600 if (Config->ZNow) {
601 DtFlags |= DF_BIND_NOW;
602 DtFlags1 |= DF_1_NOW;
603 }
604 if (Config->ZOrigin) {
605 DtFlags |= DF_ORIGIN;
606 DtFlags1 |= DF_1_ORIGIN;
607 }
608
609 if (DtFlags)
Davide Italiano58cbaf02015-10-19 23:32:16 +0000610 ++NumEntries; // DT_FLAGS
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000611 if (DtFlags1)
George Rimar97aad172015-10-07 15:00:21 +0000612 ++NumEntries; // DT_FLAGS_1
Igor Kudrin304860a2015-11-12 04:39:49 +0000613
614 if (Config->EMachine == EM_MIPS) {
615 ++NumEntries; // DT_MIPS_RLD_VERSION
616 ++NumEntries; // DT_MIPS_FLAGS
617 ++NumEntries; // DT_MIPS_BASE_ADDRESS
618 ++NumEntries; // DT_MIPS_SYMTABNO
619 ++NumEntries; // DT_MIPS_LOCAL_GOTNO
620 ++NumEntries; // DT_MIPS_GOTSYM;
621 ++NumEntries; // DT_PLTGOT
622 if (Out<ELFT>::MipsRldMap)
623 ++NumEntries; // DT_MIPS_RLD_MAP
624 }
625
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000626 ++NumEntries; // DT_NULL
627
628 Header.sh_size = NumEntries * Header.sh_entsize;
629}
630
631template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000632 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
633
Rui Ueyama8c205d52015-10-02 01:33:31 +0000634 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
635 P->d_tag = Tag;
636 P->d_un.d_ptr = Val;
637 ++P;
638 };
639
640 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
641 P->d_tag = Tag;
642 P->d_un.d_val = Val;
643 ++P;
644 };
645
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000646 if (Out<ELFT>::RelaDyn->hasRelocs()) {
647 bool IsRela = Out<ELFT>::RelaDyn->isRela();
648 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
649 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000650 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
651 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000652 }
George Rimar648a2c32015-10-20 08:54:27 +0000653 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
654 WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
655 WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
Igor Kudrin304860a2015-11-12 04:39:49 +0000656 // On MIPS, the address of the .got.plt section is stored in
657 // the DT_MIPS_PLTGOT entry because the DT_PLTGOT entry points to
658 // the .got section. See "Dynamic Section" in the following document:
659 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
660 WritePtr((Config->EMachine == EM_MIPS) ? DT_MIPS_PLTGOT : DT_PLTGOT,
661 Out<ELFT>::GotPlt->getVA());
George Rimar648a2c32015-10-20 08:54:27 +0000662 WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
663 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000664
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000665 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000666 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000667 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
668 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000669 if (Out<ELFT>::GnuHashTab)
670 WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA());
671 if (Out<ELFT>::HashTab)
672 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000673
Rui Ueyamadfa577b2015-11-19 23:30:10 +0000674 // If --enable-new-dtags is set, lld emits DT_RUNPATH
675 // instead of DT_RPATH. The two tags are functionally
676 // equivalent except for the following:
677 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
678 // DT_RPATH is searched before.
679 // - DT_RUNPATH is used only to search for direct
680 // dependencies of the object it's contained in, while
681 // DT_RPATH is used for indirect dependencies as well.
Rui Ueyama8c205d52015-10-02 01:33:31 +0000682 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000683 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000684 Out<ELFT>::DynStrTab->getOffset(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000685
Rui Ueyama8c205d52015-10-02 01:33:31 +0000686 if (!Config->SoName.empty())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000687 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000688
Rafael Espindola77572242015-10-02 19:37:55 +0000689 auto WriteArray = [&](int32_t T1, int32_t T2,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000690 const OutputSectionBase<ELFT> *Sec) {
Rafael Espindola77572242015-10-02 19:37:55 +0000691 if (!Sec)
692 return;
693 WritePtr(T1, Sec->getVA());
694 WriteVal(T2, Sec->getSize());
695 };
696 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
697 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
698 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
699
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000700 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000701 if (F->isNeeded())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000702 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000703
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000704 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000705 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000706 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000707 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000708 if (DtFlags)
709 WriteVal(DT_FLAGS, DtFlags);
710 if (DtFlags1)
711 WriteVal(DT_FLAGS_1, DtFlags1);
Igor Kudrin304860a2015-11-12 04:39:49 +0000712
713 // See "Dynamic Section" in Chapter 5 in the following document
714 // for detailed description:
715 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
716 if (Config->EMachine == EM_MIPS) {
717 WriteVal(DT_MIPS_RLD_VERSION, 1);
718 WriteVal(DT_MIPS_FLAGS, RHF_NOTPOT);
719 WritePtr(DT_MIPS_BASE_ADDRESS, Target->getVAStart());
720 WriteVal(DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols());
721 WriteVal(DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum());
722 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
723 WriteVal(DT_MIPS_GOTSYM, B->getDynamicSymbolTableIndex());
724 else
725 WriteVal(DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols());
726 WritePtr(DT_PLTGOT, Out<ELFT>::Got->getVA());
727 if (Out<ELFT>::MipsRldMap)
728 WritePtr(DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap->getVA());
729 }
730
Rui Ueyama8c205d52015-10-02 01:33:31 +0000731 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000732}
733
734template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000735OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000736 uintX_t sh_flags)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000737 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000738
739template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000740void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
741 Sections.push_back(C);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000742 C->OutSec = this;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000743 uint32_t Align = C->getAlign();
744 if (Align > this->Header.sh_addralign)
745 this->Header.sh_addralign = Align;
746
747 uintX_t Off = this->Header.sh_size;
748 Off = RoundUpToAlignment(Off, Align);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000749 C->OutSecOff = Off;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000750 Off += C->getSize();
751 this->Header.sh_size = Off;
752}
753
754template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000755typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000756 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000757 case SymbolBody::DefinedSyntheticKind: {
758 auto &D = cast<DefinedSynthetic<ELFT>>(S);
759 return D.Section.getVA() + D.Sym.st_value;
760 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000761 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000762 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000763 case SymbolBody::DefinedRegularKind: {
764 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindola48225b42015-10-23 19:55:11 +0000765 InputSectionBase<ELFT> &SC = DR.Section;
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000766 if (DR.Sym.getType() == STT_TLS)
767 return SC.OutSec->getVA() + SC.getOffset(DR.Sym) -
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000768 Out<ELFT>::TlsPhdr->p_vaddr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000769 return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
Rafael Espindolacd076f02015-09-25 18:19:03 +0000770 }
771 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000772 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
George Rimarbc590fe2015-10-28 16:48:58 +0000773 case SymbolBody::SharedKind: {
774 auto &SS = cast<SharedSymbol<ELFT>>(S);
Rafael Espindola9b896082015-11-03 14:34:11 +0000775 if (SS.needsCopy())
George Rimarbc590fe2015-10-28 16:48:58 +0000776 return Out<ELFT>::Bss->getVA() + SS.OffsetInBSS;
777 return 0;
778 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000779 case SymbolBody::UndefinedKind:
780 return 0;
781 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000782 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
783 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000784 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000785 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000786}
787
Rui Ueyama34f29242015-10-13 19:51:57 +0000788// Returns a VA which a relocatin RI refers to. Used only for local symbols.
789// For non-local symbols, use getSymVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000790template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000791typename ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000792lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
Rafael Espindola932efcf2015-10-19 20:24:44 +0000793 const Elf_Rel_Impl<ELFT, IsRela> &RI) {
794 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
795 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
796
797 uintX_t Addend = getAddend<ELFT>(RI);
798
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000799 // PPC64 has a special relocation representing the TOC base pointer
800 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000801 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000802 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000803
Rui Ueyama126d08f2015-10-12 20:28:22 +0000804 const Elf_Sym *Sym =
805 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000806
Rui Ueyama126d08f2015-10-12 20:28:22 +0000807 if (!Sym)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000808 error("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000809
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000810 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
811
812 if (Sym->getType() == STT_TLS)
813 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
814 Out<ELF64LE>::TlsPhdr->p_vaddr;
815
Rafael Espindola444576d2015-10-09 19:25:07 +0000816 // According to the ELF spec reference to a local symbol from outside
817 // the group are not allowed. Unfortunately .eh_frame breaks that rule
818 // and must be treated specially. For now we just replace the symbol with
819 // 0.
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000820 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000821 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000822
Rafael Espindolac159c962015-10-19 21:00:02 +0000823 uintX_t VA = Section->OutSec->getVA();
824 if (isa<InputSection<ELFT>>(Section))
825 return VA + Section->getOffset(*Sym) + Addend;
826
827 uintX_t Offset = Sym->st_value;
828 if (Sym->getType() == STT_SECTION) {
829 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000830 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000831 }
832 return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
833 Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000834}
835
Rui Ueyama34f29242015-10-13 19:51:57 +0000836// Returns true if a symbol can be replaced at load-time by a symbol
837// with the same name defined in other ELF executable or DSO.
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000838bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000839 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000840 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000841 if (Body->isShared())
842 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000843
844 if (Body->isUndefined()) {
845 if (!Body->isWeak())
846 return true;
847
848 // This is an horrible corner case. Ideally we would like to say that any
849 // undefined symbol can be preempted so that the dynamic linker has a
850 // chance of finding it at runtime.
851 //
852 // The problem is that the code sequence used to test for weak undef
853 // functions looks like
854 // if (func) func()
855 // If the code is -fPIC the first reference is a load from the got and
856 // everything works.
857 // If the code is not -fPIC there is no reasonable way to solve it:
858 // * A relocation writing to the text segment will fail (it is ro).
859 // * A copy relocation doesn't work for functions.
860 // * The trick of using a plt entry as the address would fail here since
861 // the plt entry would have a non zero address.
862 // Since we cannot do anything better, we just resolve the symbol to 0 and
863 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000864 //
865 // As an extra hack, assume that if we are producing a shared library the
866 // user knows what he or she is doing and can handle a dynamic relocation.
867 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000868 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000869 if (!Config->Shared)
870 return false;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000871 return Body->getVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000872}
873
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000874template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000875 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000876 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000877}
878
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000879template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000880EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t sh_type,
881 uintX_t sh_flags)
882 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
883
884template <class ELFT>
885EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
886 : S(S), Index(Index) {}
887
888template <class ELFT> StringRef EHRegion<ELFT>::data() const {
889 ArrayRef<uint8_t> SecData = S->getSectionData();
890 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
891 size_t Start = Offsets[Index].first;
892 size_t End =
893 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
894 return StringRef((const char *)SecData.data() + Start, End - Start);
895}
896
897template <class ELFT>
898Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
899 : EHRegion<ELFT>(S, Index) {}
900
901template <class ELFT>
902template <bool IsRela>
903void EHOutputSection<ELFT>::addSectionAux(
904 EHInputSection<ELFT> *S,
905 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
906 const endianness E = ELFT::TargetEndianness;
907
908 S->OutSec = this;
909 uint32_t Align = S->getAlign();
910 if (Align > this->Header.sh_addralign)
911 this->Header.sh_addralign = Align;
912
913 Sections.push_back(S);
914
915 ArrayRef<uint8_t> SecData = S->getSectionData();
916 ArrayRef<uint8_t> D = SecData;
917 uintX_t Offset = 0;
918 auto RelI = Rels.begin();
919 auto RelE = Rels.end();
920
921 DenseMap<unsigned, unsigned> OffsetToIndex;
922 while (!D.empty()) {
923 if (D.size() < 4)
924 error("Truncated CIE/FDE length");
925 uint32_t Length = read32<E>(D.data());
926 Length += 4;
927
928 unsigned Index = S->Offsets.size();
929 S->Offsets.push_back(std::make_pair(Offset, -1));
930
931 if (Length > D.size())
932 error("CIE/FIE ends past the end of the section");
933 StringRef Entry((const char *)D.data(), Length);
934
935 while (RelI != RelE && RelI->r_offset < Offset)
936 ++RelI;
937 uintX_t NextOffset = Offset + Length;
938 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
939
940 uint32_t ID = read32<E>(D.data() + 4);
941 if (ID == 0) {
942 // CIE
943 Cie<ELFT> C(S, Index);
944
945 StringRef Personality;
946 if (HasReloc) {
947 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
948 SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl();
949 Personality = Body.getName();
950 }
951
952 std::pair<StringRef, StringRef> CieInfo(Entry, Personality);
953 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
954 if (P.second) {
955 Cies.push_back(C);
956 this->Header.sh_size += Length;
957 }
958 OffsetToIndex[Offset] = P.first->second;
959 } else {
960 if (!HasReloc)
961 error("FDE doesn't reference another section");
962 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
963 if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) {
964 uint32_t CieOffset = Offset + 4 - ID;
965 auto I = OffsetToIndex.find(CieOffset);
966 if (I == OffsetToIndex.end())
967 error("Invalid CIE reference");
968 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
969 this->Header.sh_size += Length;
970 }
971 }
972
973 Offset = NextOffset;
974 D = D.slice(Length);
975 }
976}
977
978template <class ELFT>
979void EHOutputSection<ELFT>::addSection(EHInputSection<ELFT> *S) {
980 const Elf_Shdr *RelSec = S->RelocSection;
981 if (!RelSec)
982 return addSectionAux(
983 S, make_range((const Elf_Rela *)nullptr, (const Elf_Rela *)nullptr));
984 ELFFile<ELFT> &Obj = S->getFile()->getObj();
985 if (RelSec->sh_type == SHT_RELA)
986 return addSectionAux(S, Obj.relas(RelSec));
987 return addSectionAux(S, Obj.rels(RelSec));
988}
989
990template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
991 const endianness E = ELFT::TargetEndianness;
992 size_t Offset = 0;
993 for (const Cie<ELFT> &C : Cies) {
994 size_t CieOffset = Offset;
995
996 StringRef CieData = C.data();
997 memcpy(Buf + Offset, CieData.data(), CieData.size());
998 C.S->Offsets[C.Index].second = Offset;
999 Offset += CieData.size();
1000
1001 for (const EHRegion<ELFT> &F : C.Fdes) {
1002 StringRef FdeData = F.data();
1003 memcpy(Buf + Offset, FdeData.data(), 4); // Legnth
1004 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
1005 memcpy(Buf + Offset + 8, FdeData.data() + 8, FdeData.size() - 8);
1006 F.S->Offsets[F.Index].second = Offset;
1007 Offset += FdeData.size();
1008 }
1009 }
1010
1011 for (EHInputSection<ELFT> *S : Sections) {
1012 const Elf_Shdr *RelSec = S->RelocSection;
1013 if (!RelSec)
1014 continue;
1015 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1016 if (RelSec->sh_type == SHT_RELA)
1017 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1018 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001019 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001020 }
1021}
1022
1023template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +00001024MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
1025 uintX_t sh_flags)
1026 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
1027
1028template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001029 if (shouldTailMerge()) {
1030 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001031 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001032 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001033 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001034 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1035 StringRef Data = P.first;
1036 memcpy(Buf + P.second, Data.data(), Data.size());
1037 }
1038}
1039
1040static size_t findNull(StringRef S, size_t EntSize) {
1041 // Optimize the common case.
1042 if (EntSize == 1)
1043 return S.find(0);
1044
1045 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1046 const char *B = S.begin() + I;
1047 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1048 return I;
1049 }
1050 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001051}
1052
1053template <class ELFT>
1054void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
1055 S->OutSec = this;
1056 uint32_t Align = S->getAlign();
1057 if (Align > this->Header.sh_addralign)
1058 this->Header.sh_addralign = Align;
1059
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001060 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001061 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001062 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001063 uintX_t Offset = 0;
1064
1065 if (this->Header.sh_flags & SHF_STRINGS) {
1066 while (!Data.empty()) {
1067 size_t End = findNull(Data, EntSize);
1068 if (End == StringRef::npos)
1069 error("String is not null terminated");
1070 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001071 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001072 if (shouldTailMerge())
1073 OutputOffset = -1;
1074 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1075 uintX_t Size = End + EntSize;
1076 Data = Data.substr(Size);
1077 Offset += Size;
1078 }
1079 } else {
1080 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1081 StringRef Entry = Data.substr(I, EntSize);
1082 size_t OutputOffset = Builder.add(Entry);
1083 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1084 Offset += EntSize;
1085 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001086 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001087}
1088
1089template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001090unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1091 return Builder.getOffset(Val);
1092}
1093
1094template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1095 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1096}
1097
1098template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1099 if (shouldTailMerge())
1100 Builder.finalize();
1101 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001102}
1103
1104template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001105StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
1106 : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001107 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001108 Dynamic(Dynamic) {
1109 this->Header.sh_addralign = 1;
1110}
1111
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001112template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001113 StringRef Data = StrTabBuilder.data();
1114 memcpy(Buf, Data.data(), Data.size());
1115}
1116
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001117template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001118 if (!B.isUsedInRegularObj())
1119 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001120
1121 // Don't include synthetic symbols like __init_array_start in every output.
1122 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
1123 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
1124 return false;
1125
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001126 return true;
1127}
1128
Rafael Espindola05a3dd22015-09-22 23:38:23 +00001129bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001130 uint8_t V = B.getVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001131 if (V != STV_DEFAULT && V != STV_PROTECTED)
1132 return false;
1133
Rafael Espindola05a3dd22015-09-22 23:38:23 +00001134 if (Config->ExportDynamic || Config->Shared)
1135 return true;
1136 return B.isUsedInDynamicReloc();
1137}
1138
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001139template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +00001140bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
1141 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001142 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
1143 if (Sym.getType() == STT_SECTION)
1144 return false;
1145
Rafael Espindola444576d2015-10-09 19:25:07 +00001146 // If sym references a section in a discarded group, don't keep it.
Rafael Espindola4cda5812015-10-16 15:29:48 +00001147 if (File.getSection(Sym) == &InputSection<ELFT>::Discarded)
1148 return false;
Rafael Espindola444576d2015-10-09 19:25:07 +00001149
Davide Italiano6993ba42015-09-26 00:47:56 +00001150 if (Config->DiscardNone)
1151 return true;
1152
1153 // ELF defines dynamic locals as symbols which name starts with ".L".
1154 return !(Config->DiscardLocals && SymName.startswith(".L"));
1155}
1156
Rafael Espindola35c6af32015-09-25 17:19:10 +00001157template <class ELFT>
1158SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001159 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
1160 : OutputSectionBase<ELFT>(
Rafael Espindola35c6af32015-09-25 17:19:10 +00001161 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1162 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
1163 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001164 Table(Table), StrTabSec(StrTabSec) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001165 typedef OutputSectionBase<ELFT> Base;
1166 typename Base::Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +00001167
1168 Header.sh_entsize = sizeof(Elf_Sym);
1169 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
1170}
1171
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001172// Orders symbols according to their positions in the GOT,
1173// in compliance with MIPS ABI rules.
1174// See "Global Offset Table" in Chapter 5 in the following document
1175// for detailed description:
1176// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1177static bool sortMipsSymbols(SymbolBody *L, SymbolBody *R) {
1178 if (!L->isInGot() || !R->isInGot())
1179 return R->isInGot();
1180 return L->GotIndex < R->GotIndex;
1181}
1182
Rui Ueyama0db335f2015-10-07 16:58:54 +00001183template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001184 if (this->Header.sh_size)
1185 return; // Already finalized.
1186
Rui Ueyama0db335f2015-10-07 16:58:54 +00001187 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001188 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001189 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001190
1191 if (!StrTabSec.isDynamic()) {
1192 std::stable_sort(Symbols.begin(), Symbols.end(),
Igor Kudrinf1d60292015-10-28 07:05:56 +00001193 [](SymbolBody *L, SymbolBody *R) {
1194 return getSymbolBinding(L) == STB_LOCAL &&
1195 getSymbolBinding(R) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001196 });
1197 return;
1198 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001199 if (Out<ELFT>::GnuHashTab)
1200 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1201 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001202 else if (Config->EMachine == EM_MIPS)
1203 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001204 size_t I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +00001205 for (SymbolBody *B : Symbols)
1206 B->setDynamicSymbolTableIndex(++I);
Rui Ueyama0db335f2015-10-07 16:58:54 +00001207}
1208
1209template <class ELFT>
Igor Kudrinab665fc2015-10-20 21:47:58 +00001210void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
Rui Ueyama0db335f2015-10-07 16:58:54 +00001211 StrTabSec.add(Name);
1212 ++NumVisible;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001213 ++NumLocals;
1214}
1215
1216template <class ELFT>
1217void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
1218 StrTabSec.add(Body->getName());
Igor Kudrinf1d60292015-10-28 07:05:56 +00001219 Symbols.push_back(Body);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001220 ++NumVisible;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001221}
1222
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001223template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001224 Buf += sizeof(Elf_Sym);
1225
1226 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1227 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001228 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1229 writeLocalSymbols(Buf);
1230
1231 writeGlobalSymbols(Buf);
1232}
1233
1234template <class ELFT>
1235void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1236 // Iterate over all input object files to copy their local symbols
1237 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001238 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
1239 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001240 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001241 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +00001242 error(SymNameOrErr);
1243 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001244 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001245 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +00001246
Rui Ueyamac55733e2015-09-30 00:54:29 +00001247 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001248 uintX_t VA = 0;
Rafael Espindola4cda5812015-10-16 15:29:48 +00001249 if (Sym.st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001250 ESym->st_shndx = SHN_ABS;
Rafael Espindolac159c962015-10-19 21:00:02 +00001251 VA = Sym.st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001252 } else {
Rafael Espindola48225b42015-10-23 19:55:11 +00001253 InputSectionBase<ELFT> *Section = File->getSection(Sym);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001254 if (!Section->isLive())
1255 continue;
Rafael Espindolac159c962015-10-19 21:00:02 +00001256 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1257 ESym->st_shndx = OutSec->SectionIndex;
1258 VA += OutSec->getVA() + Section->getOffset(Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001259 }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001260 ESym->st_name = StrTabSec.getOffset(SymName);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001261 ESym->st_size = Sym.st_size;
1262 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001263 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001264 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001265 }
1266 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001267}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001268
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001269template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001270void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001271 // Write the internal symbol table contents to the output symbol table
1272 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001273 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +00001274 for (SymbolBody *Body : Symbols) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001275 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001276
Rafael Espindola8614c562015-10-06 14:33:58 +00001277 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001278 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001279 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001280 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001281 case SymbolBody::DefinedRegularKind: {
1282 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
1283 if (!Sym->Section.isLive())
1284 continue;
1285 OutSec = Sym->Section.OutSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001286 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001287 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001288 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001289 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001290 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001291 case SymbolBody::SharedKind: {
Rafael Espindola9b896082015-11-03 14:34:11 +00001292 if (cast<SharedSymbol<ELFT>>(Body)->needsCopy())
George Rimarbc590fe2015-10-28 16:48:58 +00001293 OutSec = Out<ELFT>::Bss;
1294 break;
1295 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001296 case SymbolBody::UndefinedKind:
1297 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001298 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001299 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001300 }
1301
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001302 StringRef Name = Body->getName();
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001303 ESym->st_name = StrTabSec.getOffset(Name);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001304
Rafael Espindola8614c562015-10-06 14:33:58 +00001305 unsigned char Type = STT_NOTYPE;
1306 uintX_t Size = 0;
1307 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
1308 const Elf_Sym &InputSym = EBody->Sym;
Rafael Espindola8614c562015-10-06 14:33:58 +00001309 Type = InputSym.getType();
1310 Size = InputSym.st_size;
1311 }
1312
Igor Kudrin853b88d2015-10-20 20:52:14 +00001313 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001314 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001315 ESym->setVisibility(Body->getVisibility());
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001316 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001317
Rafael Espindola8614c562015-10-06 14:33:58 +00001318 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +00001319 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +00001320 else if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001321 ESym->st_shndx = OutSec->SectionIndex;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001322
1323 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001324 }
1325}
1326
Igor Kudrin853b88d2015-10-20 20:52:14 +00001327template <class ELFT>
1328uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001329 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001330 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1331 return STB_LOCAL;
1332 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body))
1333 return EBody->Sym.getBinding();
1334 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1335}
1336
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001337namespace lld {
1338namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001339template class OutputSectionBase<ELF32LE>;
1340template class OutputSectionBase<ELF32BE>;
1341template class OutputSectionBase<ELF64LE>;
1342template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001343
George Rimar648a2c32015-10-20 08:54:27 +00001344template class GotPltSection<ELF32LE>;
1345template class GotPltSection<ELF32BE>;
1346template class GotPltSection<ELF64LE>;
1347template class GotPltSection<ELF64BE>;
1348
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001349template class GotSection<ELF32LE>;
1350template class GotSection<ELF32BE>;
1351template class GotSection<ELF64LE>;
1352template class GotSection<ELF64BE>;
1353
1354template class PltSection<ELF32LE>;
1355template class PltSection<ELF32BE>;
1356template class PltSection<ELF64LE>;
1357template class PltSection<ELF64BE>;
1358
1359template class RelocationSection<ELF32LE>;
1360template class RelocationSection<ELF32BE>;
1361template class RelocationSection<ELF64LE>;
1362template class RelocationSection<ELF64BE>;
1363
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001364template class InterpSection<ELF32LE>;
1365template class InterpSection<ELF32BE>;
1366template class InterpSection<ELF64LE>;
1367template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001368
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001369template class GnuHashTableSection<ELF32LE>;
1370template class GnuHashTableSection<ELF32BE>;
1371template class GnuHashTableSection<ELF64LE>;
1372template class GnuHashTableSection<ELF64BE>;
1373
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001374template class HashTableSection<ELF32LE>;
1375template class HashTableSection<ELF32BE>;
1376template class HashTableSection<ELF64LE>;
1377template class HashTableSection<ELF64BE>;
1378
1379template class DynamicSection<ELF32LE>;
1380template class DynamicSection<ELF32BE>;
1381template class DynamicSection<ELF64LE>;
1382template class DynamicSection<ELF64BE>;
1383
1384template class OutputSection<ELF32LE>;
1385template class OutputSection<ELF32BE>;
1386template class OutputSection<ELF64LE>;
1387template class OutputSection<ELF64BE>;
1388
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001389template class EHOutputSection<ELF32LE>;
1390template class EHOutputSection<ELF32BE>;
1391template class EHOutputSection<ELF64LE>;
1392template class EHOutputSection<ELF64BE>;
1393
Rafael Espindolac159c962015-10-19 21:00:02 +00001394template class MergeOutputSection<ELF32LE>;
1395template class MergeOutputSection<ELF32BE>;
1396template class MergeOutputSection<ELF64LE>;
1397template class MergeOutputSection<ELF64BE>;
1398
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001399template class StringTableSection<ELF32LE>;
1400template class StringTableSection<ELF32BE>;
1401template class StringTableSection<ELF64LE>;
1402template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001403
1404template class SymbolTableSection<ELF32LE>;
1405template class SymbolTableSection<ELF32BE>;
1406template class SymbolTableSection<ELF64LE>;
1407template class SymbolTableSection<ELF64BE>;
1408
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001409template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1410template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1411template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1412template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +00001413
Rafael Espindola56f965f2015-09-21 22:48:12 +00001414template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001415getLocalRelTarget(const ObjectFile<ELF32LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001416 const ELFFile<ELF32LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001417template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001418getLocalRelTarget(const ObjectFile<ELF32BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001419 const ELFFile<ELF32BE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001420template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001421getLocalRelTarget(const ObjectFile<ELF64LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001422 const ELFFile<ELF64LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001423template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001424getLocalRelTarget(const ObjectFile<ELF64BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001425 const ELFFile<ELF64BE>::Elf_Rel &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001426
Rafael Espindolac159c962015-10-19 21:00:02 +00001427template ELFFile<ELF32LE>::uintX_t
1428getLocalRelTarget(const ObjectFile<ELF32LE> &,
1429 const ELFFile<ELF32LE>::Elf_Rela &);
1430template ELFFile<ELF32BE>::uintX_t
1431getLocalRelTarget(const ObjectFile<ELF32BE> &,
1432 const ELFFile<ELF32BE>::Elf_Rela &);
1433template ELFFile<ELF64LE>::uintX_t
1434getLocalRelTarget(const ObjectFile<ELF64LE> &,
1435 const ELFFile<ELF64LE>::Elf_Rela &);
1436template ELFFile<ELF64BE>::uintX_t
1437getLocalRelTarget(const ObjectFile<ELF64BE> &,
1438 const ELFFile<ELF64BE>::Elf_Rela &);
1439
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001440template bool includeInSymtab<ELF32LE>(const SymbolBody &);
1441template bool includeInSymtab<ELF32BE>(const SymbolBody &);
1442template bool includeInSymtab<ELF64LE>(const SymbolBody &);
1443template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001444
Rafael Espindola444576d2015-10-09 19:25:07 +00001445template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1446 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001447 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001448template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1449 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001450 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001451template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1452 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001453 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001454template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1455 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001456 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001457}
1458}