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