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