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 "OutputSections.h"
16 #include "Target.h"
17 
18 #include "llvm/Support/Endian.h"
19 
20 using namespace llvm;
21 using namespace llvm::ELF;
22 using namespace llvm::object;
23 using namespace llvm::support::endian;
24 
25 using namespace lld;
26 using namespace lld::elf;
27 
28 template <class ELFT>
29 InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
30                                          const Elf_Shdr *Header,
31                                          Kind SectionKind)
32     : Header(Header), File(File), SectionKind(SectionKind), Repl(this) {
33   // The garbage collector sets sections' Live bits.
34   // If GC is disabled, all sections are considered live by default.
35   Live = !Config->GcSections;
36 
37   // The ELF spec states that a value of 0 means the section has
38   // no alignment constraits.
39   Align = std::max<uintX_t>(Header->sh_addralign, 1);
40 }
41 
42 template <class ELFT> size_t InputSectionBase<ELFT>::getSize() const {
43   if (auto *D = dyn_cast<InputSection<ELFT>>(this))
44     if (D->getThunksSize() > 0)
45       return D->getThunkOff() + D->getThunksSize();
46   return Header->sh_size;
47 }
48 
49 template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
50   return check(File->getObj().getSectionName(this->Header));
51 }
52 
53 template <class ELFT>
54 ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
55   return check(this->File->getObj().getSectionContents(this->Header));
56 }
57 
58 template <class ELFT>
59 typename ELFT::uint InputSectionBase<ELFT>::getOffset(uintX_t Offset) {
60   switch (SectionKind) {
61   case Regular:
62     return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
63   case EHFrame:
64     return cast<EhInputSection<ELFT>>(this)->getOffset(Offset);
65   case Merge:
66     return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
67   case MipsReginfo:
68   case MipsOptions:
69     // MIPS .reginfo and .MIPS.options sections are consumed by the linker,
70     // and the linker produces a single output section. It is possible that
71     // input files contain section symbol points to the corresponding input
72     // section. Redirect it to the produced output section.
73     if (Offset != 0)
74       fatal("Unsupported reference to the middle of '" + getSectionName() +
75             "' section");
76     return this->OutSec->getVA();
77   }
78   llvm_unreachable("invalid section kind");
79 }
80 
81 template <class ELFT>
82 typename ELFT::uint
83 InputSectionBase<ELFT>::getOffset(const DefinedRegular<ELFT> &Sym) {
84   return getOffset(Sym.Value);
85 }
86 
87 template <class ELFT>
88 InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F,
89                                  const Elf_Shdr *Header)
90     : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
91 
92 template <class ELFT>
93 bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
94   return S->SectionKind == Base::Regular;
95 }
96 
97 template <class ELFT>
98 InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() {
99   assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL);
100   ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections();
101   return Sections[this->Header->sh_info];
102 }
103 
104 template <class ELFT> void InputSection<ELFT>::addThunk(SymbolBody &Body) {
105   Body.ThunkIndex = Thunks.size();
106   Thunks.push_back(&Body);
107 }
108 
109 template <class ELFT> uint64_t InputSection<ELFT>::getThunkOff() const {
110   return this->Header->sh_size;
111 }
112 
113 template <class ELFT> uint64_t InputSection<ELFT>::getThunksSize() const {
114   return Thunks.size() * Target->ThunkSize;
115 }
116 
117 // This is used for -r. We can't use memcpy to copy relocations because we need
118 // to update symbol table offset and section index for each relocation. So we
119 // copy relocations one by one.
120 template <class ELFT>
121 template <class RelTy>
122 void InputSection<ELFT>::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) {
123   InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
124 
125   for (const RelTy &Rel : Rels) {
126     uint32_t Type = Rel.getType(Config->Mips64EL);
127     SymbolBody &Body = this->File->getRelocTargetSym(Rel);
128 
129     RelTy *P = reinterpret_cast<RelTy *>(Buf);
130     Buf += sizeof(RelTy);
131 
132     P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
133     P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL);
134   }
135 }
136 
137 // Page(Expr) is the page address of the expression Expr, defined
138 // as (Expr & ~0xFFF). (This applies even if the machine page size
139 // supported by the platform has a different value.)
140 static uint64_t getAArch64Page(uint64_t Expr) {
141   return Expr & (~static_cast<uint64_t>(0xFFF));
142 }
143 
144 // For computing values, each R_RELAX_TLS_* corresponds to whatever expression
145 // the target uses in the mode this is being relaxed into. For example, anything
146 // that relaxes to LE just needs an R_TLS since that is what is used if we
147 // had a local exec expression to begin with.
148 static RelExpr getRelaxedExpr(RelExpr Expr) {
149   switch (Expr) {
150   default:
151     return Expr;
152   case R_RELAX_TLS_GD_TO_LE:
153     if (Config->EMachine == EM_386)
154       return R_NEG_TLS;
155     return R_TLS;
156   case R_RELAX_TLS_GD_TO_IE:
157     if (Config->EMachine == EM_386)
158       return R_GOT_FROM_END;
159     return R_GOT_PC;
160   case R_RELAX_TLS_IE_TO_LE:
161   case R_RELAX_TLS_LD_TO_LE:
162     return R_TLS;
163   }
164 }
165 
166 template <class ELFT>
167 static typename ELFT::uint
168 getSymVA(uint32_t Type, typename ELFT::uint A, typename ELFT::uint P,
169          const SymbolBody &Body, uint8_t *BufLoc,
170          const elf::ObjectFile<ELFT> &File, RelExpr Expr) {
171   typedef typename ELFT::uint uintX_t;
172   Expr = getRelaxedExpr(Expr);
173 
174   switch (Expr) {
175   case R_HINT:
176     llvm_unreachable("cannot relocate hint relocs");
177   case R_RELAX_TLS_GD_TO_LE:
178   case R_RELAX_TLS_GD_TO_IE:
179   case R_RELAX_TLS_IE_TO_LE:
180   case R_RELAX_TLS_LD_TO_LE:
181     llvm_unreachable("Should have been mapped");
182   case R_TLSLD:
183     return Out<ELFT>::Got->getTlsIndexOff() + A -
184            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
185   case R_TLSLD_PC:
186     return Out<ELFT>::Got->getTlsIndexVA() + A - P;
187   case R_THUNK:
188     return Body.getThunkVA<ELFT>();
189   case R_PPC_TOC:
190     return getPPC64TocBase() + A;
191   case R_TLSGD:
192     return Out<ELFT>::Got->getGlobalDynOffset(Body) + A -
193            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
194   case R_TLSGD_PC:
195     return Out<ELFT>::Got->getGlobalDynAddr(Body) + A - P;
196   case R_TLSDESC:
197     return Out<ELFT>::Got->getGlobalDynAddr(Body) + A;
198   case R_TLSDESC_PAGE:
199     return getAArch64Page(Out<ELFT>::Got->getGlobalDynAddr(Body) + A) -
200            getAArch64Page(P);
201   case R_PLT:
202     return Body.getPltVA<ELFT>() + A;
203   case R_PLT_PC:
204   case R_PPC_PLT_OPD:
205     return Body.getPltVA<ELFT>() + A - P;
206   case R_SIZE:
207     return Body.getSize<ELFT>() + A;
208   case R_GOTREL:
209     return Body.getVA<ELFT>(A) - Out<ELFT>::Got->getVA();
210   case R_GOT_FROM_END:
211     return Body.getGotOffset<ELFT>() + A -
212            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
213   case R_GOT:
214     return Body.getGotVA<ELFT>() + A;
215   case R_GOT_PAGE_PC:
216     return getAArch64Page(Body.getGotVA<ELFT>() + A) - getAArch64Page(P);
217   case R_GOT_PC:
218     return Body.getGotVA<ELFT>() + A - P;
219   case R_GOTONLY_PC:
220     return Out<ELFT>::Got->getVA() + A - P;
221   case R_TLS:
222     if (Target->TcbSize)
223       return Body.getVA<ELFT>(A) +
224              alignTo(Target->TcbSize, Out<ELFT>::TlsPhdr->p_align);
225     return Body.getVA<ELFT>(A) - Out<ELFT>::TlsPhdr->p_memsz;
226   case R_NEG_TLS:
227     return Out<ELF32LE>::TlsPhdr->p_memsz - Body.getVA<ELFT>(A);
228   case R_ABS:
229   case R_RELAX_GOT_PC_NOPIC:
230     return Body.getVA<ELFT>(A);
231   case R_GOT_OFF:
232     return Body.getGotOffset<ELFT>() + A;
233   case R_MIPS_GOT_LOCAL_PAGE:
234     // If relocation against MIPS local symbol requires GOT entry, this entry
235     // should be initialized by 'page address'. This address is high 16-bits
236     // of sum the symbol's value and the addend.
237     return Out<ELFT>::Got->getMipsLocalPageOffset(Body.getVA<ELFT>(A));
238   case R_MIPS_GOT_LOCAL:
239     // For non-local symbols GOT entries should contain their full
240     // addresses. But if such symbol cannot be preempted, we do not
241     // have to put them into the "global" part of GOT and use dynamic
242     // linker to determine their actual addresses. That is why we
243     // create GOT entries for them in the "local" part of GOT.
244     return Out<ELFT>::Got->getMipsLocalEntryOffset(Body.getVA<ELFT>(A));
245   case R_PPC_OPD: {
246     uint64_t SymVA = Body.getVA<ELFT>(A);
247     // If we have an undefined weak symbol, we might get here with a symbol
248     // address of zero. That could overflow, but the code must be unreachable,
249     // so don't bother doing anything at all.
250     if (!SymVA)
251       return 0;
252     if (Out<ELF64BE>::Opd) {
253       // If this is a local call, and we currently have the address of a
254       // function-descriptor, get the underlying code address instead.
255       uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
256       uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
257       bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd;
258       if (InOpd)
259         SymVA = read64be(&Out<ELF64BE>::OpdBuf[SymVA - OpdStart]);
260     }
261     return SymVA - P;
262   }
263   case R_PC:
264   case R_RELAX_GOT_PC:
265     return Body.getVA<ELFT>(A) - P;
266   case R_PAGE_PC:
267     return getAArch64Page(Body.getVA<ELFT>(A)) - getAArch64Page(P);
268   }
269   llvm_unreachable("Invalid expression");
270 }
271 
272 // This function applies relocations to sections without SHF_ALLOC bit.
273 // Such sections are never mapped to memory at runtime. Debug sections are
274 // an example. Relocations in non-alloc sections are much easier to
275 // handle than in allocated sections because it will never need complex
276 // treatement such as GOT or PLT (because at runtime no one refers them).
277 // So, we handle relocations for non-alloc sections directly in this
278 // function as a performance optimization.
279 template <class ELFT>
280 template <class RelTy>
281 void InputSection<ELFT>::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
282   const unsigned Bits = sizeof(uintX_t) * 8;
283   for (const RelTy &Rel : Rels) {
284     uint32_t Type = Rel.getType(Config->Mips64EL);
285     uintX_t Offset = this->getOffset(Rel.r_offset);
286     uint8_t *BufLoc = Buf + Offset;
287     uintX_t Addend = getAddend<ELFT>(Rel);
288     if (!RelTy::IsRela)
289       Addend += Target->getImplicitAddend(BufLoc, Type);
290 
291     SymbolBody &Sym = this->File->getRelocTargetSym(Rel);
292     if (Target->getRelExpr(Type, Sym) != R_ABS) {
293       error(this->getSectionName() + " has non-ABS reloc");
294       return;
295     }
296 
297     uintX_t AddrLoc = this->OutSec->getVA() + Offset;
298     uint64_t SymVA = SignExtend64<Bits>(getSymVA<ELFT>(
299         Type, Addend, AddrLoc, Sym, BufLoc, *this->File, R_ABS));
300     Target->relocateOne(BufLoc, Type, SymVA);
301   }
302 }
303 
304 template <class ELFT>
305 void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd) {
306   // scanReloc function in Writer.cpp constructs Relocations
307   // vector only for SHF_ALLOC'ed sections. For other sections,
308   // we handle relocations directly here.
309   auto *IS = dyn_cast<InputSection<ELFT>>(this);
310   if (IS && !(IS->Header->sh_flags & SHF_ALLOC)) {
311     for (const Elf_Shdr *RelSec : IS->RelocSections) {
312       if (RelSec->sh_type == SHT_RELA)
313         IS->relocateNonAlloc(Buf, IS->File->getObj().relas(RelSec));
314       else
315         IS->relocateNonAlloc(Buf, IS->File->getObj().rels(RelSec));
316     }
317     return;
318   }
319 
320   const unsigned Bits = sizeof(uintX_t) * 8;
321   for (const Relocation &Rel : Relocations) {
322     uintX_t Offset = Rel.Offset;
323     uint8_t *BufLoc = Buf + Offset;
324     uint32_t Type = Rel.Type;
325     uintX_t A = Rel.Addend;
326 
327     uintX_t AddrLoc = OutSec->getVA() + Offset;
328     RelExpr Expr = Rel.Expr;
329     uint64_t SymVA = SignExtend64<Bits>(
330         getSymVA<ELFT>(Type, A, AddrLoc, *Rel.Sym, BufLoc, *File, Expr));
331 
332     switch (Expr) {
333     case R_RELAX_GOT_PC:
334     case R_RELAX_GOT_PC_NOPIC:
335       Target->relaxGot(BufLoc, SymVA);
336       break;
337     case R_RELAX_TLS_IE_TO_LE:
338       Target->relaxTlsIeToLe(BufLoc, Type, SymVA);
339       break;
340     case R_RELAX_TLS_LD_TO_LE:
341       Target->relaxTlsLdToLe(BufLoc, Type, SymVA);
342       break;
343     case R_RELAX_TLS_GD_TO_LE:
344       Target->relaxTlsGdToLe(BufLoc, Type, SymVA);
345       break;
346     case R_RELAX_TLS_GD_TO_IE:
347       Target->relaxTlsGdToIe(BufLoc, Type, SymVA);
348       break;
349     case R_PPC_PLT_OPD:
350       // Patch a nop (0x60000000) to a ld.
351       if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == 0x60000000)
352         write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1)
353       // fallthrough
354     default:
355       Target->relocateOne(BufLoc, Type, SymVA);
356       break;
357     }
358   }
359 }
360 
361 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
362   if (this->Header->sh_type == SHT_NOBITS)
363     return;
364   ELFFile<ELFT> &EObj = this->File->getObj();
365 
366   // If -r is given, then an InputSection may be a relocation section.
367   if (this->Header->sh_type == SHT_RELA) {
368     copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
369     return;
370   }
371   if (this->Header->sh_type == SHT_REL) {
372     copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
373     return;
374   }
375 
376   // Copy section contents from source object file to output file.
377   ArrayRef<uint8_t> Data = this->getSectionData();
378   memcpy(Buf + OutSecOff, Data.data(), Data.size());
379 
380   // Iterate over all relocation sections that apply to this section.
381   uint8_t *BufEnd = Buf + OutSecOff + Data.size();
382   this->relocate(Buf, BufEnd);
383 
384   // The section might have a data/code generated by the linker and need
385   // to be written after the section. Usually these are thunks - small piece
386   // of code used to jump between "incompatible" functions like PIC and non-PIC
387   // or if the jump target too far and its address does not fit to the short
388   // jump istruction.
389   if (!Thunks.empty()) {
390     Buf += OutSecOff + getThunkOff();
391     for (const SymbolBody *S : Thunks) {
392       Target->writeThunk(Buf, S->getVA<ELFT>());
393       Buf += Target->ThunkSize;
394     }
395   }
396 }
397 
398 template <class ELFT>
399 void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
400   this->Align = std::max(this->Align, Other->Align);
401   Other->Repl = this->Repl;
402   Other->Live = false;
403 }
404 
405 template <class ELFT>
406 SplitInputSection<ELFT>::SplitInputSection(
407     elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
408     typename InputSectionBase<ELFT>::Kind SectionKind)
409     : InputSectionBase<ELFT>(File, Header, SectionKind) {}
410 
411 template <class ELFT>
412 EhInputSection<ELFT>::EhInputSection(elf::ObjectFile<ELFT> *F,
413                                      const Elf_Shdr *Header)
414     : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
415   // Mark .eh_frame sections as live by default because there are
416   // usually no relocations that point to .eh_frames. Otherwise,
417   // the garbage collector would drop all .eh_frame sections.
418   this->Live = true;
419 }
420 
421 template <class ELFT>
422 bool EhInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
423   return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
424 }
425 
426 // .eh_frame is a sequence of CIE or FDE records.
427 // This function splits an input section into records and returns them.
428 template <class ELFT>
429 void EhInputSection<ELFT>::split() {
430   ArrayRef<uint8_t> Data = this->getSectionData();
431   for (size_t Off = 0, End = Data.size(); Off != End;) {
432     size_t Size = readEhRecordSize<ELFT>(Data.slice(Off));
433     this->Pieces.emplace_back(Off, Data.slice(Off, Size));
434     // The empty record is the end marker.
435     if (Size == 4)
436       break;
437     Off += Size;
438   }
439 }
440 
441 template <class ELFT>
442 typename ELFT::uint EhInputSection<ELFT>::getOffset(uintX_t Offset) {
443   // The file crtbeginT.o has relocations pointing to the start of an empty
444   // .eh_frame that is known to be the first in the link. It does that to
445   // identify the start of the output .eh_frame. Handle this special case.
446   if (this->getSectionHdr()->sh_size == 0)
447     return Offset;
448   SectionPiece *Piece = this->getSectionPiece(Offset);
449   if (Piece->OutputOff == size_t(-1))
450     return -1; // Not in the output
451 
452   uintX_t Addend = Offset - Piece->InputOff;
453   return Piece->OutputOff + Addend;
454 }
455 
456 static size_t findNull(ArrayRef<uint8_t> A, size_t EntSize) {
457   // Optimize the common case.
458   StringRef S((const char *)A.data(), A.size());
459   if (EntSize == 1)
460     return S.find(0);
461 
462   for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
463     const char *B = S.begin() + I;
464     if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
465       return I;
466   }
467   return StringRef::npos;
468 }
469 
470 // Split SHF_STRINGS section. Such section is a sequence of
471 // null-terminated strings.
472 static std::vector<SectionPiece> splitStrings(ArrayRef<uint8_t> Data,
473                                               size_t EntSize) {
474   std::vector<SectionPiece> V;
475   size_t Off = 0;
476   while (!Data.empty()) {
477     size_t End = findNull(Data, EntSize);
478     if (End == StringRef::npos)
479       fatal("string is not null terminated");
480     size_t Size = End + EntSize;
481     V.emplace_back(Off, Data.slice(0, Size));
482     Data = Data.slice(Size);
483     Off += Size;
484   }
485   return V;
486 }
487 
488 // Split non-SHF_STRINGS section. Such section is a sequence of
489 // fixed size records.
490 static std::vector<SectionPiece> splitNonStrings(ArrayRef<uint8_t> Data,
491                                                  size_t EntSize) {
492   std::vector<SectionPiece> V;
493   size_t Size = Data.size();
494   assert((Size % EntSize) == 0);
495   for (unsigned I = 0, N = Size; I != N; I += EntSize)
496     V.emplace_back(I, Data.slice(I, EntSize));
497   return V;
498 }
499 
500 template <class ELFT>
501 MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
502                                            const Elf_Shdr *Header)
503     : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
504 
505 template <class ELFT> void MergeInputSection<ELFT>::splitIntoPieces() {
506   ArrayRef<uint8_t> Data = this->getSectionData();
507   uintX_t EntSize = this->Header->sh_entsize;
508   if (this->Header->sh_flags & SHF_STRINGS)
509     this->Pieces = splitStrings(Data, EntSize);
510   else
511     this->Pieces = splitNonStrings(Data, EntSize);
512 
513   if (Config->GcSections)
514     for (uintX_t Off : LiveOffsets)
515       this->getSectionPiece(Off)->Live = true;
516 }
517 
518 template <class ELFT>
519 bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
520   return S->SectionKind == InputSectionBase<ELFT>::Merge;
521 }
522 
523 // Do binary search to get a section piece at a given input offset.
524 template <class ELFT>
525 SectionPiece *SplitInputSection<ELFT>::getSectionPiece(uintX_t Offset) {
526   ArrayRef<uint8_t> D = this->getSectionData();
527   StringRef Data((const char *)D.data(), D.size());
528   uintX_t Size = Data.size();
529   if (Offset >= Size)
530     fatal("entry is past the end of the section");
531 
532   // Find the element this offset points to.
533   auto I = std::upper_bound(
534       Pieces.begin(), Pieces.end(), Offset,
535       [](const uintX_t &A, const SectionPiece &B) { return A < B.InputOff; });
536   --I;
537   return &*I;
538 }
539 
540 // Returns the offset in an output section for a given input offset.
541 // Because contents of a mergeable section is not contiguous in output,
542 // it is not just an addition to a base output offset.
543 template <class ELFT>
544 typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
545   auto It = OffsetMap.find(Offset);
546   if (It != OffsetMap.end())
547     return It->second;
548 
549   // If Offset is not at beginning of a section piece, it is not in the map.
550   // In that case we need to search from the original section piece vector.
551   SectionPiece &Piece = *this->getSectionPiece(Offset);
552   assert(Piece.Live);
553   uintX_t Addend = Offset - Piece.InputOff;
554   return Piece.OutputOff + Addend;
555 }
556 
557 // Create a map from input offsets to output offsets for all section pieces.
558 // It is called after finalize().
559 template <class ELFT> void  MergeInputSection<ELFT>::finalizePieces() {
560   OffsetMap.grow(this->Pieces.size());
561   for (SectionPiece &Piece : this->Pieces) {
562     if (!Piece.Live)
563       continue;
564     if (Piece.OutputOff == size_t(-1)) {
565       // Offsets of tail-merged strings are computed lazily.
566       auto *OutSec = static_cast<MergeOutputSection<ELFT> *>(this->OutSec);
567       ArrayRef<uint8_t> D = Piece.data();
568       StringRef S((const char *)D.data(), D.size());
569       Piece.OutputOff = OutSec->getOffset(S);
570     }
571     OffsetMap[Piece.InputOff] = Piece.OutputOff;
572   }
573 }
574 
575 template <class ELFT>
576 MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
577                                                        const Elf_Shdr *Hdr)
578     : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
579   // Initialize this->Reginfo.
580   ArrayRef<uint8_t> D = this->getSectionData();
581   if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) {
582     error("invalid size of .reginfo section");
583     return;
584   }
585   Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
586 }
587 
588 template <class ELFT>
589 bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
590   return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
591 }
592 
593 template <class ELFT>
594 MipsOptionsInputSection<ELFT>::MipsOptionsInputSection(elf::ObjectFile<ELFT> *F,
595                                                        const Elf_Shdr *Hdr)
596     : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsOptions) {
597   // Find ODK_REGINFO option in the section's content.
598   ArrayRef<uint8_t> D = this->getSectionData();
599   while (!D.empty()) {
600     if (D.size() < sizeof(Elf_Mips_Options<ELFT>)) {
601       error("invalid size of .MIPS.options section");
602       break;
603     }
604     auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(D.data());
605     if (O->kind == ODK_REGINFO) {
606       Reginfo = &O->getRegInfo();
607       break;
608     }
609     D = D.slice(O->size);
610   }
611 }
612 
613 template <class ELFT>
614 bool MipsOptionsInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
615   return S->SectionKind == InputSectionBase<ELFT>::MipsOptions;
616 }
617 
618 template class elf::InputSectionBase<ELF32LE>;
619 template class elf::InputSectionBase<ELF32BE>;
620 template class elf::InputSectionBase<ELF64LE>;
621 template class elf::InputSectionBase<ELF64BE>;
622 
623 template class elf::InputSection<ELF32LE>;
624 template class elf::InputSection<ELF32BE>;
625 template class elf::InputSection<ELF64LE>;
626 template class elf::InputSection<ELF64BE>;
627 
628 template class elf::SplitInputSection<ELF32LE>;
629 template class elf::SplitInputSection<ELF32BE>;
630 template class elf::SplitInputSection<ELF64LE>;
631 template class elf::SplitInputSection<ELF64BE>;
632 
633 template class elf::EhInputSection<ELF32LE>;
634 template class elf::EhInputSection<ELF32BE>;
635 template class elf::EhInputSection<ELF64LE>;
636 template class elf::EhInputSection<ELF64BE>;
637 
638 template class elf::MergeInputSection<ELF32LE>;
639 template class elf::MergeInputSection<ELF32BE>;
640 template class elf::MergeInputSection<ELF64LE>;
641 template class elf::MergeInputSection<ELF64BE>;
642 
643 template class elf::MipsReginfoInputSection<ELF32LE>;
644 template class elf::MipsReginfoInputSection<ELF32BE>;
645 template class elf::MipsReginfoInputSection<ELF64LE>;
646 template class elf::MipsReginfoInputSection<ELF64BE>;
647 
648 template class elf::MipsOptionsInputSection<ELF32LE>;
649 template class elf::MipsOptionsInputSection<ELF32BE>;
650 template class elf::MipsOptionsInputSection<ELF64LE>;
651 template class elf::MipsOptionsInputSection<ELF64BE>;
652