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