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