1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>.
6 This file is part of BFD, the Binary File Descriptor library.
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 2 of the License, or
11 (at your option) any later version.
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.
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, MA 02110-1301, USA. */
28 #include "elf/x86-64.h"
30 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
31 #define MINUS_ONE (~ (bfd_vma) 0)
33 /* The relocation "howto" table. Order of fields:
34 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
35 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
36 static reloc_howto_type x86_64_elf_howto_table
[] =
38 HOWTO(R_X86_64_NONE
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
39 bfd_elf_generic_reloc
, "R_X86_64_NONE", FALSE
, 0x00000000, 0x00000000,
41 HOWTO(R_X86_64_64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
42 bfd_elf_generic_reloc
, "R_X86_64_64", FALSE
, MINUS_ONE
, MINUS_ONE
,
44 HOWTO(R_X86_64_PC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
45 bfd_elf_generic_reloc
, "R_X86_64_PC32", FALSE
, 0xffffffff, 0xffffffff,
47 HOWTO(R_X86_64_GOT32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
48 bfd_elf_generic_reloc
, "R_X86_64_GOT32", FALSE
, 0xffffffff, 0xffffffff,
50 HOWTO(R_X86_64_PLT32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
51 bfd_elf_generic_reloc
, "R_X86_64_PLT32", FALSE
, 0xffffffff, 0xffffffff,
53 HOWTO(R_X86_64_COPY
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
54 bfd_elf_generic_reloc
, "R_X86_64_COPY", FALSE
, 0xffffffff, 0xffffffff,
56 HOWTO(R_X86_64_GLOB_DAT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
57 bfd_elf_generic_reloc
, "R_X86_64_GLOB_DAT", FALSE
, MINUS_ONE
,
59 HOWTO(R_X86_64_JUMP_SLOT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
60 bfd_elf_generic_reloc
, "R_X86_64_JUMP_SLOT", FALSE
, MINUS_ONE
,
62 HOWTO(R_X86_64_RELATIVE
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
63 bfd_elf_generic_reloc
, "R_X86_64_RELATIVE", FALSE
, MINUS_ONE
,
65 HOWTO(R_X86_64_GOTPCREL
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
66 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL", FALSE
, 0xffffffff,
68 HOWTO(R_X86_64_32
, 0, 2, 32, FALSE
, 0, complain_overflow_unsigned
,
69 bfd_elf_generic_reloc
, "R_X86_64_32", FALSE
, 0xffffffff, 0xffffffff,
71 HOWTO(R_X86_64_32S
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
72 bfd_elf_generic_reloc
, "R_X86_64_32S", FALSE
, 0xffffffff, 0xffffffff,
74 HOWTO(R_X86_64_16
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
,
75 bfd_elf_generic_reloc
, "R_X86_64_16", FALSE
, 0xffff, 0xffff, FALSE
),
76 HOWTO(R_X86_64_PC16
,0, 1, 16, TRUE
, 0, complain_overflow_bitfield
,
77 bfd_elf_generic_reloc
, "R_X86_64_PC16", FALSE
, 0xffff, 0xffff, TRUE
),
78 HOWTO(R_X86_64_8
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
,
79 bfd_elf_generic_reloc
, "R_X86_64_8", FALSE
, 0xff, 0xff, FALSE
),
80 HOWTO(R_X86_64_PC8
, 0, 0, 8, TRUE
, 0, complain_overflow_signed
,
81 bfd_elf_generic_reloc
, "R_X86_64_PC8", FALSE
, 0xff, 0xff, TRUE
),
82 HOWTO(R_X86_64_DTPMOD64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
83 bfd_elf_generic_reloc
, "R_X86_64_DTPMOD64", FALSE
, MINUS_ONE
,
85 HOWTO(R_X86_64_DTPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
86 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF64", FALSE
, MINUS_ONE
,
88 HOWTO(R_X86_64_TPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
89 bfd_elf_generic_reloc
, "R_X86_64_TPOFF64", FALSE
, MINUS_ONE
,
91 HOWTO(R_X86_64_TLSGD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
92 bfd_elf_generic_reloc
, "R_X86_64_TLSGD", FALSE
, 0xffffffff,
94 HOWTO(R_X86_64_TLSLD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
95 bfd_elf_generic_reloc
, "R_X86_64_TLSLD", FALSE
, 0xffffffff,
97 HOWTO(R_X86_64_DTPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
98 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF32", FALSE
, 0xffffffff,
100 HOWTO(R_X86_64_GOTTPOFF
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
101 bfd_elf_generic_reloc
, "R_X86_64_GOTTPOFF", FALSE
, 0xffffffff,
103 HOWTO(R_X86_64_TPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
104 bfd_elf_generic_reloc
, "R_X86_64_TPOFF32", FALSE
, 0xffffffff,
106 HOWTO(R_X86_64_PC64
, 0, 4, 64, TRUE
, 0, complain_overflow_bitfield
,
107 bfd_elf_generic_reloc
, "R_X86_64_PC64", FALSE
, MINUS_ONE
, MINUS_ONE
,
109 HOWTO(R_X86_64_GOTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
110 bfd_elf_generic_reloc
, "R_X86_64_GOTOFF64",
111 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
112 HOWTO(R_X86_64_GOTPC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
113 bfd_elf_generic_reloc
, "R_X86_64_GOTPC32",
114 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
115 HOWTO(R_X86_64_GOT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
116 bfd_elf_generic_reloc
, "R_X86_64_GOT64", FALSE
, MINUS_ONE
, MINUS_ONE
,
118 HOWTO(R_X86_64_GOTPCREL64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
119 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL64", FALSE
, MINUS_ONE
,
121 HOWTO(R_X86_64_GOTPC64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
122 bfd_elf_generic_reloc
, "R_X86_64_GOTPC64",
123 FALSE
, MINUS_ONE
, MINUS_ONE
, TRUE
),
124 HOWTO(R_X86_64_GOTPLT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
125 bfd_elf_generic_reloc
, "R_X86_64_GOTPLT64", FALSE
, MINUS_ONE
,
127 HOWTO(R_X86_64_PLTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
128 bfd_elf_generic_reloc
, "R_X86_64_PLTOFF64", FALSE
, MINUS_ONE
,
132 HOWTO(R_X86_64_GOTPC32_TLSDESC
, 0, 2, 32, TRUE
, 0,
133 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
134 "R_X86_64_GOTPC32_TLSDESC",
135 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
136 HOWTO(R_X86_64_TLSDESC_CALL
, 0, 0, 0, FALSE
, 0,
137 complain_overflow_dont
, bfd_elf_generic_reloc
,
138 "R_X86_64_TLSDESC_CALL",
140 HOWTO(R_X86_64_TLSDESC
, 0, 4, 64, FALSE
, 0,
141 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
143 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
145 /* We have a gap in the reloc numbers here.
146 R_X86_64_standard counts the number up to this point, and
147 R_X86_64_vt_offset is the value to subtract from a reloc type of
148 R_X86_64_GNU_VT* to form an index into this table. */
149 #define R_X86_64_standard (R_X86_64_TLSDESC + 1)
150 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
152 /* GNU extension to record C++ vtable hierarchy. */
153 HOWTO (R_X86_64_GNU_VTINHERIT
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
154 NULL
, "R_X86_64_GNU_VTINHERIT", FALSE
, 0, 0, FALSE
),
156 /* GNU extension to record C++ vtable member usage. */
157 HOWTO (R_X86_64_GNU_VTENTRY
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
158 _bfd_elf_rel_vtable_reloc_fn
, "R_X86_64_GNU_VTENTRY", FALSE
, 0, 0,
162 /* Map BFD relocs to the x86_64 elf relocs. */
165 bfd_reloc_code_real_type bfd_reloc_val
;
166 unsigned char elf_reloc_val
;
169 static const struct elf_reloc_map x86_64_reloc_map
[] =
171 { BFD_RELOC_NONE
, R_X86_64_NONE
, },
172 { BFD_RELOC_64
, R_X86_64_64
, },
173 { BFD_RELOC_32_PCREL
, R_X86_64_PC32
, },
174 { BFD_RELOC_X86_64_GOT32
, R_X86_64_GOT32
,},
175 { BFD_RELOC_X86_64_PLT32
, R_X86_64_PLT32
,},
176 { BFD_RELOC_X86_64_COPY
, R_X86_64_COPY
, },
177 { BFD_RELOC_X86_64_GLOB_DAT
, R_X86_64_GLOB_DAT
, },
178 { BFD_RELOC_X86_64_JUMP_SLOT
, R_X86_64_JUMP_SLOT
, },
179 { BFD_RELOC_X86_64_RELATIVE
, R_X86_64_RELATIVE
, },
180 { BFD_RELOC_X86_64_GOTPCREL
, R_X86_64_GOTPCREL
, },
181 { BFD_RELOC_32
, R_X86_64_32
, },
182 { BFD_RELOC_X86_64_32S
, R_X86_64_32S
, },
183 { BFD_RELOC_16
, R_X86_64_16
, },
184 { BFD_RELOC_16_PCREL
, R_X86_64_PC16
, },
185 { BFD_RELOC_8
, R_X86_64_8
, },
186 { BFD_RELOC_8_PCREL
, R_X86_64_PC8
, },
187 { BFD_RELOC_X86_64_DTPMOD64
, R_X86_64_DTPMOD64
, },
188 { BFD_RELOC_X86_64_DTPOFF64
, R_X86_64_DTPOFF64
, },
189 { BFD_RELOC_X86_64_TPOFF64
, R_X86_64_TPOFF64
, },
190 { BFD_RELOC_X86_64_TLSGD
, R_X86_64_TLSGD
, },
191 { BFD_RELOC_X86_64_TLSLD
, R_X86_64_TLSLD
, },
192 { BFD_RELOC_X86_64_DTPOFF32
, R_X86_64_DTPOFF32
, },
193 { BFD_RELOC_X86_64_GOTTPOFF
, R_X86_64_GOTTPOFF
, },
194 { BFD_RELOC_X86_64_TPOFF32
, R_X86_64_TPOFF32
, },
195 { BFD_RELOC_64_PCREL
, R_X86_64_PC64
, },
196 { BFD_RELOC_X86_64_GOTOFF64
, R_X86_64_GOTOFF64
, },
197 { BFD_RELOC_X86_64_GOTPC32
, R_X86_64_GOTPC32
, },
198 { BFD_RELOC_X86_64_GOT64
, R_X86_64_GOT64
, },
199 { BFD_RELOC_X86_64_GOTPCREL64
,R_X86_64_GOTPCREL64
, },
200 { BFD_RELOC_X86_64_GOTPC64
, R_X86_64_GOTPC64
, },
201 { BFD_RELOC_X86_64_GOTPLT64
, R_X86_64_GOTPLT64
, },
202 { BFD_RELOC_X86_64_PLTOFF64
, R_X86_64_PLTOFF64
, },
203 { BFD_RELOC_X86_64_GOTPC32_TLSDESC
, R_X86_64_GOTPC32_TLSDESC
, },
204 { BFD_RELOC_X86_64_TLSDESC_CALL
, R_X86_64_TLSDESC_CALL
, },
205 { BFD_RELOC_X86_64_TLSDESC
, R_X86_64_TLSDESC
, },
206 { BFD_RELOC_VTABLE_INHERIT
, R_X86_64_GNU_VTINHERIT
, },
207 { BFD_RELOC_VTABLE_ENTRY
, R_X86_64_GNU_VTENTRY
, },
210 static reloc_howto_type
*
211 elf64_x86_64_rtype_to_howto (bfd
*abfd
, unsigned r_type
)
215 if (r_type
< (unsigned int) R_X86_64_GNU_VTINHERIT
216 || r_type
>= (unsigned int) R_X86_64_max
)
218 if (r_type
>= (unsigned int) R_X86_64_standard
)
220 (*_bfd_error_handler
) (_("%B: invalid relocation type %d"),
222 r_type
= R_X86_64_NONE
;
227 i
= r_type
- (unsigned int) R_X86_64_vt_offset
;
228 BFD_ASSERT (x86_64_elf_howto_table
[i
].type
== r_type
);
229 return &x86_64_elf_howto_table
[i
];
232 /* Given a BFD reloc type, return a HOWTO structure. */
233 static reloc_howto_type
*
234 elf64_x86_64_reloc_type_lookup (bfd
*abfd
,
235 bfd_reloc_code_real_type code
)
239 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
242 if (x86_64_reloc_map
[i
].bfd_reloc_val
== code
)
243 return elf64_x86_64_rtype_to_howto (abfd
,
244 x86_64_reloc_map
[i
].elf_reloc_val
);
249 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
252 elf64_x86_64_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
253 Elf_Internal_Rela
*dst
)
257 r_type
= ELF64_R_TYPE (dst
->r_info
);
258 cache_ptr
->howto
= elf64_x86_64_rtype_to_howto (abfd
, r_type
);
259 BFD_ASSERT (r_type
== cache_ptr
->howto
->type
);
262 /* Support for core dump NOTE sections. */
264 elf64_x86_64_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
269 switch (note
->descsz
)
274 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
276 elf_tdata (abfd
)->core_signal
277 = bfd_get_16 (abfd
, note
->descdata
+ 12);
280 elf_tdata (abfd
)->core_pid
281 = bfd_get_32 (abfd
, note
->descdata
+ 32);
290 /* Make a ".reg/999" section. */
291 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
292 size
, note
->descpos
+ offset
);
296 elf64_x86_64_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
298 switch (note
->descsz
)
303 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
304 elf_tdata (abfd
)->core_program
305 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 40, 16);
306 elf_tdata (abfd
)->core_command
307 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 56, 80);
310 /* Note that for some reason, a spurious space is tacked
311 onto the end of the args in some (at least one anyway)
312 implementations, so strip it off if it exists. */
315 char *command
= elf_tdata (abfd
)->core_command
;
316 int n
= strlen (command
);
318 if (0 < n
&& command
[n
- 1] == ' ')
319 command
[n
- 1] = '\0';
325 /* Functions for the x86-64 ELF linker. */
327 /* The name of the dynamic interpreter. This is put in the .interp
330 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
332 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
333 copying dynamic variables from a shared lib into an app's dynbss
334 section, and instead use a dynamic relocation to point into the
336 #define ELIMINATE_COPY_RELOCS 1
338 /* The size in bytes of an entry in the global offset table. */
340 #define GOT_ENTRY_SIZE 8
342 /* The size in bytes of an entry in the procedure linkage table. */
344 #define PLT_ENTRY_SIZE 16
346 /* The first entry in a procedure linkage table looks like this. See the
347 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
349 static const bfd_byte elf64_x86_64_plt0_entry
[PLT_ENTRY_SIZE
] =
351 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
352 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
353 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
356 /* Subsequent entries in a procedure linkage table look like this. */
358 static const bfd_byte elf64_x86_64_plt_entry
[PLT_ENTRY_SIZE
] =
360 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
361 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
362 0x68, /* pushq immediate */
363 0, 0, 0, 0, /* replaced with index into relocation table. */
364 0xe9, /* jmp relative */
365 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
368 /* The x86-64 linker needs to keep track of the number of relocs that
369 it decides to copy as dynamic relocs in check_relocs for each symbol.
370 This is so that it can later discard them if they are found to be
371 unnecessary. We store the information in a field extending the
372 regular ELF linker hash table. */
374 struct elf64_x86_64_dyn_relocs
377 struct elf64_x86_64_dyn_relocs
*next
;
379 /* The input section of the reloc. */
382 /* Total number of relocs copied for the input section. */
385 /* Number of pc-relative relocs copied for the input section. */
386 bfd_size_type pc_count
;
389 /* x86-64 ELF linker hash entry. */
391 struct elf64_x86_64_link_hash_entry
393 struct elf_link_hash_entry elf
;
395 /* Track dynamic relocs copied for this symbol. */
396 struct elf64_x86_64_dyn_relocs
*dyn_relocs
;
398 #define GOT_UNKNOWN 0
402 #define GOT_TLS_GDESC 4
403 #define GOT_TLS_GD_BOTH_P(type) \
404 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
405 #define GOT_TLS_GD_P(type) \
406 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
407 #define GOT_TLS_GDESC_P(type) \
408 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
409 #define GOT_TLS_GD_ANY_P(type) \
410 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
411 unsigned char tls_type
;
413 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
414 starting at the end of the jump table. */
418 #define elf64_x86_64_hash_entry(ent) \
419 ((struct elf64_x86_64_link_hash_entry *)(ent))
421 struct elf64_x86_64_obj_tdata
423 struct elf_obj_tdata root
;
425 /* tls_type for each local got entry. */
426 char *local_got_tls_type
;
428 /* GOTPLT entries for TLS descriptors. */
429 bfd_vma
*local_tlsdesc_gotent
;
432 #define elf64_x86_64_tdata(abfd) \
433 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
435 #define elf64_x86_64_local_got_tls_type(abfd) \
436 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
438 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
439 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
441 /* x86-64 ELF linker hash table. */
443 struct elf64_x86_64_link_hash_table
445 struct elf_link_hash_table elf
;
447 /* Short-cuts to get to dynamic linker sections. */
456 /* The offset into splt of the PLT entry for the TLS descriptor
457 resolver. Special values are 0, if not necessary (or not found
458 to be necessary yet), and -1 if needed but not determined
461 /* The offset into sgot of the GOT entry used by the PLT entry
466 bfd_signed_vma refcount
;
470 /* The amount of space used by the jump slots in the GOT. */
471 bfd_vma sgotplt_jump_table_size
;
473 /* Small local sym to section mapping cache. */
474 struct sym_sec_cache sym_sec
;
477 /* Get the x86-64 ELF linker hash table from a link_info structure. */
479 #define elf64_x86_64_hash_table(p) \
480 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
482 #define elf64_x86_64_compute_jump_table_size(htab) \
483 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
485 /* Create an entry in an x86-64 ELF linker hash table. */
487 static struct bfd_hash_entry
*
488 link_hash_newfunc (struct bfd_hash_entry
*entry
, struct bfd_hash_table
*table
,
491 /* Allocate the structure if it has not already been allocated by a
495 entry
= bfd_hash_allocate (table
,
496 sizeof (struct elf64_x86_64_link_hash_entry
));
501 /* Call the allocation method of the superclass. */
502 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
505 struct elf64_x86_64_link_hash_entry
*eh
;
507 eh
= (struct elf64_x86_64_link_hash_entry
*) entry
;
508 eh
->dyn_relocs
= NULL
;
509 eh
->tls_type
= GOT_UNKNOWN
;
510 eh
->tlsdesc_got
= (bfd_vma
) -1;
516 /* Create an X86-64 ELF linker hash table. */
518 static struct bfd_link_hash_table
*
519 elf64_x86_64_link_hash_table_create (bfd
*abfd
)
521 struct elf64_x86_64_link_hash_table
*ret
;
522 bfd_size_type amt
= sizeof (struct elf64_x86_64_link_hash_table
);
524 ret
= (struct elf64_x86_64_link_hash_table
*) bfd_malloc (amt
);
528 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
529 sizeof (struct elf64_x86_64_link_hash_entry
)))
542 ret
->sym_sec
.abfd
= NULL
;
543 ret
->tlsdesc_plt
= 0;
544 ret
->tlsdesc_got
= 0;
545 ret
->tls_ld_got
.refcount
= 0;
546 ret
->sgotplt_jump_table_size
= 0;
548 return &ret
->elf
.root
;
551 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
552 shortcuts to them in our hash table. */
555 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
557 struct elf64_x86_64_link_hash_table
*htab
;
559 if (! _bfd_elf_create_got_section (dynobj
, info
))
562 htab
= elf64_x86_64_hash_table (info
);
563 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
564 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
565 if (!htab
->sgot
|| !htab
->sgotplt
)
568 htab
->srelgot
= bfd_make_section_with_flags (dynobj
, ".rela.got",
569 (SEC_ALLOC
| SEC_LOAD
574 if (htab
->srelgot
== NULL
575 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 3))
580 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
581 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
585 elf64_x86_64_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
587 struct elf64_x86_64_link_hash_table
*htab
;
589 htab
= elf64_x86_64_hash_table (info
);
590 if (!htab
->sgot
&& !create_got_section (dynobj
, info
))
593 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
596 htab
->splt
= bfd_get_section_by_name (dynobj
, ".plt");
597 htab
->srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
598 htab
->sdynbss
= bfd_get_section_by_name (dynobj
, ".dynbss");
600 htab
->srelbss
= bfd_get_section_by_name (dynobj
, ".rela.bss");
602 if (!htab
->splt
|| !htab
->srelplt
|| !htab
->sdynbss
603 || (!info
->shared
&& !htab
->srelbss
))
609 /* Copy the extra info we tack onto an elf_link_hash_entry. */
612 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info
*info
,
613 struct elf_link_hash_entry
*dir
,
614 struct elf_link_hash_entry
*ind
)
616 struct elf64_x86_64_link_hash_entry
*edir
, *eind
;
618 edir
= (struct elf64_x86_64_link_hash_entry
*) dir
;
619 eind
= (struct elf64_x86_64_link_hash_entry
*) ind
;
621 if (eind
->dyn_relocs
!= NULL
)
623 if (edir
->dyn_relocs
!= NULL
)
625 struct elf64_x86_64_dyn_relocs
**pp
;
626 struct elf64_x86_64_dyn_relocs
*p
;
628 /* Add reloc counts against the indirect sym to the direct sym
629 list. Merge any entries against the same section. */
630 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
632 struct elf64_x86_64_dyn_relocs
*q
;
634 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
635 if (q
->sec
== p
->sec
)
637 q
->pc_count
+= p
->pc_count
;
638 q
->count
+= p
->count
;
645 *pp
= edir
->dyn_relocs
;
648 edir
->dyn_relocs
= eind
->dyn_relocs
;
649 eind
->dyn_relocs
= NULL
;
652 if (ind
->root
.type
== bfd_link_hash_indirect
653 && dir
->got
.refcount
<= 0)
655 edir
->tls_type
= eind
->tls_type
;
656 eind
->tls_type
= GOT_UNKNOWN
;
659 if (ELIMINATE_COPY_RELOCS
660 && ind
->root
.type
!= bfd_link_hash_indirect
661 && dir
->dynamic_adjusted
)
663 /* If called to transfer flags for a weakdef during processing
664 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
665 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
666 dir
->ref_dynamic
|= ind
->ref_dynamic
;
667 dir
->ref_regular
|= ind
->ref_regular
;
668 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
669 dir
->needs_plt
|= ind
->needs_plt
;
670 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
673 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
677 elf64_x86_64_mkobject (bfd
*abfd
)
679 if (abfd
->tdata
.any
== NULL
)
681 bfd_size_type amt
= sizeof (struct elf64_x86_64_obj_tdata
);
682 abfd
->tdata
.any
= bfd_zalloc (abfd
, amt
);
683 if (abfd
->tdata
.any
== NULL
)
686 return bfd_elf_mkobject (abfd
);
690 elf64_x86_64_elf_object_p (bfd
*abfd
)
692 /* Set the right machine number for an x86-64 elf64 file. */
693 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x86_64
);
698 elf64_x86_64_tls_transition (struct bfd_link_info
*info
, int r_type
, int is_local
)
706 case R_X86_64_GOTPC32_TLSDESC
:
707 case R_X86_64_TLSDESC_CALL
:
708 case R_X86_64_GOTTPOFF
:
710 return R_X86_64_TPOFF32
;
711 return R_X86_64_GOTTPOFF
;
713 return R_X86_64_TPOFF32
;
719 /* Look through the relocs for a section during the first phase, and
720 calculate needed space in the global offset table, procedure
721 linkage table, and dynamic reloc sections. */
724 elf64_x86_64_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
725 const Elf_Internal_Rela
*relocs
)
727 struct elf64_x86_64_link_hash_table
*htab
;
728 Elf_Internal_Shdr
*symtab_hdr
;
729 struct elf_link_hash_entry
**sym_hashes
;
730 const Elf_Internal_Rela
*rel
;
731 const Elf_Internal_Rela
*rel_end
;
734 if (info
->relocatable
)
737 htab
= elf64_x86_64_hash_table (info
);
738 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
739 sym_hashes
= elf_sym_hashes (abfd
);
743 rel_end
= relocs
+ sec
->reloc_count
;
744 for (rel
= relocs
; rel
< rel_end
; rel
++)
747 unsigned long r_symndx
;
748 struct elf_link_hash_entry
*h
;
750 r_symndx
= ELF64_R_SYM (rel
->r_info
);
751 r_type
= ELF64_R_TYPE (rel
->r_info
);
753 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
755 (*_bfd_error_handler
) (_("%B: bad symbol index: %d"),
760 if (r_symndx
< symtab_hdr
->sh_info
)
764 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
765 while (h
->root
.type
== bfd_link_hash_indirect
766 || h
->root
.type
== bfd_link_hash_warning
)
767 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
770 r_type
= elf64_x86_64_tls_transition (info
, r_type
, h
== NULL
);
774 htab
->tls_ld_got
.refcount
+= 1;
777 case R_X86_64_TPOFF32
:
780 (*_bfd_error_handler
)
781 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
783 x86_64_elf_howto_table
[r_type
].name
,
784 (h
) ? h
->root
.root
.string
: "a local symbol");
785 bfd_set_error (bfd_error_bad_value
);
790 case R_X86_64_GOTTPOFF
:
792 info
->flags
|= DF_STATIC_TLS
;
796 case R_X86_64_GOTPCREL
:
799 case R_X86_64_GOTPCREL64
:
800 case R_X86_64_GOTPLT64
:
801 case R_X86_64_GOTPC32_TLSDESC
:
802 case R_X86_64_TLSDESC_CALL
:
803 /* This symbol requires a global offset table entry. */
805 int tls_type
, old_tls_type
;
809 default: tls_type
= GOT_NORMAL
; break;
810 case R_X86_64_TLSGD
: tls_type
= GOT_TLS_GD
; break;
811 case R_X86_64_GOTTPOFF
: tls_type
= GOT_TLS_IE
; break;
812 case R_X86_64_GOTPC32_TLSDESC
:
813 case R_X86_64_TLSDESC_CALL
:
814 tls_type
= GOT_TLS_GDESC
; break;
819 if (r_type
== R_X86_64_GOTPLT64
)
821 /* This relocation indicates that we also need
822 a PLT entry, as this is a function. We don't need
823 a PLT entry for local symbols. */
825 h
->plt
.refcount
+= 1;
827 h
->got
.refcount
+= 1;
828 old_tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
832 bfd_signed_vma
*local_got_refcounts
;
834 /* This is a global offset table entry for a local symbol. */
835 local_got_refcounts
= elf_local_got_refcounts (abfd
);
836 if (local_got_refcounts
== NULL
)
840 size
= symtab_hdr
->sh_info
;
841 size
*= sizeof (bfd_signed_vma
)
842 + sizeof (bfd_vma
) + sizeof (char);
843 local_got_refcounts
= ((bfd_signed_vma
*)
844 bfd_zalloc (abfd
, size
));
845 if (local_got_refcounts
== NULL
)
847 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
848 elf64_x86_64_local_tlsdesc_gotent (abfd
)
849 = (bfd_vma
*) (local_got_refcounts
+ symtab_hdr
->sh_info
);
850 elf64_x86_64_local_got_tls_type (abfd
)
851 = (char *) (local_got_refcounts
+ 2 * symtab_hdr
->sh_info
);
853 local_got_refcounts
[r_symndx
] += 1;
855 = elf64_x86_64_local_got_tls_type (abfd
) [r_symndx
];
858 /* If a TLS symbol is accessed using IE at least once,
859 there is no point to use dynamic model for it. */
860 if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
861 && (! GOT_TLS_GD_ANY_P (old_tls_type
)
862 || tls_type
!= GOT_TLS_IE
))
864 if (old_tls_type
== GOT_TLS_IE
&& GOT_TLS_GD_ANY_P (tls_type
))
865 tls_type
= old_tls_type
;
866 else if (GOT_TLS_GD_ANY_P (old_tls_type
)
867 && GOT_TLS_GD_ANY_P (tls_type
))
868 tls_type
|= old_tls_type
;
871 (*_bfd_error_handler
)
872 (_("%B: %s' accessed both as normal and thread local symbol"),
873 abfd
, h
? h
->root
.root
.string
: "<local>");
878 if (old_tls_type
!= tls_type
)
881 elf64_x86_64_hash_entry (h
)->tls_type
= tls_type
;
883 elf64_x86_64_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
888 case R_X86_64_GOTOFF64
:
889 case R_X86_64_GOTPC32
:
890 case R_X86_64_GOTPC64
:
892 if (htab
->sgot
== NULL
)
894 if (htab
->elf
.dynobj
== NULL
)
895 htab
->elf
.dynobj
= abfd
;
896 if (!create_got_section (htab
->elf
.dynobj
, info
))
902 /* This symbol requires a procedure linkage table entry. We
903 actually build the entry in adjust_dynamic_symbol,
904 because this might be a case of linking PIC code which is
905 never referenced by a dynamic object, in which case we
906 don't need to generate a procedure linkage table entry
909 /* If this is a local symbol, we resolve it directly without
910 creating a procedure linkage table entry. */
915 h
->plt
.refcount
+= 1;
918 case R_X86_64_PLTOFF64
:
919 /* This tries to form the 'address' of a function relative
920 to GOT. For global symbols we need a PLT entry. */
924 h
->plt
.refcount
+= 1;
932 /* Let's help debug shared library creation. These relocs
933 cannot be used in shared libs. Don't error out for
934 sections we don't care about, such as debug sections or
935 non-constant sections. */
937 && (sec
->flags
& SEC_ALLOC
) != 0
938 && (sec
->flags
& SEC_READONLY
) != 0)
940 (*_bfd_error_handler
)
941 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
943 x86_64_elf_howto_table
[r_type
].name
,
944 (h
) ? h
->root
.root
.string
: "a local symbol");
945 bfd_set_error (bfd_error_bad_value
);
955 if (h
!= NULL
&& !info
->shared
)
957 /* If this reloc is in a read-only section, we might
958 need a copy reloc. We can't check reliably at this
959 stage whether the section is read-only, as input
960 sections have not yet been mapped to output sections.
961 Tentatively set the flag for now, and correct in
962 adjust_dynamic_symbol. */
965 /* We may need a .plt entry if the function this reloc
966 refers to is in a shared lib. */
967 h
->plt
.refcount
+= 1;
968 if (r_type
!= R_X86_64_PC32
&& r_type
!= R_X86_64_PC64
)
969 h
->pointer_equality_needed
= 1;
972 /* If we are creating a shared library, and this is a reloc
973 against a global symbol, or a non PC relative reloc
974 against a local symbol, then we need to copy the reloc
975 into the shared library. However, if we are linking with
976 -Bsymbolic, we do not need to copy a reloc against a
977 global symbol which is defined in an object we are
978 including in the link (i.e., DEF_REGULAR is set). At
979 this point we have not seen all the input files, so it is
980 possible that DEF_REGULAR is not set now but will be set
981 later (it is never cleared). In case of a weak definition,
982 DEF_REGULAR may be cleared later by a strong definition in
983 a shared library. We account for that possibility below by
984 storing information in the relocs_copied field of the hash
985 table entry. A similar situation occurs when creating
986 shared libraries and symbol visibility changes render the
989 If on the other hand, we are creating an executable, we
990 may need to keep relocations for symbols satisfied by a
991 dynamic library if we manage to avoid copy relocs for the
994 && (sec
->flags
& SEC_ALLOC
) != 0
995 && (((r_type
!= R_X86_64_PC8
)
996 && (r_type
!= R_X86_64_PC16
)
997 && (r_type
!= R_X86_64_PC32
)
998 && (r_type
!= R_X86_64_PC64
))
1000 && (! SYMBOLIC_BIND (info
, h
)
1001 || h
->root
.type
== bfd_link_hash_defweak
1002 || !h
->def_regular
))))
1003 || (ELIMINATE_COPY_RELOCS
1005 && (sec
->flags
& SEC_ALLOC
) != 0
1007 && (h
->root
.type
== bfd_link_hash_defweak
1008 || !h
->def_regular
)))
1010 struct elf64_x86_64_dyn_relocs
*p
;
1011 struct elf64_x86_64_dyn_relocs
**head
;
1013 /* We must copy these reloc types into the output file.
1014 Create a reloc section in dynobj and make room for
1021 name
= (bfd_elf_string_from_elf_section
1023 elf_elfheader (abfd
)->e_shstrndx
,
1024 elf_section_data (sec
)->rel_hdr
.sh_name
));
1028 if (! CONST_STRNEQ (name
, ".rela")
1029 || strcmp (bfd_get_section_name (abfd
, sec
),
1032 (*_bfd_error_handler
)
1033 (_("%B: bad relocation section name `%s\'"),
1037 if (htab
->elf
.dynobj
== NULL
)
1038 htab
->elf
.dynobj
= abfd
;
1040 dynobj
= htab
->elf
.dynobj
;
1042 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1047 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
1048 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1049 if ((sec
->flags
& SEC_ALLOC
) != 0)
1050 flags
|= SEC_ALLOC
| SEC_LOAD
;
1051 sreloc
= bfd_make_section_with_flags (dynobj
,
1055 || ! bfd_set_section_alignment (dynobj
, sreloc
, 3))
1058 elf_section_data (sec
)->sreloc
= sreloc
;
1061 /* If this is a global symbol, we count the number of
1062 relocations we need for this symbol. */
1065 head
= &((struct elf64_x86_64_link_hash_entry
*) h
)->dyn_relocs
;
1070 /* Track dynamic relocs needed for local syms too.
1071 We really need local syms available to do this
1075 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
1080 /* Beware of type punned pointers vs strict aliasing
1082 vpp
= &(elf_section_data (s
)->local_dynrel
);
1083 head
= (struct elf64_x86_64_dyn_relocs
**)vpp
;
1087 if (p
== NULL
|| p
->sec
!= sec
)
1089 bfd_size_type amt
= sizeof *p
;
1090 p
= ((struct elf64_x86_64_dyn_relocs
*)
1091 bfd_alloc (htab
->elf
.dynobj
, amt
));
1102 if (r_type
== R_X86_64_PC8
1103 || r_type
== R_X86_64_PC16
1104 || r_type
== R_X86_64_PC32
1105 || r_type
== R_X86_64_PC64
)
1110 /* This relocation describes the C++ object vtable hierarchy.
1111 Reconstruct it for later use during GC. */
1112 case R_X86_64_GNU_VTINHERIT
:
1113 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
1117 /* This relocation describes which C++ vtable entries are actually
1118 used. Record for later use during GC. */
1119 case R_X86_64_GNU_VTENTRY
:
1120 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
1132 /* Return the section that should be marked against GC for a given
1136 elf64_x86_64_gc_mark_hook (asection
*sec
,
1137 struct bfd_link_info
*info
,
1138 Elf_Internal_Rela
*rel
,
1139 struct elf_link_hash_entry
*h
,
1140 Elf_Internal_Sym
*sym
)
1143 switch (ELF64_R_TYPE (rel
->r_info
))
1145 case R_X86_64_GNU_VTINHERIT
:
1146 case R_X86_64_GNU_VTENTRY
:
1150 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
1153 /* Update the got entry reference counts for the section being removed. */
1156 elf64_x86_64_gc_sweep_hook (bfd
*abfd
, struct bfd_link_info
*info
,
1157 asection
*sec
, const Elf_Internal_Rela
*relocs
)
1159 Elf_Internal_Shdr
*symtab_hdr
;
1160 struct elf_link_hash_entry
**sym_hashes
;
1161 bfd_signed_vma
*local_got_refcounts
;
1162 const Elf_Internal_Rela
*rel
, *relend
;
1164 elf_section_data (sec
)->local_dynrel
= NULL
;
1166 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1167 sym_hashes
= elf_sym_hashes (abfd
);
1168 local_got_refcounts
= elf_local_got_refcounts (abfd
);
1170 relend
= relocs
+ sec
->reloc_count
;
1171 for (rel
= relocs
; rel
< relend
; rel
++)
1173 unsigned long r_symndx
;
1174 unsigned int r_type
;
1175 struct elf_link_hash_entry
*h
= NULL
;
1177 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1178 if (r_symndx
>= symtab_hdr
->sh_info
)
1180 struct elf64_x86_64_link_hash_entry
*eh
;
1181 struct elf64_x86_64_dyn_relocs
**pp
;
1182 struct elf64_x86_64_dyn_relocs
*p
;
1184 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1185 while (h
->root
.type
== bfd_link_hash_indirect
1186 || h
->root
.type
== bfd_link_hash_warning
)
1187 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1188 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1190 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
1193 /* Everything must go for SEC. */
1199 r_type
= ELF64_R_TYPE (rel
->r_info
);
1200 r_type
= elf64_x86_64_tls_transition (info
, r_type
, h
!= NULL
);
1203 case R_X86_64_TLSLD
:
1204 if (elf64_x86_64_hash_table (info
)->tls_ld_got
.refcount
> 0)
1205 elf64_x86_64_hash_table (info
)->tls_ld_got
.refcount
-= 1;
1208 case R_X86_64_TLSGD
:
1209 case R_X86_64_GOTPC32_TLSDESC
:
1210 case R_X86_64_TLSDESC_CALL
:
1211 case R_X86_64_GOTTPOFF
:
1212 case R_X86_64_GOT32
:
1213 case R_X86_64_GOTPCREL
:
1214 case R_X86_64_GOT64
:
1215 case R_X86_64_GOTPCREL64
:
1216 case R_X86_64_GOTPLT64
:
1219 if (r_type
== R_X86_64_GOTPLT64
&& h
->plt
.refcount
> 0)
1220 h
->plt
.refcount
-= 1;
1221 if (h
->got
.refcount
> 0)
1222 h
->got
.refcount
-= 1;
1224 else if (local_got_refcounts
!= NULL
)
1226 if (local_got_refcounts
[r_symndx
] > 0)
1227 local_got_refcounts
[r_symndx
] -= 1;
1244 case R_X86_64_PLT32
:
1245 case R_X86_64_PLTOFF64
:
1248 if (h
->plt
.refcount
> 0)
1249 h
->plt
.refcount
-= 1;
1261 /* Adjust a symbol defined by a dynamic object and referenced by a
1262 regular object. The current definition is in some section of the
1263 dynamic object, but we're not including those sections. We have to
1264 change the definition to something the rest of the link can
1268 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info
*info
,
1269 struct elf_link_hash_entry
*h
)
1271 struct elf64_x86_64_link_hash_table
*htab
;
1273 unsigned int power_of_two
;
1275 /* If this is a function, put it in the procedure linkage table. We
1276 will fill in the contents of the procedure linkage table later,
1277 when we know the address of the .got section. */
1278 if (h
->type
== STT_FUNC
1281 if (h
->plt
.refcount
<= 0
1282 || SYMBOL_CALLS_LOCAL (info
, h
)
1283 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1284 && h
->root
.type
== bfd_link_hash_undefweak
))
1286 /* This case can occur if we saw a PLT32 reloc in an input
1287 file, but the symbol was never referred to by a dynamic
1288 object, or if all references were garbage collected. In
1289 such a case, we don't actually need to build a procedure
1290 linkage table, and we can just do a PC32 reloc instead. */
1291 h
->plt
.offset
= (bfd_vma
) -1;
1298 /* It's possible that we incorrectly decided a .plt reloc was
1299 needed for an R_X86_64_PC32 reloc to a non-function sym in
1300 check_relocs. We can't decide accurately between function and
1301 non-function syms in check-relocs; Objects loaded later in
1302 the link may change h->type. So fix it now. */
1303 h
->plt
.offset
= (bfd_vma
) -1;
1305 /* If this is a weak symbol, and there is a real definition, the
1306 processor independent code will have arranged for us to see the
1307 real definition first, and we can just use the same value. */
1308 if (h
->u
.weakdef
!= NULL
)
1310 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
1311 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
1312 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
1313 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
1314 if (ELIMINATE_COPY_RELOCS
|| info
->nocopyreloc
)
1315 h
->non_got_ref
= h
->u
.weakdef
->non_got_ref
;
1319 /* This is a reference to a symbol defined by a dynamic object which
1320 is not a function. */
1322 /* If we are creating a shared library, we must presume that the
1323 only references to the symbol are via the global offset table.
1324 For such cases we need not do anything here; the relocations will
1325 be handled correctly by relocate_section. */
1329 /* If there are no references to this symbol that do not use the
1330 GOT, we don't need to generate a copy reloc. */
1331 if (!h
->non_got_ref
)
1334 /* If -z nocopyreloc was given, we won't generate them either. */
1335 if (info
->nocopyreloc
)
1341 if (ELIMINATE_COPY_RELOCS
)
1343 struct elf64_x86_64_link_hash_entry
* eh
;
1344 struct elf64_x86_64_dyn_relocs
*p
;
1346 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1347 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1349 s
= p
->sec
->output_section
;
1350 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1354 /* If we didn't find any dynamic relocs in read-only sections, then
1355 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1365 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
1366 h
->root
.root
.string
);
1370 /* We must allocate the symbol in our .dynbss section, which will
1371 become part of the .bss section of the executable. There will be
1372 an entry for this symbol in the .dynsym section. The dynamic
1373 object will contain position independent code, so all references
1374 from the dynamic object to this symbol will go through the global
1375 offset table. The dynamic linker will use the .dynsym entry to
1376 determine the address it must put in the global offset table, so
1377 both the dynamic object and the regular object will refer to the
1378 same memory location for the variable. */
1380 htab
= elf64_x86_64_hash_table (info
);
1382 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1383 to copy the initial value out of the dynamic object and into the
1384 runtime process image. */
1385 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
1387 htab
->srelbss
->size
+= sizeof (Elf64_External_Rela
);
1391 /* We need to figure out the alignment required for this symbol. I
1392 have no idea how ELF linkers handle this. 16-bytes is the size
1393 of the largest type that requires hard alignment -- long double. */
1394 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1396 power_of_two
= bfd_log2 (h
->size
);
1397 if (power_of_two
> 4)
1400 /* Apply the required alignment. */
1402 s
->size
= BFD_ALIGN (s
->size
, (bfd_size_type
) (1 << power_of_two
));
1403 if (power_of_two
> bfd_get_section_alignment (htab
->elf
.dynobj
, s
))
1405 if (! bfd_set_section_alignment (htab
->elf
.dynobj
, s
, power_of_two
))
1409 /* Define the symbol as being at this point in the section. */
1410 h
->root
.u
.def
.section
= s
;
1411 h
->root
.u
.def
.value
= s
->size
;
1413 /* Increment the section size to make room for the symbol. */
1419 /* Allocate space in .plt, .got and associated reloc sections for
1423 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
1425 struct bfd_link_info
*info
;
1426 struct elf64_x86_64_link_hash_table
*htab
;
1427 struct elf64_x86_64_link_hash_entry
*eh
;
1428 struct elf64_x86_64_dyn_relocs
*p
;
1430 if (h
->root
.type
== bfd_link_hash_indirect
)
1433 if (h
->root
.type
== bfd_link_hash_warning
)
1434 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1436 info
= (struct bfd_link_info
*) inf
;
1437 htab
= elf64_x86_64_hash_table (info
);
1439 if (htab
->elf
.dynamic_sections_created
1440 && h
->plt
.refcount
> 0)
1442 /* Make sure this symbol is output as a dynamic symbol.
1443 Undefined weak syms won't yet be marked as dynamic. */
1444 if (h
->dynindx
== -1
1445 && !h
->forced_local
)
1447 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1452 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
1454 asection
*s
= htab
->splt
;
1456 /* If this is the first .plt entry, make room for the special
1459 s
->size
+= PLT_ENTRY_SIZE
;
1461 h
->plt
.offset
= s
->size
;
1463 /* If this symbol is not defined in a regular file, and we are
1464 not generating a shared library, then set the symbol to this
1465 location in the .plt. This is required to make function
1466 pointers compare as equal between the normal executable and
1467 the shared library. */
1471 h
->root
.u
.def
.section
= s
;
1472 h
->root
.u
.def
.value
= h
->plt
.offset
;
1475 /* Make room for this entry. */
1476 s
->size
+= PLT_ENTRY_SIZE
;
1478 /* We also need to make an entry in the .got.plt section, which
1479 will be placed in the .got section by the linker script. */
1480 htab
->sgotplt
->size
+= GOT_ENTRY_SIZE
;
1482 /* We also need to make an entry in the .rela.plt section. */
1483 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
1484 htab
->srelplt
->reloc_count
++;
1488 h
->plt
.offset
= (bfd_vma
) -1;
1494 h
->plt
.offset
= (bfd_vma
) -1;
1498 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1499 eh
->tlsdesc_got
= (bfd_vma
) -1;
1501 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1502 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1503 if (h
->got
.refcount
> 0
1506 && elf64_x86_64_hash_entry (h
)->tls_type
== GOT_TLS_IE
)
1507 h
->got
.offset
= (bfd_vma
) -1;
1508 else if (h
->got
.refcount
> 0)
1512 int tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
1514 /* Make sure this symbol is output as a dynamic symbol.
1515 Undefined weak syms won't yet be marked as dynamic. */
1516 if (h
->dynindx
== -1
1517 && !h
->forced_local
)
1519 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1523 if (GOT_TLS_GDESC_P (tls_type
))
1525 eh
->tlsdesc_got
= htab
->sgotplt
->size
1526 - elf64_x86_64_compute_jump_table_size (htab
);
1527 htab
->sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
1528 h
->got
.offset
= (bfd_vma
) -2;
1530 if (! GOT_TLS_GDESC_P (tls_type
)
1531 || GOT_TLS_GD_P (tls_type
))
1534 h
->got
.offset
= s
->size
;
1535 s
->size
+= GOT_ENTRY_SIZE
;
1536 if (GOT_TLS_GD_P (tls_type
))
1537 s
->size
+= GOT_ENTRY_SIZE
;
1539 dyn
= htab
->elf
.dynamic_sections_created
;
1540 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1542 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1543 if ((GOT_TLS_GD_P (tls_type
) && h
->dynindx
== -1)
1544 || tls_type
== GOT_TLS_IE
)
1545 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
1546 else if (GOT_TLS_GD_P (tls_type
))
1547 htab
->srelgot
->size
+= 2 * sizeof (Elf64_External_Rela
);
1548 else if (! GOT_TLS_GDESC_P (tls_type
)
1549 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
1550 || h
->root
.type
!= bfd_link_hash_undefweak
)
1552 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
1553 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
1554 if (GOT_TLS_GDESC_P (tls_type
))
1556 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
1557 htab
->tlsdesc_plt
= (bfd_vma
) -1;
1561 h
->got
.offset
= (bfd_vma
) -1;
1563 if (eh
->dyn_relocs
== NULL
)
1566 /* In the shared -Bsymbolic case, discard space allocated for
1567 dynamic pc-relative relocs against symbols which turn out to be
1568 defined in regular objects. For the normal shared case, discard
1569 space for pc-relative relocs that have become local due to symbol
1570 visibility changes. */
1574 /* Relocs that use pc_count are those that appear on a call
1575 insn, or certain REL relocs that can generated via assembly.
1576 We want calls to protected symbols to resolve directly to the
1577 function rather than going via the plt. If people want
1578 function pointer comparisons to work as expected then they
1579 should avoid writing weird assembly. */
1580 if (SYMBOL_CALLS_LOCAL (info
, h
))
1582 struct elf64_x86_64_dyn_relocs
**pp
;
1584 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
1586 p
->count
-= p
->pc_count
;
1595 /* Also discard relocs on undefined weak syms with non-default
1597 if (eh
->dyn_relocs
!= NULL
1598 && h
->root
.type
== bfd_link_hash_undefweak
)
1600 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
1601 eh
->dyn_relocs
= NULL
;
1603 /* Make sure undefined weak symbols are output as a dynamic
1605 else if (h
->dynindx
== -1
1606 && !h
->forced_local
)
1608 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1613 else if (ELIMINATE_COPY_RELOCS
)
1615 /* For the non-shared case, discard space for relocs against
1616 symbols which turn out to need copy relocs or are not
1622 || (htab
->elf
.dynamic_sections_created
1623 && (h
->root
.type
== bfd_link_hash_undefweak
1624 || h
->root
.type
== bfd_link_hash_undefined
))))
1626 /* Make sure this symbol is output as a dynamic symbol.
1627 Undefined weak syms won't yet be marked as dynamic. */
1628 if (h
->dynindx
== -1
1629 && !h
->forced_local
)
1631 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1635 /* If that succeeded, we know we'll be keeping all the
1637 if (h
->dynindx
!= -1)
1641 eh
->dyn_relocs
= NULL
;
1646 /* Finally, allocate space. */
1647 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1649 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1650 sreloc
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
1656 /* Find any dynamic relocs that apply to read-only sections. */
1659 readonly_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
1661 struct elf64_x86_64_link_hash_entry
*eh
;
1662 struct elf64_x86_64_dyn_relocs
*p
;
1664 if (h
->root
.type
== bfd_link_hash_warning
)
1665 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1667 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1668 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1670 asection
*s
= p
->sec
->output_section
;
1672 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1674 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
1676 info
->flags
|= DF_TEXTREL
;
1678 /* Not an error, just cut short the traversal. */
1685 /* Set the sizes of the dynamic sections. */
1688 elf64_x86_64_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1689 struct bfd_link_info
*info
)
1691 struct elf64_x86_64_link_hash_table
*htab
;
1697 htab
= elf64_x86_64_hash_table (info
);
1698 dynobj
= htab
->elf
.dynobj
;
1702 if (htab
->elf
.dynamic_sections_created
)
1704 /* Set the contents of the .interp section to the interpreter. */
1705 if (info
->executable
)
1707 s
= bfd_get_section_by_name (dynobj
, ".interp");
1710 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1711 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1715 /* Set up .got offsets for local syms, and space for local dynamic
1717 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
1719 bfd_signed_vma
*local_got
;
1720 bfd_signed_vma
*end_local_got
;
1721 char *local_tls_type
;
1722 bfd_vma
*local_tlsdesc_gotent
;
1723 bfd_size_type locsymcount
;
1724 Elf_Internal_Shdr
*symtab_hdr
;
1727 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
1730 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1732 struct elf64_x86_64_dyn_relocs
*p
;
1734 for (p
= (struct elf64_x86_64_dyn_relocs
*)
1735 (elf_section_data (s
)->local_dynrel
);
1739 if (!bfd_is_abs_section (p
->sec
)
1740 && bfd_is_abs_section (p
->sec
->output_section
))
1742 /* Input section has been discarded, either because
1743 it is a copy of a linkonce section or due to
1744 linker script /DISCARD/, so we'll be discarding
1747 else if (p
->count
!= 0)
1749 srel
= elf_section_data (p
->sec
)->sreloc
;
1750 srel
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
1751 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1752 info
->flags
|= DF_TEXTREL
;
1758 local_got
= elf_local_got_refcounts (ibfd
);
1762 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
1763 locsymcount
= symtab_hdr
->sh_info
;
1764 end_local_got
= local_got
+ locsymcount
;
1765 local_tls_type
= elf64_x86_64_local_got_tls_type (ibfd
);
1766 local_tlsdesc_gotent
= elf64_x86_64_local_tlsdesc_gotent (ibfd
);
1768 srel
= htab
->srelgot
;
1769 for (; local_got
< end_local_got
;
1770 ++local_got
, ++local_tls_type
, ++local_tlsdesc_gotent
)
1772 *local_tlsdesc_gotent
= (bfd_vma
) -1;
1775 if (GOT_TLS_GDESC_P (*local_tls_type
))
1777 *local_tlsdesc_gotent
= htab
->sgotplt
->size
1778 - elf64_x86_64_compute_jump_table_size (htab
);
1779 htab
->sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
1780 *local_got
= (bfd_vma
) -2;
1782 if (! GOT_TLS_GDESC_P (*local_tls_type
)
1783 || GOT_TLS_GD_P (*local_tls_type
))
1785 *local_got
= s
->size
;
1786 s
->size
+= GOT_ENTRY_SIZE
;
1787 if (GOT_TLS_GD_P (*local_tls_type
))
1788 s
->size
+= GOT_ENTRY_SIZE
;
1791 || GOT_TLS_GD_ANY_P (*local_tls_type
)
1792 || *local_tls_type
== GOT_TLS_IE
)
1794 if (GOT_TLS_GDESC_P (*local_tls_type
))
1796 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
1797 htab
->tlsdesc_plt
= (bfd_vma
) -1;
1799 if (! GOT_TLS_GDESC_P (*local_tls_type
)
1800 || GOT_TLS_GD_P (*local_tls_type
))
1801 srel
->size
+= sizeof (Elf64_External_Rela
);
1805 *local_got
= (bfd_vma
) -1;
1809 if (htab
->tls_ld_got
.refcount
> 0)
1811 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1813 htab
->tls_ld_got
.offset
= htab
->sgot
->size
;
1814 htab
->sgot
->size
+= 2 * GOT_ENTRY_SIZE
;
1815 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
1818 htab
->tls_ld_got
.offset
= -1;
1820 /* Allocate global sym .plt and .got entries, and space for global
1821 sym dynamic relocs. */
1822 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, (PTR
) info
);
1824 /* For every jump slot reserved in the sgotplt, reloc_count is
1825 incremented. However, when we reserve space for TLS descriptors,
1826 it's not incremented, so in order to compute the space reserved
1827 for them, it suffices to multiply the reloc count by the jump
1830 htab
->sgotplt_jump_table_size
1831 = elf64_x86_64_compute_jump_table_size (htab
);
1833 if (htab
->tlsdesc_plt
)
1835 /* If we're not using lazy TLS relocations, don't generate the
1836 PLT and GOT entries they require. */
1837 if ((info
->flags
& DF_BIND_NOW
))
1838 htab
->tlsdesc_plt
= 0;
1841 htab
->tlsdesc_got
= htab
->sgot
->size
;
1842 htab
->sgot
->size
+= GOT_ENTRY_SIZE
;
1843 /* Reserve room for the initial entry.
1844 FIXME: we could probably do away with it in this case. */
1845 if (htab
->splt
->size
== 0)
1846 htab
->splt
->size
+= PLT_ENTRY_SIZE
;
1847 htab
->tlsdesc_plt
= htab
->splt
->size
;
1848 htab
->splt
->size
+= PLT_ENTRY_SIZE
;
1852 /* We now have determined the sizes of the various dynamic sections.
1853 Allocate memory for them. */
1855 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1857 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1862 || s
== htab
->sgotplt
1863 || s
== htab
->sdynbss
)
1865 /* Strip this section if we don't need it; see the
1868 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
1870 if (s
->size
!= 0 && s
!= htab
->srelplt
)
1873 /* We use the reloc_count field as a counter if we need
1874 to copy relocs into the output file. */
1875 if (s
!= htab
->srelplt
)
1880 /* It's not one of our sections, so don't allocate space. */
1886 /* If we don't need this section, strip it from the
1887 output file. This is mostly to handle .rela.bss and
1888 .rela.plt. We must create both sections in
1889 create_dynamic_sections, because they must be created
1890 before the linker maps input sections to output
1891 sections. The linker does that before
1892 adjust_dynamic_symbol is called, and it is that
1893 function which decides whether anything needs to go
1894 into these sections. */
1896 s
->flags
|= SEC_EXCLUDE
;
1900 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1903 /* Allocate memory for the section contents. We use bfd_zalloc
1904 here in case unused entries are not reclaimed before the
1905 section's contents are written out. This should not happen,
1906 but this way if it does, we get a R_X86_64_NONE reloc instead
1908 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1909 if (s
->contents
== NULL
)
1913 if (htab
->elf
.dynamic_sections_created
)
1915 /* Add some entries to the .dynamic section. We fill in the
1916 values later, in elf64_x86_64_finish_dynamic_sections, but we
1917 must add the entries now so that we get the correct size for
1918 the .dynamic section. The DT_DEBUG entry is filled in by the
1919 dynamic linker and used by the debugger. */
1920 #define add_dynamic_entry(TAG, VAL) \
1921 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1923 if (info
->executable
)
1925 if (!add_dynamic_entry (DT_DEBUG
, 0))
1929 if (htab
->splt
->size
!= 0)
1931 if (!add_dynamic_entry (DT_PLTGOT
, 0)
1932 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
1933 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
1934 || !add_dynamic_entry (DT_JMPREL
, 0))
1937 if (htab
->tlsdesc_plt
1938 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
1939 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
1945 if (!add_dynamic_entry (DT_RELA
, 0)
1946 || !add_dynamic_entry (DT_RELASZ
, 0)
1947 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf64_External_Rela
)))
1950 /* If any dynamic relocs apply to a read-only section,
1951 then we need a DT_TEXTREL entry. */
1952 if ((info
->flags
& DF_TEXTREL
) == 0)
1953 elf_link_hash_traverse (&htab
->elf
, readonly_dynrelocs
,
1956 if ((info
->flags
& DF_TEXTREL
) != 0)
1958 if (!add_dynamic_entry (DT_TEXTREL
, 0))
1963 #undef add_dynamic_entry
1969 elf64_x86_64_always_size_sections (bfd
*output_bfd
,
1970 struct bfd_link_info
*info
)
1972 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
1976 struct elf_link_hash_entry
*tlsbase
;
1978 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
1979 "_TLS_MODULE_BASE_",
1980 FALSE
, FALSE
, FALSE
);
1982 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
1984 struct bfd_link_hash_entry
*bh
= NULL
;
1985 const struct elf_backend_data
*bed
1986 = get_elf_backend_data (output_bfd
);
1988 if (!(_bfd_generic_link_add_one_symbol
1989 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
1990 tls_sec
, 0, NULL
, FALSE
,
1991 bed
->collect
, &bh
)))
1993 tlsbase
= (struct elf_link_hash_entry
*)bh
;
1994 tlsbase
->def_regular
= 1;
1995 tlsbase
->other
= STV_HIDDEN
;
1996 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, TRUE
);
2003 /* Return the base VMA address which should be subtracted from real addresses
2004 when resolving @dtpoff relocation.
2005 This is PT_TLS segment p_vaddr. */
2008 dtpoff_base (struct bfd_link_info
*info
)
2010 /* If tls_sec is NULL, we should have signalled an error already. */
2011 if (elf_hash_table (info
)->tls_sec
== NULL
)
2013 return elf_hash_table (info
)->tls_sec
->vma
;
2016 /* Return the relocation value for @tpoff relocation
2017 if STT_TLS virtual address is ADDRESS. */
2020 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
2022 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2024 /* If tls_segment is NULL, we should have signalled an error already. */
2025 if (htab
->tls_sec
== NULL
)
2027 return address
- htab
->tls_size
- htab
->tls_sec
->vma
;
2030 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2034 is_32bit_relative_branch (bfd_byte
*contents
, bfd_vma offset
)
2036 /* Opcode Instruction
2039 0x0f 0x8x conditional jump */
2041 && (contents
[offset
- 1] == 0xe8
2042 || contents
[offset
- 1] == 0xe9))
2044 && contents
[offset
- 2] == 0x0f
2045 && (contents
[offset
- 1] & 0xf0) == 0x80));
2048 /* Relocate an x86_64 ELF section. */
2051 elf64_x86_64_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
2052 bfd
*input_bfd
, asection
*input_section
,
2053 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
2054 Elf_Internal_Sym
*local_syms
,
2055 asection
**local_sections
)
2057 struct elf64_x86_64_link_hash_table
*htab
;
2058 Elf_Internal_Shdr
*symtab_hdr
;
2059 struct elf_link_hash_entry
**sym_hashes
;
2060 bfd_vma
*local_got_offsets
;
2061 bfd_vma
*local_tlsdesc_gotents
;
2062 Elf_Internal_Rela
*rel
;
2063 Elf_Internal_Rela
*relend
;
2065 if (info
->relocatable
)
2068 htab
= elf64_x86_64_hash_table (info
);
2069 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2070 sym_hashes
= elf_sym_hashes (input_bfd
);
2071 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2072 local_tlsdesc_gotents
= elf64_x86_64_local_tlsdesc_gotent (input_bfd
);
2075 relend
= relocs
+ input_section
->reloc_count
;
2076 for (; rel
< relend
; rel
++)
2078 unsigned int r_type
;
2079 reloc_howto_type
*howto
;
2080 unsigned long r_symndx
;
2081 struct elf_link_hash_entry
*h
;
2082 Elf_Internal_Sym
*sym
;
2084 bfd_vma off
, offplt
;
2086 bfd_boolean unresolved_reloc
;
2087 bfd_reloc_status_type r
;
2090 r_type
= ELF64_R_TYPE (rel
->r_info
);
2091 if (r_type
== (int) R_X86_64_GNU_VTINHERIT
2092 || r_type
== (int) R_X86_64_GNU_VTENTRY
)
2095 if (r_type
>= R_X86_64_max
)
2097 bfd_set_error (bfd_error_bad_value
);
2101 howto
= x86_64_elf_howto_table
+ r_type
;
2102 r_symndx
= ELF64_R_SYM (rel
->r_info
);
2106 unresolved_reloc
= FALSE
;
2107 if (r_symndx
< symtab_hdr
->sh_info
)
2109 sym
= local_syms
+ r_symndx
;
2110 sec
= local_sections
[r_symndx
];
2112 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2118 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2119 r_symndx
, symtab_hdr
, sym_hashes
,
2121 unresolved_reloc
, warned
);
2123 /* When generating a shared object, the relocations handled here are
2124 copied into the output file to be resolved at run time. */
2128 case R_X86_64_GOT32
:
2129 case R_X86_64_GOT64
:
2130 /* Relocation is to the entry for this symbol in the global
2132 case R_X86_64_GOTPCREL
:
2133 case R_X86_64_GOTPCREL64
:
2134 /* Use global offset table entry as symbol value. */
2135 case R_X86_64_GOTPLT64
:
2136 /* This is the same as GOT64 for relocation purposes, but
2137 indicates the existence of a PLT entry. The difficulty is,
2138 that we must calculate the GOT slot offset from the PLT
2139 offset, if this symbol got a PLT entry (it was global).
2140 Additionally if it's computed from the PLT entry, then that
2141 GOT offset is relative to .got.plt, not to .got. */
2142 base_got
= htab
->sgot
;
2144 if (htab
->sgot
== NULL
)
2151 off
= h
->got
.offset
;
2153 && h
->plt
.offset
!= (bfd_vma
)-1
2154 && off
== (bfd_vma
)-1)
2156 /* We can't use h->got.offset here to save
2157 state, or even just remember the offset, as
2158 finish_dynamic_symbol would use that as offset into
2160 bfd_vma plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
2161 off
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
2162 base_got
= htab
->sgotplt
;
2165 dyn
= htab
->elf
.dynamic_sections_created
;
2167 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
2169 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2170 || (ELF_ST_VISIBILITY (h
->other
)
2171 && h
->root
.type
== bfd_link_hash_undefweak
))
2173 /* This is actually a static link, or it is a -Bsymbolic
2174 link and the symbol is defined locally, or the symbol
2175 was forced to be local because of a version file. We
2176 must initialize this entry in the global offset table.
2177 Since the offset must always be a multiple of 8, we
2178 use the least significant bit to record whether we
2179 have initialized it already.
2181 When doing a dynamic link, we create a .rela.got
2182 relocation entry to initialize the value. This is
2183 done in the finish_dynamic_symbol routine. */
2188 bfd_put_64 (output_bfd
, relocation
,
2189 base_got
->contents
+ off
);
2190 /* Note that this is harmless for the GOTPLT64 case,
2191 as -1 | 1 still is -1. */
2196 unresolved_reloc
= FALSE
;
2200 if (local_got_offsets
== NULL
)
2203 off
= local_got_offsets
[r_symndx
];
2205 /* The offset must always be a multiple of 8. We use
2206 the least significant bit to record whether we have
2207 already generated the necessary reloc. */
2212 bfd_put_64 (output_bfd
, relocation
,
2213 base_got
->contents
+ off
);
2218 Elf_Internal_Rela outrel
;
2221 /* We need to generate a R_X86_64_RELATIVE reloc
2222 for the dynamic linker. */
2227 outrel
.r_offset
= (base_got
->output_section
->vma
2228 + base_got
->output_offset
2230 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
2231 outrel
.r_addend
= relocation
;
2233 loc
+= s
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2234 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2237 local_got_offsets
[r_symndx
] |= 1;
2241 if (off
>= (bfd_vma
) -2)
2244 relocation
= base_got
->output_section
->vma
2245 + base_got
->output_offset
+ off
;
2246 if (r_type
!= R_X86_64_GOTPCREL
&& r_type
!= R_X86_64_GOTPCREL64
)
2247 relocation
-= htab
->sgotplt
->output_section
->vma
2248 - htab
->sgotplt
->output_offset
;
2252 case R_X86_64_GOTOFF64
:
2253 /* Relocation is relative to the start of the global offset
2256 /* Check to make sure it isn't a protected function symbol
2257 for shared library since it may not be local when used
2258 as function address. */
2262 && h
->type
== STT_FUNC
2263 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
2265 (*_bfd_error_handler
)
2266 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2267 input_bfd
, h
->root
.root
.string
);
2268 bfd_set_error (bfd_error_bad_value
);
2272 /* Note that sgot is not involved in this
2273 calculation. We always want the start of .got.plt. If we
2274 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2275 permitted by the ABI, we might have to change this
2277 relocation
-= htab
->sgotplt
->output_section
->vma
2278 + htab
->sgotplt
->output_offset
;
2281 case R_X86_64_GOTPC32
:
2282 case R_X86_64_GOTPC64
:
2283 /* Use global offset table as symbol value. */
2284 relocation
= htab
->sgotplt
->output_section
->vma
2285 + htab
->sgotplt
->output_offset
;
2286 unresolved_reloc
= FALSE
;
2289 case R_X86_64_PLTOFF64
:
2290 /* Relocation is PLT entry relative to GOT. For local
2291 symbols it's the symbol itself relative to GOT. */
2293 /* See PLT32 handling. */
2294 && h
->plt
.offset
!= (bfd_vma
) -1
2295 && htab
->splt
!= NULL
)
2297 relocation
= (htab
->splt
->output_section
->vma
2298 + htab
->splt
->output_offset
2300 unresolved_reloc
= FALSE
;
2303 relocation
-= htab
->sgotplt
->output_section
->vma
2304 + htab
->sgotplt
->output_offset
;
2307 case R_X86_64_PLT32
:
2308 /* Relocation is to the entry for this symbol in the
2309 procedure linkage table. */
2311 /* Resolve a PLT32 reloc against a local symbol directly,
2312 without using the procedure linkage table. */
2316 if (h
->plt
.offset
== (bfd_vma
) -1
2317 || htab
->splt
== NULL
)
2319 /* We didn't make a PLT entry for this symbol. This
2320 happens when statically linking PIC code, or when
2321 using -Bsymbolic. */
2325 relocation
= (htab
->splt
->output_section
->vma
2326 + htab
->splt
->output_offset
2328 unresolved_reloc
= FALSE
;
2335 && !SYMBOL_REFERENCES_LOCAL (info
, h
)
2336 && (input_section
->flags
& SEC_ALLOC
) != 0
2337 && (input_section
->flags
& SEC_READONLY
) != 0
2339 || r_type
!= R_X86_64_PC32
2340 || h
->type
!= STT_FUNC
2341 || ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
2342 || !is_32bit_relative_branch (contents
,
2346 && r_type
== R_X86_64_PC32
2347 && h
->type
== STT_FUNC
2348 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
2349 (*_bfd_error_handler
)
2350 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
2351 input_bfd
, h
->root
.root
.string
);
2353 (*_bfd_error_handler
)
2354 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2355 input_bfd
, x86_64_elf_howto_table
[r_type
].name
,
2356 h
->root
.root
.string
);
2357 bfd_set_error (bfd_error_bad_value
);
2367 /* FIXME: The ABI says the linker should make sure the value is
2368 the same when it's zeroextended to 64 bit. */
2370 /* r_symndx will be zero only for relocs against symbols
2371 from removed linkonce sections, or sections discarded by
2374 || (input_section
->flags
& SEC_ALLOC
) == 0)
2379 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2380 || h
->root
.type
!= bfd_link_hash_undefweak
)
2381 && ((r_type
!= R_X86_64_PC8
2382 && r_type
!= R_X86_64_PC16
2383 && r_type
!= R_X86_64_PC32
2384 && r_type
!= R_X86_64_PC64
)
2385 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2386 || (ELIMINATE_COPY_RELOCS
2393 || h
->root
.type
== bfd_link_hash_undefweak
2394 || h
->root
.type
== bfd_link_hash_undefined
)))
2396 Elf_Internal_Rela outrel
;
2398 bfd_boolean skip
, relocate
;
2401 /* When generating a shared object, these relocations
2402 are copied into the output file to be resolved at run
2408 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2410 if (outrel
.r_offset
== (bfd_vma
) -1)
2412 else if (outrel
.r_offset
== (bfd_vma
) -2)
2413 skip
= TRUE
, relocate
= TRUE
;
2415 outrel
.r_offset
+= (input_section
->output_section
->vma
2416 + input_section
->output_offset
);
2419 memset (&outrel
, 0, sizeof outrel
);
2421 /* h->dynindx may be -1 if this symbol was marked to
2425 && (r_type
== R_X86_64_PC8
2426 || r_type
== R_X86_64_PC16
2427 || r_type
== R_X86_64_PC32
2428 || r_type
== R_X86_64_PC64
2430 || !SYMBOLIC_BIND (info
, h
)
2431 || !h
->def_regular
))
2433 outrel
.r_info
= ELF64_R_INFO (h
->dynindx
, r_type
);
2434 outrel
.r_addend
= rel
->r_addend
;
2438 /* This symbol is local, or marked to become local. */
2439 if (r_type
== R_X86_64_64
)
2442 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
2443 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2449 if (bfd_is_abs_section (sec
))
2451 else if (sec
== NULL
|| sec
->owner
== NULL
)
2453 bfd_set_error (bfd_error_bad_value
);
2460 /* We are turning this relocation into one
2461 against a section symbol. It would be
2462 proper to subtract the symbol's value,
2463 osec->vma, from the emitted reloc addend,
2464 but ld.so expects buggy relocs. */
2465 osec
= sec
->output_section
;
2466 sindx
= elf_section_data (osec
)->dynindx
;
2469 asection
*oi
= htab
->elf
.text_index_section
;
2470 sindx
= elf_section_data (oi
)->dynindx
;
2472 BFD_ASSERT (sindx
!= 0);
2475 outrel
.r_info
= ELF64_R_INFO (sindx
, r_type
);
2476 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2480 sreloc
= elf_section_data (input_section
)->sreloc
;
2484 loc
= sreloc
->contents
;
2485 loc
+= sreloc
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2486 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2488 /* If this reloc is against an external symbol, we do
2489 not want to fiddle with the addend. Otherwise, we
2490 need to include the symbol value so that it becomes
2491 an addend for the dynamic reloc. */
2498 case R_X86_64_TLSGD
:
2499 case R_X86_64_GOTPC32_TLSDESC
:
2500 case R_X86_64_TLSDESC_CALL
:
2501 case R_X86_64_GOTTPOFF
:
2502 r_type
= elf64_x86_64_tls_transition (info
, r_type
, h
== NULL
);
2503 tls_type
= GOT_UNKNOWN
;
2504 if (h
== NULL
&& local_got_offsets
)
2505 tls_type
= elf64_x86_64_local_got_tls_type (input_bfd
) [r_symndx
];
2508 tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
2509 if (!info
->shared
&& h
->dynindx
== -1 && tls_type
== GOT_TLS_IE
)
2510 r_type
= R_X86_64_TPOFF32
;
2512 if (r_type
== R_X86_64_TLSGD
2513 || r_type
== R_X86_64_GOTPC32_TLSDESC
2514 || r_type
== R_X86_64_TLSDESC_CALL
)
2516 if (tls_type
== GOT_TLS_IE
)
2517 r_type
= R_X86_64_GOTTPOFF
;
2520 if (r_type
== R_X86_64_TPOFF32
)
2522 BFD_ASSERT (! unresolved_reloc
);
2523 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
2526 static unsigned char tlsgd
[8]
2527 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2529 /* GD->LE transition.
2530 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2531 .word 0x6666; rex64; call __tls_get_addr@plt
2534 leaq foo@tpoff(%rax), %rax */
2535 BFD_ASSERT (rel
->r_offset
>= 4);
2536 for (i
= 0; i
< 4; i
++)
2537 BFD_ASSERT (bfd_get_8 (input_bfd
,
2538 contents
+ rel
->r_offset
- 4 + i
)
2540 BFD_ASSERT (rel
->r_offset
+ 12 <= input_section
->size
);
2541 for (i
= 0; i
< 4; i
++)
2542 BFD_ASSERT (bfd_get_8 (input_bfd
,
2543 contents
+ rel
->r_offset
+ 4 + i
)
2545 BFD_ASSERT (rel
+ 1 < relend
);
2546 BFD_ASSERT (ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PLT32
);
2547 memcpy (contents
+ rel
->r_offset
- 4,
2548 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2550 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2551 contents
+ rel
->r_offset
+ 8);
2552 /* Skip R_X86_64_PLT32. */
2556 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
2558 /* GDesc -> LE transition.
2559 It's originally something like:
2560 leaq x@tlsdesc(%rip), %rax
2565 Registers other than %rax may be set up here. */
2567 unsigned int val
, type
, type2
;
2570 /* First, make sure it's a leaq adding rip to a
2571 32-bit offset into any register, although it's
2572 probably almost always going to be rax. */
2573 roff
= rel
->r_offset
;
2574 BFD_ASSERT (roff
>= 3);
2575 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
2576 BFD_ASSERT ((type
& 0xfb) == 0x48);
2577 type2
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
2578 BFD_ASSERT (type2
== 0x8d);
2579 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
2580 BFD_ASSERT ((val
& 0xc7) == 0x05);
2581 BFD_ASSERT (roff
+ 4 <= input_section
->size
);
2583 /* Now modify the instruction as appropriate. */
2584 bfd_put_8 (output_bfd
, 0x48 | ((type
>> 2) & 1),
2585 contents
+ roff
- 3);
2586 bfd_put_8 (output_bfd
, 0xc7, contents
+ roff
- 2);
2587 bfd_put_8 (output_bfd
, 0xc0 | ((val
>> 3) & 7),
2588 contents
+ roff
- 1);
2589 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2593 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
2595 /* GDesc -> LE transition.
2601 unsigned int val
, type
;
2604 /* First, make sure it's a call *(%rax). */
2605 roff
= rel
->r_offset
;
2606 BFD_ASSERT (roff
+ 2 <= input_section
->size
);
2607 type
= bfd_get_8 (input_bfd
, contents
+ roff
);
2608 BFD_ASSERT (type
== 0xff);
2609 val
= bfd_get_8 (input_bfd
, contents
+ roff
+ 1);
2610 BFD_ASSERT (val
== 0x10);
2612 /* Now modify the instruction as appropriate. Use
2613 xchg %ax,%ax instead of 2 nops. */
2614 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
2615 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
2620 unsigned int val
, type
, reg
;
2622 /* IE->LE transition:
2623 Originally it can be one of:
2624 movq foo@gottpoff(%rip), %reg
2625 addq foo@gottpoff(%rip), %reg
2628 leaq foo(%reg), %reg
2630 BFD_ASSERT (rel
->r_offset
>= 3);
2631 val
= bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 3);
2632 BFD_ASSERT (val
== 0x48 || val
== 0x4c);
2633 type
= bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 2);
2634 BFD_ASSERT (type
== 0x8b || type
== 0x03);
2635 reg
= bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 1);
2636 BFD_ASSERT ((reg
& 0xc7) == 5);
2638 BFD_ASSERT (rel
->r_offset
+ 4 <= input_section
->size
);
2643 bfd_put_8 (output_bfd
, 0x49,
2644 contents
+ rel
->r_offset
- 3);
2645 bfd_put_8 (output_bfd
, 0xc7,
2646 contents
+ rel
->r_offset
- 2);
2647 bfd_put_8 (output_bfd
, 0xc0 | reg
,
2648 contents
+ rel
->r_offset
- 1);
2652 /* addq -> addq - addressing with %rsp/%r12 is
2655 bfd_put_8 (output_bfd
, 0x49,
2656 contents
+ rel
->r_offset
- 3);
2657 bfd_put_8 (output_bfd
, 0x81,
2658 contents
+ rel
->r_offset
- 2);
2659 bfd_put_8 (output_bfd
, 0xc0 | reg
,
2660 contents
+ rel
->r_offset
- 1);
2666 bfd_put_8 (output_bfd
, 0x4d,
2667 contents
+ rel
->r_offset
- 3);
2668 bfd_put_8 (output_bfd
, 0x8d,
2669 contents
+ rel
->r_offset
- 2);
2670 bfd_put_8 (output_bfd
, 0x80 | reg
| (reg
<< 3),
2671 contents
+ rel
->r_offset
- 1);
2673 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2674 contents
+ rel
->r_offset
);
2679 if (htab
->sgot
== NULL
)
2684 off
= h
->got
.offset
;
2685 offplt
= elf64_x86_64_hash_entry (h
)->tlsdesc_got
;
2689 if (local_got_offsets
== NULL
)
2692 off
= local_got_offsets
[r_symndx
];
2693 offplt
= local_tlsdesc_gotents
[r_symndx
];
2700 Elf_Internal_Rela outrel
;
2705 if (htab
->srelgot
== NULL
)
2708 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
2710 if (GOT_TLS_GDESC_P (tls_type
))
2712 outrel
.r_info
= ELF64_R_INFO (indx
, R_X86_64_TLSDESC
);
2713 BFD_ASSERT (htab
->sgotplt_jump_table_size
+ offplt
2714 + 2 * GOT_ENTRY_SIZE
<= htab
->sgotplt
->size
);
2715 outrel
.r_offset
= (htab
->sgotplt
->output_section
->vma
2716 + htab
->sgotplt
->output_offset
2718 + htab
->sgotplt_jump_table_size
);
2719 sreloc
= htab
->srelplt
;
2720 loc
= sreloc
->contents
;
2721 loc
+= sreloc
->reloc_count
++
2722 * sizeof (Elf64_External_Rela
);
2723 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
2724 <= sreloc
->contents
+ sreloc
->size
);
2726 outrel
.r_addend
= relocation
- dtpoff_base (info
);
2728 outrel
.r_addend
= 0;
2729 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2732 sreloc
= htab
->srelgot
;
2734 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
2735 + htab
->sgot
->output_offset
+ off
);
2737 if (GOT_TLS_GD_P (tls_type
))
2738 dr_type
= R_X86_64_DTPMOD64
;
2739 else if (GOT_TLS_GDESC_P (tls_type
))
2742 dr_type
= R_X86_64_TPOFF64
;
2744 bfd_put_64 (output_bfd
, 0, htab
->sgot
->contents
+ off
);
2745 outrel
.r_addend
= 0;
2746 if ((dr_type
== R_X86_64_TPOFF64
2747 || dr_type
== R_X86_64_TLSDESC
) && indx
== 0)
2748 outrel
.r_addend
= relocation
- dtpoff_base (info
);
2749 outrel
.r_info
= ELF64_R_INFO (indx
, dr_type
);
2751 loc
= sreloc
->contents
;
2752 loc
+= sreloc
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2753 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
2754 <= sreloc
->contents
+ sreloc
->size
);
2755 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2757 if (GOT_TLS_GD_P (tls_type
))
2761 BFD_ASSERT (! unresolved_reloc
);
2762 bfd_put_64 (output_bfd
,
2763 relocation
- dtpoff_base (info
),
2764 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
2768 bfd_put_64 (output_bfd
, 0,
2769 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
2770 outrel
.r_info
= ELF64_R_INFO (indx
,
2772 outrel
.r_offset
+= GOT_ENTRY_SIZE
;
2773 sreloc
->reloc_count
++;
2774 loc
+= sizeof (Elf64_External_Rela
);
2775 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
2776 <= sreloc
->contents
+ sreloc
->size
);
2777 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2785 local_got_offsets
[r_symndx
] |= 1;
2788 if (off
>= (bfd_vma
) -2
2789 && ! GOT_TLS_GDESC_P (tls_type
))
2791 if (r_type
== ELF64_R_TYPE (rel
->r_info
))
2793 if (r_type
== R_X86_64_GOTPC32_TLSDESC
2794 || r_type
== R_X86_64_TLSDESC_CALL
)
2795 relocation
= htab
->sgotplt
->output_section
->vma
2796 + htab
->sgotplt
->output_offset
2797 + offplt
+ htab
->sgotplt_jump_table_size
;
2799 relocation
= htab
->sgot
->output_section
->vma
2800 + htab
->sgot
->output_offset
+ off
;
2801 unresolved_reloc
= FALSE
;
2803 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
2806 static unsigned char tlsgd
[8]
2807 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2809 /* GD->IE transition.
2810 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2811 .word 0x6666; rex64; call __tls_get_addr@plt
2814 addq foo@gottpoff(%rip), %rax */
2815 BFD_ASSERT (rel
->r_offset
>= 4);
2816 for (i
= 0; i
< 4; i
++)
2817 BFD_ASSERT (bfd_get_8 (input_bfd
,
2818 contents
+ rel
->r_offset
- 4 + i
)
2820 BFD_ASSERT (rel
->r_offset
+ 12 <= input_section
->size
);
2821 for (i
= 0; i
< 4; i
++)
2822 BFD_ASSERT (bfd_get_8 (input_bfd
,
2823 contents
+ rel
->r_offset
+ 4 + i
)
2825 BFD_ASSERT (rel
+ 1 < relend
);
2826 BFD_ASSERT (ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PLT32
);
2827 memcpy (contents
+ rel
->r_offset
- 4,
2828 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2831 relocation
= (htab
->sgot
->output_section
->vma
2832 + htab
->sgot
->output_offset
+ off
2834 - input_section
->output_section
->vma
2835 - input_section
->output_offset
2837 bfd_put_32 (output_bfd
, relocation
,
2838 contents
+ rel
->r_offset
+ 8);
2839 /* Skip R_X86_64_PLT32. */
2843 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
2845 /* GDesc -> IE transition.
2846 It's originally something like:
2847 leaq x@tlsdesc(%rip), %rax
2850 movq x@gottpoff(%rip), %rax # before nop; nop
2852 Registers other than %rax may be set up here. */
2854 unsigned int val
, type
, type2
;
2857 /* First, make sure it's a leaq adding rip to a 32-bit
2858 offset into any register, although it's probably
2859 almost always going to be rax. */
2860 roff
= rel
->r_offset
;
2861 BFD_ASSERT (roff
>= 3);
2862 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
2863 BFD_ASSERT ((type
& 0xfb) == 0x48);
2864 type2
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
2865 BFD_ASSERT (type2
== 0x8d);
2866 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
2867 BFD_ASSERT ((val
& 0xc7) == 0x05);
2868 BFD_ASSERT (roff
+ 4 <= input_section
->size
);
2870 /* Now modify the instruction as appropriate. */
2871 /* To turn a leaq into a movq in the form we use it, it
2872 suffices to change the second byte from 0x8d to
2874 bfd_put_8 (output_bfd
, 0x8b, contents
+ roff
- 2);
2876 bfd_put_32 (output_bfd
,
2877 htab
->sgot
->output_section
->vma
2878 + htab
->sgot
->output_offset
+ off
2880 - input_section
->output_section
->vma
2881 - input_section
->output_offset
2886 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
2888 /* GDesc -> IE transition.
2895 unsigned int val
, type
;
2898 /* First, make sure it's a call *(%eax). */
2899 roff
= rel
->r_offset
;
2900 BFD_ASSERT (roff
+ 2 <= input_section
->size
);
2901 type
= bfd_get_8 (input_bfd
, contents
+ roff
);
2902 BFD_ASSERT (type
== 0xff);
2903 val
= bfd_get_8 (input_bfd
, contents
+ roff
+ 1);
2904 BFD_ASSERT (val
== 0x10);
2906 /* Now modify the instruction as appropriate. Use
2907 xchg %ax,%ax instead of 2 nops. */
2908 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
2909 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
2917 case R_X86_64_TLSLD
:
2920 /* LD->LE transition:
2922 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2924 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2925 BFD_ASSERT (rel
->r_offset
>= 3);
2926 BFD_ASSERT (bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 3)
2928 BFD_ASSERT (bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 2)
2930 BFD_ASSERT (bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 1)
2932 BFD_ASSERT (rel
->r_offset
+ 9 <= input_section
->size
);
2933 BFD_ASSERT (bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
+ 4)
2935 BFD_ASSERT (rel
+ 1 < relend
);
2936 BFD_ASSERT (ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PLT32
);
2937 memcpy (contents
+ rel
->r_offset
- 3,
2938 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2939 /* Skip R_X86_64_PLT32. */
2944 if (htab
->sgot
== NULL
)
2947 off
= htab
->tls_ld_got
.offset
;
2952 Elf_Internal_Rela outrel
;
2955 if (htab
->srelgot
== NULL
)
2958 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
2959 + htab
->sgot
->output_offset
+ off
);
2961 bfd_put_64 (output_bfd
, 0,
2962 htab
->sgot
->contents
+ off
);
2963 bfd_put_64 (output_bfd
, 0,
2964 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
2965 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_DTPMOD64
);
2966 outrel
.r_addend
= 0;
2967 loc
= htab
->srelgot
->contents
;
2968 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2969 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2970 htab
->tls_ld_got
.offset
|= 1;
2972 relocation
= htab
->sgot
->output_section
->vma
2973 + htab
->sgot
->output_offset
+ off
;
2974 unresolved_reloc
= FALSE
;
2977 case R_X86_64_DTPOFF32
:
2978 if (info
->shared
|| (input_section
->flags
& SEC_CODE
) == 0)
2979 relocation
-= dtpoff_base (info
);
2981 relocation
= tpoff (info
, relocation
);
2984 case R_X86_64_TPOFF32
:
2985 BFD_ASSERT (! info
->shared
);
2986 relocation
= tpoff (info
, relocation
);
2993 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2994 because such sections are not SEC_ALLOC and thus ld.so will
2995 not process them. */
2996 if (unresolved_reloc
2997 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2999 (*_bfd_error_handler
)
3000 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3003 (long) rel
->r_offset
,
3005 h
->root
.root
.string
);
3007 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3008 contents
, rel
->r_offset
,
3009 relocation
, rel
->r_addend
);
3011 if (r
!= bfd_reloc_ok
)
3016 name
= h
->root
.root
.string
;
3019 name
= bfd_elf_string_from_elf_section (input_bfd
,
3020 symtab_hdr
->sh_link
,
3025 name
= bfd_section_name (input_bfd
, sec
);
3028 if (r
== bfd_reloc_overflow
)
3030 if (! ((*info
->callbacks
->reloc_overflow
)
3031 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3032 (bfd_vma
) 0, input_bfd
, input_section
,
3038 (*_bfd_error_handler
)
3039 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3040 input_bfd
, input_section
,
3041 (long) rel
->r_offset
, name
, (int) r
);
3050 /* Finish up dynamic symbol handling. We set the contents of various
3051 dynamic sections here. */
3054 elf64_x86_64_finish_dynamic_symbol (bfd
*output_bfd
,
3055 struct bfd_link_info
*info
,
3056 struct elf_link_hash_entry
*h
,
3057 Elf_Internal_Sym
*sym
)
3059 struct elf64_x86_64_link_hash_table
*htab
;
3061 htab
= elf64_x86_64_hash_table (info
);
3063 if (h
->plt
.offset
!= (bfd_vma
) -1)
3067 Elf_Internal_Rela rela
;
3070 /* This symbol has an entry in the procedure linkage table. Set
3072 if (h
->dynindx
== -1
3073 || htab
->splt
== NULL
3074 || htab
->sgotplt
== NULL
3075 || htab
->srelplt
== NULL
)
3078 /* Get the index in the procedure linkage table which
3079 corresponds to this symbol. This is the index of this symbol
3080 in all the symbols for which we are making plt entries. The
3081 first entry in the procedure linkage table is reserved. */
3082 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
3084 /* Get the offset into the .got table of the entry that
3085 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3086 bytes. The first three are reserved for the dynamic linker. */
3087 got_offset
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
3089 /* Fill in the entry in the procedure linkage table. */
3090 memcpy (htab
->splt
->contents
+ h
->plt
.offset
, elf64_x86_64_plt_entry
,
3093 /* Insert the relocation positions of the plt section. The magic
3094 numbers at the end of the statements are the positions of the
3095 relocations in the plt section. */
3096 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3097 instruction uses 6 bytes, subtract this value. */
3098 bfd_put_32 (output_bfd
,
3099 (htab
->sgotplt
->output_section
->vma
3100 + htab
->sgotplt
->output_offset
3102 - htab
->splt
->output_section
->vma
3103 - htab
->splt
->output_offset
3106 htab
->splt
->contents
+ h
->plt
.offset
+ 2);
3107 /* Put relocation index. */
3108 bfd_put_32 (output_bfd
, plt_index
,
3109 htab
->splt
->contents
+ h
->plt
.offset
+ 7);
3110 /* Put offset for jmp .PLT0. */
3111 bfd_put_32 (output_bfd
, - (h
->plt
.offset
+ PLT_ENTRY_SIZE
),
3112 htab
->splt
->contents
+ h
->plt
.offset
+ 12);
3114 /* Fill in the entry in the global offset table, initially this
3115 points to the pushq instruction in the PLT which is at offset 6. */
3116 bfd_put_64 (output_bfd
, (htab
->splt
->output_section
->vma
3117 + htab
->splt
->output_offset
3118 + h
->plt
.offset
+ 6),
3119 htab
->sgotplt
->contents
+ got_offset
);
3121 /* Fill in the entry in the .rela.plt section. */
3122 rela
.r_offset
= (htab
->sgotplt
->output_section
->vma
3123 + htab
->sgotplt
->output_offset
3125 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_JUMP_SLOT
);
3127 loc
= htab
->srelplt
->contents
+ plt_index
* sizeof (Elf64_External_Rela
);
3128 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3130 if (!h
->def_regular
)
3132 /* Mark the symbol as undefined, rather than as defined in
3133 the .plt section. Leave the value if there were any
3134 relocations where pointer equality matters (this is a clue
3135 for the dynamic linker, to make function pointer
3136 comparisons work between an application and shared
3137 library), otherwise set it to zero. If a function is only
3138 called from a binary, there is no need to slow down
3139 shared libraries because of that. */
3140 sym
->st_shndx
= SHN_UNDEF
;
3141 if (!h
->pointer_equality_needed
)
3146 if (h
->got
.offset
!= (bfd_vma
) -1
3147 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h
)->tls_type
)
3148 && elf64_x86_64_hash_entry (h
)->tls_type
!= GOT_TLS_IE
)
3150 Elf_Internal_Rela rela
;
3153 /* This symbol has an entry in the global offset table. Set it
3155 if (htab
->sgot
== NULL
|| htab
->srelgot
== NULL
)
3158 rela
.r_offset
= (htab
->sgot
->output_section
->vma
3159 + htab
->sgot
->output_offset
3160 + (h
->got
.offset
&~ (bfd_vma
) 1));
3162 /* If this is a static link, or it is a -Bsymbolic link and the
3163 symbol is defined locally or was forced to be local because
3164 of a version file, we just want to emit a RELATIVE reloc.
3165 The entry in the global offset table will already have been
3166 initialized in the relocate_section function. */
3168 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3170 BFD_ASSERT((h
->got
.offset
& 1) != 0);
3171 rela
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
3172 rela
.r_addend
= (h
->root
.u
.def
.value
3173 + h
->root
.u
.def
.section
->output_section
->vma
3174 + h
->root
.u
.def
.section
->output_offset
);
3178 BFD_ASSERT((h
->got
.offset
& 1) == 0);
3179 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
3180 htab
->sgot
->contents
+ h
->got
.offset
);
3181 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_GLOB_DAT
);
3185 loc
= htab
->srelgot
->contents
;
3186 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3187 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3192 Elf_Internal_Rela rela
;
3195 /* This symbol needs a copy reloc. Set it up. */
3197 if (h
->dynindx
== -1
3198 || (h
->root
.type
!= bfd_link_hash_defined
3199 && h
->root
.type
!= bfd_link_hash_defweak
)
3200 || htab
->srelbss
== NULL
)
3203 rela
.r_offset
= (h
->root
.u
.def
.value
3204 + h
->root
.u
.def
.section
->output_section
->vma
3205 + h
->root
.u
.def
.section
->output_offset
);
3206 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_COPY
);
3208 loc
= htab
->srelbss
->contents
;
3209 loc
+= htab
->srelbss
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3210 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3213 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3214 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
3215 || h
== htab
->elf
.hgot
)
3216 sym
->st_shndx
= SHN_ABS
;
3221 /* Used to decide how to sort relocs in an optimal manner for the
3222 dynamic linker, before writing them out. */
3224 static enum elf_reloc_type_class
3225 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela
*rela
)
3227 switch ((int) ELF64_R_TYPE (rela
->r_info
))
3229 case R_X86_64_RELATIVE
:
3230 return reloc_class_relative
;
3231 case R_X86_64_JUMP_SLOT
:
3232 return reloc_class_plt
;
3234 return reloc_class_copy
;
3236 return reloc_class_normal
;
3240 /* Finish up the dynamic sections. */
3243 elf64_x86_64_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
3245 struct elf64_x86_64_link_hash_table
*htab
;
3249 htab
= elf64_x86_64_hash_table (info
);
3250 dynobj
= htab
->elf
.dynobj
;
3251 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3253 if (htab
->elf
.dynamic_sections_created
)
3255 Elf64_External_Dyn
*dyncon
, *dynconend
;
3257 if (sdyn
== NULL
|| htab
->sgot
== NULL
)
3260 dyncon
= (Elf64_External_Dyn
*) sdyn
->contents
;
3261 dynconend
= (Elf64_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
3262 for (; dyncon
< dynconend
; dyncon
++)
3264 Elf_Internal_Dyn dyn
;
3267 bfd_elf64_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3276 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3280 dyn
.d_un
.d_ptr
= htab
->srelplt
->output_section
->vma
;
3284 s
= htab
->srelplt
->output_section
;
3285 dyn
.d_un
.d_val
= s
->size
;
3289 /* The procedure linkage table relocs (DT_JMPREL) should
3290 not be included in the overall relocs (DT_RELA).
3291 Therefore, we override the DT_RELASZ entry here to
3292 make it not include the JMPREL relocs. Since the
3293 linker script arranges for .rela.plt to follow all
3294 other relocation sections, we don't have to worry
3295 about changing the DT_RELA entry. */
3296 if (htab
->srelplt
!= NULL
)
3298 s
= htab
->srelplt
->output_section
;
3299 dyn
.d_un
.d_val
-= s
->size
;
3303 case DT_TLSDESC_PLT
:
3305 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
3306 + htab
->tlsdesc_plt
;
3309 case DT_TLSDESC_GOT
:
3311 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
3312 + htab
->tlsdesc_got
;
3316 bfd_elf64_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3319 /* Fill in the special first entry in the procedure linkage table. */
3320 if (htab
->splt
&& htab
->splt
->size
> 0)
3322 /* Fill in the first entry in the procedure linkage table. */
3323 memcpy (htab
->splt
->contents
, elf64_x86_64_plt0_entry
,
3325 /* Add offset for pushq GOT+8(%rip), since the instruction
3326 uses 6 bytes subtract this value. */
3327 bfd_put_32 (output_bfd
,
3328 (htab
->sgotplt
->output_section
->vma
3329 + htab
->sgotplt
->output_offset
3331 - htab
->splt
->output_section
->vma
3332 - htab
->splt
->output_offset
3334 htab
->splt
->contents
+ 2);
3335 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3336 the end of the instruction. */
3337 bfd_put_32 (output_bfd
,
3338 (htab
->sgotplt
->output_section
->vma
3339 + htab
->sgotplt
->output_offset
3341 - htab
->splt
->output_section
->vma
3342 - htab
->splt
->output_offset
3344 htab
->splt
->contents
+ 8);
3346 elf_section_data (htab
->splt
->output_section
)->this_hdr
.sh_entsize
=
3349 if (htab
->tlsdesc_plt
)
3351 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
3352 htab
->sgot
->contents
+ htab
->tlsdesc_got
);
3354 memcpy (htab
->splt
->contents
+ htab
->tlsdesc_plt
,
3355 elf64_x86_64_plt0_entry
,
3358 /* Add offset for pushq GOT+8(%rip), since the
3359 instruction uses 6 bytes subtract this value. */
3360 bfd_put_32 (output_bfd
,
3361 (htab
->sgotplt
->output_section
->vma
3362 + htab
->sgotplt
->output_offset
3364 - htab
->splt
->output_section
->vma
3365 - htab
->splt
->output_offset
3368 htab
->splt
->contents
+ htab
->tlsdesc_plt
+ 2);
3369 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3370 htab->tlsdesc_got. The 12 is the offset to the end of
3372 bfd_put_32 (output_bfd
,
3373 (htab
->sgot
->output_section
->vma
3374 + htab
->sgot
->output_offset
3376 - htab
->splt
->output_section
->vma
3377 - htab
->splt
->output_offset
3380 htab
->splt
->contents
+ htab
->tlsdesc_plt
+ 8);
3387 /* Fill in the first three entries in the global offset table. */
3388 if (htab
->sgotplt
->size
> 0)
3390 /* Set the first entry in the global offset table to the address of
3391 the dynamic section. */
3393 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
);
3395 bfd_put_64 (output_bfd
,
3396 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
3397 htab
->sgotplt
->contents
);
3398 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3399 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3400 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
*2);
3403 elf_section_data (htab
->sgotplt
->output_section
)->this_hdr
.sh_entsize
=
3407 if (htab
->sgot
&& htab
->sgot
->size
> 0)
3408 elf_section_data (htab
->sgot
->output_section
)->this_hdr
.sh_entsize
3414 /* Return address for Ith PLT stub in section PLT, for relocation REL
3415 or (bfd_vma) -1 if it should not be included. */
3418 elf64_x86_64_plt_sym_val (bfd_vma i
, const asection
*plt
,
3419 const arelent
*rel ATTRIBUTE_UNUSED
)
3421 return plt
->vma
+ (i
+ 1) * PLT_ENTRY_SIZE
;
3424 /* Handle an x86-64 specific section when reading an object file. This
3425 is called when elfcode.h finds a section with an unknown type. */
3428 elf64_x86_64_section_from_shdr (bfd
*abfd
,
3429 Elf_Internal_Shdr
*hdr
,
3433 if (hdr
->sh_type
!= SHT_X86_64_UNWIND
)
3436 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
3442 /* Hook called by the linker routine which adds symbols from an object
3443 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3447 elf64_x86_64_add_symbol_hook (bfd
*abfd
,
3448 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3449 Elf_Internal_Sym
*sym
,
3450 const char **namep ATTRIBUTE_UNUSED
,
3451 flagword
*flagsp ATTRIBUTE_UNUSED
,
3452 asection
**secp
, bfd_vma
*valp
)
3456 switch (sym
->st_shndx
)
3458 case SHN_X86_64_LCOMMON
:
3459 lcomm
= bfd_get_section_by_name (abfd
, "LARGE_COMMON");
3462 lcomm
= bfd_make_section_with_flags (abfd
,
3466 | SEC_LINKER_CREATED
));
3469 elf_section_flags (lcomm
) |= SHF_X86_64_LARGE
;
3472 *valp
= sym
->st_size
;
3479 /* Given a BFD section, try to locate the corresponding ELF section
3483 elf64_x86_64_elf_section_from_bfd_section (bfd
*abfd ATTRIBUTE_UNUSED
,
3484 asection
*sec
, int *index
)
3486 if (sec
== &_bfd_elf_large_com_section
)
3488 *index
= SHN_X86_64_LCOMMON
;
3494 /* Process a symbol. */
3497 elf64_x86_64_symbol_processing (bfd
*abfd ATTRIBUTE_UNUSED
,
3500 elf_symbol_type
*elfsym
= (elf_symbol_type
*) asym
;
3502 switch (elfsym
->internal_elf_sym
.st_shndx
)
3504 case SHN_X86_64_LCOMMON
:
3505 asym
->section
= &_bfd_elf_large_com_section
;
3506 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
3507 /* Common symbol doesn't set BSF_GLOBAL. */
3508 asym
->flags
&= ~BSF_GLOBAL
;
3514 elf64_x86_64_common_definition (Elf_Internal_Sym
*sym
)
3516 return (sym
->st_shndx
== SHN_COMMON
3517 || sym
->st_shndx
== SHN_X86_64_LCOMMON
);
3521 elf64_x86_64_common_section_index (asection
*sec
)
3523 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
3526 return SHN_X86_64_LCOMMON
;
3530 elf64_x86_64_common_section (asection
*sec
)
3532 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
3533 return bfd_com_section_ptr
;
3535 return &_bfd_elf_large_com_section
;
3539 elf64_x86_64_merge_symbol (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3540 struct elf_link_hash_entry
**sym_hash ATTRIBUTE_UNUSED
,
3541 struct elf_link_hash_entry
*h
,
3542 Elf_Internal_Sym
*sym
,
3544 bfd_vma
*pvalue ATTRIBUTE_UNUSED
,
3545 unsigned int *pold_alignment ATTRIBUTE_UNUSED
,
3546 bfd_boolean
*skip ATTRIBUTE_UNUSED
,
3547 bfd_boolean
*override ATTRIBUTE_UNUSED
,
3548 bfd_boolean
*type_change_ok ATTRIBUTE_UNUSED
,
3549 bfd_boolean
*size_change_ok ATTRIBUTE_UNUSED
,
3550 bfd_boolean
*newdef ATTRIBUTE_UNUSED
,
3551 bfd_boolean
*newdyn
,
3552 bfd_boolean
*newdyncommon ATTRIBUTE_UNUSED
,
3553 bfd_boolean
*newweak ATTRIBUTE_UNUSED
,
3554 bfd
*abfd ATTRIBUTE_UNUSED
,
3556 bfd_boolean
*olddef ATTRIBUTE_UNUSED
,
3557 bfd_boolean
*olddyn
,
3558 bfd_boolean
*olddyncommon ATTRIBUTE_UNUSED
,
3559 bfd_boolean
*oldweak ATTRIBUTE_UNUSED
,
3563 /* A normal common symbol and a large common symbol result in a
3564 normal common symbol. We turn the large common symbol into a
3567 && h
->root
.type
== bfd_link_hash_common
3569 && bfd_is_com_section (*sec
)
3572 if (sym
->st_shndx
== SHN_COMMON
3573 && (elf_section_flags (*oldsec
) & SHF_X86_64_LARGE
) != 0)
3575 h
->root
.u
.c
.p
->section
3576 = bfd_make_section_old_way (oldbfd
, "COMMON");
3577 h
->root
.u
.c
.p
->section
->flags
= SEC_ALLOC
;
3579 else if (sym
->st_shndx
== SHN_X86_64_LCOMMON
3580 && (elf_section_flags (*oldsec
) & SHF_X86_64_LARGE
) == 0)
3581 *psec
= *sec
= bfd_com_section_ptr
;
3588 elf64_x86_64_additional_program_headers (bfd
*abfd
,
3589 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
3594 /* Check to see if we need a large readonly segment. */
3595 s
= bfd_get_section_by_name (abfd
, ".lrodata");
3596 if (s
&& (s
->flags
& SEC_LOAD
))
3599 /* Check to see if we need a large data segment. Since .lbss sections
3600 is placed right after the .bss section, there should be no need for
3601 a large data segment just because of .lbss. */
3602 s
= bfd_get_section_by_name (abfd
, ".ldata");
3603 if (s
&& (s
->flags
& SEC_LOAD
))
3609 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3612 elf64_x86_64_hash_symbol (struct elf_link_hash_entry
*h
)
3614 if (h
->plt
.offset
!= (bfd_vma
) -1
3616 && !h
->pointer_equality_needed
)
3619 return _bfd_elf_hash_symbol (h
);
3622 static const struct bfd_elf_special_section
3623 elf64_x86_64_special_sections
[]=
3625 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3626 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
3627 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
+ SHF_X86_64_LARGE
},
3628 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3629 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3630 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
3631 { NULL
, 0, 0, 0, 0 }
3634 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3635 #define TARGET_LITTLE_NAME "elf64-x86-64"
3636 #define ELF_ARCH bfd_arch_i386
3637 #define ELF_MACHINE_CODE EM_X86_64
3638 #define ELF_MAXPAGESIZE 0x200000
3639 #define ELF_MINPAGESIZE 0x1000
3640 #define ELF_COMMONPAGESIZE 0x1000
3642 #define elf_backend_can_gc_sections 1
3643 #define elf_backend_can_refcount 1
3644 #define elf_backend_want_got_plt 1
3645 #define elf_backend_plt_readonly 1
3646 #define elf_backend_want_plt_sym 0
3647 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
3648 #define elf_backend_rela_normal 1
3650 #define elf_info_to_howto elf64_x86_64_info_to_howto
3652 #define bfd_elf64_bfd_link_hash_table_create \
3653 elf64_x86_64_link_hash_table_create
3654 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
3656 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3657 #define elf_backend_check_relocs elf64_x86_64_check_relocs
3658 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3659 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3660 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
3661 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3662 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3663 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3664 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3665 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
3666 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
3667 #define elf_backend_relocate_section elf64_x86_64_relocate_section
3668 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
3669 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
3670 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
3671 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
3672 #define elf_backend_object_p elf64_x86_64_elf_object_p
3673 #define bfd_elf64_mkobject elf64_x86_64_mkobject
3675 #define elf_backend_section_from_shdr \
3676 elf64_x86_64_section_from_shdr
3678 #define elf_backend_section_from_bfd_section \
3679 elf64_x86_64_elf_section_from_bfd_section
3680 #define elf_backend_add_symbol_hook \
3681 elf64_x86_64_add_symbol_hook
3682 #define elf_backend_symbol_processing \
3683 elf64_x86_64_symbol_processing
3684 #define elf_backend_common_section_index \
3685 elf64_x86_64_common_section_index
3686 #define elf_backend_common_section \
3687 elf64_x86_64_common_section
3688 #define elf_backend_common_definition \
3689 elf64_x86_64_common_definition
3690 #define elf_backend_merge_symbol \
3691 elf64_x86_64_merge_symbol
3692 #define elf_backend_special_sections \
3693 elf64_x86_64_special_sections
3694 #define elf_backend_additional_program_headers \
3695 elf64_x86_64_additional_program_headers
3696 #define elf_backend_hash_symbol \
3697 elf64_x86_64_hash_symbol
3699 #include "elf64-target.h"
3701 /* FreeBSD support. */
3703 #undef TARGET_LITTLE_SYM
3704 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
3705 #undef TARGET_LITTLE_NAME
3706 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
3708 /* The kernel recognizes executables as valid only if they carry a
3709 "FreeBSD" label in the ELF header. So we put this label on all
3710 executables and (for simplicity) also all other object files. */
3713 elf64_x86_64_fbsd_post_process_headers (bfd
* abfd
,
3714 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
3716 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
3718 i_ehdrp
= elf_elfheader (abfd
);
3720 /* Put an ABI label supported by FreeBSD >= 4.1. */
3721 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
3724 #undef elf_backend_post_process_headers
3725 #define elf_backend_post_process_headers elf64_x86_64_fbsd_post_process_headers
3728 #define elf64_bed elf64_x86_64_fbsd_bed
3730 #include "elf64-target.h"