*** empty log message ***
[deliverable/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
8d88c4ca 1/* X86-64 specific support for 64-bit ELF
67a4f2b7 2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006
3eb128b2 3 Free Software Foundation, Inc.
8d88c4ca
NC
4 Contributed by Jan Hubicka <jh@suse.cz>.
5
ae9a127f 6 This file is part of BFD, the Binary File Descriptor library.
8d88c4ca 7
ae9a127f
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
8d88c4ca 12
ae9a127f
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
8d88c4ca 17
ae9a127f
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
3e110533 20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
8d88c4ca
NC
21
22#include "bfd.h"
23#include "sysdep.h"
c434dee6 24#include "bfdlink.h"
8d88c4ca
NC
25#include "libbfd.h"
26#include "elf-bfd.h"
27
28#include "elf/x86-64.h"
29
8d88c4ca
NC
30/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
31#define MINUS_ONE (~ (bfd_vma) 0)
32
33/* The relocation "howto" table. Order of fields:
407443a3
AJ
34 type, size, bitsize, pc_relative, complain_on_overflow,
35 special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */
70256ad8
AJ
36static reloc_howto_type x86_64_elf_howto_table[] =
37{
b34976b6
AM
38 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
39 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
40 FALSE),
41 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
42 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
43 FALSE),
44 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
45 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
46 TRUE),
47 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
48 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
49 FALSE),
50 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
51 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
52 TRUE),
53 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
54 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
55 FALSE),
56 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
57 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
58 MINUS_ONE, FALSE),
59 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
60 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
61 MINUS_ONE, FALSE),
62 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
64 MINUS_ONE, FALSE),
65 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
66 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
67 0xffffffff, TRUE),
68 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
69 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
70 FALSE),
71 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
72 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
73 FALSE),
74 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
75 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
b0360d8c 76 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
b34976b6 77 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
ac2aa337 78 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
b34976b6
AM
79 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
80 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
81 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
82 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
84 MINUS_ONE, FALSE),
85 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
86 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
87 MINUS_ONE, FALSE),
88 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
89 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
90 MINUS_ONE, FALSE),
91 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
92 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
93 0xffffffff, TRUE),
94 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
95 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
96 0xffffffff, TRUE),
ac2aa337 97 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
b34976b6
AM
98 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
99 0xffffffff, FALSE),
100 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
101 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
102 0xffffffff, TRUE),
103 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
104 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
105 0xffffffff, FALSE),
d6ab8113
JB
106 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
107 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
108 TRUE),
109 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
110 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
111 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
112 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
113 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
114 FALSE, 0xffffffff, 0xffffffff, TRUE),
67a4f2b7
AO
115 EMPTY_HOWTO (27),
116 EMPTY_HOWTO (28),
117 EMPTY_HOWTO (29),
118 EMPTY_HOWTO (30),
119 EMPTY_HOWTO (31),
120 EMPTY_HOWTO (32),
121 EMPTY_HOWTO (33),
122 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
123 complain_overflow_bitfield, bfd_elf_generic_reloc,
124 "R_X86_64_GOTPC32_TLSDESC",
125 FALSE, 0xffffffff, 0xffffffff, TRUE),
126 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
127 complain_overflow_dont, bfd_elf_generic_reloc,
128 "R_X86_64_TLSDESC_CALL",
129 FALSE, 0, 0, FALSE),
130 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
131 complain_overflow_bitfield, bfd_elf_generic_reloc,
132 "R_X86_64_TLSDESC",
133 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
fe4770f4 134
a33d77bc
JB
135 /* We have a gap in the reloc numbers here.
136 R_X86_64_standard counts the number up to this point, and
137 R_X86_64_vt_offset is the value to subtract from a reloc type of
138 R_X86_64_GNU_VT* to form an index into this table. */
67a4f2b7 139#define R_X86_64_standard (R_X86_64_TLSDESC + 1)
a33d77bc
JB
140#define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
141
fe4770f4 142/* GNU extension to record C++ vtable hierarchy. */
b34976b6
AM
143 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
144 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
fe4770f4
AJ
145
146/* GNU extension to record C++ vtable member usage. */
b34976b6
AM
147 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
148 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
149 FALSE)
8d88c4ca
NC
150};
151
152/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
153struct elf_reloc_map
154{
8d88c4ca
NC
155 bfd_reloc_code_real_type bfd_reloc_val;
156 unsigned char elf_reloc_val;
157};
158
dc810e39 159static const struct elf_reloc_map x86_64_reloc_map[] =
8d88c4ca 160{
70256ad8
AJ
161 { BFD_RELOC_NONE, R_X86_64_NONE, },
162 { BFD_RELOC_64, R_X86_64_64, },
163 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
164 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
165 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
166 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
167 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
168 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
169 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
170 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
171 { BFD_RELOC_32, R_X86_64_32, },
172 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
173 { BFD_RELOC_16, R_X86_64_16, },
174 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
175 { BFD_RELOC_8, R_X86_64_8, },
176 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
bffbf940
JJ
177 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
178 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
179 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
180 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
181 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
182 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
183 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
184 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
d6ab8113
JB
185 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
186 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
187 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
67a4f2b7
AO
188 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
189 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
190 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
fe4770f4
AJ
191 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
192 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
193};
194
67a4f2b7
AO
195static reloc_howto_type *
196elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
197{
198 unsigned i;
199
200 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
201 || r_type >= (unsigned int) R_X86_64_max)
202 {
203 if (r_type >= (unsigned int) R_X86_64_standard)
204 {
205 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
206 abfd, (int) r_type);
207 r_type = R_X86_64_NONE;
208 }
209 i = r_type;
210 }
211 else
212 i = r_type - (unsigned int) R_X86_64_vt_offset;
213 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
214 return &x86_64_elf_howto_table[i];
215}
8d88c4ca
NC
216
217/* Given a BFD reloc type, return a HOWTO structure. */
218static reloc_howto_type *
67a4f2b7 219elf64_x86_64_reloc_type_lookup (bfd *abfd,
27482721 220 bfd_reloc_code_real_type code)
8d88c4ca
NC
221{
222 unsigned int i;
27482721 223
8d88c4ca
NC
224 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
225 i++)
226 {
227 if (x86_64_reloc_map[i].bfd_reloc_val == code)
67a4f2b7
AO
228 return elf64_x86_64_rtype_to_howto (abfd,
229 x86_64_reloc_map[i].elf_reloc_val);
8d88c4ca
NC
230 }
231 return 0;
232}
233
8d88c4ca 234/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 235
8d88c4ca 236static void
27482721
AJ
237elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
238 Elf_Internal_Rela *dst)
8d88c4ca 239{
67a4f2b7 240 unsigned r_type;
8d88c4ca
NC
241
242 r_type = ELF64_R_TYPE (dst->r_info);
67a4f2b7 243 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
8d88c4ca
NC
244 BFD_ASSERT (r_type == cache_ptr->howto->type);
245}
70256ad8 246\f
3bab7989 247/* Support for core dump NOTE sections. */
b34976b6 248static bfd_boolean
27482721 249elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
250{
251 int offset;
eea6121a 252 size_t size;
3bab7989
ML
253
254 switch (note->descsz)
255 {
256 default:
b34976b6 257 return FALSE;
3bab7989
ML
258
259 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
260 /* pr_cursig */
cedb70c5 261 elf_tdata (abfd)->core_signal
3bab7989
ML
262 = bfd_get_16 (abfd, note->descdata + 12);
263
264 /* pr_pid */
cedb70c5 265 elf_tdata (abfd)->core_pid
3bab7989
ML
266 = bfd_get_32 (abfd, note->descdata + 32);
267
268 /* pr_reg */
269 offset = 112;
eea6121a 270 size = 216;
3bab7989
ML
271
272 break;
273 }
274
275 /* Make a ".reg/999" section. */
276 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 277 size, note->descpos + offset);
3bab7989
ML
278}
279
b34976b6 280static bfd_boolean
27482721 281elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
282{
283 switch (note->descsz)
284 {
285 default:
b34976b6 286 return FALSE;
3bab7989
ML
287
288 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
289 elf_tdata (abfd)->core_program
290 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
291 elf_tdata (abfd)->core_command
292 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
293 }
294
295 /* Note that for some reason, a spurious space is tacked
296 onto the end of the args in some (at least one anyway)
297 implementations, so strip it off if it exists. */
298
299 {
300 char *command = elf_tdata (abfd)->core_command;
301 int n = strlen (command);
302
303 if (0 < n && command[n - 1] == ' ')
304 command[n - 1] = '\0';
305 }
306
b34976b6 307 return TRUE;
3bab7989
ML
308}
309\f
407443a3 310/* Functions for the x86-64 ELF linker. */
70256ad8 311
407443a3 312/* The name of the dynamic interpreter. This is put in the .interp
70256ad8
AJ
313 section. */
314
407443a3 315#define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
70256ad8 316
d40d037c
AJ
317/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
318 copying dynamic variables from a shared lib into an app's dynbss
319 section, and instead use a dynamic relocation to point into the
320 shared lib. */
321#define ELIMINATE_COPY_RELOCS 1
322
70256ad8
AJ
323/* The size in bytes of an entry in the global offset table. */
324
325#define GOT_ENTRY_SIZE 8
8d88c4ca 326
70256ad8 327/* The size in bytes of an entry in the procedure linkage table. */
8d88c4ca 328
70256ad8
AJ
329#define PLT_ENTRY_SIZE 16
330
331/* The first entry in a procedure linkage table looks like this. See the
332 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
333
334static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
335{
653165cc
AJ
336 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
337 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
338 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
70256ad8
AJ
339};
340
341/* Subsequent entries in a procedure linkage table look like this. */
342
343static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
344{
653165cc 345 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 346 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 347 0x68, /* pushq immediate */
70256ad8
AJ
348 0, 0, 0, 0, /* replaced with index into relocation table. */
349 0xe9, /* jmp relative */
350 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
351};
352
353/* The x86-64 linker needs to keep track of the number of relocs that
985142a4 354 it decides to copy as dynamic relocs in check_relocs for each symbol.
c434dee6
AJ
355 This is so that it can later discard them if they are found to be
356 unnecessary. We store the information in a field extending the
357 regular ELF linker hash table. */
70256ad8 358
c434dee6 359struct elf64_x86_64_dyn_relocs
70256ad8
AJ
360{
361 /* Next section. */
c434dee6
AJ
362 struct elf64_x86_64_dyn_relocs *next;
363
364 /* The input section of the reloc. */
365 asection *sec;
366
367 /* Total number of relocs copied for the input section. */
70256ad8 368 bfd_size_type count;
c434dee6
AJ
369
370 /* Number of pc-relative relocs copied for the input section. */
371 bfd_size_type pc_count;
70256ad8
AJ
372};
373
374/* x86-64 ELF linker hash entry. */
375
376struct elf64_x86_64_link_hash_entry
377{
c434dee6 378 struct elf_link_hash_entry elf;
70256ad8 379
c434dee6
AJ
380 /* Track dynamic relocs copied for this symbol. */
381 struct elf64_x86_64_dyn_relocs *dyn_relocs;
bffbf940
JJ
382
383#define GOT_UNKNOWN 0
384#define GOT_NORMAL 1
385#define GOT_TLS_GD 2
386#define GOT_TLS_IE 3
67a4f2b7
AO
387#define GOT_TLS_GDESC 4
388#define GOT_TLS_GD_BOTH_P(type) \
389 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
390#define GOT_TLS_GD_P(type) \
391 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
392#define GOT_TLS_GDESC_P(type) \
393 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
394#define GOT_TLS_GD_ANY_P(type) \
395 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
bffbf940 396 unsigned char tls_type;
67a4f2b7
AO
397
398 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
399 starting at the end of the jump table. */
400 bfd_vma tlsdesc_got;
bffbf940
JJ
401};
402
403#define elf64_x86_64_hash_entry(ent) \
404 ((struct elf64_x86_64_link_hash_entry *)(ent))
405
406struct elf64_x86_64_obj_tdata
407{
408 struct elf_obj_tdata root;
409
410 /* tls_type for each local got entry. */
411 char *local_got_tls_type;
67a4f2b7
AO
412
413 /* GOTPLT entries for TLS descriptors. */
414 bfd_vma *local_tlsdesc_gotent;
70256ad8
AJ
415};
416
bffbf940
JJ
417#define elf64_x86_64_tdata(abfd) \
418 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
419
420#define elf64_x86_64_local_got_tls_type(abfd) \
421 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
422
67a4f2b7
AO
423#define elf64_x86_64_local_tlsdesc_gotent(abfd) \
424 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
bffbf940 425
c434dee6 426/* x86-64 ELF linker hash table. */
8d88c4ca 427
407443a3
AJ
428struct elf64_x86_64_link_hash_table
429{
c434dee6 430 struct elf_link_hash_table elf;
70256ad8 431
c434dee6
AJ
432 /* Short-cuts to get to dynamic linker sections. */
433 asection *sgot;
434 asection *sgotplt;
435 asection *srelgot;
436 asection *splt;
437 asection *srelplt;
438 asection *sdynbss;
439 asection *srelbss;
70256ad8 440
67a4f2b7
AO
441 /* The offset into splt of the PLT entry for the TLS descriptor
442 resolver. Special values are 0, if not necessary (or not found
443 to be necessary yet), and -1 if needed but not determined
444 yet. */
445 bfd_vma tlsdesc_plt;
446 /* The offset into sgot of the GOT entry used by the PLT entry
447 above. */
448 bfd_vma tlsdesc_got;
449
bffbf940
JJ
450 union {
451 bfd_signed_vma refcount;
452 bfd_vma offset;
453 } tls_ld_got;
454
67a4f2b7
AO
455 /* The amount of space used by the jump slots in the GOT. */
456 bfd_vma sgotplt_jump_table_size;
457
c434dee6
AJ
458 /* Small local sym to section mapping cache. */
459 struct sym_sec_cache sym_sec;
460};
70256ad8
AJ
461
462/* Get the x86-64 ELF linker hash table from a link_info structure. */
8d88c4ca
NC
463
464#define elf64_x86_64_hash_table(p) \
465 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
466
67a4f2b7
AO
467#define elf64_x86_64_compute_jump_table_size(htab) \
468 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
469
407443a3 470/* Create an entry in an x86-64 ELF linker hash table. */
70256ad8
AJ
471
472static struct bfd_hash_entry *
27482721
AJ
473link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
474 const char *string)
70256ad8 475{
70256ad8 476 /* Allocate the structure if it has not already been allocated by a
c434dee6
AJ
477 subclass. */
478 if (entry == NULL)
479 {
480 entry = bfd_hash_allocate (table,
481 sizeof (struct elf64_x86_64_link_hash_entry));
482 if (entry == NULL)
483 return entry;
484 }
70256ad8
AJ
485
486 /* Call the allocation method of the superclass. */
c434dee6
AJ
487 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
488 if (entry != NULL)
70256ad8 489 {
c434dee6
AJ
490 struct elf64_x86_64_link_hash_entry *eh;
491
492 eh = (struct elf64_x86_64_link_hash_entry *) entry;
493 eh->dyn_relocs = NULL;
bffbf940 494 eh->tls_type = GOT_UNKNOWN;
67a4f2b7 495 eh->tlsdesc_got = (bfd_vma) -1;
70256ad8
AJ
496 }
497
c434dee6 498 return entry;
70256ad8
AJ
499}
500
8d88c4ca
NC
501/* Create an X86-64 ELF linker hash table. */
502
503static struct bfd_link_hash_table *
27482721 504elf64_x86_64_link_hash_table_create (bfd *abfd)
8d88c4ca
NC
505{
506 struct elf64_x86_64_link_hash_table *ret;
dc810e39 507 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
8d88c4ca 508
e2d34d7d 509 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
c434dee6 510 if (ret == NULL)
8d88c4ca
NC
511 return NULL;
512
66eb6687
AM
513 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
514 sizeof (struct elf64_x86_64_link_hash_entry)))
8d88c4ca 515 {
e2d34d7d 516 free (ret);
8d88c4ca
NC
517 return NULL;
518 }
519
c434dee6
AJ
520 ret->sgot = NULL;
521 ret->sgotplt = NULL;
522 ret->srelgot = NULL;
523 ret->splt = NULL;
524 ret->srelplt = NULL;
525 ret->sdynbss = NULL;
526 ret->srelbss = NULL;
527 ret->sym_sec.abfd = NULL;
67a4f2b7
AO
528 ret->tlsdesc_plt = 0;
529 ret->tlsdesc_got = 0;
bffbf940 530 ret->tls_ld_got.refcount = 0;
67a4f2b7 531 ret->sgotplt_jump_table_size = 0;
c434dee6
AJ
532
533 return &ret->elf.root;
534}
535
536/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
537 shortcuts to them in our hash table. */
538
b34976b6 539static bfd_boolean
27482721 540create_got_section (bfd *dynobj, struct bfd_link_info *info)
c434dee6
AJ
541{
542 struct elf64_x86_64_link_hash_table *htab;
543
544 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 545 return FALSE;
c434dee6
AJ
546
547 htab = elf64_x86_64_hash_table (info);
548 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
549 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
550 if (!htab->sgot || !htab->sgotplt)
551 abort ();
552
3496cb2a
L
553 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
554 (SEC_ALLOC | SEC_LOAD
555 | SEC_HAS_CONTENTS
556 | SEC_IN_MEMORY
557 | SEC_LINKER_CREATED
558 | SEC_READONLY));
c434dee6 559 if (htab->srelgot == NULL
c434dee6 560 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
b34976b6
AM
561 return FALSE;
562 return TRUE;
c434dee6
AJ
563}
564
565/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
566 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
567 hash table. */
568
b34976b6 569static bfd_boolean
27482721 570elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
c434dee6
AJ
571{
572 struct elf64_x86_64_link_hash_table *htab;
573
574 htab = elf64_x86_64_hash_table (info);
575 if (!htab->sgot && !create_got_section (dynobj, info))
b34976b6 576 return FALSE;
c434dee6
AJ
577
578 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 579 return FALSE;
c434dee6
AJ
580
581 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
582 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
583 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
584 if (!info->shared)
585 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
586
587 if (!htab->splt || !htab->srelplt || !htab->sdynbss
588 || (!info->shared && !htab->srelbss))
589 abort ();
590
b34976b6 591 return TRUE;
c434dee6
AJ
592}
593
594/* Copy the extra info we tack onto an elf_link_hash_entry. */
595
596static void
fcfa13d2 597elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
27482721
AJ
598 struct elf_link_hash_entry *dir,
599 struct elf_link_hash_entry *ind)
c434dee6
AJ
600{
601 struct elf64_x86_64_link_hash_entry *edir, *eind;
602
603 edir = (struct elf64_x86_64_link_hash_entry *) dir;
604 eind = (struct elf64_x86_64_link_hash_entry *) ind;
605
606 if (eind->dyn_relocs != NULL)
607 {
608 if (edir->dyn_relocs != NULL)
609 {
610 struct elf64_x86_64_dyn_relocs **pp;
611 struct elf64_x86_64_dyn_relocs *p;
612
fcfa13d2 613 /* Add reloc counts against the indirect sym to the direct sym
c434dee6
AJ
614 list. Merge any entries against the same section. */
615 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
616 {
617 struct elf64_x86_64_dyn_relocs *q;
618
619 for (q = edir->dyn_relocs; q != NULL; q = q->next)
620 if (q->sec == p->sec)
621 {
622 q->pc_count += p->pc_count;
623 q->count += p->count;
624 *pp = p->next;
625 break;
626 }
627 if (q == NULL)
628 pp = &p->next;
629 }
630 *pp = edir->dyn_relocs;
631 }
632
633 edir->dyn_relocs = eind->dyn_relocs;
634 eind->dyn_relocs = NULL;
635 }
636
bffbf940
JJ
637 if (ind->root.type == bfd_link_hash_indirect
638 && dir->got.refcount <= 0)
639 {
640 edir->tls_type = eind->tls_type;
641 eind->tls_type = GOT_UNKNOWN;
642 }
643
d40d037c
AJ
644 if (ELIMINATE_COPY_RELOCS
645 && ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
646 && dir->dynamic_adjusted)
647 {
648 /* If called to transfer flags for a weakdef during processing
649 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
650 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
651 dir->ref_dynamic |= ind->ref_dynamic;
652 dir->ref_regular |= ind->ref_regular;
653 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
654 dir->needs_plt |= ind->needs_plt;
655 dir->pointer_equality_needed |= ind->pointer_equality_needed;
656 }
d40d037c 657 else
fcfa13d2 658 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
8d88c4ca
NC
659}
660
b34976b6 661static bfd_boolean
27482721 662elf64_x86_64_mkobject (bfd *abfd)
8d88c4ca 663{
bffbf940
JJ
664 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
665 abfd->tdata.any = bfd_zalloc (abfd, amt);
666 if (abfd->tdata.any == NULL)
b34976b6
AM
667 return FALSE;
668 return TRUE;
bffbf940
JJ
669}
670
b34976b6 671static bfd_boolean
27482721 672elf64_x86_64_elf_object_p (bfd *abfd)
bffbf940 673{
8d88c4ca
NC
674 /* Set the right machine number for an x86-64 elf64 file. */
675 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
b34976b6 676 return TRUE;
8d88c4ca
NC
677}
678
bffbf940 679static int
27482721 680elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
bffbf940
JJ
681{
682 if (info->shared)
683 return r_type;
684
685 switch (r_type)
686 {
687 case R_X86_64_TLSGD:
67a4f2b7
AO
688 case R_X86_64_GOTPC32_TLSDESC:
689 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
690 case R_X86_64_GOTTPOFF:
691 if (is_local)
692 return R_X86_64_TPOFF32;
693 return R_X86_64_GOTTPOFF;
694 case R_X86_64_TLSLD:
695 return R_X86_64_TPOFF32;
696 }
697
698 return r_type;
699}
700
70256ad8 701/* Look through the relocs for a section during the first phase, and
c434dee6
AJ
702 calculate needed space in the global offset table, procedure
703 linkage table, and dynamic reloc sections. */
70256ad8 704
b34976b6 705static bfd_boolean
27482721
AJ
706elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
707 const Elf_Internal_Rela *relocs)
70256ad8 708{
c434dee6 709 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
710 Elf_Internal_Shdr *symtab_hdr;
711 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
712 const Elf_Internal_Rela *rel;
713 const Elf_Internal_Rela *rel_end;
70256ad8
AJ
714 asection *sreloc;
715
1049f94e 716 if (info->relocatable)
b34976b6 717 return TRUE;
70256ad8 718
c434dee6 719 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
720 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
721 sym_hashes = elf_sym_hashes (abfd);
70256ad8 722
c434dee6
AJ
723 sreloc = NULL;
724
70256ad8
AJ
725 rel_end = relocs + sec->reloc_count;
726 for (rel = relocs; rel < rel_end; rel++)
727 {
bffbf940 728 unsigned int r_type;
70256ad8
AJ
729 unsigned long r_symndx;
730 struct elf_link_hash_entry *h;
731
732 r_symndx = ELF64_R_SYM (rel->r_info);
bffbf940 733 r_type = ELF64_R_TYPE (rel->r_info);
c434dee6
AJ
734
735 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
736 {
d003868e
AM
737 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
738 abfd, r_symndx);
b34976b6 739 return FALSE;
c434dee6
AJ
740 }
741
70256ad8
AJ
742 if (r_symndx < symtab_hdr->sh_info)
743 h = NULL;
744 else
71cb9464
L
745 {
746 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
747 while (h->root.type == bfd_link_hash_indirect
748 || h->root.type == bfd_link_hash_warning)
749 h = (struct elf_link_hash_entry *) h->root.u.i.link;
750 }
70256ad8 751
bffbf940
JJ
752 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
753 switch (r_type)
70256ad8 754 {
bffbf940
JJ
755 case R_X86_64_TLSLD:
756 htab->tls_ld_got.refcount += 1;
757 goto create_got;
758
759 case R_X86_64_TPOFF32:
760 if (info->shared)
70256ad8 761 {
bffbf940 762 (*_bfd_error_handler)
d003868e
AM
763 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
764 abfd,
6610a52d
L
765 x86_64_elf_howto_table[r_type].name,
766 (h) ? h->root.root.string : "a local symbol");
bffbf940 767 bfd_set_error (bfd_error_bad_value);
b34976b6 768 return FALSE;
70256ad8 769 }
bffbf940 770 break;
c434dee6 771
bffbf940
JJ
772 case R_X86_64_GOTTPOFF:
773 if (info->shared)
774 info->flags |= DF_STATIC_TLS;
775 /* Fall through */
70256ad8 776
bffbf940
JJ
777 case R_X86_64_GOT32:
778 case R_X86_64_GOTPCREL:
779 case R_X86_64_TLSGD:
67a4f2b7
AO
780 case R_X86_64_GOTPC32_TLSDESC:
781 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
782 /* This symbol requires a global offset table entry. */
783 {
784 int tls_type, old_tls_type;
785
786 switch (r_type)
787 {
788 default: tls_type = GOT_NORMAL; break;
789 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
790 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
67a4f2b7
AO
791 case R_X86_64_GOTPC32_TLSDESC:
792 case R_X86_64_TLSDESC_CALL:
793 tls_type = GOT_TLS_GDESC; break;
bffbf940
JJ
794 }
795
796 if (h != NULL)
797 {
798 h->got.refcount += 1;
799 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
800 }
801 else
802 {
803 bfd_signed_vma *local_got_refcounts;
804
805 /* This is a global offset table entry for a local symbol. */
806 local_got_refcounts = elf_local_got_refcounts (abfd);
807 if (local_got_refcounts == NULL)
808 {
809 bfd_size_type size;
810
811 size = symtab_hdr->sh_info;
67a4f2b7
AO
812 size *= sizeof (bfd_signed_vma)
813 + sizeof (bfd_vma) + sizeof (char);
bffbf940
JJ
814 local_got_refcounts = ((bfd_signed_vma *)
815 bfd_zalloc (abfd, size));
816 if (local_got_refcounts == NULL)
b34976b6 817 return FALSE;
bffbf940 818 elf_local_got_refcounts (abfd) = local_got_refcounts;
67a4f2b7
AO
819 elf64_x86_64_local_tlsdesc_gotent (abfd)
820 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
bffbf940 821 elf64_x86_64_local_got_tls_type (abfd)
67a4f2b7 822 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
bffbf940
JJ
823 }
824 local_got_refcounts[r_symndx] += 1;
825 old_tls_type
826 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
827 }
828
829 /* If a TLS symbol is accessed using IE at least once,
830 there is no point to use dynamic model for it. */
831 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7
AO
832 && (! GOT_TLS_GD_ANY_P (old_tls_type)
833 || tls_type != GOT_TLS_IE))
bffbf940 834 {
67a4f2b7 835 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
bffbf940 836 tls_type = old_tls_type;
67a4f2b7
AO
837 else if (GOT_TLS_GD_ANY_P (old_tls_type)
838 && GOT_TLS_GD_ANY_P (tls_type))
839 tls_type |= old_tls_type;
bffbf940
JJ
840 else
841 {
842 (*_bfd_error_handler)
d003868e
AM
843 (_("%B: %s' accessed both as normal and thread local symbol"),
844 abfd, h ? h->root.root.string : "<local>");
b34976b6 845 return FALSE;
bffbf940
JJ
846 }
847 }
848
849 if (old_tls_type != tls_type)
850 {
851 if (h != NULL)
852 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
853 else
854 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
855 }
856 }
c434dee6
AJ
857 /* Fall through */
858
d6ab8113
JB
859 case R_X86_64_GOTOFF64:
860 case R_X86_64_GOTPC32:
bffbf940 861 create_got:
c434dee6
AJ
862 if (htab->sgot == NULL)
863 {
864 if (htab->elf.dynobj == NULL)
865 htab->elf.dynobj = abfd;
866 if (!create_got_section (htab->elf.dynobj, info))
b34976b6 867 return FALSE;
c434dee6 868 }
70256ad8
AJ
869 break;
870
871 case R_X86_64_PLT32:
872 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
873 actually build the entry in adjust_dynamic_symbol,
874 because this might be a case of linking PIC code which is
875 never referenced by a dynamic object, in which case we
876 don't need to generate a procedure linkage table entry
877 after all. */
70256ad8
AJ
878
879 /* If this is a local symbol, we resolve it directly without
407443a3 880 creating a procedure linkage table entry. */
70256ad8
AJ
881 if (h == NULL)
882 continue;
883
f5385ebf 884 h->needs_plt = 1;
51b64d56 885 h->plt.refcount += 1;
70256ad8
AJ
886 break;
887
cc78d0af
AJ
888 case R_X86_64_8:
889 case R_X86_64_16:
70256ad8
AJ
890 case R_X86_64_32:
891 case R_X86_64_32S:
1b71fb54
AJ
892 /* Let's help debug shared library creation. These relocs
893 cannot be used in shared libs. Don't error out for
894 sections we don't care about, such as debug sections or
895 non-constant sections. */
896 if (info->shared
897 && (sec->flags & SEC_ALLOC) != 0
898 && (sec->flags & SEC_READONLY) != 0)
899 {
900 (*_bfd_error_handler)
d003868e
AM
901 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
902 abfd,
6610a52d
L
903 x86_64_elf_howto_table[r_type].name,
904 (h) ? h->root.root.string : "a local symbol");
1b71fb54 905 bfd_set_error (bfd_error_bad_value);
b34976b6 906 return FALSE;
1b71fb54
AJ
907 }
908 /* Fall through. */
909
c434dee6
AJ
910 case R_X86_64_PC8:
911 case R_X86_64_PC16:
70256ad8 912 case R_X86_64_PC32:
d6ab8113 913 case R_X86_64_PC64:
1b71fb54 914 case R_X86_64_64:
c434dee6
AJ
915 if (h != NULL && !info->shared)
916 {
917 /* If this reloc is in a read-only section, we might
918 need a copy reloc. We can't check reliably at this
919 stage whether the section is read-only, as input
920 sections have not yet been mapped to output sections.
921 Tentatively set the flag for now, and correct in
922 adjust_dynamic_symbol. */
f5385ebf 923 h->non_got_ref = 1;
c434dee6
AJ
924
925 /* We may need a .plt entry if the function this reloc
926 refers to is in a shared lib. */
927 h->plt.refcount += 1;
d6ab8113 928 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
f5385ebf 929 h->pointer_equality_needed = 1;
c434dee6 930 }
70256ad8
AJ
931
932 /* If we are creating a shared library, and this is a reloc
933 against a global symbol, or a non PC relative reloc
934 against a local symbol, then we need to copy the reloc
935 into the shared library. However, if we are linking with
936 -Bsymbolic, we do not need to copy a reloc against a
937 global symbol which is defined in an object we are
407443a3 938 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
939 this point we have not seen all the input files, so it is
940 possible that DEF_REGULAR is not set now but will be set
c434dee6
AJ
941 later (it is never cleared). In case of a weak definition,
942 DEF_REGULAR may be cleared later by a strong definition in
943 a shared library. We account for that possibility below by
944 storing information in the relocs_copied field of the hash
945 table entry. A similar situation occurs when creating
946 shared libraries and symbol visibility changes render the
947 symbol local.
948
949 If on the other hand, we are creating an executable, we
950 may need to keep relocations for symbols satisfied by a
951 dynamic library if we manage to avoid copy relocs for the
952 symbol. */
953 if ((info->shared
954 && (sec->flags & SEC_ALLOC) != 0
bffbf940
JJ
955 && (((r_type != R_X86_64_PC8)
956 && (r_type != R_X86_64_PC16)
d6ab8113
JB
957 && (r_type != R_X86_64_PC32)
958 && (r_type != R_X86_64_PC64))
c434dee6
AJ
959 || (h != NULL
960 && (! info->symbolic
961 || h->root.type == bfd_link_hash_defweak
f5385ebf 962 || !h->def_regular))))
d40d037c
AJ
963 || (ELIMINATE_COPY_RELOCS
964 && !info->shared
c434dee6
AJ
965 && (sec->flags & SEC_ALLOC) != 0
966 && h != NULL
967 && (h->root.type == bfd_link_hash_defweak
f5385ebf 968 || !h->def_regular)))
70256ad8 969 {
c434dee6
AJ
970 struct elf64_x86_64_dyn_relocs *p;
971 struct elf64_x86_64_dyn_relocs **head;
972
973 /* We must copy these reloc types into the output file.
974 Create a reloc section in dynobj and make room for
975 this reloc. */
70256ad8
AJ
976 if (sreloc == NULL)
977 {
978 const char *name;
c434dee6 979 bfd *dynobj;
70256ad8
AJ
980
981 name = (bfd_elf_string_from_elf_section
982 (abfd,
983 elf_elfheader (abfd)->e_shstrndx,
984 elf_section_data (sec)->rel_hdr.sh_name));
985 if (name == NULL)
b34976b6 986 return FALSE;
70256ad8 987
c434dee6
AJ
988 if (strncmp (name, ".rela", 5) != 0
989 || strcmp (bfd_get_section_name (abfd, sec),
990 name + 5) != 0)
991 {
992 (*_bfd_error_handler)
d003868e
AM
993 (_("%B: bad relocation section name `%s\'"),
994 abfd, name);
c434dee6
AJ
995 }
996
997 if (htab->elf.dynobj == NULL)
998 htab->elf.dynobj = abfd;
999
1000 dynobj = htab->elf.dynobj;
70256ad8
AJ
1001
1002 sreloc = bfd_get_section_by_name (dynobj, name);
1003 if (sreloc == NULL)
1004 {
1005 flagword flags;
1006
70256ad8
AJ
1007 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1008 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1009 if ((sec->flags & SEC_ALLOC) != 0)
1010 flags |= SEC_ALLOC | SEC_LOAD;
3496cb2a
L
1011 sreloc = bfd_make_section_with_flags (dynobj,
1012 name,
1013 flags);
70256ad8 1014 if (sreloc == NULL
cc78d0af 1015 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
b34976b6 1016 return FALSE;
70256ad8 1017 }
c434dee6 1018 elf_section_data (sec)->sreloc = sreloc;
70256ad8
AJ
1019 }
1020
c434dee6
AJ
1021 /* If this is a global symbol, we count the number of
1022 relocations we need for this symbol. */
1023 if (h != NULL)
70256ad8 1024 {
c434dee6
AJ
1025 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1026 }
1027 else
1028 {
e81d3500 1029 void **vpp;
c434dee6
AJ
1030 /* Track dynamic relocs needed for local syms too.
1031 We really need local syms available to do this
1032 easily. Oh well. */
1033
1034 asection *s;
1035 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1036 sec, r_symndx);
1037 if (s == NULL)
b34976b6 1038 return FALSE;
70256ad8 1039
e81d3500
DD
1040 /* Beware of type punned pointers vs strict aliasing
1041 rules. */
1042 vpp = &(elf_section_data (s)->local_dynrel);
1043 head = (struct elf64_x86_64_dyn_relocs **)vpp;
c434dee6 1044 }
70256ad8 1045
c434dee6
AJ
1046 p = *head;
1047 if (p == NULL || p->sec != sec)
1048 {
1049 bfd_size_type amt = sizeof *p;
1050 p = ((struct elf64_x86_64_dyn_relocs *)
1051 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 1052 if (p == NULL)
b34976b6 1053 return FALSE;
c434dee6
AJ
1054 p->next = *head;
1055 *head = p;
1056 p->sec = sec;
1057 p->count = 0;
1058 p->pc_count = 0;
70256ad8 1059 }
c434dee6
AJ
1060
1061 p->count += 1;
bffbf940
JJ
1062 if (r_type == R_X86_64_PC8
1063 || r_type == R_X86_64_PC16
d6ab8113
JB
1064 || r_type == R_X86_64_PC32
1065 || r_type == R_X86_64_PC64)
c434dee6 1066 p->pc_count += 1;
70256ad8
AJ
1067 }
1068 break;
fe4770f4
AJ
1069
1070 /* This relocation describes the C++ object vtable hierarchy.
1071 Reconstruct it for later use during GC. */
1072 case R_X86_64_GNU_VTINHERIT:
c152c796 1073 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 1074 return FALSE;
fe4770f4
AJ
1075 break;
1076
1077 /* This relocation describes which C++ vtable entries are actually
1078 used. Record for later use during GC. */
1079 case R_X86_64_GNU_VTENTRY:
c152c796 1080 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 1081 return FALSE;
fe4770f4 1082 break;
c434dee6
AJ
1083
1084 default:
1085 break;
70256ad8
AJ
1086 }
1087 }
1088
b34976b6 1089 return TRUE;
70256ad8
AJ
1090}
1091
1092/* Return the section that should be marked against GC for a given
407443a3 1093 relocation. */
70256ad8
AJ
1094
1095static asection *
27482721
AJ
1096elf64_x86_64_gc_mark_hook (asection *sec,
1097 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1098 Elf_Internal_Rela *rel,
1099 struct elf_link_hash_entry *h,
1100 Elf_Internal_Sym *sym)
70256ad8
AJ
1101{
1102 if (h != NULL)
1103 {
fe4770f4 1104 switch (ELF64_R_TYPE (rel->r_info))
70256ad8 1105 {
fe4770f4
AJ
1106 case R_X86_64_GNU_VTINHERIT:
1107 case R_X86_64_GNU_VTENTRY:
1108 break;
70256ad8
AJ
1109
1110 default:
fe4770f4
AJ
1111 switch (h->root.type)
1112 {
1113 case bfd_link_hash_defined:
1114 case bfd_link_hash_defweak:
1115 return h->root.u.def.section;
1116
1117 case bfd_link_hash_common:
1118 return h->root.u.c.p->section;
1119
1120 default:
1121 break;
1122 }
70256ad8
AJ
1123 }
1124 }
1125 else
1e2f5b6e 1126 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
70256ad8
AJ
1127
1128 return NULL;
1129}
1130
407443a3 1131/* Update the got entry reference counts for the section being removed. */
70256ad8 1132
b34976b6 1133static bfd_boolean
27482721
AJ
1134elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1135 asection *sec, const Elf_Internal_Rela *relocs)
70256ad8
AJ
1136{
1137 Elf_Internal_Shdr *symtab_hdr;
1138 struct elf_link_hash_entry **sym_hashes;
1139 bfd_signed_vma *local_got_refcounts;
1140 const Elf_Internal_Rela *rel, *relend;
c434dee6
AJ
1141
1142 elf_section_data (sec)->local_dynrel = NULL;
70256ad8
AJ
1143
1144 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1145 sym_hashes = elf_sym_hashes (abfd);
1146 local_got_refcounts = elf_local_got_refcounts (abfd);
1147
70256ad8
AJ
1148 relend = relocs + sec->reloc_count;
1149 for (rel = relocs; rel < relend; rel++)
26e41594
AM
1150 {
1151 unsigned long r_symndx;
1152 unsigned int r_type;
1153 struct elf_link_hash_entry *h = NULL;
70256ad8 1154
26e41594
AM
1155 r_symndx = ELF64_R_SYM (rel->r_info);
1156 if (r_symndx >= symtab_hdr->sh_info)
1157 {
1158 struct elf64_x86_64_link_hash_entry *eh;
1159 struct elf64_x86_64_dyn_relocs **pp;
1160 struct elf64_x86_64_dyn_relocs *p;
c434dee6 1161
26e41594 1162 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
1163 while (h->root.type == bfd_link_hash_indirect
1164 || h->root.type == bfd_link_hash_warning)
1165 h = (struct elf_link_hash_entry *) h->root.u.i.link;
26e41594 1166 eh = (struct elf64_x86_64_link_hash_entry *) h;
c434dee6 1167
26e41594
AM
1168 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1169 if (p->sec == sec)
1170 {
1171 /* Everything must go for SEC. */
1172 *pp = p->next;
1173 break;
1174 }
1175 }
c434dee6 1176
26e41594
AM
1177 r_type = ELF64_R_TYPE (rel->r_info);
1178 r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
1179 switch (r_type)
1180 {
1181 case R_X86_64_TLSLD:
1182 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1183 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1184 break;
c434dee6 1185
26e41594 1186 case R_X86_64_TLSGD:
67a4f2b7
AO
1187 case R_X86_64_GOTPC32_TLSDESC:
1188 case R_X86_64_TLSDESC_CALL:
26e41594
AM
1189 case R_X86_64_GOTTPOFF:
1190 case R_X86_64_GOT32:
1191 case R_X86_64_GOTPCREL:
1192 if (h != NULL)
1193 {
1194 if (h->got.refcount > 0)
1195 h->got.refcount -= 1;
1196 }
1197 else if (local_got_refcounts != NULL)
1198 {
1199 if (local_got_refcounts[r_symndx] > 0)
1200 local_got_refcounts[r_symndx] -= 1;
1201 }
1202 break;
c434dee6 1203
26e41594
AM
1204 case R_X86_64_8:
1205 case R_X86_64_16:
1206 case R_X86_64_32:
1207 case R_X86_64_64:
1208 case R_X86_64_32S:
1209 case R_X86_64_PC8:
1210 case R_X86_64_PC16:
1211 case R_X86_64_PC32:
d6ab8113 1212 case R_X86_64_PC64:
26e41594
AM
1213 if (info->shared)
1214 break;
1215 /* Fall thru */
c434dee6 1216
26e41594
AM
1217 case R_X86_64_PLT32:
1218 if (h != NULL)
1219 {
1220 if (h->plt.refcount > 0)
1221 h->plt.refcount -= 1;
1222 }
1223 break;
70256ad8 1224
26e41594
AM
1225 default:
1226 break;
1227 }
1228 }
70256ad8 1229
b34976b6 1230 return TRUE;
70256ad8
AJ
1231}
1232
1233/* Adjust a symbol defined by a dynamic object and referenced by a
1234 regular object. The current definition is in some section of the
1235 dynamic object, but we're not including those sections. We have to
1236 change the definition to something the rest of the link can
407443a3 1237 understand. */
70256ad8 1238
b34976b6 1239static bfd_boolean
27482721
AJ
1240elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1241 struct elf_link_hash_entry *h)
70256ad8 1242{
c434dee6 1243 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
1244 asection *s;
1245 unsigned int power_of_two;
1246
70256ad8
AJ
1247 /* If this is a function, put it in the procedure linkage table. We
1248 will fill in the contents of the procedure linkage table later,
1249 when we know the address of the .got section. */
1250 if (h->type == STT_FUNC
f5385ebf 1251 || h->needs_plt)
70256ad8 1252 {
c434dee6 1253 if (h->plt.refcount <= 0
27482721
AJ
1254 || SYMBOL_CALLS_LOCAL (info, h)
1255 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1256 && h->root.type == bfd_link_hash_undefweak))
70256ad8 1257 {
70256ad8
AJ
1258 /* This case can occur if we saw a PLT32 reloc in an input
1259 file, but the symbol was never referred to by a dynamic
1260 object, or if all references were garbage collected. In
1261 such a case, we don't actually need to build a procedure
1262 linkage table, and we can just do a PC32 reloc instead. */
70256ad8 1263 h->plt.offset = (bfd_vma) -1;
f5385ebf 1264 h->needs_plt = 0;
70256ad8
AJ
1265 }
1266
b34976b6 1267 return TRUE;
70256ad8 1268 }
bbd7ec4a 1269 else
c434dee6
AJ
1270 /* It's possible that we incorrectly decided a .plt reloc was
1271 needed for an R_X86_64_PC32 reloc to a non-function sym in
1272 check_relocs. We can't decide accurately between function and
1273 non-function syms in check-relocs; Objects loaded later in
1274 the link may change h->type. So fix it now. */
bbd7ec4a 1275 h->plt.offset = (bfd_vma) -1;
70256ad8
AJ
1276
1277 /* If this is a weak symbol, and there is a real definition, the
1278 processor independent code will have arranged for us to see the
407443a3 1279 real definition first, and we can just use the same value. */
f6e332e6 1280 if (h->u.weakdef != NULL)
70256ad8 1281 {
f6e332e6
AM
1282 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1283 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1284 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1285 h->root.u.def.value = h->u.weakdef->root.u.def.value;
d40d037c 1286 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
f6e332e6 1287 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 1288 return TRUE;
70256ad8
AJ
1289 }
1290
1291 /* This is a reference to a symbol defined by a dynamic object which
407443a3 1292 is not a function. */
70256ad8
AJ
1293
1294 /* If we are creating a shared library, we must presume that the
1295 only references to the symbol are via the global offset table.
1296 For such cases we need not do anything here; the relocations will
407443a3 1297 be handled correctly by relocate_section. */
70256ad8 1298 if (info->shared)
b34976b6 1299 return TRUE;
70256ad8
AJ
1300
1301 /* If there are no references to this symbol that do not use the
1302 GOT, we don't need to generate a copy reloc. */
f5385ebf 1303 if (!h->non_got_ref)
b34976b6 1304 return TRUE;
70256ad8 1305
c434dee6
AJ
1306 /* If -z nocopyreloc was given, we won't generate them either. */
1307 if (info->nocopyreloc)
1308 {
f5385ebf 1309 h->non_got_ref = 0;
b34976b6 1310 return TRUE;
c434dee6
AJ
1311 }
1312
d40d037c 1313 if (ELIMINATE_COPY_RELOCS)
c434dee6 1314 {
d40d037c
AJ
1315 struct elf64_x86_64_link_hash_entry * eh;
1316 struct elf64_x86_64_dyn_relocs *p;
c434dee6 1317
d40d037c
AJ
1318 eh = (struct elf64_x86_64_link_hash_entry *) h;
1319 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1320 {
1321 s = p->sec->output_section;
1322 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1323 break;
1324 }
1325
1326 /* If we didn't find any dynamic relocs in read-only sections, then
1327 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1328 if (p == NULL)
1329 {
f5385ebf 1330 h->non_got_ref = 0;
d40d037c
AJ
1331 return TRUE;
1332 }
c434dee6
AJ
1333 }
1334
909272ee
AM
1335 if (h->size == 0)
1336 {
1337 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1338 h->root.root.string);
1339 return TRUE;
1340 }
1341
70256ad8 1342 /* We must allocate the symbol in our .dynbss section, which will
407443a3 1343 become part of the .bss section of the executable. There will be
70256ad8
AJ
1344 an entry for this symbol in the .dynsym section. The dynamic
1345 object will contain position independent code, so all references
1346 from the dynamic object to this symbol will go through the global
1347 offset table. The dynamic linker will use the .dynsym entry to
1348 determine the address it must put in the global offset table, so
1349 both the dynamic object and the regular object will refer to the
1350 same memory location for the variable. */
1351
c434dee6 1352 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
1353
1354 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1355 to copy the initial value out of the dynamic object and into the
cedb70c5 1356 runtime process image. */
70256ad8
AJ
1357 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1358 {
eea6121a 1359 htab->srelbss->size += sizeof (Elf64_External_Rela);
f5385ebf 1360 h->needs_copy = 1;
70256ad8
AJ
1361 }
1362
1363 /* We need to figure out the alignment required for this symbol. I
407443a3 1364 have no idea how ELF linkers handle this. 16-bytes is the size
70256ad8
AJ
1365 of the largest type that requires hard alignment -- long double. */
1366 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1367 this construct. */
1368 power_of_two = bfd_log2 (h->size);
1369 if (power_of_two > 4)
1370 power_of_two = 4;
1371
1372 /* Apply the required alignment. */
c434dee6 1373 s = htab->sdynbss;
eea6121a 1374 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
c434dee6 1375 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
70256ad8 1376 {
c434dee6 1377 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
b34976b6 1378 return FALSE;
70256ad8
AJ
1379 }
1380
1381 /* Define the symbol as being at this point in the section. */
1382 h->root.u.def.section = s;
eea6121a 1383 h->root.u.def.value = s->size;
70256ad8
AJ
1384
1385 /* Increment the section size to make room for the symbol. */
eea6121a 1386 s->size += h->size;
70256ad8 1387
b34976b6 1388 return TRUE;
70256ad8
AJ
1389}
1390
c434dee6
AJ
1391/* Allocate space in .plt, .got and associated reloc sections for
1392 dynamic relocs. */
1393
b34976b6 1394static bfd_boolean
27482721 1395allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
1396{
1397 struct bfd_link_info *info;
1398 struct elf64_x86_64_link_hash_table *htab;
1399 struct elf64_x86_64_link_hash_entry *eh;
1400 struct elf64_x86_64_dyn_relocs *p;
1401
e92d460e 1402 if (h->root.type == bfd_link_hash_indirect)
b34976b6 1403 return TRUE;
c434dee6 1404
e92d460e
AM
1405 if (h->root.type == bfd_link_hash_warning)
1406 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1407
c434dee6
AJ
1408 info = (struct bfd_link_info *) inf;
1409 htab = elf64_x86_64_hash_table (info);
1410
1411 if (htab->elf.dynamic_sections_created
27482721 1412 && h->plt.refcount > 0)
c434dee6
AJ
1413 {
1414 /* Make sure this symbol is output as a dynamic symbol.
1415 Undefined weak syms won't yet be marked as dynamic. */
1416 if (h->dynindx == -1
f5385ebf 1417 && !h->forced_local)
c434dee6 1418 {
c152c796 1419 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1420 return FALSE;
c434dee6
AJ
1421 }
1422
27482721
AJ
1423 if (info->shared
1424 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
c434dee6
AJ
1425 {
1426 asection *s = htab->splt;
1427
1428 /* If this is the first .plt entry, make room for the special
1429 first entry. */
eea6121a
AM
1430 if (s->size == 0)
1431 s->size += PLT_ENTRY_SIZE;
c434dee6 1432
eea6121a 1433 h->plt.offset = s->size;
c434dee6
AJ
1434
1435 /* If this symbol is not defined in a regular file, and we are
1436 not generating a shared library, then set the symbol to this
1437 location in the .plt. This is required to make function
1438 pointers compare as equal between the normal executable and
1439 the shared library. */
1440 if (! info->shared
f5385ebf 1441 && !h->def_regular)
c434dee6
AJ
1442 {
1443 h->root.u.def.section = s;
1444 h->root.u.def.value = h->plt.offset;
1445 }
1446
1447 /* Make room for this entry. */
eea6121a 1448 s->size += PLT_ENTRY_SIZE;
c434dee6
AJ
1449
1450 /* We also need to make an entry in the .got.plt section, which
1451 will be placed in the .got section by the linker script. */
eea6121a 1452 htab->sgotplt->size += GOT_ENTRY_SIZE;
c434dee6
AJ
1453
1454 /* We also need to make an entry in the .rela.plt section. */
eea6121a 1455 htab->srelplt->size += sizeof (Elf64_External_Rela);
67a4f2b7 1456 htab->srelplt->reloc_count++;
c434dee6
AJ
1457 }
1458 else
1459 {
1460 h->plt.offset = (bfd_vma) -1;
f5385ebf 1461 h->needs_plt = 0;
c434dee6
AJ
1462 }
1463 }
1464 else
1465 {
1466 h->plt.offset = (bfd_vma) -1;
f5385ebf 1467 h->needs_plt = 0;
c434dee6
AJ
1468 }
1469
67a4f2b7
AO
1470 eh = (struct elf64_x86_64_link_hash_entry *) h;
1471 eh->tlsdesc_got = (bfd_vma) -1;
1472
bffbf940
JJ
1473 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1474 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1475 if (h->got.refcount > 0
1476 && !info->shared
1477 && h->dynindx == -1
1478 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1479 h->got.offset = (bfd_vma) -1;
1480 else if (h->got.refcount > 0)
c434dee6
AJ
1481 {
1482 asection *s;
b34976b6 1483 bfd_boolean dyn;
bffbf940 1484 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
c434dee6
AJ
1485
1486 /* Make sure this symbol is output as a dynamic symbol.
1487 Undefined weak syms won't yet be marked as dynamic. */
1488 if (h->dynindx == -1
f5385ebf 1489 && !h->forced_local)
c434dee6 1490 {
c152c796 1491 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1492 return FALSE;
c434dee6
AJ
1493 }
1494
67a4f2b7
AO
1495 if (GOT_TLS_GDESC_P (tls_type))
1496 {
1497 eh->tlsdesc_got = htab->sgotplt->size
1498 - elf64_x86_64_compute_jump_table_size (htab);
1499 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1500 h->got.offset = (bfd_vma) -2;
1501 }
1502 if (! GOT_TLS_GDESC_P (tls_type)
1503 || GOT_TLS_GD_P (tls_type))
1504 {
1505 s = htab->sgot;
1506 h->got.offset = s->size;
1507 s->size += GOT_ENTRY_SIZE;
1508 if (GOT_TLS_GD_P (tls_type))
1509 s->size += GOT_ENTRY_SIZE;
1510 }
c434dee6 1511 dyn = htab->elf.dynamic_sections_created;
bffbf940
JJ
1512 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1513 and two if global.
1514 R_X86_64_GOTTPOFF needs one dynamic relocation. */
67a4f2b7 1515 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
bffbf940 1516 || tls_type == GOT_TLS_IE)
eea6121a 1517 htab->srelgot->size += sizeof (Elf64_External_Rela);
67a4f2b7 1518 else if (GOT_TLS_GD_P (tls_type))
eea6121a 1519 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
67a4f2b7
AO
1520 else if (! GOT_TLS_GDESC_P (tls_type)
1521 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1522 || h->root.type != bfd_link_hash_undefweak)
27482721
AJ
1523 && (info->shared
1524 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
eea6121a 1525 htab->srelgot->size += sizeof (Elf64_External_Rela);
67a4f2b7
AO
1526 if (GOT_TLS_GDESC_P (tls_type))
1527 {
1528 htab->srelplt->size += sizeof (Elf64_External_Rela);
1529 htab->tlsdesc_plt = (bfd_vma) -1;
1530 }
c434dee6
AJ
1531 }
1532 else
1533 h->got.offset = (bfd_vma) -1;
1534
c434dee6 1535 if (eh->dyn_relocs == NULL)
b34976b6 1536 return TRUE;
c434dee6
AJ
1537
1538 /* In the shared -Bsymbolic case, discard space allocated for
1539 dynamic pc-relative relocs against symbols which turn out to be
1540 defined in regular objects. For the normal shared case, discard
1541 space for pc-relative relocs that have become local due to symbol
1542 visibility changes. */
1543
1544 if (info->shared)
1545 {
27482721
AJ
1546 /* Relocs that use pc_count are those that appear on a call
1547 insn, or certain REL relocs that can generated via assembly.
1548 We want calls to protected symbols to resolve directly to the
1549 function rather than going via the plt. If people want
1550 function pointer comparisons to work as expected then they
1551 should avoid writing weird assembly. */
1552 if (SYMBOL_CALLS_LOCAL (info, h))
c434dee6
AJ
1553 {
1554 struct elf64_x86_64_dyn_relocs **pp;
1555
1556 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1557 {
1558 p->count -= p->pc_count;
1559 p->pc_count = 0;
1560 if (p->count == 0)
1561 *pp = p->next;
1562 else
1563 pp = &p->next;
1564 }
1565 }
4e795f50
AM
1566
1567 /* Also discard relocs on undefined weak syms with non-default
1568 visibility. */
22d606e9 1569 if (eh->dyn_relocs != NULL
4e795f50 1570 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
1571 {
1572 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1573 eh->dyn_relocs = NULL;
1574
1575 /* Make sure undefined weak symbols are output as a dynamic
1576 symbol in PIEs. */
1577 else if (h->dynindx == -1
1578 && !h->forced_local)
1579 {
1580 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1581 return FALSE;
1582 }
1583 }
c434dee6 1584 }
d40d037c 1585 else if (ELIMINATE_COPY_RELOCS)
c434dee6
AJ
1586 {
1587 /* For the non-shared case, discard space for relocs against
1588 symbols which turn out to need copy relocs or are not
1589 dynamic. */
1590
f5385ebf
AM
1591 if (!h->non_got_ref
1592 && ((h->def_dynamic
1593 && !h->def_regular)
c434dee6
AJ
1594 || (htab->elf.dynamic_sections_created
1595 && (h->root.type == bfd_link_hash_undefweak
1596 || h->root.type == bfd_link_hash_undefined))))
1597 {
1598 /* Make sure this symbol is output as a dynamic symbol.
1599 Undefined weak syms won't yet be marked as dynamic. */
1600 if (h->dynindx == -1
f5385ebf 1601 && !h->forced_local)
c434dee6 1602 {
c152c796 1603 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1604 return FALSE;
c434dee6
AJ
1605 }
1606
1607 /* If that succeeded, we know we'll be keeping all the
1608 relocs. */
1609 if (h->dynindx != -1)
1610 goto keep;
1611 }
1612
1613 eh->dyn_relocs = NULL;
1614
1615 keep: ;
1616 }
1617
1618 /* Finally, allocate space. */
1619 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1620 {
1621 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 1622 sreloc->size += p->count * sizeof (Elf64_External_Rela);
c434dee6
AJ
1623 }
1624
b34976b6 1625 return TRUE;
c434dee6
AJ
1626}
1627
1628/* Find any dynamic relocs that apply to read-only sections. */
1629
b34976b6 1630static bfd_boolean
27482721 1631readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
1632{
1633 struct elf64_x86_64_link_hash_entry *eh;
1634 struct elf64_x86_64_dyn_relocs *p;
1635
e92d460e
AM
1636 if (h->root.type == bfd_link_hash_warning)
1637 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1638
c434dee6
AJ
1639 eh = (struct elf64_x86_64_link_hash_entry *) h;
1640 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1641 {
1642 asection *s = p->sec->output_section;
1643
1644 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1645 {
1646 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1647
1648 info->flags |= DF_TEXTREL;
1649
1650 /* Not an error, just cut short the traversal. */
b34976b6 1651 return FALSE;
c434dee6
AJ
1652 }
1653 }
b34976b6 1654 return TRUE;
c434dee6
AJ
1655}
1656
70256ad8
AJ
1657/* Set the sizes of the dynamic sections. */
1658
b34976b6 1659static bfd_boolean
27482721
AJ
1660elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1661 struct bfd_link_info *info)
70256ad8 1662{
c434dee6 1663 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
1664 bfd *dynobj;
1665 asection *s;
b34976b6 1666 bfd_boolean relocs;
c434dee6 1667 bfd *ibfd;
70256ad8 1668
c434dee6
AJ
1669 htab = elf64_x86_64_hash_table (info);
1670 dynobj = htab->elf.dynobj;
1671 if (dynobj == NULL)
1672 abort ();
70256ad8 1673
c434dee6 1674 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1675 {
1676 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 1677 if (info->executable)
70256ad8
AJ
1678 {
1679 s = bfd_get_section_by_name (dynobj, ".interp");
c434dee6
AJ
1680 if (s == NULL)
1681 abort ();
eea6121a 1682 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
70256ad8
AJ
1683 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1684 }
1685 }
70256ad8 1686
c434dee6
AJ
1687 /* Set up .got offsets for local syms, and space for local dynamic
1688 relocs. */
1689 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
70256ad8 1690 {
c434dee6
AJ
1691 bfd_signed_vma *local_got;
1692 bfd_signed_vma *end_local_got;
bffbf940 1693 char *local_tls_type;
67a4f2b7 1694 bfd_vma *local_tlsdesc_gotent;
c434dee6
AJ
1695 bfd_size_type locsymcount;
1696 Elf_Internal_Shdr *symtab_hdr;
1697 asection *srel;
70256ad8 1698
c434dee6 1699 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
70256ad8
AJ
1700 continue;
1701
c434dee6 1702 for (s = ibfd->sections; s != NULL; s = s->next)
70256ad8 1703 {
c434dee6
AJ
1704 struct elf64_x86_64_dyn_relocs *p;
1705
e81d3500
DD
1706 for (p = (struct elf64_x86_64_dyn_relocs *)
1707 (elf_section_data (s)->local_dynrel);
c434dee6
AJ
1708 p != NULL;
1709 p = p->next)
70256ad8 1710 {
c434dee6
AJ
1711 if (!bfd_is_abs_section (p->sec)
1712 && bfd_is_abs_section (p->sec->output_section))
1713 {
1714 /* Input section has been discarded, either because
1715 it is a copy of a linkonce section or due to
1716 linker script /DISCARD/, so we'll be discarding
1717 the relocs too. */
1718 }
1719 else if (p->count != 0)
1720 {
1721 srel = elf_section_data (p->sec)->sreloc;
eea6121a 1722 srel->size += p->count * sizeof (Elf64_External_Rela);
c434dee6
AJ
1723 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1724 info->flags |= DF_TEXTREL;
1725
1726 }
70256ad8
AJ
1727 }
1728 }
c434dee6
AJ
1729
1730 local_got = elf_local_got_refcounts (ibfd);
1731 if (!local_got)
1732 continue;
1733
1734 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1735 locsymcount = symtab_hdr->sh_info;
1736 end_local_got = local_got + locsymcount;
bffbf940 1737 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
67a4f2b7 1738 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
c434dee6
AJ
1739 s = htab->sgot;
1740 srel = htab->srelgot;
67a4f2b7
AO
1741 for (; local_got < end_local_got;
1742 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
70256ad8 1743 {
67a4f2b7 1744 *local_tlsdesc_gotent = (bfd_vma) -1;
c434dee6 1745 if (*local_got > 0)
70256ad8 1746 {
67a4f2b7
AO
1747 if (GOT_TLS_GDESC_P (*local_tls_type))
1748 {
1749 *local_tlsdesc_gotent = htab->sgotplt->size
1750 - elf64_x86_64_compute_jump_table_size (htab);
1751 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1752 *local_got = (bfd_vma) -2;
1753 }
1754 if (! GOT_TLS_GDESC_P (*local_tls_type)
1755 || GOT_TLS_GD_P (*local_tls_type))
1756 {
1757 *local_got = s->size;
1758 s->size += GOT_ENTRY_SIZE;
1759 if (GOT_TLS_GD_P (*local_tls_type))
1760 s->size += GOT_ENTRY_SIZE;
1761 }
bffbf940 1762 if (info->shared
67a4f2b7 1763 || GOT_TLS_GD_ANY_P (*local_tls_type)
bffbf940 1764 || *local_tls_type == GOT_TLS_IE)
67a4f2b7
AO
1765 {
1766 if (GOT_TLS_GDESC_P (*local_tls_type))
1767 {
1768 htab->srelplt->size += sizeof (Elf64_External_Rela);
1769 htab->tlsdesc_plt = (bfd_vma) -1;
1770 }
1771 if (! GOT_TLS_GDESC_P (*local_tls_type)
1772 || GOT_TLS_GD_P (*local_tls_type))
1773 srel->size += sizeof (Elf64_External_Rela);
1774 }
70256ad8
AJ
1775 }
1776 else
c434dee6
AJ
1777 *local_got = (bfd_vma) -1;
1778 }
1779 }
70256ad8 1780
bffbf940
JJ
1781 if (htab->tls_ld_got.refcount > 0)
1782 {
1783 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1784 relocs. */
eea6121a
AM
1785 htab->tls_ld_got.offset = htab->sgot->size;
1786 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
1787 htab->srelgot->size += sizeof (Elf64_External_Rela);
bffbf940
JJ
1788 }
1789 else
1790 htab->tls_ld_got.offset = -1;
1791
c434dee6
AJ
1792 /* Allocate global sym .plt and .got entries, and space for global
1793 sym dynamic relocs. */
1794 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1795
67a4f2b7
AO
1796 /* For every jump slot reserved in the sgotplt, reloc_count is
1797 incremented. However, when we reserve space for TLS descriptors,
1798 it's not incremented, so in order to compute the space reserved
1799 for them, it suffices to multiply the reloc count by the jump
1800 slot size. */
1801 if (htab->srelplt)
1802 htab->sgotplt_jump_table_size
1803 = elf64_x86_64_compute_jump_table_size (htab);
1804
1805 if (htab->tlsdesc_plt)
1806 {
1807 /* If we're not using lazy TLS relocations, don't generate the
1808 PLT and GOT entries they require. */
1809 if ((info->flags & DF_BIND_NOW))
1810 htab->tlsdesc_plt = 0;
1811 else
1812 {
1813 htab->tlsdesc_got = htab->sgot->size;
1814 htab->sgot->size += GOT_ENTRY_SIZE;
1815 /* Reserve room for the initial entry.
1816 FIXME: we could probably do away with it in this case. */
1817 if (htab->splt->size == 0)
1818 htab->splt->size += PLT_ENTRY_SIZE;
1819 htab->tlsdesc_plt = htab->splt->size;
1820 htab->splt->size += PLT_ENTRY_SIZE;
1821 }
1822 }
1823
c434dee6
AJ
1824 /* We now have determined the sizes of the various dynamic sections.
1825 Allocate memory for them. */
b34976b6 1826 relocs = FALSE;
c434dee6
AJ
1827 for (s = dynobj->sections; s != NULL; s = s->next)
1828 {
1829 if ((s->flags & SEC_LINKER_CREATED) == 0)
1830 continue;
1831
1832 if (s == htab->splt
1833 || s == htab->sgot
75ff4589
L
1834 || s == htab->sgotplt
1835 || s == htab->sdynbss)
c434dee6
AJ
1836 {
1837 /* Strip this section if we don't need it; see the
1838 comment below. */
1839 }
1840 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1841 {
eea6121a 1842 if (s->size != 0 && s != htab->srelplt)
b34976b6 1843 relocs = TRUE;
c434dee6
AJ
1844
1845 /* We use the reloc_count field as a counter if we need
1846 to copy relocs into the output file. */
67a4f2b7
AO
1847 if (s != htab->srelplt)
1848 s->reloc_count = 0;
70256ad8 1849 }
c434dee6 1850 else
70256ad8
AJ
1851 {
1852 /* It's not one of our sections, so don't allocate space. */
1853 continue;
1854 }
1855
eea6121a 1856 if (s->size == 0)
70256ad8 1857 {
c434dee6
AJ
1858 /* If we don't need this section, strip it from the
1859 output file. This is mostly to handle .rela.bss and
1860 .rela.plt. We must create both sections in
1861 create_dynamic_sections, because they must be created
1862 before the linker maps input sections to output
1863 sections. The linker does that before
1864 adjust_dynamic_symbol is called, and it is that
1865 function which decides whether anything needs to go
1866 into these sections. */
1867
8423293d 1868 s->flags |= SEC_EXCLUDE;
70256ad8
AJ
1869 continue;
1870 }
1871
c456f082
AM
1872 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1873 continue;
1874
70256ad8
AJ
1875 /* Allocate memory for the section contents. We use bfd_zalloc
1876 here in case unused entries are not reclaimed before the
1877 section's contents are written out. This should not happen,
1878 but this way if it does, we get a R_X86_64_NONE reloc instead
1879 of garbage. */
eea6121a 1880 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c434dee6 1881 if (s->contents == NULL)
b34976b6 1882 return FALSE;
70256ad8
AJ
1883 }
1884
c434dee6 1885 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1886 {
1887 /* Add some entries to the .dynamic section. We fill in the
1888 values later, in elf64_x86_64_finish_dynamic_sections, but we
1889 must add the entries now so that we get the correct size for
407443a3 1890 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8 1891 dynamic linker and used by the debugger. */
dc810e39 1892#define add_dynamic_entry(TAG, VAL) \
5a580b3a 1893 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 1894
36af4a4e 1895 if (info->executable)
70256ad8 1896 {
dc810e39 1897 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 1898 return FALSE;
70256ad8
AJ
1899 }
1900
eea6121a 1901 if (htab->splt->size != 0)
70256ad8 1902 {
dc810e39
AM
1903 if (!add_dynamic_entry (DT_PLTGOT, 0)
1904 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1905 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1906 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 1907 return FALSE;
67a4f2b7
AO
1908
1909 if (htab->tlsdesc_plt
1910 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
1911 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
1912 return FALSE;
70256ad8
AJ
1913 }
1914
1915 if (relocs)
1916 {
dc810e39
AM
1917 if (!add_dynamic_entry (DT_RELA, 0)
1918 || !add_dynamic_entry (DT_RELASZ, 0)
1919 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 1920 return FALSE;
70256ad8 1921
c434dee6
AJ
1922 /* If any dynamic relocs apply to a read-only section,
1923 then we need a DT_TEXTREL entry. */
1924 if ((info->flags & DF_TEXTREL) == 0)
1925 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1926 (PTR) info);
1927
1928 if ((info->flags & DF_TEXTREL) != 0)
1929 {
1930 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 1931 return FALSE;
c434dee6 1932 }
70256ad8
AJ
1933 }
1934 }
dc810e39 1935#undef add_dynamic_entry
70256ad8 1936
b34976b6 1937 return TRUE;
70256ad8
AJ
1938}
1939
67a4f2b7
AO
1940static bfd_boolean
1941elf64_x86_64_always_size_sections (bfd *output_bfd,
1942 struct bfd_link_info *info)
1943{
1944 asection *tls_sec = elf_hash_table (info)->tls_sec;
1945
1946 if (tls_sec)
1947 {
1948 struct elf_link_hash_entry *tlsbase;
1949
1950 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
1951 "_TLS_MODULE_BASE_",
1952 FALSE, FALSE, FALSE);
1953
1954 if (tlsbase && tlsbase->type == STT_TLS)
1955 {
1956 struct bfd_link_hash_entry *bh = NULL;
1957 const struct elf_backend_data *bed
1958 = get_elf_backend_data (output_bfd);
1959
1960 if (!(_bfd_generic_link_add_one_symbol
1961 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1962 tls_sec, 0, NULL, FALSE,
1963 bed->collect, &bh)))
1964 return FALSE;
1965 tlsbase = (struct elf_link_hash_entry *)bh;
1966 tlsbase->def_regular = 1;
1967 tlsbase->other = STV_HIDDEN;
1968 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
1969 }
1970 }
1971
1972 return TRUE;
1973}
1974
bffbf940
JJ
1975/* Return the base VMA address which should be subtracted from real addresses
1976 when resolving @dtpoff relocation.
1977 This is PT_TLS segment p_vaddr. */
1978
1979static bfd_vma
27482721 1980dtpoff_base (struct bfd_link_info *info)
bffbf940 1981{
e1918d23
AM
1982 /* If tls_sec is NULL, we should have signalled an error already. */
1983 if (elf_hash_table (info)->tls_sec == NULL)
bffbf940 1984 return 0;
e1918d23 1985 return elf_hash_table (info)->tls_sec->vma;
bffbf940
JJ
1986}
1987
1988/* Return the relocation value for @tpoff relocation
1989 if STT_TLS virtual address is ADDRESS. */
1990
1991static bfd_vma
27482721 1992tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 1993{
e1918d23 1994 struct elf_link_hash_table *htab = elf_hash_table (info);
bffbf940
JJ
1995
1996 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 1997 if (htab->tls_sec == NULL)
bffbf940 1998 return 0;
e1918d23 1999 return address - htab->tls_size - htab->tls_sec->vma;
bffbf940
JJ
2000}
2001
90f487df
L
2002/* Is the instruction before OFFSET in CONTENTS a 32bit relative
2003 branch? */
2004
2005static bfd_boolean
2006is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2007{
2008 /* Opcode Instruction
2009 0xe8 call
2010 0xe9 jump
2011 0x0f 0x8x conditional jump */
2012 return ((offset > 0
2013 && (contents [offset - 1] == 0xe8
2014 || contents [offset - 1] == 0xe9))
2015 || (offset > 1
2016 && contents [offset - 2] == 0x0f
2017 && (contents [offset - 1] & 0xf0) == 0x80));
2018}
2019
8d88c4ca
NC
2020/* Relocate an x86_64 ELF section. */
2021
b34976b6 2022static bfd_boolean
27482721
AJ
2023elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2024 bfd *input_bfd, asection *input_section,
2025 bfd_byte *contents, Elf_Internal_Rela *relocs,
2026 Elf_Internal_Sym *local_syms,
2027 asection **local_sections)
8d88c4ca 2028{
c434dee6 2029 struct elf64_x86_64_link_hash_table *htab;
8d88c4ca
NC
2030 Elf_Internal_Shdr *symtab_hdr;
2031 struct elf_link_hash_entry **sym_hashes;
2032 bfd_vma *local_got_offsets;
67a4f2b7 2033 bfd_vma *local_tlsdesc_gotents;
c434dee6 2034 Elf_Internal_Rela *rel;
8d88c4ca
NC
2035 Elf_Internal_Rela *relend;
2036
1049f94e 2037 if (info->relocatable)
b34976b6 2038 return TRUE;
b491616a 2039
c434dee6 2040 htab = elf64_x86_64_hash_table (info);
8d88c4ca
NC
2041 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2042 sym_hashes = elf_sym_hashes (input_bfd);
2043 local_got_offsets = elf_local_got_offsets (input_bfd);
67a4f2b7 2044 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
8d88c4ca 2045
c434dee6 2046 rel = relocs;
8d88c4ca 2047 relend = relocs + input_section->reloc_count;
c434dee6 2048 for (; rel < relend; rel++)
8d88c4ca 2049 {
bffbf940 2050 unsigned int r_type;
8d88c4ca
NC
2051 reloc_howto_type *howto;
2052 unsigned long r_symndx;
2053 struct elf_link_hash_entry *h;
2054 Elf_Internal_Sym *sym;
2055 asection *sec;
67a4f2b7 2056 bfd_vma off, offplt;
8d88c4ca 2057 bfd_vma relocation;
b34976b6 2058 bfd_boolean unresolved_reloc;
8d88c4ca 2059 bfd_reloc_status_type r;
bffbf940 2060 int tls_type;
8d88c4ca 2061
c434dee6 2062 r_type = ELF64_R_TYPE (rel->r_info);
fe4770f4
AJ
2063 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2064 || r_type == (int) R_X86_64_GNU_VTENTRY)
2065 continue;
8d88c4ca 2066
bffbf940 2067 if (r_type >= R_X86_64_max)
8da6118f
KH
2068 {
2069 bfd_set_error (bfd_error_bad_value);
b34976b6 2070 return FALSE;
8da6118f 2071 }
8d88c4ca 2072
b491616a 2073 howto = x86_64_elf_howto_table + r_type;
c434dee6 2074 r_symndx = ELF64_R_SYM (rel->r_info);
8d88c4ca
NC
2075 h = NULL;
2076 sym = NULL;
2077 sec = NULL;
b34976b6 2078 unresolved_reloc = FALSE;
8d88c4ca 2079 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
2080 {
2081 sym = local_syms + r_symndx;
2082 sec = local_sections[r_symndx];
c434dee6 2083
8517fae7 2084 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8da6118f 2085 }
8d88c4ca 2086 else
8da6118f 2087 {
560e09e9 2088 bfd_boolean warned;
c434dee6 2089
b2a8e766
AM
2090 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2091 r_symndx, symtab_hdr, sym_hashes,
2092 h, sec, relocation,
2093 unresolved_reloc, warned);
8da6118f 2094 }
70256ad8
AJ
2095 /* When generating a shared object, the relocations handled here are
2096 copied into the output file to be resolved at run time. */
2097 switch (r_type)
2098 {
2099 case R_X86_64_GOT32:
2100 /* Relocation is to the entry for this symbol in the global
2101 offset table. */
70256ad8
AJ
2102 case R_X86_64_GOTPCREL:
2103 /* Use global offset table as symbol value. */
c434dee6
AJ
2104 if (htab->sgot == NULL)
2105 abort ();
053579d7 2106
51e0a107 2107 if (h != NULL)
70256ad8 2108 {
b34976b6 2109 bfd_boolean dyn;
c434dee6
AJ
2110
2111 off = h->got.offset;
2112 dyn = htab->elf.dynamic_sections_created;
51e0a107 2113
27482721 2114 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
51e0a107 2115 || (info->shared
27482721 2116 && SYMBOL_REFERENCES_LOCAL (info, h))
4bc6e03a
AJ
2117 || (ELF_ST_VISIBILITY (h->other)
2118 && h->root.type == bfd_link_hash_undefweak))
51e0a107
JH
2119 {
2120 /* This is actually a static link, or it is a -Bsymbolic
2121 link and the symbol is defined locally, or the symbol
407443a3 2122 was forced to be local because of a version file. We
51e0a107
JH
2123 must initialize this entry in the global offset table.
2124 Since the offset must always be a multiple of 8, we
2125 use the least significant bit to record whether we
2126 have initialized it already.
2127
2128 When doing a dynamic link, we create a .rela.got
407443a3
AJ
2129 relocation entry to initialize the value. This is
2130 done in the finish_dynamic_symbol routine. */
51e0a107
JH
2131 if ((off & 1) != 0)
2132 off &= ~1;
2133 else
2134 {
2135 bfd_put_64 (output_bfd, relocation,
c434dee6 2136 htab->sgot->contents + off);
51e0a107
JH
2137 h->got.offset |= 1;
2138 }
2139 }
053579d7 2140 else
b34976b6 2141 unresolved_reloc = FALSE;
70256ad8 2142 }
51e0a107
JH
2143 else
2144 {
c434dee6
AJ
2145 if (local_got_offsets == NULL)
2146 abort ();
51e0a107
JH
2147
2148 off = local_got_offsets[r_symndx];
2149
2150 /* The offset must always be a multiple of 8. We use
407443a3
AJ
2151 the least significant bit to record whether we have
2152 already generated the necessary reloc. */
51e0a107
JH
2153 if ((off & 1) != 0)
2154 off &= ~1;
2155 else
2156 {
c434dee6
AJ
2157 bfd_put_64 (output_bfd, relocation,
2158 htab->sgot->contents + off);
51e0a107
JH
2159
2160 if (info->shared)
2161 {
947216bf 2162 asection *s;
51e0a107 2163 Elf_Internal_Rela outrel;
947216bf 2164 bfd_byte *loc;
70256ad8 2165
51e0a107
JH
2166 /* We need to generate a R_X86_64_RELATIVE reloc
2167 for the dynamic linker. */
947216bf
AM
2168 s = htab->srelgot;
2169 if (s == NULL)
c434dee6 2170 abort ();
51e0a107 2171
c434dee6
AJ
2172 outrel.r_offset = (htab->sgot->output_section->vma
2173 + htab->sgot->output_offset
51e0a107
JH
2174 + off);
2175 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2176 outrel.r_addend = relocation;
947216bf
AM
2177 loc = s->contents;
2178 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2179 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
51e0a107
JH
2180 }
2181
2182 local_got_offsets[r_symndx] |= 1;
2183 }
51e0a107 2184 }
6a2bda3f 2185
c434dee6
AJ
2186 if (off >= (bfd_vma) -2)
2187 abort ();
2188
8c37241b
JJ
2189 relocation = htab->sgot->output_section->vma
2190 + htab->sgot->output_offset + off;
2191 if (r_type != R_X86_64_GOTPCREL)
2192 relocation -= htab->sgotplt->output_section->vma
2193 - htab->sgotplt->output_offset;
c434dee6 2194
70256ad8
AJ
2195 break;
2196
d6ab8113
JB
2197 case R_X86_64_GOTOFF64:
2198 /* Relocation is relative to the start of the global offset
2199 table. */
2200
2201 /* Check to make sure it isn't a protected function symbol
2202 for shared library since it may not be local when used
2203 as function address. */
2204 if (info->shared
2205 && h
2206 && h->def_regular
2207 && h->type == STT_FUNC
2208 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2209 {
2210 (*_bfd_error_handler)
2211 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2212 input_bfd, h->root.root.string);
2213 bfd_set_error (bfd_error_bad_value);
2214 return FALSE;
2215 }
2216
2217 /* Note that sgot is not involved in this
2218 calculation. We always want the start of .got.plt. If we
2219 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2220 permitted by the ABI, we might have to change this
2221 calculation. */
2222 relocation -= htab->sgotplt->output_section->vma
2223 + htab->sgotplt->output_offset;
2224 break;
2225
2226 case R_X86_64_GOTPC32:
2227 /* Use global offset table as symbol value. */
2228 relocation = htab->sgotplt->output_section->vma
2229 + htab->sgotplt->output_offset;
2230 unresolved_reloc = FALSE;
2231 break;
2232
70256ad8
AJ
2233 case R_X86_64_PLT32:
2234 /* Relocation is to the entry for this symbol in the
2235 procedure linkage table. */
2236
2237 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 2238 without using the procedure linkage table. */
70256ad8
AJ
2239 if (h == NULL)
2240 break;
2241
c434dee6
AJ
2242 if (h->plt.offset == (bfd_vma) -1
2243 || htab->splt == NULL)
70256ad8
AJ
2244 {
2245 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
2246 happens when statically linking PIC code, or when
2247 using -Bsymbolic. */
70256ad8
AJ
2248 break;
2249 }
2250
c434dee6
AJ
2251 relocation = (htab->splt->output_section->vma
2252 + htab->splt->output_offset
70256ad8 2253 + h->plt.offset);
b34976b6 2254 unresolved_reloc = FALSE;
70256ad8
AJ
2255 break;
2256
fd8ab9e5
AJ
2257 case R_X86_64_PC8:
2258 case R_X86_64_PC16:
2259 case R_X86_64_PC32:
6610a52d
L
2260 if (info->shared
2261 && !SYMBOL_REFERENCES_LOCAL (info, h)
ba3bee0b 2262 && (input_section->flags & SEC_ALLOC) != 0
90f487df
L
2263 && (input_section->flags & SEC_READONLY) != 0
2264 && (!h->def_regular
2265 || r_type != R_X86_64_PC32
2266 || h->type != STT_FUNC
2267 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
2268 || !is_32bit_relative_branch (contents,
2269 rel->r_offset)))
6610a52d 2270 {
90f487df
L
2271 if (h->def_regular
2272 && r_type == R_X86_64_PC32
2273 && h->type == STT_FUNC
2274 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2275 (*_bfd_error_handler)
2276 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
2277 input_bfd, h->root.root.string);
2278 else
2279 (*_bfd_error_handler)
2280 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2281 input_bfd, x86_64_elf_howto_table[r_type].name,
2282 h->root.root.string);
6610a52d
L
2283 bfd_set_error (bfd_error_bad_value);
2284 return FALSE;
2285 }
2286 /* Fall through. */
2287
70256ad8
AJ
2288 case R_X86_64_8:
2289 case R_X86_64_16:
2290 case R_X86_64_32:
d6ab8113 2291 case R_X86_64_PC64:
6b3db546 2292 case R_X86_64_64:
80643fbc 2293 /* FIXME: The ABI says the linker should make sure the value is
407443a3 2294 the same when it's zeroextended to 64 bit. */
c434dee6
AJ
2295
2296 /* r_symndx will be zero only for relocs against symbols
2297 from removed linkonce sections, or sections discarded by
2298 a linker script. */
2299 if (r_symndx == 0
2300 || (input_section->flags & SEC_ALLOC) == 0)
2301 break;
2302
2303 if ((info->shared
4bc6e03a
AJ
2304 && (h == NULL
2305 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2306 || h->root.type != bfd_link_hash_undefweak)
c434dee6
AJ
2307 && ((r_type != R_X86_64_PC8
2308 && r_type != R_X86_64_PC16
d6ab8113
JB
2309 && r_type != R_X86_64_PC32
2310 && r_type != R_X86_64_PC64)
f6c52c13 2311 || !SYMBOL_CALLS_LOCAL (info, h)))
d40d037c
AJ
2312 || (ELIMINATE_COPY_RELOCS
2313 && !info->shared
c434dee6
AJ
2314 && h != NULL
2315 && h->dynindx != -1
f5385ebf
AM
2316 && !h->non_got_ref
2317 && ((h->def_dynamic
2318 && !h->def_regular)
c434dee6
AJ
2319 || h->root.type == bfd_link_hash_undefweak
2320 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
2321 {
2322 Elf_Internal_Rela outrel;
947216bf 2323 bfd_byte *loc;
b34976b6 2324 bfd_boolean skip, relocate;
c434dee6 2325 asection *sreloc;
70256ad8
AJ
2326
2327 /* When generating a shared object, these relocations
2328 are copied into the output file to be resolved at run
407443a3 2329 time. */
b34976b6
AM
2330 skip = FALSE;
2331 relocate = FALSE;
70256ad8 2332
c629eae0
JJ
2333 outrel.r_offset =
2334 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 2335 rel->r_offset);
c629eae0 2336 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 2337 skip = TRUE;
0fb19cbc 2338 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 2339 skip = TRUE, relocate = TRUE;
70256ad8
AJ
2340
2341 outrel.r_offset += (input_section->output_section->vma
2342 + input_section->output_offset);
2343
2344 if (skip)
0bb2d96a 2345 memset (&outrel, 0, sizeof outrel);
c434dee6 2346
fd8ab9e5
AJ
2347 /* h->dynindx may be -1 if this symbol was marked to
2348 become local. */
2349 else if (h != NULL
c434dee6
AJ
2350 && h->dynindx != -1
2351 && (r_type == R_X86_64_PC8
2352 || r_type == R_X86_64_PC16
2353 || r_type == R_X86_64_PC32
d6ab8113 2354 || r_type == R_X86_64_PC64
c434dee6
AJ
2355 || !info->shared
2356 || !info->symbolic
f5385ebf 2357 || !h->def_regular))
70256ad8 2358 {
70256ad8 2359 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
c434dee6 2360 outrel.r_addend = rel->r_addend;
70256ad8
AJ
2361 }
2362 else
2363 {
c434dee6 2364 /* This symbol is local, or marked to become local. */
607c0e09
AS
2365 if (r_type == R_X86_64_64)
2366 {
b34976b6 2367 relocate = TRUE;
607c0e09
AS
2368 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2369 outrel.r_addend = relocation + rel->r_addend;
2370 }
2371 else
2372 {
2373 long sindx;
2374
8517fae7 2375 if (bfd_is_abs_section (sec))
607c0e09
AS
2376 sindx = 0;
2377 else if (sec == NULL || sec->owner == NULL)
2378 {
2379 bfd_set_error (bfd_error_bad_value);
b34976b6 2380 return FALSE;
607c0e09
AS
2381 }
2382 else
2383 {
2384 asection *osec;
2385
2386 osec = sec->output_section;
2387 sindx = elf_section_data (osec)->dynindx;
2388 BFD_ASSERT (sindx > 0);
2389 }
2390
2391 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2392 outrel.r_addend = relocation + rel->r_addend;
2393 }
70256ad8
AJ
2394 }
2395
c434dee6
AJ
2396 sreloc = elf_section_data (input_section)->sreloc;
2397 if (sreloc == NULL)
2398 abort ();
2399
947216bf
AM
2400 loc = sreloc->contents;
2401 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2402 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
70256ad8
AJ
2403
2404 /* If this reloc is against an external symbol, we do
2405 not want to fiddle with the addend. Otherwise, we
2406 need to include the symbol value so that it becomes
2407 an addend for the dynamic reloc. */
2408 if (! relocate)
2409 continue;
2410 }
2411
2412 break;
2413
bffbf940 2414 case R_X86_64_TLSGD:
67a4f2b7
AO
2415 case R_X86_64_GOTPC32_TLSDESC:
2416 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
2417 case R_X86_64_GOTTPOFF:
2418 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2419 tls_type = GOT_UNKNOWN;
2420 if (h == NULL && local_got_offsets)
2421 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2422 else if (h != NULL)
2423 {
2424 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2425 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2426 r_type = R_X86_64_TPOFF32;
2427 }
67a4f2b7
AO
2428 if (r_type == R_X86_64_TLSGD
2429 || r_type == R_X86_64_GOTPC32_TLSDESC
2430 || r_type == R_X86_64_TLSDESC_CALL)
bffbf940
JJ
2431 {
2432 if (tls_type == GOT_TLS_IE)
2433 r_type = R_X86_64_GOTTPOFF;
2434 }
2435
2436 if (r_type == R_X86_64_TPOFF32)
2437 {
2438 BFD_ASSERT (! unresolved_reloc);
2439 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2440 {
2441 unsigned int i;
abcf1d52
JJ
2442 static unsigned char tlsgd[8]
2443 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
bffbf940
JJ
2444
2445 /* GD->LE transition.
abcf1d52
JJ
2446 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2447 .word 0x6666; rex64; call __tls_get_addr@plt
bffbf940
JJ
2448 Change it into:
2449 movq %fs:0, %rax
2450 leaq foo@tpoff(%rax), %rax */
abcf1d52
JJ
2451 BFD_ASSERT (rel->r_offset >= 4);
2452 for (i = 0; i < 4; i++)
bffbf940 2453 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52 2454 contents + rel->r_offset - 4 + i)
bffbf940 2455 == tlsgd[i]);
eea6121a 2456 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
abcf1d52
JJ
2457 for (i = 0; i < 4; i++)
2458 BFD_ASSERT (bfd_get_8 (input_bfd,
2459 contents + rel->r_offset + 4 + i)
2460 == tlsgd[i+4]);
bffbf940
JJ
2461 BFD_ASSERT (rel + 1 < relend);
2462 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
abcf1d52 2463 memcpy (contents + rel->r_offset - 4,
bffbf940
JJ
2464 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2465 16);
2466 bfd_put_32 (output_bfd, tpoff (info, relocation),
abcf1d52 2467 contents + rel->r_offset + 8);
bffbf940
JJ
2468 /* Skip R_X86_64_PLT32. */
2469 rel++;
2470 continue;
2471 }
67a4f2b7
AO
2472 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2473 {
2474 /* GDesc -> LE transition.
2475 It's originally something like:
2476 leaq x@tlsdesc(%rip), %rax
2477
2478 Change it to:
2479 movl $x@tpoff, %rax
2480
2481 Registers other than %rax may be set up here. */
2482
2483 unsigned int val, type, type2;
2484 bfd_vma roff;
2485
2486 /* First, make sure it's a leaq adding rip to a
2487 32-bit offset into any register, although it's
2488 probably almost always going to be rax. */
2489 roff = rel->r_offset;
2490 BFD_ASSERT (roff >= 3);
2491 type = bfd_get_8 (input_bfd, contents + roff - 3);
2492 BFD_ASSERT ((type & 0xfb) == 0x48);
2493 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2494 BFD_ASSERT (type2 == 0x8d);
2495 val = bfd_get_8 (input_bfd, contents + roff - 1);
2496 BFD_ASSERT ((val & 0xc7) == 0x05);
2497 BFD_ASSERT (roff + 4 <= input_section->size);
2498
2499 /* Now modify the instruction as appropriate. */
2500 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
2501 contents + roff - 3);
2502 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
2503 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2504 contents + roff - 1);
2505 bfd_put_32 (output_bfd, tpoff (info, relocation),
2506 contents + roff);
2507 continue;
2508 }
2509 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2510 {
2511 /* GDesc -> LE transition.
2512 It's originally:
2513 call *(%rax)
2514 Turn it into:
2515 nop; nop. */
2516
2517 unsigned int val, type;
2518 bfd_vma roff;
2519
2520 /* First, make sure it's a call *(%rax). */
2521 roff = rel->r_offset;
2522 BFD_ASSERT (roff + 2 <= input_section->size);
2523 type = bfd_get_8 (input_bfd, contents + roff);
2524 BFD_ASSERT (type == 0xff);
2525 val = bfd_get_8 (input_bfd, contents + roff + 1);
2526 BFD_ASSERT (val == 0x10);
2527
2528 /* Now modify the instruction as appropriate. */
2529 bfd_put_8 (output_bfd, 0x90, contents + roff);
2530 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2531 continue;
2532 }
bffbf940
JJ
2533 else
2534 {
2535 unsigned int val, type, reg;
2536
2537 /* IE->LE transition:
2538 Originally it can be one of:
2539 movq foo@gottpoff(%rip), %reg
2540 addq foo@gottpoff(%rip), %reg
2541 We change it into:
2542 movq $foo, %reg
2543 leaq foo(%reg), %reg
2544 addq $foo, %reg. */
2545 BFD_ASSERT (rel->r_offset >= 3);
2546 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2547 BFD_ASSERT (val == 0x48 || val == 0x4c);
2548 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2549 BFD_ASSERT (type == 0x8b || type == 0x03);
2550 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2551 BFD_ASSERT ((reg & 0xc7) == 5);
2552 reg >>= 3;
eea6121a 2553 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
bffbf940
JJ
2554 if (type == 0x8b)
2555 {
2556 /* movq */
2557 if (val == 0x4c)
2558 bfd_put_8 (output_bfd, 0x49,
2559 contents + rel->r_offset - 3);
2560 bfd_put_8 (output_bfd, 0xc7,
2561 contents + rel->r_offset - 2);
2562 bfd_put_8 (output_bfd, 0xc0 | reg,
2563 contents + rel->r_offset - 1);
2564 }
2565 else if (reg == 4)
2566 {
2567 /* addq -> addq - addressing with %rsp/%r12 is
2568 special */
2569 if (val == 0x4c)
2570 bfd_put_8 (output_bfd, 0x49,
2571 contents + rel->r_offset - 3);
2572 bfd_put_8 (output_bfd, 0x81,
2573 contents + rel->r_offset - 2);
2574 bfd_put_8 (output_bfd, 0xc0 | reg,
2575 contents + rel->r_offset - 1);
2576 }
2577 else
2578 {
2579 /* addq -> leaq */
2580 if (val == 0x4c)
2581 bfd_put_8 (output_bfd, 0x4d,
2582 contents + rel->r_offset - 3);
2583 bfd_put_8 (output_bfd, 0x8d,
2584 contents + rel->r_offset - 2);
2585 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2586 contents + rel->r_offset - 1);
2587 }
2588 bfd_put_32 (output_bfd, tpoff (info, relocation),
2589 contents + rel->r_offset);
2590 continue;
2591 }
2592 }
2593
2594 if (htab->sgot == NULL)
2595 abort ();
2596
2597 if (h != NULL)
67a4f2b7
AO
2598 {
2599 off = h->got.offset;
2600 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
2601 }
bffbf940
JJ
2602 else
2603 {
2604 if (local_got_offsets == NULL)
2605 abort ();
2606
2607 off = local_got_offsets[r_symndx];
67a4f2b7 2608 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
2609 }
2610
2611 if ((off & 1) != 0)
2612 off &= ~1;
26e41594 2613 else
bffbf940
JJ
2614 {
2615 Elf_Internal_Rela outrel;
947216bf 2616 bfd_byte *loc;
bffbf940 2617 int dr_type, indx;
67a4f2b7 2618 asection *sreloc;
bffbf940
JJ
2619
2620 if (htab->srelgot == NULL)
2621 abort ();
2622
67a4f2b7
AO
2623 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2624
2625 if (GOT_TLS_GDESC_P (tls_type))
2626 {
2627 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
2628 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
2629 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
2630 outrel.r_offset = (htab->sgotplt->output_section->vma
2631 + htab->sgotplt->output_offset
2632 + offplt
2633 + htab->sgotplt_jump_table_size);
2634 sreloc = htab->srelplt;
2635 loc = sreloc->contents;
2636 loc += sreloc->reloc_count++
2637 * sizeof (Elf64_External_Rela);
2638 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2639 <= sreloc->contents + sreloc->size);
2640 if (indx == 0)
2641 outrel.r_addend = relocation - dtpoff_base (info);
2642 else
2643 outrel.r_addend = 0;
2644 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2645 }
2646
2647 sreloc = htab->srelgot;
2648
bffbf940
JJ
2649 outrel.r_offset = (htab->sgot->output_section->vma
2650 + htab->sgot->output_offset + off);
2651
67a4f2b7 2652 if (GOT_TLS_GD_P (tls_type))
bffbf940 2653 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
2654 else if (GOT_TLS_GDESC_P (tls_type))
2655 goto dr_done;
bffbf940
JJ
2656 else
2657 dr_type = R_X86_64_TPOFF64;
2658
2659 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2660 outrel.r_addend = 0;
67a4f2b7
AO
2661 if ((dr_type == R_X86_64_TPOFF64
2662 || dr_type == R_X86_64_TLSDESC) && indx == 0)
bffbf940
JJ
2663 outrel.r_addend = relocation - dtpoff_base (info);
2664 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2665
67a4f2b7
AO
2666 loc = sreloc->contents;
2667 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2668 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2669 <= sreloc->contents + sreloc->size);
bffbf940
JJ
2670 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2671
67a4f2b7 2672 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
2673 {
2674 if (indx == 0)
2675 {
d40d037c 2676 BFD_ASSERT (! unresolved_reloc);
bffbf940
JJ
2677 bfd_put_64 (output_bfd,
2678 relocation - dtpoff_base (info),
2679 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2680 }
2681 else
2682 {
2683 bfd_put_64 (output_bfd, 0,
2684 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2685 outrel.r_info = ELF64_R_INFO (indx,
2686 R_X86_64_DTPOFF64);
2687 outrel.r_offset += GOT_ENTRY_SIZE;
67a4f2b7 2688 sreloc->reloc_count++;
947216bf 2689 loc += sizeof (Elf64_External_Rela);
67a4f2b7
AO
2690 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2691 <= sreloc->contents + sreloc->size);
947216bf 2692 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
bffbf940
JJ
2693 }
2694 }
2695
67a4f2b7 2696 dr_done:
bffbf940
JJ
2697 if (h != NULL)
2698 h->got.offset |= 1;
2699 else
2700 local_got_offsets[r_symndx] |= 1;
2701 }
2702
67a4f2b7
AO
2703 if (off >= (bfd_vma) -2
2704 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940
JJ
2705 abort ();
2706 if (r_type == ELF64_R_TYPE (rel->r_info))
2707 {
67a4f2b7
AO
2708 if (r_type == R_X86_64_GOTPC32_TLSDESC
2709 || r_type == R_X86_64_TLSDESC_CALL)
2710 relocation = htab->sgotplt->output_section->vma
2711 + htab->sgotplt->output_offset
2712 + offplt + htab->sgotplt_jump_table_size;
2713 else
2714 relocation = htab->sgot->output_section->vma
2715 + htab->sgot->output_offset + off;
b34976b6 2716 unresolved_reloc = FALSE;
bffbf940 2717 }
67a4f2b7 2718 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
bffbf940
JJ
2719 {
2720 unsigned int i;
abcf1d52
JJ
2721 static unsigned char tlsgd[8]
2722 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
bffbf940
JJ
2723
2724 /* GD->IE transition.
abcf1d52
JJ
2725 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2726 .word 0x6666; rex64; call __tls_get_addr@plt
bffbf940
JJ
2727 Change it into:
2728 movq %fs:0, %rax
2729 addq foo@gottpoff(%rip), %rax */
abcf1d52
JJ
2730 BFD_ASSERT (rel->r_offset >= 4);
2731 for (i = 0; i < 4; i++)
26e41594 2732 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52 2733 contents + rel->r_offset - 4 + i)
bffbf940 2734 == tlsgd[i]);
eea6121a 2735 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
abcf1d52 2736 for (i = 0; i < 4; i++)
26e41594 2737 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52
JJ
2738 contents + rel->r_offset + 4 + i)
2739 == tlsgd[i+4]);
bffbf940
JJ
2740 BFD_ASSERT (rel + 1 < relend);
2741 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
abcf1d52 2742 memcpy (contents + rel->r_offset - 4,
bffbf940
JJ
2743 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2744 16);
2745
2746 relocation = (htab->sgot->output_section->vma
2747 + htab->sgot->output_offset + off
2748 - rel->r_offset
2749 - input_section->output_section->vma
2750 - input_section->output_offset
abcf1d52 2751 - 12);
bffbf940 2752 bfd_put_32 (output_bfd, relocation,
abcf1d52 2753 contents + rel->r_offset + 8);
bffbf940
JJ
2754 /* Skip R_X86_64_PLT32. */
2755 rel++;
2756 continue;
2757 }
67a4f2b7
AO
2758 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2759 {
2760 /* GDesc -> IE transition.
2761 It's originally something like:
2762 leaq x@tlsdesc(%rip), %rax
2763
2764 Change it to:
2765 movq x@gottpoff(%rip), %rax # before nop; nop
2766
2767 Registers other than %rax may be set up here. */
2768
2769 unsigned int val, type, type2;
2770 bfd_vma roff;
2771
2772 /* First, make sure it's a leaq adding rip to a 32-bit
2773 offset into any register, although it's probably
2774 almost always going to be rax. */
2775 roff = rel->r_offset;
2776 BFD_ASSERT (roff >= 3);
2777 type = bfd_get_8 (input_bfd, contents + roff - 3);
2778 BFD_ASSERT ((type & 0xfb) == 0x48);
2779 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2780 BFD_ASSERT (type2 == 0x8d);
2781 val = bfd_get_8 (input_bfd, contents + roff - 1);
2782 BFD_ASSERT ((val & 0xc7) == 0x05);
2783 BFD_ASSERT (roff + 4 <= input_section->size);
2784
2785 /* Now modify the instruction as appropriate. */
2786 /* To turn a leaq into a movq in the form we use it, it
2787 suffices to change the second byte from 0x8d to
2788 0x8b. */
2789 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
2790
2791 bfd_put_32 (output_bfd,
2792 htab->sgot->output_section->vma
2793 + htab->sgot->output_offset + off
2794 - rel->r_offset
2795 - input_section->output_section->vma
2796 - input_section->output_offset
2797 - 4,
2798 contents + roff);
2799 continue;
2800 }
2801 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2802 {
2803 /* GDesc -> IE transition.
2804 It's originally:
2805 call *(%rax)
2806
2807 Change it to:
2808 nop; nop. */
2809
2810 unsigned int val, type;
2811 bfd_vma roff;
2812
2813 /* First, make sure it's a call *(%eax). */
2814 roff = rel->r_offset;
2815 BFD_ASSERT (roff + 2 <= input_section->size);
2816 type = bfd_get_8 (input_bfd, contents + roff);
2817 BFD_ASSERT (type == 0xff);
2818 val = bfd_get_8 (input_bfd, contents + roff + 1);
2819 BFD_ASSERT (val == 0x10);
2820
2821 /* Now modify the instruction as appropriate. */
2822 bfd_put_8 (output_bfd, 0x90, contents + roff);
2823 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2824
2825 continue;
2826 }
2827 else
2828 BFD_ASSERT (FALSE);
bffbf940
JJ
2829 break;
2830
2831 case R_X86_64_TLSLD:
2832 if (! info->shared)
2833 {
2834 /* LD->LE transition:
2835 Ensure it is:
2836 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2837 We change it into:
2838 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2839 BFD_ASSERT (rel->r_offset >= 3);
2840 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2841 == 0x48);
2842 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2843 == 0x8d);
2844 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2845 == 0x3d);
eea6121a 2846 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
bffbf940
JJ
2847 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2848 == 0xe8);
2849 BFD_ASSERT (rel + 1 < relend);
2850 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2851 memcpy (contents + rel->r_offset - 3,
2852 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2853 /* Skip R_X86_64_PLT32. */
2854 rel++;
2855 continue;
2856 }
2857
2858 if (htab->sgot == NULL)
2859 abort ();
2860
2861 off = htab->tls_ld_got.offset;
2862 if (off & 1)
2863 off &= ~1;
2864 else
2865 {
2866 Elf_Internal_Rela outrel;
947216bf 2867 bfd_byte *loc;
bffbf940
JJ
2868
2869 if (htab->srelgot == NULL)
2870 abort ();
2871
2872 outrel.r_offset = (htab->sgot->output_section->vma
2873 + htab->sgot->output_offset + off);
2874
2875 bfd_put_64 (output_bfd, 0,
2876 htab->sgot->contents + off);
2877 bfd_put_64 (output_bfd, 0,
2878 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2879 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2880 outrel.r_addend = 0;
947216bf
AM
2881 loc = htab->srelgot->contents;
2882 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
bffbf940
JJ
2883 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2884 htab->tls_ld_got.offset |= 1;
2885 }
2886 relocation = htab->sgot->output_section->vma
2887 + htab->sgot->output_offset + off;
b34976b6 2888 unresolved_reloc = FALSE;
bffbf940
JJ
2889 break;
2890
2891 case R_X86_64_DTPOFF32:
a45bb67d 2892 if (info->shared || (input_section->flags & SEC_CODE) == 0)
bffbf940
JJ
2893 relocation -= dtpoff_base (info);
2894 else
2895 relocation = tpoff (info, relocation);
2896 break;
2897
2898 case R_X86_64_TPOFF32:
2899 BFD_ASSERT (! info->shared);
2900 relocation = tpoff (info, relocation);
2901 break;
2902
70256ad8
AJ
2903 default:
2904 break;
2905 }
8d88c4ca 2906
239e1f3a
AM
2907 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2908 because such sections are not SEC_ALLOC and thus ld.so will
2909 not process them. */
c434dee6 2910 if (unresolved_reloc
239e1f3a 2911 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 2912 && h->def_dynamic))
c434dee6 2913 (*_bfd_error_handler)
843fe662 2914 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
2915 input_bfd,
2916 input_section,
c434dee6 2917 (long) rel->r_offset,
843fe662 2918 howto->name,
c434dee6
AJ
2919 h->root.root.string);
2920
8d88c4ca 2921 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
2922 contents, rel->r_offset,
2923 relocation, rel->r_addend);
8d88c4ca
NC
2924
2925 if (r != bfd_reloc_ok)
8da6118f 2926 {
c434dee6
AJ
2927 const char *name;
2928
2929 if (h != NULL)
2930 name = h->root.root.string;
2931 else
8da6118f 2932 {
c434dee6
AJ
2933 name = bfd_elf_string_from_elf_section (input_bfd,
2934 symtab_hdr->sh_link,
2935 sym->st_name);
2936 if (name == NULL)
b34976b6 2937 return FALSE;
c434dee6
AJ
2938 if (*name == '\0')
2939 name = bfd_section_name (input_bfd, sec);
2940 }
2941
2942 if (r == bfd_reloc_overflow)
2943 {
3ffa5234
AM
2944 if (h != NULL
2945 && h->root.type == bfd_link_hash_undefweak
2946 && howto->pc_relative)
2947 /* Ignore reloc overflow on branches to undefweak syms. */
2948 continue;
c434dee6
AJ
2949
2950 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
2951 (info, (h ? &h->root : NULL), name, howto->name,
2952 (bfd_vma) 0, input_bfd, input_section,
2953 rel->r_offset)))
b34976b6 2954 return FALSE;
c434dee6
AJ
2955 }
2956 else
2957 {
2958 (*_bfd_error_handler)
d003868e
AM
2959 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2960 input_bfd, input_section,
c434dee6 2961 (long) rel->r_offset, name, (int) r);
b34976b6 2962 return FALSE;
8da6118f
KH
2963 }
2964 }
8d88c4ca 2965 }
70256ad8 2966
b34976b6 2967 return TRUE;
70256ad8
AJ
2968}
2969
2970/* Finish up dynamic symbol handling. We set the contents of various
2971 dynamic sections here. */
2972
b34976b6 2973static bfd_boolean
27482721
AJ
2974elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
2975 struct bfd_link_info *info,
2976 struct elf_link_hash_entry *h,
2977 Elf_Internal_Sym *sym)
70256ad8 2978{
c434dee6 2979 struct elf64_x86_64_link_hash_table *htab;
70256ad8 2980
c434dee6 2981 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
2982
2983 if (h->plt.offset != (bfd_vma) -1)
2984 {
70256ad8
AJ
2985 bfd_vma plt_index;
2986 bfd_vma got_offset;
2987 Elf_Internal_Rela rela;
947216bf 2988 bfd_byte *loc;
70256ad8
AJ
2989
2990 /* This symbol has an entry in the procedure linkage table. Set
407443a3 2991 it up. */
c434dee6
AJ
2992 if (h->dynindx == -1
2993 || htab->splt == NULL
2994 || htab->sgotplt == NULL
2995 || htab->srelplt == NULL)
2996 abort ();
70256ad8
AJ
2997
2998 /* Get the index in the procedure linkage table which
2999 corresponds to this symbol. This is the index of this symbol
3000 in all the symbols for which we are making plt entries. The
3001 first entry in the procedure linkage table is reserved. */
3002 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3003
3004 /* Get the offset into the .got table of the entry that
407443a3 3005 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
fe4770f4 3006 bytes. The first three are reserved for the dynamic linker. */
70256ad8
AJ
3007 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3008
3009 /* Fill in the entry in the procedure linkage table. */
c434dee6 3010 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
70256ad8
AJ
3011 PLT_ENTRY_SIZE);
3012
3013 /* Insert the relocation positions of the plt section. The magic
3014 numbers at the end of the statements are the positions of the
3015 relocations in the plt section. */
653165cc
AJ
3016 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3017 instruction uses 6 bytes, subtract this value. */
3018 bfd_put_32 (output_bfd,
c434dee6
AJ
3019 (htab->sgotplt->output_section->vma
3020 + htab->sgotplt->output_offset
653165cc 3021 + got_offset
c434dee6
AJ
3022 - htab->splt->output_section->vma
3023 - htab->splt->output_offset
653165cc
AJ
3024 - h->plt.offset
3025 - 6),
c434dee6 3026 htab->splt->contents + h->plt.offset + 2);
653165cc
AJ
3027 /* Put relocation index. */
3028 bfd_put_32 (output_bfd, plt_index,
c434dee6 3029 htab->splt->contents + h->plt.offset + 7);
653165cc
AJ
3030 /* Put offset for jmp .PLT0. */
3031 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
c434dee6 3032 htab->splt->contents + h->plt.offset + 12);
70256ad8 3033
653165cc
AJ
3034 /* Fill in the entry in the global offset table, initially this
3035 points to the pushq instruction in the PLT which is at offset 6. */
c434dee6
AJ
3036 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
3037 + htab->splt->output_offset
70256ad8 3038 + h->plt.offset + 6),
c434dee6 3039 htab->sgotplt->contents + got_offset);
70256ad8
AJ
3040
3041 /* Fill in the entry in the .rela.plt section. */
c434dee6
AJ
3042 rela.r_offset = (htab->sgotplt->output_section->vma
3043 + htab->sgotplt->output_offset
70256ad8
AJ
3044 + got_offset);
3045 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3046 rela.r_addend = 0;
947216bf 3047 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
c434dee6 3048 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8 3049
f5385ebf 3050 if (!h->def_regular)
70256ad8
AJ
3051 {
3052 /* Mark the symbol as undefined, rather than as defined in
47a9f7b3
JJ
3053 the .plt section. Leave the value if there were any
3054 relocations where pointer equality matters (this is a clue
c434dee6
AJ
3055 for the dynamic linker, to make function pointer
3056 comparisons work between an application and shared
47a9f7b3
JJ
3057 library), otherwise set it to zero. If a function is only
3058 called from a binary, there is no need to slow down
3059 shared libraries because of that. */
70256ad8 3060 sym->st_shndx = SHN_UNDEF;
f5385ebf 3061 if (!h->pointer_equality_needed)
47a9f7b3 3062 sym->st_value = 0;
70256ad8
AJ
3063 }
3064 }
3065
bffbf940 3066 if (h->got.offset != (bfd_vma) -1
67a4f2b7 3067 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
bffbf940 3068 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
053579d7 3069 {
053579d7 3070 Elf_Internal_Rela rela;
947216bf 3071 bfd_byte *loc;
053579d7
AJ
3072
3073 /* This symbol has an entry in the global offset table. Set it
bffbf940 3074 up. */
c434dee6
AJ
3075 if (htab->sgot == NULL || htab->srelgot == NULL)
3076 abort ();
053579d7 3077
c434dee6
AJ
3078 rela.r_offset = (htab->sgot->output_section->vma
3079 + htab->sgot->output_offset
dc810e39 3080 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
3081
3082 /* If this is a static link, or it is a -Bsymbolic link and the
3083 symbol is defined locally or was forced to be local because
3084 of a version file, we just want to emit a RELATIVE reloc.
3085 The entry in the global offset table will already have been
3086 initialized in the relocate_section function. */
c434dee6 3087 if (info->shared
27482721 3088 && SYMBOL_REFERENCES_LOCAL (info, h))
053579d7 3089 {
cc78d0af 3090 BFD_ASSERT((h->got.offset & 1) != 0);
053579d7
AJ
3091 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3092 rela.r_addend = (h->root.u.def.value
3093 + h->root.u.def.section->output_section->vma
3094 + h->root.u.def.section->output_offset);
3095 }
3096 else
3097 {
3098 BFD_ASSERT((h->got.offset & 1) == 0);
c434dee6
AJ
3099 bfd_put_64 (output_bfd, (bfd_vma) 0,
3100 htab->sgot->contents + h->got.offset);
053579d7
AJ
3101 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3102 rela.r_addend = 0;
3103 }
3104
947216bf
AM
3105 loc = htab->srelgot->contents;
3106 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 3107 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
053579d7
AJ
3108 }
3109
f5385ebf 3110 if (h->needs_copy)
70256ad8 3111 {
70256ad8 3112 Elf_Internal_Rela rela;
947216bf 3113 bfd_byte *loc;
70256ad8
AJ
3114
3115 /* This symbol needs a copy reloc. Set it up. */
3116
c434dee6
AJ
3117 if (h->dynindx == -1
3118 || (h->root.type != bfd_link_hash_defined
3119 && h->root.type != bfd_link_hash_defweak)
3120 || htab->srelbss == NULL)
3121 abort ();
70256ad8
AJ
3122
3123 rela.r_offset = (h->root.u.def.value
3124 + h->root.u.def.section->output_section->vma
3125 + h->root.u.def.section->output_offset);
3126 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3127 rela.r_addend = 0;
947216bf
AM
3128 loc = htab->srelbss->contents;
3129 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 3130 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8
AJ
3131 }
3132
3133 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3134 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
22edb2f1 3135 || h == htab->elf.hgot)
70256ad8
AJ
3136 sym->st_shndx = SHN_ABS;
3137
b34976b6 3138 return TRUE;
70256ad8
AJ
3139}
3140
c434dee6
AJ
3141/* Used to decide how to sort relocs in an optimal manner for the
3142 dynamic linker, before writing them out. */
3143
3144static enum elf_reloc_type_class
27482721 3145elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
c434dee6
AJ
3146{
3147 switch ((int) ELF64_R_TYPE (rela->r_info))
3148 {
3149 case R_X86_64_RELATIVE:
3150 return reloc_class_relative;
3151 case R_X86_64_JUMP_SLOT:
3152 return reloc_class_plt;
3153 case R_X86_64_COPY:
3154 return reloc_class_copy;
3155 default:
3156 return reloc_class_normal;
3157 }
3158}
3159
70256ad8
AJ
3160/* Finish up the dynamic sections. */
3161
b34976b6 3162static bfd_boolean
27482721 3163elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
70256ad8 3164{
c434dee6 3165 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
3166 bfd *dynobj;
3167 asection *sdyn;
70256ad8 3168
c434dee6
AJ
3169 htab = elf64_x86_64_hash_table (info);
3170 dynobj = htab->elf.dynobj;
70256ad8
AJ
3171 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3172
c434dee6 3173 if (htab->elf.dynamic_sections_created)
70256ad8 3174 {
70256ad8
AJ
3175 Elf64_External_Dyn *dyncon, *dynconend;
3176
c434dee6
AJ
3177 if (sdyn == NULL || htab->sgot == NULL)
3178 abort ();
70256ad8
AJ
3179
3180 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 3181 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
70256ad8
AJ
3182 for (; dyncon < dynconend; dyncon++)
3183 {
3184 Elf_Internal_Dyn dyn;
70256ad8
AJ
3185 asection *s;
3186
3187 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3188
3189 switch (dyn.d_tag)
3190 {
3191 default:
053579d7 3192 continue;
70256ad8
AJ
3193
3194 case DT_PLTGOT:
8c37241b
JJ
3195 s = htab->sgotplt;
3196 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
c434dee6 3197 break;
70256ad8
AJ
3198
3199 case DT_JMPREL:
c434dee6
AJ
3200 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3201 break;
70256ad8 3202
c434dee6
AJ
3203 case DT_PLTRELSZ:
3204 s = htab->srelplt->output_section;
eea6121a 3205 dyn.d_un.d_val = s->size;
70256ad8
AJ
3206 break;
3207
3208 case DT_RELASZ:
c434dee6
AJ
3209 /* The procedure linkage table relocs (DT_JMPREL) should
3210 not be included in the overall relocs (DT_RELA).
3211 Therefore, we override the DT_RELASZ entry here to
3212 make it not include the JMPREL relocs. Since the
3213 linker script arranges for .rela.plt to follow all
3214 other relocation sections, we don't have to worry
3215 about changing the DT_RELA entry. */
3216 if (htab->srelplt != NULL)
70256ad8 3217 {
c434dee6 3218 s = htab->srelplt->output_section;
eea6121a 3219 dyn.d_un.d_val -= s->size;
70256ad8
AJ
3220 }
3221 break;
67a4f2b7
AO
3222
3223 case DT_TLSDESC_PLT:
3224 s = htab->splt;
3225 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3226 + htab->tlsdesc_plt;
3227 break;
3228
3229 case DT_TLSDESC_GOT:
3230 s = htab->sgot;
3231 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3232 + htab->tlsdesc_got;
3233 break;
70256ad8 3234 }
c434dee6 3235
70256ad8
AJ
3236 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3237 }
3238
c434dee6 3239 /* Fill in the special first entry in the procedure linkage table. */
eea6121a 3240 if (htab->splt && htab->splt->size > 0)
70256ad8 3241 {
653165cc 3242 /* Fill in the first entry in the procedure linkage table. */
c434dee6
AJ
3243 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
3244 PLT_ENTRY_SIZE);
653165cc
AJ
3245 /* Add offset for pushq GOT+8(%rip), since the instruction
3246 uses 6 bytes subtract this value. */
3247 bfd_put_32 (output_bfd,
c434dee6
AJ
3248 (htab->sgotplt->output_section->vma
3249 + htab->sgotplt->output_offset
653165cc 3250 + 8
c434dee6
AJ
3251 - htab->splt->output_section->vma
3252 - htab->splt->output_offset
653165cc 3253 - 6),
c434dee6 3254 htab->splt->contents + 2);
653165cc
AJ
3255 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3256 the end of the instruction. */
3257 bfd_put_32 (output_bfd,
c434dee6
AJ
3258 (htab->sgotplt->output_section->vma
3259 + htab->sgotplt->output_offset
653165cc 3260 + 16
c434dee6
AJ
3261 - htab->splt->output_section->vma
3262 - htab->splt->output_offset
653165cc 3263 - 12),
c434dee6 3264 htab->splt->contents + 8);
653165cc 3265
c434dee6
AJ
3266 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
3267 PLT_ENTRY_SIZE;
67a4f2b7
AO
3268
3269 if (htab->tlsdesc_plt)
3270 {
3271 bfd_put_64 (output_bfd, (bfd_vma) 0,
3272 htab->sgot->contents + htab->tlsdesc_got);
3273
3274 memcpy (htab->splt->contents + htab->tlsdesc_plt,
3275 elf64_x86_64_plt0_entry,
3276 PLT_ENTRY_SIZE);
3277
3278 /* Add offset for pushq GOT+8(%rip), since the
3279 instruction uses 6 bytes subtract this value. */
3280 bfd_put_32 (output_bfd,
3281 (htab->sgotplt->output_section->vma
3282 + htab->sgotplt->output_offset
3283 + 8
3284 - htab->splt->output_section->vma
3285 - htab->splt->output_offset
3286 - htab->tlsdesc_plt
3287 - 6),
3288 htab->splt->contents + htab->tlsdesc_plt + 2);
3289 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3290 htab->tlsdesc_got. The 12 is the offset to the end of
3291 the instruction. */
3292 bfd_put_32 (output_bfd,
3293 (htab->sgot->output_section->vma
3294 + htab->sgot->output_offset
3295 + htab->tlsdesc_got
3296 - htab->splt->output_section->vma
3297 - htab->splt->output_offset
3298 - htab->tlsdesc_plt
3299 - 12),
3300 htab->splt->contents + htab->tlsdesc_plt + 8);
3301 }
70256ad8 3302 }
70256ad8
AJ
3303 }
3304
c434dee6 3305 if (htab->sgotplt)
70256ad8 3306 {
c434dee6 3307 /* Fill in the first three entries in the global offset table. */
eea6121a 3308 if (htab->sgotplt->size > 0)
c434dee6
AJ
3309 {
3310 /* Set the first entry in the global offset table to the address of
3311 the dynamic section. */
3312 if (sdyn == NULL)
3313 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
3314 else
3315 bfd_put_64 (output_bfd,
3316 sdyn->output_section->vma + sdyn->output_offset,
3317 htab->sgotplt->contents);
3318 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3319 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
3320 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
3321 }
70256ad8 3322
c434dee6
AJ
3323 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
3324 GOT_ENTRY_SIZE;
3325 }
70256ad8 3326
eea6121a 3327 if (htab->sgot && htab->sgot->size > 0)
8c37241b
JJ
3328 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
3329 = GOT_ENTRY_SIZE;
3330
b34976b6 3331 return TRUE;
8d88c4ca
NC
3332}
3333
4c45e5c9
JJ
3334/* Return address for Ith PLT stub in section PLT, for relocation REL
3335 or (bfd_vma) -1 if it should not be included. */
3336
3337static bfd_vma
3338elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
3339 const arelent *rel ATTRIBUTE_UNUSED)
3340{
3341 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3342}
8df9fc9d 3343
d2b2c203
DJ
3344/* Handle an x86-64 specific section when reading an object file. This
3345 is called when elfcode.h finds a section with an unknown type. */
3346
3347static bfd_boolean
6dc132d9
L
3348elf64_x86_64_section_from_shdr (bfd *abfd,
3349 Elf_Internal_Shdr *hdr,
3350 const char *name,
3351 int shindex)
d2b2c203
DJ
3352{
3353 if (hdr->sh_type != SHT_X86_64_UNWIND)
3354 return FALSE;
3355
6dc132d9 3356 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
3357 return FALSE;
3358
3359 return TRUE;
3360}
3361
3b22753a
L
3362/* Hook called by the linker routine which adds symbols from an object
3363 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3364 of .bss. */
3365
3366static bfd_boolean
3367elf64_x86_64_add_symbol_hook (bfd *abfd,
3368 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3369 Elf_Internal_Sym *sym,
3370 const char **namep ATTRIBUTE_UNUSED,
3371 flagword *flagsp ATTRIBUTE_UNUSED,
3372 asection **secp, bfd_vma *valp)
3373{
3374 asection *lcomm;
3375
3376 switch (sym->st_shndx)
3377 {
3378 case SHN_X86_64_LCOMMON:
3379 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
3380 if (lcomm == NULL)
3381 {
3382 lcomm = bfd_make_section_with_flags (abfd,
3383 "LARGE_COMMON",
3384 (SEC_ALLOC
3385 | SEC_IS_COMMON
3386 | SEC_LINKER_CREATED));
3387 if (lcomm == NULL)
3388 return FALSE;
3389 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
3390 }
3391 *secp = lcomm;
3392 *valp = sym->st_size;
3393 break;
3394 }
3395 return TRUE;
3396}
3397
3398
3399/* Given a BFD section, try to locate the corresponding ELF section
3400 index. */
3401
3402static bfd_boolean
3403elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
3404 asection *sec, int *index)
3405{
3406 if (sec == &_bfd_elf_large_com_section)
3407 {
3408 *index = SHN_X86_64_LCOMMON;
3409 return TRUE;
3410 }
3411 return FALSE;
3412}
3413
3414/* Process a symbol. */
3415
3416static void
3417elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
3418 asymbol *asym)
3419{
3420 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
3421
3422 switch (elfsym->internal_elf_sym.st_shndx)
3423 {
3424 case SHN_X86_64_LCOMMON:
3425 asym->section = &_bfd_elf_large_com_section;
3426 asym->value = elfsym->internal_elf_sym.st_size;
3427 /* Common symbol doesn't set BSF_GLOBAL. */
3428 asym->flags &= ~BSF_GLOBAL;
3429 break;
3430 }
3431}
3432
3433static bfd_boolean
3434elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
3435{
3436 return (sym->st_shndx == SHN_COMMON
3437 || sym->st_shndx == SHN_X86_64_LCOMMON);
3438}
3439
3440static unsigned int
3441elf64_x86_64_common_section_index (asection *sec)
3442{
3443 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3444 return SHN_COMMON;
3445 else
3446 return SHN_X86_64_LCOMMON;
3447}
3448
3449static asection *
3450elf64_x86_64_common_section (asection *sec)
3451{
3452 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3453 return bfd_com_section_ptr;
3454 else
3455 return &_bfd_elf_large_com_section;
3456}
3457
3458static bfd_boolean
3459elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3460 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
3461 struct elf_link_hash_entry *h,
3462 Elf_Internal_Sym *sym,
00492999 3463 asection **psec,
3b22753a
L
3464 bfd_vma *pvalue ATTRIBUTE_UNUSED,
3465 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
3466 bfd_boolean *skip ATTRIBUTE_UNUSED,
3467 bfd_boolean *override ATTRIBUTE_UNUSED,
3468 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
3469 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
3470 bfd_boolean *newdef ATTRIBUTE_UNUSED,
3471 bfd_boolean *newdyn,
3472 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
3473 bfd_boolean *newweak ATTRIBUTE_UNUSED,
3474 bfd *abfd ATTRIBUTE_UNUSED,
3475 asection **sec,
3476 bfd_boolean *olddef ATTRIBUTE_UNUSED,
3477 bfd_boolean *olddyn,
3478 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
3479 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
00492999 3480 bfd *oldbfd,
3b22753a
L
3481 asection **oldsec)
3482{
3483 /* A normal common symbol and a large common symbol result in a
00492999
L
3484 normal common symbol. We turn the large common symbol into a
3485 normal one. */
3b22753a
L
3486 if (!*olddyn
3487 && h->root.type == bfd_link_hash_common
3488 && !*newdyn
3489 && bfd_is_com_section (*sec)
00492999 3490 && *oldsec != *sec)
3b22753a 3491 {
00492999
L
3492 if (sym->st_shndx == SHN_COMMON
3493 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
3494 {
3495 h->root.u.c.p->section
3496 = bfd_make_section_old_way (oldbfd, "COMMON");
3497 h->root.u.c.p->section->flags = SEC_ALLOC;
3498 }
3499 else if (sym->st_shndx == SHN_X86_64_LCOMMON
3500 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
3501 *psec = *sec = bfd_com_section_ptr;
3b22753a
L
3502 }
3503
3504 return TRUE;
3505}
3506
3507static int
3508elf64_x86_64_additional_program_headers (bfd *abfd)
3509{
3510 asection *s;
3511 int count = 0;
3512
3513 /* Check to see if we need a large readonly segment. */
3514 s = bfd_get_section_by_name (abfd, ".lrodata");
3515 if (s && (s->flags & SEC_LOAD))
3516 count++;
3517
3518 /* Check to see if we need a large data segment. Since .lbss sections
3519 is placed right after the .bss section, there should be no need for
3520 a large data segment just because of .lbss. */
3521 s = bfd_get_section_by_name (abfd, ".ldata");
3522 if (s && (s->flags & SEC_LOAD))
3523 count++;
3524
3525 return count;
3526}
3527
3528static const struct bfd_elf_special_section
3529 elf64_x86_64_special_sections[]=
3530{
3531 { ".gnu.linkonce.lb", 16, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3532 { ".gnu.linkonce.lr", 16, -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3533 { ".gnu.linkonce.lt", 16, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
3534 { ".lbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3535 { ".ldata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3536 { ".lrodata", 8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3537 { NULL, 0, 0, 0, 0 }
3538};
3539
70256ad8
AJ
3540#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3541#define TARGET_LITTLE_NAME "elf64-x86-64"
3542#define ELF_ARCH bfd_arch_i386
3543#define ELF_MACHINE_CODE EM_X86_64
3544#define ELF_MAXPAGESIZE 0x100000
3545
3546#define elf_backend_can_gc_sections 1
51b64d56 3547#define elf_backend_can_refcount 1
70256ad8
AJ
3548#define elf_backend_want_got_plt 1
3549#define elf_backend_plt_readonly 1
3550#define elf_backend_want_plt_sym 0
3551#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 3552#define elf_backend_rela_normal 1
70256ad8
AJ
3553
3554#define elf_info_to_howto elf64_x86_64_info_to_howto
70256ad8 3555
70256ad8
AJ
3556#define bfd_elf64_bfd_link_hash_table_create \
3557 elf64_x86_64_link_hash_table_create
407443a3 3558#define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
70256ad8
AJ
3559
3560#define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3561#define elf_backend_check_relocs elf64_x86_64_check_relocs
c434dee6
AJ
3562#define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3563#define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3564#define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
70256ad8
AJ
3565#define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3566#define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3567#define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3bab7989
ML
3568#define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3569#define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
c434dee6 3570#define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
70256ad8
AJ
3571#define elf_backend_relocate_section elf64_x86_64_relocate_section
3572#define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
67a4f2b7 3573#define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4c45e5c9 3574#define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
407443a3 3575#define elf_backend_object_p elf64_x86_64_elf_object_p
bffbf940 3576#define bfd_elf64_mkobject elf64_x86_64_mkobject
8d88c4ca 3577
d2b2c203
DJ
3578#define elf_backend_section_from_shdr \
3579 elf64_x86_64_section_from_shdr
3580
3b22753a
L
3581#define elf_backend_section_from_bfd_section \
3582 elf64_x86_64_elf_section_from_bfd_section
3583#define elf_backend_add_symbol_hook \
3584 elf64_x86_64_add_symbol_hook
3585#define elf_backend_symbol_processing \
3586 elf64_x86_64_symbol_processing
3587#define elf_backend_common_section_index \
3588 elf64_x86_64_common_section_index
3589#define elf_backend_common_section \
3590 elf64_x86_64_common_section
3591#define elf_backend_common_definition \
3592 elf64_x86_64_common_definition
3593#define elf_backend_merge_symbol \
3594 elf64_x86_64_merge_symbol
3595#define elf_backend_special_sections \
3596 elf64_x86_64_special_sections
3597#define elf_backend_additional_program_headers \
3598 elf64_x86_64_additional_program_headers
3599
8d88c4ca 3600#include "elf64-target.h"
This page took 0.5197 seconds and 4 git commands to generate.