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