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