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