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