blob: f1189e691dfee304f18b4e63f17f7983498817b5 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.cpp -------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "OutputSections.h"
11#include "Config.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000012#include "SymbolTable.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000013#include "Target.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000014#include "llvm/Support/Dwarf.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000015#include "llvm/Support/MathExtras.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000016#include <map>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000017
Rafael Espindola5805c4f2015-09-21 21:38:08 +000018using namespace llvm;
19using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000020using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021using namespace llvm::ELF;
22
23using namespace lld;
24using namespace lld::elf2;
25
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000026template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +000027OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t Type,
28 uintX_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029 : Name(Name) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000030 memset(&Header, 0, sizeof(Elf_Shdr));
Rui Ueyamad97e5c42016-01-07 18:33:11 +000031 Header.sh_type = Type;
32 Header.sh_flags = Flags;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000033}
34
Rafael Espindola35c6af32015-09-25 17:19:10 +000035template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000036GotPltSection<ELFT>::GotPltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000037 : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
George Rimar648a2c32015-10-20 08:54:27 +000038 this->Header.sh_addralign = sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000039}
40
41template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama724d6252016-01-29 01:49:32 +000042 Sym->GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
George Rimar648a2c32015-10-20 08:54:27 +000043 Entries.push_back(Sym);
44}
45
46template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +000047 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +000048}
49
George Rimar648a2c32015-10-20 08:54:27 +000050template <class ELFT> void GotPltSection<ELFT>::finalize() {
Igor Kudrin351b41d2015-11-16 17:44:08 +000051 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +000052 (Target->GotPltHeaderEntriesNum + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000053}
54
55template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +000056 Target->writeGotPltHeader(Buf);
Rui Ueyama724d6252016-01-29 01:49:32 +000057 Buf += Target->GotPltHeaderEntriesNum * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000058 for (const SymbolBody *B : Entries) {
Rui Ueyamab5a69702016-02-01 21:00:35 +000059 Target->writeGotPlt(Buf, B->getPltVA<ELFT>());
George Rimar648a2c32015-10-20 08:54:27 +000060 Buf += sizeof(uintX_t);
61 }
62}
63
64template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000065GotSection<ELFT>::GotSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000066 : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000067 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +000068 this->Header.sh_flags |= SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000069 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000070}
71
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000073 Sym->GotIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074 Entries.push_back(Sym);
George Rimar687138c2015-11-13 16:28:53 +000075}
76
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000077template <class ELFT> void GotSection<ELFT>::addMipsLocalEntry() {
78 ++MipsLocalEntries;
79}
80
George Rimar90cd0a82015-12-01 19:20:26 +000081template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) {
82 if (Sym->hasGlobalDynIndex())
83 return false;
Rui Ueyama724d6252016-01-29 01:49:32 +000084 Sym->GlobalDynIndex = Target->GotHeaderEntriesNum + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +000085 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +000086 Entries.push_back(Sym);
87 Entries.push_back(nullptr);
George Rimar90cd0a82015-12-01 19:20:26 +000088 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000089}
90
George Rimar95c1a582015-12-07 08:02:20 +000091template <class ELFT> bool GotSection<ELFT>::addCurrentModuleTlsIndex() {
George Rimarb17f7392015-12-01 18:24:07 +000092 if (LocalTlsIndexOff != uint32_t(-1))
93 return false;
Michael J. Spencer1e225612015-11-11 01:00:24 +000094 Entries.push_back(nullptr);
95 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +000096 LocalTlsIndexOff = (Entries.size() - 2) * sizeof(uintX_t);
97 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +000098}
99
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000100template <class ELFT>
101typename GotSection<ELFT>::uintX_t
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000102GotSection<ELFT>::getMipsLocalFullAddr(const SymbolBody &B) {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000103 return getMipsLocalEntryAddr(B.getVA<ELFT>());
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000104}
105
106template <class ELFT>
107typename GotSection<ELFT>::uintX_t
108GotSection<ELFT>::getMipsLocalPageAddr(uintX_t EntryValue) {
109 // Initialize the entry by the %hi(EntryValue) expression
110 // but without right-shifting.
111 return getMipsLocalEntryAddr((EntryValue + 0x8000) & ~0xffff);
112}
113
114template <class ELFT>
115typename GotSection<ELFT>::uintX_t
116GotSection<ELFT>::getMipsLocalEntryAddr(uintX_t EntryValue) {
Rui Ueyama724d6252016-01-29 01:49:32 +0000117 size_t NewIndex = Target->GotHeaderEntriesNum + MipsLocalGotPos.size();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000118 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
119 assert(!P.second || MipsLocalGotPos.size() <= MipsLocalEntries);
120 return this->getVA() + P.first->second * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000121}
122
Igor Kudrin304860a2015-11-12 04:39:49 +0000123template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000124typename GotSection<ELFT>::uintX_t
125GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
126 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
127}
128
129template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000130const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
131 return Entries.empty() ? nullptr : Entries.front();
132}
133
134template <class ELFT>
135unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
Rui Ueyama724d6252016-01-29 01:49:32 +0000136 return Target->GotHeaderEntriesNum + MipsLocalEntries;
Igor Kudrin304860a2015-11-12 04:39:49 +0000137}
138
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000139template <class ELFT> void GotSection<ELFT>::finalize() {
140 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +0000141 (Target->GotHeaderEntriesNum + MipsLocalEntries + Entries.size()) *
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000142 sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000143}
144
Rafael Espindolaa6627382015-10-06 23:56:53 +0000145template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000146 Target->writeGotHeader(Buf);
Rui Ueyama5cbf5d22016-02-02 02:29:03 +0000147 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000148 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
149 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, L.first);
150 }
Rui Ueyama724d6252016-01-29 01:49:32 +0000151 Buf += Target->GotHeaderEntriesNum * sizeof(uintX_t);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000152 Buf += MipsLocalEntries * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000153 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000154 uint8_t *Entry = Buf;
155 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000156 if (!B)
157 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000158 // MIPS has special rules to fill up GOT entries.
159 // See "Global Offset Table" in Chapter 5 in the following document
160 // for detailed description:
161 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
162 // As the first approach, we can just store addresses for all symbols.
163 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000164 continue; // The dynamic linker will take care of it.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000165 uintX_t VA = B->getVA<ELFT>();
Rafael Espindolae782f672015-10-07 03:56:05 +0000166 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000167 }
168}
169
Rafael Espindola35c6af32015-09-25 17:19:10 +0000170template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000171PltSection<ELFT>::PltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000172 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000173 this->Header.sh_addralign = 16;
174}
175
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000177 size_t Off = 0;
Rui Ueyama9398f862016-01-29 04:15:02 +0000178 if (Target->UseLazyBinding) {
Rui Ueyama74937fc2016-02-02 02:53:58 +0000179 // At beginning of PLT, we have code to call the dynamic linker
180 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama900e2d22016-01-29 03:51:49 +0000181 Target->writePltZero(Buf);
Rui Ueyama62515452016-01-29 03:00:32 +0000182 Off += Target->PltZeroSize;
George Rimar648a2c32015-10-20 08:54:27 +0000183 }
George Rimarfb5d7f22015-11-26 19:58:51 +0000184 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000185 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000186 unsigned RelOff = I.second;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000187 uint64_t Got =
188 Target->UseLazyBinding ? B->getGotPltVA<ELFT>() : B->getGotVA<ELFT>();
Rui Ueyama242ddf42015-10-12 18:56:36 +0000189 uint64_t Plt = this->getVA() + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000190 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000191 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000192 }
193}
194
195template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000196 Sym->PltIndex = Entries.size();
Rui Ueyama724d6252016-01-29 01:49:32 +0000197 unsigned RelOff = Target->UseLazyBinding
George Rimar77b77792015-11-25 22:15:01 +0000198 ? Out<ELFT>::RelaPlt->getRelocOffset()
199 : Out<ELFT>::RelaDyn->getRelocOffset();
200 Entries.push_back(std::make_pair(Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000201}
202
George Rimar648a2c32015-10-20 08:54:27 +0000203template <class ELFT> void PltSection<ELFT>::finalize() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000204 this->Header.sh_size =
Rui Ueyama62515452016-01-29 03:00:32 +0000205 Target->PltZeroSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000206}
207
208template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000209RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000210 : OutputSectionBase<ELFT>(Name, IsRela ? SHT_RELA : SHT_REL, SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000211 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000212 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000213 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000214}
215
George Rimar5828c232015-11-30 17:49:19 +0000216template <class ELFT>
Rafael Espindolade9857e2016-02-04 21:33:05 +0000217static typename ELFFile<ELFT>::uintX_t
218getOffset(const DynamicReloc<ELFT> &Rel) {
219 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
220 SymbolBody *Sym = Rel.Sym;
221 switch (Rel.OKind) {
222 case DynamicReloc<ELFT>::Off_GTlsIndex:
223 return Out<ELFT>::Got->getGlobalDynAddr(*Sym);
224 case DynamicReloc<ELFT>::Off_GTlsOffset:
225 return Out<ELFT>::Got->getGlobalDynAddr(*Sym) + sizeof(uintX_t);
226 case DynamicReloc<ELFT>::Off_LTlsIndex:
227 return Out<ELFT>::Got->getLocalTlsIndexVA();
228 case DynamicReloc<ELFT>::Off_Sec:
229 return Rel.OffsetSec->getOffset(Rel.OffsetInSec) +
230 Rel.OffsetSec->OutSec->getVA();
231 case DynamicReloc<ELFT>::Off_Bss:
232 return cast<SharedSymbol<ELFT>>(Sym)->OffsetInBss + Out<ELFT>::Bss->getVA();
233 case DynamicReloc<ELFT>::Off_Got:
234 return Sym->getGotVA<ELFT>();
235 case DynamicReloc<ELFT>::Off_GotPlt:
236 return Sym->getGotPltVA<ELFT>();
George Rimar5828c232015-11-30 17:49:19 +0000237 }
Rafael Espindolade9857e2016-02-04 21:33:05 +0000238 llvm_unreachable("Invalid offset kind");
George Rimar5828c232015-11-30 17:49:19 +0000239}
240
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000242 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000243 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000244 Buf += IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindolade9857e2016-02-04 21:33:05 +0000245 SymbolBody *Sym = Rel.Sym;
Rui Ueyamab0210e832016-01-26 00:24:57 +0000246
Rafael Espindola9e3f84b2016-02-03 21:02:48 +0000247 if (IsRela) {
Rafael Espindolade9857e2016-02-04 21:33:05 +0000248 uintX_t VA = 0;
249 if (Rel.UseSymVA)
250 VA = Sym->getVA<ELFT>();
251 else if (Rel.TargetSec)
252 VA = Rel.TargetSec->getOffset(Rel.OffsetInTargetSec) +
253 Rel.TargetSec->OutSec->getVA();
254 reinterpret_cast<Elf_Rela *>(P)->r_addend = Rel.Addend + VA;
Rafael Espindola9e3f84b2016-02-03 21:02:48 +0000255 }
256
Rafael Espindolade9857e2016-02-04 21:33:05 +0000257 P->r_offset = getOffset(Rel);
258 uint32_t SymIdx = (!Rel.UseSymVA && Sym) ? Sym->DynsymIndex : 0;
259 P->setSymbolAndType(SymIdx, Rel.Type, Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000260 }
261}
262
George Rimar77b77792015-11-25 22:15:01 +0000263template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
264 const unsigned EntrySize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
265 return EntrySize * Relocs.size();
266}
267
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000268template <class ELFT> void RelocationSection<ELFT>::finalize() {
George Rimara07ff662015-12-21 10:12:06 +0000269 this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex
270 : Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000271 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
272}
273
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000274template <class ELFT>
275InterpSection<ELFT>::InterpSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000276 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000277 this->Header.sh_size = Config->DynamicLinker.size() + 1;
278 this->Header.sh_addralign = 1;
279}
280
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000281template <class ELFT>
282void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
283 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000284}
285
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000286template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000287 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
288}
289
Rafael Espindola35c6af32015-09-25 17:19:10 +0000290template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000291HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000292 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000293 this->Header.sh_entsize = sizeof(Elf_Word);
294 this->Header.sh_addralign = sizeof(Elf_Word);
295}
296
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000297static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000298 uint32_t H = 0;
299 for (char C : Name) {
300 H = (H << 4) + C;
301 uint32_t G = H & 0xf0000000;
302 if (G)
303 H ^= G >> 24;
304 H &= ~G;
305 }
306 return H;
307}
308
Rui Ueyama0db335f2015-10-07 16:58:54 +0000309template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000310 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000311
Rui Ueyama0db335f2015-10-07 16:58:54 +0000312 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000313 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000314
315 // Create as many buckets as there are symbols.
316 // FIXME: This is simplistic. We can try to optimize it, but implementing
317 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000318 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000319 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
320}
321
322template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000323 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000324 auto *P = reinterpret_cast<Elf_Word *>(Buf);
325 *P++ = NumSymbols; // nbucket
326 *P++ = NumSymbols; // nchain
327
328 Elf_Word *Buckets = P;
329 Elf_Word *Chains = P + NumSymbols;
330
Rafael Espindolae2c24612016-01-29 01:24:25 +0000331 for (const std::pair<SymbolBody *, unsigned> &P :
332 Out<ELFT>::DynSymTab->getSymbols()) {
333 SymbolBody *Body = P.first;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000334 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000335 unsigned I = Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000336 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000337 Chains[I] = Buckets[Hash];
338 Buckets[Hash] = I;
339 }
340}
341
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000342static uint32_t hashGnu(StringRef Name) {
343 uint32_t H = 5381;
344 for (uint8_t C : Name)
345 H = (H << 5) + H + C;
346 return H;
347}
348
349template <class ELFT>
350GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000351 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000352 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000353 this->Header.sh_addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000354}
355
356template <class ELFT>
357unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
358 if (!NumHashed)
359 return 0;
360
361 // These values are prime numbers which are not greater than 2^(N-1) + 1.
362 // In result, for any particular NumHashed we return a prime number
363 // which is not greater than NumHashed.
364 static const unsigned Primes[] = {
365 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
366 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
367
368 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
369 array_lengthof(Primes) - 1)];
370}
371
372// Bloom filter estimation: at least 8 bits for each hashed symbol.
373// GNU Hash table requirement: it should be a power of 2,
374// the minimum value is 1, even for an empty table.
375// Expected results for a 32-bit target:
376// calcMaskWords(0..4) = 1
377// calcMaskWords(5..8) = 2
378// calcMaskWords(9..16) = 4
379// For a 64-bit target:
380// calcMaskWords(0..8) = 1
381// calcMaskWords(9..16) = 2
382// calcMaskWords(17..32) = 4
383template <class ELFT>
384unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
385 if (!NumHashed)
386 return 1;
387 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
388}
389
390template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000391 unsigned NumHashed = HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000392 NBuckets = calcNBuckets(NumHashed);
393 MaskWords = calcMaskWords(NumHashed);
394 // Second hash shift estimation: just predefined values.
395 Shift2 = ELFT::Is64Bits ? 6 : 5;
396
397 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
398 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
399 + sizeof(Elf_Off) * MaskWords // Bloom Filter
400 + sizeof(Elf_Word) * NBuckets // Hash Buckets
401 + sizeof(Elf_Word) * NumHashed; // Hash Values
402}
403
404template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
405 writeHeader(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000406 if (HashedSymbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000407 return;
408 writeBloomFilter(Buf);
409 writeHashTable(Buf);
410}
411
412template <class ELFT>
413void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
414 auto *P = reinterpret_cast<Elf_Word *>(Buf);
415 *P++ = NBuckets;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000416 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - HashedSymbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000417 *P++ = MaskWords;
418 *P++ = Shift2;
419 Buf = reinterpret_cast<uint8_t *>(P);
420}
421
422template <class ELFT>
423void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
424 unsigned C = sizeof(Elf_Off) * 8;
425
426 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000427 for (const HashedSymbolData &Item : HashedSymbols) {
428 size_t Pos = (Item.Hash / C) & (MaskWords - 1);
429 uintX_t V = (uintX_t(1) << (Item.Hash % C)) |
430 (uintX_t(1) << ((Item.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000431 Masks[Pos] |= V;
432 }
433 Buf += sizeof(Elf_Off) * MaskWords;
434}
435
436template <class ELFT>
437void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
438 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
439 Elf_Word *Values = Buckets + NBuckets;
440
441 int PrevBucket = -1;
442 int I = 0;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000443 for (const HashedSymbolData &Item : HashedSymbols) {
444 int Bucket = Item.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000445 assert(PrevBucket <= Bucket);
446 if (Bucket != PrevBucket) {
Rui Ueyama572a6f72016-01-29 01:49:33 +0000447 Buckets[Bucket] = Item.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000448 PrevBucket = Bucket;
449 if (I > 0)
450 Values[I - 1] |= 1;
451 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000452 Values[I] = Item.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000453 ++I;
454 }
455 if (I > 0)
456 Values[I - 1] |= 1;
457}
458
Rafael Espindola31f88882015-11-02 14:33:11 +0000459static bool includeInGnuHashTable(SymbolBody *B) {
Rui Ueyamac112c1b2016-01-29 02:17:01 +0000460 // Assume that includeInDynsym() is already checked.
Rafael Espindola31f88882015-11-02 14:33:11 +0000461 return !B->isUndefined();
462}
463
Rafael Espindola35c6af32015-09-25 17:19:10 +0000464template <class ELFT>
Rafael Espindolae2c24612016-01-29 01:24:25 +0000465void GnuHashTableSection<ELFT>::addSymbols(
466 std::vector<std::pair<SymbolBody *, unsigned>> &Symbols) {
467 std::vector<std::pair<SymbolBody *, unsigned>> NotHashed;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000468 NotHashed.reserve(Symbols.size());
469 HashedSymbols.reserve(Symbols.size());
Rafael Espindolae2c24612016-01-29 01:24:25 +0000470 for (const std::pair<SymbolBody *, unsigned> &P : Symbols) {
471 SymbolBody *B = P.first;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000472 if (includeInGnuHashTable(B))
Rafael Espindolae2c24612016-01-29 01:24:25 +0000473 HashedSymbols.push_back(
474 HashedSymbolData{B, P.second, hashGnu(B->getName())});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000475 else
Rafael Espindolae2c24612016-01-29 01:24:25 +0000476 NotHashed.push_back(P);
Igor Kudrinf1d60292015-10-28 07:05:56 +0000477 }
478 if (HashedSymbols.empty())
479 return;
480
481 unsigned NBuckets = calcNBuckets(HashedSymbols.size());
482 std::stable_sort(HashedSymbols.begin(), HashedSymbols.end(),
483 [&](const HashedSymbolData &L, const HashedSymbolData &R) {
484 return L.Hash % NBuckets < R.Hash % NBuckets;
485 });
486
487 Symbols = std::move(NotHashed);
488 for (const HashedSymbolData &Item : HashedSymbols)
Rafael Espindolae2c24612016-01-29 01:24:25 +0000489 Symbols.push_back(std::make_pair(Item.Body, Item.STName));
Igor Kudrinf1d60292015-10-28 07:05:56 +0000490}
491
492template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000493DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000494 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000495 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000496 Elf_Shdr &Header = this->Header;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000497 Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000498 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000499
500 // .dynamic section is not writable on MIPS.
501 // See "Special Section" in Chapter 4 in the following document:
502 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
503 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000504 Header.sh_flags = SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000505}
506
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000507template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000508 if (this->Header.sh_size)
509 return; // Already finalized.
510
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000511 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000512 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000513
Rafael Espindolae2c24612016-01-29 01:24:25 +0000514 auto Add = [=](Entry E) { Entries.push_back(E); };
515
516 // Add strings. We know that these are the last strings to be added to
Rafael Espindolade069362016-01-25 21:32:04 +0000517 // DynStrTab and doing this here allows this function to set DT_STRSZ.
518 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000519 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
520 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rafael Espindolade069362016-01-25 21:32:04 +0000521 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
522 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000523 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
524 if (!Config->SoName.empty())
525 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
526
Rafael Espindolade069362016-01-25 21:32:04 +0000527 Out<ELFT>::DynStrTab->finalize();
528
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000529 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindolade069362016-01-25 21:32:04 +0000530 bool IsRela = Out<ELFT>::RelaDyn->isRela();
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000531 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
532 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
533 Add({IsRela ? DT_RELAENT : DT_RELENT,
534 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000535 }
George Rimar648a2c32015-10-20 08:54:27 +0000536 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000537 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
538 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
539 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
540 Out<ELFT>::GotPlt});
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000541 Add({DT_PLTREL, uint64_t(Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000542 }
543
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000544 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
545 Add({DT_SYMENT, sizeof(Elf_Sym)});
546 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
547 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000548 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000549 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000550 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000551 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000552
Rafael Espindolade069362016-01-25 21:32:04 +0000553 if (PreInitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000554 Add({DT_PREINIT_ARRAY, PreInitArraySec});
555 Add({DT_PREINIT_ARRAYSZ, PreInitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000556 }
557 if (InitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000558 Add({DT_INIT_ARRAY, InitArraySec});
559 Add({DT_INIT_ARRAYSZ, (uintX_t)InitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000560 }
561 if (FiniArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000562 Add({DT_FINI_ARRAY, FiniArraySec});
563 Add({DT_FINI_ARRAYSZ, (uintX_t)FiniArraySec->getSize()});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000564 }
565
Rui Ueyama304d1352016-01-25 21:47:25 +0000566 if (SymbolBody *B = SymTab.find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000567 Add({DT_INIT, B});
Rui Ueyama304d1352016-01-25 21:47:25 +0000568 if (SymbolBody *B = SymTab.find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000569 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000570
Rafael Espindolade069362016-01-25 21:32:04 +0000571 uint32_t DtFlags = 0;
572 uint32_t DtFlags1 = 0;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000573 if (Config->Bsymbolic)
574 DtFlags |= DF_SYMBOLIC;
575 if (Config->ZNodelete)
576 DtFlags1 |= DF_1_NODELETE;
577 if (Config->ZNow) {
578 DtFlags |= DF_BIND_NOW;
579 DtFlags1 |= DF_1_NOW;
580 }
581 if (Config->ZOrigin) {
582 DtFlags |= DF_ORIGIN;
583 DtFlags1 |= DF_1_ORIGIN;
584 }
585
586 if (DtFlags)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000587 Add({DT_FLAGS, DtFlags});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000588 if (DtFlags1)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000589 Add({DT_FLAGS_1, DtFlags1});
Igor Kudrin304860a2015-11-12 04:39:49 +0000590
Ed Mastef5d3cf62016-01-06 15:52:27 +0000591 if (!Config->Entry.empty())
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000592 Add({DT_DEBUG, (uint64_t)0});
Ed Mastef5d3cf62016-01-06 15:52:27 +0000593
Igor Kudrin304860a2015-11-12 04:39:49 +0000594 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000595 Add({DT_MIPS_RLD_VERSION, 1});
596 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
597 Add({DT_MIPS_BASE_ADDRESS, (uintX_t)Target->getVAStart()});
598 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
599 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000600 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000601 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000602 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000603 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
604 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000605 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000606 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000607 }
608
Rafael Espindolade069362016-01-25 21:32:04 +0000609 // +1 for DT_NULL
610 Header.sh_size = (Entries.size() + 1) * Header.sh_entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000611}
612
613template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000614 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
615
Rafael Espindolade069362016-01-25 21:32:04 +0000616 for (const Entry &E : Entries) {
617 P->d_tag = E.Tag;
618 switch (E.Kind) {
619 case Entry::SecAddr:
620 P->d_un.d_ptr = E.OutSec->getVA();
621 break;
622 case Entry::SymAddr:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000623 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
Rafael Espindolade069362016-01-25 21:32:04 +0000624 break;
625 case Entry::PlainInt:
626 P->d_un.d_val = E.Val;
627 break;
628 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000629 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000630 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000631}
632
633template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000634EhFrameHeader<ELFT>::EhFrameHeader()
635 : OutputSectionBase<ELFT>(".eh_frame_hdr", llvm::ELF::SHT_PROGBITS,
636 SHF_ALLOC) {
637 // It's a 4 bytes of header + pointer to the contents of the .eh_frame section
638 // + the number of FDE pointers in the table.
639 this->Header.sh_size = 12;
640}
641
642// We have to get PC values of FDEs. They depend on relocations
643// which are target specific, so we run this code after performing
644// all relocations. We read the values from ouput buffer according to the
645// encoding given for FDEs. Return value is an offset to the initial PC value
646// for the FDE.
647template <class ELFT>
648typename EhFrameHeader<ELFT>::uintX_t
649EhFrameHeader<ELFT>::getFdePc(uintX_t EhVA, const FdeData &F) {
650 const endianness E = ELFT::TargetEndianness;
651 assert((F.Enc & 0xF0) != dwarf::DW_EH_PE_datarel);
652
653 uintX_t FdeOff = EhVA + F.Off + 8;
654 switch (F.Enc & 0xF) {
655 case dwarf::DW_EH_PE_udata2:
656 case dwarf::DW_EH_PE_sdata2:
657 return FdeOff + read16<E>(F.PCRel);
658 case dwarf::DW_EH_PE_udata4:
659 case dwarf::DW_EH_PE_sdata4:
660 return FdeOff + read32<E>(F.PCRel);
661 case dwarf::DW_EH_PE_udata8:
662 case dwarf::DW_EH_PE_sdata8:
663 return FdeOff + read64<E>(F.PCRel);
664 case dwarf::DW_EH_PE_absptr:
665 if (sizeof(uintX_t) == 8)
666 return FdeOff + read64<E>(F.PCRel);
667 return FdeOff + read32<E>(F.PCRel);
668 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000669 fatal("unknown FDE size encoding");
George Rimarf6bc65a2016-01-15 13:34:52 +0000670}
671
672template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
673 const endianness E = ELFT::TargetEndianness;
674
675 const uint8_t Header[] = {1, dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4,
676 dwarf::DW_EH_PE_udata4,
677 dwarf::DW_EH_PE_datarel | dwarf::DW_EH_PE_sdata4};
678 memcpy(Buf, Header, sizeof(Header));
679
680 uintX_t EhVA = Sec->getVA();
681 uintX_t VA = this->getVA();
682 uintX_t EhOff = EhVA - VA - 4;
683 write32<E>(Buf + 4, EhOff);
684 write32<E>(Buf + 8, this->FdeList.size());
685 Buf += 12;
686
687 // InitialPC -> Offset in .eh_frame, sorted by InitialPC.
688 std::map<uintX_t, size_t> PcToOffset;
689 for (const FdeData &F : FdeList)
690 PcToOffset[getFdePc(EhVA, F)] = F.Off;
691
692 for (auto &I : PcToOffset) {
693 // The first four bytes are an offset to the initial PC value for the FDE.
694 write32<E>(Buf, I.first - VA);
695 // The last four bytes are an offset to the FDE data itself.
696 write32<E>(Buf + 4, EhVA + I.second - VA);
697 Buf += 8;
698 }
699}
700
701template <class ELFT>
702void EhFrameHeader<ELFT>::assignEhFrame(EHOutputSection<ELFT> *Sec) {
George Rimar45ca88d2016-01-25 19:27:50 +0000703 assert((!this->Sec || this->Sec == Sec) &&
704 "multiple .eh_frame sections not supported for .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000705 Live = Config->EhFrameHdr;
706 this->Sec = Sec;
707}
708
709template <class ELFT>
710void EhFrameHeader<ELFT>::addFde(uint8_t Enc, size_t Off, uint8_t *PCRel) {
711 if (Live && (Enc & 0xF0) == dwarf::DW_EH_PE_datarel)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000712 fatal("DW_EH_PE_datarel encoding unsupported for FDEs by .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000713 FdeList.push_back(FdeData{Enc, Off, PCRel});
714}
715
716template <class ELFT> void EhFrameHeader<ELFT>::reserveFde() {
717 // Each FDE entry is 8 bytes long:
718 // The first four bytes are an offset to the initial PC value for the FDE. The
719 // last four byte are an offset to the FDE data itself.
720 this->Header.sh_size += 8;
721}
722
723template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000724OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type,
725 uintX_t Flags)
726 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000727
728template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000729void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
730 auto *S = cast<InputSection<ELFT>>(C);
731 Sections.push_back(S);
732 S->OutSec = this;
733 uint32_t Align = S->getAlign();
Rui Ueyama65d98ea2016-01-29 20:31:05 +0000734 this->updateAlign(Align);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000735
736 uintX_t Off = this->Header.sh_size;
Rui Ueyama489a8062016-01-14 20:53:50 +0000737 Off = alignTo(Off, Align);
Rui Ueyama40845e62015-12-26 05:51:07 +0000738 S->OutSecOff = Off;
739 Off += S->getSize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000740 this->Header.sh_size = Off;
741}
742
Rui Ueyama34f29242015-10-13 19:51:57 +0000743// Returns a VA which a relocatin RI refers to. Used only for local symbols.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000744// For non-local symbols, use SymbolBody::getVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000745template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000746typename ELFFile<ELFT>::uintX_t
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000747elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
748 const Elf_Rel_Impl<ELFT, IsRela> &RI,
749 typename ELFFile<ELFT>::uintX_t Addend) {
Rafael Espindola932efcf2015-10-19 20:24:44 +0000750 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
751 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
752
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000753 // PPC64 has a special relocation representing the TOC base pointer
754 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000755 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000756 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000757
Rui Ueyama126d08f2015-10-12 20:28:22 +0000758 const Elf_Sym *Sym =
759 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000760
Rui Ueyama126d08f2015-10-12 20:28:22 +0000761 if (!Sym)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000762 fatal("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000763
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000764 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
765
766 if (Sym->getType() == STT_TLS)
767 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
George Rimarcc06a6f2015-11-29 14:14:20 +0000768 Out<ELFT>::TlsPhdr->p_vaddr;
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000769
Rafael Espindola444576d2015-10-09 19:25:07 +0000770 // According to the ELF spec reference to a local symbol from outside
771 // the group are not allowed. Unfortunately .eh_frame breaks that rule
772 // and must be treated specially. For now we just replace the symbol with
773 // 0.
Rafael Espindola8ea46e02015-11-09 17:44:10 +0000774 if (Section == &InputSection<ELFT>::Discarded || !Section->isLive())
Rafael Espindola932efcf2015-10-19 20:24:44 +0000775 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000776
Rafael Espindolac159c962015-10-19 21:00:02 +0000777 uintX_t Offset = Sym->st_value;
778 if (Sym->getType() == STT_SECTION) {
779 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000780 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000781 }
Rafael Espindola38a36c42016-02-03 16:53:39 +0000782 return Section->OutSec->getVA() + Section->getOffset(Offset) + Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000783}
784
Rui Ueyama34f29242015-10-13 19:51:57 +0000785// Returns true if a symbol can be replaced at load-time by a symbol
786// with the same name defined in other ELF executable or DSO.
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000787bool elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000788 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000789 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000790 if (Body->isShared())
791 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000792
793 if (Body->isUndefined()) {
794 if (!Body->isWeak())
795 return true;
796
797 // This is an horrible corner case. Ideally we would like to say that any
798 // undefined symbol can be preempted so that the dynamic linker has a
799 // chance of finding it at runtime.
800 //
801 // The problem is that the code sequence used to test for weak undef
802 // functions looks like
803 // if (func) func()
804 // If the code is -fPIC the first reference is a load from the got and
805 // everything works.
806 // If the code is not -fPIC there is no reasonable way to solve it:
807 // * A relocation writing to the text segment will fail (it is ro).
808 // * A copy relocation doesn't work for functions.
809 // * The trick of using a plt entry as the address would fail here since
810 // the plt entry would have a non zero address.
811 // Since we cannot do anything better, we just resolve the symbol to 0 and
812 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000813 //
814 // As an extra hack, assume that if we are producing a shared library the
815 // user knows what he or she is doing and can handle a dynamic relocation.
816 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000817 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000818 if (!Config->Shared)
819 return false;
George Rimar5c36e592016-02-02 09:28:53 +0000820 if (Body->getVisibility() != STV_DEFAULT)
821 return false;
822 if (Config->Bsymbolic || (Config->BsymbolicFunctions && Body->isFunc()))
823 return false;
824 return true;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000825}
826
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000827template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000828 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000829 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000830}
831
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000832template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000833EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t Type,
834 uintX_t Flags)
George Rimarf6bc65a2016-01-15 13:34:52 +0000835 : OutputSectionBase<ELFT>(Name, Type, Flags) {
836 Out<ELFT>::EhFrameHdr->assignEhFrame(this);
837}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000838
839template <class ELFT>
840EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
841 : S(S), Index(Index) {}
842
843template <class ELFT> StringRef EHRegion<ELFT>::data() const {
844 ArrayRef<uint8_t> SecData = S->getSectionData();
845 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
846 size_t Start = Offsets[Index].first;
847 size_t End =
848 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
849 return StringRef((const char *)SecData.data() + Start, End - Start);
850}
851
852template <class ELFT>
853Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
854 : EHRegion<ELFT>(S, Index) {}
855
George Rimarf6bc65a2016-01-15 13:34:52 +0000856// Read a byte and advance D by one byte.
857static uint8_t readByte(ArrayRef<uint8_t> &D) {
858 if (D.empty())
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000859 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000860 uint8_t B = D.front();
861 D = D.slice(1);
862 return B;
863}
864
865static void skipLeb128(ArrayRef<uint8_t> &D) {
866 while (!D.empty()) {
867 uint8_t Val = D.front();
868 D = D.slice(1);
869 if ((Val & 0x80) == 0)
870 return;
871 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000872 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000873}
874
875template <class ELFT> static unsigned getSizeForEncoding(unsigned Enc) {
876 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
877 switch (Enc & 0x0f) {
878 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000879 fatal("unknown FDE encoding");
George Rimarf6bc65a2016-01-15 13:34:52 +0000880 case dwarf::DW_EH_PE_absptr:
881 case dwarf::DW_EH_PE_signed:
882 return sizeof(uintX_t);
883 case dwarf::DW_EH_PE_udata2:
884 case dwarf::DW_EH_PE_sdata2:
885 return 2;
886 case dwarf::DW_EH_PE_udata4:
887 case dwarf::DW_EH_PE_sdata4:
888 return 4;
889 case dwarf::DW_EH_PE_udata8:
890 case dwarf::DW_EH_PE_sdata8:
891 return 8;
892 }
893}
894
895template <class ELFT>
896uint8_t EHOutputSection<ELFT>::getFdeEncoding(ArrayRef<uint8_t> D) {
897 auto Check = [](bool C) {
898 if (!C)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000899 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000900 };
901
902 Check(D.size() >= 8);
903 D = D.slice(8);
904
905 uint8_t Version = readByte(D);
906 if (Version != 1 && Version != 3)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000907 fatal("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
George Rimarf6bc65a2016-01-15 13:34:52 +0000908
909 auto AugEnd = std::find(D.begin() + 1, D.end(), '\0');
910 Check(AugEnd != D.end());
911 ArrayRef<uint8_t> AugString(D.begin(), AugEnd - D.begin());
912 D = D.slice(AugString.size() + 1);
913
914 // Code alignment factor should always be 1 for .eh_frame.
915 if (readByte(D) != 1)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000916 fatal("CIE code alignment must be 1");
George Rimarf6bc65a2016-01-15 13:34:52 +0000917 // Skip data alignment factor
918 skipLeb128(D);
919
920 // Skip the return address register. In CIE version 1 this is a single
921 // byte. In CIE version 3 this is an unsigned LEB128.
922 if (Version == 1)
923 readByte(D);
924 else
925 skipLeb128(D);
926
927 while (!AugString.empty()) {
928 switch (readByte(AugString)) {
929 case 'z':
930 skipLeb128(D);
931 break;
932 case 'R':
933 return readByte(D);
934 case 'P': {
935 uint8_t Enc = readByte(D);
936 if ((Enc & 0xf0) == dwarf::DW_EH_PE_aligned)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000937 fatal("DW_EH_PE_aligned encoding for address of a personality routine "
George Rimarf6bc65a2016-01-15 13:34:52 +0000938 "handler not supported");
939 unsigned EncSize = getSizeForEncoding<ELFT>(Enc);
940 Check(D.size() >= EncSize);
941 D = D.slice(EncSize);
942 break;
943 }
944 case 'S':
945 case 'L':
946 // L: Language Specific Data Area (LSDA) encoding
947 // S: This CIE represents a stack frame for the invocation of a signal
948 // handler
949 break;
950 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000951 fatal("unknown .eh_frame augmentation string value");
George Rimarf6bc65a2016-01-15 13:34:52 +0000952 }
953 }
954 return dwarf::DW_EH_PE_absptr;
955}
956
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000957template <class ELFT>
958template <bool IsRela>
959void EHOutputSection<ELFT>::addSectionAux(
960 EHInputSection<ELFT> *S,
961 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
962 const endianness E = ELFT::TargetEndianness;
963
964 S->OutSec = this;
Rui Ueyama65d98ea2016-01-29 20:31:05 +0000965 this->updateAlign(S->getAlign());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000966 Sections.push_back(S);
967
968 ArrayRef<uint8_t> SecData = S->getSectionData();
969 ArrayRef<uint8_t> D = SecData;
970 uintX_t Offset = 0;
971 auto RelI = Rels.begin();
972 auto RelE = Rels.end();
973
George Rimar147747a2016-01-02 16:55:01 +0000974 DenseMap<unsigned, unsigned> OffsetToIndex;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000975 while (!D.empty()) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000976 unsigned Index = S->Offsets.size();
977 S->Offsets.push_back(std::make_pair(Offset, -1));
978
George Rimar003be4f2015-12-17 09:23:40 +0000979 uintX_t Length = readEntryLength(D);
George Rimarf6bc65a2016-01-15 13:34:52 +0000980 // If CIE/FDE data length is zero then Length is 4, this
981 // shall be considered a terminator and processing shall end.
982 if (Length == 4)
983 break;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000984 StringRef Entry((const char *)D.data(), Length);
985
986 while (RelI != RelE && RelI->r_offset < Offset)
987 ++RelI;
988 uintX_t NextOffset = Offset + Length;
989 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
990
991 uint32_t ID = read32<E>(D.data() + 4);
992 if (ID == 0) {
993 // CIE
994 Cie<ELFT> C(S, Index);
George Rimarf6bc65a2016-01-15 13:34:52 +0000995 if (Config->EhFrameHdr)
996 C.FdeEncoding = getFdeEncoding(D);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000997
998 StringRef Personality;
999 if (HasReloc) {
1000 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
1001 SymbolBody &Body = *S->getFile()->getSymbolBody(SymIndex)->repl();
1002 Personality = Body.getName();
1003 }
1004
1005 std::pair<StringRef, StringRef> CieInfo(Entry, Personality);
1006 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
George Rimar147747a2016-01-02 16:55:01 +00001007 if (P.second) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001008 Cies.push_back(C);
Rui Ueyama489a8062016-01-14 20:53:50 +00001009 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
George Rimar147747a2016-01-02 16:55:01 +00001010 }
1011 OffsetToIndex[Offset] = P.first->second;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001012 } else {
1013 if (!HasReloc)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001014 fatal("FDE doesn't reference another section");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001015 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
1016 if (Target != &InputSection<ELFT>::Discarded && Target->isLive()) {
1017 uint32_t CieOffset = Offset + 4 - ID;
George Rimar147747a2016-01-02 16:55:01 +00001018 auto I = OffsetToIndex.find(CieOffset);
1019 if (I == OffsetToIndex.end())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001020 fatal("Invalid CIE reference");
George Rimar147747a2016-01-02 16:55:01 +00001021 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
George Rimarf6bc65a2016-01-15 13:34:52 +00001022 Out<ELFT>::EhFrameHdr->reserveFde();
Rui Ueyama489a8062016-01-14 20:53:50 +00001023 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001024 }
1025 }
1026
1027 Offset = NextOffset;
1028 D = D.slice(Length);
1029 }
1030}
1031
1032template <class ELFT>
George Rimar003be4f2015-12-17 09:23:40 +00001033typename EHOutputSection<ELFT>::uintX_t
1034EHOutputSection<ELFT>::readEntryLength(ArrayRef<uint8_t> D) {
1035 const endianness E = ELFT::TargetEndianness;
1036
1037 if (D.size() < 4)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001038 fatal("Truncated CIE/FDE length");
George Rimar003be4f2015-12-17 09:23:40 +00001039 uint64_t Len = read32<E>(D.data());
1040 if (Len < UINT32_MAX) {
1041 if (Len > (UINT32_MAX - 4))
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001042 fatal("CIE/FIE size is too large");
George Rimar003be4f2015-12-17 09:23:40 +00001043 if (Len + 4 > D.size())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001044 fatal("CIE/FIE ends past the end of the section");
George Rimar003be4f2015-12-17 09:23:40 +00001045 return Len + 4;
1046 }
1047
1048 if (D.size() < 12)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001049 fatal("Truncated CIE/FDE length");
George Rimar003be4f2015-12-17 09:23:40 +00001050 Len = read64<E>(D.data() + 4);
1051 if (Len > (UINT64_MAX - 12))
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001052 fatal("CIE/FIE size is too large");
George Rimar003be4f2015-12-17 09:23:40 +00001053 if (Len + 12 > D.size())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001054 fatal("CIE/FIE ends past the end of the section");
George Rimar003be4f2015-12-17 09:23:40 +00001055 return Len + 12;
1056}
1057
1058template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001059void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1060 auto *S = cast<EHInputSection<ELFT>>(C);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001061 const Elf_Shdr *RelSec = S->RelocSection;
Rui Ueyama0de86c12016-01-27 22:23:44 +00001062 if (!RelSec) {
1063 addSectionAux(S, make_range<const Elf_Rela *>(nullptr, nullptr));
1064 return;
1065 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001066 ELFFile<ELFT> &Obj = S->getFile()->getObj();
1067 if (RelSec->sh_type == SHT_RELA)
Rui Ueyama0de86c12016-01-27 22:23:44 +00001068 addSectionAux(S, Obj.relas(RelSec));
1069 else
1070 addSectionAux(S, Obj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001071}
1072
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001073template <class ELFT>
1074static typename ELFFile<ELFT>::uintX_t writeAlignedCieOrFde(StringRef Data,
1075 uint8_t *Buf) {
1076 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1077 const endianness E = ELFT::TargetEndianness;
Rui Ueyama489a8062016-01-14 20:53:50 +00001078 uint64_t Len = alignTo(Data.size(), sizeof(uintX_t));
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001079 write32<E>(Buf, Len - 4);
1080 memcpy(Buf + 4, Data.data() + 4, Data.size() - 4);
1081 return Len;
1082}
1083
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001084template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1085 const endianness E = ELFT::TargetEndianness;
1086 size_t Offset = 0;
1087 for (const Cie<ELFT> &C : Cies) {
1088 size_t CieOffset = Offset;
1089
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001090 uintX_t CIELen = writeAlignedCieOrFde<ELFT>(C.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001091 C.S->Offsets[C.Index].second = Offset;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001092 Offset += CIELen;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001093
1094 for (const EHRegion<ELFT> &F : C.Fdes) {
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001095 uintX_t Len = writeAlignedCieOrFde<ELFT>(F.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001096 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001097 F.S->Offsets[F.Index].second = Offset;
George Rimarf6bc65a2016-01-15 13:34:52 +00001098 Out<ELFT>::EhFrameHdr->addFde(C.FdeEncoding, Offset, Buf + Offset + 8);
George Rimare72beba2015-12-21 09:38:59 +00001099 Offset += Len;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001100 }
1101 }
1102
1103 for (EHInputSection<ELFT> *S : Sections) {
1104 const Elf_Shdr *RelSec = S->RelocSection;
1105 if (!RelSec)
1106 continue;
1107 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1108 if (RelSec->sh_type == SHT_RELA)
1109 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1110 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001111 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001112 }
1113}
1114
1115template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001116MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
1117 uintX_t Flags)
1118 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001119
1120template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001121 if (shouldTailMerge()) {
1122 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001123 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001124 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001125 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001126 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1127 StringRef Data = P.first;
1128 memcpy(Buf + P.second, Data.data(), Data.size());
1129 }
1130}
1131
1132static size_t findNull(StringRef S, size_t EntSize) {
1133 // Optimize the common case.
1134 if (EntSize == 1)
1135 return S.find(0);
1136
1137 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1138 const char *B = S.begin() + I;
1139 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1140 return I;
1141 }
1142 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001143}
1144
1145template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001146void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1147 auto *S = cast<MergeInputSection<ELFT>>(C);
Rafael Espindolac159c962015-10-19 21:00:02 +00001148 S->OutSec = this;
Rui Ueyama65d98ea2016-01-29 20:31:05 +00001149 this->updateAlign(S->getAlign());
Rafael Espindolac159c962015-10-19 21:00:02 +00001150
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001151 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001152 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001153 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001154
Rui Ueyamaead75fc2016-01-29 22:18:55 +00001155 // If this is of type string, the contents are null-terminated strings.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001156 if (this->Header.sh_flags & SHF_STRINGS) {
Rui Ueyamaad87ff72016-01-06 02:52:24 +00001157 uintX_t Offset = 0;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001158 while (!Data.empty()) {
1159 size_t End = findNull(Data, EntSize);
1160 if (End == StringRef::npos)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001161 fatal("String is not null terminated");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001162 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001163 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001164 if (shouldTailMerge())
1165 OutputOffset = -1;
1166 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1167 uintX_t Size = End + EntSize;
1168 Data = Data.substr(Size);
1169 Offset += Size;
1170 }
Rui Ueyamaead75fc2016-01-29 22:18:55 +00001171 return;
1172 }
1173
1174 // If this is not of type string, every entry has the same size.
1175 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1176 StringRef Entry = Data.substr(I, EntSize);
1177 size_t OutputOffset = Builder.add(Entry);
1178 S->Offsets.push_back(std::make_pair(I, OutputOffset));
Rafael Espindolac159c962015-10-19 21:00:02 +00001179 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001180}
1181
1182template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001183unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1184 return Builder.getOffset(Val);
1185}
1186
1187template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1188 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1189}
1190
1191template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1192 if (shouldTailMerge())
1193 Builder.finalize();
1194 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001195}
1196
1197template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001198StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001199 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1200 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001201 Dynamic(Dynamic) {
1202 this->Header.sh_addralign = 1;
1203}
1204
Rafael Espindolae2c24612016-01-29 01:24:25 +00001205// Adds a string to the string table. If HashIt is true we hash and check for
1206// duplicates. It is optional because the name of global symbols are already
1207// uniqued and hashing them again has a big cost for a small value: uniquing
1208// them with some other string that happens to be the same.
1209template <class ELFT>
1210unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1211 if (HashIt) {
1212 auto R = StringMap.insert(std::make_pair(S, Size));
1213 if (!R.second)
1214 return R.first->second;
1215 }
1216 unsigned Ret = Size;
1217 Size += S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001218 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001219 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001220}
1221
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001222template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001223 // ELF string tables start with NUL byte, so advance the pointer by one.
1224 ++Buf;
1225 for (StringRef S : Strings) {
1226 memcpy(Buf, S.data(), S.size());
1227 Buf += S.size() + 1;
1228 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001229}
1230
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001231template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001232SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001233 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001234 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1235 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001236 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001237 StrTabSec(StrTabSec), Table(Table) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001238 this->Header.sh_entsize = sizeof(Elf_Sym);
Rui Ueyama5e378dd2016-01-29 22:18:57 +00001239 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +00001240}
1241
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001242// Orders symbols according to their positions in the GOT,
1243// in compliance with MIPS ABI rules.
1244// See "Global Offset Table" in Chapter 5 in the following document
1245// for detailed description:
1246// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rafael Espindolae2c24612016-01-29 01:24:25 +00001247static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
1248 const std::pair<SymbolBody *, unsigned> &R) {
1249 if (!L.first->isInGot() || !R.first->isInGot())
1250 return R.first->isInGot();
1251 return L.first->GotIndex < R.first->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001252}
1253
Rui Ueyama0db335f2015-10-07 16:58:54 +00001254template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001255 if (this->Header.sh_size)
1256 return; // Already finalized.
1257
Rui Ueyama0db335f2015-10-07 16:58:54 +00001258 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001259 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001260 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001261
1262 if (!StrTabSec.isDynamic()) {
1263 std::stable_sort(Symbols.begin(), Symbols.end(),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001264 [](const std::pair<SymbolBody *, unsigned> &L,
1265 const std::pair<SymbolBody *, unsigned> &R) {
1266 return getSymbolBinding(L.first) == STB_LOCAL &&
1267 getSymbolBinding(R.first) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001268 });
1269 return;
1270 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001271 if (Out<ELFT>::GnuHashTab)
1272 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1273 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001274 else if (Config->EMachine == EM_MIPS)
1275 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001276 size_t I = 0;
Rafael Espindolae2c24612016-01-29 01:24:25 +00001277 for (const std::pair<SymbolBody *, unsigned> &P : Symbols)
Rui Ueyama572a6f72016-01-29 01:49:33 +00001278 P.first->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001279}
1280
1281template <class ELFT>
1282void SymbolTableSection<ELFT>::addSymbol(SymbolBody *Body) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001283 Symbols.push_back(
1284 std::make_pair(Body, StrTabSec.addString(Body->getName(), false)));
Rui Ueyama0db335f2015-10-07 16:58:54 +00001285}
1286
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001287template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001288 Buf += sizeof(Elf_Sym);
1289
1290 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1291 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001292 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1293 writeLocalSymbols(Buf);
1294
1295 writeGlobalSymbols(Buf);
1296}
1297
1298template <class ELFT>
1299void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1300 // Iterate over all input object files to copy their local symbols
1301 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001302 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001303 for (const std::pair<const Elf_Sym *, unsigned> &P : File->KeptLocalSyms) {
1304 const Elf_Sym *Sym = P.first;
Rafael Espindola444576d2015-10-09 19:25:07 +00001305
Rui Ueyamac55733e2015-09-30 00:54:29 +00001306 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001307 uintX_t VA = 0;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001308 if (Sym->st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001309 ESym->st_shndx = SHN_ABS;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001310 VA = Sym->st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001311 } else {
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001312 InputSectionBase<ELFT> *Section = File->getSection(*Sym);
Rafael Espindolac159c962015-10-19 21:00:02 +00001313 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1314 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001315 VA = Section->getOffset(*Sym);
Tom Stellard80efb162016-01-07 03:59:08 +00001316 // Symbol offsets for AMDGPU need to be the offset in bytes of the
1317 // symbol from the beginning of the section.
1318 if (Config->EMachine != EM_AMDGPU)
1319 VA += OutSec->getVA();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001320 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001321 ESym->st_name = P.second;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001322 ESym->st_size = Sym->st_size;
1323 ESym->setBindingAndType(Sym->getBinding(), Sym->getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001324 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001325 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001326 }
1327 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001328}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001329
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001330template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001331static const typename llvm::object::ELFFile<ELFT>::Elf_Sym *
1332getElfSym(SymbolBody &Body) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001333 if (auto *EBody = dyn_cast<DefinedElf<ELFT>>(&Body))
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001334 return &EBody->Sym;
1335 if (auto *EBody = dyn_cast<UndefinedElf<ELFT>>(&Body))
1336 return &EBody->Sym;
1337 return nullptr;
1338}
1339
1340template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001341void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001342 // Write the internal symbol table contents to the output symbol table
1343 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001344 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001345 for (const std::pair<SymbolBody *, unsigned> &P : Symbols) {
1346 SymbolBody *Body = P.first;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001347 const OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001348
Rafael Espindola8614c562015-10-06 14:33:58 +00001349 switch (Body->kind()) {
Rafael Espindola0e604f92015-09-25 18:56:53 +00001350 case SymbolBody::DefinedSyntheticKind:
Rui Ueyamab4908762015-10-07 17:04:18 +00001351 OutSec = &cast<DefinedSynthetic<ELFT>>(Body)->Section;
Rafael Espindola0e604f92015-09-25 18:56:53 +00001352 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001353 case SymbolBody::DefinedRegularKind: {
1354 auto *Sym = cast<DefinedRegular<ELFT>>(Body->repl());
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001355 if (InputSectionBase<ELFT> *Sec = Sym->Section) {
1356 if (!Sec->isLive())
1357 continue;
1358 OutSec = Sec->OutSec;
1359 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001360 break;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001361 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001362 case SymbolBody::DefinedCommonKind:
Rui Ueyama15ef5e12015-10-07 19:18:16 +00001363 OutSec = Out<ELFT>::Bss;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001364 break;
George Rimarbc590fe2015-10-28 16:48:58 +00001365 case SymbolBody::SharedKind: {
Rui Ueyamabb936062015-12-17 01:14:23 +00001366 if (cast<SharedSymbol<ELFT>>(Body)->NeedsCopy)
George Rimarbc590fe2015-10-28 16:48:58 +00001367 OutSec = Out<ELFT>::Bss;
1368 break;
1369 }
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001370 case SymbolBody::UndefinedElfKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001371 case SymbolBody::UndefinedKind:
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001372 case SymbolBody::LazyKind:
Rafael Espindola8614c562015-10-06 14:33:58 +00001373 break;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001374 }
1375
Rafael Espindolae2c24612016-01-29 01:24:25 +00001376 ESym->st_name = P.second;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001377
Rafael Espindola8614c562015-10-06 14:33:58 +00001378 unsigned char Type = STT_NOTYPE;
1379 uintX_t Size = 0;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001380 if (const Elf_Sym *InputSym = getElfSym<ELFT>(*Body)) {
1381 Type = InputSym->getType();
1382 Size = InputSym->st_size;
Rafael Espindola11191912015-12-24 16:23:37 +00001383 } else if (auto *C = dyn_cast<DefinedCommon>(Body)) {
1384 Type = STT_OBJECT;
1385 Size = C->Size;
Rafael Espindola8614c562015-10-06 14:33:58 +00001386 }
1387
Igor Kudrin853b88d2015-10-20 20:52:14 +00001388 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001389 ESym->st_size = Size;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001390 ESym->setVisibility(Body->getVisibility());
Rui Ueyamab5a69702016-02-01 21:00:35 +00001391 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001392
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001393 if (OutSec)
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001394 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001395 else if (isa<DefinedRegular<ELFT>>(Body))
1396 ESym->st_shndx = SHN_ABS;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001397
1398 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001399 }
1400}
1401
Igor Kudrin853b88d2015-10-20 20:52:14 +00001402template <class ELFT>
1403uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001404 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001405 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1406 return STB_LOCAL;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001407 if (const Elf_Sym *ESym = getElfSym<ELFT>(*Body))
1408 return ESym->getBinding();
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001409 if (isa<DefinedSynthetic<ELFT>>(Body))
1410 return STB_LOCAL;
Igor Kudrin853b88d2015-10-20 20:52:14 +00001411 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1412}
1413
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001414template <class ELFT>
1415MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1416 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1417 this->Header.sh_addralign = 4;
1418 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1419 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1420}
1421
1422template <class ELFT>
1423void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1424 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
1425 R->ri_gp_value = getMipsGpAddr<ELFT>();
Rui Ueyama70eed362016-01-06 22:42:43 +00001426 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001427}
1428
1429template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001430void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001431 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001432 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001433 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001434}
1435
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001436namespace lld {
1437namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001438template class OutputSectionBase<ELF32LE>;
1439template class OutputSectionBase<ELF32BE>;
1440template class OutputSectionBase<ELF64LE>;
1441template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001442
George Rimarf6bc65a2016-01-15 13:34:52 +00001443template class EhFrameHeader<ELF32LE>;
1444template class EhFrameHeader<ELF32BE>;
1445template class EhFrameHeader<ELF64LE>;
1446template class EhFrameHeader<ELF64BE>;
1447
George Rimar648a2c32015-10-20 08:54:27 +00001448template class GotPltSection<ELF32LE>;
1449template class GotPltSection<ELF32BE>;
1450template class GotPltSection<ELF64LE>;
1451template class GotPltSection<ELF64BE>;
1452
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001453template class GotSection<ELF32LE>;
1454template class GotSection<ELF32BE>;
1455template class GotSection<ELF64LE>;
1456template class GotSection<ELF64BE>;
1457
1458template class PltSection<ELF32LE>;
1459template class PltSection<ELF32BE>;
1460template class PltSection<ELF64LE>;
1461template class PltSection<ELF64BE>;
1462
1463template class RelocationSection<ELF32LE>;
1464template class RelocationSection<ELF32BE>;
1465template class RelocationSection<ELF64LE>;
1466template class RelocationSection<ELF64BE>;
1467
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001468template class InterpSection<ELF32LE>;
1469template class InterpSection<ELF32BE>;
1470template class InterpSection<ELF64LE>;
1471template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001472
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001473template class GnuHashTableSection<ELF32LE>;
1474template class GnuHashTableSection<ELF32BE>;
1475template class GnuHashTableSection<ELF64LE>;
1476template class GnuHashTableSection<ELF64BE>;
1477
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001478template class HashTableSection<ELF32LE>;
1479template class HashTableSection<ELF32BE>;
1480template class HashTableSection<ELF64LE>;
1481template class HashTableSection<ELF64BE>;
1482
1483template class DynamicSection<ELF32LE>;
1484template class DynamicSection<ELF32BE>;
1485template class DynamicSection<ELF64LE>;
1486template class DynamicSection<ELF64BE>;
1487
1488template class OutputSection<ELF32LE>;
1489template class OutputSection<ELF32BE>;
1490template class OutputSection<ELF64LE>;
1491template class OutputSection<ELF64BE>;
1492
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001493template class EHOutputSection<ELF32LE>;
1494template class EHOutputSection<ELF32BE>;
1495template class EHOutputSection<ELF64LE>;
1496template class EHOutputSection<ELF64BE>;
1497
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001498template class MipsReginfoOutputSection<ELF32LE>;
1499template class MipsReginfoOutputSection<ELF32BE>;
1500template class MipsReginfoOutputSection<ELF64LE>;
1501template class MipsReginfoOutputSection<ELF64BE>;
1502
Rafael Espindolac159c962015-10-19 21:00:02 +00001503template class MergeOutputSection<ELF32LE>;
1504template class MergeOutputSection<ELF32BE>;
1505template class MergeOutputSection<ELF64LE>;
1506template class MergeOutputSection<ELF64BE>;
1507
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001508template class StringTableSection<ELF32LE>;
1509template class StringTableSection<ELF32BE>;
1510template class StringTableSection<ELF64LE>;
1511template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001512
1513template class SymbolTableSection<ELF32LE>;
1514template class SymbolTableSection<ELF32BE>;
1515template class SymbolTableSection<ELF64LE>;
1516template class SymbolTableSection<ELF64BE>;
1517
Rui Ueyama5ec41f32016-01-26 01:32:00 +00001518template uint32_t getLocalRelTarget(const ObjectFile<ELF32LE> &,
1519 const ELFFile<ELF32LE>::Elf_Rel &,
1520 uint32_t);
1521template uint32_t getLocalRelTarget(const ObjectFile<ELF32BE> &,
1522 const ELFFile<ELF32BE>::Elf_Rel &,
1523 uint32_t);
1524template uint64_t getLocalRelTarget(const ObjectFile<ELF64LE> &,
1525 const ELFFile<ELF64LE>::Elf_Rel &,
1526 uint64_t);
1527template uint64_t getLocalRelTarget(const ObjectFile<ELF64BE> &,
1528 const ELFFile<ELF64BE>::Elf_Rel &,
1529 uint64_t);
1530template uint32_t getLocalRelTarget(const ObjectFile<ELF32LE> &,
1531 const ELFFile<ELF32LE>::Elf_Rela &,
1532 uint32_t);
1533template uint32_t getLocalRelTarget(const ObjectFile<ELF32BE> &,
1534 const ELFFile<ELF32BE>::Elf_Rela &,
1535 uint32_t);
1536template uint64_t getLocalRelTarget(const ObjectFile<ELF64LE> &,
1537 const ELFFile<ELF64LE>::Elf_Rela &,
1538 uint64_t);
1539template uint64_t getLocalRelTarget(const ObjectFile<ELF64BE> &,
1540 const ELFFile<ELF64BE>::Elf_Rela &,
1541 uint64_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001542}
1543}