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