blob: 3bc592bb1147d1b6d6941930679c86f985e7696b [file] [log] [blame]
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +00001//===- SyntheticSections.cpp ----------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains linker-synthesized sections. Currently,
11// synthetic sections are created either output sections or input sections,
12// but we are rewriting code so that all synthetic sections are created as
13// input sections.
14//
15//===----------------------------------------------------------------------===//
16
17#include "SyntheticSections.h"
18#include "Config.h"
19#include "Error.h"
20#include "InputFiles.h"
Eugene Leviant17b7a572016-11-22 17:49:14 +000021#include "LinkerScript.h"
Rui Ueyama9381eb12016-12-18 14:06:06 +000022#include "Memory.h"
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +000023#include "OutputSections.h"
24#include "Strings.h"
Rui Ueyamae8a61022016-11-05 23:05:47 +000025#include "SymbolTable.h"
Simon Atanasyance02cf02016-11-09 21:36:56 +000026#include "Target.h"
Rui Ueyama244a4352016-12-03 21:24:51 +000027#include "Threads.h"
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +000028#include "Writer.h"
Rui Ueyama3da3f062016-11-10 20:20:37 +000029#include "lld/Config/Version.h"
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
Rui Ueyamab2a23cf2017-01-24 03:41:20 +000062 if (!Config->DefineCommon)
63 return Ret;
64
Rui Ueyamae8a61022016-11-05 23:05:47 +000065 // Sort the common symbols by alignment as an heuristic to pack them better.
66 std::vector<DefinedCommon *> Syms = getCommonSymbols<ELFT>();
67 std::stable_sort(Syms.begin(), Syms.end(),
68 [](const DefinedCommon *A, const DefinedCommon *B) {
69 return A->Alignment > B->Alignment;
70 });
71
72 // Assign offsets to symbols.
73 size_t Size = 0;
74 size_t Alignment = 1;
75 for (DefinedCommon *Sym : Syms) {
Rui Ueyama1c786822016-11-05 23:14:54 +000076 Alignment = std::max<size_t>(Alignment, Sym->Alignment);
Rui Ueyamae8a61022016-11-05 23:05:47 +000077 Size = alignTo(Size, Sym->Alignment);
78
79 // Compute symbol offset relative to beginning of input section.
80 Sym->Offset = Size;
81 Size += Sym->Size;
82 }
Rafael Espindola682a5bc2016-11-08 14:42:34 +000083 Ret->Alignment = Alignment;
84 Ret->Data = makeArrayRef<uint8_t>(nullptr, Size);
85 return Ret;
Rui Ueyamae8a61022016-11-05 23:05:47 +000086}
87
Rui Ueyama3da3f062016-11-10 20:20:37 +000088// Returns an LLD version string.
89static ArrayRef<uint8_t> getVersion() {
90 // Check LLD_VERSION first for ease of testing.
91 // You can get consitent output by using the environment variable.
92 // This is only for testing.
93 StringRef S = getenv("LLD_VERSION");
94 if (S.empty())
95 S = Saver.save(Twine("Linker: ") + getLLDVersion());
96
97 // +1 to include the terminating '\0'.
98 return {(const uint8_t *)S.data(), S.size() + 1};
Davide Italianob69f38f2016-11-11 00:05:41 +000099}
Rui Ueyama3da3f062016-11-10 20:20:37 +0000100
101// Creates a .comment section containing LLD version info.
102// With this feature, you can identify LLD-generated binaries easily
103// by "objdump -s -j .comment <file>".
104// The returned object is a mergeable string section.
105template <class ELFT> MergeInputSection<ELFT> *elf::createCommentSection() {
106 typename ELFT::Shdr Hdr = {};
107 Hdr.sh_flags = SHF_MERGE | SHF_STRINGS;
108 Hdr.sh_type = SHT_PROGBITS;
109 Hdr.sh_entsize = 1;
110 Hdr.sh_addralign = 1;
111
112 auto *Ret = make<MergeInputSection<ELFT>>(/*file=*/nullptr, &Hdr, ".comment");
113 Ret->Data = getVersion();
114 Ret->splitIntoPieces();
115 return Ret;
116}
117
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000118// .MIPS.abiflags section.
119template <class ELFT>
Rui Ueyama12f2da82016-11-22 03:57:06 +0000120MipsAbiFlagsSection<ELFT>::MipsAbiFlagsSection(Elf_Mips_ABIFlags Flags)
121 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_ABIFLAGS, 8, ".MIPS.abiflags"),
122 Flags(Flags) {}
123
124template <class ELFT> void MipsAbiFlagsSection<ELFT>::writeTo(uint8_t *Buf) {
125 memcpy(Buf, &Flags, sizeof(Flags));
126}
127
128template <class ELFT>
129MipsAbiFlagsSection<ELFT> *MipsAbiFlagsSection<ELFT>::create() {
130 Elf_Mips_ABIFlags Flags = {};
131 bool Create = false;
132
133 for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
134 if (!Sec->Live || Sec->Type != SHT_MIPS_ABIFLAGS)
135 continue;
136 Sec->Live = false;
137 Create = true;
138
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000139 std::string Filename = toString(Sec->getFile());
Simon Atanasyan86dc60d2016-12-21 05:31:57 +0000140 const size_t Size = Sec->Data.size();
141 // Older version of BFD (such as the default FreeBSD linker) concatenate
142 // .MIPS.abiflags instead of merging. To allow for this case (or potential
143 // zero padding) we ignore everything after the first Elf_Mips_ABIFlags
144 if (Size < sizeof(Elf_Mips_ABIFlags)) {
145 error(Filename + ": invalid size of .MIPS.abiflags section: got " +
146 Twine(Size) + " instead of " + Twine(sizeof(Elf_Mips_ABIFlags)));
Rui Ueyama12f2da82016-11-22 03:57:06 +0000147 return nullptr;
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000148 }
Rui Ueyama12f2da82016-11-22 03:57:06 +0000149 auto *S = reinterpret_cast<const Elf_Mips_ABIFlags *>(Sec->Data.data());
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000150 if (S->version != 0) {
Rui Ueyama12f2da82016-11-22 03:57:06 +0000151 error(Filename + ": unexpected .MIPS.abiflags version " +
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000152 Twine(S->version));
Rui Ueyama12f2da82016-11-22 03:57:06 +0000153 return nullptr;
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000154 }
Rui Ueyama12f2da82016-11-22 03:57:06 +0000155
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000156 // LLD checks ISA compatibility in getMipsEFlags(). Here we just
157 // select the highest number of ISA/Rev/Ext.
158 Flags.isa_level = std::max(Flags.isa_level, S->isa_level);
159 Flags.isa_rev = std::max(Flags.isa_rev, S->isa_rev);
160 Flags.isa_ext = std::max(Flags.isa_ext, S->isa_ext);
161 Flags.gpr_size = std::max(Flags.gpr_size, S->gpr_size);
162 Flags.cpr1_size = std::max(Flags.cpr1_size, S->cpr1_size);
163 Flags.cpr2_size = std::max(Flags.cpr2_size, S->cpr2_size);
164 Flags.ases |= S->ases;
165 Flags.flags1 |= S->flags1;
166 Flags.flags2 |= S->flags2;
Rui Ueyama12f2da82016-11-22 03:57:06 +0000167 Flags.fp_abi = elf::getMipsFpAbiFlag(Flags.fp_abi, S->fp_abi, Filename);
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000168 };
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000169
Rui Ueyama12f2da82016-11-22 03:57:06 +0000170 if (Create)
171 return make<MipsAbiFlagsSection<ELFT>>(Flags);
172 return nullptr;
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +0000173}
174
Simon Atanasyance02cf02016-11-09 21:36:56 +0000175// .MIPS.options section.
176template <class ELFT>
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000177MipsOptionsSection<ELFT>::MipsOptionsSection(Elf_Mips_RegInfo Reginfo)
178 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_OPTIONS, 8, ".MIPS.options"),
179 Reginfo(Reginfo) {}
Simon Atanasyance02cf02016-11-09 21:36:56 +0000180
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000181template <class ELFT> void MipsOptionsSection<ELFT>::writeTo(uint8_t *Buf) {
182 auto *Options = reinterpret_cast<Elf_Mips_Options *>(Buf);
183 Options->kind = ODK_REGINFO;
184 Options->size = getSize();
185
186 if (!Config->Relocatable)
Simon Atanasyan8469b882016-11-23 22:22:16 +0000187 Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp();
Rafael Espindola4862ae82016-11-24 16:38:35 +0000188 memcpy(Buf + sizeof(Elf_Mips_Options), &Reginfo, sizeof(Reginfo));
Simon Atanasyance02cf02016-11-09 21:36:56 +0000189}
190
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000191template <class ELFT>
192MipsOptionsSection<ELFT> *MipsOptionsSection<ELFT>::create() {
193 // N64 ABI only.
194 if (!ELFT::Is64Bits)
195 return nullptr;
196
197 Elf_Mips_RegInfo Reginfo = {};
198 bool Create = false;
199
200 for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
201 if (!Sec->Live || Sec->Type != SHT_MIPS_OPTIONS)
202 continue;
203 Sec->Live = false;
204 Create = true;
205
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000206 std::string Filename = toString(Sec->getFile());
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000207 ArrayRef<uint8_t> D = Sec->Data;
208
209 while (!D.empty()) {
210 if (D.size() < sizeof(Elf_Mips_Options)) {
211 error(Filename + ": invalid size of .MIPS.options section");
212 break;
213 }
214
215 auto *Opt = reinterpret_cast<const Elf_Mips_Options *>(D.data());
216 if (Opt->kind == ODK_REGINFO) {
217 if (Config->Relocatable && Opt->getRegInfo().ri_gp_value)
218 error(Filename + ": unsupported non-zero ri_gp_value");
219 Reginfo.ri_gprmask |= Opt->getRegInfo().ri_gprmask;
220 Sec->getFile()->MipsGp0 = Opt->getRegInfo().ri_gp_value;
221 break;
222 }
223
224 if (!Opt->size)
225 fatal(Filename + ": zero option descriptor size");
226 D = D.slice(Opt->size);
227 }
228 };
229
230 if (Create)
Rui Ueyama3cc93d72016-11-22 23:13:08 +0000231 return make<MipsOptionsSection<ELFT>>(Reginfo);
Rui Ueyama9cfac8a2016-11-22 04:13:09 +0000232 return nullptr;
Simon Atanasyance02cf02016-11-09 21:36:56 +0000233}
234
235// MIPS .reginfo section.
236template <class ELFT>
Rui Ueyamab71cae92016-11-22 03:57:08 +0000237MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo)
238 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),
239 Reginfo(Reginfo) {}
Simon Atanasyance02cf02016-11-09 21:36:56 +0000240
Rui Ueyamab71cae92016-11-22 03:57:08 +0000241template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
Simon Atanasyance02cf02016-11-09 21:36:56 +0000242 if (!Config->Relocatable)
Simon Atanasyan8469b882016-11-23 22:22:16 +0000243 Reginfo.ri_gp_value = In<ELFT>::MipsGot->getGp();
Rui Ueyamab71cae92016-11-22 03:57:08 +0000244 memcpy(Buf, &Reginfo, sizeof(Reginfo));
245}
246
247template <class ELFT>
248MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
249 // Section should be alive for O32 and N32 ABIs only.
250 if (ELFT::Is64Bits)
251 return nullptr;
252
253 Elf_Mips_RegInfo Reginfo = {};
254 bool Create = false;
255
256 for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
257 if (!Sec->Live || Sec->Type != SHT_MIPS_REGINFO)
258 continue;
259 Sec->Live = false;
260 Create = true;
261
262 if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000263 error(toString(Sec->getFile()) + ": invalid size of .reginfo section");
Rui Ueyamab71cae92016-11-22 03:57:08 +0000264 return nullptr;
265 }
266 auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
267 if (Config->Relocatable && R->ri_gp_value)
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000268 error(toString(Sec->getFile()) + ": unsupported non-zero ri_gp_value");
Rui Ueyamab71cae92016-11-22 03:57:08 +0000269
270 Reginfo.ri_gprmask |= R->ri_gprmask;
271 Sec->getFile()->MipsGp0 = R->ri_gp_value;
272 };
273
274 if (Create)
275 return make<MipsReginfoSection<ELFT>>(Reginfo);
276 return nullptr;
Simon Atanasyance02cf02016-11-09 21:36:56 +0000277}
278
Rafael Espindolac0e47fb2016-11-08 14:56:27 +0000279template <class ELFT> InputSection<ELFT> *elf::createInterpSection() {
280 auto *Ret = make<InputSection<ELFT>>(SHF_ALLOC, SHT_PROGBITS, 1,
Rui Ueyama81a4b262016-11-22 04:33:01 +0000281 ArrayRef<uint8_t>(), ".interp");
Rafael Espindolac0e47fb2016-11-08 14:56:27 +0000282 Ret->Live = true;
Rui Ueyama81a4b262016-11-22 04:33:01 +0000283
284 // StringSaver guarantees that the returned string ends with '\0'.
285 StringRef S = Saver.save(Config->DynamicLinker);
286 Ret->Data = {(const uint8_t *)S.data(), S.size() + 1};
Rafael Espindolac0e47fb2016-11-08 14:56:27 +0000287 return Ret;
Rui Ueyamaa9ee8d62016-11-04 22:25:39 +0000288}
Rui Ueyamae288eef2016-11-02 18:58:44 +0000289
Peter Smith96943762017-01-25 10:31:16 +0000290template <class ELFT>
291SymbolBody *elf::addSyntheticLocal(StringRef Name, uint8_t Type,
292 typename ELFT::uint Value,
293 typename ELFT::uint Size,
294 InputSectionBase<ELFT> *Section) {
295 auto *S = make<DefinedRegular<ELFT>>(Name, /*IsLocal*/ true, STV_DEFAULT,
296 Type, Value, Size, Section, nullptr);
297 if (In<ELFT>::SymTab)
298 In<ELFT>::SymTab->addLocal(S);
299 return S;
300}
301
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000302static size_t getHashSize() {
Rui Ueyamac4030a12016-11-22 00:54:15 +0000303 switch (Config->BuildId) {
304 case BuildIdKind::Fast:
305 return 8;
306 case BuildIdKind::Md5:
307 case BuildIdKind::Uuid:
308 return 16;
309 case BuildIdKind::Sha1:
310 return 20;
311 case BuildIdKind::Hexstring:
312 return Config->BuildIdVector.size();
313 default:
314 llvm_unreachable("unknown BuildIdKind");
315 }
316}
317
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000318template <class ELFT>
319BuildIdSection<ELFT>::BuildIdSection()
320 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_NOTE, 1, ".note.gnu.build-id"),
Rui Ueyamabb536fe2016-11-22 01:36:19 +0000321 HashSize(getHashSize()) {}
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000322
323template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
324 const endianness E = ELFT::TargetEndianness;
325 write32<E>(Buf, 4); // Name size
326 write32<E>(Buf + 4, HashSize); // Content size
327 write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type
328 memcpy(Buf + 12, "GNU", 4); // Name string
329 HashBuf = Buf + 16;
330}
331
Rui Ueyama35e00752016-11-10 00:12:28 +0000332// Split one uint8 array into small pieces of uint8 arrays.
George Rimar364b59e22016-11-06 07:42:55 +0000333static std::vector<ArrayRef<uint8_t>> split(ArrayRef<uint8_t> Arr,
334 size_t ChunkSize) {
335 std::vector<ArrayRef<uint8_t>> Ret;
336 while (Arr.size() > ChunkSize) {
337 Ret.push_back(Arr.take_front(ChunkSize));
338 Arr = Arr.drop_front(ChunkSize);
339 }
340 if (!Arr.empty())
341 Ret.push_back(Arr);
342 return Ret;
343}
344
Rui Ueyama35e00752016-11-10 00:12:28 +0000345// Computes a hash value of Data using a given hash function.
346// In order to utilize multiple cores, we first split data into 1MB
347// chunks, compute a hash for each chunk, and then compute a hash value
348// of the hash values.
George Rimar364b59e22016-11-06 07:42:55 +0000349template <class ELFT>
350void BuildIdSection<ELFT>::computeHash(
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000351 llvm::ArrayRef<uint8_t> Data,
352 std::function<void(uint8_t *Dest, ArrayRef<uint8_t> Arr)> HashFn) {
George Rimar364b59e22016-11-06 07:42:55 +0000353 std::vector<ArrayRef<uint8_t>> Chunks = split(Data, 1024 * 1024);
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000354 std::vector<uint8_t> Hashes(Chunks.size() * HashSize);
George Rimar364b59e22016-11-06 07:42:55 +0000355
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000356 // Compute hash values.
Rui Ueyama244a4352016-12-03 21:24:51 +0000357 forLoop(0, Chunks.size(),
358 [&](size_t I) { HashFn(Hashes.data() + I * HashSize, Chunks[I]); });
Rui Ueyama35e00752016-11-10 00:12:28 +0000359
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000360 // Write to the final output buffer.
361 HashFn(HashBuf, Hashes);
George Rimar364b59e22016-11-06 07:42:55 +0000362}
363
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000364template <class ELFT>
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000365void BuildIdSection<ELFT>::writeBuildId(ArrayRef<uint8_t> Buf) {
Rui Ueyamac4030a12016-11-22 00:54:15 +0000366 switch (Config->BuildId) {
367 case BuildIdKind::Fast:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000368 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
Rui Ueyamac4030a12016-11-22 00:54:15 +0000369 write64le(Dest, xxHash64(toStringRef(Arr)));
370 });
371 break;
372 case BuildIdKind::Md5:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000373 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
Rui Ueyama28590b62016-11-23 18:11:38 +0000374 memcpy(Dest, MD5::hash(Arr).data(), 16);
Rui Ueyamac4030a12016-11-22 00:54:15 +0000375 });
376 break;
377 case BuildIdKind::Sha1:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000378 computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
Rui Ueyama28590b62016-11-23 18:11:38 +0000379 memcpy(Dest, SHA1::hash(Arr).data(), 20);
Rui Ueyamac4030a12016-11-22 00:54:15 +0000380 });
381 break;
382 case BuildIdKind::Uuid:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000383 if (getRandomBytes(HashBuf, HashSize))
Rui Ueyamac4030a12016-11-22 00:54:15 +0000384 error("entropy source failure");
385 break;
386 case BuildIdKind::Hexstring:
Rui Ueyama2d98fea2016-11-22 01:31:32 +0000387 memcpy(HashBuf, Config->BuildIdVector.data(), Config->BuildIdVector.size());
Rui Ueyamac4030a12016-11-22 00:54:15 +0000388 break;
389 default:
390 llvm_unreachable("unknown BuildIdKind");
391 }
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +0000392}
393
Eugene Leviant41ca3272016-11-10 09:48:29 +0000394template <class ELFT>
Eugene Leviantad4439e2016-11-11 11:33:32 +0000395GotSection<ELFT>::GotSection()
396 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
Simon Atanasyan725dc142016-11-16 21:01:02 +0000397 Target->GotEntrySize, ".got") {}
Eugene Leviantad4439e2016-11-11 11:33:32 +0000398
399template <class ELFT> void GotSection<ELFT>::addEntry(SymbolBody &Sym) {
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000400 Sym.GotIndex = NumEntries;
401 ++NumEntries;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000402}
403
Simon Atanasyan725dc142016-11-16 21:01:02 +0000404template <class ELFT> bool GotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
405 if (Sym.GlobalDynIndex != -1U)
406 return false;
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000407 Sym.GlobalDynIndex = NumEntries;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000408 // Global Dynamic TLS entries take two GOT slots.
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000409 NumEntries += 2;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000410 return true;
411}
412
413// Reserves TLS entries for a TLS module ID and a TLS block offset.
414// In total it takes two GOT slots.
415template <class ELFT> bool GotSection<ELFT>::addTlsIndex() {
416 if (TlsIndexOff != uint32_t(-1))
417 return false;
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000418 TlsIndexOff = NumEntries * sizeof(uintX_t);
419 NumEntries += 2;
Simon Atanasyan725dc142016-11-16 21:01:02 +0000420 return true;
421}
422
Eugene Leviantad4439e2016-11-11 11:33:32 +0000423template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000424typename GotSection<ELFT>::uintX_t
425GotSection<ELFT>::getGlobalDynAddr(const SymbolBody &B) const {
426 return this->getVA() + B.GlobalDynIndex * sizeof(uintX_t);
427}
428
429template <class ELFT>
430typename GotSection<ELFT>::uintX_t
431GotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
432 return B.GlobalDynIndex * sizeof(uintX_t);
433}
434
435template <class ELFT> void GotSection<ELFT>::finalize() {
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000436 Size = NumEntries * sizeof(uintX_t);
Simon Atanasyan725dc142016-11-16 21:01:02 +0000437}
438
George Rimar11992c862016-11-25 08:05:41 +0000439template <class ELFT> bool GotSection<ELFT>::empty() const {
440 // If we have a relocation that is relative to GOT (such as GOTOFFREL),
441 // we need to emit a GOT even if it's empty.
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000442 return NumEntries == 0 && !HasGotOffRel;
George Rimar11992c862016-11-25 08:05:41 +0000443}
444
Simon Atanasyan725dc142016-11-16 21:01:02 +0000445template <class ELFT> void GotSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolaf1e24532016-11-29 03:45:36 +0000446 this->relocate(Buf, Buf + Size);
Simon Atanasyan725dc142016-11-16 21:01:02 +0000447}
448
449template <class ELFT>
450MipsGotSection<ELFT>::MipsGotSection()
451 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL,
Simon Atanasyan2bd98af2017-01-16 21:17:23 +0000452 SHT_PROGBITS, 16, ".got") {}
Simon Atanasyan725dc142016-11-16 21:01:02 +0000453
454template <class ELFT>
455void MipsGotSection<ELFT>::addEntry(SymbolBody &Sym, uintX_t Addend,
Eugene Leviantad4439e2016-11-11 11:33:32 +0000456 RelExpr Expr) {
457 // For "true" local symbols which can be referenced from the same module
458 // only compiler creates two instructions for address loading:
459 //
460 // lw $8, 0($gp) # R_MIPS_GOT16
461 // addi $8, $8, 0 # R_MIPS_LO16
462 //
463 // The first instruction loads high 16 bits of the symbol address while
464 // the second adds an offset. That allows to reduce number of required
465 // GOT entries because only one global offset table entry is necessary
466 // for every 64 KBytes of local data. So for local symbols we need to
467 // allocate number of GOT entries to hold all required "page" addresses.
468 //
469 // All global symbols (hidden and regular) considered by compiler uniformly.
470 // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
471 // to load address of the symbol. So for each such symbol we need to
472 // allocate dedicated GOT entry to store its address.
473 //
474 // If a symbol is preemptible we need help of dynamic linker to get its
475 // final address. The corresponding GOT entries are allocated in the
476 // "global" part of GOT. Entries for non preemptible global symbol allocated
477 // in the "local" part of GOT.
478 //
479 // See "Global Offset Table" in Chapter 5:
480 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
481 if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
482 // At this point we do not know final symbol value so to reduce number
483 // of allocated GOT entries do the following trick. Save all output
484 // sections referenced by GOT relocations. Then later in the `finalize`
485 // method calculate number of "pages" required to cover all saved output
486 // section and allocate appropriate number of GOT entries.
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000487 PageIndexMap.insert({cast<DefinedRegular<ELFT>>(&Sym)->Section->OutSec, 0});
Eugene Leviantad4439e2016-11-11 11:33:32 +0000488 return;
489 }
490 if (Sym.isTls()) {
491 // GOT entries created for MIPS TLS relocations behave like
492 // almost GOT entries from other ABIs. They go to the end
493 // of the global offset table.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000494 Sym.GotIndex = TlsEntries.size();
495 TlsEntries.push_back(&Sym);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000496 return;
497 }
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000498 auto AddEntry = [&](SymbolBody &S, uintX_t A, GotEntries &Items) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000499 if (S.isInGot() && !A)
500 return;
501 size_t NewIndex = Items.size();
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000502 if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second)
Eugene Leviantad4439e2016-11-11 11:33:32 +0000503 return;
504 Items.emplace_back(&S, A);
505 if (!A)
506 S.GotIndex = NewIndex;
507 };
508 if (Sym.isPreemptible()) {
509 // Ignore addends for preemptible symbols. They got single GOT entry anyway.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000510 AddEntry(Sym, 0, GlobalEntries);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000511 Sym.IsInGlobalMipsGot = true;
512 } else if (Expr == R_MIPS_GOT_OFF32) {
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000513 AddEntry(Sym, Addend, LocalEntries32);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000514 Sym.Is32BitMipsGot = true;
515 } else {
516 // Hold local GOT entries accessed via a 16-bit index separately.
517 // That allows to write them in the beginning of the GOT and keep
518 // their indexes as less as possible to escape relocation's overflow.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000519 AddEntry(Sym, Addend, LocalEntries);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000520 }
521}
522
George Rimar879a6572016-12-15 15:38:58 +0000523template <class ELFT>
524bool MipsGotSection<ELFT>::addDynTlsEntry(SymbolBody &Sym) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000525 if (Sym.GlobalDynIndex != -1U)
526 return false;
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000527 Sym.GlobalDynIndex = TlsEntries.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000528 // Global Dynamic TLS entries take two GOT slots.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000529 TlsEntries.push_back(nullptr);
530 TlsEntries.push_back(&Sym);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000531 return true;
532}
533
534// Reserves TLS entries for a TLS module ID and a TLS block offset.
535// In total it takes two GOT slots.
Simon Atanasyan725dc142016-11-16 21:01:02 +0000536template <class ELFT> bool MipsGotSection<ELFT>::addTlsIndex() {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000537 if (TlsIndexOff != uint32_t(-1))
538 return false;
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000539 TlsIndexOff = TlsEntries.size() * sizeof(uintX_t);
540 TlsEntries.push_back(nullptr);
541 TlsEntries.push_back(nullptr);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000542 return true;
543}
544
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000545static uint64_t getMipsPageAddr(uint64_t Addr) {
546 return (Addr + 0x8000) & ~0xffff;
547}
548
549static uint64_t getMipsPageCount(uint64_t Size) {
550 return (Size + 0xfffe) / 0xffff + 1;
551}
552
Eugene Leviantad4439e2016-11-11 11:33:32 +0000553template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000554typename MipsGotSection<ELFT>::uintX_t
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000555MipsGotSection<ELFT>::getPageEntryOffset(const SymbolBody &B,
556 uintX_t Addend) const {
557 const OutputSectionBase *OutSec =
558 cast<DefinedRegular<ELFT>>(&B)->Section->OutSec;
559 uintX_t SecAddr = getMipsPageAddr(OutSec->Addr);
560 uintX_t SymAddr = getMipsPageAddr(B.getVA<ELFT>(Addend));
561 uintX_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff;
562 assert(Index < PageEntriesNum);
563 return (HeaderEntriesNum + Index) * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000564}
565
566template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000567typename MipsGotSection<ELFT>::uintX_t
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000568MipsGotSection<ELFT>::getBodyEntryOffset(const SymbolBody &B,
569 uintX_t Addend) const {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000570 // Calculate offset of the GOT entries block: TLS, global, local.
Simon Atanasyana0efc422016-11-29 10:23:50 +0000571 uintX_t Index = HeaderEntriesNum + PageEntriesNum;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000572 if (B.isTls())
Simon Atanasyana0efc422016-11-29 10:23:50 +0000573 Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000574 else if (B.IsInGlobalMipsGot)
Simon Atanasyana0efc422016-11-29 10:23:50 +0000575 Index += LocalEntries.size() + LocalEntries32.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000576 else if (B.Is32BitMipsGot)
Simon Atanasyana0efc422016-11-29 10:23:50 +0000577 Index += LocalEntries.size();
578 // Calculate offset of the GOT entry in the block.
Eugene Leviantad4439e2016-11-11 11:33:32 +0000579 if (B.isInGot())
Simon Atanasyana0efc422016-11-29 10:23:50 +0000580 Index += B.GotIndex;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000581 else {
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000582 auto It = EntryIndexMap.find({&B, Addend});
583 assert(It != EntryIndexMap.end());
Simon Atanasyana0efc422016-11-29 10:23:50 +0000584 Index += It->second;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000585 }
Simon Atanasyana0efc422016-11-29 10:23:50 +0000586 return Index * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000587}
588
589template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000590typename MipsGotSection<ELFT>::uintX_t
591MipsGotSection<ELFT>::getTlsOffset() const {
592 return (getLocalEntriesNum() + GlobalEntries.size()) * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000593}
594
595template <class ELFT>
Simon Atanasyan725dc142016-11-16 21:01:02 +0000596typename MipsGotSection<ELFT>::uintX_t
597MipsGotSection<ELFT>::getGlobalDynOffset(const SymbolBody &B) const {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000598 return B.GlobalDynIndex * sizeof(uintX_t);
599}
600
601template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000602const SymbolBody *MipsGotSection<ELFT>::getFirstGlobalEntry() const {
603 return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first;
Eugene Leviantad4439e2016-11-11 11:33:32 +0000604}
605
606template <class ELFT>
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000607unsigned MipsGotSection<ELFT>::getLocalEntriesNum() const {
Simon Atanasyana0efc422016-11-29 10:23:50 +0000608 return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() +
609 LocalEntries32.size();
Eugene Leviantad4439e2016-11-11 11:33:32 +0000610}
611
Simon Atanasyan725dc142016-11-16 21:01:02 +0000612template <class ELFT> void MipsGotSection<ELFT>::finalize() {
Simon Atanasyana0efc422016-11-29 10:23:50 +0000613 PageEntriesNum = 0;
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000614 for (std::pair<const OutputSectionBase *, size_t> &P : PageIndexMap) {
615 // For each output section referenced by GOT page relocations calculate
616 // and save into PageIndexMap an upper bound of MIPS GOT entries required
617 // to store page addresses of local symbols. We assume the worst case -
618 // each 64kb page of the output section has at least one GOT relocation
619 // against it. And take in account the case when the section intersects
620 // page boundaries.
621 P.second = PageEntriesNum;
622 PageEntriesNum += getMipsPageCount(P.first->Size);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000623 }
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000624 Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) *
625 sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000626}
627
George Rimar11992c862016-11-25 08:05:41 +0000628template <class ELFT> bool MipsGotSection<ELFT>::empty() const {
629 // We add the .got section to the result for dynamic MIPS target because
630 // its address and properties are mentioned in the .dynamic section.
631 return Config->Relocatable;
632}
633
Simon Atanasyanb9666652016-12-12 14:30:18 +0000634template <class ELFT>
635typename MipsGotSection<ELFT>::uintX_t MipsGotSection<ELFT>::getGp() const {
Simon Atanasyan8469b882016-11-23 22:22:16 +0000636 return ElfSym<ELFT>::MipsGp->template getVA<ELFT>(0);
637}
638
Eugene Leviantad4439e2016-11-11 11:33:32 +0000639template <class ELFT>
640static void writeUint(uint8_t *Buf, typename ELFT::uint Val) {
641 typedef typename ELFT::uint uintX_t;
642 write<uintX_t, ELFT::TargetEndianness, sizeof(uintX_t)>(Buf, Val);
643}
644
Simon Atanasyan725dc142016-11-16 21:01:02 +0000645template <class ELFT> void MipsGotSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000646 // Set the MSB of the second GOT slot. This is not required by any
647 // MIPS ABI documentation, though.
648 //
649 // There is a comment in glibc saying that "The MSB of got[1] of a
650 // gnu object is set to identify gnu objects," and in GNU gold it
651 // says "the second entry will be used by some runtime loaders".
652 // But how this field is being used is unclear.
653 //
654 // We are not really willing to mimic other linkers behaviors
655 // without understanding why they do that, but because all files
656 // generated by GNU tools have this special GOT value, and because
657 // we've been doing this for years, it is probably a safe bet to
658 // keep doing this for now. We really need to revisit this to see
659 // if we had to do this.
660 auto *P = reinterpret_cast<typename ELFT::Off *>(Buf);
661 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyana0efc422016-11-29 10:23:50 +0000662 Buf += HeaderEntriesNum * sizeof(uintX_t);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000663 // Write 'page address' entries to the local part of the GOT.
Simon Atanasyan9fae3b82016-11-29 10:23:56 +0000664 for (std::pair<const OutputSectionBase *, size_t> &L : PageIndexMap) {
665 size_t PageCount = getMipsPageCount(L.first->Size);
666 uintX_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
667 for (size_t PI = 0; PI < PageCount; ++PI) {
668 uint8_t *Entry = Buf + (L.second + PI) * sizeof(uintX_t);
669 writeUint<ELFT>(Entry, FirstPageAddr + PI * 0x10000);
670 }
Eugene Leviantad4439e2016-11-11 11:33:32 +0000671 }
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000672 Buf += PageEntriesNum * sizeof(uintX_t);
673 auto AddEntry = [&](const GotEntry &SA) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000674 uint8_t *Entry = Buf;
675 Buf += sizeof(uintX_t);
676 const SymbolBody *Body = SA.first;
677 uintX_t VA = Body->template getVA<ELFT>(SA.second);
678 writeUint<ELFT>(Entry, VA);
679 };
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000680 std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry);
681 std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry);
682 std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry);
Eugene Leviantad4439e2016-11-11 11:33:32 +0000683 // Initialize TLS-related GOT entries. If the entry has a corresponding
684 // dynamic relocations, leave it initialized by zero. Write down adjusted
685 // TLS symbol's values otherwise. To calculate the adjustments use offsets
686 // for thread-local storage.
687 // https://www.linux-mips.org/wiki/NPTL
688 if (TlsIndexOff != -1U && !Config->Pic)
689 writeUint<ELFT>(Buf + TlsIndexOff, 1);
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000690 for (const SymbolBody *B : TlsEntries) {
Eugene Leviantad4439e2016-11-11 11:33:32 +0000691 if (!B || B->isPreemptible())
692 continue;
693 uintX_t VA = B->getVA<ELFT>();
694 if (B->GotIndex != -1U) {
695 uint8_t *Entry = Buf + B->GotIndex * sizeof(uintX_t);
696 writeUint<ELFT>(Entry, VA - 0x7000);
697 }
698 if (B->GlobalDynIndex != -1U) {
699 uint8_t *Entry = Buf + B->GlobalDynIndex * sizeof(uintX_t);
700 writeUint<ELFT>(Entry, 1);
701 Entry += sizeof(uintX_t);
702 writeUint<ELFT>(Entry, VA - 0x8000);
703 }
704 }
705}
706
Eugene Leviantad4439e2016-11-11 11:33:32 +0000707template <class ELFT>
Eugene Leviant41ca3272016-11-10 09:48:29 +0000708GotPltSection<ELFT>::GotPltSection()
709 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
George Rimard8b27762016-11-14 10:14:18 +0000710 Target->GotPltEntrySize, ".got.plt") {}
Eugene Leviant41ca3272016-11-10 09:48:29 +0000711
712template <class ELFT> void GotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
713 Sym.GotPltIndex = Target->GotPltHeaderEntriesNum + Entries.size();
714 Entries.push_back(&Sym);
715}
716
Eugene Leviant41ca3272016-11-10 09:48:29 +0000717template <class ELFT> size_t GotPltSection<ELFT>::getSize() const {
718 return (Target->GotPltHeaderEntriesNum + Entries.size()) *
719 Target->GotPltEntrySize;
720}
721
722template <class ELFT> void GotPltSection<ELFT>::writeTo(uint8_t *Buf) {
723 Target->writeGotPltHeader(Buf);
724 Buf += Target->GotPltHeaderEntriesNum * Target->GotPltEntrySize;
725 for (const SymbolBody *B : Entries) {
726 Target->writeGotPlt(Buf, *B);
727 Buf += sizeof(uintX_t);
728 }
729}
730
Peter Smithbaffdb82016-12-08 12:58:55 +0000731// On ARM the IgotPltSection is part of the GotSection, on other Targets it is
732// part of the .got.plt
733template <class ELFT>
734IgotPltSection<ELFT>::IgotPltSection()
735 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
736 Target->GotPltEntrySize,
737 Config->EMachine == EM_ARM ? ".got" : ".got.plt") {
738}
739
740template <class ELFT> void IgotPltSection<ELFT>::addEntry(SymbolBody &Sym) {
741 Sym.IsInIgot = true;
742 Sym.GotPltIndex = Entries.size();
743 Entries.push_back(&Sym);
744}
745
746template <class ELFT> size_t IgotPltSection<ELFT>::getSize() const {
747 return Entries.size() * Target->GotPltEntrySize;
748}
749
750template <class ELFT> void IgotPltSection<ELFT>::writeTo(uint8_t *Buf) {
751 for (const SymbolBody *B : Entries) {
Peter Smith4b360292016-12-09 09:59:54 +0000752 Target->writeIgotPlt(Buf, *B);
Peter Smithbaffdb82016-12-08 12:58:55 +0000753 Buf += sizeof(uintX_t);
754 }
755}
756
Eugene Leviant22eb0262016-11-14 09:16:00 +0000757template <class ELFT>
758StringTableSection<ELFT>::StringTableSection(StringRef Name, bool Dynamic)
759 : SyntheticSection<ELFT>(Dynamic ? (uintX_t)SHF_ALLOC : 0, SHT_STRTAB, 1,
760 Name),
761 Dynamic(Dynamic) {}
762
763// Adds a string to the string table. If HashIt is true we hash and check for
764// duplicates. It is optional because the name of global symbols are already
765// uniqued and hashing them again has a big cost for a small value: uniquing
766// them with some other string that happens to be the same.
767template <class ELFT>
768unsigned StringTableSection<ELFT>::addString(StringRef S, bool HashIt) {
769 if (HashIt) {
770 auto R = StringMap.insert(std::make_pair(S, this->Size));
771 if (!R.second)
772 return R.first->second;
773 }
774 unsigned Ret = this->Size;
775 this->Size = this->Size + S.size() + 1;
776 Strings.push_back(S);
777 return Ret;
778}
779
780template <class ELFT> void StringTableSection<ELFT>::writeTo(uint8_t *Buf) {
781 // ELF string tables start with NUL byte, so advance the pointer by one.
782 ++Buf;
783 for (StringRef S : Strings) {
784 memcpy(Buf, S.data(), S.size());
785 Buf += S.size() + 1;
786 }
787}
788
Eugene Leviante9bab5d2016-11-21 16:59:33 +0000789// Returns the number of version definition entries. Because the first entry
790// is for the version definition itself, it is the number of versioned symbols
791// plus one. Note that we don't support multiple versions yet.
Eugene Leviant6380ce22016-11-15 12:26:55 +0000792static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
793
794template <class ELFT>
795DynamicSection<ELFT>::DynamicSection()
796 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_DYNAMIC,
797 sizeof(uintX_t), ".dynamic") {
798 this->Entsize = ELFT::Is64Bits ? 16 : 8;
799 // .dynamic section is not writable on MIPS.
800 // See "Special Section" in Chapter 4 in the following document:
801 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
802 if (Config->EMachine == EM_MIPS)
803 this->Flags = SHF_ALLOC;
804
805 addEntries();
806}
807
808// There are some dynamic entries that don't depend on other sections.
809// Such entries can be set early.
810template <class ELFT> void DynamicSection<ELFT>::addEntries() {
811 // Add strings to .dynstr early so that .dynstr's size will be
812 // fixed early.
813 for (StringRef S : Config->AuxiliaryList)
Rui Ueyama729ac792016-11-17 04:10:09 +0000814 add({DT_AUXILIARY, In<ELFT>::DynStrTab->addString(S)});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000815 if (!Config->RPath.empty())
Rui Ueyama729ac792016-11-17 04:10:09 +0000816 add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
Eugene Leviant6380ce22016-11-15 12:26:55 +0000817 In<ELFT>::DynStrTab->addString(Config->RPath)});
818 for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
819 if (F->isNeeded())
Rui Ueyama729ac792016-11-17 04:10:09 +0000820 add({DT_NEEDED, In<ELFT>::DynStrTab->addString(F->getSoName())});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000821 if (!Config->SoName.empty())
Rui Ueyama729ac792016-11-17 04:10:09 +0000822 add({DT_SONAME, In<ELFT>::DynStrTab->addString(Config->SoName)});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000823
824 // Set DT_FLAGS and DT_FLAGS_1.
825 uint32_t DtFlags = 0;
826 uint32_t DtFlags1 = 0;
827 if (Config->Bsymbolic)
828 DtFlags |= DF_SYMBOLIC;
829 if (Config->ZNodelete)
830 DtFlags1 |= DF_1_NODELETE;
831 if (Config->ZNow) {
832 DtFlags |= DF_BIND_NOW;
833 DtFlags1 |= DF_1_NOW;
834 }
835 if (Config->ZOrigin) {
836 DtFlags |= DF_ORIGIN;
837 DtFlags1 |= DF_1_ORIGIN;
838 }
839
840 if (DtFlags)
Rui Ueyama729ac792016-11-17 04:10:09 +0000841 add({DT_FLAGS, DtFlags});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000842 if (DtFlags1)
Rui Ueyama729ac792016-11-17 04:10:09 +0000843 add({DT_FLAGS_1, DtFlags1});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000844
Petr Hosek668bebe2016-12-07 02:05:42 +0000845 if (!Config->Shared && !Config->Relocatable)
Rui Ueyama729ac792016-11-17 04:10:09 +0000846 add({DT_DEBUG, (uint64_t)0});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000847}
848
849// Add remaining entries to complete .dynamic contents.
850template <class ELFT> void DynamicSection<ELFT>::finalize() {
851 if (this->Size)
852 return; // Already finalized.
853
854 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
Peter Smithbaffdb82016-12-08 12:58:55 +0000855 if (In<ELFT>::RelaDyn->OutSec->Size > 0) {
Eugene Leviant6380ce22016-11-15 12:26:55 +0000856 bool IsRela = Config->Rela;
Rui Ueyama729ac792016-11-17 04:10:09 +0000857 add({IsRela ? DT_RELA : DT_REL, In<ELFT>::RelaDyn});
Peter Smithbaffdb82016-12-08 12:58:55 +0000858 add({IsRela ? DT_RELASZ : DT_RELSZ, In<ELFT>::RelaDyn->OutSec->Size});
Rui Ueyama729ac792016-11-17 04:10:09 +0000859 add({IsRela ? DT_RELAENT : DT_RELENT,
Eugene Leviant6380ce22016-11-15 12:26:55 +0000860 uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});
861
862 // MIPS dynamic loader does not support RELCOUNT tag.
863 // The problem is in the tight relation between dynamic
864 // relocations and GOT. So do not emit this tag on MIPS.
865 if (Config->EMachine != EM_MIPS) {
Eugene Levianta96d9022016-11-16 10:02:27 +0000866 size_t NumRelativeRels = In<ELFT>::RelaDyn->getRelativeRelocCount();
Eugene Leviant6380ce22016-11-15 12:26:55 +0000867 if (Config->ZCombreloc && NumRelativeRels)
Rui Ueyama729ac792016-11-17 04:10:09 +0000868 add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000869 }
870 }
Peter Smithbaffdb82016-12-08 12:58:55 +0000871 if (In<ELFT>::RelaPlt->OutSec->Size > 0) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000872 add({DT_JMPREL, In<ELFT>::RelaPlt});
Peter Smithbaffdb82016-12-08 12:58:55 +0000873 add({DT_PLTRELSZ, In<ELFT>::RelaPlt->OutSec->Size});
Rui Ueyama729ac792016-11-17 04:10:09 +0000874 add({Config->EMachine == EM_MIPS ? DT_MIPS_PLTGOT : DT_PLTGOT,
Eugene Leviant6380ce22016-11-15 12:26:55 +0000875 In<ELFT>::GotPlt});
Rui Ueyama729ac792016-11-17 04:10:09 +0000876 add({DT_PLTREL, uint64_t(Config->Rela ? DT_RELA : DT_REL)});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000877 }
878
Eugene Leviant9230db92016-11-17 09:16:34 +0000879 add({DT_SYMTAB, In<ELFT>::DynSymTab});
Rui Ueyama729ac792016-11-17 04:10:09 +0000880 add({DT_SYMENT, sizeof(Elf_Sym)});
881 add({DT_STRTAB, In<ELFT>::DynStrTab});
882 add({DT_STRSZ, In<ELFT>::DynStrTab->getSize()});
Eugene Leviantbe809a72016-11-18 06:44:18 +0000883 if (In<ELFT>::GnuHashTab)
884 add({DT_GNU_HASH, In<ELFT>::GnuHashTab});
Eugene Leviantb96e8092016-11-18 09:06:47 +0000885 if (In<ELFT>::HashTab)
886 add({DT_HASH, In<ELFT>::HashTab});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000887
888 if (Out<ELFT>::PreinitArray) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000889 add({DT_PREINIT_ARRAY, Out<ELFT>::PreinitArray});
890 add({DT_PREINIT_ARRAYSZ, Out<ELFT>::PreinitArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000891 }
892 if (Out<ELFT>::InitArray) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000893 add({DT_INIT_ARRAY, Out<ELFT>::InitArray});
894 add({DT_INIT_ARRAYSZ, Out<ELFT>::InitArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000895 }
896 if (Out<ELFT>::FiniArray) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000897 add({DT_FINI_ARRAY, Out<ELFT>::FiniArray});
898 add({DT_FINI_ARRAYSZ, Out<ELFT>::FiniArray, Entry::SecSize});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000899 }
900
Rafael Espindola1d6d1b42017-01-17 16:08:06 +0000901 if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Init))
Rui Ueyama729ac792016-11-17 04:10:09 +0000902 add({DT_INIT, B});
Rafael Espindola1d6d1b42017-01-17 16:08:06 +0000903 if (SymbolBody *B = Symtab<ELFT>::X->findInCurrentDSO(Config->Fini))
Rui Ueyama729ac792016-11-17 04:10:09 +0000904 add({DT_FINI, B});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000905
Eugene Leviante9bab5d2016-11-21 16:59:33 +0000906 bool HasVerNeed = In<ELFT>::VerNeed->getNeedNum() != 0;
907 if (HasVerNeed || In<ELFT>::VerDef)
908 add({DT_VERSYM, In<ELFT>::VerSym});
909 if (In<ELFT>::VerDef) {
910 add({DT_VERDEF, In<ELFT>::VerDef});
Rui Ueyama729ac792016-11-17 04:10:09 +0000911 add({DT_VERDEFNUM, getVerDefNum()});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000912 }
913 if (HasVerNeed) {
Eugene Leviante9bab5d2016-11-21 16:59:33 +0000914 add({DT_VERNEED, In<ELFT>::VerNeed});
915 add({DT_VERNEEDNUM, In<ELFT>::VerNeed->getNeedNum()});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000916 }
917
918 if (Config->EMachine == EM_MIPS) {
Rui Ueyama729ac792016-11-17 04:10:09 +0000919 add({DT_MIPS_RLD_VERSION, 1});
920 add({DT_MIPS_FLAGS, RHF_NOTPOT});
921 add({DT_MIPS_BASE_ADDRESS, Config->ImageBase});
Eugene Leviant9230db92016-11-17 09:16:34 +0000922 add({DT_MIPS_SYMTABNO, In<ELFT>::DynSymTab->getNumSymbols()});
Simon Atanasyanb8bfec62016-11-17 21:49:14 +0000923 add({DT_MIPS_LOCAL_GOTNO, In<ELFT>::MipsGot->getLocalEntriesNum()});
924 if (const SymbolBody *B = In<ELFT>::MipsGot->getFirstGlobalEntry())
Rui Ueyama729ac792016-11-17 04:10:09 +0000925 add({DT_MIPS_GOTSYM, B->DynsymIndex});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000926 else
Eugene Leviant9230db92016-11-17 09:16:34 +0000927 add({DT_MIPS_GOTSYM, In<ELFT>::DynSymTab->getNumSymbols()});
Rui Ueyama729ac792016-11-17 04:10:09 +0000928 add({DT_PLTGOT, In<ELFT>::MipsGot});
Eugene Leviant17b7a572016-11-22 17:49:14 +0000929 if (In<ELFT>::MipsRldMap)
930 add({DT_MIPS_RLD_MAP, In<ELFT>::MipsRldMap});
Eugene Leviant6380ce22016-11-15 12:26:55 +0000931 }
932
933 this->OutSec->Entsize = this->Entsize;
934 this->OutSec->Link = this->Link;
935
936 // +1 for DT_NULL
937 this->Size = (Entries.size() + 1) * this->Entsize;
938}
939
940template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *Buf) {
941 auto *P = reinterpret_cast<Elf_Dyn *>(Buf);
942
943 for (const Entry &E : Entries) {
944 P->d_tag = E.Tag;
945 switch (E.Kind) {
946 case Entry::SecAddr:
947 P->d_un.d_ptr = E.OutSec->Addr;
948 break;
949 case Entry::InSecAddr:
950 P->d_un.d_ptr = E.InSec->OutSec->Addr + E.InSec->OutSecOff;
951 break;
952 case Entry::SecSize:
953 P->d_un.d_val = E.OutSec->Size;
954 break;
955 case Entry::SymAddr:
956 P->d_un.d_ptr = E.Sym->template getVA<ELFT>();
957 break;
958 case Entry::PlainInt:
959 P->d_un.d_val = E.Val;
960 break;
961 }
962 ++P;
963 }
964}
965
Eugene Levianta96d9022016-11-16 10:02:27 +0000966template <class ELFT>
967typename ELFT::uint DynamicReloc<ELFT>::getOffset() const {
968 if (OutputSec)
969 return OutputSec->Addr + OffsetInSec;
970 return InputSec->OutSec->Addr + InputSec->getOffset(OffsetInSec);
971}
972
973template <class ELFT>
974typename ELFT::uint DynamicReloc<ELFT>::getAddend() const {
975 if (UseSymVA)
976 return Sym->getVA<ELFT>(Addend);
977 return Addend;
978}
979
980template <class ELFT> uint32_t DynamicReloc<ELFT>::getSymIndex() const {
981 if (Sym && !UseSymVA)
982 return Sym->DynsymIndex;
983 return 0;
984}
985
986template <class ELFT>
987RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
988 : SyntheticSection<ELFT>(SHF_ALLOC, Config->Rela ? SHT_RELA : SHT_REL,
989 sizeof(uintX_t), Name),
990 Sort(Sort) {
991 this->Entsize = Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
992}
993
994template <class ELFT>
995void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
996 if (Reloc.Type == Target->RelativeRel)
997 ++NumRelativeRelocs;
998 Relocs.push_back(Reloc);
999}
1000
1001template <class ELFT, class RelTy>
1002static bool compRelocations(const RelTy &A, const RelTy &B) {
1003 bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel;
1004 bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel;
1005 if (AIsRel != BIsRel)
1006 return AIsRel;
1007
1008 return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL);
1009}
1010
1011template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1012 uint8_t *BufBegin = Buf;
1013 for (const DynamicReloc<ELFT> &Rel : Relocs) {
1014 auto *P = reinterpret_cast<Elf_Rela *>(Buf);
1015 Buf += Config->Rela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
1016
1017 if (Config->Rela)
1018 P->r_addend = Rel.getAddend();
1019 P->r_offset = Rel.getOffset();
Simon Atanasyan725dc142016-11-16 21:01:02 +00001020 if (Config->EMachine == EM_MIPS && Rel.getInputSec() == In<ELFT>::MipsGot)
Eugene Levianta96d9022016-11-16 10:02:27 +00001021 // Dynamic relocation against MIPS GOT section make deal TLS entries
1022 // allocated in the end of the GOT. We need to adjust the offset to take
1023 // in account 'local' and 'global' GOT entries.
Simon Atanasyanb8bfec62016-11-17 21:49:14 +00001024 P->r_offset += In<ELFT>::MipsGot->getTlsOffset();
Eugene Levianta96d9022016-11-16 10:02:27 +00001025 P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->Mips64EL);
1026 }
1027
1028 if (Sort) {
1029 if (Config->Rela)
1030 std::stable_sort((Elf_Rela *)BufBegin,
1031 (Elf_Rela *)BufBegin + Relocs.size(),
1032 compRelocations<ELFT, Elf_Rela>);
1033 else
1034 std::stable_sort((Elf_Rel *)BufBegin, (Elf_Rel *)BufBegin + Relocs.size(),
1035 compRelocations<ELFT, Elf_Rel>);
1036 }
1037}
1038
1039template <class ELFT> unsigned RelocationSection<ELFT>::getRelocOffset() {
1040 return this->Entsize * Relocs.size();
1041}
1042
1043template <class ELFT> void RelocationSection<ELFT>::finalize() {
Eugene Leviant9230db92016-11-17 09:16:34 +00001044 this->Link = In<ELFT>::DynSymTab ? In<ELFT>::DynSymTab->OutSec->SectionIndex
1045 : In<ELFT>::SymTab->OutSec->SectionIndex;
Eugene Levianta96d9022016-11-16 10:02:27 +00001046
1047 // Set required output section properties.
1048 this->OutSec->Link = this->Link;
1049 this->OutSec->Entsize = this->Entsize;
1050}
1051
Eugene Leviant9230db92016-11-17 09:16:34 +00001052template <class ELFT>
1053SymbolTableSection<ELFT>::SymbolTableSection(
1054 StringTableSection<ELFT> &StrTabSec)
1055 : SyntheticSection<ELFT>(StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0,
1056 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
1057 sizeof(uintX_t),
1058 StrTabSec.isDynamic() ? ".dynsym" : ".symtab"),
1059 StrTabSec(StrTabSec) {
1060 this->Entsize = sizeof(Elf_Sym);
1061}
1062
1063// Orders symbols according to their positions in the GOT,
1064// in compliance with MIPS ABI rules.
1065// See "Global Offset Table" in Chapter 5 in the following document
1066// for detailed description:
1067// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1068static bool sortMipsSymbols(const SymbolBody *L, const SymbolBody *R) {
1069 // Sort entries related to non-local preemptible symbols by GOT indexes.
1070 // All other entries go to the first part of GOT in arbitrary order.
1071 bool LIsInLocalGot = !L->IsInGlobalMipsGot;
1072 bool RIsInLocalGot = !R->IsInGlobalMipsGot;
1073 if (LIsInLocalGot || RIsInLocalGot)
1074 return !RIsInLocalGot;
1075 return L->GotIndex < R->GotIndex;
1076}
1077
Eugene Leviant9230db92016-11-17 09:16:34 +00001078template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
1079 this->OutSec->Link = this->Link = StrTabSec.OutSec->SectionIndex;
1080 this->OutSec->Info = this->Info = NumLocals + 1;
1081 this->OutSec->Entsize = this->Entsize;
1082
George Rimar190bac52017-01-23 14:07:23 +00001083 if (Config->Relocatable)
1084 return;
1085
1086 if (!StrTabSec.isDynamic()) {
1087 auto GlobBegin = Symbols.begin() + NumLocals;
Peter Smithccb34ef2017-01-24 10:43:40 +00001088 auto It = std::stable_partition(
1089 GlobBegin, Symbols.end(), [](const SymbolTableEntry &S) {
1090 return S.Symbol->symbol()->computeBinding() == STB_LOCAL;
1091 });
1092 // update sh_info with number of Global symbols output with computed
1093 // binding of STB_LOCAL
George Rimar3a934ab2017-01-24 16:07:18 +00001094 this->OutSec->Info = this->Info = 1 + (It - Symbols.begin());
Eugene Leviant9230db92016-11-17 09:16:34 +00001095 return;
1096 }
1097
Eugene Leviantbe809a72016-11-18 06:44:18 +00001098 if (In<ELFT>::GnuHashTab)
Eugene Leviant9230db92016-11-17 09:16:34 +00001099 // NB: It also sorts Symbols to meet the GNU hash table requirements.
Eugene Leviantbe809a72016-11-18 06:44:18 +00001100 In<ELFT>::GnuHashTab->addSymbols(Symbols);
Eugene Leviant9230db92016-11-17 09:16:34 +00001101 else if (Config->EMachine == EM_MIPS)
1102 std::stable_sort(Symbols.begin(), Symbols.end(),
1103 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
1104 return sortMipsSymbols(L.Symbol, R.Symbol);
1105 });
1106 size_t I = 0;
1107 for (const SymbolTableEntry &S : Symbols)
1108 S.Symbol->DynsymIndex = ++I;
1109}
1110
George Rimar190bac52017-01-23 14:07:23 +00001111template <class ELFT> void SymbolTableSection<ELFT>::addGlobal(SymbolBody *B) {
Eugene Leviant9230db92016-11-17 09:16:34 +00001112 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
1113}
1114
George Rimar190bac52017-01-23 14:07:23 +00001115template <class ELFT> void SymbolTableSection<ELFT>::addLocal(SymbolBody *B) {
1116 assert(!StrTabSec.isDynamic());
1117 ++NumLocals;
1118 Symbols.push_back({B, StrTabSec.addString(B->getName())});
1119}
1120
1121template <class ELFT>
1122size_t SymbolTableSection<ELFT>::getSymbolIndex(SymbolBody *Body) {
1123 auto I = llvm::find_if(
1124 Symbols, [&](const SymbolTableEntry &E) { return E.Symbol == Body; });
1125 return I - Symbols.begin() + 1;
1126}
1127
Eugene Leviant9230db92016-11-17 09:16:34 +00001128template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
1129 Buf += sizeof(Elf_Sym);
1130
1131 // All symbols with STB_LOCAL binding precede the weak and global symbols.
1132 // .dynsym only contains global symbols.
1133 if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic())
1134 writeLocalSymbols(Buf);
1135
1136 writeGlobalSymbols(Buf);
1137}
1138
1139template <class ELFT>
1140void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
1141 // Iterate over all input object files to copy their local symbols
1142 // to the output symbol table pointed by Buf.
Eugene Leviant9230db92016-11-17 09:16:34 +00001143
George Rimar190bac52017-01-23 14:07:23 +00001144 for (auto I = Symbols.begin(); I != Symbols.begin() + NumLocals; ++I) {
1145 const DefinedRegular<ELFT> &Body = *cast<DefinedRegular<ELFT>>(I->Symbol);
1146 InputSectionBase<ELFT> *Section = Body.Section;
1147 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
1148
1149 if (!Section) {
1150 ESym->st_shndx = SHN_ABS;
1151 ESym->st_value = Body.Value;
1152 } else {
1153 const OutputSectionBase *OutSec = Section->OutSec;
1154 ESym->st_shndx = OutSec->SectionIndex;
1155 ESym->st_value = OutSec->Addr + Section->getOffset(Body);
Eugene Leviant9230db92016-11-17 09:16:34 +00001156 }
George Rimar190bac52017-01-23 14:07:23 +00001157 ESym->st_name = I->StrTabOffset;
1158 ESym->st_size = Body.template getSize<ELFT>();
1159 ESym->setBindingAndType(STB_LOCAL, Body.Type);
1160 Buf += sizeof(*ESym);
Eugene Leviant9230db92016-11-17 09:16:34 +00001161 }
1162}
1163
1164template <class ELFT>
1165void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
1166 // Write the internal symbol table contents to the output symbol table
1167 // pointed by Buf.
1168 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
George Rimar190bac52017-01-23 14:07:23 +00001169
1170 for (auto I = Symbols.begin() + NumLocals; I != Symbols.end(); ++I) {
1171 const SymbolTableEntry &S = *I;
Eugene Leviant9230db92016-11-17 09:16:34 +00001172 SymbolBody *Body = S.Symbol;
1173 size_t StrOff = S.StrTabOffset;
1174
1175 uint8_t Type = Body->Type;
1176 uintX_t Size = Body->getSize<ELFT>();
1177
Rui Ueyama9f0f8b82017-01-10 21:52:56 +00001178 ESym->setBindingAndType(Body->symbol()->computeBinding(), Type);
Eugene Leviant9230db92016-11-17 09:16:34 +00001179 ESym->st_size = Size;
1180 ESym->st_name = StrOff;
1181 ESym->setVisibility(Body->symbol()->Visibility);
1182 ESym->st_value = Body->getVA<ELFT>();
1183
Rui Ueyamab2a23cf2017-01-24 03:41:20 +00001184 if (const OutputSectionBase *OutSec = getOutputSection(Body)) {
Eugene Leviant9230db92016-11-17 09:16:34 +00001185 ESym->st_shndx = OutSec->SectionIndex;
Rui Ueyamab2a23cf2017-01-24 03:41:20 +00001186 } else if (isa<DefinedRegular<ELFT>>(Body)) {
Eugene Leviant9230db92016-11-17 09:16:34 +00001187 ESym->st_shndx = SHN_ABS;
Rui Ueyamab2a23cf2017-01-24 03:41:20 +00001188 } else if (isa<DefinedCommon>(Body)) {
1189 ESym->st_shndx = SHN_COMMON;
1190 ESym->st_value = cast<DefinedCommon>(Body)->Alignment;
1191 }
Eugene Leviant9230db92016-11-17 09:16:34 +00001192
1193 if (Config->EMachine == EM_MIPS) {
1194 // On MIPS we need to mark symbol which has a PLT entry and requires
1195 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
1196 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
1197 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
1198 if (Body->isInPlt() && Body->NeedsCopyOrPltAddr)
1199 ESym->st_other |= STO_MIPS_PLT;
1200 if (Config->Relocatable) {
1201 auto *D = dyn_cast<DefinedRegular<ELFT>>(Body);
1202 if (D && D->isMipsPIC())
1203 ESym->st_other |= STO_MIPS_PIC;
1204 }
1205 }
1206 ++ESym;
1207 }
1208}
1209
1210template <class ELFT>
1211const OutputSectionBase *
1212SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
1213 switch (Sym->kind()) {
1214 case SymbolBody::DefinedSyntheticKind:
Rui Ueyama4f2f50d2016-12-21 08:40:09 +00001215 return cast<DefinedSynthetic>(Sym)->Section;
Eugene Leviant9230db92016-11-17 09:16:34 +00001216 case SymbolBody::DefinedRegularKind: {
1217 auto &D = cast<DefinedRegular<ELFT>>(*Sym);
1218 if (D.Section)
1219 return D.Section->OutSec;
1220 break;
1221 }
1222 case SymbolBody::DefinedCommonKind:
Rui Ueyamab2a23cf2017-01-24 03:41:20 +00001223 if (!Config->DefineCommon)
1224 return nullptr;
Eugene Leviant9230db92016-11-17 09:16:34 +00001225 return In<ELFT>::Common->OutSec;
Peter Collingbournefeb66292017-01-10 01:21:50 +00001226 case SymbolBody::SharedKind: {
1227 auto &SS = cast<SharedSymbol<ELFT>>(*Sym);
1228 if (SS.needsCopy())
1229 return SS.getBssSectionForCopy();
Eugene Leviant9230db92016-11-17 09:16:34 +00001230 break;
Peter Collingbournefeb66292017-01-10 01:21:50 +00001231 }
Eugene Leviant9230db92016-11-17 09:16:34 +00001232 case SymbolBody::UndefinedKind:
1233 case SymbolBody::LazyArchiveKind:
1234 case SymbolBody::LazyObjectKind:
1235 break;
1236 }
1237 return nullptr;
1238}
1239
Eugene Leviantbe809a72016-11-18 06:44:18 +00001240template <class ELFT>
1241GnuHashTableSection<ELFT>::GnuHashTableSection()
1242 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_HASH, sizeof(uintX_t),
1243 ".gnu.hash") {
1244 this->Entsize = ELFT::Is64Bits ? 0 : 4;
1245}
1246
1247template <class ELFT>
1248unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
1249 if (!NumHashed)
1250 return 0;
1251
1252 // These values are prime numbers which are not greater than 2^(N-1) + 1.
1253 // In result, for any particular NumHashed we return a prime number
1254 // which is not greater than NumHashed.
1255 static const unsigned Primes[] = {
1256 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
1257 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
1258
1259 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
1260 array_lengthof(Primes) - 1)];
1261}
1262
1263// Bloom filter estimation: at least 8 bits for each hashed symbol.
1264// GNU Hash table requirement: it should be a power of 2,
1265// the minimum value is 1, even for an empty table.
1266// Expected results for a 32-bit target:
1267// calcMaskWords(0..4) = 1
1268// calcMaskWords(5..8) = 2
1269// calcMaskWords(9..16) = 4
1270// For a 64-bit target:
1271// calcMaskWords(0..8) = 1
1272// calcMaskWords(9..16) = 2
1273// calcMaskWords(17..32) = 4
1274template <class ELFT>
1275unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
1276 if (!NumHashed)
1277 return 1;
1278 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
1279}
1280
1281template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
1282 unsigned NumHashed = Symbols.size();
1283 NBuckets = calcNBuckets(NumHashed);
1284 MaskWords = calcMaskWords(NumHashed);
1285 // Second hash shift estimation: just predefined values.
1286 Shift2 = ELFT::Is64Bits ? 6 : 5;
1287
1288 this->OutSec->Entsize = this->Entsize;
1289 this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1290 this->Size = sizeof(Elf_Word) * 4 // Header
1291 + sizeof(Elf_Off) * MaskWords // Bloom Filter
1292 + sizeof(Elf_Word) * NBuckets // Hash Buckets
1293 + sizeof(Elf_Word) * NumHashed; // Hash Values
1294}
1295
1296template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1297 writeHeader(Buf);
1298 if (Symbols.empty())
1299 return;
1300 writeBloomFilter(Buf);
1301 writeHashTable(Buf);
1302}
1303
1304template <class ELFT>
1305void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
1306 auto *P = reinterpret_cast<Elf_Word *>(Buf);
1307 *P++ = NBuckets;
1308 *P++ = In<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
1309 *P++ = MaskWords;
1310 *P++ = Shift2;
1311 Buf = reinterpret_cast<uint8_t *>(P);
1312}
1313
1314template <class ELFT>
1315void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
1316 unsigned C = sizeof(Elf_Off) * 8;
1317
1318 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
1319 for (const SymbolData &Sym : Symbols) {
1320 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
1321 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
1322 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
1323 Masks[Pos] |= V;
1324 }
1325 Buf += sizeof(Elf_Off) * MaskWords;
1326}
1327
1328template <class ELFT>
1329void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
1330 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
1331 Elf_Word *Values = Buckets + NBuckets;
1332
1333 int PrevBucket = -1;
1334 int I = 0;
1335 for (const SymbolData &Sym : Symbols) {
1336 int Bucket = Sym.Hash % NBuckets;
1337 assert(PrevBucket <= Bucket);
1338 if (Bucket != PrevBucket) {
1339 Buckets[Bucket] = Sym.Body->DynsymIndex;
1340 PrevBucket = Bucket;
1341 if (I > 0)
1342 Values[I - 1] |= 1;
1343 }
1344 Values[I] = Sym.Hash & ~1;
1345 ++I;
1346 }
1347 if (I > 0)
1348 Values[I - 1] |= 1;
1349}
1350
1351static uint32_t hashGnu(StringRef Name) {
1352 uint32_t H = 5381;
1353 for (uint8_t C : Name)
1354 H = (H << 5) + H + C;
1355 return H;
1356}
1357
1358// Add symbols to this symbol hash table. Note that this function
1359// destructively sort a given vector -- which is needed because
1360// GNU-style hash table places some sorting requirements.
1361template <class ELFT>
1362void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) {
1363 // Ideally this will just be 'auto' but GCC 6.1 is not able
1364 // to deduce it correctly.
1365 std::vector<SymbolTableEntry>::iterator Mid =
1366 std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
1367 return S.Symbol->isUndefined();
1368 });
1369 if (Mid == V.end())
1370 return;
1371 for (auto I = Mid, E = V.end(); I != E; ++I) {
1372 SymbolBody *B = I->Symbol;
1373 size_t StrOff = I->StrTabOffset;
1374 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
1375 }
1376
1377 unsigned NBuckets = calcNBuckets(Symbols.size());
1378 std::stable_sort(Symbols.begin(), Symbols.end(),
1379 [&](const SymbolData &L, const SymbolData &R) {
1380 return L.Hash % NBuckets < R.Hash % NBuckets;
1381 });
1382
1383 V.erase(Mid, V.end());
1384 for (const SymbolData &Sym : Symbols)
1385 V.push_back({Sym.Body, Sym.STName});
1386}
1387
Eugene Leviantb96e8092016-11-18 09:06:47 +00001388template <class ELFT>
1389HashTableSection<ELFT>::HashTableSection()
1390 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_HASH, sizeof(Elf_Word), ".hash") {
1391 this->Entsize = sizeof(Elf_Word);
1392}
1393
1394template <class ELFT> void HashTableSection<ELFT>::finalize() {
1395 this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1396 this->OutSec->Entsize = this->Entsize;
1397
1398 unsigned NumEntries = 2; // nbucket and nchain.
1399 NumEntries += In<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
1400
1401 // Create as many buckets as there are symbols.
1402 // FIXME: This is simplistic. We can try to optimize it, but implementing
1403 // support for SHT_GNU_HASH is probably even more profitable.
1404 NumEntries += In<ELFT>::DynSymTab->getNumSymbols();
1405 this->Size = NumEntries * sizeof(Elf_Word);
1406}
1407
1408template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
1409 unsigned NumSymbols = In<ELFT>::DynSymTab->getNumSymbols();
1410 auto *P = reinterpret_cast<Elf_Word *>(Buf);
1411 *P++ = NumSymbols; // nbucket
1412 *P++ = NumSymbols; // nchain
1413
1414 Elf_Word *Buckets = P;
1415 Elf_Word *Chains = P + NumSymbols;
1416
1417 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
1418 SymbolBody *Body = S.Symbol;
1419 StringRef Name = Body->getName();
1420 unsigned I = Body->DynsymIndex;
1421 uint32_t Hash = hashSysV(Name) % NumSymbols;
1422 Chains[I] = Buckets[Hash];
1423 Buckets[Hash] = I;
1424 }
1425}
1426
Eugene Leviantff23d3e2016-11-18 14:35:03 +00001427template <class ELFT>
1428PltSection<ELFT>::PltSection()
1429 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16,
1430 ".plt") {}
1431
1432template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
1433 // At beginning of PLT, we have code to call the dynamic linker
1434 // to resolve dynsyms at runtime. Write such code.
1435 Target->writePltHeader(Buf);
1436 size_t Off = Target->PltHeaderSize;
1437
1438 for (auto &I : Entries) {
1439 const SymbolBody *B = I.first;
1440 unsigned RelOff = I.second;
1441 uint64_t Got = B->getGotPltVA<ELFT>();
1442 uint64_t Plt = this->getVA() + Off;
1443 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
1444 Off += Target->PltEntrySize;
1445 }
1446}
1447
1448template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
1449 Sym.PltIndex = Entries.size();
1450 unsigned RelOff = In<ELFT>::RelaPlt->getRelocOffset();
1451 Entries.push_back(std::make_pair(&Sym, RelOff));
1452}
1453
1454template <class ELFT> size_t PltSection<ELFT>::getSize() const {
1455 return Target->PltHeaderSize + Entries.size() * Target->PltEntrySize;
1456}
1457
Peter Smith96943762017-01-25 10:31:16 +00001458// Some architectures such as additional symbols in the PLT section. For
1459// example ARM uses mapping symbols to aid disassembly
1460template <class ELFT> void PltSection<ELFT>::addSymbols() {
1461 Target->addPltHeaderSymbols(this);
1462 size_t Off = Target->PltHeaderSize;
1463 for (size_t I = 0; I < Entries.size(); ++I) {
1464 Target->addPltSymbols(this, Off);
1465 Off += Target->PltEntrySize;
1466 }
1467}
1468
Eugene Levianta113a412016-11-21 09:24:43 +00001469template <class ELFT>
Peter Smithbaffdb82016-12-08 12:58:55 +00001470IpltSection<ELFT>::IpltSection()
1471 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS, 16,
1472 ".plt") {}
1473
1474template <class ELFT> void IpltSection<ELFT>::writeTo(uint8_t *Buf) {
1475 // The IRelative relocations do not support lazy binding so no header is
1476 // needed
1477 size_t Off = 0;
1478 for (auto &I : Entries) {
1479 const SymbolBody *B = I.first;
1480 unsigned RelOff = I.second + In<ELFT>::Plt->getSize();
1481 uint64_t Got = B->getGotPltVA<ELFT>();
1482 uint64_t Plt = this->getVA() + Off;
1483 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
1484 Off += Target->PltEntrySize;
1485 }
1486}
1487
1488template <class ELFT> void IpltSection<ELFT>::addEntry(SymbolBody &Sym) {
1489 Sym.PltIndex = Entries.size();
1490 Sym.IsInIplt = true;
1491 unsigned RelOff = In<ELFT>::RelaIplt->getRelocOffset();
1492 Entries.push_back(std::make_pair(&Sym, RelOff));
1493}
1494
1495template <class ELFT> size_t IpltSection<ELFT>::getSize() const {
1496 return Entries.size() * Target->PltEntrySize;
1497}
1498
Peter Smith96943762017-01-25 10:31:16 +00001499template <class ELFT> void IpltSection<ELFT>::addSymbols() {
1500 size_t Off = 0;
1501 for (size_t I = 0; I < Entries.size(); ++I) {
1502 Target->addPltSymbols(this, Off);
1503 Off += Target->PltEntrySize;
1504 }
1505}
1506
Peter Smithbaffdb82016-12-08 12:58:55 +00001507template <class ELFT>
Eugene Levianta113a412016-11-21 09:24:43 +00001508GdbIndexSection<ELFT>::GdbIndexSection()
George Rimarec02b8d2016-12-15 12:07:53 +00001509 : SyntheticSection<ELFT>(0, SHT_PROGBITS, 1, ".gdb_index"),
1510 StringPool(llvm::StringTableBuilder::ELF) {}
Eugene Levianta113a412016-11-21 09:24:43 +00001511
1512template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() {
George Rimar8b547392016-12-15 09:08:13 +00001513 for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections)
1514 if (InputSection<ELFT> *IS = dyn_cast<InputSection<ELFT>>(S))
1515 if (IS->OutSec && IS->Name == ".debug_info")
1516 readDwarf(IS);
Eugene Levianta113a412016-11-21 09:24:43 +00001517}
1518
George Rimarec02b8d2016-12-15 12:07:53 +00001519// Iterative hash function for symbol's name is described in .gdb_index format
1520// specification. Note that we use one for version 5 to 7 here, it is different
1521// for version 4.
1522static uint32_t hash(StringRef Str) {
1523 uint32_t R = 0;
1524 for (uint8_t C : Str)
1525 R = R * 67 + tolower(C) - 113;
1526 return R;
1527}
1528
Eugene Levianta113a412016-11-21 09:24:43 +00001529template <class ELFT>
1530void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) {
George Rimar8b547392016-12-15 09:08:13 +00001531 GdbIndexBuilder<ELFT> Builder(I);
1532 if (ErrorCount)
1533 return;
1534
1535 size_t CuId = CompilationUnits.size();
1536 std::vector<std::pair<uintX_t, uintX_t>> CuList = Builder.readCUList();
Eugene Levianta113a412016-11-21 09:24:43 +00001537 CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end());
George Rimar8b547392016-12-15 09:08:13 +00001538
1539 std::vector<AddressEntry<ELFT>> AddrArea = Builder.readAddressArea(CuId);
1540 AddressArea.insert(AddressArea.end(), AddrArea.begin(), AddrArea.end());
George Rimarec02b8d2016-12-15 12:07:53 +00001541
1542 std::vector<std::pair<StringRef, uint8_t>> NamesAndTypes =
1543 Builder.readPubNamesAndTypes();
1544
1545 for (std::pair<StringRef, uint8_t> &Pair : NamesAndTypes) {
1546 uint32_t Hash = hash(Pair.first);
1547 size_t Offset = StringPool.add(Pair.first);
1548
1549 bool IsNew;
1550 GdbSymbol *Sym;
1551 std::tie(IsNew, Sym) = SymbolTable.add(Hash, Offset);
1552 if (IsNew) {
1553 Sym->CuVectorIndex = CuVectors.size();
1554 CuVectors.push_back({{CuId, Pair.second}});
1555 continue;
1556 }
1557
1558 std::vector<std::pair<uint32_t, uint8_t>> &CuVec =
1559 CuVectors[Sym->CuVectorIndex];
1560 CuVec.push_back({CuId, Pair.second});
1561 }
Eugene Levianta113a412016-11-21 09:24:43 +00001562}
1563
1564template <class ELFT> void GdbIndexSection<ELFT>::finalize() {
George Rimar8b547392016-12-15 09:08:13 +00001565 if (Finalized)
1566 return;
1567 Finalized = true;
1568
Eugene Levianta113a412016-11-21 09:24:43 +00001569 parseDebugSections();
1570
1571 // GdbIndex header consist from version fields
1572 // and 5 more fields with different kinds of offsets.
1573 CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize;
George Rimar8b547392016-12-15 09:08:13 +00001574 SymTabOffset = CuTypesOffset + AddressArea.size() * AddressEntrySize;
George Rimarec02b8d2016-12-15 12:07:53 +00001575
1576 ConstantPoolOffset =
1577 SymTabOffset + SymbolTable.getCapacity() * SymTabEntrySize;
1578
1579 for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) {
1580 CuVectorsOffset.push_back(CuVectorsSize);
1581 CuVectorsSize += OffsetTypeSize * (CuVec.size() + 1);
1582 }
1583 StringPoolOffset = ConstantPoolOffset + CuVectorsSize;
1584
1585 StringPool.finalizeInOrder();
George Rimar8b547392016-12-15 09:08:13 +00001586}
1587
1588template <class ELFT> size_t GdbIndexSection<ELFT>::getSize() const {
1589 const_cast<GdbIndexSection<ELFT> *>(this)->finalize();
George Rimarec02b8d2016-12-15 12:07:53 +00001590 return StringPoolOffset + StringPool.getSize();
Eugene Levianta113a412016-11-21 09:24:43 +00001591}
1592
1593template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) {
George Rimarec02b8d2016-12-15 12:07:53 +00001594 write32le(Buf, 7); // Write version.
1595 write32le(Buf + 4, CuListOffset); // CU list offset.
1596 write32le(Buf + 8, CuTypesOffset); // Types CU list offset.
1597 write32le(Buf + 12, CuTypesOffset); // Address area offset.
1598 write32le(Buf + 16, SymTabOffset); // Symbol table offset.
1599 write32le(Buf + 20, ConstantPoolOffset); // Constant pool offset.
Eugene Levianta113a412016-11-21 09:24:43 +00001600 Buf += 24;
1601
1602 // Write the CU list.
1603 for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) {
1604 write64le(Buf, CU.first);
1605 write64le(Buf + 8, CU.second);
1606 Buf += 16;
1607 }
George Rimar8b547392016-12-15 09:08:13 +00001608
1609 // Write the address area.
1610 for (AddressEntry<ELFT> &E : AddressArea) {
1611 uintX_t BaseAddr = E.Section->OutSec->Addr + E.Section->getOffset(0);
1612 write64le(Buf, BaseAddr + E.LowAddress);
1613 write64le(Buf + 8, BaseAddr + E.HighAddress);
1614 write32le(Buf + 16, E.CuIndex);
1615 Buf += 20;
1616 }
George Rimarec02b8d2016-12-15 12:07:53 +00001617
1618 // Write the symbol table.
1619 for (size_t I = 0; I < SymbolTable.getCapacity(); ++I) {
1620 GdbSymbol *Sym = SymbolTable.getSymbol(I);
1621 if (Sym) {
1622 size_t NameOffset =
1623 Sym->NameOffset + StringPoolOffset - ConstantPoolOffset;
1624 size_t CuVectorOffset = CuVectorsOffset[Sym->CuVectorIndex];
1625 write32le(Buf, NameOffset);
1626 write32le(Buf + 4, CuVectorOffset);
1627 }
1628 Buf += 8;
1629 }
1630
1631 // Write the CU vectors into the constant pool.
1632 for (std::vector<std::pair<uint32_t, uint8_t>> &CuVec : CuVectors) {
1633 write32le(Buf, CuVec.size());
1634 Buf += 4;
1635 for (std::pair<uint32_t, uint8_t> &P : CuVec) {
1636 uint32_t Index = P.first;
1637 uint8_t Flags = P.second;
1638 Index |= Flags << 24;
1639 write32le(Buf, Index);
1640 Buf += 4;
1641 }
1642 }
1643
1644 StringPool.write(Buf);
Eugene Levianta113a412016-11-21 09:24:43 +00001645}
1646
George Rimar3fb5a6d2016-11-29 16:05:27 +00001647template <class ELFT> bool GdbIndexSection<ELFT>::empty() const {
1648 return !Out<ELFT>::DebugInfo;
1649}
1650
Eugene Leviant952eb4d2016-11-21 15:52:10 +00001651template <class ELFT>
1652EhFrameHeader<ELFT>::EhFrameHeader()
1653 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_PROGBITS, 1, ".eh_frame_hdr") {}
1654
1655// .eh_frame_hdr contains a binary search table of pointers to FDEs.
1656// Each entry of the search table consists of two values,
1657// the starting PC from where FDEs covers, and the FDE's address.
1658// It is sorted by PC.
1659template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
1660 const endianness E = ELFT::TargetEndianness;
1661
1662 // Sort the FDE list by their PC and uniqueify. Usually there is only
1663 // one FDE for a PC (i.e. function), but if ICF merges two functions
1664 // into one, there can be more than one FDEs pointing to the address.
1665 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
1666 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
1667 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
1668 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
1669
1670 Buf[0] = 1;
1671 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
1672 Buf[2] = DW_EH_PE_udata4;
1673 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
1674 write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->getVA() - 4);
1675 write32<E>(Buf + 8, Fdes.size());
1676 Buf += 12;
1677
1678 uintX_t VA = this->getVA();
1679 for (FdeData &Fde : Fdes) {
1680 write32<E>(Buf, Fde.Pc - VA);
1681 write32<E>(Buf + 4, Fde.FdeVA - VA);
1682 Buf += 8;
1683 }
1684}
1685
1686template <class ELFT> size_t EhFrameHeader<ELFT>::getSize() const {
1687 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
1688 return 12 + Out<ELFT>::EhFrame->NumFdes * 8;
1689}
1690
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001691template <class ELFT>
Rui Ueyamab38ddb12016-11-21 19:46:04 +00001692void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
1693 Fdes.push_back({Pc, FdeVA});
1694}
1695
George Rimar11992c862016-11-25 08:05:41 +00001696template <class ELFT> bool EhFrameHeader<ELFT>::empty() const {
1697 return Out<ELFT>::EhFrame->empty();
1698}
1699
Rui Ueyamab38ddb12016-11-21 19:46:04 +00001700template <class ELFT>
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001701VersionDefinitionSection<ELFT>::VersionDefinitionSection()
1702 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_verdef, sizeof(uint32_t),
1703 ".gnu.version_d") {}
1704
1705static StringRef getFileDefName() {
1706 if (!Config->SoName.empty())
1707 return Config->SoName;
1708 return Config->OutputFile;
1709}
1710
1711template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
1712 FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName());
1713 for (VersionDefinition &V : Config->VersionDefinitions)
1714 V.NameOff = In<ELFT>::DynStrTab->addString(V.Name);
1715
1716 this->OutSec->Link = this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
1717
1718 // sh_info should be set to the number of definitions. This fact is missed in
1719 // documentation, but confirmed by binutils community:
1720 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
1721 this->OutSec->Info = this->Info = getVerDefNum();
1722}
1723
1724template <class ELFT>
1725void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1726 StringRef Name, size_t NameOff) {
1727 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
1728 Verdef->vd_version = 1;
1729 Verdef->vd_cnt = 1;
1730 Verdef->vd_aux = sizeof(Elf_Verdef);
1731 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1732 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
1733 Verdef->vd_ndx = Index;
1734 Verdef->vd_hash = hashSysV(Name);
1735
1736 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
1737 Verdaux->vda_name = NameOff;
1738 Verdaux->vda_next = 0;
1739}
1740
1741template <class ELFT>
1742void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
1743 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
1744
1745 for (VersionDefinition &V : Config->VersionDefinitions) {
1746 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1747 writeOne(Buf, V.Id, V.Name, V.NameOff);
1748 }
1749
1750 // Need to terminate the last version definition.
1751 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
1752 Verdef->vd_next = 0;
1753}
1754
1755template <class ELFT> size_t VersionDefinitionSection<ELFT>::getSize() const {
1756 return (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
1757}
1758
1759template <class ELFT>
1760VersionTableSection<ELFT>::VersionTableSection()
1761 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_versym, sizeof(uint16_t),
1762 ".gnu.version") {}
1763
1764template <class ELFT> void VersionTableSection<ELFT>::finalize() {
1765 this->OutSec->Entsize = this->Entsize = sizeof(Elf_Versym);
1766 // At the moment of june 2016 GNU docs does not mention that sh_link field
1767 // should be set, but Sun docs do. Also readelf relies on this field.
1768 this->OutSec->Link = this->Link = In<ELFT>::DynSymTab->OutSec->SectionIndex;
1769}
1770
1771template <class ELFT> size_t VersionTableSection<ELFT>::getSize() const {
1772 return sizeof(Elf_Versym) * (In<ELFT>::DynSymTab->getSymbols().size() + 1);
1773}
1774
1775template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
1776 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
1777 for (const SymbolTableEntry &S : In<ELFT>::DynSymTab->getSymbols()) {
1778 OutVersym->vs_index = S.Symbol->symbol()->VersionId;
1779 ++OutVersym;
1780 }
1781}
1782
George Rimar11992c862016-11-25 08:05:41 +00001783template <class ELFT> bool VersionTableSection<ELFT>::empty() const {
1784 return !In<ELFT>::VerDef && In<ELFT>::VerNeed->empty();
1785}
1786
Eugene Leviante9bab5d2016-11-21 16:59:33 +00001787template <class ELFT>
1788VersionNeedSection<ELFT>::VersionNeedSection()
1789 : SyntheticSection<ELFT>(SHF_ALLOC, SHT_GNU_verneed, sizeof(uint32_t),
1790 ".gnu.version_r") {
1791 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
1792 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
1793 // First identifiers are reserved by verdef section if it exist.
1794 NextIndex = getVerDefNum() + 1;
1795}
1796
1797template <class ELFT>
1798void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
1799 if (!SS->Verdef) {
1800 SS->symbol()->VersionId = VER_NDX_GLOBAL;
1801 return;
1802 }
1803 SharedFile<ELFT> *F = SS->file();
1804 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
1805 // to create one by adding it to our needed list and creating a dynstr entry
1806 // for the soname.
1807 if (F->VerdefMap.empty())
1808 Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())});
1809 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
1810 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
1811 // prepare to create one by allocating a version identifier and creating a
1812 // dynstr entry for the version name.
1813 if (NV.Index == 0) {
1814 NV.StrTab = In<ELFT>::DynStrTab->addString(
1815 SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
1816 NV.Index = NextIndex++;
1817 }
1818 SS->symbol()->VersionId = NV.Index;
1819}
1820
1821template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
1822 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
1823 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
1824 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
1825
1826 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
1827 // Create an Elf_Verneed for this DSO.
1828 Verneed->vn_version = 1;
1829 Verneed->vn_cnt = P.first->VerdefMap.size();
1830 Verneed->vn_file = P.second;
1831 Verneed->vn_aux =
1832 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
1833 Verneed->vn_next = sizeof(Elf_Verneed);
1834 ++Verneed;
1835
1836 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
1837 // VerdefMap, which will only contain references to needed version
1838 // definitions. Each Elf_Vernaux is based on the information contained in
1839 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
1840 // pointers, but is deterministic because the pointers refer to Elf_Verdef
1841 // data structures within a single input file.
1842 for (auto &NV : P.first->VerdefMap) {
1843 Vernaux->vna_hash = NV.first->vd_hash;
1844 Vernaux->vna_flags = 0;
1845 Vernaux->vna_other = NV.second.Index;
1846 Vernaux->vna_name = NV.second.StrTab;
1847 Vernaux->vna_next = sizeof(Elf_Vernaux);
1848 ++Vernaux;
1849 }
1850
1851 Vernaux[-1].vna_next = 0;
1852 }
1853 Verneed[-1].vn_next = 0;
1854}
1855
1856template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
1857 this->OutSec->Link = this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
1858 this->OutSec->Info = this->Info = Needed.size();
1859}
1860
1861template <class ELFT> size_t VersionNeedSection<ELFT>::getSize() const {
1862 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
1863 for (const std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
1864 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
1865 return Size;
1866}
1867
George Rimar11992c862016-11-25 08:05:41 +00001868template <class ELFT> bool VersionNeedSection<ELFT>::empty() const {
1869 return getNeedNum() == 0;
1870}
1871
Eugene Leviant17b7a572016-11-22 17:49:14 +00001872template <class ELFT>
Rui Ueyamabdfa1552016-11-22 19:24:52 +00001873MipsRldMapSection<ELFT>::MipsRldMapSection()
Eugene Leviant17b7a572016-11-22 17:49:14 +00001874 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
1875 sizeof(typename ELFT::uint), ".rld_map") {}
1876
Rui Ueyamabdfa1552016-11-22 19:24:52 +00001877template <class ELFT> void MipsRldMapSection<ELFT>::writeTo(uint8_t *Buf) {
Eugene Leviant17b7a572016-11-22 17:49:14 +00001878 // Apply filler from linker script.
1879 uint64_t Filler = Script<ELFT>::X->getFiller(this->Name);
1880 Filler = (Filler << 32) | Filler;
1881 memcpy(Buf, &Filler, getSize());
1882}
1883
Peter Smith719eb8e2016-11-24 11:43:55 +00001884template <class ELFT>
1885ARMExidxSentinelSection<ELFT>::ARMExidxSentinelSection()
1886 : SyntheticSection<ELFT>(SHF_ALLOC | SHF_LINK_ORDER, SHT_ARM_EXIDX,
1887 sizeof(typename ELFT::uint), ".ARM.exidx") {}
1888
1889// Write a terminating sentinel entry to the end of the .ARM.exidx table.
1890// This section will have been sorted last in the .ARM.exidx table.
1891// This table entry will have the form:
1892// | PREL31 upper bound of code that has exception tables | EXIDX_CANTUNWIND |
George Rimar879a6572016-12-15 15:38:58 +00001893template <class ELFT>
1894void ARMExidxSentinelSection<ELFT>::writeTo(uint8_t *Buf) {
Peter Smith719eb8e2016-11-24 11:43:55 +00001895 // Get the InputSection before us, we are by definition last
1896 auto RI = cast<OutputSection<ELFT>>(this->OutSec)->Sections.rbegin();
1897 InputSection<ELFT> *LE = *(++RI);
1898 InputSection<ELFT> *LC = cast<InputSection<ELFT>>(LE->getLinkOrderDep());
1899 uint64_t S = LC->OutSec->Addr + LC->getOffset(LC->getSize());
1900 uint64_t P = this->getVA();
1901 Target->relocateOne(Buf, R_ARM_PREL31, S - P);
1902 write32le(Buf + 4, 0x1);
1903}
1904
Rafael Espindola682a5bc2016-11-08 14:42:34 +00001905template InputSection<ELF32LE> *elf::createCommonSection();
1906template InputSection<ELF32BE> *elf::createCommonSection();
1907template InputSection<ELF64LE> *elf::createCommonSection();
1908template InputSection<ELF64BE> *elf::createCommonSection();
Rui Ueyamae8a61022016-11-05 23:05:47 +00001909
Rafael Espindolac0e47fb2016-11-08 14:56:27 +00001910template InputSection<ELF32LE> *elf::createInterpSection();
1911template InputSection<ELF32BE> *elf::createInterpSection();
1912template InputSection<ELF64LE> *elf::createInterpSection();
1913template InputSection<ELF64BE> *elf::createInterpSection();
Rui Ueyamae288eef2016-11-02 18:58:44 +00001914
Rui Ueyama3da3f062016-11-10 20:20:37 +00001915template MergeInputSection<ELF32LE> *elf::createCommentSection();
1916template MergeInputSection<ELF32BE> *elf::createCommentSection();
1917template MergeInputSection<ELF64LE> *elf::createCommentSection();
1918template MergeInputSection<ELF64BE> *elf::createCommentSection();
1919
Peter Smith96943762017-01-25 10:31:16 +00001920template SymbolBody *
1921elf::addSyntheticLocal<ELF32LE>(StringRef, uint8_t, ELF32LE::uint,
1922 ELF32LE::uint, InputSectionBase<ELF32LE> *);
1923template SymbolBody *
1924elf::addSyntheticLocal<ELF32BE>(StringRef, uint8_t, ELF32BE::uint,
1925 ELF32BE::uint, InputSectionBase<ELF32BE> *);
1926template SymbolBody *
1927elf::addSyntheticLocal<ELF64LE>(StringRef, uint8_t, ELF64LE::uint,
1928 ELF64LE::uint, InputSectionBase<ELF64LE> *);
1929template SymbolBody *
1930elf::addSyntheticLocal<ELF64BE>(StringRef, uint8_t, ELF64BE::uint,
1931 ELF64BE::uint, InputSectionBase<ELF64BE> *);
1932
Simon Atanasyanfa03b0f2016-11-09 21:37:06 +00001933template class elf::MipsAbiFlagsSection<ELF32LE>;
1934template class elf::MipsAbiFlagsSection<ELF32BE>;
1935template class elf::MipsAbiFlagsSection<ELF64LE>;
1936template class elf::MipsAbiFlagsSection<ELF64BE>;
1937
Simon Atanasyance02cf02016-11-09 21:36:56 +00001938template class elf::MipsOptionsSection<ELF32LE>;
1939template class elf::MipsOptionsSection<ELF32BE>;
1940template class elf::MipsOptionsSection<ELF64LE>;
1941template class elf::MipsOptionsSection<ELF64BE>;
1942
1943template class elf::MipsReginfoSection<ELF32LE>;
1944template class elf::MipsReginfoSection<ELF32BE>;
1945template class elf::MipsReginfoSection<ELF64LE>;
1946template class elf::MipsReginfoSection<ELF64BE>;
1947
Rui Ueyama6dc7fcb2016-11-01 20:28:21 +00001948template class elf::BuildIdSection<ELF32LE>;
1949template class elf::BuildIdSection<ELF32BE>;
1950template class elf::BuildIdSection<ELF64LE>;
1951template class elf::BuildIdSection<ELF64BE>;
1952
Eugene Leviantad4439e2016-11-11 11:33:32 +00001953template class elf::GotSection<ELF32LE>;
1954template class elf::GotSection<ELF32BE>;
1955template class elf::GotSection<ELF64LE>;
1956template class elf::GotSection<ELF64BE>;
1957
Simon Atanasyan725dc142016-11-16 21:01:02 +00001958template class elf::MipsGotSection<ELF32LE>;
1959template class elf::MipsGotSection<ELF32BE>;
1960template class elf::MipsGotSection<ELF64LE>;
1961template class elf::MipsGotSection<ELF64BE>;
1962
Eugene Leviant41ca3272016-11-10 09:48:29 +00001963template class elf::GotPltSection<ELF32LE>;
1964template class elf::GotPltSection<ELF32BE>;
1965template class elf::GotPltSection<ELF64LE>;
1966template class elf::GotPltSection<ELF64BE>;
Eugene Leviant22eb0262016-11-14 09:16:00 +00001967
Peter Smithbaffdb82016-12-08 12:58:55 +00001968template class elf::IgotPltSection<ELF32LE>;
1969template class elf::IgotPltSection<ELF32BE>;
1970template class elf::IgotPltSection<ELF64LE>;
1971template class elf::IgotPltSection<ELF64BE>;
1972
Eugene Leviant22eb0262016-11-14 09:16:00 +00001973template class elf::StringTableSection<ELF32LE>;
1974template class elf::StringTableSection<ELF32BE>;
1975template class elf::StringTableSection<ELF64LE>;
1976template class elf::StringTableSection<ELF64BE>;
Eugene Leviant6380ce22016-11-15 12:26:55 +00001977
1978template class elf::DynamicSection<ELF32LE>;
1979template class elf::DynamicSection<ELF32BE>;
1980template class elf::DynamicSection<ELF64LE>;
1981template class elf::DynamicSection<ELF64BE>;
Eugene Levianta96d9022016-11-16 10:02:27 +00001982
1983template class elf::RelocationSection<ELF32LE>;
1984template class elf::RelocationSection<ELF32BE>;
1985template class elf::RelocationSection<ELF64LE>;
1986template class elf::RelocationSection<ELF64BE>;
Eugene Leviant9230db92016-11-17 09:16:34 +00001987
1988template class elf::SymbolTableSection<ELF32LE>;
1989template class elf::SymbolTableSection<ELF32BE>;
1990template class elf::SymbolTableSection<ELF64LE>;
1991template class elf::SymbolTableSection<ELF64BE>;
Eugene Leviantbe809a72016-11-18 06:44:18 +00001992
1993template class elf::GnuHashTableSection<ELF32LE>;
1994template class elf::GnuHashTableSection<ELF32BE>;
1995template class elf::GnuHashTableSection<ELF64LE>;
1996template class elf::GnuHashTableSection<ELF64BE>;
Eugene Leviantb96e8092016-11-18 09:06:47 +00001997
1998template class elf::HashTableSection<ELF32LE>;
1999template class elf::HashTableSection<ELF32BE>;
2000template class elf::HashTableSection<ELF64LE>;
2001template class elf::HashTableSection<ELF64BE>;
Eugene Leviantff23d3e2016-11-18 14:35:03 +00002002
2003template class elf::PltSection<ELF32LE>;
2004template class elf::PltSection<ELF32BE>;
2005template class elf::PltSection<ELF64LE>;
2006template class elf::PltSection<ELF64BE>;
Eugene Levianta113a412016-11-21 09:24:43 +00002007
Peter Smithbaffdb82016-12-08 12:58:55 +00002008template class elf::IpltSection<ELF32LE>;
2009template class elf::IpltSection<ELF32BE>;
2010template class elf::IpltSection<ELF64LE>;
2011template class elf::IpltSection<ELF64BE>;
2012
Eugene Levianta113a412016-11-21 09:24:43 +00002013template class elf::GdbIndexSection<ELF32LE>;
2014template class elf::GdbIndexSection<ELF32BE>;
2015template class elf::GdbIndexSection<ELF64LE>;
2016template class elf::GdbIndexSection<ELF64BE>;
Eugene Leviant952eb4d2016-11-21 15:52:10 +00002017
2018template class elf::EhFrameHeader<ELF32LE>;
2019template class elf::EhFrameHeader<ELF32BE>;
2020template class elf::EhFrameHeader<ELF64LE>;
2021template class elf::EhFrameHeader<ELF64BE>;
Eugene Leviante9bab5d2016-11-21 16:59:33 +00002022
2023template class elf::VersionTableSection<ELF32LE>;
2024template class elf::VersionTableSection<ELF32BE>;
2025template class elf::VersionTableSection<ELF64LE>;
2026template class elf::VersionTableSection<ELF64BE>;
2027
2028template class elf::VersionNeedSection<ELF32LE>;
2029template class elf::VersionNeedSection<ELF32BE>;
2030template class elf::VersionNeedSection<ELF64LE>;
2031template class elf::VersionNeedSection<ELF64BE>;
2032
2033template class elf::VersionDefinitionSection<ELF32LE>;
2034template class elf::VersionDefinitionSection<ELF32BE>;
2035template class elf::VersionDefinitionSection<ELF64LE>;
2036template class elf::VersionDefinitionSection<ELF64BE>;
Eugene Leviant17b7a572016-11-22 17:49:14 +00002037
Rui Ueyamabdfa1552016-11-22 19:24:52 +00002038template class elf::MipsRldMapSection<ELF32LE>;
2039template class elf::MipsRldMapSection<ELF32BE>;
2040template class elf::MipsRldMapSection<ELF64LE>;
2041template class elf::MipsRldMapSection<ELF64BE>;
Peter Smith719eb8e2016-11-24 11:43:55 +00002042
2043template class elf::ARMExidxSentinelSection<ELF32LE>;
2044template class elf::ARMExidxSentinelSection<ELF32BE>;
2045template class elf::ARMExidxSentinelSection<ELF64LE>;
2046template class elf::ARMExidxSentinelSection<ELF64BE>;