OSDN Git Service

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