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