1 //===- InputSection.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 "InputSection.h"
11 #include "Config.h"
12 #include "Error.h"
13 #include "InputFiles.h"
14 #include "OutputSections.h"
15 #include "Target.h"
16 
17 #include "llvm/Support/Endian.h"
18 
19 using namespace llvm;
20 using namespace llvm::ELF;
21 using namespace llvm::object;
22 using namespace llvm::support::endian;
23 
24 using namespace lld;
25 using namespace lld::elf;
26 
27 template <class ELFT>
28 InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
29                                          const Elf_Shdr *Header,
30                                          Kind SectionKind)
31     : Header(Header), File(File), SectionKind(SectionKind), Repl(this) {
32   // The garbage collector sets sections' Live bits.
33   // If GC is disabled, all sections are considered live by default.
34   Live = !Config->GcSections;
35 
36   // The ELF spec states that a value of 0 means the section has
37   // no alignment constraits.
38   Align = std::max<uintX_t>(Header->sh_addralign, 1);
39 }
40 
41 template <class ELFT> size_t InputSectionBase<ELFT>::getSize() const {
42   if (auto *D = dyn_cast<InputSection<ELFT>>(this))
43     if (D->getThunksSize() > 0)
44       return D->getThunkOff() + D->getThunksSize();
45   return Header->sh_size;
46 }
47 
48 template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
49   return check(File->getObj().getSectionName(this->Header));
50 }
51 
52 template <class ELFT>
53 ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
54   return check(this->File->getObj().getSectionContents(this->Header));
55 }
56 
57 template <class ELFT>
58 typename ELFT::uint InputSectionBase<ELFT>::getOffset(uintX_t Offset) {
59   switch (SectionKind) {
60   case Regular:
61     return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
62   case EHFrame:
63     return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
64   case Merge:
65     return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
66   case MipsReginfo:
67     // MIPS .reginfo sections are consumed by the linker,
68     // so it should never be copied to output.
69     llvm_unreachable("MIPS .reginfo reached writeTo().");
70   }
71   llvm_unreachable("invalid section kind");
72 }
73 
74 template <class ELFT>
75 typename ELFT::uint
76 InputSectionBase<ELFT>::getOffset(const DefinedRegular<ELFT> &Sym) {
77   return getOffset(Sym.Value);
78 }
79 
80 template <class ELFT>
81 InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F,
82                                  const Elf_Shdr *Header)
83     : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
84 
85 template <class ELFT>
86 bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
87   return S->SectionKind == Base::Regular;
88 }
89 
90 template <class ELFT>
91 InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() {
92   assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL);
93   ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections();
94   return Sections[this->Header->sh_info];
95 }
96 
97 template <class ELFT> void InputSection<ELFT>::addThunk(SymbolBody &Body) {
98   Body.ThunkIndex = Thunks.size();
99   Thunks.push_back(&Body);
100 }
101 
102 template <class ELFT> uint64_t InputSection<ELFT>::getThunkOff() const {
103   return this->Header->sh_size;
104 }
105 
106 template <class ELFT> uint64_t InputSection<ELFT>::getThunksSize() const {
107   return Thunks.size() * Target->ThunkSize;
108 }
109 
110 // This is used for -r. We can't use memcpy to copy relocations because we need
111 // to update symbol table offset and section index for each relocation. So we
112 // copy relocations one by one.
113 template <class ELFT>
114 template <class RelTy>
115 void InputSection<ELFT>::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
116   InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
117 
118   for (const RelTy &Rel : Rels) {
119     uint32_t Type = Rel.getType(Config->Mips64EL);
120     SymbolBody &Body = this->File->getRelocTargetSym(Rel);
121 
122     RelTy *P = reinterpret_cast<RelTy *>(Buf);
123     Buf += sizeof(RelTy);
124 
125     P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
126     P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL);
127   }
128 }
129 
130 // Page(Expr) is the page address of the expression Expr, defined
131 // as (Expr & ~0xFFF). (This applies even if the machine page size
132 // supported by the platform has a different value.)
133 static uint64_t getAArch64Page(uint64_t Expr) {
134   return Expr & (~static_cast<uint64_t>(0xFFF));
135 }
136 
137 template <class ELFT>
138 static typename ELFT::uint
139 getSymVA(uint32_t Type, typename ELFT::uint A, typename ELFT::uint P,
140          const SymbolBody &Body, uint8_t *BufLoc,
141          const elf::ObjectFile<ELFT> &File, RelExpr Expr) {
142   typedef typename ELFT::uint uintX_t;
143   switch (Expr) {
144   case R_TLSLD:
145     return Out<ELFT>::Got->getTlsIndexOff() + A -
146            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
147   case R_TLSLD_PC:
148     return Out<ELFT>::Got->getTlsIndexVA() + A - P;
149   case R_THUNK:
150     return Body.getThunkVA<ELFT>();
151   case R_PPC_TOC:
152     return getPPC64TocBase() + A;
153   case R_TLSGD:
154     return Out<ELFT>::Got->getGlobalDynOffset(Body) + A -
155            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
156   case R_TLSGD_PC:
157     return Out<ELFT>::Got->getGlobalDynAddr(Body) + A - P;
158   case R_PLT:
159     return Body.getPltVA<ELFT>() + A;
160   case R_PLT_PC:
161   case R_PPC_PLT_OPD:
162     return Body.getPltVA<ELFT>() + A - P;
163   case R_SIZE:
164     return Body.getSize<ELFT>() + A;
165   case R_GOTREL:
166     return Body.getVA<ELFT>(A) - Out<ELFT>::Got->getVA();
167   case R_GOT_FROM_END:
168     return Body.getGotOffset<ELFT>() + A -
169            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
170   case R_GOT:
171   case R_RELAX_TLS_GD_TO_IE:
172     return Body.getGotVA<ELFT>() + A;
173   case R_GOT_PAGE_PC:
174     return getAArch64Page(Body.getGotVA<ELFT>() + A) - getAArch64Page(P);
175   case R_GOT_PC:
176   case R_RELAX_TLS_GD_TO_IE_PC:
177     return Body.getGotVA<ELFT>() + A - P;
178   case R_GOTONLY_PC:
179     return Out<ELFT>::Got->getVA() + A - P;
180   case R_TLS:
181     return Body.getVA<ELFT>(A) - Out<ELFT>::TlsPhdr->p_memsz;
182   case R_NEG_TLS:
183     return Out<ELF32LE>::TlsPhdr->p_memsz - Body.getVA<ELFT>(A);
184   case R_ABS:
185   case R_RELAX_TLS_GD_TO_LE:
186   case R_RELAX_TLS_IE_TO_LE:
187   case R_RELAX_TLS_LD_TO_LE:
188     return Body.getVA<ELFT>(A);
189   case R_GOT_OFF:
190     return Body.getGotOffset<ELFT>() + A;
191   case R_MIPS_GOT_LOCAL:
192     // If relocation against MIPS local symbol requires GOT entry, this entry
193     // should be initialized by 'page address'. This address is high 16-bits
194     // of sum the symbol's value and the addend.
195     return Out<ELFT>::Got->getMipsLocalPageOffset(Body.getVA<ELFT>(A));
196   case R_MIPS_GOT:
197     // For non-local symbols GOT entries should contain their full
198     // addresses. But if such symbol cannot be preempted, we do not
199     // have to put them into the "global" part of GOT and use dynamic
200     // linker to determine their actual addresses. That is why we
201     // create GOT entries for them in the "local" part of GOT.
202     return Out<ELFT>::Got->getMipsLocalEntryOffset(Body.getVA<ELFT>(A));
203   case R_PPC_OPD: {
204     uint64_t SymVA = Body.getVA<ELFT>(A);
205     // If we have an undefined weak symbol, we might get here with a symbol
206     // address of zero. That could overflow, but the code must be unreachable,
207     // so don't bother doing anything at all.
208     if (!SymVA)
209       return 0;
210     if (Out<ELF64BE>::Opd) {
211       // If this is a local call, and we currently have the address of a
212       // function-descriptor, get the underlying code address instead.
213       uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
214       uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
215       bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd;
216       if (InOpd)
217         SymVA = read64be(&Out<ELF64BE>::OpdBuf[SymVA - OpdStart]);
218     }
219     return SymVA - P;
220   }
221   case R_PC:
222     return Body.getVA<ELFT>(A) - P;
223   case R_PAGE_PC:
224     return getAArch64Page(Body.getVA<ELFT>(A)) - getAArch64Page(P);
225   }
226   llvm_unreachable("Invalid expression");
227 }
228 
229 // This function applies relocations to sections without SHF_ALLOC bit.
230 // Such sections are never mapped to memory at runtime. Debug sections are
231 // an example. Relocations in non-alloc sections are much easier to
232 // handle than in allocated sections because it will never need complex
233 // treatement such as GOT or PLT (because at runtime no one refers them).
234 // So, we handle relocations for non-alloc sections directly in this
235 // function as a performance optimization.
236 template <class ELFT>
237 template <class RelTy>
238 void InputSection<ELFT>::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
239   const unsigned Bits = sizeof(uintX_t) * 8;
240   for (const RelTy &Rel : Rels) {
241     uint32_t Type = Rel.getType(Config->Mips64EL);
242     uintX_t Addend = getAddend<ELFT>(Rel);
243     if (!RelTy::IsRela)
244       Addend += Target->getImplicitAddend(Buf + Rel.r_offset, Type);
245 
246     SymbolBody &Sym = this->File->getRelocTargetSym(Rel);
247     if (Target->getRelExpr(Type, Sym) != R_ABS) {
248       error(this->getSectionName() + " has non-ABS reloc");
249       return;
250     }
251 
252     uintX_t Offset = this->getOffset(Rel.r_offset);
253     uint8_t *BufLoc = Buf + Offset;
254     uintX_t AddrLoc = this->OutSec->getVA() + Offset;
255     uint64_t SymVA = SignExtend64<Bits>(getSymVA<ELFT>(
256         Type, Addend, AddrLoc, Sym, BufLoc, *this->File, R_ABS));
257     Target->relocateOne(BufLoc, Type, SymVA);
258   }
259 }
260 
261 template <class ELFT>
262 void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd) {
263   // scanReloc function in Writer.cpp constructs Relocations
264   // vector only for SHF_ALLOC'ed sections. For other sections,
265   // we handle relocations directly here.
266   auto *IS = dyn_cast<InputSection<ELFT>>(this);
267   if (IS && !(IS->Header->sh_flags & SHF_ALLOC)) {
268     for (const Elf_Shdr *RelSec : IS->RelocSections) {
269       if (RelSec->sh_type == SHT_RELA)
270         IS->relocateNonAlloc(Buf, IS->File->getObj().relas(RelSec));
271       else
272         IS->relocateNonAlloc(Buf, IS->File->getObj().rels(RelSec));
273     }
274     return;
275   }
276 
277   const unsigned Bits = sizeof(uintX_t) * 8;
278   for (const Relocation &Rel : Relocations) {
279     uintX_t Offset = Rel.Offset;
280     uint8_t *BufLoc = Buf + Offset;
281     uint32_t Type = Rel.Type;
282     uintX_t A = Rel.Addend;
283 
284     uintX_t AddrLoc = OutSec->getVA() + Offset;
285     RelExpr Expr = Rel.Expr;
286     uint64_t SymVA = SignExtend64<Bits>(
287         getSymVA<ELFT>(Type, A, AddrLoc, *Rel.Sym, BufLoc, *File, Expr));
288 
289     if (Expr == R_RELAX_TLS_IE_TO_LE) {
290       Target->relaxTlsIeToLe(BufLoc, Type, SymVA);
291       continue;
292     }
293     if (Expr == R_RELAX_TLS_LD_TO_LE) {
294       Target->relaxTlsLdToLe(BufLoc, Type, SymVA);
295       continue;
296     }
297     if (Expr == R_RELAX_TLS_GD_TO_LE) {
298       Target->relaxTlsGdToLe(BufLoc, Type, SymVA);
299       continue;
300     }
301     if (Expr == R_RELAX_TLS_GD_TO_IE_PC || Expr == R_RELAX_TLS_GD_TO_IE) {
302       Target->relaxTlsGdToIe(BufLoc, Type, SymVA);
303       continue;
304     }
305 
306     if (Expr == R_PPC_PLT_OPD) {
307       uint32_t Nop = 0x60000000;
308       if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == Nop)
309         write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1)
310     }
311 
312     Target->relocateOne(BufLoc, Type, SymVA);
313   }
314 }
315 
316 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
317   if (this->Header->sh_type == SHT_NOBITS)
318     return;
319   ELFFile<ELFT> &EObj = this->File->getObj();
320 
321   // If -r is given, then an InputSection may be a relocation section.
322   if (this->Header->sh_type == SHT_RELA) {
323     copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
324     return;
325   }
326   if (this->Header->sh_type == SHT_REL) {
327     copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
328     return;
329   }
330 
331   // Copy section contents from source object file to output file.
332   ArrayRef<uint8_t> Data = this->getSectionData();
333   memcpy(Buf + OutSecOff, Data.data(), Data.size());
334 
335   // Iterate over all relocation sections that apply to this section.
336   uint8_t *BufEnd = Buf + OutSecOff + Data.size();
337   this->relocate(Buf, BufEnd);
338 
339   // The section might have a data/code generated by the linker and need
340   // to be written after the section. Usually these are thunks - small piece
341   // of code used to jump between "incompatible" functions like PIC and non-PIC
342   // or if the jump target too far and its address does not fit to the short
343   // jump istruction.
344   if (!Thunks.empty()) {
345     Buf += OutSecOff + getThunkOff();
346     for (const SymbolBody *S : Thunks) {
347       Target->writeThunk(Buf, S->getVA<ELFT>());
348       Buf += Target->ThunkSize;
349     }
350   }
351 }
352 
353 template <class ELFT>
354 void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
355   this->Align = std::max(this->Align, Other->Align);
356   Other->Repl = this->Repl;
357   Other->Live = false;
358 }
359 
360 template <class ELFT>
361 SplitInputSection<ELFT>::SplitInputSection(
362     elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
363     typename InputSectionBase<ELFT>::Kind SectionKind)
364     : InputSectionBase<ELFT>(File, Header, SectionKind) {}
365 
366 template <class ELFT>
367 EHInputSection<ELFT>::EHInputSection(elf::ObjectFile<ELFT> *F,
368                                      const Elf_Shdr *Header)
369     : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
370   // Mark .eh_frame sections as live by default because there are
371   // usually no relocations that point to .eh_frames. Otherwise,
372   // the garbage collector would drop all .eh_frame sections.
373   this->Live = true;
374 }
375 
376 template <class ELFT>
377 bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
378   return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
379 }
380 
381 template <class ELFT>
382 typename ELFT::uint EHInputSection<ELFT>::getOffset(uintX_t Offset) {
383   // The file crtbeginT.o has relocations pointing to the start of an empty
384   // .eh_frame that is known to be the first in the link. It does that to
385   // identify the start of the output .eh_frame. Handle this special case.
386   if (this->getSectionHdr()->sh_size == 0)
387     return Offset;
388   std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
389   uintX_t Base = I->second;
390   if (Base == uintX_t(-1))
391     return -1; // Not in the output
392 
393   uintX_t Addend = Offset - I->first;
394   return Base + Addend;
395 }
396 
397 static size_t findNull(StringRef S, size_t EntSize) {
398   // Optimize the common case.
399   if (EntSize == 1)
400     return S.find(0);
401 
402   for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
403     const char *B = S.begin() + I;
404     if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
405       return I;
406   }
407   return StringRef::npos;
408 }
409 
410 template <class ELFT>
411 MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
412                                            const Elf_Shdr *Header)
413     : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {
414   uintX_t EntSize = Header->sh_entsize;
415   ArrayRef<uint8_t> D = this->getSectionData();
416   StringRef Data((const char *)D.data(), D.size());
417   std::vector<std::pair<uintX_t, uintX_t>> &Offsets = this->Offsets;
418 
419   uintX_t V = Config->GcSections ? -1 : 0;
420   if (Header->sh_flags & SHF_STRINGS) {
421     uintX_t Offset = 0;
422     while (!Data.empty()) {
423       size_t End = findNull(Data, EntSize);
424       if (End == StringRef::npos)
425         fatal("string is not null terminated");
426       Offsets.push_back(std::make_pair(Offset, V));
427       uintX_t Size = End + EntSize;
428       Data = Data.substr(Size);
429       Offset += Size;
430     }
431     return;
432   }
433 
434   // If this is not of type string, every entry has the same size.
435   size_t Size = Data.size();
436   assert((Size % EntSize) == 0);
437   for (unsigned I = 0, N = Size; I != N; I += EntSize)
438     Offsets.push_back(std::make_pair(I, V));
439 }
440 
441 template <class ELFT>
442 bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
443   return S->SectionKind == InputSectionBase<ELFT>::Merge;
444 }
445 
446 template <class ELFT>
447 std::pair<std::pair<typename ELFT::uint, typename ELFT::uint> *,
448           typename ELFT::uint>
449 SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
450   ArrayRef<uint8_t> D = this->getSectionData();
451   StringRef Data((const char *)D.data(), D.size());
452   uintX_t Size = Data.size();
453   if (Offset >= Size)
454     fatal("entry is past the end of the section");
455 
456   // Find the element this offset points to.
457   auto I = std::upper_bound(
458       Offsets.begin(), Offsets.end(), Offset,
459       [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
460         return A < B.first;
461       });
462   uintX_t End = I == Offsets.end() ? Data.size() : I->first;
463   --I;
464   return std::make_pair(&*I, End);
465 }
466 
467 template <class ELFT>
468 typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
469   std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
470       this->getRangeAndSize(Offset);
471   std::pair<uintX_t, uintX_t> *I = T.first;
472   uintX_t End = T.second;
473   uintX_t Start = I->first;
474 
475   // Compute the Addend and if the Base is cached, return.
476   uintX_t Addend = Offset - Start;
477   uintX_t &Base = I->second;
478   if (Base != uintX_t(-1))
479     return Base + Addend;
480 
481   // Map the base to the offset in the output section and cache it.
482   ArrayRef<uint8_t> D = this->getSectionData();
483   StringRef Data((const char *)D.data(), D.size());
484   StringRef Entry = Data.substr(Start, End - Start);
485   Base =
486       static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
487   return Base + Addend;
488 }
489 
490 template <class ELFT>
491 MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
492                                                        const Elf_Shdr *Hdr)
493     : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
494   // Initialize this->Reginfo.
495   ArrayRef<uint8_t> D = this->getSectionData();
496   if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
497     fatal("invalid size of .reginfo section");
498   Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
499 }
500 
501 template <class ELFT>
502 bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
503   return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
504 }
505 
506 template class elf::InputSectionBase<ELF32LE>;
507 template class elf::InputSectionBase<ELF32BE>;
508 template class elf::InputSectionBase<ELF64LE>;
509 template class elf::InputSectionBase<ELF64BE>;
510 
511 template class elf::InputSection<ELF32LE>;
512 template class elf::InputSection<ELF32BE>;
513 template class elf::InputSection<ELF64LE>;
514 template class elf::InputSection<ELF64BE>;
515 
516 template class elf::SplitInputSection<ELF32LE>;
517 template class elf::SplitInputSection<ELF32BE>;
518 template class elf::SplitInputSection<ELF64LE>;
519 template class elf::SplitInputSection<ELF64BE>;
520 
521 template class elf::EHInputSection<ELF32LE>;
522 template class elf::EHInputSection<ELF32BE>;
523 template class elf::EHInputSection<ELF64LE>;
524 template class elf::EHInputSection<ELF64BE>;
525 
526 template class elf::MergeInputSection<ELF32LE>;
527 template class elf::MergeInputSection<ELF32BE>;
528 template class elf::MergeInputSection<ELF64LE>;
529 template class elf::MergeInputSection<ELF64BE>;
530 
531 template class elf::MipsReginfoInputSection<ELF32LE>;
532 template class elf::MipsReginfoInputSection<ELF32BE>;
533 template class elf::MipsReginfoInputSection<ELF64LE>;
534 template class elf::MipsReginfoInputSection<ELF64BE>;
535