Upload Tizen:Base source
[external/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           case R_X86_64_DTPOFF64:
1221           case R_X86_64_TPOFF64:
1222           case R_X86_64_PC64:
1223           case R_X86_64_GOTOFF64:
1224           case R_X86_64_GOT64:
1225           case R_X86_64_GOTPCREL64:
1226           case R_X86_64_GOTPC64:
1227           case R_X86_64_GOTPLT64:
1228           case R_X86_64_PLTOFF64:
1229               {
1230                 if (h)
1231                   name = h->root.root.string;
1232                 else
1233                   name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1234                                            NULL);
1235                 (*_bfd_error_handler)
1236                   (_("%B: relocation %s against symbol `%s' isn't "
1237                      "supported in x32 mode"), abfd,
1238                    x86_64_elf_howto_table[r_type].name, name);
1239                 bfd_set_error (bfd_error_bad_value);
1240                 return FALSE;
1241               }
1242             break;
1243           }
1244
1245       if (h != NULL)
1246         {
1247           /* Create the ifunc sections for static executables.  If we
1248              never see an indirect function symbol nor we are building
1249              a static executable, those sections will be empty and
1250              won't appear in output.  */
1251           switch (r_type)
1252             {
1253             default:
1254               break;
1255
1256             case R_X86_64_32S:
1257             case R_X86_64_32:
1258             case R_X86_64_64:
1259             case R_X86_64_PC32:
1260             case R_X86_64_PC64:
1261             case R_X86_64_PLT32:
1262             case R_X86_64_GOTPCREL:
1263             case R_X86_64_GOTPCREL64:
1264               if (!_bfd_elf_create_ifunc_sections (abfd, info))
1265                 return FALSE;
1266               break;
1267             }
1268
1269           /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1270              it here if it is defined in a non-shared object.  */
1271           if (h->type == STT_GNU_IFUNC
1272               && h->def_regular)
1273             {
1274               /* It is referenced by a non-shared object. */
1275               h->ref_regular = 1;
1276               h->needs_plt = 1;
1277
1278               /* STT_GNU_IFUNC symbol must go through PLT.  */
1279               h->plt.refcount += 1;
1280
1281               /* STT_GNU_IFUNC needs dynamic sections.  */
1282               if (htab->elf.dynobj == NULL)
1283                 htab->elf.dynobj = abfd;
1284
1285               switch (r_type)
1286                 {
1287                 default:
1288                   if (h->root.root.string)
1289                     name = h->root.root.string;
1290                   else
1291                     name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1292                                              NULL);
1293                   (*_bfd_error_handler)
1294                     (_("%B: relocation %s against STT_GNU_IFUNC "
1295                        "symbol `%s' isn't handled by %s"), abfd,
1296                      x86_64_elf_howto_table[r_type].name,
1297                      name, __FUNCTION__);
1298                   bfd_set_error (bfd_error_bad_value);
1299                   return FALSE;
1300
1301                 case R_X86_64_32:
1302                   if (ABI_64_P (abfd))
1303                     goto not_pointer;
1304                 case R_X86_64_64:
1305                   h->non_got_ref = 1;
1306                   h->pointer_equality_needed = 1;
1307                   if (info->shared)
1308                     {
1309                       /* We must copy these reloc types into the output
1310                          file.  Create a reloc section in dynobj and
1311                          make room for this reloc.  */
1312                       sreloc = _bfd_elf_create_ifunc_dyn_reloc
1313                         (abfd, info, sec, sreloc,
1314                          &((struct elf_x86_64_link_hash_entry *) h)->dyn_relocs);
1315                       if (sreloc == NULL)
1316                         return FALSE;
1317                     }
1318                   break;
1319
1320                 case R_X86_64_32S:
1321                 case R_X86_64_PC32:
1322                 case R_X86_64_PC64:
1323 not_pointer:
1324                   h->non_got_ref = 1;
1325                   if (r_type != R_X86_64_PC32
1326                       && r_type != R_X86_64_PC64)
1327                     h->pointer_equality_needed = 1;
1328                   break;
1329
1330                 case R_X86_64_PLT32:
1331                   break;
1332
1333                 case R_X86_64_GOTPCREL:
1334                 case R_X86_64_GOTPCREL64:
1335                   h->got.refcount += 1;
1336                   if (htab->elf.sgot == NULL
1337                       && !_bfd_elf_create_got_section (htab->elf.dynobj,
1338                                                        info))
1339                     return FALSE;
1340                   break;
1341                 }
1342
1343               continue;
1344             }
1345         }
1346
1347       if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
1348                                        symtab_hdr, sym_hashes,
1349                                        &r_type, GOT_UNKNOWN,
1350                                        rel, rel_end, h, r_symndx))
1351         return FALSE;
1352
1353       switch (r_type)
1354         {
1355         case R_X86_64_TLSLD:
1356           htab->tls_ld_got.refcount += 1;
1357           goto create_got;
1358
1359         case R_X86_64_TPOFF32:
1360           if (!info->executable && ABI_64_P (abfd))
1361             {
1362               if (h)
1363                 name = h->root.root.string;
1364               else
1365                 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1366                                          NULL);
1367               (*_bfd_error_handler)
1368                 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1369                  abfd,
1370                  x86_64_elf_howto_table[r_type].name, name);
1371               bfd_set_error (bfd_error_bad_value);
1372               return FALSE;
1373             }
1374           break;
1375
1376         case R_X86_64_GOTTPOFF:
1377           if (!info->executable)
1378             info->flags |= DF_STATIC_TLS;
1379           /* Fall through */
1380
1381         case R_X86_64_GOT32:
1382         case R_X86_64_GOTPCREL:
1383         case R_X86_64_TLSGD:
1384         case R_X86_64_GOT64:
1385         case R_X86_64_GOTPCREL64:
1386         case R_X86_64_GOTPLT64:
1387         case R_X86_64_GOTPC32_TLSDESC:
1388         case R_X86_64_TLSDESC_CALL:
1389           /* This symbol requires a global offset table entry.  */
1390           {
1391             int tls_type, old_tls_type;
1392
1393             switch (r_type)
1394               {
1395               default: tls_type = GOT_NORMAL; break;
1396               case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1397               case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1398               case R_X86_64_GOTPC32_TLSDESC:
1399               case R_X86_64_TLSDESC_CALL:
1400                 tls_type = GOT_TLS_GDESC; break;
1401               }
1402
1403             if (h != NULL)
1404               {
1405                 if (r_type == R_X86_64_GOTPLT64)
1406                   {
1407                     /* This relocation indicates that we also need
1408                        a PLT entry, as this is a function.  We don't need
1409                        a PLT entry for local symbols.  */
1410                     h->needs_plt = 1;
1411                     h->plt.refcount += 1;
1412                   }
1413                 h->got.refcount += 1;
1414                 old_tls_type = elf_x86_64_hash_entry (h)->tls_type;
1415               }
1416             else
1417               {
1418                 bfd_signed_vma *local_got_refcounts;
1419
1420                 /* This is a global offset table entry for a local symbol.  */
1421                 local_got_refcounts = elf_local_got_refcounts (abfd);
1422                 if (local_got_refcounts == NULL)
1423                   {
1424                     bfd_size_type size;
1425
1426                     size = symtab_hdr->sh_info;
1427                     size *= sizeof (bfd_signed_vma)
1428                       + sizeof (bfd_vma) + sizeof (char);
1429                     local_got_refcounts = ((bfd_signed_vma *)
1430                                            bfd_zalloc (abfd, size));
1431                     if (local_got_refcounts == NULL)
1432                       return FALSE;
1433                     elf_local_got_refcounts (abfd) = local_got_refcounts;
1434                     elf_x86_64_local_tlsdesc_gotent (abfd)
1435                       = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1436                     elf_x86_64_local_got_tls_type (abfd)
1437                       = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1438                   }
1439                 local_got_refcounts[r_symndx] += 1;
1440                 old_tls_type
1441                   = elf_x86_64_local_got_tls_type (abfd) [r_symndx];
1442               }
1443
1444             /* If a TLS symbol is accessed using IE at least once,
1445                there is no point to use dynamic model for it.  */
1446             if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1447                 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1448                     || tls_type != GOT_TLS_IE))
1449               {
1450                 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1451                   tls_type = old_tls_type;
1452                 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1453                          && GOT_TLS_GD_ANY_P (tls_type))
1454                   tls_type |= old_tls_type;
1455                 else
1456                   {
1457                     if (h)
1458                       name = h->root.root.string;
1459                     else
1460                       name = bfd_elf_sym_name (abfd, symtab_hdr,
1461                                                isym, NULL);
1462                     (*_bfd_error_handler)
1463                       (_("%B: '%s' accessed both as normal and thread local symbol"),
1464                        abfd, name);
1465                     return FALSE;
1466                   }
1467               }
1468
1469             if (old_tls_type != tls_type)
1470               {
1471                 if (h != NULL)
1472                   elf_x86_64_hash_entry (h)->tls_type = tls_type;
1473                 else
1474                   elf_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1475               }
1476           }
1477           /* Fall through */
1478
1479         case R_X86_64_GOTOFF64:
1480         case R_X86_64_GOTPC32:
1481         case R_X86_64_GOTPC64:
1482         create_got:
1483           if (htab->elf.sgot == NULL)
1484             {
1485               if (htab->elf.dynobj == NULL)
1486                 htab->elf.dynobj = abfd;
1487               if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1488                                                 info))
1489                 return FALSE;
1490             }
1491           break;
1492
1493         case R_X86_64_PLT32:
1494           /* This symbol requires a procedure linkage table entry.  We
1495              actually build the entry in adjust_dynamic_symbol,
1496              because this might be a case of linking PIC code which is
1497              never referenced by a dynamic object, in which case we
1498              don't need to generate a procedure linkage table entry
1499              after all.  */
1500
1501           /* If this is a local symbol, we resolve it directly without
1502              creating a procedure linkage table entry.  */
1503           if (h == NULL)
1504             continue;
1505
1506           h->needs_plt = 1;
1507           h->plt.refcount += 1;
1508           break;
1509
1510         case R_X86_64_PLTOFF64:
1511           /* This tries to form the 'address' of a function relative
1512              to GOT.  For global symbols we need a PLT entry.  */
1513           if (h != NULL)
1514             {
1515               h->needs_plt = 1;
1516               h->plt.refcount += 1;
1517             }
1518           goto create_got;
1519
1520         case R_X86_64_32:
1521           if (!ABI_64_P (abfd))
1522             goto pointer;
1523         case R_X86_64_8:
1524         case R_X86_64_16:
1525         case R_X86_64_32S:
1526           /* Let's help debug shared library creation.  These relocs
1527              cannot be used in shared libs.  Don't error out for
1528              sections we don't care about, such as debug sections or
1529              non-constant sections.  */
1530           if (info->shared
1531               && (sec->flags & SEC_ALLOC) != 0
1532               && (sec->flags & SEC_READONLY) != 0)
1533             {
1534               if (h)
1535                 name = h->root.root.string;
1536               else
1537                 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1538               (*_bfd_error_handler)
1539                 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1540                  abfd, x86_64_elf_howto_table[r_type].name, name);
1541               bfd_set_error (bfd_error_bad_value);
1542               return FALSE;
1543             }
1544           /* Fall through.  */
1545
1546         case R_X86_64_PC8:
1547         case R_X86_64_PC16:
1548         case R_X86_64_PC32:
1549         case R_X86_64_PC64:
1550         case R_X86_64_64:
1551 pointer:
1552           if (h != NULL && info->executable)
1553             {
1554               /* If this reloc is in a read-only section, we might
1555                  need a copy reloc.  We can't check reliably at this
1556                  stage whether the section is read-only, as input
1557                  sections have not yet been mapped to output sections.
1558                  Tentatively set the flag for now, and correct in
1559                  adjust_dynamic_symbol.  */
1560               h->non_got_ref = 1;
1561
1562               /* We may need a .plt entry if the function this reloc
1563                  refers to is in a shared lib.  */
1564               h->plt.refcount += 1;
1565               if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1566                 h->pointer_equality_needed = 1;
1567             }
1568
1569           /* If we are creating a shared library, and this is a reloc
1570              against a global symbol, or a non PC relative reloc
1571              against a local symbol, then we need to copy the reloc
1572              into the shared library.  However, if we are linking with
1573              -Bsymbolic, we do not need to copy a reloc against a
1574              global symbol which is defined in an object we are
1575              including in the link (i.e., DEF_REGULAR is set).  At
1576              this point we have not seen all the input files, so it is
1577              possible that DEF_REGULAR is not set now but will be set
1578              later (it is never cleared).  In case of a weak definition,
1579              DEF_REGULAR may be cleared later by a strong definition in
1580              a shared library.  We account for that possibility below by
1581              storing information in the relocs_copied field of the hash
1582              table entry.  A similar situation occurs when creating
1583              shared libraries and symbol visibility changes render the
1584              symbol local.
1585
1586              If on the other hand, we are creating an executable, we
1587              may need to keep relocations for symbols satisfied by a
1588              dynamic library if we manage to avoid copy relocs for the
1589              symbol.  */
1590           if ((info->shared
1591                && (sec->flags & SEC_ALLOC) != 0
1592                && (! IS_X86_64_PCREL_TYPE (r_type)
1593                    || (h != NULL
1594                        && (! SYMBOLIC_BIND (info, h)
1595                            || h->root.type == bfd_link_hash_defweak
1596                            || !h->def_regular))))
1597               || (ELIMINATE_COPY_RELOCS
1598                   && !info->shared
1599                   && (sec->flags & SEC_ALLOC) != 0
1600                   && h != NULL
1601                   && (h->root.type == bfd_link_hash_defweak
1602                       || !h->def_regular)))
1603             {
1604               struct elf_dyn_relocs *p;
1605               struct elf_dyn_relocs **head;
1606
1607               /* We must copy these reloc types into the output file.
1608                  Create a reloc section in dynobj and make room for
1609                  this reloc.  */
1610               if (sreloc == NULL)
1611                 {
1612                   if (htab->elf.dynobj == NULL)
1613                     htab->elf.dynobj = abfd;
1614
1615                   sreloc = _bfd_elf_make_dynamic_reloc_section
1616                     (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
1617                      abfd, /*rela?*/ TRUE);
1618
1619                   if (sreloc == NULL)
1620                     return FALSE;
1621                 }
1622
1623               /* If this is a global symbol, we count the number of
1624                  relocations we need for this symbol.  */
1625               if (h != NULL)
1626                 {
1627                   head = &((struct elf_x86_64_link_hash_entry *) h)->dyn_relocs;
1628                 }
1629               else
1630                 {
1631                   /* Track dynamic relocs needed for local syms too.
1632                      We really need local syms available to do this
1633                      easily.  Oh well.  */
1634                   asection *s;
1635                   void **vpp;
1636
1637                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1638                                                 abfd, r_symndx);
1639                   if (isym == NULL)
1640                     return FALSE;
1641
1642                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1643                   if (s == NULL)
1644                     s = sec;
1645
1646                   /* Beware of type punned pointers vs strict aliasing
1647                      rules.  */
1648                   vpp = &(elf_section_data (s)->local_dynrel);
1649                   head = (struct elf_dyn_relocs **)vpp;
1650                 }
1651
1652               p = *head;
1653               if (p == NULL || p->sec != sec)
1654                 {
1655                   bfd_size_type amt = sizeof *p;
1656
1657                   p = ((struct elf_dyn_relocs *)
1658                        bfd_alloc (htab->elf.dynobj, amt));
1659                   if (p == NULL)
1660                     return FALSE;
1661                   p->next = *head;
1662                   *head = p;
1663                   p->sec = sec;
1664                   p->count = 0;
1665                   p->pc_count = 0;
1666                 }
1667
1668               p->count += 1;
1669               if (IS_X86_64_PCREL_TYPE (r_type))
1670                 p->pc_count += 1;
1671             }
1672           break;
1673
1674           /* This relocation describes the C++ object vtable hierarchy.
1675              Reconstruct it for later use during GC.  */
1676         case R_X86_64_GNU_VTINHERIT:
1677           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1678             return FALSE;
1679           break;
1680
1681           /* This relocation describes which C++ vtable entries are actually
1682              used.  Record for later use during GC.  */
1683         case R_X86_64_GNU_VTENTRY:
1684           BFD_ASSERT (h != NULL);
1685           if (h != NULL
1686               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1687             return FALSE;
1688           break;
1689
1690         default:
1691           break;
1692         }
1693     }
1694
1695   return TRUE;
1696 }
1697
1698 /* Return the section that should be marked against GC for a given
1699    relocation.  */
1700
1701 static asection *
1702 elf_x86_64_gc_mark_hook (asection *sec,
1703                          struct bfd_link_info *info,
1704                          Elf_Internal_Rela *rel,
1705                          struct elf_link_hash_entry *h,
1706                          Elf_Internal_Sym *sym)
1707 {
1708   if (h != NULL)
1709     switch (ELF32_R_TYPE (rel->r_info))
1710       {
1711       case R_X86_64_GNU_VTINHERIT:
1712       case R_X86_64_GNU_VTENTRY:
1713         return NULL;
1714       }
1715
1716   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1717 }
1718
1719 /* Update the got entry reference counts for the section being removed.  */
1720
1721 static bfd_boolean
1722 elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1723                           asection *sec,
1724                           const Elf_Internal_Rela *relocs)
1725 {
1726   struct elf_x86_64_link_hash_table *htab;
1727   Elf_Internal_Shdr *symtab_hdr;
1728   struct elf_link_hash_entry **sym_hashes;
1729   bfd_signed_vma *local_got_refcounts;
1730   const Elf_Internal_Rela *rel, *relend;
1731
1732   if (info->relocatable)
1733     return TRUE;
1734
1735   htab = elf_x86_64_hash_table (info);
1736   if (htab == NULL)
1737     return FALSE;
1738
1739   elf_section_data (sec)->local_dynrel = NULL;
1740
1741   symtab_hdr = &elf_symtab_hdr (abfd);
1742   sym_hashes = elf_sym_hashes (abfd);
1743   local_got_refcounts = elf_local_got_refcounts (abfd);
1744
1745   htab = elf_x86_64_hash_table (info);
1746   relend = relocs + sec->reloc_count;
1747   for (rel = relocs; rel < relend; rel++)
1748     {
1749       unsigned long r_symndx;
1750       unsigned int r_type;
1751       struct elf_link_hash_entry *h = NULL;
1752
1753       r_symndx = htab->r_sym (rel->r_info);
1754       if (r_symndx >= symtab_hdr->sh_info)
1755         {
1756           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1757           while (h->root.type == bfd_link_hash_indirect
1758                  || h->root.type == bfd_link_hash_warning)
1759             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1760         }
1761       else
1762         {
1763           /* A local symbol.  */
1764           Elf_Internal_Sym *isym;
1765
1766           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1767                                         abfd, r_symndx);
1768
1769           /* Check relocation against local STT_GNU_IFUNC symbol.  */
1770           if (isym != NULL
1771               && ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1772             {
1773               h = elf_x86_64_get_local_sym_hash (htab, abfd, rel, FALSE);
1774               if (h == NULL)
1775                 abort ();
1776             }
1777         }
1778
1779       if (h)
1780         {
1781           struct elf_x86_64_link_hash_entry *eh;
1782           struct elf_dyn_relocs **pp;
1783           struct elf_dyn_relocs *p;
1784
1785           eh = (struct elf_x86_64_link_hash_entry *) h;
1786
1787           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1788             if (p->sec == sec)
1789               {
1790                 /* Everything must go for SEC.  */
1791                 *pp = p->next;
1792                 break;
1793               }
1794         }
1795
1796       r_type = ELF32_R_TYPE (rel->r_info);
1797       if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
1798                                        symtab_hdr, sym_hashes,
1799                                        &r_type, GOT_UNKNOWN,
1800                                        rel, relend, h, r_symndx))
1801         return FALSE;
1802
1803       switch (r_type)
1804         {
1805         case R_X86_64_TLSLD:
1806           if (htab->tls_ld_got.refcount > 0)
1807             htab->tls_ld_got.refcount -= 1;
1808           break;
1809
1810         case R_X86_64_TLSGD:
1811         case R_X86_64_GOTPC32_TLSDESC:
1812         case R_X86_64_TLSDESC_CALL:
1813         case R_X86_64_GOTTPOFF:
1814         case R_X86_64_GOT32:
1815         case R_X86_64_GOTPCREL:
1816         case R_X86_64_GOT64:
1817         case R_X86_64_GOTPCREL64:
1818         case R_X86_64_GOTPLT64:
1819           if (h != NULL)
1820             {
1821               if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1822                 h->plt.refcount -= 1;
1823               if (h->got.refcount > 0)
1824                 h->got.refcount -= 1;
1825               if (h->type == STT_GNU_IFUNC)
1826                 {
1827                   if (h->plt.refcount > 0)
1828                     h->plt.refcount -= 1;
1829                 }
1830             }
1831           else if (local_got_refcounts != NULL)
1832             {
1833               if (local_got_refcounts[r_symndx] > 0)
1834                 local_got_refcounts[r_symndx] -= 1;
1835             }
1836           break;
1837
1838         case R_X86_64_8:
1839         case R_X86_64_16:
1840         case R_X86_64_32:
1841         case R_X86_64_64:
1842         case R_X86_64_32S:
1843         case R_X86_64_PC8:
1844         case R_X86_64_PC16:
1845         case R_X86_64_PC32:
1846         case R_X86_64_PC64:
1847           if (info->shared
1848               && (h == NULL || h->type != STT_GNU_IFUNC))
1849             break;
1850           /* Fall thru */
1851
1852         case R_X86_64_PLT32:
1853         case R_X86_64_PLTOFF64:
1854           if (h != NULL)
1855             {
1856               if (h->plt.refcount > 0)
1857                 h->plt.refcount -= 1;
1858             }
1859           break;
1860
1861         default:
1862           break;
1863         }
1864     }
1865
1866   return TRUE;
1867 }
1868
1869 /* Adjust a symbol defined by a dynamic object and referenced by a
1870    regular object.  The current definition is in some section of the
1871    dynamic object, but we're not including those sections.  We have to
1872    change the definition to something the rest of the link can
1873    understand.  */
1874
1875 static bfd_boolean
1876 elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1877                                   struct elf_link_hash_entry *h)
1878 {
1879   struct elf_x86_64_link_hash_table *htab;
1880   asection *s;
1881
1882   /* STT_GNU_IFUNC symbol must go through PLT. */
1883   if (h->type == STT_GNU_IFUNC)
1884     {
1885       if (h->plt.refcount <= 0)
1886         {
1887           h->plt.offset = (bfd_vma) -1;
1888           h->needs_plt = 0;
1889         }
1890       return TRUE;
1891     }
1892
1893   /* If this is a function, put it in the procedure linkage table.  We
1894      will fill in the contents of the procedure linkage table later,
1895      when we know the address of the .got section.  */
1896   if (h->type == STT_FUNC
1897       || h->needs_plt)
1898     {
1899       if (h->plt.refcount <= 0
1900           || SYMBOL_CALLS_LOCAL (info, h)
1901           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1902               && h->root.type == bfd_link_hash_undefweak))
1903         {
1904           /* This case can occur if we saw a PLT32 reloc in an input
1905              file, but the symbol was never referred to by a dynamic
1906              object, or if all references were garbage collected.  In
1907              such a case, we don't actually need to build a procedure
1908              linkage table, and we can just do a PC32 reloc instead.  */
1909           h->plt.offset = (bfd_vma) -1;
1910           h->needs_plt = 0;
1911         }
1912
1913       return TRUE;
1914     }
1915   else
1916     /* It's possible that we incorrectly decided a .plt reloc was
1917        needed for an R_X86_64_PC32 reloc to a non-function sym in
1918        check_relocs.  We can't decide accurately between function and
1919        non-function syms in check-relocs;  Objects loaded later in
1920        the link may change h->type.  So fix it now.  */
1921     h->plt.offset = (bfd_vma) -1;
1922
1923   /* If this is a weak symbol, and there is a real definition, the
1924      processor independent code will have arranged for us to see the
1925      real definition first, and we can just use the same value.  */
1926   if (h->u.weakdef != NULL)
1927     {
1928       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1929                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
1930       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1931       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1932       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1933         h->non_got_ref = h->u.weakdef->non_got_ref;
1934       return TRUE;
1935     }
1936
1937   /* This is a reference to a symbol defined by a dynamic object which
1938      is not a function.  */
1939
1940   /* If we are creating a shared library, we must presume that the
1941      only references to the symbol are via the global offset table.
1942      For such cases we need not do anything here; the relocations will
1943      be handled correctly by relocate_section.  */
1944   if (info->shared)
1945     return TRUE;
1946
1947   /* If there are no references to this symbol that do not use the
1948      GOT, we don't need to generate a copy reloc.  */
1949   if (!h->non_got_ref)
1950     return TRUE;
1951
1952   /* If -z nocopyreloc was given, we won't generate them either.  */
1953   if (info->nocopyreloc)
1954     {
1955       h->non_got_ref = 0;
1956       return TRUE;
1957     }
1958
1959   if (ELIMINATE_COPY_RELOCS)
1960     {
1961       struct elf_x86_64_link_hash_entry * eh;
1962       struct elf_dyn_relocs *p;
1963
1964       eh = (struct elf_x86_64_link_hash_entry *) h;
1965       for (p = eh->dyn_relocs; p != NULL; p = p->next)
1966         {
1967           s = p->sec->output_section;
1968           if (s != NULL && (s->flags & SEC_READONLY) != 0)
1969             break;
1970         }
1971
1972       /* If we didn't find any dynamic relocs in read-only sections, then
1973          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1974       if (p == NULL)
1975         {
1976           h->non_got_ref = 0;
1977           return TRUE;
1978         }
1979     }
1980
1981   if (h->size == 0)
1982     {
1983       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1984                              h->root.root.string);
1985       return TRUE;
1986     }
1987
1988   /* We must allocate the symbol in our .dynbss section, which will
1989      become part of the .bss section of the executable.  There will be
1990      an entry for this symbol in the .dynsym section.  The dynamic
1991      object will contain position independent code, so all references
1992      from the dynamic object to this symbol will go through the global
1993      offset table.  The dynamic linker will use the .dynsym entry to
1994      determine the address it must put in the global offset table, so
1995      both the dynamic object and the regular object will refer to the
1996      same memory location for the variable.  */
1997
1998   htab = elf_x86_64_hash_table (info);
1999   if (htab == NULL)
2000     return FALSE;
2001
2002   /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
2003      to copy the initial value out of the dynamic object and into the
2004      runtime process image.  */
2005   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2006     {
2007       const struct elf_backend_data *bed;
2008       bed = get_elf_backend_data (info->output_bfd);
2009       htab->srelbss->size += bed->s->sizeof_rela;
2010       h->needs_copy = 1;
2011     }
2012
2013   s = htab->sdynbss;
2014
2015   return _bfd_elf_adjust_dynamic_copy (h, s);
2016 }
2017
2018 /* Allocate space in .plt, .got and associated reloc sections for
2019    dynamic relocs.  */
2020
2021 static bfd_boolean
2022 elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2023 {
2024   struct bfd_link_info *info;
2025   struct elf_x86_64_link_hash_table *htab;
2026   struct elf_x86_64_link_hash_entry *eh;
2027   struct elf_dyn_relocs *p;
2028   const struct elf_backend_data *bed;
2029
2030   if (h->root.type == bfd_link_hash_indirect)
2031     return TRUE;
2032
2033   if (h->root.type == bfd_link_hash_warning)
2034     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2035   eh = (struct elf_x86_64_link_hash_entry *) h;
2036
2037   info = (struct bfd_link_info *) inf;
2038   htab = elf_x86_64_hash_table (info);
2039   if (htab == NULL)
2040     return FALSE;
2041   bed = get_elf_backend_data (info->output_bfd);
2042
2043   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2044      here if it is defined and referenced in a non-shared object.  */
2045   if (h->type == STT_GNU_IFUNC
2046       && h->def_regular)
2047     return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2048                                                &eh->dyn_relocs,
2049                                                PLT_ENTRY_SIZE,
2050                                                GOT_ENTRY_SIZE);
2051   else if (htab->elf.dynamic_sections_created
2052            && h->plt.refcount > 0)
2053     {
2054       /* Make sure this symbol is output as a dynamic symbol.
2055          Undefined weak syms won't yet be marked as dynamic.  */
2056       if (h->dynindx == -1
2057           && !h->forced_local)
2058         {
2059           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2060             return FALSE;
2061         }
2062
2063       if (info->shared
2064           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2065         {
2066           asection *s = htab->elf.splt;
2067
2068           /* If this is the first .plt entry, make room for the special
2069              first entry.  */
2070           if (s->size == 0)
2071             s->size += PLT_ENTRY_SIZE;
2072
2073           h->plt.offset = s->size;
2074
2075           /* If this symbol is not defined in a regular file, and we are
2076              not generating a shared library, then set the symbol to this
2077              location in the .plt.  This is required to make function
2078              pointers compare as equal between the normal executable and
2079              the shared library.  */
2080           if (! info->shared
2081               && !h->def_regular)
2082             {
2083               h->root.u.def.section = s;
2084               h->root.u.def.value = h->plt.offset;
2085             }
2086
2087           /* Make room for this entry.  */
2088           s->size += PLT_ENTRY_SIZE;
2089
2090           /* We also need to make an entry in the .got.plt section, which
2091              will be placed in the .got section by the linker script.  */
2092           htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
2093
2094           /* We also need to make an entry in the .rela.plt section.  */
2095           htab->elf.srelplt->size += bed->s->sizeof_rela;
2096           htab->elf.srelplt->reloc_count++;
2097         }
2098       else
2099         {
2100           h->plt.offset = (bfd_vma) -1;
2101           h->needs_plt = 0;
2102         }
2103     }
2104   else
2105     {
2106       h->plt.offset = (bfd_vma) -1;
2107       h->needs_plt = 0;
2108     }
2109
2110   eh->tlsdesc_got = (bfd_vma) -1;
2111
2112   /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
2113      make it a R_X86_64_TPOFF32 requiring no GOT entry.  */
2114   if (h->got.refcount > 0
2115       && info->executable
2116       && h->dynindx == -1
2117       && elf_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
2118     {
2119       h->got.offset = (bfd_vma) -1;
2120     }
2121   else if (h->got.refcount > 0)
2122     {
2123       asection *s;
2124       bfd_boolean dyn;
2125       int tls_type = elf_x86_64_hash_entry (h)->tls_type;
2126
2127       /* Make sure this symbol is output as a dynamic symbol.
2128          Undefined weak syms won't yet be marked as dynamic.  */
2129       if (h->dynindx == -1
2130           && !h->forced_local)
2131         {
2132           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2133             return FALSE;
2134         }
2135
2136       if (GOT_TLS_GDESC_P (tls_type))
2137         {
2138           eh->tlsdesc_got = htab->elf.sgotplt->size
2139             - elf_x86_64_compute_jump_table_size (htab);
2140           htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2141           h->got.offset = (bfd_vma) -2;
2142         }
2143       if (! GOT_TLS_GDESC_P (tls_type)
2144           || GOT_TLS_GD_P (tls_type))
2145         {
2146           s = htab->elf.sgot;
2147           h->got.offset = s->size;
2148           s->size += GOT_ENTRY_SIZE;
2149           if (GOT_TLS_GD_P (tls_type))
2150             s->size += GOT_ENTRY_SIZE;
2151         }
2152       dyn = htab->elf.dynamic_sections_created;
2153       /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2154          and two if global.
2155          R_X86_64_GOTTPOFF needs one dynamic relocation.  */
2156       if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2157           || tls_type == GOT_TLS_IE)
2158         htab->elf.srelgot->size += bed->s->sizeof_rela;
2159       else if (GOT_TLS_GD_P (tls_type))
2160         htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
2161       else if (! GOT_TLS_GDESC_P (tls_type)
2162                && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2163                    || h->root.type != bfd_link_hash_undefweak)
2164                && (info->shared
2165                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2166         htab->elf.srelgot->size += bed->s->sizeof_rela;
2167       if (GOT_TLS_GDESC_P (tls_type))
2168         {
2169           htab->elf.srelplt->size += bed->s->sizeof_rela;
2170           htab->tlsdesc_plt = (bfd_vma) -1;
2171         }
2172     }
2173   else
2174     h->got.offset = (bfd_vma) -1;
2175
2176   if (eh->dyn_relocs == NULL)
2177     return TRUE;
2178
2179   /* In the shared -Bsymbolic case, discard space allocated for
2180      dynamic pc-relative relocs against symbols which turn out to be
2181      defined in regular objects.  For the normal shared case, discard
2182      space for pc-relative relocs that have become local due to symbol
2183      visibility changes.  */
2184
2185   if (info->shared)
2186     {
2187       /* Relocs that use pc_count are those that appear on a call
2188          insn, or certain REL relocs that can generated via assembly.
2189          We want calls to protected symbols to resolve directly to the
2190          function rather than going via the plt.  If people want
2191          function pointer comparisons to work as expected then they
2192          should avoid writing weird assembly.  */
2193       if (SYMBOL_CALLS_LOCAL (info, h))
2194         {
2195           struct elf_dyn_relocs **pp;
2196
2197           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2198             {
2199               p->count -= p->pc_count;
2200               p->pc_count = 0;
2201               if (p->count == 0)
2202                 *pp = p->next;
2203               else
2204                 pp = &p->next;
2205             }
2206         }
2207
2208       /* Also discard relocs on undefined weak syms with non-default
2209          visibility.  */
2210       if (eh->dyn_relocs != NULL
2211           && h->root.type == bfd_link_hash_undefweak)
2212         {
2213           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2214             eh->dyn_relocs = NULL;
2215
2216           /* Make sure undefined weak symbols are output as a dynamic
2217              symbol in PIEs.  */
2218           else if (h->dynindx == -1
2219                    && ! h->forced_local
2220                    && ! bfd_elf_link_record_dynamic_symbol (info, h))
2221             return FALSE;
2222         }
2223
2224     }
2225   else if (ELIMINATE_COPY_RELOCS)
2226     {
2227       /* For the non-shared case, discard space for relocs against
2228          symbols which turn out to need copy relocs or are not
2229          dynamic.  */
2230
2231       if (!h->non_got_ref
2232           && ((h->def_dynamic
2233                && !h->def_regular)
2234               || (htab->elf.dynamic_sections_created
2235                   && (h->root.type == bfd_link_hash_undefweak
2236                       || h->root.type == bfd_link_hash_undefined))))
2237         {
2238           /* Make sure this symbol is output as a dynamic symbol.
2239              Undefined weak syms won't yet be marked as dynamic.  */
2240           if (h->dynindx == -1
2241               && ! h->forced_local
2242               && ! bfd_elf_link_record_dynamic_symbol (info, h))
2243             return FALSE;
2244
2245           /* If that succeeded, we know we'll be keeping all the
2246              relocs.  */
2247           if (h->dynindx != -1)
2248             goto keep;
2249         }
2250
2251       eh->dyn_relocs = NULL;
2252
2253     keep: ;
2254     }
2255
2256   /* Finally, allocate space.  */
2257   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2258     {
2259       asection * sreloc;
2260
2261       sreloc = elf_section_data (p->sec)->sreloc;
2262
2263       BFD_ASSERT (sreloc != NULL);
2264
2265       sreloc->size += p->count * bed->s->sizeof_rela;
2266     }
2267
2268   return TRUE;
2269 }
2270
2271 /* Allocate space in .plt, .got and associated reloc sections for
2272    local dynamic relocs.  */
2273
2274 static bfd_boolean
2275 elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
2276 {
2277   struct elf_link_hash_entry *h
2278     = (struct elf_link_hash_entry *) *slot;
2279
2280   if (h->type != STT_GNU_IFUNC
2281       || !h->def_regular
2282       || !h->ref_regular
2283       || !h->forced_local
2284       || h->root.type != bfd_link_hash_defined)
2285     abort ();
2286
2287   return elf_x86_64_allocate_dynrelocs (h, inf);
2288 }
2289
2290 /* Find any dynamic relocs that apply to read-only sections.  */
2291
2292 static bfd_boolean
2293 elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h,
2294                                void * inf)
2295 {
2296   struct elf_x86_64_link_hash_entry *eh;
2297   struct elf_dyn_relocs *p;
2298
2299   if (h->root.type == bfd_link_hash_warning)
2300     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2301
2302   eh = (struct elf_x86_64_link_hash_entry *) h;
2303   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2304     {
2305       asection *s = p->sec->output_section;
2306
2307       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2308         {
2309           struct bfd_link_info *info = (struct bfd_link_info *) inf;
2310
2311           info->flags |= DF_TEXTREL;
2312
2313           /* Not an error, just cut short the traversal.  */
2314           return FALSE;
2315         }
2316     }
2317   return TRUE;
2318 }
2319
2320 /* Set the sizes of the dynamic sections.  */
2321
2322 static bfd_boolean
2323 elf_x86_64_size_dynamic_sections (bfd *output_bfd,
2324                                   struct bfd_link_info *info)
2325 {
2326   struct elf_x86_64_link_hash_table *htab;
2327   bfd *dynobj;
2328   asection *s;
2329   bfd_boolean relocs;
2330   bfd *ibfd;
2331   const struct elf_backend_data *bed;
2332
2333   htab = elf_x86_64_hash_table (info);
2334   if (htab == NULL)
2335     return FALSE;
2336   bed = get_elf_backend_data (output_bfd);
2337
2338   dynobj = htab->elf.dynobj;
2339   if (dynobj == NULL)
2340     abort ();
2341
2342   if (htab->elf.dynamic_sections_created)
2343     {
2344       /* Set the contents of the .interp section to the interpreter.  */
2345       if (info->executable)
2346         {
2347           s = bfd_get_section_by_name (dynobj, ".interp");
2348           if (s == NULL)
2349             abort ();
2350           s->size = htab->dynamic_interpreter_size;
2351           s->contents = (unsigned char *) htab->dynamic_interpreter;
2352         }
2353     }
2354
2355   /* Set up .got offsets for local syms, and space for local dynamic
2356      relocs.  */
2357   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2358     {
2359       bfd_signed_vma *local_got;
2360       bfd_signed_vma *end_local_got;
2361       char *local_tls_type;
2362       bfd_vma *local_tlsdesc_gotent;
2363       bfd_size_type locsymcount;
2364       Elf_Internal_Shdr *symtab_hdr;
2365       asection *srel;
2366
2367       if (! is_x86_64_elf (ibfd))
2368         continue;
2369
2370       for (s = ibfd->sections; s != NULL; s = s->next)
2371         {
2372           struct elf_dyn_relocs *p;
2373
2374           for (p = (struct elf_dyn_relocs *)
2375                     (elf_section_data (s)->local_dynrel);
2376                p != NULL;
2377                p = p->next)
2378             {
2379               if (!bfd_is_abs_section (p->sec)
2380                   && bfd_is_abs_section (p->sec->output_section))
2381                 {
2382                   /* Input section has been discarded, either because
2383                      it is a copy of a linkonce section or due to
2384                      linker script /DISCARD/, so we'll be discarding
2385                      the relocs too.  */
2386                 }
2387               else if (p->count != 0)
2388                 {
2389                   srel = elf_section_data (p->sec)->sreloc;
2390                   srel->size += p->count * bed->s->sizeof_rela;
2391                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2392                     info->flags |= DF_TEXTREL;
2393                 }
2394             }
2395         }
2396
2397       local_got = elf_local_got_refcounts (ibfd);
2398       if (!local_got)
2399         continue;
2400
2401       symtab_hdr = &elf_symtab_hdr (ibfd);
2402       locsymcount = symtab_hdr->sh_info;
2403       end_local_got = local_got + locsymcount;
2404       local_tls_type = elf_x86_64_local_got_tls_type (ibfd);
2405       local_tlsdesc_gotent = elf_x86_64_local_tlsdesc_gotent (ibfd);
2406       s = htab->elf.sgot;
2407       srel = htab->elf.srelgot;
2408       for (; local_got < end_local_got;
2409            ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2410         {
2411           *local_tlsdesc_gotent = (bfd_vma) -1;
2412           if (*local_got > 0)
2413             {
2414               if (GOT_TLS_GDESC_P (*local_tls_type))
2415                 {
2416                   *local_tlsdesc_gotent = htab->elf.sgotplt->size
2417                     - elf_x86_64_compute_jump_table_size (htab);
2418                   htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2419                   *local_got = (bfd_vma) -2;
2420                 }
2421               if (! GOT_TLS_GDESC_P (*local_tls_type)
2422                   || GOT_TLS_GD_P (*local_tls_type))
2423                 {
2424                   *local_got = s->size;
2425                   s->size += GOT_ENTRY_SIZE;
2426                   if (GOT_TLS_GD_P (*local_tls_type))
2427                     s->size += GOT_ENTRY_SIZE;
2428                 }
2429               if (info->shared
2430                   || GOT_TLS_GD_ANY_P (*local_tls_type)
2431                   || *local_tls_type == GOT_TLS_IE)
2432                 {
2433                   if (GOT_TLS_GDESC_P (*local_tls_type))
2434                     {
2435                       htab->elf.srelplt->size
2436                         += bed->s->sizeof_rela;
2437                       htab->tlsdesc_plt = (bfd_vma) -1;
2438                     }
2439                   if (! GOT_TLS_GDESC_P (*local_tls_type)
2440                       || GOT_TLS_GD_P (*local_tls_type))
2441                     srel->size += bed->s->sizeof_rela;
2442                 }
2443             }
2444           else
2445             *local_got = (bfd_vma) -1;
2446         }
2447     }
2448
2449   if (htab->tls_ld_got.refcount > 0)
2450     {
2451       /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2452          relocs.  */
2453       htab->tls_ld_got.offset = htab->elf.sgot->size;
2454       htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2455       htab->elf.srelgot->size += bed->s->sizeof_rela;
2456     }
2457   else
2458     htab->tls_ld_got.offset = -1;
2459
2460   /* Allocate global sym .plt and .got entries, and space for global
2461      sym dynamic relocs.  */
2462   elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
2463                           info);
2464
2465   /* Allocate .plt and .got entries, and space for local symbols.  */
2466   htab_traverse (htab->loc_hash_table,
2467                  elf_x86_64_allocate_local_dynrelocs,
2468                  info);
2469
2470   /* For every jump slot reserved in the sgotplt, reloc_count is
2471      incremented.  However, when we reserve space for TLS descriptors,
2472      it's not incremented, so in order to compute the space reserved
2473      for them, it suffices to multiply the reloc count by the jump
2474      slot size.  */
2475   if (htab->elf.srelplt)
2476     htab->sgotplt_jump_table_size
2477       = elf_x86_64_compute_jump_table_size (htab);
2478
2479   if (htab->tlsdesc_plt)
2480     {
2481       /* If we're not using lazy TLS relocations, don't generate the
2482          PLT and GOT entries they require.  */
2483       if ((info->flags & DF_BIND_NOW))
2484         htab->tlsdesc_plt = 0;
2485       else
2486         {
2487           htab->tlsdesc_got = htab->elf.sgot->size;
2488           htab->elf.sgot->size += GOT_ENTRY_SIZE;
2489           /* Reserve room for the initial entry.
2490              FIXME: we could probably do away with it in this case.  */
2491           if (htab->elf.splt->size == 0)
2492             htab->elf.splt->size += PLT_ENTRY_SIZE;
2493           htab->tlsdesc_plt = htab->elf.splt->size;
2494           htab->elf.splt->size += PLT_ENTRY_SIZE;
2495         }
2496     }
2497
2498   if (htab->elf.sgotplt)
2499     {
2500       struct elf_link_hash_entry *got;
2501       got = elf_link_hash_lookup (elf_hash_table (info),
2502                                   "_GLOBAL_OFFSET_TABLE_",
2503                                   FALSE, FALSE, FALSE);
2504
2505       /* Don't allocate .got.plt section if there are no GOT nor PLT
2506          entries and there is no refeence to _GLOBAL_OFFSET_TABLE_.  */
2507       if ((got == NULL
2508            || !got->ref_regular_nonweak)
2509           && (htab->elf.sgotplt->size
2510               == get_elf_backend_data (output_bfd)->got_header_size)
2511           && (htab->elf.splt == NULL
2512               || htab->elf.splt->size == 0)
2513           && (htab->elf.sgot == NULL
2514               || htab->elf.sgot->size == 0)
2515           && (htab->elf.iplt == NULL
2516               || htab->elf.iplt->size == 0)
2517           && (htab->elf.igotplt == NULL
2518               || htab->elf.igotplt->size == 0))
2519         htab->elf.sgotplt->size = 0;
2520     }
2521
2522   /* We now have determined the sizes of the various dynamic sections.
2523      Allocate memory for them.  */
2524   relocs = FALSE;
2525   for (s = dynobj->sections; s != NULL; s = s->next)
2526     {
2527       if ((s->flags & SEC_LINKER_CREATED) == 0)
2528         continue;
2529
2530       if (s == htab->elf.splt
2531           || s == htab->elf.sgot
2532           || s == htab->elf.sgotplt
2533           || s == htab->elf.iplt
2534           || s == htab->elf.igotplt
2535           || s == htab->sdynbss)
2536         {
2537           /* Strip this section if we don't need it; see the
2538              comment below.  */
2539         }
2540       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2541         {
2542           if (s->size != 0 && s != htab->elf.srelplt)
2543             relocs = TRUE;
2544
2545           /* We use the reloc_count field as a counter if we need
2546              to copy relocs into the output file.  */
2547           if (s != htab->elf.srelplt)
2548             s->reloc_count = 0;
2549         }
2550       else
2551         {
2552           /* It's not one of our sections, so don't allocate space.  */
2553           continue;
2554         }
2555
2556       if (s->size == 0)
2557         {
2558           /* If we don't need this section, strip it from the
2559              output file.  This is mostly to handle .rela.bss and
2560              .rela.plt.  We must create both sections in
2561              create_dynamic_sections, because they must be created
2562              before the linker maps input sections to output
2563              sections.  The linker does that before
2564              adjust_dynamic_symbol is called, and it is that
2565              function which decides whether anything needs to go
2566              into these sections.  */
2567
2568           s->flags |= SEC_EXCLUDE;
2569           continue;
2570         }
2571
2572       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2573         continue;
2574
2575       /* Allocate memory for the section contents.  We use bfd_zalloc
2576          here in case unused entries are not reclaimed before the
2577          section's contents are written out.  This should not happen,
2578          but this way if it does, we get a R_X86_64_NONE reloc instead
2579          of garbage.  */
2580       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2581       if (s->contents == NULL)
2582         return FALSE;
2583     }
2584
2585   if (htab->elf.dynamic_sections_created)
2586     {
2587       /* Add some entries to the .dynamic section.  We fill in the
2588          values later, in elf_x86_64_finish_dynamic_sections, but we
2589          must add the entries now so that we get the correct size for
2590          the .dynamic section.  The DT_DEBUG entry is filled in by the
2591          dynamic linker and used by the debugger.  */
2592 #define add_dynamic_entry(TAG, VAL) \
2593   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2594
2595       if (info->executable)
2596         {
2597           if (!add_dynamic_entry (DT_DEBUG, 0))
2598             return FALSE;
2599         }
2600
2601       if (htab->elf.splt->size != 0)
2602         {
2603           if (!add_dynamic_entry (DT_PLTGOT, 0)
2604               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2605               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2606               || !add_dynamic_entry (DT_JMPREL, 0))
2607             return FALSE;
2608
2609           if (htab->tlsdesc_plt
2610               && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2611                   || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2612             return FALSE;
2613         }
2614
2615       if (relocs)
2616         {
2617           if (!add_dynamic_entry (DT_RELA, 0)
2618               || !add_dynamic_entry (DT_RELASZ, 0)
2619               || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
2620             return FALSE;
2621
2622           /* If any dynamic relocs apply to a read-only section,
2623              then we need a DT_TEXTREL entry.  */
2624           if ((info->flags & DF_TEXTREL) == 0)
2625             elf_link_hash_traverse (&htab->elf, 
2626                                     elf_x86_64_readonly_dynrelocs,
2627                                     info);
2628
2629           if ((info->flags & DF_TEXTREL) != 0)
2630             {
2631               if (!add_dynamic_entry (DT_TEXTREL, 0))
2632                 return FALSE;
2633             }
2634         }
2635     }
2636 #undef add_dynamic_entry
2637
2638   return TRUE;
2639 }
2640
2641 static bfd_boolean
2642 elf_x86_64_always_size_sections (bfd *output_bfd,
2643                                  struct bfd_link_info *info)
2644 {
2645   asection *tls_sec = elf_hash_table (info)->tls_sec;
2646
2647   if (tls_sec)
2648     {
2649       struct elf_link_hash_entry *tlsbase;
2650
2651       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2652                                       "_TLS_MODULE_BASE_",
2653                                       FALSE, FALSE, FALSE);
2654
2655       if (tlsbase && tlsbase->type == STT_TLS)
2656         {
2657           struct elf_x86_64_link_hash_table *htab;
2658           struct bfd_link_hash_entry *bh = NULL;
2659           const struct elf_backend_data *bed
2660             = get_elf_backend_data (output_bfd);
2661
2662           htab = elf_x86_64_hash_table (info);
2663           if (htab == NULL)
2664             return FALSE;
2665
2666           if (!(_bfd_generic_link_add_one_symbol
2667                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2668                  tls_sec, 0, NULL, FALSE,
2669                  bed->collect, &bh)))
2670             return FALSE;
2671
2672           htab->tls_module_base = bh;
2673
2674           tlsbase = (struct elf_link_hash_entry *)bh;
2675           tlsbase->def_regular = 1;
2676           tlsbase->other = STV_HIDDEN;
2677           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2678         }
2679     }
2680
2681   return TRUE;
2682 }
2683
2684 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2685    executables.  Rather than setting it to the beginning of the TLS
2686    section, we have to set it to the end.  This function may be called
2687    multiple times, it is idempotent.  */
2688
2689 static void
2690 elf_x86_64_set_tls_module_base (struct bfd_link_info *info)
2691 {
2692   struct elf_x86_64_link_hash_table *htab;
2693   struct bfd_link_hash_entry *base;
2694
2695   if (!info->executable)
2696     return;
2697
2698   htab = elf_x86_64_hash_table (info);
2699   if (htab == NULL)
2700     return;
2701
2702   base = htab->tls_module_base;
2703   if (base == NULL)
2704     return;
2705
2706   base->u.def.value = htab->elf.tls_size;
2707 }
2708
2709 /* Return the base VMA address which should be subtracted from real addresses
2710    when resolving @dtpoff relocation.
2711    This is PT_TLS segment p_vaddr.  */
2712
2713 static bfd_vma
2714 elf_x86_64_dtpoff_base (struct bfd_link_info *info)
2715 {
2716   /* If tls_sec is NULL, we should have signalled an error already.  */
2717   if (elf_hash_table (info)->tls_sec == NULL)
2718     return 0;
2719   return elf_hash_table (info)->tls_sec->vma;
2720 }
2721
2722 /* Return the relocation value for @tpoff relocation
2723    if STT_TLS virtual address is ADDRESS.  */
2724
2725 static bfd_vma
2726 elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2727 {
2728   struct elf_link_hash_table *htab = elf_hash_table (info);
2729   const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2730   bfd_vma static_tls_size;
2731
2732   /* If tls_segment is NULL, we should have signalled an error already.  */
2733   if (htab->tls_sec == NULL)
2734     return 0;
2735
2736   /* Consider special static TLS alignment requirements.  */
2737   static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2738   return address - static_tls_size - htab->tls_sec->vma;
2739 }
2740
2741 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2742    branch?  */
2743
2744 static bfd_boolean
2745 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2746 {
2747   /* Opcode             Instruction
2748      0xe8               call
2749      0xe9               jump
2750      0x0f 0x8x          conditional jump */
2751   return ((offset > 0
2752            && (contents [offset - 1] == 0xe8
2753                || contents [offset - 1] == 0xe9))
2754           || (offset > 1
2755               && contents [offset - 2] == 0x0f
2756               && (contents [offset - 1] & 0xf0) == 0x80));
2757 }
2758
2759 /* Relocate an x86_64 ELF section.  */
2760
2761 static bfd_boolean
2762 elf_x86_64_relocate_section (bfd *output_bfd,
2763                              struct bfd_link_info *info,
2764                              bfd *input_bfd,
2765                              asection *input_section,
2766                              bfd_byte *contents,
2767                              Elf_Internal_Rela *relocs,
2768                              Elf_Internal_Sym *local_syms,
2769                              asection **local_sections)
2770 {
2771   struct elf_x86_64_link_hash_table *htab;
2772   Elf_Internal_Shdr *symtab_hdr;
2773   struct elf_link_hash_entry **sym_hashes;
2774   bfd_vma *local_got_offsets;
2775   bfd_vma *local_tlsdesc_gotents;
2776   Elf_Internal_Rela *rel;
2777   Elf_Internal_Rela *relend;
2778
2779   BFD_ASSERT (is_x86_64_elf (input_bfd));
2780
2781   htab = elf_x86_64_hash_table (info);
2782   if (htab == NULL)
2783     return FALSE;
2784   symtab_hdr = &elf_symtab_hdr (input_bfd);
2785   sym_hashes = elf_sym_hashes (input_bfd);
2786   local_got_offsets = elf_local_got_offsets (input_bfd);
2787   local_tlsdesc_gotents = elf_x86_64_local_tlsdesc_gotent (input_bfd);
2788
2789   elf_x86_64_set_tls_module_base (info);
2790
2791   rel = relocs;
2792   relend = relocs + input_section->reloc_count;
2793   for (; rel < relend; rel++)
2794     {
2795       unsigned int r_type;
2796       reloc_howto_type *howto;
2797       unsigned long r_symndx;
2798       struct elf_link_hash_entry *h;
2799       Elf_Internal_Sym *sym;
2800       asection *sec;
2801       bfd_vma off, offplt;
2802       bfd_vma relocation;
2803       bfd_boolean unresolved_reloc;
2804       bfd_reloc_status_type r;
2805       int tls_type;
2806       asection *base_got;
2807
2808       r_type = ELF32_R_TYPE (rel->r_info);
2809       if (r_type == (int) R_X86_64_GNU_VTINHERIT
2810           || r_type == (int) R_X86_64_GNU_VTENTRY)
2811         continue;
2812
2813       if (r_type >= R_X86_64_max)
2814         {
2815           bfd_set_error (bfd_error_bad_value);
2816           return FALSE;
2817         }
2818
2819       howto = x86_64_elf_howto_table + r_type;
2820       r_symndx = htab->r_sym (rel->r_info);
2821       h = NULL;
2822       sym = NULL;
2823       sec = NULL;
2824       unresolved_reloc = FALSE;
2825       if (r_symndx < symtab_hdr->sh_info)
2826         {
2827           sym = local_syms + r_symndx;
2828           sec = local_sections[r_symndx];
2829
2830           relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2831                                                 &sec, rel);
2832
2833           /* Relocate against local STT_GNU_IFUNC symbol.  */
2834           if (!info->relocatable
2835               && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2836             {
2837               h = elf_x86_64_get_local_sym_hash (htab, input_bfd,
2838                                                  rel, FALSE);
2839               if (h == NULL)
2840                 abort ();
2841
2842               /* Set STT_GNU_IFUNC symbol value.  */ 
2843               h->root.u.def.value = sym->st_value;
2844               h->root.u.def.section = sec;
2845             }
2846         }
2847       else
2848         {
2849           bfd_boolean warned ATTRIBUTE_UNUSED;
2850
2851           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2852                                    r_symndx, symtab_hdr, sym_hashes,
2853                                    h, sec, relocation,
2854                                    unresolved_reloc, warned);
2855         }
2856
2857       if (sec != NULL && elf_discarded_section (sec))
2858         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2859                                          rel, relend, howto, contents);
2860
2861       if (info->relocatable)
2862         continue;
2863
2864       /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2865          it here if it is defined in a non-shared object.  */
2866       if (h != NULL
2867           && h->type == STT_GNU_IFUNC
2868           && h->def_regular)
2869         {
2870           asection *plt;
2871           bfd_vma plt_index;
2872           const char *name;
2873
2874           if ((input_section->flags & SEC_ALLOC) == 0
2875               || h->plt.offset == (bfd_vma) -1)
2876             abort ();
2877
2878           /* STT_GNU_IFUNC symbol must go through PLT.  */
2879           plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2880           relocation = (plt->output_section->vma
2881                         + plt->output_offset + h->plt.offset);
2882
2883           switch (r_type)
2884             {
2885             default:
2886               if (h->root.root.string)
2887                 name = h->root.root.string;
2888               else
2889                 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2890                                          NULL);
2891               (*_bfd_error_handler)
2892                 (_("%B: relocation %s against STT_GNU_IFUNC "
2893                    "symbol `%s' isn't handled by %s"), input_bfd,
2894                  x86_64_elf_howto_table[r_type].name,
2895                  name, __FUNCTION__);
2896               bfd_set_error (bfd_error_bad_value);
2897               return FALSE;
2898
2899             case R_X86_64_32S:
2900               if (info->shared)
2901                 abort ();
2902               goto do_relocation;
2903
2904             case R_X86_64_32:
2905               if (ABI_64_P (output_bfd))
2906                 goto do_relocation;
2907               /* FALLTHROUGH */
2908             case R_X86_64_64: 
2909               if (rel->r_addend != 0)
2910                 {
2911                   if (h->root.root.string)
2912                     name = h->root.root.string;
2913                   else
2914                     name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2915                                              sym, NULL);
2916                   (*_bfd_error_handler)
2917                     (_("%B: relocation %s against STT_GNU_IFUNC "
2918                        "symbol `%s' has non-zero addend: %d"),
2919                      input_bfd, x86_64_elf_howto_table[r_type].name,
2920                      name, rel->r_addend);
2921                   bfd_set_error (bfd_error_bad_value);
2922                   return FALSE;
2923                 }
2924
2925               /* Generate dynamic relcoation only when there is a
2926                  non-GOF reference in a shared object.  */
2927               if (info->shared && h->non_got_ref)
2928                 {
2929                   Elf_Internal_Rela outrel;
2930                   asection *sreloc;
2931
2932                   /* Need a dynamic relocation to get the real function
2933                      address.  */
2934                   outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2935                                                              info,
2936                                                              input_section,
2937                                                              rel->r_offset);
2938                   if (outrel.r_offset == (bfd_vma) -1
2939                       || outrel.r_offset == (bfd_vma) -2)
2940                     abort ();
2941
2942                   outrel.r_offset += (input_section->output_section->vma
2943                                       + input_section->output_offset);
2944
2945                   if (h->dynindx == -1
2946                       || h->forced_local
2947                       || info->executable)
2948                     {
2949                       /* This symbol is resolved locally.  */
2950                       outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
2951                       outrel.r_addend = (h->root.u.def.value
2952                                          + h->root.u.def.section->output_section->vma
2953                                          + h->root.u.def.section->output_offset);
2954                     }
2955                   else
2956                     {
2957                       outrel.r_info = htab->r_info (h->dynindx, r_type);
2958                       outrel.r_addend = 0;
2959                     }
2960
2961                   sreloc = htab->elf.irelifunc;
2962                   elf_append_rela (output_bfd, sreloc, &outrel);
2963
2964                   /* If this reloc is against an external symbol, we
2965                      do not want to fiddle with the addend.  Otherwise,
2966                      we need to include the symbol value so that it
2967                      becomes an addend for the dynamic reloc.  For an
2968                      internal symbol, we have updated addend.  */
2969                   continue;
2970                 }
2971               /* FALLTHROUGH */
2972             case R_X86_64_PC32:
2973             case R_X86_64_PC64:
2974             case R_X86_64_PLT32:
2975               goto do_relocation;
2976
2977             case R_X86_64_GOTPCREL:
2978             case R_X86_64_GOTPCREL64:
2979               base_got = htab->elf.sgot;
2980               off = h->got.offset;
2981
2982               if (base_got == NULL)
2983                 abort ();
2984
2985               if (off == (bfd_vma) -1)
2986                 {
2987                   /* We can't use h->got.offset here to save state, or
2988                      even just remember the offset, as finish_dynamic_symbol
2989                      would use that as offset into .got.  */
2990
2991                   if (htab->elf.splt != NULL)
2992                     {
2993                       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2994                       off = (plt_index + 3) * GOT_ENTRY_SIZE;
2995                       base_got = htab->elf.sgotplt;
2996                     }
2997                   else
2998                     {
2999                       plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3000                       off = plt_index * GOT_ENTRY_SIZE;
3001                       base_got = htab->elf.igotplt;
3002                     }
3003
3004                   if (h->dynindx == -1
3005                       || h->forced_local
3006                       || info->symbolic)
3007                     {
3008                       /* This references the local defitionion.  We must 
3009                          initialize this entry in the global offset table.
3010                          Since the offset must always be a multiple of 8, 
3011                          we use the least significant bit to record
3012                          whether we have initialized it already.
3013
3014                          When doing a dynamic link, we create a .rela.got
3015                          relocation entry to initialize the value.  This
3016                          is done in the finish_dynamic_symbol routine.   */
3017                       if ((off & 1) != 0)
3018                         off &= ~1;
3019                       else
3020                         {
3021                           bfd_put_64 (output_bfd, relocation,
3022                                       base_got->contents + off);
3023                           /* Note that this is harmless for the GOTPLT64
3024                              case, as -1 | 1 still is -1.  */
3025                           h->got.offset |= 1;
3026                         }
3027                     }
3028                 }
3029
3030               relocation = (base_got->output_section->vma
3031                             + base_got->output_offset + off);
3032
3033               goto do_relocation;
3034             }
3035         }
3036
3037       /* When generating a shared object, the relocations handled here are
3038          copied into the output file to be resolved at run time.  */
3039       switch (r_type)
3040         {
3041         case R_X86_64_GOT32:
3042         case R_X86_64_GOT64:
3043           /* Relocation is to the entry for this symbol in the global
3044              offset table.  */
3045         case R_X86_64_GOTPCREL:
3046         case R_X86_64_GOTPCREL64:
3047           /* Use global offset table entry as symbol value.  */
3048         case R_X86_64_GOTPLT64:
3049           /* This is the same as GOT64 for relocation purposes, but
3050              indicates the existence of a PLT entry.  The difficulty is,
3051              that we must calculate the GOT slot offset from the PLT
3052              offset, if this symbol got a PLT entry (it was global).
3053              Additionally if it's computed from the PLT entry, then that
3054              GOT offset is relative to .got.plt, not to .got.  */
3055           base_got = htab->elf.sgot;
3056
3057           if (htab->elf.sgot == NULL)
3058             abort ();
3059
3060           if (h != NULL)
3061             {
3062               bfd_boolean dyn;
3063
3064               off = h->got.offset;
3065               if (h->needs_plt
3066                   && h->plt.offset != (bfd_vma)-1
3067                   && off == (bfd_vma)-1)
3068                 {
3069                   /* We can't use h->got.offset here to save
3070                      state, or even just remember the offset, as
3071                      finish_dynamic_symbol would use that as offset into
3072                      .got.  */
3073                   bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3074                   off = (plt_index + 3) * GOT_ENTRY_SIZE;
3075                   base_got = htab->elf.sgotplt;
3076                 }
3077
3078               dyn = htab->elf.dynamic_sections_created;
3079
3080               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3081                   || (info->shared
3082                       && SYMBOL_REFERENCES_LOCAL (info, h))
3083                   || (ELF_ST_VISIBILITY (h->other)
3084                       && h->root.type == bfd_link_hash_undefweak))
3085                 {
3086                   /* This is actually a static link, or it is a -Bsymbolic
3087                      link and the symbol is defined locally, or the symbol
3088                      was forced to be local because of a version file.  We
3089                      must initialize this entry in the global offset table.
3090                      Since the offset must always be a multiple of 8, we
3091                      use the least significant bit to record whether we
3092                      have initialized it already.
3093
3094                      When doing a dynamic link, we create a .rela.got
3095                      relocation entry to initialize the value.  This is
3096                      done in the finish_dynamic_symbol routine.  */
3097                   if ((off & 1) != 0)
3098                     off &= ~1;
3099                   else
3100                     {
3101                       bfd_put_64 (output_bfd, relocation,
3102                                   base_got->contents + off);
3103                       /* Note that this is harmless for the GOTPLT64 case,
3104                          as -1 | 1 still is -1.  */
3105                       h->got.offset |= 1;
3106                     }
3107                 }
3108               else
3109                 unresolved_reloc = FALSE;
3110             }
3111           else
3112             {
3113               if (local_got_offsets == NULL)
3114                 abort ();
3115
3116               off = local_got_offsets[r_symndx];
3117
3118               /* The offset must always be a multiple of 8.  We use
3119                  the least significant bit to record whether we have
3120                  already generated the necessary reloc.  */
3121               if ((off & 1) != 0)
3122                 off &= ~1;
3123               else
3124                 {
3125                   bfd_put_64 (output_bfd, relocation,
3126                               base_got->contents + off);
3127
3128                   if (info->shared)
3129                     {
3130                       asection *s;
3131                       Elf_Internal_Rela outrel;
3132
3133                       /* We need to generate a R_X86_64_RELATIVE reloc
3134                          for the dynamic linker.  */
3135                       s = htab->elf.srelgot;
3136                       if (s == NULL)
3137                         abort ();
3138
3139                       outrel.r_offset = (base_got->output_section->vma
3140                                          + base_got->output_offset
3141                                          + off);
3142                       outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3143                       outrel.r_addend = relocation;
3144                       elf_append_rela (output_bfd, s, &outrel);
3145                     }
3146
3147                   local_got_offsets[r_symndx] |= 1;
3148                 }
3149             }
3150
3151           if (off >= (bfd_vma) -2)
3152             abort ();
3153
3154           relocation = base_got->output_section->vma
3155                        + base_got->output_offset + off;
3156           if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
3157             relocation -= htab->elf.sgotplt->output_section->vma
3158                           - htab->elf.sgotplt->output_offset;
3159
3160           break;
3161
3162         case R_X86_64_GOTOFF64:
3163           /* Relocation is relative to the start of the global offset
3164              table.  */
3165
3166           /* Check to make sure it isn't a protected function symbol
3167              for shared library since it may not be local when used
3168              as function address.  */
3169           if (info->shared
3170               && h
3171               && h->def_regular
3172               && h->type == STT_FUNC
3173               && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3174             {
3175               (*_bfd_error_handler)
3176                 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3177                  input_bfd, h->root.root.string);
3178               bfd_set_error (bfd_error_bad_value);
3179               return FALSE;
3180             }
3181
3182           /* Note that sgot is not involved in this
3183              calculation.  We always want the start of .got.plt.  If we
3184              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3185              permitted by the ABI, we might have to change this
3186              calculation.  */
3187           relocation -= htab->elf.sgotplt->output_section->vma
3188                         + htab->elf.sgotplt->output_offset;
3189           break;
3190
3191         case R_X86_64_GOTPC32:
3192         case R_X86_64_GOTPC64:
3193           /* Use global offset table as symbol value.  */
3194           relocation = htab->elf.sgotplt->output_section->vma
3195                        + htab->elf.sgotplt->output_offset;
3196           unresolved_reloc = FALSE;
3197           break;
3198
3199         case R_X86_64_PLTOFF64:
3200           /* Relocation is PLT entry relative to GOT.  For local
3201              symbols it's the symbol itself relative to GOT.  */
3202           if (h != NULL
3203               /* See PLT32 handling.  */
3204               && h->plt.offset != (bfd_vma) -1
3205               && htab->elf.splt != NULL)
3206             {
3207               relocation = (htab->elf.splt->output_section->vma
3208                             + htab->elf.splt->output_offset
3209                             + h->plt.offset);
3210               unresolved_reloc = FALSE;
3211             }
3212
3213           relocation -= htab->elf.sgotplt->output_section->vma
3214                         + htab->elf.sgotplt->output_offset;
3215           break;
3216
3217         case R_X86_64_PLT32:
3218           /* Relocation is to the entry for this symbol in the
3219              procedure linkage table.  */
3220
3221           /* Resolve a PLT32 reloc against a local symbol directly,
3222              without using the procedure linkage table.  */
3223           if (h == NULL)
3224             break;
3225
3226           if (h->plt.offset == (bfd_vma) -1
3227               || htab->elf.splt == NULL)
3228             {
3229               /* We didn't make a PLT entry for this symbol.  This
3230                  happens when statically linking PIC code, or when
3231                  using -Bsymbolic.  */
3232               break;
3233             }
3234
3235           relocation = (htab->elf.splt->output_section->vma
3236                         + htab->elf.splt->output_offset
3237                         + h->plt.offset);
3238           unresolved_reloc = FALSE;
3239           break;
3240
3241         case R_X86_64_PC8:
3242         case R_X86_64_PC16:
3243         case R_X86_64_PC32:
3244           if (info->shared
3245               && ABI_64_P (output_bfd)
3246               && (input_section->flags & SEC_ALLOC) != 0
3247               && (input_section->flags & SEC_READONLY) != 0
3248               && h != NULL)
3249             {
3250               bfd_boolean fail = FALSE;
3251               bfd_boolean branch
3252                 = (r_type == R_X86_64_PC32
3253                    && is_32bit_relative_branch (contents, rel->r_offset));
3254
3255               if (SYMBOL_REFERENCES_LOCAL (info, h))
3256                 {
3257                   /* Symbol is referenced locally.  Make sure it is
3258                      defined locally or for a branch.  */
3259                   fail = !h->def_regular && !branch;
3260                 }
3261               else
3262                 {
3263                   /* Symbol isn't referenced locally.  We only allow
3264                      branch to symbol with non-default visibility. */
3265                   fail = (!branch
3266                           || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3267                 }
3268
3269               if (fail)
3270                 {
3271                   const char *fmt;
3272                   const char *v;
3273                   const char *pic = "";
3274
3275                   switch (ELF_ST_VISIBILITY (h->other))
3276                     {
3277                     case STV_HIDDEN:
3278                       v = _("hidden symbol");
3279                       break;
3280                     case STV_INTERNAL:
3281                       v = _("internal symbol");
3282                       break;
3283                     case STV_PROTECTED:
3284                       v = _("protected symbol");
3285                       break;
3286                     default:
3287                       v = _("symbol");
3288                       pic = _("; recompile with -fPIC");
3289                       break;
3290                     }
3291
3292                   if (h->def_regular)
3293                     fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3294                   else
3295                     fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3296
3297                   (*_bfd_error_handler) (fmt, input_bfd,
3298                                          x86_64_elf_howto_table[r_type].name,
3299                                          v,  h->root.root.string, pic);
3300                   bfd_set_error (bfd_error_bad_value);
3301                   return FALSE;
3302                 }
3303             }
3304           /* Fall through.  */
3305
3306         case R_X86_64_8:
3307         case R_X86_64_16:
3308         case R_X86_64_32:
3309         case R_X86_64_PC64:
3310         case R_X86_64_64:
3311           /* FIXME: The ABI says the linker should make sure the value is
3312              the same when it's zeroextended to 64 bit.  */
3313
3314           if ((input_section->flags & SEC_ALLOC) == 0)
3315             break;
3316
3317           if ((info->shared
3318                && (h == NULL
3319                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3320                    || h->root.type != bfd_link_hash_undefweak)
3321                && (! IS_X86_64_PCREL_TYPE (r_type)
3322                    || ! SYMBOL_CALLS_LOCAL (info, h)))
3323               || (ELIMINATE_COPY_RELOCS
3324                   && !info->shared
3325                   && h != NULL
3326                   && h->dynindx != -1
3327                   && !h->non_got_ref
3328                   && ((h->def_dynamic
3329                        && !h->def_regular)
3330                       || h->root.type == bfd_link_hash_undefweak
3331                       || h->root.type == bfd_link_hash_undefined)))
3332             {
3333               Elf_Internal_Rela outrel;
3334               bfd_boolean skip, relocate;
3335               asection *sreloc;
3336
3337               /* When generating a shared object, these relocations
3338                  are copied into the output file to be resolved at run
3339                  time.  */
3340               skip = FALSE;
3341               relocate = FALSE;
3342
3343               outrel.r_offset =
3344                 _bfd_elf_section_offset (output_bfd, info, input_section,
3345                                          rel->r_offset);
3346               if (outrel.r_offset == (bfd_vma) -1)
3347                 skip = TRUE;
3348               else if (outrel.r_offset == (bfd_vma) -2)
3349                 skip = TRUE, relocate = TRUE;
3350
3351               outrel.r_offset += (input_section->output_section->vma
3352                                   + input_section->output_offset);
3353
3354               if (skip)
3355                 memset (&outrel, 0, sizeof outrel);
3356
3357               /* h->dynindx may be -1 if this symbol was marked to
3358                  become local.  */
3359               else if (h != NULL
3360                        && h->dynindx != -1
3361                        && (IS_X86_64_PCREL_TYPE (r_type)
3362                            || ! info->shared
3363                            || ! SYMBOLIC_BIND (info, h)
3364                            || ! h->def_regular))
3365                 {
3366                   outrel.r_info = htab->r_info (h->dynindx, r_type);
3367                   outrel.r_addend = rel->r_addend;
3368                 }
3369               else
3370                 {
3371                   /* This symbol is local, or marked to become local.  */
3372                   if (r_type == htab->pointer_r_type)
3373                     {
3374                       relocate = TRUE;
3375                       outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3376                       outrel.r_addend = relocation + rel->r_addend;
3377                     }
3378                   else
3379                     {
3380                       long sindx;
3381
3382                       if (bfd_is_abs_section (sec))
3383                         sindx = 0;
3384                       else if (sec == NULL || sec->owner == NULL)
3385                         {
3386                           bfd_set_error (bfd_error_bad_value);
3387                           return FALSE;
3388                         }
3389                       else
3390                         {
3391                           asection *osec;
3392
3393                           /* We are turning this relocation into one
3394                              against a section symbol.  It would be
3395                              proper to subtract the symbol's value,
3396                              osec->vma, from the emitted reloc addend,
3397                              but ld.so expects buggy relocs.  */
3398                           osec = sec->output_section;
3399                           sindx = elf_section_data (osec)->dynindx;
3400                           if (sindx == 0)
3401                             {
3402                               asection *oi = htab->elf.text_index_section;
3403                               sindx = elf_section_data (oi)->dynindx;
3404                             }
3405                           BFD_ASSERT (sindx != 0);
3406                         }
3407
3408                       outrel.r_info = htab->r_info (sindx, r_type);
3409                       outrel.r_addend = relocation + rel->r_addend;
3410                     }
3411                 }
3412
3413               sreloc = elf_section_data (input_section)->sreloc;
3414
3415               if (sreloc == NULL || sreloc->contents == NULL)
3416                 {
3417                   r = bfd_reloc_notsupported;
3418                   goto check_relocation_error;
3419                 }
3420
3421               elf_append_rela (output_bfd, sreloc, &outrel);
3422
3423               /* If this reloc is against an external symbol, we do
3424                  not want to fiddle with the addend.  Otherwise, we
3425                  need to include the symbol value so that it becomes
3426                  an addend for the dynamic reloc.  */
3427               if (! relocate)
3428                 continue;
3429             }
3430
3431           break;
3432
3433         case R_X86_64_TLSGD:
3434         case R_X86_64_GOTPC32_TLSDESC:
3435         case R_X86_64_TLSDESC_CALL:
3436         case R_X86_64_GOTTPOFF:
3437           tls_type = GOT_UNKNOWN;
3438           if (h == NULL && local_got_offsets)
3439             tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3440           else if (h != NULL)
3441             tls_type = elf_x86_64_hash_entry (h)->tls_type;
3442
3443           if (! elf_x86_64_tls_transition (info, input_bfd,
3444                                            input_section, contents,
3445                                            symtab_hdr, sym_hashes,
3446                                            &r_type, tls_type, rel,
3447                                            relend, h, r_symndx))
3448             return FALSE;
3449
3450           if (r_type == R_X86_64_TPOFF32)
3451             {
3452               bfd_vma roff = rel->r_offset;
3453
3454               BFD_ASSERT (! unresolved_reloc);
3455
3456               if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3457                 {
3458                   /* GD->LE transition.  For 64bit, change
3459                      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3460                      .word 0x6666; rex64; call __tls_get_addr
3461                      into:
3462                      movq %fs:0, %rax
3463                      leaq foo@tpoff(%rax), %rax
3464                      For 32bit, change
3465                      leaq foo@tlsgd(%rip), %rdi
3466                      .word 0x6666; rex64; call __tls_get_addr
3467                      into:
3468                      movl %fs:0, %eax
3469                      leaq foo@tpoff(%rax), %rax */
3470                   if (ABI_64_P (output_bfd))
3471                     memcpy (contents + roff - 4,
3472                             "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3473                             16);
3474                   else
3475                     memcpy (contents + roff - 3,
3476                             "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3477                             15);
3478                   bfd_put_32 (output_bfd,
3479                               elf_x86_64_tpoff (info, relocation),
3480                               contents + roff + 8);
3481                   /* Skip R_X86_64_PC32/R_X86_64_PLT32.  */
3482                   rel++;
3483                   continue;
3484                 }
3485               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3486                 {
3487                   /* GDesc -> LE transition.
3488                      It's originally something like:
3489                      leaq x@tlsdesc(%rip), %rax
3490
3491                      Change it to:
3492                      movl $x@tpoff, %rax.  */
3493
3494                   unsigned int val, type;
3495
3496                   type = bfd_get_8 (input_bfd, contents + roff - 3);
3497                   val = bfd_get_8 (input_bfd, contents + roff - 1);
3498                   bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3499                              contents + roff - 3);
3500                   bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3501                   bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3502                              contents + roff - 1);
3503                   bfd_put_32 (output_bfd,
3504                               elf_x86_64_tpoff (info, relocation),
3505                               contents + roff);
3506                   continue;
3507                 }
3508               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3509                 {
3510                   /* GDesc -> LE transition.
3511                      It's originally:
3512                      call *(%rax)
3513                      Turn it into:
3514                      xchg %ax,%ax.  */
3515                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3516                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3517                   continue;
3518                 }
3519               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3520                 {
3521                   /* IE->LE transition:
3522                      Originally it can be one of:
3523                      movq foo@gottpoff(%rip), %reg
3524                      addq foo@gottpoff(%rip), %reg
3525                      We change it into:
3526                      movq $foo, %reg
3527                      leaq foo(%reg), %reg
3528                      addq $foo, %reg.  */
3529
3530                   unsigned int val, type, reg;
3531
3532                   val = bfd_get_8 (input_bfd, contents + roff - 3);
3533                   type = bfd_get_8 (input_bfd, contents + roff - 2);
3534                   reg = bfd_get_8 (input_bfd, contents + roff - 1);
3535                   reg >>= 3;
3536                   if (type == 0x8b)
3537                     {
3538                       /* movq */
3539                       if (val == 0x4c)
3540                         bfd_put_8 (output_bfd, 0x49,
3541                                    contents + roff - 3);
3542                       else if (!ABI_64_P (output_bfd) && val == 0x44)
3543                         bfd_put_8 (output_bfd, 0x41,
3544                                    contents + roff - 3);
3545                       bfd_put_8 (output_bfd, 0xc7,
3546                                  contents + roff - 2);
3547                       bfd_put_8 (output_bfd, 0xc0 | reg,
3548                                  contents + roff - 1);
3549                     }
3550                   else if (reg == 4)
3551                     {
3552                       /* addq -> addq - addressing with %rsp/%r12 is
3553                          special  */
3554                       if (val == 0x4c)
3555                         bfd_put_8 (output_bfd, 0x49,
3556                                    contents + roff - 3);
3557                       else if (!ABI_64_P (output_bfd) && val == 0x44)
3558                         bfd_put_8 (output_bfd, 0x41,
3559                                    contents + roff - 3);
3560                       bfd_put_8 (output_bfd, 0x81,
3561                                  contents + roff - 2);
3562                       bfd_put_8 (output_bfd, 0xc0 | reg,
3563                                  contents + roff - 1);
3564                     }
3565                   else
3566                     {
3567                       /* addq -> leaq */
3568                       if (val == 0x4c)
3569                         bfd_put_8 (output_bfd, 0x4d,
3570                                    contents + roff - 3);
3571                       else if (!ABI_64_P (output_bfd) && val == 0x44)
3572                         bfd_put_8 (output_bfd, 0x45,
3573                                    contents + roff - 3);
3574                       bfd_put_8 (output_bfd, 0x8d,
3575                                  contents + roff - 2);
3576                       bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3577                                  contents + roff - 1);
3578                     }
3579                   bfd_put_32 (output_bfd,
3580                               elf_x86_64_tpoff (info, relocation),
3581                               contents + roff);
3582                   continue;
3583                 }
3584               else
3585                 BFD_ASSERT (FALSE);
3586             }
3587
3588           if (htab->elf.sgot == NULL)
3589             abort ();
3590
3591           if (h != NULL)
3592             {
3593               off = h->got.offset;
3594               offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
3595             }
3596           else
3597             {
3598               if (local_got_offsets == NULL)
3599                 abort ();
3600
3601               off = local_got_offsets[r_symndx];
3602               offplt = local_tlsdesc_gotents[r_symndx];
3603             }
3604
3605           if ((off & 1) != 0)
3606             off &= ~1;
3607           else
3608             {
3609               Elf_Internal_Rela outrel;
3610               int dr_type, indx;
3611               asection *sreloc;
3612
3613               if (htab->elf.srelgot == NULL)
3614                 abort ();
3615
3616               indx = h && h->dynindx != -1 ? h->dynindx : 0;
3617
3618               if (GOT_TLS_GDESC_P (tls_type))
3619                 {
3620                   outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
3621                   BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3622                               + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3623                   outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3624                                      + htab->elf.sgotplt->output_offset
3625                                      + offplt
3626                                      + htab->sgotplt_jump_table_size);
3627                   sreloc = htab->elf.srelplt;
3628                   if (indx == 0)
3629                     outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
3630                   else
3631                     outrel.r_addend = 0;
3632                   elf_append_rela (output_bfd, sreloc, &outrel);
3633                 }
3634
3635               sreloc = htab->elf.srelgot;
3636
3637               outrel.r_offset = (htab->elf.sgot->output_section->vma
3638                                  + htab->elf.sgot->output_offset + off);
3639
3640               if (GOT_TLS_GD_P (tls_type))
3641                 dr_type = R_X86_64_DTPMOD64;
3642               else if (GOT_TLS_GDESC_P (tls_type))
3643                 goto dr_done;
3644               else
3645                 dr_type = R_X86_64_TPOFF64;
3646
3647               bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3648               outrel.r_addend = 0;
3649               if ((dr_type == R_X86_64_TPOFF64
3650                    || dr_type == R_X86_64_TLSDESC) && indx == 0)
3651                 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
3652               outrel.r_info = htab->r_info (indx, dr_type);
3653
3654               elf_append_rela (output_bfd, sreloc, &outrel);
3655
3656               if (GOT_TLS_GD_P (tls_type))
3657                 {
3658                   if (indx == 0)
3659                     {
3660                       BFD_ASSERT (! unresolved_reloc);
3661                       bfd_put_64 (output_bfd,
3662                                   relocation - elf_x86_64_dtpoff_base (info),
3663                                   htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3664                     }
3665                   else
3666                     {
3667                       bfd_put_64 (output_bfd, 0,
3668                                   htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3669                       outrel.r_info = htab->r_info (indx,
3670                                                     R_X86_64_DTPOFF64);
3671                       outrel.r_offset += GOT_ENTRY_SIZE;
3672                       elf_append_rela (output_bfd, sreloc,
3673                                                 &outrel);
3674                     }
3675                 }
3676
3677             dr_done:
3678               if (h != NULL)
3679                 h->got.offset |= 1;
3680               else
3681                 local_got_offsets[r_symndx] |= 1;
3682             }
3683
3684           if (off >= (bfd_vma) -2
3685               && ! GOT_TLS_GDESC_P (tls_type))
3686             abort ();
3687           if (r_type == ELF32_R_TYPE (rel->r_info))
3688             {
3689               if (r_type == R_X86_64_GOTPC32_TLSDESC
3690                   || r_type == R_X86_64_TLSDESC_CALL)
3691                 relocation = htab->elf.sgotplt->output_section->vma
3692                   + htab->elf.sgotplt->output_offset
3693                   + offplt + htab->sgotplt_jump_table_size;
3694               else
3695                 relocation = htab->elf.sgot->output_section->vma
3696                   + htab->elf.sgot->output_offset + off;
3697               unresolved_reloc = FALSE;
3698             }
3699           else
3700             {
3701               bfd_vma roff = rel->r_offset;
3702
3703               if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3704                 {
3705                   /* GD->IE transition.  For 64bit, change
3706                      .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3707                      .word 0x6666; rex64; call __tls_get_addr@plt
3708                      into:
3709                      movq %fs:0, %rax
3710                      addq foo@gottpoff(%rip), %rax
3711                      For 32bit, change
3712                      leaq foo@tlsgd(%rip), %rdi
3713                      .word 0x6666; rex64; call __tls_get_addr@plt
3714                      into:
3715                      movl %fs:0, %eax
3716                      addq foo@gottpoff(%rip), %rax */
3717                   if (ABI_64_P (output_bfd))
3718                     memcpy (contents + roff - 4,
3719                             "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3720                             16);
3721                   else
3722                     memcpy (contents + roff - 3,
3723                             "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3724                             15);
3725
3726                   relocation = (htab->elf.sgot->output_section->vma
3727                                 + htab->elf.sgot->output_offset + off
3728                                 - roff
3729                                 - input_section->output_section->vma
3730                                 - input_section->output_offset
3731                                 - 12);
3732                   bfd_put_32 (output_bfd, relocation,
3733                               contents + roff + 8);
3734                   /* Skip R_X86_64_PLT32.  */
3735                   rel++;
3736                   continue;
3737                 }
3738               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3739                 {
3740                   /* GDesc -> IE transition.
3741                      It's originally something like:
3742                      leaq x@tlsdesc(%rip), %rax
3743
3744                      Change it to:
3745                      movq x@gottpoff(%rip), %rax # before xchg %ax,%ax.  */
3746
3747                   /* Now modify the instruction as appropriate. To
3748                      turn a leaq into a movq in the form we use it, it
3749                      suffices to change the second byte from 0x8d to
3750                      0x8b.  */
3751                   bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3752
3753                   bfd_put_32 (output_bfd,
3754                               htab->elf.sgot->output_section->vma
3755                               + htab->elf.sgot->output_offset + off
3756                               - rel->r_offset
3757                               - input_section->output_section->vma
3758                               - input_section->output_offset
3759                               - 4,
3760                               contents + roff);
3761                   continue;
3762                 }
3763               else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3764                 {
3765                   /* GDesc -> IE transition.
3766                      It's originally:
3767                      call *(%rax)
3768
3769                      Change it to:
3770                      xchg %ax, %ax.  */
3771
3772                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3773                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3774                   continue;
3775                 }
3776               else
3777                 BFD_ASSERT (FALSE);
3778             }
3779           break;
3780
3781         case R_X86_64_TLSLD:
3782           if (! elf_x86_64_tls_transition (info, input_bfd,
3783                                            input_section, contents,
3784                                            symtab_hdr, sym_hashes,
3785                                            &r_type, GOT_UNKNOWN,
3786                                            rel, relend, h, r_symndx))
3787             return FALSE;
3788
3789           if (r_type != R_X86_64_TLSLD)
3790             {
3791               /* LD->LE transition:
3792                  leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3793                  For 64bit, we change it into:
3794                  .word 0x6666; .byte 0x66; movq %fs:0, %rax.
3795                  For 32bit, we change it into:
3796                  nopl 0x0(%rax); movl %fs:0, %eax.  */
3797
3798               BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3799               if (ABI_64_P (output_bfd))
3800                 memcpy (contents + rel->r_offset - 3,
3801                         "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3802               else
3803                 memcpy (contents + rel->r_offset - 3,
3804                         "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
3805               /* Skip R_X86_64_PC32/R_X86_64_PLT32.  */
3806               rel++;
3807               continue;
3808             }
3809
3810           if (htab->elf.sgot == NULL)
3811             abort ();
3812
3813           off = htab->tls_ld_got.offset;
3814           if (off & 1)
3815             off &= ~1;
3816           else
3817             {
3818               Elf_Internal_Rela outrel;
3819
3820               if (htab->elf.srelgot == NULL)
3821                 abort ();
3822
3823               outrel.r_offset = (htab->elf.sgot->output_section->vma
3824                                  + htab->elf.sgot->output_offset + off);
3825
3826               bfd_put_64 (output_bfd, 0,
3827                           htab->elf.sgot->contents + off);
3828               bfd_put_64 (output_bfd, 0,
3829                           htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3830               outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
3831               outrel.r_addend = 0;
3832               elf_append_rela (output_bfd, htab->elf.srelgot,
3833                                         &outrel);
3834               htab->tls_ld_got.offset |= 1;
3835             }
3836           relocation = htab->elf.sgot->output_section->vma
3837                        + htab->elf.sgot->output_offset + off;
3838           unresolved_reloc = FALSE;
3839           break;
3840
3841         case R_X86_64_DTPOFF32:
3842           if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
3843             relocation -= elf_x86_64_dtpoff_base (info);
3844           else
3845             relocation = elf_x86_64_tpoff (info, relocation);
3846           break;
3847
3848         case R_X86_64_TPOFF32:
3849           BFD_ASSERT (info->executable);
3850           relocation = elf_x86_64_tpoff (info, relocation);
3851           break;
3852
3853         default:
3854           break;
3855         }
3856
3857       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3858          because such sections are not SEC_ALLOC and thus ld.so will
3859          not process them.  */
3860       if (unresolved_reloc
3861           && !((input_section->flags & SEC_DEBUGGING) != 0
3862                && h->def_dynamic))
3863         (*_bfd_error_handler)
3864           (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3865            input_bfd,
3866            input_section,
3867            (long) rel->r_offset,
3868            howto->name,
3869            h->root.root.string);
3870
3871 do_relocation:
3872       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3873                                     contents, rel->r_offset,
3874                                     relocation, rel->r_addend);
3875
3876 check_relocation_error:
3877       if (r != bfd_reloc_ok)
3878         {
3879           const char *name;
3880
3881           if (h != NULL)
3882             name = h->root.root.string;
3883           else
3884             {
3885               name = bfd_elf_string_from_elf_section (input_bfd,
3886                                                       symtab_hdr->sh_link,
3887                                                       sym->st_name);
3888               if (name == NULL)
3889                 return FALSE;
3890               if (*name == '\0')
3891                 name = bfd_section_name (input_bfd, sec);
3892             }
3893
3894           if (r == bfd_reloc_overflow)
3895             {
3896               if (! ((*info->callbacks->reloc_overflow)
3897                      (info, (h ? &h->root : NULL), name, howto->name,
3898                       (bfd_vma) 0, input_bfd, input_section,
3899                       rel->r_offset)))
3900                 return FALSE;
3901             }
3902           else
3903             {
3904               (*_bfd_error_handler)
3905                 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3906                  input_bfd, input_section,
3907                  (long) rel->r_offset, name, (int) r);
3908               return FALSE;
3909             }
3910         }
3911     }
3912
3913   return TRUE;
3914 }
3915
3916 /* Finish up dynamic symbol handling.  We set the contents of various
3917    dynamic sections here.  */
3918
3919 static bfd_boolean
3920 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3921                                   struct bfd_link_info *info,
3922                                   struct elf_link_hash_entry *h,
3923                                   Elf_Internal_Sym *sym)
3924 {
3925   struct elf_x86_64_link_hash_table *htab;
3926
3927   htab = elf_x86_64_hash_table (info);
3928   if (htab == NULL)
3929     return FALSE;
3930
3931   if (h->plt.offset != (bfd_vma) -1)
3932     {
3933       bfd_vma plt_index;
3934       bfd_vma got_offset;
3935       Elf_Internal_Rela rela;
3936       bfd_byte *loc;
3937       asection *plt, *gotplt, *relplt;
3938       const struct elf_backend_data *bed;
3939
3940       /* When building a static executable, use .iplt, .igot.plt and
3941          .rela.iplt sections for STT_GNU_IFUNC symbols.  */
3942       if (htab->elf.splt != NULL)
3943         {
3944           plt = htab->elf.splt;
3945           gotplt = htab->elf.sgotplt;
3946           relplt = htab->elf.srelplt;
3947         }
3948       else
3949         {
3950           plt = htab->elf.iplt;
3951           gotplt = htab->elf.igotplt;
3952           relplt = htab->elf.irelplt;
3953         }
3954
3955       /* This symbol has an entry in the procedure linkage table.  Set
3956          it up.  */
3957       if ((h->dynindx == -1
3958            && !((h->forced_local || info->executable)
3959                 && h->def_regular
3960                 && h->type == STT_GNU_IFUNC))
3961           || plt == NULL
3962           || gotplt == NULL
3963           || relplt == NULL)
3964         abort ();
3965
3966       /* Get the index in the procedure linkage table which
3967          corresponds to this symbol.  This is the index of this symbol
3968          in all the symbols for which we are making plt entries.  The
3969          first entry in the procedure linkage table is reserved.
3970
3971          Get the offset into the .got table of the entry that
3972          corresponds to this function.  Each .got entry is GOT_ENTRY_SIZE
3973          bytes. The first three are reserved for the dynamic linker.
3974
3975          For static executables, we don't reserve anything.  */
3976
3977       if (plt == htab->elf.splt)
3978         {
3979           plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3980           got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3981         }
3982       else
3983         {
3984           plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3985           got_offset = plt_index * GOT_ENTRY_SIZE;
3986         }
3987
3988       /* Fill in the entry in the procedure linkage table.  */
3989       memcpy (plt->contents + h->plt.offset, elf_x86_64_plt_entry,
3990               PLT_ENTRY_SIZE);
3991
3992       /* Insert the relocation positions of the plt section.  The magic
3993          numbers at the end of the statements are the positions of the
3994          relocations in the plt section.  */
3995       /* Put offset for jmp *name@GOTPCREL(%rip), since the
3996          instruction uses 6 bytes, subtract this value.  */
3997       bfd_put_32 (output_bfd,
3998                       (gotplt->output_section->vma
3999                        + gotplt->output_offset
4000                        + got_offset
4001                        - plt->output_section->vma
4002                        - plt->output_offset
4003                        - h->plt.offset
4004                        - 6),
4005                   plt->contents + h->plt.offset + 2);
4006
4007       /* Don't fill PLT entry for static executables.  */
4008       if (plt == htab->elf.splt)
4009         {
4010           /* Put relocation index.  */
4011           bfd_put_32 (output_bfd, plt_index,
4012                       plt->contents + h->plt.offset + 7);
4013           /* Put offset for jmp .PLT0.  */
4014           bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
4015                       plt->contents + h->plt.offset + 12);
4016         }
4017
4018       /* Fill in the entry in the global offset table, initially this
4019          points to the pushq instruction in the PLT which is at offset 6.  */
4020       bfd_put_64 (output_bfd, (plt->output_section->vma
4021                                + plt->output_offset
4022                                + h->plt.offset + 6),
4023                   gotplt->contents + got_offset);
4024
4025       /* Fill in the entry in the .rela.plt section.  */
4026       rela.r_offset = (gotplt->output_section->vma
4027                        + gotplt->output_offset
4028                        + got_offset);
4029       if (h->dynindx == -1
4030           || ((info->executable
4031                || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4032               && h->def_regular
4033               && h->type == STT_GNU_IFUNC))
4034         {
4035           /* If an STT_GNU_IFUNC symbol is locally defined, generate
4036              R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT.  */
4037           rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4038           rela.r_addend = (h->root.u.def.value
4039                            + h->root.u.def.section->output_section->vma
4040                            + h->root.u.def.section->output_offset);
4041         }
4042       else
4043         {
4044           rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
4045           rela.r_addend = 0;
4046         }
4047
4048       bed = get_elf_backend_data (output_bfd);
4049       loc = relplt->contents + plt_index * bed->s->sizeof_rela;
4050       bed->s->swap_reloca_out (output_bfd, &rela, loc);
4051
4052       if (!h->def_regular)
4053         {
4054           /* Mark the symbol as undefined, rather than as defined in
4055              the .plt section.  Leave the value if there were any
4056              relocations where pointer equality matters (this is a clue
4057              for the dynamic linker, to make function pointer
4058              comparisons work between an application and shared
4059              library), otherwise set it to zero.  If a function is only
4060              called from a binary, there is no need to slow down
4061              shared libraries because of that.  */
4062           sym->st_shndx = SHN_UNDEF;
4063           if (!h->pointer_equality_needed)
4064             sym->st_value = 0;
4065         }
4066     }
4067
4068   if (h->got.offset != (bfd_vma) -1
4069       && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
4070       && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
4071     {
4072       Elf_Internal_Rela rela;
4073
4074       /* This symbol has an entry in the global offset table.  Set it
4075          up.  */
4076       if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4077         abort ();
4078
4079       rela.r_offset = (htab->elf.sgot->output_section->vma
4080                        + htab->elf.sgot->output_offset
4081                        + (h->got.offset &~ (bfd_vma) 1));
4082
4083       /* If this is a static link, or it is a -Bsymbolic link and the
4084          symbol is defined locally or was forced to be local because
4085          of a version file, we just want to emit a RELATIVE reloc.
4086          The entry in the global offset table will already have been
4087          initialized in the relocate_section function.  */
4088       if (h->def_regular
4089           && h->type == STT_GNU_IFUNC)
4090         {
4091           if (info->shared)
4092             {
4093               /* Generate R_X86_64_GLOB_DAT.  */
4094               goto do_glob_dat;
4095             }
4096           else
4097             {
4098               asection *plt;
4099
4100               if (!h->pointer_equality_needed)
4101                 abort ();
4102
4103               /* For non-shared object, we can't use .got.plt, which
4104                  contains the real function addres if we need pointer
4105                  equality.  We load the GOT entry with the PLT entry.  */
4106               plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4107               bfd_put_64 (output_bfd, (plt->output_section->vma
4108                                        + plt->output_offset
4109                                        + h->plt.offset),
4110                           htab->elf.sgot->contents + h->got.offset);
4111               return TRUE;
4112             }
4113         }
4114       else if (info->shared
4115                && SYMBOL_REFERENCES_LOCAL (info, h))
4116         {
4117           if (!h->def_regular)
4118             return FALSE;
4119           BFD_ASSERT((h->got.offset & 1) != 0);
4120           rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4121           rela.r_addend = (h->root.u.def.value
4122                            + h->root.u.def.section->output_section->vma
4123                            + h->root.u.def.section->output_offset);
4124         }
4125       else
4126         {
4127           BFD_ASSERT((h->got.offset & 1) == 0);
4128 do_glob_dat:
4129           bfd_put_64 (output_bfd, (bfd_vma) 0,
4130                       htab->elf.sgot->contents + h->got.offset);
4131           rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
4132           rela.r_addend = 0;
4133         }
4134
4135       elf_append_rela (output_bfd, htab->elf.srelgot, &rela);
4136     }
4137
4138   if (h->needs_copy)
4139     {
4140       Elf_Internal_Rela rela;
4141
4142       /* This symbol needs a copy reloc.  Set it up.  */
4143
4144       if (h->dynindx == -1
4145           || (h->root.type != bfd_link_hash_defined
4146               && h->root.type != bfd_link_hash_defweak)
4147           || htab->srelbss == NULL)
4148         abort ();
4149
4150       rela.r_offset = (h->root.u.def.value
4151                        + h->root.u.def.section->output_section->vma
4152                        + h->root.u.def.section->output_offset);
4153       rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
4154       rela.r_addend = 0;
4155       elf_append_rela (output_bfd, htab->srelbss, &rela);
4156     }
4157
4158   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
4159      be NULL for local symbols.  */
4160   if (sym != NULL
4161       && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4162           || h == htab->elf.hgot))
4163     sym->st_shndx = SHN_ABS;
4164
4165   return TRUE;
4166 }
4167
4168 /* Finish up local dynamic symbol handling.  We set the contents of
4169    various dynamic sections here.  */
4170
4171 static bfd_boolean
4172 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
4173 {
4174   struct elf_link_hash_entry *h
4175     = (struct elf_link_hash_entry *) *slot;
4176   struct bfd_link_info *info
4177     = (struct bfd_link_info *) inf; 
4178
4179   return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
4180                                              info, h, NULL);
4181 }
4182
4183 /* Used to decide how to sort relocs in an optimal manner for the
4184    dynamic linker, before writing them out.  */
4185
4186 static enum elf_reloc_type_class
4187 elf_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
4188 {
4189   switch ((int) ELF32_R_TYPE (rela->r_info))
4190     {
4191     case R_X86_64_RELATIVE:
4192       return reloc_class_relative;
4193     case R_X86_64_JUMP_SLOT:
4194       return reloc_class_plt;
4195     case R_X86_64_COPY:
4196       return reloc_class_copy;
4197     default:
4198       return reloc_class_normal;
4199     }
4200 }
4201
4202 /* Finish up the dynamic sections.  */
4203
4204 static bfd_boolean
4205 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4206                                     struct bfd_link_info *info)
4207 {
4208   struct elf_x86_64_link_hash_table *htab;
4209   bfd *dynobj;
4210   asection *sdyn;
4211
4212   htab = elf_x86_64_hash_table (info);
4213   if (htab == NULL)
4214     return FALSE;
4215
4216   dynobj = htab->elf.dynobj;
4217   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4218
4219   if (htab->elf.dynamic_sections_created)
4220     {
4221       bfd_byte *dyncon, *dynconend;
4222       const struct elf_backend_data *bed;
4223       bfd_size_type sizeof_dyn;
4224
4225       if (sdyn == NULL || htab->elf.sgot == NULL)
4226         abort ();
4227
4228       bed = get_elf_backend_data (dynobj);
4229       sizeof_dyn = bed->s->sizeof_dyn;
4230       dyncon = sdyn->contents;
4231       dynconend = sdyn->contents + sdyn->size;
4232       for (; dyncon < dynconend; dyncon += sizeof_dyn)
4233         {
4234           Elf_Internal_Dyn dyn;
4235           asection *s;
4236
4237           (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
4238
4239           switch (dyn.d_tag)
4240             {
4241             default:
4242               continue;
4243
4244             case DT_PLTGOT:
4245               s = htab->elf.sgotplt;
4246               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4247               break;
4248
4249             case DT_JMPREL:
4250               dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4251               break;
4252
4253             case DT_PLTRELSZ:
4254               s = htab->elf.srelplt->output_section;
4255               dyn.d_un.d_val = s->size;
4256               break;
4257
4258             case DT_RELASZ:
4259               /* The procedure linkage table relocs (DT_JMPREL) should
4260                  not be included in the overall relocs (DT_RELA).
4261                  Therefore, we override the DT_RELASZ entry here to
4262                  make it not include the JMPREL relocs.  Since the
4263                  linker script arranges for .rela.plt to follow all
4264                  other relocation sections, we don't have to worry
4265                  about changing the DT_RELA entry.  */
4266               if (htab->elf.srelplt != NULL)
4267                 {
4268                   s = htab->elf.srelplt->output_section;
4269                   dyn.d_un.d_val -= s->size;
4270                 }
4271               break;
4272
4273             case DT_TLSDESC_PLT:
4274               s = htab->elf.splt;
4275               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4276                 + htab->tlsdesc_plt;
4277               break;
4278
4279             case DT_TLSDESC_GOT:
4280               s = htab->elf.sgot;
4281               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4282                 + htab->tlsdesc_got;
4283               break;
4284             }
4285
4286           (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
4287         }
4288
4289       /* Fill in the special first entry in the procedure linkage table.  */
4290       if (htab->elf.splt && htab->elf.splt->size > 0)
4291         {
4292           /* Fill in the first entry in the procedure linkage table.  */
4293           memcpy (htab->elf.splt->contents, elf_x86_64_plt0_entry,
4294                   PLT_ENTRY_SIZE);
4295           /* Add offset for pushq GOT+8(%rip), since the instruction
4296              uses 6 bytes subtract this value.  */
4297           bfd_put_32 (output_bfd,
4298                       (htab->elf.sgotplt->output_section->vma
4299                        + htab->elf.sgotplt->output_offset
4300                        + 8
4301                        - htab->elf.splt->output_section->vma
4302                        - htab->elf.splt->output_offset
4303                        - 6),
4304                       htab->elf.splt->contents + 2);
4305           /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4306              the end of the instruction.  */
4307           bfd_put_32 (output_bfd,
4308                       (htab->elf.sgotplt->output_section->vma
4309                        + htab->elf.sgotplt->output_offset
4310                        + 16
4311                        - htab->elf.splt->output_section->vma
4312                        - htab->elf.splt->output_offset
4313                        - 12),
4314                       htab->elf.splt->contents + 8);
4315
4316           elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize =
4317             PLT_ENTRY_SIZE;
4318
4319           if (htab->tlsdesc_plt)
4320             {
4321               bfd_put_64 (output_bfd, (bfd_vma) 0,
4322                           htab->elf.sgot->contents + htab->tlsdesc_got);
4323
4324               memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4325                       elf_x86_64_plt0_entry,
4326                       PLT_ENTRY_SIZE);
4327
4328               /* Add offset for pushq GOT+8(%rip), since the
4329                  instruction uses 6 bytes subtract this value.  */
4330               bfd_put_32 (output_bfd,
4331                           (htab->elf.sgotplt->output_section->vma
4332                            + htab->elf.sgotplt->output_offset
4333                            + 8
4334                            - htab->elf.splt->output_section->vma
4335                            - htab->elf.splt->output_offset
4336                            - htab->tlsdesc_plt
4337                            - 6),
4338                           htab->elf.splt->contents + htab->tlsdesc_plt + 2);
4339               /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4340                  htab->tlsdesc_got. The 12 is the offset to the end of
4341                  the instruction.  */
4342               bfd_put_32 (output_bfd,
4343                           (htab->elf.sgot->output_section->vma
4344                            + htab->elf.sgot->output_offset
4345                            + htab->tlsdesc_got
4346                            - htab->elf.splt->output_section->vma
4347                            - htab->elf.splt->output_offset
4348                            - htab->tlsdesc_plt
4349                            - 12),
4350                           htab->elf.splt->contents + htab->tlsdesc_plt + 8);
4351             }
4352         }
4353     }
4354
4355   if (htab->elf.sgotplt)
4356     {
4357       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4358         {
4359           (*_bfd_error_handler)
4360             (_("discarded output section: `%A'"), htab->elf.sgotplt);
4361           return FALSE;
4362         }
4363
4364       /* Fill in the first three entries in the global offset table.  */
4365       if (htab->elf.sgotplt->size > 0)
4366         {
4367           /* Set the first entry in the global offset table to the address of
4368              the dynamic section.  */
4369           if (sdyn == NULL)
4370             bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4371           else
4372             bfd_put_64 (output_bfd,
4373                         sdyn->output_section->vma + sdyn->output_offset,
4374                         htab->elf.sgotplt->contents);
4375           /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
4376           bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4377           bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4378         }
4379
4380       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4381         GOT_ENTRY_SIZE;
4382     }
4383
4384   if (htab->elf.sgot && htab->elf.sgot->size > 0)
4385     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
4386       = GOT_ENTRY_SIZE;
4387
4388   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
4389   htab_traverse (htab->loc_hash_table,
4390                  elf_x86_64_finish_local_dynamic_symbol,
4391                  info);
4392
4393   return TRUE;
4394 }
4395
4396 /* Return address for Ith PLT stub in section PLT, for relocation REL
4397    or (bfd_vma) -1 if it should not be included.  */
4398
4399 static bfd_vma
4400 elf_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4401                         const arelent *rel ATTRIBUTE_UNUSED)
4402 {
4403   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4404 }
4405
4406 /* Handle an x86-64 specific section when reading an object file.  This
4407    is called when elfcode.h finds a section with an unknown type.  */
4408
4409 static bfd_boolean
4410 elf_x86_64_section_from_shdr (bfd *abfd,
4411                                 Elf_Internal_Shdr *hdr,
4412                                 const char *name,
4413                                 int shindex)
4414 {
4415   if (hdr->sh_type != SHT_X86_64_UNWIND)
4416     return FALSE;
4417
4418   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4419     return FALSE;
4420
4421   return TRUE;
4422 }
4423
4424 /* Hook called by the linker routine which adds symbols from an object
4425    file.  We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4426    of .bss.  */
4427
4428 static bfd_boolean
4429 elf_x86_64_add_symbol_hook (bfd *abfd,
4430                             struct bfd_link_info *info,
4431                             Elf_Internal_Sym *sym,
4432                             const char **namep ATTRIBUTE_UNUSED,
4433                             flagword *flagsp ATTRIBUTE_UNUSED,
4434                             asection **secp,
4435                             bfd_vma *valp)
4436 {
4437   asection *lcomm;
4438
4439   switch (sym->st_shndx)
4440     {
4441     case SHN_X86_64_LCOMMON:
4442       lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4443       if (lcomm == NULL)
4444         {
4445           lcomm = bfd_make_section_with_flags (abfd,
4446                                                "LARGE_COMMON",
4447                                                (SEC_ALLOC
4448                                                 | SEC_IS_COMMON
4449                                                 | SEC_LINKER_CREATED));
4450           if (lcomm == NULL)
4451             return FALSE;
4452           elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4453         }
4454       *secp = lcomm;
4455       *valp = sym->st_size;
4456       return TRUE;
4457     }
4458
4459   if ((abfd->flags & DYNAMIC) == 0
4460       && (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
4461           || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE))
4462     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4463
4464   return TRUE;
4465 }
4466
4467
4468 /* Given a BFD section, try to locate the corresponding ELF section
4469    index.  */
4470
4471 static bfd_boolean
4472 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4473                                          asection *sec, int *index_return)
4474 {
4475   if (sec == &_bfd_elf_large_com_section)
4476     {
4477       *index_return = SHN_X86_64_LCOMMON;
4478       return TRUE;
4479     }
4480   return FALSE;
4481 }
4482
4483 /* Process a symbol.  */
4484
4485 static void
4486 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4487                               asymbol *asym)
4488 {
4489   elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4490
4491   switch (elfsym->internal_elf_sym.st_shndx)
4492     {
4493     case SHN_X86_64_LCOMMON:
4494       asym->section = &_bfd_elf_large_com_section;
4495       asym->value = elfsym->internal_elf_sym.st_size;
4496       /* Common symbol doesn't set BSF_GLOBAL.  */
4497       asym->flags &= ~BSF_GLOBAL;
4498       break;
4499     }
4500 }
4501
4502 static bfd_boolean
4503 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
4504 {
4505   return (sym->st_shndx == SHN_COMMON
4506           || sym->st_shndx == SHN_X86_64_LCOMMON);
4507 }
4508
4509 static unsigned int
4510 elf_x86_64_common_section_index (asection *sec)
4511 {
4512   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4513     return SHN_COMMON;
4514   else
4515     return SHN_X86_64_LCOMMON;
4516 }
4517
4518 static asection *
4519 elf_x86_64_common_section (asection *sec)
4520 {
4521   if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4522     return bfd_com_section_ptr;
4523   else
4524     return &_bfd_elf_large_com_section;
4525 }
4526
4527 static bfd_boolean
4528 elf_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4529                          struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4530                          struct elf_link_hash_entry *h,
4531                          Elf_Internal_Sym *sym,
4532                          asection **psec,
4533                          bfd_vma *pvalue ATTRIBUTE_UNUSED,
4534                          unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4535                          bfd_boolean *skip ATTRIBUTE_UNUSED,
4536                          bfd_boolean *override ATTRIBUTE_UNUSED,
4537                          bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4538                          bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4539                          bfd_boolean *newdef ATTRIBUTE_UNUSED,
4540                          bfd_boolean *newdyn,
4541                          bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4542                          bfd_boolean *newweak ATTRIBUTE_UNUSED,
4543                          bfd *abfd ATTRIBUTE_UNUSED,
4544                          asection **sec,
4545                          bfd_boolean *olddef ATTRIBUTE_UNUSED,
4546                          bfd_boolean *olddyn,
4547                          bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4548                          bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4549                          bfd *oldbfd,
4550                          asection **oldsec)
4551 {
4552   /* A normal common symbol and a large common symbol result in a
4553      normal common symbol.  We turn the large common symbol into a
4554      normal one.  */
4555   if (!*olddyn
4556       && h->root.type == bfd_link_hash_common
4557       && !*newdyn
4558       && bfd_is_com_section (*sec)
4559       && *oldsec != *sec)
4560     {
4561       if (sym->st_shndx == SHN_COMMON
4562           && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4563         {
4564           h->root.u.c.p->section
4565             = bfd_make_section_old_way (oldbfd, "COMMON");
4566           h->root.u.c.p->section->flags = SEC_ALLOC;
4567         }
4568       else if (sym->st_shndx == SHN_X86_64_LCOMMON
4569                && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4570         *psec = *sec = bfd_com_section_ptr;
4571     }
4572
4573   return TRUE;
4574 }
4575
4576 static int
4577 elf_x86_64_additional_program_headers (bfd *abfd,
4578                                        struct bfd_link_info *info ATTRIBUTE_UNUSED)
4579 {
4580   asection *s;
4581   int count = 0;
4582
4583   /* Check to see if we need a large readonly segment.  */
4584   s = bfd_get_section_by_name (abfd, ".lrodata");
4585   if (s && (s->flags & SEC_LOAD))
4586     count++;
4587
4588   /* Check to see if we need a large data segment.  Since .lbss sections
4589      is placed right after the .bss section, there should be no need for
4590      a large data segment just because of .lbss.  */
4591   s = bfd_get_section_by_name (abfd, ".ldata");
4592   if (s && (s->flags & SEC_LOAD))
4593     count++;
4594
4595   return count;
4596 }
4597
4598 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
4599
4600 static bfd_boolean
4601 elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4602 {
4603   if (h->plt.offset != (bfd_vma) -1
4604       && !h->def_regular
4605       && !h->pointer_equality_needed)
4606     return FALSE;
4607
4608   return _bfd_elf_hash_symbol (h);
4609 }
4610
4611 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
4612
4613 static bfd_boolean
4614 elf_x86_64_relocs_compatible (const bfd_target *input,
4615                               const bfd_target *output)
4616 {
4617   return ((xvec_get_elf_backend_data (input)->s->elfclass
4618            == xvec_get_elf_backend_data (output)->s->elfclass)
4619           && _bfd_elf_relocs_compatible (input, output));
4620 }
4621
4622 static const struct bfd_elf_special_section
4623   elf_x86_64_special_sections[]=
4624 {
4625   { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4626   { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4627   { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4628   { STRING_COMMA_LEN (".lbss"),            -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4629   { STRING_COMMA_LEN (".ldata"),           -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4630   { STRING_COMMA_LEN (".lrodata"),         -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4631   { NULL,                       0,          0, 0,            0 }
4632 };
4633
4634 #define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_vec
4635 #define TARGET_LITTLE_NAME                  "elf64-x86-64"
4636 #define ELF_ARCH                            bfd_arch_i386
4637 #define ELF_TARGET_ID                       X86_64_ELF_DATA
4638 #define ELF_MACHINE_CODE                    EM_X86_64
4639 #define ELF_MAXPAGESIZE                     0x200000
4640 #define ELF_MINPAGESIZE                     0x1000
4641 #define ELF_COMMONPAGESIZE                  0x1000
4642
4643 #define elf_backend_can_gc_sections         1
4644 #define elf_backend_can_refcount            1
4645 #define elf_backend_want_got_plt            1
4646 #define elf_backend_plt_readonly            1
4647 #define elf_backend_want_plt_sym            0
4648 #define elf_backend_got_header_size         (GOT_ENTRY_SIZE*3)
4649 #define elf_backend_rela_normal             1
4650
4651 #define elf_info_to_howto                   elf_x86_64_info_to_howto
4652
4653 #define bfd_elf64_bfd_link_hash_table_create \
4654   elf_x86_64_link_hash_table_create
4655 #define bfd_elf64_bfd_link_hash_table_free \
4656   elf_x86_64_link_hash_table_free
4657 #define bfd_elf64_bfd_reloc_type_lookup     elf_x86_64_reloc_type_lookup
4658 #define bfd_elf64_bfd_reloc_name_lookup \
4659   elf_x86_64_reloc_name_lookup
4660
4661 #define elf_backend_adjust_dynamic_symbol   elf_x86_64_adjust_dynamic_symbol
4662 #define elf_backend_relocs_compatible       elf_x86_64_relocs_compatible
4663 #define elf_backend_check_relocs            elf_x86_64_check_relocs
4664 #define elf_backend_copy_indirect_symbol    elf_x86_64_copy_indirect_symbol
4665 #define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
4666 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
4667 #define elf_backend_finish_dynamic_symbol   elf_x86_64_finish_dynamic_symbol
4668 #define elf_backend_gc_mark_hook            elf_x86_64_gc_mark_hook
4669 #define elf_backend_gc_sweep_hook           elf_x86_64_gc_sweep_hook
4670 #define elf_backend_grok_prstatus           elf_x86_64_grok_prstatus
4671 #define elf_backend_grok_psinfo             elf_x86_64_grok_psinfo
4672 #define elf_backend_reloc_type_class        elf_x86_64_reloc_type_class
4673 #define elf_backend_relocate_section        elf_x86_64_relocate_section
4674 #define elf_backend_size_dynamic_sections   elf_x86_64_size_dynamic_sections
4675 #define elf_backend_always_size_sections    elf_x86_64_always_size_sections
4676 #define elf_backend_init_index_section      _bfd_elf_init_1_index_section
4677 #define elf_backend_plt_sym_val             elf_x86_64_plt_sym_val
4678 #define elf_backend_object_p                elf64_x86_64_elf_object_p
4679 #define bfd_elf64_mkobject                  elf_x86_64_mkobject
4680
4681 #define elf_backend_section_from_shdr \
4682         elf_x86_64_section_from_shdr
4683
4684 #define elf_backend_section_from_bfd_section \
4685   elf_x86_64_elf_section_from_bfd_section
4686 #define elf_backend_add_symbol_hook \
4687   elf_x86_64_add_symbol_hook
4688 #define elf_backend_symbol_processing \
4689   elf_x86_64_symbol_processing
4690 #define elf_backend_common_section_index \
4691   elf_x86_64_common_section_index
4692 #define elf_backend_common_section \
4693   elf_x86_64_common_section
4694 #define elf_backend_common_definition \
4695   elf_x86_64_common_definition
4696 #define elf_backend_merge_symbol \
4697   elf_x86_64_merge_symbol
4698 #define elf_backend_special_sections \
4699   elf_x86_64_special_sections
4700 #define elf_backend_additional_program_headers \
4701   elf_x86_64_additional_program_headers
4702 #define elf_backend_hash_symbol \
4703   elf_x86_64_hash_symbol
4704
4705 #undef  elf_backend_post_process_headers
4706 #define elf_backend_post_process_headers  _bfd_elf_set_osabi
4707
4708 #include "elf64-target.h"
4709
4710 /* FreeBSD support.  */
4711
4712 #undef  TARGET_LITTLE_SYM
4713 #define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_freebsd_vec
4714 #undef  TARGET_LITTLE_NAME
4715 #define TARGET_LITTLE_NAME                  "elf64-x86-64-freebsd"
4716
4717 #undef  ELF_OSABI
4718 #define ELF_OSABI                           ELFOSABI_FREEBSD
4719
4720 #undef  elf64_bed
4721 #define elf64_bed elf64_x86_64_fbsd_bed
4722
4723 #include "elf64-target.h"
4724
4725 /* Solaris 2 support.  */
4726
4727 #undef  TARGET_LITTLE_SYM
4728 #define TARGET_LITTLE_SYM                   bfd_elf64_x86_64_sol2_vec
4729 #undef  TARGET_LITTLE_NAME
4730 #define TARGET_LITTLE_NAME                  "elf64-x86-64-sol2"
4731
4732 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4733    objects won't be recognized.  */
4734 #undef ELF_OSABI
4735
4736 #undef  elf64_bed
4737 #define elf64_bed                           elf64_x86_64_sol2_bed
4738
4739 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
4740    boundary.  */
4741 #undef elf_backend_static_tls_alignment
4742 #define elf_backend_static_tls_alignment    16
4743
4744 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4745
4746    Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4747    File, p.63.  */
4748 #undef elf_backend_want_plt_sym
4749 #define elf_backend_want_plt_sym            1
4750
4751 #include "elf64-target.h"
4752
4753 /* Intel L1OM support.  */
4754
4755 static bfd_boolean
4756 elf64_l1om_elf_object_p (bfd *abfd)
4757 {
4758   /* Set the right machine number for an L1OM elf64 file.  */
4759   bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
4760   return TRUE;
4761 }
4762
4763 #undef  TARGET_LITTLE_SYM
4764 #define TARGET_LITTLE_SYM                   bfd_elf64_l1om_vec
4765 #undef  TARGET_LITTLE_NAME
4766 #define TARGET_LITTLE_NAME                  "elf64-l1om"
4767 #undef ELF_ARCH
4768 #define ELF_ARCH                            bfd_arch_l1om
4769
4770 #undef  ELF_MACHINE_CODE
4771 #define ELF_MACHINE_CODE                    EM_L1OM
4772
4773 #undef  ELF_OSABI
4774
4775 #undef  elf64_bed
4776 #define elf64_bed elf64_l1om_bed
4777
4778 #undef elf_backend_object_p
4779 #define elf_backend_object_p                elf64_l1om_elf_object_p
4780
4781 #undef  elf_backend_post_process_headers
4782 #undef  elf_backend_static_tls_alignment
4783
4784 #undef elf_backend_want_plt_sym
4785 #define elf_backend_want_plt_sym            0
4786
4787 #include "elf64-target.h"
4788
4789 /* FreeBSD L1OM support.  */
4790
4791 #undef  TARGET_LITTLE_SYM
4792 #define TARGET_LITTLE_SYM                   bfd_elf64_l1om_freebsd_vec
4793 #undef  TARGET_LITTLE_NAME
4794 #define TARGET_LITTLE_NAME                  "elf64-l1om-freebsd"
4795
4796 #undef  ELF_OSABI
4797 #define ELF_OSABI                           ELFOSABI_FREEBSD
4798
4799 #undef  elf64_bed
4800 #define elf64_bed elf64_l1om_fbsd_bed
4801
4802 #undef  elf_backend_post_process_headers
4803 #define elf_backend_post_process_headers  _bfd_elf_set_osabi
4804
4805 #include "elf64-target.h"
4806
4807 /* 32bit x86-64 support.  */
4808
4809 static bfd_boolean
4810 elf32_x86_64_elf_object_p (bfd *abfd)
4811 {
4812   /* Set the right machine number for an x86-64 elf32 file.  */
4813   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
4814   return TRUE;
4815 }
4816
4817 #undef  TARGET_LITTLE_SYM
4818 #define TARGET_LITTLE_SYM                   bfd_elf32_x86_64_vec
4819 #undef  TARGET_LITTLE_NAME
4820 #define TARGET_LITTLE_NAME                  "elf32-x86-64"
4821
4822 #undef ELF_ARCH
4823 #define ELF_ARCH                            bfd_arch_i386
4824
4825 #undef  ELF_MACHINE_CODE
4826 #define ELF_MACHINE_CODE                    EM_X86_64
4827
4828 #define bfd_elf32_bfd_link_hash_table_create \
4829   elf_x86_64_link_hash_table_create
4830 #define bfd_elf32_bfd_link_hash_table_free \
4831   elf_x86_64_link_hash_table_free
4832 #define bfd_elf32_bfd_reloc_type_lookup \
4833   elf_x86_64_reloc_type_lookup
4834 #define bfd_elf32_bfd_reloc_name_lookup \
4835   elf_x86_64_reloc_name_lookup
4836 #define bfd_elf32_mkobject \
4837   elf_x86_64_mkobject
4838
4839 #undef  ELF_OSABI
4840
4841 #undef elf_backend_post_process_headers
4842
4843 #undef elf_backend_object_p
4844 #define elf_backend_object_p \
4845   elf32_x86_64_elf_object_p
4846
4847 #undef elf_backend_bfd_from_remote_memory
4848 #define elf_backend_bfd_from_remote_memory \
4849   _bfd_elf32_bfd_from_remote_memory
4850
4851 #undef elf_backend_size_info
4852 #define elf_backend_size_info \
4853   _bfd_elf32_size_info
4854
4855 #include "elf32-target.h"