blob: 4fc87ded16989909449e683855a2aecaa0707417 [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) {
95 auto *P = reinterpret_cast<Elf_Rela *>(Buf);
96 bool IsMips64EL = Relocs[0].C.getFile()->getObj()->isMips64EL();
97 for (const DynamicReloc<ELFT> &Rel : Relocs) {
98 const InputSection<ELFT> &C = Rel.C;
99 const Elf_Rel &RI = Rel.RI;
100 OutputSection<ELFT> *Out = C.getOutputSection();
101 uint32_t SymIndex = RI.getSymbol(IsMips64EL);
102 const SymbolBody *Body = C.getFile()->getSymbolBody(SymIndex);
103 uint32_t Type = RI.getType(IsMips64EL);
104 if (relocNeedsGOT(Type)) {
105 P->r_offset = GotSec.getEntryAddr(*Body);
106 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), R_X86_64_GLOB_DAT,
107 IsMips64EL);
108 } else {
109 P->r_offset = RI.r_offset + C.getOutputSectionOff() + Out->getVA();
110 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type, IsMips64EL);
111 if (IsRela)
112 P->r_addend = static_cast<const Elf_Rela &>(RI).r_addend;
113 }
114
115 ++P;
116 }
117}
118
119template <class ELFT> void RelocationSection<ELFT>::finalize() {
120 this->Header.sh_link = DynSymSec.getSectionIndex();
121 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
122}
123
124template <bool Is64Bits>
125InterpSection<Is64Bits>::InterpSection()
126 : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS,
127 llvm::ELF::SHF_ALLOC) {
128 this->Header.sh_size = Config->DynamicLinker.size() + 1;
129 this->Header.sh_addralign = 1;
130}
131
132template <bool Is64Bits>
133template <endianness E>
134void OutputSectionBase<Is64Bits>::writeHeaderTo(
135 typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) {
136 SHdr->sh_name = Header.sh_name;
137 SHdr->sh_type = Header.sh_type;
138 SHdr->sh_flags = Header.sh_flags;
139 SHdr->sh_addr = Header.sh_addr;
140 SHdr->sh_offset = Header.sh_offset;
141 SHdr->sh_size = Header.sh_size;
142 SHdr->sh_link = Header.sh_link;
143 SHdr->sh_info = Header.sh_info;
144 SHdr->sh_addralign = Header.sh_addralign;
145 SHdr->sh_entsize = Header.sh_entsize;
146}
147
148template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) {
149 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
150}
151
152template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
153 StringRef Name = S->getName();
154 DynSymSec.addSymbol(Name);
155 Hashes.push_back(hash(Name));
156 S->setDynamicSymbolTableIndex(Hashes.size());
157}
158
159template <class ELFT> void DynamicSection<ELFT>::finalize() {
160 typename Base::HeaderT &Header = this->Header;
161 Header.sh_link = DynStrSec.getSectionIndex();
162
163 unsigned NumEntries = 0;
164 if (RelaDynSec.hasRelocs()) {
165 ++NumEntries; // DT_RELA / DT_REL
166 ++NumEntries; // DT_RELASZ / DTRELSZ
167 }
168 ++NumEntries; // DT_SYMTAB
169 ++NumEntries; // DT_STRTAB
170 ++NumEntries; // DT_STRSZ
171 ++NumEntries; // DT_HASH
172
173 StringRef RPath = Config->RPath;
174 if (!RPath.empty()) {
175 ++NumEntries; // DT_RUNPATH
176 DynStrSec.add(RPath);
177 }
178
179 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
180 SymTab.getSharedFiles();
181 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
182 DynStrSec.add(File->getName());
183 NumEntries += SharedFiles.size();
184
185 ++NumEntries; // DT_NULL
186
187 Header.sh_size = NumEntries * Header.sh_entsize;
188}
189
190template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
191 typedef typename std::conditional<ELFT::Is64Bits, Elf64_Dyn, Elf32_Dyn>::type
192 Elf_Dyn;
193 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
194
195 if (RelaDynSec.hasRelocs()) {
196 bool IsRela = RelaDynSec.isRela();
197 P->d_tag = IsRela ? DT_RELA : DT_REL;
198 P->d_un.d_ptr = RelaDynSec.getVA();
199 ++P;
200
201 P->d_tag = IsRela ? DT_RELASZ : DT_RELSZ;
202 P->d_un.d_val = RelaDynSec.getSize();
203 ++P;
204 }
205
206 P->d_tag = DT_SYMTAB;
207 P->d_un.d_ptr = DynSymSec.getVA();
208 ++P;
209
210 P->d_tag = DT_STRTAB;
211 P->d_un.d_ptr = DynStrSec.getVA();
212 ++P;
213
214 P->d_tag = DT_STRSZ;
215 P->d_un.d_val = DynStrSec.data().size();
216 ++P;
217
218 P->d_tag = DT_HASH;
219 P->d_un.d_ptr = HashSec.getVA();
220 ++P;
221
222 StringRef RPath = Config->RPath;
223 if (!RPath.empty()) {
224 P->d_tag = DT_RUNPATH;
225 P->d_un.d_val = DynStrSec.getFileOff(RPath);
226 ++P;
227 }
228
229 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
230 SymTab.getSharedFiles();
231 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) {
232 P->d_tag = DT_NEEDED;
233 P->d_un.d_val = DynStrSec.getFileOff(File->getName());
234 ++P;
235 }
236
237 P->d_tag = DT_NULL;
238 P->d_un.d_val = 0;
239 ++P;
240}
241
242template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000243void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
244 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245 C->setOutputSection(this);
246 uint32_t Align = C->getAlign();
247 if (Align > this->Header.sh_addralign)
248 this->Header.sh_addralign = Align;
249
250 uintX_t Off = this->Header.sh_size;
251 Off = RoundUpToAlignment(Off, Align);
252 C->setOutputSectionOff(Off);
253 Off += C->getSize();
254 this->Header.sh_size = Off;
255}
256
257template <class ELFT>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000258typename ELFFile<ELFT>::uintX_t
259lld::elf2::getSymVA(const DefinedRegular<ELFT> *DR) {
260 const InputSection<ELFT> *SC = &DR->Section;
261 OutputSection<ELFT> *OS = SC->getOutputSection();
262 return OS->getVA() + SC->getOutputSectionOff() + DR->Sym.st_value;
263}
264
265template <class ELFT>
266typename ELFFile<ELFT>::uintX_t
267lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym,
268 const ObjectFile<ELFT> &File) {
269 uint32_t SecIndex = Sym->st_shndx;
270
271 if (SecIndex == SHN_XINDEX)
272 SecIndex = File.getObj()->getExtendedSymbolTableIndex(
273 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000274 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
275 InputSection<ELFT> *Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000276 OutputSection<ELFT> *Out = Section->getOutputSection();
277 return Out->getVA() + Section->getOutputSectionOff() + Sym->st_value;
278}
279
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000280template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000281 for (InputSection<ELFT> *C : Sections)
Rafael Espindola4ea00212015-09-21 22:01:00 +0000282 C->writeTo(Buf, PltSec, GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000283}
284
285template <bool Is64Bits>
286void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
287 StringRef Data = StrTabBuilder.data();
288 memcpy(Buf, Data.data(), Data.size());
289}
290
291bool lld::elf2::includeInSymtab(const SymbolBody &B) {
292 if (B.isLazy())
293 return false;
294 if (!B.isUsedInRegularObj())
295 return false;
296 uint8_t V = B.getMostConstrainingVisibility();
297 if (V != STV_DEFAULT && V != STV_PROTECTED)
298 return false;
299 return true;
300}
301
302template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
303 const OutputSection<ELFT> *Out = nullptr;
304 const InputSection<ELFT> *Section = nullptr;
305 Buf += sizeof(Elf_Sym);
306
307 // All symbols with STB_LOCAL binding precede the weak and global symbols.
308 // .dynsym only contains global symbols.
309 if (!Config->DiscardAll && !StrTabSec.isDynamic()) {
310 for (const std::unique_ptr<ObjectFileBase> &FileB :
311 Table.getObjectFiles()) {
312 auto &File = cast<ObjectFile<ELFT>>(*FileB);
313 Elf_Sym_Range Syms = File.getLocalSymbols();
314 for (const Elf_Sym &Sym : Syms) {
315 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
316 uint32_t SecIndex = Sym.st_shndx;
317 ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
318 if (Config->DiscardLocals && SymName->startswith(".L"))
319 continue;
320 ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
321 ESym->st_size = Sym.st_size;
322 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
323 if (SecIndex == SHN_XINDEX)
324 SecIndex = File.getObj()->getExtendedSymbolTableIndex(
325 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000326 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
327 Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000328 assert(Section != nullptr);
329 Out = Section->getOutputSection();
330 assert(Out != nullptr);
331 ESym->st_shndx = Out->getSectionIndex();
332 ESym->st_value =
333 Out->getVA() + Section->getOutputSectionOff() + Sym.st_value;
334 Buf += sizeof(Elf_Sym);
335 }
336 }
337 }
338
339 for (auto &P : Table.getSymbols()) {
340 StringRef Name = P.first;
341 Symbol *Sym = P.second;
342 SymbolBody *Body = Sym->Body;
343 if (!includeInSymtab(*Body))
344 continue;
345 const Elf_Sym &InputSym = cast<ELFSymbolBody<ELFT>>(Body)->Sym;
346
347 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
348 ESym->st_name = StrTabSec.getFileOff(Name);
349
350 Out = nullptr;
351 Section = nullptr;
352
353 switch (Body->kind()) {
354 case SymbolBody::DefinedRegularKind:
355 Section = &cast<DefinedRegular<ELFT>>(Body)->Section;
356 break;
357 case SymbolBody::DefinedCommonKind:
358 Out = BssSec;
359 break;
360 case SymbolBody::UndefinedKind:
361 case SymbolBody::DefinedAbsoluteKind:
362 case SymbolBody::SharedKind:
363 break;
364 case SymbolBody::LazyKind:
365 llvm_unreachable("Lazy symbol got to output symbol table!");
366 }
367
368 ESym->setBindingAndType(InputSym.getBinding(), InputSym.getType());
369 ESym->st_size = InputSym.st_size;
370 ESym->setVisibility(Body->getMostConstrainingVisibility());
371 if (InputSym.isAbsolute()) {
372 ESym->st_shndx = SHN_ABS;
373 ESym->st_value = InputSym.st_value;
374 }
375
376 if (Section)
377 Out = Section->getOutputSection();
378
379 if (Out) {
380 ESym->st_shndx = Out->getSectionIndex();
381 uintX_t VA = Out->getVA();
382 if (Section)
383 VA += Section->getOutputSectionOff();
384 if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body))
385 VA += C->OffsetInBSS;
386 else
387 VA += InputSym.st_value;
388 ESym->st_value = VA;
389 }
390
391 Buf += sizeof(Elf_Sym);
392 }
393}
394
395namespace lld {
396namespace elf2 {
397template class OutputSectionBase<false>;
398template class OutputSectionBase<true>;
399
400template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000401 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000402template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000403 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000404template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000405 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000407 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000408
409template class GotSection<ELF32LE>;
410template class GotSection<ELF32BE>;
411template class GotSection<ELF64LE>;
412template class GotSection<ELF64BE>;
413
414template class PltSection<ELF32LE>;
415template class PltSection<ELF32BE>;
416template class PltSection<ELF64LE>;
417template class PltSection<ELF64BE>;
418
419template class RelocationSection<ELF32LE>;
420template class RelocationSection<ELF32BE>;
421template class RelocationSection<ELF64LE>;
422template class RelocationSection<ELF64BE>;
423
424template class InterpSection<false>;
425template class InterpSection<true>;
426
427template class HashTableSection<ELF32LE>;
428template class HashTableSection<ELF32BE>;
429template class HashTableSection<ELF64LE>;
430template class HashTableSection<ELF64BE>;
431
432template class DynamicSection<ELF32LE>;
433template class DynamicSection<ELF32BE>;
434template class DynamicSection<ELF64LE>;
435template class DynamicSection<ELF64BE>;
436
437template class OutputSection<ELF32LE>;
438template class OutputSection<ELF32BE>;
439template class OutputSection<ELF64LE>;
440template class OutputSection<ELF64BE>;
441
442template class StringTableSection<false>;
443template class StringTableSection<true>;
444
445template class SymbolTableSection<ELF32LE>;
446template class SymbolTableSection<ELF32BE>;
447template class SymbolTableSection<ELF64LE>;
448template class SymbolTableSection<ELF64BE>;
449
Rafael Espindola56f965f2015-09-21 22:48:12 +0000450template ELFFile<ELF32LE>::uintX_t getSymVA(const DefinedRegular<ELF32LE> *DR);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000451
Rafael Espindola56f965f2015-09-21 22:48:12 +0000452template ELFFile<ELF32BE>::uintX_t getSymVA(const DefinedRegular<ELF32BE> *DR);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000453
Rafael Espindola56f965f2015-09-21 22:48:12 +0000454template ELFFile<ELF64LE>::uintX_t getSymVA(const DefinedRegular<ELF64LE> *DR);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000455
Rafael Espindola56f965f2015-09-21 22:48:12 +0000456template ELFFile<ELF64BE>::uintX_t getSymVA(const DefinedRegular<ELF64BE> *DR);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000457
Rafael Espindola56f965f2015-09-21 22:48:12 +0000458template ELFFile<ELF32LE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000459getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *Sym,
460 const ObjectFile<ELF32LE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000461
Rafael Espindola56f965f2015-09-21 22:48:12 +0000462template ELFFile<ELF32BE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000463getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *Sym,
464 const ObjectFile<ELF32BE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000465
Rafael Espindola56f965f2015-09-21 22:48:12 +0000466template ELFFile<ELF64LE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000467getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *Sym,
468 const ObjectFile<ELF64LE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000469
Rafael Espindola56f965f2015-09-21 22:48:12 +0000470template ELFFile<ELF64BE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000471getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *Sym,
472 const ObjectFile<ELF64BE> &File);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000473}
474}