blob: ee2d87c1d100f3b9f1a1073fc0142c50a43da3b5 [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;
17using namespace llvm::ELF;
18
19using namespace lld;
20using namespace lld::elf2;
21
22template <bool Is64Bits>
23OutputSectionBase<Is64Bits>::OutputSectionBase(StringRef Name, uint32_t sh_type,
24 uintX_t sh_flags)
25 : Name(Name) {
26 memset(&Header, 0, sizeof(HeaderT));
27 Header.sh_type = sh_type;
28 Header.sh_flags = sh_flags;
29}
30
Rafael Espindola35c6af32015-09-25 17:19:10 +000031template <class ELFT>
32GotSection<ELFT>::GotSection()
33 : OutputSectionBase<ELFT::Is64Bits>(".got", llvm::ELF::SHT_PROGBITS,
34 llvm::ELF::SHF_ALLOC |
35 llvm::ELF::SHF_WRITE) {
36 this->Header.sh_addralign = this->getAddrSize();
37}
38
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
40 Sym->setGotIndex(Entries.size());
41 Entries.push_back(Sym);
42}
43
44template <class ELFT>
45typename GotSection<ELFT>::uintX_t
46GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
47 return this->getVA() + B.getGotIndex() * this->getAddrSize();
48}
49
Rafael Espindola35c6af32015-09-25 17:19:10 +000050template <class ELFT>
51PltSection<ELFT>::PltSection(const GotSection<ELFT> &GotSec)
52 : OutputSectionBase<ELFT::Is64Bits>(".plt", llvm::ELF::SHT_PROGBITS,
53 llvm::ELF::SHF_ALLOC |
54 llvm::ELF::SHF_EXECINSTR),
55 GotSec(GotSec) {
56 this->Header.sh_addralign = 16;
57}
58
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
60 uintptr_t Start = reinterpret_cast<uintptr_t>(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000061 for (const SymbolBody *E : Entries) {
Rafael Espindola01205f72015-09-22 18:19:46 +000062 uint64_t GotEntryAddr = GotSec.getEntryAddr(*E);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000063 uintptr_t InstPos = reinterpret_cast<uintptr_t>(Buf);
Rafael Espindola01205f72015-09-22 18:19:46 +000064 uint64_t PltEntryAddr = (InstPos - Start) + this->getVA();
65 Target->writePltEntry(Buf, GotEntryAddr, PltEntryAddr);
66 Buf += 8;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000067 }
68}
69
70template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
71 Sym->setPltIndex(Entries.size());
72 Entries.push_back(Sym);
73}
74
75template <class ELFT>
76typename PltSection<ELFT>::uintX_t
77PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
78 return this->getVA() + B.getPltIndex() * EntrySize;
79}
80
Rafael Espindola35c6af32015-09-25 17:19:10 +000081template <class ELFT>
82RelocationSection<ELFT>::RelocationSection(SymbolTableSection<ELFT> &DynSymSec,
83 const GotSection<ELFT> &GotSec,
Rafael Espindola9c3e4d22015-10-05 21:23:08 +000084 const OutputSection<ELFT> &BssSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +000085 bool IsRela)
86 : OutputSectionBase<ELFT::Is64Bits>(IsRela ? ".rela.dyn" : ".rel.dyn",
87 IsRela ? llvm::ELF::SHT_RELA
88 : llvm::ELF::SHT_REL,
89 llvm::ELF::SHF_ALLOC),
Rafael Espindola9c3e4d22015-10-05 21:23:08 +000090 DynSymSec(DynSymSec), GotSec(GotSec), BssSec(BssSec), IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +000091 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
92 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
93}
94
Rafael Espindola03ab3362015-10-06 12:39:58 +000095static bool isLocalDefinition(const SymbolBody *Body) {
96 if (!Body)
97 return true;
98 return !Body->isShared() && !Body->isUndefined();
99}
100
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000101template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000102 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000103 bool IsMips64EL = Relocs[0].C.getFile()->getObj().isMips64EL();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000105 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
106 Buf += EntrySize;
107
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000108 const InputSection<ELFT> &C = Rel.C;
109 const Elf_Rel &RI = Rel.RI;
110 OutputSection<ELFT> *Out = C.getOutputSection();
111 uint32_t SymIndex = RI.getSymbol(IsMips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000112 const ObjectFile<ELFT> &File = *C.getFile();
113 const SymbolBody *Body = File.getSymbolBody(SymIndex);
114 const ELFFile<ELFT> &Obj = File.getObj();
115
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000116 uint32_t Type = RI.getType(IsMips64EL);
Rafael Espindolaae244002015-10-05 19:30:12 +0000117 if (Body && Target->relocNeedsGot(Type, *Body)) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000118 P->r_offset = GotSec.getEntryAddr(*Body);
Rafael Espindola7f074422015-09-22 21:35:51 +0000119 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
120 Target->getGotReloc(), IsMips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000121 } else {
122 P->r_offset = RI.r_offset + C.getOutputSectionOff() + Out->getVA();
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000123 uintX_t Addent = 0;
124 if (IsRela)
125 Addent = static_cast<const Elf_Rela &>(RI).r_addend;
126
Rafael Espindola03ab3362015-10-06 12:39:58 +0000127 if (!isLocalDefinition(Body)) {
Rafael Espindolaae244002015-10-05 19:30:12 +0000128 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type,
129 IsMips64EL);
Rafael Espindolaae244002015-10-05 19:30:12 +0000130 } else {
131 P->setSymbolAndType(0, Target->getRelativeReloc(), IsMips64EL);
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000132 if (IsRela) {
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000133 if (Body)
Rafael Espindola9c3e4d22015-10-05 21:23:08 +0000134 Addent += getSymVA(cast<ELFSymbolBody<ELFT>>(*Body), BssSec);
135 else
Rafael Espindola41127ad2015-10-05 22:49:16 +0000136 Addent += getLocalSymVA(
137 Obj.getRelocationSymbol(&RI, File.getSymbolTable()), File);
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000138 }
Rafael Espindolaae244002015-10-05 19:30:12 +0000139 }
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000140 if (IsRela)
141 static_cast<Elf_Rela *>(P)->r_addend = Addent;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000142 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000143 }
144}
145
146template <class ELFT> void RelocationSection<ELFT>::finalize() {
147 this->Header.sh_link = DynSymSec.getSectionIndex();
148 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
149}
150
151template <bool Is64Bits>
152InterpSection<Is64Bits>::InterpSection()
153 : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS,
154 llvm::ELF::SHF_ALLOC) {
155 this->Header.sh_size = Config->DynamicLinker.size() + 1;
156 this->Header.sh_addralign = 1;
157}
158
159template <bool Is64Bits>
160template <endianness E>
161void OutputSectionBase<Is64Bits>::writeHeaderTo(
162 typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) {
163 SHdr->sh_name = Header.sh_name;
164 SHdr->sh_type = Header.sh_type;
165 SHdr->sh_flags = Header.sh_flags;
166 SHdr->sh_addr = Header.sh_addr;
167 SHdr->sh_offset = Header.sh_offset;
168 SHdr->sh_size = Header.sh_size;
169 SHdr->sh_link = Header.sh_link;
170 SHdr->sh_info = Header.sh_info;
171 SHdr->sh_addralign = Header.sh_addralign;
172 SHdr->sh_entsize = Header.sh_entsize;
173}
174
175template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) {
176 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
177}
178
Rafael Espindola35c6af32015-09-25 17:19:10 +0000179template <class ELFT>
180HashTableSection<ELFT>::HashTableSection(SymbolTableSection<ELFT> &DynSymSec)
181 : OutputSectionBase<ELFT::Is64Bits>(".hash", llvm::ELF::SHT_HASH,
182 llvm::ELF::SHF_ALLOC),
183 DynSymSec(DynSymSec) {
184 this->Header.sh_entsize = sizeof(Elf_Word);
185 this->Header.sh_addralign = sizeof(Elf_Word);
186}
187
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000188template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
189 StringRef Name = S->getName();
190 DynSymSec.addSymbol(Name);
191 Hashes.push_back(hash(Name));
192 S->setDynamicSymbolTableIndex(Hashes.size());
193}
194
Rafael Espindola35c6af32015-09-25 17:19:10 +0000195template <class ELFT>
196DynamicSection<ELFT>::DynamicSection(SymbolTable &SymTab,
197 HashTableSection<ELFT> &HashSec,
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000198 RelocationSection<ELFT> &RelaDynSec,
199 const OutputSection<ELFT> &BssSec)
Rafael Espindola35c6af32015-09-25 17:19:10 +0000200 : OutputSectionBase<ELFT::Is64Bits>(".dynamic", llvm::ELF::SHT_DYNAMIC,
201 llvm::ELF::SHF_ALLOC |
202 llvm::ELF::SHF_WRITE),
203 HashSec(HashSec), DynSymSec(HashSec.getDynSymSec()),
204 DynStrSec(DynSymSec.getStrTabSec()), RelaDynSec(RelaDynSec),
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000205 BssSec(BssSec), SymTab(SymTab) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000206 typename Base::HeaderT &Header = this->Header;
207 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
208 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
209}
210
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000211template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000212 if (this->Header.sh_size)
213 return; // Already finalized.
214
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215 typename Base::HeaderT &Header = this->Header;
216 Header.sh_link = DynStrSec.getSectionIndex();
217
218 unsigned NumEntries = 0;
219 if (RelaDynSec.hasRelocs()) {
220 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000221 ++NumEntries; // DT_RELASZ / DT_RELSZ
222 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000223 }
224 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000225 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000226 ++NumEntries; // DT_STRTAB
227 ++NumEntries; // DT_STRSZ
228 ++NumEntries; // DT_HASH
229
Rui Ueyama7de3f372015-10-01 19:36:04 +0000230 if (!Config->RPath.empty()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000231 ++NumEntries; // DT_RUNPATH
Rui Ueyama7de3f372015-10-01 19:36:04 +0000232 DynStrSec.add(Config->RPath);
233 }
234
235 if (!Config->SoName.empty()) {
236 ++NumEntries; // DT_SONAME
237 DynStrSec.add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000238 }
239
Rafael Espindola77572242015-10-02 19:37:55 +0000240 if (PreInitArraySec)
241 NumEntries += 2;
242 if (InitArraySec)
243 NumEntries += 2;
244 if (FiniArraySec)
245 NumEntries += 2;
246
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
248 SymTab.getSharedFiles();
249 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
Rafael Espindolac8b15812015-10-01 15:47:50 +0000250 DynStrSec.add(File->getSoName());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251 NumEntries += SharedFiles.size();
252
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000253 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
254 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
255 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
256 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000257 if (InitSym)
258 ++NumEntries; // DT_INIT
259 if (FiniSym)
260 ++NumEntries; // DT_FINI
261
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262 ++NumEntries; // DT_NULL
263
264 Header.sh_size = NumEntries * Header.sh_entsize;
265}
266
267template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000268 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
269
Rui Ueyama8c205d52015-10-02 01:33:31 +0000270 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
271 P->d_tag = Tag;
272 P->d_un.d_ptr = Val;
273 ++P;
274 };
275
276 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
277 P->d_tag = Tag;
278 P->d_un.d_val = Val;
279 ++P;
280 };
281
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000282 if (RelaDynSec.hasRelocs()) {
283 bool IsRela = RelaDynSec.isRela();
Rui Ueyama8c205d52015-10-02 01:33:31 +0000284 WritePtr(IsRela ? DT_RELA : DT_REL, RelaDynSec.getVA());
285 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, RelaDynSec.getSize());
286 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
287 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000288 }
289
Rui Ueyama8c205d52015-10-02 01:33:31 +0000290 WritePtr(DT_SYMTAB, DynSymSec.getVA());
291 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
292 WritePtr(DT_STRTAB, DynStrSec.getVA());
293 WriteVal(DT_STRSZ, DynStrSec.data().size());
294 WritePtr(DT_HASH, HashSec.getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000295
Rui Ueyama8c205d52015-10-02 01:33:31 +0000296 if (!Config->RPath.empty())
297 WriteVal(DT_RUNPATH, DynStrSec.getFileOff(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000298
Rui Ueyama8c205d52015-10-02 01:33:31 +0000299 if (!Config->SoName.empty())
300 WriteVal(DT_SONAME, DynStrSec.getFileOff(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000301
Rafael Espindola77572242015-10-02 19:37:55 +0000302 auto WriteArray = [&](int32_t T1, int32_t T2,
303 const OutputSection<ELFT> *Sec) {
304 if (!Sec)
305 return;
306 WritePtr(T1, Sec->getVA());
307 WriteVal(T2, Sec->getSize());
308 };
309 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
310 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
311 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
312
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000313 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
314 SymTab.getSharedFiles();
Rui Ueyama8c205d52015-10-02 01:33:31 +0000315 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
316 WriteVal(DT_NEEDED, DynStrSec.getFileOff(File->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000317
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000318 if (InitSym)
319 WritePtr(DT_INIT, getSymVA(*InitSym, BssSec));
320 if (FiniSym)
321 WritePtr(DT_FINI, getSymVA(*FiniSym, BssSec));
322
Rui Ueyama8c205d52015-10-02 01:33:31 +0000323 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324}
325
326template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000327OutputSection<ELFT>::OutputSection(const PltSection<ELFT> &PltSec,
328 const GotSection<ELFT> &GotSec,
329 const OutputSection<ELFT> &BssSec,
330 StringRef Name, uint32_t sh_type,
331 uintX_t sh_flags)
332 : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags),
333 PltSec(PltSec), GotSec(GotSec), BssSec(BssSec) {}
334
335template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000336void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
337 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000338 C->setOutputSection(this);
339 uint32_t Align = C->getAlign();
340 if (Align > this->Header.sh_addralign)
341 this->Header.sh_addralign = Align;
342
343 uintX_t Off = this->Header.sh_size;
344 Off = RoundUpToAlignment(Off, Align);
345 C->setOutputSectionOff(Off);
346 Off += C->getSize();
347 this->Header.sh_size = Off;
348}
349
350template <class ELFT>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000351typename ELFFile<ELFT>::uintX_t
Rafael Espindolacd076f02015-09-25 18:19:03 +0000352lld::elf2::getSymVA(const ELFSymbolBody<ELFT> &S,
353 const OutputSection<ELFT> &BssSec) {
354 switch (S.kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000355 case SymbolBody::DefinedSyntheticKind:
356 return cast<DefinedSynthetic<ELFT>>(S).Section.getVA() + S.Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000357 case SymbolBody::DefinedAbsoluteKind:
358 return S.Sym.st_value;
359 case SymbolBody::DefinedRegularKind: {
360 const auto &DR = cast<DefinedRegular<ELFT>>(S);
361 const InputSection<ELFT> *SC = &DR.Section;
362 OutputSection<ELFT> *OS = SC->getOutputSection();
363 return OS->getVA() + SC->getOutputSectionOff() + DR.Sym.st_value;
364 }
365 case SymbolBody::DefinedCommonKind:
366 return BssSec.getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
367 case SymbolBody::SharedKind:
368 case SymbolBody::UndefinedKind:
369 return 0;
370 case SymbolBody::LazyKind:
Rafael Espindola27322352015-09-28 22:12:54 +0000371 break;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000372 }
Rafael Espindola27322352015-09-28 22:12:54 +0000373 llvm_unreachable("Lazy symbol reached writer");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000374}
375
376template <class ELFT>
377typename ELFFile<ELFT>::uintX_t
378lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym,
379 const ObjectFile<ELFT> &File) {
380 uint32_t SecIndex = Sym->st_shndx;
381
382 if (SecIndex == SHN_XINDEX)
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000383 SecIndex = File.getObj().getExtendedSymbolTableIndex(
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000384 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000385 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
386 InputSection<ELFT> *Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000387 OutputSection<ELFT> *Out = Section->getOutputSection();
388 return Out->getVA() + Section->getOutputSectionOff() + Sym->st_value;
389}
390
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000391template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000392 for (InputSection<ELFT> *C : Sections)
Rafael Espindolac2d21192015-09-23 18:25:05 +0000393 C->writeTo(Buf, BssSec, PltSec, GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000394}
395
396template <bool Is64Bits>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000397StringTableSection<Is64Bits>::StringTableSection(bool Dynamic)
398 : OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab",
399 llvm::ELF::SHT_STRTAB,
400 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
401 Dynamic(Dynamic) {
402 this->Header.sh_addralign = 1;
403}
404
405template <bool Is64Bits>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
407 StringRef Data = StrTabBuilder.data();
408 memcpy(Buf, Data.data(), Data.size());
409}
410
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000411template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000412 if (B.isLazy())
413 return false;
414 if (!B.isUsedInRegularObj())
415 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000416
417 // Don't include synthetic symbols like __init_array_start in every output.
418 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
419 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
420 return false;
421
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000422 return true;
423}
424
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000425bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000426 uint8_t V = B.getMostConstrainingVisibility();
427 if (V != STV_DEFAULT && V != STV_PROTECTED)
428 return false;
429
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000430 if (Config->ExportDynamic || Config->Shared)
431 return true;
432 return B.isUsedInDynamicReloc();
433}
434
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000435template <class ELFT>
436bool lld::elf2::shouldKeepInSymtab(StringRef SymName,
437 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
438 if (Sym.getType() == STT_SECTION)
439 return false;
440
Davide Italiano6993ba42015-09-26 00:47:56 +0000441 if (Config->DiscardNone)
442 return true;
443
444 // ELF defines dynamic locals as symbols which name starts with ".L".
445 return !(Config->DiscardLocals && SymName.startswith(".L"));
446}
447
Rafael Espindola35c6af32015-09-25 17:19:10 +0000448template <class ELFT>
449SymbolTableSection<ELFT>::SymbolTableSection(
450 SymbolTable &Table, StringTableSection<ELFT::Is64Bits> &StrTabSec,
451 const OutputSection<ELFT> &BssSec)
452 : OutputSectionBase<ELFT::Is64Bits>(
453 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
454 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
455 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
456 Table(Table), StrTabSec(StrTabSec), BssSec(BssSec) {
457 typedef OutputSectionBase<ELFT::Is64Bits> Base;
458 typename Base::HeaderT &Header = this->Header;
459
460 Header.sh_entsize = sizeof(Elf_Sym);
461 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
462}
463
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000464template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000465 Buf += sizeof(Elf_Sym);
466
467 // All symbols with STB_LOCAL binding precede the weak and global symbols.
468 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000469 if (!Config->DiscardAll && !StrTabSec.isDynamic())
470 writeLocalSymbols(Buf);
471
472 writeGlobalSymbols(Buf);
473}
474
475template <class ELFT>
476void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
477 // Iterate over all input object files to copy their local symbols
478 // to the output symbol table pointed by Buf.
479 for (const std::unique_ptr<ObjectFileBase> &FileB : Table.getObjectFiles()) {
480 auto &File = cast<ObjectFile<ELFT>>(*FileB);
481 Elf_Sym_Range Syms = File.getLocalSymbols();
482 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000483 ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000484 if (SymName && !shouldKeepInSymtab<ELFT>(*SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000485 continue;
Rui Ueyamac55733e2015-09-30 00:54:29 +0000486 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
487 Buf += sizeof(*ESym);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000488 ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
489 ESym->st_size = Sym.st_size;
490 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
491 uint32_t SecIndex = Sym.st_shndx;
492 uintX_t VA = Sym.st_value;
493 if (SecIndex == SHN_ABS) {
494 ESym->st_shndx = SHN_ABS;
495 } else {
496 if (SecIndex == SHN_XINDEX)
497 SecIndex = File.getObj().getExtendedSymbolTableIndex(
498 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
499 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
500 const InputSection<ELFT> *Section = Sections[SecIndex];
501 const OutputSection<ELFT> *Out = Section->getOutputSection();
502 ESym->st_shndx = Out->getSectionIndex();
503 VA += Out->getVA() + Section->getOutputSectionOff();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000504 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000505 ESym->st_value = VA;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000506 }
507 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000508}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000509
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000510template <class ELFT>
511void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *&Buf) {
512 // Write the internal symbol table contents to the output symbol table
513 // pointed by Buf.
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000514 uint8_t *Start = Buf;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000515 for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000516 StringRef Name = P.first;
517 Symbol *Sym = P.second;
518 SymbolBody *Body = Sym->Body;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000519 if (!includeInSymtab<ELFT>(*Body))
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000520 continue;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000521 if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body))
522 continue;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000523
Rafael Espindolacd076f02015-09-25 18:19:03 +0000524 const auto &EBody = *cast<ELFSymbolBody<ELFT>>(Body);
525 const Elf_Sym &InputSym = EBody.Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000526 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac55733e2015-09-30 00:54:29 +0000527 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000528 ESym->st_name = StrTabSec.getFileOff(Name);
529
Rafael Espindola25b0acb2015-09-25 17:32:37 +0000530 const OutputSection<ELFT> *Out = nullptr;
531 const InputSection<ELFT> *Section = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000532
Rafael Espindolacd076f02015-09-25 18:19:03 +0000533 switch (EBody.kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000534 case SymbolBody::DefinedSyntheticKind:
535 Out = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
536 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000537 case SymbolBody::DefinedRegularKind:
Rafael Espindolacd076f02015-09-25 18:19:03 +0000538 Section = &cast<DefinedRegular<ELFT>>(EBody).Section;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000539 break;
540 case SymbolBody::DefinedCommonKind:
Rafael Espindolac2d21192015-09-23 18:25:05 +0000541 Out = &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000542 break;
543 case SymbolBody::UndefinedKind:
544 case SymbolBody::DefinedAbsoluteKind:
545 case SymbolBody::SharedKind:
546 break;
547 case SymbolBody::LazyKind:
548 llvm_unreachable("Lazy symbol got to output symbol table!");
549 }
550
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000551 unsigned char Binding = InputSym.getBinding();
552 unsigned char Visibility = EBody.getMostConstrainingVisibility();
553 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
554 Binding = STB_LOCAL;
555
556 ESym->setBindingAndType(Binding, InputSym.getType());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000557 ESym->st_size = InputSym.st_size;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000558 ESym->setVisibility(Visibility);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000559 if (InputSym.isAbsolute()) {
560 ESym->st_shndx = SHN_ABS;
561 ESym->st_value = InputSym.st_value;
562 }
563
564 if (Section)
565 Out = Section->getOutputSection();
566
Rafael Espindolacd076f02015-09-25 18:19:03 +0000567 ESym->st_value = getSymVA(EBody, BssSec);
568
569 if (Out)
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000570 ESym->st_shndx = Out->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000571 }
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000572 if (!StrTabSec.isDynamic())
573 std::stable_sort(
574 reinterpret_cast<Elf_Sym *>(Start), reinterpret_cast<Elf_Sym *>(Buf),
575 [](const Elf_Sym &A, const Elf_Sym &B) -> bool {
576 return A.getBinding() == STB_LOCAL && B.getBinding() != STB_LOCAL;
577 });
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000578}
579
580namespace lld {
581namespace elf2 {
582template class OutputSectionBase<false>;
583template class OutputSectionBase<true>;
584
585template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000586 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000587template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000588 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000589template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000590 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000591template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000592 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000593
594template class GotSection<ELF32LE>;
595template class GotSection<ELF32BE>;
596template class GotSection<ELF64LE>;
597template class GotSection<ELF64BE>;
598
599template class PltSection<ELF32LE>;
600template class PltSection<ELF32BE>;
601template class PltSection<ELF64LE>;
602template class PltSection<ELF64BE>;
603
604template class RelocationSection<ELF32LE>;
605template class RelocationSection<ELF32BE>;
606template class RelocationSection<ELF64LE>;
607template class RelocationSection<ELF64BE>;
608
609template class InterpSection<false>;
610template class InterpSection<true>;
611
612template class HashTableSection<ELF32LE>;
613template class HashTableSection<ELF32BE>;
614template class HashTableSection<ELF64LE>;
615template class HashTableSection<ELF64BE>;
616
617template class DynamicSection<ELF32LE>;
618template class DynamicSection<ELF32BE>;
619template class DynamicSection<ELF64LE>;
620template class DynamicSection<ELF64BE>;
621
622template class OutputSection<ELF32LE>;
623template class OutputSection<ELF32BE>;
624template class OutputSection<ELF64LE>;
625template class OutputSection<ELF64BE>;
626
627template class StringTableSection<false>;
628template class StringTableSection<true>;
629
630template class SymbolTableSection<ELF32LE>;
631template class SymbolTableSection<ELF32BE>;
632template class SymbolTableSection<ELF64LE>;
633template class SymbolTableSection<ELF64BE>;
634
Rafael Espindolacd076f02015-09-25 18:19:03 +0000635template ELFFile<ELF32LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000636getSymVA(const ELFSymbolBody<ELF32LE> &, const OutputSection<ELF32LE> &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000637
Rafael Espindolacd076f02015-09-25 18:19:03 +0000638template ELFFile<ELF32BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000639getSymVA(const ELFSymbolBody<ELF32BE> &, const OutputSection<ELF32BE> &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000640
Rafael Espindolacd076f02015-09-25 18:19:03 +0000641template ELFFile<ELF64LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000642getSymVA(const ELFSymbolBody<ELF64LE> &, const OutputSection<ELF64LE> &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000643
Rafael Espindolacd076f02015-09-25 18:19:03 +0000644template ELFFile<ELF64BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000645getSymVA(const ELFSymbolBody<ELF64BE> &, const OutputSection<ELF64BE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000646
Rafael Espindola56f965f2015-09-21 22:48:12 +0000647template ELFFile<ELF32LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000648getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *, const ObjectFile<ELF32LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000649
Rafael Espindola56f965f2015-09-21 22:48:12 +0000650template ELFFile<ELF32BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000651getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *, const ObjectFile<ELF32BE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000652
Rafael Espindola56f965f2015-09-21 22:48:12 +0000653template ELFFile<ELF64LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000654getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *, const ObjectFile<ELF64LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000655
Rafael Espindola56f965f2015-09-21 22:48:12 +0000656template ELFFile<ELF64BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000657getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *, const ObjectFile<ELF64BE> &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000658
659template bool includeInSymtab<ELF32LE>(const SymbolBody &);
660template bool includeInSymtab<ELF32BE>(const SymbolBody &);
661template bool includeInSymtab<ELF64LE>(const SymbolBody &);
662template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000663
664template bool shouldKeepInSymtab<ELF32LE>(StringRef,
665 const ELFFile<ELF32LE>::Elf_Sym &);
666template bool shouldKeepInSymtab<ELF32BE>(StringRef,
667 const ELFFile<ELF32BE>::Elf_Sym &);
668template bool shouldKeepInSymtab<ELF64LE>(StringRef,
669 const ELFFile<ELF64LE>::Elf_Sym &);
670template bool shouldKeepInSymtab<ELF64BE>(StringRef,
671 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000672}
673}