blob: 1f72e123cd3a822841bc7c9bbb0a3c60573e0783 [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"
George Rimare2ee72b2016-02-26 14:48:31 +000012#include "LinkerScript.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000013#include "SymbolTable.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000014#include "Target.h"
Rui Ueyamae9809502016-03-11 04:23:12 +000015#include "lld/Core/Parallel.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000016#include "llvm/Support/Dwarf.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000017#include "llvm/Support/MathExtras.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000018#include <map>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000019
Rafael Espindola5805c4f2015-09-21 21:38:08 +000020using namespace llvm;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +000021using namespace llvm::dwarf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000022using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000023using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024using namespace llvm::ELF;
25
26using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000027using namespace lld::elf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028
George Rimar12737b72016-02-25 08:40:26 +000029static bool isAlpha(char C) {
30 return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z') || C == '_';
31}
32
33static bool isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
34
35// Returns true if S is valid as a C language identifier.
Rafael Espindolae0df00b2016-02-28 00:25:54 +000036bool elf::isValidCIdentifier(StringRef S) {
Rui Ueyamadad77c52016-02-26 15:42:06 +000037 return !S.empty() && isAlpha(S[0]) &&
38 std::all_of(S.begin() + 1, S.end(), isAlnum);
George Rimar12737b72016-02-25 08:40:26 +000039}
40
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000041template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +000042OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t Type,
43 uintX_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000044 : Name(Name) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000045 memset(&Header, 0, sizeof(Elf_Shdr));
Rui Ueyamad97e5c42016-01-07 18:33:11 +000046 Header.sh_type = Type;
47 Header.sh_flags = Flags;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000048}
49
Rafael Espindola35c6af32015-09-25 17:19:10 +000050template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000051GotPltSection<ELFT>::GotPltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000052 : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
George Rimar648a2c32015-10-20 08:54:27 +000053 this->Header.sh_addralign = sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000054}
55
Rafael Espindola67d72c02016-03-11 12:06:30 +000056template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
57 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
58 Entries.push_back(&Sym);
George Rimar648a2c32015-10-20 08:54:27 +000059}
60
61template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +000062 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +000063}
64
George Rimar648a2c32015-10-20 08:54:27 +000065template <class ELFT> void GotPltSection<ELFT>::finalize() {
Igor Kudrin351b41d2015-11-16 17:44:08 +000066 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +000067 (Target->GotPltHeaderEntriesNum + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000068}
69
70template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +000071 Target->writeGotPltHeader(Buf);
Rui Ueyama724d6252016-01-29 01:49:32 +000072 Buf += Target->GotPltHeaderEntriesNum * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000073 for (const SymbolBody *B : Entries) {
Rui Ueyamab5a69702016-02-01 21:00:35 +000074 Target->writeGotPlt(Buf, B->getPltVA<ELFT>());
George Rimar648a2c32015-10-20 08:54:27 +000075 Buf += sizeof(uintX_t);
76 }
77}
78
79template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000080GotSection<ELFT>::GotSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000081 : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000082 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +000083 this->Header.sh_flags |= SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000084 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000085}
86
Rafael Espindola67d72c02016-03-11 12:06:30 +000087template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
88 Sym.GotIndex = Entries.size();
89 Entries.push_back(&Sym);
George Rimar687138c2015-11-13 16:28:53 +000090}
91
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000092template <class ELFT> void GotSection<ELFT>::addMipsLocalEntry() {
93 ++MipsLocalEntries;
94}
95
Rafael Espindola67d72c02016-03-11 12:06:30 +000096template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
97 if (Sym.hasGlobalDynIndex())
George Rimar90cd0a82015-12-01 19:20:26 +000098 return false;
Rafael Espindola67d72c02016-03-11 12:06:30 +000099 Sym.GlobalDynIndex = Target->GotHeaderEntriesNum + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +0000100 // Global Dynamic TLS entries take two GOT slots.
Rafael Espindola67d72c02016-03-11 12:06:30 +0000101 Entries.push_back(&Sym);
George Rimar687138c2015-11-13 16:28:53 +0000102 Entries.push_back(nullptr);
George Rimar90cd0a82015-12-01 19:20:26 +0000103 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104}
105
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000106// Reserves TLS entries for a TLS module ID and a TLS block offset.
107// In total it takes two GOT slots.
108template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
109 if (TlsIndexOff != uint32_t(-1))
George Rimarb17f7392015-12-01 18:24:07 +0000110 return false;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000111 TlsIndexOff = Entries.size() * sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000112 Entries.push_back(nullptr);
113 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +0000114 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000115}
116
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000117template <class ELFT>
118typename GotSection<ELFT>::uintX_t
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000119GotSection<ELFT>::getMipsLocalFullAddr(const SymbolBody &B) {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000120 return getMipsLocalEntryAddr(B.getVA<ELFT>());
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000121}
122
123template <class ELFT>
124typename GotSection<ELFT>::uintX_t
125GotSection<ELFT>::getMipsLocalPageAddr(uintX_t EntryValue) {
126 // Initialize the entry by the %hi(EntryValue) expression
127 // but without right-shifting.
128 return getMipsLocalEntryAddr((EntryValue + 0x8000) & ~0xffff);
129}
130
131template <class ELFT>
132typename GotSection<ELFT>::uintX_t
133GotSection<ELFT>::getMipsLocalEntryAddr(uintX_t EntryValue) {
Rui Ueyama724d6252016-01-29 01:49:32 +0000134 size_t NewIndex = Target->GotHeaderEntriesNum + MipsLocalGotPos.size();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000135 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
136 assert(!P.second || MipsLocalGotPos.size() <= MipsLocalEntries);
137 return this->getVA() + P.first->second * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000138}
139
Igor Kudrin304860a2015-11-12 04:39:49 +0000140template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000141typename GotSection<ELFT>::uintX_t
142GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
143 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
144}
145
146template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000147const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
148 return Entries.empty() ? nullptr : Entries.front();
149}
150
151template <class ELFT>
152unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
Rui Ueyama724d6252016-01-29 01:49:32 +0000153 return Target->GotHeaderEntriesNum + MipsLocalEntries;
Igor Kudrin304860a2015-11-12 04:39:49 +0000154}
155
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000156template <class ELFT> void GotSection<ELFT>::finalize() {
157 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +0000158 (Target->GotHeaderEntriesNum + MipsLocalEntries + Entries.size()) *
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000159 sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000160}
161
Rafael Espindolaa6627382015-10-06 23:56:53 +0000162template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000163 Target->writeGotHeader(Buf);
Rui Ueyama5cbf5d22016-02-02 02:29:03 +0000164 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000165 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
166 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, L.first);
167 }
Rui Ueyama724d6252016-01-29 01:49:32 +0000168 Buf += Target->GotHeaderEntriesNum * sizeof(uintX_t);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000169 Buf += MipsLocalEntries * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000170 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000171 uint8_t *Entry = Buf;
172 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000173 if (!B)
174 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000175 // MIPS has special rules to fill up GOT entries.
176 // See "Global Offset Table" in Chapter 5 in the following document
177 // for detailed description:
178 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
179 // As the first approach, we can just store addresses for all symbols.
Rafael Espindola67d72c02016-03-11 12:06:30 +0000180 if (Config->EMachine != EM_MIPS && canBePreempted(*B))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000181 continue; // The dynamic linker will take care of it.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000182 uintX_t VA = B->getVA<ELFT>();
Rafael Espindolae782f672015-10-07 03:56:05 +0000183 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000184 }
185}
186
Rafael Espindola35c6af32015-09-25 17:19:10 +0000187template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000188PltSection<ELFT>::PltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000189 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000190 this->Header.sh_addralign = 16;
191}
192
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000194 size_t Off = 0;
Rui Ueyama9398f862016-01-29 04:15:02 +0000195 if (Target->UseLazyBinding) {
Rui Ueyama74937fc2016-02-02 02:53:58 +0000196 // At beginning of PLT, we have code to call the dynamic linker
197 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama900e2d22016-01-29 03:51:49 +0000198 Target->writePltZero(Buf);
Rui Ueyama62515452016-01-29 03:00:32 +0000199 Off += Target->PltZeroSize;
George Rimar648a2c32015-10-20 08:54:27 +0000200 }
George Rimarfb5d7f22015-11-26 19:58:51 +0000201 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000202 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000203 unsigned RelOff = I.second;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000204 uint64_t Got =
205 Target->UseLazyBinding ? B->getGotPltVA<ELFT>() : B->getGotVA<ELFT>();
Rui Ueyama242ddf42015-10-12 18:56:36 +0000206 uint64_t Plt = this->getVA() + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000207 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000208 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000209 }
210}
211
Rafael Espindola67d72c02016-03-11 12:06:30 +0000212template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
213 Sym.PltIndex = Entries.size();
Rui Ueyama724d6252016-01-29 01:49:32 +0000214 unsigned RelOff = Target->UseLazyBinding
George Rimar77b77792015-11-25 22:15:01 +0000215 ? Out<ELFT>::RelaPlt->getRelocOffset()
216 : Out<ELFT>::RelaDyn->getRelocOffset();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000217 Entries.push_back(std::make_pair(&Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218}
219
George Rimar648a2c32015-10-20 08:54:27 +0000220template <class ELFT> void PltSection<ELFT>::finalize() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000221 this->Header.sh_size =
Rui Ueyama62515452016-01-29 03:00:32 +0000222 Target->PltZeroSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000223}
224
225template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000226RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000227 : OutputSectionBase<ELFT>(Name, IsRela ? SHT_RELA : SHT_REL, SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000228 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000229 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000230 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000231}
232
George Rimar5828c232015-11-30 17:49:19 +0000233template <class ELFT>
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000234void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
235 SymbolBody *Sym = Reloc.Sym;
236 if (!Reloc.UseSymVA && Sym)
Rafael Espindolaabebed92016-02-05 15:27:15 +0000237 Sym->MustBeInDynSym = true;
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000238 Relocs.push_back(Reloc);
239}
240
241template <class ELFT>
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000242typename ELFFile<ELFT>::uintX_t DynamicReloc<ELFT>::getOffset() const {
243 switch (OKind) {
244 case Off_GTlsIndex:
Rafael Espindolade9857e2016-02-04 21:33:05 +0000245 return Out<ELFT>::Got->getGlobalDynAddr(*Sym);
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000246 case Off_GTlsOffset:
Rafael Espindolade9857e2016-02-04 21:33:05 +0000247 return Out<ELFT>::Got->getGlobalDynAddr(*Sym) + sizeof(uintX_t);
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000248 case Off_LTlsIndex:
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000249 return Out<ELFT>::Got->getTlsIndexVA();
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000250 case Off_Sec:
251 return OffsetSec->getOffset(OffsetInSec) + OffsetSec->OutSec->getVA();
252 case Off_Bss:
Rafael Espindolade9857e2016-02-04 21:33:05 +0000253 return cast<SharedSymbol<ELFT>>(Sym)->OffsetInBss + Out<ELFT>::Bss->getVA();
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000254 case Off_Got:
Rafael Espindolade9857e2016-02-04 21:33:05 +0000255 return Sym->getGotVA<ELFT>();
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000256 case Off_GotPlt:
Rafael Espindolade9857e2016-02-04 21:33:05 +0000257 return Sym->getGotPltVA<ELFT>();
George Rimar5828c232015-11-30 17:49:19 +0000258 }
George Rimar777f9632016-03-12 08:31:34 +0000259 llvm_unreachable("invalid offset kind");
George Rimar5828c232015-11-30 17:49:19 +0000260}
261
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000263 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000264 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000265 Buf += IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindolade9857e2016-02-04 21:33:05 +0000266 SymbolBody *Sym = Rel.Sym;
Rui Ueyamab0210e832016-01-26 00:24:57 +0000267
Rafael Espindola9e3f84b2016-02-03 21:02:48 +0000268 if (IsRela) {
Rafael Espindola87d9f102016-03-11 12:19:05 +0000269 uintX_t VA;
270 if (Rel.UseSymVA)
271 VA = Sym->getVA<ELFT>(Rel.Addend);
272 else
273 VA = Rel.Addend;
274 reinterpret_cast<Elf_Rela *>(P)->r_addend = VA;
Rafael Espindola9e3f84b2016-02-03 21:02:48 +0000275 }
276
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000277 P->r_offset = Rel.getOffset();
Rafael Espindolade9857e2016-02-04 21:33:05 +0000278 uint32_t SymIdx = (!Rel.UseSymVA && Sym) ? Sym->DynsymIndex : 0;
279 P->setSymbolAndType(SymIdx, Rel.Type, Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000280 }
281}
282
George Rimar77b77792015-11-25 22:15:01 +0000283template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
Rui Ueyama5a0b2f72016-02-05 19:13:18 +0000284 return this->Header.sh_entsize * Relocs.size();
George Rimar77b77792015-11-25 22:15:01 +0000285}
286
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000287template <class ELFT> void RelocationSection<ELFT>::finalize() {
George Rimara07ff662015-12-21 10:12:06 +0000288 this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex
289 : Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000290 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
291}
292
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000293template <class ELFT>
294InterpSection<ELFT>::InterpSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000295 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000296 this->Header.sh_size = Config->DynamicLinker.size() + 1;
297 this->Header.sh_addralign = 1;
298}
299
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000300template <class ELFT>
301void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
302 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000303}
304
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000305template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000306 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
307}
308
Rafael Espindola35c6af32015-09-25 17:19:10 +0000309template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000310HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000311 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000312 this->Header.sh_entsize = sizeof(Elf_Word);
313 this->Header.sh_addralign = sizeof(Elf_Word);
314}
315
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000316static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000317 uint32_t H = 0;
318 for (char C : Name) {
319 H = (H << 4) + C;
320 uint32_t G = H & 0xf0000000;
321 if (G)
322 H ^= G >> 24;
323 H &= ~G;
324 }
325 return H;
326}
327
Rui Ueyama0db335f2015-10-07 16:58:54 +0000328template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000329 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000330
George Rimare9e1d322016-02-18 15:17:01 +0000331 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000332 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000333
334 // Create as many buckets as there are symbols.
335 // FIXME: This is simplistic. We can try to optimize it, but implementing
336 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000337 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000338 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
339}
340
341template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000342 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000343 auto *P = reinterpret_cast<Elf_Word *>(Buf);
344 *P++ = NumSymbols; // nbucket
345 *P++ = NumSymbols; // nchain
346
347 Elf_Word *Buckets = P;
348 Elf_Word *Chains = P + NumSymbols;
349
Rafael Espindolae2c24612016-01-29 01:24:25 +0000350 for (const std::pair<SymbolBody *, unsigned> &P :
351 Out<ELFT>::DynSymTab->getSymbols()) {
352 SymbolBody *Body = P.first;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000353 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000354 unsigned I = Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000355 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000356 Chains[I] = Buckets[Hash];
357 Buckets[Hash] = I;
358 }
359}
360
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000361static uint32_t hashGnu(StringRef Name) {
362 uint32_t H = 5381;
363 for (uint8_t C : Name)
364 H = (H << 5) + H + C;
365 return H;
366}
367
368template <class ELFT>
369GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000370 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000371 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000372 this->Header.sh_addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000373}
374
375template <class ELFT>
376unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
377 if (!NumHashed)
378 return 0;
379
380 // These values are prime numbers which are not greater than 2^(N-1) + 1.
381 // In result, for any particular NumHashed we return a prime number
382 // which is not greater than NumHashed.
383 static const unsigned Primes[] = {
384 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
385 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
386
387 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
388 array_lengthof(Primes) - 1)];
389}
390
391// Bloom filter estimation: at least 8 bits for each hashed symbol.
392// GNU Hash table requirement: it should be a power of 2,
393// the minimum value is 1, even for an empty table.
394// Expected results for a 32-bit target:
395// calcMaskWords(0..4) = 1
396// calcMaskWords(5..8) = 2
397// calcMaskWords(9..16) = 4
398// For a 64-bit target:
399// calcMaskWords(0..8) = 1
400// calcMaskWords(9..16) = 2
401// calcMaskWords(17..32) = 4
402template <class ELFT>
403unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
404 if (!NumHashed)
405 return 1;
406 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
407}
408
409template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000410 unsigned NumHashed = Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000411 NBuckets = calcNBuckets(NumHashed);
412 MaskWords = calcMaskWords(NumHashed);
413 // Second hash shift estimation: just predefined values.
414 Shift2 = ELFT::Is64Bits ? 6 : 5;
415
416 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
417 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
418 + sizeof(Elf_Off) * MaskWords // Bloom Filter
419 + sizeof(Elf_Word) * NBuckets // Hash Buckets
420 + sizeof(Elf_Word) * NumHashed; // Hash Values
421}
422
423template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
424 writeHeader(Buf);
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000425 if (Symbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000426 return;
427 writeBloomFilter(Buf);
428 writeHashTable(Buf);
429}
430
431template <class ELFT>
432void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
433 auto *P = reinterpret_cast<Elf_Word *>(Buf);
434 *P++ = NBuckets;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000435 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000436 *P++ = MaskWords;
437 *P++ = Shift2;
438 Buf = reinterpret_cast<uint8_t *>(P);
439}
440
441template <class ELFT>
442void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
443 unsigned C = sizeof(Elf_Off) * 8;
444
445 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama861c7312016-02-17 05:40:03 +0000446 for (const SymbolData &Sym : Symbols) {
447 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
448 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
449 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000450 Masks[Pos] |= V;
451 }
452 Buf += sizeof(Elf_Off) * MaskWords;
453}
454
455template <class ELFT>
456void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
457 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
458 Elf_Word *Values = Buckets + NBuckets;
459
460 int PrevBucket = -1;
461 int I = 0;
Rui Ueyama861c7312016-02-17 05:40:03 +0000462 for (const SymbolData &Sym : Symbols) {
463 int Bucket = Sym.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000464 assert(PrevBucket <= Bucket);
465 if (Bucket != PrevBucket) {
Rui Ueyama861c7312016-02-17 05:40:03 +0000466 Buckets[Bucket] = Sym.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000467 PrevBucket = Bucket;
468 if (I > 0)
469 Values[I - 1] |= 1;
470 }
Rui Ueyama861c7312016-02-17 05:40:03 +0000471 Values[I] = Sym.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000472 ++I;
473 }
474 if (I > 0)
475 Values[I - 1] |= 1;
476}
477
Rafael Espindola31f88882015-11-02 14:33:11 +0000478static bool includeInGnuHashTable(SymbolBody *B) {
Rui Ueyamac112c1b2016-01-29 02:17:01 +0000479 // Assume that includeInDynsym() is already checked.
Rafael Espindola31f88882015-11-02 14:33:11 +0000480 return !B->isUndefined();
481}
482
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000483// Add symbols to this symbol hash table. Note that this function
484// destructively sort a given vector -- which is needed because
485// GNU-style hash table places some sorting requirements.
Rafael Espindola35c6af32015-09-25 17:19:10 +0000486template <class ELFT>
Rafael Espindolae2c24612016-01-29 01:24:25 +0000487void GnuHashTableSection<ELFT>::addSymbols(
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000488 std::vector<std::pair<SymbolBody *, size_t>> &V) {
489 auto Mid = std::stable_partition(V.begin(), V.end(),
490 [](std::pair<SymbolBody *, size_t> &P) {
491 return !includeInGnuHashTable(P.first);
492 });
493 if (Mid == V.end())
Igor Kudrinf1d60292015-10-28 07:05:56 +0000494 return;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000495 for (auto I = Mid, E = V.end(); I != E; ++I) {
496 SymbolBody *B = I->first;
497 size_t StrOff = I->second;
498 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
499 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000500
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000501 unsigned NBuckets = calcNBuckets(Symbols.size());
502 std::stable_sort(Symbols.begin(), Symbols.end(),
Rui Ueyama861c7312016-02-17 05:40:03 +0000503 [&](const SymbolData &L, const SymbolData &R) {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000504 return L.Hash % NBuckets < R.Hash % NBuckets;
505 });
506
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000507 V.erase(Mid, V.end());
Rui Ueyama861c7312016-02-17 05:40:03 +0000508 for (const SymbolData &Sym : Symbols)
509 V.push_back({Sym.Body, Sym.STName});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000510}
511
512template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000513DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000514 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000515 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000516 Elf_Shdr &Header = this->Header;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000517 Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000518 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000519
520 // .dynamic section is not writable on MIPS.
521 // See "Special Section" in Chapter 4 in the following document:
522 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
523 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000524 Header.sh_flags = SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000525}
526
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000527template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000528 if (this->Header.sh_size)
529 return; // Already finalized.
530
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000531 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000532 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000533
Rafael Espindolae2c24612016-01-29 01:24:25 +0000534 auto Add = [=](Entry E) { Entries.push_back(E); };
535
536 // Add strings. We know that these are the last strings to be added to
Rafael Espindolade069362016-01-25 21:32:04 +0000537 // DynStrTab and doing this here allows this function to set DT_STRSZ.
538 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000539 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
540 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rafael Espindolade069362016-01-25 21:32:04 +0000541 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
542 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000543 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
544 if (!Config->SoName.empty())
545 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
546
Rafael Espindolade069362016-01-25 21:32:04 +0000547 Out<ELFT>::DynStrTab->finalize();
548
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000549 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindolade069362016-01-25 21:32:04 +0000550 bool IsRela = Out<ELFT>::RelaDyn->isRela();
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000551 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
552 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
553 Add({IsRela ? DT_RELAENT : DT_RELENT,
554 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000555 }
George Rimar648a2c32015-10-20 08:54:27 +0000556 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000557 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
558 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
559 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
560 Out<ELFT>::GotPlt});
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000561 Add({DT_PLTREL, uint64_t(Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000562 }
563
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000564 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
565 Add({DT_SYMENT, sizeof(Elf_Sym)});
566 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
567 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000568 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000569 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000570 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000571 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000572
Rafael Espindolade069362016-01-25 21:32:04 +0000573 if (PreInitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000574 Add({DT_PREINIT_ARRAY, PreInitArraySec});
575 Add({DT_PREINIT_ARRAYSZ, PreInitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000576 }
577 if (InitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000578 Add({DT_INIT_ARRAY, InitArraySec});
579 Add({DT_INIT_ARRAYSZ, (uintX_t)InitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000580 }
581 if (FiniArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000582 Add({DT_FINI_ARRAY, FiniArraySec});
583 Add({DT_FINI_ARRAYSZ, (uintX_t)FiniArraySec->getSize()});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000584 }
585
Rui Ueyama304d1352016-01-25 21:47:25 +0000586 if (SymbolBody *B = SymTab.find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000587 Add({DT_INIT, B});
Rui Ueyama304d1352016-01-25 21:47:25 +0000588 if (SymbolBody *B = SymTab.find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000589 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000590
Rafael Espindolade069362016-01-25 21:32:04 +0000591 uint32_t DtFlags = 0;
592 uint32_t DtFlags1 = 0;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000593 if (Config->Bsymbolic)
594 DtFlags |= DF_SYMBOLIC;
595 if (Config->ZNodelete)
596 DtFlags1 |= DF_1_NODELETE;
597 if (Config->ZNow) {
598 DtFlags |= DF_BIND_NOW;
599 DtFlags1 |= DF_1_NOW;
600 }
601 if (Config->ZOrigin) {
602 DtFlags |= DF_ORIGIN;
603 DtFlags1 |= DF_1_ORIGIN;
604 }
605
606 if (DtFlags)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000607 Add({DT_FLAGS, DtFlags});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000608 if (DtFlags1)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000609 Add({DT_FLAGS_1, DtFlags1});
Igor Kudrin304860a2015-11-12 04:39:49 +0000610
Ed Mastef5d3cf62016-01-06 15:52:27 +0000611 if (!Config->Entry.empty())
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000612 Add({DT_DEBUG, (uint64_t)0});
Ed Mastef5d3cf62016-01-06 15:52:27 +0000613
Igor Kudrin304860a2015-11-12 04:39:49 +0000614 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000615 Add({DT_MIPS_RLD_VERSION, 1});
616 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
617 Add({DT_MIPS_BASE_ADDRESS, (uintX_t)Target->getVAStart()});
618 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
619 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000620 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000621 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000622 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000623 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
624 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000625 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000626 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000627 }
628
Rafael Espindolade069362016-01-25 21:32:04 +0000629 // +1 for DT_NULL
630 Header.sh_size = (Entries.size() + 1) * Header.sh_entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000631}
632
633template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000634 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
635
Rafael Espindolade069362016-01-25 21:32:04 +0000636 for (const Entry &E : Entries) {
637 P->d_tag = E.Tag;
638 switch (E.Kind) {
639 case Entry::SecAddr:
640 P->d_un.d_ptr = E.OutSec->getVA();
641 break;
642 case Entry::SymAddr:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000643 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
Rafael Espindolade069362016-01-25 21:32:04 +0000644 break;
645 case Entry::PlainInt:
646 P->d_un.d_val = E.Val;
647 break;
648 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000649 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000650 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000651}
652
653template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000654EhFrameHeader<ELFT>::EhFrameHeader()
655 : OutputSectionBase<ELFT>(".eh_frame_hdr", llvm::ELF::SHT_PROGBITS,
656 SHF_ALLOC) {
657 // It's a 4 bytes of header + pointer to the contents of the .eh_frame section
658 // + the number of FDE pointers in the table.
659 this->Header.sh_size = 12;
660}
661
662// We have to get PC values of FDEs. They depend on relocations
663// which are target specific, so we run this code after performing
664// all relocations. We read the values from ouput buffer according to the
665// encoding given for FDEs. Return value is an offset to the initial PC value
666// for the FDE.
667template <class ELFT>
668typename EhFrameHeader<ELFT>::uintX_t
669EhFrameHeader<ELFT>::getFdePc(uintX_t EhVA, const FdeData &F) {
670 const endianness E = ELFT::TargetEndianness;
Simon Atanasyanea423e22016-03-02 05:38:42 +0000671 uint8_t Size = F.Enc & 0x7;
672 if (Size == DW_EH_PE_absptr)
673 Size = sizeof(uintX_t) == 8 ? DW_EH_PE_udata8 : DW_EH_PE_udata4;
674 uint64_t PC;
675 switch (Size) {
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000676 case DW_EH_PE_udata2:
Simon Atanasyanea423e22016-03-02 05:38:42 +0000677 PC = read16<E>(F.PCRel);
678 break;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000679 case DW_EH_PE_udata4:
Simon Atanasyanea423e22016-03-02 05:38:42 +0000680 PC = read32<E>(F.PCRel);
681 break;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000682 case DW_EH_PE_udata8:
Simon Atanasyanea423e22016-03-02 05:38:42 +0000683 PC = read64<E>(F.PCRel);
684 break;
685 default:
George Rimar57610422016-03-11 14:43:02 +0000686 fatal("unknown FDE size encoding");
George Rimarf6bc65a2016-01-15 13:34:52 +0000687 }
Simon Atanasyanea423e22016-03-02 05:38:42 +0000688 switch (F.Enc & 0x70) {
689 case DW_EH_PE_absptr:
690 return PC;
691 case DW_EH_PE_pcrel:
692 return PC + EhVA + F.Off + 8;
693 default:
George Rimar57610422016-03-11 14:43:02 +0000694 fatal("unknown FDE size relative encoding");
Simon Atanasyanea423e22016-03-02 05:38:42 +0000695 }
George Rimarf6bc65a2016-01-15 13:34:52 +0000696}
697
698template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
699 const endianness E = ELFT::TargetEndianness;
700
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000701 const uint8_t Header[] = {1, DW_EH_PE_pcrel | DW_EH_PE_sdata4,
702 DW_EH_PE_udata4,
703 DW_EH_PE_datarel | DW_EH_PE_sdata4};
George Rimarf6bc65a2016-01-15 13:34:52 +0000704 memcpy(Buf, Header, sizeof(Header));
705
706 uintX_t EhVA = Sec->getVA();
707 uintX_t VA = this->getVA();
708 uintX_t EhOff = EhVA - VA - 4;
709 write32<E>(Buf + 4, EhOff);
710 write32<E>(Buf + 8, this->FdeList.size());
711 Buf += 12;
712
713 // InitialPC -> Offset in .eh_frame, sorted by InitialPC.
714 std::map<uintX_t, size_t> PcToOffset;
715 for (const FdeData &F : FdeList)
716 PcToOffset[getFdePc(EhVA, F)] = F.Off;
717
718 for (auto &I : PcToOffset) {
719 // The first four bytes are an offset to the initial PC value for the FDE.
720 write32<E>(Buf, I.first - VA);
721 // The last four bytes are an offset to the FDE data itself.
722 write32<E>(Buf + 4, EhVA + I.second - VA);
723 Buf += 8;
724 }
725}
726
727template <class ELFT>
728void EhFrameHeader<ELFT>::assignEhFrame(EHOutputSection<ELFT> *Sec) {
George Rimar45ca88d2016-01-25 19:27:50 +0000729 assert((!this->Sec || this->Sec == Sec) &&
730 "multiple .eh_frame sections not supported for .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000731 Live = Config->EhFrameHdr;
732 this->Sec = Sec;
733}
734
735template <class ELFT>
736void EhFrameHeader<ELFT>::addFde(uint8_t Enc, size_t Off, uint8_t *PCRel) {
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000737 if (Live && (Enc & 0xF0) == DW_EH_PE_datarel)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000738 fatal("DW_EH_PE_datarel encoding unsupported for FDEs by .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000739 FdeList.push_back(FdeData{Enc, Off, PCRel});
740}
741
742template <class ELFT> void EhFrameHeader<ELFT>::reserveFde() {
743 // Each FDE entry is 8 bytes long:
744 // The first four bytes are an offset to the initial PC value for the FDE. The
745 // last four byte are an offset to the FDE data itself.
746 this->Header.sh_size += 8;
747}
748
749template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000750OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
751 : OutputSectionBase<ELFT>(Name, Type, Flags) {
752 if (Type == SHT_RELA)
753 this->Header.sh_entsize = sizeof(Elf_Rela);
754 else if (Type == SHT_REL)
755 this->Header.sh_entsize = sizeof(Elf_Rel);
756}
757
758template <class ELFT> void OutputSection<ELFT>::finalize() {
759 uint32_t Type = this->Header.sh_type;
760 if (Type != SHT_RELA && Type != SHT_REL)
761 return;
762 this->Header.sh_link = Out<ELFT>::SymTab->SectionIndex;
763 // sh_info for SHT_REL[A] sections should contain the section header index of
764 // the section to which the relocation applies.
765 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
766 this->Header.sh_info = S->OutSec->SectionIndex;
767}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000768
769template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000770void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000771 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000772 auto *S = cast<InputSection<ELFT>>(C);
773 Sections.push_back(S);
774 S->OutSec = this;
Rui Ueyama5ac58912016-02-24 00:38:18 +0000775 this->updateAlign(S->Align);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000776
777 uintX_t Off = this->Header.sh_size;
Rui Ueyama5ac58912016-02-24 00:38:18 +0000778 Off = alignTo(Off, S->Align);
Rui Ueyama40845e62015-12-26 05:51:07 +0000779 S->OutSecOff = Off;
780 Off += S->getSize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000781 this->Header.sh_size = Off;
782}
783
Rui Ueyamac4185702016-02-10 23:20:42 +0000784// If an input string is in the form of "foo.N" where N is a number,
785// return N. Otherwise, returns 65536, which is one greater than the
786// lowest priority.
787static int getPriority(StringRef S) {
788 size_t Pos = S.rfind('.');
789 if (Pos == StringRef::npos)
790 return 65536;
791 int V;
792 if (S.substr(Pos + 1).getAsInteger(10, V))
793 return 65536;
794 return V;
795}
796
Rui Ueyama5af83682016-02-11 23:41:38 +0000797// This function is called after we sort input sections
798// to update their offsets.
799template <class ELFT> void OutputSection<ELFT>::reassignOffsets() {
800 uintX_t Off = 0;
801 for (InputSection<ELFT> *S : Sections) {
Rui Ueyama5ac58912016-02-24 00:38:18 +0000802 Off = alignTo(Off, S->Align);
Rui Ueyama5af83682016-02-11 23:41:38 +0000803 S->OutSecOff = Off;
804 Off += S->getSize();
805 }
806 this->Header.sh_size = Off;
807}
808
Rui Ueyamac4185702016-02-10 23:20:42 +0000809// Sorts input sections by section name suffixes, so that .foo.N comes
810// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000811// We want to keep the original order if the priorities are the same
812// because the compiler keeps the original initialization order in a
813// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000814// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000815template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000816 // Sort sections by priority.
817 typedef std::pair<int, InputSection<ELFT> *> Pair;
Rui Ueyama704da022016-02-10 23:43:16 +0000818 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
819
Rui Ueyamac4185702016-02-10 23:20:42 +0000820 std::vector<Pair> V;
821 for (InputSection<ELFT> *S : Sections)
822 V.push_back({getPriority(S->getSectionName()), S});
Rui Ueyama704da022016-02-10 23:43:16 +0000823 std::stable_sort(V.begin(), V.end(), Comp);
Rui Ueyamac4185702016-02-10 23:20:42 +0000824 Sections.clear();
825 for (Pair &P : V)
826 Sections.push_back(P.second);
Rui Ueyama5af83682016-02-11 23:41:38 +0000827 reassignOffsets();
828}
Rui Ueyamac4185702016-02-10 23:20:42 +0000829
Rui Ueyama5af83682016-02-11 23:41:38 +0000830// Returns true if S matches /Filename.?\.o$/.
831static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
832 if (!S.endswith(".o"))
833 return false;
834 S = S.drop_back(2);
835 if (S.endswith(Filename))
836 return true;
837 return !S.empty() && S.drop_back().endswith(Filename);
838}
839
840static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
841static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
842
843// .ctors and .dtors are sorted by this priority from highest to lowest.
844//
845// 1. The section was contained in crtbegin (crtbegin contains
846// some sentinel value in its .ctors and .dtors so that the runtime
847// can find the beginning of the sections.)
848//
849// 2. The section has an optional priority value in the form of ".ctors.N"
850// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
851// they are compared as string rather than number.
852//
853// 3. The section is just ".ctors" or ".dtors".
854//
855// 4. The section was contained in crtend, which contains an end marker.
856//
857// In an ideal world, we don't need this function because .init_array and
858// .ctors are duplicate features (and .init_array is newer.) However, there
859// are too many real-world use cases of .ctors, so we had no choice to
860// support that with this rather ad-hoc semantics.
861template <class ELFT>
862static bool compCtors(const InputSection<ELFT> *A,
863 const InputSection<ELFT> *B) {
864 bool BeginA = isCrtbegin(A->getFile()->getName());
865 bool BeginB = isCrtbegin(B->getFile()->getName());
866 if (BeginA != BeginB)
867 return BeginA;
868 bool EndA = isCrtend(A->getFile()->getName());
869 bool EndB = isCrtend(B->getFile()->getName());
870 if (EndA != EndB)
871 return EndB;
872 StringRef X = A->getSectionName();
873 StringRef Y = B->getSectionName();
874 assert(X.startswith(".ctors") || X.startswith(".dtors"));
875 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
876 X = X.substr(6);
877 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000878 if (X.empty() && Y.empty())
879 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000880 return X < Y;
881}
882
883// Sorts input sections by the special rules for .ctors and .dtors.
884// Unfortunately, the rules are different from the one for .{init,fini}_array.
885// Read the comment above.
886template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
887 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
888 reassignOffsets();
Rui Ueyamac4185702016-02-10 23:20:42 +0000889}
890
Rui Ueyama34f29242015-10-13 19:51:57 +0000891// Returns true if a symbol can be replaced at load-time by a symbol
892// with the same name defined in other ELF executable or DSO.
Rafael Espindola67d72c02016-03-11 12:06:30 +0000893bool elf::canBePreempted(const SymbolBody &Body) {
894 if (Body.isLocal())
895 return false;
896
897 if (Body.isShared())
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000898 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000899
Rafael Espindola67d72c02016-03-11 12:06:30 +0000900 if (Body.isUndefined()) {
901 if (!Body.isWeak())
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000902 return true;
903
Rafael Espindola993f0272016-02-26 14:27:47 +0000904 // Ideally the static linker should see a definition for every symbol, but
905 // shared object are normally allowed to have undefined references that the
906 // static linker never sees a definition for.
907 if (Config->Shared)
908 return true;
909
910 // Otherwise, just resolve to 0.
911 return false;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000912 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000913 if (!Config->Shared)
914 return false;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000915 if (Body.getVisibility() != STV_DEFAULT)
George Rimar5c36e592016-02-02 09:28:53 +0000916 return false;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000917 if (Config->Bsymbolic || (Config->BsymbolicFunctions && Body.IsFunc))
George Rimar5c36e592016-02-02 09:28:53 +0000918 return false;
919 return true;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000920}
921
George Rimare2ee72b2016-02-26 14:48:31 +0000922static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) {
923 size_t I = 0;
924 for (; I + A.size() < Size; I += A.size())
925 memcpy(Buf + I, A.data(), A.size());
926 memcpy(Buf + I, A.data(), Size - I);
927}
928
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000929template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
George Rimare2ee72b2016-02-26 14:48:31 +0000930 ArrayRef<uint8_t> Filler = Script->getFiller(this->Name);
931 if (!Filler.empty())
932 fill(Buf, this->getSize(), Filler);
Rui Ueyamae9809502016-03-11 04:23:12 +0000933 if (Config->Threads) {
934 parallel_for_each(Sections.begin(), Sections.end(),
935 [=](InputSection<ELFT> *C) { C->writeTo(Buf); });
936 } else {
937 for (InputSection<ELFT> *C : Sections)
938 C->writeTo(Buf);
939 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000940}
941
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000942template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000943EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t Type,
944 uintX_t Flags)
George Rimarf6bc65a2016-01-15 13:34:52 +0000945 : OutputSectionBase<ELFT>(Name, Type, Flags) {
946 Out<ELFT>::EhFrameHdr->assignEhFrame(this);
947}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000948
949template <class ELFT>
950EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
951 : S(S), Index(Index) {}
952
953template <class ELFT> StringRef EHRegion<ELFT>::data() const {
954 ArrayRef<uint8_t> SecData = S->getSectionData();
955 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
956 size_t Start = Offsets[Index].first;
957 size_t End =
958 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
959 return StringRef((const char *)SecData.data() + Start, End - Start);
960}
961
962template <class ELFT>
963Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
964 : EHRegion<ELFT>(S, Index) {}
965
George Rimarf6bc65a2016-01-15 13:34:52 +0000966// Read a byte and advance D by one byte.
967static uint8_t readByte(ArrayRef<uint8_t> &D) {
968 if (D.empty())
George Rimar57610422016-03-11 14:43:02 +0000969 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000970 uint8_t B = D.front();
971 D = D.slice(1);
972 return B;
973}
974
975static void skipLeb128(ArrayRef<uint8_t> &D) {
976 while (!D.empty()) {
977 uint8_t Val = D.front();
978 D = D.slice(1);
979 if ((Val & 0x80) == 0)
980 return;
981 }
George Rimar57610422016-03-11 14:43:02 +0000982 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000983}
984
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000985template <class ELFT> static size_t getAugPSize(unsigned Enc) {
986 switch (Enc & 0x0f) {
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000987 case DW_EH_PE_absptr:
988 case DW_EH_PE_signed:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000989 return ELFT::Is64Bits ? 8 : 4;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000990 case DW_EH_PE_udata2:
991 case DW_EH_PE_sdata2:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000992 return 2;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000993 case DW_EH_PE_udata4:
994 case DW_EH_PE_sdata4:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000995 return 4;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000996 case DW_EH_PE_udata8:
997 case DW_EH_PE_sdata8:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000998 return 8;
999 }
George Rimar57610422016-03-11 14:43:02 +00001000 fatal("unknown FDE encoding");
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001001}
1002
1003template <class ELFT> static void skipAugP(ArrayRef<uint8_t> &D) {
1004 uint8_t Enc = readByte(D);
Rui Ueyamadbcfedb2016-02-09 21:46:11 +00001005 if ((Enc & 0xf0) == DW_EH_PE_aligned)
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001006 fatal("DW_EH_PE_aligned encoding is not supported");
1007 size_t Size = getAugPSize<ELFT>(Enc);
Rafael Espindola9e072d32016-02-09 22:47:34 +00001008 if (Size >= D.size())
George Rimar57610422016-03-11 14:43:02 +00001009 fatal("corrupted CIE");
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001010 D = D.slice(Size);
1011}
1012
George Rimarf6bc65a2016-01-15 13:34:52 +00001013template <class ELFT>
1014uint8_t EHOutputSection<ELFT>::getFdeEncoding(ArrayRef<uint8_t> D) {
Rui Ueyamabe748c22016-02-08 05:18:44 +00001015 if (D.size() < 8)
1016 fatal("CIE too small");
George Rimarf6bc65a2016-01-15 13:34:52 +00001017 D = D.slice(8);
1018
1019 uint8_t Version = readByte(D);
1020 if (Version != 1 && Version != 3)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001021 fatal("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
George Rimarf6bc65a2016-01-15 13:34:52 +00001022
Saleem Abdulrasoolc0571e12016-02-15 03:45:18 +00001023 const unsigned char *AugEnd = std::find(D.begin() + 1, D.end(), '\0');
Rui Ueyamabe748c22016-02-08 05:18:44 +00001024 if (AugEnd == D.end())
George Rimar57610422016-03-11 14:43:02 +00001025 fatal("corrupted CIE");
Saleem Abdulrasoolc0571e12016-02-15 03:45:18 +00001026 StringRef Aug(reinterpret_cast<const char *>(D.begin()), AugEnd - D.begin());
Rui Ueyamabe748c22016-02-08 05:18:44 +00001027 D = D.slice(Aug.size() + 1);
George Rimarf6bc65a2016-01-15 13:34:52 +00001028
1029 // Code alignment factor should always be 1 for .eh_frame.
1030 if (readByte(D) != 1)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001031 fatal("CIE code alignment must be 1");
Rui Ueyamabe748c22016-02-08 05:18:44 +00001032
1033 // Skip data alignment factor.
George Rimarf6bc65a2016-01-15 13:34:52 +00001034 skipLeb128(D);
1035
1036 // Skip the return address register. In CIE version 1 this is a single
1037 // byte. In CIE version 3 this is an unsigned LEB128.
1038 if (Version == 1)
1039 readByte(D);
1040 else
1041 skipLeb128(D);
1042
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001043 // We only care about an 'R' value, but other records may precede an 'R'
1044 // record. Records are not in TLV (type-length-value) format, so we need
1045 // to teach the linker how to skip records for each type.
Rui Ueyamad3bd97a2016-02-09 23:11:21 +00001046 for (char C : Aug) {
1047 if (C == 'R')
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001048 return readByte(D);
Rui Ueyamad3bd97a2016-02-09 23:11:21 +00001049 if (C == 'z') {
1050 skipLeb128(D);
1051 continue;
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001052 }
Rui Ueyamad3bd97a2016-02-09 23:11:21 +00001053 if (C == 'P') {
1054 skipAugP<ELFT>(D);
1055 continue;
1056 }
Simon Atanasyanea423e22016-03-02 05:38:42 +00001057 if (C == 'L') {
1058 readByte(D);
Rui Ueyamad3bd97a2016-02-09 23:11:21 +00001059 continue;
Simon Atanasyanea423e22016-03-02 05:38:42 +00001060 }
George Rimar57610422016-03-11 14:43:02 +00001061 fatal("unknown .eh_frame augmentation string: " + Aug);
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001062 }
Rui Ueyamadbcfedb2016-02-09 21:46:11 +00001063 return DW_EH_PE_absptr;
George Rimarf6bc65a2016-01-15 13:34:52 +00001064}
1065
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001066template <class ELFT>
Rui Ueyamac0c92602016-02-05 22:56:03 +00001067static typename ELFFile<ELFT>::uintX_t readEntryLength(ArrayRef<uint8_t> D) {
1068 const endianness E = ELFT::TargetEndianness;
Rui Ueyamac0c92602016-02-05 22:56:03 +00001069 if (D.size() < 4)
Rui Ueyama1b45cca2016-02-05 23:24:05 +00001070 fatal("CIE/FDE too small");
1071
1072 // First 4 bytes of CIE/FDE is the size of the record.
1073 // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead.
1074 uint64_t V = read32<E>(D.data());
1075 if (V < UINT32_MAX) {
1076 uint64_t Len = V + 4;
1077 if (Len > D.size())
Rui Ueyamac0c92602016-02-05 22:56:03 +00001078 fatal("CIE/FIE ends past the end of the section");
Rui Ueyama1b45cca2016-02-05 23:24:05 +00001079 return Len;
Rui Ueyamac0c92602016-02-05 22:56:03 +00001080 }
1081
1082 if (D.size() < 12)
Rui Ueyama1b45cca2016-02-05 23:24:05 +00001083 fatal("CIE/FDE too small");
1084 V = read64<E>(D.data() + 4);
1085 uint64_t Len = V + 12;
1086 if (Len < V || D.size() < Len)
Rui Ueyamac0c92602016-02-05 22:56:03 +00001087 fatal("CIE/FIE ends past the end of the section");
Rui Ueyama1b45cca2016-02-05 23:24:05 +00001088 return Len;
Rui Ueyamac0c92602016-02-05 22:56:03 +00001089}
1090
1091template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +00001092template <class RelTy>
1093void EHOutputSection<ELFT>::addSectionAux(EHInputSection<ELFT> *S,
1094 iterator_range<const RelTy *> Rels) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001095 const endianness E = ELFT::TargetEndianness;
1096
1097 S->OutSec = this;
Rui Ueyama5ac58912016-02-24 00:38:18 +00001098 this->updateAlign(S->Align);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001099 Sections.push_back(S);
1100
1101 ArrayRef<uint8_t> SecData = S->getSectionData();
1102 ArrayRef<uint8_t> D = SecData;
1103 uintX_t Offset = 0;
1104 auto RelI = Rels.begin();
1105 auto RelE = Rels.end();
1106
George Rimar147747a2016-01-02 16:55:01 +00001107 DenseMap<unsigned, unsigned> OffsetToIndex;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001108 while (!D.empty()) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001109 unsigned Index = S->Offsets.size();
1110 S->Offsets.push_back(std::make_pair(Offset, -1));
1111
Rui Ueyamac0c92602016-02-05 22:56:03 +00001112 uintX_t Length = readEntryLength<ELFT>(D);
George Rimarf6bc65a2016-01-15 13:34:52 +00001113 // If CIE/FDE data length is zero then Length is 4, this
1114 // shall be considered a terminator and processing shall end.
1115 if (Length == 4)
1116 break;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001117 StringRef Entry((const char *)D.data(), Length);
1118
1119 while (RelI != RelE && RelI->r_offset < Offset)
1120 ++RelI;
1121 uintX_t NextOffset = Offset + Length;
1122 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
1123
1124 uint32_t ID = read32<E>(D.data() + 4);
1125 if (ID == 0) {
1126 // CIE
1127 Cie<ELFT> C(S, Index);
George Rimarf6bc65a2016-01-15 13:34:52 +00001128 if (Config->EhFrameHdr)
1129 C.FdeEncoding = getFdeEncoding(D);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001130
Rafael Espindola156ed8d2016-02-10 13:19:32 +00001131 SymbolBody *Personality = nullptr;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001132 if (HasReloc) {
1133 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
Rafael Espindola67d72c02016-03-11 12:06:30 +00001134 Personality = &S->getFile()->getSymbolBody(SymIndex).repl();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001135 }
1136
Rafael Espindola156ed8d2016-02-10 13:19:32 +00001137 std::pair<StringRef, SymbolBody *> CieInfo(Entry, Personality);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001138 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
George Rimar147747a2016-01-02 16:55:01 +00001139 if (P.second) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001140 Cies.push_back(C);
Rui Ueyama489a8062016-01-14 20:53:50 +00001141 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
George Rimar147747a2016-01-02 16:55:01 +00001142 }
1143 OffsetToIndex[Offset] = P.first->second;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001144 } else {
1145 if (!HasReloc)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001146 fatal("FDE doesn't reference another section");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001147 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
Rafael Espindola1f5b70f2016-03-11 14:21:37 +00001148 if (Target && Target->Live) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001149 uint32_t CieOffset = Offset + 4 - ID;
George Rimar147747a2016-01-02 16:55:01 +00001150 auto I = OffsetToIndex.find(CieOffset);
1151 if (I == OffsetToIndex.end())
George Rimar777f9632016-03-12 08:31:34 +00001152 fatal("invalid CIE reference");
George Rimar147747a2016-01-02 16:55:01 +00001153 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
George Rimarf6bc65a2016-01-15 13:34:52 +00001154 Out<ELFT>::EhFrameHdr->reserveFde();
Rui Ueyama489a8062016-01-14 20:53:50 +00001155 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001156 }
1157 }
1158
1159 Offset = NextOffset;
1160 D = D.slice(Length);
1161 }
1162}
1163
1164template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001165void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1166 auto *S = cast<EHInputSection<ELFT>>(C);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001167 const Elf_Shdr *RelSec = S->RelocSection;
Rui Ueyama0de86c12016-01-27 22:23:44 +00001168 if (!RelSec) {
1169 addSectionAux(S, make_range<const Elf_Rela *>(nullptr, nullptr));
1170 return;
1171 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001172 ELFFile<ELFT> &Obj = S->getFile()->getObj();
1173 if (RelSec->sh_type == SHT_RELA)
Rui Ueyama0de86c12016-01-27 22:23:44 +00001174 addSectionAux(S, Obj.relas(RelSec));
1175 else
1176 addSectionAux(S, Obj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001177}
1178
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001179template <class ELFT>
1180static typename ELFFile<ELFT>::uintX_t writeAlignedCieOrFde(StringRef Data,
1181 uint8_t *Buf) {
1182 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1183 const endianness E = ELFT::TargetEndianness;
Rui Ueyama489a8062016-01-14 20:53:50 +00001184 uint64_t Len = alignTo(Data.size(), sizeof(uintX_t));
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001185 write32<E>(Buf, Len - 4);
1186 memcpy(Buf + 4, Data.data() + 4, Data.size() - 4);
1187 return Len;
1188}
1189
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001190template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1191 const endianness E = ELFT::TargetEndianness;
1192 size_t Offset = 0;
1193 for (const Cie<ELFT> &C : Cies) {
1194 size_t CieOffset = Offset;
1195
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001196 uintX_t CIELen = writeAlignedCieOrFde<ELFT>(C.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001197 C.S->Offsets[C.Index].second = Offset;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001198 Offset += CIELen;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001199
1200 for (const EHRegion<ELFT> &F : C.Fdes) {
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001201 uintX_t Len = writeAlignedCieOrFde<ELFT>(F.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001202 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001203 F.S->Offsets[F.Index].second = Offset;
George Rimarf6bc65a2016-01-15 13:34:52 +00001204 Out<ELFT>::EhFrameHdr->addFde(C.FdeEncoding, Offset, Buf + Offset + 8);
George Rimare72beba2015-12-21 09:38:59 +00001205 Offset += Len;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001206 }
1207 }
1208
1209 for (EHInputSection<ELFT> *S : Sections) {
1210 const Elf_Shdr *RelSec = S->RelocSection;
1211 if (!RelSec)
1212 continue;
1213 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1214 if (RelSec->sh_type == SHT_RELA)
1215 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1216 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001217 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001218 }
1219}
1220
1221template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001222MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +00001223 uintX_t Flags, uintX_t Alignment)
1224 : OutputSectionBase<ELFT>(Name, Type, Flags),
1225 Builder(llvm::StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001226
1227template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001228 if (shouldTailMerge()) {
1229 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001230 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001231 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001232 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001233 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1234 StringRef Data = P.first;
1235 memcpy(Buf + P.second, Data.data(), Data.size());
1236 }
1237}
1238
1239static size_t findNull(StringRef S, size_t EntSize) {
1240 // Optimize the common case.
1241 if (EntSize == 1)
1242 return S.find(0);
1243
1244 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1245 const char *B = S.begin() + I;
1246 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1247 return I;
1248 }
1249 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001250}
1251
1252template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001253void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1254 auto *S = cast<MergeInputSection<ELFT>>(C);
Rafael Espindolac159c962015-10-19 21:00:02 +00001255 S->OutSec = this;
Rui Ueyama5ac58912016-02-24 00:38:18 +00001256 this->updateAlign(S->Align);
Rafael Espindolac159c962015-10-19 21:00:02 +00001257
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001258 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001259 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001260 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001261
Rui Ueyamaead75fc2016-01-29 22:18:55 +00001262 // If this is of type string, the contents are null-terminated strings.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001263 if (this->Header.sh_flags & SHF_STRINGS) {
Rui Ueyamaad87ff72016-01-06 02:52:24 +00001264 uintX_t Offset = 0;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001265 while (!Data.empty()) {
1266 size_t End = findNull(Data, EntSize);
1267 if (End == StringRef::npos)
George Rimar777f9632016-03-12 08:31:34 +00001268 fatal("string is not null terminated");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001269 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001270 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001271 if (shouldTailMerge())
1272 OutputOffset = -1;
1273 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1274 uintX_t Size = End + EntSize;
1275 Data = Data.substr(Size);
1276 Offset += Size;
1277 }
Rui Ueyamaead75fc2016-01-29 22:18:55 +00001278 return;
1279 }
1280
1281 // If this is not of type string, every entry has the same size.
1282 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1283 StringRef Entry = Data.substr(I, EntSize);
1284 size_t OutputOffset = Builder.add(Entry);
1285 S->Offsets.push_back(std::make_pair(I, OutputOffset));
Rafael Espindolac159c962015-10-19 21:00:02 +00001286 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001287}
1288
1289template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001290unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1291 return Builder.getOffset(Val);
1292}
1293
1294template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1295 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1296}
1297
1298template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1299 if (shouldTailMerge())
1300 Builder.finalize();
1301 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001302}
1303
1304template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001305StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001306 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1307 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001308 Dynamic(Dynamic) {
1309 this->Header.sh_addralign = 1;
1310}
1311
Rafael Espindolae2c24612016-01-29 01:24:25 +00001312// Adds a string to the string table. If HashIt is true we hash and check for
1313// duplicates. It is optional because the name of global symbols are already
1314// uniqued and hashing them again has a big cost for a small value: uniquing
1315// them with some other string that happens to be the same.
1316template <class ELFT>
1317unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1318 if (HashIt) {
1319 auto R = StringMap.insert(std::make_pair(S, Size));
1320 if (!R.second)
1321 return R.first->second;
1322 }
1323 unsigned Ret = Size;
1324 Size += S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001325 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001326 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001327}
1328
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001329template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001330 // ELF string tables start with NUL byte, so advance the pointer by one.
1331 ++Buf;
1332 for (StringRef S : Strings) {
1333 memcpy(Buf, S.data(), S.size());
1334 Buf += S.size() + 1;
1335 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001336}
1337
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001338template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001339SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001340 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001341 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1342 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001343 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001344 StrTabSec(StrTabSec), Table(Table) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001345 this->Header.sh_entsize = sizeof(Elf_Sym);
Rui Ueyama5e378dd2016-01-29 22:18:57 +00001346 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +00001347}
1348
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001349// Orders symbols according to their positions in the GOT,
1350// in compliance with MIPS ABI rules.
1351// See "Global Offset Table" in Chapter 5 in the following document
1352// for detailed description:
1353// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rafael Espindolae2c24612016-01-29 01:24:25 +00001354static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
1355 const std::pair<SymbolBody *, unsigned> &R) {
1356 if (!L.first->isInGot() || !R.first->isInGot())
1357 return R.first->isInGot();
1358 return L.first->GotIndex < R.first->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001359}
1360
Rui Ueyama0db335f2015-10-07 16:58:54 +00001361template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001362 if (this->Header.sh_size)
1363 return; // Already finalized.
1364
Rui Ueyama0db335f2015-10-07 16:58:54 +00001365 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001366 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001367 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001368
George Rimar58941ee2016-02-25 08:23:37 +00001369 if (Config->Relocatable) {
1370 size_t I = NumLocals;
1371 for (const std::pair<SymbolBody *, size_t> &P : Symbols)
1372 P.first->DynsymIndex = ++I;
1373 return;
1374 }
1375
Igor Kudrinab665fc2015-10-20 21:47:58 +00001376 if (!StrTabSec.isDynamic()) {
1377 std::stable_sort(Symbols.begin(), Symbols.end(),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001378 [](const std::pair<SymbolBody *, unsigned> &L,
1379 const std::pair<SymbolBody *, unsigned> &R) {
1380 return getSymbolBinding(L.first) == STB_LOCAL &&
1381 getSymbolBinding(R.first) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001382 });
1383 return;
1384 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001385 if (Out<ELFT>::GnuHashTab)
1386 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1387 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001388 else if (Config->EMachine == EM_MIPS)
1389 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001390 size_t I = 0;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001391 for (const std::pair<SymbolBody *, size_t> &P : Symbols)
Rui Ueyama572a6f72016-01-29 01:49:33 +00001392 P.first->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001393}
1394
1395template <class ELFT>
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001396void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
1397 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
Rui Ueyama0db335f2015-10-07 16:58:54 +00001398}
1399
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001400template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001401 Buf += sizeof(Elf_Sym);
1402
1403 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1404 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001405 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1406 writeLocalSymbols(Buf);
1407
1408 writeGlobalSymbols(Buf);
1409}
1410
1411template <class ELFT>
1412void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1413 // Iterate over all input object files to copy their local symbols
1414 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001415 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001416 for (const std::pair<const Elf_Sym *, size_t> &P : File->KeptLocalSyms) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001417 const Elf_Sym *Sym = P.first;
Rafael Espindola444576d2015-10-09 19:25:07 +00001418
Rui Ueyamac55733e2015-09-30 00:54:29 +00001419 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001420 uintX_t VA = 0;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001421 if (Sym->st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001422 ESym->st_shndx = SHN_ABS;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001423 VA = Sym->st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001424 } else {
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001425 InputSectionBase<ELFT> *Section = File->getSection(*Sym);
Rafael Espindolac159c962015-10-19 21:00:02 +00001426 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1427 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001428 VA = Section->getOffset(*Sym);
Rafael Espindolae090fb22016-03-09 21:37:22 +00001429 VA += OutSec->getVA();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001430 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001431 ESym->st_name = P.second;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001432 ESym->st_size = Sym->st_size;
1433 ESym->setBindingAndType(Sym->getBinding(), Sym->getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001434 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001435 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001436 }
1437 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001438}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001439
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001440template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001441static const typename llvm::object::ELFFile<ELFT>::Elf_Sym *
1442getElfSym(SymbolBody &Body) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001443 if (auto *EBody = dyn_cast<DefinedElf<ELFT>>(&Body))
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001444 return &EBody->Sym;
1445 if (auto *EBody = dyn_cast<UndefinedElf<ELFT>>(&Body))
1446 return &EBody->Sym;
1447 return nullptr;
1448}
1449
1450template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001451void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001452 // Write the internal symbol table contents to the output symbol table
1453 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001454 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001455 for (const std::pair<SymbolBody *, size_t> &P : Symbols) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001456 SymbolBody *Body = P.first;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001457 size_t StrOff = P.second;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001458
Davide Italiano05920b142016-03-03 21:54:03 +00001459 uint8_t Type = STT_NOTYPE;
Rafael Espindola8614c562015-10-06 14:33:58 +00001460 uintX_t Size = 0;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001461 if (const Elf_Sym *InputSym = getElfSym<ELFT>(*Body)) {
1462 Type = InputSym->getType();
1463 Size = InputSym->st_size;
Rafael Espindola11191912015-12-24 16:23:37 +00001464 } else if (auto *C = dyn_cast<DefinedCommon>(Body)) {
1465 Type = STT_OBJECT;
1466 Size = C->Size;
Rafael Espindola8614c562015-10-06 14:33:58 +00001467 }
1468
Igor Kudrin853b88d2015-10-20 20:52:14 +00001469 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001470 ESym->st_size = Size;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001471 ESym->st_name = StrOff;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001472 ESym->setVisibility(Body->getVisibility());
Rui Ueyamab5a69702016-02-01 21:00:35 +00001473 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001474
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001475 if (const OutputSectionBase<ELFT> *OutSec = getOutputSection(Body))
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001476 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001477 else if (isa<DefinedRegular<ELFT>>(Body))
1478 ESym->st_shndx = SHN_ABS;
Simon Atanasyand040a582016-02-25 16:19:15 +00001479
1480 // On MIPS we need to mark symbol which has a PLT entry and requires pointer
1481 // equality by STO_MIPS_PLT flag. That is necessary to help dynamic linker
1482 // distinguish such symbols and MIPS lazy-binding stubs.
1483 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1484 if (Config->EMachine == EM_MIPS && Body->isInPlt() &&
1485 Body->NeedsCopyOrPltAddr)
Simon Atanasyanbea96982016-02-25 21:09:05 +00001486 ESym->st_other |= STO_MIPS_PLT;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001487 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001488 }
1489}
1490
Igor Kudrin853b88d2015-10-20 20:52:14 +00001491template <class ELFT>
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001492const OutputSectionBase<ELFT> *
1493SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1494 switch (Sym->kind()) {
1495 case SymbolBody::DefinedSyntheticKind:
1496 return &cast<DefinedSynthetic<ELFT>>(Sym)->Section;
1497 case SymbolBody::DefinedRegularKind: {
Rafael Espindola67d72c02016-03-11 12:06:30 +00001498 auto &D = cast<DefinedRegular<ELFT>>(Sym->repl());
1499 if (D.Section)
1500 return D.Section->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001501 break;
1502 }
1503 case SymbolBody::DefinedCommonKind:
1504 return Out<ELFT>::Bss;
1505 case SymbolBody::SharedKind:
1506 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1507 return Out<ELFT>::Bss;
1508 break;
1509 case SymbolBody::UndefinedElfKind:
1510 case SymbolBody::UndefinedKind:
1511 case SymbolBody::LazyKind:
1512 break;
1513 case SymbolBody::DefinedBitcodeKind:
George Rimar777f9632016-03-12 08:31:34 +00001514 llvm_unreachable("should have been replaced");
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001515 }
1516 return nullptr;
1517}
1518
1519template <class ELFT>
Igor Kudrin853b88d2015-10-20 20:52:14 +00001520uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001521 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001522 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1523 return STB_LOCAL;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001524 if (const Elf_Sym *ESym = getElfSym<ELFT>(*Body))
1525 return ESym->getBinding();
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001526 if (isa<DefinedSynthetic<ELFT>>(Body))
1527 return STB_LOCAL;
Igor Kudrin853b88d2015-10-20 20:52:14 +00001528 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1529}
1530
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001531template <class ELFT>
Rui Ueyama634ddf02016-03-11 20:51:53 +00001532BuildIdSection<ELFT>::BuildIdSection()
1533 : OutputSectionBase<ELFT>(".note.gnu.build-id", SHT_NOTE, SHF_ALLOC) {
1534 // 16 bytes for the note section header and 8 bytes for FNV1 hash.
1535 this->Header.sh_size = 24;
1536}
1537
1538template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
1539 const endianness E = ELFT::TargetEndianness;
1540 write32<E>(Buf, 4); // Name size
1541 write32<E>(Buf + 4, sizeof(Hash)); // Content size
1542 write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type
1543 memcpy(Buf + 12, "GNU", 4); // Name string
1544 HashBuf = Buf + 16;
1545}
1546
1547template <class ELFT> void BuildIdSection<ELFT>::update(ArrayRef<uint8_t> Buf) {
1548 // 64-bit FNV1 hash
1549 const uint64_t Prime = 0x100000001b3;
1550 for (uint8_t B : Buf) {
1551 Hash *= Prime;
1552 Hash ^= B;
1553 }
1554}
1555
1556template <class ELFT> void BuildIdSection<ELFT>::writeBuildId() {
1557 const endianness E = ELFT::TargetEndianness;
1558 write64<E>(HashBuf, Hash);
1559}
1560
1561template <class ELFT>
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001562MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1563 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1564 this->Header.sh_addralign = 4;
1565 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1566 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1567}
1568
1569template <class ELFT>
1570void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1571 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
1572 R->ri_gp_value = getMipsGpAddr<ELFT>();
Rui Ueyama70eed362016-01-06 22:42:43 +00001573 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001574}
1575
1576template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001577void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001578 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001579 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001580 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001581}
1582
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001583namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001584namespace elf {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001585template class OutputSectionBase<ELF32LE>;
1586template class OutputSectionBase<ELF32BE>;
1587template class OutputSectionBase<ELF64LE>;
1588template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001589
George Rimarf6bc65a2016-01-15 13:34:52 +00001590template class EhFrameHeader<ELF32LE>;
1591template class EhFrameHeader<ELF32BE>;
1592template class EhFrameHeader<ELF64LE>;
1593template class EhFrameHeader<ELF64BE>;
1594
George Rimar648a2c32015-10-20 08:54:27 +00001595template class GotPltSection<ELF32LE>;
1596template class GotPltSection<ELF32BE>;
1597template class GotPltSection<ELF64LE>;
1598template class GotPltSection<ELF64BE>;
1599
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001600template class GotSection<ELF32LE>;
1601template class GotSection<ELF32BE>;
1602template class GotSection<ELF64LE>;
1603template class GotSection<ELF64BE>;
1604
1605template class PltSection<ELF32LE>;
1606template class PltSection<ELF32BE>;
1607template class PltSection<ELF64LE>;
1608template class PltSection<ELF64BE>;
1609
1610template class RelocationSection<ELF32LE>;
1611template class RelocationSection<ELF32BE>;
1612template class RelocationSection<ELF64LE>;
1613template class RelocationSection<ELF64BE>;
1614
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001615template class InterpSection<ELF32LE>;
1616template class InterpSection<ELF32BE>;
1617template class InterpSection<ELF64LE>;
1618template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001619
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001620template class GnuHashTableSection<ELF32LE>;
1621template class GnuHashTableSection<ELF32BE>;
1622template class GnuHashTableSection<ELF64LE>;
1623template class GnuHashTableSection<ELF64BE>;
1624
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001625template class HashTableSection<ELF32LE>;
1626template class HashTableSection<ELF32BE>;
1627template class HashTableSection<ELF64LE>;
1628template class HashTableSection<ELF64BE>;
1629
1630template class DynamicSection<ELF32LE>;
1631template class DynamicSection<ELF32BE>;
1632template class DynamicSection<ELF64LE>;
1633template class DynamicSection<ELF64BE>;
1634
1635template class OutputSection<ELF32LE>;
1636template class OutputSection<ELF32BE>;
1637template class OutputSection<ELF64LE>;
1638template class OutputSection<ELF64BE>;
1639
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001640template class EHOutputSection<ELF32LE>;
1641template class EHOutputSection<ELF32BE>;
1642template class EHOutputSection<ELF64LE>;
1643template class EHOutputSection<ELF64BE>;
1644
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001645template class MipsReginfoOutputSection<ELF32LE>;
1646template class MipsReginfoOutputSection<ELF32BE>;
1647template class MipsReginfoOutputSection<ELF64LE>;
1648template class MipsReginfoOutputSection<ELF64BE>;
1649
Rafael Espindolac159c962015-10-19 21:00:02 +00001650template class MergeOutputSection<ELF32LE>;
1651template class MergeOutputSection<ELF32BE>;
1652template class MergeOutputSection<ELF64LE>;
1653template class MergeOutputSection<ELF64BE>;
1654
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001655template class StringTableSection<ELF32LE>;
1656template class StringTableSection<ELF32BE>;
1657template class StringTableSection<ELF64LE>;
1658template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001659
1660template class SymbolTableSection<ELF32LE>;
1661template class SymbolTableSection<ELF32BE>;
1662template class SymbolTableSection<ELF64LE>;
1663template class SymbolTableSection<ELF64BE>;
Rui Ueyama634ddf02016-03-11 20:51:53 +00001664
1665template class BuildIdSection<ELF32LE>;
1666template class BuildIdSection<ELF32BE>;
1667template class BuildIdSection<ELF64LE>;
1668template class BuildIdSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001669}
1670}