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