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