blob: 2a0fc0c1d685d9fc5676b14c7a0c900e0ac62803 [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"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000014
Rafael Espindola5805c4f2015-09-21 21:38:08 +000015using namespace llvm;
16using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000017using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000018using namespace llvm::ELF;
19
20using namespace lld;
21using namespace lld::elf2;
22
23template <bool Is64Bits>
24OutputSectionBase<Is64Bits>::OutputSectionBase(StringRef Name, uint32_t sh_type,
25 uintX_t sh_flags)
26 : Name(Name) {
27 memset(&Header, 0, sizeof(HeaderT));
28 Header.sh_type = sh_type;
29 Header.sh_flags = sh_flags;
30}
31
Rafael Espindola35c6af32015-09-25 17:19:10 +000032template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000033GotSection<ELFT>::GotSection()
Rafael Espindola35c6af32015-09-25 17:19:10 +000034 : OutputSectionBase<ELFT::Is64Bits>(".got", llvm::ELF::SHT_PROGBITS,
35 llvm::ELF::SHF_ALLOC |
Rui Ueyama15ef5e12015-10-07 19:18:16 +000036 llvm::ELF::SHF_WRITE) {
Rafael Espindola35c6af32015-09-25 17:19:10 +000037 this->Header.sh_addralign = this->getAddrSize();
38}
39
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +000041 Sym->GotIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000042 Entries.push_back(Sym);
43}
44
45template <class ELFT>
46typename GotSection<ELFT>::uintX_t
47GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama49c68a72015-10-09 00:42:06 +000048 return this->getVA() + B.GotIndex * this->getAddrSize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000049}
50
Rafael Espindolaa6627382015-10-06 23:56:53 +000051template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
52 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +000053 uint8_t *Entry = Buf;
54 Buf += sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +000055 if (canBePreempted(B))
56 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +000057 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +000058 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +000059 }
60}
61
Rafael Espindola35c6af32015-09-25 17:19:10 +000062template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000063PltSection<ELFT>::PltSection()
Rafael Espindola35c6af32015-09-25 17:19:10 +000064 : OutputSectionBase<ELFT::Is64Bits>(".plt", llvm::ELF::SHT_PROGBITS,
65 llvm::ELF::SHF_ALLOC |
Rui Ueyama15ef5e12015-10-07 19:18:16 +000066 llvm::ELF::SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +000067 this->Header.sh_addralign = 16;
68}
69
Rafael Espindola5805c4f2015-09-21 21:38:08 +000070template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +000071 size_t Off = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072 for (const SymbolBody *E : Entries) {
Rui Ueyama36f69222015-10-09 00:50:05 +000073 uint64_t Got = Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +000074 uint64_t Plt = this->getVA() + Off;
75 Target->writePltEntry(Buf + Off, Got, Plt);
76 Off += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000077 }
78}
79
80template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +000081 Sym->PltIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 Entries.push_back(Sym);
83}
84
85template <class ELFT>
86typename PltSection<ELFT>::uintX_t
87PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama49c68a72015-10-09 00:42:06 +000088 return this->getVA() + B.PltIndex * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +000089}
90
91template <class ELFT>
92void PltSection<ELFT>::finalize() {
93 this->Header.sh_size = Entries.size() * Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000094}
95
Rafael Espindola35c6af32015-09-25 17:19:10 +000096template <class ELFT>
Rafael Espindolad5409192015-10-09 14:25:49 +000097RelocationSection<ELFT>::RelocationSection(bool IsRela)
98 : OutputSectionBase<ELFT::Is64Bits>(IsRela ? ".rela.dyn" : ".rel.dyn",
99 IsRela ? llvm::ELF::SHT_RELA
100 : llvm::ELF::SHT_REL,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000101 llvm::ELF::SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000102 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000103 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
104 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
105}
106
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000108 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000109 bool IsMips64EL = Relocs[0].C.getFile()->getObj().isMips64EL();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000110 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000111 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
112 Buf += EntrySize;
113
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000114 const InputSection<ELFT> &C = Rel.C;
115 const Elf_Rel &RI = Rel.RI;
Rui Ueyamab4908762015-10-07 17:04:18 +0000116 OutputSection<ELFT> *OutSec = C.getOutputSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000117 uint32_t SymIndex = RI.getSymbol(IsMips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000118 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000119 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000120 const ELFFile<ELFT> &Obj = File.getObj();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000121 if (Body)
122 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000123
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000124 uint32_t Type = RI.getType(IsMips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000125
126 bool CanBePreempted = canBePreempted(Body);
127 uintX_t Addend = 0;
128 if (!CanBePreempted) {
129 if (IsRela) {
130 if (Body)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000131 Addend += getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body));
Rafael Espindola52dca342015-10-07 00:58:20 +0000132 else
133 Addend += getLocalSymVA(
134 Obj.getRelocationSymbol(&RI, File.getSymbolTable()), File);
135 }
136 P->setSymbolAndType(0, Target->getRelativeReloc(), IsMips64EL);
137 }
138
Rafael Espindolaae244002015-10-05 19:30:12 +0000139 if (Body && Target->relocNeedsGot(Type, *Body)) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000140 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
Rafael Espindola52dca342015-10-07 00:58:20 +0000141 if (CanBePreempted)
142 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
143 Target->getGotReloc(), IsMips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000144 } else {
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000145 if (IsRela)
Rafael Espindola52dca342015-10-07 00:58:20 +0000146 Addend += static_cast<const Elf_Rela &>(RI).r_addend;
Rui Ueyamab4908762015-10-07 17:04:18 +0000147 P->r_offset = RI.r_offset + C.getOutputSectionOff() + OutSec->getVA();
Rafael Espindola52dca342015-10-07 00:58:20 +0000148 if (CanBePreempted)
Rafael Espindolaae244002015-10-05 19:30:12 +0000149 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type,
150 IsMips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000151 }
Rafael Espindola52dca342015-10-07 00:58:20 +0000152
153 if (IsRela)
154 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000155 }
156}
157
158template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000159 this->Header.sh_link = Out<ELFT>::DynSymTab->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000160 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
161}
162
163template <bool Is64Bits>
164InterpSection<Is64Bits>::InterpSection()
165 : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS,
166 llvm::ELF::SHF_ALLOC) {
167 this->Header.sh_size = Config->DynamicLinker.size() + 1;
168 this->Header.sh_addralign = 1;
169}
170
171template <bool Is64Bits>
172template <endianness E>
173void OutputSectionBase<Is64Bits>::writeHeaderTo(
174 typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) {
175 SHdr->sh_name = Header.sh_name;
176 SHdr->sh_type = Header.sh_type;
177 SHdr->sh_flags = Header.sh_flags;
178 SHdr->sh_addr = Header.sh_addr;
179 SHdr->sh_offset = Header.sh_offset;
180 SHdr->sh_size = Header.sh_size;
181 SHdr->sh_link = Header.sh_link;
182 SHdr->sh_info = Header.sh_info;
183 SHdr->sh_addralign = Header.sh_addralign;
184 SHdr->sh_entsize = Header.sh_entsize;
185}
186
187template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) {
188 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
189}
190
Rafael Espindola35c6af32015-09-25 17:19:10 +0000191template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000192HashTableSection<ELFT>::HashTableSection()
Rafael Espindola35c6af32015-09-25 17:19:10 +0000193 : OutputSectionBase<ELFT::Is64Bits>(".hash", llvm::ELF::SHT_HASH,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000194 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000195 this->Header.sh_entsize = sizeof(Elf_Word);
196 this->Header.sh_addralign = sizeof(Elf_Word);
197}
198
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
200 StringRef Name = S->getName();
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000201 Out<ELFT>::DynSymTab->addSymbol(Name);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000202 Hashes.push_back(hash(Name));
203 S->setDynamicSymbolTableIndex(Hashes.size());
204}
205
Rui Ueyama0db335f2015-10-07 16:58:54 +0000206template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000207 this->Header.sh_link = Out<ELFT>::DynSymTab->getSectionIndex();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000208
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000209 assert(Out<ELFT>::DynSymTab->getNumSymbols() == Hashes.size() + 1);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000210 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000211 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000212
213 // Create as many buckets as there are symbols.
214 // FIXME: This is simplistic. We can try to optimize it, but implementing
215 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000216 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000217 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
218}
219
220template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000221 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000222 auto *P = reinterpret_cast<Elf_Word *>(Buf);
223 *P++ = NumSymbols; // nbucket
224 *P++ = NumSymbols; // nchain
225
226 Elf_Word *Buckets = P;
227 Elf_Word *Chains = P + NumSymbols;
228
229 for (unsigned I = 1; I < NumSymbols; ++I) {
230 uint32_t Hash = Hashes[I - 1] % NumSymbols;
231 Chains[I] = Buckets[Hash];
232 Buckets[Hash] = I;
233 }
234}
235
Rafael Espindola35c6af32015-09-25 17:19:10 +0000236template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000237DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rafael Espindola35c6af32015-09-25 17:19:10 +0000238 : OutputSectionBase<ELFT::Is64Bits>(".dynamic", llvm::ELF::SHT_DYNAMIC,
239 llvm::ELF::SHF_ALLOC |
240 llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000241 SymTab(SymTab) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000242 typename Base::HeaderT &Header = this->Header;
243 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
244 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
245}
246
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000248 if (this->Header.sh_size)
249 return; // Already finalized.
250
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251 typename Base::HeaderT &Header = this->Header;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000252 Header.sh_link = Out<ELFT>::DynStrTab->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253
254 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000255 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000257 ++NumEntries; // DT_RELASZ / DT_RELSZ
258 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259 }
260 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000261 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262 ++NumEntries; // DT_STRTAB
263 ++NumEntries; // DT_STRSZ
264 ++NumEntries; // DT_HASH
265
Rui Ueyama7de3f372015-10-01 19:36:04 +0000266 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000267 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000268 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000269 }
270
271 if (!Config->SoName.empty()) {
272 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000273 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000274 }
275
Rafael Espindola77572242015-10-02 19:37:55 +0000276 if (PreInitArraySec)
277 NumEntries += 2;
278 if (InitArraySec)
279 NumEntries += 2;
280 if (FiniArraySec)
281 NumEntries += 2;
282
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000283 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000284 if (!F->isNeeded())
285 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000286 Out<ELFT>::DynStrTab->add(F->getSoName());
287 ++NumEntries;
288 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000289
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000290 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
291 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
292 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
293 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000294 if (InitSym)
295 ++NumEntries; // DT_INIT
296 if (FiniSym)
297 ++NumEntries; // DT_FINI
George Rimar97aad172015-10-07 15:00:21 +0000298 if (Config->ZNow)
299 ++NumEntries; // DT_FLAGS_1
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000300
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000301 ++NumEntries; // DT_NULL
302
303 Header.sh_size = NumEntries * Header.sh_entsize;
304}
305
306template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000307 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
308
Rui Ueyama8c205d52015-10-02 01:33:31 +0000309 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
310 P->d_tag = Tag;
311 P->d_un.d_ptr = Val;
312 ++P;
313 };
314
315 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
316 P->d_tag = Tag;
317 P->d_un.d_val = Val;
318 ++P;
319 };
320
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000321 if (Out<ELFT>::RelaDyn->hasRelocs()) {
322 bool IsRela = Out<ELFT>::RelaDyn->isRela();
323 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
324 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000325 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
326 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000327 }
328
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000329 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000330 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000331 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
332 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
333 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000334
Rui Ueyama8c205d52015-10-02 01:33:31 +0000335 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000336
337 // If --enable-new-dtags is set lld emits DT_RUNPATH
338 // instead of DT_RPATH. The two tags are functionally
339 // equivalent except for the following:
340 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
341 // DT_RPATH is searched before.
342 // - DT_RUNPATH is used only to search for direct
343 // dependencies of the object it's contained in, while
344 // DT_RPATH is used for indirect dependencies as well.
345 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000346 Out<ELFT>::DynStrTab->getFileOff(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000347
Rui Ueyama8c205d52015-10-02 01:33:31 +0000348 if (!Config->SoName.empty())
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000349 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getFileOff(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000350
Rafael Espindola77572242015-10-02 19:37:55 +0000351 auto WriteArray = [&](int32_t T1, int32_t T2,
352 const OutputSection<ELFT> *Sec) {
353 if (!Sec)
354 return;
355 WritePtr(T1, Sec->getVA());
356 WriteVal(T2, Sec->getSize());
357 };
358 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
359 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
360 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
361
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000362 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000363 if (F->isNeeded())
364 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getFileOff(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000365
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000366 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000367 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000368 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000369 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000370
George Rimar97aad172015-10-07 15:00:21 +0000371 if (Config->ZNow)
372 WriteVal(DT_FLAGS_1, DF_1_NOW);
373
Rui Ueyama8c205d52015-10-02 01:33:31 +0000374 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000375}
376
377template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000378OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000379 uintX_t sh_flags)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000380 : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000381
382template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000383void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
384 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000385 C->setOutputSection(this);
386 uint32_t Align = C->getAlign();
387 if (Align > this->Header.sh_addralign)
388 this->Header.sh_addralign = Align;
389
390 uintX_t Off = this->Header.sh_size;
391 Off = RoundUpToAlignment(Off, Align);
392 C->setOutputSectionOff(Off);
393 Off += C->getSize();
394 this->Header.sh_size = Off;
395}
396
397template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000398typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000399 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000400 case SymbolBody::DefinedSyntheticKind: {
401 auto &D = cast<DefinedSynthetic<ELFT>>(S);
402 return D.Section.getVA() + D.Sym.st_value;
403 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000404 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000405 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000406 case SymbolBody::DefinedRegularKind: {
407 const auto &DR = cast<DefinedRegular<ELFT>>(S);
408 const InputSection<ELFT> *SC = &DR.Section;
409 OutputSection<ELFT> *OS = SC->getOutputSection();
410 return OS->getVA() + SC->getOutputSectionOff() + DR.Sym.st_value;
411 }
412 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000413 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000414 case SymbolBody::SharedKind:
415 case SymbolBody::UndefinedKind:
416 return 0;
417 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000418 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
419 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000420 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000421 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000422}
423
424template <class ELFT>
425typename ELFFile<ELFT>::uintX_t
426lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym,
427 const ObjectFile<ELFT> &File) {
428 uint32_t SecIndex = Sym->st_shndx;
429
430 if (SecIndex == SHN_XINDEX)
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000431 SecIndex = File.getObj().getExtendedSymbolTableIndex(
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000432 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000433 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
434 InputSection<ELFT> *Section = Sections[SecIndex];
Rafael Espindola444576d2015-10-09 19:25:07 +0000435
436 // According to the ELF spec reference to a local symbol from outside
437 // the group are not allowed. Unfortunately .eh_frame breaks that rule
438 // and must be treated specially. For now we just replace the symbol with
439 // 0.
440 if (Section == &InputSection<ELFT>::Discarded)
441 return 0;
442
Rui Ueyamab4908762015-10-07 17:04:18 +0000443 OutputSection<ELFT> *OutSec = Section->getOutputSection();
444 return OutSec->getVA() + Section->getOutputSectionOff() + Sym->st_value;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000445}
446
Rafael Espindolaa6627382015-10-06 23:56:53 +0000447bool lld::elf2::canBePreempted(const SymbolBody *Body) {
448 if (!Body)
449 return false;
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000450 if (Body->isShared())
451 return true;
452 if (Body->isUndefined() && !Body->isWeak())
Rafael Espindolaa6627382015-10-06 23:56:53 +0000453 return true;
454 if (!Config->Shared)
455 return false;
Rafael Espindola52dca342015-10-07 00:58:20 +0000456 return Body->getMostConstrainingVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000457}
458
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000459template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000460 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000461 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000462}
463
464template <bool Is64Bits>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000465StringTableSection<Is64Bits>::StringTableSection(bool Dynamic)
466 : OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab",
467 llvm::ELF::SHT_STRTAB,
468 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
469 Dynamic(Dynamic) {
470 this->Header.sh_addralign = 1;
471}
472
473template <bool Is64Bits>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000474void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
475 StringRef Data = StrTabBuilder.data();
476 memcpy(Buf, Data.data(), Data.size());
477}
478
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000479template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000480 if (!B.isUsedInRegularObj())
481 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000482
483 // Don't include synthetic symbols like __init_array_start in every output.
484 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
485 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
486 return false;
487
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000488 return true;
489}
490
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000491bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000492 uint8_t V = B.getMostConstrainingVisibility();
493 if (V != STV_DEFAULT && V != STV_PROTECTED)
494 return false;
495
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000496 if (Config->ExportDynamic || Config->Shared)
497 return true;
498 return B.isUsedInDynamicReloc();
499}
500
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000501template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +0000502bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
503 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000504 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
505 if (Sym.getType() == STT_SECTION)
506 return false;
507
Rafael Espindola444576d2015-10-09 19:25:07 +0000508 // If sym references a section in a discarded group, don't keep it.
509 uint32_t SecIndex = Sym.st_shndx;
510 if (SecIndex != SHN_ABS) {
511 if (SecIndex == SHN_XINDEX)
512 SecIndex = File.getObj().getExtendedSymbolTableIndex(
513 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
514 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
515 const InputSection<ELFT> *Section = Sections[SecIndex];
516 if (Section == &InputSection<ELFT>::Discarded)
517 return false;
518 }
519
Davide Italiano6993ba42015-09-26 00:47:56 +0000520 if (Config->DiscardNone)
521 return true;
522
523 // ELF defines dynamic locals as symbols which name starts with ".L".
524 return !(Config->DiscardLocals && SymName.startswith(".L"));
525}
526
Rafael Espindola35c6af32015-09-25 17:19:10 +0000527template <class ELFT>
528SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000529 SymbolTable<ELFT> &Table, StringTableSection<ELFT::Is64Bits> &StrTabSec)
Rafael Espindola35c6af32015-09-25 17:19:10 +0000530 : OutputSectionBase<ELFT::Is64Bits>(
531 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
532 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
533 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000534 Table(Table), StrTabSec(StrTabSec) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000535 typedef OutputSectionBase<ELFT::Is64Bits> Base;
536 typename Base::HeaderT &Header = this->Header;
537
538 Header.sh_entsize = sizeof(Elf_Sym);
539 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
540}
541
Rui Ueyama0db335f2015-10-07 16:58:54 +0000542template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
543 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
544 this->Header.sh_link = StrTabSec.getSectionIndex();
545 this->Header.sh_info = NumLocals + 1;
546}
547
548template <class ELFT>
549void SymbolTableSection<ELFT>::addSymbol(StringRef Name, bool isLocal) {
550 StrTabSec.add(Name);
551 ++NumVisible;
552 if (isLocal)
553 ++NumLocals;
554}
555
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000556template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000557 Buf += sizeof(Elf_Sym);
558
559 // All symbols with STB_LOCAL binding precede the weak and global symbols.
560 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000561 if (!Config->DiscardAll && !StrTabSec.isDynamic())
562 writeLocalSymbols(Buf);
563
564 writeGlobalSymbols(Buf);
565}
566
567template <class ELFT>
568void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
569 // Iterate over all input object files to copy their local symbols
570 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000571 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
572 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000573 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000574 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +0000575 error(SymNameOrErr);
576 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000577 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000578 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +0000579
Rui Ueyamac55733e2015-09-30 00:54:29 +0000580 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
581 Buf += sizeof(*ESym);
Rafael Espindola26fd69d2015-10-09 16:15:57 +0000582 ESym->st_name = StrTabSec.getFileOff(SymName);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000583 ESym->st_size = Sym.st_size;
584 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
585 uint32_t SecIndex = Sym.st_shndx;
586 uintX_t VA = Sym.st_value;
587 if (SecIndex == SHN_ABS) {
588 ESym->st_shndx = SHN_ABS;
589 } else {
590 if (SecIndex == SHN_XINDEX)
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000591 SecIndex = File->getObj().getExtendedSymbolTableIndex(
592 &Sym, File->getSymbolTable(), File->getSymbolTableShndx());
593 ArrayRef<InputSection<ELFT> *> Sections = File->getSections();
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000594 const InputSection<ELFT> *Section = Sections[SecIndex];
Rui Ueyamab4908762015-10-07 17:04:18 +0000595 const OutputSection<ELFT> *OutSec = Section->getOutputSection();
596 ESym->st_shndx = OutSec->getSectionIndex();
597 VA += OutSec->getVA() + Section->getOutputSectionOff();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000598 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000599 ESym->st_value = VA;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000600 }
601 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000602}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000603
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000604template <class ELFT>
605void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *&Buf) {
606 // Write the internal symbol table contents to the output symbol table
607 // pointed by Buf.
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000608 uint8_t *Start = Buf;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000609 for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000610 StringRef Name = P.first;
611 Symbol *Sym = P.second;
612 SymbolBody *Body = Sym->Body;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000613 if (!includeInSymtab<ELFT>(*Body))
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000614 continue;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000615 if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body))
616 continue;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000617
618 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac55733e2015-09-30 00:54:29 +0000619 Buf += sizeof(*ESym);
Rafael Espindola8614c562015-10-06 14:33:58 +0000620
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000621 ESym->st_name = StrTabSec.getFileOff(Name);
622
Rui Ueyamab4908762015-10-07 17:04:18 +0000623 const OutputSection<ELFT> *OutSec = nullptr;
Rafael Espindola25b0acb2015-09-25 17:32:37 +0000624 const InputSection<ELFT> *Section = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000625
Rafael Espindola8614c562015-10-06 14:33:58 +0000626 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000627 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +0000628 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000629 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000630 case SymbolBody::DefinedRegularKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000631 Section = &cast<DefinedRegular<ELFT>>(Body)->Section;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000632 break;
633 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000634 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000635 break;
636 case SymbolBody::UndefinedKind:
637 case SymbolBody::DefinedAbsoluteKind:
638 case SymbolBody::SharedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000639 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000640 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000641 }
642
Rafael Espindola8614c562015-10-06 14:33:58 +0000643 unsigned char Binding = Body->isWeak() ? STB_WEAK : STB_GLOBAL;
644 unsigned char Type = STT_NOTYPE;
645 uintX_t Size = 0;
646 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
647 const Elf_Sym &InputSym = EBody->Sym;
648 Binding = InputSym.getBinding();
649 Type = InputSym.getType();
650 Size = InputSym.st_size;
651 }
652
653 unsigned char Visibility = Body->getMostConstrainingVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000654 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
655 Binding = STB_LOCAL;
656
Rafael Espindola8614c562015-10-06 14:33:58 +0000657 ESym->setBindingAndType(Binding, Type);
658 ESym->st_size = Size;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000659 ESym->setVisibility(Visibility);
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000660 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000661
662 if (Section)
Rui Ueyamab4908762015-10-07 17:04:18 +0000663 OutSec = Section->getOutputSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000664
Rafael Espindola8614c562015-10-06 14:33:58 +0000665 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +0000666 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +0000667 else if (OutSec)
668 ESym->st_shndx = OutSec->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000669 }
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000670 if (!StrTabSec.isDynamic())
671 std::stable_sort(
672 reinterpret_cast<Elf_Sym *>(Start), reinterpret_cast<Elf_Sym *>(Buf),
673 [](const Elf_Sym &A, const Elf_Sym &B) -> bool {
674 return A.getBinding() == STB_LOCAL && B.getBinding() != STB_LOCAL;
675 });
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000676}
677
678namespace lld {
679namespace elf2 {
680template class OutputSectionBase<false>;
681template class OutputSectionBase<true>;
682
683template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000684 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000685template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000686 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000687template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000688 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000689template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000690 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000691
692template class GotSection<ELF32LE>;
693template class GotSection<ELF32BE>;
694template class GotSection<ELF64LE>;
695template class GotSection<ELF64BE>;
696
697template class PltSection<ELF32LE>;
698template class PltSection<ELF32BE>;
699template class PltSection<ELF64LE>;
700template class PltSection<ELF64BE>;
701
702template class RelocationSection<ELF32LE>;
703template class RelocationSection<ELF32BE>;
704template class RelocationSection<ELF64LE>;
705template class RelocationSection<ELF64BE>;
706
707template class InterpSection<false>;
708template class InterpSection<true>;
709
710template class HashTableSection<ELF32LE>;
711template class HashTableSection<ELF32BE>;
712template class HashTableSection<ELF64LE>;
713template class HashTableSection<ELF64BE>;
714
715template class DynamicSection<ELF32LE>;
716template class DynamicSection<ELF32BE>;
717template class DynamicSection<ELF64LE>;
718template class DynamicSection<ELF64BE>;
719
720template class OutputSection<ELF32LE>;
721template class OutputSection<ELF32BE>;
722template class OutputSection<ELF64LE>;
723template class OutputSection<ELF64BE>;
724
725template class StringTableSection<false>;
726template class StringTableSection<true>;
727
728template class SymbolTableSection<ELF32LE>;
729template class SymbolTableSection<ELF32BE>;
730template class SymbolTableSection<ELF64LE>;
731template class SymbolTableSection<ELF64BE>;
732
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000733template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
734template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
735template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
736template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000737
Rafael Espindola56f965f2015-09-21 22:48:12 +0000738template ELFFile<ELF32LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000739getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *, const ObjectFile<ELF32LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000740
Rafael Espindola56f965f2015-09-21 22:48:12 +0000741template ELFFile<ELF32BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000742getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *, const ObjectFile<ELF32BE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000743
Rafael Espindola56f965f2015-09-21 22:48:12 +0000744template ELFFile<ELF64LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000745getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *, const ObjectFile<ELF64LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000746
Rafael Espindola56f965f2015-09-21 22:48:12 +0000747template ELFFile<ELF64BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000748getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *, const ObjectFile<ELF64BE> &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000749
750template bool includeInSymtab<ELF32LE>(const SymbolBody &);
751template bool includeInSymtab<ELF32BE>(const SymbolBody &);
752template bool includeInSymtab<ELF64LE>(const SymbolBody &);
753template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000754
Rafael Espindola444576d2015-10-09 19:25:07 +0000755template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
756 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000757 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000758template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
759 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000760 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000761template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
762 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000763 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000764template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
765 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000766 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000767}
768}