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