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