blob: 98801023d6de8a97204bb7ff25fccb226b639648 [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 Espindola52dca342015-10-07 00:58:20 +0000119
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000120 bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
121 bool CanBePreempted = canBePreempted(Body, NeedsGot);
Rafael Espindola52dca342015-10-07 00:58:20 +0000122 uintX_t Addend = 0;
123 if (!CanBePreempted) {
124 if (IsRela) {
125 if (Body)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000126 Addend += getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body));
Rafael Espindola52dca342015-10-07 00:58:20 +0000127 else
Hal Finkel230c5c52015-10-16 22:37:32 +0000128 Addend += getLocalRelTarget(File, RI);
Rafael Espindola52dca342015-10-07 00:58:20 +0000129 }
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 Espindolacc6ebb82015-10-14 18:42:16 +0000133 if (NeedsGot) {
Rui Ueyamac58656c2015-10-13 16:59:30 +0000134 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
Rafael Espindola52dca342015-10-07 00:58:20 +0000135 if (CanBePreempted)
136 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
Rui Ueyama64558522015-10-16 22:51:43 +0000137 Target->getGotReloc(), Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000138 } else {
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000139 if (IsRela)
Rafael Espindola52dca342015-10-07 00:58:20 +0000140 Addend += static_cast<const Elf_Rela &>(RI).r_addend;
Rui Ueyama55c3f892015-10-15 01:58:40 +0000141 P->r_offset = RI.r_offset + C.OutSec->getVA() + C.OutSecOff;
Rafael Espindola52dca342015-10-07 00:58:20 +0000142 if (CanBePreempted)
Rafael Espindolaae244002015-10-05 19:30:12 +0000143 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type,
Rui Ueyama64558522015-10-16 22:51:43 +0000144 Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000145 }
Rafael Espindola52dca342015-10-07 00:58:20 +0000146
147 if (IsRela)
148 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000149 }
150}
151
152template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000153 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000154 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
155}
156
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000157template <class ELFT>
158InterpSection<ELFT>::InterpSection()
159 : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
160 llvm::ELF::SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000161 this->Header.sh_size = Config->DynamicLinker.size() + 1;
162 this->Header.sh_addralign = 1;
163}
164
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000165template <class ELFT>
166void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
167 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168}
169
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000170template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000171 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
172}
173
Rafael Espindola35c6af32015-09-25 17:19:10 +0000174template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000175HashTableSection<ELFT>::HashTableSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000176 : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
177 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000178 this->Header.sh_entsize = sizeof(Elf_Word);
179 this->Header.sh_addralign = sizeof(Elf_Word);
180}
181
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000182static uint32_t hash(StringRef Name) {
183 uint32_t H = 0;
184 for (char C : Name) {
185 H = (H << 4) + C;
186 uint32_t G = H & 0xf0000000;
187 if (G)
188 H ^= G >> 24;
189 H &= ~G;
190 }
191 return H;
192}
193
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000194template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
195 StringRef Name = S->getName();
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000196 Out<ELFT>::DynSymTab->addSymbol(Name);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197 Hashes.push_back(hash(Name));
198 S->setDynamicSymbolTableIndex(Hashes.size());
199}
200
Rui Ueyama0db335f2015-10-07 16:58:54 +0000201template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000202 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000203
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000204 assert(Out<ELFT>::DynSymTab->getNumSymbols() == Hashes.size() + 1);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000205 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000206 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000207
208 // Create as many buckets as there are symbols.
209 // FIXME: This is simplistic. We can try to optimize it, but implementing
210 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000211 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000212 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
213}
214
215template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000216 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000217 auto *P = reinterpret_cast<Elf_Word *>(Buf);
218 *P++ = NumSymbols; // nbucket
219 *P++ = NumSymbols; // nchain
220
221 Elf_Word *Buckets = P;
222 Elf_Word *Chains = P + NumSymbols;
223
224 for (unsigned I = 1; I < NumSymbols; ++I) {
225 uint32_t Hash = Hashes[I - 1] % NumSymbols;
226 Chains[I] = Buckets[Hash];
227 Buckets[Hash] = I;
228 }
229}
230
Rafael Espindola35c6af32015-09-25 17:19:10 +0000231template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000232DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000233 : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
234 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000235 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000236 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000237 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
238 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
239}
240
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000242 if (this->Header.sh_size)
243 return; // Already finalized.
244
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000245 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000246 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247
248 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000249 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000250 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000251 ++NumEntries; // DT_RELASZ / DT_RELSZ
252 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253 }
254 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000255 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256 ++NumEntries; // DT_STRTAB
257 ++NumEntries; // DT_STRSZ
258 ++NumEntries; // DT_HASH
259
Rui Ueyama7de3f372015-10-01 19:36:04 +0000260 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000261 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000262 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000263 }
264
265 if (!Config->SoName.empty()) {
266 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000267 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000268 }
269
Rafael Espindola77572242015-10-02 19:37:55 +0000270 if (PreInitArraySec)
271 NumEntries += 2;
272 if (InitArraySec)
273 NumEntries += 2;
274 if (FiniArraySec)
275 NumEntries += 2;
276
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000277 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000278 if (!F->isNeeded())
279 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000280 Out<ELFT>::DynStrTab->add(F->getSoName());
281 ++NumEntries;
282 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000283
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000284 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
285 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
286 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
287 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000288 if (InitSym)
289 ++NumEntries; // DT_INIT
290 if (FiniSym)
291 ++NumEntries; // DT_FINI
Davide Italianocebb4492015-10-13 21:02:34 +0000292 if (Config->ZNow || Config->Bsymbolic)
George Rimar97aad172015-10-07 15:00:21 +0000293 ++NumEntries; // DT_FLAGS_1
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000294 ++NumEntries; // DT_NULL
295
296 Header.sh_size = NumEntries * Header.sh_entsize;
297}
298
299template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000300 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
301
Rui Ueyama8c205d52015-10-02 01:33:31 +0000302 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
303 P->d_tag = Tag;
304 P->d_un.d_ptr = Val;
305 ++P;
306 };
307
308 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
309 P->d_tag = Tag;
310 P->d_un.d_val = Val;
311 ++P;
312 };
313
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000314 if (Out<ELFT>::RelaDyn->hasRelocs()) {
315 bool IsRela = Out<ELFT>::RelaDyn->isRela();
316 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
317 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000318 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
319 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000320 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000321
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000322 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000323 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000324 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
325 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
326 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000327
Rui Ueyama8c205d52015-10-02 01:33:31 +0000328 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000329
330 // If --enable-new-dtags is set lld emits DT_RUNPATH
331 // instead of DT_RPATH. The two tags are functionally
332 // equivalent except for the following:
333 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
334 // DT_RPATH is searched before.
335 // - DT_RUNPATH is used only to search for direct
336 // dependencies of the object it's contained in, while
337 // DT_RPATH is used for indirect dependencies as well.
338 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000339 Out<ELFT>::DynStrTab->getFileOff(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000340
Rui Ueyama8c205d52015-10-02 01:33:31 +0000341 if (!Config->SoName.empty())
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000342 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getFileOff(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000343
Rafael Espindola77572242015-10-02 19:37:55 +0000344 auto WriteArray = [&](int32_t T1, int32_t T2,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000345 const OutputSectionBase<ELFT> *Sec) {
Rafael Espindola77572242015-10-02 19:37:55 +0000346 if (!Sec)
347 return;
348 WritePtr(T1, Sec->getVA());
349 WriteVal(T2, Sec->getSize());
350 };
351 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
352 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
353 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
354
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000355 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000356 if (F->isNeeded())
357 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getFileOff(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000358
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000359 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000360 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000361 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000362 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000363
Davide Italianocebb4492015-10-13 21:02:34 +0000364 uint32_t Flags = 0;
365 if (Config->Bsymbolic)
366 Flags |= DF_SYMBOLIC;
George Rimar97aad172015-10-07 15:00:21 +0000367 if (Config->ZNow)
Davide Italianocebb4492015-10-13 21:02:34 +0000368 Flags |= DF_1_NOW;
369 if (Flags)
370 WriteVal(DT_FLAGS_1, Flags);
George Rimar97aad172015-10-07 15:00:21 +0000371
Rui Ueyama8c205d52015-10-02 01:33:31 +0000372 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000373}
374
375template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000376OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000377 uintX_t sh_flags)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000378 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000379
380template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000381void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
382 Sections.push_back(C);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000383 C->OutSec = this;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000384 uint32_t Align = C->getAlign();
385 if (Align > this->Header.sh_addralign)
386 this->Header.sh_addralign = Align;
387
388 uintX_t Off = this->Header.sh_size;
389 Off = RoundUpToAlignment(Off, Align);
Rui Ueyama55c3f892015-10-15 01:58:40 +0000390 C->OutSecOff = Off;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000391 Off += C->getSize();
392 this->Header.sh_size = Off;
393}
394
395template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000396typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000397 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000398 case SymbolBody::DefinedSyntheticKind: {
399 auto &D = cast<DefinedSynthetic<ELFT>>(S);
400 return D.Section.getVA() + D.Sym.st_value;
401 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000402 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000403 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000404 case SymbolBody::DefinedRegularKind: {
405 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindola80c94a72015-10-16 23:22:23 +0000406 const InputSection<ELFT> &SC = DR.Section;
407 return SC.OutSec->getVA() + SC.OutSecOff + DR.Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000408 }
409 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000410 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000411 case SymbolBody::SharedKind:
412 case SymbolBody::UndefinedKind:
413 return 0;
414 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000415 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
416 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000417 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000418 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000419}
420
Rui Ueyama34f29242015-10-13 19:51:57 +0000421// Returns a VA which a relocatin RI refers to. Used only for local symbols.
422// For non-local symbols, use getSymVA instead.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000423template <class ELFT>
424typename ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000425lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
Hal Finkel230c5c52015-10-16 22:37:32 +0000426 const typename ELFFile<ELFT>::Elf_Rel &RI) {
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000427 // PPC64 has a special relocation representing the TOC base pointer
428 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000429 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000430 return getPPC64TocBase();
431
Rui Ueyama126d08f2015-10-12 20:28:22 +0000432 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
433 const Elf_Sym *Sym =
434 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000435
Rui Ueyama126d08f2015-10-12 20:28:22 +0000436 if (!Sym)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000437 error("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000438
Rafael Espindola444576d2015-10-09 19:25:07 +0000439 // According to the ELF spec reference to a local symbol from outside
440 // the group are not allowed. Unfortunately .eh_frame breaks that rule
441 // and must be treated specially. For now we just replace the symbol with
442 // 0.
Rafael Espindola4cda5812015-10-16 15:29:48 +0000443 InputSection<ELFT> *Section = File.getSection(*Sym);
Rafael Espindola444576d2015-10-09 19:25:07 +0000444 if (Section == &InputSection<ELFT>::Discarded)
445 return 0;
446
Rui Ueyama55c3f892015-10-15 01:58:40 +0000447 return Section->OutSec->getVA() + Section->OutSecOff + Sym->st_value;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000448}
449
Rui Ueyama34f29242015-10-13 19:51:57 +0000450// Returns true if a symbol can be replaced at load-time by a symbol
451// with the same name defined in other ELF executable or DSO.
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000452bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000453 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000454 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000455 if (Body->isShared())
456 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000457
458 if (Body->isUndefined()) {
459 if (!Body->isWeak())
460 return true;
461
462 // This is an horrible corner case. Ideally we would like to say that any
463 // undefined symbol can be preempted so that the dynamic linker has a
464 // chance of finding it at runtime.
465 //
466 // The problem is that the code sequence used to test for weak undef
467 // functions looks like
468 // if (func) func()
469 // If the code is -fPIC the first reference is a load from the got and
470 // everything works.
471 // If the code is not -fPIC there is no reasonable way to solve it:
472 // * A relocation writing to the text segment will fail (it is ro).
473 // * A copy relocation doesn't work for functions.
474 // * The trick of using a plt entry as the address would fail here since
475 // the plt entry would have a non zero address.
476 // Since we cannot do anything better, we just resolve the symbol to 0 and
477 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000478 //
479 // As an extra hack, assume that if we are producing a shared library the
480 // user knows what he or she is doing and can handle a dynamic relocation.
481 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000482 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000483 if (!Config->Shared)
484 return false;
Rafael Espindola52dca342015-10-07 00:58:20 +0000485 return Body->getMostConstrainingVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000486}
487
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000488template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000489 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000490 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000491}
492
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000493template <class ELFT>
494StringTableSection<ELFT>::StringTableSection(bool Dynamic)
495 : OutputSectionBase<ELFT>(Dynamic ? ".dynstr" : ".strtab",
496 llvm::ELF::SHT_STRTAB,
497 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +0000498 Dynamic(Dynamic) {
499 this->Header.sh_addralign = 1;
500}
501
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000502template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000503 StringRef Data = StrTabBuilder.data();
504 memcpy(Buf, Data.data(), Data.size());
505}
506
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000507template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000508 if (!B.isUsedInRegularObj())
509 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000510
511 // Don't include synthetic symbols like __init_array_start in every output.
512 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
513 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
514 return false;
515
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000516 return true;
517}
518
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000519bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000520 uint8_t V = B.getMostConstrainingVisibility();
521 if (V != STV_DEFAULT && V != STV_PROTECTED)
522 return false;
523
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000524 if (Config->ExportDynamic || Config->Shared)
525 return true;
526 return B.isUsedInDynamicReloc();
527}
528
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000529template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +0000530bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
531 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000532 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
533 if (Sym.getType() == STT_SECTION)
534 return false;
535
Rafael Espindola444576d2015-10-09 19:25:07 +0000536 // If sym references a section in a discarded group, don't keep it.
Rafael Espindola4cda5812015-10-16 15:29:48 +0000537 if (File.getSection(Sym) == &InputSection<ELFT>::Discarded)
538 return false;
Rafael Espindola444576d2015-10-09 19:25:07 +0000539
Davide Italiano6993ba42015-09-26 00:47:56 +0000540 if (Config->DiscardNone)
541 return true;
542
543 // ELF defines dynamic locals as symbols which name starts with ".L".
544 return !(Config->DiscardLocals && SymName.startswith(".L"));
545}
546
Rafael Espindola35c6af32015-09-25 17:19:10 +0000547template <class ELFT>
548SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000549 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
550 : OutputSectionBase<ELFT>(
Rafael Espindola35c6af32015-09-25 17:19:10 +0000551 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
552 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
553 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000554 Table(Table), StrTabSec(StrTabSec) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000555 typedef OutputSectionBase<ELFT> Base;
556 typename Base::Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000557
558 Header.sh_entsize = sizeof(Elf_Sym);
559 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
560}
561
Rui Ueyama0db335f2015-10-07 16:58:54 +0000562template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
563 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000564 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000565 this->Header.sh_info = NumLocals + 1;
566}
567
568template <class ELFT>
569void SymbolTableSection<ELFT>::addSymbol(StringRef Name, bool isLocal) {
570 StrTabSec.add(Name);
571 ++NumVisible;
572 if (isLocal)
573 ++NumLocals;
574}
575
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000576template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000577 Buf += sizeof(Elf_Sym);
578
579 // All symbols with STB_LOCAL binding precede the weak and global symbols.
580 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000581 if (!Config->DiscardAll && !StrTabSec.isDynamic())
582 writeLocalSymbols(Buf);
583
584 writeGlobalSymbols(Buf);
585}
586
587template <class ELFT>
588void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
589 // Iterate over all input object files to copy their local symbols
590 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000591 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
592 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000593 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000594 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +0000595 error(SymNameOrErr);
596 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000597 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000598 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +0000599
Rui Ueyamac55733e2015-09-30 00:54:29 +0000600 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
601 Buf += sizeof(*ESym);
Rafael Espindola26fd69d2015-10-09 16:15:57 +0000602 ESym->st_name = StrTabSec.getFileOff(SymName);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000603 ESym->st_size = Sym.st_size;
604 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000605 uintX_t VA = Sym.st_value;
Rafael Espindola4cda5812015-10-16 15:29:48 +0000606 if (Sym.st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000607 ESym->st_shndx = SHN_ABS;
608 } else {
Rafael Espindola4cda5812015-10-16 15:29:48 +0000609 const InputSection<ELFT> *Sec = File->getSection(Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000610 ESym->st_shndx = Sec->OutSec->SectionIndex;
Rui Ueyama55c3f892015-10-15 01:58:40 +0000611 VA += Sec->OutSec->getVA() + Sec->OutSecOff;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000612 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000613 ESym->st_value = VA;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000614 }
615 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000616}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000617
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000618template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +0000619void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000620 // Write the internal symbol table contents to the output symbol table
621 // pointed by Buf.
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000622 uint8_t *Start = Buf;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000623 for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000624 StringRef Name = P.first;
625 Symbol *Sym = P.second;
626 SymbolBody *Body = Sym->Body;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000627 if (!includeInSymtab<ELFT>(*Body))
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000628 continue;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000629 if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body))
630 continue;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000631
632 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac55733e2015-09-30 00:54:29 +0000633 Buf += sizeof(*ESym);
Rafael Espindola8614c562015-10-06 14:33:58 +0000634
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000635 ESym->st_name = StrTabSec.getFileOff(Name);
636
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000637 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola25b0acb2015-09-25 17:32:37 +0000638 const InputSection<ELFT> *Section = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000639
Rafael Espindola8614c562015-10-06 14:33:58 +0000640 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000641 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +0000642 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000643 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000644 case SymbolBody::DefinedRegularKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000645 Section = &cast<DefinedRegular<ELFT>>(Body)->Section;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000646 break;
647 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000648 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000649 break;
650 case SymbolBody::UndefinedKind:
651 case SymbolBody::DefinedAbsoluteKind:
652 case SymbolBody::SharedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000653 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000654 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000655 }
656
Rafael Espindola8614c562015-10-06 14:33:58 +0000657 unsigned char Binding = Body->isWeak() ? STB_WEAK : STB_GLOBAL;
658 unsigned char Type = STT_NOTYPE;
659 uintX_t Size = 0;
660 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
661 const Elf_Sym &InputSym = EBody->Sym;
662 Binding = InputSym.getBinding();
663 Type = InputSym.getType();
664 Size = InputSym.st_size;
665 }
666
667 unsigned char Visibility = Body->getMostConstrainingVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000668 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
669 Binding = STB_LOCAL;
670
Rafael Espindola8614c562015-10-06 14:33:58 +0000671 ESym->setBindingAndType(Binding, Type);
672 ESym->st_size = Size;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000673 ESym->setVisibility(Visibility);
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000674 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000675
676 if (Section)
Rui Ueyama55c3f892015-10-15 01:58:40 +0000677 OutSec = Section->OutSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000678
Rafael Espindola8614c562015-10-06 14:33:58 +0000679 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +0000680 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +0000681 else if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000682 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000683 }
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000684 if (!StrTabSec.isDynamic())
685 std::stable_sort(
686 reinterpret_cast<Elf_Sym *>(Start), reinterpret_cast<Elf_Sym *>(Buf),
687 [](const Elf_Sym &A, const Elf_Sym &B) -> bool {
688 return A.getBinding() == STB_LOCAL && B.getBinding() != STB_LOCAL;
689 });
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000690}
691
692namespace lld {
693namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000694template class OutputSectionBase<ELF32LE>;
695template class OutputSectionBase<ELF32BE>;
696template class OutputSectionBase<ELF64LE>;
697template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000698
699template class GotSection<ELF32LE>;
700template class GotSection<ELF32BE>;
701template class GotSection<ELF64LE>;
702template class GotSection<ELF64BE>;
703
704template class PltSection<ELF32LE>;
705template class PltSection<ELF32BE>;
706template class PltSection<ELF64LE>;
707template class PltSection<ELF64BE>;
708
709template class RelocationSection<ELF32LE>;
710template class RelocationSection<ELF32BE>;
711template class RelocationSection<ELF64LE>;
712template class RelocationSection<ELF64BE>;
713
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000714template class InterpSection<ELF32LE>;
715template class InterpSection<ELF32BE>;
716template class InterpSection<ELF64LE>;
717template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000718
719template class HashTableSection<ELF32LE>;
720template class HashTableSection<ELF32BE>;
721template class HashTableSection<ELF64LE>;
722template class HashTableSection<ELF64BE>;
723
724template class DynamicSection<ELF32LE>;
725template class DynamicSection<ELF32BE>;
726template class DynamicSection<ELF64LE>;
727template class DynamicSection<ELF64BE>;
728
729template class OutputSection<ELF32LE>;
730template class OutputSection<ELF32BE>;
731template class OutputSection<ELF64LE>;
732template class OutputSection<ELF64BE>;
733
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000734template class StringTableSection<ELF32LE>;
735template class StringTableSection<ELF32BE>;
736template class StringTableSection<ELF64LE>;
737template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000738
739template class SymbolTableSection<ELF32LE>;
740template class SymbolTableSection<ELF32BE>;
741template class SymbolTableSection<ELF64LE>;
742template class SymbolTableSection<ELF64BE>;
743
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000744template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
745template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
746template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
747template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000748
Rafael Espindola56f965f2015-09-21 22:48:12 +0000749template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000750getLocalRelTarget(const ObjectFile<ELF32LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +0000751 const ELFFile<ELF32LE>::Elf_Rel &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000752
Rafael Espindola56f965f2015-09-21 22:48:12 +0000753template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000754getLocalRelTarget(const ObjectFile<ELF32BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +0000755 const ELFFile<ELF32BE>::Elf_Rel &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000756
Rafael Espindola56f965f2015-09-21 22:48:12 +0000757template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000758getLocalRelTarget(const ObjectFile<ELF64LE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +0000759 const ELFFile<ELF64LE>::Elf_Rel &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000760
Rafael Espindola56f965f2015-09-21 22:48:12 +0000761template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000762getLocalRelTarget(const ObjectFile<ELF64BE> &,
Hal Finkel230c5c52015-10-16 22:37:32 +0000763 const ELFFile<ELF64BE>::Elf_Rel &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000764
765template bool includeInSymtab<ELF32LE>(const SymbolBody &);
766template bool includeInSymtab<ELF32BE>(const SymbolBody &);
767template bool includeInSymtab<ELF64LE>(const SymbolBody &);
768template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000769
Rafael Espindola444576d2015-10-09 19:25:07 +0000770template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
771 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000772 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000773template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
774 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000775 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000776template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
777 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000778 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +0000779template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
780 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000781 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000782}
783}