blob: b594638b2735d348d0fe8c9556d2e1a2ba1253ab [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)
Rui Ueyama0b289522016-02-25 18:43:51 +000028 : Header(Header), File(File), SectionKind(SectionKind), Repl(this) {
Rui Ueyama8fc070d2016-02-24 00:23:15 +000029 // The garbage collector sets sections' Live bits.
30 // If GC is disabled, all sections are considered live by default.
Rui Ueyama733153d2016-02-24 18:33:35 +000031 Live = !Config->GcSections;
Rui Ueyama5ac58912016-02-24 00:38:18 +000032
33 // The ELF spec states that a value of 0 means the section has
34 // no alignment constraits.
Rui Ueyama733153d2016-02-24 18:33:35 +000035 Align = std::max<uintX_t>(Header->sh_addralign, 1);
Rui Ueyama8fc070d2016-02-24 00:23:15 +000036}
Rafael Espindolac159c962015-10-19 21:00:02 +000037
38template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
39 ErrorOr<StringRef> Name = File->getObj().getSectionName(this->Header);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000040 fatal(Name);
Rafael Espindolac159c962015-10-19 21:00:02 +000041 return *Name;
42}
43
44template <class ELFT>
45ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
46 ErrorOr<ArrayRef<uint8_t>> Ret =
47 this->File->getObj().getSectionContents(this->Header);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000048 fatal(Ret);
Rafael Espindolac159c962015-10-19 21:00:02 +000049 return *Ret;
50}
51
52template <class ELFT>
53typename ELFFile<ELFT>::uintX_t
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000054InputSectionBase<ELFT>::getOffset(uintX_t Offset) {
55 switch (SectionKind) {
56 case Regular:
57 return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000058 case EHFrame:
59 return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000060 case Merge:
61 return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
Simon Atanasyan1d7df402015-12-20 10:57:34 +000062 case MipsReginfo:
Rui Ueyama58a636a2016-01-06 22:01:25 +000063 // MIPS .reginfo sections are consumed by the linker,
64 // so it should never be copied to output.
65 llvm_unreachable("MIPS .reginfo reached writeTo().");
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000066 }
Denis Protivensky1b1b34e2015-11-12 09:11:20 +000067 llvm_unreachable("Invalid section kind");
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000068}
69
70template <class ELFT>
71typename ELFFile<ELFT>::uintX_t
Rafael Espindola48225b42015-10-23 19:55:11 +000072InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000073 return getOffset(Sym.st_value);
Rafael Espindolac159c962015-10-19 21:00:02 +000074}
75
Rui Ueyama12504642015-10-27 21:51:13 +000076// Returns a section that Rel relocation is pointing to.
77template <class ELFT>
78InputSectionBase<ELFT> *
Rui Ueyamad7e4a282016-02-24 00:23:13 +000079InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) const {
Rui Ueyama12504642015-10-27 21:51:13 +000080 // Global symbol
81 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
82 if (SymbolBody *B = File->getSymbolBody(SymIndex))
83 if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B->repl()))
Rui Ueyama0b289522016-02-25 18:43:51 +000084 return D->Section->Repl;
Rui Ueyama12504642015-10-27 21:51:13 +000085 // Local symbol
86 if (const Elf_Sym *Sym = File->getLocalSymbol(SymIndex))
87 if (InputSectionBase<ELFT> *Sec = File->getSection(*Sym))
88 return Sec;
89 return nullptr;
90}
91
92template <class ELFT>
93InputSectionBase<ELFT> *
Rui Ueyamad7e4a282016-02-24 00:23:13 +000094InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) const {
Rui Ueyama12504642015-10-27 21:51:13 +000095 return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel));
96}
97
Rafael Espindolac159c962015-10-19 21:00:02 +000098template <class ELFT>
Rafael Espindola53d5cea2015-09-21 17:47:00 +000099InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header)
Rafael Espindolac159c962015-10-19 21:00:02 +0000100 : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
101
102template <class ELFT>
103bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
104 return S->SectionKind == Base::Regular;
105}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000106
Rafael Espindola4ea00212015-09-21 22:01:00 +0000107template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000108InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() {
109 assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL);
110 ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections();
111 return Sections[this->Header->sh_info];
112}
113
114// This is used for -r. We can't use memcpy to copy relocations because we need
115// to update symbol table offset and section index for each relocation. So we
116// copy relocations one by one.
117template <class ELFT>
118template <bool isRela>
119void InputSection<ELFT>::copyRelocations(uint8_t *Buf,
120 RelIteratorRange<isRela> Rels) {
121 typedef Elf_Rel_Impl<ELFT, isRela> RelType;
122 InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
123
124 for (const RelType &Rel : Rels) {
125 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
126 uint32_t Type = Rel.getType(Config->Mips64EL);
127 const Elf_Shdr *SymTab = this->File->getSymbolTable();
128
129 RelType *P = reinterpret_cast<RelType *>(Buf);
130 Buf += sizeof(RelType);
131
132 // Relocation for local symbol here means that it is probably
133 // rel[a].eh_frame section which has references to
134 // sections in r_info field. Also needs fix for addend.
135 if (SymIndex < SymTab->sh_info)
136 fatal("Relocation against local symbols is not supported yet");
137
138 SymbolBody *Body = this->File->getSymbolBody(SymIndex)->repl();
139 P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
140 P->setSymbolAndType(Body->DynsymIndex, Type, Config->Mips64EL);
141 }
142}
143
144template <class ELFT>
Rafael Espindola4ea00212015-09-21 22:01:00 +0000145template <bool isRela>
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000146uint8_t *
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000147InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf, uint32_t SymIndex,
148 uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000149 RelIteratorRange<isRela> Rels) {
150 // Some MIPS relocations use addend calculated from addend of the relocation
151 // itself and addend of paired relocation. ABI requires to compute such
152 // combined addend in case of REL relocation record format only.
153 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
154 if (isRela || Config->EMachine != EM_MIPS)
155 return nullptr;
156 if (Type == R_MIPS_HI16)
157 Type = R_MIPS_LO16;
158 else if (Type == R_MIPS_PCHI16)
159 Type = R_MIPS_PCLO16;
160 else if (Type == R_MICROMIPS_HI16)
161 Type = R_MICROMIPS_LO16;
162 else
163 return nullptr;
164 for (const auto &RI : Rels) {
165 if (RI.getType(Config->Mips64EL) != Type)
166 continue;
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000167 if (RI.getSymbol(Config->Mips64EL) != SymIndex)
168 continue;
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000169 uintX_t Offset = getOffset(RI.r_offset);
170 if (Offset == (uintX_t)-1)
171 return nullptr;
172 return Buf + Offset;
173 }
174 return nullptr;
175}
176
177template <class ELFT>
178template <bool isRela>
179void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd,
180 RelIteratorRange<isRela> Rels) {
Rafael Espindola4ea00212015-09-21 22:01:00 +0000181 typedef Elf_Rel_Impl<ELFT, isRela> RelType;
George Rimar6713cf82015-11-25 21:46:05 +0000182 size_t Num = Rels.end() - Rels.begin();
183 for (size_t I = 0; I < Num; ++I) {
184 const RelType &RI = *(Rels.begin() + I);
Rui Ueyama64558522015-10-16 22:51:43 +0000185 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
186 uint32_t Type = RI.getType(Config->Mips64EL);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000187 uintX_t Offset = getOffset(RI.r_offset);
188 if (Offset == (uintX_t)-1)
189 continue;
190
191 uint8_t *BufLoc = Buf + Offset;
192 uintX_t AddrLoc = OutSec->getVA() + Offset;
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000193 auto NextRelocs = llvm::make_range(&RI, Rels.end());
Michael J. Spencer1e225612015-11-11 01:00:24 +0000194
Rui Ueyamac516ae12016-01-29 02:33:45 +0000195 if (Target->isTlsLocalDynamicRel(Type) &&
Rui Ueyamabaf16512016-01-29 00:20:12 +0000196 !Target->canRelaxTls(Type, nullptr)) {
Michael J. Spencer1e225612015-11-11 01:00:24 +0000197 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000198 Out<ELFT>::Got->getTlsIndexVA() +
Michael J. Spencer1e225612015-11-11 01:00:24 +0000199 getAddend<ELFT>(RI));
200 continue;
201 }
Rafael Espindola4ea00212015-09-21 22:01:00 +0000202
George Rimar0b8ed1d2015-12-21 10:37:33 +0000203 const Elf_Shdr *SymTab = File->getSymbolTable();
204 SymbolBody *Body = nullptr;
205 if (SymIndex >= SymTab->sh_info)
206 Body = File->getSymbolBody(SymIndex)->repl();
207
Rui Ueyamabaf16512016-01-29 00:20:12 +0000208 if (Target->canRelaxTls(Type, Body)) {
George Rimar0b8ed1d2015-12-21 10:37:33 +0000209 uintX_t SymVA;
210 if (!Body)
211 SymVA = getLocalRelTarget(*File, RI, 0);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000212 else if (Target->needsGot(Type, *Body))
Rui Ueyamab5a69702016-02-01 21:00:35 +0000213 SymVA = Body->getGotVA<ELFT>();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000214 else
Rui Ueyamab5a69702016-02-01 21:00:35 +0000215 SymVA = Body->getVA<ELFT>();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000216 // By optimizing TLS relocations, it is sometimes needed to skip
217 // relocations that immediately follow TLS relocations. This function
218 // knows how many slots we need to skip.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000219 I += Target->relaxTls(BufLoc, BufEnd, Type, AddrLoc, SymVA, Body);
George Rimar0b8ed1d2015-12-21 10:37:33 +0000220 continue;
221 }
222
Rafael Espindola4ea00212015-09-21 22:01:00 +0000223 // Handle relocations for local symbols -- they never get
224 // resolved so we don't allocate a SymbolBody.
George Rimar0b8ed1d2015-12-21 10:37:33 +0000225 uintX_t A = getAddend<ELFT>(RI);
226 if (!Body) {
227 uintX_t SymVA = getLocalRelTarget(*File, RI, A);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000228 if (Config->EMachine == EM_MIPS) {
229 if (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32)
230 // We need to adjust SymVA value in case of R_MIPS_GPREL16/32
231 // relocations because they use the following expression to calculate
232 // the relocation's result for local symbol: S + A + GP0 - G.
233 SymVA += File->getMipsGp0();
234 else if (Type == R_MIPS_GOT16)
235 // R_MIPS_GOT16 relocation against local symbol requires index of
236 // a local GOT entry which contains page address corresponds
237 // to the symbol address.
238 SymVA = Out<ELFT>::Got->getMipsLocalPageAddr(SymVA);
239 }
George Rimar48651482015-12-11 08:59:37 +0000240 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0,
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000241 findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs));
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000242 continue;
Rafael Espindola4ea00212015-09-21 22:01:00 +0000243 }
244
Rui Ueyamac516ae12016-01-29 02:33:45 +0000245 if (Target->isTlsGlobalDynamicRel(Type) &&
Rui Ueyamabaf16512016-01-29 00:20:12 +0000246 !Target->canRelaxTls(Type, Body)) {
Michael J. Spencer627ae702015-11-13 00:28:34 +0000247 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
George Rimar0b8ed1d2015-12-21 10:37:33 +0000248 Out<ELFT>::Got->getGlobalDynAddr(*Body) +
Michael J. Spencer627ae702015-11-13 00:28:34 +0000249 getAddend<ELFT>(RI));
250 continue;
251 }
252
Rui Ueyamab5a69702016-02-01 21:00:35 +0000253 uintX_t SymVA = Body->getVA<ELFT>();
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000254 if (Target->needsPlt<ELFT>(Type, *Body)) {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000255 SymVA = Body->getPltVA<ELFT>();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000256 } else if (Target->needsGot(Type, *Body)) {
Simon Atanasyan4b034512016-02-04 11:51:45 +0000257 if (Config->EMachine == EM_MIPS && !canBePreempted(Body, true))
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000258 // Under some conditions relocations against non-local symbols require
259 // entries in the local part of MIPS GOT. In that case we need an entry
260 // initialized by full address of the symbol.
261 SymVA = Out<ELFT>::Got->getMipsLocalFullAddr(*Body);
262 else
Rui Ueyamab5a69702016-02-01 21:00:35 +0000263 SymVA = Body->getGotVA<ELFT>();
Rafael Espindola5e8b54a2016-02-22 23:16:05 +0000264 if (Body->IsTls)
Rui Ueyama724d6252016-01-29 01:49:32 +0000265 Type = Target->getTlsGotRel(Type);
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000266 } else if (!Target->needsCopyRel<ELFT>(Type, *Body) &&
George Rimar0b8ed1d2015-12-21 10:37:33 +0000267 isa<SharedSymbol<ELFT>>(*Body)) {
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000268 continue;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000269 } else if (Target->isTlsDynRel(Type, *Body)) {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000270 continue;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000271 } else if (Target->isSizeRel(Type) && canBePreempted(Body, false)) {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000272 // A SIZE relocation is supposed to set a symbol size, but if a symbol
273 // can be preempted, the size at runtime may be different than link time.
274 // If that's the case, we leave the field alone rather than filling it
275 // with a possibly incorrect value.
George Rimard23970f2015-11-25 20:41:53 +0000276 continue;
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000277 } else if (Config->EMachine == EM_MIPS) {
George Rimar0b8ed1d2015-12-21 10:37:33 +0000278 if (Type == R_MIPS_HI16 && Body == Config->MipsGpDisp)
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000279 SymVA = getMipsGpAddr<ELFT>() - AddrLoc;
George Rimar0b8ed1d2015-12-21 10:37:33 +0000280 else if (Type == R_MIPS_LO16 && Body == Config->MipsGpDisp)
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000281 SymVA = getMipsGpAddr<ELFT>() - AddrLoc + 4;
Simon Atanasyan597df212016-02-04 12:09:49 +0000282 else if (Body == Config->MipsLocalGp)
283 SymVA = getMipsGpAddr<ELFT>();
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000284 }
Rui Ueyama512c61d2016-02-03 00:12:24 +0000285 uintX_t Size = Body->getSize<ELFT>();
George Rimar48651482015-12-11 08:59:37 +0000286 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + A, Size + A,
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000287 findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000288 }
289}
290
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000291template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000292 if (this->Header->sh_type == SHT_NOBITS)
Michael J. Spencer84487f12015-07-24 21:03:07 +0000293 return;
294 // Copy section contents from source object file to output file.
Rafael Espindolac159c962015-10-19 21:00:02 +0000295 ArrayRef<uint8_t> Data = this->getSectionData();
George Rimar58941ee2016-02-25 08:23:37 +0000296 ELFFile<ELFT> &EObj = this->File->getObj();
297
298 // That happens with -r. In that case we need fix the relocation position and
299 // target. No relocations are applied.
300 if (this->Header->sh_type == SHT_RELA) {
301 this->copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
302 return;
303 }
304 if (this->Header->sh_type == SHT_REL) {
305 this->copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
306 return;
307 }
308
Rui Ueyama55c3f892015-10-15 01:58:40 +0000309 memcpy(Buf + OutSecOff, Data.data(), Data.size());
Rafael Espindola4ea00212015-09-21 22:01:00 +0000310
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000311 uint8_t *BufEnd = Buf + OutSecOff + Data.size();
Rafael Espindola4ea00212015-09-21 22:01:00 +0000312 // Iterate over all relocation sections that apply to this section.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000313 for (const Elf_Shdr *RelSec : this->RelocSections) {
Rafael Espindola4ea00212015-09-21 22:01:00 +0000314 if (RelSec->sh_type == SHT_RELA)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000315 this->relocate(Buf, BufEnd, EObj.relas(RelSec));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000316 else
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000317 this->relocate(Buf, BufEnd, EObj.rels(RelSec));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000318 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000319}
320
Rafael Espindolac159c962015-10-19 21:00:02 +0000321template <class ELFT>
Rui Ueyama0b289522016-02-25 18:43:51 +0000322void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
323 this->Align = std::max(this->Align, Other->Align);
324 Other->Repl = this->Repl;
325 Other->Live = false;
326}
327
328template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000329SplitInputSection<ELFT>::SplitInputSection(
330 ObjectFile<ELFT> *File, const Elf_Shdr *Header,
331 typename InputSectionBase<ELFT>::Kind SectionKind)
332 : InputSectionBase<ELFT>(File, Header, SectionKind) {}
333
334template <class ELFT>
335EHInputSection<ELFT>::EHInputSection(ObjectFile<ELFT> *F,
336 const Elf_Shdr *Header)
Rui Ueyamada735322015-12-24 10:08:54 +0000337 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
338 // Mark .eh_frame sections as live by default because there are
339 // usually no relocations that point to .eh_frames. Otherwise,
George Rimare9e1d322016-02-18 15:17:01 +0000340 // the garbage collector would drop all .eh_frame sections.
Rui Ueyamada735322015-12-24 10:08:54 +0000341 this->Live = true;
342}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000343
344template <class ELFT>
345bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
346 return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
347}
348
349template <class ELFT>
350typename EHInputSection<ELFT>::uintX_t
351EHInputSection<ELFT>::getOffset(uintX_t Offset) {
George Rimar6ab275c2015-12-25 09:51:42 +0000352 // The file crtbeginT.o has relocations pointing to the start of an empty
353 // .eh_frame that is known to be the first in the link. It does that to
354 // identify the start of the output .eh_frame. Handle this special case.
355 if (this->getSectionHdr()->sh_size == 0)
356 return Offset;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000357 std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
358 uintX_t Base = I->second;
George Rimar4b40ebc2015-11-13 13:44:59 +0000359 if (Base == uintX_t(-1))
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000360 return -1; // Not in the output
361
362 uintX_t Addend = Offset - I->first;
363 return Base + Addend;
364}
365
366template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000367MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F,
368 const Elf_Shdr *Header)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000369 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
Rafael Espindolac159c962015-10-19 21:00:02 +0000370
371template <class ELFT>
372bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000373 return S->SectionKind == InputSectionBase<ELFT>::Merge;
Rafael Espindolac159c962015-10-19 21:00:02 +0000374}
375
Rafael Espindolac159c962015-10-19 21:00:02 +0000376template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000377std::pair<std::pair<typename ELFFile<ELFT>::uintX_t,
378 typename ELFFile<ELFT>::uintX_t> *,
379 typename ELFFile<ELFT>::uintX_t>
380SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000381 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama7ba639b2015-10-25 16:25:04 +0000382 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000383 uintX_t Size = Data.size();
384 if (Offset >= Size)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000385 fatal("Entry is past the end of the section");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000386
387 // Find the element this offset points to.
388 auto I = std::upper_bound(
Rafael Espindolad04c12a2015-11-11 15:20:45 +0000389 Offsets.begin(), Offsets.end(), Offset,
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000390 [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000391 return A < B.first;
392 });
Rafael Espindola32994992015-11-11 15:40:37 +0000393 uintX_t End = I == Offsets.end() ? Data.size() : I->first;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000394 --I;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000395 return std::make_pair(&*I, End);
396}
397
398template <class ELFT>
399typename MergeInputSection<ELFT>::uintX_t
400MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
George Rimar4b40ebc2015-11-13 13:44:59 +0000401 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000402 this->getRangeAndSize(Offset);
403 std::pair<uintX_t, uintX_t> *I = T.first;
404 uintX_t End = T.second;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000405 uintX_t Start = I->first;
406
407 // Compute the Addend and if the Base is cached, return.
408 uintX_t Addend = Offset - Start;
Rafael Espindola32994992015-11-11 15:40:37 +0000409 uintX_t &Base = I->second;
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000410 if (Base != uintX_t(-1))
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000411 return Base + Addend;
412
Hal Finkelf9505952015-11-25 23:54:53 +0000413 // Map the base to the offset in the output section and cache it.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000414 ArrayRef<uint8_t> D = this->getSectionData();
415 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000416 StringRef Entry = Data.substr(Start, End - Start);
417 Base =
418 static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
419 return Base + Addend;
Rafael Espindola5d83ccd2015-08-13 19:18:30 +0000420}
421
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000422template <class ELFT>
423MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(ObjectFile<ELFT> *F,
Rui Ueyama70eed362016-01-06 22:42:43 +0000424 const Elf_Shdr *Hdr)
425 : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
426 // Initialize this->Reginfo.
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000427 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama70eed362016-01-06 22:42:43 +0000428 if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000429 fatal("Invalid size of .reginfo section");
Rui Ueyama70eed362016-01-06 22:42:43 +0000430 Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
Simon Atanasyan57830b62015-12-25 13:02:13 +0000431}
432
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000433template <class ELFT>
434bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
435 return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
436}
437
Rafael Espindola25472ee2016-01-22 21:49:07 +0000438template class elf2::InputSectionBase<ELF32LE>;
439template class elf2::InputSectionBase<ELF32BE>;
440template class elf2::InputSectionBase<ELF64LE>;
441template class elf2::InputSectionBase<ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000442
Rafael Espindola25472ee2016-01-22 21:49:07 +0000443template class elf2::InputSection<ELF32LE>;
444template class elf2::InputSection<ELF32BE>;
445template class elf2::InputSection<ELF64LE>;
446template class elf2::InputSection<ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000447
Rafael Espindola25472ee2016-01-22 21:49:07 +0000448template class elf2::EHInputSection<ELF32LE>;
449template class elf2::EHInputSection<ELF32BE>;
450template class elf2::EHInputSection<ELF64LE>;
451template class elf2::EHInputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000452
Rafael Espindola25472ee2016-01-22 21:49:07 +0000453template class elf2::MergeInputSection<ELF32LE>;
454template class elf2::MergeInputSection<ELF32BE>;
455template class elf2::MergeInputSection<ELF64LE>;
456template class elf2::MergeInputSection<ELF64BE>;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000457
Rafael Espindola25472ee2016-01-22 21:49:07 +0000458template class elf2::MipsReginfoInputSection<ELF32LE>;
459template class elf2::MipsReginfoInputSection<ELF32BE>;
460template class elf2::MipsReginfoInputSection<ELF64LE>;
461template class elf2::MipsReginfoInputSection<ELF64BE>;