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