blob: 5011796f083aab66e9ca6124ddb9d367c9bc1e3d [file] [log] [blame]
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +00001//===- SyntheticSections.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// This file contains linker-synthesized sections. Currently,
11// synthetic sections are created either output sections or input sections,
12// but we are rewriting code so that all synthetic sections are created as
13// input sections.
14//
15//===----------------------------------------------------------------------===//
16
17#include "SyntheticSections.h"
18#include "Config.h"
19#include "Error.h"
20#include "InputFiles.h"
Eugene Leviant17b7a572016-11-22 17:49:14 +000021#include "LinkerScript.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000022#include "Memory.h"
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000023#include "OutputSections.h"
24#include "Strings.h"
Rui Ueyamae8a61022016-11-05 23:05:47 +000025#include "SymbolTable.h"
Simon Atanasyance02cf02016-11-09 21:36:56 +000026#include "Target.h"
Rui Ueyama244a4352016-12-03 21:24:51 +000027#include "Threads.h"
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +000028#include "Writer.h"
Rui Ueyama3da3f062016-11-10 20:20:37 +000029#include "lld/Config/Version.h"
Rui Ueyamaac2d8152017-03-01 22:54:50 +000030#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
31#include "llvm/Object/ELFObjectFile.h"
Eugene Leviant952eb4d2016-11-21 15:52:10 +000032#include "llvm/Support/Dwarf.h"
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000033#include "llvm/Support/Endian.h"
34#include "llvm/Support/MD5.h"
35#include "llvm/Support/RandomNumberGenerator.h"
36#include "llvm/Support/SHA1.h"
37#include "llvm/Support/xxhash.h"
Rui Ueyama3da3f062016-11-10 20:20:37 +000038#include <cstdlib>
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000039
40using namespace llvm;
Eugene Leviant952eb4d2016-11-21 15:52:10 +000041using namespace llvm::dwarf;
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000042using namespace llvm::ELF;
43using namespace llvm::object;
44using namespace llvm::support;
45using namespace llvm::support::endian;
46
47using namespace lld;
48using namespace lld::elf;
49
Rui Ueyama9320cb02017-02-27 02:56:02 +000050uint64_t SyntheticSection::getVA() const {
51 if (this->OutSec)
52 return this->OutSec->Addr + this->OutSecOff;
53 return 0;
54}
55
Rui Ueyamae8a61022016-11-05 23:05:47 +000056template <class ELFT> static std::vector<DefinedCommon *> getCommonSymbols() {
57 std::vector<DefinedCommon *> V;
58 for (Symbol *S : Symtab<ELFT>::X->getSymbols())
59 if (auto *B = dyn_cast<DefinedCommon>(S->body()))
60 V.push_back(B);
61 return V;
62}
63
64// Find all common symbols and allocate space for them.
Rafael Espindola774ea7d2017-02-23 16:49:07 +000065template <class ELFT> InputSection *elf::createCommonSection() {
66 auto *Ret = make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1,
67 ArrayRef<uint8_t>(), "COMMON");
Rafael Espindola682a5bc2016-11-08 14:42:34 +000068 Ret->Live = true;
Rui Ueyamae8a61022016-11-05 23:05:47 +000069
Rui Ueyamab2a23cf2017-01-24 03:41:20 +000070 if (!Config->DefineCommon)
71 return Ret;
72
Rui Ueyamae8a61022016-11-05 23:05:47 +000073 // Sort the common symbols by alignment as an heuristic to pack them better.
74 std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>();
75 std::stable_sort(Syms.begin(), Syms.end(),
76 [](const DefinedCommon *A, const DefinedCommon *B) {
77 return A->Alignment > B->Alignment;
78 });
79
80 // Assign offsets to symbols.
81 size_t Size = 0;
82 size_t Alignment = 1;
83 for (DefinedCommon *Sym : Syms) {
Rui Ueyama1c786822016-11-05 23:14:54 +000084 Alignment = std::max<size_t>(Alignment, Sym->Alignment);
Rui Ueyamae8a61022016-11-05 23:05:47 +000085 Size = alignTo(Size, Sym->Alignment);
86
87 // Compute symbol offset relative to beginning of input section.
88 Sym->Offset = Size;
89 Size += Sym->Size;
90 }
Rafael Espindola682a5bc2016-11-08 14:42:34 +000091 Ret->Alignment = Alignment;
92 Ret->Data = makeArrayRef<uint8_t>(nullptr, Size);
93 return Ret;
Rui Ueyamae8a61022016-11-05 23:05:47 +000094}
95
Rui Ueyama3da3f062016-11-10 20:20:37 +000096// Returns an LLD version string.
97static ArrayRef<uint8_t> getVersion() {
98 // Check LLD_VERSION first for ease of testing.
99 // You can get consitent output by using the environment variable.
100 // This is only for testing.
101 StringRef S = getenv("LLD_VERSION");
102 if (S.empty())
103 S = Saver.save(Twine("Linker: ") + getLLDVersion());
104
105 // +1 to include the terminating '\0'.
106 return {(const uint8_t *)S.data(), S.size() + 1};
Davide Italianob69f38f2016-11-11 00:05:41 +0000107}
Rui Ueyama3da3f062016-11-10 20:20:37 +0000108
109// Creates a .comment section containing LLD version info.
110// With this feature, you can identify LLD-generated binaries easily
111// by "objdump -s -j .comment <file>".
112// The returned object is a mergeable string section.
Rafael Espindola6119b862017-03-06 20:23:56 +0000113template <class ELFT> MergeInputSection *elf::createCommentSection() {
Rui Ueyama3da3f062016-11-10 20:20:37 +0000114 typename ELFT::Shdr Hdr = {};
115 Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
116 Hdr.sh_type = SHT_PROGBITS;
117 Hdr.sh_entsize = 1;
118 Hdr.sh_addralign = 1;
119
Rafael Espindola6119b862017-03-06 20:23:56 +0000120 auto *Ret =
121 make<MergeInputSection>((ObjectFile<ELFT> *)nullptr, &Hdr, ".comment");
Rui Ueyama3da3f062016-11-10 20:20:37 +0000122 Ret->Data = getVersion();
123 Ret->splitIntoPieces();
124 return Ret;
125}
126
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000127// .MIPS.abiflags section.
128template <class ELFT>
Rui Ueyama12f2da82016-11-22 03:57:06 +0000129MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags)
Rui Ueyama9320cb02017-02-27 02:56:02 +0000130 : SyntheticSection(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"),
Rui Ueyama27876642017-03-01 04:04:23 +0000131 Flags(Flags) {
132 this->Entsize = sizeof(Elf_Mips_ABIFlags);
133}
Rui Ueyama12f2da82016-11-22 03:57:06 +0000134
135template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
136 memcpy(Buf, &Flags, sizeof(Flags));
137}
138
139template <class ELFT>
140MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
141 Elf_Mips_ABIFlags Flags = {};
142 bool Create = false;
143
Rui Ueyama536a2672017-02-27 02:32:08 +0000144 for (InputSectionBase *Sec : InputSections) {
Rui Ueyama12f2da82016-11-22 03:57:06 +0000145 if (!Sec->Live || Sec->Type != SHT_MIPS_ABIFLAGS)
146 continue;
147 Sec->Live = false;
148 Create = true;
149
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000150 std::string Filename = toString(Sec->getFile<ELFT>());
Simon Atanasyan86dc60d2016-12-21 05:31:57 +0000151 const size_t Size = Sec->Data.size();
152 // Older version of BFD (such as the default FreeBSD linker) concatenate
153 // .MIPS.abiflags instead of merging. To allow for this case (or potential
154 // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
155 if (Size < sizeof(Elf_Mips_ABIFlags)) {
156 error(Filename + ": invalid size of .MIPS.abiflags section: got " +
157 Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
Rui Ueyama12f2da82016-11-22 03:57:06 +0000158 return nullptr;
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000159 }
Rui Ueyama12f2da82016-11-22 03:57:06 +0000160 auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000161 if (S->version != 0) {
Rui Ueyama12f2da82016-11-22 03:57:06 +0000162 error(Filename + ": unexpected .MIPS.abiflags version " +
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000163 Twine(S->version));
Rui Ueyama12f2da82016-11-22 03:57:06 +0000164 return nullptr;
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000165 }
Rui Ueyama12f2da82016-11-22 03:57:06 +0000166
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000167 // LLD checks ISA compatibility in getMipsEFlags(). Here we just
168 // select the highest number of ISA/Rev/Ext.
169 Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
170 Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
171 Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
172 Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
173 Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
174 Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
175 Flags.ases |= S->ases;
176 Flags.flags1 |= S->flags1;
177 Flags.flags2 |= S->flags2;
Rui Ueyama12f2da82016-11-22 03:57:06 +0000178 Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000179 };
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000180
Rui Ueyama12f2da82016-11-22 03:57:06 +0000181 if (Create)
182 return make<MipsAbiFlagsSection<ELFT>>(Flags);
183 return nullptr;
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000184}
185
Simon Atanasyance02cf02016-11-09 21:36:56 +0000186// .MIPS.options section.
187template <class ELFT>
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000188MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo Reginfo)
Rui Ueyama9320cb02017-02-27 02:56:02 +0000189 : SyntheticSection(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"),
Rui Ueyama27876642017-03-01 04:04:23 +0000190 Reginfo(Reginfo) {
191 this->Entsize = sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo);
192}
Simon Atanasyance02cf02016-11-09 21:36:56 +0000193
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000194template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
195 auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
196 Options->kind = ODK_REGINFO;
197 Options->size = getSize();
198
199 if (!Config->Relocatable)
Simon Atanasyan8469b882016-11-23 22:22:16 +0000200 Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp();
Rafael Espindola4862ae82016-11-24 16:38:35 +0000201 memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
Simon Atanasyance02cf02016-11-09 21:36:56 +0000202}
203
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000204template <class ELFT>
205MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
206 // N64 ABI only.
207 if (!ELFT::Is64Bits)
208 return nullptr;
209
210 Elf_Mips_RegInfo Reginfo = {};
211 bool Create = false;
212
Rui Ueyama536a2672017-02-27 02:32:08 +0000213 for (InputSectionBase *Sec : InputSections) {
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000214 if (!Sec->Live || Sec->Type != SHT_MIPS_OPTIONS)
215 continue;
216 Sec->Live = false;
217 Create = true;
218
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000219 std::string Filename = toString(Sec->getFile<ELFT>());
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000220 ArrayRef<uint8_t> D = Sec->Data;
221
222 while (!D.empty()) {
223 if (D.size() < sizeof(Elf_Mips_Options)) {
224 error(Filename + ": invalid size of .MIPS.options section");
225 break;
226 }
227
228 auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
229 if (Opt->kind == ODK_REGINFO) {
230 if (Config->Relocatable && Opt->getRegInfo().ri_gp_value)
231 error(Filename + ": unsupported non-zero ri_gp_value");
232 Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000233 Sec->getFile<ELFT>()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000234 break;
235 }
236
237 if (!Opt->size)
238 fatal(Filename + ": zero option descriptor size");
239 D = D.slice(Opt->size);
240 }
241 };
242
243 if (Create)
Rui Ueyama3cc93d72016-11-22 23:13:08 +0000244 return make<MipsOptionsSection<ELFT>>(Reginfo);
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000245 return nullptr;
Simon Atanasyance02cf02016-11-09 21:36:56 +0000246}
247
248// MIPS .reginfo section.
249template <class ELFT>
Rui Ueyamab71cae92016-11-22 03:57:08 +0000250MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo)
Rui Ueyama9320cb02017-02-27 02:56:02 +0000251 : SyntheticSection(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),
Rui Ueyama27876642017-03-01 04:04:23 +0000252 Reginfo(Reginfo) {
253 this->Entsize = sizeof(Elf_Mips_RegInfo);
254}
Simon Atanasyance02cf02016-11-09 21:36:56 +0000255
Rui Ueyamab71cae92016-11-22 03:57:08 +0000256template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
Simon Atanasyance02cf02016-11-09 21:36:56 +0000257 if (!Config->Relocatable)
Simon Atanasyan8469b882016-11-23 22:22:16 +0000258 Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp();
Rui Ueyamab71cae92016-11-22 03:57:08 +0000259 memcpy(Buf, &Reginfo, sizeof(Reginfo));
260}
261
262template <class ELFT>
263MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
264 // Section should be alive for O32 and N32 ABIs only.
265 if (ELFT::Is64Bits)
266 return nullptr;
267
268 Elf_Mips_RegInfo Reginfo = {};
269 bool Create = false;
270
Rui Ueyama536a2672017-02-27 02:32:08 +0000271 for (InputSectionBase *Sec : InputSections) {
Rui Ueyamab71cae92016-11-22 03:57:08 +0000272 if (!Sec->Live || Sec->Type != SHT_MIPS_REGINFO)
273 continue;
274 Sec->Live = false;
275 Create = true;
276
277 if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000278 error(toString(Sec->getFile<ELFT>()) +
279 ": invalid size of .reginfo section");
Rui Ueyamab71cae92016-11-22 03:57:08 +0000280 return nullptr;
281 }
282 auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
283 if (Config->Relocatable && R->ri_gp_value)
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000284 error(toString(Sec->getFile<ELFT>()) +
285 ": unsupported non-zero ri_gp_value");
Rui Ueyamab71cae92016-11-22 03:57:08 +0000286
287 Reginfo.ri_gprmask |= R->ri_gprmask;
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000288 Sec->getFile<ELFT>()->MipsGp0 = R->ri_gp_value;
Rui Ueyamab71cae92016-11-22 03:57:08 +0000289 };
290
291 if (Create)
292 return make<MipsReginfoSection<ELFT>>(Reginfo);
293 return nullptr;
Simon Atanasyance02cf02016-11-09 21:36:56 +0000294}
295
Rui Ueyama3255a522017-02-27 02:32:49 +0000296InputSection *elf::createInterpSection() {
Rui Ueyama81a4b262016-11-22 04:33:01 +0000297 // StringSaver guarantees that the returned string ends with '\0'.
298 StringRef S = Saver.save(Config->DynamicLinker);
Rui Ueyama6e50fd52017-03-01 07:39:06 +0000299 ArrayRef<uint8_t> Contents = {(const uint8_t *)S.data(), S.size() + 1};
300
301 auto *Sec =
302 make<InputSection>(SHF_ALLOC, SHT_PROGBITS, 1, Contents, ".interp");
303 Sec->Live = true;
304 return Sec;
Rui Ueyamaa9ee8d62016-11-04 22:25:39 +0000305}
Rui Ueyamae288eef2016-11-02 18:58:44 +0000306
Peter Smith96943762017-01-25 10:31:16 +0000307template <class ELFT>
Rui Ueyama65316d72017-02-23 03:15:57 +0000308SymbolBody *elf::addSyntheticLocal(StringRef Name, uint8_t Type, uint64_t Value,
309 uint64_t Size, InputSectionBase *Section) {
Rui Ueyama80474a22017-02-28 19:29:55 +0000310 auto *S = make<DefinedRegular>(Name, /*IsLocal*/ true, STV_DEFAULT, Type,
311 Value, Size, Section, nullptr);
Peter Smith96943762017-01-25 10:31:16 +0000312 if (In<ELFT>::SymTab)
Rui Ueyamab8dcdb52017-02-28 04:20:16 +0000313 In<ELFT>::SymTab->addSymbol(S);
Peter Smith96943762017-01-25 10:31:16 +0000314 return S;
315}
316
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000317static size_t getHashSize() {
Rui Ueyamac4030a12016-11-22 00:54:15 +0000318 switch (Config->BuildId) {
319 case BuildIdKind::Fast:
320 return 8;
321 case BuildIdKind::Md5:
322 case BuildIdKind::Uuid:
323 return 16;
324 case BuildIdKind::Sha1:
325 return 20;
326 case BuildIdKind::Hexstring:
327 return Config->BuildIdVector.size();
328 default:
329 llvm_unreachable("unknown BuildIdKind");
330 }
331}
332
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000333template <class ELFT>
334BuildIdSection<ELFT>::BuildIdSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +0000335 : SyntheticSection(SHF_ALLOC, SHT_NOTE, 1, ".note.gnu.build-id"),
Rui Ueyamabb536fe2016-11-22 01:36:19 +0000336 HashSize(getHashSize()) {}
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000337
338template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
339 const endianness E = ELFT::TargetEndianness;
340 write32<E>(Buf, 4); // Name size
341 write32<E>(Buf + 4, HashSize); // Content size
342 write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type
343 memcpy(Buf + 12, "GNU", 4); // Name string
344 HashBuf = Buf + 16;
345}
346
Rui Ueyama35e00752016-11-10 00:12:28 +0000347// Split one uint8 array into small pieces of uint8 arrays.
George Rimar364b59e22016-11-06 07:42:55 +0000348static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr,
349 size_t ChunkSize) {
350 std::vector<ArrayRef<uint8_t>> Ret;
351 while (Arr.size() > ChunkSize) {
352 Ret.push_back(Arr.take_front(ChunkSize));
353 Arr = Arr.drop_front(ChunkSize);
354 }
355 if (!Arr.empty())
356 Ret.push_back(Arr);
357 return Ret;
358}
359
Rui Ueyama35e00752016-11-10 00:12:28 +0000360// Computes a hash value of Data using a given hash function.
361// In order to utilize multiple cores, we first split data into 1MB
362// chunks, compute a hash for each chunk, and then compute a hash value
363// of the hash values.
George Rimar364b59e22016-11-06 07:42:55 +0000364template <class ELFT>
365void BuildIdSection<ELFT>::computeHash(
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000366 llvm::ArrayRef<uint8_t> Data,
367 std::function<void(uint8_t *Dest, ArrayRef<uint8_t> Arr)> HashFn) {
George Rimar364b59e22016-11-06 07:42:55 +0000368 std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024);
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000369 std::vector<uint8_t> Hashes(Chunks.size() * HashSize);
George Rimar364b59e22016-11-06 07:42:55 +0000370
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000371 // Compute hash values.
Rui Ueyama244a4352016-12-03 21:24:51 +0000372 forLoop(0, Chunks.size(),
373 [&](size_t I) { HashFn(Hashes.data() + I * HashSize, Chunks[I]); });
Rui Ueyama35e00752016-11-10 00:12:28 +0000374
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000375 // Write to the final output buffer.
376 HashFn(HashBuf, Hashes);
George Rimar364b59e22016-11-06 07:42:55 +0000377}
378
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000379template <class ELFT>
Rui Ueyama007c0022017-03-08 17:24:24 +0000380CopyRelSection<ELFT>::CopyRelSection(bool ReadOnly, uintX_t AddrAlign, size_t S)
381 : SyntheticSection(SHF_ALLOC, SHT_NOBITS, AddrAlign,
382 ReadOnly ? ".bss.rel.ro" : ".bss"),
383 Size(S) {}
Peter Smithebfe9942017-02-09 10:27:57 +0000384
385template <class ELFT>
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000386void BuildIdSection<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
Rui Ueyamac4030a12016-11-22 00:54:15 +0000387 switch (Config->BuildId) {
388 case BuildIdKind::Fast:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000389 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
Rui Ueyamac4030a12016-11-22 00:54:15 +0000390 write64le(Dest, xxHash64(toStringRef(Arr)));
391 });
392 break;
393 case BuildIdKind::Md5:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000394 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
Rui Ueyama28590b62016-11-23 18:11:38 +0000395 memcpy(Dest, MD5::hash(Arr).data(), 16);
Rui Ueyamac4030a12016-11-22 00:54:15 +0000396 });
397 break;
398 case BuildIdKind::Sha1:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000399 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
Rui Ueyama28590b62016-11-23 18:11:38 +0000400 memcpy(Dest, SHA1::hash(Arr).data(), 20);
Rui Ueyamac4030a12016-11-22 00:54:15 +0000401 });
402 break;
403 case BuildIdKind::Uuid:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000404 if (getRandomBytes(HashBuf, HashSize))
Rui Ueyamac4030a12016-11-22 00:54:15 +0000405 error("entropy source failure");
406 break;
407 case BuildIdKind::Hexstring:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000408 memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size());
Rui Ueyamac4030a12016-11-22 00:54:15 +0000409 break;
410 default:
411 llvm_unreachable("unknown BuildIdKind");
412 }
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000413}
414
Eugene Leviant41ca3272016-11-10 09:48:29 +0000415template <class ELFT>
Rafael Espindola66b4e212017-02-23 22:06:28 +0000416EhFrameSection<ELFT>::EhFrameSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +0000417 : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame") {}
Rafael Espindola66b4e212017-02-23 22:06:28 +0000418
419// Search for an existing CIE record or create a new one.
420// CIE records from input object files are uniquified by their contents
421// and where their relocations point to.
422template <class ELFT>
423template <class RelTy>
424CieRecord *EhFrameSection<ELFT>::addCie(EhSectionPiece &Piece,
425 ArrayRef<RelTy> Rels) {
Rafael Espindola5c02b742017-03-06 21:17:18 +0000426 auto *Sec = cast<EhInputSection>(Piece.ID);
Rafael Espindola66b4e212017-02-23 22:06:28 +0000427 const endianness E = ELFT::TargetEndianness;
428 if (read32<E>(Piece.data().data() + 4) != 0)
429 fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
430
431 SymbolBody *Personality = nullptr;
432 unsigned FirstRelI = Piece.FirstRelocation;
433 if (FirstRelI != (unsigned)-1)
434 Personality =
435 &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
436
437 // Search for an existing CIE by CIE contents/relocation target pair.
438 CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
439
440 // If not found, create a new one.
441 if (Cie->Piece == nullptr) {
442 Cie->Piece = &Piece;
443 Cies.push_back(Cie);
444 }
445 return Cie;
446}
447
448// There is one FDE per function. Returns true if a given FDE
449// points to a live function.
450template <class ELFT>
451template <class RelTy>
452bool EhFrameSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
453 ArrayRef<RelTy> Rels) {
Rafael Espindola5c02b742017-03-06 21:17:18 +0000454 auto *Sec = cast<EhInputSection>(Piece.ID);
Rafael Espindola66b4e212017-02-23 22:06:28 +0000455 unsigned FirstRelI = Piece.FirstRelocation;
456 if (FirstRelI == (unsigned)-1)
457 return false;
458 const RelTy &Rel = Rels[FirstRelI];
459 SymbolBody &B = Sec->template getFile<ELFT>()->getRelocTargetSym(Rel);
Rui Ueyama80474a22017-02-28 19:29:55 +0000460 auto *D = dyn_cast<DefinedRegular>(&B);
Rafael Espindola66b4e212017-02-23 22:06:28 +0000461 if (!D || !D->Section)
462 return false;
Rafael Espindola5616adf2017-03-08 22:36:28 +0000463 auto *Target =
464 cast<InputSectionBase>(cast<InputSectionBase>(D->Section)->Repl);
Rafael Espindola66b4e212017-02-23 22:06:28 +0000465 return Target && Target->Live;
466}
467
468// .eh_frame is a sequence of CIE or FDE records. In general, there
469// is one CIE record per input object file which is followed by
470// a list of FDEs. This function searches an existing CIE or create a new
471// one and associates FDEs to the CIE.
472template <class ELFT>
473template <class RelTy>
Rafael Espindola5c02b742017-03-06 21:17:18 +0000474void EhFrameSection<ELFT>::addSectionAux(EhInputSection *Sec,
Rafael Espindola66b4e212017-02-23 22:06:28 +0000475 ArrayRef<RelTy> Rels) {
476 const endianness E = ELFT::TargetEndianness;
477
478 DenseMap<size_t, CieRecord *> OffsetToCie;
479 for (EhSectionPiece &Piece : Sec->Pieces) {
480 // The empty record is the end marker.
481 if (Piece.size() == 4)
482 return;
483
484 size_t Offset = Piece.InputOff;
485 uint32_t ID = read32<E>(Piece.data().data() + 4);
486 if (ID == 0) {
487 OffsetToCie[Offset] = addCie(Piece, Rels);
488 continue;
489 }
490
491 uint32_t CieOffset = Offset + 4 - ID;
492 CieRecord *Cie = OffsetToCie[CieOffset];
493 if (!Cie)
494 fatal(toString(Sec) + ": invalid CIE reference");
495
496 if (!isFdeLive(Piece, Rels))
497 continue;
498 Cie->FdePieces.push_back(&Piece);
499 NumFdes++;
500 }
501}
502
503template <class ELFT>
504void EhFrameSection<ELFT>::addSection(InputSectionBase *C) {
Rafael Espindola5c02b742017-03-06 21:17:18 +0000505 auto *Sec = cast<EhInputSection>(C);
Rafael Espindola66b4e212017-02-23 22:06:28 +0000506 Sec->EHSec = this;
507 updateAlignment(Sec->Alignment);
508 Sections.push_back(Sec);
509
510 // .eh_frame is a sequence of CIE or FDE records. This function
511 // splits it into pieces so that we can call
512 // SplitInputSection::getSectionPiece on the section.
Rafael Espindola5c02b742017-03-06 21:17:18 +0000513 Sec->split<ELFT>();
Rafael Espindola66b4e212017-02-23 22:06:28 +0000514 if (Sec->Pieces.empty())
515 return;
516
517 if (Sec->NumRelocations) {
518 if (Sec->AreRelocsRela)
519 addSectionAux(Sec, Sec->template relas<ELFT>());
520 else
521 addSectionAux(Sec, Sec->template rels<ELFT>());
522 return;
523 }
524 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
525}
526
527template <class ELFT>
528static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
529 memcpy(Buf, D.data(), D.size());
530
531 // Fix the size field. -4 since size does not include the size field itself.
532 const endianness E = ELFT::TargetEndianness;
533 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
534}
535
Rui Ueyama945055a2017-02-27 03:07:41 +0000536template <class ELFT> void EhFrameSection<ELFT>::finalizeContents() {
Rafael Espindola66b4e212017-02-23 22:06:28 +0000537 if (this->Size)
538 return; // Already finalized.
539
540 size_t Off = 0;
541 for (CieRecord *Cie : Cies) {
542 Cie->Piece->OutputOff = Off;
543 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
544
545 for (EhSectionPiece *Fde : Cie->FdePieces) {
546 Fde->OutputOff = Off;
547 Off += alignTo(Fde->size(), sizeof(uintX_t));
548 }
549 }
Rafael Espindolab691ccf2017-02-28 18:55:08 +0000550 this->Size = Off;
Rafael Espindola66b4e212017-02-23 22:06:28 +0000551}
552
553template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
554 const endianness E = ELFT::TargetEndianness;
555 switch (Size) {
556 case DW_EH_PE_udata2:
557 return read16<E>(Buf);
558 case DW_EH_PE_udata4:
559 return read32<E>(Buf);
560 case DW_EH_PE_udata8:
561 return read64<E>(Buf);
562 case DW_EH_PE_absptr:
563 if (ELFT::Is64Bits)
564 return read64<E>(Buf);
565 return read32<E>(Buf);
566 }
567 fatal("unknown FDE size encoding");
568}
569
570// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
571// We need it to create .eh_frame_hdr section.
572template <class ELFT>
573typename ELFT::uint EhFrameSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
574 uint8_t Enc) {
575 // The starting address to which this FDE applies is
576 // stored at FDE + 8 byte.
577 size_t Off = FdeOff + 8;
578 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
579 if ((Enc & 0x70) == DW_EH_PE_absptr)
580 return Addr;
581 if ((Enc & 0x70) == DW_EH_PE_pcrel)
582 return Addr + this->OutSec->Addr + Off;
583 fatal("unknown FDE size relative encoding");
584}
585
586template <class ELFT> void EhFrameSection<ELFT>::writeTo(uint8_t *Buf) {
587 const endianness E = ELFT::TargetEndianness;
588 for (CieRecord *Cie : Cies) {
589 size_t CieOffset = Cie->Piece->OutputOff;
590 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
591
592 for (EhSectionPiece *Fde : Cie->FdePieces) {
593 size_t Off = Fde->OutputOff;
594 writeCieFde<ELFT>(Buf + Off, Fde->data());
595
596 // FDE's second word should have the offset to an associated CIE.
597 // Write it.
598 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
599 }
600 }
601
Rafael Espindola5c02b742017-03-06 21:17:18 +0000602 for (EhInputSection *S : Sections)
Rafael Espindola66b4e212017-02-23 22:06:28 +0000603 S->template relocate<ELFT>(Buf, nullptr);
604
605 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
606 // to get a FDE from an address to which FDE is applied. So here
607 // we obtain two addresses and pass them to EhFrameHdr object.
608 if (In<ELFT>::EhFrameHdr) {
609 for (CieRecord *Cie : Cies) {
610 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece);
611 for (SectionPiece *Fde : Cie->FdePieces) {
612 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
613 uintX_t FdeVA = this->OutSec->Addr + Fde->OutputOff;
614 In<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
615 }
616 }
617 }
618}
619
620template <class ELFT>
Eugene Leviantad4439e2016-11-11 11:33:32 +0000621GotSection<ELFT>::GotSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +0000622 : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
623 Target->GotEntrySize, ".got") {}
Eugene Leviantad4439e2016-11-11 11:33:32 +0000624
625template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000626 Sym.GotIndex = NumEntries;
627 ++NumEntries;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000628}
629
Simon Atanasyan725dc142016-11-16 21:01:02 +0000630template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
631 if (Sym.GlobalDynIndex != -1U)
632 return false;
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000633 Sym.GlobalDynIndex = NumEntries;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000634 // Global Dynamic TLS entries take two GOT slots.
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000635 NumEntries += 2;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000636 return true;
637}
638
639// Reserves TLS entries for a TLS module ID and a TLS block offset.
640// In total it takes two GOT slots.
641template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
642 if (TlsIndexOff != uint32_t(-1))
643 return false;
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000644 TlsIndexOff = NumEntries * sizeof(uintX_t);
645 NumEntries += 2;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000646 return true;
647}
648
Eugene Leviantad4439e2016-11-11 11:33:32 +0000649template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000650typename GotSection<ELFT>::uintX_t
651GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
652 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
653}
654
655template <class ELFT>
656typename GotSection<ELFT>::uintX_t
657GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
658 return B.GlobalDynIndex * sizeof(uintX_t);
659}
660
Rui Ueyama945055a2017-02-27 03:07:41 +0000661template <class ELFT> void GotSection<ELFT>::finalizeContents() {
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000662 Size = NumEntries * sizeof(uintX_t);
Simon Atanasyan725dc142016-11-16 21:01:02 +0000663}
664
George Rimar11992c862016-11-25 08:05:41 +0000665template <class ELFT> bool GotSection<ELFT>::empty() const {
666 // If we have a relocation that is relative to GOT (such as GOTOFFREL),
667 // we need to emit a GOT even if it's empty.
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000668 return NumEntries == 0 && !HasGotOffRel;
George Rimar11992c862016-11-25 08:05:41 +0000669}
670
Simon Atanasyan725dc142016-11-16 21:01:02 +0000671template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000672 this->template relocate<ELFT>(Buf, Buf + Size);
Simon Atanasyan725dc142016-11-16 21:01:02 +0000673}
674
675template <class ELFT>
676MipsGotSection<ELFT>::MipsGotSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +0000677 : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
678 ".got") {}
Simon Atanasyan725dc142016-11-16 21:01:02 +0000679
680template <class ELFT>
Rafael Espindola7386cea2017-02-16 00:12:34 +0000681void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, int64_t Addend,
Eugene Leviantad4439e2016-11-11 11:33:32 +0000682 RelExpr Expr) {
683 // For "true" local symbols which can be referenced from the same module
684 // only compiler creates two instructions for address loading:
685 //
686 // lw $8, 0($gp) # R_MIPS_GOT16
687 // addi $8, $8, 0 # R_MIPS_LO16
688 //
689 // The first instruction loads high 16 bits of the symbol address while
690 // the second adds an offset. That allows to reduce number of required
691 // GOT entries because only one global offset table entry is necessary
692 // for every 64 KBytes of local data. So for local symbols we need to
693 // allocate number of GOT entries to hold all required "page" addresses.
694 //
695 // All global symbols (hidden and regular) considered by compiler uniformly.
696 // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
697 // to load address of the symbol. So for each such symbol we need to
698 // allocate dedicated GOT entry to store its address.
699 //
700 // If a symbol is preemptible we need help of dynamic linker to get its
701 // final address. The corresponding GOT entries are allocated in the
702 // "global" part of GOT. Entries for non preemptible global symbol allocated
703 // in the "local" part of GOT.
704 //
705 // See "Global Offset Table" in Chapter 5:
706 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
707 if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
708 // At this point we do not know final symbol value so to reduce number
709 // of allocated GOT entries do the following trick. Save all output
710 // sections referenced by GOT relocations. Then later in the `finalize`
711 // method calculate number of "pages" required to cover all saved output
712 // section and allocate appropriate number of GOT entries.
Rui Ueyama80474a22017-02-28 19:29:55 +0000713 auto *DefSym = cast<DefinedRegular>(&Sym);
Rafael Espindola5e434b32017-03-08 16:08:36 +0000714 PageIndexMap.insert({DefSym->Section->getOutputSection(), 0});
Eugene Leviantad4439e2016-11-11 11:33:32 +0000715 return;
716 }
717 if (Sym.isTls()) {
718 // GOT entries created for MIPS TLS relocations behave like
719 // almost GOT entries from other ABIs. They go to the end
720 // of the global offset table.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000721 Sym.GotIndex = TlsEntries.size();
722 TlsEntries.push_back(&Sym);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000723 return;
724 }
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000725 auto AddEntry = [&](SymbolBody &S, uintX_t A, GotEntries &Items) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000726 if (S.isInGot() && !A)
727 return;
728 size_t NewIndex = Items.size();
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000729 if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second)
Eugene Leviantad4439e2016-11-11 11:33:32 +0000730 return;
731 Items.emplace_back(&S, A);
732 if (!A)
733 S.GotIndex = NewIndex;
734 };
735 if (Sym.isPreemptible()) {
736 // Ignore addends for preemptible symbols. They got single GOT entry anyway.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000737 AddEntry(Sym, 0, GlobalEntries);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000738 Sym.IsInGlobalMipsGot = true;
739 } else if (Expr == R_MIPS_GOT_OFF32) {
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000740 AddEntry(Sym, Addend, LocalEntries32);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000741 Sym.Is32BitMipsGot = true;
742 } else {
743 // Hold local GOT entries accessed via a 16-bit index separately.
744 // That allows to write them in the beginning of the GOT and keep
745 // their indexes as less as possible to escape relocation's overflow.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000746 AddEntry(Sym, Addend, LocalEntries);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000747 }
748}
749
George Rimar879a6572016-12-15 15:38:58 +0000750template <class ELFT>
751bool MipsGotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000752 if (Sym.GlobalDynIndex != -1U)
753 return false;
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000754 Sym.GlobalDynIndex = TlsEntries.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000755 // Global Dynamic TLS entries take two GOT slots.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000756 TlsEntries.push_back(nullptr);
757 TlsEntries.push_back(&Sym);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000758 return true;
759}
760
761// Reserves TLS entries for a TLS module ID and a TLS block offset.
762// In total it takes two GOT slots.
Simon Atanasyan725dc142016-11-16 21:01:02 +0000763template <class ELFT> bool MipsGotSection<ELFT>::addTlsIndex() {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000764 if (TlsIndexOff != uint32_t(-1))
765 return false;
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000766 TlsIndexOff = TlsEntries.size() * sizeof(uintX_t);
767 TlsEntries.push_back(nullptr);
768 TlsEntries.push_back(nullptr);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000769 return true;
770}
771
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000772static uint64_t getMipsPageAddr(uint64_t Addr) {
773 return (Addr + 0x8000) & ~0xffff;
774}
775
776static uint64_t getMipsPageCount(uint64_t Size) {
777 return (Size + 0xfffe) / 0xffff + 1;
778}
779
Eugene Leviantad4439e2016-11-11 11:33:32 +0000780template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000781typename MipsGotSection<ELFT>::uintX_t
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000782MipsGotSection<ELFT>::getPageEntryOffset(const SymbolBody &B,
Rafael Espindola7386cea2017-02-16 00:12:34 +0000783 int64_t Addend) const {
Rafael Espindola24e6f362017-02-24 15:07:30 +0000784 const OutputSection *OutSec =
Rafael Espindola5e434b32017-03-08 16:08:36 +0000785 cast<DefinedRegular>(&B)->Section->getOutputSection();
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000786 uintX_t SecAddr = getMipsPageAddr(OutSec->Addr);
787 uintX_t SymAddr = getMipsPageAddr(B.getVA<ELFT>(Addend));
788 uintX_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff;
789 assert(Index < PageEntriesNum);
790 return (HeaderEntriesNum + Index) * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000791}
792
793template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000794typename MipsGotSection<ELFT>::uintX_t
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000795MipsGotSection<ELFT>::getBodyEntryOffset(const SymbolBody &B,
Rafael Espindola7386cea2017-02-16 00:12:34 +0000796 int64_t Addend) const {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000797 // Calculate offset of the GOT entries block: TLS, global, local.
Simon Atanasyana0efc422016-11-29 10:23:50 +0000798 uintX_t Index = HeaderEntriesNum + PageEntriesNum;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000799 if (B.isTls())
Simon Atanasyana0efc422016-11-29 10:23:50 +0000800 Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000801 else if (B.IsInGlobalMipsGot)
Simon Atanasyana0efc422016-11-29 10:23:50 +0000802 Index += LocalEntries.size() + LocalEntries32.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000803 else if (B.Is32BitMipsGot)
Simon Atanasyana0efc422016-11-29 10:23:50 +0000804 Index += LocalEntries.size();
805 // Calculate offset of the GOT entry in the block.
Eugene Leviantad4439e2016-11-11 11:33:32 +0000806 if (B.isInGot())
Simon Atanasyana0efc422016-11-29 10:23:50 +0000807 Index += B.GotIndex;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000808 else {
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000809 auto It = EntryIndexMap.find({&B, Addend});
810 assert(It != EntryIndexMap.end());
Simon Atanasyana0efc422016-11-29 10:23:50 +0000811 Index += It->second;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000812 }
Simon Atanasyana0efc422016-11-29 10:23:50 +0000813 return Index * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000814}
815
816template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000817typename MipsGotSection<ELFT>::uintX_t
818MipsGotSection<ELFT>::getTlsOffset() const {
819 return (getLocalEntriesNum() + GlobalEntries.size()) * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000820}
821
822template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000823typename MipsGotSection<ELFT>::uintX_t
824MipsGotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000825 return B.GlobalDynIndex * sizeof(uintX_t);
826}
827
828template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000829const SymbolBody *MipsGotSection<ELFT>::getFirstGlobalEntry() const {
830 return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000831}
832
833template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000834unsigned MipsGotSection<ELFT>::getLocalEntriesNum() const {
Simon Atanasyana0efc422016-11-29 10:23:50 +0000835 return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() +
836 LocalEntries32.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000837}
838
Rui Ueyama945055a2017-02-27 03:07:41 +0000839template <class ELFT> void MipsGotSection<ELFT>::finalizeContents() {
Peter Smith1ec42d92017-03-08 14:06:24 +0000840 updateAllocSize();
841}
842
843template <class ELFT> void MipsGotSection<ELFT>::updateAllocSize() {
Simon Atanasyana0efc422016-11-29 10:23:50 +0000844 PageEntriesNum = 0;
Rafael Espindola24e6f362017-02-24 15:07:30 +0000845 for (std::pair<const OutputSection *, size_t> &P : PageIndexMap) {
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000846 // For each output section referenced by GOT page relocations calculate
847 // and save into PageIndexMap an upper bound of MIPS GOT entries required
848 // to store page addresses of local symbols. We assume the worst case -
849 // each 64kb page of the output section has at least one GOT relocation
850 // against it. And take in account the case when the section intersects
851 // page boundaries.
852 P.second = PageEntriesNum;
853 PageEntriesNum += getMipsPageCount(P.first->Size);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000854 }
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000855 Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) *
856 sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000857}
858
George Rimar11992c862016-11-25 08:05:41 +0000859template <class ELFT> bool MipsGotSection<ELFT>::empty() const {
860 // We add the .got section to the result for dynamic MIPS target because
861 // its address and properties are mentioned in the .dynamic section.
862 return Config->Relocatable;
863}
864
Simon Atanasyanb9666652016-12-12 14:30:18 +0000865template <class ELFT>
866typename MipsGotSection<ELFT>::uintX_t MipsGotSection<ELFT>::getGp() const {
Rui Ueyama80474a22017-02-28 19:29:55 +0000867 return ElfSym::MipsGp->template getVA<ELFT>(0);
Simon Atanasyan8469b882016-11-23 22:22:16 +0000868}
869
Eugene Leviantad4439e2016-11-11 11:33:32 +0000870template <class ELFT>
871static void writeUint(uint8_t *Buf, typename ELFT::uint Val) {
872 typedef typename ELFT::uint uintX_t;
873 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val);
874}
875
Simon Atanasyan725dc142016-11-16 21:01:02 +0000876template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000877 // Set the MSB of the second GOT slot. This is not required by any
878 // MIPS ABI documentation, though.
879 //
880 // There is a comment in glibc saying that "The MSB of got[1] of a
881 // gnu object is set to identify gnu objects," and in GNU gold it
882 // says "the second entry will be used by some runtime loaders".
883 // But how this field is being used is unclear.
884 //
885 // We are not really willing to mimic other linkers behaviors
886 // without understanding why they do that, but because all files
887 // generated by GNU tools have this special GOT value, and because
888 // we've been doing this for years, it is probably a safe bet to
889 // keep doing this for now. We really need to revisit this to see
890 // if we had to do this.
891 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
892 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyana0efc422016-11-29 10:23:50 +0000893 Buf += HeaderEntriesNum * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000894 // Write 'page address' entries to the local part of the GOT.
Rafael Espindola24e6f362017-02-24 15:07:30 +0000895 for (std::pair<const OutputSection *, size_t> &L : PageIndexMap) {
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000896 size_t PageCount = getMipsPageCount(L.first->Size);
897 uintX_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
898 for (size_t PI = 0; PI < PageCount; ++PI) {
899 uint8_t *Entry = Buf + (L.second + PI) * sizeof(uintX_t);
900 writeUint<ELFT>(Entry, FirstPageAddr + PI * 0x10000);
901 }
Eugene Leviantad4439e2016-11-11 11:33:32 +0000902 }
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000903 Buf += PageEntriesNum * sizeof(uintX_t);
904 auto AddEntry = [&](const GotEntry &SA) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000905 uint8_t *Entry = Buf;
906 Buf += sizeof(uintX_t);
907 const SymbolBody *Body = SA.first;
908 uintX_t VA = Body->template getVA<ELFT>(SA.second);
909 writeUint<ELFT>(Entry, VA);
910 };
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000911 std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry);
912 std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry);
913 std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000914 // Initialize TLS-related GOT entries. If the entry has a corresponding
915 // dynamic relocations, leave it initialized by zero. Write down adjusted
916 // TLS symbol's values otherwise. To calculate the adjustments use offsets
917 // for thread-local storage.
918 // https://www.linux-mips.org/wiki/NPTL
Rui Ueyama104e2352017-02-14 05:45:47 +0000919 if (TlsIndexOff != -1U && !Config->pic())
Eugene Leviantad4439e2016-11-11 11:33:32 +0000920 writeUint<ELFT>(Buf + TlsIndexOff, 1);
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000921 for (const SymbolBody *B : TlsEntries) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000922 if (!B || B->isPreemptible())
923 continue;
924 uintX_t VA = B->getVA<ELFT>();
925 if (B->GotIndex != -1U) {
926 uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t);
927 writeUint<ELFT>(Entry, VA - 0x7000);
928 }
929 if (B->GlobalDynIndex != -1U) {
930 uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t);
931 writeUint<ELFT>(Entry, 1);
932 Entry += sizeof(uintX_t);
933 writeUint<ELFT>(Entry, VA - 0x8000);
934 }
935 }
936}
937
Eugene Leviantad4439e2016-11-11 11:33:32 +0000938template <class ELFT>
Eugene Leviant41ca3272016-11-10 09:48:29 +0000939GotPltSection<ELFT>::GotPltSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +0000940 : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
941 Target->GotPltEntrySize, ".got.plt") {}
Eugene Leviant41ca3272016-11-10 09:48:29 +0000942
943template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
944 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
945 Entries.push_back(&Sym);
946}
947
Eugene Leviant41ca3272016-11-10 09:48:29 +0000948template <class ELFT> size_t GotPltSection<ELFT>::getSize() const {
949 return (Target->GotPltHeaderEntriesNum + Entries.size()) *
950 Target->GotPltEntrySize;
951}
952
953template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
954 Target->writeGotPltHeader(Buf);
955 Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
956 for (const SymbolBody *B : Entries) {
957 Target->writeGotPlt(Buf, *B);
958 Buf += sizeof(uintX_t);
959 }
960}
961
Peter Smithbaffdb82016-12-08 12:58:55 +0000962// On ARM the IgotPltSection is part of the GotSection, on other Targets it is
963// part of the .got.plt
964template <class ELFT>
965IgotPltSection<ELFT>::IgotPltSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +0000966 : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
967 Target->GotPltEntrySize,
968 Config->EMachine == EM_ARM ? ".got" : ".got.plt") {}
Peter Smithbaffdb82016-12-08 12:58:55 +0000969
970template <class ELFT> void IgotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
971 Sym.IsInIgot = true;
972 Sym.GotPltIndex = Entries.size();
973 Entries.push_back(&Sym);
974}
975
976template <class ELFT> size_t IgotPltSection<ELFT>::getSize() const {
977 return Entries.size() * Target->GotPltEntrySize;
978}
979
980template <class ELFT> void IgotPltSection<ELFT>::writeTo(uint8_t *Buf) {
981 for (const SymbolBody *B : Entries) {
Peter Smith4b360292016-12-09 09:59:54 +0000982 Target->writeIgotPlt(Buf, *B);
Peter Smithbaffdb82016-12-08 12:58:55 +0000983 Buf += sizeof(uintX_t);
984 }
985}
986
Eugene Leviant22eb0262016-11-14 09:16:00 +0000987template <class ELFT>
988StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
Rui Ueyama9320cb02017-02-27 02:56:02 +0000989 : SyntheticSection(Dynamic ? (uintX_t)SHF_ALLOC : 0, SHT_STRTAB, 1, Name),
Rafael Espindola1b36eea2017-02-15 00:23:09 +0000990 Dynamic(Dynamic) {
991 // ELF string tables start with a NUL byte.
992 addString("");
993}
Eugene Leviant22eb0262016-11-14 09:16:00 +0000994
995// Adds a string to the string table. If HashIt is true we hash and check for
996// duplicates. It is optional because the name of global symbols are already
997// uniqued and hashing them again has a big cost for a small value: uniquing
998// them with some other string that happens to be the same.
999template <class ELFT>
1000unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
1001 if (HashIt) {
1002 auto R = StringMap.insert(std::make_pair(S, this->Size));
1003 if (!R.second)
1004 return R.first->second;
1005 }
1006 unsigned Ret = this->Size;
1007 this->Size = this->Size + S.size() + 1;
1008 Strings.push_back(S);
1009 return Ret;
1010}
1011
1012template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviant22eb0262016-11-14 09:16:00 +00001013 for (StringRef S : Strings) {
1014 memcpy(Buf, S.data(), S.size());
1015 Buf += S.size() + 1;
1016 }
1017}
1018
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001019// Returns the number of version definition entries. Because the first entry
1020// is for the version definition itself, it is the number of versioned symbols
1021// plus one. Note that we don't support multiple versions yet.
Eugene Leviant6380ce22016-11-15 12:26:55 +00001022static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
1023
1024template <class ELFT>
1025DynamicSection<ELFT>::DynamicSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +00001026 : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC, sizeof(uintX_t),
1027 ".dynamic") {
Eugene Leviant6380ce22016-11-15 12:26:55 +00001028 this->Entsize = ELFT::Is64Bits ? 16 : 8;
Rui Ueyama27876642017-03-01 04:04:23 +00001029
Eugene Leviant6380ce22016-11-15 12:26:55 +00001030 // .dynamic section is not writable on MIPS.
1031 // See "Special Section" in Chapter 4 in the following document:
1032 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1033 if (Config->EMachine == EM_MIPS)
1034 this->Flags = SHF_ALLOC;
1035
1036 addEntries();
1037}
1038
1039// There are some dynamic entries that don't depend on other sections.
1040// Such entries can be set early.
1041template <class ELFT> void DynamicSection<ELFT>::addEntries() {
1042 // Add strings to .dynstr early so that .dynstr's size will be
1043 // fixed early.
1044 for (StringRef S : Config->AuxiliaryList)
Rui Ueyama729ac792016-11-17 04:10:09 +00001045 add({DT_AUXILIARY, In<ELFT>::DynStrTab->addString(S)});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001046 if (!Config->RPath.empty())
Rui Ueyama729ac792016-11-17 04:10:09 +00001047 add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Eugene Leviant6380ce22016-11-15 12:26:55 +00001048 In<ELFT>::DynStrTab->addString(Config->RPath)});
1049 for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
1050 if (F->isNeeded())
Rui Ueyama729ac792016-11-17 04:10:09 +00001051 add({DT_NEEDED, In<ELFT>::DynStrTab->addString(F->getSoName())});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001052 if (!Config->SoName.empty())
Rui Ueyama729ac792016-11-17 04:10:09 +00001053 add({DT_SONAME, In<ELFT>::DynStrTab->addString(Config->SoName)});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001054
1055 // Set DT_FLAGS and DT_FLAGS_1.
1056 uint32_t DtFlags = 0;
1057 uint32_t DtFlags1 = 0;
1058 if (Config->Bsymbolic)
1059 DtFlags |= DF_SYMBOLIC;
1060 if (Config->ZNodelete)
1061 DtFlags1 |= DF_1_NODELETE;
1062 if (Config->ZNow) {
1063 DtFlags |= DF_BIND_NOW;
1064 DtFlags1 |= DF_1_NOW;
1065 }
1066 if (Config->ZOrigin) {
1067 DtFlags |= DF_ORIGIN;
1068 DtFlags1 |= DF_1_ORIGIN;
1069 }
1070
1071 if (DtFlags)
Rui Ueyama729ac792016-11-17 04:10:09 +00001072 add({DT_FLAGS, DtFlags});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001073 if (DtFlags1)
Rui Ueyama729ac792016-11-17 04:10:09 +00001074 add({DT_FLAGS_1, DtFlags1});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001075
Petr Hosek668bebe2016-12-07 02:05:42 +00001076 if (!Config->Shared && !Config->Relocatable)
Rui Ueyama729ac792016-11-17 04:10:09 +00001077 add({DT_DEBUG, (uint64_t)0});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001078}
1079
1080// Add remaining entries to complete .dynamic contents.
Rui Ueyama945055a2017-02-27 03:07:41 +00001081template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
Eugene Leviant6380ce22016-11-15 12:26:55 +00001082 if (this->Size)
1083 return; // Already finalized.
1084
1085 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
Peter Smithbaffdb82016-12-08 12:58:55 +00001086 if (In<ELFT>::RelaDyn->OutSec->Size > 0) {
Rui Ueyamaaf6198d2017-03-07 00:43:53 +00001087 bool IsRela = Config->isRela();
Rui Ueyama729ac792016-11-17 04:10:09 +00001088 add({IsRela ? DT_RELA : DT_REL, In<ELFT>::RelaDyn});
Peter Smithbaffdb82016-12-08 12:58:55 +00001089 add({IsRela ? DT_RELASZ : DT_RELSZ, In<ELFT>::RelaDyn->OutSec->Size});
Rui Ueyama729ac792016-11-17 04:10:09 +00001090 add({IsRela ? DT_RELAENT : DT_RELENT,
Eugene Leviant6380ce22016-11-15 12:26:55 +00001091 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
1092
1093 // MIPS dynamic loader does not support RELCOUNT tag.
1094 // The problem is in the tight relation between dynamic
1095 // relocations and GOT. So do not emit this tag on MIPS.
1096 if (Config->EMachine != EM_MIPS) {
Eugene Levianta96d9022016-11-16 10:02:27 +00001097 size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
Eugene Leviant6380ce22016-11-15 12:26:55 +00001098 if (Config->ZCombreloc && NumRelativeRels)
Rui Ueyama729ac792016-11-17 04:10:09 +00001099 add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001100 }
1101 }
Peter Smithbaffdb82016-12-08 12:58:55 +00001102 if (In<ELFT>::RelaPlt->OutSec->Size > 0) {
Rui Ueyama729ac792016-11-17 04:10:09 +00001103 add({DT_JMPREL, In<ELFT>::RelaPlt});
Peter Smithbaffdb82016-12-08 12:58:55 +00001104 add({DT_PLTRELSZ, In<ELFT>::RelaPlt->OutSec->Size});
Rui Ueyama729ac792016-11-17 04:10:09 +00001105 add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
Eugene Leviant6380ce22016-11-15 12:26:55 +00001106 In<ELFT>::GotPlt});
Rui Ueyamaaf6198d2017-03-07 00:43:53 +00001107 add({DT_PLTREL, uint64_t(Config->isRela() ? DT_RELA : DT_REL)});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001108 }
1109
Eugene Leviant9230db92016-11-17 09:16:34 +00001110 add({DT_SYMTAB, In<ELFT>::DynSymTab});
Rui Ueyama729ac792016-11-17 04:10:09 +00001111 add({DT_SYMENT, sizeof(Elf_Sym)});
1112 add({DT_STRTAB, In<ELFT>::DynStrTab});
1113 add({DT_STRSZ, In<ELFT>::DynStrTab->getSize()});
George Rimar0a7412f2017-03-09 08:48:34 +00001114 if (!Config->ZText)
1115 add({DT_TEXTREL, (uint64_t)0});
Eugene Leviantbe809a72016-11-18 06:44:18 +00001116 if (In<ELFT>::GnuHashTab)
1117 add({DT_GNU_HASH, In<ELFT>::GnuHashTab});
Eugene Leviantb96e8092016-11-18 09:06:47 +00001118 if (In<ELFT>::HashTab)
1119 add({DT_HASH, In<ELFT>::HashTab});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001120
Rui Ueyama9d1bacb12017-02-27 02:31:26 +00001121 if (Out::PreinitArray) {
1122 add({DT_PREINIT_ARRAY, Out::PreinitArray});
1123 add({DT_PREINIT_ARRAYSZ, Out::PreinitArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001124 }
Rui Ueyama9d1bacb12017-02-27 02:31:26 +00001125 if (Out::InitArray) {
1126 add({DT_INIT_ARRAY, Out::InitArray});
1127 add({DT_INIT_ARRAYSZ, Out::InitArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001128 }
Rui Ueyama9d1bacb12017-02-27 02:31:26 +00001129 if (Out::FiniArray) {
1130 add({DT_FINI_ARRAY, Out::FiniArray});
1131 add({DT_FINI_ARRAYSZ, Out::FiniArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001132 }
1133
Rafael Espindola1d6d1b42017-01-17 16:08:06 +00001134 if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Init))
Rui Ueyama729ac792016-11-17 04:10:09 +00001135 add({DT_INIT, B});
Rafael Espindola1d6d1b42017-01-17 16:08:06 +00001136 if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Fini))
Rui Ueyama729ac792016-11-17 04:10:09 +00001137 add({DT_FINI, B});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001138
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001139 bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
1140 if (HasVerNeed || In<ELFT>::VerDef)
1141 add({DT_VERSYM, In<ELFT>::VerSym});
1142 if (In<ELFT>::VerDef) {
1143 add({DT_VERDEF, In<ELFT>::VerDef});
Rui Ueyama729ac792016-11-17 04:10:09 +00001144 add({DT_VERDEFNUM, getVerDefNum()});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001145 }
1146 if (HasVerNeed) {
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001147 add({DT_VERNEED, In<ELFT>::VerNeed});
1148 add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001149 }
1150
1151 if (Config->EMachine == EM_MIPS) {
Rui Ueyama729ac792016-11-17 04:10:09 +00001152 add({DT_MIPS_RLD_VERSION, 1});
1153 add({DT_MIPS_FLAGS, RHF_NOTPOT});
1154 add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
Eugene Leviant9230db92016-11-17 09:16:34 +00001155 add({DT_MIPS_SYMTABNO, In<ELFT>::DynSymTab->getNumSymbols()});
Simon Atanasyanb8bfec62016-11-17 21:49:14 +00001156 add({DT_MIPS_LOCAL_GOTNO, In<ELFT>::MipsGot->getLocalEntriesNum()});
1157 if (const SymbolBody *B = In<ELFT>::MipsGot->getFirstGlobalEntry())
Rui Ueyama729ac792016-11-17 04:10:09 +00001158 add({DT_MIPS_GOTSYM, B->DynsymIndex});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001159 else
Eugene Leviant9230db92016-11-17 09:16:34 +00001160 add({DT_MIPS_GOTSYM, In<ELFT>::DynSymTab->getNumSymbols()});
Rui Ueyama729ac792016-11-17 04:10:09 +00001161 add({DT_PLTGOT, In<ELFT>::MipsGot});
Eugene Leviant17b7a572016-11-22 17:49:14 +00001162 if (In<ELFT>::MipsRldMap)
1163 add({DT_MIPS_RLD_MAP, In<ELFT>::MipsRldMap});
Eugene Leviant6380ce22016-11-15 12:26:55 +00001164 }
1165
Eugene Leviant6380ce22016-11-15 12:26:55 +00001166 this->OutSec->Link = this->Link;
1167
1168 // +1 for DT_NULL
1169 this->Size = (Entries.size() + 1) * this->Entsize;
1170}
1171
1172template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
1173 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
1174
1175 for (const Entry &E : Entries) {
1176 P->d_tag = E.Tag;
1177 switch (E.Kind) {
1178 case Entry::SecAddr:
1179 P->d_un.d_ptr = E.OutSec->Addr;
1180 break;
1181 case Entry::InSecAddr:
1182 P->d_un.d_ptr = E.InSec->OutSec->Addr + E.InSec->OutSecOff;
1183 break;
1184 case Entry::SecSize:
1185 P->d_un.d_val = E.OutSec->Size;
1186 break;
1187 case Entry::SymAddr:
1188 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
1189 break;
1190 case Entry::PlainInt:
1191 P->d_un.d_val = E.Val;
1192 break;
1193 }
1194 ++P;
1195 }
1196}
1197
Eugene Levianta96d9022016-11-16 10:02:27 +00001198template <class ELFT>
1199typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
Rafael Espindolae1294092017-03-08 16:03:41 +00001200 return InputSec->OutSec->Addr + InputSec->getOffset(OffsetInSec);
Eugene Levianta96d9022016-11-16 10:02:27 +00001201}
1202
Rafael Espindola7386cea2017-02-16 00:12:34 +00001203template <class ELFT> int64_t DynamicReloc<ELFT>::getAddend() const {
Eugene Levianta96d9022016-11-16 10:02:27 +00001204 if (UseSymVA)
1205 return Sym->getVA<ELFT>(Addend);
1206 return Addend;
1207}
1208
1209template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const {
1210 if (Sym && !UseSymVA)
1211 return Sym->DynsymIndex;
1212 return 0;
1213}
1214
1215template <class ELFT>
1216RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
Rui Ueyamaaf6198d2017-03-07 00:43:53 +00001217 : SyntheticSection(SHF_ALLOC, Config->isRela() ? SHT_RELA : SHT_REL,
Rui Ueyama9320cb02017-02-27 02:56:02 +00001218 sizeof(uintX_t), Name),
Eugene Levianta96d9022016-11-16 10:02:27 +00001219 Sort(Sort) {
Rui Ueyamaaf6198d2017-03-07 00:43:53 +00001220 this->Entsize = Config->isRela() ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Eugene Levianta96d9022016-11-16 10:02:27 +00001221}
1222
1223template <class ELFT>
1224void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
1225 if (Reloc.Type == Target->RelativeRel)
1226 ++NumRelativeRelocs;
1227 Relocs.push_back(Reloc);
1228}
1229
1230template <class ELFT, class RelTy>
1231static bool compRelocations(const RelTy &A, const RelTy &B) {
Rui Ueyamadf8eb172017-03-07 00:43:33 +00001232 bool AIsRel = A.getType(Config->isMips64EL()) == Target->RelativeRel;
1233 bool BIsRel = B.getType(Config->isMips64EL()) == Target->RelativeRel;
Eugene Levianta96d9022016-11-16 10:02:27 +00001234 if (AIsRel != BIsRel)
1235 return AIsRel;
1236
Rui Ueyamadf8eb172017-03-07 00:43:33 +00001237 return A.getSymbol(Config->isMips64EL()) < B.getSymbol(Config->isMips64EL());
Eugene Levianta96d9022016-11-16 10:02:27 +00001238}
1239
1240template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1241 uint8_t *BufBegin = Buf;
1242 for (const DynamicReloc<ELFT> &Rel : Relocs) {
1243 auto *P = reinterpret_cast<Elf_Rela *>(Buf);
Rui Ueyamaaf6198d2017-03-07 00:43:53 +00001244 Buf += Config->isRela() ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
Eugene Levianta96d9022016-11-16 10:02:27 +00001245
Rui Ueyamaaf6198d2017-03-07 00:43:53 +00001246 if (Config->isRela())
Eugene Levianta96d9022016-11-16 10:02:27 +00001247 P->r_addend = Rel.getAddend();
1248 P->r_offset = Rel.getOffset();
Simon Atanasyan725dc142016-11-16 21:01:02 +00001249 if (Config->EMachine == EM_MIPS && Rel.getInputSec() == In<ELFT>::MipsGot)
Eugene Levianta96d9022016-11-16 10:02:27 +00001250 // Dynamic relocation against MIPS GOT section make deal TLS entries
1251 // allocated in the end of the GOT. We need to adjust the offset to take
1252 // in account 'local' and 'global' GOT entries.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +00001253 P->r_offset += In<ELFT>::MipsGot->getTlsOffset();
Rui Ueyamadf8eb172017-03-07 00:43:33 +00001254 P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->isMips64EL());
Eugene Levianta96d9022016-11-16 10:02:27 +00001255 }
1256
1257 if (Sort) {
Rui Ueyamaaf6198d2017-03-07 00:43:53 +00001258 if (Config->isRela())
Eugene Levianta96d9022016-11-16 10:02:27 +00001259 std::stable_sort((Elf_Rela *)BufBegin,
1260 (Elf_Rela *)BufBegin + Relocs.size(),
1261 compRelocations<ELFT, Elf_Rela>);
1262 else
1263 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1264 compRelocations<ELFT, Elf_Rel>);
1265 }
1266}
1267
1268template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1269 return this->Entsize * Relocs.size();
1270}
1271
Rui Ueyama945055a2017-02-27 03:07:41 +00001272template <class ELFT> void RelocationSection<ELFT>::finalizeContents() {
Eugene Leviant9230db92016-11-17 09:16:34 +00001273 this->Link = In<ELFT>::DynSymTab ? In<ELFT>::DynSymTab->OutSec->SectionIndex
1274 : In<ELFT>::SymTab->OutSec->SectionIndex;
Eugene Levianta96d9022016-11-16 10:02:27 +00001275
1276 // Set required output section properties.
1277 this->OutSec->Link = this->Link;
Eugene Levianta96d9022016-11-16 10:02:27 +00001278}
1279
Eugene Leviant9230db92016-11-17 09:16:34 +00001280template <class ELFT>
1281SymbolTableSection<ELFT>::SymbolTableSection(
1282 StringTableSection<ELFT> &StrTabSec)
Rui Ueyama9320cb02017-02-27 02:56:02 +00001283 : SyntheticSection(StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0,
1284 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1285 sizeof(uintX_t),
1286 StrTabSec.isDynamic() ? ".dynsym" : ".symtab"),
Eugene Leviant9230db92016-11-17 09:16:34 +00001287 StrTabSec(StrTabSec) {
1288 this->Entsize = sizeof(Elf_Sym);
1289}
1290
1291// Orders symbols according to their positions in the GOT,
1292// in compliance with MIPS ABI rules.
1293// See "Global Offset Table" in Chapter 5 in the following document
1294// for detailed description:
1295// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Rui Ueyamaaa5c5272017-02-27 03:31:38 +00001296static bool sortMipsSymbols(const SymbolTableEntry &L, const SymbolTableEntry &R) {
Eugene Leviant9230db92016-11-17 09:16:34 +00001297 // Sort entries related to non-local preemptible symbols by GOT indexes.
1298 // All other entries go to the first part of GOT in arbitrary order.
Rui Ueyamaaa5c5272017-02-27 03:31:38 +00001299 bool LIsInLocalGot = !L.Symbol->IsInGlobalMipsGot;
1300 bool RIsInLocalGot = !R.Symbol->IsInGlobalMipsGot;
Eugene Leviant9230db92016-11-17 09:16:34 +00001301 if (LIsInLocalGot || RIsInLocalGot)
1302 return !RIsInLocalGot;
Rui Ueyamaaa5c5272017-02-27 03:31:38 +00001303 return L.Symbol->GotIndex < R.Symbol->GotIndex;
Eugene Leviant9230db92016-11-17 09:16:34 +00001304}
1305
Rui Ueyamabb07d102017-02-27 03:31:19 +00001306// Finalize a symbol table. The ELF spec requires that all local
1307// symbols precede global symbols, so we sort symbol entries in this
1308// function. (For .dynsym, we don't do that because symbols for
1309// dynamic linking are inherently all globals.)
Rui Ueyama945055a2017-02-27 03:07:41 +00001310template <class ELFT> void SymbolTableSection<ELFT>::finalizeContents() {
Rui Ueyama6e967342017-02-28 03:29:12 +00001311 this->OutSec->Link = StrTabSec.OutSec->SectionIndex;
Eugene Leviant9230db92016-11-17 09:16:34 +00001312
Rui Ueyama6e967342017-02-28 03:29:12 +00001313 // If it is a .dynsym, there should be no local symbols, but we need
1314 // to do a few things for the dynamic linker.
1315 if (this->Type == SHT_DYNSYM) {
1316 // Section's Info field has the index of the first non-local symbol.
1317 // Because the first symbol entry is a null entry, 1 is the first.
Rui Ueyama6e967342017-02-28 03:29:12 +00001318 this->OutSec->Info = 1;
1319
1320 if (In<ELFT>::GnuHashTab) {
1321 // NB: It also sorts Symbols to meet the GNU hash table requirements.
1322 In<ELFT>::GnuHashTab->addSymbols(Symbols);
1323 } else if (Config->EMachine == EM_MIPS) {
1324 std::stable_sort(Symbols.begin(), Symbols.end(), sortMipsSymbols);
1325 }
1326
1327 size_t I = 0;
1328 for (const SymbolTableEntry &S : Symbols)
1329 S.Symbol->DynsymIndex = ++I;
Rui Ueyama406331e2017-02-28 04:11:01 +00001330 return;
Peter Smith55865432017-02-20 11:12:33 +00001331 }
Peter Smith1ec42d92017-03-08 14:06:24 +00001332}
Peter Smith55865432017-02-20 11:12:33 +00001333
Peter Smith1ec42d92017-03-08 14:06:24 +00001334template <class ELFT> void SymbolTableSection<ELFT>::postThunkContents() {
1335 if (this->Type == SHT_DYNSYM)
1336 return;
1337 // move all local symbols before global symbols.
Rui Ueyama6e967342017-02-28 03:29:12 +00001338 auto It = std::stable_partition(
1339 Symbols.begin(), Symbols.end(), [](const SymbolTableEntry &S) {
1340 return S.Symbol->isLocal() ||
1341 S.Symbol->symbol()->computeBinding() == STB_LOCAL;
1342 });
1343 size_t NumLocals = It - Symbols.begin();
Rui Ueyama1f032532017-02-28 01:56:36 +00001344 this->OutSec->Info = NumLocals + 1;
Eugene Leviant9230db92016-11-17 09:16:34 +00001345}
1346
Rui Ueyamab8dcdb52017-02-28 04:20:16 +00001347template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
1348 // Adding a local symbol to a .dynsym is a bug.
1349 assert(this->Type != SHT_DYNSYM || !B->isLocal());
Eugene Leviant9230db92016-11-17 09:16:34 +00001350
Rui Ueyamab8dcdb52017-02-28 04:20:16 +00001351 bool HashIt = B->isLocal();
1352 Symbols.push_back({B, StrTabSec.addString(B->getName(), HashIt)});
George Rimar190bac52017-01-23 14:07:23 +00001353}
1354
1355template <class ELFT>
1356size_t SymbolTableSection<ELFT>::getSymbolIndex(SymbolBody *Body) {
Rafael Espindola08d6a3f2017-02-11 01:40:49 +00001357 auto I = llvm::find_if(Symbols, [&](const SymbolTableEntry &E) {
1358 if (E.Symbol == Body)
1359 return true;
1360 // This is used for -r, so we have to handle multiple section
1361 // symbols being combined.
1362 if (Body->Type == STT_SECTION && E.Symbol->Type == STT_SECTION)
Rafael Espindola5616adf2017-03-08 22:36:28 +00001363 return cast<DefinedRegular>(Body)->Section->getOutputSection() ==
1364 cast<DefinedRegular>(E.Symbol)->Section->getOutputSection();
Rafael Espindola08d6a3f2017-02-11 01:40:49 +00001365 return false;
1366 });
Rafael Espindola0b034d62017-01-26 14:09:18 +00001367 if (I == Symbols.end())
1368 return 0;
George Rimar190bac52017-01-23 14:07:23 +00001369 return I - Symbols.begin() + 1;
1370}
1371
Rui Ueyama1f032532017-02-28 01:56:36 +00001372// Write the internal symbol table contents to the output symbol table.
Eugene Leviant9230db92016-11-17 09:16:34 +00001373template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama1f032532017-02-28 01:56:36 +00001374 // The first entry is a null entry as per the ELF spec.
Eugene Leviant9230db92016-11-17 09:16:34 +00001375 Buf += sizeof(Elf_Sym);
1376
Eugene Leviant9230db92016-11-17 09:16:34 +00001377 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
George Rimar190bac52017-01-23 14:07:23 +00001378
Rui Ueyama1f032532017-02-28 01:56:36 +00001379 for (SymbolTableEntry &Ent : Symbols) {
1380 SymbolBody *Body = Ent.Symbol;
Eugene Leviant9230db92016-11-17 09:16:34 +00001381
Rui Ueyama1b003182017-02-28 19:22:09 +00001382 // Set st_info and st_other.
Rui Ueyama1f032532017-02-28 01:56:36 +00001383 if (Body->isLocal()) {
1384 ESym->setBindingAndType(STB_LOCAL, Body->Type);
1385 } else {
1386 ESym->setBindingAndType(Body->symbol()->computeBinding(), Body->Type);
1387 ESym->setVisibility(Body->symbol()->Visibility);
1388 }
1389
1390 ESym->st_name = Ent.StrTabOffset;
Rui Ueyama3bc39012017-02-27 22:39:50 +00001391 ESym->st_size = Body->getSize<ELFT>();
Eugene Leviant9230db92016-11-17 09:16:34 +00001392
Rui Ueyama1b003182017-02-28 19:22:09 +00001393 // Set a section index.
1394 if (const OutputSection *OutSec = Body->getOutputSection<ELFT>())
Eugene Leviant9230db92016-11-17 09:16:34 +00001395 ESym->st_shndx = OutSec->SectionIndex;
Rui Ueyama80474a22017-02-28 19:29:55 +00001396 else if (isa<DefinedRegular>(Body))
Eugene Leviant9230db92016-11-17 09:16:34 +00001397 ESym->st_shndx = SHN_ABS;
Rui Ueyama1b003182017-02-28 19:22:09 +00001398 else if (isa<DefinedCommon>(Body))
Rui Ueyamab2a23cf2017-01-24 03:41:20 +00001399 ESym->st_shndx = SHN_COMMON;
Rui Ueyama1b003182017-02-28 19:22:09 +00001400
1401 // st_value is usually an address of a symbol, but that has a
1402 // special meaining for uninstantiated common symbols (this can
1403 // occur if -r is given).
1404 if (!Config->DefineCommon && isa<DefinedCommon>(Body))
Rui Ueyamab2a23cf2017-01-24 03:41:20 +00001405 ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
Rui Ueyama1b003182017-02-28 19:22:09 +00001406 else
1407 ESym->st_value = Body->getVA<ELFT>();
1408
Rui Ueyama1f032532017-02-28 01:56:36 +00001409 ++ESym;
1410 }
Eugene Leviant9230db92016-11-17 09:16:34 +00001411
Rui Ueyama1f032532017-02-28 01:56:36 +00001412 // On MIPS we need to mark symbol which has a PLT entry and requires
1413 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1414 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1415 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1416 if (Config->EMachine == EM_MIPS) {
1417 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1418
1419 for (SymbolTableEntry &Ent : Symbols) {
1420 SymbolBody *Body = Ent.Symbol;
Rui Ueyama924b3612017-02-16 06:12:22 +00001421 if (Body->isInPlt() && Body->NeedsPltAddr)
Eugene Leviant9230db92016-11-17 09:16:34 +00001422 ESym->st_other |= STO_MIPS_PLT;
Rui Ueyama1f032532017-02-28 01:56:36 +00001423
1424 if (Config->Relocatable)
Rui Ueyama80474a22017-02-28 19:29:55 +00001425 if (auto *D = dyn_cast<DefinedRegular>(Body))
1426 if (D->isMipsPIC<ELFT>())
Rui Ueyama1f032532017-02-28 01:56:36 +00001427 ESym->st_other |= STO_MIPS_PIC;
1428 ++ESym;
Eugene Leviant9230db92016-11-17 09:16:34 +00001429 }
Eugene Leviant9230db92016-11-17 09:16:34 +00001430 }
1431}
1432
Rui Ueyamae4120632017-02-28 22:05:13 +00001433// .hash and .gnu.hash sections contain on-disk hash tables that map
1434// symbol names to their dynamic symbol table indices. Their purpose
1435// is to help the dynamic linker resolve symbols quickly. If ELF files
1436// don't have them, the dynamic linker has to do linear search on all
1437// dynamic symbols, which makes programs slower. Therefore, a .hash
1438// section is added to a DSO by default. A .gnu.hash is added if you
1439// give the -hash-style=gnu or -hash-style=both option.
1440//
1441// The Unix semantics of resolving dynamic symbols is somewhat expensive.
1442// Each ELF file has a list of DSOs that the ELF file depends on and a
1443// list of dynamic symbols that need to be resolved from any of the
1444// DSOs. That means resolving all dynamic symbols takes O(m)*O(n)
1445// where m is the number of DSOs and n is the number of dynamic
1446// symbols. For modern large programs, both m and n are large. So
1447// making each step faster by using hash tables substiantially
1448// improves time to load programs.
1449//
1450// (Note that this is not the only way to design the shared library.
1451// For instance, the Windows DLL takes a different approach. On
1452// Windows, each dynamic symbol has a name of DLL from which the symbol
1453// has to be resolved. That makes the cost of symbol resolution O(n).
1454// This disables some hacky techniques you can use on Unix such as
1455// LD_PRELOAD, but this is arguably better semantics than the Unix ones.)
1456//
1457// Due to historical reasons, we have two different hash tables, .hash
1458// and .gnu.hash. They are for the same purpose, and .gnu.hash is a new
1459// and better version of .hash. .hash is just an on-disk hash table, but
1460// .gnu.hash has a bloom filter in addition to a hash table to skip
1461// DSOs very quickly. If you are sure that your dynamic linker knows
1462// about .gnu.hash, you want to specify -hash-style=gnu. Otherwise, a
1463// safe bet is to specify -hash-style=both for backward compatibilty.
Eugene Leviant9230db92016-11-17 09:16:34 +00001464template <class ELFT>
Eugene Leviantbe809a72016-11-18 06:44:18 +00001465GnuHashTableSection<ELFT>::GnuHashTableSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +00001466 : SyntheticSection(SHF_ALLOC, SHT_GNU_HASH, sizeof(uintX_t), ".gnu.hash") {
Eugene Leviantbe809a72016-11-18 06:44:18 +00001467 this->Entsize = ELFT::Is64Bits ? 0 : 4;
1468}
1469
Rui Ueyama945055a2017-02-27 03:07:41 +00001470template <class ELFT> void GnuHashTableSection<ELFT>::finalizeContents() {
Rui Ueyamac3726f82017-02-28 04:41:20 +00001471 this->OutSec->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
Rui Ueyamae13373b2017-03-01 02:51:42 +00001472
1473 // Computes bloom filter size in word size. We want to allocate 8
1474 // bits for each symbol. It must be a power of two.
1475 if (Symbols.empty())
1476 MaskWords = 1;
1477 else
1478 MaskWords = NextPowerOf2((Symbols.size() - 1) / sizeof(uintX_t));
1479
1480 Size = 16; // Header
1481 Size += sizeof(uintX_t) * MaskWords; // Bloom filter
1482 Size += NBuckets * 4; // Hash buckets
1483 Size += Symbols.size() * 4; // Hash values
Eugene Leviantbe809a72016-11-18 06:44:18 +00001484}
1485
1486template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyamae13373b2017-03-01 02:51:42 +00001487 // Write a header.
Rui Ueyama6b776ad2017-02-28 05:53:47 +00001488 const endianness E = ELFT::TargetEndianness;
1489 write32<E>(Buf, NBuckets);
1490 write32<E>(Buf + 4, In<ELFT>::DynSymTab->getNumSymbols() - Symbols.size());
1491 write32<E>(Buf + 8, MaskWords);
Rui Ueyamae13373b2017-03-01 02:51:42 +00001492 write32<E>(Buf + 12, getShift2());
1493 Buf += 16;
1494
Rui Ueyama7986b452017-03-01 18:09:09 +00001495 // Write a bloom filter and a hash table.
Rui Ueyamae13373b2017-03-01 02:51:42 +00001496 writeBloomFilter(Buf);
1497 Buf += sizeof(uintX_t) * MaskWords;
Rui Ueyamae13373b2017-03-01 02:51:42 +00001498 writeHashTable(Buf);
Eugene Leviantbe809a72016-11-18 06:44:18 +00001499}
1500
Rui Ueyama7986b452017-03-01 18:09:09 +00001501// This function writes a 2-bit bloom filter. This bloom filter alone
1502// usually filters out 80% or more of all symbol lookups [1].
1503// The dynamic linker uses the hash table only when a symbol is not
1504// filtered out by a bloom filter.
1505//
1506// [1] Ulrich Drepper (2011), "How To Write Shared Libraries" (Ver. 4.1.2),
1507// p.9, https://www.akkadia.org/drepper/dsohowto.pdf
Eugene Leviantbe809a72016-11-18 06:44:18 +00001508template <class ELFT>
Rui Ueyamae13373b2017-03-01 02:51:42 +00001509void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *Buf) {
Rui Ueyama6b776ad2017-02-28 05:53:47 +00001510 typedef typename ELFT::Off Elf_Off;
Rui Ueyama6b776ad2017-02-28 05:53:47 +00001511 const unsigned C = sizeof(uintX_t) * 8;
Eugene Leviantbe809a72016-11-18 06:44:18 +00001512
Rui Ueyamae13373b2017-03-01 02:51:42 +00001513 auto *Filter = reinterpret_cast<Elf_Off *>(Buf);
1514 for (const Entry &Sym : Symbols) {
1515 size_t I = (Sym.Hash / C) & (MaskWords - 1);
1516 Filter[I] |= uintX_t(1) << (Sym.Hash % C);
1517 Filter[I] |= uintX_t(1) << ((Sym.Hash >> getShift2()) % C);
Eugene Leviantbe809a72016-11-18 06:44:18 +00001518 }
Eugene Leviantbe809a72016-11-18 06:44:18 +00001519}
1520
1521template <class ELFT>
1522void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
Rui Ueyama6b776ad2017-02-28 05:53:47 +00001523 // A 32-bit integer type in the target endianness.
1524 typedef typename ELFT::Word Elf_Word;
1525
Rui Ueyamae13373b2017-03-01 02:51:42 +00001526 // Group symbols by hash value.
1527 std::vector<std::vector<Entry>> Syms(NBuckets);
1528 for (const Entry &Ent : Symbols)
1529 Syms[Ent.Hash % NBuckets].push_back(Ent);
Eugene Leviantbe809a72016-11-18 06:44:18 +00001530
Rui Ueyamae13373b2017-03-01 02:51:42 +00001531 // Write hash buckets. Hash buckets contain indices in the following
1532 // hash value table.
1533 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
1534 for (size_t I = 0; I < NBuckets; ++I)
1535 if (!Syms[I].empty())
1536 Buckets[I] = Syms[I][0].Body->DynsymIndex;
1537
1538 // Write a hash value table. It represents a sequence of chains that
1539 // share the same hash modulo value. The last element of each chain
1540 // is terminated by LSB 1.
1541 Elf_Word *Values = Buckets + NBuckets;
1542 size_t I = 0;
1543 for (std::vector<Entry> &Vec : Syms) {
1544 if (Vec.empty())
1545 continue;
1546 for (const Entry &Ent : makeArrayRef(Vec).drop_back())
1547 Values[I++] = Ent.Hash & ~1;
1548 Values[I++] = Vec.back().Hash | 1;
Eugene Leviantbe809a72016-11-18 06:44:18 +00001549 }
Eugene Leviantbe809a72016-11-18 06:44:18 +00001550}
1551
1552static uint32_t hashGnu(StringRef Name) {
1553 uint32_t H = 5381;
1554 for (uint8_t C : Name)
1555 H = (H << 5) + H + C;
1556 return H;
1557}
1558
Rui Ueyamae13373b2017-03-01 02:51:42 +00001559// Returns a number of hash buckets to accomodate given number of elements.
1560// We want to choose a moderate number that is not too small (which
1561// causes too many hash collisions) and not too large (which wastes
1562// disk space.)
1563//
1564// We return a prime number because it (is believed to) achieve good
1565// hash distribution.
1566static size_t getBucketSize(size_t NumSymbols) {
1567 // List of largest prime numbers that are not greater than 2^n + 1.
1568 for (size_t N : {131071, 65521, 32749, 16381, 8191, 4093, 2039, 1021, 509,
1569 251, 127, 61, 31, 13, 7, 3, 1})
1570 if (N <= NumSymbols)
1571 return N;
1572 return 0;
1573}
1574
Eugene Leviantbe809a72016-11-18 06:44:18 +00001575// Add symbols to this symbol hash table. Note that this function
1576// destructively sort a given vector -- which is needed because
1577// GNU-style hash table places some sorting requirements.
1578template <class ELFT>
1579void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) {
Rui Ueyamae13373b2017-03-01 02:51:42 +00001580 // We cannot use 'auto' for Mid because GCC 6.1 cannot deduce
1581 // its type correctly.
Eugene Leviantbe809a72016-11-18 06:44:18 +00001582 std::vector<SymbolTableEntry>::iterator Mid =
1583 std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
1584 return S.Symbol->isUndefined();
1585 });
1586 if (Mid == V.end())
1587 return;
Rui Ueyamae13373b2017-03-01 02:51:42 +00001588
1589 for (SymbolTableEntry &Ent : llvm::make_range(Mid, V.end())) {
1590 SymbolBody *B = Ent.Symbol;
1591 Symbols.push_back({B, Ent.StrTabOffset, hashGnu(B->getName())});
Eugene Leviantbe809a72016-11-18 06:44:18 +00001592 }
1593
Rui Ueyamae13373b2017-03-01 02:51:42 +00001594 NBuckets = getBucketSize(Symbols.size());
Eugene Leviantbe809a72016-11-18 06:44:18 +00001595 std::stable_sort(Symbols.begin(), Symbols.end(),
Rui Ueyamae13373b2017-03-01 02:51:42 +00001596 [&](const Entry &L, const Entry &R) {
Eugene Leviantbe809a72016-11-18 06:44:18 +00001597 return L.Hash % NBuckets < R.Hash % NBuckets;
1598 });
1599
1600 V.erase(Mid, V.end());
Rui Ueyamae13373b2017-03-01 02:51:42 +00001601 for (const Entry &Ent : Symbols)
1602 V.push_back({Ent.Body, Ent.StrTabOffset});
Eugene Leviantbe809a72016-11-18 06:44:18 +00001603}
1604
Eugene Leviantb96e8092016-11-18 09:06:47 +00001605template <class ELFT>
1606HashTableSection<ELFT>::HashTableSection()
Rui Ueyama6b776ad2017-02-28 05:53:47 +00001607 : SyntheticSection(SHF_ALLOC, SHT_HASH, 4, ".hash") {
1608 this->Entsize = 4;
Eugene Leviantb96e8092016-11-18 09:06:47 +00001609}
1610
Rui Ueyama945055a2017-02-27 03:07:41 +00001611template <class ELFT> void HashTableSection<ELFT>::finalizeContents() {
Rui Ueyamac3726f82017-02-28 04:41:20 +00001612 this->OutSec->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
Eugene Leviantb96e8092016-11-18 09:06:47 +00001613
1614 unsigned NumEntries = 2; // nbucket and nchain.
1615 NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
1616
1617 // Create as many buckets as there are symbols.
1618 // FIXME: This is simplistic. We can try to optimize it, but implementing
1619 // support for SHT_GNU_HASH is probably even more profitable.
1620 NumEntries += In<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama6b776ad2017-02-28 05:53:47 +00001621 this->Size = NumEntries * 4;
Eugene Leviantb96e8092016-11-18 09:06:47 +00001622}
1623
1624template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama6b776ad2017-02-28 05:53:47 +00001625 // A 32-bit integer type in the target endianness.
1626 typedef typename ELFT::Word Elf_Word;
1627
Eugene Leviantb96e8092016-11-18 09:06:47 +00001628 unsigned NumSymbols = In<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama6b776ad2017-02-28 05:53:47 +00001629
Eugene Leviantb96e8092016-11-18 09:06:47 +00001630 auto *P = reinterpret_cast<Elf_Word *>(Buf);
1631 *P++ = NumSymbols; // nbucket
1632 *P++ = NumSymbols; // nchain
1633
1634 Elf_Word *Buckets = P;
1635 Elf_Word *Chains = P + NumSymbols;
1636
1637 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
1638 SymbolBody *Body = S.Symbol;
1639 StringRef Name = Body->getName();
1640 unsigned I = Body->DynsymIndex;
1641 uint32_t Hash = hashSysV(Name) % NumSymbols;
1642 Chains[I] = Buckets[Hash];
1643 Buckets[Hash] = I;
1644 }
1645}
1646
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001647template <class ELFT>
Peter Smithf09245a2017-02-09 10:56:15 +00001648PltSection<ELFT>::PltSection(size_t S)
Rui Ueyama9320cb02017-02-27 02:56:02 +00001649 : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16, ".plt"),
Peter Smithf09245a2017-02-09 10:56:15 +00001650 HeaderSize(S) {}
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001651
1652template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Peter Smithf09245a2017-02-09 10:56:15 +00001653 // At beginning of PLT but not the IPLT, we have code to call the dynamic
1654 // linker to resolve dynsyms at runtime. Write such code.
1655 if (HeaderSize != 0)
1656 Target->writePltHeader(Buf);
1657 size_t Off = HeaderSize;
1658 // The IPlt is immediately after the Plt, account for this in RelOff
1659 unsigned PltOff = getPltRelocOff();
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001660
1661 for (auto &I : Entries) {
1662 const SymbolBody *B = I.first;
Peter Smithf09245a2017-02-09 10:56:15 +00001663 unsigned RelOff = I.second + PltOff;
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001664 uint64_t Got = B->getGotPltVA<ELFT>();
1665 uint64_t Plt = this->getVA() + Off;
1666 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
1667 Off += Target->PltEntrySize;
1668 }
1669}
1670
1671template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
1672 Sym.PltIndex = Entries.size();
Peter Smithf09245a2017-02-09 10:56:15 +00001673 RelocationSection<ELFT> *PltRelocSection = In<ELFT>::RelaPlt;
1674 if (HeaderSize == 0) {
1675 PltRelocSection = In<ELFT>::RelaIplt;
1676 Sym.IsInIplt = true;
1677 }
1678 unsigned RelOff = PltRelocSection->getRelocOffset();
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001679 Entries.push_back(std::make_pair(&Sym, RelOff));
1680}
1681
1682template <class ELFT> size_t PltSection<ELFT>::getSize() const {
Peter Smithf09245a2017-02-09 10:56:15 +00001683 return HeaderSize + Entries.size() * Target->PltEntrySize;
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001684}
1685
Peter Smith96943762017-01-25 10:31:16 +00001686// Some architectures such as additional symbols in the PLT section. For
1687// example ARM uses mapping symbols to aid disassembly
1688template <class ELFT> void PltSection<ELFT>::addSymbols() {
Peter Smithf09245a2017-02-09 10:56:15 +00001689 // The PLT may have symbols defined for the Header, the IPLT has no header
1690 if (HeaderSize != 0)
1691 Target->addPltHeaderSymbols(this);
1692 size_t Off = HeaderSize;
Peter Smith96943762017-01-25 10:31:16 +00001693 for (size_t I = 0; I < Entries.size(); ++I) {
1694 Target->addPltSymbols(this, Off);
1695 Off += Target->PltEntrySize;
1696 }
1697}
1698
Peter Smithf09245a2017-02-09 10:56:15 +00001699template <class ELFT> unsigned PltSection<ELFT>::getPltRelocOff() const {
1700 return (HeaderSize == 0) ? In<ELFT>::Plt->getSize() : 0;
Peter Smith96943762017-01-25 10:31:16 +00001701}
1702
Peter Smithbaffdb82016-12-08 12:58:55 +00001703template <class ELFT>
Eugene Levianta113a412016-11-21 09:24:43 +00001704GdbIndexSection<ELFT>::GdbIndexSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +00001705 : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index"),
George Rimarec02b8d2016-12-15 12:07:53 +00001706 StringPool(llvm::StringTableBuilder::ELF) {}
Eugene Levianta113a412016-11-21 09:24:43 +00001707
George Rimarec02b8d2016-12-15 12:07:53 +00001708// Iterative hash function for symbol's name is described in .gdb_index format
1709// specification. Note that we use one for version 5 to 7 here, it is different
1710// for version 4.
1711static uint32_t hash(StringRef Str) {
1712 uint32_t R = 0;
1713 for (uint8_t C : Str)
1714 R = R * 67 + tolower(C) - 113;
1715 return R;
1716}
1717
Rui Ueyamaac2d8152017-03-01 22:54:50 +00001718static std::vector<std::pair<uint64_t, uint64_t>>
1719readCuList(DWARFContext &Dwarf, InputSection *Sec) {
1720 std::vector<std::pair<uint64_t, uint64_t>> Ret;
1721 for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf.compile_units())
1722 Ret.push_back({Sec->OutSecOff + CU->getOffset(), CU->getLength() + 4});
1723 return Ret;
1724}
1725
1726template <class ELFT>
1727static InputSectionBase *findSection(ArrayRef<InputSectionBase *> Arr,
1728 uint64_t Offset) {
1729 for (InputSectionBase *S : Arr)
1730 if (S && S != &InputSection::Discarded)
Rafael Espindola35ae65e2017-03-08 15:57:17 +00001731 if (Offset >= S->getOffsetInFile() &&
1732 Offset < S->getOffsetInFile() + S->getSize())
Rui Ueyamaac2d8152017-03-01 22:54:50 +00001733 return S;
1734 return nullptr;
1735}
1736
1737template <class ELFT>
1738static std::vector<AddressEntry>
1739readAddressArea(DWARFContext &Dwarf, InputSection *Sec, size_t CurrentCU) {
1740 std::vector<AddressEntry> Ret;
1741
1742 for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf.compile_units()) {
1743 DWARFAddressRangesVector Ranges;
1744 CU->collectAddressRanges(Ranges);
1745
1746 ArrayRef<InputSectionBase *> Sections =
1747 Sec->template getFile<ELFT>()->getSections();
1748
1749 for (std::pair<uint64_t, uint64_t> &R : Ranges)
1750 if (InputSectionBase *S = findSection<ELFT>(Sections, R.first))
Rafael Espindola35ae65e2017-03-08 15:57:17 +00001751 Ret.push_back({S, R.first - S->getOffsetInFile(),
1752 R.second - S->getOffsetInFile(), CurrentCU});
Rui Ueyamaac2d8152017-03-01 22:54:50 +00001753 ++CurrentCU;
1754 }
1755 return Ret;
1756}
1757
1758static std::vector<std::pair<StringRef, uint8_t>>
1759readPubNamesAndTypes(DWARFContext &Dwarf, bool IsLE) {
1760 StringRef Data[] = {Dwarf.getGnuPubNamesSection(),
1761 Dwarf.getGnuPubTypesSection()};
1762
1763 std::vector<std::pair<StringRef, uint8_t>> Ret;
1764 for (StringRef D : Data) {
1765 DWARFDebugPubTable PubTable(D, IsLE, true);
1766 for (const DWARFDebugPubTable::Set &Set : PubTable.getData())
1767 for (const DWARFDebugPubTable::Entry &Ent : Set.Entries)
1768 Ret.push_back({Ent.Name, Ent.Descriptor.toBits()});
1769 }
1770 return Ret;
1771}
1772
1773class ObjInfoTy : public llvm::LoadedObjectInfo {
1774 uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override {
1775 auto &S = static_cast<const object::ELFSectionRef &>(Sec);
1776 if (S.getFlags() & ELF::SHF_ALLOC)
1777 return S.getOffset();
1778 return 0;
1779 }
1780
1781 std::unique_ptr<llvm::LoadedObjectInfo> clone() const override { return {}; }
1782};
1783
Rui Ueyama475b1dd2017-03-01 22:02:17 +00001784template <class ELFT> void GdbIndexSection<ELFT>::readDwarf(InputSection *Sec) {
Rui Ueyamaac2d8152017-03-01 22:54:50 +00001785 elf::ObjectFile<ELFT> *File = Sec->template getFile<ELFT>();
1786
1787 Expected<std::unique_ptr<object::ObjectFile>> Obj =
1788 object::ObjectFile::createObjectFile(File->MB);
1789 if (!Obj) {
1790 error(toString(File) + ": error creating DWARF context");
George Rimar8b547392016-12-15 09:08:13 +00001791 return;
Rui Ueyamaac2d8152017-03-01 22:54:50 +00001792 }
1793
1794 ObjInfoTy ObjInfo;
1795 DWARFContextInMemory Dwarf(*Obj.get(), &ObjInfo);
George Rimar8b547392016-12-15 09:08:13 +00001796
1797 size_t CuId = CompilationUnits.size();
Rui Ueyamaac2d8152017-03-01 22:54:50 +00001798 for (std::pair<uint64_t, uint64_t> &P : readCuList(Dwarf, Sec))
1799 CompilationUnits.push_back(P);
George Rimar8b547392016-12-15 09:08:13 +00001800
Rui Ueyamaac2d8152017-03-01 22:54:50 +00001801 for (AddressEntry &Ent : readAddressArea<ELFT>(Dwarf, Sec, CuId))
1802 AddressArea.push_back(Ent);
George Rimarec02b8d2016-12-15 12:07:53 +00001803
1804 std::vector<std::pair<StringRef, uint8_t>> NamesAndTypes =
Rui Ueyamaac2d8152017-03-01 22:54:50 +00001805 readPubNamesAndTypes(Dwarf, ELFT::TargetEndianness == support::little);
George Rimarec02b8d2016-12-15 12:07:53 +00001806
1807 for (std::pair<StringRef, uint8_t> &Pair : NamesAndTypes) {
1808 uint32_t Hash = hash(Pair.first);
1809 size_t Offset = StringPool.add(Pair.first);
1810
1811 bool IsNew;
1812 GdbSymbol *Sym;
1813 std::tie(IsNew, Sym) = SymbolTable.add(Hash, Offset);
1814 if (IsNew) {
1815 Sym->CuVectorIndex = CuVectors.size();
1816 CuVectors.push_back({{CuId, Pair.second}});
1817 continue;
1818 }
1819
Rui Ueyamaaab18c02017-03-01 22:24:46 +00001820 CuVectors[Sym->CuVectorIndex].push_back({CuId, Pair.second});
George Rimarec02b8d2016-12-15 12:07:53 +00001821 }
Eugene Levianta113a412016-11-21 09:24:43 +00001822}
1823
Rui Ueyama945055a2017-02-27 03:07:41 +00001824template <class ELFT> void GdbIndexSection<ELFT>::finalizeContents() {
George Rimar8b547392016-12-15 09:08:13 +00001825 if (Finalized)
1826 return;
1827 Finalized = true;
1828
Rui Ueyama475b1dd2017-03-01 22:02:17 +00001829 for (InputSectionBase *S : InputSections)
1830 if (InputSection *IS = dyn_cast<InputSection>(S))
1831 if (IS->OutSec && IS->Name == ".debug_info")
1832 readDwarf(IS);
1833
Rui Ueyamad0e07b92017-03-01 21:08:21 +00001834 SymbolTable.finalizeContents();
Eugene Levianta113a412016-11-21 09:24:43 +00001835
1836 // GdbIndex header consist from version fields
1837 // and 5 more fields with different kinds of offsets.
1838 CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize;
George Rimar8b547392016-12-15 09:08:13 +00001839 SymTabOffset = CuTypesOffset + AddressArea.size() * AddressEntrySize;
George Rimarec02b8d2016-12-15 12:07:53 +00001840
1841 ConstantPoolOffset =
1842 SymTabOffset + SymbolTable.getCapacity() * SymTabEntrySize;
1843
1844 for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) {
1845 CuVectorsOffset.push_back(CuVectorsSize);
1846 CuVectorsSize += OffsetTypeSize * (CuVec.size() + 1);
1847 }
1848 StringPoolOffset = ConstantPoolOffset + CuVectorsSize;
1849
1850 StringPool.finalizeInOrder();
George Rimar8b547392016-12-15 09:08:13 +00001851}
1852
1853template <class ELFT> size_t GdbIndexSection<ELFT>::getSize() const {
Rui Ueyama945055a2017-02-27 03:07:41 +00001854 const_cast<GdbIndexSection<ELFT> *>(this)->finalizeContents();
George Rimarec02b8d2016-12-15 12:07:53 +00001855 return StringPoolOffset + StringPool.getSize();
Eugene Levianta113a412016-11-21 09:24:43 +00001856}
1857
1858template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) {
George Rimarec02b8d2016-12-15 12:07:53 +00001859 write32le(Buf, 7); // Write version.
1860 write32le(Buf + 4, CuListOffset); // CU list offset.
1861 write32le(Buf + 8, CuTypesOffset); // Types CU list offset.
1862 write32le(Buf + 12, CuTypesOffset); // Address area offset.
1863 write32le(Buf + 16, SymTabOffset); // Symbol table offset.
1864 write32le(Buf + 20, ConstantPoolOffset); // Constant pool offset.
Eugene Levianta113a412016-11-21 09:24:43 +00001865 Buf += 24;
1866
1867 // Write the CU list.
1868 for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) {
1869 write64le(Buf, CU.first);
1870 write64le(Buf + 8, CU.second);
1871 Buf += 16;
1872 }
George Rimar8b547392016-12-15 09:08:13 +00001873
1874 // Write the address area.
Rui Ueyamaac2d8152017-03-01 22:54:50 +00001875 for (AddressEntry &E : AddressArea) {
Rafael Espindolae1294092017-03-08 16:03:41 +00001876 uintX_t BaseAddr = E.Section->OutSec->Addr + E.Section->getOffset(0);
George Rimar8b547392016-12-15 09:08:13 +00001877 write64le(Buf, BaseAddr + E.LowAddress);
1878 write64le(Buf + 8, BaseAddr + E.HighAddress);
1879 write32le(Buf + 16, E.CuIndex);
1880 Buf += 20;
1881 }
George Rimarec02b8d2016-12-15 12:07:53 +00001882
1883 // Write the symbol table.
1884 for (size_t I = 0; I < SymbolTable.getCapacity(); ++I) {
1885 GdbSymbol *Sym = SymbolTable.getSymbol(I);
1886 if (Sym) {
1887 size_t NameOffset =
1888 Sym->NameOffset + StringPoolOffset - ConstantPoolOffset;
1889 size_t CuVectorOffset = CuVectorsOffset[Sym->CuVectorIndex];
1890 write32le(Buf, NameOffset);
1891 write32le(Buf + 4, CuVectorOffset);
1892 }
1893 Buf += 8;
1894 }
1895
1896 // Write the CU vectors into the constant pool.
1897 for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) {
1898 write32le(Buf, CuVec.size());
1899 Buf += 4;
1900 for (std::pair<uint32_t, uint8_t> &P : CuVec) {
1901 uint32_t Index = P.first;
1902 uint8_t Flags = P.second;
1903 Index |= Flags << 24;
1904 write32le(Buf, Index);
1905 Buf += 4;
1906 }
1907 }
1908
1909 StringPool.write(Buf);
Eugene Levianta113a412016-11-21 09:24:43 +00001910}
1911
George Rimar3fb5a6d2016-11-29 16:05:27 +00001912template <class ELFT> bool GdbIndexSection<ELFT>::empty() const {
Rui Ueyama9d1bacb12017-02-27 02:31:26 +00001913 return !Out::DebugInfo;
George Rimar3fb5a6d2016-11-29 16:05:27 +00001914}
1915
Eugene Leviant952eb4d2016-11-21 15:52:10 +00001916template <class ELFT>
1917EhFrameHeader<ELFT>::EhFrameHeader()
Rui Ueyama9320cb02017-02-27 02:56:02 +00001918 : SyntheticSection(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
Eugene Leviant952eb4d2016-11-21 15:52:10 +00001919
1920// .eh_frame_hdr contains a binary search table of pointers to FDEs.
1921// Each entry of the search table consists of two values,
1922// the starting PC from where FDEs covers, and the FDE's address.
1923// It is sorted by PC.
1924template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
1925 const endianness E = ELFT::TargetEndianness;
1926
1927 // Sort the FDE list by their PC and uniqueify. Usually there is only
1928 // one FDE for a PC (i.e. function), but if ICF merges two functions
1929 // into one, there can be more than one FDEs pointing to the address.
1930 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
1931 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
1932 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
1933 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
1934
1935 Buf[0] = 1;
1936 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1937 Buf[2] = DW_EH_PE_udata4;
1938 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
Rafael Espindola66b4e212017-02-23 22:06:28 +00001939 write32<E>(Buf + 4, In<ELFT>::EhFrame->OutSec->Addr - this->getVA() - 4);
Eugene Leviant952eb4d2016-11-21 15:52:10 +00001940 write32<E>(Buf + 8, Fdes.size());
1941 Buf += 12;
1942
1943 uintX_t VA = this->getVA();
1944 for (FdeData &Fde : Fdes) {
1945 write32<E>(Buf, Fde.Pc - VA);
1946 write32<E>(Buf + 4, Fde.FdeVA - VA);
1947 Buf += 8;
1948 }
1949}
1950
1951template <class ELFT> size_t EhFrameHeader<ELFT>::getSize() const {
1952 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
Rafael Espindola66b4e212017-02-23 22:06:28 +00001953 return 12 + In<ELFT>::EhFrame->NumFdes * 8;
Eugene Leviant952eb4d2016-11-21 15:52:10 +00001954}
1955
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001956template <class ELFT>
Rui Ueyamab38ddb12016-11-21 19:46:04 +00001957void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
1958 Fdes.push_back({Pc, FdeVA});
1959}
1960
George Rimar11992c862016-11-25 08:05:41 +00001961template <class ELFT> bool EhFrameHeader<ELFT>::empty() const {
Rafael Espindola66b4e212017-02-23 22:06:28 +00001962 return In<ELFT>::EhFrame->empty();
George Rimar11992c862016-11-25 08:05:41 +00001963}
1964
Rui Ueyamab38ddb12016-11-21 19:46:04 +00001965template <class ELFT>
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001966VersionDefinitionSection<ELFT>::VersionDefinitionSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +00001967 : SyntheticSection(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t),
1968 ".gnu.version_d") {}
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001969
1970static StringRef getFileDefName() {
1971 if (!Config->SoName.empty())
1972 return Config->SoName;
1973 return Config->OutputFile;
1974}
1975
Rui Ueyama945055a2017-02-27 03:07:41 +00001976template <class ELFT> void VersionDefinitionSection<ELFT>::finalizeContents() {
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001977 FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName());
1978 for (VersionDefinition &V : Config->VersionDefinitions)
1979 V.NameOff = In<ELFT>::DynStrTab->addString(V.Name);
1980
Rui Ueyamac3726f82017-02-28 04:41:20 +00001981 this->OutSec->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001982
1983 // sh_info should be set to the number of definitions. This fact is missed in
1984 // documentation, but confirmed by binutils community:
1985 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
Rui Ueyamac3726f82017-02-28 04:41:20 +00001986 this->OutSec->Info = getVerDefNum();
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001987}
1988
1989template <class ELFT>
1990void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1991 StringRef Name, size_t NameOff) {
1992 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
1993 Verdef->vd_version = 1;
1994 Verdef->vd_cnt = 1;
1995 Verdef->vd_aux = sizeof(Elf_Verdef);
1996 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1997 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
1998 Verdef->vd_ndx = Index;
1999 Verdef->vd_hash = hashSysV(Name);
2000
2001 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
2002 Verdaux->vda_name = NameOff;
2003 Verdaux->vda_next = 0;
2004}
2005
2006template <class ELFT>
2007void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
2008 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
2009
2010 for (VersionDefinition &V : Config->VersionDefinitions) {
2011 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
2012 writeOne(Buf, V.Id, V.Name, V.NameOff);
2013 }
2014
2015 // Need to terminate the last version definition.
2016 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
2017 Verdef->vd_next = 0;
2018}
2019
2020template <class ELFT> size_t VersionDefinitionSection<ELFT>::getSize() const {
2021 return (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
2022}
2023
2024template <class ELFT>
2025VersionTableSection<ELFT>::VersionTableSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +00002026 : SyntheticSection(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t),
Rui Ueyama27876642017-03-01 04:04:23 +00002027 ".gnu.version") {
2028 this->Entsize = sizeof(Elf_Versym);
2029}
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002030
Rui Ueyama945055a2017-02-27 03:07:41 +00002031template <class ELFT> void VersionTableSection<ELFT>::finalizeContents() {
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002032 // At the moment of june 2016 GNU docs does not mention that sh_link field
2033 // should be set, but Sun docs do. Also readelf relies on this field.
Rui Ueyamac3726f82017-02-28 04:41:20 +00002034 this->OutSec->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002035}
2036
2037template <class ELFT> size_t VersionTableSection<ELFT>::getSize() const {
2038 return sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1);
2039}
2040
2041template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
2042 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
2043 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
2044 OutVersym->vs_index = S.Symbol->symbol()->VersionId;
2045 ++OutVersym;
2046 }
2047}
2048
George Rimar11992c862016-11-25 08:05:41 +00002049template <class ELFT> bool VersionTableSection<ELFT>::empty() const {
2050 return !In<ELFT>::VerDef && In<ELFT>::VerNeed->empty();
2051}
2052
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002053template <class ELFT>
2054VersionNeedSection<ELFT>::VersionNeedSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +00002055 : SyntheticSection(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t),
2056 ".gnu.version_r") {
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002057 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
2058 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
2059 // First identifiers are reserved by verdef section if it exist.
2060 NextIndex = getVerDefNum() + 1;
2061}
2062
2063template <class ELFT>
Rui Ueyama4076fa12017-02-26 23:35:34 +00002064void VersionNeedSection<ELFT>::addSymbol(SharedSymbol *SS) {
2065 auto *Ver = reinterpret_cast<const typename ELFT::Verdef *>(SS->Verdef);
2066 if (!Ver) {
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002067 SS->symbol()->VersionId = VER_NDX_GLOBAL;
2068 return;
2069 }
Rui Ueyama4076fa12017-02-26 23:35:34 +00002070
2071 auto *File = cast<SharedFile<ELFT>>(SS->File);
2072
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002073 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
2074 // to create one by adding it to our needed list and creating a dynstr entry
2075 // for the soname.
Rui Ueyama4076fa12017-02-26 23:35:34 +00002076 if (File->VerdefMap.empty())
2077 Needed.push_back({File, In<ELFT>::DynStrTab->addString(File->getSoName())});
2078 typename SharedFile<ELFT>::NeededVer &NV = File->VerdefMap[Ver];
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002079 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
2080 // prepare to create one by allocating a version identifier and creating a
2081 // dynstr entry for the version name.
2082 if (NV.Index == 0) {
Rui Ueyama4076fa12017-02-26 23:35:34 +00002083 NV.StrTab = In<ELFT>::DynStrTab->addString(File->getStringTable().data() +
2084 Ver->getAux()->vda_name);
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002085 NV.Index = NextIndex++;
2086 }
2087 SS->symbol()->VersionId = NV.Index;
2088}
2089
2090template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
2091 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
2092 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
2093 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
2094
2095 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
2096 // Create an Elf_Verneed for this DSO.
2097 Verneed->vn_version = 1;
2098 Verneed->vn_cnt = P.first->VerdefMap.size();
2099 Verneed->vn_file = P.second;
2100 Verneed->vn_aux =
2101 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
2102 Verneed->vn_next = sizeof(Elf_Verneed);
2103 ++Verneed;
2104
2105 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
2106 // VerdefMap, which will only contain references to needed version
2107 // definitions. Each Elf_Vernaux is based on the information contained in
2108 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
2109 // pointers, but is deterministic because the pointers refer to Elf_Verdef
2110 // data structures within a single input file.
2111 for (auto &NV : P.first->VerdefMap) {
2112 Vernaux->vna_hash = NV.first->vd_hash;
2113 Vernaux->vna_flags = 0;
2114 Vernaux->vna_other = NV.second.Index;
2115 Vernaux->vna_name = NV.second.StrTab;
2116 Vernaux->vna_next = sizeof(Elf_Vernaux);
2117 ++Vernaux;
2118 }
2119
2120 Vernaux[-1].vna_next = 0;
2121 }
2122 Verneed[-1].vn_next = 0;
2123}
2124
Rui Ueyama945055a2017-02-27 03:07:41 +00002125template <class ELFT> void VersionNeedSection<ELFT>::finalizeContents() {
Rui Ueyamac3726f82017-02-28 04:41:20 +00002126 this->OutSec->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
2127 this->OutSec->Info = Needed.size();
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002128}
2129
2130template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
2131 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
2132 for (const std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
2133 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
2134 return Size;
2135}
2136
George Rimar11992c862016-11-25 08:05:41 +00002137template <class ELFT> bool VersionNeedSection<ELFT>::empty() const {
2138 return getNeedNum() == 0;
2139}
2140
Rafael Espindola6119b862017-03-06 20:23:56 +00002141MergeSyntheticSection::MergeSyntheticSection(StringRef Name, uint32_t Type,
Rafael Espindolafcd208f2017-03-08 19:35:29 +00002142 uint64_t Flags, uint32_t Alignment)
Rui Ueyama9320cb02017-02-27 02:56:02 +00002143 : SyntheticSection(Flags, Type, Alignment, Name),
Rafael Espindola25324312017-02-03 21:29:51 +00002144 Builder(StringTableBuilder::RAW, Alignment) {}
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002145
Rafael Espindola6119b862017-03-06 20:23:56 +00002146void MergeSyntheticSection::addSection(MergeInputSection *MS) {
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002147 assert(!Finalized);
2148 MS->MergeSec = this;
2149 Sections.push_back(MS);
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002150}
2151
Rafael Espindola6119b862017-03-06 20:23:56 +00002152void MergeSyntheticSection::writeTo(uint8_t *Buf) { Builder.write(Buf); }
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002153
Rafael Espindola6119b862017-03-06 20:23:56 +00002154bool MergeSyntheticSection::shouldTailMerge() const {
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002155 return (this->Flags & SHF_STRINGS) && Config->Optimize >= 2;
2156}
2157
Rafael Espindola6119b862017-03-06 20:23:56 +00002158void MergeSyntheticSection::finalizeTailMerge() {
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002159 // Add all string pieces to the string table builder to create section
2160 // contents.
Rafael Espindola6119b862017-03-06 20:23:56 +00002161 for (MergeInputSection *Sec : Sections)
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002162 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
2163 if (Sec->Pieces[I].Live)
2164 Builder.add(Sec->getData(I));
2165
2166 // Fix the string table content. After this, the contents will never change.
2167 Builder.finalize();
2168
2169 // finalize() fixed tail-optimized strings, so we can now get
2170 // offsets of strings. Get an offset for each string and save it
2171 // to a corresponding StringPiece for easy access.
Rafael Espindola6119b862017-03-06 20:23:56 +00002172 for (MergeInputSection *Sec : Sections)
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002173 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
2174 if (Sec->Pieces[I].Live)
2175 Sec->Pieces[I].OutputOff = Builder.getOffset(Sec->getData(I));
2176}
2177
Rafael Espindola6119b862017-03-06 20:23:56 +00002178void MergeSyntheticSection::finalizeNoTailMerge() {
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002179 // Add all string pieces to the string table builder to create section
2180 // contents. Because we are not tail-optimizing, offsets of strings are
2181 // fixed when they are added to the builder (string table builder contains
2182 // a hash table from strings to offsets).
Rafael Espindola6119b862017-03-06 20:23:56 +00002183 for (MergeInputSection *Sec : Sections)
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002184 for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I)
2185 if (Sec->Pieces[I].Live)
2186 Sec->Pieces[I].OutputOff = Builder.add(Sec->getData(I));
2187
2188 Builder.finalizeInOrder();
2189}
2190
Rafael Espindola6119b862017-03-06 20:23:56 +00002191void MergeSyntheticSection::finalizeContents() {
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002192 if (Finalized)
2193 return;
2194 Finalized = true;
2195 if (shouldTailMerge())
2196 finalizeTailMerge();
2197 else
2198 finalizeNoTailMerge();
2199}
2200
Rafael Espindola6119b862017-03-06 20:23:56 +00002201size_t MergeSyntheticSection::getSize() const {
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002202 // We should finalize string builder to know the size.
Rafael Espindola6119b862017-03-06 20:23:56 +00002203 const_cast<MergeSyntheticSection *>(this)->finalizeContents();
Rafael Espindola9e9754b2017-02-03 13:06:18 +00002204 return Builder.getSize();
2205}
2206
2207template <class ELFT>
Rui Ueyamabdfa1552016-11-22 19:24:52 +00002208MipsRldMapSection<ELFT>::MipsRldMapSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +00002209 : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
2210 sizeof(typename ELFT::uint), ".rld_map") {}
Eugene Leviant17b7a572016-11-22 17:49:14 +00002211
Rui Ueyamabdfa1552016-11-22 19:24:52 +00002212template <class ELFT> void MipsRldMapSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviant17b7a572016-11-22 17:49:14 +00002213 // Apply filler from linker script.
2214 uint64_t Filler = Script<ELFT>::X->getFiller(this->Name);
2215 Filler = (Filler << 32) | Filler;
2216 memcpy(Buf, &Filler, getSize());
2217}
2218
Peter Smith719eb8e2016-11-24 11:43:55 +00002219template <class ELFT>
2220ARMExidxSentinelSection<ELFT>::ARMExidxSentinelSection()
Rui Ueyama9320cb02017-02-27 02:56:02 +00002221 : SyntheticSection(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX,
2222 sizeof(typename ELFT::uint), ".ARM.exidx") {}
Peter Smith719eb8e2016-11-24 11:43:55 +00002223
2224// Write a terminating sentinel entry to the end of the .ARM.exidx table.
2225// This section will have been sorted last in the .ARM.exidx table.
2226// This table entry will have the form:
2227// | PREL31 upper bound of code that has exception tables | EXIDX_CANTUNWIND |
George Rimar879a6572016-12-15 15:38:58 +00002228template <class ELFT>
2229void ARMExidxSentinelSection<ELFT>::writeTo(uint8_t *Buf) {
Peter Smith719eb8e2016-11-24 11:43:55 +00002230 // Get the InputSection before us, we are by definition last
Rafael Espindola24e6f362017-02-24 15:07:30 +00002231 auto RI = cast<OutputSection>(this->OutSec)->Sections.rbegin();
Rafael Espindola774ea7d2017-02-23 16:49:07 +00002232 InputSection *LE = *(++RI);
2233 InputSection *LC = cast<InputSection>(LE->template getLinkOrderDep<ELFT>());
Rafael Espindolae1294092017-03-08 16:03:41 +00002234 uint64_t S = LC->OutSec->Addr + LC->getOffset(LC->getSize());
Peter Smith719eb8e2016-11-24 11:43:55 +00002235 uint64_t P = this->getVA();
2236 Target->relocateOne(Buf, R_ARM_PREL31, S - P);
2237 write32le(Buf + 4, 0x1);
2238}
2239
Peter Smith3a52eb02017-02-01 10:26:03 +00002240template <class ELFT>
Rafael Espindola24e6f362017-02-24 15:07:30 +00002241ThunkSection<ELFT>::ThunkSection(OutputSection *OS, uint64_t Off)
Rui Ueyama9320cb02017-02-27 02:56:02 +00002242 : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS,
2243 sizeof(typename ELFT::uint), ".text.thunk") {
Peter Smith3a52eb02017-02-01 10:26:03 +00002244 this->OutSec = OS;
2245 this->OutSecOff = Off;
2246}
2247
2248template <class ELFT> void ThunkSection<ELFT>::addThunk(Thunk<ELFT> *T) {
2249 uint64_t Off = alignTo(Size, T->alignment);
2250 T->Offset = Off;
2251 Thunks.push_back(T);
2252 T->addSymbols(*this);
2253 Size = Off + T->size();
2254}
2255
2256template <class ELFT> void ThunkSection<ELFT>::writeTo(uint8_t *Buf) {
2257 for (const Thunk<ELFT> *T : Thunks)
2258 T->writeTo(Buf + T->Offset, *this);
2259}
2260
2261template <class ELFT>
Rafael Espindola774ea7d2017-02-23 16:49:07 +00002262InputSection *ThunkSection<ELFT>::getTargetInputSection() const {
Peter Smith3a52eb02017-02-01 10:26:03 +00002263 const Thunk<ELFT> *T = Thunks.front();
2264 return T->getTargetInputSection();
2265}
2266
Rafael Espindola774ea7d2017-02-23 16:49:07 +00002267template InputSection *elf::createCommonSection<ELF32LE>();
2268template InputSection *elf::createCommonSection<ELF32BE>();
2269template InputSection *elf::createCommonSection<ELF64LE>();
2270template InputSection *elf::createCommonSection<ELF64BE>();
Rui Ueyamae8a61022016-11-05 23:05:47 +00002271
Rafael Espindola6119b862017-03-06 20:23:56 +00002272template MergeInputSection *elf::createCommentSection<ELF32LE>();
2273template MergeInputSection *elf::createCommentSection<ELF32BE>();
2274template MergeInputSection *elf::createCommentSection<ELF64LE>();
2275template MergeInputSection *elf::createCommentSection<ELF64BE>();
Rui Ueyama3da3f062016-11-10 20:20:37 +00002276
Rafael Espindolab4c9b812017-02-23 02:28:28 +00002277template SymbolBody *elf::addSyntheticLocal<ELF32LE>(StringRef, uint8_t,
Rui Ueyama65316d72017-02-23 03:15:57 +00002278 uint64_t, uint64_t,
Rafael Espindolab4c9b812017-02-23 02:28:28 +00002279 InputSectionBase *);
2280template SymbolBody *elf::addSyntheticLocal<ELF32BE>(StringRef, uint8_t,
Rui Ueyama65316d72017-02-23 03:15:57 +00002281 uint64_t, uint64_t,
Rafael Espindolab4c9b812017-02-23 02:28:28 +00002282 InputSectionBase *);
2283template SymbolBody *elf::addSyntheticLocal<ELF64LE>(StringRef, uint8_t,
Rui Ueyama65316d72017-02-23 03:15:57 +00002284 uint64_t, uint64_t,
Rafael Espindolab4c9b812017-02-23 02:28:28 +00002285 InputSectionBase *);
2286template SymbolBody *elf::addSyntheticLocal<ELF64BE>(StringRef, uint8_t,
Rui Ueyama65316d72017-02-23 03:15:57 +00002287 uint64_t, uint64_t,
Rafael Espindolab4c9b812017-02-23 02:28:28 +00002288 InputSectionBase *);
Peter Smith96943762017-01-25 10:31:16 +00002289
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +00002290template class elf::MipsAbiFlagsSection<ELF32LE>;
2291template class elf::MipsAbiFlagsSection<ELF32BE>;
2292template class elf::MipsAbiFlagsSection<ELF64LE>;
2293template class elf::MipsAbiFlagsSection<ELF64BE>;
2294
Simon Atanasyance02cf02016-11-09 21:36:56 +00002295template class elf::MipsOptionsSection<ELF32LE>;
2296template class elf::MipsOptionsSection<ELF32BE>;
2297template class elf::MipsOptionsSection<ELF64LE>;
2298template class elf::MipsOptionsSection<ELF64BE>;
2299
2300template class elf::MipsReginfoSection<ELF32LE>;
2301template class elf::MipsReginfoSection<ELF32BE>;
2302template class elf::MipsReginfoSection<ELF64LE>;
2303template class elf::MipsReginfoSection<ELF64BE>;
2304
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +00002305template class elf::BuildIdSection<ELF32LE>;
2306template class elf::BuildIdSection<ELF32BE>;
2307template class elf::BuildIdSection<ELF64LE>;
2308template class elf::BuildIdSection<ELF64BE>;
2309
Rui Ueyama007c0022017-03-08 17:24:24 +00002310template class elf::CopyRelSection<ELF32LE>;
2311template class elf::CopyRelSection<ELF32BE>;
2312template class elf::CopyRelSection<ELF64LE>;
2313template class elf::CopyRelSection<ELF64BE>;
Peter Smithebfe9942017-02-09 10:27:57 +00002314
Eugene Leviantad4439e2016-11-11 11:33:32 +00002315template class elf::GotSection<ELF32LE>;
2316template class elf::GotSection<ELF32BE>;
2317template class elf::GotSection<ELF64LE>;
2318template class elf::GotSection<ELF64BE>;
2319
Simon Atanasyan725dc142016-11-16 21:01:02 +00002320template class elf::MipsGotSection<ELF32LE>;
2321template class elf::MipsGotSection<ELF32BE>;
2322template class elf::MipsGotSection<ELF64LE>;
2323template class elf::MipsGotSection<ELF64BE>;
2324
Eugene Leviant41ca3272016-11-10 09:48:29 +00002325template class elf::GotPltSection<ELF32LE>;
2326template class elf::GotPltSection<ELF32BE>;
2327template class elf::GotPltSection<ELF64LE>;
2328template class elf::GotPltSection<ELF64BE>;
Eugene Leviant22eb0262016-11-14 09:16:00 +00002329
Peter Smithbaffdb82016-12-08 12:58:55 +00002330template class elf::IgotPltSection<ELF32LE>;
2331template class elf::IgotPltSection<ELF32BE>;
2332template class elf::IgotPltSection<ELF64LE>;
2333template class elf::IgotPltSection<ELF64BE>;
2334
Eugene Leviant22eb0262016-11-14 09:16:00 +00002335template class elf::StringTableSection<ELF32LE>;
2336template class elf::StringTableSection<ELF32BE>;
2337template class elf::StringTableSection<ELF64LE>;
2338template class elf::StringTableSection<ELF64BE>;
Eugene Leviant6380ce22016-11-15 12:26:55 +00002339
2340template class elf::DynamicSection<ELF32LE>;
2341template class elf::DynamicSection<ELF32BE>;
2342template class elf::DynamicSection<ELF64LE>;
2343template class elf::DynamicSection<ELF64BE>;
Eugene Levianta96d9022016-11-16 10:02:27 +00002344
2345template class elf::RelocationSection<ELF32LE>;
2346template class elf::RelocationSection<ELF32BE>;
2347template class elf::RelocationSection<ELF64LE>;
2348template class elf::RelocationSection<ELF64BE>;
Eugene Leviant9230db92016-11-17 09:16:34 +00002349
2350template class elf::SymbolTableSection<ELF32LE>;
2351template class elf::SymbolTableSection<ELF32BE>;
2352template class elf::SymbolTableSection<ELF64LE>;
2353template class elf::SymbolTableSection<ELF64BE>;
Eugene Leviantbe809a72016-11-18 06:44:18 +00002354
2355template class elf::GnuHashTableSection<ELF32LE>;
2356template class elf::GnuHashTableSection<ELF32BE>;
2357template class elf::GnuHashTableSection<ELF64LE>;
2358template class elf::GnuHashTableSection<ELF64BE>;
Eugene Leviantb96e8092016-11-18 09:06:47 +00002359
2360template class elf::HashTableSection<ELF32LE>;
2361template class elf::HashTableSection<ELF32BE>;
2362template class elf::HashTableSection<ELF64LE>;
2363template class elf::HashTableSection<ELF64BE>;
Eugene Leviantff23d3e2016-11-18 14:35:03 +00002364
2365template class elf::PltSection<ELF32LE>;
2366template class elf::PltSection<ELF32BE>;
2367template class elf::PltSection<ELF64LE>;
2368template class elf::PltSection<ELF64BE>;
Eugene Levianta113a412016-11-21 09:24:43 +00002369
2370template class elf::GdbIndexSection<ELF32LE>;
2371template class elf::GdbIndexSection<ELF32BE>;
2372template class elf::GdbIndexSection<ELF64LE>;
2373template class elf::GdbIndexSection<ELF64BE>;
Eugene Leviant952eb4d2016-11-21 15:52:10 +00002374
2375template class elf::EhFrameHeader<ELF32LE>;
2376template class elf::EhFrameHeader<ELF32BE>;
2377template class elf::EhFrameHeader<ELF64LE>;
2378template class elf::EhFrameHeader<ELF64BE>;
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002379
2380template class elf::VersionTableSection<ELF32LE>;
2381template class elf::VersionTableSection<ELF32BE>;
2382template class elf::VersionTableSection<ELF64LE>;
2383template class elf::VersionTableSection<ELF64BE>;
2384
2385template class elf::VersionNeedSection<ELF32LE>;
2386template class elf::VersionNeedSection<ELF32BE>;
2387template class elf::VersionNeedSection<ELF64LE>;
2388template class elf::VersionNeedSection<ELF64BE>;
2389
2390template class elf::VersionDefinitionSection<ELF32LE>;
2391template class elf::VersionDefinitionSection<ELF32BE>;
2392template class elf::VersionDefinitionSection<ELF64LE>;
2393template class elf::VersionDefinitionSection<ELF64BE>;
Eugene Leviant17b7a572016-11-22 17:49:14 +00002394
Rui Ueyamabdfa1552016-11-22 19:24:52 +00002395template class elf::MipsRldMapSection<ELF32LE>;
2396template class elf::MipsRldMapSection<ELF32BE>;
2397template class elf::MipsRldMapSection<ELF64LE>;
2398template class elf::MipsRldMapSection<ELF64BE>;
Peter Smith719eb8e2016-11-24 11:43:55 +00002399
2400template class elf::ARMExidxSentinelSection<ELF32LE>;
2401template class elf::ARMExidxSentinelSection<ELF32BE>;
2402template class elf::ARMExidxSentinelSection<ELF64LE>;
2403template class elf::ARMExidxSentinelSection<ELF64BE>;
Peter Smith3a52eb02017-02-01 10:26:03 +00002404
2405template class elf::ThunkSection<ELF32LE>;
2406template class elf::ThunkSection<ELF32BE>;
2407template class elf::ThunkSection<ELF64LE>;
2408template class elf::ThunkSection<ELF64BE>;
Rafael Espindola66b4e212017-02-23 22:06:28 +00002409
2410template class elf::EhFrameSection<ELF32LE>;
2411template class elf::EhFrameSection<ELF32BE>;
2412template class elf::EhFrameSection<ELF64LE>;
2413template class elf::EhFrameSection<ELF64BE>;