blob: 25828b495e900076dbc1987f79ded5c751bf6841 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.cpp -------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "OutputSections.h"
11#include "Config.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000012#include "SymbolTable.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000013#include "Target.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000014
Rafael Espindola5805c4f2015-09-21 21:38:08 +000015using namespace llvm;
16using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000017using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000018using namespace llvm::ELF;
19
20using namespace lld;
21using namespace lld::elf2;
22
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000023template <class ELFT>
24OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t sh_type,
25 uintX_t sh_flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026 : Name(Name) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000027 memset(&Header, 0, sizeof(Elf_Shdr));
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028 Header.sh_type = sh_type;
29 Header.sh_flags = sh_flags;
30}
31
Rafael Espindola35c6af32015-09-25 17:19:10 +000032template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000033GotSection<ELFT>::GotSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000034 : OutputSectionBase<ELFT>(".got", llvm::ELF::SHT_PROGBITS,
35 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
Rui Ueyama5f551ae2015-10-14 14:02:06 +000036 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000037}
38
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +000040 Sym->GotIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041 Entries.push_back(Sym);
42}
43
44template <class ELFT>
45typename GotSection<ELFT>::uintX_t
46GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama5f551ae2015-10-14 14:02:06 +000047 return this->getVA() + B.GotIndex * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000048}
49
Rafael Espindolaa6627382015-10-06 23:56:53 +000050template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
51 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +000052 uint8_t *Entry = Buf;
53 Buf += sizeof(uintX_t);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +000054 if (canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +000055 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +000056 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +000057 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +000058 }
59}
60
Rafael Espindola35c6af32015-09-25 17:19:10 +000061template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000062PltSection<ELFT>::PltSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000063 : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS,
64 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +000065 this->Header.sh_addralign = 16;
66}
67
Rafael Espindola5805c4f2015-09-21 21:38:08 +000068template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +000069 size_t Off = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000070 for (const SymbolBody *E : Entries) {
Rui Ueyamac58656c2015-10-13 16:59:30 +000071 uint64_t Got = Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +000072 uint64_t Plt = this->getVA() + Off;
Rui Ueyamac58656c2015-10-13 16:59:30 +000073 Target->writePltEntry(Buf + Off, Got, Plt);
Rui Ueyama242ddf42015-10-12 18:56:36 +000074 Off += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075 }
76}
77
78template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +000079 Sym->PltIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080 Entries.push_back(Sym);
81}
82
83template <class ELFT>
84typename PltSection<ELFT>::uintX_t
85PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyamac58656c2015-10-13 16:59:30 +000086 return this->getVA() + B.PltIndex * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +000087}
88
89template <class ELFT>
90void PltSection<ELFT>::finalize() {
Rui Ueyamac58656c2015-10-13 16:59:30 +000091 this->Header.sh_size = Entries.size() * Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000092}
93
Rafael Espindola35c6af32015-09-25 17:19:10 +000094template <class ELFT>
Rui Ueyamac58656c2015-10-13 16:59:30 +000095RelocationSection<ELFT>::RelocationSection(bool IsRela)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000096 : OutputSectionBase<ELFT>(IsRela ? ".rela.dyn" : ".rel.dyn",
97 IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
98 llvm::ELF::SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +000099 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000100 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
101 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
102}
103
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000105 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000107 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
108 Buf += EntrySize;
109
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000110 const InputSection<ELFT> &C = Rel.C;
111 const Elf_Rel &RI = Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000112 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000113 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000114 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000115 if (Body)
116 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000117
Rui Ueyama64558522015-10-16 22:51:43 +0000118 uint32_t Type = RI.getType(Config->Mips64EL);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000119 bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
120 bool CanBePreempted = canBePreempted(Body, NeedsGot);
Rafael Espindola49757522015-10-19 19:58:18 +0000121
122 if (CanBePreempted) {
123 if (NeedsGot)
124 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
125 Target->getGotReloc(), Config->Mips64EL);
126 else
127 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type,
128 Config->Mips64EL);
129 } else {
Rui Ueyama64558522015-10-16 22:51:43 +0000130 P->setSymbolAndType(0, Target->getRelativeReloc(), Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000131 }
132
Rafael Espindola49757522015-10-19 19:58:18 +0000133 if (NeedsGot)
Rui Ueyamac58656c2015-10-13 16:59:30 +0000134 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
Rafael Espindola49757522015-10-19 19:58:18 +0000135 else
Rui Ueyama55c3f892015-10-15 01:58:40 +0000136 P->r_offset = RI.r_offset + C.OutSec->getVA() + C.OutSecOff;
Rafael Espindola49757522015-10-19 19:58:18 +0000137
Rafael Espindola932efcf2015-10-19 20:24:44 +0000138 uintX_t OrigAddend = 0;
Rafael Espindola49757522015-10-19 19:58:18 +0000139 if (IsRela && !NeedsGot)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000140 OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola49757522015-10-19 19:58:18 +0000141
Rafael Espindola932efcf2015-10-19 20:24:44 +0000142 uintX_t Addend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000143 if (CanBePreempted)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000144 Addend = OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000145 else if (Body)
146 Addend = getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body)) + OrigAddend;
147 else if (IsRela)
148 Addend = getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI));
149 else
150 Addend = getLocalRelTarget(File, RI);
Rafael Espindola52dca342015-10-07 00:58:20 +0000151
152 if (IsRela)
153 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000154 }
155}
156
157template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000158 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
160}
161
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000162template <class ELFT>
163InterpSection<ELFT>::InterpSection()
164 : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
165 llvm::ELF::SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000166 this->Header.sh_size = Config->DynamicLinker.size() + 1;
167 this->Header.sh_addralign = 1;
168}
169
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000170template <class ELFT>
171void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
172 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000173}
174
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000175template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
177}
178
Rafael Espindola35c6af32015-09-25 17:19:10 +0000179template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000180HashTableSection<ELFT>::HashTableSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000181 : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
182 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000183 this->Header.sh_entsize = sizeof(Elf_Word);
184 this->Header.sh_addralign = sizeof(Elf_Word);
185}
186
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000187static uint32_t hash(StringRef Name) {
188 uint32_t H = 0;
189 for (char C : Name) {
190 H = (H << 4) + C;
191 uint32_t G = H & 0xf0000000;
192 if (G)
193 H ^= G >> 24;
194 H &= ~G;
195 }
196 return H;
197}
198
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
200 StringRef Name = S->getName();
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000201 Out<ELFT>::DynSymTab->addSymbol(Name);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000202 Hashes.push_back(hash(Name));
203 S->setDynamicSymbolTableIndex(Hashes.size());
204}
205
Rui Ueyama0db335f2015-10-07 16:58:54 +0000206template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000207 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000208
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000209 assert(Out<ELFT>::DynSymTab->getNumSymbols() == Hashes.size() + 1);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000210 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000211 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000212
213 // Create as many buckets as there are symbols.
214 // FIXME: This is simplistic. We can try to optimize it, but implementing
215 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000216 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000217 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
218}
219
220template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000221 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000222 auto *P = reinterpret_cast<Elf_Word *>(Buf);
223 *P++ = NumSymbols; // nbucket
224 *P++ = NumSymbols; // nchain
225
226 Elf_Word *Buckets = P;
227 Elf_Word *Chains = P + NumSymbols;
228
229 for (unsigned I = 1; I < NumSymbols; ++I) {
230 uint32_t Hash = Hashes[I - 1] % NumSymbols;
231 Chains[I] = Buckets[Hash];
232 Buckets[Hash] = I;
233 }
234}
235
Rafael Espindola35c6af32015-09-25 17:19:10 +0000236template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000237DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000238 : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
239 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000240 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000241 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000242 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
243 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
244}
245
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000246template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000247 if (this->Header.sh_size)
248 return; // Already finalized.
249
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000250 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000251 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000252
253 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000254 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000255 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000256 ++NumEntries; // DT_RELASZ / DT_RELSZ
257 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000258 }
259 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000260 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000261 ++NumEntries; // DT_STRTAB
262 ++NumEntries; // DT_STRSZ
263 ++NumEntries; // DT_HASH
264
Rui Ueyama7de3f372015-10-01 19:36:04 +0000265 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000266 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000267 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000268 }
269
270 if (!Config->SoName.empty()) {
271 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000272 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000273 }
274
Rafael Espindola77572242015-10-02 19:37:55 +0000275 if (PreInitArraySec)
276 NumEntries += 2;
277 if (InitArraySec)
278 NumEntries += 2;
279 if (FiniArraySec)
280 NumEntries += 2;
281
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000282 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000283 if (!F->isNeeded())
284 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000285 Out<ELFT>::DynStrTab->add(F->getSoName());
286 ++NumEntries;
287 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000288
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000289 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
290 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
291 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
292 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000293 if (InitSym)
294 ++NumEntries; // DT_INIT
295 if (FiniSym)
296 ++NumEntries; // DT_FINI
Davide Italiano58cbaf02015-10-19 23:32:16 +0000297 if (Config->Bsymbolic)
298 ++NumEntries; // DT_FLAGS
299 if (Config->ZNow)
George Rimar97aad172015-10-07 15:00:21 +0000300 ++NumEntries; // DT_FLAGS_1
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000301 ++NumEntries; // DT_NULL
302
303 Header.sh_size = NumEntries * Header.sh_entsize;
304}
305
306template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000307 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
308
Rui Ueyama8c205d52015-10-02 01:33:31 +0000309 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
310 P->d_tag = Tag;
311 P->d_un.d_ptr = Val;
312 ++P;
313 };
314
315 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
316 P->d_tag = Tag;
317 P->d_un.d_val = Val;
318 ++P;
319 };
320
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000321 if (Out<ELFT>::RelaDyn->hasRelocs()) {
322 bool IsRela = Out<ELFT>::RelaDyn->isRela();
323 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
324 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000325 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
326 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000327 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000328
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000329 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000330 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000331 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
332 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
333 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000334
Rui Ueyama8c205d52015-10-02 01:33:31 +0000335 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000336
337 // If --enable-new-dtags is set lld emits DT_RUNPATH
338 // instead of DT_RPATH. The two tags are functionally
339 // equivalent except for the following:
340 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
341 // DT_RPATH is searched before.
342 // - DT_RUNPATH is used only to search for direct
343 // dependencies of the object it's contained in, while
344 // DT_RPATH is used for indirect dependencies as well.
345 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000346 Out<ELFT>::DynStrTab->getFileOff(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000347
Rui Ueyama8c205d52015-10-02 01:33:31 +0000348 if (!Config->SoName.empty())
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000349 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getFileOff(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000350
Rafael Espindola77572242015-10-02 19:37:55 +0000351 auto WriteArray = [&](int32_t T1, int32_t T2,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000352 const OutputSectionBase<ELFT> *Sec) {
Rafael Espindola77572242015-10-02 19:37:55 +0000353 if (!Sec)
354 return;
355 WritePtr(T1, Sec->getVA());
356 WriteVal(T2, Sec->getSize());
357 };
358 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
359 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
360 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
361
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000362 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000363 if (F->isNeeded())
364 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getFileOff(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000365
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000366 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000367 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000368 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000369 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000370
Davide Italianocebb4492015-10-13 21:02:34 +0000371 uint32_t Flags = 0;
372 if (Config->Bsymbolic)
373 Flags |= DF_SYMBOLIC;
Davide Italiano56d18f42015-10-19 21:34:00 +0000374 if (Flags)
375 WriteVal(DT_FLAGS, Flags);
376 Flags = 0;
George Rimar97aad172015-10-07 15:00:21 +0000377 if (Config->ZNow)
Davide Italianocebb4492015-10-13 21:02:34 +0000378 Flags |= DF_1_NOW;
379 if (Flags)
380 WriteVal(DT_FLAGS_1, Flags);
George Rimar97aad172015-10-07 15:00:21 +0000381
Rui Ueyama8c205d52015-10-02 01:33:31 +0000382 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000383}
384
385template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000386OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000387 uintX_t sh_flags)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000388 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000389
390template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000391void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
392 Sections.push_back(C);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000393 C->OutSec = this;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000394 uint32_t Align = C->getAlign();
395 if (Align > this->Header.sh_addralign)
396 this->Header.sh_addralign = Align;
397
398 uintX_t Off = this->Header.sh_size;
399 Off = RoundUpToAlignment(Off, Align);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000400 C->OutSecOff = Off;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000401 Off += C->getSize();
402 this->Header.sh_size = Off;
403}
404
405template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000406typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000407 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000408 case SymbolBody::DefinedSyntheticKind: {
409 auto &D = cast<DefinedSynthetic<ELFT>>(S);
410 return D.Section.getVA() + D.Sym.st_value;
411 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000412 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000413 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000414 case SymbolBody::DefinedRegularKind: {
415 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindolac159c962015-10-19 21:00:02 +0000416 const InputSectionBase<ELFT> &SC = DR.Section;
417 return SC.OutSec->getVA() + SC.getOffset(DR.Sym);
Rafael Espindolacd076f02015-09-25 18:19:03 +0000418 }
419 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000420 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000421 case SymbolBody::SharedKind:
422 case SymbolBody::UndefinedKind:
423 return 0;
424 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000425 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
426 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000427 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000428 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000429}
430
Rui Ueyama34f29242015-10-13 19:51:57 +0000431// Returns a VA which a relocatin RI refers to. Used only for local symbols.
432// For non-local symbols, use getSymVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000433template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000434typename ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000435lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
Rafael Espindola932efcf2015-10-19 20:24:44 +0000436 const Elf_Rel_Impl<ELFT, IsRela> &RI) {
437 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
438 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
439
440 uintX_t Addend = getAddend<ELFT>(RI);
441
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000442 // PPC64 has a special relocation representing the TOC base pointer
443 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000444 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000445 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000446
Rui Ueyama126d08f2015-10-12 20:28:22 +0000447 const Elf_Sym *Sym =
448 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000449
Rui Ueyama126d08f2015-10-12 20:28:22 +0000450 if (!Sym)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000451 error("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000452
Rafael Espindola444576d2015-10-09 19:25:07 +0000453 // According to the ELF spec reference to a local symbol from outside
454 // the group are not allowed. Unfortunately .eh_frame breaks that rule
455 // and must be treated specially. For now we just replace the symbol with
456 // 0.
Rafael Espindolac159c962015-10-19 21:00:02 +0000457 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
Rafael Espindola444576d2015-10-09 19:25:07 +0000458 if (Section == &InputSection<ELFT>::Discarded)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000459 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000460
Rafael Espindolac159c962015-10-19 21:00:02 +0000461 uintX_t VA = Section->OutSec->getVA();
462 if (isa<InputSection<ELFT>>(Section))
463 return VA + Section->getOffset(*Sym) + Addend;
464
465 uintX_t Offset = Sym->st_value;
466 if (Sym->getType() == STT_SECTION) {
467 Offset += Addend;
468 Addend = Offset % Section->getSectionHdr()->sh_entsize;
469 Offset -= Addend;
470 }
471 return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
472 Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000473}
474
Rui Ueyama34f29242015-10-13 19:51:57 +0000475// Returns true if a symbol can be replaced at load-time by a symbol
476// with the same name defined in other ELF executable or DSO.
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000477bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000478 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000479 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000480 if (Body->isShared())
481 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000482
483 if (Body->isUndefined()) {
484 if (!Body->isWeak())
485 return true;
486
487 // This is an horrible corner case. Ideally we would like to say that any
488 // undefined symbol can be preempted so that the dynamic linker has a
489 // chance of finding it at runtime.
490 //
491 // The problem is that the code sequence used to test for weak undef
492 // functions looks like
493 // if (func) func()
494 // If the code is -fPIC the first reference is a load from the got and
495 // everything works.
496 // If the code is not -fPIC there is no reasonable way to solve it:
497 // * A relocation writing to the text segment will fail (it is ro).
498 // * A copy relocation doesn't work for functions.
499 // * The trick of using a plt entry as the address would fail here since
500 // the plt entry would have a non zero address.
501 // Since we cannot do anything better, we just resolve the symbol to 0 and
502 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000503 //
504 // As an extra hack, assume that if we are producing a shared library the
505 // user knows what he or she is doing and can handle a dynamic relocation.
506 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000507 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000508 if (!Config->Shared)
509 return false;
Rafael Espindola52dca342015-10-07 00:58:20 +0000510 return Body->getMostConstrainingVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000511}
512
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000513template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000514 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000515 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000516}
517
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000518template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000519MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
520 uintX_t sh_flags)
521 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
522
523template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
524 for (const std::pair<ArrayRef<uint8_t>, uintX_t> &P : Offsets) {
525 ArrayRef<uint8_t> Data = P.first;
526 memcpy(Buf, Data.data(), Data.size());
527 Buf += Data.size();
528 }
529}
530
531template <class ELFT>
532void MergeOutputSection<ELFT>::addSection(MergeInputSection<ELFT> *S) {
533 S->OutSec = this;
534 uint32_t Align = S->getAlign();
535 if (Align > this->Header.sh_addralign)
536 this->Header.sh_addralign = Align;
537
538 uintX_t Off = this->Header.sh_size;
539 ArrayRef<uint8_t> Data = S->getSectionData();
540 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
541 if (Data.size() % EntSize)
542 error("SHF_MERGE section size must be a multiple of sh_entsize");
543 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
544 auto P = Offsets.insert(std::make_pair(Data.slice(I, EntSize), Off));
545 if (P.second)
546 Off += EntSize;
547 }
548 this->Header.sh_size = Off;
549}
550
551template <class ELFT>
552unsigned MergeOutputSection<ELFT>::getOffset(ArrayRef<uint8_t> Val) {
553 return Offsets.find(Val)->second;
554}
555
556template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000557StringTableSection<ELFT>::StringTableSection(bool Dynamic)
558 : OutputSectionBase<ELFT>(Dynamic ? ".dynstr" : ".strtab",
559 llvm::ELF::SHT_STRTAB,
560 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +0000561 Dynamic(Dynamic) {
562 this->Header.sh_addralign = 1;
563}
564
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000565template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000566 StringRef Data = StrTabBuilder.data();
567 memcpy(Buf, Data.data(), Data.size());
568}
569
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000570template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000571 if (!B.isUsedInRegularObj())
572 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000573
574 // Don't include synthetic symbols like __init_array_start in every output.
575 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
576 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
577 return false;
578
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000579 return true;
580}
581
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000582bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000583 uint8_t V = B.getMostConstrainingVisibility();
584 if (V != STV_DEFAULT && V != STV_PROTECTED)
585 return false;
586
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000587 if (Config->ExportDynamic || Config->Shared)
588 return true;
589 return B.isUsedInDynamicReloc();
590}
591
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000592template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +0000593bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
594 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000595 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
596 if (Sym.getType() == STT_SECTION)
597 return false;
598
Rafael Espindola444576d2015-10-09 19:25:07 +0000599 // If sym references a section in a discarded group, don't keep it.
Rafael Espindola4cda5812015-10-16 15:29:48 +0000600 if (File.getSection(Sym) == &InputSection<ELFT>::Discarded)
601 return false;
Rafael Espindola444576d2015-10-09 19:25:07 +0000602
Davide Italiano6993ba42015-09-26 00:47:56 +0000603 if (Config->DiscardNone)
604 return true;
605
606 // ELF defines dynamic locals as symbols which name starts with ".L".
607 return !(Config->DiscardLocals && SymName.startswith(".L"));
608}
609
Rafael Espindola35c6af32015-09-25 17:19:10 +0000610template <class ELFT>
611SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000612 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
613 : OutputSectionBase<ELFT>(
Rafael Espindola35c6af32015-09-25 17:19:10 +0000614 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
615 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
616 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000617 Table(Table), StrTabSec(StrTabSec) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000618 typedef OutputSectionBase<ELFT> Base;
619 typename Base::Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000620
621 Header.sh_entsize = sizeof(Elf_Sym);
622 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
623}
624
Rui Ueyama0db335f2015-10-07 16:58:54 +0000625template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
626 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000627 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000628 this->Header.sh_info = NumLocals + 1;
629}
630
631template <class ELFT>
632void SymbolTableSection<ELFT>::addSymbol(StringRef Name, bool isLocal) {
633 StrTabSec.add(Name);
634 ++NumVisible;
635 if (isLocal)
636 ++NumLocals;
637}
638
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000639template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000640 Buf += sizeof(Elf_Sym);
641
642 // All symbols with STB_LOCAL binding precede the weak and global symbols.
643 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000644 if (!Config->DiscardAll && !StrTabSec.isDynamic())
645 writeLocalSymbols(Buf);
646
647 writeGlobalSymbols(Buf);
648}
649
650template <class ELFT>
651void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
652 // Iterate over all input object files to copy their local symbols
653 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000654 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
655 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000656 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000657 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +0000658 error(SymNameOrErr);
659 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000660 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000661 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +0000662
Rui Ueyamac55733e2015-09-30 00:54:29 +0000663 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
664 Buf += sizeof(*ESym);
Rafael Espindola26fd69d2015-10-09 16:15:57 +0000665 ESym->st_name = StrTabSec.getFileOff(SymName);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000666 ESym->st_size = Sym.st_size;
667 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rafael Espindolac159c962015-10-19 21:00:02 +0000668 uintX_t VA = 0;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000669 if (Sym.st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000670 ESym->st_shndx = SHN_ABS;
Rafael Espindolac159c962015-10-19 21:00:02 +0000671 VA = Sym.st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000672 } else {
Rafael Espindolac159c962015-10-19 21:00:02 +0000673 const InputSectionBase<ELFT> *Section = File->getSection(Sym);
674 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
675 ESym->st_shndx = OutSec->SectionIndex;
676 VA += OutSec->getVA() + Section->getOffset(Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000677 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000678 ESym->st_value = VA;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000679 }
680 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000681}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000682
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000683template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +0000684void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000685 // Write the internal symbol table contents to the output symbol table
686 // pointed by Buf.
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000687 uint8_t *Start = Buf;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000688 for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000689 StringRef Name = P.first;
690 Symbol *Sym = P.second;
691 SymbolBody *Body = Sym->Body;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000692 if (!includeInSymtab<ELFT>(*Body))
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000693 continue;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000694 if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body))
695 continue;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000696
697 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac55733e2015-09-30 00:54:29 +0000698 Buf += sizeof(*ESym);
Rafael Espindola8614c562015-10-06 14:33:58 +0000699
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000700 ESym->st_name = StrTabSec.getFileOff(Name);
701
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000702 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000703 const InputSectionBase<ELFT> *Section = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000704
Rafael Espindola8614c562015-10-06 14:33:58 +0000705 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000706 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +0000707 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000708 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000709 case SymbolBody::DefinedRegularKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000710 Section = &cast<DefinedRegular<ELFT>>(Body)->Section;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000711 break;
712 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000713 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000714 break;
715 case SymbolBody::UndefinedKind:
716 case SymbolBody::DefinedAbsoluteKind:
717 case SymbolBody::SharedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000718 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000719 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000720 }
721
Rafael Espindola8614c562015-10-06 14:33:58 +0000722 unsigned char Binding = Body->isWeak() ? STB_WEAK : STB_GLOBAL;
723 unsigned char Type = STT_NOTYPE;
724 uintX_t Size = 0;
725 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
726 const Elf_Sym &InputSym = EBody->Sym;
727 Binding = InputSym.getBinding();
728 Type = InputSym.getType();
729 Size = InputSym.st_size;
730 }
731
732 unsigned char Visibility = Body->getMostConstrainingVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000733 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
734 Binding = STB_LOCAL;
735
Rafael Espindola8614c562015-10-06 14:33:58 +0000736 ESym->setBindingAndType(Binding, Type);
737 ESym->st_size = Size;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000738 ESym->setVisibility(Visibility);
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000739 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000740
741 if (Section)
Rui Ueyama55c3f892015-10-15 01:58:40 +0000742 OutSec = Section->OutSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000743
Rafael Espindola8614c562015-10-06 14:33:58 +0000744 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +0000745 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +0000746 else if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000747 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000748 }
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000749 if (!StrTabSec.isDynamic())
750 std::stable_sort(
751 reinterpret_cast<Elf_Sym *>(Start), reinterpret_cast<Elf_Sym *>(Buf),
752 [](const Elf_Sym &A, const Elf_Sym &B) -> bool {
753 return A.getBinding() == STB_LOCAL && B.getBinding() != STB_LOCAL;
754 });
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000755}
756
757namespace lld {
758namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000759template class OutputSectionBase<ELF32LE>;
760template class OutputSectionBase<ELF32BE>;
761template class OutputSectionBase<ELF64LE>;
762template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000763
764template class GotSection<ELF32LE>;
765template class GotSection<ELF32BE>;
766template class GotSection<ELF64LE>;
767template class GotSection<ELF64BE>;
768
769template class PltSection<ELF32LE>;
770template class PltSection<ELF32BE>;
771template class PltSection<ELF64LE>;
772template class PltSection<ELF64BE>;
773
774template class RelocationSection<ELF32LE>;
775template class RelocationSection<ELF32BE>;
776template class RelocationSection<ELF64LE>;
777template class RelocationSection<ELF64BE>;
778
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000779template class InterpSection<ELF32LE>;
780template class InterpSection<ELF32BE>;
781template class InterpSection<ELF64LE>;
782template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000783
784template class HashTableSection<ELF32LE>;
785template class HashTableSection<ELF32BE>;
786template class HashTableSection<ELF64LE>;
787template class HashTableSection<ELF64BE>;
788
789template class DynamicSection<ELF32LE>;
790template class DynamicSection<ELF32BE>;
791template class DynamicSection<ELF64LE>;
792template class DynamicSection<ELF64BE>;
793
794template class OutputSection<ELF32LE>;
795template class OutputSection<ELF32BE>;
796template class OutputSection<ELF64LE>;
797template class OutputSection<ELF64BE>;
798
Rafael Espindolac159c962015-10-19 21:00:02 +0000799template class MergeOutputSection<ELF32LE>;
800template class MergeOutputSection<ELF32BE>;
801template class MergeOutputSection<ELF64LE>;
802template class MergeOutputSection<ELF64BE>;
803
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000804template class StringTableSection<ELF32LE>;
805template class StringTableSection<ELF32BE>;
806template class StringTableSection<ELF64LE>;
807template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000808
809template class SymbolTableSection<ELF32LE>;
810template class SymbolTableSection<ELF32BE>;
811template class SymbolTableSection<ELF64LE>;
812template class SymbolTableSection<ELF64BE>;
813
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000814template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
815template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
816template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
817template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000818
Rafael Espindola56f965f2015-09-21 22:48:12 +0000819template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000820getLocalRelTarget(const ObjectFile<ELF32LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +0000821 const ELFFile<ELF32LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +0000822template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000823getLocalRelTarget(const ObjectFile<ELF32BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +0000824 const ELFFile<ELF32BE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +0000825template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000826getLocalRelTarget(const ObjectFile<ELF64LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +0000827 const ELFFile<ELF64LE>::Elf_Rel &);
Rafael Espindola56f965f2015-09-21 22:48:12 +0000828template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000829getLocalRelTarget(const ObjectFile<ELF64BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +0000830 const ELFFile<ELF64BE>::Elf_Rel &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000831
Rafael Espindolac159c962015-10-19 21:00:02 +0000832template ELFFile<ELF32LE>::uintX_t
833getLocalRelTarget(const ObjectFile<ELF32LE> &,
834 const ELFFile<ELF32LE>::Elf_Rela &);
835template ELFFile<ELF32BE>::uintX_t
836getLocalRelTarget(const ObjectFile<ELF32BE> &,
837 const ELFFile<ELF32BE>::Elf_Rela &);
838template ELFFile<ELF64LE>::uintX_t
839getLocalRelTarget(const ObjectFile<ELF64LE> &,
840 const ELFFile<ELF64LE>::Elf_Rela &);
841template ELFFile<ELF64BE>::uintX_t
842getLocalRelTarget(const ObjectFile<ELF64BE> &,
843 const ELFFile<ELF64BE>::Elf_Rela &);
844
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000845template bool includeInSymtab<ELF32LE>(const SymbolBody &);
846template bool includeInSymtab<ELF32BE>(const SymbolBody &);
847template bool includeInSymtab<ELF64LE>(const SymbolBody &);
848template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000849
Rafael Espindola444576d2015-10-09 19:25:07 +0000850template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
851 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000852 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000853template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
854 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000855 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000856template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
857 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000858 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000859template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
860 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000861 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000862}
863}