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   Alignment = 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 template <class ELFT>
145 static typename ELFT::uint
146 getSymVA(uint32_t Type, typename ELFT::uint A, typename ELFT::uint P,
147          const SymbolBody &Body, uint8_t *BufLoc,
148          const elf::ObjectFile<ELFT> &File, RelExpr Expr) {
149   typedef typename ELFT::uint uintX_t;
150 
151   switch (Expr) {
152   case R_HINT:
153     llvm_unreachable("cannot relocate hint relocs");
154   case R_TLSLD:
155     return Out<ELFT>::Got->getTlsIndexOff() + A -
156            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
157   case R_TLSLD_PC:
158     return Out<ELFT>::Got->getTlsIndexVA() + A - P;
159   case R_THUNK:
160     return Body.getThunkVA<ELFT>();
161   case R_PPC_TOC:
162     return getPPC64TocBase() + A;
163   case R_TLSGD:
164     return Out<ELFT>::Got->getGlobalDynOffset(Body) + A -
165            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
166   case R_TLSGD_PC:
167     return Out<ELFT>::Got->getGlobalDynAddr(Body) + A - P;
168   case R_TLSDESC:
169     return Out<ELFT>::Got->getGlobalDynAddr(Body) + A;
170   case R_TLSDESC_PAGE:
171     return getAArch64Page(Out<ELFT>::Got->getGlobalDynAddr(Body) + A) -
172            getAArch64Page(P);
173   case R_PLT:
174     return Body.getPltVA<ELFT>() + A;
175   case R_PLT_PC:
176   case R_PPC_PLT_OPD:
177     return Body.getPltVA<ELFT>() + A - P;
178   case R_SIZE:
179     return Body.getSize<ELFT>() + A;
180   case R_GOTREL:
181     return Body.getVA<ELFT>(A) - Out<ELFT>::Got->getVA();
182   case R_RELAX_TLS_GD_TO_IE_END:
183   case R_GOT_FROM_END:
184     return Body.getGotOffset<ELFT>() + A -
185            Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t);
186   case R_RELAX_TLS_GD_TO_IE_ABS:
187   case R_GOT:
188     return Body.getGotVA<ELFT>() + A;
189   case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
190   case R_GOT_PAGE_PC:
191     return getAArch64Page(Body.getGotVA<ELFT>() + A) - getAArch64Page(P);
192   case R_RELAX_TLS_GD_TO_IE:
193   case R_GOT_PC:
194     return Body.getGotVA<ELFT>() + A - P;
195   case R_GOTONLY_PC:
196     return Out<ELFT>::Got->getVA() + A - P;
197   case R_RELAX_TLS_LD_TO_LE:
198   case R_RELAX_TLS_IE_TO_LE:
199   case R_RELAX_TLS_GD_TO_LE:
200   case R_TLS:
201     if (Target->TcbSize)
202       return Body.getVA<ELFT>(A) +
203              alignTo(Target->TcbSize, Out<ELFT>::TlsPhdr->p_align);
204     return Body.getVA<ELFT>(A) - Out<ELFT>::TlsPhdr->p_memsz;
205   case R_RELAX_TLS_GD_TO_LE_NEG:
206   case R_NEG_TLS:
207     return Out<ELF32LE>::TlsPhdr->p_memsz - Body.getVA<ELFT>(A);
208   case R_ABS:
209   case R_RELAX_GOT_PC_NOPIC:
210     return Body.getVA<ELFT>(A);
211   case R_GOT_OFF:
212     return Body.getGotOffset<ELFT>() + A;
213   case R_MIPS_GOT_LOCAL_PAGE:
214     // If relocation against MIPS local symbol requires GOT entry, this entry
215     // should be initialized by 'page address'. This address is high 16-bits
216     // of sum the symbol's value and the addend.
217     return Out<ELFT>::Got->getMipsLocalPageOffset(Body.getVA<ELFT>(A));
218   case R_MIPS_GOT_OFF:
219     // In case of MIPS if a GOT relocation has non-zero addend this addend
220     // should be applied to the GOT entry content not to the GOT entry offset.
221     // That is why we use separate expression type.
222     return Out<ELFT>::Got->getMipsGotOffset(Body, A);
223   case R_PPC_OPD: {
224     uint64_t SymVA = Body.getVA<ELFT>(A);
225     // If we have an undefined weak symbol, we might get here with a symbol
226     // address of zero. That could overflow, but the code must be unreachable,
227     // so don't bother doing anything at all.
228     if (!SymVA)
229       return 0;
230     if (Out<ELF64BE>::Opd) {
231       // If this is a local call, and we currently have the address of a
232       // function-descriptor, get the underlying code address instead.
233       uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
234       uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
235       bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd;
236       if (InOpd)
237         SymVA = read64be(&Out<ELF64BE>::OpdBuf[SymVA - OpdStart]);
238     }
239     return SymVA - P;
240   }
241   case R_PC:
242   case R_RELAX_GOT_PC:
243     return Body.getVA<ELFT>(A) - P;
244   case R_PLT_PAGE_PC:
245   case R_PAGE_PC:
246     return getAArch64Page(Body.getVA<ELFT>(A)) - getAArch64Page(P);
247   }
248   llvm_unreachable("Invalid expression");
249 }
250 
251 // This function applies relocations to sections without SHF_ALLOC bit.
252 // Such sections are never mapped to memory at runtime. Debug sections are
253 // an example. Relocations in non-alloc sections are much easier to
254 // handle than in allocated sections because it will never need complex
255 // treatement such as GOT or PLT (because at runtime no one refers them).
256 // So, we handle relocations for non-alloc sections directly in this
257 // function as a performance optimization.
258 template <class ELFT>
259 template <class RelTy>
260 void InputSection<ELFT>::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) {
261   const unsigned Bits = sizeof(uintX_t) * 8;
262   for (const RelTy &Rel : Rels) {
263     uint32_t Type = Rel.getType(Config->Mips64EL);
264     uintX_t Offset = this->getOffset(Rel.r_offset);
265     uint8_t *BufLoc = Buf + Offset;
266     uintX_t Addend = getAddend<ELFT>(Rel);
267     if (!RelTy::IsRela)
268       Addend += Target->getImplicitAddend(BufLoc, Type);
269 
270     SymbolBody &Sym = this->File->getRelocTargetSym(Rel);
271     if (Target->getRelExpr(Type, Sym) != R_ABS) {
272       error(this->getSectionName() + " has non-ABS reloc");
273       return;
274     }
275 
276     uintX_t AddrLoc = this->OutSec->getVA() + Offset;
277     uint64_t SymVA = SignExtend64<Bits>(getSymVA<ELFT>(
278         Type, Addend, AddrLoc, Sym, BufLoc, *this->File, R_ABS));
279     Target->relocateOne(BufLoc, Type, SymVA);
280   }
281 }
282 
283 template <class ELFT>
284 void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd) {
285   // scanReloc function in Writer.cpp constructs Relocations
286   // vector only for SHF_ALLOC'ed sections. For other sections,
287   // we handle relocations directly here.
288   auto *IS = dyn_cast<InputSection<ELFT>>(this);
289   if (IS && !(IS->Header->sh_flags & SHF_ALLOC)) {
290     for (const Elf_Shdr *RelSec : IS->RelocSections) {
291       if (RelSec->sh_type == SHT_RELA)
292         IS->relocateNonAlloc(Buf, IS->File->getObj().relas(RelSec));
293       else
294         IS->relocateNonAlloc(Buf, IS->File->getObj().rels(RelSec));
295     }
296     return;
297   }
298 
299   const unsigned Bits = sizeof(uintX_t) * 8;
300   for (const Relocation &Rel : Relocations) {
301     uintX_t Offset = Rel.Offset;
302     uint8_t *BufLoc = Buf + Offset;
303     uint32_t Type = Rel.Type;
304     uintX_t A = Rel.Addend;
305 
306     uintX_t AddrLoc = OutSec->getVA() + Offset;
307     RelExpr Expr = Rel.Expr;
308     uint64_t SymVA = SignExtend64<Bits>(
309         getSymVA<ELFT>(Type, A, AddrLoc, *Rel.Sym, BufLoc, *File, Expr));
310 
311     switch (Expr) {
312     case R_RELAX_GOT_PC:
313     case R_RELAX_GOT_PC_NOPIC:
314       Target->relaxGot(BufLoc, SymVA);
315       break;
316     case R_RELAX_TLS_IE_TO_LE:
317       Target->relaxTlsIeToLe(BufLoc, Type, SymVA);
318       break;
319     case R_RELAX_TLS_LD_TO_LE:
320       Target->relaxTlsLdToLe(BufLoc, Type, SymVA);
321       break;
322     case R_RELAX_TLS_GD_TO_LE:
323     case R_RELAX_TLS_GD_TO_LE_NEG:
324       Target->relaxTlsGdToLe(BufLoc, Type, SymVA);
325       break;
326     case R_RELAX_TLS_GD_TO_IE:
327     case R_RELAX_TLS_GD_TO_IE_ABS:
328     case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
329     case R_RELAX_TLS_GD_TO_IE_END:
330       Target->relaxTlsGdToIe(BufLoc, Type, SymVA);
331       break;
332     case R_PPC_PLT_OPD:
333       // Patch a nop (0x60000000) to a ld.
334       if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == 0x60000000)
335         write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1)
336       // fallthrough
337     default:
338       Target->relocateOne(BufLoc, Type, SymVA);
339       break;
340     }
341   }
342 }
343 
344 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
345   if (this->Header->sh_type == SHT_NOBITS)
346     return;
347   ELFFile<ELFT> &EObj = this->File->getObj();
348 
349   // If -r is given, then an InputSection may be a relocation section.
350   if (this->Header->sh_type == SHT_RELA) {
351     copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
352     return;
353   }
354   if (this->Header->sh_type == SHT_REL) {
355     copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
356     return;
357   }
358 
359   // Copy section contents from source object file to output file.
360   ArrayRef<uint8_t> Data = this->getSectionData();
361   memcpy(Buf + OutSecOff, Data.data(), Data.size());
362 
363   // Iterate over all relocation sections that apply to this section.
364   uint8_t *BufEnd = Buf + OutSecOff + Data.size();
365   this->relocate(Buf, BufEnd);
366 
367   // The section might have a data/code generated by the linker and need
368   // to be written after the section. Usually these are thunks - small piece
369   // of code used to jump between "incompatible" functions like PIC and non-PIC
370   // or if the jump target too far and its address does not fit to the short
371   // jump istruction.
372   if (!Thunks.empty()) {
373     Buf += OutSecOff + getThunkOff();
374     for (const SymbolBody *S : Thunks) {
375       Target->writeThunk(Buf, S->getVA<ELFT>());
376       Buf += Target->ThunkSize;
377     }
378   }
379 }
380 
381 template <class ELFT>
382 void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
383   this->Alignment = std::max(this->Alignment, Other->Alignment);
384   Other->Repl = this->Repl;
385   Other->Live = false;
386 }
387 
388 template <class ELFT>
389 SplitInputSection<ELFT>::SplitInputSection(
390     elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
391     typename InputSectionBase<ELFT>::Kind SectionKind)
392     : InputSectionBase<ELFT>(File, Header, SectionKind) {}
393 
394 template <class ELFT>
395 EhInputSection<ELFT>::EhInputSection(elf::ObjectFile<ELFT> *F,
396                                      const Elf_Shdr *Header)
397     : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
398   // Mark .eh_frame sections as live by default because there are
399   // usually no relocations that point to .eh_frames. Otherwise,
400   // the garbage collector would drop all .eh_frame sections.
401   this->Live = true;
402 }
403 
404 template <class ELFT>
405 bool EhInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
406   return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
407 }
408 
409 // .eh_frame is a sequence of CIE or FDE records.
410 // This function splits an input section into records and returns them.
411 template <class ELFT>
412 void EhInputSection<ELFT>::split() {
413   ArrayRef<uint8_t> Data = this->getSectionData();
414   for (size_t Off = 0, End = Data.size(); Off != End;) {
415     size_t Size = readEhRecordSize<ELFT>(Data.slice(Off));
416     this->Pieces.emplace_back(Off, Data.slice(Off, Size));
417     // The empty record is the end marker.
418     if (Size == 4)
419       break;
420     Off += Size;
421   }
422 }
423 
424 template <class ELFT>
425 typename ELFT::uint EhInputSection<ELFT>::getOffset(uintX_t Offset) {
426   // The file crtbeginT.o has relocations pointing to the start of an empty
427   // .eh_frame that is known to be the first in the link. It does that to
428   // identify the start of the output .eh_frame. Handle this special case.
429   if (this->getSectionHdr()->sh_size == 0)
430     return Offset;
431   SectionPiece *Piece = this->getSectionPiece(Offset);
432   if (Piece->OutputOff == size_t(-1))
433     return -1; // Not in the output
434 
435   uintX_t Addend = Offset - Piece->InputOff;
436   return Piece->OutputOff + Addend;
437 }
438 
439 static size_t findNull(ArrayRef<uint8_t> A, size_t EntSize) {
440   // Optimize the common case.
441   StringRef S((const char *)A.data(), A.size());
442   if (EntSize == 1)
443     return S.find(0);
444 
445   for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
446     const char *B = S.begin() + I;
447     if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
448       return I;
449   }
450   return StringRef::npos;
451 }
452 
453 // Split SHF_STRINGS section. Such section is a sequence of
454 // null-terminated strings.
455 static std::vector<SectionPiece> splitStrings(ArrayRef<uint8_t> Data,
456                                               size_t EntSize) {
457   std::vector<SectionPiece> V;
458   size_t Off = 0;
459   while (!Data.empty()) {
460     size_t End = findNull(Data, EntSize);
461     if (End == StringRef::npos)
462       fatal("string is not null terminated");
463     size_t Size = End + EntSize;
464     V.emplace_back(Off, Data.slice(0, Size));
465     Data = Data.slice(Size);
466     Off += Size;
467   }
468   return V;
469 }
470 
471 // Split non-SHF_STRINGS section. Such section is a sequence of
472 // fixed size records.
473 static std::vector<SectionPiece> splitNonStrings(ArrayRef<uint8_t> Data,
474                                                  size_t EntSize) {
475   std::vector<SectionPiece> V;
476   size_t Size = Data.size();
477   assert((Size % EntSize) == 0);
478   for (unsigned I = 0, N = Size; I != N; I += EntSize)
479     V.emplace_back(I, Data.slice(I, EntSize));
480   return V;
481 }
482 
483 template <class ELFT>
484 MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
485                                            const Elf_Shdr *Header)
486     : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
487 
488 template <class ELFT> void MergeInputSection<ELFT>::splitIntoPieces() {
489   ArrayRef<uint8_t> Data = this->getSectionData();
490   uintX_t EntSize = this->Header->sh_entsize;
491   if (this->Header->sh_flags & SHF_STRINGS)
492     this->Pieces = splitStrings(Data, EntSize);
493   else
494     this->Pieces = splitNonStrings(Data, EntSize);
495 
496   if (Config->GcSections)
497     for (uintX_t Off : LiveOffsets)
498       this->getSectionPiece(Off)->Live = true;
499 }
500 
501 template <class ELFT>
502 bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
503   return S->SectionKind == InputSectionBase<ELFT>::Merge;
504 }
505 
506 // Do binary search to get a section piece at a given input offset.
507 template <class ELFT>
508 SectionPiece *SplitInputSection<ELFT>::getSectionPiece(uintX_t Offset) {
509   ArrayRef<uint8_t> D = this->getSectionData();
510   StringRef Data((const char *)D.data(), D.size());
511   uintX_t Size = Data.size();
512   if (Offset >= Size)
513     fatal("entry is past the end of the section");
514 
515   // Find the element this offset points to.
516   auto I = std::upper_bound(
517       Pieces.begin(), Pieces.end(), Offset,
518       [](const uintX_t &A, const SectionPiece &B) { return A < B.InputOff; });
519   --I;
520   return &*I;
521 }
522 
523 // Returns the offset in an output section for a given input offset.
524 // Because contents of a mergeable section is not contiguous in output,
525 // it is not just an addition to a base output offset.
526 template <class ELFT>
527 typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
528   auto It = OffsetMap.find(Offset);
529   if (It != OffsetMap.end())
530     return It->second;
531 
532   // If Offset is not at beginning of a section piece, it is not in the map.
533   // In that case we need to search from the original section piece vector.
534   SectionPiece &Piece = *this->getSectionPiece(Offset);
535   assert(Piece.Live);
536   uintX_t Addend = Offset - Piece.InputOff;
537   return Piece.OutputOff + Addend;
538 }
539 
540 // Create a map from input offsets to output offsets for all section pieces.
541 // It is called after finalize().
542 template <class ELFT> void  MergeInputSection<ELFT>::finalizePieces() {
543   OffsetMap.grow(this->Pieces.size());
544   for (SectionPiece &Piece : this->Pieces) {
545     if (!Piece.Live)
546       continue;
547     if (Piece.OutputOff == size_t(-1)) {
548       // Offsets of tail-merged strings are computed lazily.
549       auto *OutSec = static_cast<MergeOutputSection<ELFT> *>(this->OutSec);
550       ArrayRef<uint8_t> D = Piece.data();
551       StringRef S((const char *)D.data(), D.size());
552       Piece.OutputOff = OutSec->getOffset(S);
553     }
554     OffsetMap[Piece.InputOff] = Piece.OutputOff;
555   }
556 }
557 
558 template <class ELFT>
559 MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
560                                                        const Elf_Shdr *Hdr)
561     : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
562   // Initialize this->Reginfo.
563   ArrayRef<uint8_t> D = this->getSectionData();
564   if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) {
565     error("invalid size of .reginfo section");
566     return;
567   }
568   Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
569 }
570 
571 template <class ELFT>
572 bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
573   return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
574 }
575 
576 template <class ELFT>
577 MipsOptionsInputSection<ELFT>::MipsOptionsInputSection(elf::ObjectFile<ELFT> *F,
578                                                        const Elf_Shdr *Hdr)
579     : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsOptions) {
580   // Find ODK_REGINFO option in the section's content.
581   ArrayRef<uint8_t> D = this->getSectionData();
582   while (!D.empty()) {
583     if (D.size() < sizeof(Elf_Mips_Options<ELFT>)) {
584       error("invalid size of .MIPS.options section");
585       break;
586     }
587     auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(D.data());
588     if (O->kind == ODK_REGINFO) {
589       Reginfo = &O->getRegInfo();
590       break;
591     }
592     D = D.slice(O->size);
593   }
594 }
595 
596 template <class ELFT>
597 bool MipsOptionsInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
598   return S->SectionKind == InputSectionBase<ELFT>::MipsOptions;
599 }
600 
601 template class elf::InputSectionBase<ELF32LE>;
602 template class elf::InputSectionBase<ELF32BE>;
603 template class elf::InputSectionBase<ELF64LE>;
604 template class elf::InputSectionBase<ELF64BE>;
605 
606 template class elf::InputSection<ELF32LE>;
607 template class elf::InputSection<ELF32BE>;
608 template class elf::InputSection<ELF64LE>;
609 template class elf::InputSection<ELF64BE>;
610 
611 template class elf::SplitInputSection<ELF32LE>;
612 template class elf::SplitInputSection<ELF32BE>;
613 template class elf::SplitInputSection<ELF64LE>;
614 template class elf::SplitInputSection<ELF64BE>;
615 
616 template class elf::EhInputSection<ELF32LE>;
617 template class elf::EhInputSection<ELF32BE>;
618 template class elf::EhInputSection<ELF64LE>;
619 template class elf::EhInputSection<ELF64BE>;
620 
621 template class elf::MergeInputSection<ELF32LE>;
622 template class elf::MergeInputSection<ELF32BE>;
623 template class elf::MergeInputSection<ELF64LE>;
624 template class elf::MergeInputSection<ELF64BE>;
625 
626 template class elf::MipsReginfoInputSection<ELF32LE>;
627 template class elf::MipsReginfoInputSection<ELF32BE>;
628 template class elf::MipsReginfoInputSection<ELF64LE>;
629 template class elf::MipsReginfoInputSection<ELF64BE>;
630 
631 template class elf::MipsOptionsInputSection<ELF32LE>;
632 template class elf::MipsOptionsInputSection<ELF32BE>;
633 template class elf::MipsOptionsInputSection<ELF64LE>;
634 template class elf::MipsOptionsInputSection<ELF64BE>;
635