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