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