blob: 53835cb9bd205c252329c6e8a73cbc8d7ada661a [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) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000042 Sym->GotPltIndex = Target->getGotPltHeaderEntriesNum() + 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 =
58 (Target->getGotPltHeaderEntriesNum() + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000059}
60
61template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000062 Target->writeGotPltHeaderEntries(Buf);
63 Buf += Target->getGotPltHeaderEntriesNum() * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000064 for (const SymbolBody *B : Entries) {
Igor Kudrin351b41d2015-11-16 17:44:08 +000065 Target->writeGotPltEntry(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;
90 Sym->GlobalDynIndex = Target->getGotHeaderEntriesNum() + 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() +
110 (Target->getGotHeaderEntriesNum() + MipsLocalEntries + B.GotIndex) *
111 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) {
131 size_t NewIndex = Target->getGotHeaderEntriesNum() + MipsLocalGotPos.size();
132 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 {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000150 return Target->getGotHeaderEntriesNum() + 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 =
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000155 (Target->getGotHeaderEntriesNum() + MipsLocalEntries + Entries.size()) *
156 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) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000160 Target->writeGotHeaderEntries(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 }
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000165 Buf += Target->getGotHeaderEntriesNum() * 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;
George Rimar648a2c32015-10-20 08:54:27 +0000192 bool LazyReloc = Target->supportsLazyRelocations();
193 if (LazyReloc) {
194 // First write PLT[0] entry which is special.
195 Target->writePltZeroEntry(Buf, Out<ELFT>::GotPlt->getVA(), this->getVA());
196 Off += Target->getPltZeroEntrySize();
197 }
George Rimarfb5d7f22015-11-26 19:58:51 +0000198 for (auto &I : Entries) {
George Rimar77b77792015-11-25 22:15:01 +0000199 const SymbolBody *E = I.first;
200 unsigned RelOff = I.second;
201 uint64_t GotVA =
202 LazyReloc ? Out<ELFT>::GotPlt->getVA() : Out<ELFT>::Got->getVA();
203 uint64_t GotE = LazyReloc ? Out<ELFT>::GotPlt->getEntryAddr(*E)
204 : Out<ELFT>::Got->getEntryAddr(*E);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000205 uint64_t Plt = this->getVA() + Off;
George Rimar77b77792015-11-25 22:15:01 +0000206 Target->writePltEntry(Buf + Off, GotVA, GotE, Plt, E->PltIndex, RelOff);
Rui Ueyama242ddf42015-10-12 18:56:36 +0000207 Off += Target->getPltEntrySize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000208 }
209}
210
211template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000212 Sym->PltIndex = Entries.size();
George Rimar77b77792015-11-25 22:15:01 +0000213 unsigned RelOff = Target->supportsLazyRelocations()
214 ? Out<ELFT>::RelaPlt->getRelocOffset()
215 : Out<ELFT>::RelaDyn->getRelocOffset();
216 Entries.push_back(std::make_pair(Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000217}
218
219template <class ELFT>
220typename PltSection<ELFT>::uintX_t
221PltSection<ELFT>::getEntryAddr(const SymbolBody &B) const {
George Rimar648a2c32015-10-20 08:54:27 +0000222 return this->getVA() + Target->getPltZeroEntrySize() +
223 B.PltIndex * Target->getPltEntrySize();
224}
225
226template <class ELFT> void PltSection<ELFT>::finalize() {
227 this->Header.sh_size = Target->getPltZeroEntrySize() +
228 Entries.size() * Target->getPltEntrySize();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000229}
230
231template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000232RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000233 : OutputSectionBase<ELFT>(Name, IsRela ? SHT_RELA : SHT_REL, SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000234 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000235 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
236 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
237}
238
George Rimar5828c232015-11-30 17:49:19 +0000239// Applies corresponding symbol and type for dynamic tls relocation.
240// Returns true if relocation was handled.
241template <class ELFT>
242bool RelocationSection<ELFT>::applyTlsDynamicReloc(SymbolBody *Body,
243 uint32_t Type, Elf_Rel *P,
244 Elf_Rel *N) {
245 if (Target->isTlsLocalDynamicReloc(Type)) {
246 P->setSymbolAndType(0, Target->getTlsModuleIndexReloc(), Config->Mips64EL);
George Rimarb17f7392015-12-01 18:24:07 +0000247 P->r_offset = Out<ELFT>::Got->getLocalTlsIndexVA();
George Rimar5828c232015-11-30 17:49:19 +0000248 return true;
249 }
250
George Rimar25411f252015-12-04 11:20:13 +0000251 if (!Body || !Target->isTlsGlobalDynamicReloc(Type))
252 return false;
253
254 if (Target->isTlsOptimized(Type, Body)) {
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000255 P->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000256 Target->getTlsGotReloc(), Config->Mips64EL);
257 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimar5828c232015-11-30 17:49:19 +0000258 return true;
259 }
George Rimar25411f252015-12-04 11:20:13 +0000260
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000261 P->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000262 Target->getTlsModuleIndexReloc(), Config->Mips64EL);
263 P->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body);
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000264 N->setSymbolAndType(Body->DynamicSymbolTableIndex,
George Rimar25411f252015-12-04 11:20:13 +0000265 Target->getTlsOffsetReloc(), Config->Mips64EL);
266 N->r_offset = Out<ELFT>::Got->getGlobalDynAddr(*Body) + sizeof(uintX_t);
267 return true;
George Rimar5828c232015-11-30 17:49:19 +0000268}
269
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000270template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000271 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000272 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000273 Buf += IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindola50534c22015-09-22 17:49:38 +0000274
Michael J. Spencer627ae702015-11-13 00:28:34 +0000275 // Skip placeholder for global dynamic TLS relocation pair. It was already
276 // handled by the previous relocation.
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000277 if (!Rel.C)
Michael J. Spencer627ae702015-11-13 00:28:34 +0000278 continue;
279
280 InputSectionBase<ELFT> &C = *Rel.C;
281 const Elf_Rel &RI = *Rel.RI;
Rui Ueyama64558522015-10-16 22:51:43 +0000282 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
Rafael Espindola41127ad2015-10-05 22:49:16 +0000283 const ObjectFile<ELFT> &File = *C.getFile();
Rui Ueyama35da9b62015-10-11 20:59:12 +0000284 SymbolBody *Body = File.getSymbolBody(SymIndex);
Rui Ueyama35da9b62015-10-11 20:59:12 +0000285 if (Body)
286 Body = Body->repl();
Rafael Espindola41127ad2015-10-05 22:49:16 +0000287
Rui Ueyama64558522015-10-16 22:51:43 +0000288 uint32_t Type = RI.getType(Config->Mips64EL);
George Rimar5828c232015-11-30 17:49:19 +0000289 if (applyTlsDynamicReloc(Body, Type, P, reinterpret_cast<Elf_Rel *>(Buf)))
Michael J. Spencer1e225612015-11-11 01:00:24 +0000290 continue;
Rui Ueyamabef81f32016-01-21 20:59:22 +0000291
292 // Emit a copy relocation.
293 auto *SS = dyn_cast_or_null<SharedSymbol<ELFT>>(Body);
294 if (SS && SS->NeedsCopy) {
295 P->setSymbolAndType(Body->DynamicSymbolTableIndex, Target->getCopyReloc(),
296 Config->Mips64EL);
297 P->r_offset = Out<ELFT>::Bss->getVA() + SS->OffsetInBss;
298 continue;
299 }
300
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000301 bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body);
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000302 bool CBP = canBePreempted(Body, NeedsGot);
George Rimar648a2c32015-10-20 08:54:27 +0000303 bool LazyReloc = Body && Target->supportsLazyRelocations() &&
304 Target->relocNeedsPlt(Type, *Body);
George Rimar6f17e092015-12-17 09:32:21 +0000305 bool IsDynRelative = Type == Target->getRelativeReloc();
Rafael Espindola49757522015-10-19 19:58:18 +0000306
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000307 unsigned Sym = CBP ? Body->DynamicSymbolTableIndex : 0;
George Rimarc7dc0be2015-12-15 08:48:39 +0000308 unsigned Reloc;
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000309 if (!CBP && Body && isGnuIFunc<ELFT>(*Body))
George Rimara07ff662015-12-21 10:12:06 +0000310 Reloc = Target->getIRelativeReloc();
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000311 else if (!CBP || IsDynRelative)
George Rimarc7dc0be2015-12-15 08:48:39 +0000312 Reloc = Target->getRelativeReloc();
313 else if (LazyReloc)
314 Reloc = Target->getPltReloc();
315 else if (NeedsGot)
Rui Ueyama62d0e322015-12-17 00:04:18 +0000316 Reloc = Body->isTls() ? Target->getTlsGotReloc() : Target->getGotReloc();
George Rimarc7dc0be2015-12-15 08:48:39 +0000317 else
318 Reloc = Target->getDynReloc(Type);
319 P->setSymbolAndType(Sym, Reloc, Config->Mips64EL);
Rafael Espindola52dca342015-10-07 00:58:20 +0000320
George Rimar4b5346f2015-12-29 16:17:32 +0000321 if (LazyReloc)
322 P->r_offset = Out<ELFT>::GotPlt->getEntryAddr(*Body);
323 else if (NeedsGot)
324 P->r_offset = Out<ELFT>::Got->getEntryAddr(*Body);
George Rimar4b5346f2015-12-29 16:17:32 +0000325 else
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000326 P->r_offset = C.getOffset(RI.r_offset) + C.OutSec->getVA();
Rafael Espindola49757522015-10-19 19:58:18 +0000327
Rafael Espindola932efcf2015-10-19 20:24:44 +0000328 uintX_t OrigAddend = 0;
Rafael Espindola49757522015-10-19 19:58:18 +0000329 if (IsRela && !NeedsGot)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000330 OrigAddend = static_cast<const Elf_Rela &>(RI).r_addend;
Rafael Espindola49757522015-10-19 19:58:18 +0000331
Rafael Espindola932efcf2015-10-19 20:24:44 +0000332 uintX_t Addend;
Rui Ueyamabef81f32016-01-21 20:59:22 +0000333 if (CBP || IsDynRelative)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000334 Addend = OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000335 else if (Body)
Rafael Espindola02346402015-12-21 20:18:04 +0000336 Addend = getSymVA<ELFT>(*Body) + OrigAddend;
Rui Ueyamab7f28672015-10-19 20:31:49 +0000337 else if (IsRela)
George Rimar0b8ed1d2015-12-21 10:37:33 +0000338 Addend =
339 getLocalRelTarget(File, static_cast<const Elf_Rela &>(RI),
340 getAddend<ELFT>(static_cast<const Elf_Rela &>(RI)));
Rui Ueyamab7f28672015-10-19 20:31:49 +0000341 else
George Rimar0b8ed1d2015-12-21 10:37:33 +0000342 Addend = getLocalRelTarget(File, RI, 0);
Rafael Espindola52dca342015-10-07 00:58:20 +0000343
344 if (IsRela)
345 static_cast<Elf_Rela *>(P)->r_addend = Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000346 }
347}
348
George Rimar77b77792015-11-25 22:15:01 +0000349template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
350 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
351 return EntrySize * Relocs.size();
352}
353
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000354template <class ELFT> void RelocationSection<ELFT>::finalize() {
George Rimara07ff662015-12-21 10:12:06 +0000355 this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex
356 : Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000357 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
358}
359
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000360template <class ELFT>
361InterpSection<ELFT>::InterpSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000362 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000363 this->Header.sh_size = Config->DynamicLinker.size() + 1;
364 this->Header.sh_addralign = 1;
365}
366
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000367template <class ELFT>
368void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
Rui Ueyama76c00632016-01-07 02:35:32 +0000369 Header.sh_name = Out<ELFT>::ShStrTab->addString(Name);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000370 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000371}
372
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000373template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000374 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
375}
376
Rafael Espindola35c6af32015-09-25 17:19:10 +0000377template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000378HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000379 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000380 this->Header.sh_entsize = sizeof(Elf_Word);
381 this->Header.sh_addralign = sizeof(Elf_Word);
382}
383
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000384static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000385 uint32_t H = 0;
386 for (char C : Name) {
387 H = (H << 4) + C;
388 uint32_t G = H & 0xf0000000;
389 if (G)
390 H ^= G >> 24;
391 H &= ~G;
392 }
393 return H;
394}
395
Rui Ueyama0db335f2015-10-07 16:58:54 +0000396template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000397 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000398
Rui Ueyama0db335f2015-10-07 16:58:54 +0000399 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000400 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000401
402 // Create as many buckets as there are symbols.
403 // FIXME: This is simplistic. We can try to optimize it, but implementing
404 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000405 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000406 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
407}
408
409template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000410 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000411 auto *P = reinterpret_cast<Elf_Word *>(Buf);
412 *P++ = NumSymbols; // nbucket
413 *P++ = NumSymbols; // nchain
414
415 Elf_Word *Buckets = P;
416 Elf_Word *Chains = P + NumSymbols;
417
Igor Kudrinf1d60292015-10-28 07:05:56 +0000418 for (SymbolBody *Body : Out<ELFT>::DynSymTab->getSymbols()) {
Igor Kudrinab665fc2015-10-20 21:47:58 +0000419 StringRef Name = Body->getName();
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000420 unsigned I = Body->DynamicSymbolTableIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000421 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000422 Chains[I] = Buckets[Hash];
423 Buckets[Hash] = I;
424 }
425}
426
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000427static uint32_t hashGnu(StringRef Name) {
428 uint32_t H = 5381;
429 for (uint8_t C : Name)
430 H = (H << 5) + H + C;
431 return H;
432}
433
434template <class ELFT>
435GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000436 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000437 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
438 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
439}
440
441template <class ELFT>
442unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
443 if (!NumHashed)
444 return 0;
445
446 // These values are prime numbers which are not greater than 2^(N-1) + 1.
447 // In result, for any particular NumHashed we return a prime number
448 // which is not greater than NumHashed.
449 static const unsigned Primes[] = {
450 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
451 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
452
453 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
454 array_lengthof(Primes) - 1)];
455}
456
457// Bloom filter estimation: at least 8 bits for each hashed symbol.
458// GNU Hash table requirement: it should be a power of 2,
459// the minimum value is 1, even for an empty table.
460// Expected results for a 32-bit target:
461// calcMaskWords(0..4) = 1
462// calcMaskWords(5..8) = 2
463// calcMaskWords(9..16) = 4
464// For a 64-bit target:
465// calcMaskWords(0..8) = 1
466// calcMaskWords(9..16) = 2
467// calcMaskWords(17..32) = 4
468template <class ELFT>
469unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
470 if (!NumHashed)
471 return 1;
472 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
473}
474
475template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000476 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000477 NBuckets = calcNBuckets(NumHashed);
478 MaskWords = calcMaskWords(NumHashed);
479 // Second hash shift estimation: just predefined values.
480 Shift2 = ELFT::Is64Bits ? 6 : 5;
481
482 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
483 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
484 + sizeof(Elf_Off) * MaskWords // Bloom Filter
485 + sizeof(Elf_Word) * NBuckets // Hash Buckets
486 + sizeof(Elf_Word) * NumHashed; // Hash Values
487}
488
489template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
490 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000491 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000492 return;
493 writeBloomFilter(Buf);
494 writeHashTable(Buf);
495}
496
497template <class ELFT>
498void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
499 auto *P = reinterpret_cast<Elf_Word *>(Buf);
500 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000501 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000502 *P++ = MaskWords;
503 *P++ = Shift2;
504 Buf = reinterpret_cast<uint8_t *>(P);
505}
506
507template <class ELFT>
508void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
509 unsigned C = sizeof(Elf_Off) * 8;
510
511 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000512 for (const HashedSymbolData &Item : HashedSymbols) {
513 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
514 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
515 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000516 Masks[Pos] |= V;
517 }
518 Buf += sizeof(Elf_Off) * MaskWords;
519}
520
521template <class ELFT>
522void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
523 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
524 Elf_Word *Values = Buckets + NBuckets;
525
526 int PrevBucket = -1;
527 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000528 for (const HashedSymbolData &Item : HashedSymbols) {
529 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000530 assert(PrevBucket <= Bucket);
531 if (Bucket != PrevBucket) {
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000532 Buckets[Bucket] = Item.Body->DynamicSymbolTableIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000533 PrevBucket = Bucket;
534 if (I > 0)
535 Values[I - 1] |= 1;
536 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000537 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000538 ++I;
539 }
540 if (I > 0)
541 Values[I - 1] |= 1;
542}
543
Rafael Espindola31f88882015-11-02 14:33:11 +0000544static bool includeInGnuHashTable(SymbolBody *B) {
545 // Assume that includeInDynamicSymtab() is already checked.
546 return !B->isUndefined();
547}
548
Rafael Espindola35c6af32015-09-25 17:19:10 +0000549template <class ELFT>
Igor Kudrinf1d60292015-10-28 07:05:56 +0000550void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolBody *> &Symbols) {
551 std::vector<SymbolBody *> NotHashed;
552 NotHashed.reserve(Symbols.size());
553 HashedSymbols.reserve(Symbols.size());
554 for (SymbolBody *B : Symbols) {
555 if (includeInGnuHashTable(B))
556 HashedSymbols.push_back(HashedSymbolData{B, hashGnu(B->getName())});
557 else
558 NotHashed.push_back(B);
559 }
560 if (HashedSymbols.empty())
561 return;
562
563 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
564 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
565 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
566 return L.Hash % NBuckets < R.Hash % NBuckets;
567 });
568
569 Symbols = std::move(NotHashed);
570 for (const HashedSymbolData &Item : HashedSymbols)
571 Symbols.push_back(Item.Body);
572}
573
574template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000575DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000576 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000577 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000578 Elf_Shdr &Header = this->Header;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000579 Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
580 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000581
582 // .dynamic section is not writable on MIPS.
583 // See "Special Section" in Chapter 4 in the following document:
584 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
585 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000586 Header.sh_flags = SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000587}
588
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000589template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000590 if (this->Header.sh_size)
591 return; // Already finalized.
592
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000593 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000594 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000595
596 unsigned NumEntries = 0;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000597 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000598 ++NumEntries; // DT_RELA / DT_REL
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000599 ++NumEntries; // DT_RELASZ / DT_RELSZ
600 ++NumEntries; // DT_RELAENT / DT_RELENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000601 }
George Rimar648a2c32015-10-20 08:54:27 +0000602 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
603 ++NumEntries; // DT_JMPREL
604 ++NumEntries; // DT_PLTRELSZ
Igor Kudrin304860a2015-11-12 04:39:49 +0000605 ++NumEntries; // DT_PLTGOT / DT_MIPS_PLTGOT
George Rimar648a2c32015-10-20 08:54:27 +0000606 ++NumEntries; // DT_PLTREL
607 }
608
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000609 ++NumEntries; // DT_SYMTAB
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000610 ++NumEntries; // DT_SYMENT
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000611 ++NumEntries; // DT_STRTAB
612 ++NumEntries; // DT_STRSZ
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000613 if (Out<ELFT>::GnuHashTab)
614 ++NumEntries; // DT_GNU_HASH
615 if (Out<ELFT>::HashTab)
616 ++NumEntries; // DT_HASH
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000617
Rui Ueyama7de3f372015-10-01 19:36:04 +0000618 if (!Config->RPath.empty()) {
Davide Italianoc39c75d2015-10-06 16:20:00 +0000619 ++NumEntries; // DT_RUNPATH / DT_RPATH
Rui Ueyama76c00632016-01-07 02:35:32 +0000620 Out<ELFT>::DynStrTab->reserve(Config->RPath);
Rui Ueyama7de3f372015-10-01 19:36:04 +0000621 }
622
623 if (!Config->SoName.empty()) {
624 ++NumEntries; // DT_SONAME
Rui Ueyama76c00632016-01-07 02:35:32 +0000625 Out<ELFT>::DynStrTab->reserve(Config->SoName);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000626 }
627
Rafael Espindola77572242015-10-02 19:37:55 +0000628 if (PreInitArraySec)
629 NumEntries += 2;
630 if (InitArraySec)
631 NumEntries += 2;
632 if (FiniArraySec)
633 NumEntries += 2;
634
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000635 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles()) {
Rui Ueyama35da9b62015-10-11 20:59:12 +0000636 if (!F->isNeeded())
637 continue;
Rui Ueyama76c00632016-01-07 02:35:32 +0000638 Out<ELFT>::DynStrTab->reserve(F->getSoName());
Rui Ueyama6ccc8ca2015-10-09 20:32:54 +0000639 ++NumEntries;
640 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000641
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000642 if (Symbol *S = SymTab.getSymbols().lookup(Config->Init))
Rafael Espindola167e62f2015-12-21 20:59:29 +0000643 InitSym = S->Body;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000644 if (Symbol *S = SymTab.getSymbols().lookup(Config->Fini))
Rafael Espindola167e62f2015-12-21 20:59:29 +0000645 FiniSym = S->Body;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000646 if (InitSym)
647 ++NumEntries; // DT_INIT
648 if (FiniSym)
649 ++NumEntries; // DT_FINI
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000650
651 if (Config->Bsymbolic)
652 DtFlags |= DF_SYMBOLIC;
653 if (Config->ZNodelete)
654 DtFlags1 |= DF_1_NODELETE;
655 if (Config->ZNow) {
656 DtFlags |= DF_BIND_NOW;
657 DtFlags1 |= DF_1_NOW;
658 }
659 if (Config->ZOrigin) {
660 DtFlags |= DF_ORIGIN;
661 DtFlags1 |= DF_1_ORIGIN;
662 }
663
664 if (DtFlags)
Davide Italiano58cbaf02015-10-19 23:32:16 +0000665 ++NumEntries; // DT_FLAGS
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000666 if (DtFlags1)
George Rimar97aad172015-10-07 15:00:21 +0000667 ++NumEntries; // DT_FLAGS_1
Igor Kudrin304860a2015-11-12 04:39:49 +0000668
Ed Mastef5d3cf62016-01-06 15:52:27 +0000669 if (!Config->Entry.empty())
670 ++NumEntries; // DT_DEBUG
671
Igor Kudrin304860a2015-11-12 04:39:49 +0000672 if (Config->EMachine == EM_MIPS) {
673 ++NumEntries; // DT_MIPS_RLD_VERSION
674 ++NumEntries; // DT_MIPS_FLAGS
675 ++NumEntries; // DT_MIPS_BASE_ADDRESS
676 ++NumEntries; // DT_MIPS_SYMTABNO
677 ++NumEntries; // DT_MIPS_LOCAL_GOTNO
678 ++NumEntries; // DT_MIPS_GOTSYM;
679 ++NumEntries; // DT_PLTGOT
680 if (Out<ELFT>::MipsRldMap)
681 ++NumEntries; // DT_MIPS_RLD_MAP
682 }
683
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000684 ++NumEntries; // DT_NULL
685
686 Header.sh_size = NumEntries * Header.sh_entsize;
687}
688
689template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000690 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
691
Rui Ueyama8c205d52015-10-02 01:33:31 +0000692 auto WritePtr = [&](int32_t Tag, uint64_t Val) {
693 P->d_tag = Tag;
694 P->d_un.d_ptr = Val;
695 ++P;
696 };
697
698 auto WriteVal = [&](int32_t Tag, uint32_t Val) {
699 P->d_tag = Tag;
700 P->d_un.d_val = Val;
701 ++P;
702 };
703
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000704 if (Out<ELFT>::RelaDyn->hasRelocs()) {
705 bool IsRela = Out<ELFT>::RelaDyn->isRela();
706 WritePtr(IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn->getVA());
707 WriteVal(IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000708 WriteVal(IsRela ? DT_RELAENT : DT_RELENT,
709 IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000710 }
George Rimar648a2c32015-10-20 08:54:27 +0000711 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
712 WritePtr(DT_JMPREL, Out<ELFT>::RelaPlt->getVA());
713 WriteVal(DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize());
Igor Kudrin304860a2015-11-12 04:39:49 +0000714 // On MIPS, the address of the .got.plt section is stored in
715 // the DT_MIPS_PLTGOT entry because the DT_PLTGOT entry points to
716 // the .got section. See "Dynamic Section" in the following document:
717 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
718 WritePtr((Config->EMachine == EM_MIPS) ? DT_MIPS_PLTGOT : DT_PLTGOT,
719 Out<ELFT>::GotPlt->getVA());
George Rimar648a2c32015-10-20 08:54:27 +0000720 WriteVal(DT_PLTREL, Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL);
721 }
Rui Ueyamac58656c2015-10-13 16:59:30 +0000722
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000723 WritePtr(DT_SYMTAB, Out<ELFT>::DynSymTab->getVA());
Rui Ueyama8c205d52015-10-02 01:33:31 +0000724 WritePtr(DT_SYMENT, sizeof(Elf_Sym));
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000725 WritePtr(DT_STRTAB, Out<ELFT>::DynStrTab->getVA());
Rui Ueyama76c00632016-01-07 02:35:32 +0000726 WriteVal(DT_STRSZ, Out<ELFT>::DynStrTab->getSize());
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000727 if (Out<ELFT>::GnuHashTab)
728 WritePtr(DT_GNU_HASH, Out<ELFT>::GnuHashTab->getVA());
729 if (Out<ELFT>::HashTab)
730 WritePtr(DT_HASH, Out<ELFT>::HashTab->getVA());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000731
Rui Ueyamadfa577b2015-11-19 23:30:10 +0000732 // If --enable-new-dtags is set, lld emits DT_RUNPATH
733 // instead of DT_RPATH. The two tags are functionally
734 // equivalent except for the following:
735 // - DT_RUNPATH is searched after LD_LIBRARY_PATH, while
736 // DT_RPATH is searched before.
737 // - DT_RUNPATH is used only to search for direct
738 // dependencies of the object it's contained in, while
739 // DT_RPATH is used for indirect dependencies as well.
Rui Ueyama8c205d52015-10-02 01:33:31 +0000740 if (!Config->RPath.empty())
Davide Italianoc39c75d2015-10-06 16:20:00 +0000741 WriteVal(Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Rui Ueyama76c00632016-01-07 02:35:32 +0000742 Out<ELFT>::DynStrTab->addString(Config->RPath));
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000743
Rui Ueyama8c205d52015-10-02 01:33:31 +0000744 if (!Config->SoName.empty())
Rui Ueyama76c00632016-01-07 02:35:32 +0000745 WriteVal(DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000746
Rafael Espindola77572242015-10-02 19:37:55 +0000747 auto WriteArray = [&](int32_t T1, int32_t T2,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000748 const OutputSectionBase<ELFT> *Sec) {
Rafael Espindola77572242015-10-02 19:37:55 +0000749 if (!Sec)
750 return;
751 WritePtr(T1, Sec->getVA());
752 WriteVal(T2, Sec->getSize());
753 };
754 WriteArray(DT_PREINIT_ARRAY, DT_PREINIT_ARRAYSZ, PreInitArraySec);
755 WriteArray(DT_INIT_ARRAY, DT_INIT_ARRAYSZ, InitArraySec);
756 WriteArray(DT_FINI_ARRAY, DT_FINI_ARRAYSZ, FiniArraySec);
757
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000758 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
Rui Ueyama35da9b62015-10-11 20:59:12 +0000759 if (F->isNeeded())
Rui Ueyama76c00632016-01-07 02:35:32 +0000760 WriteVal(DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName()));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000761
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000762 if (InitSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000763 WritePtr(DT_INIT, getSymVA<ELFT>(*InitSym));
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000764 if (FiniSym)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000765 WritePtr(DT_FINI, getSymVA<ELFT>(*FiniSym));
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000766 if (DtFlags)
767 WriteVal(DT_FLAGS, DtFlags);
768 if (DtFlags1)
769 WriteVal(DT_FLAGS_1, DtFlags1);
Ed Mastef5d3cf62016-01-06 15:52:27 +0000770 if (!Config->Entry.empty())
771 WriteVal(DT_DEBUG, 0);
Igor Kudrin304860a2015-11-12 04:39:49 +0000772
773 // See "Dynamic Section" in Chapter 5 in the following document
774 // for detailed description:
775 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
776 if (Config->EMachine == EM_MIPS) {
777 WriteVal(DT_MIPS_RLD_VERSION, 1);
778 WriteVal(DT_MIPS_FLAGS, RHF_NOTPOT);
779 WritePtr(DT_MIPS_BASE_ADDRESS, Target->getVAStart());
780 WriteVal(DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols());
781 WriteVal(DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum());
782 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyamaa02bba62015-12-17 00:12:04 +0000783 WriteVal(DT_MIPS_GOTSYM, B->DynamicSymbolTableIndex);
Igor Kudrin304860a2015-11-12 04:39:49 +0000784 else
785 WriteVal(DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols());
786 WritePtr(DT_PLTGOT, Out<ELFT>::Got->getVA());
787 if (Out<ELFT>::MipsRldMap)
788 WritePtr(DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap->getVA());
789 }
790
Rui Ueyama8c205d52015-10-02 01:33:31 +0000791 WriteVal(DT_NULL, 0);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000792}
793
794template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000795EhFrameHeader<ELFT>::EhFrameHeader()
796 : OutputSectionBase<ELFT>(".eh_frame_hdr", llvm::ELF::SHT_PROGBITS,
797 SHF_ALLOC) {
798 // It's a 4 bytes of header + pointer to the contents of the .eh_frame section
799 // + the number of FDE pointers in the table.
800 this->Header.sh_size = 12;
801}
802
803// We have to get PC values of FDEs. They depend on relocations
804// which are target specific, so we run this code after performing
805// all relocations. We read the values from ouput buffer according to the
806// encoding given for FDEs. Return value is an offset to the initial PC value
807// for the FDE.
808template <class ELFT>
809typename EhFrameHeader<ELFT>::uintX_t
810EhFrameHeader<ELFT>::getFdePc(uintX_t EhVA, const FdeData &F) {
811 const endianness E = ELFT::TargetEndianness;
812 assert((F.Enc & 0xF0) != dwarf::DW_EH_PE_datarel);
813
814 uintX_t FdeOff = EhVA + F.Off + 8;
815 switch (F.Enc & 0xF) {
816 case dwarf::DW_EH_PE_udata2:
817 case dwarf::DW_EH_PE_sdata2:
818 return FdeOff + read16<E>(F.PCRel);
819 case dwarf::DW_EH_PE_udata4:
820 case dwarf::DW_EH_PE_sdata4:
821 return FdeOff + read32<E>(F.PCRel);
822 case dwarf::DW_EH_PE_udata8:
823 case dwarf::DW_EH_PE_sdata8:
824 return FdeOff + read64<E>(F.PCRel);
825 case dwarf::DW_EH_PE_absptr:
826 if (sizeof(uintX_t) == 8)
827 return FdeOff + read64<E>(F.PCRel);
828 return FdeOff + read32<E>(F.PCRel);
829 }
830 error("unknown FDE size encoding");
831}
832
833template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
834 const endianness E = ELFT::TargetEndianness;
835
836 const uint8_t Header[] = {1, dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4,
837 dwarf::DW_EH_PE_udata4,
838 dwarf::DW_EH_PE_datarel | dwarf::DW_EH_PE_sdata4};
839 memcpy(Buf, Header, sizeof(Header));
840
841 uintX_t EhVA = Sec->getVA();
842 uintX_t VA = this->getVA();
843 uintX_t EhOff = EhVA - VA - 4;
844 write32<E>(Buf + 4, EhOff);
845 write32<E>(Buf + 8, this->FdeList.size());
846 Buf += 12;
847
848 // InitialPC -> Offset in .eh_frame, sorted by InitialPC.
849 std::map<uintX_t, size_t> PcToOffset;
850 for (const FdeData &F : FdeList)
851 PcToOffset[getFdePc(EhVA, F)] = F.Off;
852
853 for (auto &I : PcToOffset) {
854 // The first four bytes are an offset to the initial PC value for the FDE.
855 write32<E>(Buf, I.first - VA);
856 // The last four bytes are an offset to the FDE data itself.
857 write32<E>(Buf + 4, EhVA + I.second - VA);
858 Buf += 8;
859 }
860}
861
862template <class ELFT>
863void EhFrameHeader<ELFT>::assignEhFrame(EHOutputSection<ELFT> *Sec) {
George Rimar45ca88d2016-01-25 19:27:50 +0000864 assert((!this->Sec || this->Sec == Sec) &&
865 "multiple .eh_frame sections not supported for .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000866 Live = Config->EhFrameHdr;
867 this->Sec = Sec;
868}
869
870template <class ELFT>
871void EhFrameHeader<ELFT>::addFde(uint8_t Enc, size_t Off, uint8_t *PCRel) {
872 if (Live && (Enc & 0xF0) == dwarf::DW_EH_PE_datarel)
873 error("DW_EH_PE_datarel encoding unsupported for FDEs by .eh_frame_hdr");
874 FdeList.push_back(FdeData{Enc, Off, PCRel});
875}
876
877template <class ELFT> void EhFrameHeader<ELFT>::reserveFde() {
878 // Each FDE entry is 8 bytes long:
879 // The first four bytes are an offset to the initial PC value for the FDE. The
880 // last four byte are an offset to the FDE data itself.
881 this->Header.sh_size += 8;
882}
883
884template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000885OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type,
886 uintX_t Flags)
887 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000888
889template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000890void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
891 auto *S = cast<InputSection<ELFT>>(C);
892 Sections.push_back(S);
893 S->OutSec = this;
894 uint32_t Align = S->getAlign();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000895 if (Align > this->Header.sh_addralign)
896 this->Header.sh_addralign = Align;
897
898 uintX_t Off = this->Header.sh_size;
Rui Ueyama489a8062016-01-14 20:53:50 +0000899 Off = alignTo(Off, Align);
Rui Ueyama40845e62015-12-26 05:51:07 +0000900 S->OutSecOff = Off;
901 Off += S->getSize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000902 this->Header.sh_size = Off;
903}
904
905template <class ELFT>
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000906typename ELFFile<ELFT>::uintX_t elf2::getSymVA(const SymbolBody &S) {
Rafael Espindolacd076f02015-09-25 18:19:03 +0000907 switch (S.kind()) {
Rafael Espindola8614c562015-10-06 14:33:58 +0000908 case SymbolBody::DefinedSyntheticKind: {
909 auto &D = cast<DefinedSynthetic<ELFT>>(S);
Rafael Espindola4d4b06a2015-12-24 00:47:42 +0000910 return D.Section.getVA() + D.Value;
Rafael Espindola8614c562015-10-06 14:33:58 +0000911 }
Rafael Espindolacd076f02015-09-25 18:19:03 +0000912 case SymbolBody::DefinedRegularKind: {
913 const auto &DR = cast<DefinedRegular<ELFT>>(S);
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000914 InputSectionBase<ELFT> *SC = DR.Section;
915 if (!SC)
916 return DR.Sym.st_value;
Tom Stellard80efb162016-01-07 03:59:08 +0000917
918 // Symbol offsets for AMDGPU need to be the offset in bytes of the symbol
919 // from the beginning of the section.
920 if (Config->EMachine == EM_AMDGPU)
921 return SC->getOffset(DR.Sym);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000922 if (DR.Sym.getType() == STT_TLS)
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000923 return SC->OutSec->getVA() + SC->getOffset(DR.Sym) -
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000924 Out<ELFT>::TlsPhdr->p_vaddr;
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000925 return SC->OutSec->getVA() + SC->getOffset(DR.Sym);
Rafael Espindolacd076f02015-09-25 18:19:03 +0000926 }
927 case SymbolBody::DefinedCommonKind:
Rui Ueyamae57c4872016-01-05 16:35:43 +0000928 return Out<ELFT>::Bss->getVA() + cast<DefinedCommon>(S).OffsetInBss;
George Rimarbc590fe2015-10-28 16:48:58 +0000929 case SymbolBody::SharedKind: {
930 auto &SS = cast<SharedSymbol<ELFT>>(S);
Rui Ueyamabb936062015-12-17 01:14:23 +0000931 if (SS.NeedsCopy)
Rui Ueyamae57c4872016-01-05 16:35:43 +0000932 return Out<ELFT>::Bss->getVA() + SS.OffsetInBss;
George Rimarbc590fe2015-10-28 16:48:58 +0000933 return 0;
934 }
Rafael Espindola5d7593b2015-12-22 23:00:50 +0000935 case SymbolBody::UndefinedElfKind:
Rafael Espindolacd076f02015-09-25 18:19:03 +0000936 case SymbolBody::UndefinedKind:
937 return 0;
938 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +0000939 assert(S.isUsedInRegularObj() && "Lazy symbol reached writer");
940 return 0;
Rafael Espindolacd076f02015-09-25 18:19:03 +0000941 }
Denis Protivensky92aa1c02015-10-07 08:21:34 +0000942 llvm_unreachable("Invalid symbol kind");
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000943}
944
Rui Ueyama34f29242015-10-13 19:51:57 +0000945// Returns a VA which a relocatin RI refers to. Used only for local symbols.
946// For non-local symbols, use getSymVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000947template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000948typename ELFFile<ELFT>::uintX_t
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000949elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
950 const Elf_Rel_Impl<ELFT, IsRela> &RI,
951 typename ELFFile<ELFT>::uintX_t Addend) {
Rafael Espindola932efcf2015-10-19 20:24:44 +0000952 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
953 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
954
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000955 // PPC64 has a special relocation representing the TOC base pointer
956 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000957 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000958 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000959
Rui Ueyama126d08f2015-10-12 20:28:22 +0000960 const Elf_Sym *Sym =
961 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000962
Rui Ueyama126d08f2015-10-12 20:28:22 +0000963 if (!Sym)
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000964 error("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000965
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000966 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
967
968 if (Sym->getType() == STT_TLS)
969 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
George Rimarcc06a6f2015-11-29 14:14:20 +0000970 Out<ELFT>::TlsPhdr->p_vaddr;
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000971
Rafael Espindola444576d2015-10-09 19:25:07 +0000972 // According to the ELF spec reference to a local symbol from outside
973 // the group are not allowed. Unfortunately .eh_frame breaks that rule
974 // and must be treated specially. For now we just replace the symbol with
975 // 0.
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000976 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000977 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000978
Rafael Espindolac159c962015-10-19 21:00:02 +0000979 uintX_t VA = Section->OutSec->getVA();
980 if (isa<InputSection<ELFT>>(Section))
981 return VA + Section->getOffset(*Sym) + Addend;
982
983 uintX_t Offset = Sym->st_value;
984 if (Sym->getType() == STT_SECTION) {
985 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000986 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000987 }
Rafael Espindola7f040bf2015-12-25 01:00:41 +0000988 return VA + Section->getOffset(Offset) + Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000989}
990
Rui Ueyama34f29242015-10-13 19:51:57 +0000991// Returns true if a symbol can be replaced at load-time by a symbol
992// with the same name defined in other ELF executable or DSO.
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000993bool elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000994 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000995 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000996 if (Body->isShared())
997 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000998
999 if (Body->isUndefined()) {
1000 if (!Body->isWeak())
1001 return true;
1002
1003 // This is an horrible corner case. Ideally we would like to say that any
1004 // undefined symbol can be preempted so that the dynamic linker has a
1005 // chance of finding it at runtime.
1006 //
1007 // The problem is that the code sequence used to test for weak undef
1008 // functions looks like
1009 // if (func) func()
1010 // If the code is -fPIC the first reference is a load from the got and
1011 // everything works.
1012 // If the code is not -fPIC there is no reasonable way to solve it:
1013 // * A relocation writing to the text segment will fail (it is ro).
1014 // * A copy relocation doesn't work for functions.
1015 // * The trick of using a plt entry as the address would fail here since
1016 // the plt entry would have a non zero address.
1017 // Since we cannot do anything better, we just resolve the symbol to 0 and
1018 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +00001019 //
1020 // As an extra hack, assume that if we are producing a shared library the
1021 // user knows what he or she is doing and can handle a dynamic relocation.
1022 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +00001023 }
Rafael Espindolaa6627382015-10-06 23:56:53 +00001024 if (!Config->Shared)
1025 return false;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001026 return Body->getVisibility() == STV_DEFAULT;
Rafael Espindolaa6627382015-10-06 23:56:53 +00001027}
1028
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001029template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +00001030 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001031 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001032}
1033
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001034template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001035EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t Type,
1036 uintX_t Flags)
George Rimarf6bc65a2016-01-15 13:34:52 +00001037 : OutputSectionBase<ELFT>(Name, Type, Flags) {
1038 Out<ELFT>::EhFrameHdr->assignEhFrame(this);
1039}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001040
1041template <class ELFT>
1042EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
1043 : S(S), Index(Index) {}
1044
1045template <class ELFT> StringRef EHRegion<ELFT>::data() const {
1046 ArrayRef<uint8_t> SecData = S->getSectionData();
1047 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
1048 size_t Start = Offsets[Index].first;
1049 size_t End =
1050 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
1051 return StringRef((const char *)SecData.data() + Start, End - Start);
1052}
1053
1054template <class ELFT>
1055Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
1056 : EHRegion<ELFT>(S, Index) {}
1057
George Rimarf6bc65a2016-01-15 13:34:52 +00001058// Read a byte and advance D by one byte.
1059static uint8_t readByte(ArrayRef<uint8_t> &D) {
1060 if (D.empty())
1061 error("corrupted or unsupported CIE information");
1062 uint8_t B = D.front();
1063 D = D.slice(1);
1064 return B;
1065}
1066
1067static void skipLeb128(ArrayRef<uint8_t> &D) {
1068 while (!D.empty()) {
1069 uint8_t Val = D.front();
1070 D = D.slice(1);
1071 if ((Val & 0x80) == 0)
1072 return;
1073 }
1074 error("corrupted or unsupported CIE information");
1075}
1076
1077template <class ELFT> static unsigned getSizeForEncoding(unsigned Enc) {
1078 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1079 switch (Enc & 0x0f) {
1080 default:
1081 error("unknown FDE encoding");
1082 case dwarf::DW_EH_PE_absptr:
1083 case dwarf::DW_EH_PE_signed:
1084 return sizeof(uintX_t);
1085 case dwarf::DW_EH_PE_udata2:
1086 case dwarf::DW_EH_PE_sdata2:
1087 return 2;
1088 case dwarf::DW_EH_PE_udata4:
1089 case dwarf::DW_EH_PE_sdata4:
1090 return 4;
1091 case dwarf::DW_EH_PE_udata8:
1092 case dwarf::DW_EH_PE_sdata8:
1093 return 8;
1094 }
1095}
1096
1097template <class ELFT>
1098uint8_t EHOutputSection<ELFT>::getFdeEncoding(ArrayRef<uint8_t> D) {
1099 auto Check = [](bool C) {
1100 if (!C)
1101 error("corrupted or unsupported CIE information");
1102 };
1103
1104 Check(D.size() >= 8);
1105 D = D.slice(8);
1106
1107 uint8_t Version = readByte(D);
1108 if (Version != 1 && Version != 3)
1109 error("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
1110
1111 auto AugEnd = std::find(D.begin() + 1, D.end(), '\0');
1112 Check(AugEnd != D.end());
1113 ArrayRef<uint8_t> AugString(D.begin(), AugEnd - D.begin());
1114 D = D.slice(AugString.size() + 1);
1115
1116 // Code alignment factor should always be 1 for .eh_frame.
1117 if (readByte(D) != 1)
1118 error("CIE code alignment must be 1");
1119 // Skip data alignment factor
1120 skipLeb128(D);
1121
1122 // Skip the return address register. In CIE version 1 this is a single
1123 // byte. In CIE version 3 this is an unsigned LEB128.
1124 if (Version == 1)
1125 readByte(D);
1126 else
1127 skipLeb128(D);
1128
1129 while (!AugString.empty()) {
1130 switch (readByte(AugString)) {
1131 case 'z':
1132 skipLeb128(D);
1133 break;
1134 case 'R':
1135 return readByte(D);
1136 case 'P': {
1137 uint8_t Enc = readByte(D);
1138 if ((Enc & 0xf0) == dwarf::DW_EH_PE_aligned)
1139 error("DW_EH_PE_aligned encoding for address of a personality routine "
1140 "handler not supported");
1141 unsigned EncSize = getSizeForEncoding<ELFT>(Enc);
1142 Check(D.size() >= EncSize);
1143 D = D.slice(EncSize);
1144 break;
1145 }
1146 case 'S':
1147 case 'L':
1148 // L: Language Specific Data Area (LSDA) encoding
1149 // S: This CIE represents a stack frame for the invocation of a signal
1150 // handler
1151 break;
1152 default:
1153 error("unknown .eh_frame augmentation string value");
1154 }
1155 }
1156 return dwarf::DW_EH_PE_absptr;
1157}
1158
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001159template <class ELFT>
1160template <bool IsRela>
1161void EHOutputSection<ELFT>::addSectionAux(
1162 EHInputSection<ELFT> *S,
1163 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
1164 const endianness E = ELFT::TargetEndianness;
1165
1166 S->OutSec = this;
1167 uint32_t Align = S->getAlign();
1168 if (Align > this->Header.sh_addralign)
1169 this->Header.sh_addralign = Align;
1170
1171 Sections.push_back(S);
1172
1173 ArrayRef<uint8_t> SecData = S->getSectionData();
1174 ArrayRef<uint8_t> D = SecData;
1175 uintX_t Offset = 0;
1176 auto RelI = Rels.begin();
1177 auto RelE = Rels.end();
1178
George Rimar147747a2016-01-02 16:55:01 +00001179 DenseMap<unsigned, unsigned> OffsetToIndex;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001180 while (!D.empty()) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001181 unsigned Index = S->Offsets.size();
1182 S->Offsets.push_back(std::make_pair(Offset, -1));
1183
George Rimar003be4f2015-12-17 09:23:40 +00001184 uintX_t Length = readEntryLength(D);
George Rimarf6bc65a2016-01-15 13:34:52 +00001185 // If CIE/FDE data length is zero then Length is 4, this
1186 // shall be considered a terminator and processing shall end.
1187 if (Length == 4)
1188 break;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001189 StringRef Entry((const char *)D.data(), Length);
1190
1191 while (RelI != RelE && RelI->r_offset < Offset)
1192 ++RelI;
1193 uintX_t NextOffset = Offset + Length;
1194 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
1195
1196 uint32_t ID = read32<E>(D.data() + 4);
1197 if (ID == 0) {
1198 // CIE
1199 Cie<ELFT> C(S, Index);
George Rimarf6bc65a2016-01-15 13:34:52 +00001200 if (Config->EhFrameHdr)
1201 C.FdeEncoding = getFdeEncoding(D);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001202
1203 StringRef Personality;
1204 if (HasReloc) {
1205 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
1206 SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl();
1207 Personality = Body.getName();
1208 }
1209
1210 std::pair<StringRef, StringRef> CieInfo(Entry, Personality);
1211 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
George Rimar147747a2016-01-02 16:55:01 +00001212 if (P.second) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001213 Cies.push_back(C);
Rui Ueyama489a8062016-01-14 20:53:50 +00001214 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
George Rimar147747a2016-01-02 16:55:01 +00001215 }
1216 OffsetToIndex[Offset] = P.first->second;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001217 } else {
1218 if (!HasReloc)
1219 error("FDE doesn't reference another section");
1220 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
1221 if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) {
1222 uint32_t CieOffset = Offset + 4 - ID;
George Rimar147747a2016-01-02 16:55:01 +00001223 auto I = OffsetToIndex.find(CieOffset);
1224 if (I == OffsetToIndex.end())
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001225 error("Invalid CIE reference");
George Rimar147747a2016-01-02 16:55:01 +00001226 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
George Rimarf6bc65a2016-01-15 13:34:52 +00001227 Out<ELFT>::EhFrameHdr->reserveFde();
Rui Ueyama489a8062016-01-14 20:53:50 +00001228 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001229 }
1230 }
1231
1232 Offset = NextOffset;
1233 D = D.slice(Length);
1234 }
1235}
1236
1237template <class ELFT>
George Rimar003be4f2015-12-17 09:23:40 +00001238typename EHOutputSection<ELFT>::uintX_t
1239EHOutputSection<ELFT>::readEntryLength(ArrayRef<uint8_t> D) {
1240 const endianness E = ELFT::TargetEndianness;
1241
1242 if (D.size() < 4)
1243 error("Truncated CIE/FDE length");
1244 uint64_t Len = read32<E>(D.data());
1245 if (Len < UINT32_MAX) {
1246 if (Len > (UINT32_MAX - 4))
1247 error("CIE/FIE size is too large");
1248 if (Len + 4 > D.size())
1249 error("CIE/FIE ends past the end of the section");
1250 return Len + 4;
1251 }
1252
1253 if (D.size() < 12)
1254 error("Truncated CIE/FDE length");
1255 Len = read64<E>(D.data() + 4);
1256 if (Len > (UINT64_MAX - 12))
1257 error("CIE/FIE size is too large");
1258 if (Len + 12 > D.size())
1259 error("CIE/FIE ends past the end of the section");
1260 return Len + 12;
1261}
1262
1263template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001264void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1265 auto *S = cast<EHInputSection<ELFT>>(C);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001266 const Elf_Shdr *RelSec = S->RelocSection;
1267 if (!RelSec)
1268 return addSectionAux(
1269 S, make_range((const Elf_Rela *)nullptr, (const Elf_Rela *)nullptr));
1270 ELFFile<ELFT> &Obj = S->getFile()->getObj();
1271 if (RelSec->sh_type == SHT_RELA)
1272 return addSectionAux(S, Obj.relas(RelSec));
1273 return addSectionAux(S, Obj.rels(RelSec));
1274}
1275
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001276template <class ELFT>
1277static typename ELFFile<ELFT>::uintX_t writeAlignedCieOrFde(StringRef Data,
1278 uint8_t *Buf) {
1279 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1280 const endianness E = ELFT::TargetEndianness;
Rui Ueyama489a8062016-01-14 20:53:50 +00001281 uint64_t Len = alignTo(Data.size(), sizeof(uintX_t));
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001282 write32<E>(Buf, Len - 4);
1283 memcpy(Buf + 4, Data.data() + 4, Data.size() - 4);
1284 return Len;
1285}
1286
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001287template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1288 const endianness E = ELFT::TargetEndianness;
1289 size_t Offset = 0;
1290 for (const Cie<ELFT> &C : Cies) {
1291 size_t CieOffset = Offset;
1292
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001293 uintX_t CIELen = writeAlignedCieOrFde<ELFT>(C.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001294 C.S->Offsets[C.Index].second = Offset;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001295 Offset += CIELen;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001296
1297 for (const EHRegion<ELFT> &F : C.Fdes) {
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001298 uintX_t Len = writeAlignedCieOrFde<ELFT>(F.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001299 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001300 F.S->Offsets[F.Index].second = Offset;
George Rimarf6bc65a2016-01-15 13:34:52 +00001301 Out<ELFT>::EhFrameHdr->addFde(C.FdeEncoding, Offset, Buf + Offset + 8);
George Rimare72beba2015-12-21 09:38:59 +00001302 Offset += Len;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001303 }
1304 }
1305
1306 for (EHInputSection<ELFT> *S : Sections) {
1307 const Elf_Shdr *RelSec = S->RelocSection;
1308 if (!RelSec)
1309 continue;
1310 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1311 if (RelSec->sh_type == SHT_RELA)
1312 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1313 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001314 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001315 }
1316}
1317
1318template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001319MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
1320 uintX_t Flags)
1321 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001322
1323template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001324 if (shouldTailMerge()) {
1325 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001326 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001327 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001328 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001329 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1330 StringRef Data = P.first;
1331 memcpy(Buf + P.second, Data.data(), Data.size());
1332 }
1333}
1334
1335static size_t findNull(StringRef S, size_t EntSize) {
1336 // Optimize the common case.
1337 if (EntSize == 1)
1338 return S.find(0);
1339
1340 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1341 const char *B = S.begin() + I;
1342 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1343 return I;
1344 }
1345 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001346}
1347
1348template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001349void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1350 auto *S = cast<MergeInputSection<ELFT>>(C);
Rafael Espindolac159c962015-10-19 21:00:02 +00001351 S->OutSec = this;
1352 uint32_t Align = S->getAlign();
1353 if (Align > this->Header.sh_addralign)
1354 this->Header.sh_addralign = Align;
1355
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001356 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001357 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001358 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001359
1360 if (this->Header.sh_flags & SHF_STRINGS) {
Rui Ueyamaad87ff72016-01-06 02:52:24 +00001361 uintX_t Offset = 0;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001362 while (!Data.empty()) {
1363 size_t End = findNull(Data, EntSize);
1364 if (End == StringRef::npos)
1365 error("String is not null terminated");
1366 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001367 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001368 if (shouldTailMerge())
1369 OutputOffset = -1;
1370 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1371 uintX_t Size = End + EntSize;
1372 Data = Data.substr(Size);
1373 Offset += Size;
1374 }
1375 } else {
1376 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1377 StringRef Entry = Data.substr(I, EntSize);
1378 size_t OutputOffset = Builder.add(Entry);
Rui Ueyamaad87ff72016-01-06 02:52:24 +00001379 S->Offsets.push_back(std::make_pair(I, OutputOffset));
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001380 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001381 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001382}
1383
1384template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001385unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1386 return Builder.getOffset(Val);
1387}
1388
1389template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1390 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1391}
1392
1393template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1394 if (shouldTailMerge())
1395 Builder.finalize();
1396 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001397}
1398
1399template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001400StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001401 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1402 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001403 Dynamic(Dynamic) {
1404 this->Header.sh_addralign = 1;
1405}
1406
Rui Ueyama76c00632016-01-07 02:35:32 +00001407// String tables are created in two phases. First you call reserve()
1408// to reserve room in the string table, and then call addString() to actually
1409// add that string.
1410//
1411// Why two phases? We want to know the size of the string table as early as
1412// possible to fix file layout. So we have separated finalize(), which
1413// determines the size of the section, from writeTo(), which writes the section
1414// contents to the output buffer. If we merge reserve() with addString(),
1415// we need a plumbing work for finalize() and writeTo() so that offsets
1416// we obtained in the former function can be written in the latter.
1417// This design eliminated that need.
1418template <class ELFT> void StringTableSection<ELFT>::reserve(StringRef S) {
1419 Reserved += S.size() + 1; // +1 for NUL
1420}
1421
Rui Ueyama7562d0e2016-01-07 16:41:06 +00001422// Adds a string to the string table. You must call reserve() with the
Rui Ueyama76c00632016-01-07 02:35:32 +00001423// same string before calling addString().
1424template <class ELFT> size_t StringTableSection<ELFT>::addString(StringRef S) {
1425 size_t Pos = Used;
1426 Strings.push_back(S);
1427 Used += S.size() + 1;
1428 Reserved -= S.size() + 1;
1429 assert((int64_t)Reserved >= 0);
1430 return Pos;
1431}
1432
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001433template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001434 // ELF string tables start with NUL byte, so advance the pointer by one.
1435 ++Buf;
1436 for (StringRef S : Strings) {
1437 memcpy(Buf, S.data(), S.size());
1438 Buf += S.size() + 1;
1439 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001440}
1441
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001442template <class ELFT>
Rui Ueyama83cd6e02016-01-06 20:11:55 +00001443bool elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &File, StringRef SymName,
1444 const typename ELFFile<ELFT>::Elf_Sym &Sym) {
Simon Atanasyan0d5e1b72016-01-20 18:59:45 +00001445 if (Sym.getType() == STT_SECTION || Sym.getType() == STT_FILE)
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001446 return false;
1447
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001448 InputSectionBase<ELFT> *Sec = File.getSection(Sym);
Rafael Espindola444576d2015-10-09 19:25:07 +00001449 // If sym references a section in a discarded group, don't keep it.
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001450 if (Sec == &InputSection<ELFT>::Discarded)
Rafael Espindola4cda5812015-10-16 15:29:48 +00001451 return false;
Rafael Espindola444576d2015-10-09 19:25:07 +00001452
Davide Italiano6993ba42015-09-26 00:47:56 +00001453 if (Config->DiscardNone)
1454 return true;
1455
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001456 // In ELF assembly .L symbols are normally discarded by the assembler.
1457 // If the assembler fails to do so, the linker discards them if
1458 // * --discard-locals is used.
1459 // * The symbol is in a SHF_MERGE section, which is normally the reason for
1460 // the assembler keeping the .L symbol.
Rafael Espindola29925632015-12-11 19:09:21 +00001461 if (!SymName.startswith(".L") && !SymName.empty())
Rafael Espindolaa6763e82015-12-11 18:49:29 +00001462 return true;
1463
1464 if (Config->DiscardLocals)
1465 return false;
1466
1467 return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE);
Davide Italiano6993ba42015-09-26 00:47:56 +00001468}
1469
Rafael Espindola35c6af32015-09-25 17:19:10 +00001470template <class ELFT>
1471SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001472 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001473 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1474 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001475 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001476 Table(Table), StrTabSec(StrTabSec) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001477 this->Header.sh_entsize = sizeof(Elf_Sym);
1478 this->Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
Rafael Espindola35c6af32015-09-25 17:19:10 +00001479}
1480
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001481// Orders symbols according to their positions in the GOT,
1482// in compliance with MIPS ABI rules.
1483// See "Global Offset Table" in Chapter 5 in the following document
1484// for detailed description:
1485// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1486static bool sortMipsSymbols(SymbolBody *L, SymbolBody *R) {
1487 if (!L->isInGot() || !R->isInGot())
1488 return R->isInGot();
1489 return L->GotIndex < R->GotIndex;
1490}
1491
Rui Ueyama0db335f2015-10-07 16:58:54 +00001492template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001493 if (this->Header.sh_size)
1494 return; // Already finalized.
1495
Rui Ueyama0db335f2015-10-07 16:58:54 +00001496 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001497 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001498 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001499
1500 if (!StrTabSec.isDynamic()) {
1501 std::stable_sort(Symbols.begin(), Symbols.end(),
Igor Kudrinf1d60292015-10-28 07:05:56 +00001502 [](SymbolBody *L, SymbolBody *R) {
1503 return getSymbolBinding(L) == STB_LOCAL &&
1504 getSymbolBinding(R) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001505 });
1506 return;
1507 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001508 if (Out<ELFT>::GnuHashTab)
1509 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1510 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001511 else if (Config->EMachine == EM_MIPS)
1512 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001513 size_t I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +00001514 for (SymbolBody *B : Symbols)
Rui Ueyamaa02bba62015-12-17 00:12:04 +00001515 B->DynamicSymbolTableIndex = ++I;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001516}
1517
1518template <class ELFT>
Igor Kudrinab665fc2015-10-20 21:47:58 +00001519void SymbolTableSection<ELFT>::addLocalSymbol(StringRef Name) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001520 StrTabSec.reserve(Name);
Rui Ueyama0db335f2015-10-07 16:58:54 +00001521 ++NumVisible;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001522 ++NumLocals;
1523}
1524
1525template <class ELFT>
1526void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001527 StrTabSec.reserve(Body->getName());
Igor Kudrinf1d60292015-10-28 07:05:56 +00001528 Symbols.push_back(Body);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001529 ++NumVisible;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001530}
1531
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001532template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001533 Buf += sizeof(Elf_Sym);
1534
1535 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1536 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001537 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1538 writeLocalSymbols(Buf);
1539
1540 writeGlobalSymbols(Buf);
1541}
1542
1543template <class ELFT>
1544void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1545 // Iterate over all input object files to copy their local symbols
1546 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001547 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
1548 Elf_Sym_Range Syms = File->getLocalSymbols();
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001549 for (const Elf_Sym &Sym : Syms) {
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001550 ErrorOr<StringRef> SymNameOrErr = Sym.getName(File->getStringTable());
Rafael Espindola26fd69d2015-10-09 16:15:57 +00001551 error(SymNameOrErr);
1552 StringRef SymName = *SymNameOrErr;
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001553 if (!shouldKeepInSymtab<ELFT>(*File, SymName, Sym))
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001554 continue;
Rafael Espindola444576d2015-10-09 19:25:07 +00001555
Rui Ueyamac55733e2015-09-30 00:54:29 +00001556 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001557 uintX_t VA = 0;
Rafael Espindola4cda5812015-10-16 15:29:48 +00001558 if (Sym.st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001559 ESym->st_shndx = SHN_ABS;
Rafael Espindolac159c962015-10-19 21:00:02 +00001560 VA = Sym.st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001561 } else {
Rafael Espindola48225b42015-10-23 19:55:11 +00001562 InputSectionBase<ELFT> *Section = File->getSection(Sym);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001563 if (!Section->isLive())
1564 continue;
Rafael Espindolac159c962015-10-19 21:00:02 +00001565 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1566 ESym->st_shndx = OutSec->SectionIndex;
Tom Stellard80efb162016-01-07 03:59:08 +00001567 VA = Section->getOffset(Sym);
1568 // Symbol offsets for AMDGPU need to be the offset in bytes of the
1569 // symbol from the beginning of the section.
1570 if (Config->EMachine != EM_AMDGPU)
1571 VA += OutSec->getVA();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001572 }
Rui Ueyama76c00632016-01-07 02:35:32 +00001573 ESym->st_name = StrTabSec.addString(SymName);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001574 ESym->st_size = Sym.st_size;
1575 ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001576 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001577 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001578 }
1579 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001580}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001581
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001582template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001583static const typename llvm::object::ELFFile<ELFT>::Elf_Sym *
1584getElfSym(SymbolBody &Body) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001585 if (auto *EBody = dyn_cast<DefinedElf<ELFT>>(&Body))
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001586 return &EBody->Sym;
1587 if (auto *EBody = dyn_cast<UndefinedElf<ELFT>>(&Body))
1588 return &EBody->Sym;
1589 return nullptr;
1590}
1591
1592template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001593void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001594 // Write the internal symbol table contents to the output symbol table
1595 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001596 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +00001597 for (SymbolBody *Body : Symbols) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001598 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001599
Rafael Espindola8614c562015-10-06 14:33:58 +00001600 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001601 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001602 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001603 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001604 case SymbolBody::DefinedRegularKind: {
1605 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001606 if (InputSectionBase<ELFT> *Sec = Sym->Section) {
1607 if (!Sec->isLive())
1608 continue;
1609 OutSec = Sec->OutSec;
1610 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001611 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001612 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001613 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001614 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001615 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001616 case SymbolBody::SharedKind: {
Rui Ueyamabb936062015-12-17 01:14:23 +00001617 if (cast<SharedSymbol<ELFT>>(Body)->NeedsCopy)
George Rimarbc590fe2015-10-28 16:48:58 +00001618 OutSec = Out<ELFT>::Bss;
1619 break;
1620 }
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001621 case SymbolBody::UndefinedElfKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001622 case SymbolBody::UndefinedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001623 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001624 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001625 }
1626
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001627 StringRef Name = Body->getName();
Rui Ueyama76c00632016-01-07 02:35:32 +00001628 ESym->st_name = StrTabSec.addString(Name);
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001629
Rafael Espindola8614c562015-10-06 14:33:58 +00001630 unsigned char Type = STT_NOTYPE;
1631 uintX_t Size = 0;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001632 if (const Elf_Sym *InputSym = getElfSym<ELFT>(*Body)) {
1633 Type = InputSym->getType();
1634 Size = InputSym->st_size;
Rafael Espindola11191912015-12-24 16:23:37 +00001635 } else if (auto *C = dyn_cast<DefinedCommon>(Body)) {
1636 Type = STT_OBJECT;
1637 Size = C->Size;
Rafael Espindola8614c562015-10-06 14:33:58 +00001638 }
1639
Igor Kudrin853b88d2015-10-20 20:52:14 +00001640 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001641 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001642 ESym->setVisibility(Body->getVisibility());
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001643 ESym->st_value = getSymVA<ELFT>(*Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001644
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001645 if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001646 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001647 else if (isa<DefinedRegular<ELFT>>(Body))
1648 ESym->st_shndx = SHN_ABS;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001649
1650 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001651 }
1652}
1653
Igor Kudrin853b88d2015-10-20 20:52:14 +00001654template <class ELFT>
1655uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001656 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001657 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1658 return STB_LOCAL;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001659 if (const Elf_Sym *ESym = getElfSym<ELFT>(*Body))
1660 return ESym->getBinding();
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001661 if (isa<DefinedSynthetic<ELFT>>(Body))
1662 return STB_LOCAL;
Igor Kudrin853b88d2015-10-20 20:52:14 +00001663 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1664}
1665
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001666template <class ELFT>
1667MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1668 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1669 this->Header.sh_addralign = 4;
1670 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1671 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1672}
1673
1674template <class ELFT>
1675void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1676 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
1677 R->ri_gp_value = getMipsGpAddr<ELFT>();
Rui Ueyama70eed362016-01-06 22:42:43 +00001678 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001679}
1680
1681template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001682void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001683 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001684 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001685 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001686}
1687
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001688namespace lld {
1689namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001690template class OutputSectionBase<ELF32LE>;
1691template class OutputSectionBase<ELF32BE>;
1692template class OutputSectionBase<ELF64LE>;
1693template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001694
George Rimarf6bc65a2016-01-15 13:34:52 +00001695template class EhFrameHeader<ELF32LE>;
1696template class EhFrameHeader<ELF32BE>;
1697template class EhFrameHeader<ELF64LE>;
1698template class EhFrameHeader<ELF64BE>;
1699
George Rimar648a2c32015-10-20 08:54:27 +00001700template class GotPltSection<ELF32LE>;
1701template class GotPltSection<ELF32BE>;
1702template class GotPltSection<ELF64LE>;
1703template class GotPltSection<ELF64BE>;
1704
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001705template class GotSection<ELF32LE>;
1706template class GotSection<ELF32BE>;
1707template class GotSection<ELF64LE>;
1708template class GotSection<ELF64BE>;
1709
1710template class PltSection<ELF32LE>;
1711template class PltSection<ELF32BE>;
1712template class PltSection<ELF64LE>;
1713template class PltSection<ELF64BE>;
1714
1715template class RelocationSection<ELF32LE>;
1716template class RelocationSection<ELF32BE>;
1717template class RelocationSection<ELF64LE>;
1718template class RelocationSection<ELF64BE>;
1719
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001720template class InterpSection<ELF32LE>;
1721template class InterpSection<ELF32BE>;
1722template class InterpSection<ELF64LE>;
1723template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001724
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001725template class GnuHashTableSection<ELF32LE>;
1726template class GnuHashTableSection<ELF32BE>;
1727template class GnuHashTableSection<ELF64LE>;
1728template class GnuHashTableSection<ELF64BE>;
1729
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001730template class HashTableSection<ELF32LE>;
1731template class HashTableSection<ELF32BE>;
1732template class HashTableSection<ELF64LE>;
1733template class HashTableSection<ELF64BE>;
1734
1735template class DynamicSection<ELF32LE>;
1736template class DynamicSection<ELF32BE>;
1737template class DynamicSection<ELF64LE>;
1738template class DynamicSection<ELF64BE>;
1739
1740template class OutputSection<ELF32LE>;
1741template class OutputSection<ELF32BE>;
1742template class OutputSection<ELF64LE>;
1743template class OutputSection<ELF64BE>;
1744
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001745template class EHOutputSection<ELF32LE>;
1746template class EHOutputSection<ELF32BE>;
1747template class EHOutputSection<ELF64LE>;
1748template class EHOutputSection<ELF64BE>;
1749
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001750template class MipsReginfoOutputSection<ELF32LE>;
1751template class MipsReginfoOutputSection<ELF32BE>;
1752template class MipsReginfoOutputSection<ELF64LE>;
1753template class MipsReginfoOutputSection<ELF64BE>;
1754
Rafael Espindolac159c962015-10-19 21:00:02 +00001755template class MergeOutputSection<ELF32LE>;
1756template class MergeOutputSection<ELF32BE>;
1757template class MergeOutputSection<ELF64LE>;
1758template class MergeOutputSection<ELF64BE>;
1759
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001760template class StringTableSection<ELF32LE>;
1761template class StringTableSection<ELF32BE>;
1762template class StringTableSection<ELF64LE>;
1763template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001764
1765template class SymbolTableSection<ELF32LE>;
1766template class SymbolTableSection<ELF32BE>;
1767template class SymbolTableSection<ELF64LE>;
1768template class SymbolTableSection<ELF64BE>;
1769
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001770template ELFFile<ELF32LE>::uintX_t getSymVA<ELF32LE>(const SymbolBody &);
1771template ELFFile<ELF32BE>::uintX_t getSymVA<ELF32BE>(const SymbolBody &);
1772template ELFFile<ELF64LE>::uintX_t getSymVA<ELF64LE>(const SymbolBody &);
1773template ELFFile<ELF64BE>::uintX_t getSymVA<ELF64BE>(const SymbolBody &);
Rafael Espindola4ea00212015-09-21 22:01:00 +00001774
Rafael Espindola56f965f2015-09-21 22:48:12 +00001775template ELFFile<ELF32LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001776getLocalRelTarget(const ObjectFile<ELF32LE> &,
George Rimar0b8ed1d2015-12-21 10:37:33 +00001777 const ELFFile<ELF32LE>::Elf_Rel &,
1778 ELFFile<ELF32LE>::uintX_t Addend);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001779template ELFFile<ELF32BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001780getLocalRelTarget(const ObjectFile<ELF32BE> &,
George Rimar0b8ed1d2015-12-21 10:37:33 +00001781 const ELFFile<ELF32BE>::Elf_Rel &,
1782 ELFFile<ELF32BE>::uintX_t Addend);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001783template ELFFile<ELF64LE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001784getLocalRelTarget(const ObjectFile<ELF64LE> &,
George Rimar0b8ed1d2015-12-21 10:37:33 +00001785 const ELFFile<ELF64LE>::Elf_Rel &,
1786 ELFFile<ELF64LE>::uintX_t Addend);
Rafael Espindola56f965f2015-09-21 22:48:12 +00001787template ELFFile<ELF64BE>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +00001788getLocalRelTarget(const ObjectFile<ELF64BE> &,
George Rimar0b8ed1d2015-12-21 10:37:33 +00001789 const ELFFile<ELF64BE>::Elf_Rel &,
1790 ELFFile<ELF64BE>::uintX_t Addend);
Rafael Espindolac159c962015-10-19 21:00:02 +00001791
Rafael Espindola444576d2015-10-09 19:25:07 +00001792template bool shouldKeepInSymtab<ELF32LE>(const ObjectFile<ELF32LE> &,
1793 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001794 const ELFFile<ELF32LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001795template bool shouldKeepInSymtab<ELF32BE>(const ObjectFile<ELF32BE> &,
1796 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001797 const ELFFile<ELF32BE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001798template bool shouldKeepInSymtab<ELF64LE>(const ObjectFile<ELF64LE> &,
1799 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001800 const ELFFile<ELF64LE>::Elf_Sym &);
Rafael Espindola444576d2015-10-09 19:25:07 +00001801template bool shouldKeepInSymtab<ELF64BE>(const ObjectFile<ELF64BE> &,
1802 StringRef,
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001803 const ELFFile<ELF64BE>::Elf_Sym &);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001804}
1805}