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