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