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