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