blob: 4929e0339ff3bcce69f8f1244eab786291052589 [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>
Rui Ueyamaf7149552016-03-11 18:46:51 +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>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000051typename ELFT::uint InputSectionBase<ELFT>::getOffset(uintX_t Offset) {
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000052 switch (SectionKind) {
53 case Regular:
54 return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000055 case EHFrame:
56 return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000057 case Merge:
58 return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
Simon Atanasyan1d7df402015-12-20 10:57:34 +000059 case MipsReginfo:
Rui Ueyama58a636a2016-01-06 22:01:25 +000060 // MIPS .reginfo sections are consumed by the linker,
61 // so it should never be copied to output.
62 llvm_unreachable("MIPS .reginfo reached writeTo().");
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000063 }
George Rimar777f9632016-03-12 08:31:34 +000064 llvm_unreachable("invalid section kind");
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000065}
66
67template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000068typename ELFT::uint InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000069 return getOffset(Sym.st_value);
Rafael Espindolac159c962015-10-19 21:00:02 +000070}
71
Rui Ueyama12504642015-10-27 21:51:13 +000072// Returns a section that Rel relocation is pointing to.
73template <class ELFT>
74InputSectionBase<ELFT> *
Rui Ueyamad7e4a282016-02-24 00:23:13 +000075InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) const {
Rui Ueyama12504642015-10-27 21:51:13 +000076 // Global symbol
77 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
Rafael Espindola67d72c02016-03-11 12:06:30 +000078 SymbolBody &B = File->getSymbolBody(SymIndex).repl();
79 if (auto *D = dyn_cast<DefinedRegular<ELFT>>(&B))
Rui Ueyama54945f62016-03-13 20:34:34 +000080 if (D->Section)
81 return D->Section->Repl;
Rui Ueyama12504642015-10-27 21:51:13 +000082 return nullptr;
83}
84
85template <class ELFT>
86InputSectionBase<ELFT> *
Rui Ueyamad7e4a282016-02-24 00:23:13 +000087InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) const {
Rui Ueyama12504642015-10-27 21:51:13 +000088 return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel));
89}
90
Rafael Espindolac159c962015-10-19 21:00:02 +000091template <class ELFT>
Rui Ueyamaf7149552016-03-11 18:46:51 +000092InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F,
93 const Elf_Shdr *Header)
Rafael Espindolac159c962015-10-19 21:00:02 +000094 : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
95
96template <class ELFT>
97bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
98 return S->SectionKind == Base::Regular;
99}
Michael J. Spencer84487f12015-07-24 21:03:07 +0000100
Rafael Espindola4ea00212015-09-21 22:01:00 +0000101template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000102InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() {
103 assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL);
104 ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections();
105 return Sections[this->Header->sh_info];
106}
107
108// This is used for -r. We can't use memcpy to copy relocations because we need
109// to update symbol table offset and section index for each relocation. So we
110// copy relocations one by one.
111template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +0000112template <class RelTy>
George Rimar58941ee2016-02-25 08:23:37 +0000113void InputSection<ELFT>::copyRelocations(uint8_t *Buf,
Rui Ueyamafc467e72016-03-13 05:06:50 +0000114 iterator_range<const RelTy *> Rels) {
George Rimar58941ee2016-02-25 08:23:37 +0000115 InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
116
Rui Ueyamafc467e72016-03-13 05:06:50 +0000117 for (const RelTy &Rel : Rels) {
George Rimar58941ee2016-02-25 08:23:37 +0000118 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
119 uint32_t Type = Rel.getType(Config->Mips64EL);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000120 SymbolBody &Body = this->File->getSymbolBody(SymIndex).repl();
George Rimar58941ee2016-02-25 08:23:37 +0000121
Rui Ueyamafc467e72016-03-13 05:06:50 +0000122 RelTy *P = reinterpret_cast<RelTy *>(Buf);
123 Buf += sizeof(RelTy);
George Rimar58941ee2016-02-25 08:23:37 +0000124
George Rimar58941ee2016-02-25 08:23:37 +0000125 P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000126 P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL);
George Rimar58941ee2016-02-25 08:23:37 +0000127 }
128}
129
Rui Ueyama20398472016-03-13 03:09:40 +0000130template <class RelTy>
131static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) {
Rui Ueyama20398472016-03-13 03:09:40 +0000132 switch (Rel->getType(Config->Mips64EL)) {
Simon Atanasyan860fbf02016-02-25 21:33:56 +0000133 case R_MIPS_HI16:
134 return R_MIPS_LO16;
Simon Atanasyan92a32552016-03-12 11:58:15 +0000135 case R_MIPS_GOT16:
Rui Ueyama20398472016-03-13 03:09:40 +0000136 return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE;
Simon Atanasyan860fbf02016-02-25 21:33:56 +0000137 case R_MIPS_PCHI16:
138 return R_MIPS_PCLO16;
139 case R_MICROMIPS_HI16:
140 return R_MICROMIPS_LO16;
141 default:
142 return R_MIPS_NONE;
143 }
144}
145
Rafael Espindola163974d2016-03-29 23:05:59 +0000146template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
147 return read32<E>(Loc) & 0xffff;
148}
149
George Rimar58941ee2016-02-25 08:23:37 +0000150template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +0000151template <class RelTy>
Rafael Espindola163974d2016-03-29 23:05:59 +0000152int32_t
153InputSectionBase<ELFT>::findMipsPairedAddend(uint8_t *Buf, uint8_t *BufLoc,
154 SymbolBody &Sym, const RelTy *Rel,
155 const RelTy *End) {
Rui Ueyama20398472016-03-13 03:09:40 +0000156 uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL);
Rui Ueyama20398472016-03-13 03:09:40 +0000157 uint32_t Type = getMipsPairType(Rel, Sym);
158
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000159 // Some MIPS relocations use addend calculated from addend of the relocation
160 // itself and addend of paired relocation. ABI requires to compute such
161 // combined addend in case of REL relocation record format only.
162 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rui Ueyamafc467e72016-03-13 05:06:50 +0000163 if (RelTy::IsRela || Type == R_MIPS_NONE)
Rafael Espindola163974d2016-03-29 23:05:59 +0000164 return 0;
Rui Ueyama20398472016-03-13 03:09:40 +0000165
Rui Ueyamafc467e72016-03-13 05:06:50 +0000166 for (const RelTy *RI = Rel; RI != End; ++RI) {
Rui Ueyama20398472016-03-13 03:09:40 +0000167 if (RI->getType(Config->Mips64EL) != Type)
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000168 continue;
Rui Ueyama20398472016-03-13 03:09:40 +0000169 if (RI->getSymbol(Config->Mips64EL) != SymIndex)
Simon Atanasyandddbeb72015-12-13 06:49:08 +0000170 continue;
Rui Ueyama20398472016-03-13 03:09:40 +0000171 uintX_t Offset = getOffset(RI->r_offset);
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000172 if (Offset == (uintX_t)-1)
Rafael Espindola163974d2016-03-29 23:05:59 +0000173 break;
174 const endianness E = ELFT::TargetEndianness;
175 return ((read32<E>(BufLoc) & 0xffff) << 16) +
176 readSignedLo16<E>(Buf + Offset);
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000177 }
Rafael Espindola163974d2016-03-29 23:05:59 +0000178 unsigned OldType = Rel->getType(Config->Mips64EL);
179 StringRef OldName = getELFRelocationTypeName(Config->EMachine, OldType);
180 StringRef NewName = getELFRelocationTypeName(Config->EMachine, Type);
181 warning("can't find matching " + NewName + " relocation for " + OldName);
182 return 0;
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000183}
184
Simon Atanasyan604aee12016-03-13 15:37:38 +0000185template <class ELFT, class uintX_t>
Simon Atanasyand27e61c2016-03-13 15:48:41 +0000186static uintX_t adjustMipsSymVA(uint32_t Type, const elf::ObjectFile<ELFT> &File,
Simon Atanasyan604aee12016-03-13 15:37:38 +0000187 const SymbolBody &Body, uintX_t AddrLoc,
188 uintX_t SymVA) {
189 if (Type == R_MIPS_HI16 && &Body == Config->MipsGpDisp)
190 return getMipsGpAddr<ELFT>() - AddrLoc;
191 if (Type == R_MIPS_LO16 && &Body == Config->MipsGpDisp)
192 return getMipsGpAddr<ELFT>() - AddrLoc + 4;
193 if (&Body == Config->MipsLocalGp)
194 return getMipsGpAddr<ELFT>();
195 if (Body.isLocal() && (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32))
196 // We need to adjust SymVA value in case of R_MIPS_GPREL16/32
197 // relocations because they use the following expression to calculate
198 // the relocation's result for local symbol: S + A + GP0 - G.
199 return SymVA + File.getMipsGp0();
200 return SymVA;
201}
202
203template <class ELFT, class uintX_t>
204static uintX_t getMipsGotVA(const SymbolBody &Body, uintX_t SymVA,
Rafael Espindolaabc9a122016-03-30 12:45:58 +0000205 uint8_t *BufLoc) {
Rafael Espindola163974d2016-03-29 23:05:59 +0000206 if (Body.isLocal())
Simon Atanasyan604aee12016-03-13 15:37:38 +0000207 // If relocation against MIPS local symbol requires GOT entry, this entry
208 // should be initialized by 'page address'. This address is high 16-bits
Rafael Espindolaabc9a122016-03-30 12:45:58 +0000209 // of sum the symbol's value and the addend.
210 return Out<ELFT>::Got->getMipsLocalPageAddr(SymVA);
Rui Ueyamac4466602016-03-13 19:48:18 +0000211 if (!Body.isPreemptible())
Simon Atanasyan604aee12016-03-13 15:37:38 +0000212 // For non-local symbols GOT entries should contain their full
213 // addresses. But if such symbol cannot be preempted, we do not
214 // have to put them into the "global" part of GOT and use dynamic
215 // linker to determine their actual addresses. That is why we
216 // create GOT entries for them in the "local" part of GOT.
217 return Out<ELFT>::Got->getMipsLocalFullAddr(Body);
218 return Body.getGotVA<ELFT>();
219}
220
Rafael Espindola69082f02016-03-18 18:11:26 +0000221template <class ELFT>
222template <class RelTy>
223void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd,
224 iterator_range<const RelTy *> Rels) {
225 size_t Num = Rels.end() - Rels.begin();
226 for (size_t I = 0; I < Num; ++I) {
227 const RelTy &RI = *(Rels.begin() + I);
228 uintX_t Offset = getOffset(RI.r_offset);
229 if (Offset == (uintX_t)-1)
230 continue;
231
232 uintX_t A = getAddend<ELFT>(RI);
233 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
234 uint32_t Type = RI.getType(Config->Mips64EL);
235 uint8_t *BufLoc = Buf + Offset;
236 uintX_t AddrLoc = OutSec->getVA() + Offset;
237
238 if (Target->pointsToLocalDynamicGotEntry(Type) &&
239 !Target->canRelaxTls(Type, nullptr)) {
240 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
241 Out<ELFT>::Got->getTlsIndexVA() + A);
242 continue;
243 }
244
245 SymbolBody &Body = File->getSymbolBody(SymIndex).repl();
246
247 if (Target->canRelaxTls(Type, &Body)) {
248 uintX_t SymVA;
249 if (Target->needsGot(Type, Body))
250 SymVA = Body.getGotVA<ELFT>();
251 else
252 SymVA = Body.getVA<ELFT>();
253 // By optimizing TLS relocations, it is sometimes needed to skip
254 // relocations that immediately follow TLS relocations. This function
255 // knows how many slots we need to skip.
256 I += Target->relaxTls(BufLoc, BufEnd, Type, AddrLoc, SymVA, Body);
257 continue;
258 }
259
260 // PPC64 has a special relocation representing the TOC base pointer
261 // that does not have a corresponding symbol.
262 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC) {
263 uintX_t SymVA = getPPC64TocBase() + A;
264 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0);
265 continue;
266 }
267
268 if (Target->isTlsGlobalDynamicRel(Type) &&
269 !Target->canRelaxTls(Type, &Body)) {
270 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
271 Out<ELFT>::Got->getGlobalDynAddr(Body) + A);
272 continue;
273 }
274
Rafael Espindolada99df32016-03-30 12:40:38 +0000275 if (!RelTy::IsRela)
276 A += Target->getImplicitAddend(BufLoc, Type);
Rafael Espindola69082f02016-03-18 18:11:26 +0000277 if (Config->EMachine == EM_MIPS)
Rafael Espindola163974d2016-03-29 23:05:59 +0000278 A += findMipsPairedAddend(Buf, BufLoc, Body, &RI, Rels.end());
Rafael Espindolaabc9a122016-03-30 12:45:58 +0000279 uintX_t SymVA = Body.getVA<ELFT>(A);
Rafael Espindola69082f02016-03-18 18:11:26 +0000280
Rafael Espindola54322872016-03-24 12:55:27 +0000281 if (Target->needsPlt(Type, Body)) {
Rafael Espindola69082f02016-03-18 18:11:26 +0000282 SymVA = Body.getPltVA<ELFT>() + A;
283 } else if (Target->needsGot(Type, Body)) {
284 if (Config->EMachine == EM_MIPS)
Rafael Espindolaabc9a122016-03-30 12:45:58 +0000285 SymVA = getMipsGotVA<ELFT>(Body, SymVA, BufLoc);
Rafael Espindola69082f02016-03-18 18:11:26 +0000286 else
287 SymVA = Body.getGotVA<ELFT>() + A;
288 if (Body.IsTls)
289 Type = Target->getTlsGotRel(Type);
290 } else if (Target->isSizeRel(Type) && Body.isPreemptible()) {
291 // A SIZE relocation is supposed to set a symbol size, but if a symbol
292 // can be preempted, the size at runtime may be different than link time.
293 // If that's the case, we leave the field alone rather than filling it
294 // with a possibly incorrect value.
295 continue;
296 } else if (Config->EMachine == EM_MIPS) {
Rafael Espindolaabc9a122016-03-30 12:45:58 +0000297 SymVA = adjustMipsSymVA<ELFT>(Type, *File, Body, AddrLoc, SymVA);
Rafael Espindola69082f02016-03-18 18:11:26 +0000298 } else if (!Target->needsCopyRel<ELFT>(Type, Body) &&
299 Body.isPreemptible()) {
300 continue;
301 }
302 uintX_t Size = Body.getSize<ELFT>();
Rafael Espindola163974d2016-03-29 23:05:59 +0000303 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, Size + A);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000304 }
305}
306
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000307template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000308 if (this->Header->sh_type == SHT_NOBITS)
Michael J. Spencer84487f12015-07-24 21:03:07 +0000309 return;
George Rimar58941ee2016-02-25 08:23:37 +0000310 ELFFile<ELFT> &EObj = this->File->getObj();
311
Rui Ueyamaf43d1502016-03-30 00:43:49 +0000312 // If -r is given, then an InputSection may be a relocation section.
George Rimar58941ee2016-02-25 08:23:37 +0000313 if (this->Header->sh_type == SHT_RELA) {
Rui Ueyamaf43d1502016-03-30 00:43:49 +0000314 copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
George Rimar58941ee2016-02-25 08:23:37 +0000315 return;
316 }
317 if (this->Header->sh_type == SHT_REL) {
Rui Ueyamaf43d1502016-03-30 00:43:49 +0000318 copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
George Rimar58941ee2016-02-25 08:23:37 +0000319 return;
320 }
321
Rui Ueyamaf43d1502016-03-30 00:43:49 +0000322 // Copy section contents from source object file to output file.
323 ArrayRef<uint8_t> Data = this->getSectionData();
Rui Ueyama55c3f892015-10-15 01:58:40 +0000324 memcpy(Buf + OutSecOff, Data.data(), Data.size());
Rafael Espindola4ea00212015-09-21 22:01:00 +0000325
Rafael Espindola69082f02016-03-18 18:11:26 +0000326 // Iterate over all relocation sections that apply to this section.
Rui Ueyamaf43d1502016-03-30 00:43:49 +0000327 uint8_t *BufEnd = Buf + OutSecOff + Data.size();
Rafael Espindola69082f02016-03-18 18:11:26 +0000328 for (const Elf_Shdr *RelSec : this->RelocSections) {
329 if (RelSec->sh_type == SHT_RELA)
330 this->relocate(Buf, BufEnd, EObj.relas(RelSec));
331 else
332 this->relocate(Buf, BufEnd, EObj.rels(RelSec));
333 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000334}
335
Rafael Espindolac159c962015-10-19 21:00:02 +0000336template <class ELFT>
Rui Ueyama0b289522016-02-25 18:43:51 +0000337void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
338 this->Align = std::max(this->Align, Other->Align);
339 Other->Repl = this->Repl;
340 Other->Live = false;
341}
342
343template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000344SplitInputSection<ELFT>::SplitInputSection(
Rui Ueyamaf7149552016-03-11 18:46:51 +0000345 elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000346 typename InputSectionBase<ELFT>::Kind SectionKind)
347 : InputSectionBase<ELFT>(File, Header, SectionKind) {}
348
349template <class ELFT>
Rui Ueyamaf7149552016-03-11 18:46:51 +0000350EHInputSection<ELFT>::EHInputSection(elf::ObjectFile<ELFT> *F,
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000351 const Elf_Shdr *Header)
Rui Ueyamada735322015-12-24 10:08:54 +0000352 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
353 // Mark .eh_frame sections as live by default because there are
354 // usually no relocations that point to .eh_frames. Otherwise,
George Rimare9e1d322016-02-18 15:17:01 +0000355 // the garbage collector would drop all .eh_frame sections.
Rui Ueyamada735322015-12-24 10:08:54 +0000356 this->Live = true;
357}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000358
359template <class ELFT>
360bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
361 return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
362}
363
364template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000365typename ELFT::uint EHInputSection<ELFT>::getOffset(uintX_t Offset) {
George Rimar6ab275c2015-12-25 09:51:42 +0000366 // The file crtbeginT.o has relocations pointing to the start of an empty
367 // .eh_frame that is known to be the first in the link. It does that to
368 // identify the start of the output .eh_frame. Handle this special case.
369 if (this->getSectionHdr()->sh_size == 0)
370 return Offset;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000371 std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
372 uintX_t Base = I->second;
George Rimar4b40ebc2015-11-13 13:44:59 +0000373 if (Base == uintX_t(-1))
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000374 return -1; // Not in the output
375
376 uintX_t Addend = Offset - I->first;
377 return Base + Addend;
378}
379
380template <class ELFT>
Rafael Espindola36a73d22016-03-11 16:32:46 +0000381MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
Rafael Espindolac159c962015-10-19 21:00:02 +0000382 const Elf_Shdr *Header)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000383 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
Rafael Espindolac159c962015-10-19 21:00:02 +0000384
385template <class ELFT>
386bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000387 return S->SectionKind == InputSectionBase<ELFT>::Merge;
Rafael Espindolac159c962015-10-19 21:00:02 +0000388}
389
Rafael Espindolac159c962015-10-19 21:00:02 +0000390template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000391std::pair<std::pair<typename ELFT::uint, typename ELFT::uint> *,
392 typename ELFT::uint>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000393SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000394 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama7ba639b2015-10-25 16:25:04 +0000395 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000396 uintX_t Size = Data.size();
397 if (Offset >= Size)
George Rimar777f9632016-03-12 08:31:34 +0000398 fatal("entry is past the end of the section");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000399
400 // Find the element this offset points to.
401 auto I = std::upper_bound(
Rafael Espindolad04c12a2015-11-11 15:20:45 +0000402 Offsets.begin(), Offsets.end(), Offset,
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000403 [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000404 return A < B.first;
405 });
Rafael Espindola32994992015-11-11 15:40:37 +0000406 uintX_t End = I == Offsets.end() ? Data.size() : I->first;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000407 --I;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000408 return std::make_pair(&*I, End);
409}
410
411template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000412typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
George Rimar4b40ebc2015-11-13 13:44:59 +0000413 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000414 this->getRangeAndSize(Offset);
415 std::pair<uintX_t, uintX_t> *I = T.first;
416 uintX_t End = T.second;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000417 uintX_t Start = I->first;
418
419 // Compute the Addend and if the Base is cached, return.
420 uintX_t Addend = Offset - Start;
Rafael Espindola32994992015-11-11 15:40:37 +0000421 uintX_t &Base = I->second;
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000422 if (Base != uintX_t(-1))
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000423 return Base + Addend;
424
Hal Finkelf9505952015-11-25 23:54:53 +0000425 // Map the base to the offset in the output section and cache it.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000426 ArrayRef<uint8_t> D = this->getSectionData();
427 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000428 StringRef Entry = Data.substr(Start, End - Start);
429 Base =
430 static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
431 return Base + Addend;
Rafael Espindola5d83ccd2015-08-13 19:18:30 +0000432}
433
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000434template <class ELFT>
Rui Ueyamaf7149552016-03-11 18:46:51 +0000435MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
Rui Ueyama70eed362016-01-06 22:42:43 +0000436 const Elf_Shdr *Hdr)
437 : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
438 // Initialize this->Reginfo.
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000439 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama70eed362016-01-06 22:42:43 +0000440 if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
George Rimar777f9632016-03-12 08:31:34 +0000441 fatal("invalid size of .reginfo section");
Rui Ueyama70eed362016-01-06 22:42:43 +0000442 Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
Simon Atanasyan57830b62015-12-25 13:02:13 +0000443}
444
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000445template <class ELFT>
446bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
447 return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
448}
449
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000450template class elf::InputSectionBase<ELF32LE>;
451template class elf::InputSectionBase<ELF32BE>;
452template class elf::InputSectionBase<ELF64LE>;
453template class elf::InputSectionBase<ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000454
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000455template class elf::InputSection<ELF32LE>;
456template class elf::InputSection<ELF32BE>;
457template class elf::InputSection<ELF64LE>;
458template class elf::InputSection<ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000459
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000460template class elf::EHInputSection<ELF32LE>;
461template class elf::EHInputSection<ELF32BE>;
462template class elf::EHInputSection<ELF64LE>;
463template class elf::EHInputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000464
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000465template class elf::MergeInputSection<ELF32LE>;
466template class elf::MergeInputSection<ELF32BE>;
467template class elf::MergeInputSection<ELF64LE>;
468template class elf::MergeInputSection<ELF64BE>;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000469
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000470template class elf::MipsReginfoInputSection<ELF32LE>;
471template class elf::MipsReginfoInputSection<ELF32BE>;
472template class elf::MipsReginfoInputSection<ELF64LE>;
473template class elf::MipsReginfoInputSection<ELF64BE>;