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