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