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