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