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