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