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