]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-x86-64.c
Better ld --fatal-warnings support
[thirdparty/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
351f65ca 1/* X86-64 specific support for ELF
6f2750fe 2 Copyright (C) 2000-2016 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
8d88c4ca 22#include "sysdep.h"
3db64b00 23#include "bfd.h"
c434dee6 24#include "bfdlink.h"
8d88c4ca
NC
25#include "libbfd.h"
26#include "elf-bfd.h"
5a68afcf 27#include "elf-nacl.h"
142411ca 28#include "bfd_stdint.h"
c25bc9fc
L
29#include "objalloc.h"
30#include "hashtab.h"
e41b3a13 31#include "dwarf2.h"
d7921315 32#include "libiberty.h"
8d88c4ca 33
56ceb5b5 34#include "opcode/i386.h"
8d88c4ca
NC
35#include "elf/x86-64.h"
36
8fd79e71
L
37#ifdef CORE_HEADER
38#include <stdarg.h>
39#include CORE_HEADER
40#endif
41
8d88c4ca
NC
42/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
43#define MINUS_ONE (~ (bfd_vma) 0)
44
351f65ca
L
45/* Since both 32-bit and 64-bit x86-64 encode relocation type in the
46 identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
47 relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
48 since they are the same. */
49
50#define ABI_64_P(abfd) \
51 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
52
8d88c4ca 53/* The relocation "howto" table. Order of fields:
7b81dfbb
AJ
54 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
55 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
70256ad8
AJ
56static reloc_howto_type x86_64_elf_howto_table[] =
57{
6346d5ca 58 HOWTO(R_X86_64_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
b34976b6
AM
59 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
60 FALSE),
61 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
63 FALSE),
64 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
65 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
66 TRUE),
67 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
68 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
69 FALSE),
70 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
71 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
72 TRUE),
73 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
74 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
75 FALSE),
76 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
78 MINUS_ONE, FALSE),
79 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
80 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
81 MINUS_ONE, FALSE),
82 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
84 MINUS_ONE, FALSE),
85 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
86 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
87 0xffffffff, TRUE),
88 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
89 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
90 FALSE),
91 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
92 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
93 FALSE),
94 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
95 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
b0360d8c 96 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
b34976b6 97 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
ac2aa337 98 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
b34976b6
AM
99 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
100 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
101 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
102 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
103 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
104 MINUS_ONE, FALSE),
105 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
106 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
107 MINUS_ONE, FALSE),
108 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
109 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
110 MINUS_ONE, FALSE),
111 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
112 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
113 0xffffffff, TRUE),
114 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
115 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
116 0xffffffff, TRUE),
ac2aa337 117 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
b34976b6
AM
118 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
119 0xffffffff, FALSE),
120 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
121 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
122 0xffffffff, TRUE),
123 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
124 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
125 0xffffffff, FALSE),
d6ab8113
JB
126 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
127 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
128 TRUE),
129 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
130 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
131 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
132 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
133 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
134 FALSE, 0xffffffff, 0xffffffff, TRUE),
7b81dfbb
AJ
135 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
136 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
137 FALSE),
138 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
139 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
140 MINUS_ONE, TRUE),
141 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
142 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
143 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
144 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
145 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
146 MINUS_ONE, FALSE),
147 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
148 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
149 MINUS_ONE, FALSE),
1788fc08
L
150 HOWTO(R_X86_64_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
151 bfd_elf_generic_reloc, "R_X86_64_SIZE32", FALSE, 0xffffffff, 0xffffffff,
152 FALSE),
153 HOWTO(R_X86_64_SIZE64, 0, 4, 64, FALSE, 0, complain_overflow_unsigned,
154 bfd_elf_generic_reloc, "R_X86_64_SIZE64", FALSE, MINUS_ONE, MINUS_ONE,
155 FALSE),
67a4f2b7
AO
156 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
157 complain_overflow_bitfield, bfd_elf_generic_reloc,
158 "R_X86_64_GOTPC32_TLSDESC",
159 FALSE, 0xffffffff, 0xffffffff, TRUE),
160 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
161 complain_overflow_dont, bfd_elf_generic_reloc,
162 "R_X86_64_TLSDESC_CALL",
163 FALSE, 0, 0, FALSE),
164 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
165 complain_overflow_bitfield, bfd_elf_generic_reloc,
166 "R_X86_64_TLSDESC",
167 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
cbe950e9
L
168 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
169 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
170 MINUS_ONE, FALSE),
64d25c44
L
171 HOWTO(R_X86_64_RELATIVE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
172 bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", FALSE, MINUS_ONE,
173 MINUS_ONE, FALSE),
c3320543
L
174 HOWTO(R_X86_64_PC32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
175 bfd_elf_generic_reloc, "R_X86_64_PC32_BND", FALSE, 0xffffffff, 0xffffffff,
176 TRUE),
177 HOWTO(R_X86_64_PLT32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
178 bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", FALSE, 0xffffffff, 0xffffffff,
179 TRUE),
56ceb5b5
L
180 HOWTO(R_X86_64_GOTPCRELX, 0, 2, 32, TRUE, 0, complain_overflow_signed,
181 bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", FALSE, 0xffffffff,
182 0xffffffff, TRUE),
183 HOWTO(R_X86_64_REX_GOTPCRELX, 0, 2, 32, TRUE, 0, complain_overflow_signed,
184 bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", FALSE, 0xffffffff,
185 0xffffffff, TRUE),
fe4770f4 186
a33d77bc
JB
187 /* We have a gap in the reloc numbers here.
188 R_X86_64_standard counts the number up to this point, and
189 R_X86_64_vt_offset is the value to subtract from a reloc type of
190 R_X86_64_GNU_VT* to form an index into this table. */
56ceb5b5 191#define R_X86_64_standard (R_X86_64_REX_GOTPCRELX + 1)
a33d77bc
JB
192#define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
193
fe4770f4 194/* GNU extension to record C++ vtable hierarchy. */
b34976b6
AM
195 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
196 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
fe4770f4
AJ
197
198/* GNU extension to record C++ vtable member usage. */
b34976b6
AM
199 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
200 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
d7921315
L
201 FALSE),
202
203/* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
204 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
205 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
206 FALSE)
8d88c4ca
NC
207};
208
d8045f23
NC
209#define IS_X86_64_PCREL_TYPE(TYPE) \
210 ( ((TYPE) == R_X86_64_PC8) \
211 || ((TYPE) == R_X86_64_PC16) \
212 || ((TYPE) == R_X86_64_PC32) \
c3320543 213 || ((TYPE) == R_X86_64_PC32_BND) \
d8045f23
NC
214 || ((TYPE) == R_X86_64_PC64))
215
8d88c4ca 216/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
217struct elf_reloc_map
218{
8d88c4ca
NC
219 bfd_reloc_code_real_type bfd_reloc_val;
220 unsigned char elf_reloc_val;
221};
222
dc810e39 223static const struct elf_reloc_map x86_64_reloc_map[] =
8d88c4ca 224{
70256ad8
AJ
225 { BFD_RELOC_NONE, R_X86_64_NONE, },
226 { BFD_RELOC_64, R_X86_64_64, },
227 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
228 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
229 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
230 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
231 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
232 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
233 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
234 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
235 { BFD_RELOC_32, R_X86_64_32, },
236 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
237 { BFD_RELOC_16, R_X86_64_16, },
238 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
239 { BFD_RELOC_8, R_X86_64_8, },
240 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
bffbf940
JJ
241 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
242 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
243 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
244 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
245 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
246 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
247 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
248 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
d6ab8113
JB
249 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
250 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
251 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
7b81dfbb
AJ
252 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
253 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
254 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
255 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
256 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
1788fc08
L
257 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
258 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
67a4f2b7
AO
259 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
260 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
261 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
cbe950e9 262 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
56ceb5b5
L
263 { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND, },
264 { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND, },
265 { BFD_RELOC_X86_64_GOTPCRELX, R_X86_64_GOTPCRELX, },
266 { BFD_RELOC_X86_64_REX_GOTPCRELX, R_X86_64_REX_GOTPCRELX, },
fe4770f4
AJ
267 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
268 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
269};
270
67a4f2b7 271static reloc_howto_type *
351f65ca 272elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
67a4f2b7
AO
273{
274 unsigned i;
275
d7921315
L
276 if (r_type == (unsigned int) R_X86_64_32)
277 {
278 if (ABI_64_P (abfd))
279 i = r_type;
280 else
281 i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
282 }
283 else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
284 || r_type >= (unsigned int) R_X86_64_max)
67a4f2b7
AO
285 {
286 if (r_type >= (unsigned int) R_X86_64_standard)
287 {
288 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
289 abfd, (int) r_type);
290 r_type = R_X86_64_NONE;
291 }
292 i = r_type;
293 }
294 else
295 i = r_type - (unsigned int) R_X86_64_vt_offset;
296 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
297 return &x86_64_elf_howto_table[i];
298}
8d88c4ca
NC
299
300/* Given a BFD reloc type, return a HOWTO structure. */
301static reloc_howto_type *
351f65ca
L
302elf_x86_64_reloc_type_lookup (bfd *abfd,
303 bfd_reloc_code_real_type code)
8d88c4ca
NC
304{
305 unsigned int i;
27482721 306
8d88c4ca
NC
307 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
308 i++)
309 {
310 if (x86_64_reloc_map[i].bfd_reloc_val == code)
351f65ca
L
311 return elf_x86_64_rtype_to_howto (abfd,
312 x86_64_reloc_map[i].elf_reloc_val);
8d88c4ca 313 }
5860e3f8 314 return NULL;
8d88c4ca
NC
315}
316
157090f7 317static reloc_howto_type *
d7921315 318elf_x86_64_reloc_name_lookup (bfd *abfd,
351f65ca 319 const char *r_name)
157090f7
AM
320{
321 unsigned int i;
322
d7921315
L
323 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
324 {
325 /* Get x32 R_X86_64_32. */
326 reloc_howto_type *reloc
327 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
328 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
329 return reloc;
330 }
331
332 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
157090f7
AM
333 if (x86_64_elf_howto_table[i].name != NULL
334 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
335 return &x86_64_elf_howto_table[i];
336
337 return NULL;
338}
339
8d88c4ca 340/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 341
8d88c4ca 342static void
351f65ca
L
343elf_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
344 Elf_Internal_Rela *dst)
8d88c4ca 345{
67a4f2b7 346 unsigned r_type;
8d88c4ca 347
351f65ca
L
348 r_type = ELF32_R_TYPE (dst->r_info);
349 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
8d88c4ca
NC
350 BFD_ASSERT (r_type == cache_ptr->howto->type);
351}
70256ad8 352\f
3bab7989 353/* Support for core dump NOTE sections. */
b34976b6 354static bfd_boolean
351f65ca 355elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
356{
357 int offset;
eea6121a 358 size_t size;
3bab7989
ML
359
360 switch (note->descsz)
361 {
362 default:
b34976b6 363 return FALSE;
3bab7989 364
bcd823f1
L
365 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
366 /* pr_cursig */
228e534f 367 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
bcd823f1
L
368
369 /* pr_pid */
228e534f 370 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
bcd823f1
L
371
372 /* pr_reg */
373 offset = 72;
374 size = 216;
375
376 break;
377
3bab7989
ML
378 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
379 /* pr_cursig */
228e534f 380 elf_tdata (abfd)->core->signal
3bab7989
ML
381 = bfd_get_16 (abfd, note->descdata + 12);
382
383 /* pr_pid */
228e534f 384 elf_tdata (abfd)->core->lwpid
3bab7989
ML
385 = bfd_get_32 (abfd, note->descdata + 32);
386
387 /* pr_reg */
388 offset = 112;
eea6121a 389 size = 216;
3bab7989
ML
390
391 break;
392 }
393
394 /* Make a ".reg/999" section. */
395 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 396 size, note->descpos + offset);
3bab7989
ML
397}
398
b34976b6 399static bfd_boolean
351f65ca 400elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
401{
402 switch (note->descsz)
403 {
404 default:
b34976b6 405 return FALSE;
3bab7989 406
bcd823f1 407 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
228e534f 408 elf_tdata (abfd)->core->pid
bcd823f1 409 = bfd_get_32 (abfd, note->descdata + 12);
228e534f 410 elf_tdata (abfd)->core->program
bcd823f1 411 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 412 elf_tdata (abfd)->core->command
bcd823f1
L
413 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
414 break;
415
3bab7989 416 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
228e534f 417 elf_tdata (abfd)->core->pid
261b8d08 418 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 419 elf_tdata (abfd)->core->program
3bab7989 420 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 421 elf_tdata (abfd)->core->command
3bab7989
ML
422 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
423 }
424
425 /* Note that for some reason, a spurious space is tacked
426 onto the end of the args in some (at least one anyway)
427 implementations, so strip it off if it exists. */
428
429 {
228e534f 430 char *command = elf_tdata (abfd)->core->command;
3bab7989
ML
431 int n = strlen (command);
432
433 if (0 < n && command[n - 1] == ' ')
434 command[n - 1] = '\0';
435 }
436
b34976b6 437 return TRUE;
3bab7989 438}
8fd79e71
L
439
440#ifdef CORE_HEADER
441static char *
442elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
443 int note_type, ...)
444{
445 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8fd79e71
L
446 va_list ap;
447 const char *fname, *psargs;
448 long pid;
449 int cursig;
450 const void *gregs;
451
452 switch (note_type)
453 {
454 default:
455 return NULL;
456
457 case NT_PRPSINFO:
458 va_start (ap, note_type);
459 fname = va_arg (ap, const char *);
460 psargs = va_arg (ap, const char *);
461 va_end (ap);
462
463 if (bed->s->elfclass == ELFCLASS32)
464 {
465 prpsinfo32_t data;
466 memset (&data, 0, sizeof (data));
467 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
468 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
e85c6a70
JK
469 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
470 &data, sizeof (data));
8fd79e71
L
471 }
472 else
473 {
b1bd052d 474 prpsinfo64_t data;
8fd79e71
L
475 memset (&data, 0, sizeof (data));
476 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
477 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
e85c6a70
JK
478 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
479 &data, sizeof (data));
8fd79e71 480 }
e85c6a70 481 /* NOTREACHED */
8fd79e71
L
482
483 case NT_PRSTATUS:
484 va_start (ap, note_type);
485 pid = va_arg (ap, long);
486 cursig = va_arg (ap, int);
487 gregs = va_arg (ap, const void *);
488 va_end (ap);
489
490 if (bed->s->elfclass == ELFCLASS32)
491 {
492 if (bed->elf_machine_code == EM_X86_64)
493 {
494 prstatusx32_t prstat;
495 memset (&prstat, 0, sizeof (prstat));
496 prstat.pr_pid = pid;
497 prstat.pr_cursig = cursig;
498 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
499 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
500 &prstat, sizeof (prstat));
8fd79e71
L
501 }
502 else
503 {
504 prstatus32_t prstat;
505 memset (&prstat, 0, sizeof (prstat));
506 prstat.pr_pid = pid;
507 prstat.pr_cursig = cursig;
508 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
509 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
510 &prstat, sizeof (prstat));
8fd79e71
L
511 }
512 }
513 else
514 {
b1bd052d 515 prstatus64_t prstat;
8fd79e71
L
516 memset (&prstat, 0, sizeof (prstat));
517 prstat.pr_pid = pid;
518 prstat.pr_cursig = cursig;
519 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
520 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
521 &prstat, sizeof (prstat));
8fd79e71 522 }
8fd79e71 523 }
e85c6a70 524 /* NOTREACHED */
8fd79e71
L
525}
526#endif
3bab7989 527\f
407443a3 528/* Functions for the x86-64 ELF linker. */
70256ad8 529
407443a3 530/* The name of the dynamic interpreter. This is put in the .interp
70256ad8
AJ
531 section. */
532
351f65ca 533#define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
eec9dd95 534#define ELF32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
70256ad8 535
d40d037c
AJ
536/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
537 copying dynamic variables from a shared lib into an app's dynbss
538 section, and instead use a dynamic relocation to point into the
539 shared lib. */
540#define ELIMINATE_COPY_RELOCS 1
541
70256ad8
AJ
542/* The size in bytes of an entry in the global offset table. */
543
544#define GOT_ENTRY_SIZE 8
8d88c4ca 545
70256ad8 546/* The size in bytes of an entry in the procedure linkage table. */
8d88c4ca 547
70256ad8
AJ
548#define PLT_ENTRY_SIZE 16
549
550/* The first entry in a procedure linkage table looks like this. See the
551 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
552
351f65ca 553static const bfd_byte elf_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
70256ad8 554{
653165cc
AJ
555 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
556 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
10efb593 557 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
70256ad8
AJ
558};
559
560/* Subsequent entries in a procedure linkage table look like this. */
561
351f65ca 562static const bfd_byte elf_x86_64_plt_entry[PLT_ENTRY_SIZE] =
70256ad8 563{
653165cc 564 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 565 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 566 0x68, /* pushq immediate */
70256ad8
AJ
567 0, 0, 0, 0, /* replaced with index into relocation table. */
568 0xe9, /* jmp relative */
569 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
570};
571
0ff2b86e
L
572/* The first entry in a procedure linkage table with BND relocations
573 like this. */
574
575static const bfd_byte elf_x86_64_bnd_plt0_entry[PLT_ENTRY_SIZE] =
576{
577 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
578 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
579 0x0f, 0x1f, 0 /* nopl (%rax) */
580};
581
582/* Subsequent entries for legacy branches in a procedure linkage table
583 with BND relocations look like this. */
584
585static const bfd_byte elf_x86_64_legacy_plt_entry[PLT_ENTRY_SIZE] =
586{
587 0x68, 0, 0, 0, 0, /* pushq immediate */
588 0xe9, 0, 0, 0, 0, /* jmpq relative */
589 0x66, 0x0f, 0x1f, 0x44, 0, 0 /* nopw (%rax,%rax,1) */
590};
591
592/* Subsequent entries for branches with BND prefx in a procedure linkage
593 table with BND relocations look like this. */
594
595static const bfd_byte elf_x86_64_bnd_plt_entry[PLT_ENTRY_SIZE] =
596{
597 0x68, 0, 0, 0, 0, /* pushq immediate */
598 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
599 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
600};
601
602/* Entries for legacy branches in the second procedure linkage table
603 look like this. */
604
605static const bfd_byte elf_x86_64_legacy_plt2_entry[8] =
606{
607 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
608 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
609 0x66, 0x90 /* xchg %ax,%ax */
610};
611
612/* Entries for branches with BND prefix in the second procedure linkage
613 table look like this. */
614
615static const bfd_byte elf_x86_64_bnd_plt2_entry[8] =
616{
617 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
618 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
619 0x90 /* nop */
620};
621
e41b3a13
JJ
622/* .eh_frame covering the .plt section. */
623
624static const bfd_byte elf_x86_64_eh_frame_plt[] =
625{
626#define PLT_CIE_LENGTH 20
627#define PLT_FDE_LENGTH 36
628#define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
629#define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
630 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
631 0, 0, 0, 0, /* CIE ID */
632 1, /* CIE version */
633 'z', 'R', 0, /* Augmentation string */
634 1, /* Code alignment factor */
635 0x78, /* Data alignment factor */
636 16, /* Return address column */
637 1, /* Augmentation size */
638 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
639 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
640 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
641 DW_CFA_nop, DW_CFA_nop,
642
643 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
644 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
645 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
646 0, 0, 0, 0, /* .plt size goes here */
647 0, /* Augmentation size */
648 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
649 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
650 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
651 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
652 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
653 11, /* Block length */
654 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
655 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
656 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
657 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
658 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
659};
660
eed180f8
RM
661/* Architecture-specific backend data for x86-64. */
662
663struct elf_x86_64_backend_data
664{
665 /* Templates for the initial PLT entry and for subsequent entries. */
666 const bfd_byte *plt0_entry;
667 const bfd_byte *plt_entry;
668 unsigned int plt_entry_size; /* Size of each PLT entry. */
669
670 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
671 unsigned int plt0_got1_offset;
672 unsigned int plt0_got2_offset;
673
674 /* Offset of the end of the PC-relative instruction containing
675 plt0_got2_offset. */
676 unsigned int plt0_got2_insn_end;
677
678 /* Offsets into plt_entry that are to be replaced with... */
679 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
680 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
681 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
682
683 /* Length of the PC-relative instruction containing plt_got_offset. */
684 unsigned int plt_got_insn_size;
685
686 /* Offset of the end of the PC-relative jump to plt0_entry. */
687 unsigned int plt_plt_insn_end;
688
689 /* Offset into plt_entry where the initial value of the GOT entry points. */
690 unsigned int plt_lazy_offset;
691
692 /* .eh_frame covering the .plt section. */
693 const bfd_byte *eh_frame_plt;
694 unsigned int eh_frame_plt_size;
695};
696
f8222080
L
697#define get_elf_x86_64_arch_data(bed) \
698 ((const struct elf_x86_64_backend_data *) (bed)->arch_data)
699
eed180f8 700#define get_elf_x86_64_backend_data(abfd) \
f8222080 701 get_elf_x86_64_arch_data (get_elf_backend_data (abfd))
eed180f8
RM
702
703#define GET_PLT_ENTRY_SIZE(abfd) \
704 get_elf_x86_64_backend_data (abfd)->plt_entry_size
705
706/* These are the standard parameters. */
707static const struct elf_x86_64_backend_data elf_x86_64_arch_bed =
708 {
709 elf_x86_64_plt0_entry, /* plt0_entry */
710 elf_x86_64_plt_entry, /* plt_entry */
711 sizeof (elf_x86_64_plt_entry), /* plt_entry_size */
712 2, /* plt0_got1_offset */
713 8, /* plt0_got2_offset */
714 12, /* plt0_got2_insn_end */
715 2, /* plt_got_offset */
716 7, /* plt_reloc_offset */
717 12, /* plt_plt_offset */
718 6, /* plt_got_insn_size */
719 PLT_ENTRY_SIZE, /* plt_plt_insn_end */
720 6, /* plt_lazy_offset */
721 elf_x86_64_eh_frame_plt, /* eh_frame_plt */
722 sizeof (elf_x86_64_eh_frame_plt), /* eh_frame_plt_size */
723 };
724
0ff2b86e
L
725static const struct elf_x86_64_backend_data elf_x86_64_bnd_arch_bed =
726 {
727 elf_x86_64_bnd_plt0_entry, /* plt0_entry */
728 elf_x86_64_bnd_plt_entry, /* plt_entry */
729 sizeof (elf_x86_64_bnd_plt_entry), /* plt_entry_size */
730 2, /* plt0_got1_offset */
731 1+8, /* plt0_got2_offset */
732 1+12, /* plt0_got2_insn_end */
733 1+2, /* plt_got_offset */
734 1, /* plt_reloc_offset */
735 7, /* plt_plt_offset */
736 1+6, /* plt_got_insn_size */
737 11, /* plt_plt_insn_end */
738 0, /* plt_lazy_offset */
739 elf_x86_64_eh_frame_plt, /* eh_frame_plt */
740 sizeof (elf_x86_64_eh_frame_plt), /* eh_frame_plt_size */
741 };
742
eed180f8
RM
743#define elf_backend_arch_data &elf_x86_64_arch_bed
744
aec6b87e
L
745/* Is a undefined weak symbol which is resolved to 0. Reference to an
746 undefined weak symbol is resolved to 0 when building executable if
747 it isn't dynamic and
748 1. Has non-GOT/non-PLT relocations in text section. Or
749 2. Has no GOT/PLT relocation.
750 */
e62b9723 751#define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, GOT_RELOC, EH) \
aec6b87e
L
752 ((EH)->elf.root.type == bfd_link_hash_undefweak \
753 && bfd_link_executable (INFO) \
754 && (elf_x86_64_hash_table (INFO)->interp == NULL \
e62b9723 755 || !(GOT_RELOC) \
aec6b87e
L
756 || (EH)->has_non_got_reloc \
757 || !(INFO)->dynamic_undefined_weak))
758
70256ad8
AJ
759/* x86-64 ELF linker hash entry. */
760
351f65ca 761struct elf_x86_64_link_hash_entry
70256ad8 762{
c434dee6 763 struct elf_link_hash_entry elf;
70256ad8 764
c434dee6 765 /* Track dynamic relocs copied for this symbol. */
e03a8ed8 766 struct elf_dyn_relocs *dyn_relocs;
bffbf940
JJ
767
768#define GOT_UNKNOWN 0
769#define GOT_NORMAL 1
770#define GOT_TLS_GD 2
771#define GOT_TLS_IE 3
67a4f2b7
AO
772#define GOT_TLS_GDESC 4
773#define GOT_TLS_GD_BOTH_P(type) \
774 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
775#define GOT_TLS_GD_P(type) \
776 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
777#define GOT_TLS_GDESC_P(type) \
778 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
779#define GOT_TLS_GD_ANY_P(type) \
780 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
bffbf940 781 unsigned char tls_type;
67a4f2b7 782
bc696fd5
L
783 /* TRUE if a weak symbol with a real definition needs a copy reloc.
784 When there is a weak symbol with a real definition, the processor
785 independent code will have arranged for us to see the real
786 definition first. We need to copy the needs_copy bit from the
787 real definition and check it when allowing copy reloc in PIE. */
788 unsigned int needs_copy : 1;
789
0ff2b86e 790 /* TRUE if symbol has at least one BND relocation. */
bc696fd5 791 unsigned int has_bnd_reloc : 1;
0ff2b86e 792
aec6b87e
L
793 /* TRUE if symbol has GOT or PLT relocations. */
794 unsigned int has_got_reloc : 1;
795
796 /* TRUE if symbol has non-GOT/non-PLT relocations in text sections. */
797 unsigned int has_non_got_reloc : 1;
798
e2cbcd91
L
799 /* 0: symbol isn't __tls_get_addr.
800 1: symbol is __tls_get_addr.
801 2: symbol is unknown. */
802 unsigned int tls_get_addr : 2;
803
04ebc307
L
804 /* Reference count of C/C++ function pointer relocations in read-write
805 section which can be resolved at run-time. */
806 bfd_signed_vma func_pointer_refcount;
807
dd7e64d4
L
808 /* Information about the GOT PLT entry. Filled when there are both
809 GOT and PLT relocations against the same function. */
810 union gotplt_union plt_got;
811
0ff2b86e
L
812 /* Information about the second PLT entry. Filled when has_bnd_reloc is
813 set. */
814 union gotplt_union plt_bnd;
815
67a4f2b7
AO
816 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
817 starting at the end of the jump table. */
818 bfd_vma tlsdesc_got;
bffbf940
JJ
819};
820
351f65ca
L
821#define elf_x86_64_hash_entry(ent) \
822 ((struct elf_x86_64_link_hash_entry *)(ent))
bffbf940 823
351f65ca 824struct elf_x86_64_obj_tdata
bffbf940
JJ
825{
826 struct elf_obj_tdata root;
827
828 /* tls_type for each local got entry. */
829 char *local_got_tls_type;
67a4f2b7
AO
830
831 /* GOTPLT entries for TLS descriptors. */
832 bfd_vma *local_tlsdesc_gotent;
70256ad8
AJ
833};
834
351f65ca
L
835#define elf_x86_64_tdata(abfd) \
836 ((struct elf_x86_64_obj_tdata *) (abfd)->tdata.any)
bffbf940 837
351f65ca
L
838#define elf_x86_64_local_got_tls_type(abfd) \
839 (elf_x86_64_tdata (abfd)->local_got_tls_type)
bffbf940 840
351f65ca
L
841#define elf_x86_64_local_tlsdesc_gotent(abfd) \
842 (elf_x86_64_tdata (abfd)->local_tlsdesc_gotent)
bffbf940 843
0ffa91dd
NC
844#define is_x86_64_elf(bfd) \
845 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
846 && elf_tdata (bfd) != NULL \
4dfe6ac6 847 && elf_object_id (bfd) == X86_64_ELF_DATA)
0ffa91dd
NC
848
849static bfd_boolean
351f65ca 850elf_x86_64_mkobject (bfd *abfd)
0ffa91dd 851{
351f65ca 852 return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_64_obj_tdata),
4dfe6ac6 853 X86_64_ELF_DATA);
0ffa91dd
NC
854}
855
c434dee6 856/* x86-64 ELF linker hash table. */
8d88c4ca 857
351f65ca 858struct elf_x86_64_link_hash_table
407443a3 859{
c434dee6 860 struct elf_link_hash_table elf;
70256ad8 861
c434dee6 862 /* Short-cuts to get to dynamic linker sections. */
aec6b87e 863 asection *interp;
c434dee6
AJ
864 asection *sdynbss;
865 asection *srelbss;
e41b3a13 866 asection *plt_eh_frame;
0ff2b86e 867 asection *plt_bnd;
dd7e64d4 868 asection *plt_got;
70256ad8 869
4dfe6ac6
NC
870 union
871 {
bffbf940
JJ
872 bfd_signed_vma refcount;
873 bfd_vma offset;
874 } tls_ld_got;
875
67a4f2b7
AO
876 /* The amount of space used by the jump slots in the GOT. */
877 bfd_vma sgotplt_jump_table_size;
878
87d72d41
AM
879 /* Small local sym cache. */
880 struct sym_cache sym_cache;
9f03412a 881
351f65ca
L
882 bfd_vma (*r_info) (bfd_vma, bfd_vma);
883 bfd_vma (*r_sym) (bfd_vma);
248775ba 884 unsigned int pointer_r_type;
351f65ca
L
885 const char *dynamic_interpreter;
886 int dynamic_interpreter_size;
887
9f03412a
AO
888 /* _TLS_MODULE_BASE_ symbol. */
889 struct bfd_link_hash_entry *tls_module_base;
c25bc9fc
L
890
891 /* Used by local STT_GNU_IFUNC symbols. */
892 htab_t loc_hash_table;
4dfe6ac6
NC
893 void * loc_hash_memory;
894
895 /* The offset into splt of the PLT entry for the TLS descriptor
896 resolver. Special values are 0, if not necessary (or not found
897 to be necessary yet), and -1 if needed but not determined
898 yet. */
899 bfd_vma tlsdesc_plt;
900 /* The offset into sgot of the GOT entry used by the PLT entry
901 above. */
902 bfd_vma tlsdesc_got;
e1f98742
L
903
904 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
905 bfd_vma next_jump_slot_index;
906 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
907 bfd_vma next_irelative_index;
2df3368d
L
908
909 /* TRUE if there are dynamic relocs against IFUNC symbols that apply
910 to read-only sections. */
911 bfd_boolean readonly_dynrelocs_against_ifunc;
c434dee6 912};
70256ad8
AJ
913
914/* Get the x86-64 ELF linker hash table from a link_info structure. */
8d88c4ca 915
351f65ca 916#define elf_x86_64_hash_table(p) \
4dfe6ac6 917 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
351f65ca 918 == X86_64_ELF_DATA ? ((struct elf_x86_64_link_hash_table *) ((p)->hash)) : NULL)
8d88c4ca 919
351f65ca 920#define elf_x86_64_compute_jump_table_size(htab) \
6de2ae4a 921 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
67a4f2b7 922
407443a3 923/* Create an entry in an x86-64 ELF linker hash table. */
70256ad8
AJ
924
925static struct bfd_hash_entry *
351f65ca
L
926elf_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
927 struct bfd_hash_table *table,
928 const char *string)
70256ad8 929{
70256ad8 930 /* Allocate the structure if it has not already been allocated by a
c434dee6
AJ
931 subclass. */
932 if (entry == NULL)
933 {
a50b1753 934 entry = (struct bfd_hash_entry *)
eed180f8
RM
935 bfd_hash_allocate (table,
936 sizeof (struct elf_x86_64_link_hash_entry));
c434dee6
AJ
937 if (entry == NULL)
938 return entry;
939 }
70256ad8
AJ
940
941 /* Call the allocation method of the superclass. */
c434dee6
AJ
942 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
943 if (entry != NULL)
70256ad8 944 {
351f65ca 945 struct elf_x86_64_link_hash_entry *eh;
c434dee6 946
351f65ca 947 eh = (struct elf_x86_64_link_hash_entry *) entry;
c434dee6 948 eh->dyn_relocs = NULL;
bffbf940 949 eh->tls_type = GOT_UNKNOWN;
bc696fd5
L
950 eh->needs_copy = 0;
951 eh->has_bnd_reloc = 0;
aec6b87e
L
952 eh->has_got_reloc = 0;
953 eh->has_non_got_reloc = 0;
e2cbcd91 954 eh->tls_get_addr = 2;
04ebc307 955 eh->func_pointer_refcount = 0;
0ff2b86e 956 eh->plt_bnd.offset = (bfd_vma) -1;
dd7e64d4 957 eh->plt_got.offset = (bfd_vma) -1;
67a4f2b7 958 eh->tlsdesc_got = (bfd_vma) -1;
70256ad8
AJ
959 }
960
c434dee6 961 return entry;
70256ad8
AJ
962}
963
c25bc9fc
L
964/* Compute a hash of a local hash entry. We use elf_link_hash_entry
965 for local symbol so that we can handle local STT_GNU_IFUNC symbols
966 as global symbol. We reuse indx and dynstr_index for local symbol
967 hash since they aren't used by global symbols in this backend. */
968
969static hashval_t
351f65ca 970elf_x86_64_local_htab_hash (const void *ptr)
c25bc9fc
L
971{
972 struct elf_link_hash_entry *h
973 = (struct elf_link_hash_entry *) ptr;
d2149d72 974 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
c25bc9fc
L
975}
976
977/* Compare local hash entries. */
978
979static int
351f65ca 980elf_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
c25bc9fc
L
981{
982 struct elf_link_hash_entry *h1
983 = (struct elf_link_hash_entry *) ptr1;
984 struct elf_link_hash_entry *h2
985 = (struct elf_link_hash_entry *) ptr2;
986
987 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
988}
989
990/* Find and/or create a hash entry for local symbol. */
991
992static struct elf_link_hash_entry *
351f65ca
L
993elf_x86_64_get_local_sym_hash (struct elf_x86_64_link_hash_table *htab,
994 bfd *abfd, const Elf_Internal_Rela *rel,
995 bfd_boolean create)
c25bc9fc 996{
351f65ca 997 struct elf_x86_64_link_hash_entry e, *ret;
c25bc9fc 998 asection *sec = abfd->sections;
d2149d72 999 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
351f65ca 1000 htab->r_sym (rel->r_info));
c25bc9fc
L
1001 void **slot;
1002
1003 e.elf.indx = sec->id;
351f65ca 1004 e.elf.dynstr_index = htab->r_sym (rel->r_info);
c25bc9fc
L
1005 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
1006 create ? INSERT : NO_INSERT);
1007
1008 if (!slot)
1009 return NULL;
1010
1011 if (*slot)
1012 {
351f65ca 1013 ret = (struct elf_x86_64_link_hash_entry *) *slot;
c25bc9fc
L
1014 return &ret->elf;
1015 }
1016
351f65ca 1017 ret = (struct elf_x86_64_link_hash_entry *)
c25bc9fc 1018 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
351f65ca 1019 sizeof (struct elf_x86_64_link_hash_entry));
c25bc9fc
L
1020 if (ret)
1021 {
1022 memset (ret, 0, sizeof (*ret));
1023 ret->elf.indx = sec->id;
351f65ca 1024 ret->elf.dynstr_index = htab->r_sym (rel->r_info);
c25bc9fc 1025 ret->elf.dynindx = -1;
04ebc307 1026 ret->func_pointer_refcount = 0;
dd7e64d4 1027 ret->plt_got.offset = (bfd_vma) -1;
c25bc9fc
L
1028 *slot = ret;
1029 }
1030 return &ret->elf;
1031}
1032
68faa637
AM
1033/* Destroy an X86-64 ELF linker hash table. */
1034
1035static void
d495ab0d 1036elf_x86_64_link_hash_table_free (bfd *obfd)
68faa637
AM
1037{
1038 struct elf_x86_64_link_hash_table *htab
d495ab0d 1039 = (struct elf_x86_64_link_hash_table *) obfd->link.hash;
68faa637
AM
1040
1041 if (htab->loc_hash_table)
1042 htab_delete (htab->loc_hash_table);
1043 if (htab->loc_hash_memory)
1044 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
d495ab0d 1045 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
1046}
1047
8d88c4ca
NC
1048/* Create an X86-64 ELF linker hash table. */
1049
1050static struct bfd_link_hash_table *
351f65ca 1051elf_x86_64_link_hash_table_create (bfd *abfd)
8d88c4ca 1052{
351f65ca
L
1053 struct elf_x86_64_link_hash_table *ret;
1054 bfd_size_type amt = sizeof (struct elf_x86_64_link_hash_table);
8d88c4ca 1055
7bf52ea2 1056 ret = (struct elf_x86_64_link_hash_table *) bfd_zmalloc (amt);
c434dee6 1057 if (ret == NULL)
8d88c4ca
NC
1058 return NULL;
1059
eb4ff4d6 1060 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
351f65ca
L
1061 elf_x86_64_link_hash_newfunc,
1062 sizeof (struct elf_x86_64_link_hash_entry),
4dfe6ac6 1063 X86_64_ELF_DATA))
8d88c4ca 1064 {
e2d34d7d 1065 free (ret);
8d88c4ca
NC
1066 return NULL;
1067 }
1068
351f65ca
L
1069 if (ABI_64_P (abfd))
1070 {
1071 ret->r_info = elf64_r_info;
1072 ret->r_sym = elf64_r_sym;
248775ba 1073 ret->pointer_r_type = R_X86_64_64;
351f65ca
L
1074 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
1075 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
1076 }
1077 else
1078 {
1079 ret->r_info = elf32_r_info;
1080 ret->r_sym = elf32_r_sym;
248775ba 1081 ret->pointer_r_type = R_X86_64_32;
351f65ca
L
1082 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
1083 ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
1084 }
1085
c25bc9fc 1086 ret->loc_hash_table = htab_try_create (1024,
351f65ca
L
1087 elf_x86_64_local_htab_hash,
1088 elf_x86_64_local_htab_eq,
c25bc9fc
L
1089 NULL);
1090 ret->loc_hash_memory = objalloc_create ();
1091 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1092 {
d495ab0d 1093 elf_x86_64_link_hash_table_free (abfd);
c25bc9fc
L
1094 return NULL;
1095 }
d495ab0d 1096 ret->elf.root.hash_table_free = elf_x86_64_link_hash_table_free;
c25bc9fc 1097
c434dee6
AJ
1098 return &ret->elf.root;
1099}
1100
c434dee6
AJ
1101/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
1102 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
1103 hash table. */
1104
b34976b6 1105static bfd_boolean
351f65ca
L
1106elf_x86_64_create_dynamic_sections (bfd *dynobj,
1107 struct bfd_link_info *info)
c434dee6 1108{
351f65ca 1109 struct elf_x86_64_link_hash_table *htab;
c434dee6 1110
c434dee6 1111 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 1112 return FALSE;
c434dee6 1113
351f65ca 1114 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
1115 if (htab == NULL)
1116 return FALSE;
1117
493f652c
L
1118 /* Set the contents of the .interp section to the interpreter. */
1119 if (bfd_link_executable (info) && !info->nointerp)
1120 {
1121 asection *s = bfd_get_linker_section (dynobj, ".interp");
1122 if (s == NULL)
1123 abort ();
1124 s->size = htab->dynamic_interpreter_size;
1125 s->contents = (unsigned char *) htab->dynamic_interpreter;
1126 htab->interp = s;
1127 }
1128
3d4d4302 1129 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
9a926d55 1130 if (!htab->sdynbss)
c434dee6
AJ
1131 abort ();
1132
0e1862bb 1133 if (bfd_link_executable (info))
9a926d55
L
1134 {
1135 /* Always allow copy relocs for building executables. */
9d157cb9 1136 asection *s = bfd_get_linker_section (dynobj, ".rela.bss");
9a926d55
L
1137 if (s == NULL)
1138 {
1139 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
1140 s = bfd_make_section_anyway_with_flags (dynobj,
1141 ".rela.bss",
1142 (bed->dynamic_sec_flags
1143 | SEC_READONLY));
1144 if (s == NULL
1145 || ! bfd_set_section_alignment (dynobj, s,
1146 bed->s->log_file_align))
1147 return FALSE;
1148 }
1149 htab->srelbss = s;
1150 }
1151
e41b3a13 1152 if (!info->no_ld_generated_unwind_info
2fe0fd06 1153 && htab->plt_eh_frame == NULL
e4de50d4 1154 && htab->elf.splt != NULL)
e41b3a13 1155 {
bbf96e4e
L
1156 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
1157 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1158 | SEC_LINKER_CREATED);
e41b3a13 1159 htab->plt_eh_frame
bbf96e4e 1160 = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
e41b3a13
JJ
1161 if (htab->plt_eh_frame == NULL
1162 || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 3))
1163 return FALSE;
e41b3a13 1164 }
37567a2c
L
1165
1166 /* Align .got section to its entry size. */
1167 if (htab->elf.sgot != NULL
1168 && !bfd_set_section_alignment (dynobj, htab->elf.sgot, 3))
1169 return FALSE;
1170
1171 /* Align .got.plt section to its entry size. */
1172 if (htab->elf.sgotplt != NULL
1173 && !bfd_set_section_alignment (dynobj, htab->elf.sgotplt, 3))
1174 return FALSE;
1175
b34976b6 1176 return TRUE;
c434dee6
AJ
1177}
1178
1179/* Copy the extra info we tack onto an elf_link_hash_entry. */
1180
1181static void
351f65ca
L
1182elf_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
1183 struct elf_link_hash_entry *dir,
1184 struct elf_link_hash_entry *ind)
c434dee6 1185{
351f65ca 1186 struct elf_x86_64_link_hash_entry *edir, *eind;
c434dee6 1187
351f65ca
L
1188 edir = (struct elf_x86_64_link_hash_entry *) dir;
1189 eind = (struct elf_x86_64_link_hash_entry *) ind;
c434dee6 1190
0ff2b86e
L
1191 if (!edir->has_bnd_reloc)
1192 edir->has_bnd_reloc = eind->has_bnd_reloc;
1193
aec6b87e
L
1194 if (!edir->has_got_reloc)
1195 edir->has_got_reloc = eind->has_got_reloc;
1196
1197 if (!edir->has_non_got_reloc)
1198 edir->has_non_got_reloc = eind->has_non_got_reloc;
1199
c434dee6
AJ
1200 if (eind->dyn_relocs != NULL)
1201 {
1202 if (edir->dyn_relocs != NULL)
1203 {
e03a8ed8
L
1204 struct elf_dyn_relocs **pp;
1205 struct elf_dyn_relocs *p;
c434dee6 1206
fcfa13d2 1207 /* Add reloc counts against the indirect sym to the direct sym
c434dee6
AJ
1208 list. Merge any entries against the same section. */
1209 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1210 {
e03a8ed8 1211 struct elf_dyn_relocs *q;
c434dee6
AJ
1212
1213 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1214 if (q->sec == p->sec)
1215 {
1216 q->pc_count += p->pc_count;
1217 q->count += p->count;
1218 *pp = p->next;
1219 break;
1220 }
1221 if (q == NULL)
1222 pp = &p->next;
1223 }
1224 *pp = edir->dyn_relocs;
1225 }
1226
1227 edir->dyn_relocs = eind->dyn_relocs;
1228 eind->dyn_relocs = NULL;
1229 }
1230
bffbf940
JJ
1231 if (ind->root.type == bfd_link_hash_indirect
1232 && dir->got.refcount <= 0)
1233 {
1234 edir->tls_type = eind->tls_type;
1235 eind->tls_type = GOT_UNKNOWN;
1236 }
1237
d40d037c
AJ
1238 if (ELIMINATE_COPY_RELOCS
1239 && ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
1240 && dir->dynamic_adjusted)
1241 {
1242 /* If called to transfer flags for a weakdef during processing
1243 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1244 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1245 dir->ref_dynamic |= ind->ref_dynamic;
1246 dir->ref_regular |= ind->ref_regular;
1247 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1248 dir->needs_plt |= ind->needs_plt;
1249 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1250 }
d40d037c 1251 else
04ebc307
L
1252 {
1253 if (eind->func_pointer_refcount > 0)
1254 {
1255 edir->func_pointer_refcount += eind->func_pointer_refcount;
1256 eind->func_pointer_refcount = 0;
1257 }
1258
1259 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1260 }
8d88c4ca
NC
1261}
1262
b34976b6 1263static bfd_boolean
27482721 1264elf64_x86_64_elf_object_p (bfd *abfd)
bffbf940 1265{
8d88c4ca
NC
1266 /* Set the right machine number for an x86-64 elf64 file. */
1267 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
b34976b6 1268 return TRUE;
8d88c4ca
NC
1269}
1270
8059fb19
RM
1271static bfd_boolean
1272elf32_x86_64_elf_object_p (bfd *abfd)
1273{
1274 /* Set the right machine number for an x86-64 elf32 file. */
1275 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1276 return TRUE;
1277}
1278
142411ca
L
1279/* Return TRUE if the TLS access code sequence support transition
1280 from R_TYPE. */
1281
1282static bfd_boolean
351f65ca
L
1283elf_x86_64_check_tls_transition (bfd *abfd,
1284 struct bfd_link_info *info,
1285 asection *sec,
1286 bfd_byte *contents,
1287 Elf_Internal_Shdr *symtab_hdr,
1288 struct elf_link_hash_entry **sym_hashes,
1289 unsigned int r_type,
1290 const Elf_Internal_Rela *rel,
1291 const Elf_Internal_Rela *relend)
bffbf940 1292{
142411ca
L
1293 unsigned int val;
1294 unsigned long r_symndx;
5c98a14e 1295 bfd_boolean largepic = FALSE;
142411ca
L
1296 struct elf_link_hash_entry *h;
1297 bfd_vma offset;
351f65ca 1298 struct elf_x86_64_link_hash_table *htab;
e2cbcd91
L
1299 bfd_byte *call;
1300 bfd_boolean indirect_call, tls_get_addr;
142411ca 1301
351f65ca 1302 htab = elf_x86_64_hash_table (info);
142411ca 1303 offset = rel->r_offset;
bffbf940 1304 switch (r_type)
142411ca
L
1305 {
1306 case R_X86_64_TLSGD:
1307 case R_X86_64_TLSLD:
1308 if ((rel + 1) >= relend)
1309 return FALSE;
1310
1311 if (r_type == R_X86_64_TLSGD)
1312 {
52bc799a 1313 /* Check transition from GD access model. For 64bit, only
142411ca 1314 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
e2cbcd91
L
1315 .word 0x6666; rex64; call __tls_get_addr@PLT
1316 or
1317 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1318 .byte 0x66; rex64
1319 call *__tls_get_addr@GOTPCREL(%rip)
1320 which may be converted to
1321 addr32 call __tls_get_addr
52bc799a
L
1322 can transit to different access model. For 32bit, only
1323 leaq foo@tlsgd(%rip), %rdi
e2cbcd91
L
1324 .word 0x6666; rex64; call __tls_get_addr@PLT
1325 or
1326 leaq foo@tlsgd(%rip), %rdi
1327 .byte 0x66; rex64
1328 call *__tls_get_addr@GOTPCREL(%rip)
1329 which may be converted to
1330 addr32 call __tls_get_addr
1331 can transit to different access model. For largepic,
5c98a14e 1332 we also support:
e2cbcd91
L
1333 leaq foo@tlsgd(%rip), %rdi
1334 movabsq $__tls_get_addr@pltoff, %rax
1335 addq $r15, %rax
1336 call *%rax
1337 or
5c98a14e
JJ
1338 leaq foo@tlsgd(%rip), %rdi
1339 movabsq $__tls_get_addr@pltoff, %rax
1340 addq $rbx, %rax
e2cbcd91 1341 call *%rax */
142411ca 1342
fa289a5f
AM
1343 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1344
5c98a14e 1345 if ((offset + 12) > sec->size)
142411ca 1346 return FALSE;
52bc799a 1347
e2cbcd91
L
1348 call = contents + offset + 4;
1349 if (call[0] != 0x66
1350 || !((call[1] == 0x48
1351 && call[2] == 0xff
1352 && call[3] == 0x15)
1353 || (call[1] == 0x48
1354 && call[2] == 0x67
1355 && call[3] == 0xe8)
1356 || (call[1] == 0x66
1357 && call[2] == 0x48
1358 && call[3] == 0xe8)))
5c98a14e
JJ
1359 {
1360 if (!ABI_64_P (abfd)
1361 || (offset + 19) > sec->size
1362 || offset < 3
e2cbcd91
L
1363 || memcmp (call - 7, leaq + 1, 3) != 0
1364 || memcmp (call, "\x48\xb8", 2) != 0
1365 || call[11] != 0x01
1366 || call[13] != 0xff
1367 || call[14] != 0xd0
1368 || !((call[10] == 0x48 && call[12] == 0xd8)
1369 || (call[10] == 0x4c && call[12] == 0xf8)))
5c98a14e
JJ
1370 return FALSE;
1371 largepic = TRUE;
1372 }
1373 else if (ABI_64_P (abfd))
52bc799a 1374 {
52bc799a 1375 if (offset < 4
fa289a5f 1376 || memcmp (contents + offset - 4, leaq, 4) != 0)
52bc799a
L
1377 return FALSE;
1378 }
1379 else
1380 {
52bc799a 1381 if (offset < 3
fa289a5f 1382 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
52bc799a
L
1383 return FALSE;
1384 }
e2cbcd91 1385 indirect_call = call[2] == 0xff;
142411ca
L
1386 }
1387 else
1388 {
1389 /* Check transition from LD access model. Only
1390 leaq foo@tlsld(%rip), %rdi;
e2cbcd91
L
1391 call __tls_get_addr@PLT
1392 or
1393 leaq foo@tlsld(%rip), %rdi;
1394 call *__tls_get_addr@GOTPCREL(%rip)
1395 which may be converted to
1396 addr32 call __tls_get_addr
5c98a14e
JJ
1397 can transit to different access model. For largepic
1398 we also support:
e2cbcd91
L
1399 leaq foo@tlsld(%rip), %rdi
1400 movabsq $__tls_get_addr@pltoff, %rax
1401 addq $r15, %rax
1402 call *%rax
1403 or
5c98a14e
JJ
1404 leaq foo@tlsld(%rip), %rdi
1405 movabsq $__tls_get_addr@pltoff, %rax
1406 addq $rbx, %rax
e2cbcd91 1407 call *%rax */
142411ca 1408
fa289a5f 1409 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
142411ca
L
1410
1411 if (offset < 3 || (offset + 9) > sec->size)
1412 return FALSE;
1413
5c98a14e 1414 if (memcmp (contents + offset - 3, lea, 3) != 0)
142411ca 1415 return FALSE;
5c98a14e 1416
e2cbcd91
L
1417 call = contents + offset + 4;
1418 if (!(call[0] == 0xe8
1419 || (call[0] == 0xff && call[1] == 0x15)
1420 || (call[0] == 0x67 && call[1] == 0xe8)))
5c98a14e
JJ
1421 {
1422 if (!ABI_64_P (abfd)
1423 || (offset + 19) > sec->size
e2cbcd91
L
1424 || memcmp (call, "\x48\xb8", 2) != 0
1425 || call[11] != 0x01
1426 || call[13] != 0xff
1427 || call[14] != 0xd0
1428 || !((call[10] == 0x48 && call[12] == 0xd8)
1429 || (call[10] == 0x4c && call[12] == 0xf8)))
5c98a14e
JJ
1430 return FALSE;
1431 largepic = TRUE;
1432 }
e2cbcd91 1433 indirect_call = call[0] == 0xff;
142411ca
L
1434 }
1435
351f65ca 1436 r_symndx = htab->r_sym (rel[1].r_info);
142411ca
L
1437 if (r_symndx < symtab_hdr->sh_info)
1438 return FALSE;
1439
e2cbcd91 1440 tls_get_addr = FALSE;
142411ca 1441 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
e2cbcd91
L
1442 if (h != NULL && h->root.root.string != NULL)
1443 {
1444 struct elf_x86_64_link_hash_entry *eh
1445 = (struct elf_x86_64_link_hash_entry *) h;
1446 tls_get_addr = eh->tls_get_addr == 1;
1447 if (eh->tls_get_addr > 1)
1448 {
1449 /* Use strncmp to check __tls_get_addr since
1450 __tls_get_addr may be versioned. */
1451 if (strncmp (h->root.root.string, "__tls_get_addr", 14)
1452 == 0)
1453 {
1454 eh->tls_get_addr = 1;
1455 tls_get_addr = TRUE;
1456 }
1457 else
1458 eh->tls_get_addr = 0;
1459 }
1460 }
1461
1462 if (!tls_get_addr)
1463 return FALSE;
1464 else if (largepic)
1465 return ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLTOFF64;
1466 else if (indirect_call)
1467 return ELF32_R_TYPE (rel[1].r_info) == R_X86_64_GOTPCRELX;
1468 else
1469 return (ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PC32
1470 || ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
142411ca
L
1471
1472 case R_X86_64_GOTTPOFF:
1473 /* Check transition from IE access model:
4a4c5f25
L
1474 mov foo@gottpoff(%rip), %reg
1475 add foo@gottpoff(%rip), %reg
142411ca
L
1476 */
1477
4a4c5f25
L
1478 /* Check REX prefix first. */
1479 if (offset >= 3 && (offset + 4) <= sec->size)
1480 {
1481 val = bfd_get_8 (abfd, contents + offset - 3);
1482 if (val != 0x48 && val != 0x4c)
1483 {
1484 /* X32 may have 0x44 REX prefix or no REX prefix. */
1485 if (ABI_64_P (abfd))
1486 return FALSE;
1487 }
1488 }
1489 else
1490 {
1491 /* X32 may not have any REX prefix. */
1492 if (ABI_64_P (abfd))
1493 return FALSE;
1494 if (offset < 2 || (offset + 3) > sec->size)
1495 return FALSE;
1496 }
142411ca
L
1497
1498 val = bfd_get_8 (abfd, contents + offset - 2);
1499 if (val != 0x8b && val != 0x03)
1500 return FALSE;
1501
1502 val = bfd_get_8 (abfd, contents + offset - 1);
1503 return (val & 0xc7) == 5;
1504
1505 case R_X86_64_GOTPC32_TLSDESC:
1506 /* Check transition from GDesc access model:
1507 leaq x@tlsdesc(%rip), %rax
1508
1509 Make sure it's a leaq adding rip to a 32-bit offset
1510 into any register, although it's probably almost always
1511 going to be rax. */
1512
1513 if (offset < 3 || (offset + 4) > sec->size)
1514 return FALSE;
1515
1516 val = bfd_get_8 (abfd, contents + offset - 3);
1517 if ((val & 0xfb) != 0x48)
1518 return FALSE;
1519
1520 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1521 return FALSE;
1522
1523 val = bfd_get_8 (abfd, contents + offset - 1);
1524 return (val & 0xc7) == 0x05;
1525
1526 case R_X86_64_TLSDESC_CALL:
1527 /* Check transition from GDesc access model:
1528 call *x@tlsdesc(%rax)
1529 */
1530 if (offset + 2 <= sec->size)
1531 {
1532 /* Make sure that it's a call *x@tlsdesc(%rax). */
e2cbcd91
L
1533 call = contents + offset;
1534 return call[0] == 0xff && call[1] == 0x10;
142411ca
L
1535 }
1536
1537 return FALSE;
1538
1539 default:
1540 abort ();
1541 }
1542}
1543
1544/* Return TRUE if the TLS access transition is OK or no transition
1545 will be performed. Update R_TYPE if there is a transition. */
1546
1547static bfd_boolean
351f65ca
L
1548elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1549 asection *sec, bfd_byte *contents,
1550 Elf_Internal_Shdr *symtab_hdr,
1551 struct elf_link_hash_entry **sym_hashes,
1552 unsigned int *r_type, int tls_type,
1553 const Elf_Internal_Rela *rel,
1554 const Elf_Internal_Rela *relend,
1555 struct elf_link_hash_entry *h,
bedfd056
L
1556 unsigned long r_symndx,
1557 bfd_boolean from_relocate_section)
142411ca
L
1558{
1559 unsigned int from_type = *r_type;
1560 unsigned int to_type = from_type;
1561 bfd_boolean check = TRUE;
1562
bb1cb422
L
1563 /* Skip TLS transition for functions. */
1564 if (h != NULL
1565 && (h->type == STT_FUNC
1566 || h->type == STT_GNU_IFUNC))
1567 return TRUE;
1568
142411ca 1569 switch (from_type)
bffbf940
JJ
1570 {
1571 case R_X86_64_TLSGD:
67a4f2b7
AO
1572 case R_X86_64_GOTPC32_TLSDESC:
1573 case R_X86_64_TLSDESC_CALL:
bffbf940 1574 case R_X86_64_GOTTPOFF:
0e1862bb 1575 if (bfd_link_executable (info))
142411ca
L
1576 {
1577 if (h == NULL)
1578 to_type = R_X86_64_TPOFF32;
1579 else
1580 to_type = R_X86_64_GOTTPOFF;
1581 }
1582
bedfd056
L
1583 /* When we are called from elf_x86_64_relocate_section, there may
1584 be additional transitions based on TLS_TYPE. */
1585 if (from_relocate_section)
142411ca
L
1586 {
1587 unsigned int new_to_type = to_type;
1588
0e1862bb 1589 if (bfd_link_executable (info)
142411ca
L
1590 && h != NULL
1591 && h->dynindx == -1
1592 && tls_type == GOT_TLS_IE)
1593 new_to_type = R_X86_64_TPOFF32;
1594
1595 if (to_type == R_X86_64_TLSGD
1596 || to_type == R_X86_64_GOTPC32_TLSDESC
1597 || to_type == R_X86_64_TLSDESC_CALL)
1598 {
1599 if (tls_type == GOT_TLS_IE)
1600 new_to_type = R_X86_64_GOTTPOFF;
1601 }
1602
1603 /* We checked the transition before when we were called from
351f65ca 1604 elf_x86_64_check_relocs. We only want to check the new
142411ca
L
1605 transition which hasn't been checked before. */
1606 check = new_to_type != to_type && from_type == to_type;
1607 to_type = new_to_type;
1608 }
1609
1610 break;
1611
bffbf940 1612 case R_X86_64_TLSLD:
0e1862bb 1613 if (bfd_link_executable (info))
142411ca
L
1614 to_type = R_X86_64_TPOFF32;
1615 break;
1616
1617 default:
1618 return TRUE;
bffbf940
JJ
1619 }
1620
142411ca
L
1621 /* Return TRUE if there is no transition. */
1622 if (from_type == to_type)
1623 return TRUE;
1624
1625 /* Check if the transition can be performed. */
1626 if (check
351f65ca
L
1627 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1628 symtab_hdr, sym_hashes,
1629 from_type, rel, relend))
142411ca 1630 {
2f629d23 1631 reloc_howto_type *from, *to;
4c544807 1632 const char *name;
142411ca 1633
351f65ca
L
1634 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1635 to = elf_x86_64_rtype_to_howto (abfd, to_type);
142411ca 1636
4c544807
L
1637 if (h)
1638 name = h->root.root.string;
1639 else
1640 {
351f65ca 1641 struct elf_x86_64_link_hash_table *htab;
4dfe6ac6 1642
351f65ca 1643 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
1644 if (htab == NULL)
1645 name = "*unknown*";
1646 else
1647 {
1648 Elf_Internal_Sym *isym;
1649
1650 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1651 abfd, r_symndx);
1652 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1653 }
4c544807
L
1654 }
1655
142411ca
L
1656 (*_bfd_error_handler)
1657 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1658 "in section `%A' failed"),
4c544807 1659 abfd, sec, from->name, to->name, name,
142411ca
L
1660 (unsigned long) rel->r_offset);
1661 bfd_set_error (bfd_error_bad_value);
1662 return FALSE;
1663 }
1664
1665 *r_type = to_type;
1666 return TRUE;
bffbf940
JJ
1667}
1668
c1d11331
L
1669/* Rename some of the generic section flags to better document how they
1670 are used here. */
338c190a
L
1671#define need_convert_load sec_flg0
1672#define check_relocs_failed sec_flg1
1673
1674static bfd_boolean
1675elf_x86_64_need_pic (bfd *input_bfd, asection *sec,
1676 struct elf_link_hash_entry *h,
1677 Elf_Internal_Shdr *symtab_hdr,
1678 Elf_Internal_Sym *isym,
1679 reloc_howto_type *howto)
1680{
1681 const char *v = "";
1682 const char *und = "";
1683 const char *pic = "";
1684
1685 const char *name;
1686 if (h)
1687 {
1688 name = h->root.root.string;
1689 switch (ELF_ST_VISIBILITY (h->other))
1690 {
1691 case STV_HIDDEN:
1692 v = _("hidden symbol ");
1693 break;
1694 case STV_INTERNAL:
1695 v = _("internal symbol ");
1696 break;
1697 case STV_PROTECTED:
1698 v = _("protected symbol ");
1699 break;
1700 default:
1701 v = _("symbol ");
1702 pic = _("; recompile with -fPIC");
1703 break;
1704 }
1705
1706 if (!h->def_regular && !h->def_dynamic)
1707 und = _("undefined ");
1708 }
1709 else
1710 {
1711 name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
1712 pic = _("; recompile with -fPIC");
1713 }
1714
1715 (*_bfd_error_handler) (_("%B: relocation %s against %s%s`%s' can "
1716 "not be used when making a shared object%s"),
1717 input_bfd, howto->name, und, v, name, pic);
1718 bfd_set_error (bfd_error_bad_value);
1719 sec->check_relocs_failed = 1;
1720 return FALSE;
1721}
c1d11331 1722
c175a657
L
1723/* With the local symbol, foo, we convert
1724 mov foo@GOTPCREL(%rip), %reg
1725 to
1726 lea foo(%rip), %reg
1727 and convert
1728 call/jmp *foo@GOTPCREL(%rip)
1729 to
1730 nop call foo/jmp foo nop
1731 When PIC is false, convert
1732 test %reg, foo@GOTPCREL(%rip)
1733 to
1734 test $foo, %reg
1735 and convert
1736 binop foo@GOTPCREL(%rip), %reg
1737 to
1738 binop $foo, %reg
1739 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1740 instructions. */
1741
1742static bfd_boolean
1743elf_x86_64_convert_load_reloc (bfd *abfd, asection *sec,
1744 bfd_byte *contents,
1745 Elf_Internal_Rela *irel,
1746 struct elf_link_hash_entry *h,
1747 bfd_boolean *converted,
1748 struct bfd_link_info *link_info)
1749{
1750 struct elf_x86_64_link_hash_table *htab;
1751 bfd_boolean is_pic;
1752 bfd_boolean require_reloc_pc32;
1753 bfd_boolean relocx;
1754 bfd_boolean to_reloc_pc32;
1755 asection *tsec;
1756 char symtype;
1757 bfd_signed_vma raddend;
1758 unsigned int opcode;
1759 unsigned int modrm;
1760 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
1761 unsigned int r_symndx;
1762 bfd_vma toff;
1763 bfd_vma roff = irel->r_offset;
1764
1765 if (roff < (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2))
1766 return TRUE;
1767
1768 raddend = irel->r_addend;
1769 /* Addend for 32-bit PC-relative relocation must be -4. */
1770 if (raddend != -4)
1771 return TRUE;
1772
1773 htab = elf_x86_64_hash_table (link_info);
1774 is_pic = bfd_link_pic (link_info);
1775
1776 relocx = (r_type == R_X86_64_GOTPCRELX
1777 || r_type == R_X86_64_REX_GOTPCRELX);
1778
1779 /* TRUE if we can convert only to R_X86_64_PC32. Enable it for
1780 --no-relax. */
1781 require_reloc_pc32
1782 = link_info->disable_target_specific_optimizations > 1;
1783
1784 r_symndx = htab->r_sym (irel->r_info);
1785
1786 opcode = bfd_get_8 (abfd, contents + roff - 2);
1787
1788 /* Convert mov to lea since it has been done for a while. */
1789 if (opcode != 0x8b)
1790 {
1791 /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1792 for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1793 test, xor instructions. */
1794 if (!relocx)
1795 return TRUE;
1796 }
1797
1798 /* We convert only to R_X86_64_PC32:
1799 1. Branch.
1800 2. R_X86_64_GOTPCREL since we can't modify REX byte.
1801 3. require_reloc_pc32 is true.
1802 4. PIC.
1803 */
1804 to_reloc_pc32 = (opcode == 0xff
1805 || !relocx
1806 || require_reloc_pc32
1807 || is_pic);
1808
1809 /* Get the symbol referred to by the reloc. */
1810 if (h == NULL)
1811 {
1812 Elf_Internal_Sym *isym
1813 = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
1814
1815 /* Skip relocation against undefined symbols. */
1816 if (isym->st_shndx == SHN_UNDEF)
1817 return TRUE;
1818
1819 symtype = ELF_ST_TYPE (isym->st_info);
1820
1821 if (isym->st_shndx == SHN_ABS)
1822 tsec = bfd_abs_section_ptr;
1823 else if (isym->st_shndx == SHN_COMMON)
1824 tsec = bfd_com_section_ptr;
1825 else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1826 tsec = &_bfd_elf_large_com_section;
1827 else
1828 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1829
1830 toff = isym->st_value;
1831 }
1832 else
1833 {
1834 /* Undefined weak symbol is only bound locally in executable
1835 and its reference is resolved as 0 without relocation
1836 overflow. We can only perform this optimization for
1837 GOTPCRELX relocations since we need to modify REX byte.
1838 It is OK convert mov with R_X86_64_GOTPCREL to
1839 R_X86_64_PC32. */
1840 if ((relocx || opcode == 0x8b)
1841 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info,
1842 TRUE,
1843 elf_x86_64_hash_entry (h)))
1844 {
1845 if (opcode == 0xff)
1846 {
1847 /* Skip for branch instructions since R_X86_64_PC32
1848 may overflow. */
1849 if (require_reloc_pc32)
1850 return TRUE;
1851 }
1852 else if (relocx)
1853 {
1854 /* For non-branch instructions, we can convert to
1855 R_X86_64_32/R_X86_64_32S since we know if there
1856 is a REX byte. */
1857 to_reloc_pc32 = FALSE;
1858 }
1859
1860 /* Since we don't know the current PC when PIC is true,
1861 we can't convert to R_X86_64_PC32. */
1862 if (to_reloc_pc32 && is_pic)
1863 return TRUE;
1864
1865 goto convert;
1866 }
1867 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1868 ld.so may use its link-time address. */
1869 else if ((h->def_regular
1870 || h->root.type == bfd_link_hash_defined
1871 || h->root.type == bfd_link_hash_defweak)
1872 && h != htab->elf.hdynamic
1873 && SYMBOL_REFERENCES_LOCAL (link_info, h))
1874 {
1875 /* bfd_link_hash_new or bfd_link_hash_undefined is
1876 set by an assignment in a linker script in
1877 bfd_elf_record_link_assignment. */
1878 if (h->def_regular
1879 && (h->root.type == bfd_link_hash_new
1880 || h->root.type == bfd_link_hash_undefined))
1881 {
1882 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
1883 if (require_reloc_pc32)
1884 return TRUE;
1885 goto convert;
1886 }
1887 tsec = h->root.u.def.section;
1888 toff = h->root.u.def.value;
1889 symtype = h->type;
1890 }
1891 else
1892 return TRUE;
1893 }
1894
2168b268
L
1895 /* Don't convert GOTPCREL relocation against large section. */
1896 if (elf_section_data (tsec) != NULL
1897 && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
1898 return TRUE;
1899
c175a657
L
1900 /* We can only estimate relocation overflow for R_X86_64_PC32. */
1901 if (!to_reloc_pc32)
1902 goto convert;
1903
1904 if (tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
1905 {
1906 /* At this stage in linking, no SEC_MERGE symbol has been
1907 adjusted, so all references to such symbols need to be
1908 passed through _bfd_merged_section_offset. (Later, in
1909 relocate_section, all SEC_MERGE symbols *except* for
1910 section symbols have been adjusted.)
1911
1912 gas may reduce relocations against symbols in SEC_MERGE
1913 sections to a relocation against the section symbol when
1914 the original addend was zero. When the reloc is against
1915 a section symbol we should include the addend in the
1916 offset passed to _bfd_merged_section_offset, since the
1917 location of interest is the original symbol. On the
1918 other hand, an access to "sym+addend" where "sym" is not
1919 a section symbol should not include the addend; Such an
1920 access is presumed to be an offset from "sym"; The
1921 location of interest is just "sym". */
1922 if (symtype == STT_SECTION)
1923 toff += raddend;
1924
1925 toff = _bfd_merged_section_offset (abfd, &tsec,
1926 elf_section_data (tsec)->sec_info,
1927 toff);
1928
1929 if (symtype != STT_SECTION)
1930 toff += raddend;
1931 }
1932 else
1933 toff += raddend;
1934
1935 /* Don't convert if R_X86_64_PC32 relocation overflows. */
1936 if (tsec->output_section == sec->output_section)
1937 {
1938 if ((toff - roff + 0x80000000) > 0xffffffff)
1939 return TRUE;
1940 }
1941 else
1942 {
1943 bfd_signed_vma distance;
1944
1945 /* At this point, we don't know the load addresses of TSEC
1946 section nor SEC section. We estimate the distrance between
1947 SEC and TSEC. We store the estimated distances in the
1948 compressed_size field of the output section, which is only
1949 used to decompress the compressed input section. */
1950 if (sec->output_section->compressed_size == 0)
1951 {
1952 asection *asect;
1953 bfd_size_type size = 0;
1954 for (asect = link_info->output_bfd->sections;
1955 asect != NULL;
1956 asect = asect->next)
1957 /* Skip debug sections since compressed_size is used to
1958 compress debug sections. */
1959 if ((asect->flags & SEC_DEBUGGING) == 0)
1960 {
1961 asection *i;
1962 for (i = asect->map_head.s;
1963 i != NULL;
1964 i = i->map_head.s)
1965 {
1966 size = align_power (size, i->alignment_power);
1967 size += i->size;
1968 }
1969 asect->compressed_size = size;
1970 }
1971 }
1972
1973 /* Don't convert GOTPCREL relocations if TSEC isn't placed
1974 after SEC. */
1975 distance = (tsec->output_section->compressed_size
1976 - sec->output_section->compressed_size);
1977 if (distance < 0)
1978 return TRUE;
1979
1980 /* Take PT_GNU_RELRO segment into account by adding
1981 maxpagesize. */
1982 if ((toff + distance + get_elf_backend_data (abfd)->maxpagesize
1983 - roff + 0x80000000) > 0xffffffff)
1984 return TRUE;
1985 }
1986
1987convert:
1988 if (opcode == 0xff)
1989 {
1990 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
1991 unsigned int nop;
1992 unsigned int disp;
1993 bfd_vma nop_offset;
1994
1995 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1996 R_X86_64_PC32. */
1997 modrm = bfd_get_8 (abfd, contents + roff - 1);
1998 if (modrm == 0x25)
1999 {
2000 /* Convert to "jmp foo nop". */
2001 modrm = 0xe9;
2002 nop = NOP_OPCODE;
2003 nop_offset = irel->r_offset + 3;
2004 disp = bfd_get_32 (abfd, contents + irel->r_offset);
2005 irel->r_offset -= 1;
2006 bfd_put_32 (abfd, disp, contents + irel->r_offset);
2007 }
2008 else
2009 {
e2cbcd91
L
2010 struct elf_x86_64_link_hash_entry *eh
2011 = (struct elf_x86_64_link_hash_entry *) h;
2012
c175a657
L
2013 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
2014 is a nop prefix. */
2015 modrm = 0xe8;
e2cbcd91
L
2016 /* To support TLS optimization, always use addr32 prefix for
2017 "call *__tls_get_addr@GOTPCREL(%rip)". */
2018 if (eh && eh->tls_get_addr == 1)
c175a657 2019 {
e2cbcd91
L
2020 nop = 0x67;
2021 nop_offset = irel->r_offset - 2;
c175a657
L
2022 }
2023 else
e2cbcd91
L
2024 {
2025 nop = link_info->call_nop_byte;
2026 if (link_info->call_nop_as_suffix)
2027 {
2028 nop_offset = irel->r_offset + 3;
2029 disp = bfd_get_32 (abfd, contents + irel->r_offset);
2030 irel->r_offset -= 1;
2031 bfd_put_32 (abfd, disp, contents + irel->r_offset);
2032 }
2033 else
2034 nop_offset = irel->r_offset - 2;
2035 }
c175a657
L
2036 }
2037 bfd_put_8 (abfd, nop, contents + nop_offset);
2038 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
2039 r_type = R_X86_64_PC32;
2040 }
2041 else
2042 {
2043 unsigned int rex;
2044 unsigned int rex_mask = REX_R;
2045
2046 if (r_type == R_X86_64_REX_GOTPCRELX)
2047 rex = bfd_get_8 (abfd, contents + roff - 3);
2048 else
2049 rex = 0;
2050
2051 if (opcode == 0x8b)
2052 {
2053 if (to_reloc_pc32)
2054 {
2055 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
2056 "lea foo(%rip), %reg". */
2057 opcode = 0x8d;
2058 r_type = R_X86_64_PC32;
2059 }
2060 else
2061 {
2062 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
2063 "mov $foo, %reg". */
2064 opcode = 0xc7;
2065 modrm = bfd_get_8 (abfd, contents + roff - 1);
2066 modrm = 0xc0 | (modrm & 0x38) >> 3;
2067 if ((rex & REX_W) != 0
2068 && ABI_64_P (link_info->output_bfd))
2069 {
2070 /* Keep the REX_W bit in REX byte for LP64. */
2071 r_type = R_X86_64_32S;
2072 goto rewrite_modrm_rex;
2073 }
2074 else
2075 {
2076 /* If the REX_W bit in REX byte isn't needed,
2077 use R_X86_64_32 and clear the W bit to avoid
2078 sign-extend imm32 to imm64. */
2079 r_type = R_X86_64_32;
2080 /* Clear the W bit in REX byte. */
2081 rex_mask |= REX_W;
2082 goto rewrite_modrm_rex;
2083 }
2084 }
2085 }
2086 else
2087 {
2088 /* R_X86_64_PC32 isn't supported. */
2089 if (to_reloc_pc32)
2090 return TRUE;
2091
2092 modrm = bfd_get_8 (abfd, contents + roff - 1);
2093 if (opcode == 0x85)
2094 {
2095 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
2096 "test $foo, %reg". */
2097 modrm = 0xc0 | (modrm & 0x38) >> 3;
2098 opcode = 0xf7;
2099 }
2100 else
2101 {
2102 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
2103 "binop $foo, %reg". */
2104 modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
2105 opcode = 0x81;
2106 }
2107
2108 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
2109 overflow when sign-extending imm32 to imm64. */
2110 r_type = (rex & REX_W) != 0 ? R_X86_64_32S : R_X86_64_32;
2111
2112rewrite_modrm_rex:
2113 bfd_put_8 (abfd, modrm, contents + roff - 1);
2114
2115 if (rex)
2116 {
2117 /* Move the R bit to the B bit in REX byte. */
2118 rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
2119 bfd_put_8 (abfd, rex, contents + roff - 3);
2120 }
2121
2122 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
2123 irel->r_addend = 0;
2124 }
2125
2126 bfd_put_8 (abfd, opcode, contents + roff - 2);
2127 }
2128
2129 irel->r_info = htab->r_info (r_symndx, r_type);
2130
2131 *converted = TRUE;
2132
2133 return TRUE;
2134}
2135
70256ad8 2136/* Look through the relocs for a section during the first phase, and
c434dee6
AJ
2137 calculate needed space in the global offset table, procedure
2138 linkage table, and dynamic reloc sections. */
70256ad8 2139
b34976b6 2140static bfd_boolean
351f65ca
L
2141elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
2142 asection *sec,
2143 const Elf_Internal_Rela *relocs)
70256ad8 2144{
351f65ca 2145 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
2146 Elf_Internal_Shdr *symtab_hdr;
2147 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
2148 const Elf_Internal_Rela *rel;
2149 const Elf_Internal_Rela *rel_end;
70256ad8 2150 asection *sreloc;
bedfd056 2151 bfd_byte *contents;
dd7e64d4 2152 bfd_boolean use_plt_got;
70256ad8 2153
0e1862bb 2154 if (bfd_link_relocatable (info))
b34976b6 2155 return TRUE;
70256ad8 2156
081b1afe
L
2157 /* Don't do anything special with non-loaded, non-alloced sections.
2158 In particular, any relocs in such sections should not affect GOT
2159 and PLT reference counting (ie. we don't allow them to create GOT
2160 or PLT entries), there's no possibility or desire to optimize TLS
2161 relocs, and there's not much point in propagating relocs to shared
2162 libs that the dynamic linker won't relocate. */
2163 if ((sec->flags & SEC_ALLOC) == 0)
2164 return TRUE;
2165
0ffa91dd
NC
2166 BFD_ASSERT (is_x86_64_elf (abfd));
2167
351f65ca 2168 htab = elf_x86_64_hash_table (info);
4dfe6ac6 2169 if (htab == NULL)
afd9acee
L
2170 {
2171 sec->check_relocs_failed = 1;
2172 return FALSE;
2173 }
4dfe6ac6 2174
bedfd056
L
2175 /* Get the section contents. */
2176 if (elf_section_data (sec)->this_hdr.contents != NULL)
2177 contents = elf_section_data (sec)->this_hdr.contents;
2178 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2179 {
2180 sec->check_relocs_failed = 1;
2181 return FALSE;
2182 }
2183
dd7e64d4
L
2184 use_plt_got = get_elf_x86_64_backend_data (abfd) == &elf_x86_64_arch_bed;
2185
0ffa91dd 2186 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8 2187 sym_hashes = elf_sym_hashes (abfd);
70256ad8 2188
c434dee6 2189 sreloc = NULL;
cbe950e9 2190
70256ad8
AJ
2191 rel_end = relocs + sec->reloc_count;
2192 for (rel = relocs; rel < rel_end; rel++)
2193 {
bffbf940 2194 unsigned int r_type;
70256ad8
AJ
2195 unsigned long r_symndx;
2196 struct elf_link_hash_entry *h;
aec6b87e 2197 struct elf_x86_64_link_hash_entry *eh;
4c544807
L
2198 Elf_Internal_Sym *isym;
2199 const char *name;
06a6a421 2200 bfd_boolean size_reloc;
70256ad8 2201
351f65ca
L
2202 r_symndx = htab->r_sym (rel->r_info);
2203 r_type = ELF32_R_TYPE (rel->r_info);
c434dee6
AJ
2204
2205 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
2206 {
d003868e
AM
2207 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
2208 abfd, r_symndx);
afd9acee 2209 goto error_return;
c434dee6
AJ
2210 }
2211
70256ad8 2212 if (r_symndx < symtab_hdr->sh_info)
c25bc9fc
L
2213 {
2214 /* A local symbol. */
c2e61a4e
L
2215 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2216 abfd, r_symndx);
2217 if (isym == NULL)
afd9acee 2218 goto error_return;
c25bc9fc
L
2219
2220 /* Check relocation against local STT_GNU_IFUNC symbol. */
351f65ca 2221 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c25bc9fc 2222 {
351f65ca
L
2223 h = elf_x86_64_get_local_sym_hash (htab, abfd, rel,
2224 TRUE);
c25bc9fc 2225 if (h == NULL)
afd9acee 2226 goto error_return;
6bbec505 2227
c25bc9fc
L
2228 /* Fake a STT_GNU_IFUNC symbol. */
2229 h->type = STT_GNU_IFUNC;
2230 h->def_regular = 1;
2231 h->ref_regular = 1;
2232 h->forced_local = 1;
2233 h->root.type = bfd_link_hash_defined;
2234 }
2235 else
2236 h = NULL;
2237 }
70256ad8 2238 else
71cb9464 2239 {
4c544807 2240 isym = NULL;
71cb9464
L
2241 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2242 while (h->root.type == bfd_link_hash_indirect
2243 || h->root.type == bfd_link_hash_warning)
2244 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c25bc9fc 2245 }
cbe950e9 2246
d1534d16
L
2247 /* Check invalid x32 relocations. */
2248 if (!ABI_64_P (abfd))
2249 switch (r_type)
2250 {
2251 default:
2252 break;
2253
d1534d16
L
2254 case R_X86_64_DTPOFF64:
2255 case R_X86_64_TPOFF64:
2256 case R_X86_64_PC64:
2257 case R_X86_64_GOTOFF64:
2258 case R_X86_64_GOT64:
2259 case R_X86_64_GOTPCREL64:
2260 case R_X86_64_GOTPC64:
2261 case R_X86_64_GOTPLT64:
2262 case R_X86_64_PLTOFF64:
2263 {
2264 if (h)
2265 name = h->root.root.string;
2266 else
2267 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2268 NULL);
2269 (*_bfd_error_handler)
2270 (_("%B: relocation %s against symbol `%s' isn't "
2271 "supported in x32 mode"), abfd,
2272 x86_64_elf_howto_table[r_type].name, name);
2273 bfd_set_error (bfd_error_bad_value);
afd9acee 2274 goto error_return;
d1534d16
L
2275 }
2276 break;
2277 }
2278
c25bc9fc
L
2279 if (h != NULL)
2280 {
cbe950e9
L
2281 switch (r_type)
2282 {
2283 default:
2284 break;
2285
0ff2b86e
L
2286 case R_X86_64_PC32_BND:
2287 case R_X86_64_PLT32_BND:
d258b828
IZ
2288 case R_X86_64_PC32:
2289 case R_X86_64_PLT32:
2290 case R_X86_64_32:
2291 case R_X86_64_64:
0ff2b86e
L
2292 /* MPX PLT is supported only if elf_x86_64_arch_bed
2293 is used in 64-bit mode. */
2294 if (ABI_64_P (abfd)
d258b828
IZ
2295 && info->bndplt
2296 && (get_elf_x86_64_backend_data (abfd)
2297 == &elf_x86_64_arch_bed))
0ff2b86e 2298 {
bc696fd5 2299 elf_x86_64_hash_entry (h)->has_bnd_reloc = 1;
0ff2b86e
L
2300
2301 /* Create the second PLT for Intel MPX support. */
2302 if (htab->plt_bnd == NULL)
2303 {
2304 unsigned int plt_bnd_align;
2305 const struct elf_backend_data *bed;
2306
2307 bed = get_elf_backend_data (info->output_bfd);
6db50b4c
L
2308 BFD_ASSERT (sizeof (elf_x86_64_bnd_plt2_entry) == 8
2309 && (sizeof (elf_x86_64_bnd_plt2_entry)
2310 == sizeof (elf_x86_64_legacy_plt2_entry)));
2311 plt_bnd_align = 3;
0ff2b86e
L
2312
2313 if (htab->elf.dynobj == NULL)
2314 htab->elf.dynobj = abfd;
2315 htab->plt_bnd
2316 = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2317 ".plt.bnd",
2318 (bed->dynamic_sec_flags
2319 | SEC_ALLOC
2320 | SEC_CODE
2321 | SEC_LOAD
2322 | SEC_READONLY));
2323 if (htab->plt_bnd == NULL
2324 || !bfd_set_section_alignment (htab->elf.dynobj,
2325 htab->plt_bnd,
2326 plt_bnd_align))
afd9acee 2327 goto error_return;
0ff2b86e
L
2328 }
2329 }
2330
cbe950e9 2331 case R_X86_64_32S:
cbe950e9 2332 case R_X86_64_PC64:
cbe950e9 2333 case R_X86_64_GOTPCREL:
56ceb5b5
L
2334 case R_X86_64_GOTPCRELX:
2335 case R_X86_64_REX_GOTPCRELX:
cbe950e9 2336 case R_X86_64_GOTPCREL64:
9d4057ee
AM
2337 if (htab->elf.dynobj == NULL)
2338 htab->elf.dynobj = abfd;
466ee2af
L
2339 /* Create the ifunc sections for static executables. */
2340 if (h->type == STT_GNU_IFUNC
2341 && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj,
2342 info))
afd9acee 2343 goto error_return;
cbe950e9
L
2344 break;
2345 }
2346
ad1e85de
L
2347 /* It is referenced by a non-shared object. */
2348 h->ref_regular = 1;
61315175 2349 h->root.non_ir_ref = 1;
13a2df29
L
2350
2351 if (h->type == STT_GNU_IFUNC)
2352 elf_tdata (info->output_bfd)->has_gnu_symbols
2353 |= elf_gnu_symbol_ifunc;
71cb9464 2354 }
70256ad8 2355
bedfd056 2356 if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
351f65ca
L
2357 symtab_hdr, sym_hashes,
2358 &r_type, GOT_UNKNOWN,
bedfd056 2359 rel, rel_end, h, r_symndx, FALSE))
afd9acee 2360 goto error_return;
142411ca 2361
aec6b87e 2362 eh = (struct elf_x86_64_link_hash_entry *) h;
bffbf940 2363 switch (r_type)
70256ad8 2364 {
bffbf940
JJ
2365 case R_X86_64_TLSLD:
2366 htab->tls_ld_got.refcount += 1;
2367 goto create_got;
2368
2369 case R_X86_64_TPOFF32:
0e1862bb 2370 if (!bfd_link_executable (info) && ABI_64_P (abfd))
338c190a
L
2371 return elf_x86_64_need_pic (abfd, sec, h, symtab_hdr, isym,
2372 &x86_64_elf_howto_table[r_type]);
aec6b87e
L
2373 if (eh != NULL)
2374 eh->has_got_reloc = 1;
bffbf940 2375 break;
c434dee6 2376
bffbf940 2377 case R_X86_64_GOTTPOFF:
0e1862bb 2378 if (!bfd_link_executable (info))
bffbf940
JJ
2379 info->flags |= DF_STATIC_TLS;
2380 /* Fall through */
70256ad8 2381
bffbf940
JJ
2382 case R_X86_64_GOT32:
2383 case R_X86_64_GOTPCREL:
56ceb5b5
L
2384 case R_X86_64_GOTPCRELX:
2385 case R_X86_64_REX_GOTPCRELX:
bffbf940 2386 case R_X86_64_TLSGD:
7b81dfbb
AJ
2387 case R_X86_64_GOT64:
2388 case R_X86_64_GOTPCREL64:
2389 case R_X86_64_GOTPLT64:
67a4f2b7
AO
2390 case R_X86_64_GOTPC32_TLSDESC:
2391 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
2392 /* This symbol requires a global offset table entry. */
2393 {
2394 int tls_type, old_tls_type;
2395
2396 switch (r_type)
2397 {
2398 default: tls_type = GOT_NORMAL; break;
2399 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
2400 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
67a4f2b7
AO
2401 case R_X86_64_GOTPC32_TLSDESC:
2402 case R_X86_64_TLSDESC_CALL:
2403 tls_type = GOT_TLS_GDESC; break;
bffbf940
JJ
2404 }
2405
2406 if (h != NULL)
2407 {
2408 h->got.refcount += 1;
aec6b87e 2409 old_tls_type = eh->tls_type;
bffbf940
JJ
2410 }
2411 else
2412 {
2413 bfd_signed_vma *local_got_refcounts;
2414
2415 /* This is a global offset table entry for a local symbol. */
2416 local_got_refcounts = elf_local_got_refcounts (abfd);
2417 if (local_got_refcounts == NULL)
2418 {
2419 bfd_size_type size;
2420
2421 size = symtab_hdr->sh_info;
67a4f2b7
AO
2422 size *= sizeof (bfd_signed_vma)
2423 + sizeof (bfd_vma) + sizeof (char);
bffbf940
JJ
2424 local_got_refcounts = ((bfd_signed_vma *)
2425 bfd_zalloc (abfd, size));
2426 if (local_got_refcounts == NULL)
afd9acee 2427 goto error_return;
bffbf940 2428 elf_local_got_refcounts (abfd) = local_got_refcounts;
351f65ca 2429 elf_x86_64_local_tlsdesc_gotent (abfd)
67a4f2b7 2430 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
351f65ca 2431 elf_x86_64_local_got_tls_type (abfd)
67a4f2b7 2432 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
bffbf940
JJ
2433 }
2434 local_got_refcounts[r_symndx] += 1;
2435 old_tls_type
351f65ca 2436 = elf_x86_64_local_got_tls_type (abfd) [r_symndx];
bffbf940
JJ
2437 }
2438
2439 /* If a TLS symbol is accessed using IE at least once,
2440 there is no point to use dynamic model for it. */
2441 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7
AO
2442 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2443 || tls_type != GOT_TLS_IE))
bffbf940 2444 {
67a4f2b7 2445 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
bffbf940 2446 tls_type = old_tls_type;
67a4f2b7
AO
2447 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2448 && GOT_TLS_GD_ANY_P (tls_type))
2449 tls_type |= old_tls_type;
bffbf940
JJ
2450 else
2451 {
09a24cbf 2452 if (h)
4c544807
L
2453 name = h->root.root.string;
2454 else
2455 name = bfd_elf_sym_name (abfd, symtab_hdr,
2456 isym, NULL);
bffbf940 2457 (*_bfd_error_handler)
1f7a4e42 2458 (_("%B: '%s' accessed both as normal and thread local symbol"),
4c544807 2459 abfd, name);
68c4a57e 2460 bfd_set_error (bfd_error_bad_value);
afd9acee 2461 goto error_return;
bffbf940
JJ
2462 }
2463 }
2464
2465 if (old_tls_type != tls_type)
2466 {
aec6b87e
L
2467 if (eh != NULL)
2468 eh->tls_type = tls_type;
bffbf940 2469 else
351f65ca 2470 elf_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
bffbf940
JJ
2471 }
2472 }
c434dee6
AJ
2473 /* Fall through */
2474
d6ab8113
JB
2475 case R_X86_64_GOTOFF64:
2476 case R_X86_64_GOTPC32:
7b81dfbb 2477 case R_X86_64_GOTPC64:
bffbf940 2478 create_got:
aec6b87e
L
2479 if (eh != NULL)
2480 eh->has_got_reloc = 1;
6de2ae4a 2481 if (htab->elf.sgot == NULL)
c434dee6
AJ
2482 {
2483 if (htab->elf.dynobj == NULL)
2484 htab->elf.dynobj = abfd;
6de2ae4a
L
2485 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
2486 info))
afd9acee 2487 goto error_return;
c434dee6 2488 }
70256ad8
AJ
2489 break;
2490
2491 case R_X86_64_PLT32:
c3320543 2492 case R_X86_64_PLT32_BND:
70256ad8 2493 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
2494 actually build the entry in adjust_dynamic_symbol,
2495 because this might be a case of linking PIC code which is
2496 never referenced by a dynamic object, in which case we
2497 don't need to generate a procedure linkage table entry
2498 after all. */
70256ad8
AJ
2499
2500 /* If this is a local symbol, we resolve it directly without
407443a3 2501 creating a procedure linkage table entry. */
70256ad8
AJ
2502 if (h == NULL)
2503 continue;
2504
aec6b87e 2505 eh->has_got_reloc = 1;
f5385ebf 2506 h->needs_plt = 1;
51b64d56 2507 h->plt.refcount += 1;
70256ad8
AJ
2508 break;
2509
7b81dfbb
AJ
2510 case R_X86_64_PLTOFF64:
2511 /* This tries to form the 'address' of a function relative
2512 to GOT. For global symbols we need a PLT entry. */
2513 if (h != NULL)
2514 {
2515 h->needs_plt = 1;
2516 h->plt.refcount += 1;
2517 }
2518 goto create_got;
2519
6a3e1bae
L
2520 case R_X86_64_SIZE32:
2521 case R_X86_64_SIZE64:
06a6a421 2522 size_reloc = TRUE;
6a3e1bae
L
2523 goto do_size;
2524
248775ba
L
2525 case R_X86_64_32:
2526 if (!ABI_64_P (abfd))
2527 goto pointer;
cc78d0af
AJ
2528 case R_X86_64_8:
2529 case R_X86_64_16:
70256ad8 2530 case R_X86_64_32S:
338c190a
L
2531 /* Check relocation overflow as these relocs may lead to
2532 run-time relocation overflow. Don't error out for
1b71fb54 2533 sections we don't care about, such as debug sections or
338c190a 2534 when relocation overflow check is disabled. */
4c10bbaa 2535 if (!info->no_reloc_overflow_check
338c190a
L
2536 && (bfd_link_pic (info)
2537 || (bfd_link_executable (info)
2538 && h != NULL
2539 && !h->def_regular
2540 && h->def_dynamic
081b1afe 2541 && (sec->flags & SEC_READONLY) == 0)))
338c190a
L
2542 return elf_x86_64_need_pic (abfd, sec, h, symtab_hdr, isym,
2543 &x86_64_elf_howto_table[r_type]);
1b71fb54
AJ
2544 /* Fall through. */
2545
c434dee6
AJ
2546 case R_X86_64_PC8:
2547 case R_X86_64_PC16:
70256ad8 2548 case R_X86_64_PC32:
c3320543 2549 case R_X86_64_PC32_BND:
d6ab8113 2550 case R_X86_64_PC64:
1b71fb54 2551 case R_X86_64_64:
248775ba 2552pointer:
aec6b87e
L
2553 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2554 eh->has_non_got_reloc = 1;
bf52d7c7
L
2555 /* We are called after all symbols have been resolved. Only
2556 relocation against STT_GNU_IFUNC symbol must go through
2557 PLT. */
d1ed1c7d
L
2558 if (h != NULL
2559 && (bfd_link_executable (info)
bf52d7c7 2560 || h->type == STT_GNU_IFUNC))
c434dee6
AJ
2561 {
2562 /* If this reloc is in a read-only section, we might
2563 need a copy reloc. We can't check reliably at this
2564 stage whether the section is read-only, as input
2565 sections have not yet been mapped to output sections.
2566 Tentatively set the flag for now, and correct in
2567 adjust_dynamic_symbol. */
f5385ebf 2568 h->non_got_ref = 1;
c434dee6 2569
233cc9c1
L
2570 /* We may need a .plt entry if the symbol is a function
2571 defined in a shared lib or is a STT_GNU_IFUNC function
2572 referenced from the code or read-only section. */
2573 if (!h->def_regular
2574 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2575 h->plt.refcount += 1;
2576
5db4f0d3
L
2577 if (r_type == R_X86_64_PC32)
2578 {
2579 /* Since something like ".long foo - ." may be used
2580 as pointer, make sure that PLT is used if foo is
2581 a function defined in a shared library. */
2582 if ((sec->flags & SEC_CODE) == 0)
2583 h->pointer_equality_needed = 1;
2584 }
2585 else if (r_type != R_X86_64_PC32_BND
2586 && r_type != R_X86_64_PC64)
04ebc307
L
2587 {
2588 h->pointer_equality_needed = 1;
2589 /* At run-time, R_X86_64_64 can be resolved for both
2590 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2591 can only be resolved for x32. */
2592 if ((sec->flags & SEC_READONLY) == 0
2593 && (r_type == R_X86_64_64
2594 || (!ABI_64_P (abfd)
2595 && (r_type == R_X86_64_32
2596 || r_type == R_X86_64_32S))))
aec6b87e 2597 eh->func_pointer_refcount += 1;
04ebc307 2598 }
c434dee6 2599 }
70256ad8 2600
06a6a421 2601 size_reloc = FALSE;
6a3e1bae 2602do_size:
70256ad8
AJ
2603 /* If we are creating a shared library, and this is a reloc
2604 against a global symbol, or a non PC relative reloc
2605 against a local symbol, then we need to copy the reloc
2606 into the shared library. However, if we are linking with
2607 -Bsymbolic, we do not need to copy a reloc against a
2608 global symbol which is defined in an object we are
407443a3 2609 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
2610 this point we have not seen all the input files, so it is
2611 possible that DEF_REGULAR is not set now but will be set
c434dee6
AJ
2612 later (it is never cleared). In case of a weak definition,
2613 DEF_REGULAR may be cleared later by a strong definition in
2614 a shared library. We account for that possibility below by
2615 storing information in the relocs_copied field of the hash
2616 table entry. A similar situation occurs when creating
2617 shared libraries and symbol visibility changes render the
31c0ebfe 2618 symbol local.
c434dee6
AJ
2619
2620 If on the other hand, we are creating an executable, we
2621 may need to keep relocations for symbols satisfied by a
2622 dynamic library if we manage to avoid copy relocs for the
233cc9c1
L
2623 symbol.
2624
2625 Generate dynamic pointer relocation against STT_GNU_IFUNC
2626 symbol in the non-code section. */
081b1afe
L
2627 if ((bfd_link_pic (info)
2628 && (! IS_X86_64_PCREL_TYPE (r_type)
2629 || (h != NULL
2630 && (! (bfd_link_pie (info)
2631 || SYMBOLIC_BIND (info, h))
2632 || h->root.type == bfd_link_hash_defweak
2633 || !h->def_regular))))
233cc9c1
L
2634 || (h != NULL
2635 && h->type == STT_GNU_IFUNC
2636 && r_type == htab->pointer_r_type
2637 && (sec->flags & SEC_CODE) == 0)
081b1afe
L
2638 || (ELIMINATE_COPY_RELOCS
2639 && !bfd_link_pic (info)
2640 && h != NULL
2641 && (h->root.type == bfd_link_hash_defweak
2642 || !h->def_regular)))
70256ad8 2643 {
e03a8ed8
L
2644 struct elf_dyn_relocs *p;
2645 struct elf_dyn_relocs **head;
c434dee6
AJ
2646
2647 /* We must copy these reloc types into the output file.
2648 Create a reloc section in dynobj and make room for
2649 this reloc. */
70256ad8
AJ
2650 if (sreloc == NULL)
2651 {
c434dee6
AJ
2652 if (htab->elf.dynobj == NULL)
2653 htab->elf.dynobj = abfd;
2654
83bac4b0 2655 sreloc = _bfd_elf_make_dynamic_reloc_section
82e96e07
L
2656 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
2657 abfd, /*rela?*/ TRUE);
70256ad8 2658
70256ad8 2659 if (sreloc == NULL)
afd9acee 2660 goto error_return;
70256ad8
AJ
2661 }
2662
c434dee6
AJ
2663 /* If this is a global symbol, we count the number of
2664 relocations we need for this symbol. */
2665 if (h != NULL)
aec6b87e 2666 head = &eh->dyn_relocs;
c434dee6
AJ
2667 else
2668 {
2669 /* Track dynamic relocs needed for local syms too.
2670 We really need local syms available to do this
2671 easily. Oh well. */
c434dee6 2672 asection *s;
87d72d41 2673 void **vpp;
87d72d41
AM
2674
2675 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2676 abfd, r_symndx);
2677 if (isym == NULL)
afd9acee 2678 goto error_return;
87d72d41
AM
2679
2680 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
c434dee6 2681 if (s == NULL)
87d72d41 2682 s = sec;
70256ad8 2683
e81d3500
DD
2684 /* Beware of type punned pointers vs strict aliasing
2685 rules. */
2686 vpp = &(elf_section_data (s)->local_dynrel);
e03a8ed8 2687 head = (struct elf_dyn_relocs **)vpp;
c434dee6 2688 }
70256ad8 2689
c434dee6
AJ
2690 p = *head;
2691 if (p == NULL || p->sec != sec)
2692 {
2693 bfd_size_type amt = sizeof *p;
d8045f23 2694
e03a8ed8 2695 p = ((struct elf_dyn_relocs *)
c434dee6 2696 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 2697 if (p == NULL)
afd9acee 2698 goto error_return;
c434dee6
AJ
2699 p->next = *head;
2700 *head = p;
2701 p->sec = sec;
2702 p->count = 0;
2703 p->pc_count = 0;
70256ad8 2704 }
c434dee6
AJ
2705
2706 p->count += 1;
06a6a421
L
2707 /* Count size relocation as PC-relative relocation. */
2708 if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
c434dee6 2709 p->pc_count += 1;
70256ad8
AJ
2710 }
2711 break;
fe4770f4
AJ
2712
2713 /* This relocation describes the C++ object vtable hierarchy.
2714 Reconstruct it for later use during GC. */
2715 case R_X86_64_GNU_VTINHERIT:
c152c796 2716 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
afd9acee 2717 goto error_return;
fe4770f4
AJ
2718 break;
2719
2720 /* This relocation describes which C++ vtable entries are actually
2721 used. Record for later use during GC. */
2722 case R_X86_64_GNU_VTENTRY:
d17e0c6e
JB
2723 BFD_ASSERT (h != NULL);
2724 if (h != NULL
2725 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
afd9acee 2726 goto error_return;
fe4770f4 2727 break;
c434dee6
AJ
2728
2729 default:
2730 break;
70256ad8 2731 }
dd7e64d4
L
2732
2733 if (use_plt_got
2734 && h != NULL
2735 && h->plt.refcount > 0
8ded2ddc
L
2736 && (((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
2737 || h->got.refcount > 0)
dd7e64d4
L
2738 && htab->plt_got == NULL)
2739 {
2740 /* Create the GOT procedure linkage table. */
2741 unsigned int plt_got_align;
2742 const struct elf_backend_data *bed;
2743
2744 bed = get_elf_backend_data (info->output_bfd);
2745 BFD_ASSERT (sizeof (elf_x86_64_legacy_plt2_entry) == 8
2746 && (sizeof (elf_x86_64_bnd_plt2_entry)
2747 == sizeof (elf_x86_64_legacy_plt2_entry)));
2748 plt_got_align = 3;
2749
2750 if (htab->elf.dynobj == NULL)
2751 htab->elf.dynobj = abfd;
2752 htab->plt_got
2753 = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2754 ".plt.got",
2755 (bed->dynamic_sec_flags
2756 | SEC_ALLOC
2757 | SEC_CODE
2758 | SEC_LOAD
2759 | SEC_READONLY));
2760 if (htab->plt_got == NULL
2761 || !bfd_set_section_alignment (htab->elf.dynobj,
2762 htab->plt_got,
2763 plt_got_align))
afd9acee 2764 goto error_return;
dd7e64d4 2765 }
c1d11331 2766
56ceb5b5
L
2767 if ((r_type == R_X86_64_GOTPCREL
2768 || r_type == R_X86_64_GOTPCRELX
2769 || r_type == R_X86_64_REX_GOTPCRELX)
c1d11331 2770 && (h == NULL || h->type != STT_GNU_IFUNC))
56ceb5b5 2771 sec->need_convert_load = 1;
70256ad8
AJ
2772 }
2773
bedfd056
L
2774 if (elf_section_data (sec)->this_hdr.contents != contents)
2775 {
2776 if (!info->keep_memory)
2777 free (contents);
2778 else
2779 {
2780 /* Cache the section contents for elf_link_input_bfd. */
2781 elf_section_data (sec)->this_hdr.contents = contents;
2782 }
2783 }
2784
b34976b6 2785 return TRUE;
afd9acee
L
2786
2787error_return:
bedfd056
L
2788 if (elf_section_data (sec)->this_hdr.contents != contents)
2789 free (contents);
afd9acee
L
2790 sec->check_relocs_failed = 1;
2791 return FALSE;
70256ad8
AJ
2792}
2793
2794/* Return the section that should be marked against GC for a given
407443a3 2795 relocation. */
70256ad8
AJ
2796
2797static asection *
351f65ca
L
2798elf_x86_64_gc_mark_hook (asection *sec,
2799 struct bfd_link_info *info,
2800 Elf_Internal_Rela *rel,
2801 struct elf_link_hash_entry *h,
2802 Elf_Internal_Sym *sym)
70256ad8
AJ
2803{
2804 if (h != NULL)
351f65ca 2805 switch (ELF32_R_TYPE (rel->r_info))
07adf181
AM
2806 {
2807 case R_X86_64_GNU_VTINHERIT:
2808 case R_X86_64_GNU_VTENTRY:
2809 return NULL;
2810 }
2811
2812 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
70256ad8
AJ
2813}
2814
aec6b87e
L
2815/* Remove undefined weak symbol from the dynamic symbol table if it
2816 is resolved to 0. */
2817
2818static bfd_boolean
2819elf_x86_64_fixup_symbol (struct bfd_link_info *info,
2820 struct elf_link_hash_entry *h)
2821{
2822 if (h->dynindx != -1
2823 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
e62b9723 2824 elf_x86_64_hash_entry (h)->has_got_reloc,
aec6b87e
L
2825 elf_x86_64_hash_entry (h)))
2826 {
2827 h->dynindx = -1;
2828 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2829 h->dynstr_index);
2830 }
2831 return TRUE;
2832}
2833
70256ad8
AJ
2834/* Adjust a symbol defined by a dynamic object and referenced by a
2835 regular object. The current definition is in some section of the
2836 dynamic object, but we're not including those sections. We have to
2837 change the definition to something the rest of the link can
407443a3 2838 understand. */
70256ad8 2839
b34976b6 2840static bfd_boolean
351f65ca
L
2841elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
2842 struct elf_link_hash_entry *h)
70256ad8 2843{
351f65ca 2844 struct elf_x86_64_link_hash_table *htab;
70256ad8 2845 asection *s;
5ca5bb35
L
2846 struct elf_x86_64_link_hash_entry *eh;
2847 struct elf_dyn_relocs *p;
70256ad8 2848
cbe950e9
L
2849 /* STT_GNU_IFUNC symbol must go through PLT. */
2850 if (h->type == STT_GNU_IFUNC)
2851 {
73bcf233
L
2852 /* All local STT_GNU_IFUNC references must be treate as local
2853 calls via local PLT. */
5ca5bb35
L
2854 if (h->ref_regular
2855 && SYMBOL_CALLS_LOCAL (info, h))
2856 {
73bcf233 2857 bfd_size_type pc_count = 0, count = 0;
5ca5bb35
L
2858 struct elf_dyn_relocs **pp;
2859
2860 eh = (struct elf_x86_64_link_hash_entry *) h;
2861 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2862 {
2863 pc_count += p->pc_count;
2864 p->count -= p->pc_count;
2865 p->pc_count = 0;
73bcf233 2866 count += p->count;
5ca5bb35
L
2867 if (p->count == 0)
2868 *pp = p->next;
2869 else
2870 pp = &p->next;
2871 }
2872
73bcf233 2873 if (pc_count || count)
5ca5bb35 2874 {
5ca5bb35 2875 h->non_got_ref = 1;
233cc9c1
L
2876 if (pc_count)
2877 {
2878 /* Increment PLT reference count only for PC-relative
2879 references. */
2880 h->needs_plt = 1;
2881 if (h->plt.refcount <= 0)
2882 h->plt.refcount = 1;
2883 else
2884 h->plt.refcount += 1;
2885 }
5ca5bb35
L
2886 }
2887 }
2888
cbe950e9
L
2889 if (h->plt.refcount <= 0)
2890 {
2891 h->plt.offset = (bfd_vma) -1;
2892 h->needs_plt = 0;
2893 }
2894 return TRUE;
2895 }
2896
70256ad8
AJ
2897 /* If this is a function, put it in the procedure linkage table. We
2898 will fill in the contents of the procedure linkage table later,
2899 when we know the address of the .got section. */
2900 if (h->type == STT_FUNC
f5385ebf 2901 || h->needs_plt)
70256ad8 2902 {
c434dee6 2903 if (h->plt.refcount <= 0
27482721
AJ
2904 || SYMBOL_CALLS_LOCAL (info, h)
2905 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2906 && h->root.type == bfd_link_hash_undefweak))
70256ad8 2907 {
70256ad8
AJ
2908 /* This case can occur if we saw a PLT32 reloc in an input
2909 file, but the symbol was never referred to by a dynamic
2910 object, or if all references were garbage collected. In
2911 such a case, we don't actually need to build a procedure
2912 linkage table, and we can just do a PC32 reloc instead. */
70256ad8 2913 h->plt.offset = (bfd_vma) -1;
f5385ebf 2914 h->needs_plt = 0;
70256ad8
AJ
2915 }
2916
b34976b6 2917 return TRUE;
70256ad8 2918 }
bbd7ec4a 2919 else
c434dee6
AJ
2920 /* It's possible that we incorrectly decided a .plt reloc was
2921 needed for an R_X86_64_PC32 reloc to a non-function sym in
2922 check_relocs. We can't decide accurately between function and
2923 non-function syms in check-relocs; Objects loaded later in
2924 the link may change h->type. So fix it now. */
bbd7ec4a 2925 h->plt.offset = (bfd_vma) -1;
70256ad8
AJ
2926
2927 /* If this is a weak symbol, and there is a real definition, the
2928 processor independent code will have arranged for us to see the
407443a3 2929 real definition first, and we can just use the same value. */
f6e332e6 2930 if (h->u.weakdef != NULL)
70256ad8 2931 {
f6e332e6
AM
2932 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2933 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2934 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2935 h->root.u.def.value = h->u.weakdef->root.u.def.value;
d40d037c 2936 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
bc696fd5
L
2937 {
2938 eh = (struct elf_x86_64_link_hash_entry *) h;
2939 h->non_got_ref = h->u.weakdef->non_got_ref;
2940 eh->needs_copy = h->u.weakdef->needs_copy;
2941 }
b34976b6 2942 return TRUE;
70256ad8
AJ
2943 }
2944
2945 /* This is a reference to a symbol defined by a dynamic object which
407443a3 2946 is not a function. */
70256ad8
AJ
2947
2948 /* If we are creating a shared library, we must presume that the
2949 only references to the symbol are via the global offset table.
2950 For such cases we need not do anything here; the relocations will
407443a3 2951 be handled correctly by relocate_section. */
0e1862bb 2952 if (!bfd_link_executable (info))
b34976b6 2953 return TRUE;
70256ad8
AJ
2954
2955 /* If there are no references to this symbol that do not use the
2956 GOT, we don't need to generate a copy reloc. */
f5385ebf 2957 if (!h->non_got_ref)
b34976b6 2958 return TRUE;
70256ad8 2959
c434dee6
AJ
2960 /* If -z nocopyreloc was given, we won't generate them either. */
2961 if (info->nocopyreloc)
2962 {
f5385ebf 2963 h->non_got_ref = 0;
b34976b6 2964 return TRUE;
c434dee6
AJ
2965 }
2966
31c0ebfe 2967 if (ELIMINATE_COPY_RELOCS)
c434dee6 2968 {
351f65ca 2969 eh = (struct elf_x86_64_link_hash_entry *) h;
d40d037c
AJ
2970 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2971 {
2972 s = p->sec->output_section;
2973 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2974 break;
2975 }
2976
2977 /* If we didn't find any dynamic relocs in read-only sections, then
2978 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2979 if (p == NULL)
2980 {
f5385ebf 2981 h->non_got_ref = 0;
d40d037c
AJ
2982 return TRUE;
2983 }
c434dee6
AJ
2984 }
2985
70256ad8 2986 /* We must allocate the symbol in our .dynbss section, which will
407443a3 2987 become part of the .bss section of the executable. There will be
70256ad8
AJ
2988 an entry for this symbol in the .dynsym section. The dynamic
2989 object will contain position independent code, so all references
2990 from the dynamic object to this symbol will go through the global
2991 offset table. The dynamic linker will use the .dynsym entry to
2992 determine the address it must put in the global offset table, so
2993 both the dynamic object and the regular object will refer to the
2994 same memory location for the variable. */
2995
351f65ca 2996 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2997 if (htab == NULL)
2998 return FALSE;
70256ad8
AJ
2999
3000 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
3001 to copy the initial value out of the dynamic object and into the
cedb70c5 3002 runtime process image. */
1d7e9d18 3003 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
70256ad8 3004 {
351f65ca
L
3005 const struct elf_backend_data *bed;
3006 bed = get_elf_backend_data (info->output_bfd);
3007 htab->srelbss->size += bed->s->sizeof_rela;
f5385ebf 3008 h->needs_copy = 1;
70256ad8
AJ
3009 }
3010
c434dee6 3011 s = htab->sdynbss;
70256ad8 3012
6cabe1ea 3013 return _bfd_elf_adjust_dynamic_copy (info, h, s);
70256ad8
AJ
3014}
3015
c434dee6
AJ
3016/* Allocate space in .plt, .got and associated reloc sections for
3017 dynamic relocs. */
3018
b34976b6 3019static bfd_boolean
351f65ca 3020elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
3021{
3022 struct bfd_link_info *info;
351f65ca
L
3023 struct elf_x86_64_link_hash_table *htab;
3024 struct elf_x86_64_link_hash_entry *eh;
e03a8ed8 3025 struct elf_dyn_relocs *p;
351f65ca 3026 const struct elf_backend_data *bed;
eed180f8 3027 unsigned int plt_entry_size;
aec6b87e 3028 bfd_boolean resolved_to_zero;
c434dee6 3029
e92d460e 3030 if (h->root.type == bfd_link_hash_indirect)
b34976b6 3031 return TRUE;
c434dee6 3032
351f65ca 3033 eh = (struct elf_x86_64_link_hash_entry *) h;
e92d460e 3034
c434dee6 3035 info = (struct bfd_link_info *) inf;
351f65ca 3036 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3037 if (htab == NULL)
3038 return FALSE;
351f65ca 3039 bed = get_elf_backend_data (info->output_bfd);
eed180f8 3040 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
c434dee6 3041
e62b9723
L
3042 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
3043 eh->has_got_reloc,
3044 eh);
aec6b87e 3045
dd7e64d4
L
3046 /* We can't use the GOT PLT if pointer equality is needed since
3047 finish_dynamic_symbol won't clear symbol value and the dynamic
3048 linker won't update the GOT slot. We will get into an infinite
3049 loop at run-time. */
3050 if (htab->plt_got != NULL
3051 && h->type != STT_GNU_IFUNC
3052 && !h->pointer_equality_needed
3053 && h->plt.refcount > 0
3054 && h->got.refcount > 0)
3055 {
3056 /* Don't use the regular PLT if there are both GOT and GOTPLT
3057 reloctions. */
3058 h->plt.offset = (bfd_vma) -1;
3059
3060 /* Use the GOT PLT. */
3061 eh->plt_got.refcount = 1;
3062 }
3063
04ebc307
L
3064 /* Clear the reference count of function pointer relocations if
3065 symbol isn't a normal function. */
3066 if (h->type != STT_FUNC)
3067 eh->func_pointer_refcount = 0;
3068
cbe950e9
L
3069 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
3070 here if it is defined and referenced in a non-shared object. */
3071 if (h->type == STT_GNU_IFUNC
3072 && h->def_regular)
0ff2b86e
L
3073 {
3074 if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h,
3075 &eh->dyn_relocs,
2df3368d 3076 &htab->readonly_dynrelocs_against_ifunc,
0ff2b86e
L
3077 plt_entry_size,
3078 plt_entry_size,
233cc9c1 3079 GOT_ENTRY_SIZE, TRUE))
0ff2b86e
L
3080 {
3081 asection *s = htab->plt_bnd;
3082 if (h->plt.offset != (bfd_vma) -1 && s != NULL)
3083 {
3084 /* Use the .plt.bnd section if it is created. */
3085 eh->plt_bnd.offset = s->size;
3086
3087 /* Make room for this entry in the .plt.bnd section. */
3088 s->size += sizeof (elf_x86_64_legacy_plt2_entry);
3089 }
3090
3091 return TRUE;
3092 }
3093 else
3094 return FALSE;
3095 }
04ebc307
L
3096 /* Don't create the PLT entry if there are only function pointer
3097 relocations which can be resolved at run-time. */
cbe950e9 3098 else if (htab->elf.dynamic_sections_created
04ebc307
L
3099 && (h->plt.refcount > eh->func_pointer_refcount
3100 || eh->plt_got.refcount > 0))
c434dee6 3101 {
25070364
L
3102 bfd_boolean use_plt_got;
3103
04ebc307
L
3104 /* Clear the reference count of function pointer relocations
3105 if PLT is used. */
3106 eh->func_pointer_refcount = 0;
3107
8ded2ddc 3108 if ((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
25070364
L
3109 {
3110 /* Don't use the regular PLT for DF_BIND_NOW. */
3111 h->plt.offset = (bfd_vma) -1;
3112
3113 /* Use the GOT PLT. */
3114 h->got.refcount = 1;
3115 eh->plt_got.refcount = 1;
3116 }
3117
3118 use_plt_got = eh->plt_got.refcount > 0;
dd7e64d4 3119
c434dee6
AJ
3120 /* Make sure this symbol is output as a dynamic symbol.
3121 Undefined weak syms won't yet be marked as dynamic. */
3122 if (h->dynindx == -1
aec6b87e
L
3123 && !h->forced_local
3124 && !resolved_to_zero)
c434dee6 3125 {
c152c796 3126 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3127 return FALSE;
c434dee6
AJ
3128 }
3129
0e1862bb 3130 if (bfd_link_pic (info)
27482721 3131 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
c434dee6 3132 {
6de2ae4a 3133 asection *s = htab->elf.splt;
0ff2b86e 3134 asection *bnd_s = htab->plt_bnd;
dd7e64d4 3135 asection *got_s = htab->plt_got;
c434dee6 3136
7c1e8d3e
L
3137 /* If this is the first .plt entry, make room for the special
3138 first entry. The .plt section is used by prelink to undo
3139 prelinking for dynamic relocations. */
3140 if (s->size == 0)
3141 s->size = plt_entry_size;
3142
dd7e64d4
L
3143 if (use_plt_got)
3144 eh->plt_got.offset = got_s->size;
3145 else
3146 {
3147 h->plt.offset = s->size;
3148 if (bnd_s)
3149 eh->plt_bnd.offset = bnd_s->size;
3150 }
c434dee6
AJ
3151
3152 /* If this symbol is not defined in a regular file, and we are
3153 not generating a shared library, then set the symbol to this
3154 location in the .plt. This is required to make function
3155 pointers compare as equal between the normal executable and
3156 the shared library. */
0e1862bb 3157 if (! bfd_link_pic (info)
f5385ebf 3158 && !h->def_regular)
c434dee6 3159 {
dd7e64d4 3160 if (use_plt_got)
0ff2b86e 3161 {
dd7e64d4
L
3162 /* We need to make a call to the entry of the GOT PLT
3163 instead of regular PLT entry. */
3164 h->root.u.def.section = got_s;
3165 h->root.u.def.value = eh->plt_got.offset;
0ff2b86e
L
3166 }
3167 else
3168 {
dd7e64d4
L
3169 if (bnd_s)
3170 {
3171 /* We need to make a call to the entry of the second
3172 PLT instead of regular PLT entry. */
3173 h->root.u.def.section = bnd_s;
3174 h->root.u.def.value = eh->plt_bnd.offset;
3175 }
3176 else
3177 {
3178 h->root.u.def.section = s;
3179 h->root.u.def.value = h->plt.offset;
3180 }
0ff2b86e 3181 }
c434dee6
AJ
3182 }
3183
3184 /* Make room for this entry. */
dd7e64d4
L
3185 if (use_plt_got)
3186 got_s->size += sizeof (elf_x86_64_legacy_plt2_entry);
3187 else
3188 {
3189 s->size += plt_entry_size;
3190 if (bnd_s)
3191 bnd_s->size += sizeof (elf_x86_64_legacy_plt2_entry);
c434dee6 3192
dd7e64d4
L
3193 /* We also need to make an entry in the .got.plt section,
3194 which will be placed in the .got section by the linker
3195 script. */
3196 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
c434dee6 3197
aec6b87e
L
3198 /* There should be no PLT relocation against resolved
3199 undefined weak symbol in executable. */
3200 if (!resolved_to_zero)
3201 {
3202 /* We also need to make an entry in the .rela.plt
3203 section. */
3204 htab->elf.srelplt->size += bed->s->sizeof_rela;
3205 htab->elf.srelplt->reloc_count++;
3206 }
dd7e64d4 3207 }
c434dee6
AJ
3208 }
3209 else
3210 {
13f42277 3211 eh->plt_got.offset = (bfd_vma) -1;
c434dee6 3212 h->plt.offset = (bfd_vma) -1;
f5385ebf 3213 h->needs_plt = 0;
c434dee6
AJ
3214 }
3215 }
3216 else
3217 {
13f42277 3218 eh->plt_got.offset = (bfd_vma) -1;
c434dee6 3219 h->plt.offset = (bfd_vma) -1;
f5385ebf 3220 h->needs_plt = 0;
c434dee6
AJ
3221 }
3222
67a4f2b7
AO
3223 eh->tlsdesc_got = (bfd_vma) -1;
3224
bffbf940
JJ
3225 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
3226 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
3227 if (h->got.refcount > 0
0e1862bb 3228 && bfd_link_executable (info)
bffbf940 3229 && h->dynindx == -1
351f65ca 3230 && elf_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
d8045f23
NC
3231 {
3232 h->got.offset = (bfd_vma) -1;
3233 }
bffbf940 3234 else if (h->got.refcount > 0)
c434dee6
AJ
3235 {
3236 asection *s;
b34976b6 3237 bfd_boolean dyn;
351f65ca 3238 int tls_type = elf_x86_64_hash_entry (h)->tls_type;
c434dee6
AJ
3239
3240 /* Make sure this symbol is output as a dynamic symbol.
3241 Undefined weak syms won't yet be marked as dynamic. */
3242 if (h->dynindx == -1
aec6b87e
L
3243 && !h->forced_local
3244 && !resolved_to_zero)
c434dee6 3245 {
c152c796 3246 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3247 return FALSE;
c434dee6
AJ
3248 }
3249
67a4f2b7
AO
3250 if (GOT_TLS_GDESC_P (tls_type))
3251 {
6de2ae4a 3252 eh->tlsdesc_got = htab->elf.sgotplt->size
351f65ca 3253 - elf_x86_64_compute_jump_table_size (htab);
6de2ae4a 3254 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
67a4f2b7
AO
3255 h->got.offset = (bfd_vma) -2;
3256 }
3257 if (! GOT_TLS_GDESC_P (tls_type)
3258 || GOT_TLS_GD_P (tls_type))
3259 {
6de2ae4a 3260 s = htab->elf.sgot;
67a4f2b7
AO
3261 h->got.offset = s->size;
3262 s->size += GOT_ENTRY_SIZE;
3263 if (GOT_TLS_GD_P (tls_type))
3264 s->size += GOT_ENTRY_SIZE;
3265 }
c434dee6 3266 dyn = htab->elf.dynamic_sections_created;
bffbf940 3267 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
aec6b87e
L
3268 and two if global. R_X86_64_GOTTPOFF needs one dynamic
3269 relocation. No dynamic relocation against resolved undefined
3270 weak symbol in executable. */
67a4f2b7 3271 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
bffbf940 3272 || tls_type == GOT_TLS_IE)
351f65ca 3273 htab->elf.srelgot->size += bed->s->sizeof_rela;
67a4f2b7 3274 else if (GOT_TLS_GD_P (tls_type))
351f65ca 3275 htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
67a4f2b7 3276 else if (! GOT_TLS_GDESC_P (tls_type)
aec6b87e
L
3277 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3278 && !resolved_to_zero)
67a4f2b7 3279 || h->root.type != bfd_link_hash_undefweak)
0e1862bb 3280 && (bfd_link_pic (info)
27482721 3281 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
351f65ca 3282 htab->elf.srelgot->size += bed->s->sizeof_rela;
67a4f2b7
AO
3283 if (GOT_TLS_GDESC_P (tls_type))
3284 {
351f65ca 3285 htab->elf.srelplt->size += bed->s->sizeof_rela;
67a4f2b7
AO
3286 htab->tlsdesc_plt = (bfd_vma) -1;
3287 }
c434dee6
AJ
3288 }
3289 else
3290 h->got.offset = (bfd_vma) -1;
3291
c434dee6 3292 if (eh->dyn_relocs == NULL)
b34976b6 3293 return TRUE;
c434dee6
AJ
3294
3295 /* In the shared -Bsymbolic case, discard space allocated for
3296 dynamic pc-relative relocs against symbols which turn out to be
3297 defined in regular objects. For the normal shared case, discard
3298 space for pc-relative relocs that have become local due to symbol
3299 visibility changes. */
3300
0e1862bb 3301 if (bfd_link_pic (info))
c434dee6 3302 {
27482721
AJ
3303 /* Relocs that use pc_count are those that appear on a call
3304 insn, or certain REL relocs that can generated via assembly.
3305 We want calls to protected symbols to resolve directly to the
3306 function rather than going via the plt. If people want
3307 function pointer comparisons to work as expected then they
3308 should avoid writing weird assembly. */
3309 if (SYMBOL_CALLS_LOCAL (info, h))
c434dee6 3310 {
e03a8ed8 3311 struct elf_dyn_relocs **pp;
c434dee6
AJ
3312
3313 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3314 {
c3ce498c
L
3315 p->count -= p->pc_count;
3316 p->pc_count = 0;
c434dee6
AJ
3317 if (p->count == 0)
3318 *pp = p->next;
3319 else
3320 pp = &p->next;
3321 }
3322 }
4e795f50
AM
3323
3324 /* Also discard relocs on undefined weak syms with non-default
aec6b87e 3325 visibility or in PIE. */
31c0ebfe 3326 if (eh->dyn_relocs != NULL)
22d606e9 3327 {
31c0ebfe
L
3328 if (h->root.type == bfd_link_hash_undefweak)
3329 {
aec6b87e
L
3330 /* Undefined weak symbol is never bound locally in shared
3331 library. */
3332 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3333 || resolved_to_zero)
31c0ebfe 3334 eh->dyn_relocs = NULL;
31c0ebfe
L
3335 else if (h->dynindx == -1
3336 && ! h->forced_local
3337 && ! bfd_elf_link_record_dynamic_symbol (info, h))
3338 return FALSE;
3339 }
9d1d54d5
L
3340 /* For PIE, discard space for pc-relative relocs against
3341 symbols which turn out to need copy relocs. */
0e1862bb 3342 else if (bfd_link_executable (info)
bc696fd5 3343 && (h->needs_copy || eh->needs_copy)
31c0ebfe
L
3344 && h->def_dynamic
3345 && !h->def_regular)
9d1d54d5
L
3346 {
3347 struct elf_dyn_relocs **pp;
3348
3349 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3350 {
3351 if (p->pc_count != 0)
3352 *pp = p->next;
3353 else
3354 pp = &p->next;
3355 }
3356 }
22d606e9 3357 }
d8045f23 3358 }
d40d037c 3359 else if (ELIMINATE_COPY_RELOCS)
c434dee6
AJ
3360 {
3361 /* For the non-shared case, discard space for relocs against
3362 symbols which turn out to need copy relocs or are not
04ebc307
L
3363 dynamic. Keep dynamic relocations for run-time function
3364 pointer initialization. */
c434dee6 3365
aec6b87e
L
3366 if ((!h->non_got_ref
3367 || eh->func_pointer_refcount > 0
3368 || (h->root.type == bfd_link_hash_undefweak
3369 && !resolved_to_zero))
f5385ebf
AM
3370 && ((h->def_dynamic
3371 && !h->def_regular)
c434dee6
AJ
3372 || (htab->elf.dynamic_sections_created
3373 && (h->root.type == bfd_link_hash_undefweak
3374 || h->root.type == bfd_link_hash_undefined))))
3375 {
3376 /* Make sure this symbol is output as a dynamic symbol.
3377 Undefined weak syms won't yet be marked as dynamic. */
3378 if (h->dynindx == -1
d8045f23 3379 && ! h->forced_local
aec6b87e 3380 && ! resolved_to_zero
d8045f23
NC
3381 && ! bfd_elf_link_record_dynamic_symbol (info, h))
3382 return FALSE;
c434dee6
AJ
3383
3384 /* If that succeeded, we know we'll be keeping all the
3385 relocs. */
3386 if (h->dynindx != -1)
3387 goto keep;
3388 }
3389
3390 eh->dyn_relocs = NULL;
04ebc307 3391 eh->func_pointer_refcount = 0;
c434dee6
AJ
3392
3393 keep: ;
3394 }
3395
3396 /* Finally, allocate space. */
3397 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3398 {
e7c33416
NC
3399 asection * sreloc;
3400
cbe950e9 3401 sreloc = elf_section_data (p->sec)->sreloc;
e7c33416
NC
3402
3403 BFD_ASSERT (sreloc != NULL);
3404
351f65ca 3405 sreloc->size += p->count * bed->s->sizeof_rela;
c434dee6
AJ
3406 }
3407
b34976b6 3408 return TRUE;
c434dee6
AJ
3409}
3410
c25bc9fc
L
3411/* Allocate space in .plt, .got and associated reloc sections for
3412 local dynamic relocs. */
3413
3414static bfd_boolean
351f65ca 3415elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
c25bc9fc
L
3416{
3417 struct elf_link_hash_entry *h
3418 = (struct elf_link_hash_entry *) *slot;
3419
3420 if (h->type != STT_GNU_IFUNC
3421 || !h->def_regular
3422 || !h->ref_regular
3423 || !h->forced_local
3424 || h->root.type != bfd_link_hash_defined)
3425 abort ();
3426
351f65ca 3427 return elf_x86_64_allocate_dynrelocs (h, inf);
c25bc9fc
L
3428}
3429
c434dee6
AJ
3430/* Find any dynamic relocs that apply to read-only sections. */
3431
b34976b6 3432static bfd_boolean
351f65ca
L
3433elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h,
3434 void * inf)
c434dee6 3435{
351f65ca 3436 struct elf_x86_64_link_hash_entry *eh;
e03a8ed8 3437 struct elf_dyn_relocs *p;
c434dee6 3438
aa715242
L
3439 /* Skip local IFUNC symbols. */
3440 if (h->forced_local && h->type == STT_GNU_IFUNC)
3441 return TRUE;
3442
351f65ca 3443 eh = (struct elf_x86_64_link_hash_entry *) h;
c434dee6
AJ
3444 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3445 {
3446 asection *s = p->sec->output_section;
3447
3448 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3449 {
3450 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3451
3452 info->flags |= DF_TEXTREL;
3453
0e1862bb 3454 if ((info->warn_shared_textrel && bfd_link_pic (info))
1952c5cd
L
3455 || info->error_textrel)
3456 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
b70321a2
L
3457 p->sec->owner, h->root.root.string,
3458 p->sec);
3459
c434dee6 3460 /* Not an error, just cut short the traversal. */
b34976b6 3461 return FALSE;
c434dee6
AJ
3462 }
3463 }
b34976b6 3464 return TRUE;
c434dee6
AJ
3465}
3466
c175a657 3467/* Convert load via the GOT slot to load immediate. */
daa67607
L
3468
3469static bfd_boolean
56ceb5b5
L
3470elf_x86_64_convert_load (bfd *abfd, asection *sec,
3471 struct bfd_link_info *link_info)
daa67607
L
3472{
3473 Elf_Internal_Shdr *symtab_hdr;
3474 Elf_Internal_Rela *internal_relocs;
3475 Elf_Internal_Rela *irel, *irelend;
3476 bfd_byte *contents;
3477 struct elf_x86_64_link_hash_table *htab;
c175a657 3478 bfd_boolean changed;
daa67607
L
3479 bfd_signed_vma *local_got_refcounts;
3480
3481 /* Don't even try to convert non-ELF outputs. */
3482 if (!is_elf_hash_table (link_info->hash))
3483 return FALSE;
3484
c1d11331 3485 /* Nothing to do if there is no need or no output. */
daa67607 3486 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
56ceb5b5 3487 || sec->need_convert_load == 0
c8831961 3488 || bfd_is_abs_section (sec->output_section))
daa67607
L
3489 return TRUE;
3490
3491 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3492
3493 /* Load the relocations for this section. */
3494 internal_relocs = (_bfd_elf_link_read_relocs
3495 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
3496 link_info->keep_memory));
3497 if (internal_relocs == NULL)
3498 return FALSE;
3499
c175a657 3500 changed = FALSE;
daa67607 3501 htab = elf_x86_64_hash_table (link_info);
daa67607
L
3502 local_got_refcounts = elf_local_got_refcounts (abfd);
3503
3504 /* Get the section contents. */
3505 if (elf_section_data (sec)->this_hdr.contents != NULL)
3506 contents = elf_section_data (sec)->this_hdr.contents;
3507 else
3508 {
3509 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3510 goto error_return;
3511 }
3512
3513 irelend = internal_relocs + sec->reloc_count;
3514 for (irel = internal_relocs; irel < irelend; irel++)
3515 {
3516 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
c175a657 3517 unsigned int r_symndx;
daa67607 3518 struct elf_link_hash_entry *h;
c175a657 3519 bfd_boolean converted;
59cab532 3520
c175a657
L
3521 if (r_type != R_X86_64_GOTPCRELX
3522 && r_type != R_X86_64_REX_GOTPCRELX
3523 && r_type != R_X86_64_GOTPCREL)
dfc87947
L
3524 continue;
3525
c175a657 3526 r_symndx = htab->r_sym (irel->r_info);
daa67607 3527 if (r_symndx < symtab_hdr->sh_info)
c175a657
L
3528 h = elf_x86_64_get_local_sym_hash (htab, sec->owner,
3529 (const Elf_Internal_Rela *) irel,
3530 FALSE);
59cab532
L
3531 else
3532 {
c175a657 3533 h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
59cab532
L
3534 while (h->root.type == bfd_link_hash_indirect
3535 || h->root.type == bfd_link_hash_warning)
3536 h = (struct elf_link_hash_entry *) h->root.u.i.link;
59cab532
L
3537 }
3538
c175a657
L
3539 /* STT_GNU_IFUNC must keep GOTPCREL relocations. */
3540 if (h != NULL && h->type == STT_GNU_IFUNC)
3541 continue;
daa67607 3542
c175a657
L
3543 converted = FALSE;
3544 if (!elf_x86_64_convert_load_reloc (abfd, sec, contents, irel, h,
3545 &converted, link_info))
3546 goto error_return;
56ceb5b5 3547
c175a657 3548 if (converted)
56ceb5b5 3549 {
c175a657
L
3550 changed = converted;
3551 if (h)
56ceb5b5 3552 {
c175a657
L
3553 if (h->got.refcount > 0)
3554 h->got.refcount -= 1;
56ceb5b5
L
3555 }
3556 else
3557 {
c175a657
L
3558 if (local_got_refcounts != NULL
3559 && local_got_refcounts[r_symndx] > 0)
3560 local_got_refcounts[r_symndx] -= 1;
56ceb5b5 3561 }
daa67607
L
3562 }
3563 }
3564
3565 if (contents != NULL
3566 && elf_section_data (sec)->this_hdr.contents != contents)
3567 {
c175a657 3568 if (!changed && !link_info->keep_memory)
daa67607
L
3569 free (contents);
3570 else
3571 {
3572 /* Cache the section contents for elf_link_input_bfd. */
3573 elf_section_data (sec)->this_hdr.contents = contents;
3574 }
3575 }
3576
3577 if (elf_section_data (sec)->relocs != internal_relocs)
3578 {
c175a657 3579 if (!changed)
daa67607
L
3580 free (internal_relocs);
3581 else
3582 elf_section_data (sec)->relocs = internal_relocs;
3583 }
3584
3585 return TRUE;
3586
3587 error_return:
3588 if (contents != NULL
3589 && elf_section_data (sec)->this_hdr.contents != contents)
3590 free (contents);
3591 if (internal_relocs != NULL
3592 && elf_section_data (sec)->relocs != internal_relocs)
3593 free (internal_relocs);
3594 return FALSE;
3595}
3596
70256ad8
AJ
3597/* Set the sizes of the dynamic sections. */
3598
b34976b6 3599static bfd_boolean
351f65ca
L
3600elf_x86_64_size_dynamic_sections (bfd *output_bfd,
3601 struct bfd_link_info *info)
70256ad8 3602{
351f65ca 3603 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
3604 bfd *dynobj;
3605 asection *s;
b34976b6 3606 bfd_boolean relocs;
c434dee6 3607 bfd *ibfd;
351f65ca 3608 const struct elf_backend_data *bed;
70256ad8 3609
351f65ca 3610 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3611 if (htab == NULL)
3612 return FALSE;
351f65ca 3613 bed = get_elf_backend_data (output_bfd);
4dfe6ac6 3614
c434dee6
AJ
3615 dynobj = htab->elf.dynobj;
3616 if (dynobj == NULL)
3617 abort ();
70256ad8 3618
c434dee6
AJ
3619 /* Set up .got offsets for local syms, and space for local dynamic
3620 relocs. */
c72f2fb2 3621 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
70256ad8 3622 {
c434dee6
AJ
3623 bfd_signed_vma *local_got;
3624 bfd_signed_vma *end_local_got;
bffbf940 3625 char *local_tls_type;
67a4f2b7 3626 bfd_vma *local_tlsdesc_gotent;
c434dee6
AJ
3627 bfd_size_type locsymcount;
3628 Elf_Internal_Shdr *symtab_hdr;
3629 asection *srel;
70256ad8 3630
0ffa91dd 3631 if (! is_x86_64_elf (ibfd))
70256ad8
AJ
3632 continue;
3633
c434dee6 3634 for (s = ibfd->sections; s != NULL; s = s->next)
70256ad8 3635 {
e03a8ed8 3636 struct elf_dyn_relocs *p;
c434dee6 3637
56ceb5b5 3638 if (!elf_x86_64_convert_load (ibfd, s, info))
daa67607
L
3639 return FALSE;
3640
e03a8ed8 3641 for (p = (struct elf_dyn_relocs *)
e81d3500 3642 (elf_section_data (s)->local_dynrel);
c434dee6
AJ
3643 p != NULL;
3644 p = p->next)
70256ad8 3645 {
c434dee6
AJ
3646 if (!bfd_is_abs_section (p->sec)
3647 && bfd_is_abs_section (p->sec->output_section))
3648 {
3649 /* Input section has been discarded, either because
3650 it is a copy of a linkonce section or due to
3651 linker script /DISCARD/, so we'll be discarding
3652 the relocs too. */
3653 }
3654 else if (p->count != 0)
3655 {
3656 srel = elf_section_data (p->sec)->sreloc;
351f65ca 3657 srel->size += p->count * bed->s->sizeof_rela;
4b819e1f
L
3658 if ((p->sec->output_section->flags & SEC_READONLY) != 0
3659 && (info->flags & DF_TEXTREL) == 0)
b70321a2
L
3660 {
3661 info->flags |= DF_TEXTREL;
0e1862bb 3662 if ((info->warn_shared_textrel && bfd_link_pic (info))
1952c5cd
L
3663 || info->error_textrel)
3664 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
b70321a2 3665 p->sec->owner, p->sec);
b70321a2 3666 }
c434dee6 3667 }
70256ad8
AJ
3668 }
3669 }
c434dee6
AJ
3670
3671 local_got = elf_local_got_refcounts (ibfd);
3672 if (!local_got)
3673 continue;
3674
0ffa91dd 3675 symtab_hdr = &elf_symtab_hdr (ibfd);
c434dee6
AJ
3676 locsymcount = symtab_hdr->sh_info;
3677 end_local_got = local_got + locsymcount;
351f65ca
L
3678 local_tls_type = elf_x86_64_local_got_tls_type (ibfd);
3679 local_tlsdesc_gotent = elf_x86_64_local_tlsdesc_gotent (ibfd);
6de2ae4a
L
3680 s = htab->elf.sgot;
3681 srel = htab->elf.srelgot;
67a4f2b7
AO
3682 for (; local_got < end_local_got;
3683 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
70256ad8 3684 {
67a4f2b7 3685 *local_tlsdesc_gotent = (bfd_vma) -1;
c434dee6 3686 if (*local_got > 0)
70256ad8 3687 {
67a4f2b7
AO
3688 if (GOT_TLS_GDESC_P (*local_tls_type))
3689 {
6de2ae4a 3690 *local_tlsdesc_gotent = htab->elf.sgotplt->size
351f65ca 3691 - elf_x86_64_compute_jump_table_size (htab);
6de2ae4a 3692 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
67a4f2b7
AO
3693 *local_got = (bfd_vma) -2;
3694 }
3695 if (! GOT_TLS_GDESC_P (*local_tls_type)
3696 || GOT_TLS_GD_P (*local_tls_type))
3697 {
3698 *local_got = s->size;
3699 s->size += GOT_ENTRY_SIZE;
3700 if (GOT_TLS_GD_P (*local_tls_type))
3701 s->size += GOT_ENTRY_SIZE;
3702 }
0e1862bb 3703 if (bfd_link_pic (info)
67a4f2b7 3704 || GOT_TLS_GD_ANY_P (*local_tls_type)
bffbf940 3705 || *local_tls_type == GOT_TLS_IE)
67a4f2b7
AO
3706 {
3707 if (GOT_TLS_GDESC_P (*local_tls_type))
3708 {
6de2ae4a 3709 htab->elf.srelplt->size
351f65ca 3710 += bed->s->sizeof_rela;
67a4f2b7
AO
3711 htab->tlsdesc_plt = (bfd_vma) -1;
3712 }
3713 if (! GOT_TLS_GDESC_P (*local_tls_type)
3714 || GOT_TLS_GD_P (*local_tls_type))
351f65ca 3715 srel->size += bed->s->sizeof_rela;
67a4f2b7 3716 }
70256ad8
AJ
3717 }
3718 else
c434dee6
AJ
3719 *local_got = (bfd_vma) -1;
3720 }
3721 }
70256ad8 3722
bffbf940
JJ
3723 if (htab->tls_ld_got.refcount > 0)
3724 {
3725 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
3726 relocs. */
6de2ae4a
L
3727 htab->tls_ld_got.offset = htab->elf.sgot->size;
3728 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
351f65ca 3729 htab->elf.srelgot->size += bed->s->sizeof_rela;
bffbf940
JJ
3730 }
3731 else
3732 htab->tls_ld_got.offset = -1;
3733
c434dee6
AJ
3734 /* Allocate global sym .plt and .got entries, and space for global
3735 sym dynamic relocs. */
351f65ca 3736 elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
eb4ff4d6 3737 info);
c434dee6 3738
c25bc9fc
L
3739 /* Allocate .plt and .got entries, and space for local symbols. */
3740 htab_traverse (htab->loc_hash_table,
351f65ca 3741 elf_x86_64_allocate_local_dynrelocs,
c25bc9fc
L
3742 info);
3743
67a4f2b7
AO
3744 /* For every jump slot reserved in the sgotplt, reloc_count is
3745 incremented. However, when we reserve space for TLS descriptors,
3746 it's not incremented, so in order to compute the space reserved
3747 for them, it suffices to multiply the reloc count by the jump
e1f98742
L
3748 slot size.
3749
3750 PR ld/13302: We start next_irelative_index at the end of .rela.plt
3751 so that R_X86_64_IRELATIVE entries come last. */
6de2ae4a 3752 if (htab->elf.srelplt)
e1f98742
L
3753 {
3754 htab->sgotplt_jump_table_size
3755 = elf_x86_64_compute_jump_table_size (htab);
3756 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
3757 }
3758 else if (htab->elf.irelplt)
3759 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
67a4f2b7
AO
3760
3761 if (htab->tlsdesc_plt)
3762 {
3763 /* If we're not using lazy TLS relocations, don't generate the
3764 PLT and GOT entries they require. */
3765 if ((info->flags & DF_BIND_NOW))
3766 htab->tlsdesc_plt = 0;
3767 else
3768 {
6de2ae4a
L
3769 htab->tlsdesc_got = htab->elf.sgot->size;
3770 htab->elf.sgot->size += GOT_ENTRY_SIZE;
67a4f2b7
AO
3771 /* Reserve room for the initial entry.
3772 FIXME: we could probably do away with it in this case. */
6de2ae4a 3773 if (htab->elf.splt->size == 0)
eed180f8 3774 htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
6de2ae4a 3775 htab->tlsdesc_plt = htab->elf.splt->size;
eed180f8 3776 htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
67a4f2b7
AO
3777 }
3778 }
3779
a7b16ceb
L
3780 if (htab->elf.sgotplt)
3781 {
3782 /* Don't allocate .got.plt section if there are no GOT nor PLT
eed180f8 3783 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
9d4b2dba
L
3784 if ((htab->elf.hgot == NULL
3785 || !htab->elf.hgot->ref_regular_nonweak)
e28df02b
L
3786 && (htab->elf.sgotplt->size
3787 == get_elf_backend_data (output_bfd)->got_header_size)
a7b16ceb
L
3788 && (htab->elf.splt == NULL
3789 || htab->elf.splt->size == 0)
3790 && (htab->elf.sgot == NULL
3791 || htab->elf.sgot->size == 0)
3792 && (htab->elf.iplt == NULL
3793 || htab->elf.iplt->size == 0)
3794 && (htab->elf.igotplt == NULL
3795 || htab->elf.igotplt->size == 0))
3796 htab->elf.sgotplt->size = 0;
3797 }
3798
9a2a56cc
AM
3799 if (htab->plt_eh_frame != NULL
3800 && htab->elf.splt != NULL
3801 && htab->elf.splt->size != 0
3802 && !bfd_is_abs_section (htab->elf.splt->output_section)
3803 && _bfd_elf_eh_frame_present (info))
3804 {
3805 const struct elf_x86_64_backend_data *arch_data
f8222080 3806 = get_elf_x86_64_arch_data (bed);
9a2a56cc
AM
3807 htab->plt_eh_frame->size = arch_data->eh_frame_plt_size;
3808 }
3809
c434dee6
AJ
3810 /* We now have determined the sizes of the various dynamic sections.
3811 Allocate memory for them. */
b34976b6 3812 relocs = FALSE;
c434dee6
AJ
3813 for (s = dynobj->sections; s != NULL; s = s->next)
3814 {
3815 if ((s->flags & SEC_LINKER_CREATED) == 0)
3816 continue;
3817
6de2ae4a
L
3818 if (s == htab->elf.splt
3819 || s == htab->elf.sgot
3820 || s == htab->elf.sgotplt
3821 || s == htab->elf.iplt
3822 || s == htab->elf.igotplt
0ff2b86e 3823 || s == htab->plt_bnd
dd7e64d4 3824 || s == htab->plt_got
9a2a56cc 3825 || s == htab->plt_eh_frame
75ff4589 3826 || s == htab->sdynbss)
c434dee6
AJ
3827 {
3828 /* Strip this section if we don't need it; see the
3829 comment below. */
3830 }
0112cd26 3831 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
c434dee6 3832 {
6de2ae4a 3833 if (s->size != 0 && s != htab->elf.srelplt)
b34976b6 3834 relocs = TRUE;
c434dee6
AJ
3835
3836 /* We use the reloc_count field as a counter if we need
3837 to copy relocs into the output file. */
6de2ae4a 3838 if (s != htab->elf.srelplt)
67a4f2b7 3839 s->reloc_count = 0;
70256ad8 3840 }
c434dee6 3841 else
70256ad8
AJ
3842 {
3843 /* It's not one of our sections, so don't allocate space. */
3844 continue;
3845 }
3846
eea6121a 3847 if (s->size == 0)
70256ad8 3848 {
c434dee6
AJ
3849 /* If we don't need this section, strip it from the
3850 output file. This is mostly to handle .rela.bss and
3851 .rela.plt. We must create both sections in
3852 create_dynamic_sections, because they must be created
3853 before the linker maps input sections to output
3854 sections. The linker does that before
3855 adjust_dynamic_symbol is called, and it is that
3856 function which decides whether anything needs to go
3857 into these sections. */
3858
8423293d 3859 s->flags |= SEC_EXCLUDE;
70256ad8
AJ
3860 continue;
3861 }
3862
c456f082
AM
3863 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3864 continue;
3865
70256ad8
AJ
3866 /* Allocate memory for the section contents. We use bfd_zalloc
3867 here in case unused entries are not reclaimed before the
3868 section's contents are written out. This should not happen,
3869 but this way if it does, we get a R_X86_64_NONE reloc instead
3870 of garbage. */
eea6121a 3871 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c434dee6 3872 if (s->contents == NULL)
b34976b6 3873 return FALSE;
70256ad8
AJ
3874 }
3875
e41b3a13 3876 if (htab->plt_eh_frame != NULL
9a2a56cc
AM
3877 && htab->plt_eh_frame->contents != NULL)
3878 {
3879 const struct elf_x86_64_backend_data *arch_data
f8222080 3880 = get_elf_x86_64_arch_data (bed);
9a2a56cc
AM
3881
3882 memcpy (htab->plt_eh_frame->contents,
3883 arch_data->eh_frame_plt, htab->plt_eh_frame->size);
3884 bfd_put_32 (dynobj, htab->elf.splt->size,
3885 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3886 }
e41b3a13 3887
c434dee6 3888 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
3889 {
3890 /* Add some entries to the .dynamic section. We fill in the
351f65ca 3891 values later, in elf_x86_64_finish_dynamic_sections, but we
70256ad8 3892 must add the entries now so that we get the correct size for
407443a3 3893 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8 3894 dynamic linker and used by the debugger. */
dc810e39 3895#define add_dynamic_entry(TAG, VAL) \
5a580b3a 3896 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 3897
0e1862bb 3898 if (bfd_link_executable (info))
70256ad8 3899 {
dc810e39 3900 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 3901 return FALSE;
70256ad8
AJ
3902 }
3903
6de2ae4a 3904 if (htab->elf.splt->size != 0)
70256ad8 3905 {
7c1e8d3e
L
3906 /* DT_PLTGOT is used by prelink even if there is no PLT
3907 relocation. */
3908 if (!add_dynamic_entry (DT_PLTGOT, 0))
b34976b6 3909 return FALSE;
67a4f2b7 3910
7c1e8d3e
L
3911 if (htab->elf.srelplt->size != 0)
3912 {
3913 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3914 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3915 || !add_dynamic_entry (DT_JMPREL, 0))
3916 return FALSE;
3917 }
3918
67a4f2b7
AO
3919 if (htab->tlsdesc_plt
3920 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
3921 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
3922 return FALSE;
70256ad8
AJ
3923 }
3924
3925 if (relocs)
3926 {
dc810e39
AM
3927 if (!add_dynamic_entry (DT_RELA, 0)
3928 || !add_dynamic_entry (DT_RELASZ, 0)
351f65ca 3929 || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
b34976b6 3930 return FALSE;
70256ad8 3931
c434dee6
AJ
3932 /* If any dynamic relocs apply to a read-only section,
3933 then we need a DT_TEXTREL entry. */
3934 if ((info->flags & DF_TEXTREL) == 0)
eed180f8 3935 elf_link_hash_traverse (&htab->elf,
351f65ca 3936 elf_x86_64_readonly_dynrelocs,
eb4ff4d6 3937 info);
c434dee6
AJ
3938
3939 if ((info->flags & DF_TEXTREL) != 0)
3940 {
2df3368d 3941 if (htab->readonly_dynrelocs_against_ifunc)
8efa2874
L
3942 {
3943 info->callbacks->einfo
3944 (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
3945 bfd_set_error (bfd_error_bad_value);
3946 return FALSE;
3947 }
3948
c434dee6 3949 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 3950 return FALSE;
c434dee6 3951 }
70256ad8
AJ
3952 }
3953 }
dc810e39 3954#undef add_dynamic_entry
70256ad8 3955
b34976b6 3956 return TRUE;
70256ad8
AJ
3957}
3958
67a4f2b7 3959static bfd_boolean
351f65ca
L
3960elf_x86_64_always_size_sections (bfd *output_bfd,
3961 struct bfd_link_info *info)
67a4f2b7
AO
3962{
3963 asection *tls_sec = elf_hash_table (info)->tls_sec;
3964
3965 if (tls_sec)
3966 {
3967 struct elf_link_hash_entry *tlsbase;
3968
3969 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3970 "_TLS_MODULE_BASE_",
3971 FALSE, FALSE, FALSE);
3972
3973 if (tlsbase && tlsbase->type == STT_TLS)
3974 {
351f65ca 3975 struct elf_x86_64_link_hash_table *htab;
67a4f2b7
AO
3976 struct bfd_link_hash_entry *bh = NULL;
3977 const struct elf_backend_data *bed
3978 = get_elf_backend_data (output_bfd);
3979
351f65ca 3980 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3981 if (htab == NULL)
3982 return FALSE;
3983
67a4f2b7
AO
3984 if (!(_bfd_generic_link_add_one_symbol
3985 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3986 tls_sec, 0, NULL, FALSE,
3987 bed->collect, &bh)))
3988 return FALSE;
9f03412a 3989
4dfe6ac6 3990 htab->tls_module_base = bh;
9f03412a 3991
67a4f2b7
AO
3992 tlsbase = (struct elf_link_hash_entry *)bh;
3993 tlsbase->def_regular = 1;
3994 tlsbase->other = STV_HIDDEN;
576fa883 3995 tlsbase->root.linker_def = 1;
67a4f2b7
AO
3996 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
3997 }
3998 }
3999
4000 return TRUE;
4001}
4002
9f03412a
AO
4003/* _TLS_MODULE_BASE_ needs to be treated especially when linking
4004 executables. Rather than setting it to the beginning of the TLS
4005 section, we have to set it to the end. This function may be called
4006 multiple times, it is idempotent. */
4007
4008static void
351f65ca 4009elf_x86_64_set_tls_module_base (struct bfd_link_info *info)
9f03412a 4010{
351f65ca 4011 struct elf_x86_64_link_hash_table *htab;
9f03412a
AO
4012 struct bfd_link_hash_entry *base;
4013
0e1862bb 4014 if (!bfd_link_executable (info))
9f03412a
AO
4015 return;
4016
351f65ca 4017 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
4018 if (htab == NULL)
4019 return;
9f03412a 4020
4dfe6ac6
NC
4021 base = htab->tls_module_base;
4022 if (base == NULL)
9f03412a
AO
4023 return;
4024
4dfe6ac6 4025 base->u.def.value = htab->elf.tls_size;
9f03412a
AO
4026}
4027
bffbf940
JJ
4028/* Return the base VMA address which should be subtracted from real addresses
4029 when resolving @dtpoff relocation.
4030 This is PT_TLS segment p_vaddr. */
4031
4032static bfd_vma
351f65ca 4033elf_x86_64_dtpoff_base (struct bfd_link_info *info)
bffbf940 4034{
e1918d23
AM
4035 /* If tls_sec is NULL, we should have signalled an error already. */
4036 if (elf_hash_table (info)->tls_sec == NULL)
bffbf940 4037 return 0;
e1918d23 4038 return elf_hash_table (info)->tls_sec->vma;
bffbf940
JJ
4039}
4040
4041/* Return the relocation value for @tpoff relocation
4042 if STT_TLS virtual address is ADDRESS. */
4043
4044static bfd_vma
351f65ca 4045elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 4046{
e1918d23 4047 struct elf_link_hash_table *htab = elf_hash_table (info);
7dc98aea
RO
4048 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
4049 bfd_vma static_tls_size;
bffbf940
JJ
4050
4051 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 4052 if (htab->tls_sec == NULL)
bffbf940 4053 return 0;
7dc98aea
RO
4054
4055 /* Consider special static TLS alignment requirements. */
4056 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
4057 return address - static_tls_size - htab->tls_sec->vma;
bffbf940
JJ
4058}
4059
90f487df
L
4060/* Is the instruction before OFFSET in CONTENTS a 32bit relative
4061 branch? */
4062
4063static bfd_boolean
4064is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
4065{
4066 /* Opcode Instruction
4067 0xe8 call
4068 0xe9 jump
4069 0x0f 0x8x conditional jump */
4070 return ((offset > 0
4071 && (contents [offset - 1] == 0xe8
4072 || contents [offset - 1] == 0xe9))
4073 || (offset > 1
4074 && contents [offset - 2] == 0x0f
4075 && (contents [offset - 1] & 0xf0) == 0x80));
4076}
4077
8d88c4ca
NC
4078/* Relocate an x86_64 ELF section. */
4079
b34976b6 4080static bfd_boolean
351f65ca
L
4081elf_x86_64_relocate_section (bfd *output_bfd,
4082 struct bfd_link_info *info,
4083 bfd *input_bfd,
4084 asection *input_section,
4085 bfd_byte *contents,
4086 Elf_Internal_Rela *relocs,
4087 Elf_Internal_Sym *local_syms,
4088 asection **local_sections)
8d88c4ca 4089{
351f65ca 4090 struct elf_x86_64_link_hash_table *htab;
8d88c4ca
NC
4091 Elf_Internal_Shdr *symtab_hdr;
4092 struct elf_link_hash_entry **sym_hashes;
4093 bfd_vma *local_got_offsets;
67a4f2b7 4094 bfd_vma *local_tlsdesc_gotents;
c434dee6 4095 Elf_Internal_Rela *rel;
60f2e42e 4096 Elf_Internal_Rela *wrel;
8d88c4ca 4097 Elf_Internal_Rela *relend;
eed180f8 4098 const unsigned int plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
8d88c4ca 4099
0ffa91dd
NC
4100 BFD_ASSERT (is_x86_64_elf (input_bfd));
4101
338c190a
L
4102 /* Skip if check_relocs failed. */
4103 if (input_section->check_relocs_failed)
4104 return FALSE;
4105
351f65ca 4106 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
4107 if (htab == NULL)
4108 return FALSE;
0ffa91dd 4109 symtab_hdr = &elf_symtab_hdr (input_bfd);
8d88c4ca
NC
4110 sym_hashes = elf_sym_hashes (input_bfd);
4111 local_got_offsets = elf_local_got_offsets (input_bfd);
351f65ca 4112 local_tlsdesc_gotents = elf_x86_64_local_tlsdesc_gotent (input_bfd);
8d88c4ca 4113
351f65ca 4114 elf_x86_64_set_tls_module_base (info);
9f03412a 4115
60f2e42e 4116 rel = wrel = relocs;
8d88c4ca 4117 relend = relocs + input_section->reloc_count;
60f2e42e 4118 for (; rel < relend; wrel++, rel++)
8d88c4ca 4119 {
bffbf940 4120 unsigned int r_type;
8d88c4ca
NC
4121 reloc_howto_type *howto;
4122 unsigned long r_symndx;
4123 struct elf_link_hash_entry *h;
0ff2b86e 4124 struct elf_x86_64_link_hash_entry *eh;
8d88c4ca
NC
4125 Elf_Internal_Sym *sym;
4126 asection *sec;
0ff2b86e 4127 bfd_vma off, offplt, plt_offset;
8d88c4ca 4128 bfd_vma relocation;
b34976b6 4129 bfd_boolean unresolved_reloc;
8d88c4ca 4130 bfd_reloc_status_type r;
bffbf940 4131 int tls_type;
0ff2b86e 4132 asection *base_got, *resolved_plt;
1788fc08 4133 bfd_vma st_size;
aec6b87e 4134 bfd_boolean resolved_to_zero;
8d88c4ca 4135
351f65ca 4136 r_type = ELF32_R_TYPE (rel->r_info);
fe4770f4
AJ
4137 if (r_type == (int) R_X86_64_GNU_VTINHERIT
4138 || r_type == (int) R_X86_64_GNU_VTENTRY)
18954b29
L
4139 {
4140 if (wrel != rel)
4141 *wrel = *rel;
4142 continue;
4143 }
8d88c4ca 4144
9911c0fc 4145 if (r_type >= (int) R_X86_64_standard)
8da6118f 4146 {
9911c0fc
L
4147 (*_bfd_error_handler)
4148 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
4149 input_bfd, input_section, r_type);
8da6118f 4150 bfd_set_error (bfd_error_bad_value);
b34976b6 4151 return FALSE;
8da6118f 4152 }
8d88c4ca 4153
d7921315 4154 if (r_type != (int) R_X86_64_32
eed180f8 4155 || ABI_64_P (output_bfd))
d7921315
L
4156 howto = x86_64_elf_howto_table + r_type;
4157 else
4158 howto = (x86_64_elf_howto_table
4159 + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
351f65ca 4160 r_symndx = htab->r_sym (rel->r_info);
8d88c4ca
NC
4161 h = NULL;
4162 sym = NULL;
4163 sec = NULL;
b34976b6 4164 unresolved_reloc = FALSE;
8d88c4ca 4165 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
4166 {
4167 sym = local_syms + r_symndx;
4168 sec = local_sections[r_symndx];
c434dee6 4169
c25bc9fc
L
4170 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
4171 &sec, rel);
1788fc08 4172 st_size = sym->st_size;
c25bc9fc
L
4173
4174 /* Relocate against local STT_GNU_IFUNC symbol. */
0e1862bb 4175 if (!bfd_link_relocatable (info)
351f65ca 4176 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc 4177 {
351f65ca
L
4178 h = elf_x86_64_get_local_sym_hash (htab, input_bfd,
4179 rel, FALSE);
c25bc9fc
L
4180 if (h == NULL)
4181 abort ();
4182
eed180f8 4183 /* Set STT_GNU_IFUNC symbol value. */
c25bc9fc
L
4184 h->root.u.def.value = sym->st_value;
4185 h->root.u.def.section = sec;
4186 }
8da6118f 4187 }
8d88c4ca 4188 else
8da6118f 4189 {
c9736ba0 4190 bfd_boolean warned ATTRIBUTE_UNUSED;
62d887d4 4191 bfd_boolean ignored ATTRIBUTE_UNUSED;
c434dee6 4192
b2a8e766
AM
4193 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4194 r_symndx, symtab_hdr, sym_hashes,
4195 h, sec, relocation,
62d887d4 4196 unresolved_reloc, warned, ignored);
1788fc08 4197 st_size = h->size;
8da6118f 4198 }
ab96bf03 4199
dbaa2011 4200 if (sec != NULL && discarded_section (sec))
60f2e42e
L
4201 {
4202 _bfd_clear_contents (howto, input_bfd, input_section,
4203 contents + rel->r_offset);
4204 wrel->r_offset = rel->r_offset;
4205 wrel->r_info = 0;
4206 wrel->r_addend = 0;
4207
4208 /* For ld -r, remove relocations in debug sections against
4209 sections defined in discarded sections. Not done for
4210 eh_frame editing code expects to be present. */
4211 if (bfd_link_relocatable (info)
4212 && (input_section->flags & SEC_DEBUGGING))
4213 wrel--;
4214
4215 continue;
4216 }
ab96bf03 4217
0e1862bb 4218 if (bfd_link_relocatable (info))
2d5da473
L
4219 {
4220 if (wrel != rel)
4221 *wrel = *rel;
4222 continue;
4223 }
ab96bf03 4224
1788fc08 4225 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
64d25c44 4226 {
1788fc08
L
4227 if (r_type == R_X86_64_64)
4228 {
4229 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
4230 zero-extend it to 64bit if addend is zero. */
4231 r_type = R_X86_64_32;
4232 memset (contents + rel->r_offset + 4, 0, 4);
4233 }
4234 else if (r_type == R_X86_64_SIZE64)
4235 {
4236 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
4237 zero-extend it to 64bit if addend is zero. */
4238 r_type = R_X86_64_SIZE32;
4239 memset (contents + rel->r_offset + 4, 0, 4);
4240 }
64d25c44
L
4241 }
4242
0ff2b86e
L
4243 eh = (struct elf_x86_64_link_hash_entry *) h;
4244
cbe950e9
L
4245 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
4246 it here if it is defined in a non-shared object. */
4247 if (h != NULL
4248 && h->type == STT_GNU_IFUNC
4249 && h->def_regular)
4250 {
cbe950e9 4251 bfd_vma plt_index;
4c544807 4252 const char *name;
cbe950e9 4253
97dc35c8
L
4254 if ((input_section->flags & SEC_ALLOC) == 0)
4255 {
4256 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4257 sections because such sections are not SEC_ALLOC and
4258 thus ld.so will not process them. */
4259 if ((input_section->flags & SEC_DEBUGGING) != 0)
0eace210 4260 continue;
97dc35c8
L
4261 abort ();
4262 }
233cc9c1
L
4263
4264 switch (r_type)
4265 {
4266 default:
4267 break;
4268
4269 case R_X86_64_GOTPCREL:
4270 case R_X86_64_GOTPCRELX:
4271 case R_X86_64_REX_GOTPCRELX:
4272 case R_X86_64_GOTPCREL64:
4273 base_got = htab->elf.sgot;
4274 off = h->got.offset;
4275
4276 if (base_got == NULL)
4277 abort ();
4278
4279 if (off == (bfd_vma) -1)
4280 {
4281 /* We can't use h->got.offset here to save state, or
4282 even just remember the offset, as finish_dynamic_symbol
4283 would use that as offset into .got. */
4284
4285 if (h->plt.offset == (bfd_vma) -1)
4286 abort ();
4287
4288 if (htab->elf.splt != NULL)
4289 {
4290 plt_index = h->plt.offset / plt_entry_size - 1;
4291 off = (plt_index + 3) * GOT_ENTRY_SIZE;
4292 base_got = htab->elf.sgotplt;
4293 }
4294 else
4295 {
4296 plt_index = h->plt.offset / plt_entry_size;
4297 off = plt_index * GOT_ENTRY_SIZE;
4298 base_got = htab->elf.igotplt;
4299 }
4300
4301 if (h->dynindx == -1
4302 || h->forced_local
4303 || info->symbolic)
4304 {
4305 /* This references the local defitionion. We must
4306 initialize this entry in the global offset table.
4307 Since the offset must always be a multiple of 8,
4308 we use the least significant bit to record
4309 whether we have initialized it already.
4310
4311 When doing a dynamic link, we create a .rela.got
4312 relocation entry to initialize the value. This
4313 is done in the finish_dynamic_symbol routine. */
4314 if ((off & 1) != 0)
4315 off &= ~1;
4316 else
4317 {
4318 bfd_put_64 (output_bfd, relocation,
4319 base_got->contents + off);
4320 /* Note that this is harmless for the GOTPLT64
4321 case, as -1 | 1 still is -1. */
4322 h->got.offset |= 1;
4323 }
4324 }
4325 }
4326
4327 relocation = (base_got->output_section->vma
4328 + base_got->output_offset + off);
4329
4330 goto do_relocation;
4331 }
4332
4333 if (h->plt.offset == (bfd_vma) -1)
4334 {
4335 /* Handle static pointers of STT_GNU_IFUNC symbols. */
4336 if (r_type == htab->pointer_r_type
4337 && (input_section->flags & SEC_CODE) == 0)
4338 goto do_ifunc_pointer;
4339 goto bad_ifunc_reloc;
4340 }
cbe950e9
L
4341
4342 /* STT_GNU_IFUNC symbol must go through PLT. */
0ff2b86e
L
4343 if (htab->elf.splt != NULL)
4344 {
4345 if (htab->plt_bnd != NULL)
4346 {
4347 resolved_plt = htab->plt_bnd;
4348 plt_offset = eh->plt_bnd.offset;
4349 }
4350 else
4351 {
4352 resolved_plt = htab->elf.splt;
4353 plt_offset = h->plt.offset;
4354 }
4355 }
4356 else
4357 {
4358 resolved_plt = htab->elf.iplt;
4359 plt_offset = h->plt.offset;
4360 }
4361
4362 relocation = (resolved_plt->output_section->vma
4363 + resolved_plt->output_offset + plt_offset);
cbe950e9
L
4364
4365 switch (r_type)
4366 {
4367 default:
233cc9c1 4368bad_ifunc_reloc:
4c544807
L
4369 if (h->root.root.string)
4370 name = h->root.root.string;
4371 else
4372 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4373 NULL);
cbe950e9
L
4374 (*_bfd_error_handler)
4375 (_("%B: relocation %s against STT_GNU_IFUNC "
233cc9c1
L
4376 "symbol `%s' isn't supported"), input_bfd,
4377 howto->name, name);
cbe950e9
L
4378 bfd_set_error (bfd_error_bad_value);
4379 return FALSE;
4380
4381 case R_X86_64_32S:
0e1862bb 4382 if (bfd_link_pic (info))
cbe950e9 4383 abort ();
710ab287
L
4384 goto do_relocation;
4385
248775ba
L
4386 case R_X86_64_32:
4387 if (ABI_64_P (output_bfd))
4388 goto do_relocation;
17672001 4389 /* FALLTHROUGH */
eed180f8 4390 case R_X86_64_64:
233cc9c1 4391do_ifunc_pointer:
710ab287
L
4392 if (rel->r_addend != 0)
4393 {
4c544807
L
4394 if (h->root.root.string)
4395 name = h->root.root.string;
4396 else
4397 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4398 sym, NULL);
710ab287
L
4399 (*_bfd_error_handler)
4400 (_("%B: relocation %s against STT_GNU_IFUNC "
4401 "symbol `%s' has non-zero addend: %d"),
ef77d6af 4402 input_bfd, howto->name, name, rel->r_addend);
710ab287
L
4403 bfd_set_error (bfd_error_bad_value);
4404 return FALSE;
4405 }
4406
4407 /* Generate dynamic relcoation only when there is a
233cc9c1
L
4408 non-GOT reference in a shared object or there is no
4409 PLT. */
4410 if ((bfd_link_pic (info) && h->non_got_ref)
4411 || h->plt.offset == (bfd_vma) -1)
710ab287
L
4412 {
4413 Elf_Internal_Rela outrel;
710ab287
L
4414 asection *sreloc;
4415
c25bc9fc
L
4416 /* Need a dynamic relocation to get the real function
4417 address. */
710ab287
L
4418 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4419 info,
4420 input_section,
4421 rel->r_offset);
4422 if (outrel.r_offset == (bfd_vma) -1
4423 || outrel.r_offset == (bfd_vma) -2)
4424 abort ();
4425
4426 outrel.r_offset += (input_section->output_section->vma
4427 + input_section->output_offset);
4428
4429 if (h->dynindx == -1
44c4ea11 4430 || h->forced_local
0e1862bb 4431 || bfd_link_executable (info))
710ab287
L
4432 {
4433 /* This symbol is resolved locally. */
56b8aada
L
4434 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4435 outrel.r_addend = (h->root.u.def.value
4436 + h->root.u.def.section->output_section->vma
4437 + h->root.u.def.section->output_offset);
710ab287
L
4438 }
4439 else
4440 {
351f65ca 4441 outrel.r_info = htab->r_info (h->dynindx, r_type);
710ab287
L
4442 outrel.r_addend = 0;
4443 }
4444
233cc9c1
L
4445 /* Dynamic relocations are stored in
4446 1. .rela.ifunc section in PIC object.
4447 2. .rela.got section in dynamic executable.
4448 3. .rela.iplt section in static executable. */
4449 if (bfd_link_pic (info))
4450 sreloc = htab->elf.irelifunc;
4451 else if (htab->elf.splt != NULL)
4452 sreloc = htab->elf.srelgot;
4453 else
4454 sreloc = htab->elf.irelplt;
351f65ca 4455 elf_append_rela (output_bfd, sreloc, &outrel);
710ab287
L
4456
4457 /* If this reloc is against an external symbol, we
4458 do not want to fiddle with the addend. Otherwise,
4459 we need to include the symbol value so that it
4460 becomes an addend for the dynamic reloc. For an
4461 internal symbol, we have updated addend. */
56b8aada 4462 continue;
710ab287 4463 }
17672001 4464 /* FALLTHROUGH */
cbe950e9 4465 case R_X86_64_PC32:
c3320543 4466 case R_X86_64_PC32_BND:
cbe950e9
L
4467 case R_X86_64_PC64:
4468 case R_X86_64_PLT32:
c3320543 4469 case R_X86_64_PLT32_BND:
cbe950e9 4470 goto do_relocation;
cbe950e9
L
4471 }
4472 }
4473
aec6b87e 4474 resolved_to_zero = (eh != NULL
e62b9723
L
4475 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
4476 eh->has_got_reloc,
4477 eh));
aec6b87e 4478
70256ad8
AJ
4479 /* When generating a shared object, the relocations handled here are
4480 copied into the output file to be resolved at run time. */
4481 switch (r_type)
4482 {
4483 case R_X86_64_GOT32:
7b81dfbb 4484 case R_X86_64_GOT64:
70256ad8
AJ
4485 /* Relocation is to the entry for this symbol in the global
4486 offset table. */
70256ad8 4487 case R_X86_64_GOTPCREL:
56ceb5b5
L
4488 case R_X86_64_GOTPCRELX:
4489 case R_X86_64_REX_GOTPCRELX:
7b81dfbb
AJ
4490 case R_X86_64_GOTPCREL64:
4491 /* Use global offset table entry as symbol value. */
4492 case R_X86_64_GOTPLT64:
553d1284 4493 /* This is obsolete and treated the the same as GOT64. */
6de2ae4a 4494 base_got = htab->elf.sgot;
7b81dfbb 4495
6de2ae4a 4496 if (htab->elf.sgot == NULL)
c434dee6 4497 abort ();
053579d7 4498
51e0a107 4499 if (h != NULL)
70256ad8 4500 {
b34976b6 4501 bfd_boolean dyn;
c434dee6
AJ
4502
4503 off = h->got.offset;
7b81dfbb 4504 if (h->needs_plt
eed180f8 4505 && h->plt.offset != (bfd_vma)-1
7b81dfbb
AJ
4506 && off == (bfd_vma)-1)
4507 {
4508 /* We can't use h->got.offset here to save
4509 state, or even just remember the offset, as
4510 finish_dynamic_symbol would use that as offset into
4511 .got. */
eed180f8 4512 bfd_vma plt_index = h->plt.offset / plt_entry_size - 1;
7b81dfbb 4513 off = (plt_index + 3) * GOT_ENTRY_SIZE;
6de2ae4a 4514 base_got = htab->elf.sgotplt;
7b81dfbb
AJ
4515 }
4516
c434dee6 4517 dyn = htab->elf.dynamic_sections_created;
51e0a107 4518
0e1862bb
L
4519 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
4520 || (bfd_link_pic (info)
27482721 4521 && SYMBOL_REFERENCES_LOCAL (info, h))
4bc6e03a
AJ
4522 || (ELF_ST_VISIBILITY (h->other)
4523 && h->root.type == bfd_link_hash_undefweak))
51e0a107
JH
4524 {
4525 /* This is actually a static link, or it is a -Bsymbolic
4526 link and the symbol is defined locally, or the symbol
407443a3 4527 was forced to be local because of a version file. We
51e0a107
JH
4528 must initialize this entry in the global offset table.
4529 Since the offset must always be a multiple of 8, we
4530 use the least significant bit to record whether we
4531 have initialized it already.
4532
4533 When doing a dynamic link, we create a .rela.got
407443a3
AJ
4534 relocation entry to initialize the value. This is
4535 done in the finish_dynamic_symbol routine. */
51e0a107
JH
4536 if ((off & 1) != 0)
4537 off &= ~1;
4538 else
4539 {
4540 bfd_put_64 (output_bfd, relocation,
7b81dfbb
AJ
4541 base_got->contents + off);
4542 /* Note that this is harmless for the GOTPLT64 case,
eed180f8 4543 as -1 | 1 still is -1. */
51e0a107
JH
4544 h->got.offset |= 1;
4545 }
4546 }
053579d7 4547 else
b34976b6 4548 unresolved_reloc = FALSE;
70256ad8 4549 }
51e0a107
JH
4550 else
4551 {
c434dee6
AJ
4552 if (local_got_offsets == NULL)
4553 abort ();
51e0a107
JH
4554
4555 off = local_got_offsets[r_symndx];
4556
4557 /* The offset must always be a multiple of 8. We use
407443a3
AJ
4558 the least significant bit to record whether we have
4559 already generated the necessary reloc. */
51e0a107
JH
4560 if ((off & 1) != 0)
4561 off &= ~1;
4562 else
4563 {
c434dee6 4564 bfd_put_64 (output_bfd, relocation,
7b81dfbb 4565 base_got->contents + off);
51e0a107 4566
0e1862bb 4567 if (bfd_link_pic (info))
51e0a107 4568 {
947216bf 4569 asection *s;
51e0a107 4570 Elf_Internal_Rela outrel;
70256ad8 4571
51e0a107
JH
4572 /* We need to generate a R_X86_64_RELATIVE reloc
4573 for the dynamic linker. */
6de2ae4a 4574 s = htab->elf.srelgot;
947216bf 4575 if (s == NULL)
c434dee6 4576 abort ();
51e0a107 4577
7b81dfbb
AJ
4578 outrel.r_offset = (base_got->output_section->vma
4579 + base_got->output_offset
51e0a107 4580 + off);
351f65ca 4581 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
51e0a107 4582 outrel.r_addend = relocation;
351f65ca 4583 elf_append_rela (output_bfd, s, &outrel);
51e0a107
JH
4584 }
4585
4586 local_got_offsets[r_symndx] |= 1;
4587 }
51e0a107 4588 }
6a2bda3f 4589
c434dee6
AJ
4590 if (off >= (bfd_vma) -2)
4591 abort ();
4592
7b81dfbb
AJ
4593 relocation = base_got->output_section->vma
4594 + base_got->output_offset + off;
56ceb5b5
L
4595 if (r_type != R_X86_64_GOTPCREL
4596 && r_type != R_X86_64_GOTPCRELX
4597 && r_type != R_X86_64_REX_GOTPCRELX
4598 && r_type != R_X86_64_GOTPCREL64)
6de2ae4a
L
4599 relocation -= htab->elf.sgotplt->output_section->vma
4600 - htab->elf.sgotplt->output_offset;
c434dee6 4601
70256ad8
AJ
4602 break;
4603
d6ab8113
JB
4604 case R_X86_64_GOTOFF64:
4605 /* Relocation is relative to the start of the global offset
4606 table. */
4607
3d949995
L
4608 /* Check to make sure it isn't a protected function or data
4609 symbol for shared library since it may not be local when
e3c0e327
L
4610 used as function address or with copy relocation. We also
4611 need to make sure that a symbol is referenced locally. */
0e1862bb 4612 if (bfd_link_pic (info) && h)
d6ab8113 4613 {
e3c0e327
L
4614 if (!h->def_regular)
4615 {
4616 const char *v;
4617
4618 switch (ELF_ST_VISIBILITY (h->other))
4619 {
4620 case STV_HIDDEN:
4621 v = _("hidden symbol");
4622 break;
4623 case STV_INTERNAL:
4624 v = _("internal symbol");
4625 break;
4626 case STV_PROTECTED:
4627 v = _("protected symbol");
4628 break;
4629 default:
4630 v = _("symbol");
4631 break;
4632 }
4633
4634 (*_bfd_error_handler)
4635 (_("%B: relocation R_X86_64_GOTOFF64 against undefined %s `%s' can not be used when making a shared object"),
4636 input_bfd, v, h->root.root.string);
4637 bfd_set_error (bfd_error_bad_value);
4638 return FALSE;
4639 }
0e1862bb 4640 else if (!bfd_link_executable (info)
e3c0e327
L
4641 && !SYMBOL_REFERENCES_LOCAL (info, h)
4642 && (h->type == STT_FUNC
4643 || h->type == STT_OBJECT)
4644 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
4645 {
4646 (*_bfd_error_handler)
4647 (_("%B: relocation R_X86_64_GOTOFF64 against protected %s `%s' can not be used when making a shared object"),
4648 input_bfd,
4649 h->type == STT_FUNC ? "function" : "data",
4650 h->root.root.string);
4651 bfd_set_error (bfd_error_bad_value);
d6ab8113 4652 return FALSE;
e3c0e327 4653 }
d6ab8113
JB
4654 }
4655
4656 /* Note that sgot is not involved in this
4657 calculation. We always want the start of .got.plt. If we
4658 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
4659 permitted by the ABI, we might have to change this
4660 calculation. */
6de2ae4a
L
4661 relocation -= htab->elf.sgotplt->output_section->vma
4662 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
4663 break;
4664
4665 case R_X86_64_GOTPC32:
7b81dfbb 4666 case R_X86_64_GOTPC64:
d6ab8113 4667 /* Use global offset table as symbol value. */
6de2ae4a
L
4668 relocation = htab->elf.sgotplt->output_section->vma
4669 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
4670 unresolved_reloc = FALSE;
4671 break;
7b81dfbb
AJ
4672
4673 case R_X86_64_PLTOFF64:
4674 /* Relocation is PLT entry relative to GOT. For local
4675 symbols it's the symbol itself relative to GOT. */
eed180f8 4676 if (h != NULL
7b81dfbb
AJ
4677 /* See PLT32 handling. */
4678 && h->plt.offset != (bfd_vma) -1
6de2ae4a 4679 && htab->elf.splt != NULL)
7b81dfbb 4680 {
0ff2b86e
L
4681 if (htab->plt_bnd != NULL)
4682 {
4683 resolved_plt = htab->plt_bnd;
4684 plt_offset = eh->plt_bnd.offset;
4685 }
4686 else
4687 {
4688 resolved_plt = htab->elf.splt;
4689 plt_offset = h->plt.offset;
4690 }
4691
4692 relocation = (resolved_plt->output_section->vma
4693 + resolved_plt->output_offset
4694 + plt_offset);
7b81dfbb
AJ
4695 unresolved_reloc = FALSE;
4696 }
4697
6de2ae4a
L
4698 relocation -= htab->elf.sgotplt->output_section->vma
4699 + htab->elf.sgotplt->output_offset;
7b81dfbb 4700 break;
d6ab8113 4701
70256ad8 4702 case R_X86_64_PLT32:
c3320543 4703 case R_X86_64_PLT32_BND:
70256ad8
AJ
4704 /* Relocation is to the entry for this symbol in the
4705 procedure linkage table. */
4706
4707 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 4708 without using the procedure linkage table. */
70256ad8
AJ
4709 if (h == NULL)
4710 break;
4711
dd7e64d4
L
4712 if ((h->plt.offset == (bfd_vma) -1
4713 && eh->plt_got.offset == (bfd_vma) -1)
6de2ae4a 4714 || htab->elf.splt == NULL)
70256ad8
AJ
4715 {
4716 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
4717 happens when statically linking PIC code, or when
4718 using -Bsymbolic. */
70256ad8
AJ
4719 break;
4720 }
4721
dd7e64d4 4722 if (h->plt.offset != (bfd_vma) -1)
0ff2b86e 4723 {
dd7e64d4
L
4724 if (htab->plt_bnd != NULL)
4725 {
4726 resolved_plt = htab->plt_bnd;
4727 plt_offset = eh->plt_bnd.offset;
4728 }
4729 else
4730 {
4731 resolved_plt = htab->elf.splt;
4732 plt_offset = h->plt.offset;
4733 }
0ff2b86e
L
4734 }
4735 else
4736 {
dd7e64d4
L
4737 /* Use the GOT PLT. */
4738 resolved_plt = htab->plt_got;
4739 plt_offset = eh->plt_got.offset;
0ff2b86e
L
4740 }
4741
4742 relocation = (resolved_plt->output_section->vma
4743 + resolved_plt->output_offset
4744 + plt_offset);
b34976b6 4745 unresolved_reloc = FALSE;
70256ad8
AJ
4746 break;
4747
1788fc08
L
4748 case R_X86_64_SIZE32:
4749 case R_X86_64_SIZE64:
1788fc08
L
4750 /* Set to symbol size. */
4751 relocation = st_size;
4752 goto direct;
4753
fd8ab9e5
AJ
4754 case R_X86_64_PC8:
4755 case R_X86_64_PC16:
4756 case R_X86_64_PC32:
c3320543 4757 case R_X86_64_PC32_BND:
6333bc0d 4758 /* Don't complain about -fPIC if the symbol is undefined when
aec6b87e
L
4759 building executable unless it is unresolved weak symbol. */
4760 if ((input_section->flags & SEC_ALLOC) != 0
90f487df 4761 && (input_section->flags & SEC_READONLY) != 0
6333bc0d 4762 && h != NULL
aec6b87e
L
4763 && ((bfd_link_executable (info)
4764 && h->root.type == bfd_link_hash_undefweak
4765 && !resolved_to_zero)
4766 || (bfd_link_pic (info)
4767 && !(bfd_link_pie (info)
4768 && h->root.type == bfd_link_hash_undefined))))
6610a52d 4769 {
41bed6dd
L
4770 bfd_boolean fail = FALSE;
4771 bfd_boolean branch
c3320543
L
4772 = ((r_type == R_X86_64_PC32
4773 || r_type == R_X86_64_PC32_BND)
41bed6dd
L
4774 && is_32bit_relative_branch (contents, rel->r_offset));
4775
4776 if (SYMBOL_REFERENCES_LOCAL (info, h))
4777 {
4778 /* Symbol is referenced locally. Make sure it is
4779 defined locally or for a branch. */
4780 fail = !h->def_regular && !branch;
4781 }
aec6b87e 4782 else if (!(bfd_link_pie (info)
bc696fd5 4783 && (h->needs_copy || eh->needs_copy)))
41bed6dd 4784 {
9a926d55
L
4785 /* Symbol doesn't need copy reloc and isn't referenced
4786 locally. We only allow branch to symbol with
4787 non-default visibility. */
41bed6dd
L
4788 fail = (!branch
4789 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
4790 }
4791
4792 if (fail)
338c190a
L
4793 return elf_x86_64_need_pic (input_bfd, input_section,
4794 h, NULL, NULL, howto);
6610a52d
L
4795 }
4796 /* Fall through. */
4797
70256ad8
AJ
4798 case R_X86_64_8:
4799 case R_X86_64_16:
4800 case R_X86_64_32:
d6ab8113 4801 case R_X86_64_PC64:
6b3db546 4802 case R_X86_64_64:
80643fbc 4803 /* FIXME: The ABI says the linker should make sure the value is
407443a3 4804 the same when it's zeroextended to 64 bit. */
c434dee6 4805
1788fc08 4806direct:
b1e24c02 4807 if ((input_section->flags & SEC_ALLOC) == 0)
c434dee6
AJ
4808 break;
4809
9a926d55 4810 /* Don't copy a pc-relative relocation into the output file
6333bc0d 4811 if the symbol needs copy reloc or the symbol is undefined
04ebc307 4812 when building executable. Copy dynamic function pointer
aec6b87e
L
4813 relocations. Don't generate dynamic relocations against
4814 resolved undefined weak symbols in PIE. */
0e1862bb 4815 if ((bfd_link_pic (info)
aec6b87e 4816 && !(bfd_link_pie (info)
fd9edc90 4817 && h != NULL
6333bc0d
L
4818 && (h->needs_copy
4819 || eh->needs_copy
4820 || h->root.type == bfd_link_hash_undefined)
d8457a04
L
4821 && (IS_X86_64_PCREL_TYPE (r_type)
4822 || r_type == R_X86_64_SIZE32
4823 || r_type == R_X86_64_SIZE64))
4bc6e03a 4824 && (h == NULL
aec6b87e
L
4825 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4826 && !resolved_to_zero)
4827 || h->root.type != bfd_link_hash_undefweak))
1788fc08
L
4828 && ((! IS_X86_64_PCREL_TYPE (r_type)
4829 && r_type != R_X86_64_SIZE32
4830 && r_type != R_X86_64_SIZE64)
d8045f23 4831 || ! SYMBOL_CALLS_LOCAL (info, h)))
d40d037c 4832 || (ELIMINATE_COPY_RELOCS
0e1862bb 4833 && !bfd_link_pic (info)
c434dee6
AJ
4834 && h != NULL
4835 && h->dynindx != -1
aec6b87e
L
4836 && (!h->non_got_ref
4837 || eh->func_pointer_refcount > 0
4838 || (h->root.type == bfd_link_hash_undefweak
4839 && !resolved_to_zero))
bae420ef
L
4840 && ((h->def_dynamic && !h->def_regular)
4841 /* Undefined weak symbol is bound locally when
4842 PIC is false. */
0f88be7a 4843 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
4844 {
4845 Elf_Internal_Rela outrel;
b34976b6 4846 bfd_boolean skip, relocate;
c434dee6 4847 asection *sreloc;
70256ad8
AJ
4848
4849 /* When generating a shared object, these relocations
4850 are copied into the output file to be resolved at run
407443a3 4851 time. */
b34976b6
AM
4852 skip = FALSE;
4853 relocate = FALSE;
70256ad8 4854
c629eae0
JJ
4855 outrel.r_offset =
4856 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 4857 rel->r_offset);
c629eae0 4858 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 4859 skip = TRUE;
0fb19cbc 4860 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 4861 skip = TRUE, relocate = TRUE;
70256ad8
AJ
4862
4863 outrel.r_offset += (input_section->output_section->vma
4864 + input_section->output_offset);
4865
4866 if (skip)
0bb2d96a 4867 memset (&outrel, 0, sizeof outrel);
c434dee6 4868
fd8ab9e5
AJ
4869 /* h->dynindx may be -1 if this symbol was marked to
4870 become local. */
4871 else if (h != NULL
c434dee6 4872 && h->dynindx != -1
d8045f23 4873 && (IS_X86_64_PCREL_TYPE (r_type)
4e0c91e4
L
4874 || !(bfd_link_executable (info)
4875 || SYMBOLIC_BIND (info, h))
d8045f23 4876 || ! h->def_regular))
70256ad8 4877 {
351f65ca 4878 outrel.r_info = htab->r_info (h->dynindx, r_type);
c434dee6 4879 outrel.r_addend = rel->r_addend;
70256ad8
AJ
4880 }
4881 else
4882 {
4c10bbaa
L
4883 /* This symbol is local, or marked to become local.
4884 When relocation overflow check is disabled, we
4885 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
4886 if (r_type == htab->pointer_r_type
4887 || (r_type == R_X86_64_32
4888 && info->no_reloc_overflow_check))
607c0e09 4889 {
b34976b6 4890 relocate = TRUE;
351f65ca 4891 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
607c0e09
AS
4892 outrel.r_addend = relocation + rel->r_addend;
4893 }
64d25c44
L
4894 else if (r_type == R_X86_64_64
4895 && !ABI_64_P (output_bfd))
4896 {
4897 relocate = TRUE;
4898 outrel.r_info = htab->r_info (0,
4899 R_X86_64_RELATIVE64);
4900 outrel.r_addend = relocation + rel->r_addend;
8cf0d2dd
L
4901 /* Check addend overflow. */
4902 if ((outrel.r_addend & 0x80000000)
4903 != (rel->r_addend & 0x80000000))
4904 {
4905 const char *name;
268a8d3a 4906 int addend = rel->r_addend;
8cf0d2dd
L
4907 if (h && h->root.root.string)
4908 name = h->root.root.string;
4909 else
4910 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4911 sym, NULL);
6f2c9068
L
4912 if (addend < 0)
4913 (*_bfd_error_handler)
268a8d3a 4914 (_("%B: addend -0x%x in relocation %s against "
6f2c9068
L
4915 "symbol `%s' at 0x%lx in section `%A' is "
4916 "out of range"),
4917 input_bfd, input_section, addend,
ef77d6af
L
4918 howto->name, name,
4919 (unsigned long) rel->r_offset);
6f2c9068
L
4920 else
4921 (*_bfd_error_handler)
268a8d3a 4922 (_("%B: addend 0x%x in relocation %s against "
6f2c9068
L
4923 "symbol `%s' at 0x%lx in section `%A' is "
4924 "out of range"),
4925 input_bfd, input_section, addend,
ef77d6af
L
4926 howto->name, name,
4927 (unsigned long) rel->r_offset);
8cf0d2dd
L
4928 bfd_set_error (bfd_error_bad_value);
4929 return FALSE;
4930 }
64d25c44 4931 }
607c0e09
AS
4932 else
4933 {
4934 long sindx;
4935
8517fae7 4936 if (bfd_is_abs_section (sec))
607c0e09
AS
4937 sindx = 0;
4938 else if (sec == NULL || sec->owner == NULL)
4939 {
4940 bfd_set_error (bfd_error_bad_value);
b34976b6 4941 return FALSE;
607c0e09
AS
4942 }
4943 else
4944 {
4945 asection *osec;
4946
74541ad4
AM
4947 /* We are turning this relocation into one
4948 against a section symbol. It would be
4949 proper to subtract the symbol's value,
4950 osec->vma, from the emitted reloc addend,
4951 but ld.so expects buggy relocs. */
607c0e09
AS
4952 osec = sec->output_section;
4953 sindx = elf_section_data (osec)->dynindx;
74541ad4
AM
4954 if (sindx == 0)
4955 {
4956 asection *oi = htab->elf.text_index_section;
4957 sindx = elf_section_data (oi)->dynindx;
4958 }
4959 BFD_ASSERT (sindx != 0);
607c0e09
AS
4960 }
4961
351f65ca 4962 outrel.r_info = htab->r_info (sindx, r_type);
607c0e09
AS
4963 outrel.r_addend = relocation + rel->r_addend;
4964 }
70256ad8
AJ
4965 }
4966
cbe950e9 4967 sreloc = elf_section_data (input_section)->sreloc;
d8045f23 4968
62d78908
L
4969 if (sreloc == NULL || sreloc->contents == NULL)
4970 {
4971 r = bfd_reloc_notsupported;
4972 goto check_relocation_error;
4973 }
c434dee6 4974
351f65ca 4975 elf_append_rela (output_bfd, sreloc, &outrel);
70256ad8
AJ
4976
4977 /* If this reloc is against an external symbol, we do
4978 not want to fiddle with the addend. Otherwise, we
4979 need to include the symbol value so that it becomes
4980 an addend for the dynamic reloc. */
0f88be7a 4981 if (! relocate)
70256ad8
AJ
4982 continue;
4983 }
4984
4985 break;
4986
bffbf940 4987 case R_X86_64_TLSGD:
67a4f2b7
AO
4988 case R_X86_64_GOTPC32_TLSDESC:
4989 case R_X86_64_TLSDESC_CALL:
bffbf940 4990 case R_X86_64_GOTTPOFF:
bffbf940
JJ
4991 tls_type = GOT_UNKNOWN;
4992 if (h == NULL && local_got_offsets)
351f65ca 4993 tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
bffbf940 4994 else if (h != NULL)
351f65ca 4995 tls_type = elf_x86_64_hash_entry (h)->tls_type;
142411ca 4996
351f65ca
L
4997 if (! elf_x86_64_tls_transition (info, input_bfd,
4998 input_section, contents,
4999 symtab_hdr, sym_hashes,
5000 &r_type, tls_type, rel,
bedfd056 5001 relend, h, r_symndx, TRUE))
534a31f6 5002 return FALSE;
bffbf940
JJ
5003
5004 if (r_type == R_X86_64_TPOFF32)
5005 {
142411ca
L
5006 bfd_vma roff = rel->r_offset;
5007
bffbf940 5008 BFD_ASSERT (! unresolved_reloc);
142411ca 5009
351f65ca 5010 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
bffbf940 5011 {
52bc799a 5012 /* GD->LE transition. For 64bit, change
e2cbcd91
L
5013 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5014 .word 0x6666; rex64; call __tls_get_addr@PLT
5015 or
5016 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5017 .byte 0x66; rex64
5018 call *__tls_get_addr@GOTPCREL(%rip)
5019 which may be converted to
5020 addr32 call __tls_get_addr
52bc799a 5021 into:
e2cbcd91
L
5022 movq %fs:0, %rax
5023 leaq foo@tpoff(%rax), %rax
52bc799a 5024 For 32bit, change
e2cbcd91
L
5025 leaq foo@tlsgd(%rip), %rdi
5026 .word 0x6666; rex64; call __tls_get_addr@PLT
5027 or
5028 leaq foo@tlsgd(%rip), %rdi
5029 .byte 0x66; rex64
5030 call *__tls_get_addr@GOTPCREL(%rip)
5031 which may be converted to
5032 addr32 call __tls_get_addr
52bc799a 5033 into:
e2cbcd91
L
5034 movl %fs:0, %eax
5035 leaq foo@tpoff(%rax), %rax
5c98a14e 5036 For largepic, change:
e2cbcd91
L
5037 leaq foo@tlsgd(%rip), %rdi
5038 movabsq $__tls_get_addr@pltoff, %rax
5039 addq %r15, %rax
5040 call *%rax
5c98a14e 5041 into:
e2cbcd91
L
5042 movq %fs:0, %rax
5043 leaq foo@tpoff(%rax), %rax
5044 nopw 0x0(%rax,%rax,1) */
5c98a14e 5045 int largepic = 0;
e2cbcd91 5046 if (ABI_64_P (output_bfd))
5c98a14e 5047 {
e2cbcd91
L
5048 if (contents[roff + 5] == 0xb8)
5049 {
5050 memcpy (contents + roff - 3,
5051 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
5052 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
5053 largepic = 1;
5054 }
5055 else
5056 memcpy (contents + roff - 4,
5057 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
5058 16);
5c98a14e 5059 }
52bc799a
L
5060 else
5061 memcpy (contents + roff - 3,
5062 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
5063 15);
eb4ff4d6 5064 bfd_put_32 (output_bfd,
351f65ca 5065 elf_x86_64_tpoff (info, relocation),
5c98a14e 5066 contents + roff + 8 + largepic);
e2cbcd91
L
5067 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
5068 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
bffbf940 5069 rel++;
60f2e42e 5070 wrel++;
bffbf940
JJ
5071 continue;
5072 }
351f65ca 5073 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
67a4f2b7
AO
5074 {
5075 /* GDesc -> LE transition.
5076 It's originally something like:
5077 leaq x@tlsdesc(%rip), %rax
5078
5079 Change it to:
c9736ba0 5080 movl $x@tpoff, %rax. */
67a4f2b7 5081
c9736ba0 5082 unsigned int val, type;
67a4f2b7 5083
67a4f2b7 5084 type = bfd_get_8 (input_bfd, contents + roff - 3);
67a4f2b7 5085 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
5086 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
5087 contents + roff - 3);
5088 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
5089 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
5090 contents + roff - 1);
eb4ff4d6 5091 bfd_put_32 (output_bfd,
351f65ca 5092 elf_x86_64_tpoff (info, relocation),
67a4f2b7
AO
5093 contents + roff);
5094 continue;
5095 }
351f65ca 5096 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
67a4f2b7
AO
5097 {
5098 /* GDesc -> LE transition.
5099 It's originally:
5100 call *(%rax)
5101 Turn it into:
142411ca 5102 xchg %ax,%ax. */
10efb593 5103 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
5104 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5105 continue;
5106 }
351f65ca 5107 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
bffbf940 5108 {
bffbf940 5109 /* IE->LE transition:
cf61b747 5110 For 64bit, originally it can be one of:
bffbf940
JJ
5111 movq foo@gottpoff(%rip), %reg
5112 addq foo@gottpoff(%rip), %reg
5113 We change it into:
5114 movq $foo, %reg
5115 leaq foo(%reg), %reg
cf61b747
L
5116 addq $foo, %reg.
5117 For 32bit, originally it can be one of:
5118 movq foo@gottpoff(%rip), %reg
5119 addl foo@gottpoff(%rip), %reg
5120 We change it into:
5121 movq $foo, %reg
5122 leal foo(%reg), %reg
5123 addl $foo, %reg. */
142411ca
L
5124
5125 unsigned int val, type, reg;
5126
cf61b747
L
5127 if (roff >= 3)
5128 val = bfd_get_8 (input_bfd, contents + roff - 3);
5129 else
5130 val = 0;
142411ca
L
5131 type = bfd_get_8 (input_bfd, contents + roff - 2);
5132 reg = bfd_get_8 (input_bfd, contents + roff - 1);
bffbf940 5133 reg >>= 3;
bffbf940
JJ
5134 if (type == 0x8b)
5135 {
5136 /* movq */
5137 if (val == 0x4c)
5138 bfd_put_8 (output_bfd, 0x49,
142411ca 5139 contents + roff - 3);
4a4c5f25
L
5140 else if (!ABI_64_P (output_bfd) && val == 0x44)
5141 bfd_put_8 (output_bfd, 0x41,
5142 contents + roff - 3);
bffbf940 5143 bfd_put_8 (output_bfd, 0xc7,
142411ca 5144 contents + roff - 2);
bffbf940 5145 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 5146 contents + roff - 1);
bffbf940
JJ
5147 }
5148 else if (reg == 4)
5149 {
cf61b747
L
5150 /* addq/addl -> addq/addl - addressing with %rsp/%r12
5151 is special */
bffbf940
JJ
5152 if (val == 0x4c)
5153 bfd_put_8 (output_bfd, 0x49,
142411ca 5154 contents + roff - 3);
4a4c5f25
L
5155 else if (!ABI_64_P (output_bfd) && val == 0x44)
5156 bfd_put_8 (output_bfd, 0x41,
5157 contents + roff - 3);
bffbf940 5158 bfd_put_8 (output_bfd, 0x81,
142411ca 5159 contents + roff - 2);
bffbf940 5160 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 5161 contents + roff - 1);
bffbf940
JJ
5162 }
5163 else
5164 {
cf61b747 5165 /* addq/addl -> leaq/leal */
bffbf940
JJ
5166 if (val == 0x4c)
5167 bfd_put_8 (output_bfd, 0x4d,
142411ca 5168 contents + roff - 3);
4a4c5f25
L
5169 else if (!ABI_64_P (output_bfd) && val == 0x44)
5170 bfd_put_8 (output_bfd, 0x45,
5171 contents + roff - 3);
bffbf940 5172 bfd_put_8 (output_bfd, 0x8d,
142411ca 5173 contents + roff - 2);
bffbf940 5174 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
142411ca 5175 contents + roff - 1);
bffbf940 5176 }
eb4ff4d6 5177 bfd_put_32 (output_bfd,
351f65ca 5178 elf_x86_64_tpoff (info, relocation),
142411ca 5179 contents + roff);
bffbf940
JJ
5180 continue;
5181 }
142411ca
L
5182 else
5183 BFD_ASSERT (FALSE);
bffbf940
JJ
5184 }
5185
6de2ae4a 5186 if (htab->elf.sgot == NULL)
bffbf940
JJ
5187 abort ();
5188
5189 if (h != NULL)
67a4f2b7
AO
5190 {
5191 off = h->got.offset;
351f65ca 5192 offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
67a4f2b7 5193 }
bffbf940
JJ
5194 else
5195 {
5196 if (local_got_offsets == NULL)
5197 abort ();
5198
5199 off = local_got_offsets[r_symndx];
67a4f2b7 5200 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
5201 }
5202
5203 if ((off & 1) != 0)
5204 off &= ~1;
26e41594 5205 else
bffbf940
JJ
5206 {
5207 Elf_Internal_Rela outrel;
bffbf940 5208 int dr_type, indx;
67a4f2b7 5209 asection *sreloc;
bffbf940 5210
6de2ae4a 5211 if (htab->elf.srelgot == NULL)
bffbf940
JJ
5212 abort ();
5213
67a4f2b7
AO
5214 indx = h && h->dynindx != -1 ? h->dynindx : 0;
5215
5216 if (GOT_TLS_GDESC_P (tls_type))
5217 {
351f65ca 5218 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
67a4f2b7 5219 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
6de2ae4a
L
5220 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
5221 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
5222 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
5223 + offplt
5224 + htab->sgotplt_jump_table_size);
6de2ae4a 5225 sreloc = htab->elf.srelplt;
67a4f2b7 5226 if (indx == 0)
351f65ca 5227 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
67a4f2b7
AO
5228 else
5229 outrel.r_addend = 0;
351f65ca 5230 elf_append_rela (output_bfd, sreloc, &outrel);
67a4f2b7
AO
5231 }
5232
6de2ae4a 5233 sreloc = htab->elf.srelgot;
67a4f2b7 5234
6de2ae4a
L
5235 outrel.r_offset = (htab->elf.sgot->output_section->vma
5236 + htab->elf.sgot->output_offset + off);
bffbf940 5237
67a4f2b7 5238 if (GOT_TLS_GD_P (tls_type))
bffbf940 5239 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
5240 else if (GOT_TLS_GDESC_P (tls_type))
5241 goto dr_done;
bffbf940
JJ
5242 else
5243 dr_type = R_X86_64_TPOFF64;
5244
6de2ae4a 5245 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
bffbf940 5246 outrel.r_addend = 0;
67a4f2b7
AO
5247 if ((dr_type == R_X86_64_TPOFF64
5248 || dr_type == R_X86_64_TLSDESC) && indx == 0)
351f65ca
L
5249 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
5250 outrel.r_info = htab->r_info (indx, dr_type);
bffbf940 5251
351f65ca 5252 elf_append_rela (output_bfd, sreloc, &outrel);
bffbf940 5253
67a4f2b7 5254 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
5255 {
5256 if (indx == 0)
5257 {
d40d037c 5258 BFD_ASSERT (! unresolved_reloc);
bffbf940 5259 bfd_put_64 (output_bfd,
351f65ca 5260 relocation - elf_x86_64_dtpoff_base (info),
6de2ae4a 5261 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
bffbf940
JJ
5262 }
5263 else
5264 {
5265 bfd_put_64 (output_bfd, 0,
6de2ae4a 5266 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 5267 outrel.r_info = htab->r_info (indx,
bffbf940
JJ
5268 R_X86_64_DTPOFF64);
5269 outrel.r_offset += GOT_ENTRY_SIZE;
351f65ca 5270 elf_append_rela (output_bfd, sreloc,
464d3bd4 5271 &outrel);
bffbf940
JJ
5272 }
5273 }
5274
67a4f2b7 5275 dr_done:
bffbf940
JJ
5276 if (h != NULL)
5277 h->got.offset |= 1;
5278 else
5279 local_got_offsets[r_symndx] |= 1;
5280 }
5281
67a4f2b7
AO
5282 if (off >= (bfd_vma) -2
5283 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940 5284 abort ();
351f65ca 5285 if (r_type == ELF32_R_TYPE (rel->r_info))
bffbf940 5286 {
67a4f2b7
AO
5287 if (r_type == R_X86_64_GOTPC32_TLSDESC
5288 || r_type == R_X86_64_TLSDESC_CALL)
6de2ae4a
L
5289 relocation = htab->elf.sgotplt->output_section->vma
5290 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
5291 + offplt + htab->sgotplt_jump_table_size;
5292 else
6de2ae4a
L
5293 relocation = htab->elf.sgot->output_section->vma
5294 + htab->elf.sgot->output_offset + off;
b34976b6 5295 unresolved_reloc = FALSE;
bffbf940 5296 }
142411ca 5297 else
67a4f2b7 5298 {
142411ca 5299 bfd_vma roff = rel->r_offset;
67a4f2b7 5300
351f65ca 5301 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
142411ca 5302 {
52bc799a 5303 /* GD->IE transition. For 64bit, change
e2cbcd91
L
5304 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5305 .word 0x6666; rex64; call __tls_get_addr@PLT
5306 or
5307 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5308 .byte 0x66; rex64
5309 call *__tls_get_addr@GOTPCREL(%rip
5310 which may be converted to
5311 addr32 call __tls_get_addr
52bc799a 5312 into:
e2cbcd91
L
5313 movq %fs:0, %rax
5314 addq foo@gottpoff(%rip), %rax
52bc799a 5315 For 32bit, change
e2cbcd91
L
5316 leaq foo@tlsgd(%rip), %rdi
5317 .word 0x6666; rex64; call __tls_get_addr@PLT
5318 or
5319 leaq foo@tlsgd(%rip), %rdi
5320 .byte 0x66; rex64;
5321 call *__tls_get_addr@GOTPCREL(%rip)
5322 which may be converted to
5323 addr32 call __tls_get_addr
52bc799a 5324 into:
e2cbcd91
L
5325 movl %fs:0, %eax
5326 addq foo@gottpoff(%rip), %rax
5c98a14e 5327 For largepic, change:
e2cbcd91
L
5328 leaq foo@tlsgd(%rip), %rdi
5329 movabsq $__tls_get_addr@pltoff, %rax
5330 addq %r15, %rax
5331 call *%rax
5c98a14e 5332 into:
e2cbcd91
L
5333 movq %fs:0, %rax
5334 addq foo@gottpoff(%rax), %rax
5335 nopw 0x0(%rax,%rax,1) */
5c98a14e 5336 int largepic = 0;
e2cbcd91 5337 if (ABI_64_P (output_bfd))
5c98a14e 5338 {
e2cbcd91
L
5339 if (contents[roff + 5] == 0xb8)
5340 {
5341 memcpy (contents + roff - 3,
5342 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
5343 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
5344 largepic = 1;
5345 }
5346 else
5347 memcpy (contents + roff - 4,
5348 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5349 16);
5c98a14e 5350 }
52bc799a
L
5351 else
5352 memcpy (contents + roff - 3,
5353 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5354 15);
142411ca 5355
6de2ae4a
L
5356 relocation = (htab->elf.sgot->output_section->vma
5357 + htab->elf.sgot->output_offset + off
142411ca 5358 - roff
5c98a14e 5359 - largepic
142411ca
L
5360 - input_section->output_section->vma
5361 - input_section->output_offset
5362 - 12);
5363 bfd_put_32 (output_bfd, relocation,
5c98a14e
JJ
5364 contents + roff + 8 + largepic);
5365 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
142411ca 5366 rel++;
60f2e42e 5367 wrel++;
142411ca
L
5368 continue;
5369 }
351f65ca 5370 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
142411ca
L
5371 {
5372 /* GDesc -> IE transition.
5373 It's originally something like:
5374 leaq x@tlsdesc(%rip), %rax
67a4f2b7 5375
142411ca 5376 Change it to:
c9736ba0 5377 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
67a4f2b7 5378
142411ca
L
5379 /* Now modify the instruction as appropriate. To
5380 turn a leaq into a movq in the form we use it, it
5381 suffices to change the second byte from 0x8d to
5382 0x8b. */
5383 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
5384
5385 bfd_put_32 (output_bfd,
6de2ae4a
L
5386 htab->elf.sgot->output_section->vma
5387 + htab->elf.sgot->output_offset + off
142411ca
L
5388 - rel->r_offset
5389 - input_section->output_section->vma
5390 - input_section->output_offset
5391 - 4,
5392 contents + roff);
5393 continue;
5394 }
351f65ca 5395 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
142411ca
L
5396 {
5397 /* GDesc -> IE transition.
5398 It's originally:
5399 call *(%rax)
5400
5401 Change it to:
c9736ba0 5402 xchg %ax, %ax. */
142411ca 5403
142411ca
L
5404 bfd_put_8 (output_bfd, 0x66, contents + roff);
5405 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5406 continue;
5407 }
5408 else
5409 BFD_ASSERT (FALSE);
67a4f2b7 5410 }
bffbf940
JJ
5411 break;
5412
5413 case R_X86_64_TLSLD:
351f65ca
L
5414 if (! elf_x86_64_tls_transition (info, input_bfd,
5415 input_section, contents,
5416 symtab_hdr, sym_hashes,
bedfd056
L
5417 &r_type, GOT_UNKNOWN, rel,
5418 relend, h, r_symndx, TRUE))
142411ca 5419 return FALSE;
a3fadc9a 5420
142411ca
L
5421 if (r_type != R_X86_64_TLSLD)
5422 {
bffbf940 5423 /* LD->LE transition:
e2cbcd91
L
5424 leaq foo@tlsld(%rip), %rdi
5425 call __tls_get_addr@PLT
5426 For 64bit, we change it into:
5427 .word 0x6666; .byte 0x66; movq %fs:0, %rax
5428 For 32bit, we change it into:
5429 nopl 0x0(%rax); movl %fs:0, %eax
5430 Or
5431 leaq foo@tlsld(%rip), %rdi;
5432 call *__tls_get_addr@GOTPCREL(%rip)
5433 which may be converted to
5434 addr32 call __tls_get_addr
52bc799a 5435 For 64bit, we change it into:
e2cbcd91 5436 .word 0x6666; .word 0x6666; movq %fs:0, %rax
52bc799a 5437 For 32bit, we change it into:
e2cbcd91 5438 nopw 0x0(%rax); movl %fs:0, %eax
5c98a14e 5439 For largepic, change:
e2cbcd91
L
5440 leaq foo@tlsgd(%rip), %rdi
5441 movabsq $__tls_get_addr@pltoff, %rax
5442 addq %rbx, %rax
5443 call *%rax
5444 into
5445 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
5446 movq %fs:0, %eax */
142411ca
L
5447
5448 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
e2cbcd91
L
5449 if (ABI_64_P (output_bfd))
5450 {
5451 if (contents[rel->r_offset + 5] == 0xb8)
5452 memcpy (contents + rel->r_offset - 3,
5453 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
5454 "\x64\x48\x8b\x04\x25\0\0\0", 22);
5455 else if (contents[rel->r_offset + 4] == 0xff
5456 || contents[rel->r_offset + 4] == 0x67)
5457 memcpy (contents + rel->r_offset - 3,
5458 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
5459 13);
5460 else
5461 memcpy (contents + rel->r_offset - 3,
5462 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
5463 }
52bc799a 5464 else
e2cbcd91
L
5465 {
5466 if (contents[rel->r_offset + 4] == 0xff)
5467 memcpy (contents + rel->r_offset - 3,
5468 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
5469 13);
5470 else
5471 memcpy (contents + rel->r_offset - 3,
5472 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
5473 }
5474 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
5475 and R_X86_64_PLTOFF64. */
bffbf940 5476 rel++;
60f2e42e 5477 wrel++;
bffbf940
JJ
5478 continue;
5479 }
5480
6de2ae4a 5481 if (htab->elf.sgot == NULL)
bffbf940
JJ
5482 abort ();
5483
5484 off = htab->tls_ld_got.offset;
5485 if (off & 1)
5486 off &= ~1;
5487 else
5488 {
5489 Elf_Internal_Rela outrel;
bffbf940 5490
6de2ae4a 5491 if (htab->elf.srelgot == NULL)
bffbf940
JJ
5492 abort ();
5493
6de2ae4a
L
5494 outrel.r_offset = (htab->elf.sgot->output_section->vma
5495 + htab->elf.sgot->output_offset + off);
bffbf940
JJ
5496
5497 bfd_put_64 (output_bfd, 0,
6de2ae4a 5498 htab->elf.sgot->contents + off);
bffbf940 5499 bfd_put_64 (output_bfd, 0,
6de2ae4a 5500 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 5501 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
bffbf940 5502 outrel.r_addend = 0;
351f65ca 5503 elf_append_rela (output_bfd, htab->elf.srelgot,
464d3bd4 5504 &outrel);
bffbf940
JJ
5505 htab->tls_ld_got.offset |= 1;
5506 }
6de2ae4a
L
5507 relocation = htab->elf.sgot->output_section->vma
5508 + htab->elf.sgot->output_offset + off;
b34976b6 5509 unresolved_reloc = FALSE;
bffbf940
JJ
5510 break;
5511
5512 case R_X86_64_DTPOFF32:
0e1862bb
L
5513 if (!bfd_link_executable (info)
5514 || (input_section->flags & SEC_CODE) == 0)
351f65ca 5515 relocation -= elf_x86_64_dtpoff_base (info);
bffbf940 5516 else
351f65ca 5517 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
5518 break;
5519
5520 case R_X86_64_TPOFF32:
6769d501 5521 case R_X86_64_TPOFF64:
0e1862bb 5522 BFD_ASSERT (bfd_link_executable (info));
351f65ca 5523 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
5524 break;
5525
a69ed7f7
L
5526 case R_X86_64_DTPOFF64:
5527 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
5528 relocation -= elf_x86_64_dtpoff_base (info);
5529 break;
5530
70256ad8
AJ
5531 default:
5532 break;
5533 }
8d88c4ca 5534
239e1f3a
AM
5535 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5536 because such sections are not SEC_ALLOC and thus ld.so will
5537 not process them. */
c434dee6 5538 if (unresolved_reloc
239e1f3a 5539 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
5540 && h->def_dynamic)
5541 && _bfd_elf_section_offset (output_bfd, info, input_section,
5542 rel->r_offset) != (bfd_vma) -1)
a040981f
L
5543 {
5544 (*_bfd_error_handler)
5545 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5546 input_bfd,
5547 input_section,
5548 (long) rel->r_offset,
5549 howto->name,
5550 h->root.root.string);
5551 return FALSE;
5552 }
c434dee6 5553
cbe950e9 5554do_relocation:
8d88c4ca 5555 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
5556 contents, rel->r_offset,
5557 relocation, rel->r_addend);
8d88c4ca 5558
62d78908 5559check_relocation_error:
8d88c4ca 5560 if (r != bfd_reloc_ok)
8da6118f 5561 {
c434dee6
AJ
5562 const char *name;
5563
5564 if (h != NULL)
5565 name = h->root.root.string;
5566 else
8da6118f 5567 {
c434dee6
AJ
5568 name = bfd_elf_string_from_elf_section (input_bfd,
5569 symtab_hdr->sh_link,
5570 sym->st_name);
5571 if (name == NULL)
b34976b6 5572 return FALSE;
c434dee6
AJ
5573 if (*name == '\0')
5574 name = bfd_section_name (input_bfd, sec);
5575 }
5576
5577 if (r == bfd_reloc_overflow)
1a72702b
AM
5578 (*info->callbacks->reloc_overflow)
5579 (info, (h ? &h->root : NULL), name, howto->name,
5580 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
c434dee6
AJ
5581 else
5582 {
5583 (*_bfd_error_handler)
bb95161d 5584 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
d003868e 5585 input_bfd, input_section,
c434dee6 5586 (long) rel->r_offset, name, (int) r);
b34976b6 5587 return FALSE;
8da6118f
KH
5588 }
5589 }
60f2e42e
L
5590
5591 if (wrel != rel)
5592 *wrel = *rel;
5593 }
5594
5595 if (wrel != rel)
5596 {
5597 Elf_Internal_Shdr *rel_hdr;
5598 size_t deleted = rel - wrel;
5599
5600 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5601 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5602 if (rel_hdr->sh_size == 0)
5603 {
5604 /* It is too late to remove an empty reloc section. Leave
5605 one NONE reloc.
5606 ??? What is wrong with an empty section??? */
5607 rel_hdr->sh_size = rel_hdr->sh_entsize;
5608 deleted -= 1;
5609 }
5610 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5611 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5612 input_section->reloc_count -= deleted;
8d88c4ca 5613 }
70256ad8 5614
b34976b6 5615 return TRUE;
70256ad8
AJ
5616}
5617
5618/* Finish up dynamic symbol handling. We set the contents of various
5619 dynamic sections here. */
5620
b34976b6 5621static bfd_boolean
351f65ca
L
5622elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
5623 struct bfd_link_info *info,
5624 struct elf_link_hash_entry *h,
aec6b87e 5625 Elf_Internal_Sym *sym)
70256ad8 5626{
351f65ca 5627 struct elf_x86_64_link_hash_table *htab;
0ff2b86e
L
5628 const struct elf_x86_64_backend_data *abed;
5629 bfd_boolean use_plt_bnd;
dd7e64d4 5630 struct elf_x86_64_link_hash_entry *eh;
aec6b87e 5631 bfd_boolean local_undefweak;
70256ad8 5632
351f65ca 5633 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
5634 if (htab == NULL)
5635 return FALSE;
70256ad8 5636
0ff2b86e
L
5637 /* Use MPX backend data in case of BND relocation. Use .plt_bnd
5638 section only if there is .plt section. */
5639 use_plt_bnd = htab->elf.splt != NULL && htab->plt_bnd != NULL;
5640 abed = (use_plt_bnd
5641 ? &elf_x86_64_bnd_arch_bed
5642 : get_elf_x86_64_backend_data (output_bfd));
5643
dd7e64d4
L
5644 eh = (struct elf_x86_64_link_hash_entry *) h;
5645
aec6b87e
L
5646 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5647 resolved undefined weak symbols in executable so that their
5648 references have value 0 at run-time. */
e62b9723
L
5649 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
5650 eh->has_got_reloc,
5651 eh);
aec6b87e 5652
70256ad8
AJ
5653 if (h->plt.offset != (bfd_vma) -1)
5654 {
70256ad8 5655 bfd_vma plt_index;
0ff2b86e
L
5656 bfd_vma got_offset, plt_offset, plt_plt_offset, plt_got_offset;
5657 bfd_vma plt_plt_insn_end, plt_got_insn_size;
70256ad8 5658 Elf_Internal_Rela rela;
947216bf 5659 bfd_byte *loc;
0ff2b86e 5660 asection *plt, *gotplt, *relplt, *resolved_plt;
351f65ca 5661 const struct elf_backend_data *bed;
5974eba6 5662 bfd_vma plt_got_pcrel_offset;
cbe950e9
L
5663
5664 /* When building a static executable, use .iplt, .igot.plt and
5665 .rela.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 5666 if (htab->elf.splt != NULL)
cbe950e9 5667 {
6de2ae4a
L
5668 plt = htab->elf.splt;
5669 gotplt = htab->elf.sgotplt;
5670 relplt = htab->elf.srelplt;
cbe950e9
L
5671 }
5672 else
5673 {
6de2ae4a
L
5674 plt = htab->elf.iplt;
5675 gotplt = htab->elf.igotplt;
5676 relplt = htab->elf.irelplt;
cbe950e9 5677 }
70256ad8
AJ
5678
5679 /* This symbol has an entry in the procedure linkage table. Set
407443a3 5680 it up. */
cbe950e9 5681 if ((h->dynindx == -1
aec6b87e 5682 && !local_undefweak
0e1862bb 5683 && !((h->forced_local || bfd_link_executable (info))
cbe950e9
L
5684 && h->def_regular
5685 && h->type == STT_GNU_IFUNC))
5686 || plt == NULL
5687 || gotplt == NULL
5688 || relplt == NULL)
cec7f46a 5689 abort ();
70256ad8
AJ
5690
5691 /* Get the index in the procedure linkage table which
5692 corresponds to this symbol. This is the index of this symbol
5693 in all the symbols for which we are making plt entries. The
cbe950e9 5694 first entry in the procedure linkage table is reserved.
6bbec505 5695
cbe950e9 5696 Get the offset into the .got table of the entry that
407443a3 5697 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
cbe950e9
L
5698 bytes. The first three are reserved for the dynamic linker.
5699
5700 For static executables, we don't reserve anything. */
5701
6de2ae4a 5702 if (plt == htab->elf.splt)
cbe950e9 5703 {
eed180f8 5704 got_offset = h->plt.offset / abed->plt_entry_size - 1;
e1f98742 5705 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
cbe950e9
L
5706 }
5707 else
5708 {
eed180f8 5709 got_offset = h->plt.offset / abed->plt_entry_size;
e1f98742 5710 got_offset = got_offset * GOT_ENTRY_SIZE;
cbe950e9 5711 }
70256ad8 5712
0ff2b86e
L
5713 plt_plt_insn_end = abed->plt_plt_insn_end;
5714 plt_plt_offset = abed->plt_plt_offset;
5715 plt_got_insn_size = abed->plt_got_insn_size;
5716 plt_got_offset = abed->plt_got_offset;
5717 if (use_plt_bnd)
5718 {
5719 /* Use the second PLT with BND relocations. */
5720 const bfd_byte *plt_entry, *plt2_entry;
0ff2b86e
L
5721
5722 if (eh->has_bnd_reloc)
5723 {
5724 plt_entry = elf_x86_64_bnd_plt_entry;
5725 plt2_entry = elf_x86_64_bnd_plt2_entry;
5726 }
5727 else
5728 {
5729 plt_entry = elf_x86_64_legacy_plt_entry;
5730 plt2_entry = elf_x86_64_legacy_plt2_entry;
5731
5732 /* Subtract 1 since there is no BND prefix. */
5733 plt_plt_insn_end -= 1;
5734 plt_plt_offset -= 1;
5735 plt_got_insn_size -= 1;
5736 plt_got_offset -= 1;
5737 }
5738
5739 BFD_ASSERT (sizeof (elf_x86_64_bnd_plt_entry)
5740 == sizeof (elf_x86_64_legacy_plt_entry));
5741
5742 /* Fill in the entry in the procedure linkage table. */
5743 memcpy (plt->contents + h->plt.offset,
5744 plt_entry, sizeof (elf_x86_64_legacy_plt_entry));
5745 /* Fill in the entry in the second PLT. */
5746 memcpy (htab->plt_bnd->contents + eh->plt_bnd.offset,
5747 plt2_entry, sizeof (elf_x86_64_legacy_plt2_entry));
5748
5749 resolved_plt = htab->plt_bnd;
5750 plt_offset = eh->plt_bnd.offset;
5751 }
5752 else
5753 {
5754 /* Fill in the entry in the procedure linkage table. */
5755 memcpy (plt->contents + h->plt.offset, abed->plt_entry,
5756 abed->plt_entry_size);
5757
5758 resolved_plt = plt;
5759 plt_offset = h->plt.offset;
5760 }
eed180f8
RM
5761
5762 /* Insert the relocation positions of the plt section. */
5763
5764 /* Put offset the PC-relative instruction referring to the GOT entry,
5765 subtracting the size of that instruction. */
ab7fede8
L
5766 plt_got_pcrel_offset = (gotplt->output_section->vma
5767 + gotplt->output_offset
5768 + got_offset
5769 - resolved_plt->output_section->vma
5770 - resolved_plt->output_offset
5771 - plt_offset
5772 - plt_got_insn_size);
5773
5774 /* Check PC-relative offset overflow in PLT entry. */
5974eba6 5775 if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
ab7fede8
L
5776 info->callbacks->einfo (_("%F%B: PC-relative offset overflow in PLT entry for `%s'\n"),
5777 output_bfd, h->root.root.string);
5778
5779 bfd_put_32 (output_bfd, plt_got_pcrel_offset,
0ff2b86e 5780 resolved_plt->contents + plt_offset + plt_got_offset);
cbe950e9 5781
653165cc 5782 /* Fill in the entry in the global offset table, initially this
aec6b87e
L
5783 points to the second part of the PLT entry. Leave the entry
5784 as zero for undefined weak symbol in PIE. No PLT relocation
5785 against undefined weak symbol in PIE. */
5786 if (!local_undefweak)
cbe950e9 5787 {
aec6b87e
L
5788 bfd_put_64 (output_bfd, (plt->output_section->vma
5789 + plt->output_offset
5790 + h->plt.offset
5791 + abed->plt_lazy_offset),
5792 gotplt->contents + got_offset);
5793
5794 /* Fill in the entry in the .rela.plt section. */
5795 rela.r_offset = (gotplt->output_section->vma
5796 + gotplt->output_offset
5797 + got_offset);
5798 if (h->dynindx == -1
5799 || ((bfd_link_executable (info)
5800 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5801 && h->def_regular
5802 && h->type == STT_GNU_IFUNC))
5803 {
5804 /* If an STT_GNU_IFUNC symbol is locally defined, generate
5805 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
5806 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
5807 rela.r_addend = (h->root.u.def.value
5808 + h->root.u.def.section->output_section->vma
5809 + h->root.u.def.section->output_offset);
5810 /* R_X86_64_IRELATIVE comes last. */
5811 plt_index = htab->next_irelative_index--;
5812 }
5813 else
5814 {
5815 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
5816 rela.r_addend = 0;
5817 plt_index = htab->next_jump_slot_index++;
5818 }
e1f98742 5819
aec6b87e
L
5820 /* Don't fill PLT entry for static executables. */
5821 if (plt == htab->elf.splt)
5822 {
5823 bfd_vma plt0_offset = h->plt.offset + plt_plt_insn_end;
5824
5825 /* Put relocation index. */
5826 bfd_put_32 (output_bfd, plt_index,
5827 (plt->contents + h->plt.offset
5828 + abed->plt_reloc_offset));
5829
5830 /* Put offset for jmp .PLT0 and check for overflow. We don't
5831 check relocation index for overflow since branch displacement
5832 will overflow first. */
5833 if (plt0_offset > 0x80000000)
5834 info->callbacks->einfo (_("%F%B: branch displacement overflow in PLT entry for `%s'\n"),
5835 output_bfd, h->root.root.string);
5836 bfd_put_32 (output_bfd, - plt0_offset,
5837 plt->contents + h->plt.offset + plt_plt_offset);
5838 }
351f65ca 5839
aec6b87e
L
5840 bed = get_elf_backend_data (output_bfd);
5841 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
5842 bed->s->swap_reloca_out (output_bfd, &rela, loc);
5843 }
dd7e64d4
L
5844 }
5845 else if (eh->plt_got.offset != (bfd_vma) -1)
5846 {
5847 bfd_vma got_offset, plt_offset, plt_got_offset, plt_got_insn_size;
5848 asection *plt, *got;
5849 bfd_boolean got_after_plt;
5850 int32_t got_pcrel_offset;
5851 const bfd_byte *got_plt_entry;
5852
5853 /* Set the entry in the GOT procedure linkage table. */
5854 plt = htab->plt_got;
5855 got = htab->elf.sgot;
5856 got_offset = h->got.offset;
5857
5858 if (got_offset == (bfd_vma) -1
5859 || h->type == STT_GNU_IFUNC
5860 || plt == NULL
5861 || got == NULL)
5862 abort ();
70256ad8 5863
dd7e64d4
L
5864 /* Use the second PLT entry template for the GOT PLT since they
5865 are the identical. */
5866 plt_got_insn_size = elf_x86_64_bnd_arch_bed.plt_got_insn_size;
5867 plt_got_offset = elf_x86_64_bnd_arch_bed.plt_got_offset;
5868 if (eh->has_bnd_reloc)
5869 got_plt_entry = elf_x86_64_bnd_plt2_entry;
5870 else
70256ad8 5871 {
dd7e64d4
L
5872 got_plt_entry = elf_x86_64_legacy_plt2_entry;
5873
5874 /* Subtract 1 since there is no BND prefix. */
5875 plt_got_insn_size -= 1;
5876 plt_got_offset -= 1;
70256ad8 5877 }
dd7e64d4
L
5878
5879 /* Fill in the entry in the GOT procedure linkage table. */
5880 plt_offset = eh->plt_got.offset;
5881 memcpy (plt->contents + plt_offset,
5882 got_plt_entry, sizeof (elf_x86_64_legacy_plt2_entry));
5883
5884 /* Put offset the PC-relative instruction referring to the GOT
5885 entry, subtracting the size of that instruction. */
5886 got_pcrel_offset = (got->output_section->vma
5887 + got->output_offset
5888 + got_offset
5889 - plt->output_section->vma
5890 - plt->output_offset
5891 - plt_offset
5892 - plt_got_insn_size);
5893
5894 /* Check PC-relative offset overflow in GOT PLT entry. */
5895 got_after_plt = got->output_section->vma > plt->output_section->vma;
5896 if ((got_after_plt && got_pcrel_offset < 0)
5897 || (!got_after_plt && got_pcrel_offset > 0))
5898 info->callbacks->einfo (_("%F%B: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
5899 output_bfd, h->root.root.string);
5900
5901 bfd_put_32 (output_bfd, got_pcrel_offset,
5902 plt->contents + plt_offset + plt_got_offset);
5903 }
5904
aec6b87e
L
5905 if (!local_undefweak
5906 && !h->def_regular
dd7e64d4
L
5907 && (h->plt.offset != (bfd_vma) -1
5908 || eh->plt_got.offset != (bfd_vma) -1))
5909 {
5910 /* Mark the symbol as undefined, rather than as defined in
5911 the .plt section. Leave the value if there were any
5912 relocations where pointer equality matters (this is a clue
5913 for the dynamic linker, to make function pointer
5914 comparisons work between an application and shared
5915 library), otherwise set it to zero. If a function is only
5916 called from a binary, there is no need to slow down
5917 shared libraries because of that. */
5918 sym->st_shndx = SHN_UNDEF;
5919 if (!h->pointer_equality_needed)
5920 sym->st_value = 0;
70256ad8
AJ
5921 }
5922
aec6b87e
L
5923 /* Don't generate dynamic GOT relocation against undefined weak
5924 symbol in executable. */
bffbf940 5925 if (h->got.offset != (bfd_vma) -1
351f65ca 5926 && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
aec6b87e
L
5927 && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE
5928 && !local_undefweak)
053579d7 5929 {
053579d7 5930 Elf_Internal_Rela rela;
233cc9c1 5931 asection *relgot = htab->elf.srelgot;
053579d7
AJ
5932
5933 /* This symbol has an entry in the global offset table. Set it
bffbf940 5934 up. */
6de2ae4a 5935 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
c434dee6 5936 abort ();
053579d7 5937
6de2ae4a
L
5938 rela.r_offset = (htab->elf.sgot->output_section->vma
5939 + htab->elf.sgot->output_offset
dc810e39 5940 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
5941
5942 /* If this is a static link, or it is a -Bsymbolic link and the
5943 symbol is defined locally or was forced to be local because
5944 of a version file, we just want to emit a RELATIVE reloc.
5945 The entry in the global offset table will already have been
5946 initialized in the relocate_section function. */
710ab287 5947 if (h->def_regular
0018b0a3
L
5948 && h->type == STT_GNU_IFUNC)
5949 {
233cc9c1
L
5950 if (h->plt.offset == (bfd_vma) -1)
5951 {
5952 /* STT_GNU_IFUNC is referenced without PLT. */
5953 if (htab->elf.splt == NULL)
5954 {
5955 /* use .rel[a].iplt section to store .got relocations
5956 in static executable. */
5957 relgot = htab->elf.irelplt;
5958 }
5959 if (SYMBOL_REFERENCES_LOCAL (info, h))
5960 {
5961 rela.r_info = htab->r_info (0,
5962 R_X86_64_IRELATIVE);
5963 rela.r_addend = (h->root.u.def.value
5964 + h->root.u.def.section->output_section->vma
5965 + h->root.u.def.section->output_offset);
5966 }
5967 else
5968 goto do_glob_dat;
5969 }
5970 else if (bfd_link_pic (info))
710ab287
L
5971 {
5972 /* Generate R_X86_64_GLOB_DAT. */
5973 goto do_glob_dat;
5974 }
5975 else
5976 {
90d60710
L
5977 asection *plt;
5978
710ab287
L
5979 if (!h->pointer_equality_needed)
5980 abort ();
5981
5982 /* For non-shared object, we can't use .got.plt, which
5983 contains the real function addres if we need pointer
5984 equality. We load the GOT entry with the PLT entry. */
90d60710 5985 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
710ab287
L
5986 bfd_put_64 (output_bfd, (plt->output_section->vma
5987 + plt->output_offset
5988 + h->plt.offset),
6de2ae4a 5989 htab->elf.sgot->contents + h->got.offset);
710ab287
L
5990 return TRUE;
5991 }
0018b0a3 5992 }
0e1862bb 5993 else if (bfd_link_pic (info)
0018b0a3 5994 && SYMBOL_REFERENCES_LOCAL (info, h))
053579d7 5995 {
41bed6dd
L
5996 if (!h->def_regular)
5997 return FALSE;
cc78d0af 5998 BFD_ASSERT((h->got.offset & 1) != 0);
351f65ca 5999 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
053579d7
AJ
6000 rela.r_addend = (h->root.u.def.value
6001 + h->root.u.def.section->output_section->vma
6002 + h->root.u.def.section->output_offset);
6003 }
6004 else
6005 {
6006 BFD_ASSERT((h->got.offset & 1) == 0);
710ab287 6007do_glob_dat:
c434dee6 6008 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 6009 htab->elf.sgot->contents + h->got.offset);
351f65ca 6010 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
053579d7
AJ
6011 rela.r_addend = 0;
6012 }
6013
233cc9c1 6014 elf_append_rela (output_bfd, relgot, &rela);
053579d7
AJ
6015 }
6016
f5385ebf 6017 if (h->needs_copy)
70256ad8 6018 {
70256ad8
AJ
6019 Elf_Internal_Rela rela;
6020
6021 /* This symbol needs a copy reloc. Set it up. */
6022
c434dee6
AJ
6023 if (h->dynindx == -1
6024 || (h->root.type != bfd_link_hash_defined
6025 && h->root.type != bfd_link_hash_defweak)
6026 || htab->srelbss == NULL)
6027 abort ();
70256ad8
AJ
6028
6029 rela.r_offset = (h->root.u.def.value
6030 + h->root.u.def.section->output_section->vma
6031 + h->root.u.def.section->output_offset);
351f65ca 6032 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
70256ad8 6033 rela.r_addend = 0;
351f65ca 6034 elf_append_rela (output_bfd, htab->srelbss, &rela);
70256ad8
AJ
6035 }
6036
b34976b6 6037 return TRUE;
70256ad8
AJ
6038}
6039
c25bc9fc
L
6040/* Finish up local dynamic symbol handling. We set the contents of
6041 various dynamic sections here. */
6042
6043static bfd_boolean
351f65ca 6044elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
c25bc9fc
L
6045{
6046 struct elf_link_hash_entry *h
6047 = (struct elf_link_hash_entry *) *slot;
6048 struct bfd_link_info *info
eed180f8 6049 = (struct bfd_link_info *) inf;
c25bc9fc 6050
351f65ca 6051 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
c25bc9fc
L
6052 info, h, NULL);
6053}
6054
aec6b87e
L
6055/* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
6056 here since undefined weak symbol may not be dynamic and may not be
6057 called for elf_x86_64_finish_dynamic_symbol. */
6058
6059static bfd_boolean
6060elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
6061 void *inf)
6062{
6063 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
6064 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6065
6066 if (h->root.type != bfd_link_hash_undefweak
6067 || h->dynindx != -1)
6068 return TRUE;
6069
6070 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
6071 info, h, NULL);
6072}
6073
c434dee6
AJ
6074/* Used to decide how to sort relocs in an optimal manner for the
6075 dynamic linker, before writing them out. */
6076
6077static enum elf_reloc_type_class
cae1fbbb 6078elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
7e612e98
AM
6079 const asection *rel_sec ATTRIBUTE_UNUSED,
6080 const Elf_Internal_Rela *rela)
c434dee6 6081{
cae1fbbb
L
6082 bfd *abfd = info->output_bfd;
6083 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6084 struct elf_x86_64_link_hash_table *htab = elf_x86_64_hash_table (info);
cae1fbbb 6085
d9e3b590
L
6086 if (htab->elf.dynsym != NULL
6087 && htab->elf.dynsym->contents != NULL)
6088 {
6089 /* Check relocation against STT_GNU_IFUNC symbol if there are
6090 dynamic symbols. */
6091 unsigned long r_symndx = htab->r_sym (rela->r_info);
897463b1
L
6092 if (r_symndx != STN_UNDEF)
6093 {
6094 Elf_Internal_Sym sym;
6095 if (!bed->s->swap_symbol_in (abfd,
6096 (htab->elf.dynsym->contents
6097 + r_symndx * bed->s->sizeof_sym),
6098 0, &sym))
6099 abort ();
d9e3b590 6100
897463b1
L
6101 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
6102 return reloc_class_ifunc;
6103 }
d9e3b590 6104 }
cae1fbbb 6105
351f65ca 6106 switch ((int) ELF32_R_TYPE (rela->r_info))
c434dee6 6107 {
c428ce9d
L
6108 case R_X86_64_IRELATIVE:
6109 return reloc_class_ifunc;
c434dee6 6110 case R_X86_64_RELATIVE:
1da80baa 6111 case R_X86_64_RELATIVE64:
c434dee6
AJ
6112 return reloc_class_relative;
6113 case R_X86_64_JUMP_SLOT:
6114 return reloc_class_plt;
6115 case R_X86_64_COPY:
6116 return reloc_class_copy;
6117 default:
6118 return reloc_class_normal;
6119 }
6120}
6121
70256ad8
AJ
6122/* Finish up the dynamic sections. */
6123
b34976b6 6124static bfd_boolean
351f65ca
L
6125elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
6126 struct bfd_link_info *info)
70256ad8 6127{
351f65ca 6128 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
6129 bfd *dynobj;
6130 asection *sdyn;
0ff2b86e 6131 const struct elf_x86_64_backend_data *abed;
70256ad8 6132
351f65ca 6133 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
6134 if (htab == NULL)
6135 return FALSE;
6136
0ff2b86e
L
6137 /* Use MPX backend data in case of BND relocation. Use .plt_bnd
6138 section only if there is .plt section. */
6139 abed = (htab->elf.splt != NULL && htab->plt_bnd != NULL
6140 ? &elf_x86_64_bnd_arch_bed
6141 : get_elf_x86_64_backend_data (output_bfd));
6142
c434dee6 6143 dynobj = htab->elf.dynobj;
3d4d4302 6144 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
70256ad8 6145
c434dee6 6146 if (htab->elf.dynamic_sections_created)
70256ad8 6147 {
82e96e07
L
6148 bfd_byte *dyncon, *dynconend;
6149 const struct elf_backend_data *bed;
6150 bfd_size_type sizeof_dyn;
70256ad8 6151
6de2ae4a 6152 if (sdyn == NULL || htab->elf.sgot == NULL)
c434dee6 6153 abort ();
70256ad8 6154
82e96e07
L
6155 bed = get_elf_backend_data (dynobj);
6156 sizeof_dyn = bed->s->sizeof_dyn;
6157 dyncon = sdyn->contents;
6158 dynconend = sdyn->contents + sdyn->size;
6159 for (; dyncon < dynconend; dyncon += sizeof_dyn)
70256ad8
AJ
6160 {
6161 Elf_Internal_Dyn dyn;
70256ad8
AJ
6162 asection *s;
6163
82e96e07 6164 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
70256ad8
AJ
6165
6166 switch (dyn.d_tag)
6167 {
6168 default:
053579d7 6169 continue;
70256ad8
AJ
6170
6171 case DT_PLTGOT:
6de2ae4a 6172 s = htab->elf.sgotplt;
8c37241b 6173 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
c434dee6 6174 break;
70256ad8
AJ
6175
6176 case DT_JMPREL:
6de2ae4a 6177 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
c434dee6 6178 break;
70256ad8 6179
c434dee6 6180 case DT_PLTRELSZ:
6de2ae4a 6181 s = htab->elf.srelplt->output_section;
eea6121a 6182 dyn.d_un.d_val = s->size;
70256ad8
AJ
6183 break;
6184
6185 case DT_RELASZ:
c434dee6
AJ
6186 /* The procedure linkage table relocs (DT_JMPREL) should
6187 not be included in the overall relocs (DT_RELA).
6188 Therefore, we override the DT_RELASZ entry here to
6189 make it not include the JMPREL relocs. Since the
6190 linker script arranges for .rela.plt to follow all
6191 other relocation sections, we don't have to worry
6192 about changing the DT_RELA entry. */
6de2ae4a 6193 if (htab->elf.srelplt != NULL)
70256ad8 6194 {
6de2ae4a 6195 s = htab->elf.srelplt->output_section;
eea6121a 6196 dyn.d_un.d_val -= s->size;
70256ad8
AJ
6197 }
6198 break;
67a4f2b7
AO
6199
6200 case DT_TLSDESC_PLT:
6de2ae4a 6201 s = htab->elf.splt;
67a4f2b7
AO
6202 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
6203 + htab->tlsdesc_plt;
6204 break;
6205
6206 case DT_TLSDESC_GOT:
6de2ae4a 6207 s = htab->elf.sgot;
67a4f2b7
AO
6208 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
6209 + htab->tlsdesc_got;
6210 break;
70256ad8 6211 }
c434dee6 6212
82e96e07 6213 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
70256ad8
AJ
6214 }
6215
c434dee6 6216 /* Fill in the special first entry in the procedure linkage table. */
6de2ae4a 6217 if (htab->elf.splt && htab->elf.splt->size > 0)
70256ad8 6218 {
653165cc 6219 /* Fill in the first entry in the procedure linkage table. */
eed180f8
RM
6220 memcpy (htab->elf.splt->contents,
6221 abed->plt0_entry, abed->plt_entry_size);
653165cc
AJ
6222 /* Add offset for pushq GOT+8(%rip), since the instruction
6223 uses 6 bytes subtract this value. */
6224 bfd_put_32 (output_bfd,
6de2ae4a
L
6225 (htab->elf.sgotplt->output_section->vma
6226 + htab->elf.sgotplt->output_offset
653165cc 6227 + 8
6de2ae4a
L
6228 - htab->elf.splt->output_section->vma
6229 - htab->elf.splt->output_offset
653165cc 6230 - 6),
eed180f8
RM
6231 htab->elf.splt->contents + abed->plt0_got1_offset);
6232 /* Add offset for the PC-relative instruction accessing GOT+16,
6233 subtracting the offset to the end of that instruction. */
653165cc 6234 bfd_put_32 (output_bfd,
6de2ae4a
L
6235 (htab->elf.sgotplt->output_section->vma
6236 + htab->elf.sgotplt->output_offset
653165cc 6237 + 16
6de2ae4a
L
6238 - htab->elf.splt->output_section->vma
6239 - htab->elf.splt->output_offset
eed180f8
RM
6240 - abed->plt0_got2_insn_end),
6241 htab->elf.splt->contents + abed->plt0_got2_offset);
653165cc 6242
eed180f8
RM
6243 elf_section_data (htab->elf.splt->output_section)
6244 ->this_hdr.sh_entsize = abed->plt_entry_size;
67a4f2b7
AO
6245
6246 if (htab->tlsdesc_plt)
6247 {
6248 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 6249 htab->elf.sgot->contents + htab->tlsdesc_got);
67a4f2b7 6250
6de2ae4a 6251 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
eed180f8 6252 abed->plt0_entry, abed->plt_entry_size);
67a4f2b7
AO
6253
6254 /* Add offset for pushq GOT+8(%rip), since the
6255 instruction uses 6 bytes subtract this value. */
6256 bfd_put_32 (output_bfd,
6de2ae4a
L
6257 (htab->elf.sgotplt->output_section->vma
6258 + htab->elf.sgotplt->output_offset
67a4f2b7 6259 + 8
6de2ae4a
L
6260 - htab->elf.splt->output_section->vma
6261 - htab->elf.splt->output_offset
67a4f2b7
AO
6262 - htab->tlsdesc_plt
6263 - 6),
eed180f8
RM
6264 htab->elf.splt->contents
6265 + htab->tlsdesc_plt + abed->plt0_got1_offset);
6266 /* Add offset for the PC-relative instruction accessing GOT+TDG,
6267 where TGD stands for htab->tlsdesc_got, subtracting the offset
6268 to the end of that instruction. */
67a4f2b7 6269 bfd_put_32 (output_bfd,
6de2ae4a
L
6270 (htab->elf.sgot->output_section->vma
6271 + htab->elf.sgot->output_offset
67a4f2b7 6272 + htab->tlsdesc_got
6de2ae4a
L
6273 - htab->elf.splt->output_section->vma
6274 - htab->elf.splt->output_offset
67a4f2b7 6275 - htab->tlsdesc_plt
eed180f8
RM
6276 - abed->plt0_got2_insn_end),
6277 htab->elf.splt->contents
6278 + htab->tlsdesc_plt + abed->plt0_got2_offset);
67a4f2b7 6279 }
70256ad8 6280 }
70256ad8
AJ
6281 }
6282
0ff2b86e
L
6283 if (htab->plt_bnd != NULL)
6284 elf_section_data (htab->plt_bnd->output_section)
6285 ->this_hdr.sh_entsize = sizeof (elf_x86_64_bnd_plt2_entry);
6286
6de2ae4a 6287 if (htab->elf.sgotplt)
70256ad8 6288 {
56d4289c
L
6289 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
6290 {
6291 (*_bfd_error_handler)
6292 (_("discarded output section: `%A'"), htab->elf.sgotplt);
6293 return FALSE;
6294 }
6295
c434dee6 6296 /* Fill in the first three entries in the global offset table. */
6de2ae4a 6297 if (htab->elf.sgotplt->size > 0)
c434dee6
AJ
6298 {
6299 /* Set the first entry in the global offset table to the address of
6300 the dynamic section. */
6301 if (sdyn == NULL)
6de2ae4a 6302 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
c434dee6
AJ
6303 else
6304 bfd_put_64 (output_bfd,
6305 sdyn->output_section->vma + sdyn->output_offset,
6de2ae4a 6306 htab->elf.sgotplt->contents);
c434dee6 6307 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
6de2ae4a
L
6308 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
6309 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
c434dee6 6310 }
70256ad8 6311
6de2ae4a 6312 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
c434dee6
AJ
6313 GOT_ENTRY_SIZE;
6314 }
70256ad8 6315
e41b3a13 6316 /* Adjust .eh_frame for .plt section. */
9a2a56cc
AM
6317 if (htab->plt_eh_frame != NULL
6318 && htab->plt_eh_frame->contents != NULL)
e41b3a13
JJ
6319 {
6320 if (htab->elf.splt != NULL
6321 && htab->elf.splt->size != 0
6322 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
6323 && htab->elf.splt->output_section != NULL
6324 && htab->plt_eh_frame->output_section != NULL)
6325 {
6326 bfd_vma plt_start = htab->elf.splt->output_section->vma;
6327 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
6328 + htab->plt_eh_frame->output_offset
6329 + PLT_FDE_START_OFFSET;
6330 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6331 htab->plt_eh_frame->contents
6332 + PLT_FDE_START_OFFSET);
6333 }
dbaa2011 6334 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
e41b3a13
JJ
6335 {
6336 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6337 htab->plt_eh_frame,
6338 htab->plt_eh_frame->contents))
6339 return FALSE;
6340 }
6341 }
6342
6de2ae4a
L
6343 if (htab->elf.sgot && htab->elf.sgot->size > 0)
6344 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
8c37241b
JJ
6345 = GOT_ENTRY_SIZE;
6346
aec6b87e
L
6347 /* Fill PLT entries for undefined weak symbols in PIE. */
6348 if (bfd_link_pie (info))
6349 bfd_hash_traverse (&info->hash->table,
6350 elf_x86_64_pie_finish_undefweak_symbol,
6351 info);
6352
b34976b6 6353 return TRUE;
8d88c4ca
NC
6354}
6355
233cc9c1
L
6356/* Fill PLT/GOT entries and allocate dynamic relocations for local
6357 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
6358 It has to be done before elf_link_sort_relocs is called so that
6359 dynamic relocations are properly sorted. */
6360
6361static bfd_boolean
6362elf_x86_64_output_arch_local_syms
6363 (bfd *output_bfd ATTRIBUTE_UNUSED,
6364 struct bfd_link_info *info,
6365 void *flaginfo ATTRIBUTE_UNUSED,
6366 int (*func) (void *, const char *,
6367 Elf_Internal_Sym *,
6368 asection *,
6369 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
6370{
6371 struct elf_x86_64_link_hash_table *htab = elf_x86_64_hash_table (info);
6372 if (htab == NULL)
6373 return FALSE;
6374
6375 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
6376 htab_traverse (htab->loc_hash_table,
6377 elf_x86_64_finish_local_dynamic_symbol,
6378 info);
6379
6380 return TRUE;
6381}
6382
3972882e 6383/* Return an array of PLT entry symbol values. */
4c45e5c9 6384
3972882e
L
6385static bfd_vma *
6386elf_x86_64_get_plt_sym_val (bfd *abfd, asymbol **dynsyms, asection *plt,
6387 asection *relplt)
4c45e5c9 6388{
3972882e
L
6389 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6390 arelent *p;
6391 long count, i;
6392 bfd_vma *plt_sym_val;
144bed8d 6393 bfd_vma plt_offset;
3972882e
L
6394 bfd_byte *plt_contents;
6395 const struct elf_x86_64_backend_data *bed;
6396 Elf_Internal_Shdr *hdr;
6397 asection *plt_bnd;
144bed8d 6398
3972882e
L
6399 /* Get the .plt section contents. PLT passed down may point to the
6400 .plt.bnd section. Make sure that PLT always points to the .plt
6401 section. */
6402 plt_bnd = bfd_get_section_by_name (abfd, ".plt.bnd");
6403 if (plt_bnd)
6404 {
6405 if (plt != plt_bnd)
6406 abort ();
6407 plt = bfd_get_section_by_name (abfd, ".plt");
6408 if (plt == NULL)
6409 abort ();
6410 bed = &elf_x86_64_bnd_arch_bed;
6411 }
6412 else
6413 bed = get_elf_x86_64_backend_data (abfd);
cca5b8b6 6414
3972882e
L
6415 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
6416 if (plt_contents == NULL)
6417 return NULL;
6418 if (!bfd_get_section_contents (abfd, (asection *) plt,
6419 plt_contents, 0, plt->size))
144bed8d 6420 {
3972882e
L
6421bad_return:
6422 free (plt_contents);
6423 return NULL;
144bed8d
L
6424 }
6425
3972882e
L
6426 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6427 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
6428 goto bad_return;
144bed8d 6429
3972882e
L
6430 hdr = &elf_section_data (relplt)->this_hdr;
6431 count = relplt->size / hdr->sh_entsize;
144bed8d 6432
3972882e
L
6433 plt_sym_val = (bfd_vma *) bfd_malloc (sizeof (bfd_vma) * count);
6434 if (plt_sym_val == NULL)
6435 goto bad_return;
cca5b8b6 6436
35181b3e 6437 for (i = 0; i < count; i++)
3972882e 6438 plt_sym_val[i] = -1;
cca5b8b6 6439
3972882e
L
6440 plt_offset = bed->plt_entry_size;
6441 p = relplt->relocation;
6442 for (i = 0; i < count; i++, p++)
144bed8d 6443 {
3972882e 6444 long reloc_index;
144bed8d 6445
6f25f223 6446 /* Skip unknown relocation. */
533d0af0 6447 if (p->howto == NULL)
6f25f223 6448 continue;
533d0af0 6449
3972882e
L
6450 if (p->howto->type != R_X86_64_JUMP_SLOT
6451 && p->howto->type != R_X86_64_IRELATIVE)
6452 continue;
144bed8d 6453
3972882e
L
6454 reloc_index = H_GET_32 (abfd, (plt_contents + plt_offset
6455 + bed->plt_reloc_offset));
ec1f73bb 6456 if (reloc_index < count)
144bed8d 6457 {
ec1f73bb
AM
6458 if (plt_bnd)
6459 {
6460 /* This is the index in .plt section. */
6461 long plt_index = plt_offset / bed->plt_entry_size;
6462 /* Store VMA + the offset in .plt.bnd section. */
6463 plt_sym_val[reloc_index] =
6464 (plt_bnd->vma
6465 + (plt_index - 1) * sizeof (elf_x86_64_legacy_plt2_entry));
6466 }
6467 else
6468 plt_sym_val[reloc_index] = plt->vma + plt_offset;
144bed8d
L
6469 }
6470 plt_offset += bed->plt_entry_size;
fca6ae69
L
6471
6472 /* PR binutils/18437: Skip extra relocations in the .rela.plt
6473 section. */
6474 if (plt_offset >= plt->size)
6475 break;
144bed8d
L
6476 }
6477
3972882e
L
6478 free (plt_contents);
6479
6480 return plt_sym_val;
4c45e5c9 6481}
8df9fc9d 6482
0ff2b86e
L
6483/* Similar to _bfd_elf_get_synthetic_symtab, with .plt.bnd section
6484 support. */
6485
6486static long
6487elf_x86_64_get_synthetic_symtab (bfd *abfd,
6488 long symcount,
6489 asymbol **syms,
6490 long dynsymcount,
6491 asymbol **dynsyms,
6492 asymbol **ret)
6493{
3972882e
L
6494 /* Pass the .plt.bnd section to _bfd_elf_ifunc_get_synthetic_symtab
6495 as PLT if it exists. */
6496 asection *plt = bfd_get_section_by_name (abfd, ".plt.bnd");
0ff2b86e 6497 if (plt == NULL)
3972882e
L
6498 plt = bfd_get_section_by_name (abfd, ".plt");
6499 return _bfd_elf_ifunc_get_synthetic_symtab (abfd, symcount, syms,
6500 dynsymcount, dynsyms, ret,
6501 plt,
6502 elf_x86_64_get_plt_sym_val);
0ff2b86e
L
6503}
6504
d2b2c203
DJ
6505/* Handle an x86-64 specific section when reading an object file. This
6506 is called when elfcode.h finds a section with an unknown type. */
6507
6508static bfd_boolean
0c723101
L
6509elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
6510 const char *name, int shindex)
d2b2c203
DJ
6511{
6512 if (hdr->sh_type != SHT_X86_64_UNWIND)
6513 return FALSE;
6514
6dc132d9 6515 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
6516 return FALSE;
6517
6518 return TRUE;
6519}
6520
3b22753a
L
6521/* Hook called by the linker routine which adds symbols from an object
6522 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
6523 of .bss. */
6524
6525static bfd_boolean
351f65ca 6526elf_x86_64_add_symbol_hook (bfd *abfd,
a43942db 6527 struct bfd_link_info *info ATTRIBUTE_UNUSED,
351f65ca
L
6528 Elf_Internal_Sym *sym,
6529 const char **namep ATTRIBUTE_UNUSED,
6530 flagword *flagsp ATTRIBUTE_UNUSED,
6531 asection **secp,
6532 bfd_vma *valp)
3b22753a
L
6533{
6534 asection *lcomm;
6535
6536 switch (sym->st_shndx)
6537 {
6538 case SHN_X86_64_LCOMMON:
6539 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
6540 if (lcomm == NULL)
6541 {
6542 lcomm = bfd_make_section_with_flags (abfd,
6543 "LARGE_COMMON",
6544 (SEC_ALLOC
6545 | SEC_IS_COMMON
6546 | SEC_LINKER_CREATED));
6547 if (lcomm == NULL)
6548 return FALSE;
6549 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
6550 }
6551 *secp = lcomm;
6552 *valp = sym->st_size;
c35bdf6e 6553 return TRUE;
3b22753a 6554 }
d8045f23 6555
3b22753a
L
6556 return TRUE;
6557}
6558
6559
6560/* Given a BFD section, try to locate the corresponding ELF section
6561 index. */
6562
6563static bfd_boolean
351f65ca
L
6564elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6565 asection *sec, int *index_return)
3b22753a
L
6566{
6567 if (sec == &_bfd_elf_large_com_section)
6568 {
91d6fa6a 6569 *index_return = SHN_X86_64_LCOMMON;
3b22753a
L
6570 return TRUE;
6571 }
6572 return FALSE;
6573}
6574
6575/* Process a symbol. */
6576
6577static void
351f65ca
L
6578elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
6579 asymbol *asym)
3b22753a
L
6580{
6581 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
6582
6583 switch (elfsym->internal_elf_sym.st_shndx)
6584 {
6585 case SHN_X86_64_LCOMMON:
6586 asym->section = &_bfd_elf_large_com_section;
6587 asym->value = elfsym->internal_elf_sym.st_size;
6588 /* Common symbol doesn't set BSF_GLOBAL. */
6589 asym->flags &= ~BSF_GLOBAL;
6590 break;
6591 }
6592}
6593
6594static bfd_boolean
351f65ca 6595elf_x86_64_common_definition (Elf_Internal_Sym *sym)
3b22753a
L
6596{
6597 return (sym->st_shndx == SHN_COMMON
6598 || sym->st_shndx == SHN_X86_64_LCOMMON);
6599}
6600
6601static unsigned int
351f65ca 6602elf_x86_64_common_section_index (asection *sec)
3b22753a
L
6603{
6604 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6605 return SHN_COMMON;
6606 else
6607 return SHN_X86_64_LCOMMON;
6608}
6609
6610static asection *
351f65ca 6611elf_x86_64_common_section (asection *sec)
3b22753a
L
6612{
6613 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6614 return bfd_com_section_ptr;
6615 else
6616 return &_bfd_elf_large_com_section;
6617}
6618
6619static bfd_boolean
5d13b3b3
AM
6620elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
6621 const Elf_Internal_Sym *sym,
351f65ca 6622 asection **psec,
5d13b3b3
AM
6623 bfd_boolean newdef,
6624 bfd_boolean olddef,
351f65ca 6625 bfd *oldbfd,
5d13b3b3 6626 const asection *oldsec)
3b22753a
L
6627{
6628 /* A normal common symbol and a large common symbol result in a
00492999
L
6629 normal common symbol. We turn the large common symbol into a
6630 normal one. */
5d13b3b3 6631 if (!olddef
3b22753a 6632 && h->root.type == bfd_link_hash_common
5d13b3b3
AM
6633 && !newdef
6634 && bfd_is_com_section (*psec)
6635 && oldsec != *psec)
3b22753a 6636 {
00492999 6637 if (sym->st_shndx == SHN_COMMON
5d13b3b3 6638 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
00492999
L
6639 {
6640 h->root.u.c.p->section
6641 = bfd_make_section_old_way (oldbfd, "COMMON");
6642 h->root.u.c.p->section->flags = SEC_ALLOC;
6643 }
6644 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5d13b3b3
AM
6645 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
6646 *psec = bfd_com_section_ptr;
3b22753a
L
6647 }
6648
6649 return TRUE;
6650}
6651
6652static int
351f65ca
L
6653elf_x86_64_additional_program_headers (bfd *abfd,
6654 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3b22753a
L
6655{
6656 asection *s;
9a2e389a 6657 int count = 0;
3b22753a
L
6658
6659 /* Check to see if we need a large readonly segment. */
6660 s = bfd_get_section_by_name (abfd, ".lrodata");
6661 if (s && (s->flags & SEC_LOAD))
6662 count++;
6663
6664 /* Check to see if we need a large data segment. Since .lbss sections
6665 is placed right after the .bss section, there should be no need for
6666 a large data segment just because of .lbss. */
6667 s = bfd_get_section_by_name (abfd, ".ldata");
6668 if (s && (s->flags & SEC_LOAD))
6669 count++;
6670
6671 return count;
6672}
6673
fdc90cb4
JJ
6674/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6675
6676static bfd_boolean
351f65ca 6677elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
fdc90cb4
JJ
6678{
6679 if (h->plt.offset != (bfd_vma) -1
6680 && !h->def_regular
6681 && !h->pointer_equality_needed)
6682 return FALSE;
6683
6684 return _bfd_elf_hash_symbol (h);
6685}
6686
c543bf9a
L
6687/* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
6688
6689static bfd_boolean
6690elf_x86_64_relocs_compatible (const bfd_target *input,
6691 const bfd_target *output)
6692{
6693 return ((xvec_get_elf_backend_data (input)->s->elfclass
6694 == xvec_get_elf_backend_data (output)->s->elfclass)
6695 && _bfd_elf_relocs_compatible (input, output));
6696}
6697
9a2e389a 6698static const struct bfd_elf_special_section
351f65ca 6699 elf_x86_64_special_sections[]=
3b22753a 6700{
0112cd26
NC
6701 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6702 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
6703 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
6704 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6705 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6706 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
6707 { NULL, 0, 0, 0, 0 }
3b22753a
L
6708};
6709
6d00b590 6710#define TARGET_LITTLE_SYM x86_64_elf64_vec
70256ad8
AJ
6711#define TARGET_LITTLE_NAME "elf64-x86-64"
6712#define ELF_ARCH bfd_arch_i386
ae95ffa6 6713#define ELF_TARGET_ID X86_64_ELF_DATA
70256ad8 6714#define ELF_MACHINE_CODE EM_X86_64
f7661549 6715#define ELF_MAXPAGESIZE 0x200000
2043964e 6716#define ELF_MINPAGESIZE 0x1000
24718e3b 6717#define ELF_COMMONPAGESIZE 0x1000
70256ad8
AJ
6718
6719#define elf_backend_can_gc_sections 1
51b64d56 6720#define elf_backend_can_refcount 1
70256ad8
AJ
6721#define elf_backend_want_got_plt 1
6722#define elf_backend_plt_readonly 1
6723#define elf_backend_want_plt_sym 0
6724#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 6725#define elf_backend_rela_normal 1
e41b3a13 6726#define elf_backend_plt_alignment 4
f7483970 6727#define elf_backend_extern_protected_data 1
bedfd056 6728#define elf_backend_caches_rawsize 1
70256ad8 6729
351f65ca 6730#define elf_info_to_howto elf_x86_64_info_to_howto
70256ad8 6731
70256ad8 6732#define bfd_elf64_bfd_link_hash_table_create \
351f65ca 6733 elf_x86_64_link_hash_table_create
351f65ca 6734#define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
157090f7 6735#define bfd_elf64_bfd_reloc_name_lookup \
351f65ca 6736 elf_x86_64_reloc_name_lookup
70256ad8 6737
351f65ca 6738#define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
c543bf9a 6739#define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
351f65ca
L
6740#define elf_backend_check_relocs elf_x86_64_check_relocs
6741#define elf_backend_copy_indirect_symbol elf_x86_64_copy_indirect_symbol
6742#define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
6743#define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
6744#define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
233cc9c1 6745#define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
351f65ca 6746#define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
351f65ca
L
6747#define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
6748#define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
8fd79e71
L
6749#ifdef CORE_HEADER
6750#define elf_backend_write_core_note elf_x86_64_write_core_note
6751#endif
351f65ca
L
6752#define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
6753#define elf_backend_relocate_section elf_x86_64_relocate_section
6754#define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
6755#define elf_backend_always_size_sections elf_x86_64_always_size_sections
74541ad4 6756#define elf_backend_init_index_section _bfd_elf_init_1_index_section
407443a3 6757#define elf_backend_object_p elf64_x86_64_elf_object_p
351f65ca 6758#define bfd_elf64_mkobject elf_x86_64_mkobject
0ff2b86e 6759#define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
8d88c4ca 6760
d2b2c203 6761#define elf_backend_section_from_shdr \
351f65ca 6762 elf_x86_64_section_from_shdr
d2b2c203 6763
3b22753a 6764#define elf_backend_section_from_bfd_section \
351f65ca 6765 elf_x86_64_elf_section_from_bfd_section
3b22753a 6766#define elf_backend_add_symbol_hook \
351f65ca 6767 elf_x86_64_add_symbol_hook
3b22753a 6768#define elf_backend_symbol_processing \
351f65ca 6769 elf_x86_64_symbol_processing
3b22753a 6770#define elf_backend_common_section_index \
351f65ca 6771 elf_x86_64_common_section_index
3b22753a 6772#define elf_backend_common_section \
351f65ca 6773 elf_x86_64_common_section
3b22753a 6774#define elf_backend_common_definition \
351f65ca 6775 elf_x86_64_common_definition
3b22753a 6776#define elf_backend_merge_symbol \
351f65ca 6777 elf_x86_64_merge_symbol
3b22753a 6778#define elf_backend_special_sections \
351f65ca 6779 elf_x86_64_special_sections
3b22753a 6780#define elf_backend_additional_program_headers \
351f65ca 6781 elf_x86_64_additional_program_headers
fdc90cb4 6782#define elf_backend_hash_symbol \
351f65ca 6783 elf_x86_64_hash_symbol
8f79b794
L
6784#define elf_backend_omit_section_dynsym \
6785 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
aec6b87e
L
6786#define elf_backend_fixup_symbol \
6787 elf_x86_64_fixup_symbol
3b22753a 6788
8d88c4ca 6789#include "elf64-target.h"
9d7cbccd 6790
6036f486
ES
6791/* CloudABI support. */
6792
6793#undef TARGET_LITTLE_SYM
6794#define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
6795#undef TARGET_LITTLE_NAME
6796#define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
6797
6798#undef ELF_OSABI
6799#define ELF_OSABI ELFOSABI_CLOUDABI
6800
6801#undef elf64_bed
6802#define elf64_bed elf64_x86_64_cloudabi_bed
6803
6804#include "elf64-target.h"
6805
9d7cbccd
NC
6806/* FreeBSD support. */
6807
6808#undef TARGET_LITTLE_SYM
6d00b590 6809#define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
9d7cbccd
NC
6810#undef TARGET_LITTLE_NAME
6811#define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
6812
d1036acb
L
6813#undef ELF_OSABI
6814#define ELF_OSABI ELFOSABI_FREEBSD
9d7cbccd 6815
9d7cbccd
NC
6816#undef elf64_bed
6817#define elf64_bed elf64_x86_64_fbsd_bed
6818
6819#include "elf64-target.h"
8a9036a4 6820
a6cc6b3b
RO
6821/* Solaris 2 support. */
6822
6823#undef TARGET_LITTLE_SYM
6d00b590 6824#define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
a6cc6b3b
RO
6825#undef TARGET_LITTLE_NAME
6826#define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
6827
6828/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
6829 objects won't be recognized. */
6830#undef ELF_OSABI
6831
6832#undef elf64_bed
6833#define elf64_bed elf64_x86_64_sol2_bed
6834
7dc98aea
RO
6835/* The 64-bit static TLS arena size is rounded to the nearest 16-byte
6836 boundary. */
84865015 6837#undef elf_backend_static_tls_alignment
7dc98aea
RO
6838#define elf_backend_static_tls_alignment 16
6839
a6cc6b3b
RO
6840/* The Solaris 2 ABI requires a plt symbol on all platforms.
6841
6842 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
6843 File, p.63. */
84865015 6844#undef elf_backend_want_plt_sym
a6cc6b3b
RO
6845#define elf_backend_want_plt_sym 1
6846
84865015
NC
6847#undef elf_backend_strtab_flags
6848#define elf_backend_strtab_flags SHF_STRINGS
6849
6850static bfd_boolean
5522f910
NC
6851elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
6852 bfd *obfd ATTRIBUTE_UNUSED,
6853 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
6854 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
84865015
NC
6855{
6856 /* PR 19938: FIXME: Need to add code for setting the sh_info
6857 and sh_link fields of Solaris specific section types. */
6858 return FALSE;
6859}
6860
5522f910
NC
6861#undef elf_backend_copy_special_section_fields
6862#define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
84865015 6863
a6cc6b3b
RO
6864#include "elf64-target.h"
6865
8059fb19
RM
6866/* Native Client support. */
6867
64b384e1
RM
6868static bfd_boolean
6869elf64_x86_64_nacl_elf_object_p (bfd *abfd)
6870{
6871 /* Set the right machine number for a NaCl x86-64 ELF64 file. */
6872 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
6873 return TRUE;
6874}
6875
8059fb19 6876#undef TARGET_LITTLE_SYM
6d00b590 6877#define TARGET_LITTLE_SYM x86_64_elf64_nacl_vec
8059fb19
RM
6878#undef TARGET_LITTLE_NAME
6879#define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
6880#undef elf64_bed
6881#define elf64_bed elf64_x86_64_nacl_bed
6882
6883#undef ELF_MAXPAGESIZE
6884#undef ELF_MINPAGESIZE
6885#undef ELF_COMMONPAGESIZE
6886#define ELF_MAXPAGESIZE 0x10000
6887#define ELF_MINPAGESIZE 0x10000
6888#define ELF_COMMONPAGESIZE 0x10000
6889
6890/* Restore defaults. */
6891#undef ELF_OSABI
6892#undef elf_backend_static_tls_alignment
6893#undef elf_backend_want_plt_sym
6894#define elf_backend_want_plt_sym 0
84865015 6895#undef elf_backend_strtab_flags
5522f910 6896#undef elf_backend_copy_special_section_fields
8059fb19
RM
6897
6898/* NaCl uses substantially different PLT entries for the same effects. */
6899
6900#undef elf_backend_plt_alignment
6901#define elf_backend_plt_alignment 5
6902#define NACL_PLT_ENTRY_SIZE 64
6903#define NACLMASK 0xe0 /* 32-byte alignment mask. */
6904
6905static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
6906 {
6907 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
6908 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
6909 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
6910 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
6911 0x41, 0xff, 0xe3, /* jmpq *%r11 */
6912
ea2d813e 6913 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
70cc877f 6914 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1) */
ea2d813e
RM
6915
6916 /* 32 bytes of nop to pad out to the standard size. */
3ddf1bdd 6917 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19 6918 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
3ddf1bdd 6919 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19 6920 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
3ddf1bdd 6921 0x66, /* excess data16 prefix */
ea2d813e 6922 0x90 /* nop */
8059fb19
RM
6923 };
6924
6925static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
6926 {
6927 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
6928 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
6929 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
6930 0x41, 0xff, 0xe3, /* jmpq *%r11 */
6931
6932 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
3ddf1bdd 6933 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19
RM
6934 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
6935
6936 /* Lazy GOT entries point here (32-byte aligned). */
6937 0x68, /* pushq immediate */
6938 0, 0, 0, 0, /* replaced with index into relocation table. */
6939 0xe9, /* jmp relative */
6940 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
6941
6942 /* 22 bytes of nop to pad out to the standard size. */
3ddf1bdd 6943 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
8059fb19
RM
6944 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
6945 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
6946 };
6947
6948/* .eh_frame covering the .plt section. */
6949
6950static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
6951 {
6952#if (PLT_CIE_LENGTH != 20 \
6953 || PLT_FDE_LENGTH != 36 \
6954 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
6955 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
6956# error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
6957#endif
6958 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
6959 0, 0, 0, 0, /* CIE ID */
6960 1, /* CIE version */
6961 'z', 'R', 0, /* Augmentation string */
6962 1, /* Code alignment factor */
6963 0x78, /* Data alignment factor */
6964 16, /* Return address column */
6965 1, /* Augmentation size */
6966 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
6967 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
6968 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
6969 DW_CFA_nop, DW_CFA_nop,
6970
6971 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
6972 PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
6973 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
6974 0, 0, 0, 0, /* .plt size goes here */
6975 0, /* Augmentation size */
6976 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
6977 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
6978 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
6979 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
6980 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
6981 13, /* Block length */
6982 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
6983 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
6984 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
6985 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
6986 DW_CFA_nop, DW_CFA_nop
6987 };
6988
6989static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
6990 {
6991 elf_x86_64_nacl_plt0_entry, /* plt0_entry */
6992 elf_x86_64_nacl_plt_entry, /* plt_entry */
6993 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
6994 2, /* plt0_got1_offset */
6995 9, /* plt0_got2_offset */
6996 13, /* plt0_got2_insn_end */
6997 3, /* plt_got_offset */
6998 33, /* plt_reloc_offset */
6999 38, /* plt_plt_offset */
7000 7, /* plt_got_insn_size */
7001 42, /* plt_plt_insn_end */
7002 32, /* plt_lazy_offset */
7003 elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
7004 sizeof (elf_x86_64_nacl_eh_frame_plt), /* eh_frame_plt_size */
7005 };
7006
7007#undef elf_backend_arch_data
7008#define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
7009
64b384e1
RM
7010#undef elf_backend_object_p
7011#define elf_backend_object_p elf64_x86_64_nacl_elf_object_p
5a68afcf
RM
7012#undef elf_backend_modify_segment_map
7013#define elf_backend_modify_segment_map nacl_modify_segment_map
7014#undef elf_backend_modify_program_headers
7015#define elf_backend_modify_program_headers nacl_modify_program_headers
887badb3
RM
7016#undef elf_backend_final_write_processing
7017#define elf_backend_final_write_processing nacl_final_write_processing
5a68afcf 7018
8059fb19
RM
7019#include "elf64-target.h"
7020
7021/* Native Client x32 support. */
7022
64b384e1
RM
7023static bfd_boolean
7024elf32_x86_64_nacl_elf_object_p (bfd *abfd)
7025{
7026 /* Set the right machine number for a NaCl x86-64 ELF32 file. */
7027 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
7028 return TRUE;
7029}
7030
8059fb19 7031#undef TARGET_LITTLE_SYM
6d00b590 7032#define TARGET_LITTLE_SYM x86_64_elf32_nacl_vec
8059fb19
RM
7033#undef TARGET_LITTLE_NAME
7034#define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
7035#undef elf32_bed
7036#define elf32_bed elf32_x86_64_nacl_bed
7037
7038#define bfd_elf32_bfd_link_hash_table_create \
7039 elf_x86_64_link_hash_table_create
8059fb19
RM
7040#define bfd_elf32_bfd_reloc_type_lookup \
7041 elf_x86_64_reloc_type_lookup
7042#define bfd_elf32_bfd_reloc_name_lookup \
7043 elf_x86_64_reloc_name_lookup
7044#define bfd_elf32_mkobject \
7045 elf_x86_64_mkobject
b7365e5d
L
7046#define bfd_elf32_get_synthetic_symtab \
7047 elf_x86_64_get_synthetic_symtab
8059fb19
RM
7048
7049#undef elf_backend_object_p
7050#define elf_backend_object_p \
64b384e1 7051 elf32_x86_64_nacl_elf_object_p
8059fb19
RM
7052
7053#undef elf_backend_bfd_from_remote_memory
7054#define elf_backend_bfd_from_remote_memory \
7055 _bfd_elf32_bfd_from_remote_memory
7056
7057#undef elf_backend_size_info
7058#define elf_backend_size_info \
7059 _bfd_elf32_size_info
7060
7061#include "elf32-target.h"
7062
7063/* Restore defaults. */
5a68afcf 7064#undef elf_backend_object_p
8059fb19 7065#define elf_backend_object_p elf64_x86_64_elf_object_p
5a68afcf
RM
7066#undef elf_backend_bfd_from_remote_memory
7067#undef elf_backend_size_info
7068#undef elf_backend_modify_segment_map
7069#undef elf_backend_modify_program_headers
887badb3 7070#undef elf_backend_final_write_processing
8059fb19 7071
8a9036a4
L
7072/* Intel L1OM support. */
7073
7074static bfd_boolean
7075elf64_l1om_elf_object_p (bfd *abfd)
7076{
7077 /* Set the right machine number for an L1OM elf64 file. */
7078 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
7079 return TRUE;
7080}
7081
7082#undef TARGET_LITTLE_SYM
6d00b590 7083#define TARGET_LITTLE_SYM l1om_elf64_vec
8a9036a4
L
7084#undef TARGET_LITTLE_NAME
7085#define TARGET_LITTLE_NAME "elf64-l1om"
7086#undef ELF_ARCH
7087#define ELF_ARCH bfd_arch_l1om
7088
7089#undef ELF_MACHINE_CODE
7090#define ELF_MACHINE_CODE EM_L1OM
7091
7092#undef ELF_OSABI
7093
7094#undef elf64_bed
7095#define elf64_bed elf64_l1om_bed
7096
7097#undef elf_backend_object_p
7098#define elf_backend_object_p elf64_l1om_elf_object_p
7099
8059fb19
RM
7100/* Restore defaults. */
7101#undef ELF_MAXPAGESIZE
7102#undef ELF_MINPAGESIZE
7103#undef ELF_COMMONPAGESIZE
7104#define ELF_MAXPAGESIZE 0x200000
7105#define ELF_MINPAGESIZE 0x1000
7106#define ELF_COMMONPAGESIZE 0x1000
7107#undef elf_backend_plt_alignment
7108#define elf_backend_plt_alignment 4
7109#undef elf_backend_arch_data
7110#define elf_backend_arch_data &elf_x86_64_arch_bed
1a0c107f 7111
8a9036a4
L
7112#include "elf64-target.h"
7113
7114/* FreeBSD L1OM support. */
7115
7116#undef TARGET_LITTLE_SYM
6d00b590 7117#define TARGET_LITTLE_SYM l1om_elf64_fbsd_vec
8a9036a4
L
7118#undef TARGET_LITTLE_NAME
7119#define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
7120
7121#undef ELF_OSABI
7122#define ELF_OSABI ELFOSABI_FREEBSD
7123
7124#undef elf64_bed
7125#define elf64_bed elf64_l1om_fbsd_bed
7126
8a9036a4 7127#include "elf64-target.h"
351f65ca 7128
7a9068fe
L
7129/* Intel K1OM support. */
7130
7131static bfd_boolean
7132elf64_k1om_elf_object_p (bfd *abfd)
7133{
7134 /* Set the right machine number for an K1OM elf64 file. */
7135 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
7136 return TRUE;
7137}
7138
7139#undef TARGET_LITTLE_SYM
6d00b590 7140#define TARGET_LITTLE_SYM k1om_elf64_vec
7a9068fe
L
7141#undef TARGET_LITTLE_NAME
7142#define TARGET_LITTLE_NAME "elf64-k1om"
7143#undef ELF_ARCH
7144#define ELF_ARCH bfd_arch_k1om
7145
7146#undef ELF_MACHINE_CODE
7147#define ELF_MACHINE_CODE EM_K1OM
7148
7149#undef ELF_OSABI
7150
7151#undef elf64_bed
7152#define elf64_bed elf64_k1om_bed
7153
7154#undef elf_backend_object_p
7155#define elf_backend_object_p elf64_k1om_elf_object_p
7156
7157#undef elf_backend_static_tls_alignment
7158
7159#undef elf_backend_want_plt_sym
7160#define elf_backend_want_plt_sym 0
7161
7162#include "elf64-target.h"
7163
7164/* FreeBSD K1OM support. */
7165
7166#undef TARGET_LITTLE_SYM
6d00b590 7167#define TARGET_LITTLE_SYM k1om_elf64_fbsd_vec
7a9068fe
L
7168#undef TARGET_LITTLE_NAME
7169#define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
7170
7171#undef ELF_OSABI
7172#define ELF_OSABI ELFOSABI_FREEBSD
7173
7174#undef elf64_bed
7175#define elf64_bed elf64_k1om_fbsd_bed
7176
7177#include "elf64-target.h"
7178
351f65ca
L
7179/* 32bit x86-64 support. */
7180
351f65ca 7181#undef TARGET_LITTLE_SYM
6d00b590 7182#define TARGET_LITTLE_SYM x86_64_elf32_vec
351f65ca
L
7183#undef TARGET_LITTLE_NAME
7184#define TARGET_LITTLE_NAME "elf32-x86-64"
8059fb19 7185#undef elf32_bed
351f65ca
L
7186
7187#undef ELF_ARCH
7188#define ELF_ARCH bfd_arch_i386
7189
7190#undef ELF_MACHINE_CODE
7191#define ELF_MACHINE_CODE EM_X86_64
7192
351f65ca
L
7193#undef ELF_OSABI
7194
351f65ca
L
7195#undef elf_backend_object_p
7196#define elf_backend_object_p \
7197 elf32_x86_64_elf_object_p
7198
7199#undef elf_backend_bfd_from_remote_memory
7200#define elf_backend_bfd_from_remote_memory \
7201 _bfd_elf32_bfd_from_remote_memory
7202
7203#undef elf_backend_size_info
7204#define elf_backend_size_info \
7205 _bfd_elf32_size_info
7206
7207#include "elf32-target.h"