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 // Returns a section that Rel relocation is pointing to.
81 template <class ELFT>
82 InputSectionBase<ELFT> *
83 InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) const {
84   // Global symbol
85   uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
86   SymbolBody &B = File->getSymbolBody(SymIndex).repl();
87   if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B))
88     if (D->Section)
89       return D->Section->Repl;
90   return nullptr;
91 }
92 
93 template <class ELFT>
94 InputSectionBase<ELFT> *
95 InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) const {
96   return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel));
97 }
98 
99 template <class ELFT>
100 InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F,
101                                  const Elf_Shdr *Header)
102     : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
103 
104 template <class ELFT>
105 bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
106   return S->SectionKind == Base::Regular;
107 }
108 
109 template <class ELFT>
110 InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() {
111   assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL);
112   ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections();
113   return Sections[this->Header->sh_info];
114 }
115 
116 template <class ELFT> void InputSection<ELFT>::addThunk(SymbolBody &Body) {
117   Body.ThunkIndex = Thunks.size();
118   Thunks.push_back(&Body);
119 }
120 
121 template <class ELFT> uint64_t InputSection<ELFT>::getThunkOff() const {
122   return this->Header->sh_size;
123 }
124 
125 template <class ELFT> uint64_t InputSection<ELFT>::getThunksSize() const {
126   return Thunks.size() * Target->ThunkSize;
127 }
128 
129 // This is used for -r. We can't use memcpy to copy relocations because we need
130 // to update symbol table offset and section index for each relocation. So we
131 // copy relocations one by one.
132 template <class ELFT>
133 template <class RelTy>
134 void InputSection<ELFT>::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
135   InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
136 
137   for (const RelTy &Rel : Rels) {
138     uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
139     uint32_t Type = Rel.getType(Config->Mips64EL);
140     SymbolBody &Body = this->File->getSymbolBody(SymIndex).repl();
141 
142     RelTy *P = reinterpret_cast<RelTy *>(Buf);
143     Buf += sizeof(RelTy);
144 
145     P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
146     P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL);
147   }
148 }
149 
150 // Page(Expr) is the page address of the expression Expr, defined
151 // as (Expr & ~0xFFF). (This applies even if the machine page size
152 // supported by the platform has a different value.)
153 static uint64_t getAArch64Page(uint64_t Expr) {
154   return Expr & (~static_cast<uint64_t>(0xFFF));
155 }
156 
157 template <class ELFT>
158 static typename ELFT::uint
159 getSymVA(uint32_t Type, typename ELFT::uint A, typename ELFT::uint P,
160          const SymbolBody &Body, uint8_t *BufLoc,
161          const elf::ObjectFile<ELFT> &File, RelExpr Expr) {
162   typedef typename ELFT::uint uintX_t;
163   switch (Expr) {
164   case R_TLSLD:
165     return Out<ELFT>::Got->getTlsIndexOff() + A -
166            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
167   case R_TLSLD_PC:
168     return Out<ELFT>::Got->getTlsIndexVA() + A - P;
169   case R_THUNK:
170     return Body.getThunkVA<ELFT>();
171   case R_PPC_TOC:
172     return getPPC64TocBase() + A;
173   case R_TLSGD:
174     return Out<ELFT>::Got->getGlobalDynOffset(Body) + A -
175            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
176   case R_TLSGD_PC:
177     return Out<ELFT>::Got->getGlobalDynAddr(Body) + A - P;
178   case R_PLT:
179     return Body.getPltVA<ELFT>() + A;
180   case R_PLT_PC:
181   case R_PPC_PLT_OPD:
182     return Body.getPltVA<ELFT>() + A - P;
183   case R_SIZE:
184     return Body.getSize<ELFT>() + A;
185   case R_GOTREL:
186     return Body.getVA<ELFT>(A) - Out<ELFT>::Got->getVA();
187   case R_GOT_FROM_END:
188     return Body.getGotOffset<ELFT>() + A -
189            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
190   case R_GOT:
191   case R_RELAX_TLS_GD_TO_IE:
192     return Body.getGotVA<ELFT>() + A;
193   case R_GOT_PAGE_PC:
194     return getAArch64Page(Body.getGotVA<ELFT>() + A) - getAArch64Page(P);
195   case R_GOT_PC:
196   case R_RELAX_TLS_GD_TO_IE_PC:
197     return Body.getGotVA<ELFT>() + A - P;
198   case R_GOTONLY_PC:
199     return Out<ELFT>::Got->getVA() + A - P;
200   case R_TLS:
201     return Body.getVA<ELFT>(A) - Out<ELFT>::TlsPhdr->p_memsz;
202   case R_NEG_TLS:
203     return Out<ELF32LE>::TlsPhdr->p_memsz - Body.getVA<ELFT>(A);
204   case R_ABS:
205   case R_RELAX_TLS_GD_TO_LE:
206   case R_RELAX_TLS_IE_TO_LE:
207   case R_RELAX_TLS_LD_TO_LE:
208     return Body.getVA<ELFT>(A);
209   case R_MIPS_GP0:
210     // We need to adjust SymVA value in case of R_MIPS_GPREL16/32
211     // relocations because they use the following expression to calculate
212     // the relocation's result for local symbol: S + A + GP0 - G.
213     return Body.getVA<ELFT>(A) + File.getMipsGp0();
214   case R_MIPS_GOT_LOCAL:
215     // If relocation against MIPS local symbol requires GOT entry, this entry
216     // should be initialized by 'page address'. This address is high 16-bits
217     // of sum the symbol's value and the addend.
218     return Out<ELFT>::Got->getMipsLocalPageAddr(Body.getVA<ELFT>(A));
219   case R_MIPS_GOT:
220     // For non-local symbols GOT entries should contain their full
221     // addresses. But if such symbol cannot be preempted, we do not
222     // have to put them into the "global" part of GOT and use dynamic
223     // linker to determine their actual addresses. That is why we
224     // create GOT entries for them in the "local" part of GOT.
225     return Out<ELFT>::Got->getMipsLocalEntryAddr(Body.getVA<ELFT>(A));
226   case R_PPC_OPD: {
227     uint64_t SymVA = Body.getVA<ELFT>(A);
228     // If we have an undefined weak symbol, we might get here with a symbol
229     // address of zero. That could overflow, but the code must be unreachable,
230     // so don't bother doing anything at all.
231     if (!SymVA)
232       return 0;
233     if (Out<ELF64BE>::Opd) {
234       // If this is a local call, and we currently have the address of a
235       // function-descriptor, get the underlying code address instead.
236       uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
237       uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
238       bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd;
239       if (InOpd)
240         SymVA = read64be(&Out<ELF64BE>::OpdBuf[SymVA - OpdStart]);
241     }
242     return SymVA - P;
243   }
244   case R_PC:
245     return Body.getVA<ELFT>(A) - P;
246   case R_PAGE_PC:
247     return getAArch64Page(Body.getVA<ELFT>(A)) - getAArch64Page(P);
248   }
249   llvm_unreachable("Invalid expression");
250 }
251 
252 template <class ELFT>
253 void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd) {
254   const unsigned Bits = sizeof(uintX_t) * 8;
255   for (const Relocation &Rel : Relocations) {
256     uintX_t Offset = Rel.Offset;
257     uint8_t *BufLoc = Buf + Offset;
258     uint32_t Type = Rel.Type;
259     uintX_t A = Rel.Addend;
260 
261     uintX_t AddrLoc = OutSec->getVA() + Offset;
262     RelExpr Expr = Rel.Expr;
263     uint64_t SymVA = SignExtend64<Bits>(
264         getSymVA<ELFT>(Type, A, AddrLoc, *Rel.Sym, BufLoc, *File, Expr));
265 
266     if (Expr == R_RELAX_TLS_IE_TO_LE) {
267       Target->relaxTlsIeToLe(BufLoc, Type, SymVA);
268       continue;
269     }
270     if (Expr == R_RELAX_TLS_LD_TO_LE) {
271       Target->relaxTlsLdToLe(BufLoc, Type, SymVA);
272       continue;
273     }
274     if (Expr == R_RELAX_TLS_GD_TO_LE) {
275       Target->relaxTlsGdToLe(BufLoc, Type, SymVA);
276       continue;
277     }
278     if (Expr == R_RELAX_TLS_GD_TO_IE_PC || Expr == R_RELAX_TLS_GD_TO_IE) {
279       Target->relaxTlsGdToIe(BufLoc, Type, SymVA);
280       continue;
281     }
282 
283     if (Expr == R_PPC_PLT_OPD) {
284       uint32_t Nop = 0x60000000;
285       if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == Nop)
286         write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1)
287     }
288 
289     Target->relocateOne(BufLoc, Type, SymVA);
290   }
291 }
292 
293 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
294   if (this->Header->sh_type == SHT_NOBITS)
295     return;
296   ELFFile<ELFT> &EObj = this->File->getObj();
297 
298   // If -r is given, then an InputSection may be a relocation section.
299   if (this->Header->sh_type == SHT_RELA) {
300     copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
301     return;
302   }
303   if (this->Header->sh_type == SHT_REL) {
304     copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
305     return;
306   }
307 
308   // Copy section contents from source object file to output file.
309   ArrayRef<uint8_t> Data = this->getSectionData();
310   memcpy(Buf + OutSecOff, Data.data(), Data.size());
311 
312   // Iterate over all relocation sections that apply to this section.
313   uint8_t *BufEnd = Buf + OutSecOff + Data.size();
314   this->relocate(Buf, BufEnd);
315 
316   // The section might have a data/code generated by the linker and need
317   // to be written after the section. Usually these are thunks - small piece
318   // of code used to jump between "incompatible" functions like PIC and non-PIC
319   // or if the jump target too far and its address does not fit to the short
320   // jump istruction.
321   if (!Thunks.empty()) {
322     Buf += OutSecOff + getThunkOff();
323     for (const SymbolBody *S : Thunks) {
324       Target->writeThunk(Buf, S->getVA<ELFT>());
325       Buf += Target->ThunkSize;
326     }
327   }
328 }
329 
330 template <class ELFT>
331 void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
332   this->Align = std::max(this->Align, Other->Align);
333   Other->Repl = this->Repl;
334   Other->Live = false;
335 }
336 
337 template <class ELFT>
338 SplitInputSection<ELFT>::SplitInputSection(
339     elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
340     typename InputSectionBase<ELFT>::Kind SectionKind)
341     : InputSectionBase<ELFT>(File, Header, SectionKind) {}
342 
343 template <class ELFT>
344 EHInputSection<ELFT>::EHInputSection(elf::ObjectFile<ELFT> *F,
345                                      const Elf_Shdr *Header)
346     : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
347   // Mark .eh_frame sections as live by default because there are
348   // usually no relocations that point to .eh_frames. Otherwise,
349   // the garbage collector would drop all .eh_frame sections.
350   this->Live = true;
351 }
352 
353 template <class ELFT>
354 bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
355   return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
356 }
357 
358 template <class ELFT>
359 typename ELFT::uint EHInputSection<ELFT>::getOffset(uintX_t Offset) {
360   // The file crtbeginT.o has relocations pointing to the start of an empty
361   // .eh_frame that is known to be the first in the link. It does that to
362   // identify the start of the output .eh_frame. Handle this special case.
363   if (this->getSectionHdr()->sh_size == 0)
364     return Offset;
365   std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
366   uintX_t Base = I->second;
367   if (Base == uintX_t(-1))
368     return -1; // Not in the output
369 
370   uintX_t Addend = Offset - I->first;
371   return Base + Addend;
372 }
373 
374 template <class ELFT>
375 MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
376                                            const Elf_Shdr *Header)
377     : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
378 
379 template <class ELFT>
380 bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
381   return S->SectionKind == InputSectionBase<ELFT>::Merge;
382 }
383 
384 template <class ELFT>
385 std::pair<std::pair<typename ELFT::uint, typename ELFT::uint> *,
386           typename ELFT::uint>
387 SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
388   ArrayRef<uint8_t> D = this->getSectionData();
389   StringRef Data((const char *)D.data(), D.size());
390   uintX_t Size = Data.size();
391   if (Offset >= Size)
392     fatal("entry is past the end of the section");
393 
394   // Find the element this offset points to.
395   auto I = std::upper_bound(
396       Offsets.begin(), Offsets.end(), Offset,
397       [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
398         return A < B.first;
399       });
400   uintX_t End = I == Offsets.end() ? Data.size() : I->first;
401   --I;
402   return std::make_pair(&*I, End);
403 }
404 
405 template <class ELFT>
406 typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
407   std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
408       this->getRangeAndSize(Offset);
409   std::pair<uintX_t, uintX_t> *I = T.first;
410   uintX_t End = T.second;
411   uintX_t Start = I->first;
412 
413   // Compute the Addend and if the Base is cached, return.
414   uintX_t Addend = Offset - Start;
415   uintX_t &Base = I->second;
416   if (Base != uintX_t(-1))
417     return Base + Addend;
418 
419   // Map the base to the offset in the output section and cache it.
420   ArrayRef<uint8_t> D = this->getSectionData();
421   StringRef Data((const char *)D.data(), D.size());
422   StringRef Entry = Data.substr(Start, End - Start);
423   Base =
424       static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
425   return Base + Addend;
426 }
427 
428 template <class ELFT>
429 MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
430                                                        const Elf_Shdr *Hdr)
431     : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
432   // Initialize this->Reginfo.
433   ArrayRef<uint8_t> D = this->getSectionData();
434   if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
435     fatal("invalid size of .reginfo section");
436   Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
437 }
438 
439 template <class ELFT>
440 bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
441   return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
442 }
443 
444 template class elf::InputSectionBase<ELF32LE>;
445 template class elf::InputSectionBase<ELF32BE>;
446 template class elf::InputSectionBase<ELF64LE>;
447 template class elf::InputSectionBase<ELF64BE>;
448 
449 template class elf::InputSection<ELF32LE>;
450 template class elf::InputSection<ELF32BE>;
451 template class elf::InputSection<ELF64LE>;
452 template class elf::InputSection<ELF64BE>;
453 
454 template class elf::EHInputSection<ELF32LE>;
455 template class elf::EHInputSection<ELF32BE>;
456 template class elf::EHInputSection<ELF64LE>;
457 template class elf::EHInputSection<ELF64BE>;
458 
459 template class elf::MergeInputSection<ELF32LE>;
460 template class elf::MergeInputSection<ELF32BE>;
461 template class elf::MergeInputSection<ELF64LE>;
462 template class elf::MergeInputSection<ELF64BE>;
463 
464 template class elf::MipsReginfoInputSection<ELF32LE>;
465 template class elf::MipsReginfoInputSection<ELF32BE>;
466 template class elf::MipsReginfoInputSection<ELF64LE>;
467 template class elf::MipsReginfoInputSection<ELF64BE>;
468