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