blob: e563dd36bfe51486cf98cd5beb0bc1f66f443a60 [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"
Rui Ueyamae288eef2016-11-02 18:58:44 +000021#include "Memory.h"
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000022#include "OutputSections.h"
23#include "Strings.h"
Rui Ueyamae8a61022016-11-05 23:05:47 +000024#include "SymbolTable.h"
Simon Atanasyance02cf02016-11-09 21:36:56 +000025#include "Target.h"
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +000026#include "Writer.h"
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000027
Rui Ueyama3da3f062016-11-10 20:20:37 +000028#include "lld/Config/Version.h"
George Rimar364b59e22016-11-06 07:42:55 +000029#include "lld/Core/Parallel.h"
Eugene Leviant952eb4d2016-11-21 15:52:10 +000030#include "llvm/Support/Dwarf.h"
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000031#include "llvm/Support/Endian.h"
32#include "llvm/Support/MD5.h"
33#include "llvm/Support/RandomNumberGenerator.h"
34#include "llvm/Support/SHA1.h"
35#include "llvm/Support/xxhash.h"
Rui Ueyama3da3f062016-11-10 20:20:37 +000036#include <cstdlib>
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000037
38using namespace llvm;
Eugene Leviant952eb4d2016-11-21 15:52:10 +000039using namespace llvm::dwarf;
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000040using namespace llvm::ELF;
41using namespace llvm::object;
42using namespace llvm::support;
43using namespace llvm::support::endian;
44
45using namespace lld;
46using namespace lld::elf;
47
Rui Ueyamae8a61022016-11-05 23:05:47 +000048template <class ELFT> static std::vector<DefinedCommon *> getCommonSymbols() {
49 std::vector<DefinedCommon *> V;
50 for (Symbol *S : Symtab<ELFT>::X->getSymbols())
51 if (auto *B = dyn_cast<DefinedCommon>(S->body()))
52 V.push_back(B);
53 return V;
54}
55
56// Find all common symbols and allocate space for them.
Rafael Espindola682a5bc2016-11-08 14:42:34 +000057template <class ELFT> InputSection<ELFT> *elf::createCommonSection() {
58 auto *Ret = make<InputSection<ELFT>>(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, 1,
59 ArrayRef<uint8_t>(), "COMMON");
60 Ret->Live = true;
Rui Ueyamae8a61022016-11-05 23:05:47 +000061
62 // Sort the common symbols by alignment as an heuristic to pack them better.
63 std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>();
64 std::stable_sort(Syms.begin(), Syms.end(),
65 [](const DefinedCommon *A, const DefinedCommon *B) {
66 return A->Alignment > B->Alignment;
67 });
68
69 // Assign offsets to symbols.
70 size_t Size = 0;
71 size_t Alignment = 1;
72 for (DefinedCommon *Sym : Syms) {
Rui Ueyama1c786822016-11-05 23:14:54 +000073 Alignment = std::max<size_t>(Alignment, Sym->Alignment);
Rui Ueyamae8a61022016-11-05 23:05:47 +000074 Size = alignTo(Size, Sym->Alignment);
75
76 // Compute symbol offset relative to beginning of input section.
77 Sym->Offset = Size;
78 Size += Sym->Size;
79 }
Rafael Espindola682a5bc2016-11-08 14:42:34 +000080 Ret->Alignment = Alignment;
81 Ret->Data = makeArrayRef<uint8_t>(nullptr, Size);
82 return Ret;
Rui Ueyamae8a61022016-11-05 23:05:47 +000083}
84
Rui Ueyama3da3f062016-11-10 20:20:37 +000085// Returns an LLD version string.
86static ArrayRef<uint8_t> getVersion() {
87 // Check LLD_VERSION first for ease of testing.
88 // You can get consitent output by using the environment variable.
89 // This is only for testing.
90 StringRef S = getenv("LLD_VERSION");
91 if (S.empty())
92 S = Saver.save(Twine("Linker: ") + getLLDVersion());
93
94 // +1 to include the terminating '\0'.
95 return {(const uint8_t *)S.data(), S.size() + 1};
Davide Italianob69f38f2016-11-11 00:05:41 +000096}
Rui Ueyama3da3f062016-11-10 20:20:37 +000097
98// Creates a .comment section containing LLD version info.
99// With this feature, you can identify LLD-generated binaries easily
100// by "objdump -s -j .comment <file>".
101// The returned object is a mergeable string section.
102template <class ELFT> MergeInputSection<ELFT> *elf::createCommentSection() {
103 typename ELFT::Shdr Hdr = {};
104 Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
105 Hdr.sh_type = SHT_PROGBITS;
106 Hdr.sh_entsize = 1;
107 Hdr.sh_addralign = 1;
108
109 auto *Ret = make<MergeInputSection<ELFT>>(/*file=*/nullptr, &Hdr, ".comment");
110 Ret->Data = getVersion();
111 Ret->splitIntoPieces();
112 return Ret;
113}
114
Simon Atanasyance02cf02016-11-09 21:36:56 +0000115// Iterate over sections of the specified type. For each section call
116// provided function. After that "kill" the section by turning off
117// "Live" flag, so that they won't be included in the final output.
118template <class ELFT>
119static void iterateSectionContents(
120 uint32_t Type,
Simon Atanasyan93214b72016-11-09 21:46:42 +0000121 std::function<void(elf::ObjectFile<ELFT> *, ArrayRef<uint8_t>)> F) {
Simon Atanasyance02cf02016-11-09 21:36:56 +0000122 for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
123 if (Sec && Sec->Live && Sec->Type == Type) {
124 Sec->Live = false;
125 F(Sec->getFile(), Sec->Data);
126 }
127 }
128}
129
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000130// .MIPS.abiflags section.
131template <class ELFT>
132MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection()
133 : InputSection<ELFT>(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ArrayRef<uint8_t>(),
134 ".MIPS.abiflags") {
135 auto Func = [this](ObjectFile<ELFT> *F, ArrayRef<uint8_t> D) {
136 if (D.size() != sizeof(Elf_Mips_ABIFlags)) {
137 error(getFilename(F) + ": invalid size of .MIPS.abiflags section");
138 return;
139 }
140 auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(D.data());
141 if (S->version != 0) {
142 error(getFilename(F) + ": unexpected .MIPS.abiflags version " +
143 Twine(S->version));
144 return;
145 }
146 // LLD checks ISA compatibility in getMipsEFlags(). Here we just
147 // select the highest number of ISA/Rev/Ext.
148 Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
149 Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
150 Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
151 Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
152 Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
153 Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
154 Flags.ases |= S->ases;
155 Flags.flags1 |= S->flags1;
156 Flags.flags2 |= S->flags2;
157 Flags.fp_abi =
158 elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, getFilename(F));
159 };
160 iterateSectionContents<ELFT>(SHT_MIPS_ABIFLAGS, Func);
161
162 this->Data = ArrayRef<uint8_t>((const uint8_t *)&Flags, sizeof(Flags));
163 this->Live = true;
164}
165
Simon Atanasyance02cf02016-11-09 21:36:56 +0000166// .MIPS.options section.
167template <class ELFT>
168MipsOptionsSection<ELFT>::MipsOptionsSection()
169 : InputSection<ELFT>(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ArrayRef<uint8_t>(),
170 ".MIPS.options") {
171 Buf.resize(sizeof(Elf_Mips_Options) + sizeof(Elf_Mips_RegInfo));
172 getOptions()->kind = ODK_REGINFO;
173 getOptions()->size = Buf.size();
174 auto Func = [this](ObjectFile<ELFT> *F, ArrayRef<uint8_t> D) {
175 while (!D.empty()) {
176 if (D.size() < sizeof(Elf_Mips_Options)) {
177 error(getFilename(F) + ": invalid size of .MIPS.options section");
178 break;
179 }
180 auto *O = reinterpret_cast<const Elf_Mips_Options *>(D.data());
181 if (O->kind == ODK_REGINFO) {
182 if (Config->Relocatable && O->getRegInfo().ri_gp_value)
183 error(getFilename(F) + ": unsupported non-zero ri_gp_value");
184 getOptions()->getRegInfo().ri_gprmask |= O->getRegInfo().ri_gprmask;
185 F->MipsGp0 = O->getRegInfo().ri_gp_value;
186 break;
187 }
188 if (!O->size)
189 fatal(getFilename(F) + ": zero option descriptor size");
190 D = D.slice(O->size);
191 }
192 };
193 iterateSectionContents<ELFT>(SHT_MIPS_OPTIONS, Func);
194
195 this->Data = ArrayRef<uint8_t>(Buf);
196 // Section should be alive for N64 ABI only.
197 this->Live = ELFT::Is64Bits;
198}
199
200template <class ELFT> void MipsOptionsSection<ELFT>::finalize() {
201 if (!Config->Relocatable)
202 getOptions()->getRegInfo().ri_gp_value =
Simon Atanasyan725dc142016-11-16 21:01:02 +0000203 In<ELFT>::MipsGot->getVA() + MipsGPOffset;
Simon Atanasyance02cf02016-11-09 21:36:56 +0000204}
205
206// MIPS .reginfo section.
207template <class ELFT>
208MipsReginfoSection<ELFT>::MipsReginfoSection()
209 : InputSection<ELFT>(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ArrayRef<uint8_t>(),
210 ".reginfo") {
211 auto Func = [this](ObjectFile<ELFT> *F, ArrayRef<uint8_t> D) {
212 if (D.size() != sizeof(Elf_Mips_RegInfo)) {
213 error(getFilename(F) + ": invalid size of .reginfo section");
214 return;
215 }
216 auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(D.data());
217 if (Config->Relocatable && R->ri_gp_value)
218 error(getFilename(F) + ": unsupported non-zero ri_gp_value");
219 Reginfo.ri_gprmask |= R->ri_gprmask;
220 F->MipsGp0 = R->ri_gp_value;
221 };
222 iterateSectionContents<ELFT>(SHT_MIPS_REGINFO, Func);
223
224 this->Data = ArrayRef<uint8_t>((const uint8_t *)&Reginfo, sizeof(Reginfo));
225 // Section should be alive for O32 and N32 ABIs only.
226 this->Live = !ELFT::Is64Bits;
227}
228
229template <class ELFT> void MipsReginfoSection<ELFT>::finalize() {
230 if (!Config->Relocatable)
Simon Atanasyan725dc142016-11-16 21:01:02 +0000231 Reginfo.ri_gp_value = In<ELFT>::MipsGot->getVA() + MipsGPOffset;
Simon Atanasyance02cf02016-11-09 21:36:56 +0000232}
233
Rui Ueyamae288eef2016-11-02 18:58:44 +0000234static ArrayRef<uint8_t> createInterp() {
235 // StringSaver guarantees that the returned string ends with '\0'.
236 StringRef S = Saver.save(Config->DynamicLinker);
Rui Ueyama6294a242016-11-04 17:41:29 +0000237 return {(const uint8_t *)S.data(), S.size() + 1};
Rui Ueyamae288eef2016-11-02 18:58:44 +0000238}
239
Rafael Espindolac0e47fb2016-11-08 14:56:27 +0000240template <class ELFT> InputSection<ELFT> *elf::createInterpSection() {
241 auto *Ret = make<InputSection<ELFT>>(SHF_ALLOC, SHT_PROGBITS, 1,
242 createInterp(), ".interp");
243 Ret->Live = true;
244 return Ret;
Rui Ueyamaa9ee8d62016-11-04 22:25:39 +0000245}
Rui Ueyamae288eef2016-11-02 18:58:44 +0000246
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000247template <class ELFT>
Rui Ueyamac4030a12016-11-22 00:54:15 +0000248BuildIdSection<ELFT>::BuildIdSection()
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000249 : InputSection<ELFT>(SHF_ALLOC, SHT_NOTE, 1, ArrayRef<uint8_t>(),
George Rimar364b59e22016-11-06 07:42:55 +0000250 ".note.gnu.build-id"),
Rui Ueyamac4030a12016-11-22 00:54:15 +0000251 HashSize(getHashSize()) {
Rui Ueyamaa9ee8d62016-11-04 22:25:39 +0000252 this->Live = true;
253
Rafael Espindola0e876cf2016-11-10 15:41:34 +0000254 Buf.resize(HeaderSize + HashSize);
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000255 const endianness E = ELFT::TargetEndianness;
Rafael Espindola1a541122016-11-08 14:47:16 +0000256 write32<E>(Buf.data(), 4); // Name size
257 write32<E>(Buf.data() + 4, HashSize); // Content size
258 write32<E>(Buf.data() + 8, NT_GNU_BUILD_ID); // Type
259 memcpy(Buf.data() + 12, "GNU", 4); // Name string
260 this->Data = ArrayRef<uint8_t>(Buf);
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000261}
262
Rui Ueyama35e00752016-11-10 00:12:28 +0000263// Returns the location of the build-id hash value in the output.
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000264template <class ELFT>
Rui Ueyamac4030a12016-11-22 00:54:15 +0000265uint8_t *BuildIdSection<ELFT>::getOutputLoc(uint8_t *Start) {
Rafael Espindola0e876cf2016-11-10 15:41:34 +0000266 return Start + this->OutSec->Offset + this->OutSecOff + HeaderSize;
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000267}
268
Rui Ueyamac4030a12016-11-22 00:54:15 +0000269template <class ELFT> size_t BuildIdSection<ELFT>::getHashSize() {
270 switch (Config->BuildId) {
271 case BuildIdKind::Fast:
272 return 8;
273 case BuildIdKind::Md5:
274 case BuildIdKind::Uuid:
275 return 16;
276 case BuildIdKind::Sha1:
277 return 20;
278 case BuildIdKind::Hexstring:
279 return Config->BuildIdVector.size();
280 default:
281 llvm_unreachable("unknown BuildIdKind");
282 }
283}
284
Rui Ueyama35e00752016-11-10 00:12:28 +0000285// Split one uint8 array into small pieces of uint8 arrays.
George Rimar364b59e22016-11-06 07:42:55 +0000286static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr,
287 size_t ChunkSize) {
288 std::vector<ArrayRef<uint8_t>> Ret;
289 while (Arr.size() > ChunkSize) {
290 Ret.push_back(Arr.take_front(ChunkSize));
291 Arr = Arr.drop_front(ChunkSize);
292 }
293 if (!Arr.empty())
294 Ret.push_back(Arr);
295 return Ret;
296}
297
Rui Ueyama35e00752016-11-10 00:12:28 +0000298// Computes a hash value of Data using a given hash function.
299// In order to utilize multiple cores, we first split data into 1MB
300// chunks, compute a hash for each chunk, and then compute a hash value
301// of the hash values.
George Rimar364b59e22016-11-06 07:42:55 +0000302template <class ELFT>
303void BuildIdSection<ELFT>::computeHash(
George Rimar828787a2016-11-06 08:39:46 +0000304 llvm::MutableArrayRef<uint8_t> Data,
Rui Ueyama35e00752016-11-10 00:12:28 +0000305 std::function<void(ArrayRef<uint8_t> Arr, uint8_t *Dest)> HashFn) {
George Rimar364b59e22016-11-06 07:42:55 +0000306 std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024);
307 std::vector<uint8_t> HashList(Chunks.size() * HashSize);
308
Rui Ueyama35e00752016-11-10 00:12:28 +0000309 auto Fn = [&](ArrayRef<uint8_t> &Chunk) {
310 size_t Idx = &Chunk - Chunks.data();
311 HashFn(Chunk, HashList.data() + Idx * HashSize);
312 };
George Rimar364b59e22016-11-06 07:42:55 +0000313
Rui Ueyama35e00752016-11-10 00:12:28 +0000314 if (Config->Threads)
315 parallel_for_each(Chunks.begin(), Chunks.end(), Fn);
316 else
317 std::for_each(Chunks.begin(), Chunks.end(), Fn);
318
Rui Ueyamac4030a12016-11-22 00:54:15 +0000319 HashFn(HashList, getOutputLoc(Data.begin()));
George Rimar364b59e22016-11-06 07:42:55 +0000320}
321
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000322template <class ELFT>
Rui Ueyamac4030a12016-11-22 00:54:15 +0000323void BuildIdSection<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
324 switch (Config->BuildId) {
325 case BuildIdKind::Fast:
326 computeHash(Buf, [](ArrayRef<uint8_t> Arr, uint8_t *Dest) {
327 write64le(Dest, xxHash64(toStringRef(Arr)));
328 });
329 break;
330 case BuildIdKind::Md5:
331 computeHash(Buf, [](ArrayRef<uint8_t> Arr, uint8_t *Dest) {
332 MD5 Hash;
333 Hash.update(Arr);
334 MD5::MD5Result Res;
335 Hash.final(Res);
336 memcpy(Dest, Res, 16);
337 });
338 break;
339 case BuildIdKind::Sha1:
340 computeHash(Buf, [](ArrayRef<uint8_t> Arr, uint8_t *Dest) {
341 SHA1 Hash;
342 Hash.update(Arr);
343 memcpy(Dest, Hash.final().data(), 20);
344 });
345 break;
346 case BuildIdKind::Uuid:
347 if (getRandomBytes(getOutputLoc(Buf.data()), HashSize))
348 error("entropy source failure");
349 break;
350 case BuildIdKind::Hexstring:
351 memcpy(this->getOutputLoc(Buf.data()), Config->BuildIdVector.data(),
352 Config->BuildIdVector.size());
353 break;
354 default:
355 llvm_unreachable("unknown BuildIdKind");
356 }
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000357}
358
Eugene Leviant41ca3272016-11-10 09:48:29 +0000359template <class ELFT>
Eugene Leviantad4439e2016-11-11 11:33:32 +0000360GotSection<ELFT>::GotSection()
361 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
Simon Atanasyan725dc142016-11-16 21:01:02 +0000362 Target->GotEntrySize, ".got") {}
Eugene Leviantad4439e2016-11-11 11:33:32 +0000363
364template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
365 Sym.GotIndex = Entries.size();
366 Entries.push_back(&Sym);
367}
368
Simon Atanasyan725dc142016-11-16 21:01:02 +0000369template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
370 if (Sym.GlobalDynIndex != -1U)
371 return false;
372 Sym.GlobalDynIndex = Entries.size();
373 // Global Dynamic TLS entries take two GOT slots.
374 Entries.push_back(nullptr);
375 Entries.push_back(&Sym);
376 return true;
377}
378
379// Reserves TLS entries for a TLS module ID and a TLS block offset.
380// In total it takes two GOT slots.
381template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
382 if (TlsIndexOff != uint32_t(-1))
383 return false;
384 TlsIndexOff = Entries.size() * sizeof(uintX_t);
385 Entries.push_back(nullptr);
386 Entries.push_back(nullptr);
387 return true;
388}
389
Eugene Leviantad4439e2016-11-11 11:33:32 +0000390template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000391typename GotSection<ELFT>::uintX_t
392GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
393 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
394}
395
396template <class ELFT>
397typename GotSection<ELFT>::uintX_t
398GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
399 return B.GlobalDynIndex * sizeof(uintX_t);
400}
401
402template <class ELFT> void GotSection<ELFT>::finalize() {
403 Size = Entries.size() * sizeof(uintX_t);
404}
405
406template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
407 for (const SymbolBody *B : Entries) {
408 uint8_t *Entry = Buf;
409 Buf += sizeof(uintX_t);
410 if (!B)
411 continue;
412 if (B->isPreemptible())
413 continue; // The dynamic linker will take care of it.
414 uintX_t VA = B->getVA<ELFT>();
415 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Entry, VA);
416 }
417}
418
419template <class ELFT>
420MipsGotSection<ELFT>::MipsGotSection()
421 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL,
422 SHT_PROGBITS, Target->GotEntrySize, ".got") {}
423
424template <class ELFT>
425void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, uintX_t Addend,
Eugene Leviantad4439e2016-11-11 11:33:32 +0000426 RelExpr Expr) {
427 // For "true" local symbols which can be referenced from the same module
428 // only compiler creates two instructions for address loading:
429 //
430 // lw $8, 0($gp) # R_MIPS_GOT16
431 // addi $8, $8, 0 # R_MIPS_LO16
432 //
433 // The first instruction loads high 16 bits of the symbol address while
434 // the second adds an offset. That allows to reduce number of required
435 // GOT entries because only one global offset table entry is necessary
436 // for every 64 KBytes of local data. So for local symbols we need to
437 // allocate number of GOT entries to hold all required "page" addresses.
438 //
439 // All global symbols (hidden and regular) considered by compiler uniformly.
440 // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
441 // to load address of the symbol. So for each such symbol we need to
442 // allocate dedicated GOT entry to store its address.
443 //
444 // If a symbol is preemptible we need help of dynamic linker to get its
445 // final address. The corresponding GOT entries are allocated in the
446 // "global" part of GOT. Entries for non preemptible global symbol allocated
447 // in the "local" part of GOT.
448 //
449 // See "Global Offset Table" in Chapter 5:
450 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
451 if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
452 // At this point we do not know final symbol value so to reduce number
453 // of allocated GOT entries do the following trick. Save all output
454 // sections referenced by GOT relocations. Then later in the `finalize`
455 // method calculate number of "pages" required to cover all saved output
456 // section and allocate appropriate number of GOT entries.
457 auto *OutSec = cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec;
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000458 OutSections.insert(OutSec);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000459 return;
460 }
461 if (Sym.isTls()) {
462 // GOT entries created for MIPS TLS relocations behave like
463 // almost GOT entries from other ABIs. They go to the end
464 // of the global offset table.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000465 Sym.GotIndex = TlsEntries.size();
466 TlsEntries.push_back(&Sym);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000467 return;
468 }
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000469 auto AddEntry = [&](SymbolBody &S, uintX_t A, GotEntries &Items) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000470 if (S.isInGot() && !A)
471 return;
472 size_t NewIndex = Items.size();
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000473 if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second)
Eugene Leviantad4439e2016-11-11 11:33:32 +0000474 return;
475 Items.emplace_back(&S, A);
476 if (!A)
477 S.GotIndex = NewIndex;
478 };
479 if (Sym.isPreemptible()) {
480 // Ignore addends for preemptible symbols. They got single GOT entry anyway.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000481 AddEntry(Sym, 0, GlobalEntries);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000482 Sym.IsInGlobalMipsGot = true;
483 } else if (Expr == R_MIPS_GOT_OFF32) {
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000484 AddEntry(Sym, Addend, LocalEntries32);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000485 Sym.Is32BitMipsGot = true;
486 } else {
487 // Hold local GOT entries accessed via a 16-bit index separately.
488 // That allows to write them in the beginning of the GOT and keep
489 // their indexes as less as possible to escape relocation's overflow.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000490 AddEntry(Sym, Addend, LocalEntries);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000491 }
492}
493
Simon Atanasyan725dc142016-11-16 21:01:02 +0000494template <class ELFT> bool MipsGotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000495 if (Sym.GlobalDynIndex != -1U)
496 return false;
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000497 Sym.GlobalDynIndex = TlsEntries.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000498 // Global Dynamic TLS entries take two GOT slots.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000499 TlsEntries.push_back(nullptr);
500 TlsEntries.push_back(&Sym);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000501 return true;
502}
503
504// Reserves TLS entries for a TLS module ID and a TLS block offset.
505// In total it takes two GOT slots.
Simon Atanasyan725dc142016-11-16 21:01:02 +0000506template <class ELFT> bool MipsGotSection<ELFT>::addTlsIndex() {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000507 if (TlsIndexOff != uint32_t(-1))
508 return false;
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000509 TlsIndexOff = TlsEntries.size() * sizeof(uintX_t);
510 TlsEntries.push_back(nullptr);
511 TlsEntries.push_back(nullptr);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000512 return true;
513}
514
515template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000516typename MipsGotSection<ELFT>::uintX_t
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000517MipsGotSection<ELFT>::getPageEntryOffset(uintX_t EntryValue) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000518 // Initialize the entry by the %hi(EntryValue) expression
519 // but without right-shifting.
520 EntryValue = (EntryValue + 0x8000) & ~0xffff;
521 // Take into account MIPS GOT header.
Simon Atanasyan725dc142016-11-16 21:01:02 +0000522 // See comment in the MipsGotSection::writeTo.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000523 size_t NewIndex = PageIndexMap.size() + 2;
524 auto P = PageIndexMap.insert(std::make_pair(EntryValue, NewIndex));
525 assert(!P.second || PageIndexMap.size() <= PageEntriesNum);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000526 return (uintX_t)P.first->second * sizeof(uintX_t) - MipsGPOffset;
527}
528
529template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000530typename MipsGotSection<ELFT>::uintX_t
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000531MipsGotSection<ELFT>::getBodyEntryOffset(const SymbolBody &B,
532 uintX_t Addend) const {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000533 // Calculate offset of the GOT entries block: TLS, global, local.
534 uintX_t GotBlockOff;
535 if (B.isTls())
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000536 GotBlockOff = getTlsOffset();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000537 else if (B.IsInGlobalMipsGot)
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000538 GotBlockOff = getLocalEntriesNum() * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000539 else if (B.Is32BitMipsGot)
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000540 GotBlockOff = (PageEntriesNum + LocalEntries.size()) * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000541 else
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000542 GotBlockOff = PageEntriesNum * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000543 // Calculate index of the GOT entry in the block.
544 uintX_t GotIndex;
545 if (B.isInGot())
546 GotIndex = B.GotIndex;
547 else {
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000548 auto It = EntryIndexMap.find({&B, Addend});
549 assert(It != EntryIndexMap.end());
Eugene Leviantad4439e2016-11-11 11:33:32 +0000550 GotIndex = It->second;
551 }
552 return GotBlockOff + GotIndex * sizeof(uintX_t) - MipsGPOffset;
553}
554
555template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000556typename MipsGotSection<ELFT>::uintX_t
557MipsGotSection<ELFT>::getTlsOffset() const {
558 return (getLocalEntriesNum() + GlobalEntries.size()) * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000559}
560
561template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000562typename MipsGotSection<ELFT>::uintX_t
563MipsGotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000564 return B.GlobalDynIndex * sizeof(uintX_t);
565}
566
567template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000568const SymbolBody *MipsGotSection<ELFT>::getFirstGlobalEntry() const {
569 return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000570}
571
572template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000573unsigned MipsGotSection<ELFT>::getLocalEntriesNum() const {
574 return PageEntriesNum + LocalEntries.size() + LocalEntries32.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000575}
576
Simon Atanasyan725dc142016-11-16 21:01:02 +0000577template <class ELFT> void MipsGotSection<ELFT>::finalize() {
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000578 size_t EntriesNum = TlsEntries.size();
Simon Atanasyan725dc142016-11-16 21:01:02 +0000579 // Take into account MIPS GOT header.
580 // See comment in the MipsGotSection::writeTo.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000581 PageEntriesNum += 2;
582 for (const OutputSectionBase *OutSec : OutSections) {
Simon Atanasyan725dc142016-11-16 21:01:02 +0000583 // Calculate an upper bound of MIPS GOT entries required to store page
584 // addresses of local symbols. We assume the worst case - each 64kb
585 // page of the output section has at least one GOT relocation against it.
586 // Add 0x8000 to the section's size because the page address stored
587 // in the GOT entry is calculated as (value + 0x8000) & ~0xffff.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000588 PageEntriesNum += (OutSec->Size + 0x8000 + 0xfffe) / 0xffff;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000589 }
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000590 EntriesNum += getLocalEntriesNum() + GlobalEntries.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000591 Size = EntriesNum * sizeof(uintX_t);
592}
593
594template <class ELFT>
595static void writeUint(uint8_t *Buf, typename ELFT::uint Val) {
596 typedef typename ELFT::uint uintX_t;
597 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val);
598}
599
Simon Atanasyan725dc142016-11-16 21:01:02 +0000600template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000601 // Set the MSB of the second GOT slot. This is not required by any
602 // MIPS ABI documentation, though.
603 //
604 // There is a comment in glibc saying that "The MSB of got[1] of a
605 // gnu object is set to identify gnu objects," and in GNU gold it
606 // says "the second entry will be used by some runtime loaders".
607 // But how this field is being used is unclear.
608 //
609 // We are not really willing to mimic other linkers behaviors
610 // without understanding why they do that, but because all files
611 // generated by GNU tools have this special GOT value, and because
612 // we've been doing this for years, it is probably a safe bet to
613 // keep doing this for now. We really need to revisit this to see
614 // if we had to do this.
615 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
616 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
617 // Write 'page address' entries to the local part of the GOT.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000618 for (std::pair<uintX_t, size_t> &L : PageIndexMap) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000619 uint8_t *Entry = Buf + L.second * sizeof(uintX_t);
620 writeUint<ELFT>(Entry, L.first);
621 }
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000622 Buf += PageEntriesNum * sizeof(uintX_t);
623 auto AddEntry = [&](const GotEntry &SA) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000624 uint8_t *Entry = Buf;
625 Buf += sizeof(uintX_t);
626 const SymbolBody *Body = SA.first;
627 uintX_t VA = Body->template getVA<ELFT>(SA.second);
628 writeUint<ELFT>(Entry, VA);
629 };
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000630 std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry);
631 std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry);
632 std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000633 // Initialize TLS-related GOT entries. If the entry has a corresponding
634 // dynamic relocations, leave it initialized by zero. Write down adjusted
635 // TLS symbol's values otherwise. To calculate the adjustments use offsets
636 // for thread-local storage.
637 // https://www.linux-mips.org/wiki/NPTL
638 if (TlsIndexOff != -1U && !Config->Pic)
639 writeUint<ELFT>(Buf + TlsIndexOff, 1);
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000640 for (const SymbolBody *B : TlsEntries) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000641 if (!B || B->isPreemptible())
642 continue;
643 uintX_t VA = B->getVA<ELFT>();
644 if (B->GotIndex != -1U) {
645 uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t);
646 writeUint<ELFT>(Entry, VA - 0x7000);
647 }
648 if (B->GlobalDynIndex != -1U) {
649 uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t);
650 writeUint<ELFT>(Entry, 1);
651 Entry += sizeof(uintX_t);
652 writeUint<ELFT>(Entry, VA - 0x8000);
653 }
654 }
655}
656
Eugene Leviantad4439e2016-11-11 11:33:32 +0000657template <class ELFT>
Eugene Leviant41ca3272016-11-10 09:48:29 +0000658GotPltSection<ELFT>::GotPltSection()
659 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
George Rimard8b27762016-11-14 10:14:18 +0000660 Target->GotPltEntrySize, ".got.plt") {}
Eugene Leviant41ca3272016-11-10 09:48:29 +0000661
662template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
663 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
664 Entries.push_back(&Sym);
665}
666
667template <class ELFT> bool GotPltSection<ELFT>::empty() const {
668 return Entries.empty();
669}
670
671template <class ELFT> size_t GotPltSection<ELFT>::getSize() const {
672 return (Target->GotPltHeaderEntriesNum + Entries.size()) *
673 Target->GotPltEntrySize;
674}
675
676template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
677 Target->writeGotPltHeader(Buf);
678 Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
679 for (const SymbolBody *B : Entries) {
680 Target->writeGotPlt(Buf, *B);
681 Buf += sizeof(uintX_t);
682 }
683}
684
Eugene Leviant22eb0262016-11-14 09:16:00 +0000685template <class ELFT>
686StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
687 : SyntheticSection<ELFT>(Dynamic ? (uintX_t)SHF_ALLOC : 0, SHT_STRTAB, 1,
688 Name),
689 Dynamic(Dynamic) {}
690
691// Adds a string to the string table. If HashIt is true we hash and check for
692// duplicates. It is optional because the name of global symbols are already
693// uniqued and hashing them again has a big cost for a small value: uniquing
694// them with some other string that happens to be the same.
695template <class ELFT>
696unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
697 if (HashIt) {
698 auto R = StringMap.insert(std::make_pair(S, this->Size));
699 if (!R.second)
700 return R.first->second;
701 }
702 unsigned Ret = this->Size;
703 this->Size = this->Size + S.size() + 1;
704 Strings.push_back(S);
705 return Ret;
706}
707
708template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
709 // ELF string tables start with NUL byte, so advance the pointer by one.
710 ++Buf;
711 for (StringRef S : Strings) {
712 memcpy(Buf, S.data(), S.size());
713 Buf += S.size() + 1;
714 }
715}
716
Eugene Leviante9bab5d2016-11-21 16:59:33 +0000717// Returns the number of version definition entries. Because the first entry
718// is for the version definition itself, it is the number of versioned symbols
719// plus one. Note that we don't support multiple versions yet.
Eugene Leviant6380ce22016-11-15 12:26:55 +0000720static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
721
722template <class ELFT>
723DynamicSection<ELFT>::DynamicSection()
724 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC,
725 sizeof(uintX_t), ".dynamic") {
726 this->Entsize = ELFT::Is64Bits ? 16 : 8;
727 // .dynamic section is not writable on MIPS.
728 // See "Special Section" in Chapter 4 in the following document:
729 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
730 if (Config->EMachine == EM_MIPS)
731 this->Flags = SHF_ALLOC;
732
733 addEntries();
734}
735
736// There are some dynamic entries that don't depend on other sections.
737// Such entries can be set early.
738template <class ELFT> void DynamicSection<ELFT>::addEntries() {
739 // Add strings to .dynstr early so that .dynstr's size will be
740 // fixed early.
741 for (StringRef S : Config->AuxiliaryList)
Rui Ueyama729ac792016-11-17 04:10:09 +0000742 add({DT_AUXILIARY, In<ELFT>::DynStrTab->addString(S)});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000743 if (!Config->RPath.empty())
Rui Ueyama729ac792016-11-17 04:10:09 +0000744 add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Eugene Leviant6380ce22016-11-15 12:26:55 +0000745 In<ELFT>::DynStrTab->addString(Config->RPath)});
746 for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
747 if (F->isNeeded())
Rui Ueyama729ac792016-11-17 04:10:09 +0000748 add({DT_NEEDED, In<ELFT>::DynStrTab->addString(F->getSoName())});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000749 if (!Config->SoName.empty())
Rui Ueyama729ac792016-11-17 04:10:09 +0000750 add({DT_SONAME, In<ELFT>::DynStrTab->addString(Config->SoName)});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000751
752 // Set DT_FLAGS and DT_FLAGS_1.
753 uint32_t DtFlags = 0;
754 uint32_t DtFlags1 = 0;
755 if (Config->Bsymbolic)
756 DtFlags |= DF_SYMBOLIC;
757 if (Config->ZNodelete)
758 DtFlags1 |= DF_1_NODELETE;
759 if (Config->ZNow) {
760 DtFlags |= DF_BIND_NOW;
761 DtFlags1 |= DF_1_NOW;
762 }
763 if (Config->ZOrigin) {
764 DtFlags |= DF_ORIGIN;
765 DtFlags1 |= DF_1_ORIGIN;
766 }
767
768 if (DtFlags)
Rui Ueyama729ac792016-11-17 04:10:09 +0000769 add({DT_FLAGS, DtFlags});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000770 if (DtFlags1)
Rui Ueyama729ac792016-11-17 04:10:09 +0000771 add({DT_FLAGS_1, DtFlags1});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000772
773 if (!Config->Entry.empty())
Rui Ueyama729ac792016-11-17 04:10:09 +0000774 add({DT_DEBUG, (uint64_t)0});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000775}
776
777// Add remaining entries to complete .dynamic contents.
778template <class ELFT> void DynamicSection<ELFT>::finalize() {
779 if (this->Size)
780 return; // Already finalized.
781
782 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
783
Eugene Levianta96d9022016-11-16 10:02:27 +0000784 if (In<ELFT>::RelaDyn->hasRelocs()) {
Eugene Leviant6380ce22016-11-15 12:26:55 +0000785 bool IsRela = Config->Rela;
Rui Ueyama729ac792016-11-17 04:10:09 +0000786 add({IsRela ? DT_RELA : DT_REL, In<ELFT>::RelaDyn});
787 add({IsRela ? DT_RELASZ : DT_RELSZ, In<ELFT>::RelaDyn->getSize()});
788 add({IsRela ? DT_RELAENT : DT_RELENT,
Eugene Leviant6380ce22016-11-15 12:26:55 +0000789 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
790
791 // MIPS dynamic loader does not support RELCOUNT tag.
792 // The problem is in the tight relation between dynamic
793 // relocations and GOT. So do not emit this tag on MIPS.
794 if (Config->EMachine != EM_MIPS) {
Eugene Levianta96d9022016-11-16 10:02:27 +0000795 size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
Eugene Leviant6380ce22016-11-15 12:26:55 +0000796 if (Config->ZCombreloc && NumRelativeRels)
Rui Ueyama729ac792016-11-17 04:10:09 +0000797 add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000798 }
799 }
Eugene Levianta96d9022016-11-16 10:02:27 +0000800 if (In<ELFT>::RelaPlt->hasRelocs()) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000801 add({DT_JMPREL, In<ELFT>::RelaPlt});
802 add({DT_PLTRELSZ, In<ELFT>::RelaPlt->getSize()});
803 add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
Eugene Leviant6380ce22016-11-15 12:26:55 +0000804 In<ELFT>::GotPlt});
Rui Ueyama729ac792016-11-17 04:10:09 +0000805 add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000806 }
807
Eugene Leviant9230db92016-11-17 09:16:34 +0000808 add({DT_SYMTAB, In<ELFT>::DynSymTab});
Rui Ueyama729ac792016-11-17 04:10:09 +0000809 add({DT_SYMENT, sizeof(Elf_Sym)});
810 add({DT_STRTAB, In<ELFT>::DynStrTab});
811 add({DT_STRSZ, In<ELFT>::DynStrTab->getSize()});
Eugene Leviantbe809a72016-11-18 06:44:18 +0000812 if (In<ELFT>::GnuHashTab)
813 add({DT_GNU_HASH, In<ELFT>::GnuHashTab});
Eugene Leviantb96e8092016-11-18 09:06:47 +0000814 if (In<ELFT>::HashTab)
815 add({DT_HASH, In<ELFT>::HashTab});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000816
817 if (Out<ELFT>::PreinitArray) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000818 add({DT_PREINIT_ARRAY, Out<ELFT>::PreinitArray});
819 add({DT_PREINIT_ARRAYSZ, Out<ELFT>::PreinitArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000820 }
821 if (Out<ELFT>::InitArray) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000822 add({DT_INIT_ARRAY, Out<ELFT>::InitArray});
823 add({DT_INIT_ARRAYSZ, Out<ELFT>::InitArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000824 }
825 if (Out<ELFT>::FiniArray) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000826 add({DT_FINI_ARRAY, Out<ELFT>::FiniArray});
827 add({DT_FINI_ARRAYSZ, Out<ELFT>::FiniArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000828 }
829
830 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Init))
Rui Ueyama729ac792016-11-17 04:10:09 +0000831 add({DT_INIT, B});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000832 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Fini))
Rui Ueyama729ac792016-11-17 04:10:09 +0000833 add({DT_FINI, B});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000834
Eugene Leviante9bab5d2016-11-21 16:59:33 +0000835 bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
836 if (HasVerNeed || In<ELFT>::VerDef)
837 add({DT_VERSYM, In<ELFT>::VerSym});
838 if (In<ELFT>::VerDef) {
839 add({DT_VERDEF, In<ELFT>::VerDef});
Rui Ueyama729ac792016-11-17 04:10:09 +0000840 add({DT_VERDEFNUM, getVerDefNum()});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000841 }
842 if (HasVerNeed) {
Eugene Leviante9bab5d2016-11-21 16:59:33 +0000843 add({DT_VERNEED, In<ELFT>::VerNeed});
844 add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000845 }
846
847 if (Config->EMachine == EM_MIPS) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000848 add({DT_MIPS_RLD_VERSION, 1});
849 add({DT_MIPS_FLAGS, RHF_NOTPOT});
850 add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
Eugene Leviant9230db92016-11-17 09:16:34 +0000851 add({DT_MIPS_SYMTABNO, In<ELFT>::DynSymTab->getNumSymbols()});
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000852 add({DT_MIPS_LOCAL_GOTNO, In<ELFT>::MipsGot->getLocalEntriesNum()});
853 if (const SymbolBody *B = In<ELFT>::MipsGot->getFirstGlobalEntry())
Rui Ueyama729ac792016-11-17 04:10:09 +0000854 add({DT_MIPS_GOTSYM, B->DynsymIndex});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000855 else
Eugene Leviant9230db92016-11-17 09:16:34 +0000856 add({DT_MIPS_GOTSYM, In<ELFT>::DynSymTab->getNumSymbols()});
Rui Ueyama729ac792016-11-17 04:10:09 +0000857 add({DT_PLTGOT, In<ELFT>::MipsGot});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000858 if (Out<ELFT>::MipsRldMap)
Rui Ueyama729ac792016-11-17 04:10:09 +0000859 add({DT_MIPS_RLD_MAP, Out<ELFT>::MipsRldMap});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000860 }
861
862 this->OutSec->Entsize = this->Entsize;
863 this->OutSec->Link = this->Link;
864
865 // +1 for DT_NULL
866 this->Size = (Entries.size() + 1) * this->Entsize;
867}
868
869template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
870 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
871
872 for (const Entry &E : Entries) {
873 P->d_tag = E.Tag;
874 switch (E.Kind) {
875 case Entry::SecAddr:
876 P->d_un.d_ptr = E.OutSec->Addr;
877 break;
878 case Entry::InSecAddr:
879 P->d_un.d_ptr = E.InSec->OutSec->Addr + E.InSec->OutSecOff;
880 break;
881 case Entry::SecSize:
882 P->d_un.d_val = E.OutSec->Size;
883 break;
884 case Entry::SymAddr:
885 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
886 break;
887 case Entry::PlainInt:
888 P->d_un.d_val = E.Val;
889 break;
890 }
891 ++P;
892 }
893}
894
Eugene Levianta96d9022016-11-16 10:02:27 +0000895template <class ELFT>
896typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
897 if (OutputSec)
898 return OutputSec->Addr + OffsetInSec;
899 return InputSec->OutSec->Addr + InputSec->getOffset(OffsetInSec);
900}
901
902template <class ELFT>
903typename ELFT::uint DynamicReloc<ELFT>::getAddend() const {
904 if (UseSymVA)
905 return Sym->getVA<ELFT>(Addend);
906 return Addend;
907}
908
909template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const {
910 if (Sym && !UseSymVA)
911 return Sym->DynsymIndex;
912 return 0;
913}
914
915template <class ELFT>
916RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
917 : SyntheticSection<ELFT>(SHF_ALLOC, Config->Rela ? SHT_RELA : SHT_REL,
918 sizeof(uintX_t), Name),
919 Sort(Sort) {
920 this->Entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
921}
922
923template <class ELFT>
924void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
925 if (Reloc.Type == Target->RelativeRel)
926 ++NumRelativeRelocs;
927 Relocs.push_back(Reloc);
928}
929
930template <class ELFT, class RelTy>
931static bool compRelocations(const RelTy &A, const RelTy &B) {
932 bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel;
933 bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel;
934 if (AIsRel != BIsRel)
935 return AIsRel;
936
937 return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL);
938}
939
940template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
941 uint8_t *BufBegin = Buf;
942 for (const DynamicReloc<ELFT> &Rel : Relocs) {
943 auto *P = reinterpret_cast<Elf_Rela *>(Buf);
944 Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
945
946 if (Config->Rela)
947 P->r_addend = Rel.getAddend();
948 P->r_offset = Rel.getOffset();
Simon Atanasyan725dc142016-11-16 21:01:02 +0000949 if (Config->EMachine == EM_MIPS && Rel.getInputSec() == In<ELFT>::MipsGot)
Eugene Levianta96d9022016-11-16 10:02:27 +0000950 // Dynamic relocation against MIPS GOT section make deal TLS entries
951 // allocated in the end of the GOT. We need to adjust the offset to take
952 // in account 'local' and 'global' GOT entries.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000953 P->r_offset += In<ELFT>::MipsGot->getTlsOffset();
Eugene Levianta96d9022016-11-16 10:02:27 +0000954 P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL);
955 }
956
957 if (Sort) {
958 if (Config->Rela)
959 std::stable_sort((Elf_Rela *)BufBegin,
960 (Elf_Rela *)BufBegin + Relocs.size(),
961 compRelocations<ELFT, Elf_Rela>);
962 else
963 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
964 compRelocations<ELFT, Elf_Rel>);
965 }
966}
967
968template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
969 return this->Entsize * Relocs.size();
970}
971
972template <class ELFT> void RelocationSection<ELFT>::finalize() {
Eugene Leviant9230db92016-11-17 09:16:34 +0000973 this->Link = In<ELFT>::DynSymTab ? In<ELFT>::DynSymTab->OutSec->SectionIndex
974 : In<ELFT>::SymTab->OutSec->SectionIndex;
Eugene Levianta96d9022016-11-16 10:02:27 +0000975
976 // Set required output section properties.
977 this->OutSec->Link = this->Link;
978 this->OutSec->Entsize = this->Entsize;
979}
980
Eugene Leviant9230db92016-11-17 09:16:34 +0000981template <class ELFT>
982SymbolTableSection<ELFT>::SymbolTableSection(
983 StringTableSection<ELFT> &StrTabSec)
984 : SyntheticSection<ELFT>(StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0,
985 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
986 sizeof(uintX_t),
987 StrTabSec.isDynamic() ? ".dynsym" : ".symtab"),
988 StrTabSec(StrTabSec) {
989 this->Entsize = sizeof(Elf_Sym);
990}
991
992// Orders symbols according to their positions in the GOT,
993// in compliance with MIPS ABI rules.
994// See "Global Offset Table" in Chapter 5 in the following document
995// for detailed description:
996// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
997static bool sortMipsSymbols(const SymbolBody *L, const SymbolBody *R) {
998 // Sort entries related to non-local preemptible symbols by GOT indexes.
999 // All other entries go to the first part of GOT in arbitrary order.
1000 bool LIsInLocalGot = !L->IsInGlobalMipsGot;
1001 bool RIsInLocalGot = !R->IsInGlobalMipsGot;
1002 if (LIsInLocalGot || RIsInLocalGot)
1003 return !RIsInLocalGot;
1004 return L->GotIndex < R->GotIndex;
1005}
1006
1007static uint8_t getSymbolBinding(SymbolBody *Body) {
1008 Symbol *S = Body->symbol();
1009 if (Config->Relocatable)
1010 return S->Binding;
1011 uint8_t Visibility = S->Visibility;
1012 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
1013 return STB_LOCAL;
1014 if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE)
1015 return STB_GLOBAL;
1016 return S->Binding;
1017}
1018
1019template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
1020 this->OutSec->Link = this->Link = StrTabSec.OutSec->SectionIndex;
1021 this->OutSec->Info = this->Info = NumLocals + 1;
1022 this->OutSec->Entsize = this->Entsize;
1023
1024 if (Config->Relocatable) {
1025 size_t I = NumLocals;
1026 for (const SymbolTableEntry &S : Symbols)
1027 S.Symbol->DynsymIndex = ++I;
1028 return;
1029 }
1030
1031 if (!StrTabSec.isDynamic()) {
1032 std::stable_sort(Symbols.begin(), Symbols.end(),
1033 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1034 return getSymbolBinding(L.Symbol) == STB_LOCAL &&
1035 getSymbolBinding(R.Symbol) != STB_LOCAL;
1036 });
1037 return;
1038 }
Eugene Leviantbe809a72016-11-18 06:44:18 +00001039 if (In<ELFT>::GnuHashTab)
Eugene Leviant9230db92016-11-17 09:16:34 +00001040 // NB: It also sorts Symbols to meet the GNU hash table requirements.
Eugene Leviantbe809a72016-11-18 06:44:18 +00001041 In<ELFT>::GnuHashTab->addSymbols(Symbols);
Eugene Leviant9230db92016-11-17 09:16:34 +00001042 else if (Config->EMachine == EM_MIPS)
1043 std::stable_sort(Symbols.begin(), Symbols.end(),
1044 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1045 return sortMipsSymbols(L.Symbol, R.Symbol);
1046 });
1047 size_t I = 0;
1048 for (const SymbolTableEntry &S : Symbols)
1049 S.Symbol->DynsymIndex = ++I;
1050}
1051
1052template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
1053 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
1054}
1055
1056template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1057 Buf += sizeof(Elf_Sym);
1058
1059 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1060 // .dynsym only contains global symbols.
1061 if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic())
1062 writeLocalSymbols(Buf);
1063
1064 writeGlobalSymbols(Buf);
1065}
1066
1067template <class ELFT>
1068void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1069 // Iterate over all input object files to copy their local symbols
1070 // to the output symbol table pointed by Buf.
1071 for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
1072 for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P :
1073 File->KeptLocalSyms) {
1074 const DefinedRegular<ELFT> &Body = *P.first;
1075 InputSectionBase<ELFT> *Section = Body.Section;
1076 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1077
1078 if (!Section) {
1079 ESym->st_shndx = SHN_ABS;
1080 ESym->st_value = Body.Value;
1081 } else {
1082 const OutputSectionBase *OutSec = Section->OutSec;
1083 ESym->st_shndx = OutSec->SectionIndex;
1084 ESym->st_value = OutSec->Addr + Section->getOffset(Body);
1085 }
1086 ESym->st_name = P.second;
1087 ESym->st_size = Body.template getSize<ELFT>();
1088 ESym->setBindingAndType(STB_LOCAL, Body.Type);
1089 Buf += sizeof(*ESym);
1090 }
1091 }
1092}
1093
1094template <class ELFT>
1095void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
1096 // Write the internal symbol table contents to the output symbol table
1097 // pointed by Buf.
1098 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1099 for (const SymbolTableEntry &S : Symbols) {
1100 SymbolBody *Body = S.Symbol;
1101 size_t StrOff = S.StrTabOffset;
1102
1103 uint8_t Type = Body->Type;
1104 uintX_t Size = Body->getSize<ELFT>();
1105
1106 ESym->setBindingAndType(getSymbolBinding(Body), Type);
1107 ESym->st_size = Size;
1108 ESym->st_name = StrOff;
1109 ESym->setVisibility(Body->symbol()->Visibility);
1110 ESym->st_value = Body->getVA<ELFT>();
1111
1112 if (const OutputSectionBase *OutSec = getOutputSection(Body))
1113 ESym->st_shndx = OutSec->SectionIndex;
1114 else if (isa<DefinedRegular<ELFT>>(Body))
1115 ESym->st_shndx = SHN_ABS;
1116
1117 if (Config->EMachine == EM_MIPS) {
1118 // On MIPS we need to mark symbol which has a PLT entry and requires
1119 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1120 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1121 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1122 if (Body->isInPlt() && Body->NeedsCopyOrPltAddr)
1123 ESym->st_other |= STO_MIPS_PLT;
1124 if (Config->Relocatable) {
1125 auto *D = dyn_cast<DefinedRegular<ELFT>>(Body);
1126 if (D && D->isMipsPIC())
1127 ESym->st_other |= STO_MIPS_PIC;
1128 }
1129 }
1130 ++ESym;
1131 }
1132}
1133
1134template <class ELFT>
1135const OutputSectionBase *
1136SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1137 switch (Sym->kind()) {
1138 case SymbolBody::DefinedSyntheticKind:
1139 return cast<DefinedSynthetic<ELFT>>(Sym)->Section;
1140 case SymbolBody::DefinedRegularKind: {
1141 auto &D = cast<DefinedRegular<ELFT>>(*Sym);
1142 if (D.Section)
1143 return D.Section->OutSec;
1144 break;
1145 }
1146 case SymbolBody::DefinedCommonKind:
1147 return In<ELFT>::Common->OutSec;
1148 case SymbolBody::SharedKind:
1149 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
1150 return Out<ELFT>::Bss;
1151 break;
1152 case SymbolBody::UndefinedKind:
1153 case SymbolBody::LazyArchiveKind:
1154 case SymbolBody::LazyObjectKind:
1155 break;
1156 }
1157 return nullptr;
1158}
1159
Eugene Leviantbe809a72016-11-18 06:44:18 +00001160template <class ELFT>
1161GnuHashTableSection<ELFT>::GnuHashTableSection()
1162 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_HASH, sizeof(uintX_t),
1163 ".gnu.hash") {
1164 this->Entsize = ELFT::Is64Bits ? 0 : 4;
1165}
1166
1167template <class ELFT>
1168unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
1169 if (!NumHashed)
1170 return 0;
1171
1172 // These values are prime numbers which are not greater than 2^(N-1) + 1.
1173 // In result, for any particular NumHashed we return a prime number
1174 // which is not greater than NumHashed.
1175 static const unsigned Primes[] = {
1176 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
1177 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
1178
1179 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
1180 array_lengthof(Primes) - 1)];
1181}
1182
1183// Bloom filter estimation: at least 8 bits for each hashed symbol.
1184// GNU Hash table requirement: it should be a power of 2,
1185// the minimum value is 1, even for an empty table.
1186// Expected results for a 32-bit target:
1187// calcMaskWords(0..4) = 1
1188// calcMaskWords(5..8) = 2
1189// calcMaskWords(9..16) = 4
1190// For a 64-bit target:
1191// calcMaskWords(0..8) = 1
1192// calcMaskWords(9..16) = 2
1193// calcMaskWords(17..32) = 4
1194template <class ELFT>
1195unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
1196 if (!NumHashed)
1197 return 1;
1198 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
1199}
1200
1201template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
1202 unsigned NumHashed = Symbols.size();
1203 NBuckets = calcNBuckets(NumHashed);
1204 MaskWords = calcMaskWords(NumHashed);
1205 // Second hash shift estimation: just predefined values.
1206 Shift2 = ELFT::Is64Bits ? 6 : 5;
1207
1208 this->OutSec->Entsize = this->Entsize;
1209 this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1210 this->Size = sizeof(Elf_Word) * 4 // Header
1211 + sizeof(Elf_Off) * MaskWords // Bloom Filter
1212 + sizeof(Elf_Word) * NBuckets // Hash Buckets
1213 + sizeof(Elf_Word) * NumHashed; // Hash Values
1214}
1215
1216template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1217 writeHeader(Buf);
1218 if (Symbols.empty())
1219 return;
1220 writeBloomFilter(Buf);
1221 writeHashTable(Buf);
1222}
1223
1224template <class ELFT>
1225void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
1226 auto *P = reinterpret_cast<Elf_Word *>(Buf);
1227 *P++ = NBuckets;
1228 *P++ = In<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
1229 *P++ = MaskWords;
1230 *P++ = Shift2;
1231 Buf = reinterpret_cast<uint8_t *>(P);
1232}
1233
1234template <class ELFT>
1235void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
1236 unsigned C = sizeof(Elf_Off) * 8;
1237
1238 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
1239 for (const SymbolData &Sym : Symbols) {
1240 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
1241 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
1242 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
1243 Masks[Pos] |= V;
1244 }
1245 Buf += sizeof(Elf_Off) * MaskWords;
1246}
1247
1248template <class ELFT>
1249void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
1250 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
1251 Elf_Word *Values = Buckets + NBuckets;
1252
1253 int PrevBucket = -1;
1254 int I = 0;
1255 for (const SymbolData &Sym : Symbols) {
1256 int Bucket = Sym.Hash % NBuckets;
1257 assert(PrevBucket <= Bucket);
1258 if (Bucket != PrevBucket) {
1259 Buckets[Bucket] = Sym.Body->DynsymIndex;
1260 PrevBucket = Bucket;
1261 if (I > 0)
1262 Values[I - 1] |= 1;
1263 }
1264 Values[I] = Sym.Hash & ~1;
1265 ++I;
1266 }
1267 if (I > 0)
1268 Values[I - 1] |= 1;
1269}
1270
1271static uint32_t hashGnu(StringRef Name) {
1272 uint32_t H = 5381;
1273 for (uint8_t C : Name)
1274 H = (H << 5) + H + C;
1275 return H;
1276}
1277
1278// Add symbols to this symbol hash table. Note that this function
1279// destructively sort a given vector -- which is needed because
1280// GNU-style hash table places some sorting requirements.
1281template <class ELFT>
1282void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) {
1283 // Ideally this will just be 'auto' but GCC 6.1 is not able
1284 // to deduce it correctly.
1285 std::vector<SymbolTableEntry>::iterator Mid =
1286 std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
1287 return S.Symbol->isUndefined();
1288 });
1289 if (Mid == V.end())
1290 return;
1291 for (auto I = Mid, E = V.end(); I != E; ++I) {
1292 SymbolBody *B = I->Symbol;
1293 size_t StrOff = I->StrTabOffset;
1294 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
1295 }
1296
1297 unsigned NBuckets = calcNBuckets(Symbols.size());
1298 std::stable_sort(Symbols.begin(), Symbols.end(),
1299 [&](const SymbolData &L, const SymbolData &R) {
1300 return L.Hash % NBuckets < R.Hash % NBuckets;
1301 });
1302
1303 V.erase(Mid, V.end());
1304 for (const SymbolData &Sym : Symbols)
1305 V.push_back({Sym.Body, Sym.STName});
1306}
1307
Eugene Leviantb96e8092016-11-18 09:06:47 +00001308template <class ELFT>
1309HashTableSection<ELFT>::HashTableSection()
1310 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_HASH, sizeof(Elf_Word), ".hash") {
1311 this->Entsize = sizeof(Elf_Word);
1312}
1313
1314template <class ELFT> void HashTableSection<ELFT>::finalize() {
1315 this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1316 this->OutSec->Entsize = this->Entsize;
1317
1318 unsigned NumEntries = 2; // nbucket and nchain.
1319 NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
1320
1321 // Create as many buckets as there are symbols.
1322 // FIXME: This is simplistic. We can try to optimize it, but implementing
1323 // support for SHT_GNU_HASH is probably even more profitable.
1324 NumEntries += In<ELFT>::DynSymTab->getNumSymbols();
1325 this->Size = NumEntries * sizeof(Elf_Word);
1326}
1327
1328template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1329 unsigned NumSymbols = In<ELFT>::DynSymTab->getNumSymbols();
1330 auto *P = reinterpret_cast<Elf_Word *>(Buf);
1331 *P++ = NumSymbols; // nbucket
1332 *P++ = NumSymbols; // nchain
1333
1334 Elf_Word *Buckets = P;
1335 Elf_Word *Chains = P + NumSymbols;
1336
1337 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
1338 SymbolBody *Body = S.Symbol;
1339 StringRef Name = Body->getName();
1340 unsigned I = Body->DynsymIndex;
1341 uint32_t Hash = hashSysV(Name) % NumSymbols;
1342 Chains[I] = Buckets[Hash];
1343 Buckets[Hash] = I;
1344 }
1345}
1346
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001347template <class ELFT>
1348PltSection<ELFT>::PltSection()
1349 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16,
1350 ".plt") {}
1351
1352template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
1353 // At beginning of PLT, we have code to call the dynamic linker
1354 // to resolve dynsyms at runtime. Write such code.
1355 Target->writePltHeader(Buf);
1356 size_t Off = Target->PltHeaderSize;
1357
1358 for (auto &I : Entries) {
1359 const SymbolBody *B = I.first;
1360 unsigned RelOff = I.second;
1361 uint64_t Got = B->getGotPltVA<ELFT>();
1362 uint64_t Plt = this->getVA() + Off;
1363 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
1364 Off += Target->PltEntrySize;
1365 }
1366}
1367
1368template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
1369 Sym.PltIndex = Entries.size();
1370 unsigned RelOff = In<ELFT>::RelaPlt->getRelocOffset();
1371 Entries.push_back(std::make_pair(&Sym, RelOff));
1372}
1373
1374template <class ELFT> size_t PltSection<ELFT>::getSize() const {
1375 return Target->PltHeaderSize + Entries.size() * Target->PltEntrySize;
1376}
1377
Eugene Levianta113a412016-11-21 09:24:43 +00001378template <class ELFT>
1379GdbIndexSection<ELFT>::GdbIndexSection()
1380 : SyntheticSection<ELFT>(0, SHT_PROGBITS, 1, ".gdb_index") {}
1381
1382template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() {
1383 std::vector<InputSection<ELFT> *> &IS =
1384 static_cast<OutputSection<ELFT> *>(Out<ELFT>::DebugInfo)->Sections;
1385
1386 for (InputSection<ELFT> *I : IS)
1387 readDwarf(I);
1388}
1389
1390template <class ELFT>
1391void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) {
1392 std::vector<std::pair<uintX_t, uintX_t>> CuList = readCuList(I);
1393 CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end());
1394}
1395
1396template <class ELFT> void GdbIndexSection<ELFT>::finalize() {
1397 parseDebugSections();
1398
1399 // GdbIndex header consist from version fields
1400 // and 5 more fields with different kinds of offsets.
1401 CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize;
1402}
1403
1404template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) {
1405 write32le(Buf, 7); // Write Version
1406 write32le(Buf + 4, CuListOffset); // CU list offset
1407 write32le(Buf + 8, CuTypesOffset); // Types CU list offset
1408 write32le(Buf + 12, CuTypesOffset); // Address area offset
1409 write32le(Buf + 16, CuTypesOffset); // Symbol table offset
1410 write32le(Buf + 20, CuTypesOffset); // Constant pool offset
1411 Buf += 24;
1412
1413 // Write the CU list.
1414 for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) {
1415 write64le(Buf, CU.first);
1416 write64le(Buf + 8, CU.second);
1417 Buf += 16;
1418 }
1419}
1420
Eugene Leviant952eb4d2016-11-21 15:52:10 +00001421template <class ELFT>
1422EhFrameHeader<ELFT>::EhFrameHeader()
1423 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
1424
1425// .eh_frame_hdr contains a binary search table of pointers to FDEs.
1426// Each entry of the search table consists of two values,
1427// the starting PC from where FDEs covers, and the FDE's address.
1428// It is sorted by PC.
1429template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
1430 const endianness E = ELFT::TargetEndianness;
1431
1432 // Sort the FDE list by their PC and uniqueify. Usually there is only
1433 // one FDE for a PC (i.e. function), but if ICF merges two functions
1434 // into one, there can be more than one FDEs pointing to the address.
1435 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
1436 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
1437 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
1438 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
1439
1440 Buf[0] = 1;
1441 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1442 Buf[2] = DW_EH_PE_udata4;
1443 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
1444 write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->getVA() - 4);
1445 write32<E>(Buf + 8, Fdes.size());
1446 Buf += 12;
1447
1448 uintX_t VA = this->getVA();
1449 for (FdeData &Fde : Fdes) {
1450 write32<E>(Buf, Fde.Pc - VA);
1451 write32<E>(Buf + 4, Fde.FdeVA - VA);
1452 Buf += 8;
1453 }
1454}
1455
1456template <class ELFT> size_t EhFrameHeader<ELFT>::getSize() const {
1457 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
1458 return 12 + Out<ELFT>::EhFrame->NumFdes * 8;
1459}
1460
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001461template <class ELFT>
Rui Ueyamab38ddb12016-11-21 19:46:04 +00001462void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
1463 Fdes.push_back({Pc, FdeVA});
1464}
1465
1466template <class ELFT>
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001467VersionDefinitionSection<ELFT>::VersionDefinitionSection()
1468 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t),
1469 ".gnu.version_d") {}
1470
1471static StringRef getFileDefName() {
1472 if (!Config->SoName.empty())
1473 return Config->SoName;
1474 return Config->OutputFile;
1475}
1476
1477template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
1478 FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName());
1479 for (VersionDefinition &V : Config->VersionDefinitions)
1480 V.NameOff = In<ELFT>::DynStrTab->addString(V.Name);
1481
1482 this->OutSec->Link = this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
1483
1484 // sh_info should be set to the number of definitions. This fact is missed in
1485 // documentation, but confirmed by binutils community:
1486 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
1487 this->OutSec->Info = this->Info = getVerDefNum();
1488}
1489
1490template <class ELFT>
1491void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1492 StringRef Name, size_t NameOff) {
1493 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
1494 Verdef->vd_version = 1;
1495 Verdef->vd_cnt = 1;
1496 Verdef->vd_aux = sizeof(Elf_Verdef);
1497 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1498 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
1499 Verdef->vd_ndx = Index;
1500 Verdef->vd_hash = hashSysV(Name);
1501
1502 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
1503 Verdaux->vda_name = NameOff;
1504 Verdaux->vda_next = 0;
1505}
1506
1507template <class ELFT>
1508void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
1509 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
1510
1511 for (VersionDefinition &V : Config->VersionDefinitions) {
1512 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1513 writeOne(Buf, V.Id, V.Name, V.NameOff);
1514 }
1515
1516 // Need to terminate the last version definition.
1517 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
1518 Verdef->vd_next = 0;
1519}
1520
1521template <class ELFT> size_t VersionDefinitionSection<ELFT>::getSize() const {
1522 return (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
1523}
1524
1525template <class ELFT>
1526VersionTableSection<ELFT>::VersionTableSection()
1527 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t),
1528 ".gnu.version") {}
1529
1530template <class ELFT> void VersionTableSection<ELFT>::finalize() {
1531 this->OutSec->Entsize = this->Entsize = sizeof(Elf_Versym);
1532 // At the moment of june 2016 GNU docs does not mention that sh_link field
1533 // should be set, but Sun docs do. Also readelf relies on this field.
1534 this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1535}
1536
1537template <class ELFT> size_t VersionTableSection<ELFT>::getSize() const {
1538 return sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1);
1539}
1540
1541template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
1542 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
1543 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
1544 OutVersym->vs_index = S.Symbol->symbol()->VersionId;
1545 ++OutVersym;
1546 }
1547}
1548
1549template <class ELFT>
1550VersionNeedSection<ELFT>::VersionNeedSection()
1551 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t),
1552 ".gnu.version_r") {
1553 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
1554 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
1555 // First identifiers are reserved by verdef section if it exist.
1556 NextIndex = getVerDefNum() + 1;
1557}
1558
1559template <class ELFT>
1560void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
1561 if (!SS->Verdef) {
1562 SS->symbol()->VersionId = VER_NDX_GLOBAL;
1563 return;
1564 }
1565 SharedFile<ELFT> *F = SS->file();
1566 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
1567 // to create one by adding it to our needed list and creating a dynstr entry
1568 // for the soname.
1569 if (F->VerdefMap.empty())
1570 Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())});
1571 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
1572 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
1573 // prepare to create one by allocating a version identifier and creating a
1574 // dynstr entry for the version name.
1575 if (NV.Index == 0) {
1576 NV.StrTab = In<ELFT>::DynStrTab->addString(
1577 SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
1578 NV.Index = NextIndex++;
1579 }
1580 SS->symbol()->VersionId = NV.Index;
1581}
1582
1583template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
1584 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
1585 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
1586 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
1587
1588 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
1589 // Create an Elf_Verneed for this DSO.
1590 Verneed->vn_version = 1;
1591 Verneed->vn_cnt = P.first->VerdefMap.size();
1592 Verneed->vn_file = P.second;
1593 Verneed->vn_aux =
1594 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
1595 Verneed->vn_next = sizeof(Elf_Verneed);
1596 ++Verneed;
1597
1598 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
1599 // VerdefMap, which will only contain references to needed version
1600 // definitions. Each Elf_Vernaux is based on the information contained in
1601 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
1602 // pointers, but is deterministic because the pointers refer to Elf_Verdef
1603 // data structures within a single input file.
1604 for (auto &NV : P.first->VerdefMap) {
1605 Vernaux->vna_hash = NV.first->vd_hash;
1606 Vernaux->vna_flags = 0;
1607 Vernaux->vna_other = NV.second.Index;
1608 Vernaux->vna_name = NV.second.StrTab;
1609 Vernaux->vna_next = sizeof(Elf_Vernaux);
1610 ++Vernaux;
1611 }
1612
1613 Vernaux[-1].vna_next = 0;
1614 }
1615 Verneed[-1].vn_next = 0;
1616}
1617
1618template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
1619 this->OutSec->Link = this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
1620 this->OutSec->Info = this->Info = Needed.size();
1621}
1622
1623template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
1624 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
1625 for (const std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
1626 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
1627 return Size;
1628}
1629
Rafael Espindola682a5bc2016-11-08 14:42:34 +00001630template InputSection<ELF32LE> *elf::createCommonSection();
1631template InputSection<ELF32BE> *elf::createCommonSection();
1632template InputSection<ELF64LE> *elf::createCommonSection();
1633template InputSection<ELF64BE> *elf::createCommonSection();
Rui Ueyamae8a61022016-11-05 23:05:47 +00001634
Rafael Espindolac0e47fb2016-11-08 14:56:27 +00001635template InputSection<ELF32LE> *elf::createInterpSection();
1636template InputSection<ELF32BE> *elf::createInterpSection();
1637template InputSection<ELF64LE> *elf::createInterpSection();
1638template InputSection<ELF64BE> *elf::createInterpSection();
Rui Ueyamae288eef2016-11-02 18:58:44 +00001639
Rui Ueyama3da3f062016-11-10 20:20:37 +00001640template MergeInputSection<ELF32LE> *elf::createCommentSection();
1641template MergeInputSection<ELF32BE> *elf::createCommentSection();
1642template MergeInputSection<ELF64LE> *elf::createCommentSection();
1643template MergeInputSection<ELF64BE> *elf::createCommentSection();
1644
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +00001645template class elf::MipsAbiFlagsSection<ELF32LE>;
1646template class elf::MipsAbiFlagsSection<ELF32BE>;
1647template class elf::MipsAbiFlagsSection<ELF64LE>;
1648template class elf::MipsAbiFlagsSection<ELF64BE>;
1649
Simon Atanasyance02cf02016-11-09 21:36:56 +00001650template class elf::MipsOptionsSection<ELF32LE>;
1651template class elf::MipsOptionsSection<ELF32BE>;
1652template class elf::MipsOptionsSection<ELF64LE>;
1653template class elf::MipsOptionsSection<ELF64BE>;
1654
1655template class elf::MipsReginfoSection<ELF32LE>;
1656template class elf::MipsReginfoSection<ELF32BE>;
1657template class elf::MipsReginfoSection<ELF64LE>;
1658template class elf::MipsReginfoSection<ELF64BE>;
1659
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +00001660template class elf::BuildIdSection<ELF32LE>;
1661template class elf::BuildIdSection<ELF32BE>;
1662template class elf::BuildIdSection<ELF64LE>;
1663template class elf::BuildIdSection<ELF64BE>;
1664
Eugene Leviantad4439e2016-11-11 11:33:32 +00001665template class elf::GotSection<ELF32LE>;
1666template class elf::GotSection<ELF32BE>;
1667template class elf::GotSection<ELF64LE>;
1668template class elf::GotSection<ELF64BE>;
1669
Simon Atanasyan725dc142016-11-16 21:01:02 +00001670template class elf::MipsGotSection<ELF32LE>;
1671template class elf::MipsGotSection<ELF32BE>;
1672template class elf::MipsGotSection<ELF64LE>;
1673template class elf::MipsGotSection<ELF64BE>;
1674
Eugene Leviant41ca3272016-11-10 09:48:29 +00001675template class elf::GotPltSection<ELF32LE>;
1676template class elf::GotPltSection<ELF32BE>;
1677template class elf::GotPltSection<ELF64LE>;
1678template class elf::GotPltSection<ELF64BE>;
Eugene Leviant22eb0262016-11-14 09:16:00 +00001679
1680template class elf::StringTableSection<ELF32LE>;
1681template class elf::StringTableSection<ELF32BE>;
1682template class elf::StringTableSection<ELF64LE>;
1683template class elf::StringTableSection<ELF64BE>;
Eugene Leviant6380ce22016-11-15 12:26:55 +00001684
1685template class elf::DynamicSection<ELF32LE>;
1686template class elf::DynamicSection<ELF32BE>;
1687template class elf::DynamicSection<ELF64LE>;
1688template class elf::DynamicSection<ELF64BE>;
Eugene Levianta96d9022016-11-16 10:02:27 +00001689
1690template class elf::RelocationSection<ELF32LE>;
1691template class elf::RelocationSection<ELF32BE>;
1692template class elf::RelocationSection<ELF64LE>;
1693template class elf::RelocationSection<ELF64BE>;
Eugene Leviant9230db92016-11-17 09:16:34 +00001694
1695template class elf::SymbolTableSection<ELF32LE>;
1696template class elf::SymbolTableSection<ELF32BE>;
1697template class elf::SymbolTableSection<ELF64LE>;
1698template class elf::SymbolTableSection<ELF64BE>;
Eugene Leviantbe809a72016-11-18 06:44:18 +00001699
1700template class elf::GnuHashTableSection<ELF32LE>;
1701template class elf::GnuHashTableSection<ELF32BE>;
1702template class elf::GnuHashTableSection<ELF64LE>;
1703template class elf::GnuHashTableSection<ELF64BE>;
Eugene Leviantb96e8092016-11-18 09:06:47 +00001704
1705template class elf::HashTableSection<ELF32LE>;
1706template class elf::HashTableSection<ELF32BE>;
1707template class elf::HashTableSection<ELF64LE>;
1708template class elf::HashTableSection<ELF64BE>;
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001709
1710template class elf::PltSection<ELF32LE>;
1711template class elf::PltSection<ELF32BE>;
1712template class elf::PltSection<ELF64LE>;
1713template class elf::PltSection<ELF64BE>;
Eugene Levianta113a412016-11-21 09:24:43 +00001714
1715template class elf::GdbIndexSection<ELF32LE>;
1716template class elf::GdbIndexSection<ELF32BE>;
1717template class elf::GdbIndexSection<ELF64LE>;
1718template class elf::GdbIndexSection<ELF64BE>;
Eugene Leviant952eb4d2016-11-21 15:52:10 +00001719
1720template class elf::EhFrameHeader<ELF32LE>;
1721template class elf::EhFrameHeader<ELF32BE>;
1722template class elf::EhFrameHeader<ELF64LE>;
1723template class elf::EhFrameHeader<ELF64BE>;
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001724
1725template class elf::VersionTableSection<ELF32LE>;
1726template class elf::VersionTableSection<ELF32BE>;
1727template class elf::VersionTableSection<ELF64LE>;
1728template class elf::VersionTableSection<ELF64BE>;
1729
1730template class elf::VersionNeedSection<ELF32LE>;
1731template class elf::VersionNeedSection<ELF32BE>;
1732template class elf::VersionNeedSection<ELF64LE>;
1733template class elf::VersionNeedSection<ELF64BE>;
1734
1735template class elf::VersionDefinitionSection<ELF32LE>;
1736template class elf::VersionDefinitionSection<ELF32BE>;
1737template class elf::VersionDefinitionSection<ELF64LE>;
1738template class elf::VersionDefinitionSection<ELF64BE>;