blob: 3653354a46da5cc6a1bf3754ea11d32d6f6f6104 [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 Ueyamae288eef2016-11-02 18:58:44 +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 Ueyama6dc7fcb2016-11-01 20:28:21 +000029
Rui Ueyama3da3f062016-11-10 20:20:37 +000030#include "lld/Config/Version.h"
Eugene Leviant952eb4d2016-11-21 15:52:10 +000031#include "llvm/Support/Dwarf.h"
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000032#include "llvm/Support/Endian.h"
33#include "llvm/Support/MD5.h"
34#include "llvm/Support/RandomNumberGenerator.h"
35#include "llvm/Support/SHA1.h"
36#include "llvm/Support/xxhash.h"
Rui Ueyama3da3f062016-11-10 20:20:37 +000037#include <cstdlib>
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000038
39using namespace llvm;
Eugene Leviant952eb4d2016-11-21 15:52:10 +000040using namespace llvm::dwarf;
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000041using namespace llvm::ELF;
42using namespace llvm::object;
43using namespace llvm::support;
44using namespace llvm::support::endian;
45
46using namespace lld;
47using namespace lld::elf;
48
Rui Ueyamae8a61022016-11-05 23:05:47 +000049template <class ELFT> static std::vector<DefinedCommon *> getCommonSymbols() {
50 std::vector<DefinedCommon *> V;
51 for (Symbol *S : Symtab<ELFT>::X->getSymbols())
52 if (auto *B = dyn_cast<DefinedCommon>(S->body()))
53 V.push_back(B);
54 return V;
55}
56
57// Find all common symbols and allocate space for them.
Rafael Espindola682a5bc2016-11-08 14:42:34 +000058template <class ELFT> InputSection<ELFT> *elf::createCommonSection() {
59 auto *Ret = make<InputSection<ELFT>>(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1,
60 ArrayRef<uint8_t>(), "COMMON");
61 Ret->Live = true;
Rui Ueyamae8a61022016-11-05 23:05:47 +000062
63 // Sort the common symbols by alignment as an heuristic to pack them better.
64 std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>();
65 std::stable_sort(Syms.begin(), Syms.end(),
66 [](const DefinedCommon *A, const DefinedCommon *B) {
67 return A->Alignment > B->Alignment;
68 });
69
70 // Assign offsets to symbols.
71 size_t Size = 0;
72 size_t Alignment = 1;
73 for (DefinedCommon *Sym : Syms) {
Rui Ueyama1c786822016-11-05 23:14:54 +000074 Alignment = std::max<size_t>(Alignment, Sym->Alignment);
Rui Ueyamae8a61022016-11-05 23:05:47 +000075 Size = alignTo(Size, Sym->Alignment);
76
77 // Compute symbol offset relative to beginning of input section.
78 Sym->Offset = Size;
79 Size += Sym->Size;
80 }
Rafael Espindola682a5bc2016-11-08 14:42:34 +000081 Ret->Alignment = Alignment;
82 Ret->Data = makeArrayRef<uint8_t>(nullptr, Size);
83 return Ret;
Rui Ueyamae8a61022016-11-05 23:05:47 +000084}
85
Rui Ueyama3da3f062016-11-10 20:20:37 +000086// Returns an LLD version string.
87static ArrayRef<uint8_t> getVersion() {
88 // Check LLD_VERSION first for ease of testing.
89 // You can get consitent output by using the environment variable.
90 // This is only for testing.
91 StringRef S = getenv("LLD_VERSION");
92 if (S.empty())
93 S = Saver.save(Twine("Linker: ") + getLLDVersion());
94
95 // +1 to include the terminating '\0'.
96 return {(const uint8_t *)S.data(), S.size() + 1};
Davide Italianob69f38f2016-11-11 00:05:41 +000097}
Rui Ueyama3da3f062016-11-10 20:20:37 +000098
99// Creates a .comment section containing LLD version info.
100// With this feature, you can identify LLD-generated binaries easily
101// by "objdump -s -j .comment <file>".
102// The returned object is a mergeable string section.
103template <class ELFT> MergeInputSection<ELFT> *elf::createCommentSection() {
104 typename ELFT::Shdr Hdr = {};
105 Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
106 Hdr.sh_type = SHT_PROGBITS;
107 Hdr.sh_entsize = 1;
108 Hdr.sh_addralign = 1;
109
110 auto *Ret = make<MergeInputSection<ELFT>>(/*file=*/nullptr, &Hdr, ".comment");
111 Ret->Data = getVersion();
112 Ret->splitIntoPieces();
113 return Ret;
114}
115
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000116// .MIPS.abiflags section.
117template <class ELFT>
Rui Ueyama12f2da82016-11-22 03:57:06 +0000118MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags)
119 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"),
120 Flags(Flags) {}
121
122template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
123 memcpy(Buf, &Flags, sizeof(Flags));
124}
125
126template <class ELFT>
127MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
128 Elf_Mips_ABIFlags Flags = {};
129 bool Create = false;
130
131 for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
132 if (!Sec->Live || Sec->Type != SHT_MIPS_ABIFLAGS)
133 continue;
134 Sec->Live = false;
135 Create = true;
136
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000137 std::string Filename = toString(Sec->getFile());
Rui Ueyama12f2da82016-11-22 03:57:06 +0000138 if (Sec->Data.size() != sizeof(Elf_Mips_ABIFlags)) {
139 error(Filename + ": invalid size of .MIPS.abiflags section");
140 return nullptr;
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000141 }
Rui Ueyama12f2da82016-11-22 03:57:06 +0000142 auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000143 if (S->version != 0) {
Rui Ueyama12f2da82016-11-22 03:57:06 +0000144 error(Filename + ": unexpected .MIPS.abiflags version " +
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000145 Twine(S->version));
Rui Ueyama12f2da82016-11-22 03:57:06 +0000146 return nullptr;
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000147 }
Rui Ueyama12f2da82016-11-22 03:57:06 +0000148
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000149 // LLD checks ISA compatibility in getMipsEFlags(). Here we just
150 // select the highest number of ISA/Rev/Ext.
151 Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
152 Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
153 Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
154 Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
155 Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
156 Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
157 Flags.ases |= S->ases;
158 Flags.flags1 |= S->flags1;
159 Flags.flags2 |= S->flags2;
Rui Ueyama12f2da82016-11-22 03:57:06 +0000160 Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000161 };
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000162
Rui Ueyama12f2da82016-11-22 03:57:06 +0000163 if (Create)
164 return make<MipsAbiFlagsSection<ELFT>>(Flags);
165 return nullptr;
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000166}
167
Simon Atanasyance02cf02016-11-09 21:36:56 +0000168// .MIPS.options section.
169template <class ELFT>
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000170MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo Reginfo)
171 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"),
172 Reginfo(Reginfo) {}
Simon Atanasyance02cf02016-11-09 21:36:56 +0000173
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000174template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
175 auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
176 Options->kind = ODK_REGINFO;
177 Options->size = getSize();
178
179 if (!Config->Relocatable)
Simon Atanasyan8469b882016-11-23 22:22:16 +0000180 Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp();
Rafael Espindola4862ae82016-11-24 16:38:35 +0000181 memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
Simon Atanasyance02cf02016-11-09 21:36:56 +0000182}
183
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000184template <class ELFT>
185MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
186 // N64 ABI only.
187 if (!ELFT::Is64Bits)
188 return nullptr;
189
190 Elf_Mips_RegInfo Reginfo = {};
191 bool Create = false;
192
193 for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
194 if (!Sec->Live || Sec->Type != SHT_MIPS_OPTIONS)
195 continue;
196 Sec->Live = false;
197 Create = true;
198
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000199 std::string Filename = toString(Sec->getFile());
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000200 ArrayRef<uint8_t> D = Sec->Data;
201
202 while (!D.empty()) {
203 if (D.size() < sizeof(Elf_Mips_Options)) {
204 error(Filename + ": invalid size of .MIPS.options section");
205 break;
206 }
207
208 auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
209 if (Opt->kind == ODK_REGINFO) {
210 if (Config->Relocatable && Opt->getRegInfo().ri_gp_value)
211 error(Filename + ": unsupported non-zero ri_gp_value");
212 Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
213 Sec->getFile()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
214 break;
215 }
216
217 if (!Opt->size)
218 fatal(Filename + ": zero option descriptor size");
219 D = D.slice(Opt->size);
220 }
221 };
222
223 if (Create)
Rui Ueyama3cc93d72016-11-22 23:13:08 +0000224 return make<MipsOptionsSection<ELFT>>(Reginfo);
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000225 return nullptr;
Simon Atanasyance02cf02016-11-09 21:36:56 +0000226}
227
228// MIPS .reginfo section.
229template <class ELFT>
Rui Ueyamab71cae92016-11-22 03:57:08 +0000230MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo)
231 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),
232 Reginfo(Reginfo) {}
Simon Atanasyance02cf02016-11-09 21:36:56 +0000233
Rui Ueyamab71cae92016-11-22 03:57:08 +0000234template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
Simon Atanasyance02cf02016-11-09 21:36:56 +0000235 if (!Config->Relocatable)
Simon Atanasyan8469b882016-11-23 22:22:16 +0000236 Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp();
Rui Ueyamab71cae92016-11-22 03:57:08 +0000237 memcpy(Buf, &Reginfo, sizeof(Reginfo));
238}
239
240template <class ELFT>
241MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
242 // Section should be alive for O32 and N32 ABIs only.
243 if (ELFT::Is64Bits)
244 return nullptr;
245
246 Elf_Mips_RegInfo Reginfo = {};
247 bool Create = false;
248
249 for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
250 if (!Sec->Live || Sec->Type != SHT_MIPS_REGINFO)
251 continue;
252 Sec->Live = false;
253 Create = true;
254
255 if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000256 error(toString(Sec->getFile()) + ": invalid size of .reginfo section");
Rui Ueyamab71cae92016-11-22 03:57:08 +0000257 return nullptr;
258 }
259 auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
260 if (Config->Relocatable && R->ri_gp_value)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000261 error(toString(Sec->getFile()) + ": unsupported non-zero ri_gp_value");
Rui Ueyamab71cae92016-11-22 03:57:08 +0000262
263 Reginfo.ri_gprmask |= R->ri_gprmask;
264 Sec->getFile()->MipsGp0 = R->ri_gp_value;
265 };
266
267 if (Create)
268 return make<MipsReginfoSection<ELFT>>(Reginfo);
269 return nullptr;
Simon Atanasyance02cf02016-11-09 21:36:56 +0000270}
271
Rafael Espindolac0e47fb2016-11-08 14:56:27 +0000272template <class ELFT> InputSection<ELFT> *elf::createInterpSection() {
273 auto *Ret = make<InputSection<ELFT>>(SHF_ALLOC, SHT_PROGBITS, 1,
Rui Ueyama81a4b262016-11-22 04:33:01 +0000274 ArrayRef<uint8_t>(), ".interp");
Rafael Espindolac0e47fb2016-11-08 14:56:27 +0000275 Ret->Live = true;
Rui Ueyama81a4b262016-11-22 04:33:01 +0000276
277 // StringSaver guarantees that the returned string ends with '\0'.
278 StringRef S = Saver.save(Config->DynamicLinker);
279 Ret->Data = {(const uint8_t *)S.data(), S.size() + 1};
Rafael Espindolac0e47fb2016-11-08 14:56:27 +0000280 return Ret;
Rui Ueyamaa9ee8d62016-11-04 22:25:39 +0000281}
Rui Ueyamae288eef2016-11-02 18:58:44 +0000282
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000283static size_t getHashSize() {
Rui Ueyamac4030a12016-11-22 00:54:15 +0000284 switch (Config->BuildId) {
285 case BuildIdKind::Fast:
286 return 8;
287 case BuildIdKind::Md5:
288 case BuildIdKind::Uuid:
289 return 16;
290 case BuildIdKind::Sha1:
291 return 20;
292 case BuildIdKind::Hexstring:
293 return Config->BuildIdVector.size();
294 default:
295 llvm_unreachable("unknown BuildIdKind");
296 }
297}
298
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000299template <class ELFT>
300BuildIdSection<ELFT>::BuildIdSection()
301 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_NOTE, 1, ".note.gnu.build-id"),
Rui Ueyamabb536fe2016-11-22 01:36:19 +0000302 HashSize(getHashSize()) {}
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000303
304template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
305 const endianness E = ELFT::TargetEndianness;
306 write32<E>(Buf, 4); // Name size
307 write32<E>(Buf + 4, HashSize); // Content size
308 write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type
309 memcpy(Buf + 12, "GNU", 4); // Name string
310 HashBuf = Buf + 16;
311}
312
Rui Ueyama35e00752016-11-10 00:12:28 +0000313// Split one uint8 array into small pieces of uint8 arrays.
George Rimar364b59e22016-11-06 07:42:55 +0000314static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr,
315 size_t ChunkSize) {
316 std::vector<ArrayRef<uint8_t>> Ret;
317 while (Arr.size() > ChunkSize) {
318 Ret.push_back(Arr.take_front(ChunkSize));
319 Arr = Arr.drop_front(ChunkSize);
320 }
321 if (!Arr.empty())
322 Ret.push_back(Arr);
323 return Ret;
324}
325
Rui Ueyama35e00752016-11-10 00:12:28 +0000326// Computes a hash value of Data using a given hash function.
327// In order to utilize multiple cores, we first split data into 1MB
328// chunks, compute a hash for each chunk, and then compute a hash value
329// of the hash values.
George Rimar364b59e22016-11-06 07:42:55 +0000330template <class ELFT>
331void BuildIdSection<ELFT>::computeHash(
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000332 llvm::ArrayRef<uint8_t> Data,
333 std::function<void(uint8_t *Dest, ArrayRef<uint8_t> Arr)> HashFn) {
George Rimar364b59e22016-11-06 07:42:55 +0000334 std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024);
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000335 std::vector<uint8_t> Hashes(Chunks.size() * HashSize);
George Rimar364b59e22016-11-06 07:42:55 +0000336
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000337 // Compute hash values.
Rui Ueyama244a4352016-12-03 21:24:51 +0000338 forLoop(0, Chunks.size(),
339 [&](size_t I) { HashFn(Hashes.data() + I * HashSize, Chunks[I]); });
Rui Ueyama35e00752016-11-10 00:12:28 +0000340
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000341 // Write to the final output buffer.
342 HashFn(HashBuf, Hashes);
George Rimar364b59e22016-11-06 07:42:55 +0000343}
344
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000345template <class ELFT>
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000346void BuildIdSection<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
Rui Ueyamac4030a12016-11-22 00:54:15 +0000347 switch (Config->BuildId) {
348 case BuildIdKind::Fast:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000349 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
Rui Ueyamac4030a12016-11-22 00:54:15 +0000350 write64le(Dest, xxHash64(toStringRef(Arr)));
351 });
352 break;
353 case BuildIdKind::Md5:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000354 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
Rui Ueyama28590b62016-11-23 18:11:38 +0000355 memcpy(Dest, MD5::hash(Arr).data(), 16);
Rui Ueyamac4030a12016-11-22 00:54:15 +0000356 });
357 break;
358 case BuildIdKind::Sha1:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000359 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
Rui Ueyama28590b62016-11-23 18:11:38 +0000360 memcpy(Dest, SHA1::hash(Arr).data(), 20);
Rui Ueyamac4030a12016-11-22 00:54:15 +0000361 });
362 break;
363 case BuildIdKind::Uuid:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000364 if (getRandomBytes(HashBuf, HashSize))
Rui Ueyamac4030a12016-11-22 00:54:15 +0000365 error("entropy source failure");
366 break;
367 case BuildIdKind::Hexstring:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000368 memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size());
Rui Ueyamac4030a12016-11-22 00:54:15 +0000369 break;
370 default:
371 llvm_unreachable("unknown BuildIdKind");
372 }
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000373}
374
Eugene Leviant41ca3272016-11-10 09:48:29 +0000375template <class ELFT>
Eugene Leviantad4439e2016-11-11 11:33:32 +0000376GotSection<ELFT>::GotSection()
377 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
Simon Atanasyan725dc142016-11-16 21:01:02 +0000378 Target->GotEntrySize, ".got") {}
Eugene Leviantad4439e2016-11-11 11:33:32 +0000379
380template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000381 Sym.GotIndex = NumEntries;
382 ++NumEntries;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000383}
384
Simon Atanasyan725dc142016-11-16 21:01:02 +0000385template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
386 if (Sym.GlobalDynIndex != -1U)
387 return false;
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000388 Sym.GlobalDynIndex = NumEntries;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000389 // Global Dynamic TLS entries take two GOT slots.
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000390 NumEntries += 2;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000391 return true;
392}
393
394// Reserves TLS entries for a TLS module ID and a TLS block offset.
395// In total it takes two GOT slots.
396template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
397 if (TlsIndexOff != uint32_t(-1))
398 return false;
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000399 TlsIndexOff = NumEntries * sizeof(uintX_t);
400 NumEntries += 2;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000401 return true;
402}
403
Eugene Leviantad4439e2016-11-11 11:33:32 +0000404template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000405typename GotSection<ELFT>::uintX_t
406GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
407 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
408}
409
410template <class ELFT>
411typename GotSection<ELFT>::uintX_t
412GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
413 return B.GlobalDynIndex * sizeof(uintX_t);
414}
415
416template <class ELFT> void GotSection<ELFT>::finalize() {
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000417 Size = NumEntries * sizeof(uintX_t);
Simon Atanasyan725dc142016-11-16 21:01:02 +0000418}
419
George Rimar11992c862016-11-25 08:05:41 +0000420template <class ELFT> bool GotSection<ELFT>::empty() const {
421 // If we have a relocation that is relative to GOT (such as GOTOFFREL),
422 // we need to emit a GOT even if it's empty.
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000423 return NumEntries == 0 && !HasGotOffRel;
George Rimar11992c862016-11-25 08:05:41 +0000424}
425
Simon Atanasyan725dc142016-11-16 21:01:02 +0000426template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000427 this->relocate(Buf, Buf + Size);
Simon Atanasyan725dc142016-11-16 21:01:02 +0000428}
429
430template <class ELFT>
431MipsGotSection<ELFT>::MipsGotSection()
432 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL,
433 SHT_PROGBITS, Target->GotEntrySize, ".got") {}
434
435template <class ELFT>
436void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, uintX_t Addend,
Eugene Leviantad4439e2016-11-11 11:33:32 +0000437 RelExpr Expr) {
438 // For "true" local symbols which can be referenced from the same module
439 // only compiler creates two instructions for address loading:
440 //
441 // lw $8, 0($gp) # R_MIPS_GOT16
442 // addi $8, $8, 0 # R_MIPS_LO16
443 //
444 // The first instruction loads high 16 bits of the symbol address while
445 // the second adds an offset. That allows to reduce number of required
446 // GOT entries because only one global offset table entry is necessary
447 // for every 64 KBytes of local data. So for local symbols we need to
448 // allocate number of GOT entries to hold all required "page" addresses.
449 //
450 // All global symbols (hidden and regular) considered by compiler uniformly.
451 // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
452 // to load address of the symbol. So for each such symbol we need to
453 // allocate dedicated GOT entry to store its address.
454 //
455 // If a symbol is preemptible we need help of dynamic linker to get its
456 // final address. The corresponding GOT entries are allocated in the
457 // "global" part of GOT. Entries for non preemptible global symbol allocated
458 // in the "local" part of GOT.
459 //
460 // See "Global Offset Table" in Chapter 5:
461 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
462 if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
463 // At this point we do not know final symbol value so to reduce number
464 // of allocated GOT entries do the following trick. Save all output
465 // sections referenced by GOT relocations. Then later in the `finalize`
466 // method calculate number of "pages" required to cover all saved output
467 // section and allocate appropriate number of GOT entries.
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000468 PageIndexMap.insert({cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec, 0});
Eugene Leviantad4439e2016-11-11 11:33:32 +0000469 return;
470 }
471 if (Sym.isTls()) {
472 // GOT entries created for MIPS TLS relocations behave like
473 // almost GOT entries from other ABIs. They go to the end
474 // of the global offset table.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000475 Sym.GotIndex = TlsEntries.size();
476 TlsEntries.push_back(&Sym);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000477 return;
478 }
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000479 auto AddEntry = [&](SymbolBody &S, uintX_t A, GotEntries &Items) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000480 if (S.isInGot() && !A)
481 return;
482 size_t NewIndex = Items.size();
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000483 if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second)
Eugene Leviantad4439e2016-11-11 11:33:32 +0000484 return;
485 Items.emplace_back(&S, A);
486 if (!A)
487 S.GotIndex = NewIndex;
488 };
489 if (Sym.isPreemptible()) {
490 // Ignore addends for preemptible symbols. They got single GOT entry anyway.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000491 AddEntry(Sym, 0, GlobalEntries);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000492 Sym.IsInGlobalMipsGot = true;
493 } else if (Expr == R_MIPS_GOT_OFF32) {
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000494 AddEntry(Sym, Addend, LocalEntries32);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000495 Sym.Is32BitMipsGot = true;
496 } else {
497 // Hold local GOT entries accessed via a 16-bit index separately.
498 // That allows to write them in the beginning of the GOT and keep
499 // their indexes as less as possible to escape relocation's overflow.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000500 AddEntry(Sym, Addend, LocalEntries);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000501 }
502}
503
Simon Atanasyan725dc142016-11-16 21:01:02 +0000504template <class ELFT> bool MipsGotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000505 if (Sym.GlobalDynIndex != -1U)
506 return false;
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000507 Sym.GlobalDynIndex = TlsEntries.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000508 // Global Dynamic TLS entries take two GOT slots.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000509 TlsEntries.push_back(nullptr);
510 TlsEntries.push_back(&Sym);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000511 return true;
512}
513
514// Reserves TLS entries for a TLS module ID and a TLS block offset.
515// In total it takes two GOT slots.
Simon Atanasyan725dc142016-11-16 21:01:02 +0000516template <class ELFT> bool MipsGotSection<ELFT>::addTlsIndex() {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000517 if (TlsIndexOff != uint32_t(-1))
518 return false;
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000519 TlsIndexOff = TlsEntries.size() * sizeof(uintX_t);
520 TlsEntries.push_back(nullptr);
521 TlsEntries.push_back(nullptr);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000522 return true;
523}
524
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000525static uint64_t getMipsPageAddr(uint64_t Addr) {
526 return (Addr + 0x8000) & ~0xffff;
527}
528
529static uint64_t getMipsPageCount(uint64_t Size) {
530 return (Size + 0xfffe) / 0xffff + 1;
531}
532
Eugene Leviantad4439e2016-11-11 11:33:32 +0000533template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000534typename MipsGotSection<ELFT>::uintX_t
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000535MipsGotSection<ELFT>::getPageEntryOffset(const SymbolBody &B,
536 uintX_t Addend) const {
537 const OutputSectionBase *OutSec =
538 cast<DefinedRegular<ELFT>>(&B)->Section->OutSec;
539 uintX_t SecAddr = getMipsPageAddr(OutSec->Addr);
540 uintX_t SymAddr = getMipsPageAddr(B.getVA<ELFT>(Addend));
541 uintX_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff;
542 assert(Index < PageEntriesNum);
543 return (HeaderEntriesNum + Index) * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000544}
545
546template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000547typename MipsGotSection<ELFT>::uintX_t
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000548MipsGotSection<ELFT>::getBodyEntryOffset(const SymbolBody &B,
549 uintX_t Addend) const {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000550 // Calculate offset of the GOT entries block: TLS, global, local.
Simon Atanasyana0efc422016-11-29 10:23:50 +0000551 uintX_t Index = HeaderEntriesNum + PageEntriesNum;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000552 if (B.isTls())
Simon Atanasyana0efc422016-11-29 10:23:50 +0000553 Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000554 else if (B.IsInGlobalMipsGot)
Simon Atanasyana0efc422016-11-29 10:23:50 +0000555 Index += LocalEntries.size() + LocalEntries32.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000556 else if (B.Is32BitMipsGot)
Simon Atanasyana0efc422016-11-29 10:23:50 +0000557 Index += LocalEntries.size();
558 // Calculate offset of the GOT entry in the block.
Eugene Leviantad4439e2016-11-11 11:33:32 +0000559 if (B.isInGot())
Simon Atanasyana0efc422016-11-29 10:23:50 +0000560 Index += B.GotIndex;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000561 else {
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000562 auto It = EntryIndexMap.find({&B, Addend});
563 assert(It != EntryIndexMap.end());
Simon Atanasyana0efc422016-11-29 10:23:50 +0000564 Index += It->second;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000565 }
Simon Atanasyana0efc422016-11-29 10:23:50 +0000566 return Index * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000567}
568
569template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000570typename MipsGotSection<ELFT>::uintX_t
571MipsGotSection<ELFT>::getTlsOffset() const {
572 return (getLocalEntriesNum() + GlobalEntries.size()) * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000573}
574
575template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000576typename MipsGotSection<ELFT>::uintX_t
577MipsGotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000578 return B.GlobalDynIndex * sizeof(uintX_t);
579}
580
581template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000582const SymbolBody *MipsGotSection<ELFT>::getFirstGlobalEntry() const {
583 return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000584}
585
586template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000587unsigned MipsGotSection<ELFT>::getLocalEntriesNum() const {
Simon Atanasyana0efc422016-11-29 10:23:50 +0000588 return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() +
589 LocalEntries32.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000590}
591
Simon Atanasyan725dc142016-11-16 21:01:02 +0000592template <class ELFT> void MipsGotSection<ELFT>::finalize() {
Simon Atanasyana0efc422016-11-29 10:23:50 +0000593 PageEntriesNum = 0;
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000594 for (std::pair<const OutputSectionBase *, size_t> &P : PageIndexMap) {
595 // For each output section referenced by GOT page relocations calculate
596 // and save into PageIndexMap an upper bound of MIPS GOT entries required
597 // to store page addresses of local symbols. We assume the worst case -
598 // each 64kb page of the output section has at least one GOT relocation
599 // against it. And take in account the case when the section intersects
600 // page boundaries.
601 P.second = PageEntriesNum;
602 PageEntriesNum += getMipsPageCount(P.first->Size);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000603 }
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000604 Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) *
605 sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000606}
607
George Rimar11992c862016-11-25 08:05:41 +0000608template <class ELFT> bool MipsGotSection<ELFT>::empty() const {
609 // We add the .got section to the result for dynamic MIPS target because
610 // its address and properties are mentioned in the .dynamic section.
611 return Config->Relocatable;
612}
613
Simon Atanasyan8469b882016-11-23 22:22:16 +0000614template <class ELFT> unsigned MipsGotSection<ELFT>::getGp() const {
615 return ElfSym<ELFT>::MipsGp->template getVA<ELFT>(0);
616}
617
Eugene Leviantad4439e2016-11-11 11:33:32 +0000618template <class ELFT>
619static void writeUint(uint8_t *Buf, typename ELFT::uint Val) {
620 typedef typename ELFT::uint uintX_t;
621 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val);
622}
623
Simon Atanasyan725dc142016-11-16 21:01:02 +0000624template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000625 // Set the MSB of the second GOT slot. This is not required by any
626 // MIPS ABI documentation, though.
627 //
628 // There is a comment in glibc saying that "The MSB of got[1] of a
629 // gnu object is set to identify gnu objects," and in GNU gold it
630 // says "the second entry will be used by some runtime loaders".
631 // But how this field is being used is unclear.
632 //
633 // We are not really willing to mimic other linkers behaviors
634 // without understanding why they do that, but because all files
635 // generated by GNU tools have this special GOT value, and because
636 // we've been doing this for years, it is probably a safe bet to
637 // keep doing this for now. We really need to revisit this to see
638 // if we had to do this.
639 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
640 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyana0efc422016-11-29 10:23:50 +0000641 Buf += HeaderEntriesNum * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000642 // Write 'page address' entries to the local part of the GOT.
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000643 for (std::pair<const OutputSectionBase *, size_t> &L : PageIndexMap) {
644 size_t PageCount = getMipsPageCount(L.first->Size);
645 uintX_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
646 for (size_t PI = 0; PI < PageCount; ++PI) {
647 uint8_t *Entry = Buf + (L.second + PI) * sizeof(uintX_t);
648 writeUint<ELFT>(Entry, FirstPageAddr + PI * 0x10000);
649 }
Eugene Leviantad4439e2016-11-11 11:33:32 +0000650 }
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000651 Buf += PageEntriesNum * sizeof(uintX_t);
652 auto AddEntry = [&](const GotEntry &SA) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000653 uint8_t *Entry = Buf;
654 Buf += sizeof(uintX_t);
655 const SymbolBody *Body = SA.first;
656 uintX_t VA = Body->template getVA<ELFT>(SA.second);
657 writeUint<ELFT>(Entry, VA);
658 };
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000659 std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry);
660 std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry);
661 std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000662 // Initialize TLS-related GOT entries. If the entry has a corresponding
663 // dynamic relocations, leave it initialized by zero. Write down adjusted
664 // TLS symbol's values otherwise. To calculate the adjustments use offsets
665 // for thread-local storage.
666 // https://www.linux-mips.org/wiki/NPTL
667 if (TlsIndexOff != -1U && !Config->Pic)
668 writeUint<ELFT>(Buf + TlsIndexOff, 1);
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000669 for (const SymbolBody *B : TlsEntries) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000670 if (!B || B->isPreemptible())
671 continue;
672 uintX_t VA = B->getVA<ELFT>();
673 if (B->GotIndex != -1U) {
674 uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t);
675 writeUint<ELFT>(Entry, VA - 0x7000);
676 }
677 if (B->GlobalDynIndex != -1U) {
678 uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t);
679 writeUint<ELFT>(Entry, 1);
680 Entry += sizeof(uintX_t);
681 writeUint<ELFT>(Entry, VA - 0x8000);
682 }
683 }
684}
685
Eugene Leviantad4439e2016-11-11 11:33:32 +0000686template <class ELFT>
Eugene Leviant41ca3272016-11-10 09:48:29 +0000687GotPltSection<ELFT>::GotPltSection()
688 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
George Rimard8b27762016-11-14 10:14:18 +0000689 Target->GotPltEntrySize, ".got.plt") {}
Eugene Leviant41ca3272016-11-10 09:48:29 +0000690
691template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
692 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
693 Entries.push_back(&Sym);
694}
695
Eugene Leviant41ca3272016-11-10 09:48:29 +0000696template <class ELFT> size_t GotPltSection<ELFT>::getSize() const {
697 return (Target->GotPltHeaderEntriesNum + Entries.size()) *
698 Target->GotPltEntrySize;
699}
700
701template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
702 Target->writeGotPltHeader(Buf);
703 Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
704 for (const SymbolBody *B : Entries) {
705 Target->writeGotPlt(Buf, *B);
706 Buf += sizeof(uintX_t);
707 }
708}
709
Eugene Leviant22eb0262016-11-14 09:16:00 +0000710template <class ELFT>
711StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
712 : SyntheticSection<ELFT>(Dynamic ? (uintX_t)SHF_ALLOC : 0, SHT_STRTAB, 1,
713 Name),
714 Dynamic(Dynamic) {}
715
716// Adds a string to the string table. If HashIt is true we hash and check for
717// duplicates. It is optional because the name of global symbols are already
718// uniqued and hashing them again has a big cost for a small value: uniquing
719// them with some other string that happens to be the same.
720template <class ELFT>
721unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
722 if (HashIt) {
723 auto R = StringMap.insert(std::make_pair(S, this->Size));
724 if (!R.second)
725 return R.first->second;
726 }
727 unsigned Ret = this->Size;
728 this->Size = this->Size + S.size() + 1;
729 Strings.push_back(S);
730 return Ret;
731}
732
733template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
734 // ELF string tables start with NUL byte, so advance the pointer by one.
735 ++Buf;
736 for (StringRef S : Strings) {
737 memcpy(Buf, S.data(), S.size());
738 Buf += S.size() + 1;
739 }
740}
741
Eugene Leviante9bab5d2016-11-21 16:59:33 +0000742// Returns the number of version definition entries. Because the first entry
743// is for the version definition itself, it is the number of versioned symbols
744// plus one. Note that we don't support multiple versions yet.
Eugene Leviant6380ce22016-11-15 12:26:55 +0000745static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
746
747template <class ELFT>
748DynamicSection<ELFT>::DynamicSection()
749 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC,
750 sizeof(uintX_t), ".dynamic") {
751 this->Entsize = ELFT::Is64Bits ? 16 : 8;
752 // .dynamic section is not writable on MIPS.
753 // See "Special Section" in Chapter 4 in the following document:
754 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
755 if (Config->EMachine == EM_MIPS)
756 this->Flags = SHF_ALLOC;
757
758 addEntries();
759}
760
761// There are some dynamic entries that don't depend on other sections.
762// Such entries can be set early.
763template <class ELFT> void DynamicSection<ELFT>::addEntries() {
764 // Add strings to .dynstr early so that .dynstr's size will be
765 // fixed early.
766 for (StringRef S : Config->AuxiliaryList)
Rui Ueyama729ac792016-11-17 04:10:09 +0000767 add({DT_AUXILIARY, In<ELFT>::DynStrTab->addString(S)});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000768 if (!Config->RPath.empty())
Rui Ueyama729ac792016-11-17 04:10:09 +0000769 add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Eugene Leviant6380ce22016-11-15 12:26:55 +0000770 In<ELFT>::DynStrTab->addString(Config->RPath)});
771 for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
772 if (F->isNeeded())
Rui Ueyama729ac792016-11-17 04:10:09 +0000773 add({DT_NEEDED, In<ELFT>::DynStrTab->addString(F->getSoName())});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000774 if (!Config->SoName.empty())
Rui Ueyama729ac792016-11-17 04:10:09 +0000775 add({DT_SONAME, In<ELFT>::DynStrTab->addString(Config->SoName)});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000776
777 // Set DT_FLAGS and DT_FLAGS_1.
778 uint32_t DtFlags = 0;
779 uint32_t DtFlags1 = 0;
780 if (Config->Bsymbolic)
781 DtFlags |= DF_SYMBOLIC;
782 if (Config->ZNodelete)
783 DtFlags1 |= DF_1_NODELETE;
784 if (Config->ZNow) {
785 DtFlags |= DF_BIND_NOW;
786 DtFlags1 |= DF_1_NOW;
787 }
788 if (Config->ZOrigin) {
789 DtFlags |= DF_ORIGIN;
790 DtFlags1 |= DF_1_ORIGIN;
791 }
792
793 if (DtFlags)
Rui Ueyama729ac792016-11-17 04:10:09 +0000794 add({DT_FLAGS, DtFlags});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000795 if (DtFlags1)
Rui Ueyama729ac792016-11-17 04:10:09 +0000796 add({DT_FLAGS_1, DtFlags1});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000797
Petr Hosek668bebe2016-12-07 02:05:42 +0000798 if (!Config->Shared && !Config->Relocatable)
Rui Ueyama729ac792016-11-17 04:10:09 +0000799 add({DT_DEBUG, (uint64_t)0});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000800}
801
802// Add remaining entries to complete .dynamic contents.
803template <class ELFT> void DynamicSection<ELFT>::finalize() {
804 if (this->Size)
805 return; // Already finalized.
806
807 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
808
George Rimar11992c862016-11-25 08:05:41 +0000809 if (!In<ELFT>::RelaDyn->empty()) {
Eugene Leviant6380ce22016-11-15 12:26:55 +0000810 bool IsRela = Config->Rela;
Rui Ueyama729ac792016-11-17 04:10:09 +0000811 add({IsRela ? DT_RELA : DT_REL, In<ELFT>::RelaDyn});
812 add({IsRela ? DT_RELASZ : DT_RELSZ, In<ELFT>::RelaDyn->getSize()});
813 add({IsRela ? DT_RELAENT : DT_RELENT,
Eugene Leviant6380ce22016-11-15 12:26:55 +0000814 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
815
816 // MIPS dynamic loader does not support RELCOUNT tag.
817 // The problem is in the tight relation between dynamic
818 // relocations and GOT. So do not emit this tag on MIPS.
819 if (Config->EMachine != EM_MIPS) {
Eugene Levianta96d9022016-11-16 10:02:27 +0000820 size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
Eugene Leviant6380ce22016-11-15 12:26:55 +0000821 if (Config->ZCombreloc && NumRelativeRels)
Rui Ueyama729ac792016-11-17 04:10:09 +0000822 add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000823 }
824 }
George Rimar11992c862016-11-25 08:05:41 +0000825 if (!In<ELFT>::RelaPlt->empty()) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000826 add({DT_JMPREL, In<ELFT>::RelaPlt});
827 add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getSize()});
828 add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
Eugene Leviant6380ce22016-11-15 12:26:55 +0000829 In<ELFT>::GotPlt});
Rui Ueyama729ac792016-11-17 04:10:09 +0000830 add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000831 }
832
Eugene Leviant9230db92016-11-17 09:16:34 +0000833 add({DT_SYMTAB, In<ELFT>::DynSymTab});
Rui Ueyama729ac792016-11-17 04:10:09 +0000834 add({DT_SYMENT, sizeof(Elf_Sym)});
835 add({DT_STRTAB, In<ELFT>::DynStrTab});
836 add({DT_STRSZ, In<ELFT>::DynStrTab->getSize()});
Eugene Leviantbe809a72016-11-18 06:44:18 +0000837 if (In<ELFT>::GnuHashTab)
838 add({DT_GNU_HASH, In<ELFT>::GnuHashTab});
Eugene Leviantb96e8092016-11-18 09:06:47 +0000839 if (In<ELFT>::HashTab)
840 add({DT_HASH, In<ELFT>::HashTab});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000841
842 if (Out<ELFT>::PreinitArray) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000843 add({DT_PREINIT_ARRAY, Out<ELFT>::PreinitArray});
844 add({DT_PREINIT_ARRAYSZ, Out<ELFT>::PreinitArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000845 }
846 if (Out<ELFT>::InitArray) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000847 add({DT_INIT_ARRAY, Out<ELFT>::InitArray});
848 add({DT_INIT_ARRAYSZ, Out<ELFT>::InitArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000849 }
850 if (Out<ELFT>::FiniArray) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000851 add({DT_FINI_ARRAY, Out<ELFT>::FiniArray});
852 add({DT_FINI_ARRAYSZ, Out<ELFT>::FiniArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000853 }
854
855 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Init))
Rui Ueyama729ac792016-11-17 04:10:09 +0000856 add({DT_INIT, B});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000857 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Fini))
Rui Ueyama729ac792016-11-17 04:10:09 +0000858 add({DT_FINI, B});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000859
Eugene Leviante9bab5d2016-11-21 16:59:33 +0000860 bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
861 if (HasVerNeed || In<ELFT>::VerDef)
862 add({DT_VERSYM, In<ELFT>::VerSym});
863 if (In<ELFT>::VerDef) {
864 add({DT_VERDEF, In<ELFT>::VerDef});
Rui Ueyama729ac792016-11-17 04:10:09 +0000865 add({DT_VERDEFNUM, getVerDefNum()});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000866 }
867 if (HasVerNeed) {
Eugene Leviante9bab5d2016-11-21 16:59:33 +0000868 add({DT_VERNEED, In<ELFT>::VerNeed});
869 add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000870 }
871
872 if (Config->EMachine == EM_MIPS) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000873 add({DT_MIPS_RLD_VERSION, 1});
874 add({DT_MIPS_FLAGS, RHF_NOTPOT});
875 add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
Eugene Leviant9230db92016-11-17 09:16:34 +0000876 add({DT_MIPS_SYMTABNO, In<ELFT>::DynSymTab->getNumSymbols()});
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000877 add({DT_MIPS_LOCAL_GOTNO, In<ELFT>::MipsGot->getLocalEntriesNum()});
878 if (const SymbolBody *B = In<ELFT>::MipsGot->getFirstGlobalEntry())
Rui Ueyama729ac792016-11-17 04:10:09 +0000879 add({DT_MIPS_GOTSYM, B->DynsymIndex});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000880 else
Eugene Leviant9230db92016-11-17 09:16:34 +0000881 add({DT_MIPS_GOTSYM, In<ELFT>::DynSymTab->getNumSymbols()});
Rui Ueyama729ac792016-11-17 04:10:09 +0000882 add({DT_PLTGOT, In<ELFT>::MipsGot});
Eugene Leviant17b7a572016-11-22 17:49:14 +0000883 if (In<ELFT>::MipsRldMap)
884 add({DT_MIPS_RLD_MAP, In<ELFT>::MipsRldMap});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000885 }
886
887 this->OutSec->Entsize = this->Entsize;
888 this->OutSec->Link = this->Link;
889
890 // +1 for DT_NULL
891 this->Size = (Entries.size() + 1) * this->Entsize;
892}
893
894template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
895 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
896
897 for (const Entry &E : Entries) {
898 P->d_tag = E.Tag;
899 switch (E.Kind) {
900 case Entry::SecAddr:
901 P->d_un.d_ptr = E.OutSec->Addr;
902 break;
903 case Entry::InSecAddr:
904 P->d_un.d_ptr = E.InSec->OutSec->Addr + E.InSec->OutSecOff;
905 break;
906 case Entry::SecSize:
907 P->d_un.d_val = E.OutSec->Size;
908 break;
909 case Entry::SymAddr:
910 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
911 break;
912 case Entry::PlainInt:
913 P->d_un.d_val = E.Val;
914 break;
915 }
916 ++P;
917 }
918}
919
Eugene Levianta96d9022016-11-16 10:02:27 +0000920template <class ELFT>
921typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
922 if (OutputSec)
923 return OutputSec->Addr + OffsetInSec;
924 return InputSec->OutSec->Addr + InputSec->getOffset(OffsetInSec);
925}
926
927template <class ELFT>
928typename ELFT::uint DynamicReloc<ELFT>::getAddend() const {
929 if (UseSymVA)
930 return Sym->getVA<ELFT>(Addend);
931 return Addend;
932}
933
934template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const {
935 if (Sym && !UseSymVA)
936 return Sym->DynsymIndex;
937 return 0;
938}
939
940template <class ELFT>
941RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
942 : SyntheticSection<ELFT>(SHF_ALLOC, Config->Rela ? SHT_RELA : SHT_REL,
943 sizeof(uintX_t), Name),
944 Sort(Sort) {
945 this->Entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
946}
947
948template <class ELFT>
949void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
950 if (Reloc.Type == Target->RelativeRel)
951 ++NumRelativeRelocs;
952 Relocs.push_back(Reloc);
953}
954
955template <class ELFT, class RelTy>
956static bool compRelocations(const RelTy &A, const RelTy &B) {
957 bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel;
958 bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel;
959 if (AIsRel != BIsRel)
960 return AIsRel;
961
962 return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL);
963}
964
965template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
966 uint8_t *BufBegin = Buf;
967 for (const DynamicReloc<ELFT> &Rel : Relocs) {
968 auto *P = reinterpret_cast<Elf_Rela *>(Buf);
969 Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
970
971 if (Config->Rela)
972 P->r_addend = Rel.getAddend();
973 P->r_offset = Rel.getOffset();
Simon Atanasyan725dc142016-11-16 21:01:02 +0000974 if (Config->EMachine == EM_MIPS && Rel.getInputSec() == In<ELFT>::MipsGot)
Eugene Levianta96d9022016-11-16 10:02:27 +0000975 // Dynamic relocation against MIPS GOT section make deal TLS entries
976 // allocated in the end of the GOT. We need to adjust the offset to take
977 // in account 'local' and 'global' GOT entries.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000978 P->r_offset += In<ELFT>::MipsGot->getTlsOffset();
Eugene Levianta96d9022016-11-16 10:02:27 +0000979 P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL);
980 }
981
982 if (Sort) {
983 if (Config->Rela)
984 std::stable_sort((Elf_Rela *)BufBegin,
985 (Elf_Rela *)BufBegin + Relocs.size(),
986 compRelocations<ELFT, Elf_Rela>);
987 else
988 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
989 compRelocations<ELFT, Elf_Rel>);
990 }
991}
992
993template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
994 return this->Entsize * Relocs.size();
995}
996
997template <class ELFT> void RelocationSection<ELFT>::finalize() {
Eugene Leviant9230db92016-11-17 09:16:34 +0000998 this->Link = In<ELFT>::DynSymTab ? In<ELFT>::DynSymTab->OutSec->SectionIndex
999 : In<ELFT>::SymTab->OutSec->SectionIndex;
Eugene Levianta96d9022016-11-16 10:02:27 +00001000
1001 // Set required output section properties.
1002 this->OutSec->Link = this->Link;
1003 this->OutSec->Entsize = this->Entsize;
1004}
1005
Eugene Leviant9230db92016-11-17 09:16:34 +00001006template <class ELFT>
1007SymbolTableSection<ELFT>::SymbolTableSection(
1008 StringTableSection<ELFT> &StrTabSec)
1009 : SyntheticSection<ELFT>(StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0,
1010 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1011 sizeof(uintX_t),
1012 StrTabSec.isDynamic() ? ".dynsym" : ".symtab"),
1013 StrTabSec(StrTabSec) {
1014 this->Entsize = sizeof(Elf_Sym);
1015}
1016
1017// Orders symbols according to their positions in the GOT,
1018// in compliance with MIPS ABI rules.
1019// See "Global Offset Table" in Chapter 5 in the following document
1020// for detailed description:
1021// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1022static bool sortMipsSymbols(const SymbolBody *L, const SymbolBody *R) {
1023 // Sort entries related to non-local preemptible symbols by GOT indexes.
1024 // All other entries go to the first part of GOT in arbitrary order.
1025 bool LIsInLocalGot = !L->IsInGlobalMipsGot;
1026 bool RIsInLocalGot = !R->IsInGlobalMipsGot;
1027 if (LIsInLocalGot || RIsInLocalGot)
1028 return !RIsInLocalGot;
1029 return L->GotIndex < R->GotIndex;
1030}
1031
1032static uint8_t getSymbolBinding(SymbolBody *Body) {
1033 Symbol *S = Body->symbol();
1034 if (Config->Relocatable)
1035 return S->Binding;
1036 uint8_t Visibility = S->Visibility;
1037 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1038 return STB_LOCAL;
1039 if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE)
1040 return STB_GLOBAL;
1041 return S->Binding;
1042}
1043
1044template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
1045 this->OutSec->Link = this->Link = StrTabSec.OutSec->SectionIndex;
1046 this->OutSec->Info = this->Info = NumLocals + 1;
1047 this->OutSec->Entsize = this->Entsize;
1048
1049 if (Config->Relocatable) {
1050 size_t I = NumLocals;
1051 for (const SymbolTableEntry &S : Symbols)
1052 S.Symbol->DynsymIndex = ++I;
1053 return;
1054 }
1055
1056 if (!StrTabSec.isDynamic()) {
1057 std::stable_sort(Symbols.begin(), Symbols.end(),
1058 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1059 return getSymbolBinding(L.Symbol) == STB_LOCAL &&
1060 getSymbolBinding(R.Symbol) != STB_LOCAL;
1061 });
1062 return;
1063 }
Eugene Leviantbe809a72016-11-18 06:44:18 +00001064 if (In<ELFT>::GnuHashTab)
Eugene Leviant9230db92016-11-17 09:16:34 +00001065 // NB: It also sorts Symbols to meet the GNU hash table requirements.
Eugene Leviantbe809a72016-11-18 06:44:18 +00001066 In<ELFT>::GnuHashTab->addSymbols(Symbols);
Eugene Leviant9230db92016-11-17 09:16:34 +00001067 else if (Config->EMachine == EM_MIPS)
1068 std::stable_sort(Symbols.begin(), Symbols.end(),
1069 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1070 return sortMipsSymbols(L.Symbol, R.Symbol);
1071 });
1072 size_t I = 0;
1073 for (const SymbolTableEntry &S : Symbols)
1074 S.Symbol->DynsymIndex = ++I;
1075}
1076
1077template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
1078 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
1079}
1080
1081template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1082 Buf += sizeof(Elf_Sym);
1083
1084 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1085 // .dynsym only contains global symbols.
1086 if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic())
1087 writeLocalSymbols(Buf);
1088
1089 writeGlobalSymbols(Buf);
1090}
1091
1092template <class ELFT>
1093void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1094 // Iterate over all input object files to copy their local symbols
1095 // to the output symbol table pointed by Buf.
1096 for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
1097 for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P :
1098 File->KeptLocalSyms) {
1099 const DefinedRegular<ELFT> &Body = *P.first;
1100 InputSectionBase<ELFT> *Section = Body.Section;
1101 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1102
1103 if (!Section) {
1104 ESym->st_shndx = SHN_ABS;
1105 ESym->st_value = Body.Value;
1106 } else {
1107 const OutputSectionBase *OutSec = Section->OutSec;
1108 ESym->st_shndx = OutSec->SectionIndex;
1109 ESym->st_value = OutSec->Addr + Section->getOffset(Body);
1110 }
1111 ESym->st_name = P.second;
1112 ESym->st_size = Body.template getSize<ELFT>();
1113 ESym->setBindingAndType(STB_LOCAL, Body.Type);
1114 Buf += sizeof(*ESym);
1115 }
1116 }
1117}
1118
1119template <class ELFT>
1120void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
1121 // Write the internal symbol table contents to the output symbol table
1122 // pointed by Buf.
1123 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1124 for (const SymbolTableEntry &S : Symbols) {
1125 SymbolBody *Body = S.Symbol;
1126 size_t StrOff = S.StrTabOffset;
1127
1128 uint8_t Type = Body->Type;
1129 uintX_t Size = Body->getSize<ELFT>();
1130
1131 ESym->setBindingAndType(getSymbolBinding(Body), Type);
1132 ESym->st_size = Size;
1133 ESym->st_name = StrOff;
1134 ESym->setVisibility(Body->symbol()->Visibility);
1135 ESym->st_value = Body->getVA<ELFT>();
1136
1137 if (const OutputSectionBase *OutSec = getOutputSection(Body))
1138 ESym->st_shndx = OutSec->SectionIndex;
1139 else if (isa<DefinedRegular<ELFT>>(Body))
1140 ESym->st_shndx = SHN_ABS;
1141
1142 if (Config->EMachine == EM_MIPS) {
1143 // On MIPS we need to mark symbol which has a PLT entry and requires
1144 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1145 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1146 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1147 if (Body->isInPlt() && Body->NeedsCopyOrPltAddr)
1148 ESym->st_other |= STO_MIPS_PLT;
1149 if (Config->Relocatable) {
1150 auto *D = dyn_cast<DefinedRegular<ELFT>>(Body);
1151 if (D && D->isMipsPIC())
1152 ESym->st_other |= STO_MIPS_PIC;
1153 }
1154 }
1155 ++ESym;
1156 }
1157}
1158
1159template <class ELFT>
1160const OutputSectionBase *
1161SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1162 switch (Sym->kind()) {
1163 case SymbolBody::DefinedSyntheticKind:
1164 return cast<DefinedSynthetic<ELFT>>(Sym)->Section;
1165 case SymbolBody::DefinedRegularKind: {
1166 auto &D = cast<DefinedRegular<ELFT>>(*Sym);
1167 if (D.Section)
1168 return D.Section->OutSec;
1169 break;
1170 }
1171 case SymbolBody::DefinedCommonKind:
1172 return In<ELFT>::Common->OutSec;
1173 case SymbolBody::SharedKind:
1174 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1175 return Out<ELFT>::Bss;
1176 break;
1177 case SymbolBody::UndefinedKind:
1178 case SymbolBody::LazyArchiveKind:
1179 case SymbolBody::LazyObjectKind:
1180 break;
1181 }
1182 return nullptr;
1183}
1184
Eugene Leviantbe809a72016-11-18 06:44:18 +00001185template <class ELFT>
1186GnuHashTableSection<ELFT>::GnuHashTableSection()
1187 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_HASH, sizeof(uintX_t),
1188 ".gnu.hash") {
1189 this->Entsize = ELFT::Is64Bits ? 0 : 4;
1190}
1191
1192template <class ELFT>
1193unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
1194 if (!NumHashed)
1195 return 0;
1196
1197 // These values are prime numbers which are not greater than 2^(N-1) + 1.
1198 // In result, for any particular NumHashed we return a prime number
1199 // which is not greater than NumHashed.
1200 static const unsigned Primes[] = {
1201 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
1202 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
1203
1204 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
1205 array_lengthof(Primes) - 1)];
1206}
1207
1208// Bloom filter estimation: at least 8 bits for each hashed symbol.
1209// GNU Hash table requirement: it should be a power of 2,
1210// the minimum value is 1, even for an empty table.
1211// Expected results for a 32-bit target:
1212// calcMaskWords(0..4) = 1
1213// calcMaskWords(5..8) = 2
1214// calcMaskWords(9..16) = 4
1215// For a 64-bit target:
1216// calcMaskWords(0..8) = 1
1217// calcMaskWords(9..16) = 2
1218// calcMaskWords(17..32) = 4
1219template <class ELFT>
1220unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
1221 if (!NumHashed)
1222 return 1;
1223 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
1224}
1225
1226template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
1227 unsigned NumHashed = Symbols.size();
1228 NBuckets = calcNBuckets(NumHashed);
1229 MaskWords = calcMaskWords(NumHashed);
1230 // Second hash shift estimation: just predefined values.
1231 Shift2 = ELFT::Is64Bits ? 6 : 5;
1232
1233 this->OutSec->Entsize = this->Entsize;
1234 this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1235 this->Size = sizeof(Elf_Word) * 4 // Header
1236 + sizeof(Elf_Off) * MaskWords // Bloom Filter
1237 + sizeof(Elf_Word) * NBuckets // Hash Buckets
1238 + sizeof(Elf_Word) * NumHashed; // Hash Values
1239}
1240
1241template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1242 writeHeader(Buf);
1243 if (Symbols.empty())
1244 return;
1245 writeBloomFilter(Buf);
1246 writeHashTable(Buf);
1247}
1248
1249template <class ELFT>
1250void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
1251 auto *P = reinterpret_cast<Elf_Word *>(Buf);
1252 *P++ = NBuckets;
1253 *P++ = In<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
1254 *P++ = MaskWords;
1255 *P++ = Shift2;
1256 Buf = reinterpret_cast<uint8_t *>(P);
1257}
1258
1259template <class ELFT>
1260void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
1261 unsigned C = sizeof(Elf_Off) * 8;
1262
1263 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
1264 for (const SymbolData &Sym : Symbols) {
1265 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
1266 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
1267 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
1268 Masks[Pos] |= V;
1269 }
1270 Buf += sizeof(Elf_Off) * MaskWords;
1271}
1272
1273template <class ELFT>
1274void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
1275 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
1276 Elf_Word *Values = Buckets + NBuckets;
1277
1278 int PrevBucket = -1;
1279 int I = 0;
1280 for (const SymbolData &Sym : Symbols) {
1281 int Bucket = Sym.Hash % NBuckets;
1282 assert(PrevBucket <= Bucket);
1283 if (Bucket != PrevBucket) {
1284 Buckets[Bucket] = Sym.Body->DynsymIndex;
1285 PrevBucket = Bucket;
1286 if (I > 0)
1287 Values[I - 1] |= 1;
1288 }
1289 Values[I] = Sym.Hash & ~1;
1290 ++I;
1291 }
1292 if (I > 0)
1293 Values[I - 1] |= 1;
1294}
1295
1296static uint32_t hashGnu(StringRef Name) {
1297 uint32_t H = 5381;
1298 for (uint8_t C : Name)
1299 H = (H << 5) + H + C;
1300 return H;
1301}
1302
1303// Add symbols to this symbol hash table. Note that this function
1304// destructively sort a given vector -- which is needed because
1305// GNU-style hash table places some sorting requirements.
1306template <class ELFT>
1307void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) {
1308 // Ideally this will just be 'auto' but GCC 6.1 is not able
1309 // to deduce it correctly.
1310 std::vector<SymbolTableEntry>::iterator Mid =
1311 std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
1312 return S.Symbol->isUndefined();
1313 });
1314 if (Mid == V.end())
1315 return;
1316 for (auto I = Mid, E = V.end(); I != E; ++I) {
1317 SymbolBody *B = I->Symbol;
1318 size_t StrOff = I->StrTabOffset;
1319 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
1320 }
1321
1322 unsigned NBuckets = calcNBuckets(Symbols.size());
1323 std::stable_sort(Symbols.begin(), Symbols.end(),
1324 [&](const SymbolData &L, const SymbolData &R) {
1325 return L.Hash % NBuckets < R.Hash % NBuckets;
1326 });
1327
1328 V.erase(Mid, V.end());
1329 for (const SymbolData &Sym : Symbols)
1330 V.push_back({Sym.Body, Sym.STName});
1331}
1332
Eugene Leviantb96e8092016-11-18 09:06:47 +00001333template <class ELFT>
1334HashTableSection<ELFT>::HashTableSection()
1335 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_HASH, sizeof(Elf_Word), ".hash") {
1336 this->Entsize = sizeof(Elf_Word);
1337}
1338
1339template <class ELFT> void HashTableSection<ELFT>::finalize() {
1340 this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1341 this->OutSec->Entsize = this->Entsize;
1342
1343 unsigned NumEntries = 2; // nbucket and nchain.
1344 NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
1345
1346 // Create as many buckets as there are symbols.
1347 // FIXME: This is simplistic. We can try to optimize it, but implementing
1348 // support for SHT_GNU_HASH is probably even more profitable.
1349 NumEntries += In<ELFT>::DynSymTab->getNumSymbols();
1350 this->Size = NumEntries * sizeof(Elf_Word);
1351}
1352
1353template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1354 unsigned NumSymbols = In<ELFT>::DynSymTab->getNumSymbols();
1355 auto *P = reinterpret_cast<Elf_Word *>(Buf);
1356 *P++ = NumSymbols; // nbucket
1357 *P++ = NumSymbols; // nchain
1358
1359 Elf_Word *Buckets = P;
1360 Elf_Word *Chains = P + NumSymbols;
1361
1362 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
1363 SymbolBody *Body = S.Symbol;
1364 StringRef Name = Body->getName();
1365 unsigned I = Body->DynsymIndex;
1366 uint32_t Hash = hashSysV(Name) % NumSymbols;
1367 Chains[I] = Buckets[Hash];
1368 Buckets[Hash] = I;
1369 }
1370}
1371
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001372template <class ELFT>
1373PltSection<ELFT>::PltSection()
1374 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16,
1375 ".plt") {}
1376
1377template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
1378 // At beginning of PLT, we have code to call the dynamic linker
1379 // to resolve dynsyms at runtime. Write such code.
1380 Target->writePltHeader(Buf);
1381 size_t Off = Target->PltHeaderSize;
1382
1383 for (auto &I : Entries) {
1384 const SymbolBody *B = I.first;
1385 unsigned RelOff = I.second;
1386 uint64_t Got = B->getGotPltVA<ELFT>();
1387 uint64_t Plt = this->getVA() + Off;
1388 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
1389 Off += Target->PltEntrySize;
1390 }
1391}
1392
1393template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
1394 Sym.PltIndex = Entries.size();
1395 unsigned RelOff = In<ELFT>::RelaPlt->getRelocOffset();
1396 Entries.push_back(std::make_pair(&Sym, RelOff));
1397}
1398
1399template <class ELFT> size_t PltSection<ELFT>::getSize() const {
1400 return Target->PltHeaderSize + Entries.size() * Target->PltEntrySize;
1401}
1402
Eugene Levianta113a412016-11-21 09:24:43 +00001403template <class ELFT>
1404GdbIndexSection<ELFT>::GdbIndexSection()
1405 : SyntheticSection<ELFT>(0, SHT_PROGBITS, 1, ".gdb_index") {}
1406
1407template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() {
1408 std::vector<InputSection<ELFT> *> &IS =
1409 static_cast<OutputSection<ELFT> *>(Out<ELFT>::DebugInfo)->Sections;
1410
1411 for (InputSection<ELFT> *I : IS)
1412 readDwarf(I);
1413}
1414
1415template <class ELFT>
1416void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) {
1417 std::vector<std::pair<uintX_t, uintX_t>> CuList = readCuList(I);
1418 CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end());
1419}
1420
1421template <class ELFT> void GdbIndexSection<ELFT>::finalize() {
1422 parseDebugSections();
1423
1424 // GdbIndex header consist from version fields
1425 // and 5 more fields with different kinds of offsets.
1426 CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize;
1427}
1428
1429template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) {
1430 write32le(Buf, 7); // Write Version
1431 write32le(Buf + 4, CuListOffset); // CU list offset
1432 write32le(Buf + 8, CuTypesOffset); // Types CU list offset
1433 write32le(Buf + 12, CuTypesOffset); // Address area offset
1434 write32le(Buf + 16, CuTypesOffset); // Symbol table offset
1435 write32le(Buf + 20, CuTypesOffset); // Constant pool offset
1436 Buf += 24;
1437
1438 // Write the CU list.
1439 for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) {
1440 write64le(Buf, CU.first);
1441 write64le(Buf + 8, CU.second);
1442 Buf += 16;
1443 }
1444}
1445
George Rimar3fb5a6d2016-11-29 16:05:27 +00001446template <class ELFT> bool GdbIndexSection<ELFT>::empty() const {
1447 return !Out<ELFT>::DebugInfo;
1448}
1449
Eugene Leviant952eb4d2016-11-21 15:52:10 +00001450template <class ELFT>
1451EhFrameHeader<ELFT>::EhFrameHeader()
1452 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
1453
1454// .eh_frame_hdr contains a binary search table of pointers to FDEs.
1455// Each entry of the search table consists of two values,
1456// the starting PC from where FDEs covers, and the FDE's address.
1457// It is sorted by PC.
1458template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
1459 const endianness E = ELFT::TargetEndianness;
1460
1461 // Sort the FDE list by their PC and uniqueify. Usually there is only
1462 // one FDE for a PC (i.e. function), but if ICF merges two functions
1463 // into one, there can be more than one FDEs pointing to the address.
1464 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
1465 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
1466 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
1467 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
1468
1469 Buf[0] = 1;
1470 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1471 Buf[2] = DW_EH_PE_udata4;
1472 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
1473 write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->getVA() - 4);
1474 write32<E>(Buf + 8, Fdes.size());
1475 Buf += 12;
1476
1477 uintX_t VA = this->getVA();
1478 for (FdeData &Fde : Fdes) {
1479 write32<E>(Buf, Fde.Pc - VA);
1480 write32<E>(Buf + 4, Fde.FdeVA - VA);
1481 Buf += 8;
1482 }
1483}
1484
1485template <class ELFT> size_t EhFrameHeader<ELFT>::getSize() const {
1486 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
1487 return 12 + Out<ELFT>::EhFrame->NumFdes * 8;
1488}
1489
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001490template <class ELFT>
Rui Ueyamab38ddb12016-11-21 19:46:04 +00001491void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
1492 Fdes.push_back({Pc, FdeVA});
1493}
1494
George Rimar11992c862016-11-25 08:05:41 +00001495template <class ELFT> bool EhFrameHeader<ELFT>::empty() const {
1496 return Out<ELFT>::EhFrame->empty();
1497}
1498
Rui Ueyamab38ddb12016-11-21 19:46:04 +00001499template <class ELFT>
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001500VersionDefinitionSection<ELFT>::VersionDefinitionSection()
1501 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t),
1502 ".gnu.version_d") {}
1503
1504static StringRef getFileDefName() {
1505 if (!Config->SoName.empty())
1506 return Config->SoName;
1507 return Config->OutputFile;
1508}
1509
1510template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
1511 FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName());
1512 for (VersionDefinition &V : Config->VersionDefinitions)
1513 V.NameOff = In<ELFT>::DynStrTab->addString(V.Name);
1514
1515 this->OutSec->Link = this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
1516
1517 // sh_info should be set to the number of definitions. This fact is missed in
1518 // documentation, but confirmed by binutils community:
1519 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
1520 this->OutSec->Info = this->Info = getVerDefNum();
1521}
1522
1523template <class ELFT>
1524void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1525 StringRef Name, size_t NameOff) {
1526 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
1527 Verdef->vd_version = 1;
1528 Verdef->vd_cnt = 1;
1529 Verdef->vd_aux = sizeof(Elf_Verdef);
1530 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1531 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
1532 Verdef->vd_ndx = Index;
1533 Verdef->vd_hash = hashSysV(Name);
1534
1535 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
1536 Verdaux->vda_name = NameOff;
1537 Verdaux->vda_next = 0;
1538}
1539
1540template <class ELFT>
1541void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
1542 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
1543
1544 for (VersionDefinition &V : Config->VersionDefinitions) {
1545 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1546 writeOne(Buf, V.Id, V.Name, V.NameOff);
1547 }
1548
1549 // Need to terminate the last version definition.
1550 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
1551 Verdef->vd_next = 0;
1552}
1553
1554template <class ELFT> size_t VersionDefinitionSection<ELFT>::getSize() const {
1555 return (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
1556}
1557
1558template <class ELFT>
1559VersionTableSection<ELFT>::VersionTableSection()
1560 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t),
1561 ".gnu.version") {}
1562
1563template <class ELFT> void VersionTableSection<ELFT>::finalize() {
1564 this->OutSec->Entsize = this->Entsize = sizeof(Elf_Versym);
1565 // At the moment of june 2016 GNU docs does not mention that sh_link field
1566 // should be set, but Sun docs do. Also readelf relies on this field.
1567 this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1568}
1569
1570template <class ELFT> size_t VersionTableSection<ELFT>::getSize() const {
1571 return sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1);
1572}
1573
1574template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
1575 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
1576 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
1577 OutVersym->vs_index = S.Symbol->symbol()->VersionId;
1578 ++OutVersym;
1579 }
1580}
1581
George Rimar11992c862016-11-25 08:05:41 +00001582template <class ELFT> bool VersionTableSection<ELFT>::empty() const {
1583 return !In<ELFT>::VerDef && In<ELFT>::VerNeed->empty();
1584}
1585
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001586template <class ELFT>
1587VersionNeedSection<ELFT>::VersionNeedSection()
1588 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t),
1589 ".gnu.version_r") {
1590 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
1591 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
1592 // First identifiers are reserved by verdef section if it exist.
1593 NextIndex = getVerDefNum() + 1;
1594}
1595
1596template <class ELFT>
1597void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
1598 if (!SS->Verdef) {
1599 SS->symbol()->VersionId = VER_NDX_GLOBAL;
1600 return;
1601 }
1602 SharedFile<ELFT> *F = SS->file();
1603 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
1604 // to create one by adding it to our needed list and creating a dynstr entry
1605 // for the soname.
1606 if (F->VerdefMap.empty())
1607 Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())});
1608 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
1609 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
1610 // prepare to create one by allocating a version identifier and creating a
1611 // dynstr entry for the version name.
1612 if (NV.Index == 0) {
1613 NV.StrTab = In<ELFT>::DynStrTab->addString(
1614 SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
1615 NV.Index = NextIndex++;
1616 }
1617 SS->symbol()->VersionId = NV.Index;
1618}
1619
1620template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
1621 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
1622 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
1623 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
1624
1625 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
1626 // Create an Elf_Verneed for this DSO.
1627 Verneed->vn_version = 1;
1628 Verneed->vn_cnt = P.first->VerdefMap.size();
1629 Verneed->vn_file = P.second;
1630 Verneed->vn_aux =
1631 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
1632 Verneed->vn_next = sizeof(Elf_Verneed);
1633 ++Verneed;
1634
1635 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
1636 // VerdefMap, which will only contain references to needed version
1637 // definitions. Each Elf_Vernaux is based on the information contained in
1638 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
1639 // pointers, but is deterministic because the pointers refer to Elf_Verdef
1640 // data structures within a single input file.
1641 for (auto &NV : P.first->VerdefMap) {
1642 Vernaux->vna_hash = NV.first->vd_hash;
1643 Vernaux->vna_flags = 0;
1644 Vernaux->vna_other = NV.second.Index;
1645 Vernaux->vna_name = NV.second.StrTab;
1646 Vernaux->vna_next = sizeof(Elf_Vernaux);
1647 ++Vernaux;
1648 }
1649
1650 Vernaux[-1].vna_next = 0;
1651 }
1652 Verneed[-1].vn_next = 0;
1653}
1654
1655template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
1656 this->OutSec->Link = this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
1657 this->OutSec->Info = this->Info = Needed.size();
1658}
1659
1660template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
1661 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
1662 for (const std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
1663 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
1664 return Size;
1665}
1666
George Rimar11992c862016-11-25 08:05:41 +00001667template <class ELFT> bool VersionNeedSection<ELFT>::empty() const {
1668 return getNeedNum() == 0;
1669}
1670
Eugene Leviant17b7a572016-11-22 17:49:14 +00001671template <class ELFT>
Rui Ueyamabdfa1552016-11-22 19:24:52 +00001672MipsRldMapSection<ELFT>::MipsRldMapSection()
Eugene Leviant17b7a572016-11-22 17:49:14 +00001673 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
1674 sizeof(typename ELFT::uint), ".rld_map") {}
1675
Rui Ueyamabdfa1552016-11-22 19:24:52 +00001676template <class ELFT> void MipsRldMapSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviant17b7a572016-11-22 17:49:14 +00001677 // Apply filler from linker script.
1678 uint64_t Filler = Script<ELFT>::X->getFiller(this->Name);
1679 Filler = (Filler << 32) | Filler;
1680 memcpy(Buf, &Filler, getSize());
1681}
1682
Peter Smith719eb8e2016-11-24 11:43:55 +00001683template <class ELFT>
1684ARMExidxSentinelSection<ELFT>::ARMExidxSentinelSection()
1685 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX,
1686 sizeof(typename ELFT::uint), ".ARM.exidx") {}
1687
1688// Write a terminating sentinel entry to the end of the .ARM.exidx table.
1689// This section will have been sorted last in the .ARM.exidx table.
1690// This table entry will have the form:
1691// | PREL31 upper bound of code that has exception tables | EXIDX_CANTUNWIND |
1692template <class ELFT> void ARMExidxSentinelSection<ELFT>::writeTo(uint8_t *Buf){
1693 // Get the InputSection before us, we are by definition last
1694 auto RI = cast<OutputSection<ELFT>>(this->OutSec)->Sections.rbegin();
1695 InputSection<ELFT> *LE = *(++RI);
1696 InputSection<ELFT> *LC = cast<InputSection<ELFT>>(LE->getLinkOrderDep());
1697 uint64_t S = LC->OutSec->Addr + LC->getOffset(LC->getSize());
1698 uint64_t P = this->getVA();
1699 Target->relocateOne(Buf, R_ARM_PREL31, S - P);
1700 write32le(Buf + 4, 0x1);
1701}
1702
Rafael Espindola682a5bc2016-11-08 14:42:34 +00001703template InputSection<ELF32LE> *elf::createCommonSection();
1704template InputSection<ELF32BE> *elf::createCommonSection();
1705template InputSection<ELF64LE> *elf::createCommonSection();
1706template InputSection<ELF64BE> *elf::createCommonSection();
Rui Ueyamae8a61022016-11-05 23:05:47 +00001707
Rafael Espindolac0e47fb2016-11-08 14:56:27 +00001708template InputSection<ELF32LE> *elf::createInterpSection();
1709template InputSection<ELF32BE> *elf::createInterpSection();
1710template InputSection<ELF64LE> *elf::createInterpSection();
1711template InputSection<ELF64BE> *elf::createInterpSection();
Rui Ueyamae288eef2016-11-02 18:58:44 +00001712
Rui Ueyama3da3f062016-11-10 20:20:37 +00001713template MergeInputSection<ELF32LE> *elf::createCommentSection();
1714template MergeInputSection<ELF32BE> *elf::createCommentSection();
1715template MergeInputSection<ELF64LE> *elf::createCommentSection();
1716template MergeInputSection<ELF64BE> *elf::createCommentSection();
1717
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +00001718template class elf::MipsAbiFlagsSection<ELF32LE>;
1719template class elf::MipsAbiFlagsSection<ELF32BE>;
1720template class elf::MipsAbiFlagsSection<ELF64LE>;
1721template class elf::MipsAbiFlagsSection<ELF64BE>;
1722
Simon Atanasyance02cf02016-11-09 21:36:56 +00001723template class elf::MipsOptionsSection<ELF32LE>;
1724template class elf::MipsOptionsSection<ELF32BE>;
1725template class elf::MipsOptionsSection<ELF64LE>;
1726template class elf::MipsOptionsSection<ELF64BE>;
1727
1728template class elf::MipsReginfoSection<ELF32LE>;
1729template class elf::MipsReginfoSection<ELF32BE>;
1730template class elf::MipsReginfoSection<ELF64LE>;
1731template class elf::MipsReginfoSection<ELF64BE>;
1732
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +00001733template class elf::BuildIdSection<ELF32LE>;
1734template class elf::BuildIdSection<ELF32BE>;
1735template class elf::BuildIdSection<ELF64LE>;
1736template class elf::BuildIdSection<ELF64BE>;
1737
Eugene Leviantad4439e2016-11-11 11:33:32 +00001738template class elf::GotSection<ELF32LE>;
1739template class elf::GotSection<ELF32BE>;
1740template class elf::GotSection<ELF64LE>;
1741template class elf::GotSection<ELF64BE>;
1742
Simon Atanasyan725dc142016-11-16 21:01:02 +00001743template class elf::MipsGotSection<ELF32LE>;
1744template class elf::MipsGotSection<ELF32BE>;
1745template class elf::MipsGotSection<ELF64LE>;
1746template class elf::MipsGotSection<ELF64BE>;
1747
Eugene Leviant41ca3272016-11-10 09:48:29 +00001748template class elf::GotPltSection<ELF32LE>;
1749template class elf::GotPltSection<ELF32BE>;
1750template class elf::GotPltSection<ELF64LE>;
1751template class elf::GotPltSection<ELF64BE>;
Eugene Leviant22eb0262016-11-14 09:16:00 +00001752
1753template class elf::StringTableSection<ELF32LE>;
1754template class elf::StringTableSection<ELF32BE>;
1755template class elf::StringTableSection<ELF64LE>;
1756template class elf::StringTableSection<ELF64BE>;
Eugene Leviant6380ce22016-11-15 12:26:55 +00001757
1758template class elf::DynamicSection<ELF32LE>;
1759template class elf::DynamicSection<ELF32BE>;
1760template class elf::DynamicSection<ELF64LE>;
1761template class elf::DynamicSection<ELF64BE>;
Eugene Levianta96d9022016-11-16 10:02:27 +00001762
1763template class elf::RelocationSection<ELF32LE>;
1764template class elf::RelocationSection<ELF32BE>;
1765template class elf::RelocationSection<ELF64LE>;
1766template class elf::RelocationSection<ELF64BE>;
Eugene Leviant9230db92016-11-17 09:16:34 +00001767
1768template class elf::SymbolTableSection<ELF32LE>;
1769template class elf::SymbolTableSection<ELF32BE>;
1770template class elf::SymbolTableSection<ELF64LE>;
1771template class elf::SymbolTableSection<ELF64BE>;
Eugene Leviantbe809a72016-11-18 06:44:18 +00001772
1773template class elf::GnuHashTableSection<ELF32LE>;
1774template class elf::GnuHashTableSection<ELF32BE>;
1775template class elf::GnuHashTableSection<ELF64LE>;
1776template class elf::GnuHashTableSection<ELF64BE>;
Eugene Leviantb96e8092016-11-18 09:06:47 +00001777
1778template class elf::HashTableSection<ELF32LE>;
1779template class elf::HashTableSection<ELF32BE>;
1780template class elf::HashTableSection<ELF64LE>;
1781template class elf::HashTableSection<ELF64BE>;
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001782
1783template class elf::PltSection<ELF32LE>;
1784template class elf::PltSection<ELF32BE>;
1785template class elf::PltSection<ELF64LE>;
1786template class elf::PltSection<ELF64BE>;
Eugene Levianta113a412016-11-21 09:24:43 +00001787
1788template class elf::GdbIndexSection<ELF32LE>;
1789template class elf::GdbIndexSection<ELF32BE>;
1790template class elf::GdbIndexSection<ELF64LE>;
1791template class elf::GdbIndexSection<ELF64BE>;
Eugene Leviant952eb4d2016-11-21 15:52:10 +00001792
1793template class elf::EhFrameHeader<ELF32LE>;
1794template class elf::EhFrameHeader<ELF32BE>;
1795template class elf::EhFrameHeader<ELF64LE>;
1796template class elf::EhFrameHeader<ELF64BE>;
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001797
1798template class elf::VersionTableSection<ELF32LE>;
1799template class elf::VersionTableSection<ELF32BE>;
1800template class elf::VersionTableSection<ELF64LE>;
1801template class elf::VersionTableSection<ELF64BE>;
1802
1803template class elf::VersionNeedSection<ELF32LE>;
1804template class elf::VersionNeedSection<ELF32BE>;
1805template class elf::VersionNeedSection<ELF64LE>;
1806template class elf::VersionNeedSection<ELF64BE>;
1807
1808template class elf::VersionDefinitionSection<ELF32LE>;
1809template class elf::VersionDefinitionSection<ELF32BE>;
1810template class elf::VersionDefinitionSection<ELF64LE>;
1811template class elf::VersionDefinitionSection<ELF64BE>;
Eugene Leviant17b7a572016-11-22 17:49:14 +00001812
Rui Ueyamabdfa1552016-11-22 19:24:52 +00001813template class elf::MipsRldMapSection<ELF32LE>;
1814template class elf::MipsRldMapSection<ELF32BE>;
1815template class elf::MipsRldMapSection<ELF64LE>;
1816template class elf::MipsRldMapSection<ELF64BE>;
Peter Smith719eb8e2016-11-24 11:43:55 +00001817
1818template class elf::ARMExidxSentinelSection<ELF32LE>;
1819template class elf::ARMExidxSentinelSection<ELF32BE>;
1820template class elf::ARMExidxSentinelSection<ELF64LE>;
1821template class elf::ARMExidxSentinelSection<ELF64BE>;