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