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