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     uint8_t *BufLoc = Buf + Rel.r_offset;
242     uintX_t AddrLoc = this->OutSec->getVA() + Rel.r_offset;
243     uint32_t Type = Rel.getType(Config->Mips64EL);
244     SymbolBody &Sym = this->File->getRelocTargetSym(Rel);
245 
246     if (Target->getRelExpr(Type, Sym) != R_ABS) {
247       error(this->getSectionName() + " has non-ABS reloc");
248       return;
249     }
250 
251     uint64_t SymVA = SignExtend64<Bits>(getSymVA<ELFT>(
252         Type, getAddend<ELFT>(Rel), AddrLoc, Sym, BufLoc, *this->File, R_ABS));
253     Target->relocateOne(BufLoc, Type, SymVA);
254   }
255 }
256 
257 template <class ELFT>
258 void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd) {
259   // scanReloc function in Writer.cpp constructs Relocations
260   // vector only for SHF_ALLOC'ed sections. For other sections,
261   // we handle relocations directly here.
262   auto *IS = dyn_cast<InputSection<ELFT>>(this);
263   if (IS && !(IS->Header->sh_flags & SHF_ALLOC)) {
264     for (const Elf_Shdr *RelSec : IS->RelocSections) {
265       if (RelSec->sh_type == SHT_RELA)
266         IS->relocateNonAlloc(Buf, IS->File->getObj().relas(RelSec));
267       else
268         IS->relocateNonAlloc(Buf, IS->File->getObj().rels(RelSec));
269     }
270     return;
271   }
272 
273   const unsigned Bits = sizeof(uintX_t) * 8;
274   for (const Relocation &Rel : Relocations) {
275     uintX_t Offset = Rel.Offset;
276     uint8_t *BufLoc = Buf + Offset;
277     uint32_t Type = Rel.Type;
278     uintX_t A = Rel.Addend;
279 
280     uintX_t AddrLoc = OutSec->getVA() + Offset;
281     RelExpr Expr = Rel.Expr;
282     uint64_t SymVA = SignExtend64<Bits>(
283         getSymVA<ELFT>(Type, A, AddrLoc, *Rel.Sym, BufLoc, *File, Expr));
284 
285     if (Expr == R_RELAX_TLS_IE_TO_LE) {
286       Target->relaxTlsIeToLe(BufLoc, Type, SymVA);
287       continue;
288     }
289     if (Expr == R_RELAX_TLS_LD_TO_LE) {
290       Target->relaxTlsLdToLe(BufLoc, Type, SymVA);
291       continue;
292     }
293     if (Expr == R_RELAX_TLS_GD_TO_LE) {
294       Target->relaxTlsGdToLe(BufLoc, Type, SymVA);
295       continue;
296     }
297     if (Expr == R_RELAX_TLS_GD_TO_IE_PC || Expr == R_RELAX_TLS_GD_TO_IE) {
298       Target->relaxTlsGdToIe(BufLoc, Type, SymVA);
299       continue;
300     }
301 
302     if (Expr == R_PPC_PLT_OPD) {
303       uint32_t Nop = 0x60000000;
304       if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == Nop)
305         write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1)
306     }
307 
308     Target->relocateOne(BufLoc, Type, SymVA);
309   }
310 }
311 
312 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
313   if (this->Header->sh_type == SHT_NOBITS)
314     return;
315   ELFFile<ELFT> &EObj = this->File->getObj();
316 
317   // If -r is given, then an InputSection may be a relocation section.
318   if (this->Header->sh_type == SHT_RELA) {
319     copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
320     return;
321   }
322   if (this->Header->sh_type == SHT_REL) {
323     copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
324     return;
325   }
326 
327   // Copy section contents from source object file to output file.
328   ArrayRef<uint8_t> Data = this->getSectionData();
329   memcpy(Buf + OutSecOff, Data.data(), Data.size());
330 
331   // Iterate over all relocation sections that apply to this section.
332   uint8_t *BufEnd = Buf + OutSecOff + Data.size();
333   this->relocate(Buf, BufEnd);
334 
335   // The section might have a data/code generated by the linker and need
336   // to be written after the section. Usually these are thunks - small piece
337   // of code used to jump between "incompatible" functions like PIC and non-PIC
338   // or if the jump target too far and its address does not fit to the short
339   // jump istruction.
340   if (!Thunks.empty()) {
341     Buf += OutSecOff + getThunkOff();
342     for (const SymbolBody *S : Thunks) {
343       Target->writeThunk(Buf, S->getVA<ELFT>());
344       Buf += Target->ThunkSize;
345     }
346   }
347 }
348 
349 template <class ELFT>
350 void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
351   this->Align = std::max(this->Align, Other->Align);
352   Other->Repl = this->Repl;
353   Other->Live = false;
354 }
355 
356 template <class ELFT>
357 SplitInputSection<ELFT>::SplitInputSection(
358     elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
359     typename InputSectionBase<ELFT>::Kind SectionKind)
360     : InputSectionBase<ELFT>(File, Header, SectionKind) {}
361 
362 template <class ELFT>
363 EHInputSection<ELFT>::EHInputSection(elf::ObjectFile<ELFT> *F,
364                                      const Elf_Shdr *Header)
365     : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
366   // Mark .eh_frame sections as live by default because there are
367   // usually no relocations that point to .eh_frames. Otherwise,
368   // the garbage collector would drop all .eh_frame sections.
369   this->Live = true;
370 }
371 
372 template <class ELFT>
373 bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
374   return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
375 }
376 
377 template <class ELFT>
378 typename ELFT::uint EHInputSection<ELFT>::getOffset(uintX_t Offset) {
379   // The file crtbeginT.o has relocations pointing to the start of an empty
380   // .eh_frame that is known to be the first in the link. It does that to
381   // identify the start of the output .eh_frame. Handle this special case.
382   if (this->getSectionHdr()->sh_size == 0)
383     return Offset;
384   std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
385   uintX_t Base = I->second;
386   if (Base == uintX_t(-1))
387     return -1; // Not in the output
388 
389   uintX_t Addend = Offset - I->first;
390   return Base + Addend;
391 }
392 
393 static size_t findNull(StringRef S, size_t EntSize) {
394   // Optimize the common case.
395   if (EntSize == 1)
396     return S.find(0);
397 
398   for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
399     const char *B = S.begin() + I;
400     if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
401       return I;
402   }
403   return StringRef::npos;
404 }
405 
406 template <class ELFT>
407 MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
408                                            const Elf_Shdr *Header)
409     : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {
410   uintX_t EntSize = Header->sh_entsize;
411   ArrayRef<uint8_t> D = this->getSectionData();
412   StringRef Data((const char *)D.data(), D.size());
413   std::vector<std::pair<uintX_t, uintX_t>> &Offsets = this->Offsets;
414 
415   uintX_t V = Config->GcSections ? -1 : 0;
416   if (Header->sh_flags & SHF_STRINGS) {
417     uintX_t Offset = 0;
418     while (!Data.empty()) {
419       size_t End = findNull(Data, EntSize);
420       if (End == StringRef::npos)
421         fatal("string is not null terminated");
422       Offsets.push_back(std::make_pair(Offset, V));
423       uintX_t Size = End + EntSize;
424       Data = Data.substr(Size);
425       Offset += Size;
426     }
427     return;
428   }
429 
430   // If this is not of type string, every entry has the same size.
431   size_t Size = Data.size();
432   assert((Size % EntSize) == 0);
433   for (unsigned I = 0, N = Size; I != N; I += EntSize)
434     Offsets.push_back(std::make_pair(I, V));
435 }
436 
437 template <class ELFT>
438 bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
439   return S->SectionKind == InputSectionBase<ELFT>::Merge;
440 }
441 
442 template <class ELFT>
443 std::pair<std::pair<typename ELFT::uint, typename ELFT::uint> *,
444           typename ELFT::uint>
445 SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
446   ArrayRef<uint8_t> D = this->getSectionData();
447   StringRef Data((const char *)D.data(), D.size());
448   uintX_t Size = Data.size();
449   if (Offset >= Size)
450     fatal("entry is past the end of the section");
451 
452   // Find the element this offset points to.
453   auto I = std::upper_bound(
454       Offsets.begin(), Offsets.end(), Offset,
455       [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
456         return A < B.first;
457       });
458   uintX_t End = I == Offsets.end() ? Data.size() : I->first;
459   --I;
460   return std::make_pair(&*I, End);
461 }
462 
463 template <class ELFT>
464 typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
465   std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
466       this->getRangeAndSize(Offset);
467   std::pair<uintX_t, uintX_t> *I = T.first;
468   uintX_t End = T.second;
469   uintX_t Start = I->first;
470 
471   // Compute the Addend and if the Base is cached, return.
472   uintX_t Addend = Offset - Start;
473   uintX_t &Base = I->second;
474   if (Base != uintX_t(-1))
475     return Base + Addend;
476 
477   // Map the base to the offset in the output section and cache it.
478   ArrayRef<uint8_t> D = this->getSectionData();
479   StringRef Data((const char *)D.data(), D.size());
480   StringRef Entry = Data.substr(Start, End - Start);
481   Base =
482       static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
483   return Base + Addend;
484 }
485 
486 template <class ELFT>
487 MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
488                                                        const Elf_Shdr *Hdr)
489     : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
490   // Initialize this->Reginfo.
491   ArrayRef<uint8_t> D = this->getSectionData();
492   if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
493     fatal("invalid size of .reginfo section");
494   Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
495 }
496 
497 template <class ELFT>
498 bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
499   return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
500 }
501 
502 template class elf::InputSectionBase<ELF32LE>;
503 template class elf::InputSectionBase<ELF32BE>;
504 template class elf::InputSectionBase<ELF64LE>;
505 template class elf::InputSectionBase<ELF64BE>;
506 
507 template class elf::InputSection<ELF32LE>;
508 template class elf::InputSection<ELF32BE>;
509 template class elf::InputSection<ELF64LE>;
510 template class elf::InputSection<ELF64BE>;
511 
512 template class elf::SplitInputSection<ELF32LE>;
513 template class elf::SplitInputSection<ELF32BE>;
514 template class elf::SplitInputSection<ELF64LE>;
515 template class elf::SplitInputSection<ELF64BE>;
516 
517 template class elf::EHInputSection<ELF32LE>;
518 template class elf::EHInputSection<ELF32BE>;
519 template class elf::EHInputSection<ELF64LE>;
520 template class elf::EHInputSection<ELF64BE>;
521 
522 template class elf::MergeInputSection<ELF32LE>;
523 template class elf::MergeInputSection<ELF32BE>;
524 template class elf::MergeInputSection<ELF64LE>;
525 template class elf::MergeInputSection<ELF64BE>;
526 
527 template class elf::MipsReginfoInputSection<ELF32LE>;
528 template class elf::MipsReginfoInputSection<ELF32BE>;
529 template class elf::MipsReginfoInputSection<ELF64LE>;
530 template class elf::MipsReginfoInputSection<ELF64BE>;
531