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