]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-x86-64.c
2.41 Release sources
[thirdparty/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
351f65ca 1/* X86-64 specific support for ELF
d87bef3a 2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
8d88c4ca
NC
3 Contributed by Jan Hubicka <jh@suse.cz>.
4
ae9a127f 5 This file is part of BFD, the Binary File Descriptor library.
8d88c4ca 6
ae9a127f
NC
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
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
ae9a127f 10 (at your option) any later version.
8d88c4ca 11
ae9a127f
NC
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.
8d88c4ca 16
ae9a127f
NC
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
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
8d88c4ca 21
0afcef53 22#include "elfxx-x86.h"
e41b3a13 23#include "dwarf2.h"
d7921315 24#include "libiberty.h"
cf0e0a0b 25#include "sframe.h"
8d88c4ca 26
56ceb5b5 27#include "opcode/i386.h"
8d88c4ca 28
8fd79e71
L
29#ifdef CORE_HEADER
30#include <stdarg.h>
31#include CORE_HEADER
32#endif
33
8d88c4ca
NC
34/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
35#define MINUS_ONE (~ (bfd_vma) 0)
36
351f65ca
L
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. */
41
8d88c4ca 42/* The relocation "howto" table. Order of fields:
7b81dfbb
AJ
43 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
44 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
70256ad8
AJ
45static reloc_howto_type x86_64_elf_howto_table[] =
46{
c94cb026 47 HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
17c6c3b9 48 bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0, 0x00000000,
0a1b45a2 49 false),
c94cb026 50 HOWTO(R_X86_64_64, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9 51 bfd_elf_generic_reloc, "R_X86_64_64", false, 0, MINUS_ONE,
0a1b45a2 52 false),
c94cb026 53 HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9 54 bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0, 0xffffffff,
0a1b45a2 55 true),
c94cb026 56 HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed,
17c6c3b9 57 bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0, 0xffffffff,
0a1b45a2 58 false),
c94cb026 59 HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9 60 bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0, 0xffffffff,
0a1b45a2 61 true),
c94cb026 62 HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield,
17c6c3b9 63 bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0, 0xffffffff,
0a1b45a2 64 false),
c94cb026 65 HOWTO(R_X86_64_GLOB_DAT, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9
JB
66 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, 0, MINUS_ONE,
67 false),
c94cb026 68 HOWTO(R_X86_64_JUMP_SLOT, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9
JB
69 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, 0, MINUS_ONE,
70 false),
c94cb026 71 HOWTO(R_X86_64_RELATIVE, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9
JB
72 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, 0, MINUS_ONE,
73 false),
c94cb026 74 HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9
JB
75 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0, 0xffffffff,
76 true),
c94cb026 77 HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned,
17c6c3b9 78 bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
0a1b45a2 79 false),
c94cb026 80 HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed,
17c6c3b9 81 bfd_elf_generic_reloc, "R_X86_64_32S", false, 0, 0xffffffff,
0a1b45a2 82 false),
c94cb026 83 HOWTO(R_X86_64_16, 0, 2, 16, false, 0, complain_overflow_bitfield,
17c6c3b9 84 bfd_elf_generic_reloc, "R_X86_64_16", false, 0, 0xffff, false),
c94cb026 85 HOWTO(R_X86_64_PC16, 0, 2, 16, true, 0, complain_overflow_bitfield,
17c6c3b9 86 bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0, 0xffff, true),
c94cb026 87 HOWTO(R_X86_64_8, 0, 1, 8, false, 0, complain_overflow_bitfield,
17c6c3b9 88 bfd_elf_generic_reloc, "R_X86_64_8", false, 0, 0xff, false),
c94cb026 89 HOWTO(R_X86_64_PC8, 0, 1, 8, true, 0, complain_overflow_signed,
17c6c3b9 90 bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0, 0xff, true),
c94cb026 91 HOWTO(R_X86_64_DTPMOD64, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9
JB
92 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", false, 0, MINUS_ONE,
93 false),
c94cb026 94 HOWTO(R_X86_64_DTPOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9
JB
95 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", false, 0, MINUS_ONE,
96 false),
c94cb026 97 HOWTO(R_X86_64_TPOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9
JB
98 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", false, 0, MINUS_ONE,
99 false),
c94cb026 100 HOWTO(R_X86_64_TLSGD, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9
JB
101 bfd_elf_generic_reloc, "R_X86_64_TLSGD", false, 0, 0xffffffff,
102 true),
c94cb026 103 HOWTO(R_X86_64_TLSLD, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9
JB
104 bfd_elf_generic_reloc, "R_X86_64_TLSLD", false, 0, 0xffffffff,
105 true),
c94cb026 106 HOWTO(R_X86_64_DTPOFF32, 0, 4, 32, false, 0, complain_overflow_signed,
17c6c3b9
JB
107 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", false, 0, 0xffffffff,
108 false),
c94cb026 109 HOWTO(R_X86_64_GOTTPOFF, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9
JB
110 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", false, 0, 0xffffffff,
111 true),
c94cb026 112 HOWTO(R_X86_64_TPOFF32, 0, 4, 32, false, 0, complain_overflow_signed,
17c6c3b9
JB
113 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", false, 0, 0xffffffff,
114 false),
c94cb026 115 HOWTO(R_X86_64_PC64, 0, 8, 64, true, 0, complain_overflow_dont,
17c6c3b9 116 bfd_elf_generic_reloc, "R_X86_64_PC64", false, 0, MINUS_ONE,
0a1b45a2 117 true),
c94cb026 118 HOWTO(R_X86_64_GOTOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9
JB
119 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64", false, 0, MINUS_ONE,
120 false),
c94cb026 121 HOWTO(R_X86_64_GOTPC32, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9
JB
122 bfd_elf_generic_reloc, "R_X86_64_GOTPC32", false, 0, 0xffffffff,
123 true),
c94cb026 124 HOWTO(R_X86_64_GOT64, 0, 8, 64, false, 0, complain_overflow_signed,
17c6c3b9 125 bfd_elf_generic_reloc, "R_X86_64_GOT64", false, 0, MINUS_ONE,
0a1b45a2 126 false),
c94cb026 127 HOWTO(R_X86_64_GOTPCREL64, 0, 8, 64, true, 0, complain_overflow_signed,
17c6c3b9
JB
128 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", false, 0, MINUS_ONE,
129 true),
c94cb026 130 HOWTO(R_X86_64_GOTPC64, 0, 8, 64, true, 0, complain_overflow_signed,
17c6c3b9
JB
131 bfd_elf_generic_reloc, "R_X86_64_GOTPC64", false, 0, MINUS_ONE,
132 true),
c94cb026 133 HOWTO(R_X86_64_GOTPLT64, 0, 8, 64, false, 0, complain_overflow_signed,
17c6c3b9
JB
134 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", false, 0, MINUS_ONE,
135 false),
c94cb026 136 HOWTO(R_X86_64_PLTOFF64, 0, 8, 64, false, 0, complain_overflow_signed,
17c6c3b9
JB
137 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", false, 0, MINUS_ONE,
138 false),
c94cb026 139 HOWTO(R_X86_64_SIZE32, 0, 4, 32, false, 0, complain_overflow_unsigned,
17c6c3b9 140 bfd_elf_generic_reloc, "R_X86_64_SIZE32", false, 0, 0xffffffff,
0a1b45a2 141 false),
c94cb026 142 HOWTO(R_X86_64_SIZE64, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9 143 bfd_elf_generic_reloc, "R_X86_64_SIZE64", false, 0, MINUS_ONE,
0a1b45a2 144 false),
c94cb026 145 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 4, 32, true, 0,
67a4f2b7 146 complain_overflow_bitfield, bfd_elf_generic_reloc,
17c6c3b9 147 "R_X86_64_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
c94cb026 148 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, false, 0,
67a4f2b7
AO
149 complain_overflow_dont, bfd_elf_generic_reloc,
150 "R_X86_64_TLSDESC_CALL",
0a1b45a2 151 false, 0, 0, false),
c94cb026 152 HOWTO(R_X86_64_TLSDESC, 0, 8, 64, false, 0,
4c398cc0 153 complain_overflow_dont, bfd_elf_generic_reloc,
17c6c3b9 154 "R_X86_64_TLSDESC", false, 0, MINUS_ONE, false),
c94cb026 155 HOWTO(R_X86_64_IRELATIVE, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9
JB
156 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", false, 0, MINUS_ONE,
157 false),
c94cb026 158 HOWTO(R_X86_64_RELATIVE64, 0, 8, 64, false, 0, complain_overflow_dont,
17c6c3b9
JB
159 bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", false, 0, MINUS_ONE,
160 false),
c94cb026 161 HOWTO(R_X86_64_PC32_BND, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9 162 bfd_elf_generic_reloc, "R_X86_64_PC32_BND", false, 0, 0xffffffff,
0a1b45a2 163 true),
c94cb026 164 HOWTO(R_X86_64_PLT32_BND, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9 165 bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", false, 0, 0xffffffff,
0a1b45a2 166 true),
c94cb026 167 HOWTO(R_X86_64_GOTPCRELX, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9
JB
168 bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", false, 0, 0xffffffff,
169 true),
c94cb026 170 HOWTO(R_X86_64_REX_GOTPCRELX, 0, 4, 32, true, 0, complain_overflow_signed,
17c6c3b9
JB
171 bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", false, 0, 0xffffffff,
172 true),
fe4770f4 173
a33d77bc
JB
174 /* We have a gap in the reloc numbers here.
175 R_X86_64_standard counts the number up to this point, and
176 R_X86_64_vt_offset is the value to subtract from a reloc type of
177 R_X86_64_GNU_VT* to form an index into this table. */
56ceb5b5 178#define R_X86_64_standard (R_X86_64_REX_GOTPCRELX + 1)
a33d77bc
JB
179#define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
180
fe4770f4 181/* GNU extension to record C++ vtable hierarchy. */
c94cb026 182 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 8, 0, false, 0, complain_overflow_dont,
0a1b45a2 183 NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
fe4770f4
AJ
184
185/* GNU extension to record C++ vtable member usage. */
c94cb026 186 HOWTO (R_X86_64_GNU_VTENTRY, 0, 8, 0, false, 0, complain_overflow_dont,
0a1b45a2
AM
187 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
188 false),
d7921315
L
189
190/* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
c94cb026 191 HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_bitfield,
17c6c3b9 192 bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
0a1b45a2 193 false)
8d88c4ca
NC
194};
195
196/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
197struct elf_reloc_map
198{
8d88c4ca
NC
199 bfd_reloc_code_real_type bfd_reloc_val;
200 unsigned char elf_reloc_val;
201};
202
dc810e39 203static const struct elf_reloc_map x86_64_reloc_map[] =
8d88c4ca 204{
70256ad8
AJ
205 { BFD_RELOC_NONE, R_X86_64_NONE, },
206 { BFD_RELOC_64, R_X86_64_64, },
207 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
208 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
209 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
210 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
211 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
212 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
213 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
214 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
215 { BFD_RELOC_32, R_X86_64_32, },
216 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
217 { BFD_RELOC_16, R_X86_64_16, },
218 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
219 { BFD_RELOC_8, R_X86_64_8, },
220 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
bffbf940
JJ
221 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
222 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
223 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
224 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
225 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
226 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
227 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
228 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
d6ab8113
JB
229 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
230 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
231 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
7b81dfbb
AJ
232 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
233 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
234 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
235 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
236 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
1788fc08
L
237 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
238 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
67a4f2b7
AO
239 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
240 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
241 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
cbe950e9 242 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
56ceb5b5
L
243 { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND, },
244 { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND, },
245 { BFD_RELOC_X86_64_GOTPCRELX, R_X86_64_GOTPCRELX, },
246 { BFD_RELOC_X86_64_REX_GOTPCRELX, R_X86_64_REX_GOTPCRELX, },
fe4770f4
AJ
247 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
248 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
249};
250
67a4f2b7 251static reloc_howto_type *
351f65ca 252elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
67a4f2b7
AO
253{
254 unsigned i;
255
d7921315
L
256 if (r_type == (unsigned int) R_X86_64_32)
257 {
258 if (ABI_64_P (abfd))
259 i = r_type;
260 else
261 i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
262 }
263 else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
264 || r_type >= (unsigned int) R_X86_64_max)
67a4f2b7
AO
265 {
266 if (r_type >= (unsigned int) R_X86_64_standard)
267 {
695344c0 268 /* xgettext:c-format */
0aa13fee
AM
269 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
270 abfd, r_type);
f3185997
NC
271 bfd_set_error (bfd_error_bad_value);
272 return NULL;
67a4f2b7
AO
273 }
274 i = r_type;
275 }
276 else
277 i = r_type - (unsigned int) R_X86_64_vt_offset;
278 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
279 return &x86_64_elf_howto_table[i];
280}
8d88c4ca
NC
281
282/* Given a BFD reloc type, return a HOWTO structure. */
283static reloc_howto_type *
351f65ca
L
284elf_x86_64_reloc_type_lookup (bfd *abfd,
285 bfd_reloc_code_real_type code)
8d88c4ca
NC
286{
287 unsigned int i;
27482721 288
8d88c4ca
NC
289 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
290 i++)
291 {
292 if (x86_64_reloc_map[i].bfd_reloc_val == code)
351f65ca
L
293 return elf_x86_64_rtype_to_howto (abfd,
294 x86_64_reloc_map[i].elf_reloc_val);
8d88c4ca 295 }
5860e3f8 296 return NULL;
8d88c4ca
NC
297}
298
157090f7 299static reloc_howto_type *
d7921315 300elf_x86_64_reloc_name_lookup (bfd *abfd,
351f65ca 301 const char *r_name)
157090f7
AM
302{
303 unsigned int i;
304
d7921315
L
305 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
306 {
307 /* Get x32 R_X86_64_32. */
308 reloc_howto_type *reloc
309 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
310 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
311 return reloc;
312 }
313
314 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
157090f7
AM
315 if (x86_64_elf_howto_table[i].name != NULL
316 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
317 return &x86_64_elf_howto_table[i];
318
319 return NULL;
320}
321
8d88c4ca 322/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 323
0a1b45a2 324static bool
f3185997 325elf_x86_64_info_to_howto (bfd *abfd, arelent *cache_ptr,
351f65ca 326 Elf_Internal_Rela *dst)
8d88c4ca 327{
67a4f2b7 328 unsigned r_type;
8d88c4ca 329
351f65ca
L
330 r_type = ELF32_R_TYPE (dst->r_info);
331 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
f3185997 332 if (cache_ptr->howto == NULL)
0a1b45a2 333 return false;
262c0a87 334 BFD_ASSERT (r_type == cache_ptr->howto->type || cache_ptr->howto->type == R_X86_64_NONE);
0a1b45a2 335 return true;
8d88c4ca 336}
70256ad8 337\f
3bab7989 338/* Support for core dump NOTE sections. */
0a1b45a2 339static bool
351f65ca 340elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
341{
342 int offset;
eea6121a 343 size_t size;
3bab7989
ML
344
345 switch (note->descsz)
346 {
347 default:
0a1b45a2 348 return false;
3bab7989 349
bcd823f1
L
350 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
351 /* pr_cursig */
228e534f 352 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
bcd823f1
L
353
354 /* pr_pid */
228e534f 355 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
bcd823f1
L
356
357 /* pr_reg */
358 offset = 72;
359 size = 216;
360
361 break;
362
3bab7989
ML
363 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
364 /* pr_cursig */
228e534f 365 elf_tdata (abfd)->core->signal
3bab7989
ML
366 = bfd_get_16 (abfd, note->descdata + 12);
367
368 /* pr_pid */
228e534f 369 elf_tdata (abfd)->core->lwpid
3bab7989
ML
370 = bfd_get_32 (abfd, note->descdata + 32);
371
372 /* pr_reg */
373 offset = 112;
eea6121a 374 size = 216;
3bab7989
ML
375
376 break;
377 }
378
379 /* Make a ".reg/999" section. */
380 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 381 size, note->descpos + offset);
3bab7989
ML
382}
383
0a1b45a2 384static bool
351f65ca 385elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
386{
387 switch (note->descsz)
388 {
389 default:
0a1b45a2 390 return false;
3bab7989 391
c3846307
FW
392 case 124:
393 /* sizeof (struct elf_external_linux_prpsinfo32_ugid16). */
228e534f 394 elf_tdata (abfd)->core->pid
bcd823f1 395 = bfd_get_32 (abfd, note->descdata + 12);
228e534f 396 elf_tdata (abfd)->core->program
bcd823f1 397 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 398 elf_tdata (abfd)->core->command
bcd823f1
L
399 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
400 break;
401
c3846307
FW
402 case 128:
403 /* sizeof (struct elf_external_linux_prpsinfo32_ugid32). */
404 elf_tdata (abfd)->core->pid
405 = bfd_get_32 (abfd, note->descdata + 12);
406 elf_tdata (abfd)->core->program
407 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
408 elf_tdata (abfd)->core->command
409 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
410 break;
411
412 case 136:
413 /* sizeof (struct elf_prpsinfo) on Linux/x86_64. */
228e534f 414 elf_tdata (abfd)->core->pid
261b8d08 415 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 416 elf_tdata (abfd)->core->program
3bab7989 417 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 418 elf_tdata (abfd)->core->command
3bab7989
ML
419 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
420 }
421
422 /* Note that for some reason, a spurious space is tacked
423 onto the end of the args in some (at least one anyway)
424 implementations, so strip it off if it exists. */
425
426 {
228e534f 427 char *command = elf_tdata (abfd)->core->command;
3bab7989
ML
428 int n = strlen (command);
429
430 if (0 < n && command[n - 1] == ' ')
431 command[n - 1] = '\0';
432 }
433
0a1b45a2 434 return true;
3bab7989 435}
8fd79e71
L
436
437#ifdef CORE_HEADER
602f1657
AM
438# if GCC_VERSION >= 8000
439# pragma GCC diagnostic push
440# pragma GCC diagnostic ignored "-Wstringop-truncation"
441# endif
8fd79e71
L
442static char *
443elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
444 int note_type, ...)
445{
446 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8fd79e71
L
447 va_list ap;
448 const char *fname, *psargs;
449 long pid;
450 int cursig;
451 const void *gregs;
452
453 switch (note_type)
454 {
455 default:
456 return NULL;
457
458 case NT_PRPSINFO:
459 va_start (ap, note_type);
460 fname = va_arg (ap, const char *);
461 psargs = va_arg (ap, const char *);
462 va_end (ap);
463
464 if (bed->s->elfclass == ELFCLASS32)
465 {
466 prpsinfo32_t data;
467 memset (&data, 0, sizeof (data));
468 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
469 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
e85c6a70
JK
470 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
471 &data, sizeof (data));
8fd79e71
L
472 }
473 else
474 {
b1bd052d 475 prpsinfo64_t data;
8fd79e71
L
476 memset (&data, 0, sizeof (data));
477 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
478 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
e85c6a70
JK
479 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
480 &data, sizeof (data));
8fd79e71 481 }
e85c6a70 482 /* NOTREACHED */
8fd79e71
L
483
484 case NT_PRSTATUS:
485 va_start (ap, note_type);
486 pid = va_arg (ap, long);
487 cursig = va_arg (ap, int);
488 gregs = va_arg (ap, const void *);
489 va_end (ap);
490
491 if (bed->s->elfclass == ELFCLASS32)
492 {
493 if (bed->elf_machine_code == EM_X86_64)
494 {
495 prstatusx32_t prstat;
496 memset (&prstat, 0, sizeof (prstat));
497 prstat.pr_pid = pid;
498 prstat.pr_cursig = cursig;
499 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
500 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
501 &prstat, sizeof (prstat));
8fd79e71
L
502 }
503 else
504 {
505 prstatus32_t prstat;
506 memset (&prstat, 0, sizeof (prstat));
507 prstat.pr_pid = pid;
508 prstat.pr_cursig = cursig;
509 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
510 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
511 &prstat, sizeof (prstat));
8fd79e71
L
512 }
513 }
514 else
515 {
b1bd052d 516 prstatus64_t prstat;
8fd79e71
L
517 memset (&prstat, 0, sizeof (prstat));
518 prstat.pr_pid = pid;
519 prstat.pr_cursig = cursig;
520 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
521 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
522 &prstat, sizeof (prstat));
8fd79e71 523 }
8fd79e71 524 }
e85c6a70 525 /* NOTREACHED */
8fd79e71 526}
602f1657
AM
527# if GCC_VERSION >= 8000
528# pragma GCC diagnostic pop
529# endif
8fd79e71 530#endif
3bab7989 531\f
407443a3 532/* Functions for the x86-64 ELF linker. */
70256ad8 533
70256ad8
AJ
534/* The size in bytes of an entry in the global offset table. */
535
536#define GOT_ENTRY_SIZE 8
8d88c4ca 537
38b12349 538/* The size in bytes of an entry in the lazy procedure linkage table. */
8d88c4ca 539
38b12349 540#define LAZY_PLT_ENTRY_SIZE 16
70256ad8 541
38b12349
L
542/* The size in bytes of an entry in the non-lazy procedure linkage
543 table. */
70256ad8 544
38b12349
L
545#define NON_LAZY_PLT_ENTRY_SIZE 8
546
547/* The first entry in a lazy procedure linkage table looks like this.
548 See the SVR4 ABI i386 supplement and the x86-64 ABI to see how this
549 works. */
550
551static const bfd_byte elf_x86_64_lazy_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
70256ad8 552{
653165cc
AJ
553 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
554 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
10efb593 555 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
70256ad8
AJ
556};
557
38b12349 558/* Subsequent entries in a lazy procedure linkage table look like this. */
70256ad8 559
38b12349 560static const bfd_byte elf_x86_64_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
70256ad8 561{
653165cc 562 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 563 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 564 0x68, /* pushq immediate */
70256ad8
AJ
565 0, 0, 0, 0, /* replaced with index into relocation table. */
566 0xe9, /* jmp relative */
567 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
568};
569
38b12349 570/* The first entry in a lazy procedure linkage table with BND prefix
0ff2b86e
L
571 like this. */
572
38b12349 573static const bfd_byte elf_x86_64_lazy_bnd_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
0ff2b86e 574{
07d6d2b8 575 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
0ff2b86e 576 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
07d6d2b8 577 0x0f, 0x1f, 0 /* nopl (%rax) */
0ff2b86e
L
578};
579
38b12349
L
580/* Subsequent entries for branches with BND prefx in a lazy procedure
581 linkage table look like this. */
0ff2b86e 582
38b12349 583static const bfd_byte elf_x86_64_lazy_bnd_plt_entry[LAZY_PLT_ENTRY_SIZE] =
0ff2b86e 584{
07d6d2b8
AM
585 0x68, 0, 0, 0, 0, /* pushq immediate */
586 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
587 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
0ff2b86e
L
588};
589
ee2fdd6f
L
590/* The first entry in the IBT-enabled lazy procedure linkage table is the
591 the same as the lazy PLT with BND prefix so that bound registers are
592 preserved when control is passed to dynamic linker. Subsequent
593 entries for a IBT-enabled lazy procedure linkage table look like
594 this. */
595
596static const bfd_byte elf_x86_64_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
597{
07d6d2b8
AM
598 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
599 0x68, 0, 0, 0, 0, /* pushq immediate */
600 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
601 0x90 /* nop */
ee2fdd6f
L
602};
603
604/* The first entry in the x32 IBT-enabled lazy procedure linkage table
de194d85 605 is the same as the normal lazy PLT. Subsequent entries for an
ee2fdd6f
L
606 x32 IBT-enabled lazy procedure linkage table look like this. */
607
608static const bfd_byte elf_x32_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
609{
07d6d2b8
AM
610 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
611 0x68, 0, 0, 0, 0, /* pushq immediate */
612 0xe9, 0, 0, 0, 0, /* jmpq relative */
613 0x66, 0x90 /* xchg %ax,%ax */
ee2fdd6f
L
614};
615
38b12349 616/* Entries in the non-lazey procedure linkage table look like this. */
0ff2b86e 617
38b12349 618static const bfd_byte elf_x86_64_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
0ff2b86e 619{
07d6d2b8
AM
620 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
621 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
622 0x66, 0x90 /* xchg %ax,%ax */
0ff2b86e
L
623};
624
38b12349
L
625/* Entries for branches with BND prefix in the non-lazey procedure
626 linkage table look like this. */
0ff2b86e 627
38b12349 628static const bfd_byte elf_x86_64_non_lazy_bnd_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
0ff2b86e 629{
07d6d2b8
AM
630 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
631 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
632 0x90 /* nop */
0ff2b86e
L
633};
634
ee2fdd6f
L
635/* Entries for branches with IBT-enabled in the non-lazey procedure
636 linkage table look like this. They have the same size as the lazy
637 PLT entry. */
638
639static const bfd_byte elf_x86_64_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
640{
07d6d2b8
AM
641 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
642 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
ee2fdd6f 643 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
07d6d2b8 644 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopl 0x0(%rax,%rax,1) */
ee2fdd6f
L
645};
646
647/* Entries for branches with IBT-enabled in the x32 non-lazey procedure
648 linkage table look like this. They have the same size as the lazy
649 PLT entry. */
650
651static const bfd_byte elf_x32_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
652{
07d6d2b8
AM
653 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
654 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
ee2fdd6f
L
655 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
656 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
657};
658
92e68c1d
L
659/* The TLSDESC entry in a lazy procedure linkage table. */
660static const bfd_byte elf_x86_64_tlsdesc_plt_entry[LAZY_PLT_ENTRY_SIZE] =
661{
662 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
663 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
664 0xff, 0x25, 16, 0, 0, 0 /* jmpq *GOT+TDG(%rip) */
665};
666
38b12349 667/* .eh_frame covering the lazy .plt section. */
e41b3a13 668
38b12349 669static const bfd_byte elf_x86_64_eh_frame_lazy_plt[] =
e41b3a13 670{
e41b3a13
JJ
671 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
672 0, 0, 0, 0, /* CIE ID */
673 1, /* CIE version */
674 'z', 'R', 0, /* Augmentation string */
675 1, /* Code alignment factor */
676 0x78, /* Data alignment factor */
677 16, /* Return address column */
678 1, /* Augmentation size */
679 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
680 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
681 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
682 DW_CFA_nop, DW_CFA_nop,
683
684 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
685 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
686 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
687 0, 0, 0, 0, /* .plt size goes here */
688 0, /* Augmentation size */
689 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
690 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
691 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
692 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
693 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
694 11, /* Block length */
695 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
696 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
697 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
698 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
699 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
700};
701
38b12349 702/* .eh_frame covering the lazy BND .plt section. */
9e659176 703
38b12349 704static const bfd_byte elf_x86_64_eh_frame_lazy_bnd_plt[] =
9e659176
L
705{
706 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
707 0, 0, 0, 0, /* CIE ID */
708 1, /* CIE version */
709 'z', 'R', 0, /* Augmentation string */
710 1, /* Code alignment factor */
711 0x78, /* Data alignment factor */
712 16, /* Return address column */
713 1, /* Augmentation size */
714 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
715 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
716 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
717 DW_CFA_nop, DW_CFA_nop,
718
719 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
720 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
721 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
722 0, 0, 0, 0, /* .plt size goes here */
723 0, /* Augmentation size */
724 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
725 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
726 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
727 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
728 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
729 11, /* Block length */
730 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
731 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
732 DW_OP_lit15, DW_OP_and, DW_OP_lit5, DW_OP_ge,
733 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
734 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
735};
736
ee2fdd6f
L
737/* .eh_frame covering the lazy .plt section with IBT-enabled. */
738
739static const bfd_byte elf_x86_64_eh_frame_lazy_ibt_plt[] =
740{
741 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
742 0, 0, 0, 0, /* CIE ID */
743 1, /* CIE version */
744 'z', 'R', 0, /* Augmentation string */
745 1, /* Code alignment factor */
746 0x78, /* Data alignment factor */
747 16, /* Return address column */
748 1, /* Augmentation size */
749 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
750 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
751 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
752 DW_CFA_nop, DW_CFA_nop,
753
754 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
755 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
756 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
757 0, 0, 0, 0, /* .plt size goes here */
758 0, /* Augmentation size */
759 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
760 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
761 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
762 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
763 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
764 11, /* Block length */
765 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
766 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
767 DW_OP_lit15, DW_OP_and, DW_OP_lit10, DW_OP_ge,
768 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
769 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
770};
771
772/* .eh_frame covering the x32 lazy .plt section with IBT-enabled. */
773
774static const bfd_byte elf_x32_eh_frame_lazy_ibt_plt[] =
775{
776 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
777 0, 0, 0, 0, /* CIE ID */
778 1, /* CIE version */
779 'z', 'R', 0, /* Augmentation string */
780 1, /* Code alignment factor */
781 0x78, /* Data alignment factor */
782 16, /* Return address column */
783 1, /* Augmentation size */
784 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
785 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
786 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
787 DW_CFA_nop, DW_CFA_nop,
788
789 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
790 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
791 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
792 0, 0, 0, 0, /* .plt size goes here */
793 0, /* Augmentation size */
794 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
795 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
796 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
797 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
798 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
799 11, /* Block length */
800 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
801 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
802 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
803 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
804 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
805};
806
38b12349 807/* .eh_frame covering the non-lazy .plt section. */
fff53dae 808
38b12349 809static const bfd_byte elf_x86_64_eh_frame_non_lazy_plt[] =
fff53dae
L
810{
811#define PLT_GOT_FDE_LENGTH 20
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,
824
825 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
826 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
38b12349
L
827 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
828 0, 0, 0, 0, /* non-lazy .plt size goes here */
fff53dae
L
829 0, /* Augmentation size */
830 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop,
831 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
832};
833
cf0e0a0b
IB
834static const sframe_frame_row_entry elf_x86_64_sframe_null_fre =
835{
836 0,
8c078abd
IB
837 {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
838 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
cf0e0a0b
IB
839};
840
841/* .sframe FRE covering the .plt section entry. */
842static const sframe_frame_row_entry elf_x86_64_sframe_plt0_fre1 =
843{
844 0, /* SFrame FRE start address. */
8c078abd
IB
845 {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
846 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
cf0e0a0b
IB
847};
848
849/* .sframe FRE covering the .plt section entry. */
850static const sframe_frame_row_entry elf_x86_64_sframe_plt0_fre2 =
851{
852 6, /* SFrame FRE start address. */
8c078abd
IB
853 {24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
854 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
cf0e0a0b
IB
855};
856
857/* .sframe FRE covering the .plt section entry. */
858static const sframe_frame_row_entry elf_x86_64_sframe_pltn_fre1 =
859{
860 0, /* SFrame FRE start address. */
8c078abd
IB
861 {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
862 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
cf0e0a0b
IB
863};
864
865/* .sframe FRE covering the .plt section entry. */
866static const sframe_frame_row_entry elf_x86_64_sframe_pltn_fre2 =
867{
868 11, /* SFrame FRE start address. */
8c078abd
IB
869 {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
870 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
cf0e0a0b
IB
871};
872
873/* .sframe FRE covering the second .plt section entry. */
874static const sframe_frame_row_entry elf_x86_64_sframe_sec_pltn_fre1 =
875{
876 0, /* SFrame FRE start address. */
8c078abd
IB
877 {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
878 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
cf0e0a0b
IB
879};
880
881/* SFrame helper object for non-lazy PLT. Also used for IBT enabled PLT. */
882static const struct elf_x86_sframe_plt elf_x86_64_sframe_non_lazy_plt =
883{
884 LAZY_PLT_ENTRY_SIZE,
885 2, /* Number of FREs for PLT0. */
886 /* Array of SFrame FREs for plt0. */
887 { &elf_x86_64_sframe_plt0_fre1, &elf_x86_64_sframe_plt0_fre2 },
888 LAZY_PLT_ENTRY_SIZE,
889 1, /* Number of FREs for PLTn. */
890 /* Array of SFrame FREs for plt. */
891 { &elf_x86_64_sframe_sec_pltn_fre1, &elf_x86_64_sframe_null_fre },
892 0,
893 0, /* There is no second PLT necessary. */
894 { &elf_x86_64_sframe_null_fre }
895};
896
897/* SFrame helper object for lazy PLT. Also used for IBT enabled PLT. */
898static const struct elf_x86_sframe_plt elf_x86_64_sframe_plt =
899{
900 LAZY_PLT_ENTRY_SIZE,
901 2, /* Number of FREs for PLT0. */
902 /* Array of SFrame FREs for plt0. */
903 { &elf_x86_64_sframe_plt0_fre1, &elf_x86_64_sframe_plt0_fre2 },
904 LAZY_PLT_ENTRY_SIZE,
905 2, /* Number of FREs for PLTn. */
906 /* Array of SFrame FREs for plt. */
907 { &elf_x86_64_sframe_pltn_fre1, &elf_x86_64_sframe_pltn_fre2 },
908 NON_LAZY_PLT_ENTRY_SIZE,
909 1, /* Number of FREs for PLTn for second PLT. */
53d8d3f0 910 /* FREs for second plt (stack trace info for .plt.got is
cf0e0a0b
IB
911 identical). Used when IBT or non-lazy PLT is in effect. */
912 { &elf_x86_64_sframe_sec_pltn_fre1 }
913};
914
eed180f8 915/* These are the standard parameters. */
765e526c 916static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_plt =
eed180f8 917 {
07d6d2b8
AM
918 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
919 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
920 elf_x86_64_lazy_plt_entry, /* plt_entry */
921 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
922 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
923 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
924 6, /* plt_tlsdesc_got1_offset */
925 12, /* plt_tlsdesc_got2_offset */
926 10, /* plt_tlsdesc_got1_insn_end */
927 16, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
928 2, /* plt0_got1_offset */
929 8, /* plt0_got2_offset */
930 12, /* plt0_got2_insn_end */
931 2, /* plt_got_offset */
932 7, /* plt_reloc_offset */
933 12, /* plt_plt_offset */
934 6, /* plt_got_insn_size */
935 LAZY_PLT_ENTRY_SIZE, /* plt_plt_insn_end */
936 6, /* plt_lazy_offset */
937 elf_x86_64_lazy_plt0_entry, /* pic_plt0_entry */
938 elf_x86_64_lazy_plt_entry, /* pic_plt_entry */
939 elf_x86_64_eh_frame_lazy_plt, /* eh_frame_plt */
38b12349
L
940 sizeof (elf_x86_64_eh_frame_lazy_plt) /* eh_frame_plt_size */
941 };
942
765e526c 943static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_plt =
38b12349 944 {
07d6d2b8
AM
945 elf_x86_64_non_lazy_plt_entry, /* plt_entry */
946 elf_x86_64_non_lazy_plt_entry, /* pic_plt_entry */
947 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
948 2, /* plt_got_offset */
949 6, /* plt_got_insn_size */
950 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
38b12349 951 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
eed180f8
RM
952 };
953
765e526c 954static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_bnd_plt =
0ff2b86e 955 {
07d6d2b8
AM
956 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
957 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
958 elf_x86_64_lazy_bnd_plt_entry, /* plt_entry */
959 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
960 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
961 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
962 6, /* plt_tlsdesc_got1_offset */
963 12, /* plt_tlsdesc_got2_offset */
964 10, /* plt_tlsdesc_got1_insn_end */
965 16, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
966 2, /* plt0_got1_offset */
967 1+8, /* plt0_got2_offset */
968 1+12, /* plt0_got2_insn_end */
969 1+2, /* plt_got_offset */
970 1, /* plt_reloc_offset */
971 7, /* plt_plt_offset */
972 1+6, /* plt_got_insn_size */
973 11, /* plt_plt_insn_end */
974 0, /* plt_lazy_offset */
975 elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
976 elf_x86_64_lazy_bnd_plt_entry, /* pic_plt_entry */
977 elf_x86_64_eh_frame_lazy_bnd_plt, /* eh_frame_plt */
38b12349
L
978 sizeof (elf_x86_64_eh_frame_lazy_bnd_plt) /* eh_frame_plt_size */
979 };
980
765e526c 981static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_bnd_plt =
38b12349 982 {
07d6d2b8
AM
983 elf_x86_64_non_lazy_bnd_plt_entry, /* plt_entry */
984 elf_x86_64_non_lazy_bnd_plt_entry, /* pic_plt_entry */
985 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
986 1+2, /* plt_got_offset */
987 1+6, /* plt_got_insn_size */
988 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
38b12349
L
989 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
990 };
991
765e526c 992static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_ibt_plt =
ee2fdd6f 993 {
07d6d2b8
AM
994 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
995 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
996 elf_x86_64_lazy_ibt_plt_entry, /* plt_entry */
997 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
998 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
999 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
1000 6, /* plt_tlsdesc_got1_offset */
1001 12, /* plt_tlsdesc_got2_offset */
1002 10, /* plt_tlsdesc_got1_insn_end */
1003 16, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
1004 2, /* plt0_got1_offset */
1005 1+8, /* plt0_got2_offset */
1006 1+12, /* plt0_got2_insn_end */
1007 4+1+2, /* plt_got_offset */
1008 4+1, /* plt_reloc_offset */
1009 4+1+6, /* plt_plt_offset */
1010 4+1+6, /* plt_got_insn_size */
1011 4+1+5+5, /* plt_plt_insn_end */
1012 0, /* plt_lazy_offset */
1013 elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
1014 elf_x86_64_lazy_ibt_plt_entry, /* pic_plt_entry */
1015 elf_x86_64_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
ee2fdd6f
L
1016 sizeof (elf_x86_64_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
1017 };
1018
765e526c 1019static const struct elf_x86_lazy_plt_layout elf_x32_lazy_ibt_plt =
ee2fdd6f 1020 {
07d6d2b8
AM
1021 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
1022 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
1023 elf_x32_lazy_ibt_plt_entry, /* plt_entry */
1024 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
92e68c1d
L
1025 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
1026 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
1027 6, /* plt_tlsdesc_got1_offset */
1028 12, /* plt_tlsdesc_got2_offset */
1029 10, /* plt_tlsdesc_got1_insn_end */
1030 16, /* plt_tlsdesc_got2_insn_end */
07d6d2b8
AM
1031 2, /* plt0_got1_offset */
1032 8, /* plt0_got2_offset */
1033 12, /* plt0_got2_insn_end */
1034 4+2, /* plt_got_offset */
1035 4+1, /* plt_reloc_offset */
1036 4+6, /* plt_plt_offset */
1037 4+6, /* plt_got_insn_size */
1038 4+5+5, /* plt_plt_insn_end */
1039 0, /* plt_lazy_offset */
1040 elf_x86_64_lazy_plt0_entry, /* pic_plt0_entry */
1041 elf_x32_lazy_ibt_plt_entry, /* pic_plt_entry */
1042 elf_x32_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
ee2fdd6f
L
1043 sizeof (elf_x32_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
1044 };
1045
765e526c 1046static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_ibt_plt =
ee2fdd6f 1047 {
07d6d2b8
AM
1048 elf_x86_64_non_lazy_ibt_plt_entry, /* plt_entry */
1049 elf_x86_64_non_lazy_ibt_plt_entry, /* pic_plt_entry */
1050 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1051 4+1+2, /* plt_got_offset */
1052 4+1+6, /* plt_got_insn_size */
1053 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
ee2fdd6f
L
1054 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1055 };
1056
765e526c 1057static const struct elf_x86_non_lazy_plt_layout elf_x32_non_lazy_ibt_plt =
ee2fdd6f 1058 {
07d6d2b8
AM
1059 elf_x32_non_lazy_ibt_plt_entry, /* plt_entry */
1060 elf_x32_non_lazy_ibt_plt_entry, /* pic_plt_entry */
1061 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1062 4+2, /* plt_got_offset */
1063 4+6, /* plt_got_insn_size */
1064 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
ee2fdd6f
L
1065 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1066 };
1067
0a1b45a2 1068static bool
27482721 1069elf64_x86_64_elf_object_p (bfd *abfd)
bffbf940 1070{
8d88c4ca
NC
1071 /* Set the right machine number for an x86-64 elf64 file. */
1072 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
0a1b45a2 1073 return true;
8d88c4ca
NC
1074}
1075
0a1b45a2 1076static bool
8059fb19
RM
1077elf32_x86_64_elf_object_p (bfd *abfd)
1078{
1079 /* Set the right machine number for an x86-64 elf32 file. */
1080 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
0a1b45a2 1081 return true;
8059fb19
RM
1082}
1083
142411ca
L
1084/* Return TRUE if the TLS access code sequence support transition
1085 from R_TYPE. */
1086
0a1b45a2 1087static bool
351f65ca
L
1088elf_x86_64_check_tls_transition (bfd *abfd,
1089 struct bfd_link_info *info,
1090 asection *sec,
1091 bfd_byte *contents,
1092 Elf_Internal_Shdr *symtab_hdr,
1093 struct elf_link_hash_entry **sym_hashes,
1094 unsigned int r_type,
1095 const Elf_Internal_Rela *rel,
1096 const Elf_Internal_Rela *relend)
bffbf940 1097{
142411ca
L
1098 unsigned int val;
1099 unsigned long r_symndx;
0a1b45a2 1100 bool largepic = false;
142411ca
L
1101 struct elf_link_hash_entry *h;
1102 bfd_vma offset;
0afcef53 1103 struct elf_x86_link_hash_table *htab;
e2cbcd91 1104 bfd_byte *call;
0a1b45a2 1105 bool indirect_call;
142411ca 1106
0afcef53 1107 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
142411ca 1108 offset = rel->r_offset;
bffbf940 1109 switch (r_type)
142411ca
L
1110 {
1111 case R_X86_64_TLSGD:
1112 case R_X86_64_TLSLD:
1113 if ((rel + 1) >= relend)
0a1b45a2 1114 return false;
142411ca
L
1115
1116 if (r_type == R_X86_64_TLSGD)
1117 {
52bc799a 1118 /* Check transition from GD access model. For 64bit, only
142411ca 1119 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
e2cbcd91
L
1120 .word 0x6666; rex64; call __tls_get_addr@PLT
1121 or
1122 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1123 .byte 0x66; rex64
1124 call *__tls_get_addr@GOTPCREL(%rip)
1125 which may be converted to
1126 addr32 call __tls_get_addr
52bc799a
L
1127 can transit to different access model. For 32bit, only
1128 leaq foo@tlsgd(%rip), %rdi
e2cbcd91
L
1129 .word 0x6666; rex64; call __tls_get_addr@PLT
1130 or
1131 leaq foo@tlsgd(%rip), %rdi
1132 .byte 0x66; rex64
1133 call *__tls_get_addr@GOTPCREL(%rip)
1134 which may be converted to
1135 addr32 call __tls_get_addr
1136 can transit to different access model. For largepic,
5c98a14e 1137 we also support:
07d6d2b8
AM
1138 leaq foo@tlsgd(%rip), %rdi
1139 movabsq $__tls_get_addr@pltoff, %rax
1140 addq $r15, %rax
1141 call *%rax
e2cbcd91 1142 or
07d6d2b8
AM
1143 leaq foo@tlsgd(%rip), %rdi
1144 movabsq $__tls_get_addr@pltoff, %rax
1145 addq $rbx, %rax
1146 call *%rax */
142411ca 1147
fa289a5f
AM
1148 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1149
5c98a14e 1150 if ((offset + 12) > sec->size)
0a1b45a2 1151 return false;
52bc799a 1152
e2cbcd91
L
1153 call = contents + offset + 4;
1154 if (call[0] != 0x66
1155 || !((call[1] == 0x48
1156 && call[2] == 0xff
1157 && call[3] == 0x15)
1158 || (call[1] == 0x48
1159 && call[2] == 0x67
1160 && call[3] == 0xe8)
1161 || (call[1] == 0x66
1162 && call[2] == 0x48
1163 && call[3] == 0xe8)))
5c98a14e
JJ
1164 {
1165 if (!ABI_64_P (abfd)
1166 || (offset + 19) > sec->size
1167 || offset < 3
e2cbcd91
L
1168 || memcmp (call - 7, leaq + 1, 3) != 0
1169 || memcmp (call, "\x48\xb8", 2) != 0
1170 || call[11] != 0x01
1171 || call[13] != 0xff
1172 || call[14] != 0xd0
1173 || !((call[10] == 0x48 && call[12] == 0xd8)
1174 || (call[10] == 0x4c && call[12] == 0xf8)))
0a1b45a2
AM
1175 return false;
1176 largepic = true;
5c98a14e
JJ
1177 }
1178 else if (ABI_64_P (abfd))
52bc799a 1179 {
52bc799a 1180 if (offset < 4
fa289a5f 1181 || memcmp (contents + offset - 4, leaq, 4) != 0)
0a1b45a2 1182 return false;
52bc799a
L
1183 }
1184 else
1185 {
52bc799a 1186 if (offset < 3
fa289a5f 1187 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
0a1b45a2 1188 return false;
52bc799a 1189 }
e2cbcd91 1190 indirect_call = call[2] == 0xff;
142411ca
L
1191 }
1192 else
1193 {
1194 /* Check transition from LD access model. Only
1195 leaq foo@tlsld(%rip), %rdi;
e2cbcd91 1196 call __tls_get_addr@PLT
07d6d2b8 1197 or
e2cbcd91
L
1198 leaq foo@tlsld(%rip), %rdi;
1199 call *__tls_get_addr@GOTPCREL(%rip)
1200 which may be converted to
1201 addr32 call __tls_get_addr
5c98a14e
JJ
1202 can transit to different access model. For largepic
1203 we also support:
07d6d2b8
AM
1204 leaq foo@tlsld(%rip), %rdi
1205 movabsq $__tls_get_addr@pltoff, %rax
1206 addq $r15, %rax
1207 call *%rax
e2cbcd91 1208 or
07d6d2b8
AM
1209 leaq foo@tlsld(%rip), %rdi
1210 movabsq $__tls_get_addr@pltoff, %rax
1211 addq $rbx, %rax
1212 call *%rax */
142411ca 1213
fa289a5f 1214 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
142411ca
L
1215
1216 if (offset < 3 || (offset + 9) > sec->size)
0a1b45a2 1217 return false;
142411ca 1218
5c98a14e 1219 if (memcmp (contents + offset - 3, lea, 3) != 0)
0a1b45a2 1220 return false;
5c98a14e 1221
e2cbcd91
L
1222 call = contents + offset + 4;
1223 if (!(call[0] == 0xe8
1224 || (call[0] == 0xff && call[1] == 0x15)
1225 || (call[0] == 0x67 && call[1] == 0xe8)))
5c98a14e
JJ
1226 {
1227 if (!ABI_64_P (abfd)
1228 || (offset + 19) > sec->size
e2cbcd91
L
1229 || memcmp (call, "\x48\xb8", 2) != 0
1230 || call[11] != 0x01
1231 || call[13] != 0xff
1232 || call[14] != 0xd0
1233 || !((call[10] == 0x48 && call[12] == 0xd8)
1234 || (call[10] == 0x4c && call[12] == 0xf8)))
0a1b45a2
AM
1235 return false;
1236 largepic = true;
5c98a14e 1237 }
e2cbcd91 1238 indirect_call = call[0] == 0xff;
142411ca
L
1239 }
1240
351f65ca 1241 r_symndx = htab->r_sym (rel[1].r_info);
142411ca 1242 if (r_symndx < symtab_hdr->sh_info)
0a1b45a2 1243 return false;
142411ca
L
1244
1245 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1d4af308 1246 if (h == NULL
0afcef53 1247 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
0a1b45a2 1248 return false;
e2cbcd91 1249 else
78984959
L
1250 {
1251 r_type = (ELF32_R_TYPE (rel[1].r_info)
1252 & ~R_X86_64_converted_reloc_bit);
1253 if (largepic)
1254 return r_type == R_X86_64_PLTOFF64;
1255 else if (indirect_call)
d58854b6 1256 return (r_type == R_X86_64_GOTPCRELX || r_type == R_X86_64_GOTPCREL);
78984959
L
1257 else
1258 return (r_type == R_X86_64_PC32 || r_type == R_X86_64_PLT32);
1259 }
142411ca
L
1260
1261 case R_X86_64_GOTTPOFF:
1262 /* Check transition from IE access model:
4a4c5f25
L
1263 mov foo@gottpoff(%rip), %reg
1264 add foo@gottpoff(%rip), %reg
142411ca
L
1265 */
1266
4a4c5f25
L
1267 /* Check REX prefix first. */
1268 if (offset >= 3 && (offset + 4) <= sec->size)
1269 {
1270 val = bfd_get_8 (abfd, contents + offset - 3);
1271 if (val != 0x48 && val != 0x4c)
1272 {
1273 /* X32 may have 0x44 REX prefix or no REX prefix. */
1274 if (ABI_64_P (abfd))
0a1b45a2 1275 return false;
4a4c5f25
L
1276 }
1277 }
1278 else
1279 {
1280 /* X32 may not have any REX prefix. */
1281 if (ABI_64_P (abfd))
0a1b45a2 1282 return false;
4a4c5f25 1283 if (offset < 2 || (offset + 3) > sec->size)
0a1b45a2 1284 return false;
4a4c5f25 1285 }
142411ca
L
1286
1287 val = bfd_get_8 (abfd, contents + offset - 2);
1288 if (val != 0x8b && val != 0x03)
0a1b45a2 1289 return false;
142411ca
L
1290
1291 val = bfd_get_8 (abfd, contents + offset - 1);
1292 return (val & 0xc7) == 5;
1293
1294 case R_X86_64_GOTPC32_TLSDESC:
1295 /* Check transition from GDesc access model:
14470f07
L
1296 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
1297 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
142411ca
L
1298
1299 Make sure it's a leaq adding rip to a 32-bit offset
1300 into any register, although it's probably almost always
1301 going to be rax. */
1302
1303 if (offset < 3 || (offset + 4) > sec->size)
0a1b45a2 1304 return false;
142411ca
L
1305
1306 val = bfd_get_8 (abfd, contents + offset - 3);
14470f07
L
1307 val &= 0xfb;
1308 if (val != 0x48 && (ABI_64_P (abfd) || val != 0x40))
0a1b45a2 1309 return false;
142411ca
L
1310
1311 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
0a1b45a2 1312 return false;
142411ca
L
1313
1314 val = bfd_get_8 (abfd, contents + offset - 1);
1315 return (val & 0xc7) == 0x05;
1316
1317 case R_X86_64_TLSDESC_CALL:
1318 /* Check transition from GDesc access model:
14470f07
L
1319 call *x@tlsdesc(%rax) <--- LP64 mode.
1320 call *x@tlsdesc(%eax) <--- X32 mode.
142411ca
L
1321 */
1322 if (offset + 2 <= sec->size)
1323 {
14470f07 1324 unsigned int prefix;
e2cbcd91 1325 call = contents + offset;
14470f07
L
1326 prefix = 0;
1327 if (!ABI_64_P (abfd))
1328 {
1329 /* Check for call *x@tlsdesc(%eax). */
1330 if (call[0] == 0x67)
1331 {
1332 prefix = 1;
1333 if (offset + 3 > sec->size)
0a1b45a2 1334 return false;
14470f07
L
1335 }
1336 }
1337 /* Make sure that it's a call *x@tlsdesc(%rax). */
1338 return call[prefix] == 0xff && call[1 + prefix] == 0x10;
142411ca
L
1339 }
1340
0a1b45a2 1341 return false;
142411ca
L
1342
1343 default:
1344 abort ();
1345 }
1346}
1347
1348/* Return TRUE if the TLS access transition is OK or no transition
1349 will be performed. Update R_TYPE if there is a transition. */
1350
0a1b45a2 1351static bool
351f65ca
L
1352elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1353 asection *sec, bfd_byte *contents,
1354 Elf_Internal_Shdr *symtab_hdr,
1355 struct elf_link_hash_entry **sym_hashes,
1356 unsigned int *r_type, int tls_type,
1357 const Elf_Internal_Rela *rel,
1358 const Elf_Internal_Rela *relend,
1359 struct elf_link_hash_entry *h,
bedfd056 1360 unsigned long r_symndx,
0a1b45a2 1361 bool from_relocate_section)
142411ca
L
1362{
1363 unsigned int from_type = *r_type;
1364 unsigned int to_type = from_type;
0a1b45a2 1365 bool check = true;
142411ca 1366
bb1cb422
L
1367 /* Skip TLS transition for functions. */
1368 if (h != NULL
1369 && (h->type == STT_FUNC
1370 || h->type == STT_GNU_IFUNC))
0a1b45a2 1371 return true;
bb1cb422 1372
142411ca 1373 switch (from_type)
bffbf940
JJ
1374 {
1375 case R_X86_64_TLSGD:
67a4f2b7
AO
1376 case R_X86_64_GOTPC32_TLSDESC:
1377 case R_X86_64_TLSDESC_CALL:
bffbf940 1378 case R_X86_64_GOTTPOFF:
0e1862bb 1379 if (bfd_link_executable (info))
142411ca
L
1380 {
1381 if (h == NULL)
1382 to_type = R_X86_64_TPOFF32;
1383 else
1384 to_type = R_X86_64_GOTTPOFF;
1385 }
1386
bedfd056
L
1387 /* When we are called from elf_x86_64_relocate_section, there may
1388 be additional transitions based on TLS_TYPE. */
1389 if (from_relocate_section)
142411ca
L
1390 {
1391 unsigned int new_to_type = to_type;
1392
51537393 1393 if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
142411ca
L
1394 new_to_type = R_X86_64_TPOFF32;
1395
1396 if (to_type == R_X86_64_TLSGD
1397 || to_type == R_X86_64_GOTPC32_TLSDESC
1398 || to_type == R_X86_64_TLSDESC_CALL)
1399 {
1400 if (tls_type == GOT_TLS_IE)
1401 new_to_type = R_X86_64_GOTTPOFF;
1402 }
1403
1404 /* We checked the transition before when we were called from
3747999c 1405 elf_x86_64_scan_relocs. We only want to check the new
142411ca
L
1406 transition which hasn't been checked before. */
1407 check = new_to_type != to_type && from_type == to_type;
1408 to_type = new_to_type;
1409 }
1410
1411 break;
1412
bffbf940 1413 case R_X86_64_TLSLD:
0e1862bb 1414 if (bfd_link_executable (info))
142411ca
L
1415 to_type = R_X86_64_TPOFF32;
1416 break;
1417
1418 default:
0a1b45a2 1419 return true;
bffbf940
JJ
1420 }
1421
142411ca
L
1422 /* Return TRUE if there is no transition. */
1423 if (from_type == to_type)
0a1b45a2 1424 return true;
142411ca
L
1425
1426 /* Check if the transition can be performed. */
1427 if (check
351f65ca
L
1428 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1429 symtab_hdr, sym_hashes,
1430 from_type, rel, relend))
142411ca 1431 {
2f629d23 1432 reloc_howto_type *from, *to;
4c544807 1433 const char *name;
142411ca 1434
351f65ca
L
1435 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1436 to = elf_x86_64_rtype_to_howto (abfd, to_type);
142411ca 1437
f3185997 1438 if (from == NULL || to == NULL)
0a1b45a2 1439 return false;
f3185997 1440
4c544807
L
1441 if (h)
1442 name = h->root.root.string;
1443 else
1444 {
0afcef53 1445 struct elf_x86_link_hash_table *htab;
4dfe6ac6 1446
0afcef53 1447 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6
NC
1448 if (htab == NULL)
1449 name = "*unknown*";
1450 else
1451 {
1452 Elf_Internal_Sym *isym;
1453
f1dfbfdb 1454 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
4dfe6ac6
NC
1455 abfd, r_symndx);
1456 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1457 }
4c544807
L
1458 }
1459
4eca0228 1460 _bfd_error_handler
695344c0 1461 /* xgettext:c-format */
2dcf00ce
AM
1462 (_("%pB: TLS transition from %s to %s against `%s' at %#" PRIx64
1463 " in section `%pA' failed"),
1464 abfd, from->name, to->name, name, (uint64_t) rel->r_offset, sec);
142411ca 1465 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1466 return false;
142411ca
L
1467 }
1468
1469 *r_type = to_type;
0a1b45a2 1470 return true;
bffbf940
JJ
1471}
1472
0a1b45a2 1473static bool
aab921ad
L
1474elf_x86_64_need_pic (struct bfd_link_info *info,
1475 bfd *input_bfd, asection *sec,
338c190a
L
1476 struct elf_link_hash_entry *h,
1477 Elf_Internal_Shdr *symtab_hdr,
1478 Elf_Internal_Sym *isym,
1479 reloc_howto_type *howto)
1480{
1481 const char *v = "";
1482 const char *und = "";
1483 const char *pic = "";
aab921ad 1484 const char *object;
338c190a
L
1485
1486 const char *name;
1487 if (h)
1488 {
1489 name = h->root.root.string;
1490 switch (ELF_ST_VISIBILITY (h->other))
1491 {
1492 case STV_HIDDEN:
1493 v = _("hidden symbol ");
1494 break;
1495 case STV_INTERNAL:
1496 v = _("internal symbol ");
1497 break;
1498 case STV_PROTECTED:
1499 v = _("protected symbol ");
1500 break;
1501 default:
0afcef53 1502 if (((struct elf_x86_link_hash_entry *) h)->def_protected)
a5b4ee94
L
1503 v = _("protected symbol ");
1504 else
1505 v = _("symbol ");
9a7f0679 1506 pic = NULL;
338c190a
L
1507 break;
1508 }
1509
83924b38 1510 if (!SYMBOL_DEFINED_NON_SHARED_P (h) && !h->def_dynamic)
338c190a
L
1511 und = _("undefined ");
1512 }
1513 else
1514 {
1515 name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
9a7f0679 1516 pic = NULL;
338c190a
L
1517 }
1518
aab921ad 1519 if (bfd_link_dll (info))
9a7f0679
L
1520 {
1521 object = _("a shared object");
1522 if (!pic)
1523 pic = _("; recompile with -fPIC");
1524 }
aab921ad 1525 else
9a7f0679
L
1526 {
1527 if (bfd_link_pie (info))
1528 object = _("a PIE object");
1529 else
1530 object = _("a PDE object");
1531 if (!pic)
1532 pic = _("; recompile with -fPIE");
1533 }
aab921ad 1534
695344c0 1535 /* xgettext:c-format */
871b3ab2 1536 _bfd_error_handler (_("%pB: relocation %s against %s%s`%s' can "
aab921ad
L
1537 "not be used when making %s%s"),
1538 input_bfd, howto->name, und, v, name,
1539 object, pic);
338c190a
L
1540 bfd_set_error (bfd_error_bad_value);
1541 sec->check_relocs_failed = 1;
0a1b45a2 1542 return false;
338c190a 1543}
c1d11331 1544
c175a657
L
1545/* With the local symbol, foo, we convert
1546 mov foo@GOTPCREL(%rip), %reg
1547 to
1548 lea foo(%rip), %reg
1549 and convert
1550 call/jmp *foo@GOTPCREL(%rip)
1551 to
1552 nop call foo/jmp foo nop
1553 When PIC is false, convert
1554 test %reg, foo@GOTPCREL(%rip)
1555 to
1556 test $foo, %reg
1557 and convert
1558 binop foo@GOTPCREL(%rip), %reg
1559 to
1560 binop $foo, %reg
1561 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1562 instructions. */
1563
0a1b45a2 1564static bool
8b43e456 1565elf_x86_64_convert_load_reloc (bfd *abfd,
c175a657 1566 bfd_byte *contents,
6999821f 1567 unsigned int *r_type_p,
c175a657
L
1568 Elf_Internal_Rela *irel,
1569 struct elf_link_hash_entry *h,
0a1b45a2 1570 bool *converted,
c175a657
L
1571 struct bfd_link_info *link_info)
1572{
0afcef53 1573 struct elf_x86_link_hash_table *htab;
0a1b45a2
AM
1574 bool is_pic;
1575 bool no_overflow;
1576 bool relocx;
1577 bool to_reloc_pc32;
1578 bool abs_symbol;
1579 bool local_ref;
c175a657 1580 asection *tsec;
c175a657
L
1581 bfd_signed_vma raddend;
1582 unsigned int opcode;
1583 unsigned int modrm;
6999821f 1584 unsigned int r_type = *r_type_p;
c175a657 1585 unsigned int r_symndx;
c175a657 1586 bfd_vma roff = irel->r_offset;
382aae06 1587 bfd_vma abs_relocation;
c175a657
L
1588
1589 if (roff < (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2))
0a1b45a2 1590 return true;
c175a657
L
1591
1592 raddend = irel->r_addend;
1593 /* Addend for 32-bit PC-relative relocation must be -4. */
1594 if (raddend != -4)
0a1b45a2 1595 return true;
c175a657 1596
0afcef53 1597 htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
c175a657
L
1598 is_pic = bfd_link_pic (link_info);
1599
1600 relocx = (r_type == R_X86_64_GOTPCRELX
1601 || r_type == R_X86_64_REX_GOTPCRELX);
1602
8b43e456
L
1603 /* TRUE if --no-relax is used. */
1604 no_overflow = link_info->disable_target_specific_optimizations > 1;
c175a657
L
1605
1606 r_symndx = htab->r_sym (irel->r_info);
1607
1608 opcode = bfd_get_8 (abfd, contents + roff - 2);
1609
1610 /* Convert mov to lea since it has been done for a while. */
1611 if (opcode != 0x8b)
1612 {
1613 /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1614 for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1615 test, xor instructions. */
1616 if (!relocx)
0a1b45a2 1617 return true;
c175a657
L
1618 }
1619
1620 /* We convert only to R_X86_64_PC32:
1621 1. Branch.
1622 2. R_X86_64_GOTPCREL since we can't modify REX byte.
8b43e456 1623 3. no_overflow is true.
c175a657
L
1624 4. PIC.
1625 */
1626 to_reloc_pc32 = (opcode == 0xff
1627 || !relocx
8b43e456 1628 || no_overflow
c175a657
L
1629 || is_pic);
1630
0a1b45a2 1631 abs_symbol = false;
382aae06
L
1632 abs_relocation = 0;
1633
c175a657
L
1634 /* Get the symbol referred to by the reloc. */
1635 if (h == NULL)
1636 {
1637 Elf_Internal_Sym *isym
f1dfbfdb 1638 = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
c175a657
L
1639
1640 /* Skip relocation against undefined symbols. */
1641 if (isym->st_shndx == SHN_UNDEF)
0a1b45a2 1642 return true;
c175a657 1643
0a1b45a2 1644 local_ref = true;
c175a657 1645 if (isym->st_shndx == SHN_ABS)
382aae06
L
1646 {
1647 tsec = bfd_abs_section_ptr;
0a1b45a2 1648 abs_symbol = true;
382aae06
L
1649 abs_relocation = isym->st_value;
1650 }
c175a657
L
1651 else if (isym->st_shndx == SHN_COMMON)
1652 tsec = bfd_com_section_ptr;
1653 else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1654 tsec = &_bfd_elf_large_com_section;
1655 else
1656 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
c175a657
L
1657 }
1658 else
1659 {
1660 /* Undefined weak symbol is only bound locally in executable
1661 and its reference is resolved as 0 without relocation
1662 overflow. We can only perform this optimization for
1663 GOTPCRELX relocations since we need to modify REX byte.
1664 It is OK convert mov with R_X86_64_GOTPCREL to
1665 R_X86_64_PC32. */
0a27fed7
L
1666 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
1667
382aae06
L
1668 abs_symbol = ABS_SYMBOL_P (h);
1669 abs_relocation = h->root.u.def.value;
1670
0a27fed7
L
1671 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1672 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
c175a657 1673 if ((relocx || opcode == 0x8b)
0a27fed7
L
1674 && (h->root.type == bfd_link_hash_undefweak
1675 && !eh->linker_def
1676 && local_ref))
c175a657
L
1677 {
1678 if (opcode == 0xff)
1679 {
1680 /* Skip for branch instructions since R_X86_64_PC32
1681 may overflow. */
8b43e456 1682 if (no_overflow)
0a1b45a2 1683 return true;
c175a657
L
1684 }
1685 else if (relocx)
1686 {
1687 /* For non-branch instructions, we can convert to
1688 R_X86_64_32/R_X86_64_32S since we know if there
1689 is a REX byte. */
0a1b45a2 1690 to_reloc_pc32 = false;
c175a657
L
1691 }
1692
1693 /* Since we don't know the current PC when PIC is true,
1694 we can't convert to R_X86_64_PC32. */
1695 if (to_reloc_pc32 && is_pic)
0a1b45a2 1696 return true;
c175a657
L
1697
1698 goto convert;
1699 }
1700 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1701 ld.so may use its link-time address. */
cbd0eecf 1702 else if (h->start_stop
0a27fed7 1703 || eh->linker_def
cbd0eecf
L
1704 || ((h->def_regular
1705 || h->root.type == bfd_link_hash_defined
1706 || h->root.type == bfd_link_hash_defweak)
1707 && h != htab->elf.hdynamic
0a27fed7 1708 && local_ref))
c175a657
L
1709 {
1710 /* bfd_link_hash_new or bfd_link_hash_undefined is
1711 set by an assignment in a linker script in
cbd0eecf
L
1712 bfd_elf_record_link_assignment. start_stop is set
1713 on __start_SECNAME/__stop_SECNAME which mark section
1714 SECNAME. */
1715 if (h->start_stop
0a27fed7 1716 || eh->linker_def
cbd0eecf
L
1717 || (h->def_regular
1718 && (h->root.type == bfd_link_hash_new
1719 || h->root.type == bfd_link_hash_undefined
1720 || ((h->root.type == bfd_link_hash_defined
1721 || h->root.type == bfd_link_hash_defweak)
1722 && h->root.u.def.section == bfd_und_section_ptr))))
c175a657
L
1723 {
1724 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
8b43e456 1725 if (no_overflow)
0a1b45a2 1726 return true;
c175a657
L
1727 goto convert;
1728 }
1729 tsec = h->root.u.def.section;
c175a657
L
1730 }
1731 else
0a1b45a2 1732 return true;
c175a657
L
1733 }
1734
2168b268
L
1735 /* Don't convert GOTPCREL relocation against large section. */
1736 if (elf_section_data (tsec) != NULL
1737 && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
0a1b45a2 1738 return true;
2168b268 1739
8b43e456
L
1740 /* Skip since R_X86_64_PC32/R_X86_64_32/R_X86_64_32S may overflow. */
1741 if (no_overflow)
0a1b45a2 1742 return true;
c175a657 1743
dc1e8a47 1744 convert:
c175a657
L
1745 if (opcode == 0xff)
1746 {
1747 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
1748 unsigned int nop;
1749 unsigned int disp;
1750 bfd_vma nop_offset;
1751
1752 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1753 R_X86_64_PC32. */
1754 modrm = bfd_get_8 (abfd, contents + roff - 1);
1755 if (modrm == 0x25)
1756 {
1757 /* Convert to "jmp foo nop". */
1758 modrm = 0xe9;
1759 nop = NOP_OPCODE;
1760 nop_offset = irel->r_offset + 3;
1761 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1762 irel->r_offset -= 1;
1763 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1764 }
1765 else
1766 {
0afcef53
L
1767 struct elf_x86_link_hash_entry *eh
1768 = (struct elf_x86_link_hash_entry *) h;
e2cbcd91 1769
c175a657
L
1770 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1771 is a nop prefix. */
1772 modrm = 0xe8;
e2cbcd91
L
1773 /* To support TLS optimization, always use addr32 prefix for
1774 "call *__tls_get_addr@GOTPCREL(%rip)". */
1d4af308 1775 if (eh && eh->tls_get_addr)
c175a657 1776 {
e2cbcd91
L
1777 nop = 0x67;
1778 nop_offset = irel->r_offset - 2;
c175a657
L
1779 }
1780 else
e2cbcd91 1781 {
5b9c07b2
L
1782 nop = htab->params->call_nop_byte;
1783 if (htab->params->call_nop_as_suffix)
e2cbcd91
L
1784 {
1785 nop_offset = irel->r_offset + 3;
1786 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1787 irel->r_offset -= 1;
1788 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1789 }
1790 else
1791 nop_offset = irel->r_offset - 2;
1792 }
c175a657
L
1793 }
1794 bfd_put_8 (abfd, nop, contents + nop_offset);
1795 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1796 r_type = R_X86_64_PC32;
1797 }
1798 else
1799 {
1800 unsigned int rex;
1801 unsigned int rex_mask = REX_R;
1802
1803 if (r_type == R_X86_64_REX_GOTPCRELX)
1804 rex = bfd_get_8 (abfd, contents + roff - 3);
1805 else
1806 rex = 0;
1807
1808 if (opcode == 0x8b)
1809 {
7e45e7a9 1810 if (abs_symbol && local_ref && relocx)
0a1b45a2 1811 to_reloc_pc32 = false;
382aae06 1812
c175a657
L
1813 if (to_reloc_pc32)
1814 {
1815 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1816 "lea foo(%rip), %reg". */
1817 opcode = 0x8d;
1818 r_type = R_X86_64_PC32;
1819 }
1820 else
1821 {
1822 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1823 "mov $foo, %reg". */
1824 opcode = 0xc7;
1825 modrm = bfd_get_8 (abfd, contents + roff - 1);
1826 modrm = 0xc0 | (modrm & 0x38) >> 3;
1827 if ((rex & REX_W) != 0
1828 && ABI_64_P (link_info->output_bfd))
1829 {
1830 /* Keep the REX_W bit in REX byte for LP64. */
1831 r_type = R_X86_64_32S;
1832 goto rewrite_modrm_rex;
1833 }
1834 else
1835 {
1836 /* If the REX_W bit in REX byte isn't needed,
1837 use R_X86_64_32 and clear the W bit to avoid
1838 sign-extend imm32 to imm64. */
1839 r_type = R_X86_64_32;
1840 /* Clear the W bit in REX byte. */
1841 rex_mask |= REX_W;
1842 goto rewrite_modrm_rex;
1843 }
1844 }
1845 }
1846 else
1847 {
1848 /* R_X86_64_PC32 isn't supported. */
1849 if (to_reloc_pc32)
0a1b45a2 1850 return true;
c175a657
L
1851
1852 modrm = bfd_get_8 (abfd, contents + roff - 1);
1853 if (opcode == 0x85)
1854 {
1855 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
1856 "test $foo, %reg". */
1857 modrm = 0xc0 | (modrm & 0x38) >> 3;
1858 opcode = 0xf7;
1859 }
1860 else
1861 {
1862 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
1863 "binop $foo, %reg". */
1864 modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
1865 opcode = 0x81;
1866 }
1867
1868 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
1869 overflow when sign-extending imm32 to imm64. */
1870 r_type = (rex & REX_W) != 0 ? R_X86_64_32S : R_X86_64_32;
1871
dc1e8a47 1872 rewrite_modrm_rex:
382aae06
L
1873 if (abs_relocation)
1874 {
1875 /* Check if R_X86_64_32S/R_X86_64_32 fits. */
1876 if (r_type == R_X86_64_32S)
1877 {
1878 if ((abs_relocation + 0x80000000) > 0xffffffff)
0a1b45a2 1879 return true;
382aae06
L
1880 }
1881 else
1882 {
1883 if (abs_relocation > 0xffffffff)
0a1b45a2 1884 return true;
382aae06
L
1885 }
1886 }
1887
c175a657
L
1888 bfd_put_8 (abfd, modrm, contents + roff - 1);
1889
1890 if (rex)
1891 {
1892 /* Move the R bit to the B bit in REX byte. */
1893 rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
1894 bfd_put_8 (abfd, rex, contents + roff - 3);
1895 }
1896
1897 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
1898 irel->r_addend = 0;
1899 }
1900
1901 bfd_put_8 (abfd, opcode, contents + roff - 2);
1902 }
1903
6999821f 1904 *r_type_p = r_type;
78984959
L
1905 irel->r_info = htab->r_info (r_symndx,
1906 r_type | R_X86_64_converted_reloc_bit);
c175a657 1907
0a1b45a2 1908 *converted = true;
c175a657 1909
0a1b45a2 1910 return true;
c175a657
L
1911}
1912
70256ad8 1913/* Look through the relocs for a section during the first phase, and
3747999c
L
1914 calculate needed space in the global offset table, and procedure
1915 linkage table. */
70256ad8 1916
0a1b45a2 1917static bool
3747999c
L
1918elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
1919 asection *sec,
1920 const Elf_Internal_Rela *relocs)
70256ad8 1921{
0afcef53 1922 struct elf_x86_link_hash_table *htab;
70256ad8
AJ
1923 Elf_Internal_Shdr *symtab_hdr;
1924 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
1925 const Elf_Internal_Rela *rel;
1926 const Elf_Internal_Rela *rel_end;
bedfd056 1927 bfd_byte *contents;
0a1b45a2 1928 bool converted;
70256ad8 1929
0e1862bb 1930 if (bfd_link_relocatable (info))
0a1b45a2 1931 return true;
70256ad8 1932
0afcef53 1933 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6 1934 if (htab == NULL)
afd9acee
L
1935 {
1936 sec->check_relocs_failed = 1;
0a1b45a2 1937 return false;
afd9acee 1938 }
4dfe6ac6 1939
fe53b4a4
L
1940 BFD_ASSERT (is_x86_elf (abfd, htab));
1941
bedfd056
L
1942 /* Get the section contents. */
1943 if (elf_section_data (sec)->this_hdr.contents != NULL)
1944 contents = elf_section_data (sec)->this_hdr.contents;
1945 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1946 {
1947 sec->check_relocs_failed = 1;
0a1b45a2 1948 return false;
bedfd056
L
1949 }
1950
0ffa91dd 1951 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8 1952 sym_hashes = elf_sym_hashes (abfd);
70256ad8 1953
0a1b45a2 1954 converted = false;
5e5e02ae 1955
70256ad8
AJ
1956 rel_end = relocs + sec->reloc_count;
1957 for (rel = relocs; rel < rel_end; rel++)
1958 {
bffbf940 1959 unsigned int r_type;
d42c267e 1960 unsigned int r_symndx;
70256ad8 1961 struct elf_link_hash_entry *h;
0afcef53 1962 struct elf_x86_link_hash_entry *eh;
4c544807
L
1963 Elf_Internal_Sym *isym;
1964 const char *name;
0a1b45a2
AM
1965 bool size_reloc;
1966 bool converted_reloc;
1967 bool no_dynreloc;
70256ad8 1968
351f65ca
L
1969 r_symndx = htab->r_sym (rel->r_info);
1970 r_type = ELF32_R_TYPE (rel->r_info);
c434dee6
AJ
1971
1972 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1973 {
695344c0 1974 /* xgettext:c-format */
871b3ab2 1975 _bfd_error_handler (_("%pB: bad symbol index: %d"),
4eca0228 1976 abfd, r_symndx);
afd9acee 1977 goto error_return;
c434dee6
AJ
1978 }
1979
70256ad8 1980 if (r_symndx < symtab_hdr->sh_info)
c25bc9fc
L
1981 {
1982 /* A local symbol. */
f1dfbfdb 1983 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
c2e61a4e
L
1984 abfd, r_symndx);
1985 if (isym == NULL)
afd9acee 1986 goto error_return;
c25bc9fc
L
1987
1988 /* Check relocation against local STT_GNU_IFUNC symbol. */
351f65ca 1989 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c25bc9fc 1990 {
0afcef53 1991 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel,
0a1b45a2 1992 true);
c25bc9fc 1993 if (h == NULL)
afd9acee 1994 goto error_return;
6bbec505 1995
c25bc9fc 1996 /* Fake a STT_GNU_IFUNC symbol. */
6322e5c5
L
1997 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1998 isym, NULL);
c25bc9fc
L
1999 h->type = STT_GNU_IFUNC;
2000 h->def_regular = 1;
2001 h->ref_regular = 1;
2002 h->forced_local = 1;
2003 h->root.type = bfd_link_hash_defined;
2004 }
2005 else
2006 h = NULL;
2007 }
70256ad8 2008 else
71cb9464 2009 {
4c544807 2010 isym = NULL;
71cb9464
L
2011 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2012 while (h->root.type == bfd_link_hash_indirect
2013 || h->root.type == bfd_link_hash_warning)
2014 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c25bc9fc 2015 }
cbe950e9 2016
d1534d16
L
2017 /* Check invalid x32 relocations. */
2018 if (!ABI_64_P (abfd))
2019 switch (r_type)
2020 {
2021 default:
2022 break;
2023
d1534d16
L
2024 case R_X86_64_DTPOFF64:
2025 case R_X86_64_TPOFF64:
2026 case R_X86_64_PC64:
2027 case R_X86_64_GOTOFF64:
2028 case R_X86_64_GOT64:
2029 case R_X86_64_GOTPCREL64:
2030 case R_X86_64_GOTPC64:
2031 case R_X86_64_GOTPLT64:
2032 case R_X86_64_PLTOFF64:
2033 {
2034 if (h)
2035 name = h->root.root.string;
2036 else
2037 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2038 NULL);
4eca0228 2039 _bfd_error_handler
695344c0 2040 /* xgettext:c-format */
871b3ab2 2041 (_("%pB: relocation %s against symbol `%s' isn't "
d1534d16
L
2042 "supported in x32 mode"), abfd,
2043 x86_64_elf_howto_table[r_type].name, name);
2044 bfd_set_error (bfd_error_bad_value);
afd9acee 2045 goto error_return;
d1534d16
L
2046 }
2047 break;
2048 }
2049
6f365fda
L
2050 eh = (struct elf_x86_link_hash_entry *) h;
2051
c25bc9fc
L
2052 if (h != NULL)
2053 {
ad1e85de
L
2054 /* It is referenced by a non-shared object. */
2055 h->ref_regular = 1;
71cb9464 2056 }
70256ad8 2057
0a1b45a2 2058 converted_reloc = false;
6999821f
L
2059 if ((r_type == R_X86_64_GOTPCREL
2060 || r_type == R_X86_64_GOTPCRELX
2061 || r_type == R_X86_64_REX_GOTPCRELX)
2062 && (h == NULL || h->type != STT_GNU_IFUNC))
2063 {
2064 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
2065 if (!elf_x86_64_convert_load_reloc (abfd, contents, &r_type,
2066 irel, h, &converted_reloc,
2067 info))
2068 goto error_return;
5e5e02ae
L
2069
2070 if (converted_reloc)
0a1b45a2 2071 converted = true;
6999821f
L
2072 }
2073
382aae06
L
2074 if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
2075 symtab_hdr, &no_dynreloc))
0a1b45a2 2076 return false;
382aae06 2077
bedfd056 2078 if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
351f65ca
L
2079 symtab_hdr, sym_hashes,
2080 &r_type, GOT_UNKNOWN,
0a1b45a2 2081 rel, rel_end, h, r_symndx, false))
afd9acee 2082 goto error_return;
142411ca 2083
cd048363
L
2084 /* Check if _GLOBAL_OFFSET_TABLE_ is referenced. */
2085 if (h == htab->elf.hgot)
0a1b45a2 2086 htab->got_referenced = true;
cd048363 2087
bffbf940 2088 switch (r_type)
70256ad8 2089 {
bffbf940 2090 case R_X86_64_TLSLD:
0f09b4d8 2091 htab->tls_ld_or_ldm_got.refcount = 1;
bffbf940
JJ
2092 goto create_got;
2093
2094 case R_X86_64_TPOFF32:
0e1862bb 2095 if (!bfd_link_executable (info) && ABI_64_P (abfd))
aab921ad 2096 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
338c190a 2097 &x86_64_elf_howto_table[r_type]);
aec6b87e 2098 if (eh != NULL)
98b273dc 2099 eh->zero_undefweak &= 0x2;
bffbf940 2100 break;
c434dee6 2101
bffbf940 2102 case R_X86_64_GOTTPOFF:
0e1862bb 2103 if (!bfd_link_executable (info))
bffbf940
JJ
2104 info->flags |= DF_STATIC_TLS;
2105 /* Fall through */
70256ad8 2106
bffbf940
JJ
2107 case R_X86_64_GOT32:
2108 case R_X86_64_GOTPCREL:
56ceb5b5
L
2109 case R_X86_64_GOTPCRELX:
2110 case R_X86_64_REX_GOTPCRELX:
bffbf940 2111 case R_X86_64_TLSGD:
7b81dfbb
AJ
2112 case R_X86_64_GOT64:
2113 case R_X86_64_GOTPCREL64:
2114 case R_X86_64_GOTPLT64:
67a4f2b7
AO
2115 case R_X86_64_GOTPC32_TLSDESC:
2116 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
2117 /* This symbol requires a global offset table entry. */
2118 {
2119 int tls_type, old_tls_type;
2120
2121 switch (r_type)
2122 {
382aae06
L
2123 default:
2124 tls_type = GOT_NORMAL;
2125 if (h)
2126 {
2127 if (ABS_SYMBOL_P (h))
2128 tls_type = GOT_ABS;
2129 }
2130 else if (isym->st_shndx == SHN_ABS)
2131 tls_type = GOT_ABS;
2132 break;
2133 case R_X86_64_TLSGD:
2134 tls_type = GOT_TLS_GD;
2135 break;
2136 case R_X86_64_GOTTPOFF:
2137 tls_type = GOT_TLS_IE;
2138 break;
67a4f2b7
AO
2139 case R_X86_64_GOTPC32_TLSDESC:
2140 case R_X86_64_TLSDESC_CALL:
382aae06
L
2141 tls_type = GOT_TLS_GDESC;
2142 break;
bffbf940
JJ
2143 }
2144
2145 if (h != NULL)
2146 {
0f09b4d8 2147 h->got.refcount = 1;
aec6b87e 2148 old_tls_type = eh->tls_type;
bffbf940
JJ
2149 }
2150 else
2151 {
2152 bfd_signed_vma *local_got_refcounts;
2153
bbdeb108
L
2154 if (!elf_x86_allocate_local_got_info (abfd,
2155 symtab_hdr->sh_info))
2156 goto error_return;
2157
bffbf940
JJ
2158 /* This is a global offset table entry for a local symbol. */
2159 local_got_refcounts = elf_local_got_refcounts (abfd);
0f09b4d8 2160 local_got_refcounts[r_symndx] = 1;
bffbf940 2161 old_tls_type
0afcef53 2162 = elf_x86_local_got_tls_type (abfd) [r_symndx];
bffbf940
JJ
2163 }
2164
2165 /* If a TLS symbol is accessed using IE at least once,
2166 there is no point to use dynamic model for it. */
2167 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7
AO
2168 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2169 || tls_type != GOT_TLS_IE))
bffbf940 2170 {
67a4f2b7 2171 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
bffbf940 2172 tls_type = old_tls_type;
67a4f2b7
AO
2173 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2174 && GOT_TLS_GD_ANY_P (tls_type))
2175 tls_type |= old_tls_type;
bffbf940
JJ
2176 else
2177 {
09a24cbf 2178 if (h)
4c544807
L
2179 name = h->root.root.string;
2180 else
2181 name = bfd_elf_sym_name (abfd, symtab_hdr,
2182 isym, NULL);
4eca0228 2183 _bfd_error_handler
695344c0 2184 /* xgettext:c-format */
871b3ab2 2185 (_("%pB: '%s' accessed both as normal and"
63a5468a 2186 " thread local symbol"),
4c544807 2187 abfd, name);
68c4a57e 2188 bfd_set_error (bfd_error_bad_value);
afd9acee 2189 goto error_return;
bffbf940
JJ
2190 }
2191 }
2192
2193 if (old_tls_type != tls_type)
2194 {
aec6b87e
L
2195 if (eh != NULL)
2196 eh->tls_type = tls_type;
bffbf940 2197 else
0afcef53 2198 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
bffbf940
JJ
2199 }
2200 }
c434dee6
AJ
2201 /* Fall through */
2202
d6ab8113
JB
2203 case R_X86_64_GOTOFF64:
2204 case R_X86_64_GOTPC32:
7b81dfbb 2205 case R_X86_64_GOTPC64:
bffbf940 2206 create_got:
aec6b87e 2207 if (eh != NULL)
98b273dc 2208 eh->zero_undefweak &= 0x2;
70256ad8
AJ
2209 break;
2210
2211 case R_X86_64_PLT32:
2212 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
2213 actually build the entry in adjust_dynamic_symbol,
2214 because this might be a case of linking PIC code which is
2215 never referenced by a dynamic object, in which case we
2216 don't need to generate a procedure linkage table entry
2217 after all. */
70256ad8
AJ
2218
2219 /* If this is a local symbol, we resolve it directly without
407443a3 2220 creating a procedure linkage table entry. */
70256ad8
AJ
2221 if (h == NULL)
2222 continue;
2223
98b273dc 2224 eh->zero_undefweak &= 0x2;
f5385ebf 2225 h->needs_plt = 1;
79b0c981 2226 h->plt.refcount = 1;
70256ad8
AJ
2227 break;
2228
7b81dfbb
AJ
2229 case R_X86_64_PLTOFF64:
2230 /* This tries to form the 'address' of a function relative
2231 to GOT. For global symbols we need a PLT entry. */
2232 if (h != NULL)
2233 {
2234 h->needs_plt = 1;
79b0c981 2235 h->plt.refcount = 1;
7b81dfbb
AJ
2236 }
2237 goto create_got;
2238
6a3e1bae
L
2239 case R_X86_64_SIZE32:
2240 case R_X86_64_SIZE64:
0a1b45a2 2241 size_reloc = true;
6a3e1bae
L
2242 goto do_size;
2243
248775ba
L
2244 case R_X86_64_32:
2245 if (!ABI_64_P (abfd))
2246 goto pointer;
1a0670f3 2247 /* Fall through. */
cc78d0af
AJ
2248 case R_X86_64_8:
2249 case R_X86_64_16:
70256ad8 2250 case R_X86_64_32S:
338c190a
L
2251 /* Check relocation overflow as these relocs may lead to
2252 run-time relocation overflow. Don't error out for
1b71fb54 2253 sections we don't care about, such as debug sections or
338c190a 2254 when relocation overflow check is disabled. */
5b9c07b2 2255 if (!htab->params->no_reloc_overflow_check
6999821f 2256 && !converted_reloc
338c190a
L
2257 && (bfd_link_pic (info)
2258 || (bfd_link_executable (info)
2259 && h != NULL
2260 && !h->def_regular
2261 && h->def_dynamic
081b1afe 2262 && (sec->flags & SEC_READONLY) == 0)))
aab921ad 2263 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
338c190a 2264 &x86_64_elf_howto_table[r_type]);
1b71fb54
AJ
2265 /* Fall through. */
2266
81e8046d
L
2267 case R_X86_64_PC8:
2268 case R_X86_64_PC16:
2269 case R_X86_64_PC32:
d6ab8113 2270 case R_X86_64_PC64:
1b71fb54 2271 case R_X86_64_64:
dc1e8a47 2272 pointer:
aec6b87e 2273 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
98b273dc 2274 eh->zero_undefweak |= 0x2;
bf52d7c7
L
2275 /* We are called after all symbols have been resolved. Only
2276 relocation against STT_GNU_IFUNC symbol must go through
2277 PLT. */
d1ed1c7d
L
2278 if (h != NULL
2279 && (bfd_link_executable (info)
bf52d7c7 2280 || h->type == STT_GNU_IFUNC))
c434dee6 2281 {
0a1b45a2 2282 bool func_pointer_ref = false;
233cc9c1 2283
5db4f0d3
L
2284 if (r_type == R_X86_64_PC32)
2285 {
2286 /* Since something like ".long foo - ." may be used
2287 as pointer, make sure that PLT is used if foo is
2288 a function defined in a shared library. */
2289 if ((sec->flags & SEC_CODE) == 0)
451875b4
L
2290 {
2291 h->pointer_equality_needed = 1;
2292 if (bfd_link_pie (info)
2293 && h->type == STT_FUNC
2294 && !h->def_regular
2295 && h->def_dynamic)
2296 {
2297 h->needs_plt = 1;
2298 h->plt.refcount = 1;
2299 }
2300 }
5db4f0d3 2301 }
8ad93045 2302 else if (r_type != R_X86_64_PC64)
04ebc307 2303 {
04ebc307
L
2304 /* At run-time, R_X86_64_64 can be resolved for both
2305 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
68c4956b
L
2306 can only be resolved for x32. Function pointer
2307 reference doesn't need PLT for pointer equality. */
04ebc307
L
2308 if ((sec->flags & SEC_READONLY) == 0
2309 && (r_type == R_X86_64_64
2310 || (!ABI_64_P (abfd)
2311 && (r_type == R_X86_64_32
2312 || r_type == R_X86_64_32S))))
0a1b45a2 2313 func_pointer_ref = true;
98a1627f
L
2314
2315 /* IFUNC symbol needs pointer equality in PDE so that
2316 function pointer reference will be resolved to its
2317 PLT entry directly. */
2318 if (!func_pointer_ref
2319 || (bfd_link_pde (info)
2320 && h->type == STT_GNU_IFUNC))
68c4956b 2321 h->pointer_equality_needed = 1;
ebb191ad
L
2322 }
2323
79b0c981
L
2324 if (!func_pointer_ref)
2325 {
2326 /* If this reloc is in a read-only section, we might
2327 need a copy reloc. We can't check reliably at this
2328 stage whether the section is read-only, as input
2329 sections have not yet been mapped to output sections.
2330 Tentatively set the flag for now, and correct in
2331 adjust_dynamic_symbol. */
2332 h->non_got_ref = 1;
2333
6f365fda
L
2334 if (!elf_has_indirect_extern_access (sec->owner))
2335 eh->non_got_ref_without_indirect_extern_access = 1;
2336
79b0c981
L
2337 /* We may need a .plt entry if the symbol is a function
2338 defined in a shared lib or is a function referenced
2339 from the code or read-only section. */
2340 if (!h->def_regular
2341 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2342 h->plt.refcount = 1;
68c4956b 2343
2c43d202
L
2344 if (htab->elf.target_os != is_solaris
2345 && h->pointer_equality_needed
68c4956b
L
2346 && h->type == STT_FUNC
2347 && eh->def_protected
bd073612 2348 && !SYMBOL_DEFINED_NON_SHARED_P (h)
f6386577 2349 && h->def_dynamic)
68c4956b
L
2350 {
2351 /* Disallow non-canonical reference to canonical
2352 protected function. */
2353 _bfd_error_handler
2354 /* xgettext:c-format */
2355 (_("%pB: non-canonical reference to canonical "
2356 "protected function `%s' in %pB"),
2357 abfd, h->root.root.string,
2358 h->root.u.def.section->owner);
2359 bfd_set_error (bfd_error_bad_value);
2360 goto error_return;
2361 }
04ebc307 2362 }
c434dee6 2363 }
70256ad8 2364
0a1b45a2 2365 size_reloc = false;
dc1e8a47 2366 do_size:
382aae06 2367 if (!no_dynreloc
831083d3
L
2368 && NEED_DYNAMIC_RELOCATION_P (true, info, true, h, sec,
2369 r_type,
382aae06 2370 htab->pointer_r_type))
70256ad8 2371 {
e03a8ed8
L
2372 struct elf_dyn_relocs *p;
2373 struct elf_dyn_relocs **head;
c434dee6 2374
c434dee6
AJ
2375 /* If this is a global symbol, we count the number of
2376 relocations we need for this symbol. */
2377 if (h != NULL)
190eb1dd 2378 head = &h->dyn_relocs;
c434dee6
AJ
2379 else
2380 {
2381 /* Track dynamic relocs needed for local syms too.
2382 We really need local syms available to do this
2383 easily. Oh well. */
c434dee6 2384 asection *s;
87d72d41 2385 void **vpp;
87d72d41 2386
f1dfbfdb 2387 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
87d72d41
AM
2388 abfd, r_symndx);
2389 if (isym == NULL)
afd9acee 2390 goto error_return;
87d72d41
AM
2391
2392 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
c434dee6 2393 if (s == NULL)
87d72d41 2394 s = sec;
70256ad8 2395
e81d3500
DD
2396 /* Beware of type punned pointers vs strict aliasing
2397 rules. */
2398 vpp = &(elf_section_data (s)->local_dynrel);
e03a8ed8 2399 head = (struct elf_dyn_relocs **)vpp;
c434dee6 2400 }
70256ad8 2401
c434dee6
AJ
2402 p = *head;
2403 if (p == NULL || p->sec != sec)
2404 {
986f0783 2405 size_t amt = sizeof *p;
d8045f23 2406
e03a8ed8 2407 p = ((struct elf_dyn_relocs *)
c434dee6 2408 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 2409 if (p == NULL)
afd9acee 2410 goto error_return;
c434dee6
AJ
2411 p->next = *head;
2412 *head = p;
2413 p->sec = sec;
2414 p->count = 0;
2415 p->pc_count = 0;
70256ad8 2416 }
c434dee6
AJ
2417
2418 p->count += 1;
06a6a421 2419 /* Count size relocation as PC-relative relocation. */
831083d3 2420 if (X86_PCREL_TYPE_P (true, r_type) || size_reloc)
c434dee6 2421 p->pc_count += 1;
70256ad8
AJ
2422 }
2423 break;
fe4770f4
AJ
2424
2425 /* This relocation describes the C++ object vtable hierarchy.
2426 Reconstruct it for later use during GC. */
2427 case R_X86_64_GNU_VTINHERIT:
c152c796 2428 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
afd9acee 2429 goto error_return;
fe4770f4
AJ
2430 break;
2431
2432 /* This relocation describes which C++ vtable entries are actually
2433 used. Record for later use during GC. */
2434 case R_X86_64_GNU_VTENTRY:
a0ea3a14 2435 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
afd9acee 2436 goto error_return;
fe4770f4 2437 break;
c434dee6
AJ
2438
2439 default:
2440 break;
70256ad8
AJ
2441 }
2442 }
2443
bedfd056
L
2444 if (elf_section_data (sec)->this_hdr.contents != contents)
2445 {
a8dde0a2 2446 if (!converted && !_bfd_link_keep_memory (info))
bedfd056
L
2447 free (contents);
2448 else
2449 {
5e5e02ae
L
2450 /* Cache the section contents for elf_link_input_bfd if any
2451 load is converted or --no-keep-memory isn't used. */
bedfd056 2452 elf_section_data (sec)->this_hdr.contents = contents;
a8dde0a2 2453 info->cache_size += sec->size;
bedfd056
L
2454 }
2455 }
2456
5e5e02ae
L
2457 /* Cache relocations if any load is converted. */
2458 if (elf_section_data (sec)->relocs != relocs && converted)
2459 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
2460
0a1b45a2 2461 return true;
afd9acee 2462
dc1e8a47 2463 error_return:
bedfd056
L
2464 if (elf_section_data (sec)->this_hdr.contents != contents)
2465 free (contents);
afd9acee 2466 sec->check_relocs_failed = 1;
0a1b45a2 2467 return false;
70256ad8
AJ
2468}
2469
3747999c
L
2470static bool
2471elf_x86_64_always_size_sections (bfd *output_bfd,
2472 struct bfd_link_info *info)
2473{
2474 bfd *abfd;
2475
2476 /* Scan relocations after rel_from_abs has been set on __ehdr_start. */
2477 for (abfd = info->input_bfds;
2478 abfd != (bfd *) NULL;
2479 abfd = abfd->link.next)
2480 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2481 && !_bfd_elf_link_iterate_on_relocs (abfd, info,
2482 elf_x86_64_scan_relocs))
2483 return false;
2484
2485 return _bfd_x86_elf_always_size_sections (output_bfd, info);
2486}
2487
bffbf940
JJ
2488/* Return the relocation value for @tpoff relocation
2489 if STT_TLS virtual address is ADDRESS. */
2490
2491static bfd_vma
351f65ca 2492elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 2493{
e1918d23 2494 struct elf_link_hash_table *htab = elf_hash_table (info);
7dc98aea
RO
2495 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2496 bfd_vma static_tls_size;
bffbf940
JJ
2497
2498 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 2499 if (htab->tls_sec == NULL)
bffbf940 2500 return 0;
7dc98aea
RO
2501
2502 /* Consider special static TLS alignment requirements. */
2503 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2504 return address - static_tls_size - htab->tls_sec->vma;
bffbf940
JJ
2505}
2506
8d88c4ca
NC
2507/* Relocate an x86_64 ELF section. */
2508
0f684201 2509static int
351f65ca
L
2510elf_x86_64_relocate_section (bfd *output_bfd,
2511 struct bfd_link_info *info,
2512 bfd *input_bfd,
2513 asection *input_section,
2514 bfd_byte *contents,
2515 Elf_Internal_Rela *relocs,
2516 Elf_Internal_Sym *local_syms,
2517 asection **local_sections)
8d88c4ca 2518{
0afcef53 2519 struct elf_x86_link_hash_table *htab;
8d88c4ca
NC
2520 Elf_Internal_Shdr *symtab_hdr;
2521 struct elf_link_hash_entry **sym_hashes;
2522 bfd_vma *local_got_offsets;
67a4f2b7 2523 bfd_vma *local_tlsdesc_gotents;
c434dee6 2524 Elf_Internal_Rela *rel;
60f2e42e 2525 Elf_Internal_Rela *wrel;
8d88c4ca 2526 Elf_Internal_Rela *relend;
38b12349 2527 unsigned int plt_entry_size;
0a1b45a2 2528 bool status;
8d88c4ca 2529
3747999c 2530 /* Skip if check_relocs or scan_relocs failed. */
338c190a 2531 if (input_section->check_relocs_failed)
0a1b45a2 2532 return false;
338c190a 2533
0afcef53 2534 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6 2535 if (htab == NULL)
0a1b45a2 2536 return false;
fe53b4a4 2537
7af5d5c4
AM
2538 if (!is_x86_elf (input_bfd, htab))
2539 {
2540 bfd_set_error (bfd_error_wrong_format);
0a1b45a2 2541 return false;
7af5d5c4 2542 }
fe53b4a4 2543
765e526c 2544 plt_entry_size = htab->plt.plt_entry_size;
0ffa91dd 2545 symtab_hdr = &elf_symtab_hdr (input_bfd);
8d88c4ca
NC
2546 sym_hashes = elf_sym_hashes (input_bfd);
2547 local_got_offsets = elf_local_got_offsets (input_bfd);
0afcef53 2548 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
8d88c4ca 2549
0afcef53 2550 _bfd_x86_elf_set_tls_module_base (info);
9f03412a 2551
0a1b45a2 2552 status = true;
60f2e42e 2553 rel = wrel = relocs;
8d88c4ca 2554 relend = relocs + input_section->reloc_count;
60f2e42e 2555 for (; rel < relend; wrel++, rel++)
8d88c4ca 2556 {
419414ea 2557 unsigned int r_type, r_type_tls;
8d88c4ca
NC
2558 reloc_howto_type *howto;
2559 unsigned long r_symndx;
2560 struct elf_link_hash_entry *h;
0afcef53 2561 struct elf_x86_link_hash_entry *eh;
8d88c4ca
NC
2562 Elf_Internal_Sym *sym;
2563 asection *sec;
0ff2b86e 2564 bfd_vma off, offplt, plt_offset;
8d88c4ca 2565 bfd_vma relocation;
0a1b45a2 2566 bool unresolved_reloc;
8d88c4ca 2567 bfd_reloc_status_type r;
bffbf940 2568 int tls_type;
0ff2b86e 2569 asection *base_got, *resolved_plt;
1788fc08 2570 bfd_vma st_size;
0a1b45a2
AM
2571 bool resolved_to_zero;
2572 bool relative_reloc;
2573 bool converted_reloc;
2574 bool need_copy_reloc_in_pie;
2575 bool no_copyreloc_p;
8d88c4ca 2576
351f65ca 2577 r_type = ELF32_R_TYPE (rel->r_info);
fe4770f4
AJ
2578 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2579 || r_type == (int) R_X86_64_GNU_VTENTRY)
18954b29
L
2580 {
2581 if (wrel != rel)
2582 *wrel = *rel;
2583 continue;
2584 }
8d88c4ca 2585
b638b5d5 2586 r_symndx = htab->r_sym (rel->r_info);
78984959 2587 converted_reloc = (r_type & R_X86_64_converted_reloc_bit) != 0;
b638b5d5
L
2588 if (converted_reloc)
2589 {
2590 r_type &= ~R_X86_64_converted_reloc_bit;
2591 rel->r_info = htab->r_info (r_symndx, r_type);
2592 }
78984959 2593
76268e02
L
2594 howto = elf_x86_64_rtype_to_howto (input_bfd, r_type);
2595 if (howto == NULL)
47aeb64c 2596 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
8d88c4ca 2597
8d88c4ca
NC
2598 h = NULL;
2599 sym = NULL;
2600 sec = NULL;
0a1b45a2 2601 unresolved_reloc = false;
8d88c4ca 2602 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
2603 {
2604 sym = local_syms + r_symndx;
2605 sec = local_sections[r_symndx];
c434dee6 2606
c25bc9fc
L
2607 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2608 &sec, rel);
1788fc08 2609 st_size = sym->st_size;
c25bc9fc
L
2610
2611 /* Relocate against local STT_GNU_IFUNC symbol. */
0e1862bb 2612 if (!bfd_link_relocatable (info)
351f65ca 2613 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc 2614 {
0afcef53 2615 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd,
0a1b45a2 2616 rel, false);
c25bc9fc
L
2617 if (h == NULL)
2618 abort ();
2619
eed180f8 2620 /* Set STT_GNU_IFUNC symbol value. */
c25bc9fc
L
2621 h->root.u.def.value = sym->st_value;
2622 h->root.u.def.section = sec;
2623 }
8da6118f 2624 }
8d88c4ca 2625 else
8da6118f 2626 {
0a1b45a2
AM
2627 bool warned ATTRIBUTE_UNUSED;
2628 bool ignored ATTRIBUTE_UNUSED;
c434dee6 2629
b2a8e766
AM
2630 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2631 r_symndx, symtab_hdr, sym_hashes,
2632 h, sec, relocation,
62d887d4 2633 unresolved_reloc, warned, ignored);
1788fc08 2634 st_size = h->size;
8da6118f 2635 }
ab96bf03 2636
dbaa2011 2637 if (sec != NULL && discarded_section (sec))
60f2e42e
L
2638 {
2639 _bfd_clear_contents (howto, input_bfd, input_section,
0930cb30 2640 contents, rel->r_offset);
60f2e42e
L
2641 wrel->r_offset = rel->r_offset;
2642 wrel->r_info = 0;
2643 wrel->r_addend = 0;
2644
2645 /* For ld -r, remove relocations in debug sections against
2646 sections defined in discarded sections. Not done for
2647 eh_frame editing code expects to be present. */
2648 if (bfd_link_relocatable (info)
2649 && (input_section->flags & SEC_DEBUGGING))
2650 wrel--;
2651
2652 continue;
2653 }
ab96bf03 2654
0e1862bb 2655 if (bfd_link_relocatable (info))
2d5da473
L
2656 {
2657 if (wrel != rel)
2658 *wrel = *rel;
2659 continue;
2660 }
ab96bf03 2661
1788fc08 2662 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
64d25c44 2663 {
1788fc08
L
2664 if (r_type == R_X86_64_64)
2665 {
2666 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
2667 zero-extend it to 64bit if addend is zero. */
2668 r_type = R_X86_64_32;
2669 memset (contents + rel->r_offset + 4, 0, 4);
2670 }
2671 else if (r_type == R_X86_64_SIZE64)
2672 {
2673 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
2674 zero-extend it to 64bit if addend is zero. */
2675 r_type = R_X86_64_SIZE32;
2676 memset (contents + rel->r_offset + 4, 0, 4);
2677 }
64d25c44
L
2678 }
2679
0afcef53 2680 eh = (struct elf_x86_link_hash_entry *) h;
0ff2b86e 2681
cbe950e9
L
2682 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2683 it here if it is defined in a non-shared object. */
2684 if (h != NULL
2685 && h->type == STT_GNU_IFUNC
2686 && h->def_regular)
2687 {
cbe950e9 2688 bfd_vma plt_index;
4c544807 2689 const char *name;
cbe950e9 2690
97dc35c8
L
2691 if ((input_section->flags & SEC_ALLOC) == 0)
2692 {
347a8774
L
2693 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2694 STT_GNU_IFUNC symbol as STT_FUNC. */
2695 if (elf_section_type (input_section) == SHT_NOTE)
2696 goto skip_ifunc;
97dc35c8
L
2697 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2698 sections because such sections are not SEC_ALLOC and
2699 thus ld.so will not process them. */
2700 if ((input_section->flags & SEC_DEBUGGING) != 0)
0eace210 2701 continue;
97dc35c8
L
2702 abort ();
2703 }
233cc9c1
L
2704
2705 switch (r_type)
2706 {
2707 default:
2708 break;
2709
2710 case R_X86_64_GOTPCREL:
2711 case R_X86_64_GOTPCRELX:
2712 case R_X86_64_REX_GOTPCRELX:
2713 case R_X86_64_GOTPCREL64:
2714 base_got = htab->elf.sgot;
2715 off = h->got.offset;
2716
2717 if (base_got == NULL)
2718 abort ();
2719
2720 if (off == (bfd_vma) -1)
2721 {
2722 /* We can't use h->got.offset here to save state, or
2723 even just remember the offset, as finish_dynamic_symbol
2724 would use that as offset into .got. */
2725
2726 if (h->plt.offset == (bfd_vma) -1)
2727 abort ();
2728
2729 if (htab->elf.splt != NULL)
2730 {
38b12349 2731 plt_index = (h->plt.offset / plt_entry_size
765e526c 2732 - htab->plt.has_plt0);
233cc9c1
L
2733 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2734 base_got = htab->elf.sgotplt;
2735 }
2736 else
2737 {
2738 plt_index = h->plt.offset / plt_entry_size;
2739 off = plt_index * GOT_ENTRY_SIZE;
2740 base_got = htab->elf.igotplt;
2741 }
2742
2743 if (h->dynindx == -1
2744 || h->forced_local
2745 || info->symbolic)
2746 {
2747 /* This references the local defitionion. We must
2748 initialize this entry in the global offset table.
2749 Since the offset must always be a multiple of 8,
2750 we use the least significant bit to record
2751 whether we have initialized it already.
2752
2753 When doing a dynamic link, we create a .rela.got
2754 relocation entry to initialize the value. This
2755 is done in the finish_dynamic_symbol routine. */
2756 if ((off & 1) != 0)
2757 off &= ~1;
2758 else
2759 {
2760 bfd_put_64 (output_bfd, relocation,
2761 base_got->contents + off);
2762 /* Note that this is harmless for the GOTPLT64
2763 case, as -1 | 1 still is -1. */
2764 h->got.offset |= 1;
2765 }
2766 }
2767 }
2768
2769 relocation = (base_got->output_section->vma
2770 + base_got->output_offset + off);
2771
2772 goto do_relocation;
2773 }
2774
2775 if (h->plt.offset == (bfd_vma) -1)
2776 {
2777 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2778 if (r_type == htab->pointer_r_type
2779 && (input_section->flags & SEC_CODE) == 0)
2780 goto do_ifunc_pointer;
2781 goto bad_ifunc_reloc;
2782 }
cbe950e9
L
2783
2784 /* STT_GNU_IFUNC symbol must go through PLT. */
0ff2b86e
L
2785 if (htab->elf.splt != NULL)
2786 {
f2c29a16 2787 if (htab->plt_second != NULL)
0ff2b86e 2788 {
f2c29a16
L
2789 resolved_plt = htab->plt_second;
2790 plt_offset = eh->plt_second.offset;
0ff2b86e
L
2791 }
2792 else
2793 {
2794 resolved_plt = htab->elf.splt;
2795 plt_offset = h->plt.offset;
2796 }
2797 }
2798 else
2799 {
2800 resolved_plt = htab->elf.iplt;
2801 plt_offset = h->plt.offset;
2802 }
2803
2804 relocation = (resolved_plt->output_section->vma
2805 + resolved_plt->output_offset + plt_offset);
cbe950e9
L
2806
2807 switch (r_type)
2808 {
2809 default:
dc1e8a47 2810 bad_ifunc_reloc:
4c544807
L
2811 if (h->root.root.string)
2812 name = h->root.root.string;
2813 else
2814 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2815 NULL);
4eca0228 2816 _bfd_error_handler
695344c0 2817 /* xgettext:c-format */
871b3ab2 2818 (_("%pB: relocation %s against STT_GNU_IFUNC "
233cc9c1
L
2819 "symbol `%s' isn't supported"), input_bfd,
2820 howto->name, name);
cbe950e9 2821 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2822 return false;
cbe950e9
L
2823
2824 case R_X86_64_32S:
0e1862bb 2825 if (bfd_link_pic (info))
cbe950e9 2826 abort ();
710ab287
L
2827 goto do_relocation;
2828
248775ba
L
2829 case R_X86_64_32:
2830 if (ABI_64_P (output_bfd))
2831 goto do_relocation;
17672001 2832 /* FALLTHROUGH */
eed180f8 2833 case R_X86_64_64:
dc1e8a47 2834 do_ifunc_pointer:
710ab287
L
2835 if (rel->r_addend != 0)
2836 {
4c544807
L
2837 if (h->root.root.string)
2838 name = h->root.root.string;
2839 else
2840 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2841 sym, NULL);
4eca0228 2842 _bfd_error_handler
695344c0 2843 /* xgettext:c-format */
871b3ab2 2844 (_("%pB: relocation %s against STT_GNU_IFUNC "
2dcf00ce
AM
2845 "symbol `%s' has non-zero addend: %" PRId64),
2846 input_bfd, howto->name, name, (int64_t) rel->r_addend);
710ab287 2847 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2848 return false;
710ab287
L
2849 }
2850
2851 /* Generate dynamic relcoation only when there is a
233cc9c1
L
2852 non-GOT reference in a shared object or there is no
2853 PLT. */
2854 if ((bfd_link_pic (info) && h->non_got_ref)
2855 || h->plt.offset == (bfd_vma) -1)
710ab287
L
2856 {
2857 Elf_Internal_Rela outrel;
710ab287
L
2858 asection *sreloc;
2859
c25bc9fc
L
2860 /* Need a dynamic relocation to get the real function
2861 address. */
710ab287
L
2862 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2863 info,
2864 input_section,
2865 rel->r_offset);
2866 if (outrel.r_offset == (bfd_vma) -1
2867 || outrel.r_offset == (bfd_vma) -2)
2868 abort ();
2869
2870 outrel.r_offset += (input_section->output_section->vma
2871 + input_section->output_offset);
2872
cf1070f1 2873 if (POINTER_LOCAL_IFUNC_P (info, h))
710ab287 2874 {
871b3ab2 2875 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
6322e5c5
L
2876 h->root.root.string,
2877 h->root.u.def.section->owner);
2878
710ab287 2879 /* This symbol is resolved locally. */
56b8aada
L
2880 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
2881 outrel.r_addend = (h->root.u.def.value
2882 + h->root.u.def.section->output_section->vma
2883 + h->root.u.def.section->output_offset);
68b00778
L
2884
2885 if (htab->params->report_relative_reloc)
2886 _bfd_x86_elf_link_report_relative_reloc
2887 (info, input_section, h, sym,
2888 "R_X86_64_IRELATIVE", &outrel);
710ab287
L
2889 }
2890 else
2891 {
351f65ca 2892 outrel.r_info = htab->r_info (h->dynindx, r_type);
710ab287
L
2893 outrel.r_addend = 0;
2894 }
2895
233cc9c1
L
2896 /* Dynamic relocations are stored in
2897 1. .rela.ifunc section in PIC object.
2898 2. .rela.got section in dynamic executable.
2899 3. .rela.iplt section in static executable. */
2900 if (bfd_link_pic (info))
2901 sreloc = htab->elf.irelifunc;
2902 else if (htab->elf.splt != NULL)
2903 sreloc = htab->elf.srelgot;
2904 else
2905 sreloc = htab->elf.irelplt;
351f65ca 2906 elf_append_rela (output_bfd, sreloc, &outrel);
710ab287
L
2907
2908 /* If this reloc is against an external symbol, we
2909 do not want to fiddle with the addend. Otherwise,
2910 we need to include the symbol value so that it
2911 becomes an addend for the dynamic reloc. For an
2912 internal symbol, we have updated addend. */
56b8aada 2913 continue;
710ab287 2914 }
17672001 2915 /* FALLTHROUGH */
cbe950e9
L
2916 case R_X86_64_PC32:
2917 case R_X86_64_PC64:
2918 case R_X86_64_PLT32:
2919 goto do_relocation;
cbe950e9
L
2920 }
2921 }
2922
dc1e8a47 2923 skip_ifunc:
aec6b87e 2924 resolved_to_zero = (eh != NULL
c5bce5c6 2925 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
aec6b87e 2926
70256ad8
AJ
2927 /* When generating a shared object, the relocations handled here are
2928 copied into the output file to be resolved at run time. */
2929 switch (r_type)
2930 {
2931 case R_X86_64_GOT32:
7b81dfbb 2932 case R_X86_64_GOT64:
70256ad8
AJ
2933 /* Relocation is to the entry for this symbol in the global
2934 offset table. */
70256ad8 2935 case R_X86_64_GOTPCREL:
56ceb5b5
L
2936 case R_X86_64_GOTPCRELX:
2937 case R_X86_64_REX_GOTPCRELX:
7b81dfbb
AJ
2938 case R_X86_64_GOTPCREL64:
2939 /* Use global offset table entry as symbol value. */
2940 case R_X86_64_GOTPLT64:
de194d85 2941 /* This is obsolete and treated the same as GOT64. */
6de2ae4a 2942 base_got = htab->elf.sgot;
7b81dfbb 2943
6de2ae4a 2944 if (htab->elf.sgot == NULL)
c434dee6 2945 abort ();
053579d7 2946
0a1b45a2 2947 relative_reloc = false;
51e0a107 2948 if (h != NULL)
70256ad8 2949 {
c434dee6 2950 off = h->got.offset;
7b81dfbb 2951 if (h->needs_plt
eed180f8 2952 && h->plt.offset != (bfd_vma)-1
7b81dfbb
AJ
2953 && off == (bfd_vma)-1)
2954 {
2955 /* We can't use h->got.offset here to save
2956 state, or even just remember the offset, as
2957 finish_dynamic_symbol would use that as offset into
2958 .got. */
38b12349 2959 bfd_vma plt_index = (h->plt.offset / plt_entry_size
765e526c 2960 - htab->plt.has_plt0);
7b81dfbb 2961 off = (plt_index + 3) * GOT_ENTRY_SIZE;
6de2ae4a 2962 base_got = htab->elf.sgotplt;
7b81dfbb
AJ
2963 }
2964
2eba97c2 2965 if (RESOLVED_LOCALLY_P (info, h, htab))
51e0a107 2966 {
2eba97c2
L
2967 /* We must initialize this entry in the global offset
2968 table. Since the offset must always be a multiple
2969 of 8, we use the least significant bit to record
2970 whether we have initialized it already.
51e0a107
JH
2971
2972 When doing a dynamic link, we create a .rela.got
407443a3
AJ
2973 relocation entry to initialize the value. This is
2974 done in the finish_dynamic_symbol routine. */
51e0a107
JH
2975 if ((off & 1) != 0)
2976 off &= ~1;
2977 else
2978 {
2979 bfd_put_64 (output_bfd, relocation,
7b81dfbb
AJ
2980 base_got->contents + off);
2981 /* Note that this is harmless for the GOTPLT64 case,
eed180f8 2982 as -1 | 1 still is -1. */
51e0a107 2983 h->got.offset |= 1;
9e9821dd 2984
5af6f000
L
2985 /* NB: Don't generate relative relocation here if
2986 it has been generated by DT_RELR. */
2987 if (!info->enable_dt_relr
2988 && GENERATE_RELATIVE_RELOC_P (info, h))
9e9821dd
L
2989 {
2990 /* If this symbol isn't dynamic in PIC,
2991 generate R_X86_64_RELATIVE here. */
2992 eh->no_finish_dynamic_symbol = 1;
0a1b45a2 2993 relative_reloc = true;
9e9821dd 2994 }
51e0a107
JH
2995 }
2996 }
053579d7 2997 else
0a1b45a2 2998 unresolved_reloc = false;
70256ad8 2999 }
51e0a107
JH
3000 else
3001 {
c434dee6
AJ
3002 if (local_got_offsets == NULL)
3003 abort ();
51e0a107
JH
3004
3005 off = local_got_offsets[r_symndx];
3006
3007 /* The offset must always be a multiple of 8. We use
407443a3
AJ
3008 the least significant bit to record whether we have
3009 already generated the necessary reloc. */
51e0a107
JH
3010 if ((off & 1) != 0)
3011 off &= ~1;
3012 else
3013 {
c434dee6 3014 bfd_put_64 (output_bfd, relocation,
7b81dfbb 3015 base_got->contents + off);
9e9821dd 3016 local_got_offsets[r_symndx] |= 1;
51e0a107 3017
382aae06
L
3018 /* NB: GOTPCREL relocations against local absolute
3019 symbol store relocation value in the GOT slot
5af6f000
L
3020 without relative relocation. Don't generate
3021 relative relocation here if it has been generated
3022 by DT_RELR. */
3023 if (!info->enable_dt_relr
3024 && bfd_link_pic (info)
382aae06
L
3025 && !(sym->st_shndx == SHN_ABS
3026 && (r_type == R_X86_64_GOTPCREL
3027 || r_type == R_X86_64_GOTPCRELX
3028 || r_type == R_X86_64_REX_GOTPCRELX)))
0a1b45a2 3029 relative_reloc = true;
51e0a107 3030 }
51e0a107 3031 }
6a2bda3f 3032
9e9821dd
L
3033 if (relative_reloc)
3034 {
3035 asection *s;
3036 Elf_Internal_Rela outrel;
3037
3038 /* We need to generate a R_X86_64_RELATIVE reloc
3039 for the dynamic linker. */
3040 s = htab->elf.srelgot;
3041 if (s == NULL)
3042 abort ();
3043
3044 outrel.r_offset = (base_got->output_section->vma
3045 + base_got->output_offset
3046 + off);
3047 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3048 outrel.r_addend = relocation;
68b00778
L
3049
3050 if (htab->params->report_relative_reloc)
3051 _bfd_x86_elf_link_report_relative_reloc
3052 (info, input_section, h, sym, "R_X86_64_RELATIVE",
3053 &outrel);
3054
9e9821dd
L
3055 elf_append_rela (output_bfd, s, &outrel);
3056 }
3057
c434dee6
AJ
3058 if (off >= (bfd_vma) -2)
3059 abort ();
3060
7b81dfbb
AJ
3061 relocation = base_got->output_section->vma
3062 + base_got->output_offset + off;
56ceb5b5
L
3063 if (r_type != R_X86_64_GOTPCREL
3064 && r_type != R_X86_64_GOTPCRELX
3065 && r_type != R_X86_64_REX_GOTPCRELX
3066 && r_type != R_X86_64_GOTPCREL64)
6de2ae4a
L
3067 relocation -= htab->elf.sgotplt->output_section->vma
3068 - htab->elf.sgotplt->output_offset;
c434dee6 3069
70256ad8
AJ
3070 break;
3071
d6ab8113
JB
3072 case R_X86_64_GOTOFF64:
3073 /* Relocation is relative to the start of the global offset
3074 table. */
3075
3d949995
L
3076 /* Check to make sure it isn't a protected function or data
3077 symbol for shared library since it may not be local when
e3c0e327
L
3078 used as function address or with copy relocation. We also
3079 need to make sure that a symbol is referenced locally. */
0e1862bb 3080 if (bfd_link_pic (info) && h)
d6ab8113 3081 {
e3c0e327
L
3082 if (!h->def_regular)
3083 {
3084 const char *v;
3085
3086 switch (ELF_ST_VISIBILITY (h->other))
3087 {
3088 case STV_HIDDEN:
3089 v = _("hidden symbol");
3090 break;
3091 case STV_INTERNAL:
3092 v = _("internal symbol");
3093 break;
3094 case STV_PROTECTED:
3095 v = _("protected symbol");
3096 break;
3097 default:
3098 v = _("symbol");
3099 break;
3100 }
3101
4eca0228 3102 _bfd_error_handler
695344c0 3103 /* xgettext:c-format */
871b3ab2 3104 (_("%pB: relocation R_X86_64_GOTOFF64 against undefined %s"
63a5468a 3105 " `%s' can not be used when making a shared object"),
e3c0e327
L
3106 input_bfd, v, h->root.root.string);
3107 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3108 return false;
e3c0e327 3109 }
0e1862bb 3110 else if (!bfd_link_executable (info)
6999821f 3111 && !SYMBOL_REFERENCES_LOCAL_P (info, h)
e3c0e327
L
3112 && (h->type == STT_FUNC
3113 || h->type == STT_OBJECT)
3114 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3115 {
4eca0228 3116 _bfd_error_handler
da0d12d2 3117 /* xgettext:c-format */
871b3ab2 3118 (_("%pB: relocation R_X86_64_GOTOFF64 against protected %s"
63a5468a 3119 " `%s' can not be used when making a shared object"),
e3c0e327
L
3120 input_bfd,
3121 h->type == STT_FUNC ? "function" : "data",
3122 h->root.root.string);
3123 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3124 return false;
e3c0e327 3125 }
d6ab8113
JB
3126 }
3127
3128 /* Note that sgot is not involved in this
3129 calculation. We always want the start of .got.plt. If we
3130 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3131 permitted by the ABI, we might have to change this
3132 calculation. */
6de2ae4a
L
3133 relocation -= htab->elf.sgotplt->output_section->vma
3134 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
3135 break;
3136
3137 case R_X86_64_GOTPC32:
7b81dfbb 3138 case R_X86_64_GOTPC64:
d6ab8113 3139 /* Use global offset table as symbol value. */
6de2ae4a
L
3140 relocation = htab->elf.sgotplt->output_section->vma
3141 + htab->elf.sgotplt->output_offset;
0a1b45a2 3142 unresolved_reloc = false;
d6ab8113 3143 break;
7b81dfbb
AJ
3144
3145 case R_X86_64_PLTOFF64:
3146 /* Relocation is PLT entry relative to GOT. For local
3147 symbols it's the symbol itself relative to GOT. */
eed180f8 3148 if (h != NULL
7b81dfbb 3149 /* See PLT32 handling. */
9d734efa
L
3150 && (h->plt.offset != (bfd_vma) -1
3151 || eh->plt_got.offset != (bfd_vma) -1)
6de2ae4a 3152 && htab->elf.splt != NULL)
7b81dfbb 3153 {
9d734efa
L
3154 if (eh->plt_got.offset != (bfd_vma) -1)
3155 {
3156 /* Use the GOT PLT. */
3157 resolved_plt = htab->plt_got;
3158 plt_offset = eh->plt_got.offset;
3159 }
f2c29a16 3160 else if (htab->plt_second != NULL)
0ff2b86e 3161 {
f2c29a16
L
3162 resolved_plt = htab->plt_second;
3163 plt_offset = eh->plt_second.offset;
0ff2b86e
L
3164 }
3165 else
3166 {
3167 resolved_plt = htab->elf.splt;
3168 plt_offset = h->plt.offset;
3169 }
3170
3171 relocation = (resolved_plt->output_section->vma
3172 + resolved_plt->output_offset
3173 + plt_offset);
0a1b45a2 3174 unresolved_reloc = false;
7b81dfbb
AJ
3175 }
3176
6de2ae4a
L
3177 relocation -= htab->elf.sgotplt->output_section->vma
3178 + htab->elf.sgotplt->output_offset;
7b81dfbb 3179 break;
d6ab8113 3180
70256ad8
AJ
3181 case R_X86_64_PLT32:
3182 /* Relocation is to the entry for this symbol in the
3183 procedure linkage table. */
3184
3185 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 3186 without using the procedure linkage table. */
70256ad8
AJ
3187 if (h == NULL)
3188 break;
3189
dd7e64d4
L
3190 if ((h->plt.offset == (bfd_vma) -1
3191 && eh->plt_got.offset == (bfd_vma) -1)
6de2ae4a 3192 || htab->elf.splt == NULL)
70256ad8
AJ
3193 {
3194 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
3195 happens when statically linking PIC code, or when
3196 using -Bsymbolic. */
70256ad8
AJ
3197 break;
3198 }
3199
dc1e8a47 3200 use_plt:
dd7e64d4 3201 if (h->plt.offset != (bfd_vma) -1)
0ff2b86e 3202 {
f2c29a16 3203 if (htab->plt_second != NULL)
dd7e64d4 3204 {
f2c29a16
L
3205 resolved_plt = htab->plt_second;
3206 plt_offset = eh->plt_second.offset;
dd7e64d4
L
3207 }
3208 else
3209 {
3210 resolved_plt = htab->elf.splt;
3211 plt_offset = h->plt.offset;
3212 }
0ff2b86e
L
3213 }
3214 else
3215 {
dd7e64d4
L
3216 /* Use the GOT PLT. */
3217 resolved_plt = htab->plt_got;
3218 plt_offset = eh->plt_got.offset;
0ff2b86e
L
3219 }
3220
3221 relocation = (resolved_plt->output_section->vma
3222 + resolved_plt->output_offset
3223 + plt_offset);
0a1b45a2 3224 unresolved_reloc = false;
70256ad8
AJ
3225 break;
3226
1788fc08
L
3227 case R_X86_64_SIZE32:
3228 case R_X86_64_SIZE64:
1788fc08
L
3229 /* Set to symbol size. */
3230 relocation = st_size;
3231 goto direct;
3232
fd8ab9e5
AJ
3233 case R_X86_64_PC8:
3234 case R_X86_64_PC16:
3235 case R_X86_64_PC32:
81e8046d
L
3236 /* Don't complain about -fPIC if the symbol is undefined when
3237 building executable unless it is unresolved weak symbol,
3238 references a dynamic definition in PIE or -z nocopyreloc
3239 is used. */
3240 no_copyreloc_p
3241 = (info->nocopyreloc
3242 || (h != NULL
3243 && !h->root.linker_def
3244 && !h->root.ldscript_def
f6386577 3245 && eh->def_protected));
81e8046d
L
3246
3247 if ((input_section->flags & SEC_ALLOC) != 0
3248 && (input_section->flags & SEC_READONLY) != 0
3249 && h != NULL
3250 && ((bfd_link_executable (info)
3251 && ((h->root.type == bfd_link_hash_undefweak
3252 && (eh == NULL
3253 || !UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
3254 eh)))
3255 || (bfd_link_pie (info)
3256 && !SYMBOL_DEFINED_NON_SHARED_P (h)
3257 && h->def_dynamic)
3258 || (no_copyreloc_p
3259 && h->def_dynamic
3260 && !(h->root.u.def.section->flags & SEC_CODE))))
661b504d
L
3261 || (bfd_link_pie (info)
3262 && h->root.type == bfd_link_hash_undefweak)
81e8046d
L
3263 || bfd_link_dll (info)))
3264 {
0a1b45a2 3265 bool fail = false;
81e8046d
L
3266 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3267 {
3268 /* Symbol is referenced locally. Make sure it is
3269 defined locally. */
3270 fail = !SYMBOL_DEFINED_NON_SHARED_P (h);
3271 }
3272 else if (bfd_link_pie (info))
3273 {
3274 /* We can only use PC-relative relocations in PIE
3275 from non-code sections. */
661b504d
L
3276 if (h->root.type == bfd_link_hash_undefweak
3277 || (h->type == STT_FUNC
3278 && (sec->flags & SEC_CODE) != 0))
0a1b45a2 3279 fail = true;
81e8046d
L
3280 }
3281 else if (no_copyreloc_p || bfd_link_dll (info))
3282 {
3283 /* Symbol doesn't need copy reloc and isn't
3284 referenced locally. Don't allow PC-relative
3285 relocations against default and protected
3286 symbols since address of protected function
3287 and location of protected data may not be in
3288 the shared object. */
3289 fail = (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3290 || ELF_ST_VISIBILITY (h->other) == STV_PROTECTED);
3291 }
3292
3293 if (fail)
3294 return elf_x86_64_need_pic (info, input_bfd, input_section,
3295 h, NULL, NULL, howto);
3296 }
451875b4
L
3297 /* Since x86-64 has PC-relative PLT, we can use PLT in PIE
3298 as function address. */
81e8046d
L
3299 else if (h != NULL
3300 && (input_section->flags & SEC_CODE) == 0
3301 && bfd_link_pie (info)
3302 && h->type == STT_FUNC
3303 && !h->def_regular
3304 && h->def_dynamic)
451875b4 3305 goto use_plt;
6610a52d
L
3306 /* Fall through. */
3307
70256ad8
AJ
3308 case R_X86_64_8:
3309 case R_X86_64_16:
3310 case R_X86_64_32:
d6ab8113 3311 case R_X86_64_PC64:
6b3db546 3312 case R_X86_64_64:
80643fbc 3313 /* FIXME: The ABI says the linker should make sure the value is
407443a3 3314 the same when it's zeroextended to 64 bit. */
c434dee6 3315
dc1e8a47 3316 direct:
b1e24c02 3317 if ((input_section->flags & SEC_ALLOC) == 0)
c434dee6
AJ
3318 break;
3319
aebcc8ff
L
3320 need_copy_reloc_in_pie = (bfd_link_pie (info)
3321 && h != NULL
3322 && (h->needs_copy
3323 || eh->needs_copy
3324 || (h->root.type
3325 == bfd_link_hash_undefined))
831083d3
L
3326 && (X86_PCREL_TYPE_P (true, r_type)
3327 || X86_SIZE_TYPE_P (true,
3328 r_type)));
aebcc8ff 3329
831083d3 3330 if (GENERATE_DYNAMIC_RELOCATION_P (true, info, eh, r_type, sec,
aebcc8ff 3331 need_copy_reloc_in_pie,
0a1b45a2 3332 resolved_to_zero, false))
70256ad8
AJ
3333 {
3334 Elf_Internal_Rela outrel;
0a1b45a2 3335 bool skip, relocate;
5af6f000 3336 bool generate_dynamic_reloc = true;
c434dee6 3337 asection *sreloc;
68b00778 3338 const char *relative_reloc_name = NULL;
70256ad8
AJ
3339
3340 /* When generating a shared object, these relocations
3341 are copied into the output file to be resolved at run
407443a3 3342 time. */
0a1b45a2
AM
3343 skip = false;
3344 relocate = false;
70256ad8 3345
c629eae0
JJ
3346 outrel.r_offset =
3347 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 3348 rel->r_offset);
c629eae0 3349 if (outrel.r_offset == (bfd_vma) -1)
0a1b45a2 3350 skip = true;
0fb19cbc 3351 else if (outrel.r_offset == (bfd_vma) -2)
0a1b45a2 3352 skip = true, relocate = true;
70256ad8
AJ
3353
3354 outrel.r_offset += (input_section->output_section->vma
3355 + input_section->output_offset);
3356
3357 if (skip)
0bb2d96a 3358 memset (&outrel, 0, sizeof outrel);
c434dee6 3359
831083d3 3360 else if (COPY_INPUT_RELOC_P (true, info, h, r_type))
70256ad8 3361 {
351f65ca 3362 outrel.r_info = htab->r_info (h->dynindx, r_type);
c434dee6 3363 outrel.r_addend = rel->r_addend;
70256ad8
AJ
3364 }
3365 else
3366 {
4c10bbaa
L
3367 /* This symbol is local, or marked to become local.
3368 When relocation overflow check is disabled, we
3369 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
3370 if (r_type == htab->pointer_r_type
3371 || (r_type == R_X86_64_32
5b9c07b2 3372 && htab->params->no_reloc_overflow_check))
607c0e09 3373 {
0a1b45a2 3374 relocate = true;
5af6f000
L
3375 /* NB: Don't generate relative relocation here if
3376 it has been generated by DT_RELR. */
3377 if (info->enable_dt_relr)
3378 generate_dynamic_reloc = false;
3379 else
3380 {
3381 outrel.r_info =
3382 htab->r_info (0, R_X86_64_RELATIVE);
3383 outrel.r_addend = relocation + rel->r_addend;
3384 relative_reloc_name = "R_X86_64_RELATIVE";
3385 }
607c0e09 3386 }
64d25c44
L
3387 else if (r_type == R_X86_64_64
3388 && !ABI_64_P (output_bfd))
3389 {
0a1b45a2 3390 relocate = true;
64d25c44
L
3391 outrel.r_info = htab->r_info (0,
3392 R_X86_64_RELATIVE64);
3393 outrel.r_addend = relocation + rel->r_addend;
68b00778 3394 relative_reloc_name = "R_X86_64_RELATIVE64";
8cf0d2dd
L
3395 /* Check addend overflow. */
3396 if ((outrel.r_addend & 0x80000000)
3397 != (rel->r_addend & 0x80000000))
3398 {
3399 const char *name;
268a8d3a 3400 int addend = rel->r_addend;
8cf0d2dd
L
3401 if (h && h->root.root.string)
3402 name = h->root.root.string;
3403 else
3404 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3405 sym, NULL);
d42c267e
AM
3406 _bfd_error_handler
3407 /* xgettext:c-format */
871b3ab2 3408 (_("%pB: addend %s%#x in relocation %s against "
2dcf00ce
AM
3409 "symbol `%s' at %#" PRIx64
3410 " in section `%pA' is out of range"),
d42c267e 3411 input_bfd, addend < 0 ? "-" : "", addend,
2dcf00ce
AM
3412 howto->name, name, (uint64_t) rel->r_offset,
3413 input_section);
8cf0d2dd 3414 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3415 return false;
8cf0d2dd 3416 }
64d25c44 3417 }
607c0e09
AS
3418 else
3419 {
3420 long sindx;
3421
8517fae7 3422 if (bfd_is_abs_section (sec))
607c0e09
AS
3423 sindx = 0;
3424 else if (sec == NULL || sec->owner == NULL)
3425 {
3426 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3427 return false;
607c0e09
AS
3428 }
3429 else
3430 {
3431 asection *osec;
3432
74541ad4
AM
3433 /* We are turning this relocation into one
3434 against a section symbol. It would be
3435 proper to subtract the symbol's value,
3436 osec->vma, from the emitted reloc addend,
3437 but ld.so expects buggy relocs. */
607c0e09
AS
3438 osec = sec->output_section;
3439 sindx = elf_section_data (osec)->dynindx;
74541ad4
AM
3440 if (sindx == 0)
3441 {
3442 asection *oi = htab->elf.text_index_section;
3443 sindx = elf_section_data (oi)->dynindx;
3444 }
3445 BFD_ASSERT (sindx != 0);
607c0e09
AS
3446 }
3447
351f65ca 3448 outrel.r_info = htab->r_info (sindx, r_type);
607c0e09
AS
3449 outrel.r_addend = relocation + rel->r_addend;
3450 }
70256ad8
AJ
3451 }
3452
5af6f000 3453 if (generate_dynamic_reloc)
62d78908 3454 {
5af6f000 3455 sreloc = elf_section_data (input_section)->sreloc;
c434dee6 3456
5af6f000
L
3457 if (sreloc == NULL || sreloc->contents == NULL)
3458 {
3459 r = bfd_reloc_notsupported;
3460 goto check_relocation_error;
3461 }
68b00778 3462
5af6f000
L
3463 if (relative_reloc_name
3464 && htab->params->report_relative_reloc)
3465 _bfd_x86_elf_link_report_relative_reloc
3466 (info, input_section, h, sym,
3467 relative_reloc_name, &outrel);
3468
3469 elf_append_rela (output_bfd, sreloc, &outrel);
3470 }
70256ad8
AJ
3471
3472 /* If this reloc is against an external symbol, we do
3473 not want to fiddle with the addend. Otherwise, we
3474 need to include the symbol value so that it becomes
3475 an addend for the dynamic reloc. */
0f88be7a 3476 if (! relocate)
70256ad8
AJ
3477 continue;
3478 }
3479
3480 break;
3481
bffbf940 3482 case R_X86_64_TLSGD:
67a4f2b7
AO
3483 case R_X86_64_GOTPC32_TLSDESC:
3484 case R_X86_64_TLSDESC_CALL:
bffbf940 3485 case R_X86_64_GOTTPOFF:
bffbf940
JJ
3486 tls_type = GOT_UNKNOWN;
3487 if (h == NULL && local_got_offsets)
0afcef53 3488 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
bffbf940 3489 else if (h != NULL)
0afcef53 3490 tls_type = elf_x86_hash_entry (h)->tls_type;
142411ca 3491
419414ea 3492 r_type_tls = r_type;
351f65ca
L
3493 if (! elf_x86_64_tls_transition (info, input_bfd,
3494 input_section, contents,
3495 symtab_hdr, sym_hashes,
419414ea 3496 &r_type_tls, tls_type, rel,
0a1b45a2
AM
3497 relend, h, r_symndx, true))
3498 return false;
bffbf940 3499
419414ea 3500 if (r_type_tls == R_X86_64_TPOFF32)
bffbf940 3501 {
142411ca
L
3502 bfd_vma roff = rel->r_offset;
3503
74d39f70
NC
3504 if (roff >= input_section->size)
3505 goto corrupt_input;
3506
bffbf940 3507 BFD_ASSERT (! unresolved_reloc);
142411ca 3508
419414ea 3509 if (r_type == R_X86_64_TLSGD)
bffbf940 3510 {
52bc799a 3511 /* GD->LE transition. For 64bit, change
e2cbcd91
L
3512 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3513 .word 0x6666; rex64; call __tls_get_addr@PLT
3514 or
3515 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3516 .byte 0x66; rex64
3517 call *__tls_get_addr@GOTPCREL(%rip)
3518 which may be converted to
3519 addr32 call __tls_get_addr
52bc799a 3520 into:
e2cbcd91
L
3521 movq %fs:0, %rax
3522 leaq foo@tpoff(%rax), %rax
52bc799a 3523 For 32bit, change
e2cbcd91
L
3524 leaq foo@tlsgd(%rip), %rdi
3525 .word 0x6666; rex64; call __tls_get_addr@PLT
3526 or
3527 leaq foo@tlsgd(%rip), %rdi
3528 .byte 0x66; rex64
3529 call *__tls_get_addr@GOTPCREL(%rip)
3530 which may be converted to
3531 addr32 call __tls_get_addr
52bc799a 3532 into:
e2cbcd91
L
3533 movl %fs:0, %eax
3534 leaq foo@tpoff(%rax), %rax
5c98a14e 3535 For largepic, change:
e2cbcd91
L
3536 leaq foo@tlsgd(%rip), %rdi
3537 movabsq $__tls_get_addr@pltoff, %rax
3538 addq %r15, %rax
3539 call *%rax
5c98a14e 3540 into:
e2cbcd91
L
3541 movq %fs:0, %rax
3542 leaq foo@tpoff(%rax), %rax
3543 nopw 0x0(%rax,%rax,1) */
5c98a14e 3544 int largepic = 0;
e2cbcd91 3545 if (ABI_64_P (output_bfd))
5c98a14e 3546 {
74d39f70
NC
3547 if (roff + 5 >= input_section->size)
3548 goto corrupt_input;
e2cbcd91
L
3549 if (contents[roff + 5] == 0xb8)
3550 {
418d4036
L
3551 if (roff < 3
3552 || (roff - 3 + 22) > input_section->size)
3553 {
dc1e8a47 3554 corrupt_input:
418d4036
L
3555 info->callbacks->einfo
3556 (_("%F%P: corrupt input: %pB\n"),
3557 input_bfd);
0a1b45a2 3558 return false;
418d4036 3559 }
e2cbcd91
L
3560 memcpy (contents + roff - 3,
3561 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
3562 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3563 largepic = 1;
3564 }
3565 else
418d4036
L
3566 {
3567 if (roff < 4
3568 || (roff - 4 + 16) > input_section->size)
3569 goto corrupt_input;
3570 memcpy (contents + roff - 4,
3571 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3572 16);
3573 }
5c98a14e 3574 }
52bc799a 3575 else
418d4036
L
3576 {
3577 if (roff < 3
3578 || (roff - 3 + 15) > input_section->size)
3579 goto corrupt_input;
3580 memcpy (contents + roff - 3,
3581 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3582 15);
3583 }
74d39f70
NC
3584
3585 if (roff + 8 + largepic >= input_section->size)
3586 goto corrupt_input;
3587
eb4ff4d6 3588 bfd_put_32 (output_bfd,
351f65ca 3589 elf_x86_64_tpoff (info, relocation),
5c98a14e 3590 contents + roff + 8 + largepic);
e2cbcd91
L
3591 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
3592 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
bffbf940 3593 rel++;
60f2e42e 3594 wrel++;
bffbf940
JJ
3595 continue;
3596 }
419414ea 3597 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
67a4f2b7
AO
3598 {
3599 /* GDesc -> LE transition.
3600 It's originally something like:
14470f07
L
3601 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3602 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
67a4f2b7
AO
3603
3604 Change it to:
14470f07
L
3605 movq $x@tpoff, %rax <--- LP64 mode.
3606 rex movl $x@tpoff, %eax <--- X32 mode.
3607 */
67a4f2b7 3608
c9736ba0 3609 unsigned int val, type;
67a4f2b7 3610
418d4036
L
3611 if (roff < 3)
3612 goto corrupt_input;
67a4f2b7 3613 type = bfd_get_8 (input_bfd, contents + roff - 3);
67a4f2b7 3614 val = bfd_get_8 (input_bfd, contents + roff - 1);
14470f07
L
3615 bfd_put_8 (output_bfd,
3616 (type & 0x48) | ((type >> 2) & 1),
67a4f2b7
AO
3617 contents + roff - 3);
3618 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3619 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3620 contents + roff - 1);
eb4ff4d6 3621 bfd_put_32 (output_bfd,
351f65ca 3622 elf_x86_64_tpoff (info, relocation),
67a4f2b7
AO
3623 contents + roff);
3624 continue;
3625 }
419414ea 3626 else if (r_type == R_X86_64_TLSDESC_CALL)
67a4f2b7
AO
3627 {
3628 /* GDesc -> LE transition.
3629 It's originally:
14470f07
L
3630 call *(%rax) <--- LP64 mode.
3631 call *(%eax) <--- X32 mode.
67a4f2b7 3632 Turn it into:
14470f07
L
3633 xchg %ax,%ax <-- LP64 mode.
3634 nopl (%rax) <-- X32 mode.
3635 */
3636 unsigned int prefix = 0;
3637 if (!ABI_64_P (input_bfd))
3638 {
3639 /* Check for call *x@tlsdesc(%eax). */
3640 if (contents[roff] == 0x67)
3641 prefix = 1;
3642 }
3643 if (prefix)
3644 {
74d39f70
NC
3645 if (roff + 2 >= input_section->size)
3646 goto corrupt_input;
3647
14470f07
L
3648 bfd_put_8 (output_bfd, 0x0f, contents + roff);
3649 bfd_put_8 (output_bfd, 0x1f, contents + roff + 1);
3650 bfd_put_8 (output_bfd, 0x00, contents + roff + 2);
3651 }
3652 else
3653 {
74d39f70
NC
3654 if (roff + 1 >= input_section->size)
3655 goto corrupt_input;
3656
14470f07
L
3657 bfd_put_8 (output_bfd, 0x66, contents + roff);
3658 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3659 }
67a4f2b7
AO
3660 continue;
3661 }
419414ea 3662 else if (r_type == R_X86_64_GOTTPOFF)
bffbf940 3663 {
bffbf940 3664 /* IE->LE transition:
cf61b747 3665 For 64bit, originally it can be one of:
bffbf940
JJ
3666 movq foo@gottpoff(%rip), %reg
3667 addq foo@gottpoff(%rip), %reg
3668 We change it into:
3669 movq $foo, %reg
3670 leaq foo(%reg), %reg
cf61b747
L
3671 addq $foo, %reg.
3672 For 32bit, originally it can be one of:
3673 movq foo@gottpoff(%rip), %reg
3674 addl foo@gottpoff(%rip), %reg
3675 We change it into:
3676 movq $foo, %reg
3677 leal foo(%reg), %reg
3678 addl $foo, %reg. */
142411ca
L
3679
3680 unsigned int val, type, reg;
3681
cf61b747
L
3682 if (roff >= 3)
3683 val = bfd_get_8 (input_bfd, contents + roff - 3);
3684 else
418d4036
L
3685 {
3686 if (roff < 2)
3687 goto corrupt_input;
3688 val = 0;
3689 }
142411ca
L
3690 type = bfd_get_8 (input_bfd, contents + roff - 2);
3691 reg = bfd_get_8 (input_bfd, contents + roff - 1);
bffbf940 3692 reg >>= 3;
bffbf940
JJ
3693 if (type == 0x8b)
3694 {
3695 /* movq */
3696 if (val == 0x4c)
418d4036
L
3697 {
3698 if (roff < 3)
3699 goto corrupt_input;
3700 bfd_put_8 (output_bfd, 0x49,
3701 contents + roff - 3);
3702 }
4a4c5f25 3703 else if (!ABI_64_P (output_bfd) && val == 0x44)
418d4036
L
3704 {
3705 if (roff < 3)
3706 goto corrupt_input;
3707 bfd_put_8 (output_bfd, 0x41,
3708 contents + roff - 3);
3709 }
bffbf940 3710 bfd_put_8 (output_bfd, 0xc7,
142411ca 3711 contents + roff - 2);
bffbf940 3712 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 3713 contents + roff - 1);
bffbf940
JJ
3714 }
3715 else if (reg == 4)
3716 {
cf61b747
L
3717 /* addq/addl -> addq/addl - addressing with %rsp/%r12
3718 is special */
bffbf940 3719 if (val == 0x4c)
418d4036
L
3720 {
3721 if (roff < 3)
3722 goto corrupt_input;
3723 bfd_put_8 (output_bfd, 0x49,
3724 contents + roff - 3);
3725 }
4a4c5f25 3726 else if (!ABI_64_P (output_bfd) && val == 0x44)
418d4036
L
3727 {
3728 if (roff < 3)
3729 goto corrupt_input;
3730 bfd_put_8 (output_bfd, 0x41,
3731 contents + roff - 3);
3732 }
bffbf940 3733 bfd_put_8 (output_bfd, 0x81,
142411ca 3734 contents + roff - 2);
bffbf940 3735 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 3736 contents + roff - 1);
bffbf940
JJ
3737 }
3738 else
3739 {
cf61b747 3740 /* addq/addl -> leaq/leal */
bffbf940 3741 if (val == 0x4c)
418d4036
L
3742 {
3743 if (roff < 3)
3744 goto corrupt_input;
3745 bfd_put_8 (output_bfd, 0x4d,
3746 contents + roff - 3);
3747 }
4a4c5f25 3748 else if (!ABI_64_P (output_bfd) && val == 0x44)
418d4036
L
3749 {
3750 if (roff < 3)
3751 goto corrupt_input;
3752 bfd_put_8 (output_bfd, 0x45,
3753 contents + roff - 3);
3754 }
bffbf940 3755 bfd_put_8 (output_bfd, 0x8d,
142411ca 3756 contents + roff - 2);
bffbf940 3757 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
142411ca 3758 contents + roff - 1);
bffbf940 3759 }
eb4ff4d6 3760 bfd_put_32 (output_bfd,
351f65ca 3761 elf_x86_64_tpoff (info, relocation),
142411ca 3762 contents + roff);
bffbf940
JJ
3763 continue;
3764 }
142411ca 3765 else
0a1b45a2 3766 BFD_ASSERT (false);
bffbf940
JJ
3767 }
3768
6de2ae4a 3769 if (htab->elf.sgot == NULL)
bffbf940
JJ
3770 abort ();
3771
3772 if (h != NULL)
67a4f2b7
AO
3773 {
3774 off = h->got.offset;
0afcef53 3775 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
67a4f2b7 3776 }
bffbf940
JJ
3777 else
3778 {
3779 if (local_got_offsets == NULL)
3780 abort ();
3781
3782 off = local_got_offsets[r_symndx];
67a4f2b7 3783 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
3784 }
3785
3786 if ((off & 1) != 0)
3787 off &= ~1;
26e41594 3788 else
bffbf940
JJ
3789 {
3790 Elf_Internal_Rela outrel;
bffbf940 3791 int dr_type, indx;
67a4f2b7 3792 asection *sreloc;
bffbf940 3793
6de2ae4a 3794 if (htab->elf.srelgot == NULL)
bffbf940
JJ
3795 abort ();
3796
67a4f2b7
AO
3797 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3798
3799 if (GOT_TLS_GDESC_P (tls_type))
3800 {
351f65ca 3801 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
67a4f2b7 3802 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
6de2ae4a
L
3803 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3804 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3805 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
3806 + offplt
3807 + htab->sgotplt_jump_table_size);
6de2ae4a 3808 sreloc = htab->elf.srelplt;
67a4f2b7 3809 if (indx == 0)
0afcef53 3810 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
67a4f2b7
AO
3811 else
3812 outrel.r_addend = 0;
351f65ca 3813 elf_append_rela (output_bfd, sreloc, &outrel);
67a4f2b7
AO
3814 }
3815
6de2ae4a 3816 sreloc = htab->elf.srelgot;
67a4f2b7 3817
6de2ae4a
L
3818 outrel.r_offset = (htab->elf.sgot->output_section->vma
3819 + htab->elf.sgot->output_offset + off);
bffbf940 3820
67a4f2b7 3821 if (GOT_TLS_GD_P (tls_type))
bffbf940 3822 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
3823 else if (GOT_TLS_GDESC_P (tls_type))
3824 goto dr_done;
bffbf940
JJ
3825 else
3826 dr_type = R_X86_64_TPOFF64;
3827
6de2ae4a 3828 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
bffbf940 3829 outrel.r_addend = 0;
67a4f2b7
AO
3830 if ((dr_type == R_X86_64_TPOFF64
3831 || dr_type == R_X86_64_TLSDESC) && indx == 0)
0afcef53 3832 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
351f65ca 3833 outrel.r_info = htab->r_info (indx, dr_type);
bffbf940 3834
351f65ca 3835 elf_append_rela (output_bfd, sreloc, &outrel);
bffbf940 3836
67a4f2b7 3837 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
3838 {
3839 if (indx == 0)
3840 {
d40d037c 3841 BFD_ASSERT (! unresolved_reloc);
bffbf940 3842 bfd_put_64 (output_bfd,
0afcef53 3843 relocation - _bfd_x86_elf_dtpoff_base (info),
6de2ae4a 3844 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
bffbf940
JJ
3845 }
3846 else
3847 {
3848 bfd_put_64 (output_bfd, 0,
6de2ae4a 3849 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 3850 outrel.r_info = htab->r_info (indx,
bffbf940
JJ
3851 R_X86_64_DTPOFF64);
3852 outrel.r_offset += GOT_ENTRY_SIZE;
351f65ca 3853 elf_append_rela (output_bfd, sreloc,
464d3bd4 3854 &outrel);
bffbf940
JJ
3855 }
3856 }
3857
67a4f2b7 3858 dr_done:
bffbf940
JJ
3859 if (h != NULL)
3860 h->got.offset |= 1;
3861 else
3862 local_got_offsets[r_symndx] |= 1;
3863 }
3864
67a4f2b7
AO
3865 if (off >= (bfd_vma) -2
3866 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940 3867 abort ();
419414ea 3868 if (r_type_tls == r_type)
bffbf940 3869 {
67a4f2b7
AO
3870 if (r_type == R_X86_64_GOTPC32_TLSDESC
3871 || r_type == R_X86_64_TLSDESC_CALL)
6de2ae4a
L
3872 relocation = htab->elf.sgotplt->output_section->vma
3873 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
3874 + offplt + htab->sgotplt_jump_table_size;
3875 else
6de2ae4a
L
3876 relocation = htab->elf.sgot->output_section->vma
3877 + htab->elf.sgot->output_offset + off;
0a1b45a2 3878 unresolved_reloc = false;
bffbf940 3879 }
142411ca 3880 else
67a4f2b7 3881 {
142411ca 3882 bfd_vma roff = rel->r_offset;
67a4f2b7 3883
419414ea 3884 if (r_type == R_X86_64_TLSGD)
142411ca 3885 {
52bc799a 3886 /* GD->IE transition. For 64bit, change
e2cbcd91
L
3887 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3888 .word 0x6666; rex64; call __tls_get_addr@PLT
3889 or
3890 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3891 .byte 0x66; rex64
3892 call *__tls_get_addr@GOTPCREL(%rip
3893 which may be converted to
3894 addr32 call __tls_get_addr
52bc799a 3895 into:
e2cbcd91
L
3896 movq %fs:0, %rax
3897 addq foo@gottpoff(%rip), %rax
52bc799a 3898 For 32bit, change
e2cbcd91
L
3899 leaq foo@tlsgd(%rip), %rdi
3900 .word 0x6666; rex64; call __tls_get_addr@PLT
3901 or
3902 leaq foo@tlsgd(%rip), %rdi
3903 .byte 0x66; rex64;
3904 call *__tls_get_addr@GOTPCREL(%rip)
3905 which may be converted to
3906 addr32 call __tls_get_addr
52bc799a 3907 into:
e2cbcd91
L
3908 movl %fs:0, %eax
3909 addq foo@gottpoff(%rip), %rax
5c98a14e 3910 For largepic, change:
e2cbcd91
L
3911 leaq foo@tlsgd(%rip), %rdi
3912 movabsq $__tls_get_addr@pltoff, %rax
3913 addq %r15, %rax
3914 call *%rax
5c98a14e 3915 into:
e2cbcd91
L
3916 movq %fs:0, %rax
3917 addq foo@gottpoff(%rax), %rax
3918 nopw 0x0(%rax,%rax,1) */
5c98a14e 3919 int largepic = 0;
e2cbcd91 3920 if (ABI_64_P (output_bfd))
5c98a14e 3921 {
e2cbcd91
L
3922 if (contents[roff + 5] == 0xb8)
3923 {
418d4036
L
3924 if (roff < 3
3925 || (roff - 3 + 22) > input_section->size)
3926 goto corrupt_input;
e2cbcd91
L
3927 memcpy (contents + roff - 3,
3928 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
3929 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3930 largepic = 1;
3931 }
3932 else
418d4036
L
3933 {
3934 if (roff < 4
3935 || (roff - 4 + 16) > input_section->size)
3936 goto corrupt_input;
3937 memcpy (contents + roff - 4,
3938 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3939 16);
3940 }
5c98a14e 3941 }
52bc799a 3942 else
418d4036
L
3943 {
3944 if (roff < 3
3945 || (roff - 3 + 15) > input_section->size)
3946 goto corrupt_input;
3947 memcpy (contents + roff - 3,
3948 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3949 15);
3950 }
142411ca 3951
6de2ae4a
L
3952 relocation = (htab->elf.sgot->output_section->vma
3953 + htab->elf.sgot->output_offset + off
142411ca 3954 - roff
5c98a14e 3955 - largepic
142411ca
L
3956 - input_section->output_section->vma
3957 - input_section->output_offset
3958 - 12);
3959 bfd_put_32 (output_bfd, relocation,
5c98a14e
JJ
3960 contents + roff + 8 + largepic);
3961 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
142411ca 3962 rel++;
60f2e42e 3963 wrel++;
142411ca
L
3964 continue;
3965 }
419414ea 3966 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
142411ca
L
3967 {
3968 /* GDesc -> IE transition.
3969 It's originally something like:
14470f07
L
3970 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3971 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
67a4f2b7 3972
142411ca 3973 Change it to:
14470f07
L
3974 # before xchg %ax,%ax in LP64 mode.
3975 movq x@gottpoff(%rip), %rax
3976 # before nopl (%rax) in X32 mode.
3977 rex movl x@gottpoff(%rip), %eax
3978 */
67a4f2b7 3979
142411ca 3980 /* Now modify the instruction as appropriate. To
14470f07 3981 turn a lea into a mov in the form we use it, it
142411ca
L
3982 suffices to change the second byte from 0x8d to
3983 0x8b. */
418d4036
L
3984 if (roff < 2)
3985 goto corrupt_input;
142411ca
L
3986 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3987
3988 bfd_put_32 (output_bfd,
6de2ae4a
L
3989 htab->elf.sgot->output_section->vma
3990 + htab->elf.sgot->output_offset + off
142411ca
L
3991 - rel->r_offset
3992 - input_section->output_section->vma
3993 - input_section->output_offset
3994 - 4,
3995 contents + roff);
3996 continue;
3997 }
419414ea 3998 else if (r_type == R_X86_64_TLSDESC_CALL)
142411ca
L
3999 {
4000 /* GDesc -> IE transition.
4001 It's originally:
14470f07
L
4002 call *(%rax) <--- LP64 mode.
4003 call *(%eax) <--- X32 mode.
142411ca
L
4004
4005 Change it to:
14470f07
L
4006 xchg %ax, %ax <-- LP64 mode.
4007 nopl (%rax) <-- X32 mode.
4008 */
142411ca 4009
14470f07
L
4010 unsigned int prefix = 0;
4011 if (!ABI_64_P (input_bfd))
4012 {
4013 /* Check for call *x@tlsdesc(%eax). */
4014 if (contents[roff] == 0x67)
4015 prefix = 1;
4016 }
4017 if (prefix)
4018 {
4019 bfd_put_8 (output_bfd, 0x0f, contents + roff);
4020 bfd_put_8 (output_bfd, 0x1f, contents + roff + 1);
4021 bfd_put_8 (output_bfd, 0x00, contents + roff + 2);
4022 }
4023 else
4024 {
4025 bfd_put_8 (output_bfd, 0x66, contents + roff);
4026 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4027 }
142411ca
L
4028 continue;
4029 }
4030 else
0a1b45a2 4031 BFD_ASSERT (false);
67a4f2b7 4032 }
bffbf940
JJ
4033 break;
4034
4035 case R_X86_64_TLSLD:
351f65ca
L
4036 if (! elf_x86_64_tls_transition (info, input_bfd,
4037 input_section, contents,
4038 symtab_hdr, sym_hashes,
bedfd056 4039 &r_type, GOT_UNKNOWN, rel,
0a1b45a2
AM
4040 relend, h, r_symndx, true))
4041 return false;
a3fadc9a 4042
142411ca
L
4043 if (r_type != R_X86_64_TLSLD)
4044 {
bffbf940 4045 /* LD->LE transition:
e2cbcd91
L
4046 leaq foo@tlsld(%rip), %rdi
4047 call __tls_get_addr@PLT
4048 For 64bit, we change it into:
4049 .word 0x6666; .byte 0x66; movq %fs:0, %rax
4050 For 32bit, we change it into:
4051 nopl 0x0(%rax); movl %fs:0, %eax
4052 Or
4053 leaq foo@tlsld(%rip), %rdi;
4054 call *__tls_get_addr@GOTPCREL(%rip)
4055 which may be converted to
4056 addr32 call __tls_get_addr
52bc799a 4057 For 64bit, we change it into:
e2cbcd91 4058 .word 0x6666; .word 0x6666; movq %fs:0, %rax
52bc799a 4059 For 32bit, we change it into:
e2cbcd91 4060 nopw 0x0(%rax); movl %fs:0, %eax
5c98a14e 4061 For largepic, change:
e2cbcd91
L
4062 leaq foo@tlsgd(%rip), %rdi
4063 movabsq $__tls_get_addr@pltoff, %rax
4064 addq %rbx, %rax
4065 call *%rax
4066 into
4067 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
4068 movq %fs:0, %eax */
142411ca
L
4069
4070 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
e2cbcd91
L
4071 if (ABI_64_P (output_bfd))
4072 {
418d4036
L
4073 if ((rel->r_offset + 5) >= input_section->size)
4074 goto corrupt_input;
e2cbcd91 4075 if (contents[rel->r_offset + 5] == 0xb8)
418d4036
L
4076 {
4077 if (rel->r_offset < 3
4078 || (rel->r_offset - 3 + 22) > input_section->size)
4079 goto corrupt_input;
4080 memcpy (contents + rel->r_offset - 3,
4081 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
4082 "\x64\x48\x8b\x04\x25\0\0\0", 22);
4083 }
e2cbcd91
L
4084 else if (contents[rel->r_offset + 4] == 0xff
4085 || contents[rel->r_offset + 4] == 0x67)
418d4036
L
4086 {
4087 if (rel->r_offset < 3
4088 || (rel->r_offset - 3 + 13) > input_section->size)
4089 goto corrupt_input;
4090 memcpy (contents + rel->r_offset - 3,
4091 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
4092 13);
4093
4094 }
e2cbcd91 4095 else
418d4036
L
4096 {
4097 if (rel->r_offset < 3
4098 || (rel->r_offset - 3 + 12) > input_section->size)
4099 goto corrupt_input;
4100 memcpy (contents + rel->r_offset - 3,
4101 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
4102 }
e2cbcd91 4103 }
52bc799a 4104 else
e2cbcd91 4105 {
418d4036
L
4106 if ((rel->r_offset + 4) >= input_section->size)
4107 goto corrupt_input;
e2cbcd91 4108 if (contents[rel->r_offset + 4] == 0xff)
418d4036
L
4109 {
4110 if (rel->r_offset < 3
4111 || (rel->r_offset - 3 + 13) > input_section->size)
4112 goto corrupt_input;
4113 memcpy (contents + rel->r_offset - 3,
4114 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
4115 13);
4116 }
e2cbcd91 4117 else
418d4036
L
4118 {
4119 if (rel->r_offset < 3
4120 || (rel->r_offset - 3 + 12) > input_section->size)
4121 goto corrupt_input;
4122 memcpy (contents + rel->r_offset - 3,
4123 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
4124 }
e2cbcd91
L
4125 }
4126 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
4127 and R_X86_64_PLTOFF64. */
bffbf940 4128 rel++;
60f2e42e 4129 wrel++;
bffbf940
JJ
4130 continue;
4131 }
4132
6de2ae4a 4133 if (htab->elf.sgot == NULL)
bffbf940
JJ
4134 abort ();
4135
0afcef53 4136 off = htab->tls_ld_or_ldm_got.offset;
bffbf940
JJ
4137 if (off & 1)
4138 off &= ~1;
4139 else
4140 {
4141 Elf_Internal_Rela outrel;
bffbf940 4142
6de2ae4a 4143 if (htab->elf.srelgot == NULL)
bffbf940
JJ
4144 abort ();
4145
6de2ae4a
L
4146 outrel.r_offset = (htab->elf.sgot->output_section->vma
4147 + htab->elf.sgot->output_offset + off);
bffbf940
JJ
4148
4149 bfd_put_64 (output_bfd, 0,
6de2ae4a 4150 htab->elf.sgot->contents + off);
bffbf940 4151 bfd_put_64 (output_bfd, 0,
6de2ae4a 4152 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 4153 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
bffbf940 4154 outrel.r_addend = 0;
351f65ca 4155 elf_append_rela (output_bfd, htab->elf.srelgot,
464d3bd4 4156 &outrel);
0afcef53 4157 htab->tls_ld_or_ldm_got.offset |= 1;
bffbf940 4158 }
6de2ae4a
L
4159 relocation = htab->elf.sgot->output_section->vma
4160 + htab->elf.sgot->output_offset + off;
0a1b45a2 4161 unresolved_reloc = false;
bffbf940
JJ
4162 break;
4163
4164 case R_X86_64_DTPOFF32:
0e1862bb
L
4165 if (!bfd_link_executable (info)
4166 || (input_section->flags & SEC_CODE) == 0)
0afcef53 4167 relocation -= _bfd_x86_elf_dtpoff_base (info);
bffbf940 4168 else
351f65ca 4169 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
4170 break;
4171
4172 case R_X86_64_TPOFF32:
6769d501 4173 case R_X86_64_TPOFF64:
0e1862bb 4174 BFD_ASSERT (bfd_link_executable (info));
351f65ca 4175 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
4176 break;
4177
a69ed7f7
L
4178 case R_X86_64_DTPOFF64:
4179 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
0afcef53 4180 relocation -= _bfd_x86_elf_dtpoff_base (info);
a69ed7f7
L
4181 break;
4182
70256ad8
AJ
4183 default:
4184 break;
4185 }
8d88c4ca 4186
239e1f3a
AM
4187 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4188 because such sections are not SEC_ALLOC and thus ld.so will
4189 not process them. */
c434dee6 4190 if (unresolved_reloc
239e1f3a 4191 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
4192 && h->def_dynamic)
4193 && _bfd_elf_section_offset (output_bfd, info, input_section,
4194 rel->r_offset) != (bfd_vma) -1)
a040981f 4195 {
7073b5b9
L
4196 switch (r_type)
4197 {
4198 case R_X86_64_32S:
a5b4ee94 4199 sec = h->root.u.def.section;
f6386577 4200 if ((info->nocopyreloc || eh->def_protected)
7073b5b9
L
4201 && !(h->root.u.def.section->flags & SEC_CODE))
4202 return elf_x86_64_need_pic (info, input_bfd, input_section,
4203 h, NULL, NULL, howto);
4204 /* Fall through. */
4205
4206 default:
4207 _bfd_error_handler
4208 /* xgettext:c-format */
2dcf00ce
AM
4209 (_("%pB(%pA+%#" PRIx64 "): "
4210 "unresolvable %s relocation against symbol `%s'"),
7073b5b9
L
4211 input_bfd,
4212 input_section,
2dcf00ce 4213 (uint64_t) rel->r_offset,
7073b5b9
L
4214 howto->name,
4215 h->root.root.string);
0a1b45a2 4216 return false;
7073b5b9 4217 }
a040981f 4218 }
c434dee6 4219
dc1e8a47 4220 do_relocation:
8d88c4ca 4221 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
4222 contents, rel->r_offset,
4223 relocation, rel->r_addend);
8d88c4ca 4224
dc1e8a47 4225 check_relocation_error:
8d88c4ca 4226 if (r != bfd_reloc_ok)
8da6118f 4227 {
c434dee6
AJ
4228 const char *name;
4229
4230 if (h != NULL)
4231 name = h->root.root.string;
4232 else
8da6118f 4233 {
c434dee6
AJ
4234 name = bfd_elf_string_from_elf_section (input_bfd,
4235 symtab_hdr->sh_link,
4236 sym->st_name);
4237 if (name == NULL)
0a1b45a2 4238 return false;
c434dee6 4239 if (*name == '\0')
fd361982 4240 name = bfd_section_name (sec);
c434dee6
AJ
4241 }
4242
4243 if (r == bfd_reloc_overflow)
8b43e456 4244 {
78984959 4245 if (converted_reloc)
8b43e456
L
4246 {
4247 info->callbacks->einfo
1a2f1b54
L
4248 ("%X%H:", input_bfd, input_section, rel->r_offset);
4249 info->callbacks->einfo
4250 (_(" failed to convert GOTPCREL relocation against "
4251 "'%s'; relink with --no-relax\n"),
4252 name);
0a1b45a2 4253 status = false;
1a2f1b54 4254 continue;
8b43e456
L
4255 }
4256 (*info->callbacks->reloc_overflow)
4257 (info, (h ? &h->root : NULL), name, howto->name,
4258 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4259 }
c434dee6
AJ
4260 else
4261 {
4eca0228 4262 _bfd_error_handler
695344c0 4263 /* xgettext:c-format */
2dcf00ce 4264 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
d003868e 4265 input_bfd, input_section,
2dcf00ce 4266 (uint64_t) rel->r_offset, name, (int) r);
0a1b45a2 4267 return false;
8da6118f
KH
4268 }
4269 }
60f2e42e
L
4270
4271 if (wrel != rel)
4272 *wrel = *rel;
4273 }
4274
4275 if (wrel != rel)
4276 {
4277 Elf_Internal_Shdr *rel_hdr;
4278 size_t deleted = rel - wrel;
4279
4280 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
4281 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4282 if (rel_hdr->sh_size == 0)
4283 {
4284 /* It is too late to remove an empty reloc section. Leave
4285 one NONE reloc.
4286 ??? What is wrong with an empty section??? */
4287 rel_hdr->sh_size = rel_hdr->sh_entsize;
4288 deleted -= 1;
4289 }
4290 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
4291 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4292 input_section->reloc_count -= deleted;
8d88c4ca 4293 }
70256ad8 4294
1a2f1b54 4295 return status;
70256ad8
AJ
4296}
4297
4298/* Finish up dynamic symbol handling. We set the contents of various
4299 dynamic sections here. */
4300
0a1b45a2 4301static bool
351f65ca
L
4302elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
4303 struct bfd_link_info *info,
4304 struct elf_link_hash_entry *h,
aec6b87e 4305 Elf_Internal_Sym *sym)
70256ad8 4306{
0afcef53 4307 struct elf_x86_link_hash_table *htab;
0a1b45a2 4308 bool use_plt_second;
0afcef53 4309 struct elf_x86_link_hash_entry *eh;
0a1b45a2 4310 bool local_undefweak;
70256ad8 4311
0afcef53 4312 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4dfe6ac6 4313 if (htab == NULL)
0a1b45a2 4314 return false;
70256ad8 4315
f2c29a16
L
4316 /* Use the second PLT section only if there is .plt section. */
4317 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
0ff2b86e 4318
0afcef53 4319 eh = (struct elf_x86_link_hash_entry *) h;
9e9821dd
L
4320 if (eh->no_finish_dynamic_symbol)
4321 abort ();
dd7e64d4 4322
aec6b87e
L
4323 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
4324 resolved undefined weak symbols in executable so that their
4325 references have value 0 at run-time. */
c5bce5c6 4326 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
aec6b87e 4327
70256ad8
AJ
4328 if (h->plt.offset != (bfd_vma) -1)
4329 {
70256ad8 4330 bfd_vma plt_index;
38b12349 4331 bfd_vma got_offset, plt_offset;
70256ad8 4332 Elf_Internal_Rela rela;
947216bf 4333 bfd_byte *loc;
0ff2b86e 4334 asection *plt, *gotplt, *relplt, *resolved_plt;
351f65ca 4335 const struct elf_backend_data *bed;
5974eba6 4336 bfd_vma plt_got_pcrel_offset;
cbe950e9
L
4337
4338 /* When building a static executable, use .iplt, .igot.plt and
4339 .rela.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 4340 if (htab->elf.splt != NULL)
cbe950e9 4341 {
6de2ae4a
L
4342 plt = htab->elf.splt;
4343 gotplt = htab->elf.sgotplt;
4344 relplt = htab->elf.srelplt;
cbe950e9
L
4345 }
4346 else
4347 {
6de2ae4a
L
4348 plt = htab->elf.iplt;
4349 gotplt = htab->elf.igotplt;
4350 relplt = htab->elf.irelplt;
cbe950e9 4351 }
70256ad8 4352
f3180fa9 4353 VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
70256ad8
AJ
4354
4355 /* Get the index in the procedure linkage table which
4356 corresponds to this symbol. This is the index of this symbol
4357 in all the symbols for which we are making plt entries. The
cbe950e9 4358 first entry in the procedure linkage table is reserved.
6bbec505 4359
cbe950e9 4360 Get the offset into the .got table of the entry that
407443a3 4361 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
cbe950e9
L
4362 bytes. The first three are reserved for the dynamic linker.
4363
4364 For static executables, we don't reserve anything. */
4365
6de2ae4a 4366 if (plt == htab->elf.splt)
cbe950e9 4367 {
765e526c
L
4368 got_offset = (h->plt.offset / htab->plt.plt_entry_size
4369 - htab->plt.has_plt0);
e1f98742 4370 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
cbe950e9
L
4371 }
4372 else
4373 {
765e526c 4374 got_offset = h->plt.offset / htab->plt.plt_entry_size;
e1f98742 4375 got_offset = got_offset * GOT_ENTRY_SIZE;
cbe950e9 4376 }
70256ad8 4377
38b12349 4378 /* Fill in the entry in the procedure linkage table. */
765e526c
L
4379 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
4380 htab->plt.plt_entry_size);
f2c29a16 4381 if (use_plt_second)
0ff2b86e 4382 {
f2c29a16 4383 memcpy (htab->plt_second->contents + eh->plt_second.offset,
765e526c
L
4384 htab->non_lazy_plt->plt_entry,
4385 htab->non_lazy_plt->plt_entry_size);
0ff2b86e 4386
f2c29a16
L
4387 resolved_plt = htab->plt_second;
4388 plt_offset = eh->plt_second.offset;
0ff2b86e
L
4389 }
4390 else
4391 {
0ff2b86e
L
4392 resolved_plt = plt;
4393 plt_offset = h->plt.offset;
4394 }
eed180f8
RM
4395
4396 /* Insert the relocation positions of the plt section. */
4397
4398 /* Put offset the PC-relative instruction referring to the GOT entry,
4399 subtracting the size of that instruction. */
ab7fede8
L
4400 plt_got_pcrel_offset = (gotplt->output_section->vma
4401 + gotplt->output_offset
4402 + got_offset
4403 - resolved_plt->output_section->vma
4404 - resolved_plt->output_offset
4405 - plt_offset
765e526c 4406 - htab->plt.plt_got_insn_size);
ab7fede8
L
4407
4408 /* Check PC-relative offset overflow in PLT entry. */
5974eba6 4409 if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
695344c0 4410 /* xgettext:c-format */
871b3ab2 4411 info->callbacks->einfo (_("%F%pB: PC-relative offset overflow in PLT entry for `%s'\n"),
ab7fede8
L
4412 output_bfd, h->root.root.string);
4413
4414 bfd_put_32 (output_bfd, plt_got_pcrel_offset,
38b12349 4415 (resolved_plt->contents + plt_offset
765e526c 4416 + htab->plt.plt_got_offset));
cbe950e9 4417
653165cc 4418 /* Fill in the entry in the global offset table, initially this
aec6b87e
L
4419 points to the second part of the PLT entry. Leave the entry
4420 as zero for undefined weak symbol in PIE. No PLT relocation
4421 against undefined weak symbol in PIE. */
4422 if (!local_undefweak)
cbe950e9 4423 {
765e526c 4424 if (htab->plt.has_plt0)
38b12349
L
4425 bfd_put_64 (output_bfd, (plt->output_section->vma
4426 + plt->output_offset
4427 + h->plt.offset
765e526c 4428 + htab->lazy_plt->plt_lazy_offset),
38b12349 4429 gotplt->contents + got_offset);
aec6b87e
L
4430
4431 /* Fill in the entry in the .rela.plt section. */
4432 rela.r_offset = (gotplt->output_section->vma
4433 + gotplt->output_offset
4434 + got_offset);
cf1070f1 4435 if (PLT_LOCAL_IFUNC_P (info, h))
aec6b87e 4436 {
871b3ab2 4437 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
6322e5c5
L
4438 h->root.root.string,
4439 h->root.u.def.section->owner);
4440
aec6b87e
L
4441 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4442 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
4443 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4444 rela.r_addend = (h->root.u.def.value
4445 + h->root.u.def.section->output_section->vma
4446 + h->root.u.def.section->output_offset);
68b00778
L
4447
4448 if (htab->params->report_relative_reloc)
4449 _bfd_x86_elf_link_report_relative_reloc
4450 (info, relplt, h, sym, "R_X86_64_IRELATIVE", &rela);
4451
aec6b87e
L
4452 /* R_X86_64_IRELATIVE comes last. */
4453 plt_index = htab->next_irelative_index--;
4454 }
4455 else
4456 {
4457 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
4458 rela.r_addend = 0;
4459 plt_index = htab->next_jump_slot_index++;
4460 }
e1f98742 4461
38b12349
L
4462 /* Don't fill the second and third slots in PLT entry for
4463 static executables nor without PLT0. */
765e526c 4464 if (plt == htab->elf.splt && htab->plt.has_plt0)
aec6b87e 4465 {
38b12349 4466 bfd_vma plt0_offset
765e526c 4467 = h->plt.offset + htab->lazy_plt->plt_plt_insn_end;
aec6b87e
L
4468
4469 /* Put relocation index. */
4470 bfd_put_32 (output_bfd, plt_index,
4471 (plt->contents + h->plt.offset
765e526c 4472 + htab->lazy_plt->plt_reloc_offset));
aec6b87e
L
4473
4474 /* Put offset for jmp .PLT0 and check for overflow. We don't
4475 check relocation index for overflow since branch displacement
4476 will overflow first. */
4477 if (plt0_offset > 0x80000000)
695344c0 4478 /* xgettext:c-format */
871b3ab2 4479 info->callbacks->einfo (_("%F%pB: branch displacement overflow in PLT entry for `%s'\n"),
aec6b87e
L
4480 output_bfd, h->root.root.string);
4481 bfd_put_32 (output_bfd, - plt0_offset,
38b12349 4482 (plt->contents + h->plt.offset
765e526c 4483 + htab->lazy_plt->plt_plt_offset));
aec6b87e 4484 }
351f65ca 4485
aec6b87e
L
4486 bed = get_elf_backend_data (output_bfd);
4487 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
4488 bed->s->swap_reloca_out (output_bfd, &rela, loc);
4489 }
dd7e64d4
L
4490 }
4491 else if (eh->plt_got.offset != (bfd_vma) -1)
4492 {
38b12349 4493 bfd_vma got_offset, plt_offset;
dd7e64d4 4494 asection *plt, *got;
0a1b45a2 4495 bool got_after_plt;
dd7e64d4 4496 int32_t got_pcrel_offset;
dd7e64d4
L
4497
4498 /* Set the entry in the GOT procedure linkage table. */
4499 plt = htab->plt_got;
4500 got = htab->elf.sgot;
4501 got_offset = h->got.offset;
4502
4503 if (got_offset == (bfd_vma) -1
e492d2f8 4504 || (h->type == STT_GNU_IFUNC && h->def_regular)
dd7e64d4
L
4505 || plt == NULL
4506 || got == NULL)
4507 abort ();
70256ad8 4508
38b12349 4509 /* Use the non-lazy PLT entry template for the GOT PLT since they
dd7e64d4 4510 are the identical. */
dd7e64d4
L
4511 /* Fill in the entry in the GOT procedure linkage table. */
4512 plt_offset = eh->plt_got.offset;
4513 memcpy (plt->contents + plt_offset,
765e526c
L
4514 htab->non_lazy_plt->plt_entry,
4515 htab->non_lazy_plt->plt_entry_size);
dd7e64d4
L
4516
4517 /* Put offset the PC-relative instruction referring to the GOT
4518 entry, subtracting the size of that instruction. */
4519 got_pcrel_offset = (got->output_section->vma
4520 + got->output_offset
4521 + got_offset
4522 - plt->output_section->vma
4523 - plt->output_offset
4524 - plt_offset
765e526c 4525 - htab->non_lazy_plt->plt_got_insn_size);
dd7e64d4
L
4526
4527 /* Check PC-relative offset overflow in GOT PLT entry. */
4528 got_after_plt = got->output_section->vma > plt->output_section->vma;
4529 if ((got_after_plt && got_pcrel_offset < 0)
4530 || (!got_after_plt && got_pcrel_offset > 0))
695344c0 4531 /* xgettext:c-format */
871b3ab2 4532 info->callbacks->einfo (_("%F%pB: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
dd7e64d4
L
4533 output_bfd, h->root.root.string);
4534
4535 bfd_put_32 (output_bfd, got_pcrel_offset,
38b12349 4536 (plt->contents + plt_offset
765e526c 4537 + htab->non_lazy_plt->plt_got_offset));
dd7e64d4
L
4538 }
4539
aec6b87e
L
4540 if (!local_undefweak
4541 && !h->def_regular
dd7e64d4
L
4542 && (h->plt.offset != (bfd_vma) -1
4543 || eh->plt_got.offset != (bfd_vma) -1))
4544 {
4545 /* Mark the symbol as undefined, rather than as defined in
4546 the .plt section. Leave the value if there were any
4547 relocations where pointer equality matters (this is a clue
4548 for the dynamic linker, to make function pointer
4549 comparisons work between an application and shared
4550 library), otherwise set it to zero. If a function is only
4551 called from a binary, there is no need to slow down
4552 shared libraries because of that. */
4553 sym->st_shndx = SHN_UNDEF;
4554 if (!h->pointer_equality_needed)
4555 sym->st_value = 0;
70256ad8
AJ
4556 }
4557
4ec09950
L
4558 _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
4559
aec6b87e
L
4560 /* Don't generate dynamic GOT relocation against undefined weak
4561 symbol in executable. */
bffbf940 4562 if (h->got.offset != (bfd_vma) -1
0afcef53
L
4563 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
4564 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
aec6b87e 4565 && !local_undefweak)
053579d7 4566 {
053579d7 4567 Elf_Internal_Rela rela;
233cc9c1 4568 asection *relgot = htab->elf.srelgot;
68b00778 4569 const char *relative_reloc_name = NULL;
5af6f000 4570 bool generate_dynamic_reloc = true;
053579d7
AJ
4571
4572 /* This symbol has an entry in the global offset table. Set it
bffbf940 4573 up. */
6de2ae4a 4574 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
c434dee6 4575 abort ();
053579d7 4576
6de2ae4a
L
4577 rela.r_offset = (htab->elf.sgot->output_section->vma
4578 + htab->elf.sgot->output_offset
dc810e39 4579 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
4580
4581 /* If this is a static link, or it is a -Bsymbolic link and the
4582 symbol is defined locally or was forced to be local because
4583 of a version file, we just want to emit a RELATIVE reloc.
4584 The entry in the global offset table will already have been
4585 initialized in the relocate_section function. */
710ab287 4586 if (h->def_regular
0018b0a3
L
4587 && h->type == STT_GNU_IFUNC)
4588 {
233cc9c1
L
4589 if (h->plt.offset == (bfd_vma) -1)
4590 {
4591 /* STT_GNU_IFUNC is referenced without PLT. */
4592 if (htab->elf.splt == NULL)
4593 {
4594 /* use .rel[a].iplt section to store .got relocations
4595 in static executable. */
4596 relgot = htab->elf.irelplt;
4597 }
6999821f 4598 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
233cc9c1 4599 {
871b3ab2 4600 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
6322e5c5
L
4601 h->root.root.string,
4602 h->root.u.def.section->owner);
4603
233cc9c1
L
4604 rela.r_info = htab->r_info (0,
4605 R_X86_64_IRELATIVE);
4606 rela.r_addend = (h->root.u.def.value
4607 + h->root.u.def.section->output_section->vma
4608 + h->root.u.def.section->output_offset);
68b00778 4609 relative_reloc_name = "R_X86_64_IRELATIVE";
233cc9c1
L
4610 }
4611 else
4612 goto do_glob_dat;
4613 }
4614 else if (bfd_link_pic (info))
710ab287
L
4615 {
4616 /* Generate R_X86_64_GLOB_DAT. */
4617 goto do_glob_dat;
4618 }
4619 else
4620 {
90d60710 4621 asection *plt;
aab82f4c 4622 bfd_vma plt_offset;
90d60710 4623
710ab287
L
4624 if (!h->pointer_equality_needed)
4625 abort ();
4626
4627 /* For non-shared object, we can't use .got.plt, which
4628 contains the real function addres if we need pointer
4629 equality. We load the GOT entry with the PLT entry. */
f2c29a16 4630 if (htab->plt_second != NULL)
aab82f4c 4631 {
f2c29a16
L
4632 plt = htab->plt_second;
4633 plt_offset = eh->plt_second.offset;
aab82f4c
L
4634 }
4635 else
4636 {
4637 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4638 plt_offset = h->plt.offset;
4639 }
710ab287
L
4640 bfd_put_64 (output_bfd, (plt->output_section->vma
4641 + plt->output_offset
aab82f4c 4642 + plt_offset),
6de2ae4a 4643 htab->elf.sgot->contents + h->got.offset);
0a1b45a2 4644 return true;
710ab287 4645 }
0018b0a3 4646 }
0e1862bb 4647 else if (bfd_link_pic (info)
6999821f 4648 && SYMBOL_REFERENCES_LOCAL_P (info, h))
053579d7 4649 {
83924b38 4650 if (!SYMBOL_DEFINED_NON_SHARED_P (h))
0a1b45a2 4651 return false;
cc78d0af 4652 BFD_ASSERT((h->got.offset & 1) != 0);
5af6f000
L
4653 if (info->enable_dt_relr)
4654 generate_dynamic_reloc = false;
4655 else
4656 {
4657 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4658 rela.r_addend = (h->root.u.def.value
4659 + h->root.u.def.section->output_section->vma
4660 + h->root.u.def.section->output_offset);
4661 relative_reloc_name = "R_X86_64_RELATIVE";
4662 }
053579d7
AJ
4663 }
4664 else
4665 {
4666 BFD_ASSERT((h->got.offset & 1) == 0);
dc1e8a47 4667 do_glob_dat:
c434dee6 4668 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 4669 htab->elf.sgot->contents + h->got.offset);
351f65ca 4670 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
053579d7
AJ
4671 rela.r_addend = 0;
4672 }
4673
5af6f000
L
4674 if (generate_dynamic_reloc)
4675 {
4676 if (relative_reloc_name != NULL
4677 && htab->params->report_relative_reloc)
4678 _bfd_x86_elf_link_report_relative_reloc
4679 (info, relgot, h, sym, relative_reloc_name, &rela);
68b00778 4680
5af6f000
L
4681 elf_append_rela (output_bfd, relgot, &rela);
4682 }
053579d7
AJ
4683 }
4684
f5385ebf 4685 if (h->needs_copy)
70256ad8 4686 {
70256ad8 4687 Elf_Internal_Rela rela;
5474d94f 4688 asection *s;
70256ad8
AJ
4689
4690 /* This symbol needs a copy reloc. Set it up. */
ff38b4cc 4691 VERIFY_COPY_RELOC (h, htab)
70256ad8
AJ
4692
4693 rela.r_offset = (h->root.u.def.value
4694 + h->root.u.def.section->output_section->vma
4695 + h->root.u.def.section->output_offset);
351f65ca 4696 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
70256ad8 4697 rela.r_addend = 0;
afbf7e8e 4698 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
4699 s = htab->elf.sreldynrelro;
4700 else
4701 s = htab->elf.srelbss;
4702 elf_append_rela (output_bfd, s, &rela);
70256ad8
AJ
4703 }
4704
0a1b45a2 4705 return true;
70256ad8
AJ
4706}
4707
c25bc9fc
L
4708/* Finish up local dynamic symbol handling. We set the contents of
4709 various dynamic sections here. */
4710
1201fda6 4711static int
351f65ca 4712elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
c25bc9fc
L
4713{
4714 struct elf_link_hash_entry *h
4715 = (struct elf_link_hash_entry *) *slot;
4716 struct bfd_link_info *info
eed180f8 4717 = (struct bfd_link_info *) inf;
c25bc9fc 4718
351f65ca 4719 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5f0f0847 4720 info, h, NULL);
c25bc9fc
L
4721}
4722
aec6b87e
L
4723/* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4724 here since undefined weak symbol may not be dynamic and may not be
4725 called for elf_x86_64_finish_dynamic_symbol. */
4726
0a1b45a2 4727static bool
aec6b87e
L
4728elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4729 void *inf)
4730{
4731 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4732 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4733
4734 if (h->root.type != bfd_link_hash_undefweak
4735 || h->dynindx != -1)
0a1b45a2 4736 return true;
aec6b87e
L
4737
4738 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5f0f0847 4739 info, h, NULL);
aec6b87e
L
4740}
4741
c434dee6
AJ
4742/* Used to decide how to sort relocs in an optimal manner for the
4743 dynamic linker, before writing them out. */
4744
4745static enum elf_reloc_type_class
cae1fbbb 4746elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
7e612e98
AM
4747 const asection *rel_sec ATTRIBUTE_UNUSED,
4748 const Elf_Internal_Rela *rela)
c434dee6 4749{
cae1fbbb
L
4750 bfd *abfd = info->output_bfd;
4751 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0afcef53
L
4752 struct elf_x86_link_hash_table *htab
4753 = elf_x86_hash_table (info, X86_64_ELF_DATA);
cae1fbbb 4754
d9e3b590
L
4755 if (htab->elf.dynsym != NULL
4756 && htab->elf.dynsym->contents != NULL)
4757 {
4758 /* Check relocation against STT_GNU_IFUNC symbol if there are
07d6d2b8 4759 dynamic symbols. */
d9e3b590 4760 unsigned long r_symndx = htab->r_sym (rela->r_info);
897463b1
L
4761 if (r_symndx != STN_UNDEF)
4762 {
4763 Elf_Internal_Sym sym;
4764 if (!bed->s->swap_symbol_in (abfd,
4765 (htab->elf.dynsym->contents
4766 + r_symndx * bed->s->sizeof_sym),
4767 0, &sym))
4768 abort ();
d9e3b590 4769
897463b1
L
4770 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4771 return reloc_class_ifunc;
4772 }
d9e3b590 4773 }
cae1fbbb 4774
351f65ca 4775 switch ((int) ELF32_R_TYPE (rela->r_info))
c434dee6 4776 {
c428ce9d
L
4777 case R_X86_64_IRELATIVE:
4778 return reloc_class_ifunc;
c434dee6 4779 case R_X86_64_RELATIVE:
1da80baa 4780 case R_X86_64_RELATIVE64:
c434dee6
AJ
4781 return reloc_class_relative;
4782 case R_X86_64_JUMP_SLOT:
4783 return reloc_class_plt;
4784 case R_X86_64_COPY:
4785 return reloc_class_copy;
4786 default:
4787 return reloc_class_normal;
4788 }
4789}
4790
70256ad8
AJ
4791/* Finish up the dynamic sections. */
4792
0a1b45a2 4793static bool
351f65ca
L
4794elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4795 struct bfd_link_info *info)
70256ad8 4796{
0afcef53 4797 struct elf_x86_link_hash_table *htab;
70256ad8 4798
9577f60b 4799 htab = _bfd_x86_elf_finish_dynamic_sections (output_bfd, info);
4dfe6ac6 4800 if (htab == NULL)
0a1b45a2 4801 return false;
4dfe6ac6 4802
9577f60b 4803 if (! htab->elf.dynamic_sections_created)
0a1b45a2 4804 return true;
70256ad8 4805
9577f60b 4806 if (htab->elf.splt && htab->elf.splt->size > 0)
e41b3a13 4807 {
7c80aab9
L
4808 if (bfd_is_abs_section (htab->elf.splt->output_section))
4809 {
4810 info->callbacks->einfo
4811 (_("%F%P: discarded output section: `%pA'\n"),
4812 htab->elf.splt);
4813 return false;
4814 }
4815
9577f60b
L
4816 elf_section_data (htab->elf.splt->output_section)
4817 ->this_hdr.sh_entsize = htab->plt.plt_entry_size;
e41b3a13 4818
9577f60b 4819 if (htab->plt.has_plt0)
fff53dae 4820 {
9577f60b
L
4821 /* Fill in the special first entry in the procedure linkage
4822 table. */
4823 memcpy (htab->elf.splt->contents,
4824 htab->lazy_plt->plt0_entry,
4825 htab->lazy_plt->plt0_entry_size);
4826 /* Add offset for pushq GOT+8(%rip), since the instruction
4827 uses 6 bytes subtract this value. */
4828 bfd_put_32 (output_bfd,
4829 (htab->elf.sgotplt->output_section->vma
4830 + htab->elf.sgotplt->output_offset
4831 + 8
4832 - htab->elf.splt->output_section->vma
4833 - htab->elf.splt->output_offset
4834 - 6),
4835 (htab->elf.splt->contents
4836 + htab->lazy_plt->plt0_got1_offset));
4837 /* Add offset for the PC-relative instruction accessing
4838 GOT+16, subtracting the offset to the end of that
4839 instruction. */
4840 bfd_put_32 (output_bfd,
4841 (htab->elf.sgotplt->output_section->vma
4842 + htab->elf.sgotplt->output_offset
4843 + 16
4844 - htab->elf.splt->output_section->vma
4845 - htab->elf.splt->output_offset
4846 - htab->lazy_plt->plt0_got2_insn_end),
4847 (htab->elf.splt->contents
4848 + htab->lazy_plt->plt0_got2_offset));
fff53dae 4849 }
fff53dae 4850
9bcc30e4 4851 if (htab->elf.tlsdesc_plt)
8361ed4d 4852 {
9577f60b 4853 bfd_put_64 (output_bfd, (bfd_vma) 0,
9bcc30e4 4854 htab->elf.sgot->contents + htab->elf.tlsdesc_got);
9577f60b 4855
9bcc30e4 4856 memcpy (htab->elf.splt->contents + htab->elf.tlsdesc_plt,
92e68c1d
L
4857 htab->lazy_plt->plt_tlsdesc_entry,
4858 htab->lazy_plt->plt_tlsdesc_entry_size);
9577f60b 4859
bf54968b
L
4860 /* Add offset for pushq GOT+8(%rip), since ENDBR64 uses 4
4861 bytes and the instruction uses 6 bytes, subtract these
4862 values. */
9577f60b
L
4863 bfd_put_32 (output_bfd,
4864 (htab->elf.sgotplt->output_section->vma
4865 + htab->elf.sgotplt->output_offset
4866 + 8
4867 - htab->elf.splt->output_section->vma
4868 - htab->elf.splt->output_offset
9bcc30e4 4869 - htab->elf.tlsdesc_plt
92e68c1d 4870 - htab->lazy_plt->plt_tlsdesc_got1_insn_end),
9577f60b 4871 (htab->elf.splt->contents
9bcc30e4 4872 + htab->elf.tlsdesc_plt
92e68c1d 4873 + htab->lazy_plt->plt_tlsdesc_got1_offset));
bf54968b
L
4874 /* Add offset for indirect branch via GOT+TDG, where TDG
4875 stands for htab->tlsdesc_got, subtracting the offset
4876 to the end of that instruction. */
9577f60b
L
4877 bfd_put_32 (output_bfd,
4878 (htab->elf.sgot->output_section->vma
4879 + htab->elf.sgot->output_offset
9bcc30e4 4880 + htab->elf.tlsdesc_got
9577f60b
L
4881 - htab->elf.splt->output_section->vma
4882 - htab->elf.splt->output_offset
9bcc30e4 4883 - htab->elf.tlsdesc_plt
92e68c1d 4884 - htab->lazy_plt->plt_tlsdesc_got2_insn_end),
9577f60b 4885 (htab->elf.splt->contents
9bcc30e4 4886 + htab->elf.tlsdesc_plt
92e68c1d 4887 + htab->lazy_plt->plt_tlsdesc_got2_offset));
8361ed4d
L
4888 }
4889 }
4890
aec6b87e
L
4891 /* Fill PLT entries for undefined weak symbols in PIE. */
4892 if (bfd_link_pie (info))
4893 bfd_hash_traverse (&info->hash->table,
4894 elf_x86_64_pie_finish_undefweak_symbol,
4895 info);
4896
0a1b45a2 4897 return true;
8d88c4ca
NC
4898}
4899
233cc9c1
L
4900/* Fill PLT/GOT entries and allocate dynamic relocations for local
4901 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4902 It has to be done before elf_link_sort_relocs is called so that
4903 dynamic relocations are properly sorted. */
4904
0a1b45a2 4905static bool
233cc9c1
L
4906elf_x86_64_output_arch_local_syms
4907 (bfd *output_bfd ATTRIBUTE_UNUSED,
4908 struct bfd_link_info *info,
4909 void *flaginfo ATTRIBUTE_UNUSED,
4910 int (*func) (void *, const char *,
4911 Elf_Internal_Sym *,
4912 asection *,
4913 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4914{
0afcef53
L
4915 struct elf_x86_link_hash_table *htab
4916 = elf_x86_hash_table (info, X86_64_ELF_DATA);
233cc9c1 4917 if (htab == NULL)
0a1b45a2 4918 return false;
233cc9c1
L
4919
4920 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4921 htab_traverse (htab->loc_hash_table,
4922 elf_x86_64_finish_local_dynamic_symbol,
4923 info);
4924
0a1b45a2 4925 return true;
233cc9c1
L
4926}
4927
38b12349
L
4928/* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4929 dynamic relocations. */
4930
4931static long
4932elf_x86_64_get_synthetic_symtab (bfd *abfd,
4933 long symcount ATTRIBUTE_UNUSED,
4934 asymbol **syms ATTRIBUTE_UNUSED,
4935 long dynsymcount,
4936 asymbol **dynsyms,
4937 asymbol **ret)
4938{
f493882d 4939 long count, i, n;
38b12349 4940 int j;
38b12349 4941 bfd_byte *plt_contents;
f493882d 4942 long relsize;
765e526c
L
4943 const struct elf_x86_lazy_plt_layout *lazy_plt;
4944 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4945 const struct elf_x86_lazy_plt_layout *lazy_bnd_plt;
4946 const struct elf_x86_non_lazy_plt_layout *non_lazy_bnd_plt;
4947 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4948 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
e9c11d58
L
4949 const struct elf_x86_lazy_plt_layout *x32_lazy_ibt_plt;
4950 const struct elf_x86_non_lazy_plt_layout *x32_non_lazy_ibt_plt;
38b12349 4951 asection *plt;
f493882d
L
4952 enum elf_x86_plt_type plt_type;
4953 struct elf_x86_plt plts[] =
144bed8d 4954 {
38b12349
L
4955 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4956 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
f2c29a16
L
4957 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4958 { ".plt.bnd", NULL, NULL, plt_second, 0, 0, 0, 0 },
dd9e66ee 4959 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
38b12349 4960 };
144bed8d 4961
38b12349 4962 *ret = NULL;
144bed8d 4963
38b12349
L
4964 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4965 return 0;
144bed8d 4966
38b12349
L
4967 if (dynsymcount <= 0)
4968 return 0;
cca5b8b6 4969
38b12349
L
4970 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4971 if (relsize <= 0)
4972 return -1;
4973
bbd19b19
L
4974 lazy_plt = &elf_x86_64_lazy_plt;
4975 non_lazy_plt = &elf_x86_64_non_lazy_plt;
4976 lazy_bnd_plt = &elf_x86_64_lazy_bnd_plt;
4977 non_lazy_bnd_plt = &elf_x86_64_non_lazy_bnd_plt;
4978 if (ABI_64_P (abfd))
144bed8d 4979 {
bbd19b19
L
4980 lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
4981 non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
e9c11d58
L
4982 x32_lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
4983 x32_non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
38b12349
L
4984 }
4985 else
4986 {
bbd19b19
L
4987 lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
4988 non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
e9c11d58
L
4989 x32_lazy_ibt_plt = NULL;
4990 x32_non_lazy_ibt_plt = NULL;
38b12349 4991 }
144bed8d 4992
38b12349
L
4993 count = 0;
4994 for (j = 0; plts[j].name != NULL; j++)
4995 {
4996 plt = bfd_get_section_by_name (abfd, plts[j].name);
81ff113f
AM
4997 if (plt == NULL
4998 || plt->size == 0
4999 || (plt->flags & SEC_HAS_CONTENTS) == 0)
6f25f223 5000 continue;
533d0af0 5001
38b12349 5002 /* Get the PLT section contents. */
b5f386d5 5003 if (!bfd_malloc_and_get_section (abfd, plt, &plt_contents))
38b12349 5004 break;
144bed8d 5005
38b12349
L
5006 /* Check what kind of PLT it is. */
5007 plt_type = plt_unknown;
90efb642
L
5008 if (plts[j].type == plt_unknown
5009 && (plt->size >= (lazy_plt->plt_entry_size
5010 + lazy_plt->plt_entry_size)))
144bed8d 5011 {
38b12349
L
5012 /* Match lazy PLT first. Need to check the first two
5013 instructions. */
5014 if ((memcmp (plt_contents, lazy_plt->plt0_entry,
5015 lazy_plt->plt0_got1_offset) == 0)
5016 && (memcmp (plt_contents + 6, lazy_plt->plt0_entry + 6,
5017 2) == 0))
e9c11d58
L
5018 {
5019 if (x32_lazy_ibt_plt != NULL
5020 && (memcmp (plt_contents
5021 + x32_lazy_ibt_plt->plt_entry_size,
5022 x32_lazy_ibt_plt->plt_entry,
5023 x32_lazy_ibt_plt->plt_got_offset) == 0))
5024 {
5025 /* The fist entry in the x32 lazy IBT PLT is the same
5026 as the lazy PLT. */
5027 plt_type = plt_lazy | plt_second;
5028 lazy_plt = x32_lazy_ibt_plt;
5029 }
5030 else
5031 plt_type = plt_lazy;
5032 }
38b12349
L
5033 else if (lazy_bnd_plt != NULL
5034 && (memcmp (plt_contents, lazy_bnd_plt->plt0_entry,
5035 lazy_bnd_plt->plt0_got1_offset) == 0)
5036 && (memcmp (plt_contents + 6,
5037 lazy_bnd_plt->plt0_entry + 6, 3) == 0))
ec1f73bb 5038 {
f2c29a16 5039 plt_type = plt_lazy | plt_second;
ee2fdd6f
L
5040 /* The fist entry in the lazy IBT PLT is the same as the
5041 lazy BND PLT. */
5042 if ((memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
5043 lazy_ibt_plt->plt_entry,
5044 lazy_ibt_plt->plt_got_offset) == 0))
5045 lazy_plt = lazy_ibt_plt;
5046 else
5047 lazy_plt = lazy_bnd_plt;
ec1f73bb 5048 }
144bed8d 5049 }
fca6ae69 5050
38b12349 5051 if (non_lazy_plt != NULL
90efb642
L
5052 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
5053 && plt->size >= non_lazy_plt->plt_entry_size)
38b12349
L
5054 {
5055 /* Match non-lazy PLT. */
5056 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
5057 non_lazy_plt->plt_got_offset) == 0)
5058 plt_type = plt_non_lazy;
5059 }
5060
ee2fdd6f 5061 if (plt_type == plt_unknown || plt_type == plt_second)
38b12349 5062 {
ee2fdd6f 5063 if (non_lazy_bnd_plt != NULL
90efb642 5064 && plt->size >= non_lazy_bnd_plt->plt_entry_size
ee2fdd6f
L
5065 && (memcmp (plt_contents, non_lazy_bnd_plt->plt_entry,
5066 non_lazy_bnd_plt->plt_got_offset) == 0))
38b12349 5067 {
ee2fdd6f 5068 /* Match BND PLT. */
f2c29a16 5069 plt_type = plt_second;
38b12349
L
5070 non_lazy_plt = non_lazy_bnd_plt;
5071 }
ee2fdd6f 5072 else if (non_lazy_ibt_plt != NULL
90efb642 5073 && plt->size >= non_lazy_ibt_plt->plt_entry_size
ee2fdd6f
L
5074 && (memcmp (plt_contents,
5075 non_lazy_ibt_plt->plt_entry,
5076 non_lazy_ibt_plt->plt_got_offset) == 0))
5077 {
5078 /* Match IBT PLT. */
5079 plt_type = plt_second;
5080 non_lazy_plt = non_lazy_ibt_plt;
5081 }
e9c11d58
L
5082 else if (x32_non_lazy_ibt_plt != NULL
5083 && plt->size >= x32_non_lazy_ibt_plt->plt_entry_size
5084 && (memcmp (plt_contents,
5085 x32_non_lazy_ibt_plt->plt_entry,
5086 x32_non_lazy_ibt_plt->plt_got_offset) == 0))
5087 {
5088 /* Match x32 IBT PLT. */
5089 plt_type = plt_second;
5090 non_lazy_plt = x32_non_lazy_ibt_plt;
5091 }
38b12349
L
5092 }
5093
5094 if (plt_type == plt_unknown)
37c0b6ee
L
5095 {
5096 free (plt_contents);
5097 continue;
5098 }
38b12349
L
5099
5100 plts[j].sec = plt;
5101 plts[j].type = plt_type;
5102
5103 if ((plt_type & plt_lazy))
5104 {
5105 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
5106 plts[j].plt_got_insn_size = lazy_plt->plt_got_insn_size;
5107 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
5108 /* Skip PLT0 in lazy PLT. */
5109 i = 1;
5110 }
5111 else
5112 {
5113 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
5114 plts[j].plt_got_insn_size = non_lazy_plt->plt_got_insn_size;
5115 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
5116 i = 0;
5117 }
5118
f2c29a16
L
5119 /* Skip lazy PLT when the second PLT is used. */
5120 if (plt_type == (plt_lazy | plt_second))
38b12349
L
5121 plts[j].count = 0;
5122 else
5123 {
5124 n = plt->size / plts[j].plt_entry_size;
5125 plts[j].count = n;
5126 count += n - i;
5127 }
5128
5129 plts[j].contents = plt_contents;
144bed8d
L
5130 }
5131
f493882d
L
5132 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
5133 (bfd_vma) 0, plts, dynsyms,
5134 ret);
0ff2b86e
L
5135}
5136
d2b2c203
DJ
5137/* Handle an x86-64 specific section when reading an object file. This
5138 is called when elfcode.h finds a section with an unknown type. */
5139
0a1b45a2 5140static bool
0c723101
L
5141elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
5142 const char *name, int shindex)
d2b2c203
DJ
5143{
5144 if (hdr->sh_type != SHT_X86_64_UNWIND)
0a1b45a2 5145 return false;
d2b2c203 5146
6dc132d9 5147 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
0a1b45a2 5148 return false;
d2b2c203 5149
0a1b45a2 5150 return true;
d2b2c203
DJ
5151}
5152
3b22753a
L
5153/* Hook called by the linker routine which adds symbols from an object
5154 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
5155 of .bss. */
5156
0a1b45a2 5157static bool
351f65ca 5158elf_x86_64_add_symbol_hook (bfd *abfd,
a43942db 5159 struct bfd_link_info *info ATTRIBUTE_UNUSED,
351f65ca
L
5160 Elf_Internal_Sym *sym,
5161 const char **namep ATTRIBUTE_UNUSED,
5162 flagword *flagsp ATTRIBUTE_UNUSED,
5163 asection **secp,
5164 bfd_vma *valp)
3b22753a
L
5165{
5166 asection *lcomm;
5167
5168 switch (sym->st_shndx)
5169 {
5170 case SHN_X86_64_LCOMMON:
5171 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
5172 if (lcomm == NULL)
5173 {
5174 lcomm = bfd_make_section_with_flags (abfd,
5175 "LARGE_COMMON",
5176 (SEC_ALLOC
5177 | SEC_IS_COMMON
5178 | SEC_LINKER_CREATED));
5179 if (lcomm == NULL)
0a1b45a2 5180 return false;
3b22753a
L
5181 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
5182 }
5183 *secp = lcomm;
5184 *valp = sym->st_size;
0a1b45a2 5185 return true;
3b22753a 5186 }
d8045f23 5187
0a1b45a2 5188 return true;
3b22753a
L
5189}
5190
5191
5192/* Given a BFD section, try to locate the corresponding ELF section
5193 index. */
5194
0a1b45a2 5195static bool
351f65ca
L
5196elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5197 asection *sec, int *index_return)
3b22753a
L
5198{
5199 if (sec == &_bfd_elf_large_com_section)
5200 {
91d6fa6a 5201 *index_return = SHN_X86_64_LCOMMON;
0a1b45a2 5202 return true;
3b22753a 5203 }
0a1b45a2 5204 return false;
3b22753a
L
5205}
5206
5207/* Process a symbol. */
5208
5209static void
351f65ca
L
5210elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5211 asymbol *asym)
3b22753a
L
5212{
5213 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5214
5215 switch (elfsym->internal_elf_sym.st_shndx)
5216 {
5217 case SHN_X86_64_LCOMMON:
5218 asym->section = &_bfd_elf_large_com_section;
5219 asym->value = elfsym->internal_elf_sym.st_size;
5220 /* Common symbol doesn't set BSF_GLOBAL. */
5221 asym->flags &= ~BSF_GLOBAL;
5222 break;
5223 }
5224}
5225
0a1b45a2 5226static bool
351f65ca 5227elf_x86_64_common_definition (Elf_Internal_Sym *sym)
3b22753a
L
5228{
5229 return (sym->st_shndx == SHN_COMMON
5230 || sym->st_shndx == SHN_X86_64_LCOMMON);
5231}
5232
5233static unsigned int
351f65ca 5234elf_x86_64_common_section_index (asection *sec)
3b22753a
L
5235{
5236 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5237 return SHN_COMMON;
5238 else
5239 return SHN_X86_64_LCOMMON;
5240}
5241
5242static asection *
351f65ca 5243elf_x86_64_common_section (asection *sec)
3b22753a
L
5244{
5245 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5246 return bfd_com_section_ptr;
5247 else
5248 return &_bfd_elf_large_com_section;
5249}
5250
0a1b45a2 5251static bool
5d13b3b3
AM
5252elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
5253 const Elf_Internal_Sym *sym,
351f65ca 5254 asection **psec,
0a1b45a2
AM
5255 bool newdef,
5256 bool olddef,
351f65ca 5257 bfd *oldbfd,
5d13b3b3 5258 const asection *oldsec)
3b22753a
L
5259{
5260 /* A normal common symbol and a large common symbol result in a
00492999
L
5261 normal common symbol. We turn the large common symbol into a
5262 normal one. */
5d13b3b3 5263 if (!olddef
3b22753a 5264 && h->root.type == bfd_link_hash_common
5d13b3b3
AM
5265 && !newdef
5266 && bfd_is_com_section (*psec)
5267 && oldsec != *psec)
3b22753a 5268 {
00492999 5269 if (sym->st_shndx == SHN_COMMON
5d13b3b3 5270 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
00492999
L
5271 {
5272 h->root.u.c.p->section
5273 = bfd_make_section_old_way (oldbfd, "COMMON");
5274 h->root.u.c.p->section->flags = SEC_ALLOC;
5275 }
5276 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5d13b3b3
AM
5277 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
5278 *psec = bfd_com_section_ptr;
3b22753a
L
5279 }
5280
0a1b45a2 5281 return true;
3b22753a
L
5282}
5283
5284static int
351f65ca
L
5285elf_x86_64_additional_program_headers (bfd *abfd,
5286 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3b22753a
L
5287{
5288 asection *s;
9a2e389a 5289 int count = 0;
3b22753a
L
5290
5291 /* Check to see if we need a large readonly segment. */
5292 s = bfd_get_section_by_name (abfd, ".lrodata");
5293 if (s && (s->flags & SEC_LOAD))
5294 count++;
5295
5296 /* Check to see if we need a large data segment. Since .lbss sections
5297 is placed right after the .bss section, there should be no need for
5298 a large data segment just because of .lbss. */
5299 s = bfd_get_section_by_name (abfd, ".ldata");
5300 if (s && (s->flags & SEC_LOAD))
5301 count++;
5302
5303 return count;
5304}
5305
c543bf9a
L
5306/* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5307
0a1b45a2 5308static bool
c543bf9a
L
5309elf_x86_64_relocs_compatible (const bfd_target *input,
5310 const bfd_target *output)
5311{
5312 return ((xvec_get_elf_backend_data (input)->s->elfclass
5313 == xvec_get_elf_backend_data (output)->s->elfclass)
5314 && _bfd_elf_relocs_compatible (input, output));
5315}
5316
38b12349
L
5317/* Set up x86-64 GNU properties. Return the first relocatable ELF input
5318 with GNU properties if found. Otherwise, return NULL. */
5319
5320static bfd *
5321elf_x86_64_link_setup_gnu_properties (struct bfd_link_info *info)
5322{
1de031c8 5323 struct elf_x86_init_table init_table;
bbd19b19
L
5324 const struct elf_backend_data *bed;
5325 struct elf_x86_link_hash_table *htab;
ee2fdd6f 5326
78984959
L
5327 if ((int) R_X86_64_standard >= (int) R_X86_64_converted_reloc_bit
5328 || (int) R_X86_64_max <= (int) R_X86_64_converted_reloc_bit
5329 || ((int) (R_X86_64_GNU_VTINHERIT | R_X86_64_converted_reloc_bit)
5330 != (int) R_X86_64_GNU_VTINHERIT)
5331 || ((int) (R_X86_64_GNU_VTENTRY | R_X86_64_converted_reloc_bit)
5332 != (int) R_X86_64_GNU_VTENTRY))
5333 abort ();
5334
851b6fa1
L
5335 /* This is unused for x86-64. */
5336 init_table.plt0_pad_byte = 0x90;
5337
bbd19b19
L
5338 bed = get_elf_backend_data (info->output_bfd);
5339 htab = elf_x86_hash_table (info, bed->target_id);
5340 if (!htab)
5341 abort ();
8ad93045
ML
5342
5343 init_table.lazy_plt = &elf_x86_64_lazy_plt;
5344 init_table.non_lazy_plt = &elf_x86_64_non_lazy_plt;
38b12349 5345
e9c11d58
L
5346 init_table.lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
5347 init_table.non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
38b12349 5348
cf0e0a0b
IB
5349 if (ABI_64_P (info->output_bfd))
5350 {
5351 init_table.sframe_lazy_plt = &elf_x86_64_sframe_plt;
5352 init_table.sframe_non_lazy_plt = &elf_x86_64_sframe_non_lazy_plt;
5353 init_table.sframe_lazy_ibt_plt = &elf_x86_64_sframe_plt;
5354 init_table.sframe_non_lazy_ibt_plt = &elf_x86_64_sframe_non_lazy_plt;
5355 }
5356 else
5357 {
5358 /* SFrame is not supported for non AMD64. */
5359 init_table.sframe_lazy_plt = NULL;
5360 init_table.sframe_non_lazy_plt = NULL;
5361 }
5362
7a382c1c
L
5363 if (ABI_64_P (info->output_bfd))
5364 {
1de031c8
L
5365 init_table.r_info = elf64_r_info;
5366 init_table.r_sym = elf64_r_sym;
7a382c1c
L
5367 }
5368 else
5369 {
1de031c8
L
5370 init_table.r_info = elf32_r_info;
5371 init_table.r_sym = elf32_r_sym;
7a382c1c
L
5372 }
5373
1de031c8 5374 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
38b12349
L
5375}
5376
9a2e389a 5377static const struct bfd_elf_special_section
46bed679 5378elf_x86_64_special_sections[]=
3b22753a 5379{
0112cd26
NC
5380 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5381 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5382 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
07d6d2b8 5383 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
0112cd26
NC
5384 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5385 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
07d6d2b8 5386 { NULL, 0, 0, 0, 0 }
3b22753a
L
5387};
5388
6d00b590 5389#define TARGET_LITTLE_SYM x86_64_elf64_vec
70256ad8
AJ
5390#define TARGET_LITTLE_NAME "elf64-x86-64"
5391#define ELF_ARCH bfd_arch_i386
ae95ffa6 5392#define ELF_TARGET_ID X86_64_ELF_DATA
70256ad8 5393#define ELF_MACHINE_CODE EM_X86_64
a2267dbf 5394#define ELF_MAXPAGESIZE 0x1000
24718e3b 5395#define ELF_COMMONPAGESIZE 0x1000
70256ad8
AJ
5396
5397#define elf_backend_can_gc_sections 1
51b64d56 5398#define elf_backend_can_refcount 1
70256ad8
AJ
5399#define elf_backend_want_got_plt 1
5400#define elf_backend_plt_readonly 1
5401#define elf_backend_want_plt_sym 0
5402#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 5403#define elf_backend_rela_normal 1
07d6d2b8 5404#define elf_backend_plt_alignment 4
bedfd056 5405#define elf_backend_caches_rawsize 1
64f52338 5406#define elf_backend_dtrel_excludes_plt 1
5474d94f 5407#define elf_backend_want_dynrelro 1
70256ad8 5408
351f65ca 5409#define elf_info_to_howto elf_x86_64_info_to_howto
70256ad8 5410
351f65ca 5411#define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
157090f7 5412#define bfd_elf64_bfd_reloc_name_lookup \
351f65ca 5413 elf_x86_64_reloc_name_lookup
70256ad8 5414
c543bf9a 5415#define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
3747999c 5416#define elf_backend_always_size_sections elf_x86_64_always_size_sections
38b12349 5417#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
351f65ca
L
5418#define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5419#define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
233cc9c1 5420#define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
351f65ca
L
5421#define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5422#define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
8fd79e71
L
5423#ifdef CORE_HEADER
5424#define elf_backend_write_core_note elf_x86_64_write_core_note
5425#endif
351f65ca
L
5426#define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5427#define elf_backend_relocate_section elf_x86_64_relocate_section
74541ad4 5428#define elf_backend_init_index_section _bfd_elf_init_1_index_section
407443a3 5429#define elf_backend_object_p elf64_x86_64_elf_object_p
0ff2b86e 5430#define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
8d88c4ca 5431
d2b2c203 5432#define elf_backend_section_from_shdr \
351f65ca 5433 elf_x86_64_section_from_shdr
d2b2c203 5434
3b22753a 5435#define elf_backend_section_from_bfd_section \
351f65ca 5436 elf_x86_64_elf_section_from_bfd_section
3b22753a 5437#define elf_backend_add_symbol_hook \
351f65ca 5438 elf_x86_64_add_symbol_hook
3b22753a 5439#define elf_backend_symbol_processing \
351f65ca 5440 elf_x86_64_symbol_processing
3b22753a 5441#define elf_backend_common_section_index \
351f65ca 5442 elf_x86_64_common_section_index
3b22753a 5443#define elf_backend_common_section \
351f65ca 5444 elf_x86_64_common_section
3b22753a 5445#define elf_backend_common_definition \
351f65ca 5446 elf_x86_64_common_definition
3b22753a 5447#define elf_backend_merge_symbol \
351f65ca 5448 elf_x86_64_merge_symbol
3b22753a 5449#define elf_backend_special_sections \
351f65ca 5450 elf_x86_64_special_sections
3b22753a 5451#define elf_backend_additional_program_headers \
351f65ca 5452 elf_x86_64_additional_program_headers
38b12349 5453#define elf_backend_setup_gnu_properties \
9f857535
L
5454 elf_x86_64_link_setup_gnu_properties
5455#define elf_backend_hide_symbol \
5456 _bfd_x86_elf_hide_symbol
3b22753a 5457
8f56f7a2
L
5458#undef elf64_bed
5459#define elf64_bed elf64_x86_64_bed
5460
8d88c4ca 5461#include "elf64-target.h"
9d7cbccd 5462
6036f486
ES
5463/* CloudABI support. */
5464
07d6d2b8 5465#undef TARGET_LITTLE_SYM
6036f486 5466#define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
07d6d2b8 5467#undef TARGET_LITTLE_NAME
6036f486
ES
5468#define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
5469
5470#undef ELF_OSABI
5471#define ELF_OSABI ELFOSABI_CLOUDABI
5472
07d6d2b8 5473#undef elf64_bed
6036f486
ES
5474#define elf64_bed elf64_x86_64_cloudabi_bed
5475
5476#include "elf64-target.h"
5477
9d7cbccd
NC
5478/* FreeBSD support. */
5479
07d6d2b8 5480#undef TARGET_LITTLE_SYM
6d00b590 5481#define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
07d6d2b8 5482#undef TARGET_LITTLE_NAME
9d7cbccd
NC
5483#define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5484
d1036acb
L
5485#undef ELF_OSABI
5486#define ELF_OSABI ELFOSABI_FREEBSD
9d7cbccd 5487
07d6d2b8 5488#undef elf64_bed
9d7cbccd
NC
5489#define elf64_bed elf64_x86_64_fbsd_bed
5490
5491#include "elf64-target.h"
8a9036a4 5492
a6cc6b3b
RO
5493/* Solaris 2 support. */
5494
5495#undef TARGET_LITTLE_SYM
6d00b590 5496#define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
a6cc6b3b
RO
5497#undef TARGET_LITTLE_NAME
5498#define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5499
90c14f0c
L
5500#undef ELF_TARGET_OS
5501#define ELF_TARGET_OS is_solaris
3b4c3844 5502
a6cc6b3b
RO
5503/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5504 objects won't be recognized. */
5505#undef ELF_OSABI
5506
5507#undef elf64_bed
5508#define elf64_bed elf64_x86_64_sol2_bed
5509
7dc98aea
RO
5510/* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5511 boundary. */
84865015 5512#undef elf_backend_static_tls_alignment
7dc98aea
RO
5513#define elf_backend_static_tls_alignment 16
5514
a6cc6b3b
RO
5515/* The Solaris 2 ABI requires a plt symbol on all platforms.
5516
5517 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5518 File, p.63. */
84865015 5519#undef elf_backend_want_plt_sym
a6cc6b3b
RO
5520#define elf_backend_want_plt_sym 1
5521
84865015
NC
5522#undef elf_backend_strtab_flags
5523#define elf_backend_strtab_flags SHF_STRINGS
5524
0a1b45a2 5525static bool
5522f910
NC
5526elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
5527 bfd *obfd ATTRIBUTE_UNUSED,
5528 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
5529 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
84865015
NC
5530{
5531 /* PR 19938: FIXME: Need to add code for setting the sh_info
5532 and sh_link fields of Solaris specific section types. */
0a1b45a2 5533 return false;
84865015
NC
5534}
5535
5522f910
NC
5536#undef elf_backend_copy_special_section_fields
5537#define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
84865015 5538
a6cc6b3b
RO
5539#include "elf64-target.h"
5540
8059fb19
RM
5541/* Restore defaults. */
5542#undef ELF_OSABI
5543#undef elf_backend_static_tls_alignment
5544#undef elf_backend_want_plt_sym
5545#define elf_backend_want_plt_sym 0
84865015 5546#undef elf_backend_strtab_flags
5522f910 5547#undef elf_backend_copy_special_section_fields
8059fb19 5548
351f65ca
L
5549/* 32bit x86-64 support. */
5550
351f65ca 5551#undef TARGET_LITTLE_SYM
6d00b590 5552#define TARGET_LITTLE_SYM x86_64_elf32_vec
351f65ca
L
5553#undef TARGET_LITTLE_NAME
5554#define TARGET_LITTLE_NAME "elf32-x86-64"
8059fb19 5555#undef elf32_bed
bbd19b19 5556#define elf32_bed elf32_x86_64_bed
351f65ca
L
5557
5558#undef ELF_ARCH
5559#define ELF_ARCH bfd_arch_i386
5560
5561#undef ELF_MACHINE_CODE
5562#define ELF_MACHINE_CODE EM_X86_64
5563
801a7eab 5564#undef ELF_TARGET_OS
351f65ca
L
5565#undef ELF_OSABI
5566
bbd19b19
L
5567#define bfd_elf32_bfd_reloc_type_lookup \
5568 elf_x86_64_reloc_type_lookup
5569#define bfd_elf32_bfd_reloc_name_lookup \
5570 elf_x86_64_reloc_name_lookup
5571#define bfd_elf32_get_synthetic_symtab \
5572 elf_x86_64_get_synthetic_symtab
5573
351f65ca
L
5574#undef elf_backend_object_p
5575#define elf_backend_object_p \
5576 elf32_x86_64_elf_object_p
5577
5578#undef elf_backend_bfd_from_remote_memory
5579#define elf_backend_bfd_from_remote_memory \
5580 _bfd_elf32_bfd_from_remote_memory
5581
5582#undef elf_backend_size_info
5583#define elf_backend_size_info \
5584 _bfd_elf32_size_info
5585
5586#include "elf32-target.h"