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