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