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