blob: d8ec21c722a4904cdbd0aaa8dfc6c220bee4c412 [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"
George Rimarf6bc65a2016-01-15 13:34:52 +000014#include "llvm/Support/Dwarf.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000015#include "llvm/Support/MathExtras.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000016#include <map>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000017
Rafael Espindola5805c4f2015-09-21 21:38:08 +000018using namespace llvm;
19using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000020using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021using namespace llvm::ELF;
22
23using namespace lld;
24using namespace lld::elf2;
25
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000026template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +000027OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t Type,
28 uintX_t 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));
Rui Ueyamad97e5c42016-01-07 18:33:11 +000031 Header.sh_type = Type;
32 Header.sh_flags = Flags;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000033}
34
Rafael Espindola35c6af32015-09-25 17:19:10 +000035template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000036GotPltSection<ELFT>::GotPltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000037 : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
George Rimar648a2c32015-10-20 08:54:27 +000038 this->Header.sh_addralign = sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000039}
40
41template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama724d6252016-01-29 01:49:32 +000042 Sym->GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
George Rimar648a2c32015-10-20 08:54:27 +000043 Entries.push_back(Sym);
44}
45
46template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +000047 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +000048}
49
George Rimar648a2c32015-10-20 08:54:27 +000050template <class ELFT> void GotPltSection<ELFT>::finalize() {
Igor Kudrin351b41d2015-11-16 17:44:08 +000051 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +000052 (Target->GotPltHeaderEntriesNum + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000053}
54
55template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +000056 Target->writeGotPltHeader(Buf);
Rui Ueyama724d6252016-01-29 01:49:32 +000057 Buf += Target->GotPltHeaderEntriesNum * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000058 for (const SymbolBody *B : Entries) {
Rui Ueyamab5a69702016-02-01 21:00:35 +000059 Target->writeGotPlt(Buf, B->getPltVA<ELFT>());
George Rimar648a2c32015-10-20 08:54:27 +000060 Buf += sizeof(uintX_t);
61 }
62}
63
64template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000065GotSection<ELFT>::GotSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000066 : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000067 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +000068 this->Header.sh_flags |= SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000069 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000070}
71
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000073 Sym->GotIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074 Entries.push_back(Sym);
George Rimar687138c2015-11-13 16:28:53 +000075}
76
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000077template <class ELFT> void GotSection<ELFT>::addMipsLocalEntry() {
78 ++MipsLocalEntries;
79}
80
George Rimar90cd0a82015-12-01 19:20:26 +000081template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) {
82 if (Sym->hasGlobalDynIndex())
83 return false;
Rui Ueyama724d6252016-01-29 01:49:32 +000084 Sym->GlobalDynIndex = Target->GotHeaderEntriesNum + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +000085 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +000086 Entries.push_back(Sym);
87 Entries.push_back(nullptr);
George Rimar90cd0a82015-12-01 19:20:26 +000088 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000089}
90
George Rimar95c1a582015-12-07 08:02:20 +000091template <class ELFT> bool GotSection<ELFT>::addCurrentModuleTlsIndex() {
George Rimarb17f7392015-12-01 18:24:07 +000092 if (LocalTlsIndexOff != uint32_t(-1))
93 return false;
Michael J. Spencer1e225612015-11-11 01:00:24 +000094 Entries.push_back(nullptr);
95 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +000096 LocalTlsIndexOff = (Entries.size() - 2) * sizeof(uintX_t);
97 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +000098}
99
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000100template <class ELFT>
101typename GotSection<ELFT>::uintX_t
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000102GotSection<ELFT>::getMipsLocalFullAddr(const SymbolBody &B) {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000103 return getMipsLocalEntryAddr(B.getVA<ELFT>());
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000104}
105
106template <class ELFT>
107typename GotSection<ELFT>::uintX_t
108GotSection<ELFT>::getMipsLocalPageAddr(uintX_t EntryValue) {
109 // Initialize the entry by the %hi(EntryValue) expression
110 // but without right-shifting.
111 return getMipsLocalEntryAddr((EntryValue + 0x8000) & ~0xffff);
112}
113
114template <class ELFT>
115typename GotSection<ELFT>::uintX_t
116GotSection<ELFT>::getMipsLocalEntryAddr(uintX_t EntryValue) {
Rui Ueyama724d6252016-01-29 01:49:32 +0000117 size_t NewIndex = Target->GotHeaderEntriesNum + MipsLocalGotPos.size();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000118 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
119 assert(!P.second || MipsLocalGotPos.size() <= MipsLocalEntries);
120 return this->getVA() + P.first->second * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000121}
122
Igor Kudrin304860a2015-11-12 04:39:49 +0000123template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000124typename GotSection<ELFT>::uintX_t
125GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
126 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
127}
128
129template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000130const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
131 return Entries.empty() ? nullptr : Entries.front();
132}
133
134template <class ELFT>
135unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
Rui Ueyama724d6252016-01-29 01:49:32 +0000136 return Target->GotHeaderEntriesNum + MipsLocalEntries;
Igor Kudrin304860a2015-11-12 04:39:49 +0000137}
138
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000139template <class ELFT> void GotSection<ELFT>::finalize() {
140 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +0000141 (Target->GotHeaderEntriesNum + MipsLocalEntries + Entries.size()) *
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000142 sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000143}
144
Rafael Espindolaa6627382015-10-06 23:56:53 +0000145template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000146 Target->writeGotHeader(Buf);
Rui Ueyama5cbf5d22016-02-02 02:29:03 +0000147 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000148 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
149 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, L.first);
150 }
Rui Ueyama724d6252016-01-29 01:49:32 +0000151 Buf += Target->GotHeaderEntriesNum * sizeof(uintX_t);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000152 Buf += MipsLocalEntries * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000153 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000154 uint8_t *Entry = Buf;
155 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000156 if (!B)
157 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000158 // MIPS has special rules to fill up GOT entries.
159 // See "Global Offset Table" in Chapter 5 in the following document
160 // for detailed description:
161 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
162 // As the first approach, we can just store addresses for all symbols.
163 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000164 continue; // The dynamic linker will take care of it.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000165 uintX_t VA = B->getVA<ELFT>();
Rafael Espindolae782f672015-10-07 03:56:05 +0000166 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000167 }
168}
169
Rafael Espindola35c6af32015-09-25 17:19:10 +0000170template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000171PltSection<ELFT>::PltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000172 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000173 this->Header.sh_addralign = 16;
174}
175
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000177 size_t Off = 0;
Rui Ueyama9398f862016-01-29 04:15:02 +0000178 if (Target->UseLazyBinding) {
Rui Ueyama74937fc2016-02-02 02:53:58 +0000179 // At beginning of PLT, we have code to call the dynamic linker
180 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama900e2d22016-01-29 03:51:49 +0000181 Target->writePltZero(Buf);
Rui Ueyama62515452016-01-29 03:00:32 +0000182 Off += Target->PltZeroSize;
George Rimar648a2c32015-10-20 08:54:27 +0000183 }
George Rimarfb5d7f22015-11-26 19:58:51 +0000184 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000185 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000186 unsigned RelOff = I.second;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000187 uint64_t Got =
188 Target->UseLazyBinding ? B->getGotPltVA<ELFT>() : B->getGotVA<ELFT>();
Rui Ueyama242ddf42015-10-12 18:56:36 +0000189 uint64_t Plt = this->getVA() + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000190 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000191 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000192 }
193}
194
195template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000196 Sym->PltIndex = Entries.size();
Rui Ueyama724d6252016-01-29 01:49:32 +0000197 unsigned RelOff = Target->UseLazyBinding
George Rimar77b77792015-11-25 22:15:01 +0000198 ? Out<ELFT>::RelaPlt->getRelocOffset()
199 : Out<ELFT>::RelaDyn->getRelocOffset();
200 Entries.push_back(std::make_pair(Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000201}
202
George Rimar648a2c32015-10-20 08:54:27 +0000203template <class ELFT> void PltSection<ELFT>::finalize() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000204 this->Header.sh_size =
Rui Ueyama62515452016-01-29 03:00:32 +0000205 Target->PltZeroSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000206}
207
208template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000209RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000210 : OutputSectionBase<ELFT>(Name, IsRela ? SHT_RELA : SHT_REL, SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000211 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000212 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000213 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000214}
215
George Rimar5828c232015-11-30 17:49:19 +0000216// Applies corresponding symbol and type for dynamic tls relocation.
217// Returns true if relocation was handled.
218template <class ELFT>
219bool RelocationSection<ELFT>::applyTlsDynamicReloc(SymbolBody *Body,
220 uint32_t Type, Elf_Rel *P,
221 Elf_Rel *N) {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000222 if (Target->isTlsLocalDynamicRel(Type)) {
Rui Ueyama724d6252016-01-29 01:49:32 +0000223 P->setSymbolAndType(0, Target->TlsModuleIndexRel, Config->Mips64EL);
George Rimarb17f7392015-12-01 18:24:07 +0000224 P->r_offset = Out<ELFT>::Got->getLocalTlsIndexVA();
George Rimar5828c232015-11-30 17:49:19 +0000225 return true;
226 }
227
Rui Ueyamac516ae12016-01-29 02:33:45 +0000228 if (!Body || !Target->isTlsGlobalDynamicRel(Type))
George Rimar25411f252015-12-04 11:20:13 +0000229 return false;
230
Rui Ueyamabaf16512016-01-29 00:20:12 +0000231 if (Target->canRelaxTls(Type, Body)) {
Rui Ueyama572a6f72016-01-29 01:49:33 +0000232 P->setSymbolAndType(Body->DynsymIndex, Target->getTlsGotRel(),
Rui Ueyama724d6252016-01-29 01:49:32 +0000233 Config->Mips64EL);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000234 P->r_offset = Body->getGotVA<ELFT>();
George Rimar5828c232015-11-30 17:49:19 +0000235 return true;
236 }
George Rimar25411f252015-12-04 11:20:13 +0000237
Rui Ueyama572a6f72016-01-29 01:49:33 +0000238 P->setSymbolAndType(Body->DynsymIndex, Target->TlsModuleIndexRel,
Rui Ueyama724d6252016-01-29 01:49:32 +0000239 Config->Mips64EL);
George Rimar25411f252015-12-04 11:20:13 +0000240 P->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body);
Rui Ueyama572a6f72016-01-29 01:49:33 +0000241 N->setSymbolAndType(Body->DynsymIndex, Target->TlsOffsetRel,
Rui Ueyama724d6252016-01-29 01:49:32 +0000242 Config->Mips64EL);
George Rimar25411f252015-12-04 11:20:13 +0000243 N->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body) + sizeof(uintX_t);
244 return true;
George Rimar5828c232015-11-30 17:49:19 +0000245}
246
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000249 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000250 Buf += IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola50534c22015-09-22 17:49:38 +0000251
Michael J. Spencer627ae702015-11-13 00:28:34 +0000252 // Skip placeholder for global dynamic TLS relocation pair. It was already
253 // handled by the previous relocation.
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000254 if (!Rel.C)
Michael J. Spencer627ae702015-11-13 00:28:34 +0000255 continue;
256
257 InputSectionBase<ELFT> &C = *Rel.C;
258 const Elf_Rel &RI = *Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000259 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000260 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000261 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000262 if (Body)
263 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000264
Rui Ueyama64558522015-10-16 22:51:43 +0000265 uint32_t Type = RI.getType(Config->Mips64EL);
George Rimar5828c232015-11-30 17:49:19 +0000266 if (applyTlsDynamicReloc(Body, Type, P, reinterpret_cast<Elf_Rel *>(Buf)))
Michael J. Spencer1e225612015-11-11 01:00:24 +0000267 continue;
Rui Ueyamabef81f32016-01-21 20:59:22 +0000268
Rui Ueyama1546fb22016-01-26 01:03:21 +0000269 // Writer::scanRelocs creates a RELATIVE reloc for some type of TLS reloc.
270 // We want to write it down as is.
Rui Ueyama724d6252016-01-29 01:49:32 +0000271 if (Type == Target->RelativeRel) {
Rui Ueyama1546fb22016-01-26 01:03:21 +0000272 P->setSymbolAndType(0, Type, Config->Mips64EL);
273 P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA();
274 continue;
275 }
276
Rui Ueyamabef81f32016-01-21 20:59:22 +0000277 // Emit a copy relocation.
278 auto *SS = dyn_cast_or_null<SharedSymbol<ELFT>>(Body);
279 if (SS && SS->NeedsCopy) {
Rui Ueyama572a6f72016-01-29 01:49:33 +0000280 P->setSymbolAndType(Body->DynsymIndex, Target->CopyRel, Config->Mips64EL);
Rui Ueyamabef81f32016-01-21 20:59:22 +0000281 P->r_offset = Out<ELFT>::Bss->getVA() + SS->OffsetInBss;
282 continue;
283 }
284
Rui Ueyamac516ae12016-01-29 02:33:45 +0000285 bool NeedsGot = Body && Target->needsGot(Type, *Body);
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000286 bool CBP = canBePreempted(Body, NeedsGot);
Rui Ueyamab0210e832016-01-26 00:24:57 +0000287
288 // For a symbol with STT_GNU_IFUNC type, we always create a PLT and
289 // a GOT entry for the symbol, and emit an IRELATIVE reloc rather than
290 // the usual JUMP_SLOT reloc for the GOT entry. For the details, you
291 // want to read http://www.airs.com/blog/archives/403
292 if (!CBP && Body && isGnuIFunc<ELFT>(*Body)) {
Rui Ueyama724d6252016-01-29 01:49:32 +0000293 P->setSymbolAndType(0, Target->IRelativeRel, Config->Mips64EL);
Rui Ueyamab0210e832016-01-26 00:24:57 +0000294 if (Out<ELFT>::GotPlt)
Rui Ueyamab5a69702016-02-01 21:00:35 +0000295 P->r_offset = Body->getGotPltVA<ELFT>();
Rui Ueyamab0210e832016-01-26 00:24:57 +0000296 else
Rui Ueyamab5a69702016-02-01 21:00:35 +0000297 P->r_offset = Body->getGotVA<ELFT>();
Rui Ueyamab0210e832016-01-26 00:24:57 +0000298 continue;
299 }
300
Rui Ueyama724d6252016-01-29 01:49:32 +0000301 bool LazyReloc =
Rui Ueyamac516ae12016-01-29 02:33:45 +0000302 Body && Target->UseLazyBinding && Target->needsPlt(Type, *Body);
Rafael Espindola49757522015-10-19 19:58:18 +0000303
George Rimarc7dc0be2015-12-15 08:48:39 +0000304 unsigned Reloc;
Rui Ueyama1546fb22016-01-26 01:03:21 +0000305 if (!CBP)
Rui Ueyama724d6252016-01-29 01:49:32 +0000306 Reloc = Target->RelativeRel;
George Rimarc7dc0be2015-12-15 08:48:39 +0000307 else if (LazyReloc)
Rui Ueyama724d6252016-01-29 01:49:32 +0000308 Reloc = Target->PltRel;
George Rimarc7dc0be2015-12-15 08:48:39 +0000309 else if (NeedsGot)
Rui Ueyama724d6252016-01-29 01:49:32 +0000310 Reloc = Body->isTls() ? Target->getTlsGotRel() : Target->GotRel;
George Rimarc7dc0be2015-12-15 08:48:39 +0000311 else
Rui Ueyamac516ae12016-01-29 02:33:45 +0000312 Reloc = Target->getDynRel(Type);
Rui Ueyama572a6f72016-01-29 01:49:33 +0000313 P->setSymbolAndType(CBP ? Body->DynsymIndex : 0, Reloc, Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000314
George Rimar4b5346f2015-12-29 16:17:32 +0000315 if (LazyReloc)
Rui Ueyamab5a69702016-02-01 21:00:35 +0000316 P->r_offset = Body->getGotPltVA<ELFT>();
George Rimar4b5346f2015-12-29 16:17:32 +0000317 else if (NeedsGot)
Rui Ueyamab5a69702016-02-01 21:00:35 +0000318 P->r_offset = Body->getGotVA<ELFT>();
George Rimar4b5346f2015-12-29 16:17:32 +0000319 else
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000320 P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA();
Rafael Espindola49757522015-10-19 19:58:18 +0000321
Rui Ueyamad6cea142016-01-26 04:58:58 +0000322 if (!IsRela)
323 continue;
Rafael Espindola49757522015-10-19 19:58:18 +0000324
Rui Ueyamad6cea142016-01-26 04:58:58 +0000325 auto R = static_cast<const Elf_Rela &>(RI);
Rui Ueyama3ae28a42016-01-26 07:17:27 +0000326 auto S = static_cast<Elf_Rela *>(P);
327 uintX_t A = NeedsGot ? 0 : R.r_addend;
Rui Ueyama1546fb22016-01-26 01:03:21 +0000328 if (CBP)
Rui Ueyama3ae28a42016-01-26 07:17:27 +0000329 S->r_addend = A;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000330 else if (Body)
Rui Ueyamab5a69702016-02-01 21:00:35 +0000331 S->r_addend = Body->getVA<ELFT>() + A;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000332 else
Rui Ueyama3ae28a42016-01-26 07:17:27 +0000333 S->r_addend = getLocalRelTarget(File, R, A);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000334 }
335}
336
George Rimar77b77792015-11-25 22:15:01 +0000337template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
338 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
339 return EntrySize * Relocs.size();
340}
341
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000342template <class ELFT> void RelocationSection<ELFT>::finalize() {
George Rimara07ff662015-12-21 10:12:06 +0000343 this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex
344 : Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000345 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
346}
347
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000348template <class ELFT>
349InterpSection<ELFT>::InterpSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000350 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000351 this->Header.sh_size = Config->DynamicLinker.size() + 1;
352 this->Header.sh_addralign = 1;
353}
354
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000355template <class ELFT>
356void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
357 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000358}
359
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000360template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000361 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
362}
363
Rafael Espindola35c6af32015-09-25 17:19:10 +0000364template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000365HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000366 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000367 this->Header.sh_entsize = sizeof(Elf_Word);
368 this->Header.sh_addralign = sizeof(Elf_Word);
369}
370
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000371static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000372 uint32_t H = 0;
373 for (char C : Name) {
374 H = (H << 4) + C;
375 uint32_t G = H & 0xf0000000;
376 if (G)
377 H ^= G >> 24;
378 H &= ~G;
379 }
380 return H;
381}
382
Rui Ueyama0db335f2015-10-07 16:58:54 +0000383template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000384 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000385
Rui Ueyama0db335f2015-10-07 16:58:54 +0000386 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000387 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000388
389 // Create as many buckets as there are symbols.
390 // FIXME: This is simplistic. We can try to optimize it, but implementing
391 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000392 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000393 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
394}
395
396template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000397 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000398 auto *P = reinterpret_cast<Elf_Word *>(Buf);
399 *P++ = NumSymbols; // nbucket
400 *P++ = NumSymbols; // nchain
401
402 Elf_Word *Buckets = P;
403 Elf_Word *Chains = P + NumSymbols;
404
Rafael Espindolae2c24612016-01-29 01:24:25 +0000405 for (const std::pair<SymbolBody *, unsigned> &P :
406 Out<ELFT>::DynSymTab->getSymbols()) {
407 SymbolBody *Body = P.first;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000408 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000409 unsigned I = Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000410 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000411 Chains[I] = Buckets[Hash];
412 Buckets[Hash] = I;
413 }
414}
415
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000416static uint32_t hashGnu(StringRef Name) {
417 uint32_t H = 5381;
418 for (uint8_t C : Name)
419 H = (H << 5) + H + C;
420 return H;
421}
422
423template <class ELFT>
424GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000425 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000426 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000427 this->Header.sh_addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000428}
429
430template <class ELFT>
431unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
432 if (!NumHashed)
433 return 0;
434
435 // These values are prime numbers which are not greater than 2^(N-1) + 1.
436 // In result, for any particular NumHashed we return a prime number
437 // which is not greater than NumHashed.
438 static const unsigned Primes[] = {
439 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
440 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
441
442 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
443 array_lengthof(Primes) - 1)];
444}
445
446// Bloom filter estimation: at least 8 bits for each hashed symbol.
447// GNU Hash table requirement: it should be a power of 2,
448// the minimum value is 1, even for an empty table.
449// Expected results for a 32-bit target:
450// calcMaskWords(0..4) = 1
451// calcMaskWords(5..8) = 2
452// calcMaskWords(9..16) = 4
453// For a 64-bit target:
454// calcMaskWords(0..8) = 1
455// calcMaskWords(9..16) = 2
456// calcMaskWords(17..32) = 4
457template <class ELFT>
458unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
459 if (!NumHashed)
460 return 1;
461 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
462}
463
464template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000465 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000466 NBuckets = calcNBuckets(NumHashed);
467 MaskWords = calcMaskWords(NumHashed);
468 // Second hash shift estimation: just predefined values.
469 Shift2 = ELFT::Is64Bits ? 6 : 5;
470
471 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
472 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
473 + sizeof(Elf_Off) * MaskWords // Bloom Filter
474 + sizeof(Elf_Word) * NBuckets // Hash Buckets
475 + sizeof(Elf_Word) * NumHashed; // Hash Values
476}
477
478template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
479 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000480 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000481 return;
482 writeBloomFilter(Buf);
483 writeHashTable(Buf);
484}
485
486template <class ELFT>
487void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
488 auto *P = reinterpret_cast<Elf_Word *>(Buf);
489 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000490 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000491 *P++ = MaskWords;
492 *P++ = Shift2;
493 Buf = reinterpret_cast<uint8_t *>(P);
494}
495
496template <class ELFT>
497void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
498 unsigned C = sizeof(Elf_Off) * 8;
499
500 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000501 for (const HashedSymbolData &Item : HashedSymbols) {
502 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
503 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
504 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000505 Masks[Pos] |= V;
506 }
507 Buf += sizeof(Elf_Off) * MaskWords;
508}
509
510template <class ELFT>
511void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
512 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
513 Elf_Word *Values = Buckets + NBuckets;
514
515 int PrevBucket = -1;
516 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000517 for (const HashedSymbolData &Item : HashedSymbols) {
518 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000519 assert(PrevBucket <= Bucket);
520 if (Bucket != PrevBucket) {
Rui Ueyama572a6f72016-01-29 01:49:33 +0000521 Buckets[Bucket] = Item.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000522 PrevBucket = Bucket;
523 if (I > 0)
524 Values[I - 1] |= 1;
525 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000526 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000527 ++I;
528 }
529 if (I > 0)
530 Values[I - 1] |= 1;
531}
532
Rafael Espindola31f88882015-11-02 14:33:11 +0000533static bool includeInGnuHashTable(SymbolBody *B) {
Rui Ueyamac112c1b2016-01-29 02:17:01 +0000534 // Assume that includeInDynsym() is already checked.
Rafael Espindola31f88882015-11-02 14:33:11 +0000535 return !B->isUndefined();
536}
537
Rafael Espindola35c6af32015-09-25 17:19:10 +0000538template <class ELFT>
Rafael Espindolae2c24612016-01-29 01:24:25 +0000539void GnuHashTableSection<ELFT>::addSymbols(
540 std::vector<std::pair<SymbolBody *, unsigned>> &Symbols) {
541 std::vector<std::pair<SymbolBody *, unsigned>> NotHashed;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000542 NotHashed.reserve(Symbols.size());
543 HashedSymbols.reserve(Symbols.size());
Rafael Espindolae2c24612016-01-29 01:24:25 +0000544 for (const std::pair<SymbolBody *, unsigned> &P : Symbols) {
545 SymbolBody *B = P.first;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000546 if (includeInGnuHashTable(B))
Rafael Espindolae2c24612016-01-29 01:24:25 +0000547 HashedSymbols.push_back(
548 HashedSymbolData{B, P.second, hashGnu(B->getName())});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000549 else
Rafael Espindolae2c24612016-01-29 01:24:25 +0000550 NotHashed.push_back(P);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000551 }
552 if (HashedSymbols.empty())
553 return;
554
555 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
556 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
557 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
558 return L.Hash % NBuckets < R.Hash % NBuckets;
559 });
560
561 Symbols = std::move(NotHashed);
562 for (const HashedSymbolData &Item : HashedSymbols)
Rafael Espindolae2c24612016-01-29 01:24:25 +0000563 Symbols.push_back(std::make_pair(Item.Body, Item.STName));
Igor Kudrinf1d60292015-10-28 07:05:56 +0000564}
565
566template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000567DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000568 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000569 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000570 Elf_Shdr &Header = this->Header;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000571 Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000572 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000573
574 // .dynamic section is not writable on MIPS.
575 // See "Special Section" in Chapter 4 in the following document:
576 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
577 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000578 Header.sh_flags = SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000579}
580
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000581template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000582 if (this->Header.sh_size)
583 return; // Already finalized.
584
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000585 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000586 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000587
Rafael Espindolae2c24612016-01-29 01:24:25 +0000588 auto Add = [=](Entry E) { Entries.push_back(E); };
589
590 // Add strings. We know that these are the last strings to be added to
Rafael Espindolade069362016-01-25 21:32:04 +0000591 // DynStrTab and doing this here allows this function to set DT_STRSZ.
592 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000593 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
594 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rafael Espindolade069362016-01-25 21:32:04 +0000595 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
596 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000597 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
598 if (!Config->SoName.empty())
599 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
600
Rafael Espindolade069362016-01-25 21:32:04 +0000601 Out<ELFT>::DynStrTab->finalize();
602
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000603 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindolade069362016-01-25 21:32:04 +0000604 bool IsRela = Out<ELFT>::RelaDyn->isRela();
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000605 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
606 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
607 Add({IsRela ? DT_RELAENT : DT_RELENT,
608 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000609 }
George Rimar648a2c32015-10-20 08:54:27 +0000610 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000611 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
612 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
613 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
614 Out<ELFT>::GotPlt});
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000615 Add({DT_PLTREL, uint64_t(Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000616 }
617
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000618 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
619 Add({DT_SYMENT, sizeof(Elf_Sym)});
620 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
621 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000622 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000623 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000624 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000625 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000626
Rafael Espindolade069362016-01-25 21:32:04 +0000627 if (PreInitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000628 Add({DT_PREINIT_ARRAY, PreInitArraySec});
629 Add({DT_PREINIT_ARRAYSZ, PreInitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000630 }
631 if (InitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000632 Add({DT_INIT_ARRAY, InitArraySec});
633 Add({DT_INIT_ARRAYSZ, (uintX_t)InitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000634 }
635 if (FiniArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000636 Add({DT_FINI_ARRAY, FiniArraySec});
637 Add({DT_FINI_ARRAYSZ, (uintX_t)FiniArraySec->getSize()});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000638 }
639
Rui Ueyama304d1352016-01-25 21:47:25 +0000640 if (SymbolBody *B = SymTab.find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000641 Add({DT_INIT, B});
Rui Ueyama304d1352016-01-25 21:47:25 +0000642 if (SymbolBody *B = SymTab.find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000643 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000644
Rafael Espindolade069362016-01-25 21:32:04 +0000645 uint32_t DtFlags = 0;
646 uint32_t DtFlags1 = 0;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000647 if (Config->Bsymbolic)
648 DtFlags |= DF_SYMBOLIC;
649 if (Config->ZNodelete)
650 DtFlags1 |= DF_1_NODELETE;
651 if (Config->ZNow) {
652 DtFlags |= DF_BIND_NOW;
653 DtFlags1 |= DF_1_NOW;
654 }
655 if (Config->ZOrigin) {
656 DtFlags |= DF_ORIGIN;
657 DtFlags1 |= DF_1_ORIGIN;
658 }
659
660 if (DtFlags)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000661 Add({DT_FLAGS, DtFlags});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000662 if (DtFlags1)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000663 Add({DT_FLAGS_1, DtFlags1});
Igor Kudrin304860a2015-11-12 04:39:49 +0000664
Ed Mastef5d3cf62016-01-06 15:52:27 +0000665 if (!Config->Entry.empty())
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000666 Add({DT_DEBUG, (uint64_t)0});
Ed Mastef5d3cf62016-01-06 15:52:27 +0000667
Igor Kudrin304860a2015-11-12 04:39:49 +0000668 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000669 Add({DT_MIPS_RLD_VERSION, 1});
670 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
671 Add({DT_MIPS_BASE_ADDRESS, (uintX_t)Target->getVAStart()});
672 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
673 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000674 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000675 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000676 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000677 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
678 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000679 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000680 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000681 }
682
Rafael Espindolade069362016-01-25 21:32:04 +0000683 // +1 for DT_NULL
684 Header.sh_size = (Entries.size() + 1) * Header.sh_entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000685}
686
687template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000688 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
689
Rafael Espindolade069362016-01-25 21:32:04 +0000690 for (const Entry &E : Entries) {
691 P->d_tag = E.Tag;
692 switch (E.Kind) {
693 case Entry::SecAddr:
694 P->d_un.d_ptr = E.OutSec->getVA();
695 break;
696 case Entry::SymAddr:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000697 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
Rafael Espindolade069362016-01-25 21:32:04 +0000698 break;
699 case Entry::PlainInt:
700 P->d_un.d_val = E.Val;
701 break;
702 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000703 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000704 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000705}
706
707template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000708EhFrameHeader<ELFT>::EhFrameHeader()
709 : OutputSectionBase<ELFT>(".eh_frame_hdr", llvm::ELF::SHT_PROGBITS,
710 SHF_ALLOC) {
711 // It's a 4 bytes of header + pointer to the contents of the .eh_frame section
712 // + the number of FDE pointers in the table.
713 this->Header.sh_size = 12;
714}
715
716// We have to get PC values of FDEs. They depend on relocations
717// which are target specific, so we run this code after performing
718// all relocations. We read the values from ouput buffer according to the
719// encoding given for FDEs. Return value is an offset to the initial PC value
720// for the FDE.
721template <class ELFT>
722typename EhFrameHeader<ELFT>::uintX_t
723EhFrameHeader<ELFT>::getFdePc(uintX_t EhVA, const FdeData &F) {
724 const endianness E = ELFT::TargetEndianness;
725 assert((F.Enc & 0xF0) != dwarf::DW_EH_PE_datarel);
726
727 uintX_t FdeOff = EhVA + F.Off + 8;
728 switch (F.Enc & 0xF) {
729 case dwarf::DW_EH_PE_udata2:
730 case dwarf::DW_EH_PE_sdata2:
731 return FdeOff + read16<E>(F.PCRel);
732 case dwarf::DW_EH_PE_udata4:
733 case dwarf::DW_EH_PE_sdata4:
734 return FdeOff + read32<E>(F.PCRel);
735 case dwarf::DW_EH_PE_udata8:
736 case dwarf::DW_EH_PE_sdata8:
737 return FdeOff + read64<E>(F.PCRel);
738 case dwarf::DW_EH_PE_absptr:
739 if (sizeof(uintX_t) == 8)
740 return FdeOff + read64<E>(F.PCRel);
741 return FdeOff + read32<E>(F.PCRel);
742 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000743 fatal("unknown FDE size encoding");
George Rimarf6bc65a2016-01-15 13:34:52 +0000744}
745
746template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
747 const endianness E = ELFT::TargetEndianness;
748
749 const uint8_t Header[] = {1, dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4,
750 dwarf::DW_EH_PE_udata4,
751 dwarf::DW_EH_PE_datarel | dwarf::DW_EH_PE_sdata4};
752 memcpy(Buf, Header, sizeof(Header));
753
754 uintX_t EhVA = Sec->getVA();
755 uintX_t VA = this->getVA();
756 uintX_t EhOff = EhVA - VA - 4;
757 write32<E>(Buf + 4, EhOff);
758 write32<E>(Buf + 8, this->FdeList.size());
759 Buf += 12;
760
761 // InitialPC -> Offset in .eh_frame, sorted by InitialPC.
762 std::map<uintX_t, size_t> PcToOffset;
763 for (const FdeData &F : FdeList)
764 PcToOffset[getFdePc(EhVA, F)] = F.Off;
765
766 for (auto &I : PcToOffset) {
767 // The first four bytes are an offset to the initial PC value for the FDE.
768 write32<E>(Buf, I.first - VA);
769 // The last four bytes are an offset to the FDE data itself.
770 write32<E>(Buf + 4, EhVA + I.second - VA);
771 Buf += 8;
772 }
773}
774
775template <class ELFT>
776void EhFrameHeader<ELFT>::assignEhFrame(EHOutputSection<ELFT> *Sec) {
George Rimar45ca88d2016-01-25 19:27:50 +0000777 assert((!this->Sec || this->Sec == Sec) &&
778 "multiple .eh_frame sections not supported for .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000779 Live = Config->EhFrameHdr;
780 this->Sec = Sec;
781}
782
783template <class ELFT>
784void EhFrameHeader<ELFT>::addFde(uint8_t Enc, size_t Off, uint8_t *PCRel) {
785 if (Live && (Enc & 0xF0) == dwarf::DW_EH_PE_datarel)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000786 fatal("DW_EH_PE_datarel encoding unsupported for FDEs by .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000787 FdeList.push_back(FdeData{Enc, Off, PCRel});
788}
789
790template <class ELFT> void EhFrameHeader<ELFT>::reserveFde() {
791 // Each FDE entry is 8 bytes long:
792 // The first four bytes are an offset to the initial PC value for the FDE. The
793 // last four byte are an offset to the FDE data itself.
794 this->Header.sh_size += 8;
795}
796
797template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000798OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type,
799 uintX_t Flags)
800 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000801
802template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000803void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
804 auto *S = cast<InputSection<ELFT>>(C);
805 Sections.push_back(S);
806 S->OutSec = this;
807 uint32_t Align = S->getAlign();
Rui Ueyama65d98ea2016-01-29 20:31:05 +0000808 this->updateAlign(Align);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000809
810 uintX_t Off = this->Header.sh_size;
Rui Ueyama489a8062016-01-14 20:53:50 +0000811 Off = alignTo(Off, Align);
Rui Ueyama40845e62015-12-26 05:51:07 +0000812 S->OutSecOff = Off;
813 Off += S->getSize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000814 this->Header.sh_size = Off;
815}
816
Rui Ueyama34f29242015-10-13 19:51:57 +0000817// Returns a VA which a relocatin RI refers to. Used only for local symbols.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000818// For non-local symbols, use SymbolBody::getVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000819template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000820typename ELFFile<ELFT>::uintX_t
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000821elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
822 const Elf_Rel_Impl<ELFT, IsRela> &RI,
823 typename ELFFile<ELFT>::uintX_t Addend) {
Rafael Espindola932efcf2015-10-19 20:24:44 +0000824 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
825 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
826
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000827 // PPC64 has a special relocation representing the TOC base pointer
828 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000829 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000830 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000831
Rui Ueyama126d08f2015-10-12 20:28:22 +0000832 const Elf_Sym *Sym =
833 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000834
Rui Ueyama126d08f2015-10-12 20:28:22 +0000835 if (!Sym)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000836 fatal("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000837
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000838 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
839
840 if (Sym->getType() == STT_TLS)
841 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
George Rimarcc06a6f2015-11-29 14:14:20 +0000842 Out<ELFT>::TlsPhdr->p_vaddr;
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000843
Rafael Espindola444576d2015-10-09 19:25:07 +0000844 // According to the ELF spec reference to a local symbol from outside
845 // the group are not allowed. Unfortunately .eh_frame breaks that rule
846 // and must be treated specially. For now we just replace the symbol with
847 // 0.
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000848 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000849 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000850
Rafael Espindolac159c962015-10-19 21:00:02 +0000851 uintX_t VA = Section->OutSec->getVA();
852 if (isa<InputSection<ELFT>>(Section))
853 return VA + Section->getOffset(*Sym) + Addend;
854
855 uintX_t Offset = Sym->st_value;
856 if (Sym->getType() == STT_SECTION) {
857 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000858 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000859 }
Rafael Espindola7f040bf2015-12-25 01:00:41 +0000860 return VA + Section->getOffset(Offset) + Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000861}
862
Rui Ueyama34f29242015-10-13 19:51:57 +0000863// Returns true if a symbol can be replaced at load-time by a symbol
864// with the same name defined in other ELF executable or DSO.
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000865bool elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000866 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000867 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000868 if (Body->isShared())
869 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000870
871 if (Body->isUndefined()) {
872 if (!Body->isWeak())
873 return true;
874
875 // This is an horrible corner case. Ideally we would like to say that any
876 // undefined symbol can be preempted so that the dynamic linker has a
877 // chance of finding it at runtime.
878 //
879 // The problem is that the code sequence used to test for weak undef
880 // functions looks like
881 // if (func) func()
882 // If the code is -fPIC the first reference is a load from the got and
883 // everything works.
884 // If the code is not -fPIC there is no reasonable way to solve it:
885 // * A relocation writing to the text segment will fail (it is ro).
886 // * A copy relocation doesn't work for functions.
887 // * The trick of using a plt entry as the address would fail here since
888 // the plt entry would have a non zero address.
889 // Since we cannot do anything better, we just resolve the symbol to 0 and
890 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000891 //
892 // As an extra hack, assume that if we are producing a shared library the
893 // user knows what he or she is doing and can handle a dynamic relocation.
894 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000895 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000896 if (!Config->Shared)
897 return false;
George Rimar5c36e592016-02-02 09:28:53 +0000898 if (Body->getVisibility() != STV_DEFAULT)
899 return false;
900 if (Config->Bsymbolic || (Config->BsymbolicFunctions && Body->isFunc()))
901 return false;
902 return true;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000903}
904
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000905template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000906 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000907 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000908}
909
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000910template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000911EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t Type,
912 uintX_t Flags)
George Rimarf6bc65a2016-01-15 13:34:52 +0000913 : OutputSectionBase<ELFT>(Name, Type, Flags) {
914 Out<ELFT>::EhFrameHdr->assignEhFrame(this);
915}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000916
917template <class ELFT>
918EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
919 : S(S), Index(Index) {}
920
921template <class ELFT> StringRef EHRegion<ELFT>::data() const {
922 ArrayRef<uint8_t> SecData = S->getSectionData();
923 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
924 size_t Start = Offsets[Index].first;
925 size_t End =
926 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
927 return StringRef((const char *)SecData.data() + Start, End - Start);
928}
929
930template <class ELFT>
931Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
932 : EHRegion<ELFT>(S, Index) {}
933
George Rimarf6bc65a2016-01-15 13:34:52 +0000934// Read a byte and advance D by one byte.
935static uint8_t readByte(ArrayRef<uint8_t> &D) {
936 if (D.empty())
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000937 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000938 uint8_t B = D.front();
939 D = D.slice(1);
940 return B;
941}
942
943static void skipLeb128(ArrayRef<uint8_t> &D) {
944 while (!D.empty()) {
945 uint8_t Val = D.front();
946 D = D.slice(1);
947 if ((Val & 0x80) == 0)
948 return;
949 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000950 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000951}
952
953template <class ELFT> static unsigned getSizeForEncoding(unsigned Enc) {
954 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
955 switch (Enc & 0x0f) {
956 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000957 fatal("unknown FDE encoding");
George Rimarf6bc65a2016-01-15 13:34:52 +0000958 case dwarf::DW_EH_PE_absptr:
959 case dwarf::DW_EH_PE_signed:
960 return sizeof(uintX_t);
961 case dwarf::DW_EH_PE_udata2:
962 case dwarf::DW_EH_PE_sdata2:
963 return 2;
964 case dwarf::DW_EH_PE_udata4:
965 case dwarf::DW_EH_PE_sdata4:
966 return 4;
967 case dwarf::DW_EH_PE_udata8:
968 case dwarf::DW_EH_PE_sdata8:
969 return 8;
970 }
971}
972
973template <class ELFT>
974uint8_t EHOutputSection<ELFT>::getFdeEncoding(ArrayRef<uint8_t> D) {
975 auto Check = [](bool C) {
976 if (!C)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000977 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000978 };
979
980 Check(D.size() >= 8);
981 D = D.slice(8);
982
983 uint8_t Version = readByte(D);
984 if (Version != 1 && Version != 3)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000985 fatal("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
George Rimarf6bc65a2016-01-15 13:34:52 +0000986
987 auto AugEnd = std::find(D.begin() + 1, D.end(), '\0');
988 Check(AugEnd != D.end());
989 ArrayRef<uint8_t> AugString(D.begin(), AugEnd - D.begin());
990 D = D.slice(AugString.size() + 1);
991
992 // Code alignment factor should always be 1 for .eh_frame.
993 if (readByte(D) != 1)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000994 fatal("CIE code alignment must be 1");
George Rimarf6bc65a2016-01-15 13:34:52 +0000995 // Skip data alignment factor
996 skipLeb128(D);
997
998 // Skip the return address register. In CIE version 1 this is a single
999 // byte. In CIE version 3 this is an unsigned LEB128.
1000 if (Version == 1)
1001 readByte(D);
1002 else
1003 skipLeb128(D);
1004
1005 while (!AugString.empty()) {
1006 switch (readByte(AugString)) {
1007 case 'z':
1008 skipLeb128(D);
1009 break;
1010 case 'R':
1011 return readByte(D);
1012 case 'P': {
1013 uint8_t Enc = readByte(D);
1014 if ((Enc & 0xf0) == dwarf::DW_EH_PE_aligned)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001015 fatal("DW_EH_PE_aligned encoding for address of a personality routine "
George Rimarf6bc65a2016-01-15 13:34:52 +00001016 "handler not supported");
1017 unsigned EncSize = getSizeForEncoding<ELFT>(Enc);
1018 Check(D.size() >= EncSize);
1019 D = D.slice(EncSize);
1020 break;
1021 }
1022 case 'S':
1023 case 'L':
1024 // L: Language Specific Data Area (LSDA) encoding
1025 // S: This CIE represents a stack frame for the invocation of a signal
1026 // handler
1027 break;
1028 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001029 fatal("unknown .eh_frame augmentation string value");
George Rimarf6bc65a2016-01-15 13:34:52 +00001030 }
1031 }
1032 return dwarf::DW_EH_PE_absptr;
1033}
1034
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001035template <class ELFT>
1036template <bool IsRela>
1037void EHOutputSection<ELFT>::addSectionAux(
1038 EHInputSection<ELFT> *S,
1039 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
1040 const endianness E = ELFT::TargetEndianness;
1041
1042 S->OutSec = this;
Rui Ueyama65d98ea2016-01-29 20:31:05 +00001043 this->updateAlign(S->getAlign());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001044 Sections.push_back(S);
1045
1046 ArrayRef<uint8_t> SecData = S->getSectionData();
1047 ArrayRef<uint8_t> D = SecData;
1048 uintX_t Offset = 0;
1049 auto RelI = Rels.begin();
1050 auto RelE = Rels.end();
1051
George Rimar147747a2016-01-02 16:55:01 +00001052 DenseMap<unsigned, unsigned> OffsetToIndex;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001053 while (!D.empty()) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001054 unsigned Index = S->Offsets.size();
1055 S->Offsets.push_back(std::make_pair(Offset, -1));
1056
George Rimar003be4f2015-12-17 09:23:40 +00001057 uintX_t Length = readEntryLength(D);
George Rimarf6bc65a2016-01-15 13:34:52 +00001058 // If CIE/FDE data length is zero then Length is 4, this
1059 // shall be considered a terminator and processing shall end.
1060 if (Length == 4)
1061 break;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001062 StringRef Entry((const char *)D.data(), Length);
1063
1064 while (RelI != RelE && RelI->r_offset < Offset)
1065 ++RelI;
1066 uintX_t NextOffset = Offset + Length;
1067 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
1068
1069 uint32_t ID = read32<E>(D.data() + 4);
1070 if (ID == 0) {
1071 // CIE
1072 Cie<ELFT> C(S, Index);
George Rimarf6bc65a2016-01-15 13:34:52 +00001073 if (Config->EhFrameHdr)
1074 C.FdeEncoding = getFdeEncoding(D);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001075
1076 StringRef Personality;
1077 if (HasReloc) {
1078 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
1079 SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl();
1080 Personality = Body.getName();
1081 }
1082
1083 std::pair<StringRef, StringRef> CieInfo(Entry, Personality);
1084 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
George Rimar147747a2016-01-02 16:55:01 +00001085 if (P.second) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001086 Cies.push_back(C);
Rui Ueyama489a8062016-01-14 20:53:50 +00001087 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
George Rimar147747a2016-01-02 16:55:01 +00001088 }
1089 OffsetToIndex[Offset] = P.first->second;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001090 } else {
1091 if (!HasReloc)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001092 fatal("FDE doesn't reference another section");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001093 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
1094 if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) {
1095 uint32_t CieOffset = Offset + 4 - ID;
George Rimar147747a2016-01-02 16:55:01 +00001096 auto I = OffsetToIndex.find(CieOffset);
1097 if (I == OffsetToIndex.end())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001098 fatal("Invalid CIE reference");
George Rimar147747a2016-01-02 16:55:01 +00001099 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
George Rimarf6bc65a2016-01-15 13:34:52 +00001100 Out<ELFT>::EhFrameHdr->reserveFde();
Rui Ueyama489a8062016-01-14 20:53:50 +00001101 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001102 }
1103 }
1104
1105 Offset = NextOffset;
1106 D = D.slice(Length);
1107 }
1108}
1109
1110template <class ELFT>
George Rimar003be4f2015-12-17 09:23:40 +00001111typename EHOutputSection<ELFT>::uintX_t
1112EHOutputSection<ELFT>::readEntryLength(ArrayRef<uint8_t> D) {
1113 const endianness E = ELFT::TargetEndianness;
1114
1115 if (D.size() < 4)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001116 fatal("Truncated CIE/FDE length");
George Rimar003be4f2015-12-17 09:23:40 +00001117 uint64_t Len = read32<E>(D.data());
1118 if (Len < UINT32_MAX) {
1119 if (Len > (UINT32_MAX - 4))
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001120 fatal("CIE/FIE size is too large");
George Rimar003be4f2015-12-17 09:23:40 +00001121 if (Len + 4 > D.size())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001122 fatal("CIE/FIE ends past the end of the section");
George Rimar003be4f2015-12-17 09:23:40 +00001123 return Len + 4;
1124 }
1125
1126 if (D.size() < 12)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001127 fatal("Truncated CIE/FDE length");
George Rimar003be4f2015-12-17 09:23:40 +00001128 Len = read64<E>(D.data() + 4);
1129 if (Len > (UINT64_MAX - 12))
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001130 fatal("CIE/FIE size is too large");
George Rimar003be4f2015-12-17 09:23:40 +00001131 if (Len + 12 > D.size())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001132 fatal("CIE/FIE ends past the end of the section");
George Rimar003be4f2015-12-17 09:23:40 +00001133 return Len + 12;
1134}
1135
1136template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001137void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1138 auto *S = cast<EHInputSection<ELFT>>(C);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001139 const Elf_Shdr *RelSec = S->RelocSection;
Rui Ueyama0de86c12016-01-27 22:23:44 +00001140 if (!RelSec) {
1141 addSectionAux(S, make_range<const Elf_Rela *>(nullptr, nullptr));
1142 return;
1143 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001144 ELFFile<ELFT> &Obj = S->getFile()->getObj();
1145 if (RelSec->sh_type == SHT_RELA)
Rui Ueyama0de86c12016-01-27 22:23:44 +00001146 addSectionAux(S, Obj.relas(RelSec));
1147 else
1148 addSectionAux(S, Obj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001149}
1150
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001151template <class ELFT>
1152static typename ELFFile<ELFT>::uintX_t writeAlignedCieOrFde(StringRef Data,
1153 uint8_t *Buf) {
1154 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1155 const endianness E = ELFT::TargetEndianness;
Rui Ueyama489a8062016-01-14 20:53:50 +00001156 uint64_t Len = alignTo(Data.size(), sizeof(uintX_t));
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001157 write32<E>(Buf, Len - 4);
1158 memcpy(Buf + 4, Data.data() + 4, Data.size() - 4);
1159 return Len;
1160}
1161
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001162template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1163 const endianness E = ELFT::TargetEndianness;
1164 size_t Offset = 0;
1165 for (const Cie<ELFT> &C : Cies) {
1166 size_t CieOffset = Offset;
1167
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001168 uintX_t CIELen = writeAlignedCieOrFde<ELFT>(C.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001169 C.S->Offsets[C.Index].second = Offset;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001170 Offset += CIELen;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001171
1172 for (const EHRegion<ELFT> &F : C.Fdes) {
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001173 uintX_t Len = writeAlignedCieOrFde<ELFT>(F.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001174 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001175 F.S->Offsets[F.Index].second = Offset;
George Rimarf6bc65a2016-01-15 13:34:52 +00001176 Out<ELFT>::EhFrameHdr->addFde(C.FdeEncoding, Offset, Buf + Offset + 8);
George Rimare72beba2015-12-21 09:38:59 +00001177 Offset += Len;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001178 }
1179 }
1180
1181 for (EHInputSection<ELFT> *S : Sections) {
1182 const Elf_Shdr *RelSec = S->RelocSection;
1183 if (!RelSec)
1184 continue;
1185 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1186 if (RelSec->sh_type == SHT_RELA)
1187 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1188 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001189 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001190 }
1191}
1192
1193template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001194MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
1195 uintX_t Flags)
1196 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001197
1198template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001199 if (shouldTailMerge()) {
1200 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001201 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001202 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001203 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001204 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1205 StringRef Data = P.first;
1206 memcpy(Buf + P.second, Data.data(), Data.size());
1207 }
1208}
1209
1210static size_t findNull(StringRef S, size_t EntSize) {
1211 // Optimize the common case.
1212 if (EntSize == 1)
1213 return S.find(0);
1214
1215 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1216 const char *B = S.begin() + I;
1217 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1218 return I;
1219 }
1220 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001221}
1222
1223template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001224void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1225 auto *S = cast<MergeInputSection<ELFT>>(C);
Rafael Espindolac159c962015-10-19 21:00:02 +00001226 S->OutSec = this;
Rui Ueyama65d98ea2016-01-29 20:31:05 +00001227 this->updateAlign(S->getAlign());
Rafael Espindolac159c962015-10-19 21:00:02 +00001228
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001229 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001230 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001231 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001232
Rui Ueyamaead75fc2016-01-29 22:18:55 +00001233 // If this is of type string, the contents are null-terminated strings.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001234 if (this->Header.sh_flags & SHF_STRINGS) {
Rui Ueyamaad87ff72016-01-06 02:52:24 +00001235 uintX_t Offset = 0;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001236 while (!Data.empty()) {
1237 size_t End = findNull(Data, EntSize);
1238 if (End == StringRef::npos)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001239 fatal("String is not null terminated");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001240 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001241 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001242 if (shouldTailMerge())
1243 OutputOffset = -1;
1244 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1245 uintX_t Size = End + EntSize;
1246 Data = Data.substr(Size);
1247 Offset += Size;
1248 }
Rui Ueyamaead75fc2016-01-29 22:18:55 +00001249 return;
1250 }
1251
1252 // If this is not of type string, every entry has the same size.
1253 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1254 StringRef Entry = Data.substr(I, EntSize);
1255 size_t OutputOffset = Builder.add(Entry);
1256 S->Offsets.push_back(std::make_pair(I, OutputOffset));
Rafael Espindolac159c962015-10-19 21:00:02 +00001257 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001258}
1259
1260template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001261unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1262 return Builder.getOffset(Val);
1263}
1264
1265template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1266 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1267}
1268
1269template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1270 if (shouldTailMerge())
1271 Builder.finalize();
1272 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001273}
1274
1275template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001276StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001277 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1278 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001279 Dynamic(Dynamic) {
1280 this->Header.sh_addralign = 1;
1281}
1282
Rafael Espindolae2c24612016-01-29 01:24:25 +00001283// Adds a string to the string table. If HashIt is true we hash and check for
1284// duplicates. It is optional because the name of global symbols are already
1285// uniqued and hashing them again has a big cost for a small value: uniquing
1286// them with some other string that happens to be the same.
1287template <class ELFT>
1288unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1289 if (HashIt) {
1290 auto R = StringMap.insert(std::make_pair(S, Size));
1291 if (!R.second)
1292 return R.first->second;
1293 }
1294 unsigned Ret = Size;
1295 Size += S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001296 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001297 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001298}
1299
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001300template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001301 // ELF string tables start with NUL byte, so advance the pointer by one.
1302 ++Buf;
1303 for (StringRef S : Strings) {
1304 memcpy(Buf, S.data(), S.size());
1305 Buf += S.size() + 1;
1306 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001307}
1308
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001309template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001310SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001311 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001312 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1313 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001314 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001315 StrTabSec(StrTabSec), Table(Table) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001316 this->Header.sh_entsize = sizeof(Elf_Sym);
Rui Ueyama5e378dd2016-01-29 22:18:57 +00001317 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +00001318}
1319
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001320// Orders symbols according to their positions in the GOT,
1321// in compliance with MIPS ABI rules.
1322// See "Global Offset Table" in Chapter 5 in the following document
1323// for detailed description:
1324// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rafael Espindolae2c24612016-01-29 01:24:25 +00001325static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
1326 const std::pair<SymbolBody *, unsigned> &R) {
1327 if (!L.first->isInGot() || !R.first->isInGot())
1328 return R.first->isInGot();
1329 return L.first->GotIndex < R.first->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001330}
1331
Rui Ueyama0db335f2015-10-07 16:58:54 +00001332template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001333 if (this->Header.sh_size)
1334 return; // Already finalized.
1335
Rui Ueyama0db335f2015-10-07 16:58:54 +00001336 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001337 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001338 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001339
1340 if (!StrTabSec.isDynamic()) {
1341 std::stable_sort(Symbols.begin(), Symbols.end(),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001342 [](const std::pair<SymbolBody *, unsigned> &L,
1343 const std::pair<SymbolBody *, unsigned> &R) {
1344 return getSymbolBinding(L.first) == STB_LOCAL &&
1345 getSymbolBinding(R.first) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001346 });
1347 return;
1348 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001349 if (Out<ELFT>::GnuHashTab)
1350 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1351 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001352 else if (Config->EMachine == EM_MIPS)
1353 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001354 size_t I = 0;
Rafael Espindolae2c24612016-01-29 01:24:25 +00001355 for (const std::pair<SymbolBody *, unsigned> &P : Symbols)
Rui Ueyama572a6f72016-01-29 01:49:33 +00001356 P.first->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001357}
1358
1359template <class ELFT>
1360void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001361 Symbols.push_back(
1362 std::make_pair(Body, StrTabSec.addString(Body->getName(), false)));
Rui Ueyama0db335f2015-10-07 16:58:54 +00001363}
1364
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001365template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001366 Buf += sizeof(Elf_Sym);
1367
1368 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1369 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001370 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1371 writeLocalSymbols(Buf);
1372
1373 writeGlobalSymbols(Buf);
1374}
1375
1376template <class ELFT>
1377void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1378 // Iterate over all input object files to copy their local symbols
1379 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001380 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001381 for (const std::pair<const Elf_Sym *, unsigned> &P : File->KeptLocalSyms) {
1382 const Elf_Sym *Sym = P.first;
Rafael Espindola444576d2015-10-09 19:25:07 +00001383
Rui Ueyamac55733e2015-09-30 00:54:29 +00001384 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001385 uintX_t VA = 0;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001386 if (Sym->st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001387 ESym->st_shndx = SHN_ABS;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001388 VA = Sym->st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001389 } else {
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001390 InputSectionBase<ELFT> *Section = File->getSection(*Sym);
Rafael Espindolac159c962015-10-19 21:00:02 +00001391 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1392 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001393 VA = Section->getOffset(*Sym);
Tom Stellard80efb162016-01-07 03:59:08 +00001394 // Symbol offsets for AMDGPU need to be the offset in bytes of the
1395 // symbol from the beginning of the section.
1396 if (Config->EMachine != EM_AMDGPU)
1397 VA += OutSec->getVA();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001398 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001399 ESym->st_name = P.second;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001400 ESym->st_size = Sym->st_size;
1401 ESym->setBindingAndType(Sym->getBinding(), Sym->getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001402 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001403 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001404 }
1405 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001406}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001407
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001408template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001409static const typename llvm::object::ELFFile<ELFT>::Elf_Sym *
1410getElfSym(SymbolBody &Body) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001411 if (auto *EBody = dyn_cast<DefinedElf<ELFT>>(&Body))
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001412 return &EBody->Sym;
1413 if (auto *EBody = dyn_cast<UndefinedElf<ELFT>>(&Body))
1414 return &EBody->Sym;
1415 return nullptr;
1416}
1417
1418template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001419void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001420 // Write the internal symbol table contents to the output symbol table
1421 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001422 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001423 for (const std::pair<SymbolBody *, unsigned> &P : Symbols) {
1424 SymbolBody *Body = P.first;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001425 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001426
Rafael Espindola8614c562015-10-06 14:33:58 +00001427 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001428 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001429 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001430 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001431 case SymbolBody::DefinedRegularKind: {
1432 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001433 if (InputSectionBase<ELFT> *Sec = Sym->Section) {
1434 if (!Sec->isLive())
1435 continue;
1436 OutSec = Sec->OutSec;
1437 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001438 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001439 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001440 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001441 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001442 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001443 case SymbolBody::SharedKind: {
Rui Ueyamabb936062015-12-17 01:14:23 +00001444 if (cast<SharedSymbol<ELFT>>(Body)->NeedsCopy)
George Rimarbc590fe2015-10-28 16:48:58 +00001445 OutSec = Out<ELFT>::Bss;
1446 break;
1447 }
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001448 case SymbolBody::UndefinedElfKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001449 case SymbolBody::UndefinedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001450 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001451 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001452 }
1453
Rafael Espindolae2c24612016-01-29 01:24:25 +00001454 ESym->st_name = P.second;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001455
Rafael Espindola8614c562015-10-06 14:33:58 +00001456 unsigned char Type = STT_NOTYPE;
1457 uintX_t Size = 0;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001458 if (const Elf_Sym *InputSym = getElfSym<ELFT>(*Body)) {
1459 Type = InputSym->getType();
1460 Size = InputSym->st_size;
Rafael Espindola11191912015-12-24 16:23:37 +00001461 } else if (auto *C = dyn_cast<DefinedCommon>(Body)) {
1462 Type = STT_OBJECT;
1463 Size = C->Size;
Rafael Espindola8614c562015-10-06 14:33:58 +00001464 }
1465
Igor Kudrin853b88d2015-10-20 20:52:14 +00001466 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001467 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001468 ESym->setVisibility(Body->getVisibility());
Rui Ueyamab5a69702016-02-01 21:00:35 +00001469 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001470
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001471 if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001472 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001473 else if (isa<DefinedRegular<ELFT>>(Body))
1474 ESym->st_shndx = SHN_ABS;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001475
1476 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001477 }
1478}
1479
Igor Kudrin853b88d2015-10-20 20:52:14 +00001480template <class ELFT>
1481uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001482 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001483 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1484 return STB_LOCAL;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001485 if (const Elf_Sym *ESym = getElfSym<ELFT>(*Body))
1486 return ESym->getBinding();
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001487 if (isa<DefinedSynthetic<ELFT>>(Body))
1488 return STB_LOCAL;
Igor Kudrin853b88d2015-10-20 20:52:14 +00001489 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1490}
1491
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001492template <class ELFT>
1493MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1494 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1495 this->Header.sh_addralign = 4;
1496 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1497 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1498}
1499
1500template <class ELFT>
1501void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1502 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
1503 R->ri_gp_value = getMipsGpAddr<ELFT>();
Rui Ueyama70eed362016-01-06 22:42:43 +00001504 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001505}
1506
1507template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001508void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001509 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001510 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001511 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001512}
1513
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001514namespace lld {
1515namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001516template class OutputSectionBase<ELF32LE>;
1517template class OutputSectionBase<ELF32BE>;
1518template class OutputSectionBase<ELF64LE>;
1519template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001520
George Rimarf6bc65a2016-01-15 13:34:52 +00001521template class EhFrameHeader<ELF32LE>;
1522template class EhFrameHeader<ELF32BE>;
1523template class EhFrameHeader<ELF64LE>;
1524template class EhFrameHeader<ELF64BE>;
1525
George Rimar648a2c32015-10-20 08:54:27 +00001526template class GotPltSection<ELF32LE>;
1527template class GotPltSection<ELF32BE>;
1528template class GotPltSection<ELF64LE>;
1529template class GotPltSection<ELF64BE>;
1530
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001531template class GotSection<ELF32LE>;
1532template class GotSection<ELF32BE>;
1533template class GotSection<ELF64LE>;
1534template class GotSection<ELF64BE>;
1535
1536template class PltSection<ELF32LE>;
1537template class PltSection<ELF32BE>;
1538template class PltSection<ELF64LE>;
1539template class PltSection<ELF64BE>;
1540
1541template class RelocationSection<ELF32LE>;
1542template class RelocationSection<ELF32BE>;
1543template class RelocationSection<ELF64LE>;
1544template class RelocationSection<ELF64BE>;
1545
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001546template class InterpSection<ELF32LE>;
1547template class InterpSection<ELF32BE>;
1548template class InterpSection<ELF64LE>;
1549template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001550
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001551template class GnuHashTableSection<ELF32LE>;
1552template class GnuHashTableSection<ELF32BE>;
1553template class GnuHashTableSection<ELF64LE>;
1554template class GnuHashTableSection<ELF64BE>;
1555
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001556template class HashTableSection<ELF32LE>;
1557template class HashTableSection<ELF32BE>;
1558template class HashTableSection<ELF64LE>;
1559template class HashTableSection<ELF64BE>;
1560
1561template class DynamicSection<ELF32LE>;
1562template class DynamicSection<ELF32BE>;
1563template class DynamicSection<ELF64LE>;
1564template class DynamicSection<ELF64BE>;
1565
1566template class OutputSection<ELF32LE>;
1567template class OutputSection<ELF32BE>;
1568template class OutputSection<ELF64LE>;
1569template class OutputSection<ELF64BE>;
1570
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001571template class EHOutputSection<ELF32LE>;
1572template class EHOutputSection<ELF32BE>;
1573template class EHOutputSection<ELF64LE>;
1574template class EHOutputSection<ELF64BE>;
1575
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001576template class MipsReginfoOutputSection<ELF32LE>;
1577template class MipsReginfoOutputSection<ELF32BE>;
1578template class MipsReginfoOutputSection<ELF64LE>;
1579template class MipsReginfoOutputSection<ELF64BE>;
1580
Rafael Espindolac159c962015-10-19 21:00:02 +00001581template class MergeOutputSection<ELF32LE>;
1582template class MergeOutputSection<ELF32BE>;
1583template class MergeOutputSection<ELF64LE>;
1584template class MergeOutputSection<ELF64BE>;
1585
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001586template class StringTableSection<ELF32LE>;
1587template class StringTableSection<ELF32BE>;
1588template class StringTableSection<ELF64LE>;
1589template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001590
1591template class SymbolTableSection<ELF32LE>;
1592template class SymbolTableSection<ELF32BE>;
1593template class SymbolTableSection<ELF64LE>;
1594template class SymbolTableSection<ELF64BE>;
1595
Rui Ueyama5ec41f32016-01-26 01:32:00 +00001596template uint32_t getLocalRelTarget(const ObjectFile<ELF32LE> &,
1597 const ELFFile<ELF32LE>::Elf_Rel &,
1598 uint32_t);
1599template uint32_t getLocalRelTarget(const ObjectFile<ELF32BE> &,
1600 const ELFFile<ELF32BE>::Elf_Rel &,
1601 uint32_t);
1602template uint64_t getLocalRelTarget(const ObjectFile<ELF64LE> &,
1603 const ELFFile<ELF64LE>::Elf_Rel &,
1604 uint64_t);
1605template uint64_t getLocalRelTarget(const ObjectFile<ELF64BE> &,
1606 const ELFFile<ELF64BE>::Elf_Rel &,
1607 uint64_t);
1608template uint32_t getLocalRelTarget(const ObjectFile<ELF32LE> &,
1609 const ELFFile<ELF32LE>::Elf_Rela &,
1610 uint32_t);
1611template uint32_t getLocalRelTarget(const ObjectFile<ELF32BE> &,
1612 const ELFFile<ELF32BE>::Elf_Rela &,
1613 uint32_t);
1614template uint64_t getLocalRelTarget(const ObjectFile<ELF64LE> &,
1615 const ELFFile<ELF64LE>::Elf_Rela &,
1616 uint64_t);
1617template uint64_t getLocalRelTarget(const ObjectFile<ELF64BE> &,
1618 const ELFFile<ELF64BE>::Elf_Rela &,
1619 uint64_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001620}
1621}