* gdb.fortran/array_element.f: New file.
[deliverable/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
8d88c4ca 1/* X86-64 specific support for 64-bit ELF
3eb128b2
AM
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005
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),
76 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
78 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_signed,
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),
97 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
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),
fe4770f4
AJ
106
107/* GNU extension to record C++ vtable hierarchy. */
b34976b6
AM
108 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
109 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
fe4770f4
AJ
110
111/* GNU extension to record C++ vtable member usage. */
b34976b6
AM
112 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
113 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
114 FALSE)
8d88c4ca
NC
115};
116
117/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
118struct elf_reloc_map
119{
8d88c4ca
NC
120 bfd_reloc_code_real_type bfd_reloc_val;
121 unsigned char elf_reloc_val;
122};
123
dc810e39 124static const struct elf_reloc_map x86_64_reloc_map[] =
8d88c4ca 125{
70256ad8
AJ
126 { BFD_RELOC_NONE, R_X86_64_NONE, },
127 { BFD_RELOC_64, R_X86_64_64, },
128 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
129 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
130 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
131 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
132 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
133 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
134 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
135 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
136 { BFD_RELOC_32, R_X86_64_32, },
137 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
138 { BFD_RELOC_16, R_X86_64_16, },
139 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
140 { BFD_RELOC_8, R_X86_64_8, },
141 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
bffbf940
JJ
142 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
143 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
144 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
145 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
146 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
147 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
148 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
149 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
fe4770f4
AJ
150 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
151 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
152};
153
8d88c4ca
NC
154
155/* Given a BFD reloc type, return a HOWTO structure. */
156static reloc_howto_type *
27482721
AJ
157elf64_x86_64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
158 bfd_reloc_code_real_type code)
8d88c4ca
NC
159{
160 unsigned int i;
27482721 161
8d88c4ca
NC
162 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
163 i++)
164 {
165 if (x86_64_reloc_map[i].bfd_reloc_val == code)
ffaef159 166 return &x86_64_elf_howto_table[i];
8d88c4ca
NC
167 }
168 return 0;
169}
170
8d88c4ca 171/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 172
8d88c4ca 173static void
27482721
AJ
174elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
175 Elf_Internal_Rela *dst)
8d88c4ca 176{
fe4770f4 177 unsigned r_type, i;
8d88c4ca
NC
178
179 r_type = ELF64_R_TYPE (dst->r_info);
fe4770f4
AJ
180 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
181 {
bffbf940 182 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_TPOFF32);
fe4770f4
AJ
183 i = r_type;
184 }
185 else
186 {
187 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
bffbf940 188 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_TPOFF32 - 1);
fe4770f4
AJ
189 }
190 cache_ptr->howto = &x86_64_elf_howto_table[i];
8d88c4ca
NC
191 BFD_ASSERT (r_type == cache_ptr->howto->type);
192}
70256ad8 193\f
3bab7989 194/* Support for core dump NOTE sections. */
b34976b6 195static bfd_boolean
27482721 196elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
197{
198 int offset;
eea6121a 199 size_t size;
3bab7989
ML
200
201 switch (note->descsz)
202 {
203 default:
b34976b6 204 return FALSE;
3bab7989
ML
205
206 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
207 /* pr_cursig */
cedb70c5 208 elf_tdata (abfd)->core_signal
3bab7989
ML
209 = bfd_get_16 (abfd, note->descdata + 12);
210
211 /* pr_pid */
cedb70c5 212 elf_tdata (abfd)->core_pid
3bab7989
ML
213 = bfd_get_32 (abfd, note->descdata + 32);
214
215 /* pr_reg */
216 offset = 112;
eea6121a 217 size = 216;
3bab7989
ML
218
219 break;
220 }
221
222 /* Make a ".reg/999" section. */
223 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 224 size, note->descpos + offset);
3bab7989
ML
225}
226
b34976b6 227static bfd_boolean
27482721 228elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
229{
230 switch (note->descsz)
231 {
232 default:
b34976b6 233 return FALSE;
3bab7989
ML
234
235 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
236 elf_tdata (abfd)->core_program
237 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
238 elf_tdata (abfd)->core_command
239 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
240 }
241
242 /* Note that for some reason, a spurious space is tacked
243 onto the end of the args in some (at least one anyway)
244 implementations, so strip it off if it exists. */
245
246 {
247 char *command = elf_tdata (abfd)->core_command;
248 int n = strlen (command);
249
250 if (0 < n && command[n - 1] == ' ')
251 command[n - 1] = '\0';
252 }
253
b34976b6 254 return TRUE;
3bab7989
ML
255}
256\f
407443a3 257/* Functions for the x86-64 ELF linker. */
70256ad8 258
407443a3 259/* The name of the dynamic interpreter. This is put in the .interp
70256ad8
AJ
260 section. */
261
407443a3 262#define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
70256ad8 263
d40d037c
AJ
264/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
265 copying dynamic variables from a shared lib into an app's dynbss
266 section, and instead use a dynamic relocation to point into the
267 shared lib. */
268#define ELIMINATE_COPY_RELOCS 1
269
70256ad8
AJ
270/* The size in bytes of an entry in the global offset table. */
271
272#define GOT_ENTRY_SIZE 8
8d88c4ca 273
70256ad8 274/* The size in bytes of an entry in the procedure linkage table. */
8d88c4ca 275
70256ad8
AJ
276#define PLT_ENTRY_SIZE 16
277
278/* The first entry in a procedure linkage table looks like this. See the
279 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
280
281static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
282{
653165cc
AJ
283 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
284 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
285 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
70256ad8
AJ
286};
287
288/* Subsequent entries in a procedure linkage table look like this. */
289
290static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
291{
653165cc 292 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 293 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 294 0x68, /* pushq immediate */
70256ad8
AJ
295 0, 0, 0, 0, /* replaced with index into relocation table. */
296 0xe9, /* jmp relative */
297 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
298};
299
300/* The x86-64 linker needs to keep track of the number of relocs that
985142a4 301 it decides to copy as dynamic relocs in check_relocs for each symbol.
c434dee6
AJ
302 This is so that it can later discard them if they are found to be
303 unnecessary. We store the information in a field extending the
304 regular ELF linker hash table. */
70256ad8 305
c434dee6 306struct elf64_x86_64_dyn_relocs
70256ad8
AJ
307{
308 /* Next section. */
c434dee6
AJ
309 struct elf64_x86_64_dyn_relocs *next;
310
311 /* The input section of the reloc. */
312 asection *sec;
313
314 /* Total number of relocs copied for the input section. */
70256ad8 315 bfd_size_type count;
c434dee6
AJ
316
317 /* Number of pc-relative relocs copied for the input section. */
318 bfd_size_type pc_count;
70256ad8
AJ
319};
320
321/* x86-64 ELF linker hash entry. */
322
323struct elf64_x86_64_link_hash_entry
324{
c434dee6 325 struct elf_link_hash_entry elf;
70256ad8 326
c434dee6
AJ
327 /* Track dynamic relocs copied for this symbol. */
328 struct elf64_x86_64_dyn_relocs *dyn_relocs;
bffbf940
JJ
329
330#define GOT_UNKNOWN 0
331#define GOT_NORMAL 1
332#define GOT_TLS_GD 2
333#define GOT_TLS_IE 3
334 unsigned char tls_type;
335};
336
337#define elf64_x86_64_hash_entry(ent) \
338 ((struct elf64_x86_64_link_hash_entry *)(ent))
339
340struct elf64_x86_64_obj_tdata
341{
342 struct elf_obj_tdata root;
343
344 /* tls_type for each local got entry. */
345 char *local_got_tls_type;
70256ad8
AJ
346};
347
bffbf940
JJ
348#define elf64_x86_64_tdata(abfd) \
349 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
350
351#define elf64_x86_64_local_got_tls_type(abfd) \
352 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
353
354
c434dee6 355/* x86-64 ELF linker hash table. */
8d88c4ca 356
407443a3
AJ
357struct elf64_x86_64_link_hash_table
358{
c434dee6 359 struct elf_link_hash_table elf;
70256ad8 360
c434dee6
AJ
361 /* Short-cuts to get to dynamic linker sections. */
362 asection *sgot;
363 asection *sgotplt;
364 asection *srelgot;
365 asection *splt;
366 asection *srelplt;
367 asection *sdynbss;
368 asection *srelbss;
70256ad8 369
bffbf940
JJ
370 union {
371 bfd_signed_vma refcount;
372 bfd_vma offset;
373 } tls_ld_got;
374
c434dee6
AJ
375 /* Small local sym to section mapping cache. */
376 struct sym_sec_cache sym_sec;
377};
70256ad8
AJ
378
379/* Get the x86-64 ELF linker hash table from a link_info structure. */
8d88c4ca
NC
380
381#define elf64_x86_64_hash_table(p) \
382 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
383
407443a3 384/* Create an entry in an x86-64 ELF linker hash table. */
70256ad8
AJ
385
386static struct bfd_hash_entry *
27482721
AJ
387link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
388 const char *string)
70256ad8 389{
70256ad8 390 /* Allocate the structure if it has not already been allocated by a
c434dee6
AJ
391 subclass. */
392 if (entry == NULL)
393 {
394 entry = bfd_hash_allocate (table,
395 sizeof (struct elf64_x86_64_link_hash_entry));
396 if (entry == NULL)
397 return entry;
398 }
70256ad8
AJ
399
400 /* Call the allocation method of the superclass. */
c434dee6
AJ
401 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
402 if (entry != NULL)
70256ad8 403 {
c434dee6
AJ
404 struct elf64_x86_64_link_hash_entry *eh;
405
406 eh = (struct elf64_x86_64_link_hash_entry *) entry;
407 eh->dyn_relocs = NULL;
bffbf940 408 eh->tls_type = GOT_UNKNOWN;
70256ad8
AJ
409 }
410
c434dee6 411 return entry;
70256ad8
AJ
412}
413
8d88c4ca
NC
414/* Create an X86-64 ELF linker hash table. */
415
416static struct bfd_link_hash_table *
27482721 417elf64_x86_64_link_hash_table_create (bfd *abfd)
8d88c4ca
NC
418{
419 struct elf64_x86_64_link_hash_table *ret;
dc810e39 420 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
8d88c4ca 421
e2d34d7d 422 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
c434dee6 423 if (ret == NULL)
8d88c4ca
NC
424 return NULL;
425
c434dee6 426 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
8d88c4ca 427 {
e2d34d7d 428 free (ret);
8d88c4ca
NC
429 return NULL;
430 }
431
c434dee6
AJ
432 ret->sgot = NULL;
433 ret->sgotplt = NULL;
434 ret->srelgot = NULL;
435 ret->splt = NULL;
436 ret->srelplt = NULL;
437 ret->sdynbss = NULL;
438 ret->srelbss = NULL;
439 ret->sym_sec.abfd = NULL;
bffbf940 440 ret->tls_ld_got.refcount = 0;
c434dee6
AJ
441
442 return &ret->elf.root;
443}
444
445/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
446 shortcuts to them in our hash table. */
447
b34976b6 448static bfd_boolean
27482721 449create_got_section (bfd *dynobj, struct bfd_link_info *info)
c434dee6
AJ
450{
451 struct elf64_x86_64_link_hash_table *htab;
452
453 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 454 return FALSE;
c434dee6
AJ
455
456 htab = elf64_x86_64_hash_table (info);
457 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
458 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
459 if (!htab->sgot || !htab->sgotplt)
460 abort ();
461
3496cb2a
L
462 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
463 (SEC_ALLOC | SEC_LOAD
464 | SEC_HAS_CONTENTS
465 | SEC_IN_MEMORY
466 | SEC_LINKER_CREATED
467 | SEC_READONLY));
c434dee6 468 if (htab->srelgot == NULL
c434dee6 469 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
b34976b6
AM
470 return FALSE;
471 return TRUE;
c434dee6
AJ
472}
473
474/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
475 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
476 hash table. */
477
b34976b6 478static bfd_boolean
27482721 479elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
c434dee6
AJ
480{
481 struct elf64_x86_64_link_hash_table *htab;
482
483 htab = elf64_x86_64_hash_table (info);
484 if (!htab->sgot && !create_got_section (dynobj, info))
b34976b6 485 return FALSE;
c434dee6
AJ
486
487 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 488 return FALSE;
c434dee6
AJ
489
490 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
491 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
492 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
493 if (!info->shared)
494 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
495
496 if (!htab->splt || !htab->srelplt || !htab->sdynbss
497 || (!info->shared && !htab->srelbss))
498 abort ();
499
b34976b6 500 return TRUE;
c434dee6
AJ
501}
502
503/* Copy the extra info we tack onto an elf_link_hash_entry. */
504
505static void
9c5bfbb7 506elf64_x86_64_copy_indirect_symbol (const struct elf_backend_data *bed,
27482721
AJ
507 struct elf_link_hash_entry *dir,
508 struct elf_link_hash_entry *ind)
c434dee6
AJ
509{
510 struct elf64_x86_64_link_hash_entry *edir, *eind;
511
512 edir = (struct elf64_x86_64_link_hash_entry *) dir;
513 eind = (struct elf64_x86_64_link_hash_entry *) ind;
514
515 if (eind->dyn_relocs != NULL)
516 {
517 if (edir->dyn_relocs != NULL)
518 {
519 struct elf64_x86_64_dyn_relocs **pp;
520 struct elf64_x86_64_dyn_relocs *p;
521
522 if (ind->root.type == bfd_link_hash_indirect)
523 abort ();
524
525 /* Add reloc counts against the weak sym to the strong sym
526 list. Merge any entries against the same section. */
527 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
528 {
529 struct elf64_x86_64_dyn_relocs *q;
530
531 for (q = edir->dyn_relocs; q != NULL; q = q->next)
532 if (q->sec == p->sec)
533 {
534 q->pc_count += p->pc_count;
535 q->count += p->count;
536 *pp = p->next;
537 break;
538 }
539 if (q == NULL)
540 pp = &p->next;
541 }
542 *pp = edir->dyn_relocs;
543 }
544
545 edir->dyn_relocs = eind->dyn_relocs;
546 eind->dyn_relocs = NULL;
547 }
548
bffbf940
JJ
549 if (ind->root.type == bfd_link_hash_indirect
550 && dir->got.refcount <= 0)
551 {
552 edir->tls_type = eind->tls_type;
553 eind->tls_type = GOT_UNKNOWN;
554 }
555
d40d037c
AJ
556 if (ELIMINATE_COPY_RELOCS
557 && ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
558 && dir->dynamic_adjusted)
559 {
560 /* If called to transfer flags for a weakdef during processing
561 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
562 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
563 dir->ref_dynamic |= ind->ref_dynamic;
564 dir->ref_regular |= ind->ref_regular;
565 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
566 dir->needs_plt |= ind->needs_plt;
567 dir->pointer_equality_needed |= ind->pointer_equality_needed;
568 }
d40d037c
AJ
569 else
570 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
8d88c4ca
NC
571}
572
b34976b6 573static bfd_boolean
27482721 574elf64_x86_64_mkobject (bfd *abfd)
8d88c4ca 575{
bffbf940
JJ
576 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
577 abfd->tdata.any = bfd_zalloc (abfd, amt);
578 if (abfd->tdata.any == NULL)
b34976b6
AM
579 return FALSE;
580 return TRUE;
bffbf940
JJ
581}
582
b34976b6 583static bfd_boolean
27482721 584elf64_x86_64_elf_object_p (bfd *abfd)
bffbf940 585{
8d88c4ca
NC
586 /* Set the right machine number for an x86-64 elf64 file. */
587 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
b34976b6 588 return TRUE;
8d88c4ca
NC
589}
590
bffbf940 591static int
27482721 592elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
bffbf940
JJ
593{
594 if (info->shared)
595 return r_type;
596
597 switch (r_type)
598 {
599 case R_X86_64_TLSGD:
600 case R_X86_64_GOTTPOFF:
601 if (is_local)
602 return R_X86_64_TPOFF32;
603 return R_X86_64_GOTTPOFF;
604 case R_X86_64_TLSLD:
605 return R_X86_64_TPOFF32;
606 }
607
608 return r_type;
609}
610
70256ad8 611/* Look through the relocs for a section during the first phase, and
c434dee6
AJ
612 calculate needed space in the global offset table, procedure
613 linkage table, and dynamic reloc sections. */
70256ad8 614
b34976b6 615static bfd_boolean
27482721
AJ
616elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
617 const Elf_Internal_Rela *relocs)
70256ad8 618{
c434dee6 619 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
620 Elf_Internal_Shdr *symtab_hdr;
621 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
622 const Elf_Internal_Rela *rel;
623 const Elf_Internal_Rela *rel_end;
70256ad8
AJ
624 asection *sreloc;
625
1049f94e 626 if (info->relocatable)
b34976b6 627 return TRUE;
70256ad8 628
c434dee6 629 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
630 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
631 sym_hashes = elf_sym_hashes (abfd);
70256ad8 632
c434dee6
AJ
633 sreloc = NULL;
634
70256ad8
AJ
635 rel_end = relocs + sec->reloc_count;
636 for (rel = relocs; rel < rel_end; rel++)
637 {
bffbf940 638 unsigned int r_type;
70256ad8
AJ
639 unsigned long r_symndx;
640 struct elf_link_hash_entry *h;
641
642 r_symndx = ELF64_R_SYM (rel->r_info);
bffbf940 643 r_type = ELF64_R_TYPE (rel->r_info);
c434dee6
AJ
644
645 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
646 {
d003868e
AM
647 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
648 abfd, r_symndx);
b34976b6 649 return FALSE;
c434dee6
AJ
650 }
651
70256ad8
AJ
652 if (r_symndx < symtab_hdr->sh_info)
653 h = NULL;
654 else
655 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
656
bffbf940
JJ
657 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
658 switch (r_type)
70256ad8 659 {
bffbf940
JJ
660 case R_X86_64_TLSLD:
661 htab->tls_ld_got.refcount += 1;
662 goto create_got;
663
664 case R_X86_64_TPOFF32:
665 if (info->shared)
70256ad8 666 {
bffbf940 667 (*_bfd_error_handler)
d003868e
AM
668 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
669 abfd,
6610a52d
L
670 x86_64_elf_howto_table[r_type].name,
671 (h) ? h->root.root.string : "a local symbol");
bffbf940 672 bfd_set_error (bfd_error_bad_value);
b34976b6 673 return FALSE;
70256ad8 674 }
bffbf940 675 break;
c434dee6 676
bffbf940
JJ
677 case R_X86_64_GOTTPOFF:
678 if (info->shared)
679 info->flags |= DF_STATIC_TLS;
680 /* Fall through */
70256ad8 681
bffbf940
JJ
682 case R_X86_64_GOT32:
683 case R_X86_64_GOTPCREL:
684 case R_X86_64_TLSGD:
685 /* This symbol requires a global offset table entry. */
686 {
687 int tls_type, old_tls_type;
688
689 switch (r_type)
690 {
691 default: tls_type = GOT_NORMAL; break;
692 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
693 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
694 }
695
696 if (h != NULL)
697 {
698 h->got.refcount += 1;
699 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
700 }
701 else
702 {
703 bfd_signed_vma *local_got_refcounts;
704
705 /* This is a global offset table entry for a local symbol. */
706 local_got_refcounts = elf_local_got_refcounts (abfd);
707 if (local_got_refcounts == NULL)
708 {
709 bfd_size_type size;
710
711 size = symtab_hdr->sh_info;
712 size *= sizeof (bfd_signed_vma) + sizeof (char);
713 local_got_refcounts = ((bfd_signed_vma *)
714 bfd_zalloc (abfd, size));
715 if (local_got_refcounts == NULL)
b34976b6 716 return FALSE;
bffbf940
JJ
717 elf_local_got_refcounts (abfd) = local_got_refcounts;
718 elf64_x86_64_local_got_tls_type (abfd)
719 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
720 }
721 local_got_refcounts[r_symndx] += 1;
722 old_tls_type
723 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
724 }
725
726 /* If a TLS symbol is accessed using IE at least once,
727 there is no point to use dynamic model for it. */
728 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
729 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
730 {
731 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
732 tls_type = old_tls_type;
733 else
734 {
735 (*_bfd_error_handler)
d003868e
AM
736 (_("%B: %s' accessed both as normal and thread local symbol"),
737 abfd, h ? h->root.root.string : "<local>");
b34976b6 738 return FALSE;
bffbf940
JJ
739 }
740 }
741
742 if (old_tls_type != tls_type)
743 {
744 if (h != NULL)
745 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
746 else
747 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
748 }
749 }
c434dee6
AJ
750 /* Fall through */
751
752 //case R_X86_64_GOTPCREL:
bffbf940 753 create_got:
c434dee6
AJ
754 if (htab->sgot == NULL)
755 {
756 if (htab->elf.dynobj == NULL)
757 htab->elf.dynobj = abfd;
758 if (!create_got_section (htab->elf.dynobj, info))
b34976b6 759 return FALSE;
c434dee6 760 }
70256ad8
AJ
761 break;
762
763 case R_X86_64_PLT32:
764 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
765 actually build the entry in adjust_dynamic_symbol,
766 because this might be a case of linking PIC code which is
767 never referenced by a dynamic object, in which case we
768 don't need to generate a procedure linkage table entry
769 after all. */
70256ad8
AJ
770
771 /* If this is a local symbol, we resolve it directly without
407443a3 772 creating a procedure linkage table entry. */
70256ad8
AJ
773 if (h == NULL)
774 continue;
775
f5385ebf 776 h->needs_plt = 1;
51b64d56 777 h->plt.refcount += 1;
70256ad8
AJ
778 break;
779
cc78d0af
AJ
780 case R_X86_64_8:
781 case R_X86_64_16:
70256ad8
AJ
782 case R_X86_64_32:
783 case R_X86_64_32S:
1b71fb54
AJ
784 /* Let's help debug shared library creation. These relocs
785 cannot be used in shared libs. Don't error out for
786 sections we don't care about, such as debug sections or
787 non-constant sections. */
788 if (info->shared
789 && (sec->flags & SEC_ALLOC) != 0
790 && (sec->flags & SEC_READONLY) != 0)
791 {
792 (*_bfd_error_handler)
d003868e
AM
793 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
794 abfd,
6610a52d
L
795 x86_64_elf_howto_table[r_type].name,
796 (h) ? h->root.root.string : "a local symbol");
1b71fb54 797 bfd_set_error (bfd_error_bad_value);
b34976b6 798 return FALSE;
1b71fb54
AJ
799 }
800 /* Fall through. */
801
c434dee6
AJ
802 case R_X86_64_PC8:
803 case R_X86_64_PC16:
70256ad8 804 case R_X86_64_PC32:
1b71fb54 805 case R_X86_64_64:
c434dee6
AJ
806 if (h != NULL && !info->shared)
807 {
808 /* If this reloc is in a read-only section, we might
809 need a copy reloc. We can't check reliably at this
810 stage whether the section is read-only, as input
811 sections have not yet been mapped to output sections.
812 Tentatively set the flag for now, and correct in
813 adjust_dynamic_symbol. */
f5385ebf 814 h->non_got_ref = 1;
c434dee6
AJ
815
816 /* We may need a .plt entry if the function this reloc
817 refers to is in a shared lib. */
818 h->plt.refcount += 1;
47a9f7b3 819 if (r_type != R_X86_64_PC32)
f5385ebf 820 h->pointer_equality_needed = 1;
c434dee6 821 }
70256ad8
AJ
822
823 /* If we are creating a shared library, and this is a reloc
824 against a global symbol, or a non PC relative reloc
825 against a local symbol, then we need to copy the reloc
826 into the shared library. However, if we are linking with
827 -Bsymbolic, we do not need to copy a reloc against a
828 global symbol which is defined in an object we are
407443a3 829 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
830 this point we have not seen all the input files, so it is
831 possible that DEF_REGULAR is not set now but will be set
c434dee6
AJ
832 later (it is never cleared). In case of a weak definition,
833 DEF_REGULAR may be cleared later by a strong definition in
834 a shared library. We account for that possibility below by
835 storing information in the relocs_copied field of the hash
836 table entry. A similar situation occurs when creating
837 shared libraries and symbol visibility changes render the
838 symbol local.
839
840 If on the other hand, we are creating an executable, we
841 may need to keep relocations for symbols satisfied by a
842 dynamic library if we manage to avoid copy relocs for the
843 symbol. */
844 if ((info->shared
845 && (sec->flags & SEC_ALLOC) != 0
bffbf940
JJ
846 && (((r_type != R_X86_64_PC8)
847 && (r_type != R_X86_64_PC16)
848 && (r_type != R_X86_64_PC32))
c434dee6
AJ
849 || (h != NULL
850 && (! info->symbolic
851 || h->root.type == bfd_link_hash_defweak
f5385ebf 852 || !h->def_regular))))
d40d037c
AJ
853 || (ELIMINATE_COPY_RELOCS
854 && !info->shared
c434dee6
AJ
855 && (sec->flags & SEC_ALLOC) != 0
856 && h != NULL
857 && (h->root.type == bfd_link_hash_defweak
f5385ebf 858 || !h->def_regular)))
70256ad8 859 {
c434dee6
AJ
860 struct elf64_x86_64_dyn_relocs *p;
861 struct elf64_x86_64_dyn_relocs **head;
862
863 /* We must copy these reloc types into the output file.
864 Create a reloc section in dynobj and make room for
865 this reloc. */
70256ad8
AJ
866 if (sreloc == NULL)
867 {
868 const char *name;
c434dee6 869 bfd *dynobj;
70256ad8
AJ
870
871 name = (bfd_elf_string_from_elf_section
872 (abfd,
873 elf_elfheader (abfd)->e_shstrndx,
874 elf_section_data (sec)->rel_hdr.sh_name));
875 if (name == NULL)
b34976b6 876 return FALSE;
70256ad8 877
c434dee6
AJ
878 if (strncmp (name, ".rela", 5) != 0
879 || strcmp (bfd_get_section_name (abfd, sec),
880 name + 5) != 0)
881 {
882 (*_bfd_error_handler)
d003868e
AM
883 (_("%B: bad relocation section name `%s\'"),
884 abfd, name);
c434dee6
AJ
885 }
886
887 if (htab->elf.dynobj == NULL)
888 htab->elf.dynobj = abfd;
889
890 dynobj = htab->elf.dynobj;
70256ad8
AJ
891
892 sreloc = bfd_get_section_by_name (dynobj, name);
893 if (sreloc == NULL)
894 {
895 flagword flags;
896
70256ad8
AJ
897 flags = (SEC_HAS_CONTENTS | SEC_READONLY
898 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
899 if ((sec->flags & SEC_ALLOC) != 0)
900 flags |= SEC_ALLOC | SEC_LOAD;
3496cb2a
L
901 sreloc = bfd_make_section_with_flags (dynobj,
902 name,
903 flags);
70256ad8 904 if (sreloc == NULL
cc78d0af 905 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
b34976b6 906 return FALSE;
70256ad8 907 }
c434dee6 908 elf_section_data (sec)->sreloc = sreloc;
70256ad8
AJ
909 }
910
c434dee6
AJ
911 /* If this is a global symbol, we count the number of
912 relocations we need for this symbol. */
913 if (h != NULL)
70256ad8 914 {
c434dee6
AJ
915 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
916 }
917 else
918 {
919 /* Track dynamic relocs needed for local syms too.
920 We really need local syms available to do this
921 easily. Oh well. */
922
923 asection *s;
924 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
925 sec, r_symndx);
926 if (s == NULL)
b34976b6 927 return FALSE;
70256ad8 928
c434dee6
AJ
929 head = ((struct elf64_x86_64_dyn_relocs **)
930 &elf_section_data (s)->local_dynrel);
931 }
70256ad8 932
c434dee6
AJ
933 p = *head;
934 if (p == NULL || p->sec != sec)
935 {
936 bfd_size_type amt = sizeof *p;
937 p = ((struct elf64_x86_64_dyn_relocs *)
938 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 939 if (p == NULL)
b34976b6 940 return FALSE;
c434dee6
AJ
941 p->next = *head;
942 *head = p;
943 p->sec = sec;
944 p->count = 0;
945 p->pc_count = 0;
70256ad8 946 }
c434dee6
AJ
947
948 p->count += 1;
bffbf940
JJ
949 if (r_type == R_X86_64_PC8
950 || r_type == R_X86_64_PC16
951 || r_type == R_X86_64_PC32)
c434dee6 952 p->pc_count += 1;
70256ad8
AJ
953 }
954 break;
fe4770f4
AJ
955
956 /* This relocation describes the C++ object vtable hierarchy.
957 Reconstruct it for later use during GC. */
958 case R_X86_64_GNU_VTINHERIT:
c152c796 959 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 960 return FALSE;
fe4770f4
AJ
961 break;
962
963 /* This relocation describes which C++ vtable entries are actually
964 used. Record for later use during GC. */
965 case R_X86_64_GNU_VTENTRY:
c152c796 966 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 967 return FALSE;
fe4770f4 968 break;
c434dee6
AJ
969
970 default:
971 break;
70256ad8
AJ
972 }
973 }
974
b34976b6 975 return TRUE;
70256ad8
AJ
976}
977
978/* Return the section that should be marked against GC for a given
407443a3 979 relocation. */
70256ad8
AJ
980
981static asection *
27482721
AJ
982elf64_x86_64_gc_mark_hook (asection *sec,
983 struct bfd_link_info *info ATTRIBUTE_UNUSED,
984 Elf_Internal_Rela *rel,
985 struct elf_link_hash_entry *h,
986 Elf_Internal_Sym *sym)
70256ad8
AJ
987{
988 if (h != NULL)
989 {
fe4770f4 990 switch (ELF64_R_TYPE (rel->r_info))
70256ad8 991 {
fe4770f4
AJ
992 case R_X86_64_GNU_VTINHERIT:
993 case R_X86_64_GNU_VTENTRY:
994 break;
70256ad8
AJ
995
996 default:
fe4770f4
AJ
997 switch (h->root.type)
998 {
999 case bfd_link_hash_defined:
1000 case bfd_link_hash_defweak:
1001 return h->root.u.def.section;
1002
1003 case bfd_link_hash_common:
1004 return h->root.u.c.p->section;
1005
1006 default:
1007 break;
1008 }
70256ad8
AJ
1009 }
1010 }
1011 else
1e2f5b6e 1012 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
70256ad8
AJ
1013
1014 return NULL;
1015}
1016
407443a3 1017/* Update the got entry reference counts for the section being removed. */
70256ad8 1018
b34976b6 1019static bfd_boolean
27482721
AJ
1020elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1021 asection *sec, const Elf_Internal_Rela *relocs)
70256ad8
AJ
1022{
1023 Elf_Internal_Shdr *symtab_hdr;
1024 struct elf_link_hash_entry **sym_hashes;
1025 bfd_signed_vma *local_got_refcounts;
1026 const Elf_Internal_Rela *rel, *relend;
c434dee6
AJ
1027
1028 elf_section_data (sec)->local_dynrel = NULL;
70256ad8
AJ
1029
1030 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1031 sym_hashes = elf_sym_hashes (abfd);
1032 local_got_refcounts = elf_local_got_refcounts (abfd);
1033
70256ad8
AJ
1034 relend = relocs + sec->reloc_count;
1035 for (rel = relocs; rel < relend; rel++)
26e41594
AM
1036 {
1037 unsigned long r_symndx;
1038 unsigned int r_type;
1039 struct elf_link_hash_entry *h = NULL;
70256ad8 1040
26e41594
AM
1041 r_symndx = ELF64_R_SYM (rel->r_info);
1042 if (r_symndx >= symtab_hdr->sh_info)
1043 {
1044 struct elf64_x86_64_link_hash_entry *eh;
1045 struct elf64_x86_64_dyn_relocs **pp;
1046 struct elf64_x86_64_dyn_relocs *p;
c434dee6 1047
26e41594 1048 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
1049 while (h->root.type == bfd_link_hash_indirect
1050 || h->root.type == bfd_link_hash_warning)
1051 h = (struct elf_link_hash_entry *) h->root.u.i.link;
26e41594 1052 eh = (struct elf64_x86_64_link_hash_entry *) h;
c434dee6 1053
26e41594
AM
1054 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1055 if (p->sec == sec)
1056 {
1057 /* Everything must go for SEC. */
1058 *pp = p->next;
1059 break;
1060 }
1061 }
c434dee6 1062
26e41594
AM
1063 r_type = ELF64_R_TYPE (rel->r_info);
1064 r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
1065 switch (r_type)
1066 {
1067 case R_X86_64_TLSLD:
1068 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1069 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1070 break;
c434dee6 1071
26e41594
AM
1072 case R_X86_64_TLSGD:
1073 case R_X86_64_GOTTPOFF:
1074 case R_X86_64_GOT32:
1075 case R_X86_64_GOTPCREL:
1076 if (h != NULL)
1077 {
1078 if (h->got.refcount > 0)
1079 h->got.refcount -= 1;
1080 }
1081 else if (local_got_refcounts != NULL)
1082 {
1083 if (local_got_refcounts[r_symndx] > 0)
1084 local_got_refcounts[r_symndx] -= 1;
1085 }
1086 break;
c434dee6 1087
26e41594
AM
1088 case R_X86_64_8:
1089 case R_X86_64_16:
1090 case R_X86_64_32:
1091 case R_X86_64_64:
1092 case R_X86_64_32S:
1093 case R_X86_64_PC8:
1094 case R_X86_64_PC16:
1095 case R_X86_64_PC32:
1096 if (info->shared)
1097 break;
1098 /* Fall thru */
c434dee6 1099
26e41594
AM
1100 case R_X86_64_PLT32:
1101 if (h != NULL)
1102 {
1103 if (h->plt.refcount > 0)
1104 h->plt.refcount -= 1;
1105 }
1106 break;
70256ad8 1107
26e41594
AM
1108 default:
1109 break;
1110 }
1111 }
70256ad8 1112
b34976b6 1113 return TRUE;
70256ad8
AJ
1114}
1115
1116/* Adjust a symbol defined by a dynamic object and referenced by a
1117 regular object. The current definition is in some section of the
1118 dynamic object, but we're not including those sections. We have to
1119 change the definition to something the rest of the link can
407443a3 1120 understand. */
70256ad8 1121
b34976b6 1122static bfd_boolean
27482721
AJ
1123elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1124 struct elf_link_hash_entry *h)
70256ad8 1125{
c434dee6 1126 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
1127 asection *s;
1128 unsigned int power_of_two;
1129
70256ad8
AJ
1130 /* If this is a function, put it in the procedure linkage table. We
1131 will fill in the contents of the procedure linkage table later,
1132 when we know the address of the .got section. */
1133 if (h->type == STT_FUNC
f5385ebf 1134 || h->needs_plt)
70256ad8 1135 {
c434dee6 1136 if (h->plt.refcount <= 0
27482721
AJ
1137 || SYMBOL_CALLS_LOCAL (info, h)
1138 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1139 && h->root.type == bfd_link_hash_undefweak))
70256ad8 1140 {
70256ad8
AJ
1141 /* This case can occur if we saw a PLT32 reloc in an input
1142 file, but the symbol was never referred to by a dynamic
1143 object, or if all references were garbage collected. In
1144 such a case, we don't actually need to build a procedure
1145 linkage table, and we can just do a PC32 reloc instead. */
70256ad8 1146 h->plt.offset = (bfd_vma) -1;
f5385ebf 1147 h->needs_plt = 0;
70256ad8
AJ
1148 }
1149
b34976b6 1150 return TRUE;
70256ad8 1151 }
bbd7ec4a 1152 else
c434dee6
AJ
1153 /* It's possible that we incorrectly decided a .plt reloc was
1154 needed for an R_X86_64_PC32 reloc to a non-function sym in
1155 check_relocs. We can't decide accurately between function and
1156 non-function syms in check-relocs; Objects loaded later in
1157 the link may change h->type. So fix it now. */
bbd7ec4a 1158 h->plt.offset = (bfd_vma) -1;
70256ad8
AJ
1159
1160 /* If this is a weak symbol, and there is a real definition, the
1161 processor independent code will have arranged for us to see the
407443a3 1162 real definition first, and we can just use the same value. */
f6e332e6 1163 if (h->u.weakdef != NULL)
70256ad8 1164 {
f6e332e6
AM
1165 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1166 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1167 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1168 h->root.u.def.value = h->u.weakdef->root.u.def.value;
d40d037c 1169 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
f6e332e6 1170 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 1171 return TRUE;
70256ad8
AJ
1172 }
1173
1174 /* This is a reference to a symbol defined by a dynamic object which
407443a3 1175 is not a function. */
70256ad8
AJ
1176
1177 /* If we are creating a shared library, we must presume that the
1178 only references to the symbol are via the global offset table.
1179 For such cases we need not do anything here; the relocations will
407443a3 1180 be handled correctly by relocate_section. */
70256ad8 1181 if (info->shared)
b34976b6 1182 return TRUE;
70256ad8
AJ
1183
1184 /* If there are no references to this symbol that do not use the
1185 GOT, we don't need to generate a copy reloc. */
f5385ebf 1186 if (!h->non_got_ref)
b34976b6 1187 return TRUE;
70256ad8 1188
c434dee6
AJ
1189 /* If -z nocopyreloc was given, we won't generate them either. */
1190 if (info->nocopyreloc)
1191 {
f5385ebf 1192 h->non_got_ref = 0;
b34976b6 1193 return TRUE;
c434dee6
AJ
1194 }
1195
d40d037c 1196 if (ELIMINATE_COPY_RELOCS)
c434dee6 1197 {
d40d037c
AJ
1198 struct elf64_x86_64_link_hash_entry * eh;
1199 struct elf64_x86_64_dyn_relocs *p;
c434dee6 1200
d40d037c
AJ
1201 eh = (struct elf64_x86_64_link_hash_entry *) h;
1202 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1203 {
1204 s = p->sec->output_section;
1205 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1206 break;
1207 }
1208
1209 /* If we didn't find any dynamic relocs in read-only sections, then
1210 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1211 if (p == NULL)
1212 {
f5385ebf 1213 h->non_got_ref = 0;
d40d037c
AJ
1214 return TRUE;
1215 }
c434dee6
AJ
1216 }
1217
70256ad8 1218 /* We must allocate the symbol in our .dynbss section, which will
407443a3 1219 become part of the .bss section of the executable. There will be
70256ad8
AJ
1220 an entry for this symbol in the .dynsym section. The dynamic
1221 object will contain position independent code, so all references
1222 from the dynamic object to this symbol will go through the global
1223 offset table. The dynamic linker will use the .dynsym entry to
1224 determine the address it must put in the global offset table, so
1225 both the dynamic object and the regular object will refer to the
1226 same memory location for the variable. */
1227
c434dee6 1228 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
1229
1230 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1231 to copy the initial value out of the dynamic object and into the
cedb70c5 1232 runtime process image. */
70256ad8
AJ
1233 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1234 {
eea6121a 1235 htab->srelbss->size += sizeof (Elf64_External_Rela);
f5385ebf 1236 h->needs_copy = 1;
70256ad8
AJ
1237 }
1238
1239 /* We need to figure out the alignment required for this symbol. I
407443a3 1240 have no idea how ELF linkers handle this. 16-bytes is the size
70256ad8
AJ
1241 of the largest type that requires hard alignment -- long double. */
1242 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1243 this construct. */
1244 power_of_two = bfd_log2 (h->size);
1245 if (power_of_two > 4)
1246 power_of_two = 4;
1247
1248 /* Apply the required alignment. */
c434dee6 1249 s = htab->sdynbss;
eea6121a 1250 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
c434dee6 1251 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
70256ad8 1252 {
c434dee6 1253 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
b34976b6 1254 return FALSE;
70256ad8
AJ
1255 }
1256
1257 /* Define the symbol as being at this point in the section. */
1258 h->root.u.def.section = s;
eea6121a 1259 h->root.u.def.value = s->size;
70256ad8
AJ
1260
1261 /* Increment the section size to make room for the symbol. */
eea6121a 1262 s->size += h->size;
70256ad8 1263
b34976b6 1264 return TRUE;
70256ad8
AJ
1265}
1266
c434dee6
AJ
1267/* Allocate space in .plt, .got and associated reloc sections for
1268 dynamic relocs. */
1269
b34976b6 1270static bfd_boolean
27482721 1271allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
1272{
1273 struct bfd_link_info *info;
1274 struct elf64_x86_64_link_hash_table *htab;
1275 struct elf64_x86_64_link_hash_entry *eh;
1276 struct elf64_x86_64_dyn_relocs *p;
1277
e92d460e 1278 if (h->root.type == bfd_link_hash_indirect)
b34976b6 1279 return TRUE;
c434dee6 1280
e92d460e
AM
1281 if (h->root.type == bfd_link_hash_warning)
1282 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1283
c434dee6
AJ
1284 info = (struct bfd_link_info *) inf;
1285 htab = elf64_x86_64_hash_table (info);
1286
1287 if (htab->elf.dynamic_sections_created
27482721 1288 && h->plt.refcount > 0)
c434dee6
AJ
1289 {
1290 /* Make sure this symbol is output as a dynamic symbol.
1291 Undefined weak syms won't yet be marked as dynamic. */
1292 if (h->dynindx == -1
f5385ebf 1293 && !h->forced_local)
c434dee6 1294 {
c152c796 1295 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1296 return FALSE;
c434dee6
AJ
1297 }
1298
27482721
AJ
1299 if (info->shared
1300 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
c434dee6
AJ
1301 {
1302 asection *s = htab->splt;
1303
1304 /* If this is the first .plt entry, make room for the special
1305 first entry. */
eea6121a
AM
1306 if (s->size == 0)
1307 s->size += PLT_ENTRY_SIZE;
c434dee6 1308
eea6121a 1309 h->plt.offset = s->size;
c434dee6
AJ
1310
1311 /* If this symbol is not defined in a regular file, and we are
1312 not generating a shared library, then set the symbol to this
1313 location in the .plt. This is required to make function
1314 pointers compare as equal between the normal executable and
1315 the shared library. */
1316 if (! info->shared
f5385ebf 1317 && !h->def_regular)
c434dee6
AJ
1318 {
1319 h->root.u.def.section = s;
1320 h->root.u.def.value = h->plt.offset;
1321 }
1322
1323 /* Make room for this entry. */
eea6121a 1324 s->size += PLT_ENTRY_SIZE;
c434dee6
AJ
1325
1326 /* We also need to make an entry in the .got.plt section, which
1327 will be placed in the .got section by the linker script. */
eea6121a 1328 htab->sgotplt->size += GOT_ENTRY_SIZE;
c434dee6
AJ
1329
1330 /* We also need to make an entry in the .rela.plt section. */
eea6121a 1331 htab->srelplt->size += sizeof (Elf64_External_Rela);
c434dee6
AJ
1332 }
1333 else
1334 {
1335 h->plt.offset = (bfd_vma) -1;
f5385ebf 1336 h->needs_plt = 0;
c434dee6
AJ
1337 }
1338 }
1339 else
1340 {
1341 h->plt.offset = (bfd_vma) -1;
f5385ebf 1342 h->needs_plt = 0;
c434dee6
AJ
1343 }
1344
bffbf940
JJ
1345 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1346 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1347 if (h->got.refcount > 0
1348 && !info->shared
1349 && h->dynindx == -1
1350 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1351 h->got.offset = (bfd_vma) -1;
1352 else if (h->got.refcount > 0)
c434dee6
AJ
1353 {
1354 asection *s;
b34976b6 1355 bfd_boolean dyn;
bffbf940 1356 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
c434dee6
AJ
1357
1358 /* Make sure this symbol is output as a dynamic symbol.
1359 Undefined weak syms won't yet be marked as dynamic. */
1360 if (h->dynindx == -1
f5385ebf 1361 && !h->forced_local)
c434dee6 1362 {
c152c796 1363 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1364 return FALSE;
c434dee6
AJ
1365 }
1366
1367 s = htab->sgot;
eea6121a
AM
1368 h->got.offset = s->size;
1369 s->size += GOT_ENTRY_SIZE;
bffbf940
JJ
1370 /* R_X86_64_TLSGD needs 2 consecutive GOT slots. */
1371 if (tls_type == GOT_TLS_GD)
eea6121a 1372 s->size += GOT_ENTRY_SIZE;
c434dee6 1373 dyn = htab->elf.dynamic_sections_created;
bffbf940
JJ
1374 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1375 and two if global.
1376 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1377 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1378 || tls_type == GOT_TLS_IE)
eea6121a 1379 htab->srelgot->size += sizeof (Elf64_External_Rela);
bffbf940 1380 else if (tls_type == GOT_TLS_GD)
eea6121a 1381 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
4bc6e03a
AJ
1382 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1383 || h->root.type != bfd_link_hash_undefweak)
27482721
AJ
1384 && (info->shared
1385 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
eea6121a 1386 htab->srelgot->size += sizeof (Elf64_External_Rela);
c434dee6
AJ
1387 }
1388 else
1389 h->got.offset = (bfd_vma) -1;
1390
1391 eh = (struct elf64_x86_64_link_hash_entry *) h;
1392 if (eh->dyn_relocs == NULL)
b34976b6 1393 return TRUE;
c434dee6
AJ
1394
1395 /* In the shared -Bsymbolic case, discard space allocated for
1396 dynamic pc-relative relocs against symbols which turn out to be
1397 defined in regular objects. For the normal shared case, discard
1398 space for pc-relative relocs that have become local due to symbol
1399 visibility changes. */
1400
1401 if (info->shared)
1402 {
27482721
AJ
1403 /* Relocs that use pc_count are those that appear on a call
1404 insn, or certain REL relocs that can generated via assembly.
1405 We want calls to protected symbols to resolve directly to the
1406 function rather than going via the plt. If people want
1407 function pointer comparisons to work as expected then they
1408 should avoid writing weird assembly. */
1409 if (SYMBOL_CALLS_LOCAL (info, h))
c434dee6
AJ
1410 {
1411 struct elf64_x86_64_dyn_relocs **pp;
1412
1413 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1414 {
1415 p->count -= p->pc_count;
1416 p->pc_count = 0;
1417 if (p->count == 0)
1418 *pp = p->next;
1419 else
1420 pp = &p->next;
1421 }
1422 }
4e795f50
AM
1423
1424 /* Also discard relocs on undefined weak syms with non-default
1425 visibility. */
1426 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1427 && h->root.type == bfd_link_hash_undefweak)
1428 eh->dyn_relocs = NULL;
c434dee6 1429 }
d40d037c 1430 else if (ELIMINATE_COPY_RELOCS)
c434dee6
AJ
1431 {
1432 /* For the non-shared case, discard space for relocs against
1433 symbols which turn out to need copy relocs or are not
1434 dynamic. */
1435
f5385ebf
AM
1436 if (!h->non_got_ref
1437 && ((h->def_dynamic
1438 && !h->def_regular)
c434dee6
AJ
1439 || (htab->elf.dynamic_sections_created
1440 && (h->root.type == bfd_link_hash_undefweak
1441 || h->root.type == bfd_link_hash_undefined))))
1442 {
1443 /* Make sure this symbol is output as a dynamic symbol.
1444 Undefined weak syms won't yet be marked as dynamic. */
1445 if (h->dynindx == -1
f5385ebf 1446 && !h->forced_local)
c434dee6 1447 {
c152c796 1448 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1449 return FALSE;
c434dee6
AJ
1450 }
1451
1452 /* If that succeeded, we know we'll be keeping all the
1453 relocs. */
1454 if (h->dynindx != -1)
1455 goto keep;
1456 }
1457
1458 eh->dyn_relocs = NULL;
1459
1460 keep: ;
1461 }
1462
1463 /* Finally, allocate space. */
1464 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1465 {
1466 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 1467 sreloc->size += p->count * sizeof (Elf64_External_Rela);
c434dee6
AJ
1468 }
1469
b34976b6 1470 return TRUE;
c434dee6
AJ
1471}
1472
1473/* Find any dynamic relocs that apply to read-only sections. */
1474
b34976b6 1475static bfd_boolean
27482721 1476readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
1477{
1478 struct elf64_x86_64_link_hash_entry *eh;
1479 struct elf64_x86_64_dyn_relocs *p;
1480
e92d460e
AM
1481 if (h->root.type == bfd_link_hash_warning)
1482 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1483
c434dee6
AJ
1484 eh = (struct elf64_x86_64_link_hash_entry *) h;
1485 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1486 {
1487 asection *s = p->sec->output_section;
1488
1489 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1490 {
1491 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1492
1493 info->flags |= DF_TEXTREL;
1494
1495 /* Not an error, just cut short the traversal. */
b34976b6 1496 return FALSE;
c434dee6
AJ
1497 }
1498 }
b34976b6 1499 return TRUE;
c434dee6
AJ
1500}
1501
70256ad8
AJ
1502/* Set the sizes of the dynamic sections. */
1503
b34976b6 1504static bfd_boolean
27482721
AJ
1505elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1506 struct bfd_link_info *info)
70256ad8 1507{
c434dee6 1508 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
1509 bfd *dynobj;
1510 asection *s;
b34976b6 1511 bfd_boolean relocs;
c434dee6 1512 bfd *ibfd;
70256ad8 1513
c434dee6
AJ
1514 htab = elf64_x86_64_hash_table (info);
1515 dynobj = htab->elf.dynobj;
1516 if (dynobj == NULL)
1517 abort ();
70256ad8 1518
c434dee6 1519 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1520 {
1521 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 1522 if (info->executable)
70256ad8
AJ
1523 {
1524 s = bfd_get_section_by_name (dynobj, ".interp");
c434dee6
AJ
1525 if (s == NULL)
1526 abort ();
eea6121a 1527 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
70256ad8
AJ
1528 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1529 }
1530 }
70256ad8 1531
c434dee6
AJ
1532 /* Set up .got offsets for local syms, and space for local dynamic
1533 relocs. */
1534 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
70256ad8 1535 {
c434dee6
AJ
1536 bfd_signed_vma *local_got;
1537 bfd_signed_vma *end_local_got;
bffbf940 1538 char *local_tls_type;
c434dee6
AJ
1539 bfd_size_type locsymcount;
1540 Elf_Internal_Shdr *symtab_hdr;
1541 asection *srel;
70256ad8 1542
c434dee6 1543 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
70256ad8
AJ
1544 continue;
1545
c434dee6 1546 for (s = ibfd->sections; s != NULL; s = s->next)
70256ad8 1547 {
c434dee6
AJ
1548 struct elf64_x86_64_dyn_relocs *p;
1549
1550 for (p = *((struct elf64_x86_64_dyn_relocs **)
1551 &elf_section_data (s)->local_dynrel);
1552 p != NULL;
1553 p = p->next)
70256ad8 1554 {
c434dee6
AJ
1555 if (!bfd_is_abs_section (p->sec)
1556 && bfd_is_abs_section (p->sec->output_section))
1557 {
1558 /* Input section has been discarded, either because
1559 it is a copy of a linkonce section or due to
1560 linker script /DISCARD/, so we'll be discarding
1561 the relocs too. */
1562 }
1563 else if (p->count != 0)
1564 {
1565 srel = elf_section_data (p->sec)->sreloc;
eea6121a 1566 srel->size += p->count * sizeof (Elf64_External_Rela);
c434dee6
AJ
1567 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1568 info->flags |= DF_TEXTREL;
1569
1570 }
70256ad8
AJ
1571 }
1572 }
c434dee6
AJ
1573
1574 local_got = elf_local_got_refcounts (ibfd);
1575 if (!local_got)
1576 continue;
1577
1578 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1579 locsymcount = symtab_hdr->sh_info;
1580 end_local_got = local_got + locsymcount;
bffbf940 1581 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
c434dee6
AJ
1582 s = htab->sgot;
1583 srel = htab->srelgot;
bffbf940 1584 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
70256ad8 1585 {
c434dee6 1586 if (*local_got > 0)
70256ad8 1587 {
eea6121a
AM
1588 *local_got = s->size;
1589 s->size += GOT_ENTRY_SIZE;
bffbf940 1590 if (*local_tls_type == GOT_TLS_GD)
eea6121a 1591 s->size += GOT_ENTRY_SIZE;
bffbf940
JJ
1592 if (info->shared
1593 || *local_tls_type == GOT_TLS_GD
1594 || *local_tls_type == GOT_TLS_IE)
eea6121a 1595 srel->size += sizeof (Elf64_External_Rela);
70256ad8
AJ
1596 }
1597 else
c434dee6
AJ
1598 *local_got = (bfd_vma) -1;
1599 }
1600 }
70256ad8 1601
bffbf940
JJ
1602 if (htab->tls_ld_got.refcount > 0)
1603 {
1604 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1605 relocs. */
eea6121a
AM
1606 htab->tls_ld_got.offset = htab->sgot->size;
1607 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
1608 htab->srelgot->size += sizeof (Elf64_External_Rela);
bffbf940
JJ
1609 }
1610 else
1611 htab->tls_ld_got.offset = -1;
1612
c434dee6
AJ
1613 /* Allocate global sym .plt and .got entries, and space for global
1614 sym dynamic relocs. */
1615 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1616
1617 /* We now have determined the sizes of the various dynamic sections.
1618 Allocate memory for them. */
b34976b6 1619 relocs = FALSE;
c434dee6
AJ
1620 for (s = dynobj->sections; s != NULL; s = s->next)
1621 {
1622 if ((s->flags & SEC_LINKER_CREATED) == 0)
1623 continue;
1624
1625 if (s == htab->splt
1626 || s == htab->sgot
75ff4589
L
1627 || s == htab->sgotplt
1628 || s == htab->sdynbss)
c434dee6
AJ
1629 {
1630 /* Strip this section if we don't need it; see the
1631 comment below. */
1632 }
1633 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1634 {
eea6121a 1635 if (s->size != 0 && s != htab->srelplt)
b34976b6 1636 relocs = TRUE;
c434dee6
AJ
1637
1638 /* We use the reloc_count field as a counter if we need
1639 to copy relocs into the output file. */
1640 s->reloc_count = 0;
70256ad8 1641 }
c434dee6 1642 else
70256ad8
AJ
1643 {
1644 /* It's not one of our sections, so don't allocate space. */
1645 continue;
1646 }
1647
eea6121a 1648 if (s->size == 0)
70256ad8 1649 {
c434dee6
AJ
1650 /* If we don't need this section, strip it from the
1651 output file. This is mostly to handle .rela.bss and
1652 .rela.plt. We must create both sections in
1653 create_dynamic_sections, because they must be created
1654 before the linker maps input sections to output
1655 sections. The linker does that before
1656 adjust_dynamic_symbol is called, and it is that
1657 function which decides whether anything needs to go
1658 into these sections. */
1659
8423293d 1660 s->flags |= SEC_EXCLUDE;
70256ad8
AJ
1661 continue;
1662 }
1663
1664 /* Allocate memory for the section contents. We use bfd_zalloc
1665 here in case unused entries are not reclaimed before the
1666 section's contents are written out. This should not happen,
1667 but this way if it does, we get a R_X86_64_NONE reloc instead
1668 of garbage. */
eea6121a 1669 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c434dee6 1670 if (s->contents == NULL)
b34976b6 1671 return FALSE;
70256ad8
AJ
1672 }
1673
c434dee6 1674 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1675 {
1676 /* Add some entries to the .dynamic section. We fill in the
1677 values later, in elf64_x86_64_finish_dynamic_sections, but we
1678 must add the entries now so that we get the correct size for
407443a3 1679 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8 1680 dynamic linker and used by the debugger. */
dc810e39 1681#define add_dynamic_entry(TAG, VAL) \
5a580b3a 1682 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 1683
36af4a4e 1684 if (info->executable)
70256ad8 1685 {
dc810e39 1686 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 1687 return FALSE;
70256ad8
AJ
1688 }
1689
eea6121a 1690 if (htab->splt->size != 0)
70256ad8 1691 {
dc810e39
AM
1692 if (!add_dynamic_entry (DT_PLTGOT, 0)
1693 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1694 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1695 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 1696 return FALSE;
70256ad8
AJ
1697 }
1698
1699 if (relocs)
1700 {
dc810e39
AM
1701 if (!add_dynamic_entry (DT_RELA, 0)
1702 || !add_dynamic_entry (DT_RELASZ, 0)
1703 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 1704 return FALSE;
70256ad8 1705
c434dee6
AJ
1706 /* If any dynamic relocs apply to a read-only section,
1707 then we need a DT_TEXTREL entry. */
1708 if ((info->flags & DF_TEXTREL) == 0)
1709 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1710 (PTR) info);
1711
1712 if ((info->flags & DF_TEXTREL) != 0)
1713 {
1714 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 1715 return FALSE;
c434dee6 1716 }
70256ad8
AJ
1717 }
1718 }
dc810e39 1719#undef add_dynamic_entry
70256ad8 1720
b34976b6 1721 return TRUE;
70256ad8
AJ
1722}
1723
bffbf940
JJ
1724/* Return the base VMA address which should be subtracted from real addresses
1725 when resolving @dtpoff relocation.
1726 This is PT_TLS segment p_vaddr. */
1727
1728static bfd_vma
27482721 1729dtpoff_base (struct bfd_link_info *info)
bffbf940 1730{
e1918d23
AM
1731 /* If tls_sec is NULL, we should have signalled an error already. */
1732 if (elf_hash_table (info)->tls_sec == NULL)
bffbf940 1733 return 0;
e1918d23 1734 return elf_hash_table (info)->tls_sec->vma;
bffbf940
JJ
1735}
1736
1737/* Return the relocation value for @tpoff relocation
1738 if STT_TLS virtual address is ADDRESS. */
1739
1740static bfd_vma
27482721 1741tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 1742{
e1918d23 1743 struct elf_link_hash_table *htab = elf_hash_table (info);
bffbf940
JJ
1744
1745 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 1746 if (htab->tls_sec == NULL)
bffbf940 1747 return 0;
e1918d23 1748 return address - htab->tls_size - htab->tls_sec->vma;
bffbf940
JJ
1749}
1750
90f487df
L
1751/* Is the instruction before OFFSET in CONTENTS a 32bit relative
1752 branch? */
1753
1754static bfd_boolean
1755is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
1756{
1757 /* Opcode Instruction
1758 0xe8 call
1759 0xe9 jump
1760 0x0f 0x8x conditional jump */
1761 return ((offset > 0
1762 && (contents [offset - 1] == 0xe8
1763 || contents [offset - 1] == 0xe9))
1764 || (offset > 1
1765 && contents [offset - 2] == 0x0f
1766 && (contents [offset - 1] & 0xf0) == 0x80));
1767}
1768
8d88c4ca
NC
1769/* Relocate an x86_64 ELF section. */
1770
b34976b6 1771static bfd_boolean
27482721
AJ
1772elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
1773 bfd *input_bfd, asection *input_section,
1774 bfd_byte *contents, Elf_Internal_Rela *relocs,
1775 Elf_Internal_Sym *local_syms,
1776 asection **local_sections)
8d88c4ca 1777{
c434dee6 1778 struct elf64_x86_64_link_hash_table *htab;
8d88c4ca
NC
1779 Elf_Internal_Shdr *symtab_hdr;
1780 struct elf_link_hash_entry **sym_hashes;
1781 bfd_vma *local_got_offsets;
c434dee6 1782 Elf_Internal_Rela *rel;
8d88c4ca
NC
1783 Elf_Internal_Rela *relend;
1784
1049f94e 1785 if (info->relocatable)
b34976b6 1786 return TRUE;
b491616a 1787
c434dee6 1788 htab = elf64_x86_64_hash_table (info);
8d88c4ca
NC
1789 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1790 sym_hashes = elf_sym_hashes (input_bfd);
1791 local_got_offsets = elf_local_got_offsets (input_bfd);
1792
c434dee6 1793 rel = relocs;
8d88c4ca 1794 relend = relocs + input_section->reloc_count;
c434dee6 1795 for (; rel < relend; rel++)
8d88c4ca 1796 {
bffbf940 1797 unsigned int r_type;
8d88c4ca
NC
1798 reloc_howto_type *howto;
1799 unsigned long r_symndx;
1800 struct elf_link_hash_entry *h;
1801 Elf_Internal_Sym *sym;
1802 asection *sec;
c434dee6 1803 bfd_vma off;
8d88c4ca 1804 bfd_vma relocation;
b34976b6 1805 bfd_boolean unresolved_reloc;
8d88c4ca 1806 bfd_reloc_status_type r;
bffbf940 1807 int tls_type;
8d88c4ca 1808
c434dee6 1809 r_type = ELF64_R_TYPE (rel->r_info);
fe4770f4
AJ
1810 if (r_type == (int) R_X86_64_GNU_VTINHERIT
1811 || r_type == (int) R_X86_64_GNU_VTENTRY)
1812 continue;
8d88c4ca 1813
bffbf940 1814 if (r_type >= R_X86_64_max)
8da6118f
KH
1815 {
1816 bfd_set_error (bfd_error_bad_value);
b34976b6 1817 return FALSE;
8da6118f 1818 }
8d88c4ca 1819
b491616a 1820 howto = x86_64_elf_howto_table + r_type;
c434dee6 1821 r_symndx = ELF64_R_SYM (rel->r_info);
8d88c4ca
NC
1822 h = NULL;
1823 sym = NULL;
1824 sec = NULL;
b34976b6 1825 unresolved_reloc = FALSE;
8d88c4ca 1826 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
1827 {
1828 sym = local_syms + r_symndx;
1829 sec = local_sections[r_symndx];
c434dee6 1830
8517fae7 1831 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8da6118f 1832 }
8d88c4ca 1833 else
8da6118f 1834 {
560e09e9 1835 bfd_boolean warned;
c434dee6 1836
b2a8e766
AM
1837 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1838 r_symndx, symtab_hdr, sym_hashes,
1839 h, sec, relocation,
1840 unresolved_reloc, warned);
8da6118f 1841 }
70256ad8
AJ
1842 /* When generating a shared object, the relocations handled here are
1843 copied into the output file to be resolved at run time. */
1844 switch (r_type)
1845 {
1846 case R_X86_64_GOT32:
1847 /* Relocation is to the entry for this symbol in the global
1848 offset table. */
70256ad8
AJ
1849 case R_X86_64_GOTPCREL:
1850 /* Use global offset table as symbol value. */
c434dee6
AJ
1851 if (htab->sgot == NULL)
1852 abort ();
053579d7 1853
51e0a107 1854 if (h != NULL)
70256ad8 1855 {
b34976b6 1856 bfd_boolean dyn;
c434dee6
AJ
1857
1858 off = h->got.offset;
1859 dyn = htab->elf.dynamic_sections_created;
51e0a107 1860
27482721 1861 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
51e0a107 1862 || (info->shared
27482721 1863 && SYMBOL_REFERENCES_LOCAL (info, h))
4bc6e03a
AJ
1864 || (ELF_ST_VISIBILITY (h->other)
1865 && h->root.type == bfd_link_hash_undefweak))
51e0a107
JH
1866 {
1867 /* This is actually a static link, or it is a -Bsymbolic
1868 link and the symbol is defined locally, or the symbol
407443a3 1869 was forced to be local because of a version file. We
51e0a107
JH
1870 must initialize this entry in the global offset table.
1871 Since the offset must always be a multiple of 8, we
1872 use the least significant bit to record whether we
1873 have initialized it already.
1874
1875 When doing a dynamic link, we create a .rela.got
407443a3
AJ
1876 relocation entry to initialize the value. This is
1877 done in the finish_dynamic_symbol routine. */
51e0a107
JH
1878 if ((off & 1) != 0)
1879 off &= ~1;
1880 else
1881 {
1882 bfd_put_64 (output_bfd, relocation,
c434dee6 1883 htab->sgot->contents + off);
51e0a107
JH
1884 h->got.offset |= 1;
1885 }
1886 }
053579d7 1887 else
b34976b6 1888 unresolved_reloc = FALSE;
70256ad8 1889 }
51e0a107
JH
1890 else
1891 {
c434dee6
AJ
1892 if (local_got_offsets == NULL)
1893 abort ();
51e0a107
JH
1894
1895 off = local_got_offsets[r_symndx];
1896
1897 /* The offset must always be a multiple of 8. We use
407443a3
AJ
1898 the least significant bit to record whether we have
1899 already generated the necessary reloc. */
51e0a107
JH
1900 if ((off & 1) != 0)
1901 off &= ~1;
1902 else
1903 {
c434dee6
AJ
1904 bfd_put_64 (output_bfd, relocation,
1905 htab->sgot->contents + off);
51e0a107
JH
1906
1907 if (info->shared)
1908 {
947216bf 1909 asection *s;
51e0a107 1910 Elf_Internal_Rela outrel;
947216bf 1911 bfd_byte *loc;
70256ad8 1912
51e0a107
JH
1913 /* We need to generate a R_X86_64_RELATIVE reloc
1914 for the dynamic linker. */
947216bf
AM
1915 s = htab->srelgot;
1916 if (s == NULL)
c434dee6 1917 abort ();
51e0a107 1918
c434dee6
AJ
1919 outrel.r_offset = (htab->sgot->output_section->vma
1920 + htab->sgot->output_offset
51e0a107
JH
1921 + off);
1922 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1923 outrel.r_addend = relocation;
947216bf
AM
1924 loc = s->contents;
1925 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 1926 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
51e0a107
JH
1927 }
1928
1929 local_got_offsets[r_symndx] |= 1;
1930 }
51e0a107 1931 }
6a2bda3f 1932
c434dee6
AJ
1933 if (off >= (bfd_vma) -2)
1934 abort ();
1935
8c37241b
JJ
1936 relocation = htab->sgot->output_section->vma
1937 + htab->sgot->output_offset + off;
1938 if (r_type != R_X86_64_GOTPCREL)
1939 relocation -= htab->sgotplt->output_section->vma
1940 - htab->sgotplt->output_offset;
c434dee6 1941
70256ad8
AJ
1942 break;
1943
1944 case R_X86_64_PLT32:
1945 /* Relocation is to the entry for this symbol in the
1946 procedure linkage table. */
1947
1948 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 1949 without using the procedure linkage table. */
70256ad8
AJ
1950 if (h == NULL)
1951 break;
1952
c434dee6
AJ
1953 if (h->plt.offset == (bfd_vma) -1
1954 || htab->splt == NULL)
70256ad8
AJ
1955 {
1956 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
1957 happens when statically linking PIC code, or when
1958 using -Bsymbolic. */
70256ad8
AJ
1959 break;
1960 }
1961
c434dee6
AJ
1962 relocation = (htab->splt->output_section->vma
1963 + htab->splt->output_offset
70256ad8 1964 + h->plt.offset);
b34976b6 1965 unresolved_reloc = FALSE;
70256ad8
AJ
1966 break;
1967
fd8ab9e5
AJ
1968 case R_X86_64_PC8:
1969 case R_X86_64_PC16:
1970 case R_X86_64_PC32:
6610a52d
L
1971 if (info->shared
1972 && !SYMBOL_REFERENCES_LOCAL (info, h)
ba3bee0b 1973 && (input_section->flags & SEC_ALLOC) != 0
90f487df
L
1974 && (input_section->flags & SEC_READONLY) != 0
1975 && (!h->def_regular
1976 || r_type != R_X86_64_PC32
1977 || h->type != STT_FUNC
1978 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
1979 || !is_32bit_relative_branch (contents,
1980 rel->r_offset)))
6610a52d 1981 {
90f487df
L
1982 if (h->def_regular
1983 && r_type == R_X86_64_PC32
1984 && h->type == STT_FUNC
1985 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1986 (*_bfd_error_handler)
1987 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
1988 input_bfd, h->root.root.string);
1989 else
1990 (*_bfd_error_handler)
1991 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1992 input_bfd, x86_64_elf_howto_table[r_type].name,
1993 h->root.root.string);
6610a52d
L
1994 bfd_set_error (bfd_error_bad_value);
1995 return FALSE;
1996 }
1997 /* Fall through. */
1998
70256ad8
AJ
1999 case R_X86_64_8:
2000 case R_X86_64_16:
2001 case R_X86_64_32:
6b3db546 2002 case R_X86_64_64:
80643fbc 2003 /* FIXME: The ABI says the linker should make sure the value is
407443a3 2004 the same when it's zeroextended to 64 bit. */
c434dee6
AJ
2005
2006 /* r_symndx will be zero only for relocs against symbols
2007 from removed linkonce sections, or sections discarded by
2008 a linker script. */
2009 if (r_symndx == 0
2010 || (input_section->flags & SEC_ALLOC) == 0)
2011 break;
2012
2013 if ((info->shared
4bc6e03a
AJ
2014 && (h == NULL
2015 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2016 || h->root.type != bfd_link_hash_undefweak)
c434dee6
AJ
2017 && ((r_type != R_X86_64_PC8
2018 && r_type != R_X86_64_PC16
2019 && r_type != R_X86_64_PC32)
f6c52c13 2020 || !SYMBOL_CALLS_LOCAL (info, h)))
d40d037c
AJ
2021 || (ELIMINATE_COPY_RELOCS
2022 && !info->shared
c434dee6
AJ
2023 && h != NULL
2024 && h->dynindx != -1
f5385ebf
AM
2025 && !h->non_got_ref
2026 && ((h->def_dynamic
2027 && !h->def_regular)
c434dee6
AJ
2028 || h->root.type == bfd_link_hash_undefweak
2029 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
2030 {
2031 Elf_Internal_Rela outrel;
947216bf 2032 bfd_byte *loc;
b34976b6 2033 bfd_boolean skip, relocate;
c434dee6 2034 asection *sreloc;
70256ad8
AJ
2035
2036 /* When generating a shared object, these relocations
2037 are copied into the output file to be resolved at run
407443a3 2038 time. */
b34976b6
AM
2039 skip = FALSE;
2040 relocate = FALSE;
70256ad8 2041
c629eae0
JJ
2042 outrel.r_offset =
2043 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 2044 rel->r_offset);
c629eae0 2045 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 2046 skip = TRUE;
0fb19cbc 2047 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 2048 skip = TRUE, relocate = TRUE;
70256ad8
AJ
2049
2050 outrel.r_offset += (input_section->output_section->vma
2051 + input_section->output_offset);
2052
2053 if (skip)
0bb2d96a 2054 memset (&outrel, 0, sizeof outrel);
c434dee6 2055
fd8ab9e5
AJ
2056 /* h->dynindx may be -1 if this symbol was marked to
2057 become local. */
2058 else if (h != NULL
c434dee6
AJ
2059 && h->dynindx != -1
2060 && (r_type == R_X86_64_PC8
2061 || r_type == R_X86_64_PC16
2062 || r_type == R_X86_64_PC32
2063 || !info->shared
2064 || !info->symbolic
f5385ebf 2065 || !h->def_regular))
70256ad8 2066 {
70256ad8 2067 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
c434dee6 2068 outrel.r_addend = rel->r_addend;
70256ad8
AJ
2069 }
2070 else
2071 {
c434dee6 2072 /* This symbol is local, or marked to become local. */
607c0e09
AS
2073 if (r_type == R_X86_64_64)
2074 {
b34976b6 2075 relocate = TRUE;
607c0e09
AS
2076 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2077 outrel.r_addend = relocation + rel->r_addend;
2078 }
2079 else
2080 {
2081 long sindx;
2082
8517fae7 2083 if (bfd_is_abs_section (sec))
607c0e09
AS
2084 sindx = 0;
2085 else if (sec == NULL || sec->owner == NULL)
2086 {
2087 bfd_set_error (bfd_error_bad_value);
b34976b6 2088 return FALSE;
607c0e09
AS
2089 }
2090 else
2091 {
2092 asection *osec;
2093
2094 osec = sec->output_section;
2095 sindx = elf_section_data (osec)->dynindx;
2096 BFD_ASSERT (sindx > 0);
2097 }
2098
2099 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2100 outrel.r_addend = relocation + rel->r_addend;
2101 }
70256ad8
AJ
2102 }
2103
c434dee6
AJ
2104 sreloc = elf_section_data (input_section)->sreloc;
2105 if (sreloc == NULL)
2106 abort ();
2107
947216bf
AM
2108 loc = sreloc->contents;
2109 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2110 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
70256ad8
AJ
2111
2112 /* If this reloc is against an external symbol, we do
2113 not want to fiddle with the addend. Otherwise, we
2114 need to include the symbol value so that it becomes
2115 an addend for the dynamic reloc. */
2116 if (! relocate)
2117 continue;
2118 }
2119
2120 break;
2121
bffbf940
JJ
2122 case R_X86_64_TLSGD:
2123 case R_X86_64_GOTTPOFF:
2124 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2125 tls_type = GOT_UNKNOWN;
2126 if (h == NULL && local_got_offsets)
2127 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2128 else if (h != NULL)
2129 {
2130 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2131 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2132 r_type = R_X86_64_TPOFF32;
2133 }
2134 if (r_type == R_X86_64_TLSGD)
2135 {
2136 if (tls_type == GOT_TLS_IE)
2137 r_type = R_X86_64_GOTTPOFF;
2138 }
2139
2140 if (r_type == R_X86_64_TPOFF32)
2141 {
2142 BFD_ASSERT (! unresolved_reloc);
2143 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2144 {
2145 unsigned int i;
abcf1d52
JJ
2146 static unsigned char tlsgd[8]
2147 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
bffbf940
JJ
2148
2149 /* GD->LE transition.
abcf1d52
JJ
2150 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2151 .word 0x6666; rex64; call __tls_get_addr@plt
bffbf940
JJ
2152 Change it into:
2153 movq %fs:0, %rax
2154 leaq foo@tpoff(%rax), %rax */
abcf1d52
JJ
2155 BFD_ASSERT (rel->r_offset >= 4);
2156 for (i = 0; i < 4; i++)
bffbf940 2157 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52 2158 contents + rel->r_offset - 4 + i)
bffbf940 2159 == tlsgd[i]);
eea6121a 2160 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
abcf1d52
JJ
2161 for (i = 0; i < 4; i++)
2162 BFD_ASSERT (bfd_get_8 (input_bfd,
2163 contents + rel->r_offset + 4 + i)
2164 == tlsgd[i+4]);
bffbf940
JJ
2165 BFD_ASSERT (rel + 1 < relend);
2166 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
abcf1d52 2167 memcpy (contents + rel->r_offset - 4,
bffbf940
JJ
2168 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2169 16);
2170 bfd_put_32 (output_bfd, tpoff (info, relocation),
abcf1d52 2171 contents + rel->r_offset + 8);
bffbf940
JJ
2172 /* Skip R_X86_64_PLT32. */
2173 rel++;
2174 continue;
2175 }
2176 else
2177 {
2178 unsigned int val, type, reg;
2179
2180 /* IE->LE transition:
2181 Originally it can be one of:
2182 movq foo@gottpoff(%rip), %reg
2183 addq foo@gottpoff(%rip), %reg
2184 We change it into:
2185 movq $foo, %reg
2186 leaq foo(%reg), %reg
2187 addq $foo, %reg. */
2188 BFD_ASSERT (rel->r_offset >= 3);
2189 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2190 BFD_ASSERT (val == 0x48 || val == 0x4c);
2191 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2192 BFD_ASSERT (type == 0x8b || type == 0x03);
2193 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2194 BFD_ASSERT ((reg & 0xc7) == 5);
2195 reg >>= 3;
eea6121a 2196 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
bffbf940
JJ
2197 if (type == 0x8b)
2198 {
2199 /* movq */
2200 if (val == 0x4c)
2201 bfd_put_8 (output_bfd, 0x49,
2202 contents + rel->r_offset - 3);
2203 bfd_put_8 (output_bfd, 0xc7,
2204 contents + rel->r_offset - 2);
2205 bfd_put_8 (output_bfd, 0xc0 | reg,
2206 contents + rel->r_offset - 1);
2207 }
2208 else if (reg == 4)
2209 {
2210 /* addq -> addq - addressing with %rsp/%r12 is
2211 special */
2212 if (val == 0x4c)
2213 bfd_put_8 (output_bfd, 0x49,
2214 contents + rel->r_offset - 3);
2215 bfd_put_8 (output_bfd, 0x81,
2216 contents + rel->r_offset - 2);
2217 bfd_put_8 (output_bfd, 0xc0 | reg,
2218 contents + rel->r_offset - 1);
2219 }
2220 else
2221 {
2222 /* addq -> leaq */
2223 if (val == 0x4c)
2224 bfd_put_8 (output_bfd, 0x4d,
2225 contents + rel->r_offset - 3);
2226 bfd_put_8 (output_bfd, 0x8d,
2227 contents + rel->r_offset - 2);
2228 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2229 contents + rel->r_offset - 1);
2230 }
2231 bfd_put_32 (output_bfd, tpoff (info, relocation),
2232 contents + rel->r_offset);
2233 continue;
2234 }
2235 }
2236
2237 if (htab->sgot == NULL)
2238 abort ();
2239
2240 if (h != NULL)
2241 off = h->got.offset;
2242 else
2243 {
2244 if (local_got_offsets == NULL)
2245 abort ();
2246
2247 off = local_got_offsets[r_symndx];
2248 }
2249
2250 if ((off & 1) != 0)
2251 off &= ~1;
26e41594 2252 else
bffbf940
JJ
2253 {
2254 Elf_Internal_Rela outrel;
947216bf 2255 bfd_byte *loc;
bffbf940
JJ
2256 int dr_type, indx;
2257
2258 if (htab->srelgot == NULL)
2259 abort ();
2260
2261 outrel.r_offset = (htab->sgot->output_section->vma
2262 + htab->sgot->output_offset + off);
2263
2264 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2265 if (r_type == R_X86_64_TLSGD)
2266 dr_type = R_X86_64_DTPMOD64;
2267 else
2268 dr_type = R_X86_64_TPOFF64;
2269
2270 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2271 outrel.r_addend = 0;
2272 if (dr_type == R_X86_64_TPOFF64 && indx == 0)
2273 outrel.r_addend = relocation - dtpoff_base (info);
2274 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2275
947216bf
AM
2276 loc = htab->srelgot->contents;
2277 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
bffbf940
JJ
2278 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2279
2280 if (r_type == R_X86_64_TLSGD)
2281 {
2282 if (indx == 0)
2283 {
d40d037c 2284 BFD_ASSERT (! unresolved_reloc);
bffbf940
JJ
2285 bfd_put_64 (output_bfd,
2286 relocation - dtpoff_base (info),
2287 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2288 }
2289 else
2290 {
2291 bfd_put_64 (output_bfd, 0,
2292 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2293 outrel.r_info = ELF64_R_INFO (indx,
2294 R_X86_64_DTPOFF64);
2295 outrel.r_offset += GOT_ENTRY_SIZE;
2296 htab->srelgot->reloc_count++;
947216bf
AM
2297 loc += sizeof (Elf64_External_Rela);
2298 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
bffbf940
JJ
2299 }
2300 }
2301
2302 if (h != NULL)
2303 h->got.offset |= 1;
2304 else
2305 local_got_offsets[r_symndx] |= 1;
2306 }
2307
2308 if (off >= (bfd_vma) -2)
2309 abort ();
2310 if (r_type == ELF64_R_TYPE (rel->r_info))
2311 {
2312 relocation = htab->sgot->output_section->vma
2313 + htab->sgot->output_offset + off;
b34976b6 2314 unresolved_reloc = FALSE;
bffbf940
JJ
2315 }
2316 else
2317 {
2318 unsigned int i;
abcf1d52
JJ
2319 static unsigned char tlsgd[8]
2320 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
bffbf940
JJ
2321
2322 /* GD->IE transition.
abcf1d52
JJ
2323 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2324 .word 0x6666; rex64; call __tls_get_addr@plt
bffbf940
JJ
2325 Change it into:
2326 movq %fs:0, %rax
2327 addq foo@gottpoff(%rip), %rax */
abcf1d52
JJ
2328 BFD_ASSERT (rel->r_offset >= 4);
2329 for (i = 0; i < 4; i++)
26e41594 2330 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52 2331 contents + rel->r_offset - 4 + i)
bffbf940 2332 == tlsgd[i]);
eea6121a 2333 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
abcf1d52 2334 for (i = 0; i < 4; i++)
26e41594 2335 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52
JJ
2336 contents + rel->r_offset + 4 + i)
2337 == tlsgd[i+4]);
bffbf940
JJ
2338 BFD_ASSERT (rel + 1 < relend);
2339 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
abcf1d52 2340 memcpy (contents + rel->r_offset - 4,
bffbf940
JJ
2341 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2342 16);
2343
2344 relocation = (htab->sgot->output_section->vma
2345 + htab->sgot->output_offset + off
2346 - rel->r_offset
2347 - input_section->output_section->vma
2348 - input_section->output_offset
abcf1d52 2349 - 12);
bffbf940 2350 bfd_put_32 (output_bfd, relocation,
abcf1d52 2351 contents + rel->r_offset + 8);
bffbf940
JJ
2352 /* Skip R_X86_64_PLT32. */
2353 rel++;
2354 continue;
2355 }
2356 break;
2357
2358 case R_X86_64_TLSLD:
2359 if (! info->shared)
2360 {
2361 /* LD->LE transition:
2362 Ensure it is:
2363 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2364 We change it into:
2365 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2366 BFD_ASSERT (rel->r_offset >= 3);
2367 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2368 == 0x48);
2369 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2370 == 0x8d);
2371 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2372 == 0x3d);
eea6121a 2373 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
bffbf940
JJ
2374 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2375 == 0xe8);
2376 BFD_ASSERT (rel + 1 < relend);
2377 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2378 memcpy (contents + rel->r_offset - 3,
2379 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2380 /* Skip R_X86_64_PLT32. */
2381 rel++;
2382 continue;
2383 }
2384
2385 if (htab->sgot == NULL)
2386 abort ();
2387
2388 off = htab->tls_ld_got.offset;
2389 if (off & 1)
2390 off &= ~1;
2391 else
2392 {
2393 Elf_Internal_Rela outrel;
947216bf 2394 bfd_byte *loc;
bffbf940
JJ
2395
2396 if (htab->srelgot == NULL)
2397 abort ();
2398
2399 outrel.r_offset = (htab->sgot->output_section->vma
2400 + htab->sgot->output_offset + off);
2401
2402 bfd_put_64 (output_bfd, 0,
2403 htab->sgot->contents + off);
2404 bfd_put_64 (output_bfd, 0,
2405 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2406 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2407 outrel.r_addend = 0;
947216bf
AM
2408 loc = htab->srelgot->contents;
2409 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
bffbf940
JJ
2410 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2411 htab->tls_ld_got.offset |= 1;
2412 }
2413 relocation = htab->sgot->output_section->vma
2414 + htab->sgot->output_offset + off;
b34976b6 2415 unresolved_reloc = FALSE;
bffbf940
JJ
2416 break;
2417
2418 case R_X86_64_DTPOFF32:
a45bb67d 2419 if (info->shared || (input_section->flags & SEC_CODE) == 0)
bffbf940
JJ
2420 relocation -= dtpoff_base (info);
2421 else
2422 relocation = tpoff (info, relocation);
2423 break;
2424
2425 case R_X86_64_TPOFF32:
2426 BFD_ASSERT (! info->shared);
2427 relocation = tpoff (info, relocation);
2428 break;
2429
70256ad8
AJ
2430 default:
2431 break;
2432 }
8d88c4ca 2433
239e1f3a
AM
2434 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2435 because such sections are not SEC_ALLOC and thus ld.so will
2436 not process them. */
c434dee6 2437 if (unresolved_reloc
239e1f3a 2438 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 2439 && h->def_dynamic))
c434dee6 2440 (*_bfd_error_handler)
d003868e
AM
2441 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2442 input_bfd,
2443 input_section,
c434dee6
AJ
2444 (long) rel->r_offset,
2445 h->root.root.string);
2446
8d88c4ca 2447 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
2448 contents, rel->r_offset,
2449 relocation, rel->r_addend);
8d88c4ca
NC
2450
2451 if (r != bfd_reloc_ok)
8da6118f 2452 {
c434dee6
AJ
2453 const char *name;
2454
2455 if (h != NULL)
2456 name = h->root.root.string;
2457 else
8da6118f 2458 {
c434dee6
AJ
2459 name = bfd_elf_string_from_elf_section (input_bfd,
2460 symtab_hdr->sh_link,
2461 sym->st_name);
2462 if (name == NULL)
b34976b6 2463 return FALSE;
c434dee6
AJ
2464 if (*name == '\0')
2465 name = bfd_section_name (input_bfd, sec);
2466 }
2467
2468 if (r == bfd_reloc_overflow)
2469 {
3ffa5234
AM
2470 if (h != NULL
2471 && h->root.type == bfd_link_hash_undefweak
2472 && howto->pc_relative)
2473 /* Ignore reloc overflow on branches to undefweak syms. */
2474 continue;
c434dee6
AJ
2475
2476 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
2477 (info, (h ? &h->root : NULL), name, howto->name,
2478 (bfd_vma) 0, input_bfd, input_section,
2479 rel->r_offset)))
b34976b6 2480 return FALSE;
c434dee6
AJ
2481 }
2482 else
2483 {
2484 (*_bfd_error_handler)
d003868e
AM
2485 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2486 input_bfd, input_section,
c434dee6 2487 (long) rel->r_offset, name, (int) r);
b34976b6 2488 return FALSE;
8da6118f
KH
2489 }
2490 }
8d88c4ca 2491 }
70256ad8 2492
b34976b6 2493 return TRUE;
70256ad8
AJ
2494}
2495
2496/* Finish up dynamic symbol handling. We set the contents of various
2497 dynamic sections here. */
2498
b34976b6 2499static bfd_boolean
27482721
AJ
2500elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
2501 struct bfd_link_info *info,
2502 struct elf_link_hash_entry *h,
2503 Elf_Internal_Sym *sym)
70256ad8 2504{
c434dee6 2505 struct elf64_x86_64_link_hash_table *htab;
70256ad8 2506
c434dee6 2507 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
2508
2509 if (h->plt.offset != (bfd_vma) -1)
2510 {
70256ad8
AJ
2511 bfd_vma plt_index;
2512 bfd_vma got_offset;
2513 Elf_Internal_Rela rela;
947216bf 2514 bfd_byte *loc;
70256ad8
AJ
2515
2516 /* This symbol has an entry in the procedure linkage table. Set
407443a3 2517 it up. */
c434dee6
AJ
2518 if (h->dynindx == -1
2519 || htab->splt == NULL
2520 || htab->sgotplt == NULL
2521 || htab->srelplt == NULL)
2522 abort ();
70256ad8
AJ
2523
2524 /* Get the index in the procedure linkage table which
2525 corresponds to this symbol. This is the index of this symbol
2526 in all the symbols for which we are making plt entries. The
2527 first entry in the procedure linkage table is reserved. */
2528 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2529
2530 /* Get the offset into the .got table of the entry that
407443a3 2531 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
fe4770f4 2532 bytes. The first three are reserved for the dynamic linker. */
70256ad8
AJ
2533 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2534
2535 /* Fill in the entry in the procedure linkage table. */
c434dee6 2536 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
70256ad8
AJ
2537 PLT_ENTRY_SIZE);
2538
2539 /* Insert the relocation positions of the plt section. The magic
2540 numbers at the end of the statements are the positions of the
2541 relocations in the plt section. */
653165cc
AJ
2542 /* Put offset for jmp *name@GOTPCREL(%rip), since the
2543 instruction uses 6 bytes, subtract this value. */
2544 bfd_put_32 (output_bfd,
c434dee6
AJ
2545 (htab->sgotplt->output_section->vma
2546 + htab->sgotplt->output_offset
653165cc 2547 + got_offset
c434dee6
AJ
2548 - htab->splt->output_section->vma
2549 - htab->splt->output_offset
653165cc
AJ
2550 - h->plt.offset
2551 - 6),
c434dee6 2552 htab->splt->contents + h->plt.offset + 2);
653165cc
AJ
2553 /* Put relocation index. */
2554 bfd_put_32 (output_bfd, plt_index,
c434dee6 2555 htab->splt->contents + h->plt.offset + 7);
653165cc
AJ
2556 /* Put offset for jmp .PLT0. */
2557 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
c434dee6 2558 htab->splt->contents + h->plt.offset + 12);
70256ad8 2559
653165cc
AJ
2560 /* Fill in the entry in the global offset table, initially this
2561 points to the pushq instruction in the PLT which is at offset 6. */
c434dee6
AJ
2562 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
2563 + htab->splt->output_offset
70256ad8 2564 + h->plt.offset + 6),
c434dee6 2565 htab->sgotplt->contents + got_offset);
70256ad8
AJ
2566
2567 /* Fill in the entry in the .rela.plt section. */
c434dee6
AJ
2568 rela.r_offset = (htab->sgotplt->output_section->vma
2569 + htab->sgotplt->output_offset
70256ad8
AJ
2570 + got_offset);
2571 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
2572 rela.r_addend = 0;
947216bf 2573 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
c434dee6 2574 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8 2575
f5385ebf 2576 if (!h->def_regular)
70256ad8
AJ
2577 {
2578 /* Mark the symbol as undefined, rather than as defined in
47a9f7b3
JJ
2579 the .plt section. Leave the value if there were any
2580 relocations where pointer equality matters (this is a clue
c434dee6
AJ
2581 for the dynamic linker, to make function pointer
2582 comparisons work between an application and shared
47a9f7b3
JJ
2583 library), otherwise set it to zero. If a function is only
2584 called from a binary, there is no need to slow down
2585 shared libraries because of that. */
70256ad8 2586 sym->st_shndx = SHN_UNDEF;
f5385ebf 2587 if (!h->pointer_equality_needed)
47a9f7b3 2588 sym->st_value = 0;
70256ad8
AJ
2589 }
2590 }
2591
bffbf940
JJ
2592 if (h->got.offset != (bfd_vma) -1
2593 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD
2594 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
053579d7 2595 {
053579d7 2596 Elf_Internal_Rela rela;
947216bf 2597 bfd_byte *loc;
053579d7
AJ
2598
2599 /* This symbol has an entry in the global offset table. Set it
bffbf940 2600 up. */
c434dee6
AJ
2601 if (htab->sgot == NULL || htab->srelgot == NULL)
2602 abort ();
053579d7 2603
c434dee6
AJ
2604 rela.r_offset = (htab->sgot->output_section->vma
2605 + htab->sgot->output_offset
dc810e39 2606 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
2607
2608 /* If this is a static link, or it is a -Bsymbolic link and the
2609 symbol is defined locally or was forced to be local because
2610 of a version file, we just want to emit a RELATIVE reloc.
2611 The entry in the global offset table will already have been
2612 initialized in the relocate_section function. */
c434dee6 2613 if (info->shared
27482721 2614 && SYMBOL_REFERENCES_LOCAL (info, h))
053579d7 2615 {
cc78d0af 2616 BFD_ASSERT((h->got.offset & 1) != 0);
053579d7
AJ
2617 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2618 rela.r_addend = (h->root.u.def.value
2619 + h->root.u.def.section->output_section->vma
2620 + h->root.u.def.section->output_offset);
2621 }
2622 else
2623 {
2624 BFD_ASSERT((h->got.offset & 1) == 0);
c434dee6
AJ
2625 bfd_put_64 (output_bfd, (bfd_vma) 0,
2626 htab->sgot->contents + h->got.offset);
053579d7
AJ
2627 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
2628 rela.r_addend = 0;
2629 }
2630
947216bf
AM
2631 loc = htab->srelgot->contents;
2632 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2633 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
053579d7
AJ
2634 }
2635
f5385ebf 2636 if (h->needs_copy)
70256ad8 2637 {
70256ad8 2638 Elf_Internal_Rela rela;
947216bf 2639 bfd_byte *loc;
70256ad8
AJ
2640
2641 /* This symbol needs a copy reloc. Set it up. */
2642
c434dee6
AJ
2643 if (h->dynindx == -1
2644 || (h->root.type != bfd_link_hash_defined
2645 && h->root.type != bfd_link_hash_defweak)
2646 || htab->srelbss == NULL)
2647 abort ();
70256ad8
AJ
2648
2649 rela.r_offset = (h->root.u.def.value
2650 + h->root.u.def.section->output_section->vma
2651 + h->root.u.def.section->output_offset);
2652 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
2653 rela.r_addend = 0;
947216bf
AM
2654 loc = htab->srelbss->contents;
2655 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2656 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8
AJ
2657 }
2658
2659 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2660 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2661 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2662 sym->st_shndx = SHN_ABS;
2663
b34976b6 2664 return TRUE;
70256ad8
AJ
2665}
2666
c434dee6
AJ
2667/* Used to decide how to sort relocs in an optimal manner for the
2668 dynamic linker, before writing them out. */
2669
2670static enum elf_reloc_type_class
27482721 2671elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
c434dee6
AJ
2672{
2673 switch ((int) ELF64_R_TYPE (rela->r_info))
2674 {
2675 case R_X86_64_RELATIVE:
2676 return reloc_class_relative;
2677 case R_X86_64_JUMP_SLOT:
2678 return reloc_class_plt;
2679 case R_X86_64_COPY:
2680 return reloc_class_copy;
2681 default:
2682 return reloc_class_normal;
2683 }
2684}
2685
70256ad8
AJ
2686/* Finish up the dynamic sections. */
2687
b34976b6 2688static bfd_boolean
27482721 2689elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
70256ad8 2690{
c434dee6 2691 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
2692 bfd *dynobj;
2693 asection *sdyn;
70256ad8 2694
c434dee6
AJ
2695 htab = elf64_x86_64_hash_table (info);
2696 dynobj = htab->elf.dynobj;
70256ad8
AJ
2697 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2698
c434dee6 2699 if (htab->elf.dynamic_sections_created)
70256ad8 2700 {
70256ad8
AJ
2701 Elf64_External_Dyn *dyncon, *dynconend;
2702
c434dee6
AJ
2703 if (sdyn == NULL || htab->sgot == NULL)
2704 abort ();
70256ad8
AJ
2705
2706 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 2707 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
70256ad8
AJ
2708 for (; dyncon < dynconend; dyncon++)
2709 {
2710 Elf_Internal_Dyn dyn;
70256ad8
AJ
2711 asection *s;
2712
2713 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2714
2715 switch (dyn.d_tag)
2716 {
2717 default:
053579d7 2718 continue;
70256ad8
AJ
2719
2720 case DT_PLTGOT:
8c37241b
JJ
2721 s = htab->sgotplt;
2722 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
c434dee6 2723 break;
70256ad8
AJ
2724
2725 case DT_JMPREL:
c434dee6
AJ
2726 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2727 break;
70256ad8 2728
c434dee6
AJ
2729 case DT_PLTRELSZ:
2730 s = htab->srelplt->output_section;
eea6121a 2731 dyn.d_un.d_val = s->size;
70256ad8
AJ
2732 break;
2733
2734 case DT_RELASZ:
c434dee6
AJ
2735 /* The procedure linkage table relocs (DT_JMPREL) should
2736 not be included in the overall relocs (DT_RELA).
2737 Therefore, we override the DT_RELASZ entry here to
2738 make it not include the JMPREL relocs. Since the
2739 linker script arranges for .rela.plt to follow all
2740 other relocation sections, we don't have to worry
2741 about changing the DT_RELA entry. */
2742 if (htab->srelplt != NULL)
70256ad8 2743 {
c434dee6 2744 s = htab->srelplt->output_section;
eea6121a 2745 dyn.d_un.d_val -= s->size;
70256ad8
AJ
2746 }
2747 break;
70256ad8 2748 }
c434dee6 2749
70256ad8
AJ
2750 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2751 }
2752
c434dee6 2753 /* Fill in the special first entry in the procedure linkage table. */
eea6121a 2754 if (htab->splt && htab->splt->size > 0)
70256ad8 2755 {
653165cc 2756 /* Fill in the first entry in the procedure linkage table. */
c434dee6
AJ
2757 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
2758 PLT_ENTRY_SIZE);
653165cc
AJ
2759 /* Add offset for pushq GOT+8(%rip), since the instruction
2760 uses 6 bytes subtract this value. */
2761 bfd_put_32 (output_bfd,
c434dee6
AJ
2762 (htab->sgotplt->output_section->vma
2763 + htab->sgotplt->output_offset
653165cc 2764 + 8
c434dee6
AJ
2765 - htab->splt->output_section->vma
2766 - htab->splt->output_offset
653165cc 2767 - 6),
c434dee6 2768 htab->splt->contents + 2);
653165cc
AJ
2769 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2770 the end of the instruction. */
2771 bfd_put_32 (output_bfd,
c434dee6
AJ
2772 (htab->sgotplt->output_section->vma
2773 + htab->sgotplt->output_offset
653165cc 2774 + 16
c434dee6
AJ
2775 - htab->splt->output_section->vma
2776 - htab->splt->output_offset
653165cc 2777 - 12),
c434dee6 2778 htab->splt->contents + 8);
653165cc 2779
c434dee6
AJ
2780 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
2781 PLT_ENTRY_SIZE;
70256ad8 2782 }
70256ad8
AJ
2783 }
2784
c434dee6 2785 if (htab->sgotplt)
70256ad8 2786 {
c434dee6 2787 /* Fill in the first three entries in the global offset table. */
eea6121a 2788 if (htab->sgotplt->size > 0)
c434dee6
AJ
2789 {
2790 /* Set the first entry in the global offset table to the address of
2791 the dynamic section. */
2792 if (sdyn == NULL)
2793 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
2794 else
2795 bfd_put_64 (output_bfd,
2796 sdyn->output_section->vma + sdyn->output_offset,
2797 htab->sgotplt->contents);
2798 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
2799 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
2800 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
2801 }
70256ad8 2802
c434dee6
AJ
2803 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
2804 GOT_ENTRY_SIZE;
2805 }
70256ad8 2806
eea6121a 2807 if (htab->sgot && htab->sgot->size > 0)
8c37241b
JJ
2808 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
2809 = GOT_ENTRY_SIZE;
2810
b34976b6 2811 return TRUE;
8d88c4ca
NC
2812}
2813
4c45e5c9
JJ
2814/* Return address for Ith PLT stub in section PLT, for relocation REL
2815 or (bfd_vma) -1 if it should not be included. */
2816
2817static bfd_vma
2818elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
2819 const arelent *rel ATTRIBUTE_UNUSED)
2820{
2821 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
2822}
8df9fc9d 2823
d2b2c203
DJ
2824/* Handle an x86-64 specific section when reading an object file. This
2825 is called when elfcode.h finds a section with an unknown type. */
2826
2827static bfd_boolean
6dc132d9
L
2828elf64_x86_64_section_from_shdr (bfd *abfd,
2829 Elf_Internal_Shdr *hdr,
2830 const char *name,
2831 int shindex)
d2b2c203
DJ
2832{
2833 if (hdr->sh_type != SHT_X86_64_UNWIND)
2834 return FALSE;
2835
6dc132d9 2836 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
2837 return FALSE;
2838
2839 return TRUE;
2840}
2841
70256ad8
AJ
2842#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
2843#define TARGET_LITTLE_NAME "elf64-x86-64"
2844#define ELF_ARCH bfd_arch_i386
2845#define ELF_MACHINE_CODE EM_X86_64
2846#define ELF_MAXPAGESIZE 0x100000
2847
2848#define elf_backend_can_gc_sections 1
51b64d56 2849#define elf_backend_can_refcount 1
70256ad8
AJ
2850#define elf_backend_want_got_plt 1
2851#define elf_backend_plt_readonly 1
2852#define elf_backend_want_plt_sym 0
2853#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 2854#define elf_backend_rela_normal 1
70256ad8
AJ
2855
2856#define elf_info_to_howto elf64_x86_64_info_to_howto
70256ad8 2857
70256ad8
AJ
2858#define bfd_elf64_bfd_link_hash_table_create \
2859 elf64_x86_64_link_hash_table_create
407443a3 2860#define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
70256ad8
AJ
2861
2862#define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
2863#define elf_backend_check_relocs elf64_x86_64_check_relocs
c434dee6
AJ
2864#define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
2865#define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
2866#define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
70256ad8
AJ
2867#define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
2868#define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
2869#define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3bab7989
ML
2870#define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
2871#define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
c434dee6 2872#define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
70256ad8
AJ
2873#define elf_backend_relocate_section elf64_x86_64_relocate_section
2874#define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
4c45e5c9 2875#define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
407443a3 2876#define elf_backend_object_p elf64_x86_64_elf_object_p
bffbf940 2877#define bfd_elf64_mkobject elf64_x86_64_mkobject
8d88c4ca 2878
d2b2c203
DJ
2879#define elf_backend_section_from_shdr \
2880 elf64_x86_64_section_from_shdr
2881
8d88c4ca 2882#include "elf64-target.h"
This page took 0.526316 seconds and 4 git commands to generate.