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