blob: a2096c6c9ba7f41c77a48327c46d4d510521b035 [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 Ueyama15ef5e12015-10-07 19:18:16 +000023template <> DynamicSection<ELF32BE> *Out<ELF32BE>::Dynamic = nullptr;
24template <> DynamicSection<ELF32LE> *Out<ELF32LE>::Dynamic = nullptr;
25template <> DynamicSection<ELF64BE> *Out<ELF64BE>::Dynamic = nullptr;
26template <> DynamicSection<ELF64LE> *Out<ELF64LE>::Dynamic = nullptr;
27template <> GotSection<ELF32BE> *Out<ELF32BE>::Got = nullptr;
28template <> GotSection<ELF32LE> *Out<ELF32LE>::Got = nullptr;
29template <> GotSection<ELF64BE> *Out<ELF64BE>::Got = nullptr;
30template <> GotSection<ELF64LE> *Out<ELF64LE>::Got = nullptr;
31template <> HashTableSection<ELF32BE> *Out<ELF32BE>::HashTab = nullptr;
32template <> HashTableSection<ELF32LE> *Out<ELF32LE>::HashTab = nullptr;
33template <> HashTableSection<ELF64BE> *Out<ELF64BE>::HashTab = nullptr;
34template <> HashTableSection<ELF64LE> *Out<ELF64LE>::HashTab = nullptr;
35template <> InterpSection<false> *Out<ELF32BE>::Interp = nullptr;
36template <> InterpSection<false> *Out<ELF32LE>::Interp = nullptr;
37template <> InterpSection<true> *Out<ELF64BE>::Interp = nullptr;
38template <> InterpSection<true> *Out<ELF64LE>::Interp = nullptr;
39template <> OutputSection<ELF32BE> *Out<ELF32BE>::Bss = nullptr;
40template <> OutputSection<ELF32LE> *Out<ELF32LE>::Bss = nullptr;
41template <> OutputSection<ELF64BE> *Out<ELF64BE>::Bss = nullptr;
42template <> OutputSection<ELF64LE> *Out<ELF64LE>::Bss = nullptr;
43template <> PltSection<ELF32BE> *Out<ELF32BE>::Plt = nullptr;
44template <> PltSection<ELF32LE> *Out<ELF32LE>::Plt = nullptr;
45template <> PltSection<ELF64BE> *Out<ELF64BE>::Plt = nullptr;
46template <> PltSection<ELF64LE> *Out<ELF64LE>::Plt = nullptr;
47template <> RelocationSection<ELF32BE> *Out<ELF32BE>::RelaDyn = nullptr;
48template <> RelocationSection<ELF32LE> *Out<ELF32LE>::RelaDyn = nullptr;
49template <> RelocationSection<ELF64BE> *Out<ELF64BE>::RelaDyn = nullptr;
50template <> RelocationSection<ELF64LE> *Out<ELF64LE>::RelaDyn = nullptr;
51template <> StringTableSection<false> *Out<ELF32BE>::DynStrTab = nullptr;
52template <> StringTableSection<false> *Out<ELF32LE>::DynStrTab = nullptr;
53template <> StringTableSection<true> *Out<ELF64BE>::DynStrTab = nullptr;
54template <> StringTableSection<true> *Out<ELF64LE>::DynStrTab = nullptr;
55template <> StringTableSection<false> *Out<ELF32BE>::StrTab = nullptr;
56template <> StringTableSection<false> *Out<ELF32LE>::StrTab = nullptr;
57template <> StringTableSection<true> *Out<ELF64BE>::StrTab = nullptr;
58template <> StringTableSection<true> *Out<ELF64LE>::StrTab = nullptr;
59template <> SymbolTableSection<ELF32BE> *Out<ELF32BE>::DynSymTab = nullptr;
60template <> SymbolTableSection<ELF32LE> *Out<ELF32LE>::DynSymTab = nullptr;
61template <> SymbolTableSection<ELF64BE> *Out<ELF64BE>::DynSymTab = nullptr;
62template <> SymbolTableSection<ELF64LE> *Out<ELF64LE>::DynSymTab = nullptr;
63template <> SymbolTableSection<ELF32BE> *Out<ELF32BE>::SymTab = nullptr;
64template <> SymbolTableSection<ELF32LE> *Out<ELF32LE>::SymTab = nullptr;
65template <> SymbolTableSection<ELF64BE> *Out<ELF64BE>::SymTab = nullptr;
66template <> SymbolTableSection<ELF64LE> *Out<ELF64LE>::SymTab = nullptr;
67
Rafael Espindola5805c4f2015-09-21 21:38:08 +000068template <bool Is64Bits>
69OutputSectionBase<Is64Bits>::OutputSectionBase(StringRef Name, uint32_t sh_type,
70 uintX_t sh_flags)
71 : Name(Name) {
72 memset(&Header, 0, sizeof(HeaderT));
73 Header.sh_type = sh_type;
74 Header.sh_flags = sh_flags;
75}
76
Rafael Espindola35c6af32015-09-25 17:19:10 +000077template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000078GotSection<ELFT>::GotSection()
Rafael Espindola35c6af32015-09-25 17:19:10 +000079 : OutputSectionBase<ELFT::Is64Bits>(".got", llvm::ELF::SHT_PROGBITS,
80 llvm::ELF::SHF_ALLOC |
Rui Ueyama15ef5e12015-10-07 19:18:16 +000081 llvm::ELF::SHF_WRITE) {
Rafael Espindola35c6af32015-09-25 17:19:10 +000082 this->Header.sh_addralign = this->getAddrSize();
83}
84
Rafael Espindola5805c4f2015-09-21 21:38:08 +000085template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
86 Sym->setGotIndex(Entries.size());
87 Entries.push_back(Sym);
88}
89
90template <class ELFT>
91typename GotSection<ELFT>::uintX_t
92GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
93 return this->getVA() + B.getGotIndex() * this->getAddrSize();
94}
95
Rafael Espindolaa6627382015-10-06 23:56:53 +000096template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
97 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +000098 uint8_t *Entry = Buf;
99 Buf += sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000100 if (canBePreempted(B))
101 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000102 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +0000103 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000104 }
105}
106
Rafael Espindola35c6af32015-09-25 17:19:10 +0000107template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000108PltSection<ELFT>::PltSection()
Rafael Espindola35c6af32015-09-25 17:19:10 +0000109 : OutputSectionBase<ELFT::Is64Bits>(".plt", llvm::ELF::SHT_PROGBITS,
110 llvm::ELF::SHF_ALLOC |
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000111 llvm::ELF::SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000112 this->Header.sh_addralign = 16;
113}
114
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000115template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
116 uintptr_t Start = reinterpret_cast<uintptr_t>(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000117 for (const SymbolBody *E : Entries) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000118 uint64_t GotEntryAddr = Out<ELFT>::Got->getEntryAddr(*E);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000119 uintptr_t InstPos = reinterpret_cast<uintptr_t>(Buf);
Rafael Espindola01205f72015-09-22 18:19:46 +0000120 uint64_t PltEntryAddr = (InstPos - Start) + this->getVA();
121 Target->writePltEntry(Buf, GotEntryAddr, PltEntryAddr);
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000122 Buf += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123 }
124}
125
126template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
127 Sym->setPltIndex(Entries.size());
128 Entries.push_back(Sym);
129}
130
131template <class ELFT>
132typename PltSection<ELFT>::uintX_t
133PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000134 return this->getVA() + B.getPltIndex() * Target->getPltEntrySize();
135}
136
137template <class ELFT>
138void PltSection<ELFT>::finalize() {
139 this->Header.sh_size = Entries.size() * Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000140}
141
Rafael Espindola35c6af32015-09-25 17:19:10 +0000142template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000143RelocationSection<ELFT>::RelocationSection(bool IsRela)
Rafael Espindola35c6af32015-09-25 17:19:10 +0000144 : OutputSectionBase<ELFT::Is64Bits>(IsRela ? ".rela.dyn" : ".rel.dyn",
145 IsRela ? llvm::ELF::SHT_RELA
146 : llvm::ELF::SHT_REL,
147 llvm::ELF::SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000148 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000149 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
150 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
151}
152
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000153template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000154 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000155 bool IsMips64EL = Relocs[0].C.getFile()->getObj().isMips64EL();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000156 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000157 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
158 Buf += EntrySize;
159
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000160 const InputSection<ELFT> &C = Rel.C;
161 const Elf_Rel &RI = Rel.RI;
Rui Ueyamab4908762015-10-07 17:04:18 +0000162 OutputSection<ELFT> *OutSec = C.getOutputSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163 uint32_t SymIndex = RI.getSymbol(IsMips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000164 const ObjectFile<ELFT> &File = *C.getFile();
165 const SymbolBody *Body = File.getSymbolBody(SymIndex);
166 const ELFFile<ELFT> &Obj = File.getObj();
167
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168 uint32_t Type = RI.getType(IsMips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000169
170 bool CanBePreempted = canBePreempted(Body);
171 uintX_t Addend = 0;
172 if (!CanBePreempted) {
173 if (IsRela) {
174 if (Body)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000175 Addend += getSymVA<ELFT>(cast<ELFSymbolBody<ELFT>>(*Body));
Rafael Espindola52dca342015-10-07 00:58:20 +0000176 else
177 Addend += getLocalSymVA(
178 Obj.getRelocationSymbol(&RI, File.getSymbolTable()), File);
179 }
180 P->setSymbolAndType(0, Target->getRelativeReloc(), IsMips64EL);
181 }
182
Rafael Espindolaae244002015-10-05 19:30:12 +0000183 if (Body && Target->relocNeedsGot(Type, *Body)) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000184 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
Rafael Espindola52dca342015-10-07 00:58:20 +0000185 if (CanBePreempted)
186 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
187 Target->getGotReloc(), IsMips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000188 } else {
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000189 if (IsRela)
Rafael Espindola52dca342015-10-07 00:58:20 +0000190 Addend += static_cast<const Elf_Rela &>(RI).r_addend;
Rui Ueyamab4908762015-10-07 17:04:18 +0000191 P->r_offset = RI.r_offset + C.getOutputSectionOff() + OutSec->getVA();
Rafael Espindola52dca342015-10-07 00:58:20 +0000192 if (CanBePreempted)
Rafael Espindolaae244002015-10-05 19:30:12 +0000193 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type,
194 IsMips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000195 }
Rafael Espindola52dca342015-10-07 00:58:20 +0000196
197 if (IsRela)
198 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199 }
200}
201
202template <class ELFT> void RelocationSection<ELFT>::finalize() {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000203 this->Header.sh_link = Out<ELFT>::DynSymTab->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000204 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
205}
206
207template <bool Is64Bits>
208InterpSection<Is64Bits>::InterpSection()
209 : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS,
210 llvm::ELF::SHF_ALLOC) {
211 this->Header.sh_size = Config->DynamicLinker.size() + 1;
212 this->Header.sh_addralign = 1;
213}
214
215template <bool Is64Bits>
216template <endianness E>
217void OutputSectionBase<Is64Bits>::writeHeaderTo(
218 typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) {
219 SHdr->sh_name = Header.sh_name;
220 SHdr->sh_type = Header.sh_type;
221 SHdr->sh_flags = Header.sh_flags;
222 SHdr->sh_addr = Header.sh_addr;
223 SHdr->sh_offset = Header.sh_offset;
224 SHdr->sh_size = Header.sh_size;
225 SHdr->sh_link = Header.sh_link;
226 SHdr->sh_info = Header.sh_info;
227 SHdr->sh_addralign = Header.sh_addralign;
228 SHdr->sh_entsize = Header.sh_entsize;
229}
230
231template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) {
232 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
233}
234
Rafael Espindola35c6af32015-09-25 17:19:10 +0000235template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000236HashTableSection<ELFT>::HashTableSection()
Rafael Espindola35c6af32015-09-25 17:19:10 +0000237 : OutputSectionBase<ELFT::Is64Bits>(".hash", llvm::ELF::SHT_HASH,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000238 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000239 this->Header.sh_entsize = sizeof(Elf_Word);
240 this->Header.sh_addralign = sizeof(Elf_Word);
241}
242
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
244 StringRef Name = S->getName();
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000245 Out<ELFT>::DynSymTab->addSymbol(Name);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000246 Hashes.push_back(hash(Name));
247 S->setDynamicSymbolTableIndex(Hashes.size());
248}
249
Rui Ueyama0db335f2015-10-07 16:58:54 +0000250template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000251 this->Header.sh_link = Out<ELFT>::DynSymTab->getSectionIndex();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000252
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000253 assert(Out<ELFT>::DynSymTab->getNumSymbols() == Hashes.size() + 1);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000254 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000255 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000256
257 // Create as many buckets as there are symbols.
258 // FIXME: This is simplistic. We can try to optimize it, but implementing
259 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000260 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000261 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
262}
263
264template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000265 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000266 auto *P = reinterpret_cast<Elf_Word *>(Buf);
267 *P++ = NumSymbols; // nbucket
268 *P++ = NumSymbols; // nchain
269
270 Elf_Word *Buckets = P;
271 Elf_Word *Chains = P + NumSymbols;
272
273 for (unsigned I = 1; I < NumSymbols; ++I) {
274 uint32_t Hash = Hashes[I - 1] % NumSymbols;
275 Chains[I] = Buckets[Hash];
276 Buckets[Hash] = I;
277 }
278}
279
Rafael Espindola35c6af32015-09-25 17:19:10 +0000280template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000281DynamicSection<ELFT>::DynamicSection(SymbolTable &SymTab)
Rafael Espindola35c6af32015-09-25 17:19:10 +0000282 : OutputSectionBase<ELFT::Is64Bits>(".dynamic", llvm::ELF::SHT_DYNAMIC,
283 llvm::ELF::SHF_ALLOC |
284 llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000285 SymTab(SymTab) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000286 typename Base::HeaderT &Header = this->Header;
287 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
288 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
289}
290
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000291template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000292 if (this->Header.sh_size)
293 return; // Already finalized.
294
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000295 typename Base::HeaderT &Header = this->Header;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000296 Header.sh_link = Out<ELFT>::DynStrTab->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000297
298 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000299 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000300 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000301 ++NumEntries; // DT_RELASZ / DT_RELSZ
302 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000303 }
304 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000305 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000306 ++NumEntries; // DT_STRTAB
307 ++NumEntries; // DT_STRSZ
308 ++NumEntries; // DT_HASH
309
Rui Ueyama7de3f372015-10-01 19:36:04 +0000310 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000311 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000312 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000313 }
314
315 if (!Config->SoName.empty()) {
316 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000317 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000318 }
319
Rafael Espindola77572242015-10-02 19:37:55 +0000320 if (PreInitArraySec)
321 NumEntries += 2;
322 if (InitArraySec)
323 NumEntries += 2;
324 if (FiniArraySec)
325 NumEntries += 2;
326
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000327 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
328 SymTab.getSharedFiles();
329 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000330 Out<ELFT>::DynStrTab->add(File->getSoName());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000331 NumEntries += SharedFiles.size();
332
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000333 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
334 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
335 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
336 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000337 if (InitSym)
338 ++NumEntries; // DT_INIT
339 if (FiniSym)
340 ++NumEntries; // DT_FINI
George Rimar97aad172015-10-07 15:00:21 +0000341 if (Config->ZNow)
342 ++NumEntries; // DT_FLAGS_1
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000343
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000344 ++NumEntries; // DT_NULL
345
346 Header.sh_size = NumEntries * Header.sh_entsize;
347}
348
349template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000350 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
351
Rui Ueyama8c205d52015-10-02 01:33:31 +0000352 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
353 P->d_tag = Tag;
354 P->d_un.d_ptr = Val;
355 ++P;
356 };
357
358 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
359 P->d_tag = Tag;
360 P->d_un.d_val = Val;
361 ++P;
362 };
363
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000364 if (Out<ELFT>::RelaDyn->hasRelocs()) {
365 bool IsRela = Out<ELFT>::RelaDyn->isRela();
366 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
367 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000368 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
369 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000370 }
371
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000372 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000373 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000374 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
375 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
376 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000377
Rui Ueyama8c205d52015-10-02 01:33:31 +0000378 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000379
380 // If --enable-new-dtags is set lld emits DT_RUNPATH
381 // instead of DT_RPATH. The two tags are functionally
382 // equivalent except for the following:
383 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
384 // DT_RPATH is searched before.
385 // - DT_RUNPATH is used only to search for direct
386 // dependencies of the object it's contained in, while
387 // DT_RPATH is used for indirect dependencies as well.
388 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000389 Out<ELFT>::DynStrTab->getFileOff(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000390
Rui Ueyama8c205d52015-10-02 01:33:31 +0000391 if (!Config->SoName.empty())
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000392 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getFileOff(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000393
Rafael Espindola77572242015-10-02 19:37:55 +0000394 auto WriteArray = [&](int32_t T1, int32_t T2,
395 const OutputSection<ELFT> *Sec) {
396 if (!Sec)
397 return;
398 WritePtr(T1, Sec->getVA());
399 WriteVal(T2, Sec->getSize());
400 };
401 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
402 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
403 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
404
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000405 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
406 SymTab.getSharedFiles();
Rui Ueyama8c205d52015-10-02 01:33:31 +0000407 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000408 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getFileOff(File->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000409
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000410 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000411 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000412 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000413 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000414
George Rimar97aad172015-10-07 15:00:21 +0000415 if (Config->ZNow)
416 WriteVal(DT_FLAGS_1, DF_1_NOW);
417
Rui Ueyama8c205d52015-10-02 01:33:31 +0000418 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000419}
420
421template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000422OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000423 uintX_t sh_flags)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000424 : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000425
426template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000427void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
428 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000429 C->setOutputSection(this);
430 uint32_t Align = C->getAlign();
431 if (Align > this->Header.sh_addralign)
432 this->Header.sh_addralign = Align;
433
434 uintX_t Off = this->Header.sh_size;
435 Off = RoundUpToAlignment(Off, Align);
436 C->setOutputSectionOff(Off);
437 Off += C->getSize();
438 this->Header.sh_size = Off;
439}
440
441template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000442typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000443 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000444 case SymbolBody::DefinedSyntheticKind: {
445 auto &D = cast<DefinedSynthetic<ELFT>>(S);
446 return D.Section.getVA() + D.Sym.st_value;
447 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000448 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000449 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000450 case SymbolBody::DefinedRegularKind: {
451 const auto &DR = cast<DefinedRegular<ELFT>>(S);
452 const InputSection<ELFT> *SC = &DR.Section;
453 OutputSection<ELFT> *OS = SC->getOutputSection();
454 return OS->getVA() + SC->getOutputSectionOff() + DR.Sym.st_value;
455 }
456 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000457 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000458 case SymbolBody::SharedKind:
459 case SymbolBody::UndefinedKind:
460 return 0;
461 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000462 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
463 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000464 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000465 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000466}
467
468template <class ELFT>
469typename ELFFile<ELFT>::uintX_t
470lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym,
471 const ObjectFile<ELFT> &File) {
472 uint32_t SecIndex = Sym->st_shndx;
473
474 if (SecIndex == SHN_XINDEX)
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000475 SecIndex = File.getObj().getExtendedSymbolTableIndex(
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000476 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000477 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
478 InputSection<ELFT> *Section = Sections[SecIndex];
Rui Ueyamab4908762015-10-07 17:04:18 +0000479 OutputSection<ELFT> *OutSec = Section->getOutputSection();
480 return OutSec->getVA() + Section->getOutputSectionOff() + Sym->st_value;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000481}
482
Rafael Espindolaa6627382015-10-06 23:56:53 +0000483bool lld::elf2::canBePreempted(const SymbolBody *Body) {
484 if (!Body)
485 return false;
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000486 if (Body->isShared())
487 return true;
488 if (Body->isUndefined() && !Body->isWeak())
Rafael Espindolaa6627382015-10-06 23:56:53 +0000489 return true;
490 if (!Config->Shared)
491 return false;
Rafael Espindola52dca342015-10-07 00:58:20 +0000492 return Body->getMostConstrainingVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000493}
494
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000495template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000496 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000497 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000498}
499
500template <bool Is64Bits>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000501StringTableSection<Is64Bits>::StringTableSection(bool Dynamic)
502 : OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab",
503 llvm::ELF::SHT_STRTAB,
504 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
505 Dynamic(Dynamic) {
506 this->Header.sh_addralign = 1;
507}
508
509template <bool Is64Bits>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000510void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
511 StringRef Data = StrTabBuilder.data();
512 memcpy(Buf, Data.data(), Data.size());
513}
514
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000515template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000516 if (!B.isUsedInRegularObj())
517 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000518
519 // Don't include synthetic symbols like __init_array_start in every output.
520 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
521 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
522 return false;
523
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000524 return true;
525}
526
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000527bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000528 uint8_t V = B.getMostConstrainingVisibility();
529 if (V != STV_DEFAULT && V != STV_PROTECTED)
530 return false;
531
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000532 if (Config->ExportDynamic || Config->Shared)
533 return true;
534 return B.isUsedInDynamicReloc();
535}
536
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000537template <class ELFT>
538bool lld::elf2::shouldKeepInSymtab(StringRef SymName,
539 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
540 if (Sym.getType() == STT_SECTION)
541 return false;
542
Davide Italiano6993ba42015-09-26 00:47:56 +0000543 if (Config->DiscardNone)
544 return true;
545
546 // ELF defines dynamic locals as symbols which name starts with ".L".
547 return !(Config->DiscardLocals && SymName.startswith(".L"));
548}
549
Rafael Espindola35c6af32015-09-25 17:19:10 +0000550template <class ELFT>
551SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000552 SymbolTable &Table, StringTableSection<ELFT::Is64Bits> &StrTabSec)
Rafael Espindola35c6af32015-09-25 17:19:10 +0000553 : OutputSectionBase<ELFT::Is64Bits>(
554 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
555 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
556 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000557 Table(Table), StrTabSec(StrTabSec) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000558 typedef OutputSectionBase<ELFT::Is64Bits> Base;
559 typename Base::HeaderT &Header = this->Header;
560
561 Header.sh_entsize = sizeof(Elf_Sym);
562 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
563}
564
Rui Ueyama0db335f2015-10-07 16:58:54 +0000565template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
566 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
567 this->Header.sh_link = StrTabSec.getSectionIndex();
568 this->Header.sh_info = NumLocals + 1;
569}
570
571template <class ELFT>
572void SymbolTableSection<ELFT>::addSymbol(StringRef Name, bool isLocal) {
573 StrTabSec.add(Name);
574 ++NumVisible;
575 if (isLocal)
576 ++NumLocals;
577}
578
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000579template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000580 Buf += sizeof(Elf_Sym);
581
582 // All symbols with STB_LOCAL binding precede the weak and global symbols.
583 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000584 if (!Config->DiscardAll && !StrTabSec.isDynamic())
585 writeLocalSymbols(Buf);
586
587 writeGlobalSymbols(Buf);
588}
589
590template <class ELFT>
591void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
592 // Iterate over all input object files to copy their local symbols
593 // to the output symbol table pointed by Buf.
594 for (const std::unique_ptr<ObjectFileBase> &FileB : Table.getObjectFiles()) {
595 auto &File = cast<ObjectFile<ELFT>>(*FileB);
596 Elf_Sym_Range Syms = File.getLocalSymbols();
597 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000598 ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000599 if (SymName && !shouldKeepInSymtab<ELFT>(*SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000600 continue;
Rui Ueyamac55733e2015-09-30 00:54:29 +0000601 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
602 Buf += sizeof(*ESym);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000603 ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
604 ESym->st_size = Sym.st_size;
605 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
606 uint32_t SecIndex = Sym.st_shndx;
607 uintX_t VA = Sym.st_value;
608 if (SecIndex == SHN_ABS) {
609 ESym->st_shndx = SHN_ABS;
610 } else {
611 if (SecIndex == SHN_XINDEX)
612 SecIndex = File.getObj().getExtendedSymbolTableIndex(
613 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
614 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
615 const InputSection<ELFT> *Section = Sections[SecIndex];
Rui Ueyamab4908762015-10-07 17:04:18 +0000616 const OutputSection<ELFT> *OutSec = Section->getOutputSection();
617 ESym->st_shndx = OutSec->getSectionIndex();
618 VA += OutSec->getVA() + Section->getOutputSectionOff();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000619 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000620 ESym->st_value = VA;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000621 }
622 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000623}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000624
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000625template <class ELFT>
626void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *&Buf) {
627 // Write the internal symbol table contents to the output symbol table
628 // pointed by Buf.
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000629 uint8_t *Start = Buf;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000630 for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000631 StringRef Name = P.first;
632 Symbol *Sym = P.second;
633 SymbolBody *Body = Sym->Body;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000634 if (!includeInSymtab<ELFT>(*Body))
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000635 continue;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000636 if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body))
637 continue;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000638
639 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac55733e2015-09-30 00:54:29 +0000640 Buf += sizeof(*ESym);
Rafael Espindola8614c562015-10-06 14:33:58 +0000641
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000642 ESym->st_name = StrTabSec.getFileOff(Name);
643
Rui Ueyamab4908762015-10-07 17:04:18 +0000644 const OutputSection<ELFT> *OutSec = nullptr;
Rafael Espindola25b0acb2015-09-25 17:32:37 +0000645 const InputSection<ELFT> *Section = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000646
Rafael Espindola8614c562015-10-06 14:33:58 +0000647 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000648 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +0000649 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000650 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000651 case SymbolBody::DefinedRegularKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000652 Section = &cast<DefinedRegular<ELFT>>(Body)->Section;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000653 break;
654 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000655 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000656 break;
657 case SymbolBody::UndefinedKind:
658 case SymbolBody::DefinedAbsoluteKind:
659 case SymbolBody::SharedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000660 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000661 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000662 }
663
Rafael Espindola8614c562015-10-06 14:33:58 +0000664 unsigned char Binding = Body->isWeak() ? STB_WEAK : STB_GLOBAL;
665 unsigned char Type = STT_NOTYPE;
666 uintX_t Size = 0;
667 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
668 const Elf_Sym &InputSym = EBody->Sym;
669 Binding = InputSym.getBinding();
670 Type = InputSym.getType();
671 Size = InputSym.st_size;
672 }
673
674 unsigned char Visibility = Body->getMostConstrainingVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000675 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
676 Binding = STB_LOCAL;
677
Rafael Espindola8614c562015-10-06 14:33:58 +0000678 ESym->setBindingAndType(Binding, Type);
679 ESym->st_size = Size;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000680 ESym->setVisibility(Visibility);
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000681 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000682
683 if (Section)
Rui Ueyamab4908762015-10-07 17:04:18 +0000684 OutSec = Section->getOutputSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000685
Rafael Espindola8614c562015-10-06 14:33:58 +0000686 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +0000687 ESym->st_shndx = SHN_ABS;
Rui Ueyamab4908762015-10-07 17:04:18 +0000688 else if (OutSec)
689 ESym->st_shndx = OutSec->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000690 }
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000691 if (!StrTabSec.isDynamic())
692 std::stable_sort(
693 reinterpret_cast<Elf_Sym *>(Start), reinterpret_cast<Elf_Sym *>(Buf),
694 [](const Elf_Sym &A, const Elf_Sym &B) -> bool {
695 return A.getBinding() == STB_LOCAL && B.getBinding() != STB_LOCAL;
696 });
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000697}
698
699namespace lld {
700namespace elf2 {
701template class OutputSectionBase<false>;
702template class OutputSectionBase<true>;
703
704template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000705 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000706template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000707 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000708template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000709 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000710template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000711 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000712
713template class GotSection<ELF32LE>;
714template class GotSection<ELF32BE>;
715template class GotSection<ELF64LE>;
716template class GotSection<ELF64BE>;
717
718template class PltSection<ELF32LE>;
719template class PltSection<ELF32BE>;
720template class PltSection<ELF64LE>;
721template class PltSection<ELF64BE>;
722
723template class RelocationSection<ELF32LE>;
724template class RelocationSection<ELF32BE>;
725template class RelocationSection<ELF64LE>;
726template class RelocationSection<ELF64BE>;
727
728template class InterpSection<false>;
729template class InterpSection<true>;
730
731template class HashTableSection<ELF32LE>;
732template class HashTableSection<ELF32BE>;
733template class HashTableSection<ELF64LE>;
734template class HashTableSection<ELF64BE>;
735
736template class DynamicSection<ELF32LE>;
737template class DynamicSection<ELF32BE>;
738template class DynamicSection<ELF64LE>;
739template class DynamicSection<ELF64BE>;
740
741template class OutputSection<ELF32LE>;
742template class OutputSection<ELF32BE>;
743template class OutputSection<ELF64LE>;
744template class OutputSection<ELF64BE>;
745
746template class StringTableSection<false>;
747template class StringTableSection<true>;
748
749template class SymbolTableSection<ELF32LE>;
750template class SymbolTableSection<ELF32BE>;
751template class SymbolTableSection<ELF64LE>;
752template class SymbolTableSection<ELF64BE>;
753
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000754template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
755template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
756template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
757template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000758
Rafael Espindola56f965f2015-09-21 22:48:12 +0000759template ELFFile<ELF32LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000760getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *, const ObjectFile<ELF32LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000761
Rafael Espindola56f965f2015-09-21 22:48:12 +0000762template ELFFile<ELF32BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000763getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *, const ObjectFile<ELF32BE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000764
Rafael Espindola56f965f2015-09-21 22:48:12 +0000765template ELFFile<ELF64LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000766getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *, const ObjectFile<ELF64LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000767
Rafael Espindola56f965f2015-09-21 22:48:12 +0000768template ELFFile<ELF64BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000769getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *, const ObjectFile<ELF64BE> &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000770
771template bool includeInSymtab<ELF32LE>(const SymbolBody &);
772template bool includeInSymtab<ELF32BE>(const SymbolBody &);
773template bool includeInSymtab<ELF64LE>(const SymbolBody &);
774template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000775
776template bool shouldKeepInSymtab<ELF32LE>(StringRef,
777 const ELFFile<ELF32LE>::Elf_Sym &);
778template bool shouldKeepInSymtab<ELF32BE>(StringRef,
779 const ELFFile<ELF32BE>::Elf_Sym &);
780template bool shouldKeepInSymtab<ELF64LE>(StringRef,
781 const ELFFile<ELF64LE>::Elf_Sym &);
782template bool shouldKeepInSymtab<ELF64BE>(StringRef,
783 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000784}
785}