blob: b9af139208a0fb97f769d47e62969069bd6c5c03 [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) {
Rui Ueyama5f551ae2015-10-14 14:02:06 +000037 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000038}
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 Ueyama5f551ae2015-10-14 14:02:06 +000048 return this->getVA() + B.GotIndex * sizeof(uintX_t);
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 Ueyamac58656c2015-10-13 16:59:30 +000073 uint64_t Got = Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +000074 uint64_t Plt = this->getVA() + Off;
Rui Ueyamac58656c2015-10-13 16:59:30 +000075 Target->writePltEntry(Buf + Off, Got, Plt);
Rui Ueyama242ddf42015-10-12 18:56:36 +000076 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 Ueyamac58656c2015-10-13 16:59:30 +000088 return this->getVA() + B.PltIndex * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +000089}
90
91template <class ELFT>
92void PltSection<ELFT>::finalize() {
Rui Ueyamac58656c2015-10-13 16:59:30 +000093 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>
Rui Ueyamac58656c2015-10-13 16:59:30 +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);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000120 if (Body)
121 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000122
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123 uint32_t Type = RI.getType(IsMips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000124
125 bool CanBePreempted = canBePreempted(Body);
126 uintX_t Addend = 0;
127 if (!CanBePreempted) {
128 if (IsRela) {
129 if (Body)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000130 Addend += getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body));
Rafael Espindola52dca342015-10-07 00:58:20 +0000131 else
Rui Ueyama126d08f2015-10-12 20:28:22 +0000132 Addend += getLocalRelTarget(File, RI);
Rafael Espindola52dca342015-10-07 00:58:20 +0000133 }
134 P->setSymbolAndType(0, Target->getRelativeReloc(), IsMips64EL);
135 }
136
Rafael Espindolaae244002015-10-05 19:30:12 +0000137 if (Body && Target->relocNeedsGot(Type, *Body)) {
Rui Ueyamac58656c2015-10-13 16:59:30 +0000138 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
Rafael Espindola52dca342015-10-07 00:58:20 +0000139 if (CanBePreempted)
140 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
Rui Ueyamac58656c2015-10-13 16:59:30 +0000141 Target->getGotReloc(), IsMips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000142 } else {
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000143 if (IsRela)
Rafael Espindola52dca342015-10-07 00:58:20 +0000144 Addend += static_cast<const Elf_Rela &>(RI).r_addend;
Rui Ueyamab4908762015-10-07 17:04:18 +0000145 P->r_offset = RI.r_offset + C.getOutputSectionOff() + OutSec->getVA();
Rafael Espindola52dca342015-10-07 00:58:20 +0000146 if (CanBePreempted)
Rafael Espindolaae244002015-10-05 19:30:12 +0000147 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type,
148 IsMips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000149 }
Rafael Espindola52dca342015-10-07 00:58:20 +0000150
151 if (IsRela)
152 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000153 }
154}
155
156template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000157 this->Header.sh_link = Out<ELFT>::DynSymTab->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000158 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
159}
160
161template <bool Is64Bits>
162InterpSection<Is64Bits>::InterpSection()
163 : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS,
164 llvm::ELF::SHF_ALLOC) {
165 this->Header.sh_size = Config->DynamicLinker.size() + 1;
166 this->Header.sh_addralign = 1;
167}
168
169template <bool Is64Bits>
170template <endianness E>
171void OutputSectionBase<Is64Bits>::writeHeaderTo(
172 typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) {
173 SHdr->sh_name = Header.sh_name;
174 SHdr->sh_type = Header.sh_type;
175 SHdr->sh_flags = Header.sh_flags;
176 SHdr->sh_addr = Header.sh_addr;
177 SHdr->sh_offset = Header.sh_offset;
178 SHdr->sh_size = Header.sh_size;
179 SHdr->sh_link = Header.sh_link;
180 SHdr->sh_info = Header.sh_info;
181 SHdr->sh_addralign = Header.sh_addralign;
182 SHdr->sh_entsize = Header.sh_entsize;
183}
184
185template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) {
186 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
187}
188
Rafael Espindola35c6af32015-09-25 17:19:10 +0000189template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000190HashTableSection<ELFT>::HashTableSection()
Rafael Espindola35c6af32015-09-25 17:19:10 +0000191 : OutputSectionBase<ELFT::Is64Bits>(".hash", llvm::ELF::SHT_HASH,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000192 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000193 this->Header.sh_entsize = sizeof(Elf_Word);
194 this->Header.sh_addralign = sizeof(Elf_Word);
195}
196
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
198 StringRef Name = S->getName();
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000199 Out<ELFT>::DynSymTab->addSymbol(Name);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200 Hashes.push_back(hash(Name));
201 S->setDynamicSymbolTableIndex(Hashes.size());
202}
203
Rui Ueyama0db335f2015-10-07 16:58:54 +0000204template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000205 this->Header.sh_link = Out<ELFT>::DynSymTab->getSectionIndex();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000206
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000207 assert(Out<ELFT>::DynSymTab->getNumSymbols() == Hashes.size() + 1);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000208 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000209 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000210
211 // Create as many buckets as there are symbols.
212 // FIXME: This is simplistic. We can try to optimize it, but implementing
213 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000214 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000215 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
216}
217
218template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000219 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000220 auto *P = reinterpret_cast<Elf_Word *>(Buf);
221 *P++ = NumSymbols; // nbucket
222 *P++ = NumSymbols; // nchain
223
224 Elf_Word *Buckets = P;
225 Elf_Word *Chains = P + NumSymbols;
226
227 for (unsigned I = 1; I < NumSymbols; ++I) {
228 uint32_t Hash = Hashes[I - 1] % NumSymbols;
229 Chains[I] = Buckets[Hash];
230 Buckets[Hash] = I;
231 }
232}
233
Rafael Espindola35c6af32015-09-25 17:19:10 +0000234template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000235DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rafael Espindola35c6af32015-09-25 17:19:10 +0000236 : OutputSectionBase<ELFT::Is64Bits>(".dynamic", llvm::ELF::SHT_DYNAMIC,
237 llvm::ELF::SHF_ALLOC |
238 llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000239 SymTab(SymTab) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000240 typename Base::HeaderT &Header = this->Header;
241 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
242 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
243}
244
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000246 if (this->Header.sh_size)
247 return; // Already finalized.
248
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249 typename Base::HeaderT &Header = this->Header;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000250 Header.sh_link = Out<ELFT>::DynStrTab->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251
252 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000253 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000254 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000255 ++NumEntries; // DT_RELASZ / DT_RELSZ
256 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000257 }
258 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000259 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000260 ++NumEntries; // DT_STRTAB
261 ++NumEntries; // DT_STRSZ
262 ++NumEntries; // DT_HASH
263
Rui Ueyama7de3f372015-10-01 19:36:04 +0000264 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000265 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000266 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000267 }
268
269 if (!Config->SoName.empty()) {
270 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000271 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000272 }
273
Rafael Espindola77572242015-10-02 19:37:55 +0000274 if (PreInitArraySec)
275 NumEntries += 2;
276 if (InitArraySec)
277 NumEntries += 2;
278 if (FiniArraySec)
279 NumEntries += 2;
280
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000281 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000282 if (!F->isNeeded())
283 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000284 Out<ELFT>::DynStrTab->add(F->getSoName());
285 ++NumEntries;
286 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000287
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000288 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
289 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
290 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
291 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000292 if (InitSym)
293 ++NumEntries; // DT_INIT
294 if (FiniSym)
295 ++NumEntries; // DT_FINI
Davide Italianocebb4492015-10-13 21:02:34 +0000296 if (Config->ZNow || Config->Bsymbolic)
George Rimar97aad172015-10-07 15:00:21 +0000297 ++NumEntries; // DT_FLAGS_1
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000298 ++NumEntries; // DT_NULL
299
300 Header.sh_size = NumEntries * Header.sh_entsize;
301}
302
303template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000304 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
305
Rui Ueyama8c205d52015-10-02 01:33:31 +0000306 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
307 P->d_tag = Tag;
308 P->d_un.d_ptr = Val;
309 ++P;
310 };
311
312 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
313 P->d_tag = Tag;
314 P->d_un.d_val = Val;
315 ++P;
316 };
317
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000318 if (Out<ELFT>::RelaDyn->hasRelocs()) {
319 bool IsRela = Out<ELFT>::RelaDyn->isRela();
320 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
321 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000322 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
323 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000325
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000326 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000327 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000328 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
329 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
330 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000331
Rui Ueyama8c205d52015-10-02 01:33:31 +0000332 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000333
334 // If --enable-new-dtags is set lld emits DT_RUNPATH
335 // instead of DT_RPATH. The two tags are functionally
336 // equivalent except for the following:
337 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
338 // DT_RPATH is searched before.
339 // - DT_RUNPATH is used only to search for direct
340 // dependencies of the object it's contained in, while
341 // DT_RPATH is used for indirect dependencies as well.
342 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000343 Out<ELFT>::DynStrTab->getFileOff(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000344
Rui Ueyama8c205d52015-10-02 01:33:31 +0000345 if (!Config->SoName.empty())
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000346 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getFileOff(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000347
Rafael Espindola77572242015-10-02 19:37:55 +0000348 auto WriteArray = [&](int32_t T1, int32_t T2,
349 const OutputSection<ELFT> *Sec) {
350 if (!Sec)
351 return;
352 WritePtr(T1, Sec->getVA());
353 WriteVal(T2, Sec->getSize());
354 };
355 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
356 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
357 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
358
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000359 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000360 if (F->isNeeded())
361 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getFileOff(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000362
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000363 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000364 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000365 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000366 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000367
Davide Italianocebb4492015-10-13 21:02:34 +0000368 uint32_t Flags = 0;
369 if (Config->Bsymbolic)
370 Flags |= DF_SYMBOLIC;
George Rimar97aad172015-10-07 15:00:21 +0000371 if (Config->ZNow)
Davide Italianocebb4492015-10-13 21:02:34 +0000372 Flags |= DF_1_NOW;
373 if (Flags)
374 WriteVal(DT_FLAGS_1, Flags);
George Rimar97aad172015-10-07 15:00:21 +0000375
Rui Ueyama8c205d52015-10-02 01:33:31 +0000376 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000377}
378
379template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000380OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000381 uintX_t sh_flags)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000382 : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000383
384template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000385void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
386 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000387 C->setOutputSection(this);
388 uint32_t Align = C->getAlign();
389 if (Align > this->Header.sh_addralign)
390 this->Header.sh_addralign = Align;
391
392 uintX_t Off = this->Header.sh_size;
393 Off = RoundUpToAlignment(Off, Align);
394 C->setOutputSectionOff(Off);
395 Off += C->getSize();
396 this->Header.sh_size = Off;
397}
398
399template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000400typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000401 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000402 case SymbolBody::DefinedSyntheticKind: {
403 auto &D = cast<DefinedSynthetic<ELFT>>(S);
404 return D.Section.getVA() + D.Sym.st_value;
405 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000406 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000407 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000408 case SymbolBody::DefinedRegularKind: {
409 const auto &DR = cast<DefinedRegular<ELFT>>(S);
410 const InputSection<ELFT> *SC = &DR.Section;
411 OutputSection<ELFT> *OS = SC->getOutputSection();
412 return OS->getVA() + SC->getOutputSectionOff() + DR.Sym.st_value;
413 }
414 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000415 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000416 case SymbolBody::SharedKind:
417 case SymbolBody::UndefinedKind:
418 return 0;
419 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000420 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
421 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000422 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000423 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000424}
425
Rui Ueyama34f29242015-10-13 19:51:57 +0000426// Returns a VA which a relocatin RI refers to. Used only for local symbols.
427// For non-local symbols, use getSymVA instead.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000428template <class ELFT>
429typename ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000430lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
431 const typename ELFFile<ELFT>::Elf_Rel &RI) {
432 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
433 const Elf_Sym *Sym =
434 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000435
Rui Ueyama126d08f2015-10-12 20:28:22 +0000436 // For certain special relocations, such as R_PPC64_TOC, there's no
437 // corresponding symbol. Just return 0 in that case.
438 if (!Sym)
439 return 0;
440
441 uint32_t SecIndex = Sym->st_shndx;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000442 if (SecIndex == SHN_XINDEX)
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000443 SecIndex = File.getObj().getExtendedSymbolTableIndex(
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000444 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000445 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
446 InputSection<ELFT> *Section = Sections[SecIndex];
Rafael Espindola444576d2015-10-09 19:25:07 +0000447
448 // According to the ELF spec reference to a local symbol from outside
449 // the group are not allowed. Unfortunately .eh_frame breaks that rule
450 // and must be treated specially. For now we just replace the symbol with
451 // 0.
452 if (Section == &InputSection<ELFT>::Discarded)
453 return 0;
454
Rui Ueyamab4908762015-10-07 17:04:18 +0000455 OutputSection<ELFT> *OutSec = Section->getOutputSection();
456 return OutSec->getVA() + Section->getOutputSectionOff() + Sym->st_value;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000457}
458
Rui Ueyama34f29242015-10-13 19:51:57 +0000459// Returns true if a symbol can be replaced at load-time by a symbol
460// with the same name defined in other ELF executable or DSO.
Rafael Espindolaa6627382015-10-06 23:56:53 +0000461bool lld::elf2::canBePreempted(const SymbolBody *Body) {
462 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000463 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000464 if (Body->isShared())
465 return true;
466 if (Body->isUndefined() && !Body->isWeak())
Rafael Espindolaa6627382015-10-06 23:56:53 +0000467 return true;
468 if (!Config->Shared)
469 return false;
Rafael Espindola52dca342015-10-07 00:58:20 +0000470 return Body->getMostConstrainingVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000471}
472
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000473template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000474 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000475 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000476}
477
478template <bool Is64Bits>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000479StringTableSection<Is64Bits>::StringTableSection(bool Dynamic)
480 : OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab",
481 llvm::ELF::SHT_STRTAB,
482 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
483 Dynamic(Dynamic) {
484 this->Header.sh_addralign = 1;
485}
486
487template <bool Is64Bits>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000488void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
489 StringRef Data = StrTabBuilder.data();
490 memcpy(Buf, Data.data(), Data.size());
491}
492
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000493template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000494 if (!B.isUsedInRegularObj())
495 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000496
497 // Don't include synthetic symbols like __init_array_start in every output.
498 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
499 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
500 return false;
501
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000502 return true;
503}
504
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000505bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000506 uint8_t V = B.getMostConstrainingVisibility();
507 if (V != STV_DEFAULT && V != STV_PROTECTED)
508 return false;
509
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000510 if (Config->ExportDynamic || Config->Shared)
511 return true;
512 return B.isUsedInDynamicReloc();
513}
514
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000515template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +0000516bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
517 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000518 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
519 if (Sym.getType() == STT_SECTION)
520 return false;
521
Rafael Espindola444576d2015-10-09 19:25:07 +0000522 // If sym references a section in a discarded group, don't keep it.
523 uint32_t SecIndex = Sym.st_shndx;
524 if (SecIndex != SHN_ABS) {
525 if (SecIndex == SHN_XINDEX)
526 SecIndex = File.getObj().getExtendedSymbolTableIndex(
527 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
528 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
529 const InputSection<ELFT> *Section = Sections[SecIndex];
530 if (Section == &InputSection<ELFT>::Discarded)
531 return false;
532 }
533
Davide Italiano6993ba42015-09-26 00:47:56 +0000534 if (Config->DiscardNone)
535 return true;
536
537 // ELF defines dynamic locals as symbols which name starts with ".L".
538 return !(Config->DiscardLocals && SymName.startswith(".L"));
539}
540
Rafael Espindola35c6af32015-09-25 17:19:10 +0000541template <class ELFT>
542SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000543 SymbolTable<ELFT> &Table, StringTableSection<ELFT::Is64Bits> &StrTabSec)
Rafael Espindola35c6af32015-09-25 17:19:10 +0000544 : OutputSectionBase<ELFT::Is64Bits>(
545 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
546 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
547 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000548 Table(Table), StrTabSec(StrTabSec) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000549 typedef OutputSectionBase<ELFT::Is64Bits> Base;
550 typename Base::HeaderT &Header = this->Header;
551
552 Header.sh_entsize = sizeof(Elf_Sym);
553 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
554}
555
Rui Ueyama0db335f2015-10-07 16:58:54 +0000556template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
557 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
558 this->Header.sh_link = StrTabSec.getSectionIndex();
559 this->Header.sh_info = NumLocals + 1;
560}
561
562template <class ELFT>
563void SymbolTableSection<ELFT>::addSymbol(StringRef Name, bool isLocal) {
564 StrTabSec.add(Name);
565 ++NumVisible;
566 if (isLocal)
567 ++NumLocals;
568}
569
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000570template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000571 Buf += sizeof(Elf_Sym);
572
573 // All symbols with STB_LOCAL binding precede the weak and global symbols.
574 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000575 if (!Config->DiscardAll && !StrTabSec.isDynamic())
576 writeLocalSymbols(Buf);
577
578 writeGlobalSymbols(Buf);
579}
580
581template <class ELFT>
582void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
583 // Iterate over all input object files to copy their local symbols
584 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000585 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
586 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000587 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000588 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +0000589 error(SymNameOrErr);
590 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000591 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000592 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +0000593
Rui Ueyamac55733e2015-09-30 00:54:29 +0000594 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
595 Buf += sizeof(*ESym);
Rafael Espindola26fd69d2015-10-09 16:15:57 +0000596 ESym->st_name = StrTabSec.getFileOff(SymName);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000597 ESym->st_size = Sym.st_size;
598 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
599 uint32_t SecIndex = Sym.st_shndx;
600 uintX_t VA = Sym.st_value;
601 if (SecIndex == SHN_ABS) {
602 ESym->st_shndx = SHN_ABS;
603 } else {
604 if (SecIndex == SHN_XINDEX)
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000605 SecIndex = File->getObj().getExtendedSymbolTableIndex(
606 &Sym, File->getSymbolTable(), File->getSymbolTableShndx());
607 ArrayRef<InputSection<ELFT> *> Sections = File->getSections();
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000608 const InputSection<ELFT> *Section = Sections[SecIndex];
Rui Ueyamab4908762015-10-07 17:04:18 +0000609 const OutputSection<ELFT> *OutSec = Section->getOutputSection();
610 ESym->st_shndx = OutSec->getSectionIndex();
611 VA += OutSec->getVA() + Section->getOutputSectionOff();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000612 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000613 ESym->st_value = VA;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000614 }
615 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000616}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000617
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000618template <class ELFT>
619void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *&Buf) {
620 // Write the internal symbol table contents to the output symbol table
621 // pointed by Buf.
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000622 uint8_t *Start = Buf;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000623 for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000624 StringRef Name = P.first;
625 Symbol *Sym = P.second;
626 SymbolBody *Body = Sym->Body;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000627 if (!includeInSymtab<ELFT>(*Body))
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000628 continue;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000629 if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body))
630 continue;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000631
632 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac55733e2015-09-30 00:54:29 +0000633 Buf += sizeof(*ESym);
Rafael Espindola8614c562015-10-06 14:33:58 +0000634
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000635 ESym->st_name = StrTabSec.getFileOff(Name);
636
Rui Ueyamab4908762015-10-07 17:04:18 +0000637 const OutputSection<ELFT> *OutSec = nullptr;
Rafael Espindola25b0acb2015-09-25 17:32:37 +0000638 const InputSection<ELFT> *Section = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000639
Rafael Espindola8614c562015-10-06 14:33:58 +0000640 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000641 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +0000642 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000643 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000644 case SymbolBody::DefinedRegularKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000645 Section = &cast<DefinedRegular<ELFT>>(Body)->Section;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000646 break;
647 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000648 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000649 break;
650 case SymbolBody::UndefinedKind:
651 case SymbolBody::DefinedAbsoluteKind:
652 case SymbolBody::SharedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000653 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000654 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000655 }
656
Rafael Espindola8614c562015-10-06 14:33:58 +0000657 unsigned char Binding = Body->isWeak() ? STB_WEAK : STB_GLOBAL;
658 unsigned char Type = STT_NOTYPE;
659 uintX_t Size = 0;
660 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
661 const Elf_Sym &InputSym = EBody->Sym;
662 Binding = InputSym.getBinding();
663 Type = InputSym.getType();
664 Size = InputSym.st_size;
665 }
666
667 unsigned char Visibility = Body->getMostConstrainingVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000668 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
669 Binding = STB_LOCAL;
670
Rafael Espindola8614c562015-10-06 14:33:58 +0000671 ESym->setBindingAndType(Binding, Type);
672 ESym->st_size = Size;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000673 ESym->setVisibility(Visibility);
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000674 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000675
676 if (Section)
Rui Ueyamab4908762015-10-07 17:04:18 +0000677 OutSec = Section->getOutputSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000678
Rafael Espindola8614c562015-10-06 14:33:58 +0000679 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +0000680 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +0000681 else if (OutSec)
682 ESym->st_shndx = OutSec->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000683 }
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000684 if (!StrTabSec.isDynamic())
685 std::stable_sort(
686 reinterpret_cast<Elf_Sym *>(Start), reinterpret_cast<Elf_Sym *>(Buf),
687 [](const Elf_Sym &A, const Elf_Sym &B) -> bool {
688 return A.getBinding() == STB_LOCAL && B.getBinding() != STB_LOCAL;
689 });
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000690}
691
692namespace lld {
693namespace elf2 {
694template class OutputSectionBase<false>;
695template class OutputSectionBase<true>;
696
697template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000698 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000699template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000700 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000701template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000702 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000703template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000704 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000705
706template class GotSection<ELF32LE>;
707template class GotSection<ELF32BE>;
708template class GotSection<ELF64LE>;
709template class GotSection<ELF64BE>;
710
711template class PltSection<ELF32LE>;
712template class PltSection<ELF32BE>;
713template class PltSection<ELF64LE>;
714template class PltSection<ELF64BE>;
715
716template class RelocationSection<ELF32LE>;
717template class RelocationSection<ELF32BE>;
718template class RelocationSection<ELF64LE>;
719template class RelocationSection<ELF64BE>;
720
721template class InterpSection<false>;
722template class InterpSection<true>;
723
724template class HashTableSection<ELF32LE>;
725template class HashTableSection<ELF32BE>;
726template class HashTableSection<ELF64LE>;
727template class HashTableSection<ELF64BE>;
728
729template class DynamicSection<ELF32LE>;
730template class DynamicSection<ELF32BE>;
731template class DynamicSection<ELF64LE>;
732template class DynamicSection<ELF64BE>;
733
734template class OutputSection<ELF32LE>;
735template class OutputSection<ELF32BE>;
736template class OutputSection<ELF64LE>;
737template class OutputSection<ELF64BE>;
738
739template class StringTableSection<false>;
740template class StringTableSection<true>;
741
742template class SymbolTableSection<ELF32LE>;
743template class SymbolTableSection<ELF32BE>;
744template class SymbolTableSection<ELF64LE>;
745template class SymbolTableSection<ELF64BE>;
746
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000747template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
748template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
749template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
750template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000751
Rafael Espindola56f965f2015-09-21 22:48:12 +0000752template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000753getLocalRelTarget(const ObjectFile<ELF32LE> &,
754 const ELFFile<ELF32LE>::Elf_Rel &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000755
Rafael Espindola56f965f2015-09-21 22:48:12 +0000756template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000757getLocalRelTarget(const ObjectFile<ELF32BE> &,
758 const ELFFile<ELF32BE>::Elf_Rel &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000759
Rafael Espindola56f965f2015-09-21 22:48:12 +0000760template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000761getLocalRelTarget(const ObjectFile<ELF64LE> &,
762 const ELFFile<ELF64LE>::Elf_Rel &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000763
Rafael Espindola56f965f2015-09-21 22:48:12 +0000764template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000765getLocalRelTarget(const ObjectFile<ELF64BE> &,
766 const ELFFile<ELF64BE>::Elf_Rel &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000767
768template bool includeInSymtab<ELF32LE>(const SymbolBody &);
769template bool includeInSymtab<ELF32BE>(const SymbolBody &);
770template bool includeInSymtab<ELF64LE>(const SymbolBody &);
771template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000772
Rafael Espindola444576d2015-10-09 19:25:07 +0000773template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
774 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000775 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000776template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
777 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000778 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000779template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
780 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000781 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000782template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
783 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000784 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000785}
786}