blob: 0a6c33c1d022ed15aeef3c03a6ccc784d2831edd [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);
38 // .got.plt has 3 reserved entry
39 Entries.resize(3);
40}
41
42template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
43 Sym->GotPltIndex = Entries.size();
44 Entries.push_back(Sym);
45}
46
47template <class ELFT> bool GotPltSection<ELFT>::empty() const {
48 return Entries.size() == 3;
49}
50
51template <class ELFT>
52typename GotPltSection<ELFT>::uintX_t
53GotPltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
54 return this->getVA() + B.GotPltIndex * sizeof(uintX_t);
55}
56
57template <class ELFT> void GotPltSection<ELFT>::finalize() {
58 this->Header.sh_size = Entries.size() * sizeof(uintX_t);
59}
60
61template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
62 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(
63 Buf, Out<ELFT>::Dynamic->getVA());
64 for (const SymbolBody *B : Entries) {
65 if (B)
66 Target->writeGotPltEntry(Buf, Out<ELFT>::Plt->getEntryAddr(*B));
67 Buf += sizeof(uintX_t);
68 }
69}
70
71template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000072GotSection<ELFT>::GotSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000073 : OutputSectionBase<ELFT>(".got", llvm::ELF::SHT_PROGBITS,
74 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000075 if (Config->EMachine == EM_MIPS)
76 this->Header.sh_flags |= llvm::ELF::SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000077 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000078}
79
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000081 Sym->GotIndex = Target->getGotHeaderEntriesNum() + Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 Entries.push_back(Sym);
83}
84
85template <class ELFT>
86typename GotSection<ELFT>::uintX_t
87GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama5f551ae2015-10-14 14:02:06 +000088 return this->getVA() + B.GotIndex * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000089}
90
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000091template <class ELFT> void GotSection<ELFT>::finalize() {
92 this->Header.sh_size =
93 (Target->getGotHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
94}
95
Rafael Espindolaa6627382015-10-06 23:56:53 +000096template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000097 Target->writeGotHeaderEntries(Buf);
98 Buf += Target->getGotHeaderEntriesNum() * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +000099 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000100 uint8_t *Entry = Buf;
101 Buf += sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000102 // MIPS has special rules to fill up GOT entries.
103 // See "Global Offset Table" in Chapter 5 in the following document
104 // for detailed description:
105 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
106 // As the first approach, we can just store addresses for all symbols.
107 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000108 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000109 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +0000110 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000111 }
112}
113
Rafael Espindola35c6af32015-09-25 17:19:10 +0000114template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000115PltSection<ELFT>::PltSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000116 : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS,
117 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000118 this->Header.sh_addralign = 16;
119}
120
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000121template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000122 size_t Off = 0;
George Rimar648a2c32015-10-20 08:54:27 +0000123 bool LazyReloc = Target->supportsLazyRelocations();
124 if (LazyReloc) {
125 // First write PLT[0] entry which is special.
126 Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA());
127 Off += Target->getPltZeroEntrySize();
128 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000129 for (const SymbolBody *E : Entries) {
George Rimar648a2c32015-10-20 08:54:27 +0000130 uint64_t Got = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
131 : Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000132 uint64_t Plt = this->getVA() + Off;
George Rimar648a2c32015-10-20 08:54:27 +0000133 Target->writePltEntry(Buf + Off, Got, Plt, E->PltIndex);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000134 Off += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000135 }
136}
137
138template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000139 Sym->PltIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000140 Entries.push_back(Sym);
141}
142
143template <class ELFT>
144typename PltSection<ELFT>::uintX_t
145PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
George Rimar648a2c32015-10-20 08:54:27 +0000146 return this->getVA() + Target->getPltZeroEntrySize() +
147 B.PltIndex * Target->getPltEntrySize();
148}
149
150template <class ELFT> void PltSection<ELFT>::finalize() {
151 this->Header.sh_size = Target->getPltZeroEntrySize() +
152 Entries.size() * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000153}
154
155template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000156RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
157 : OutputSectionBase<ELFT>(Name,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000158 IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
159 llvm::ELF::SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000160 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000161 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
162 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
163}
164
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000165template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000166 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000168 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
169 Buf += EntrySize;
170
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000171 const InputSection<ELFT> &C = Rel.C;
172 const Elf_Rel &RI = Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000173 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000174 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000175 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000176 if (Body)
177 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000178
Rui Ueyama64558522015-10-16 22:51:43 +0000179 uint32_t Type = RI.getType(Config->Mips64EL);
George Rimarbc590fe2015-10-28 16:48:58 +0000180 bool NeedsCopy = Body && Target->relocNeedsCopy(Type, *Body);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000181 bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
182 bool CanBePreempted = canBePreempted(Body, NeedsGot);
George Rimar648a2c32015-10-20 08:54:27 +0000183 bool LazyReloc = Body && Target->supportsLazyRelocations() &&
184 Target->relocNeedsPlt(Type, *Body);
Rafael Espindola49757522015-10-19 19:58:18 +0000185
186 if (CanBePreempted) {
187 if (NeedsGot)
188 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
George Rimar648a2c32015-10-20 08:54:27 +0000189 LazyReloc ? Target->getPltReloc()
190 : Target->getGotReloc(),
191 Config->Mips64EL);
Rafael Espindola49757522015-10-19 19:58:18 +0000192 else
George Rimarbc590fe2015-10-28 16:48:58 +0000193 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
194 NeedsCopy ? Target->getCopyReloc() : Type,
Rafael Espindola49757522015-10-19 19:58:18 +0000195 Config->Mips64EL);
196 } else {
Rui Ueyama64558522015-10-16 22:51:43 +0000197 P->setSymbolAndType(0, Target->getRelativeReloc(), Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000198 }
199
George Rimar648a2c32015-10-20 08:54:27 +0000200 if (NeedsGot) {
201 if (LazyReloc)
202 P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
203 else
204 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimarbc590fe2015-10-28 16:48:58 +0000205 } else if (NeedsCopy) {
206 P->r_offset = Out<ELFT>::Bss->getVA() +
207 dyn_cast<SharedSymbol<ELFT>>(Body)->OffsetInBSS;
George Rimar648a2c32015-10-20 08:54:27 +0000208 } else {
Rui Ueyama55c3f892015-10-15 01:58:40 +0000209 P->r_offset = RI.r_offset + C.OutSec->getVA() + C.OutSecOff;
George Rimar648a2c32015-10-20 08:54:27 +0000210 }
Rafael Espindola49757522015-10-19 19:58:18 +0000211
Rafael Espindola932efcf2015-10-19 20:24:44 +0000212 uintX_t OrigAddend = 0;
Rafael Espindola49757522015-10-19 19:58:18 +0000213 if (IsRela && !NeedsGot)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000214 OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola49757522015-10-19 19:58:18 +0000215
Rafael Espindola932efcf2015-10-19 20:24:44 +0000216 uintX_t Addend;
George Rimarbc590fe2015-10-28 16:48:58 +0000217 if (NeedsCopy)
218 Addend = 0;
219 else if (CanBePreempted)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000220 Addend = OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000221 else if (Body)
222 Addend = getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body)) + OrigAddend;
223 else if (IsRela)
224 Addend = getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI));
225 else
226 Addend = getLocalRelTarget(File, RI);
Rafael Espindola52dca342015-10-07 00:58:20 +0000227
228 if (IsRela)
229 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230 }
231}
232
233template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000234 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000235 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
236}
237
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000238template <class ELFT>
239InterpSection<ELFT>::InterpSection()
240 : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
241 llvm::ELF::SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000242 this->Header.sh_size = Config->DynamicLinker.size() + 1;
243 this->Header.sh_addralign = 1;
244}
245
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000246template <class ELFT>
247void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
Rui Ueyama157c4332015-10-24 17:57:39 +0000248 Header.sh_name = Out<ELFT>::ShStrTab->getOffset(Name);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000249 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000250}
251
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000252template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
254}
255
Rafael Espindola35c6af32015-09-25 17:19:10 +0000256template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000257HashTableSection<ELFT>::HashTableSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000258 : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
259 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000260 this->Header.sh_entsize = sizeof(Elf_Word);
261 this->Header.sh_addralign = sizeof(Elf_Word);
262}
263
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000264static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000265 uint32_t H = 0;
266 for (char C : Name) {
267 H = (H << 4) + C;
268 uint32_t G = H & 0xf0000000;
269 if (G)
270 H ^= G >> 24;
271 H &= ~G;
272 }
273 return H;
274}
275
Rui Ueyama0db335f2015-10-07 16:58:54 +0000276template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000277 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000278
Rui Ueyama0db335f2015-10-07 16:58:54 +0000279 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000280 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000281
282 // Create as many buckets as there are symbols.
283 // FIXME: This is simplistic. We can try to optimize it, but implementing
284 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000285 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000286 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
287}
288
289template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000290 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000291 auto *P = reinterpret_cast<Elf_Word *>(Buf);
292 *P++ = NumSymbols; // nbucket
293 *P++ = NumSymbols; // nchain
294
295 Elf_Word *Buckets = P;
296 Elf_Word *Chains = P + NumSymbols;
297
Igor Kudrinf1d60292015-10-28 07:05:56 +0000298 for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) {
Igor Kudrinab665fc2015-10-20 21:47:58 +0000299 StringRef Name = Body->getName();
300 unsigned I = Body->getDynamicSymbolTableIndex();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000301 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000302 Chains[I] = Buckets[Hash];
303 Buckets[Hash] = I;
304 }
305}
306
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000307static uint32_t hashGnu(StringRef Name) {
308 uint32_t H = 5381;
309 for (uint8_t C : Name)
310 H = (H << 5) + H + C;
311 return H;
312}
313
314template <class ELFT>
315GnuHashTableSection<ELFT>::GnuHashTableSection()
316 : OutputSectionBase<ELFT>(".gnu.hash", llvm::ELF::SHT_GNU_HASH,
317 llvm::ELF::SHF_ALLOC) {
318 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
319 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
320}
321
322template <class ELFT>
323unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
324 if (!NumHashed)
325 return 0;
326
327 // These values are prime numbers which are not greater than 2^(N-1) + 1.
328 // In result, for any particular NumHashed we return a prime number
329 // which is not greater than NumHashed.
330 static const unsigned Primes[] = {
331 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
332 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
333
334 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
335 array_lengthof(Primes) - 1)];
336}
337
338// Bloom filter estimation: at least 8 bits for each hashed symbol.
339// GNU Hash table requirement: it should be a power of 2,
340// the minimum value is 1, even for an empty table.
341// Expected results for a 32-bit target:
342// calcMaskWords(0..4) = 1
343// calcMaskWords(5..8) = 2
344// calcMaskWords(9..16) = 4
345// For a 64-bit target:
346// calcMaskWords(0..8) = 1
347// calcMaskWords(9..16) = 2
348// calcMaskWords(17..32) = 4
349template <class ELFT>
350unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
351 if (!NumHashed)
352 return 1;
353 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
354}
355
356template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000357 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000358 NBuckets = calcNBuckets(NumHashed);
359 MaskWords = calcMaskWords(NumHashed);
360 // Second hash shift estimation: just predefined values.
361 Shift2 = ELFT::Is64Bits ? 6 : 5;
362
363 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
364 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
365 + sizeof(Elf_Off) * MaskWords // Bloom Filter
366 + sizeof(Elf_Word) * NBuckets // Hash Buckets
367 + sizeof(Elf_Word) * NumHashed; // Hash Values
368}
369
370template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
371 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000372 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000373 return;
374 writeBloomFilter(Buf);
375 writeHashTable(Buf);
376}
377
378template <class ELFT>
379void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
380 auto *P = reinterpret_cast<Elf_Word *>(Buf);
381 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000382 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000383 *P++ = MaskWords;
384 *P++ = Shift2;
385 Buf = reinterpret_cast<uint8_t *>(P);
386}
387
388template <class ELFT>
389void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
390 unsigned C = sizeof(Elf_Off) * 8;
391
392 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000393 for (const HashedSymbolData &Item : HashedSymbols) {
394 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
395 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
396 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000397 Masks[Pos] |= V;
398 }
399 Buf += sizeof(Elf_Off) * MaskWords;
400}
401
402template <class ELFT>
403void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
404 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
405 Elf_Word *Values = Buckets + NBuckets;
406
407 int PrevBucket = -1;
408 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000409 for (const HashedSymbolData &Item : HashedSymbols) {
410 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000411 assert(PrevBucket <= Bucket);
412 if (Bucket != PrevBucket) {
413 Buckets[Bucket] = Item.Body->getDynamicSymbolTableIndex();
414 PrevBucket = Bucket;
415 if (I > 0)
416 Values[I - 1] |= 1;
417 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000418 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000419 ++I;
420 }
421 if (I > 0)
422 Values[I - 1] |= 1;
423}
424
Rafael Espindola31f88882015-11-02 14:33:11 +0000425static bool includeInGnuHashTable(SymbolBody *B) {
426 // Assume that includeInDynamicSymtab() is already checked.
427 return !B->isUndefined();
428}
429
Rafael Espindola35c6af32015-09-25 17:19:10 +0000430template <class ELFT>
Igor Kudrinf1d60292015-10-28 07:05:56 +0000431void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) {
432 std::vector<SymbolBody *> NotHashed;
433 NotHashed.reserve(Symbols.size());
434 HashedSymbols.reserve(Symbols.size());
435 for (SymbolBody *B : Symbols) {
436 if (includeInGnuHashTable(B))
437 HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())});
438 else
439 NotHashed.push_back(B);
440 }
441 if (HashedSymbols.empty())
442 return;
443
444 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
445 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
446 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
447 return L.Hash % NBuckets < R.Hash % NBuckets;
448 });
449
450 Symbols = std::move(NotHashed);
451 for (const HashedSymbolData &Item : HashedSymbols)
452 Symbols.push_back(Item.Body);
453}
454
455template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000456DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000457 : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
458 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000459 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000460 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000461 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
462 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
463}
464
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000465template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000466 if (this->Header.sh_size)
467 return; // Already finalized.
468
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000469 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000470 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000471
472 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000473 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000474 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000475 ++NumEntries; // DT_RELASZ / DT_RELSZ
476 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000477 }
George Rimar648a2c32015-10-20 08:54:27 +0000478 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
479 ++NumEntries; // DT_JMPREL
480 ++NumEntries; // DT_PLTRELSZ
481 ++NumEntries; // DT_PLTGOT
482 ++NumEntries; // DT_PLTREL
483 }
484
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000485 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000486 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000487 ++NumEntries; // DT_STRTAB
488 ++NumEntries; // DT_STRSZ
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000489 if (Out<ELFT>::GnuHashTab)
490 ++NumEntries; // DT_GNU_HASH
491 if (Out<ELFT>::HashTab)
492 ++NumEntries; // DT_HASH
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000493
Rui Ueyama7de3f372015-10-01 19:36:04 +0000494 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000495 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000496 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000497 }
498
499 if (!Config->SoName.empty()) {
500 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000501 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000502 }
503
Rafael Espindola77572242015-10-02 19:37:55 +0000504 if (PreInitArraySec)
505 NumEntries += 2;
506 if (InitArraySec)
507 NumEntries += 2;
508 if (FiniArraySec)
509 NumEntries += 2;
510
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000511 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000512 if (!F->isNeeded())
513 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000514 Out<ELFT>::DynStrTab->add(F->getSoName());
515 ++NumEntries;
516 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000517
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000518 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
519 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
520 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
521 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000522 if (InitSym)
523 ++NumEntries; // DT_INIT
524 if (FiniSym)
525 ++NumEntries; // DT_FINI
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000526
527 if (Config->Bsymbolic)
528 DtFlags |= DF_SYMBOLIC;
529 if (Config->ZNodelete)
530 DtFlags1 |= DF_1_NODELETE;
531 if (Config->ZNow) {
532 DtFlags |= DF_BIND_NOW;
533 DtFlags1 |= DF_1_NOW;
534 }
535 if (Config->ZOrigin) {
536 DtFlags |= DF_ORIGIN;
537 DtFlags1 |= DF_1_ORIGIN;
538 }
539
540 if (DtFlags)
Davide Italiano58cbaf02015-10-19 23:32:16 +0000541 ++NumEntries; // DT_FLAGS
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000542 if (DtFlags1)
George Rimar97aad172015-10-07 15:00:21 +0000543 ++NumEntries; // DT_FLAGS_1
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000544 ++NumEntries; // DT_NULL
545
546 Header.sh_size = NumEntries * Header.sh_entsize;
547}
548
549template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000550 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
551
Rui Ueyama8c205d52015-10-02 01:33:31 +0000552 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
553 P->d_tag = Tag;
554 P->d_un.d_ptr = Val;
555 ++P;
556 };
557
558 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
559 P->d_tag = Tag;
560 P->d_un.d_val = Val;
561 ++P;
562 };
563
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000564 if (Out<ELFT>::RelaDyn->hasRelocs()) {
565 bool IsRela = Out<ELFT>::RelaDyn->isRela();
566 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
567 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000568 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
569 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000570 }
George Rimar648a2c32015-10-20 08:54:27 +0000571 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
572 WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
573 WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
574 WritePtr(DT_PLTGOT, Out<ELFT>::GotPlt->getVA());
575 WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
576 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000577
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000578 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000579 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000580 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
581 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000582 if (Out<ELFT>::GnuHashTab)
583 WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA());
584 if (Out<ELFT>::HashTab)
585 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000586
Rui Ueyama8c205d52015-10-02 01:33:31 +0000587 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000588
589 // If --enable-new-dtags is set lld emits DT_RUNPATH
590 // instead of DT_RPATH. The two tags are functionally
591 // equivalent except for the following:
592 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
593 // DT_RPATH is searched before.
594 // - DT_RUNPATH is used only to search for direct
595 // dependencies of the object it's contained in, while
596 // DT_RPATH is used for indirect dependencies as well.
597 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000598 Out<ELFT>::DynStrTab->getOffset(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000599
Rui Ueyama8c205d52015-10-02 01:33:31 +0000600 if (!Config->SoName.empty())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000601 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000602
Rafael Espindola77572242015-10-02 19:37:55 +0000603 auto WriteArray = [&](int32_t T1, int32_t T2,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000604 const OutputSectionBase<ELFT> *Sec) {
Rafael Espindola77572242015-10-02 19:37:55 +0000605 if (!Sec)
606 return;
607 WritePtr(T1, Sec->getVA());
608 WriteVal(T2, Sec->getSize());
609 };
610 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
611 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
612 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
613
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000614 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000615 if (F->isNeeded())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000616 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000617
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000618 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000619 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000620 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000621 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000622 if (DtFlags)
623 WriteVal(DT_FLAGS, DtFlags);
624 if (DtFlags1)
625 WriteVal(DT_FLAGS_1, DtFlags1);
Rui Ueyama8c205d52015-10-02 01:33:31 +0000626 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000627}
628
629template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000630OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000631 uintX_t sh_flags)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000632 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000633
634template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000635void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
636 Sections.push_back(C);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000637 C->OutSec = this;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000638 uint32_t Align = C->getAlign();
639 if (Align > this->Header.sh_addralign)
640 this->Header.sh_addralign = Align;
641
642 uintX_t Off = this->Header.sh_size;
643 Off = RoundUpToAlignment(Off, Align);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000644 C->OutSecOff = Off;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000645 Off += C->getSize();
646 this->Header.sh_size = Off;
647}
648
649template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000650typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000651 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000652 case SymbolBody::DefinedSyntheticKind: {
653 auto &D = cast<DefinedSynthetic<ELFT>>(S);
654 return D.Section.getVA() + D.Sym.st_value;
655 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000656 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000657 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000658 case SymbolBody::DefinedRegularKind: {
659 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindola48225b42015-10-23 19:55:11 +0000660 InputSectionBase<ELFT> &SC = DR.Section;
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000661 if (DR.Sym.getType() == STT_TLS)
662 return SC.OutSec->getVA() + SC.getOffset(DR.Sym) -
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000663 Out<ELFT>::TlsPhdr->p_vaddr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000664 return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
Rafael Espindolacd076f02015-09-25 18:19:03 +0000665 }
666 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000667 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
George Rimarbc590fe2015-10-28 16:48:58 +0000668 case SymbolBody::SharedKind: {
669 auto &SS = cast<SharedSymbol<ELFT>>(S);
Rafael Espindola9b896082015-11-03 14:34:11 +0000670 if (SS.needsCopy())
George Rimarbc590fe2015-10-28 16:48:58 +0000671 return Out<ELFT>::Bss->getVA() + SS.OffsetInBSS;
672 return 0;
673 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000674 case SymbolBody::UndefinedKind:
675 return 0;
676 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000677 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
678 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000679 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000680 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000681}
682
Rui Ueyama34f29242015-10-13 19:51:57 +0000683// Returns a VA which a relocatin RI refers to. Used only for local symbols.
684// For non-local symbols, use getSymVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000685template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000686typename ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000687lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
Rafael Espindola932efcf2015-10-19 20:24:44 +0000688 const Elf_Rel_Impl<ELFT, IsRela> &RI) {
689 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
690 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
691
692 uintX_t Addend = getAddend<ELFT>(RI);
693
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000694 // PPC64 has a special relocation representing the TOC base pointer
695 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000696 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000697 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000698
Rui Ueyama126d08f2015-10-12 20:28:22 +0000699 const Elf_Sym *Sym =
700 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000701
Rui Ueyama126d08f2015-10-12 20:28:22 +0000702 if (!Sym)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000703 error("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000704
Rafael Espindola444576d2015-10-09 19:25:07 +0000705 // According to the ELF spec reference to a local symbol from outside
706 // the group are not allowed. Unfortunately .eh_frame breaks that rule
707 // and must be treated specially. For now we just replace the symbol with
708 // 0.
Rafael Espindolac159c962015-10-19 21:00:02 +0000709 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000710 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000711 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000712
Rafael Espindolac159c962015-10-19 21:00:02 +0000713 uintX_t VA = Section->OutSec->getVA();
714 if (isa<InputSection<ELFT>>(Section))
715 return VA + Section->getOffset(*Sym) + Addend;
716
717 uintX_t Offset = Sym->st_value;
718 if (Sym->getType() == STT_SECTION) {
719 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000720 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000721 }
722 return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
723 Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000724}
725
Rui Ueyama34f29242015-10-13 19:51:57 +0000726// Returns true if a symbol can be replaced at load-time by a symbol
727// with the same name defined in other ELF executable or DSO.
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000728bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000729 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000730 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000731 if (Body->isShared())
732 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000733
734 if (Body->isUndefined()) {
735 if (!Body->isWeak())
736 return true;
737
738 // This is an horrible corner case. Ideally we would like to say that any
739 // undefined symbol can be preempted so that the dynamic linker has a
740 // chance of finding it at runtime.
741 //
742 // The problem is that the code sequence used to test for weak undef
743 // functions looks like
744 // if (func) func()
745 // If the code is -fPIC the first reference is a load from the got and
746 // everything works.
747 // If the code is not -fPIC there is no reasonable way to solve it:
748 // * A relocation writing to the text segment will fail (it is ro).
749 // * A copy relocation doesn't work for functions.
750 // * The trick of using a plt entry as the address would fail here since
751 // the plt entry would have a non zero address.
752 // Since we cannot do anything better, we just resolve the symbol to 0 and
753 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000754 //
755 // As an extra hack, assume that if we are producing a shared library the
756 // user knows what he or she is doing and can handle a dynamic relocation.
757 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000758 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000759 if (!Config->Shared)
760 return false;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000761 return Body->getVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000762}
763
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000764template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000765 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000766 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000767}
768
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000769template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000770MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
771 uintX_t sh_flags)
772 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
773
774template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000775 if (shouldTailMerge()) {
776 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +0000777 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000778 return;
Rafael Espindolac159c962015-10-19 21:00:02 +0000779 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000780 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
781 StringRef Data = P.first;
782 memcpy(Buf + P.second, Data.data(), Data.size());
783 }
784}
785
786static size_t findNull(StringRef S, size_t EntSize) {
787 // Optimize the common case.
788 if (EntSize == 1)
789 return S.find(0);
790
791 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
792 const char *B = S.begin() + I;
793 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
794 return I;
795 }
796 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +0000797}
798
799template <class ELFT>
800void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
801 S->OutSec = this;
802 uint32_t Align = S->getAlign();
803 if (Align > this->Header.sh_addralign)
804 this->Header.sh_addralign = Align;
805
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000806 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +0000807 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +0000808 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000809 uintX_t Offset = 0;
810
811 if (this->Header.sh_flags & SHF_STRINGS) {
812 while (!Data.empty()) {
813 size_t End = findNull(Data, EntSize);
814 if (End == StringRef::npos)
815 error("String is not null terminated");
816 StringRef Entry = Data.substr(0, End + EntSize);
817 size_t OutputOffset = Builder.add(Entry);
818 if (shouldTailMerge())
819 OutputOffset = -1;
820 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
821 uintX_t Size = End + EntSize;
822 Data = Data.substr(Size);
823 Offset += Size;
824 }
825 } else {
826 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
827 StringRef Entry = Data.substr(I, EntSize);
828 size_t OutputOffset = Builder.add(Entry);
829 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
830 Offset += EntSize;
831 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000832 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000833}
834
835template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000836unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
837 return Builder.getOffset(Val);
838}
839
840template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
841 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
842}
843
844template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
845 if (shouldTailMerge())
846 Builder.finalize();
847 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +0000848}
849
850template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +0000851StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
852 : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000853 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +0000854 Dynamic(Dynamic) {
855 this->Header.sh_addralign = 1;
856}
857
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000858template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000859 StringRef Data = StrTabBuilder.data();
860 memcpy(Buf, Data.data(), Data.size());
861}
862
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000863template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000864 if (!B.isUsedInRegularObj())
865 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000866
867 // Don't include synthetic symbols like __init_array_start in every output.
868 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
869 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
870 return false;
871
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000872 return true;
873}
874
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000875bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000876 uint8_t V = B.getVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000877 if (V != STV_DEFAULT && V != STV_PROTECTED)
878 return false;
879
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000880 if (Config->ExportDynamic || Config->Shared)
881 return true;
882 return B.isUsedInDynamicReloc();
883}
884
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000885template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +0000886bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
887 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000888 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
889 if (Sym.getType() == STT_SECTION)
890 return false;
891
Rafael Espindola444576d2015-10-09 19:25:07 +0000892 // If sym references a section in a discarded group, don't keep it.
Rafael Espindola4cda5812015-10-16 15:29:48 +0000893 if (File.getSection(Sym) == &InputSection<ELFT>::Discarded)
894 return false;
Rafael Espindola444576d2015-10-09 19:25:07 +0000895
Davide Italiano6993ba42015-09-26 00:47:56 +0000896 if (Config->DiscardNone)
897 return true;
898
899 // ELF defines dynamic locals as symbols which name starts with ".L".
900 return !(Config->DiscardLocals && SymName.startswith(".L"));
901}
902
Rafael Espindola35c6af32015-09-25 17:19:10 +0000903template <class ELFT>
904SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000905 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
906 : OutputSectionBase<ELFT>(
Rafael Espindola35c6af32015-09-25 17:19:10 +0000907 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
908 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
909 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000910 Table(Table), StrTabSec(StrTabSec) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000911 typedef OutputSectionBase<ELFT> Base;
912 typename Base::Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000913
914 Header.sh_entsize = sizeof(Elf_Sym);
915 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
916}
917
Rui Ueyama0db335f2015-10-07 16:58:54 +0000918template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000919 if (this->Header.sh_size)
920 return; // Already finalized.
921
Rui Ueyama0db335f2015-10-07 16:58:54 +0000922 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000923 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000924 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000925
926 if (!StrTabSec.isDynamic()) {
927 std::stable_sort(Symbols.begin(), Symbols.end(),
Igor Kudrinf1d60292015-10-28 07:05:56 +0000928 [](SymbolBody *L, SymbolBody *R) {
929 return getSymbolBinding(L) == STB_LOCAL &&
930 getSymbolBinding(R) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000931 });
932 return;
933 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000934 if (Out<ELFT>::GnuHashTab)
935 // NB: It also sorts Symbols to meet the GNU hash table requirements.
936 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +0000937 size_t I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000938 for (SymbolBody *B : Symbols)
939 B->setDynamicSymbolTableIndex(++I);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000940}
941
942template <class ELFT>
Igor Kudrinab665fc2015-10-20 21:47:58 +0000943void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
Rui Ueyama0db335f2015-10-07 16:58:54 +0000944 StrTabSec.add(Name);
945 ++NumVisible;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000946 ++NumLocals;
947}
948
949template <class ELFT>
950void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
951 StrTabSec.add(Body->getName());
Igor Kudrinf1d60292015-10-28 07:05:56 +0000952 Symbols.push_back(Body);
Igor Kudrinab665fc2015-10-20 21:47:58 +0000953 ++NumVisible;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000954}
955
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000956template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000957 Buf += sizeof(Elf_Sym);
958
959 // All symbols with STB_LOCAL binding precede the weak and global symbols.
960 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000961 if (!Config->DiscardAll && !StrTabSec.isDynamic())
962 writeLocalSymbols(Buf);
963
964 writeGlobalSymbols(Buf);
965}
966
967template <class ELFT>
968void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
969 // Iterate over all input object files to copy their local symbols
970 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000971 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
972 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000973 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000974 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +0000975 error(SymNameOrErr);
976 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000977 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000978 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +0000979
Rui Ueyamac55733e2015-09-30 00:54:29 +0000980 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +0000981 uintX_t VA = 0;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000982 if (Sym.st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000983 ESym->st_shndx = SHN_ABS;
Rafael Espindolac159c962015-10-19 21:00:02 +0000984 VA = Sym.st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000985 } else {
Rafael Espindola48225b42015-10-23 19:55:11 +0000986 InputSectionBase<ELFT> *Section = File->getSection(Sym);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000987 if (!Section->isLive())
988 continue;
Rafael Espindolac159c962015-10-19 21:00:02 +0000989 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
990 ESym->st_shndx = OutSec->SectionIndex;
991 VA += OutSec->getVA() + Section->getOffset(Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000992 }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000993 ESym->st_name = StrTabSec.getOffset(SymName);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000994 ESym->st_size = Sym.st_size;
995 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000996 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000997 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000998 }
999 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001000}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001001
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001002template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001003void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001004 // Write the internal symbol table contents to the output symbol table
1005 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001006 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +00001007 for (SymbolBody *Body : Symbols) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001008 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001009
Rafael Espindola8614c562015-10-06 14:33:58 +00001010 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001011 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001012 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001013 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001014 case SymbolBody::DefinedRegularKind: {
1015 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
1016 if (!Sym->Section.isLive())
1017 continue;
1018 OutSec = Sym->Section.OutSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001019 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001020 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001021 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001022 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001023 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001024 case SymbolBody::SharedKind: {
Rafael Espindola9b896082015-11-03 14:34:11 +00001025 if (cast<SharedSymbol<ELFT>>(Body)->needsCopy())
George Rimarbc590fe2015-10-28 16:48:58 +00001026 OutSec = Out<ELFT>::Bss;
1027 break;
1028 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001029 case SymbolBody::UndefinedKind:
1030 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001031 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001032 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001033 }
1034
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001035 StringRef Name = Body->getName();
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001036 ESym->st_name = StrTabSec.getOffset(Name);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001037
Rafael Espindola8614c562015-10-06 14:33:58 +00001038 unsigned char Type = STT_NOTYPE;
1039 uintX_t Size = 0;
1040 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
1041 const Elf_Sym &InputSym = EBody->Sym;
Rafael Espindola8614c562015-10-06 14:33:58 +00001042 Type = InputSym.getType();
1043 Size = InputSym.st_size;
1044 }
1045
Igor Kudrin853b88d2015-10-20 20:52:14 +00001046 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001047 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001048 ESym->setVisibility(Body->getVisibility());
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001049 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001050
Rafael Espindola8614c562015-10-06 14:33:58 +00001051 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +00001052 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +00001053 else if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001054 ESym->st_shndx = OutSec->SectionIndex;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001055
1056 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001057 }
1058}
1059
Igor Kudrin853b88d2015-10-20 20:52:14 +00001060template <class ELFT>
1061uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001062 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001063 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1064 return STB_LOCAL;
1065 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body))
1066 return EBody->Sym.getBinding();
1067 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1068}
1069
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001070namespace lld {
1071namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001072template class OutputSectionBase<ELF32LE>;
1073template class OutputSectionBase<ELF32BE>;
1074template class OutputSectionBase<ELF64LE>;
1075template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001076
George Rimar648a2c32015-10-20 08:54:27 +00001077template class GotPltSection<ELF32LE>;
1078template class GotPltSection<ELF32BE>;
1079template class GotPltSection<ELF64LE>;
1080template class GotPltSection<ELF64BE>;
1081
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001082template class GotSection<ELF32LE>;
1083template class GotSection<ELF32BE>;
1084template class GotSection<ELF64LE>;
1085template class GotSection<ELF64BE>;
1086
1087template class PltSection<ELF32LE>;
1088template class PltSection<ELF32BE>;
1089template class PltSection<ELF64LE>;
1090template class PltSection<ELF64BE>;
1091
1092template class RelocationSection<ELF32LE>;
1093template class RelocationSection<ELF32BE>;
1094template class RelocationSection<ELF64LE>;
1095template class RelocationSection<ELF64BE>;
1096
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001097template class InterpSection<ELF32LE>;
1098template class InterpSection<ELF32BE>;
1099template class InterpSection<ELF64LE>;
1100template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001101
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001102template class GnuHashTableSection<ELF32LE>;
1103template class GnuHashTableSection<ELF32BE>;
1104template class GnuHashTableSection<ELF64LE>;
1105template class GnuHashTableSection<ELF64BE>;
1106
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001107template class HashTableSection<ELF32LE>;
1108template class HashTableSection<ELF32BE>;
1109template class HashTableSection<ELF64LE>;
1110template class HashTableSection<ELF64BE>;
1111
1112template class DynamicSection<ELF32LE>;
1113template class DynamicSection<ELF32BE>;
1114template class DynamicSection<ELF64LE>;
1115template class DynamicSection<ELF64BE>;
1116
1117template class OutputSection<ELF32LE>;
1118template class OutputSection<ELF32BE>;
1119template class OutputSection<ELF64LE>;
1120template class OutputSection<ELF64BE>;
1121
Rafael Espindolac159c962015-10-19 21:00:02 +00001122template class MergeOutputSection<ELF32LE>;
1123template class MergeOutputSection<ELF32BE>;
1124template class MergeOutputSection<ELF64LE>;
1125template class MergeOutputSection<ELF64BE>;
1126
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001127template class StringTableSection<ELF32LE>;
1128template class StringTableSection<ELF32BE>;
1129template class StringTableSection<ELF64LE>;
1130template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001131
1132template class SymbolTableSection<ELF32LE>;
1133template class SymbolTableSection<ELF32BE>;
1134template class SymbolTableSection<ELF64LE>;
1135template class SymbolTableSection<ELF64BE>;
1136
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001137template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1138template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1139template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1140template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +00001141
Rafael Espindola56f965f2015-09-21 22:48:12 +00001142template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001143getLocalRelTarget(const ObjectFile<ELF32LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001144 const ELFFile<ELF32LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001145template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001146getLocalRelTarget(const ObjectFile<ELF32BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001147 const ELFFile<ELF32BE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001148template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001149getLocalRelTarget(const ObjectFile<ELF64LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001150 const ELFFile<ELF64LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001151template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001152getLocalRelTarget(const ObjectFile<ELF64BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001153 const ELFFile<ELF64BE>::Elf_Rel &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001154
Rafael Espindolac159c962015-10-19 21:00:02 +00001155template ELFFile<ELF32LE>::uintX_t
1156getLocalRelTarget(const ObjectFile<ELF32LE> &,
1157 const ELFFile<ELF32LE>::Elf_Rela &);
1158template ELFFile<ELF32BE>::uintX_t
1159getLocalRelTarget(const ObjectFile<ELF32BE> &,
1160 const ELFFile<ELF32BE>::Elf_Rela &);
1161template ELFFile<ELF64LE>::uintX_t
1162getLocalRelTarget(const ObjectFile<ELF64LE> &,
1163 const ELFFile<ELF64LE>::Elf_Rela &);
1164template ELFFile<ELF64BE>::uintX_t
1165getLocalRelTarget(const ObjectFile<ELF64BE> &,
1166 const ELFFile<ELF64BE>::Elf_Rela &);
1167
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001168template bool includeInSymtab<ELF32LE>(const SymbolBody &);
1169template bool includeInSymtab<ELF32BE>(const SymbolBody &);
1170template bool includeInSymtab<ELF64LE>(const SymbolBody &);
1171template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001172
Rafael Espindola444576d2015-10-09 19:25:07 +00001173template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1174 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001175 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001176template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1177 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001178 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001179template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1180 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001181 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001182template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1183 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001184 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001185}
1186}