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