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