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