* elf-bfd.h (elf_backend_reloc_type_class): Pass in the entire
[external/binutils.git] / bfd / elf64-x86-64.c
1 /* X86-64 specific support for 64-bit ELF
2    Copyright 2000, 2001 Free Software Foundation, Inc.
3    Contributed by Jan Hubicka <jh@suse.cz>.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25
26 #include "elf/x86-64.h"
27
28 /* We use only the RELA entries.  */
29 #define USE_RELA
30
31 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
32 #define MINUS_ONE (~ (bfd_vma) 0)
33
34 /* The relocation "howto" table.  Order of fields:
35    type, size, bitsize, pc_relative, complain_on_overflow,
36    special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset.  */
37 static reloc_howto_type x86_64_elf_howto_table[] =
38 {
39   HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
40         bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0x00000000, 0x00000000,
41         false),
42   HOWTO(R_X86_64_64, 0, 4, 64, false, 0, complain_overflow_bitfield,
43         bfd_elf_generic_reloc, "R_X86_64_64", false, MINUS_ONE, MINUS_ONE,
44         false),
45   HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed,
46         bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff,
47         true),
48   HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed,
49         bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff,
50         false),
51   HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed,
52         bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff,
53         true),
54   HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield,
55         bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff,
56         false),
57   HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_bitfield,
58         bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, MINUS_ONE,
59         MINUS_ONE, false),
60   HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, false, 0, complain_overflow_bitfield,
61         bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, MINUS_ONE,
62         MINUS_ONE, false),
63   HOWTO(R_X86_64_RELATIVE, 0, 4, 64, false, 0, complain_overflow_bitfield,
64         bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, MINUS_ONE,
65         MINUS_ONE, false),
66   HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true,0 , complain_overflow_signed,
67         bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0xffffffff,
68         0xffffffff, true),
69   HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned,
70         bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff,
71         false),
72   HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed,
73         bfd_elf_generic_reloc, "R_X86_64_32S", false, 0xffffffff, 0xffffffff,
74         false),
75   HOWTO(R_X86_64_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
76         bfd_elf_generic_reloc, "R_X86_64_16", false, 0xffff, 0xffff, false),
77   HOWTO(R_X86_64_PC16,0, 1, 16, true, 0, complain_overflow_bitfield,
78         bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0xffff, 0xffff, true),
79   HOWTO(R_X86_64_8, 0, 0, 8, false, 0, complain_overflow_signed,
80         bfd_elf_generic_reloc, "R_X86_64_8", false, 0xff, 0xff, false),
81   HOWTO(R_X86_64_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
82         bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0xff, 0xff, true),
83
84 /* GNU extension to record C++ vtable hierarchy.  */
85   HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, false, 0, complain_overflow_dont,
86          NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
87
88 /* GNU extension to record C++ vtable member usage.  */
89   HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, false, 0, complain_overflow_dont,
90          _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
91          false)
92 };
93
94 /* Map BFD relocs to the x86_64 elf relocs.  */
95 struct elf_reloc_map
96 {
97   bfd_reloc_code_real_type bfd_reloc_val;
98   unsigned char elf_reloc_val;
99 };
100
101 static const struct elf_reloc_map x86_64_reloc_map[] =
102 {
103   { BFD_RELOC_NONE,             R_X86_64_NONE, },
104   { BFD_RELOC_64,               R_X86_64_64,   },
105   { BFD_RELOC_32_PCREL,         R_X86_64_PC32, },
106   { BFD_RELOC_X86_64_GOT32,     R_X86_64_GOT32,},
107   { BFD_RELOC_X86_64_PLT32,     R_X86_64_PLT32,},
108   { BFD_RELOC_X86_64_COPY,      R_X86_64_COPY, },
109   { BFD_RELOC_X86_64_GLOB_DAT,  R_X86_64_GLOB_DAT, },
110   { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
111   { BFD_RELOC_X86_64_RELATIVE,  R_X86_64_RELATIVE, },
112   { BFD_RELOC_X86_64_GOTPCREL,  R_X86_64_GOTPCREL, },
113   { BFD_RELOC_32,               R_X86_64_32, },
114   { BFD_RELOC_X86_64_32S,       R_X86_64_32S, },
115   { BFD_RELOC_16,               R_X86_64_16, },
116   { BFD_RELOC_16_PCREL,         R_X86_64_PC16, },
117   { BFD_RELOC_8,                R_X86_64_8, },
118   { BFD_RELOC_8_PCREL,          R_X86_64_PC8, },
119   { BFD_RELOC_VTABLE_INHERIT,   R_X86_64_GNU_VTINHERIT, },
120   { BFD_RELOC_VTABLE_ENTRY,     R_X86_64_GNU_VTENTRY, },
121 };
122
123 static reloc_howto_type *elf64_x86_64_reloc_type_lookup
124   PARAMS ((bfd *, bfd_reloc_code_real_type));
125 static void elf64_x86_64_info_to_howto
126   PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
127 static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create
128   PARAMS ((bfd *));
129 static boolean elf64_x86_64_elf_object_p PARAMS ((bfd *abfd));
130 static boolean elf64_x86_64_check_relocs
131   PARAMS ((bfd *, struct bfd_link_info *, asection *sec,
132            const Elf_Internal_Rela *));
133 static asection *elf64_x86_64_gc_mark_hook
134   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
135            struct elf_link_hash_entry *, Elf_Internal_Sym *));
136
137 static boolean elf64_x86_64_gc_sweep_hook
138   PARAMS ((bfd *, struct bfd_link_info *, asection *,
139            const Elf_Internal_Rela *));
140
141 static struct bfd_hash_entry *elf64_x86_64_link_hash_newfunc
142   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
143 static boolean elf64_x86_64_adjust_dynamic_symbol
144   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
145
146 static boolean elf64_x86_64_size_dynamic_sections
147   PARAMS ((bfd *, struct bfd_link_info *));
148 static boolean elf64_x86_64_relocate_section
149   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
150          Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
151 static boolean elf64_x86_64_finish_dynamic_symbol
152   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
153            Elf_Internal_Sym *sym));
154 static boolean elf64_x86_64_finish_dynamic_sections
155   PARAMS ((bfd *, struct bfd_link_info *));
156 static enum elf_reloc_type_class elf64_x86_64_reloc_type_class
157   PARAMS ((const Elf_Internal_Rela *));
158
159 /* Given a BFD reloc type, return a HOWTO structure.  */
160 static reloc_howto_type *
161 elf64_x86_64_reloc_type_lookup (abfd, code)
162      bfd *abfd ATTRIBUTE_UNUSED;
163      bfd_reloc_code_real_type code;
164 {
165   unsigned int i;
166   for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
167        i++)
168     {
169       if (x86_64_reloc_map[i].bfd_reloc_val == code)
170         return &x86_64_elf_howto_table[(int)
171                                        x86_64_reloc_map[i].elf_reloc_val];
172     }
173   return 0;
174 }
175
176 /* Given an x86_64 ELF reloc type, fill in an arelent structure.  */
177
178 static void
179 elf64_x86_64_info_to_howto (abfd, cache_ptr, dst)
180      bfd *abfd ATTRIBUTE_UNUSED;
181      arelent *cache_ptr;
182      Elf64_Internal_Rela *dst;
183 {
184   unsigned r_type, i;
185
186   r_type = ELF64_R_TYPE (dst->r_info);
187   if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
188     {
189       BFD_ASSERT (r_type <= (unsigned int) R_X86_64_PC8);
190       i = r_type;
191     }
192   else
193     {
194       BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
195       i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_PC8 - 1);
196     }
197   cache_ptr->howto = &x86_64_elf_howto_table[i];
198   BFD_ASSERT (r_type == cache_ptr->howto->type);
199 }
200 \f
201 /* Functions for the x86-64 ELF linker.  */
202
203 /* The name of the dynamic interpreter.  This is put in the .interp
204    section.  */
205
206 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
207
208 /* The size in bytes of an entry in the global offset table.  */
209
210 #define GOT_ENTRY_SIZE 8
211
212 /* The size in bytes of an entry in the procedure linkage table.  */
213
214 #define PLT_ENTRY_SIZE 16
215
216 /* The first entry in a procedure linkage table looks like this.  See the
217    SVR4 ABI i386 supplement and the x86-64 ABI to see how this works.  */
218
219 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
220 {
221   0xff, 0x35, 8, 0, 0, 0,       /* pushq GOT+8(%rip)  */
222   0xff, 0x25, 16, 0, 0, 0,      /* jmpq *GOT+16(%rip) */
223   0x90, 0x90, 0x90, 0x90        /* pad out to 16 bytes with nops.  */
224 };
225
226 /* Subsequent entries in a procedure linkage table look like this.  */
227
228 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
229 {
230   0xff, 0x25,   /* jmpq *name@GOTPC(%rip) */
231   0, 0, 0, 0,   /* replaced with offset to this symbol in .got.  */
232   0x68,         /* pushq immediate */
233   0, 0, 0, 0,   /* replaced with index into relocation table.  */
234   0xe9,         /* jmp relative */
235   0, 0, 0, 0    /* replaced with offset to start of .plt0.  */
236 };
237
238 /* The x86-64 linker needs to keep track of the number of relocs that
239    it decides to copy in check_relocs for each symbol.  This is so
240    that it can discard PC relative relocs if it doesn't need them when
241    linking with -Bsymbolic.  We store the information in a field
242    extending the regular ELF linker hash table.  */
243
244 /* This structure keeps track of the number of PC relative relocs we
245    have copied for a given symbol.  */
246
247 struct elf64_x86_64_pcrel_relocs_copied
248 {
249   /* Next section.  */
250   struct elf64_x86_64_pcrel_relocs_copied *next;
251   /* A section in dynobj.  */
252   asection *section;
253   /* Number of relocs copied in this section.  */
254   bfd_size_type count;
255 };
256
257 /* x86-64 ELF linker hash entry.  */
258
259 struct elf64_x86_64_link_hash_entry
260 {
261   struct elf_link_hash_entry root;
262
263   /* Number of PC relative relocs copied for this symbol.  */
264   struct elf64_x86_64_pcrel_relocs_copied *pcrel_relocs_copied;
265 };
266
267 /* x86-64  ELF linker hash table.  */
268
269 struct elf64_x86_64_link_hash_table
270 {
271   struct elf_link_hash_table root;
272 };
273
274 /* Declare this now that the above structures are defined.  */
275
276 static boolean elf64_x86_64_discard_copies
277   PARAMS ((struct elf64_x86_64_link_hash_entry *, PTR));
278
279 /* Traverse an x86-64 ELF linker hash table.  */
280
281 #define elf64_x86_64_link_hash_traverse(table, func, info)              \
282   (elf_link_hash_traverse                                               \
283    (&(table)->root,                                                     \
284     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
285     (info)))
286
287 /* Get the x86-64 ELF linker hash table from a link_info structure.  */
288
289 #define elf64_x86_64_hash_table(p) \
290   ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
291
292 /* Create an entry in an x86-64 ELF linker hash table.  */
293
294 static struct bfd_hash_entry *
295 elf64_x86_64_link_hash_newfunc (entry, table, string)
296      struct bfd_hash_entry *entry;
297      struct bfd_hash_table *table;
298      const char *string;
299 {
300   struct elf64_x86_64_link_hash_entry *ret =
301     (struct elf64_x86_64_link_hash_entry *) entry;
302
303   /* Allocate the structure if it has not already been allocated by a
304      subclass.  */
305   if (ret == (struct elf64_x86_64_link_hash_entry *) NULL)
306     ret = ((struct elf64_x86_64_link_hash_entry *)
307            bfd_hash_allocate (table,
308                               sizeof (struct elf64_x86_64_link_hash_entry)));
309   if (ret == (struct elf64_x86_64_link_hash_entry *) NULL)
310     return (struct bfd_hash_entry *) ret;
311
312   /* Call the allocation method of the superclass.  */
313   ret = ((struct elf64_x86_64_link_hash_entry *)
314          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
315                                      table, string));
316   if (ret != (struct elf64_x86_64_link_hash_entry *) NULL)
317     {
318       ret->pcrel_relocs_copied = NULL;
319     }
320
321   return (struct bfd_hash_entry *) ret;
322 }
323
324 /* Create an X86-64 ELF linker hash table.  */
325
326 static struct bfd_link_hash_table *
327 elf64_x86_64_link_hash_table_create (abfd)
328      bfd *abfd;
329 {
330   struct elf64_x86_64_link_hash_table *ret;
331   bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
332
333   ret = ((struct elf64_x86_64_link_hash_table *) bfd_alloc (abfd, amt));
334   if (ret == (struct elf64_x86_64_link_hash_table *) NULL)
335     return NULL;
336
337   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
338                                        elf64_x86_64_link_hash_newfunc))
339     {
340       bfd_release (abfd, ret);
341       return NULL;
342     }
343
344   return &ret->root.root;
345 }
346
347 static boolean
348 elf64_x86_64_elf_object_p (abfd)
349      bfd *abfd;
350 {
351   /* Set the right machine number for an x86-64 elf64 file.  */
352   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
353   return true;
354 }
355
356 /* Look through the relocs for a section during the first phase, and
357    allocate space in the global offset table or procedure linkage
358    table.  */
359
360 static boolean
361 elf64_x86_64_check_relocs (abfd, info, sec, relocs)
362      bfd *abfd;
363      struct bfd_link_info *info;
364      asection *sec;
365      const Elf_Internal_Rela *relocs;
366 {
367   bfd *dynobj;
368   Elf_Internal_Shdr *symtab_hdr;
369   struct elf_link_hash_entry **sym_hashes;
370   bfd_signed_vma *local_got_refcounts;
371   const Elf_Internal_Rela *rel;
372   const Elf_Internal_Rela *rel_end;
373   asection *sgot;
374   asection *srelgot;
375   asection *sreloc;
376
377   if (info->relocateable)
378     return true;
379
380   dynobj = elf_hash_table (info)->dynobj;
381   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
382   sym_hashes = elf_sym_hashes (abfd);
383   local_got_refcounts = elf_local_got_refcounts (abfd);
384
385   sgot = srelgot = sreloc = NULL;
386   rel_end = relocs + sec->reloc_count;
387   for (rel = relocs; rel < rel_end; rel++)
388     {
389       unsigned long r_symndx;
390       struct elf_link_hash_entry *h;
391
392       r_symndx = ELF64_R_SYM (rel->r_info);
393       if (r_symndx < symtab_hdr->sh_info)
394         h = NULL;
395       else
396         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
397
398       /* Some relocs require a global offset table.  */
399       if (dynobj == NULL)
400         {
401           switch (ELF64_R_TYPE (rel->r_info))
402             {
403             case R_X86_64_GOT32:
404             case R_X86_64_GOTPCREL:
405               elf_hash_table (info)->dynobj = dynobj = abfd;
406               if (! _bfd_elf_create_got_section (dynobj, info))
407                 return false;
408               break;
409             }
410         }
411
412       switch (ELF64_R_TYPE (rel->r_info))
413         {
414         case R_X86_64_GOTPCREL:
415         case R_X86_64_GOT32:
416           /* This symbol requires a global offset table entry.  */
417
418           if (sgot == NULL)
419             {
420               sgot = bfd_get_section_by_name (dynobj, ".got");
421               BFD_ASSERT (sgot != NULL);
422             }
423
424           if (srelgot == NULL && (h != NULL || info->shared))
425             {
426               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
427               if (srelgot == NULL)
428                 {
429                   srelgot = bfd_make_section (dynobj, ".rela.got");
430                   if (srelgot == NULL
431                       || ! bfd_set_section_flags (dynobj, srelgot,
432                                                   (SEC_ALLOC
433                                                    | SEC_LOAD
434                                                    | SEC_HAS_CONTENTS
435                                                    | SEC_IN_MEMORY
436                                                    | SEC_LINKER_CREATED
437                                                    | SEC_READONLY))
438                       || ! bfd_set_section_alignment (dynobj, srelgot, 3))
439                     return false;
440                 }
441             }
442
443           if (h != NULL)
444             {
445               if (h->got.refcount == -1)
446                 {
447                   h->got.refcount = 1;
448
449                   /* Make sure this symbol is output as a dynamic symbol.  */
450                   if (h->dynindx == -1)
451                     {
452                       if (! bfd_elf64_link_record_dynamic_symbol (info, h))
453                         return false;
454                     }
455
456                   sgot->_raw_size += GOT_ENTRY_SIZE;
457                   srelgot->_raw_size += sizeof (Elf64_External_Rela);
458                 }
459               else
460                 h->got.refcount += 1;
461             }
462           else
463             {
464               /* This is a global offset table entry for a local symbol.  */
465               if (local_got_refcounts == NULL)
466                 {
467                   bfd_size_type size;
468
469                   size = symtab_hdr->sh_info;
470                   size *= sizeof (bfd_signed_vma);
471                   local_got_refcounts = ((bfd_signed_vma *)
472                                          bfd_alloc (abfd, size));
473                   if (local_got_refcounts == NULL)
474                     return false;
475                   elf_local_got_refcounts (abfd) = local_got_refcounts;
476                   memset (local_got_refcounts, -1, (size_t) size);
477                 }
478               if (local_got_refcounts[r_symndx] == -1)
479                 {
480                   local_got_refcounts[r_symndx] = 1;
481
482                   sgot->_raw_size += GOT_ENTRY_SIZE;
483                   if (info->shared)
484                     {
485                       /* If we are generating a shared object, we need to
486                          output a R_X86_64_RELATIVE reloc so that the dynamic
487                          linker can adjust this GOT entry.  */
488                       srelgot->_raw_size += sizeof (Elf64_External_Rela);
489                     }
490                 }
491               else
492                 local_got_refcounts[r_symndx] += 1;
493             }
494           break;
495
496         case R_X86_64_PLT32:
497           /* This symbol requires a procedure linkage table entry.  We
498              actually build the entry in adjust_dynamic_symbol,
499              because this might be a case of linking PIC code which is
500              never referenced by a dynamic object, in which case we
501              don't need to generate a procedure linkage table entry
502              after all.  */
503
504           /* If this is a local symbol, we resolve it directly without
505              creating a procedure linkage table entry.  */
506           if (h == NULL)
507             continue;
508
509           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
510           if (h->plt.refcount == -1)
511             h->plt.refcount = 1;
512           else
513             h->plt.refcount += 1;
514           break;
515
516         case R_X86_64_8:
517         case R_X86_64_16:
518         case R_X86_64_32:
519         case R_X86_64_64:
520         case R_X86_64_32S:
521         case R_X86_64_PC32:
522           if (h != NULL)
523             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
524
525           /* If we are creating a shared library, and this is a reloc
526              against a global symbol, or a non PC relative reloc
527              against a local symbol, then we need to copy the reloc
528              into the shared library.  However, if we are linking with
529              -Bsymbolic, we do not need to copy a reloc against a
530              global symbol which is defined in an object we are
531              including in the link (i.e., DEF_REGULAR is set).  At
532              this point we have not seen all the input files, so it is
533              possible that DEF_REGULAR is not set now but will be set
534              later (it is never cleared).  We account for that
535              possibility below by storing information in the
536              pcrel_relocs_copied field of the hash table entry.
537              A similar situation occurs when creating shared libraries
538              and symbol visibility changes render the symbol local.  */
539           if (info->shared
540               && (sec->flags & SEC_ALLOC) != 0
541               && (((ELF64_R_TYPE (rel->r_info) != R_X86_64_PC8)
542                   && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC16)
543                   && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32))
544                   || (h != NULL
545                       && (! info->symbolic
546                           || (h->elf_link_hash_flags
547                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
548             {
549               /* When creating a shared object, we must copy these
550                  reloc types into the output file.  We create a reloc
551                  section in dynobj and make room for this reloc.  */
552               if (sreloc == NULL)
553                 {
554                   const char *name;
555
556                   name = (bfd_elf_string_from_elf_section
557                           (abfd,
558                            elf_elfheader (abfd)->e_shstrndx,
559                            elf_section_data (sec)->rel_hdr.sh_name));
560                   if (name == NULL)
561                     return false;
562
563                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
564                               && strcmp (bfd_get_section_name (abfd, sec),
565                                          name + 5) == 0);
566
567                   sreloc = bfd_get_section_by_name (dynobj, name);
568                   if (sreloc == NULL)
569                     {
570                       flagword flags;
571
572                       sreloc = bfd_make_section (dynobj, name);
573                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
574                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
575                       if ((sec->flags & SEC_ALLOC) != 0)
576                         flags |= SEC_ALLOC | SEC_LOAD;
577                       if (sreloc == NULL
578                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
579                           || ! bfd_set_section_alignment (dynobj, sreloc, 3))
580                         return false;
581                     }
582                   if (sec->flags & SEC_READONLY)
583                     info->flags |= DF_TEXTREL;
584                 }
585
586               sreloc->_raw_size += sizeof (Elf64_External_Rela);
587
588               /* If this is a global symbol, we count the number of PC
589                  relative relocations we have entered for this symbol,
590                  so that we can discard them later as necessary.  Note
591                  that this function is only called if we are using an
592                  elf64_x86_64 linker hash table, which means that h is
593                  really a pointer to an elf64_x86_64_link_hash_entry.  */
594               if (h != NULL
595                   && ((ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8)
596                       || (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16)
597                       || (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)))
598                 {
599                   struct elf64_x86_64_link_hash_entry *eh;
600                   struct elf64_x86_64_pcrel_relocs_copied *p;
601
602                   eh = (struct elf64_x86_64_link_hash_entry *) h;
603
604                   for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
605                     if (p->section == sreloc)
606                       break;
607
608                   if (p == NULL)
609                     {
610                       p = ((struct elf64_x86_64_pcrel_relocs_copied *)
611                            bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
612                       if (p == NULL)
613                         return false;
614                       p->next = eh->pcrel_relocs_copied;
615                       eh->pcrel_relocs_copied = p;
616                       p->section = sreloc;
617                       p->count = 0;
618                     }
619
620                   ++p->count;
621                 }
622             }
623           break;
624
625           /* This relocation describes the C++ object vtable hierarchy.
626              Reconstruct it for later use during GC.  */
627         case R_X86_64_GNU_VTINHERIT:
628           if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
629             return false;
630           break;
631
632           /* This relocation describes which C++ vtable entries are actually
633              used.  Record for later use during GC.  */
634         case R_X86_64_GNU_VTENTRY:
635           if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
636             return false;
637           break;
638         }
639     }
640
641   return true;
642 }
643
644 /* Return the section that should be marked against GC for a given
645    relocation.  */
646
647 static asection *
648 elf64_x86_64_gc_mark_hook (abfd, info, rel, h, sym)
649      bfd *abfd;
650      struct bfd_link_info *info ATTRIBUTE_UNUSED;
651      Elf_Internal_Rela *rel ATTRIBUTE_UNUSED;
652      struct elf_link_hash_entry *h;
653      Elf_Internal_Sym *sym;
654 {
655   if (h != NULL)
656     {
657       switch (ELF64_R_TYPE (rel->r_info))
658         {
659         case R_X86_64_GNU_VTINHERIT:
660         case R_X86_64_GNU_VTENTRY:
661           break;
662
663         default:
664           switch (h->root.type)
665             {
666             case bfd_link_hash_defined:
667             case bfd_link_hash_defweak:
668               return h->root.u.def.section;
669
670             case bfd_link_hash_common:
671               return h->root.u.c.p->section;
672
673             default:
674               break;
675             }
676         }
677     }
678   else
679     {
680       if (!(elf_bad_symtab (abfd)
681             && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
682           && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
683                 && sym->st_shndx != SHN_COMMON))
684         {
685           return bfd_section_from_elf_index (abfd, sym->st_shndx);
686         }
687     }
688
689   return NULL;
690 }
691
692 /* Update the got entry reference counts for the section being removed.  */
693
694 static boolean
695 elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)
696      bfd *abfd;
697      struct bfd_link_info *info ATTRIBUTE_UNUSED;
698      asection *sec;
699      const Elf_Internal_Rela *relocs;
700 {
701   Elf_Internal_Shdr *symtab_hdr;
702   struct elf_link_hash_entry **sym_hashes;
703   bfd_signed_vma *local_got_refcounts;
704   const Elf_Internal_Rela *rel, *relend;
705   unsigned long r_symndx;
706   struct elf_link_hash_entry *h;
707   bfd *dynobj;
708   asection *sgot;
709   asection *srelgot;
710
711   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
712   sym_hashes = elf_sym_hashes (abfd);
713   local_got_refcounts = elf_local_got_refcounts (abfd);
714
715   dynobj = elf_hash_table (info)->dynobj;
716   if (dynobj == NULL)
717     return true;
718
719   sgot = bfd_get_section_by_name (dynobj, ".got");
720   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
721
722   relend = relocs + sec->reloc_count;
723   for (rel = relocs; rel < relend; rel++)
724     switch (ELF64_R_TYPE (rel->r_info))
725       {
726       case R_X86_64_GOT32:
727       case R_X86_64_GOTPCREL:
728         r_symndx = ELF64_R_SYM (rel->r_info);
729         if (r_symndx >= symtab_hdr->sh_info)
730           {
731             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
732             if (h->got.refcount > 0)
733               {
734                 h->got.refcount -= 1;
735                 if (h->got.refcount == 0)
736                   {
737                     sgot->_raw_size -= GOT_ENTRY_SIZE;
738                     srelgot->_raw_size -= sizeof (Elf64_External_Rela);
739                   }
740               }
741           }
742         else if (local_got_refcounts != NULL)
743           {
744             if (local_got_refcounts[r_symndx] > 0)
745               {
746                 local_got_refcounts[r_symndx] -= 1;
747                 if (local_got_refcounts[r_symndx] == 0)
748                   {
749                     sgot->_raw_size -= GOT_ENTRY_SIZE;
750                     if (info->shared)
751                       srelgot->_raw_size -= sizeof (Elf64_External_Rela);
752                   }
753               }
754           }
755         break;
756
757       case R_X86_64_PLT32:
758         r_symndx = ELF64_R_SYM (rel->r_info);
759         if (r_symndx >= symtab_hdr->sh_info)
760           {
761             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
762             if (h->plt.refcount > 0)
763               h->plt.refcount -= 1;
764           }
765         break;
766
767       default:
768         break;
769       }
770
771   return true;
772 }
773
774 /* Adjust a symbol defined by a dynamic object and referenced by a
775    regular object.  The current definition is in some section of the
776    dynamic object, but we're not including those sections.  We have to
777    change the definition to something the rest of the link can
778    understand.  */
779
780 static boolean
781 elf64_x86_64_adjust_dynamic_symbol (info, h)
782      struct bfd_link_info *info;
783      struct elf_link_hash_entry *h;
784 {
785   bfd *dynobj;
786   asection *s;
787   unsigned int power_of_two;
788
789   dynobj = elf_hash_table (info)->dynobj;
790
791   /* Make sure we know what is going on here.  */
792   BFD_ASSERT (dynobj != NULL
793               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
794                   || h->weakdef != NULL
795                   || ((h->elf_link_hash_flags
796                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
797                       && (h->elf_link_hash_flags
798                           & ELF_LINK_HASH_REF_REGULAR) != 0
799                       && (h->elf_link_hash_flags
800                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
801
802   /* If this is a function, put it in the procedure linkage table.  We
803      will fill in the contents of the procedure linkage table later,
804      when we know the address of the .got section.  */
805   if (h->type == STT_FUNC
806       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
807     {
808       if ((! info->shared
809            && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
810            && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
811           || (info->shared && h->plt.refcount <= 0))
812         {
813           /* This case can occur if we saw a PLT32 reloc in an input
814              file, but the symbol was never referred to by a dynamic
815              object, or if all references were garbage collected.  In
816              such a case, we don't actually need to build a procedure
817              linkage table, and we can just do a PC32 reloc instead.  */
818           h->plt.offset = (bfd_vma) -1;
819           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
820           return true;
821         }
822
823       /* Make sure this symbol is output as a dynamic symbol.  */
824       if (h->dynindx == -1)
825         {
826           if (! bfd_elf64_link_record_dynamic_symbol (info, h))
827             return false;
828         }
829
830       s = bfd_get_section_by_name (dynobj, ".plt");
831       BFD_ASSERT (s != NULL);
832
833       /* If this is the first .plt entry, make room for the special
834          first entry.  */
835       if (s->_raw_size == 0)
836         s->_raw_size = PLT_ENTRY_SIZE;
837
838       /* If this symbol is not defined in a regular file, and we are
839          not generating a shared library, then set the symbol to this
840          location in the .plt.  This is required to make function
841          pointers compare as equal between the normal executable and
842          the shared library.  */
843       if (! info->shared
844           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
845         {
846           h->root.u.def.section = s;
847           h->root.u.def.value = s->_raw_size;
848         }
849
850       h->plt.offset = s->_raw_size;
851
852       /* Make room for this entry.  */
853       s->_raw_size += PLT_ENTRY_SIZE;
854
855       /* We also need to make an entry in the .got.plt section, which
856          will be placed in the .got section by the linker script.  */
857       s = bfd_get_section_by_name (dynobj, ".got.plt");
858       BFD_ASSERT (s != NULL);
859       s->_raw_size += GOT_ENTRY_SIZE;
860
861       /* We also need to make an entry in the .rela.plt section.  */
862       s = bfd_get_section_by_name (dynobj, ".rela.plt");
863       BFD_ASSERT (s != NULL);
864       s->_raw_size += sizeof (Elf64_External_Rela);
865
866       return true;
867     }
868
869   /* If this is a weak symbol, and there is a real definition, the
870      processor independent code will have arranged for us to see the
871      real definition first, and we can just use the same value.  */
872   if (h->weakdef != NULL)
873     {
874       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
875                   || h->weakdef->root.type == bfd_link_hash_defweak);
876       h->root.u.def.section = h->weakdef->root.u.def.section;
877       h->root.u.def.value = h->weakdef->root.u.def.value;
878       return true;
879     }
880
881   /* This is a reference to a symbol defined by a dynamic object which
882      is not a function.  */
883
884   /* If we are creating a shared library, we must presume that the
885      only references to the symbol are via the global offset table.
886      For such cases we need not do anything here; the relocations will
887      be handled correctly by relocate_section.  */
888   if (info->shared)
889     return true;
890
891   /* If there are no references to this symbol that do not use the
892      GOT, we don't need to generate a copy reloc.  */
893   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
894     return true;
895
896   /* We must allocate the symbol in our .dynbss section, which will
897      become part of the .bss section of the executable.  There will be
898      an entry for this symbol in the .dynsym section.  The dynamic
899      object will contain position independent code, so all references
900      from the dynamic object to this symbol will go through the global
901      offset table.  The dynamic linker will use the .dynsym entry to
902      determine the address it must put in the global offset table, so
903      both the dynamic object and the regular object will refer to the
904      same memory location for the variable.  */
905
906   s = bfd_get_section_by_name (dynobj, ".dynbss");
907   BFD_ASSERT (s != NULL);
908
909   /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
910      to copy the initial value out of the dynamic object and into the
911      runtime process image.  We need to remember the offset into the
912      .rela.bss section we are going to use.  */
913   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
914     {
915       asection *srel;
916
917       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
918       BFD_ASSERT (srel != NULL);
919       srel->_raw_size += sizeof (Elf64_External_Rela);
920       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
921     }
922
923   /* We need to figure out the alignment required for this symbol.  I
924      have no idea how ELF linkers handle this.  16-bytes is the size
925      of the largest type that requires hard alignment -- long double.  */
926   /* FIXME: This is VERY ugly. Should be fixed for all architectures using
927      this construct.  */
928   power_of_two = bfd_log2 (h->size);
929   if (power_of_two > 4)
930     power_of_two = 4;
931
932   /* Apply the required alignment.  */
933   s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
934   if (power_of_two > bfd_get_section_alignment (dynobj, s))
935     {
936       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
937         return false;
938     }
939
940   /* Define the symbol as being at this point in the section.  */
941   h->root.u.def.section = s;
942   h->root.u.def.value = s->_raw_size;
943
944   /* Increment the section size to make room for the symbol.  */
945   s->_raw_size += h->size;
946
947   return true;
948 }
949
950 /* Set the sizes of the dynamic sections.  */
951
952 static boolean
953 elf64_x86_64_size_dynamic_sections (output_bfd, info)
954      bfd *output_bfd ATTRIBUTE_UNUSED;
955      struct bfd_link_info *info;
956 {
957   bfd *dynobj;
958   asection *s;
959   boolean plt;
960   boolean relocs;
961
962   dynobj = elf_hash_table (info)->dynobj;
963   BFD_ASSERT (dynobj != NULL);
964
965   if (elf_hash_table (info)->dynamic_sections_created)
966     {
967       /* Set the contents of the .interp section to the interpreter.  */
968       if (! info->shared)
969         {
970           s = bfd_get_section_by_name (dynobj, ".interp");
971           BFD_ASSERT (s != NULL);
972           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
973           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
974         }
975     }
976   else
977     {
978       /* We may have created entries in the .rela.got section.
979          However, if we are not creating the dynamic sections, we will
980          not actually use these entries.  Reset the size of .rela.got,
981          which will cause it to get stripped from the output file
982          below.  */
983       s = bfd_get_section_by_name (dynobj, ".rela.got");
984       if (s != NULL)
985         s->_raw_size = 0;
986     }
987
988   /* If this is a -Bsymbolic shared link, then we need to discard all
989      PC relative relocs against symbols defined in a regular object.
990      We allocated space for them in the check_relocs routine, but we
991      will not fill them in in the relocate_section routine.  */
992   if (info->shared)
993     elf64_x86_64_link_hash_traverse (elf64_x86_64_hash_table (info),
994                                      elf64_x86_64_discard_copies,
995                                      (PTR) info);
996
997   /* The check_relocs and adjust_dynamic_symbol entry points have
998      determined the sizes of the various dynamic sections.  Allocate
999      memory for them.  */
1000   plt = relocs = false;
1001   for (s = dynobj->sections; s != NULL; s = s->next)
1002     {
1003       const char *name;
1004       boolean strip;
1005
1006       if ((s->flags & SEC_LINKER_CREATED) == 0)
1007         continue;
1008
1009       /* It's OK to base decisions on the section name, because none
1010          of the dynobj section names depend upon the input files.  */
1011       name = bfd_get_section_name (dynobj, s);
1012
1013       strip = false;
1014       if (strcmp (name, ".plt") == 0)
1015         {
1016           if (s->_raw_size == 0)
1017             {
1018               /* Strip this section if we don't need it; see the
1019                  comment below.  */
1020               strip = true;
1021             }
1022           else
1023             {
1024               /* Remember whether there is a PLT.  */
1025               plt = true;
1026             }
1027         }
1028       else if (strncmp (name, ".rela", 5) == 0)
1029         {
1030           if (s->_raw_size == 0)
1031             {
1032               /* If we don't need this section, strip it from the
1033                  output file.  This is mostly to handle .rela.bss and
1034                  .rela.plt.  We must create both sections in
1035                  create_dynamic_sections, because they must be created
1036                  before the linker maps input sections to output
1037                  sections.  The linker does that before
1038                  adjust_dynamic_symbol is called, and it is that
1039                  function which decides whether anything needs to go
1040                  into these sections.  */
1041               strip = true;
1042             }
1043           else
1044             {
1045               if (strcmp (name, ".rela.plt") != 0)
1046                 relocs = true;
1047
1048               /* We use the reloc_count field as a counter if we need
1049                  to copy relocs into the output file.  */
1050               s->reloc_count = 0;
1051             }
1052         }
1053       else if (strncmp (name, ".got", 4) != 0)
1054         {
1055           /* It's not one of our sections, so don't allocate space.  */
1056           continue;
1057         }
1058
1059       if (strip)
1060         {
1061           _bfd_strip_section_from_output (info, s);
1062           continue;
1063         }
1064
1065       /* Allocate memory for the section contents.  We use bfd_zalloc
1066          here in case unused entries are not reclaimed before the
1067          section's contents are written out.  This should not happen,
1068          but this way if it does, we get a R_X86_64_NONE reloc instead
1069          of garbage.  */
1070       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1071       if (s->contents == NULL && s->_raw_size != 0)
1072         return false;
1073     }
1074
1075   if (elf_hash_table (info)->dynamic_sections_created)
1076     {
1077       /* Add some entries to the .dynamic section.  We fill in the
1078          values later, in elf64_x86_64_finish_dynamic_sections, but we
1079          must add the entries now so that we get the correct size for
1080          the .dynamic section.  The DT_DEBUG entry is filled in by the
1081          dynamic linker and used by the debugger.  */
1082 #define add_dynamic_entry(TAG, VAL) \
1083   bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1084
1085       if (! info->shared)
1086         {
1087           if (!add_dynamic_entry (DT_DEBUG, 0))
1088             return false;
1089         }
1090
1091       if (plt)
1092         {
1093           if (!add_dynamic_entry (DT_PLTGOT, 0)
1094               || !add_dynamic_entry (DT_PLTRELSZ, 0)
1095               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1096               || !add_dynamic_entry (DT_JMPREL, 0))
1097             return false;
1098         }
1099
1100       if (relocs)
1101         {
1102           if (!add_dynamic_entry (DT_RELA, 0)
1103               || !add_dynamic_entry (DT_RELASZ, 0)
1104               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1105             return false;
1106         }
1107
1108       if ((info->flags & DF_TEXTREL) != 0)
1109         {
1110           if (!add_dynamic_entry (DT_TEXTREL, 0))
1111             return false;
1112         }
1113     }
1114 #undef add_dynamic_entry
1115
1116   return true;
1117 }
1118
1119 /* This function is called via elf64_x86_64_link_hash_traverse if we are
1120    creating a shared object.  In the -Bsymbolic case, it discards the
1121    space allocated to copy PC relative relocs against symbols which
1122    are defined in regular objects.  For the normal non-symbolic case,
1123    we also discard space for relocs that have become local due to
1124    symbol visibility changes.  We allocated space for them in the
1125    check_relocs routine, but we won't fill them in in the
1126    relocate_section routine.  */
1127
1128 static boolean
1129 elf64_x86_64_discard_copies (h, inf)
1130      struct elf64_x86_64_link_hash_entry *h;
1131      PTR inf;
1132 {
1133   struct elf64_x86_64_pcrel_relocs_copied *s;
1134   struct bfd_link_info *info = (struct bfd_link_info *) inf;
1135
1136   /* If a symbol has been forced local or we have found a regular
1137      definition for the symbolic link case, then we won't be needing
1138      any relocs.  */
1139   if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1140       && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1141           || info->symbolic))
1142     {
1143       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1144         s->section->_raw_size -= s->count * sizeof (Elf64_External_Rela);
1145     }
1146
1147   return true;
1148 }
1149
1150 /* Relocate an x86_64 ELF section.  */
1151
1152 static boolean
1153 elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
1154                                contents, relocs, local_syms, local_sections)
1155      bfd *output_bfd;
1156      struct bfd_link_info *info;
1157      bfd *input_bfd;
1158      asection *input_section;
1159      bfd_byte *contents;
1160      Elf_Internal_Rela *relocs;
1161      Elf_Internal_Sym *local_syms;
1162      asection **local_sections;
1163 {
1164   bfd *dynobj;
1165   Elf_Internal_Shdr *symtab_hdr;
1166   struct elf_link_hash_entry **sym_hashes;
1167   bfd_vma *local_got_offsets;
1168   asection *sgot;
1169   asection *splt;
1170   asection *sreloc;
1171   Elf_Internal_Rela *rela;
1172   Elf_Internal_Rela *relend;
1173
1174   dynobj = elf_hash_table (info)->dynobj;
1175   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1176   sym_hashes = elf_sym_hashes (input_bfd);
1177   local_got_offsets = elf_local_got_offsets (input_bfd);
1178
1179   sreloc = splt = sgot = NULL;
1180   if (dynobj != NULL)
1181     {
1182       splt = bfd_get_section_by_name (dynobj, ".plt");
1183       sgot = bfd_get_section_by_name (dynobj, ".got");
1184     }
1185
1186   rela = relocs;
1187   relend = relocs + input_section->reloc_count;
1188   for (; rela < relend; rela++)
1189     {
1190       int r_type;
1191       reloc_howto_type *howto;
1192       unsigned long r_symndx;
1193       struct elf_link_hash_entry *h;
1194       Elf_Internal_Sym *sym;
1195       asection *sec;
1196       bfd_vma relocation;
1197       bfd_reloc_status_type r;
1198       unsigned int indx;
1199
1200       r_type = ELF64_R_TYPE (rela->r_info);
1201       if (r_type == (int) R_X86_64_GNU_VTINHERIT
1202           || r_type == (int) R_X86_64_GNU_VTENTRY)
1203         continue;
1204
1205       if ((indx = (unsigned) r_type) >= R_X86_64_max)
1206         {
1207           bfd_set_error (bfd_error_bad_value);
1208           return false;
1209         }
1210       howto = x86_64_elf_howto_table + indx;
1211
1212       r_symndx = ELF64_R_SYM (rela->r_info);
1213
1214       if (info->relocateable)
1215         {
1216           /* This is a relocateable link.  We don't have to change
1217              anything, unless the reloc is against a section symbol,
1218              in which case we have to adjust according to where the
1219              section symbol winds up in the output section.  */
1220           if (r_symndx < symtab_hdr->sh_info)
1221             {
1222               sym = local_syms + r_symndx;
1223               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1224                 {
1225                   sec = local_sections[r_symndx];
1226                   rela->r_addend += sec->output_offset + sym->st_value;
1227                 }
1228             }
1229
1230           continue;
1231         }
1232
1233       /* This is a final link.  */
1234       h = NULL;
1235       sym = NULL;
1236       sec = NULL;
1237       if (r_symndx < symtab_hdr->sh_info)
1238         {
1239           sym = local_syms + r_symndx;
1240           sec = local_sections[r_symndx];
1241           relocation = (sec->output_section->vma
1242                         + sec->output_offset
1243                         + sym->st_value);
1244         }
1245       else
1246         {
1247           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1248           while (h->root.type == bfd_link_hash_indirect
1249                  || h->root.type == bfd_link_hash_warning)
1250             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1251           if (h->root.type == bfd_link_hash_defined
1252               || h->root.type == bfd_link_hash_defweak)
1253             {
1254               sec = h->root.u.def.section;
1255               if ((r_type == R_X86_64_PLT32
1256                    && splt != NULL
1257                    && h->plt.offset != (bfd_vma) -1)
1258                   || ((r_type == R_X86_64_GOT32 || r_type == R_X86_64_GOTPCREL)
1259                       && elf_hash_table (info)->dynamic_sections_created
1260                       && (!info->shared
1261                           || (! info->symbolic && h->dynindx != -1)
1262                           || (h->elf_link_hash_flags
1263                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
1264                   || (info->shared
1265                       && ((! info->symbolic && h->dynindx != -1)
1266                           || (h->elf_link_hash_flags
1267                               & ELF_LINK_HASH_DEF_REGULAR) == 0)
1268                       && (r_type == R_X86_64_8
1269                           || r_type == R_X86_64_16
1270                           || r_type == R_X86_64_32
1271                           || r_type == R_X86_64_64
1272                           || r_type == R_X86_64_PC8
1273                           || r_type == R_X86_64_PC16
1274                           || r_type == R_X86_64_PC32)
1275                       && ((input_section->flags & SEC_ALLOC) != 0
1276                           /* DWARF will emit R_X86_64_32 relocations in its
1277                              sections against symbols defined externally
1278                              in shared libraries.  We can't do anything
1279                              with them here.  */
1280                           || ((input_section->flags & SEC_DEBUGGING) != 0
1281                               && (h->elf_link_hash_flags
1282                                   & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
1283                 {
1284                   /* In these cases, we don't need the relocation
1285                      value.  We check specially because in some
1286                      obscure cases sec->output_section will be NULL.  */
1287                   relocation = 0;
1288                 }
1289               else if (sec->output_section == NULL)
1290                 {
1291                   (*_bfd_error_handler)
1292                     (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1293                      bfd_archive_filename (input_bfd), h->root.root.string,
1294                      bfd_get_section_name (input_bfd, input_section));
1295                   relocation = 0;
1296                 }
1297               else
1298                 relocation = (h->root.u.def.value
1299                               + sec->output_section->vma
1300                               + sec->output_offset);
1301             }
1302           else if (h->root.type == bfd_link_hash_undefweak)
1303             relocation = 0;
1304           else if (info->shared
1305                    && (!info->symbolic || info->allow_shlib_undefined)
1306                    && !info->no_undefined
1307                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1308             relocation = 0;
1309           else
1310             {
1311               if (! ((*info->callbacks->undefined_symbol)
1312                      (info, h->root.root.string, input_bfd,
1313                       input_section, rela->r_offset,
1314                       (!info->shared || info->no_undefined
1315                        || ELF_ST_VISIBILITY (h->other)))))
1316                 return false;
1317               relocation = 0;
1318             }
1319         }
1320
1321       /* When generating a shared object, the relocations handled here are
1322          copied into the output file to be resolved at run time.  */
1323       switch (r_type)
1324         {
1325         case R_X86_64_GOT32:
1326           /* Relocation is to the entry for this symbol in the global
1327              offset table.  */
1328         case R_X86_64_GOTPCREL:
1329           /* Use global offset table as symbol value.  */
1330           BFD_ASSERT (sgot != NULL);
1331
1332           if (h != NULL)
1333             {
1334               bfd_vma off = h->got.offset;
1335               BFD_ASSERT (off != (bfd_vma) -1);
1336
1337               if (! elf_hash_table (info)->dynamic_sections_created
1338                   || (info->shared
1339                       && (info->symbolic || h->dynindx == -1)
1340                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1341                 {
1342                   /* This is actually a static link, or it is a -Bsymbolic
1343                      link and the symbol is defined locally, or the symbol
1344                      was forced to be local because of a version file.  We
1345                      must initialize this entry in the global offset table.
1346                      Since the offset must always be a multiple of 8, we
1347                      use the least significant bit to record whether we
1348                      have initialized it already.
1349
1350                      When doing a dynamic link, we create a .rela.got
1351                      relocation entry to initialize the value.  This is
1352                      done in the finish_dynamic_symbol routine.  */
1353                   if ((off & 1) != 0)
1354                     off &= ~1;
1355                   else
1356                     {
1357                       bfd_put_64 (output_bfd, relocation,
1358                                   sgot->contents + off);
1359                       h->got.offset |= 1;
1360                     }
1361                 }
1362               if (r_type == R_X86_64_GOTPCREL)
1363                 relocation = sgot->output_section->vma + sgot->output_offset + off;
1364               else
1365                 relocation = sgot->output_offset + off;
1366             }
1367           else
1368             {
1369               bfd_vma off;
1370
1371               BFD_ASSERT (local_got_offsets != NULL
1372                           && local_got_offsets[r_symndx] != (bfd_vma) -1);
1373
1374               off = local_got_offsets[r_symndx];
1375
1376               /* The offset must always be a multiple of 8.  We use
1377                  the least significant bit to record whether we have
1378                  already generated the necessary reloc.  */
1379               if ((off & 1) != 0)
1380                 off &= ~1;
1381               else
1382                 {
1383                   bfd_put_64 (output_bfd, relocation, sgot->contents + off);
1384
1385                   if (info->shared)
1386                     {
1387                       asection *srelgot;
1388                       Elf_Internal_Rela outrel;
1389
1390                       /* We need to generate a R_X86_64_RELATIVE reloc
1391                          for the dynamic linker.  */
1392                       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1393                       BFD_ASSERT (srelgot != NULL);
1394
1395                       outrel.r_offset = (sgot->output_section->vma
1396                                          + sgot->output_offset
1397                                          + off);
1398                       outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1399                       outrel.r_addend = relocation;
1400                       bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1401                                                  (((Elf64_External_Rela *)
1402                                                    srelgot->contents)
1403                                                   + srelgot->reloc_count));
1404                       ++srelgot->reloc_count;
1405                     }
1406
1407                   local_got_offsets[r_symndx] |= 1;
1408                 }
1409
1410               if (r_type == R_X86_64_GOTPCREL)
1411                 relocation = sgot->output_section->vma + sgot->output_offset + off;
1412               else
1413                 relocation = sgot->output_offset + off;
1414             }
1415
1416           break;
1417
1418         case R_X86_64_PLT32:
1419           /* Relocation is to the entry for this symbol in the
1420              procedure linkage table.  */
1421
1422           /* Resolve a PLT32 reloc against a local symbol directly,
1423              without using the procedure linkage table.  */
1424           if (h == NULL)
1425             break;
1426
1427           if (h->plt.offset == (bfd_vma) -1 || splt == NULL)
1428             {
1429               /* We didn't make a PLT entry for this symbol.  This
1430                  happens when statically linking PIC code, or when
1431                  using -Bsymbolic.  */
1432               break;
1433             }
1434
1435           relocation = (splt->output_section->vma
1436                         + splt->output_offset
1437                         + h->plt.offset);
1438           break;
1439
1440         case R_X86_64_PC8:
1441         case R_X86_64_PC16:
1442         case R_X86_64_PC32:
1443           if (h == NULL || h->dynindx == -1
1444               || (info->symbolic
1445                   && h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1446             break;
1447           /* Fall through.  */
1448         case R_X86_64_8:
1449         case R_X86_64_16:
1450         case R_X86_64_32:
1451         case R_X86_64_64:
1452           /* FIXME: The ABI says the linker should make sure the value is
1453              the same when it's zeroextended to 64 bit.  */
1454           if (info->shared && (input_section->flags & SEC_ALLOC) != 0)
1455             {
1456               Elf_Internal_Rela outrel;
1457               boolean skip, relocate;
1458
1459               /* When generating a shared object, these relocations
1460                  are copied into the output file to be resolved at run
1461                  time.  */
1462
1463               if (sreloc == NULL)
1464                 {
1465                   const char *name;
1466
1467                   name = (bfd_elf_string_from_elf_section
1468                           (input_bfd,
1469                            elf_elfheader (input_bfd)->e_shstrndx,
1470                            elf_section_data (input_section)->rel_hdr.sh_name));
1471                   if (name == NULL)
1472                     return false;
1473
1474                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1475                               && strcmp (bfd_get_section_name (input_bfd,
1476                                                                input_section),
1477                                          name + 5) == 0);
1478
1479                   sreloc = bfd_get_section_by_name (dynobj, name);
1480                   BFD_ASSERT (sreloc != NULL);
1481                 }
1482
1483               skip = false;
1484
1485               if (elf_section_data (input_section)->stab_info == NULL)
1486                 outrel.r_offset = rela->r_offset;
1487               else
1488                 {
1489                   bfd_vma off;
1490
1491                   off = (_bfd_stab_section_offset
1492                          (output_bfd, &elf_hash_table (info)->stab_info,
1493                           input_section,
1494                           &elf_section_data (input_section)->stab_info,
1495                           rela->r_offset));
1496                   if (off == (bfd_vma) -1)
1497                     skip = true;
1498                   outrel.r_offset = off;
1499                 }
1500
1501               outrel.r_offset += (input_section->output_section->vma
1502                                   + input_section->output_offset);
1503
1504               if (skip)
1505                 {
1506                   memset (&outrel, 0, sizeof outrel);
1507                   relocate = false;
1508                 }
1509               /* h->dynindx may be -1 if this symbol was marked to
1510                  become local.  */
1511               else if (h != NULL
1512                        && ((! info->symbolic && h->dynindx != -1)
1513                            || (h->elf_link_hash_flags
1514                                & ELF_LINK_HASH_DEF_REGULAR) == 0))
1515                 {
1516                   BFD_ASSERT (h->dynindx != -1);
1517                   relocate = false;
1518                   outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1519                   outrel.r_addend = relocation + rela->r_addend;
1520                 }
1521               else
1522                 {
1523                   if (r_type == R_X86_64_64)
1524                     {
1525                       relocate = true;
1526                       outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1527                       outrel.r_addend = relocation + rela->r_addend;
1528                     }
1529                   else
1530                     {
1531                       long sindx;
1532
1533                       if (h == NULL)
1534                         sec = local_sections[r_symndx];
1535                       else
1536                         {
1537                           BFD_ASSERT (h->root.type == bfd_link_hash_defined
1538                                       || (h->root.type
1539                                           == bfd_link_hash_defweak));
1540                           sec = h->root.u.def.section;
1541                         }
1542                       if (sec != NULL && bfd_is_abs_section (sec))
1543                         sindx = 0;
1544                       else if (sec == NULL || sec->owner == NULL)
1545                         {
1546                           bfd_set_error (bfd_error_bad_value);
1547                           return false;
1548                         }
1549                       else
1550                         {
1551                           asection *osec;
1552
1553                           osec = sec->output_section;
1554                           sindx = elf_section_data (osec)->dynindx;
1555                           BFD_ASSERT (sindx > 0);
1556                         }
1557
1558                       relocate = false;
1559                       outrel.r_info = ELF64_R_INFO (sindx, r_type);
1560                       outrel.r_addend = relocation + rela->r_addend;
1561                     }
1562
1563                 }
1564
1565               bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1566                                         (((Elf64_External_Rela *)
1567                                           sreloc->contents)
1568                                          + sreloc->reloc_count));
1569               ++sreloc->reloc_count;
1570
1571               /* If this reloc is against an external symbol, we do
1572                  not want to fiddle with the addend.  Otherwise, we
1573                  need to include the symbol value so that it becomes
1574                  an addend for the dynamic reloc.  */
1575               if (! relocate)
1576                 continue;
1577             }
1578
1579           break;
1580
1581         default:
1582           break;
1583         }
1584
1585       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1586                                     contents, rela->r_offset,
1587                                     relocation, rela->r_addend);
1588
1589       if (r != bfd_reloc_ok)
1590         {
1591           switch (r)
1592             {
1593             default:
1594             case bfd_reloc_outofrange:
1595               abort ();
1596             case bfd_reloc_overflow:
1597               {
1598                 const char *name;
1599
1600                 if (h != NULL)
1601                   name = h->root.root.string;
1602                 else
1603                   {
1604                     name = bfd_elf_string_from_elf_section (input_bfd,
1605                                                             symtab_hdr->sh_link,
1606                                                             sym->st_name);
1607                     if (name == NULL)
1608                       return false;
1609                     if (*name == '\0')
1610                       name = bfd_section_name (input_bfd, sec);
1611                   }
1612                 if (! ((*info->callbacks->reloc_overflow)
1613                        (info, name, howto->name, (bfd_vma) 0,
1614                         input_bfd, input_section, rela->r_offset)))
1615                   return false;
1616               }
1617               break;
1618             }
1619         }
1620     }
1621
1622   return true;
1623 }
1624
1625 /* Finish up dynamic symbol handling.  We set the contents of various
1626    dynamic sections here.  */
1627
1628 static boolean
1629 elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym)
1630      bfd *output_bfd;
1631      struct bfd_link_info *info;
1632      struct elf_link_hash_entry *h;
1633      Elf_Internal_Sym *sym;
1634 {
1635   bfd *dynobj;
1636
1637   dynobj = elf_hash_table (info)->dynobj;
1638
1639   if (h->plt.offset != (bfd_vma) -1)
1640     {
1641       asection *splt;
1642       asection *sgot;
1643       asection *srela;
1644       bfd_vma plt_index;
1645       bfd_vma got_offset;
1646       Elf_Internal_Rela rela;
1647
1648       /* This symbol has an entry in the procedure linkage table.  Set
1649          it up.  */
1650
1651       BFD_ASSERT (h->dynindx != -1);
1652
1653       splt = bfd_get_section_by_name (dynobj, ".plt");
1654       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1655       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1656       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1657
1658       /* Get the index in the procedure linkage table which
1659          corresponds to this symbol.  This is the index of this symbol
1660          in all the symbols for which we are making plt entries.  The
1661          first entry in the procedure linkage table is reserved.  */
1662       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1663
1664       /* Get the offset into the .got table of the entry that
1665          corresponds to this function.  Each .got entry is GOT_ENTRY_SIZE
1666          bytes. The first three are reserved for the dynamic linker.  */
1667       got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
1668
1669       /* Fill in the entry in the procedure linkage table.  */
1670       memcpy (splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
1671               PLT_ENTRY_SIZE);
1672
1673       /* Insert the relocation positions of the plt section.  The magic
1674          numbers at the end of the statements are the positions of the
1675          relocations in the plt section.  */
1676       /* Put offset for jmp *name@GOTPCREL(%rip), since the
1677          instruction uses 6 bytes, subtract this value.  */
1678       bfd_put_32 (output_bfd,
1679                       (sgot->output_section->vma
1680                        + sgot->output_offset
1681                        + got_offset
1682                        - splt->output_section->vma
1683                        - splt->output_offset
1684                        - h->plt.offset
1685                        - 6),
1686                   splt->contents + h->plt.offset + 2);
1687       /* Put relocation index.  */
1688       bfd_put_32 (output_bfd, plt_index,
1689                   splt->contents + h->plt.offset + 7);
1690       /* Put offset for jmp .PLT0.  */
1691       bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
1692                   splt->contents + h->plt.offset + 12);
1693
1694       /* Fill in the entry in the global offset table, initially this
1695          points to the pushq instruction in the PLT which is at offset 6.  */
1696       bfd_put_64 (output_bfd, (splt->output_section->vma + splt->output_offset
1697                                + h->plt.offset + 6),
1698                   sgot->contents + got_offset);
1699
1700       /* Fill in the entry in the .rela.plt section.  */
1701       rela.r_offset = (sgot->output_section->vma
1702                        + sgot->output_offset
1703                        + got_offset);
1704       rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
1705       rela.r_addend = 0;
1706       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1707                                  ((Elf64_External_Rela *) srela->contents
1708                                   + plt_index));
1709
1710       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1711         {
1712           /* Mark the symbol as undefined, rather than as defined in
1713              the .plt section.  Leave the value alone.  */
1714           sym->st_shndx = SHN_UNDEF;
1715           /* If the symbol is weak, we do need to clear the value.
1716              Otherwise, the PLT entry would provide a definition for
1717              the symbol even if the symbol wasn't defined anywhere,
1718              and so the symbol would never be NULL.  */
1719           if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1720               == 0)
1721             sym->st_value = 0;
1722         }
1723     }
1724
1725   if (h->got.offset != (bfd_vma) -1)
1726     {
1727       asection *sgot;
1728       asection *srela;
1729       Elf_Internal_Rela rela;
1730
1731       /* This symbol has an entry in the global offset table.  Set it
1732          up.  */
1733
1734       sgot = bfd_get_section_by_name (dynobj, ".got");
1735       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1736       BFD_ASSERT (sgot != NULL && srela != NULL);
1737
1738       rela.r_offset = (sgot->output_section->vma
1739                        + sgot->output_offset
1740                        + (h->got.offset &~ (bfd_vma) 1));
1741
1742       /* If this is a static link, or it is a -Bsymbolic link and the
1743          symbol is defined locally or was forced to be local because
1744          of a version file, we just want to emit a RELATIVE reloc.
1745          The entry in the global offset table will already have been
1746          initialized in the relocate_section function.  */
1747       if (! elf_hash_table (info)->dynamic_sections_created
1748           || (info->shared
1749               && (info->symbolic || h->dynindx == -1)
1750               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1751         {
1752           BFD_ASSERT((h->got.offset & 1) != 0);
1753           rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1754           rela.r_addend = (h->root.u.def.value
1755                            + h->root.u.def.section->output_section->vma
1756                            + h->root.u.def.section->output_offset);
1757         }
1758       else
1759         {
1760           BFD_ASSERT((h->got.offset & 1) == 0);
1761           bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1762           rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
1763           rela.r_addend = 0;
1764         }
1765
1766       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1767                                  ((Elf64_External_Rela *) srela->contents
1768                                   + srela->reloc_count));
1769       ++srela->reloc_count;
1770     }
1771
1772   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1773     {
1774       asection *s;
1775       Elf_Internal_Rela rela;
1776
1777       /* This symbol needs a copy reloc.  Set it up.  */
1778
1779       BFD_ASSERT (h->dynindx != -1
1780                   && (h->root.type == bfd_link_hash_defined
1781                       || h->root.type == bfd_link_hash_defweak));
1782
1783       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1784                                    ".rela.bss");
1785       BFD_ASSERT (s != NULL);
1786
1787       rela.r_offset = (h->root.u.def.value
1788                        + h->root.u.def.section->output_section->vma
1789                        + h->root.u.def.section->output_offset);
1790       rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
1791       rela.r_addend = 0;
1792       bfd_elf64_swap_reloca_out (output_bfd, &rela,
1793                                  ((Elf64_External_Rela *) s->contents
1794                                   + s->reloc_count));
1795       ++s->reloc_count;
1796     }
1797
1798   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
1799   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1800       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1801     sym->st_shndx = SHN_ABS;
1802
1803   return true;
1804 }
1805
1806 /* Finish up the dynamic sections.  */
1807
1808 static boolean
1809 elf64_x86_64_finish_dynamic_sections (output_bfd, info)
1810      bfd *output_bfd;
1811      struct bfd_link_info *info;
1812 {
1813   bfd *dynobj;
1814   asection *sdyn;
1815   asection *sgot;
1816
1817   dynobj = elf_hash_table (info)->dynobj;
1818
1819   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1820   BFD_ASSERT (sgot != NULL);
1821   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1822
1823   if (elf_hash_table (info)->dynamic_sections_created)
1824     {
1825       asection *splt;
1826       Elf64_External_Dyn *dyncon, *dynconend;
1827
1828       BFD_ASSERT (sdyn != NULL);
1829
1830       dyncon = (Elf64_External_Dyn *) sdyn->contents;
1831       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1832       for (; dyncon < dynconend; dyncon++)
1833         {
1834           Elf_Internal_Dyn dyn;
1835           const char *name;
1836           asection *s;
1837
1838           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
1839
1840           switch (dyn.d_tag)
1841             {
1842             default:
1843               continue;
1844
1845             case DT_PLTGOT:
1846               name = ".got";
1847               goto get_vma;
1848
1849             case DT_JMPREL:
1850               name = ".rela.plt";
1851
1852             get_vma:
1853               s = bfd_get_section_by_name (output_bfd, name);
1854               BFD_ASSERT (s != NULL);
1855               dyn.d_un.d_ptr = s->vma;
1856               break;
1857
1858             case DT_RELASZ:
1859               /* FIXME: This comment and code is from elf64-alpha.c:  */
1860               /* My interpretation of the TIS v1.1 ELF document indicates
1861                  that RELASZ should not include JMPREL.  This is not what
1862                  the rest of the BFD does.  It is, however, what the
1863                  glibc ld.so wants.  Do this fixup here until we found
1864                  out who is right.  */
1865               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1866               if (s)
1867                 {
1868                   /* Subtract JMPREL size from RELASZ.  */
1869                   dyn.d_un.d_val -=
1870                     (s->_cooked_size ? s->_cooked_size : s->_raw_size);
1871                 }
1872               break;
1873
1874             case DT_PLTRELSZ:
1875               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1876               BFD_ASSERT (s != NULL);
1877               dyn.d_un.d_val =
1878                 (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size);
1879               break;
1880             }
1881           bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
1882         }
1883
1884       /* Initialize the contents of the .plt section.  */
1885       splt = bfd_get_section_by_name (dynobj, ".plt");
1886       BFD_ASSERT (splt != NULL);
1887       if (splt->_raw_size > 0)
1888         {
1889           /* Fill in the first entry in the procedure linkage table.  */
1890           memcpy (splt->contents, elf64_x86_64_plt0_entry, PLT_ENTRY_SIZE);
1891           /* Add offset for pushq GOT+8(%rip), since the instruction
1892              uses 6 bytes subtract this value.  */
1893           bfd_put_32 (output_bfd,
1894                       (sgot->output_section->vma
1895                        + sgot->output_offset
1896                        + 8
1897                        - splt->output_section->vma
1898                        - splt->output_offset
1899                        - 6),
1900                       splt->contents + 2);
1901           /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
1902              the end of the instruction.  */
1903           bfd_put_32 (output_bfd,
1904                       (sgot->output_section->vma
1905                        + sgot->output_offset
1906                        + 16
1907                        - splt->output_section->vma
1908                        - splt->output_offset
1909                        - 12),
1910                       splt->contents + 8);
1911
1912         }
1913
1914       elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1915         PLT_ENTRY_SIZE;
1916     }
1917
1918   /* Set the first entry in the global offset table to the address of
1919      the dynamic section.  */
1920   if (sgot->_raw_size > 0)
1921     {
1922       if (sdyn == NULL)
1923         bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
1924       else
1925         bfd_put_64 (output_bfd,
1926                     sdyn->output_section->vma + sdyn->output_offset,
1927                     sgot->contents);
1928       /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
1929       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE);
1930       bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE*2);
1931     }
1932
1933   elf_section_data (sgot->output_section)->this_hdr.sh_entsize =
1934     GOT_ENTRY_SIZE;
1935
1936   return true;
1937 }
1938
1939 static enum elf_reloc_type_class
1940 elf64_x86_64_reloc_type_class (rela)
1941      const Elf_Internal_Rela *rela;
1942 {
1943   switch ((int) ELF64_R_TYPE (rela->r_info))
1944     {
1945     case R_X86_64_RELATIVE:
1946       return reloc_class_relative;
1947     case R_X86_64_JUMP_SLOT:
1948       return reloc_class_plt;
1949     case R_X86_64_COPY:
1950       return reloc_class_copy;
1951     default:
1952       return reloc_class_normal;
1953     }
1954 }
1955
1956 #define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_vec
1957 #define TARGET_LITTLE_NAME                  "elf64-x86-64"
1958 #define ELF_ARCH                            bfd_arch_i386
1959 #define ELF_MACHINE_CODE                    EM_X86_64
1960 #define ELF_MAXPAGESIZE                     0x100000
1961
1962 #define elf_backend_can_gc_sections         1
1963 #define elf_backend_want_got_plt            1
1964 #define elf_backend_plt_readonly            1
1965 #define elf_backend_want_plt_sym            0
1966 #define elf_backend_got_header_size         (GOT_ENTRY_SIZE*3)
1967 #define elf_backend_plt_header_size         PLT_ENTRY_SIZE
1968
1969 #define elf_info_to_howto                   elf64_x86_64_info_to_howto
1970
1971 #define bfd_elf64_bfd_final_link            _bfd_elf64_gc_common_final_link
1972 #define bfd_elf64_bfd_link_hash_table_create \
1973   elf64_x86_64_link_hash_table_create
1974 #define bfd_elf64_bfd_reloc_type_lookup     elf64_x86_64_reloc_type_lookup
1975
1976 #define elf_backend_adjust_dynamic_symbol   elf64_x86_64_adjust_dynamic_symbol
1977 #define elf_backend_check_relocs            elf64_x86_64_check_relocs
1978 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
1979 #define elf_backend_finish_dynamic_sections \
1980   elf64_x86_64_finish_dynamic_sections
1981 #define elf_backend_finish_dynamic_symbol   elf64_x86_64_finish_dynamic_symbol
1982 #define elf_backend_gc_mark_hook            elf64_x86_64_gc_mark_hook
1983 #define elf_backend_gc_sweep_hook           elf64_x86_64_gc_sweep_hook
1984 #define elf_backend_relocate_section        elf64_x86_64_relocate_section
1985 #define elf_backend_size_dynamic_sections   elf64_x86_64_size_dynamic_sections
1986 #define elf_backend_object_p                elf64_x86_64_elf_object_p
1987 #define elf_backend_reloc_type_class        elf64_x86_64_reloc_type_class
1988
1989 #include "elf64-target.h"