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