blob: b529957c862cf8fec6003e59411430e12790fb6c [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>
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 }
George Rimar777f9632016-03-12 08:31:34 +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>
Rui Ueyamaf7149552016-03-11 18:46:51 +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>
Rui Ueyamafc467e72016-03-13 05:06:50 +0000116template <class RelTy>
George Rimar58941ee2016-02-25 08:23:37 +0000117void InputSection<ELFT>::copyRelocations(uint8_t *Buf,
Rui Ueyamafc467e72016-03-13 05:06:50 +0000118 iterator_range<const RelTy *> Rels) {
George Rimar58941ee2016-02-25 08:23:37 +0000119 InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection();
120
Rui Ueyamafc467e72016-03-13 05:06:50 +0000121 for (const RelTy &Rel : Rels) {
George Rimar58941ee2016-02-25 08:23:37 +0000122 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
123 uint32_t Type = Rel.getType(Config->Mips64EL);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000124 SymbolBody &Body = this->File->getSymbolBody(SymIndex).repl();
George Rimar58941ee2016-02-25 08:23:37 +0000125
Rui Ueyamafc467e72016-03-13 05:06:50 +0000126 RelTy *P = reinterpret_cast<RelTy *>(Buf);
127 Buf += sizeof(RelTy);
George Rimar58941ee2016-02-25 08:23:37 +0000128
George Rimar58941ee2016-02-25 08:23:37 +0000129 P->r_offset = RelocatedSection->getOffset(Rel.r_offset);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000130 P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL);
George Rimar58941ee2016-02-25 08:23:37 +0000131 }
132}
133
Rui Ueyama20398472016-03-13 03:09:40 +0000134template <class RelTy>
135static uint32_t getMipsPairType(const RelTy *Rel, const SymbolBody &Sym) {
Rui Ueyama20398472016-03-13 03:09:40 +0000136 switch (Rel->getType(Config->Mips64EL)) {
Simon Atanasyan860fbf02016-02-25 21:33:56 +0000137 case R_MIPS_HI16:
138 return R_MIPS_LO16;
Simon Atanasyan92a32552016-03-12 11:58:15 +0000139 case R_MIPS_GOT16:
Rui Ueyama20398472016-03-13 03:09:40 +0000140 return Sym.isLocal() ? R_MIPS_LO16 : R_MIPS_NONE;
Simon Atanasyan860fbf02016-02-25 21:33:56 +0000141 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>
Rui Ueyamafc467e72016-03-13 05:06:50 +0000151template <class RelTy>
152uint8_t *InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf,
153 const RelTy *Rel,
154 const RelTy *End) {
Rui Ueyama20398472016-03-13 03:09:40 +0000155 uint32_t SymIndex = Rel->getSymbol(Config->Mips64EL);
156 SymbolBody &Sym = File->getSymbolBody(SymIndex).repl();
157 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)
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000164 return nullptr;
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)
173 return nullptr;
174 return Buf + Offset;
175 }
176 return nullptr;
177}
178
179template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +0000180template <class RelTy>
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000181void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd,
Rui Ueyamafc467e72016-03-13 05:06:50 +0000182 iterator_range<const RelTy *> Rels) {
George Rimar6713cf82015-11-25 21:46:05 +0000183 size_t Num = Rels.end() - Rels.begin();
184 for (size_t I = 0; I < Num; ++I) {
Rui Ueyamafc467e72016-03-13 05:06:50 +0000185 const RelTy &RI = *(Rels.begin() + I);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000186 uintX_t Offset = getOffset(RI.r_offset);
187 if (Offset == (uintX_t)-1)
188 continue;
189
George Rimara4ab97d2016-03-11 12:57:52 +0000190 uintX_t A = getAddend<ELFT>(RI);
191 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
192 uint32_t Type = RI.getType(Config->Mips64EL);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000193 uint8_t *BufLoc = Buf + Offset;
194 uintX_t AddrLoc = OutSec->getVA() + Offset;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000195
Rafael Espindolad405f472016-03-04 21:37:09 +0000196 if (Target->pointsToLocalDynamicGotEntry(Type) &&
Rui Ueyamabaf16512016-01-29 00:20:12 +0000197 !Target->canRelaxTls(Type, nullptr)) {
Michael J. Spencer1e225612015-11-11 01:00:24 +0000198 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
George Rimar1d9738c2016-03-11 12:53:17 +0000199 Out<ELFT>::Got->getTlsIndexVA() + A);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000200 continue;
201 }
Rafael Espindola4ea00212015-09-21 22:01:00 +0000202
Rafael Espindola67d72c02016-03-11 12:06:30 +0000203 SymbolBody &Body = File->getSymbolBody(SymIndex).repl();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000204
Rafael Espindola67d72c02016-03-11 12:06:30 +0000205 if (Target->canRelaxTls(Type, &Body)) {
George Rimar0b8ed1d2015-12-21 10:37:33 +0000206 uintX_t SymVA;
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000207 if (Target->needsGot(Type, Body))
Rafael Espindola67d72c02016-03-11 12:06:30 +0000208 SymVA = Body.getGotVA<ELFT>();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000209 else
Rafael Espindola67d72c02016-03-11 12:06:30 +0000210 SymVA = Body.getVA<ELFT>();
George Rimar0b8ed1d2015-12-21 10:37:33 +0000211 // By optimizing TLS relocations, it is sometimes needed to skip
212 // relocations that immediately follow TLS relocations. This function
213 // knows how many slots we need to skip.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000214 I += Target->relaxTls(BufLoc, BufEnd, Type, AddrLoc, SymVA, Body);
George Rimar0b8ed1d2015-12-21 10:37:33 +0000215 continue;
216 }
217
Rafael Espindola87d9f102016-03-11 12:19:05 +0000218 // PPC64 has a special relocation representing the TOC base pointer
219 // that does not have a corresponding symbol.
220 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC) {
221 uintX_t SymVA = getPPC64TocBase() + A;
222 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0);
223 continue;
224 }
225
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000226 if (Target->isTlsGlobalDynamicRel(Type) &&
227 !Target->canRelaxTls(Type, &Body)) {
228 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
229 Out<ELFT>::Got->getGlobalDynAddr(Body) + A);
230 continue;
231 }
232
233 uintX_t SymVA = Body.getVA<ELFT>(A);
234 bool CBP = canBePreempted(Body);
235 uint8_t *PairedLoc = nullptr;
Simon Atanasyan92a32552016-03-12 11:58:15 +0000236 if (Config->EMachine == EM_MIPS)
Rui Ueyama20398472016-03-13 03:09:40 +0000237 PairedLoc = findMipsPairedReloc(Buf, &RI, Rels.end());
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000238
239 if (Target->needsPlt<ELFT>(Type, Body)) {
240 SymVA = Body.getPltVA<ELFT>() + A;
241 } else if (Target->needsGot(Type, Body)) {
242 if (Config->EMachine == EM_MIPS && !CBP) {
243 if (Body.isLocal()) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000244 // R_MIPS_GOT16 relocation against local symbol requires index of
245 // a local GOT entry which contains page address corresponds
Simon Atanasyan860fbf02016-02-25 21:33:56 +0000246 // to sum of the symbol address and addend. The addend in that case
247 // is calculated using addends from R_MIPS_GOT16 and paired
248 // R_MIPS_LO16 relocations.
249 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan860fbf02016-02-25 21:33:56 +0000250 uint64_t AHL = read32<E>(BufLoc) << 16;
Simon Atanasyan92a32552016-03-12 11:58:15 +0000251 if (PairedLoc)
252 AHL += SignExtend64<16>(read32<E>(PairedLoc));
Simon Atanasyan860fbf02016-02-25 21:33:56 +0000253 SymVA = Out<ELFT>::Got->getMipsLocalPageAddr(SymVA + AHL);
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000254 } else {
Simon Atanasyan019049f2016-03-11 13:57:53 +0000255 // For non-local symbols GOT entries should contain their full
256 // addresses. But if such symbol cannot be preempted, we do not
257 // have to put them into the "global" part of GOT and use dynamic
258 // linker to determine their actual addresses. That is why we
259 // create GOT entries for them in the "local" part of GOT.
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000260 SymVA = Out<ELFT>::Got->getMipsLocalFullAddr(Body) + A;
261 }
262 } else {
263 SymVA = Body.getGotVA<ELFT>() + A;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000264 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000265 if (Body.IsTls)
Rui Ueyama724d6252016-01-29 01:49:32 +0000266 Type = Target->getTlsGotRel(Type);
Rafael Espindolaa350e262016-02-26 14:33:23 +0000267 } else if (Target->isSizeRel(Type) && CBP) {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000268 // A SIZE relocation is supposed to set a symbol size, but if a symbol
269 // can be preempted, the size at runtime may be different than link time.
270 // If that's the case, we leave the field alone rather than filling it
271 // with a possibly incorrect value.
George Rimard23970f2015-11-25 20:41:53 +0000272 continue;
Simon Atanasyan09dae7c2015-12-16 14:45:09 +0000273 } else if (Config->EMachine == EM_MIPS) {
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000274 if (Type == R_MIPS_HI16 && &Body == Config->MipsGpDisp) {
275 SymVA = getMipsGpAddr<ELFT>() - AddrLoc + A;
276 } else if (Type == R_MIPS_LO16 && &Body == Config->MipsGpDisp) {
277 SymVA = getMipsGpAddr<ELFT>() - AddrLoc + 4 + A;
278 } else if (&Body == Config->MipsLocalGp) {
279 SymVA = getMipsGpAddr<ELFT>() + A;
280 } else if (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32) {
281 // We need to adjust SymVA value in case of R_MIPS_GPREL16/32
282 // relocations because they use the following expression to calculate
283 // the relocation's result for local symbol: S + A + GP0 - G.
284 SymVA += File->getMipsGp0();
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000285 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000286 } else if (!Target->needsCopyRel<ELFT>(Type, Body) && CBP) {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000287 continue;
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000288 }
Rafael Espindola67d72c02016-03-11 12:06:30 +0000289 uintX_t Size = Body.getSize<ELFT>();
Rafael Espindola38e5d4e2016-03-11 13:04:28 +0000290 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, Size + A,
291 PairedLoc);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000292 }
293}
294
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000295template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000296 if (this->Header->sh_type == SHT_NOBITS)
Michael J. Spencer84487f12015-07-24 21:03:07 +0000297 return;
298 // Copy section contents from source object file to output file.
Rafael Espindolac159c962015-10-19 21:00:02 +0000299 ArrayRef<uint8_t> Data = this->getSectionData();
George Rimar58941ee2016-02-25 08:23:37 +0000300 ELFFile<ELFT> &EObj = this->File->getObj();
301
302 // That happens with -r. In that case we need fix the relocation position and
303 // target. No relocations are applied.
304 if (this->Header->sh_type == SHT_RELA) {
305 this->copyRelocations(Buf + OutSecOff, EObj.relas(this->Header));
306 return;
307 }
308 if (this->Header->sh_type == SHT_REL) {
309 this->copyRelocations(Buf + OutSecOff, EObj.rels(this->Header));
310 return;
311 }
312
Rui Ueyama55c3f892015-10-15 01:58:40 +0000313 memcpy(Buf + OutSecOff, Data.data(), Data.size());
Rafael Espindola4ea00212015-09-21 22:01:00 +0000314
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000315 uint8_t *BufEnd = Buf + OutSecOff + Data.size();
Rafael Espindola4ea00212015-09-21 22:01:00 +0000316 // Iterate over all relocation sections that apply to this section.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000317 for (const Elf_Shdr *RelSec : this->RelocSections) {
Rafael Espindola4ea00212015-09-21 22:01:00 +0000318 if (RelSec->sh_type == SHT_RELA)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000319 this->relocate(Buf, BufEnd, EObj.relas(RelSec));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000320 else
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000321 this->relocate(Buf, BufEnd, EObj.rels(RelSec));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000322 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000323}
324
Rafael Espindolac159c962015-10-19 21:00:02 +0000325template <class ELFT>
Rui Ueyama0b289522016-02-25 18:43:51 +0000326void InputSection<ELFT>::replace(InputSection<ELFT> *Other) {
327 this->Align = std::max(this->Align, Other->Align);
328 Other->Repl = this->Repl;
329 Other->Live = false;
330}
331
332template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000333SplitInputSection<ELFT>::SplitInputSection(
Rui Ueyamaf7149552016-03-11 18:46:51 +0000334 elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header,
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000335 typename InputSectionBase<ELFT>::Kind SectionKind)
336 : InputSectionBase<ELFT>(File, Header, SectionKind) {}
337
338template <class ELFT>
Rui Ueyamaf7149552016-03-11 18:46:51 +0000339EHInputSection<ELFT>::EHInputSection(elf::ObjectFile<ELFT> *F,
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000340 const Elf_Shdr *Header)
Rui Ueyamada735322015-12-24 10:08:54 +0000341 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {
342 // Mark .eh_frame sections as live by default because there are
343 // usually no relocations that point to .eh_frames. Otherwise,
George Rimare9e1d322016-02-18 15:17:01 +0000344 // the garbage collector would drop all .eh_frame sections.
Rui Ueyamada735322015-12-24 10:08:54 +0000345 this->Live = true;
346}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000347
348template <class ELFT>
349bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
350 return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
351}
352
353template <class ELFT>
354typename EHInputSection<ELFT>::uintX_t
355EHInputSection<ELFT>::getOffset(uintX_t Offset) {
George Rimar6ab275c2015-12-25 09:51:42 +0000356 // The file crtbeginT.o has relocations pointing to the start of an empty
357 // .eh_frame that is known to be the first in the link. It does that to
358 // identify the start of the output .eh_frame. Handle this special case.
359 if (this->getSectionHdr()->sh_size == 0)
360 return Offset;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000361 std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
362 uintX_t Base = I->second;
George Rimar4b40ebc2015-11-13 13:44:59 +0000363 if (Base == uintX_t(-1))
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000364 return -1; // Not in the output
365
366 uintX_t Addend = Offset - I->first;
367 return Base + Addend;
368}
369
370template <class ELFT>
Rafael Espindola36a73d22016-03-11 16:32:46 +0000371MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F,
Rafael Espindolac159c962015-10-19 21:00:02 +0000372 const Elf_Shdr *Header)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000373 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
Rafael Espindolac159c962015-10-19 21:00:02 +0000374
375template <class ELFT>
376bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000377 return S->SectionKind == InputSectionBase<ELFT>::Merge;
Rafael Espindolac159c962015-10-19 21:00:02 +0000378}
379
Rafael Espindolac159c962015-10-19 21:00:02 +0000380template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000381std::pair<std::pair<typename ELFFile<ELFT>::uintX_t,
382 typename ELFFile<ELFT>::uintX_t> *,
383 typename ELFFile<ELFT>::uintX_t>
384SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000385 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama7ba639b2015-10-25 16:25:04 +0000386 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000387 uintX_t Size = Data.size();
388 if (Offset >= Size)
George Rimar777f9632016-03-12 08:31:34 +0000389 fatal("entry is past the end of the section");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000390
391 // Find the element this offset points to.
392 auto I = std::upper_bound(
Rafael Espindolad04c12a2015-11-11 15:20:45 +0000393 Offsets.begin(), Offsets.end(), Offset,
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000394 [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000395 return A < B.first;
396 });
Rafael Espindola32994992015-11-11 15:40:37 +0000397 uintX_t End = I == Offsets.end() ? Data.size() : I->first;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000398 --I;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000399 return std::make_pair(&*I, End);
400}
401
402template <class ELFT>
403typename MergeInputSection<ELFT>::uintX_t
404MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
George Rimar4b40ebc2015-11-13 13:44:59 +0000405 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T =
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000406 this->getRangeAndSize(Offset);
407 std::pair<uintX_t, uintX_t> *I = T.first;
408 uintX_t End = T.second;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000409 uintX_t Start = I->first;
410
411 // Compute the Addend and if the Base is cached, return.
412 uintX_t Addend = Offset - Start;
Rafael Espindola32994992015-11-11 15:40:37 +0000413 uintX_t &Base = I->second;
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000414 if (Base != uintX_t(-1))
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000415 return Base + Addend;
416
Hal Finkelf9505952015-11-25 23:54:53 +0000417 // Map the base to the offset in the output section and cache it.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000418 ArrayRef<uint8_t> D = this->getSectionData();
419 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000420 StringRef Entry = Data.substr(Start, End - Start);
421 Base =
422 static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
423 return Base + Addend;
Rafael Espindola5d83ccd2015-08-13 19:18:30 +0000424}
425
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000426template <class ELFT>
Rui Ueyamaf7149552016-03-11 18:46:51 +0000427MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F,
Rui Ueyama70eed362016-01-06 22:42:43 +0000428 const Elf_Shdr *Hdr)
429 : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) {
430 // Initialize this->Reginfo.
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000431 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama70eed362016-01-06 22:42:43 +0000432 if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>))
George Rimar777f9632016-03-12 08:31:34 +0000433 fatal("invalid size of .reginfo section");
Rui Ueyama70eed362016-01-06 22:42:43 +0000434 Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data());
Simon Atanasyan57830b62015-12-25 13:02:13 +0000435}
436
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000437template <class ELFT>
438bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
439 return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
440}
441
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000442template class elf::InputSectionBase<ELF32LE>;
443template class elf::InputSectionBase<ELF32BE>;
444template class elf::InputSectionBase<ELF64LE>;
445template class elf::InputSectionBase<ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000446
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000447template class elf::InputSection<ELF32LE>;
448template class elf::InputSection<ELF32BE>;
449template class elf::InputSection<ELF64LE>;
450template class elf::InputSection<ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000451
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000452template class elf::EHInputSection<ELF32LE>;
453template class elf::EHInputSection<ELF32BE>;
454template class elf::EHInputSection<ELF64LE>;
455template class elf::EHInputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000456
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000457template class elf::MergeInputSection<ELF32LE>;
458template class elf::MergeInputSection<ELF32BE>;
459template class elf::MergeInputSection<ELF64LE>;
460template class elf::MergeInputSection<ELF64BE>;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000461
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000462template class elf::MipsReginfoInputSection<ELF32LE>;
463template class elf::MipsReginfoInputSection<ELF32BE>;
464template class elf::MipsReginfoInputSection<ELF64LE>;
465template class elf::MipsReginfoInputSection<ELF64BE>;