bfd/
[external/binutils.git] / bfd / elf64-x86-64.c
1 /* X86-64 specific support for 64-bit ELF
2    Copyright 2000, 2001, 2002, 2003, 2004, 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_signed,
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 (const struct elf_backend_data *bed,
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           if (ind->root.type == bfd_link_hash_indirect)
545             abort ();
546
547           /* Add reloc counts against the weak sym to the strong sym
548              list.  Merge any entries against the same section.  */
549           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
550             {
551               struct elf64_x86_64_dyn_relocs *q;
552
553               for (q = edir->dyn_relocs; q != NULL; q = q->next)
554                 if (q->sec == p->sec)
555                   {
556                     q->pc_count += p->pc_count;
557                     q->count += p->count;
558                     *pp = p->next;
559                     break;
560                   }
561               if (q == NULL)
562                 pp = &p->next;
563             }
564           *pp = edir->dyn_relocs;
565         }
566
567       edir->dyn_relocs = eind->dyn_relocs;
568       eind->dyn_relocs = NULL;
569     }
570
571   if (ind->root.type == bfd_link_hash_indirect
572       && dir->got.refcount <= 0)
573     {
574       edir->tls_type = eind->tls_type;
575       eind->tls_type = GOT_UNKNOWN;
576     }
577
578   if (ELIMINATE_COPY_RELOCS
579       && ind->root.type != bfd_link_hash_indirect
580       && dir->dynamic_adjusted)
581     {
582       /* If called to transfer flags for a weakdef during processing
583          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
584          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
585       dir->ref_dynamic |= ind->ref_dynamic;
586       dir->ref_regular |= ind->ref_regular;
587       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
588       dir->needs_plt |= ind->needs_plt;
589       dir->pointer_equality_needed |= ind->pointer_equality_needed;
590     }
591   else
592     _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
593 }
594
595 static bfd_boolean
596 elf64_x86_64_mkobject (bfd *abfd)
597 {
598   bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
599   abfd->tdata.any = bfd_zalloc (abfd, amt);
600   if (abfd->tdata.any == NULL)
601     return FALSE;
602   return TRUE;
603 }
604
605 static bfd_boolean
606 elf64_x86_64_elf_object_p (bfd *abfd)
607 {
608   /* Set the right machine number for an x86-64 elf64 file.  */
609   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
610   return TRUE;
611 }
612
613 static int
614 elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
615 {
616   if (info->shared)
617     return r_type;
618
619   switch (r_type)
620     {
621     case R_X86_64_TLSGD:
622     case R_X86_64_GOTTPOFF:
623       if (is_local)
624         return R_X86_64_TPOFF32;
625       return R_X86_64_GOTTPOFF;
626     case R_X86_64_TLSLD:
627       return R_X86_64_TPOFF32;
628     }
629
630    return r_type;
631 }
632
633 /* Look through the relocs for a section during the first phase, and
634    calculate needed space in the global offset table, procedure
635    linkage table, and dynamic reloc sections.  */
636
637 static bfd_boolean
638 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
639                            const Elf_Internal_Rela *relocs)
640 {
641   struct elf64_x86_64_link_hash_table *htab;
642   Elf_Internal_Shdr *symtab_hdr;
643   struct elf_link_hash_entry **sym_hashes;
644   const Elf_Internal_Rela *rel;
645   const Elf_Internal_Rela *rel_end;
646   asection *sreloc;
647
648   if (info->relocatable)
649     return TRUE;
650
651   htab = elf64_x86_64_hash_table (info);
652   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
653   sym_hashes = elf_sym_hashes (abfd);
654
655   sreloc = NULL;
656
657   rel_end = relocs + sec->reloc_count;
658   for (rel = relocs; rel < rel_end; rel++)
659     {
660       unsigned int r_type;
661       unsigned long r_symndx;
662       struct elf_link_hash_entry *h;
663
664       r_symndx = ELF64_R_SYM (rel->r_info);
665       r_type = ELF64_R_TYPE (rel->r_info);
666
667       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
668         {
669           (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
670                                  abfd, r_symndx);
671           return FALSE;
672         }
673
674       if (r_symndx < symtab_hdr->sh_info)
675         h = NULL;
676       else
677         {
678           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
679           while (h->root.type == bfd_link_hash_indirect
680                  || h->root.type == bfd_link_hash_warning)
681             h = (struct elf_link_hash_entry *) h->root.u.i.link;
682         }
683
684       r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
685       switch (r_type)
686         {
687         case R_X86_64_TLSLD:
688           htab->tls_ld_got.refcount += 1;
689           goto create_got;
690
691         case R_X86_64_TPOFF32:
692           if (info->shared)
693             {
694               (*_bfd_error_handler)
695                 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
696                  abfd,
697                  x86_64_elf_howto_table[r_type].name,
698                  (h) ? h->root.root.string : "a local symbol");
699               bfd_set_error (bfd_error_bad_value);
700               return FALSE;
701             }
702           break;
703
704         case R_X86_64_GOTTPOFF:
705           if (info->shared)
706             info->flags |= DF_STATIC_TLS;
707           /* Fall through */
708
709         case R_X86_64_GOT32:
710         case R_X86_64_GOTPCREL:
711         case R_X86_64_TLSGD:
712           /* This symbol requires a global offset table entry.  */
713           {
714             int tls_type, old_tls_type;
715
716             switch (r_type)
717               {
718               default: tls_type = GOT_NORMAL; break;
719               case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
720               case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
721               }
722
723             if (h != NULL)
724               {
725                 h->got.refcount += 1;
726                 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
727               }
728             else
729               {
730                 bfd_signed_vma *local_got_refcounts;
731
732                 /* This is a global offset table entry for a local symbol.  */
733                 local_got_refcounts = elf_local_got_refcounts (abfd);
734                 if (local_got_refcounts == NULL)
735                   {
736                     bfd_size_type size;
737
738                     size = symtab_hdr->sh_info;
739                     size *= sizeof (bfd_signed_vma) + sizeof (char);
740                     local_got_refcounts = ((bfd_signed_vma *)
741                                            bfd_zalloc (abfd, size));
742                     if (local_got_refcounts == NULL)
743                       return FALSE;
744                     elf_local_got_refcounts (abfd) = local_got_refcounts;
745                     elf64_x86_64_local_got_tls_type (abfd)
746                       = (char *) (local_got_refcounts + symtab_hdr->sh_info);
747                   }
748                 local_got_refcounts[r_symndx] += 1;
749                 old_tls_type
750                   = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
751               }
752
753             /* If a TLS symbol is accessed using IE at least once,
754                there is no point to use dynamic model for it.  */
755             if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
756                 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
757               {
758                 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
759                   tls_type = old_tls_type;
760                 else
761                   {
762                     (*_bfd_error_handler)
763                       (_("%B: %s' accessed both as normal and thread local symbol"),
764                        abfd, h ? h->root.root.string : "<local>");
765                     return FALSE;
766                   }
767               }
768
769             if (old_tls_type != tls_type)
770               {
771                 if (h != NULL)
772                   elf64_x86_64_hash_entry (h)->tls_type = tls_type;
773                 else
774                   elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
775               }
776           }
777           /* Fall through */
778
779         case R_X86_64_GOTOFF64:
780         case R_X86_64_GOTPC32:
781         create_got:
782           if (htab->sgot == NULL)
783             {
784               if (htab->elf.dynobj == NULL)
785                 htab->elf.dynobj = abfd;
786               if (!create_got_section (htab->elf.dynobj, info))
787                 return FALSE;
788             }
789           break;
790
791         case R_X86_64_PLT32:
792           /* This symbol requires a procedure linkage table entry.  We
793              actually build the entry in adjust_dynamic_symbol,
794              because this might be a case of linking PIC code which is
795              never referenced by a dynamic object, in which case we
796              don't need to generate a procedure linkage table entry
797              after all.  */
798
799           /* If this is a local symbol, we resolve it directly without
800              creating a procedure linkage table entry.  */
801           if (h == NULL)
802             continue;
803
804           h->needs_plt = 1;
805           h->plt.refcount += 1;
806           break;
807
808         case R_X86_64_8:
809         case R_X86_64_16:
810         case R_X86_64_32:
811         case R_X86_64_32S:
812           /* Let's help debug shared library creation.  These relocs
813              cannot be used in shared libs.  Don't error out for
814              sections we don't care about, such as debug sections or
815              non-constant sections.  */
816           if (info->shared
817               && (sec->flags & SEC_ALLOC) != 0
818               && (sec->flags & SEC_READONLY) != 0)
819             {
820               (*_bfd_error_handler)
821                 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
822                  abfd,
823                  x86_64_elf_howto_table[r_type].name,
824                  (h) ? h->root.root.string : "a local symbol");
825               bfd_set_error (bfd_error_bad_value);
826               return FALSE;
827             }
828           /* Fall through.  */
829
830         case R_X86_64_PC8:
831         case R_X86_64_PC16:
832         case R_X86_64_PC32:
833         case R_X86_64_PC64:
834         case R_X86_64_64:
835           if (h != NULL && !info->shared)
836             {
837               /* If this reloc is in a read-only section, we might
838                  need a copy reloc.  We can't check reliably at this
839                  stage whether the section is read-only, as input
840                  sections have not yet been mapped to output sections.
841                  Tentatively set the flag for now, and correct in
842                  adjust_dynamic_symbol.  */
843               h->non_got_ref = 1;
844
845               /* We may need a .plt entry if the function this reloc
846                  refers to is in a shared lib.  */
847               h->plt.refcount += 1;
848               if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
849                 h->pointer_equality_needed = 1;
850             }
851
852           /* If we are creating a shared library, and this is a reloc
853              against a global symbol, or a non PC relative reloc
854              against a local symbol, then we need to copy the reloc
855              into the shared library.  However, if we are linking with
856              -Bsymbolic, we do not need to copy a reloc against a
857              global symbol which is defined in an object we are
858              including in the link (i.e., DEF_REGULAR is set).  At
859              this point we have not seen all the input files, so it is
860              possible that DEF_REGULAR is not set now but will be set
861              later (it is never cleared).  In case of a weak definition,
862              DEF_REGULAR may be cleared later by a strong definition in
863              a shared library.  We account for that possibility below by
864              storing information in the relocs_copied field of the hash
865              table entry.  A similar situation occurs when creating
866              shared libraries and symbol visibility changes render the
867              symbol local.
868
869              If on the other hand, we are creating an executable, we
870              may need to keep relocations for symbols satisfied by a
871              dynamic library if we manage to avoid copy relocs for the
872              symbol.  */
873           if ((info->shared
874                && (sec->flags & SEC_ALLOC) != 0
875                && (((r_type != R_X86_64_PC8)
876                     && (r_type != R_X86_64_PC16)
877                     && (r_type != R_X86_64_PC32)
878                     && (r_type != R_X86_64_PC64))
879                    || (h != NULL
880                        && (! info->symbolic
881                            || h->root.type == bfd_link_hash_defweak
882                            || !h->def_regular))))
883               || (ELIMINATE_COPY_RELOCS
884                   && !info->shared
885                   && (sec->flags & SEC_ALLOC) != 0
886                   && h != NULL
887                   && (h->root.type == bfd_link_hash_defweak
888                       || !h->def_regular)))
889             {
890               struct elf64_x86_64_dyn_relocs *p;
891               struct elf64_x86_64_dyn_relocs **head;
892
893               /* We must copy these reloc types into the output file.
894                  Create a reloc section in dynobj and make room for
895                  this reloc.  */
896               if (sreloc == NULL)
897                 {
898                   const char *name;
899                   bfd *dynobj;
900
901                   name = (bfd_elf_string_from_elf_section
902                           (abfd,
903                            elf_elfheader (abfd)->e_shstrndx,
904                            elf_section_data (sec)->rel_hdr.sh_name));
905                   if (name == NULL)
906                     return FALSE;
907
908                   if (strncmp (name, ".rela", 5) != 0
909                       || strcmp (bfd_get_section_name (abfd, sec),
910                                  name + 5) != 0)
911                     {
912                       (*_bfd_error_handler)
913                         (_("%B: bad relocation section name `%s\'"),
914                          abfd, name);
915                     }
916
917                   if (htab->elf.dynobj == NULL)
918                     htab->elf.dynobj = abfd;
919
920                   dynobj = htab->elf.dynobj;
921
922                   sreloc = bfd_get_section_by_name (dynobj, name);
923                   if (sreloc == NULL)
924                     {
925                       flagword flags;
926
927                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
928                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
929                       if ((sec->flags & SEC_ALLOC) != 0)
930                         flags |= SEC_ALLOC | SEC_LOAD;
931                       sreloc = bfd_make_section_with_flags (dynobj,
932                                                             name,
933                                                             flags);
934                       if (sreloc == NULL
935                           || ! bfd_set_section_alignment (dynobj, sreloc, 3))
936                         return FALSE;
937                     }
938                   elf_section_data (sec)->sreloc = sreloc;
939                 }
940
941               /* If this is a global symbol, we count the number of
942                  relocations we need for this symbol.  */
943               if (h != NULL)
944                 {
945                   head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
946                 }
947               else
948                 {
949                   /* Track dynamic relocs needed for local syms too.
950                      We really need local syms available to do this
951                      easily.  Oh well.  */
952
953                   asection *s;
954                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
955                                                  sec, r_symndx);
956                   if (s == NULL)
957                     return FALSE;
958
959                   head = ((struct elf64_x86_64_dyn_relocs **)
960                           &elf_section_data (s)->local_dynrel);
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   /* We must allocate the symbol in our .dynbss section, which will
1251      become part of the .bss section of the executable.  There will be
1252      an entry for this symbol in the .dynsym section.  The dynamic
1253      object will contain position independent code, so all references
1254      from the dynamic object to this symbol will go through the global
1255      offset table.  The dynamic linker will use the .dynsym entry to
1256      determine the address it must put in the global offset table, so
1257      both the dynamic object and the regular object will refer to the
1258      same memory location for the variable.  */
1259
1260   htab = elf64_x86_64_hash_table (info);
1261
1262   /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1263      to copy the initial value out of the dynamic object and into the
1264      runtime process image.  */
1265   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1266     {
1267       htab->srelbss->size += sizeof (Elf64_External_Rela);
1268       h->needs_copy = 1;
1269     }
1270
1271   /* We need to figure out the alignment required for this symbol.  I
1272      have no idea how ELF linkers handle this.  16-bytes is the size
1273      of the largest type that requires hard alignment -- long double.  */
1274   /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1275      this construct.  */
1276   power_of_two = bfd_log2 (h->size);
1277   if (power_of_two > 4)
1278     power_of_two = 4;
1279
1280   /* Apply the required alignment.  */
1281   s = htab->sdynbss;
1282   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1283   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1284     {
1285       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1286         return FALSE;
1287     }
1288
1289   /* Define the symbol as being at this point in the section.  */
1290   h->root.u.def.section = s;
1291   h->root.u.def.value = s->size;
1292
1293   /* Increment the section size to make room for the symbol.  */
1294   s->size += h->size;
1295
1296   return TRUE;
1297 }
1298
1299 /* Allocate space in .plt, .got and associated reloc sections for
1300    dynamic relocs.  */
1301
1302 static bfd_boolean
1303 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1304 {
1305   struct bfd_link_info *info;
1306   struct elf64_x86_64_link_hash_table *htab;
1307   struct elf64_x86_64_link_hash_entry *eh;
1308   struct elf64_x86_64_dyn_relocs *p;
1309
1310   if (h->root.type == bfd_link_hash_indirect)
1311     return TRUE;
1312
1313   if (h->root.type == bfd_link_hash_warning)
1314     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1315
1316   info = (struct bfd_link_info *) inf;
1317   htab = elf64_x86_64_hash_table (info);
1318
1319   if (htab->elf.dynamic_sections_created
1320       && h->plt.refcount > 0)
1321     {
1322       /* Make sure this symbol is output as a dynamic symbol.
1323          Undefined weak syms won't yet be marked as dynamic.  */
1324       if (h->dynindx == -1
1325           && !h->forced_local)
1326         {
1327           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1328             return FALSE;
1329         }
1330
1331       if (info->shared
1332           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1333         {
1334           asection *s = htab->splt;
1335
1336           /* If this is the first .plt entry, make room for the special
1337              first entry.  */
1338           if (s->size == 0)
1339             s->size += PLT_ENTRY_SIZE;
1340
1341           h->plt.offset = s->size;
1342
1343           /* If this symbol is not defined in a regular file, and we are
1344              not generating a shared library, then set the symbol to this
1345              location in the .plt.  This is required to make function
1346              pointers compare as equal between the normal executable and
1347              the shared library.  */
1348           if (! info->shared
1349               && !h->def_regular)
1350             {
1351               h->root.u.def.section = s;
1352               h->root.u.def.value = h->plt.offset;
1353             }
1354
1355           /* Make room for this entry.  */
1356           s->size += PLT_ENTRY_SIZE;
1357
1358           /* We also need to make an entry in the .got.plt section, which
1359              will be placed in the .got section by the linker script.  */
1360           htab->sgotplt->size += GOT_ENTRY_SIZE;
1361
1362           /* We also need to make an entry in the .rela.plt section.  */
1363           htab->srelplt->size += sizeof (Elf64_External_Rela);
1364         }
1365       else
1366         {
1367           h->plt.offset = (bfd_vma) -1;
1368           h->needs_plt = 0;
1369         }
1370     }
1371   else
1372     {
1373       h->plt.offset = (bfd_vma) -1;
1374       h->needs_plt = 0;
1375     }
1376
1377   /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1378      make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
1379   if (h->got.refcount > 0
1380       && !info->shared
1381       && h->dynindx == -1
1382       && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1383     h->got.offset = (bfd_vma) -1;
1384   else if (h->got.refcount > 0)
1385     {
1386       asection *s;
1387       bfd_boolean dyn;
1388       int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1389
1390       /* Make sure this symbol is output as a dynamic symbol.
1391          Undefined weak syms won't yet be marked as dynamic.  */
1392       if (h->dynindx == -1
1393           && !h->forced_local)
1394         {
1395           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1396             return FALSE;
1397         }
1398
1399       s = htab->sgot;
1400       h->got.offset = s->size;
1401       s->size += GOT_ENTRY_SIZE;
1402       /* R_X86_64_TLSGD needs 2 consecutive GOT slots.  */
1403       if (tls_type == GOT_TLS_GD)
1404         s->size += GOT_ENTRY_SIZE;
1405       dyn = htab->elf.dynamic_sections_created;
1406       /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1407          and two if global.
1408          R_X86_64_GOTTPOFF needs one dynamic relocation.  */
1409       if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1410           || tls_type == GOT_TLS_IE)
1411         htab->srelgot->size += sizeof (Elf64_External_Rela);
1412       else if (tls_type == GOT_TLS_GD)
1413         htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
1414       else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1415                 || h->root.type != bfd_link_hash_undefweak)
1416                && (info->shared
1417                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1418         htab->srelgot->size += sizeof (Elf64_External_Rela);
1419     }
1420   else
1421     h->got.offset = (bfd_vma) -1;
1422
1423   eh = (struct elf64_x86_64_link_hash_entry *) h;
1424   if (eh->dyn_relocs == NULL)
1425     return TRUE;
1426
1427   /* In the shared -Bsymbolic case, discard space allocated for
1428      dynamic pc-relative relocs against symbols which turn out to be
1429      defined in regular objects.  For the normal shared case, discard
1430      space for pc-relative relocs that have become local due to symbol
1431      visibility changes.  */
1432
1433   if (info->shared)
1434     {
1435       /* Relocs that use pc_count are those that appear on a call
1436          insn, or certain REL relocs that can generated via assembly.
1437          We want calls to protected symbols to resolve directly to the
1438          function rather than going via the plt.  If people want
1439          function pointer comparisons to work as expected then they
1440          should avoid writing weird assembly.  */
1441       if (SYMBOL_CALLS_LOCAL (info, h))
1442         {
1443           struct elf64_x86_64_dyn_relocs **pp;
1444
1445           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1446             {
1447               p->count -= p->pc_count;
1448               p->pc_count = 0;
1449               if (p->count == 0)
1450                 *pp = p->next;
1451               else
1452                 pp = &p->next;
1453             }
1454         }
1455
1456       /* Also discard relocs on undefined weak syms with non-default
1457          visibility.  */
1458       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1459           && h->root.type == bfd_link_hash_undefweak)
1460         eh->dyn_relocs = NULL;
1461     }
1462   else if (ELIMINATE_COPY_RELOCS)
1463     {
1464       /* For the non-shared case, discard space for relocs against
1465          symbols which turn out to need copy relocs or are not
1466          dynamic.  */
1467
1468       if (!h->non_got_ref
1469           && ((h->def_dynamic
1470                && !h->def_regular)
1471               || (htab->elf.dynamic_sections_created
1472                   && (h->root.type == bfd_link_hash_undefweak
1473                       || h->root.type == bfd_link_hash_undefined))))
1474         {
1475           /* Make sure this symbol is output as a dynamic symbol.
1476              Undefined weak syms won't yet be marked as dynamic.  */
1477           if (h->dynindx == -1
1478               && !h->forced_local)
1479             {
1480               if (! bfd_elf_link_record_dynamic_symbol (info, h))
1481                 return FALSE;
1482             }
1483
1484           /* If that succeeded, we know we'll be keeping all the
1485              relocs.  */
1486           if (h->dynindx != -1)
1487             goto keep;
1488         }
1489
1490       eh->dyn_relocs = NULL;
1491
1492     keep: ;
1493     }
1494
1495   /* Finally, allocate space.  */
1496   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1497     {
1498       asection *sreloc = elf_section_data (p->sec)->sreloc;
1499       sreloc->size += p->count * sizeof (Elf64_External_Rela);
1500     }
1501
1502   return TRUE;
1503 }
1504
1505 /* Find any dynamic relocs that apply to read-only sections.  */
1506
1507 static bfd_boolean
1508 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1509 {
1510   struct elf64_x86_64_link_hash_entry *eh;
1511   struct elf64_x86_64_dyn_relocs *p;
1512
1513   if (h->root.type == bfd_link_hash_warning)
1514     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1515
1516   eh = (struct elf64_x86_64_link_hash_entry *) h;
1517   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1518     {
1519       asection *s = p->sec->output_section;
1520
1521       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1522         {
1523           struct bfd_link_info *info = (struct bfd_link_info *) inf;
1524
1525           info->flags |= DF_TEXTREL;
1526
1527           /* Not an error, just cut short the traversal.  */
1528           return FALSE;
1529         }
1530     }
1531   return TRUE;
1532 }
1533
1534 /* Set the sizes of the dynamic sections.  */
1535
1536 static bfd_boolean
1537 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1538                                     struct bfd_link_info *info)
1539 {
1540   struct elf64_x86_64_link_hash_table *htab;
1541   bfd *dynobj;
1542   asection *s;
1543   bfd_boolean relocs;
1544   bfd *ibfd;
1545
1546   htab = elf64_x86_64_hash_table (info);
1547   dynobj = htab->elf.dynobj;
1548   if (dynobj == NULL)
1549     abort ();
1550
1551   if (htab->elf.dynamic_sections_created)
1552     {
1553       /* Set the contents of the .interp section to the interpreter.  */
1554       if (info->executable)
1555         {
1556           s = bfd_get_section_by_name (dynobj, ".interp");
1557           if (s == NULL)
1558             abort ();
1559           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1560           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1561         }
1562     }
1563
1564   /* Set up .got offsets for local syms, and space for local dynamic
1565      relocs.  */
1566   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1567     {
1568       bfd_signed_vma *local_got;
1569       bfd_signed_vma *end_local_got;
1570       char *local_tls_type;
1571       bfd_size_type locsymcount;
1572       Elf_Internal_Shdr *symtab_hdr;
1573       asection *srel;
1574
1575       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1576         continue;
1577
1578       for (s = ibfd->sections; s != NULL; s = s->next)
1579         {
1580           struct elf64_x86_64_dyn_relocs *p;
1581
1582           for (p = *((struct elf64_x86_64_dyn_relocs **)
1583                      &elf_section_data (s)->local_dynrel);
1584                p != NULL;
1585                p = p->next)
1586             {
1587               if (!bfd_is_abs_section (p->sec)
1588                   && bfd_is_abs_section (p->sec->output_section))
1589                 {
1590                   /* Input section has been discarded, either because
1591                      it is a copy of a linkonce section or due to
1592                      linker script /DISCARD/, so we'll be discarding
1593                      the relocs too.  */
1594                 }
1595               else if (p->count != 0)
1596                 {
1597                   srel = elf_section_data (p->sec)->sreloc;
1598                   srel->size += p->count * sizeof (Elf64_External_Rela);
1599                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1600                     info->flags |= DF_TEXTREL;
1601
1602                 }
1603             }
1604         }
1605
1606       local_got = elf_local_got_refcounts (ibfd);
1607       if (!local_got)
1608         continue;
1609
1610       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1611       locsymcount = symtab_hdr->sh_info;
1612       end_local_got = local_got + locsymcount;
1613       local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
1614       s = htab->sgot;
1615       srel = htab->srelgot;
1616       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1617         {
1618           if (*local_got > 0)
1619             {
1620               *local_got = s->size;
1621               s->size += GOT_ENTRY_SIZE;
1622               if (*local_tls_type == GOT_TLS_GD)
1623                 s->size += GOT_ENTRY_SIZE;
1624               if (info->shared
1625                   || *local_tls_type == GOT_TLS_GD
1626                   || *local_tls_type == GOT_TLS_IE)
1627                 srel->size += sizeof (Elf64_External_Rela);
1628             }
1629           else
1630             *local_got = (bfd_vma) -1;
1631         }
1632     }
1633
1634   if (htab->tls_ld_got.refcount > 0)
1635     {
1636       /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1637          relocs.  */
1638       htab->tls_ld_got.offset = htab->sgot->size;
1639       htab->sgot->size += 2 * GOT_ENTRY_SIZE;
1640       htab->srelgot->size += sizeof (Elf64_External_Rela);
1641     }
1642   else
1643     htab->tls_ld_got.offset = -1;
1644
1645   /* Allocate global sym .plt and .got entries, and space for global
1646      sym dynamic relocs.  */
1647   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1648
1649   /* We now have determined the sizes of the various dynamic sections.
1650      Allocate memory for them.  */
1651   relocs = FALSE;
1652   for (s = dynobj->sections; s != NULL; s = s->next)
1653     {
1654       if ((s->flags & SEC_LINKER_CREATED) == 0)
1655         continue;
1656
1657       if (s == htab->splt
1658           || s == htab->sgot
1659           || s == htab->sgotplt
1660           || s == htab->sdynbss)
1661         {
1662           /* Strip this section if we don't need it; see the
1663              comment below.  */
1664         }
1665       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1666         {
1667           if (s->size != 0 && s != htab->srelplt)
1668             relocs = TRUE;
1669
1670           /* We use the reloc_count field as a counter if we need
1671              to copy relocs into the output file.  */
1672           s->reloc_count = 0;
1673         }
1674       else
1675         {
1676           /* It's not one of our sections, so don't allocate space.  */
1677           continue;
1678         }
1679
1680       if (s->size == 0)
1681         {
1682           /* If we don't need this section, strip it from the
1683              output file.  This is mostly to handle .rela.bss and
1684              .rela.plt.  We must create both sections in
1685              create_dynamic_sections, because they must be created
1686              before the linker maps input sections to output
1687              sections.  The linker does that before
1688              adjust_dynamic_symbol is called, and it is that
1689              function which decides whether anything needs to go
1690              into these sections.  */
1691
1692           s->flags |= SEC_EXCLUDE;
1693           continue;
1694         }
1695
1696       if ((s->flags & SEC_HAS_CONTENTS) == 0)
1697         continue;
1698
1699       /* Allocate memory for the section contents.  We use bfd_zalloc
1700          here in case unused entries are not reclaimed before the
1701          section's contents are written out.  This should not happen,
1702          but this way if it does, we get a R_X86_64_NONE reloc instead
1703          of garbage.  */
1704       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1705       if (s->contents == NULL)
1706         return FALSE;
1707     }
1708
1709   if (htab->elf.dynamic_sections_created)
1710     {
1711       /* Add some entries to the .dynamic section.  We fill in the
1712          values later, in elf64_x86_64_finish_dynamic_sections, but we
1713          must add the entries now so that we get the correct size for
1714          the .dynamic section.  The DT_DEBUG entry is filled in by the
1715          dynamic linker and used by the debugger.  */
1716 #define add_dynamic_entry(TAG, VAL) \
1717   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1718
1719       if (info->executable)
1720         {
1721           if (!add_dynamic_entry (DT_DEBUG, 0))
1722             return FALSE;
1723         }
1724
1725       if (htab->splt->size != 0)
1726         {
1727           if (!add_dynamic_entry (DT_PLTGOT, 0)
1728               || !add_dynamic_entry (DT_PLTRELSZ, 0)
1729               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1730               || !add_dynamic_entry (DT_JMPREL, 0))
1731             return FALSE;
1732         }
1733
1734       if (relocs)
1735         {
1736           if (!add_dynamic_entry (DT_RELA, 0)
1737               || !add_dynamic_entry (DT_RELASZ, 0)
1738               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1739             return FALSE;
1740
1741           /* If any dynamic relocs apply to a read-only section,
1742              then we need a DT_TEXTREL entry.  */
1743           if ((info->flags & DF_TEXTREL) == 0)
1744             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1745                                     (PTR) info);
1746
1747           if ((info->flags & DF_TEXTREL) != 0)
1748             {
1749               if (!add_dynamic_entry (DT_TEXTREL, 0))
1750                 return FALSE;
1751             }
1752         }
1753     }
1754 #undef add_dynamic_entry
1755
1756   return TRUE;
1757 }
1758
1759 /* Return the base VMA address which should be subtracted from real addresses
1760    when resolving @dtpoff relocation.
1761    This is PT_TLS segment p_vaddr.  */
1762
1763 static bfd_vma
1764 dtpoff_base (struct bfd_link_info *info)
1765 {
1766   /* If tls_sec is NULL, we should have signalled an error already.  */
1767   if (elf_hash_table (info)->tls_sec == NULL)
1768     return 0;
1769   return elf_hash_table (info)->tls_sec->vma;
1770 }
1771
1772 /* Return the relocation value for @tpoff relocation
1773    if STT_TLS virtual address is ADDRESS.  */
1774
1775 static bfd_vma
1776 tpoff (struct bfd_link_info *info, bfd_vma address)
1777 {
1778   struct elf_link_hash_table *htab = elf_hash_table (info);
1779
1780   /* If tls_segment is NULL, we should have signalled an error already.  */
1781   if (htab->tls_sec == NULL)
1782     return 0;
1783   return address - htab->tls_size - htab->tls_sec->vma;
1784 }
1785
1786 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
1787    branch?  */
1788
1789 static bfd_boolean
1790 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
1791 {
1792   /* Opcode             Instruction
1793      0xe8               call
1794      0xe9               jump
1795      0x0f 0x8x          conditional jump */
1796   return ((offset > 0
1797            && (contents [offset - 1] == 0xe8
1798                || contents [offset - 1] == 0xe9))
1799           || (offset > 1
1800               && contents [offset - 2] == 0x0f
1801               && (contents [offset - 1] & 0xf0) == 0x80));
1802 }
1803
1804 /* Relocate an x86_64 ELF section.  */
1805
1806 static bfd_boolean
1807 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
1808                                bfd *input_bfd, asection *input_section,
1809                                bfd_byte *contents, Elf_Internal_Rela *relocs,
1810                                Elf_Internal_Sym *local_syms,
1811                                asection **local_sections)
1812 {
1813   struct elf64_x86_64_link_hash_table *htab;
1814   Elf_Internal_Shdr *symtab_hdr;
1815   struct elf_link_hash_entry **sym_hashes;
1816   bfd_vma *local_got_offsets;
1817   Elf_Internal_Rela *rel;
1818   Elf_Internal_Rela *relend;
1819
1820   if (info->relocatable)
1821     return TRUE;
1822
1823   htab = elf64_x86_64_hash_table (info);
1824   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1825   sym_hashes = elf_sym_hashes (input_bfd);
1826   local_got_offsets = elf_local_got_offsets (input_bfd);
1827
1828   rel = relocs;
1829   relend = relocs + input_section->reloc_count;
1830   for (; rel < relend; rel++)
1831     {
1832       unsigned int r_type;
1833       reloc_howto_type *howto;
1834       unsigned long r_symndx;
1835       struct elf_link_hash_entry *h;
1836       Elf_Internal_Sym *sym;
1837       asection *sec;
1838       bfd_vma off;
1839       bfd_vma relocation;
1840       bfd_boolean unresolved_reloc;
1841       bfd_reloc_status_type r;
1842       int tls_type;
1843
1844       r_type = ELF64_R_TYPE (rel->r_info);
1845       if (r_type == (int) R_X86_64_GNU_VTINHERIT
1846           || r_type == (int) R_X86_64_GNU_VTENTRY)
1847         continue;
1848
1849       if (r_type >= R_X86_64_max)
1850         {
1851           bfd_set_error (bfd_error_bad_value);
1852           return FALSE;
1853         }
1854
1855       howto = x86_64_elf_howto_table + r_type;
1856       r_symndx = ELF64_R_SYM (rel->r_info);
1857       h = NULL;
1858       sym = NULL;
1859       sec = NULL;
1860       unresolved_reloc = FALSE;
1861       if (r_symndx < symtab_hdr->sh_info)
1862         {
1863           sym = local_syms + r_symndx;
1864           sec = local_sections[r_symndx];
1865
1866           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1867         }
1868       else
1869         {
1870           bfd_boolean warned;
1871
1872           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1873                                    r_symndx, symtab_hdr, sym_hashes,
1874                                    h, sec, relocation,
1875                                    unresolved_reloc, warned);
1876         }
1877       /* When generating a shared object, the relocations handled here are
1878          copied into the output file to be resolved at run time.  */
1879       switch (r_type)
1880         {
1881         case R_X86_64_GOT32:
1882           /* Relocation is to the entry for this symbol in the global
1883              offset table.  */
1884         case R_X86_64_GOTPCREL:
1885           /* Use global offset table as symbol value.  */
1886           if (htab->sgot == NULL)
1887             abort ();
1888
1889           if (h != NULL)
1890             {
1891               bfd_boolean dyn;
1892
1893               off = h->got.offset;
1894               dyn = htab->elf.dynamic_sections_created;
1895
1896               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1897                   || (info->shared
1898                       && SYMBOL_REFERENCES_LOCAL (info, h))
1899                   || (ELF_ST_VISIBILITY (h->other)
1900                       && h->root.type == bfd_link_hash_undefweak))
1901                 {
1902                   /* This is actually a static link, or it is a -Bsymbolic
1903                      link and the symbol is defined locally, or the symbol
1904                      was forced to be local because of a version file.  We
1905                      must initialize this entry in the global offset table.
1906                      Since the offset must always be a multiple of 8, we
1907                      use the least significant bit to record whether we
1908                      have initialized it already.
1909
1910                      When doing a dynamic link, we create a .rela.got
1911                      relocation entry to initialize the value.  This is
1912                      done in the finish_dynamic_symbol routine.  */
1913                   if ((off & 1) != 0)
1914                     off &= ~1;
1915                   else
1916                     {
1917                       bfd_put_64 (output_bfd, relocation,
1918                                   htab->sgot->contents + off);
1919                       h->got.offset |= 1;
1920                     }
1921                 }
1922               else
1923                 unresolved_reloc = FALSE;
1924             }
1925           else
1926             {
1927               if (local_got_offsets == NULL)
1928                 abort ();
1929
1930               off = local_got_offsets[r_symndx];
1931
1932               /* The offset must always be a multiple of 8.  We use
1933                  the least significant bit to record whether we have
1934                  already generated the necessary reloc.  */
1935               if ((off & 1) != 0)
1936                 off &= ~1;
1937               else
1938                 {
1939                   bfd_put_64 (output_bfd, relocation,
1940                               htab->sgot->contents + off);
1941
1942                   if (info->shared)
1943                     {
1944                       asection *s;
1945                       Elf_Internal_Rela outrel;
1946                       bfd_byte *loc;
1947
1948                       /* We need to generate a R_X86_64_RELATIVE reloc
1949                          for the dynamic linker.  */
1950                       s = htab->srelgot;
1951                       if (s == NULL)
1952                         abort ();
1953
1954                       outrel.r_offset = (htab->sgot->output_section->vma
1955                                          + htab->sgot->output_offset
1956                                          + off);
1957                       outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1958                       outrel.r_addend = relocation;
1959                       loc = s->contents;
1960                       loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
1961                       bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1962                     }
1963
1964                   local_got_offsets[r_symndx] |= 1;
1965                 }
1966             }
1967
1968           if (off >= (bfd_vma) -2)
1969             abort ();
1970
1971           relocation = htab->sgot->output_section->vma
1972                        + htab->sgot->output_offset + off;
1973           if (r_type != R_X86_64_GOTPCREL)
1974             relocation -= htab->sgotplt->output_section->vma
1975                           - htab->sgotplt->output_offset;
1976
1977           break;
1978
1979         case R_X86_64_GOTOFF64:
1980           /* Relocation is relative to the start of the global offset
1981              table.  */
1982
1983           /* Check to make sure it isn't a protected function symbol
1984              for shared library since it may not be local when used
1985              as function address.  */
1986           if (info->shared
1987               && h
1988               && h->def_regular
1989               && h->type == STT_FUNC
1990               && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1991             {
1992               (*_bfd_error_handler)
1993                 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
1994                  input_bfd, h->root.root.string);
1995               bfd_set_error (bfd_error_bad_value);
1996               return FALSE;
1997             }
1998
1999           /* Note that sgot is not involved in this
2000              calculation.  We always want the start of .got.plt.  If we
2001              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2002              permitted by the ABI, we might have to change this
2003              calculation.  */
2004           relocation -= htab->sgotplt->output_section->vma
2005                         + htab->sgotplt->output_offset;
2006           break;
2007
2008         case R_X86_64_GOTPC32:
2009           /* Use global offset table as symbol value.  */
2010           relocation = htab->sgotplt->output_section->vma
2011                        + htab->sgotplt->output_offset;
2012           unresolved_reloc = FALSE;
2013           break;
2014
2015         case R_X86_64_PLT32:
2016           /* Relocation is to the entry for this symbol in the
2017              procedure linkage table.  */
2018
2019           /* Resolve a PLT32 reloc against a local symbol directly,
2020              without using the procedure linkage table.  */
2021           if (h == NULL)
2022             break;
2023
2024           if (h->plt.offset == (bfd_vma) -1
2025               || htab->splt == NULL)
2026             {
2027               /* We didn't make a PLT entry for this symbol.  This
2028                  happens when statically linking PIC code, or when
2029                  using -Bsymbolic.  */
2030               break;
2031             }
2032
2033           relocation = (htab->splt->output_section->vma
2034                         + htab->splt->output_offset
2035                         + h->plt.offset);
2036           unresolved_reloc = FALSE;
2037           break;
2038
2039         case R_X86_64_PC8:
2040         case R_X86_64_PC16:
2041         case R_X86_64_PC32:
2042           if (info->shared
2043               && !SYMBOL_REFERENCES_LOCAL (info, h)
2044               && (input_section->flags & SEC_ALLOC) != 0
2045               && (input_section->flags & SEC_READONLY) != 0
2046               && (!h->def_regular
2047                   || r_type != R_X86_64_PC32
2048                   || h->type != STT_FUNC
2049                   || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
2050                   || !is_32bit_relative_branch (contents,
2051                                                 rel->r_offset)))
2052             {
2053               if (h->def_regular
2054                   && r_type == R_X86_64_PC32
2055                   && h->type == STT_FUNC
2056                   && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2057                 (*_bfd_error_handler)
2058                    (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
2059                     input_bfd, h->root.root.string);
2060               else
2061                 (*_bfd_error_handler)
2062                   (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2063                    input_bfd, x86_64_elf_howto_table[r_type].name,
2064                    h->root.root.string);
2065               bfd_set_error (bfd_error_bad_value);
2066               return FALSE;
2067             }
2068           /* Fall through.  */
2069
2070         case R_X86_64_8:
2071         case R_X86_64_16:
2072         case R_X86_64_32:
2073         case R_X86_64_PC64:
2074         case R_X86_64_64:
2075           /* FIXME: The ABI says the linker should make sure the value is
2076              the same when it's zeroextended to 64 bit.  */
2077
2078           /* r_symndx will be zero only for relocs against symbols
2079              from removed linkonce sections, or sections discarded by
2080              a linker script.  */
2081           if (r_symndx == 0
2082               || (input_section->flags & SEC_ALLOC) == 0)
2083             break;
2084
2085           if ((info->shared
2086                && (h == NULL
2087                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2088                    || h->root.type != bfd_link_hash_undefweak)
2089                && ((r_type != R_X86_64_PC8
2090                     && r_type != R_X86_64_PC16
2091                     && r_type != R_X86_64_PC32
2092                     && r_type != R_X86_64_PC64)
2093                    || !SYMBOL_CALLS_LOCAL (info, h)))
2094               || (ELIMINATE_COPY_RELOCS
2095                   && !info->shared
2096                   && h != NULL
2097                   && h->dynindx != -1
2098                   && !h->non_got_ref
2099                   && ((h->def_dynamic
2100                        && !h->def_regular)
2101                       || h->root.type == bfd_link_hash_undefweak
2102                       || h->root.type == bfd_link_hash_undefined)))
2103             {
2104               Elf_Internal_Rela outrel;
2105               bfd_byte *loc;
2106               bfd_boolean skip, relocate;
2107               asection *sreloc;
2108
2109               /* When generating a shared object, these relocations
2110                  are copied into the output file to be resolved at run
2111                  time.  */
2112               skip = FALSE;
2113               relocate = FALSE;
2114
2115               outrel.r_offset =
2116                 _bfd_elf_section_offset (output_bfd, info, input_section,
2117                                          rel->r_offset);
2118               if (outrel.r_offset == (bfd_vma) -1)
2119                 skip = TRUE;
2120               else if (outrel.r_offset == (bfd_vma) -2)
2121                 skip = TRUE, relocate = TRUE;
2122
2123               outrel.r_offset += (input_section->output_section->vma
2124                                   + input_section->output_offset);
2125
2126               if (skip)
2127                 memset (&outrel, 0, sizeof outrel);
2128
2129               /* h->dynindx may be -1 if this symbol was marked to
2130                  become local.  */
2131               else if (h != NULL
2132                        && h->dynindx != -1
2133                        && (r_type == R_X86_64_PC8
2134                            || r_type == R_X86_64_PC16
2135                            || r_type == R_X86_64_PC32
2136                            || r_type == R_X86_64_PC64
2137                            || !info->shared
2138                            || !info->symbolic
2139                            || !h->def_regular))
2140                 {
2141                   outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2142                   outrel.r_addend = rel->r_addend;
2143                 }
2144               else
2145                 {
2146                   /* This symbol is local, or marked to become local.  */
2147                   if (r_type == R_X86_64_64)
2148                     {
2149                       relocate = TRUE;
2150                       outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2151                       outrel.r_addend = relocation + rel->r_addend;
2152                     }
2153                   else
2154                     {
2155                       long sindx;
2156
2157                       if (bfd_is_abs_section (sec))
2158                         sindx = 0;
2159                       else if (sec == NULL || sec->owner == NULL)
2160                         {
2161                           bfd_set_error (bfd_error_bad_value);
2162                           return FALSE;
2163                         }
2164                       else
2165                         {
2166                           asection *osec;
2167
2168                           osec = sec->output_section;
2169                           sindx = elf_section_data (osec)->dynindx;
2170                           BFD_ASSERT (sindx > 0);
2171                         }
2172
2173                       outrel.r_info = ELF64_R_INFO (sindx, r_type);
2174                       outrel.r_addend = relocation + rel->r_addend;
2175                     }
2176                 }
2177
2178               sreloc = elf_section_data (input_section)->sreloc;
2179               if (sreloc == NULL)
2180                 abort ();
2181
2182               loc = sreloc->contents;
2183               loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2184               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2185
2186               /* If this reloc is against an external symbol, we do
2187                  not want to fiddle with the addend.  Otherwise, we
2188                  need to include the symbol value so that it becomes
2189                  an addend for the dynamic reloc.  */
2190               if (! relocate)
2191                 continue;
2192             }
2193
2194           break;
2195
2196         case R_X86_64_TLSGD:
2197         case R_X86_64_GOTTPOFF:
2198           r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2199           tls_type = GOT_UNKNOWN;
2200           if (h == NULL && local_got_offsets)
2201             tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2202           else if (h != NULL)
2203             {
2204               tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2205               if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2206                 r_type = R_X86_64_TPOFF32;
2207             }
2208           if (r_type == R_X86_64_TLSGD)
2209             {
2210               if (tls_type == GOT_TLS_IE)
2211                 r_type = R_X86_64_GOTTPOFF;
2212             }
2213
2214           if (r_type == R_X86_64_TPOFF32)
2215             {
2216               BFD_ASSERT (! unresolved_reloc);
2217               if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2218                 {
2219                   unsigned int i;
2220                   static unsigned char tlsgd[8]
2221                     = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2222
2223                   /* GD->LE transition.
2224                      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2225                      .word 0x6666; rex64; call __tls_get_addr@plt
2226                      Change it into:
2227                      movq %fs:0, %rax
2228                      leaq foo@tpoff(%rax), %rax */
2229                   BFD_ASSERT (rel->r_offset >= 4);
2230                   for (i = 0; i < 4; i++)
2231                     BFD_ASSERT (bfd_get_8 (input_bfd,
2232                                            contents + rel->r_offset - 4 + i)
2233                                 == tlsgd[i]);
2234                   BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2235                   for (i = 0; i < 4; i++)
2236                     BFD_ASSERT (bfd_get_8 (input_bfd,
2237                                            contents + rel->r_offset + 4 + i)
2238                                 == tlsgd[i+4]);
2239                   BFD_ASSERT (rel + 1 < relend);
2240                   BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2241                   memcpy (contents + rel->r_offset - 4,
2242                           "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2243                           16);
2244                   bfd_put_32 (output_bfd, tpoff (info, relocation),
2245                               contents + rel->r_offset + 8);
2246                   /* Skip R_X86_64_PLT32.  */
2247                   rel++;
2248                   continue;
2249                 }
2250               else
2251                 {
2252                   unsigned int val, type, reg;
2253
2254                   /* IE->LE transition:
2255                      Originally it can be one of:
2256                      movq foo@gottpoff(%rip), %reg
2257                      addq foo@gottpoff(%rip), %reg
2258                      We change it into:
2259                      movq $foo, %reg
2260                      leaq foo(%reg), %reg
2261                      addq $foo, %reg.  */
2262                   BFD_ASSERT (rel->r_offset >= 3);
2263                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2264                   BFD_ASSERT (val == 0x48 || val == 0x4c);
2265                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2266                   BFD_ASSERT (type == 0x8b || type == 0x03);
2267                   reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2268                   BFD_ASSERT ((reg & 0xc7) == 5);
2269                   reg >>= 3;
2270                   BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2271                   if (type == 0x8b)
2272                     {
2273                       /* movq */
2274                       if (val == 0x4c)
2275                         bfd_put_8 (output_bfd, 0x49,
2276                                    contents + rel->r_offset - 3);
2277                       bfd_put_8 (output_bfd, 0xc7,
2278                                  contents + rel->r_offset - 2);
2279                       bfd_put_8 (output_bfd, 0xc0 | reg,
2280                                  contents + rel->r_offset - 1);
2281                     }
2282                   else if (reg == 4)
2283                     {
2284                       /* addq -> addq - addressing with %rsp/%r12 is
2285                          special  */
2286                       if (val == 0x4c)
2287                         bfd_put_8 (output_bfd, 0x49,
2288                                    contents + rel->r_offset - 3);
2289                       bfd_put_8 (output_bfd, 0x81,
2290                                  contents + rel->r_offset - 2);
2291                       bfd_put_8 (output_bfd, 0xc0 | reg,
2292                                  contents + rel->r_offset - 1);
2293                     }
2294                   else
2295                     {
2296                       /* addq -> leaq */
2297                       if (val == 0x4c)
2298                         bfd_put_8 (output_bfd, 0x4d,
2299                                    contents + rel->r_offset - 3);
2300                       bfd_put_8 (output_bfd, 0x8d,
2301                                  contents + rel->r_offset - 2);
2302                       bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2303                                  contents + rel->r_offset - 1);
2304                     }
2305                   bfd_put_32 (output_bfd, tpoff (info, relocation),
2306                               contents + rel->r_offset);
2307                   continue;
2308                 }
2309             }
2310
2311           if (htab->sgot == NULL)
2312             abort ();
2313
2314           if (h != NULL)
2315             off = h->got.offset;
2316           else
2317             {
2318               if (local_got_offsets == NULL)
2319                 abort ();
2320
2321               off = local_got_offsets[r_symndx];
2322             }
2323
2324           if ((off & 1) != 0)
2325             off &= ~1;
2326           else
2327             {
2328               Elf_Internal_Rela outrel;
2329               bfd_byte *loc;
2330               int dr_type, indx;
2331
2332               if (htab->srelgot == NULL)
2333                 abort ();
2334
2335               outrel.r_offset = (htab->sgot->output_section->vma
2336                                  + htab->sgot->output_offset + off);
2337
2338               indx = h && h->dynindx != -1 ? h->dynindx : 0;
2339               if (r_type == R_X86_64_TLSGD)
2340                 dr_type = R_X86_64_DTPMOD64;
2341               else
2342                 dr_type = R_X86_64_TPOFF64;
2343
2344               bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2345               outrel.r_addend = 0;
2346               if (dr_type == R_X86_64_TPOFF64 && indx == 0)
2347                 outrel.r_addend = relocation - dtpoff_base (info);
2348               outrel.r_info = ELF64_R_INFO (indx, dr_type);
2349
2350               loc = htab->srelgot->contents;
2351               loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2352               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2353
2354               if (r_type == R_X86_64_TLSGD)
2355                 {
2356                   if (indx == 0)
2357                     {
2358                       BFD_ASSERT (! unresolved_reloc);
2359                       bfd_put_64 (output_bfd,
2360                                   relocation - dtpoff_base (info),
2361                                   htab->sgot->contents + off + GOT_ENTRY_SIZE);
2362                     }
2363                   else
2364                     {
2365                       bfd_put_64 (output_bfd, 0,
2366                                   htab->sgot->contents + off + GOT_ENTRY_SIZE);
2367                       outrel.r_info = ELF64_R_INFO (indx,
2368                                                     R_X86_64_DTPOFF64);
2369                       outrel.r_offset += GOT_ENTRY_SIZE;
2370                       htab->srelgot->reloc_count++;
2371                       loc += sizeof (Elf64_External_Rela);
2372                       bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2373                     }
2374                 }
2375
2376               if (h != NULL)
2377                 h->got.offset |= 1;
2378               else
2379                 local_got_offsets[r_symndx] |= 1;
2380             }
2381
2382           if (off >= (bfd_vma) -2)
2383             abort ();
2384           if (r_type == ELF64_R_TYPE (rel->r_info))
2385             {
2386               relocation = htab->sgot->output_section->vma
2387                            + htab->sgot->output_offset + off;
2388               unresolved_reloc = FALSE;
2389             }
2390           else
2391             {
2392               unsigned int i;
2393               static unsigned char tlsgd[8]
2394                 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2395
2396               /* GD->IE transition.
2397                  .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2398                  .word 0x6666; rex64; call __tls_get_addr@plt
2399                  Change it into:
2400                  movq %fs:0, %rax
2401                  addq foo@gottpoff(%rip), %rax */
2402               BFD_ASSERT (rel->r_offset >= 4);
2403               for (i = 0; i < 4; i++)
2404                 BFD_ASSERT (bfd_get_8 (input_bfd,
2405                                        contents + rel->r_offset - 4 + i)
2406                             == tlsgd[i]);
2407               BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2408               for (i = 0; i < 4; i++)
2409                 BFD_ASSERT (bfd_get_8 (input_bfd,
2410                                        contents + rel->r_offset + 4 + i)
2411                             == tlsgd[i+4]);
2412               BFD_ASSERT (rel + 1 < relend);
2413               BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2414               memcpy (contents + rel->r_offset - 4,
2415                       "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2416                       16);
2417
2418               relocation = (htab->sgot->output_section->vma
2419                             + htab->sgot->output_offset + off
2420                             - rel->r_offset
2421                             - input_section->output_section->vma
2422                             - input_section->output_offset
2423                             - 12);
2424               bfd_put_32 (output_bfd, relocation,
2425                           contents + rel->r_offset + 8);
2426               /* Skip R_X86_64_PLT32.  */
2427               rel++;
2428               continue;
2429             }
2430           break;
2431
2432         case R_X86_64_TLSLD:
2433           if (! info->shared)
2434             {
2435               /* LD->LE transition:
2436                  Ensure it is:
2437                  leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2438                  We change it into:
2439                  .word 0x6666; .byte 0x66; movl %fs:0, %rax.  */
2440               BFD_ASSERT (rel->r_offset >= 3);
2441               BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2442                           == 0x48);
2443               BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2444                           == 0x8d);
2445               BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2446                           == 0x3d);
2447               BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2448               BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2449                           == 0xe8);
2450               BFD_ASSERT (rel + 1 < relend);
2451               BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2452               memcpy (contents + rel->r_offset - 3,
2453                       "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2454               /* Skip R_X86_64_PLT32.  */
2455               rel++;
2456               continue;
2457             }
2458
2459           if (htab->sgot == NULL)
2460             abort ();
2461
2462           off = htab->tls_ld_got.offset;
2463           if (off & 1)
2464             off &= ~1;
2465           else
2466             {
2467               Elf_Internal_Rela outrel;
2468               bfd_byte *loc;
2469
2470               if (htab->srelgot == NULL)
2471                 abort ();
2472
2473               outrel.r_offset = (htab->sgot->output_section->vma
2474                                  + htab->sgot->output_offset + off);
2475
2476               bfd_put_64 (output_bfd, 0,
2477                           htab->sgot->contents + off);
2478               bfd_put_64 (output_bfd, 0,
2479                           htab->sgot->contents + off + GOT_ENTRY_SIZE);
2480               outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2481               outrel.r_addend = 0;
2482               loc = htab->srelgot->contents;
2483               loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2484               bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2485               htab->tls_ld_got.offset |= 1;
2486             }
2487           relocation = htab->sgot->output_section->vma
2488                        + htab->sgot->output_offset + off;
2489           unresolved_reloc = FALSE;
2490           break;
2491
2492         case R_X86_64_DTPOFF32:
2493           if (info->shared || (input_section->flags & SEC_CODE) == 0)
2494             relocation -= dtpoff_base (info);
2495           else
2496             relocation = tpoff (info, relocation);
2497           break;
2498
2499         case R_X86_64_TPOFF32:
2500           BFD_ASSERT (! info->shared);
2501           relocation = tpoff (info, relocation);
2502           break;
2503
2504         default:
2505           break;
2506         }
2507
2508       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2509          because such sections are not SEC_ALLOC and thus ld.so will
2510          not process them.  */
2511       if (unresolved_reloc
2512           && !((input_section->flags & SEC_DEBUGGING) != 0
2513                && h->def_dynamic))
2514         (*_bfd_error_handler)
2515           (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2516            input_bfd,
2517            input_section,
2518            (long) rel->r_offset,
2519            h->root.root.string);
2520
2521       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2522                                     contents, rel->r_offset,
2523                                     relocation, rel->r_addend);
2524
2525       if (r != bfd_reloc_ok)
2526         {
2527           const char *name;
2528
2529           if (h != NULL)
2530             name = h->root.root.string;
2531           else
2532             {
2533               name = bfd_elf_string_from_elf_section (input_bfd,
2534                                                       symtab_hdr->sh_link,
2535                                                       sym->st_name);
2536               if (name == NULL)
2537                 return FALSE;
2538               if (*name == '\0')
2539                 name = bfd_section_name (input_bfd, sec);
2540             }
2541
2542           if (r == bfd_reloc_overflow)
2543             {
2544               if (h != NULL
2545                   && h->root.type == bfd_link_hash_undefweak
2546                   && howto->pc_relative)
2547                 /* Ignore reloc overflow on branches to undefweak syms.  */
2548                 continue;
2549
2550               if (! ((*info->callbacks->reloc_overflow)
2551                      (info, (h ? &h->root : NULL), name, howto->name,
2552                       (bfd_vma) 0, input_bfd, input_section,
2553                       rel->r_offset)))
2554                 return FALSE;
2555             }
2556           else
2557             {
2558               (*_bfd_error_handler)
2559                 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2560                  input_bfd, input_section,
2561                  (long) rel->r_offset, name, (int) r);
2562               return FALSE;
2563             }
2564         }
2565     }
2566
2567   return TRUE;
2568 }
2569
2570 /* Finish up dynamic symbol handling.  We set the contents of various
2571    dynamic sections here.  */
2572
2573 static bfd_boolean
2574 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
2575                                     struct bfd_link_info *info,
2576                                     struct elf_link_hash_entry *h,
2577                                     Elf_Internal_Sym *sym)
2578 {
2579   struct elf64_x86_64_link_hash_table *htab;
2580
2581   htab = elf64_x86_64_hash_table (info);
2582
2583   if (h->plt.offset != (bfd_vma) -1)
2584     {
2585       bfd_vma plt_index;
2586       bfd_vma got_offset;
2587       Elf_Internal_Rela rela;
2588       bfd_byte *loc;
2589
2590       /* This symbol has an entry in the procedure linkage table.  Set
2591          it up.  */
2592       if (h->dynindx == -1
2593           || htab->splt == NULL
2594           || htab->sgotplt == NULL
2595           || htab->srelplt == NULL)
2596         abort ();
2597
2598       /* Get the index in the procedure linkage table which
2599          corresponds to this symbol.  This is the index of this symbol
2600          in all the symbols for which we are making plt entries.  The
2601          first entry in the procedure linkage table is reserved.  */
2602       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2603
2604       /* Get the offset into the .got table of the entry that
2605          corresponds to this function.  Each .got entry is GOT_ENTRY_SIZE
2606          bytes. The first three are reserved for the dynamic linker.  */
2607       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2608
2609       /* Fill in the entry in the procedure linkage table.  */
2610       memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
2611               PLT_ENTRY_SIZE);
2612
2613       /* Insert the relocation positions of the plt section.  The magic
2614          numbers at the end of the statements are the positions of the
2615          relocations in the plt section.  */
2616       /* Put offset for jmp *name@GOTPCREL(%rip), since the
2617          instruction uses 6 bytes, subtract this value.  */
2618       bfd_put_32 (output_bfd,
2619                       (htab->sgotplt->output_section->vma
2620                        + htab->sgotplt->output_offset
2621                        + got_offset
2622                        - htab->splt->output_section->vma
2623                        - htab->splt->output_offset
2624                        - h->plt.offset
2625                        - 6),
2626                   htab->splt->contents + h->plt.offset + 2);
2627       /* Put relocation index.  */
2628       bfd_put_32 (output_bfd, plt_index,
2629                   htab->splt->contents + h->plt.offset + 7);
2630       /* Put offset for jmp .PLT0.  */
2631       bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
2632                   htab->splt->contents + h->plt.offset + 12);
2633
2634       /* Fill in the entry in the global offset table, initially this
2635          points to the pushq instruction in the PLT which is at offset 6.  */
2636       bfd_put_64 (output_bfd, (htab->splt->output_section->vma
2637                                + htab->splt->output_offset
2638                                + h->plt.offset + 6),
2639                   htab->sgotplt->contents + got_offset);
2640
2641       /* Fill in the entry in the .rela.plt section.  */
2642       rela.r_offset = (htab->sgotplt->output_section->vma
2643                        + htab->sgotplt->output_offset
2644                        + got_offset);
2645       rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
2646       rela.r_addend = 0;
2647       loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
2648       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2649
2650       if (!h->def_regular)
2651         {
2652           /* Mark the symbol as undefined, rather than as defined in
2653              the .plt section.  Leave the value if there were any
2654              relocations where pointer equality matters (this is a clue
2655              for the dynamic linker, to make function pointer
2656              comparisons work between an application and shared
2657              library), otherwise set it to zero.  If a function is only
2658              called from a binary, there is no need to slow down
2659              shared libraries because of that.  */
2660           sym->st_shndx = SHN_UNDEF;
2661           if (!h->pointer_equality_needed)
2662             sym->st_value = 0;
2663         }
2664     }
2665
2666   if (h->got.offset != (bfd_vma) -1
2667       && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD
2668       && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
2669     {
2670       Elf_Internal_Rela rela;
2671       bfd_byte *loc;
2672
2673       /* This symbol has an entry in the global offset table.  Set it
2674          up.  */
2675       if (htab->sgot == NULL || htab->srelgot == NULL)
2676         abort ();
2677
2678       rela.r_offset = (htab->sgot->output_section->vma
2679                        + htab->sgot->output_offset
2680                        + (h->got.offset &~ (bfd_vma) 1));
2681
2682       /* If this is a static link, or it is a -Bsymbolic link and the
2683          symbol is defined locally or was forced to be local because
2684          of a version file, we just want to emit a RELATIVE reloc.
2685          The entry in the global offset table will already have been
2686          initialized in the relocate_section function.  */
2687       if (info->shared
2688           && SYMBOL_REFERENCES_LOCAL (info, h))
2689         {
2690           BFD_ASSERT((h->got.offset & 1) != 0);
2691           rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2692           rela.r_addend = (h->root.u.def.value
2693                            + h->root.u.def.section->output_section->vma
2694                            + h->root.u.def.section->output_offset);
2695         }
2696       else
2697         {
2698           BFD_ASSERT((h->got.offset & 1) == 0);
2699           bfd_put_64 (output_bfd, (bfd_vma) 0,
2700                       htab->sgot->contents + h->got.offset);
2701           rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
2702           rela.r_addend = 0;
2703         }
2704
2705       loc = htab->srelgot->contents;
2706       loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2707       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2708     }
2709
2710   if (h->needs_copy)
2711     {
2712       Elf_Internal_Rela rela;
2713       bfd_byte *loc;
2714
2715       /* This symbol needs a copy reloc.  Set it up.  */
2716
2717       if (h->dynindx == -1
2718           || (h->root.type != bfd_link_hash_defined
2719               && h->root.type != bfd_link_hash_defweak)
2720           || htab->srelbss == NULL)
2721         abort ();
2722
2723       rela.r_offset = (h->root.u.def.value
2724                        + h->root.u.def.section->output_section->vma
2725                        + h->root.u.def.section->output_offset);
2726       rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
2727       rela.r_addend = 0;
2728       loc = htab->srelbss->contents;
2729       loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
2730       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2731     }
2732
2733   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
2734   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2735       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2736     sym->st_shndx = SHN_ABS;
2737
2738   return TRUE;
2739 }
2740
2741 /* Used to decide how to sort relocs in an optimal manner for the
2742    dynamic linker, before writing them out.  */
2743
2744 static enum elf_reloc_type_class
2745 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
2746 {
2747   switch ((int) ELF64_R_TYPE (rela->r_info))
2748     {
2749     case R_X86_64_RELATIVE:
2750       return reloc_class_relative;
2751     case R_X86_64_JUMP_SLOT:
2752       return reloc_class_plt;
2753     case R_X86_64_COPY:
2754       return reloc_class_copy;
2755     default:
2756       return reloc_class_normal;
2757     }
2758 }
2759
2760 /* Finish up the dynamic sections.  */
2761
2762 static bfd_boolean
2763 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
2764 {
2765   struct elf64_x86_64_link_hash_table *htab;
2766   bfd *dynobj;
2767   asection *sdyn;
2768
2769   htab = elf64_x86_64_hash_table (info);
2770   dynobj = htab->elf.dynobj;
2771   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2772
2773   if (htab->elf.dynamic_sections_created)
2774     {
2775       Elf64_External_Dyn *dyncon, *dynconend;
2776
2777       if (sdyn == NULL || htab->sgot == NULL)
2778         abort ();
2779
2780       dyncon = (Elf64_External_Dyn *) sdyn->contents;
2781       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2782       for (; dyncon < dynconend; dyncon++)
2783         {
2784           Elf_Internal_Dyn dyn;
2785           asection *s;
2786
2787           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2788
2789           switch (dyn.d_tag)
2790             {
2791             default:
2792               continue;
2793
2794             case DT_PLTGOT:
2795               s = htab->sgotplt;
2796               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2797               break;
2798
2799             case DT_JMPREL:
2800               dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2801               break;
2802
2803             case DT_PLTRELSZ:
2804               s = htab->srelplt->output_section;
2805               dyn.d_un.d_val = s->size;
2806               break;
2807
2808             case DT_RELASZ:
2809               /* The procedure linkage table relocs (DT_JMPREL) should
2810                  not be included in the overall relocs (DT_RELA).
2811                  Therefore, we override the DT_RELASZ entry here to
2812                  make it not include the JMPREL relocs.  Since the
2813                  linker script arranges for .rela.plt to follow all
2814                  other relocation sections, we don't have to worry
2815                  about changing the DT_RELA entry.  */
2816               if (htab->srelplt != NULL)
2817                 {
2818                   s = htab->srelplt->output_section;
2819                   dyn.d_un.d_val -= s->size;
2820                 }
2821               break;
2822             }
2823
2824           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2825         }
2826
2827       /* Fill in the special first entry in the procedure linkage table.  */
2828       if (htab->splt && htab->splt->size > 0)
2829         {
2830           /* Fill in the first entry in the procedure linkage table.  */
2831           memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
2832                   PLT_ENTRY_SIZE);
2833           /* Add offset for pushq GOT+8(%rip), since the instruction
2834              uses 6 bytes subtract this value.  */
2835           bfd_put_32 (output_bfd,
2836                       (htab->sgotplt->output_section->vma
2837                        + htab->sgotplt->output_offset
2838                        + 8
2839                        - htab->splt->output_section->vma
2840                        - htab->splt->output_offset
2841                        - 6),
2842                       htab->splt->contents + 2);
2843           /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2844              the end of the instruction.  */
2845           bfd_put_32 (output_bfd,
2846                       (htab->sgotplt->output_section->vma
2847                        + htab->sgotplt->output_offset
2848                        + 16
2849                        - htab->splt->output_section->vma
2850                        - htab->splt->output_offset
2851                        - 12),
2852                       htab->splt->contents + 8);
2853
2854           elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
2855             PLT_ENTRY_SIZE;
2856         }
2857     }
2858
2859   if (htab->sgotplt)
2860     {
2861       /* Fill in the first three entries in the global offset table.  */
2862       if (htab->sgotplt->size > 0)
2863         {
2864           /* Set the first entry in the global offset table to the address of
2865              the dynamic section.  */
2866           if (sdyn == NULL)
2867             bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
2868           else
2869             bfd_put_64 (output_bfd,
2870                         sdyn->output_section->vma + sdyn->output_offset,
2871                         htab->sgotplt->contents);
2872           /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
2873           bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
2874           bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
2875         }
2876
2877       elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
2878         GOT_ENTRY_SIZE;
2879     }
2880
2881   if (htab->sgot && htab->sgot->size > 0)
2882     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
2883       = GOT_ENTRY_SIZE;
2884
2885   return TRUE;
2886 }
2887
2888 /* Return address for Ith PLT stub in section PLT, for relocation REL
2889    or (bfd_vma) -1 if it should not be included.  */
2890
2891 static bfd_vma
2892 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
2893                           const arelent *rel ATTRIBUTE_UNUSED)
2894 {
2895   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
2896 }
2897
2898 /* Handle an x86-64 specific section when reading an object file.  This
2899    is called when elfcode.h finds a section with an unknown type.  */
2900
2901 static bfd_boolean
2902 elf64_x86_64_section_from_shdr (bfd *abfd,
2903                                 Elf_Internal_Shdr *hdr,
2904                                 const char *name,
2905                                 int shindex)
2906 {
2907   if (hdr->sh_type != SHT_X86_64_UNWIND)
2908     return FALSE;
2909
2910   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2911     return FALSE;
2912
2913   return TRUE;
2914 }
2915
2916 /* Hook called by the linker routine which adds symbols from an object
2917    file.  We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
2918    of .bss.  */
2919
2920 static bfd_boolean
2921 elf64_x86_64_add_symbol_hook (bfd *abfd,
2922                               struct bfd_link_info *info ATTRIBUTE_UNUSED,
2923                               Elf_Internal_Sym *sym,
2924                               const char **namep ATTRIBUTE_UNUSED,
2925                               flagword *flagsp ATTRIBUTE_UNUSED,
2926                               asection **secp, bfd_vma *valp)
2927 {
2928   asection *lcomm;
2929
2930   switch (sym->st_shndx)
2931     {
2932     case SHN_X86_64_LCOMMON:
2933       lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
2934       if (lcomm == NULL)
2935         {
2936           lcomm = bfd_make_section_with_flags (abfd,
2937                                                "LARGE_COMMON",
2938                                                (SEC_ALLOC
2939                                                 | SEC_IS_COMMON
2940                                                 | SEC_LINKER_CREATED));
2941           if (lcomm == NULL)
2942             return FALSE;
2943           elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
2944         }
2945       *secp = lcomm;
2946       *valp = sym->st_size;
2947       break;
2948     }
2949   return TRUE;
2950 }
2951
2952
2953 /* Given a BFD section, try to locate the corresponding ELF section
2954    index.  */
2955
2956 static bfd_boolean
2957 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
2958                                            asection *sec, int *index)
2959 {
2960   if (sec == &_bfd_elf_large_com_section)
2961     {
2962       *index = SHN_X86_64_LCOMMON;
2963       return TRUE;
2964     }
2965   return FALSE;
2966 }
2967
2968 /* Process a symbol.  */
2969
2970 static void
2971 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
2972                                 asymbol *asym)
2973 {
2974   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
2975
2976   switch (elfsym->internal_elf_sym.st_shndx)
2977     {
2978     case SHN_X86_64_LCOMMON:
2979       asym->section = &_bfd_elf_large_com_section;
2980       asym->value = elfsym->internal_elf_sym.st_size;
2981       /* Common symbol doesn't set BSF_GLOBAL.  */
2982       asym->flags &= ~BSF_GLOBAL;
2983       break;
2984     }
2985 }
2986
2987 static bfd_boolean
2988 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
2989 {
2990   return (sym->st_shndx == SHN_COMMON
2991           || sym->st_shndx == SHN_X86_64_LCOMMON);
2992 }
2993
2994 static unsigned int
2995 elf64_x86_64_common_section_index (asection *sec)
2996 {
2997   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
2998     return SHN_COMMON;
2999   else
3000     return SHN_X86_64_LCOMMON;
3001 }
3002
3003 static asection *
3004 elf64_x86_64_common_section (asection *sec)
3005 {
3006   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3007     return bfd_com_section_ptr;
3008   else
3009     return &_bfd_elf_large_com_section;
3010 }
3011
3012 static bfd_boolean
3013 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3014                            struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
3015                            struct elf_link_hash_entry *h,
3016                            Elf_Internal_Sym *sym,
3017                            asection **psec ATTRIBUTE_UNUSED,
3018                            bfd_vma *pvalue ATTRIBUTE_UNUSED,
3019                            unsigned int *pold_alignment ATTRIBUTE_UNUSED,
3020                            bfd_boolean *skip ATTRIBUTE_UNUSED,
3021                            bfd_boolean *override ATTRIBUTE_UNUSED,
3022                            bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
3023                            bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
3024                            bfd_boolean *newdef ATTRIBUTE_UNUSED,
3025                            bfd_boolean *newdyn,
3026                            bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
3027                            bfd_boolean *newweak ATTRIBUTE_UNUSED,
3028                            bfd *abfd ATTRIBUTE_UNUSED,
3029                            asection **sec,
3030                            bfd_boolean *olddef ATTRIBUTE_UNUSED,
3031                            bfd_boolean *olddyn,
3032                            bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
3033                            bfd_boolean *oldweak ATTRIBUTE_UNUSED,
3034                            bfd *oldbfd ATTRIBUTE_UNUSED,
3035                            asection **oldsec)
3036 {
3037   /* A normal common symbol and a large common symbol result in a
3038      normal common symbol.  If we see the normal symbol first, we
3039      do nothing since the first one will be used.  If we see the
3040      large common symbol first, we need to change the large common
3041      symbol to the normal common symbol.  */
3042   if (!*olddyn
3043       && h->root.type == bfd_link_hash_common
3044       && !*newdyn
3045       && bfd_is_com_section (*sec)
3046       && *oldsec != *sec
3047       && sym->st_shndx == SHN_COMMON
3048       && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
3049     {
3050       h->root.u.c.p->section = bfd_make_section_old_way (abfd,
3051                                                          "COMMON");
3052       h->root.u.c.p->section->flags = SEC_ALLOC;
3053     }
3054
3055   return TRUE;
3056 }
3057
3058 static int
3059 elf64_x86_64_additional_program_headers (bfd *abfd)
3060 {
3061   asection *s;
3062   int count = 0; 
3063
3064   /* Check to see if we need a large readonly segment.  */
3065   s = bfd_get_section_by_name (abfd, ".lrodata");
3066   if (s && (s->flags & SEC_LOAD))
3067     count++;
3068
3069   /* Check to see if we need a large data segment.  Since .lbss sections
3070      is placed right after the .bss section, there should be no need for
3071      a large data segment just because of .lbss.  */
3072   s = bfd_get_section_by_name (abfd, ".ldata");
3073   if (s && (s->flags & SEC_LOAD))
3074     count++;
3075
3076   return count;
3077 }
3078
3079 static const struct bfd_elf_special_section 
3080   elf64_x86_64_special_sections[]=
3081 {
3082   { ".gnu.linkonce.lb", 16, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3083   { ".gnu.linkonce.lr", 16, -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3084   { ".gnu.linkonce.lt", 16, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
3085   { ".lbss",    5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3086   { ".ldata",   6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3087   { ".lrodata", 8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3088   { NULL,       0,  0, 0,            0 }
3089 };
3090
3091 #define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_vec
3092 #define TARGET_LITTLE_NAME                  "elf64-x86-64"
3093 #define ELF_ARCH                            bfd_arch_i386
3094 #define ELF_MACHINE_CODE                    EM_X86_64
3095 #define ELF_MAXPAGESIZE                     0x100000
3096
3097 #define elf_backend_can_gc_sections         1
3098 #define elf_backend_can_refcount            1
3099 #define elf_backend_want_got_plt            1
3100 #define elf_backend_plt_readonly            1
3101 #define elf_backend_want_plt_sym            0
3102 #define elf_backend_got_header_size         (GOT_ENTRY_SIZE*3)
3103 #define elf_backend_rela_normal             1
3104
3105 #define elf_info_to_howto                   elf64_x86_64_info_to_howto
3106
3107 #define bfd_elf64_bfd_link_hash_table_create \
3108   elf64_x86_64_link_hash_table_create
3109 #define bfd_elf64_bfd_reloc_type_lookup     elf64_x86_64_reloc_type_lookup
3110
3111 #define elf_backend_adjust_dynamic_symbol   elf64_x86_64_adjust_dynamic_symbol
3112 #define elf_backend_check_relocs            elf64_x86_64_check_relocs
3113 #define elf_backend_copy_indirect_symbol    elf64_x86_64_copy_indirect_symbol
3114 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3115 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
3116 #define elf_backend_finish_dynamic_symbol   elf64_x86_64_finish_dynamic_symbol
3117 #define elf_backend_gc_mark_hook            elf64_x86_64_gc_mark_hook
3118 #define elf_backend_gc_sweep_hook           elf64_x86_64_gc_sweep_hook
3119 #define elf_backend_grok_prstatus           elf64_x86_64_grok_prstatus
3120 #define elf_backend_grok_psinfo             elf64_x86_64_grok_psinfo
3121 #define elf_backend_reloc_type_class        elf64_x86_64_reloc_type_class
3122 #define elf_backend_relocate_section        elf64_x86_64_relocate_section
3123 #define elf_backend_size_dynamic_sections   elf64_x86_64_size_dynamic_sections
3124 #define elf_backend_plt_sym_val             elf64_x86_64_plt_sym_val
3125 #define elf_backend_object_p                elf64_x86_64_elf_object_p
3126 #define bfd_elf64_mkobject                  elf64_x86_64_mkobject
3127
3128 #define elf_backend_section_from_shdr \
3129         elf64_x86_64_section_from_shdr
3130
3131 #define elf_backend_section_from_bfd_section \
3132   elf64_x86_64_elf_section_from_bfd_section
3133 #define elf_backend_add_symbol_hook \
3134   elf64_x86_64_add_symbol_hook
3135 #define elf_backend_symbol_processing \
3136   elf64_x86_64_symbol_processing
3137 #define elf_backend_common_section_index \
3138   elf64_x86_64_common_section_index
3139 #define elf_backend_common_section \
3140   elf64_x86_64_common_section
3141 #define elf_backend_common_definition \
3142   elf64_x86_64_common_definition
3143 #define elf_backend_merge_symbol \
3144   elf64_x86_64_merge_symbol
3145 #define elf_backend_special_sections \
3146   elf64_x86_64_special_sections
3147 #define elf_backend_additional_program_headers \
3148   elf64_x86_64_additional_program_headers
3149
3150 #include "elf64-target.h"