blob: 2cec4e71bb86ca9fb2cf76fe82784efc2e9b9083 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.cpp -------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "OutputSections.h"
11#include "Config.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000012#include "SymbolTable.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000013#include "Target.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000014#include "llvm/Support/Dwarf.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000015#include "llvm/Support/MathExtras.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000016#include <map>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000017
Rafael Espindola5805c4f2015-09-21 21:38:08 +000018using namespace llvm;
19using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000020using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021using namespace llvm::ELF;
22
23using namespace lld;
24using namespace lld::elf2;
25
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000026template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +000027OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t Type,
28 uintX_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029 : Name(Name) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000030 memset(&Header, 0, sizeof(Elf_Shdr));
Rui Ueyamad97e5c42016-01-07 18:33:11 +000031 Header.sh_type = Type;
32 Header.sh_flags = Flags;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000033}
34
Rafael Espindola35c6af32015-09-25 17:19:10 +000035template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000036GotPltSection<ELFT>::GotPltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000037 : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
George Rimar648a2c32015-10-20 08:54:27 +000038 this->Header.sh_addralign = sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000039}
40
41template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama724d6252016-01-29 01:49:32 +000042 Sym->GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
George Rimar648a2c32015-10-20 08:54:27 +000043 Entries.push_back(Sym);
44}
45
46template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +000047 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +000048}
49
George Rimar648a2c32015-10-20 08:54:27 +000050template <class ELFT> void GotPltSection<ELFT>::finalize() {
Igor Kudrin351b41d2015-11-16 17:44:08 +000051 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +000052 (Target->GotPltHeaderEntriesNum + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000053}
54
55template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +000056 Target->writeGotPltHeader(Buf);
Rui Ueyama724d6252016-01-29 01:49:32 +000057 Buf += Target->GotPltHeaderEntriesNum * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000058 for (const SymbolBody *B : Entries) {
Rui Ueyamab5a69702016-02-01 21:00:35 +000059 Target->writeGotPlt(Buf, B->getPltVA<ELFT>());
George Rimar648a2c32015-10-20 08:54:27 +000060 Buf += sizeof(uintX_t);
61 }
62}
63
64template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000065GotSection<ELFT>::GotSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000066 : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000067 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +000068 this->Header.sh_flags |= SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000069 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000070}
71
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000073 Sym->GotIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074 Entries.push_back(Sym);
George Rimar687138c2015-11-13 16:28:53 +000075}
76
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000077template <class ELFT> void GotSection<ELFT>::addMipsLocalEntry() {
78 ++MipsLocalEntries;
79}
80
George Rimar90cd0a82015-12-01 19:20:26 +000081template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) {
82 if (Sym->hasGlobalDynIndex())
83 return false;
Rui Ueyama724d6252016-01-29 01:49:32 +000084 Sym->GlobalDynIndex = Target->GotHeaderEntriesNum + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +000085 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +000086 Entries.push_back(Sym);
87 Entries.push_back(nullptr);
George Rimar90cd0a82015-12-01 19:20:26 +000088 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000089}
90
Rui Ueyama0e53c7d2016-02-05 00:10:02 +000091// Reserves TLS entries for a TLS module ID and a TLS block offset.
92// In total it takes two GOT slots.
93template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
94 if (TlsIndexOff != uint32_t(-1))
George Rimarb17f7392015-12-01 18:24:07 +000095 return false;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +000096 TlsIndexOff = Entries.size() * sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +000097 Entries.push_back(nullptr);
98 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +000099 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000100}
101
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000102template <class ELFT>
103typename GotSection<ELFT>::uintX_t
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000104GotSection<ELFT>::getMipsLocalFullAddr(const SymbolBody &B) {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000105 return getMipsLocalEntryAddr(B.getVA<ELFT>());
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000106}
107
108template <class ELFT>
109typename GotSection<ELFT>::uintX_t
110GotSection<ELFT>::getMipsLocalPageAddr(uintX_t EntryValue) {
111 // Initialize the entry by the %hi(EntryValue) expression
112 // but without right-shifting.
113 return getMipsLocalEntryAddr((EntryValue + 0x8000) & ~0xffff);
114}
115
116template <class ELFT>
117typename GotSection<ELFT>::uintX_t
118GotSection<ELFT>::getMipsLocalEntryAddr(uintX_t EntryValue) {
Rui Ueyama724d6252016-01-29 01:49:32 +0000119 size_t NewIndex = Target->GotHeaderEntriesNum + MipsLocalGotPos.size();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000120 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
121 assert(!P.second || MipsLocalGotPos.size() <= MipsLocalEntries);
122 return this->getVA() + P.first->second * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123}
124
Igor Kudrin304860a2015-11-12 04:39:49 +0000125template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000126typename GotSection<ELFT>::uintX_t
127GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
128 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
129}
130
131template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000132const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
133 return Entries.empty() ? nullptr : Entries.front();
134}
135
136template <class ELFT>
137unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
Rui Ueyama724d6252016-01-29 01:49:32 +0000138 return Target->GotHeaderEntriesNum + MipsLocalEntries;
Igor Kudrin304860a2015-11-12 04:39:49 +0000139}
140
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000141template <class ELFT> void GotSection<ELFT>::finalize() {
142 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +0000143 (Target->GotHeaderEntriesNum + MipsLocalEntries + Entries.size()) *
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000144 sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000145}
146
Rafael Espindolaa6627382015-10-06 23:56:53 +0000147template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000148 Target->writeGotHeader(Buf);
Rui Ueyama5cbf5d22016-02-02 02:29:03 +0000149 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000150 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
151 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, L.first);
152 }
Rui Ueyama724d6252016-01-29 01:49:32 +0000153 Buf += Target->GotHeaderEntriesNum * sizeof(uintX_t);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000154 Buf += MipsLocalEntries * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000155 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000156 uint8_t *Entry = Buf;
157 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000158 if (!B)
159 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000160 // MIPS has special rules to fill up GOT entries.
161 // See "Global Offset Table" in Chapter 5 in the following document
162 // for detailed description:
163 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
164 // As the first approach, we can just store addresses for all symbols.
165 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000166 continue; // The dynamic linker will take care of it.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000167 uintX_t VA = B->getVA<ELFT>();
Rafael Espindolae782f672015-10-07 03:56:05 +0000168 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000169 }
170}
171
Rafael Espindola35c6af32015-09-25 17:19:10 +0000172template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000173PltSection<ELFT>::PltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000174 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000175 this->Header.sh_addralign = 16;
176}
177
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000178template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000179 size_t Off = 0;
Rui Ueyama9398f862016-01-29 04:15:02 +0000180 if (Target->UseLazyBinding) {
Rui Ueyama74937fc2016-02-02 02:53:58 +0000181 // At beginning of PLT, we have code to call the dynamic linker
182 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama900e2d22016-01-29 03:51:49 +0000183 Target->writePltZero(Buf);
Rui Ueyama62515452016-01-29 03:00:32 +0000184 Off += Target->PltZeroSize;
George Rimar648a2c32015-10-20 08:54:27 +0000185 }
George Rimarfb5d7f22015-11-26 19:58:51 +0000186 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000187 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000188 unsigned RelOff = I.second;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000189 uint64_t Got =
190 Target->UseLazyBinding ? B->getGotPltVA<ELFT>() : B->getGotVA<ELFT>();
Rui Ueyama242ddf42015-10-12 18:56:36 +0000191 uint64_t Plt = this->getVA() + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000192 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000193 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000194 }
195}
196
197template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000198 Sym->PltIndex = Entries.size();
Rui Ueyama724d6252016-01-29 01:49:32 +0000199 unsigned RelOff = Target->UseLazyBinding
George Rimar77b77792015-11-25 22:15:01 +0000200 ? Out<ELFT>::RelaPlt->getRelocOffset()
201 : Out<ELFT>::RelaDyn->getRelocOffset();
202 Entries.push_back(std::make_pair(Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203}
204
George Rimar648a2c32015-10-20 08:54:27 +0000205template <class ELFT> void PltSection<ELFT>::finalize() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000206 this->Header.sh_size =
Rui Ueyama62515452016-01-29 03:00:32 +0000207 Target->PltZeroSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000208}
209
210template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000211RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000212 : OutputSectionBase<ELFT>(Name, IsRela ? SHT_RELA : SHT_REL, SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000213 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000214 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000215 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000216}
217
George Rimar5828c232015-11-30 17:49:19 +0000218template <class ELFT>
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000219void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
220 SymbolBody *Sym = Reloc.Sym;
221 if (!Reloc.UseSymVA && Sym)
Rafael Espindolaabebed92016-02-05 15:27:15 +0000222 Sym->MustBeInDynSym = true;
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000223 Relocs.push_back(Reloc);
224}
225
226template <class ELFT>
Rafael Espindolade9857e2016-02-04 21:33:05 +0000227static typename ELFFile<ELFT>::uintX_t
228getOffset(const DynamicReloc<ELFT> &Rel) {
229 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
230 SymbolBody *Sym = Rel.Sym;
231 switch (Rel.OKind) {
232 case DynamicReloc<ELFT>::Off_GTlsIndex:
233 return Out<ELFT>::Got->getGlobalDynAddr(*Sym);
234 case DynamicReloc<ELFT>::Off_GTlsOffset:
235 return Out<ELFT>::Got->getGlobalDynAddr(*Sym) + sizeof(uintX_t);
236 case DynamicReloc<ELFT>::Off_LTlsIndex:
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000237 return Out<ELFT>::Got->getTlsIndexVA();
Rafael Espindolade9857e2016-02-04 21:33:05 +0000238 case DynamicReloc<ELFT>::Off_Sec:
239 return Rel.OffsetSec->getOffset(Rel.OffsetInSec) +
240 Rel.OffsetSec->OutSec->getVA();
241 case DynamicReloc<ELFT>::Off_Bss:
242 return cast<SharedSymbol<ELFT>>(Sym)->OffsetInBss + Out<ELFT>::Bss->getVA();
243 case DynamicReloc<ELFT>::Off_Got:
244 return Sym->getGotVA<ELFT>();
245 case DynamicReloc<ELFT>::Off_GotPlt:
246 return Sym->getGotPltVA<ELFT>();
George Rimar5828c232015-11-30 17:49:19 +0000247 }
Rafael Espindolade9857e2016-02-04 21:33:05 +0000248 llvm_unreachable("Invalid offset kind");
George Rimar5828c232015-11-30 17:49:19 +0000249}
250
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000252 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000253 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000254 Buf += IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindolade9857e2016-02-04 21:33:05 +0000255 SymbolBody *Sym = Rel.Sym;
Rui Ueyamab0210e832016-01-26 00:24:57 +0000256
Rafael Espindola9e3f84b2016-02-03 21:02:48 +0000257 if (IsRela) {
Rafael Espindolade9857e2016-02-04 21:33:05 +0000258 uintX_t VA = 0;
259 if (Rel.UseSymVA)
260 VA = Sym->getVA<ELFT>();
261 else if (Rel.TargetSec)
262 VA = Rel.TargetSec->getOffset(Rel.OffsetInTargetSec) +
263 Rel.TargetSec->OutSec->getVA();
264 reinterpret_cast<Elf_Rela *>(P)->r_addend = Rel.Addend + VA;
Rafael Espindola9e3f84b2016-02-03 21:02:48 +0000265 }
266
Rafael Espindolade9857e2016-02-04 21:33:05 +0000267 P->r_offset = getOffset(Rel);
268 uint32_t SymIdx = (!Rel.UseSymVA && Sym) ? Sym->DynsymIndex : 0;
269 P->setSymbolAndType(SymIdx, Rel.Type, Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000270 }
271}
272
George Rimar77b77792015-11-25 22:15:01 +0000273template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
Rui Ueyama5a0b2f72016-02-05 19:13:18 +0000274 return this->Header.sh_entsize * Relocs.size();
George Rimar77b77792015-11-25 22:15:01 +0000275}
276
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000277template <class ELFT> void RelocationSection<ELFT>::finalize() {
George Rimara07ff662015-12-21 10:12:06 +0000278 this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex
279 : Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000280 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
281}
282
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000283template <class ELFT>
284InterpSection<ELFT>::InterpSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000285 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000286 this->Header.sh_size = Config->DynamicLinker.size() + 1;
287 this->Header.sh_addralign = 1;
288}
289
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000290template <class ELFT>
291void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
292 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000293}
294
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000295template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000296 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
297}
298
Rafael Espindola35c6af32015-09-25 17:19:10 +0000299template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000300HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000301 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000302 this->Header.sh_entsize = sizeof(Elf_Word);
303 this->Header.sh_addralign = sizeof(Elf_Word);
304}
305
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000306static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000307 uint32_t H = 0;
308 for (char C : Name) {
309 H = (H << 4) + C;
310 uint32_t G = H & 0xf0000000;
311 if (G)
312 H ^= G >> 24;
313 H &= ~G;
314 }
315 return H;
316}
317
Rui Ueyama0db335f2015-10-07 16:58:54 +0000318template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000319 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000320
Rui Ueyama0db335f2015-10-07 16:58:54 +0000321 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000322 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000323
324 // Create as many buckets as there are symbols.
325 // FIXME: This is simplistic. We can try to optimize it, but implementing
326 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000327 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000328 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
329}
330
331template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000332 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000333 auto *P = reinterpret_cast<Elf_Word *>(Buf);
334 *P++ = NumSymbols; // nbucket
335 *P++ = NumSymbols; // nchain
336
337 Elf_Word *Buckets = P;
338 Elf_Word *Chains = P + NumSymbols;
339
Rafael Espindolae2c24612016-01-29 01:24:25 +0000340 for (const std::pair<SymbolBody *, unsigned> &P :
341 Out<ELFT>::DynSymTab->getSymbols()) {
342 SymbolBody *Body = P.first;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000343 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000344 unsigned I = Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000345 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000346 Chains[I] = Buckets[Hash];
347 Buckets[Hash] = I;
348 }
349}
350
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000351static uint32_t hashGnu(StringRef Name) {
352 uint32_t H = 5381;
353 for (uint8_t C : Name)
354 H = (H << 5) + H + C;
355 return H;
356}
357
358template <class ELFT>
359GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000360 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000361 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000362 this->Header.sh_addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000363}
364
365template <class ELFT>
366unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
367 if (!NumHashed)
368 return 0;
369
370 // These values are prime numbers which are not greater than 2^(N-1) + 1.
371 // In result, for any particular NumHashed we return a prime number
372 // which is not greater than NumHashed.
373 static const unsigned Primes[] = {
374 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
375 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
376
377 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
378 array_lengthof(Primes) - 1)];
379}
380
381// Bloom filter estimation: at least 8 bits for each hashed symbol.
382// GNU Hash table requirement: it should be a power of 2,
383// the minimum value is 1, even for an empty table.
384// Expected results for a 32-bit target:
385// calcMaskWords(0..4) = 1
386// calcMaskWords(5..8) = 2
387// calcMaskWords(9..16) = 4
388// For a 64-bit target:
389// calcMaskWords(0..8) = 1
390// calcMaskWords(9..16) = 2
391// calcMaskWords(17..32) = 4
392template <class ELFT>
393unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
394 if (!NumHashed)
395 return 1;
396 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
397}
398
399template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000400 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000401 NBuckets = calcNBuckets(NumHashed);
402 MaskWords = calcMaskWords(NumHashed);
403 // Second hash shift estimation: just predefined values.
404 Shift2 = ELFT::Is64Bits ? 6 : 5;
405
406 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
407 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
408 + sizeof(Elf_Off) * MaskWords // Bloom Filter
409 + sizeof(Elf_Word) * NBuckets // Hash Buckets
410 + sizeof(Elf_Word) * NumHashed; // Hash Values
411}
412
413template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
414 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000415 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000416 return;
417 writeBloomFilter(Buf);
418 writeHashTable(Buf);
419}
420
421template <class ELFT>
422void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
423 auto *P = reinterpret_cast<Elf_Word *>(Buf);
424 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000425 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000426 *P++ = MaskWords;
427 *P++ = Shift2;
428 Buf = reinterpret_cast<uint8_t *>(P);
429}
430
431template <class ELFT>
432void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
433 unsigned C = sizeof(Elf_Off) * 8;
434
435 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000436 for (const HashedSymbolData &Item : HashedSymbols) {
437 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
438 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
439 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000440 Masks[Pos] |= V;
441 }
442 Buf += sizeof(Elf_Off) * MaskWords;
443}
444
445template <class ELFT>
446void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
447 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
448 Elf_Word *Values = Buckets + NBuckets;
449
450 int PrevBucket = -1;
451 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000452 for (const HashedSymbolData &Item : HashedSymbols) {
453 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000454 assert(PrevBucket <= Bucket);
455 if (Bucket != PrevBucket) {
Rui Ueyama572a6f72016-01-29 01:49:33 +0000456 Buckets[Bucket] = Item.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000457 PrevBucket = Bucket;
458 if (I > 0)
459 Values[I - 1] |= 1;
460 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000461 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000462 ++I;
463 }
464 if (I > 0)
465 Values[I - 1] |= 1;
466}
467
Rafael Espindola31f88882015-11-02 14:33:11 +0000468static bool includeInGnuHashTable(SymbolBody *B) {
Rui Ueyamac112c1b2016-01-29 02:17:01 +0000469 // Assume that includeInDynsym() is already checked.
Rafael Espindola31f88882015-11-02 14:33:11 +0000470 return !B->isUndefined();
471}
472
Rafael Espindola35c6af32015-09-25 17:19:10 +0000473template <class ELFT>
Rafael Espindolae2c24612016-01-29 01:24:25 +0000474void GnuHashTableSection<ELFT>::addSymbols(
475 std::vector<std::pair<SymbolBody *, unsigned>> &Symbols) {
476 std::vector<std::pair<SymbolBody *, unsigned>> NotHashed;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000477 NotHashed.reserve(Symbols.size());
478 HashedSymbols.reserve(Symbols.size());
Rafael Espindolae2c24612016-01-29 01:24:25 +0000479 for (const std::pair<SymbolBody *, unsigned> &P : Symbols) {
480 SymbolBody *B = P.first;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000481 if (includeInGnuHashTable(B))
Rafael Espindolae2c24612016-01-29 01:24:25 +0000482 HashedSymbols.push_back(
483 HashedSymbolData{B, P.second, hashGnu(B->getName())});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000484 else
Rafael Espindolae2c24612016-01-29 01:24:25 +0000485 NotHashed.push_back(P);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000486 }
487 if (HashedSymbols.empty())
488 return;
489
490 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
491 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
492 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
493 return L.Hash % NBuckets < R.Hash % NBuckets;
494 });
495
496 Symbols = std::move(NotHashed);
497 for (const HashedSymbolData &Item : HashedSymbols)
Rafael Espindolae2c24612016-01-29 01:24:25 +0000498 Symbols.push_back(std::make_pair(Item.Body, Item.STName));
Igor Kudrinf1d60292015-10-28 07:05:56 +0000499}
500
501template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000502DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000503 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000504 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000505 Elf_Shdr &Header = this->Header;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000506 Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000507 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000508
509 // .dynamic section is not writable on MIPS.
510 // See "Special Section" in Chapter 4 in the following document:
511 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
512 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000513 Header.sh_flags = SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000514}
515
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000516template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000517 if (this->Header.sh_size)
518 return; // Already finalized.
519
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000520 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000521 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000522
Rafael Espindolae2c24612016-01-29 01:24:25 +0000523 auto Add = [=](Entry E) { Entries.push_back(E); };
524
525 // Add strings. We know that these are the last strings to be added to
Rafael Espindolade069362016-01-25 21:32:04 +0000526 // DynStrTab and doing this here allows this function to set DT_STRSZ.
527 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000528 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
529 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rafael Espindolade069362016-01-25 21:32:04 +0000530 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
531 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000532 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
533 if (!Config->SoName.empty())
534 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
535
Rafael Espindolade069362016-01-25 21:32:04 +0000536 Out<ELFT>::DynStrTab->finalize();
537
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000538 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindolade069362016-01-25 21:32:04 +0000539 bool IsRela = Out<ELFT>::RelaDyn->isRela();
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000540 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
541 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
542 Add({IsRela ? DT_RELAENT : DT_RELENT,
543 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000544 }
George Rimar648a2c32015-10-20 08:54:27 +0000545 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000546 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
547 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
548 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
549 Out<ELFT>::GotPlt});
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000550 Add({DT_PLTREL, uint64_t(Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000551 }
552
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000553 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
554 Add({DT_SYMENT, sizeof(Elf_Sym)});
555 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
556 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000557 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000558 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000559 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000560 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000561
Rafael Espindolade069362016-01-25 21:32:04 +0000562 if (PreInitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000563 Add({DT_PREINIT_ARRAY, PreInitArraySec});
564 Add({DT_PREINIT_ARRAYSZ, PreInitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000565 }
566 if (InitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000567 Add({DT_INIT_ARRAY, InitArraySec});
568 Add({DT_INIT_ARRAYSZ, (uintX_t)InitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000569 }
570 if (FiniArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000571 Add({DT_FINI_ARRAY, FiniArraySec});
572 Add({DT_FINI_ARRAYSZ, (uintX_t)FiniArraySec->getSize()});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000573 }
574
Rui Ueyama304d1352016-01-25 21:47:25 +0000575 if (SymbolBody *B = SymTab.find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000576 Add({DT_INIT, B});
Rui Ueyama304d1352016-01-25 21:47:25 +0000577 if (SymbolBody *B = SymTab.find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000578 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000579
Rafael Espindolade069362016-01-25 21:32:04 +0000580 uint32_t DtFlags = 0;
581 uint32_t DtFlags1 = 0;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000582 if (Config->Bsymbolic)
583 DtFlags |= DF_SYMBOLIC;
584 if (Config->ZNodelete)
585 DtFlags1 |= DF_1_NODELETE;
586 if (Config->ZNow) {
587 DtFlags |= DF_BIND_NOW;
588 DtFlags1 |= DF_1_NOW;
589 }
590 if (Config->ZOrigin) {
591 DtFlags |= DF_ORIGIN;
592 DtFlags1 |= DF_1_ORIGIN;
593 }
594
595 if (DtFlags)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000596 Add({DT_FLAGS, DtFlags});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000597 if (DtFlags1)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000598 Add({DT_FLAGS_1, DtFlags1});
Igor Kudrin304860a2015-11-12 04:39:49 +0000599
Ed Mastef5d3cf62016-01-06 15:52:27 +0000600 if (!Config->Entry.empty())
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000601 Add({DT_DEBUG, (uint64_t)0});
Ed Mastef5d3cf62016-01-06 15:52:27 +0000602
Igor Kudrin304860a2015-11-12 04:39:49 +0000603 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000604 Add({DT_MIPS_RLD_VERSION, 1});
605 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
606 Add({DT_MIPS_BASE_ADDRESS, (uintX_t)Target->getVAStart()});
607 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
608 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000609 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000610 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000611 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000612 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
613 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000614 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000615 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000616 }
617
Rafael Espindolade069362016-01-25 21:32:04 +0000618 // +1 for DT_NULL
619 Header.sh_size = (Entries.size() + 1) * Header.sh_entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000620}
621
622template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000623 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
624
Rafael Espindolade069362016-01-25 21:32:04 +0000625 for (const Entry &E : Entries) {
626 P->d_tag = E.Tag;
627 switch (E.Kind) {
628 case Entry::SecAddr:
629 P->d_un.d_ptr = E.OutSec->getVA();
630 break;
631 case Entry::SymAddr:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000632 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
Rafael Espindolade069362016-01-25 21:32:04 +0000633 break;
634 case Entry::PlainInt:
635 P->d_un.d_val = E.Val;
636 break;
637 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000638 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000639 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000640}
641
642template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000643EhFrameHeader<ELFT>::EhFrameHeader()
644 : OutputSectionBase<ELFT>(".eh_frame_hdr", llvm::ELF::SHT_PROGBITS,
645 SHF_ALLOC) {
646 // It's a 4 bytes of header + pointer to the contents of the .eh_frame section
647 // + the number of FDE pointers in the table.
648 this->Header.sh_size = 12;
649}
650
651// We have to get PC values of FDEs. They depend on relocations
652// which are target specific, so we run this code after performing
653// all relocations. We read the values from ouput buffer according to the
654// encoding given for FDEs. Return value is an offset to the initial PC value
655// for the FDE.
656template <class ELFT>
657typename EhFrameHeader<ELFT>::uintX_t
658EhFrameHeader<ELFT>::getFdePc(uintX_t EhVA, const FdeData &F) {
659 const endianness E = ELFT::TargetEndianness;
660 assert((F.Enc & 0xF0) != dwarf::DW_EH_PE_datarel);
661
662 uintX_t FdeOff = EhVA + F.Off + 8;
663 switch (F.Enc & 0xF) {
664 case dwarf::DW_EH_PE_udata2:
665 case dwarf::DW_EH_PE_sdata2:
666 return FdeOff + read16<E>(F.PCRel);
667 case dwarf::DW_EH_PE_udata4:
668 case dwarf::DW_EH_PE_sdata4:
669 return FdeOff + read32<E>(F.PCRel);
670 case dwarf::DW_EH_PE_udata8:
671 case dwarf::DW_EH_PE_sdata8:
672 return FdeOff + read64<E>(F.PCRel);
673 case dwarf::DW_EH_PE_absptr:
674 if (sizeof(uintX_t) == 8)
675 return FdeOff + read64<E>(F.PCRel);
676 return FdeOff + read32<E>(F.PCRel);
677 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000678 fatal("unknown FDE size encoding");
George Rimarf6bc65a2016-01-15 13:34:52 +0000679}
680
681template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
682 const endianness E = ELFT::TargetEndianness;
683
684 const uint8_t Header[] = {1, dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4,
685 dwarf::DW_EH_PE_udata4,
686 dwarf::DW_EH_PE_datarel | dwarf::DW_EH_PE_sdata4};
687 memcpy(Buf, Header, sizeof(Header));
688
689 uintX_t EhVA = Sec->getVA();
690 uintX_t VA = this->getVA();
691 uintX_t EhOff = EhVA - VA - 4;
692 write32<E>(Buf + 4, EhOff);
693 write32<E>(Buf + 8, this->FdeList.size());
694 Buf += 12;
695
696 // InitialPC -> Offset in .eh_frame, sorted by InitialPC.
697 std::map<uintX_t, size_t> PcToOffset;
698 for (const FdeData &F : FdeList)
699 PcToOffset[getFdePc(EhVA, F)] = F.Off;
700
701 for (auto &I : PcToOffset) {
702 // The first four bytes are an offset to the initial PC value for the FDE.
703 write32<E>(Buf, I.first - VA);
704 // The last four bytes are an offset to the FDE data itself.
705 write32<E>(Buf + 4, EhVA + I.second - VA);
706 Buf += 8;
707 }
708}
709
710template <class ELFT>
711void EhFrameHeader<ELFT>::assignEhFrame(EHOutputSection<ELFT> *Sec) {
George Rimar45ca88d2016-01-25 19:27:50 +0000712 assert((!this->Sec || this->Sec == Sec) &&
713 "multiple .eh_frame sections not supported for .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000714 Live = Config->EhFrameHdr;
715 this->Sec = Sec;
716}
717
718template <class ELFT>
719void EhFrameHeader<ELFT>::addFde(uint8_t Enc, size_t Off, uint8_t *PCRel) {
720 if (Live && (Enc & 0xF0) == dwarf::DW_EH_PE_datarel)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000721 fatal("DW_EH_PE_datarel encoding unsupported for FDEs by .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000722 FdeList.push_back(FdeData{Enc, Off, PCRel});
723}
724
725template <class ELFT> void EhFrameHeader<ELFT>::reserveFde() {
726 // Each FDE entry is 8 bytes long:
727 // The first four bytes are an offset to the initial PC value for the FDE. The
728 // last four byte are an offset to the FDE data itself.
729 this->Header.sh_size += 8;
730}
731
732template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000733OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type,
734 uintX_t Flags)
735 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000736
737template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000738void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
739 auto *S = cast<InputSection<ELFT>>(C);
740 Sections.push_back(S);
741 S->OutSec = this;
742 uint32_t Align = S->getAlign();
Rui Ueyama65d98ea2016-01-29 20:31:05 +0000743 this->updateAlign(Align);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000744
745 uintX_t Off = this->Header.sh_size;
Rui Ueyama489a8062016-01-14 20:53:50 +0000746 Off = alignTo(Off, Align);
Rui Ueyama40845e62015-12-26 05:51:07 +0000747 S->OutSecOff = Off;
748 Off += S->getSize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000749 this->Header.sh_size = Off;
750}
751
Rui Ueyama34f29242015-10-13 19:51:57 +0000752// Returns a VA which a relocatin RI refers to. Used only for local symbols.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000753// For non-local symbols, use SymbolBody::getVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000754template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000755typename ELFFile<ELFT>::uintX_t
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000756elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
757 const Elf_Rel_Impl<ELFT, IsRela> &RI,
758 typename ELFFile<ELFT>::uintX_t Addend) {
Rafael Espindola932efcf2015-10-19 20:24:44 +0000759 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
760 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
761
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000762 // PPC64 has a special relocation representing the TOC base pointer
763 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000764 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000765 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000766
Rui Ueyama126d08f2015-10-12 20:28:22 +0000767 const Elf_Sym *Sym =
768 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000769
Rui Ueyama126d08f2015-10-12 20:28:22 +0000770 if (!Sym)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000771 fatal("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000772
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000773 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
774
775 if (Sym->getType() == STT_TLS)
776 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
George Rimarcc06a6f2015-11-29 14:14:20 +0000777 Out<ELFT>::TlsPhdr->p_vaddr;
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000778
Rafael Espindola444576d2015-10-09 19:25:07 +0000779 // According to the ELF spec reference to a local symbol from outside
780 // the group are not allowed. Unfortunately .eh_frame breaks that rule
781 // and must be treated specially. For now we just replace the symbol with
782 // 0.
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000783 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000784 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000785
Rafael Espindolac159c962015-10-19 21:00:02 +0000786 uintX_t Offset = Sym->st_value;
787 if (Sym->getType() == STT_SECTION) {
788 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000789 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000790 }
Rafael Espindola38a36c42016-02-03 16:53:39 +0000791 return Section->OutSec->getVA() + Section->getOffset(Offset) + Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000792}
793
Rui Ueyama34f29242015-10-13 19:51:57 +0000794// Returns true if a symbol can be replaced at load-time by a symbol
795// with the same name defined in other ELF executable or DSO.
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000796bool elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000797 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000798 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000799 if (Body->isShared())
800 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000801
802 if (Body->isUndefined()) {
803 if (!Body->isWeak())
804 return true;
805
806 // This is an horrible corner case. Ideally we would like to say that any
807 // undefined symbol can be preempted so that the dynamic linker has a
808 // chance of finding it at runtime.
809 //
810 // The problem is that the code sequence used to test for weak undef
811 // functions looks like
812 // if (func) func()
813 // If the code is -fPIC the first reference is a load from the got and
814 // everything works.
815 // If the code is not -fPIC there is no reasonable way to solve it:
816 // * A relocation writing to the text segment will fail (it is ro).
817 // * A copy relocation doesn't work for functions.
818 // * The trick of using a plt entry as the address would fail here since
819 // the plt entry would have a non zero address.
820 // Since we cannot do anything better, we just resolve the symbol to 0 and
821 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000822 //
823 // As an extra hack, assume that if we are producing a shared library the
824 // user knows what he or she is doing and can handle a dynamic relocation.
825 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000826 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000827 if (!Config->Shared)
828 return false;
George Rimar5c36e592016-02-02 09:28:53 +0000829 if (Body->getVisibility() != STV_DEFAULT)
830 return false;
831 if (Config->Bsymbolic || (Config->BsymbolicFunctions && Body->isFunc()))
832 return false;
833 return true;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000834}
835
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000836template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000837 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000838 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000839}
840
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000841template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000842EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t Type,
843 uintX_t Flags)
George Rimarf6bc65a2016-01-15 13:34:52 +0000844 : OutputSectionBase<ELFT>(Name, Type, Flags) {
845 Out<ELFT>::EhFrameHdr->assignEhFrame(this);
846}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000847
848template <class ELFT>
849EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
850 : S(S), Index(Index) {}
851
852template <class ELFT> StringRef EHRegion<ELFT>::data() const {
853 ArrayRef<uint8_t> SecData = S->getSectionData();
854 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
855 size_t Start = Offsets[Index].first;
856 size_t End =
857 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
858 return StringRef((const char *)SecData.data() + Start, End - Start);
859}
860
861template <class ELFT>
862Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
863 : EHRegion<ELFT>(S, Index) {}
864
George Rimarf6bc65a2016-01-15 13:34:52 +0000865// Read a byte and advance D by one byte.
866static uint8_t readByte(ArrayRef<uint8_t> &D) {
867 if (D.empty())
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000868 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000869 uint8_t B = D.front();
870 D = D.slice(1);
871 return B;
872}
873
874static void skipLeb128(ArrayRef<uint8_t> &D) {
875 while (!D.empty()) {
876 uint8_t Val = D.front();
877 D = D.slice(1);
878 if ((Val & 0x80) == 0)
879 return;
880 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000881 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000882}
883
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000884template <class ELFT> static size_t getAugPSize(unsigned Enc) {
885 switch (Enc & 0x0f) {
886 case dwarf::DW_EH_PE_absptr:
887 case dwarf::DW_EH_PE_signed:
888 return ELFT::Is64Bits ? 8 : 4;
889 case dwarf::DW_EH_PE_udata2:
890 case dwarf::DW_EH_PE_sdata2:
891 return 2;
892 case dwarf::DW_EH_PE_udata4:
893 case dwarf::DW_EH_PE_sdata4:
894 return 4;
895 case dwarf::DW_EH_PE_udata8:
896 case dwarf::DW_EH_PE_sdata8:
897 return 8;
898 }
899 fatal("unknown FDE encoding");
900}
901
902template <class ELFT> static void skipAugP(ArrayRef<uint8_t> &D) {
903 uint8_t Enc = readByte(D);
904 if ((Enc & 0xf0) == dwarf::DW_EH_PE_aligned)
905 fatal("DW_EH_PE_aligned encoding is not supported");
906 size_t Size = getAugPSize<ELFT>(Enc);
907 if (Size < D.size())
908 fatal("corrupted CIE");
909 D = D.slice(Size);
910}
911
George Rimarf6bc65a2016-01-15 13:34:52 +0000912template <class ELFT>
913uint8_t EHOutputSection<ELFT>::getFdeEncoding(ArrayRef<uint8_t> D) {
Rui Ueyamabe748c22016-02-08 05:18:44 +0000914 if (D.size() < 8)
915 fatal("CIE too small");
George Rimarf6bc65a2016-01-15 13:34:52 +0000916 D = D.slice(8);
917
918 uint8_t Version = readByte(D);
919 if (Version != 1 && Version != 3)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000920 fatal("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
George Rimarf6bc65a2016-01-15 13:34:52 +0000921
922 auto AugEnd = std::find(D.begin() + 1, D.end(), '\0');
Rui Ueyamabe748c22016-02-08 05:18:44 +0000923 if (AugEnd == D.end())
924 fatal("corrupted CIE");
925 StringRef Aug((char *)D.begin(), AugEnd - D.begin());
926 D = D.slice(Aug.size() + 1);
George Rimarf6bc65a2016-01-15 13:34:52 +0000927
928 // Code alignment factor should always be 1 for .eh_frame.
929 if (readByte(D) != 1)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000930 fatal("CIE code alignment must be 1");
Rui Ueyamabe748c22016-02-08 05:18:44 +0000931
932 // Skip data alignment factor.
George Rimarf6bc65a2016-01-15 13:34:52 +0000933 skipLeb128(D);
934
935 // Skip the return address register. In CIE version 1 this is a single
936 // byte. In CIE version 3 this is an unsigned LEB128.
937 if (Version == 1)
938 readByte(D);
939 else
940 skipLeb128(D);
941
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000942 // We only care about an 'R' value, but other records may precede an 'R'
943 // record. Records are not in TLV (type-length-value) format, so we need
944 // to teach the linker how to skip records for each type.
945 for (; !Aug.empty(); Aug = Aug.substr(1)) {
946 switch (Aug[0]) {
947 case 'z':
948 skipLeb128(D);
949 break;
950 case 'R':
951 return readByte(D);
952 case 'P':
953 skipAugP<ELFT>(D);
954 break;
955 case 'L':
956 break;
957 default:
958 fatal("unknown .eh_frame augmentation string: " + Aug);
959 }
960 }
961 return dwarf::DW_EH_PE_absptr;
George Rimarf6bc65a2016-01-15 13:34:52 +0000962}
963
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000964template <class ELFT>
Rui Ueyamac0c92602016-02-05 22:56:03 +0000965static typename ELFFile<ELFT>::uintX_t readEntryLength(ArrayRef<uint8_t> D) {
966 const endianness E = ELFT::TargetEndianness;
Rui Ueyamac0c92602016-02-05 22:56:03 +0000967 if (D.size() < 4)
Rui Ueyama1b45cca2016-02-05 23:24:05 +0000968 fatal("CIE/FDE too small");
969
970 // First 4 bytes of CIE/FDE is the size of the record.
971 // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead.
972 uint64_t V = read32<E>(D.data());
973 if (V < UINT32_MAX) {
974 uint64_t Len = V + 4;
975 if (Len > D.size())
Rui Ueyamac0c92602016-02-05 22:56:03 +0000976 fatal("CIE/FIE ends past the end of the section");
Rui Ueyama1b45cca2016-02-05 23:24:05 +0000977 return Len;
Rui Ueyamac0c92602016-02-05 22:56:03 +0000978 }
979
980 if (D.size() < 12)
Rui Ueyama1b45cca2016-02-05 23:24:05 +0000981 fatal("CIE/FDE too small");
982 V = read64<E>(D.data() + 4);
983 uint64_t Len = V + 12;
984 if (Len < V || D.size() < Len)
Rui Ueyamac0c92602016-02-05 22:56:03 +0000985 fatal("CIE/FIE ends past the end of the section");
Rui Ueyama1b45cca2016-02-05 23:24:05 +0000986 return Len;
Rui Ueyamac0c92602016-02-05 22:56:03 +0000987}
988
989template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000990template <bool IsRela>
991void EHOutputSection<ELFT>::addSectionAux(
992 EHInputSection<ELFT> *S,
993 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
994 const endianness E = ELFT::TargetEndianness;
995
996 S->OutSec = this;
Rui Ueyama65d98ea2016-01-29 20:31:05 +0000997 this->updateAlign(S->getAlign());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000998 Sections.push_back(S);
999
1000 ArrayRef<uint8_t> SecData = S->getSectionData();
1001 ArrayRef<uint8_t> D = SecData;
1002 uintX_t Offset = 0;
1003 auto RelI = Rels.begin();
1004 auto RelE = Rels.end();
1005
George Rimar147747a2016-01-02 16:55:01 +00001006 DenseMap<unsigned, unsigned> OffsetToIndex;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001007 while (!D.empty()) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001008 unsigned Index = S->Offsets.size();
1009 S->Offsets.push_back(std::make_pair(Offset, -1));
1010
Rui Ueyamac0c92602016-02-05 22:56:03 +00001011 uintX_t Length = readEntryLength<ELFT>(D);
George Rimarf6bc65a2016-01-15 13:34:52 +00001012 // If CIE/FDE data length is zero then Length is 4, this
1013 // shall be considered a terminator and processing shall end.
1014 if (Length == 4)
1015 break;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001016 StringRef Entry((const char *)D.data(), Length);
1017
1018 while (RelI != RelE && RelI->r_offset < Offset)
1019 ++RelI;
1020 uintX_t NextOffset = Offset + Length;
1021 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
1022
1023 uint32_t ID = read32<E>(D.data() + 4);
1024 if (ID == 0) {
1025 // CIE
1026 Cie<ELFT> C(S, Index);
George Rimarf6bc65a2016-01-15 13:34:52 +00001027 if (Config->EhFrameHdr)
1028 C.FdeEncoding = getFdeEncoding(D);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001029
1030 StringRef Personality;
1031 if (HasReloc) {
1032 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
1033 SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl();
1034 Personality = Body.getName();
1035 }
1036
1037 std::pair<StringRef, StringRef> CieInfo(Entry, Personality);
1038 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
George Rimar147747a2016-01-02 16:55:01 +00001039 if (P.second) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001040 Cies.push_back(C);
Rui Ueyama489a8062016-01-14 20:53:50 +00001041 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
George Rimar147747a2016-01-02 16:55:01 +00001042 }
1043 OffsetToIndex[Offset] = P.first->second;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001044 } else {
1045 if (!HasReloc)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001046 fatal("FDE doesn't reference another section");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001047 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
1048 if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) {
1049 uint32_t CieOffset = Offset + 4 - ID;
George Rimar147747a2016-01-02 16:55:01 +00001050 auto I = OffsetToIndex.find(CieOffset);
1051 if (I == OffsetToIndex.end())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001052 fatal("Invalid CIE reference");
George Rimar147747a2016-01-02 16:55:01 +00001053 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
George Rimarf6bc65a2016-01-15 13:34:52 +00001054 Out<ELFT>::EhFrameHdr->reserveFde();
Rui Ueyama489a8062016-01-14 20:53:50 +00001055 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001056 }
1057 }
1058
1059 Offset = NextOffset;
1060 D = D.slice(Length);
1061 }
1062}
1063
1064template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001065void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1066 auto *S = cast<EHInputSection<ELFT>>(C);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001067 const Elf_Shdr *RelSec = S->RelocSection;
Rui Ueyama0de86c12016-01-27 22:23:44 +00001068 if (!RelSec) {
1069 addSectionAux(S, make_range<const Elf_Rela *>(nullptr, nullptr));
1070 return;
1071 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001072 ELFFile<ELFT> &Obj = S->getFile()->getObj();
1073 if (RelSec->sh_type == SHT_RELA)
Rui Ueyama0de86c12016-01-27 22:23:44 +00001074 addSectionAux(S, Obj.relas(RelSec));
1075 else
1076 addSectionAux(S, Obj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001077}
1078
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001079template <class ELFT>
1080static typename ELFFile<ELFT>::uintX_t writeAlignedCieOrFde(StringRef Data,
1081 uint8_t *Buf) {
1082 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1083 const endianness E = ELFT::TargetEndianness;
Rui Ueyama489a8062016-01-14 20:53:50 +00001084 uint64_t Len = alignTo(Data.size(), sizeof(uintX_t));
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001085 write32<E>(Buf, Len - 4);
1086 memcpy(Buf + 4, Data.data() + 4, Data.size() - 4);
1087 return Len;
1088}
1089
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001090template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1091 const endianness E = ELFT::TargetEndianness;
1092 size_t Offset = 0;
1093 for (const Cie<ELFT> &C : Cies) {
1094 size_t CieOffset = Offset;
1095
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001096 uintX_t CIELen = writeAlignedCieOrFde<ELFT>(C.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001097 C.S->Offsets[C.Index].second = Offset;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001098 Offset += CIELen;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001099
1100 for (const EHRegion<ELFT> &F : C.Fdes) {
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001101 uintX_t Len = writeAlignedCieOrFde<ELFT>(F.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001102 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001103 F.S->Offsets[F.Index].second = Offset;
George Rimarf6bc65a2016-01-15 13:34:52 +00001104 Out<ELFT>::EhFrameHdr->addFde(C.FdeEncoding, Offset, Buf + Offset + 8);
George Rimare72beba2015-12-21 09:38:59 +00001105 Offset += Len;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001106 }
1107 }
1108
1109 for (EHInputSection<ELFT> *S : Sections) {
1110 const Elf_Shdr *RelSec = S->RelocSection;
1111 if (!RelSec)
1112 continue;
1113 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1114 if (RelSec->sh_type == SHT_RELA)
1115 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1116 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001117 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001118 }
1119}
1120
1121template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001122MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
1123 uintX_t Flags)
1124 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001125
1126template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001127 if (shouldTailMerge()) {
1128 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001129 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001130 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001131 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001132 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1133 StringRef Data = P.first;
1134 memcpy(Buf + P.second, Data.data(), Data.size());
1135 }
1136}
1137
1138static size_t findNull(StringRef S, size_t EntSize) {
1139 // Optimize the common case.
1140 if (EntSize == 1)
1141 return S.find(0);
1142
1143 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1144 const char *B = S.begin() + I;
1145 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1146 return I;
1147 }
1148 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001149}
1150
1151template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001152void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1153 auto *S = cast<MergeInputSection<ELFT>>(C);
Rafael Espindolac159c962015-10-19 21:00:02 +00001154 S->OutSec = this;
Rui Ueyama65d98ea2016-01-29 20:31:05 +00001155 this->updateAlign(S->getAlign());
Rafael Espindolac159c962015-10-19 21:00:02 +00001156
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001157 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001158 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001159 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001160
Rui Ueyamaead75fc2016-01-29 22:18:55 +00001161 // If this is of type string, the contents are null-terminated strings.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001162 if (this->Header.sh_flags & SHF_STRINGS) {
Rui Ueyamaad87ff72016-01-06 02:52:24 +00001163 uintX_t Offset = 0;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001164 while (!Data.empty()) {
1165 size_t End = findNull(Data, EntSize);
1166 if (End == StringRef::npos)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001167 fatal("String is not null terminated");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001168 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001169 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001170 if (shouldTailMerge())
1171 OutputOffset = -1;
1172 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1173 uintX_t Size = End + EntSize;
1174 Data = Data.substr(Size);
1175 Offset += Size;
1176 }
Rui Ueyamaead75fc2016-01-29 22:18:55 +00001177 return;
1178 }
1179
1180 // If this is not of type string, every entry has the same size.
1181 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1182 StringRef Entry = Data.substr(I, EntSize);
1183 size_t OutputOffset = Builder.add(Entry);
1184 S->Offsets.push_back(std::make_pair(I, OutputOffset));
Rafael Espindolac159c962015-10-19 21:00:02 +00001185 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001186}
1187
1188template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001189unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1190 return Builder.getOffset(Val);
1191}
1192
1193template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1194 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1195}
1196
1197template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1198 if (shouldTailMerge())
1199 Builder.finalize();
1200 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001201}
1202
1203template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001204StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001205 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1206 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001207 Dynamic(Dynamic) {
1208 this->Header.sh_addralign = 1;
1209}
1210
Rafael Espindolae2c24612016-01-29 01:24:25 +00001211// Adds a string to the string table. If HashIt is true we hash and check for
1212// duplicates. It is optional because the name of global symbols are already
1213// uniqued and hashing them again has a big cost for a small value: uniquing
1214// them with some other string that happens to be the same.
1215template <class ELFT>
1216unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1217 if (HashIt) {
1218 auto R = StringMap.insert(std::make_pair(S, Size));
1219 if (!R.second)
1220 return R.first->second;
1221 }
1222 unsigned Ret = Size;
1223 Size += S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001224 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001225 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001226}
1227
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001228template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001229 // ELF string tables start with NUL byte, so advance the pointer by one.
1230 ++Buf;
1231 for (StringRef S : Strings) {
1232 memcpy(Buf, S.data(), S.size());
1233 Buf += S.size() + 1;
1234 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001235}
1236
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001237template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001238SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001239 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001240 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1241 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001242 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001243 StrTabSec(StrTabSec), Table(Table) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001244 this->Header.sh_entsize = sizeof(Elf_Sym);
Rui Ueyama5e378dd2016-01-29 22:18:57 +00001245 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +00001246}
1247
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001248// Orders symbols according to their positions in the GOT,
1249// in compliance with MIPS ABI rules.
1250// See "Global Offset Table" in Chapter 5 in the following document
1251// for detailed description:
1252// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rafael Espindolae2c24612016-01-29 01:24:25 +00001253static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
1254 const std::pair<SymbolBody *, unsigned> &R) {
1255 if (!L.first->isInGot() || !R.first->isInGot())
1256 return R.first->isInGot();
1257 return L.first->GotIndex < R.first->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001258}
1259
Rui Ueyama0db335f2015-10-07 16:58:54 +00001260template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001261 if (this->Header.sh_size)
1262 return; // Already finalized.
1263
Rui Ueyama0db335f2015-10-07 16:58:54 +00001264 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001265 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001266 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001267
1268 if (!StrTabSec.isDynamic()) {
1269 std::stable_sort(Symbols.begin(), Symbols.end(),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001270 [](const std::pair<SymbolBody *, unsigned> &L,
1271 const std::pair<SymbolBody *, unsigned> &R) {
1272 return getSymbolBinding(L.first) == STB_LOCAL &&
1273 getSymbolBinding(R.first) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001274 });
1275 return;
1276 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001277 if (Out<ELFT>::GnuHashTab)
1278 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1279 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001280 else if (Config->EMachine == EM_MIPS)
1281 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001282 size_t I = 0;
Rafael Espindolae2c24612016-01-29 01:24:25 +00001283 for (const std::pair<SymbolBody *, unsigned> &P : Symbols)
Rui Ueyama572a6f72016-01-29 01:49:33 +00001284 P.first->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001285}
1286
1287template <class ELFT>
1288void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001289 Symbols.push_back(
1290 std::make_pair(Body, StrTabSec.addString(Body->getName(), false)));
Rui Ueyama0db335f2015-10-07 16:58:54 +00001291}
1292
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001293template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001294 Buf += sizeof(Elf_Sym);
1295
1296 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1297 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001298 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1299 writeLocalSymbols(Buf);
1300
1301 writeGlobalSymbols(Buf);
1302}
1303
1304template <class ELFT>
1305void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1306 // Iterate over all input object files to copy their local symbols
1307 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001308 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001309 for (const std::pair<const Elf_Sym *, unsigned> &P : File->KeptLocalSyms) {
1310 const Elf_Sym *Sym = P.first;
Rafael Espindola444576d2015-10-09 19:25:07 +00001311
Rui Ueyamac55733e2015-09-30 00:54:29 +00001312 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001313 uintX_t VA = 0;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001314 if (Sym->st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001315 ESym->st_shndx = SHN_ABS;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001316 VA = Sym->st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001317 } else {
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001318 InputSectionBase<ELFT> *Section = File->getSection(*Sym);
Rafael Espindolac159c962015-10-19 21:00:02 +00001319 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1320 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001321 VA = Section->getOffset(*Sym);
Tom Stellard80efb162016-01-07 03:59:08 +00001322 // Symbol offsets for AMDGPU need to be the offset in bytes of the
1323 // symbol from the beginning of the section.
1324 if (Config->EMachine != EM_AMDGPU)
1325 VA += OutSec->getVA();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001326 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001327 ESym->st_name = P.second;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001328 ESym->st_size = Sym->st_size;
1329 ESym->setBindingAndType(Sym->getBinding(), Sym->getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001330 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001331 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001332 }
1333 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001334}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001335
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001336template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001337static const typename llvm::object::ELFFile<ELFT>::Elf_Sym *
1338getElfSym(SymbolBody &Body) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001339 if (auto *EBody = dyn_cast<DefinedElf<ELFT>>(&Body))
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001340 return &EBody->Sym;
1341 if (auto *EBody = dyn_cast<UndefinedElf<ELFT>>(&Body))
1342 return &EBody->Sym;
1343 return nullptr;
1344}
1345
1346template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001347void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001348 // Write the internal symbol table contents to the output symbol table
1349 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001350 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001351 for (const std::pair<SymbolBody *, unsigned> &P : Symbols) {
1352 SymbolBody *Body = P.first;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001353 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001354
Rafael Espindola8614c562015-10-06 14:33:58 +00001355 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001356 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001357 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001358 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001359 case SymbolBody::DefinedRegularKind: {
1360 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001361 if (InputSectionBase<ELFT> *Sec = Sym->Section) {
1362 if (!Sec->isLive())
1363 continue;
1364 OutSec = Sec->OutSec;
1365 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001366 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001367 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001368 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001369 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001370 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001371 case SymbolBody::SharedKind: {
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001372 if (cast<SharedSymbol<ELFT>>(Body)->needsCopy())
George Rimarbc590fe2015-10-28 16:48:58 +00001373 OutSec = Out<ELFT>::Bss;
1374 break;
1375 }
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001376 case SymbolBody::UndefinedElfKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001377 case SymbolBody::UndefinedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001378 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001379 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001380 }
1381
Rafael Espindolae2c24612016-01-29 01:24:25 +00001382 ESym->st_name = P.second;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001383
Rafael Espindola8614c562015-10-06 14:33:58 +00001384 unsigned char Type = STT_NOTYPE;
1385 uintX_t Size = 0;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001386 if (const Elf_Sym *InputSym = getElfSym<ELFT>(*Body)) {
1387 Type = InputSym->getType();
1388 Size = InputSym->st_size;
Rafael Espindola11191912015-12-24 16:23:37 +00001389 } else if (auto *C = dyn_cast<DefinedCommon>(Body)) {
1390 Type = STT_OBJECT;
1391 Size = C->Size;
Rafael Espindola8614c562015-10-06 14:33:58 +00001392 }
1393
Igor Kudrin853b88d2015-10-20 20:52:14 +00001394 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001395 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001396 ESym->setVisibility(Body->getVisibility());
Rui Ueyamab5a69702016-02-01 21:00:35 +00001397 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001398
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001399 if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001400 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001401 else if (isa<DefinedRegular<ELFT>>(Body))
1402 ESym->st_shndx = SHN_ABS;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001403
1404 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001405 }
1406}
1407
Igor Kudrin853b88d2015-10-20 20:52:14 +00001408template <class ELFT>
1409uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001410 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001411 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1412 return STB_LOCAL;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001413 if (const Elf_Sym *ESym = getElfSym<ELFT>(*Body))
1414 return ESym->getBinding();
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001415 if (isa<DefinedSynthetic<ELFT>>(Body))
1416 return STB_LOCAL;
Igor Kudrin853b88d2015-10-20 20:52:14 +00001417 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1418}
1419
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001420template <class ELFT>
1421MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1422 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1423 this->Header.sh_addralign = 4;
1424 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1425 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1426}
1427
1428template <class ELFT>
1429void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1430 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
1431 R->ri_gp_value = getMipsGpAddr<ELFT>();
Rui Ueyama70eed362016-01-06 22:42:43 +00001432 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001433}
1434
1435template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001436void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001437 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001438 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001439 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001440}
1441
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001442namespace lld {
1443namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001444template class OutputSectionBase<ELF32LE>;
1445template class OutputSectionBase<ELF32BE>;
1446template class OutputSectionBase<ELF64LE>;
1447template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001448
George Rimarf6bc65a2016-01-15 13:34:52 +00001449template class EhFrameHeader<ELF32LE>;
1450template class EhFrameHeader<ELF32BE>;
1451template class EhFrameHeader<ELF64LE>;
1452template class EhFrameHeader<ELF64BE>;
1453
George Rimar648a2c32015-10-20 08:54:27 +00001454template class GotPltSection<ELF32LE>;
1455template class GotPltSection<ELF32BE>;
1456template class GotPltSection<ELF64LE>;
1457template class GotPltSection<ELF64BE>;
1458
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001459template class GotSection<ELF32LE>;
1460template class GotSection<ELF32BE>;
1461template class GotSection<ELF64LE>;
1462template class GotSection<ELF64BE>;
1463
1464template class PltSection<ELF32LE>;
1465template class PltSection<ELF32BE>;
1466template class PltSection<ELF64LE>;
1467template class PltSection<ELF64BE>;
1468
1469template class RelocationSection<ELF32LE>;
1470template class RelocationSection<ELF32BE>;
1471template class RelocationSection<ELF64LE>;
1472template class RelocationSection<ELF64BE>;
1473
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001474template class InterpSection<ELF32LE>;
1475template class InterpSection<ELF32BE>;
1476template class InterpSection<ELF64LE>;
1477template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001478
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001479template class GnuHashTableSection<ELF32LE>;
1480template class GnuHashTableSection<ELF32BE>;
1481template class GnuHashTableSection<ELF64LE>;
1482template class GnuHashTableSection<ELF64BE>;
1483
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001484template class HashTableSection<ELF32LE>;
1485template class HashTableSection<ELF32BE>;
1486template class HashTableSection<ELF64LE>;
1487template class HashTableSection<ELF64BE>;
1488
1489template class DynamicSection<ELF32LE>;
1490template class DynamicSection<ELF32BE>;
1491template class DynamicSection<ELF64LE>;
1492template class DynamicSection<ELF64BE>;
1493
1494template class OutputSection<ELF32LE>;
1495template class OutputSection<ELF32BE>;
1496template class OutputSection<ELF64LE>;
1497template class OutputSection<ELF64BE>;
1498
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001499template class EHOutputSection<ELF32LE>;
1500template class EHOutputSection<ELF32BE>;
1501template class EHOutputSection<ELF64LE>;
1502template class EHOutputSection<ELF64BE>;
1503
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001504template class MipsReginfoOutputSection<ELF32LE>;
1505template class MipsReginfoOutputSection<ELF32BE>;
1506template class MipsReginfoOutputSection<ELF64LE>;
1507template class MipsReginfoOutputSection<ELF64BE>;
1508
Rafael Espindolac159c962015-10-19 21:00:02 +00001509template class MergeOutputSection<ELF32LE>;
1510template class MergeOutputSection<ELF32BE>;
1511template class MergeOutputSection<ELF64LE>;
1512template class MergeOutputSection<ELF64BE>;
1513
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001514template class StringTableSection<ELF32LE>;
1515template class StringTableSection<ELF32BE>;
1516template class StringTableSection<ELF64LE>;
1517template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001518
1519template class SymbolTableSection<ELF32LE>;
1520template class SymbolTableSection<ELF32BE>;
1521template class SymbolTableSection<ELF64LE>;
1522template class SymbolTableSection<ELF64BE>;
1523
Rui Ueyama5ec41f32016-01-26 01:32:00 +00001524template uint32_t getLocalRelTarget(const ObjectFile<ELF32LE> &,
1525 const ELFFile<ELF32LE>::Elf_Rel &,
1526 uint32_t);
1527template uint32_t getLocalRelTarget(const ObjectFile<ELF32BE> &,
1528 const ELFFile<ELF32BE>::Elf_Rel &,
1529 uint32_t);
1530template uint64_t getLocalRelTarget(const ObjectFile<ELF64LE> &,
1531 const ELFFile<ELF64LE>::Elf_Rel &,
1532 uint64_t);
1533template uint64_t getLocalRelTarget(const ObjectFile<ELF64BE> &,
1534 const ELFFile<ELF64BE>::Elf_Rel &,
1535 uint64_t);
1536template uint32_t getLocalRelTarget(const ObjectFile<ELF32LE> &,
1537 const ELFFile<ELF32LE>::Elf_Rela &,
1538 uint32_t);
1539template uint32_t getLocalRelTarget(const ObjectFile<ELF32BE> &,
1540 const ELFFile<ELF32BE>::Elf_Rela &,
1541 uint32_t);
1542template uint64_t getLocalRelTarget(const ObjectFile<ELF64LE> &,
1543 const ELFFile<ELF64LE>::Elf_Rela &,
1544 uint64_t);
1545template uint64_t getLocalRelTarget(const ObjectFile<ELF64BE> &,
1546 const ELFFile<ELF64BE>::Elf_Rela &,
1547 uint64_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001548}
1549}