Automatic date update in version.in
[binutils-gdb.git] / bfd / elf64-x86-64.c
blob3300a2017bd919cd592a111d33ab0901b36c50ea
1 /* X86-64 specific support for ELF
2 Copyright (C) 2000-2024 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"
23 #include "dwarf2.h"
24 #include "libiberty.h"
25 #include "sframe.h"
27 #include "opcode/i386.h"
29 #ifdef CORE_HEADER
30 #include <stdarg.h>
31 #include CORE_HEADER
32 #endif
34 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
35 #define MINUS_ONE (~ (bfd_vma) 0)
37 /* Since both 32-bit and 64-bit x86-64 encode relocation type in the
38 identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
39 relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
40 since they are the same. */
42 /* The relocation "howto" table. Order of fields:
43 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
44 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
45 static reloc_howto_type x86_64_elf_howto_table[] =
47 HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
48 bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0, 0x00000000,
49 false),
50 HOWTO(R_X86_64_64, 0, 8, 64, false, 0, complain_overflow_dont,
51 bfd_elf_generic_reloc, "R_X86_64_64", false, 0, MINUS_ONE,
52 false),
53 HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed,
54 bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0, 0xffffffff,
55 true),
56 HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed,
57 bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0, 0xffffffff,
58 false),
59 HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed,
60 bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0, 0xffffffff,
61 true),
62 HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0, 0xffffffff,
64 false),
65 HOWTO(R_X86_64_GLOB_DAT, 0, 8, 64, false, 0, complain_overflow_dont,
66 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, 0, MINUS_ONE,
67 false),
68 HOWTO(R_X86_64_JUMP_SLOT, 0, 8, 64, false, 0, complain_overflow_dont,
69 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, 0, MINUS_ONE,
70 false),
71 HOWTO(R_X86_64_RELATIVE, 0, 8, 64, false, 0, complain_overflow_dont,
72 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, 0, MINUS_ONE,
73 false),
74 HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true, 0, complain_overflow_signed,
75 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0, 0xffffffff,
76 true),
77 HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned,
78 bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
79 false),
80 HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed,
81 bfd_elf_generic_reloc, "R_X86_64_32S", false, 0, 0xffffffff,
82 false),
83 HOWTO(R_X86_64_16, 0, 2, 16, false, 0, complain_overflow_bitfield,
84 bfd_elf_generic_reloc, "R_X86_64_16", false, 0, 0xffff, false),
85 HOWTO(R_X86_64_PC16, 0, 2, 16, true, 0, complain_overflow_bitfield,
86 bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0, 0xffff, true),
87 HOWTO(R_X86_64_8, 0, 1, 8, false, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_X86_64_8", false, 0, 0xff, false),
89 HOWTO(R_X86_64_PC8, 0, 1, 8, true, 0, complain_overflow_signed,
90 bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0, 0xff, true),
91 HOWTO(R_X86_64_DTPMOD64, 0, 8, 64, false, 0, complain_overflow_dont,
92 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", false, 0, MINUS_ONE,
93 false),
94 HOWTO(R_X86_64_DTPOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
95 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", false, 0, MINUS_ONE,
96 false),
97 HOWTO(R_X86_64_TPOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
98 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", false, 0, MINUS_ONE,
99 false),
100 HOWTO(R_X86_64_TLSGD, 0, 4, 32, true, 0, complain_overflow_signed,
101 bfd_elf_generic_reloc, "R_X86_64_TLSGD", false, 0, 0xffffffff,
102 true),
103 HOWTO(R_X86_64_TLSLD, 0, 4, 32, true, 0, complain_overflow_signed,
104 bfd_elf_generic_reloc, "R_X86_64_TLSLD", false, 0, 0xffffffff,
105 true),
106 HOWTO(R_X86_64_DTPOFF32, 0, 4, 32, false, 0, complain_overflow_signed,
107 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", false, 0, 0xffffffff,
108 false),
109 HOWTO(R_X86_64_GOTTPOFF, 0, 4, 32, true, 0, complain_overflow_signed,
110 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", false, 0, 0xffffffff,
111 true),
112 HOWTO(R_X86_64_TPOFF32, 0, 4, 32, false, 0, complain_overflow_signed,
113 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", false, 0, 0xffffffff,
114 false),
115 HOWTO(R_X86_64_PC64, 0, 8, 64, true, 0, complain_overflow_dont,
116 bfd_elf_generic_reloc, "R_X86_64_PC64", false, 0, MINUS_ONE,
117 true),
118 HOWTO(R_X86_64_GOTOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
119 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64", false, 0, MINUS_ONE,
120 false),
121 HOWTO(R_X86_64_GOTPC32, 0, 4, 32, true, 0, complain_overflow_signed,
122 bfd_elf_generic_reloc, "R_X86_64_GOTPC32", false, 0, 0xffffffff,
123 true),
124 HOWTO(R_X86_64_GOT64, 0, 8, 64, false, 0, complain_overflow_signed,
125 bfd_elf_generic_reloc, "R_X86_64_GOT64", false, 0, MINUS_ONE,
126 false),
127 HOWTO(R_X86_64_GOTPCREL64, 0, 8, 64, true, 0, complain_overflow_signed,
128 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", false, 0, MINUS_ONE,
129 true),
130 HOWTO(R_X86_64_GOTPC64, 0, 8, 64, true, 0, complain_overflow_signed,
131 bfd_elf_generic_reloc, "R_X86_64_GOTPC64", false, 0, MINUS_ONE,
132 true),
133 HOWTO(R_X86_64_GOTPLT64, 0, 8, 64, false, 0, complain_overflow_signed,
134 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", false, 0, MINUS_ONE,
135 false),
136 HOWTO(R_X86_64_PLTOFF64, 0, 8, 64, false, 0, complain_overflow_signed,
137 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", false, 0, MINUS_ONE,
138 false),
139 HOWTO(R_X86_64_SIZE32, 0, 4, 32, false, 0, complain_overflow_unsigned,
140 bfd_elf_generic_reloc, "R_X86_64_SIZE32", false, 0, 0xffffffff,
141 false),
142 HOWTO(R_X86_64_SIZE64, 0, 8, 64, false, 0, complain_overflow_dont,
143 bfd_elf_generic_reloc, "R_X86_64_SIZE64", false, 0, MINUS_ONE,
144 false),
145 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 4, 32, true, 0,
146 complain_overflow_bitfield, bfd_elf_generic_reloc,
147 "R_X86_64_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
148 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, false, 0,
149 complain_overflow_dont, bfd_elf_generic_reloc,
150 "R_X86_64_TLSDESC_CALL",
151 false, 0, 0, false),
152 HOWTO(R_X86_64_TLSDESC, 0, 8, 64, false, 0,
153 complain_overflow_dont, bfd_elf_generic_reloc,
154 "R_X86_64_TLSDESC", false, 0, MINUS_ONE, false),
155 HOWTO(R_X86_64_IRELATIVE, 0, 8, 64, false, 0, complain_overflow_dont,
156 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", false, 0, MINUS_ONE,
157 false),
158 HOWTO(R_X86_64_RELATIVE64, 0, 8, 64, false, 0, complain_overflow_dont,
159 bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", false, 0, MINUS_ONE,
160 false),
161 HOWTO(R_X86_64_PC32_BND, 0, 4, 32, true, 0, complain_overflow_signed,
162 bfd_elf_generic_reloc, "R_X86_64_PC32_BND", false, 0, 0xffffffff,
163 true),
164 HOWTO(R_X86_64_PLT32_BND, 0, 4, 32, true, 0, complain_overflow_signed,
165 bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", false, 0, 0xffffffff,
166 true),
167 HOWTO(R_X86_64_GOTPCRELX, 0, 4, 32, true, 0, complain_overflow_signed,
168 bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", false, 0, 0xffffffff,
169 true),
170 HOWTO(R_X86_64_REX_GOTPCRELX, 0, 4, 32, true, 0, complain_overflow_signed,
171 bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", false, 0, 0xffffffff,
172 true),
173 HOWTO(R_X86_64_CODE_4_GOTPCRELX, 0, 4, 32, true, 0, complain_overflow_signed,
174 bfd_elf_generic_reloc, "R_X86_64_CODE_4_GOTPCRELX", false, 0, 0xffffffff,
175 true),
176 HOWTO(R_X86_64_CODE_4_GOTTPOFF, 0, 4, 32, true, 0, complain_overflow_signed,
177 bfd_elf_generic_reloc, "R_X86_64_CODE_4_GOTTPOFF", false, 0, 0xffffffff,
178 true),
179 HOWTO(R_X86_64_CODE_4_GOTPC32_TLSDESC, 0, 4, 32, true, 0,
180 complain_overflow_bitfield, bfd_elf_generic_reloc,
181 "R_X86_64_CODE_4_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
182 HOWTO(R_X86_64_CODE_5_GOTPCRELX, 0, 4, 32, true, 0,
183 complain_overflow_signed, bfd_elf_generic_reloc,
184 "R_X86_64_CODE_5_GOTPCRELX", false, 0, 0xffffffff, true),
185 HOWTO(R_X86_64_CODE_5_GOTTPOFF, 0, 4, 32, true, 0,
186 complain_overflow_signed, bfd_elf_generic_reloc,
187 "R_X86_64_CODE_5_GOTTPOFF", false, 0, 0xffffffff, true),
188 HOWTO(R_X86_64_CODE_5_GOTPC32_TLSDESC, 0, 4, 32, true, 0,
189 complain_overflow_bitfield, bfd_elf_generic_reloc,
190 "R_X86_64_CODE_5_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
191 HOWTO(R_X86_64_CODE_6_GOTPCRELX, 0, 4, 32, true, 0,
192 complain_overflow_signed, bfd_elf_generic_reloc,
193 "R_X86_64_CODE_6_GOTPCRELX", false, 0, 0xffffffff, true),
194 HOWTO(R_X86_64_CODE_6_GOTTPOFF, 0, 4, 32, true, 0,
195 complain_overflow_signed, bfd_elf_generic_reloc,
196 "R_X86_64_CODE_6_GOTTPOFF", false, 0, 0xffffffff, true),
197 HOWTO(R_X86_64_CODE_6_GOTPC32_TLSDESC, 0, 4, 32, true, 0,
198 complain_overflow_bitfield, bfd_elf_generic_reloc,
199 "R_X86_64_CODE_6_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
201 /* We have a gap in the reloc numbers here.
202 R_X86_64_standard counts the number up to this point, and
203 R_X86_64_vt_offset is the value to subtract from a reloc type of
204 R_X86_64_GNU_VT* to form an index into this table. */
205 #define R_X86_64_standard (R_X86_64_CODE_6_GOTPC32_TLSDESC + 1)
206 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
208 /* GNU extension to record C++ vtable hierarchy. */
209 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 8, 0, false, 0, complain_overflow_dont,
210 NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
212 /* GNU extension to record C++ vtable member usage. */
213 HOWTO (R_X86_64_GNU_VTENTRY, 0, 8, 0, false, 0, complain_overflow_dont,
214 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
215 false),
217 /* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
218 HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_bitfield,
219 bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
220 false)
223 /* Map BFD relocs to the x86_64 elf relocs. */
224 struct elf_reloc_map
226 bfd_reloc_code_real_type bfd_reloc_val;
227 unsigned char elf_reloc_val;
230 static const struct elf_reloc_map x86_64_reloc_map[] =
232 { BFD_RELOC_NONE, R_X86_64_NONE, },
233 { BFD_RELOC_64, R_X86_64_64, },
234 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
235 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
236 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
237 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
238 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
239 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
240 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
241 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
242 { BFD_RELOC_32, R_X86_64_32, },
243 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
244 { BFD_RELOC_16, R_X86_64_16, },
245 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
246 { BFD_RELOC_8, R_X86_64_8, },
247 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
248 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
249 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
250 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
251 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
252 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
253 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
254 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
255 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
256 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
257 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
258 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
259 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
260 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
261 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
262 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
263 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
264 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
265 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
266 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
267 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
268 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
269 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
270 { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND, },
271 { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND, },
272 { BFD_RELOC_X86_64_GOTPCRELX, R_X86_64_GOTPCRELX, },
273 { BFD_RELOC_X86_64_REX_GOTPCRELX, R_X86_64_REX_GOTPCRELX, },
274 { BFD_RELOC_X86_64_CODE_4_GOTPCRELX, R_X86_64_CODE_4_GOTPCRELX, },
275 { BFD_RELOC_X86_64_CODE_4_GOTTPOFF, R_X86_64_CODE_4_GOTTPOFF, },
276 { BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC, R_X86_64_CODE_4_GOTPC32_TLSDESC, },
277 { BFD_RELOC_X86_64_CODE_5_GOTPCRELX, R_X86_64_CODE_5_GOTPCRELX, },
278 { BFD_RELOC_X86_64_CODE_5_GOTTPOFF, R_X86_64_CODE_5_GOTTPOFF, },
279 { BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC, R_X86_64_CODE_5_GOTPC32_TLSDESC, },
280 { BFD_RELOC_X86_64_CODE_6_GOTPCRELX, R_X86_64_CODE_6_GOTPCRELX, },
281 { BFD_RELOC_X86_64_CODE_6_GOTTPOFF, R_X86_64_CODE_6_GOTTPOFF, },
282 { BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC, R_X86_64_CODE_6_GOTPC32_TLSDESC, },
283 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
284 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
287 static reloc_howto_type *
288 elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
290 unsigned i;
292 if (r_type == (unsigned int) R_X86_64_32)
294 if (ABI_64_P (abfd))
295 i = r_type;
296 else
297 i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
299 else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
300 || r_type >= (unsigned int) R_X86_64_max)
302 if (r_type >= (unsigned int) R_X86_64_standard)
304 /* xgettext:c-format */
305 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
306 abfd, r_type);
307 bfd_set_error (bfd_error_bad_value);
308 return NULL;
310 i = r_type;
312 else
313 i = r_type - (unsigned int) R_X86_64_vt_offset;
314 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
315 return &x86_64_elf_howto_table[i];
318 /* Given a BFD reloc type, return a HOWTO structure. */
319 static reloc_howto_type *
320 elf_x86_64_reloc_type_lookup (bfd *abfd,
321 bfd_reloc_code_real_type code)
323 unsigned int i;
325 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
326 i++)
328 if (x86_64_reloc_map[i].bfd_reloc_val == code)
329 return elf_x86_64_rtype_to_howto (abfd,
330 x86_64_reloc_map[i].elf_reloc_val);
332 return NULL;
335 static reloc_howto_type *
336 elf_x86_64_reloc_name_lookup (bfd *abfd,
337 const char *r_name)
339 unsigned int i;
341 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
343 /* Get x32 R_X86_64_32. */
344 reloc_howto_type *reloc
345 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
346 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
347 return reloc;
350 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
351 if (x86_64_elf_howto_table[i].name != NULL
352 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
353 return &x86_64_elf_howto_table[i];
355 return NULL;
358 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
360 static bool
361 elf_x86_64_info_to_howto (bfd *abfd, arelent *cache_ptr,
362 Elf_Internal_Rela *dst)
364 unsigned r_type;
366 r_type = ELF32_R_TYPE (dst->r_info);
367 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
368 if (cache_ptr->howto == NULL)
369 return false;
370 BFD_ASSERT (r_type == cache_ptr->howto->type || cache_ptr->howto->type == R_X86_64_NONE);
371 return true;
374 /* Support for core dump NOTE sections. */
375 static bool
376 elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
378 int offset;
379 size_t size;
381 switch (note->descsz)
383 default:
384 return false;
386 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
387 /* pr_cursig */
388 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
390 /* pr_pid */
391 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
393 /* pr_reg */
394 offset = 72;
395 size = 216;
397 break;
399 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
400 /* pr_cursig */
401 elf_tdata (abfd)->core->signal
402 = bfd_get_16 (abfd, note->descdata + 12);
404 /* pr_pid */
405 elf_tdata (abfd)->core->lwpid
406 = bfd_get_32 (abfd, note->descdata + 32);
408 /* pr_reg */
409 offset = 112;
410 size = 216;
412 break;
415 /* Make a ".reg/999" section. */
416 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
417 size, note->descpos + offset);
420 static bool
421 elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
423 switch (note->descsz)
425 default:
426 return false;
428 case 124:
429 /* sizeof (struct elf_external_linux_prpsinfo32_ugid16). */
430 elf_tdata (abfd)->core->pid
431 = bfd_get_32 (abfd, note->descdata + 12);
432 elf_tdata (abfd)->core->program
433 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
434 elf_tdata (abfd)->core->command
435 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
436 break;
438 case 128:
439 /* sizeof (struct elf_external_linux_prpsinfo32_ugid32). */
440 elf_tdata (abfd)->core->pid
441 = bfd_get_32 (abfd, note->descdata + 12);
442 elf_tdata (abfd)->core->program
443 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
444 elf_tdata (abfd)->core->command
445 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
446 break;
448 case 136:
449 /* sizeof (struct elf_prpsinfo) on Linux/x86_64. */
450 elf_tdata (abfd)->core->pid
451 = bfd_get_32 (abfd, note->descdata + 24);
452 elf_tdata (abfd)->core->program
453 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
454 elf_tdata (abfd)->core->command
455 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
458 /* Note that for some reason, a spurious space is tacked
459 onto the end of the args in some (at least one anyway)
460 implementations, so strip it off if it exists. */
463 char *command = elf_tdata (abfd)->core->command;
464 int n = strlen (command);
466 if (0 < n && command[n - 1] == ' ')
467 command[n - 1] = '\0';
470 return true;
473 #ifdef CORE_HEADER
474 # if GCC_VERSION >= 8000
475 # pragma GCC diagnostic push
476 # pragma GCC diagnostic ignored "-Wstringop-truncation"
477 # endif
478 static char *
479 elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
480 int note_type, ...)
482 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
483 va_list ap;
484 const char *fname, *psargs;
485 long pid;
486 int cursig;
487 const void *gregs;
489 switch (note_type)
491 default:
492 return NULL;
494 case NT_PRPSINFO:
495 va_start (ap, note_type);
496 fname = va_arg (ap, const char *);
497 psargs = va_arg (ap, const char *);
498 va_end (ap);
500 if (bed->s->elfclass == ELFCLASS32)
502 prpsinfo32_t data;
503 memset (&data, 0, sizeof (data));
504 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
505 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
506 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
507 &data, sizeof (data));
509 else
511 prpsinfo64_t data;
512 memset (&data, 0, sizeof (data));
513 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
514 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
515 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
516 &data, sizeof (data));
518 /* NOTREACHED */
520 case NT_PRSTATUS:
521 va_start (ap, note_type);
522 pid = va_arg (ap, long);
523 cursig = va_arg (ap, int);
524 gregs = va_arg (ap, const void *);
525 va_end (ap);
527 if (bed->s->elfclass == ELFCLASS32)
529 if (bed->elf_machine_code == EM_X86_64)
531 prstatusx32_t prstat;
532 memset (&prstat, 0, sizeof (prstat));
533 prstat.pr_pid = pid;
534 prstat.pr_cursig = cursig;
535 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
536 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
537 &prstat, sizeof (prstat));
539 else
541 prstatus32_t prstat;
542 memset (&prstat, 0, sizeof (prstat));
543 prstat.pr_pid = pid;
544 prstat.pr_cursig = cursig;
545 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
546 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
547 &prstat, sizeof (prstat));
550 else
552 prstatus64_t prstat;
553 memset (&prstat, 0, sizeof (prstat));
554 prstat.pr_pid = pid;
555 prstat.pr_cursig = cursig;
556 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
557 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
558 &prstat, sizeof (prstat));
561 /* NOTREACHED */
563 # if GCC_VERSION >= 8000
564 # pragma GCC diagnostic pop
565 # endif
566 #endif
568 /* Functions for the x86-64 ELF linker. */
570 /* The size in bytes of an entry in the global offset table. */
572 #define GOT_ENTRY_SIZE 8
574 /* The size in bytes of an entry in the lazy procedure linkage table. */
576 #define LAZY_PLT_ENTRY_SIZE 16
578 /* The size in bytes of an entry in the non-lazy procedure linkage
579 table. */
581 #define NON_LAZY_PLT_ENTRY_SIZE 8
583 /* The first entry in a lazy procedure linkage table looks like this.
584 See the SVR4 ABI i386 supplement and the x86-64 ABI to see how this
585 works. */
587 static const bfd_byte elf_x86_64_lazy_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
589 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
590 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
591 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
594 /* Subsequent entries in a lazy procedure linkage table look like this. */
596 static const bfd_byte elf_x86_64_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
598 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
599 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
600 0x68, /* pushq immediate */
601 0, 0, 0, 0, /* replaced with index into relocation table. */
602 0xe9, /* jmp relative */
603 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
606 /* The first entry in a lazy procedure linkage table with BND prefix
607 like this. */
609 static const bfd_byte elf_x86_64_lazy_bnd_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
611 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
612 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
613 0x0f, 0x1f, 0 /* nopl (%rax) */
616 /* Subsequent entries for branches with BND prefx in a lazy procedure
617 linkage table look like this. */
619 static const bfd_byte elf_x86_64_lazy_bnd_plt_entry[LAZY_PLT_ENTRY_SIZE] =
621 0x68, 0, 0, 0, 0, /* pushq immediate */
622 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
623 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
626 /* The first entry in the IBT-enabled lazy procedure linkage table is the
627 the same as the lazy PLT with BND prefix so that bound registers are
628 preserved when control is passed to dynamic linker. Subsequent
629 entries for a IBT-enabled lazy procedure linkage table look like
630 this. */
632 static const bfd_byte elf_x86_64_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
634 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
635 0x68, 0, 0, 0, 0, /* pushq immediate */
636 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
637 0x90 /* nop */
640 /* The first entry in the x32 IBT-enabled lazy procedure linkage table
641 is the same as the normal lazy PLT. Subsequent entries for an
642 x32 IBT-enabled lazy procedure linkage table look like this. */
644 static const bfd_byte elf_x32_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
646 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
647 0x68, 0, 0, 0, 0, /* pushq immediate */
648 0xe9, 0, 0, 0, 0, /* jmpq relative */
649 0x66, 0x90 /* xchg %ax,%ax */
652 /* Entries in the non-lazey procedure linkage table look like this. */
654 static const bfd_byte elf_x86_64_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
656 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
657 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
658 0x66, 0x90 /* xchg %ax,%ax */
661 /* Entries for branches with BND prefix in the non-lazey procedure
662 linkage table look like this. */
664 static const bfd_byte elf_x86_64_non_lazy_bnd_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
666 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
667 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
668 0x90 /* nop */
671 /* Entries for branches with IBT-enabled in the non-lazey procedure
672 linkage table look like this. They have the same size as the lazy
673 PLT entry. */
675 static const bfd_byte elf_x86_64_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
677 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
678 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
679 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
680 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopl 0x0(%rax,%rax,1) */
683 /* Entries for branches with IBT-enabled in the x32 non-lazey procedure
684 linkage table look like this. They have the same size as the lazy
685 PLT entry. */
687 static const bfd_byte elf_x32_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
689 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
690 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
691 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
692 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
695 /* The TLSDESC entry in a lazy procedure linkage table. */
696 static const bfd_byte elf_x86_64_tlsdesc_plt_entry[LAZY_PLT_ENTRY_SIZE] =
698 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
699 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
700 0xff, 0x25, 16, 0, 0, 0 /* jmpq *GOT+TDG(%rip) */
703 /* .eh_frame covering the lazy .plt section. */
705 static const bfd_byte elf_x86_64_eh_frame_lazy_plt[] =
707 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
708 0, 0, 0, 0, /* CIE ID */
709 1, /* CIE version */
710 'z', 'R', 0, /* Augmentation string */
711 1, /* Code alignment factor */
712 0x78, /* Data alignment factor */
713 16, /* Return address column */
714 1, /* Augmentation size */
715 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
716 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
717 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
718 DW_CFA_nop, DW_CFA_nop,
720 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
721 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
722 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
723 0, 0, 0, 0, /* .plt size goes here */
724 0, /* Augmentation size */
725 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
726 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
727 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
728 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
729 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
730 11, /* Block length */
731 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
732 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
733 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
734 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
735 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
738 /* .eh_frame covering the lazy BND .plt section. */
740 static const bfd_byte elf_x86_64_eh_frame_lazy_bnd_plt[] =
742 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
743 0, 0, 0, 0, /* CIE ID */
744 1, /* CIE version */
745 'z', 'R', 0, /* Augmentation string */
746 1, /* Code alignment factor */
747 0x78, /* Data alignment factor */
748 16, /* Return address column */
749 1, /* Augmentation size */
750 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
751 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
752 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
753 DW_CFA_nop, DW_CFA_nop,
755 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
756 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
757 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
758 0, 0, 0, 0, /* .plt size goes here */
759 0, /* Augmentation size */
760 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
761 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
762 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
763 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
764 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
765 11, /* Block length */
766 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
767 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
768 DW_OP_lit15, DW_OP_and, DW_OP_lit5, DW_OP_ge,
769 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
770 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
773 /* .eh_frame covering the lazy .plt section with IBT-enabled. */
775 static const bfd_byte elf_x86_64_eh_frame_lazy_ibt_plt[] =
777 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
778 0, 0, 0, 0, /* CIE ID */
779 1, /* CIE version */
780 'z', 'R', 0, /* Augmentation string */
781 1, /* Code alignment factor */
782 0x78, /* Data alignment factor */
783 16, /* Return address column */
784 1, /* Augmentation size */
785 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
786 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
787 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
788 DW_CFA_nop, DW_CFA_nop,
790 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
791 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
792 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
793 0, 0, 0, 0, /* .plt size goes here */
794 0, /* Augmentation size */
795 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
796 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
797 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
798 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
799 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
800 11, /* Block length */
801 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
802 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
803 DW_OP_lit15, DW_OP_and, DW_OP_lit10, DW_OP_ge,
804 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
805 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
808 /* .eh_frame covering the x32 lazy .plt section with IBT-enabled. */
810 static const bfd_byte elf_x32_eh_frame_lazy_ibt_plt[] =
812 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
813 0, 0, 0, 0, /* CIE ID */
814 1, /* CIE version */
815 'z', 'R', 0, /* Augmentation string */
816 1, /* Code alignment factor */
817 0x78, /* Data alignment factor */
818 16, /* Return address column */
819 1, /* Augmentation size */
820 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
821 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
822 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
823 DW_CFA_nop, DW_CFA_nop,
825 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
826 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
827 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
828 0, 0, 0, 0, /* .plt size goes here */
829 0, /* Augmentation size */
830 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
831 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
832 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
833 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
834 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
835 11, /* Block length */
836 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
837 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
838 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
839 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
840 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
843 /* .eh_frame covering the non-lazy .plt section. */
845 static const bfd_byte elf_x86_64_eh_frame_non_lazy_plt[] =
847 #define PLT_GOT_FDE_LENGTH 20
848 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
849 0, 0, 0, 0, /* CIE ID */
850 1, /* CIE version */
851 'z', 'R', 0, /* Augmentation string */
852 1, /* Code alignment factor */
853 0x78, /* Data alignment factor */
854 16, /* Return address column */
855 1, /* Augmentation size */
856 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
857 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
858 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
859 DW_CFA_nop, DW_CFA_nop,
861 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
862 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
863 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
864 0, 0, 0, 0, /* non-lazy .plt size goes here */
865 0, /* Augmentation size */
866 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop,
867 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
870 static const sframe_frame_row_entry elf_x86_64_sframe_null_fre =
873 {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
874 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
877 /* .sframe FRE covering the .plt section entry. */
878 static const sframe_frame_row_entry elf_x86_64_sframe_plt0_fre1 =
880 0, /* SFrame FRE start address. */
881 {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
882 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
885 /* .sframe FRE covering the .plt section entry. */
886 static const sframe_frame_row_entry elf_x86_64_sframe_plt0_fre2 =
888 6, /* SFrame FRE start address. */
889 {24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
890 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
893 /* .sframe FRE covering the .plt section entry. */
894 static const sframe_frame_row_entry elf_x86_64_sframe_pltn_fre1 =
896 0, /* SFrame FRE start address. */
897 {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
898 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
901 /* .sframe FRE covering the .plt section entry. */
902 static const sframe_frame_row_entry elf_x86_64_sframe_pltn_fre2 =
904 11, /* SFrame FRE start address. */
905 {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
906 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
909 /* .sframe FRE covering the second .plt section entry. */
910 static const sframe_frame_row_entry elf_x86_64_sframe_sec_pltn_fre1 =
912 0, /* SFrame FRE start address. */
913 {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
914 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
917 /* SFrame helper object for non-lazy PLT. Also used for IBT enabled PLT. */
918 static const struct elf_x86_sframe_plt elf_x86_64_sframe_non_lazy_plt =
920 LAZY_PLT_ENTRY_SIZE,
921 2, /* Number of FREs for PLT0. */
922 /* Array of SFrame FREs for plt0. */
923 { &elf_x86_64_sframe_plt0_fre1, &elf_x86_64_sframe_plt0_fre2 },
924 LAZY_PLT_ENTRY_SIZE,
925 1, /* Number of FREs for PLTn. */
926 /* Array of SFrame FREs for plt. */
927 { &elf_x86_64_sframe_sec_pltn_fre1, &elf_x86_64_sframe_null_fre },
929 0, /* There is no second PLT necessary. */
930 { &elf_x86_64_sframe_null_fre }
933 /* SFrame helper object for lazy PLT. Also used for IBT enabled PLT. */
934 static const struct elf_x86_sframe_plt elf_x86_64_sframe_plt =
936 LAZY_PLT_ENTRY_SIZE,
937 2, /* Number of FREs for PLT0. */
938 /* Array of SFrame FREs for plt0. */
939 { &elf_x86_64_sframe_plt0_fre1, &elf_x86_64_sframe_plt0_fre2 },
940 LAZY_PLT_ENTRY_SIZE,
941 2, /* Number of FREs for PLTn. */
942 /* Array of SFrame FREs for plt. */
943 { &elf_x86_64_sframe_pltn_fre1, &elf_x86_64_sframe_pltn_fre2 },
944 NON_LAZY_PLT_ENTRY_SIZE,
945 1, /* Number of FREs for PLTn for second PLT. */
946 /* FREs for second plt (stack trace info for .plt.got is
947 identical). Used when IBT or non-lazy PLT is in effect. */
948 { &elf_x86_64_sframe_sec_pltn_fre1 }
951 /* These are the standard parameters. */
952 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_plt =
954 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
955 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
956 elf_x86_64_lazy_plt_entry, /* plt_entry */
957 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
958 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
959 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
960 6, /* plt_tlsdesc_got1_offset */
961 12, /* plt_tlsdesc_got2_offset */
962 10, /* plt_tlsdesc_got1_insn_end */
963 16, /* plt_tlsdesc_got2_insn_end */
964 2, /* plt0_got1_offset */
965 8, /* plt0_got2_offset */
966 12, /* plt0_got2_insn_end */
967 2, /* plt_got_offset */
968 7, /* plt_reloc_offset */
969 12, /* plt_plt_offset */
970 6, /* plt_got_insn_size */
971 LAZY_PLT_ENTRY_SIZE, /* plt_plt_insn_end */
972 6, /* plt_lazy_offset */
973 elf_x86_64_lazy_plt0_entry, /* pic_plt0_entry */
974 elf_x86_64_lazy_plt_entry, /* pic_plt_entry */
975 elf_x86_64_eh_frame_lazy_plt, /* eh_frame_plt */
976 sizeof (elf_x86_64_eh_frame_lazy_plt) /* eh_frame_plt_size */
979 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_plt =
981 elf_x86_64_non_lazy_plt_entry, /* plt_entry */
982 elf_x86_64_non_lazy_plt_entry, /* pic_plt_entry */
983 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
984 2, /* plt_got_offset */
985 6, /* plt_got_insn_size */
986 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
987 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
990 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_bnd_plt =
992 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
993 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
994 elf_x86_64_lazy_bnd_plt_entry, /* plt_entry */
995 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
996 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
997 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
998 6, /* plt_tlsdesc_got1_offset */
999 12, /* plt_tlsdesc_got2_offset */
1000 10, /* plt_tlsdesc_got1_insn_end */
1001 16, /* plt_tlsdesc_got2_insn_end */
1002 2, /* plt0_got1_offset */
1003 1+8, /* plt0_got2_offset */
1004 1+12, /* plt0_got2_insn_end */
1005 1+2, /* plt_got_offset */
1006 1, /* plt_reloc_offset */
1007 7, /* plt_plt_offset */
1008 1+6, /* plt_got_insn_size */
1009 11, /* plt_plt_insn_end */
1010 0, /* plt_lazy_offset */
1011 elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
1012 elf_x86_64_lazy_bnd_plt_entry, /* pic_plt_entry */
1013 elf_x86_64_eh_frame_lazy_bnd_plt, /* eh_frame_plt */
1014 sizeof (elf_x86_64_eh_frame_lazy_bnd_plt) /* eh_frame_plt_size */
1017 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_bnd_plt =
1019 elf_x86_64_non_lazy_bnd_plt_entry, /* plt_entry */
1020 elf_x86_64_non_lazy_bnd_plt_entry, /* pic_plt_entry */
1021 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1022 1+2, /* plt_got_offset */
1023 1+6, /* plt_got_insn_size */
1024 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1025 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1028 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_ibt_plt =
1030 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
1031 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
1032 elf_x86_64_lazy_ibt_plt_entry, /* plt_entry */
1033 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1034 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
1035 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
1036 6, /* plt_tlsdesc_got1_offset */
1037 12, /* plt_tlsdesc_got2_offset */
1038 10, /* plt_tlsdesc_got1_insn_end */
1039 16, /* plt_tlsdesc_got2_insn_end */
1040 2, /* plt0_got1_offset */
1041 1+8, /* plt0_got2_offset */
1042 1+12, /* plt0_got2_insn_end */
1043 4+1+2, /* plt_got_offset */
1044 4+1, /* plt_reloc_offset */
1045 4+1+6, /* plt_plt_offset */
1046 4+1+6, /* plt_got_insn_size */
1047 4+1+5+5, /* plt_plt_insn_end */
1048 0, /* plt_lazy_offset */
1049 elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
1050 elf_x86_64_lazy_ibt_plt_entry, /* pic_plt_entry */
1051 elf_x86_64_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
1052 sizeof (elf_x86_64_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
1055 static const struct elf_x86_lazy_plt_layout elf_x32_lazy_ibt_plt =
1057 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
1058 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
1059 elf_x32_lazy_ibt_plt_entry, /* plt_entry */
1060 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1061 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
1062 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
1063 6, /* plt_tlsdesc_got1_offset */
1064 12, /* plt_tlsdesc_got2_offset */
1065 10, /* plt_tlsdesc_got1_insn_end */
1066 16, /* plt_tlsdesc_got2_insn_end */
1067 2, /* plt0_got1_offset */
1068 8, /* plt0_got2_offset */
1069 12, /* plt0_got2_insn_end */
1070 4+2, /* plt_got_offset */
1071 4+1, /* plt_reloc_offset */
1072 4+6, /* plt_plt_offset */
1073 4+6, /* plt_got_insn_size */
1074 4+5+5, /* plt_plt_insn_end */
1075 0, /* plt_lazy_offset */
1076 elf_x86_64_lazy_plt0_entry, /* pic_plt0_entry */
1077 elf_x32_lazy_ibt_plt_entry, /* pic_plt_entry */
1078 elf_x32_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
1079 sizeof (elf_x32_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
1082 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_ibt_plt =
1084 elf_x86_64_non_lazy_ibt_plt_entry, /* plt_entry */
1085 elf_x86_64_non_lazy_ibt_plt_entry, /* pic_plt_entry */
1086 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1087 4+1+2, /* plt_got_offset */
1088 4+1+6, /* plt_got_insn_size */
1089 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1090 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1093 static const struct elf_x86_non_lazy_plt_layout elf_x32_non_lazy_ibt_plt =
1095 elf_x32_non_lazy_ibt_plt_entry, /* plt_entry */
1096 elf_x32_non_lazy_ibt_plt_entry, /* pic_plt_entry */
1097 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1098 4+2, /* plt_got_offset */
1099 4+6, /* plt_got_insn_size */
1100 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1101 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1104 static bool
1105 elf64_x86_64_elf_object_p (bfd *abfd)
1107 /* Set the right machine number for an x86-64 elf64 file. */
1108 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
1109 return true;
1112 static bool
1113 elf32_x86_64_elf_object_p (bfd *abfd)
1115 /* Set the right machine number for an x86-64 elf32 file. */
1116 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1117 return true;
1120 /* Return TRUE if the TLS access code sequence support transition
1121 from R_TYPE. */
1123 static bool
1124 elf_x86_64_check_tls_transition (bfd *abfd,
1125 struct bfd_link_info *info,
1126 asection *sec,
1127 bfd_byte *contents,
1128 Elf_Internal_Shdr *symtab_hdr,
1129 struct elf_link_hash_entry **sym_hashes,
1130 unsigned int r_type,
1131 const Elf_Internal_Rela *rel,
1132 const Elf_Internal_Rela *relend)
1134 unsigned int val;
1135 unsigned long r_symndx;
1136 bool largepic = false;
1137 struct elf_link_hash_entry *h;
1138 bfd_vma offset;
1139 struct elf_x86_link_hash_table *htab;
1140 bfd_byte *call;
1141 bool indirect_call;
1143 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1144 offset = rel->r_offset;
1145 switch (r_type)
1147 case R_X86_64_TLSGD:
1148 case R_X86_64_TLSLD:
1149 if ((rel + 1) >= relend)
1150 return false;
1152 if (r_type == R_X86_64_TLSGD)
1154 /* Check transition from GD access model. For 64bit, only
1155 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1156 .word 0x6666; rex64; call __tls_get_addr@PLT
1158 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1159 .byte 0x66; rex64
1160 call *__tls_get_addr@GOTPCREL(%rip)
1161 which may be converted to
1162 addr32 call __tls_get_addr
1163 can transit to different access model. For 32bit, only
1164 leaq foo@tlsgd(%rip), %rdi
1165 .word 0x6666; rex64; call __tls_get_addr@PLT
1167 leaq foo@tlsgd(%rip), %rdi
1168 .byte 0x66; rex64
1169 call *__tls_get_addr@GOTPCREL(%rip)
1170 which may be converted to
1171 addr32 call __tls_get_addr
1172 can transit to different access model. For largepic,
1173 we also support:
1174 leaq foo@tlsgd(%rip), %rdi
1175 movabsq $__tls_get_addr@pltoff, %rax
1176 addq $r15, %rax
1177 call *%rax
1179 leaq foo@tlsgd(%rip), %rdi
1180 movabsq $__tls_get_addr@pltoff, %rax
1181 addq $rbx, %rax
1182 call *%rax */
1184 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1186 if ((offset + 12) > sec->size)
1187 return false;
1189 call = contents + offset + 4;
1190 if (call[0] != 0x66
1191 || !((call[1] == 0x48
1192 && call[2] == 0xff
1193 && call[3] == 0x15)
1194 || (call[1] == 0x48
1195 && call[2] == 0x67
1196 && call[3] == 0xe8)
1197 || (call[1] == 0x66
1198 && call[2] == 0x48
1199 && call[3] == 0xe8)))
1201 if (!ABI_64_P (abfd)
1202 || (offset + 19) > sec->size
1203 || offset < 3
1204 || memcmp (call - 7, leaq + 1, 3) != 0
1205 || memcmp (call, "\x48\xb8", 2) != 0
1206 || call[11] != 0x01
1207 || call[13] != 0xff
1208 || call[14] != 0xd0
1209 || !((call[10] == 0x48 && call[12] == 0xd8)
1210 || (call[10] == 0x4c && call[12] == 0xf8)))
1211 return false;
1212 largepic = true;
1214 else if (ABI_64_P (abfd))
1216 if (offset < 4
1217 || memcmp (contents + offset - 4, leaq, 4) != 0)
1218 return false;
1220 else
1222 if (offset < 3
1223 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
1224 return false;
1226 indirect_call = call[2] == 0xff;
1228 else
1230 /* Check transition from LD access model. Only
1231 leaq foo@tlsld(%rip), %rdi;
1232 call __tls_get_addr@PLT
1234 leaq foo@tlsld(%rip), %rdi;
1235 call *__tls_get_addr@GOTPCREL(%rip)
1236 which may be converted to
1237 addr32 call __tls_get_addr
1238 can transit to different access model. For largepic
1239 we also support:
1240 leaq foo@tlsld(%rip), %rdi
1241 movabsq $__tls_get_addr@pltoff, %rax
1242 addq $r15, %rax
1243 call *%rax
1245 leaq foo@tlsld(%rip), %rdi
1246 movabsq $__tls_get_addr@pltoff, %rax
1247 addq $rbx, %rax
1248 call *%rax */
1250 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
1252 if (offset < 3 || (offset + 9) > sec->size)
1253 return false;
1255 if (memcmp (contents + offset - 3, lea, 3) != 0)
1256 return false;
1258 call = contents + offset + 4;
1259 if (!(call[0] == 0xe8
1260 || (call[0] == 0xff && call[1] == 0x15)
1261 || (call[0] == 0x67 && call[1] == 0xe8)))
1263 if (!ABI_64_P (abfd)
1264 || (offset + 19) > sec->size
1265 || memcmp (call, "\x48\xb8", 2) != 0
1266 || call[11] != 0x01
1267 || call[13] != 0xff
1268 || call[14] != 0xd0
1269 || !((call[10] == 0x48 && call[12] == 0xd8)
1270 || (call[10] == 0x4c && call[12] == 0xf8)))
1271 return false;
1272 largepic = true;
1274 indirect_call = call[0] == 0xff;
1277 r_symndx = htab->r_sym (rel[1].r_info);
1278 if (r_symndx < symtab_hdr->sh_info)
1279 return false;
1281 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1282 if (h == NULL
1283 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
1284 return false;
1285 else
1287 r_type = (ELF32_R_TYPE (rel[1].r_info)
1288 & ~R_X86_64_converted_reloc_bit);
1289 if (largepic)
1290 return r_type == R_X86_64_PLTOFF64;
1291 else if (indirect_call)
1292 return (r_type == R_X86_64_GOTPCRELX || r_type == R_X86_64_GOTPCREL);
1293 else
1294 return (r_type == R_X86_64_PC32 || r_type == R_X86_64_PLT32);
1297 case R_X86_64_CODE_4_GOTTPOFF:
1298 /* Check transition from IE access model:
1299 mov foo@gottpoff(%rip), %reg
1300 add foo@gottpoff(%rip), %reg
1301 where reg is one of r16 to r31. */
1303 if (offset < 4
1304 || (offset + 4) > sec->size
1305 || contents[offset - 4] != 0xd5)
1306 return false;
1308 goto check_gottpoff;
1310 case R_X86_64_CODE_6_GOTTPOFF:
1311 /* Check transition from IE access model:
1312 add %reg1, foo@gottpoff(%rip), %reg2
1313 where reg1/reg2 are one of r16 to r31. */
1315 if (offset < 6
1316 || (offset + 4) > sec->size
1317 || contents[offset - 6] != 0x62)
1318 return false;
1320 val = bfd_get_8 (abfd, contents + offset - 2);
1321 if (val != 0x01 && val != 0x03)
1322 return false;
1324 val = bfd_get_8 (abfd, contents + offset - 1);
1325 return (val & 0xc7) == 5;
1327 case R_X86_64_GOTTPOFF:
1328 /* Check transition from IE access model:
1329 mov foo@gottpoff(%rip), %reg
1330 add foo@gottpoff(%rip), %reg
1333 /* Check REX prefix first. */
1334 if (offset >= 3 && (offset + 4) <= sec->size)
1336 val = bfd_get_8 (abfd, contents + offset - 3);
1337 if (val != 0x48 && val != 0x4c)
1339 /* X32 may have 0x44 REX prefix or no REX prefix. */
1340 if (ABI_64_P (abfd))
1341 return false;
1344 else
1346 /* X32 may not have any REX prefix. */
1347 if (ABI_64_P (abfd))
1348 return false;
1349 if (offset < 2 || (offset + 3) > sec->size)
1350 return false;
1353 check_gottpoff:
1354 val = bfd_get_8 (abfd, contents + offset - 2);
1355 if (val != 0x8b && val != 0x03)
1356 return false;
1358 val = bfd_get_8 (abfd, contents + offset - 1);
1359 return (val & 0xc7) == 5;
1361 case R_X86_64_CODE_4_GOTPC32_TLSDESC:
1362 /* Check transition from GDesc access model:
1363 lea x@tlsdesc(%rip), %reg
1364 where reg is one of r16 to r31. */
1366 if (offset < 4
1367 || (offset + 4) > sec->size
1368 || contents[offset - 4] != 0xd5)
1369 return false;
1371 goto check_tlsdesc;
1373 case R_X86_64_GOTPC32_TLSDESC:
1374 /* Check transition from GDesc access model:
1375 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
1376 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
1378 Make sure it's a leaq adding rip to a 32-bit offset
1379 into any register, although it's probably almost always
1380 going to be rax. */
1382 if (offset < 3 || (offset + 4) > sec->size)
1383 return false;
1385 val = bfd_get_8 (abfd, contents + offset - 3);
1386 val &= 0xfb;
1387 if (val != 0x48 && (ABI_64_P (abfd) || val != 0x40))
1388 return false;
1390 check_tlsdesc:
1391 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1392 return false;
1394 val = bfd_get_8 (abfd, contents + offset - 1);
1395 return (val & 0xc7) == 0x05;
1397 case R_X86_64_TLSDESC_CALL:
1398 /* Check transition from GDesc access model:
1399 call *x@tlsdesc(%rax) <--- LP64 mode.
1400 call *x@tlsdesc(%eax) <--- X32 mode.
1402 if (offset + 2 <= sec->size)
1404 unsigned int prefix;
1405 call = contents + offset;
1406 prefix = 0;
1407 if (!ABI_64_P (abfd))
1409 /* Check for call *x@tlsdesc(%eax). */
1410 if (call[0] == 0x67)
1412 prefix = 1;
1413 if (offset + 3 > sec->size)
1414 return false;
1417 /* Make sure that it's a call *x@tlsdesc(%rax). */
1418 return call[prefix] == 0xff && call[1 + prefix] == 0x10;
1421 return false;
1423 default:
1424 abort ();
1428 /* Return TRUE if the TLS access transition is OK or no transition
1429 will be performed. Update R_TYPE if there is a transition. */
1431 static bool
1432 elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1433 asection *sec, bfd_byte *contents,
1434 Elf_Internal_Shdr *symtab_hdr,
1435 struct elf_link_hash_entry **sym_hashes,
1436 unsigned int *r_type, int tls_type,
1437 const Elf_Internal_Rela *rel,
1438 const Elf_Internal_Rela *relend,
1439 struct elf_link_hash_entry *h,
1440 unsigned long r_symndx,
1441 bool from_relocate_section)
1443 unsigned int from_type = *r_type;
1444 unsigned int to_type = from_type;
1445 bool check = true;
1447 /* Skip TLS transition for functions. */
1448 if (h != NULL
1449 && (h->type == STT_FUNC
1450 || h->type == STT_GNU_IFUNC))
1451 return true;
1453 switch (from_type)
1455 case R_X86_64_TLSGD:
1456 case R_X86_64_GOTPC32_TLSDESC:
1457 case R_X86_64_CODE_4_GOTPC32_TLSDESC:
1458 case R_X86_64_TLSDESC_CALL:
1459 case R_X86_64_GOTTPOFF:
1460 case R_X86_64_CODE_4_GOTTPOFF:
1461 case R_X86_64_CODE_6_GOTTPOFF:
1462 if (bfd_link_executable (info))
1464 if (h == NULL)
1465 to_type = R_X86_64_TPOFF32;
1466 else
1467 to_type = R_X86_64_GOTTPOFF;
1470 /* When we are called from elf_x86_64_relocate_section, there may
1471 be additional transitions based on TLS_TYPE. */
1472 if (from_relocate_section)
1474 unsigned int new_to_type = to_type;
1476 if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
1477 new_to_type = R_X86_64_TPOFF32;
1479 if (to_type == R_X86_64_TLSGD
1480 || to_type == R_X86_64_GOTPC32_TLSDESC
1481 || to_type == R_X86_64_CODE_4_GOTPC32_TLSDESC
1482 || to_type == R_X86_64_TLSDESC_CALL)
1484 if (tls_type == GOT_TLS_IE)
1485 new_to_type = R_X86_64_GOTTPOFF;
1488 /* We checked the transition before when we were called from
1489 elf_x86_64_scan_relocs. We only want to check the new
1490 transition which hasn't been checked before. */
1491 check = new_to_type != to_type && from_type == to_type;
1492 to_type = new_to_type;
1495 break;
1497 case R_X86_64_TLSLD:
1498 if (bfd_link_executable (info))
1499 to_type = R_X86_64_TPOFF32;
1500 break;
1502 default:
1503 return true;
1506 /* Return TRUE if there is no transition. */
1507 if (from_type == to_type
1508 || (from_type == R_X86_64_CODE_4_GOTTPOFF
1509 && to_type == R_X86_64_GOTTPOFF)
1510 || (from_type == R_X86_64_CODE_6_GOTTPOFF
1511 && to_type == R_X86_64_GOTTPOFF))
1512 return true;
1514 /* Check if the transition can be performed. */
1515 if (check
1516 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1517 symtab_hdr, sym_hashes,
1518 from_type, rel, relend))
1520 reloc_howto_type *from, *to;
1521 const char *name;
1523 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1524 to = elf_x86_64_rtype_to_howto (abfd, to_type);
1526 if (from == NULL || to == NULL)
1527 return false;
1529 if (h)
1530 name = h->root.root.string;
1531 else
1533 struct elf_x86_link_hash_table *htab;
1535 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1536 if (htab == NULL)
1537 name = "*unknown*";
1538 else
1540 Elf_Internal_Sym *isym;
1542 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1543 abfd, r_symndx);
1544 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1548 _bfd_error_handler
1549 /* xgettext:c-format */
1550 (_("%pB: TLS transition from %s to %s against `%s' at %#" PRIx64
1551 " in section `%pA' failed"),
1552 abfd, from->name, to->name, name, (uint64_t) rel->r_offset, sec);
1553 bfd_set_error (bfd_error_bad_value);
1554 return false;
1557 *r_type = to_type;
1558 return true;
1561 static bool
1562 elf_x86_64_need_pic (struct bfd_link_info *info,
1563 bfd *input_bfd, asection *sec,
1564 struct elf_link_hash_entry *h,
1565 Elf_Internal_Shdr *symtab_hdr,
1566 Elf_Internal_Sym *isym,
1567 reloc_howto_type *howto)
1569 const char *v = "";
1570 const char *und = "";
1571 const char *pic = "";
1572 const char *object;
1574 const char *name;
1575 if (h)
1577 name = h->root.root.string;
1578 switch (ELF_ST_VISIBILITY (h->other))
1580 case STV_HIDDEN:
1581 v = _("hidden symbol ");
1582 break;
1583 case STV_INTERNAL:
1584 v = _("internal symbol ");
1585 break;
1586 case STV_PROTECTED:
1587 v = _("protected symbol ");
1588 break;
1589 default:
1590 if (((struct elf_x86_link_hash_entry *) h)->def_protected)
1591 v = _("protected symbol ");
1592 else
1593 v = _("symbol ");
1594 pic = NULL;
1595 break;
1598 if (!SYMBOL_DEFINED_NON_SHARED_P (h) && !h->def_dynamic)
1599 und = _("undefined ");
1601 else
1603 name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
1604 pic = NULL;
1607 if (bfd_link_dll (info))
1609 object = _("a shared object");
1610 if (!pic)
1611 pic = _("; recompile with -fPIC");
1613 else
1615 if (bfd_link_pie (info))
1616 object = _("a PIE object");
1617 else
1618 object = _("a PDE object");
1619 if (!pic)
1620 pic = _("; recompile with -fPIE");
1623 /* xgettext:c-format */
1624 _bfd_error_handler (_("%pB: relocation %s against %s%s`%s' can "
1625 "not be used when making %s%s"),
1626 input_bfd, howto->name, und, v, name,
1627 object, pic);
1628 bfd_set_error (bfd_error_bad_value);
1629 sec->check_relocs_failed = 1;
1630 return false;
1633 /* With the local symbol, foo, we convert
1634 mov foo@GOTPCREL(%rip), %reg
1636 lea foo(%rip), %reg
1637 and convert
1638 call/jmp *foo@GOTPCREL(%rip)
1640 nop call foo/jmp foo nop
1641 When PIC is false, convert
1642 test %reg, foo@GOTPCREL(%rip)
1644 test $foo, %reg
1645 and convert
1646 binop foo@GOTPCREL(%rip), %reg
1648 binop $foo, %reg
1649 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1650 instructions. */
1652 static bool
1653 elf_x86_64_convert_load_reloc (bfd *abfd,
1654 bfd_byte *contents,
1655 unsigned int *r_type_p,
1656 Elf_Internal_Rela *irel,
1657 struct elf_link_hash_entry *h,
1658 bool *converted,
1659 struct bfd_link_info *link_info)
1661 struct elf_x86_link_hash_table *htab;
1662 bool is_pic;
1663 bool no_overflow;
1664 bool relocx;
1665 bool to_reloc_pc32;
1666 bool abs_symbol;
1667 bool local_ref;
1668 asection *tsec;
1669 bfd_signed_vma raddend;
1670 unsigned int opcode;
1671 unsigned int modrm;
1672 unsigned int r_type = *r_type_p;
1673 unsigned int r_symndx;
1674 bfd_vma roff = irel->r_offset;
1675 bfd_vma abs_relocation;
1677 if (roff < (r_type == R_X86_64_CODE_4_GOTPCRELX
1678 ? 4 : (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2)))
1679 return true;
1681 raddend = irel->r_addend;
1682 /* Addend for 32-bit PC-relative relocation must be -4. */
1683 if (raddend != -4)
1684 return true;
1686 htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
1687 is_pic = bfd_link_pic (link_info);
1689 if (r_type == R_X86_64_CODE_4_GOTPCRELX)
1691 /* Skip if this isn't a REX2 instruction. */
1692 opcode = bfd_get_8 (abfd, contents + roff - 4);
1693 if (opcode != 0xd5)
1694 return true;
1696 relocx = true;
1698 else
1699 relocx = (r_type == R_X86_64_GOTPCRELX
1700 || r_type == R_X86_64_REX_GOTPCRELX);
1702 /* TRUE if --no-relax is used. */
1703 no_overflow = link_info->disable_target_specific_optimizations > 1;
1705 r_symndx = htab->r_sym (irel->r_info);
1707 opcode = bfd_get_8 (abfd, contents + roff - 2);
1709 /* Convert mov to lea since it has been done for a while. */
1710 if (opcode != 0x8b)
1712 /* Only convert R_X86_64_GOTPCRELX, R_X86_64_REX_GOTPCRELX
1713 and R_X86_64_CODE_4_GOTPCRELX for call, jmp or one of adc,
1714 add, and, cmp, or, sbb, sub, test, xor instructions. */
1715 if (!relocx)
1716 return true;
1719 /* We convert only to R_X86_64_PC32:
1720 1. Branch.
1721 2. R_X86_64_GOTPCREL since we can't modify REX byte.
1722 3. no_overflow is true.
1723 4. PIC.
1725 to_reloc_pc32 = (opcode == 0xff
1726 || !relocx
1727 || no_overflow
1728 || is_pic);
1730 abs_symbol = false;
1731 abs_relocation = 0;
1733 /* Get the symbol referred to by the reloc. */
1734 if (h == NULL)
1736 Elf_Internal_Sym *isym
1737 = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
1739 /* Skip relocation against undefined symbols. */
1740 if (isym->st_shndx == SHN_UNDEF)
1741 return true;
1743 local_ref = true;
1744 if (isym->st_shndx == SHN_ABS)
1746 tsec = bfd_abs_section_ptr;
1747 abs_symbol = true;
1748 abs_relocation = isym->st_value;
1750 else if (isym->st_shndx == SHN_COMMON)
1751 tsec = bfd_com_section_ptr;
1752 else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1753 tsec = &_bfd_elf_large_com_section;
1754 else
1755 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1757 else
1759 /* Undefined weak symbol is only bound locally in executable
1760 and its reference is resolved as 0 without relocation
1761 overflow. We can only perform this optimization for
1762 GOTPCRELX relocations since we need to modify REX byte.
1763 It is OK convert mov with R_X86_64_GOTPCREL to
1764 R_X86_64_PC32. */
1765 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
1767 abs_symbol = ABS_SYMBOL_P (h);
1768 abs_relocation = h->root.u.def.value;
1770 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1771 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1772 if ((relocx || opcode == 0x8b)
1773 && (h->root.type == bfd_link_hash_undefweak
1774 && !eh->linker_def
1775 && local_ref))
1777 if (opcode == 0xff)
1779 /* Skip for branch instructions since R_X86_64_PC32
1780 may overflow. */
1781 if (no_overflow)
1782 return true;
1784 else if (relocx)
1786 /* For non-branch instructions, we can convert to
1787 R_X86_64_32/R_X86_64_32S since we know if there
1788 is a REX byte. */
1789 to_reloc_pc32 = false;
1792 /* Since we don't know the current PC when PIC is true,
1793 we can't convert to R_X86_64_PC32. */
1794 if (to_reloc_pc32 && is_pic)
1795 return true;
1797 goto convert;
1799 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1800 ld.so may use its link-time address. */
1801 else if (h->start_stop
1802 || eh->linker_def
1803 || ((h->def_regular
1804 || h->root.type == bfd_link_hash_defined
1805 || h->root.type == bfd_link_hash_defweak)
1806 && h != htab->elf.hdynamic
1807 && local_ref))
1809 /* bfd_link_hash_new or bfd_link_hash_undefined is
1810 set by an assignment in a linker script in
1811 bfd_elf_record_link_assignment. start_stop is set
1812 on __start_SECNAME/__stop_SECNAME which mark section
1813 SECNAME. */
1814 if (h->start_stop
1815 || eh->linker_def
1816 || (h->def_regular
1817 && (h->root.type == bfd_link_hash_new
1818 || h->root.type == bfd_link_hash_undefined
1819 || ((h->root.type == bfd_link_hash_defined
1820 || h->root.type == bfd_link_hash_defweak)
1821 && h->root.u.def.section == bfd_und_section_ptr))))
1823 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
1824 if (no_overflow)
1825 return true;
1826 goto convert;
1828 tsec = h->root.u.def.section;
1830 else
1831 return true;
1834 /* Don't convert GOTPCREL relocation against large section. */
1835 if (elf_section_data (tsec) != NULL
1836 && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
1837 return true;
1839 /* Skip since R_X86_64_PC32/R_X86_64_32/R_X86_64_32S may overflow. */
1840 if (no_overflow)
1841 return true;
1843 convert:
1844 if (opcode == 0xff)
1846 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
1847 unsigned int nop;
1848 unsigned int disp;
1849 bfd_vma nop_offset;
1851 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1852 R_X86_64_PC32. */
1853 modrm = bfd_get_8 (abfd, contents + roff - 1);
1854 if (modrm == 0x25)
1856 /* Convert to "jmp foo nop". */
1857 modrm = 0xe9;
1858 nop = NOP_OPCODE;
1859 nop_offset = irel->r_offset + 3;
1860 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1861 irel->r_offset -= 1;
1862 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1864 else
1866 struct elf_x86_link_hash_entry *eh
1867 = (struct elf_x86_link_hash_entry *) h;
1869 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1870 is a nop prefix. */
1871 modrm = 0xe8;
1872 /* To support TLS optimization, always use addr32 prefix for
1873 "call *__tls_get_addr@GOTPCREL(%rip)". */
1874 if (eh && eh->tls_get_addr)
1876 nop = 0x67;
1877 nop_offset = irel->r_offset - 2;
1879 else
1881 nop = htab->params->call_nop_byte;
1882 if (htab->params->call_nop_as_suffix)
1884 nop_offset = irel->r_offset + 3;
1885 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1886 irel->r_offset -= 1;
1887 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1889 else
1890 nop_offset = irel->r_offset - 2;
1893 bfd_put_8 (abfd, nop, contents + nop_offset);
1894 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1895 r_type = R_X86_64_PC32;
1897 else
1899 unsigned int rex = 0;
1900 unsigned int rex_mask = REX_R;
1901 unsigned int rex2 = 0;
1902 unsigned int rex2_mask = REX_R | REX_R << 4;
1903 bool rex_w = false;
1905 if (r_type == R_X86_64_CODE_4_GOTPCRELX)
1907 rex2 = bfd_get_8 (abfd, contents + roff - 3);
1908 rex_w = (rex2 & REX_W) != 0;
1910 else if (r_type == R_X86_64_REX_GOTPCRELX)
1912 rex = bfd_get_8 (abfd, contents + roff - 3);
1913 rex_w = (rex & REX_W) != 0;
1916 if (opcode == 0x8b)
1918 if (abs_symbol && local_ref && relocx)
1919 to_reloc_pc32 = false;
1921 if (to_reloc_pc32)
1923 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1924 "lea foo(%rip), %reg". */
1925 opcode = 0x8d;
1926 r_type = R_X86_64_PC32;
1928 else
1930 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1931 "mov $foo, %reg". */
1932 opcode = 0xc7;
1933 modrm = bfd_get_8 (abfd, contents + roff - 1);
1934 modrm = 0xc0 | (modrm & 0x38) >> 3;
1935 if (rex_w && ABI_64_P (link_info->output_bfd))
1937 /* Keep the REX_W bit in REX byte for LP64. */
1938 r_type = R_X86_64_32S;
1939 goto rewrite_modrm_rex;
1941 else
1943 /* If the REX_W bit in REX byte isn't needed,
1944 use R_X86_64_32 and clear the W bit to avoid
1945 sign-extend imm32 to imm64. */
1946 r_type = R_X86_64_32;
1947 /* Clear the W bit in REX byte and REX2 payload. */
1948 rex_mask |= REX_W;
1949 rex2_mask |= REX_W;
1950 goto rewrite_modrm_rex;
1954 else
1956 /* R_X86_64_PC32 isn't supported. */
1957 if (to_reloc_pc32)
1958 return true;
1960 modrm = bfd_get_8 (abfd, contents + roff - 1);
1961 if (opcode == 0x85)
1963 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
1964 "test $foo, %reg". */
1965 modrm = 0xc0 | (modrm & 0x38) >> 3;
1966 opcode = 0xf7;
1968 else
1970 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
1971 "binop $foo, %reg". */
1972 modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
1973 opcode = 0x81;
1976 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
1977 overflow when sign-extending imm32 to imm64. */
1978 r_type = rex_w ? R_X86_64_32S : R_X86_64_32;
1980 rewrite_modrm_rex:
1981 if (abs_relocation)
1983 /* Check if R_X86_64_32S/R_X86_64_32 fits. */
1984 if (r_type == R_X86_64_32S)
1986 if ((abs_relocation + 0x80000000) > 0xffffffff)
1987 return true;
1989 else
1991 if (abs_relocation > 0xffffffff)
1992 return true;
1996 bfd_put_8 (abfd, modrm, contents + roff - 1);
1998 if (rex)
2000 /* Move the R bit to the B bit in REX byte. */
2001 rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
2002 bfd_put_8 (abfd, rex, contents + roff - 3);
2004 else if (rex2)
2006 /* Move the R bits to the B bits in REX2 payload byte. */
2007 rex2 = ((rex2 & ~rex2_mask)
2008 | (rex2 & (REX_R | REX_R << 4)) >> 2);
2009 bfd_put_8 (abfd, rex2, contents + roff - 3);
2012 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
2013 irel->r_addend = 0;
2016 bfd_put_8 (abfd, opcode, contents + roff - 2);
2019 *r_type_p = r_type;
2020 irel->r_info = htab->r_info (r_symndx,
2021 r_type | R_X86_64_converted_reloc_bit);
2023 *converted = true;
2025 return true;
2028 /* Look through the relocs for a section during the first phase, and
2029 calculate needed space in the global offset table, and procedure
2030 linkage table. */
2032 static bool
2033 elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
2034 asection *sec,
2035 const Elf_Internal_Rela *relocs)
2037 struct elf_x86_link_hash_table *htab;
2038 Elf_Internal_Shdr *symtab_hdr;
2039 struct elf_link_hash_entry **sym_hashes;
2040 const Elf_Internal_Rela *rel;
2041 const Elf_Internal_Rela *rel_end;
2042 bfd_byte *contents;
2043 bool converted;
2045 if (bfd_link_relocatable (info))
2046 return true;
2048 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2049 if (htab == NULL)
2051 sec->check_relocs_failed = 1;
2052 return false;
2055 BFD_ASSERT (is_x86_elf (abfd, htab));
2057 /* Get the section contents. */
2058 if (elf_section_data (sec)->this_hdr.contents != NULL)
2059 contents = elf_section_data (sec)->this_hdr.contents;
2060 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2062 sec->check_relocs_failed = 1;
2063 return false;
2066 symtab_hdr = &elf_symtab_hdr (abfd);
2067 sym_hashes = elf_sym_hashes (abfd);
2069 converted = false;
2071 rel_end = relocs + sec->reloc_count;
2072 for (rel = relocs; rel < rel_end; rel++)
2074 unsigned int r_type;
2075 unsigned int r_symndx;
2076 struct elf_link_hash_entry *h;
2077 struct elf_x86_link_hash_entry *eh;
2078 Elf_Internal_Sym *isym;
2079 const char *name;
2080 bool size_reloc;
2081 bool converted_reloc;
2082 bool no_dynreloc;
2084 r_symndx = htab->r_sym (rel->r_info);
2085 r_type = ELF32_R_TYPE (rel->r_info);
2087 /* Don't check R_X86_64_NONE. */
2088 if (r_type == R_X86_64_NONE)
2089 continue;
2091 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
2093 /* xgettext:c-format */
2094 _bfd_error_handler (_("%pB: bad symbol index: %d"),
2095 abfd, r_symndx);
2096 goto error_return;
2099 if (r_symndx < symtab_hdr->sh_info)
2101 /* A local symbol. */
2102 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
2103 abfd, r_symndx);
2104 if (isym == NULL)
2105 goto error_return;
2107 /* Check relocation against local STT_GNU_IFUNC symbol. */
2108 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
2110 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel,
2111 true);
2112 if (h == NULL)
2113 goto error_return;
2115 /* Fake a STT_GNU_IFUNC symbol. */
2116 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
2117 isym, NULL);
2118 h->type = STT_GNU_IFUNC;
2119 h->def_regular = 1;
2120 h->ref_regular = 1;
2121 h->forced_local = 1;
2122 h->root.type = bfd_link_hash_defined;
2124 else
2125 h = NULL;
2127 else
2129 isym = NULL;
2130 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2131 while (h->root.type == bfd_link_hash_indirect
2132 || h->root.type == bfd_link_hash_warning)
2133 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2136 /* Check invalid x32 relocations. */
2137 if (!ABI_64_P (abfd))
2138 switch (r_type)
2140 default:
2141 break;
2143 case R_X86_64_DTPOFF64:
2144 case R_X86_64_TPOFF64:
2145 case R_X86_64_PC64:
2146 case R_X86_64_GOTOFF64:
2147 case R_X86_64_GOT64:
2148 case R_X86_64_GOTPCREL64:
2149 case R_X86_64_GOTPC64:
2150 case R_X86_64_GOTPLT64:
2151 case R_X86_64_PLTOFF64:
2153 if (h)
2154 name = h->root.root.string;
2155 else
2156 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2157 NULL);
2158 _bfd_error_handler
2159 /* xgettext:c-format */
2160 (_("%pB: relocation %s against symbol `%s' isn't "
2161 "supported in x32 mode"), abfd,
2162 x86_64_elf_howto_table[r_type].name, name);
2163 bfd_set_error (bfd_error_bad_value);
2164 goto error_return;
2166 break;
2169 eh = (struct elf_x86_link_hash_entry *) h;
2171 if (h != NULL)
2173 /* It is referenced by a non-shared object. */
2174 h->ref_regular = 1;
2177 converted_reloc = false;
2178 if ((r_type == R_X86_64_GOTPCREL
2179 || r_type == R_X86_64_GOTPCRELX
2180 || r_type == R_X86_64_REX_GOTPCRELX
2181 || r_type == R_X86_64_CODE_4_GOTPCRELX)
2182 && (h == NULL || h->type != STT_GNU_IFUNC))
2184 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
2185 if (!elf_x86_64_convert_load_reloc (abfd, contents, &r_type,
2186 irel, h, &converted_reloc,
2187 info))
2188 goto error_return;
2190 if (converted_reloc)
2191 converted = true;
2194 if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
2195 symtab_hdr, &no_dynreloc))
2196 return false;
2198 if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
2199 symtab_hdr, sym_hashes,
2200 &r_type, GOT_UNKNOWN,
2201 rel, rel_end, h, r_symndx, false))
2202 goto error_return;
2204 /* Check if _GLOBAL_OFFSET_TABLE_ is referenced. */
2205 if (h == htab->elf.hgot)
2206 htab->got_referenced = true;
2208 switch (r_type)
2210 case R_X86_64_TLSLD:
2211 htab->tls_ld_or_ldm_got.refcount = 1;
2212 goto create_got;
2214 case R_X86_64_TPOFF32:
2215 if (!bfd_link_executable (info) && ABI_64_P (abfd))
2216 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2217 &x86_64_elf_howto_table[r_type]);
2218 if (eh != NULL)
2219 eh->zero_undefweak &= 0x2;
2220 break;
2222 case R_X86_64_GOTTPOFF:
2223 case R_X86_64_CODE_4_GOTTPOFF:
2224 case R_X86_64_CODE_6_GOTTPOFF:
2225 if (!bfd_link_executable (info))
2226 info->flags |= DF_STATIC_TLS;
2227 /* Fall through */
2229 case R_X86_64_GOT32:
2230 case R_X86_64_GOTPCREL:
2231 case R_X86_64_GOTPCRELX:
2232 case R_X86_64_REX_GOTPCRELX:
2233 case R_X86_64_CODE_4_GOTPCRELX:
2234 case R_X86_64_TLSGD:
2235 case R_X86_64_GOT64:
2236 case R_X86_64_GOTPCREL64:
2237 case R_X86_64_GOTPLT64:
2238 case R_X86_64_GOTPC32_TLSDESC:
2239 case R_X86_64_CODE_4_GOTPC32_TLSDESC:
2240 case R_X86_64_TLSDESC_CALL:
2241 /* This symbol requires a global offset table entry. */
2243 int tls_type, old_tls_type;
2245 switch (r_type)
2247 default:
2248 tls_type = GOT_NORMAL;
2249 if (h)
2251 if (ABS_SYMBOL_P (h))
2252 tls_type = GOT_ABS;
2254 else if (isym->st_shndx == SHN_ABS)
2255 tls_type = GOT_ABS;
2256 break;
2257 case R_X86_64_TLSGD:
2258 tls_type = GOT_TLS_GD;
2259 break;
2260 case R_X86_64_GOTTPOFF:
2261 case R_X86_64_CODE_4_GOTTPOFF:
2262 case R_X86_64_CODE_6_GOTTPOFF:
2263 tls_type = GOT_TLS_IE;
2264 break;
2265 case R_X86_64_GOTPC32_TLSDESC:
2266 case R_X86_64_CODE_4_GOTPC32_TLSDESC:
2267 case R_X86_64_TLSDESC_CALL:
2268 tls_type = GOT_TLS_GDESC;
2269 break;
2272 if (h != NULL)
2274 h->got.refcount = 1;
2275 old_tls_type = eh->tls_type;
2277 else
2279 bfd_signed_vma *local_got_refcounts;
2281 if (!elf_x86_allocate_local_got_info (abfd,
2282 symtab_hdr->sh_info))
2283 goto error_return;
2285 /* This is a global offset table entry for a local symbol. */
2286 local_got_refcounts = elf_local_got_refcounts (abfd);
2287 local_got_refcounts[r_symndx] = 1;
2288 old_tls_type
2289 = elf_x86_local_got_tls_type (abfd) [r_symndx];
2292 /* If a TLS symbol is accessed using IE at least once,
2293 there is no point to use dynamic model for it. */
2294 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2295 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2296 || tls_type != GOT_TLS_IE))
2298 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
2299 tls_type = old_tls_type;
2300 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2301 && GOT_TLS_GD_ANY_P (tls_type))
2302 tls_type |= old_tls_type;
2303 else
2305 if (h)
2306 name = h->root.root.string;
2307 else
2308 name = bfd_elf_sym_name (abfd, symtab_hdr,
2309 isym, NULL);
2310 _bfd_error_handler
2311 /* xgettext:c-format */
2312 (_("%pB: '%s' accessed both as normal and"
2313 " thread local symbol"),
2314 abfd, name);
2315 bfd_set_error (bfd_error_bad_value);
2316 goto error_return;
2320 if (old_tls_type != tls_type)
2322 if (eh != NULL)
2323 eh->tls_type = tls_type;
2324 else
2325 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
2328 /* Fall through */
2330 case R_X86_64_GOTOFF64:
2331 case R_X86_64_GOTPC32:
2332 case R_X86_64_GOTPC64:
2333 create_got:
2334 if (eh != NULL)
2335 eh->zero_undefweak &= 0x2;
2336 break;
2338 case R_X86_64_PLT32:
2339 /* This symbol requires a procedure linkage table entry. We
2340 actually build the entry in adjust_dynamic_symbol,
2341 because this might be a case of linking PIC code which is
2342 never referenced by a dynamic object, in which case we
2343 don't need to generate a procedure linkage table entry
2344 after all. */
2346 /* If this is a local symbol, we resolve it directly without
2347 creating a procedure linkage table entry. */
2348 if (h == NULL)
2349 continue;
2351 eh->zero_undefweak &= 0x2;
2352 h->needs_plt = 1;
2353 h->plt.refcount = 1;
2354 break;
2356 case R_X86_64_PLTOFF64:
2357 /* This tries to form the 'address' of a function relative
2358 to GOT. For global symbols we need a PLT entry. */
2359 if (h != NULL)
2361 h->needs_plt = 1;
2362 h->plt.refcount = 1;
2364 goto create_got;
2366 case R_X86_64_SIZE32:
2367 case R_X86_64_SIZE64:
2368 size_reloc = true;
2369 goto do_size;
2371 case R_X86_64_32:
2372 if (!ABI_64_P (abfd))
2373 goto pointer;
2374 /* Fall through. */
2375 case R_X86_64_8:
2376 case R_X86_64_16:
2377 case R_X86_64_32S:
2378 /* Check relocation overflow as these relocs may lead to
2379 run-time relocation overflow. Don't error out for
2380 sections we don't care about, such as debug sections or
2381 when relocation overflow check is disabled. */
2382 if (!htab->params->no_reloc_overflow_check
2383 && !converted_reloc
2384 && (bfd_link_pic (info)
2385 || (bfd_link_executable (info)
2386 && h != NULL
2387 && !h->def_regular
2388 && h->def_dynamic
2389 && (sec->flags & SEC_READONLY) == 0)))
2390 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2391 &x86_64_elf_howto_table[r_type]);
2392 /* Fall through. */
2394 case R_X86_64_PC8:
2395 case R_X86_64_PC16:
2396 case R_X86_64_PC32:
2397 case R_X86_64_PC64:
2398 case R_X86_64_64:
2399 pointer:
2400 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2401 eh->zero_undefweak |= 0x2;
2402 /* We are called after all symbols have been resolved. Only
2403 relocation against STT_GNU_IFUNC symbol must go through
2404 PLT. */
2405 if (h != NULL
2406 && (bfd_link_executable (info)
2407 || h->type == STT_GNU_IFUNC))
2409 bool func_pointer_ref = false;
2411 if (r_type == R_X86_64_PC32)
2413 /* Since something like ".long foo - ." may be used
2414 as pointer, make sure that PLT is used if foo is
2415 a function defined in a shared library. */
2416 if ((sec->flags & SEC_CODE) == 0)
2418 h->pointer_equality_needed = 1;
2419 if (bfd_link_pie (info)
2420 && h->type == STT_FUNC
2421 && !h->def_regular
2422 && h->def_dynamic)
2424 h->needs_plt = 1;
2425 h->plt.refcount = 1;
2429 else if (r_type != R_X86_64_PC64)
2431 /* At run-time, R_X86_64_64 can be resolved for both
2432 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2433 can only be resolved for x32. Function pointer
2434 reference doesn't need PLT for pointer equality. */
2435 if ((sec->flags & SEC_READONLY) == 0
2436 && (r_type == R_X86_64_64
2437 || (!ABI_64_P (abfd)
2438 && (r_type == R_X86_64_32
2439 || r_type == R_X86_64_32S))))
2440 func_pointer_ref = true;
2442 /* IFUNC symbol needs pointer equality in PDE so that
2443 function pointer reference will be resolved to its
2444 PLT entry directly. */
2445 if (!func_pointer_ref
2446 || (bfd_link_pde (info)
2447 && h->type == STT_GNU_IFUNC))
2448 h->pointer_equality_needed = 1;
2451 if (!func_pointer_ref)
2453 /* If this reloc is in a read-only section, we might
2454 need a copy reloc. We can't check reliably at this
2455 stage whether the section is read-only, as input
2456 sections have not yet been mapped to output sections.
2457 Tentatively set the flag for now, and correct in
2458 adjust_dynamic_symbol. */
2459 h->non_got_ref = 1;
2461 if (!elf_has_indirect_extern_access (sec->owner))
2462 eh->non_got_ref_without_indirect_extern_access = 1;
2464 /* We may need a .plt entry if the symbol is a function
2465 defined in a shared lib or is a function referenced
2466 from the code or read-only section. */
2467 if (!h->def_regular
2468 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2469 h->plt.refcount = 1;
2471 if (htab->elf.target_os != is_solaris
2472 && h->pointer_equality_needed
2473 && h->type == STT_FUNC
2474 && eh->def_protected
2475 && !SYMBOL_DEFINED_NON_SHARED_P (h)
2476 && h->def_dynamic)
2478 /* Disallow non-canonical reference to canonical
2479 protected function. */
2480 _bfd_error_handler
2481 /* xgettext:c-format */
2482 (_("%pB: non-canonical reference to canonical "
2483 "protected function `%s' in %pB"),
2484 abfd, h->root.root.string,
2485 h->root.u.def.section->owner);
2486 bfd_set_error (bfd_error_bad_value);
2487 goto error_return;
2492 size_reloc = false;
2493 do_size:
2494 if (!no_dynreloc
2495 && NEED_DYNAMIC_RELOCATION_P (true, info, true, h, sec,
2496 r_type,
2497 htab->pointer_r_type))
2499 struct elf_dyn_relocs *p;
2500 struct elf_dyn_relocs **head;
2502 /* If this is a global symbol, we count the number of
2503 relocations we need for this symbol. */
2504 if (h != NULL)
2505 head = &h->dyn_relocs;
2506 else
2508 /* Track dynamic relocs needed for local syms too.
2509 We really need local syms available to do this
2510 easily. Oh well. */
2511 asection *s;
2512 void **vpp;
2514 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
2515 abfd, r_symndx);
2516 if (isym == NULL)
2517 goto error_return;
2519 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2520 if (s == NULL)
2521 s = sec;
2523 /* Beware of type punned pointers vs strict aliasing
2524 rules. */
2525 vpp = &(elf_section_data (s)->local_dynrel);
2526 head = (struct elf_dyn_relocs **)vpp;
2529 p = *head;
2530 if (p == NULL || p->sec != sec)
2532 size_t amt = sizeof *p;
2534 p = ((struct elf_dyn_relocs *)
2535 bfd_alloc (htab->elf.dynobj, amt));
2536 if (p == NULL)
2537 goto error_return;
2538 p->next = *head;
2539 *head = p;
2540 p->sec = sec;
2541 p->count = 0;
2542 p->pc_count = 0;
2545 p->count += 1;
2546 /* Count size relocation as PC-relative relocation. */
2547 if (X86_PCREL_TYPE_P (true, r_type) || size_reloc)
2548 p->pc_count += 1;
2550 break;
2552 case R_X86_64_CODE_5_GOTPCRELX:
2553 case R_X86_64_CODE_5_GOTTPOFF:
2554 case R_X86_64_CODE_5_GOTPC32_TLSDESC:
2555 case R_X86_64_CODE_6_GOTPCRELX:
2556 case R_X86_64_CODE_6_GOTPC32_TLSDESC:
2558 /* These relocations are added only for completeness and
2559 aren't be used. */
2560 if (h)
2561 name = h->root.root.string;
2562 else
2563 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2564 NULL);
2565 _bfd_error_handler
2566 /* xgettext:c-format */
2567 (_("%pB: unsupported relocation %s against symbol `%s'"),
2568 abfd, x86_64_elf_howto_table[r_type].name, name);
2570 break;
2572 /* This relocation describes the C++ object vtable hierarchy.
2573 Reconstruct it for later use during GC. */
2574 case R_X86_64_GNU_VTINHERIT:
2575 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2576 goto error_return;
2577 break;
2579 /* This relocation describes which C++ vtable entries are actually
2580 used. Record for later use during GC. */
2581 case R_X86_64_GNU_VTENTRY:
2582 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2583 goto error_return;
2584 break;
2586 default:
2587 break;
2591 if (elf_section_data (sec)->this_hdr.contents != contents)
2593 if (!converted && !_bfd_link_keep_memory (info))
2594 free (contents);
2595 else
2597 /* Cache the section contents for elf_link_input_bfd if any
2598 load is converted or --no-keep-memory isn't used. */
2599 elf_section_data (sec)->this_hdr.contents = contents;
2600 info->cache_size += sec->size;
2604 /* Cache relocations if any load is converted. */
2605 if (elf_section_data (sec)->relocs != relocs && converted)
2606 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
2608 return true;
2610 error_return:
2611 if (elf_section_data (sec)->this_hdr.contents != contents)
2612 free (contents);
2613 sec->check_relocs_failed = 1;
2614 return false;
2617 static bool
2618 elf_x86_64_always_size_sections (bfd *output_bfd,
2619 struct bfd_link_info *info)
2621 bfd *abfd;
2623 /* Scan relocations after rel_from_abs has been set on __ehdr_start. */
2624 for (abfd = info->input_bfds;
2625 abfd != (bfd *) NULL;
2626 abfd = abfd->link.next)
2627 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2628 && !_bfd_elf_link_iterate_on_relocs (abfd, info,
2629 elf_x86_64_scan_relocs))
2630 return false;
2632 return _bfd_x86_elf_always_size_sections (output_bfd, info);
2635 /* Return the relocation value for @tpoff relocation
2636 if STT_TLS virtual address is ADDRESS. */
2638 static bfd_vma
2639 elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2641 struct elf_link_hash_table *htab = elf_hash_table (info);
2642 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2643 bfd_vma static_tls_size;
2645 /* If tls_segment is NULL, we should have signalled an error already. */
2646 if (htab->tls_sec == NULL)
2647 return 0;
2649 /* Consider special static TLS alignment requirements. */
2650 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2651 return address - static_tls_size - htab->tls_sec->vma;
2654 /* Relocate an x86_64 ELF section. */
2656 static int
2657 elf_x86_64_relocate_section (bfd *output_bfd,
2658 struct bfd_link_info *info,
2659 bfd *input_bfd,
2660 asection *input_section,
2661 bfd_byte *contents,
2662 Elf_Internal_Rela *relocs,
2663 Elf_Internal_Sym *local_syms,
2664 asection **local_sections)
2666 struct elf_x86_link_hash_table *htab;
2667 Elf_Internal_Shdr *symtab_hdr;
2668 struct elf_link_hash_entry **sym_hashes;
2669 bfd_vma *local_got_offsets;
2670 bfd_vma *local_tlsdesc_gotents;
2671 Elf_Internal_Rela *rel;
2672 Elf_Internal_Rela *wrel;
2673 Elf_Internal_Rela *relend;
2674 unsigned int plt_entry_size;
2675 bool status;
2677 /* Skip if check_relocs or scan_relocs failed. */
2678 if (input_section->check_relocs_failed)
2679 return false;
2681 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2682 if (htab == NULL)
2683 return false;
2685 if (!is_x86_elf (input_bfd, htab))
2687 bfd_set_error (bfd_error_wrong_format);
2688 return false;
2691 plt_entry_size = htab->plt.plt_entry_size;
2692 symtab_hdr = &elf_symtab_hdr (input_bfd);
2693 sym_hashes = elf_sym_hashes (input_bfd);
2694 local_got_offsets = elf_local_got_offsets (input_bfd);
2695 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2697 _bfd_x86_elf_set_tls_module_base (info);
2699 status = true;
2700 rel = wrel = relocs;
2701 relend = relocs + input_section->reloc_count;
2702 for (; rel < relend; wrel++, rel++)
2704 unsigned int r_type, r_type_tls;
2705 reloc_howto_type *howto;
2706 unsigned long r_symndx;
2707 struct elf_link_hash_entry *h;
2708 struct elf_x86_link_hash_entry *eh;
2709 Elf_Internal_Sym *sym;
2710 asection *sec;
2711 bfd_vma off, offplt, plt_offset;
2712 bfd_vma relocation;
2713 bool unresolved_reloc;
2714 bfd_reloc_status_type r;
2715 int tls_type;
2716 asection *base_got, *resolved_plt;
2717 bfd_vma st_size;
2718 bool resolved_to_zero;
2719 bool relative_reloc;
2720 bool converted_reloc;
2721 bool need_copy_reloc_in_pie;
2722 bool no_copyreloc_p;
2724 r_type = ELF32_R_TYPE (rel->r_info);
2725 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2726 || r_type == (int) R_X86_64_GNU_VTENTRY)
2728 if (wrel != rel)
2729 *wrel = *rel;
2730 continue;
2733 r_symndx = htab->r_sym (rel->r_info);
2734 converted_reloc = (r_type & R_X86_64_converted_reloc_bit) != 0;
2735 if (converted_reloc)
2737 r_type &= ~R_X86_64_converted_reloc_bit;
2738 rel->r_info = htab->r_info (r_symndx, r_type);
2741 howto = elf_x86_64_rtype_to_howto (input_bfd, r_type);
2742 if (howto == NULL)
2743 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2745 h = NULL;
2746 sym = NULL;
2747 sec = NULL;
2748 unresolved_reloc = false;
2749 if (r_symndx < symtab_hdr->sh_info)
2751 sym = local_syms + r_symndx;
2752 sec = local_sections[r_symndx];
2754 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2755 &sec, rel);
2756 st_size = sym->st_size;
2758 /* Relocate against local STT_GNU_IFUNC symbol. */
2759 if (!bfd_link_relocatable (info)
2760 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2762 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd,
2763 rel, false);
2764 if (h == NULL)
2765 abort ();
2767 /* Set STT_GNU_IFUNC symbol value. */
2768 h->root.u.def.value = sym->st_value;
2769 h->root.u.def.section = sec;
2772 else
2774 bool warned ATTRIBUTE_UNUSED;
2775 bool ignored ATTRIBUTE_UNUSED;
2777 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2778 r_symndx, symtab_hdr, sym_hashes,
2779 h, sec, relocation,
2780 unresolved_reloc, warned, ignored);
2781 st_size = h->size;
2784 if (sec != NULL && discarded_section (sec))
2786 _bfd_clear_contents (howto, input_bfd, input_section,
2787 contents, rel->r_offset);
2788 wrel->r_offset = rel->r_offset;
2789 wrel->r_info = 0;
2790 wrel->r_addend = 0;
2792 /* For ld -r, remove relocations in debug sections against
2793 sections defined in discarded sections. Not done for
2794 eh_frame editing code expects to be present. */
2795 if (bfd_link_relocatable (info)
2796 && (input_section->flags & SEC_DEBUGGING))
2797 wrel--;
2799 continue;
2802 if (bfd_link_relocatable (info))
2804 if (wrel != rel)
2805 *wrel = *rel;
2806 continue;
2809 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
2811 if (r_type == R_X86_64_64)
2813 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
2814 zero-extend it to 64bit if addend is zero. */
2815 r_type = R_X86_64_32;
2816 memset (contents + rel->r_offset + 4, 0, 4);
2818 else if (r_type == R_X86_64_SIZE64)
2820 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
2821 zero-extend it to 64bit if addend is zero. */
2822 r_type = R_X86_64_SIZE32;
2823 memset (contents + rel->r_offset + 4, 0, 4);
2827 eh = (struct elf_x86_link_hash_entry *) h;
2829 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2830 it here if it is defined in a non-shared object. */
2831 if (h != NULL
2832 && h->type == STT_GNU_IFUNC
2833 && h->def_regular)
2835 bfd_vma plt_index;
2836 const char *name;
2838 if ((input_section->flags & SEC_ALLOC) == 0)
2840 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2841 STT_GNU_IFUNC symbol as STT_FUNC. */
2842 if (elf_section_type (input_section) == SHT_NOTE)
2843 goto skip_ifunc;
2844 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2845 sections because such sections are not SEC_ALLOC and
2846 thus ld.so will not process them. */
2847 if ((input_section->flags & SEC_DEBUGGING) != 0)
2848 continue;
2849 abort ();
2852 switch (r_type)
2854 default:
2855 break;
2857 case R_X86_64_GOTPCREL:
2858 case R_X86_64_GOTPCRELX:
2859 case R_X86_64_REX_GOTPCRELX:
2860 case R_X86_64_CODE_4_GOTPCRELX:
2861 case R_X86_64_GOTPCREL64:
2862 base_got = htab->elf.sgot;
2863 off = h->got.offset;
2865 if (base_got == NULL)
2866 abort ();
2868 if (off == (bfd_vma) -1)
2870 /* We can't use h->got.offset here to save state, or
2871 even just remember the offset, as finish_dynamic_symbol
2872 would use that as offset into .got. */
2874 if (h->plt.offset == (bfd_vma) -1)
2875 abort ();
2877 if (htab->elf.splt != NULL)
2879 plt_index = (h->plt.offset / plt_entry_size
2880 - htab->plt.has_plt0);
2881 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2882 base_got = htab->elf.sgotplt;
2884 else
2886 plt_index = h->plt.offset / plt_entry_size;
2887 off = plt_index * GOT_ENTRY_SIZE;
2888 base_got = htab->elf.igotplt;
2891 if (h->dynindx == -1
2892 || h->forced_local
2893 || info->symbolic)
2895 /* This references the local defitionion. We must
2896 initialize this entry in the global offset table.
2897 Since the offset must always be a multiple of 8,
2898 we use the least significant bit to record
2899 whether we have initialized it already.
2901 When doing a dynamic link, we create a .rela.got
2902 relocation entry to initialize the value. This
2903 is done in the finish_dynamic_symbol routine. */
2904 if ((off & 1) != 0)
2905 off &= ~1;
2906 else
2908 bfd_put_64 (output_bfd, relocation,
2909 base_got->contents + off);
2910 /* Note that this is harmless for the GOTPLT64
2911 case, as -1 | 1 still is -1. */
2912 h->got.offset |= 1;
2917 relocation = (base_got->output_section->vma
2918 + base_got->output_offset + off);
2920 goto do_relocation;
2923 if (h->plt.offset == (bfd_vma) -1)
2925 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2926 if (r_type == htab->pointer_r_type
2927 && (input_section->flags & SEC_CODE) == 0)
2928 goto do_ifunc_pointer;
2929 goto bad_ifunc_reloc;
2932 /* STT_GNU_IFUNC symbol must go through PLT. */
2933 if (htab->elf.splt != NULL)
2935 if (htab->plt_second != NULL)
2937 resolved_plt = htab->plt_second;
2938 plt_offset = eh->plt_second.offset;
2940 else
2942 resolved_plt = htab->elf.splt;
2943 plt_offset = h->plt.offset;
2946 else
2948 resolved_plt = htab->elf.iplt;
2949 plt_offset = h->plt.offset;
2952 relocation = (resolved_plt->output_section->vma
2953 + resolved_plt->output_offset + plt_offset);
2955 switch (r_type)
2957 default:
2958 bad_ifunc_reloc:
2959 if (h->root.root.string)
2960 name = h->root.root.string;
2961 else
2962 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2963 NULL);
2964 _bfd_error_handler
2965 /* xgettext:c-format */
2966 (_("%pB: relocation %s against STT_GNU_IFUNC "
2967 "symbol `%s' isn't supported"), input_bfd,
2968 howto->name, name);
2969 bfd_set_error (bfd_error_bad_value);
2970 return false;
2972 case R_X86_64_32S:
2973 if (bfd_link_pic (info))
2974 abort ();
2975 goto do_relocation;
2977 case R_X86_64_32:
2978 if (ABI_64_P (output_bfd))
2979 goto do_relocation;
2980 /* FALLTHROUGH */
2981 case R_X86_64_64:
2982 do_ifunc_pointer:
2983 if (rel->r_addend != 0)
2985 if (h->root.root.string)
2986 name = h->root.root.string;
2987 else
2988 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2989 sym, NULL);
2990 _bfd_error_handler
2991 /* xgettext:c-format */
2992 (_("%pB: relocation %s against STT_GNU_IFUNC "
2993 "symbol `%s' has non-zero addend: %" PRId64),
2994 input_bfd, howto->name, name, (int64_t) rel->r_addend);
2995 bfd_set_error (bfd_error_bad_value);
2996 return false;
2999 /* Generate dynamic relcoation only when there is a
3000 non-GOT reference in a shared object or there is no
3001 PLT. */
3002 if ((bfd_link_pic (info) && h->non_got_ref)
3003 || h->plt.offset == (bfd_vma) -1)
3005 Elf_Internal_Rela outrel;
3006 asection *sreloc;
3008 /* Need a dynamic relocation to get the real function
3009 address. */
3010 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3011 info,
3012 input_section,
3013 rel->r_offset);
3014 if (outrel.r_offset == (bfd_vma) -1
3015 || outrel.r_offset == (bfd_vma) -2)
3016 abort ();
3018 outrel.r_offset += (input_section->output_section->vma
3019 + input_section->output_offset);
3021 if (POINTER_LOCAL_IFUNC_P (info, h))
3023 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3024 h->root.root.string,
3025 h->root.u.def.section->owner);
3027 /* This symbol is resolved locally. */
3028 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
3029 outrel.r_addend = (h->root.u.def.value
3030 + h->root.u.def.section->output_section->vma
3031 + h->root.u.def.section->output_offset);
3033 if (htab->params->report_relative_reloc)
3034 _bfd_x86_elf_link_report_relative_reloc
3035 (info, input_section, h, sym,
3036 "R_X86_64_IRELATIVE", &outrel);
3038 else
3040 outrel.r_info = htab->r_info (h->dynindx, r_type);
3041 outrel.r_addend = 0;
3044 /* Dynamic relocations are stored in
3045 1. .rela.ifunc section in PIC object.
3046 2. .rela.got section in dynamic executable.
3047 3. .rela.iplt section in static executable. */
3048 if (bfd_link_pic (info))
3049 sreloc = htab->elf.irelifunc;
3050 else if (htab->elf.splt != NULL)
3051 sreloc = htab->elf.srelgot;
3052 else
3053 sreloc = htab->elf.irelplt;
3054 elf_append_rela (output_bfd, sreloc, &outrel);
3056 /* If this reloc is against an external symbol, we
3057 do not want to fiddle with the addend. Otherwise,
3058 we need to include the symbol value so that it
3059 becomes an addend for the dynamic reloc. For an
3060 internal symbol, we have updated addend. */
3061 continue;
3063 /* FALLTHROUGH */
3064 case R_X86_64_PC32:
3065 case R_X86_64_PC64:
3066 case R_X86_64_PLT32:
3067 goto do_relocation;
3071 skip_ifunc:
3072 resolved_to_zero = (eh != NULL
3073 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
3075 /* When generating a shared object, the relocations handled here are
3076 copied into the output file to be resolved at run time. */
3077 switch (r_type)
3079 case R_X86_64_GOT32:
3080 case R_X86_64_GOT64:
3081 /* Relocation is to the entry for this symbol in the global
3082 offset table. */
3083 case R_X86_64_GOTPCREL:
3084 case R_X86_64_GOTPCRELX:
3085 case R_X86_64_REX_GOTPCRELX:
3086 case R_X86_64_CODE_4_GOTPCRELX:
3087 case R_X86_64_GOTPCREL64:
3088 /* Use global offset table entry as symbol value. */
3089 case R_X86_64_GOTPLT64:
3090 /* This is obsolete and treated the same as GOT64. */
3091 base_got = htab->elf.sgot;
3093 if (htab->elf.sgot == NULL)
3094 abort ();
3096 relative_reloc = false;
3097 if (h != NULL)
3099 off = h->got.offset;
3100 if (h->needs_plt
3101 && h->plt.offset != (bfd_vma)-1
3102 && off == (bfd_vma)-1)
3104 /* We can't use h->got.offset here to save
3105 state, or even just remember the offset, as
3106 finish_dynamic_symbol would use that as offset into
3107 .got. */
3108 bfd_vma plt_index = (h->plt.offset / plt_entry_size
3109 - htab->plt.has_plt0);
3110 off = (plt_index + 3) * GOT_ENTRY_SIZE;
3111 base_got = htab->elf.sgotplt;
3114 if (RESOLVED_LOCALLY_P (info, h, htab))
3116 /* We must initialize this entry in the global offset
3117 table. Since the offset must always be a multiple
3118 of 8, we use the least significant bit to record
3119 whether we have initialized it already.
3121 When doing a dynamic link, we create a .rela.got
3122 relocation entry to initialize the value. This is
3123 done in the finish_dynamic_symbol routine. */
3124 if ((off & 1) != 0)
3125 off &= ~1;
3126 else
3128 bfd_put_64 (output_bfd, relocation,
3129 base_got->contents + off);
3130 /* Note that this is harmless for the GOTPLT64 case,
3131 as -1 | 1 still is -1. */
3132 h->got.offset |= 1;
3134 /* NB: Don't generate relative relocation here if
3135 it has been generated by DT_RELR. */
3136 if (!info->enable_dt_relr
3137 && GENERATE_RELATIVE_RELOC_P (info, h))
3139 /* If this symbol isn't dynamic in PIC,
3140 generate R_X86_64_RELATIVE here. */
3141 eh->no_finish_dynamic_symbol = 1;
3142 relative_reloc = true;
3146 else
3147 unresolved_reloc = false;
3149 else
3151 if (local_got_offsets == NULL)
3152 abort ();
3154 off = local_got_offsets[r_symndx];
3156 /* The offset must always be a multiple of 8. We use
3157 the least significant bit to record whether we have
3158 already generated the necessary reloc. */
3159 if ((off & 1) != 0)
3160 off &= ~1;
3161 else
3163 bfd_put_64 (output_bfd, relocation,
3164 base_got->contents + off);
3165 local_got_offsets[r_symndx] |= 1;
3167 /* NB: GOTPCREL relocations against local absolute
3168 symbol store relocation value in the GOT slot
3169 without relative relocation. Don't generate
3170 relative relocation here if it has been generated
3171 by DT_RELR. */
3172 if (!info->enable_dt_relr
3173 && bfd_link_pic (info)
3174 && !(sym->st_shndx == SHN_ABS
3175 && (r_type == R_X86_64_GOTPCREL
3176 || r_type == R_X86_64_GOTPCRELX
3177 || r_type == R_X86_64_REX_GOTPCRELX
3178 || r_type == R_X86_64_CODE_4_GOTPCRELX)))
3179 relative_reloc = true;
3183 if (relative_reloc)
3185 asection *s;
3186 Elf_Internal_Rela outrel;
3188 /* We need to generate a R_X86_64_RELATIVE reloc
3189 for the dynamic linker. */
3190 s = htab->elf.srelgot;
3191 if (s == NULL)
3192 abort ();
3194 outrel.r_offset = (base_got->output_section->vma
3195 + base_got->output_offset
3196 + off);
3197 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3198 outrel.r_addend = relocation;
3200 if (htab->params->report_relative_reloc)
3201 _bfd_x86_elf_link_report_relative_reloc
3202 (info, input_section, h, sym, "R_X86_64_RELATIVE",
3203 &outrel);
3205 elf_append_rela (output_bfd, s, &outrel);
3208 if (off >= (bfd_vma) -2)
3209 abort ();
3211 relocation = base_got->output_section->vma
3212 + base_got->output_offset + off;
3213 if (r_type != R_X86_64_GOTPCREL
3214 && r_type != R_X86_64_GOTPCRELX
3215 && r_type != R_X86_64_REX_GOTPCRELX
3216 && r_type != R_X86_64_CODE_4_GOTPCRELX
3217 && r_type != R_X86_64_GOTPCREL64)
3218 relocation -= htab->elf.sgotplt->output_section->vma
3219 - htab->elf.sgotplt->output_offset;
3221 break;
3223 case R_X86_64_GOTOFF64:
3224 /* Relocation is relative to the start of the global offset
3225 table. */
3227 /* Check to make sure it isn't a protected function or data
3228 symbol for shared library since it may not be local when
3229 used as function address or with copy relocation. We also
3230 need to make sure that a symbol is referenced locally. */
3231 if (bfd_link_pic (info) && h)
3233 if (!h->def_regular)
3235 const char *v;
3237 switch (ELF_ST_VISIBILITY (h->other))
3239 case STV_HIDDEN:
3240 v = _("hidden symbol");
3241 break;
3242 case STV_INTERNAL:
3243 v = _("internal symbol");
3244 break;
3245 case STV_PROTECTED:
3246 v = _("protected symbol");
3247 break;
3248 default:
3249 v = _("symbol");
3250 break;
3253 _bfd_error_handler
3254 /* xgettext:c-format */
3255 (_("%pB: relocation R_X86_64_GOTOFF64 against undefined %s"
3256 " `%s' can not be used when making a shared object"),
3257 input_bfd, v, h->root.root.string);
3258 bfd_set_error (bfd_error_bad_value);
3259 return false;
3261 else if (!bfd_link_executable (info)
3262 && !SYMBOL_REFERENCES_LOCAL_P (info, h)
3263 && (h->type == STT_FUNC
3264 || h->type == STT_OBJECT)
3265 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3267 _bfd_error_handler
3268 /* xgettext:c-format */
3269 (_("%pB: relocation R_X86_64_GOTOFF64 against protected %s"
3270 " `%s' can not be used when making a shared object"),
3271 input_bfd,
3272 h->type == STT_FUNC ? "function" : "data",
3273 h->root.root.string);
3274 bfd_set_error (bfd_error_bad_value);
3275 return false;
3279 /* Note that sgot is not involved in this
3280 calculation. We always want the start of .got.plt. If we
3281 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3282 permitted by the ABI, we might have to change this
3283 calculation. */
3284 relocation -= htab->elf.sgotplt->output_section->vma
3285 + htab->elf.sgotplt->output_offset;
3286 break;
3288 case R_X86_64_GOTPC32:
3289 case R_X86_64_GOTPC64:
3290 /* Use global offset table as symbol value. */
3291 relocation = htab->elf.sgotplt->output_section->vma
3292 + htab->elf.sgotplt->output_offset;
3293 unresolved_reloc = false;
3294 break;
3296 case R_X86_64_PLTOFF64:
3297 /* Relocation is PLT entry relative to GOT. For local
3298 symbols it's the symbol itself relative to GOT. */
3299 if (h != NULL
3300 /* See PLT32 handling. */
3301 && (h->plt.offset != (bfd_vma) -1
3302 || eh->plt_got.offset != (bfd_vma) -1)
3303 && htab->elf.splt != NULL)
3305 if (eh->plt_got.offset != (bfd_vma) -1)
3307 /* Use the GOT PLT. */
3308 resolved_plt = htab->plt_got;
3309 plt_offset = eh->plt_got.offset;
3311 else if (htab->plt_second != NULL)
3313 resolved_plt = htab->plt_second;
3314 plt_offset = eh->plt_second.offset;
3316 else
3318 resolved_plt = htab->elf.splt;
3319 plt_offset = h->plt.offset;
3322 relocation = (resolved_plt->output_section->vma
3323 + resolved_plt->output_offset
3324 + plt_offset);
3325 unresolved_reloc = false;
3328 relocation -= htab->elf.sgotplt->output_section->vma
3329 + htab->elf.sgotplt->output_offset;
3330 break;
3332 case R_X86_64_PLT32:
3333 /* Relocation is to the entry for this symbol in the
3334 procedure linkage table. */
3336 /* Resolve a PLT32 reloc against a local symbol directly,
3337 without using the procedure linkage table. */
3338 if (h == NULL)
3339 break;
3341 if ((h->plt.offset == (bfd_vma) -1
3342 && eh->plt_got.offset == (bfd_vma) -1)
3343 || htab->elf.splt == NULL)
3345 /* We didn't make a PLT entry for this symbol. This
3346 happens when statically linking PIC code, or when
3347 using -Bsymbolic. */
3348 break;
3351 use_plt:
3352 if (h->plt.offset != (bfd_vma) -1)
3354 if (htab->plt_second != NULL)
3356 resolved_plt = htab->plt_second;
3357 plt_offset = eh->plt_second.offset;
3359 else
3361 resolved_plt = htab->elf.splt;
3362 plt_offset = h->plt.offset;
3365 else
3367 /* Use the GOT PLT. */
3368 resolved_plt = htab->plt_got;
3369 plt_offset = eh->plt_got.offset;
3372 relocation = (resolved_plt->output_section->vma
3373 + resolved_plt->output_offset
3374 + plt_offset);
3375 unresolved_reloc = false;
3376 break;
3378 case R_X86_64_SIZE32:
3379 case R_X86_64_SIZE64:
3380 /* Set to symbol size. */
3381 relocation = st_size;
3382 goto direct;
3384 case R_X86_64_PC8:
3385 case R_X86_64_PC16:
3386 case R_X86_64_PC32:
3387 /* Don't complain about -fPIC if the symbol is undefined when
3388 building executable unless it is unresolved weak symbol,
3389 references a dynamic definition in PIE or -z nocopyreloc
3390 is used. */
3391 no_copyreloc_p
3392 = (info->nocopyreloc
3393 || (h != NULL
3394 && !h->root.linker_def
3395 && !h->root.ldscript_def
3396 && eh->def_protected));
3398 if ((input_section->flags & SEC_ALLOC) != 0
3399 && (input_section->flags & SEC_READONLY) != 0
3400 && h != NULL
3401 && ((bfd_link_executable (info)
3402 && ((h->root.type == bfd_link_hash_undefweak
3403 && (eh == NULL
3404 || !UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
3405 eh)))
3406 || (bfd_link_pie (info)
3407 && !SYMBOL_DEFINED_NON_SHARED_P (h)
3408 && h->def_dynamic)
3409 || (no_copyreloc_p
3410 && h->def_dynamic
3411 && !(h->root.u.def.section->flags & SEC_CODE))))
3412 || (bfd_link_pie (info)
3413 && h->root.type == bfd_link_hash_undefweak)
3414 || bfd_link_dll (info)))
3416 bool fail = false;
3417 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3419 /* Symbol is referenced locally. Make sure it is
3420 defined locally. */
3421 fail = !SYMBOL_DEFINED_NON_SHARED_P (h);
3423 else if (bfd_link_pie (info))
3425 /* We can only use PC-relative relocations in PIE
3426 from non-code sections. */
3427 if (h->root.type == bfd_link_hash_undefweak
3428 || (h->type == STT_FUNC
3429 && (sec->flags & SEC_CODE) != 0))
3430 fail = true;
3432 else if (no_copyreloc_p || bfd_link_dll (info))
3434 /* Symbol doesn't need copy reloc and isn't
3435 referenced locally. Don't allow PC-relative
3436 relocations against default and protected
3437 symbols since address of protected function
3438 and location of protected data may not be in
3439 the shared object. */
3440 fail = (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3441 || ELF_ST_VISIBILITY (h->other) == STV_PROTECTED);
3444 if (fail)
3445 return elf_x86_64_need_pic (info, input_bfd, input_section,
3446 h, NULL, NULL, howto);
3448 /* Since x86-64 has PC-relative PLT, we can use PLT in PIE
3449 as function address. */
3450 else if (h != NULL
3451 && (input_section->flags & SEC_CODE) == 0
3452 && bfd_link_pie (info)
3453 && h->type == STT_FUNC
3454 && !h->def_regular
3455 && h->def_dynamic)
3456 goto use_plt;
3457 /* Fall through. */
3459 case R_X86_64_8:
3460 case R_X86_64_16:
3461 case R_X86_64_32:
3462 case R_X86_64_PC64:
3463 case R_X86_64_64:
3464 /* FIXME: The ABI says the linker should make sure the value is
3465 the same when it's zeroextended to 64 bit. */
3467 direct:
3468 if ((input_section->flags & SEC_ALLOC) == 0)
3469 break;
3471 need_copy_reloc_in_pie = (bfd_link_pie (info)
3472 && h != NULL
3473 && (h->needs_copy
3474 || eh->needs_copy
3475 || (h->root.type
3476 == bfd_link_hash_undefined))
3477 && (X86_PCREL_TYPE_P (true, r_type)
3478 || X86_SIZE_TYPE_P (true,
3479 r_type)));
3481 if (GENERATE_DYNAMIC_RELOCATION_P (true, info, eh, r_type, sec,
3482 need_copy_reloc_in_pie,
3483 resolved_to_zero, false))
3485 Elf_Internal_Rela outrel;
3486 bool skip, relocate;
3487 bool generate_dynamic_reloc = true;
3488 asection *sreloc;
3489 const char *relative_reloc_name = NULL;
3491 /* When generating a shared object, these relocations
3492 are copied into the output file to be resolved at run
3493 time. */
3494 skip = false;
3495 relocate = false;
3497 outrel.r_offset =
3498 _bfd_elf_section_offset (output_bfd, info, input_section,
3499 rel->r_offset);
3500 if (outrel.r_offset == (bfd_vma) -1)
3501 skip = true;
3502 else if (outrel.r_offset == (bfd_vma) -2)
3503 skip = true, relocate = true;
3505 outrel.r_offset += (input_section->output_section->vma
3506 + input_section->output_offset);
3508 if (skip)
3509 memset (&outrel, 0, sizeof outrel);
3511 else if (COPY_INPUT_RELOC_P (true, info, h, r_type))
3513 outrel.r_info = htab->r_info (h->dynindx, r_type);
3514 outrel.r_addend = rel->r_addend;
3516 else
3518 /* This symbol is local, or marked to become local.
3519 When relocation overflow check is disabled, we
3520 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
3521 if (r_type == htab->pointer_r_type
3522 || (r_type == R_X86_64_32
3523 && htab->params->no_reloc_overflow_check))
3525 relocate = true;
3526 /* NB: Don't generate relative relocation here if
3527 it has been generated by DT_RELR. */
3528 if (info->enable_dt_relr)
3529 generate_dynamic_reloc = false;
3530 else
3532 outrel.r_info =
3533 htab->r_info (0, R_X86_64_RELATIVE);
3534 outrel.r_addend = relocation + rel->r_addend;
3535 relative_reloc_name = "R_X86_64_RELATIVE";
3538 else if (r_type == R_X86_64_64
3539 && !ABI_64_P (output_bfd))
3541 relocate = true;
3542 outrel.r_info = htab->r_info (0,
3543 R_X86_64_RELATIVE64);
3544 outrel.r_addend = relocation + rel->r_addend;
3545 relative_reloc_name = "R_X86_64_RELATIVE64";
3546 /* Check addend overflow. */
3547 if ((outrel.r_addend & 0x80000000)
3548 != (rel->r_addend & 0x80000000))
3550 const char *name;
3551 int addend = rel->r_addend;
3552 if (h && h->root.root.string)
3553 name = h->root.root.string;
3554 else
3555 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3556 sym, NULL);
3557 _bfd_error_handler
3558 /* xgettext:c-format */
3559 (_("%pB: addend %s%#x in relocation %s against "
3560 "symbol `%s' at %#" PRIx64
3561 " in section `%pA' is out of range"),
3562 input_bfd, addend < 0 ? "-" : "", addend,
3563 howto->name, name, (uint64_t) rel->r_offset,
3564 input_section);
3565 bfd_set_error (bfd_error_bad_value);
3566 return false;
3569 else
3571 long sindx;
3573 if (bfd_is_abs_section (sec))
3574 sindx = 0;
3575 else if (sec == NULL || sec->owner == NULL)
3577 bfd_set_error (bfd_error_bad_value);
3578 return false;
3580 else
3582 asection *osec;
3584 /* We are turning this relocation into one
3585 against a section symbol. It would be
3586 proper to subtract the symbol's value,
3587 osec->vma, from the emitted reloc addend,
3588 but ld.so expects buggy relocs. */
3589 osec = sec->output_section;
3590 sindx = elf_section_data (osec)->dynindx;
3591 if (sindx == 0)
3593 asection *oi = htab->elf.text_index_section;
3594 sindx = elf_section_data (oi)->dynindx;
3596 BFD_ASSERT (sindx != 0);
3599 outrel.r_info = htab->r_info (sindx, r_type);
3600 outrel.r_addend = relocation + rel->r_addend;
3604 if (generate_dynamic_reloc)
3606 sreloc = elf_section_data (input_section)->sreloc;
3608 if (sreloc == NULL || sreloc->contents == NULL)
3610 r = bfd_reloc_notsupported;
3611 goto check_relocation_error;
3614 if (relative_reloc_name
3615 && htab->params->report_relative_reloc)
3616 _bfd_x86_elf_link_report_relative_reloc
3617 (info, input_section, h, sym,
3618 relative_reloc_name, &outrel);
3620 elf_append_rela (output_bfd, sreloc, &outrel);
3623 /* If this reloc is against an external symbol, we do
3624 not want to fiddle with the addend. Otherwise, we
3625 need to include the symbol value so that it becomes
3626 an addend for the dynamic reloc. */
3627 if (! relocate)
3628 continue;
3631 break;
3633 case R_X86_64_TLSGD:
3634 case R_X86_64_GOTPC32_TLSDESC:
3635 case R_X86_64_CODE_4_GOTPC32_TLSDESC:
3636 case R_X86_64_TLSDESC_CALL:
3637 case R_X86_64_GOTTPOFF:
3638 case R_X86_64_CODE_4_GOTTPOFF:
3639 case R_X86_64_CODE_6_GOTTPOFF:
3640 tls_type = GOT_UNKNOWN;
3641 if (h == NULL && local_got_offsets)
3642 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
3643 else if (h != NULL)
3644 tls_type = elf_x86_hash_entry (h)->tls_type;
3646 r_type_tls = r_type;
3647 if (! elf_x86_64_tls_transition (info, input_bfd,
3648 input_section, contents,
3649 symtab_hdr, sym_hashes,
3650 &r_type_tls, tls_type, rel,
3651 relend, h, r_symndx, true))
3652 return false;
3654 if (r_type_tls == R_X86_64_TPOFF32)
3656 bfd_vma roff = rel->r_offset;
3658 if (roff >= input_section->size)
3659 goto corrupt_input;
3661 BFD_ASSERT (! unresolved_reloc);
3663 if (r_type == R_X86_64_TLSGD)
3665 /* GD->LE transition. For 64bit, change
3666 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3667 .word 0x6666; rex64; call __tls_get_addr@PLT
3669 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3670 .byte 0x66; rex64
3671 call *__tls_get_addr@GOTPCREL(%rip)
3672 which may be converted to
3673 addr32 call __tls_get_addr
3674 into:
3675 movq %fs:0, %rax
3676 leaq foo@tpoff(%rax), %rax
3677 For 32bit, change
3678 leaq foo@tlsgd(%rip), %rdi
3679 .word 0x6666; rex64; call __tls_get_addr@PLT
3681 leaq foo@tlsgd(%rip), %rdi
3682 .byte 0x66; rex64
3683 call *__tls_get_addr@GOTPCREL(%rip)
3684 which may be converted to
3685 addr32 call __tls_get_addr
3686 into:
3687 movl %fs:0, %eax
3688 leaq foo@tpoff(%rax), %rax
3689 For largepic, change:
3690 leaq foo@tlsgd(%rip), %rdi
3691 movabsq $__tls_get_addr@pltoff, %rax
3692 addq %r15, %rax
3693 call *%rax
3694 into:
3695 movq %fs:0, %rax
3696 leaq foo@tpoff(%rax), %rax
3697 nopw 0x0(%rax,%rax,1) */
3698 int largepic = 0;
3699 if (ABI_64_P (output_bfd))
3701 if (roff + 5 >= input_section->size)
3702 goto corrupt_input;
3703 if (contents[roff + 5] == 0xb8)
3705 if (roff < 3
3706 || (roff - 3 + 22) > input_section->size)
3708 corrupt_input:
3709 info->callbacks->einfo
3710 (_("%F%P: corrupt input: %pB\n"),
3711 input_bfd);
3712 return false;
3714 memcpy (contents + roff - 3,
3715 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
3716 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3717 largepic = 1;
3719 else
3721 if (roff < 4
3722 || (roff - 4 + 16) > input_section->size)
3723 goto corrupt_input;
3724 memcpy (contents + roff - 4,
3725 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3726 16);
3729 else
3731 if (roff < 3
3732 || (roff - 3 + 15) > input_section->size)
3733 goto corrupt_input;
3734 memcpy (contents + roff - 3,
3735 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3736 15);
3739 if (roff + 8 + largepic >= input_section->size)
3740 goto corrupt_input;
3742 bfd_put_32 (output_bfd,
3743 elf_x86_64_tpoff (info, relocation),
3744 contents + roff + 8 + largepic);
3745 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
3746 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
3747 rel++;
3748 wrel++;
3749 continue;
3751 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
3753 /* GDesc -> LE transition.
3754 It's originally something like:
3755 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3756 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
3758 Change it to:
3759 movq $x@tpoff, %rax <--- LP64 mode.
3760 rex movl $x@tpoff, %eax <--- X32 mode.
3763 unsigned int val, type;
3765 if (roff < 3)
3766 goto corrupt_input;
3767 type = bfd_get_8 (input_bfd, contents + roff - 3);
3768 val = bfd_get_8 (input_bfd, contents + roff - 1);
3769 bfd_put_8 (output_bfd,
3770 (type & 0x48) | ((type >> 2) & 1),
3771 contents + roff - 3);
3772 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3773 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3774 contents + roff - 1);
3775 bfd_put_32 (output_bfd,
3776 elf_x86_64_tpoff (info, relocation),
3777 contents + roff);
3778 continue;
3780 else if (r_type == R_X86_64_CODE_4_GOTPC32_TLSDESC)
3782 /* GDesc -> LE transition.
3783 It's originally something like:
3784 lea x@tlsdesc(%rip), %reg
3786 Change it to:
3787 mov $x@tpoff, %reg
3788 where reg is one of r16 to r31. */
3790 unsigned int val, rex2;
3791 unsigned int rex2_mask = REX_R | REX_R << 4;
3793 if (roff < 4)
3794 goto corrupt_input;
3795 rex2 = bfd_get_8 (input_bfd, contents + roff - 3);
3796 val = bfd_get_8 (input_bfd, contents + roff - 1);
3797 /* Move the R bits to the B bits in REX2 payload
3798 byte. */
3799 bfd_put_8 (output_bfd,
3800 ((rex2 & ~rex2_mask)
3801 | (rex2 & rex2_mask) >> 2),
3802 contents + roff - 3);
3803 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3804 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3805 contents + roff - 1);
3806 bfd_put_32 (output_bfd,
3807 elf_x86_64_tpoff (info, relocation),
3808 contents + roff);
3809 continue;
3811 else if (r_type == R_X86_64_TLSDESC_CALL)
3813 /* GDesc -> LE transition.
3814 It's originally:
3815 call *(%rax) <--- LP64 mode.
3816 call *(%eax) <--- X32 mode.
3817 Turn it into:
3818 xchg %ax,%ax <-- LP64 mode.
3819 nopl (%rax) <-- X32 mode.
3821 unsigned int prefix = 0;
3822 if (!ABI_64_P (input_bfd))
3824 /* Check for call *x@tlsdesc(%eax). */
3825 if (contents[roff] == 0x67)
3826 prefix = 1;
3828 if (prefix)
3830 if (roff + 2 >= input_section->size)
3831 goto corrupt_input;
3833 bfd_put_8 (output_bfd, 0x0f, contents + roff);
3834 bfd_put_8 (output_bfd, 0x1f, contents + roff + 1);
3835 bfd_put_8 (output_bfd, 0x00, contents + roff + 2);
3837 else
3839 if (roff + 1 >= input_section->size)
3840 goto corrupt_input;
3842 bfd_put_8 (output_bfd, 0x66, contents + roff);
3843 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3845 continue;
3847 else if (r_type == R_X86_64_GOTTPOFF)
3849 /* IE->LE transition:
3850 For 64bit, originally it can be one of:
3851 movq foo@gottpoff(%rip), %reg
3852 addq foo@gottpoff(%rip), %reg
3853 We change it into:
3854 movq $foo, %reg
3855 leaq foo(%reg), %reg
3856 addq $foo, %reg.
3857 For 32bit, originally it can be one of:
3858 movq foo@gottpoff(%rip), %reg
3859 addl foo@gottpoff(%rip), %reg
3860 We change it into:
3861 movq $foo, %reg
3862 leal foo(%reg), %reg
3863 addl $foo, %reg. */
3865 unsigned int val, type, reg;
3867 if (roff >= 3)
3868 val = bfd_get_8 (input_bfd, contents + roff - 3);
3869 else
3871 if (roff < 2)
3872 goto corrupt_input;
3873 val = 0;
3875 type = bfd_get_8 (input_bfd, contents + roff - 2);
3876 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3877 reg >>= 3;
3878 if (type == 0x8b)
3880 /* movq */
3881 if (val == 0x4c)
3883 if (roff < 3)
3884 goto corrupt_input;
3885 bfd_put_8 (output_bfd, 0x49,
3886 contents + roff - 3);
3888 else if (!ABI_64_P (output_bfd) && val == 0x44)
3890 if (roff < 3)
3891 goto corrupt_input;
3892 bfd_put_8 (output_bfd, 0x41,
3893 contents + roff - 3);
3895 bfd_put_8 (output_bfd, 0xc7,
3896 contents + roff - 2);
3897 bfd_put_8 (output_bfd, 0xc0 | reg,
3898 contents + roff - 1);
3900 else if (reg == 4)
3902 /* addq/addl -> addq/addl - addressing with %rsp/%r12
3903 is special */
3904 if (val == 0x4c)
3906 if (roff < 3)
3907 goto corrupt_input;
3908 bfd_put_8 (output_bfd, 0x49,
3909 contents + roff - 3);
3911 else if (!ABI_64_P (output_bfd) && val == 0x44)
3913 if (roff < 3)
3914 goto corrupt_input;
3915 bfd_put_8 (output_bfd, 0x41,
3916 contents + roff - 3);
3918 bfd_put_8 (output_bfd, 0x81,
3919 contents + roff - 2);
3920 bfd_put_8 (output_bfd, 0xc0 | reg,
3921 contents + roff - 1);
3923 else
3925 /* addq/addl -> leaq/leal */
3926 if (val == 0x4c)
3928 if (roff < 3)
3929 goto corrupt_input;
3930 bfd_put_8 (output_bfd, 0x4d,
3931 contents + roff - 3);
3933 else if (!ABI_64_P (output_bfd) && val == 0x44)
3935 if (roff < 3)
3936 goto corrupt_input;
3937 bfd_put_8 (output_bfd, 0x45,
3938 contents + roff - 3);
3940 bfd_put_8 (output_bfd, 0x8d,
3941 contents + roff - 2);
3942 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3943 contents + roff - 1);
3945 bfd_put_32 (output_bfd,
3946 elf_x86_64_tpoff (info, relocation),
3947 contents + roff);
3948 continue;
3950 else if (r_type == R_X86_64_CODE_4_GOTTPOFF)
3952 /* IE->LE transition:
3953 Originally it can be one of:
3954 mov foo@gottpoff(%rip), %reg
3955 add foo@gottpoff(%rip), %reg
3956 We change it into:
3957 mov $foo@tpoff, %reg
3958 add $foo@tpoff, %reg
3959 where reg is one of r16 to r31. */
3961 unsigned int rex2, type, reg;
3962 unsigned int rex2_mask = REX_R | REX_R << 4;
3964 if (roff < 4)
3965 goto corrupt_input;
3967 rex2 = bfd_get_8 (input_bfd, contents + roff - 3);
3968 type = bfd_get_8 (input_bfd, contents + roff - 2);
3969 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3970 reg >>= 3;
3971 /* Move the R bits to the B bits in REX2 payload
3972 byte. */
3973 if (type == 0x8b)
3974 type = 0xc7;
3975 else
3976 type = 0x81;
3977 bfd_put_8 (output_bfd,
3978 ((rex2 & ~rex2_mask)
3979 | (rex2 & rex2_mask) >> 2),
3980 contents + roff - 3);
3981 bfd_put_8 (output_bfd, type,
3982 contents + roff - 2);
3983 bfd_put_8 (output_bfd, 0xc0 | reg,
3984 contents + roff - 1);
3985 bfd_put_32 (output_bfd,
3986 elf_x86_64_tpoff (info, relocation),
3987 contents + roff);
3988 continue;
3990 else if (r_type == R_X86_64_CODE_6_GOTTPOFF)
3992 /* IE->LE transition:
3993 Originally it is
3994 add %reg1, foo@gottpoff(%rip), %reg2
3996 add foo@gottpoff(%rip), %reg1, %reg2
3997 We change it into:
3998 add $foo@tpoff, %reg1, %reg2
4000 unsigned int reg, byte1;
4001 unsigned int updated_byte1;
4003 if (roff < 6)
4004 goto corrupt_input;
4006 /* Move the R bits to the B bits in EVEX payload
4007 byte 1. */
4008 byte1 = bfd_get_8 (input_bfd, contents + roff - 5);
4009 updated_byte1 = byte1;
4011 /* Set the R bits since they is inverted. */
4012 updated_byte1 |= 1 << 7 | 1 << 4;
4014 /* Update the B bits from the R bits. */
4015 if ((byte1 & (1 << 7)) == 0)
4016 updated_byte1 &= ~(1 << 5);
4017 if ((byte1 & (1 << 4)) == 0)
4018 updated_byte1 |= 1 << 3;
4020 reg = bfd_get_8 (input_bfd, contents + roff - 1);
4021 reg >>= 3;
4023 bfd_put_8 (output_bfd, updated_byte1,
4024 contents + roff - 5);
4025 bfd_put_8 (output_bfd, 0x81,
4026 contents + roff - 2);
4027 bfd_put_8 (output_bfd, 0xc0 | reg,
4028 contents + roff - 1);
4029 bfd_put_32 (output_bfd,
4030 elf_x86_64_tpoff (info, relocation),
4031 contents + roff);
4032 continue;
4034 else
4035 BFD_ASSERT (false);
4038 if (htab->elf.sgot == NULL)
4039 abort ();
4041 if (h != NULL)
4043 off = h->got.offset;
4044 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
4046 else
4048 if (local_got_offsets == NULL)
4049 abort ();
4051 off = local_got_offsets[r_symndx];
4052 offplt = local_tlsdesc_gotents[r_symndx];
4055 if ((off & 1) != 0)
4056 off &= ~1;
4057 else
4059 Elf_Internal_Rela outrel;
4060 int dr_type, indx;
4061 asection *sreloc;
4063 if (htab->elf.srelgot == NULL)
4064 abort ();
4066 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4068 if (GOT_TLS_GDESC_P (tls_type))
4070 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
4071 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
4072 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
4073 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4074 + htab->elf.sgotplt->output_offset
4075 + offplt
4076 + htab->sgotplt_jump_table_size);
4077 sreloc = htab->elf.srelplt;
4078 if (indx == 0)
4079 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
4080 else
4081 outrel.r_addend = 0;
4082 elf_append_rela (output_bfd, sreloc, &outrel);
4085 sreloc = htab->elf.srelgot;
4087 outrel.r_offset = (htab->elf.sgot->output_section->vma
4088 + htab->elf.sgot->output_offset + off);
4090 if (GOT_TLS_GD_P (tls_type))
4091 dr_type = R_X86_64_DTPMOD64;
4092 else if (GOT_TLS_GDESC_P (tls_type))
4093 goto dr_done;
4094 else
4095 dr_type = R_X86_64_TPOFF64;
4097 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
4098 outrel.r_addend = 0;
4099 if ((dr_type == R_X86_64_TPOFF64
4100 || dr_type == R_X86_64_TLSDESC) && indx == 0)
4101 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
4102 outrel.r_info = htab->r_info (indx, dr_type);
4104 elf_append_rela (output_bfd, sreloc, &outrel);
4106 if (GOT_TLS_GD_P (tls_type))
4108 if (indx == 0)
4110 BFD_ASSERT (! unresolved_reloc);
4111 bfd_put_64 (output_bfd,
4112 relocation - _bfd_x86_elf_dtpoff_base (info),
4113 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4115 else
4117 bfd_put_64 (output_bfd, 0,
4118 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4119 outrel.r_info = htab->r_info (indx,
4120 R_X86_64_DTPOFF64);
4121 outrel.r_offset += GOT_ENTRY_SIZE;
4122 elf_append_rela (output_bfd, sreloc,
4123 &outrel);
4127 dr_done:
4128 if (h != NULL)
4129 h->got.offset |= 1;
4130 else
4131 local_got_offsets[r_symndx] |= 1;
4134 if (off >= (bfd_vma) -2
4135 && ! GOT_TLS_GDESC_P (tls_type))
4136 abort ();
4137 if (r_type_tls == r_type)
4139 if (r_type == R_X86_64_GOTPC32_TLSDESC
4140 || r_type == R_X86_64_CODE_4_GOTPC32_TLSDESC
4141 || r_type == R_X86_64_TLSDESC_CALL)
4142 relocation = htab->elf.sgotplt->output_section->vma
4143 + htab->elf.sgotplt->output_offset
4144 + offplt + htab->sgotplt_jump_table_size;
4145 else
4146 relocation = htab->elf.sgot->output_section->vma
4147 + htab->elf.sgot->output_offset + off;
4148 unresolved_reloc = false;
4150 else
4152 bfd_vma roff = rel->r_offset;
4154 if (r_type == R_X86_64_TLSGD)
4156 /* GD->IE transition. For 64bit, change
4157 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4158 .word 0x6666; rex64; call __tls_get_addr@PLT
4160 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4161 .byte 0x66; rex64
4162 call *__tls_get_addr@GOTPCREL(%rip
4163 which may be converted to
4164 addr32 call __tls_get_addr
4165 into:
4166 movq %fs:0, %rax
4167 addq foo@gottpoff(%rip), %rax
4168 For 32bit, change
4169 leaq foo@tlsgd(%rip), %rdi
4170 .word 0x6666; rex64; call __tls_get_addr@PLT
4172 leaq foo@tlsgd(%rip), %rdi
4173 .byte 0x66; rex64;
4174 call *__tls_get_addr@GOTPCREL(%rip)
4175 which may be converted to
4176 addr32 call __tls_get_addr
4177 into:
4178 movl %fs:0, %eax
4179 addq foo@gottpoff(%rip), %rax
4180 For largepic, change:
4181 leaq foo@tlsgd(%rip), %rdi
4182 movabsq $__tls_get_addr@pltoff, %rax
4183 addq %r15, %rax
4184 call *%rax
4185 into:
4186 movq %fs:0, %rax
4187 addq foo@gottpoff(%rax), %rax
4188 nopw 0x0(%rax,%rax,1) */
4189 int largepic = 0;
4190 if (ABI_64_P (output_bfd))
4192 if (contents[roff + 5] == 0xb8)
4194 if (roff < 3
4195 || (roff - 3 + 22) > input_section->size)
4196 goto corrupt_input;
4197 memcpy (contents + roff - 3,
4198 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
4199 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4200 largepic = 1;
4202 else
4204 if (roff < 4
4205 || (roff - 4 + 16) > input_section->size)
4206 goto corrupt_input;
4207 memcpy (contents + roff - 4,
4208 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4209 16);
4212 else
4214 if (roff < 3
4215 || (roff - 3 + 15) > input_section->size)
4216 goto corrupt_input;
4217 memcpy (contents + roff - 3,
4218 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4219 15);
4222 relocation = (htab->elf.sgot->output_section->vma
4223 + htab->elf.sgot->output_offset + off
4224 - roff
4225 - largepic
4226 - input_section->output_section->vma
4227 - input_section->output_offset
4228 - 12);
4229 bfd_put_32 (output_bfd, relocation,
4230 contents + roff + 8 + largepic);
4231 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
4232 rel++;
4233 wrel++;
4234 continue;
4236 else if (r_type == R_X86_64_GOTPC32_TLSDESC
4237 || r_type == R_X86_64_CODE_4_GOTPC32_TLSDESC)
4239 /* GDesc -> IE transition.
4240 It's originally something like:
4241 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
4242 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
4244 Change it to:
4245 # before xchg %ax,%ax in LP64 mode.
4246 movq x@gottpoff(%rip), %rax
4247 # before nopl (%rax) in X32 mode.
4248 rex movl x@gottpoff(%rip), %eax
4251 /* Now modify the instruction as appropriate. To
4252 turn a lea into a mov in the form we use it, it
4253 suffices to change the second byte from 0x8d to
4254 0x8b. */
4255 if (roff < 2)
4256 goto corrupt_input;
4257 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
4259 bfd_put_32 (output_bfd,
4260 htab->elf.sgot->output_section->vma
4261 + htab->elf.sgot->output_offset + off
4262 - rel->r_offset
4263 - input_section->output_section->vma
4264 - input_section->output_offset
4265 - 4,
4266 contents + roff);
4267 continue;
4269 else if (r_type == R_X86_64_TLSDESC_CALL)
4271 /* GDesc -> IE transition.
4272 It's originally:
4273 call *(%rax) <--- LP64 mode.
4274 call *(%eax) <--- X32 mode.
4276 Change it to:
4277 xchg %ax, %ax <-- LP64 mode.
4278 nopl (%rax) <-- X32 mode.
4281 unsigned int prefix = 0;
4282 if (!ABI_64_P (input_bfd))
4284 /* Check for call *x@tlsdesc(%eax). */
4285 if (contents[roff] == 0x67)
4286 prefix = 1;
4288 if (prefix)
4290 bfd_put_8 (output_bfd, 0x0f, contents + roff);
4291 bfd_put_8 (output_bfd, 0x1f, contents + roff + 1);
4292 bfd_put_8 (output_bfd, 0x00, contents + roff + 2);
4294 else
4296 bfd_put_8 (output_bfd, 0x66, contents + roff);
4297 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4299 continue;
4301 else
4302 BFD_ASSERT (false);
4304 break;
4306 case R_X86_64_TLSLD:
4307 if (! elf_x86_64_tls_transition (info, input_bfd,
4308 input_section, contents,
4309 symtab_hdr, sym_hashes,
4310 &r_type, GOT_UNKNOWN, rel,
4311 relend, h, r_symndx, true))
4312 return false;
4314 if (r_type != R_X86_64_TLSLD)
4316 /* LD->LE transition:
4317 leaq foo@tlsld(%rip), %rdi
4318 call __tls_get_addr@PLT
4319 For 64bit, we change it into:
4320 .word 0x6666; .byte 0x66; movq %fs:0, %rax
4321 For 32bit, we change it into:
4322 nopl 0x0(%rax); movl %fs:0, %eax
4324 leaq foo@tlsld(%rip), %rdi;
4325 call *__tls_get_addr@GOTPCREL(%rip)
4326 which may be converted to
4327 addr32 call __tls_get_addr
4328 For 64bit, we change it into:
4329 .word 0x6666; .word 0x6666; movq %fs:0, %rax
4330 For 32bit, we change it into:
4331 nopw 0x0(%rax); movl %fs:0, %eax
4332 For largepic, change:
4333 leaq foo@tlsgd(%rip), %rdi
4334 movabsq $__tls_get_addr@pltoff, %rax
4335 addq %rbx, %rax
4336 call *%rax
4337 into
4338 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
4339 movq %fs:0, %eax */
4341 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
4342 if (ABI_64_P (output_bfd))
4344 if ((rel->r_offset + 5) >= input_section->size)
4345 goto corrupt_input;
4346 if (contents[rel->r_offset + 5] == 0xb8)
4348 if (rel->r_offset < 3
4349 || (rel->r_offset - 3 + 22) > input_section->size)
4350 goto corrupt_input;
4351 memcpy (contents + rel->r_offset - 3,
4352 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
4353 "\x64\x48\x8b\x04\x25\0\0\0", 22);
4355 else if (contents[rel->r_offset + 4] == 0xff
4356 || contents[rel->r_offset + 4] == 0x67)
4358 if (rel->r_offset < 3
4359 || (rel->r_offset - 3 + 13) > input_section->size)
4360 goto corrupt_input;
4361 memcpy (contents + rel->r_offset - 3,
4362 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
4363 13);
4366 else
4368 if (rel->r_offset < 3
4369 || (rel->r_offset - 3 + 12) > input_section->size)
4370 goto corrupt_input;
4371 memcpy (contents + rel->r_offset - 3,
4372 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
4375 else
4377 if ((rel->r_offset + 4) >= input_section->size)
4378 goto corrupt_input;
4379 if (contents[rel->r_offset + 4] == 0xff)
4381 if (rel->r_offset < 3
4382 || (rel->r_offset - 3 + 13) > input_section->size)
4383 goto corrupt_input;
4384 memcpy (contents + rel->r_offset - 3,
4385 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
4386 13);
4388 else
4390 if (rel->r_offset < 3
4391 || (rel->r_offset - 3 + 12) > input_section->size)
4392 goto corrupt_input;
4393 memcpy (contents + rel->r_offset - 3,
4394 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
4397 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
4398 and R_X86_64_PLTOFF64. */
4399 rel++;
4400 wrel++;
4401 continue;
4404 if (htab->elf.sgot == NULL)
4405 abort ();
4407 off = htab->tls_ld_or_ldm_got.offset;
4408 if (off & 1)
4409 off &= ~1;
4410 else
4412 Elf_Internal_Rela outrel;
4414 if (htab->elf.srelgot == NULL)
4415 abort ();
4417 outrel.r_offset = (htab->elf.sgot->output_section->vma
4418 + htab->elf.sgot->output_offset + off);
4420 bfd_put_64 (output_bfd, 0,
4421 htab->elf.sgot->contents + off);
4422 bfd_put_64 (output_bfd, 0,
4423 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4424 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
4425 outrel.r_addend = 0;
4426 elf_append_rela (output_bfd, htab->elf.srelgot,
4427 &outrel);
4428 htab->tls_ld_or_ldm_got.offset |= 1;
4430 relocation = htab->elf.sgot->output_section->vma
4431 + htab->elf.sgot->output_offset + off;
4432 unresolved_reloc = false;
4433 break;
4435 case R_X86_64_DTPOFF32:
4436 if (!bfd_link_executable (info)
4437 || (input_section->flags & SEC_CODE) == 0)
4438 relocation -= _bfd_x86_elf_dtpoff_base (info);
4439 else
4440 relocation = elf_x86_64_tpoff (info, relocation);
4441 break;
4443 case R_X86_64_TPOFF32:
4444 case R_X86_64_TPOFF64:
4445 BFD_ASSERT (bfd_link_executable (info));
4446 relocation = elf_x86_64_tpoff (info, relocation);
4447 break;
4449 case R_X86_64_DTPOFF64:
4450 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
4451 relocation -= _bfd_x86_elf_dtpoff_base (info);
4452 break;
4454 default:
4455 break;
4458 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4459 because such sections are not SEC_ALLOC and thus ld.so will
4460 not process them. */
4461 if (unresolved_reloc
4462 && !((input_section->flags & SEC_DEBUGGING) != 0
4463 && h->def_dynamic)
4464 && _bfd_elf_section_offset (output_bfd, info, input_section,
4465 rel->r_offset) != (bfd_vma) -1)
4467 switch (r_type)
4469 case R_X86_64_32S:
4470 sec = h->root.u.def.section;
4471 if ((info->nocopyreloc || eh->def_protected)
4472 && !(h->root.u.def.section->flags & SEC_CODE))
4473 return elf_x86_64_need_pic (info, input_bfd, input_section,
4474 h, NULL, NULL, howto);
4475 /* Fall through. */
4477 default:
4478 _bfd_error_handler
4479 /* xgettext:c-format */
4480 (_("%pB(%pA+%#" PRIx64 "): "
4481 "unresolvable %s relocation against symbol `%s'"),
4482 input_bfd,
4483 input_section,
4484 (uint64_t) rel->r_offset,
4485 howto->name,
4486 h->root.root.string);
4487 return false;
4491 do_relocation:
4492 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4493 contents, rel->r_offset,
4494 relocation, rel->r_addend);
4496 check_relocation_error:
4497 if (r != bfd_reloc_ok)
4499 const char *name;
4501 if (h != NULL)
4502 name = h->root.root.string;
4503 else
4505 name = bfd_elf_string_from_elf_section (input_bfd,
4506 symtab_hdr->sh_link,
4507 sym->st_name);
4508 if (name == NULL)
4509 return false;
4510 if (*name == '\0')
4511 name = bfd_section_name (sec);
4514 if (r == bfd_reloc_overflow)
4516 if (converted_reloc)
4518 info->callbacks->einfo
4519 ("%X%H:", input_bfd, input_section, rel->r_offset);
4520 info->callbacks->einfo
4521 (_(" failed to convert GOTPCREL relocation against "
4522 "'%s'; relink with --no-relax\n"),
4523 name);
4524 status = false;
4525 continue;
4527 (*info->callbacks->reloc_overflow)
4528 (info, (h ? &h->root : NULL), name, howto->name,
4529 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4531 else
4533 _bfd_error_handler
4534 /* xgettext:c-format */
4535 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
4536 input_bfd, input_section,
4537 (uint64_t) rel->r_offset, name, (int) r);
4538 return false;
4542 if (wrel != rel)
4543 *wrel = *rel;
4546 if (wrel != rel)
4548 Elf_Internal_Shdr *rel_hdr;
4549 size_t deleted = rel - wrel;
4551 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
4552 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4553 if (rel_hdr->sh_size == 0)
4555 /* It is too late to remove an empty reloc section. Leave
4556 one NONE reloc.
4557 ??? What is wrong with an empty section??? */
4558 rel_hdr->sh_size = rel_hdr->sh_entsize;
4559 deleted -= 1;
4561 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
4562 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4563 input_section->reloc_count -= deleted;
4566 return status;
4569 /* Finish up dynamic symbol handling. We set the contents of various
4570 dynamic sections here. */
4572 static bool
4573 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
4574 struct bfd_link_info *info,
4575 struct elf_link_hash_entry *h,
4576 Elf_Internal_Sym *sym)
4578 struct elf_x86_link_hash_table *htab;
4579 bool use_plt_second;
4580 struct elf_x86_link_hash_entry *eh;
4581 bool local_undefweak;
4583 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4585 /* Use the second PLT section only if there is .plt section. */
4586 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
4588 eh = (struct elf_x86_link_hash_entry *) h;
4589 if (eh->no_finish_dynamic_symbol)
4590 abort ();
4592 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
4593 resolved undefined weak symbols in executable so that their
4594 references have value 0 at run-time. */
4595 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
4597 if (h->plt.offset != (bfd_vma) -1)
4599 bfd_vma plt_index;
4600 bfd_vma got_offset, plt_offset;
4601 Elf_Internal_Rela rela;
4602 bfd_byte *loc;
4603 asection *plt, *gotplt, *relplt, *resolved_plt;
4604 const struct elf_backend_data *bed;
4605 bfd_vma plt_got_pcrel_offset;
4607 /* When building a static executable, use .iplt, .igot.plt and
4608 .rela.iplt sections for STT_GNU_IFUNC symbols. */
4609 if (htab->elf.splt != NULL)
4611 plt = htab->elf.splt;
4612 gotplt = htab->elf.sgotplt;
4613 relplt = htab->elf.srelplt;
4615 else
4617 plt = htab->elf.iplt;
4618 gotplt = htab->elf.igotplt;
4619 relplt = htab->elf.irelplt;
4622 VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
4624 /* Get the index in the procedure linkage table which
4625 corresponds to this symbol. This is the index of this symbol
4626 in all the symbols for which we are making plt entries. The
4627 first entry in the procedure linkage table is reserved.
4629 Get the offset into the .got table of the entry that
4630 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
4631 bytes. The first three are reserved for the dynamic linker.
4633 For static executables, we don't reserve anything. */
4635 if (plt == htab->elf.splt)
4637 got_offset = (h->plt.offset / htab->plt.plt_entry_size
4638 - htab->plt.has_plt0);
4639 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
4641 else
4643 got_offset = h->plt.offset / htab->plt.plt_entry_size;
4644 got_offset = got_offset * GOT_ENTRY_SIZE;
4647 /* Fill in the entry in the procedure linkage table. */
4648 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
4649 htab->plt.plt_entry_size);
4650 if (use_plt_second)
4652 memcpy (htab->plt_second->contents + eh->plt_second.offset,
4653 htab->non_lazy_plt->plt_entry,
4654 htab->non_lazy_plt->plt_entry_size);
4656 resolved_plt = htab->plt_second;
4657 plt_offset = eh->plt_second.offset;
4659 else
4661 resolved_plt = plt;
4662 plt_offset = h->plt.offset;
4665 /* Insert the relocation positions of the plt section. */
4667 /* Put offset the PC-relative instruction referring to the GOT entry,
4668 subtracting the size of that instruction. */
4669 plt_got_pcrel_offset = (gotplt->output_section->vma
4670 + gotplt->output_offset
4671 + got_offset
4672 - resolved_plt->output_section->vma
4673 - resolved_plt->output_offset
4674 - plt_offset
4675 - htab->plt.plt_got_insn_size);
4677 /* Check PC-relative offset overflow in PLT entry. */
4678 if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
4679 /* xgettext:c-format */
4680 info->callbacks->einfo (_("%F%pB: PC-relative offset overflow in PLT entry for `%s'\n"),
4681 output_bfd, h->root.root.string);
4683 bfd_put_32 (output_bfd, plt_got_pcrel_offset,
4684 (resolved_plt->contents + plt_offset
4685 + htab->plt.plt_got_offset));
4687 /* Fill in the entry in the global offset table, initially this
4688 points to the second part of the PLT entry. Leave the entry
4689 as zero for undefined weak symbol in PIE. No PLT relocation
4690 against undefined weak symbol in PIE. */
4691 if (!local_undefweak)
4693 if (htab->plt.has_plt0)
4694 bfd_put_64 (output_bfd, (plt->output_section->vma
4695 + plt->output_offset
4696 + h->plt.offset
4697 + htab->lazy_plt->plt_lazy_offset),
4698 gotplt->contents + got_offset);
4700 /* Fill in the entry in the .rela.plt section. */
4701 rela.r_offset = (gotplt->output_section->vma
4702 + gotplt->output_offset
4703 + got_offset);
4704 if (PLT_LOCAL_IFUNC_P (info, h))
4706 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
4707 h->root.root.string,
4708 h->root.u.def.section->owner);
4710 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4711 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
4712 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4713 rela.r_addend = (h->root.u.def.value
4714 + h->root.u.def.section->output_section->vma
4715 + h->root.u.def.section->output_offset);
4717 if (htab->params->report_relative_reloc)
4718 _bfd_x86_elf_link_report_relative_reloc
4719 (info, relplt, h, sym, "R_X86_64_IRELATIVE", &rela);
4721 /* R_X86_64_IRELATIVE comes last. */
4722 plt_index = htab->next_irelative_index--;
4724 else
4726 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
4727 if (htab->params->mark_plt)
4728 rela.r_addend = (resolved_plt->output_section->vma
4729 + plt_offset
4730 + htab->plt.plt_indirect_branch_offset);
4731 else
4732 rela.r_addend = 0;
4733 plt_index = htab->next_jump_slot_index++;
4736 /* Don't fill the second and third slots in PLT entry for
4737 static executables nor without PLT0. */
4738 if (plt == htab->elf.splt && htab->plt.has_plt0)
4740 bfd_vma plt0_offset
4741 = h->plt.offset + htab->lazy_plt->plt_plt_insn_end;
4743 /* Put relocation index. */
4744 bfd_put_32 (output_bfd, plt_index,
4745 (plt->contents + h->plt.offset
4746 + htab->lazy_plt->plt_reloc_offset));
4748 /* Put offset for jmp .PLT0 and check for overflow. We don't
4749 check relocation index for overflow since branch displacement
4750 will overflow first. */
4751 if (plt0_offset > 0x80000000)
4752 /* xgettext:c-format */
4753 info->callbacks->einfo (_("%F%pB: branch displacement overflow in PLT entry for `%s'\n"),
4754 output_bfd, h->root.root.string);
4755 bfd_put_32 (output_bfd, - plt0_offset,
4756 (plt->contents + h->plt.offset
4757 + htab->lazy_plt->plt_plt_offset));
4760 bed = get_elf_backend_data (output_bfd);
4761 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
4762 bed->s->swap_reloca_out (output_bfd, &rela, loc);
4765 else if (eh->plt_got.offset != (bfd_vma) -1)
4767 bfd_vma got_offset, plt_offset;
4768 asection *plt, *got;
4769 bool got_after_plt;
4770 int32_t got_pcrel_offset;
4772 /* Set the entry in the GOT procedure linkage table. */
4773 plt = htab->plt_got;
4774 got = htab->elf.sgot;
4775 got_offset = h->got.offset;
4777 if (got_offset == (bfd_vma) -1
4778 || (h->type == STT_GNU_IFUNC && h->def_regular)
4779 || plt == NULL
4780 || got == NULL)
4781 abort ();
4783 /* Use the non-lazy PLT entry template for the GOT PLT since they
4784 are the identical. */
4785 /* Fill in the entry in the GOT procedure linkage table. */
4786 plt_offset = eh->plt_got.offset;
4787 memcpy (plt->contents + plt_offset,
4788 htab->non_lazy_plt->plt_entry,
4789 htab->non_lazy_plt->plt_entry_size);
4791 /* Put offset the PC-relative instruction referring to the GOT
4792 entry, subtracting the size of that instruction. */
4793 got_pcrel_offset = (got->output_section->vma
4794 + got->output_offset
4795 + got_offset
4796 - plt->output_section->vma
4797 - plt->output_offset
4798 - plt_offset
4799 - htab->non_lazy_plt->plt_got_insn_size);
4801 /* Check PC-relative offset overflow in GOT PLT entry. */
4802 got_after_plt = got->output_section->vma > plt->output_section->vma;
4803 if ((got_after_plt && got_pcrel_offset < 0)
4804 || (!got_after_plt && got_pcrel_offset > 0))
4805 /* xgettext:c-format */
4806 info->callbacks->einfo (_("%F%pB: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
4807 output_bfd, h->root.root.string);
4809 bfd_put_32 (output_bfd, got_pcrel_offset,
4810 (plt->contents + plt_offset
4811 + htab->non_lazy_plt->plt_got_offset));
4814 if (!local_undefweak
4815 && !h->def_regular
4816 && (h->plt.offset != (bfd_vma) -1
4817 || eh->plt_got.offset != (bfd_vma) -1))
4819 /* Mark the symbol as undefined, rather than as defined in
4820 the .plt section. Leave the value if there were any
4821 relocations where pointer equality matters (this is a clue
4822 for the dynamic linker, to make function pointer
4823 comparisons work between an application and shared
4824 library), otherwise set it to zero. If a function is only
4825 called from a binary, there is no need to slow down
4826 shared libraries because of that. */
4827 sym->st_shndx = SHN_UNDEF;
4828 if (!h->pointer_equality_needed)
4829 sym->st_value = 0;
4832 _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
4834 /* Don't generate dynamic GOT relocation against undefined weak
4835 symbol in executable. */
4836 if (h->got.offset != (bfd_vma) -1
4837 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
4838 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
4839 && !local_undefweak)
4841 Elf_Internal_Rela rela;
4842 asection *relgot = htab->elf.srelgot;
4843 const char *relative_reloc_name = NULL;
4844 bool generate_dynamic_reloc = true;
4846 /* This symbol has an entry in the global offset table. Set it
4847 up. */
4848 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4849 abort ();
4851 rela.r_offset = (htab->elf.sgot->output_section->vma
4852 + htab->elf.sgot->output_offset
4853 + (h->got.offset &~ (bfd_vma) 1));
4855 /* If this is a static link, or it is a -Bsymbolic link and the
4856 symbol is defined locally or was forced to be local because
4857 of a version file, we just want to emit a RELATIVE reloc.
4858 The entry in the global offset table will already have been
4859 initialized in the relocate_section function. */
4860 if (h->def_regular
4861 && h->type == STT_GNU_IFUNC)
4863 if (h->plt.offset == (bfd_vma) -1)
4865 /* STT_GNU_IFUNC is referenced without PLT. */
4866 if (htab->elf.splt == NULL)
4868 /* use .rel[a].iplt section to store .got relocations
4869 in static executable. */
4870 relgot = htab->elf.irelplt;
4872 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
4874 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
4875 h->root.root.string,
4876 h->root.u.def.section->owner);
4878 rela.r_info = htab->r_info (0,
4879 R_X86_64_IRELATIVE);
4880 rela.r_addend = (h->root.u.def.value
4881 + h->root.u.def.section->output_section->vma
4882 + h->root.u.def.section->output_offset);
4883 relative_reloc_name = "R_X86_64_IRELATIVE";
4885 else
4886 goto do_glob_dat;
4888 else if (bfd_link_pic (info))
4890 /* Generate R_X86_64_GLOB_DAT. */
4891 goto do_glob_dat;
4893 else
4895 asection *plt;
4896 bfd_vma plt_offset;
4898 if (!h->pointer_equality_needed)
4899 abort ();
4901 /* For non-shared object, we can't use .got.plt, which
4902 contains the real function addres if we need pointer
4903 equality. We load the GOT entry with the PLT entry. */
4904 if (htab->plt_second != NULL)
4906 plt = htab->plt_second;
4907 plt_offset = eh->plt_second.offset;
4909 else
4911 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4912 plt_offset = h->plt.offset;
4914 bfd_put_64 (output_bfd, (plt->output_section->vma
4915 + plt->output_offset
4916 + plt_offset),
4917 htab->elf.sgot->contents + h->got.offset);
4918 return true;
4921 else if (bfd_link_pic (info)
4922 && SYMBOL_REFERENCES_LOCAL_P (info, h))
4924 if (!SYMBOL_DEFINED_NON_SHARED_P (h))
4925 return false;
4926 BFD_ASSERT((h->got.offset & 1) != 0);
4927 if (info->enable_dt_relr)
4928 generate_dynamic_reloc = false;
4929 else
4931 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4932 rela.r_addend = (h->root.u.def.value
4933 + h->root.u.def.section->output_section->vma
4934 + h->root.u.def.section->output_offset);
4935 relative_reloc_name = "R_X86_64_RELATIVE";
4938 else
4940 BFD_ASSERT((h->got.offset & 1) == 0);
4941 do_glob_dat:
4942 bfd_put_64 (output_bfd, (bfd_vma) 0,
4943 htab->elf.sgot->contents + h->got.offset);
4944 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
4945 rela.r_addend = 0;
4948 if (generate_dynamic_reloc)
4950 if (relative_reloc_name != NULL
4951 && htab->params->report_relative_reloc)
4952 _bfd_x86_elf_link_report_relative_reloc
4953 (info, relgot, h, sym, relative_reloc_name, &rela);
4955 elf_append_rela (output_bfd, relgot, &rela);
4959 if (h->needs_copy)
4961 Elf_Internal_Rela rela;
4962 asection *s;
4964 /* This symbol needs a copy reloc. Set it up. */
4965 VERIFY_COPY_RELOC (h, htab)
4967 rela.r_offset = (h->root.u.def.value
4968 + h->root.u.def.section->output_section->vma
4969 + h->root.u.def.section->output_offset);
4970 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
4971 rela.r_addend = 0;
4972 if (h->root.u.def.section == htab->elf.sdynrelro)
4973 s = htab->elf.sreldynrelro;
4974 else
4975 s = htab->elf.srelbss;
4976 elf_append_rela (output_bfd, s, &rela);
4979 return true;
4982 /* Finish up local dynamic symbol handling. We set the contents of
4983 various dynamic sections here. */
4985 static int
4986 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
4988 struct elf_link_hash_entry *h
4989 = (struct elf_link_hash_entry *) *slot;
4990 struct bfd_link_info *info
4991 = (struct bfd_link_info *) inf;
4993 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
4994 info, h, NULL);
4997 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4998 here since undefined weak symbol may not be dynamic and may not be
4999 called for elf_x86_64_finish_dynamic_symbol. */
5001 static bool
5002 elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
5003 void *inf)
5005 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
5006 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5008 if (h->root.type != bfd_link_hash_undefweak
5009 || h->dynindx != -1)
5010 return true;
5012 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5013 info, h, NULL);
5016 /* Used to decide how to sort relocs in an optimal manner for the
5017 dynamic linker, before writing them out. */
5019 static enum elf_reloc_type_class
5020 elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
5021 const asection *rel_sec ATTRIBUTE_UNUSED,
5022 const Elf_Internal_Rela *rela)
5024 bfd *abfd = info->output_bfd;
5025 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5026 struct elf_x86_link_hash_table *htab
5027 = elf_x86_hash_table (info, X86_64_ELF_DATA);
5029 if (htab->elf.dynsym != NULL
5030 && htab->elf.dynsym->contents != NULL)
5032 /* Check relocation against STT_GNU_IFUNC symbol if there are
5033 dynamic symbols. */
5034 unsigned long r_symndx = htab->r_sym (rela->r_info);
5035 if (r_symndx != STN_UNDEF)
5037 Elf_Internal_Sym sym;
5038 if (!bed->s->swap_symbol_in (abfd,
5039 (htab->elf.dynsym->contents
5040 + r_symndx * bed->s->sizeof_sym),
5041 0, &sym))
5042 abort ();
5044 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
5045 return reloc_class_ifunc;
5049 switch ((int) ELF32_R_TYPE (rela->r_info))
5051 case R_X86_64_IRELATIVE:
5052 return reloc_class_ifunc;
5053 case R_X86_64_RELATIVE:
5054 case R_X86_64_RELATIVE64:
5055 return reloc_class_relative;
5056 case R_X86_64_JUMP_SLOT:
5057 return reloc_class_plt;
5058 case R_X86_64_COPY:
5059 return reloc_class_copy;
5060 default:
5061 return reloc_class_normal;
5065 /* Finish up the dynamic sections. */
5067 static bool
5068 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
5069 struct bfd_link_info *info)
5071 struct elf_x86_link_hash_table *htab;
5073 htab = _bfd_x86_elf_finish_dynamic_sections (output_bfd, info);
5074 if (htab == NULL)
5075 return false;
5077 if (! htab->elf.dynamic_sections_created)
5078 return true;
5080 if (htab->elf.splt && htab->elf.splt->size > 0)
5082 if (bfd_is_abs_section (htab->elf.splt->output_section))
5084 info->callbacks->einfo
5085 (_("%F%P: discarded output section: `%pA'\n"),
5086 htab->elf.splt);
5087 return false;
5090 elf_section_data (htab->elf.splt->output_section)
5091 ->this_hdr.sh_entsize = htab->plt.plt_entry_size;
5093 if (htab->plt.has_plt0)
5095 /* Fill in the special first entry in the procedure linkage
5096 table. */
5097 memcpy (htab->elf.splt->contents,
5098 htab->lazy_plt->plt0_entry,
5099 htab->lazy_plt->plt0_entry_size);
5100 /* Add offset for pushq GOT+8(%rip), since the instruction
5101 uses 6 bytes subtract this value. */
5102 bfd_put_32 (output_bfd,
5103 (htab->elf.sgotplt->output_section->vma
5104 + htab->elf.sgotplt->output_offset
5106 - htab->elf.splt->output_section->vma
5107 - htab->elf.splt->output_offset
5108 - 6),
5109 (htab->elf.splt->contents
5110 + htab->lazy_plt->plt0_got1_offset));
5111 /* Add offset for the PC-relative instruction accessing
5112 GOT+16, subtracting the offset to the end of that
5113 instruction. */
5114 bfd_put_32 (output_bfd,
5115 (htab->elf.sgotplt->output_section->vma
5116 + htab->elf.sgotplt->output_offset
5117 + 16
5118 - htab->elf.splt->output_section->vma
5119 - htab->elf.splt->output_offset
5120 - htab->lazy_plt->plt0_got2_insn_end),
5121 (htab->elf.splt->contents
5122 + htab->lazy_plt->plt0_got2_offset));
5125 if (htab->elf.tlsdesc_plt)
5127 bfd_put_64 (output_bfd, (bfd_vma) 0,
5128 htab->elf.sgot->contents + htab->elf.tlsdesc_got);
5130 memcpy (htab->elf.splt->contents + htab->elf.tlsdesc_plt,
5131 htab->lazy_plt->plt_tlsdesc_entry,
5132 htab->lazy_plt->plt_tlsdesc_entry_size);
5134 /* Add offset for pushq GOT+8(%rip), since ENDBR64 uses 4
5135 bytes and the instruction uses 6 bytes, subtract these
5136 values. */
5137 bfd_put_32 (output_bfd,
5138 (htab->elf.sgotplt->output_section->vma
5139 + htab->elf.sgotplt->output_offset
5141 - htab->elf.splt->output_section->vma
5142 - htab->elf.splt->output_offset
5143 - htab->elf.tlsdesc_plt
5144 - htab->lazy_plt->plt_tlsdesc_got1_insn_end),
5145 (htab->elf.splt->contents
5146 + htab->elf.tlsdesc_plt
5147 + htab->lazy_plt->plt_tlsdesc_got1_offset));
5148 /* Add offset for indirect branch via GOT+TDG, where TDG
5149 stands for htab->tlsdesc_got, subtracting the offset
5150 to the end of that instruction. */
5151 bfd_put_32 (output_bfd,
5152 (htab->elf.sgot->output_section->vma
5153 + htab->elf.sgot->output_offset
5154 + htab->elf.tlsdesc_got
5155 - htab->elf.splt->output_section->vma
5156 - htab->elf.splt->output_offset
5157 - htab->elf.tlsdesc_plt
5158 - htab->lazy_plt->plt_tlsdesc_got2_insn_end),
5159 (htab->elf.splt->contents
5160 + htab->elf.tlsdesc_plt
5161 + htab->lazy_plt->plt_tlsdesc_got2_offset));
5165 /* Fill PLT entries for undefined weak symbols in PIE. */
5166 if (bfd_link_pie (info))
5167 bfd_hash_traverse (&info->hash->table,
5168 elf_x86_64_pie_finish_undefweak_symbol,
5169 info);
5171 return true;
5174 /* Fill PLT/GOT entries and allocate dynamic relocations for local
5175 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
5176 It has to be done before elf_link_sort_relocs is called so that
5177 dynamic relocations are properly sorted. */
5179 static bool
5180 elf_x86_64_output_arch_local_syms
5181 (bfd *output_bfd ATTRIBUTE_UNUSED,
5182 struct bfd_link_info *info,
5183 void *flaginfo ATTRIBUTE_UNUSED,
5184 int (*func) (void *, const char *,
5185 Elf_Internal_Sym *,
5186 asection *,
5187 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
5189 struct elf_x86_link_hash_table *htab
5190 = elf_x86_hash_table (info, X86_64_ELF_DATA);
5191 if (htab == NULL)
5192 return false;
5194 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
5195 htab_traverse (htab->loc_hash_table,
5196 elf_x86_64_finish_local_dynamic_symbol,
5197 info);
5199 return true;
5202 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
5203 dynamic relocations. */
5205 static long
5206 elf_x86_64_get_synthetic_symtab (bfd *abfd,
5207 long symcount ATTRIBUTE_UNUSED,
5208 asymbol **syms ATTRIBUTE_UNUSED,
5209 long dynsymcount,
5210 asymbol **dynsyms,
5211 asymbol **ret)
5213 long count, i, n;
5214 int j;
5215 bfd_byte *plt_contents;
5216 long relsize;
5217 const struct elf_x86_lazy_plt_layout *lazy_plt;
5218 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
5219 const struct elf_x86_lazy_plt_layout *lazy_bnd_plt;
5220 const struct elf_x86_non_lazy_plt_layout *non_lazy_bnd_plt;
5221 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
5222 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
5223 const struct elf_x86_lazy_plt_layout *x32_lazy_ibt_plt;
5224 const struct elf_x86_non_lazy_plt_layout *x32_non_lazy_ibt_plt;
5225 asection *plt;
5226 enum elf_x86_plt_type plt_type;
5227 struct elf_x86_plt plts[] =
5229 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
5230 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
5231 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
5232 { ".plt.bnd", NULL, NULL, plt_second, 0, 0, 0, 0 },
5233 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
5236 *ret = NULL;
5238 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
5239 return 0;
5241 if (dynsymcount <= 0)
5242 return 0;
5244 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
5245 if (relsize <= 0)
5246 return -1;
5248 lazy_plt = &elf_x86_64_lazy_plt;
5249 non_lazy_plt = &elf_x86_64_non_lazy_plt;
5250 lazy_bnd_plt = &elf_x86_64_lazy_bnd_plt;
5251 non_lazy_bnd_plt = &elf_x86_64_non_lazy_bnd_plt;
5252 if (ABI_64_P (abfd))
5254 lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
5255 non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
5256 x32_lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
5257 x32_non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
5259 else
5261 lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
5262 non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
5263 x32_lazy_ibt_plt = NULL;
5264 x32_non_lazy_ibt_plt = NULL;
5267 count = 0;
5268 for (j = 0; plts[j].name != NULL; j++)
5270 plt = bfd_get_section_by_name (abfd, plts[j].name);
5271 if (plt == NULL
5272 || plt->size == 0
5273 || (plt->flags & SEC_HAS_CONTENTS) == 0)
5274 continue;
5276 /* Get the PLT section contents. */
5277 if (!bfd_malloc_and_get_section (abfd, plt, &plt_contents))
5278 break;
5280 /* Check what kind of PLT it is. */
5281 plt_type = plt_unknown;
5282 if (plts[j].type == plt_unknown
5283 && (plt->size >= (lazy_plt->plt_entry_size
5284 + lazy_plt->plt_entry_size)))
5286 /* Match lazy PLT first. Need to check the first two
5287 instructions. */
5288 if ((memcmp (plt_contents, lazy_plt->plt0_entry,
5289 lazy_plt->plt0_got1_offset) == 0)
5290 && (memcmp (plt_contents + 6, lazy_plt->plt0_entry + 6,
5291 2) == 0))
5293 if (x32_lazy_ibt_plt != NULL
5294 && (memcmp (plt_contents
5295 + x32_lazy_ibt_plt->plt_entry_size,
5296 x32_lazy_ibt_plt->plt_entry,
5297 x32_lazy_ibt_plt->plt_got_offset) == 0))
5299 /* The fist entry in the x32 lazy IBT PLT is the same
5300 as the lazy PLT. */
5301 plt_type = plt_lazy | plt_second;
5302 lazy_plt = x32_lazy_ibt_plt;
5304 else
5305 plt_type = plt_lazy;
5307 else if (lazy_bnd_plt != NULL
5308 && (memcmp (plt_contents, lazy_bnd_plt->plt0_entry,
5309 lazy_bnd_plt->plt0_got1_offset) == 0)
5310 && (memcmp (plt_contents + 6,
5311 lazy_bnd_plt->plt0_entry + 6, 3) == 0))
5313 plt_type = plt_lazy | plt_second;
5314 /* The fist entry in the lazy IBT PLT is the same as the
5315 lazy BND PLT. */
5316 if ((memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
5317 lazy_ibt_plt->plt_entry,
5318 lazy_ibt_plt->plt_got_offset) == 0))
5319 lazy_plt = lazy_ibt_plt;
5320 else
5321 lazy_plt = lazy_bnd_plt;
5325 if (non_lazy_plt != NULL
5326 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
5327 && plt->size >= non_lazy_plt->plt_entry_size)
5329 /* Match non-lazy PLT. */
5330 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
5331 non_lazy_plt->plt_got_offset) == 0)
5332 plt_type = plt_non_lazy;
5335 if (plt_type == plt_unknown || plt_type == plt_second)
5337 if (non_lazy_bnd_plt != NULL
5338 && plt->size >= non_lazy_bnd_plt->plt_entry_size
5339 && (memcmp (plt_contents, non_lazy_bnd_plt->plt_entry,
5340 non_lazy_bnd_plt->plt_got_offset) == 0))
5342 /* Match BND PLT. */
5343 plt_type = plt_second;
5344 non_lazy_plt = non_lazy_bnd_plt;
5346 else if (non_lazy_ibt_plt != NULL
5347 && plt->size >= non_lazy_ibt_plt->plt_entry_size
5348 && (memcmp (plt_contents,
5349 non_lazy_ibt_plt->plt_entry,
5350 non_lazy_ibt_plt->plt_got_offset) == 0))
5352 /* Match IBT PLT. */
5353 plt_type = plt_second;
5354 non_lazy_plt = non_lazy_ibt_plt;
5356 else if (x32_non_lazy_ibt_plt != NULL
5357 && plt->size >= x32_non_lazy_ibt_plt->plt_entry_size
5358 && (memcmp (plt_contents,
5359 x32_non_lazy_ibt_plt->plt_entry,
5360 x32_non_lazy_ibt_plt->plt_got_offset) == 0))
5362 /* Match x32 IBT PLT. */
5363 plt_type = plt_second;
5364 non_lazy_plt = x32_non_lazy_ibt_plt;
5368 if (plt_type == plt_unknown)
5370 free (plt_contents);
5371 continue;
5374 plts[j].sec = plt;
5375 plts[j].type = plt_type;
5377 if ((plt_type & plt_lazy))
5379 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
5380 plts[j].plt_got_insn_size = lazy_plt->plt_got_insn_size;
5381 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
5382 /* Skip PLT0 in lazy PLT. */
5383 i = 1;
5385 else
5387 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
5388 plts[j].plt_got_insn_size = non_lazy_plt->plt_got_insn_size;
5389 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
5390 i = 0;
5393 /* Skip lazy PLT when the second PLT is used. */
5394 if (plt_type == (plt_lazy | plt_second))
5395 plts[j].count = 0;
5396 else
5398 n = plt->size / plts[j].plt_entry_size;
5399 plts[j].count = n;
5400 count += n - i;
5403 plts[j].contents = plt_contents;
5406 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
5407 (bfd_vma) 0, plts, dynsyms,
5408 ret);
5411 /* Handle an x86-64 specific section when reading an object file. This
5412 is called when elfcode.h finds a section with an unknown type. */
5414 static bool
5415 elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
5416 const char *name, int shindex)
5418 if (hdr->sh_type != SHT_X86_64_UNWIND)
5419 return false;
5421 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5422 return false;
5424 return true;
5427 /* Hook called by the linker routine which adds symbols from an object
5428 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
5429 of .bss. */
5431 static bool
5432 elf_x86_64_add_symbol_hook (bfd *abfd,
5433 struct bfd_link_info *info ATTRIBUTE_UNUSED,
5434 Elf_Internal_Sym *sym,
5435 const char **namep ATTRIBUTE_UNUSED,
5436 flagword *flagsp ATTRIBUTE_UNUSED,
5437 asection **secp,
5438 bfd_vma *valp)
5440 asection *lcomm;
5442 switch (sym->st_shndx)
5444 case SHN_X86_64_LCOMMON:
5445 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
5446 if (lcomm == NULL)
5448 lcomm = bfd_make_section_with_flags (abfd,
5449 "LARGE_COMMON",
5450 (SEC_ALLOC
5451 | SEC_IS_COMMON
5452 | SEC_LINKER_CREATED));
5453 if (lcomm == NULL)
5454 return false;
5455 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
5457 *secp = lcomm;
5458 *valp = sym->st_size;
5459 return true;
5462 return true;
5466 /* Given a BFD section, try to locate the corresponding ELF section
5467 index. */
5469 static bool
5470 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5471 asection *sec, int *index_return)
5473 if (sec == &_bfd_elf_large_com_section)
5475 *index_return = SHN_X86_64_LCOMMON;
5476 return true;
5478 return false;
5481 /* Process a symbol. */
5483 static void
5484 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5485 asymbol *asym)
5487 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5489 switch (elfsym->internal_elf_sym.st_shndx)
5491 case SHN_X86_64_LCOMMON:
5492 asym->section = &_bfd_elf_large_com_section;
5493 asym->value = elfsym->internal_elf_sym.st_size;
5494 /* Common symbol doesn't set BSF_GLOBAL. */
5495 asym->flags &= ~BSF_GLOBAL;
5496 break;
5500 static bool
5501 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
5503 return (sym->st_shndx == SHN_COMMON
5504 || sym->st_shndx == SHN_X86_64_LCOMMON);
5507 static unsigned int
5508 elf_x86_64_common_section_index (asection *sec)
5510 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5511 return SHN_COMMON;
5512 else
5513 return SHN_X86_64_LCOMMON;
5516 static asection *
5517 elf_x86_64_common_section (asection *sec)
5519 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5520 return bfd_com_section_ptr;
5521 else
5522 return &_bfd_elf_large_com_section;
5525 static bool
5526 elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
5527 const Elf_Internal_Sym *sym,
5528 asection **psec,
5529 bool newdef,
5530 bool olddef,
5531 bfd *oldbfd,
5532 const asection *oldsec)
5534 /* A normal common symbol and a large common symbol result in a
5535 normal common symbol. We turn the large common symbol into a
5536 normal one. */
5537 if (!olddef
5538 && h->root.type == bfd_link_hash_common
5539 && !newdef
5540 && bfd_is_com_section (*psec)
5541 && oldsec != *psec)
5543 if (sym->st_shndx == SHN_COMMON
5544 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
5546 h->root.u.c.p->section
5547 = bfd_make_section_old_way (oldbfd, "COMMON");
5548 h->root.u.c.p->section->flags = SEC_ALLOC;
5550 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5551 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
5552 *psec = bfd_com_section_ptr;
5555 return true;
5558 static bool
5559 elf_x86_64_section_flags (const Elf_Internal_Shdr *hdr)
5561 if ((hdr->sh_flags & SHF_X86_64_LARGE) != 0)
5562 hdr->bfd_section->flags |= SEC_ELF_LARGE;
5564 return true;
5567 static bool
5568 elf_x86_64_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
5569 Elf_Internal_Shdr *hdr, asection *sec)
5571 if (sec->flags & SEC_ELF_LARGE)
5572 hdr->sh_flags |= SHF_X86_64_LARGE;
5574 return true;
5577 static bool
5578 elf_x86_64_copy_private_section_data (bfd *ibfd, asection *isec,
5579 bfd *obfd, asection *osec)
5581 if (!_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec))
5582 return false;
5584 /* objcopy --set-section-flags without "large" drops SHF_X86_64_LARGE. */
5585 if (ibfd != obfd)
5586 elf_section_flags (osec) &= ~SHF_X86_64_LARGE;
5588 return true;
5591 static int
5592 elf_x86_64_additional_program_headers (bfd *abfd,
5593 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5595 asection *s;
5596 int count = 0;
5598 /* Check to see if we need a large readonly segment. */
5599 s = bfd_get_section_by_name (abfd, ".lrodata");
5600 if (s && (s->flags & SEC_LOAD))
5601 count++;
5603 /* Check to see if we need a large data segment. Since .lbss sections
5604 is placed right after the .bss section, there should be no need for
5605 a large data segment just because of .lbss. */
5606 s = bfd_get_section_by_name (abfd, ".ldata");
5607 if (s && (s->flags & SEC_LOAD))
5608 count++;
5610 return count;
5613 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5615 static bool
5616 elf_x86_64_relocs_compatible (const bfd_target *input,
5617 const bfd_target *output)
5619 return ((xvec_get_elf_backend_data (input)->s->elfclass
5620 == xvec_get_elf_backend_data (output)->s->elfclass)
5621 && _bfd_elf_relocs_compatible (input, output));
5624 /* Set up x86-64 GNU properties. Return the first relocatable ELF input
5625 with GNU properties if found. Otherwise, return NULL. */
5627 static bfd *
5628 elf_x86_64_link_setup_gnu_properties (struct bfd_link_info *info)
5630 struct elf_x86_init_table init_table;
5631 const struct elf_backend_data *bed;
5632 struct elf_x86_link_hash_table *htab;
5634 if ((int) R_X86_64_standard >= (int) R_X86_64_converted_reloc_bit
5635 || (int) R_X86_64_max <= (int) R_X86_64_converted_reloc_bit
5636 || ((int) (R_X86_64_GNU_VTINHERIT | R_X86_64_converted_reloc_bit)
5637 != (int) R_X86_64_GNU_VTINHERIT)
5638 || ((int) (R_X86_64_GNU_VTENTRY | R_X86_64_converted_reloc_bit)
5639 != (int) R_X86_64_GNU_VTENTRY))
5640 abort ();
5642 /* This is unused for x86-64. */
5643 init_table.plt0_pad_byte = 0x90;
5645 bed = get_elf_backend_data (info->output_bfd);
5646 htab = elf_x86_hash_table (info, bed->target_id);
5647 if (!htab)
5648 abort ();
5650 init_table.lazy_plt = &elf_x86_64_lazy_plt;
5651 init_table.non_lazy_plt = &elf_x86_64_non_lazy_plt;
5653 init_table.lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
5654 init_table.non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
5656 if (ABI_64_P (info->output_bfd))
5658 init_table.sframe_lazy_plt = &elf_x86_64_sframe_plt;
5659 init_table.sframe_non_lazy_plt = &elf_x86_64_sframe_non_lazy_plt;
5660 init_table.sframe_lazy_ibt_plt = &elf_x86_64_sframe_plt;
5661 init_table.sframe_non_lazy_ibt_plt = &elf_x86_64_sframe_non_lazy_plt;
5663 else
5665 /* SFrame is not supported for non AMD64. */
5666 init_table.sframe_lazy_plt = NULL;
5667 init_table.sframe_non_lazy_plt = NULL;
5670 if (ABI_64_P (info->output_bfd))
5672 init_table.r_info = elf64_r_info;
5673 init_table.r_sym = elf64_r_sym;
5675 else
5677 init_table.r_info = elf32_r_info;
5678 init_table.r_sym = elf32_r_sym;
5681 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
5684 static void
5685 elf_x86_64_add_glibc_version_dependency
5686 (struct elf_find_verdep_info *rinfo)
5688 unsigned int i = 0;
5689 const char *version[3] = { NULL, NULL, NULL };
5690 struct elf_x86_link_hash_table *htab;
5692 if (rinfo->info->enable_dt_relr)
5694 version[i] = "GLIBC_ABI_DT_RELR";
5695 i++;
5698 htab = elf_x86_hash_table (rinfo->info, X86_64_ELF_DATA);
5699 if (htab != NULL && htab->params->mark_plt)
5701 version[i] = "GLIBC_2.36";
5702 i++;
5705 if (i != 0)
5706 _bfd_elf_link_add_glibc_version_dependency (rinfo, version);
5709 static const struct bfd_elf_special_section
5710 elf_x86_64_special_sections[]=
5712 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5713 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5714 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
5715 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5716 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5717 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5718 { NULL, 0, 0, 0, 0 }
5721 #define TARGET_LITTLE_SYM x86_64_elf64_vec
5722 #define TARGET_LITTLE_NAME "elf64-x86-64"
5723 #define ELF_ARCH bfd_arch_i386
5724 #define ELF_TARGET_ID X86_64_ELF_DATA
5725 #define ELF_MACHINE_CODE EM_X86_64
5726 #define ELF_MAXPAGESIZE 0x1000
5727 #define ELF_COMMONPAGESIZE 0x1000
5729 #define elf_backend_can_gc_sections 1
5730 #define elf_backend_can_refcount 1
5731 #define elf_backend_want_got_plt 1
5732 #define elf_backend_plt_readonly 1
5733 #define elf_backend_want_plt_sym 0
5734 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
5735 #define elf_backend_rela_normal 1
5736 #define elf_backend_plt_alignment 4
5737 #define elf_backend_caches_rawsize 1
5738 #define elf_backend_dtrel_excludes_plt 1
5739 #define elf_backend_want_dynrelro 1
5741 #define elf_info_to_howto elf_x86_64_info_to_howto
5743 #define bfd_elf64_bfd_copy_private_section_data \
5744 elf_x86_64_copy_private_section_data
5745 #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
5746 #define bfd_elf64_bfd_reloc_name_lookup \
5747 elf_x86_64_reloc_name_lookup
5749 #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
5750 #define elf_backend_always_size_sections elf_x86_64_always_size_sections
5751 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
5752 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5753 #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
5754 #define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
5755 #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5756 #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
5757 #ifdef CORE_HEADER
5758 #define elf_backend_write_core_note elf_x86_64_write_core_note
5759 #endif
5760 #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5761 #define elf_backend_relocate_section elf_x86_64_relocate_section
5762 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5763 #define elf_backend_object_p elf64_x86_64_elf_object_p
5764 #define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
5766 #define elf_backend_section_from_shdr \
5767 elf_x86_64_section_from_shdr
5769 #define elf_backend_section_from_bfd_section \
5770 elf_x86_64_elf_section_from_bfd_section
5771 #define elf_backend_add_symbol_hook \
5772 elf_x86_64_add_symbol_hook
5773 #define elf_backend_symbol_processing \
5774 elf_x86_64_symbol_processing
5775 #define elf_backend_common_section_index \
5776 elf_x86_64_common_section_index
5777 #define elf_backend_common_section \
5778 elf_x86_64_common_section
5779 #define elf_backend_common_definition \
5780 elf_x86_64_common_definition
5781 #define elf_backend_merge_symbol \
5782 elf_x86_64_merge_symbol
5783 #define elf_backend_special_sections \
5784 elf_x86_64_special_sections
5785 #define elf_backend_section_flags elf_x86_64_section_flags
5786 #define elf_backend_fake_sections elf_x86_64_fake_sections
5787 #define elf_backend_additional_program_headers \
5788 elf_x86_64_additional_program_headers
5789 #define elf_backend_setup_gnu_properties \
5790 elf_x86_64_link_setup_gnu_properties
5791 #define elf_backend_hide_symbol \
5792 _bfd_x86_elf_hide_symbol
5793 #define elf_backend_add_glibc_version_dependency \
5794 elf_x86_64_add_glibc_version_dependency
5796 #undef elf64_bed
5797 #define elf64_bed elf64_x86_64_bed
5799 #include "elf64-target.h"
5801 /* CloudABI support. */
5803 #undef TARGET_LITTLE_SYM
5804 #define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
5805 #undef TARGET_LITTLE_NAME
5806 #define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
5808 #undef ELF_OSABI
5809 #define ELF_OSABI ELFOSABI_CLOUDABI
5811 #undef elf64_bed
5812 #define elf64_bed elf64_x86_64_cloudabi_bed
5814 #include "elf64-target.h"
5816 /* FreeBSD support. */
5818 #undef TARGET_LITTLE_SYM
5819 #define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
5820 #undef TARGET_LITTLE_NAME
5821 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5823 #undef ELF_OSABI
5824 #define ELF_OSABI ELFOSABI_FREEBSD
5826 #undef elf64_bed
5827 #define elf64_bed elf64_x86_64_fbsd_bed
5829 #include "elf64-target.h"
5831 /* Solaris 2 support. */
5833 #undef TARGET_LITTLE_SYM
5834 #define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
5835 #undef TARGET_LITTLE_NAME
5836 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5838 #undef ELF_TARGET_OS
5839 #define ELF_TARGET_OS is_solaris
5841 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5842 objects won't be recognized. */
5843 #undef ELF_OSABI
5845 #undef elf64_bed
5846 #define elf64_bed elf64_x86_64_sol2_bed
5848 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5849 boundary. */
5850 #undef elf_backend_static_tls_alignment
5851 #define elf_backend_static_tls_alignment 16
5853 /* The Solaris 2 ABI requires a plt symbol on all platforms.
5855 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5856 File, p.63. */
5857 #undef elf_backend_want_plt_sym
5858 #define elf_backend_want_plt_sym 1
5860 #undef elf_backend_strtab_flags
5861 #define elf_backend_strtab_flags SHF_STRINGS
5863 static bool
5864 elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
5865 bfd *obfd ATTRIBUTE_UNUSED,
5866 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
5867 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
5869 /* PR 19938: FIXME: Need to add code for setting the sh_info
5870 and sh_link fields of Solaris specific section types. */
5871 return false;
5874 #undef elf_backend_copy_special_section_fields
5875 #define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
5877 #include "elf64-target.h"
5879 /* Restore defaults. */
5880 #undef ELF_OSABI
5881 #undef elf_backend_static_tls_alignment
5882 #undef elf_backend_want_plt_sym
5883 #define elf_backend_want_plt_sym 0
5884 #undef elf_backend_strtab_flags
5885 #undef elf_backend_copy_special_section_fields
5887 /* 32bit x86-64 support. */
5889 #undef TARGET_LITTLE_SYM
5890 #define TARGET_LITTLE_SYM x86_64_elf32_vec
5891 #undef TARGET_LITTLE_NAME
5892 #define TARGET_LITTLE_NAME "elf32-x86-64"
5893 #undef elf32_bed
5894 #define elf32_bed elf32_x86_64_bed
5896 #undef ELF_ARCH
5897 #define ELF_ARCH bfd_arch_i386
5899 #undef ELF_MACHINE_CODE
5900 #define ELF_MACHINE_CODE EM_X86_64
5902 #undef ELF_TARGET_OS
5903 #undef ELF_OSABI
5905 #define bfd_elf32_bfd_copy_private_section_data \
5906 elf_x86_64_copy_private_section_data
5907 #define bfd_elf32_bfd_reloc_type_lookup \
5908 elf_x86_64_reloc_type_lookup
5909 #define bfd_elf32_bfd_reloc_name_lookup \
5910 elf_x86_64_reloc_name_lookup
5911 #define bfd_elf32_get_synthetic_symtab \
5912 elf_x86_64_get_synthetic_symtab
5914 #undef elf_backend_object_p
5915 #define elf_backend_object_p \
5916 elf32_x86_64_elf_object_p
5918 #undef elf_backend_bfd_from_remote_memory
5919 #define elf_backend_bfd_from_remote_memory \
5920 _bfd_elf32_bfd_from_remote_memory
5922 #undef elf_backend_size_info
5923 #define elf_backend_size_info \
5924 _bfd_elf32_size_info
5926 #include "elf32-target.h"