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