blob: 4640d987d6608654b06683a500480f5cb92cff2a [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"
13
Rafael Espindola5805c4f2015-09-21 21:38:08 +000014using namespace llvm;
15using namespace llvm::object;
16using namespace llvm::ELF;
17
18using namespace lld;
19using namespace lld::elf2;
20
21template <bool Is64Bits>
22OutputSectionBase<Is64Bits>::OutputSectionBase(StringRef Name, uint32_t sh_type,
23 uintX_t sh_flags)
24 : Name(Name) {
25 memset(&Header, 0, sizeof(HeaderT));
26 Header.sh_type = sh_type;
27 Header.sh_flags = sh_flags;
28}
29
30template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
31 Sym->setGotIndex(Entries.size());
32 Entries.push_back(Sym);
33}
34
35template <class ELFT>
36typename GotSection<ELFT>::uintX_t
37GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
38 return this->getVA() + B.getGotIndex() * this->getAddrSize();
39}
40
41template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
42 uintptr_t Start = reinterpret_cast<uintptr_t>(Buf);
43 ArrayRef<uint8_t> Jmp = {0xff, 0x25}; // jmpq *val(%rip)
44 for (const SymbolBody *E : Entries) {
45 uintptr_t InstPos = reinterpret_cast<uintptr_t>(Buf);
46
47 memcpy(Buf, Jmp.data(), Jmp.size());
48 Buf += Jmp.size();
49
50 uintptr_t OffsetInPLT = (InstPos + 6) - Start;
Rafael Espindola454ca1c2015-09-22 17:08:25 +000051 intptr_t Delta = GotSec.getEntryAddr(*E) - (this->getVA() + OffsetInPLT);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000052 assert(isInt<32>(Delta));
53 support::endian::write32le(Buf, Delta);
54 Buf += 4;
55
56 *Buf = 0x90; // nop
57 ++Buf;
58 *Buf = 0x90; // nop
59 ++Buf;
60 }
61}
62
63template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
64 Sym->setPltIndex(Entries.size());
65 Entries.push_back(Sym);
66}
67
68template <class ELFT>
69typename PltSection<ELFT>::uintX_t
70PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
71 return this->getVA() + B.getPltIndex() * EntrySize;
72}
73
74bool lld::elf2::relocNeedsPLT(uint32_t Type) {
75 switch (Type) {
76 default:
77 return false;
78 case R_X86_64_PLT32:
79 return true;
80 }
81}
82
83bool lld::elf2::relocNeedsGOT(uint32_t Type) {
84 if (relocNeedsPLT(Type))
85 return true;
86 switch (Type) {
87 default:
88 return false;
89 case R_X86_64_GOTPCREL:
90 return true;
91 }
92}
93
94template <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 Espindola5805c4f2015-09-21 21:38:08 +000096 bool IsMips64EL = Relocs[0].C.getFile()->getObj()->isMips64EL();
97 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);
107 if (relocNeedsGOT(Type)) {
108 P->r_offset = GotSec.getEntryAddr(*Body);
109 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), R_X86_64_GLOB_DAT,
110 IsMips64EL);
111 } 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
154template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
155 StringRef Name = S->getName();
156 DynSymSec.addSymbol(Name);
157 Hashes.push_back(hash(Name));
158 S->setDynamicSymbolTableIndex(Hashes.size());
159}
160
161template <class ELFT> void DynamicSection<ELFT>::finalize() {
162 typename Base::HeaderT &Header = this->Header;
163 Header.sh_link = DynStrSec.getSectionIndex();
164
165 unsigned NumEntries = 0;
166 if (RelaDynSec.hasRelocs()) {
167 ++NumEntries; // DT_RELA / DT_REL
168 ++NumEntries; // DT_RELASZ / DTRELSZ
169 }
170 ++NumEntries; // DT_SYMTAB
171 ++NumEntries; // DT_STRTAB
172 ++NumEntries; // DT_STRSZ
173 ++NumEntries; // DT_HASH
174
175 StringRef RPath = Config->RPath;
176 if (!RPath.empty()) {
177 ++NumEntries; // DT_RUNPATH
178 DynStrSec.add(RPath);
179 }
180
181 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
182 SymTab.getSharedFiles();
183 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
184 DynStrSec.add(File->getName());
185 NumEntries += SharedFiles.size();
186
187 ++NumEntries; // DT_NULL
188
189 Header.sh_size = NumEntries * Header.sh_entsize;
190}
191
192template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
193 typedef typename std::conditional<ELFT::Is64Bits, Elf64_Dyn, Elf32_Dyn>::type
194 Elf_Dyn;
195 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
196
197 if (RelaDynSec.hasRelocs()) {
198 bool IsRela = RelaDynSec.isRela();
199 P->d_tag = IsRela ? DT_RELA : DT_REL;
200 P->d_un.d_ptr = RelaDynSec.getVA();
201 ++P;
202
203 P->d_tag = IsRela ? DT_RELASZ : DT_RELSZ;
204 P->d_un.d_val = RelaDynSec.getSize();
205 ++P;
206 }
207
208 P->d_tag = DT_SYMTAB;
209 P->d_un.d_ptr = DynSymSec.getVA();
210 ++P;
211
212 P->d_tag = DT_STRTAB;
213 P->d_un.d_ptr = DynStrSec.getVA();
214 ++P;
215
216 P->d_tag = DT_STRSZ;
217 P->d_un.d_val = DynStrSec.data().size();
218 ++P;
219
220 P->d_tag = DT_HASH;
221 P->d_un.d_ptr = HashSec.getVA();
222 ++P;
223
224 StringRef RPath = Config->RPath;
225 if (!RPath.empty()) {
226 P->d_tag = DT_RUNPATH;
227 P->d_un.d_val = DynStrSec.getFileOff(RPath);
228 ++P;
229 }
230
231 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
232 SymTab.getSharedFiles();
233 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) {
234 P->d_tag = DT_NEEDED;
235 P->d_un.d_val = DynStrSec.getFileOff(File->getName());
236 ++P;
237 }
238
239 P->d_tag = DT_NULL;
240 P->d_un.d_val = 0;
241 ++P;
242}
243
244template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000245void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
246 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247 C->setOutputSection(this);
248 uint32_t Align = C->getAlign();
249 if (Align > this->Header.sh_addralign)
250 this->Header.sh_addralign = Align;
251
252 uintX_t Off = this->Header.sh_size;
253 Off = RoundUpToAlignment(Off, Align);
254 C->setOutputSectionOff(Off);
255 Off += C->getSize();
256 this->Header.sh_size = Off;
257}
258
259template <class ELFT>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000260typename ELFFile<ELFT>::uintX_t
261lld::elf2::getSymVA(const DefinedRegular<ELFT> *DR) {
262 const InputSection<ELFT> *SC = &DR->Section;
263 OutputSection<ELFT> *OS = SC->getOutputSection();
264 return OS->getVA() + SC->getOutputSectionOff() + DR->Sym.st_value;
265}
266
267template <class ELFT>
268typename ELFFile<ELFT>::uintX_t
269lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym,
270 const ObjectFile<ELFT> &File) {
271 uint32_t SecIndex = Sym->st_shndx;
272
273 if (SecIndex == SHN_XINDEX)
274 SecIndex = File.getObj()->getExtendedSymbolTableIndex(
275 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000276 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
277 InputSection<ELFT> *Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000278 OutputSection<ELFT> *Out = Section->getOutputSection();
279 return Out->getVA() + Section->getOutputSectionOff() + Sym->st_value;
280}
281
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000282template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000283 for (InputSection<ELFT> *C : Sections)
Rafael Espindola4ea00212015-09-21 22:01:00 +0000284 C->writeTo(Buf, PltSec, GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000285}
286
287template <bool Is64Bits>
288void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
289 StringRef Data = StrTabBuilder.data();
290 memcpy(Buf, Data.data(), Data.size());
291}
292
293bool lld::elf2::includeInSymtab(const SymbolBody &B) {
294 if (B.isLazy())
295 return false;
296 if (!B.isUsedInRegularObj())
297 return false;
298 uint8_t V = B.getMostConstrainingVisibility();
299 if (V != STV_DEFAULT && V != STV_PROTECTED)
300 return false;
301 return true;
302}
303
304template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
305 const OutputSection<ELFT> *Out = nullptr;
306 const InputSection<ELFT> *Section = nullptr;
307 Buf += sizeof(Elf_Sym);
308
309 // All symbols with STB_LOCAL binding precede the weak and global symbols.
310 // .dynsym only contains global symbols.
311 if (!Config->DiscardAll && !StrTabSec.isDynamic()) {
312 for (const std::unique_ptr<ObjectFileBase> &FileB :
313 Table.getObjectFiles()) {
314 auto &File = cast<ObjectFile<ELFT>>(*FileB);
315 Elf_Sym_Range Syms = File.getLocalSymbols();
316 for (const Elf_Sym &Sym : Syms) {
317 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
318 uint32_t SecIndex = Sym.st_shndx;
319 ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
320 if (Config->DiscardLocals && SymName->startswith(".L"))
321 continue;
322 ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
323 ESym->st_size = Sym.st_size;
324 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
325 if (SecIndex == SHN_XINDEX)
326 SecIndex = File.getObj()->getExtendedSymbolTableIndex(
327 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000328 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
329 Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000330 assert(Section != nullptr);
331 Out = Section->getOutputSection();
332 assert(Out != nullptr);
333 ESym->st_shndx = Out->getSectionIndex();
334 ESym->st_value =
335 Out->getVA() + Section->getOutputSectionOff() + Sym.st_value;
336 Buf += sizeof(Elf_Sym);
337 }
338 }
339 }
340
341 for (auto &P : Table.getSymbols()) {
342 StringRef Name = P.first;
343 Symbol *Sym = P.second;
344 SymbolBody *Body = Sym->Body;
345 if (!includeInSymtab(*Body))
346 continue;
347 const Elf_Sym &InputSym = cast<ELFSymbolBody<ELFT>>(Body)->Sym;
348
349 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
350 ESym->st_name = StrTabSec.getFileOff(Name);
351
352 Out = nullptr;
353 Section = nullptr;
354
355 switch (Body->kind()) {
356 case SymbolBody::DefinedRegularKind:
357 Section = &cast<DefinedRegular<ELFT>>(Body)->Section;
358 break;
359 case SymbolBody::DefinedCommonKind:
360 Out = BssSec;
361 break;
362 case SymbolBody::UndefinedKind:
363 case SymbolBody::DefinedAbsoluteKind:
364 case SymbolBody::SharedKind:
365 break;
366 case SymbolBody::LazyKind:
367 llvm_unreachable("Lazy symbol got to output symbol table!");
368 }
369
370 ESym->setBindingAndType(InputSym.getBinding(), InputSym.getType());
371 ESym->st_size = InputSym.st_size;
372 ESym->setVisibility(Body->getMostConstrainingVisibility());
373 if (InputSym.isAbsolute()) {
374 ESym->st_shndx = SHN_ABS;
375 ESym->st_value = InputSym.st_value;
376 }
377
378 if (Section)
379 Out = Section->getOutputSection();
380
381 if (Out) {
382 ESym->st_shndx = Out->getSectionIndex();
383 uintX_t VA = Out->getVA();
384 if (Section)
385 VA += Section->getOutputSectionOff();
386 if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body))
387 VA += C->OffsetInBSS;
388 else
389 VA += InputSym.st_value;
390 ESym->st_value = VA;
391 }
392
393 Buf += sizeof(Elf_Sym);
394 }
395}
396
397namespace lld {
398namespace elf2 {
399template class OutputSectionBase<false>;
400template class OutputSectionBase<true>;
401
402template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000403 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000404template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000405 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000407 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000408template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000409 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000410
411template class GotSection<ELF32LE>;
412template class GotSection<ELF32BE>;
413template class GotSection<ELF64LE>;
414template class GotSection<ELF64BE>;
415
416template class PltSection<ELF32LE>;
417template class PltSection<ELF32BE>;
418template class PltSection<ELF64LE>;
419template class PltSection<ELF64BE>;
420
421template class RelocationSection<ELF32LE>;
422template class RelocationSection<ELF32BE>;
423template class RelocationSection<ELF64LE>;
424template class RelocationSection<ELF64BE>;
425
426template class InterpSection<false>;
427template class InterpSection<true>;
428
429template class HashTableSection<ELF32LE>;
430template class HashTableSection<ELF32BE>;
431template class HashTableSection<ELF64LE>;
432template class HashTableSection<ELF64BE>;
433
434template class DynamicSection<ELF32LE>;
435template class DynamicSection<ELF32BE>;
436template class DynamicSection<ELF64LE>;
437template class DynamicSection<ELF64BE>;
438
439template class OutputSection<ELF32LE>;
440template class OutputSection<ELF32BE>;
441template class OutputSection<ELF64LE>;
442template class OutputSection<ELF64BE>;
443
444template class StringTableSection<false>;
445template class StringTableSection<true>;
446
447template class SymbolTableSection<ELF32LE>;
448template class SymbolTableSection<ELF32BE>;
449template class SymbolTableSection<ELF64LE>;
450template class SymbolTableSection<ELF64BE>;
451
Rafael Espindola56f965f2015-09-21 22:48:12 +0000452template ELFFile<ELF32LE>::uintX_t getSymVA(const DefinedRegular<ELF32LE> *DR);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000453
Rafael Espindola56f965f2015-09-21 22:48:12 +0000454template ELFFile<ELF32BE>::uintX_t getSymVA(const DefinedRegular<ELF32BE> *DR);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000455
Rafael Espindola56f965f2015-09-21 22:48:12 +0000456template ELFFile<ELF64LE>::uintX_t getSymVA(const DefinedRegular<ELF64LE> *DR);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000457
Rafael Espindola56f965f2015-09-21 22:48:12 +0000458template ELFFile<ELF64BE>::uintX_t getSymVA(const DefinedRegular<ELF64BE> *DR);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000459
Rafael Espindola56f965f2015-09-21 22:48:12 +0000460template ELFFile<ELF32LE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000461getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *Sym,
462 const ObjectFile<ELF32LE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000463
Rafael Espindola56f965f2015-09-21 22:48:12 +0000464template ELFFile<ELF32BE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000465getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *Sym,
466 const ObjectFile<ELF32BE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000467
Rafael Espindola56f965f2015-09-21 22:48:12 +0000468template ELFFile<ELF64LE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000469getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *Sym,
470 const ObjectFile<ELF64LE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000471
Rafael Espindola56f965f2015-09-21 22:48:12 +0000472template ELFFile<ELF64BE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000473getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *Sym,
474 const ObjectFile<ELF64BE> &File);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000475}
476}