1 /* X86-64 specific support for ELF
2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "elfxx-x86.h"
24 #include "libiberty.h"
26 #include "opcode/i386.h"
33 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
34 #define MINUS_ONE (~ (bfd_vma) 0)
36 /* Since both 32-bit and 64-bit x86-64 encode relocation type in the
37 identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
38 relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
39 since they are the same. */
41 /* The relocation "howto" table. Order of fields:
42 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
43 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
44 static reloc_howto_type x86_64_elf_howto_table
[] =
46 HOWTO(R_X86_64_NONE
, 0, 0, 0, false, 0, complain_overflow_dont
,
47 bfd_elf_generic_reloc
, "R_X86_64_NONE", false, 0, 0x00000000,
49 HOWTO(R_X86_64_64
, 0, 8, 64, false, 0, complain_overflow_dont
,
50 bfd_elf_generic_reloc
, "R_X86_64_64", false, 0, MINUS_ONE
,
52 HOWTO(R_X86_64_PC32
, 0, 4, 32, true, 0, complain_overflow_signed
,
53 bfd_elf_generic_reloc
, "R_X86_64_PC32", false, 0, 0xffffffff,
55 HOWTO(R_X86_64_GOT32
, 0, 4, 32, false, 0, complain_overflow_signed
,
56 bfd_elf_generic_reloc
, "R_X86_64_GOT32", false, 0, 0xffffffff,
58 HOWTO(R_X86_64_PLT32
, 0, 4, 32, true, 0, complain_overflow_signed
,
59 bfd_elf_generic_reloc
, "R_X86_64_PLT32", false, 0, 0xffffffff,
61 HOWTO(R_X86_64_COPY
, 0, 4, 32, false, 0, complain_overflow_bitfield
,
62 bfd_elf_generic_reloc
, "R_X86_64_COPY", false, 0, 0xffffffff,
64 HOWTO(R_X86_64_GLOB_DAT
, 0, 8, 64, false, 0, complain_overflow_dont
,
65 bfd_elf_generic_reloc
, "R_X86_64_GLOB_DAT", false, 0, MINUS_ONE
,
67 HOWTO(R_X86_64_JUMP_SLOT
, 0, 8, 64, false, 0, complain_overflow_dont
,
68 bfd_elf_generic_reloc
, "R_X86_64_JUMP_SLOT", false, 0, MINUS_ONE
,
70 HOWTO(R_X86_64_RELATIVE
, 0, 8, 64, false, 0, complain_overflow_dont
,
71 bfd_elf_generic_reloc
, "R_X86_64_RELATIVE", false, 0, MINUS_ONE
,
73 HOWTO(R_X86_64_GOTPCREL
, 0, 4, 32, true, 0, complain_overflow_signed
,
74 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL", false, 0, 0xffffffff,
76 HOWTO(R_X86_64_32
, 0, 4, 32, false, 0, complain_overflow_unsigned
,
77 bfd_elf_generic_reloc
, "R_X86_64_32", false, 0, 0xffffffff,
79 HOWTO(R_X86_64_32S
, 0, 4, 32, false, 0, complain_overflow_signed
,
80 bfd_elf_generic_reloc
, "R_X86_64_32S", false, 0, 0xffffffff,
82 HOWTO(R_X86_64_16
, 0, 2, 16, false, 0, complain_overflow_bitfield
,
83 bfd_elf_generic_reloc
, "R_X86_64_16", false, 0, 0xffff, false),
84 HOWTO(R_X86_64_PC16
, 0, 2, 16, true, 0, complain_overflow_bitfield
,
85 bfd_elf_generic_reloc
, "R_X86_64_PC16", false, 0, 0xffff, true),
86 HOWTO(R_X86_64_8
, 0, 1, 8, false, 0, complain_overflow_bitfield
,
87 bfd_elf_generic_reloc
, "R_X86_64_8", false, 0, 0xff, false),
88 HOWTO(R_X86_64_PC8
, 0, 1, 8, true, 0, complain_overflow_signed
,
89 bfd_elf_generic_reloc
, "R_X86_64_PC8", false, 0, 0xff, true),
90 HOWTO(R_X86_64_DTPMOD64
, 0, 8, 64, false, 0, complain_overflow_dont
,
91 bfd_elf_generic_reloc
, "R_X86_64_DTPMOD64", false, 0, MINUS_ONE
,
93 HOWTO(R_X86_64_DTPOFF64
, 0, 8, 64, false, 0, complain_overflow_dont
,
94 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF64", false, 0, MINUS_ONE
,
96 HOWTO(R_X86_64_TPOFF64
, 0, 8, 64, false, 0, complain_overflow_dont
,
97 bfd_elf_generic_reloc
, "R_X86_64_TPOFF64", false, 0, MINUS_ONE
,
99 HOWTO(R_X86_64_TLSGD
, 0, 4, 32, true, 0, complain_overflow_signed
,
100 bfd_elf_generic_reloc
, "R_X86_64_TLSGD", false, 0, 0xffffffff,
102 HOWTO(R_X86_64_TLSLD
, 0, 4, 32, true, 0, complain_overflow_signed
,
103 bfd_elf_generic_reloc
, "R_X86_64_TLSLD", false, 0, 0xffffffff,
105 HOWTO(R_X86_64_DTPOFF32
, 0, 4, 32, false, 0, complain_overflow_signed
,
106 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF32", false, 0, 0xffffffff,
108 HOWTO(R_X86_64_GOTTPOFF
, 0, 4, 32, true, 0, complain_overflow_signed
,
109 bfd_elf_generic_reloc
, "R_X86_64_GOTTPOFF", false, 0, 0xffffffff,
111 HOWTO(R_X86_64_TPOFF32
, 0, 4, 32, false, 0, complain_overflow_signed
,
112 bfd_elf_generic_reloc
, "R_X86_64_TPOFF32", false, 0, 0xffffffff,
114 HOWTO(R_X86_64_PC64
, 0, 8, 64, true, 0, complain_overflow_dont
,
115 bfd_elf_generic_reloc
, "R_X86_64_PC64", false, 0, MINUS_ONE
,
117 HOWTO(R_X86_64_GOTOFF64
, 0, 8, 64, false, 0, complain_overflow_dont
,
118 bfd_elf_generic_reloc
, "R_X86_64_GOTOFF64", false, 0, MINUS_ONE
,
120 HOWTO(R_X86_64_GOTPC32
, 0, 4, 32, true, 0, complain_overflow_signed
,
121 bfd_elf_generic_reloc
, "R_X86_64_GOTPC32", false, 0, 0xffffffff,
123 HOWTO(R_X86_64_GOT64
, 0, 8, 64, false, 0, complain_overflow_signed
,
124 bfd_elf_generic_reloc
, "R_X86_64_GOT64", false, 0, MINUS_ONE
,
126 HOWTO(R_X86_64_GOTPCREL64
, 0, 8, 64, true, 0, complain_overflow_signed
,
127 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL64", false, 0, MINUS_ONE
,
129 HOWTO(R_X86_64_GOTPC64
, 0, 8, 64, true, 0, complain_overflow_signed
,
130 bfd_elf_generic_reloc
, "R_X86_64_GOTPC64", false, 0, MINUS_ONE
,
132 HOWTO(R_X86_64_GOTPLT64
, 0, 8, 64, false, 0, complain_overflow_signed
,
133 bfd_elf_generic_reloc
, "R_X86_64_GOTPLT64", false, 0, MINUS_ONE
,
135 HOWTO(R_X86_64_PLTOFF64
, 0, 8, 64, false, 0, complain_overflow_signed
,
136 bfd_elf_generic_reloc
, "R_X86_64_PLTOFF64", false, 0, MINUS_ONE
,
138 HOWTO(R_X86_64_SIZE32
, 0, 4, 32, false, 0, complain_overflow_unsigned
,
139 bfd_elf_generic_reloc
, "R_X86_64_SIZE32", false, 0, 0xffffffff,
141 HOWTO(R_X86_64_SIZE64
, 0, 8, 64, false, 0, complain_overflow_dont
,
142 bfd_elf_generic_reloc
, "R_X86_64_SIZE64", false, 0, MINUS_ONE
,
144 HOWTO(R_X86_64_GOTPC32_TLSDESC
, 0, 4, 32, true, 0,
145 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
146 "R_X86_64_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
147 HOWTO(R_X86_64_TLSDESC_CALL
, 0, 0, 0, false, 0,
148 complain_overflow_dont
, bfd_elf_generic_reloc
,
149 "R_X86_64_TLSDESC_CALL",
151 HOWTO(R_X86_64_TLSDESC
, 0, 8, 64, false, 0,
152 complain_overflow_dont
, bfd_elf_generic_reloc
,
153 "R_X86_64_TLSDESC", false, 0, MINUS_ONE
, false),
154 HOWTO(R_X86_64_IRELATIVE
, 0, 8, 64, false, 0, complain_overflow_dont
,
155 bfd_elf_generic_reloc
, "R_X86_64_IRELATIVE", false, 0, MINUS_ONE
,
157 HOWTO(R_X86_64_RELATIVE64
, 0, 8, 64, false, 0, complain_overflow_dont
,
158 bfd_elf_generic_reloc
, "R_X86_64_RELATIVE64", false, 0, MINUS_ONE
,
160 HOWTO(R_X86_64_PC32_BND
, 0, 4, 32, true, 0, complain_overflow_signed
,
161 bfd_elf_generic_reloc
, "R_X86_64_PC32_BND", false, 0, 0xffffffff,
163 HOWTO(R_X86_64_PLT32_BND
, 0, 4, 32, true, 0, complain_overflow_signed
,
164 bfd_elf_generic_reloc
, "R_X86_64_PLT32_BND", false, 0, 0xffffffff,
166 HOWTO(R_X86_64_GOTPCRELX
, 0, 4, 32, true, 0, complain_overflow_signed
,
167 bfd_elf_generic_reloc
, "R_X86_64_GOTPCRELX", false, 0, 0xffffffff,
169 HOWTO(R_X86_64_REX_GOTPCRELX
, 0, 4, 32, true, 0, complain_overflow_signed
,
170 bfd_elf_generic_reloc
, "R_X86_64_REX_GOTPCRELX", false, 0, 0xffffffff,
173 /* We have a gap in the reloc numbers here.
174 R_X86_64_standard counts the number up to this point, and
175 R_X86_64_vt_offset is the value to subtract from a reloc type of
176 R_X86_64_GNU_VT* to form an index into this table. */
177 #define R_X86_64_standard (R_X86_64_REX_GOTPCRELX + 1)
178 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
180 /* GNU extension to record C++ vtable hierarchy. */
181 HOWTO (R_X86_64_GNU_VTINHERIT
, 0, 8, 0, false, 0, complain_overflow_dont
,
182 NULL
, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
184 /* GNU extension to record C++ vtable member usage. */
185 HOWTO (R_X86_64_GNU_VTENTRY
, 0, 8, 0, false, 0, complain_overflow_dont
,
186 _bfd_elf_rel_vtable_reloc_fn
, "R_X86_64_GNU_VTENTRY", false, 0, 0,
189 /* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
190 HOWTO(R_X86_64_32
, 0, 4, 32, false, 0, complain_overflow_bitfield
,
191 bfd_elf_generic_reloc
, "R_X86_64_32", false, 0, 0xffffffff,
195 /* Map BFD relocs to the x86_64 elf relocs. */
198 bfd_reloc_code_real_type bfd_reloc_val
;
199 unsigned char elf_reloc_val
;
202 static const struct elf_reloc_map x86_64_reloc_map
[] =
204 { BFD_RELOC_NONE
, R_X86_64_NONE
, },
205 { BFD_RELOC_64
, R_X86_64_64
, },
206 { BFD_RELOC_32_PCREL
, R_X86_64_PC32
, },
207 { BFD_RELOC_X86_64_GOT32
, R_X86_64_GOT32
,},
208 { BFD_RELOC_X86_64_PLT32
, R_X86_64_PLT32
,},
209 { BFD_RELOC_X86_64_COPY
, R_X86_64_COPY
, },
210 { BFD_RELOC_X86_64_GLOB_DAT
, R_X86_64_GLOB_DAT
, },
211 { BFD_RELOC_X86_64_JUMP_SLOT
, R_X86_64_JUMP_SLOT
, },
212 { BFD_RELOC_X86_64_RELATIVE
, R_X86_64_RELATIVE
, },
213 { BFD_RELOC_X86_64_GOTPCREL
, R_X86_64_GOTPCREL
, },
214 { BFD_RELOC_32
, R_X86_64_32
, },
215 { BFD_RELOC_X86_64_32S
, R_X86_64_32S
, },
216 { BFD_RELOC_16
, R_X86_64_16
, },
217 { BFD_RELOC_16_PCREL
, R_X86_64_PC16
, },
218 { BFD_RELOC_8
, R_X86_64_8
, },
219 { BFD_RELOC_8_PCREL
, R_X86_64_PC8
, },
220 { BFD_RELOC_X86_64_DTPMOD64
, R_X86_64_DTPMOD64
, },
221 { BFD_RELOC_X86_64_DTPOFF64
, R_X86_64_DTPOFF64
, },
222 { BFD_RELOC_X86_64_TPOFF64
, R_X86_64_TPOFF64
, },
223 { BFD_RELOC_X86_64_TLSGD
, R_X86_64_TLSGD
, },
224 { BFD_RELOC_X86_64_TLSLD
, R_X86_64_TLSLD
, },
225 { BFD_RELOC_X86_64_DTPOFF32
, R_X86_64_DTPOFF32
, },
226 { BFD_RELOC_X86_64_GOTTPOFF
, R_X86_64_GOTTPOFF
, },
227 { BFD_RELOC_X86_64_TPOFF32
, R_X86_64_TPOFF32
, },
228 { BFD_RELOC_64_PCREL
, R_X86_64_PC64
, },
229 { BFD_RELOC_X86_64_GOTOFF64
, R_X86_64_GOTOFF64
, },
230 { BFD_RELOC_X86_64_GOTPC32
, R_X86_64_GOTPC32
, },
231 { BFD_RELOC_X86_64_GOT64
, R_X86_64_GOT64
, },
232 { BFD_RELOC_X86_64_GOTPCREL64
,R_X86_64_GOTPCREL64
, },
233 { BFD_RELOC_X86_64_GOTPC64
, R_X86_64_GOTPC64
, },
234 { BFD_RELOC_X86_64_GOTPLT64
, R_X86_64_GOTPLT64
, },
235 { BFD_RELOC_X86_64_PLTOFF64
, R_X86_64_PLTOFF64
, },
236 { BFD_RELOC_SIZE32
, R_X86_64_SIZE32
, },
237 { BFD_RELOC_SIZE64
, R_X86_64_SIZE64
, },
238 { BFD_RELOC_X86_64_GOTPC32_TLSDESC
, R_X86_64_GOTPC32_TLSDESC
, },
239 { BFD_RELOC_X86_64_TLSDESC_CALL
, R_X86_64_TLSDESC_CALL
, },
240 { BFD_RELOC_X86_64_TLSDESC
, R_X86_64_TLSDESC
, },
241 { BFD_RELOC_X86_64_IRELATIVE
, R_X86_64_IRELATIVE
, },
242 { BFD_RELOC_X86_64_PC32_BND
, R_X86_64_PC32_BND
, },
243 { BFD_RELOC_X86_64_PLT32_BND
, R_X86_64_PLT32_BND
, },
244 { BFD_RELOC_X86_64_GOTPCRELX
, R_X86_64_GOTPCRELX
, },
245 { BFD_RELOC_X86_64_REX_GOTPCRELX
, R_X86_64_REX_GOTPCRELX
, },
246 { BFD_RELOC_VTABLE_INHERIT
, R_X86_64_GNU_VTINHERIT
, },
247 { BFD_RELOC_VTABLE_ENTRY
, R_X86_64_GNU_VTENTRY
, },
250 static reloc_howto_type
*
251 elf_x86_64_rtype_to_howto (bfd
*abfd
, unsigned r_type
)
255 if (r_type
== (unsigned int) R_X86_64_32
)
260 i
= ARRAY_SIZE (x86_64_elf_howto_table
) - 1;
262 else if (r_type
< (unsigned int) R_X86_64_GNU_VTINHERIT
263 || r_type
>= (unsigned int) R_X86_64_max
)
265 if (r_type
>= (unsigned int) R_X86_64_standard
)
267 /* xgettext:c-format */
268 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
270 bfd_set_error (bfd_error_bad_value
);
276 i
= r_type
- (unsigned int) R_X86_64_vt_offset
;
277 BFD_ASSERT (x86_64_elf_howto_table
[i
].type
== r_type
);
278 return &x86_64_elf_howto_table
[i
];
281 /* Given a BFD reloc type, return a HOWTO structure. */
282 static reloc_howto_type
*
283 elf_x86_64_reloc_type_lookup (bfd
*abfd
,
284 bfd_reloc_code_real_type code
)
288 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
291 if (x86_64_reloc_map
[i
].bfd_reloc_val
== code
)
292 return elf_x86_64_rtype_to_howto (abfd
,
293 x86_64_reloc_map
[i
].elf_reloc_val
);
298 static reloc_howto_type
*
299 elf_x86_64_reloc_name_lookup (bfd
*abfd
,
304 if (!ABI_64_P (abfd
) && strcasecmp (r_name
, "R_X86_64_32") == 0)
306 /* Get x32 R_X86_64_32. */
307 reloc_howto_type
*reloc
308 = &x86_64_elf_howto_table
[ARRAY_SIZE (x86_64_elf_howto_table
) - 1];
309 BFD_ASSERT (reloc
->type
== (unsigned int) R_X86_64_32
);
313 for (i
= 0; i
< ARRAY_SIZE (x86_64_elf_howto_table
); i
++)
314 if (x86_64_elf_howto_table
[i
].name
!= NULL
315 && strcasecmp (x86_64_elf_howto_table
[i
].name
, r_name
) == 0)
316 return &x86_64_elf_howto_table
[i
];
321 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
324 elf_x86_64_info_to_howto (bfd
*abfd
, arelent
*cache_ptr
,
325 Elf_Internal_Rela
*dst
)
329 r_type
= ELF32_R_TYPE (dst
->r_info
);
330 cache_ptr
->howto
= elf_x86_64_rtype_to_howto (abfd
, r_type
);
331 if (cache_ptr
->howto
== NULL
)
333 BFD_ASSERT (r_type
== cache_ptr
->howto
->type
|| cache_ptr
->howto
->type
== R_X86_64_NONE
);
337 /* Support for core dump NOTE sections. */
339 elf_x86_64_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
344 switch (note
->descsz
)
349 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
351 elf_tdata (abfd
)->core
->signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
354 elf_tdata (abfd
)->core
->lwpid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
362 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
364 elf_tdata (abfd
)->core
->signal
365 = bfd_get_16 (abfd
, note
->descdata
+ 12);
368 elf_tdata (abfd
)->core
->lwpid
369 = bfd_get_32 (abfd
, note
->descdata
+ 32);
378 /* Make a ".reg/999" section. */
379 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
380 size
, note
->descpos
+ offset
);
384 elf_x86_64_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
386 switch (note
->descsz
)
391 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
392 elf_tdata (abfd
)->core
->pid
393 = bfd_get_32 (abfd
, note
->descdata
+ 12);
394 elf_tdata (abfd
)->core
->program
395 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 28, 16);
396 elf_tdata (abfd
)->core
->command
397 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 44, 80);
400 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
401 elf_tdata (abfd
)->core
->pid
402 = bfd_get_32 (abfd
, note
->descdata
+ 24);
403 elf_tdata (abfd
)->core
->program
404 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 40, 16);
405 elf_tdata (abfd
)->core
->command
406 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 56, 80);
409 /* Note that for some reason, a spurious space is tacked
410 onto the end of the args in some (at least one anyway)
411 implementations, so strip it off if it exists. */
414 char *command
= elf_tdata (abfd
)->core
->command
;
415 int n
= strlen (command
);
417 if (0 < n
&& command
[n
- 1] == ' ')
418 command
[n
- 1] = '\0';
425 # if GCC_VERSION >= 8000
426 # pragma GCC diagnostic push
427 # pragma GCC diagnostic ignored "-Wstringop-truncation"
430 elf_x86_64_write_core_note (bfd
*abfd
, char *buf
, int *bufsiz
,
433 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
435 const char *fname
, *psargs
;
446 va_start (ap
, note_type
);
447 fname
= va_arg (ap
, const char *);
448 psargs
= va_arg (ap
, const char *);
451 if (bed
->s
->elfclass
== ELFCLASS32
)
454 memset (&data
, 0, sizeof (data
));
455 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
456 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
457 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", note_type
,
458 &data
, sizeof (data
));
463 memset (&data
, 0, sizeof (data
));
464 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
465 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
466 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", note_type
,
467 &data
, sizeof (data
));
472 va_start (ap
, note_type
);
473 pid
= va_arg (ap
, long);
474 cursig
= va_arg (ap
, int);
475 gregs
= va_arg (ap
, const void *);
478 if (bed
->s
->elfclass
== ELFCLASS32
)
480 if (bed
->elf_machine_code
== EM_X86_64
)
482 prstatusx32_t prstat
;
483 memset (&prstat
, 0, sizeof (prstat
));
485 prstat
.pr_cursig
= cursig
;
486 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
487 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", note_type
,
488 &prstat
, sizeof (prstat
));
493 memset (&prstat
, 0, sizeof (prstat
));
495 prstat
.pr_cursig
= cursig
;
496 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
497 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", note_type
,
498 &prstat
, sizeof (prstat
));
504 memset (&prstat
, 0, sizeof (prstat
));
506 prstat
.pr_cursig
= cursig
;
507 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
508 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", note_type
,
509 &prstat
, sizeof (prstat
));
514 # if GCC_VERSION >= 8000
515 # pragma GCC diagnostic pop
519 /* Functions for the x86-64 ELF linker. */
521 /* The size in bytes of an entry in the global offset table. */
523 #define GOT_ENTRY_SIZE 8
525 /* The size in bytes of an entry in the lazy procedure linkage table. */
527 #define LAZY_PLT_ENTRY_SIZE 16
529 /* The size in bytes of an entry in the non-lazy procedure linkage
532 #define NON_LAZY_PLT_ENTRY_SIZE 8
534 /* The first entry in a lazy procedure linkage table looks like this.
535 See the SVR4 ABI i386 supplement and the x86-64 ABI to see how this
538 static const bfd_byte elf_x86_64_lazy_plt0_entry
[LAZY_PLT_ENTRY_SIZE
] =
540 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
541 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
542 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
545 /* Subsequent entries in a lazy procedure linkage table look like this. */
547 static const bfd_byte elf_x86_64_lazy_plt_entry
[LAZY_PLT_ENTRY_SIZE
] =
549 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
550 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
551 0x68, /* pushq immediate */
552 0, 0, 0, 0, /* replaced with index into relocation table. */
553 0xe9, /* jmp relative */
554 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
557 /* The first entry in a lazy procedure linkage table with BND prefix
560 static const bfd_byte elf_x86_64_lazy_bnd_plt0_entry
[LAZY_PLT_ENTRY_SIZE
] =
562 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
563 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
564 0x0f, 0x1f, 0 /* nopl (%rax) */
567 /* Subsequent entries for branches with BND prefx in a lazy procedure
568 linkage table look like this. */
570 static const bfd_byte elf_x86_64_lazy_bnd_plt_entry
[LAZY_PLT_ENTRY_SIZE
] =
572 0x68, 0, 0, 0, 0, /* pushq immediate */
573 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
574 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
577 /* The first entry in the IBT-enabled lazy procedure linkage table is the
578 the same as the lazy PLT with BND prefix so that bound registers are
579 preserved when control is passed to dynamic linker. Subsequent
580 entries for a IBT-enabled lazy procedure linkage table look like
583 static const bfd_byte elf_x86_64_lazy_ibt_plt_entry
[LAZY_PLT_ENTRY_SIZE
] =
585 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
586 0x68, 0, 0, 0, 0, /* pushq immediate */
587 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
591 /* The first entry in the x32 IBT-enabled lazy procedure linkage table
592 is the same as the normal lazy PLT. Subsequent entries for an
593 x32 IBT-enabled lazy procedure linkage table look like this. */
595 static const bfd_byte elf_x32_lazy_ibt_plt_entry
[LAZY_PLT_ENTRY_SIZE
] =
597 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
598 0x68, 0, 0, 0, 0, /* pushq immediate */
599 0xe9, 0, 0, 0, 0, /* jmpq relative */
600 0x66, 0x90 /* xchg %ax,%ax */
603 /* Entries in the non-lazey procedure linkage table look like this. */
605 static const bfd_byte elf_x86_64_non_lazy_plt_entry
[NON_LAZY_PLT_ENTRY_SIZE
] =
607 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
608 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
609 0x66, 0x90 /* xchg %ax,%ax */
612 /* Entries for branches with BND prefix in the non-lazey procedure
613 linkage table look like this. */
615 static const bfd_byte elf_x86_64_non_lazy_bnd_plt_entry
[NON_LAZY_PLT_ENTRY_SIZE
] =
617 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
618 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
622 /* Entries for branches with IBT-enabled in the non-lazey procedure
623 linkage table look like this. They have the same size as the lazy
626 static const bfd_byte elf_x86_64_non_lazy_ibt_plt_entry
[LAZY_PLT_ENTRY_SIZE
] =
628 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
629 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
630 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
631 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopl 0x0(%rax,%rax,1) */
634 /* Entries for branches with IBT-enabled in the x32 non-lazey procedure
635 linkage table look like this. They have the same size as the lazy
638 static const bfd_byte elf_x32_non_lazy_ibt_plt_entry
[LAZY_PLT_ENTRY_SIZE
] =
640 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
641 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
642 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
643 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
646 /* The TLSDESC entry in a lazy procedure linkage table. */
647 static const bfd_byte elf_x86_64_tlsdesc_plt_entry
[LAZY_PLT_ENTRY_SIZE
] =
649 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
650 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
651 0xff, 0x25, 16, 0, 0, 0 /* jmpq *GOT+TDG(%rip) */
654 /* .eh_frame covering the lazy .plt section. */
656 static const bfd_byte elf_x86_64_eh_frame_lazy_plt
[] =
658 PLT_CIE_LENGTH
, 0, 0, 0, /* CIE length */
659 0, 0, 0, 0, /* CIE ID */
661 'z', 'R', 0, /* Augmentation string */
662 1, /* Code alignment factor */
663 0x78, /* Data alignment factor */
664 16, /* Return address column */
665 1, /* Augmentation size */
666 DW_EH_PE_pcrel
| DW_EH_PE_sdata4
, /* FDE encoding */
667 DW_CFA_def_cfa
, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
668 DW_CFA_offset
+ 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
669 DW_CFA_nop
, DW_CFA_nop
,
671 PLT_FDE_LENGTH
, 0, 0, 0, /* FDE length */
672 PLT_CIE_LENGTH
+ 8, 0, 0, 0, /* CIE pointer */
673 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
674 0, 0, 0, 0, /* .plt size goes here */
675 0, /* Augmentation size */
676 DW_CFA_def_cfa_offset
, 16, /* DW_CFA_def_cfa_offset: 16 */
677 DW_CFA_advance_loc
+ 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
678 DW_CFA_def_cfa_offset
, 24, /* DW_CFA_def_cfa_offset: 24 */
679 DW_CFA_advance_loc
+ 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
680 DW_CFA_def_cfa_expression
, /* DW_CFA_def_cfa_expression */
681 11, /* Block length */
682 DW_OP_breg7
, 8, /* DW_OP_breg7 (rsp): 8 */
683 DW_OP_breg16
, 0, /* DW_OP_breg16 (rip): 0 */
684 DW_OP_lit15
, DW_OP_and
, DW_OP_lit11
, DW_OP_ge
,
685 DW_OP_lit3
, DW_OP_shl
, DW_OP_plus
,
686 DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
689 /* .eh_frame covering the lazy BND .plt section. */
691 static const bfd_byte elf_x86_64_eh_frame_lazy_bnd_plt
[] =
693 PLT_CIE_LENGTH
, 0, 0, 0, /* CIE length */
694 0, 0, 0, 0, /* CIE ID */
696 'z', 'R', 0, /* Augmentation string */
697 1, /* Code alignment factor */
698 0x78, /* Data alignment factor */
699 16, /* Return address column */
700 1, /* Augmentation size */
701 DW_EH_PE_pcrel
| DW_EH_PE_sdata4
, /* FDE encoding */
702 DW_CFA_def_cfa
, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
703 DW_CFA_offset
+ 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
704 DW_CFA_nop
, DW_CFA_nop
,
706 PLT_FDE_LENGTH
, 0, 0, 0, /* FDE length */
707 PLT_CIE_LENGTH
+ 8, 0, 0, 0, /* CIE pointer */
708 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
709 0, 0, 0, 0, /* .plt size goes here */
710 0, /* Augmentation size */
711 DW_CFA_def_cfa_offset
, 16, /* DW_CFA_def_cfa_offset: 16 */
712 DW_CFA_advance_loc
+ 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
713 DW_CFA_def_cfa_offset
, 24, /* DW_CFA_def_cfa_offset: 24 */
714 DW_CFA_advance_loc
+ 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
715 DW_CFA_def_cfa_expression
, /* DW_CFA_def_cfa_expression */
716 11, /* Block length */
717 DW_OP_breg7
, 8, /* DW_OP_breg7 (rsp): 8 */
718 DW_OP_breg16
, 0, /* DW_OP_breg16 (rip): 0 */
719 DW_OP_lit15
, DW_OP_and
, DW_OP_lit5
, DW_OP_ge
,
720 DW_OP_lit3
, DW_OP_shl
, DW_OP_plus
,
721 DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
724 /* .eh_frame covering the lazy .plt section with IBT-enabled. */
726 static const bfd_byte elf_x86_64_eh_frame_lazy_ibt_plt
[] =
728 PLT_CIE_LENGTH
, 0, 0, 0, /* CIE length */
729 0, 0, 0, 0, /* CIE ID */
731 'z', 'R', 0, /* Augmentation string */
732 1, /* Code alignment factor */
733 0x78, /* Data alignment factor */
734 16, /* Return address column */
735 1, /* Augmentation size */
736 DW_EH_PE_pcrel
| DW_EH_PE_sdata4
, /* FDE encoding */
737 DW_CFA_def_cfa
, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
738 DW_CFA_offset
+ 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
739 DW_CFA_nop
, DW_CFA_nop
,
741 PLT_FDE_LENGTH
, 0, 0, 0, /* FDE length */
742 PLT_CIE_LENGTH
+ 8, 0, 0, 0, /* CIE pointer */
743 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
744 0, 0, 0, 0, /* .plt size goes here */
745 0, /* Augmentation size */
746 DW_CFA_def_cfa_offset
, 16, /* DW_CFA_def_cfa_offset: 16 */
747 DW_CFA_advance_loc
+ 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
748 DW_CFA_def_cfa_offset
, 24, /* DW_CFA_def_cfa_offset: 24 */
749 DW_CFA_advance_loc
+ 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
750 DW_CFA_def_cfa_expression
, /* DW_CFA_def_cfa_expression */
751 11, /* Block length */
752 DW_OP_breg7
, 8, /* DW_OP_breg7 (rsp): 8 */
753 DW_OP_breg16
, 0, /* DW_OP_breg16 (rip): 0 */
754 DW_OP_lit15
, DW_OP_and
, DW_OP_lit10
, DW_OP_ge
,
755 DW_OP_lit3
, DW_OP_shl
, DW_OP_plus
,
756 DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
759 /* .eh_frame covering the x32 lazy .plt section with IBT-enabled. */
761 static const bfd_byte elf_x32_eh_frame_lazy_ibt_plt
[] =
763 PLT_CIE_LENGTH
, 0, 0, 0, /* CIE length */
764 0, 0, 0, 0, /* CIE ID */
766 'z', 'R', 0, /* Augmentation string */
767 1, /* Code alignment factor */
768 0x78, /* Data alignment factor */
769 16, /* Return address column */
770 1, /* Augmentation size */
771 DW_EH_PE_pcrel
| DW_EH_PE_sdata4
, /* FDE encoding */
772 DW_CFA_def_cfa
, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
773 DW_CFA_offset
+ 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
774 DW_CFA_nop
, DW_CFA_nop
,
776 PLT_FDE_LENGTH
, 0, 0, 0, /* FDE length */
777 PLT_CIE_LENGTH
+ 8, 0, 0, 0, /* CIE pointer */
778 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
779 0, 0, 0, 0, /* .plt size goes here */
780 0, /* Augmentation size */
781 DW_CFA_def_cfa_offset
, 16, /* DW_CFA_def_cfa_offset: 16 */
782 DW_CFA_advance_loc
+ 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
783 DW_CFA_def_cfa_offset
, 24, /* DW_CFA_def_cfa_offset: 24 */
784 DW_CFA_advance_loc
+ 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
785 DW_CFA_def_cfa_expression
, /* DW_CFA_def_cfa_expression */
786 11, /* Block length */
787 DW_OP_breg7
, 8, /* DW_OP_breg7 (rsp): 8 */
788 DW_OP_breg16
, 0, /* DW_OP_breg16 (rip): 0 */
789 DW_OP_lit15
, DW_OP_and
, DW_OP_lit9
, DW_OP_ge
,
790 DW_OP_lit3
, DW_OP_shl
, DW_OP_plus
,
791 DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
794 /* .eh_frame covering the non-lazy .plt section. */
796 static const bfd_byte elf_x86_64_eh_frame_non_lazy_plt
[] =
798 #define PLT_GOT_FDE_LENGTH 20
799 PLT_CIE_LENGTH
, 0, 0, 0, /* CIE length */
800 0, 0, 0, 0, /* CIE ID */
802 'z', 'R', 0, /* Augmentation string */
803 1, /* Code alignment factor */
804 0x78, /* Data alignment factor */
805 16, /* Return address column */
806 1, /* Augmentation size */
807 DW_EH_PE_pcrel
| DW_EH_PE_sdata4
, /* FDE encoding */
808 DW_CFA_def_cfa
, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
809 DW_CFA_offset
+ 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
810 DW_CFA_nop
, DW_CFA_nop
,
812 PLT_GOT_FDE_LENGTH
, 0, 0, 0, /* FDE length */
813 PLT_CIE_LENGTH
+ 8, 0, 0, 0, /* CIE pointer */
814 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
815 0, 0, 0, 0, /* non-lazy .plt size goes here */
816 0, /* Augmentation size */
817 DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
,
818 DW_CFA_nop
, DW_CFA_nop
, DW_CFA_nop
821 /* These are the standard parameters. */
822 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_plt
=
824 elf_x86_64_lazy_plt0_entry
, /* plt0_entry */
825 LAZY_PLT_ENTRY_SIZE
, /* plt0_entry_size */
826 elf_x86_64_lazy_plt_entry
, /* plt_entry */
827 LAZY_PLT_ENTRY_SIZE
, /* plt_entry_size */
828 elf_x86_64_tlsdesc_plt_entry
, /* plt_tlsdesc_entry */
829 LAZY_PLT_ENTRY_SIZE
, /* plt_tlsdesc_entry_size */
830 6, /* plt_tlsdesc_got1_offset */
831 12, /* plt_tlsdesc_got2_offset */
832 10, /* plt_tlsdesc_got1_insn_end */
833 16, /* plt_tlsdesc_got2_insn_end */
834 2, /* plt0_got1_offset */
835 8, /* plt0_got2_offset */
836 12, /* plt0_got2_insn_end */
837 2, /* plt_got_offset */
838 7, /* plt_reloc_offset */
839 12, /* plt_plt_offset */
840 6, /* plt_got_insn_size */
841 LAZY_PLT_ENTRY_SIZE
, /* plt_plt_insn_end */
842 6, /* plt_lazy_offset */
843 elf_x86_64_lazy_plt0_entry
, /* pic_plt0_entry */
844 elf_x86_64_lazy_plt_entry
, /* pic_plt_entry */
845 elf_x86_64_eh_frame_lazy_plt
, /* eh_frame_plt */
846 sizeof (elf_x86_64_eh_frame_lazy_plt
) /* eh_frame_plt_size */
849 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_plt
=
851 elf_x86_64_non_lazy_plt_entry
, /* plt_entry */
852 elf_x86_64_non_lazy_plt_entry
, /* pic_plt_entry */
853 NON_LAZY_PLT_ENTRY_SIZE
, /* plt_entry_size */
854 2, /* plt_got_offset */
855 6, /* plt_got_insn_size */
856 elf_x86_64_eh_frame_non_lazy_plt
, /* eh_frame_plt */
857 sizeof (elf_x86_64_eh_frame_non_lazy_plt
) /* eh_frame_plt_size */
860 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_bnd_plt
=
862 elf_x86_64_lazy_bnd_plt0_entry
, /* plt0_entry */
863 LAZY_PLT_ENTRY_SIZE
, /* plt0_entry_size */
864 elf_x86_64_lazy_bnd_plt_entry
, /* plt_entry */
865 LAZY_PLT_ENTRY_SIZE
, /* plt_entry_size */
866 elf_x86_64_tlsdesc_plt_entry
, /* plt_tlsdesc_entry */
867 LAZY_PLT_ENTRY_SIZE
, /* plt_tlsdesc_entry_size */
868 6, /* plt_tlsdesc_got1_offset */
869 12, /* plt_tlsdesc_got2_offset */
870 10, /* plt_tlsdesc_got1_insn_end */
871 16, /* plt_tlsdesc_got2_insn_end */
872 2, /* plt0_got1_offset */
873 1+8, /* plt0_got2_offset */
874 1+12, /* plt0_got2_insn_end */
875 1+2, /* plt_got_offset */
876 1, /* plt_reloc_offset */
877 7, /* plt_plt_offset */
878 1+6, /* plt_got_insn_size */
879 11, /* plt_plt_insn_end */
880 0, /* plt_lazy_offset */
881 elf_x86_64_lazy_bnd_plt0_entry
, /* pic_plt0_entry */
882 elf_x86_64_lazy_bnd_plt_entry
, /* pic_plt_entry */
883 elf_x86_64_eh_frame_lazy_bnd_plt
, /* eh_frame_plt */
884 sizeof (elf_x86_64_eh_frame_lazy_bnd_plt
) /* eh_frame_plt_size */
887 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_bnd_plt
=
889 elf_x86_64_non_lazy_bnd_plt_entry
, /* plt_entry */
890 elf_x86_64_non_lazy_bnd_plt_entry
, /* pic_plt_entry */
891 NON_LAZY_PLT_ENTRY_SIZE
, /* plt_entry_size */
892 1+2, /* plt_got_offset */
893 1+6, /* plt_got_insn_size */
894 elf_x86_64_eh_frame_non_lazy_plt
, /* eh_frame_plt */
895 sizeof (elf_x86_64_eh_frame_non_lazy_plt
) /* eh_frame_plt_size */
898 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_ibt_plt
=
900 elf_x86_64_lazy_bnd_plt0_entry
, /* plt0_entry */
901 LAZY_PLT_ENTRY_SIZE
, /* plt0_entry_size */
902 elf_x86_64_lazy_ibt_plt_entry
, /* plt_entry */
903 LAZY_PLT_ENTRY_SIZE
, /* plt_entry_size */
904 elf_x86_64_tlsdesc_plt_entry
, /* plt_tlsdesc_entry */
905 LAZY_PLT_ENTRY_SIZE
, /* plt_tlsdesc_entry_size */
906 6, /* plt_tlsdesc_got1_offset */
907 12, /* plt_tlsdesc_got2_offset */
908 10, /* plt_tlsdesc_got1_insn_end */
909 16, /* plt_tlsdesc_got2_insn_end */
910 2, /* plt0_got1_offset */
911 1+8, /* plt0_got2_offset */
912 1+12, /* plt0_got2_insn_end */
913 4+1+2, /* plt_got_offset */
914 4+1, /* plt_reloc_offset */
915 4+1+6, /* plt_plt_offset */
916 4+1+6, /* plt_got_insn_size */
917 4+1+5+5, /* plt_plt_insn_end */
918 0, /* plt_lazy_offset */
919 elf_x86_64_lazy_bnd_plt0_entry
, /* pic_plt0_entry */
920 elf_x86_64_lazy_ibt_plt_entry
, /* pic_plt_entry */
921 elf_x86_64_eh_frame_lazy_ibt_plt
, /* eh_frame_plt */
922 sizeof (elf_x86_64_eh_frame_lazy_ibt_plt
) /* eh_frame_plt_size */
925 static const struct elf_x86_lazy_plt_layout elf_x32_lazy_ibt_plt
=
927 elf_x86_64_lazy_plt0_entry
, /* plt0_entry */
928 LAZY_PLT_ENTRY_SIZE
, /* plt0_entry_size */
929 elf_x32_lazy_ibt_plt_entry
, /* plt_entry */
930 LAZY_PLT_ENTRY_SIZE
, /* plt_entry_size */
931 elf_x86_64_tlsdesc_plt_entry
, /* plt_tlsdesc_entry */
932 LAZY_PLT_ENTRY_SIZE
, /* plt_tlsdesc_entry_size */
933 6, /* plt_tlsdesc_got1_offset */
934 12, /* plt_tlsdesc_got2_offset */
935 10, /* plt_tlsdesc_got1_insn_end */
936 16, /* plt_tlsdesc_got2_insn_end */
937 2, /* plt0_got1_offset */
938 8, /* plt0_got2_offset */
939 12, /* plt0_got2_insn_end */
940 4+2, /* plt_got_offset */
941 4+1, /* plt_reloc_offset */
942 4+6, /* plt_plt_offset */
943 4+6, /* plt_got_insn_size */
944 4+5+5, /* plt_plt_insn_end */
945 0, /* plt_lazy_offset */
946 elf_x86_64_lazy_plt0_entry
, /* pic_plt0_entry */
947 elf_x32_lazy_ibt_plt_entry
, /* pic_plt_entry */
948 elf_x32_eh_frame_lazy_ibt_plt
, /* eh_frame_plt */
949 sizeof (elf_x32_eh_frame_lazy_ibt_plt
) /* eh_frame_plt_size */
952 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_ibt_plt
=
954 elf_x86_64_non_lazy_ibt_plt_entry
, /* plt_entry */
955 elf_x86_64_non_lazy_ibt_plt_entry
, /* pic_plt_entry */
956 LAZY_PLT_ENTRY_SIZE
, /* plt_entry_size */
957 4+1+2, /* plt_got_offset */
958 4+1+6, /* plt_got_insn_size */
959 elf_x86_64_eh_frame_non_lazy_plt
, /* eh_frame_plt */
960 sizeof (elf_x86_64_eh_frame_non_lazy_plt
) /* eh_frame_plt_size */
963 static const struct elf_x86_non_lazy_plt_layout elf_x32_non_lazy_ibt_plt
=
965 elf_x32_non_lazy_ibt_plt_entry
, /* plt_entry */
966 elf_x32_non_lazy_ibt_plt_entry
, /* pic_plt_entry */
967 LAZY_PLT_ENTRY_SIZE
, /* plt_entry_size */
968 4+2, /* plt_got_offset */
969 4+6, /* plt_got_insn_size */
970 elf_x86_64_eh_frame_non_lazy_plt
, /* eh_frame_plt */
971 sizeof (elf_x86_64_eh_frame_non_lazy_plt
) /* eh_frame_plt_size */
976 elf64_x86_64_elf_object_p (bfd
*abfd
)
978 /* Set the right machine number for an x86-64 elf64 file. */
979 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x86_64
);
984 elf32_x86_64_elf_object_p (bfd
*abfd
)
986 /* Set the right machine number for an x86-64 elf32 file. */
987 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x64_32
);
991 /* Return TRUE if the TLS access code sequence support transition
995 elf_x86_64_check_tls_transition (bfd
*abfd
,
996 struct bfd_link_info
*info
,
999 Elf_Internal_Shdr
*symtab_hdr
,
1000 struct elf_link_hash_entry
**sym_hashes
,
1001 unsigned int r_type
,
1002 const Elf_Internal_Rela
*rel
,
1003 const Elf_Internal_Rela
*relend
)
1006 unsigned long r_symndx
;
1007 bool largepic
= false;
1008 struct elf_link_hash_entry
*h
;
1010 struct elf_x86_link_hash_table
*htab
;
1014 htab
= elf_x86_hash_table (info
, X86_64_ELF_DATA
);
1015 offset
= rel
->r_offset
;
1018 case R_X86_64_TLSGD
:
1019 case R_X86_64_TLSLD
:
1020 if ((rel
+ 1) >= relend
)
1023 if (r_type
== R_X86_64_TLSGD
)
1025 /* Check transition from GD access model. For 64bit, only
1026 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1027 .word 0x6666; rex64; call __tls_get_addr@PLT
1029 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1031 call *__tls_get_addr@GOTPCREL(%rip)
1032 which may be converted to
1033 addr32 call __tls_get_addr
1034 can transit to different access model. For 32bit, only
1035 leaq foo@tlsgd(%rip), %rdi
1036 .word 0x6666; rex64; call __tls_get_addr@PLT
1038 leaq foo@tlsgd(%rip), %rdi
1040 call *__tls_get_addr@GOTPCREL(%rip)
1041 which may be converted to
1042 addr32 call __tls_get_addr
1043 can transit to different access model. For largepic,
1045 leaq foo@tlsgd(%rip), %rdi
1046 movabsq $__tls_get_addr@pltoff, %rax
1050 leaq foo@tlsgd(%rip), %rdi
1051 movabsq $__tls_get_addr@pltoff, %rax
1055 static const unsigned char leaq
[] = { 0x66, 0x48, 0x8d, 0x3d };
1057 if ((offset
+ 12) > sec
->size
)
1060 call
= contents
+ offset
+ 4;
1062 || !((call
[1] == 0x48
1070 && call
[3] == 0xe8)))
1072 if (!ABI_64_P (abfd
)
1073 || (offset
+ 19) > sec
->size
1075 || memcmp (call
- 7, leaq
+ 1, 3) != 0
1076 || memcmp (call
, "\x48\xb8", 2) != 0
1080 || !((call
[10] == 0x48 && call
[12] == 0xd8)
1081 || (call
[10] == 0x4c && call
[12] == 0xf8)))
1085 else if (ABI_64_P (abfd
))
1088 || memcmp (contents
+ offset
- 4, leaq
, 4) != 0)
1094 || memcmp (contents
+ offset
- 3, leaq
+ 1, 3) != 0)
1097 indirect_call
= call
[2] == 0xff;
1101 /* Check transition from LD access model. Only
1102 leaq foo@tlsld(%rip), %rdi;
1103 call __tls_get_addr@PLT
1105 leaq foo@tlsld(%rip), %rdi;
1106 call *__tls_get_addr@GOTPCREL(%rip)
1107 which may be converted to
1108 addr32 call __tls_get_addr
1109 can transit to different access model. For largepic
1111 leaq foo@tlsld(%rip), %rdi
1112 movabsq $__tls_get_addr@pltoff, %rax
1116 leaq foo@tlsld(%rip), %rdi
1117 movabsq $__tls_get_addr@pltoff, %rax
1121 static const unsigned char lea
[] = { 0x48, 0x8d, 0x3d };
1123 if (offset
< 3 || (offset
+ 9) > sec
->size
)
1126 if (memcmp (contents
+ offset
- 3, lea
, 3) != 0)
1129 call
= contents
+ offset
+ 4;
1130 if (!(call
[0] == 0xe8
1131 || (call
[0] == 0xff && call
[1] == 0x15)
1132 || (call
[0] == 0x67 && call
[1] == 0xe8)))
1134 if (!ABI_64_P (abfd
)
1135 || (offset
+ 19) > sec
->size
1136 || memcmp (call
, "\x48\xb8", 2) != 0
1140 || !((call
[10] == 0x48 && call
[12] == 0xd8)
1141 || (call
[10] == 0x4c && call
[12] == 0xf8)))
1145 indirect_call
= call
[0] == 0xff;
1148 r_symndx
= htab
->r_sym (rel
[1].r_info
);
1149 if (r_symndx
< symtab_hdr
->sh_info
)
1152 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1154 || !((struct elf_x86_link_hash_entry
*) h
)->tls_get_addr
)
1158 r_type
= (ELF32_R_TYPE (rel
[1].r_info
)
1159 & ~R_X86_64_converted_reloc_bit
);
1161 return r_type
== R_X86_64_PLTOFF64
;
1162 else if (indirect_call
)
1163 return r_type
== R_X86_64_GOTPCRELX
;
1165 return (r_type
== R_X86_64_PC32
|| r_type
== R_X86_64_PLT32
);
1168 case R_X86_64_GOTTPOFF
:
1169 /* Check transition from IE access model:
1170 mov foo@gottpoff(%rip), %reg
1171 add foo@gottpoff(%rip), %reg
1174 /* Check REX prefix first. */
1175 if (offset
>= 3 && (offset
+ 4) <= sec
->size
)
1177 val
= bfd_get_8 (abfd
, contents
+ offset
- 3);
1178 if (val
!= 0x48 && val
!= 0x4c)
1180 /* X32 may have 0x44 REX prefix or no REX prefix. */
1181 if (ABI_64_P (abfd
))
1187 /* X32 may not have any REX prefix. */
1188 if (ABI_64_P (abfd
))
1190 if (offset
< 2 || (offset
+ 3) > sec
->size
)
1194 val
= bfd_get_8 (abfd
, contents
+ offset
- 2);
1195 if (val
!= 0x8b && val
!= 0x03)
1198 val
= bfd_get_8 (abfd
, contents
+ offset
- 1);
1199 return (val
& 0xc7) == 5;
1201 case R_X86_64_GOTPC32_TLSDESC
:
1202 /* Check transition from GDesc access model:
1203 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
1204 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
1206 Make sure it's a leaq adding rip to a 32-bit offset
1207 into any register, although it's probably almost always
1210 if (offset
< 3 || (offset
+ 4) > sec
->size
)
1213 val
= bfd_get_8 (abfd
, contents
+ offset
- 3);
1215 if (val
!= 0x48 && (ABI_64_P (abfd
) || val
!= 0x40))
1218 if (bfd_get_8 (abfd
, contents
+ offset
- 2) != 0x8d)
1221 val
= bfd_get_8 (abfd
, contents
+ offset
- 1);
1222 return (val
& 0xc7) == 0x05;
1224 case R_X86_64_TLSDESC_CALL
:
1225 /* Check transition from GDesc access model:
1226 call *x@tlsdesc(%rax) <--- LP64 mode.
1227 call *x@tlsdesc(%eax) <--- X32 mode.
1229 if (offset
+ 2 <= sec
->size
)
1231 unsigned int prefix
;
1232 call
= contents
+ offset
;
1234 if (!ABI_64_P (abfd
))
1236 /* Check for call *x@tlsdesc(%eax). */
1237 if (call
[0] == 0x67)
1240 if (offset
+ 3 > sec
->size
)
1244 /* Make sure that it's a call *x@tlsdesc(%rax). */
1245 return call
[prefix
] == 0xff && call
[1 + prefix
] == 0x10;
1255 /* Return TRUE if the TLS access transition is OK or no transition
1256 will be performed. Update R_TYPE if there is a transition. */
1259 elf_x86_64_tls_transition (struct bfd_link_info
*info
, bfd
*abfd
,
1260 asection
*sec
, bfd_byte
*contents
,
1261 Elf_Internal_Shdr
*symtab_hdr
,
1262 struct elf_link_hash_entry
**sym_hashes
,
1263 unsigned int *r_type
, int tls_type
,
1264 const Elf_Internal_Rela
*rel
,
1265 const Elf_Internal_Rela
*relend
,
1266 struct elf_link_hash_entry
*h
,
1267 unsigned long r_symndx
,
1268 bool from_relocate_section
)
1270 unsigned int from_type
= *r_type
;
1271 unsigned int to_type
= from_type
;
1274 /* Skip TLS transition for functions. */
1276 && (h
->type
== STT_FUNC
1277 || h
->type
== STT_GNU_IFUNC
))
1282 case R_X86_64_TLSGD
:
1283 case R_X86_64_GOTPC32_TLSDESC
:
1284 case R_X86_64_TLSDESC_CALL
:
1285 case R_X86_64_GOTTPOFF
:
1286 if (bfd_link_executable (info
))
1289 to_type
= R_X86_64_TPOFF32
;
1291 to_type
= R_X86_64_GOTTPOFF
;
1294 /* When we are called from elf_x86_64_relocate_section, there may
1295 be additional transitions based on TLS_TYPE. */
1296 if (from_relocate_section
)
1298 unsigned int new_to_type
= to_type
;
1300 if (TLS_TRANSITION_IE_TO_LE_P (info
, h
, tls_type
))
1301 new_to_type
= R_X86_64_TPOFF32
;
1303 if (to_type
== R_X86_64_TLSGD
1304 || to_type
== R_X86_64_GOTPC32_TLSDESC
1305 || to_type
== R_X86_64_TLSDESC_CALL
)
1307 if (tls_type
== GOT_TLS_IE
)
1308 new_to_type
= R_X86_64_GOTTPOFF
;
1311 /* We checked the transition before when we were called from
1312 elf_x86_64_scan_relocs. We only want to check the new
1313 transition which hasn't been checked before. */
1314 check
= new_to_type
!= to_type
&& from_type
== to_type
;
1315 to_type
= new_to_type
;
1320 case R_X86_64_TLSLD
:
1321 if (bfd_link_executable (info
))
1322 to_type
= R_X86_64_TPOFF32
;
1329 /* Return TRUE if there is no transition. */
1330 if (from_type
== to_type
)
1333 /* Check if the transition can be performed. */
1335 && ! elf_x86_64_check_tls_transition (abfd
, info
, sec
, contents
,
1336 symtab_hdr
, sym_hashes
,
1337 from_type
, rel
, relend
))
1339 reloc_howto_type
*from
, *to
;
1342 from
= elf_x86_64_rtype_to_howto (abfd
, from_type
);
1343 to
= elf_x86_64_rtype_to_howto (abfd
, to_type
);
1345 if (from
== NULL
|| to
== NULL
)
1349 name
= h
->root
.root
.string
;
1352 struct elf_x86_link_hash_table
*htab
;
1354 htab
= elf_x86_hash_table (info
, X86_64_ELF_DATA
);
1359 Elf_Internal_Sym
*isym
;
1361 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
1363 name
= bfd_elf_sym_name (abfd
, symtab_hdr
, isym
, NULL
);
1368 /* xgettext:c-format */
1369 (_("%pB: TLS transition from %s to %s against `%s' at %#" PRIx64
1370 " in section `%pA' failed"),
1371 abfd
, from
->name
, to
->name
, name
, (uint64_t) rel
->r_offset
, sec
);
1372 bfd_set_error (bfd_error_bad_value
);
1381 elf_x86_64_need_pic (struct bfd_link_info
*info
,
1382 bfd
*input_bfd
, asection
*sec
,
1383 struct elf_link_hash_entry
*h
,
1384 Elf_Internal_Shdr
*symtab_hdr
,
1385 Elf_Internal_Sym
*isym
,
1386 reloc_howto_type
*howto
)
1389 const char *und
= "";
1390 const char *pic
= "";
1396 name
= h
->root
.root
.string
;
1397 switch (ELF_ST_VISIBILITY (h
->other
))
1400 v
= _("hidden symbol ");
1403 v
= _("internal symbol ");
1406 v
= _("protected symbol ");
1409 if (((struct elf_x86_link_hash_entry
*) h
)->def_protected
)
1410 v
= _("protected symbol ");
1417 if (!SYMBOL_DEFINED_NON_SHARED_P (h
) && !h
->def_dynamic
)
1418 und
= _("undefined ");
1422 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, isym
, NULL
);
1426 if (bfd_link_dll (info
))
1428 object
= _("a shared object");
1430 pic
= _("; recompile with -fPIC");
1434 if (bfd_link_pie (info
))
1435 object
= _("a PIE object");
1437 object
= _("a PDE object");
1439 pic
= _("; recompile with -fPIE");
1442 /* xgettext:c-format */
1443 _bfd_error_handler (_("%pB: relocation %s against %s%s`%s' can "
1444 "not be used when making %s%s"),
1445 input_bfd
, howto
->name
, und
, v
, name
,
1447 bfd_set_error (bfd_error_bad_value
);
1448 sec
->check_relocs_failed
= 1;
1452 /* With the local symbol, foo, we convert
1453 mov foo@GOTPCREL(%rip), %reg
1457 call/jmp *foo@GOTPCREL(%rip)
1459 nop call foo/jmp foo nop
1460 When PIC is false, convert
1461 test %reg, foo@GOTPCREL(%rip)
1465 binop foo@GOTPCREL(%rip), %reg
1468 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1472 elf_x86_64_convert_load_reloc (bfd
*abfd
,
1474 unsigned int *r_type_p
,
1475 Elf_Internal_Rela
*irel
,
1476 struct elf_link_hash_entry
*h
,
1478 struct bfd_link_info
*link_info
)
1480 struct elf_x86_link_hash_table
*htab
;
1488 bfd_signed_vma raddend
;
1489 unsigned int opcode
;
1491 unsigned int r_type
= *r_type_p
;
1492 unsigned int r_symndx
;
1493 bfd_vma roff
= irel
->r_offset
;
1494 bfd_vma abs_relocation
;
1496 if (roff
< (r_type
== R_X86_64_REX_GOTPCRELX
? 3 : 2))
1499 raddend
= irel
->r_addend
;
1500 /* Addend for 32-bit PC-relative relocation must be -4. */
1504 htab
= elf_x86_hash_table (link_info
, X86_64_ELF_DATA
);
1505 is_pic
= bfd_link_pic (link_info
);
1507 relocx
= (r_type
== R_X86_64_GOTPCRELX
1508 || r_type
== R_X86_64_REX_GOTPCRELX
);
1510 /* TRUE if --no-relax is used. */
1511 no_overflow
= link_info
->disable_target_specific_optimizations
> 1;
1513 r_symndx
= htab
->r_sym (irel
->r_info
);
1515 opcode
= bfd_get_8 (abfd
, contents
+ roff
- 2);
1517 /* Convert mov to lea since it has been done for a while. */
1520 /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1521 for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1522 test, xor instructions. */
1527 /* We convert only to R_X86_64_PC32:
1529 2. R_X86_64_GOTPCREL since we can't modify REX byte.
1530 3. no_overflow is true.
1533 to_reloc_pc32
= (opcode
== 0xff
1541 /* Get the symbol referred to by the reloc. */
1544 Elf_Internal_Sym
*isym
1545 = bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
, abfd
, r_symndx
);
1547 /* Skip relocation against undefined symbols. */
1548 if (isym
->st_shndx
== SHN_UNDEF
)
1552 if (isym
->st_shndx
== SHN_ABS
)
1554 tsec
= bfd_abs_section_ptr
;
1556 abs_relocation
= isym
->st_value
;
1558 else if (isym
->st_shndx
== SHN_COMMON
)
1559 tsec
= bfd_com_section_ptr
;
1560 else if (isym
->st_shndx
== SHN_X86_64_LCOMMON
)
1561 tsec
= &_bfd_elf_large_com_section
;
1563 tsec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1567 /* Undefined weak symbol is only bound locally in executable
1568 and its reference is resolved as 0 without relocation
1569 overflow. We can only perform this optimization for
1570 GOTPCRELX relocations since we need to modify REX byte.
1571 It is OK convert mov with R_X86_64_GOTPCREL to
1573 struct elf_x86_link_hash_entry
*eh
= elf_x86_hash_entry (h
);
1575 abs_symbol
= ABS_SYMBOL_P (h
);
1576 abs_relocation
= h
->root
.u
.def
.value
;
1578 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1579 local_ref
= SYMBOL_REFERENCES_LOCAL_P (link_info
, h
);
1580 if ((relocx
|| opcode
== 0x8b)
1581 && (h
->root
.type
== bfd_link_hash_undefweak
1587 /* Skip for branch instructions since R_X86_64_PC32
1594 /* For non-branch instructions, we can convert to
1595 R_X86_64_32/R_X86_64_32S since we know if there
1597 to_reloc_pc32
= false;
1600 /* Since we don't know the current PC when PIC is true,
1601 we can't convert to R_X86_64_PC32. */
1602 if (to_reloc_pc32
&& is_pic
)
1607 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1608 ld.so may use its link-time address. */
1609 else if (h
->start_stop
1612 || h
->root
.type
== bfd_link_hash_defined
1613 || h
->root
.type
== bfd_link_hash_defweak
)
1614 && h
!= htab
->elf
.hdynamic
1617 /* bfd_link_hash_new or bfd_link_hash_undefined is
1618 set by an assignment in a linker script in
1619 bfd_elf_record_link_assignment. start_stop is set
1620 on __start_SECNAME/__stop_SECNAME which mark section
1625 && (h
->root
.type
== bfd_link_hash_new
1626 || h
->root
.type
== bfd_link_hash_undefined
1627 || ((h
->root
.type
== bfd_link_hash_defined
1628 || h
->root
.type
== bfd_link_hash_defweak
)
1629 && h
->root
.u
.def
.section
== bfd_und_section_ptr
))))
1631 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
1636 tsec
= h
->root
.u
.def
.section
;
1642 /* Don't convert GOTPCREL relocation against large section. */
1643 if (elf_section_data (tsec
) != NULL
1644 && (elf_section_flags (tsec
) & SHF_X86_64_LARGE
) != 0)
1647 /* Skip since R_X86_64_PC32/R_X86_64_32/R_X86_64_32S may overflow. */
1654 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
1659 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1661 modrm
= bfd_get_8 (abfd
, contents
+ roff
- 1);
1664 /* Convert to "jmp foo nop". */
1667 nop_offset
= irel
->r_offset
+ 3;
1668 disp
= bfd_get_32 (abfd
, contents
+ irel
->r_offset
);
1669 irel
->r_offset
-= 1;
1670 bfd_put_32 (abfd
, disp
, contents
+ irel
->r_offset
);
1674 struct elf_x86_link_hash_entry
*eh
1675 = (struct elf_x86_link_hash_entry
*) h
;
1677 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1680 /* To support TLS optimization, always use addr32 prefix for
1681 "call *__tls_get_addr@GOTPCREL(%rip)". */
1682 if (eh
&& eh
->tls_get_addr
)
1685 nop_offset
= irel
->r_offset
- 2;
1689 nop
= htab
->params
->call_nop_byte
;
1690 if (htab
->params
->call_nop_as_suffix
)
1692 nop_offset
= irel
->r_offset
+ 3;
1693 disp
= bfd_get_32 (abfd
, contents
+ irel
->r_offset
);
1694 irel
->r_offset
-= 1;
1695 bfd_put_32 (abfd
, disp
, contents
+ irel
->r_offset
);
1698 nop_offset
= irel
->r_offset
- 2;
1701 bfd_put_8 (abfd
, nop
, contents
+ nop_offset
);
1702 bfd_put_8 (abfd
, modrm
, contents
+ irel
->r_offset
- 1);
1703 r_type
= R_X86_64_PC32
;
1708 unsigned int rex_mask
= REX_R
;
1710 if (r_type
== R_X86_64_REX_GOTPCRELX
)
1711 rex
= bfd_get_8 (abfd
, contents
+ roff
- 3);
1717 if (abs_symbol
&& local_ref
&& relocx
)
1718 to_reloc_pc32
= false;
1722 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1723 "lea foo(%rip), %reg". */
1725 r_type
= R_X86_64_PC32
;
1729 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1730 "mov $foo, %reg". */
1732 modrm
= bfd_get_8 (abfd
, contents
+ roff
- 1);
1733 modrm
= 0xc0 | (modrm
& 0x38) >> 3;
1734 if ((rex
& REX_W
) != 0
1735 && ABI_64_P (link_info
->output_bfd
))
1737 /* Keep the REX_W bit in REX byte for LP64. */
1738 r_type
= R_X86_64_32S
;
1739 goto rewrite_modrm_rex
;
1743 /* If the REX_W bit in REX byte isn't needed,
1744 use R_X86_64_32 and clear the W bit to avoid
1745 sign-extend imm32 to imm64. */
1746 r_type
= R_X86_64_32
;
1747 /* Clear the W bit in REX byte. */
1749 goto rewrite_modrm_rex
;
1755 /* R_X86_64_PC32 isn't supported. */
1759 modrm
= bfd_get_8 (abfd
, contents
+ roff
- 1);
1762 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
1763 "test $foo, %reg". */
1764 modrm
= 0xc0 | (modrm
& 0x38) >> 3;
1769 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
1770 "binop $foo, %reg". */
1771 modrm
= 0xc0 | (modrm
& 0x38) >> 3 | (opcode
& 0x3c);
1775 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
1776 overflow when sign-extending imm32 to imm64. */
1777 r_type
= (rex
& REX_W
) != 0 ? R_X86_64_32S
: R_X86_64_32
;
1782 /* Check if R_X86_64_32S/R_X86_64_32 fits. */
1783 if (r_type
== R_X86_64_32S
)
1785 if ((abs_relocation
+ 0x80000000) > 0xffffffff)
1790 if (abs_relocation
> 0xffffffff)
1795 bfd_put_8 (abfd
, modrm
, contents
+ roff
- 1);
1799 /* Move the R bit to the B bit in REX byte. */
1800 rex
= (rex
& ~rex_mask
) | (rex
& REX_R
) >> 2;
1801 bfd_put_8 (abfd
, rex
, contents
+ roff
- 3);
1804 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
1808 bfd_put_8 (abfd
, opcode
, contents
+ roff
- 2);
1812 irel
->r_info
= htab
->r_info (r_symndx
,
1813 r_type
| R_X86_64_converted_reloc_bit
);
1820 /* Look through the relocs for a section during the first phase, and
1821 calculate needed space in the global offset table, and procedure
1825 elf_x86_64_scan_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
1827 const Elf_Internal_Rela
*relocs
)
1829 struct elf_x86_link_hash_table
*htab
;
1830 Elf_Internal_Shdr
*symtab_hdr
;
1831 struct elf_link_hash_entry
**sym_hashes
;
1832 const Elf_Internal_Rela
*rel
;
1833 const Elf_Internal_Rela
*rel_end
;
1837 if (bfd_link_relocatable (info
))
1840 htab
= elf_x86_hash_table (info
, X86_64_ELF_DATA
);
1843 sec
->check_relocs_failed
= 1;
1847 BFD_ASSERT (is_x86_elf (abfd
, htab
));
1849 /* Get the section contents. */
1850 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1851 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1852 else if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
1854 sec
->check_relocs_failed
= 1;
1858 symtab_hdr
= &elf_symtab_hdr (abfd
);
1859 sym_hashes
= elf_sym_hashes (abfd
);
1863 rel_end
= relocs
+ sec
->reloc_count
;
1864 for (rel
= relocs
; rel
< rel_end
; rel
++)
1866 unsigned int r_type
;
1867 unsigned int r_symndx
;
1868 struct elf_link_hash_entry
*h
;
1869 struct elf_x86_link_hash_entry
*eh
;
1870 Elf_Internal_Sym
*isym
;
1873 bool converted_reloc
;
1876 r_symndx
= htab
->r_sym (rel
->r_info
);
1877 r_type
= ELF32_R_TYPE (rel
->r_info
);
1879 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
1881 /* xgettext:c-format */
1882 _bfd_error_handler (_("%pB: bad symbol index: %d"),
1887 if (r_symndx
< symtab_hdr
->sh_info
)
1889 /* A local symbol. */
1890 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
1895 /* Check relocation against local STT_GNU_IFUNC symbol. */
1896 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
1898 h
= _bfd_elf_x86_get_local_sym_hash (htab
, abfd
, rel
,
1903 /* Fake a STT_GNU_IFUNC symbol. */
1904 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
1906 h
->type
= STT_GNU_IFUNC
;
1909 h
->forced_local
= 1;
1910 h
->root
.type
= bfd_link_hash_defined
;
1918 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1919 while (h
->root
.type
== bfd_link_hash_indirect
1920 || h
->root
.type
== bfd_link_hash_warning
)
1921 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1924 /* Check invalid x32 relocations. */
1925 if (!ABI_64_P (abfd
))
1931 case R_X86_64_DTPOFF64
:
1932 case R_X86_64_TPOFF64
:
1934 case R_X86_64_GOTOFF64
:
1935 case R_X86_64_GOT64
:
1936 case R_X86_64_GOTPCREL64
:
1937 case R_X86_64_GOTPC64
:
1938 case R_X86_64_GOTPLT64
:
1939 case R_X86_64_PLTOFF64
:
1942 name
= h
->root
.root
.string
;
1944 name
= bfd_elf_sym_name (abfd
, symtab_hdr
, isym
,
1947 /* xgettext:c-format */
1948 (_("%pB: relocation %s against symbol `%s' isn't "
1949 "supported in x32 mode"), abfd
,
1950 x86_64_elf_howto_table
[r_type
].name
, name
);
1951 bfd_set_error (bfd_error_bad_value
);
1957 eh
= (struct elf_x86_link_hash_entry
*) h
;
1961 /* It is referenced by a non-shared object. */
1965 converted_reloc
= false;
1966 if ((r_type
== R_X86_64_GOTPCREL
1967 || r_type
== R_X86_64_GOTPCRELX
1968 || r_type
== R_X86_64_REX_GOTPCRELX
)
1969 && (h
== NULL
|| h
->type
!= STT_GNU_IFUNC
))
1971 Elf_Internal_Rela
*irel
= (Elf_Internal_Rela
*) rel
;
1972 if (!elf_x86_64_convert_load_reloc (abfd
, contents
, &r_type
,
1973 irel
, h
, &converted_reloc
,
1977 if (converted_reloc
)
1981 if (!_bfd_elf_x86_valid_reloc_p (sec
, info
, htab
, rel
, h
, isym
,
1982 symtab_hdr
, &no_dynreloc
))
1985 if (! elf_x86_64_tls_transition (info
, abfd
, sec
, contents
,
1986 symtab_hdr
, sym_hashes
,
1987 &r_type
, GOT_UNKNOWN
,
1988 rel
, rel_end
, h
, r_symndx
, false))
1991 /* Check if _GLOBAL_OFFSET_TABLE_ is referenced. */
1992 if (h
== htab
->elf
.hgot
)
1993 htab
->got_referenced
= true;
1997 case R_X86_64_TLSLD
:
1998 htab
->tls_ld_or_ldm_got
.refcount
= 1;
2001 case R_X86_64_TPOFF32
:
2002 if (!bfd_link_executable (info
) && ABI_64_P (abfd
))
2003 return elf_x86_64_need_pic (info
, abfd
, sec
, h
, symtab_hdr
, isym
,
2004 &x86_64_elf_howto_table
[r_type
]);
2006 eh
->zero_undefweak
&= 0x2;
2009 case R_X86_64_GOTTPOFF
:
2010 if (!bfd_link_executable (info
))
2011 info
->flags
|= DF_STATIC_TLS
;
2014 case R_X86_64_GOT32
:
2015 case R_X86_64_GOTPCREL
:
2016 case R_X86_64_GOTPCRELX
:
2017 case R_X86_64_REX_GOTPCRELX
:
2018 case R_X86_64_TLSGD
:
2019 case R_X86_64_GOT64
:
2020 case R_X86_64_GOTPCREL64
:
2021 case R_X86_64_GOTPLT64
:
2022 case R_X86_64_GOTPC32_TLSDESC
:
2023 case R_X86_64_TLSDESC_CALL
:
2024 /* This symbol requires a global offset table entry. */
2026 int tls_type
, old_tls_type
;
2031 tls_type
= GOT_NORMAL
;
2034 if (ABS_SYMBOL_P (h
))
2037 else if (isym
->st_shndx
== SHN_ABS
)
2040 case R_X86_64_TLSGD
:
2041 tls_type
= GOT_TLS_GD
;
2043 case R_X86_64_GOTTPOFF
:
2044 tls_type
= GOT_TLS_IE
;
2046 case R_X86_64_GOTPC32_TLSDESC
:
2047 case R_X86_64_TLSDESC_CALL
:
2048 tls_type
= GOT_TLS_GDESC
;
2054 h
->got
.refcount
= 1;
2055 old_tls_type
= eh
->tls_type
;
2059 bfd_signed_vma
*local_got_refcounts
;
2061 if (!elf_x86_allocate_local_got_info (abfd
,
2062 symtab_hdr
->sh_info
))
2065 /* This is a global offset table entry for a local symbol. */
2066 local_got_refcounts
= elf_local_got_refcounts (abfd
);
2067 local_got_refcounts
[r_symndx
] = 1;
2069 = elf_x86_local_got_tls_type (abfd
) [r_symndx
];
2072 /* If a TLS symbol is accessed using IE at least once,
2073 there is no point to use dynamic model for it. */
2074 if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
2075 && (! GOT_TLS_GD_ANY_P (old_tls_type
)
2076 || tls_type
!= GOT_TLS_IE
))
2078 if (old_tls_type
== GOT_TLS_IE
&& GOT_TLS_GD_ANY_P (tls_type
))
2079 tls_type
= old_tls_type
;
2080 else if (GOT_TLS_GD_ANY_P (old_tls_type
)
2081 && GOT_TLS_GD_ANY_P (tls_type
))
2082 tls_type
|= old_tls_type
;
2086 name
= h
->root
.root
.string
;
2088 name
= bfd_elf_sym_name (abfd
, symtab_hdr
,
2091 /* xgettext:c-format */
2092 (_("%pB: '%s' accessed both as normal and"
2093 " thread local symbol"),
2095 bfd_set_error (bfd_error_bad_value
);
2100 if (old_tls_type
!= tls_type
)
2103 eh
->tls_type
= tls_type
;
2105 elf_x86_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
2110 case R_X86_64_GOTOFF64
:
2111 case R_X86_64_GOTPC32
:
2112 case R_X86_64_GOTPC64
:
2115 eh
->zero_undefweak
&= 0x2;
2118 case R_X86_64_PLT32
:
2119 case R_X86_64_PLT32_BND
:
2120 /* This symbol requires a procedure linkage table entry. We
2121 actually build the entry in adjust_dynamic_symbol,
2122 because this might be a case of linking PIC code which is
2123 never referenced by a dynamic object, in which case we
2124 don't need to generate a procedure linkage table entry
2127 /* If this is a local symbol, we resolve it directly without
2128 creating a procedure linkage table entry. */
2132 eh
->zero_undefweak
&= 0x2;
2134 h
->plt
.refcount
= 1;
2137 case R_X86_64_PLTOFF64
:
2138 /* This tries to form the 'address' of a function relative
2139 to GOT. For global symbols we need a PLT entry. */
2143 h
->plt
.refcount
= 1;
2147 case R_X86_64_SIZE32
:
2148 case R_X86_64_SIZE64
:
2153 if (!ABI_64_P (abfd
))
2159 /* Check relocation overflow as these relocs may lead to
2160 run-time relocation overflow. Don't error out for
2161 sections we don't care about, such as debug sections or
2162 when relocation overflow check is disabled. */
2163 if (!htab
->params
->no_reloc_overflow_check
2165 && (bfd_link_pic (info
)
2166 || (bfd_link_executable (info
)
2170 && (sec
->flags
& SEC_READONLY
) == 0)))
2171 return elf_x86_64_need_pic (info
, abfd
, sec
, h
, symtab_hdr
, isym
,
2172 &x86_64_elf_howto_table
[r_type
]);
2178 case R_X86_64_PC32_BND
:
2182 if (eh
!= NULL
&& (sec
->flags
& SEC_CODE
) != 0)
2183 eh
->zero_undefweak
|= 0x2;
2184 /* We are called after all symbols have been resolved. Only
2185 relocation against STT_GNU_IFUNC symbol must go through
2188 && (bfd_link_executable (info
)
2189 || h
->type
== STT_GNU_IFUNC
))
2191 bool func_pointer_ref
= false;
2193 if (r_type
== R_X86_64_PC32
)
2195 /* Since something like ".long foo - ." may be used
2196 as pointer, make sure that PLT is used if foo is
2197 a function defined in a shared library. */
2198 if ((sec
->flags
& SEC_CODE
) == 0)
2200 h
->pointer_equality_needed
= 1;
2201 if (bfd_link_pie (info
)
2202 && h
->type
== STT_FUNC
2207 h
->plt
.refcount
= 1;
2211 else if (r_type
!= R_X86_64_PC32_BND
2212 && r_type
!= R_X86_64_PC64
)
2214 /* At run-time, R_X86_64_64 can be resolved for both
2215 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2216 can only be resolved for x32. Function pointer
2217 reference doesn't need PLT for pointer equality. */
2218 if ((sec
->flags
& SEC_READONLY
) == 0
2219 && (r_type
== R_X86_64_64
2220 || (!ABI_64_P (abfd
)
2221 && (r_type
== R_X86_64_32
2222 || r_type
== R_X86_64_32S
))))
2223 func_pointer_ref
= true;
2225 /* IFUNC symbol needs pointer equality in PDE so that
2226 function pointer reference will be resolved to its
2227 PLT entry directly. */
2228 if (!func_pointer_ref
2229 || (bfd_link_pde (info
)
2230 && h
->type
== STT_GNU_IFUNC
))
2231 h
->pointer_equality_needed
= 1;
2234 if (!func_pointer_ref
)
2236 /* If this reloc is in a read-only section, we might
2237 need a copy reloc. We can't check reliably at this
2238 stage whether the section is read-only, as input
2239 sections have not yet been mapped to output sections.
2240 Tentatively set the flag for now, and correct in
2241 adjust_dynamic_symbol. */
2244 if (!elf_has_indirect_extern_access (sec
->owner
))
2245 eh
->non_got_ref_without_indirect_extern_access
= 1;
2247 /* We may need a .plt entry if the symbol is a function
2248 defined in a shared lib or is a function referenced
2249 from the code or read-only section. */
2251 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
2252 h
->plt
.refcount
= 1;
2254 if (h
->pointer_equality_needed
2255 && h
->type
== STT_FUNC
2256 && eh
->def_protected
2257 && !SYMBOL_DEFINED_NON_SHARED_P (h
)
2260 /* Disallow non-canonical reference to canonical
2261 protected function. */
2263 /* xgettext:c-format */
2264 (_("%pB: non-canonical reference to canonical "
2265 "protected function `%s' in %pB"),
2266 abfd
, h
->root
.root
.string
,
2267 h
->root
.u
.def
.section
->owner
);
2268 bfd_set_error (bfd_error_bad_value
);
2277 && NEED_DYNAMIC_RELOCATION_P (true, info
, true, h
, sec
,
2279 htab
->pointer_r_type
))
2281 struct elf_dyn_relocs
*p
;
2282 struct elf_dyn_relocs
**head
;
2284 /* If this is a global symbol, we count the number of
2285 relocations we need for this symbol. */
2287 head
= &h
->dyn_relocs
;
2290 /* Track dynamic relocs needed for local syms too.
2291 We really need local syms available to do this
2296 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
2301 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2305 /* Beware of type punned pointers vs strict aliasing
2307 vpp
= &(elf_section_data (s
)->local_dynrel
);
2308 head
= (struct elf_dyn_relocs
**)vpp
;
2312 if (p
== NULL
|| p
->sec
!= sec
)
2314 size_t amt
= sizeof *p
;
2316 p
= ((struct elf_dyn_relocs
*)
2317 bfd_alloc (htab
->elf
.dynobj
, amt
));
2328 /* Count size relocation as PC-relative relocation. */
2329 if (X86_PCREL_TYPE_P (true, r_type
) || size_reloc
)
2334 /* This relocation describes the C++ object vtable hierarchy.
2335 Reconstruct it for later use during GC. */
2336 case R_X86_64_GNU_VTINHERIT
:
2337 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
2341 /* This relocation describes which C++ vtable entries are actually
2342 used. Record for later use during GC. */
2343 case R_X86_64_GNU_VTENTRY
:
2344 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
2353 if (elf_section_data (sec
)->this_hdr
.contents
!= contents
)
2355 if (!converted
&& !_bfd_link_keep_memory (info
))
2359 /* Cache the section contents for elf_link_input_bfd if any
2360 load is converted or --no-keep-memory isn't used. */
2361 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2362 info
->cache_size
+= sec
->size
;
2366 /* Cache relocations if any load is converted. */
2367 if (elf_section_data (sec
)->relocs
!= relocs
&& converted
)
2368 elf_section_data (sec
)->relocs
= (Elf_Internal_Rela
*) relocs
;
2373 if (elf_section_data (sec
)->this_hdr
.contents
!= contents
)
2375 sec
->check_relocs_failed
= 1;
2380 elf_x86_64_always_size_sections (bfd
*output_bfd
,
2381 struct bfd_link_info
*info
)
2385 /* Scan relocations after rel_from_abs has been set on __ehdr_start. */
2386 for (abfd
= info
->input_bfds
;
2387 abfd
!= (bfd
*) NULL
;
2388 abfd
= abfd
->link
.next
)
2389 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
2390 && !_bfd_elf_link_iterate_on_relocs (abfd
, info
,
2391 elf_x86_64_scan_relocs
))
2394 return _bfd_x86_elf_always_size_sections (output_bfd
, info
);
2397 /* Return the relocation value for @tpoff relocation
2398 if STT_TLS virtual address is ADDRESS. */
2401 elf_x86_64_tpoff (struct bfd_link_info
*info
, bfd_vma address
)
2403 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2404 const struct elf_backend_data
*bed
= get_elf_backend_data (info
->output_bfd
);
2405 bfd_vma static_tls_size
;
2407 /* If tls_segment is NULL, we should have signalled an error already. */
2408 if (htab
->tls_sec
== NULL
)
2411 /* Consider special static TLS alignment requirements. */
2412 static_tls_size
= BFD_ALIGN (htab
->tls_size
, bed
->static_tls_alignment
);
2413 return address
- static_tls_size
- htab
->tls_sec
->vma
;
2416 /* Relocate an x86_64 ELF section. */
2419 elf_x86_64_relocate_section (bfd
*output_bfd
,
2420 struct bfd_link_info
*info
,
2422 asection
*input_section
,
2424 Elf_Internal_Rela
*relocs
,
2425 Elf_Internal_Sym
*local_syms
,
2426 asection
**local_sections
)
2428 struct elf_x86_link_hash_table
*htab
;
2429 Elf_Internal_Shdr
*symtab_hdr
;
2430 struct elf_link_hash_entry
**sym_hashes
;
2431 bfd_vma
*local_got_offsets
;
2432 bfd_vma
*local_tlsdesc_gotents
;
2433 Elf_Internal_Rela
*rel
;
2434 Elf_Internal_Rela
*wrel
;
2435 Elf_Internal_Rela
*relend
;
2436 unsigned int plt_entry_size
;
2439 /* Skip if check_relocs or scan_relocs failed. */
2440 if (input_section
->check_relocs_failed
)
2443 htab
= elf_x86_hash_table (info
, X86_64_ELF_DATA
);
2447 if (!is_x86_elf (input_bfd
, htab
))
2449 bfd_set_error (bfd_error_wrong_format
);
2453 plt_entry_size
= htab
->plt
.plt_entry_size
;
2454 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
2455 sym_hashes
= elf_sym_hashes (input_bfd
);
2456 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2457 local_tlsdesc_gotents
= elf_x86_local_tlsdesc_gotent (input_bfd
);
2459 _bfd_x86_elf_set_tls_module_base (info
);
2462 rel
= wrel
= relocs
;
2463 relend
= relocs
+ input_section
->reloc_count
;
2464 for (; rel
< relend
; wrel
++, rel
++)
2466 unsigned int r_type
, r_type_tls
;
2467 reloc_howto_type
*howto
;
2468 unsigned long r_symndx
;
2469 struct elf_link_hash_entry
*h
;
2470 struct elf_x86_link_hash_entry
*eh
;
2471 Elf_Internal_Sym
*sym
;
2473 bfd_vma off
, offplt
, plt_offset
;
2475 bool unresolved_reloc
;
2476 bfd_reloc_status_type r
;
2478 asection
*base_got
, *resolved_plt
;
2480 bool resolved_to_zero
;
2481 bool relative_reloc
;
2482 bool converted_reloc
;
2483 bool need_copy_reloc_in_pie
;
2484 bool no_copyreloc_p
;
2486 r_type
= ELF32_R_TYPE (rel
->r_info
);
2487 if (r_type
== (int) R_X86_64_GNU_VTINHERIT
2488 || r_type
== (int) R_X86_64_GNU_VTENTRY
)
2495 r_symndx
= htab
->r_sym (rel
->r_info
);
2496 converted_reloc
= (r_type
& R_X86_64_converted_reloc_bit
) != 0;
2497 if (converted_reloc
)
2499 r_type
&= ~R_X86_64_converted_reloc_bit
;
2500 rel
->r_info
= htab
->r_info (r_symndx
, r_type
);
2503 howto
= elf_x86_64_rtype_to_howto (input_bfd
, r_type
);
2505 return _bfd_unrecognized_reloc (input_bfd
, input_section
, r_type
);
2510 unresolved_reloc
= false;
2511 if (r_symndx
< symtab_hdr
->sh_info
)
2513 sym
= local_syms
+ r_symndx
;
2514 sec
= local_sections
[r_symndx
];
2516 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
,
2518 st_size
= sym
->st_size
;
2520 /* Relocate against local STT_GNU_IFUNC symbol. */
2521 if (!bfd_link_relocatable (info
)
2522 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
2524 h
= _bfd_elf_x86_get_local_sym_hash (htab
, input_bfd
,
2529 /* Set STT_GNU_IFUNC symbol value. */
2530 h
->root
.u
.def
.value
= sym
->st_value
;
2531 h
->root
.u
.def
.section
= sec
;
2536 bool warned ATTRIBUTE_UNUSED
;
2537 bool ignored ATTRIBUTE_UNUSED
;
2539 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2540 r_symndx
, symtab_hdr
, sym_hashes
,
2542 unresolved_reloc
, warned
, ignored
);
2546 if (sec
!= NULL
&& discarded_section (sec
))
2548 _bfd_clear_contents (howto
, input_bfd
, input_section
,
2549 contents
, rel
->r_offset
);
2550 wrel
->r_offset
= rel
->r_offset
;
2554 /* For ld -r, remove relocations in debug sections against
2555 sections defined in discarded sections. Not done for
2556 eh_frame editing code expects to be present. */
2557 if (bfd_link_relocatable (info
)
2558 && (input_section
->flags
& SEC_DEBUGGING
))
2564 if (bfd_link_relocatable (info
))
2571 if (rel
->r_addend
== 0 && !ABI_64_P (output_bfd
))
2573 if (r_type
== R_X86_64_64
)
2575 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
2576 zero-extend it to 64bit if addend is zero. */
2577 r_type
= R_X86_64_32
;
2578 memset (contents
+ rel
->r_offset
+ 4, 0, 4);
2580 else if (r_type
== R_X86_64_SIZE64
)
2582 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
2583 zero-extend it to 64bit if addend is zero. */
2584 r_type
= R_X86_64_SIZE32
;
2585 memset (contents
+ rel
->r_offset
+ 4, 0, 4);
2589 eh
= (struct elf_x86_link_hash_entry
*) h
;
2591 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2592 it here if it is defined in a non-shared object. */
2594 && h
->type
== STT_GNU_IFUNC
2600 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2602 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2603 STT_GNU_IFUNC symbol as STT_FUNC. */
2604 if (elf_section_type (input_section
) == SHT_NOTE
)
2606 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2607 sections because such sections are not SEC_ALLOC and
2608 thus ld.so will not process them. */
2609 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
2619 case R_X86_64_GOTPCREL
:
2620 case R_X86_64_GOTPCRELX
:
2621 case R_X86_64_REX_GOTPCRELX
:
2622 case R_X86_64_GOTPCREL64
:
2623 base_got
= htab
->elf
.sgot
;
2624 off
= h
->got
.offset
;
2626 if (base_got
== NULL
)
2629 if (off
== (bfd_vma
) -1)
2631 /* We can't use h->got.offset here to save state, or
2632 even just remember the offset, as finish_dynamic_symbol
2633 would use that as offset into .got. */
2635 if (h
->plt
.offset
== (bfd_vma
) -1)
2638 if (htab
->elf
.splt
!= NULL
)
2640 plt_index
= (h
->plt
.offset
/ plt_entry_size
2641 - htab
->plt
.has_plt0
);
2642 off
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
2643 base_got
= htab
->elf
.sgotplt
;
2647 plt_index
= h
->plt
.offset
/ plt_entry_size
;
2648 off
= plt_index
* GOT_ENTRY_SIZE
;
2649 base_got
= htab
->elf
.igotplt
;
2652 if (h
->dynindx
== -1
2656 /* This references the local defitionion. We must
2657 initialize this entry in the global offset table.
2658 Since the offset must always be a multiple of 8,
2659 we use the least significant bit to record
2660 whether we have initialized it already.
2662 When doing a dynamic link, we create a .rela.got
2663 relocation entry to initialize the value. This
2664 is done in the finish_dynamic_symbol routine. */
2669 bfd_put_64 (output_bfd
, relocation
,
2670 base_got
->contents
+ off
);
2671 /* Note that this is harmless for the GOTPLT64
2672 case, as -1 | 1 still is -1. */
2678 relocation
= (base_got
->output_section
->vma
2679 + base_got
->output_offset
+ off
);
2684 if (h
->plt
.offset
== (bfd_vma
) -1)
2686 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2687 if (r_type
== htab
->pointer_r_type
2688 && (input_section
->flags
& SEC_CODE
) == 0)
2689 goto do_ifunc_pointer
;
2690 goto bad_ifunc_reloc
;
2693 /* STT_GNU_IFUNC symbol must go through PLT. */
2694 if (htab
->elf
.splt
!= NULL
)
2696 if (htab
->plt_second
!= NULL
)
2698 resolved_plt
= htab
->plt_second
;
2699 plt_offset
= eh
->plt_second
.offset
;
2703 resolved_plt
= htab
->elf
.splt
;
2704 plt_offset
= h
->plt
.offset
;
2709 resolved_plt
= htab
->elf
.iplt
;
2710 plt_offset
= h
->plt
.offset
;
2713 relocation
= (resolved_plt
->output_section
->vma
2714 + resolved_plt
->output_offset
+ plt_offset
);
2720 if (h
->root
.root
.string
)
2721 name
= h
->root
.root
.string
;
2723 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
,
2726 /* xgettext:c-format */
2727 (_("%pB: relocation %s against STT_GNU_IFUNC "
2728 "symbol `%s' isn't supported"), input_bfd
,
2730 bfd_set_error (bfd_error_bad_value
);
2734 if (bfd_link_pic (info
))
2739 if (ABI_64_P (output_bfd
))
2744 if (rel
->r_addend
!= 0)
2746 if (h
->root
.root
.string
)
2747 name
= h
->root
.root
.string
;
2749 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
2752 /* xgettext:c-format */
2753 (_("%pB: relocation %s against STT_GNU_IFUNC "
2754 "symbol `%s' has non-zero addend: %" PRId64
),
2755 input_bfd
, howto
->name
, name
, (int64_t) rel
->r_addend
);
2756 bfd_set_error (bfd_error_bad_value
);
2760 /* Generate dynamic relcoation only when there is a
2761 non-GOT reference in a shared object or there is no
2763 if ((bfd_link_pic (info
) && h
->non_got_ref
)
2764 || h
->plt
.offset
== (bfd_vma
) -1)
2766 Elf_Internal_Rela outrel
;
2769 /* Need a dynamic relocation to get the real function
2771 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2775 if (outrel
.r_offset
== (bfd_vma
) -1
2776 || outrel
.r_offset
== (bfd_vma
) -2)
2779 outrel
.r_offset
+= (input_section
->output_section
->vma
2780 + input_section
->output_offset
);
2782 if (POINTER_LOCAL_IFUNC_P (info
, h
))
2784 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
2785 h
->root
.root
.string
,
2786 h
->root
.u
.def
.section
->owner
);
2788 /* This symbol is resolved locally. */
2789 outrel
.r_info
= htab
->r_info (0, R_X86_64_IRELATIVE
);
2790 outrel
.r_addend
= (h
->root
.u
.def
.value
2791 + h
->root
.u
.def
.section
->output_section
->vma
2792 + h
->root
.u
.def
.section
->output_offset
);
2794 if (htab
->params
->report_relative_reloc
)
2795 _bfd_x86_elf_link_report_relative_reloc
2796 (info
, input_section
, h
, sym
,
2797 "R_X86_64_IRELATIVE", &outrel
);
2801 outrel
.r_info
= htab
->r_info (h
->dynindx
, r_type
);
2802 outrel
.r_addend
= 0;
2805 /* Dynamic relocations are stored in
2806 1. .rela.ifunc section in PIC object.
2807 2. .rela.got section in dynamic executable.
2808 3. .rela.iplt section in static executable. */
2809 if (bfd_link_pic (info
))
2810 sreloc
= htab
->elf
.irelifunc
;
2811 else if (htab
->elf
.splt
!= NULL
)
2812 sreloc
= htab
->elf
.srelgot
;
2814 sreloc
= htab
->elf
.irelplt
;
2815 elf_append_rela (output_bfd
, sreloc
, &outrel
);
2817 /* If this reloc is against an external symbol, we
2818 do not want to fiddle with the addend. Otherwise,
2819 we need to include the symbol value so that it
2820 becomes an addend for the dynamic reloc. For an
2821 internal symbol, we have updated addend. */
2826 case R_X86_64_PC32_BND
:
2828 case R_X86_64_PLT32
:
2829 case R_X86_64_PLT32_BND
:
2835 resolved_to_zero
= (eh
!= NULL
2836 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
, eh
));
2838 /* When generating a shared object, the relocations handled here are
2839 copied into the output file to be resolved at run time. */
2842 case R_X86_64_GOT32
:
2843 case R_X86_64_GOT64
:
2844 /* Relocation is to the entry for this symbol in the global
2846 case R_X86_64_GOTPCREL
:
2847 case R_X86_64_GOTPCRELX
:
2848 case R_X86_64_REX_GOTPCRELX
:
2849 case R_X86_64_GOTPCREL64
:
2850 /* Use global offset table entry as symbol value. */
2851 case R_X86_64_GOTPLT64
:
2852 /* This is obsolete and treated the same as GOT64. */
2853 base_got
= htab
->elf
.sgot
;
2855 if (htab
->elf
.sgot
== NULL
)
2858 relative_reloc
= false;
2861 off
= h
->got
.offset
;
2863 && h
->plt
.offset
!= (bfd_vma
)-1
2864 && off
== (bfd_vma
)-1)
2866 /* We can't use h->got.offset here to save
2867 state, or even just remember the offset, as
2868 finish_dynamic_symbol would use that as offset into
2870 bfd_vma plt_index
= (h
->plt
.offset
/ plt_entry_size
2871 - htab
->plt
.has_plt0
);
2872 off
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
2873 base_got
= htab
->elf
.sgotplt
;
2876 if (RESOLVED_LOCALLY_P (info
, h
, htab
))
2878 /* We must initialize this entry in the global offset
2879 table. Since the offset must always be a multiple
2880 of 8, we use the least significant bit to record
2881 whether we have initialized it already.
2883 When doing a dynamic link, we create a .rela.got
2884 relocation entry to initialize the value. This is
2885 done in the finish_dynamic_symbol routine. */
2890 bfd_put_64 (output_bfd
, relocation
,
2891 base_got
->contents
+ off
);
2892 /* Note that this is harmless for the GOTPLT64 case,
2893 as -1 | 1 still is -1. */
2896 /* NB: Don't generate relative relocation here if
2897 it has been generated by DT_RELR. */
2898 if (!info
->enable_dt_relr
2899 && GENERATE_RELATIVE_RELOC_P (info
, h
))
2901 /* If this symbol isn't dynamic in PIC,
2902 generate R_X86_64_RELATIVE here. */
2903 eh
->no_finish_dynamic_symbol
= 1;
2904 relative_reloc
= true;
2909 unresolved_reloc
= false;
2913 if (local_got_offsets
== NULL
)
2916 off
= local_got_offsets
[r_symndx
];
2918 /* The offset must always be a multiple of 8. We use
2919 the least significant bit to record whether we have
2920 already generated the necessary reloc. */
2925 bfd_put_64 (output_bfd
, relocation
,
2926 base_got
->contents
+ off
);
2927 local_got_offsets
[r_symndx
] |= 1;
2929 /* NB: GOTPCREL relocations against local absolute
2930 symbol store relocation value in the GOT slot
2931 without relative relocation. Don't generate
2932 relative relocation here if it has been generated
2934 if (!info
->enable_dt_relr
2935 && bfd_link_pic (info
)
2936 && !(sym
->st_shndx
== SHN_ABS
2937 && (r_type
== R_X86_64_GOTPCREL
2938 || r_type
== R_X86_64_GOTPCRELX
2939 || r_type
== R_X86_64_REX_GOTPCRELX
)))
2940 relative_reloc
= true;
2947 Elf_Internal_Rela outrel
;
2949 /* We need to generate a R_X86_64_RELATIVE reloc
2950 for the dynamic linker. */
2951 s
= htab
->elf
.srelgot
;
2955 outrel
.r_offset
= (base_got
->output_section
->vma
2956 + base_got
->output_offset
2958 outrel
.r_info
= htab
->r_info (0, R_X86_64_RELATIVE
);
2959 outrel
.r_addend
= relocation
;
2961 if (htab
->params
->report_relative_reloc
)
2962 _bfd_x86_elf_link_report_relative_reloc
2963 (info
, input_section
, h
, sym
, "R_X86_64_RELATIVE",
2966 elf_append_rela (output_bfd
, s
, &outrel
);
2969 if (off
>= (bfd_vma
) -2)
2972 relocation
= base_got
->output_section
->vma
2973 + base_got
->output_offset
+ off
;
2974 if (r_type
!= R_X86_64_GOTPCREL
2975 && r_type
!= R_X86_64_GOTPCRELX
2976 && r_type
!= R_X86_64_REX_GOTPCRELX
2977 && r_type
!= R_X86_64_GOTPCREL64
)
2978 relocation
-= htab
->elf
.sgotplt
->output_section
->vma
2979 - htab
->elf
.sgotplt
->output_offset
;
2983 case R_X86_64_GOTOFF64
:
2984 /* Relocation is relative to the start of the global offset
2987 /* Check to make sure it isn't a protected function or data
2988 symbol for shared library since it may not be local when
2989 used as function address or with copy relocation. We also
2990 need to make sure that a symbol is referenced locally. */
2991 if (bfd_link_pic (info
) && h
)
2993 if (!h
->def_regular
)
2997 switch (ELF_ST_VISIBILITY (h
->other
))
3000 v
= _("hidden symbol");
3003 v
= _("internal symbol");
3006 v
= _("protected symbol");
3014 /* xgettext:c-format */
3015 (_("%pB: relocation R_X86_64_GOTOFF64 against undefined %s"
3016 " `%s' can not be used when making a shared object"),
3017 input_bfd
, v
, h
->root
.root
.string
);
3018 bfd_set_error (bfd_error_bad_value
);
3021 else if (!bfd_link_executable (info
)
3022 && !SYMBOL_REFERENCES_LOCAL_P (info
, h
)
3023 && (h
->type
== STT_FUNC
3024 || h
->type
== STT_OBJECT
)
3025 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
3028 /* xgettext:c-format */
3029 (_("%pB: relocation R_X86_64_GOTOFF64 against protected %s"
3030 " `%s' can not be used when making a shared object"),
3032 h
->type
== STT_FUNC
? "function" : "data",
3033 h
->root
.root
.string
);
3034 bfd_set_error (bfd_error_bad_value
);
3039 /* Note that sgot is not involved in this
3040 calculation. We always want the start of .got.plt. If we
3041 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3042 permitted by the ABI, we might have to change this
3044 relocation
-= htab
->elf
.sgotplt
->output_section
->vma
3045 + htab
->elf
.sgotplt
->output_offset
;
3048 case R_X86_64_GOTPC32
:
3049 case R_X86_64_GOTPC64
:
3050 /* Use global offset table as symbol value. */
3051 relocation
= htab
->elf
.sgotplt
->output_section
->vma
3052 + htab
->elf
.sgotplt
->output_offset
;
3053 unresolved_reloc
= false;
3056 case R_X86_64_PLTOFF64
:
3057 /* Relocation is PLT entry relative to GOT. For local
3058 symbols it's the symbol itself relative to GOT. */
3060 /* See PLT32 handling. */
3061 && (h
->plt
.offset
!= (bfd_vma
) -1
3062 || eh
->plt_got
.offset
!= (bfd_vma
) -1)
3063 && htab
->elf
.splt
!= NULL
)
3065 if (eh
->plt_got
.offset
!= (bfd_vma
) -1)
3067 /* Use the GOT PLT. */
3068 resolved_plt
= htab
->plt_got
;
3069 plt_offset
= eh
->plt_got
.offset
;
3071 else if (htab
->plt_second
!= NULL
)
3073 resolved_plt
= htab
->plt_second
;
3074 plt_offset
= eh
->plt_second
.offset
;
3078 resolved_plt
= htab
->elf
.splt
;
3079 plt_offset
= h
->plt
.offset
;
3082 relocation
= (resolved_plt
->output_section
->vma
3083 + resolved_plt
->output_offset
3085 unresolved_reloc
= false;
3088 relocation
-= htab
->elf
.sgotplt
->output_section
->vma
3089 + htab
->elf
.sgotplt
->output_offset
;
3092 case R_X86_64_PLT32
:
3093 case R_X86_64_PLT32_BND
:
3094 /* Relocation is to the entry for this symbol in the
3095 procedure linkage table. */
3097 /* Resolve a PLT32 reloc against a local symbol directly,
3098 without using the procedure linkage table. */
3102 if ((h
->plt
.offset
== (bfd_vma
) -1
3103 && eh
->plt_got
.offset
== (bfd_vma
) -1)
3104 || htab
->elf
.splt
== NULL
)
3106 /* We didn't make a PLT entry for this symbol. This
3107 happens when statically linking PIC code, or when
3108 using -Bsymbolic. */
3113 if (h
->plt
.offset
!= (bfd_vma
) -1)
3115 if (htab
->plt_second
!= NULL
)
3117 resolved_plt
= htab
->plt_second
;
3118 plt_offset
= eh
->plt_second
.offset
;
3122 resolved_plt
= htab
->elf
.splt
;
3123 plt_offset
= h
->plt
.offset
;
3128 /* Use the GOT PLT. */
3129 resolved_plt
= htab
->plt_got
;
3130 plt_offset
= eh
->plt_got
.offset
;
3133 relocation
= (resolved_plt
->output_section
->vma
3134 + resolved_plt
->output_offset
3136 unresolved_reloc
= false;
3139 case R_X86_64_SIZE32
:
3140 case R_X86_64_SIZE64
:
3141 /* Set to symbol size. */
3142 relocation
= st_size
;
3148 case R_X86_64_PC32_BND
:
3149 /* Don't complain about -fPIC if the symbol is undefined when
3150 building executable unless it is unresolved weak symbol,
3151 references a dynamic definition in PIE or -z nocopyreloc
3154 = (info
->nocopyreloc
3156 && !h
->root
.linker_def
3157 && !h
->root
.ldscript_def
3158 && eh
->def_protected
));
3160 if ((input_section
->flags
& SEC_ALLOC
) != 0
3161 && (input_section
->flags
& SEC_READONLY
) != 0
3163 && ((bfd_link_executable (info
)
3164 && ((h
->root
.type
== bfd_link_hash_undefweak
3166 || !UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
,
3168 || (bfd_link_pie (info
)
3169 && !SYMBOL_DEFINED_NON_SHARED_P (h
)
3173 && !(h
->root
.u
.def
.section
->flags
& SEC_CODE
))))
3174 || (bfd_link_pie (info
)
3175 && h
->root
.type
== bfd_link_hash_undefweak
)
3176 || bfd_link_dll (info
)))
3179 if (SYMBOL_REFERENCES_LOCAL_P (info
, h
))
3181 /* Symbol is referenced locally. Make sure it is
3183 fail
= !SYMBOL_DEFINED_NON_SHARED_P (h
);
3185 else if (bfd_link_pie (info
))
3187 /* We can only use PC-relative relocations in PIE
3188 from non-code sections. */
3189 if (h
->root
.type
== bfd_link_hash_undefweak
3190 || (h
->type
== STT_FUNC
3191 && (sec
->flags
& SEC_CODE
) != 0))
3194 else if (no_copyreloc_p
|| bfd_link_dll (info
))
3196 /* Symbol doesn't need copy reloc and isn't
3197 referenced locally. Don't allow PC-relative
3198 relocations against default and protected
3199 symbols since address of protected function
3200 and location of protected data may not be in
3201 the shared object. */
3202 fail
= (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
3203 || ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
);
3207 return elf_x86_64_need_pic (info
, input_bfd
, input_section
,
3208 h
, NULL
, NULL
, howto
);
3210 /* Since x86-64 has PC-relative PLT, we can use PLT in PIE
3211 as function address. */
3213 && (input_section
->flags
& SEC_CODE
) == 0
3214 && bfd_link_pie (info
)
3215 && h
->type
== STT_FUNC
3226 /* FIXME: The ABI says the linker should make sure the value is
3227 the same when it's zeroextended to 64 bit. */
3230 if ((input_section
->flags
& SEC_ALLOC
) == 0)
3233 need_copy_reloc_in_pie
= (bfd_link_pie (info
)
3238 == bfd_link_hash_undefined
))
3239 && (X86_PCREL_TYPE_P (true, r_type
)
3240 || X86_SIZE_TYPE_P (true,
3243 if (GENERATE_DYNAMIC_RELOCATION_P (true, info
, eh
, r_type
, sec
,
3244 need_copy_reloc_in_pie
,
3245 resolved_to_zero
, false))
3247 Elf_Internal_Rela outrel
;
3248 bool skip
, relocate
;
3249 bool generate_dynamic_reloc
= true;
3251 const char *relative_reloc_name
= NULL
;
3253 /* When generating a shared object, these relocations
3254 are copied into the output file to be resolved at run
3260 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
3262 if (outrel
.r_offset
== (bfd_vma
) -1)
3264 else if (outrel
.r_offset
== (bfd_vma
) -2)
3265 skip
= true, relocate
= true;
3267 outrel
.r_offset
+= (input_section
->output_section
->vma
3268 + input_section
->output_offset
);
3271 memset (&outrel
, 0, sizeof outrel
);
3273 else if (COPY_INPUT_RELOC_P (true, info
, h
, r_type
))
3275 outrel
.r_info
= htab
->r_info (h
->dynindx
, r_type
);
3276 outrel
.r_addend
= rel
->r_addend
;
3280 /* This symbol is local, or marked to become local.
3281 When relocation overflow check is disabled, we
3282 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
3283 if (r_type
== htab
->pointer_r_type
3284 || (r_type
== R_X86_64_32
3285 && htab
->params
->no_reloc_overflow_check
))
3288 /* NB: Don't generate relative relocation here if
3289 it has been generated by DT_RELR. */
3290 if (info
->enable_dt_relr
)
3291 generate_dynamic_reloc
= false;
3295 htab
->r_info (0, R_X86_64_RELATIVE
);
3296 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3297 relative_reloc_name
= "R_X86_64_RELATIVE";
3300 else if (r_type
== R_X86_64_64
3301 && !ABI_64_P (output_bfd
))
3304 outrel
.r_info
= htab
->r_info (0,
3305 R_X86_64_RELATIVE64
);
3306 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3307 relative_reloc_name
= "R_X86_64_RELATIVE64";
3308 /* Check addend overflow. */
3309 if ((outrel
.r_addend
& 0x80000000)
3310 != (rel
->r_addend
& 0x80000000))
3313 int addend
= rel
->r_addend
;
3314 if (h
&& h
->root
.root
.string
)
3315 name
= h
->root
.root
.string
;
3317 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
3320 /* xgettext:c-format */
3321 (_("%pB: addend %s%#x in relocation %s against "
3322 "symbol `%s' at %#" PRIx64
3323 " in section `%pA' is out of range"),
3324 input_bfd
, addend
< 0 ? "-" : "", addend
,
3325 howto
->name
, name
, (uint64_t) rel
->r_offset
,
3327 bfd_set_error (bfd_error_bad_value
);
3335 if (bfd_is_abs_section (sec
))
3337 else if (sec
== NULL
|| sec
->owner
== NULL
)
3339 bfd_set_error (bfd_error_bad_value
);
3346 /* We are turning this relocation into one
3347 against a section symbol. It would be
3348 proper to subtract the symbol's value,
3349 osec->vma, from the emitted reloc addend,
3350 but ld.so expects buggy relocs. */
3351 osec
= sec
->output_section
;
3352 sindx
= elf_section_data (osec
)->dynindx
;
3355 asection
*oi
= htab
->elf
.text_index_section
;
3356 sindx
= elf_section_data (oi
)->dynindx
;
3358 BFD_ASSERT (sindx
!= 0);
3361 outrel
.r_info
= htab
->r_info (sindx
, r_type
);
3362 outrel
.r_addend
= relocation
+ rel
->r_addend
;
3366 if (generate_dynamic_reloc
)
3368 sreloc
= elf_section_data (input_section
)->sreloc
;
3370 if (sreloc
== NULL
|| sreloc
->contents
== NULL
)
3372 r
= bfd_reloc_notsupported
;
3373 goto check_relocation_error
;
3376 if (relative_reloc_name
3377 && htab
->params
->report_relative_reloc
)
3378 _bfd_x86_elf_link_report_relative_reloc
3379 (info
, input_section
, h
, sym
,
3380 relative_reloc_name
, &outrel
);
3382 elf_append_rela (output_bfd
, sreloc
, &outrel
);
3385 /* If this reloc is against an external symbol, we do
3386 not want to fiddle with the addend. Otherwise, we
3387 need to include the symbol value so that it becomes
3388 an addend for the dynamic reloc. */
3395 case R_X86_64_TLSGD
:
3396 case R_X86_64_GOTPC32_TLSDESC
:
3397 case R_X86_64_TLSDESC_CALL
:
3398 case R_X86_64_GOTTPOFF
:
3399 tls_type
= GOT_UNKNOWN
;
3400 if (h
== NULL
&& local_got_offsets
)
3401 tls_type
= elf_x86_local_got_tls_type (input_bfd
) [r_symndx
];
3403 tls_type
= elf_x86_hash_entry (h
)->tls_type
;
3405 r_type_tls
= r_type
;
3406 if (! elf_x86_64_tls_transition (info
, input_bfd
,
3407 input_section
, contents
,
3408 symtab_hdr
, sym_hashes
,
3409 &r_type_tls
, tls_type
, rel
,
3410 relend
, h
, r_symndx
, true))
3413 if (r_type_tls
== R_X86_64_TPOFF32
)
3415 bfd_vma roff
= rel
->r_offset
;
3417 BFD_ASSERT (! unresolved_reloc
);
3419 if (r_type
== R_X86_64_TLSGD
)
3421 /* GD->LE transition. For 64bit, change
3422 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3423 .word 0x6666; rex64; call __tls_get_addr@PLT
3425 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3427 call *__tls_get_addr@GOTPCREL(%rip)
3428 which may be converted to
3429 addr32 call __tls_get_addr
3432 leaq foo@tpoff(%rax), %rax
3434 leaq foo@tlsgd(%rip), %rdi
3435 .word 0x6666; rex64; call __tls_get_addr@PLT
3437 leaq foo@tlsgd(%rip), %rdi
3439 call *__tls_get_addr@GOTPCREL(%rip)
3440 which may be converted to
3441 addr32 call __tls_get_addr
3444 leaq foo@tpoff(%rax), %rax
3445 For largepic, change:
3446 leaq foo@tlsgd(%rip), %rdi
3447 movabsq $__tls_get_addr@pltoff, %rax
3452 leaq foo@tpoff(%rax), %rax
3453 nopw 0x0(%rax,%rax,1) */
3455 if (ABI_64_P (output_bfd
))
3457 if (contents
[roff
+ 5] == 0xb8)
3460 || (roff
- 3 + 22) > input_section
->size
)
3463 info
->callbacks
->einfo
3464 (_("%F%P: corrupt input: %pB\n"),
3468 memcpy (contents
+ roff
- 3,
3469 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
3470 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3476 || (roff
- 4 + 16) > input_section
->size
)
3478 memcpy (contents
+ roff
- 4,
3479 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3486 || (roff
- 3 + 15) > input_section
->size
)
3488 memcpy (contents
+ roff
- 3,
3489 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3492 bfd_put_32 (output_bfd
,
3493 elf_x86_64_tpoff (info
, relocation
),
3494 contents
+ roff
+ 8 + largepic
);
3495 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
3496 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
3501 else if (r_type
== R_X86_64_GOTPC32_TLSDESC
)
3503 /* GDesc -> LE transition.
3504 It's originally something like:
3505 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3506 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
3509 movq $x@tpoff, %rax <--- LP64 mode.
3510 rex movl $x@tpoff, %eax <--- X32 mode.
3513 unsigned int val
, type
;
3517 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
3518 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
3519 bfd_put_8 (output_bfd
,
3520 (type
& 0x48) | ((type
>> 2) & 1),
3521 contents
+ roff
- 3);
3522 bfd_put_8 (output_bfd
, 0xc7, contents
+ roff
- 2);
3523 bfd_put_8 (output_bfd
, 0xc0 | ((val
>> 3) & 7),
3524 contents
+ roff
- 1);
3525 bfd_put_32 (output_bfd
,
3526 elf_x86_64_tpoff (info
, relocation
),
3530 else if (r_type
== R_X86_64_TLSDESC_CALL
)
3532 /* GDesc -> LE transition.
3534 call *(%rax) <--- LP64 mode.
3535 call *(%eax) <--- X32 mode.
3537 xchg %ax,%ax <-- LP64 mode.
3538 nopl (%rax) <-- X32 mode.
3540 unsigned int prefix
= 0;
3541 if (!ABI_64_P (input_bfd
))
3543 /* Check for call *x@tlsdesc(%eax). */
3544 if (contents
[roff
] == 0x67)
3549 bfd_put_8 (output_bfd
, 0x0f, contents
+ roff
);
3550 bfd_put_8 (output_bfd
, 0x1f, contents
+ roff
+ 1);
3551 bfd_put_8 (output_bfd
, 0x00, contents
+ roff
+ 2);
3555 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
3556 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
3560 else if (r_type
== R_X86_64_GOTTPOFF
)
3562 /* IE->LE transition:
3563 For 64bit, originally it can be one of:
3564 movq foo@gottpoff(%rip), %reg
3565 addq foo@gottpoff(%rip), %reg
3568 leaq foo(%reg), %reg
3570 For 32bit, originally it can be one of:
3571 movq foo@gottpoff(%rip), %reg
3572 addl foo@gottpoff(%rip), %reg
3575 leal foo(%reg), %reg
3578 unsigned int val
, type
, reg
;
3581 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
3588 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
3589 reg
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
3598 bfd_put_8 (output_bfd
, 0x49,
3599 contents
+ roff
- 3);
3601 else if (!ABI_64_P (output_bfd
) && val
== 0x44)
3605 bfd_put_8 (output_bfd
, 0x41,
3606 contents
+ roff
- 3);
3608 bfd_put_8 (output_bfd
, 0xc7,
3609 contents
+ roff
- 2);
3610 bfd_put_8 (output_bfd
, 0xc0 | reg
,
3611 contents
+ roff
- 1);
3615 /* addq/addl -> addq/addl - addressing with %rsp/%r12
3621 bfd_put_8 (output_bfd
, 0x49,
3622 contents
+ roff
- 3);
3624 else if (!ABI_64_P (output_bfd
) && val
== 0x44)
3628 bfd_put_8 (output_bfd
, 0x41,
3629 contents
+ roff
- 3);
3631 bfd_put_8 (output_bfd
, 0x81,
3632 contents
+ roff
- 2);
3633 bfd_put_8 (output_bfd
, 0xc0 | reg
,
3634 contents
+ roff
- 1);
3638 /* addq/addl -> leaq/leal */
3643 bfd_put_8 (output_bfd
, 0x4d,
3644 contents
+ roff
- 3);
3646 else if (!ABI_64_P (output_bfd
) && val
== 0x44)
3650 bfd_put_8 (output_bfd
, 0x45,
3651 contents
+ roff
- 3);
3653 bfd_put_8 (output_bfd
, 0x8d,
3654 contents
+ roff
- 2);
3655 bfd_put_8 (output_bfd
, 0x80 | reg
| (reg
<< 3),
3656 contents
+ roff
- 1);
3658 bfd_put_32 (output_bfd
,
3659 elf_x86_64_tpoff (info
, relocation
),
3667 if (htab
->elf
.sgot
== NULL
)
3672 off
= h
->got
.offset
;
3673 offplt
= elf_x86_hash_entry (h
)->tlsdesc_got
;
3677 if (local_got_offsets
== NULL
)
3680 off
= local_got_offsets
[r_symndx
];
3681 offplt
= local_tlsdesc_gotents
[r_symndx
];
3688 Elf_Internal_Rela outrel
;
3692 if (htab
->elf
.srelgot
== NULL
)
3695 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
3697 if (GOT_TLS_GDESC_P (tls_type
))
3699 outrel
.r_info
= htab
->r_info (indx
, R_X86_64_TLSDESC
);
3700 BFD_ASSERT (htab
->sgotplt_jump_table_size
+ offplt
3701 + 2 * GOT_ENTRY_SIZE
<= htab
->elf
.sgotplt
->size
);
3702 outrel
.r_offset
= (htab
->elf
.sgotplt
->output_section
->vma
3703 + htab
->elf
.sgotplt
->output_offset
3705 + htab
->sgotplt_jump_table_size
);
3706 sreloc
= htab
->elf
.srelplt
;
3708 outrel
.r_addend
= relocation
- _bfd_x86_elf_dtpoff_base (info
);
3710 outrel
.r_addend
= 0;
3711 elf_append_rela (output_bfd
, sreloc
, &outrel
);
3714 sreloc
= htab
->elf
.srelgot
;
3716 outrel
.r_offset
= (htab
->elf
.sgot
->output_section
->vma
3717 + htab
->elf
.sgot
->output_offset
+ off
);
3719 if (GOT_TLS_GD_P (tls_type
))
3720 dr_type
= R_X86_64_DTPMOD64
;
3721 else if (GOT_TLS_GDESC_P (tls_type
))
3724 dr_type
= R_X86_64_TPOFF64
;
3726 bfd_put_64 (output_bfd
, 0, htab
->elf
.sgot
->contents
+ off
);
3727 outrel
.r_addend
= 0;
3728 if ((dr_type
== R_X86_64_TPOFF64
3729 || dr_type
== R_X86_64_TLSDESC
) && indx
== 0)
3730 outrel
.r_addend
= relocation
- _bfd_x86_elf_dtpoff_base (info
);
3731 outrel
.r_info
= htab
->r_info (indx
, dr_type
);
3733 elf_append_rela (output_bfd
, sreloc
, &outrel
);
3735 if (GOT_TLS_GD_P (tls_type
))
3739 BFD_ASSERT (! unresolved_reloc
);
3740 bfd_put_64 (output_bfd
,
3741 relocation
- _bfd_x86_elf_dtpoff_base (info
),
3742 htab
->elf
.sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3746 bfd_put_64 (output_bfd
, 0,
3747 htab
->elf
.sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3748 outrel
.r_info
= htab
->r_info (indx
,
3750 outrel
.r_offset
+= GOT_ENTRY_SIZE
;
3751 elf_append_rela (output_bfd
, sreloc
,
3760 local_got_offsets
[r_symndx
] |= 1;
3763 if (off
>= (bfd_vma
) -2
3764 && ! GOT_TLS_GDESC_P (tls_type
))
3766 if (r_type_tls
== r_type
)
3768 if (r_type
== R_X86_64_GOTPC32_TLSDESC
3769 || r_type
== R_X86_64_TLSDESC_CALL
)
3770 relocation
= htab
->elf
.sgotplt
->output_section
->vma
3771 + htab
->elf
.sgotplt
->output_offset
3772 + offplt
+ htab
->sgotplt_jump_table_size
;
3774 relocation
= htab
->elf
.sgot
->output_section
->vma
3775 + htab
->elf
.sgot
->output_offset
+ off
;
3776 unresolved_reloc
= false;
3780 bfd_vma roff
= rel
->r_offset
;
3782 if (r_type
== R_X86_64_TLSGD
)
3784 /* GD->IE transition. For 64bit, change
3785 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3786 .word 0x6666; rex64; call __tls_get_addr@PLT
3788 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3790 call *__tls_get_addr@GOTPCREL(%rip
3791 which may be converted to
3792 addr32 call __tls_get_addr
3795 addq foo@gottpoff(%rip), %rax
3797 leaq foo@tlsgd(%rip), %rdi
3798 .word 0x6666; rex64; call __tls_get_addr@PLT
3800 leaq foo@tlsgd(%rip), %rdi
3802 call *__tls_get_addr@GOTPCREL(%rip)
3803 which may be converted to
3804 addr32 call __tls_get_addr
3807 addq foo@gottpoff(%rip), %rax
3808 For largepic, change:
3809 leaq foo@tlsgd(%rip), %rdi
3810 movabsq $__tls_get_addr@pltoff, %rax
3815 addq foo@gottpoff(%rax), %rax
3816 nopw 0x0(%rax,%rax,1) */
3818 if (ABI_64_P (output_bfd
))
3820 if (contents
[roff
+ 5] == 0xb8)
3823 || (roff
- 3 + 22) > input_section
->size
)
3825 memcpy (contents
+ roff
- 3,
3826 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
3827 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3833 || (roff
- 4 + 16) > input_section
->size
)
3835 memcpy (contents
+ roff
- 4,
3836 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3843 || (roff
- 3 + 15) > input_section
->size
)
3845 memcpy (contents
+ roff
- 3,
3846 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3850 relocation
= (htab
->elf
.sgot
->output_section
->vma
3851 + htab
->elf
.sgot
->output_offset
+ off
3854 - input_section
->output_section
->vma
3855 - input_section
->output_offset
3857 bfd_put_32 (output_bfd
, relocation
,
3858 contents
+ roff
+ 8 + largepic
);
3859 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
3864 else if (r_type
== R_X86_64_GOTPC32_TLSDESC
)
3866 /* GDesc -> IE transition.
3867 It's originally something like:
3868 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3869 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
3872 # before xchg %ax,%ax in LP64 mode.
3873 movq x@gottpoff(%rip), %rax
3874 # before nopl (%rax) in X32 mode.
3875 rex movl x@gottpoff(%rip), %eax
3878 /* Now modify the instruction as appropriate. To
3879 turn a lea into a mov in the form we use it, it
3880 suffices to change the second byte from 0x8d to
3884 bfd_put_8 (output_bfd
, 0x8b, contents
+ roff
- 2);
3886 bfd_put_32 (output_bfd
,
3887 htab
->elf
.sgot
->output_section
->vma
3888 + htab
->elf
.sgot
->output_offset
+ off
3890 - input_section
->output_section
->vma
3891 - input_section
->output_offset
3896 else if (r_type
== R_X86_64_TLSDESC_CALL
)
3898 /* GDesc -> IE transition.
3900 call *(%rax) <--- LP64 mode.
3901 call *(%eax) <--- X32 mode.
3904 xchg %ax, %ax <-- LP64 mode.
3905 nopl (%rax) <-- X32 mode.
3908 unsigned int prefix
= 0;
3909 if (!ABI_64_P (input_bfd
))
3911 /* Check for call *x@tlsdesc(%eax). */
3912 if (contents
[roff
] == 0x67)
3917 bfd_put_8 (output_bfd
, 0x0f, contents
+ roff
);
3918 bfd_put_8 (output_bfd
, 0x1f, contents
+ roff
+ 1);
3919 bfd_put_8 (output_bfd
, 0x00, contents
+ roff
+ 2);
3923 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
3924 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
3933 case R_X86_64_TLSLD
:
3934 if (! elf_x86_64_tls_transition (info
, input_bfd
,
3935 input_section
, contents
,
3936 symtab_hdr
, sym_hashes
,
3937 &r_type
, GOT_UNKNOWN
, rel
,
3938 relend
, h
, r_symndx
, true))
3941 if (r_type
!= R_X86_64_TLSLD
)
3943 /* LD->LE transition:
3944 leaq foo@tlsld(%rip), %rdi
3945 call __tls_get_addr@PLT
3946 For 64bit, we change it into:
3947 .word 0x6666; .byte 0x66; movq %fs:0, %rax
3948 For 32bit, we change it into:
3949 nopl 0x0(%rax); movl %fs:0, %eax
3951 leaq foo@tlsld(%rip), %rdi;
3952 call *__tls_get_addr@GOTPCREL(%rip)
3953 which may be converted to
3954 addr32 call __tls_get_addr
3955 For 64bit, we change it into:
3956 .word 0x6666; .word 0x6666; movq %fs:0, %rax
3957 For 32bit, we change it into:
3958 nopw 0x0(%rax); movl %fs:0, %eax
3959 For largepic, change:
3960 leaq foo@tlsgd(%rip), %rdi
3961 movabsq $__tls_get_addr@pltoff, %rax
3965 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
3968 BFD_ASSERT (r_type
== R_X86_64_TPOFF32
);
3969 if (ABI_64_P (output_bfd
))
3971 if ((rel
->r_offset
+ 5) >= input_section
->size
)
3973 if (contents
[rel
->r_offset
+ 5] == 0xb8)
3975 if (rel
->r_offset
< 3
3976 || (rel
->r_offset
- 3 + 22) > input_section
->size
)
3978 memcpy (contents
+ rel
->r_offset
- 3,
3979 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
3980 "\x64\x48\x8b\x04\x25\0\0\0", 22);
3982 else if (contents
[rel
->r_offset
+ 4] == 0xff
3983 || contents
[rel
->r_offset
+ 4] == 0x67)
3985 if (rel
->r_offset
< 3
3986 || (rel
->r_offset
- 3 + 13) > input_section
->size
)
3988 memcpy (contents
+ rel
->r_offset
- 3,
3989 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
3995 if (rel
->r_offset
< 3
3996 || (rel
->r_offset
- 3 + 12) > input_section
->size
)
3998 memcpy (contents
+ rel
->r_offset
- 3,
3999 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
4004 if ((rel
->r_offset
+ 4) >= input_section
->size
)
4006 if (contents
[rel
->r_offset
+ 4] == 0xff)
4008 if (rel
->r_offset
< 3
4009 || (rel
->r_offset
- 3 + 13) > input_section
->size
)
4011 memcpy (contents
+ rel
->r_offset
- 3,
4012 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
4017 if (rel
->r_offset
< 3
4018 || (rel
->r_offset
- 3 + 12) > input_section
->size
)
4020 memcpy (contents
+ rel
->r_offset
- 3,
4021 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
4024 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
4025 and R_X86_64_PLTOFF64. */
4031 if (htab
->elf
.sgot
== NULL
)
4034 off
= htab
->tls_ld_or_ldm_got
.offset
;
4039 Elf_Internal_Rela outrel
;
4041 if (htab
->elf
.srelgot
== NULL
)
4044 outrel
.r_offset
= (htab
->elf
.sgot
->output_section
->vma
4045 + htab
->elf
.sgot
->output_offset
+ off
);
4047 bfd_put_64 (output_bfd
, 0,
4048 htab
->elf
.sgot
->contents
+ off
);
4049 bfd_put_64 (output_bfd
, 0,
4050 htab
->elf
.sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
4051 outrel
.r_info
= htab
->r_info (0, R_X86_64_DTPMOD64
);
4052 outrel
.r_addend
= 0;
4053 elf_append_rela (output_bfd
, htab
->elf
.srelgot
,
4055 htab
->tls_ld_or_ldm_got
.offset
|= 1;
4057 relocation
= htab
->elf
.sgot
->output_section
->vma
4058 + htab
->elf
.sgot
->output_offset
+ off
;
4059 unresolved_reloc
= false;
4062 case R_X86_64_DTPOFF32
:
4063 if (!bfd_link_executable (info
)
4064 || (input_section
->flags
& SEC_CODE
) == 0)
4065 relocation
-= _bfd_x86_elf_dtpoff_base (info
);
4067 relocation
= elf_x86_64_tpoff (info
, relocation
);
4070 case R_X86_64_TPOFF32
:
4071 case R_X86_64_TPOFF64
:
4072 BFD_ASSERT (bfd_link_executable (info
));
4073 relocation
= elf_x86_64_tpoff (info
, relocation
);
4076 case R_X86_64_DTPOFF64
:
4077 BFD_ASSERT ((input_section
->flags
& SEC_CODE
) == 0);
4078 relocation
-= _bfd_x86_elf_dtpoff_base (info
);
4085 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4086 because such sections are not SEC_ALLOC and thus ld.so will
4087 not process them. */
4088 if (unresolved_reloc
4089 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
4091 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
4092 rel
->r_offset
) != (bfd_vma
) -1)
4097 sec
= h
->root
.u
.def
.section
;
4098 if ((info
->nocopyreloc
|| eh
->def_protected
)
4099 && !(h
->root
.u
.def
.section
->flags
& SEC_CODE
))
4100 return elf_x86_64_need_pic (info
, input_bfd
, input_section
,
4101 h
, NULL
, NULL
, howto
);
4106 /* xgettext:c-format */
4107 (_("%pB(%pA+%#" PRIx64
"): "
4108 "unresolvable %s relocation against symbol `%s'"),
4111 (uint64_t) rel
->r_offset
,
4113 h
->root
.root
.string
);
4119 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
4120 contents
, rel
->r_offset
,
4121 relocation
, rel
->r_addend
);
4123 check_relocation_error
:
4124 if (r
!= bfd_reloc_ok
)
4129 name
= h
->root
.root
.string
;
4132 name
= bfd_elf_string_from_elf_section (input_bfd
,
4133 symtab_hdr
->sh_link
,
4138 name
= bfd_section_name (sec
);
4141 if (r
== bfd_reloc_overflow
)
4143 if (converted_reloc
)
4145 info
->callbacks
->einfo
4146 ("%X%H:", input_bfd
, input_section
, rel
->r_offset
);
4147 info
->callbacks
->einfo
4148 (_(" failed to convert GOTPCREL relocation against "
4149 "'%s'; relink with --no-relax\n"),
4154 (*info
->callbacks
->reloc_overflow
)
4155 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
4156 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
4161 /* xgettext:c-format */
4162 (_("%pB(%pA+%#" PRIx64
"): reloc against `%s': error %d"),
4163 input_bfd
, input_section
,
4164 (uint64_t) rel
->r_offset
, name
, (int) r
);
4175 Elf_Internal_Shdr
*rel_hdr
;
4176 size_t deleted
= rel
- wrel
;
4178 rel_hdr
= _bfd_elf_single_rel_hdr (input_section
->output_section
);
4179 rel_hdr
->sh_size
-= rel_hdr
->sh_entsize
* deleted
;
4180 if (rel_hdr
->sh_size
== 0)
4182 /* It is too late to remove an empty reloc section. Leave
4184 ??? What is wrong with an empty section??? */
4185 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
;
4188 rel_hdr
= _bfd_elf_single_rel_hdr (input_section
);
4189 rel_hdr
->sh_size
-= rel_hdr
->sh_entsize
* deleted
;
4190 input_section
->reloc_count
-= deleted
;
4196 /* Finish up dynamic symbol handling. We set the contents of various
4197 dynamic sections here. */
4200 elf_x86_64_finish_dynamic_symbol (bfd
*output_bfd
,
4201 struct bfd_link_info
*info
,
4202 struct elf_link_hash_entry
*h
,
4203 Elf_Internal_Sym
*sym
)
4205 struct elf_x86_link_hash_table
*htab
;
4206 bool use_plt_second
;
4207 struct elf_x86_link_hash_entry
*eh
;
4208 bool local_undefweak
;
4210 htab
= elf_x86_hash_table (info
, X86_64_ELF_DATA
);
4214 /* Use the second PLT section only if there is .plt section. */
4215 use_plt_second
= htab
->elf
.splt
!= NULL
&& htab
->plt_second
!= NULL
;
4217 eh
= (struct elf_x86_link_hash_entry
*) h
;
4218 if (eh
->no_finish_dynamic_symbol
)
4221 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
4222 resolved undefined weak symbols in executable so that their
4223 references have value 0 at run-time. */
4224 local_undefweak
= UNDEFINED_WEAK_RESOLVED_TO_ZERO (info
, eh
);
4226 if (h
->plt
.offset
!= (bfd_vma
) -1)
4229 bfd_vma got_offset
, plt_offset
;
4230 Elf_Internal_Rela rela
;
4232 asection
*plt
, *gotplt
, *relplt
, *resolved_plt
;
4233 const struct elf_backend_data
*bed
;
4234 bfd_vma plt_got_pcrel_offset
;
4236 /* When building a static executable, use .iplt, .igot.plt and
4237 .rela.iplt sections for STT_GNU_IFUNC symbols. */
4238 if (htab
->elf
.splt
!= NULL
)
4240 plt
= htab
->elf
.splt
;
4241 gotplt
= htab
->elf
.sgotplt
;
4242 relplt
= htab
->elf
.srelplt
;
4246 plt
= htab
->elf
.iplt
;
4247 gotplt
= htab
->elf
.igotplt
;
4248 relplt
= htab
->elf
.irelplt
;
4251 VERIFY_PLT_ENTRY (info
, h
, plt
, gotplt
, relplt
, local_undefweak
)
4253 /* Get the index in the procedure linkage table which
4254 corresponds to this symbol. This is the index of this symbol
4255 in all the symbols for which we are making plt entries. The
4256 first entry in the procedure linkage table is reserved.
4258 Get the offset into the .got table of the entry that
4259 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
4260 bytes. The first three are reserved for the dynamic linker.
4262 For static executables, we don't reserve anything. */
4264 if (plt
== htab
->elf
.splt
)
4266 got_offset
= (h
->plt
.offset
/ htab
->plt
.plt_entry_size
4267 - htab
->plt
.has_plt0
);
4268 got_offset
= (got_offset
+ 3) * GOT_ENTRY_SIZE
;
4272 got_offset
= h
->plt
.offset
/ htab
->plt
.plt_entry_size
;
4273 got_offset
= got_offset
* GOT_ENTRY_SIZE
;
4276 /* Fill in the entry in the procedure linkage table. */
4277 memcpy (plt
->contents
+ h
->plt
.offset
, htab
->plt
.plt_entry
,
4278 htab
->plt
.plt_entry_size
);
4281 memcpy (htab
->plt_second
->contents
+ eh
->plt_second
.offset
,
4282 htab
->non_lazy_plt
->plt_entry
,
4283 htab
->non_lazy_plt
->plt_entry_size
);
4285 resolved_plt
= htab
->plt_second
;
4286 plt_offset
= eh
->plt_second
.offset
;
4291 plt_offset
= h
->plt
.offset
;
4294 /* Insert the relocation positions of the plt section. */
4296 /* Put offset the PC-relative instruction referring to the GOT entry,
4297 subtracting the size of that instruction. */
4298 plt_got_pcrel_offset
= (gotplt
->output_section
->vma
4299 + gotplt
->output_offset
4301 - resolved_plt
->output_section
->vma
4302 - resolved_plt
->output_offset
4304 - htab
->plt
.plt_got_insn_size
);
4306 /* Check PC-relative offset overflow in PLT entry. */
4307 if ((plt_got_pcrel_offset
+ 0x80000000) > 0xffffffff)
4308 /* xgettext:c-format */
4309 info
->callbacks
->einfo (_("%F%pB: PC-relative offset overflow in PLT entry for `%s'\n"),
4310 output_bfd
, h
->root
.root
.string
);
4312 bfd_put_32 (output_bfd
, plt_got_pcrel_offset
,
4313 (resolved_plt
->contents
+ plt_offset
4314 + htab
->plt
.plt_got_offset
));
4316 /* Fill in the entry in the global offset table, initially this
4317 points to the second part of the PLT entry. Leave the entry
4318 as zero for undefined weak symbol in PIE. No PLT relocation
4319 against undefined weak symbol in PIE. */
4320 if (!local_undefweak
)
4322 if (htab
->plt
.has_plt0
)
4323 bfd_put_64 (output_bfd
, (plt
->output_section
->vma
4324 + plt
->output_offset
4326 + htab
->lazy_plt
->plt_lazy_offset
),
4327 gotplt
->contents
+ got_offset
);
4329 /* Fill in the entry in the .rela.plt section. */
4330 rela
.r_offset
= (gotplt
->output_section
->vma
4331 + gotplt
->output_offset
4333 if (PLT_LOCAL_IFUNC_P (info
, h
))
4335 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
4336 h
->root
.root
.string
,
4337 h
->root
.u
.def
.section
->owner
);
4339 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4340 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
4341 rela
.r_info
= htab
->r_info (0, R_X86_64_IRELATIVE
);
4342 rela
.r_addend
= (h
->root
.u
.def
.value
4343 + h
->root
.u
.def
.section
->output_section
->vma
4344 + h
->root
.u
.def
.section
->output_offset
);
4346 if (htab
->params
->report_relative_reloc
)
4347 _bfd_x86_elf_link_report_relative_reloc
4348 (info
, relplt
, h
, sym
, "R_X86_64_IRELATIVE", &rela
);
4350 /* R_X86_64_IRELATIVE comes last. */
4351 plt_index
= htab
->next_irelative_index
--;
4355 rela
.r_info
= htab
->r_info (h
->dynindx
, R_X86_64_JUMP_SLOT
);
4357 plt_index
= htab
->next_jump_slot_index
++;
4360 /* Don't fill the second and third slots in PLT entry for
4361 static executables nor without PLT0. */
4362 if (plt
== htab
->elf
.splt
&& htab
->plt
.has_plt0
)
4365 = h
->plt
.offset
+ htab
->lazy_plt
->plt_plt_insn_end
;
4367 /* Put relocation index. */
4368 bfd_put_32 (output_bfd
, plt_index
,
4369 (plt
->contents
+ h
->plt
.offset
4370 + htab
->lazy_plt
->plt_reloc_offset
));
4372 /* Put offset for jmp .PLT0 and check for overflow. We don't
4373 check relocation index for overflow since branch displacement
4374 will overflow first. */
4375 if (plt0_offset
> 0x80000000)
4376 /* xgettext:c-format */
4377 info
->callbacks
->einfo (_("%F%pB: branch displacement overflow in PLT entry for `%s'\n"),
4378 output_bfd
, h
->root
.root
.string
);
4379 bfd_put_32 (output_bfd
, - plt0_offset
,
4380 (plt
->contents
+ h
->plt
.offset
4381 + htab
->lazy_plt
->plt_plt_offset
));
4384 bed
= get_elf_backend_data (output_bfd
);
4385 loc
= relplt
->contents
+ plt_index
* bed
->s
->sizeof_rela
;
4386 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
4389 else if (eh
->plt_got
.offset
!= (bfd_vma
) -1)
4391 bfd_vma got_offset
, plt_offset
;
4392 asection
*plt
, *got
;
4394 int32_t got_pcrel_offset
;
4396 /* Set the entry in the GOT procedure linkage table. */
4397 plt
= htab
->plt_got
;
4398 got
= htab
->elf
.sgot
;
4399 got_offset
= h
->got
.offset
;
4401 if (got_offset
== (bfd_vma
) -1
4402 || (h
->type
== STT_GNU_IFUNC
&& h
->def_regular
)
4407 /* Use the non-lazy PLT entry template for the GOT PLT since they
4408 are the identical. */
4409 /* Fill in the entry in the GOT procedure linkage table. */
4410 plt_offset
= eh
->plt_got
.offset
;
4411 memcpy (plt
->contents
+ plt_offset
,
4412 htab
->non_lazy_plt
->plt_entry
,
4413 htab
->non_lazy_plt
->plt_entry_size
);
4415 /* Put offset the PC-relative instruction referring to the GOT
4416 entry, subtracting the size of that instruction. */
4417 got_pcrel_offset
= (got
->output_section
->vma
4418 + got
->output_offset
4420 - plt
->output_section
->vma
4421 - plt
->output_offset
4423 - htab
->non_lazy_plt
->plt_got_insn_size
);
4425 /* Check PC-relative offset overflow in GOT PLT entry. */
4426 got_after_plt
= got
->output_section
->vma
> plt
->output_section
->vma
;
4427 if ((got_after_plt
&& got_pcrel_offset
< 0)
4428 || (!got_after_plt
&& got_pcrel_offset
> 0))
4429 /* xgettext:c-format */
4430 info
->callbacks
->einfo (_("%F%pB: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
4431 output_bfd
, h
->root
.root
.string
);
4433 bfd_put_32 (output_bfd
, got_pcrel_offset
,
4434 (plt
->contents
+ plt_offset
4435 + htab
->non_lazy_plt
->plt_got_offset
));
4438 if (!local_undefweak
4440 && (h
->plt
.offset
!= (bfd_vma
) -1
4441 || eh
->plt_got
.offset
!= (bfd_vma
) -1))
4443 /* Mark the symbol as undefined, rather than as defined in
4444 the .plt section. Leave the value if there were any
4445 relocations where pointer equality matters (this is a clue
4446 for the dynamic linker, to make function pointer
4447 comparisons work between an application and shared
4448 library), otherwise set it to zero. If a function is only
4449 called from a binary, there is no need to slow down
4450 shared libraries because of that. */
4451 sym
->st_shndx
= SHN_UNDEF
;
4452 if (!h
->pointer_equality_needed
)
4456 _bfd_x86_elf_link_fixup_ifunc_symbol (info
, htab
, h
, sym
);
4458 /* Don't generate dynamic GOT relocation against undefined weak
4459 symbol in executable. */
4460 if (h
->got
.offset
!= (bfd_vma
) -1
4461 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h
)->tls_type
)
4462 && elf_x86_hash_entry (h
)->tls_type
!= GOT_TLS_IE
4463 && !local_undefweak
)
4465 Elf_Internal_Rela rela
;
4466 asection
*relgot
= htab
->elf
.srelgot
;
4467 const char *relative_reloc_name
= NULL
;
4468 bool generate_dynamic_reloc
= true;
4470 /* This symbol has an entry in the global offset table. Set it
4472 if (htab
->elf
.sgot
== NULL
|| htab
->elf
.srelgot
== NULL
)
4475 rela
.r_offset
= (htab
->elf
.sgot
->output_section
->vma
4476 + htab
->elf
.sgot
->output_offset
4477 + (h
->got
.offset
&~ (bfd_vma
) 1));
4479 /* If this is a static link, or it is a -Bsymbolic link and the
4480 symbol is defined locally or was forced to be local because
4481 of a version file, we just want to emit a RELATIVE reloc.
4482 The entry in the global offset table will already have been
4483 initialized in the relocate_section function. */
4485 && h
->type
== STT_GNU_IFUNC
)
4487 if (h
->plt
.offset
== (bfd_vma
) -1)
4489 /* STT_GNU_IFUNC is referenced without PLT. */
4490 if (htab
->elf
.splt
== NULL
)
4492 /* use .rel[a].iplt section to store .got relocations
4493 in static executable. */
4494 relgot
= htab
->elf
.irelplt
;
4496 if (SYMBOL_REFERENCES_LOCAL_P (info
, h
))
4498 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
4499 h
->root
.root
.string
,
4500 h
->root
.u
.def
.section
->owner
);
4502 rela
.r_info
= htab
->r_info (0,
4503 R_X86_64_IRELATIVE
);
4504 rela
.r_addend
= (h
->root
.u
.def
.value
4505 + h
->root
.u
.def
.section
->output_section
->vma
4506 + h
->root
.u
.def
.section
->output_offset
);
4507 relative_reloc_name
= "R_X86_64_IRELATIVE";
4512 else if (bfd_link_pic (info
))
4514 /* Generate R_X86_64_GLOB_DAT. */
4522 if (!h
->pointer_equality_needed
)
4525 /* For non-shared object, we can't use .got.plt, which
4526 contains the real function addres if we need pointer
4527 equality. We load the GOT entry with the PLT entry. */
4528 if (htab
->plt_second
!= NULL
)
4530 plt
= htab
->plt_second
;
4531 plt_offset
= eh
->plt_second
.offset
;
4535 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
4536 plt_offset
= h
->plt
.offset
;
4538 bfd_put_64 (output_bfd
, (plt
->output_section
->vma
4539 + plt
->output_offset
4541 htab
->elf
.sgot
->contents
+ h
->got
.offset
);
4545 else if (bfd_link_pic (info
)
4546 && SYMBOL_REFERENCES_LOCAL_P (info
, h
))
4548 if (!SYMBOL_DEFINED_NON_SHARED_P (h
))
4550 BFD_ASSERT((h
->got
.offset
& 1) != 0);
4551 if (info
->enable_dt_relr
)
4552 generate_dynamic_reloc
= false;
4555 rela
.r_info
= htab
->r_info (0, R_X86_64_RELATIVE
);
4556 rela
.r_addend
= (h
->root
.u
.def
.value
4557 + h
->root
.u
.def
.section
->output_section
->vma
4558 + h
->root
.u
.def
.section
->output_offset
);
4559 relative_reloc_name
= "R_X86_64_RELATIVE";
4564 BFD_ASSERT((h
->got
.offset
& 1) == 0);
4566 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
4567 htab
->elf
.sgot
->contents
+ h
->got
.offset
);
4568 rela
.r_info
= htab
->r_info (h
->dynindx
, R_X86_64_GLOB_DAT
);
4572 if (generate_dynamic_reloc
)
4574 if (relative_reloc_name
!= NULL
4575 && htab
->params
->report_relative_reloc
)
4576 _bfd_x86_elf_link_report_relative_reloc
4577 (info
, relgot
, h
, sym
, relative_reloc_name
, &rela
);
4579 elf_append_rela (output_bfd
, relgot
, &rela
);
4585 Elf_Internal_Rela rela
;
4588 /* This symbol needs a copy reloc. Set it up. */
4589 VERIFY_COPY_RELOC (h
, htab
)
4591 rela
.r_offset
= (h
->root
.u
.def
.value
4592 + h
->root
.u
.def
.section
->output_section
->vma
4593 + h
->root
.u
.def
.section
->output_offset
);
4594 rela
.r_info
= htab
->r_info (h
->dynindx
, R_X86_64_COPY
);
4596 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
4597 s
= htab
->elf
.sreldynrelro
;
4599 s
= htab
->elf
.srelbss
;
4600 elf_append_rela (output_bfd
, s
, &rela
);
4606 /* Finish up local dynamic symbol handling. We set the contents of
4607 various dynamic sections here. */
4610 elf_x86_64_finish_local_dynamic_symbol (void **slot
, void *inf
)
4612 struct elf_link_hash_entry
*h
4613 = (struct elf_link_hash_entry
*) *slot
;
4614 struct bfd_link_info
*info
4615 = (struct bfd_link_info
*) inf
;
4617 return elf_x86_64_finish_dynamic_symbol (info
->output_bfd
,
4621 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4622 here since undefined weak symbol may not be dynamic and may not be
4623 called for elf_x86_64_finish_dynamic_symbol. */
4626 elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry
*bh
,
4629 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
4630 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
4632 if (h
->root
.type
!= bfd_link_hash_undefweak
4633 || h
->dynindx
!= -1)
4636 return elf_x86_64_finish_dynamic_symbol (info
->output_bfd
,
4640 /* Used to decide how to sort relocs in an optimal manner for the
4641 dynamic linker, before writing them out. */
4643 static enum elf_reloc_type_class
4644 elf_x86_64_reloc_type_class (const struct bfd_link_info
*info
,
4645 const asection
*rel_sec ATTRIBUTE_UNUSED
,
4646 const Elf_Internal_Rela
*rela
)
4648 bfd
*abfd
= info
->output_bfd
;
4649 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4650 struct elf_x86_link_hash_table
*htab
4651 = elf_x86_hash_table (info
, X86_64_ELF_DATA
);
4653 if (htab
->elf
.dynsym
!= NULL
4654 && htab
->elf
.dynsym
->contents
!= NULL
)
4656 /* Check relocation against STT_GNU_IFUNC symbol if there are
4658 unsigned long r_symndx
= htab
->r_sym (rela
->r_info
);
4659 if (r_symndx
!= STN_UNDEF
)
4661 Elf_Internal_Sym sym
;
4662 if (!bed
->s
->swap_symbol_in (abfd
,
4663 (htab
->elf
.dynsym
->contents
4664 + r_symndx
* bed
->s
->sizeof_sym
),
4668 if (ELF_ST_TYPE (sym
.st_info
) == STT_GNU_IFUNC
)
4669 return reloc_class_ifunc
;
4673 switch ((int) ELF32_R_TYPE (rela
->r_info
))
4675 case R_X86_64_IRELATIVE
:
4676 return reloc_class_ifunc
;
4677 case R_X86_64_RELATIVE
:
4678 case R_X86_64_RELATIVE64
:
4679 return reloc_class_relative
;
4680 case R_X86_64_JUMP_SLOT
:
4681 return reloc_class_plt
;
4683 return reloc_class_copy
;
4685 return reloc_class_normal
;
4689 /* Finish up the dynamic sections. */
4692 elf_x86_64_finish_dynamic_sections (bfd
*output_bfd
,
4693 struct bfd_link_info
*info
)
4695 struct elf_x86_link_hash_table
*htab
;
4697 htab
= _bfd_x86_elf_finish_dynamic_sections (output_bfd
, info
);
4701 if (! htab
->elf
.dynamic_sections_created
)
4704 if (htab
->elf
.splt
&& htab
->elf
.splt
->size
> 0)
4706 if (bfd_is_abs_section (htab
->elf
.splt
->output_section
))
4708 info
->callbacks
->einfo
4709 (_("%F%P: discarded output section: `%pA'\n"),
4714 elf_section_data (htab
->elf
.splt
->output_section
)
4715 ->this_hdr
.sh_entsize
= htab
->plt
.plt_entry_size
;
4717 if (htab
->plt
.has_plt0
)
4719 /* Fill in the special first entry in the procedure linkage
4721 memcpy (htab
->elf
.splt
->contents
,
4722 htab
->lazy_plt
->plt0_entry
,
4723 htab
->lazy_plt
->plt0_entry_size
);
4724 /* Add offset for pushq GOT+8(%rip), since the instruction
4725 uses 6 bytes subtract this value. */
4726 bfd_put_32 (output_bfd
,
4727 (htab
->elf
.sgotplt
->output_section
->vma
4728 + htab
->elf
.sgotplt
->output_offset
4730 - htab
->elf
.splt
->output_section
->vma
4731 - htab
->elf
.splt
->output_offset
4733 (htab
->elf
.splt
->contents
4734 + htab
->lazy_plt
->plt0_got1_offset
));
4735 /* Add offset for the PC-relative instruction accessing
4736 GOT+16, subtracting the offset to the end of that
4738 bfd_put_32 (output_bfd
,
4739 (htab
->elf
.sgotplt
->output_section
->vma
4740 + htab
->elf
.sgotplt
->output_offset
4742 - htab
->elf
.splt
->output_section
->vma
4743 - htab
->elf
.splt
->output_offset
4744 - htab
->lazy_plt
->plt0_got2_insn_end
),
4745 (htab
->elf
.splt
->contents
4746 + htab
->lazy_plt
->plt0_got2_offset
));
4749 if (htab
->elf
.tlsdesc_plt
)
4751 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
4752 htab
->elf
.sgot
->contents
+ htab
->elf
.tlsdesc_got
);
4754 memcpy (htab
->elf
.splt
->contents
+ htab
->elf
.tlsdesc_plt
,
4755 htab
->lazy_plt
->plt_tlsdesc_entry
,
4756 htab
->lazy_plt
->plt_tlsdesc_entry_size
);
4758 /* Add offset for pushq GOT+8(%rip), since ENDBR64 uses 4
4759 bytes and the instruction uses 6 bytes, subtract these
4761 bfd_put_32 (output_bfd
,
4762 (htab
->elf
.sgotplt
->output_section
->vma
4763 + htab
->elf
.sgotplt
->output_offset
4765 - htab
->elf
.splt
->output_section
->vma
4766 - htab
->elf
.splt
->output_offset
4767 - htab
->elf
.tlsdesc_plt
4768 - htab
->lazy_plt
->plt_tlsdesc_got1_insn_end
),
4769 (htab
->elf
.splt
->contents
4770 + htab
->elf
.tlsdesc_plt
4771 + htab
->lazy_plt
->plt_tlsdesc_got1_offset
));
4772 /* Add offset for indirect branch via GOT+TDG, where TDG
4773 stands for htab->tlsdesc_got, subtracting the offset
4774 to the end of that instruction. */
4775 bfd_put_32 (output_bfd
,
4776 (htab
->elf
.sgot
->output_section
->vma
4777 + htab
->elf
.sgot
->output_offset
4778 + htab
->elf
.tlsdesc_got
4779 - htab
->elf
.splt
->output_section
->vma
4780 - htab
->elf
.splt
->output_offset
4781 - htab
->elf
.tlsdesc_plt
4782 - htab
->lazy_plt
->plt_tlsdesc_got2_insn_end
),
4783 (htab
->elf
.splt
->contents
4784 + htab
->elf
.tlsdesc_plt
4785 + htab
->lazy_plt
->plt_tlsdesc_got2_offset
));
4789 /* Fill PLT entries for undefined weak symbols in PIE. */
4790 if (bfd_link_pie (info
))
4791 bfd_hash_traverse (&info
->hash
->table
,
4792 elf_x86_64_pie_finish_undefweak_symbol
,
4798 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4799 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4800 It has to be done before elf_link_sort_relocs is called so that
4801 dynamic relocations are properly sorted. */
4804 elf_x86_64_output_arch_local_syms
4805 (bfd
*output_bfd ATTRIBUTE_UNUSED
,
4806 struct bfd_link_info
*info
,
4807 void *flaginfo ATTRIBUTE_UNUSED
,
4808 int (*func
) (void *, const char *,
4811 struct elf_link_hash_entry
*) ATTRIBUTE_UNUSED
)
4813 struct elf_x86_link_hash_table
*htab
4814 = elf_x86_hash_table (info
, X86_64_ELF_DATA
);
4818 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4819 htab_traverse (htab
->loc_hash_table
,
4820 elf_x86_64_finish_local_dynamic_symbol
,
4826 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4827 dynamic relocations. */
4830 elf_x86_64_get_synthetic_symtab (bfd
*abfd
,
4831 long symcount ATTRIBUTE_UNUSED
,
4832 asymbol
**syms ATTRIBUTE_UNUSED
,
4839 bfd_byte
*plt_contents
;
4841 const struct elf_x86_lazy_plt_layout
*lazy_plt
;
4842 const struct elf_x86_non_lazy_plt_layout
*non_lazy_plt
;
4843 const struct elf_x86_lazy_plt_layout
*lazy_bnd_plt
;
4844 const struct elf_x86_non_lazy_plt_layout
*non_lazy_bnd_plt
;
4845 const struct elf_x86_lazy_plt_layout
*lazy_ibt_plt
;
4846 const struct elf_x86_non_lazy_plt_layout
*non_lazy_ibt_plt
;
4848 enum elf_x86_plt_type plt_type
;
4849 struct elf_x86_plt plts
[] =
4851 { ".plt", NULL
, NULL
, plt_unknown
, 0, 0, 0, 0 },
4852 { ".plt.got", NULL
, NULL
, plt_non_lazy
, 0, 0, 0, 0 },
4853 { ".plt.sec", NULL
, NULL
, plt_second
, 0, 0, 0, 0 },
4854 { ".plt.bnd", NULL
, NULL
, plt_second
, 0, 0, 0, 0 },
4855 { NULL
, NULL
, NULL
, plt_non_lazy
, 0, 0, 0, 0 }
4860 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
4863 if (dynsymcount
<= 0)
4866 relsize
= bfd_get_dynamic_reloc_upper_bound (abfd
);
4870 lazy_plt
= &elf_x86_64_lazy_plt
;
4871 non_lazy_plt
= &elf_x86_64_non_lazy_plt
;
4872 lazy_bnd_plt
= &elf_x86_64_lazy_bnd_plt
;
4873 non_lazy_bnd_plt
= &elf_x86_64_non_lazy_bnd_plt
;
4874 if (ABI_64_P (abfd
))
4876 lazy_ibt_plt
= &elf_x86_64_lazy_ibt_plt
;
4877 non_lazy_ibt_plt
= &elf_x86_64_non_lazy_ibt_plt
;
4881 lazy_ibt_plt
= &elf_x32_lazy_ibt_plt
;
4882 non_lazy_ibt_plt
= &elf_x32_non_lazy_ibt_plt
;
4886 for (j
= 0; plts
[j
].name
!= NULL
; j
++)
4888 plt
= bfd_get_section_by_name (abfd
, plts
[j
].name
);
4889 if (plt
== NULL
|| plt
->size
== 0)
4892 /* Get the PLT section contents. */
4893 if (!bfd_malloc_and_get_section (abfd
, plt
, &plt_contents
))
4896 /* Check what kind of PLT it is. */
4897 plt_type
= plt_unknown
;
4898 if (plts
[j
].type
== plt_unknown
4899 && (plt
->size
>= (lazy_plt
->plt_entry_size
4900 + lazy_plt
->plt_entry_size
)))
4902 /* Match lazy PLT first. Need to check the first two
4904 if ((memcmp (plt_contents
, lazy_plt
->plt0_entry
,
4905 lazy_plt
->plt0_got1_offset
) == 0)
4906 && (memcmp (plt_contents
+ 6, lazy_plt
->plt0_entry
+ 6,
4908 plt_type
= plt_lazy
;
4909 else if (lazy_bnd_plt
!= NULL
4910 && (memcmp (plt_contents
, lazy_bnd_plt
->plt0_entry
,
4911 lazy_bnd_plt
->plt0_got1_offset
) == 0)
4912 && (memcmp (plt_contents
+ 6,
4913 lazy_bnd_plt
->plt0_entry
+ 6, 3) == 0))
4915 plt_type
= plt_lazy
| plt_second
;
4916 /* The fist entry in the lazy IBT PLT is the same as the
4918 if ((memcmp (plt_contents
+ lazy_ibt_plt
->plt_entry_size
,
4919 lazy_ibt_plt
->plt_entry
,
4920 lazy_ibt_plt
->plt_got_offset
) == 0))
4921 lazy_plt
= lazy_ibt_plt
;
4923 lazy_plt
= lazy_bnd_plt
;
4927 if (non_lazy_plt
!= NULL
4928 && (plt_type
== plt_unknown
|| plt_type
== plt_non_lazy
)
4929 && plt
->size
>= non_lazy_plt
->plt_entry_size
)
4931 /* Match non-lazy PLT. */
4932 if (memcmp (plt_contents
, non_lazy_plt
->plt_entry
,
4933 non_lazy_plt
->plt_got_offset
) == 0)
4934 plt_type
= plt_non_lazy
;
4937 if (plt_type
== plt_unknown
|| plt_type
== plt_second
)
4939 if (non_lazy_bnd_plt
!= NULL
4940 && plt
->size
>= non_lazy_bnd_plt
->plt_entry_size
4941 && (memcmp (plt_contents
, non_lazy_bnd_plt
->plt_entry
,
4942 non_lazy_bnd_plt
->plt_got_offset
) == 0))
4944 /* Match BND PLT. */
4945 plt_type
= plt_second
;
4946 non_lazy_plt
= non_lazy_bnd_plt
;
4948 else if (non_lazy_ibt_plt
!= NULL
4949 && plt
->size
>= non_lazy_ibt_plt
->plt_entry_size
4950 && (memcmp (plt_contents
,
4951 non_lazy_ibt_plt
->plt_entry
,
4952 non_lazy_ibt_plt
->plt_got_offset
) == 0))
4954 /* Match IBT PLT. */
4955 plt_type
= plt_second
;
4956 non_lazy_plt
= non_lazy_ibt_plt
;
4960 if (plt_type
== plt_unknown
)
4962 free (plt_contents
);
4967 plts
[j
].type
= plt_type
;
4969 if ((plt_type
& plt_lazy
))
4971 plts
[j
].plt_got_offset
= lazy_plt
->plt_got_offset
;
4972 plts
[j
].plt_got_insn_size
= lazy_plt
->plt_got_insn_size
;
4973 plts
[j
].plt_entry_size
= lazy_plt
->plt_entry_size
;
4974 /* Skip PLT0 in lazy PLT. */
4979 plts
[j
].plt_got_offset
= non_lazy_plt
->plt_got_offset
;
4980 plts
[j
].plt_got_insn_size
= non_lazy_plt
->plt_got_insn_size
;
4981 plts
[j
].plt_entry_size
= non_lazy_plt
->plt_entry_size
;
4985 /* Skip lazy PLT when the second PLT is used. */
4986 if (plt_type
== (plt_lazy
| plt_second
))
4990 n
= plt
->size
/ plts
[j
].plt_entry_size
;
4995 plts
[j
].contents
= plt_contents
;
4998 return _bfd_x86_elf_get_synthetic_symtab (abfd
, count
, relsize
,
4999 (bfd_vma
) 0, plts
, dynsyms
,
5003 /* Handle an x86-64 specific section when reading an object file. This
5004 is called when elfcode.h finds a section with an unknown type. */
5007 elf_x86_64_section_from_shdr (bfd
*abfd
, Elf_Internal_Shdr
*hdr
,
5008 const char *name
, int shindex
)
5010 if (hdr
->sh_type
!= SHT_X86_64_UNWIND
)
5013 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
5019 /* Hook called by the linker routine which adds symbols from an object
5020 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
5024 elf_x86_64_add_symbol_hook (bfd
*abfd
,
5025 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5026 Elf_Internal_Sym
*sym
,
5027 const char **namep ATTRIBUTE_UNUSED
,
5028 flagword
*flagsp ATTRIBUTE_UNUSED
,
5034 switch (sym
->st_shndx
)
5036 case SHN_X86_64_LCOMMON
:
5037 lcomm
= bfd_get_section_by_name (abfd
, "LARGE_COMMON");
5040 lcomm
= bfd_make_section_with_flags (abfd
,
5044 | SEC_LINKER_CREATED
));
5047 elf_section_flags (lcomm
) |= SHF_X86_64_LARGE
;
5050 *valp
= sym
->st_size
;
5058 /* Given a BFD section, try to locate the corresponding ELF section
5062 elf_x86_64_elf_section_from_bfd_section (bfd
*abfd ATTRIBUTE_UNUSED
,
5063 asection
*sec
, int *index_return
)
5065 if (sec
== &_bfd_elf_large_com_section
)
5067 *index_return
= SHN_X86_64_LCOMMON
;
5073 /* Process a symbol. */
5076 elf_x86_64_symbol_processing (bfd
*abfd ATTRIBUTE_UNUSED
,
5079 elf_symbol_type
*elfsym
= (elf_symbol_type
*) asym
;
5081 switch (elfsym
->internal_elf_sym
.st_shndx
)
5083 case SHN_X86_64_LCOMMON
:
5084 asym
->section
= &_bfd_elf_large_com_section
;
5085 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
5086 /* Common symbol doesn't set BSF_GLOBAL. */
5087 asym
->flags
&= ~BSF_GLOBAL
;
5093 elf_x86_64_common_definition (Elf_Internal_Sym
*sym
)
5095 return (sym
->st_shndx
== SHN_COMMON
5096 || sym
->st_shndx
== SHN_X86_64_LCOMMON
);
5100 elf_x86_64_common_section_index (asection
*sec
)
5102 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
5105 return SHN_X86_64_LCOMMON
;
5109 elf_x86_64_common_section (asection
*sec
)
5111 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
5112 return bfd_com_section_ptr
;
5114 return &_bfd_elf_large_com_section
;
5118 elf_x86_64_merge_symbol (struct elf_link_hash_entry
*h
,
5119 const Elf_Internal_Sym
*sym
,
5124 const asection
*oldsec
)
5126 /* A normal common symbol and a large common symbol result in a
5127 normal common symbol. We turn the large common symbol into a
5130 && h
->root
.type
== bfd_link_hash_common
5132 && bfd_is_com_section (*psec
)
5135 if (sym
->st_shndx
== SHN_COMMON
5136 && (elf_section_flags (oldsec
) & SHF_X86_64_LARGE
) != 0)
5138 h
->root
.u
.c
.p
->section
5139 = bfd_make_section_old_way (oldbfd
, "COMMON");
5140 h
->root
.u
.c
.p
->section
->flags
= SEC_ALLOC
;
5142 else if (sym
->st_shndx
== SHN_X86_64_LCOMMON
5143 && (elf_section_flags (oldsec
) & SHF_X86_64_LARGE
) == 0)
5144 *psec
= bfd_com_section_ptr
;
5151 elf_x86_64_additional_program_headers (bfd
*abfd
,
5152 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5157 /* Check to see if we need a large readonly segment. */
5158 s
= bfd_get_section_by_name (abfd
, ".lrodata");
5159 if (s
&& (s
->flags
& SEC_LOAD
))
5162 /* Check to see if we need a large data segment. Since .lbss sections
5163 is placed right after the .bss section, there should be no need for
5164 a large data segment just because of .lbss. */
5165 s
= bfd_get_section_by_name (abfd
, ".ldata");
5166 if (s
&& (s
->flags
& SEC_LOAD
))
5172 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5175 elf_x86_64_relocs_compatible (const bfd_target
*input
,
5176 const bfd_target
*output
)
5178 return ((xvec_get_elf_backend_data (input
)->s
->elfclass
5179 == xvec_get_elf_backend_data (output
)->s
->elfclass
)
5180 && _bfd_elf_relocs_compatible (input
, output
));
5183 /* Set up x86-64 GNU properties. Return the first relocatable ELF input
5184 with GNU properties if found. Otherwise, return NULL. */
5187 elf_x86_64_link_setup_gnu_properties (struct bfd_link_info
*info
)
5189 struct elf_x86_init_table init_table
;
5190 const struct elf_backend_data
*bed
;
5191 struct elf_x86_link_hash_table
*htab
;
5193 if ((int) R_X86_64_standard
>= (int) R_X86_64_converted_reloc_bit
5194 || (int) R_X86_64_max
<= (int) R_X86_64_converted_reloc_bit
5195 || ((int) (R_X86_64_GNU_VTINHERIT
| R_X86_64_converted_reloc_bit
)
5196 != (int) R_X86_64_GNU_VTINHERIT
)
5197 || ((int) (R_X86_64_GNU_VTENTRY
| R_X86_64_converted_reloc_bit
)
5198 != (int) R_X86_64_GNU_VTENTRY
))
5201 /* This is unused for x86-64. */
5202 init_table
.plt0_pad_byte
= 0x90;
5204 bed
= get_elf_backend_data (info
->output_bfd
);
5205 htab
= elf_x86_hash_table (info
, bed
->target_id
);
5208 if (htab
->params
->bndplt
)
5210 init_table
.lazy_plt
= &elf_x86_64_lazy_bnd_plt
;
5211 init_table
.non_lazy_plt
= &elf_x86_64_non_lazy_bnd_plt
;
5215 init_table
.lazy_plt
= &elf_x86_64_lazy_plt
;
5216 init_table
.non_lazy_plt
= &elf_x86_64_non_lazy_plt
;
5219 if (ABI_64_P (info
->output_bfd
))
5221 init_table
.lazy_ibt_plt
= &elf_x86_64_lazy_ibt_plt
;
5222 init_table
.non_lazy_ibt_plt
= &elf_x86_64_non_lazy_ibt_plt
;
5226 init_table
.lazy_ibt_plt
= &elf_x32_lazy_ibt_plt
;
5227 init_table
.non_lazy_ibt_plt
= &elf_x32_non_lazy_ibt_plt
;
5230 if (ABI_64_P (info
->output_bfd
))
5232 init_table
.r_info
= elf64_r_info
;
5233 init_table
.r_sym
= elf64_r_sym
;
5237 init_table
.r_info
= elf32_r_info
;
5238 init_table
.r_sym
= elf32_r_sym
;
5241 return _bfd_x86_elf_link_setup_gnu_properties (info
, &init_table
);
5244 static const struct bfd_elf_special_section
5245 elf_x86_64_special_sections
[]=
5247 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
5248 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
5249 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
+ SHF_X86_64_LARGE
},
5250 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
5251 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
5252 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
5253 { NULL
, 0, 0, 0, 0 }
5256 #define TARGET_LITTLE_SYM x86_64_elf64_vec
5257 #define TARGET_LITTLE_NAME "elf64-x86-64"
5258 #define ELF_ARCH bfd_arch_i386
5259 #define ELF_TARGET_ID X86_64_ELF_DATA
5260 #define ELF_MACHINE_CODE EM_X86_64
5261 #if DEFAULT_LD_Z_SEPARATE_CODE
5262 # define ELF_MAXPAGESIZE 0x1000
5264 # define ELF_MAXPAGESIZE 0x200000
5266 #define ELF_COMMONPAGESIZE 0x1000
5268 #define elf_backend_can_gc_sections 1
5269 #define elf_backend_can_refcount 1
5270 #define elf_backend_want_got_plt 1
5271 #define elf_backend_plt_readonly 1
5272 #define elf_backend_want_plt_sym 0
5273 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
5274 #define elf_backend_rela_normal 1
5275 #define elf_backend_plt_alignment 4
5276 #define elf_backend_caches_rawsize 1
5277 #define elf_backend_dtrel_excludes_plt 1
5278 #define elf_backend_want_dynrelro 1
5280 #define elf_info_to_howto elf_x86_64_info_to_howto
5282 #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
5283 #define bfd_elf64_bfd_reloc_name_lookup \
5284 elf_x86_64_reloc_name_lookup
5286 #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
5287 #define elf_backend_always_size_sections elf_x86_64_always_size_sections
5288 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
5289 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5290 #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
5291 #define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
5292 #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5293 #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
5295 #define elf_backend_write_core_note elf_x86_64_write_core_note
5297 #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5298 #define elf_backend_relocate_section elf_x86_64_relocate_section
5299 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5300 #define elf_backend_object_p elf64_x86_64_elf_object_p
5301 #define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
5303 #define elf_backend_section_from_shdr \
5304 elf_x86_64_section_from_shdr
5306 #define elf_backend_section_from_bfd_section \
5307 elf_x86_64_elf_section_from_bfd_section
5308 #define elf_backend_add_symbol_hook \
5309 elf_x86_64_add_symbol_hook
5310 #define elf_backend_symbol_processing \
5311 elf_x86_64_symbol_processing
5312 #define elf_backend_common_section_index \
5313 elf_x86_64_common_section_index
5314 #define elf_backend_common_section \
5315 elf_x86_64_common_section
5316 #define elf_backend_common_definition \
5317 elf_x86_64_common_definition
5318 #define elf_backend_merge_symbol \
5319 elf_x86_64_merge_symbol
5320 #define elf_backend_special_sections \
5321 elf_x86_64_special_sections
5322 #define elf_backend_additional_program_headers \
5323 elf_x86_64_additional_program_headers
5324 #define elf_backend_setup_gnu_properties \
5325 elf_x86_64_link_setup_gnu_properties
5326 #define elf_backend_hide_symbol \
5327 _bfd_x86_elf_hide_symbol
5330 #define elf64_bed elf64_x86_64_bed
5332 #include "elf64-target.h"
5334 /* CloudABI support. */
5336 #undef TARGET_LITTLE_SYM
5337 #define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
5338 #undef TARGET_LITTLE_NAME
5339 #define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
5342 #define ELF_OSABI ELFOSABI_CLOUDABI
5345 #define elf64_bed elf64_x86_64_cloudabi_bed
5347 #include "elf64-target.h"
5349 /* FreeBSD support. */
5351 #undef TARGET_LITTLE_SYM
5352 #define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
5353 #undef TARGET_LITTLE_NAME
5354 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5357 #define ELF_OSABI ELFOSABI_FREEBSD
5360 #define elf64_bed elf64_x86_64_fbsd_bed
5362 #include "elf64-target.h"
5364 /* Solaris 2 support. */
5366 #undef TARGET_LITTLE_SYM
5367 #define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
5368 #undef TARGET_LITTLE_NAME
5369 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5371 #undef ELF_TARGET_OS
5372 #define ELF_TARGET_OS is_solaris
5374 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5375 objects won't be recognized. */
5379 #define elf64_bed elf64_x86_64_sol2_bed
5381 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5383 #undef elf_backend_static_tls_alignment
5384 #define elf_backend_static_tls_alignment 16
5386 /* The Solaris 2 ABI requires a plt symbol on all platforms.
5388 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5390 #undef elf_backend_want_plt_sym
5391 #define elf_backend_want_plt_sym 1
5393 #undef elf_backend_strtab_flags
5394 #define elf_backend_strtab_flags SHF_STRINGS
5397 elf64_x86_64_copy_solaris_special_section_fields (const bfd
*ibfd ATTRIBUTE_UNUSED
,
5398 bfd
*obfd ATTRIBUTE_UNUSED
,
5399 const Elf_Internal_Shdr
*isection ATTRIBUTE_UNUSED
,
5400 Elf_Internal_Shdr
*osection ATTRIBUTE_UNUSED
)
5402 /* PR 19938: FIXME: Need to add code for setting the sh_info
5403 and sh_link fields of Solaris specific section types. */
5407 #undef elf_backend_copy_special_section_fields
5408 #define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
5410 #include "elf64-target.h"
5412 /* Restore defaults. */
5414 #undef elf_backend_static_tls_alignment
5415 #undef elf_backend_want_plt_sym
5416 #define elf_backend_want_plt_sym 0
5417 #undef elf_backend_strtab_flags
5418 #undef elf_backend_copy_special_section_fields
5420 /* 32bit x86-64 support. */
5422 #undef TARGET_LITTLE_SYM
5423 #define TARGET_LITTLE_SYM x86_64_elf32_vec
5424 #undef TARGET_LITTLE_NAME
5425 #define TARGET_LITTLE_NAME "elf32-x86-64"
5427 #define elf32_bed elf32_x86_64_bed
5430 #define ELF_ARCH bfd_arch_i386
5432 #undef ELF_MACHINE_CODE
5433 #define ELF_MACHINE_CODE EM_X86_64
5435 #undef ELF_TARGET_OS
5438 #define bfd_elf32_bfd_reloc_type_lookup \
5439 elf_x86_64_reloc_type_lookup
5440 #define bfd_elf32_bfd_reloc_name_lookup \
5441 elf_x86_64_reloc_name_lookup
5442 #define bfd_elf32_get_synthetic_symtab \
5443 elf_x86_64_get_synthetic_symtab
5445 #undef elf_backend_object_p
5446 #define elf_backend_object_p \
5447 elf32_x86_64_elf_object_p
5449 #undef elf_backend_bfd_from_remote_memory
5450 #define elf_backend_bfd_from_remote_memory \
5451 _bfd_elf32_bfd_from_remote_memory
5453 #undef elf_backend_size_info
5454 #define elf_backend_size_info \
5455 _bfd_elf32_size_info
5457 #include "elf32-target.h"