blob: 7a6b7d0edab3e2f979588f433e7082a2a0d91d15 [file] [log] [blame]
Rafael Espindola9d06ab62015-09-22 00:01:39 +00001//===- InputSection.cpp ---------------------------------------------------===//
Michael J. Spencer84487f12015-07-24 21:03:07 +00002//
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
Rafael Espindola9d06ab62015-09-22 00:01:39 +000010#include "InputSection.h"
Rafael Espindola551dfd82015-09-25 19:24:57 +000011#include "Config.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000012#include "Error.h"
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000013#include "InputFiles.h"
Rafael Espindola4ea00212015-09-21 22:01:00 +000014#include "OutputSections.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000015#include "Target.h"
Rafael Espindola4ea00212015-09-21 22:01:00 +000016
Michael J. Spencer84487f12015-07-24 21:03:07 +000017using namespace llvm;
18using namespace llvm::ELF;
Rafael Espindola4ea00212015-09-21 22:01:00 +000019using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000020
21using namespace lld;
22using namespace lld::elf2;
23
24template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +000025InputSectionBase<ELFT>::InputSectionBase(ObjectFile<ELFT> *File,
26 const Elf_Shdr *Header,
27 Kind SectionKind)
28 : Header(Header), File(File), SectionKind(SectionKind) {}
29
30template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
31 ErrorOr<StringRef> Name = File->getObj().getSectionName(this->Header);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000032 fatal(Name);
Rafael Espindolac159c962015-10-19 21:00:02 +000033 return *Name;
34}
35
36template <class ELFT>
37ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
38 ErrorOr<ArrayRef<uint8_t>> Ret =
39 this->File->getObj().getSectionContents(this->Header);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000040 fatal(Ret);
Rafael Espindolac159c962015-10-19 21:00:02 +000041 return *Ret;
42}
43
44template <class ELFT>
45typename ELFFile<ELFT>::uintX_t
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000046InputSectionBase<ELFT>::getOffset(uintX_t Offset) {
47 switch (SectionKind) {
48 case Regular:
49 return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000050 case EHFrame:
51 return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000052 case Merge:
53 return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
Simon Atanasyan1d7df402015-12-20 10:57:34 +000054 case MipsReginfo:
Rui Ueyama58a636a2016-01-06 22:01:25 +000055 // MIPS .reginfo sections are consumed by the linker,
56 // so it should never be copied to output.
57 llvm_unreachable("MIPS .reginfo reached writeTo().");
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000058 }
Denis Protivensky1b1b34e2015-11-12 09:11:20 +000059 llvm_unreachable("Invalid section kind");
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000060}
61
62template <class ELFT>
63typename ELFFile<ELFT>::uintX_t
Rafael Espindola48225b42015-10-23 19:55:11 +000064InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000065 return getOffset(Sym.st_value);
Rafael Espindolac159c962015-10-19 21:00:02 +000066}
67
Rui Ueyama12504642015-10-27 21:51:13 +000068// Returns a section that Rel relocation is pointing to.
69template <class ELFT>
70InputSectionBase<ELFT> *
71InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) {
72 // Global symbol
73 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
74 if (SymbolBody *B = File->getSymbolBody(SymIndex))
75 if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B->repl()))
Rafael Espindola02ce26a2015-12-24 14:22:24 +000076 return D->Section;
Rui Ueyama12504642015-10-27 21:51:13 +000077 // Local symbol
78 if (const Elf_Sym *Sym = File->getLocalSymbol(SymIndex))
79 if (InputSectionBase<ELFT> *Sec = File->getSection(*Sym))
80 return Sec;
81 return nullptr;
82}
83
84template <class ELFT>
85InputSectionBase<ELFT> *
86InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) {
87 return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel));
88}
89
Rafael Espindolac159c962015-10-19 21:00:02 +000090template <class ELFT>
Rafael Espindola53d5cea2015-09-21 17:47:00 +000091InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header)
Rafael Espindolac159c962015-10-19 21:00:02 +000092 : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
93
94template <class ELFT>
95bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
96 return S->SectionKind == Base::Regular;
97}
Michael J. Spencer84487f12015-07-24 21:03:07 +000098
Rafael Espindola4ea00212015-09-21 22:01:00 +000099template <class ELFT>
Rafael Espindola4ea00212015-09-21 22:01:00 +0000100template <bool isRela>
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000101uint8_t *
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000102InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf, uint32_t SymIndex,
103 uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000104 RelIteratorRange<isRela> Rels) {
105 // Some MIPS relocations use addend calculated from addend of the relocation
106 // itself and addend of paired relocation. ABI requires to compute such
107 // combined addend in case of REL relocation record format only.
108 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
109 if (isRela || Config->EMachine != EM_MIPS)
110 return nullptr;
111 if (Type == R_MIPS_HI16)
112 Type = R_MIPS_LO16;
113 else if (Type == R_MIPS_PCHI16)
114 Type = R_MIPS_PCLO16;
115 else if (Type == R_MICROMIPS_HI16)
116 Type = R_MICROMIPS_LO16;
117 else
118 return nullptr;
119 for (const auto &RI : Rels) {
120 if (RI.getType(Config->Mips64EL) != Type)
121 continue;
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000122 if (RI.getSymbol(Config->Mips64EL) != SymIndex)
123 continue;
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000124 uintX_t Offset = getOffset(RI.r_offset);
125 if (Offset == (uintX_t)-1)
126 return nullptr;
127 return Buf + Offset;
128 }
129 return nullptr;
130}
131
132template <class ELFT>
133template <bool isRela>
134void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd,
135 RelIteratorRange<isRela> Rels) {
Rafael Espindola4ea00212015-09-21 22:01:00 +0000136 typedef Elf_Rel_Impl<ELFT, isRela> RelType;
George Rimar6713cf82015-11-25 21:46:05 +0000137 size_t Num = Rels.end() - Rels.begin();
138 for (size_t I = 0; I < Num; ++I) {
139 const RelType &RI = *(Rels.begin() + I);
Rui Ueyama64558522015-10-16 22:51:43 +0000140 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
141 uint32_t Type = RI.getType(Config->Mips64EL);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000142 uintX_t Offset = getOffset(RI.r_offset);
143 if (Offset == (uintX_t)-1)
144 continue;
145
146 uint8_t *BufLoc = Buf + Offset;
147 uintX_t AddrLoc = OutSec->getVA() + Offset;
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000148 auto NextRelocs = llvm::make_range(&RI, Rels.end());
Michael J. Spencer1e225612015-11-11 01:00:24 +0000149
Rui Ueyamac516ae12016-01-29 02:33:45 +0000150 if (Target->isTlsLocalDynamicRel(Type) &&
Rui Ueyamabaf16512016-01-29 00:20:12 +0000151 !Target->canRelaxTls(Type, nullptr)) {
Michael J. Spencer1e225612015-11-11 01:00:24 +0000152 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000153 Out<ELFT>::Got->getTlsIndexVA() +
Michael J. Spencer1e225612015-11-11 01:00:24 +0000154 getAddend<ELFT>(RI));
155 continue;
156 }
Rafael Espindola4ea00212015-09-21 22:01:00 +0000157
George Rimar0b8ed1d2015-12-21 10:37:33 +0000158 const Elf_Shdr *SymTab = File->getSymbolTable();
159 SymbolBody *Body = nullptr;
160 if (SymIndex >= SymTab->sh_info)
161 Body = File->getSymbolBody(SymIndex)->repl();
162
Rui Ueyamabaf16512016-01-29 00:20:12 +0000163 if (Target->canRelaxTls(Type, Body)) {
George Rimar0b8ed1d2015-12-21 10:37:33 +0000164 uintX_t SymVA;
165 if (!Body)
166 SymVA = getLocalRelTarget(*File, RI, 0);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000167 else if (Target->needsGot(Type, *Body))
Rui Ueyamab5a69702016-02-01 21:00:35 +0000168 SymVA = Body->getGotVA<ELFT>();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000169 else
Rui Ueyamab5a69702016-02-01 21:00:35 +0000170 SymVA = Body->getVA<ELFT>();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000171 // By optimizing TLS relocations, it is sometimes needed to skip
172 // relocations that immediately follow TLS relocations. This function
173 // knows how many slots we need to skip.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000174 I += Target->relaxTls(BufLoc, BufEnd, Type, AddrLoc, SymVA, Body);
George Rimar0b8ed1d2015-12-21 10:37:33 +0000175 continue;
176 }
177
Rafael Espindola4ea00212015-09-21 22:01:00 +0000178 // Handle relocations for local symbols -- they never get
179 // resolved so we don't allocate a SymbolBody.
George Rimar0b8ed1d2015-12-21 10:37:33 +0000180 uintX_t A = getAddend<ELFT>(RI);
181 if (!Body) {
182 uintX_t SymVA = getLocalRelTarget(*File, RI, A);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000183 if (Config->EMachine == EM_MIPS) {
184 if (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32)
185 // We need to adjust SymVA value in case of R_MIPS_GPREL16/32
186 // relocations because they use the following expression to calculate
187 // the relocation's result for local symbol: S + A + GP0 - G.
188 SymVA += File->getMipsGp0();
189 else if (Type == R_MIPS_GOT16)
190 // R_MIPS_GOT16 relocation against local symbol requires index of
191 // a local GOT entry which contains page address corresponds
192 // to the symbol address.
193 SymVA = Out<ELFT>::Got->getMipsLocalPageAddr(SymVA);
194 }
George Rimar48651482015-12-11 08:59:37 +0000195 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0,
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000196 findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs));
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000197 continue;
Rafael Espindola4ea00212015-09-21 22:01:00 +0000198 }
199
Rui Ueyamac516ae12016-01-29 02:33:45 +0000200 if (Target->isTlsGlobalDynamicRel(Type) &&
Rui Ueyamabaf16512016-01-29 00:20:12 +0000201 !Target->canRelaxTls(Type, Body)) {
Michael J. Spencer627ae702015-11-13 00:28:34 +0000202 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
George Rimar0b8ed1d2015-12-21 10:37:33 +0000203 Out<ELFT>::Got->getGlobalDynAddr(*Body) +
Michael J. Spencer627ae702015-11-13 00:28:34 +0000204 getAddend<ELFT>(RI));
205 continue;
206 }
207
Rui Ueyamab5a69702016-02-01 21:00:35 +0000208 uintX_t SymVA = Body->getVA<ELFT>();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000209 if (Target->needsPlt(Type, *Body)) {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000210 SymVA = Body->getPltVA<ELFT>();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000211 } else if (Target->needsGot(Type, *Body)) {
Simon Atanasyan4b034512016-02-04 11:51:45 +0000212 if (Config->EMachine == EM_MIPS && !canBePreempted(Body, true))
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000213 // Under some conditions relocations against non-local symbols require
214 // entries in the local part of MIPS GOT. In that case we need an entry
215 // initialized by full address of the symbol.
216 SymVA = Out<ELFT>::Got->getMipsLocalFullAddr(*Body);
217 else
Rui Ueyamab5a69702016-02-01 21:00:35 +0000218 SymVA = Body->getGotVA<ELFT>();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000219 if (Body->isTls())
Rui Ueyama724d6252016-01-29 01:49:32 +0000220 Type = Target->getTlsGotRel(Type);
George Rimar0b8ed1d2015-12-21 10:37:33 +0000221 } else if (!Target->needsCopyRel(Type, *Body) &&
222 isa<SharedSymbol<ELFT>>(*Body)) {
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000223 continue;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000224 } else if (Target->isTlsDynRel(Type, *Body)) {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000225 continue;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000226 } else if (Target->isSizeRel(Type) && canBePreempted(Body, false)) {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000227 // A SIZE relocation is supposed to set a symbol size, but if a symbol
228 // can be preempted, the size at runtime may be different than link time.
229 // If that's the case, we leave the field alone rather than filling it
230 // with a possibly incorrect value.
George Rimard23970f2015-11-25 20:41:53 +0000231 continue;
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000232 } else if (Config->EMachine == EM_MIPS) {
George Rimar0b8ed1d2015-12-21 10:37:33 +0000233 if (Type == R_MIPS_HI16 && Body == Config->MipsGpDisp)
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000234 SymVA = getMipsGpAddr<ELFT>() - AddrLoc;
George Rimar0b8ed1d2015-12-21 10:37:33 +0000235 else if (Type == R_MIPS_LO16 && Body == Config->MipsGpDisp)
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000236 SymVA = getMipsGpAddr<ELFT>() - AddrLoc + 4;
Simon Atanasyan597df212016-02-04 12:09:49 +0000237 else if (Body == Config->MipsLocalGp)
238 SymVA = getMipsGpAddr<ELFT>();
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000239 }
Rui Ueyama512c61d2016-02-03 00:12:24 +0000240 uintX_t Size = Body->getSize<ELFT>();
George Rimar48651482015-12-11 08:59:37 +0000241 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + A, Size + A,
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000242 findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000243 }
244}
245
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000246template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000247 if (this->Header->sh_type == SHT_NOBITS)
Michael J. Spencer84487f12015-07-24 21:03:07 +0000248 return;
249 // Copy section contents from source object file to output file.
Rafael Espindolac159c962015-10-19 21:00:02 +0000250 ArrayRef<uint8_t> Data = this->getSectionData();
Rui Ueyama55c3f892015-10-15 01:58:40 +0000251 memcpy(Buf + OutSecOff, Data.data(), Data.size());
Rafael Espindola4ea00212015-09-21 22:01:00 +0000252
Rafael Espindolac159c962015-10-19 21:00:02 +0000253 ELFFile<ELFT> &EObj = this->File->getObj();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000254 uint8_t *BufEnd = Buf + OutSecOff + Data.size();
Rafael Espindola4ea00212015-09-21 22:01:00 +0000255 // Iterate over all relocation sections that apply to this section.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000256 for (const Elf_Shdr *RelSec : this->RelocSections) {
Rafael Espindola4ea00212015-09-21 22:01:00 +0000257 if (RelSec->sh_type == SHT_RELA)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000258 this->relocate(Buf, BufEnd, EObj.relas(RelSec));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000259 else
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000260 this->relocate(Buf, BufEnd, EObj.rels(RelSec));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000261 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000262}
263
Rafael Espindolac159c962015-10-19 21:00:02 +0000264template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000265SplitInputSection<ELFT>::SplitInputSection(
266 ObjectFile<ELFT> *File, const Elf_Shdr *Header,
267 typename InputSectionBase<ELFT>::Kind SectionKind)
268 : InputSectionBase<ELFT>(File, Header, SectionKind) {}
269
270template <class ELFT>
271EHInputSection<ELFT>::EHInputSection(ObjectFile<ELFT> *F,
272 const Elf_Shdr *Header)
Rui Ueyamada735322015-12-24 10:08:54 +0000273 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
274 // Mark .eh_frame sections as live by default because there are
275 // usually no relocations that point to .eh_frames. Otherwise,
George Rimare9e1d322016-02-18 15:17:01 +0000276 // the garbage collector would drop all .eh_frame sections.
Rui Ueyamada735322015-12-24 10:08:54 +0000277 this->Live = true;
278}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000279
280template <class ELFT>
281bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
282 return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
283}
284
285template <class ELFT>
286typename EHInputSection<ELFT>::uintX_t
287EHInputSection<ELFT>::getOffset(uintX_t Offset) {
George Rimar6ab275c2015-12-25 09:51:42 +0000288 // The file crtbeginT.o has relocations pointing to the start of an empty
289 // .eh_frame that is known to be the first in the link. It does that to
290 // identify the start of the output .eh_frame. Handle this special case.
291 if (this->getSectionHdr()->sh_size == 0)
292 return Offset;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000293 std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
294 uintX_t Base = I->second;
George Rimar4b40ebc2015-11-13 13:44:59 +0000295 if (Base == uintX_t(-1))
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000296 return -1; // Not in the output
297
298 uintX_t Addend = Offset - I->first;
299 return Base + Addend;
300}
301
302template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000303MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F,
304 const Elf_Shdr *Header)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000305 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
Rafael Espindolac159c962015-10-19 21:00:02 +0000306
307template <class ELFT>
308bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000309 return S->SectionKind == InputSectionBase<ELFT>::Merge;
Rafael Espindolac159c962015-10-19 21:00:02 +0000310}
311
Rafael Espindolac159c962015-10-19 21:00:02 +0000312template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000313std::pair<std::pair<typename ELFFile<ELFT>::uintX_t,
314 typename ELFFile<ELFT>::uintX_t> *,
315 typename ELFFile<ELFT>::uintX_t>
316SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000317 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama7ba639b2015-10-25 16:25:04 +0000318 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000319 uintX_t Size = Data.size();
320 if (Offset >= Size)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000321 fatal("Entry is past the end of the section");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000322
323 // Find the element this offset points to.
324 auto I = std::upper_bound(
Rafael Espindolad04c12a2015-11-11 15:20:45 +0000325 Offsets.begin(), Offsets.end(), Offset,
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000326 [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000327 return A < B.first;
328 });
Rafael Espindola32994992015-11-11 15:40:37 +0000329 uintX_t End = I == Offsets.end() ? Data.size() : I->first;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000330 --I;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000331 return std::make_pair(&*I, End);
332}
333
334template <class ELFT>
335typename MergeInputSection<ELFT>::uintX_t
336MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
George Rimar4b40ebc2015-11-13 13:44:59 +0000337 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000338 this->getRangeAndSize(Offset);
339 std::pair<uintX_t, uintX_t> *I = T.first;
340 uintX_t End = T.second;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000341 uintX_t Start = I->first;
342
343 // Compute the Addend and if the Base is cached, return.
344 uintX_t Addend = Offset - Start;
Rafael Espindola32994992015-11-11 15:40:37 +0000345 uintX_t &Base = I->second;
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000346 if (Base != uintX_t(-1))
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000347 return Base + Addend;
348
Hal Finkelf9505952015-11-25 23:54:53 +0000349 // Map the base to the offset in the output section and cache it.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000350 ArrayRef<uint8_t> D = this->getSectionData();
351 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000352 StringRef Entry = Data.substr(Start, End - Start);
353 Base =
354 static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
355 return Base + Addend;
Rafael Espindola5d83ccd2015-08-13 19:18:30 +0000356}
357
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000358template <class ELFT>
359MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(ObjectFile<ELFT> *F,
Rui Ueyama70eed362016-01-06 22:42:43 +0000360 const Elf_Shdr *Hdr)
361 : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
362 // Initialize this->Reginfo.
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000363 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama70eed362016-01-06 22:42:43 +0000364 if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000365 fatal("Invalid size of .reginfo section");
Rui Ueyama70eed362016-01-06 22:42:43 +0000366 Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
Simon Atanasyan57830b62015-12-25 13:02:13 +0000367}
368
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000369template <class ELFT>
370bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
371 return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
372}
373
Rafael Espindola25472ee2016-01-22 21:49:07 +0000374template class elf2::InputSectionBase<ELF32LE>;
375template class elf2::InputSectionBase<ELF32BE>;
376template class elf2::InputSectionBase<ELF64LE>;
377template class elf2::InputSectionBase<ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000378
Rafael Espindola25472ee2016-01-22 21:49:07 +0000379template class elf2::InputSection<ELF32LE>;
380template class elf2::InputSection<ELF32BE>;
381template class elf2::InputSection<ELF64LE>;
382template class elf2::InputSection<ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000383
Rafael Espindola25472ee2016-01-22 21:49:07 +0000384template class elf2::EHInputSection<ELF32LE>;
385template class elf2::EHInputSection<ELF32BE>;
386template class elf2::EHInputSection<ELF64LE>;
387template class elf2::EHInputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000388
Rafael Espindola25472ee2016-01-22 21:49:07 +0000389template class elf2::MergeInputSection<ELF32LE>;
390template class elf2::MergeInputSection<ELF32BE>;
391template class elf2::MergeInputSection<ELF64LE>;
392template class elf2::MergeInputSection<ELF64BE>;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000393
Rafael Espindola25472ee2016-01-22 21:49:07 +0000394template class elf2::MipsReginfoInputSection<ELF32LE>;
395template class elf2::MipsReginfoInputSection<ELF32BE>;
396template class elf2::MipsReginfoInputSection<ELF64LE>;
397template class elf2::MipsReginfoInputSection<ELF64BE>;