blob: 6caf99069b488a9df68110f1a3524d40c4b756fc [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.cpp -------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "OutputSections.h"
11#include "Config.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000012#include "SymbolTable.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000013#include "Target.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000014
Rafael Espindola5805c4f2015-09-21 21:38:08 +000015using namespace llvm;
16using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000017using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000018using namespace llvm::ELF;
19
20using namespace lld;
21using namespace lld::elf2;
22
23template <bool Is64Bits>
24OutputSectionBase<Is64Bits>::OutputSectionBase(StringRef Name, uint32_t sh_type,
25 uintX_t sh_flags)
26 : Name(Name) {
27 memset(&Header, 0, sizeof(HeaderT));
28 Header.sh_type = sh_type;
29 Header.sh_flags = sh_flags;
30}
31
Rafael Espindola35c6af32015-09-25 17:19:10 +000032template <class ELFT>
Rafael Espindolaa6627382015-10-06 23:56:53 +000033GotSection<ELFT>::GotSection(const OutputSection<ELFT> &BssSec)
Rafael Espindola35c6af32015-09-25 17:19:10 +000034 : OutputSectionBase<ELFT::Is64Bits>(".got", llvm::ELF::SHT_PROGBITS,
35 llvm::ELF::SHF_ALLOC |
Rafael Espindolaa6627382015-10-06 23:56:53 +000036 llvm::ELF::SHF_WRITE),
37 BssSec(BssSec) {
Rafael Espindola35c6af32015-09-25 17:19:10 +000038 this->Header.sh_addralign = this->getAddrSize();
39}
40
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
42 Sym->setGotIndex(Entries.size());
43 Entries.push_back(Sym);
44}
45
46template <class ELFT>
47typename GotSection<ELFT>::uintX_t
48GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
49 return this->getVA() + B.getGotIndex() * this->getAddrSize();
50}
51
Rafael Espindolaa6627382015-10-06 23:56:53 +000052template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
53 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +000054 uint8_t *Entry = Buf;
55 Buf += sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +000056 if (canBePreempted(B))
57 continue; // The dynamic linker will take care of it.
58 uintX_t VA = getSymVA(*B, BssSec);
Rafael Espindolae782f672015-10-07 03:56:05 +000059 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +000060 }
61}
62
Rafael Espindola35c6af32015-09-25 17:19:10 +000063template <class ELFT>
64PltSection<ELFT>::PltSection(const GotSection<ELFT> &GotSec)
65 : OutputSectionBase<ELFT::Is64Bits>(".plt", llvm::ELF::SHT_PROGBITS,
66 llvm::ELF::SHF_ALLOC |
67 llvm::ELF::SHF_EXECINSTR),
68 GotSec(GotSec) {
69 this->Header.sh_addralign = 16;
70}
71
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
73 uintptr_t Start = reinterpret_cast<uintptr_t>(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074 for (const SymbolBody *E : Entries) {
Rafael Espindola01205f72015-09-22 18:19:46 +000075 uint64_t GotEntryAddr = GotSec.getEntryAddr(*E);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000076 uintptr_t InstPos = reinterpret_cast<uintptr_t>(Buf);
Rafael Espindola01205f72015-09-22 18:19:46 +000077 uint64_t PltEntryAddr = (InstPos - Start) + this->getVA();
78 Target->writePltEntry(Buf, GotEntryAddr, PltEntryAddr);
79 Buf += 8;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080 }
81}
82
83template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
84 Sym->setPltIndex(Entries.size());
85 Entries.push_back(Sym);
86}
87
88template <class ELFT>
89typename PltSection<ELFT>::uintX_t
90PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
91 return this->getVA() + B.getPltIndex() * EntrySize;
92}
93
Rafael Espindola35c6af32015-09-25 17:19:10 +000094template <class ELFT>
95RelocationSection<ELFT>::RelocationSection(SymbolTableSection<ELFT> &DynSymSec,
96 const GotSection<ELFT> &GotSec,
Rafael Espindola9c3e4d22015-10-05 21:23:08 +000097 const OutputSection<ELFT> &BssSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +000098 bool IsRela)
99 : OutputSectionBase<ELFT::Is64Bits>(IsRela ? ".rela.dyn" : ".rel.dyn",
100 IsRela ? llvm::ELF::SHT_RELA
101 : llvm::ELF::SHT_REL,
102 llvm::ELF::SHF_ALLOC),
Rafael Espindola9c3e4d22015-10-05 21:23:08 +0000103 DynSymSec(DynSymSec), GotSec(GotSec), BssSec(BssSec), IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000104 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
105 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
106}
107
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000108template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000109 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000110 bool IsMips64EL = Relocs[0].C.getFile()->getObj().isMips64EL();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000111 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000112 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
113 Buf += EntrySize;
114
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000115 const InputSection<ELFT> &C = Rel.C;
116 const Elf_Rel &RI = Rel.RI;
117 OutputSection<ELFT> *Out = C.getOutputSection();
118 uint32_t SymIndex = RI.getSymbol(IsMips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000119 const ObjectFile<ELFT> &File = *C.getFile();
120 const SymbolBody *Body = File.getSymbolBody(SymIndex);
121 const ELFFile<ELFT> &Obj = File.getObj();
122
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123 uint32_t Type = RI.getType(IsMips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000124
125 bool CanBePreempted = canBePreempted(Body);
126 uintX_t Addend = 0;
127 if (!CanBePreempted) {
128 if (IsRela) {
129 if (Body)
130 Addend += getSymVA(cast<ELFSymbolBody<ELFT>>(*Body), BssSec);
131 else
132 Addend += getLocalSymVA(
133 Obj.getRelocationSymbol(&RI, File.getSymbolTable()), File);
134 }
135 P->setSymbolAndType(0, Target->getRelativeReloc(), IsMips64EL);
136 }
137
Rafael Espindolaae244002015-10-05 19:30:12 +0000138 if (Body && Target->relocNeedsGot(Type, *Body)) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000139 P->r_offset = GotSec.getEntryAddr(*Body);
Rafael Espindola52dca342015-10-07 00:58:20 +0000140 if (CanBePreempted)
141 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(),
142 Target->getGotReloc(), IsMips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000143 } else {
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000144 if (IsRela)
Rafael Espindola52dca342015-10-07 00:58:20 +0000145 Addend += static_cast<const Elf_Rela &>(RI).r_addend;
146 P->r_offset = RI.r_offset + C.getOutputSectionOff() + Out->getVA();
147 if (CanBePreempted)
Rafael Espindolaae244002015-10-05 19:30:12 +0000148 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type,
149 IsMips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000150 }
Rafael Espindola52dca342015-10-07 00:58:20 +0000151
152 if (IsRela)
153 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000154 }
155}
156
157template <class ELFT> void RelocationSection<ELFT>::finalize() {
158 this->Header.sh_link = DynSymSec.getSectionIndex();
159 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
160}
161
162template <bool Is64Bits>
163InterpSection<Is64Bits>::InterpSection()
164 : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS,
165 llvm::ELF::SHF_ALLOC) {
166 this->Header.sh_size = Config->DynamicLinker.size() + 1;
167 this->Header.sh_addralign = 1;
168}
169
170template <bool Is64Bits>
171template <endianness E>
172void OutputSectionBase<Is64Bits>::writeHeaderTo(
173 typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) {
174 SHdr->sh_name = Header.sh_name;
175 SHdr->sh_type = Header.sh_type;
176 SHdr->sh_flags = Header.sh_flags;
177 SHdr->sh_addr = Header.sh_addr;
178 SHdr->sh_offset = Header.sh_offset;
179 SHdr->sh_size = Header.sh_size;
180 SHdr->sh_link = Header.sh_link;
181 SHdr->sh_info = Header.sh_info;
182 SHdr->sh_addralign = Header.sh_addralign;
183 SHdr->sh_entsize = Header.sh_entsize;
184}
185
186template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) {
187 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
188}
189
Rafael Espindola35c6af32015-09-25 17:19:10 +0000190template <class ELFT>
191HashTableSection<ELFT>::HashTableSection(SymbolTableSection<ELFT> &DynSymSec)
192 : OutputSectionBase<ELFT::Is64Bits>(".hash", llvm::ELF::SHT_HASH,
193 llvm::ELF::SHF_ALLOC),
194 DynSymSec(DynSymSec) {
195 this->Header.sh_entsize = sizeof(Elf_Word);
196 this->Header.sh_addralign = sizeof(Elf_Word);
197}
198
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
200 StringRef Name = S->getName();
201 DynSymSec.addSymbol(Name);
202 Hashes.push_back(hash(Name));
203 S->setDynamicSymbolTableIndex(Hashes.size());
204}
205
Rafael Espindola35c6af32015-09-25 17:19:10 +0000206template <class ELFT>
207DynamicSection<ELFT>::DynamicSection(SymbolTable &SymTab,
208 HashTableSection<ELFT> &HashSec,
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000209 RelocationSection<ELFT> &RelaDynSec,
210 const OutputSection<ELFT> &BssSec)
Rafael Espindola35c6af32015-09-25 17:19:10 +0000211 : OutputSectionBase<ELFT::Is64Bits>(".dynamic", llvm::ELF::SHT_DYNAMIC,
212 llvm::ELF::SHF_ALLOC |
213 llvm::ELF::SHF_WRITE),
214 HashSec(HashSec), DynSymSec(HashSec.getDynSymSec()),
215 DynStrSec(DynSymSec.getStrTabSec()), RelaDynSec(RelaDynSec),
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000216 BssSec(BssSec), SymTab(SymTab) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000217 typename Base::HeaderT &Header = this->Header;
218 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
219 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
220}
221
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000222template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000223 if (this->Header.sh_size)
224 return; // Already finalized.
225
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000226 typename Base::HeaderT &Header = this->Header;
227 Header.sh_link = DynStrSec.getSectionIndex();
228
229 unsigned NumEntries = 0;
230 if (RelaDynSec.hasRelocs()) {
231 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000232 ++NumEntries; // DT_RELASZ / DT_RELSZ
233 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000234 }
235 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000236 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000237 ++NumEntries; // DT_STRTAB
238 ++NumEntries; // DT_STRSZ
239 ++NumEntries; // DT_HASH
240
Rui Ueyama7de3f372015-10-01 19:36:04 +0000241 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000242 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama7de3f372015-10-01 19:36:04 +0000243 DynStrSec.add(Config->RPath);
244 }
245
246 if (!Config->SoName.empty()) {
247 ++NumEntries; // DT_SONAME
248 DynStrSec.add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249 }
250
Rafael Espindola77572242015-10-02 19:37:55 +0000251 if (PreInitArraySec)
252 NumEntries += 2;
253 if (InitArraySec)
254 NumEntries += 2;
255 if (FiniArraySec)
256 NumEntries += 2;
257
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000258 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
259 SymTab.getSharedFiles();
260 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
Rafael Espindolac8b15812015-10-01 15:47:50 +0000261 DynStrSec.add(File->getSoName());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262 NumEntries += SharedFiles.size();
263
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000264 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
265 InitSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
266 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
267 FiniSym = dyn_cast<ELFSymbolBody<ELFT>>(S->Body);
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000268 if (InitSym)
269 ++NumEntries; // DT_INIT
270 if (FiniSym)
271 ++NumEntries; // DT_FINI
272
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000273 ++NumEntries; // DT_NULL
274
275 Header.sh_size = NumEntries * Header.sh_entsize;
276}
277
278template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000279 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
280
Rui Ueyama8c205d52015-10-02 01:33:31 +0000281 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
282 P->d_tag = Tag;
283 P->d_un.d_ptr = Val;
284 ++P;
285 };
286
287 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
288 P->d_tag = Tag;
289 P->d_un.d_val = Val;
290 ++P;
291 };
292
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000293 if (RelaDynSec.hasRelocs()) {
294 bool IsRela = RelaDynSec.isRela();
Rui Ueyama8c205d52015-10-02 01:33:31 +0000295 WritePtr(IsRela ? DT_RELA : DT_REL, RelaDynSec.getVA());
296 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, RelaDynSec.getSize());
297 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
298 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000299 }
300
Rui Ueyama8c205d52015-10-02 01:33:31 +0000301 WritePtr(DT_SYMTAB, DynSymSec.getVA());
302 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
303 WritePtr(DT_STRTAB, DynStrSec.getVA());
304 WriteVal(DT_STRSZ, DynStrSec.data().size());
305 WritePtr(DT_HASH, HashSec.getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000306
Rui Ueyama8c205d52015-10-02 01:33:31 +0000307 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000308
309 // If --enable-new-dtags is set lld emits DT_RUNPATH
310 // instead of DT_RPATH. The two tags are functionally
311 // equivalent except for the following:
312 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
313 // DT_RPATH is searched before.
314 // - DT_RUNPATH is used only to search for direct
315 // dependencies of the object it's contained in, while
316 // DT_RPATH is used for indirect dependencies as well.
317 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
318 DynStrSec.getFileOff(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000319
Rui Ueyama8c205d52015-10-02 01:33:31 +0000320 if (!Config->SoName.empty())
321 WriteVal(DT_SONAME, DynStrSec.getFileOff(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000322
Rafael Espindola77572242015-10-02 19:37:55 +0000323 auto WriteArray = [&](int32_t T1, int32_t T2,
324 const OutputSection<ELFT> *Sec) {
325 if (!Sec)
326 return;
327 WritePtr(T1, Sec->getVA());
328 WriteVal(T2, Sec->getSize());
329 };
330 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
331 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
332 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
333
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000334 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
335 SymTab.getSharedFiles();
Rui Ueyama8c205d52015-10-02 01:33:31 +0000336 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
337 WriteVal(DT_NEEDED, DynStrSec.getFileOff(File->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000338
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000339 if (InitSym)
340 WritePtr(DT_INIT, getSymVA(*InitSym, BssSec));
341 if (FiniSym)
342 WritePtr(DT_FINI, getSymVA(*FiniSym, BssSec));
343
Rui Ueyama8c205d52015-10-02 01:33:31 +0000344 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000345}
346
347template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000348OutputSection<ELFT>::OutputSection(const PltSection<ELFT> &PltSec,
349 const GotSection<ELFT> &GotSec,
350 const OutputSection<ELFT> &BssSec,
351 StringRef Name, uint32_t sh_type,
352 uintX_t sh_flags)
353 : OutputSectionBase<ELFT::Is64Bits>(Name, sh_type, sh_flags),
354 PltSec(PltSec), GotSec(GotSec), BssSec(BssSec) {}
355
356template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000357void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
358 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000359 C->setOutputSection(this);
360 uint32_t Align = C->getAlign();
361 if (Align > this->Header.sh_addralign)
362 this->Header.sh_addralign = Align;
363
364 uintX_t Off = this->Header.sh_size;
365 Off = RoundUpToAlignment(Off, Align);
366 C->setOutputSectionOff(Off);
367 Off += C->getSize();
368 this->Header.sh_size = Off;
369}
370
371template <class ELFT>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000372typename ELFFile<ELFT>::uintX_t
Rafael Espindola8614c562015-10-06 14:33:58 +0000373lld::elf2::getSymVA(const SymbolBody &S, const OutputSection<ELFT> &BssSec) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000374 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000375 case SymbolBody::DefinedSyntheticKind: {
376 auto &D = cast<DefinedSynthetic<ELFT>>(S);
377 return D.Section.getVA() + D.Sym.st_value;
378 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000379 case SymbolBody::DefinedAbsoluteKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000380 return cast<DefinedAbsolute<ELFT>>(S).Sym.st_value;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000381 case SymbolBody::DefinedRegularKind: {
382 const auto &DR = cast<DefinedRegular<ELFT>>(S);
383 const InputSection<ELFT> *SC = &DR.Section;
384 OutputSection<ELFT> *OS = SC->getOutputSection();
385 return OS->getVA() + SC->getOutputSectionOff() + DR.Sym.st_value;
386 }
387 case SymbolBody::DefinedCommonKind:
388 return BssSec.getVA() + cast<DefinedCommon<ELFT>>(S).OffsetInBSS;
389 case SymbolBody::SharedKind:
390 case SymbolBody::UndefinedKind:
391 return 0;
392 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000393 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
394 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000395 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000396}
397
398template <class ELFT>
399typename ELFFile<ELFT>::uintX_t
400lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym,
401 const ObjectFile<ELFT> &File) {
402 uint32_t SecIndex = Sym->st_shndx;
403
404 if (SecIndex == SHN_XINDEX)
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000405 SecIndex = File.getObj().getExtendedSymbolTableIndex(
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000407 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
408 InputSection<ELFT> *Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000409 OutputSection<ELFT> *Out = Section->getOutputSection();
410 return Out->getVA() + Section->getOutputSectionOff() + Sym->st_value;
411}
412
Rafael Espindolaa6627382015-10-06 23:56:53 +0000413bool lld::elf2::canBePreempted(const SymbolBody *Body) {
414 if (!Body)
415 return false;
416 if (Body->isShared() || Body->isUndefined())
417 return true;
418 if (!Config->Shared)
419 return false;
Rafael Espindola52dca342015-10-07 00:58:20 +0000420 return Body->getMostConstrainingVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000421}
422
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000423template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000424 for (InputSection<ELFT> *C : Sections)
Rafael Espindolac2d21192015-09-23 18:25:05 +0000425 C->writeTo(Buf, BssSec, PltSec, GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000426}
427
428template <bool Is64Bits>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000429StringTableSection<Is64Bits>::StringTableSection(bool Dynamic)
430 : OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab",
431 llvm::ELF::SHT_STRTAB,
432 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
433 Dynamic(Dynamic) {
434 this->Header.sh_addralign = 1;
435}
436
437template <bool Is64Bits>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000438void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
439 StringRef Data = StrTabBuilder.data();
440 memcpy(Buf, Data.data(), Data.size());
441}
442
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000443template <class ELFT> bool lld::elf2::includeInSymtab(const SymbolBody &B) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000444 if (!B.isUsedInRegularObj())
445 return false;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000446
447 // Don't include synthetic symbols like __init_array_start in every output.
448 if (auto *U = dyn_cast<DefinedAbsolute<ELFT>>(&B))
449 if (&U->Sym == &DefinedAbsolute<ELFT>::IgnoreUndef)
450 return false;
451
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000452 return true;
453}
454
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000455bool lld::elf2::includeInDynamicSymtab(const SymbolBody &B) {
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000456 uint8_t V = B.getMostConstrainingVisibility();
457 if (V != STV_DEFAULT && V != STV_PROTECTED)
458 return false;
459
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000460 if (Config->ExportDynamic || Config->Shared)
461 return true;
462 return B.isUsedInDynamicReloc();
463}
464
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000465template <class ELFT>
466bool lld::elf2::shouldKeepInSymtab(StringRef SymName,
467 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
468 if (Sym.getType() == STT_SECTION)
469 return false;
470
Davide Italiano6993ba42015-09-26 00:47:56 +0000471 if (Config->DiscardNone)
472 return true;
473
474 // ELF defines dynamic locals as symbols which name starts with ".L".
475 return !(Config->DiscardLocals && SymName.startswith(".L"));
476}
477
Rafael Espindola35c6af32015-09-25 17:19:10 +0000478template <class ELFT>
479SymbolTableSection<ELFT>::SymbolTableSection(
480 SymbolTable &Table, StringTableSection<ELFT::Is64Bits> &StrTabSec,
481 const OutputSection<ELFT> &BssSec)
482 : OutputSectionBase<ELFT::Is64Bits>(
483 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
484 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
485 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
486 Table(Table), StrTabSec(StrTabSec), BssSec(BssSec) {
487 typedef OutputSectionBase<ELFT::Is64Bits> Base;
488 typename Base::HeaderT &Header = this->Header;
489
490 Header.sh_entsize = sizeof(Elf_Sym);
491 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
492}
493
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000494template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000495 Buf += sizeof(Elf_Sym);
496
497 // All symbols with STB_LOCAL binding precede the weak and global symbols.
498 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000499 if (!Config->DiscardAll && !StrTabSec.isDynamic())
500 writeLocalSymbols(Buf);
501
502 writeGlobalSymbols(Buf);
503}
504
505template <class ELFT>
506void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
507 // Iterate over all input object files to copy their local symbols
508 // to the output symbol table pointed by Buf.
509 for (const std::unique_ptr<ObjectFileBase> &FileB : Table.getObjectFiles()) {
510 auto &File = cast<ObjectFile<ELFT>>(*FileB);
511 Elf_Sym_Range Syms = File.getLocalSymbols();
512 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000513 ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000514 if (SymName && !shouldKeepInSymtab<ELFT>(*SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000515 continue;
Rui Ueyamac55733e2015-09-30 00:54:29 +0000516 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
517 Buf += sizeof(*ESym);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000518 ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
519 ESym->st_size = Sym.st_size;
520 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
521 uint32_t SecIndex = Sym.st_shndx;
522 uintX_t VA = Sym.st_value;
523 if (SecIndex == SHN_ABS) {
524 ESym->st_shndx = SHN_ABS;
525 } else {
526 if (SecIndex == SHN_XINDEX)
527 SecIndex = File.getObj().getExtendedSymbolTableIndex(
528 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
529 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
530 const InputSection<ELFT> *Section = Sections[SecIndex];
531 const OutputSection<ELFT> *Out = Section->getOutputSection();
532 ESym->st_shndx = Out->getSectionIndex();
533 VA += Out->getVA() + Section->getOutputSectionOff();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000534 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000535 ESym->st_value = VA;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000536 }
537 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000538}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000539
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000540template <class ELFT>
541void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *&Buf) {
542 // Write the internal symbol table contents to the output symbol table
543 // pointed by Buf.
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000544 uint8_t *Start = Buf;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000545 for (const std::pair<StringRef, Symbol *> &P : Table.getSymbols()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000546 StringRef Name = P.first;
547 Symbol *Sym = P.second;
548 SymbolBody *Body = Sym->Body;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000549 if (!includeInSymtab<ELFT>(*Body))
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000550 continue;
Rafael Espindola05a3dd22015-09-22 23:38:23 +0000551 if (StrTabSec.isDynamic() && !includeInDynamicSymtab(*Body))
552 continue;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000553
554 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac55733e2015-09-30 00:54:29 +0000555 Buf += sizeof(*ESym);
Rafael Espindola8614c562015-10-06 14:33:58 +0000556
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000557 ESym->st_name = StrTabSec.getFileOff(Name);
558
Rafael Espindola25b0acb2015-09-25 17:32:37 +0000559 const OutputSection<ELFT> *Out = nullptr;
560 const InputSection<ELFT> *Section = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000561
Rafael Espindola8614c562015-10-06 14:33:58 +0000562 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +0000563 case SymbolBody::DefinedSyntheticKind:
564 Out = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
565 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000566 case SymbolBody::DefinedRegularKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000567 Section = &cast<DefinedRegular<ELFT>>(Body)->Section;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000568 break;
569 case SymbolBody::DefinedCommonKind:
Rafael Espindolac2d21192015-09-23 18:25:05 +0000570 Out = &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000571 break;
572 case SymbolBody::UndefinedKind:
573 case SymbolBody::DefinedAbsoluteKind:
574 case SymbolBody::SharedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000575 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000576 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000577 }
578
Rafael Espindola8614c562015-10-06 14:33:58 +0000579 unsigned char Binding = Body->isWeak() ? STB_WEAK : STB_GLOBAL;
580 unsigned char Type = STT_NOTYPE;
581 uintX_t Size = 0;
582 if (const auto *EBody = dyn_cast<ELFSymbolBody<ELFT>>(Body)) {
583 const Elf_Sym &InputSym = EBody->Sym;
584 Binding = InputSym.getBinding();
585 Type = InputSym.getType();
586 Size = InputSym.st_size;
587 }
588
589 unsigned char Visibility = Body->getMostConstrainingVisibility();
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000590 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
591 Binding = STB_LOCAL;
592
Rafael Espindola8614c562015-10-06 14:33:58 +0000593 ESym->setBindingAndType(Binding, Type);
594 ESym->st_size = Size;
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000595 ESym->setVisibility(Visibility);
Rafael Espindola8614c562015-10-06 14:33:58 +0000596 ESym->st_value = getSymVA(*Body, BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000597
598 if (Section)
599 Out = Section->getOutputSection();
600
Rafael Espindola8614c562015-10-06 14:33:58 +0000601 if (isa<DefinedAbsolute<ELFT>>(Body))
Rafael Espindola6f4bd532015-10-06 14:17:53 +0000602 ESym->st_shndx = SHN_ABS;
603 else if (Out)
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000604 ESym->st_shndx = Out->getSectionIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000605 }
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000606 if (!StrTabSec.isDynamic())
607 std::stable_sort(
608 reinterpret_cast<Elf_Sym *>(Start), reinterpret_cast<Elf_Sym *>(Buf),
609 [](const Elf_Sym &A, const Elf_Sym &B) -> bool {
610 return A.getBinding() == STB_LOCAL && B.getBinding() != STB_LOCAL;
611 });
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000612}
613
614namespace lld {
615namespace elf2 {
616template class OutputSectionBase<false>;
617template class OutputSectionBase<true>;
618
619template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000620 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000621template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000622 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000623template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000624 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000625template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000626 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000627
628template class GotSection<ELF32LE>;
629template class GotSection<ELF32BE>;
630template class GotSection<ELF64LE>;
631template class GotSection<ELF64BE>;
632
633template class PltSection<ELF32LE>;
634template class PltSection<ELF32BE>;
635template class PltSection<ELF64LE>;
636template class PltSection<ELF64BE>;
637
638template class RelocationSection<ELF32LE>;
639template class RelocationSection<ELF32BE>;
640template class RelocationSection<ELF64LE>;
641template class RelocationSection<ELF64BE>;
642
643template class InterpSection<false>;
644template class InterpSection<true>;
645
646template class HashTableSection<ELF32LE>;
647template class HashTableSection<ELF32BE>;
648template class HashTableSection<ELF64LE>;
649template class HashTableSection<ELF64BE>;
650
651template class DynamicSection<ELF32LE>;
652template class DynamicSection<ELF32BE>;
653template class DynamicSection<ELF64LE>;
654template class DynamicSection<ELF64BE>;
655
656template class OutputSection<ELF32LE>;
657template class OutputSection<ELF32BE>;
658template class OutputSection<ELF64LE>;
659template class OutputSection<ELF64BE>;
660
661template class StringTableSection<false>;
662template class StringTableSection<true>;
663
664template class SymbolTableSection<ELF32LE>;
665template class SymbolTableSection<ELF32BE>;
666template class SymbolTableSection<ELF64LE>;
667template class SymbolTableSection<ELF64BE>;
668
Rafael Espindola8614c562015-10-06 14:33:58 +0000669template ELFFile<ELF32LE>::uintX_t getSymVA(const SymbolBody &,
670 const OutputSection<ELF32LE> &);
671template ELFFile<ELF32BE>::uintX_t getSymVA(const SymbolBody &,
672 const OutputSection<ELF32BE> &);
673template ELFFile<ELF64LE>::uintX_t getSymVA(const SymbolBody &,
674 const OutputSection<ELF64LE> &);
675template ELFFile<ELF64BE>::uintX_t getSymVA(const SymbolBody &,
676 const OutputSection<ELF64BE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000677
Rafael Espindola56f965f2015-09-21 22:48:12 +0000678template ELFFile<ELF32LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000679getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *, const ObjectFile<ELF32LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000680
Rafael Espindola56f965f2015-09-21 22:48:12 +0000681template ELFFile<ELF32BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000682getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *, const ObjectFile<ELF32BE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000683
Rafael Espindola56f965f2015-09-21 22:48:12 +0000684template ELFFile<ELF64LE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000685getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *, const ObjectFile<ELF64LE> &);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000686
Rafael Espindola56f965f2015-09-21 22:48:12 +0000687template ELFFile<ELF64BE>::uintX_t
Rui Ueyamab189b5c2015-09-30 00:43:22 +0000688getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *, const ObjectFile<ELF64BE> &);
Rafael Espindola4f674ed2015-10-05 15:24:04 +0000689
690template bool includeInSymtab<ELF32LE>(const SymbolBody &);
691template bool includeInSymtab<ELF32BE>(const SymbolBody &);
692template bool includeInSymtab<ELF64LE>(const SymbolBody &);
693template bool includeInSymtab<ELF64BE>(const SymbolBody &);
Rafael Espindolad1cf4212015-10-05 16:25:43 +0000694
695template bool shouldKeepInSymtab<ELF32LE>(StringRef,
696 const ELFFile<ELF32LE>::Elf_Sym &);
697template bool shouldKeepInSymtab<ELF32BE>(StringRef,
698 const ELFFile<ELF32BE>::Elf_Sym &);
699template bool shouldKeepInSymtab<ELF64LE>(StringRef,
700 const ELFFile<ELF64LE>::Elf_Sym &);
701template bool shouldKeepInSymtab<ELF64BE>(StringRef,
702 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000703}
704}