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