blob: 0d435f3e7ccc4b2ca9e53b843edc3b299a34d21f [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.cpp -------------------------------------------------===//
2//
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
10#include "OutputSections.h"
11#include "Config.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000012#include "SymbolTable.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000013#include "Target.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000014#include "llvm/Support/MathExtras.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000015
Rafael Espindola5805c4f2015-09-21 21:38:08 +000016using namespace llvm;
17using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000018using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000019using namespace llvm::ELF;
20
21using namespace lld;
22using namespace lld::elf2;
23
George Rimarbfb7bf72015-12-21 10:00:12 +000024bool lld::elf2::HasGotOffRel = false;
25
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000026template <class ELFT>
27OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t sh_type,
28 uintX_t sh_flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029 : Name(Name) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000030 memset(&Header, 0, sizeof(Elf_Shdr));
Rafael Espindola5805c4f2015-09-21 21:38:08 +000031 Header.sh_type = sh_type;
32 Header.sh_flags = sh_flags;
33}
34
Rafael Espindola35c6af32015-09-25 17:19:10 +000035template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000036GotPltSection<ELFT>::GotPltSection()
37 : OutputSectionBase<ELFT>(".got.plt", llvm::ELF::SHT_PROGBITS,
38 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
39 this->Header.sh_addralign = sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000040}
41
42template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000043 Sym->GotPltIndex = Target->getGotPltHeaderEntriesNum() + Entries.size();
George Rimar648a2c32015-10-20 08:54:27 +000044 Entries.push_back(Sym);
45}
46
47template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +000048 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +000049}
50
51template <class ELFT>
52typename GotPltSection<ELFT>::uintX_t
53GotPltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
54 return this->getVA() + B.GotPltIndex * sizeof(uintX_t);
55}
56
57template <class ELFT> void GotPltSection<ELFT>::finalize() {
Igor Kudrin351b41d2015-11-16 17:44:08 +000058 this->Header.sh_size =
59 (Target->getGotPltHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000060}
61
62template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000063 Target->writeGotPltHeaderEntries(Buf);
64 Buf += Target->getGotPltHeaderEntriesNum() * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000065 for (const SymbolBody *B : Entries) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000066 Target->writeGotPltEntry(Buf, Out<ELFT>::Plt->getEntryAddr(*B));
George Rimar648a2c32015-10-20 08:54:27 +000067 Buf += sizeof(uintX_t);
68 }
69}
70
71template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000072GotSection<ELFT>::GotSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000073 : OutputSectionBase<ELFT>(".got", llvm::ELF::SHT_PROGBITS,
74 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000075 if (Config->EMachine == EM_MIPS)
76 this->Header.sh_flags |= llvm::ELF::SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000077 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000078}
79
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000081 Sym->GotIndex = Target->getGotHeaderEntriesNum() + Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 Entries.push_back(Sym);
George Rimar687138c2015-11-13 16:28:53 +000083}
84
George Rimar90cd0a82015-12-01 19:20:26 +000085template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) {
86 if (Sym->hasGlobalDynIndex())
87 return false;
88 Sym->GlobalDynIndex = Target->getGotHeaderEntriesNum() + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +000089 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +000090 Entries.push_back(Sym);
91 Entries.push_back(nullptr);
George Rimar90cd0a82015-12-01 19:20:26 +000092 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000093}
94
George Rimar95c1a582015-12-07 08:02:20 +000095template <class ELFT> bool GotSection<ELFT>::addCurrentModuleTlsIndex() {
George Rimarb17f7392015-12-01 18:24:07 +000096 if (LocalTlsIndexOff != uint32_t(-1))
97 return false;
Michael J. Spencer1e225612015-11-11 01:00:24 +000098 Entries.push_back(nullptr);
99 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +0000100 LocalTlsIndexOff = (Entries.size() - 2) * sizeof(uintX_t);
101 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000102}
103
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104template <class ELFT>
105typename GotSection<ELFT>::uintX_t
106GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama5f551ae2015-10-14 14:02:06 +0000107 return this->getVA() + B.GotIndex * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000108}
109
Igor Kudrin304860a2015-11-12 04:39:49 +0000110template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000111typename GotSection<ELFT>::uintX_t
112GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
113 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
114}
115
116template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000117const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
118 return Entries.empty() ? nullptr : Entries.front();
119}
120
121template <class ELFT>
122unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
123 // TODO: Update when the suppoort of GOT entries for local symbols is added.
124 return Target->getGotHeaderEntriesNum();
125}
126
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000127template <class ELFT> void GotSection<ELFT>::finalize() {
128 this->Header.sh_size =
129 (Target->getGotHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
130}
131
Rafael Espindolaa6627382015-10-06 23:56:53 +0000132template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000133 Target->writeGotHeaderEntries(Buf);
134 Buf += Target->getGotHeaderEntriesNum() * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000135 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000136 uint8_t *Entry = Buf;
137 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000138 if (!B)
139 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000140 // MIPS has special rules to fill up GOT entries.
141 // See "Global Offset Table" in Chapter 5 in the following document
142 // for detailed description:
143 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
144 // As the first approach, we can just store addresses for all symbols.
145 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000146 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000147 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +0000148 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000149 }
150}
151
Rafael Espindola35c6af32015-09-25 17:19:10 +0000152template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000153PltSection<ELFT>::PltSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000154 : OutputSectionBase<ELFT>(".plt", llvm::ELF::SHT_PROGBITS,
155 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000156 this->Header.sh_addralign = 16;
157}
158
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000160 size_t Off = 0;
George Rimar648a2c32015-10-20 08:54:27 +0000161 bool LazyReloc = Target->supportsLazyRelocations();
162 if (LazyReloc) {
163 // First write PLT[0] entry which is special.
164 Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA());
165 Off += Target->getPltZeroEntrySize();
166 }
George Rimarfb5d7f22015-11-26 19:58:51 +0000167 for (auto &I : Entries) {
George Rimar77b77792015-11-25 22:15:01 +0000168 const SymbolBody *E = I.first;
169 unsigned RelOff = I.second;
170 uint64_t GotVA =
171 LazyReloc ? Out<ELFT>::GotPlt->getVA() : Out<ELFT>::Got->getVA();
172 uint64_t GotE = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
173 : Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000174 uint64_t Plt = this->getVA() + Off;
George Rimar77b77792015-11-25 22:15:01 +0000175 Target->writePltEntry(Buf + Off, GotVA, GotE, Plt, E->PltIndex, RelOff);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000176 Off += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000177 }
178}
179
180template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000181 Sym->PltIndex = Entries.size();
George Rimar77b77792015-11-25 22:15:01 +0000182 unsigned RelOff = Target->supportsLazyRelocations()
183 ? Out<ELFT>::RelaPlt->getRelocOffset()
184 : Out<ELFT>::RelaDyn->getRelocOffset();
185 Entries.push_back(std::make_pair(Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000186}
187
188template <class ELFT>
189typename PltSection<ELFT>::uintX_t
190PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
George Rimar648a2c32015-10-20 08:54:27 +0000191 return this->getVA() + Target->getPltZeroEntrySize() +
192 B.PltIndex * Target->getPltEntrySize();
193}
194
195template <class ELFT> void PltSection<ELFT>::finalize() {
196 this->Header.sh_size = Target->getPltZeroEntrySize() +
197 Entries.size() * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000198}
199
200template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000201RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
202 : OutputSectionBase<ELFT>(Name,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000203 IsRela ? llvm::ELF::SHT_RELA : llvm::ELF::SHT_REL,
204 llvm::ELF::SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000205 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000206 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
207 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
208}
209
George Rimar5828c232015-11-30 17:49:19 +0000210// Applies corresponding symbol and type for dynamic tls relocation.
211// Returns true if relocation was handled.
212template <class ELFT>
213bool RelocationSection<ELFT>::applyTlsDynamicReloc(SymbolBody *Body,
214 uint32_t Type, Elf_Rel *P,
215 Elf_Rel *N) {
216 if (Target->isTlsLocalDynamicReloc(Type)) {
217 P->setSymbolAndType(0, Target->getTlsModuleIndexReloc(), Config->Mips64EL);
George Rimarb17f7392015-12-01 18:24:07 +0000218 P->r_offset = Out<ELFT>::Got->getLocalTlsIndexVA();
George Rimar5828c232015-11-30 17:49:19 +0000219 return true;
220 }
221
George Rimar25411f252015-12-04 11:20:13 +0000222 if (!Body || !Target->isTlsGlobalDynamicReloc(Type))
223 return false;
224
225 if (Target->isTlsOptimized(Type, Body)) {
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000226 P->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000227 Target->getTlsGotReloc(), Config->Mips64EL);
228 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimar5828c232015-11-30 17:49:19 +0000229 return true;
230 }
George Rimar25411f252015-12-04 11:20:13 +0000231
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000232 P->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000233 Target->getTlsModuleIndexReloc(), Config->Mips64EL);
234 P->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body);
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000235 N->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000236 Target->getTlsOffsetReloc(), Config->Mips64EL);
237 N->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body) + sizeof(uintX_t);
238 return true;
George Rimar5828c232015-11-30 17:49:19 +0000239}
240
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000242 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000244 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
245 Buf += EntrySize;
246
Michael J. Spencer627ae702015-11-13 00:28:34 +0000247 // Skip placeholder for global dynamic TLS relocation pair. It was already
248 // handled by the previous relocation.
249 if (!Rel.C || !Rel.RI)
250 continue;
251
252 InputSectionBase<ELFT> &C = *Rel.C;
253 const Elf_Rel &RI = *Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000254 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000255 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000256 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000257 if (Body)
258 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000259
Rui Ueyama64558522015-10-16 22:51:43 +0000260 uint32_t Type = RI.getType(Config->Mips64EL);
George Rimar5828c232015-11-30 17:49:19 +0000261 if (applyTlsDynamicReloc(Body, Type, P, reinterpret_cast<Elf_Rel *>(Buf)))
Michael J. Spencer1e225612015-11-11 01:00:24 +0000262 continue;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000263 bool NeedsCopy = Body && Target->needsCopyRel(Type, *Body);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000264 bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
265 bool CanBePreempted = canBePreempted(Body, NeedsGot);
George Rimar648a2c32015-10-20 08:54:27 +0000266 bool LazyReloc = Body && Target->supportsLazyRelocations() &&
267 Target->relocNeedsPlt(Type, *Body);
George Rimar6f17e092015-12-17 09:32:21 +0000268 bool IsDynRelative = Type == Target->getRelativeReloc();
Rafael Espindola49757522015-10-19 19:58:18 +0000269
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000270 unsigned Sym = CanBePreempted ? Body->DynamicSymbolTableIndex : 0;
George Rimarc7dc0be2015-12-15 08:48:39 +0000271 unsigned Reloc;
George Rimara07ff662015-12-21 10:12:06 +0000272 if (!CanBePreempted && Body && isGnuIFunc<ELFT>(*Body))
273 Reloc = Target->getIRelativeReloc();
274 else if (!CanBePreempted || IsDynRelative)
George Rimarc7dc0be2015-12-15 08:48:39 +0000275 Reloc = Target->getRelativeReloc();
276 else if (LazyReloc)
277 Reloc = Target->getPltReloc();
278 else if (NeedsGot)
Rui Ueyama62d0e322015-12-17 00:04:18 +0000279 Reloc = Body->isTls() ? Target->getTlsGotReloc() : Target->getGotReloc();
George Rimarc7dc0be2015-12-15 08:48:39 +0000280 else if (NeedsCopy)
281 Reloc = Target->getCopyReloc();
282 else
283 Reloc = Target->getDynReloc(Type);
284 P->setSymbolAndType(Sym, Reloc, Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000285
George Rimar4b5346f2015-12-29 16:17:32 +0000286 if (LazyReloc)
287 P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
288 else if (NeedsGot)
289 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
290 else if (NeedsCopy)
George Rimarbc590fe2015-10-28 16:48:58 +0000291 P->r_offset = Out<ELFT>::Bss->getVA() +
Rui Ueyamae57c4872016-01-05 16:35:43 +0000292 dyn_cast<SharedSymbol<ELFT>>(Body)->OffsetInBss;
George Rimar4b5346f2015-12-29 16:17:32 +0000293 else
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000294 P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA();
Rafael Espindola49757522015-10-19 19:58:18 +0000295
Rafael Espindola932efcf2015-10-19 20:24:44 +0000296 uintX_t OrigAddend = 0;
Rafael Espindola49757522015-10-19 19:58:18 +0000297 if (IsRela && !NeedsGot)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000298 OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola49757522015-10-19 19:58:18 +0000299
Rafael Espindola932efcf2015-10-19 20:24:44 +0000300 uintX_t Addend;
George Rimarbc590fe2015-10-28 16:48:58 +0000301 if (NeedsCopy)
302 Addend = 0;
George Rimar6f17e092015-12-17 09:32:21 +0000303 else if (CanBePreempted || IsDynRelative)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000304 Addend = OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000305 else if (Body)
Rafael Espindola02346402015-12-21 20:18:04 +0000306 Addend = getSymVA<ELFT>(*Body) + OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000307 else if (IsRela)
George Rimar0b8ed1d2015-12-21 10:37:33 +0000308 Addend =
309 getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI),
310 getAddend<ELFT>(static_cast<const Elf_Rela &>(RI)));
Rui Ueyamab7f28672015-10-19 20:31:49 +0000311 else
George Rimar0b8ed1d2015-12-21 10:37:33 +0000312 Addend = getLocalRelTarget(File, RI, 0);
Rafael Espindola52dca342015-10-07 00:58:20 +0000313
314 if (IsRela)
315 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000316 }
317}
318
George Rimar77b77792015-11-25 22:15:01 +0000319template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
320 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
321 return EntrySize * Relocs.size();
322}
323
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324template <class ELFT> void RelocationSection<ELFT>::finalize() {
George Rimara07ff662015-12-21 10:12:06 +0000325 this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex
326 : Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000327 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
328}
329
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000330template <class ELFT>
331InterpSection<ELFT>::InterpSection()
332 : OutputSectionBase<ELFT>(".interp", llvm::ELF::SHT_PROGBITS,
333 llvm::ELF::SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000334 this->Header.sh_size = Config->DynamicLinker.size() + 1;
335 this->Header.sh_addralign = 1;
336}
337
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000338template <class ELFT>
339void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
Rui Ueyama157c4332015-10-24 17:57:39 +0000340 Header.sh_name = Out<ELFT>::ShStrTab->getOffset(Name);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000341 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000342}
343
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000344template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000345 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
346}
347
Rafael Espindola35c6af32015-09-25 17:19:10 +0000348template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000349HashTableSection<ELFT>::HashTableSection()
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000350 : OutputSectionBase<ELFT>(".hash", llvm::ELF::SHT_HASH,
351 llvm::ELF::SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000352 this->Header.sh_entsize = sizeof(Elf_Word);
353 this->Header.sh_addralign = sizeof(Elf_Word);
354}
355
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000356static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000357 uint32_t H = 0;
358 for (char C : Name) {
359 H = (H << 4) + C;
360 uint32_t G = H & 0xf0000000;
361 if (G)
362 H ^= G >> 24;
363 H &= ~G;
364 }
365 return H;
366}
367
Rui Ueyama0db335f2015-10-07 16:58:54 +0000368template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000369 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000370
Rui Ueyama0db335f2015-10-07 16:58:54 +0000371 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000372 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000373
374 // Create as many buckets as there are symbols.
375 // FIXME: This is simplistic. We can try to optimize it, but implementing
376 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000377 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000378 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
379}
380
381template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000382 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000383 auto *P = reinterpret_cast<Elf_Word *>(Buf);
384 *P++ = NumSymbols; // nbucket
385 *P++ = NumSymbols; // nchain
386
387 Elf_Word *Buckets = P;
388 Elf_Word *Chains = P + NumSymbols;
389
Igor Kudrinf1d60292015-10-28 07:05:56 +0000390 for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) {
Igor Kudrinab665fc2015-10-20 21:47:58 +0000391 StringRef Name = Body->getName();
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000392 unsigned I = Body->DynamicSymbolTableIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000393 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000394 Chains[I] = Buckets[Hash];
395 Buckets[Hash] = I;
396 }
397}
398
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000399static uint32_t hashGnu(StringRef Name) {
400 uint32_t H = 5381;
401 for (uint8_t C : Name)
402 H = (H << 5) + H + C;
403 return H;
404}
405
406template <class ELFT>
407GnuHashTableSection<ELFT>::GnuHashTableSection()
408 : OutputSectionBase<ELFT>(".gnu.hash", llvm::ELF::SHT_GNU_HASH,
409 llvm::ELF::SHF_ALLOC) {
410 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
411 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
412}
413
414template <class ELFT>
415unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
416 if (!NumHashed)
417 return 0;
418
419 // These values are prime numbers which are not greater than 2^(N-1) + 1.
420 // In result, for any particular NumHashed we return a prime number
421 // which is not greater than NumHashed.
422 static const unsigned Primes[] = {
423 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
424 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
425
426 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
427 array_lengthof(Primes) - 1)];
428}
429
430// Bloom filter estimation: at least 8 bits for each hashed symbol.
431// GNU Hash table requirement: it should be a power of 2,
432// the minimum value is 1, even for an empty table.
433// Expected results for a 32-bit target:
434// calcMaskWords(0..4) = 1
435// calcMaskWords(5..8) = 2
436// calcMaskWords(9..16) = 4
437// For a 64-bit target:
438// calcMaskWords(0..8) = 1
439// calcMaskWords(9..16) = 2
440// calcMaskWords(17..32) = 4
441template <class ELFT>
442unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
443 if (!NumHashed)
444 return 1;
445 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
446}
447
448template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000449 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000450 NBuckets = calcNBuckets(NumHashed);
451 MaskWords = calcMaskWords(NumHashed);
452 // Second hash shift estimation: just predefined values.
453 Shift2 = ELFT::Is64Bits ? 6 : 5;
454
455 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
456 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
457 + sizeof(Elf_Off) * MaskWords // Bloom Filter
458 + sizeof(Elf_Word) * NBuckets // Hash Buckets
459 + sizeof(Elf_Word) * NumHashed; // Hash Values
460}
461
462template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
463 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000464 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000465 return;
466 writeBloomFilter(Buf);
467 writeHashTable(Buf);
468}
469
470template <class ELFT>
471void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
472 auto *P = reinterpret_cast<Elf_Word *>(Buf);
473 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000474 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000475 *P++ = MaskWords;
476 *P++ = Shift2;
477 Buf = reinterpret_cast<uint8_t *>(P);
478}
479
480template <class ELFT>
481void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
482 unsigned C = sizeof(Elf_Off) * 8;
483
484 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000485 for (const HashedSymbolData &Item : HashedSymbols) {
486 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
487 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
488 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000489 Masks[Pos] |= V;
490 }
491 Buf += sizeof(Elf_Off) * MaskWords;
492}
493
494template <class ELFT>
495void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
496 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
497 Elf_Word *Values = Buckets + NBuckets;
498
499 int PrevBucket = -1;
500 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000501 for (const HashedSymbolData &Item : HashedSymbols) {
502 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000503 assert(PrevBucket <= Bucket);
504 if (Bucket != PrevBucket) {
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000505 Buckets[Bucket] = Item.Body->DynamicSymbolTableIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000506 PrevBucket = Bucket;
507 if (I > 0)
508 Values[I - 1] |= 1;
509 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000510 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000511 ++I;
512 }
513 if (I > 0)
514 Values[I - 1] |= 1;
515}
516
Rafael Espindola31f88882015-11-02 14:33:11 +0000517static bool includeInGnuHashTable(SymbolBody *B) {
518 // Assume that includeInDynamicSymtab() is already checked.
519 return !B->isUndefined();
520}
521
Rafael Espindola35c6af32015-09-25 17:19:10 +0000522template <class ELFT>
Igor Kudrinf1d60292015-10-28 07:05:56 +0000523void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) {
524 std::vector<SymbolBody *> NotHashed;
525 NotHashed.reserve(Symbols.size());
526 HashedSymbols.reserve(Symbols.size());
527 for (SymbolBody *B : Symbols) {
528 if (includeInGnuHashTable(B))
529 HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())});
530 else
531 NotHashed.push_back(B);
532 }
533 if (HashedSymbols.empty())
534 return;
535
536 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
537 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
538 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
539 return L.Hash % NBuckets < R.Hash % NBuckets;
540 });
541
542 Symbols = std::move(NotHashed);
543 for (const HashedSymbolData &Item : HashedSymbols)
544 Symbols.push_back(Item.Body);
545}
546
547template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000548DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000549 : OutputSectionBase<ELFT>(".dynamic", llvm::ELF::SHT_DYNAMIC,
550 llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000551 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000552 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000553 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
554 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000555
556 // .dynamic section is not writable on MIPS.
557 // See "Special Section" in Chapter 4 in the following document:
558 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
559 if (Config->EMachine == EM_MIPS)
560 Header.sh_flags = llvm::ELF::SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000561}
562
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000563template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000564 if (this->Header.sh_size)
565 return; // Already finalized.
566
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000567 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000568 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000569
570 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000571 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000572 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000573 ++NumEntries; // DT_RELASZ / DT_RELSZ
574 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000575 }
George Rimar648a2c32015-10-20 08:54:27 +0000576 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
577 ++NumEntries; // DT_JMPREL
578 ++NumEntries; // DT_PLTRELSZ
Igor Kudrin304860a2015-11-12 04:39:49 +0000579 ++NumEntries; // DT_PLTGOT / DT_MIPS_PLTGOT
George Rimar648a2c32015-10-20 08:54:27 +0000580 ++NumEntries; // DT_PLTREL
581 }
582
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000583 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000584 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000585 ++NumEntries; // DT_STRTAB
586 ++NumEntries; // DT_STRSZ
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000587 if (Out<ELFT>::GnuHashTab)
588 ++NumEntries; // DT_GNU_HASH
589 if (Out<ELFT>::HashTab)
590 ++NumEntries; // DT_HASH
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000591
Rui Ueyama7de3f372015-10-01 19:36:04 +0000592 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000593 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000594 Out<ELFT>::DynStrTab->add(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000595 }
596
597 if (!Config->SoName.empty()) {
598 ++NumEntries; // DT_SONAME
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000599 Out<ELFT>::DynStrTab->add(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000600 }
601
Rafael Espindola77572242015-10-02 19:37:55 +0000602 if (PreInitArraySec)
603 NumEntries += 2;
604 if (InitArraySec)
605 NumEntries += 2;
606 if (FiniArraySec)
607 NumEntries += 2;
608
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000609 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000610 if (!F->isNeeded())
611 continue;
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000612 Out<ELFT>::DynStrTab->add(F->getSoName());
613 ++NumEntries;
614 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000615
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000616 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
Rafael Espindola167e62f2015-12-21 20:59:29 +0000617 InitSym = S->Body;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000618 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
Rafael Espindola167e62f2015-12-21 20:59:29 +0000619 FiniSym = S->Body;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000620 if (InitSym)
621 ++NumEntries; // DT_INIT
622 if (FiniSym)
623 ++NumEntries; // DT_FINI
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000624
625 if (Config->Bsymbolic)
626 DtFlags |= DF_SYMBOLIC;
627 if (Config->ZNodelete)
628 DtFlags1 |= DF_1_NODELETE;
629 if (Config->ZNow) {
630 DtFlags |= DF_BIND_NOW;
631 DtFlags1 |= DF_1_NOW;
632 }
633 if (Config->ZOrigin) {
634 DtFlags |= DF_ORIGIN;
635 DtFlags1 |= DF_1_ORIGIN;
636 }
637
638 if (DtFlags)
Davide Italiano58cbaf02015-10-19 23:32:16 +0000639 ++NumEntries; // DT_FLAGS
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000640 if (DtFlags1)
George Rimar97aad172015-10-07 15:00:21 +0000641 ++NumEntries; // DT_FLAGS_1
Igor Kudrin304860a2015-11-12 04:39:49 +0000642
643 if (Config->EMachine == EM_MIPS) {
644 ++NumEntries; // DT_MIPS_RLD_VERSION
645 ++NumEntries; // DT_MIPS_FLAGS
646 ++NumEntries; // DT_MIPS_BASE_ADDRESS
647 ++NumEntries; // DT_MIPS_SYMTABNO
648 ++NumEntries; // DT_MIPS_LOCAL_GOTNO
649 ++NumEntries; // DT_MIPS_GOTSYM;
650 ++NumEntries; // DT_PLTGOT
651 if (Out<ELFT>::MipsRldMap)
652 ++NumEntries; // DT_MIPS_RLD_MAP
653 }
654
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000655 ++NumEntries; // DT_NULL
656
657 Header.sh_size = NumEntries * Header.sh_entsize;
658}
659
660template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000661 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
662
Rui Ueyama8c205d52015-10-02 01:33:31 +0000663 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
664 P->d_tag = Tag;
665 P->d_un.d_ptr = Val;
666 ++P;
667 };
668
669 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
670 P->d_tag = Tag;
671 P->d_un.d_val = Val;
672 ++P;
673 };
674
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000675 if (Out<ELFT>::RelaDyn->hasRelocs()) {
676 bool IsRela = Out<ELFT>::RelaDyn->isRela();
677 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
678 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000679 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
680 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000681 }
George Rimar648a2c32015-10-20 08:54:27 +0000682 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
683 WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
684 WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
Igor Kudrin304860a2015-11-12 04:39:49 +0000685 // On MIPS, the address of the .got.plt section is stored in
686 // the DT_MIPS_PLTGOT entry because the DT_PLTGOT entry points to
687 // the .got section. See "Dynamic Section" in the following document:
688 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
689 WritePtr((Config->EMachine == EM_MIPS) ? DT_MIPS_PLTGOT : DT_PLTGOT,
690 Out<ELFT>::GotPlt->getVA());
George Rimar648a2c32015-10-20 08:54:27 +0000691 WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
692 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000693
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000694 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000695 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000696 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
697 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->data().size());
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000698 if (Out<ELFT>::GnuHashTab)
699 WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA());
700 if (Out<ELFT>::HashTab)
701 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000702
Rui Ueyamadfa577b2015-11-19 23:30:10 +0000703 // If --enable-new-dtags is set, lld emits DT_RUNPATH
704 // instead of DT_RPATH. The two tags are functionally
705 // equivalent except for the following:
706 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
707 // DT_RPATH is searched before.
708 // - DT_RUNPATH is used only to search for direct
709 // dependencies of the object it's contained in, while
710 // DT_RPATH is used for indirect dependencies as well.
Rui Ueyama8c205d52015-10-02 01:33:31 +0000711 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000712 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000713 Out<ELFT>::DynStrTab->getOffset(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000714
Rui Ueyama8c205d52015-10-02 01:33:31 +0000715 if (!Config->SoName.empty())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000716 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->getOffset(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000717
Rafael Espindola77572242015-10-02 19:37:55 +0000718 auto WriteArray = [&](int32_t T1, int32_t T2,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000719 const OutputSectionBase<ELFT> *Sec) {
Rafael Espindola77572242015-10-02 19:37:55 +0000720 if (!Sec)
721 return;
722 WritePtr(T1, Sec->getVA());
723 WriteVal(T2, Sec->getSize());
724 };
725 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
726 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
727 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
728
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000729 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000730 if (F->isNeeded())
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000731 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->getOffset(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000732
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000733 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000734 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000735 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000736 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000737 if (DtFlags)
738 WriteVal(DT_FLAGS, DtFlags);
739 if (DtFlags1)
740 WriteVal(DT_FLAGS_1, DtFlags1);
Igor Kudrin304860a2015-11-12 04:39:49 +0000741
742 // See "Dynamic Section" in Chapter 5 in the following document
743 // for detailed description:
744 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
745 if (Config->EMachine == EM_MIPS) {
746 WriteVal(DT_MIPS_RLD_VERSION, 1);
747 WriteVal(DT_MIPS_FLAGS, RHF_NOTPOT);
748 WritePtr(DT_MIPS_BASE_ADDRESS, Target->getVAStart());
749 WriteVal(DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols());
750 WriteVal(DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum());
751 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000752 WriteVal(DT_MIPS_GOTSYM, B->DynamicSymbolTableIndex);
Igor Kudrin304860a2015-11-12 04:39:49 +0000753 else
754 WriteVal(DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols());
755 WritePtr(DT_PLTGOT, Out<ELFT>::Got->getVA());
756 if (Out<ELFT>::MipsRldMap)
757 WritePtr(DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap->getVA());
758 }
759
Rui Ueyama8c205d52015-10-02 01:33:31 +0000760 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000761}
762
763template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000764OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t sh_type,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000765 uintX_t sh_flags)
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000766 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000767
768template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000769void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
770 auto *S = cast<InputSection<ELFT>>(C);
771 Sections.push_back(S);
772 S->OutSec = this;
773 uint32_t Align = S->getAlign();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000774 if (Align > this->Header.sh_addralign)
775 this->Header.sh_addralign = Align;
776
777 uintX_t Off = this->Header.sh_size;
778 Off = RoundUpToAlignment(Off, Align);
Rui Ueyama40845e62015-12-26 05:51:07 +0000779 S->OutSecOff = Off;
780 Off += S->getSize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000781 this->Header.sh_size = Off;
782}
783
784template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000785typename ELFFile<ELFT>::uintX_t lld::elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000786 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000787 case SymbolBody::DefinedSyntheticKind: {
788 auto &D = cast<DefinedSynthetic<ELFT>>(S);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000789 return D.Section.getVA() + D.Value;
Rafael Espindola8614c562015-10-06 14:33:58 +0000790 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000791 case SymbolBody::DefinedRegularKind: {
792 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000793 InputSectionBase<ELFT> *SC = DR.Section;
794 if (!SC)
795 return DR.Sym.st_value;
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000796 if (DR.Sym.getType() == STT_TLS)
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000797 return SC->OutSec->getVA() + SC->getOffset(DR.Sym) -
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000798 Out<ELFT>::TlsPhdr->p_vaddr;
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000799 return SC->OutSec->getVA() + SC->getOffset(DR.Sym);
Rafael Espindolacd076f02015-09-25 18:19:03 +0000800 }
801 case SymbolBody::DefinedCommonKind:
Rui Ueyamae57c4872016-01-05 16:35:43 +0000802 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(S).OffsetInBss;
George Rimarbc590fe2015-10-28 16:48:58 +0000803 case SymbolBody::SharedKind: {
804 auto &SS = cast<SharedSymbol<ELFT>>(S);
Rui Ueyamabb936062015-12-17 01:14:23 +0000805 if (SS.NeedsCopy)
Rui Ueyamae57c4872016-01-05 16:35:43 +0000806 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
George Rimarbc590fe2015-10-28 16:48:58 +0000807 return 0;
808 }
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000809 case SymbolBody::UndefinedElfKind:
Rafael Espindolacd076f02015-09-25 18:19:03 +0000810 case SymbolBody::UndefinedKind:
811 return 0;
812 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000813 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
814 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000815 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000816 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000817}
818
Rui Ueyama34f29242015-10-13 19:51:57 +0000819// Returns a VA which a relocatin RI refers to. Used only for local symbols.
820// For non-local symbols, use getSymVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000821template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000822typename ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +0000823lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
George Rimar0b8ed1d2015-12-21 10:37:33 +0000824 const Elf_Rel_Impl<ELFT, IsRela> &RI,
825 typename ELFFile<ELFT>::uintX_t Addend) {
Rafael Espindola932efcf2015-10-19 20:24:44 +0000826 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
827 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
828
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000829 // PPC64 has a special relocation representing the TOC base pointer
830 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000831 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000832 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000833
Rui Ueyama126d08f2015-10-12 20:28:22 +0000834 const Elf_Sym *Sym =
835 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000836
Rui Ueyama126d08f2015-10-12 20:28:22 +0000837 if (!Sym)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000838 error("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000839
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000840 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
841
842 if (Sym->getType() == STT_TLS)
843 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
George Rimarcc06a6f2015-11-29 14:14:20 +0000844 Out<ELFT>::TlsPhdr->p_vaddr;
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000845
Rafael Espindola444576d2015-10-09 19:25:07 +0000846 // According to the ELF spec reference to a local symbol from outside
847 // the group are not allowed. Unfortunately .eh_frame breaks that rule
848 // and must be treated specially. For now we just replace the symbol with
849 // 0.
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000850 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000851 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000852
Rafael Espindolac159c962015-10-19 21:00:02 +0000853 uintX_t VA = Section->OutSec->getVA();
854 if (isa<InputSection<ELFT>>(Section))
855 return VA + Section->getOffset(*Sym) + Addend;
856
857 uintX_t Offset = Sym->st_value;
858 if (Sym->getType() == STT_SECTION) {
859 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000860 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000861 }
Rafael Espindola7f040bf2015-12-25 01:00:41 +0000862 return VA + Section->getOffset(Offset) + Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000863}
864
Rui Ueyama34f29242015-10-13 19:51:57 +0000865// Returns true if a symbol can be replaced at load-time by a symbol
866// with the same name defined in other ELF executable or DSO.
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000867bool lld::elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000868 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000869 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000870 if (Body->isShared())
871 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000872
873 if (Body->isUndefined()) {
874 if (!Body->isWeak())
875 return true;
876
877 // This is an horrible corner case. Ideally we would like to say that any
878 // undefined symbol can be preempted so that the dynamic linker has a
879 // chance of finding it at runtime.
880 //
881 // The problem is that the code sequence used to test for weak undef
882 // functions looks like
883 // if (func) func()
884 // If the code is -fPIC the first reference is a load from the got and
885 // everything works.
886 // If the code is not -fPIC there is no reasonable way to solve it:
887 // * A relocation writing to the text segment will fail (it is ro).
888 // * A copy relocation doesn't work for functions.
889 // * The trick of using a plt entry as the address would fail here since
890 // the plt entry would have a non zero address.
891 // Since we cannot do anything better, we just resolve the symbol to 0 and
892 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000893 //
894 // As an extra hack, assume that if we are producing a shared library the
895 // user knows what he or she is doing and can handle a dynamic relocation.
896 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000897 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000898 if (!Config->Shared)
899 return false;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000900 return Body->getVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000901}
902
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000903template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000904 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000905 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000906}
907
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000908template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000909EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t sh_type,
910 uintX_t sh_flags)
911 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
912
913template <class ELFT>
914EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
915 : S(S), Index(Index) {}
916
917template <class ELFT> StringRef EHRegion<ELFT>::data() const {
918 ArrayRef<uint8_t> SecData = S->getSectionData();
919 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
920 size_t Start = Offsets[Index].first;
921 size_t End =
922 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
923 return StringRef((const char *)SecData.data() + Start, End - Start);
924}
925
926template <class ELFT>
927Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
928 : EHRegion<ELFT>(S, Index) {}
929
930template <class ELFT>
931template <bool IsRela>
932void EHOutputSection<ELFT>::addSectionAux(
933 EHInputSection<ELFT> *S,
934 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
935 const endianness E = ELFT::TargetEndianness;
936
937 S->OutSec = this;
938 uint32_t Align = S->getAlign();
939 if (Align > this->Header.sh_addralign)
940 this->Header.sh_addralign = Align;
941
942 Sections.push_back(S);
943
944 ArrayRef<uint8_t> SecData = S->getSectionData();
945 ArrayRef<uint8_t> D = SecData;
946 uintX_t Offset = 0;
947 auto RelI = Rels.begin();
948 auto RelE = Rels.end();
949
George Rimar147747a2016-01-02 16:55:01 +0000950 DenseMap<unsigned, unsigned> OffsetToIndex;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000951 while (!D.empty()) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000952 unsigned Index = S->Offsets.size();
953 S->Offsets.push_back(std::make_pair(Offset, -1));
954
George Rimar003be4f2015-12-17 09:23:40 +0000955 uintX_t Length = readEntryLength(D);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000956 StringRef Entry((const char *)D.data(), Length);
957
958 while (RelI != RelE && RelI->r_offset < Offset)
959 ++RelI;
960 uintX_t NextOffset = Offset + Length;
961 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
962
963 uint32_t ID = read32<E>(D.data() + 4);
964 if (ID == 0) {
965 // CIE
966 Cie<ELFT> C(S, Index);
967
968 StringRef Personality;
969 if (HasReloc) {
970 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
971 SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl();
972 Personality = Body.getName();
973 }
974
975 std::pair<StringRef, StringRef> CieInfo(Entry, Personality);
976 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
George Rimar147747a2016-01-02 16:55:01 +0000977 if (P.second) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000978 Cies.push_back(C);
George Rimar147747a2016-01-02 16:55:01 +0000979 this->Header.sh_size += RoundUpToAlignment(Length, sizeof(uintX_t));
980 }
981 OffsetToIndex[Offset] = P.first->second;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000982 } else {
983 if (!HasReloc)
984 error("FDE doesn't reference another section");
985 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
986 if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) {
987 uint32_t CieOffset = Offset + 4 - ID;
George Rimar147747a2016-01-02 16:55:01 +0000988 auto I = OffsetToIndex.find(CieOffset);
989 if (I == OffsetToIndex.end())
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000990 error("Invalid CIE reference");
George Rimar147747a2016-01-02 16:55:01 +0000991 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
George Rimare72beba2015-12-21 09:38:59 +0000992 this->Header.sh_size += RoundUpToAlignment(Length, sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000993 }
994 }
995
996 Offset = NextOffset;
997 D = D.slice(Length);
998 }
999}
1000
1001template <class ELFT>
George Rimar003be4f2015-12-17 09:23:40 +00001002typename EHOutputSection<ELFT>::uintX_t
1003EHOutputSection<ELFT>::readEntryLength(ArrayRef<uint8_t> D) {
1004 const endianness E = ELFT::TargetEndianness;
1005
1006 if (D.size() < 4)
1007 error("Truncated CIE/FDE length");
1008 uint64_t Len = read32<E>(D.data());
1009 if (Len < UINT32_MAX) {
1010 if (Len > (UINT32_MAX - 4))
1011 error("CIE/FIE size is too large");
1012 if (Len + 4 > D.size())
1013 error("CIE/FIE ends past the end of the section");
1014 return Len + 4;
1015 }
1016
1017 if (D.size() < 12)
1018 error("Truncated CIE/FDE length");
1019 Len = read64<E>(D.data() + 4);
1020 if (Len > (UINT64_MAX - 12))
1021 error("CIE/FIE size is too large");
1022 if (Len + 12 > D.size())
1023 error("CIE/FIE ends past the end of the section");
1024 return Len + 12;
1025}
1026
1027template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001028void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1029 auto *S = cast<EHInputSection<ELFT>>(C);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001030 const Elf_Shdr *RelSec = S->RelocSection;
1031 if (!RelSec)
1032 return addSectionAux(
1033 S, make_range((const Elf_Rela *)nullptr, (const Elf_Rela *)nullptr));
1034 ELFFile<ELFT> &Obj = S->getFile()->getObj();
1035 if (RelSec->sh_type == SHT_RELA)
1036 return addSectionAux(S, Obj.relas(RelSec));
1037 return addSectionAux(S, Obj.rels(RelSec));
1038}
1039
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001040template <class ELFT>
1041static typename ELFFile<ELFT>::uintX_t writeAlignedCieOrFde(StringRef Data,
1042 uint8_t *Buf) {
1043 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1044 const endianness E = ELFT::TargetEndianness;
1045 uint64_t Len = RoundUpToAlignment(Data.size(), sizeof(uintX_t));
1046 write32<E>(Buf, Len - 4);
1047 memcpy(Buf + 4, Data.data() + 4, Data.size() - 4);
1048 return Len;
1049}
1050
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001051template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1052 const endianness E = ELFT::TargetEndianness;
1053 size_t Offset = 0;
1054 for (const Cie<ELFT> &C : Cies) {
1055 size_t CieOffset = Offset;
1056
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001057 uintX_t CIELen = writeAlignedCieOrFde<ELFT>(C.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001058 C.S->Offsets[C.Index].second = Offset;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001059 Offset += CIELen;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001060
1061 for (const EHRegion<ELFT> &F : C.Fdes) {
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001062 uintX_t Len = writeAlignedCieOrFde<ELFT>(F.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001063 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001064 F.S->Offsets[F.Index].second = Offset;
George Rimare72beba2015-12-21 09:38:59 +00001065 Offset += Len;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001066 }
1067 }
1068
1069 for (EHInputSection<ELFT> *S : Sections) {
1070 const Elf_Shdr *RelSec = S->RelocSection;
1071 if (!RelSec)
1072 continue;
1073 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1074 if (RelSec->sh_type == SHT_RELA)
1075 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1076 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001077 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001078 }
1079}
1080
1081template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +00001082MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t sh_type,
1083 uintX_t sh_flags)
1084 : OutputSectionBase<ELFT>(Name, sh_type, sh_flags) {}
1085
1086template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001087 if (shouldTailMerge()) {
1088 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001089 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001090 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001091 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001092 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1093 StringRef Data = P.first;
1094 memcpy(Buf + P.second, Data.data(), Data.size());
1095 }
1096}
1097
1098static size_t findNull(StringRef S, size_t EntSize) {
1099 // Optimize the common case.
1100 if (EntSize == 1)
1101 return S.find(0);
1102
1103 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1104 const char *B = S.begin() + I;
1105 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1106 return I;
1107 }
1108 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001109}
1110
1111template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001112void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1113 auto *S = cast<MergeInputSection<ELFT>>(C);
Rafael Espindolac159c962015-10-19 21:00:02 +00001114 S->OutSec = this;
1115 uint32_t Align = S->getAlign();
1116 if (Align > this->Header.sh_addralign)
1117 this->Header.sh_addralign = Align;
1118
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001119 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001120 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001121 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001122 uintX_t Offset = 0;
1123
1124 if (this->Header.sh_flags & SHF_STRINGS) {
1125 while (!Data.empty()) {
1126 size_t End = findNull(Data, EntSize);
1127 if (End == StringRef::npos)
1128 error("String is not null terminated");
1129 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001130 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001131 if (shouldTailMerge())
1132 OutputOffset = -1;
1133 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1134 uintX_t Size = End + EntSize;
1135 Data = Data.substr(Size);
1136 Offset += Size;
1137 }
1138 } else {
1139 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1140 StringRef Entry = Data.substr(I, EntSize);
1141 size_t OutputOffset = Builder.add(Entry);
1142 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1143 Offset += EntSize;
1144 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001145 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001146}
1147
1148template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001149unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1150 return Builder.getOffset(Val);
1151}
1152
1153template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1154 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1155}
1156
1157template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1158 if (shouldTailMerge())
1159 Builder.finalize();
1160 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001161}
1162
1163template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001164StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
1165 : OutputSectionBase<ELFT>(Name, llvm::ELF::SHT_STRTAB,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001166 Dynamic ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001167 Dynamic(Dynamic) {
1168 this->Header.sh_addralign = 1;
1169}
1170
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001171template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001172 StringRef Data = StrTabBuilder.data();
1173 memcpy(Buf, Data.data(), Data.size());
1174}
1175
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001176template <class ELFT>
Rafael Espindola444576d2015-10-09 19:25:07 +00001177bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File,
1178 StringRef SymName,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001179 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
1180 if (Sym.getType() == STT_SECTION)
1181 return false;
1182
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001183 InputSectionBase<ELFT> *Sec = File.getSection(Sym);
Rafael Espindola444576d2015-10-09 19:25:07 +00001184 // If sym references a section in a discarded group, don't keep it.
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001185 if (Sec == &InputSection<ELFT>::Discarded)
Rafael Espindola4cda5812015-10-16 15:29:48 +00001186 return false;
Rafael Espindola444576d2015-10-09 19:25:07 +00001187
Davide Italiano6993ba42015-09-26 00:47:56 +00001188 if (Config->DiscardNone)
1189 return true;
1190
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001191 // In ELF assembly .L symbols are normally discarded by the assembler.
1192 // If the assembler fails to do so, the linker discards them if
1193 // * --discard-locals is used.
1194 // * The symbol is in a SHF_MERGE section, which is normally the reason for
1195 // the assembler keeping the .L symbol.
Rafael Espindola29925632015-12-11 19:09:21 +00001196 if (!SymName.startswith(".L") && !SymName.empty())
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001197 return true;
1198
1199 if (Config->DiscardLocals)
1200 return false;
1201
1202 return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE);
Davide Italiano6993ba42015-09-26 00:47:56 +00001203}
1204
Rafael Espindola35c6af32015-09-25 17:19:10 +00001205template <class ELFT>
1206SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001207 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
1208 : OutputSectionBase<ELFT>(
Rafael Espindola35c6af32015-09-25 17:19:10 +00001209 StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1210 StrTabSec.isDynamic() ? llvm::ELF::SHT_DYNSYM : llvm::ELF::SHT_SYMTAB,
1211 StrTabSec.isDynamic() ? (uintX_t)llvm::ELF::SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001212 Table(Table), StrTabSec(StrTabSec) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001213 typedef OutputSectionBase<ELFT> Base;
1214 typename Base::Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +00001215
1216 Header.sh_entsize = sizeof(Elf_Sym);
1217 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
1218}
1219
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001220// Orders symbols according to their positions in the GOT,
1221// in compliance with MIPS ABI rules.
1222// See "Global Offset Table" in Chapter 5 in the following document
1223// for detailed description:
1224// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1225static bool sortMipsSymbols(SymbolBody *L, SymbolBody *R) {
1226 if (!L->isInGot() || !R->isInGot())
1227 return R->isInGot();
1228 return L->GotIndex < R->GotIndex;
1229}
1230
Rui Ueyama0db335f2015-10-07 16:58:54 +00001231template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001232 if (this->Header.sh_size)
1233 return; // Already finalized.
1234
Rui Ueyama0db335f2015-10-07 16:58:54 +00001235 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001236 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001237 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001238
1239 if (!StrTabSec.isDynamic()) {
1240 std::stable_sort(Symbols.begin(), Symbols.end(),
Igor Kudrinf1d60292015-10-28 07:05:56 +00001241 [](SymbolBody *L, SymbolBody *R) {
1242 return getSymbolBinding(L) == STB_LOCAL &&
1243 getSymbolBinding(R) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001244 });
1245 return;
1246 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001247 if (Out<ELFT>::GnuHashTab)
1248 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1249 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001250 else if (Config->EMachine == EM_MIPS)
1251 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001252 size_t I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +00001253 for (SymbolBody *B : Symbols)
Rui Ueyamaa02bba62015-12-17 00:12:04 +00001254 B->DynamicSymbolTableIndex = ++I;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001255}
1256
1257template <class ELFT>
Igor Kudrinab665fc2015-10-20 21:47:58 +00001258void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
Rui Ueyama0db335f2015-10-07 16:58:54 +00001259 StrTabSec.add(Name);
1260 ++NumVisible;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001261 ++NumLocals;
1262}
1263
1264template <class ELFT>
1265void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
1266 StrTabSec.add(Body->getName());
Igor Kudrinf1d60292015-10-28 07:05:56 +00001267 Symbols.push_back(Body);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001268 ++NumVisible;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001269}
1270
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001271template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001272 Buf += sizeof(Elf_Sym);
1273
1274 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1275 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001276 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1277 writeLocalSymbols(Buf);
1278
1279 writeGlobalSymbols(Buf);
1280}
1281
1282template <class ELFT>
1283void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1284 // Iterate over all input object files to copy their local symbols
1285 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001286 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
1287 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001288 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001289 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +00001290 error(SymNameOrErr);
1291 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001292 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001293 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +00001294
Rui Ueyamac55733e2015-09-30 00:54:29 +00001295 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001296 uintX_t VA = 0;
Rafael Espindola4cda5812015-10-16 15:29:48 +00001297 if (Sym.st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001298 ESym->st_shndx = SHN_ABS;
Rafael Espindolac159c962015-10-19 21:00:02 +00001299 VA = Sym.st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001300 } else {
Rafael Espindola48225b42015-10-23 19:55:11 +00001301 InputSectionBase<ELFT> *Section = File->getSection(Sym);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001302 if (!Section->isLive())
1303 continue;
Rafael Espindolac159c962015-10-19 21:00:02 +00001304 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1305 ESym->st_shndx = OutSec->SectionIndex;
1306 VA += OutSec->getVA() + Section->getOffset(Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001307 }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001308 ESym->st_name = StrTabSec.getOffset(SymName);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001309 ESym->st_size = Sym.st_size;
1310 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001311 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001312 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001313 }
1314 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001315}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001316
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001317template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001318static const typename llvm::object::ELFFile<ELFT>::Elf_Sym *
1319getElfSym(SymbolBody &Body) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001320 if (auto *EBody = dyn_cast<DefinedElf<ELFT>>(&Body))
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001321 return &EBody->Sym;
1322 if (auto *EBody = dyn_cast<UndefinedElf<ELFT>>(&Body))
1323 return &EBody->Sym;
1324 return nullptr;
1325}
1326
1327template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001328void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001329 // Write the internal symbol table contents to the output symbol table
1330 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001331 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +00001332 for (SymbolBody *Body : Symbols) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001333 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001334
Rafael Espindola8614c562015-10-06 14:33:58 +00001335 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001336 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001337 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001338 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001339 case SymbolBody::DefinedRegularKind: {
1340 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001341 if (InputSectionBase<ELFT> *Sec = Sym->Section) {
1342 if (!Sec->isLive())
1343 continue;
1344 OutSec = Sec->OutSec;
1345 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001346 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001347 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001348 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001349 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001350 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001351 case SymbolBody::SharedKind: {
Rui Ueyamabb936062015-12-17 01:14:23 +00001352 if (cast<SharedSymbol<ELFT>>(Body)->NeedsCopy)
George Rimarbc590fe2015-10-28 16:48:58 +00001353 OutSec = Out<ELFT>::Bss;
1354 break;
1355 }
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001356 case SymbolBody::UndefinedElfKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001357 case SymbolBody::UndefinedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001358 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001359 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001360 }
1361
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001362 StringRef Name = Body->getName();
Rui Ueyama9fbb3d82015-10-24 17:44:52 +00001363 ESym->st_name = StrTabSec.getOffset(Name);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001364
Rafael Espindola8614c562015-10-06 14:33:58 +00001365 unsigned char Type = STT_NOTYPE;
1366 uintX_t Size = 0;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001367 if (const Elf_Sym *InputSym = getElfSym<ELFT>(*Body)) {
1368 Type = InputSym->getType();
1369 Size = InputSym->st_size;
Rafael Espindola11191912015-12-24 16:23:37 +00001370 } else if (auto *C = dyn_cast<DefinedCommon>(Body)) {
1371 Type = STT_OBJECT;
1372 Size = C->Size;
Rafael Espindola8614c562015-10-06 14:33:58 +00001373 }
1374
Igor Kudrin853b88d2015-10-20 20:52:14 +00001375 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001376 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001377 ESym->setVisibility(Body->getVisibility());
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001378 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001379
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001380 if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001381 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001382 else if (isa<DefinedRegular<ELFT>>(Body))
1383 ESym->st_shndx = SHN_ABS;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001384
1385 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001386 }
1387}
1388
Igor Kudrin853b88d2015-10-20 20:52:14 +00001389template <class ELFT>
1390uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001391 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001392 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1393 return STB_LOCAL;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001394 if (const Elf_Sym *ESym = getElfSym<ELFT>(*Body))
1395 return ESym->getBinding();
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001396 if (isa<DefinedSynthetic<ELFT>>(Body))
1397 return STB_LOCAL;
Igor Kudrin853b88d2015-10-20 20:52:14 +00001398 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1399}
1400
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001401template <class ELFT>
1402MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1403 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1404 this->Header.sh_addralign = 4;
1405 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1406 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1407}
1408
1409template <class ELFT>
1410void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1411 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
1412 R->ri_gp_value = getMipsGpAddr<ELFT>();
1413 R->ri_gprmask = GeneralMask;
1414}
1415
1416template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001417void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1418 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001419 GeneralMask |= S->getGeneralMask();
1420}
1421
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001422namespace lld {
1423namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001424template class OutputSectionBase<ELF32LE>;
1425template class OutputSectionBase<ELF32BE>;
1426template class OutputSectionBase<ELF64LE>;
1427template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001428
George Rimar648a2c32015-10-20 08:54:27 +00001429template class GotPltSection<ELF32LE>;
1430template class GotPltSection<ELF32BE>;
1431template class GotPltSection<ELF64LE>;
1432template class GotPltSection<ELF64BE>;
1433
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001434template class GotSection<ELF32LE>;
1435template class GotSection<ELF32BE>;
1436template class GotSection<ELF64LE>;
1437template class GotSection<ELF64BE>;
1438
1439template class PltSection<ELF32LE>;
1440template class PltSection<ELF32BE>;
1441template class PltSection<ELF64LE>;
1442template class PltSection<ELF64BE>;
1443
1444template class RelocationSection<ELF32LE>;
1445template class RelocationSection<ELF32BE>;
1446template class RelocationSection<ELF64LE>;
1447template class RelocationSection<ELF64BE>;
1448
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001449template class InterpSection<ELF32LE>;
1450template class InterpSection<ELF32BE>;
1451template class InterpSection<ELF64LE>;
1452template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001453
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001454template class GnuHashTableSection<ELF32LE>;
1455template class GnuHashTableSection<ELF32BE>;
1456template class GnuHashTableSection<ELF64LE>;
1457template class GnuHashTableSection<ELF64BE>;
1458
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001459template class HashTableSection<ELF32LE>;
1460template class HashTableSection<ELF32BE>;
1461template class HashTableSection<ELF64LE>;
1462template class HashTableSection<ELF64BE>;
1463
1464template class DynamicSection<ELF32LE>;
1465template class DynamicSection<ELF32BE>;
1466template class DynamicSection<ELF64LE>;
1467template class DynamicSection<ELF64BE>;
1468
1469template class OutputSection<ELF32LE>;
1470template class OutputSection<ELF32BE>;
1471template class OutputSection<ELF64LE>;
1472template class OutputSection<ELF64BE>;
1473
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001474template class EHOutputSection<ELF32LE>;
1475template class EHOutputSection<ELF32BE>;
1476template class EHOutputSection<ELF64LE>;
1477template class EHOutputSection<ELF64BE>;
1478
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001479template class MipsReginfoOutputSection<ELF32LE>;
1480template class MipsReginfoOutputSection<ELF32BE>;
1481template class MipsReginfoOutputSection<ELF64LE>;
1482template class MipsReginfoOutputSection<ELF64BE>;
1483
Rafael Espindolac159c962015-10-19 21:00:02 +00001484template class MergeOutputSection<ELF32LE>;
1485template class MergeOutputSection<ELF32BE>;
1486template class MergeOutputSection<ELF64LE>;
1487template class MergeOutputSection<ELF64BE>;
1488
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001489template class StringTableSection<ELF32LE>;
1490template class StringTableSection<ELF32BE>;
1491template class StringTableSection<ELF64LE>;
1492template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001493
1494template class SymbolTableSection<ELF32LE>;
1495template class SymbolTableSection<ELF32BE>;
1496template class SymbolTableSection<ELF64LE>;
1497template class SymbolTableSection<ELF64BE>;
1498
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001499template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1500template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1501template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1502template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +00001503
Rafael Espindola56f965f2015-09-21 22:48:12 +00001504template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001505getLocalRelTarget(const ObjectFile<ELF32LE> &,
George Rimar0b8ed1d2015-12-21 10:37:33 +00001506 const ELFFile<ELF32LE>::Elf_Rel &,
1507 ELFFile<ELF32LE>::uintX_t Addend);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001508template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001509getLocalRelTarget(const ObjectFile<ELF32BE> &,
George Rimar0b8ed1d2015-12-21 10:37:33 +00001510 const ELFFile<ELF32BE>::Elf_Rel &,
1511 ELFFile<ELF32BE>::uintX_t Addend);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001512template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001513getLocalRelTarget(const ObjectFile<ELF64LE> &,
George Rimar0b8ed1d2015-12-21 10:37:33 +00001514 const ELFFile<ELF64LE>::Elf_Rel &,
1515 ELFFile<ELF64LE>::uintX_t Addend);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001516template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001517getLocalRelTarget(const ObjectFile<ELF64BE> &,
George Rimar0b8ed1d2015-12-21 10:37:33 +00001518 const ELFFile<ELF64BE>::Elf_Rel &,
1519 ELFFile<ELF64BE>::uintX_t Addend);
Rafael Espindolac159c962015-10-19 21:00:02 +00001520
Rafael Espindola444576d2015-10-09 19:25:07 +00001521template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1522 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001523 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001524template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1525 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001526 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001527template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1528 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001529 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001530template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1531 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001532 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001533}
1534}