2006-09-16 Paul Brook <paul@codesourcery.com>
[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
c434dee6
AJ
1028 if (strncmp (name, ".rela", 5) != 0
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
AJ
1136elf64_x86_64_gc_mark_hook (asection *sec,
1137 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1138 Elf_Internal_Rela *rel,
1139 struct elf_link_hash_entry *h,
1140 Elf_Internal_Sym *sym)
70256ad8
AJ
1141{
1142 if (h != NULL)
1143 {
fe4770f4 1144 switch (ELF64_R_TYPE (rel->r_info))
70256ad8 1145 {
fe4770f4
AJ
1146 case R_X86_64_GNU_VTINHERIT:
1147 case R_X86_64_GNU_VTENTRY:
1148 break;
70256ad8
AJ
1149
1150 default:
fe4770f4
AJ
1151 switch (h->root.type)
1152 {
1153 case bfd_link_hash_defined:
1154 case bfd_link_hash_defweak:
1155 return h->root.u.def.section;
1156
1157 case bfd_link_hash_common:
1158 return h->root.u.c.p->section;
1159
1160 default:
1161 break;
1162 }
70256ad8
AJ
1163 }
1164 }
1165 else
1e2f5b6e 1166 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
70256ad8
AJ
1167
1168 return NULL;
1169}
1170
407443a3 1171/* Update the got entry reference counts for the section being removed. */
70256ad8 1172
b34976b6 1173static bfd_boolean
27482721
AJ
1174elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1175 asection *sec, const Elf_Internal_Rela *relocs)
70256ad8
AJ
1176{
1177 Elf_Internal_Shdr *symtab_hdr;
1178 struct elf_link_hash_entry **sym_hashes;
1179 bfd_signed_vma *local_got_refcounts;
1180 const Elf_Internal_Rela *rel, *relend;
c434dee6
AJ
1181
1182 elf_section_data (sec)->local_dynrel = NULL;
70256ad8
AJ
1183
1184 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1185 sym_hashes = elf_sym_hashes (abfd);
1186 local_got_refcounts = elf_local_got_refcounts (abfd);
1187
70256ad8
AJ
1188 relend = relocs + sec->reloc_count;
1189 for (rel = relocs; rel < relend; rel++)
26e41594
AM
1190 {
1191 unsigned long r_symndx;
1192 unsigned int r_type;
1193 struct elf_link_hash_entry *h = NULL;
70256ad8 1194
26e41594
AM
1195 r_symndx = ELF64_R_SYM (rel->r_info);
1196 if (r_symndx >= symtab_hdr->sh_info)
1197 {
1198 struct elf64_x86_64_link_hash_entry *eh;
1199 struct elf64_x86_64_dyn_relocs **pp;
1200 struct elf64_x86_64_dyn_relocs *p;
c434dee6 1201
26e41594 1202 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
1203 while (h->root.type == bfd_link_hash_indirect
1204 || h->root.type == bfd_link_hash_warning)
1205 h = (struct elf_link_hash_entry *) h->root.u.i.link;
26e41594 1206 eh = (struct elf64_x86_64_link_hash_entry *) h;
c434dee6 1207
26e41594
AM
1208 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1209 if (p->sec == sec)
1210 {
1211 /* Everything must go for SEC. */
1212 *pp = p->next;
1213 break;
1214 }
1215 }
c434dee6 1216
26e41594
AM
1217 r_type = ELF64_R_TYPE (rel->r_info);
1218 r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
1219 switch (r_type)
1220 {
1221 case R_X86_64_TLSLD:
1222 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1223 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1224 break;
c434dee6 1225
26e41594 1226 case R_X86_64_TLSGD:
67a4f2b7
AO
1227 case R_X86_64_GOTPC32_TLSDESC:
1228 case R_X86_64_TLSDESC_CALL:
26e41594
AM
1229 case R_X86_64_GOTTPOFF:
1230 case R_X86_64_GOT32:
1231 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
1232 case R_X86_64_GOT64:
1233 case R_X86_64_GOTPCREL64:
1234 case R_X86_64_GOTPLT64:
26e41594
AM
1235 if (h != NULL)
1236 {
7b81dfbb
AJ
1237 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1238 h->plt.refcount -= 1;
26e41594
AM
1239 if (h->got.refcount > 0)
1240 h->got.refcount -= 1;
1241 }
1242 else if (local_got_refcounts != NULL)
1243 {
1244 if (local_got_refcounts[r_symndx] > 0)
1245 local_got_refcounts[r_symndx] -= 1;
1246 }
1247 break;
c434dee6 1248
26e41594
AM
1249 case R_X86_64_8:
1250 case R_X86_64_16:
1251 case R_X86_64_32:
1252 case R_X86_64_64:
1253 case R_X86_64_32S:
1254 case R_X86_64_PC8:
1255 case R_X86_64_PC16:
1256 case R_X86_64_PC32:
d6ab8113 1257 case R_X86_64_PC64:
26e41594
AM
1258 if (info->shared)
1259 break;
1260 /* Fall thru */
c434dee6 1261
26e41594 1262 case R_X86_64_PLT32:
7b81dfbb 1263 case R_X86_64_PLTOFF64:
26e41594
AM
1264 if (h != NULL)
1265 {
1266 if (h->plt.refcount > 0)
1267 h->plt.refcount -= 1;
1268 }
1269 break;
70256ad8 1270
26e41594
AM
1271 default:
1272 break;
1273 }
1274 }
70256ad8 1275
b34976b6 1276 return TRUE;
70256ad8
AJ
1277}
1278
1279/* Adjust a symbol defined by a dynamic object and referenced by a
1280 regular object. The current definition is in some section of the
1281 dynamic object, but we're not including those sections. We have to
1282 change the definition to something the rest of the link can
407443a3 1283 understand. */
70256ad8 1284
b34976b6 1285static bfd_boolean
27482721
AJ
1286elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1287 struct elf_link_hash_entry *h)
70256ad8 1288{
c434dee6 1289 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
1290 asection *s;
1291 unsigned int power_of_two;
1292
70256ad8
AJ
1293 /* If this is a function, put it in the procedure linkage table. We
1294 will fill in the contents of the procedure linkage table later,
1295 when we know the address of the .got section. */
1296 if (h->type == STT_FUNC
f5385ebf 1297 || h->needs_plt)
70256ad8 1298 {
c434dee6 1299 if (h->plt.refcount <= 0
27482721
AJ
1300 || SYMBOL_CALLS_LOCAL (info, h)
1301 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1302 && h->root.type == bfd_link_hash_undefweak))
70256ad8 1303 {
70256ad8
AJ
1304 /* This case can occur if we saw a PLT32 reloc in an input
1305 file, but the symbol was never referred to by a dynamic
1306 object, or if all references were garbage collected. In
1307 such a case, we don't actually need to build a procedure
1308 linkage table, and we can just do a PC32 reloc instead. */
70256ad8 1309 h->plt.offset = (bfd_vma) -1;
f5385ebf 1310 h->needs_plt = 0;
70256ad8
AJ
1311 }
1312
b34976b6 1313 return TRUE;
70256ad8 1314 }
bbd7ec4a 1315 else
c434dee6
AJ
1316 /* It's possible that we incorrectly decided a .plt reloc was
1317 needed for an R_X86_64_PC32 reloc to a non-function sym in
1318 check_relocs. We can't decide accurately between function and
1319 non-function syms in check-relocs; Objects loaded later in
1320 the link may change h->type. So fix it now. */
bbd7ec4a 1321 h->plt.offset = (bfd_vma) -1;
70256ad8
AJ
1322
1323 /* If this is a weak symbol, and there is a real definition, the
1324 processor independent code will have arranged for us to see the
407443a3 1325 real definition first, and we can just use the same value. */
f6e332e6 1326 if (h->u.weakdef != NULL)
70256ad8 1327 {
f6e332e6
AM
1328 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1329 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1330 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1331 h->root.u.def.value = h->u.weakdef->root.u.def.value;
d40d037c 1332 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
f6e332e6 1333 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 1334 return TRUE;
70256ad8
AJ
1335 }
1336
1337 /* This is a reference to a symbol defined by a dynamic object which
407443a3 1338 is not a function. */
70256ad8
AJ
1339
1340 /* If we are creating a shared library, we must presume that the
1341 only references to the symbol are via the global offset table.
1342 For such cases we need not do anything here; the relocations will
407443a3 1343 be handled correctly by relocate_section. */
70256ad8 1344 if (info->shared)
b34976b6 1345 return TRUE;
70256ad8
AJ
1346
1347 /* If there are no references to this symbol that do not use the
1348 GOT, we don't need to generate a copy reloc. */
f5385ebf 1349 if (!h->non_got_ref)
b34976b6 1350 return TRUE;
70256ad8 1351
c434dee6
AJ
1352 /* If -z nocopyreloc was given, we won't generate them either. */
1353 if (info->nocopyreloc)
1354 {
f5385ebf 1355 h->non_got_ref = 0;
b34976b6 1356 return TRUE;
c434dee6
AJ
1357 }
1358
d40d037c 1359 if (ELIMINATE_COPY_RELOCS)
c434dee6 1360 {
d40d037c
AJ
1361 struct elf64_x86_64_link_hash_entry * eh;
1362 struct elf64_x86_64_dyn_relocs *p;
c434dee6 1363
d40d037c
AJ
1364 eh = (struct elf64_x86_64_link_hash_entry *) h;
1365 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1366 {
1367 s = p->sec->output_section;
1368 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1369 break;
1370 }
1371
1372 /* If we didn't find any dynamic relocs in read-only sections, then
1373 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1374 if (p == NULL)
1375 {
f5385ebf 1376 h->non_got_ref = 0;
d40d037c
AJ
1377 return TRUE;
1378 }
c434dee6
AJ
1379 }
1380
909272ee
AM
1381 if (h->size == 0)
1382 {
1383 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1384 h->root.root.string);
1385 return TRUE;
1386 }
1387
70256ad8 1388 /* We must allocate the symbol in our .dynbss section, which will
407443a3 1389 become part of the .bss section of the executable. There will be
70256ad8
AJ
1390 an entry for this symbol in the .dynsym section. The dynamic
1391 object will contain position independent code, so all references
1392 from the dynamic object to this symbol will go through the global
1393 offset table. The dynamic linker will use the .dynsym entry to
1394 determine the address it must put in the global offset table, so
1395 both the dynamic object and the regular object will refer to the
1396 same memory location for the variable. */
1397
c434dee6 1398 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
1399
1400 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1401 to copy the initial value out of the dynamic object and into the
cedb70c5 1402 runtime process image. */
70256ad8
AJ
1403 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1404 {
eea6121a 1405 htab->srelbss->size += sizeof (Elf64_External_Rela);
f5385ebf 1406 h->needs_copy = 1;
70256ad8
AJ
1407 }
1408
1409 /* We need to figure out the alignment required for this symbol. I
407443a3 1410 have no idea how ELF linkers handle this. 16-bytes is the size
70256ad8
AJ
1411 of the largest type that requires hard alignment -- long double. */
1412 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1413 this construct. */
1414 power_of_two = bfd_log2 (h->size);
1415 if (power_of_two > 4)
1416 power_of_two = 4;
1417
1418 /* Apply the required alignment. */
c434dee6 1419 s = htab->sdynbss;
eea6121a 1420 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
c434dee6 1421 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
70256ad8 1422 {
c434dee6 1423 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
b34976b6 1424 return FALSE;
70256ad8
AJ
1425 }
1426
1427 /* Define the symbol as being at this point in the section. */
1428 h->root.u.def.section = s;
eea6121a 1429 h->root.u.def.value = s->size;
70256ad8
AJ
1430
1431 /* Increment the section size to make room for the symbol. */
eea6121a 1432 s->size += h->size;
70256ad8 1433
b34976b6 1434 return TRUE;
70256ad8
AJ
1435}
1436
c434dee6
AJ
1437/* Allocate space in .plt, .got and associated reloc sections for
1438 dynamic relocs. */
1439
b34976b6 1440static bfd_boolean
27482721 1441allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
1442{
1443 struct bfd_link_info *info;
1444 struct elf64_x86_64_link_hash_table *htab;
1445 struct elf64_x86_64_link_hash_entry *eh;
1446 struct elf64_x86_64_dyn_relocs *p;
1447
e92d460e 1448 if (h->root.type == bfd_link_hash_indirect)
b34976b6 1449 return TRUE;
c434dee6 1450
e92d460e
AM
1451 if (h->root.type == bfd_link_hash_warning)
1452 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1453
c434dee6
AJ
1454 info = (struct bfd_link_info *) inf;
1455 htab = elf64_x86_64_hash_table (info);
1456
1457 if (htab->elf.dynamic_sections_created
27482721 1458 && h->plt.refcount > 0)
c434dee6
AJ
1459 {
1460 /* Make sure this symbol is output as a dynamic symbol.
1461 Undefined weak syms won't yet be marked as dynamic. */
1462 if (h->dynindx == -1
f5385ebf 1463 && !h->forced_local)
c434dee6 1464 {
c152c796 1465 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1466 return FALSE;
c434dee6
AJ
1467 }
1468
27482721
AJ
1469 if (info->shared
1470 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
c434dee6
AJ
1471 {
1472 asection *s = htab->splt;
1473
1474 /* If this is the first .plt entry, make room for the special
1475 first entry. */
eea6121a
AM
1476 if (s->size == 0)
1477 s->size += PLT_ENTRY_SIZE;
c434dee6 1478
eea6121a 1479 h->plt.offset = s->size;
c434dee6
AJ
1480
1481 /* If this symbol is not defined in a regular file, and we are
1482 not generating a shared library, then set the symbol to this
1483 location in the .plt. This is required to make function
1484 pointers compare as equal between the normal executable and
1485 the shared library. */
1486 if (! info->shared
f5385ebf 1487 && !h->def_regular)
c434dee6
AJ
1488 {
1489 h->root.u.def.section = s;
1490 h->root.u.def.value = h->plt.offset;
1491 }
1492
1493 /* Make room for this entry. */
eea6121a 1494 s->size += PLT_ENTRY_SIZE;
c434dee6
AJ
1495
1496 /* We also need to make an entry in the .got.plt section, which
1497 will be placed in the .got section by the linker script. */
eea6121a 1498 htab->sgotplt->size += GOT_ENTRY_SIZE;
c434dee6
AJ
1499
1500 /* We also need to make an entry in the .rela.plt section. */
eea6121a 1501 htab->srelplt->size += sizeof (Elf64_External_Rela);
67a4f2b7 1502 htab->srelplt->reloc_count++;
c434dee6
AJ
1503 }
1504 else
1505 {
1506 h->plt.offset = (bfd_vma) -1;
f5385ebf 1507 h->needs_plt = 0;
c434dee6
AJ
1508 }
1509 }
1510 else
1511 {
1512 h->plt.offset = (bfd_vma) -1;
f5385ebf 1513 h->needs_plt = 0;
c434dee6
AJ
1514 }
1515
67a4f2b7
AO
1516 eh = (struct elf64_x86_64_link_hash_entry *) h;
1517 eh->tlsdesc_got = (bfd_vma) -1;
1518
bffbf940
JJ
1519 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1520 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1521 if (h->got.refcount > 0
1522 && !info->shared
1523 && h->dynindx == -1
1524 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1525 h->got.offset = (bfd_vma) -1;
1526 else if (h->got.refcount > 0)
c434dee6
AJ
1527 {
1528 asection *s;
b34976b6 1529 bfd_boolean dyn;
bffbf940 1530 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
c434dee6
AJ
1531
1532 /* Make sure this symbol is output as a dynamic symbol.
1533 Undefined weak syms won't yet be marked as dynamic. */
1534 if (h->dynindx == -1
f5385ebf 1535 && !h->forced_local)
c434dee6 1536 {
c152c796 1537 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1538 return FALSE;
c434dee6
AJ
1539 }
1540
67a4f2b7
AO
1541 if (GOT_TLS_GDESC_P (tls_type))
1542 {
1543 eh->tlsdesc_got = htab->sgotplt->size
1544 - elf64_x86_64_compute_jump_table_size (htab);
1545 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1546 h->got.offset = (bfd_vma) -2;
1547 }
1548 if (! GOT_TLS_GDESC_P (tls_type)
1549 || GOT_TLS_GD_P (tls_type))
1550 {
1551 s = htab->sgot;
1552 h->got.offset = s->size;
1553 s->size += GOT_ENTRY_SIZE;
1554 if (GOT_TLS_GD_P (tls_type))
1555 s->size += GOT_ENTRY_SIZE;
1556 }
c434dee6 1557 dyn = htab->elf.dynamic_sections_created;
bffbf940
JJ
1558 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1559 and two if global.
1560 R_X86_64_GOTTPOFF needs one dynamic relocation. */
67a4f2b7 1561 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
bffbf940 1562 || tls_type == GOT_TLS_IE)
eea6121a 1563 htab->srelgot->size += sizeof (Elf64_External_Rela);
67a4f2b7 1564 else if (GOT_TLS_GD_P (tls_type))
eea6121a 1565 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
67a4f2b7
AO
1566 else if (! GOT_TLS_GDESC_P (tls_type)
1567 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1568 || h->root.type != bfd_link_hash_undefweak)
27482721
AJ
1569 && (info->shared
1570 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
eea6121a 1571 htab->srelgot->size += sizeof (Elf64_External_Rela);
67a4f2b7
AO
1572 if (GOT_TLS_GDESC_P (tls_type))
1573 {
1574 htab->srelplt->size += sizeof (Elf64_External_Rela);
1575 htab->tlsdesc_plt = (bfd_vma) -1;
1576 }
c434dee6
AJ
1577 }
1578 else
1579 h->got.offset = (bfd_vma) -1;
1580
c434dee6 1581 if (eh->dyn_relocs == NULL)
b34976b6 1582 return TRUE;
c434dee6
AJ
1583
1584 /* In the shared -Bsymbolic case, discard space allocated for
1585 dynamic pc-relative relocs against symbols which turn out to be
1586 defined in regular objects. For the normal shared case, discard
1587 space for pc-relative relocs that have become local due to symbol
1588 visibility changes. */
1589
1590 if (info->shared)
1591 {
27482721
AJ
1592 /* Relocs that use pc_count are those that appear on a call
1593 insn, or certain REL relocs that can generated via assembly.
1594 We want calls to protected symbols to resolve directly to the
1595 function rather than going via the plt. If people want
1596 function pointer comparisons to work as expected then they
1597 should avoid writing weird assembly. */
1598 if (SYMBOL_CALLS_LOCAL (info, h))
c434dee6
AJ
1599 {
1600 struct elf64_x86_64_dyn_relocs **pp;
1601
1602 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1603 {
1604 p->count -= p->pc_count;
1605 p->pc_count = 0;
1606 if (p->count == 0)
1607 *pp = p->next;
1608 else
1609 pp = &p->next;
1610 }
1611 }
4e795f50
AM
1612
1613 /* Also discard relocs on undefined weak syms with non-default
1614 visibility. */
22d606e9 1615 if (eh->dyn_relocs != NULL
4e795f50 1616 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
1617 {
1618 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1619 eh->dyn_relocs = NULL;
1620
1621 /* Make sure undefined weak symbols are output as a dynamic
1622 symbol in PIEs. */
1623 else if (h->dynindx == -1
1624 && !h->forced_local)
1625 {
1626 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1627 return FALSE;
1628 }
1629 }
c434dee6 1630 }
d40d037c 1631 else if (ELIMINATE_COPY_RELOCS)
c434dee6
AJ
1632 {
1633 /* For the non-shared case, discard space for relocs against
1634 symbols which turn out to need copy relocs or are not
1635 dynamic. */
1636
f5385ebf
AM
1637 if (!h->non_got_ref
1638 && ((h->def_dynamic
1639 && !h->def_regular)
c434dee6
AJ
1640 || (htab->elf.dynamic_sections_created
1641 && (h->root.type == bfd_link_hash_undefweak
1642 || h->root.type == bfd_link_hash_undefined))))
1643 {
1644 /* Make sure this symbol is output as a dynamic symbol.
1645 Undefined weak syms won't yet be marked as dynamic. */
1646 if (h->dynindx == -1
f5385ebf 1647 && !h->forced_local)
c434dee6 1648 {
c152c796 1649 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1650 return FALSE;
c434dee6
AJ
1651 }
1652
1653 /* If that succeeded, we know we'll be keeping all the
1654 relocs. */
1655 if (h->dynindx != -1)
1656 goto keep;
1657 }
1658
1659 eh->dyn_relocs = NULL;
1660
1661 keep: ;
1662 }
1663
1664 /* Finally, allocate space. */
1665 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1666 {
1667 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 1668 sreloc->size += p->count * sizeof (Elf64_External_Rela);
c434dee6
AJ
1669 }
1670
b34976b6 1671 return TRUE;
c434dee6
AJ
1672}
1673
1674/* Find any dynamic relocs that apply to read-only sections. */
1675
b34976b6 1676static bfd_boolean
27482721 1677readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
1678{
1679 struct elf64_x86_64_link_hash_entry *eh;
1680 struct elf64_x86_64_dyn_relocs *p;
1681
e92d460e
AM
1682 if (h->root.type == bfd_link_hash_warning)
1683 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1684
c434dee6
AJ
1685 eh = (struct elf64_x86_64_link_hash_entry *) h;
1686 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1687 {
1688 asection *s = p->sec->output_section;
1689
1690 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1691 {
1692 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1693
1694 info->flags |= DF_TEXTREL;
1695
1696 /* Not an error, just cut short the traversal. */
b34976b6 1697 return FALSE;
c434dee6
AJ
1698 }
1699 }
b34976b6 1700 return TRUE;
c434dee6
AJ
1701}
1702
70256ad8
AJ
1703/* Set the sizes of the dynamic sections. */
1704
b34976b6 1705static bfd_boolean
27482721
AJ
1706elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1707 struct bfd_link_info *info)
70256ad8 1708{
c434dee6 1709 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
1710 bfd *dynobj;
1711 asection *s;
b34976b6 1712 bfd_boolean relocs;
c434dee6 1713 bfd *ibfd;
70256ad8 1714
c434dee6
AJ
1715 htab = elf64_x86_64_hash_table (info);
1716 dynobj = htab->elf.dynobj;
1717 if (dynobj == NULL)
1718 abort ();
70256ad8 1719
c434dee6 1720 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1721 {
1722 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 1723 if (info->executable)
70256ad8
AJ
1724 {
1725 s = bfd_get_section_by_name (dynobj, ".interp");
c434dee6
AJ
1726 if (s == NULL)
1727 abort ();
eea6121a 1728 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
70256ad8
AJ
1729 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1730 }
1731 }
70256ad8 1732
c434dee6
AJ
1733 /* Set up .got offsets for local syms, and space for local dynamic
1734 relocs. */
1735 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
70256ad8 1736 {
c434dee6
AJ
1737 bfd_signed_vma *local_got;
1738 bfd_signed_vma *end_local_got;
bffbf940 1739 char *local_tls_type;
67a4f2b7 1740 bfd_vma *local_tlsdesc_gotent;
c434dee6
AJ
1741 bfd_size_type locsymcount;
1742 Elf_Internal_Shdr *symtab_hdr;
1743 asection *srel;
70256ad8 1744
c434dee6 1745 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
70256ad8
AJ
1746 continue;
1747
c434dee6 1748 for (s = ibfd->sections; s != NULL; s = s->next)
70256ad8 1749 {
c434dee6
AJ
1750 struct elf64_x86_64_dyn_relocs *p;
1751
e81d3500
DD
1752 for (p = (struct elf64_x86_64_dyn_relocs *)
1753 (elf_section_data (s)->local_dynrel);
c434dee6
AJ
1754 p != NULL;
1755 p = p->next)
70256ad8 1756 {
c434dee6
AJ
1757 if (!bfd_is_abs_section (p->sec)
1758 && bfd_is_abs_section (p->sec->output_section))
1759 {
1760 /* Input section has been discarded, either because
1761 it is a copy of a linkonce section or due to
1762 linker script /DISCARD/, so we'll be discarding
1763 the relocs too. */
1764 }
1765 else if (p->count != 0)
1766 {
1767 srel = elf_section_data (p->sec)->sreloc;
eea6121a 1768 srel->size += p->count * sizeof (Elf64_External_Rela);
c434dee6
AJ
1769 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1770 info->flags |= DF_TEXTREL;
1771
1772 }
70256ad8
AJ
1773 }
1774 }
c434dee6
AJ
1775
1776 local_got = elf_local_got_refcounts (ibfd);
1777 if (!local_got)
1778 continue;
1779
1780 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1781 locsymcount = symtab_hdr->sh_info;
1782 end_local_got = local_got + locsymcount;
bffbf940 1783 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
67a4f2b7 1784 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
c434dee6
AJ
1785 s = htab->sgot;
1786 srel = htab->srelgot;
67a4f2b7
AO
1787 for (; local_got < end_local_got;
1788 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
70256ad8 1789 {
67a4f2b7 1790 *local_tlsdesc_gotent = (bfd_vma) -1;
c434dee6 1791 if (*local_got > 0)
70256ad8 1792 {
67a4f2b7
AO
1793 if (GOT_TLS_GDESC_P (*local_tls_type))
1794 {
1795 *local_tlsdesc_gotent = htab->sgotplt->size
1796 - elf64_x86_64_compute_jump_table_size (htab);
1797 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1798 *local_got = (bfd_vma) -2;
1799 }
1800 if (! GOT_TLS_GDESC_P (*local_tls_type)
1801 || GOT_TLS_GD_P (*local_tls_type))
1802 {
1803 *local_got = s->size;
1804 s->size += GOT_ENTRY_SIZE;
1805 if (GOT_TLS_GD_P (*local_tls_type))
1806 s->size += GOT_ENTRY_SIZE;
1807 }
bffbf940 1808 if (info->shared
67a4f2b7 1809 || GOT_TLS_GD_ANY_P (*local_tls_type)
bffbf940 1810 || *local_tls_type == GOT_TLS_IE)
67a4f2b7
AO
1811 {
1812 if (GOT_TLS_GDESC_P (*local_tls_type))
1813 {
1814 htab->srelplt->size += sizeof (Elf64_External_Rela);
1815 htab->tlsdesc_plt = (bfd_vma) -1;
1816 }
1817 if (! GOT_TLS_GDESC_P (*local_tls_type)
1818 || GOT_TLS_GD_P (*local_tls_type))
1819 srel->size += sizeof (Elf64_External_Rela);
1820 }
70256ad8
AJ
1821 }
1822 else
c434dee6
AJ
1823 *local_got = (bfd_vma) -1;
1824 }
1825 }
70256ad8 1826
bffbf940
JJ
1827 if (htab->tls_ld_got.refcount > 0)
1828 {
1829 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1830 relocs. */
eea6121a
AM
1831 htab->tls_ld_got.offset = htab->sgot->size;
1832 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
1833 htab->srelgot->size += sizeof (Elf64_External_Rela);
bffbf940
JJ
1834 }
1835 else
1836 htab->tls_ld_got.offset = -1;
1837
c434dee6
AJ
1838 /* Allocate global sym .plt and .got entries, and space for global
1839 sym dynamic relocs. */
1840 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1841
67a4f2b7
AO
1842 /* For every jump slot reserved in the sgotplt, reloc_count is
1843 incremented. However, when we reserve space for TLS descriptors,
1844 it's not incremented, so in order to compute the space reserved
1845 for them, it suffices to multiply the reloc count by the jump
1846 slot size. */
1847 if (htab->srelplt)
1848 htab->sgotplt_jump_table_size
1849 = elf64_x86_64_compute_jump_table_size (htab);
1850
1851 if (htab->tlsdesc_plt)
1852 {
1853 /* If we're not using lazy TLS relocations, don't generate the
1854 PLT and GOT entries they require. */
1855 if ((info->flags & DF_BIND_NOW))
1856 htab->tlsdesc_plt = 0;
1857 else
1858 {
1859 htab->tlsdesc_got = htab->sgot->size;
1860 htab->sgot->size += GOT_ENTRY_SIZE;
1861 /* Reserve room for the initial entry.
1862 FIXME: we could probably do away with it in this case. */
1863 if (htab->splt->size == 0)
1864 htab->splt->size += PLT_ENTRY_SIZE;
1865 htab->tlsdesc_plt = htab->splt->size;
1866 htab->splt->size += PLT_ENTRY_SIZE;
1867 }
1868 }
1869
c434dee6
AJ
1870 /* We now have determined the sizes of the various dynamic sections.
1871 Allocate memory for them. */
b34976b6 1872 relocs = FALSE;
c434dee6
AJ
1873 for (s = dynobj->sections; s != NULL; s = s->next)
1874 {
1875 if ((s->flags & SEC_LINKER_CREATED) == 0)
1876 continue;
1877
1878 if (s == htab->splt
1879 || s == htab->sgot
75ff4589
L
1880 || s == htab->sgotplt
1881 || s == htab->sdynbss)
c434dee6
AJ
1882 {
1883 /* Strip this section if we don't need it; see the
1884 comment below. */
1885 }
1886 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1887 {
eea6121a 1888 if (s->size != 0 && s != htab->srelplt)
b34976b6 1889 relocs = TRUE;
c434dee6
AJ
1890
1891 /* We use the reloc_count field as a counter if we need
1892 to copy relocs into the output file. */
67a4f2b7
AO
1893 if (s != htab->srelplt)
1894 s->reloc_count = 0;
70256ad8 1895 }
c434dee6 1896 else
70256ad8
AJ
1897 {
1898 /* It's not one of our sections, so don't allocate space. */
1899 continue;
1900 }
1901
eea6121a 1902 if (s->size == 0)
70256ad8 1903 {
c434dee6
AJ
1904 /* If we don't need this section, strip it from the
1905 output file. This is mostly to handle .rela.bss and
1906 .rela.plt. We must create both sections in
1907 create_dynamic_sections, because they must be created
1908 before the linker maps input sections to output
1909 sections. The linker does that before
1910 adjust_dynamic_symbol is called, and it is that
1911 function which decides whether anything needs to go
1912 into these sections. */
1913
8423293d 1914 s->flags |= SEC_EXCLUDE;
70256ad8
AJ
1915 continue;
1916 }
1917
c456f082
AM
1918 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1919 continue;
1920
70256ad8
AJ
1921 /* Allocate memory for the section contents. We use bfd_zalloc
1922 here in case unused entries are not reclaimed before the
1923 section's contents are written out. This should not happen,
1924 but this way if it does, we get a R_X86_64_NONE reloc instead
1925 of garbage. */
eea6121a 1926 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c434dee6 1927 if (s->contents == NULL)
b34976b6 1928 return FALSE;
70256ad8
AJ
1929 }
1930
c434dee6 1931 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1932 {
1933 /* Add some entries to the .dynamic section. We fill in the
1934 values later, in elf64_x86_64_finish_dynamic_sections, but we
1935 must add the entries now so that we get the correct size for
407443a3 1936 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8 1937 dynamic linker and used by the debugger. */
dc810e39 1938#define add_dynamic_entry(TAG, VAL) \
5a580b3a 1939 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 1940
36af4a4e 1941 if (info->executable)
70256ad8 1942 {
dc810e39 1943 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 1944 return FALSE;
70256ad8
AJ
1945 }
1946
eea6121a 1947 if (htab->splt->size != 0)
70256ad8 1948 {
dc810e39
AM
1949 if (!add_dynamic_entry (DT_PLTGOT, 0)
1950 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1951 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1952 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 1953 return FALSE;
67a4f2b7
AO
1954
1955 if (htab->tlsdesc_plt
1956 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
1957 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
1958 return FALSE;
70256ad8
AJ
1959 }
1960
1961 if (relocs)
1962 {
dc810e39
AM
1963 if (!add_dynamic_entry (DT_RELA, 0)
1964 || !add_dynamic_entry (DT_RELASZ, 0)
1965 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 1966 return FALSE;
70256ad8 1967
c434dee6
AJ
1968 /* If any dynamic relocs apply to a read-only section,
1969 then we need a DT_TEXTREL entry. */
1970 if ((info->flags & DF_TEXTREL) == 0)
1971 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1972 (PTR) info);
1973
1974 if ((info->flags & DF_TEXTREL) != 0)
1975 {
1976 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 1977 return FALSE;
c434dee6 1978 }
70256ad8
AJ
1979 }
1980 }
dc810e39 1981#undef add_dynamic_entry
70256ad8 1982
b34976b6 1983 return TRUE;
70256ad8
AJ
1984}
1985
67a4f2b7
AO
1986static bfd_boolean
1987elf64_x86_64_always_size_sections (bfd *output_bfd,
1988 struct bfd_link_info *info)
1989{
1990 asection *tls_sec = elf_hash_table (info)->tls_sec;
1991
1992 if (tls_sec)
1993 {
1994 struct elf_link_hash_entry *tlsbase;
1995
1996 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
1997 "_TLS_MODULE_BASE_",
1998 FALSE, FALSE, FALSE);
1999
2000 if (tlsbase && tlsbase->type == STT_TLS)
2001 {
2002 struct bfd_link_hash_entry *bh = NULL;
2003 const struct elf_backend_data *bed
2004 = get_elf_backend_data (output_bfd);
2005
2006 if (!(_bfd_generic_link_add_one_symbol
2007 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2008 tls_sec, 0, NULL, FALSE,
2009 bed->collect, &bh)))
2010 return FALSE;
2011 tlsbase = (struct elf_link_hash_entry *)bh;
2012 tlsbase->def_regular = 1;
2013 tlsbase->other = STV_HIDDEN;
2014 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2015 }
2016 }
2017
2018 return TRUE;
2019}
2020
bffbf940
JJ
2021/* Return the base VMA address which should be subtracted from real addresses
2022 when resolving @dtpoff relocation.
2023 This is PT_TLS segment p_vaddr. */
2024
2025static bfd_vma
27482721 2026dtpoff_base (struct bfd_link_info *info)
bffbf940 2027{
e1918d23
AM
2028 /* If tls_sec is NULL, we should have signalled an error already. */
2029 if (elf_hash_table (info)->tls_sec == NULL)
bffbf940 2030 return 0;
e1918d23 2031 return elf_hash_table (info)->tls_sec->vma;
bffbf940
JJ
2032}
2033
2034/* Return the relocation value for @tpoff relocation
2035 if STT_TLS virtual address is ADDRESS. */
2036
2037static bfd_vma
27482721 2038tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 2039{
e1918d23 2040 struct elf_link_hash_table *htab = elf_hash_table (info);
bffbf940
JJ
2041
2042 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 2043 if (htab->tls_sec == NULL)
bffbf940 2044 return 0;
e1918d23 2045 return address - htab->tls_size - htab->tls_sec->vma;
bffbf940
JJ
2046}
2047
90f487df
L
2048/* Is the instruction before OFFSET in CONTENTS a 32bit relative
2049 branch? */
2050
2051static bfd_boolean
2052is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2053{
2054 /* Opcode Instruction
2055 0xe8 call
2056 0xe9 jump
2057 0x0f 0x8x conditional jump */
2058 return ((offset > 0
2059 && (contents [offset - 1] == 0xe8
2060 || contents [offset - 1] == 0xe9))
2061 || (offset > 1
2062 && contents [offset - 2] == 0x0f
2063 && (contents [offset - 1] & 0xf0) == 0x80));
2064}
2065
8d88c4ca
NC
2066/* Relocate an x86_64 ELF section. */
2067
b34976b6 2068static bfd_boolean
27482721
AJ
2069elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2070 bfd *input_bfd, asection *input_section,
2071 bfd_byte *contents, Elf_Internal_Rela *relocs,
2072 Elf_Internal_Sym *local_syms,
2073 asection **local_sections)
8d88c4ca 2074{
c434dee6 2075 struct elf64_x86_64_link_hash_table *htab;
8d88c4ca
NC
2076 Elf_Internal_Shdr *symtab_hdr;
2077 struct elf_link_hash_entry **sym_hashes;
2078 bfd_vma *local_got_offsets;
67a4f2b7 2079 bfd_vma *local_tlsdesc_gotents;
c434dee6 2080 Elf_Internal_Rela *rel;
8d88c4ca
NC
2081 Elf_Internal_Rela *relend;
2082
1049f94e 2083 if (info->relocatable)
b34976b6 2084 return TRUE;
b491616a 2085
c434dee6 2086 htab = elf64_x86_64_hash_table (info);
8d88c4ca
NC
2087 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2088 sym_hashes = elf_sym_hashes (input_bfd);
2089 local_got_offsets = elf_local_got_offsets (input_bfd);
67a4f2b7 2090 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
8d88c4ca 2091
c434dee6 2092 rel = relocs;
8d88c4ca 2093 relend = relocs + input_section->reloc_count;
c434dee6 2094 for (; rel < relend; rel++)
8d88c4ca 2095 {
bffbf940 2096 unsigned int r_type;
8d88c4ca
NC
2097 reloc_howto_type *howto;
2098 unsigned long r_symndx;
2099 struct elf_link_hash_entry *h;
2100 Elf_Internal_Sym *sym;
2101 asection *sec;
67a4f2b7 2102 bfd_vma off, offplt;
8d88c4ca 2103 bfd_vma relocation;
b34976b6 2104 bfd_boolean unresolved_reloc;
8d88c4ca 2105 bfd_reloc_status_type r;
bffbf940 2106 int tls_type;
8d88c4ca 2107
c434dee6 2108 r_type = ELF64_R_TYPE (rel->r_info);
fe4770f4
AJ
2109 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2110 || r_type == (int) R_X86_64_GNU_VTENTRY)
2111 continue;
8d88c4ca 2112
bffbf940 2113 if (r_type >= R_X86_64_max)
8da6118f
KH
2114 {
2115 bfd_set_error (bfd_error_bad_value);
b34976b6 2116 return FALSE;
8da6118f 2117 }
8d88c4ca 2118
b491616a 2119 howto = x86_64_elf_howto_table + r_type;
c434dee6 2120 r_symndx = ELF64_R_SYM (rel->r_info);
8d88c4ca
NC
2121 h = NULL;
2122 sym = NULL;
2123 sec = NULL;
b34976b6 2124 unresolved_reloc = FALSE;
8d88c4ca 2125 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
2126 {
2127 sym = local_syms + r_symndx;
2128 sec = local_sections[r_symndx];
c434dee6 2129
8517fae7 2130 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8da6118f 2131 }
8d88c4ca 2132 else
8da6118f 2133 {
560e09e9 2134 bfd_boolean warned;
c434dee6 2135
b2a8e766
AM
2136 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2137 r_symndx, symtab_hdr, sym_hashes,
2138 h, sec, relocation,
2139 unresolved_reloc, warned);
8da6118f 2140 }
70256ad8
AJ
2141 /* When generating a shared object, the relocations handled here are
2142 copied into the output file to be resolved at run time. */
2143 switch (r_type)
2144 {
7b81dfbb 2145 asection *base_got;
70256ad8 2146 case R_X86_64_GOT32:
7b81dfbb 2147 case R_X86_64_GOT64:
70256ad8
AJ
2148 /* Relocation is to the entry for this symbol in the global
2149 offset table. */
70256ad8 2150 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
2151 case R_X86_64_GOTPCREL64:
2152 /* Use global offset table entry as symbol value. */
2153 case R_X86_64_GOTPLT64:
2154 /* This is the same as GOT64 for relocation purposes, but
2155 indicates the existence of a PLT entry. The difficulty is,
2156 that we must calculate the GOT slot offset from the PLT
2157 offset, if this symbol got a PLT entry (it was global).
2158 Additionally if it's computed from the PLT entry, then that
2159 GOT offset is relative to .got.plt, not to .got. */
2160 base_got = htab->sgot;
2161
c434dee6
AJ
2162 if (htab->sgot == NULL)
2163 abort ();
053579d7 2164
51e0a107 2165 if (h != NULL)
70256ad8 2166 {
b34976b6 2167 bfd_boolean dyn;
c434dee6
AJ
2168
2169 off = h->got.offset;
7b81dfbb
AJ
2170 if (h->needs_plt
2171 && h->plt.offset != (bfd_vma)-1
2172 && off == (bfd_vma)-1)
2173 {
2174 /* We can't use h->got.offset here to save
2175 state, or even just remember the offset, as
2176 finish_dynamic_symbol would use that as offset into
2177 .got. */
2178 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2179 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2180 base_got = htab->sgotplt;
2181 }
2182
c434dee6 2183 dyn = htab->elf.dynamic_sections_created;
51e0a107 2184
27482721 2185 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
51e0a107 2186 || (info->shared
27482721 2187 && SYMBOL_REFERENCES_LOCAL (info, h))
4bc6e03a
AJ
2188 || (ELF_ST_VISIBILITY (h->other)
2189 && h->root.type == bfd_link_hash_undefweak))
51e0a107
JH
2190 {
2191 /* This is actually a static link, or it is a -Bsymbolic
2192 link and the symbol is defined locally, or the symbol
407443a3 2193 was forced to be local because of a version file. We
51e0a107
JH
2194 must initialize this entry in the global offset table.
2195 Since the offset must always be a multiple of 8, we
2196 use the least significant bit to record whether we
2197 have initialized it already.
2198
2199 When doing a dynamic link, we create a .rela.got
407443a3
AJ
2200 relocation entry to initialize the value. This is
2201 done in the finish_dynamic_symbol routine. */
51e0a107
JH
2202 if ((off & 1) != 0)
2203 off &= ~1;
2204 else
2205 {
2206 bfd_put_64 (output_bfd, relocation,
7b81dfbb
AJ
2207 base_got->contents + off);
2208 /* Note that this is harmless for the GOTPLT64 case,
2209 as -1 | 1 still is -1. */
51e0a107
JH
2210 h->got.offset |= 1;
2211 }
2212 }
053579d7 2213 else
b34976b6 2214 unresolved_reloc = FALSE;
70256ad8 2215 }
51e0a107
JH
2216 else
2217 {
c434dee6
AJ
2218 if (local_got_offsets == NULL)
2219 abort ();
51e0a107
JH
2220
2221 off = local_got_offsets[r_symndx];
2222
2223 /* The offset must always be a multiple of 8. We use
407443a3
AJ
2224 the least significant bit to record whether we have
2225 already generated the necessary reloc. */
51e0a107
JH
2226 if ((off & 1) != 0)
2227 off &= ~1;
2228 else
2229 {
c434dee6 2230 bfd_put_64 (output_bfd, relocation,
7b81dfbb 2231 base_got->contents + off);
51e0a107
JH
2232
2233 if (info->shared)
2234 {
947216bf 2235 asection *s;
51e0a107 2236 Elf_Internal_Rela outrel;
947216bf 2237 bfd_byte *loc;
70256ad8 2238
51e0a107
JH
2239 /* We need to generate a R_X86_64_RELATIVE reloc
2240 for the dynamic linker. */
947216bf
AM
2241 s = htab->srelgot;
2242 if (s == NULL)
c434dee6 2243 abort ();
51e0a107 2244
7b81dfbb
AJ
2245 outrel.r_offset = (base_got->output_section->vma
2246 + base_got->output_offset
51e0a107
JH
2247 + off);
2248 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2249 outrel.r_addend = relocation;
947216bf
AM
2250 loc = s->contents;
2251 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2252 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
51e0a107
JH
2253 }
2254
2255 local_got_offsets[r_symndx] |= 1;
2256 }
51e0a107 2257 }
6a2bda3f 2258
c434dee6
AJ
2259 if (off >= (bfd_vma) -2)
2260 abort ();
2261
7b81dfbb
AJ
2262 relocation = base_got->output_section->vma
2263 + base_got->output_offset + off;
2264 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
8c37241b
JJ
2265 relocation -= htab->sgotplt->output_section->vma
2266 - htab->sgotplt->output_offset;
c434dee6 2267
70256ad8
AJ
2268 break;
2269
d6ab8113
JB
2270 case R_X86_64_GOTOFF64:
2271 /* Relocation is relative to the start of the global offset
2272 table. */
2273
2274 /* Check to make sure it isn't a protected function symbol
2275 for shared library since it may not be local when used
2276 as function address. */
2277 if (info->shared
2278 && h
2279 && h->def_regular
2280 && h->type == STT_FUNC
2281 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2282 {
2283 (*_bfd_error_handler)
2284 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2285 input_bfd, h->root.root.string);
2286 bfd_set_error (bfd_error_bad_value);
2287 return FALSE;
2288 }
2289
2290 /* Note that sgot is not involved in this
2291 calculation. We always want the start of .got.plt. If we
2292 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2293 permitted by the ABI, we might have to change this
2294 calculation. */
2295 relocation -= htab->sgotplt->output_section->vma
2296 + htab->sgotplt->output_offset;
2297 break;
2298
2299 case R_X86_64_GOTPC32:
7b81dfbb 2300 case R_X86_64_GOTPC64:
d6ab8113
JB
2301 /* Use global offset table as symbol value. */
2302 relocation = htab->sgotplt->output_section->vma
2303 + htab->sgotplt->output_offset;
2304 unresolved_reloc = FALSE;
2305 break;
7b81dfbb
AJ
2306
2307 case R_X86_64_PLTOFF64:
2308 /* Relocation is PLT entry relative to GOT. For local
2309 symbols it's the symbol itself relative to GOT. */
2310 if (h != NULL
2311 /* See PLT32 handling. */
2312 && h->plt.offset != (bfd_vma) -1
2313 && htab->splt != NULL)
2314 {
2315 relocation = (htab->splt->output_section->vma
2316 + htab->splt->output_offset
2317 + h->plt.offset);
2318 unresolved_reloc = FALSE;
2319 }
2320
2321 relocation -= htab->sgotplt->output_section->vma
2322 + htab->sgotplt->output_offset;
2323 break;
d6ab8113 2324
70256ad8
AJ
2325 case R_X86_64_PLT32:
2326 /* Relocation is to the entry for this symbol in the
2327 procedure linkage table. */
2328
2329 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 2330 without using the procedure linkage table. */
70256ad8
AJ
2331 if (h == NULL)
2332 break;
2333
c434dee6
AJ
2334 if (h->plt.offset == (bfd_vma) -1
2335 || htab->splt == NULL)
70256ad8
AJ
2336 {
2337 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
2338 happens when statically linking PIC code, or when
2339 using -Bsymbolic. */
70256ad8
AJ
2340 break;
2341 }
2342
c434dee6
AJ
2343 relocation = (htab->splt->output_section->vma
2344 + htab->splt->output_offset
70256ad8 2345 + h->plt.offset);
b34976b6 2346 unresolved_reloc = FALSE;
70256ad8
AJ
2347 break;
2348
fd8ab9e5
AJ
2349 case R_X86_64_PC8:
2350 case R_X86_64_PC16:
2351 case R_X86_64_PC32:
6610a52d
L
2352 if (info->shared
2353 && !SYMBOL_REFERENCES_LOCAL (info, h)
ba3bee0b 2354 && (input_section->flags & SEC_ALLOC) != 0
90f487df
L
2355 && (input_section->flags & SEC_READONLY) != 0
2356 && (!h->def_regular
2357 || r_type != R_X86_64_PC32
2358 || h->type != STT_FUNC
2359 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
2360 || !is_32bit_relative_branch (contents,
2361 rel->r_offset)))
6610a52d 2362 {
90f487df
L
2363 if (h->def_regular
2364 && r_type == R_X86_64_PC32
2365 && h->type == STT_FUNC
2366 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2367 (*_bfd_error_handler)
2368 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
2369 input_bfd, h->root.root.string);
2370 else
2371 (*_bfd_error_handler)
2372 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2373 input_bfd, x86_64_elf_howto_table[r_type].name,
2374 h->root.root.string);
6610a52d
L
2375 bfd_set_error (bfd_error_bad_value);
2376 return FALSE;
2377 }
2378 /* Fall through. */
2379
70256ad8
AJ
2380 case R_X86_64_8:
2381 case R_X86_64_16:
2382 case R_X86_64_32:
d6ab8113 2383 case R_X86_64_PC64:
6b3db546 2384 case R_X86_64_64:
80643fbc 2385 /* FIXME: The ABI says the linker should make sure the value is
407443a3 2386 the same when it's zeroextended to 64 bit. */
c434dee6
AJ
2387
2388 /* r_symndx will be zero only for relocs against symbols
2389 from removed linkonce sections, or sections discarded by
2390 a linker script. */
2391 if (r_symndx == 0
2392 || (input_section->flags & SEC_ALLOC) == 0)
2393 break;
2394
2395 if ((info->shared
4bc6e03a
AJ
2396 && (h == NULL
2397 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2398 || h->root.type != bfd_link_hash_undefweak)
c434dee6
AJ
2399 && ((r_type != R_X86_64_PC8
2400 && r_type != R_X86_64_PC16
d6ab8113
JB
2401 && r_type != R_X86_64_PC32
2402 && r_type != R_X86_64_PC64)
f6c52c13 2403 || !SYMBOL_CALLS_LOCAL (info, h)))
d40d037c
AJ
2404 || (ELIMINATE_COPY_RELOCS
2405 && !info->shared
c434dee6
AJ
2406 && h != NULL
2407 && h->dynindx != -1
f5385ebf
AM
2408 && !h->non_got_ref
2409 && ((h->def_dynamic
2410 && !h->def_regular)
c434dee6
AJ
2411 || h->root.type == bfd_link_hash_undefweak
2412 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
2413 {
2414 Elf_Internal_Rela outrel;
947216bf 2415 bfd_byte *loc;
b34976b6 2416 bfd_boolean skip, relocate;
c434dee6 2417 asection *sreloc;
70256ad8
AJ
2418
2419 /* When generating a shared object, these relocations
2420 are copied into the output file to be resolved at run
407443a3 2421 time. */
b34976b6
AM
2422 skip = FALSE;
2423 relocate = FALSE;
70256ad8 2424
c629eae0
JJ
2425 outrel.r_offset =
2426 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 2427 rel->r_offset);
c629eae0 2428 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 2429 skip = TRUE;
0fb19cbc 2430 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 2431 skip = TRUE, relocate = TRUE;
70256ad8
AJ
2432
2433 outrel.r_offset += (input_section->output_section->vma
2434 + input_section->output_offset);
2435
2436 if (skip)
0bb2d96a 2437 memset (&outrel, 0, sizeof outrel);
c434dee6 2438
fd8ab9e5
AJ
2439 /* h->dynindx may be -1 if this symbol was marked to
2440 become local. */
2441 else if (h != NULL
c434dee6
AJ
2442 && h->dynindx != -1
2443 && (r_type == R_X86_64_PC8
2444 || r_type == R_X86_64_PC16
2445 || r_type == R_X86_64_PC32
d6ab8113 2446 || r_type == R_X86_64_PC64
c434dee6 2447 || !info->shared
55255dae 2448 || !SYMBOLIC_BIND (info, h)
f5385ebf 2449 || !h->def_regular))
70256ad8 2450 {
70256ad8 2451 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
c434dee6 2452 outrel.r_addend = rel->r_addend;
70256ad8
AJ
2453 }
2454 else
2455 {
c434dee6 2456 /* This symbol is local, or marked to become local. */
607c0e09
AS
2457 if (r_type == R_X86_64_64)
2458 {
b34976b6 2459 relocate = TRUE;
607c0e09
AS
2460 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2461 outrel.r_addend = relocation + rel->r_addend;
2462 }
2463 else
2464 {
2465 long sindx;
2466
8517fae7 2467 if (bfd_is_abs_section (sec))
607c0e09
AS
2468 sindx = 0;
2469 else if (sec == NULL || sec->owner == NULL)
2470 {
2471 bfd_set_error (bfd_error_bad_value);
b34976b6 2472 return FALSE;
607c0e09
AS
2473 }
2474 else
2475 {
2476 asection *osec;
2477
2478 osec = sec->output_section;
2479 sindx = elf_section_data (osec)->dynindx;
2480 BFD_ASSERT (sindx > 0);
2481 }
2482
2483 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2484 outrel.r_addend = relocation + rel->r_addend;
2485 }
70256ad8
AJ
2486 }
2487
c434dee6
AJ
2488 sreloc = elf_section_data (input_section)->sreloc;
2489 if (sreloc == NULL)
2490 abort ();
2491
947216bf
AM
2492 loc = sreloc->contents;
2493 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2494 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
70256ad8
AJ
2495
2496 /* If this reloc is against an external symbol, we do
2497 not want to fiddle with the addend. Otherwise, we
2498 need to include the symbol value so that it becomes
2499 an addend for the dynamic reloc. */
2500 if (! relocate)
2501 continue;
2502 }
2503
2504 break;
2505
bffbf940 2506 case R_X86_64_TLSGD:
67a4f2b7
AO
2507 case R_X86_64_GOTPC32_TLSDESC:
2508 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
2509 case R_X86_64_GOTTPOFF:
2510 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2511 tls_type = GOT_UNKNOWN;
2512 if (h == NULL && local_got_offsets)
2513 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2514 else if (h != NULL)
2515 {
2516 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2517 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2518 r_type = R_X86_64_TPOFF32;
2519 }
67a4f2b7
AO
2520 if (r_type == R_X86_64_TLSGD
2521 || r_type == R_X86_64_GOTPC32_TLSDESC
2522 || r_type == R_X86_64_TLSDESC_CALL)
bffbf940
JJ
2523 {
2524 if (tls_type == GOT_TLS_IE)
2525 r_type = R_X86_64_GOTTPOFF;
2526 }
2527
2528 if (r_type == R_X86_64_TPOFF32)
2529 {
2530 BFD_ASSERT (! unresolved_reloc);
2531 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2532 {
2533 unsigned int i;
abcf1d52
JJ
2534 static unsigned char tlsgd[8]
2535 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
bffbf940
JJ
2536
2537 /* GD->LE transition.
abcf1d52
JJ
2538 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2539 .word 0x6666; rex64; call __tls_get_addr@plt
bffbf940
JJ
2540 Change it into:
2541 movq %fs:0, %rax
2542 leaq foo@tpoff(%rax), %rax */
abcf1d52
JJ
2543 BFD_ASSERT (rel->r_offset >= 4);
2544 for (i = 0; i < 4; i++)
bffbf940 2545 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52 2546 contents + rel->r_offset - 4 + i)
bffbf940 2547 == tlsgd[i]);
eea6121a 2548 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
abcf1d52
JJ
2549 for (i = 0; i < 4; i++)
2550 BFD_ASSERT (bfd_get_8 (input_bfd,
2551 contents + rel->r_offset + 4 + i)
2552 == tlsgd[i+4]);
bffbf940
JJ
2553 BFD_ASSERT (rel + 1 < relend);
2554 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
abcf1d52 2555 memcpy (contents + rel->r_offset - 4,
bffbf940
JJ
2556 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2557 16);
2558 bfd_put_32 (output_bfd, tpoff (info, relocation),
abcf1d52 2559 contents + rel->r_offset + 8);
bffbf940
JJ
2560 /* Skip R_X86_64_PLT32. */
2561 rel++;
2562 continue;
2563 }
67a4f2b7
AO
2564 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2565 {
2566 /* GDesc -> LE transition.
2567 It's originally something like:
2568 leaq x@tlsdesc(%rip), %rax
2569
2570 Change it to:
2571 movl $x@tpoff, %rax
2572
2573 Registers other than %rax may be set up here. */
2574
2575 unsigned int val, type, type2;
2576 bfd_vma roff;
2577
2578 /* First, make sure it's a leaq adding rip to a
2579 32-bit offset into any register, although it's
2580 probably almost always going to be rax. */
2581 roff = rel->r_offset;
2582 BFD_ASSERT (roff >= 3);
2583 type = bfd_get_8 (input_bfd, contents + roff - 3);
2584 BFD_ASSERT ((type & 0xfb) == 0x48);
2585 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2586 BFD_ASSERT (type2 == 0x8d);
2587 val = bfd_get_8 (input_bfd, contents + roff - 1);
2588 BFD_ASSERT ((val & 0xc7) == 0x05);
2589 BFD_ASSERT (roff + 4 <= input_section->size);
2590
2591 /* Now modify the instruction as appropriate. */
2592 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
2593 contents + roff - 3);
2594 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
2595 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2596 contents + roff - 1);
2597 bfd_put_32 (output_bfd, tpoff (info, relocation),
2598 contents + roff);
2599 continue;
2600 }
2601 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2602 {
2603 /* GDesc -> LE transition.
2604 It's originally:
2605 call *(%rax)
2606 Turn it into:
2607 nop; nop. */
2608
2609 unsigned int val, type;
2610 bfd_vma roff;
2611
2612 /* First, make sure it's a call *(%rax). */
2613 roff = rel->r_offset;
2614 BFD_ASSERT (roff + 2 <= input_section->size);
2615 type = bfd_get_8 (input_bfd, contents + roff);
2616 BFD_ASSERT (type == 0xff);
2617 val = bfd_get_8 (input_bfd, contents + roff + 1);
2618 BFD_ASSERT (val == 0x10);
2619
10efb593
L
2620 /* Now modify the instruction as appropriate. Use
2621 xchg %ax,%ax instead of 2 nops. */
2622 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
2623 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2624 continue;
2625 }
bffbf940
JJ
2626 else
2627 {
2628 unsigned int val, type, reg;
2629
2630 /* IE->LE transition:
2631 Originally it can be one of:
2632 movq foo@gottpoff(%rip), %reg
2633 addq foo@gottpoff(%rip), %reg
2634 We change it into:
2635 movq $foo, %reg
2636 leaq foo(%reg), %reg
2637 addq $foo, %reg. */
2638 BFD_ASSERT (rel->r_offset >= 3);
2639 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2640 BFD_ASSERT (val == 0x48 || val == 0x4c);
2641 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2642 BFD_ASSERT (type == 0x8b || type == 0x03);
2643 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2644 BFD_ASSERT ((reg & 0xc7) == 5);
2645 reg >>= 3;
eea6121a 2646 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
bffbf940
JJ
2647 if (type == 0x8b)
2648 {
2649 /* movq */
2650 if (val == 0x4c)
2651 bfd_put_8 (output_bfd, 0x49,
2652 contents + rel->r_offset - 3);
2653 bfd_put_8 (output_bfd, 0xc7,
2654 contents + rel->r_offset - 2);
2655 bfd_put_8 (output_bfd, 0xc0 | reg,
2656 contents + rel->r_offset - 1);
2657 }
2658 else if (reg == 4)
2659 {
2660 /* addq -> addq - addressing with %rsp/%r12 is
2661 special */
2662 if (val == 0x4c)
2663 bfd_put_8 (output_bfd, 0x49,
2664 contents + rel->r_offset - 3);
2665 bfd_put_8 (output_bfd, 0x81,
2666 contents + rel->r_offset - 2);
2667 bfd_put_8 (output_bfd, 0xc0 | reg,
2668 contents + rel->r_offset - 1);
2669 }
2670 else
2671 {
2672 /* addq -> leaq */
2673 if (val == 0x4c)
2674 bfd_put_8 (output_bfd, 0x4d,
2675 contents + rel->r_offset - 3);
2676 bfd_put_8 (output_bfd, 0x8d,
2677 contents + rel->r_offset - 2);
2678 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2679 contents + rel->r_offset - 1);
2680 }
2681 bfd_put_32 (output_bfd, tpoff (info, relocation),
2682 contents + rel->r_offset);
2683 continue;
2684 }
2685 }
2686
2687 if (htab->sgot == NULL)
2688 abort ();
2689
2690 if (h != NULL)
67a4f2b7
AO
2691 {
2692 off = h->got.offset;
2693 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
2694 }
bffbf940
JJ
2695 else
2696 {
2697 if (local_got_offsets == NULL)
2698 abort ();
2699
2700 off = local_got_offsets[r_symndx];
67a4f2b7 2701 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
2702 }
2703
2704 if ((off & 1) != 0)
2705 off &= ~1;
26e41594 2706 else
bffbf940
JJ
2707 {
2708 Elf_Internal_Rela outrel;
947216bf 2709 bfd_byte *loc;
bffbf940 2710 int dr_type, indx;
67a4f2b7 2711 asection *sreloc;
bffbf940
JJ
2712
2713 if (htab->srelgot == NULL)
2714 abort ();
2715
67a4f2b7
AO
2716 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2717
2718 if (GOT_TLS_GDESC_P (tls_type))
2719 {
2720 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
2721 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
2722 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
2723 outrel.r_offset = (htab->sgotplt->output_section->vma
2724 + htab->sgotplt->output_offset
2725 + offplt
2726 + htab->sgotplt_jump_table_size);
2727 sreloc = htab->srelplt;
2728 loc = sreloc->contents;
2729 loc += sreloc->reloc_count++
2730 * sizeof (Elf64_External_Rela);
2731 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2732 <= sreloc->contents + sreloc->size);
2733 if (indx == 0)
2734 outrel.r_addend = relocation - dtpoff_base (info);
2735 else
2736 outrel.r_addend = 0;
2737 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2738 }
2739
2740 sreloc = htab->srelgot;
2741
bffbf940
JJ
2742 outrel.r_offset = (htab->sgot->output_section->vma
2743 + htab->sgot->output_offset + off);
2744
67a4f2b7 2745 if (GOT_TLS_GD_P (tls_type))
bffbf940 2746 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
2747 else if (GOT_TLS_GDESC_P (tls_type))
2748 goto dr_done;
bffbf940
JJ
2749 else
2750 dr_type = R_X86_64_TPOFF64;
2751
2752 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2753 outrel.r_addend = 0;
67a4f2b7
AO
2754 if ((dr_type == R_X86_64_TPOFF64
2755 || dr_type == R_X86_64_TLSDESC) && indx == 0)
bffbf940
JJ
2756 outrel.r_addend = relocation - dtpoff_base (info);
2757 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2758
67a4f2b7
AO
2759 loc = sreloc->contents;
2760 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2761 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2762 <= sreloc->contents + sreloc->size);
bffbf940
JJ
2763 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2764
67a4f2b7 2765 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
2766 {
2767 if (indx == 0)
2768 {
d40d037c 2769 BFD_ASSERT (! unresolved_reloc);
bffbf940
JJ
2770 bfd_put_64 (output_bfd,
2771 relocation - dtpoff_base (info),
2772 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2773 }
2774 else
2775 {
2776 bfd_put_64 (output_bfd, 0,
2777 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2778 outrel.r_info = ELF64_R_INFO (indx,
2779 R_X86_64_DTPOFF64);
2780 outrel.r_offset += GOT_ENTRY_SIZE;
67a4f2b7 2781 sreloc->reloc_count++;
947216bf 2782 loc += sizeof (Elf64_External_Rela);
67a4f2b7
AO
2783 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2784 <= sreloc->contents + sreloc->size);
947216bf 2785 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
bffbf940
JJ
2786 }
2787 }
2788
67a4f2b7 2789 dr_done:
bffbf940
JJ
2790 if (h != NULL)
2791 h->got.offset |= 1;
2792 else
2793 local_got_offsets[r_symndx] |= 1;
2794 }
2795
67a4f2b7
AO
2796 if (off >= (bfd_vma) -2
2797 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940
JJ
2798 abort ();
2799 if (r_type == ELF64_R_TYPE (rel->r_info))
2800 {
67a4f2b7
AO
2801 if (r_type == R_X86_64_GOTPC32_TLSDESC
2802 || r_type == R_X86_64_TLSDESC_CALL)
2803 relocation = htab->sgotplt->output_section->vma
2804 + htab->sgotplt->output_offset
2805 + offplt + htab->sgotplt_jump_table_size;
2806 else
2807 relocation = htab->sgot->output_section->vma
2808 + htab->sgot->output_offset + off;
b34976b6 2809 unresolved_reloc = FALSE;
bffbf940 2810 }
67a4f2b7 2811 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
bffbf940
JJ
2812 {
2813 unsigned int i;
abcf1d52
JJ
2814 static unsigned char tlsgd[8]
2815 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
bffbf940
JJ
2816
2817 /* GD->IE transition.
abcf1d52
JJ
2818 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2819 .word 0x6666; rex64; call __tls_get_addr@plt
bffbf940
JJ
2820 Change it into:
2821 movq %fs:0, %rax
2822 addq foo@gottpoff(%rip), %rax */
abcf1d52
JJ
2823 BFD_ASSERT (rel->r_offset >= 4);
2824 for (i = 0; i < 4; i++)
26e41594 2825 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52 2826 contents + rel->r_offset - 4 + i)
bffbf940 2827 == tlsgd[i]);
eea6121a 2828 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
abcf1d52 2829 for (i = 0; i < 4; i++)
26e41594 2830 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52
JJ
2831 contents + rel->r_offset + 4 + i)
2832 == tlsgd[i+4]);
bffbf940
JJ
2833 BFD_ASSERT (rel + 1 < relend);
2834 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
abcf1d52 2835 memcpy (contents + rel->r_offset - 4,
bffbf940
JJ
2836 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2837 16);
2838
2839 relocation = (htab->sgot->output_section->vma
2840 + htab->sgot->output_offset + off
2841 - rel->r_offset
2842 - input_section->output_section->vma
2843 - input_section->output_offset
abcf1d52 2844 - 12);
bffbf940 2845 bfd_put_32 (output_bfd, relocation,
abcf1d52 2846 contents + rel->r_offset + 8);
bffbf940
JJ
2847 /* Skip R_X86_64_PLT32. */
2848 rel++;
2849 continue;
2850 }
67a4f2b7
AO
2851 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2852 {
2853 /* GDesc -> IE transition.
2854 It's originally something like:
2855 leaq x@tlsdesc(%rip), %rax
2856
2857 Change it to:
2858 movq x@gottpoff(%rip), %rax # before nop; nop
2859
2860 Registers other than %rax may be set up here. */
2861
2862 unsigned int val, type, type2;
2863 bfd_vma roff;
2864
2865 /* First, make sure it's a leaq adding rip to a 32-bit
2866 offset into any register, although it's probably
2867 almost always going to be rax. */
2868 roff = rel->r_offset;
2869 BFD_ASSERT (roff >= 3);
2870 type = bfd_get_8 (input_bfd, contents + roff - 3);
2871 BFD_ASSERT ((type & 0xfb) == 0x48);
2872 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2873 BFD_ASSERT (type2 == 0x8d);
2874 val = bfd_get_8 (input_bfd, contents + roff - 1);
2875 BFD_ASSERT ((val & 0xc7) == 0x05);
2876 BFD_ASSERT (roff + 4 <= input_section->size);
2877
2878 /* Now modify the instruction as appropriate. */
2879 /* To turn a leaq into a movq in the form we use it, it
2880 suffices to change the second byte from 0x8d to
2881 0x8b. */
2882 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
2883
2884 bfd_put_32 (output_bfd,
2885 htab->sgot->output_section->vma
2886 + htab->sgot->output_offset + off
2887 - rel->r_offset
2888 - input_section->output_section->vma
2889 - input_section->output_offset
2890 - 4,
2891 contents + roff);
2892 continue;
2893 }
2894 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2895 {
2896 /* GDesc -> IE transition.
2897 It's originally:
2898 call *(%rax)
2899
2900 Change it to:
2901 nop; nop. */
2902
2903 unsigned int val, type;
2904 bfd_vma roff;
2905
2906 /* First, make sure it's a call *(%eax). */
2907 roff = rel->r_offset;
2908 BFD_ASSERT (roff + 2 <= input_section->size);
2909 type = bfd_get_8 (input_bfd, contents + roff);
2910 BFD_ASSERT (type == 0xff);
2911 val = bfd_get_8 (input_bfd, contents + roff + 1);
2912 BFD_ASSERT (val == 0x10);
2913
10efb593
L
2914 /* Now modify the instruction as appropriate. Use
2915 xchg %ax,%ax instead of 2 nops. */
2916 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
2917 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2918
2919 continue;
2920 }
2921 else
2922 BFD_ASSERT (FALSE);
bffbf940
JJ
2923 break;
2924
2925 case R_X86_64_TLSLD:
2926 if (! info->shared)
2927 {
2928 /* LD->LE transition:
2929 Ensure it is:
2930 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2931 We change it into:
2932 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2933 BFD_ASSERT (rel->r_offset >= 3);
2934 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2935 == 0x48);
2936 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2937 == 0x8d);
2938 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2939 == 0x3d);
eea6121a 2940 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
bffbf940
JJ
2941 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2942 == 0xe8);
2943 BFD_ASSERT (rel + 1 < relend);
2944 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2945 memcpy (contents + rel->r_offset - 3,
2946 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2947 /* Skip R_X86_64_PLT32. */
2948 rel++;
2949 continue;
2950 }
2951
2952 if (htab->sgot == NULL)
2953 abort ();
2954
2955 off = htab->tls_ld_got.offset;
2956 if (off & 1)
2957 off &= ~1;
2958 else
2959 {
2960 Elf_Internal_Rela outrel;
947216bf 2961 bfd_byte *loc;
bffbf940
JJ
2962
2963 if (htab->srelgot == NULL)
2964 abort ();
2965
2966 outrel.r_offset = (htab->sgot->output_section->vma
2967 + htab->sgot->output_offset + off);
2968
2969 bfd_put_64 (output_bfd, 0,
2970 htab->sgot->contents + off);
2971 bfd_put_64 (output_bfd, 0,
2972 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2973 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2974 outrel.r_addend = 0;
947216bf
AM
2975 loc = htab->srelgot->contents;
2976 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
bffbf940
JJ
2977 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2978 htab->tls_ld_got.offset |= 1;
2979 }
2980 relocation = htab->sgot->output_section->vma
2981 + htab->sgot->output_offset + off;
b34976b6 2982 unresolved_reloc = FALSE;
bffbf940
JJ
2983 break;
2984
2985 case R_X86_64_DTPOFF32:
a45bb67d 2986 if (info->shared || (input_section->flags & SEC_CODE) == 0)
bffbf940
JJ
2987 relocation -= dtpoff_base (info);
2988 else
2989 relocation = tpoff (info, relocation);
2990 break;
2991
2992 case R_X86_64_TPOFF32:
2993 BFD_ASSERT (! info->shared);
2994 relocation = tpoff (info, relocation);
2995 break;
2996
70256ad8
AJ
2997 default:
2998 break;
2999 }
8d88c4ca 3000
239e1f3a
AM
3001 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3002 because such sections are not SEC_ALLOC and thus ld.so will
3003 not process them. */
c434dee6 3004 if (unresolved_reloc
239e1f3a 3005 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3006 && h->def_dynamic))
c434dee6 3007 (*_bfd_error_handler)
843fe662 3008 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
3009 input_bfd,
3010 input_section,
c434dee6 3011 (long) rel->r_offset,
843fe662 3012 howto->name,
c434dee6
AJ
3013 h->root.root.string);
3014
8d88c4ca 3015 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
3016 contents, rel->r_offset,
3017 relocation, rel->r_addend);
8d88c4ca
NC
3018
3019 if (r != bfd_reloc_ok)
8da6118f 3020 {
c434dee6
AJ
3021 const char *name;
3022
3023 if (h != NULL)
3024 name = h->root.root.string;
3025 else
8da6118f 3026 {
c434dee6
AJ
3027 name = bfd_elf_string_from_elf_section (input_bfd,
3028 symtab_hdr->sh_link,
3029 sym->st_name);
3030 if (name == NULL)
b34976b6 3031 return FALSE;
c434dee6
AJ
3032 if (*name == '\0')
3033 name = bfd_section_name (input_bfd, sec);
3034 }
3035
3036 if (r == bfd_reloc_overflow)
3037 {
3ffa5234
AM
3038 if (h != NULL
3039 && h->root.type == bfd_link_hash_undefweak
3040 && howto->pc_relative)
3041 /* Ignore reloc overflow on branches to undefweak syms. */
3042 continue;
c434dee6
AJ
3043
3044 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
3045 (info, (h ? &h->root : NULL), name, howto->name,
3046 (bfd_vma) 0, input_bfd, input_section,
3047 rel->r_offset)))
b34976b6 3048 return FALSE;
c434dee6
AJ
3049 }
3050 else
3051 {
3052 (*_bfd_error_handler)
d003868e
AM
3053 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3054 input_bfd, input_section,
c434dee6 3055 (long) rel->r_offset, name, (int) r);
b34976b6 3056 return FALSE;
8da6118f
KH
3057 }
3058 }
8d88c4ca 3059 }
70256ad8 3060
b34976b6 3061 return TRUE;
70256ad8
AJ
3062}
3063
3064/* Finish up dynamic symbol handling. We set the contents of various
3065 dynamic sections here. */
3066
b34976b6 3067static bfd_boolean
27482721
AJ
3068elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3069 struct bfd_link_info *info,
3070 struct elf_link_hash_entry *h,
3071 Elf_Internal_Sym *sym)
70256ad8 3072{
c434dee6 3073 struct elf64_x86_64_link_hash_table *htab;
70256ad8 3074
c434dee6 3075 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
3076
3077 if (h->plt.offset != (bfd_vma) -1)
3078 {
70256ad8
AJ
3079 bfd_vma plt_index;
3080 bfd_vma got_offset;
3081 Elf_Internal_Rela rela;
947216bf 3082 bfd_byte *loc;
70256ad8
AJ
3083
3084 /* This symbol has an entry in the procedure linkage table. Set
407443a3 3085 it up. */
c434dee6
AJ
3086 if (h->dynindx == -1
3087 || htab->splt == NULL
3088 || htab->sgotplt == NULL
3089 || htab->srelplt == NULL)
3090 abort ();
70256ad8
AJ
3091
3092 /* Get the index in the procedure linkage table which
3093 corresponds to this symbol. This is the index of this symbol
3094 in all the symbols for which we are making plt entries. The
3095 first entry in the procedure linkage table is reserved. */
3096 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3097
3098 /* Get the offset into the .got table of the entry that
407443a3 3099 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
fe4770f4 3100 bytes. The first three are reserved for the dynamic linker. */
70256ad8
AJ
3101 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3102
3103 /* Fill in the entry in the procedure linkage table. */
c434dee6 3104 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
70256ad8
AJ
3105 PLT_ENTRY_SIZE);
3106
3107 /* Insert the relocation positions of the plt section. The magic
3108 numbers at the end of the statements are the positions of the
3109 relocations in the plt section. */
653165cc
AJ
3110 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3111 instruction uses 6 bytes, subtract this value. */
3112 bfd_put_32 (output_bfd,
c434dee6
AJ
3113 (htab->sgotplt->output_section->vma
3114 + htab->sgotplt->output_offset
653165cc 3115 + got_offset
c434dee6
AJ
3116 - htab->splt->output_section->vma
3117 - htab->splt->output_offset
653165cc
AJ
3118 - h->plt.offset
3119 - 6),
c434dee6 3120 htab->splt->contents + h->plt.offset + 2);
653165cc
AJ
3121 /* Put relocation index. */
3122 bfd_put_32 (output_bfd, plt_index,
c434dee6 3123 htab->splt->contents + h->plt.offset + 7);
653165cc
AJ
3124 /* Put offset for jmp .PLT0. */
3125 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
c434dee6 3126 htab->splt->contents + h->plt.offset + 12);
70256ad8 3127
653165cc
AJ
3128 /* Fill in the entry in the global offset table, initially this
3129 points to the pushq instruction in the PLT which is at offset 6. */
c434dee6
AJ
3130 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
3131 + htab->splt->output_offset
70256ad8 3132 + h->plt.offset + 6),
c434dee6 3133 htab->sgotplt->contents + got_offset);
70256ad8
AJ
3134
3135 /* Fill in the entry in the .rela.plt section. */
c434dee6
AJ
3136 rela.r_offset = (htab->sgotplt->output_section->vma
3137 + htab->sgotplt->output_offset
70256ad8
AJ
3138 + got_offset);
3139 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3140 rela.r_addend = 0;
947216bf 3141 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
c434dee6 3142 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8 3143
f5385ebf 3144 if (!h->def_regular)
70256ad8
AJ
3145 {
3146 /* Mark the symbol as undefined, rather than as defined in
47a9f7b3
JJ
3147 the .plt section. Leave the value if there were any
3148 relocations where pointer equality matters (this is a clue
c434dee6
AJ
3149 for the dynamic linker, to make function pointer
3150 comparisons work between an application and shared
47a9f7b3
JJ
3151 library), otherwise set it to zero. If a function is only
3152 called from a binary, there is no need to slow down
3153 shared libraries because of that. */
70256ad8 3154 sym->st_shndx = SHN_UNDEF;
f5385ebf 3155 if (!h->pointer_equality_needed)
47a9f7b3 3156 sym->st_value = 0;
70256ad8
AJ
3157 }
3158 }
3159
bffbf940 3160 if (h->got.offset != (bfd_vma) -1
67a4f2b7 3161 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
bffbf940 3162 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
053579d7 3163 {
053579d7 3164 Elf_Internal_Rela rela;
947216bf 3165 bfd_byte *loc;
053579d7
AJ
3166
3167 /* This symbol has an entry in the global offset table. Set it
bffbf940 3168 up. */
c434dee6
AJ
3169 if (htab->sgot == NULL || htab->srelgot == NULL)
3170 abort ();
053579d7 3171
c434dee6
AJ
3172 rela.r_offset = (htab->sgot->output_section->vma
3173 + htab->sgot->output_offset
dc810e39 3174 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
3175
3176 /* If this is a static link, or it is a -Bsymbolic link and the
3177 symbol is defined locally or was forced to be local because
3178 of a version file, we just want to emit a RELATIVE reloc.
3179 The entry in the global offset table will already have been
3180 initialized in the relocate_section function. */
c434dee6 3181 if (info->shared
27482721 3182 && SYMBOL_REFERENCES_LOCAL (info, h))
053579d7 3183 {
cc78d0af 3184 BFD_ASSERT((h->got.offset & 1) != 0);
053579d7
AJ
3185 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3186 rela.r_addend = (h->root.u.def.value
3187 + h->root.u.def.section->output_section->vma
3188 + h->root.u.def.section->output_offset);
3189 }
3190 else
3191 {
3192 BFD_ASSERT((h->got.offset & 1) == 0);
c434dee6
AJ
3193 bfd_put_64 (output_bfd, (bfd_vma) 0,
3194 htab->sgot->contents + h->got.offset);
053579d7
AJ
3195 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3196 rela.r_addend = 0;
3197 }
3198
947216bf
AM
3199 loc = htab->srelgot->contents;
3200 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 3201 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
053579d7
AJ
3202 }
3203
f5385ebf 3204 if (h->needs_copy)
70256ad8 3205 {
70256ad8 3206 Elf_Internal_Rela rela;
947216bf 3207 bfd_byte *loc;
70256ad8
AJ
3208
3209 /* This symbol needs a copy reloc. Set it up. */
3210
c434dee6
AJ
3211 if (h->dynindx == -1
3212 || (h->root.type != bfd_link_hash_defined
3213 && h->root.type != bfd_link_hash_defweak)
3214 || htab->srelbss == NULL)
3215 abort ();
70256ad8
AJ
3216
3217 rela.r_offset = (h->root.u.def.value
3218 + h->root.u.def.section->output_section->vma
3219 + h->root.u.def.section->output_offset);
3220 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3221 rela.r_addend = 0;
947216bf
AM
3222 loc = htab->srelbss->contents;
3223 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 3224 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8
AJ
3225 }
3226
3227 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3228 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
22edb2f1 3229 || h == htab->elf.hgot)
70256ad8
AJ
3230 sym->st_shndx = SHN_ABS;
3231
b34976b6 3232 return TRUE;
70256ad8
AJ
3233}
3234
c434dee6
AJ
3235/* Used to decide how to sort relocs in an optimal manner for the
3236 dynamic linker, before writing them out. */
3237
3238static enum elf_reloc_type_class
27482721 3239elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
c434dee6
AJ
3240{
3241 switch ((int) ELF64_R_TYPE (rela->r_info))
3242 {
3243 case R_X86_64_RELATIVE:
3244 return reloc_class_relative;
3245 case R_X86_64_JUMP_SLOT:
3246 return reloc_class_plt;
3247 case R_X86_64_COPY:
3248 return reloc_class_copy;
3249 default:
3250 return reloc_class_normal;
3251 }
3252}
3253
70256ad8
AJ
3254/* Finish up the dynamic sections. */
3255
b34976b6 3256static bfd_boolean
27482721 3257elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
70256ad8 3258{
c434dee6 3259 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
3260 bfd *dynobj;
3261 asection *sdyn;
70256ad8 3262
c434dee6
AJ
3263 htab = elf64_x86_64_hash_table (info);
3264 dynobj = htab->elf.dynobj;
70256ad8
AJ
3265 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3266
c434dee6 3267 if (htab->elf.dynamic_sections_created)
70256ad8 3268 {
70256ad8
AJ
3269 Elf64_External_Dyn *dyncon, *dynconend;
3270
c434dee6
AJ
3271 if (sdyn == NULL || htab->sgot == NULL)
3272 abort ();
70256ad8
AJ
3273
3274 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 3275 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
70256ad8
AJ
3276 for (; dyncon < dynconend; dyncon++)
3277 {
3278 Elf_Internal_Dyn dyn;
70256ad8
AJ
3279 asection *s;
3280
3281 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3282
3283 switch (dyn.d_tag)
3284 {
3285 default:
053579d7 3286 continue;
70256ad8
AJ
3287
3288 case DT_PLTGOT:
8c37241b
JJ
3289 s = htab->sgotplt;
3290 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
c434dee6 3291 break;
70256ad8
AJ
3292
3293 case DT_JMPREL:
c434dee6
AJ
3294 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3295 break;
70256ad8 3296
c434dee6
AJ
3297 case DT_PLTRELSZ:
3298 s = htab->srelplt->output_section;
eea6121a 3299 dyn.d_un.d_val = s->size;
70256ad8
AJ
3300 break;
3301
3302 case DT_RELASZ:
c434dee6
AJ
3303 /* The procedure linkage table relocs (DT_JMPREL) should
3304 not be included in the overall relocs (DT_RELA).
3305 Therefore, we override the DT_RELASZ entry here to
3306 make it not include the JMPREL relocs. Since the
3307 linker script arranges for .rela.plt to follow all
3308 other relocation sections, we don't have to worry
3309 about changing the DT_RELA entry. */
3310 if (htab->srelplt != NULL)
70256ad8 3311 {
c434dee6 3312 s = htab->srelplt->output_section;
eea6121a 3313 dyn.d_un.d_val -= s->size;
70256ad8
AJ
3314 }
3315 break;
67a4f2b7
AO
3316
3317 case DT_TLSDESC_PLT:
3318 s = htab->splt;
3319 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3320 + htab->tlsdesc_plt;
3321 break;
3322
3323 case DT_TLSDESC_GOT:
3324 s = htab->sgot;
3325 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3326 + htab->tlsdesc_got;
3327 break;
70256ad8 3328 }
c434dee6 3329
70256ad8
AJ
3330 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3331 }
3332
c434dee6 3333 /* Fill in the special first entry in the procedure linkage table. */
eea6121a 3334 if (htab->splt && htab->splt->size > 0)
70256ad8 3335 {
653165cc 3336 /* Fill in the first entry in the procedure linkage table. */
c434dee6
AJ
3337 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
3338 PLT_ENTRY_SIZE);
653165cc
AJ
3339 /* Add offset for pushq GOT+8(%rip), since the instruction
3340 uses 6 bytes subtract this value. */
3341 bfd_put_32 (output_bfd,
c434dee6
AJ
3342 (htab->sgotplt->output_section->vma
3343 + htab->sgotplt->output_offset
653165cc 3344 + 8
c434dee6
AJ
3345 - htab->splt->output_section->vma
3346 - htab->splt->output_offset
653165cc 3347 - 6),
c434dee6 3348 htab->splt->contents + 2);
653165cc
AJ
3349 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3350 the end of the instruction. */
3351 bfd_put_32 (output_bfd,
c434dee6
AJ
3352 (htab->sgotplt->output_section->vma
3353 + htab->sgotplt->output_offset
653165cc 3354 + 16
c434dee6
AJ
3355 - htab->splt->output_section->vma
3356 - htab->splt->output_offset
653165cc 3357 - 12),
c434dee6 3358 htab->splt->contents + 8);
653165cc 3359
c434dee6
AJ
3360 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
3361 PLT_ENTRY_SIZE;
67a4f2b7
AO
3362
3363 if (htab->tlsdesc_plt)
3364 {
3365 bfd_put_64 (output_bfd, (bfd_vma) 0,
3366 htab->sgot->contents + htab->tlsdesc_got);
3367
3368 memcpy (htab->splt->contents + htab->tlsdesc_plt,
3369 elf64_x86_64_plt0_entry,
3370 PLT_ENTRY_SIZE);
3371
3372 /* Add offset for pushq GOT+8(%rip), since the
3373 instruction uses 6 bytes subtract this value. */
3374 bfd_put_32 (output_bfd,
3375 (htab->sgotplt->output_section->vma
3376 + htab->sgotplt->output_offset
3377 + 8
3378 - htab->splt->output_section->vma
3379 - htab->splt->output_offset
3380 - htab->tlsdesc_plt
3381 - 6),
3382 htab->splt->contents + htab->tlsdesc_plt + 2);
3383 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3384 htab->tlsdesc_got. The 12 is the offset to the end of
3385 the instruction. */
3386 bfd_put_32 (output_bfd,
3387 (htab->sgot->output_section->vma
3388 + htab->sgot->output_offset
3389 + htab->tlsdesc_got
3390 - htab->splt->output_section->vma
3391 - htab->splt->output_offset
3392 - htab->tlsdesc_plt
3393 - 12),
3394 htab->splt->contents + htab->tlsdesc_plt + 8);
3395 }
70256ad8 3396 }
70256ad8
AJ
3397 }
3398
c434dee6 3399 if (htab->sgotplt)
70256ad8 3400 {
c434dee6 3401 /* Fill in the first three entries in the global offset table. */
eea6121a 3402 if (htab->sgotplt->size > 0)
c434dee6
AJ
3403 {
3404 /* Set the first entry in the global offset table to the address of
3405 the dynamic section. */
3406 if (sdyn == NULL)
3407 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
3408 else
3409 bfd_put_64 (output_bfd,
3410 sdyn->output_section->vma + sdyn->output_offset,
3411 htab->sgotplt->contents);
3412 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3413 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
3414 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
3415 }
70256ad8 3416
c434dee6
AJ
3417 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
3418 GOT_ENTRY_SIZE;
3419 }
70256ad8 3420
eea6121a 3421 if (htab->sgot && htab->sgot->size > 0)
8c37241b
JJ
3422 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
3423 = GOT_ENTRY_SIZE;
3424
b34976b6 3425 return TRUE;
8d88c4ca
NC
3426}
3427
4c45e5c9
JJ
3428/* Return address for Ith PLT stub in section PLT, for relocation REL
3429 or (bfd_vma) -1 if it should not be included. */
3430
3431static bfd_vma
3432elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
3433 const arelent *rel ATTRIBUTE_UNUSED)
3434{
3435 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3436}
8df9fc9d 3437
d2b2c203
DJ
3438/* Handle an x86-64 specific section when reading an object file. This
3439 is called when elfcode.h finds a section with an unknown type. */
3440
3441static bfd_boolean
6dc132d9
L
3442elf64_x86_64_section_from_shdr (bfd *abfd,
3443 Elf_Internal_Shdr *hdr,
3444 const char *name,
3445 int shindex)
d2b2c203
DJ
3446{
3447 if (hdr->sh_type != SHT_X86_64_UNWIND)
3448 return FALSE;
3449
6dc132d9 3450 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
3451 return FALSE;
3452
3453 return TRUE;
3454}
3455
3b22753a
L
3456/* Hook called by the linker routine which adds symbols from an object
3457 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3458 of .bss. */
3459
3460static bfd_boolean
3461elf64_x86_64_add_symbol_hook (bfd *abfd,
3462 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3463 Elf_Internal_Sym *sym,
3464 const char **namep ATTRIBUTE_UNUSED,
3465 flagword *flagsp ATTRIBUTE_UNUSED,
3466 asection **secp, bfd_vma *valp)
3467{
3468 asection *lcomm;
3469
3470 switch (sym->st_shndx)
3471 {
3472 case SHN_X86_64_LCOMMON:
3473 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
3474 if (lcomm == NULL)
3475 {
3476 lcomm = bfd_make_section_with_flags (abfd,
3477 "LARGE_COMMON",
3478 (SEC_ALLOC
3479 | SEC_IS_COMMON
3480 | SEC_LINKER_CREATED));
3481 if (lcomm == NULL)
3482 return FALSE;
3483 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
3484 }
3485 *secp = lcomm;
3486 *valp = sym->st_size;
3487 break;
3488 }
3489 return TRUE;
3490}
3491
3492
3493/* Given a BFD section, try to locate the corresponding ELF section
3494 index. */
3495
3496static bfd_boolean
3497elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
3498 asection *sec, int *index)
3499{
3500 if (sec == &_bfd_elf_large_com_section)
3501 {
3502 *index = SHN_X86_64_LCOMMON;
3503 return TRUE;
3504 }
3505 return FALSE;
3506}
3507
3508/* Process a symbol. */
3509
3510static void
3511elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
3512 asymbol *asym)
3513{
3514 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
3515
3516 switch (elfsym->internal_elf_sym.st_shndx)
3517 {
3518 case SHN_X86_64_LCOMMON:
3519 asym->section = &_bfd_elf_large_com_section;
3520 asym->value = elfsym->internal_elf_sym.st_size;
3521 /* Common symbol doesn't set BSF_GLOBAL. */
3522 asym->flags &= ~BSF_GLOBAL;
3523 break;
3524 }
3525}
3526
3527static bfd_boolean
3528elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
3529{
3530 return (sym->st_shndx == SHN_COMMON
3531 || sym->st_shndx == SHN_X86_64_LCOMMON);
3532}
3533
3534static unsigned int
3535elf64_x86_64_common_section_index (asection *sec)
3536{
3537 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3538 return SHN_COMMON;
3539 else
3540 return SHN_X86_64_LCOMMON;
3541}
3542
3543static asection *
3544elf64_x86_64_common_section (asection *sec)
3545{
3546 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3547 return bfd_com_section_ptr;
3548 else
3549 return &_bfd_elf_large_com_section;
3550}
3551
3552static bfd_boolean
3553elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3554 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
3555 struct elf_link_hash_entry *h,
3556 Elf_Internal_Sym *sym,
00492999 3557 asection **psec,
3b22753a
L
3558 bfd_vma *pvalue ATTRIBUTE_UNUSED,
3559 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
3560 bfd_boolean *skip ATTRIBUTE_UNUSED,
3561 bfd_boolean *override ATTRIBUTE_UNUSED,
3562 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
3563 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
3564 bfd_boolean *newdef ATTRIBUTE_UNUSED,
3565 bfd_boolean *newdyn,
3566 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
3567 bfd_boolean *newweak ATTRIBUTE_UNUSED,
3568 bfd *abfd ATTRIBUTE_UNUSED,
3569 asection **sec,
3570 bfd_boolean *olddef ATTRIBUTE_UNUSED,
3571 bfd_boolean *olddyn,
3572 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
3573 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
00492999 3574 bfd *oldbfd,
3b22753a
L
3575 asection **oldsec)
3576{
3577 /* A normal common symbol and a large common symbol result in a
00492999
L
3578 normal common symbol. We turn the large common symbol into a
3579 normal one. */
3b22753a
L
3580 if (!*olddyn
3581 && h->root.type == bfd_link_hash_common
3582 && !*newdyn
3583 && bfd_is_com_section (*sec)
00492999 3584 && *oldsec != *sec)
3b22753a 3585 {
00492999
L
3586 if (sym->st_shndx == SHN_COMMON
3587 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
3588 {
3589 h->root.u.c.p->section
3590 = bfd_make_section_old_way (oldbfd, "COMMON");
3591 h->root.u.c.p->section->flags = SEC_ALLOC;
3592 }
3593 else if (sym->st_shndx == SHN_X86_64_LCOMMON
3594 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
3595 *psec = *sec = bfd_com_section_ptr;
3b22753a
L
3596 }
3597
3598 return TRUE;
3599}
3600
3601static int
a6b96beb
AM
3602elf64_x86_64_additional_program_headers (bfd *abfd,
3603 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3b22753a
L
3604{
3605 asection *s;
3606 int count = 0;
3607
3608 /* Check to see if we need a large readonly segment. */
3609 s = bfd_get_section_by_name (abfd, ".lrodata");
3610 if (s && (s->flags & SEC_LOAD))
3611 count++;
3612
3613 /* Check to see if we need a large data segment. Since .lbss sections
3614 is placed right after the .bss section, there should be no need for
3615 a large data segment just because of .lbss. */
3616 s = bfd_get_section_by_name (abfd, ".ldata");
3617 if (s && (s->flags & SEC_LOAD))
3618 count++;
3619
3620 return count;
3621}
3622
fdc90cb4
JJ
3623/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3624
3625static bfd_boolean
3626elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
3627{
3628 if (h->plt.offset != (bfd_vma) -1
3629 && !h->def_regular
3630 && !h->pointer_equality_needed)
3631 return FALSE;
3632
3633 return _bfd_elf_hash_symbol (h);
3634}
3635
3b22753a
L
3636static const struct bfd_elf_special_section
3637 elf64_x86_64_special_sections[]=
3638{
3639 { ".gnu.linkonce.lb", 16, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3640 { ".gnu.linkonce.lr", 16, -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3641 { ".gnu.linkonce.lt", 16, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
3642 { ".lbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3643 { ".ldata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3644 { ".lrodata", 8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3645 { NULL, 0, 0, 0, 0 }
3646};
3647
70256ad8
AJ
3648#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3649#define TARGET_LITTLE_NAME "elf64-x86-64"
3650#define ELF_ARCH bfd_arch_i386
3651#define ELF_MACHINE_CODE EM_X86_64
f7661549 3652#define ELF_MAXPAGESIZE 0x200000
2043964e 3653#define ELF_MINPAGESIZE 0x1000
24718e3b 3654#define ELF_COMMONPAGESIZE 0x1000
70256ad8
AJ
3655
3656#define elf_backend_can_gc_sections 1
51b64d56 3657#define elf_backend_can_refcount 1
70256ad8
AJ
3658#define elf_backend_want_got_plt 1
3659#define elf_backend_plt_readonly 1
3660#define elf_backend_want_plt_sym 0
3661#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 3662#define elf_backend_rela_normal 1
70256ad8
AJ
3663
3664#define elf_info_to_howto elf64_x86_64_info_to_howto
70256ad8 3665
70256ad8
AJ
3666#define bfd_elf64_bfd_link_hash_table_create \
3667 elf64_x86_64_link_hash_table_create
407443a3 3668#define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
70256ad8
AJ
3669
3670#define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3671#define elf_backend_check_relocs elf64_x86_64_check_relocs
c434dee6
AJ
3672#define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3673#define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3674#define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
70256ad8
AJ
3675#define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3676#define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3677#define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3bab7989
ML
3678#define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3679#define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
c434dee6 3680#define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
70256ad8
AJ
3681#define elf_backend_relocate_section elf64_x86_64_relocate_section
3682#define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
67a4f2b7 3683#define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4c45e5c9 3684#define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
407443a3 3685#define elf_backend_object_p elf64_x86_64_elf_object_p
bffbf940 3686#define bfd_elf64_mkobject elf64_x86_64_mkobject
8d88c4ca 3687
d2b2c203
DJ
3688#define elf_backend_section_from_shdr \
3689 elf64_x86_64_section_from_shdr
3690
3b22753a
L
3691#define elf_backend_section_from_bfd_section \
3692 elf64_x86_64_elf_section_from_bfd_section
3693#define elf_backend_add_symbol_hook \
3694 elf64_x86_64_add_symbol_hook
3695#define elf_backend_symbol_processing \
3696 elf64_x86_64_symbol_processing
3697#define elf_backend_common_section_index \
3698 elf64_x86_64_common_section_index
3699#define elf_backend_common_section \
3700 elf64_x86_64_common_section
3701#define elf_backend_common_definition \
3702 elf64_x86_64_common_definition
3703#define elf_backend_merge_symbol \
3704 elf64_x86_64_merge_symbol
3705#define elf_backend_special_sections \
3706 elf64_x86_64_special_sections
3707#define elf_backend_additional_program_headers \
3708 elf64_x86_64_additional_program_headers
fdc90cb4
JJ
3709#define elf_backend_hash_symbol \
3710 elf64_x86_64_hash_symbol
3b22753a 3711
8d88c4ca 3712#include "elf64-target.h"
9d7cbccd
NC
3713
3714/* FreeBSD support. */
3715
3716#undef TARGET_LITTLE_SYM
3717#define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
3718#undef TARGET_LITTLE_NAME
3719#define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
3720
3721/* The kernel recognizes executables as valid only if they carry a
3722 "FreeBSD" label in the ELF header. So we put this label on all
3723 executables and (for simplicity) also all other object files. */
3724
3725static void
3726elf64_x86_64_fbsd_post_process_headers (bfd * abfd,
3727 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
3728{
3729 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
3730
3731 i_ehdrp = elf_elfheader (abfd);
3732
3733 /* Put an ABI label supported by FreeBSD >= 4.1. */
3734 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
3735}
3736
3737#undef elf_backend_post_process_headers
3738#define elf_backend_post_process_headers elf64_x86_64_fbsd_post_process_headers
3739
3740#undef elf64_bed
3741#define elf64_bed elf64_x86_64_fbsd_bed
3742
3743#include "elf64-target.h"
This page took 0.596373 seconds and 4 git commands to generate.