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