blob: 014503b178e44f70fa2ff5d469bc157d5cd66893 [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
31template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
32 Sym->setGotIndex(Entries.size());
33 Entries.push_back(Sym);
34}
35
36template <class ELFT>
37typename GotSection<ELFT>::uintX_t
38GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
39 return this->getVA() + B.getGotIndex() * this->getAddrSize();
40}
41
42template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
43 uintptr_t Start = reinterpret_cast<uintptr_t>(Buf);
44 ArrayRef<uint8_t> Jmp = {0xff, 0x25}; // jmpq *val(%rip)
45 for (const SymbolBody *E : Entries) {
Rafael Espindola01205f72015-09-22 18:19:46 +000046 uint64_t GotEntryAddr = GotSec.getEntryAddr(*E);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000047 uintptr_t InstPos = reinterpret_cast<uintptr_t>(Buf);
Rafael Espindola01205f72015-09-22 18:19:46 +000048 uint64_t PltEntryAddr = (InstPos - Start) + this->getVA();
49 Target->writePltEntry(Buf, GotEntryAddr, PltEntryAddr);
50 Buf += 8;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000051 }
52}
53
54template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
55 Sym->setPltIndex(Entries.size());
56 Entries.push_back(Sym);
57}
58
59template <class ELFT>
60typename PltSection<ELFT>::uintX_t
61PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
62 return this->getVA() + B.getPltIndex() * EntrySize;
63}
64
Rafael Espindola5805c4f2015-09-21 21:38:08 +000065template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +000066 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000067 bool IsMips64EL = Relocs[0].C.getFile()->getObj()->isMips64EL();
68 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +000069 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
70 Buf += EntrySize;
71
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072 const InputSection<ELFT> &C = Rel.C;
73 const Elf_Rel &RI = Rel.RI;
74 OutputSection<ELFT> *Out = C.getOutputSection();
75 uint32_t SymIndex = RI.getSymbol(IsMips64EL);
76 const SymbolBody *Body = C.getFile()->getSymbolBody(SymIndex);
77 uint32_t Type = RI.getType(IsMips64EL);
Rafael Espindola01205f72015-09-22 18:19:46 +000078 if (Target->relocNeedsGot(Type)) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000079 P->r_offset = GotSec.getEntryAddr(*Body);
80 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), R_X86_64_GLOB_DAT,
81 IsMips64EL);
82 } else {
83 P->r_offset = RI.r_offset + C.getOutputSectionOff() + Out->getVA();
84 P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), Type, IsMips64EL);
85 if (IsRela)
Rafael Espindola50534c22015-09-22 17:49:38 +000086 static_cast<Elf_Rela *>(P)->r_addend =
87 static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000088 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000089 }
90}
91
92template <class ELFT> void RelocationSection<ELFT>::finalize() {
93 this->Header.sh_link = DynSymSec.getSectionIndex();
94 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
95}
96
97template <bool Is64Bits>
98InterpSection<Is64Bits>::InterpSection()
99 : OutputSectionBase<Is64Bits>(".interp", llvm::ELF::SHT_PROGBITS,
100 llvm::ELF::SHF_ALLOC) {
101 this->Header.sh_size = Config->DynamicLinker.size() + 1;
102 this->Header.sh_addralign = 1;
103}
104
105template <bool Is64Bits>
106template <endianness E>
107void OutputSectionBase<Is64Bits>::writeHeaderTo(
108 typename ELFFile<ELFType<E, Is64Bits>>::Elf_Shdr *SHdr) {
109 SHdr->sh_name = Header.sh_name;
110 SHdr->sh_type = Header.sh_type;
111 SHdr->sh_flags = Header.sh_flags;
112 SHdr->sh_addr = Header.sh_addr;
113 SHdr->sh_offset = Header.sh_offset;
114 SHdr->sh_size = Header.sh_size;
115 SHdr->sh_link = Header.sh_link;
116 SHdr->sh_info = Header.sh_info;
117 SHdr->sh_addralign = Header.sh_addralign;
118 SHdr->sh_entsize = Header.sh_entsize;
119}
120
121template <bool Is64Bits> void InterpSection<Is64Bits>::writeTo(uint8_t *Buf) {
122 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
123}
124
125template <class ELFT> void HashTableSection<ELFT>::addSymbol(SymbolBody *S) {
126 StringRef Name = S->getName();
127 DynSymSec.addSymbol(Name);
128 Hashes.push_back(hash(Name));
129 S->setDynamicSymbolTableIndex(Hashes.size());
130}
131
132template <class ELFT> void DynamicSection<ELFT>::finalize() {
133 typename Base::HeaderT &Header = this->Header;
134 Header.sh_link = DynStrSec.getSectionIndex();
135
136 unsigned NumEntries = 0;
137 if (RelaDynSec.hasRelocs()) {
138 ++NumEntries; // DT_RELA / DT_REL
139 ++NumEntries; // DT_RELASZ / DTRELSZ
140 }
141 ++NumEntries; // DT_SYMTAB
142 ++NumEntries; // DT_STRTAB
143 ++NumEntries; // DT_STRSZ
144 ++NumEntries; // DT_HASH
145
146 StringRef RPath = Config->RPath;
147 if (!RPath.empty()) {
148 ++NumEntries; // DT_RUNPATH
149 DynStrSec.add(RPath);
150 }
151
152 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
153 SymTab.getSharedFiles();
154 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles)
155 DynStrSec.add(File->getName());
156 NumEntries += SharedFiles.size();
157
158 ++NumEntries; // DT_NULL
159
160 Header.sh_size = NumEntries * Header.sh_entsize;
161}
162
163template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
164 typedef typename std::conditional<ELFT::Is64Bits, Elf64_Dyn, Elf32_Dyn>::type
165 Elf_Dyn;
166 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
167
168 if (RelaDynSec.hasRelocs()) {
169 bool IsRela = RelaDynSec.isRela();
170 P->d_tag = IsRela ? DT_RELA : DT_REL;
171 P->d_un.d_ptr = RelaDynSec.getVA();
172 ++P;
173
174 P->d_tag = IsRela ? DT_RELASZ : DT_RELSZ;
175 P->d_un.d_val = RelaDynSec.getSize();
176 ++P;
177 }
178
179 P->d_tag = DT_SYMTAB;
180 P->d_un.d_ptr = DynSymSec.getVA();
181 ++P;
182
183 P->d_tag = DT_STRTAB;
184 P->d_un.d_ptr = DynStrSec.getVA();
185 ++P;
186
187 P->d_tag = DT_STRSZ;
188 P->d_un.d_val = DynStrSec.data().size();
189 ++P;
190
191 P->d_tag = DT_HASH;
192 P->d_un.d_ptr = HashSec.getVA();
193 ++P;
194
195 StringRef RPath = Config->RPath;
196 if (!RPath.empty()) {
197 P->d_tag = DT_RUNPATH;
198 P->d_un.d_val = DynStrSec.getFileOff(RPath);
199 ++P;
200 }
201
202 const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
203 SymTab.getSharedFiles();
204 for (const std::unique_ptr<SharedFileBase> &File : SharedFiles) {
205 P->d_tag = DT_NEEDED;
206 P->d_un.d_val = DynStrSec.getFileOff(File->getName());
207 ++P;
208 }
209
210 P->d_tag = DT_NULL;
211 P->d_un.d_val = 0;
212 ++P;
213}
214
215template <class ELFT>
Rafael Espindola71675852015-09-22 00:16:19 +0000216void OutputSection<ELFT>::addSection(InputSection<ELFT> *C) {
217 Sections.push_back(C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218 C->setOutputSection(this);
219 uint32_t Align = C->getAlign();
220 if (Align > this->Header.sh_addralign)
221 this->Header.sh_addralign = Align;
222
223 uintX_t Off = this->Header.sh_size;
224 Off = RoundUpToAlignment(Off, Align);
225 C->setOutputSectionOff(Off);
226 Off += C->getSize();
227 this->Header.sh_size = Off;
228}
229
230template <class ELFT>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000231typename ELFFile<ELFT>::uintX_t
232lld::elf2::getSymVA(const DefinedRegular<ELFT> *DR) {
233 const InputSection<ELFT> *SC = &DR->Section;
234 OutputSection<ELFT> *OS = SC->getOutputSection();
235 return OS->getVA() + SC->getOutputSectionOff() + DR->Sym.st_value;
236}
237
238template <class ELFT>
239typename ELFFile<ELFT>::uintX_t
240lld::elf2::getLocalSymVA(const typename ELFFile<ELFT>::Elf_Sym *Sym,
241 const ObjectFile<ELFT> &File) {
242 uint32_t SecIndex = Sym->st_shndx;
243
244 if (SecIndex == SHN_XINDEX)
245 SecIndex = File.getObj()->getExtendedSymbolTableIndex(
246 Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000247 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
248 InputSection<ELFT> *Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249 OutputSection<ELFT> *Out = Section->getOutputSection();
250 return Out->getVA() + Section->getOutputSectionOff() + Sym->st_value;
251}
252
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000254 for (InputSection<ELFT> *C : Sections)
Rafael Espindola4ea00212015-09-21 22:01:00 +0000255 C->writeTo(Buf, PltSec, GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256}
257
258template <bool Is64Bits>
259void StringTableSection<Is64Bits>::writeTo(uint8_t *Buf) {
260 StringRef Data = StrTabBuilder.data();
261 memcpy(Buf, Data.data(), Data.size());
262}
263
264bool lld::elf2::includeInSymtab(const SymbolBody &B) {
265 if (B.isLazy())
266 return false;
267 if (!B.isUsedInRegularObj())
268 return false;
269 uint8_t V = B.getMostConstrainingVisibility();
270 if (V != STV_DEFAULT && V != STV_PROTECTED)
271 return false;
272 return true;
273}
274
275template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
276 const OutputSection<ELFT> *Out = nullptr;
277 const InputSection<ELFT> *Section = nullptr;
278 Buf += sizeof(Elf_Sym);
279
280 // All symbols with STB_LOCAL binding precede the weak and global symbols.
281 // .dynsym only contains global symbols.
282 if (!Config->DiscardAll && !StrTabSec.isDynamic()) {
283 for (const std::unique_ptr<ObjectFileBase> &FileB :
284 Table.getObjectFiles()) {
285 auto &File = cast<ObjectFile<ELFT>>(*FileB);
286 Elf_Sym_Range Syms = File.getLocalSymbols();
287 for (const Elf_Sym &Sym : Syms) {
288 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
289 uint32_t SecIndex = Sym.st_shndx;
290 ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
291 if (Config->DiscardLocals && SymName->startswith(".L"))
292 continue;
293 ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
294 ESym->st_size = Sym.st_size;
295 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
296 if (SecIndex == SHN_XINDEX)
297 SecIndex = File.getObj()->getExtendedSymbolTableIndex(
298 &Sym, File.getSymbolTable(), File.getSymbolTableShndx());
Rafael Espindola71675852015-09-22 00:16:19 +0000299 ArrayRef<InputSection<ELFT> *> Sections = File.getSections();
300 Section = Sections[SecIndex];
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000301 assert(Section != nullptr);
302 Out = Section->getOutputSection();
303 assert(Out != nullptr);
304 ESym->st_shndx = Out->getSectionIndex();
305 ESym->st_value =
306 Out->getVA() + Section->getOutputSectionOff() + Sym.st_value;
307 Buf += sizeof(Elf_Sym);
308 }
309 }
310 }
311
312 for (auto &P : Table.getSymbols()) {
313 StringRef Name = P.first;
314 Symbol *Sym = P.second;
315 SymbolBody *Body = Sym->Body;
316 if (!includeInSymtab(*Body))
317 continue;
318 const Elf_Sym &InputSym = cast<ELFSymbolBody<ELFT>>(Body)->Sym;
319
320 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
321 ESym->st_name = StrTabSec.getFileOff(Name);
322
323 Out = nullptr;
324 Section = nullptr;
325
326 switch (Body->kind()) {
327 case SymbolBody::DefinedRegularKind:
328 Section = &cast<DefinedRegular<ELFT>>(Body)->Section;
329 break;
330 case SymbolBody::DefinedCommonKind:
331 Out = BssSec;
332 break;
333 case SymbolBody::UndefinedKind:
334 case SymbolBody::DefinedAbsoluteKind:
335 case SymbolBody::SharedKind:
336 break;
337 case SymbolBody::LazyKind:
338 llvm_unreachable("Lazy symbol got to output symbol table!");
339 }
340
341 ESym->setBindingAndType(InputSym.getBinding(), InputSym.getType());
342 ESym->st_size = InputSym.st_size;
343 ESym->setVisibility(Body->getMostConstrainingVisibility());
344 if (InputSym.isAbsolute()) {
345 ESym->st_shndx = SHN_ABS;
346 ESym->st_value = InputSym.st_value;
347 }
348
349 if (Section)
350 Out = Section->getOutputSection();
351
352 if (Out) {
353 ESym->st_shndx = Out->getSectionIndex();
354 uintX_t VA = Out->getVA();
355 if (Section)
356 VA += Section->getOutputSectionOff();
357 if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body))
358 VA += C->OffsetInBSS;
359 else
360 VA += InputSym.st_value;
361 ESym->st_value = VA;
362 }
363
364 Buf += sizeof(Elf_Sym);
365 }
366}
367
368namespace lld {
369namespace elf2 {
370template class OutputSectionBase<false>;
371template class OutputSectionBase<true>;
372
373template void OutputSectionBase<false>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000374 ELFFile<ELFType<support::little, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000375template void OutputSectionBase<true>::writeHeaderTo<support::little>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000376 ELFFile<ELFType<support::little, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000377template void OutputSectionBase<false>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000378 ELFFile<ELFType<support::big, false>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000379template void OutputSectionBase<true>::writeHeaderTo<support::big>(
Rafael Espindolaf68b7072015-09-21 22:21:46 +0000380 ELFFile<ELFType<support::big, true>>::Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000381
382template class GotSection<ELF32LE>;
383template class GotSection<ELF32BE>;
384template class GotSection<ELF64LE>;
385template class GotSection<ELF64BE>;
386
387template class PltSection<ELF32LE>;
388template class PltSection<ELF32BE>;
389template class PltSection<ELF64LE>;
390template class PltSection<ELF64BE>;
391
392template class RelocationSection<ELF32LE>;
393template class RelocationSection<ELF32BE>;
394template class RelocationSection<ELF64LE>;
395template class RelocationSection<ELF64BE>;
396
397template class InterpSection<false>;
398template class InterpSection<true>;
399
400template class HashTableSection<ELF32LE>;
401template class HashTableSection<ELF32BE>;
402template class HashTableSection<ELF64LE>;
403template class HashTableSection<ELF64BE>;
404
405template class DynamicSection<ELF32LE>;
406template class DynamicSection<ELF32BE>;
407template class DynamicSection<ELF64LE>;
408template class DynamicSection<ELF64BE>;
409
410template class OutputSection<ELF32LE>;
411template class OutputSection<ELF32BE>;
412template class OutputSection<ELF64LE>;
413template class OutputSection<ELF64BE>;
414
415template class StringTableSection<false>;
416template class StringTableSection<true>;
417
418template class SymbolTableSection<ELF32LE>;
419template class SymbolTableSection<ELF32BE>;
420template class SymbolTableSection<ELF64LE>;
421template class SymbolTableSection<ELF64BE>;
422
Rafael Espindola56f965f2015-09-21 22:48:12 +0000423template ELFFile<ELF32LE>::uintX_t getSymVA(const DefinedRegular<ELF32LE> *DR);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000424
Rafael Espindola56f965f2015-09-21 22:48:12 +0000425template ELFFile<ELF32BE>::uintX_t getSymVA(const DefinedRegular<ELF32BE> *DR);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000426
Rafael Espindola56f965f2015-09-21 22:48:12 +0000427template ELFFile<ELF64LE>::uintX_t getSymVA(const DefinedRegular<ELF64LE> *DR);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000428
Rafael Espindola56f965f2015-09-21 22:48:12 +0000429template ELFFile<ELF64BE>::uintX_t getSymVA(const DefinedRegular<ELF64BE> *DR);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000430
Rafael Espindola56f965f2015-09-21 22:48:12 +0000431template ELFFile<ELF32LE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000432getLocalSymVA(const ELFFile<ELF32LE>::Elf_Sym *Sym,
433 const ObjectFile<ELF32LE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000434
Rafael Espindola56f965f2015-09-21 22:48:12 +0000435template ELFFile<ELF32BE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000436getLocalSymVA(const ELFFile<ELF32BE>::Elf_Sym *Sym,
437 const ObjectFile<ELF32BE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000438
Rafael Espindola56f965f2015-09-21 22:48:12 +0000439template ELFFile<ELF64LE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000440getLocalSymVA(const ELFFile<ELF64LE>::Elf_Sym *Sym,
441 const ObjectFile<ELF64LE> &File);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000442
Rafael Espindola56f965f2015-09-21 22:48:12 +0000443template ELFFile<ELF64BE>::uintX_t
Denis Protivensky67d01482015-09-22 08:14:46 +0000444getLocalSymVA(const ELFFile<ELF64BE>::Elf_Sym *Sym,
445 const ObjectFile<ELF64BE> &File);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000446}
447}