blob: 9c89de3148e2b6bbce3081b60c1a51486772129d [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 Espindola3ef3a4c2015-09-29 23:22:16 +0000107 if (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();
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() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000186 if (this->Header.sh_size)
187 return; // Already finalized.
188
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000189 typename Base::HeaderT &Header = this->Header;
190 Header.sh_link = DynStrSec.getSectionIndex();
191
192 unsigned NumEntries = 0;
193 if (RelaDynSec.hasRelocs()) {
194 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000195 ++NumEntries; // DT_RELASZ / DT_RELSZ
196 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197 }
198 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000199 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200 ++NumEntries; // DT_STRTAB
201 ++NumEntries; // DT_STRSZ
202 ++NumEntries; // DT_HASH
203
Rui Ueyama7de3f372015-10-01 19:36:04 +0000204 if (!Config->RPath.empty()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000205 ++NumEntries; // DT_RUNPATH
Rui Ueyama7de3f372015-10-01 19:36:04 +0000206 DynStrSec.add(Config->RPath);
207 }
208
209 if (!Config->SoName.empty()) {
210 ++NumEntries; // DT_SONAME
211 DynStrSec.add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000212 }
213
214 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
215 SymTab.getSharedFiles();
216 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
Rafael Espindolac8b15812015-10-01 15:47:50 +0000217 DynStrSec.add(File->getSoName());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218 NumEntries += SharedFiles.size();
219
220 ++NumEntries; // DT_NULL
221
222 Header.sh_size = NumEntries * Header.sh_entsize;
223}
224
225template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
226 typedef typename std::conditional<ELFT::Is64Bits, Elf64_Dyn, Elf32_Dyn>::type
227 Elf_Dyn;
228 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
229
230 if (RelaDynSec.hasRelocs()) {
231 bool IsRela = RelaDynSec.isRela();
232 P->d_tag = IsRela ? DT_RELA : DT_REL;
233 P->d_un.d_ptr = RelaDynSec.getVA();
234 ++P;
235
236 P->d_tag = IsRela ? DT_RELASZ : DT_RELSZ;
237 P->d_un.d_val = RelaDynSec.getSize();
238 ++P;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000239
240 P->d_tag = IsRela ? DT_RELAENT : DT_RELENT;
241 P->d_un.d_val = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
242 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243 }
244
245 P->d_tag = DT_SYMTAB;
246 P->d_un.d_ptr = DynSymSec.getVA();
247 ++P;
248
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000249 P->d_tag = DT_SYMENT;
250 P->d_un.d_ptr = sizeof(Elf_Sym);
251 ++P;
252
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253 P->d_tag = DT_STRTAB;
254 P->d_un.d_ptr = DynStrSec.getVA();
255 ++P;
256
257 P->d_tag = DT_STRSZ;
258 P->d_un.d_val = DynStrSec.data().size();
259 ++P;
260
261 P->d_tag = DT_HASH;
262 P->d_un.d_ptr = HashSec.getVA();
263 ++P;
264
Rui Ueyama7de3f372015-10-01 19:36:04 +0000265 if (!Config->RPath.empty()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000266 P->d_tag = DT_RUNPATH;
Rui Ueyama7de3f372015-10-01 19:36:04 +0000267 P->d_un.d_val = DynStrSec.getFileOff(Config->RPath);
268 ++P;
269 }
270
271 if (!Config->SoName.empty()) {
272 P->d_tag = DT_SONAME;
273 P->d_un.d_val = DynStrSec.getFileOff(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000274 ++P;
275 }
276
277 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
278 SymTab.getSharedFiles();
279 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) {
280 P->d_tag = DT_NEEDED;
Rafael Espindolac8b15812015-10-01 15:47:50 +0000281 P->d_un.d_val = DynStrSec.getFileOff(File->getSoName());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000282 ++P;
283 }
284
285 P->d_tag = DT_NULL;
286 P->d_un.d_val = 0;
287 ++P;
288}
289
290template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000291OutputSection<ELFT>::OutputSection(const PltSection<ELFT> &PltSec,
292 const GotSection<ELFT> &GotSec,
293 const OutputSection<ELFT> &BssSec,
294 StringRef Name, uint32_t sh_type,
295 uintX_t sh_flags)
296 : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags),
297 PltSec(PltSec), GotSec(GotSec), BssSec(BssSec) {}
298
299template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000300void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
301 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000302 C->setOutputSection(this);
303 uint32_t Align = C->getAlign();
304 if (Align > this->Header.sh_addralign)
305 this->Header.sh_addralign = Align;
306
307 uintX_t Off = this->Header.sh_size;
308 Off = RoundUpToAlignment(Off, Align);
309 C->setOutputSectionOff(Off);
310 Off += C->getSize();
311 this->Header.sh_size = Off;
312}
313
314template <class ELFT>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000315typename ELFFile<ELFT>::uintX_t
Rafael Espindolacd076f02015-09-25 18:19:03 +0000316lld::elf2::getSymVA(const ELFSymbolBody<ELFT> &S,
317 const OutputSection<ELFT> &BssSec) {
318 switch (S.kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000319 case SymbolBody::DefinedSyntheticKind:
320 return cast<DefinedSynthetic<ELFT>>(S).Section.getVA() + S.Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000321 case SymbolBody::DefinedAbsoluteKind:
322 return S.Sym.st_value;
323 case SymbolBody::DefinedRegularKind: {
324 const auto &DR = cast<DefinedRegular<ELFT>>(S);
325 const InputSection<ELFT> *SC = &DR.Section;
326 OutputSection<ELFT> *OS = SC->getOutputSection();
327 return OS->getVA() + SC->getOutputSectionOff() + DR.Sym.st_value;
328 }
329 case SymbolBody::DefinedCommonKind:
330 return BssSec.getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
331 case SymbolBody::SharedKind:
332 case SymbolBody::UndefinedKind:
333 return 0;
334 case SymbolBody::LazyKind:
Rafael Espindola27322352015-09-28 22:12:54 +0000335 break;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000336 }
Rafael Espindola27322352015-09-28 22:12:54 +0000337 llvm_unreachable("Lazy symbol reached writer");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000338}
339
340template <class ELFT>
341typename ELFFile<ELFT>::uintX_t
342lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym,
343 const ObjectFile<ELFT> &File) {
344 uint32_t SecIndex = Sym->st_shndx;
345
346 if (SecIndex == SHN_XINDEX)
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000347 SecIndex = File.getObj().getExtendedSymbolTableIndex(
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000348 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000349 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
350 InputSection<ELFT> *Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000351 OutputSection<ELFT> *Out = Section->getOutputSection();
352 return Out->getVA() + Section->getOutputSectionOff() + Sym->st_value;
353}
354
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000355template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000356 for (InputSection<ELFT> *C : Sections)
Rafael Espindolac2d21192015-09-23 18:25:05 +0000357 C->writeTo(Buf, BssSec, PltSec, GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000358}
359
360template <bool Is64Bits>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000361StringTableSection<Is64Bits>::StringTableSection(bool Dynamic)
362 : OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab",
363 llvm::ELF::SHT_STRTAB,
364 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
365 Dynamic(Dynamic) {
366 this->Header.sh_addralign = 1;
367}
368
369template <bool Is64Bits>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000370void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
371 StringRef Data = StrTabBuilder.data();
372 memcpy(Buf, Data.data(), Data.size());
373}
374
375bool lld::elf2::includeInSymtab(const SymbolBody &B) {
376 if (B.isLazy())
377 return false;
378 if (!B.isUsedInRegularObj())
379 return false;
380 uint8_t V = B.getMostConstrainingVisibility();
381 if (V != STV_DEFAULT && V != STV_PROTECTED)
382 return false;
383 return true;
384}
385
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000386bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
387 if (Config->ExportDynamic || Config->Shared)
388 return true;
389 return B.isUsedInDynamicReloc();
390}
391
Davide Italiano6993ba42015-09-26 00:47:56 +0000392bool lld::elf2::shouldKeepInSymtab(StringRef SymName) {
393 if (Config->DiscardNone)
394 return true;
395
396 // ELF defines dynamic locals as symbols which name starts with ".L".
397 return !(Config->DiscardLocals && SymName.startswith(".L"));
398}
399
Rafael Espindola35c6af32015-09-25 17:19:10 +0000400template <class ELFT>
401SymbolTableSection<ELFT>::SymbolTableSection(
402 SymbolTable &Table, StringTableSection<ELFT::Is64Bits> &StrTabSec,
403 const OutputSection<ELFT> &BssSec)
404 : OutputSectionBase<ELFT::Is64Bits>(
405 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
406 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
407 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
408 Table(Table), StrTabSec(StrTabSec), BssSec(BssSec) {
409 typedef OutputSectionBase<ELFT::Is64Bits> Base;
410 typename Base::HeaderT &Header = this->Header;
411
412 Header.sh_entsize = sizeof(Elf_Sym);
413 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
414}
415
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000416template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000417 Buf += sizeof(Elf_Sym);
418
419 // All symbols with STB_LOCAL binding precede the weak and global symbols.
420 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000421 if (!Config->DiscardAll && !StrTabSec.isDynamic())
422 writeLocalSymbols(Buf);
423
424 writeGlobalSymbols(Buf);
425}
426
427template <class ELFT>
428void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
429 // Iterate over all input object files to copy their local symbols
430 // to the output symbol table pointed by Buf.
431 for (const std::unique_ptr<ObjectFileBase> &FileB : Table.getObjectFiles()) {
432 auto &File = cast<ObjectFile<ELFT>>(*FileB);
433 Elf_Sym_Range Syms = File.getLocalSymbols();
434 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000435 ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
436 if (SymName && !shouldKeepInSymtab(*SymName))
437 continue;
Rui Ueyamac55733e2015-09-30 00:54:29 +0000438 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
439 Buf += sizeof(*ESym);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000440 ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
441 ESym->st_size = Sym.st_size;
442 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
443 uint32_t SecIndex = Sym.st_shndx;
444 uintX_t VA = Sym.st_value;
445 if (SecIndex == SHN_ABS) {
446 ESym->st_shndx = SHN_ABS;
447 } else {
448 if (SecIndex == SHN_XINDEX)
449 SecIndex = File.getObj().getExtendedSymbolTableIndex(
450 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
451 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
452 const InputSection<ELFT> *Section = Sections[SecIndex];
453 const OutputSection<ELFT> *Out = Section->getOutputSection();
454 ESym->st_shndx = Out->getSectionIndex();
455 VA += Out->getVA() + Section->getOutputSectionOff();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000456 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000457 ESym->st_value = VA;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000458 }
459 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000460}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000461
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000462template <class ELFT>
463void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *&Buf) {
464 // Write the internal symbol table contents to the output symbol table
465 // pointed by Buf.
466 for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000467 StringRef Name = P.first;
468 Symbol *Sym = P.second;
469 SymbolBody *Body = Sym->Body;
470 if (!includeInSymtab(*Body))
471 continue;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000472 if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body))
473 continue;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000474
Rafael Espindolacd076f02015-09-25 18:19:03 +0000475 const auto &EBody = *cast<ELFSymbolBody<ELFT>>(Body);
476 const Elf_Sym &InputSym = EBody.Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000477 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac55733e2015-09-30 00:54:29 +0000478 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000479 ESym->st_name = StrTabSec.getFileOff(Name);
480
Rafael Espindola25b0acb2015-09-25 17:32:37 +0000481 const OutputSection<ELFT> *Out = nullptr;
482 const InputSection<ELFT> *Section = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000483
Rafael Espindolacd076f02015-09-25 18:19:03 +0000484 switch (EBody.kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000485 case SymbolBody::DefinedSyntheticKind:
486 Out = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
487 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000488 case SymbolBody::DefinedRegularKind:
Rafael Espindolacd076f02015-09-25 18:19:03 +0000489 Section = &cast<DefinedRegular<ELFT>>(EBody).Section;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000490 break;
491 case SymbolBody::DefinedCommonKind:
Rafael Espindolac2d21192015-09-23 18:25:05 +0000492 Out = &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000493 break;
494 case SymbolBody::UndefinedKind:
495 case SymbolBody::DefinedAbsoluteKind:
496 case SymbolBody::SharedKind:
497 break;
498 case SymbolBody::LazyKind:
499 llvm_unreachable("Lazy symbol got to output symbol table!");
500 }
501
502 ESym->setBindingAndType(InputSym.getBinding(), InputSym.getType());
503 ESym->st_size = InputSym.st_size;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000504 ESym->setVisibility(EBody.getMostConstrainingVisibility());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000505 if (InputSym.isAbsolute()) {
506 ESym->st_shndx = SHN_ABS;
507 ESym->st_value = InputSym.st_value;
508 }
509
510 if (Section)
511 Out = Section->getOutputSection();
512
Rafael Espindolacd076f02015-09-25 18:19:03 +0000513 ESym->st_value = getSymVA(EBody, BssSec);
514
515 if (Out)
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000516 ESym->st_shndx = Out->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000517 }
518}
519
520namespace lld {
521namespace elf2 {
522template class OutputSectionBase<false>;
523template class OutputSectionBase<true>;
524
525template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000526 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000527template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000528 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000529template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000530 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000531template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000532 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000533
534template class GotSection<ELF32LE>;
535template class GotSection<ELF32BE>;
536template class GotSection<ELF64LE>;
537template class GotSection<ELF64BE>;
538
539template class PltSection<ELF32LE>;
540template class PltSection<ELF32BE>;
541template class PltSection<ELF64LE>;
542template class PltSection<ELF64BE>;
543
544template class RelocationSection<ELF32LE>;
545template class RelocationSection<ELF32BE>;
546template class RelocationSection<ELF64LE>;
547template class RelocationSection<ELF64BE>;
548
549template class InterpSection<false>;
550template class InterpSection<true>;
551
552template class HashTableSection<ELF32LE>;
553template class HashTableSection<ELF32BE>;
554template class HashTableSection<ELF64LE>;
555template class HashTableSection<ELF64BE>;
556
557template class DynamicSection<ELF32LE>;
558template class DynamicSection<ELF32BE>;
559template class DynamicSection<ELF64LE>;
560template class DynamicSection<ELF64BE>;
561
562template class OutputSection<ELF32LE>;
563template class OutputSection<ELF32BE>;
564template class OutputSection<ELF64LE>;
565template class OutputSection<ELF64BE>;
566
567template class StringTableSection<false>;
568template class StringTableSection<true>;
569
570template class SymbolTableSection<ELF32LE>;
571template class SymbolTableSection<ELF32BE>;
572template class SymbolTableSection<ELF64LE>;
573template class SymbolTableSection<ELF64BE>;
574
Rafael Espindolacd076f02015-09-25 18:19:03 +0000575template ELFFile<ELF32LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000576getSymVA(const ELFSymbolBody<ELF32LE> &, const OutputSection<ELF32LE> &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000577
Rafael Espindolacd076f02015-09-25 18:19:03 +0000578template ELFFile<ELF32BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000579getSymVA(const ELFSymbolBody<ELF32BE> &, const OutputSection<ELF32BE> &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000580
Rafael Espindolacd076f02015-09-25 18:19:03 +0000581template ELFFile<ELF64LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000582getSymVA(const ELFSymbolBody<ELF64LE> &, const OutputSection<ELF64LE> &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000583
Rafael Espindolacd076f02015-09-25 18:19:03 +0000584template ELFFile<ELF64BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000585getSymVA(const ELFSymbolBody<ELF64BE> &, const OutputSection<ELF64BE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000586
Rafael Espindola56f965f2015-09-21 22:48:12 +0000587template ELFFile<ELF32LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000588getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *, const ObjectFile<ELF32LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000589
Rafael Espindola56f965f2015-09-21 22:48:12 +0000590template ELFFile<ELF32BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000591getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *, const ObjectFile<ELF32BE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000592
Rafael Espindola56f965f2015-09-21 22:48:12 +0000593template ELFFile<ELF64LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000594getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *, const ObjectFile<ELF64LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000595
Rafael Espindola56f965f2015-09-21 22:48:12 +0000596template ELFFile<ELF64BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000597getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *, const ObjectFile<ELF64BE> &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000598}
599}