blob: 1c2116362030012ad20f782779c756bff9e1cdf6 [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
Simon Atanasyan860fbf02016-02-25 21:33:56 +000017#include "llvm/Support/Endian.h"
18
Michael J. Spencer84487f12015-07-24 21:03:07 +000019using namespace llvm;
20using namespace llvm::ELF;
Rafael Espindola4ea00212015-09-21 22:01:00 +000021using namespace llvm::object;
Simon Atanasyan860fbf02016-02-25 21:33:56 +000022using namespace llvm::support::endian;
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
24using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000025using namespace lld::elf;
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
27template <class ELFT>
Rafael Espindolab20dcb12016-03-11 16:23:45 +000028InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
Rafael Espindolac159c962015-10-19 21:00:02 +000029 const Elf_Shdr *Header,
30 Kind SectionKind)
Rui Ueyama0b289522016-02-25 18:43:51 +000031 : Header(Header), File(File), SectionKind(SectionKind), Repl(this) {
Rui Ueyama8fc070d2016-02-24 00:23:15 +000032 // The garbage collector sets sections' Live bits.
33 // If GC is disabled, all sections are considered live by default.
Rui Ueyama733153d2016-02-24 18:33:35 +000034 Live = !Config->GcSections;
Rui Ueyama5ac58912016-02-24 00:38:18 +000035
36 // The ELF spec states that a value of 0 means the section has
37 // no alignment constraits.
Rui Ueyama733153d2016-02-24 18:33:35 +000038 Align = std::max<uintX_t>(Header->sh_addralign, 1);
Rui Ueyama8fc070d2016-02-24 00:23:15 +000039}
Rafael Espindolac159c962015-10-19 21:00:02 +000040
41template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
Rafael Espindola75714f62016-03-03 22:24:39 +000042 return check(File->getObj().getSectionName(this->Header));
Rafael Espindolac159c962015-10-19 21:00:02 +000043}
44
45template <class ELFT>
46ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
Rafael Espindola75714f62016-03-03 22:24:39 +000047 return check(this->File->getObj().getSectionContents(this->Header));
Rafael Espindolac159c962015-10-19 21:00:02 +000048}
49
50template <class ELFT>
51typename ELFFile<ELFT>::uintX_t
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000052InputSectionBase<ELFT>::getOffset(uintX_t Offset) {
53 switch (SectionKind) {
54 case Regular:
55 return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000056 case EHFrame:
57 return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000058 case Merge:
59 return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
Simon Atanasyan1d7df402015-12-20 10:57:34 +000060 case MipsReginfo:
Rui Ueyama58a636a2016-01-06 22:01:25 +000061 // MIPS .reginfo sections are consumed by the linker,
62 // so it should never be copied to output.
63 llvm_unreachable("MIPS .reginfo reached writeTo().");
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000064 }
Denis Protivensky1b1b34e2015-11-12 09:11:20 +000065 llvm_unreachable("Invalid section kind");
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000066}
67
68template <class ELFT>
69typename ELFFile<ELFT>::uintX_t
Rafael Espindola48225b42015-10-23 19:55:11 +000070InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000071 return getOffset(Sym.st_value);
Rafael Espindolac159c962015-10-19 21:00:02 +000072}
73
Rui Ueyama12504642015-10-27 21:51:13 +000074// Returns a section that Rel relocation is pointing to.
75template <class ELFT>
76InputSectionBase<ELFT> *
Rui Ueyamad7e4a282016-02-24 00:23:13 +000077InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) const {
Rui Ueyama12504642015-10-27 21:51:13 +000078 // Global symbol
79 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
Rafael Espindola67d72c02016-03-11 12:06:30 +000080 SymbolBody &B = File->getSymbolBody(SymIndex).repl();
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000081 InputSectionBase<ELFT> *S = nullptr;
Rafael Espindola67d72c02016-03-11 12:06:30 +000082 if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B))
Rafael Espindola1f5b70f2016-03-11 14:21:37 +000083 S = D->Section;
84 if (S)
85 return S->Repl;
Rui Ueyama12504642015-10-27 21:51:13 +000086 return nullptr;
87}
88
89template <class ELFT>
90InputSectionBase<ELFT> *
Rui Ueyamad7e4a282016-02-24 00:23:13 +000091InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) const {
Rui Ueyama12504642015-10-27 21:51:13 +000092 return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel));
93}
94
Rafael Espindolac159c962015-10-19 21:00:02 +000095template <class ELFT>
Rafael Espindolab20dcb12016-03-11 16:23:45 +000096InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F,
97 const Elf_Shdr *Header)
Rafael Espindolac159c962015-10-19 21:00:02 +000098 : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
99
100template <class ELFT>
101bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
102 return S->SectionKind == Base::Regular;
103}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000104
Rafael Espindola4ea00212015-09-21 22:01:00 +0000105template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000106InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() {
107 assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL);
108 ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections();
109 return Sections[this->Header->sh_info];
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.
115template <class ELFT>
116template <bool isRela>
117void InputSection<ELFT>::copyRelocations(uint8_t *Buf,
118 RelIteratorRange<isRela> Rels) {
119 typedef Elf_Rel_Impl<ELFT, isRela> RelType;
120 InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
121
122 for (const RelType &Rel : Rels) {
123 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
124 uint32_t Type = Rel.getType(Config->Mips64EL);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000125 SymbolBody &Body = this->File->getSymbolBody(SymIndex).repl();
George Rimar58941ee2016-02-25 08:23:37 +0000126
127 RelType *P = reinterpret_cast<RelType *>(Buf);
128 Buf += sizeof(RelType);
129
George Rimar58941ee2016-02-25 08:23:37 +0000130 P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000131 P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL);
George Rimar58941ee2016-02-25 08:23:37 +0000132 }
133}
134
Simon Atanasyan860fbf02016-02-25 21:33:56 +0000135static uint32_t getMipsPairedRelocType(uint32_t Type) {
136 if (Config->EMachine != EM_MIPS)
137 return R_MIPS_NONE;
138 switch (Type) {
139 case R_MIPS_HI16:
140 return R_MIPS_LO16;
141 case R_MIPS_PCHI16:
142 return R_MIPS_PCLO16;
143 case R_MICROMIPS_HI16:
144 return R_MICROMIPS_LO16;
145 default:
146 return R_MIPS_NONE;
147 }
148}
149
George Rimar58941ee2016-02-25 08:23:37 +0000150template <class ELFT>
Rafael Espindola4ea00212015-09-21 22:01:00 +0000151template <bool isRela>
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000152uint8_t *
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000153InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf, uint32_t SymIndex,
154 uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000155 RelIteratorRange<isRela> Rels) {
156 // Some MIPS relocations use addend calculated from addend of the relocation
157 // itself and addend of paired relocation. ABI requires to compute such
158 // combined addend in case of REL relocation record format only.
159 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Simon Atanasyan860fbf02016-02-25 21:33:56 +0000160 if (isRela || Type == R_MIPS_NONE)
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000161 return nullptr;
162 for (const auto &RI : Rels) {
163 if (RI.getType(Config->Mips64EL) != Type)
164 continue;
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000165 if (RI.getSymbol(Config->Mips64EL) != SymIndex)
166 continue;
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000167 uintX_t Offset = getOffset(RI.r_offset);
168 if (Offset == (uintX_t)-1)
169 return nullptr;
170 return Buf + Offset;
171 }
172 return nullptr;
173}
174
175template <class ELFT>
176template <bool isRela>
177void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd,
178 RelIteratorRange<isRela> Rels) {
Rafael Espindola4ea00212015-09-21 22:01:00 +0000179 typedef Elf_Rel_Impl<ELFT, isRela> RelType;
George Rimar6713cf82015-11-25 21:46:05 +0000180 size_t Num = Rels.end() - Rels.begin();
181 for (size_t I = 0; I < Num; ++I) {
182 const RelType &RI = *(Rels.begin() + I);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000183 uintX_t Offset = getOffset(RI.r_offset);
184 if (Offset == (uintX_t)-1)
185 continue;
186
George Rimara4ab97d2016-03-11 12:57:52 +0000187 uintX_t A = getAddend<ELFT>(RI);
188 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
189 uint32_t Type = RI.getType(Config->Mips64EL);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000190 uint8_t *BufLoc = Buf + Offset;
191 uintX_t AddrLoc = OutSec->getVA() + Offset;
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000192 auto NextRelocs = llvm::make_range(&RI, Rels.end());
Michael J. Spencer1e225612015-11-11 01:00:24 +0000193
Rafael Espindolad405f472016-03-04 21:37:09 +0000194 if (Target->pointsToLocalDynamicGotEntry(Type) &&
Rui Ueyamabaf16512016-01-29 00:20:12 +0000195 !Target->canRelaxTls(Type, nullptr)) {
Michael J. Spencer1e225612015-11-11 01:00:24 +0000196 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
George Rimar1d9738c2016-03-11 12:53:17 +0000197 Out<ELFT>::Got->getTlsIndexVA() + A);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000198 continue;
199 }
Rafael Espindola4ea00212015-09-21 22:01:00 +0000200
Rafael Espindola67d72c02016-03-11 12:06:30 +0000201 SymbolBody &Body = File->getSymbolBody(SymIndex).repl();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000202
Rafael Espindola67d72c02016-03-11 12:06:30 +0000203 if (Target->canRelaxTls(Type, &Body)) {
George Rimar0b8ed1d2015-12-21 10:37:33 +0000204 uintX_t SymVA;
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000205 if (Target->needsGot(Type, Body))
Rafael Espindola67d72c02016-03-11 12:06:30 +0000206 SymVA = Body.getGotVA<ELFT>();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000207 else
Rafael Espindola67d72c02016-03-11 12:06:30 +0000208 SymVA = Body.getVA<ELFT>();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000209 // By optimizing TLS relocations, it is sometimes needed to skip
210 // relocations that immediately follow TLS relocations. This function
211 // knows how many slots we need to skip.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000212 I += Target->relaxTls(BufLoc, BufEnd, Type, AddrLoc, SymVA, Body);
George Rimar0b8ed1d2015-12-21 10:37:33 +0000213 continue;
214 }
215
Rafael Espindola87d9f102016-03-11 12:19:05 +0000216 // PPC64 has a special relocation representing the TOC base pointer
217 // that does not have a corresponding symbol.
218 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC) {
219 uintX_t SymVA = getPPC64TocBase() + A;
220 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0);
221 continue;
222 }
223
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000224 if (Target->isTlsGlobalDynamicRel(Type) &&
225 !Target->canRelaxTls(Type, &Body)) {
226 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
227 Out<ELFT>::Got->getGlobalDynAddr(Body) + A);
228 continue;
229 }
230
231 uintX_t SymVA = Body.getVA<ELFT>(A);
232 bool CBP = canBePreempted(Body);
233 uint8_t *PairedLoc = nullptr;
234
235 if (Target->needsPlt<ELFT>(Type, Body)) {
236 SymVA = Body.getPltVA<ELFT>() + A;
237 } else if (Target->needsGot(Type, Body)) {
238 if (Config->EMachine == EM_MIPS && !CBP) {
239 if (Body.isLocal()) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000240 // R_MIPS_GOT16 relocation against local symbol requires index of
241 // a local GOT entry which contains page address corresponds
Simon Atanasyan860fbf02016-02-25 21:33:56 +0000242 // to sum of the symbol address and addend. The addend in that case
243 // is calculated using addends from R_MIPS_GOT16 and paired
244 // R_MIPS_LO16 relocations.
245 const endianness E = ELFT::TargetEndianness;
246 uint8_t *LowLoc =
247 findMipsPairedReloc(Buf, SymIndex, R_MIPS_LO16, NextRelocs);
248 uint64_t AHL = read32<E>(BufLoc) << 16;
249 if (LowLoc)
250 AHL += SignExtend64<16>(read32<E>(LowLoc));
251 SymVA = Out<ELFT>::Got->getMipsLocalPageAddr(SymVA + AHL);
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000252 } else {
Simon Atanasyan019049f2016-03-11 13:57:53 +0000253 // For non-local symbols GOT entries should contain their full
254 // addresses. But if such symbol cannot be preempted, we do not
255 // have to put them into the "global" part of GOT and use dynamic
256 // linker to determine their actual addresses. That is why we
257 // create GOT entries for them in the "local" part of GOT.
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000258 SymVA = Out<ELFT>::Got->getMipsLocalFullAddr(Body) + A;
259 }
260 } else {
261 SymVA = Body.getGotVA<ELFT>() + A;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000262 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000263 if (Body.IsTls)
Rui Ueyama724d6252016-01-29 01:49:32 +0000264 Type = Target->getTlsGotRel(Type);
Rafael Espindolaa350e262016-02-26 14:33:23 +0000265 } else if (Target->isSizeRel(Type) && CBP) {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000266 // A SIZE relocation is supposed to set a symbol size, but if a symbol
267 // can be preempted, the size at runtime may be different than link time.
268 // If that's the case, we leave the field alone rather than filling it
269 // with a possibly incorrect value.
George Rimard23970f2015-11-25 20:41:53 +0000270 continue;
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000271 } else if (Config->EMachine == EM_MIPS) {
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000272 if (Type == R_MIPS_HI16 && &Body == Config->MipsGpDisp) {
273 SymVA = getMipsGpAddr<ELFT>() - AddrLoc + A;
274 } else if (Type == R_MIPS_LO16 && &Body == Config->MipsGpDisp) {
275 SymVA = getMipsGpAddr<ELFT>() - AddrLoc + 4 + A;
276 } else if (&Body == Config->MipsLocalGp) {
277 SymVA = getMipsGpAddr<ELFT>() + A;
278 } else if (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32) {
279 // We need to adjust SymVA value in case of R_MIPS_GPREL16/32
280 // relocations because they use the following expression to calculate
281 // the relocation's result for local symbol: S + A + GP0 - G.
282 SymVA += File->getMipsGp0();
283 } else {
284 PairedLoc = findMipsPairedReloc(
285 Buf, SymIndex, getMipsPairedRelocType(Type), NextRelocs);
286 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000287 } else if (!Target->needsCopyRel<ELFT>(Type, Body) && CBP) {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000288 continue;
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000289 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000290 uintX_t Size = Body.getSize<ELFT>();
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000291 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, Size + A,
292 PairedLoc);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000293 }
294}
295
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000296template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000297 if (this->Header->sh_type == SHT_NOBITS)
Michael J. Spencer84487f12015-07-24 21:03:07 +0000298 return;
299 // Copy section contents from source object file to output file.
Rafael Espindolac159c962015-10-19 21:00:02 +0000300 ArrayRef<uint8_t> Data = this->getSectionData();
George Rimar58941ee2016-02-25 08:23:37 +0000301 ELFFile<ELFT> &EObj = this->File->getObj();
302
303 // That happens with -r. In that case we need fix the relocation position and
304 // target. No relocations are applied.
305 if (this->Header->sh_type == SHT_RELA) {
306 this->copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
307 return;
308 }
309 if (this->Header->sh_type == SHT_REL) {
310 this->copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
311 return;
312 }
313
Rui Ueyama55c3f892015-10-15 01:58:40 +0000314 memcpy(Buf + OutSecOff, Data.data(), Data.size());
Rafael Espindola4ea00212015-09-21 22:01:00 +0000315
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000316 uint8_t *BufEnd = Buf + OutSecOff + Data.size();
Rafael Espindola4ea00212015-09-21 22:01:00 +0000317 // Iterate over all relocation sections that apply to this section.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000318 for (const Elf_Shdr *RelSec : this->RelocSections) {
Rafael Espindola4ea00212015-09-21 22:01:00 +0000319 if (RelSec->sh_type == SHT_RELA)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000320 this->relocate(Buf, BufEnd, EObj.relas(RelSec));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000321 else
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000322 this->relocate(Buf, BufEnd, EObj.rels(RelSec));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000323 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000324}
325
Rafael Espindolac159c962015-10-19 21:00:02 +0000326template <class ELFT>
Rui Ueyama0b289522016-02-25 18:43:51 +0000327void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
328 this->Align = std::max(this->Align, Other->Align);
329 Other->Repl = this->Repl;
330 Other->Live = false;
331}
332
333template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000334SplitInputSection<ELFT>::SplitInputSection(
Rafael Espindolab20dcb12016-03-11 16:23:45 +0000335 elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000336 typename InputSectionBase<ELFT>::Kind SectionKind)
337 : InputSectionBase<ELFT>(File, Header, SectionKind) {}
338
339template <class ELFT>
Rafael Espindolab20dcb12016-03-11 16:23:45 +0000340EHInputSection<ELFT>::EHInputSection(elf::ObjectFile<ELFT> *F,
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000341 const Elf_Shdr *Header)
Rui Ueyamada735322015-12-24 10:08:54 +0000342 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
343 // Mark .eh_frame sections as live by default because there are
344 // usually no relocations that point to .eh_frames. Otherwise,
George Rimare9e1d322016-02-18 15:17:01 +0000345 // the garbage collector would drop all .eh_frame sections.
Rui Ueyamada735322015-12-24 10:08:54 +0000346 this->Live = true;
347}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000348
349template <class ELFT>
350bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
351 return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
352}
353
354template <class ELFT>
355typename EHInputSection<ELFT>::uintX_t
356EHInputSection<ELFT>::getOffset(uintX_t Offset) {
George Rimar6ab275c2015-12-25 09:51:42 +0000357 // The file crtbeginT.o has relocations pointing to the start of an empty
358 // .eh_frame that is known to be the first in the link. It does that to
359 // identify the start of the output .eh_frame. Handle this special case.
360 if (this->getSectionHdr()->sh_size == 0)
361 return Offset;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000362 std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
363 uintX_t Base = I->second;
George Rimar4b40ebc2015-11-13 13:44:59 +0000364 if (Base == uintX_t(-1))
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000365 return -1; // Not in the output
366
367 uintX_t Addend = Offset - I->first;
368 return Base + Addend;
369}
370
371template <class ELFT>
Rafael Espindola36a73d22016-03-11 16:32:46 +0000372MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
Rafael Espindolac159c962015-10-19 21:00:02 +0000373 const Elf_Shdr *Header)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000374 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
Rafael Espindolac159c962015-10-19 21:00:02 +0000375
376template <class ELFT>
377bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000378 return S->SectionKind == InputSectionBase<ELFT>::Merge;
Rafael Espindolac159c962015-10-19 21:00:02 +0000379}
380
Rafael Espindolac159c962015-10-19 21:00:02 +0000381template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000382std::pair<std::pair<typename ELFFile<ELFT>::uintX_t,
383 typename ELFFile<ELFT>::uintX_t> *,
384 typename ELFFile<ELFT>::uintX_t>
385SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000386 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama7ba639b2015-10-25 16:25:04 +0000387 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000388 uintX_t Size = Data.size();
389 if (Offset >= Size)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000390 fatal("Entry is past the end of the section");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000391
392 // Find the element this offset points to.
393 auto I = std::upper_bound(
Rafael Espindolad04c12a2015-11-11 15:20:45 +0000394 Offsets.begin(), Offsets.end(), Offset,
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000395 [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000396 return A < B.first;
397 });
Rafael Espindola32994992015-11-11 15:40:37 +0000398 uintX_t End = I == Offsets.end() ? Data.size() : I->first;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000399 --I;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000400 return std::make_pair(&*I, End);
401}
402
403template <class ELFT>
404typename MergeInputSection<ELFT>::uintX_t
405MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
George Rimar4b40ebc2015-11-13 13:44:59 +0000406 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000407 this->getRangeAndSize(Offset);
408 std::pair<uintX_t, uintX_t> *I = T.first;
409 uintX_t End = T.second;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000410 uintX_t Start = I->first;
411
412 // Compute the Addend and if the Base is cached, return.
413 uintX_t Addend = Offset - Start;
Rafael Espindola32994992015-11-11 15:40:37 +0000414 uintX_t &Base = I->second;
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000415 if (Base != uintX_t(-1))
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000416 return Base + Addend;
417
Hal Finkelf9505952015-11-25 23:54:53 +0000418 // Map the base to the offset in the output section and cache it.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000419 ArrayRef<uint8_t> D = this->getSectionData();
420 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000421 StringRef Entry = Data.substr(Start, End - Start);
422 Base =
423 static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
424 return Base + Addend;
Rafael Espindola5d83ccd2015-08-13 19:18:30 +0000425}
426
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000427template <class ELFT>
Rafael Espindolab20dcb12016-03-11 16:23:45 +0000428MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
Rui Ueyama70eed362016-01-06 22:42:43 +0000429 const Elf_Shdr *Hdr)
430 : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
431 // Initialize this->Reginfo.
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000432 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama70eed362016-01-06 22:42:43 +0000433 if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000434 fatal("Invalid size of .reginfo section");
Rui Ueyama70eed362016-01-06 22:42:43 +0000435 Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
Simon Atanasyan57830b62015-12-25 13:02:13 +0000436}
437
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000438template <class ELFT>
439bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
440 return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
441}
442
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000443template class elf::InputSectionBase<ELF32LE>;
444template class elf::InputSectionBase<ELF32BE>;
445template class elf::InputSectionBase<ELF64LE>;
446template class elf::InputSectionBase<ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000447
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000448template class elf::InputSection<ELF32LE>;
449template class elf::InputSection<ELF32BE>;
450template class elf::InputSection<ELF64LE>;
451template class elf::InputSection<ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000452
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000453template class elf::EHInputSection<ELF32LE>;
454template class elf::EHInputSection<ELF32BE>;
455template class elf::EHInputSection<ELF64LE>;
456template class elf::EHInputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000457
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000458template class elf::MergeInputSection<ELF32LE>;
459template class elf::MergeInputSection<ELF32BE>;
460template class elf::MergeInputSection<ELF64LE>;
461template class elf::MergeInputSection<ELF64BE>;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000462
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000463template class elf::MipsReginfoInputSection<ELF32LE>;
464template class elf::MipsReginfoInputSection<ELF32BE>;
465template class elf::MipsReginfoInputSection<ELF64LE>;
466template class elf::MipsReginfoInputSection<ELF64BE>;