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