blob: 0cfb89dfaf5f804c69fb351b8ce912cf0c796ef8 [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,
84 bool IsRela)
85 : OutputSectionBase<ELFT::Is64Bits>(IsRela ? ".rela.dyn" : ".rel.dyn",
86 IsRela ? llvm::ELF::SHT_RELA
87 : llvm::ELF::SHT_REL,
88 llvm::ELF::SHF_ALLOC),
89 DynSymSec(DynSymSec), GotSec(GotSec), IsRela(IsRela) {
90 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
91 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
92}
93
Rafael Espindola5805c4f2015-09-21 21:38:08 +000094template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +000095 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindolae1901cc2015-09-24 15:11:50 +000096 bool IsMips64EL = Relocs[0].C.getFile()->getObj().isMips64EL();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000097 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +000098 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
99 Buf += EntrySize;
100
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000101 const InputSection<ELFT> &C = Rel.C;
102 const Elf_Rel &RI = Rel.RI;
103 OutputSection<ELFT> *Out = C.getOutputSection();
104 uint32_t SymIndex = RI.getSymbol(IsMips64EL);
105 const SymbolBody *Body = C.getFile()->getSymbolBody(SymIndex);
106 uint32_t Type = RI.getType(IsMips64EL);
Rafael Espindolaae244002015-10-05 19:30:12 +0000107 if (Body && Target->relocNeedsGot(Type, *Body)) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000108 P->r_offset = GotSec.getEntryAddr(*Body);
Rafael Espindola7f074422015-09-22 21:35:51 +0000109 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
110 Target->getGotReloc(), IsMips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000111 } else {
112 P->r_offset = RI.r_offset + C.getOutputSectionOff() + Out->getVA();
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000113 uintX_t Addent = 0;
114 if (IsRela)
115 Addent = static_cast<const Elf_Rela &>(RI).r_addend;
116
Rafael Espindolaae244002015-10-05 19:30:12 +0000117 if (Body && Body->isShared()) {
118 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type,
119 IsMips64EL);
Rafael Espindolaae244002015-10-05 19:30:12 +0000120 } else {
121 P->setSymbolAndType(0, Target->getRelativeReloc(), IsMips64EL);
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000122 if (IsRela) {
123 Addent += C.getOutputSectionOff() + Out->getVA();
124 if (Body)
125 Addent += cast<DefinedRegular<ELFT>>(Body)->Sym.st_value;
126 }
Rafael Espindolaae244002015-10-05 19:30:12 +0000127 }
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000128 if (IsRela)
129 static_cast<Elf_Rela *>(P)->r_addend = Addent;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000130 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000131 }
132}
133
134template <class ELFT> void RelocationSection<ELFT>::finalize() {
135 this->Header.sh_link = DynSymSec.getSectionIndex();
136 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
137}
138
139template <bool Is64Bits>
140InterpSection<Is64Bits>::InterpSection()
141 : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS,
142 llvm::ELF::SHF_ALLOC) {
143 this->Header.sh_size = Config->DynamicLinker.size() + 1;
144 this->Header.sh_addralign = 1;
145}
146
147template <bool Is64Bits>
148template <endianness E>
149void OutputSectionBase<Is64Bits>::writeHeaderTo(
150 typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) {
151 SHdr->sh_name = Header.sh_name;
152 SHdr->sh_type = Header.sh_type;
153 SHdr->sh_flags = Header.sh_flags;
154 SHdr->sh_addr = Header.sh_addr;
155 SHdr->sh_offset = Header.sh_offset;
156 SHdr->sh_size = Header.sh_size;
157 SHdr->sh_link = Header.sh_link;
158 SHdr->sh_info = Header.sh_info;
159 SHdr->sh_addralign = Header.sh_addralign;
160 SHdr->sh_entsize = Header.sh_entsize;
161}
162
163template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) {
164 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
165}
166
Rafael Espindola35c6af32015-09-25 17:19:10 +0000167template <class ELFT>
168HashTableSection<ELFT>::HashTableSection(SymbolTableSection<ELFT> &DynSymSec)
169 : OutputSectionBase<ELFT::Is64Bits>(".hash", llvm::ELF::SHT_HASH,
170 llvm::ELF::SHF_ALLOC),
171 DynSymSec(DynSymSec) {
172 this->Header.sh_entsize = sizeof(Elf_Word);
173 this->Header.sh_addralign = sizeof(Elf_Word);
174}
175
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
177 StringRef Name = S->getName();
178 DynSymSec.addSymbol(Name);
179 Hashes.push_back(hash(Name));
180 S->setDynamicSymbolTableIndex(Hashes.size());
181}
182
Rafael Espindola35c6af32015-09-25 17:19:10 +0000183template <class ELFT>
184DynamicSection<ELFT>::DynamicSection(SymbolTable &SymTab,
185 HashTableSection<ELFT> &HashSec,
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000186 RelocationSection<ELFT> &RelaDynSec,
187 const OutputSection<ELFT> &BssSec)
Rafael Espindola35c6af32015-09-25 17:19:10 +0000188 : OutputSectionBase<ELFT::Is64Bits>(".dynamic", llvm::ELF::SHT_DYNAMIC,
189 llvm::ELF::SHF_ALLOC |
190 llvm::ELF::SHF_WRITE),
191 HashSec(HashSec), DynSymSec(HashSec.getDynSymSec()),
192 DynStrSec(DynSymSec.getStrTabSec()), RelaDynSec(RelaDynSec),
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000193 BssSec(BssSec), SymTab(SymTab) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000194 typename Base::HeaderT &Header = this->Header;
195 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
196 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
197}
198
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000200 if (this->Header.sh_size)
201 return; // Already finalized.
202
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203 typename Base::HeaderT &Header = this->Header;
204 Header.sh_link = DynStrSec.getSectionIndex();
205
206 unsigned NumEntries = 0;
207 if (RelaDynSec.hasRelocs()) {
208 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000209 ++NumEntries; // DT_RELASZ / DT_RELSZ
210 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000211 }
212 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000213 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000214 ++NumEntries; // DT_STRTAB
215 ++NumEntries; // DT_STRSZ
216 ++NumEntries; // DT_HASH
217
Rui Ueyama7de3f372015-10-01 19:36:04 +0000218 if (!Config->RPath.empty()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000219 ++NumEntries; // DT_RUNPATH
Rui Ueyama7de3f372015-10-01 19:36:04 +0000220 DynStrSec.add(Config->RPath);
221 }
222
223 if (!Config->SoName.empty()) {
224 ++NumEntries; // DT_SONAME
225 DynStrSec.add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000226 }
227
Rafael Espindola77572242015-10-02 19:37:55 +0000228 if (PreInitArraySec)
229 NumEntries += 2;
230 if (InitArraySec)
231 NumEntries += 2;
232 if (FiniArraySec)
233 NumEntries += 2;
234
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000235 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
236 SymTab.getSharedFiles();
237 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
Rafael Espindolac8b15812015-10-01 15:47:50 +0000238 DynStrSec.add(File->getSoName());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000239 NumEntries += SharedFiles.size();
240
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000241 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
242 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
243 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
244 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000245 if (InitSym)
246 ++NumEntries; // DT_INIT
247 if (FiniSym)
248 ++NumEntries; // DT_FINI
249
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000250 ++NumEntries; // DT_NULL
251
252 Header.sh_size = NumEntries * Header.sh_entsize;
253}
254
255template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
257
Rui Ueyama8c205d52015-10-02 01:33:31 +0000258 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
259 P->d_tag = Tag;
260 P->d_un.d_ptr = Val;
261 ++P;
262 };
263
264 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
265 P->d_tag = Tag;
266 P->d_un.d_val = Val;
267 ++P;
268 };
269
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000270 if (RelaDynSec.hasRelocs()) {
271 bool IsRela = RelaDynSec.isRela();
Rui Ueyama8c205d52015-10-02 01:33:31 +0000272 WritePtr(IsRela ? DT_RELA : DT_REL, RelaDynSec.getVA());
273 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, RelaDynSec.getSize());
274 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
275 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000276 }
277
Rui Ueyama8c205d52015-10-02 01:33:31 +0000278 WritePtr(DT_SYMTAB, DynSymSec.getVA());
279 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
280 WritePtr(DT_STRTAB, DynStrSec.getVA());
281 WriteVal(DT_STRSZ, DynStrSec.data().size());
282 WritePtr(DT_HASH, HashSec.getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000283
Rui Ueyama8c205d52015-10-02 01:33:31 +0000284 if (!Config->RPath.empty())
285 WriteVal(DT_RUNPATH, DynStrSec.getFileOff(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000286
Rui Ueyama8c205d52015-10-02 01:33:31 +0000287 if (!Config->SoName.empty())
288 WriteVal(DT_SONAME, DynStrSec.getFileOff(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000289
Rafael Espindola77572242015-10-02 19:37:55 +0000290 auto WriteArray = [&](int32_t T1, int32_t T2,
291 const OutputSection<ELFT> *Sec) {
292 if (!Sec)
293 return;
294 WritePtr(T1, Sec->getVA());
295 WriteVal(T2, Sec->getSize());
296 };
297 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
298 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
299 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
300
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000301 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
302 SymTab.getSharedFiles();
Rui Ueyama8c205d52015-10-02 01:33:31 +0000303 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
304 WriteVal(DT_NEEDED, DynStrSec.getFileOff(File->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000305
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000306 if (InitSym)
307 WritePtr(DT_INIT, getSymVA(*InitSym, BssSec));
308 if (FiniSym)
309 WritePtr(DT_FINI, getSymVA(*FiniSym, BssSec));
310
Rui Ueyama8c205d52015-10-02 01:33:31 +0000311 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000312}
313
314template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000315OutputSection<ELFT>::OutputSection(const PltSection<ELFT> &PltSec,
316 const GotSection<ELFT> &GotSec,
317 const OutputSection<ELFT> &BssSec,
318 StringRef Name, uint32_t sh_type,
319 uintX_t sh_flags)
320 : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags),
321 PltSec(PltSec), GotSec(GotSec), BssSec(BssSec) {}
322
323template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000324void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
325 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000326 C->setOutputSection(this);
327 uint32_t Align = C->getAlign();
328 if (Align > this->Header.sh_addralign)
329 this->Header.sh_addralign = Align;
330
331 uintX_t Off = this->Header.sh_size;
332 Off = RoundUpToAlignment(Off, Align);
333 C->setOutputSectionOff(Off);
334 Off += C->getSize();
335 this->Header.sh_size = Off;
336}
337
338template <class ELFT>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000339typename ELFFile<ELFT>::uintX_t
Rafael Espindolacd076f02015-09-25 18:19:03 +0000340lld::elf2::getSymVA(const ELFSymbolBody<ELFT> &S,
341 const OutputSection<ELFT> &BssSec) {
342 switch (S.kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000343 case SymbolBody::DefinedSyntheticKind:
344 return cast<DefinedSynthetic<ELFT>>(S).Section.getVA() + S.Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000345 case SymbolBody::DefinedAbsoluteKind:
346 return S.Sym.st_value;
347 case SymbolBody::DefinedRegularKind: {
348 const auto &DR = cast<DefinedRegular<ELFT>>(S);
349 const InputSection<ELFT> *SC = &DR.Section;
350 OutputSection<ELFT> *OS = SC->getOutputSection();
351 return OS->getVA() + SC->getOutputSectionOff() + DR.Sym.st_value;
352 }
353 case SymbolBody::DefinedCommonKind:
354 return BssSec.getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
355 case SymbolBody::SharedKind:
356 case SymbolBody::UndefinedKind:
357 return 0;
358 case SymbolBody::LazyKind:
Rafael Espindola27322352015-09-28 22:12:54 +0000359 break;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000360 }
Rafael Espindola27322352015-09-28 22:12:54 +0000361 llvm_unreachable("Lazy symbol reached writer");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000362}
363
364template <class ELFT>
365typename ELFFile<ELFT>::uintX_t
366lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym,
367 const ObjectFile<ELFT> &File) {
368 uint32_t SecIndex = Sym->st_shndx;
369
370 if (SecIndex == SHN_XINDEX)
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000371 SecIndex = File.getObj().getExtendedSymbolTableIndex(
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000372 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000373 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
374 InputSection<ELFT> *Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000375 OutputSection<ELFT> *Out = Section->getOutputSection();
376 return Out->getVA() + Section->getOutputSectionOff() + Sym->st_value;
377}
378
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000379template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000380 for (InputSection<ELFT> *C : Sections)
Rafael Espindolac2d21192015-09-23 18:25:05 +0000381 C->writeTo(Buf, BssSec, PltSec, GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000382}
383
384template <bool Is64Bits>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000385StringTableSection<Is64Bits>::StringTableSection(bool Dynamic)
386 : OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab",
387 llvm::ELF::SHT_STRTAB,
388 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
389 Dynamic(Dynamic) {
390 this->Header.sh_addralign = 1;
391}
392
393template <bool Is64Bits>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000394void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
395 StringRef Data = StrTabBuilder.data();
396 memcpy(Buf, Data.data(), Data.size());
397}
398
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000399template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000400 if (B.isLazy())
401 return false;
402 if (!B.isUsedInRegularObj())
403 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000404
405 // Don't include synthetic symbols like __init_array_start in every output.
406 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
407 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
408 return false;
409
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000410 return true;
411}
412
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000413bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000414 uint8_t V = B.getMostConstrainingVisibility();
415 if (V != STV_DEFAULT && V != STV_PROTECTED)
416 return false;
417
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000418 if (Config->ExportDynamic || Config->Shared)
419 return true;
420 return B.isUsedInDynamicReloc();
421}
422
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000423template <class ELFT>
424bool lld::elf2::shouldKeepInSymtab(StringRef SymName,
425 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
426 if (Sym.getType() == STT_SECTION)
427 return false;
428
Davide Italiano6993ba42015-09-26 00:47:56 +0000429 if (Config->DiscardNone)
430 return true;
431
432 // ELF defines dynamic locals as symbols which name starts with ".L".
433 return !(Config->DiscardLocals && SymName.startswith(".L"));
434}
435
Rafael Espindola35c6af32015-09-25 17:19:10 +0000436template <class ELFT>
437SymbolTableSection<ELFT>::SymbolTableSection(
438 SymbolTable &Table, StringTableSection<ELFT::Is64Bits> &StrTabSec,
439 const OutputSection<ELFT> &BssSec)
440 : OutputSectionBase<ELFT::Is64Bits>(
441 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
442 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
443 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
444 Table(Table), StrTabSec(StrTabSec), BssSec(BssSec) {
445 typedef OutputSectionBase<ELFT::Is64Bits> Base;
446 typename Base::HeaderT &Header = this->Header;
447
448 Header.sh_entsize = sizeof(Elf_Sym);
449 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
450}
451
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000452template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000453 Buf += sizeof(Elf_Sym);
454
455 // All symbols with STB_LOCAL binding precede the weak and global symbols.
456 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000457 if (!Config->DiscardAll && !StrTabSec.isDynamic())
458 writeLocalSymbols(Buf);
459
460 writeGlobalSymbols(Buf);
461}
462
463template <class ELFT>
464void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
465 // Iterate over all input object files to copy their local symbols
466 // to the output symbol table pointed by Buf.
467 for (const std::unique_ptr<ObjectFileBase> &FileB : Table.getObjectFiles()) {
468 auto &File = cast<ObjectFile<ELFT>>(*FileB);
469 Elf_Sym_Range Syms = File.getLocalSymbols();
470 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000471 ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000472 if (SymName && !shouldKeepInSymtab<ELFT>(*SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000473 continue;
Rui Ueyamac55733e2015-09-30 00:54:29 +0000474 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
475 Buf += sizeof(*ESym);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000476 ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
477 ESym->st_size = Sym.st_size;
478 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
479 uint32_t SecIndex = Sym.st_shndx;
480 uintX_t VA = Sym.st_value;
481 if (SecIndex == SHN_ABS) {
482 ESym->st_shndx = SHN_ABS;
483 } else {
484 if (SecIndex == SHN_XINDEX)
485 SecIndex = File.getObj().getExtendedSymbolTableIndex(
486 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
487 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
488 const InputSection<ELFT> *Section = Sections[SecIndex];
489 const OutputSection<ELFT> *Out = Section->getOutputSection();
490 ESym->st_shndx = Out->getSectionIndex();
491 VA += Out->getVA() + Section->getOutputSectionOff();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000492 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000493 ESym->st_value = VA;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000494 }
495 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000496}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000497
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000498template <class ELFT>
499void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *&Buf) {
500 // Write the internal symbol table contents to the output symbol table
501 // pointed by Buf.
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000502 uint8_t *Start = Buf;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000503 for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000504 StringRef Name = P.first;
505 Symbol *Sym = P.second;
506 SymbolBody *Body = Sym->Body;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000507 if (!includeInSymtab<ELFT>(*Body))
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000508 continue;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000509 if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body))
510 continue;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000511
Rafael Espindolacd076f02015-09-25 18:19:03 +0000512 const auto &EBody = *cast<ELFSymbolBody<ELFT>>(Body);
513 const Elf_Sym &InputSym = EBody.Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000514 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac55733e2015-09-30 00:54:29 +0000515 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000516 ESym->st_name = StrTabSec.getFileOff(Name);
517
Rafael Espindola25b0acb2015-09-25 17:32:37 +0000518 const OutputSection<ELFT> *Out = nullptr;
519 const InputSection<ELFT> *Section = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000520
Rafael Espindolacd076f02015-09-25 18:19:03 +0000521 switch (EBody.kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000522 case SymbolBody::DefinedSyntheticKind:
523 Out = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
524 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000525 case SymbolBody::DefinedRegularKind:
Rafael Espindolacd076f02015-09-25 18:19:03 +0000526 Section = &cast<DefinedRegular<ELFT>>(EBody).Section;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000527 break;
528 case SymbolBody::DefinedCommonKind:
Rafael Espindolac2d21192015-09-23 18:25:05 +0000529 Out = &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000530 break;
531 case SymbolBody::UndefinedKind:
532 case SymbolBody::DefinedAbsoluteKind:
533 case SymbolBody::SharedKind:
534 break;
535 case SymbolBody::LazyKind:
536 llvm_unreachable("Lazy symbol got to output symbol table!");
537 }
538
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000539 unsigned char Binding = InputSym.getBinding();
540 unsigned char Visibility = EBody.getMostConstrainingVisibility();
541 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
542 Binding = STB_LOCAL;
543
544 ESym->setBindingAndType(Binding, InputSym.getType());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000545 ESym->st_size = InputSym.st_size;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000546 ESym->setVisibility(Visibility);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000547 if (InputSym.isAbsolute()) {
548 ESym->st_shndx = SHN_ABS;
549 ESym->st_value = InputSym.st_value;
550 }
551
552 if (Section)
553 Out = Section->getOutputSection();
554
Rafael Espindolacd076f02015-09-25 18:19:03 +0000555 ESym->st_value = getSymVA(EBody, BssSec);
556
557 if (Out)
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000558 ESym->st_shndx = Out->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000559 }
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000560 if (!StrTabSec.isDynamic())
561 std::stable_sort(
562 reinterpret_cast<Elf_Sym *>(Start), reinterpret_cast<Elf_Sym *>(Buf),
563 [](const Elf_Sym &A, const Elf_Sym &B) -> bool {
564 return A.getBinding() == STB_LOCAL && B.getBinding() != STB_LOCAL;
565 });
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000566}
567
568namespace lld {
569namespace elf2 {
570template class OutputSectionBase<false>;
571template class OutputSectionBase<true>;
572
573template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000574 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000575template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000576 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000577template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000578 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000579template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000580 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000581
582template class GotSection<ELF32LE>;
583template class GotSection<ELF32BE>;
584template class GotSection<ELF64LE>;
585template class GotSection<ELF64BE>;
586
587template class PltSection<ELF32LE>;
588template class PltSection<ELF32BE>;
589template class PltSection<ELF64LE>;
590template class PltSection<ELF64BE>;
591
592template class RelocationSection<ELF32LE>;
593template class RelocationSection<ELF32BE>;
594template class RelocationSection<ELF64LE>;
595template class RelocationSection<ELF64BE>;
596
597template class InterpSection<false>;
598template class InterpSection<true>;
599
600template class HashTableSection<ELF32LE>;
601template class HashTableSection<ELF32BE>;
602template class HashTableSection<ELF64LE>;
603template class HashTableSection<ELF64BE>;
604
605template class DynamicSection<ELF32LE>;
606template class DynamicSection<ELF32BE>;
607template class DynamicSection<ELF64LE>;
608template class DynamicSection<ELF64BE>;
609
610template class OutputSection<ELF32LE>;
611template class OutputSection<ELF32BE>;
612template class OutputSection<ELF64LE>;
613template class OutputSection<ELF64BE>;
614
615template class StringTableSection<false>;
616template class StringTableSection<true>;
617
618template class SymbolTableSection<ELF32LE>;
619template class SymbolTableSection<ELF32BE>;
620template class SymbolTableSection<ELF64LE>;
621template class SymbolTableSection<ELF64BE>;
622
Rafael Espindolacd076f02015-09-25 18:19:03 +0000623template ELFFile<ELF32LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000624getSymVA(const ELFSymbolBody<ELF32LE> &, const OutputSection<ELF32LE> &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000625
Rafael Espindolacd076f02015-09-25 18:19:03 +0000626template ELFFile<ELF32BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000627getSymVA(const ELFSymbolBody<ELF32BE> &, const OutputSection<ELF32BE> &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000628
Rafael Espindolacd076f02015-09-25 18:19:03 +0000629template ELFFile<ELF64LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000630getSymVA(const ELFSymbolBody<ELF64LE> &, const OutputSection<ELF64LE> &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000631
Rafael Espindolacd076f02015-09-25 18:19:03 +0000632template ELFFile<ELF64BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000633getSymVA(const ELFSymbolBody<ELF64BE> &, const OutputSection<ELF64BE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000634
Rafael Espindola56f965f2015-09-21 22:48:12 +0000635template ELFFile<ELF32LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000636getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *, const ObjectFile<ELF32LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000637
Rafael Espindola56f965f2015-09-21 22:48:12 +0000638template ELFFile<ELF32BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000639getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *, const ObjectFile<ELF32BE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000640
Rafael Espindola56f965f2015-09-21 22:48:12 +0000641template ELFFile<ELF64LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000642getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *, const ObjectFile<ELF64LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000643
Rafael Espindola56f965f2015-09-21 22:48:12 +0000644template ELFFile<ELF64BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000645getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *, const ObjectFile<ELF64BE> &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000646
647template bool includeInSymtab<ELF32LE>(const SymbolBody &);
648template bool includeInSymtab<ELF32BE>(const SymbolBody &);
649template bool includeInSymtab<ELF64LE>(const SymbolBody &);
650template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000651
652template bool shouldKeepInSymtab<ELF32LE>(StringRef,
653 const ELFFile<ELF32LE>::Elf_Sym &);
654template bool shouldKeepInSymtab<ELF32BE>(StringRef,
655 const ELFFile<ELF32BE>::Elf_Sym &);
656template bool shouldKeepInSymtab<ELF64LE>(StringRef,
657 const ELFFile<ELF64LE>::Elf_Sym &);
658template bool shouldKeepInSymtab<ELF64BE>(StringRef,
659 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000660}
661}