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