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