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