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