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