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