blob: 6922d350a85af6c2f1e2ade289bef2f1f3d0a91d [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
50template <class ELFT>
51typename GotPltSection<ELFT>::uintX_t
52GotPltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
53 return this->getVA() + B.GotPltIndex * sizeof(uintX_t);
54}
55
56template <class ELFT> void GotPltSection<ELFT>::finalize() {
Igor Kudrin351b41d2015-11-16 17:44:08 +000057 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +000058 (Target->GotPltHeaderEntriesNum + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000059}
60
61template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +000062 Target->writeGotPltHeader(Buf);
Rui Ueyama724d6252016-01-29 01:49:32 +000063 Buf += Target->GotPltHeaderEntriesNum * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000064 for (const SymbolBody *B : Entries) {
Rui Ueyamac516ae12016-01-29 02:33:45 +000065 Target->writeGotPlt(Buf, Out<ELFT>::Plt->getEntryAddr(*B));
George Rimar648a2c32015-10-20 08:54:27 +000066 Buf += sizeof(uintX_t);
67 }
68}
69
70template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000071GotSection<ELFT>::GotSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000072 : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000073 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +000074 this->Header.sh_flags |= SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000075 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000076}
77
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000079 Sym->GotIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080 Entries.push_back(Sym);
George Rimar687138c2015-11-13 16:28:53 +000081}
82
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000083template <class ELFT> void GotSection<ELFT>::addMipsLocalEntry() {
84 ++MipsLocalEntries;
85}
86
George Rimar90cd0a82015-12-01 19:20:26 +000087template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) {
88 if (Sym->hasGlobalDynIndex())
89 return false;
Rui Ueyama724d6252016-01-29 01:49:32 +000090 Sym->GlobalDynIndex = Target->GotHeaderEntriesNum + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +000091 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +000092 Entries.push_back(Sym);
93 Entries.push_back(nullptr);
George Rimar90cd0a82015-12-01 19:20:26 +000094 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095}
96
George Rimar95c1a582015-12-07 08:02:20 +000097template <class ELFT> bool GotSection<ELFT>::addCurrentModuleTlsIndex() {
George Rimarb17f7392015-12-01 18:24:07 +000098 if (LocalTlsIndexOff != uint32_t(-1))
99 return false;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000100 Entries.push_back(nullptr);
101 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +0000102 LocalTlsIndexOff = (Entries.size() - 2) * sizeof(uintX_t);
103 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000104}
105
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106template <class ELFT>
107typename GotSection<ELFT>::uintX_t
108GotSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000109 return this->getVA() +
Rui Ueyama724d6252016-01-29 01:49:32 +0000110 (Target->GotHeaderEntriesNum + MipsLocalEntries + B.GotIndex) *
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000111 sizeof(uintX_t);
112}
113
114template <class ELFT>
115typename GotSection<ELFT>::uintX_t
116GotSection<ELFT>::getMipsLocalFullAddr(const SymbolBody &B) {
117 return getMipsLocalEntryAddr(getSymVA<ELFT>(B));
118}
119
120template <class ELFT>
121typename GotSection<ELFT>::uintX_t
122GotSection<ELFT>::getMipsLocalPageAddr(uintX_t EntryValue) {
123 // Initialize the entry by the %hi(EntryValue) expression
124 // but without right-shifting.
125 return getMipsLocalEntryAddr((EntryValue + 0x8000) & ~0xffff);
126}
127
128template <class ELFT>
129typename GotSection<ELFT>::uintX_t
130GotSection<ELFT>::getMipsLocalEntryAddr(uintX_t EntryValue) {
Rui Ueyama724d6252016-01-29 01:49:32 +0000131 size_t NewIndex = Target->GotHeaderEntriesNum + MipsLocalGotPos.size();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000132 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
133 assert(!P.second || MipsLocalGotPos.size() <= MipsLocalEntries);
134 return this->getVA() + P.first->second * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000135}
136
Igor Kudrin304860a2015-11-12 04:39:49 +0000137template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000138typename GotSection<ELFT>::uintX_t
139GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
140 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
141}
142
143template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000144const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
145 return Entries.empty() ? nullptr : Entries.front();
146}
147
148template <class ELFT>
149unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
Rui Ueyama724d6252016-01-29 01:49:32 +0000150 return Target->GotHeaderEntriesNum + MipsLocalEntries;
Igor Kudrin304860a2015-11-12 04:39:49 +0000151}
152
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000153template <class ELFT> void GotSection<ELFT>::finalize() {
154 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +0000155 (Target->GotHeaderEntriesNum + MipsLocalEntries + Entries.size()) *
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000156 sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000157}
158
Rafael Espindolaa6627382015-10-06 23:56:53 +0000159template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000160 Target->writeGotHeader(Buf);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000161 for (const auto &L : MipsLocalGotPos) {
162 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
163 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, L.first);
164 }
Rui Ueyama724d6252016-01-29 01:49:32 +0000165 Buf += Target->GotHeaderEntriesNum * sizeof(uintX_t);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000166 Buf += MipsLocalEntries * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000167 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000168 uint8_t *Entry = Buf;
169 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000170 if (!B)
171 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000172 // MIPS has special rules to fill up GOT entries.
173 // See "Global Offset Table" in Chapter 5 in the following document
174 // for detailed description:
175 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
176 // As the first approach, we can just store addresses for all symbols.
177 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000178 continue; // The dynamic linker will take care of it.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000179 uintX_t VA = getSymVA<ELFT>(*B);
Rafael Espindolae782f672015-10-07 03:56:05 +0000180 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000181 }
182}
183
Rafael Espindola35c6af32015-09-25 17:19:10 +0000184template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000185PltSection<ELFT>::PltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000186 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000187 this->Header.sh_addralign = 16;
188}
189
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000190template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000191 size_t Off = 0;
Rui Ueyama9398f862016-01-29 04:15:02 +0000192 if (Target->UseLazyBinding) {
George Rimar648a2c32015-10-20 08:54:27 +0000193 // First write PLT[0] entry which is special.
Rui Ueyama900e2d22016-01-29 03:51:49 +0000194 Target->writePltZero(Buf);
Rui Ueyama62515452016-01-29 03:00:32 +0000195 Off += Target->PltZeroSize;
George Rimar648a2c32015-10-20 08:54:27 +0000196 }
George Rimarfb5d7f22015-11-26 19:58:51 +0000197 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000198 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000199 unsigned RelOff = I.second;
Rui Ueyama9398f862016-01-29 04:15:02 +0000200 uint64_t Got = Target->UseLazyBinding ? Out<ELFT>::GotPlt->getEntryAddr(*B)
201 : Out<ELFT>::Got->getEntryAddr(*B);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000202 uint64_t Plt = this->getVA() + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000203 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000204 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000205 }
206}
207
208template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000209 Sym->PltIndex = Entries.size();
Rui Ueyama724d6252016-01-29 01:49:32 +0000210 unsigned RelOff = Target->UseLazyBinding
George Rimar77b77792015-11-25 22:15:01 +0000211 ? Out<ELFT>::RelaPlt->getRelocOffset()
212 : Out<ELFT>::RelaDyn->getRelocOffset();
213 Entries.push_back(std::make_pair(Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000214}
215
216template <class ELFT>
217typename PltSection<ELFT>::uintX_t
218PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
Rui Ueyama62515452016-01-29 03:00:32 +0000219 return this->getVA() + Target->PltZeroSize +
Rui Ueyama724d6252016-01-29 01:49:32 +0000220 B.PltIndex * Target->PltEntrySize;
George Rimar648a2c32015-10-20 08:54:27 +0000221}
222
223template <class ELFT> void PltSection<ELFT>::finalize() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000224 this->Header.sh_size =
Rui Ueyama62515452016-01-29 03:00:32 +0000225 Target->PltZeroSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000226}
227
228template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000229RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000230 : OutputSectionBase<ELFT>(Name, IsRela ? SHT_RELA : SHT_REL, SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000231 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000232 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
233 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
234}
235
George Rimar5828c232015-11-30 17:49:19 +0000236// Applies corresponding symbol and type for dynamic tls relocation.
237// Returns true if relocation was handled.
238template <class ELFT>
239bool RelocationSection<ELFT>::applyTlsDynamicReloc(SymbolBody *Body,
240 uint32_t Type, Elf_Rel *P,
241 Elf_Rel *N) {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000242 if (Target->isTlsLocalDynamicRel(Type)) {
Rui Ueyama724d6252016-01-29 01:49:32 +0000243 P->setSymbolAndType(0, Target->TlsModuleIndexRel, Config->Mips64EL);
George Rimarb17f7392015-12-01 18:24:07 +0000244 P->r_offset = Out<ELFT>::Got->getLocalTlsIndexVA();
George Rimar5828c232015-11-30 17:49:19 +0000245 return true;
246 }
247
Rui Ueyamac516ae12016-01-29 02:33:45 +0000248 if (!Body || !Target->isTlsGlobalDynamicRel(Type))
George Rimar25411f252015-12-04 11:20:13 +0000249 return false;
250
Rui Ueyamabaf16512016-01-29 00:20:12 +0000251 if (Target->canRelaxTls(Type, Body)) {
Rui Ueyama572a6f72016-01-29 01:49:33 +0000252 P->setSymbolAndType(Body->DynsymIndex, Target->getTlsGotRel(),
Rui Ueyama724d6252016-01-29 01:49:32 +0000253 Config->Mips64EL);
George Rimar25411f252015-12-04 11:20:13 +0000254 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimar5828c232015-11-30 17:49:19 +0000255 return true;
256 }
George Rimar25411f252015-12-04 11:20:13 +0000257
Rui Ueyama572a6f72016-01-29 01:49:33 +0000258 P->setSymbolAndType(Body->DynsymIndex, Target->TlsModuleIndexRel,
Rui Ueyama724d6252016-01-29 01:49:32 +0000259 Config->Mips64EL);
George Rimar25411f252015-12-04 11:20:13 +0000260 P->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body);
Rui Ueyama572a6f72016-01-29 01:49:33 +0000261 N->setSymbolAndType(Body->DynsymIndex, Target->TlsOffsetRel,
Rui Ueyama724d6252016-01-29 01:49:32 +0000262 Config->Mips64EL);
George Rimar25411f252015-12-04 11:20:13 +0000263 N->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body) + sizeof(uintX_t);
264 return true;
George Rimar5828c232015-11-30 17:49:19 +0000265}
266
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000267template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000268 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000269 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000270 Buf += IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola50534c22015-09-22 17:49:38 +0000271
Michael J. Spencer627ae702015-11-13 00:28:34 +0000272 // Skip placeholder for global dynamic TLS relocation pair. It was already
273 // handled by the previous relocation.
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000274 if (!Rel.C)
Michael J. Spencer627ae702015-11-13 00:28:34 +0000275 continue;
276
277 InputSectionBase<ELFT> &C = *Rel.C;
278 const Elf_Rel &RI = *Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000279 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000280 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000281 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000282 if (Body)
283 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000284
Rui Ueyama64558522015-10-16 22:51:43 +0000285 uint32_t Type = RI.getType(Config->Mips64EL);
George Rimar5828c232015-11-30 17:49:19 +0000286 if (applyTlsDynamicReloc(Body, Type, P, reinterpret_cast<Elf_Rel *>(Buf)))
Michael J. Spencer1e225612015-11-11 01:00:24 +0000287 continue;
Rui Ueyamabef81f32016-01-21 20:59:22 +0000288
Rui Ueyama1546fb22016-01-26 01:03:21 +0000289 // Writer::scanRelocs creates a RELATIVE reloc for some type of TLS reloc.
290 // We want to write it down as is.
Rui Ueyama724d6252016-01-29 01:49:32 +0000291 if (Type == Target->RelativeRel) {
Rui Ueyama1546fb22016-01-26 01:03:21 +0000292 P->setSymbolAndType(0, Type, Config->Mips64EL);
293 P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA();
294 continue;
295 }
296
Rui Ueyamabef81f32016-01-21 20:59:22 +0000297 // Emit a copy relocation.
298 auto *SS = dyn_cast_or_null<SharedSymbol<ELFT>>(Body);
299 if (SS && SS->NeedsCopy) {
Rui Ueyama572a6f72016-01-29 01:49:33 +0000300 P->setSymbolAndType(Body->DynsymIndex, Target->CopyRel, Config->Mips64EL);
Rui Ueyamabef81f32016-01-21 20:59:22 +0000301 P->r_offset = Out<ELFT>::Bss->getVA() + SS->OffsetInBss;
302 continue;
303 }
304
Rui Ueyamac516ae12016-01-29 02:33:45 +0000305 bool NeedsGot = Body && Target->needsGot(Type, *Body);
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000306 bool CBP = canBePreempted(Body, NeedsGot);
Rui Ueyamab0210e832016-01-26 00:24:57 +0000307
308 // For a symbol with STT_GNU_IFUNC type, we always create a PLT and
309 // a GOT entry for the symbol, and emit an IRELATIVE reloc rather than
310 // the usual JUMP_SLOT reloc for the GOT entry. For the details, you
311 // want to read http://www.airs.com/blog/archives/403
312 if (!CBP && Body && isGnuIFunc<ELFT>(*Body)) {
Rui Ueyama724d6252016-01-29 01:49:32 +0000313 P->setSymbolAndType(0, Target->IRelativeRel, Config->Mips64EL);
Rui Ueyamab0210e832016-01-26 00:24:57 +0000314 if (Out<ELFT>::GotPlt)
315 P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
316 else
317 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
318 continue;
319 }
320
Rui Ueyama724d6252016-01-29 01:49:32 +0000321 bool LazyReloc =
Rui Ueyamac516ae12016-01-29 02:33:45 +0000322 Body && Target->UseLazyBinding && Target->needsPlt(Type, *Body);
Rafael Espindola49757522015-10-19 19:58:18 +0000323
George Rimarc7dc0be2015-12-15 08:48:39 +0000324 unsigned Reloc;
Rui Ueyama1546fb22016-01-26 01:03:21 +0000325 if (!CBP)
Rui Ueyama724d6252016-01-29 01:49:32 +0000326 Reloc = Target->RelativeRel;
George Rimarc7dc0be2015-12-15 08:48:39 +0000327 else if (LazyReloc)
Rui Ueyama724d6252016-01-29 01:49:32 +0000328 Reloc = Target->PltRel;
George Rimarc7dc0be2015-12-15 08:48:39 +0000329 else if (NeedsGot)
Rui Ueyama724d6252016-01-29 01:49:32 +0000330 Reloc = Body->isTls() ? Target->getTlsGotRel() : Target->GotRel;
George Rimarc7dc0be2015-12-15 08:48:39 +0000331 else
Rui Ueyamac516ae12016-01-29 02:33:45 +0000332 Reloc = Target->getDynRel(Type);
Rui Ueyama572a6f72016-01-29 01:49:33 +0000333 P->setSymbolAndType(CBP ? Body->DynsymIndex : 0, Reloc, Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000334
George Rimar4b5346f2015-12-29 16:17:32 +0000335 if (LazyReloc)
336 P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
337 else if (NeedsGot)
338 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimar4b5346f2015-12-29 16:17:32 +0000339 else
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000340 P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA();
Rafael Espindola49757522015-10-19 19:58:18 +0000341
Rui Ueyamad6cea142016-01-26 04:58:58 +0000342 if (!IsRela)
343 continue;
Rafael Espindola49757522015-10-19 19:58:18 +0000344
Rui Ueyamad6cea142016-01-26 04:58:58 +0000345 auto R = static_cast<const Elf_Rela &>(RI);
Rui Ueyama3ae28a42016-01-26 07:17:27 +0000346 auto S = static_cast<Elf_Rela *>(P);
347 uintX_t A = NeedsGot ? 0 : R.r_addend;
Rui Ueyama1546fb22016-01-26 01:03:21 +0000348 if (CBP)
Rui Ueyama3ae28a42016-01-26 07:17:27 +0000349 S->r_addend = A;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000350 else if (Body)
Rui Ueyama3ae28a42016-01-26 07:17:27 +0000351 S->r_addend = getSymVA<ELFT>(*Body) + A;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000352 else
Rui Ueyama3ae28a42016-01-26 07:17:27 +0000353 S->r_addend = getLocalRelTarget(File, R, A);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000354 }
355}
356
George Rimar77b77792015-11-25 22:15:01 +0000357template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
358 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
359 return EntrySize * Relocs.size();
360}
361
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000362template <class ELFT> void RelocationSection<ELFT>::finalize() {
George Rimara07ff662015-12-21 10:12:06 +0000363 this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex
364 : Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000365 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
366}
367
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000368template <class ELFT>
369InterpSection<ELFT>::InterpSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000370 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000371 this->Header.sh_size = Config->DynamicLinker.size() + 1;
372 this->Header.sh_addralign = 1;
373}
374
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000375template <class ELFT>
376void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
377 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000378}
379
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000380template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000381 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
382}
383
Rafael Espindola35c6af32015-09-25 17:19:10 +0000384template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000385HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000386 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000387 this->Header.sh_entsize = sizeof(Elf_Word);
388 this->Header.sh_addralign = sizeof(Elf_Word);
389}
390
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000391static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000392 uint32_t H = 0;
393 for (char C : Name) {
394 H = (H << 4) + C;
395 uint32_t G = H & 0xf0000000;
396 if (G)
397 H ^= G >> 24;
398 H &= ~G;
399 }
400 return H;
401}
402
Rui Ueyama0db335f2015-10-07 16:58:54 +0000403template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000404 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000405
Rui Ueyama0db335f2015-10-07 16:58:54 +0000406 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000407 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000408
409 // Create as many buckets as there are symbols.
410 // FIXME: This is simplistic. We can try to optimize it, but implementing
411 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000412 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000413 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
414}
415
416template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000417 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000418 auto *P = reinterpret_cast<Elf_Word *>(Buf);
419 *P++ = NumSymbols; // nbucket
420 *P++ = NumSymbols; // nchain
421
422 Elf_Word *Buckets = P;
423 Elf_Word *Chains = P + NumSymbols;
424
Rafael Espindolae2c24612016-01-29 01:24:25 +0000425 for (const std::pair<SymbolBody *, unsigned> &P :
426 Out<ELFT>::DynSymTab->getSymbols()) {
427 SymbolBody *Body = P.first;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000428 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000429 unsigned I = Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000430 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000431 Chains[I] = Buckets[Hash];
432 Buckets[Hash] = I;
433 }
434}
435
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000436static uint32_t hashGnu(StringRef Name) {
437 uint32_t H = 5381;
438 for (uint8_t C : Name)
439 H = (H << 5) + H + C;
440 return H;
441}
442
443template <class ELFT>
444GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000445 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000446 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
447 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
448}
449
450template <class ELFT>
451unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
452 if (!NumHashed)
453 return 0;
454
455 // These values are prime numbers which are not greater than 2^(N-1) + 1.
456 // In result, for any particular NumHashed we return a prime number
457 // which is not greater than NumHashed.
458 static const unsigned Primes[] = {
459 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
460 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
461
462 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
463 array_lengthof(Primes) - 1)];
464}
465
466// Bloom filter estimation: at least 8 bits for each hashed symbol.
467// GNU Hash table requirement: it should be a power of 2,
468// the minimum value is 1, even for an empty table.
469// Expected results for a 32-bit target:
470// calcMaskWords(0..4) = 1
471// calcMaskWords(5..8) = 2
472// calcMaskWords(9..16) = 4
473// For a 64-bit target:
474// calcMaskWords(0..8) = 1
475// calcMaskWords(9..16) = 2
476// calcMaskWords(17..32) = 4
477template <class ELFT>
478unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
479 if (!NumHashed)
480 return 1;
481 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
482}
483
484template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000485 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000486 NBuckets = calcNBuckets(NumHashed);
487 MaskWords = calcMaskWords(NumHashed);
488 // Second hash shift estimation: just predefined values.
489 Shift2 = ELFT::Is64Bits ? 6 : 5;
490
491 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
492 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
493 + sizeof(Elf_Off) * MaskWords // Bloom Filter
494 + sizeof(Elf_Word) * NBuckets // Hash Buckets
495 + sizeof(Elf_Word) * NumHashed; // Hash Values
496}
497
498template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
499 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000500 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000501 return;
502 writeBloomFilter(Buf);
503 writeHashTable(Buf);
504}
505
506template <class ELFT>
507void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
508 auto *P = reinterpret_cast<Elf_Word *>(Buf);
509 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000510 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000511 *P++ = MaskWords;
512 *P++ = Shift2;
513 Buf = reinterpret_cast<uint8_t *>(P);
514}
515
516template <class ELFT>
517void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
518 unsigned C = sizeof(Elf_Off) * 8;
519
520 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000521 for (const HashedSymbolData &Item : HashedSymbols) {
522 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
523 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
524 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000525 Masks[Pos] |= V;
526 }
527 Buf += sizeof(Elf_Off) * MaskWords;
528}
529
530template <class ELFT>
531void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
532 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
533 Elf_Word *Values = Buckets + NBuckets;
534
535 int PrevBucket = -1;
536 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000537 for (const HashedSymbolData &Item : HashedSymbols) {
538 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000539 assert(PrevBucket <= Bucket);
540 if (Bucket != PrevBucket) {
Rui Ueyama572a6f72016-01-29 01:49:33 +0000541 Buckets[Bucket] = Item.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000542 PrevBucket = Bucket;
543 if (I > 0)
544 Values[I - 1] |= 1;
545 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000546 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000547 ++I;
548 }
549 if (I > 0)
550 Values[I - 1] |= 1;
551}
552
Rafael Espindola31f88882015-11-02 14:33:11 +0000553static bool includeInGnuHashTable(SymbolBody *B) {
Rui Ueyamac112c1b2016-01-29 02:17:01 +0000554 // Assume that includeInDynsym() is already checked.
Rafael Espindola31f88882015-11-02 14:33:11 +0000555 return !B->isUndefined();
556}
557
Rafael Espindola35c6af32015-09-25 17:19:10 +0000558template <class ELFT>
Rafael Espindolae2c24612016-01-29 01:24:25 +0000559void GnuHashTableSection<ELFT>::addSymbols(
560 std::vector<std::pair<SymbolBody *, unsigned>> &Symbols) {
561 std::vector<std::pair<SymbolBody *, unsigned>> NotHashed;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000562 NotHashed.reserve(Symbols.size());
563 HashedSymbols.reserve(Symbols.size());
Rafael Espindolae2c24612016-01-29 01:24:25 +0000564 for (const std::pair<SymbolBody *, unsigned> &P : Symbols) {
565 SymbolBody *B = P.first;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000566 if (includeInGnuHashTable(B))
Rafael Espindolae2c24612016-01-29 01:24:25 +0000567 HashedSymbols.push_back(
568 HashedSymbolData{B, P.second, hashGnu(B->getName())});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000569 else
Rafael Espindolae2c24612016-01-29 01:24:25 +0000570 NotHashed.push_back(P);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000571 }
572 if (HashedSymbols.empty())
573 return;
574
575 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
576 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
577 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
578 return L.Hash % NBuckets < R.Hash % NBuckets;
579 });
580
581 Symbols = std::move(NotHashed);
582 for (const HashedSymbolData &Item : HashedSymbols)
Rafael Espindolae2c24612016-01-29 01:24:25 +0000583 Symbols.push_back(std::make_pair(Item.Body, Item.STName));
Igor Kudrinf1d60292015-10-28 07:05:56 +0000584}
585
586template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000587DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000588 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000589 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000590 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000591 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
592 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000593
594 // .dynamic section is not writable on MIPS.
595 // See "Special Section" in Chapter 4 in the following document:
596 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
597 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000598 Header.sh_flags = SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000599}
600
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000601template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000602 if (this->Header.sh_size)
603 return; // Already finalized.
604
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000605 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000606 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000607
Rafael Espindolae2c24612016-01-29 01:24:25 +0000608 auto Add = [=](Entry E) { Entries.push_back(E); };
609
610 // Add strings. We know that these are the last strings to be added to
Rafael Espindolade069362016-01-25 21:32:04 +0000611 // DynStrTab and doing this here allows this function to set DT_STRSZ.
612 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000613 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
614 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rafael Espindolade069362016-01-25 21:32:04 +0000615 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
616 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000617 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
618 if (!Config->SoName.empty())
619 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
620
Rafael Espindolade069362016-01-25 21:32:04 +0000621 Out<ELFT>::DynStrTab->finalize();
622
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000623
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000624 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindolade069362016-01-25 21:32:04 +0000625 bool IsRela = Out<ELFT>::RelaDyn->isRela();
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000626 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
627 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
628 Add({IsRela ? DT_RELAENT : DT_RELENT,
629 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000630 }
George Rimar648a2c32015-10-20 08:54:27 +0000631 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000632 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
633 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
634 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
635 Out<ELFT>::GotPlt});
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000636 Add({DT_PLTREL, uint64_t(Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000637 }
638
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000639 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
640 Add({DT_SYMENT, sizeof(Elf_Sym)});
641 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
642 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000643 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000644 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000645 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000646 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000647
Rafael Espindolade069362016-01-25 21:32:04 +0000648 if (PreInitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000649 Add({DT_PREINIT_ARRAY, PreInitArraySec});
650 Add({DT_PREINIT_ARRAYSZ, PreInitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000651 }
652 if (InitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000653 Add({DT_INIT_ARRAY, InitArraySec});
654 Add({DT_INIT_ARRAYSZ, (uintX_t)InitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000655 }
656 if (FiniArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000657 Add({DT_FINI_ARRAY, FiniArraySec});
658 Add({DT_FINI_ARRAYSZ, (uintX_t)FiniArraySec->getSize()});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000659 }
660
Rui Ueyama304d1352016-01-25 21:47:25 +0000661 if (SymbolBody *B = SymTab.find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000662 Add({DT_INIT, B});
Rui Ueyama304d1352016-01-25 21:47:25 +0000663 if (SymbolBody *B = SymTab.find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000664 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000665
Rafael Espindolade069362016-01-25 21:32:04 +0000666 uint32_t DtFlags = 0;
667 uint32_t DtFlags1 = 0;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000668 if (Config->Bsymbolic)
669 DtFlags |= DF_SYMBOLIC;
670 if (Config->ZNodelete)
671 DtFlags1 |= DF_1_NODELETE;
672 if (Config->ZNow) {
673 DtFlags |= DF_BIND_NOW;
674 DtFlags1 |= DF_1_NOW;
675 }
676 if (Config->ZOrigin) {
677 DtFlags |= DF_ORIGIN;
678 DtFlags1 |= DF_1_ORIGIN;
679 }
680
681 if (DtFlags)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000682 Add({DT_FLAGS, DtFlags});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000683 if (DtFlags1)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000684 Add({DT_FLAGS_1, DtFlags1});
Igor Kudrin304860a2015-11-12 04:39:49 +0000685
Ed Mastef5d3cf62016-01-06 15:52:27 +0000686 if (!Config->Entry.empty())
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000687 Add({DT_DEBUG, (uint64_t)0});
Ed Mastef5d3cf62016-01-06 15:52:27 +0000688
Igor Kudrin304860a2015-11-12 04:39:49 +0000689 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000690 Add({DT_MIPS_RLD_VERSION, 1});
691 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
692 Add({DT_MIPS_BASE_ADDRESS, (uintX_t)Target->getVAStart()});
693 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
694 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000695 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000696 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000697 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000698 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
699 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000700 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000701 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000702 }
703
Rafael Espindolade069362016-01-25 21:32:04 +0000704 // +1 for DT_NULL
705 Header.sh_size = (Entries.size() + 1) * Header.sh_entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000706}
707
708template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000709 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
710
Rafael Espindolade069362016-01-25 21:32:04 +0000711 for (const Entry &E : Entries) {
712 P->d_tag = E.Tag;
713 switch (E.Kind) {
714 case Entry::SecAddr:
715 P->d_un.d_ptr = E.OutSec->getVA();
716 break;
717 case Entry::SymAddr:
718 P->d_un.d_ptr = getSymVA<ELFT>(*E.Sym);
719 break;
720 case Entry::PlainInt:
721 P->d_un.d_val = E.Val;
722 break;
723 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000724 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000725 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000726}
727
728template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000729EhFrameHeader<ELFT>::EhFrameHeader()
730 : OutputSectionBase<ELFT>(".eh_frame_hdr", llvm::ELF::SHT_PROGBITS,
731 SHF_ALLOC) {
732 // It's a 4 bytes of header + pointer to the contents of the .eh_frame section
733 // + the number of FDE pointers in the table.
734 this->Header.sh_size = 12;
735}
736
737// We have to get PC values of FDEs. They depend on relocations
738// which are target specific, so we run this code after performing
739// all relocations. We read the values from ouput buffer according to the
740// encoding given for FDEs. Return value is an offset to the initial PC value
741// for the FDE.
742template <class ELFT>
743typename EhFrameHeader<ELFT>::uintX_t
744EhFrameHeader<ELFT>::getFdePc(uintX_t EhVA, const FdeData &F) {
745 const endianness E = ELFT::TargetEndianness;
746 assert((F.Enc & 0xF0) != dwarf::DW_EH_PE_datarel);
747
748 uintX_t FdeOff = EhVA + F.Off + 8;
749 switch (F.Enc & 0xF) {
750 case dwarf::DW_EH_PE_udata2:
751 case dwarf::DW_EH_PE_sdata2:
752 return FdeOff + read16<E>(F.PCRel);
753 case dwarf::DW_EH_PE_udata4:
754 case dwarf::DW_EH_PE_sdata4:
755 return FdeOff + read32<E>(F.PCRel);
756 case dwarf::DW_EH_PE_udata8:
757 case dwarf::DW_EH_PE_sdata8:
758 return FdeOff + read64<E>(F.PCRel);
759 case dwarf::DW_EH_PE_absptr:
760 if (sizeof(uintX_t) == 8)
761 return FdeOff + read64<E>(F.PCRel);
762 return FdeOff + read32<E>(F.PCRel);
763 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000764 fatal("unknown FDE size encoding");
George Rimarf6bc65a2016-01-15 13:34:52 +0000765}
766
767template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
768 const endianness E = ELFT::TargetEndianness;
769
770 const uint8_t Header[] = {1, dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4,
771 dwarf::DW_EH_PE_udata4,
772 dwarf::DW_EH_PE_datarel | dwarf::DW_EH_PE_sdata4};
773 memcpy(Buf, Header, sizeof(Header));
774
775 uintX_t EhVA = Sec->getVA();
776 uintX_t VA = this->getVA();
777 uintX_t EhOff = EhVA - VA - 4;
778 write32<E>(Buf + 4, EhOff);
779 write32<E>(Buf + 8, this->FdeList.size());
780 Buf += 12;
781
782 // InitialPC -> Offset in .eh_frame, sorted by InitialPC.
783 std::map<uintX_t, size_t> PcToOffset;
784 for (const FdeData &F : FdeList)
785 PcToOffset[getFdePc(EhVA, F)] = F.Off;
786
787 for (auto &I : PcToOffset) {
788 // The first four bytes are an offset to the initial PC value for the FDE.
789 write32<E>(Buf, I.first - VA);
790 // The last four bytes are an offset to the FDE data itself.
791 write32<E>(Buf + 4, EhVA + I.second - VA);
792 Buf += 8;
793 }
794}
795
796template <class ELFT>
797void EhFrameHeader<ELFT>::assignEhFrame(EHOutputSection<ELFT> *Sec) {
George Rimar45ca88d2016-01-25 19:27:50 +0000798 assert((!this->Sec || this->Sec == Sec) &&
799 "multiple .eh_frame sections not supported for .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000800 Live = Config->EhFrameHdr;
801 this->Sec = Sec;
802}
803
804template <class ELFT>
805void EhFrameHeader<ELFT>::addFde(uint8_t Enc, size_t Off, uint8_t *PCRel) {
806 if (Live && (Enc & 0xF0) == dwarf::DW_EH_PE_datarel)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000807 fatal("DW_EH_PE_datarel encoding unsupported for FDEs by .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000808 FdeList.push_back(FdeData{Enc, Off, PCRel});
809}
810
811template <class ELFT> void EhFrameHeader<ELFT>::reserveFde() {
812 // Each FDE entry is 8 bytes long:
813 // The first four bytes are an offset to the initial PC value for the FDE. The
814 // last four byte are an offset to the FDE data itself.
815 this->Header.sh_size += 8;
816}
817
818template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000819OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type,
820 uintX_t Flags)
821 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000822
823template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000824void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
825 auto *S = cast<InputSection<ELFT>>(C);
826 Sections.push_back(S);
827 S->OutSec = this;
828 uint32_t Align = S->getAlign();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000829 if (Align > this->Header.sh_addralign)
830 this->Header.sh_addralign = Align;
831
832 uintX_t Off = this->Header.sh_size;
Rui Ueyama489a8062016-01-14 20:53:50 +0000833 Off = alignTo(Off, Align);
Rui Ueyama40845e62015-12-26 05:51:07 +0000834 S->OutSecOff = Off;
835 Off += S->getSize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000836 this->Header.sh_size = Off;
837}
838
839template <class ELFT>
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000840typename ELFFile<ELFT>::uintX_t elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000841 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000842 case SymbolBody::DefinedSyntheticKind: {
843 auto &D = cast<DefinedSynthetic<ELFT>>(S);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000844 return D.Section.getVA() + D.Value;
Rafael Espindola8614c562015-10-06 14:33:58 +0000845 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000846 case SymbolBody::DefinedRegularKind: {
847 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000848 InputSectionBase<ELFT> *SC = DR.Section;
849 if (!SC)
850 return DR.Sym.st_value;
Tom Stellard80efb162016-01-07 03:59:08 +0000851
852 // Symbol offsets for AMDGPU need to be the offset in bytes of the symbol
853 // from the beginning of the section.
854 if (Config->EMachine == EM_AMDGPU)
855 return SC->getOffset(DR.Sym);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000856 if (DR.Sym.getType() == STT_TLS)
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000857 return SC->OutSec->getVA() + SC->getOffset(DR.Sym) -
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000858 Out<ELFT>::TlsPhdr->p_vaddr;
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000859 return SC->OutSec->getVA() + SC->getOffset(DR.Sym);
Rafael Espindolacd076f02015-09-25 18:19:03 +0000860 }
861 case SymbolBody::DefinedCommonKind:
Rui Ueyamae57c4872016-01-05 16:35:43 +0000862 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(S).OffsetInBss;
George Rimarbc590fe2015-10-28 16:48:58 +0000863 case SymbolBody::SharedKind: {
864 auto &SS = cast<SharedSymbol<ELFT>>(S);
Rui Ueyamabb936062015-12-17 01:14:23 +0000865 if (SS.NeedsCopy)
Rui Ueyamae57c4872016-01-05 16:35:43 +0000866 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
George Rimarbc590fe2015-10-28 16:48:58 +0000867 return 0;
868 }
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000869 case SymbolBody::UndefinedElfKind:
Rafael Espindolacd076f02015-09-25 18:19:03 +0000870 case SymbolBody::UndefinedKind:
871 return 0;
872 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000873 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
874 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000875 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000876 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000877}
878
Rui Ueyama34f29242015-10-13 19:51:57 +0000879// Returns a VA which a relocatin RI refers to. Used only for local symbols.
880// For non-local symbols, use getSymVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000881template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000882typename ELFFile<ELFT>::uintX_t
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000883elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
884 const Elf_Rel_Impl<ELFT, IsRela> &RI,
885 typename ELFFile<ELFT>::uintX_t Addend) {
Rafael Espindola932efcf2015-10-19 20:24:44 +0000886 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
887 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
888
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000889 // PPC64 has a special relocation representing the TOC base pointer
890 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000891 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000892 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000893
Rui Ueyama126d08f2015-10-12 20:28:22 +0000894 const Elf_Sym *Sym =
895 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000896
Rui Ueyama126d08f2015-10-12 20:28:22 +0000897 if (!Sym)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000898 fatal("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000899
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000900 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
901
902 if (Sym->getType() == STT_TLS)
903 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
George Rimarcc06a6f2015-11-29 14:14:20 +0000904 Out<ELFT>::TlsPhdr->p_vaddr;
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000905
Rafael Espindola444576d2015-10-09 19:25:07 +0000906 // According to the ELF spec reference to a local symbol from outside
907 // the group are not allowed. Unfortunately .eh_frame breaks that rule
908 // and must be treated specially. For now we just replace the symbol with
909 // 0.
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000910 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000911 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000912
Rafael Espindolac159c962015-10-19 21:00:02 +0000913 uintX_t VA = Section->OutSec->getVA();
914 if (isa<InputSection<ELFT>>(Section))
915 return VA + Section->getOffset(*Sym) + Addend;
916
917 uintX_t Offset = Sym->st_value;
918 if (Sym->getType() == STT_SECTION) {
919 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000920 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000921 }
Rafael Espindola7f040bf2015-12-25 01:00:41 +0000922 return VA + Section->getOffset(Offset) + Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000923}
924
Rui Ueyama34f29242015-10-13 19:51:57 +0000925// Returns true if a symbol can be replaced at load-time by a symbol
926// with the same name defined in other ELF executable or DSO.
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000927bool elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000928 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000929 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000930 if (Body->isShared())
931 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000932
933 if (Body->isUndefined()) {
934 if (!Body->isWeak())
935 return true;
936
937 // This is an horrible corner case. Ideally we would like to say that any
938 // undefined symbol can be preempted so that the dynamic linker has a
939 // chance of finding it at runtime.
940 //
941 // The problem is that the code sequence used to test for weak undef
942 // functions looks like
943 // if (func) func()
944 // If the code is -fPIC the first reference is a load from the got and
945 // everything works.
946 // If the code is not -fPIC there is no reasonable way to solve it:
947 // * A relocation writing to the text segment will fail (it is ro).
948 // * A copy relocation doesn't work for functions.
949 // * The trick of using a plt entry as the address would fail here since
950 // the plt entry would have a non zero address.
951 // Since we cannot do anything better, we just resolve the symbol to 0 and
952 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000953 //
954 // As an extra hack, assume that if we are producing a shared library the
955 // user knows what he or she is doing and can handle a dynamic relocation.
956 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000957 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000958 if (!Config->Shared)
959 return false;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +0000960 return Body->getVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000961}
962
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000963template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000964 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000965 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000966}
967
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000968template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000969EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t Type,
970 uintX_t Flags)
George Rimarf6bc65a2016-01-15 13:34:52 +0000971 : OutputSectionBase<ELFT>(Name, Type, Flags) {
972 Out<ELFT>::EhFrameHdr->assignEhFrame(this);
973}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000974
975template <class ELFT>
976EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
977 : S(S), Index(Index) {}
978
979template <class ELFT> StringRef EHRegion<ELFT>::data() const {
980 ArrayRef<uint8_t> SecData = S->getSectionData();
981 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
982 size_t Start = Offsets[Index].first;
983 size_t End =
984 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
985 return StringRef((const char *)SecData.data() + Start, End - Start);
986}
987
988template <class ELFT>
989Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
990 : EHRegion<ELFT>(S, Index) {}
991
George Rimarf6bc65a2016-01-15 13:34:52 +0000992// Read a byte and advance D by one byte.
993static uint8_t readByte(ArrayRef<uint8_t> &D) {
994 if (D.empty())
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000995 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000996 uint8_t B = D.front();
997 D = D.slice(1);
998 return B;
999}
1000
1001static void skipLeb128(ArrayRef<uint8_t> &D) {
1002 while (!D.empty()) {
1003 uint8_t Val = D.front();
1004 D = D.slice(1);
1005 if ((Val & 0x80) == 0)
1006 return;
1007 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001008 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +00001009}
1010
1011template <class ELFT> static unsigned getSizeForEncoding(unsigned Enc) {
1012 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1013 switch (Enc & 0x0f) {
1014 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001015 fatal("unknown FDE encoding");
George Rimarf6bc65a2016-01-15 13:34:52 +00001016 case dwarf::DW_EH_PE_absptr:
1017 case dwarf::DW_EH_PE_signed:
1018 return sizeof(uintX_t);
1019 case dwarf::DW_EH_PE_udata2:
1020 case dwarf::DW_EH_PE_sdata2:
1021 return 2;
1022 case dwarf::DW_EH_PE_udata4:
1023 case dwarf::DW_EH_PE_sdata4:
1024 return 4;
1025 case dwarf::DW_EH_PE_udata8:
1026 case dwarf::DW_EH_PE_sdata8:
1027 return 8;
1028 }
1029}
1030
1031template <class ELFT>
1032uint8_t EHOutputSection<ELFT>::getFdeEncoding(ArrayRef<uint8_t> D) {
1033 auto Check = [](bool C) {
1034 if (!C)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001035 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +00001036 };
1037
1038 Check(D.size() >= 8);
1039 D = D.slice(8);
1040
1041 uint8_t Version = readByte(D);
1042 if (Version != 1 && Version != 3)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001043 fatal("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
George Rimarf6bc65a2016-01-15 13:34:52 +00001044
1045 auto AugEnd = std::find(D.begin() + 1, D.end(), '\0');
1046 Check(AugEnd != D.end());
1047 ArrayRef<uint8_t> AugString(D.begin(), AugEnd - D.begin());
1048 D = D.slice(AugString.size() + 1);
1049
1050 // Code alignment factor should always be 1 for .eh_frame.
1051 if (readByte(D) != 1)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001052 fatal("CIE code alignment must be 1");
George Rimarf6bc65a2016-01-15 13:34:52 +00001053 // Skip data alignment factor
1054 skipLeb128(D);
1055
1056 // Skip the return address register. In CIE version 1 this is a single
1057 // byte. In CIE version 3 this is an unsigned LEB128.
1058 if (Version == 1)
1059 readByte(D);
1060 else
1061 skipLeb128(D);
1062
1063 while (!AugString.empty()) {
1064 switch (readByte(AugString)) {
1065 case 'z':
1066 skipLeb128(D);
1067 break;
1068 case 'R':
1069 return readByte(D);
1070 case 'P': {
1071 uint8_t Enc = readByte(D);
1072 if ((Enc & 0xf0) == dwarf::DW_EH_PE_aligned)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001073 fatal("DW_EH_PE_aligned encoding for address of a personality routine "
George Rimarf6bc65a2016-01-15 13:34:52 +00001074 "handler not supported");
1075 unsigned EncSize = getSizeForEncoding<ELFT>(Enc);
1076 Check(D.size() >= EncSize);
1077 D = D.slice(EncSize);
1078 break;
1079 }
1080 case 'S':
1081 case 'L':
1082 // L: Language Specific Data Area (LSDA) encoding
1083 // S: This CIE represents a stack frame for the invocation of a signal
1084 // handler
1085 break;
1086 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001087 fatal("unknown .eh_frame augmentation string value");
George Rimarf6bc65a2016-01-15 13:34:52 +00001088 }
1089 }
1090 return dwarf::DW_EH_PE_absptr;
1091}
1092
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001093template <class ELFT>
1094template <bool IsRela>
1095void EHOutputSection<ELFT>::addSectionAux(
1096 EHInputSection<ELFT> *S,
1097 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
1098 const endianness E = ELFT::TargetEndianness;
1099
1100 S->OutSec = this;
1101 uint32_t Align = S->getAlign();
1102 if (Align > this->Header.sh_addralign)
1103 this->Header.sh_addralign = Align;
1104
1105 Sections.push_back(S);
1106
1107 ArrayRef<uint8_t> SecData = S->getSectionData();
1108 ArrayRef<uint8_t> D = SecData;
1109 uintX_t Offset = 0;
1110 auto RelI = Rels.begin();
1111 auto RelE = Rels.end();
1112
George Rimar147747a2016-01-02 16:55:01 +00001113 DenseMap<unsigned, unsigned> OffsetToIndex;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001114 while (!D.empty()) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001115 unsigned Index = S->Offsets.size();
1116 S->Offsets.push_back(std::make_pair(Offset, -1));
1117
George Rimar003be4f2015-12-17 09:23:40 +00001118 uintX_t Length = readEntryLength(D);
George Rimarf6bc65a2016-01-15 13:34:52 +00001119 // If CIE/FDE data length is zero then Length is 4, this
1120 // shall be considered a terminator and processing shall end.
1121 if (Length == 4)
1122 break;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001123 StringRef Entry((const char *)D.data(), Length);
1124
1125 while (RelI != RelE && RelI->r_offset < Offset)
1126 ++RelI;
1127 uintX_t NextOffset = Offset + Length;
1128 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
1129
1130 uint32_t ID = read32<E>(D.data() + 4);
1131 if (ID == 0) {
1132 // CIE
1133 Cie<ELFT> C(S, Index);
George Rimarf6bc65a2016-01-15 13:34:52 +00001134 if (Config->EhFrameHdr)
1135 C.FdeEncoding = getFdeEncoding(D);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001136
1137 StringRef Personality;
1138 if (HasReloc) {
1139 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
1140 SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl();
1141 Personality = Body.getName();
1142 }
1143
1144 std::pair<StringRef, StringRef> CieInfo(Entry, Personality);
1145 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
George Rimar147747a2016-01-02 16:55:01 +00001146 if (P.second) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001147 Cies.push_back(C);
Rui Ueyama489a8062016-01-14 20:53:50 +00001148 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
George Rimar147747a2016-01-02 16:55:01 +00001149 }
1150 OffsetToIndex[Offset] = P.first->second;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001151 } else {
1152 if (!HasReloc)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001153 fatal("FDE doesn't reference another section");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001154 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
1155 if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) {
1156 uint32_t CieOffset = Offset + 4 - ID;
George Rimar147747a2016-01-02 16:55:01 +00001157 auto I = OffsetToIndex.find(CieOffset);
1158 if (I == OffsetToIndex.end())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001159 fatal("Invalid CIE reference");
George Rimar147747a2016-01-02 16:55:01 +00001160 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
George Rimarf6bc65a2016-01-15 13:34:52 +00001161 Out<ELFT>::EhFrameHdr->reserveFde();
Rui Ueyama489a8062016-01-14 20:53:50 +00001162 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001163 }
1164 }
1165
1166 Offset = NextOffset;
1167 D = D.slice(Length);
1168 }
1169}
1170
1171template <class ELFT>
George Rimar003be4f2015-12-17 09:23:40 +00001172typename EHOutputSection<ELFT>::uintX_t
1173EHOutputSection<ELFT>::readEntryLength(ArrayRef<uint8_t> D) {
1174 const endianness E = ELFT::TargetEndianness;
1175
1176 if (D.size() < 4)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001177 fatal("Truncated CIE/FDE length");
George Rimar003be4f2015-12-17 09:23:40 +00001178 uint64_t Len = read32<E>(D.data());
1179 if (Len < UINT32_MAX) {
1180 if (Len > (UINT32_MAX - 4))
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001181 fatal("CIE/FIE size is too large");
George Rimar003be4f2015-12-17 09:23:40 +00001182 if (Len + 4 > D.size())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001183 fatal("CIE/FIE ends past the end of the section");
George Rimar003be4f2015-12-17 09:23:40 +00001184 return Len + 4;
1185 }
1186
1187 if (D.size() < 12)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001188 fatal("Truncated CIE/FDE length");
George Rimar003be4f2015-12-17 09:23:40 +00001189 Len = read64<E>(D.data() + 4);
1190 if (Len > (UINT64_MAX - 12))
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001191 fatal("CIE/FIE size is too large");
George Rimar003be4f2015-12-17 09:23:40 +00001192 if (Len + 12 > D.size())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001193 fatal("CIE/FIE ends past the end of the section");
George Rimar003be4f2015-12-17 09:23:40 +00001194 return Len + 12;
1195}
1196
1197template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001198void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1199 auto *S = cast<EHInputSection<ELFT>>(C);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001200 const Elf_Shdr *RelSec = S->RelocSection;
Rui Ueyama0de86c12016-01-27 22:23:44 +00001201 if (!RelSec) {
1202 addSectionAux(S, make_range<const Elf_Rela *>(nullptr, nullptr));
1203 return;
1204 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001205 ELFFile<ELFT> &Obj = S->getFile()->getObj();
1206 if (RelSec->sh_type == SHT_RELA)
Rui Ueyama0de86c12016-01-27 22:23:44 +00001207 addSectionAux(S, Obj.relas(RelSec));
1208 else
1209 addSectionAux(S, Obj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001210}
1211
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001212template <class ELFT>
1213static typename ELFFile<ELFT>::uintX_t writeAlignedCieOrFde(StringRef Data,
1214 uint8_t *Buf) {
1215 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1216 const endianness E = ELFT::TargetEndianness;
Rui Ueyama489a8062016-01-14 20:53:50 +00001217 uint64_t Len = alignTo(Data.size(), sizeof(uintX_t));
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001218 write32<E>(Buf, Len - 4);
1219 memcpy(Buf + 4, Data.data() + 4, Data.size() - 4);
1220 return Len;
1221}
1222
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001223template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1224 const endianness E = ELFT::TargetEndianness;
1225 size_t Offset = 0;
1226 for (const Cie<ELFT> &C : Cies) {
1227 size_t CieOffset = Offset;
1228
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001229 uintX_t CIELen = writeAlignedCieOrFde<ELFT>(C.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001230 C.S->Offsets[C.Index].second = Offset;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001231 Offset += CIELen;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001232
1233 for (const EHRegion<ELFT> &F : C.Fdes) {
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001234 uintX_t Len = writeAlignedCieOrFde<ELFT>(F.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001235 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001236 F.S->Offsets[F.Index].second = Offset;
George Rimarf6bc65a2016-01-15 13:34:52 +00001237 Out<ELFT>::EhFrameHdr->addFde(C.FdeEncoding, Offset, Buf + Offset + 8);
George Rimare72beba2015-12-21 09:38:59 +00001238 Offset += Len;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001239 }
1240 }
1241
1242 for (EHInputSection<ELFT> *S : Sections) {
1243 const Elf_Shdr *RelSec = S->RelocSection;
1244 if (!RelSec)
1245 continue;
1246 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1247 if (RelSec->sh_type == SHT_RELA)
1248 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1249 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001250 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001251 }
1252}
1253
1254template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001255MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
1256 uintX_t Flags)
1257 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001258
1259template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001260 if (shouldTailMerge()) {
1261 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001262 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001263 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001264 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001265 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1266 StringRef Data = P.first;
1267 memcpy(Buf + P.second, Data.data(), Data.size());
1268 }
1269}
1270
1271static size_t findNull(StringRef S, size_t EntSize) {
1272 // Optimize the common case.
1273 if (EntSize == 1)
1274 return S.find(0);
1275
1276 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1277 const char *B = S.begin() + I;
1278 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1279 return I;
1280 }
1281 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001282}
1283
1284template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001285void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1286 auto *S = cast<MergeInputSection<ELFT>>(C);
Rafael Espindolac159c962015-10-19 21:00:02 +00001287 S->OutSec = this;
1288 uint32_t Align = S->getAlign();
1289 if (Align > this->Header.sh_addralign)
1290 this->Header.sh_addralign = Align;
1291
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001292 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001293 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001294 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001295
1296 if (this->Header.sh_flags & SHF_STRINGS) {
Rui Ueyamaad87ff72016-01-06 02:52:24 +00001297 uintX_t Offset = 0;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001298 while (!Data.empty()) {
1299 size_t End = findNull(Data, EntSize);
1300 if (End == StringRef::npos)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001301 fatal("String is not null terminated");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001302 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001303 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001304 if (shouldTailMerge())
1305 OutputOffset = -1;
1306 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1307 uintX_t Size = End + EntSize;
1308 Data = Data.substr(Size);
1309 Offset += Size;
1310 }
1311 } else {
1312 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1313 StringRef Entry = Data.substr(I, EntSize);
1314 size_t OutputOffset = Builder.add(Entry);
Rui Ueyamaad87ff72016-01-06 02:52:24 +00001315 S->Offsets.push_back(std::make_pair(I, OutputOffset));
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001316 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001317 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001318}
1319
1320template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001321unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1322 return Builder.getOffset(Val);
1323}
1324
1325template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1326 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1327}
1328
1329template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1330 if (shouldTailMerge())
1331 Builder.finalize();
1332 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001333}
1334
1335template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001336StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001337 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1338 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001339 Dynamic(Dynamic) {
1340 this->Header.sh_addralign = 1;
1341}
1342
Rafael Espindolae2c24612016-01-29 01:24:25 +00001343// Adds a string to the string table. If HashIt is true we hash and check for
1344// duplicates. It is optional because the name of global symbols are already
1345// uniqued and hashing them again has a big cost for a small value: uniquing
1346// them with some other string that happens to be the same.
1347template <class ELFT>
1348unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1349 if (HashIt) {
1350 auto R = StringMap.insert(std::make_pair(S, Size));
1351 if (!R.second)
1352 return R.first->second;
1353 }
1354 unsigned Ret = Size;
1355 Size += S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001356 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001357 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001358}
1359
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001360template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001361 // ELF string tables start with NUL byte, so advance the pointer by one.
1362 ++Buf;
1363 for (StringRef S : Strings) {
1364 memcpy(Buf, S.data(), S.size());
1365 Buf += S.size() + 1;
1366 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001367}
1368
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001369template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001370SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001371 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001372 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1373 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001374 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001375 StrTabSec(StrTabSec), Table(Table) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001376 this->Header.sh_entsize = sizeof(Elf_Sym);
1377 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
Rafael Espindola35c6af32015-09-25 17:19:10 +00001378}
1379
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001380// Orders symbols according to their positions in the GOT,
1381// in compliance with MIPS ABI rules.
1382// See "Global Offset Table" in Chapter 5 in the following document
1383// for detailed description:
1384// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rafael Espindolae2c24612016-01-29 01:24:25 +00001385static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
1386 const std::pair<SymbolBody *, unsigned> &R) {
1387 if (!L.first->isInGot() || !R.first->isInGot())
1388 return R.first->isInGot();
1389 return L.first->GotIndex < R.first->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001390}
1391
Rui Ueyama0db335f2015-10-07 16:58:54 +00001392template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001393 if (this->Header.sh_size)
1394 return; // Already finalized.
1395
Rui Ueyama0db335f2015-10-07 16:58:54 +00001396 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001397 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001398 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001399
1400 if (!StrTabSec.isDynamic()) {
1401 std::stable_sort(Symbols.begin(), Symbols.end(),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001402 [](const std::pair<SymbolBody *, unsigned> &L,
1403 const std::pair<SymbolBody *, unsigned> &R) {
1404 return getSymbolBinding(L.first) == STB_LOCAL &&
1405 getSymbolBinding(R.first) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001406 });
1407 return;
1408 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001409 if (Out<ELFT>::GnuHashTab)
1410 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1411 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001412 else if (Config->EMachine == EM_MIPS)
1413 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001414 size_t I = 0;
Rafael Espindolae2c24612016-01-29 01:24:25 +00001415 for (const std::pair<SymbolBody *, unsigned> &P : Symbols)
Rui Ueyama572a6f72016-01-29 01:49:33 +00001416 P.first->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001417}
1418
1419template <class ELFT>
1420void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001421 Symbols.push_back(
1422 std::make_pair(Body, StrTabSec.addString(Body->getName(), false)));
Rui Ueyama0db335f2015-10-07 16:58:54 +00001423}
1424
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001425template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001426 Buf += sizeof(Elf_Sym);
1427
1428 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1429 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001430 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1431 writeLocalSymbols(Buf);
1432
1433 writeGlobalSymbols(Buf);
1434}
1435
1436template <class ELFT>
1437void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1438 // Iterate over all input object files to copy their local symbols
1439 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001440 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001441 for (const std::pair<const Elf_Sym *, unsigned> &P : File->KeptLocalSyms) {
1442 const Elf_Sym *Sym = P.first;
Rafael Espindola444576d2015-10-09 19:25:07 +00001443
Rui Ueyamac55733e2015-09-30 00:54:29 +00001444 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001445 uintX_t VA = 0;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001446 if (Sym->st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001447 ESym->st_shndx = SHN_ABS;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001448 VA = Sym->st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001449 } else {
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001450 InputSectionBase<ELFT> *Section = File->getSection(*Sym);
Rafael Espindolac159c962015-10-19 21:00:02 +00001451 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1452 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001453 VA = Section->getOffset(*Sym);
Tom Stellard80efb162016-01-07 03:59:08 +00001454 // Symbol offsets for AMDGPU need to be the offset in bytes of the
1455 // symbol from the beginning of the section.
1456 if (Config->EMachine != EM_AMDGPU)
1457 VA += OutSec->getVA();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001458 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001459 ESym->st_name = P.second;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001460 ESym->st_size = Sym->st_size;
1461 ESym->setBindingAndType(Sym->getBinding(), Sym->getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001462 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001463 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001464 }
1465 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001466}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001467
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001468template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001469static const typename llvm::object::ELFFile<ELFT>::Elf_Sym *
1470getElfSym(SymbolBody &Body) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001471 if (auto *EBody = dyn_cast<DefinedElf<ELFT>>(&Body))
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001472 return &EBody->Sym;
1473 if (auto *EBody = dyn_cast<UndefinedElf<ELFT>>(&Body))
1474 return &EBody->Sym;
1475 return nullptr;
1476}
1477
1478template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001479void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001480 // Write the internal symbol table contents to the output symbol table
1481 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001482 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001483 for (const std::pair<SymbolBody *, unsigned> &P : Symbols) {
1484 SymbolBody *Body = P.first;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001485 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001486
Rafael Espindola8614c562015-10-06 14:33:58 +00001487 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001488 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001489 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001490 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001491 case SymbolBody::DefinedRegularKind: {
1492 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001493 if (InputSectionBase<ELFT> *Sec = Sym->Section) {
1494 if (!Sec->isLive())
1495 continue;
1496 OutSec = Sec->OutSec;
1497 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001498 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001499 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001500 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001501 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001502 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001503 case SymbolBody::SharedKind: {
Rui Ueyamabb936062015-12-17 01:14:23 +00001504 if (cast<SharedSymbol<ELFT>>(Body)->NeedsCopy)
George Rimarbc590fe2015-10-28 16:48:58 +00001505 OutSec = Out<ELFT>::Bss;
1506 break;
1507 }
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001508 case SymbolBody::UndefinedElfKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001509 case SymbolBody::UndefinedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001510 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001511 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001512 }
1513
Rafael Espindolae2c24612016-01-29 01:24:25 +00001514 ESym->st_name = P.second;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001515
Rafael Espindola8614c562015-10-06 14:33:58 +00001516 unsigned char Type = STT_NOTYPE;
1517 uintX_t Size = 0;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001518 if (const Elf_Sym *InputSym = getElfSym<ELFT>(*Body)) {
1519 Type = InputSym->getType();
1520 Size = InputSym->st_size;
Rafael Espindola11191912015-12-24 16:23:37 +00001521 } else if (auto *C = dyn_cast<DefinedCommon>(Body)) {
1522 Type = STT_OBJECT;
1523 Size = C->Size;
Rafael Espindola8614c562015-10-06 14:33:58 +00001524 }
1525
Igor Kudrin853b88d2015-10-20 20:52:14 +00001526 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001527 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001528 ESym->setVisibility(Body->getVisibility());
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001529 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001530
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001531 if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001532 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001533 else if (isa<DefinedRegular<ELFT>>(Body))
1534 ESym->st_shndx = SHN_ABS;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001535
1536 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001537 }
1538}
1539
Igor Kudrin853b88d2015-10-20 20:52:14 +00001540template <class ELFT>
1541uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001542 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001543 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1544 return STB_LOCAL;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001545 if (const Elf_Sym *ESym = getElfSym<ELFT>(*Body))
1546 return ESym->getBinding();
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001547 if (isa<DefinedSynthetic<ELFT>>(Body))
1548 return STB_LOCAL;
Igor Kudrin853b88d2015-10-20 20:52:14 +00001549 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1550}
1551
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001552template <class ELFT>
1553MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1554 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1555 this->Header.sh_addralign = 4;
1556 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1557 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1558}
1559
1560template <class ELFT>
1561void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1562 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
1563 R->ri_gp_value = getMipsGpAddr<ELFT>();
Rui Ueyama70eed362016-01-06 22:42:43 +00001564 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001565}
1566
1567template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001568void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001569 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001570 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001571 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001572}
1573
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001574namespace lld {
1575namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001576template class OutputSectionBase<ELF32LE>;
1577template class OutputSectionBase<ELF32BE>;
1578template class OutputSectionBase<ELF64LE>;
1579template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001580
George Rimarf6bc65a2016-01-15 13:34:52 +00001581template class EhFrameHeader<ELF32LE>;
1582template class EhFrameHeader<ELF32BE>;
1583template class EhFrameHeader<ELF64LE>;
1584template class EhFrameHeader<ELF64BE>;
1585
George Rimar648a2c32015-10-20 08:54:27 +00001586template class GotPltSection<ELF32LE>;
1587template class GotPltSection<ELF32BE>;
1588template class GotPltSection<ELF64LE>;
1589template class GotPltSection<ELF64BE>;
1590
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001591template class GotSection<ELF32LE>;
1592template class GotSection<ELF32BE>;
1593template class GotSection<ELF64LE>;
1594template class GotSection<ELF64BE>;
1595
1596template class PltSection<ELF32LE>;
1597template class PltSection<ELF32BE>;
1598template class PltSection<ELF64LE>;
1599template class PltSection<ELF64BE>;
1600
1601template class RelocationSection<ELF32LE>;
1602template class RelocationSection<ELF32BE>;
1603template class RelocationSection<ELF64LE>;
1604template class RelocationSection<ELF64BE>;
1605
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001606template class InterpSection<ELF32LE>;
1607template class InterpSection<ELF32BE>;
1608template class InterpSection<ELF64LE>;
1609template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001610
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001611template class GnuHashTableSection<ELF32LE>;
1612template class GnuHashTableSection<ELF32BE>;
1613template class GnuHashTableSection<ELF64LE>;
1614template class GnuHashTableSection<ELF64BE>;
1615
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001616template class HashTableSection<ELF32LE>;
1617template class HashTableSection<ELF32BE>;
1618template class HashTableSection<ELF64LE>;
1619template class HashTableSection<ELF64BE>;
1620
1621template class DynamicSection<ELF32LE>;
1622template class DynamicSection<ELF32BE>;
1623template class DynamicSection<ELF64LE>;
1624template class DynamicSection<ELF64BE>;
1625
1626template class OutputSection<ELF32LE>;
1627template class OutputSection<ELF32BE>;
1628template class OutputSection<ELF64LE>;
1629template class OutputSection<ELF64BE>;
1630
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001631template class EHOutputSection<ELF32LE>;
1632template class EHOutputSection<ELF32BE>;
1633template class EHOutputSection<ELF64LE>;
1634template class EHOutputSection<ELF64BE>;
1635
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001636template class MipsReginfoOutputSection<ELF32LE>;
1637template class MipsReginfoOutputSection<ELF32BE>;
1638template class MipsReginfoOutputSection<ELF64LE>;
1639template class MipsReginfoOutputSection<ELF64BE>;
1640
Rafael Espindolac159c962015-10-19 21:00:02 +00001641template class MergeOutputSection<ELF32LE>;
1642template class MergeOutputSection<ELF32BE>;
1643template class MergeOutputSection<ELF64LE>;
1644template class MergeOutputSection<ELF64BE>;
1645
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001646template class StringTableSection<ELF32LE>;
1647template class StringTableSection<ELF32BE>;
1648template class StringTableSection<ELF64LE>;
1649template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001650
1651template class SymbolTableSection<ELF32LE>;
1652template class SymbolTableSection<ELF32BE>;
1653template class SymbolTableSection<ELF64LE>;
1654template class SymbolTableSection<ELF64BE>;
1655
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001656template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1657template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1658template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1659template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +00001660
Rui Ueyama5ec41f32016-01-26 01:32:00 +00001661template uint32_t getLocalRelTarget(const ObjectFile<ELF32LE> &,
1662 const ELFFile<ELF32LE>::Elf_Rel &,
1663 uint32_t);
1664template uint32_t getLocalRelTarget(const ObjectFile<ELF32BE> &,
1665 const ELFFile<ELF32BE>::Elf_Rel &,
1666 uint32_t);
1667template uint64_t getLocalRelTarget(const ObjectFile<ELF64LE> &,
1668 const ELFFile<ELF64LE>::Elf_Rel &,
1669 uint64_t);
1670template uint64_t getLocalRelTarget(const ObjectFile<ELF64BE> &,
1671 const ELFFile<ELF64BE>::Elf_Rel &,
1672 uint64_t);
1673template uint32_t getLocalRelTarget(const ObjectFile<ELF32LE> &,
1674 const ELFFile<ELF32LE>::Elf_Rela &,
1675 uint32_t);
1676template uint32_t getLocalRelTarget(const ObjectFile<ELF32BE> &,
1677 const ELFFile<ELF32BE>::Elf_Rela &,
1678 uint32_t);
1679template uint64_t getLocalRelTarget(const ObjectFile<ELF64LE> &,
1680 const ELFFile<ELF64LE>::Elf_Rela &,
1681 uint64_t);
1682template uint64_t getLocalRelTarget(const ObjectFile<ELF64BE> &,
1683 const ELFFile<ELF64BE>::Elf_Rela &,
1684 uint64_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001685}
1686}