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