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