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