blob: 13754ad0bf24737e01512cafacb135facac416ee [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 Espindola01205f72015-09-22 18:19:46 +0000107 if (Target->relocNeedsGot(Type)) {
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();
113 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type, IsMips64EL);
114 if (IsRela)
Rafael Espindola50534c22015-09-22 17:49:38 +0000115 static_cast<Elf_Rela *>(P)->r_addend =
116 static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000117 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000118 }
119}
120
121template <class ELFT> void RelocationSection<ELFT>::finalize() {
122 this->Header.sh_link = DynSymSec.getSectionIndex();
123 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
124}
125
126template <bool Is64Bits>
127InterpSection<Is64Bits>::InterpSection()
128 : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS,
129 llvm::ELF::SHF_ALLOC) {
130 this->Header.sh_size = Config->DynamicLinker.size() + 1;
131 this->Header.sh_addralign = 1;
132}
133
134template <bool Is64Bits>
135template <endianness E>
136void OutputSectionBase<Is64Bits>::writeHeaderTo(
137 typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) {
138 SHdr->sh_name = Header.sh_name;
139 SHdr->sh_type = Header.sh_type;
140 SHdr->sh_flags = Header.sh_flags;
141 SHdr->sh_addr = Header.sh_addr;
142 SHdr->sh_offset = Header.sh_offset;
143 SHdr->sh_size = Header.sh_size;
144 SHdr->sh_link = Header.sh_link;
145 SHdr->sh_info = Header.sh_info;
146 SHdr->sh_addralign = Header.sh_addralign;
147 SHdr->sh_entsize = Header.sh_entsize;
148}
149
150template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) {
151 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
152}
153
Rafael Espindola35c6af32015-09-25 17:19:10 +0000154template <class ELFT>
155HashTableSection<ELFT>::HashTableSection(SymbolTableSection<ELFT> &DynSymSec)
156 : OutputSectionBase<ELFT::Is64Bits>(".hash", llvm::ELF::SHT_HASH,
157 llvm::ELF::SHF_ALLOC),
158 DynSymSec(DynSymSec) {
159 this->Header.sh_entsize = sizeof(Elf_Word);
160 this->Header.sh_addralign = sizeof(Elf_Word);
161}
162
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
164 StringRef Name = S->getName();
165 DynSymSec.addSymbol(Name);
166 Hashes.push_back(hash(Name));
167 S->setDynamicSymbolTableIndex(Hashes.size());
168}
169
Rafael Espindola35c6af32015-09-25 17:19:10 +0000170template <class ELFT>
171DynamicSection<ELFT>::DynamicSection(SymbolTable &SymTab,
172 HashTableSection<ELFT> &HashSec,
173 RelocationSection<ELFT> &RelaDynSec)
174 : OutputSectionBase<ELFT::Is64Bits>(".dynamic", llvm::ELF::SHT_DYNAMIC,
175 llvm::ELF::SHF_ALLOC |
176 llvm::ELF::SHF_WRITE),
177 HashSec(HashSec), DynSymSec(HashSec.getDynSymSec()),
178 DynStrSec(DynSymSec.getStrTabSec()), RelaDynSec(RelaDynSec),
179 SymTab(SymTab) {
180 typename Base::HeaderT &Header = this->Header;
181 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
182 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
183}
184
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000185template <class ELFT> void DynamicSection<ELFT>::finalize() {
186 typename Base::HeaderT &Header = this->Header;
187 Header.sh_link = DynStrSec.getSectionIndex();
188
189 unsigned NumEntries = 0;
190 if (RelaDynSec.hasRelocs()) {
191 ++NumEntries; // DT_RELA / DT_REL
192 ++NumEntries; // DT_RELASZ / DTRELSZ
193 }
194 ++NumEntries; // DT_SYMTAB
195 ++NumEntries; // DT_STRTAB
196 ++NumEntries; // DT_STRSZ
197 ++NumEntries; // DT_HASH
198
199 StringRef RPath = Config->RPath;
200 if (!RPath.empty()) {
201 ++NumEntries; // DT_RUNPATH
202 DynStrSec.add(RPath);
203 }
204
205 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
206 SymTab.getSharedFiles();
207 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
208 DynStrSec.add(File->getName());
209 NumEntries += SharedFiles.size();
210
211 ++NumEntries; // DT_NULL
212
213 Header.sh_size = NumEntries * Header.sh_entsize;
214}
215
216template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
217 typedef typename std::conditional<ELFT::Is64Bits, Elf64_Dyn, Elf32_Dyn>::type
218 Elf_Dyn;
219 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
220
221 if (RelaDynSec.hasRelocs()) {
222 bool IsRela = RelaDynSec.isRela();
223 P->d_tag = IsRela ? DT_RELA : DT_REL;
224 P->d_un.d_ptr = RelaDynSec.getVA();
225 ++P;
226
227 P->d_tag = IsRela ? DT_RELASZ : DT_RELSZ;
228 P->d_un.d_val = RelaDynSec.getSize();
229 ++P;
230 }
231
232 P->d_tag = DT_SYMTAB;
233 P->d_un.d_ptr = DynSymSec.getVA();
234 ++P;
235
236 P->d_tag = DT_STRTAB;
237 P->d_un.d_ptr = DynStrSec.getVA();
238 ++P;
239
240 P->d_tag = DT_STRSZ;
241 P->d_un.d_val = DynStrSec.data().size();
242 ++P;
243
244 P->d_tag = DT_HASH;
245 P->d_un.d_ptr = HashSec.getVA();
246 ++P;
247
248 StringRef RPath = Config->RPath;
249 if (!RPath.empty()) {
250 P->d_tag = DT_RUNPATH;
251 P->d_un.d_val = DynStrSec.getFileOff(RPath);
252 ++P;
253 }
254
255 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
256 SymTab.getSharedFiles();
257 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) {
258 P->d_tag = DT_NEEDED;
259 P->d_un.d_val = DynStrSec.getFileOff(File->getName());
260 ++P;
261 }
262
263 P->d_tag = DT_NULL;
264 P->d_un.d_val = 0;
265 ++P;
266}
267
268template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000269OutputSection<ELFT>::OutputSection(const PltSection<ELFT> &PltSec,
270 const GotSection<ELFT> &GotSec,
271 const OutputSection<ELFT> &BssSec,
272 StringRef Name, uint32_t sh_type,
273 uintX_t sh_flags)
274 : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags),
275 PltSec(PltSec), GotSec(GotSec), BssSec(BssSec) {}
276
277template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000278void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
279 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000280 C->setOutputSection(this);
281 uint32_t Align = C->getAlign();
282 if (Align > this->Header.sh_addralign)
283 this->Header.sh_addralign = Align;
284
285 uintX_t Off = this->Header.sh_size;
286 Off = RoundUpToAlignment(Off, Align);
287 C->setOutputSectionOff(Off);
288 Off += C->getSize();
289 this->Header.sh_size = Off;
290}
291
292template <class ELFT>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000293typename ELFFile<ELFT>::uintX_t
Rafael Espindolacd076f02015-09-25 18:19:03 +0000294lld::elf2::getSymVA(const ELFSymbolBody<ELFT> &S,
295 const OutputSection<ELFT> &BssSec) {
296 switch (S.kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000297 case SymbolBody::DefinedSyntheticKind:
298 return cast<DefinedSynthetic<ELFT>>(S).Section.getVA() + S.Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000299 case SymbolBody::DefinedAbsoluteKind:
300 return S.Sym.st_value;
301 case SymbolBody::DefinedRegularKind: {
302 const auto &DR = cast<DefinedRegular<ELFT>>(S);
303 const InputSection<ELFT> *SC = &DR.Section;
304 OutputSection<ELFT> *OS = SC->getOutputSection();
305 return OS->getVA() + SC->getOutputSectionOff() + DR.Sym.st_value;
306 }
307 case SymbolBody::DefinedCommonKind:
308 return BssSec.getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
309 case SymbolBody::SharedKind:
310 case SymbolBody::UndefinedKind:
311 return 0;
312 case SymbolBody::LazyKind:
Rafael Espindola27322352015-09-28 22:12:54 +0000313 break;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000314 }
Rafael Espindola27322352015-09-28 22:12:54 +0000315 llvm_unreachable("Lazy symbol reached writer");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000316}
317
318template <class ELFT>
319typename ELFFile<ELFT>::uintX_t
320lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym,
321 const ObjectFile<ELFT> &File) {
322 uint32_t SecIndex = Sym->st_shndx;
323
324 if (SecIndex == SHN_XINDEX)
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000325 SecIndex = File.getObj().getExtendedSymbolTableIndex(
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000326 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000327 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
328 InputSection<ELFT> *Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000329 OutputSection<ELFT> *Out = Section->getOutputSection();
330 return Out->getVA() + Section->getOutputSectionOff() + Sym->st_value;
331}
332
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000333template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000334 for (InputSection<ELFT> *C : Sections)
Rafael Espindolac2d21192015-09-23 18:25:05 +0000335 C->writeTo(Buf, BssSec, PltSec, GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000336}
337
338template <bool Is64Bits>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000339StringTableSection<Is64Bits>::StringTableSection(bool Dynamic)
340 : OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab",
341 llvm::ELF::SHT_STRTAB,
342 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
343 Dynamic(Dynamic) {
344 this->Header.sh_addralign = 1;
345}
346
347template <bool Is64Bits>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000348void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
349 StringRef Data = StrTabBuilder.data();
350 memcpy(Buf, Data.data(), Data.size());
351}
352
353bool lld::elf2::includeInSymtab(const SymbolBody &B) {
354 if (B.isLazy())
355 return false;
356 if (!B.isUsedInRegularObj())
357 return false;
358 uint8_t V = B.getMostConstrainingVisibility();
359 if (V != STV_DEFAULT && V != STV_PROTECTED)
360 return false;
361 return true;
362}
363
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000364bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
365 if (Config->ExportDynamic || Config->Shared)
366 return true;
367 return B.isUsedInDynamicReloc();
368}
369
Davide Italiano6993ba42015-09-26 00:47:56 +0000370bool lld::elf2::shouldKeepInSymtab(StringRef SymName) {
371 if (Config->DiscardNone)
372 return true;
373
374 // ELF defines dynamic locals as symbols which name starts with ".L".
375 return !(Config->DiscardLocals && SymName.startswith(".L"));
376}
377
Rafael Espindola35c6af32015-09-25 17:19:10 +0000378template <class ELFT>
379SymbolTableSection<ELFT>::SymbolTableSection(
380 SymbolTable &Table, StringTableSection<ELFT::Is64Bits> &StrTabSec,
381 const OutputSection<ELFT> &BssSec)
382 : OutputSectionBase<ELFT::Is64Bits>(
383 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
384 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
385 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
386 Table(Table), StrTabSec(StrTabSec), BssSec(BssSec) {
387 typedef OutputSectionBase<ELFT::Is64Bits> Base;
388 typename Base::HeaderT &Header = this->Header;
389
390 Header.sh_entsize = sizeof(Elf_Sym);
391 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
392}
393
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000394template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000395 Buf += sizeof(Elf_Sym);
396
397 // All symbols with STB_LOCAL binding precede the weak and global symbols.
398 // .dynsym only contains global symbols.
399 if (!Config->DiscardAll && !StrTabSec.isDynamic()) {
400 for (const std::unique_ptr<ObjectFileBase> &FileB :
401 Table.getObjectFiles()) {
402 auto &File = cast<ObjectFile<ELFT>>(*FileB);
403 Elf_Sym_Range Syms = File.getLocalSymbols();
404 for (const Elf_Sym &Sym : Syms) {
405 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406 ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
Davide Italianod75d3b92015-09-24 15:08:23 +0000407 if (SymName && !shouldKeepInSymtab(*SymName))
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000408 continue;
409 ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
410 ESym->st_size = Sym.st_size;
411 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rafael Espindoladfc72002015-09-28 18:29:47 +0000412 uint32_t SecIndex = Sym.st_shndx;
413 uintX_t VA = Sym.st_value;
414 if (SecIndex == SHN_ABS) {
415 ESym->st_shndx = SHN_ABS;
416 } else {
417 if (SecIndex == SHN_XINDEX)
418 SecIndex = File.getObj().getExtendedSymbolTableIndex(
419 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
420 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
421 const InputSection<ELFT> *Section = Sections[SecIndex];
422 const OutputSection<ELFT> *Out = Section->getOutputSection();
423 ESym->st_shndx = Out->getSectionIndex();
424 VA += Out->getVA() + Section->getOutputSectionOff();
425 }
426 ESym->st_value = VA;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000427 Buf += sizeof(Elf_Sym);
428 }
429 }
430 }
431
432 for (auto &P : Table.getSymbols()) {
433 StringRef Name = P.first;
434 Symbol *Sym = P.second;
435 SymbolBody *Body = Sym->Body;
436 if (!includeInSymtab(*Body))
437 continue;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000438 if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body))
439 continue;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000440
Rafael Espindolacd076f02015-09-25 18:19:03 +0000441 const auto &EBody = *cast<ELFSymbolBody<ELFT>>(Body);
442 const Elf_Sym &InputSym = EBody.Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000443 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
444 ESym->st_name = StrTabSec.getFileOff(Name);
445
Rafael Espindola25b0acb2015-09-25 17:32:37 +0000446 const OutputSection<ELFT> *Out = nullptr;
447 const InputSection<ELFT> *Section = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000448
Rafael Espindolacd076f02015-09-25 18:19:03 +0000449 switch (EBody.kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000450 case SymbolBody::DefinedSyntheticKind:
451 Out = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
452 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000453 case SymbolBody::DefinedRegularKind:
Rafael Espindolacd076f02015-09-25 18:19:03 +0000454 Section = &cast<DefinedRegular<ELFT>>(EBody).Section;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000455 break;
456 case SymbolBody::DefinedCommonKind:
Rafael Espindolac2d21192015-09-23 18:25:05 +0000457 Out = &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000458 break;
459 case SymbolBody::UndefinedKind:
460 case SymbolBody::DefinedAbsoluteKind:
461 case SymbolBody::SharedKind:
462 break;
463 case SymbolBody::LazyKind:
464 llvm_unreachable("Lazy symbol got to output symbol table!");
465 }
466
467 ESym->setBindingAndType(InputSym.getBinding(), InputSym.getType());
468 ESym->st_size = InputSym.st_size;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000469 ESym->setVisibility(EBody.getMostConstrainingVisibility());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000470 if (InputSym.isAbsolute()) {
471 ESym->st_shndx = SHN_ABS;
472 ESym->st_value = InputSym.st_value;
473 }
474
475 if (Section)
476 Out = Section->getOutputSection();
477
Rafael Espindolacd076f02015-09-25 18:19:03 +0000478 ESym->st_value = getSymVA(EBody, BssSec);
479
480 if (Out)
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000481 ESym->st_shndx = Out->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000482
483 Buf += sizeof(Elf_Sym);
484 }
485}
486
487namespace lld {
488namespace elf2 {
489template class OutputSectionBase<false>;
490template class OutputSectionBase<true>;
491
492template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000493 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000494template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000495 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000496template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000497 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000498template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000499 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000500
501template class GotSection<ELF32LE>;
502template class GotSection<ELF32BE>;
503template class GotSection<ELF64LE>;
504template class GotSection<ELF64BE>;
505
506template class PltSection<ELF32LE>;
507template class PltSection<ELF32BE>;
508template class PltSection<ELF64LE>;
509template class PltSection<ELF64BE>;
510
511template class RelocationSection<ELF32LE>;
512template class RelocationSection<ELF32BE>;
513template class RelocationSection<ELF64LE>;
514template class RelocationSection<ELF64BE>;
515
516template class InterpSection<false>;
517template class InterpSection<true>;
518
519template class HashTableSection<ELF32LE>;
520template class HashTableSection<ELF32BE>;
521template class HashTableSection<ELF64LE>;
522template class HashTableSection<ELF64BE>;
523
524template class DynamicSection<ELF32LE>;
525template class DynamicSection<ELF32BE>;
526template class DynamicSection<ELF64LE>;
527template class DynamicSection<ELF64BE>;
528
529template class OutputSection<ELF32LE>;
530template class OutputSection<ELF32BE>;
531template class OutputSection<ELF64LE>;
532template class OutputSection<ELF64BE>;
533
534template class StringTableSection<false>;
535template class StringTableSection<true>;
536
537template class SymbolTableSection<ELF32LE>;
538template class SymbolTableSection<ELF32BE>;
539template class SymbolTableSection<ELF64LE>;
540template class SymbolTableSection<ELF64BE>;
541
Rafael Espindolacd076f02015-09-25 18:19:03 +0000542template ELFFile<ELF32LE>::uintX_t
543getSymVA(const ELFSymbolBody<ELF32LE> &S, const OutputSection<ELF32LE> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000544
Rafael Espindolacd076f02015-09-25 18:19:03 +0000545template ELFFile<ELF32BE>::uintX_t
546getSymVA(const ELFSymbolBody<ELF32BE> &S, const OutputSection<ELF32BE> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000547
Rafael Espindolacd076f02015-09-25 18:19:03 +0000548template ELFFile<ELF64LE>::uintX_t
549getSymVA(const ELFSymbolBody<ELF64LE> &S, const OutputSection<ELF64LE> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000550
Rafael Espindolacd076f02015-09-25 18:19:03 +0000551template ELFFile<ELF64BE>::uintX_t
552getSymVA(const ELFSymbolBody<ELF64BE> &S, const OutputSection<ELF64BE> &BssSec);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000553
Rafael Espindola56f965f2015-09-21 22:48:12 +0000554template ELFFile<ELF32LE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000555getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *Sym,
556 const ObjectFile<ELF32LE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000557
Rafael Espindola56f965f2015-09-21 22:48:12 +0000558template ELFFile<ELF32BE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000559getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *Sym,
560 const ObjectFile<ELF32BE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000561
Rafael Espindola56f965f2015-09-21 22:48:12 +0000562template ELFFile<ELF64LE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000563getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *Sym,
564 const ObjectFile<ELF64LE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000565
Rafael Espindola56f965f2015-09-21 22:48:12 +0000566template ELFFile<ELF64BE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000567getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *Sym,
568 const ObjectFile<ELF64BE> &File);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000569}
570}