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