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