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