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