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