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