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