blob: 70d458b08292973ee416cfef86b27fb8e309ab7c [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);
George Rimar687138c2015-11-13 16:28:53 +000083}
84
85template <class ELFT> void GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) {
86 Sym->GotIndex = Target->getGotHeaderEntriesNum() + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +000087 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +000088 Entries.push_back(Sym);
89 Entries.push_back(nullptr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000090}
91
Michael J. Spencer1e225612015-11-11 01:00:24 +000092template <class ELFT> uint32_t GotSection<ELFT>::addLocalModuleTlsIndex() {
93 Entries.push_back(nullptr);
94 Entries.push_back(nullptr);
95 return (Entries.size() - 2) * sizeof(uintX_t);
96}
97
Rafael Espindola5805c4f2015-09-21 21:38:08 +000098template <class ELFT>
99typename GotSection<ELFT>::uintX_t
100GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama5f551ae2015-10-14 14:02:06 +0000101 return this->getVA() + B.GotIndex * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000102}
103
Igor Kudrin304860a2015-11-12 04:39:49 +0000104template <class ELFT>
105const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
106 return Entries.empty() ? nullptr : Entries.front();
107}
108
109template <class ELFT>
110unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
111 // TODO: Update when the suppoort of GOT entries for local symbols is added.
112 return Target->getGotHeaderEntriesNum();
113}
114
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000115template <class ELFT> void GotSection<ELFT>::finalize() {
116 this->Header.sh_size =
117 (Target->getGotHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
118}
119
Rafael Espindolaa6627382015-10-06 23:56:53 +0000120template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000121 Target->writeGotHeaderEntries(Buf);
122 Buf += Target->getGotHeaderEntriesNum() * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000123 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000124 uint8_t *Entry = Buf;
125 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000126 if (!B)
127 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000128 // MIPS has special rules to fill up GOT entries.
129 // See "Global Offset Table" in Chapter 5 in the following document
130 // for detailed description:
131 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
132 // As the first approach, we can just store addresses for all symbols.
133 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000134 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000135 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +0000136 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000137 }
138}
139
Rafael Espindola35c6af32015-09-25 17:19:10 +0000140template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000141PltSection<ELFT>::PltSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000142 : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS,
143 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000144 this->Header.sh_addralign = 16;
145}
146
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000147template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000148 size_t Off = 0;
George Rimar648a2c32015-10-20 08:54:27 +0000149 bool LazyReloc = Target->supportsLazyRelocations();
150 if (LazyReloc) {
151 // First write PLT[0] entry which is special.
152 Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA());
153 Off += Target->getPltZeroEntrySize();
154 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000155 for (const SymbolBody *E : Entries) {
George Rimar648a2c32015-10-20 08:54:27 +0000156 uint64_t Got = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
157 : Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000158 uint64_t Plt = this->getVA() + Off;
George Rimar648a2c32015-10-20 08:54:27 +0000159 Target->writePltEntry(Buf + Off, Got, Plt, E->PltIndex);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000160 Off += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000161 }
162}
163
164template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000165 Sym->PltIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000166 Entries.push_back(Sym);
167}
168
169template <class ELFT>
170typename PltSection<ELFT>::uintX_t
171PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
George Rimar648a2c32015-10-20 08:54:27 +0000172 return this->getVA() + Target->getPltZeroEntrySize() +
173 B.PltIndex * Target->getPltEntrySize();
174}
175
176template <class ELFT> void PltSection<ELFT>::finalize() {
177 this->Header.sh_size = Target->getPltZeroEntrySize() +
178 Entries.size() * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000179}
180
181template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000182RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
183 : OutputSectionBase<ELFT>(Name,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000184 IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
185 llvm::ELF::SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000186 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000187 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
188 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
189}
190
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000191template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000192 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000194 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
195 Buf += EntrySize;
196
Michael J. Spencer627ae702015-11-13 00:28:34 +0000197 // Skip placeholder for global dynamic TLS relocation pair. It was already
198 // handled by the previous relocation.
199 if (!Rel.C || !Rel.RI)
200 continue;
201
202 InputSectionBase<ELFT> &C = *Rel.C;
203 const Elf_Rel &RI = *Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000204 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000205 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000206 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000207 if (Body)
208 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000209
Rui Ueyama64558522015-10-16 22:51:43 +0000210 uint32_t Type = RI.getType(Config->Mips64EL);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000211
Michael J. Spencerecd7f372015-11-13 00:32:58 +0000212 if (Target->isTlsLocalDynamicReloc(Type)) {
Michael J. Spencer1e225612015-11-11 01:00:24 +0000213 P->setSymbolAndType(0, Target->getTlsModuleIndexReloc(),
214 Config->Mips64EL);
215 P->r_offset =
216 Out<ELFT>::Got->getVA() + Out<ELFT>::LocalModuleTlsIndexOffset;
217 continue;
218 }
219
Michael J. Spencerecd7f372015-11-13 00:32:58 +0000220 if (Body && Target->isTlsGlobalDynamicReloc(Type)) {
Michael J. Spencer627ae702015-11-13 00:28:34 +0000221 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
222 Target->getTlsModuleIndexReloc(), Config->Mips64EL);
223 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
224 auto *PNext = reinterpret_cast<Elf_Rel *>(Buf);
225 PNext->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
226 Target->getTlsOffsetReloc(), Config->Mips64EL);
227 PNext->r_offset = Out<ELFT>::Got->getEntryAddr(*Body) + sizeof(uintX_t);
228 continue;
229 }
230
George Rimarbc590fe2015-10-28 16:48:58 +0000231 bool NeedsCopy = Body && Target->relocNeedsCopy(Type, *Body);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000232 bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
233 bool CanBePreempted = canBePreempted(Body, NeedsGot);
George Rimar648a2c32015-10-20 08:54:27 +0000234 bool LazyReloc = Body && Target->supportsLazyRelocations() &&
235 Target->relocNeedsPlt(Type, *Body);
Rafael Espindola49757522015-10-19 19:58:18 +0000236
237 if (CanBePreempted) {
George Rimar687138c2015-11-13 16:28:53 +0000238 unsigned GotReloc =
239 Body->isTLS() ? Target->getTlsGotReloc() : Target->getGotReloc();
Rafael Espindola49757522015-10-19 19:58:18 +0000240 if (NeedsGot)
241 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
George Rimar687138c2015-11-13 16:28:53 +0000242 LazyReloc ? Target->getPltReloc() : GotReloc,
George Rimar648a2c32015-10-20 08:54:27 +0000243 Config->Mips64EL);
Rafael Espindola49757522015-10-19 19:58:18 +0000244 else
George Rimarbc590fe2015-10-28 16:48:58 +0000245 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
246 NeedsCopy ? Target->getCopyReloc() : Type,
Rafael Espindola49757522015-10-19 19:58:18 +0000247 Config->Mips64EL);
248 } else {
Rui Ueyama64558522015-10-16 22:51:43 +0000249 P->setSymbolAndType(0, Target->getRelativeReloc(), Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000250 }
251
George Rimar648a2c32015-10-20 08:54:27 +0000252 if (NeedsGot) {
253 if (LazyReloc)
254 P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
255 else
256 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimarbc590fe2015-10-28 16:48:58 +0000257 } else if (NeedsCopy) {
258 P->r_offset = Out<ELFT>::Bss->getVA() +
259 dyn_cast<SharedSymbol<ELFT>>(Body)->OffsetInBSS;
George Rimar648a2c32015-10-20 08:54:27 +0000260 } else {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000261 P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA();
George Rimar648a2c32015-10-20 08:54:27 +0000262 }
Rafael Espindola49757522015-10-19 19:58:18 +0000263
Rafael Espindola932efcf2015-10-19 20:24:44 +0000264 uintX_t OrigAddend = 0;
Rafael Espindola49757522015-10-19 19:58:18 +0000265 if (IsRela && !NeedsGot)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000266 OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola49757522015-10-19 19:58:18 +0000267
Rafael Espindola932efcf2015-10-19 20:24:44 +0000268 uintX_t Addend;
George Rimarbc590fe2015-10-28 16:48:58 +0000269 if (NeedsCopy)
270 Addend = 0;
271 else if (CanBePreempted)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000272 Addend = OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000273 else if (Body)
274 Addend = getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body)) + OrigAddend;
275 else if (IsRela)
276 Addend = getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI));
277 else
278 Addend = getLocalRelTarget(File, RI);
Rafael Espindola52dca342015-10-07 00:58:20 +0000279
280 if (IsRela)
281 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000282 }
283}
284
285template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000286 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000287 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
288}
289
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000290template <class ELFT>
291InterpSection<ELFT>::InterpSection()
292 : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
293 llvm::ELF::SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000294 this->Header.sh_size = Config->DynamicLinker.size() + 1;
295 this->Header.sh_addralign = 1;
296}
297
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000298template <class ELFT>
299void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
Rui Ueyama157c4332015-10-24 17:57:39 +0000300 Header.sh_name = Out<ELFT>::ShStrTab->getOffset(Name);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000301 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000302}
303
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000304template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000305 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
306}
307
Rafael Espindola35c6af32015-09-25 17:19:10 +0000308template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000309HashTableSection<ELFT>::HashTableSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000310 : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
311 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000312 this->Header.sh_entsize = sizeof(Elf_Word);
313 this->Header.sh_addralign = sizeof(Elf_Word);
314}
315
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000316static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000317 uint32_t H = 0;
318 for (char C : Name) {
319 H = (H << 4) + C;
320 uint32_t G = H & 0xf0000000;
321 if (G)
322 H ^= G >> 24;
323 H &= ~G;
324 }
325 return H;
326}
327
Rui Ueyama0db335f2015-10-07 16:58:54 +0000328template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000329 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000330
Rui Ueyama0db335f2015-10-07 16:58:54 +0000331 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000332 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000333
334 // Create as many buckets as there are symbols.
335 // FIXME: This is simplistic. We can try to optimize it, but implementing
336 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000337 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000338 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
339}
340
341template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000342 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000343 auto *P = reinterpret_cast<Elf_Word *>(Buf);
344 *P++ = NumSymbols; // nbucket
345 *P++ = NumSymbols; // nchain
346
347 Elf_Word *Buckets = P;
348 Elf_Word *Chains = P + NumSymbols;
349
Igor Kudrinf1d60292015-10-28 07:05:56 +0000350 for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) {
Igor Kudrinab665fc2015-10-20 21:47:58 +0000351 StringRef Name = Body->getName();
352 unsigned I = Body->getDynamicSymbolTableIndex();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000353 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000354 Chains[I] = Buckets[Hash];
355 Buckets[Hash] = I;
356 }
357}
358
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000359static uint32_t hashGnu(StringRef Name) {
360 uint32_t H = 5381;
361 for (uint8_t C : Name)
362 H = (H << 5) + H + C;
363 return H;
364}
365
366template <class ELFT>
367GnuHashTableSection<ELFT>::GnuHashTableSection()
368 : OutputSectionBase<ELFT>(".gnu.hash", llvm::ELF::SHT_GNU_HASH,
369 llvm::ELF::SHF_ALLOC) {
370 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
371 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
372}
373
374template <class ELFT>
375unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
376 if (!NumHashed)
377 return 0;
378
379 // These values are prime numbers which are not greater than 2^(N-1) + 1.
380 // In result, for any particular NumHashed we return a prime number
381 // which is not greater than NumHashed.
382 static const unsigned Primes[] = {
383 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
384 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
385
386 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
387 array_lengthof(Primes) - 1)];
388}
389
390// Bloom filter estimation: at least 8 bits for each hashed symbol.
391// GNU Hash table requirement: it should be a power of 2,
392// the minimum value is 1, even for an empty table.
393// Expected results for a 32-bit target:
394// calcMaskWords(0..4) = 1
395// calcMaskWords(5..8) = 2
396// calcMaskWords(9..16) = 4
397// For a 64-bit target:
398// calcMaskWords(0..8) = 1
399// calcMaskWords(9..16) = 2
400// calcMaskWords(17..32) = 4
401template <class ELFT>
402unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
403 if (!NumHashed)
404 return 1;
405 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
406}
407
408template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000409 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000410 NBuckets = calcNBuckets(NumHashed);
411 MaskWords = calcMaskWords(NumHashed);
412 // Second hash shift estimation: just predefined values.
413 Shift2 = ELFT::Is64Bits ? 6 : 5;
414
415 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
416 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
417 + sizeof(Elf_Off) * MaskWords // Bloom Filter
418 + sizeof(Elf_Word) * NBuckets // Hash Buckets
419 + sizeof(Elf_Word) * NumHashed; // Hash Values
420}
421
422template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
423 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000424 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000425 return;
426 writeBloomFilter(Buf);
427 writeHashTable(Buf);
428}
429
430template <class ELFT>
431void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
432 auto *P = reinterpret_cast<Elf_Word *>(Buf);
433 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000434 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000435 *P++ = MaskWords;
436 *P++ = Shift2;
437 Buf = reinterpret_cast<uint8_t *>(P);
438}
439
440template <class ELFT>
441void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
442 unsigned C = sizeof(Elf_Off) * 8;
443
444 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000445 for (const HashedSymbolData &Item : HashedSymbols) {
446 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
447 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
448 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000449 Masks[Pos] |= V;
450 }
451 Buf += sizeof(Elf_Off) * MaskWords;
452}
453
454template <class ELFT>
455void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
456 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
457 Elf_Word *Values = Buckets + NBuckets;
458
459 int PrevBucket = -1;
460 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000461 for (const HashedSymbolData &Item : HashedSymbols) {
462 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000463 assert(PrevBucket <= Bucket);
464 if (Bucket != PrevBucket) {
465 Buckets[Bucket] = Item.Body->getDynamicSymbolTableIndex();
466 PrevBucket = Bucket;
467 if (I > 0)
468 Values[I - 1] |= 1;
469 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000470 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000471 ++I;
472 }
473 if (I > 0)
474 Values[I - 1] |= 1;
475}
476
Rafael Espindola31f88882015-11-02 14:33:11 +0000477static bool includeInGnuHashTable(SymbolBody *B) {
478 // Assume that includeInDynamicSymtab() is already checked.
479 return !B->isUndefined();
480}
481
Rafael Espindola35c6af32015-09-25 17:19:10 +0000482template <class ELFT>
Igor Kudrinf1d60292015-10-28 07:05:56 +0000483void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) {
484 std::vector<SymbolBody *> NotHashed;
485 NotHashed.reserve(Symbols.size());
486 HashedSymbols.reserve(Symbols.size());
487 for (SymbolBody *B : Symbols) {
488 if (includeInGnuHashTable(B))
489 HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())});
490 else
491 NotHashed.push_back(B);
492 }
493 if (HashedSymbols.empty())
494 return;
495
496 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
497 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
498 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
499 return L.Hash % NBuckets < R.Hash % NBuckets;
500 });
501
502 Symbols = std::move(NotHashed);
503 for (const HashedSymbolData &Item : HashedSymbols)
504 Symbols.push_back(Item.Body);
505}
506
507template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000508DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000509 : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
510 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000511 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000512 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000513 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
514 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000515
516 // .dynamic section is not writable on MIPS.
517 // See "Special Section" in Chapter 4 in the following document:
518 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
519 if (Config->EMachine == EM_MIPS)
520 Header.sh_flags = llvm::ELF::SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000521}
522
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000523template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000524 if (this->Header.sh_size)
525 return; // Already finalized.
526
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000527 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000528 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000529
530 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000531 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000532 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000533 ++NumEntries; // DT_RELASZ / DT_RELSZ
534 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000535 }
George Rimar648a2c32015-10-20 08:54:27 +0000536 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
537 ++NumEntries; // DT_JMPREL
538 ++NumEntries; // DT_PLTRELSZ
Igor Kudrin304860a2015-11-12 04:39:49 +0000539 ++NumEntries; // DT_PLTGOT / DT_MIPS_PLTGOT
George Rimar648a2c32015-10-20 08:54:27 +0000540 ++NumEntries; // DT_PLTREL
541 }
542
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000543 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000544 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000545 ++NumEntries; // DT_STRTAB
546 ++NumEntries; // DT_STRSZ
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000547 if (Out<ELFT>::GnuHashTab)
548 ++NumEntries; // DT_GNU_HASH
549 if (Out<ELFT>::HashTab)
550 ++NumEntries; // DT_HASH
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000551
Rui Ueyama7de3f372015-10-01 19:36:04 +0000552 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000553 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000554 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000555 }
556
557 if (!Config->SoName.empty()) {
558 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000559 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000560 }
561
Rafael Espindola77572242015-10-02 19:37:55 +0000562 if (PreInitArraySec)
563 NumEntries += 2;
564 if (InitArraySec)
565 NumEntries += 2;
566 if (FiniArraySec)
567 NumEntries += 2;
568
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000569 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000570 if (!F->isNeeded())
571 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000572 Out<ELFT>::DynStrTab->add(F->getSoName());
573 ++NumEntries;
574 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000575
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000576 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
577 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
578 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
579 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000580 if (InitSym)
581 ++NumEntries; // DT_INIT
582 if (FiniSym)
583 ++NumEntries; // DT_FINI
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000584
585 if (Config->Bsymbolic)
586 DtFlags |= DF_SYMBOLIC;
587 if (Config->ZNodelete)
588 DtFlags1 |= DF_1_NODELETE;
589 if (Config->ZNow) {
590 DtFlags |= DF_BIND_NOW;
591 DtFlags1 |= DF_1_NOW;
592 }
593 if (Config->ZOrigin) {
594 DtFlags |= DF_ORIGIN;
595 DtFlags1 |= DF_1_ORIGIN;
596 }
597
598 if (DtFlags)
Davide Italiano58cbaf02015-10-19 23:32:16 +0000599 ++NumEntries; // DT_FLAGS
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000600 if (DtFlags1)
George Rimar97aad172015-10-07 15:00:21 +0000601 ++NumEntries; // DT_FLAGS_1
Igor Kudrin304860a2015-11-12 04:39:49 +0000602
603 if (Config->EMachine == EM_MIPS) {
604 ++NumEntries; // DT_MIPS_RLD_VERSION
605 ++NumEntries; // DT_MIPS_FLAGS
606 ++NumEntries; // DT_MIPS_BASE_ADDRESS
607 ++NumEntries; // DT_MIPS_SYMTABNO
608 ++NumEntries; // DT_MIPS_LOCAL_GOTNO
609 ++NumEntries; // DT_MIPS_GOTSYM;
610 ++NumEntries; // DT_PLTGOT
611 if (Out<ELFT>::MipsRldMap)
612 ++NumEntries; // DT_MIPS_RLD_MAP
613 }
614
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000615 ++NumEntries; // DT_NULL
616
617 Header.sh_size = NumEntries * Header.sh_entsize;
618}
619
620template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000621 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
622
Rui Ueyama8c205d52015-10-02 01:33:31 +0000623 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
624 P->d_tag = Tag;
625 P->d_un.d_ptr = Val;
626 ++P;
627 };
628
629 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
630 P->d_tag = Tag;
631 P->d_un.d_val = Val;
632 ++P;
633 };
634
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000635 if (Out<ELFT>::RelaDyn->hasRelocs()) {
636 bool IsRela = Out<ELFT>::RelaDyn->isRela();
637 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
638 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000639 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
640 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000641 }
George Rimar648a2c32015-10-20 08:54:27 +0000642 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
643 WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
644 WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
Igor Kudrin304860a2015-11-12 04:39:49 +0000645 // On MIPS, the address of the .got.plt section is stored in
646 // the DT_MIPS_PLTGOT entry because the DT_PLTGOT entry points to
647 // the .got section. See "Dynamic Section" in the following document:
648 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
649 WritePtr((Config->EMachine == EM_MIPS) ? DT_MIPS_PLTGOT : DT_PLTGOT,
650 Out<ELFT>::GotPlt->getVA());
George Rimar648a2c32015-10-20 08:54:27 +0000651 WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
652 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000653
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000654 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000655 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000656 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
657 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000658 if (Out<ELFT>::GnuHashTab)
659 WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA());
660 if (Out<ELFT>::HashTab)
661 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000662
Rui Ueyama8c205d52015-10-02 01:33:31 +0000663 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000664
665 // If --enable-new-dtags is set lld emits DT_RUNPATH
666 // instead of DT_RPATH. The two tags are functionally
667 // equivalent except for the following:
668 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
669 // DT_RPATH is searched before.
670 // - DT_RUNPATH is used only to search for direct
671 // dependencies of the object it's contained in, while
672 // DT_RPATH is used for indirect dependencies as well.
673 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000674 Out<ELFT>::DynStrTab->getOffset(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000675
Rui Ueyama8c205d52015-10-02 01:33:31 +0000676 if (!Config->SoName.empty())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000677 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000678
Rafael Espindola77572242015-10-02 19:37:55 +0000679 auto WriteArray = [&](int32_t T1, int32_t T2,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000680 const OutputSectionBase<ELFT> *Sec) {
Rafael Espindola77572242015-10-02 19:37:55 +0000681 if (!Sec)
682 return;
683 WritePtr(T1, Sec->getVA());
684 WriteVal(T2, Sec->getSize());
685 };
686 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
687 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
688 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
689
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000690 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000691 if (F->isNeeded())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000692 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000693
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000694 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000695 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000696 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000697 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000698 if (DtFlags)
699 WriteVal(DT_FLAGS, DtFlags);
700 if (DtFlags1)
701 WriteVal(DT_FLAGS_1, DtFlags1);
Igor Kudrin304860a2015-11-12 04:39:49 +0000702
703 // See "Dynamic Section" in Chapter 5 in the following document
704 // for detailed description:
705 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
706 if (Config->EMachine == EM_MIPS) {
707 WriteVal(DT_MIPS_RLD_VERSION, 1);
708 WriteVal(DT_MIPS_FLAGS, RHF_NOTPOT);
709 WritePtr(DT_MIPS_BASE_ADDRESS, Target->getVAStart());
710 WriteVal(DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols());
711 WriteVal(DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum());
712 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
713 WriteVal(DT_MIPS_GOTSYM, B->getDynamicSymbolTableIndex());
714 else
715 WriteVal(DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols());
716 WritePtr(DT_PLTGOT, Out<ELFT>::Got->getVA());
717 if (Out<ELFT>::MipsRldMap)
718 WritePtr(DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap->getVA());
719 }
720
Rui Ueyama8c205d52015-10-02 01:33:31 +0000721 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000722}
723
724template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000725OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000726 uintX_t sh_flags)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000727 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000728
729template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000730void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
731 Sections.push_back(C);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000732 C->OutSec = this;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000733 uint32_t Align = C->getAlign();
734 if (Align > this->Header.sh_addralign)
735 this->Header.sh_addralign = Align;
736
737 uintX_t Off = this->Header.sh_size;
738 Off = RoundUpToAlignment(Off, Align);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000739 C->OutSecOff = Off;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000740 Off += C->getSize();
741 this->Header.sh_size = Off;
742}
743
744template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000745typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000746 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000747 case SymbolBody::DefinedSyntheticKind: {
748 auto &D = cast<DefinedSynthetic<ELFT>>(S);
749 return D.Section.getVA() + D.Sym.st_value;
750 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000751 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000752 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000753 case SymbolBody::DefinedRegularKind: {
754 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindola48225b42015-10-23 19:55:11 +0000755 InputSectionBase<ELFT> &SC = DR.Section;
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000756 if (DR.Sym.getType() == STT_TLS)
757 return SC.OutSec->getVA() + SC.getOffset(DR.Sym) -
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000758 Out<ELFT>::TlsPhdr->p_vaddr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000759 return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
Rafael Espindolacd076f02015-09-25 18:19:03 +0000760 }
761 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000762 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
George Rimarbc590fe2015-10-28 16:48:58 +0000763 case SymbolBody::SharedKind: {
764 auto &SS = cast<SharedSymbol<ELFT>>(S);
Rafael Espindola9b896082015-11-03 14:34:11 +0000765 if (SS.needsCopy())
George Rimarbc590fe2015-10-28 16:48:58 +0000766 return Out<ELFT>::Bss->getVA() + SS.OffsetInBSS;
767 return 0;
768 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000769 case SymbolBody::UndefinedKind:
770 return 0;
771 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000772 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
773 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000774 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000775 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000776}
777
Rui Ueyama34f29242015-10-13 19:51:57 +0000778// Returns a VA which a relocatin RI refers to. Used only for local symbols.
779// For non-local symbols, use getSymVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000780template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000781typename ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000782lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
Rafael Espindola932efcf2015-10-19 20:24:44 +0000783 const Elf_Rel_Impl<ELFT, IsRela> &RI) {
784 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
785 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
786
787 uintX_t Addend = getAddend<ELFT>(RI);
788
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000789 // PPC64 has a special relocation representing the TOC base pointer
790 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000791 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000792 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000793
Rui Ueyama126d08f2015-10-12 20:28:22 +0000794 const Elf_Sym *Sym =
795 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000796
Rui Ueyama126d08f2015-10-12 20:28:22 +0000797 if (!Sym)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000798 error("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000799
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000800 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
801
802 if (Sym->getType() == STT_TLS)
803 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
804 Out<ELF64LE>::TlsPhdr->p_vaddr;
805
Rafael Espindola444576d2015-10-09 19:25:07 +0000806 // According to the ELF spec reference to a local symbol from outside
807 // the group are not allowed. Unfortunately .eh_frame breaks that rule
808 // and must be treated specially. For now we just replace the symbol with
809 // 0.
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000810 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000811 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000812
Rafael Espindolac159c962015-10-19 21:00:02 +0000813 uintX_t VA = Section->OutSec->getVA();
814 if (isa<InputSection<ELFT>>(Section))
815 return VA + Section->getOffset(*Sym) + Addend;
816
817 uintX_t Offset = Sym->st_value;
818 if (Sym->getType() == STT_SECTION) {
819 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000820 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000821 }
822 return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
823 Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000824}
825
Rui Ueyama34f29242015-10-13 19:51:57 +0000826// Returns true if a symbol can be replaced at load-time by a symbol
827// with the same name defined in other ELF executable or DSO.
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000828bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000829 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000830 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000831 if (Body->isShared())
832 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000833
834 if (Body->isUndefined()) {
835 if (!Body->isWeak())
836 return true;
837
838 // This is an horrible corner case. Ideally we would like to say that any
839 // undefined symbol can be preempted so that the dynamic linker has a
840 // chance of finding it at runtime.
841 //
842 // The problem is that the code sequence used to test for weak undef
843 // functions looks like
844 // if (func) func()
845 // If the code is -fPIC the first reference is a load from the got and
846 // everything works.
847 // If the code is not -fPIC there is no reasonable way to solve it:
848 // * A relocation writing to the text segment will fail (it is ro).
849 // * A copy relocation doesn't work for functions.
850 // * The trick of using a plt entry as the address would fail here since
851 // the plt entry would have a non zero address.
852 // Since we cannot do anything better, we just resolve the symbol to 0 and
853 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000854 //
855 // As an extra hack, assume that if we are producing a shared library the
856 // user knows what he or she is doing and can handle a dynamic relocation.
857 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000858 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000859 if (!Config->Shared)
860 return false;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000861 return Body->getVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000862}
863
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000864template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000865 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000866 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000867}
868
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000869template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000870EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t sh_type,
871 uintX_t sh_flags)
872 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
873
874template <class ELFT>
875EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
876 : S(S), Index(Index) {}
877
878template <class ELFT> StringRef EHRegion<ELFT>::data() const {
879 ArrayRef<uint8_t> SecData = S->getSectionData();
880 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
881 size_t Start = Offsets[Index].first;
882 size_t End =
883 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
884 return StringRef((const char *)SecData.data() + Start, End - Start);
885}
886
887template <class ELFT>
888Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
889 : EHRegion<ELFT>(S, Index) {}
890
891template <class ELFT>
892template <bool IsRela>
893void EHOutputSection<ELFT>::addSectionAux(
894 EHInputSection<ELFT> *S,
895 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
896 const endianness E = ELFT::TargetEndianness;
897
898 S->OutSec = this;
899 uint32_t Align = S->getAlign();
900 if (Align > this->Header.sh_addralign)
901 this->Header.sh_addralign = Align;
902
903 Sections.push_back(S);
904
905 ArrayRef<uint8_t> SecData = S->getSectionData();
906 ArrayRef<uint8_t> D = SecData;
907 uintX_t Offset = 0;
908 auto RelI = Rels.begin();
909 auto RelE = Rels.end();
910
911 DenseMap<unsigned, unsigned> OffsetToIndex;
912 while (!D.empty()) {
913 if (D.size() < 4)
914 error("Truncated CIE/FDE length");
915 uint32_t Length = read32<E>(D.data());
916 Length += 4;
917
918 unsigned Index = S->Offsets.size();
919 S->Offsets.push_back(std::make_pair(Offset, -1));
920
921 if (Length > D.size())
922 error("CIE/FIE ends past the end of the section");
923 StringRef Entry((const char *)D.data(), Length);
924
925 while (RelI != RelE && RelI->r_offset < Offset)
926 ++RelI;
927 uintX_t NextOffset = Offset + Length;
928 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
929
930 uint32_t ID = read32<E>(D.data() + 4);
931 if (ID == 0) {
932 // CIE
933 Cie<ELFT> C(S, Index);
934
935 StringRef Personality;
936 if (HasReloc) {
937 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
938 SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl();
939 Personality = Body.getName();
940 }
941
942 std::pair<StringRef, StringRef> CieInfo(Entry, Personality);
943 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
944 if (P.second) {
945 Cies.push_back(C);
946 this->Header.sh_size += Length;
947 }
948 OffsetToIndex[Offset] = P.first->second;
949 } else {
950 if (!HasReloc)
951 error("FDE doesn't reference another section");
952 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
953 if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) {
954 uint32_t CieOffset = Offset + 4 - ID;
955 auto I = OffsetToIndex.find(CieOffset);
956 if (I == OffsetToIndex.end())
957 error("Invalid CIE reference");
958 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
959 this->Header.sh_size += Length;
960 }
961 }
962
963 Offset = NextOffset;
964 D = D.slice(Length);
965 }
966}
967
968template <class ELFT>
969void EHOutputSection<ELFT>::addSection(EHInputSection<ELFT> *S) {
970 const Elf_Shdr *RelSec = S->RelocSection;
971 if (!RelSec)
972 return addSectionAux(
973 S, make_range((const Elf_Rela *)nullptr, (const Elf_Rela *)nullptr));
974 ELFFile<ELFT> &Obj = S->getFile()->getObj();
975 if (RelSec->sh_type == SHT_RELA)
976 return addSectionAux(S, Obj.relas(RelSec));
977 return addSectionAux(S, Obj.rels(RelSec));
978}
979
980template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
981 const endianness E = ELFT::TargetEndianness;
982 size_t Offset = 0;
983 for (const Cie<ELFT> &C : Cies) {
984 size_t CieOffset = Offset;
985
986 StringRef CieData = C.data();
987 memcpy(Buf + Offset, CieData.data(), CieData.size());
988 C.S->Offsets[C.Index].second = Offset;
989 Offset += CieData.size();
990
991 for (const EHRegion<ELFT> &F : C.Fdes) {
992 StringRef FdeData = F.data();
993 memcpy(Buf + Offset, FdeData.data(), 4); // Legnth
994 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
995 memcpy(Buf + Offset + 8, FdeData.data() + 8, FdeData.size() - 8);
996 F.S->Offsets[F.Index].second = Offset;
997 Offset += FdeData.size();
998 }
999 }
1000
1001 for (EHInputSection<ELFT> *S : Sections) {
1002 const Elf_Shdr *RelSec = S->RelocSection;
1003 if (!RelSec)
1004 continue;
1005 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1006 if (RelSec->sh_type == SHT_RELA)
1007 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1008 else
1009 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1010 }
1011}
1012
1013template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +00001014MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
1015 uintX_t sh_flags)
1016 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
1017
1018template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001019 if (shouldTailMerge()) {
1020 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001021 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001022 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001023 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001024 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1025 StringRef Data = P.first;
1026 memcpy(Buf + P.second, Data.data(), Data.size());
1027 }
1028}
1029
1030static size_t findNull(StringRef S, size_t EntSize) {
1031 // Optimize the common case.
1032 if (EntSize == 1)
1033 return S.find(0);
1034
1035 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1036 const char *B = S.begin() + I;
1037 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1038 return I;
1039 }
1040 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001041}
1042
1043template <class ELFT>
1044void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
1045 S->OutSec = this;
1046 uint32_t Align = S->getAlign();
1047 if (Align > this->Header.sh_addralign)
1048 this->Header.sh_addralign = Align;
1049
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001050 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001051 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001052 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001053 uintX_t Offset = 0;
1054
1055 if (this->Header.sh_flags & SHF_STRINGS) {
1056 while (!Data.empty()) {
1057 size_t End = findNull(Data, EntSize);
1058 if (End == StringRef::npos)
1059 error("String is not null terminated");
1060 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001061 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001062 if (shouldTailMerge())
1063 OutputOffset = -1;
1064 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1065 uintX_t Size = End + EntSize;
1066 Data = Data.substr(Size);
1067 Offset += Size;
1068 }
1069 } else {
1070 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1071 StringRef Entry = Data.substr(I, EntSize);
1072 size_t OutputOffset = Builder.add(Entry);
1073 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1074 Offset += EntSize;
1075 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001076 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001077}
1078
1079template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001080unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1081 return Builder.getOffset(Val);
1082}
1083
1084template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1085 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1086}
1087
1088template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1089 if (shouldTailMerge())
1090 Builder.finalize();
1091 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001092}
1093
1094template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001095StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
1096 : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001097 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001098 Dynamic(Dynamic) {
1099 this->Header.sh_addralign = 1;
1100}
1101
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001102template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001103 StringRef Data = StrTabBuilder.data();
1104 memcpy(Buf, Data.data(), Data.size());
1105}
1106
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001107template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001108 if (!B.isUsedInRegularObj())
1109 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001110
1111 // Don't include synthetic symbols like __init_array_start in every output.
1112 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
1113 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
1114 return false;
1115
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001116 return true;
1117}
1118
Rafael Espindola05a3dd22015-09-22 23:38:23 +00001119bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001120 uint8_t V = B.getVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001121 if (V != STV_DEFAULT && V != STV_PROTECTED)
1122 return false;
1123
Rafael Espindola05a3dd22015-09-22 23:38:23 +00001124 if (Config->ExportDynamic || Config->Shared)
1125 return true;
1126 return B.isUsedInDynamicReloc();
1127}
1128
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001129template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +00001130bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
1131 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001132 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
1133 if (Sym.getType() == STT_SECTION)
1134 return false;
1135
Rafael Espindola444576d2015-10-09 19:25:07 +00001136 // If sym references a section in a discarded group, don't keep it.
Rafael Espindola4cda5812015-10-16 15:29:48 +00001137 if (File.getSection(Sym) == &InputSection<ELFT>::Discarded)
1138 return false;
Rafael Espindola444576d2015-10-09 19:25:07 +00001139
Davide Italiano6993ba42015-09-26 00:47:56 +00001140 if (Config->DiscardNone)
1141 return true;
1142
1143 // ELF defines dynamic locals as symbols which name starts with ".L".
1144 return !(Config->DiscardLocals && SymName.startswith(".L"));
1145}
1146
Rafael Espindola35c6af32015-09-25 17:19:10 +00001147template <class ELFT>
1148SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001149 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
1150 : OutputSectionBase<ELFT>(
Rafael Espindola35c6af32015-09-25 17:19:10 +00001151 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1152 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
1153 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001154 Table(Table), StrTabSec(StrTabSec) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001155 typedef OutputSectionBase<ELFT> Base;
1156 typename Base::Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +00001157
1158 Header.sh_entsize = sizeof(Elf_Sym);
1159 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
1160}
1161
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001162// Orders symbols according to their positions in the GOT,
1163// in compliance with MIPS ABI rules.
1164// See "Global Offset Table" in Chapter 5 in the following document
1165// for detailed description:
1166// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1167static bool sortMipsSymbols(SymbolBody *L, SymbolBody *R) {
1168 if (!L->isInGot() || !R->isInGot())
1169 return R->isInGot();
1170 return L->GotIndex < R->GotIndex;
1171}
1172
Rui Ueyama0db335f2015-10-07 16:58:54 +00001173template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001174 if (this->Header.sh_size)
1175 return; // Already finalized.
1176
Rui Ueyama0db335f2015-10-07 16:58:54 +00001177 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001178 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001179 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001180
1181 if (!StrTabSec.isDynamic()) {
1182 std::stable_sort(Symbols.begin(), Symbols.end(),
Igor Kudrinf1d60292015-10-28 07:05:56 +00001183 [](SymbolBody *L, SymbolBody *R) {
1184 return getSymbolBinding(L) == STB_LOCAL &&
1185 getSymbolBinding(R) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001186 });
1187 return;
1188 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001189 if (Out<ELFT>::GnuHashTab)
1190 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1191 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001192 else if (Config->EMachine == EM_MIPS)
1193 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001194 size_t I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +00001195 for (SymbolBody *B : Symbols)
1196 B->setDynamicSymbolTableIndex(++I);
Rui Ueyama0db335f2015-10-07 16:58:54 +00001197}
1198
1199template <class ELFT>
Igor Kudrinab665fc2015-10-20 21:47:58 +00001200void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
Rui Ueyama0db335f2015-10-07 16:58:54 +00001201 StrTabSec.add(Name);
1202 ++NumVisible;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001203 ++NumLocals;
1204}
1205
1206template <class ELFT>
1207void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
1208 StrTabSec.add(Body->getName());
Igor Kudrinf1d60292015-10-28 07:05:56 +00001209 Symbols.push_back(Body);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001210 ++NumVisible;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001211}
1212
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001213template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001214 Buf += sizeof(Elf_Sym);
1215
1216 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1217 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001218 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1219 writeLocalSymbols(Buf);
1220
1221 writeGlobalSymbols(Buf);
1222}
1223
1224template <class ELFT>
1225void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1226 // Iterate over all input object files to copy their local symbols
1227 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001228 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
1229 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001230 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001231 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +00001232 error(SymNameOrErr);
1233 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001234 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001235 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +00001236
Rui Ueyamac55733e2015-09-30 00:54:29 +00001237 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001238 uintX_t VA = 0;
Rafael Espindola4cda5812015-10-16 15:29:48 +00001239 if (Sym.st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001240 ESym->st_shndx = SHN_ABS;
Rafael Espindolac159c962015-10-19 21:00:02 +00001241 VA = Sym.st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001242 } else {
Rafael Espindola48225b42015-10-23 19:55:11 +00001243 InputSectionBase<ELFT> *Section = File->getSection(Sym);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001244 if (!Section->isLive())
1245 continue;
Rafael Espindolac159c962015-10-19 21:00:02 +00001246 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1247 ESym->st_shndx = OutSec->SectionIndex;
1248 VA += OutSec->getVA() + Section->getOffset(Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001249 }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001250 ESym->st_name = StrTabSec.getOffset(SymName);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001251 ESym->st_size = Sym.st_size;
1252 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001253 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001254 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001255 }
1256 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001257}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001258
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001259template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001260void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001261 // Write the internal symbol table contents to the output symbol table
1262 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001263 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +00001264 for (SymbolBody *Body : Symbols) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001265 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001266
Rafael Espindola8614c562015-10-06 14:33:58 +00001267 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001268 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001269 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001270 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001271 case SymbolBody::DefinedRegularKind: {
1272 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
1273 if (!Sym->Section.isLive())
1274 continue;
1275 OutSec = Sym->Section.OutSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001276 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001277 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001278 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001279 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001280 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001281 case SymbolBody::SharedKind: {
Rafael Espindola9b896082015-11-03 14:34:11 +00001282 if (cast<SharedSymbol<ELFT>>(Body)->needsCopy())
George Rimarbc590fe2015-10-28 16:48:58 +00001283 OutSec = Out<ELFT>::Bss;
1284 break;
1285 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001286 case SymbolBody::UndefinedKind:
1287 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001288 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001289 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001290 }
1291
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001292 StringRef Name = Body->getName();
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001293 ESym->st_name = StrTabSec.getOffset(Name);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001294
Rafael Espindola8614c562015-10-06 14:33:58 +00001295 unsigned char Type = STT_NOTYPE;
1296 uintX_t Size = 0;
1297 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
1298 const Elf_Sym &InputSym = EBody->Sym;
Rafael Espindola8614c562015-10-06 14:33:58 +00001299 Type = InputSym.getType();
1300 Size = InputSym.st_size;
1301 }
1302
Igor Kudrin853b88d2015-10-20 20:52:14 +00001303 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001304 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001305 ESym->setVisibility(Body->getVisibility());
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001306 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001307
Rafael Espindola8614c562015-10-06 14:33:58 +00001308 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +00001309 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +00001310 else if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001311 ESym->st_shndx = OutSec->SectionIndex;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001312
1313 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001314 }
1315}
1316
Igor Kudrin853b88d2015-10-20 20:52:14 +00001317template <class ELFT>
1318uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001319 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001320 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1321 return STB_LOCAL;
1322 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body))
1323 return EBody->Sym.getBinding();
1324 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1325}
1326
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001327namespace lld {
1328namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001329template class OutputSectionBase<ELF32LE>;
1330template class OutputSectionBase<ELF32BE>;
1331template class OutputSectionBase<ELF64LE>;
1332template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001333
George Rimar648a2c32015-10-20 08:54:27 +00001334template class GotPltSection<ELF32LE>;
1335template class GotPltSection<ELF32BE>;
1336template class GotPltSection<ELF64LE>;
1337template class GotPltSection<ELF64BE>;
1338
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001339template class GotSection<ELF32LE>;
1340template class GotSection<ELF32BE>;
1341template class GotSection<ELF64LE>;
1342template class GotSection<ELF64BE>;
1343
1344template class PltSection<ELF32LE>;
1345template class PltSection<ELF32BE>;
1346template class PltSection<ELF64LE>;
1347template class PltSection<ELF64BE>;
1348
1349template class RelocationSection<ELF32LE>;
1350template class RelocationSection<ELF32BE>;
1351template class RelocationSection<ELF64LE>;
1352template class RelocationSection<ELF64BE>;
1353
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001354template class InterpSection<ELF32LE>;
1355template class InterpSection<ELF32BE>;
1356template class InterpSection<ELF64LE>;
1357template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001358
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001359template class GnuHashTableSection<ELF32LE>;
1360template class GnuHashTableSection<ELF32BE>;
1361template class GnuHashTableSection<ELF64LE>;
1362template class GnuHashTableSection<ELF64BE>;
1363
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001364template class HashTableSection<ELF32LE>;
1365template class HashTableSection<ELF32BE>;
1366template class HashTableSection<ELF64LE>;
1367template class HashTableSection<ELF64BE>;
1368
1369template class DynamicSection<ELF32LE>;
1370template class DynamicSection<ELF32BE>;
1371template class DynamicSection<ELF64LE>;
1372template class DynamicSection<ELF64BE>;
1373
1374template class OutputSection<ELF32LE>;
1375template class OutputSection<ELF32BE>;
1376template class OutputSection<ELF64LE>;
1377template class OutputSection<ELF64BE>;
1378
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001379template class EHOutputSection<ELF32LE>;
1380template class EHOutputSection<ELF32BE>;
1381template class EHOutputSection<ELF64LE>;
1382template class EHOutputSection<ELF64BE>;
1383
Rafael Espindolac159c962015-10-19 21:00:02 +00001384template class MergeOutputSection<ELF32LE>;
1385template class MergeOutputSection<ELF32BE>;
1386template class MergeOutputSection<ELF64LE>;
1387template class MergeOutputSection<ELF64BE>;
1388
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001389template class StringTableSection<ELF32LE>;
1390template class StringTableSection<ELF32BE>;
1391template class StringTableSection<ELF64LE>;
1392template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001393
1394template class SymbolTableSection<ELF32LE>;
1395template class SymbolTableSection<ELF32BE>;
1396template class SymbolTableSection<ELF64LE>;
1397template class SymbolTableSection<ELF64BE>;
1398
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001399template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1400template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1401template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1402template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +00001403
Rafael Espindola56f965f2015-09-21 22:48:12 +00001404template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001405getLocalRelTarget(const ObjectFile<ELF32LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001406 const ELFFile<ELF32LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001407template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001408getLocalRelTarget(const ObjectFile<ELF32BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001409 const ELFFile<ELF32BE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001410template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001411getLocalRelTarget(const ObjectFile<ELF64LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001412 const ELFFile<ELF64LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001413template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001414getLocalRelTarget(const ObjectFile<ELF64BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +00001415 const ELFFile<ELF64BE>::Elf_Rel &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001416
Rafael Espindolac159c962015-10-19 21:00:02 +00001417template ELFFile<ELF32LE>::uintX_t
1418getLocalRelTarget(const ObjectFile<ELF32LE> &,
1419 const ELFFile<ELF32LE>::Elf_Rela &);
1420template ELFFile<ELF32BE>::uintX_t
1421getLocalRelTarget(const ObjectFile<ELF32BE> &,
1422 const ELFFile<ELF32BE>::Elf_Rela &);
1423template ELFFile<ELF64LE>::uintX_t
1424getLocalRelTarget(const ObjectFile<ELF64LE> &,
1425 const ELFFile<ELF64LE>::Elf_Rela &);
1426template ELFFile<ELF64BE>::uintX_t
1427getLocalRelTarget(const ObjectFile<ELF64BE> &,
1428 const ELFFile<ELF64BE>::Elf_Rela &);
1429
Rafael Espindola4f674ed2015-10-05 15:24:04 +00001430template bool includeInSymtab<ELF32LE>(const SymbolBody &);
1431template bool includeInSymtab<ELF32BE>(const SymbolBody &);
1432template bool includeInSymtab<ELF64LE>(const SymbolBody &);
1433template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001434
Rafael Espindola444576d2015-10-09 19:25:07 +00001435template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1436 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001437 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001438template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1439 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001440 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001441template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1442 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001443 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001444template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1445 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001446 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001447}
1448}