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