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