1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>.
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 3 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,
21 MA 02110-1301, USA. */
28 #include "bfd_stdint.h"
32 #include "elf/x86-64.h"
34 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
35 #define MINUS_ONE (~ (bfd_vma) 0)
37 /* The relocation "howto" table. Order of fields:
38 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
39 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
40 static reloc_howto_type x86_64_elf_howto_table
[] =
42 HOWTO(R_X86_64_NONE
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
43 bfd_elf_generic_reloc
, "R_X86_64_NONE", FALSE
, 0x00000000, 0x00000000,
45 HOWTO(R_X86_64_64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
46 bfd_elf_generic_reloc
, "R_X86_64_64", FALSE
, MINUS_ONE
, MINUS_ONE
,
48 HOWTO(R_X86_64_PC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
49 bfd_elf_generic_reloc
, "R_X86_64_PC32", FALSE
, 0xffffffff, 0xffffffff,
51 HOWTO(R_X86_64_GOT32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
52 bfd_elf_generic_reloc
, "R_X86_64_GOT32", FALSE
, 0xffffffff, 0xffffffff,
54 HOWTO(R_X86_64_PLT32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
55 bfd_elf_generic_reloc
, "R_X86_64_PLT32", FALSE
, 0xffffffff, 0xffffffff,
57 HOWTO(R_X86_64_COPY
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
58 bfd_elf_generic_reloc
, "R_X86_64_COPY", FALSE
, 0xffffffff, 0xffffffff,
60 HOWTO(R_X86_64_GLOB_DAT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
61 bfd_elf_generic_reloc
, "R_X86_64_GLOB_DAT", FALSE
, MINUS_ONE
,
63 HOWTO(R_X86_64_JUMP_SLOT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
64 bfd_elf_generic_reloc
, "R_X86_64_JUMP_SLOT", FALSE
, MINUS_ONE
,
66 HOWTO(R_X86_64_RELATIVE
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
67 bfd_elf_generic_reloc
, "R_X86_64_RELATIVE", FALSE
, MINUS_ONE
,
69 HOWTO(R_X86_64_GOTPCREL
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
70 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL", FALSE
, 0xffffffff,
72 HOWTO(R_X86_64_32
, 0, 2, 32, FALSE
, 0, complain_overflow_unsigned
,
73 bfd_elf_generic_reloc
, "R_X86_64_32", FALSE
, 0xffffffff, 0xffffffff,
75 HOWTO(R_X86_64_32S
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
76 bfd_elf_generic_reloc
, "R_X86_64_32S", FALSE
, 0xffffffff, 0xffffffff,
78 HOWTO(R_X86_64_16
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
,
79 bfd_elf_generic_reloc
, "R_X86_64_16", FALSE
, 0xffff, 0xffff, FALSE
),
80 HOWTO(R_X86_64_PC16
,0, 1, 16, TRUE
, 0, complain_overflow_bitfield
,
81 bfd_elf_generic_reloc
, "R_X86_64_PC16", FALSE
, 0xffff, 0xffff, TRUE
),
82 HOWTO(R_X86_64_8
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
,
83 bfd_elf_generic_reloc
, "R_X86_64_8", FALSE
, 0xff, 0xff, FALSE
),
84 HOWTO(R_X86_64_PC8
, 0, 0, 8, TRUE
, 0, complain_overflow_signed
,
85 bfd_elf_generic_reloc
, "R_X86_64_PC8", FALSE
, 0xff, 0xff, TRUE
),
86 HOWTO(R_X86_64_DTPMOD64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
87 bfd_elf_generic_reloc
, "R_X86_64_DTPMOD64", FALSE
, MINUS_ONE
,
89 HOWTO(R_X86_64_DTPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
90 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF64", FALSE
, MINUS_ONE
,
92 HOWTO(R_X86_64_TPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
93 bfd_elf_generic_reloc
, "R_X86_64_TPOFF64", FALSE
, MINUS_ONE
,
95 HOWTO(R_X86_64_TLSGD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
96 bfd_elf_generic_reloc
, "R_X86_64_TLSGD", FALSE
, 0xffffffff,
98 HOWTO(R_X86_64_TLSLD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
99 bfd_elf_generic_reloc
, "R_X86_64_TLSLD", FALSE
, 0xffffffff,
101 HOWTO(R_X86_64_DTPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
102 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF32", FALSE
, 0xffffffff,
104 HOWTO(R_X86_64_GOTTPOFF
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
105 bfd_elf_generic_reloc
, "R_X86_64_GOTTPOFF", FALSE
, 0xffffffff,
107 HOWTO(R_X86_64_TPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
108 bfd_elf_generic_reloc
, "R_X86_64_TPOFF32", FALSE
, 0xffffffff,
110 HOWTO(R_X86_64_PC64
, 0, 4, 64, TRUE
, 0, complain_overflow_bitfield
,
111 bfd_elf_generic_reloc
, "R_X86_64_PC64", FALSE
, MINUS_ONE
, MINUS_ONE
,
113 HOWTO(R_X86_64_GOTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
114 bfd_elf_generic_reloc
, "R_X86_64_GOTOFF64",
115 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
116 HOWTO(R_X86_64_GOTPC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
117 bfd_elf_generic_reloc
, "R_X86_64_GOTPC32",
118 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
119 HOWTO(R_X86_64_GOT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
120 bfd_elf_generic_reloc
, "R_X86_64_GOT64", FALSE
, MINUS_ONE
, MINUS_ONE
,
122 HOWTO(R_X86_64_GOTPCREL64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
123 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL64", FALSE
, MINUS_ONE
,
125 HOWTO(R_X86_64_GOTPC64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
126 bfd_elf_generic_reloc
, "R_X86_64_GOTPC64",
127 FALSE
, MINUS_ONE
, MINUS_ONE
, TRUE
),
128 HOWTO(R_X86_64_GOTPLT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
129 bfd_elf_generic_reloc
, "R_X86_64_GOTPLT64", FALSE
, MINUS_ONE
,
131 HOWTO(R_X86_64_PLTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
132 bfd_elf_generic_reloc
, "R_X86_64_PLTOFF64", FALSE
, MINUS_ONE
,
136 HOWTO(R_X86_64_GOTPC32_TLSDESC
, 0, 2, 32, TRUE
, 0,
137 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
138 "R_X86_64_GOTPC32_TLSDESC",
139 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
140 HOWTO(R_X86_64_TLSDESC_CALL
, 0, 0, 0, FALSE
, 0,
141 complain_overflow_dont
, bfd_elf_generic_reloc
,
142 "R_X86_64_TLSDESC_CALL",
144 HOWTO(R_X86_64_TLSDESC
, 0, 4, 64, FALSE
, 0,
145 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
147 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
148 HOWTO(R_X86_64_IRELATIVE
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
149 bfd_elf_generic_reloc
, "R_X86_64_IRELATIVE", FALSE
, MINUS_ONE
,
152 /* We have a gap in the reloc numbers here.
153 R_X86_64_standard counts the number up to this point, and
154 R_X86_64_vt_offset is the value to subtract from a reloc type of
155 R_X86_64_GNU_VT* to form an index into this table. */
156 #define R_X86_64_standard (R_X86_64_IRELATIVE + 1)
157 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
159 /* GNU extension to record C++ vtable hierarchy. */
160 HOWTO (R_X86_64_GNU_VTINHERIT
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
161 NULL
, "R_X86_64_GNU_VTINHERIT", FALSE
, 0, 0, FALSE
),
163 /* GNU extension to record C++ vtable member usage. */
164 HOWTO (R_X86_64_GNU_VTENTRY
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
165 _bfd_elf_rel_vtable_reloc_fn
, "R_X86_64_GNU_VTENTRY", FALSE
, 0, 0,
169 #define IS_X86_64_PCREL_TYPE(TYPE) \
170 ( ((TYPE) == R_X86_64_PC8) \
171 || ((TYPE) == R_X86_64_PC16) \
172 || ((TYPE) == R_X86_64_PC32) \
173 || ((TYPE) == R_X86_64_PC64))
175 /* Map BFD relocs to the x86_64 elf relocs. */
178 bfd_reloc_code_real_type bfd_reloc_val
;
179 unsigned char elf_reloc_val
;
182 static const struct elf_reloc_map x86_64_reloc_map
[] =
184 { BFD_RELOC_NONE
, R_X86_64_NONE
, },
185 { BFD_RELOC_64
, R_X86_64_64
, },
186 { BFD_RELOC_32_PCREL
, R_X86_64_PC32
, },
187 { BFD_RELOC_X86_64_GOT32
, R_X86_64_GOT32
,},
188 { BFD_RELOC_X86_64_PLT32
, R_X86_64_PLT32
,},
189 { BFD_RELOC_X86_64_COPY
, R_X86_64_COPY
, },
190 { BFD_RELOC_X86_64_GLOB_DAT
, R_X86_64_GLOB_DAT
, },
191 { BFD_RELOC_X86_64_JUMP_SLOT
, R_X86_64_JUMP_SLOT
, },
192 { BFD_RELOC_X86_64_RELATIVE
, R_X86_64_RELATIVE
, },
193 { BFD_RELOC_X86_64_GOTPCREL
, R_X86_64_GOTPCREL
, },
194 { BFD_RELOC_32
, R_X86_64_32
, },
195 { BFD_RELOC_X86_64_32S
, R_X86_64_32S
, },
196 { BFD_RELOC_16
, R_X86_64_16
, },
197 { BFD_RELOC_16_PCREL
, R_X86_64_PC16
, },
198 { BFD_RELOC_8
, R_X86_64_8
, },
199 { BFD_RELOC_8_PCREL
, R_X86_64_PC8
, },
200 { BFD_RELOC_X86_64_DTPMOD64
, R_X86_64_DTPMOD64
, },
201 { BFD_RELOC_X86_64_DTPOFF64
, R_X86_64_DTPOFF64
, },
202 { BFD_RELOC_X86_64_TPOFF64
, R_X86_64_TPOFF64
, },
203 { BFD_RELOC_X86_64_TLSGD
, R_X86_64_TLSGD
, },
204 { BFD_RELOC_X86_64_TLSLD
, R_X86_64_TLSLD
, },
205 { BFD_RELOC_X86_64_DTPOFF32
, R_X86_64_DTPOFF32
, },
206 { BFD_RELOC_X86_64_GOTTPOFF
, R_X86_64_GOTTPOFF
, },
207 { BFD_RELOC_X86_64_TPOFF32
, R_X86_64_TPOFF32
, },
208 { BFD_RELOC_64_PCREL
, R_X86_64_PC64
, },
209 { BFD_RELOC_X86_64_GOTOFF64
, R_X86_64_GOTOFF64
, },
210 { BFD_RELOC_X86_64_GOTPC32
, R_X86_64_GOTPC32
, },
211 { BFD_RELOC_X86_64_GOT64
, R_X86_64_GOT64
, },
212 { BFD_RELOC_X86_64_GOTPCREL64
,R_X86_64_GOTPCREL64
, },
213 { BFD_RELOC_X86_64_GOTPC64
, R_X86_64_GOTPC64
, },
214 { BFD_RELOC_X86_64_GOTPLT64
, R_X86_64_GOTPLT64
, },
215 { BFD_RELOC_X86_64_PLTOFF64
, R_X86_64_PLTOFF64
, },
216 { BFD_RELOC_X86_64_GOTPC32_TLSDESC
, R_X86_64_GOTPC32_TLSDESC
, },
217 { BFD_RELOC_X86_64_TLSDESC_CALL
, R_X86_64_TLSDESC_CALL
, },
218 { BFD_RELOC_X86_64_TLSDESC
, R_X86_64_TLSDESC
, },
219 { BFD_RELOC_X86_64_IRELATIVE
, R_X86_64_IRELATIVE
, },
220 { BFD_RELOC_VTABLE_INHERIT
, R_X86_64_GNU_VTINHERIT
, },
221 { BFD_RELOC_VTABLE_ENTRY
, R_X86_64_GNU_VTENTRY
, },
224 static reloc_howto_type
*
225 elf64_x86_64_rtype_to_howto (bfd
*abfd
, unsigned r_type
)
229 if (r_type
< (unsigned int) R_X86_64_GNU_VTINHERIT
230 || r_type
>= (unsigned int) R_X86_64_max
)
232 if (r_type
>= (unsigned int) R_X86_64_standard
)
234 (*_bfd_error_handler
) (_("%B: invalid relocation type %d"),
236 r_type
= R_X86_64_NONE
;
241 i
= r_type
- (unsigned int) R_X86_64_vt_offset
;
242 BFD_ASSERT (x86_64_elf_howto_table
[i
].type
== r_type
);
243 return &x86_64_elf_howto_table
[i
];
246 /* Given a BFD reloc type, return a HOWTO structure. */
247 static reloc_howto_type
*
248 elf64_x86_64_reloc_type_lookup (bfd
*abfd
,
249 bfd_reloc_code_real_type code
)
253 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
256 if (x86_64_reloc_map
[i
].bfd_reloc_val
== code
)
257 return elf64_x86_64_rtype_to_howto (abfd
,
258 x86_64_reloc_map
[i
].elf_reloc_val
);
263 static reloc_howto_type
*
264 elf64_x86_64_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
270 i
< (sizeof (x86_64_elf_howto_table
)
271 / sizeof (x86_64_elf_howto_table
[0]));
273 if (x86_64_elf_howto_table
[i
].name
!= NULL
274 && strcasecmp (x86_64_elf_howto_table
[i
].name
, r_name
) == 0)
275 return &x86_64_elf_howto_table
[i
];
280 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
283 elf64_x86_64_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
284 Elf_Internal_Rela
*dst
)
288 r_type
= ELF64_R_TYPE (dst
->r_info
);
289 cache_ptr
->howto
= elf64_x86_64_rtype_to_howto (abfd
, r_type
);
290 BFD_ASSERT (r_type
== cache_ptr
->howto
->type
);
293 /* Support for core dump NOTE sections. */
295 elf64_x86_64_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
300 switch (note
->descsz
)
305 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
307 elf_tdata (abfd
)->core_signal
308 = bfd_get_16 (abfd
, note
->descdata
+ 12);
311 elf_tdata (abfd
)->core_pid
312 = bfd_get_32 (abfd
, note
->descdata
+ 32);
321 /* Make a ".reg/999" section. */
322 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
323 size
, note
->descpos
+ offset
);
327 elf64_x86_64_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
329 switch (note
->descsz
)
334 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
335 elf_tdata (abfd
)->core_program
336 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 40, 16);
337 elf_tdata (abfd
)->core_command
338 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 56, 80);
341 /* Note that for some reason, a spurious space is tacked
342 onto the end of the args in some (at least one anyway)
343 implementations, so strip it off if it exists. */
346 char *command
= elf_tdata (abfd
)->core_command
;
347 int n
= strlen (command
);
349 if (0 < n
&& command
[n
- 1] == ' ')
350 command
[n
- 1] = '\0';
356 /* Functions for the x86-64 ELF linker. */
358 /* The name of the dynamic interpreter. This is put in the .interp
361 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
363 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
364 copying dynamic variables from a shared lib into an app's dynbss
365 section, and instead use a dynamic relocation to point into the
367 #define ELIMINATE_COPY_RELOCS 1
369 /* The size in bytes of an entry in the global offset table. */
371 #define GOT_ENTRY_SIZE 8
373 /* The size in bytes of an entry in the procedure linkage table. */
375 #define PLT_ENTRY_SIZE 16
377 /* The first entry in a procedure linkage table looks like this. See the
378 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
380 static const bfd_byte elf64_x86_64_plt0_entry
[PLT_ENTRY_SIZE
] =
382 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
383 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
384 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
387 /* Subsequent entries in a procedure linkage table look like this. */
389 static const bfd_byte elf64_x86_64_plt_entry
[PLT_ENTRY_SIZE
] =
391 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
392 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
393 0x68, /* pushq immediate */
394 0, 0, 0, 0, /* replaced with index into relocation table. */
395 0xe9, /* jmp relative */
396 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
399 /* The x86-64 linker needs to keep track of the number of relocs that
400 it decides to copy as dynamic relocs in check_relocs for each symbol.
401 This is so that it can later discard them if they are found to be
402 unnecessary. We store the information in a field extending the
403 regular ELF linker hash table. */
405 struct elf64_x86_64_dyn_relocs
408 struct elf64_x86_64_dyn_relocs
*next
;
410 /* The input section of the reloc. */
413 /* Total number of relocs copied for the input section. */
416 /* Number of pc-relative relocs copied for the input section. */
417 bfd_size_type pc_count
;
420 /* x86-64 ELF linker hash entry. */
422 struct elf64_x86_64_link_hash_entry
424 struct elf_link_hash_entry elf
;
426 /* Track dynamic relocs copied for this symbol. */
427 struct elf64_x86_64_dyn_relocs
*dyn_relocs
;
429 #define GOT_UNKNOWN 0
433 #define GOT_TLS_GDESC 4
434 #define GOT_TLS_GD_BOTH_P(type) \
435 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
436 #define GOT_TLS_GD_P(type) \
437 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
438 #define GOT_TLS_GDESC_P(type) \
439 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
440 #define GOT_TLS_GD_ANY_P(type) \
441 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
442 unsigned char tls_type
;
444 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
445 starting at the end of the jump table. */
449 #define elf64_x86_64_hash_entry(ent) \
450 ((struct elf64_x86_64_link_hash_entry *)(ent))
452 struct elf64_x86_64_obj_tdata
454 struct elf_obj_tdata root
;
456 /* tls_type for each local got entry. */
457 char *local_got_tls_type
;
459 /* GOTPLT entries for TLS descriptors. */
460 bfd_vma
*local_tlsdesc_gotent
;
463 #define elf64_x86_64_tdata(abfd) \
464 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
466 #define elf64_x86_64_local_got_tls_type(abfd) \
467 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
469 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
470 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
472 #define is_x86_64_elf(bfd) \
473 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
474 && elf_tdata (bfd) != NULL \
475 && elf_object_id (bfd) == X86_64_ELF_TDATA)
478 elf64_x86_64_mkobject (bfd
*abfd
)
480 return bfd_elf_allocate_object (abfd
, sizeof (struct elf64_x86_64_obj_tdata
),
484 /* x86-64 ELF linker hash table. */
486 struct elf64_x86_64_link_hash_table
488 struct elf_link_hash_table elf
;
490 /* Short-cuts to get to dynamic linker sections. */
503 /* The offset into splt of the PLT entry for the TLS descriptor
504 resolver. Special values are 0, if not necessary (or not found
505 to be necessary yet), and -1 if needed but not determined
508 /* The offset into sgot of the GOT entry used by the PLT entry
513 bfd_signed_vma refcount
;
517 /* The amount of space used by the jump slots in the GOT. */
518 bfd_vma sgotplt_jump_table_size
;
520 /* Small local sym to section mapping cache. */
521 struct sym_sec_cache sym_sec
;
523 /* _TLS_MODULE_BASE_ symbol. */
524 struct bfd_link_hash_entry
*tls_module_base
;
526 /* Used by local STT_GNU_IFUNC symbols. */
527 htab_t loc_hash_table
;
528 void *loc_hash_memory
;
531 /* Get the x86-64 ELF linker hash table from a link_info structure. */
533 #define elf64_x86_64_hash_table(p) \
534 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
536 #define elf64_x86_64_compute_jump_table_size(htab) \
537 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
539 /* Create an entry in an x86-64 ELF linker hash table. */
541 static struct bfd_hash_entry
*
542 elf64_x86_64_link_hash_newfunc (struct bfd_hash_entry
*entry
,
543 struct bfd_hash_table
*table
,
546 /* Allocate the structure if it has not already been allocated by a
550 entry
= bfd_hash_allocate (table
,
551 sizeof (struct elf64_x86_64_link_hash_entry
));
556 /* Call the allocation method of the superclass. */
557 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
560 struct elf64_x86_64_link_hash_entry
*eh
;
562 eh
= (struct elf64_x86_64_link_hash_entry
*) entry
;
563 eh
->dyn_relocs
= NULL
;
564 eh
->tls_type
= GOT_UNKNOWN
;
565 eh
->tlsdesc_got
= (bfd_vma
) -1;
572 elf64_x86_64_local_hash (int id
, int r_sym
)
574 return ((((id
& 0xff) << 24) | ((id
& 0xff00) << 8))
575 ^ r_sym
^ (id
>> 16));
578 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
579 for local symbol so that we can handle local STT_GNU_IFUNC symbols
580 as global symbol. We reuse indx and dynstr_index for local symbol
581 hash since they aren't used by global symbols in this backend. */
584 elf64_x86_64_local_htab_hash (const void *ptr
)
586 struct elf_link_hash_entry
*h
587 = (struct elf_link_hash_entry
*) ptr
;
588 return elf64_x86_64_local_hash (h
->indx
, h
->dynstr_index
);
591 /* Compare local hash entries. */
594 elf64_x86_64_local_htab_eq (const void *ptr1
, const void *ptr2
)
596 struct elf_link_hash_entry
*h1
597 = (struct elf_link_hash_entry
*) ptr1
;
598 struct elf_link_hash_entry
*h2
599 = (struct elf_link_hash_entry
*) ptr2
;
601 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
604 /* Find and/or create a hash entry for local symbol. */
606 static struct elf_link_hash_entry
*
607 elf64_x86_64_get_local_sym_hash (struct elf64_x86_64_link_hash_table
*htab
,
608 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
611 struct elf64_x86_64_link_hash_entry e
, *ret
;
612 asection
*sec
= abfd
->sections
;
613 hashval_t h
= elf64_x86_64_local_hash (sec
->id
,
614 ELF64_R_SYM (rel
->r_info
));
617 e
.elf
.indx
= sec
->id
;
618 e
.elf
.dynstr_index
= ELF64_R_SYM (rel
->r_info
);
619 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &e
, h
,
620 create
? INSERT
: NO_INSERT
);
627 ret
= (struct elf64_x86_64_link_hash_entry
*) *slot
;
631 ret
= (struct elf64_x86_64_link_hash_entry
*)
632 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
633 sizeof (struct elf64_x86_64_link_hash_entry
));
636 memset (ret
, 0, sizeof (*ret
));
637 ret
->elf
.indx
= sec
->id
;
638 ret
->elf
.dynstr_index
= ELF64_R_SYM (rel
->r_info
);
639 ret
->elf
.dynindx
= -1;
640 ret
->elf
.plt
.offset
= (bfd_vma
) -1;
641 ret
->elf
.got
.offset
= (bfd_vma
) -1;
647 /* Create an X86-64 ELF linker hash table. */
649 static struct bfd_link_hash_table
*
650 elf64_x86_64_link_hash_table_create (bfd
*abfd
)
652 struct elf64_x86_64_link_hash_table
*ret
;
653 bfd_size_type amt
= sizeof (struct elf64_x86_64_link_hash_table
);
655 ret
= (struct elf64_x86_64_link_hash_table
*) bfd_malloc (amt
);
659 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
,
660 elf64_x86_64_link_hash_newfunc
,
661 sizeof (struct elf64_x86_64_link_hash_entry
)))
677 ret
->irelifunc
= NULL
;
678 ret
->sym_sec
.abfd
= NULL
;
679 ret
->tlsdesc_plt
= 0;
680 ret
->tlsdesc_got
= 0;
681 ret
->tls_ld_got
.refcount
= 0;
682 ret
->sgotplt_jump_table_size
= 0;
683 ret
->tls_module_base
= NULL
;
685 ret
->loc_hash_table
= htab_try_create (1024,
686 elf64_x86_64_local_htab_hash
,
687 elf64_x86_64_local_htab_eq
,
689 ret
->loc_hash_memory
= objalloc_create ();
690 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
696 return &ret
->elf
.root
;
699 /* Destroy an X86-64 ELF linker hash table. */
702 elf64_x86_64_link_hash_table_free (struct bfd_link_hash_table
*hash
)
704 struct elf64_x86_64_link_hash_table
*htab
705 = (struct elf64_x86_64_link_hash_table
*) hash
;
707 if (htab
->loc_hash_table
)
708 htab_delete (htab
->loc_hash_table
);
709 if (htab
->loc_hash_memory
)
710 objalloc_free ((struct objalloc
*) htab
->loc_hash_memory
);
711 _bfd_generic_link_hash_table_free (hash
);
714 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
715 shortcuts to them in our hash table. */
718 elf64_x86_64_create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
720 struct elf64_x86_64_link_hash_table
*htab
;
722 if (! _bfd_elf_create_got_section (dynobj
, info
))
725 htab
= elf64_x86_64_hash_table (info
);
726 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
727 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
728 if (!htab
->sgot
|| !htab
->sgotplt
)
731 htab
->srelgot
= bfd_make_section_with_flags (dynobj
, ".rela.got",
732 (SEC_ALLOC
| SEC_LOAD
737 if (htab
->srelgot
== NULL
738 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 3))
743 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
744 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
748 elf64_x86_64_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
750 struct elf64_x86_64_link_hash_table
*htab
;
752 htab
= elf64_x86_64_hash_table (info
);
753 if (!htab
->sgot
&& !elf64_x86_64_create_got_section (dynobj
, info
))
756 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
759 htab
->splt
= bfd_get_section_by_name (dynobj
, ".plt");
760 htab
->srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
761 htab
->sdynbss
= bfd_get_section_by_name (dynobj
, ".dynbss");
763 htab
->srelbss
= bfd_get_section_by_name (dynobj
, ".rela.bss");
765 if (!htab
->splt
|| !htab
->srelplt
|| !htab
->sdynbss
766 || (!info
->shared
&& !htab
->srelbss
))
772 /* Copy the extra info we tack onto an elf_link_hash_entry. */
775 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info
*info
,
776 struct elf_link_hash_entry
*dir
,
777 struct elf_link_hash_entry
*ind
)
779 struct elf64_x86_64_link_hash_entry
*edir
, *eind
;
781 edir
= (struct elf64_x86_64_link_hash_entry
*) dir
;
782 eind
= (struct elf64_x86_64_link_hash_entry
*) ind
;
784 if (eind
->dyn_relocs
!= NULL
)
786 if (edir
->dyn_relocs
!= NULL
)
788 struct elf64_x86_64_dyn_relocs
**pp
;
789 struct elf64_x86_64_dyn_relocs
*p
;
791 /* Add reloc counts against the indirect sym to the direct sym
792 list. Merge any entries against the same section. */
793 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
795 struct elf64_x86_64_dyn_relocs
*q
;
797 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
798 if (q
->sec
== p
->sec
)
800 q
->pc_count
+= p
->pc_count
;
801 q
->count
+= p
->count
;
808 *pp
= edir
->dyn_relocs
;
811 edir
->dyn_relocs
= eind
->dyn_relocs
;
812 eind
->dyn_relocs
= NULL
;
815 if (ind
->root
.type
== bfd_link_hash_indirect
816 && dir
->got
.refcount
<= 0)
818 edir
->tls_type
= eind
->tls_type
;
819 eind
->tls_type
= GOT_UNKNOWN
;
822 if (ELIMINATE_COPY_RELOCS
823 && ind
->root
.type
!= bfd_link_hash_indirect
824 && dir
->dynamic_adjusted
)
826 /* If called to transfer flags for a weakdef during processing
827 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
828 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
829 dir
->ref_dynamic
|= ind
->ref_dynamic
;
830 dir
->ref_regular
|= ind
->ref_regular
;
831 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
832 dir
->needs_plt
|= ind
->needs_plt
;
833 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
836 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
840 elf64_x86_64_elf_object_p (bfd
*abfd
)
842 /* Set the right machine number for an x86-64 elf64 file. */
843 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x86_64
);
861 /* Return TRUE if the TLS access code sequence support transition
865 elf64_x86_64_check_tls_transition (bfd
*abfd
, asection
*sec
,
867 Elf_Internal_Shdr
*symtab_hdr
,
868 struct elf_link_hash_entry
**sym_hashes
,
870 const Elf_Internal_Rela
*rel
,
871 const Elf_Internal_Rela
*relend
)
874 unsigned long r_symndx
;
875 struct elf_link_hash_entry
*h
;
878 /* Get the section contents. */
879 if (contents
== NULL
)
881 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
882 contents
= elf_section_data (sec
)->this_hdr
.contents
;
885 /* FIXME: How to better handle error condition? */
886 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
889 /* Cache the section contents for elf_link_input_bfd. */
890 elf_section_data (sec
)->this_hdr
.contents
= contents
;
894 offset
= rel
->r_offset
;
899 if ((rel
+ 1) >= relend
)
902 if (r_type
== R_X86_64_TLSGD
)
904 /* Check transition from GD access model. Only
905 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
906 .word 0x6666; rex64; call __tls_get_addr
907 can transit to different access model. */
909 static x86_64_opcode32 leaq
= { { 0x66, 0x48, 0x8d, 0x3d } },
910 call
= { { 0x66, 0x66, 0x48, 0xe8 } };
912 || (offset
+ 12) > sec
->size
913 || bfd_get_32 (abfd
, contents
+ offset
- 4) != leaq
.i
914 || bfd_get_32 (abfd
, contents
+ offset
+ 4) != call
.i
)
919 /* Check transition from LD access model. Only
920 leaq foo@tlsld(%rip), %rdi;
922 can transit to different access model. */
924 static x86_64_opcode32 ld
= { { 0x48, 0x8d, 0x3d, 0xe8 } };
927 if (offset
< 3 || (offset
+ 9) > sec
->size
)
930 op
.i
= bfd_get_32 (abfd
, contents
+ offset
- 3);
931 op
.c
[3] = bfd_get_8 (abfd
, contents
+ offset
+ 4);
936 r_symndx
= ELF64_R_SYM (rel
[1].r_info
);
937 if (r_symndx
< symtab_hdr
->sh_info
)
940 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
941 /* Use strncmp to check __tls_get_addr since __tls_get_addr
944 && h
->root
.root
.string
!= NULL
945 && (ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PC32
946 || ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PLT32
)
947 && (strncmp (h
->root
.root
.string
,
948 "__tls_get_addr", 14) == 0));
950 case R_X86_64_GOTTPOFF
:
951 /* Check transition from IE access model:
952 movq foo@gottpoff(%rip), %reg
953 addq foo@gottpoff(%rip), %reg
956 if (offset
< 3 || (offset
+ 4) > sec
->size
)
959 val
= bfd_get_8 (abfd
, contents
+ offset
- 3);
960 if (val
!= 0x48 && val
!= 0x4c)
963 val
= bfd_get_8 (abfd
, contents
+ offset
- 2);
964 if (val
!= 0x8b && val
!= 0x03)
967 val
= bfd_get_8 (abfd
, contents
+ offset
- 1);
968 return (val
& 0xc7) == 5;
970 case R_X86_64_GOTPC32_TLSDESC
:
971 /* Check transition from GDesc access model:
972 leaq x@tlsdesc(%rip), %rax
974 Make sure it's a leaq adding rip to a 32-bit offset
975 into any register, although it's probably almost always
978 if (offset
< 3 || (offset
+ 4) > sec
->size
)
981 val
= bfd_get_8 (abfd
, contents
+ offset
- 3);
982 if ((val
& 0xfb) != 0x48)
985 if (bfd_get_8 (abfd
, contents
+ offset
- 2) != 0x8d)
988 val
= bfd_get_8 (abfd
, contents
+ offset
- 1);
989 return (val
& 0xc7) == 0x05;
991 case R_X86_64_TLSDESC_CALL
:
992 /* Check transition from GDesc access model:
993 call *x@tlsdesc(%rax)
995 if (offset
+ 2 <= sec
->size
)
997 /* Make sure that it's a call *x@tlsdesc(%rax). */
998 static x86_64_opcode16 call
= { { 0xff, 0x10 } };
999 return bfd_get_16 (abfd
, contents
+ offset
) == call
.i
;
1009 /* Return TRUE if the TLS access transition is OK or no transition
1010 will be performed. Update R_TYPE if there is a transition. */
1013 elf64_x86_64_tls_transition (struct bfd_link_info
*info
, bfd
*abfd
,
1014 asection
*sec
, bfd_byte
*contents
,
1015 Elf_Internal_Shdr
*symtab_hdr
,
1016 struct elf_link_hash_entry
**sym_hashes
,
1017 unsigned int *r_type
, int tls_type
,
1018 const Elf_Internal_Rela
*rel
,
1019 const Elf_Internal_Rela
*relend
,
1020 struct elf_link_hash_entry
*h
)
1022 unsigned int from_type
= *r_type
;
1023 unsigned int to_type
= from_type
;
1024 bfd_boolean check
= TRUE
;
1028 case R_X86_64_TLSGD
:
1029 case R_X86_64_GOTPC32_TLSDESC
:
1030 case R_X86_64_TLSDESC_CALL
:
1031 case R_X86_64_GOTTPOFF
:
1035 to_type
= R_X86_64_TPOFF32
;
1037 to_type
= R_X86_64_GOTTPOFF
;
1040 /* When we are called from elf64_x86_64_relocate_section,
1041 CONTENTS isn't NULL and there may be additional transitions
1042 based on TLS_TYPE. */
1043 if (contents
!= NULL
)
1045 unsigned int new_to_type
= to_type
;
1050 && tls_type
== GOT_TLS_IE
)
1051 new_to_type
= R_X86_64_TPOFF32
;
1053 if (to_type
== R_X86_64_TLSGD
1054 || to_type
== R_X86_64_GOTPC32_TLSDESC
1055 || to_type
== R_X86_64_TLSDESC_CALL
)
1057 if (tls_type
== GOT_TLS_IE
)
1058 new_to_type
= R_X86_64_GOTTPOFF
;
1061 /* We checked the transition before when we were called from
1062 elf64_x86_64_check_relocs. We only want to check the new
1063 transition which hasn't been checked before. */
1064 check
= new_to_type
!= to_type
&& from_type
== to_type
;
1065 to_type
= new_to_type
;
1070 case R_X86_64_TLSLD
:
1072 to_type
= R_X86_64_TPOFF32
;
1079 /* Return TRUE if there is no transition. */
1080 if (from_type
== to_type
)
1083 /* Check if the transition can be performed. */
1085 && ! elf64_x86_64_check_tls_transition (abfd
, sec
, contents
,
1086 symtab_hdr
, sym_hashes
,
1087 from_type
, rel
, relend
))
1089 reloc_howto_type
*from
, *to
;
1091 from
= elf64_x86_64_rtype_to_howto (abfd
, from_type
);
1092 to
= elf64_x86_64_rtype_to_howto (abfd
, to_type
);
1094 (*_bfd_error_handler
)
1095 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1096 "in section `%A' failed"),
1097 abfd
, sec
, from
->name
, to
->name
,
1098 h
? h
->root
.root
.string
: "a local symbol",
1099 (unsigned long) rel
->r_offset
);
1100 bfd_set_error (bfd_error_bad_value
);
1108 /* Look through the relocs for a section during the first phase, and
1109 calculate needed space in the global offset table, procedure
1110 linkage table, and dynamic reloc sections. */
1113 elf64_x86_64_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
1115 const Elf_Internal_Rela
*relocs
)
1117 struct elf64_x86_64_link_hash_table
*htab
;
1118 Elf_Internal_Shdr
*symtab_hdr
;
1119 struct elf_link_hash_entry
**sym_hashes
;
1120 const Elf_Internal_Rela
*rel
;
1121 const Elf_Internal_Rela
*rel_end
;
1123 Elf_Internal_Sym
*isymbuf
;
1125 if (info
->relocatable
)
1128 BFD_ASSERT (is_x86_64_elf (abfd
));
1130 htab
= elf64_x86_64_hash_table (info
);
1131 symtab_hdr
= &elf_symtab_hdr (abfd
);
1132 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1133 sym_hashes
= elf_sym_hashes (abfd
);
1137 rel_end
= relocs
+ sec
->reloc_count
;
1138 for (rel
= relocs
; rel
< rel_end
; rel
++)
1140 unsigned int r_type
;
1141 unsigned long r_symndx
;
1142 struct elf_link_hash_entry
*h
;
1144 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1145 r_type
= ELF64_R_TYPE (rel
->r_info
);
1147 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
1149 (*_bfd_error_handler
) (_("%B: bad symbol index: %d"),
1154 if (r_symndx
< symtab_hdr
->sh_info
)
1156 /* A local symbol. */
1157 Elf_Internal_Sym
*isym
;
1159 /* Read this BFD's local symbols. */
1160 if (isymbuf
== NULL
)
1162 if (isymbuf
== NULL
)
1163 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1164 symtab_hdr
->sh_info
, 0,
1166 if (isymbuf
== NULL
)
1170 /* Check relocation against local STT_GNU_IFUNC symbol. */
1171 isym
= isymbuf
+ r_symndx
;
1172 if (ELF64_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
1174 h
= elf64_x86_64_get_local_sym_hash (htab
, abfd
, rel
,
1179 /* Fake a STT_GNU_IFUNC symbol. */
1180 h
->type
= STT_GNU_IFUNC
;
1183 h
->forced_local
= 1;
1184 h
->root
.type
= bfd_link_hash_defined
;
1191 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1192 while (h
->root
.type
== bfd_link_hash_indirect
1193 || h
->root
.type
== bfd_link_hash_warning
)
1194 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1199 /* Create the ifunc sections for static executables. If we
1200 never see an indirect function symbol nor we are building
1201 a static executable, those sections will be empty and
1202 won't appear in output. */
1213 case R_X86_64_PLT32
:
1214 case R_X86_64_GOTPCREL
:
1215 case R_X86_64_GOTPCREL64
:
1216 if (htab
->irelifunc
== NULL
&& htab
->iplt
== NULL
)
1218 if (!_bfd_elf_create_ifunc_sections (abfd
, info
))
1223 htab
->irelifunc
= bfd_get_section_by_name (abfd
,
1225 if (!htab
->irelifunc
)
1230 htab
->iplt
= bfd_get_section_by_name (abfd
, ".iplt");
1231 htab
->irelplt
= bfd_get_section_by_name (abfd
,
1233 htab
->igotplt
= bfd_get_section_by_name (abfd
,
1244 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1245 it here if it is defined in a non-shared object. */
1246 if (h
->type
== STT_GNU_IFUNC
1249 /* It is referenced by a non-shared object. */
1252 /* STT_GNU_IFUNC symbol must go through PLT. */
1253 h
->plt
.refcount
+= 1;
1255 /* STT_GNU_IFUNC needs dynamic sections. */
1256 if (htab
->elf
.dynobj
== NULL
)
1257 htab
->elf
.dynobj
= abfd
;
1262 (*_bfd_error_handler
)
1263 (_("%B: relocation %s against STT_GNU_IFUNC "
1264 "symbol `%s' isn't handled by %s"), abfd
,
1265 x86_64_elf_howto_table
[r_type
].name
,
1266 (h
->root
.root
.string
1267 ? h
->root
.root
.string
: "a local symbol"),
1269 bfd_set_error (bfd_error_bad_value
);
1274 h
->pointer_equality_needed
= 1;
1277 struct elf64_x86_64_dyn_relocs
*p
;
1278 struct elf64_x86_64_dyn_relocs
**head
;
1280 /* We must copy these reloc types into the output
1281 file. Create a reloc section in dynobj and
1282 make room for this reloc. */
1285 if (htab
->elf
.dynobj
== NULL
)
1286 htab
->elf
.dynobj
= abfd
;
1288 sreloc
= _bfd_elf_make_dynamic_reloc_section
1289 (sec
, htab
->elf
.dynobj
, 3, abfd
, TRUE
);
1295 head
= &((struct elf64_x86_64_link_hash_entry
*) h
)->dyn_relocs
;
1297 if (p
== NULL
|| p
->sec
!= sec
)
1299 bfd_size_type amt
= sizeof *p
;
1301 p
= ((struct elf64_x86_64_dyn_relocs
*)
1302 bfd_alloc (htab
->elf
.dynobj
, amt
));
1320 if (r_type
!= R_X86_64_PC32
1321 && r_type
!= R_X86_64_PC64
)
1322 h
->pointer_equality_needed
= 1;
1325 case R_X86_64_PLT32
:
1328 case R_X86_64_GOTPCREL
:
1329 case R_X86_64_GOTPCREL64
:
1330 h
->got
.refcount
+= 1;
1331 if (htab
->sgot
== NULL
1332 && !elf64_x86_64_create_got_section (htab
->elf
.dynobj
,
1342 if (! elf64_x86_64_tls_transition (info
, abfd
, sec
, NULL
,
1343 symtab_hdr
, sym_hashes
,
1344 &r_type
, GOT_UNKNOWN
,
1350 case R_X86_64_TLSLD
:
1351 htab
->tls_ld_got
.refcount
+= 1;
1354 case R_X86_64_TPOFF32
:
1357 (*_bfd_error_handler
)
1358 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1360 x86_64_elf_howto_table
[r_type
].name
,
1361 (h
) ? h
->root
.root
.string
: "a local symbol");
1362 bfd_set_error (bfd_error_bad_value
);
1367 case R_X86_64_GOTTPOFF
:
1369 info
->flags
|= DF_STATIC_TLS
;
1372 case R_X86_64_GOT32
:
1373 case R_X86_64_GOTPCREL
:
1374 case R_X86_64_TLSGD
:
1375 case R_X86_64_GOT64
:
1376 case R_X86_64_GOTPCREL64
:
1377 case R_X86_64_GOTPLT64
:
1378 case R_X86_64_GOTPC32_TLSDESC
:
1379 case R_X86_64_TLSDESC_CALL
:
1380 /* This symbol requires a global offset table entry. */
1382 int tls_type
, old_tls_type
;
1386 default: tls_type
= GOT_NORMAL
; break;
1387 case R_X86_64_TLSGD
: tls_type
= GOT_TLS_GD
; break;
1388 case R_X86_64_GOTTPOFF
: tls_type
= GOT_TLS_IE
; break;
1389 case R_X86_64_GOTPC32_TLSDESC
:
1390 case R_X86_64_TLSDESC_CALL
:
1391 tls_type
= GOT_TLS_GDESC
; break;
1396 if (r_type
== R_X86_64_GOTPLT64
)
1398 /* This relocation indicates that we also need
1399 a PLT entry, as this is a function. We don't need
1400 a PLT entry for local symbols. */
1402 h
->plt
.refcount
+= 1;
1404 h
->got
.refcount
+= 1;
1405 old_tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
1409 bfd_signed_vma
*local_got_refcounts
;
1411 /* This is a global offset table entry for a local symbol. */
1412 local_got_refcounts
= elf_local_got_refcounts (abfd
);
1413 if (local_got_refcounts
== NULL
)
1417 size
= symtab_hdr
->sh_info
;
1418 size
*= sizeof (bfd_signed_vma
)
1419 + sizeof (bfd_vma
) + sizeof (char);
1420 local_got_refcounts
= ((bfd_signed_vma
*)
1421 bfd_zalloc (abfd
, size
));
1422 if (local_got_refcounts
== NULL
)
1424 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
1425 elf64_x86_64_local_tlsdesc_gotent (abfd
)
1426 = (bfd_vma
*) (local_got_refcounts
+ symtab_hdr
->sh_info
);
1427 elf64_x86_64_local_got_tls_type (abfd
)
1428 = (char *) (local_got_refcounts
+ 2 * symtab_hdr
->sh_info
);
1430 local_got_refcounts
[r_symndx
] += 1;
1432 = elf64_x86_64_local_got_tls_type (abfd
) [r_symndx
];
1435 /* If a TLS symbol is accessed using IE at least once,
1436 there is no point to use dynamic model for it. */
1437 if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
1438 && (! GOT_TLS_GD_ANY_P (old_tls_type
)
1439 || tls_type
!= GOT_TLS_IE
))
1441 if (old_tls_type
== GOT_TLS_IE
&& GOT_TLS_GD_ANY_P (tls_type
))
1442 tls_type
= old_tls_type
;
1443 else if (GOT_TLS_GD_ANY_P (old_tls_type
)
1444 && GOT_TLS_GD_ANY_P (tls_type
))
1445 tls_type
|= old_tls_type
;
1448 (*_bfd_error_handler
)
1449 (_("%B: '%s' accessed both as normal and thread local symbol"),
1450 abfd
, h
? h
->root
.root
.string
: "<local>");
1455 if (old_tls_type
!= tls_type
)
1458 elf64_x86_64_hash_entry (h
)->tls_type
= tls_type
;
1460 elf64_x86_64_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
1465 case R_X86_64_GOTOFF64
:
1466 case R_X86_64_GOTPC32
:
1467 case R_X86_64_GOTPC64
:
1469 if (htab
->sgot
== NULL
)
1471 if (htab
->elf
.dynobj
== NULL
)
1472 htab
->elf
.dynobj
= abfd
;
1473 if (!elf64_x86_64_create_got_section (htab
->elf
.dynobj
,
1479 case R_X86_64_PLT32
:
1480 /* This symbol requires a procedure linkage table entry. We
1481 actually build the entry in adjust_dynamic_symbol,
1482 because this might be a case of linking PIC code which is
1483 never referenced by a dynamic object, in which case we
1484 don't need to generate a procedure linkage table entry
1487 /* If this is a local symbol, we resolve it directly without
1488 creating a procedure linkage table entry. */
1493 h
->plt
.refcount
+= 1;
1496 case R_X86_64_PLTOFF64
:
1497 /* This tries to form the 'address' of a function relative
1498 to GOT. For global symbols we need a PLT entry. */
1502 h
->plt
.refcount
+= 1;
1510 /* Let's help debug shared library creation. These relocs
1511 cannot be used in shared libs. Don't error out for
1512 sections we don't care about, such as debug sections or
1513 non-constant sections. */
1515 && (sec
->flags
& SEC_ALLOC
) != 0
1516 && (sec
->flags
& SEC_READONLY
) != 0)
1518 (*_bfd_error_handler
)
1519 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1521 x86_64_elf_howto_table
[r_type
].name
,
1522 (h
) ? h
->root
.root
.string
: "a local symbol");
1523 bfd_set_error (bfd_error_bad_value
);
1533 if (h
!= NULL
&& info
->executable
)
1535 /* If this reloc is in a read-only section, we might
1536 need a copy reloc. We can't check reliably at this
1537 stage whether the section is read-only, as input
1538 sections have not yet been mapped to output sections.
1539 Tentatively set the flag for now, and correct in
1540 adjust_dynamic_symbol. */
1543 /* We may need a .plt entry if the function this reloc
1544 refers to is in a shared lib. */
1545 h
->plt
.refcount
+= 1;
1546 if (r_type
!= R_X86_64_PC32
&& r_type
!= R_X86_64_PC64
)
1547 h
->pointer_equality_needed
= 1;
1550 /* If we are creating a shared library, and this is a reloc
1551 against a global symbol, or a non PC relative reloc
1552 against a local symbol, then we need to copy the reloc
1553 into the shared library. However, if we are linking with
1554 -Bsymbolic, we do not need to copy a reloc against a
1555 global symbol which is defined in an object we are
1556 including in the link (i.e., DEF_REGULAR is set). At
1557 this point we have not seen all the input files, so it is
1558 possible that DEF_REGULAR is not set now but will be set
1559 later (it is never cleared). In case of a weak definition,
1560 DEF_REGULAR may be cleared later by a strong definition in
1561 a shared library. We account for that possibility below by
1562 storing information in the relocs_copied field of the hash
1563 table entry. A similar situation occurs when creating
1564 shared libraries and symbol visibility changes render the
1567 If on the other hand, we are creating an executable, we
1568 may need to keep relocations for symbols satisfied by a
1569 dynamic library if we manage to avoid copy relocs for the
1572 && (sec
->flags
& SEC_ALLOC
) != 0
1573 && (! IS_X86_64_PCREL_TYPE (r_type
)
1575 && (! SYMBOLIC_BIND (info
, h
)
1576 || h
->root
.type
== bfd_link_hash_defweak
1577 || !h
->def_regular
))))
1578 || (ELIMINATE_COPY_RELOCS
1580 && (sec
->flags
& SEC_ALLOC
) != 0
1582 && (h
->root
.type
== bfd_link_hash_defweak
1583 || !h
->def_regular
)))
1585 struct elf64_x86_64_dyn_relocs
*p
;
1586 struct elf64_x86_64_dyn_relocs
**head
;
1588 /* We must copy these reloc types into the output file.
1589 Create a reloc section in dynobj and make room for
1593 if (htab
->elf
.dynobj
== NULL
)
1594 htab
->elf
.dynobj
= abfd
;
1596 sreloc
= _bfd_elf_make_dynamic_reloc_section
1597 (sec
, htab
->elf
.dynobj
, 3, abfd
, /*rela?*/ TRUE
);
1603 /* If this is a global symbol, we count the number of
1604 relocations we need for this symbol. */
1607 head
= &((struct elf64_x86_64_link_hash_entry
*) h
)->dyn_relocs
;
1612 /* Track dynamic relocs needed for local syms too.
1613 We really need local syms available to do this
1617 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
1622 /* Beware of type punned pointers vs strict aliasing
1624 vpp
= &(elf_section_data (s
)->local_dynrel
);
1625 head
= (struct elf64_x86_64_dyn_relocs
**)vpp
;
1629 if (p
== NULL
|| p
->sec
!= sec
)
1631 bfd_size_type amt
= sizeof *p
;
1633 p
= ((struct elf64_x86_64_dyn_relocs
*)
1634 bfd_alloc (htab
->elf
.dynobj
, amt
));
1645 if (IS_X86_64_PCREL_TYPE (r_type
))
1650 /* This relocation describes the C++ object vtable hierarchy.
1651 Reconstruct it for later use during GC. */
1652 case R_X86_64_GNU_VTINHERIT
:
1653 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
1657 /* This relocation describes which C++ vtable entries are actually
1658 used. Record for later use during GC. */
1659 case R_X86_64_GNU_VTENTRY
:
1660 BFD_ASSERT (h
!= NULL
);
1662 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
1674 /* Return the section that should be marked against GC for a given
1678 elf64_x86_64_gc_mark_hook (asection
*sec
,
1679 struct bfd_link_info
*info
,
1680 Elf_Internal_Rela
*rel
,
1681 struct elf_link_hash_entry
*h
,
1682 Elf_Internal_Sym
*sym
)
1685 switch (ELF64_R_TYPE (rel
->r_info
))
1687 case R_X86_64_GNU_VTINHERIT
:
1688 case R_X86_64_GNU_VTENTRY
:
1692 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
1695 /* Update the got entry reference counts for the section being removed. */
1698 elf64_x86_64_gc_sweep_hook (bfd
*abfd
, struct bfd_link_info
*info
,
1700 const Elf_Internal_Rela
*relocs
)
1702 Elf_Internal_Shdr
*symtab_hdr
;
1703 struct elf_link_hash_entry
**sym_hashes
;
1704 bfd_signed_vma
*local_got_refcounts
;
1705 const Elf_Internal_Rela
*rel
, *relend
;
1707 if (info
->relocatable
)
1710 elf_section_data (sec
)->local_dynrel
= NULL
;
1712 symtab_hdr
= &elf_symtab_hdr (abfd
);
1713 sym_hashes
= elf_sym_hashes (abfd
);
1714 local_got_refcounts
= elf_local_got_refcounts (abfd
);
1716 relend
= relocs
+ sec
->reloc_count
;
1717 for (rel
= relocs
; rel
< relend
; rel
++)
1719 unsigned long r_symndx
;
1720 unsigned int r_type
;
1721 struct elf_link_hash_entry
*h
= NULL
;
1723 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1724 if (r_symndx
>= symtab_hdr
->sh_info
)
1726 struct elf64_x86_64_link_hash_entry
*eh
;
1727 struct elf64_x86_64_dyn_relocs
**pp
;
1728 struct elf64_x86_64_dyn_relocs
*p
;
1730 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1731 while (h
->root
.type
== bfd_link_hash_indirect
1732 || h
->root
.type
== bfd_link_hash_warning
)
1733 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1734 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1736 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
1739 /* Everything must go for SEC. */
1745 r_type
= ELF64_R_TYPE (rel
->r_info
);
1746 if (! elf64_x86_64_tls_transition (info
, abfd
, sec
, NULL
,
1747 symtab_hdr
, sym_hashes
,
1748 &r_type
, GOT_UNKNOWN
,
1754 case R_X86_64_TLSLD
:
1755 if (elf64_x86_64_hash_table (info
)->tls_ld_got
.refcount
> 0)
1756 elf64_x86_64_hash_table (info
)->tls_ld_got
.refcount
-= 1;
1759 case R_X86_64_TLSGD
:
1760 case R_X86_64_GOTPC32_TLSDESC
:
1761 case R_X86_64_TLSDESC_CALL
:
1762 case R_X86_64_GOTTPOFF
:
1763 case R_X86_64_GOT32
:
1764 case R_X86_64_GOTPCREL
:
1765 case R_X86_64_GOT64
:
1766 case R_X86_64_GOTPCREL64
:
1767 case R_X86_64_GOTPLT64
:
1770 if (r_type
== R_X86_64_GOTPLT64
&& h
->plt
.refcount
> 0)
1771 h
->plt
.refcount
-= 1;
1772 if (h
->got
.refcount
> 0)
1773 h
->got
.refcount
-= 1;
1775 else if (local_got_refcounts
!= NULL
)
1777 if (local_got_refcounts
[r_symndx
] > 0)
1778 local_got_refcounts
[r_symndx
] -= 1;
1795 case R_X86_64_PLT32
:
1796 case R_X86_64_PLTOFF64
:
1799 if (h
->plt
.refcount
> 0)
1800 h
->plt
.refcount
-= 1;
1812 /* Adjust a symbol defined by a dynamic object and referenced by a
1813 regular object. The current definition is in some section of the
1814 dynamic object, but we're not including those sections. We have to
1815 change the definition to something the rest of the link can
1819 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info
*info
,
1820 struct elf_link_hash_entry
*h
)
1822 struct elf64_x86_64_link_hash_table
*htab
;
1825 /* STT_GNU_IFUNC symbol must go through PLT. */
1826 if (h
->type
== STT_GNU_IFUNC
)
1828 if (h
->plt
.refcount
<= 0)
1830 h
->plt
.offset
= (bfd_vma
) -1;
1836 /* If this is a function, put it in the procedure linkage table. We
1837 will fill in the contents of the procedure linkage table later,
1838 when we know the address of the .got section. */
1839 if (h
->type
== STT_FUNC
1842 if (h
->plt
.refcount
<= 0
1843 || SYMBOL_CALLS_LOCAL (info
, h
)
1844 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1845 && h
->root
.type
== bfd_link_hash_undefweak
))
1847 /* This case can occur if we saw a PLT32 reloc in an input
1848 file, but the symbol was never referred to by a dynamic
1849 object, or if all references were garbage collected. In
1850 such a case, we don't actually need to build a procedure
1851 linkage table, and we can just do a PC32 reloc instead. */
1852 h
->plt
.offset
= (bfd_vma
) -1;
1859 /* It's possible that we incorrectly decided a .plt reloc was
1860 needed for an R_X86_64_PC32 reloc to a non-function sym in
1861 check_relocs. We can't decide accurately between function and
1862 non-function syms in check-relocs; Objects loaded later in
1863 the link may change h->type. So fix it now. */
1864 h
->plt
.offset
= (bfd_vma
) -1;
1866 /* If this is a weak symbol, and there is a real definition, the
1867 processor independent code will have arranged for us to see the
1868 real definition first, and we can just use the same value. */
1869 if (h
->u
.weakdef
!= NULL
)
1871 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
1872 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
1873 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
1874 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
1875 if (ELIMINATE_COPY_RELOCS
|| info
->nocopyreloc
)
1876 h
->non_got_ref
= h
->u
.weakdef
->non_got_ref
;
1880 /* This is a reference to a symbol defined by a dynamic object which
1881 is not a function. */
1883 /* If we are creating a shared library, we must presume that the
1884 only references to the symbol are via the global offset table.
1885 For such cases we need not do anything here; the relocations will
1886 be handled correctly by relocate_section. */
1890 /* If there are no references to this symbol that do not use the
1891 GOT, we don't need to generate a copy reloc. */
1892 if (!h
->non_got_ref
)
1895 /* If -z nocopyreloc was given, we won't generate them either. */
1896 if (info
->nocopyreloc
)
1902 if (ELIMINATE_COPY_RELOCS
)
1904 struct elf64_x86_64_link_hash_entry
* eh
;
1905 struct elf64_x86_64_dyn_relocs
*p
;
1907 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1908 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1910 s
= p
->sec
->output_section
;
1911 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1915 /* If we didn't find any dynamic relocs in read-only sections, then
1916 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1926 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
1927 h
->root
.root
.string
);
1931 /* We must allocate the symbol in our .dynbss section, which will
1932 become part of the .bss section of the executable. There will be
1933 an entry for this symbol in the .dynsym section. The dynamic
1934 object will contain position independent code, so all references
1935 from the dynamic object to this symbol will go through the global
1936 offset table. The dynamic linker will use the .dynsym entry to
1937 determine the address it must put in the global offset table, so
1938 both the dynamic object and the regular object will refer to the
1939 same memory location for the variable. */
1941 htab
= elf64_x86_64_hash_table (info
);
1943 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1944 to copy the initial value out of the dynamic object and into the
1945 runtime process image. */
1946 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
1948 htab
->srelbss
->size
+= sizeof (Elf64_External_Rela
);
1954 return _bfd_elf_adjust_dynamic_copy (h
, s
);
1957 /* Allocate space in .plt, .got and associated reloc sections for
1961 elf64_x86_64_allocate_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
1963 struct bfd_link_info
*info
;
1964 struct elf64_x86_64_link_hash_table
*htab
;
1965 struct elf64_x86_64_link_hash_entry
*eh
;
1966 struct elf64_x86_64_dyn_relocs
*p
;
1968 if (h
->root
.type
== bfd_link_hash_indirect
)
1971 if (h
->root
.type
== bfd_link_hash_warning
)
1972 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1973 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1975 info
= (struct bfd_link_info
*) inf
;
1976 htab
= elf64_x86_64_hash_table (info
);
1978 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1979 here if it is defined and referenced in a non-shared object. */
1980 if (h
->type
== STT_GNU_IFUNC
1983 asection
*plt
, *gotplt
, *relplt
;
1985 /* When a shared library references a STT_GNU_IFUNC symbol
1986 defined in executable, the address of the resolved function
1987 may be used. But in non-shared executable, the address of
1988 its .plt slot may be used. Pointer equality may not work
1989 correctly. PIE should be used if pointer equality is
1992 && (h
->dynindx
!= -1
1993 || info
->export_dynamic
)
1994 && h
->pointer_equality_needed
)
1996 info
->callbacks
->einfo
1997 (_("%F%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer "
1998 "equality in `%B' can not be used when making an "
1999 "executable; recompile with -fPIE and relink with -pie\n"),
2000 h
->root
.root
.string
,
2001 h
->root
.u
.def
.section
->owner
);
2002 bfd_set_error (bfd_error_bad_value
);
2006 /* Return and discard space for dynamic relocations against it if
2007 it is never referenced in a non-shared object. */
2008 if (!h
->ref_regular
)
2010 if (h
->plt
.refcount
> 0
2011 || h
->got
.refcount
> 0)
2013 h
->got
.offset
= (bfd_vma
) -1;
2014 eh
->dyn_relocs
= NULL
;
2018 /* When building a static executable, use .iplt, .igot.plt and
2019 .rela.iplt sections for STT_GNU_IFUNC symbols. */
2020 if (htab
->splt
!= NULL
)
2023 gotplt
= htab
->sgotplt
;
2024 relplt
= htab
->srelplt
;
2026 /* If this is the first .plt entry, make room for the special
2029 plt
->size
+= PLT_ENTRY_SIZE
;
2034 gotplt
= htab
->igotplt
;
2035 relplt
= htab
->irelplt
;
2038 /* Don't update value of STT_GNU_IFUNC symbol to PLT. We need
2039 the original value for R_X86_64_IRELATIVE. */
2040 h
->plt
.offset
= plt
->size
;
2042 /* Make room for this entry in the .plt/.iplt section. */
2043 plt
->size
+= PLT_ENTRY_SIZE
;
2045 /* We also need to make an entry in the .got.plt/.got.iplt
2046 section, which will be placed in the .got section by the
2048 gotplt
->size
+= GOT_ENTRY_SIZE
;
2050 /* We also need to make an entry in the .rela.plt/.rela.iplt
2052 relplt
->size
+= sizeof (Elf64_External_Rela
);
2053 relplt
->reloc_count
++;
2055 /* We need dynamic relocation for STT_GNU_IFUNC symbol only
2056 when there is a non-GOT reference in a shared object. */
2059 eh
->dyn_relocs
= NULL
;
2061 /* Finally, allocate space. */
2062 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2063 htab
->irelifunc
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
2065 /* For STT_GNU_IFUNC symbol, .got.plt has the real function
2066 addres and .got has the PLT entry adddress. We will load
2067 the GOT entry with the PLT entry in finish_dynamic_symbol if
2068 it is used. For branch, it uses .got.plt. For symbol value,
2069 1. Use .got.plt in a shared object if it is forced local or
2071 2. Use .got.plt in a non-shared object if pointer equality
2073 3. Use .got.plt in PIE.
2074 4. Use .got.plt if .got isn't used.
2075 5. Otherwise use .got so that it can be shared among different
2076 objects at run-time.
2077 We only need to relocate .got entry in shared object. */
2079 && (h
->dynindx
== -1
2080 || h
->forced_local
))
2082 && !h
->pointer_equality_needed
)
2083 || (info
->executable
&& info
->shared
)
2084 || htab
->sgot
== NULL
)
2087 h
->got
.offset
= (bfd_vma
) -1;
2091 h
->got
.offset
= htab
->sgot
->size
;
2092 htab
->sgot
->size
+= GOT_ENTRY_SIZE
;
2094 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
2099 else if (htab
->elf
.dynamic_sections_created
2100 && h
->plt
.refcount
> 0)
2102 /* Make sure this symbol is output as a dynamic symbol.
2103 Undefined weak syms won't yet be marked as dynamic. */
2104 if (h
->dynindx
== -1
2105 && !h
->forced_local
)
2107 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2112 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
2114 asection
*s
= htab
->splt
;
2116 /* If this is the first .plt entry, make room for the special
2119 s
->size
+= PLT_ENTRY_SIZE
;
2121 h
->plt
.offset
= s
->size
;
2123 /* If this symbol is not defined in a regular file, and we are
2124 not generating a shared library, then set the symbol to this
2125 location in the .plt. This is required to make function
2126 pointers compare as equal between the normal executable and
2127 the shared library. */
2131 h
->root
.u
.def
.section
= s
;
2132 h
->root
.u
.def
.value
= h
->plt
.offset
;
2135 /* Make room for this entry. */
2136 s
->size
+= PLT_ENTRY_SIZE
;
2138 /* We also need to make an entry in the .got.plt section, which
2139 will be placed in the .got section by the linker script. */
2140 htab
->sgotplt
->size
+= GOT_ENTRY_SIZE
;
2142 /* We also need to make an entry in the .rela.plt section. */
2143 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
2144 htab
->srelplt
->reloc_count
++;
2148 h
->plt
.offset
= (bfd_vma
) -1;
2154 h
->plt
.offset
= (bfd_vma
) -1;
2158 eh
->tlsdesc_got
= (bfd_vma
) -1;
2160 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
2161 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
2162 if (h
->got
.refcount
> 0
2165 && elf64_x86_64_hash_entry (h
)->tls_type
== GOT_TLS_IE
)
2167 h
->got
.offset
= (bfd_vma
) -1;
2169 else if (h
->got
.refcount
> 0)
2173 int tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
2175 /* Make sure this symbol is output as a dynamic symbol.
2176 Undefined weak syms won't yet be marked as dynamic. */
2177 if (h
->dynindx
== -1
2178 && !h
->forced_local
)
2180 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
2184 if (GOT_TLS_GDESC_P (tls_type
))
2186 eh
->tlsdesc_got
= htab
->sgotplt
->size
2187 - elf64_x86_64_compute_jump_table_size (htab
);
2188 htab
->sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
2189 h
->got
.offset
= (bfd_vma
) -2;
2191 if (! GOT_TLS_GDESC_P (tls_type
)
2192 || GOT_TLS_GD_P (tls_type
))
2195 h
->got
.offset
= s
->size
;
2196 s
->size
+= GOT_ENTRY_SIZE
;
2197 if (GOT_TLS_GD_P (tls_type
))
2198 s
->size
+= GOT_ENTRY_SIZE
;
2200 dyn
= htab
->elf
.dynamic_sections_created
;
2201 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2203 R_X86_64_GOTTPOFF needs one dynamic relocation. */
2204 if ((GOT_TLS_GD_P (tls_type
) && h
->dynindx
== -1)
2205 || tls_type
== GOT_TLS_IE
)
2206 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
2207 else if (GOT_TLS_GD_P (tls_type
))
2208 htab
->srelgot
->size
+= 2 * sizeof (Elf64_External_Rela
);
2209 else if (! GOT_TLS_GDESC_P (tls_type
)
2210 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2211 || h
->root
.type
!= bfd_link_hash_undefweak
)
2213 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
2214 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
2215 if (GOT_TLS_GDESC_P (tls_type
))
2217 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
2218 htab
->tlsdesc_plt
= (bfd_vma
) -1;
2222 h
->got
.offset
= (bfd_vma
) -1;
2224 if (eh
->dyn_relocs
== NULL
)
2227 /* In the shared -Bsymbolic case, discard space allocated for
2228 dynamic pc-relative relocs against symbols which turn out to be
2229 defined in regular objects. For the normal shared case, discard
2230 space for pc-relative relocs that have become local due to symbol
2231 visibility changes. */
2235 /* Relocs that use pc_count are those that appear on a call
2236 insn, or certain REL relocs that can generated via assembly.
2237 We want calls to protected symbols to resolve directly to the
2238 function rather than going via the plt. If people want
2239 function pointer comparisons to work as expected then they
2240 should avoid writing weird assembly. */
2241 if (SYMBOL_CALLS_LOCAL (info
, h
))
2243 struct elf64_x86_64_dyn_relocs
**pp
;
2245 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
2247 p
->count
-= p
->pc_count
;
2256 /* Also discard relocs on undefined weak syms with non-default
2258 if (eh
->dyn_relocs
!= NULL
2259 && h
->root
.type
== bfd_link_hash_undefweak
)
2261 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2262 eh
->dyn_relocs
= NULL
;
2264 /* Make sure undefined weak symbols are output as a dynamic
2266 else if (h
->dynindx
== -1
2267 && ! h
->forced_local
2268 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
2273 else if (ELIMINATE_COPY_RELOCS
)
2275 /* For the non-shared case, discard space for relocs against
2276 symbols which turn out to need copy relocs or are not
2282 || (htab
->elf
.dynamic_sections_created
2283 && (h
->root
.type
== bfd_link_hash_undefweak
2284 || h
->root
.type
== bfd_link_hash_undefined
))))
2286 /* Make sure this symbol is output as a dynamic symbol.
2287 Undefined weak syms won't yet be marked as dynamic. */
2288 if (h
->dynindx
== -1
2289 && ! h
->forced_local
2290 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
2293 /* If that succeeded, we know we'll be keeping all the
2295 if (h
->dynindx
!= -1)
2299 eh
->dyn_relocs
= NULL
;
2304 /* Finally, allocate space. */
2305 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2309 sreloc
= elf_section_data (p
->sec
)->sreloc
;
2311 BFD_ASSERT (sreloc
!= NULL
);
2313 sreloc
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
2319 /* Allocate space in .plt, .got and associated reloc sections for
2320 local dynamic relocs. */
2323 elf64_x86_64_allocate_local_dynrelocs (void **slot
, void *inf
)
2325 struct elf_link_hash_entry
*h
2326 = (struct elf_link_hash_entry
*) *slot
;
2328 if (h
->type
!= STT_GNU_IFUNC
2332 || h
->root
.type
!= bfd_link_hash_defined
)
2335 return elf64_x86_64_allocate_dynrelocs (h
, inf
);
2338 /* Find any dynamic relocs that apply to read-only sections. */
2341 elf64_x86_64_readonly_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
2343 struct elf64_x86_64_link_hash_entry
*eh
;
2344 struct elf64_x86_64_dyn_relocs
*p
;
2346 if (h
->root
.type
== bfd_link_hash_warning
)
2347 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2349 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
2350 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
2352 asection
*s
= p
->sec
->output_section
;
2354 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
2356 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
2358 info
->flags
|= DF_TEXTREL
;
2360 /* Not an error, just cut short the traversal. */
2367 /* Set the sizes of the dynamic sections. */
2370 elf64_x86_64_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
2371 struct bfd_link_info
*info
)
2373 struct elf64_x86_64_link_hash_table
*htab
;
2379 htab
= elf64_x86_64_hash_table (info
);
2380 dynobj
= htab
->elf
.dynobj
;
2384 if (htab
->elf
.dynamic_sections_created
)
2386 /* Set the contents of the .interp section to the interpreter. */
2387 if (info
->executable
)
2389 s
= bfd_get_section_by_name (dynobj
, ".interp");
2392 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
2393 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
2397 /* Set up .got offsets for local syms, and space for local dynamic
2399 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
2401 bfd_signed_vma
*local_got
;
2402 bfd_signed_vma
*end_local_got
;
2403 char *local_tls_type
;
2404 bfd_vma
*local_tlsdesc_gotent
;
2405 bfd_size_type locsymcount
;
2406 Elf_Internal_Shdr
*symtab_hdr
;
2409 if (! is_x86_64_elf (ibfd
))
2412 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2414 struct elf64_x86_64_dyn_relocs
*p
;
2416 for (p
= (struct elf64_x86_64_dyn_relocs
*)
2417 (elf_section_data (s
)->local_dynrel
);
2421 if (!bfd_is_abs_section (p
->sec
)
2422 && bfd_is_abs_section (p
->sec
->output_section
))
2424 /* Input section has been discarded, either because
2425 it is a copy of a linkonce section or due to
2426 linker script /DISCARD/, so we'll be discarding
2429 else if (p
->count
!= 0)
2431 srel
= elf_section_data (p
->sec
)->sreloc
;
2432 srel
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
2433 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
2434 info
->flags
|= DF_TEXTREL
;
2439 local_got
= elf_local_got_refcounts (ibfd
);
2443 symtab_hdr
= &elf_symtab_hdr (ibfd
);
2444 locsymcount
= symtab_hdr
->sh_info
;
2445 end_local_got
= local_got
+ locsymcount
;
2446 local_tls_type
= elf64_x86_64_local_got_tls_type (ibfd
);
2447 local_tlsdesc_gotent
= elf64_x86_64_local_tlsdesc_gotent (ibfd
);
2449 srel
= htab
->srelgot
;
2450 for (; local_got
< end_local_got
;
2451 ++local_got
, ++local_tls_type
, ++local_tlsdesc_gotent
)
2453 *local_tlsdesc_gotent
= (bfd_vma
) -1;
2456 if (GOT_TLS_GDESC_P (*local_tls_type
))
2458 *local_tlsdesc_gotent
= htab
->sgotplt
->size
2459 - elf64_x86_64_compute_jump_table_size (htab
);
2460 htab
->sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
2461 *local_got
= (bfd_vma
) -2;
2463 if (! GOT_TLS_GDESC_P (*local_tls_type
)
2464 || GOT_TLS_GD_P (*local_tls_type
))
2466 *local_got
= s
->size
;
2467 s
->size
+= GOT_ENTRY_SIZE
;
2468 if (GOT_TLS_GD_P (*local_tls_type
))
2469 s
->size
+= GOT_ENTRY_SIZE
;
2472 || GOT_TLS_GD_ANY_P (*local_tls_type
)
2473 || *local_tls_type
== GOT_TLS_IE
)
2475 if (GOT_TLS_GDESC_P (*local_tls_type
))
2477 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
2478 htab
->tlsdesc_plt
= (bfd_vma
) -1;
2480 if (! GOT_TLS_GDESC_P (*local_tls_type
)
2481 || GOT_TLS_GD_P (*local_tls_type
))
2482 srel
->size
+= sizeof (Elf64_External_Rela
);
2486 *local_got
= (bfd_vma
) -1;
2490 if (htab
->tls_ld_got
.refcount
> 0)
2492 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2494 htab
->tls_ld_got
.offset
= htab
->sgot
->size
;
2495 htab
->sgot
->size
+= 2 * GOT_ENTRY_SIZE
;
2496 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
2499 htab
->tls_ld_got
.offset
= -1;
2501 /* Allocate global sym .plt and .got entries, and space for global
2502 sym dynamic relocs. */
2503 elf_link_hash_traverse (&htab
->elf
, elf64_x86_64_allocate_dynrelocs
,
2506 /* Allocate .plt and .got entries, and space for local symbols. */
2507 htab_traverse (htab
->loc_hash_table
,
2508 elf64_x86_64_allocate_local_dynrelocs
,
2511 /* For every jump slot reserved in the sgotplt, reloc_count is
2512 incremented. However, when we reserve space for TLS descriptors,
2513 it's not incremented, so in order to compute the space reserved
2514 for them, it suffices to multiply the reloc count by the jump
2517 htab
->sgotplt_jump_table_size
2518 = elf64_x86_64_compute_jump_table_size (htab
);
2520 if (htab
->tlsdesc_plt
)
2522 /* If we're not using lazy TLS relocations, don't generate the
2523 PLT and GOT entries they require. */
2524 if ((info
->flags
& DF_BIND_NOW
))
2525 htab
->tlsdesc_plt
= 0;
2528 htab
->tlsdesc_got
= htab
->sgot
->size
;
2529 htab
->sgot
->size
+= GOT_ENTRY_SIZE
;
2530 /* Reserve room for the initial entry.
2531 FIXME: we could probably do away with it in this case. */
2532 if (htab
->splt
->size
== 0)
2533 htab
->splt
->size
+= PLT_ENTRY_SIZE
;
2534 htab
->tlsdesc_plt
= htab
->splt
->size
;
2535 htab
->splt
->size
+= PLT_ENTRY_SIZE
;
2539 /* We now have determined the sizes of the various dynamic sections.
2540 Allocate memory for them. */
2542 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
2544 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2549 || s
== htab
->sgotplt
2551 || s
== htab
->igotplt
2552 || s
== htab
->sdynbss
)
2554 /* Strip this section if we don't need it; see the
2557 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
2559 if (s
->size
!= 0 && s
!= htab
->srelplt
)
2562 /* We use the reloc_count field as a counter if we need
2563 to copy relocs into the output file. */
2564 if (s
!= htab
->srelplt
)
2569 /* It's not one of our sections, so don't allocate space. */
2575 /* If we don't need this section, strip it from the
2576 output file. This is mostly to handle .rela.bss and
2577 .rela.plt. We must create both sections in
2578 create_dynamic_sections, because they must be created
2579 before the linker maps input sections to output
2580 sections. The linker does that before
2581 adjust_dynamic_symbol is called, and it is that
2582 function which decides whether anything needs to go
2583 into these sections. */
2585 s
->flags
|= SEC_EXCLUDE
;
2589 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
2592 /* Allocate memory for the section contents. We use bfd_zalloc
2593 here in case unused entries are not reclaimed before the
2594 section's contents are written out. This should not happen,
2595 but this way if it does, we get a R_X86_64_NONE reloc instead
2597 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
2598 if (s
->contents
== NULL
)
2602 if (htab
->elf
.dynamic_sections_created
)
2604 /* Add some entries to the .dynamic section. We fill in the
2605 values later, in elf64_x86_64_finish_dynamic_sections, but we
2606 must add the entries now so that we get the correct size for
2607 the .dynamic section. The DT_DEBUG entry is filled in by the
2608 dynamic linker and used by the debugger. */
2609 #define add_dynamic_entry(TAG, VAL) \
2610 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2612 if (info
->executable
)
2614 if (!add_dynamic_entry (DT_DEBUG
, 0))
2618 if (htab
->splt
->size
!= 0)
2620 if (!add_dynamic_entry (DT_PLTGOT
, 0)
2621 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
2622 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
2623 || !add_dynamic_entry (DT_JMPREL
, 0))
2626 if (htab
->tlsdesc_plt
2627 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
2628 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
2634 if (!add_dynamic_entry (DT_RELA
, 0)
2635 || !add_dynamic_entry (DT_RELASZ
, 0)
2636 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf64_External_Rela
)))
2639 /* If any dynamic relocs apply to a read-only section,
2640 then we need a DT_TEXTREL entry. */
2641 if ((info
->flags
& DF_TEXTREL
) == 0)
2642 elf_link_hash_traverse (&htab
->elf
,
2643 elf64_x86_64_readonly_dynrelocs
,
2646 if ((info
->flags
& DF_TEXTREL
) != 0)
2648 if (!add_dynamic_entry (DT_TEXTREL
, 0))
2653 #undef add_dynamic_entry
2659 elf64_x86_64_always_size_sections (bfd
*output_bfd
,
2660 struct bfd_link_info
*info
)
2662 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
2666 struct elf_link_hash_entry
*tlsbase
;
2668 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
2669 "_TLS_MODULE_BASE_",
2670 FALSE
, FALSE
, FALSE
);
2672 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
2674 struct bfd_link_hash_entry
*bh
= NULL
;
2675 const struct elf_backend_data
*bed
2676 = get_elf_backend_data (output_bfd
);
2678 if (!(_bfd_generic_link_add_one_symbol
2679 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
2680 tls_sec
, 0, NULL
, FALSE
,
2681 bed
->collect
, &bh
)))
2684 elf64_x86_64_hash_table (info
)->tls_module_base
= bh
;
2686 tlsbase
= (struct elf_link_hash_entry
*)bh
;
2687 tlsbase
->def_regular
= 1;
2688 tlsbase
->other
= STV_HIDDEN
;
2689 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, TRUE
);
2696 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2697 executables. Rather than setting it to the beginning of the TLS
2698 section, we have to set it to the end. This function may be called
2699 multiple times, it is idempotent. */
2702 elf64_x86_64_set_tls_module_base (struct bfd_link_info
*info
)
2704 struct bfd_link_hash_entry
*base
;
2706 if (!info
->executable
)
2709 base
= elf64_x86_64_hash_table (info
)->tls_module_base
;
2714 base
->u
.def
.value
= elf_hash_table (info
)->tls_size
;
2717 /* Return the base VMA address which should be subtracted from real addresses
2718 when resolving @dtpoff relocation.
2719 This is PT_TLS segment p_vaddr. */
2722 elf64_x86_64_dtpoff_base (struct bfd_link_info
*info
)
2724 /* If tls_sec is NULL, we should have signalled an error already. */
2725 if (elf_hash_table (info
)->tls_sec
== NULL
)
2727 return elf_hash_table (info
)->tls_sec
->vma
;
2730 /* Return the relocation value for @tpoff relocation
2731 if STT_TLS virtual address is ADDRESS. */
2734 elf64_x86_64_tpoff (struct bfd_link_info
*info
, bfd_vma address
)
2736 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2738 /* If tls_segment is NULL, we should have signalled an error already. */
2739 if (htab
->tls_sec
== NULL
)
2741 return address
- htab
->tls_size
- htab
->tls_sec
->vma
;
2744 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2748 is_32bit_relative_branch (bfd_byte
*contents
, bfd_vma offset
)
2750 /* Opcode Instruction
2753 0x0f 0x8x conditional jump */
2755 && (contents
[offset
- 1] == 0xe8
2756 || contents
[offset
- 1] == 0xe9))
2758 && contents
[offset
- 2] == 0x0f
2759 && (contents
[offset
- 1] & 0xf0) == 0x80));
2762 /* Relocate an x86_64 ELF section. */
2765 elf64_x86_64_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
2766 bfd
*input_bfd
, asection
*input_section
,
2767 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
2768 Elf_Internal_Sym
*local_syms
,
2769 asection
**local_sections
)
2771 struct elf64_x86_64_link_hash_table
*htab
;
2772 Elf_Internal_Shdr
*symtab_hdr
;
2773 struct elf_link_hash_entry
**sym_hashes
;
2774 bfd_vma
*local_got_offsets
;
2775 bfd_vma
*local_tlsdesc_gotents
;
2776 Elf_Internal_Rela
*rel
;
2777 Elf_Internal_Rela
*relend
;
2779 BFD_ASSERT (is_x86_64_elf (input_bfd
));
2781 htab
= elf64_x86_64_hash_table (info
);
2782 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
2783 sym_hashes
= elf_sym_hashes (input_bfd
);
2784 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2785 local_tlsdesc_gotents
= elf64_x86_64_local_tlsdesc_gotent (input_bfd
);
2787 elf64_x86_64_set_tls_module_base (info
);
2790 relend
= relocs
+ input_section
->reloc_count
;
2791 for (; rel
< relend
; rel
++)
2793 unsigned int r_type
;
2794 reloc_howto_type
*howto
;
2795 unsigned long r_symndx
;
2796 struct elf_link_hash_entry
*h
;
2797 Elf_Internal_Sym
*sym
;
2799 bfd_vma off
, offplt
;
2801 bfd_boolean unresolved_reloc
;
2802 bfd_reloc_status_type r
;
2806 r_type
= ELF64_R_TYPE (rel
->r_info
);
2807 if (r_type
== (int) R_X86_64_GNU_VTINHERIT
2808 || r_type
== (int) R_X86_64_GNU_VTENTRY
)
2811 if (r_type
>= R_X86_64_max
)
2813 bfd_set_error (bfd_error_bad_value
);
2817 howto
= x86_64_elf_howto_table
+ r_type
;
2818 r_symndx
= ELF64_R_SYM (rel
->r_info
);
2822 unresolved_reloc
= FALSE
;
2823 if (r_symndx
< symtab_hdr
->sh_info
)
2825 sym
= local_syms
+ r_symndx
;
2826 sec
= local_sections
[r_symndx
];
2828 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
,
2831 /* Relocate against local STT_GNU_IFUNC symbol. */
2832 if (ELF64_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
2834 h
= elf64_x86_64_get_local_sym_hash (htab
, input_bfd
,
2839 /* Set STT_GNU_IFUNC symbol value. */
2840 h
->root
.u
.def
.value
= sym
->st_value
;
2841 h
->root
.u
.def
.section
= sec
;
2848 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2849 r_symndx
, symtab_hdr
, sym_hashes
,
2851 unresolved_reloc
, warned
);
2854 if (sec
!= NULL
&& elf_discarded_section (sec
))
2856 /* For relocs against symbols from removed linkonce sections,
2857 or sections discarded by a linker script, we just want the
2858 section contents zeroed. Avoid any special processing. */
2859 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
2865 if (info
->relocatable
)
2868 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2869 it here if it is defined in a non-shared object. */
2871 && h
->type
== STT_GNU_IFUNC
2877 if ((input_section
->flags
& SEC_ALLOC
) == 0
2878 || h
->plt
.offset
== (bfd_vma
) -1)
2881 /* STT_GNU_IFUNC symbol must go through PLT. */
2882 plt
= htab
->splt
? htab
->splt
: htab
->iplt
;
2883 relocation
= (plt
->output_section
->vma
2884 + plt
->output_offset
+ h
->plt
.offset
);
2889 (*_bfd_error_handler
)
2890 (_("%B: relocation %s against STT_GNU_IFUNC "
2891 "symbol `%s' isn't handled by %s"), input_bfd
,
2892 x86_64_elf_howto_table
[r_type
].name
,
2893 (h
->root
.root
.string
2894 ? h
->root
.root
.string
: "a local symbol"),
2896 bfd_set_error (bfd_error_bad_value
);
2905 if (rel
->r_addend
!= 0)
2907 (*_bfd_error_handler
)
2908 (_("%B: relocation %s against STT_GNU_IFUNC "
2909 "symbol `%s' has non-zero addend: %d"),
2910 input_bfd
, x86_64_elf_howto_table
[r_type
].name
,
2911 (h
->root
.root
.string
2912 ? h
->root
.root
.string
: "a local symbol"),
2914 bfd_set_error (bfd_error_bad_value
);
2918 /* Generate dynamic relcoation only when there is a
2919 non-GOF reference in a shared object. */
2920 if (info
->shared
&& h
->non_got_ref
)
2922 Elf_Internal_Rela outrel
;
2926 /* Need a dynamic relocation to get the real function
2928 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2932 if (outrel
.r_offset
== (bfd_vma
) -1
2933 || outrel
.r_offset
== (bfd_vma
) -2)
2936 outrel
.r_offset
+= (input_section
->output_section
->vma
2937 + input_section
->output_offset
);
2939 if (h
->dynindx
== -1
2941 || info
->executable
)
2943 /* This symbol is resolved locally. */
2944 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_IRELATIVE
);
2945 outrel
.r_addend
= (h
->root
.u
.def
.value
2946 + h
->root
.u
.def
.section
->output_section
->vma
2947 + h
->root
.u
.def
.section
->output_offset
);
2951 outrel
.r_info
= ELF64_R_INFO (h
->dynindx
, r_type
);
2952 outrel
.r_addend
= 0;
2955 sreloc
= htab
->irelifunc
;
2956 loc
= sreloc
->contents
;
2957 loc
+= (sreloc
->reloc_count
++
2958 * sizeof (Elf64_External_Rela
));
2959 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2961 /* If this reloc is against an external symbol, we
2962 do not want to fiddle with the addend. Otherwise,
2963 we need to include the symbol value so that it
2964 becomes an addend for the dynamic reloc. For an
2965 internal symbol, we have updated addend. */
2972 case R_X86_64_PLT32
:
2975 case R_X86_64_GOTPCREL
:
2976 case R_X86_64_GOTPCREL64
:
2977 base_got
= htab
->sgot
;
2978 off
= h
->got
.offset
;
2980 if (base_got
== NULL
)
2983 if (off
== (bfd_vma
) -1)
2985 /* We can't use h->got.offset here to save state, or
2986 even just remember the offset, as finish_dynamic_symbol
2987 would use that as offset into .got. */
2989 if (htab
->splt
!= NULL
)
2991 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
2992 off
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
2993 base_got
= htab
->sgotplt
;
2997 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2998 off
= plt_index
* GOT_ENTRY_SIZE
;
2999 base_got
= htab
->igotplt
;
3002 if (h
->dynindx
== -1
3006 /* This references the local defitionion. We must
3007 initialize this entry in the global offset table.
3008 Since the offset must always be a multiple of 8,
3009 we use the least significant bit to record
3010 whether we have initialized it already.
3012 When doing a dynamic link, we create a .rela.got
3013 relocation entry to initialize the value. This
3014 is done in the finish_dynamic_symbol routine. */
3019 bfd_put_64 (output_bfd
, relocation
,
3020 base_got
->contents
+ off
);
3021 /* Note that this is harmless for the GOTPLT64
3022 case, as -1 | 1 still is -1. */
3028 relocation
= (base_got
->output_section
->vma
3029 + base_got
->output_offset
+ off
);
3031 if (r_type
!= R_X86_64_GOTPCREL
3032 && r_type
!= R_X86_64_GOTPCREL64
)
3035 if (htab
->splt
!= NULL
)
3036 gotplt
= htab
->sgotplt
;
3038 gotplt
= htab
->igotplt
;
3039 relocation
-= (gotplt
->output_section
->vma
3040 - gotplt
->output_offset
);
3047 /* When generating a shared object, the relocations handled here are
3048 copied into the output file to be resolved at run time. */
3051 case R_X86_64_GOT32
:
3052 case R_X86_64_GOT64
:
3053 /* Relocation is to the entry for this symbol in the global
3055 case R_X86_64_GOTPCREL
:
3056 case R_X86_64_GOTPCREL64
:
3057 /* Use global offset table entry as symbol value. */
3058 case R_X86_64_GOTPLT64
:
3059 /* This is the same as GOT64 for relocation purposes, but
3060 indicates the existence of a PLT entry. The difficulty is,
3061 that we must calculate the GOT slot offset from the PLT
3062 offset, if this symbol got a PLT entry (it was global).
3063 Additionally if it's computed from the PLT entry, then that
3064 GOT offset is relative to .got.plt, not to .got. */
3065 base_got
= htab
->sgot
;
3067 if (htab
->sgot
== NULL
)
3074 off
= h
->got
.offset
;
3076 && h
->plt
.offset
!= (bfd_vma
)-1
3077 && off
== (bfd_vma
)-1)
3079 /* We can't use h->got.offset here to save
3080 state, or even just remember the offset, as
3081 finish_dynamic_symbol would use that as offset into
3083 bfd_vma plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
3084 off
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
3085 base_got
= htab
->sgotplt
;
3088 dyn
= htab
->elf
.dynamic_sections_created
;
3090 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
3092 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3093 || (ELF_ST_VISIBILITY (h
->other
)
3094 && h
->root
.type
== bfd_link_hash_undefweak
))
3096 /* This is actually a static link, or it is a -Bsymbolic
3097 link and the symbol is defined locally, or the symbol
3098 was forced to be local because of a version file. We
3099 must initialize this entry in the global offset table.
3100 Since the offset must always be a multiple of 8, we
3101 use the least significant bit to record whether we
3102 have initialized it already.
3104 When doing a dynamic link, we create a .rela.got
3105 relocation entry to initialize the value. This is
3106 done in the finish_dynamic_symbol routine. */
3111 bfd_put_64 (output_bfd
, relocation
,
3112 base_got
->contents
+ off
);
3113 /* Note that this is harmless for the GOTPLT64 case,
3114 as -1 | 1 still is -1. */
3119 unresolved_reloc
= FALSE
;
3123 if (local_got_offsets
== NULL
)
3126 off
= local_got_offsets
[r_symndx
];
3128 /* The offset must always be a multiple of 8. We use
3129 the least significant bit to record whether we have
3130 already generated the necessary reloc. */
3135 bfd_put_64 (output_bfd
, relocation
,
3136 base_got
->contents
+ off
);
3141 Elf_Internal_Rela outrel
;
3144 /* We need to generate a R_X86_64_RELATIVE reloc
3145 for the dynamic linker. */
3150 outrel
.r_offset
= (base_got
->output_section
->vma
3151 + base_got
->output_offset
3153 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
3154 outrel
.r_addend
= relocation
;
3156 loc
+= s
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3157 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3160 local_got_offsets
[r_symndx
] |= 1;
3164 if (off
>= (bfd_vma
) -2)
3167 relocation
= base_got
->output_section
->vma
3168 + base_got
->output_offset
+ off
;
3169 if (r_type
!= R_X86_64_GOTPCREL
&& r_type
!= R_X86_64_GOTPCREL64
)
3170 relocation
-= htab
->sgotplt
->output_section
->vma
3171 - htab
->sgotplt
->output_offset
;
3175 case R_X86_64_GOTOFF64
:
3176 /* Relocation is relative to the start of the global offset
3179 /* Check to make sure it isn't a protected function symbol
3180 for shared library since it may not be local when used
3181 as function address. */
3185 && h
->type
== STT_FUNC
3186 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
3188 (*_bfd_error_handler
)
3189 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3190 input_bfd
, h
->root
.root
.string
);
3191 bfd_set_error (bfd_error_bad_value
);
3195 /* Note that sgot is not involved in this
3196 calculation. We always want the start of .got.plt. If we
3197 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3198 permitted by the ABI, we might have to change this
3200 relocation
-= htab
->sgotplt
->output_section
->vma
3201 + htab
->sgotplt
->output_offset
;
3204 case R_X86_64_GOTPC32
:
3205 case R_X86_64_GOTPC64
:
3206 /* Use global offset table as symbol value. */
3207 relocation
= htab
->sgotplt
->output_section
->vma
3208 + htab
->sgotplt
->output_offset
;
3209 unresolved_reloc
= FALSE
;
3212 case R_X86_64_PLTOFF64
:
3213 /* Relocation is PLT entry relative to GOT. For local
3214 symbols it's the symbol itself relative to GOT. */
3216 /* See PLT32 handling. */
3217 && h
->plt
.offset
!= (bfd_vma
) -1
3218 && htab
->splt
!= NULL
)
3220 relocation
= (htab
->splt
->output_section
->vma
3221 + htab
->splt
->output_offset
3223 unresolved_reloc
= FALSE
;
3226 relocation
-= htab
->sgotplt
->output_section
->vma
3227 + htab
->sgotplt
->output_offset
;
3230 case R_X86_64_PLT32
:
3231 /* Relocation is to the entry for this symbol in the
3232 procedure linkage table. */
3234 /* Resolve a PLT32 reloc against a local symbol directly,
3235 without using the procedure linkage table. */
3239 if (h
->plt
.offset
== (bfd_vma
) -1
3240 || htab
->splt
== NULL
)
3242 /* We didn't make a PLT entry for this symbol. This
3243 happens when statically linking PIC code, or when
3244 using -Bsymbolic. */
3248 relocation
= (htab
->splt
->output_section
->vma
3249 + htab
->splt
->output_offset
3251 unresolved_reloc
= FALSE
;
3258 && (input_section
->flags
& SEC_ALLOC
) != 0
3259 && (input_section
->flags
& SEC_READONLY
) != 0
3262 bfd_boolean fail
= FALSE
;
3264 = (r_type
== R_X86_64_PC32
3265 && is_32bit_relative_branch (contents
, rel
->r_offset
));
3267 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
3269 /* Symbol is referenced locally. Make sure it is
3270 defined locally or for a branch. */
3271 fail
= !h
->def_regular
&& !branch
;
3275 /* Symbol isn't referenced locally. We only allow
3276 branch to symbol with non-default visibility. */
3278 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
);
3285 const char *pic
= "";
3287 switch (ELF_ST_VISIBILITY (h
->other
))
3290 v
= _("hidden symbol");
3293 v
= _("internal symbol");
3296 v
= _("protected symbol");
3300 pic
= _("; recompile with -fPIC");
3305 fmt
= _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3307 fmt
= _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3309 (*_bfd_error_handler
) (fmt
, input_bfd
,
3310 x86_64_elf_howto_table
[r_type
].name
,
3311 v
, h
->root
.root
.string
, pic
);
3312 bfd_set_error (bfd_error_bad_value
);
3323 /* FIXME: The ABI says the linker should make sure the value is
3324 the same when it's zeroextended to 64 bit. */
3326 if ((input_section
->flags
& SEC_ALLOC
) == 0)
3331 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3332 || h
->root
.type
!= bfd_link_hash_undefweak
)
3333 && (! IS_X86_64_PCREL_TYPE (r_type
)
3334 || ! SYMBOL_CALLS_LOCAL (info
, h
)))
3335 || (ELIMINATE_COPY_RELOCS
3342 || h
->root
.type
== bfd_link_hash_undefweak
3343 || h
->root
.type
== bfd_link_hash_undefined
)))
3345 Elf_Internal_Rela outrel
;
3347 bfd_boolean skip
, relocate
;
3350 /* When generating a shared object, these relocations
3351 are copied into the output file to be resolved at run
3357 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
3359 if (outrel
.r_offset
== (bfd_vma
) -1)
3361 else if (outrel
.r_offset
== (bfd_vma
) -2)
3362 skip
= TRUE
, relocate
= TRUE
;
3364 outrel
.r_offset
+= (input_section
->output_section
->vma
3365 + input_section
->output_offset
);
3368 memset (&outrel
, 0, sizeof outrel
);
3370 /* h->dynindx may be -1 if this symbol was marked to
3374 && (IS_X86_64_PCREL_TYPE (r_type
)
3376 || ! SYMBOLIC_BIND (info
, h
)
3377 || ! h
->def_regular
))
3379 outrel
.r_info
= ELF64_R_INFO (h
->dynindx
, r_type
);
3380 outrel
.r_addend
= rel
->r_addend
;
3384 /* This symbol is local, or marked to become local. */
3385 if (r_type
== R_X86_64_64
)
3388 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
3389 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3395 if (bfd_is_abs_section (sec
))
3397 else if (sec
== NULL
|| sec
->owner
== NULL
)
3399 bfd_set_error (bfd_error_bad_value
);
3406 /* We are turning this relocation into one
3407 against a section symbol. It would be
3408 proper to subtract the symbol's value,
3409 osec->vma, from the emitted reloc addend,
3410 but ld.so expects buggy relocs. */
3411 osec
= sec
->output_section
;
3412 sindx
= elf_section_data (osec
)->dynindx
;
3415 asection
*oi
= htab
->elf
.text_index_section
;
3416 sindx
= elf_section_data (oi
)->dynindx
;
3418 BFD_ASSERT (sindx
!= 0);
3421 outrel
.r_info
= ELF64_R_INFO (sindx
, r_type
);
3422 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3426 sreloc
= elf_section_data (input_section
)->sreloc
;
3428 BFD_ASSERT (sreloc
!= NULL
&& sreloc
->contents
!= NULL
);
3430 loc
= sreloc
->contents
;
3431 loc
+= sreloc
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3432 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3434 /* If this reloc is against an external symbol, we do
3435 not want to fiddle with the addend. Otherwise, we
3436 need to include the symbol value so that it becomes
3437 an addend for the dynamic reloc. */
3444 case R_X86_64_TLSGD
:
3445 case R_X86_64_GOTPC32_TLSDESC
:
3446 case R_X86_64_TLSDESC_CALL
:
3447 case R_X86_64_GOTTPOFF
:
3448 tls_type
= GOT_UNKNOWN
;
3449 if (h
== NULL
&& local_got_offsets
)
3450 tls_type
= elf64_x86_64_local_got_tls_type (input_bfd
) [r_symndx
];
3452 tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
3454 if (! elf64_x86_64_tls_transition (info
, input_bfd
,
3455 input_section
, contents
,
3456 symtab_hdr
, sym_hashes
,
3457 &r_type
, tls_type
, rel
,
3461 if (r_type
== R_X86_64_TPOFF32
)
3463 bfd_vma roff
= rel
->r_offset
;
3465 BFD_ASSERT (! unresolved_reloc
);
3467 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
3469 /* GD->LE transition.
3470 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3471 .word 0x6666; rex64; call __tls_get_addr
3474 leaq foo@tpoff(%rax), %rax */
3475 memcpy (contents
+ roff
- 4,
3476 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3478 bfd_put_32 (output_bfd
,
3479 elf64_x86_64_tpoff (info
, relocation
),
3480 contents
+ roff
+ 8);
3481 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3485 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
3487 /* GDesc -> LE transition.
3488 It's originally something like:
3489 leaq x@tlsdesc(%rip), %rax
3495 unsigned int val
, type
, type2
;
3497 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
3498 type2
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
3499 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
3500 bfd_put_8 (output_bfd
, 0x48 | ((type
>> 2) & 1),
3501 contents
+ roff
- 3);
3502 bfd_put_8 (output_bfd
, 0xc7, contents
+ roff
- 2);
3503 bfd_put_8 (output_bfd
, 0xc0 | ((val
>> 3) & 7),
3504 contents
+ roff
- 1);
3505 bfd_put_32 (output_bfd
,
3506 elf64_x86_64_tpoff (info
, relocation
),
3510 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
3512 /* GDesc -> LE transition.
3517 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
3518 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
3521 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTTPOFF
)
3523 /* IE->LE transition:
3524 Originally it can be one of:
3525 movq foo@gottpoff(%rip), %reg
3526 addq foo@gottpoff(%rip), %reg
3529 leaq foo(%reg), %reg
3532 unsigned int val
, type
, reg
;
3534 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
3535 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
3536 reg
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
3542 bfd_put_8 (output_bfd
, 0x49,
3543 contents
+ roff
- 3);
3544 bfd_put_8 (output_bfd
, 0xc7,
3545 contents
+ roff
- 2);
3546 bfd_put_8 (output_bfd
, 0xc0 | reg
,
3547 contents
+ roff
- 1);
3551 /* addq -> addq - addressing with %rsp/%r12 is
3554 bfd_put_8 (output_bfd
, 0x49,
3555 contents
+ roff
- 3);
3556 bfd_put_8 (output_bfd
, 0x81,
3557 contents
+ roff
- 2);
3558 bfd_put_8 (output_bfd
, 0xc0 | reg
,
3559 contents
+ roff
- 1);
3565 bfd_put_8 (output_bfd
, 0x4d,
3566 contents
+ roff
- 3);
3567 bfd_put_8 (output_bfd
, 0x8d,
3568 contents
+ roff
- 2);
3569 bfd_put_8 (output_bfd
, 0x80 | reg
| (reg
<< 3),
3570 contents
+ roff
- 1);
3572 bfd_put_32 (output_bfd
,
3573 elf64_x86_64_tpoff (info
, relocation
),
3581 if (htab
->sgot
== NULL
)
3586 off
= h
->got
.offset
;
3587 offplt
= elf64_x86_64_hash_entry (h
)->tlsdesc_got
;
3591 if (local_got_offsets
== NULL
)
3594 off
= local_got_offsets
[r_symndx
];
3595 offplt
= local_tlsdesc_gotents
[r_symndx
];
3602 Elf_Internal_Rela outrel
;
3607 if (htab
->srelgot
== NULL
)
3610 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
3612 if (GOT_TLS_GDESC_P (tls_type
))
3614 outrel
.r_info
= ELF64_R_INFO (indx
, R_X86_64_TLSDESC
);
3615 BFD_ASSERT (htab
->sgotplt_jump_table_size
+ offplt
3616 + 2 * GOT_ENTRY_SIZE
<= htab
->sgotplt
->size
);
3617 outrel
.r_offset
= (htab
->sgotplt
->output_section
->vma
3618 + htab
->sgotplt
->output_offset
3620 + htab
->sgotplt_jump_table_size
);
3621 sreloc
= htab
->srelplt
;
3622 loc
= sreloc
->contents
;
3623 loc
+= sreloc
->reloc_count
++
3624 * sizeof (Elf64_External_Rela
);
3625 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
3626 <= sreloc
->contents
+ sreloc
->size
);
3628 outrel
.r_addend
= relocation
- elf64_x86_64_dtpoff_base (info
);
3630 outrel
.r_addend
= 0;
3631 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3634 sreloc
= htab
->srelgot
;
3636 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
3637 + htab
->sgot
->output_offset
+ off
);
3639 if (GOT_TLS_GD_P (tls_type
))
3640 dr_type
= R_X86_64_DTPMOD64
;
3641 else if (GOT_TLS_GDESC_P (tls_type
))
3644 dr_type
= R_X86_64_TPOFF64
;
3646 bfd_put_64 (output_bfd
, 0, htab
->sgot
->contents
+ off
);
3647 outrel
.r_addend
= 0;
3648 if ((dr_type
== R_X86_64_TPOFF64
3649 || dr_type
== R_X86_64_TLSDESC
) && indx
== 0)
3650 outrel
.r_addend
= relocation
- elf64_x86_64_dtpoff_base (info
);
3651 outrel
.r_info
= ELF64_R_INFO (indx
, dr_type
);
3653 loc
= sreloc
->contents
;
3654 loc
+= sreloc
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3655 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
3656 <= sreloc
->contents
+ sreloc
->size
);
3657 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3659 if (GOT_TLS_GD_P (tls_type
))
3663 BFD_ASSERT (! unresolved_reloc
);
3664 bfd_put_64 (output_bfd
,
3665 relocation
- elf64_x86_64_dtpoff_base (info
),
3666 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3670 bfd_put_64 (output_bfd
, 0,
3671 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3672 outrel
.r_info
= ELF64_R_INFO (indx
,
3674 outrel
.r_offset
+= GOT_ENTRY_SIZE
;
3675 sreloc
->reloc_count
++;
3676 loc
+= sizeof (Elf64_External_Rela
);
3677 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
3678 <= sreloc
->contents
+ sreloc
->size
);
3679 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3687 local_got_offsets
[r_symndx
] |= 1;
3690 if (off
>= (bfd_vma
) -2
3691 && ! GOT_TLS_GDESC_P (tls_type
))
3693 if (r_type
== ELF64_R_TYPE (rel
->r_info
))
3695 if (r_type
== R_X86_64_GOTPC32_TLSDESC
3696 || r_type
== R_X86_64_TLSDESC_CALL
)
3697 relocation
= htab
->sgotplt
->output_section
->vma
3698 + htab
->sgotplt
->output_offset
3699 + offplt
+ htab
->sgotplt_jump_table_size
;
3701 relocation
= htab
->sgot
->output_section
->vma
3702 + htab
->sgot
->output_offset
+ off
;
3703 unresolved_reloc
= FALSE
;
3707 bfd_vma roff
= rel
->r_offset
;
3709 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
3711 /* GD->IE transition.
3712 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3713 .word 0x6666; rex64; call __tls_get_addr@plt
3716 addq foo@gottpoff(%rip), %rax */
3717 memcpy (contents
+ roff
- 4,
3718 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3721 relocation
= (htab
->sgot
->output_section
->vma
3722 + htab
->sgot
->output_offset
+ off
3724 - input_section
->output_section
->vma
3725 - input_section
->output_offset
3727 bfd_put_32 (output_bfd
, relocation
,
3728 contents
+ roff
+ 8);
3729 /* Skip R_X86_64_PLT32. */
3733 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
3735 /* GDesc -> IE transition.
3736 It's originally something like:
3737 leaq x@tlsdesc(%rip), %rax
3740 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3743 unsigned int val
, type
, type2
;
3745 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
3746 type2
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
3747 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
3749 /* Now modify the instruction as appropriate. To
3750 turn a leaq into a movq in the form we use it, it
3751 suffices to change the second byte from 0x8d to
3753 bfd_put_8 (output_bfd
, 0x8b, contents
+ roff
- 2);
3755 bfd_put_32 (output_bfd
,
3756 htab
->sgot
->output_section
->vma
3757 + htab
->sgot
->output_offset
+ off
3759 - input_section
->output_section
->vma
3760 - input_section
->output_offset
3765 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
3767 /* GDesc -> IE transition.
3774 unsigned int val
, type
;
3776 type
= bfd_get_8 (input_bfd
, contents
+ roff
);
3777 val
= bfd_get_8 (input_bfd
, contents
+ roff
+ 1);
3778 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
3779 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
3787 case R_X86_64_TLSLD
:
3788 if (! elf64_x86_64_tls_transition (info
, input_bfd
,
3789 input_section
, contents
,
3790 symtab_hdr
, sym_hashes
,
3791 &r_type
, GOT_UNKNOWN
,
3795 if (r_type
!= R_X86_64_TLSLD
)
3797 /* LD->LE transition:
3798 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3800 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3802 BFD_ASSERT (r_type
== R_X86_64_TPOFF32
);
3803 memcpy (contents
+ rel
->r_offset
- 3,
3804 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3805 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3810 if (htab
->sgot
== NULL
)
3813 off
= htab
->tls_ld_got
.offset
;
3818 Elf_Internal_Rela outrel
;
3821 if (htab
->srelgot
== NULL
)
3824 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
3825 + htab
->sgot
->output_offset
+ off
);
3827 bfd_put_64 (output_bfd
, 0,
3828 htab
->sgot
->contents
+ off
);
3829 bfd_put_64 (output_bfd
, 0,
3830 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3831 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_DTPMOD64
);
3832 outrel
.r_addend
= 0;
3833 loc
= htab
->srelgot
->contents
;
3834 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3835 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3836 htab
->tls_ld_got
.offset
|= 1;
3838 relocation
= htab
->sgot
->output_section
->vma
3839 + htab
->sgot
->output_offset
+ off
;
3840 unresolved_reloc
= FALSE
;
3843 case R_X86_64_DTPOFF32
:
3844 if (info
->shared
|| (input_section
->flags
& SEC_CODE
) == 0)
3845 relocation
-= elf64_x86_64_dtpoff_base (info
);
3847 relocation
= elf64_x86_64_tpoff (info
, relocation
);
3850 case R_X86_64_TPOFF32
:
3851 BFD_ASSERT (! info
->shared
);
3852 relocation
= elf64_x86_64_tpoff (info
, relocation
);
3859 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3860 because such sections are not SEC_ALLOC and thus ld.so will
3861 not process them. */
3862 if (unresolved_reloc
3863 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
3865 (*_bfd_error_handler
)
3866 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3869 (long) rel
->r_offset
,
3871 h
->root
.root
.string
);
3874 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3875 contents
, rel
->r_offset
,
3876 relocation
, rel
->r_addend
);
3878 if (r
!= bfd_reloc_ok
)
3883 name
= h
->root
.root
.string
;
3886 name
= bfd_elf_string_from_elf_section (input_bfd
,
3887 symtab_hdr
->sh_link
,
3892 name
= bfd_section_name (input_bfd
, sec
);
3895 if (r
== bfd_reloc_overflow
)
3897 if (! ((*info
->callbacks
->reloc_overflow
)
3898 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3899 (bfd_vma
) 0, input_bfd
, input_section
,
3905 (*_bfd_error_handler
)
3906 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3907 input_bfd
, input_section
,
3908 (long) rel
->r_offset
, name
, (int) r
);
3917 /* Finish up dynamic symbol handling. We set the contents of various
3918 dynamic sections here. */
3921 elf64_x86_64_finish_dynamic_symbol (bfd
*output_bfd
,
3922 struct bfd_link_info
*info
,
3923 struct elf_link_hash_entry
*h
,
3924 Elf_Internal_Sym
*sym
)
3926 struct elf64_x86_64_link_hash_table
*htab
;
3928 htab
= elf64_x86_64_hash_table (info
);
3930 if (h
->plt
.offset
!= (bfd_vma
) -1)
3934 Elf_Internal_Rela rela
;
3936 asection
*plt
, *gotplt
, *relplt
;
3938 /* When building a static executable, use .iplt, .igot.plt and
3939 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3940 if (htab
->splt
!= NULL
)
3943 gotplt
= htab
->sgotplt
;
3944 relplt
= htab
->srelplt
;
3949 gotplt
= htab
->igotplt
;
3950 relplt
= htab
->irelplt
;
3953 /* This symbol has an entry in the procedure linkage table. Set
3955 if ((h
->dynindx
== -1
3956 && !((h
->forced_local
|| info
->executable
)
3958 && h
->type
== STT_GNU_IFUNC
))
3964 /* Get the index in the procedure linkage table which
3965 corresponds to this symbol. This is the index of this symbol
3966 in all the symbols for which we are making plt entries. The
3967 first entry in the procedure linkage table is reserved.
3969 Get the offset into the .got table of the entry that
3970 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3971 bytes. The first three are reserved for the dynamic linker.
3973 For static executables, we don't reserve anything. */
3975 if (plt
== htab
->splt
)
3977 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
3978 got_offset
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
3982 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
3983 got_offset
= plt_index
* GOT_ENTRY_SIZE
;
3986 /* Fill in the entry in the procedure linkage table. */
3987 memcpy (plt
->contents
+ h
->plt
.offset
, elf64_x86_64_plt_entry
,
3990 /* Insert the relocation positions of the plt section. The magic
3991 numbers at the end of the statements are the positions of the
3992 relocations in the plt section. */
3993 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3994 instruction uses 6 bytes, subtract this value. */
3995 bfd_put_32 (output_bfd
,
3996 (gotplt
->output_section
->vma
3997 + gotplt
->output_offset
3999 - plt
->output_section
->vma
4000 - plt
->output_offset
4003 plt
->contents
+ h
->plt
.offset
+ 2);
4005 /* Don't fill PLT entry for static executables. */
4006 if (plt
== htab
->splt
)
4008 /* Put relocation index. */
4009 bfd_put_32 (output_bfd
, plt_index
,
4010 plt
->contents
+ h
->plt
.offset
+ 7);
4011 /* Put offset for jmp .PLT0. */
4012 bfd_put_32 (output_bfd
, - (h
->plt
.offset
+ PLT_ENTRY_SIZE
),
4013 plt
->contents
+ h
->plt
.offset
+ 12);
4016 /* Fill in the entry in the global offset table, initially this
4017 points to the pushq instruction in the PLT which is at offset 6. */
4018 bfd_put_64 (output_bfd
, (plt
->output_section
->vma
4019 + plt
->output_offset
4020 + h
->plt
.offset
+ 6),
4021 gotplt
->contents
+ got_offset
);
4023 /* Fill in the entry in the .rela.plt section. */
4024 rela
.r_offset
= (gotplt
->output_section
->vma
4025 + gotplt
->output_offset
4027 if (h
->dynindx
== -1
4028 || ((info
->executable
4029 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
4031 && h
->type
== STT_GNU_IFUNC
))
4033 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4034 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
4035 rela
.r_info
= ELF64_R_INFO (0, R_X86_64_IRELATIVE
);
4036 rela
.r_addend
= (h
->root
.u
.def
.value
4037 + h
->root
.u
.def
.section
->output_section
->vma
4038 + h
->root
.u
.def
.section
->output_offset
);
4042 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_JUMP_SLOT
);
4045 loc
= relplt
->contents
+ plt_index
* sizeof (Elf64_External_Rela
);
4046 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
4048 if (!h
->def_regular
)
4050 /* Mark the symbol as undefined, rather than as defined in
4051 the .plt section. Leave the value if there were any
4052 relocations where pointer equality matters (this is a clue
4053 for the dynamic linker, to make function pointer
4054 comparisons work between an application and shared
4055 library), otherwise set it to zero. If a function is only
4056 called from a binary, there is no need to slow down
4057 shared libraries because of that. */
4058 sym
->st_shndx
= SHN_UNDEF
;
4059 if (!h
->pointer_equality_needed
)
4064 if (h
->got
.offset
!= (bfd_vma
) -1
4065 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h
)->tls_type
)
4066 && elf64_x86_64_hash_entry (h
)->tls_type
!= GOT_TLS_IE
)
4068 Elf_Internal_Rela rela
;
4071 /* This symbol has an entry in the global offset table. Set it
4073 if (htab
->sgot
== NULL
|| htab
->srelgot
== NULL
)
4076 rela
.r_offset
= (htab
->sgot
->output_section
->vma
4077 + htab
->sgot
->output_offset
4078 + (h
->got
.offset
&~ (bfd_vma
) 1));
4080 /* If this is a static link, or it is a -Bsymbolic link and the
4081 symbol is defined locally or was forced to be local because
4082 of a version file, we just want to emit a RELATIVE reloc.
4083 The entry in the global offset table will already have been
4084 initialized in the relocate_section function. */
4086 && h
->type
== STT_GNU_IFUNC
)
4090 /* Generate R_X86_64_GLOB_DAT. */
4095 if (!h
->pointer_equality_needed
)
4098 /* For non-shared object, we can't use .got.plt, which
4099 contains the real function addres if we need pointer
4100 equality. We load the GOT entry with the PLT entry. */
4101 asection
*plt
= htab
->splt
? htab
->splt
: htab
->iplt
;
4102 bfd_put_64 (output_bfd
, (plt
->output_section
->vma
4103 + plt
->output_offset
4105 htab
->sgot
->contents
+ h
->got
.offset
);
4109 else if (info
->shared
4110 && SYMBOL_REFERENCES_LOCAL (info
, h
))
4112 if (!h
->def_regular
)
4114 BFD_ASSERT((h
->got
.offset
& 1) != 0);
4115 rela
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
4116 rela
.r_addend
= (h
->root
.u
.def
.value
4117 + h
->root
.u
.def
.section
->output_section
->vma
4118 + h
->root
.u
.def
.section
->output_offset
);
4122 BFD_ASSERT((h
->got
.offset
& 1) == 0);
4124 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
4125 htab
->sgot
->contents
+ h
->got
.offset
);
4126 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_GLOB_DAT
);
4130 loc
= htab
->srelgot
->contents
;
4131 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf64_External_Rela
);
4132 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
4137 Elf_Internal_Rela rela
;
4140 /* This symbol needs a copy reloc. Set it up. */
4142 if (h
->dynindx
== -1
4143 || (h
->root
.type
!= bfd_link_hash_defined
4144 && h
->root
.type
!= bfd_link_hash_defweak
)
4145 || htab
->srelbss
== NULL
)
4148 rela
.r_offset
= (h
->root
.u
.def
.value
4149 + h
->root
.u
.def
.section
->output_section
->vma
4150 + h
->root
.u
.def
.section
->output_offset
);
4151 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_COPY
);
4153 loc
= htab
->srelbss
->contents
;
4154 loc
+= htab
->srelbss
->reloc_count
++ * sizeof (Elf64_External_Rela
);
4155 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
4158 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
4159 be NULL for local symbols. */
4161 && (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
4162 || h
== htab
->elf
.hgot
))
4163 sym
->st_shndx
= SHN_ABS
;
4168 /* Finish up local dynamic symbol handling. We set the contents of
4169 various dynamic sections here. */
4172 elf64_x86_64_finish_local_dynamic_symbol (void **slot
, void *inf
)
4174 struct elf_link_hash_entry
*h
4175 = (struct elf_link_hash_entry
*) *slot
;
4176 struct bfd_link_info
*info
4177 = (struct bfd_link_info
*) inf
;
4179 return elf64_x86_64_finish_dynamic_symbol (info
->output_bfd
,
4183 /* Used to decide how to sort relocs in an optimal manner for the
4184 dynamic linker, before writing them out. */
4186 static enum elf_reloc_type_class
4187 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela
*rela
)
4189 switch ((int) ELF64_R_TYPE (rela
->r_info
))
4191 case R_X86_64_RELATIVE
:
4192 return reloc_class_relative
;
4193 case R_X86_64_JUMP_SLOT
:
4194 return reloc_class_plt
;
4196 return reloc_class_copy
;
4198 return reloc_class_normal
;
4202 /* Finish up the dynamic sections. */
4205 elf64_x86_64_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
4207 struct elf64_x86_64_link_hash_table
*htab
;
4211 htab
= elf64_x86_64_hash_table (info
);
4212 dynobj
= htab
->elf
.dynobj
;
4213 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
4215 if (htab
->elf
.dynamic_sections_created
)
4217 Elf64_External_Dyn
*dyncon
, *dynconend
;
4219 if (sdyn
== NULL
|| htab
->sgot
== NULL
)
4222 dyncon
= (Elf64_External_Dyn
*) sdyn
->contents
;
4223 dynconend
= (Elf64_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
4224 for (; dyncon
< dynconend
; dyncon
++)
4226 Elf_Internal_Dyn dyn
;
4229 bfd_elf64_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4238 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
4242 dyn
.d_un
.d_ptr
= htab
->srelplt
->output_section
->vma
;
4246 s
= htab
->srelplt
->output_section
;
4247 dyn
.d_un
.d_val
= s
->size
;
4251 /* The procedure linkage table relocs (DT_JMPREL) should
4252 not be included in the overall relocs (DT_RELA).
4253 Therefore, we override the DT_RELASZ entry here to
4254 make it not include the JMPREL relocs. Since the
4255 linker script arranges for .rela.plt to follow all
4256 other relocation sections, we don't have to worry
4257 about changing the DT_RELA entry. */
4258 if (htab
->srelplt
!= NULL
)
4260 s
= htab
->srelplt
->output_section
;
4261 dyn
.d_un
.d_val
-= s
->size
;
4265 case DT_TLSDESC_PLT
:
4267 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
4268 + htab
->tlsdesc_plt
;
4271 case DT_TLSDESC_GOT
:
4273 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
4274 + htab
->tlsdesc_got
;
4278 bfd_elf64_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4281 /* Fill in the special first entry in the procedure linkage table. */
4282 if (htab
->splt
&& htab
->splt
->size
> 0)
4284 /* Fill in the first entry in the procedure linkage table. */
4285 memcpy (htab
->splt
->contents
, elf64_x86_64_plt0_entry
,
4287 /* Add offset for pushq GOT+8(%rip), since the instruction
4288 uses 6 bytes subtract this value. */
4289 bfd_put_32 (output_bfd
,
4290 (htab
->sgotplt
->output_section
->vma
4291 + htab
->sgotplt
->output_offset
4293 - htab
->splt
->output_section
->vma
4294 - htab
->splt
->output_offset
4296 htab
->splt
->contents
+ 2);
4297 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4298 the end of the instruction. */
4299 bfd_put_32 (output_bfd
,
4300 (htab
->sgotplt
->output_section
->vma
4301 + htab
->sgotplt
->output_offset
4303 - htab
->splt
->output_section
->vma
4304 - htab
->splt
->output_offset
4306 htab
->splt
->contents
+ 8);
4308 elf_section_data (htab
->splt
->output_section
)->this_hdr
.sh_entsize
=
4311 if (htab
->tlsdesc_plt
)
4313 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
4314 htab
->sgot
->contents
+ htab
->tlsdesc_got
);
4316 memcpy (htab
->splt
->contents
+ htab
->tlsdesc_plt
,
4317 elf64_x86_64_plt0_entry
,
4320 /* Add offset for pushq GOT+8(%rip), since the
4321 instruction uses 6 bytes subtract this value. */
4322 bfd_put_32 (output_bfd
,
4323 (htab
->sgotplt
->output_section
->vma
4324 + htab
->sgotplt
->output_offset
4326 - htab
->splt
->output_section
->vma
4327 - htab
->splt
->output_offset
4330 htab
->splt
->contents
+ htab
->tlsdesc_plt
+ 2);
4331 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4332 htab->tlsdesc_got. The 12 is the offset to the end of
4334 bfd_put_32 (output_bfd
,
4335 (htab
->sgot
->output_section
->vma
4336 + htab
->sgot
->output_offset
4338 - htab
->splt
->output_section
->vma
4339 - htab
->splt
->output_offset
4342 htab
->splt
->contents
+ htab
->tlsdesc_plt
+ 8);
4349 /* Fill in the first three entries in the global offset table. */
4350 if (htab
->sgotplt
->size
> 0)
4352 /* Set the first entry in the global offset table to the address of
4353 the dynamic section. */
4355 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
);
4357 bfd_put_64 (output_bfd
,
4358 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
4359 htab
->sgotplt
->contents
);
4360 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4361 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
);
4362 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
*2);
4365 elf_section_data (htab
->sgotplt
->output_section
)->this_hdr
.sh_entsize
=
4369 if (htab
->sgot
&& htab
->sgot
->size
> 0)
4370 elf_section_data (htab
->sgot
->output_section
)->this_hdr
.sh_entsize
4373 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4374 htab_traverse (htab
->loc_hash_table
,
4375 elf64_x86_64_finish_local_dynamic_symbol
,
4381 /* Return address for Ith PLT stub in section PLT, for relocation REL
4382 or (bfd_vma) -1 if it should not be included. */
4385 elf64_x86_64_plt_sym_val (bfd_vma i
, const asection
*plt
,
4386 const arelent
*rel ATTRIBUTE_UNUSED
)
4388 return plt
->vma
+ (i
+ 1) * PLT_ENTRY_SIZE
;
4391 /* Handle an x86-64 specific section when reading an object file. This
4392 is called when elfcode.h finds a section with an unknown type. */
4395 elf64_x86_64_section_from_shdr (bfd
*abfd
,
4396 Elf_Internal_Shdr
*hdr
,
4400 if (hdr
->sh_type
!= SHT_X86_64_UNWIND
)
4403 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
4409 /* Hook called by the linker routine which adds symbols from an object
4410 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4414 elf64_x86_64_add_symbol_hook (bfd
*abfd
,
4415 struct bfd_link_info
*info
,
4416 Elf_Internal_Sym
*sym
,
4417 const char **namep ATTRIBUTE_UNUSED
,
4418 flagword
*flagsp ATTRIBUTE_UNUSED
,
4424 switch (sym
->st_shndx
)
4426 case SHN_X86_64_LCOMMON
:
4427 lcomm
= bfd_get_section_by_name (abfd
, "LARGE_COMMON");
4430 lcomm
= bfd_make_section_with_flags (abfd
,
4434 | SEC_LINKER_CREATED
));
4437 elf_section_flags (lcomm
) |= SHF_X86_64_LARGE
;
4440 *valp
= sym
->st_size
;
4444 if (ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
4445 elf_tdata (info
->output_bfd
)->has_ifunc_symbols
= TRUE
;
4451 /* Given a BFD section, try to locate the corresponding ELF section
4455 elf64_x86_64_elf_section_from_bfd_section (bfd
*abfd ATTRIBUTE_UNUSED
,
4456 asection
*sec
, int *index
)
4458 if (sec
== &_bfd_elf_large_com_section
)
4460 *index
= SHN_X86_64_LCOMMON
;
4466 /* Process a symbol. */
4469 elf64_x86_64_symbol_processing (bfd
*abfd ATTRIBUTE_UNUSED
,
4472 elf_symbol_type
*elfsym
= (elf_symbol_type
*) asym
;
4474 switch (elfsym
->internal_elf_sym
.st_shndx
)
4476 case SHN_X86_64_LCOMMON
:
4477 asym
->section
= &_bfd_elf_large_com_section
;
4478 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
4479 /* Common symbol doesn't set BSF_GLOBAL. */
4480 asym
->flags
&= ~BSF_GLOBAL
;
4486 elf64_x86_64_common_definition (Elf_Internal_Sym
*sym
)
4488 return (sym
->st_shndx
== SHN_COMMON
4489 || sym
->st_shndx
== SHN_X86_64_LCOMMON
);
4493 elf64_x86_64_common_section_index (asection
*sec
)
4495 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
4498 return SHN_X86_64_LCOMMON
;
4502 elf64_x86_64_common_section (asection
*sec
)
4504 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
4505 return bfd_com_section_ptr
;
4507 return &_bfd_elf_large_com_section
;
4511 elf64_x86_64_merge_symbol (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
4512 struct elf_link_hash_entry
**sym_hash ATTRIBUTE_UNUSED
,
4513 struct elf_link_hash_entry
*h
,
4514 Elf_Internal_Sym
*sym
,
4516 bfd_vma
*pvalue ATTRIBUTE_UNUSED
,
4517 unsigned int *pold_alignment ATTRIBUTE_UNUSED
,
4518 bfd_boolean
*skip ATTRIBUTE_UNUSED
,
4519 bfd_boolean
*override ATTRIBUTE_UNUSED
,
4520 bfd_boolean
*type_change_ok ATTRIBUTE_UNUSED
,
4521 bfd_boolean
*size_change_ok ATTRIBUTE_UNUSED
,
4522 bfd_boolean
*newdef ATTRIBUTE_UNUSED
,
4523 bfd_boolean
*newdyn
,
4524 bfd_boolean
*newdyncommon ATTRIBUTE_UNUSED
,
4525 bfd_boolean
*newweak ATTRIBUTE_UNUSED
,
4526 bfd
*abfd ATTRIBUTE_UNUSED
,
4528 bfd_boolean
*olddef ATTRIBUTE_UNUSED
,
4529 bfd_boolean
*olddyn
,
4530 bfd_boolean
*olddyncommon ATTRIBUTE_UNUSED
,
4531 bfd_boolean
*oldweak ATTRIBUTE_UNUSED
,
4535 /* A normal common symbol and a large common symbol result in a
4536 normal common symbol. We turn the large common symbol into a
4539 && h
->root
.type
== bfd_link_hash_common
4541 && bfd_is_com_section (*sec
)
4544 if (sym
->st_shndx
== SHN_COMMON
4545 && (elf_section_flags (*oldsec
) & SHF_X86_64_LARGE
) != 0)
4547 h
->root
.u
.c
.p
->section
4548 = bfd_make_section_old_way (oldbfd
, "COMMON");
4549 h
->root
.u
.c
.p
->section
->flags
= SEC_ALLOC
;
4551 else if (sym
->st_shndx
== SHN_X86_64_LCOMMON
4552 && (elf_section_flags (*oldsec
) & SHF_X86_64_LARGE
) == 0)
4553 *psec
= *sec
= bfd_com_section_ptr
;
4560 elf64_x86_64_additional_program_headers (bfd
*abfd
,
4561 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
4566 /* Check to see if we need a large readonly segment. */
4567 s
= bfd_get_section_by_name (abfd
, ".lrodata");
4568 if (s
&& (s
->flags
& SEC_LOAD
))
4571 /* Check to see if we need a large data segment. Since .lbss sections
4572 is placed right after the .bss section, there should be no need for
4573 a large data segment just because of .lbss. */
4574 s
= bfd_get_section_by_name (abfd
, ".ldata");
4575 if (s
&& (s
->flags
& SEC_LOAD
))
4581 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4584 elf64_x86_64_hash_symbol (struct elf_link_hash_entry
*h
)
4586 if (h
->plt
.offset
!= (bfd_vma
) -1
4588 && !h
->pointer_equality_needed
)
4591 return _bfd_elf_hash_symbol (h
);
4594 static const struct bfd_elf_special_section
4595 elf64_x86_64_special_sections
[]=
4597 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
4598 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
4599 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
+ SHF_X86_64_LARGE
},
4600 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
4601 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
4602 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
4603 { NULL
, 0, 0, 0, 0 }
4606 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
4607 #define TARGET_LITTLE_NAME "elf64-x86-64"
4608 #define ELF_ARCH bfd_arch_i386
4609 #define ELF_MACHINE_CODE EM_X86_64
4610 #define ELF_MAXPAGESIZE 0x200000
4611 #define ELF_MINPAGESIZE 0x1000
4612 #define ELF_COMMONPAGESIZE 0x1000
4614 #define elf_backend_can_gc_sections 1
4615 #define elf_backend_can_refcount 1
4616 #define elf_backend_want_got_plt 1
4617 #define elf_backend_plt_readonly 1
4618 #define elf_backend_want_plt_sym 0
4619 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
4620 #define elf_backend_rela_normal 1
4622 #define elf_info_to_howto elf64_x86_64_info_to_howto
4624 #define bfd_elf64_bfd_link_hash_table_create \
4625 elf64_x86_64_link_hash_table_create
4626 #define bfd_elf64_bfd_link_hash_table_free \
4627 elf64_x86_64_link_hash_table_free
4628 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
4629 #define bfd_elf64_bfd_reloc_name_lookup \
4630 elf64_x86_64_reloc_name_lookup
4632 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
4633 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4634 #define elf_backend_check_relocs elf64_x86_64_check_relocs
4635 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
4636 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
4637 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
4638 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
4639 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
4640 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
4641 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
4642 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
4643 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
4644 #define elf_backend_relocate_section elf64_x86_64_relocate_section
4645 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
4646 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4647 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4648 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
4649 #define elf_backend_object_p elf64_x86_64_elf_object_p
4650 #define bfd_elf64_mkobject elf64_x86_64_mkobject
4652 #define elf_backend_section_from_shdr \
4653 elf64_x86_64_section_from_shdr
4655 #define elf_backend_section_from_bfd_section \
4656 elf64_x86_64_elf_section_from_bfd_section
4657 #define elf_backend_add_symbol_hook \
4658 elf64_x86_64_add_symbol_hook
4659 #define elf_backend_symbol_processing \
4660 elf64_x86_64_symbol_processing
4661 #define elf_backend_common_section_index \
4662 elf64_x86_64_common_section_index
4663 #define elf_backend_common_section \
4664 elf64_x86_64_common_section
4665 #define elf_backend_common_definition \
4666 elf64_x86_64_common_definition
4667 #define elf_backend_merge_symbol \
4668 elf64_x86_64_merge_symbol
4669 #define elf_backend_special_sections \
4670 elf64_x86_64_special_sections
4671 #define elf_backend_additional_program_headers \
4672 elf64_x86_64_additional_program_headers
4673 #define elf_backend_hash_symbol \
4674 elf64_x86_64_hash_symbol
4676 #undef elf_backend_post_process_headers
4677 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4679 #include "elf64-target.h"
4681 /* FreeBSD support. */
4683 #undef TARGET_LITTLE_SYM
4684 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
4685 #undef TARGET_LITTLE_NAME
4686 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
4689 #define ELF_OSABI ELFOSABI_FREEBSD
4692 #define elf64_bed elf64_x86_64_fbsd_bed
4694 #include "elf64-target.h"