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