blob: 879478ef9daef5a8a1f665f107156b1391f05ecb [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;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +000019using namespace llvm::dwarf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000020using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000021using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000022using namespace llvm::ELF;
23
24using namespace lld;
25using namespace lld::elf2;
26
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000027template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +000028OutputSectionBase<ELFT>::OutputSectionBase(StringRef Name, uint32_t Type,
29 uintX_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000030 : Name(Name) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000031 memset(&Header, 0, sizeof(Elf_Shdr));
Rui Ueyamad97e5c42016-01-07 18:33:11 +000032 Header.sh_type = Type;
33 Header.sh_flags = Flags;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034}
35
Rafael Espindola35c6af32015-09-25 17:19:10 +000036template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +000037GotPltSection<ELFT>::GotPltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000038 : OutputSectionBase<ELFT>(".got.plt", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
George Rimar648a2c32015-10-20 08:54:27 +000039 this->Header.sh_addralign = sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000040}
41
42template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama724d6252016-01-29 01:49:32 +000043 Sym->GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
George Rimar648a2c32015-10-20 08:54:27 +000044 Entries.push_back(Sym);
45}
46
47template <class ELFT> bool GotPltSection<ELFT>::empty() const {
Igor Kudrin351b41d2015-11-16 17:44:08 +000048 return Entries.empty();
George Rimar648a2c32015-10-20 08:54:27 +000049}
50
George Rimar648a2c32015-10-20 08:54:27 +000051template <class ELFT> void GotPltSection<ELFT>::finalize() {
Igor Kudrin351b41d2015-11-16 17:44:08 +000052 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +000053 (Target->GotPltHeaderEntriesNum + Entries.size()) * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000054}
55
56template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +000057 Target->writeGotPltHeader(Buf);
Rui Ueyama724d6252016-01-29 01:49:32 +000058 Buf += Target->GotPltHeaderEntriesNum * sizeof(uintX_t);
George Rimar648a2c32015-10-20 08:54:27 +000059 for (const SymbolBody *B : Entries) {
Rui Ueyamab5a69702016-02-01 21:00:35 +000060 Target->writeGotPlt(Buf, B->getPltVA<ELFT>());
George Rimar648a2c32015-10-20 08:54:27 +000061 Buf += sizeof(uintX_t);
62 }
63}
64
65template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000066GotSection<ELFT>::GotSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +000067 : OutputSectionBase<ELFT>(".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE) {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000068 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +000069 this->Header.sh_flags |= SHF_MIPS_GPREL;
Rui Ueyama5f551ae2015-10-14 14:02:06 +000070 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +000071}
72
Rafael Espindola5805c4f2015-09-21 21:38:08 +000073template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody *Sym) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000074 Sym->GotIndex = Entries.size();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075 Entries.push_back(Sym);
George Rimar687138c2015-11-13 16:28:53 +000076}
77
Simon Atanasyan56ab5f02016-01-21 05:33:23 +000078template <class ELFT> void GotSection<ELFT>::addMipsLocalEntry() {
79 ++MipsLocalEntries;
80}
81
George Rimar90cd0a82015-12-01 19:20:26 +000082template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody *Sym) {
83 if (Sym->hasGlobalDynIndex())
84 return false;
Rui Ueyama724d6252016-01-29 01:49:32 +000085 Sym->GlobalDynIndex = Target->GotHeaderEntriesNum + Entries.size();
Michael J. Spencer627ae702015-11-13 00:28:34 +000086 // Global Dynamic TLS entries take two GOT slots.
George Rimar687138c2015-11-13 16:28:53 +000087 Entries.push_back(Sym);
88 Entries.push_back(nullptr);
George Rimar90cd0a82015-12-01 19:20:26 +000089 return true;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000090}
91
Rui Ueyama0e53c7d2016-02-05 00:10:02 +000092// Reserves TLS entries for a TLS module ID and a TLS block offset.
93// In total it takes two GOT slots.
94template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
95 if (TlsIndexOff != uint32_t(-1))
George Rimarb17f7392015-12-01 18:24:07 +000096 return false;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +000097 TlsIndexOff = Entries.size() * sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +000098 Entries.push_back(nullptr);
99 Entries.push_back(nullptr);
George Rimarb17f7392015-12-01 18:24:07 +0000100 return true;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000101}
102
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103template <class ELFT>
104typename GotSection<ELFT>::uintX_t
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000105GotSection<ELFT>::getMipsLocalFullAddr(const SymbolBody &B) {
Rui Ueyamab5a69702016-02-01 21:00:35 +0000106 return getMipsLocalEntryAddr(B.getVA<ELFT>());
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000107}
108
109template <class ELFT>
110typename GotSection<ELFT>::uintX_t
111GotSection<ELFT>::getMipsLocalPageAddr(uintX_t EntryValue) {
112 // Initialize the entry by the %hi(EntryValue) expression
113 // but without right-shifting.
114 return getMipsLocalEntryAddr((EntryValue + 0x8000) & ~0xffff);
115}
116
117template <class ELFT>
118typename GotSection<ELFT>::uintX_t
119GotSection<ELFT>::getMipsLocalEntryAddr(uintX_t EntryValue) {
Rui Ueyama724d6252016-01-29 01:49:32 +0000120 size_t NewIndex = Target->GotHeaderEntriesNum + MipsLocalGotPos.size();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000121 auto P = MipsLocalGotPos.insert(std::make_pair(EntryValue, NewIndex));
122 assert(!P.second || MipsLocalGotPos.size() <= MipsLocalEntries);
123 return this->getVA() + P.first->second * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000124}
125
Igor Kudrin304860a2015-11-12 04:39:49 +0000126template <class ELFT>
George Rimar90cd0a82015-12-01 19:20:26 +0000127typename GotSection<ELFT>::uintX_t
128GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
129 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
130}
131
132template <class ELFT>
Igor Kudrin304860a2015-11-12 04:39:49 +0000133const SymbolBody *GotSection<ELFT>::getMipsFirstGlobalEntry() const {
134 return Entries.empty() ? nullptr : Entries.front();
135}
136
137template <class ELFT>
138unsigned GotSection<ELFT>::getMipsLocalEntriesNum() const {
Rui Ueyama724d6252016-01-29 01:49:32 +0000139 return Target->GotHeaderEntriesNum + MipsLocalEntries;
Igor Kudrin304860a2015-11-12 04:39:49 +0000140}
141
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000142template <class ELFT> void GotSection<ELFT>::finalize() {
143 this->Header.sh_size =
Rui Ueyama724d6252016-01-29 01:49:32 +0000144 (Target->GotHeaderEntriesNum + MipsLocalEntries + Entries.size()) *
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000145 sizeof(uintX_t);
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000146}
147
Rafael Espindolaa6627382015-10-06 23:56:53 +0000148template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000149 Target->writeGotHeader(Buf);
Rui Ueyama5cbf5d22016-02-02 02:29:03 +0000150 for (std::pair<uintX_t, size_t> &L : MipsLocalGotPos) {
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000151 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
152 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, L.first);
153 }
Rui Ueyama724d6252016-01-29 01:49:32 +0000154 Buf += Target->GotHeaderEntriesNum * sizeof(uintX_t);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000155 Buf += MipsLocalEntries * sizeof(uintX_t);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000156 for (const SymbolBody *B : Entries) {
Rafael Espindolae782f672015-10-07 03:56:05 +0000157 uint8_t *Entry = Buf;
158 Buf += sizeof(uintX_t);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000159 if (!B)
160 continue;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000161 // MIPS has special rules to fill up GOT entries.
162 // See "Global Offset Table" in Chapter 5 in the following document
163 // for detailed description:
164 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
165 // As the first approach, we can just store addresses for all symbols.
166 if (Config->EMachine != EM_MIPS && canBePreempted(B, false))
Rafael Espindolaa6627382015-10-06 23:56:53 +0000167 continue; // The dynamic linker will take care of it.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000168 uintX_t VA = B->getVA<ELFT>();
Rafael Espindolae782f672015-10-07 03:56:05 +0000169 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
Rafael Espindolaa6627382015-10-06 23:56:53 +0000170 }
171}
172
Rafael Espindola35c6af32015-09-25 17:19:10 +0000173template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000174PltSection<ELFT>::PltSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000175 : OutputSectionBase<ELFT>(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000176 this->Header.sh_addralign = 16;
177}
178
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000179template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama242ddf42015-10-12 18:56:36 +0000180 size_t Off = 0;
Rui Ueyama9398f862016-01-29 04:15:02 +0000181 if (Target->UseLazyBinding) {
Rui Ueyama74937fc2016-02-02 02:53:58 +0000182 // At beginning of PLT, we have code to call the dynamic linker
183 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama900e2d22016-01-29 03:51:49 +0000184 Target->writePltZero(Buf);
Rui Ueyama62515452016-01-29 03:00:32 +0000185 Off += Target->PltZeroSize;
George Rimar648a2c32015-10-20 08:54:27 +0000186 }
George Rimarfb5d7f22015-11-26 19:58:51 +0000187 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000188 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000189 unsigned RelOff = I.second;
Rui Ueyamab5a69702016-02-01 21:00:35 +0000190 uint64_t Got =
191 Target->UseLazyBinding ? B->getGotPltVA<ELFT>() : B->getGotVA<ELFT>();
Rui Ueyama242ddf42015-10-12 18:56:36 +0000192 uint64_t Plt = this->getVA() + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000193 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000194 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000195 }
196}
197
198template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody *Sym) {
Rui Ueyama49c68a72015-10-09 00:42:06 +0000199 Sym->PltIndex = Entries.size();
Rui Ueyama724d6252016-01-29 01:49:32 +0000200 unsigned RelOff = Target->UseLazyBinding
George Rimar77b77792015-11-25 22:15:01 +0000201 ? Out<ELFT>::RelaPlt->getRelocOffset()
202 : Out<ELFT>::RelaDyn->getRelocOffset();
203 Entries.push_back(std::make_pair(Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000204}
205
George Rimar648a2c32015-10-20 08:54:27 +0000206template <class ELFT> void PltSection<ELFT>::finalize() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000207 this->Header.sh_size =
Rui Ueyama62515452016-01-29 03:00:32 +0000208 Target->PltZeroSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000209}
210
211template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000212RelocationSection<ELFT>::RelocationSection(StringRef Name, bool IsRela)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000213 : OutputSectionBase<ELFT>(Name, IsRela ? SHT_RELA : SHT_REL, SHF_ALLOC),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000214 IsRela(IsRela) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000215 this->Header.sh_entsize = IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000216 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000217}
218
George Rimar5828c232015-11-30 17:49:19 +0000219template <class ELFT>
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000220void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
221 SymbolBody *Sym = Reloc.Sym;
222 if (!Reloc.UseSymVA && Sym)
Rafael Espindolaabebed92016-02-05 15:27:15 +0000223 Sym->MustBeInDynSym = true;
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000224 Relocs.push_back(Reloc);
225}
226
227template <class ELFT>
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000228typename ELFFile<ELFT>::uintX_t DynamicReloc<ELFT>::getOffset() const {
229 switch (OKind) {
230 case Off_GTlsIndex:
Rafael Espindolade9857e2016-02-04 21:33:05 +0000231 return Out<ELFT>::Got->getGlobalDynAddr(*Sym);
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000232 case Off_GTlsOffset:
Rafael Espindolade9857e2016-02-04 21:33:05 +0000233 return Out<ELFT>::Got->getGlobalDynAddr(*Sym) + sizeof(uintX_t);
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000234 case Off_LTlsIndex:
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000235 return Out<ELFT>::Got->getTlsIndexVA();
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000236 case Off_Sec:
237 return OffsetSec->getOffset(OffsetInSec) + OffsetSec->OutSec->getVA();
238 case Off_Bss:
Rafael Espindolade9857e2016-02-04 21:33:05 +0000239 return cast<SharedSymbol<ELFT>>(Sym)->OffsetInBss + Out<ELFT>::Bss->getVA();
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000240 case Off_Got:
Rafael Espindolade9857e2016-02-04 21:33:05 +0000241 return Sym->getGotVA<ELFT>();
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000242 case Off_GotPlt:
Rafael Espindolade9857e2016-02-04 21:33:05 +0000243 return Sym->getGotPltVA<ELFT>();
George Rimar5828c232015-11-30 17:49:19 +0000244 }
Rafael Espindolade9857e2016-02-04 21:33:05 +0000245 llvm_unreachable("Invalid offset kind");
George Rimar5828c232015-11-30 17:49:19 +0000246}
247
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249 for (const DynamicReloc<ELFT> &Rel : Relocs) {
Rafael Espindola50534c22015-09-22 17:49:38 +0000250 auto *P = reinterpret_cast<Elf_Rel *>(Buf);
Rui Ueyamac7b073a2016-01-05 16:35:48 +0000251 Buf += IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Rafael Espindolade9857e2016-02-04 21:33:05 +0000252 SymbolBody *Sym = Rel.Sym;
Rui Ueyamab0210e832016-01-26 00:24:57 +0000253
Rafael Espindola9e3f84b2016-02-03 21:02:48 +0000254 if (IsRela) {
Rafael Espindolade9857e2016-02-04 21:33:05 +0000255 uintX_t VA = 0;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000256 if (Rel.UseSymVA)
Rafael Espindolade9857e2016-02-04 21:33:05 +0000257 VA = Sym->getVA<ELFT>();
Rafael Espindola435c00f2016-02-23 20:19:44 +0000258 else if (Rel.TargetSec)
Rafael Espindolade9857e2016-02-04 21:33:05 +0000259 VA = Rel.TargetSec->getOffset(Rel.OffsetInTargetSec) +
260 Rel.TargetSec->OutSec->getVA();
261 reinterpret_cast<Elf_Rela *>(P)->r_addend = Rel.Addend + VA;
Rafael Espindola9e3f84b2016-02-03 21:02:48 +0000262 }
263
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000264 P->r_offset = Rel.getOffset();
Rafael Espindolade9857e2016-02-04 21:33:05 +0000265 uint32_t SymIdx = (!Rel.UseSymVA && Sym) ? Sym->DynsymIndex : 0;
266 P->setSymbolAndType(SymIdx, Rel.Type, Config->Mips64EL);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000267 }
268}
269
George Rimar77b77792015-11-25 22:15:01 +0000270template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
Rui Ueyama5a0b2f72016-02-05 19:13:18 +0000271 return this->Header.sh_entsize * Relocs.size();
George Rimar77b77792015-11-25 22:15:01 +0000272}
273
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000274template <class ELFT> void RelocationSection<ELFT>::finalize() {
George Rimara07ff662015-12-21 10:12:06 +0000275 this->Header.sh_link = Static ? Out<ELFT>::SymTab->SectionIndex
276 : Out<ELFT>::DynSymTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000277 this->Header.sh_size = Relocs.size() * this->Header.sh_entsize;
278}
279
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000280template <class ELFT>
281InterpSection<ELFT>::InterpSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000282 : OutputSectionBase<ELFT>(".interp", SHT_PROGBITS, SHF_ALLOC) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000283 this->Header.sh_size = Config->DynamicLinker.size() + 1;
284 this->Header.sh_addralign = 1;
285}
286
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000287template <class ELFT>
288void OutputSectionBase<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
289 *SHdr = Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000290}
291
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000292template <class ELFT> void InterpSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000293 memcpy(Buf, Config->DynamicLinker.data(), Config->DynamicLinker.size());
294}
295
Rafael Espindola35c6af32015-09-25 17:19:10 +0000296template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000297HashTableSection<ELFT>::HashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000298 : OutputSectionBase<ELFT>(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola35c6af32015-09-25 17:19:10 +0000299 this->Header.sh_entsize = sizeof(Elf_Word);
300 this->Header.sh_addralign = sizeof(Elf_Word);
301}
302
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000303static uint32_t hashSysv(StringRef Name) {
Rui Ueyama5f1eee1a2015-10-15 21:27:17 +0000304 uint32_t H = 0;
305 for (char C : Name) {
306 H = (H << 4) + C;
307 uint32_t G = H & 0xf0000000;
308 if (G)
309 H ^= G >> 24;
310 H &= ~G;
311 }
312 return H;
313}
314
Rui Ueyama0db335f2015-10-07 16:58:54 +0000315template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000316 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000317
George Rimare9e1d322016-02-18 15:17:01 +0000318 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000319 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000320
321 // Create as many buckets as there are symbols.
322 // FIXME: This is simplistic. We can try to optimize it, but implementing
323 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000324 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000325 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
326}
327
328template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000329 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000330 auto *P = reinterpret_cast<Elf_Word *>(Buf);
331 *P++ = NumSymbols; // nbucket
332 *P++ = NumSymbols; // nchain
333
334 Elf_Word *Buckets = P;
335 Elf_Word *Chains = P + NumSymbols;
336
Rafael Espindolae2c24612016-01-29 01:24:25 +0000337 for (const std::pair<SymbolBody *, unsigned> &P :
338 Out<ELFT>::DynSymTab->getSymbols()) {
339 SymbolBody *Body = P.first;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000340 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000341 unsigned I = Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000342 uint32_t Hash = hashSysv(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000343 Chains[I] = Buckets[Hash];
344 Buckets[Hash] = I;
345 }
346}
347
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000348static uint32_t hashGnu(StringRef Name) {
349 uint32_t H = 5381;
350 for (uint8_t C : Name)
351 H = (H << 5) + H + C;
352 return H;
353}
354
355template <class ELFT>
356GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyamacf07a312016-01-06 22:53:58 +0000357 : OutputSectionBase<ELFT>(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000358 this->Header.sh_entsize = ELFT::Is64Bits ? 0 : 4;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000359 this->Header.sh_addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000360}
361
362template <class ELFT>
363unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
364 if (!NumHashed)
365 return 0;
366
367 // These values are prime numbers which are not greater than 2^(N-1) + 1.
368 // In result, for any particular NumHashed we return a prime number
369 // which is not greater than NumHashed.
370 static const unsigned Primes[] = {
371 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
372 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
373
374 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
375 array_lengthof(Primes) - 1)];
376}
377
378// Bloom filter estimation: at least 8 bits for each hashed symbol.
379// GNU Hash table requirement: it should be a power of 2,
380// the minimum value is 1, even for an empty table.
381// Expected results for a 32-bit target:
382// calcMaskWords(0..4) = 1
383// calcMaskWords(5..8) = 2
384// calcMaskWords(9..16) = 4
385// For a 64-bit target:
386// calcMaskWords(0..8) = 1
387// calcMaskWords(9..16) = 2
388// calcMaskWords(17..32) = 4
389template <class ELFT>
390unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
391 if (!NumHashed)
392 return 1;
393 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
394}
395
396template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000397 unsigned NumHashed = Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000398 NBuckets = calcNBuckets(NumHashed);
399 MaskWords = calcMaskWords(NumHashed);
400 // Second hash shift estimation: just predefined values.
401 Shift2 = ELFT::Is64Bits ? 6 : 5;
402
403 this->Header.sh_link = Out<ELFT>::DynSymTab->SectionIndex;
404 this->Header.sh_size = sizeof(Elf_Word) * 4 // Header
405 + sizeof(Elf_Off) * MaskWords // Bloom Filter
406 + sizeof(Elf_Word) * NBuckets // Hash Buckets
407 + sizeof(Elf_Word) * NumHashed; // Hash Values
408}
409
410template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
411 writeHeader(Buf);
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000412 if (Symbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000413 return;
414 writeBloomFilter(Buf);
415 writeHashTable(Buf);
416}
417
418template <class ELFT>
419void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
420 auto *P = reinterpret_cast<Elf_Word *>(Buf);
421 *P++ = NBuckets;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000422 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000423 *P++ = MaskWords;
424 *P++ = Shift2;
425 Buf = reinterpret_cast<uint8_t *>(P);
426}
427
428template <class ELFT>
429void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
430 unsigned C = sizeof(Elf_Off) * 8;
431
432 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama861c7312016-02-17 05:40:03 +0000433 for (const SymbolData &Sym : Symbols) {
434 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
435 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
436 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000437 Masks[Pos] |= V;
438 }
439 Buf += sizeof(Elf_Off) * MaskWords;
440}
441
442template <class ELFT>
443void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
444 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
445 Elf_Word *Values = Buckets + NBuckets;
446
447 int PrevBucket = -1;
448 int I = 0;
Rui Ueyama861c7312016-02-17 05:40:03 +0000449 for (const SymbolData &Sym : Symbols) {
450 int Bucket = Sym.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000451 assert(PrevBucket <= Bucket);
452 if (Bucket != PrevBucket) {
Rui Ueyama861c7312016-02-17 05:40:03 +0000453 Buckets[Bucket] = Sym.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000454 PrevBucket = Bucket;
455 if (I > 0)
456 Values[I - 1] |= 1;
457 }
Rui Ueyama861c7312016-02-17 05:40:03 +0000458 Values[I] = Sym.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000459 ++I;
460 }
461 if (I > 0)
462 Values[I - 1] |= 1;
463}
464
Rafael Espindola31f88882015-11-02 14:33:11 +0000465static bool includeInGnuHashTable(SymbolBody *B) {
Rui Ueyamac112c1b2016-01-29 02:17:01 +0000466 // Assume that includeInDynsym() is already checked.
Rafael Espindola31f88882015-11-02 14:33:11 +0000467 return !B->isUndefined();
468}
469
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000470// Add symbols to this symbol hash table. Note that this function
471// destructively sort a given vector -- which is needed because
472// GNU-style hash table places some sorting requirements.
Rafael Espindola35c6af32015-09-25 17:19:10 +0000473template <class ELFT>
Rafael Espindolae2c24612016-01-29 01:24:25 +0000474void GnuHashTableSection<ELFT>::addSymbols(
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000475 std::vector<std::pair<SymbolBody *, size_t>> &V) {
476 auto Mid = std::stable_partition(V.begin(), V.end(),
477 [](std::pair<SymbolBody *, size_t> &P) {
478 return !includeInGnuHashTable(P.first);
479 });
480 if (Mid == V.end())
Igor Kudrinf1d60292015-10-28 07:05:56 +0000481 return;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000482 for (auto I = Mid, E = V.end(); I != E; ++I) {
483 SymbolBody *B = I->first;
484 size_t StrOff = I->second;
485 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
486 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000487
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000488 unsigned NBuckets = calcNBuckets(Symbols.size());
489 std::stable_sort(Symbols.begin(), Symbols.end(),
Rui Ueyama861c7312016-02-17 05:40:03 +0000490 [&](const SymbolData &L, const SymbolData &R) {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000491 return L.Hash % NBuckets < R.Hash % NBuckets;
492 });
493
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000494 V.erase(Mid, V.end());
Rui Ueyama861c7312016-02-17 05:40:03 +0000495 for (const SymbolData &Sym : Symbols)
496 V.push_back({Sym.Body, Sym.STName});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000497}
498
499template <class ELFT>
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000500DynamicSection<ELFT>::DynamicSection(SymbolTable<ELFT> &SymTab)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000501 : OutputSectionBase<ELFT>(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE),
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000502 SymTab(SymTab) {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000503 Elf_Shdr &Header = this->Header;
Rui Ueyama5e378dd2016-01-29 22:18:57 +0000504 Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000505 Header.sh_entsize = ELFT::Is64Bits ? 16 : 8;
Igor Kudrin304860a2015-11-12 04:39:49 +0000506
507 // .dynamic section is not writable on MIPS.
508 // See "Special Section" in Chapter 4 in the following document:
509 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
510 if (Config->EMachine == EM_MIPS)
Rui Ueyamacf07a312016-01-06 22:53:58 +0000511 Header.sh_flags = SHF_ALLOC;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000512}
513
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000514template <class ELFT> void DynamicSection<ELFT>::finalize() {
Michael J. Spencer52bf0eb2015-10-01 21:15:02 +0000515 if (this->Header.sh_size)
516 return; // Already finalized.
517
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000518 Elf_Shdr &Header = this->Header;
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000519 Header.sh_link = Out<ELFT>::DynStrTab->SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000520
Rafael Espindolae2c24612016-01-29 01:24:25 +0000521 auto Add = [=](Entry E) { Entries.push_back(E); };
522
523 // Add strings. We know that these are the last strings to be added to
Rafael Espindolade069362016-01-25 21:32:04 +0000524 // DynStrTab and doing this here allows this function to set DT_STRSZ.
525 if (!Config->RPath.empty())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000526 Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
527 Out<ELFT>::DynStrTab->addString(Config->RPath)});
Rafael Espindolade069362016-01-25 21:32:04 +0000528 for (const std::unique_ptr<SharedFile<ELFT>> &F : SymTab.getSharedFiles())
529 if (F->isNeeded())
Rafael Espindolae2c24612016-01-29 01:24:25 +0000530 Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
531 if (!Config->SoName.empty())
532 Add({DT_SONAME, Out<ELFT>::DynStrTab->addString(Config->SoName)});
533
Rafael Espindolade069362016-01-25 21:32:04 +0000534 Out<ELFT>::DynStrTab->finalize();
535
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000536 if (Out<ELFT>::RelaDyn->hasRelocs()) {
Rafael Espindolade069362016-01-25 21:32:04 +0000537 bool IsRela = Out<ELFT>::RelaDyn->isRela();
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000538 Add({IsRela ? DT_RELA : DT_REL, Out<ELFT>::RelaDyn});
539 Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
540 Add({IsRela ? DT_RELAENT : DT_RELENT,
541 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000542 }
George Rimar648a2c32015-10-20 08:54:27 +0000543 if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000544 Add({DT_JMPREL, Out<ELFT>::RelaPlt});
545 Add({DT_PLTRELSZ, Out<ELFT>::RelaPlt->getSize()});
546 Add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
547 Out<ELFT>::GotPlt});
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000548 Add({DT_PLTREL, uint64_t(Out<ELFT>::RelaPlt->isRela() ? DT_RELA : DT_REL)});
George Rimar648a2c32015-10-20 08:54:27 +0000549 }
550
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000551 Add({DT_SYMTAB, Out<ELFT>::DynSymTab});
552 Add({DT_SYMENT, sizeof(Elf_Sym)});
553 Add({DT_STRTAB, Out<ELFT>::DynStrTab});
554 Add({DT_STRSZ, Out<ELFT>::DynStrTab->getSize()});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000555 if (Out<ELFT>::GnuHashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000556 Add({DT_GNU_HASH, Out<ELFT>::GnuHashTab});
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000557 if (Out<ELFT>::HashTab)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000558 Add({DT_HASH, Out<ELFT>::HashTab});
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000559
Rafael Espindolade069362016-01-25 21:32:04 +0000560 if (PreInitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000561 Add({DT_PREINIT_ARRAY, PreInitArraySec});
562 Add({DT_PREINIT_ARRAYSZ, PreInitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000563 }
564 if (InitArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000565 Add({DT_INIT_ARRAY, InitArraySec});
566 Add({DT_INIT_ARRAYSZ, (uintX_t)InitArraySec->getSize()});
Rafael Espindolade069362016-01-25 21:32:04 +0000567 }
568 if (FiniArraySec) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000569 Add({DT_FINI_ARRAY, FiniArraySec});
570 Add({DT_FINI_ARRAYSZ, (uintX_t)FiniArraySec->getSize()});
Rui Ueyama7de3f372015-10-01 19:36:04 +0000571 }
572
Rui Ueyama304d1352016-01-25 21:47:25 +0000573 if (SymbolBody *B = SymTab.find(Config->Init))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000574 Add({DT_INIT, B});
Rui Ueyama304d1352016-01-25 21:47:25 +0000575 if (SymbolBody *B = SymTab.find(Config->Fini))
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000576 Add({DT_FINI, B});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000577
Rafael Espindolade069362016-01-25 21:32:04 +0000578 uint32_t DtFlags = 0;
579 uint32_t DtFlags1 = 0;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000580 if (Config->Bsymbolic)
581 DtFlags |= DF_SYMBOLIC;
582 if (Config->ZNodelete)
583 DtFlags1 |= DF_1_NODELETE;
584 if (Config->ZNow) {
585 DtFlags |= DF_BIND_NOW;
586 DtFlags1 |= DF_1_NOW;
587 }
588 if (Config->ZOrigin) {
589 DtFlags |= DF_ORIGIN;
590 DtFlags1 |= DF_1_ORIGIN;
591 }
592
593 if (DtFlags)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000594 Add({DT_FLAGS, DtFlags});
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000595 if (DtFlags1)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000596 Add({DT_FLAGS_1, DtFlags1});
Igor Kudrin304860a2015-11-12 04:39:49 +0000597
Ed Mastef5d3cf62016-01-06 15:52:27 +0000598 if (!Config->Entry.empty())
Rafael Espindolacc3ae412016-01-26 01:30:07 +0000599 Add({DT_DEBUG, (uint64_t)0});
Ed Mastef5d3cf62016-01-06 15:52:27 +0000600
Igor Kudrin304860a2015-11-12 04:39:49 +0000601 if (Config->EMachine == EM_MIPS) {
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000602 Add({DT_MIPS_RLD_VERSION, 1});
603 Add({DT_MIPS_FLAGS, RHF_NOTPOT});
604 Add({DT_MIPS_BASE_ADDRESS, (uintX_t)Target->getVAStart()});
605 Add({DT_MIPS_SYMTABNO, Out<ELFT>::DynSymTab->getNumSymbols()});
606 Add({DT_MIPS_LOCAL_GOTNO, Out<ELFT>::Got->getMipsLocalEntriesNum()});
Rafael Espindolade069362016-01-25 21:32:04 +0000607 if (const SymbolBody *B = Out<ELFT>::Got->getMipsFirstGlobalEntry())
Rui Ueyama572a6f72016-01-29 01:49:33 +0000608 Add({DT_MIPS_GOTSYM, B->DynsymIndex});
Rafael Espindolade069362016-01-25 21:32:04 +0000609 else
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000610 Add({DT_MIPS_GOTSYM, Out<ELFT>::DynSymTab->getNumSymbols()});
611 Add({DT_PLTGOT, Out<ELFT>::Got});
Igor Kudrin304860a2015-11-12 04:39:49 +0000612 if (Out<ELFT>::MipsRldMap)
Rui Ueyamaac9fb452016-01-25 23:38:34 +0000613 Add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Igor Kudrin304860a2015-11-12 04:39:49 +0000614 }
615
Rafael Espindolade069362016-01-25 21:32:04 +0000616 // +1 for DT_NULL
617 Header.sh_size = (Entries.size() + 1) * Header.sh_entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000618}
619
620template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000621 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
622
Rafael Espindolade069362016-01-25 21:32:04 +0000623 for (const Entry &E : Entries) {
624 P->d_tag = E.Tag;
625 switch (E.Kind) {
626 case Entry::SecAddr:
627 P->d_un.d_ptr = E.OutSec->getVA();
628 break;
629 case Entry::SymAddr:
Rui Ueyamab5a69702016-02-01 21:00:35 +0000630 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
Rafael Espindolade069362016-01-25 21:32:04 +0000631 break;
632 case Entry::PlainInt:
633 P->d_un.d_val = E.Val;
634 break;
635 }
Rui Ueyama8c205d52015-10-02 01:33:31 +0000636 ++P;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000637 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000638}
639
640template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000641EhFrameHeader<ELFT>::EhFrameHeader()
642 : OutputSectionBase<ELFT>(".eh_frame_hdr", llvm::ELF::SHT_PROGBITS,
643 SHF_ALLOC) {
644 // It's a 4 bytes of header + pointer to the contents of the .eh_frame section
645 // + the number of FDE pointers in the table.
646 this->Header.sh_size = 12;
647}
648
649// We have to get PC values of FDEs. They depend on relocations
650// which are target specific, so we run this code after performing
651// all relocations. We read the values from ouput buffer according to the
652// encoding given for FDEs. Return value is an offset to the initial PC value
653// for the FDE.
654template <class ELFT>
655typename EhFrameHeader<ELFT>::uintX_t
656EhFrameHeader<ELFT>::getFdePc(uintX_t EhVA, const FdeData &F) {
657 const endianness E = ELFT::TargetEndianness;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000658 assert((F.Enc & 0xF0) != DW_EH_PE_datarel);
George Rimarf6bc65a2016-01-15 13:34:52 +0000659
660 uintX_t FdeOff = EhVA + F.Off + 8;
661 switch (F.Enc & 0xF) {
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000662 case DW_EH_PE_udata2:
663 case DW_EH_PE_sdata2:
George Rimarf6bc65a2016-01-15 13:34:52 +0000664 return FdeOff + read16<E>(F.PCRel);
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000665 case DW_EH_PE_udata4:
666 case DW_EH_PE_sdata4:
George Rimarf6bc65a2016-01-15 13:34:52 +0000667 return FdeOff + read32<E>(F.PCRel);
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000668 case DW_EH_PE_udata8:
669 case DW_EH_PE_sdata8:
George Rimarf6bc65a2016-01-15 13:34:52 +0000670 return FdeOff + read64<E>(F.PCRel);
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000671 case DW_EH_PE_absptr:
George Rimarf6bc65a2016-01-15 13:34:52 +0000672 if (sizeof(uintX_t) == 8)
673 return FdeOff + read64<E>(F.PCRel);
674 return FdeOff + read32<E>(F.PCRel);
675 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000676 fatal("unknown FDE size encoding");
George Rimarf6bc65a2016-01-15 13:34:52 +0000677}
678
679template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
680 const endianness E = ELFT::TargetEndianness;
681
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000682 const uint8_t Header[] = {1, DW_EH_PE_pcrel | DW_EH_PE_sdata4,
683 DW_EH_PE_udata4,
684 DW_EH_PE_datarel | DW_EH_PE_sdata4};
George Rimarf6bc65a2016-01-15 13:34:52 +0000685 memcpy(Buf, Header, sizeof(Header));
686
687 uintX_t EhVA = Sec->getVA();
688 uintX_t VA = this->getVA();
689 uintX_t EhOff = EhVA - VA - 4;
690 write32<E>(Buf + 4, EhOff);
691 write32<E>(Buf + 8, this->FdeList.size());
692 Buf += 12;
693
694 // InitialPC -> Offset in .eh_frame, sorted by InitialPC.
695 std::map<uintX_t, size_t> PcToOffset;
696 for (const FdeData &F : FdeList)
697 PcToOffset[getFdePc(EhVA, F)] = F.Off;
698
699 for (auto &I : PcToOffset) {
700 // The first four bytes are an offset to the initial PC value for the FDE.
701 write32<E>(Buf, I.first - VA);
702 // The last four bytes are an offset to the FDE data itself.
703 write32<E>(Buf + 4, EhVA + I.second - VA);
704 Buf += 8;
705 }
706}
707
708template <class ELFT>
709void EhFrameHeader<ELFT>::assignEhFrame(EHOutputSection<ELFT> *Sec) {
George Rimar45ca88d2016-01-25 19:27:50 +0000710 assert((!this->Sec || this->Sec == Sec) &&
711 "multiple .eh_frame sections not supported for .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000712 Live = Config->EhFrameHdr;
713 this->Sec = Sec;
714}
715
716template <class ELFT>
717void EhFrameHeader<ELFT>::addFde(uint8_t Enc, size_t Off, uint8_t *PCRel) {
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000718 if (Live && (Enc & 0xF0) == DW_EH_PE_datarel)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000719 fatal("DW_EH_PE_datarel encoding unsupported for FDEs by .eh_frame_hdr");
George Rimarf6bc65a2016-01-15 13:34:52 +0000720 FdeList.push_back(FdeData{Enc, Off, PCRel});
721}
722
723template <class ELFT> void EhFrameHeader<ELFT>::reserveFde() {
724 // Each FDE entry is 8 bytes long:
725 // The first four bytes are an offset to the initial PC value for the FDE. The
726 // last four byte are an offset to the FDE data itself.
727 this->Header.sh_size += 8;
728}
729
730template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000731OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type,
732 uintX_t Flags)
733 : OutputSectionBase<ELFT>(Name, Type, Flags) {}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000734
735template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +0000736void OutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
737 auto *S = cast<InputSection<ELFT>>(C);
738 Sections.push_back(S);
739 S->OutSec = this;
Rui Ueyama5ac58912016-02-24 00:38:18 +0000740 this->updateAlign(S->Align);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000741
742 uintX_t Off = this->Header.sh_size;
Rui Ueyama5ac58912016-02-24 00:38:18 +0000743 Off = alignTo(Off, S->Align);
Rui Ueyama40845e62015-12-26 05:51:07 +0000744 S->OutSecOff = Off;
745 Off += S->getSize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000746 this->Header.sh_size = Off;
747}
748
Rui Ueyamac4185702016-02-10 23:20:42 +0000749// If an input string is in the form of "foo.N" where N is a number,
750// return N. Otherwise, returns 65536, which is one greater than the
751// lowest priority.
752static int getPriority(StringRef S) {
753 size_t Pos = S.rfind('.');
754 if (Pos == StringRef::npos)
755 return 65536;
756 int V;
757 if (S.substr(Pos + 1).getAsInteger(10, V))
758 return 65536;
759 return V;
760}
761
Rui Ueyama5af83682016-02-11 23:41:38 +0000762// This function is called after we sort input sections
763// to update their offsets.
764template <class ELFT> void OutputSection<ELFT>::reassignOffsets() {
765 uintX_t Off = 0;
766 for (InputSection<ELFT> *S : Sections) {
Rui Ueyama5ac58912016-02-24 00:38:18 +0000767 Off = alignTo(Off, S->Align);
Rui Ueyama5af83682016-02-11 23:41:38 +0000768 S->OutSecOff = Off;
769 Off += S->getSize();
770 }
771 this->Header.sh_size = Off;
772}
773
Rui Ueyamac4185702016-02-10 23:20:42 +0000774// Sorts input sections by section name suffixes, so that .foo.N comes
775// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000776// We want to keep the original order if the priorities are the same
777// because the compiler keeps the original initialization order in a
778// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000779// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000780template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000781 // Sort sections by priority.
782 typedef std::pair<int, InputSection<ELFT> *> Pair;
Rui Ueyama704da022016-02-10 23:43:16 +0000783 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
784
Rui Ueyamac4185702016-02-10 23:20:42 +0000785 std::vector<Pair> V;
786 for (InputSection<ELFT> *S : Sections)
787 V.push_back({getPriority(S->getSectionName()), S});
Rui Ueyama704da022016-02-10 23:43:16 +0000788 std::stable_sort(V.begin(), V.end(), Comp);
Rui Ueyamac4185702016-02-10 23:20:42 +0000789 Sections.clear();
790 for (Pair &P : V)
791 Sections.push_back(P.second);
Rui Ueyama5af83682016-02-11 23:41:38 +0000792 reassignOffsets();
793}
Rui Ueyamac4185702016-02-10 23:20:42 +0000794
Rui Ueyama5af83682016-02-11 23:41:38 +0000795// Returns true if S matches /Filename.?\.o$/.
796static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
797 if (!S.endswith(".o"))
798 return false;
799 S = S.drop_back(2);
800 if (S.endswith(Filename))
801 return true;
802 return !S.empty() && S.drop_back().endswith(Filename);
803}
804
805static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
806static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
807
808// .ctors and .dtors are sorted by this priority from highest to lowest.
809//
810// 1. The section was contained in crtbegin (crtbegin contains
811// some sentinel value in its .ctors and .dtors so that the runtime
812// can find the beginning of the sections.)
813//
814// 2. The section has an optional priority value in the form of ".ctors.N"
815// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
816// they are compared as string rather than number.
817//
818// 3. The section is just ".ctors" or ".dtors".
819//
820// 4. The section was contained in crtend, which contains an end marker.
821//
822// In an ideal world, we don't need this function because .init_array and
823// .ctors are duplicate features (and .init_array is newer.) However, there
824// are too many real-world use cases of .ctors, so we had no choice to
825// support that with this rather ad-hoc semantics.
826template <class ELFT>
827static bool compCtors(const InputSection<ELFT> *A,
828 const InputSection<ELFT> *B) {
829 bool BeginA = isCrtbegin(A->getFile()->getName());
830 bool BeginB = isCrtbegin(B->getFile()->getName());
831 if (BeginA != BeginB)
832 return BeginA;
833 bool EndA = isCrtend(A->getFile()->getName());
834 bool EndB = isCrtend(B->getFile()->getName());
835 if (EndA != EndB)
836 return EndB;
837 StringRef X = A->getSectionName();
838 StringRef Y = B->getSectionName();
839 assert(X.startswith(".ctors") || X.startswith(".dtors"));
840 assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
841 X = X.substr(6);
842 Y = Y.substr(6);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000843 if (X.empty() && Y.empty())
844 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000845 return X < Y;
846}
847
848// Sorts input sections by the special rules for .ctors and .dtors.
849// Unfortunately, the rules are different from the one for .{init,fini}_array.
850// Read the comment above.
851template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
852 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
853 reassignOffsets();
Rui Ueyamac4185702016-02-10 23:20:42 +0000854}
855
Rui Ueyama34f29242015-10-13 19:51:57 +0000856// Returns a VA which a relocatin RI refers to. Used only for local symbols.
Rui Ueyamab5a69702016-02-01 21:00:35 +0000857// For non-local symbols, use SymbolBody::getVA instead.
Rafael Espindola932efcf2015-10-19 20:24:44 +0000858template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000859typename ELFFile<ELFT>::uintX_t
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000860elf2::getLocalRelTarget(const ObjectFile<ELFT> &File,
861 const Elf_Rel_Impl<ELFT, IsRela> &RI,
862 typename ELFFile<ELFT>::uintX_t Addend) {
Rafael Espindola932efcf2015-10-19 20:24:44 +0000863 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000864 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola932efcf2015-10-19 20:24:44 +0000865
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000866 // PPC64 has a special relocation representing the TOC base pointer
867 // that does not have a corresponding symbol.
Hal Finkel230c5c52015-10-16 22:37:32 +0000868 if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000869 return getPPC64TocBase() + Addend;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000870
Rui Ueyama126d08f2015-10-12 20:28:22 +0000871 const Elf_Sym *Sym =
872 File.getObj().getRelocationSymbol(&RI, File.getSymbolTable());
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000873
Rui Ueyama126d08f2015-10-12 20:28:22 +0000874 if (!Sym)
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000875 fatal("Unsupported relocation without symbol");
Rui Ueyama126d08f2015-10-12 20:28:22 +0000876
Rafael Espindola435c00f2016-02-23 20:19:44 +0000877 InputSectionBase<ELFT> *Section = File.getSection(*Sym);
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000878
Rafael Espindola435c00f2016-02-23 20:19:44 +0000879 if (Sym->getType() == STT_TLS)
880 return (Section->OutSec->getVA() + Section->getOffset(*Sym) + Addend) -
George Rimarcc06a6f2015-11-29 14:14:20 +0000881 Out<ELFT>::TlsPhdr->p_vaddr;
Michael J. Spencerdc9c5df2015-11-11 01:28:23 +0000882
Rafael Espindola444576d2015-10-09 19:25:07 +0000883 // According to the ELF spec reference to a local symbol from outside
884 // the group are not allowed. Unfortunately .eh_frame breaks that rule
885 // and must be treated specially. For now we just replace the symbol with
886 // 0.
Rui Ueyama733153d2016-02-24 18:33:35 +0000887 if (Section == InputSection<ELFT>::Discarded || !Section->Live)
Rafael Espindola932efcf2015-10-19 20:24:44 +0000888 return Addend;
Rafael Espindola444576d2015-10-09 19:25:07 +0000889
Rafael Espindola435c00f2016-02-23 20:19:44 +0000890 uintX_t Offset = Sym->st_value;
891 if (Sym->getType() == STT_SECTION) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000892 Offset += Addend;
Rafael Espindolaf5af8352015-10-20 22:08:49 +0000893 Addend = 0;
Rafael Espindolac159c962015-10-19 21:00:02 +0000894 }
Rafael Espindola38a36c42016-02-03 16:53:39 +0000895 return Section->OutSec->getVA() + Section->getOffset(Offset) + Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000896}
897
Rui Ueyama34f29242015-10-13 19:51:57 +0000898// Returns true if a symbol can be replaced at load-time by a symbol
899// with the same name defined in other ELF executable or DSO.
Rui Ueyama83cd6e02016-01-06 20:11:55 +0000900bool elf2::canBePreempted(const SymbolBody *Body, bool NeedsGot) {
Rafael Espindolaa6627382015-10-06 23:56:53 +0000901 if (!Body)
Rui Ueyama34f29242015-10-13 19:51:57 +0000902 return false; // Body is a local symbol.
Rafael Espindolacea0b3b2015-10-07 04:22:55 +0000903 if (Body->isShared())
904 return true;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000905
906 if (Body->isUndefined()) {
907 if (!Body->isWeak())
908 return true;
909
910 // This is an horrible corner case. Ideally we would like to say that any
911 // undefined symbol can be preempted so that the dynamic linker has a
912 // chance of finding it at runtime.
913 //
914 // The problem is that the code sequence used to test for weak undef
915 // functions looks like
916 // if (func) func()
917 // If the code is -fPIC the first reference is a load from the got and
918 // everything works.
919 // If the code is not -fPIC there is no reasonable way to solve it:
920 // * A relocation writing to the text segment will fail (it is ro).
921 // * A copy relocation doesn't work for functions.
922 // * The trick of using a plt entry as the address would fail here since
923 // the plt entry would have a non zero address.
924 // Since we cannot do anything better, we just resolve the symbol to 0 and
925 // don't produce a dynamic relocation.
Hal Finkelc9174062015-10-16 22:11:05 +0000926 //
927 // As an extra hack, assume that if we are producing a shared library the
928 // user knows what he or she is doing and can handle a dynamic relocation.
929 return Config->Shared || NeedsGot;
Rafael Espindolacc6ebb82015-10-14 18:42:16 +0000930 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000931 if (!Config->Shared)
932 return false;
George Rimar5c36e592016-02-02 09:28:53 +0000933 if (Body->getVisibility() != STV_DEFAULT)
934 return false;
935 if (Config->Bsymbolic || (Config->BsymbolicFunctions && Body->isFunc()))
936 return false;
937 return true;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000938}
939
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000940template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola71675852015-09-22 00:16:19 +0000941 for (InputSection<ELFT> *C : Sections)
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000942 C->writeTo(Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000943}
944
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000945template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000946EHOutputSection<ELFT>::EHOutputSection(StringRef Name, uint32_t Type,
947 uintX_t Flags)
George Rimarf6bc65a2016-01-15 13:34:52 +0000948 : OutputSectionBase<ELFT>(Name, Type, Flags) {
949 Out<ELFT>::EhFrameHdr->assignEhFrame(this);
950}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000951
952template <class ELFT>
953EHRegion<ELFT>::EHRegion(EHInputSection<ELFT> *S, unsigned Index)
954 : S(S), Index(Index) {}
955
956template <class ELFT> StringRef EHRegion<ELFT>::data() const {
957 ArrayRef<uint8_t> SecData = S->getSectionData();
958 ArrayRef<std::pair<uintX_t, uintX_t>> Offsets = S->Offsets;
959 size_t Start = Offsets[Index].first;
960 size_t End =
961 Index == Offsets.size() - 1 ? SecData.size() : Offsets[Index + 1].first;
962 return StringRef((const char *)SecData.data() + Start, End - Start);
963}
964
965template <class ELFT>
966Cie<ELFT>::Cie(EHInputSection<ELFT> *S, unsigned Index)
967 : EHRegion<ELFT>(S, Index) {}
968
George Rimarf6bc65a2016-01-15 13:34:52 +0000969// Read a byte and advance D by one byte.
970static uint8_t readByte(ArrayRef<uint8_t> &D) {
971 if (D.empty())
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000972 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000973 uint8_t B = D.front();
974 D = D.slice(1);
975 return B;
976}
977
978static void skipLeb128(ArrayRef<uint8_t> &D) {
979 while (!D.empty()) {
980 uint8_t Val = D.front();
981 D = D.slice(1);
982 if ((Val & 0x80) == 0)
983 return;
984 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000985 fatal("corrupted or unsupported CIE information");
George Rimarf6bc65a2016-01-15 13:34:52 +0000986}
987
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000988template <class ELFT> static size_t getAugPSize(unsigned Enc) {
989 switch (Enc & 0x0f) {
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000990 case DW_EH_PE_absptr:
991 case DW_EH_PE_signed:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000992 return ELFT::Is64Bits ? 8 : 4;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000993 case DW_EH_PE_udata2:
994 case DW_EH_PE_sdata2:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000995 return 2;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000996 case DW_EH_PE_udata4:
997 case DW_EH_PE_sdata4:
Rui Ueyama6448f8a2016-02-09 21:41:01 +0000998 return 4;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +0000999 case DW_EH_PE_udata8:
1000 case DW_EH_PE_sdata8:
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001001 return 8;
1002 }
1003 fatal("unknown FDE encoding");
1004}
1005
1006template <class ELFT> static void skipAugP(ArrayRef<uint8_t> &D) {
1007 uint8_t Enc = readByte(D);
Rui Ueyamadbcfedb2016-02-09 21:46:11 +00001008 if ((Enc & 0xf0) == DW_EH_PE_aligned)
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001009 fatal("DW_EH_PE_aligned encoding is not supported");
1010 size_t Size = getAugPSize<ELFT>(Enc);
Rafael Espindola9e072d32016-02-09 22:47:34 +00001011 if (Size >= D.size())
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001012 fatal("corrupted CIE");
1013 D = D.slice(Size);
1014}
1015
George Rimarf6bc65a2016-01-15 13:34:52 +00001016template <class ELFT>
1017uint8_t EHOutputSection<ELFT>::getFdeEncoding(ArrayRef<uint8_t> D) {
Rui Ueyamabe748c22016-02-08 05:18:44 +00001018 if (D.size() < 8)
1019 fatal("CIE too small");
George Rimarf6bc65a2016-01-15 13:34:52 +00001020 D = D.slice(8);
1021
1022 uint8_t Version = readByte(D);
1023 if (Version != 1 && Version != 3)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001024 fatal("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
George Rimarf6bc65a2016-01-15 13:34:52 +00001025
Saleem Abdulrasoolc0571e12016-02-15 03:45:18 +00001026 const unsigned char *AugEnd = std::find(D.begin() + 1, D.end(), '\0');
Rui Ueyamabe748c22016-02-08 05:18:44 +00001027 if (AugEnd == D.end())
1028 fatal("corrupted CIE");
Saleem Abdulrasoolc0571e12016-02-15 03:45:18 +00001029 StringRef Aug(reinterpret_cast<const char *>(D.begin()), AugEnd - D.begin());
Rui Ueyamabe748c22016-02-08 05:18:44 +00001030 D = D.slice(Aug.size() + 1);
George Rimarf6bc65a2016-01-15 13:34:52 +00001031
1032 // Code alignment factor should always be 1 for .eh_frame.
1033 if (readByte(D) != 1)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001034 fatal("CIE code alignment must be 1");
Rui Ueyamabe748c22016-02-08 05:18:44 +00001035
1036 // Skip data alignment factor.
George Rimarf6bc65a2016-01-15 13:34:52 +00001037 skipLeb128(D);
1038
1039 // Skip the return address register. In CIE version 1 this is a single
1040 // byte. In CIE version 3 this is an unsigned LEB128.
1041 if (Version == 1)
1042 readByte(D);
1043 else
1044 skipLeb128(D);
1045
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001046 // We only care about an 'R' value, but other records may precede an 'R'
1047 // record. Records are not in TLV (type-length-value) format, so we need
1048 // to teach the linker how to skip records for each type.
Rui Ueyamad3bd97a2016-02-09 23:11:21 +00001049 for (char C : Aug) {
1050 if (C == 'R')
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001051 return readByte(D);
Rui Ueyamad3bd97a2016-02-09 23:11:21 +00001052 if (C == 'z') {
1053 skipLeb128(D);
1054 continue;
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001055 }
Rui Ueyamad3bd97a2016-02-09 23:11:21 +00001056 if (C == 'P') {
1057 skipAugP<ELFT>(D);
1058 continue;
1059 }
1060 if (C == 'L')
1061 continue;
1062 fatal("unknown .eh_frame augmentation string: " + Aug);
Rui Ueyama6448f8a2016-02-09 21:41:01 +00001063 }
Rui Ueyamadbcfedb2016-02-09 21:46:11 +00001064 return DW_EH_PE_absptr;
George Rimarf6bc65a2016-01-15 13:34:52 +00001065}
1066
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001067template <class ELFT>
Rui Ueyamac0c92602016-02-05 22:56:03 +00001068static typename ELFFile<ELFT>::uintX_t readEntryLength(ArrayRef<uint8_t> D) {
1069 const endianness E = ELFT::TargetEndianness;
Rui Ueyamac0c92602016-02-05 22:56:03 +00001070 if (D.size() < 4)
Rui Ueyama1b45cca2016-02-05 23:24:05 +00001071 fatal("CIE/FDE too small");
1072
1073 // First 4 bytes of CIE/FDE is the size of the record.
1074 // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead.
1075 uint64_t V = read32<E>(D.data());
1076 if (V < UINT32_MAX) {
1077 uint64_t Len = V + 4;
1078 if (Len > D.size())
Rui Ueyamac0c92602016-02-05 22:56:03 +00001079 fatal("CIE/FIE ends past the end of the section");
Rui Ueyama1b45cca2016-02-05 23:24:05 +00001080 return Len;
Rui Ueyamac0c92602016-02-05 22:56:03 +00001081 }
1082
1083 if (D.size() < 12)
Rui Ueyama1b45cca2016-02-05 23:24:05 +00001084 fatal("CIE/FDE too small");
1085 V = read64<E>(D.data() + 4);
1086 uint64_t Len = V + 12;
1087 if (Len < V || D.size() < Len)
Rui Ueyamac0c92602016-02-05 22:56:03 +00001088 fatal("CIE/FIE ends past the end of the section");
Rui Ueyama1b45cca2016-02-05 23:24:05 +00001089 return Len;
Rui Ueyamac0c92602016-02-05 22:56:03 +00001090}
1091
1092template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001093template <bool IsRela>
1094void EHOutputSection<ELFT>::addSectionAux(
1095 EHInputSection<ELFT> *S,
1096 iterator_range<const Elf_Rel_Impl<ELFT, IsRela> *> Rels) {
1097 const endianness E = ELFT::TargetEndianness;
1098
1099 S->OutSec = this;
Rui Ueyama5ac58912016-02-24 00:38:18 +00001100 this->updateAlign(S->Align);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001101 Sections.push_back(S);
1102
1103 ArrayRef<uint8_t> SecData = S->getSectionData();
1104 ArrayRef<uint8_t> D = SecData;
1105 uintX_t Offset = 0;
1106 auto RelI = Rels.begin();
1107 auto RelE = Rels.end();
1108
George Rimar147747a2016-01-02 16:55:01 +00001109 DenseMap<unsigned, unsigned> OffsetToIndex;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001110 while (!D.empty()) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001111 unsigned Index = S->Offsets.size();
1112 S->Offsets.push_back(std::make_pair(Offset, -1));
1113
Rui Ueyamac0c92602016-02-05 22:56:03 +00001114 uintX_t Length = readEntryLength<ELFT>(D);
George Rimarf6bc65a2016-01-15 13:34:52 +00001115 // If CIE/FDE data length is zero then Length is 4, this
1116 // shall be considered a terminator and processing shall end.
1117 if (Length == 4)
1118 break;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001119 StringRef Entry((const char *)D.data(), Length);
1120
1121 while (RelI != RelE && RelI->r_offset < Offset)
1122 ++RelI;
1123 uintX_t NextOffset = Offset + Length;
1124 bool HasReloc = RelI != RelE && RelI->r_offset < NextOffset;
1125
1126 uint32_t ID = read32<E>(D.data() + 4);
1127 if (ID == 0) {
1128 // CIE
1129 Cie<ELFT> C(S, Index);
George Rimarf6bc65a2016-01-15 13:34:52 +00001130 if (Config->EhFrameHdr)
1131 C.FdeEncoding = getFdeEncoding(D);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001132
Rafael Espindola156ed8d2016-02-10 13:19:32 +00001133 SymbolBody *Personality = nullptr;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001134 if (HasReloc) {
1135 uint32_t SymIndex = RelI->getSymbol(Config->Mips64EL);
Rafael Espindola156ed8d2016-02-10 13:19:32 +00001136 Personality = S->getFile()->getSymbolBody(SymIndex)->repl();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001137 }
1138
Rafael Espindola156ed8d2016-02-10 13:19:32 +00001139 std::pair<StringRef, SymbolBody *> CieInfo(Entry, Personality);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001140 auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
George Rimar147747a2016-01-02 16:55:01 +00001141 if (P.second) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001142 Cies.push_back(C);
Rui Ueyama489a8062016-01-14 20:53:50 +00001143 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
George Rimar147747a2016-01-02 16:55:01 +00001144 }
1145 OffsetToIndex[Offset] = P.first->second;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001146 } else {
1147 if (!HasReloc)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001148 fatal("FDE doesn't reference another section");
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001149 InputSectionBase<ELFT> *Target = S->getRelocTarget(*RelI);
Rui Ueyama733153d2016-02-24 18:33:35 +00001150 if (Target != InputSection<ELFT>::Discarded && Target->Live) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001151 uint32_t CieOffset = Offset + 4 - ID;
George Rimar147747a2016-01-02 16:55:01 +00001152 auto I = OffsetToIndex.find(CieOffset);
1153 if (I == OffsetToIndex.end())
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001154 fatal("Invalid CIE reference");
George Rimar147747a2016-01-02 16:55:01 +00001155 Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
George Rimarf6bc65a2016-01-15 13:34:52 +00001156 Out<ELFT>::EhFrameHdr->reserveFde();
Rui Ueyama489a8062016-01-14 20:53:50 +00001157 this->Header.sh_size += alignTo(Length, sizeof(uintX_t));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001158 }
1159 }
1160
1161 Offset = NextOffset;
1162 D = D.slice(Length);
1163 }
1164}
1165
1166template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001167void EHOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1168 auto *S = cast<EHInputSection<ELFT>>(C);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001169 const Elf_Shdr *RelSec = S->RelocSection;
Rui Ueyama0de86c12016-01-27 22:23:44 +00001170 if (!RelSec) {
1171 addSectionAux(S, make_range<const Elf_Rela *>(nullptr, nullptr));
1172 return;
1173 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001174 ELFFile<ELFT> &Obj = S->getFile()->getObj();
1175 if (RelSec->sh_type == SHT_RELA)
Rui Ueyama0de86c12016-01-27 22:23:44 +00001176 addSectionAux(S, Obj.relas(RelSec));
1177 else
1178 addSectionAux(S, Obj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001179}
1180
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001181template <class ELFT>
1182static typename ELFFile<ELFT>::uintX_t writeAlignedCieOrFde(StringRef Data,
1183 uint8_t *Buf) {
1184 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1185 const endianness E = ELFT::TargetEndianness;
Rui Ueyama489a8062016-01-14 20:53:50 +00001186 uint64_t Len = alignTo(Data.size(), sizeof(uintX_t));
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001187 write32<E>(Buf, Len - 4);
1188 memcpy(Buf + 4, Data.data() + 4, Data.size() - 4);
1189 return Len;
1190}
1191
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001192template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1193 const endianness E = ELFT::TargetEndianness;
1194 size_t Offset = 0;
1195 for (const Cie<ELFT> &C : Cies) {
1196 size_t CieOffset = Offset;
1197
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001198 uintX_t CIELen = writeAlignedCieOrFde<ELFT>(C.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001199 C.S->Offsets[C.Index].second = Offset;
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001200 Offset += CIELen;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001201
1202 for (const EHRegion<ELFT> &F : C.Fdes) {
Rafael Espindola91bd48a2015-12-24 20:44:06 +00001203 uintX_t Len = writeAlignedCieOrFde<ELFT>(F.data(), Buf + Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001204 write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001205 F.S->Offsets[F.Index].second = Offset;
George Rimarf6bc65a2016-01-15 13:34:52 +00001206 Out<ELFT>::EhFrameHdr->addFde(C.FdeEncoding, Offset, Buf + Offset + 8);
George Rimare72beba2015-12-21 09:38:59 +00001207 Offset += Len;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001208 }
1209 }
1210
1211 for (EHInputSection<ELFT> *S : Sections) {
1212 const Elf_Shdr *RelSec = S->RelocSection;
1213 if (!RelSec)
1214 continue;
1215 ELFFile<ELFT> &EObj = S->getFile()->getObj();
1216 if (RelSec->sh_type == SHT_RELA)
1217 S->relocate(Buf, nullptr, EObj.relas(RelSec));
1218 else
Rafael Espindolae02c8682015-11-23 15:28:28 +00001219 S->relocate(Buf, nullptr, EObj.rels(RelSec));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001220 }
1221}
1222
1223template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +00001224MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +00001225 uintX_t Flags, uintX_t Alignment)
1226 : OutputSectionBase<ELFT>(Name, Type, Flags),
1227 Builder(llvm::StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +00001228
1229template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001230 if (shouldTailMerge()) {
1231 StringRef Data = Builder.data();
Rafael Espindolac159c962015-10-19 21:00:02 +00001232 memcpy(Buf, Data.data(), Data.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001233 return;
Rafael Espindolac159c962015-10-19 21:00:02 +00001234 }
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001235 for (const std::pair<StringRef, size_t> &P : Builder.getMap()) {
1236 StringRef Data = P.first;
1237 memcpy(Buf + P.second, Data.data(), Data.size());
1238 }
1239}
1240
1241static size_t findNull(StringRef S, size_t EntSize) {
1242 // Optimize the common case.
1243 if (EntSize == 1)
1244 return S.find(0);
1245
1246 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) {
1247 const char *B = S.begin() + I;
1248 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; }))
1249 return I;
1250 }
1251 return StringRef::npos;
Rafael Espindolac159c962015-10-19 21:00:02 +00001252}
1253
1254template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001255void MergeOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
1256 auto *S = cast<MergeInputSection<ELFT>>(C);
Rafael Espindolac159c962015-10-19 21:00:02 +00001257 S->OutSec = this;
Rui Ueyama5ac58912016-02-24 00:38:18 +00001258 this->updateAlign(S->Align);
Rafael Espindolac159c962015-10-19 21:00:02 +00001259
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001260 ArrayRef<uint8_t> D = S->getSectionData();
George Rimarf940d592015-10-25 20:14:07 +00001261 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolac159c962015-10-19 21:00:02 +00001262 uintX_t EntSize = S->getSectionHdr()->sh_entsize;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001263
Rui Ueyamaead75fc2016-01-29 22:18:55 +00001264 // If this is of type string, the contents are null-terminated strings.
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001265 if (this->Header.sh_flags & SHF_STRINGS) {
Rui Ueyamaad87ff72016-01-06 02:52:24 +00001266 uintX_t Offset = 0;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001267 while (!Data.empty()) {
1268 size_t End = findNull(Data, EntSize);
1269 if (End == StringRef::npos)
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001270 fatal("String is not null terminated");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001271 StringRef Entry = Data.substr(0, End + EntSize);
George Rimar4b40ebc2015-11-13 13:44:59 +00001272 uintX_t OutputOffset = Builder.add(Entry);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001273 if (shouldTailMerge())
1274 OutputOffset = -1;
1275 S->Offsets.push_back(std::make_pair(Offset, OutputOffset));
1276 uintX_t Size = End + EntSize;
1277 Data = Data.substr(Size);
1278 Offset += Size;
1279 }
Rui Ueyamaead75fc2016-01-29 22:18:55 +00001280 return;
1281 }
1282
1283 // If this is not of type string, every entry has the same size.
1284 for (unsigned I = 0, N = Data.size(); I != N; I += EntSize) {
1285 StringRef Entry = Data.substr(I, EntSize);
1286 size_t OutputOffset = Builder.add(Entry);
1287 S->Offsets.push_back(std::make_pair(I, OutputOffset));
Rafael Espindolac159c962015-10-19 21:00:02 +00001288 }
Rafael Espindolac159c962015-10-19 21:00:02 +00001289}
1290
1291template <class ELFT>
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +00001292unsigned MergeOutputSection<ELFT>::getOffset(StringRef Val) {
1293 return Builder.getOffset(Val);
1294}
1295
1296template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
1297 return Config->Optimize >= 2 && this->Header.sh_flags & SHF_STRINGS;
1298}
1299
1300template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
1301 if (shouldTailMerge())
1302 Builder.finalize();
1303 this->Header.sh_size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +00001304}
1305
1306template <class ELFT>
George Rimar0f5ac9f2015-10-20 17:21:35 +00001307StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001308 : OutputSectionBase<ELFT>(Name, SHT_STRTAB,
1309 Dynamic ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindola35c6af32015-09-25 17:19:10 +00001310 Dynamic(Dynamic) {
1311 this->Header.sh_addralign = 1;
1312}
1313
Rafael Espindolae2c24612016-01-29 01:24:25 +00001314// Adds a string to the string table. If HashIt is true we hash and check for
1315// duplicates. It is optional because the name of global symbols are already
1316// uniqued and hashing them again has a big cost for a small value: uniquing
1317// them with some other string that happens to be the same.
1318template <class ELFT>
1319unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1320 if (HashIt) {
1321 auto R = StringMap.insert(std::make_pair(S, Size));
1322 if (!R.second)
1323 return R.first->second;
1324 }
1325 unsigned Ret = Size;
1326 Size += S.size() + 1;
Rui Ueyama76c00632016-01-07 02:35:32 +00001327 Strings.push_back(S);
Rafael Espindolae2c24612016-01-29 01:24:25 +00001328 return Ret;
Rui Ueyama76c00632016-01-07 02:35:32 +00001329}
1330
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001331template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama76c00632016-01-07 02:35:32 +00001332 // ELF string tables start with NUL byte, so advance the pointer by one.
1333 ++Buf;
1334 for (StringRef S : Strings) {
1335 memcpy(Buf, S.data(), S.size());
1336 Buf += S.size() + 1;
1337 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001338}
1339
Rafael Espindolad1cf4212015-10-05 16:25:43 +00001340template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +00001341SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001342 SymbolTable<ELFT> &Table, StringTableSection<ELFT> &StrTabSec)
Rui Ueyamacf07a312016-01-06 22:53:58 +00001343 : OutputSectionBase<ELFT>(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
1344 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
Rui Ueyama6ffb42a2016-01-07 20:53:30 +00001345 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001346 StrTabSec(StrTabSec), Table(Table) {
Rui Ueyamad1e92aa2016-01-07 18:20:02 +00001347 this->Header.sh_entsize = sizeof(Elf_Sym);
Rui Ueyama5e378dd2016-01-29 22:18:57 +00001348 this->Header.sh_addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +00001349}
1350
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001351// Orders symbols according to their positions in the GOT,
1352// in compliance with MIPS ABI rules.
1353// See "Global Offset Table" in Chapter 5 in the following document
1354// for detailed description:
1355// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rafael Espindolae2c24612016-01-29 01:24:25 +00001356static bool sortMipsSymbols(const std::pair<SymbolBody *, unsigned> &L,
1357 const std::pair<SymbolBody *, unsigned> &R) {
1358 if (!L.first->isInGot() || !R.first->isInGot())
1359 return R.first->isInGot();
1360 return L.first->GotIndex < R.first->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001361}
1362
Rui Ueyama0db335f2015-10-07 16:58:54 +00001363template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Igor Kudrin2169b1b2015-11-02 10:46:14 +00001364 if (this->Header.sh_size)
1365 return; // Already finalized.
1366
Rui Ueyama0db335f2015-10-07 16:58:54 +00001367 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001368 this->Header.sh_link = StrTabSec.SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +00001369 this->Header.sh_info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001370
1371 if (!StrTabSec.isDynamic()) {
1372 std::stable_sort(Symbols.begin(), Symbols.end(),
Rafael Espindolae2c24612016-01-29 01:24:25 +00001373 [](const std::pair<SymbolBody *, unsigned> &L,
1374 const std::pair<SymbolBody *, unsigned> &R) {
1375 return getSymbolBinding(L.first) == STB_LOCAL &&
1376 getSymbolBinding(R.first) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001377 });
1378 return;
1379 }
Igor Kudrinf1d60292015-10-28 07:05:56 +00001380 if (Out<ELFT>::GnuHashTab)
1381 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1382 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +00001383 else if (Config->EMachine == EM_MIPS)
1384 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
Igor Kudrinab665fc2015-10-20 21:47:58 +00001385 size_t I = 0;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001386 for (const std::pair<SymbolBody *, size_t> &P : Symbols)
Rui Ueyama572a6f72016-01-29 01:49:33 +00001387 P.first->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001388}
1389
1390template <class ELFT>
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001391void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
1392 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
Rui Ueyama0db335f2015-10-07 16:58:54 +00001393}
1394
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001395template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001396 Buf += sizeof(Elf_Sym);
1397
1398 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1399 // .dynsym only contains global symbols.
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001400 if (!Config->DiscardAll && !StrTabSec.isDynamic())
1401 writeLocalSymbols(Buf);
1402
1403 writeGlobalSymbols(Buf);
1404}
1405
1406template <class ELFT>
1407void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1408 // Iterate over all input object files to copy their local symbols
1409 // to the output symbol table pointed by Buf.
Rui Ueyama3ce825e2015-10-09 21:07:25 +00001410 for (const std::unique_ptr<ObjectFile<ELFT>> &File : Table.getObjectFiles()) {
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001411 for (const std::pair<const Elf_Sym *, size_t> &P : File->KeptLocalSyms) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001412 const Elf_Sym *Sym = P.first;
Rafael Espindola444576d2015-10-09 19:25:07 +00001413
Rui Ueyamac55733e2015-09-30 00:54:29 +00001414 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +00001415 uintX_t VA = 0;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001416 if (Sym->st_shndx == SHN_ABS) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001417 ESym->st_shndx = SHN_ABS;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001418 VA = Sym->st_value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001419 } else {
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001420 InputSectionBase<ELFT> *Section = File->getSection(*Sym);
Rafael Espindolac159c962015-10-19 21:00:02 +00001421 const OutputSectionBase<ELFT> *OutSec = Section->OutSec;
1422 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001423 VA = Section->getOffset(*Sym);
Tom Stellard80efb162016-01-07 03:59:08 +00001424 // Symbol offsets for AMDGPU need to be the offset in bytes of the
1425 // symbol from the beginning of the section.
1426 if (Config->EMachine != EM_AMDGPU)
1427 VA += OutSec->getVA();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001428 }
Rafael Espindolae2c24612016-01-29 01:24:25 +00001429 ESym->st_name = P.second;
Rafael Espindola10d71ff2016-01-27 18:04:26 +00001430 ESym->st_size = Sym->st_size;
1431 ESym->setBindingAndType(Sym->getBinding(), Sym->getType());
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001432 ESym->st_value = VA;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001433 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001434 }
1435 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001436}
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001437
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001438template <class ELFT>
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001439static const typename llvm::object::ELFFile<ELFT>::Elf_Sym *
1440getElfSym(SymbolBody &Body) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001441 if (auto *EBody = dyn_cast<DefinedElf<ELFT>>(&Body))
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001442 return &EBody->Sym;
1443 if (auto *EBody = dyn_cast<UndefinedElf<ELFT>>(&Body))
1444 return &EBody->Sym;
1445 return nullptr;
1446}
1447
1448template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +00001449void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +00001450 // Write the internal symbol table contents to the output symbol table
1451 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +00001452 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001453 for (const std::pair<SymbolBody *, size_t> &P : Symbols) {
Rafael Espindolae2c24612016-01-29 01:24:25 +00001454 SymbolBody *Body = P.first;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001455 size_t StrOff = P.second;
Rui Ueyamac4aaed92015-10-22 18:49:53 +00001456
Rafael Espindola8614c562015-10-06 14:33:58 +00001457 unsigned char Type = STT_NOTYPE;
1458 uintX_t Size = 0;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001459 if (const Elf_Sym *InputSym = getElfSym<ELFT>(*Body)) {
1460 Type = InputSym->getType();
1461 Size = InputSym->st_size;
Rafael Espindola11191912015-12-24 16:23:37 +00001462 } else if (auto *C = dyn_cast<DefinedCommon>(Body)) {
1463 Type = STT_OBJECT;
1464 Size = C->Size;
Rafael Espindola8614c562015-10-06 14:33:58 +00001465 }
1466
Igor Kudrin853b88d2015-10-20 20:52:14 +00001467 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +00001468 ESym->st_size = Size;
Rui Ueyamac2e863a2016-02-17 05:06:40 +00001469 ESym->st_name = StrOff;
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001470 ESym->setVisibility(Body->getVisibility());
Rui Ueyamab5a69702016-02-01 21:00:35 +00001471 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001472
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001473 if (const OutputSectionBase<ELFT> *OutSec = getOutputSection(Body))
Rui Ueyama2317d0d2015-10-15 20:55:22 +00001474 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +00001475 else if (isa<DefinedRegular<ELFT>>(Body))
1476 ESym->st_shndx = SHN_ABS;
Igor Kudrinab665fc2015-10-20 21:47:58 +00001477 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001478 }
1479}
1480
Igor Kudrin853b88d2015-10-20 20:52:14 +00001481template <class ELFT>
Rui Ueyama874e7ae2016-02-17 04:56:44 +00001482const OutputSectionBase<ELFT> *
1483SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1484 switch (Sym->kind()) {
1485 case SymbolBody::DefinedSyntheticKind:
1486 return &cast<DefinedSynthetic<ELFT>>(Sym)->Section;
1487 case SymbolBody::DefinedRegularKind: {
1488 auto *D = cast<DefinedRegular<ELFT>>(Sym->repl());
1489 if (D->Section)
1490 return D->Section->OutSec;
1491 break;
1492 }
1493 case SymbolBody::DefinedCommonKind:
1494 return Out<ELFT>::Bss;
1495 case SymbolBody::SharedKind:
1496 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1497 return Out<ELFT>::Bss;
1498 break;
1499 case SymbolBody::UndefinedElfKind:
1500 case SymbolBody::UndefinedKind:
1501 case SymbolBody::LazyKind:
1502 break;
1503 case SymbolBody::DefinedBitcodeKind:
1504 llvm_unreachable("Should have been replaced");
1505 }
1506 return nullptr;
1507}
1508
1509template <class ELFT>
Igor Kudrin853b88d2015-10-20 20:52:14 +00001510uint8_t SymbolTableSection<ELFT>::getSymbolBinding(SymbolBody *Body) {
Rui Ueyama8f2c4da2015-10-21 18:13:47 +00001511 uint8_t Visibility = Body->getVisibility();
Igor Kudrin853b88d2015-10-20 20:52:14 +00001512 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1513 return STB_LOCAL;
Rafael Espindola5d7593b2015-12-22 23:00:50 +00001514 if (const Elf_Sym *ESym = getElfSym<ELFT>(*Body))
1515 return ESym->getBinding();
Rafael Espindola4d4b06a2015-12-24 00:47:42 +00001516 if (isa<DefinedSynthetic<ELFT>>(Body))
1517 return STB_LOCAL;
Igor Kudrin853b88d2015-10-20 20:52:14 +00001518 return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
1519}
1520
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001521template <class ELFT>
1522MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
1523 : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
1524 this->Header.sh_addralign = 4;
1525 this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
1526 this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
1527}
1528
1529template <class ELFT>
1530void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
1531 auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
1532 R->ri_gp_value = getMipsGpAddr<ELFT>();
Rui Ueyama70eed362016-01-06 22:42:43 +00001533 R->ri_gprmask = GprMask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001534}
1535
1536template <class ELFT>
Rui Ueyama40845e62015-12-26 05:51:07 +00001537void MipsReginfoOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
Rui Ueyama70eed362016-01-06 22:42:43 +00001538 // Copy input object file's .reginfo gprmask to output.
Rui Ueyama40845e62015-12-26 05:51:07 +00001539 auto *S = cast<MipsReginfoInputSection<ELFT>>(C);
Rui Ueyama70eed362016-01-06 22:42:43 +00001540 GprMask |= S->Reginfo->ri_gprmask;
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001541}
1542
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001543namespace lld {
1544namespace elf2 {
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001545template class OutputSectionBase<ELF32LE>;
1546template class OutputSectionBase<ELF32BE>;
1547template class OutputSectionBase<ELF64LE>;
1548template class OutputSectionBase<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001549
George Rimarf6bc65a2016-01-15 13:34:52 +00001550template class EhFrameHeader<ELF32LE>;
1551template class EhFrameHeader<ELF32BE>;
1552template class EhFrameHeader<ELF64LE>;
1553template class EhFrameHeader<ELF64BE>;
1554
George Rimar648a2c32015-10-20 08:54:27 +00001555template class GotPltSection<ELF32LE>;
1556template class GotPltSection<ELF32BE>;
1557template class GotPltSection<ELF64LE>;
1558template class GotPltSection<ELF64BE>;
1559
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001560template class GotSection<ELF32LE>;
1561template class GotSection<ELF32BE>;
1562template class GotSection<ELF64LE>;
1563template class GotSection<ELF64BE>;
1564
1565template class PltSection<ELF32LE>;
1566template class PltSection<ELF32BE>;
1567template class PltSection<ELF64LE>;
1568template class PltSection<ELF64BE>;
1569
1570template class RelocationSection<ELF32LE>;
1571template class RelocationSection<ELF32BE>;
1572template class RelocationSection<ELF64LE>;
1573template class RelocationSection<ELF64BE>;
1574
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001575template class InterpSection<ELF32LE>;
1576template class InterpSection<ELF32BE>;
1577template class InterpSection<ELF64LE>;
1578template class InterpSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001579
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001580template class GnuHashTableSection<ELF32LE>;
1581template class GnuHashTableSection<ELF32BE>;
1582template class GnuHashTableSection<ELF64LE>;
1583template class GnuHashTableSection<ELF64BE>;
1584
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001585template class HashTableSection<ELF32LE>;
1586template class HashTableSection<ELF32BE>;
1587template class HashTableSection<ELF64LE>;
1588template class HashTableSection<ELF64BE>;
1589
1590template class DynamicSection<ELF32LE>;
1591template class DynamicSection<ELF32BE>;
1592template class DynamicSection<ELF64LE>;
1593template class DynamicSection<ELF64BE>;
1594
1595template class OutputSection<ELF32LE>;
1596template class OutputSection<ELF32BE>;
1597template class OutputSection<ELF64LE>;
1598template class OutputSection<ELF64BE>;
1599
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001600template class EHOutputSection<ELF32LE>;
1601template class EHOutputSection<ELF32BE>;
1602template class EHOutputSection<ELF64LE>;
1603template class EHOutputSection<ELF64BE>;
1604
Simon Atanasyan1d7df402015-12-20 10:57:34 +00001605template class MipsReginfoOutputSection<ELF32LE>;
1606template class MipsReginfoOutputSection<ELF32BE>;
1607template class MipsReginfoOutputSection<ELF64LE>;
1608template class MipsReginfoOutputSection<ELF64BE>;
1609
Rafael Espindolac159c962015-10-19 21:00:02 +00001610template class MergeOutputSection<ELF32LE>;
1611template class MergeOutputSection<ELF32BE>;
1612template class MergeOutputSection<ELF64LE>;
1613template class MergeOutputSection<ELF64BE>;
1614
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +00001615template class StringTableSection<ELF32LE>;
1616template class StringTableSection<ELF32BE>;
1617template class StringTableSection<ELF64LE>;
1618template class StringTableSection<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001619
1620template class SymbolTableSection<ELF32LE>;
1621template class SymbolTableSection<ELF32BE>;
1622template class SymbolTableSection<ELF64LE>;
1623template class SymbolTableSection<ELF64BE>;
1624
Rui Ueyama5ec41f32016-01-26 01:32:00 +00001625template uint32_t getLocalRelTarget(const ObjectFile<ELF32LE> &,
1626 const ELFFile<ELF32LE>::Elf_Rel &,
1627 uint32_t);
1628template uint32_t getLocalRelTarget(const ObjectFile<ELF32BE> &,
1629 const ELFFile<ELF32BE>::Elf_Rel &,
1630 uint32_t);
1631template uint64_t getLocalRelTarget(const ObjectFile<ELF64LE> &,
1632 const ELFFile<ELF64LE>::Elf_Rel &,
1633 uint64_t);
1634template uint64_t getLocalRelTarget(const ObjectFile<ELF64BE> &,
1635 const ELFFile<ELF64BE>::Elf_Rel &,
1636 uint64_t);
1637template uint32_t getLocalRelTarget(const ObjectFile<ELF32LE> &,
1638 const ELFFile<ELF32LE>::Elf_Rela &,
1639 uint32_t);
1640template uint32_t getLocalRelTarget(const ObjectFile<ELF32BE> &,
1641 const ELFFile<ELF32BE>::Elf_Rela &,
1642 uint32_t);
1643template uint64_t getLocalRelTarget(const ObjectFile<ELF64LE> &,
1644 const ELFFile<ELF64LE>::Elf_Rela &,
1645 uint64_t);
1646template uint64_t getLocalRelTarget(const ObjectFile<ELF64BE> &,
1647 const ELFFile<ELF64BE>::Elf_Rela &,
1648 uint64_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001649}
1650}