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 "EhFrame.h"
13 #include "Error.h"
14 #include "InputFiles.h"
15 #include "LinkerScript.h"
16 #include "Memory.h"
17 #include "OutputSections.h"
18 #include "Relocations.h"
19 #include "SyntheticSections.h"
20 #include "Target.h"
21 #include "Thunks.h"
22 #include "llvm/Object/Decompressor.h"
23 #include "llvm/Support/Compression.h"
24 #include "llvm/Support/Endian.h"
25 #include <mutex>
26 
27 using namespace llvm;
28 using namespace llvm::ELF;
29 using namespace llvm::object;
30 using namespace llvm::support;
31 using namespace llvm::support::endian;
32 
33 using namespace lld;
34 using namespace lld::elf;
35 
36 // Returns a string to construct an error message.
37 template <class ELFT>
38 std::string lld::toString(const InputSectionBase<ELFT> *Sec) {
39   // File can be absent if section is synthetic.
40   std::string FileName =
41       Sec->getFile() ? Sec->getFile()->getName() : "<internal>";
42   return (FileName + ":(" + Sec->Name + ")").str();
43 }
44 
45 template <class ELFT>
46 static ArrayRef<uint8_t> getSectionContents(elf::ObjectFile<ELFT> *File,
47                                             const typename ELFT::Shdr *Hdr) {
48   if (!File || Hdr->sh_type == SHT_NOBITS)
49     return makeArrayRef<uint8_t>(nullptr, Hdr->sh_size);
50   return check(File->getObj().getSectionContents(Hdr));
51 }
52 
53 template <class ELFT>
54 InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
55                                          uintX_t Flags, uint32_t Type,
56                                          uintX_t Entsize, uint32_t Link,
57                                          uint32_t Info, uintX_t Addralign,
58                                          ArrayRef<uint8_t> Data, StringRef Name,
59                                          Kind SectionKind)
60     : InputSectionData(SectionKind, Name, Data,
61                        !Config->GcSections || !(Flags & SHF_ALLOC)),
62       File(File), Flags(Flags), Entsize(Entsize), Type(Type), Link(Link),
63       Info(Info), Repl(this) {
64   NumRelocations = 0;
65   AreRelocsRela = false;
66 
67   // The ELF spec states that a value of 0 means the section has
68   // no alignment constraits.
69   uint64_t V = std::max<uint64_t>(Addralign, 1);
70   if (!isPowerOf2_64(V))
71     fatal(toString(File) + ": section sh_addralign is not a power of 2");
72 
73   // We reject object files having insanely large alignments even though
74   // they are allowed by the spec. I think 4GB is a reasonable limitation.
75   // We might want to relax this in the future.
76   if (V > UINT32_MAX)
77     fatal(toString(File) + ": section sh_addralign is too large");
78   Alignment = V;
79 
80   // If it is not a mergeable section, overwrite the flag so that the flag
81   // is consistent with the class. This inconsistency could occur when
82   // string merging is disabled using -O0 flag.
83   if (!Config->Relocatable && !isa<MergeInputSection<ELFT>>(this))
84     this->Flags &= ~(SHF_MERGE | SHF_STRINGS);
85 }
86 
87 template <class ELFT>
88 InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
89                                          const Elf_Shdr *Hdr, StringRef Name,
90                                          Kind SectionKind)
91     : InputSectionBase(File, Hdr->sh_flags & ~SHF_INFO_LINK, Hdr->sh_type,
92                        Hdr->sh_entsize, Hdr->sh_link, Hdr->sh_info,
93                        Hdr->sh_addralign, getSectionContents(File, Hdr), Name,
94                        SectionKind) {
95   this->Offset = Hdr->sh_offset;
96 }
97 
98 template <class ELFT> size_t InputSectionBase<ELFT>::getSize() const {
99   if (auto *S = dyn_cast<SyntheticSection<ELFT>>(this))
100     return S->getSize();
101 
102   return Data.size();
103 }
104 
105 template <class ELFT>
106 typename ELFT::uint InputSectionBase<ELFT>::getOffset(uintX_t Offset) const {
107   switch (kind()) {
108   case Regular:
109     return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
110   case Synthetic:
111     // For synthetic sections we treat offset -1 as the end of the section.
112     // The same approach is used for synthetic symbols (DefinedSynthetic).
113     return cast<InputSection<ELFT>>(this)->OutSecOff +
114            (Offset == uintX_t(-1) ? getSize() : Offset);
115   case EHFrame:
116     // The file crtbeginT.o has relocations pointing to the start of an empty
117     // .eh_frame that is known to be the first in the link. It does that to
118     // identify the start of the output .eh_frame.
119     return Offset;
120   case Merge:
121     return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
122   }
123   llvm_unreachable("invalid section kind");
124 }
125 
126 // Uncompress section contents. Note that this function is called
127 // from parallel_for_each, so it must be thread-safe.
128 template <class ELFT> void InputSectionBase<ELFT>::uncompress() {
129   Decompressor Dec = check(Decompressor::create(
130       Name, toStringRef(Data), ELFT::TargetEndianness == llvm::support::little,
131       ELFT::Is64Bits));
132 
133   size_t Size = Dec.getDecompressedSize();
134   char *OutputBuf;
135   {
136     static std::mutex Mu;
137     std::lock_guard<std::mutex> Lock(Mu);
138     OutputBuf = BAlloc.Allocate<char>(Size);
139   }
140 
141   if (Error E = Dec.decompress({OutputBuf, Size}))
142     fatal(toString(this) +
143           ": decompress failed: " + llvm::toString(std::move(E)));
144   Data = ArrayRef<uint8_t>((uint8_t *)OutputBuf, Size);
145 }
146 
147 template <class ELFT>
148 typename ELFT::uint
149 InputSectionBase<ELFT>::getOffset(const DefinedRegular<ELFT> &Sym) const {
150   return getOffset(Sym.Value);
151 }
152 
153 template <class ELFT>
154 InputSectionBase<ELFT> *InputSectionBase<ELFT>::getLinkOrderDep() const {
155   if ((Flags & SHF_LINK_ORDER) && Link != 0)
156     return getFile()->getSections()[Link];
157   return nullptr;
158 }
159 
160 // Returns a source location string. Used to construct an error message.
161 template <class ELFT>
162 std::string InputSectionBase<ELFT>::getLocation(typename ELFT::uint Offset) {
163   // First check if we can get desired values from debugging information.
164   std::string LineInfo = File->getLineInfo(this, Offset);
165   if (!LineInfo.empty())
166     return LineInfo;
167 
168   // File->SourceFile contains STT_FILE symbol that contains a
169   // source file name. If it's missing, we use an object file name.
170   std::string SrcFile = File->SourceFile;
171   if (SrcFile.empty())
172     SrcFile = toString(File);
173 
174   // Find a function symbol that encloses a given location.
175   for (SymbolBody *B : File->getSymbols())
176     if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B))
177       if (D->Section == this && D->Type == STT_FUNC)
178         if (D->Value <= Offset && Offset < D->Value + D->Size)
179           return SrcFile + ":(function " + toString(*D) + ")";
180 
181   // If there's no symbol, print out the offset in the section.
182   return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
183 }
184 
185 template <class ELFT>
186 InputSection<ELFT>::InputSection() : InputSectionBase<ELFT>() {}
187 
188 template <class ELFT>
189 InputSection<ELFT>::InputSection(uintX_t Flags, uint32_t Type,
190                                  uintX_t Addralign, ArrayRef<uint8_t> Data,
191                                  StringRef Name, Kind K)
192     : InputSectionBase<ELFT>(nullptr, Flags, Type,
193                              /*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, Addralign,
194                              Data, Name, K) {}
195 
196 template <class ELFT>
197 InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F,
198                                  const Elf_Shdr *Header, StringRef Name)
199     : InputSectionBase<ELFT>(F, Header, Name, Base::Regular) {}
200 
201 template <class ELFT>
202 bool InputSection<ELFT>::classof(const InputSectionData *S) {
203   return S->kind() == Base::Regular || S->kind() == Base::Synthetic;
204 }
205 
206 template <class ELFT>
207 InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() {
208   assert(this->Type == SHT_RELA || this->Type == SHT_REL);
209   ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections();
210   return Sections[this->Info];
211 }
212 
213 // This is used for -r. We can't use memcpy to copy relocations because we need
214 // to update symbol table offset and section index for each relocation. So we
215 // copy relocations one by one.
216 template <class ELFT>
217 template <class RelTy>
218 void InputSection<ELFT>::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
219   InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
220 
221   // Loop is slow and have complexity O(N*M), where N - amount of
222   // relocations and M - amount of symbols in symbol table.
223   // That happens because getSymbolIndex(...) call below performs
224   // simple linear search.
225   for (const RelTy &Rel : Rels) {
226     uint32_t Type = Rel.getType(Config->Mips64EL);
227     SymbolBody &Body = this->File->getRelocTargetSym(Rel);
228 
229     Elf_Rela *P = reinterpret_cast<Elf_Rela *>(Buf);
230     Buf += sizeof(RelTy);
231 
232     if (Config->Rela)
233       P->r_addend = getAddend<ELFT>(Rel);
234     P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
235     P->setSymbolAndType(In<ELFT>::SymTab->getSymbolIndex(&Body), Type,
236                         Config->Mips64EL);
237   }
238 }
239 
240 static uint32_t getARMUndefinedRelativeWeakVA(uint32_t Type, uint32_t A,
241                                               uint32_t P) {
242   switch (Type) {
243   case R_ARM_THM_JUMP11:
244     return P + 2;
245   case R_ARM_CALL:
246   case R_ARM_JUMP24:
247   case R_ARM_PC24:
248   case R_ARM_PLT32:
249   case R_ARM_PREL31:
250   case R_ARM_THM_JUMP19:
251   case R_ARM_THM_JUMP24:
252     return P + 4;
253   case R_ARM_THM_CALL:
254     // We don't want an interworking BLX to ARM
255     return P + 5;
256   default:
257     return A;
258   }
259 }
260 
261 static uint64_t getAArch64UndefinedRelativeWeakVA(uint64_t Type, uint64_t A,
262                                                   uint64_t P) {
263   switch (Type) {
264   case R_AARCH64_CALL26:
265   case R_AARCH64_CONDBR19:
266   case R_AARCH64_JUMP26:
267   case R_AARCH64_TSTBR14:
268     return P + 4;
269   default:
270     return A;
271   }
272 }
273 
274 template <class ELFT>
275 static typename ELFT::uint
276 getRelocTargetVA(uint32_t Type, typename ELFT::uint A, typename ELFT::uint P,
277                  const SymbolBody &Body, RelExpr Expr) {
278   switch (Expr) {
279   case R_HINT:
280   case R_TLSDESC_CALL:
281     llvm_unreachable("cannot relocate hint relocs");
282   case R_TLSLD:
283     return In<ELFT>::Got->getTlsIndexOff() + A - In<ELFT>::Got->getSize();
284   case R_TLSLD_PC:
285     return In<ELFT>::Got->getTlsIndexVA() + A - P;
286   case R_PPC_TOC:
287     return getPPC64TocBase() + A;
288   case R_TLSGD:
289     return In<ELFT>::Got->getGlobalDynOffset(Body) + A -
290            In<ELFT>::Got->getSize();
291   case R_TLSGD_PC:
292     return In<ELFT>::Got->getGlobalDynAddr(Body) + A - P;
293   case R_TLSDESC:
294     return In<ELFT>::Got->getGlobalDynAddr(Body) + A;
295   case R_TLSDESC_PAGE:
296     return getAArch64Page(In<ELFT>::Got->getGlobalDynAddr(Body) + A) -
297            getAArch64Page(P);
298   case R_PLT:
299     return Body.getPltVA<ELFT>() + A;
300   case R_PLT_PC:
301   case R_PPC_PLT_OPD:
302     return Body.getPltVA<ELFT>() + A - P;
303   case R_SIZE:
304     return Body.getSize<ELFT>() + A;
305   case R_GOTREL:
306     return Body.getVA<ELFT>(A) - In<ELFT>::Got->getVA();
307   case R_GOTREL_FROM_END:
308     return Body.getVA<ELFT>(A) - In<ELFT>::Got->getVA() -
309            In<ELFT>::Got->getSize();
310   case R_RELAX_TLS_GD_TO_IE_END:
311   case R_GOT_FROM_END:
312     return Body.getGotOffset<ELFT>() + A - In<ELFT>::Got->getSize();
313   case R_RELAX_TLS_GD_TO_IE_ABS:
314   case R_GOT:
315     return Body.getGotVA<ELFT>() + A;
316   case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
317   case R_GOT_PAGE_PC:
318     return getAArch64Page(Body.getGotVA<ELFT>() + A) - getAArch64Page(P);
319   case R_RELAX_TLS_GD_TO_IE:
320   case R_GOT_PC:
321     return Body.getGotVA<ELFT>() + A - P;
322   case R_GOTONLY_PC:
323     return In<ELFT>::Got->getVA() + A - P;
324   case R_GOTONLY_PC_FROM_END:
325     return In<ELFT>::Got->getVA() + A - P + In<ELFT>::Got->getSize();
326   case R_RELAX_TLS_LD_TO_LE:
327   case R_RELAX_TLS_IE_TO_LE:
328   case R_RELAX_TLS_GD_TO_LE:
329   case R_TLS:
330     // A weak undefined TLS symbol resolves to the base of the TLS
331     // block, i.e. gets a value of zero. If we pass --gc-sections to
332     // lld and .tbss is not referenced, it gets reclaimed and we don't
333     // create a TLS program header. Therefore, we resolve this
334     // statically to zero.
335     if (Body.isTls() && (Body.isLazy() || Body.isUndefined()) &&
336         Body.symbol()->isWeak())
337       return 0;
338     if (Target->TcbSize)
339       return Body.getVA<ELFT>(A) +
340              alignTo(Target->TcbSize, Out<ELFT>::TlsPhdr->p_align);
341     return Body.getVA<ELFT>(A) - Out<ELFT>::TlsPhdr->p_memsz;
342   case R_RELAX_TLS_GD_TO_LE_NEG:
343   case R_NEG_TLS:
344     return Out<ELF32LE>::TlsPhdr->p_memsz - Body.getVA<ELFT>(A);
345   case R_ABS:
346   case R_RELAX_GOT_PC_NOPIC:
347     return Body.getVA<ELFT>(A);
348   case R_GOT_OFF:
349     return Body.getGotOffset<ELFT>() + A;
350   case R_MIPS_GOT_LOCAL_PAGE:
351     // If relocation against MIPS local symbol requires GOT entry, this entry
352     // should be initialized by 'page address'. This address is high 16-bits
353     // of sum the symbol's value and the addend.
354     return In<ELFT>::MipsGot->getVA() +
355            In<ELFT>::MipsGot->getPageEntryOffset(Body, A) -
356            In<ELFT>::MipsGot->getGp();
357   case R_MIPS_GOT_OFF:
358   case R_MIPS_GOT_OFF32:
359     // In case of MIPS if a GOT relocation has non-zero addend this addend
360     // should be applied to the GOT entry content not to the GOT entry offset.
361     // That is why we use separate expression type.
362     return In<ELFT>::MipsGot->getVA() +
363            In<ELFT>::MipsGot->getBodyEntryOffset(Body, A) -
364            In<ELFT>::MipsGot->getGp();
365   case R_MIPS_GOTREL:
366     return Body.getVA<ELFT>(A) - In<ELFT>::MipsGot->getGp();
367   case R_MIPS_TLSGD:
368     return In<ELFT>::MipsGot->getVA() + In<ELFT>::MipsGot->getTlsOffset() +
369            In<ELFT>::MipsGot->getGlobalDynOffset(Body) -
370            In<ELFT>::MipsGot->getGp();
371   case R_MIPS_TLSLD:
372     return In<ELFT>::MipsGot->getVA() + In<ELFT>::MipsGot->getTlsOffset() +
373            In<ELFT>::MipsGot->getTlsIndexOff() - In<ELFT>::MipsGot->getGp();
374   case R_PPC_OPD: {
375     uint64_t SymVA = Body.getVA<ELFT>(A);
376     // If we have an undefined weak symbol, we might get here with a symbol
377     // address of zero. That could overflow, but the code must be unreachable,
378     // so don't bother doing anything at all.
379     if (!SymVA)
380       return 0;
381     if (Out<ELF64BE>::Opd) {
382       // If this is a local call, and we currently have the address of a
383       // function-descriptor, get the underlying code address instead.
384       uint64_t OpdStart = Out<ELF64BE>::Opd->Addr;
385       uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->Size;
386       bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd;
387       if (InOpd)
388         SymVA = read64be(&Out<ELF64BE>::OpdBuf[SymVA - OpdStart]);
389     }
390     return SymVA - P;
391   }
392   case R_PC:
393     if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak()) {
394       // On ARM and AArch64 a branch to an undefined weak resolves to the
395       // next instruction, otherwise the place.
396       if (Config->EMachine == EM_ARM)
397         return getARMUndefinedRelativeWeakVA(Type, A, P);
398       if (Config->EMachine == EM_AARCH64)
399         return getAArch64UndefinedRelativeWeakVA(Type, A, P);
400     }
401   case R_RELAX_GOT_PC:
402     return Body.getVA<ELFT>(A) - P;
403   case R_PLT_PAGE_PC:
404   case R_PAGE_PC:
405     if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak())
406       return getAArch64Page(A);
407     return getAArch64Page(Body.getVA<ELFT>(A)) - getAArch64Page(P);
408   }
409   llvm_unreachable("Invalid expression");
410 }
411 
412 // This function applies relocations to sections without SHF_ALLOC bit.
413 // Such sections are never mapped to memory at runtime. Debug sections are
414 // an example. Relocations in non-alloc sections are much easier to
415 // handle than in allocated sections because it will never need complex
416 // treatement such as GOT or PLT (because at runtime no one refers them).
417 // So, we handle relocations for non-alloc sections directly in this
418 // function as a performance optimization.
419 template <class ELFT>
420 template <class RelTy>
421 void InputSection<ELFT>::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
422   for (const RelTy &Rel : Rels) {
423     uint32_t Type = Rel.getType(Config->Mips64EL);
424     uintX_t Offset = this->getOffset(Rel.r_offset);
425     uint8_t *BufLoc = Buf + Offset;
426     uintX_t Addend = getAddend<ELFT>(Rel);
427     if (!RelTy::IsRela)
428       Addend += Target->getImplicitAddend(BufLoc, Type);
429 
430     SymbolBody &Sym = this->File->getRelocTargetSym(Rel);
431     if (Target->getRelExpr(Type, Sym) != R_ABS) {
432       error(this->getLocation(Offset) + ": has non-ABS reloc");
433       return;
434     }
435 
436     uintX_t AddrLoc = this->OutSec->Addr + Offset;
437     uint64_t SymVA = 0;
438     if (!Sym.isTls() || Out<ELFT>::TlsPhdr)
439       SymVA = SignExtend64<sizeof(uintX_t) * 8>(
440           getRelocTargetVA<ELFT>(Type, Addend, AddrLoc, Sym, R_ABS));
441     Target->relocateOne(BufLoc, Type, SymVA);
442   }
443 }
444 
445 template <class ELFT>
446 void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd) {
447   // scanReloc function in Writer.cpp constructs Relocations
448   // vector only for SHF_ALLOC'ed sections. For other sections,
449   // we handle relocations directly here.
450   auto *IS = dyn_cast<InputSection<ELFT>>(this);
451   if (IS && !(IS->Flags & SHF_ALLOC)) {
452     if (IS->AreRelocsRela)
453       IS->relocateNonAlloc(Buf, IS->relas());
454     else
455       IS->relocateNonAlloc(Buf, IS->rels());
456     return;
457   }
458 
459   const unsigned Bits = sizeof(uintX_t) * 8;
460   for (const Relocation &Rel : Relocations) {
461     uintX_t Offset = getOffset(Rel.Offset);
462     uint8_t *BufLoc = Buf + Offset;
463     uint32_t Type = Rel.Type;
464     uintX_t A = Rel.Addend;
465 
466     uintX_t AddrLoc = OutSec->Addr + Offset;
467     RelExpr Expr = Rel.Expr;
468     uint64_t TargetVA = SignExtend64<Bits>(
469         getRelocTargetVA<ELFT>(Type, A, AddrLoc, *Rel.Sym, Expr));
470 
471     switch (Expr) {
472     case R_RELAX_GOT_PC:
473     case R_RELAX_GOT_PC_NOPIC:
474       Target->relaxGot(BufLoc, TargetVA);
475       break;
476     case R_RELAX_TLS_IE_TO_LE:
477       Target->relaxTlsIeToLe(BufLoc, Type, TargetVA);
478       break;
479     case R_RELAX_TLS_LD_TO_LE:
480       Target->relaxTlsLdToLe(BufLoc, Type, TargetVA);
481       break;
482     case R_RELAX_TLS_GD_TO_LE:
483     case R_RELAX_TLS_GD_TO_LE_NEG:
484       Target->relaxTlsGdToLe(BufLoc, Type, TargetVA);
485       break;
486     case R_RELAX_TLS_GD_TO_IE:
487     case R_RELAX_TLS_GD_TO_IE_ABS:
488     case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
489     case R_RELAX_TLS_GD_TO_IE_END:
490       Target->relaxTlsGdToIe(BufLoc, Type, TargetVA);
491       break;
492     case R_PPC_PLT_OPD:
493       // Patch a nop (0x60000000) to a ld.
494       if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == 0x60000000)
495         write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1)
496     // fallthrough
497     default:
498       Target->relocateOne(BufLoc, Type, TargetVA);
499       break;
500     }
501   }
502 }
503 
504 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
505   if (this->Type == SHT_NOBITS)
506     return;
507 
508   if (auto *S = dyn_cast<SyntheticSection<ELFT>>(this)) {
509     S->writeTo(Buf + OutSecOff);
510     return;
511   }
512 
513   // If -r is given, then an InputSection may be a relocation section.
514   if (this->Type == SHT_RELA) {
515     copyRelocations(Buf + OutSecOff, this->template getDataAs<Elf_Rela>());
516     return;
517   }
518   if (this->Type == SHT_REL) {
519     copyRelocations(Buf + OutSecOff, this->template getDataAs<Elf_Rel>());
520     return;
521   }
522 
523   // Copy section contents from source object file to output file.
524   ArrayRef<uint8_t> Data = this->Data;
525   memcpy(Buf + OutSecOff, Data.data(), Data.size());
526 
527   // Iterate over all relocation sections that apply to this section.
528   uint8_t *BufEnd = Buf + OutSecOff + Data.size();
529   this->relocate(Buf, BufEnd);
530 }
531 
532 template <class ELFT>
533 void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
534   this->Alignment = std::max(this->Alignment, Other->Alignment);
535   Other->Repl = this->Repl;
536   Other->Live = false;
537 }
538 
539 template <class ELFT>
540 EhInputSection<ELFT>::EhInputSection(elf::ObjectFile<ELFT> *F,
541                                      const Elf_Shdr *Header, StringRef Name)
542     : InputSectionBase<ELFT>(F, Header, Name, InputSectionBase<ELFT>::EHFrame) {
543   // Mark .eh_frame sections as live by default because there are
544   // usually no relocations that point to .eh_frames. Otherwise,
545   // the garbage collector would drop all .eh_frame sections.
546   this->Live = true;
547 }
548 
549 template <class ELFT>
550 bool EhInputSection<ELFT>::classof(const InputSectionData *S) {
551   return S->kind() == InputSectionBase<ELFT>::EHFrame;
552 }
553 
554 // Returns the index of the first relocation that points to a region between
555 // Begin and Begin+Size.
556 template <class IntTy, class RelTy>
557 static unsigned getReloc(IntTy Begin, IntTy Size, const ArrayRef<RelTy> &Rels,
558                          unsigned &RelocI) {
559   // Start search from RelocI for fast access. That works because the
560   // relocations are sorted in .eh_frame.
561   for (unsigned N = Rels.size(); RelocI < N; ++RelocI) {
562     const RelTy &Rel = Rels[RelocI];
563     if (Rel.r_offset < Begin)
564       continue;
565 
566     if (Rel.r_offset < Begin + Size)
567       return RelocI;
568     return -1;
569   }
570   return -1;
571 }
572 
573 // .eh_frame is a sequence of CIE or FDE records.
574 // This function splits an input section into records and returns them.
575 template <class ELFT> void EhInputSection<ELFT>::split() {
576   // Early exit if already split.
577   if (!this->Pieces.empty())
578     return;
579 
580   if (this->NumRelocations) {
581     if (this->AreRelocsRela)
582       split(this->relas());
583     else
584       split(this->rels());
585     return;
586   }
587   split(makeArrayRef<typename ELFT::Rela>(nullptr, nullptr));
588 }
589 
590 template <class ELFT>
591 template <class RelTy>
592 void EhInputSection<ELFT>::split(ArrayRef<RelTy> Rels) {
593   ArrayRef<uint8_t> Data = this->Data;
594   unsigned RelI = 0;
595   for (size_t Off = 0, End = Data.size(); Off != End;) {
596     size_t Size = readEhRecordSize<ELFT>(this, Off);
597     this->Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI));
598     // The empty record is the end marker.
599     if (Size == 4)
600       break;
601     Off += Size;
602   }
603 }
604 
605 static size_t findNull(ArrayRef<uint8_t> A, size_t EntSize) {
606   // Optimize the common case.
607   StringRef S((const char *)A.data(), A.size());
608   if (EntSize == 1)
609     return S.find(0);
610 
611   for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
612     const char *B = S.begin() + I;
613     if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
614       return I;
615   }
616   return StringRef::npos;
617 }
618 
619 // Split SHF_STRINGS section. Such section is a sequence of
620 // null-terminated strings.
621 template <class ELFT>
622 void MergeInputSection<ELFT>::splitStrings(ArrayRef<uint8_t> Data,
623                                            size_t EntSize) {
624   size_t Off = 0;
625   bool IsAlloc = this->Flags & SHF_ALLOC;
626   while (!Data.empty()) {
627     size_t End = findNull(Data, EntSize);
628     if (End == StringRef::npos)
629       fatal(toString(this) + ": string is not null terminated");
630     size_t Size = End + EntSize;
631     Pieces.emplace_back(Off, !IsAlloc);
632     Hashes.push_back(hash_value(toStringRef(Data.slice(0, Size))));
633     Data = Data.slice(Size);
634     Off += Size;
635   }
636 }
637 
638 // Split non-SHF_STRINGS section. Such section is a sequence of
639 // fixed size records.
640 template <class ELFT>
641 void MergeInputSection<ELFT>::splitNonStrings(ArrayRef<uint8_t> Data,
642                                               size_t EntSize) {
643   size_t Size = Data.size();
644   assert((Size % EntSize) == 0);
645   bool IsAlloc = this->Flags & SHF_ALLOC;
646   for (unsigned I = 0, N = Size; I != N; I += EntSize) {
647     Hashes.push_back(hash_value(toStringRef(Data.slice(I, EntSize))));
648     Pieces.emplace_back(I, !IsAlloc);
649   }
650 }
651 
652 template <class ELFT>
653 MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
654                                            const Elf_Shdr *Header,
655                                            StringRef Name)
656     : InputSectionBase<ELFT>(F, Header, Name, InputSectionBase<ELFT>::Merge) {}
657 
658 // This function is called after we obtain a complete list of input sections
659 // that need to be linked. This is responsible to split section contents
660 // into small chunks for further processing.
661 //
662 // Note that this function is called from parallel_for_each. This must be
663 // thread-safe (i.e. no memory allocation from the pools).
664 template <class ELFT> void MergeInputSection<ELFT>::splitIntoPieces() {
665   ArrayRef<uint8_t> Data = this->Data;
666   uintX_t EntSize = this->Entsize;
667   if (this->Flags & SHF_STRINGS)
668     splitStrings(Data, EntSize);
669   else
670     splitNonStrings(Data, EntSize);
671 
672   if (Config->GcSections && (this->Flags & SHF_ALLOC))
673     for (uintX_t Off : LiveOffsets)
674       this->getSectionPiece(Off)->Live = true;
675 }
676 
677 template <class ELFT>
678 bool MergeInputSection<ELFT>::classof(const InputSectionData *S) {
679   return S->kind() == InputSectionBase<ELFT>::Merge;
680 }
681 
682 // Do binary search to get a section piece at a given input offset.
683 template <class ELFT>
684 SectionPiece *MergeInputSection<ELFT>::getSectionPiece(uintX_t Offset) {
685   auto *This = static_cast<const MergeInputSection<ELFT> *>(this);
686   return const_cast<SectionPiece *>(This->getSectionPiece(Offset));
687 }
688 
689 template <class It, class T, class Compare>
690 static It fastUpperBound(It First, It Last, const T &Value, Compare Comp) {
691   size_t Size = std::distance(First, Last);
692   assert(Size != 0);
693   while (Size != 1) {
694     size_t H = Size / 2;
695     const It MI = First + H;
696     Size -= H;
697     First = Comp(Value, *MI) ? First : First + H;
698   }
699   return Comp(Value, *First) ? First : First + 1;
700 }
701 
702 template <class ELFT>
703 const SectionPiece *
704 MergeInputSection<ELFT>::getSectionPiece(uintX_t Offset) const {
705   uintX_t Size = this->Data.size();
706   if (Offset >= Size)
707     fatal(toString(this) + ": entry is past the end of the section");
708 
709   // Find the element this offset points to.
710   auto I = fastUpperBound(
711       Pieces.begin(), Pieces.end(), Offset,
712       [](const uintX_t &A, const SectionPiece &B) { return A < B.InputOff; });
713   --I;
714   return &*I;
715 }
716 
717 // Returns the offset in an output section for a given input offset.
718 // Because contents of a mergeable section is not contiguous in output,
719 // it is not just an addition to a base output offset.
720 template <class ELFT>
721 typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) const {
722   // Initialize OffsetMap lazily.
723   std::call_once(InitOffsetMap, [&] {
724     OffsetMap.reserve(Pieces.size());
725     for (const SectionPiece &Piece : Pieces)
726       OffsetMap[Piece.InputOff] = Piece.OutputOff;
727   });
728 
729   // Find a string starting at a given offset.
730   auto It = OffsetMap.find(Offset);
731   if (It != OffsetMap.end())
732     return It->second;
733 
734   if (!this->Live)
735     return 0;
736 
737   // If Offset is not at beginning of a section piece, it is not in the map.
738   // In that case we need to search from the original section piece vector.
739   const SectionPiece &Piece = *this->getSectionPiece(Offset);
740   if (!Piece.Live)
741     return 0;
742 
743   uintX_t Addend = Offset - Piece.InputOff;
744   return Piece.OutputOff + Addend;
745 }
746 
747 template class elf::InputSectionBase<ELF32LE>;
748 template class elf::InputSectionBase<ELF32BE>;
749 template class elf::InputSectionBase<ELF64LE>;
750 template class elf::InputSectionBase<ELF64BE>;
751 
752 template class elf::InputSection<ELF32LE>;
753 template class elf::InputSection<ELF32BE>;
754 template class elf::InputSection<ELF64LE>;
755 template class elf::InputSection<ELF64BE>;
756 
757 template class elf::EhInputSection<ELF32LE>;
758 template class elf::EhInputSection<ELF32BE>;
759 template class elf::EhInputSection<ELF64LE>;
760 template class elf::EhInputSection<ELF64BE>;
761 
762 template class elf::MergeInputSection<ELF32LE>;
763 template class elf::MergeInputSection<ELF32BE>;
764 template class elf::MergeInputSection<ELF64LE>;
765 template class elf::MergeInputSection<ELF64BE>;
766 
767 template std::string lld::toString(const InputSectionBase<ELF32LE> *);
768 template std::string lld::toString(const InputSectionBase<ELF32BE> *);
769 template std::string lld::toString(const InputSectionBase<ELF64LE> *);
770 template std::string lld::toString(const InputSectionBase<ELF64BE> *);
771