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