]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-x86-64.c
bfd/elf-ifunc.c: Remove unused function _bfd_elf_create_ifunc_dyn_reloc.
[thirdparty/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
351f65ca 1/* X86-64 specific support for ELF
4dfe6ac6 2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
61315175 3 2010, 2011, 2012, 2013
9f973f28 4 Free Software Foundation, Inc.
8d88c4ca
NC
5 Contributed by Jan Hubicka <jh@suse.cz>.
6
ae9a127f 7 This file is part of BFD, the Binary File Descriptor library.
8d88c4ca 8
ae9a127f
NC
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
cd123cb7 11 the Free Software Foundation; either version 3 of the License, or
ae9a127f 12 (at your option) any later version.
8d88c4ca 13
ae9a127f
NC
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
8d88c4ca 18
ae9a127f
NC
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
cd123cb7
NC
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
8d88c4ca 23
8d88c4ca 24#include "sysdep.h"
3db64b00 25#include "bfd.h"
c434dee6 26#include "bfdlink.h"
8d88c4ca
NC
27#include "libbfd.h"
28#include "elf-bfd.h"
5a68afcf 29#include "elf-nacl.h"
142411ca 30#include "bfd_stdint.h"
c25bc9fc
L
31#include "objalloc.h"
32#include "hashtab.h"
e41b3a13 33#include "dwarf2.h"
d7921315 34#include "libiberty.h"
8d88c4ca
NC
35
36#include "elf/x86-64.h"
37
8fd79e71
L
38#ifdef CORE_HEADER
39#include <stdarg.h>
40#include CORE_HEADER
41#endif
42
8d88c4ca
NC
43/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
44#define MINUS_ONE (~ (bfd_vma) 0)
45
351f65ca
L
46/* Since both 32-bit and 64-bit x86-64 encode relocation type in the
47 identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
48 relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
49 since they are the same. */
50
51#define ABI_64_P(abfd) \
52 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
53
8d88c4ca 54/* The relocation "howto" table. Order of fields:
7b81dfbb
AJ
55 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
56 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
70256ad8
AJ
57static reloc_howto_type x86_64_elf_howto_table[] =
58{
b34976b6
AM
59 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
60 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
61 FALSE),
62 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
64 FALSE),
65 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
66 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
67 TRUE),
68 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
69 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
70 FALSE),
71 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
72 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
73 TRUE),
74 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
75 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
76 FALSE),
77 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
78 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
79 MINUS_ONE, FALSE),
80 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
82 MINUS_ONE, FALSE),
83 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
84 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
85 MINUS_ONE, FALSE),
86 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
87 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
88 0xffffffff, TRUE),
89 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
90 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
91 FALSE),
92 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
93 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
94 FALSE),
95 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
96 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
b0360d8c 97 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
b34976b6 98 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
ac2aa337 99 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
b34976b6
AM
100 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
101 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
102 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
103 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
104 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
105 MINUS_ONE, FALSE),
106 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
107 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
108 MINUS_ONE, FALSE),
109 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
110 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
111 MINUS_ONE, FALSE),
112 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
113 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
114 0xffffffff, TRUE),
115 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
116 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
117 0xffffffff, TRUE),
ac2aa337 118 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
b34976b6
AM
119 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
120 0xffffffff, FALSE),
121 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
122 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
123 0xffffffff, TRUE),
124 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
125 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
126 0xffffffff, FALSE),
d6ab8113
JB
127 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
128 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
129 TRUE),
130 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
131 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
132 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
133 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
134 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
135 FALSE, 0xffffffff, 0xffffffff, TRUE),
7b81dfbb
AJ
136 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
137 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
138 FALSE),
139 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
140 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
141 MINUS_ONE, TRUE),
142 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
143 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
144 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
145 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
146 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
147 MINUS_ONE, FALSE),
148 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
149 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
150 MINUS_ONE, FALSE),
1788fc08
L
151 HOWTO(R_X86_64_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
152 bfd_elf_generic_reloc, "R_X86_64_SIZE32", FALSE, 0xffffffff, 0xffffffff,
153 FALSE),
154 HOWTO(R_X86_64_SIZE64, 0, 4, 64, FALSE, 0, complain_overflow_unsigned,
155 bfd_elf_generic_reloc, "R_X86_64_SIZE64", FALSE, MINUS_ONE, MINUS_ONE,
156 FALSE),
67a4f2b7
AO
157 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
158 complain_overflow_bitfield, bfd_elf_generic_reloc,
159 "R_X86_64_GOTPC32_TLSDESC",
160 FALSE, 0xffffffff, 0xffffffff, TRUE),
161 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
162 complain_overflow_dont, bfd_elf_generic_reloc,
163 "R_X86_64_TLSDESC_CALL",
164 FALSE, 0, 0, FALSE),
165 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
166 complain_overflow_bitfield, bfd_elf_generic_reloc,
167 "R_X86_64_TLSDESC",
168 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
cbe950e9
L
169 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
170 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
171 MINUS_ONE, FALSE),
64d25c44
L
172 HOWTO(R_X86_64_RELATIVE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
173 bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", FALSE, MINUS_ONE,
174 MINUS_ONE, FALSE),
fe4770f4 175
a33d77bc
JB
176 /* We have a gap in the reloc numbers here.
177 R_X86_64_standard counts the number up to this point, and
178 R_X86_64_vt_offset is the value to subtract from a reloc type of
179 R_X86_64_GNU_VT* to form an index into this table. */
1e536451 180#define R_X86_64_standard (R_X86_64_RELATIVE64 + 1)
a33d77bc
JB
181#define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
182
fe4770f4 183/* GNU extension to record C++ vtable hierarchy. */
b34976b6
AM
184 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
185 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
fe4770f4
AJ
186
187/* GNU extension to record C++ vtable member usage. */
b34976b6
AM
188 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
189 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
d7921315
L
190 FALSE),
191
192/* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
193 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
194 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
195 FALSE)
8d88c4ca
NC
196};
197
d8045f23
NC
198#define IS_X86_64_PCREL_TYPE(TYPE) \
199 ( ((TYPE) == R_X86_64_PC8) \
200 || ((TYPE) == R_X86_64_PC16) \
201 || ((TYPE) == R_X86_64_PC32) \
202 || ((TYPE) == R_X86_64_PC64))
203
8d88c4ca 204/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
205struct elf_reloc_map
206{
8d88c4ca
NC
207 bfd_reloc_code_real_type bfd_reloc_val;
208 unsigned char elf_reloc_val;
209};
210
dc810e39 211static const struct elf_reloc_map x86_64_reloc_map[] =
8d88c4ca 212{
70256ad8
AJ
213 { BFD_RELOC_NONE, R_X86_64_NONE, },
214 { BFD_RELOC_64, R_X86_64_64, },
215 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
216 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
217 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
218 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
219 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
220 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
221 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
222 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
223 { BFD_RELOC_32, R_X86_64_32, },
224 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
225 { BFD_RELOC_16, R_X86_64_16, },
226 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
227 { BFD_RELOC_8, R_X86_64_8, },
228 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
bffbf940
JJ
229 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
230 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
231 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
232 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
233 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
234 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
235 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
236 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
d6ab8113
JB
237 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
238 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
239 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
7b81dfbb
AJ
240 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
241 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
242 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
243 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
244 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
1788fc08
L
245 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
246 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
67a4f2b7
AO
247 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
248 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
249 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
cbe950e9 250 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
fe4770f4
AJ
251 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
252 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
253};
254
67a4f2b7 255static reloc_howto_type *
351f65ca 256elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
67a4f2b7
AO
257{
258 unsigned i;
259
d7921315
L
260 if (r_type == (unsigned int) R_X86_64_32)
261 {
262 if (ABI_64_P (abfd))
263 i = r_type;
264 else
265 i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
266 }
267 else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
268 || r_type >= (unsigned int) R_X86_64_max)
67a4f2b7
AO
269 {
270 if (r_type >= (unsigned int) R_X86_64_standard)
271 {
272 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
273 abfd, (int) r_type);
274 r_type = R_X86_64_NONE;
275 }
276 i = r_type;
277 }
278 else
279 i = r_type - (unsigned int) R_X86_64_vt_offset;
280 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
281 return &x86_64_elf_howto_table[i];
282}
8d88c4ca
NC
283
284/* Given a BFD reloc type, return a HOWTO structure. */
285static reloc_howto_type *
351f65ca
L
286elf_x86_64_reloc_type_lookup (bfd *abfd,
287 bfd_reloc_code_real_type code)
8d88c4ca
NC
288{
289 unsigned int i;
27482721 290
8d88c4ca
NC
291 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
292 i++)
293 {
294 if (x86_64_reloc_map[i].bfd_reloc_val == code)
351f65ca
L
295 return elf_x86_64_rtype_to_howto (abfd,
296 x86_64_reloc_map[i].elf_reloc_val);
8d88c4ca
NC
297 }
298 return 0;
299}
300
157090f7 301static reloc_howto_type *
d7921315 302elf_x86_64_reloc_name_lookup (bfd *abfd,
351f65ca 303 const char *r_name)
157090f7
AM
304{
305 unsigned int i;
306
d7921315
L
307 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
308 {
309 /* Get x32 R_X86_64_32. */
310 reloc_howto_type *reloc
311 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
312 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
313 return reloc;
314 }
315
316 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
157090f7
AM
317 if (x86_64_elf_howto_table[i].name != NULL
318 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
319 return &x86_64_elf_howto_table[i];
320
321 return NULL;
322}
323
8d88c4ca 324/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 325
8d88c4ca 326static void
351f65ca
L
327elf_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
328 Elf_Internal_Rela *dst)
8d88c4ca 329{
67a4f2b7 330 unsigned r_type;
8d88c4ca 331
351f65ca
L
332 r_type = ELF32_R_TYPE (dst->r_info);
333 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
8d88c4ca
NC
334 BFD_ASSERT (r_type == cache_ptr->howto->type);
335}
70256ad8 336\f
3bab7989 337/* Support for core dump NOTE sections. */
b34976b6 338static bfd_boolean
351f65ca 339elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
340{
341 int offset;
eea6121a 342 size_t size;
3bab7989
ML
343
344 switch (note->descsz)
345 {
346 default:
b34976b6 347 return FALSE;
3bab7989 348
bcd823f1
L
349 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
350 /* pr_cursig */
228e534f 351 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
bcd823f1
L
352
353 /* pr_pid */
228e534f 354 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
bcd823f1
L
355
356 /* pr_reg */
357 offset = 72;
358 size = 216;
359
360 break;
361
3bab7989
ML
362 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
363 /* pr_cursig */
228e534f 364 elf_tdata (abfd)->core->signal
3bab7989
ML
365 = bfd_get_16 (abfd, note->descdata + 12);
366
367 /* pr_pid */
228e534f 368 elf_tdata (abfd)->core->lwpid
3bab7989
ML
369 = bfd_get_32 (abfd, note->descdata + 32);
370
371 /* pr_reg */
372 offset = 112;
eea6121a 373 size = 216;
3bab7989
ML
374
375 break;
376 }
377
378 /* Make a ".reg/999" section. */
379 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 380 size, note->descpos + offset);
3bab7989
ML
381}
382
b34976b6 383static bfd_boolean
351f65ca 384elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
385{
386 switch (note->descsz)
387 {
388 default:
b34976b6 389 return FALSE;
3bab7989 390
bcd823f1 391 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
228e534f 392 elf_tdata (abfd)->core->pid
bcd823f1 393 = bfd_get_32 (abfd, note->descdata + 12);
228e534f 394 elf_tdata (abfd)->core->program
bcd823f1 395 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
228e534f 396 elf_tdata (abfd)->core->command
bcd823f1
L
397 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
398 break;
399
3bab7989 400 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
228e534f 401 elf_tdata (abfd)->core->pid
261b8d08 402 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 403 elf_tdata (abfd)->core->program
3bab7989 404 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 405 elf_tdata (abfd)->core->command
3bab7989
ML
406 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
407 }
408
409 /* Note that for some reason, a spurious space is tacked
410 onto the end of the args in some (at least one anyway)
411 implementations, so strip it off if it exists. */
412
413 {
228e534f 414 char *command = elf_tdata (abfd)->core->command;
3bab7989
ML
415 int n = strlen (command);
416
417 if (0 < n && command[n - 1] == ' ')
418 command[n - 1] = '\0';
419 }
420
b34976b6 421 return TRUE;
3bab7989 422}
8fd79e71
L
423
424#ifdef CORE_HEADER
425static char *
426elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
427 int note_type, ...)
428{
429 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8fd79e71
L
430 va_list ap;
431 const char *fname, *psargs;
432 long pid;
433 int cursig;
434 const void *gregs;
435
436 switch (note_type)
437 {
438 default:
439 return NULL;
440
441 case NT_PRPSINFO:
442 va_start (ap, note_type);
443 fname = va_arg (ap, const char *);
444 psargs = va_arg (ap, const char *);
445 va_end (ap);
446
447 if (bed->s->elfclass == ELFCLASS32)
448 {
449 prpsinfo32_t data;
450 memset (&data, 0, sizeof (data));
451 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
452 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
e85c6a70
JK
453 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
454 &data, sizeof (data));
8fd79e71
L
455 }
456 else
457 {
b1bd052d 458 prpsinfo64_t data;
8fd79e71
L
459 memset (&data, 0, sizeof (data));
460 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
461 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
e85c6a70
JK
462 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
463 &data, sizeof (data));
8fd79e71 464 }
e85c6a70 465 /* NOTREACHED */
8fd79e71
L
466
467 case NT_PRSTATUS:
468 va_start (ap, note_type);
469 pid = va_arg (ap, long);
470 cursig = va_arg (ap, int);
471 gregs = va_arg (ap, const void *);
472 va_end (ap);
473
474 if (bed->s->elfclass == ELFCLASS32)
475 {
476 if (bed->elf_machine_code == EM_X86_64)
477 {
478 prstatusx32_t prstat;
479 memset (&prstat, 0, sizeof (prstat));
480 prstat.pr_pid = pid;
481 prstat.pr_cursig = cursig;
482 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
483 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
484 &prstat, sizeof (prstat));
8fd79e71
L
485 }
486 else
487 {
488 prstatus32_t prstat;
489 memset (&prstat, 0, sizeof (prstat));
490 prstat.pr_pid = pid;
491 prstat.pr_cursig = cursig;
492 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
493 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
494 &prstat, sizeof (prstat));
8fd79e71
L
495 }
496 }
497 else
498 {
b1bd052d 499 prstatus64_t prstat;
8fd79e71
L
500 memset (&prstat, 0, sizeof (prstat));
501 prstat.pr_pid = pid;
502 prstat.pr_cursig = cursig;
503 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
e85c6a70
JK
504 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
505 &prstat, sizeof (prstat));
8fd79e71 506 }
8fd79e71 507 }
e85c6a70 508 /* NOTREACHED */
8fd79e71
L
509}
510#endif
3bab7989 511\f
407443a3 512/* Functions for the x86-64 ELF linker. */
70256ad8 513
407443a3 514/* The name of the dynamic interpreter. This is put in the .interp
70256ad8
AJ
515 section. */
516
351f65ca 517#define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
eec9dd95 518#define ELF32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
70256ad8 519
d40d037c
AJ
520/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
521 copying dynamic variables from a shared lib into an app's dynbss
522 section, and instead use a dynamic relocation to point into the
523 shared lib. */
524#define ELIMINATE_COPY_RELOCS 1
525
70256ad8
AJ
526/* The size in bytes of an entry in the global offset table. */
527
528#define GOT_ENTRY_SIZE 8
8d88c4ca 529
70256ad8 530/* The size in bytes of an entry in the procedure linkage table. */
8d88c4ca 531
70256ad8
AJ
532#define PLT_ENTRY_SIZE 16
533
534/* The first entry in a procedure linkage table looks like this. See the
535 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
536
351f65ca 537static const bfd_byte elf_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
70256ad8 538{
653165cc
AJ
539 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
540 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
10efb593 541 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
70256ad8
AJ
542};
543
544/* Subsequent entries in a procedure linkage table look like this. */
545
351f65ca 546static const bfd_byte elf_x86_64_plt_entry[PLT_ENTRY_SIZE] =
70256ad8 547{
653165cc 548 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 549 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 550 0x68, /* pushq immediate */
70256ad8
AJ
551 0, 0, 0, 0, /* replaced with index into relocation table. */
552 0xe9, /* jmp relative */
553 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
554};
555
e41b3a13
JJ
556/* .eh_frame covering the .plt section. */
557
558static const bfd_byte elf_x86_64_eh_frame_plt[] =
559{
560#define PLT_CIE_LENGTH 20
561#define PLT_FDE_LENGTH 36
562#define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
563#define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
564 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
565 0, 0, 0, 0, /* CIE ID */
566 1, /* CIE version */
567 'z', 'R', 0, /* Augmentation string */
568 1, /* Code alignment factor */
569 0x78, /* Data alignment factor */
570 16, /* Return address column */
571 1, /* Augmentation size */
572 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
573 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
574 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
575 DW_CFA_nop, DW_CFA_nop,
576
577 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
578 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
579 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
580 0, 0, 0, 0, /* .plt size goes here */
581 0, /* Augmentation size */
582 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
583 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
584 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
585 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
586 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
587 11, /* Block length */
588 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
589 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
590 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
591 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
592 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
593};
594
eed180f8
RM
595/* Architecture-specific backend data for x86-64. */
596
597struct elf_x86_64_backend_data
598{
599 /* Templates for the initial PLT entry and for subsequent entries. */
600 const bfd_byte *plt0_entry;
601 const bfd_byte *plt_entry;
602 unsigned int plt_entry_size; /* Size of each PLT entry. */
603
604 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
605 unsigned int plt0_got1_offset;
606 unsigned int plt0_got2_offset;
607
608 /* Offset of the end of the PC-relative instruction containing
609 plt0_got2_offset. */
610 unsigned int plt0_got2_insn_end;
611
612 /* Offsets into plt_entry that are to be replaced with... */
613 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
614 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
615 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
616
617 /* Length of the PC-relative instruction containing plt_got_offset. */
618 unsigned int plt_got_insn_size;
619
620 /* Offset of the end of the PC-relative jump to plt0_entry. */
621 unsigned int plt_plt_insn_end;
622
623 /* Offset into plt_entry where the initial value of the GOT entry points. */
624 unsigned int plt_lazy_offset;
625
626 /* .eh_frame covering the .plt section. */
627 const bfd_byte *eh_frame_plt;
628 unsigned int eh_frame_plt_size;
629};
630
631#define get_elf_x86_64_backend_data(abfd) \
632 ((const struct elf_x86_64_backend_data *) \
633 get_elf_backend_data (abfd)->arch_data)
634
635#define GET_PLT_ENTRY_SIZE(abfd) \
636 get_elf_x86_64_backend_data (abfd)->plt_entry_size
637
638/* These are the standard parameters. */
639static const struct elf_x86_64_backend_data elf_x86_64_arch_bed =
640 {
641 elf_x86_64_plt0_entry, /* plt0_entry */
642 elf_x86_64_plt_entry, /* plt_entry */
643 sizeof (elf_x86_64_plt_entry), /* plt_entry_size */
644 2, /* plt0_got1_offset */
645 8, /* plt0_got2_offset */
646 12, /* plt0_got2_insn_end */
647 2, /* plt_got_offset */
648 7, /* plt_reloc_offset */
649 12, /* plt_plt_offset */
650 6, /* plt_got_insn_size */
651 PLT_ENTRY_SIZE, /* plt_plt_insn_end */
652 6, /* plt_lazy_offset */
653 elf_x86_64_eh_frame_plt, /* eh_frame_plt */
654 sizeof (elf_x86_64_eh_frame_plt), /* eh_frame_plt_size */
655 };
656
657#define elf_backend_arch_data &elf_x86_64_arch_bed
658
70256ad8
AJ
659/* x86-64 ELF linker hash entry. */
660
351f65ca 661struct elf_x86_64_link_hash_entry
70256ad8 662{
c434dee6 663 struct elf_link_hash_entry elf;
70256ad8 664
c434dee6 665 /* Track dynamic relocs copied for this symbol. */
e03a8ed8 666 struct elf_dyn_relocs *dyn_relocs;
bffbf940
JJ
667
668#define GOT_UNKNOWN 0
669#define GOT_NORMAL 1
670#define GOT_TLS_GD 2
671#define GOT_TLS_IE 3
67a4f2b7
AO
672#define GOT_TLS_GDESC 4
673#define GOT_TLS_GD_BOTH_P(type) \
674 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
675#define GOT_TLS_GD_P(type) \
676 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
677#define GOT_TLS_GDESC_P(type) \
678 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
679#define GOT_TLS_GD_ANY_P(type) \
680 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
bffbf940 681 unsigned char tls_type;
67a4f2b7
AO
682
683 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
684 starting at the end of the jump table. */
685 bfd_vma tlsdesc_got;
bffbf940
JJ
686};
687
351f65ca
L
688#define elf_x86_64_hash_entry(ent) \
689 ((struct elf_x86_64_link_hash_entry *)(ent))
bffbf940 690
351f65ca 691struct elf_x86_64_obj_tdata
bffbf940
JJ
692{
693 struct elf_obj_tdata root;
694
695 /* tls_type for each local got entry. */
696 char *local_got_tls_type;
67a4f2b7
AO
697
698 /* GOTPLT entries for TLS descriptors. */
699 bfd_vma *local_tlsdesc_gotent;
70256ad8
AJ
700};
701
351f65ca
L
702#define elf_x86_64_tdata(abfd) \
703 ((struct elf_x86_64_obj_tdata *) (abfd)->tdata.any)
bffbf940 704
351f65ca
L
705#define elf_x86_64_local_got_tls_type(abfd) \
706 (elf_x86_64_tdata (abfd)->local_got_tls_type)
bffbf940 707
351f65ca
L
708#define elf_x86_64_local_tlsdesc_gotent(abfd) \
709 (elf_x86_64_tdata (abfd)->local_tlsdesc_gotent)
bffbf940 710
0ffa91dd
NC
711#define is_x86_64_elf(bfd) \
712 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
713 && elf_tdata (bfd) != NULL \
4dfe6ac6 714 && elf_object_id (bfd) == X86_64_ELF_DATA)
0ffa91dd
NC
715
716static bfd_boolean
351f65ca 717elf_x86_64_mkobject (bfd *abfd)
0ffa91dd 718{
351f65ca 719 return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_64_obj_tdata),
4dfe6ac6 720 X86_64_ELF_DATA);
0ffa91dd
NC
721}
722
c434dee6 723/* x86-64 ELF linker hash table. */
8d88c4ca 724
351f65ca 725struct elf_x86_64_link_hash_table
407443a3 726{
c434dee6 727 struct elf_link_hash_table elf;
70256ad8 728
c434dee6 729 /* Short-cuts to get to dynamic linker sections. */
c434dee6
AJ
730 asection *sdynbss;
731 asection *srelbss;
e41b3a13 732 asection *plt_eh_frame;
70256ad8 733
4dfe6ac6
NC
734 union
735 {
bffbf940
JJ
736 bfd_signed_vma refcount;
737 bfd_vma offset;
738 } tls_ld_got;
739
67a4f2b7
AO
740 /* The amount of space used by the jump slots in the GOT. */
741 bfd_vma sgotplt_jump_table_size;
742
87d72d41
AM
743 /* Small local sym cache. */
744 struct sym_cache sym_cache;
9f03412a 745
351f65ca
L
746 bfd_vma (*r_info) (bfd_vma, bfd_vma);
747 bfd_vma (*r_sym) (bfd_vma);
248775ba 748 unsigned int pointer_r_type;
351f65ca
L
749 const char *dynamic_interpreter;
750 int dynamic_interpreter_size;
751
9f03412a
AO
752 /* _TLS_MODULE_BASE_ symbol. */
753 struct bfd_link_hash_entry *tls_module_base;
c25bc9fc
L
754
755 /* Used by local STT_GNU_IFUNC symbols. */
756 htab_t loc_hash_table;
4dfe6ac6
NC
757 void * loc_hash_memory;
758
759 /* The offset into splt of the PLT entry for the TLS descriptor
760 resolver. Special values are 0, if not necessary (or not found
761 to be necessary yet), and -1 if needed but not determined
762 yet. */
763 bfd_vma tlsdesc_plt;
764 /* The offset into sgot of the GOT entry used by the PLT entry
765 above. */
766 bfd_vma tlsdesc_got;
e1f98742
L
767
768 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
769 bfd_vma next_jump_slot_index;
770 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
771 bfd_vma next_irelative_index;
c434dee6 772};
70256ad8
AJ
773
774/* Get the x86-64 ELF linker hash table from a link_info structure. */
8d88c4ca 775
351f65ca 776#define elf_x86_64_hash_table(p) \
4dfe6ac6 777 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
351f65ca 778 == X86_64_ELF_DATA ? ((struct elf_x86_64_link_hash_table *) ((p)->hash)) : NULL)
8d88c4ca 779
351f65ca 780#define elf_x86_64_compute_jump_table_size(htab) \
6de2ae4a 781 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
67a4f2b7 782
407443a3 783/* Create an entry in an x86-64 ELF linker hash table. */
70256ad8
AJ
784
785static struct bfd_hash_entry *
351f65ca
L
786elf_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
787 struct bfd_hash_table *table,
788 const char *string)
70256ad8 789{
70256ad8 790 /* Allocate the structure if it has not already been allocated by a
c434dee6
AJ
791 subclass. */
792 if (entry == NULL)
793 {
a50b1753 794 entry = (struct bfd_hash_entry *)
eed180f8
RM
795 bfd_hash_allocate (table,
796 sizeof (struct elf_x86_64_link_hash_entry));
c434dee6
AJ
797 if (entry == NULL)
798 return entry;
799 }
70256ad8
AJ
800
801 /* Call the allocation method of the superclass. */
c434dee6
AJ
802 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
803 if (entry != NULL)
70256ad8 804 {
351f65ca 805 struct elf_x86_64_link_hash_entry *eh;
c434dee6 806
351f65ca 807 eh = (struct elf_x86_64_link_hash_entry *) entry;
c434dee6 808 eh->dyn_relocs = NULL;
bffbf940 809 eh->tls_type = GOT_UNKNOWN;
67a4f2b7 810 eh->tlsdesc_got = (bfd_vma) -1;
70256ad8
AJ
811 }
812
c434dee6 813 return entry;
70256ad8
AJ
814}
815
c25bc9fc
L
816/* Compute a hash of a local hash entry. We use elf_link_hash_entry
817 for local symbol so that we can handle local STT_GNU_IFUNC symbols
818 as global symbol. We reuse indx and dynstr_index for local symbol
819 hash since they aren't used by global symbols in this backend. */
820
821static hashval_t
351f65ca 822elf_x86_64_local_htab_hash (const void *ptr)
c25bc9fc
L
823{
824 struct elf_link_hash_entry *h
825 = (struct elf_link_hash_entry *) ptr;
d2149d72 826 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
c25bc9fc
L
827}
828
829/* Compare local hash entries. */
830
831static int
351f65ca 832elf_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
c25bc9fc
L
833{
834 struct elf_link_hash_entry *h1
835 = (struct elf_link_hash_entry *) ptr1;
836 struct elf_link_hash_entry *h2
837 = (struct elf_link_hash_entry *) ptr2;
838
839 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
840}
841
842/* Find and/or create a hash entry for local symbol. */
843
844static struct elf_link_hash_entry *
351f65ca
L
845elf_x86_64_get_local_sym_hash (struct elf_x86_64_link_hash_table *htab,
846 bfd *abfd, const Elf_Internal_Rela *rel,
847 bfd_boolean create)
c25bc9fc 848{
351f65ca 849 struct elf_x86_64_link_hash_entry e, *ret;
c25bc9fc 850 asection *sec = abfd->sections;
d2149d72 851 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
351f65ca 852 htab->r_sym (rel->r_info));
c25bc9fc
L
853 void **slot;
854
855 e.elf.indx = sec->id;
351f65ca 856 e.elf.dynstr_index = htab->r_sym (rel->r_info);
c25bc9fc
L
857 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
858 create ? INSERT : NO_INSERT);
859
860 if (!slot)
861 return NULL;
862
863 if (*slot)
864 {
351f65ca 865 ret = (struct elf_x86_64_link_hash_entry *) *slot;
c25bc9fc
L
866 return &ret->elf;
867 }
868
351f65ca 869 ret = (struct elf_x86_64_link_hash_entry *)
c25bc9fc 870 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
351f65ca 871 sizeof (struct elf_x86_64_link_hash_entry));
c25bc9fc
L
872 if (ret)
873 {
874 memset (ret, 0, sizeof (*ret));
875 ret->elf.indx = sec->id;
351f65ca 876 ret->elf.dynstr_index = htab->r_sym (rel->r_info);
c25bc9fc 877 ret->elf.dynindx = -1;
c25bc9fc
L
878 *slot = ret;
879 }
880 return &ret->elf;
881}
882
8d88c4ca
NC
883/* Create an X86-64 ELF linker hash table. */
884
885static struct bfd_link_hash_table *
351f65ca 886elf_x86_64_link_hash_table_create (bfd *abfd)
8d88c4ca 887{
351f65ca
L
888 struct elf_x86_64_link_hash_table *ret;
889 bfd_size_type amt = sizeof (struct elf_x86_64_link_hash_table);
8d88c4ca 890
7bf52ea2 891 ret = (struct elf_x86_64_link_hash_table *) bfd_zmalloc (amt);
c434dee6 892 if (ret == NULL)
8d88c4ca
NC
893 return NULL;
894
eb4ff4d6 895 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
351f65ca
L
896 elf_x86_64_link_hash_newfunc,
897 sizeof (struct elf_x86_64_link_hash_entry),
4dfe6ac6 898 X86_64_ELF_DATA))
8d88c4ca 899 {
e2d34d7d 900 free (ret);
8d88c4ca
NC
901 return NULL;
902 }
903
351f65ca
L
904 if (ABI_64_P (abfd))
905 {
906 ret->r_info = elf64_r_info;
907 ret->r_sym = elf64_r_sym;
248775ba 908 ret->pointer_r_type = R_X86_64_64;
351f65ca
L
909 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
910 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
911 }
912 else
913 {
914 ret->r_info = elf32_r_info;
915 ret->r_sym = elf32_r_sym;
248775ba 916 ret->pointer_r_type = R_X86_64_32;
351f65ca
L
917 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
918 ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
919 }
920
c25bc9fc 921 ret->loc_hash_table = htab_try_create (1024,
351f65ca
L
922 elf_x86_64_local_htab_hash,
923 elf_x86_64_local_htab_eq,
c25bc9fc
L
924 NULL);
925 ret->loc_hash_memory = objalloc_create ();
926 if (!ret->loc_hash_table || !ret->loc_hash_memory)
927 {
928 free (ret);
929 return NULL;
930 }
931
c434dee6
AJ
932 return &ret->elf.root;
933}
934
c25bc9fc
L
935/* Destroy an X86-64 ELF linker hash table. */
936
937static void
351f65ca 938elf_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
c25bc9fc 939{
351f65ca
L
940 struct elf_x86_64_link_hash_table *htab
941 = (struct elf_x86_64_link_hash_table *) hash;
c25bc9fc
L
942
943 if (htab->loc_hash_table)
944 htab_delete (htab->loc_hash_table);
945 if (htab->loc_hash_memory)
946 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
9f7c3e5e 947 _bfd_elf_link_hash_table_free (hash);
c25bc9fc
L
948}
949
c434dee6
AJ
950/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
951 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
952 hash table. */
953
b34976b6 954static bfd_boolean
351f65ca
L
955elf_x86_64_create_dynamic_sections (bfd *dynobj,
956 struct bfd_link_info *info)
c434dee6 957{
351f65ca 958 struct elf_x86_64_link_hash_table *htab;
c434dee6 959
c434dee6 960 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 961 return FALSE;
c434dee6 962
351f65ca 963 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
964 if (htab == NULL)
965 return FALSE;
966
3d4d4302 967 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
c434dee6 968 if (!info->shared)
3d4d4302 969 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
c434dee6 970
6de2ae4a 971 if (!htab->sdynbss
c434dee6
AJ
972 || (!info->shared && !htab->srelbss))
973 abort ();
974
e41b3a13 975 if (!info->no_ld_generated_unwind_info
2fe0fd06 976 && htab->plt_eh_frame == NULL
e4de50d4 977 && htab->elf.splt != NULL)
e41b3a13 978 {
bbf96e4e
L
979 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
980 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
981 | SEC_LINKER_CREATED);
e41b3a13 982 htab->plt_eh_frame
bbf96e4e 983 = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
e41b3a13
JJ
984 if (htab->plt_eh_frame == NULL
985 || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 3))
986 return FALSE;
e41b3a13 987 }
b34976b6 988 return TRUE;
c434dee6
AJ
989}
990
991/* Copy the extra info we tack onto an elf_link_hash_entry. */
992
993static void
351f65ca
L
994elf_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
995 struct elf_link_hash_entry *dir,
996 struct elf_link_hash_entry *ind)
c434dee6 997{
351f65ca 998 struct elf_x86_64_link_hash_entry *edir, *eind;
c434dee6 999
351f65ca
L
1000 edir = (struct elf_x86_64_link_hash_entry *) dir;
1001 eind = (struct elf_x86_64_link_hash_entry *) ind;
c434dee6
AJ
1002
1003 if (eind->dyn_relocs != NULL)
1004 {
1005 if (edir->dyn_relocs != NULL)
1006 {
e03a8ed8
L
1007 struct elf_dyn_relocs **pp;
1008 struct elf_dyn_relocs *p;
c434dee6 1009
fcfa13d2 1010 /* Add reloc counts against the indirect sym to the direct sym
c434dee6
AJ
1011 list. Merge any entries against the same section. */
1012 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1013 {
e03a8ed8 1014 struct elf_dyn_relocs *q;
c434dee6
AJ
1015
1016 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1017 if (q->sec == p->sec)
1018 {
1019 q->pc_count += p->pc_count;
1020 q->count += p->count;
1021 *pp = p->next;
1022 break;
1023 }
1024 if (q == NULL)
1025 pp = &p->next;
1026 }
1027 *pp = edir->dyn_relocs;
1028 }
1029
1030 edir->dyn_relocs = eind->dyn_relocs;
1031 eind->dyn_relocs = NULL;
1032 }
1033
bffbf940
JJ
1034 if (ind->root.type == bfd_link_hash_indirect
1035 && dir->got.refcount <= 0)
1036 {
1037 edir->tls_type = eind->tls_type;
1038 eind->tls_type = GOT_UNKNOWN;
1039 }
1040
d40d037c
AJ
1041 if (ELIMINATE_COPY_RELOCS
1042 && ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
1043 && dir->dynamic_adjusted)
1044 {
1045 /* If called to transfer flags for a weakdef during processing
1046 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1047 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1048 dir->ref_dynamic |= ind->ref_dynamic;
1049 dir->ref_regular |= ind->ref_regular;
1050 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1051 dir->needs_plt |= ind->needs_plt;
1052 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1053 }
d40d037c 1054 else
fcfa13d2 1055 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
8d88c4ca
NC
1056}
1057
b34976b6 1058static bfd_boolean
27482721 1059elf64_x86_64_elf_object_p (bfd *abfd)
bffbf940 1060{
8d88c4ca
NC
1061 /* Set the right machine number for an x86-64 elf64 file. */
1062 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
b34976b6 1063 return TRUE;
8d88c4ca
NC
1064}
1065
8059fb19
RM
1066static bfd_boolean
1067elf32_x86_64_elf_object_p (bfd *abfd)
1068{
1069 /* Set the right machine number for an x86-64 elf32 file. */
1070 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1071 return TRUE;
1072}
1073
142411ca
L
1074/* Return TRUE if the TLS access code sequence support transition
1075 from R_TYPE. */
1076
1077static bfd_boolean
351f65ca
L
1078elf_x86_64_check_tls_transition (bfd *abfd,
1079 struct bfd_link_info *info,
1080 asection *sec,
1081 bfd_byte *contents,
1082 Elf_Internal_Shdr *symtab_hdr,
1083 struct elf_link_hash_entry **sym_hashes,
1084 unsigned int r_type,
1085 const Elf_Internal_Rela *rel,
1086 const Elf_Internal_Rela *relend)
bffbf940 1087{
142411ca
L
1088 unsigned int val;
1089 unsigned long r_symndx;
1090 struct elf_link_hash_entry *h;
1091 bfd_vma offset;
351f65ca 1092 struct elf_x86_64_link_hash_table *htab;
142411ca
L
1093
1094 /* Get the section contents. */
1095 if (contents == NULL)
1096 {
1097 if (elf_section_data (sec)->this_hdr.contents != NULL)
1098 contents = elf_section_data (sec)->this_hdr.contents;
1099 else
1100 {
1101 /* FIXME: How to better handle error condition? */
1102 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1103 return FALSE;
bffbf940 1104
142411ca
L
1105 /* Cache the section contents for elf_link_input_bfd. */
1106 elf_section_data (sec)->this_hdr.contents = contents;
1107 }
1108 }
1109
351f65ca 1110 htab = elf_x86_64_hash_table (info);
142411ca 1111 offset = rel->r_offset;
bffbf940 1112 switch (r_type)
142411ca
L
1113 {
1114 case R_X86_64_TLSGD:
1115 case R_X86_64_TLSLD:
1116 if ((rel + 1) >= relend)
1117 return FALSE;
1118
1119 if (r_type == R_X86_64_TLSGD)
1120 {
52bc799a 1121 /* Check transition from GD access model. For 64bit, only
142411ca
L
1122 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1123 .word 0x6666; rex64; call __tls_get_addr
52bc799a
L
1124 can transit to different access model. For 32bit, only
1125 leaq foo@tlsgd(%rip), %rdi
1126 .word 0x6666; rex64; call __tls_get_addr
142411ca
L
1127 can transit to different access model. */
1128
fa289a5f
AM
1129 static const unsigned char call[] = { 0x66, 0x66, 0x48, 0xe8 };
1130 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1131
52bc799a 1132 if ((offset + 12) > sec->size
fa289a5f 1133 || memcmp (contents + offset + 4, call, 4) != 0)
142411ca 1134 return FALSE;
52bc799a
L
1135
1136 if (ABI_64_P (abfd))
1137 {
52bc799a 1138 if (offset < 4
fa289a5f 1139 || memcmp (contents + offset - 4, leaq, 4) != 0)
52bc799a
L
1140 return FALSE;
1141 }
1142 else
1143 {
52bc799a 1144 if (offset < 3
fa289a5f 1145 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
52bc799a
L
1146 return FALSE;
1147 }
142411ca
L
1148 }
1149 else
1150 {
1151 /* Check transition from LD access model. Only
1152 leaq foo@tlsld(%rip), %rdi;
1153 call __tls_get_addr
1154 can transit to different access model. */
1155
fa289a5f 1156 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
142411ca
L
1157
1158 if (offset < 3 || (offset + 9) > sec->size)
1159 return FALSE;
1160
fa289a5f
AM
1161 if (memcmp (contents + offset - 3, lea, 3) != 0
1162 || 0xe8 != *(contents + offset + 4))
142411ca
L
1163 return FALSE;
1164 }
1165
351f65ca 1166 r_symndx = htab->r_sym (rel[1].r_info);
142411ca
L
1167 if (r_symndx < symtab_hdr->sh_info)
1168 return FALSE;
1169
1170 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
c4fb387b 1171 /* Use strncmp to check __tls_get_addr since __tls_get_addr
eed180f8 1172 may be versioned. */
142411ca
L
1173 return (h != NULL
1174 && h->root.root.string != NULL
351f65ca
L
1175 && (ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PC32
1176 || ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
c4fb387b
L
1177 && (strncmp (h->root.root.string,
1178 "__tls_get_addr", 14) == 0));
142411ca
L
1179
1180 case R_X86_64_GOTTPOFF:
1181 /* Check transition from IE access model:
4a4c5f25
L
1182 mov foo@gottpoff(%rip), %reg
1183 add foo@gottpoff(%rip), %reg
142411ca
L
1184 */
1185
4a4c5f25
L
1186 /* Check REX prefix first. */
1187 if (offset >= 3 && (offset + 4) <= sec->size)
1188 {
1189 val = bfd_get_8 (abfd, contents + offset - 3);
1190 if (val != 0x48 && val != 0x4c)
1191 {
1192 /* X32 may have 0x44 REX prefix or no REX prefix. */
1193 if (ABI_64_P (abfd))
1194 return FALSE;
1195 }
1196 }
1197 else
1198 {
1199 /* X32 may not have any REX prefix. */
1200 if (ABI_64_P (abfd))
1201 return FALSE;
1202 if (offset < 2 || (offset + 3) > sec->size)
1203 return FALSE;
1204 }
142411ca
L
1205
1206 val = bfd_get_8 (abfd, contents + offset - 2);
1207 if (val != 0x8b && val != 0x03)
1208 return FALSE;
1209
1210 val = bfd_get_8 (abfd, contents + offset - 1);
1211 return (val & 0xc7) == 5;
1212
1213 case R_X86_64_GOTPC32_TLSDESC:
1214 /* Check transition from GDesc access model:
1215 leaq x@tlsdesc(%rip), %rax
1216
1217 Make sure it's a leaq adding rip to a 32-bit offset
1218 into any register, although it's probably almost always
1219 going to be rax. */
1220
1221 if (offset < 3 || (offset + 4) > sec->size)
1222 return FALSE;
1223
1224 val = bfd_get_8 (abfd, contents + offset - 3);
1225 if ((val & 0xfb) != 0x48)
1226 return FALSE;
1227
1228 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1229 return FALSE;
1230
1231 val = bfd_get_8 (abfd, contents + offset - 1);
1232 return (val & 0xc7) == 0x05;
1233
1234 case R_X86_64_TLSDESC_CALL:
1235 /* Check transition from GDesc access model:
1236 call *x@tlsdesc(%rax)
1237 */
1238 if (offset + 2 <= sec->size)
1239 {
1240 /* Make sure that it's a call *x@tlsdesc(%rax). */
fa289a5f
AM
1241 static const unsigned char call[] = { 0xff, 0x10 };
1242 return memcmp (contents + offset, call, 2) == 0;
142411ca
L
1243 }
1244
1245 return FALSE;
1246
1247 default:
1248 abort ();
1249 }
1250}
1251
1252/* Return TRUE if the TLS access transition is OK or no transition
1253 will be performed. Update R_TYPE if there is a transition. */
1254
1255static bfd_boolean
351f65ca
L
1256elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1257 asection *sec, bfd_byte *contents,
1258 Elf_Internal_Shdr *symtab_hdr,
1259 struct elf_link_hash_entry **sym_hashes,
1260 unsigned int *r_type, int tls_type,
1261 const Elf_Internal_Rela *rel,
1262 const Elf_Internal_Rela *relend,
1263 struct elf_link_hash_entry *h,
1264 unsigned long r_symndx)
142411ca
L
1265{
1266 unsigned int from_type = *r_type;
1267 unsigned int to_type = from_type;
1268 bfd_boolean check = TRUE;
1269
bb1cb422
L
1270 /* Skip TLS transition for functions. */
1271 if (h != NULL
1272 && (h->type == STT_FUNC
1273 || h->type == STT_GNU_IFUNC))
1274 return TRUE;
1275
142411ca 1276 switch (from_type)
bffbf940
JJ
1277 {
1278 case R_X86_64_TLSGD:
67a4f2b7
AO
1279 case R_X86_64_GOTPC32_TLSDESC:
1280 case R_X86_64_TLSDESC_CALL:
bffbf940 1281 case R_X86_64_GOTTPOFF:
1d85728f 1282 if (info->executable)
142411ca
L
1283 {
1284 if (h == NULL)
1285 to_type = R_X86_64_TPOFF32;
1286 else
1287 to_type = R_X86_64_GOTTPOFF;
1288 }
1289
351f65ca 1290 /* When we are called from elf_x86_64_relocate_section,
142411ca
L
1291 CONTENTS isn't NULL and there may be additional transitions
1292 based on TLS_TYPE. */
1293 if (contents != NULL)
1294 {
1295 unsigned int new_to_type = to_type;
1296
1d85728f 1297 if (info->executable
142411ca
L
1298 && h != NULL
1299 && h->dynindx == -1
1300 && tls_type == GOT_TLS_IE)
1301 new_to_type = R_X86_64_TPOFF32;
1302
1303 if (to_type == R_X86_64_TLSGD
1304 || to_type == R_X86_64_GOTPC32_TLSDESC
1305 || to_type == R_X86_64_TLSDESC_CALL)
1306 {
1307 if (tls_type == GOT_TLS_IE)
1308 new_to_type = R_X86_64_GOTTPOFF;
1309 }
1310
1311 /* We checked the transition before when we were called from
351f65ca 1312 elf_x86_64_check_relocs. We only want to check the new
142411ca
L
1313 transition which hasn't been checked before. */
1314 check = new_to_type != to_type && from_type == to_type;
1315 to_type = new_to_type;
1316 }
1317
1318 break;
1319
bffbf940 1320 case R_X86_64_TLSLD:
1d85728f 1321 if (info->executable)
142411ca
L
1322 to_type = R_X86_64_TPOFF32;
1323 break;
1324
1325 default:
1326 return TRUE;
bffbf940
JJ
1327 }
1328
142411ca
L
1329 /* Return TRUE if there is no transition. */
1330 if (from_type == to_type)
1331 return TRUE;
1332
1333 /* Check if the transition can be performed. */
1334 if (check
351f65ca
L
1335 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1336 symtab_hdr, sym_hashes,
1337 from_type, rel, relend))
142411ca 1338 {
2f629d23 1339 reloc_howto_type *from, *to;
4c544807 1340 const char *name;
142411ca 1341
351f65ca
L
1342 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1343 to = elf_x86_64_rtype_to_howto (abfd, to_type);
142411ca 1344
4c544807
L
1345 if (h)
1346 name = h->root.root.string;
1347 else
1348 {
351f65ca 1349 struct elf_x86_64_link_hash_table *htab;
4dfe6ac6 1350
351f65ca 1351 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
1352 if (htab == NULL)
1353 name = "*unknown*";
1354 else
1355 {
1356 Elf_Internal_Sym *isym;
1357
1358 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1359 abfd, r_symndx);
1360 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1361 }
4c544807
L
1362 }
1363
142411ca
L
1364 (*_bfd_error_handler)
1365 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1366 "in section `%A' failed"),
4c544807 1367 abfd, sec, from->name, to->name, name,
142411ca
L
1368 (unsigned long) rel->r_offset);
1369 bfd_set_error (bfd_error_bad_value);
1370 return FALSE;
1371 }
1372
1373 *r_type = to_type;
1374 return TRUE;
bffbf940
JJ
1375}
1376
70256ad8 1377/* Look through the relocs for a section during the first phase, and
c434dee6
AJ
1378 calculate needed space in the global offset table, procedure
1379 linkage table, and dynamic reloc sections. */
70256ad8 1380
b34976b6 1381static bfd_boolean
351f65ca
L
1382elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1383 asection *sec,
1384 const Elf_Internal_Rela *relocs)
70256ad8 1385{
351f65ca 1386 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
1387 Elf_Internal_Shdr *symtab_hdr;
1388 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
1389 const Elf_Internal_Rela *rel;
1390 const Elf_Internal_Rela *rel_end;
70256ad8
AJ
1391 asection *sreloc;
1392
1049f94e 1393 if (info->relocatable)
b34976b6 1394 return TRUE;
70256ad8 1395
0ffa91dd
NC
1396 BFD_ASSERT (is_x86_64_elf (abfd));
1397
351f65ca 1398 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
1399 if (htab == NULL)
1400 return FALSE;
1401
0ffa91dd 1402 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8 1403 sym_hashes = elf_sym_hashes (abfd);
70256ad8 1404
c434dee6 1405 sreloc = NULL;
cbe950e9 1406
70256ad8
AJ
1407 rel_end = relocs + sec->reloc_count;
1408 for (rel = relocs; rel < rel_end; rel++)
1409 {
bffbf940 1410 unsigned int r_type;
70256ad8
AJ
1411 unsigned long r_symndx;
1412 struct elf_link_hash_entry *h;
4c544807
L
1413 Elf_Internal_Sym *isym;
1414 const char *name;
06a6a421 1415 bfd_boolean size_reloc;
70256ad8 1416
351f65ca
L
1417 r_symndx = htab->r_sym (rel->r_info);
1418 r_type = ELF32_R_TYPE (rel->r_info);
c434dee6
AJ
1419
1420 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1421 {
d003868e
AM
1422 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1423 abfd, r_symndx);
b34976b6 1424 return FALSE;
c434dee6
AJ
1425 }
1426
70256ad8 1427 if (r_symndx < symtab_hdr->sh_info)
c25bc9fc
L
1428 {
1429 /* A local symbol. */
c2e61a4e
L
1430 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1431 abfd, r_symndx);
1432 if (isym == NULL)
1433 return FALSE;
c25bc9fc
L
1434
1435 /* Check relocation against local STT_GNU_IFUNC symbol. */
351f65ca 1436 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c25bc9fc 1437 {
351f65ca
L
1438 h = elf_x86_64_get_local_sym_hash (htab, abfd, rel,
1439 TRUE);
c25bc9fc 1440 if (h == NULL)
c2e61a4e 1441 return FALSE;
6bbec505 1442
c25bc9fc
L
1443 /* Fake a STT_GNU_IFUNC symbol. */
1444 h->type = STT_GNU_IFUNC;
1445 h->def_regular = 1;
1446 h->ref_regular = 1;
1447 h->forced_local = 1;
1448 h->root.type = bfd_link_hash_defined;
1449 }
1450 else
1451 h = NULL;
1452 }
70256ad8 1453 else
71cb9464 1454 {
4c544807 1455 isym = NULL;
71cb9464
L
1456 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1457 while (h->root.type == bfd_link_hash_indirect
1458 || h->root.type == bfd_link_hash_warning)
1459 h = (struct elf_link_hash_entry *) h->root.u.i.link;
c25bc9fc 1460 }
cbe950e9 1461
d1534d16
L
1462 /* Check invalid x32 relocations. */
1463 if (!ABI_64_P (abfd))
1464 switch (r_type)
1465 {
1466 default:
1467 break;
1468
d1534d16
L
1469 case R_X86_64_DTPOFF64:
1470 case R_X86_64_TPOFF64:
1471 case R_X86_64_PC64:
1472 case R_X86_64_GOTOFF64:
1473 case R_X86_64_GOT64:
1474 case R_X86_64_GOTPCREL64:
1475 case R_X86_64_GOTPC64:
1476 case R_X86_64_GOTPLT64:
1477 case R_X86_64_PLTOFF64:
1478 {
1479 if (h)
1480 name = h->root.root.string;
1481 else
1482 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1483 NULL);
1484 (*_bfd_error_handler)
1485 (_("%B: relocation %s against symbol `%s' isn't "
1486 "supported in x32 mode"), abfd,
1487 x86_64_elf_howto_table[r_type].name, name);
1488 bfd_set_error (bfd_error_bad_value);
1489 return FALSE;
1490 }
1491 break;
1492 }
1493
c25bc9fc
L
1494 if (h != NULL)
1495 {
cbe950e9
L
1496 /* Create the ifunc sections for static executables. If we
1497 never see an indirect function symbol nor we are building
1498 a static executable, those sections will be empty and
1499 won't appear in output. */
1500 switch (r_type)
1501 {
1502 default:
1503 break;
1504
1505 case R_X86_64_32S:
1506 case R_X86_64_32:
1507 case R_X86_64_64:
1508 case R_X86_64_PC32:
1509 case R_X86_64_PC64:
1510 case R_X86_64_PLT32:
1511 case R_X86_64_GOTPCREL:
1512 case R_X86_64_GOTPCREL64:
9d4057ee
AM
1513 if (htab->elf.dynobj == NULL)
1514 htab->elf.dynobj = abfd;
1515 if (!_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
c2e61a4e 1516 return FALSE;
cbe950e9
L
1517 break;
1518 }
1519
ad1e85de
L
1520 /* It is referenced by a non-shared object. */
1521 h->ref_regular = 1;
61315175 1522 h->root.non_ir_ref = 1;
71cb9464 1523 }
70256ad8 1524
351f65ca
L
1525 if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
1526 symtab_hdr, sym_hashes,
1527 &r_type, GOT_UNKNOWN,
1528 rel, rel_end, h, r_symndx))
c2e61a4e 1529 return FALSE;
142411ca 1530
bffbf940 1531 switch (r_type)
70256ad8 1532 {
bffbf940
JJ
1533 case R_X86_64_TLSLD:
1534 htab->tls_ld_got.refcount += 1;
1535 goto create_got;
1536
1537 case R_X86_64_TPOFF32:
351f65ca 1538 if (!info->executable && ABI_64_P (abfd))
70256ad8 1539 {
09a24cbf 1540 if (h)
4c544807
L
1541 name = h->root.root.string;
1542 else
1543 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1544 NULL);
bffbf940 1545 (*_bfd_error_handler)
d003868e
AM
1546 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1547 abfd,
4c544807 1548 x86_64_elf_howto_table[r_type].name, name);
bffbf940 1549 bfd_set_error (bfd_error_bad_value);
c2e61a4e 1550 return FALSE;
70256ad8 1551 }
bffbf940 1552 break;
c434dee6 1553
bffbf940 1554 case R_X86_64_GOTTPOFF:
1d85728f 1555 if (!info->executable)
bffbf940
JJ
1556 info->flags |= DF_STATIC_TLS;
1557 /* Fall through */
70256ad8 1558
bffbf940
JJ
1559 case R_X86_64_GOT32:
1560 case R_X86_64_GOTPCREL:
1561 case R_X86_64_TLSGD:
7b81dfbb
AJ
1562 case R_X86_64_GOT64:
1563 case R_X86_64_GOTPCREL64:
1564 case R_X86_64_GOTPLT64:
67a4f2b7
AO
1565 case R_X86_64_GOTPC32_TLSDESC:
1566 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
1567 /* This symbol requires a global offset table entry. */
1568 {
1569 int tls_type, old_tls_type;
1570
1571 switch (r_type)
1572 {
1573 default: tls_type = GOT_NORMAL; break;
1574 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1575 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
67a4f2b7
AO
1576 case R_X86_64_GOTPC32_TLSDESC:
1577 case R_X86_64_TLSDESC_CALL:
1578 tls_type = GOT_TLS_GDESC; break;
bffbf940
JJ
1579 }
1580
1581 if (h != NULL)
1582 {
7b81dfbb
AJ
1583 if (r_type == R_X86_64_GOTPLT64)
1584 {
1585 /* This relocation indicates that we also need
1586 a PLT entry, as this is a function. We don't need
1587 a PLT entry for local symbols. */
1588 h->needs_plt = 1;
1589 h->plt.refcount += 1;
1590 }
bffbf940 1591 h->got.refcount += 1;
351f65ca 1592 old_tls_type = elf_x86_64_hash_entry (h)->tls_type;
bffbf940
JJ
1593 }
1594 else
1595 {
1596 bfd_signed_vma *local_got_refcounts;
1597
1598 /* This is a global offset table entry for a local symbol. */
1599 local_got_refcounts = elf_local_got_refcounts (abfd);
1600 if (local_got_refcounts == NULL)
1601 {
1602 bfd_size_type size;
1603
1604 size = symtab_hdr->sh_info;
67a4f2b7
AO
1605 size *= sizeof (bfd_signed_vma)
1606 + sizeof (bfd_vma) + sizeof (char);
bffbf940
JJ
1607 local_got_refcounts = ((bfd_signed_vma *)
1608 bfd_zalloc (abfd, size));
1609 if (local_got_refcounts == NULL)
c2e61a4e 1610 return FALSE;
bffbf940 1611 elf_local_got_refcounts (abfd) = local_got_refcounts;
351f65ca 1612 elf_x86_64_local_tlsdesc_gotent (abfd)
67a4f2b7 1613 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
351f65ca 1614 elf_x86_64_local_got_tls_type (abfd)
67a4f2b7 1615 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
bffbf940
JJ
1616 }
1617 local_got_refcounts[r_symndx] += 1;
1618 old_tls_type
351f65ca 1619 = elf_x86_64_local_got_tls_type (abfd) [r_symndx];
bffbf940
JJ
1620 }
1621
1622 /* If a TLS symbol is accessed using IE at least once,
1623 there is no point to use dynamic model for it. */
1624 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7
AO
1625 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1626 || tls_type != GOT_TLS_IE))
bffbf940 1627 {
67a4f2b7 1628 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
bffbf940 1629 tls_type = old_tls_type;
67a4f2b7
AO
1630 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1631 && GOT_TLS_GD_ANY_P (tls_type))
1632 tls_type |= old_tls_type;
bffbf940
JJ
1633 else
1634 {
09a24cbf 1635 if (h)
4c544807
L
1636 name = h->root.root.string;
1637 else
1638 name = bfd_elf_sym_name (abfd, symtab_hdr,
1639 isym, NULL);
bffbf940 1640 (*_bfd_error_handler)
1f7a4e42 1641 (_("%B: '%s' accessed both as normal and thread local symbol"),
4c544807 1642 abfd, name);
68c4a57e 1643 bfd_set_error (bfd_error_bad_value);
c2e61a4e 1644 return FALSE;
bffbf940
JJ
1645 }
1646 }
1647
1648 if (old_tls_type != tls_type)
1649 {
1650 if (h != NULL)
351f65ca 1651 elf_x86_64_hash_entry (h)->tls_type = tls_type;
bffbf940 1652 else
351f65ca 1653 elf_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
bffbf940
JJ
1654 }
1655 }
c434dee6
AJ
1656 /* Fall through */
1657
d6ab8113
JB
1658 case R_X86_64_GOTOFF64:
1659 case R_X86_64_GOTPC32:
7b81dfbb 1660 case R_X86_64_GOTPC64:
bffbf940 1661 create_got:
6de2ae4a 1662 if (htab->elf.sgot == NULL)
c434dee6
AJ
1663 {
1664 if (htab->elf.dynobj == NULL)
1665 htab->elf.dynobj = abfd;
6de2ae4a
L
1666 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1667 info))
c2e61a4e 1668 return FALSE;
c434dee6 1669 }
70256ad8
AJ
1670 break;
1671
1672 case R_X86_64_PLT32:
1673 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
1674 actually build the entry in adjust_dynamic_symbol,
1675 because this might be a case of linking PIC code which is
1676 never referenced by a dynamic object, in which case we
1677 don't need to generate a procedure linkage table entry
1678 after all. */
70256ad8
AJ
1679
1680 /* If this is a local symbol, we resolve it directly without
407443a3 1681 creating a procedure linkage table entry. */
70256ad8
AJ
1682 if (h == NULL)
1683 continue;
1684
f5385ebf 1685 h->needs_plt = 1;
51b64d56 1686 h->plt.refcount += 1;
70256ad8
AJ
1687 break;
1688
7b81dfbb
AJ
1689 case R_X86_64_PLTOFF64:
1690 /* This tries to form the 'address' of a function relative
1691 to GOT. For global symbols we need a PLT entry. */
1692 if (h != NULL)
1693 {
1694 h->needs_plt = 1;
1695 h->plt.refcount += 1;
1696 }
1697 goto create_got;
1698
6a3e1bae
L
1699 case R_X86_64_SIZE32:
1700 case R_X86_64_SIZE64:
06a6a421 1701 size_reloc = TRUE;
6a3e1bae
L
1702 goto do_size;
1703
248775ba
L
1704 case R_X86_64_32:
1705 if (!ABI_64_P (abfd))
1706 goto pointer;
cc78d0af
AJ
1707 case R_X86_64_8:
1708 case R_X86_64_16:
70256ad8 1709 case R_X86_64_32S:
1b71fb54
AJ
1710 /* Let's help debug shared library creation. These relocs
1711 cannot be used in shared libs. Don't error out for
1712 sections we don't care about, such as debug sections or
1713 non-constant sections. */
1714 if (info->shared
1715 && (sec->flags & SEC_ALLOC) != 0
1716 && (sec->flags & SEC_READONLY) != 0)
1717 {
09a24cbf 1718 if (h)
4c544807
L
1719 name = h->root.root.string;
1720 else
1721 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1b71fb54 1722 (*_bfd_error_handler)
d003868e 1723 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
4c544807 1724 abfd, x86_64_elf_howto_table[r_type].name, name);
1b71fb54 1725 bfd_set_error (bfd_error_bad_value);
c2e61a4e 1726 return FALSE;
1b71fb54
AJ
1727 }
1728 /* Fall through. */
1729
c434dee6
AJ
1730 case R_X86_64_PC8:
1731 case R_X86_64_PC16:
70256ad8 1732 case R_X86_64_PC32:
d6ab8113 1733 case R_X86_64_PC64:
1b71fb54 1734 case R_X86_64_64:
248775ba 1735pointer:
710ab287 1736 if (h != NULL && info->executable)
c434dee6
AJ
1737 {
1738 /* If this reloc is in a read-only section, we might
1739 need a copy reloc. We can't check reliably at this
1740 stage whether the section is read-only, as input
1741 sections have not yet been mapped to output sections.
1742 Tentatively set the flag for now, and correct in
1743 adjust_dynamic_symbol. */
f5385ebf 1744 h->non_got_ref = 1;
c434dee6
AJ
1745
1746 /* We may need a .plt entry if the function this reloc
1747 refers to is in a shared lib. */
1748 h->plt.refcount += 1;
d6ab8113 1749 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
f5385ebf 1750 h->pointer_equality_needed = 1;
c434dee6 1751 }
70256ad8 1752
06a6a421 1753 size_reloc = FALSE;
6a3e1bae 1754do_size:
70256ad8
AJ
1755 /* If we are creating a shared library, and this is a reloc
1756 against a global symbol, or a non PC relative reloc
1757 against a local symbol, then we need to copy the reloc
1758 into the shared library. However, if we are linking with
1759 -Bsymbolic, we do not need to copy a reloc against a
1760 global symbol which is defined in an object we are
407443a3 1761 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
1762 this point we have not seen all the input files, so it is
1763 possible that DEF_REGULAR is not set now but will be set
c434dee6
AJ
1764 later (it is never cleared). In case of a weak definition,
1765 DEF_REGULAR may be cleared later by a strong definition in
1766 a shared library. We account for that possibility below by
1767 storing information in the relocs_copied field of the hash
1768 table entry. A similar situation occurs when creating
1769 shared libraries and symbol visibility changes render the
1770 symbol local.
1771
1772 If on the other hand, we are creating an executable, we
1773 may need to keep relocations for symbols satisfied by a
1774 dynamic library if we manage to avoid copy relocs for the
0f88be7a 1775 symbol. */
c434dee6
AJ
1776 if ((info->shared
1777 && (sec->flags & SEC_ALLOC) != 0
d8045f23 1778 && (! IS_X86_64_PCREL_TYPE (r_type)
c434dee6 1779 || (h != NULL
55255dae 1780 && (! SYMBOLIC_BIND (info, h)
c434dee6 1781 || h->root.type == bfd_link_hash_defweak
f5385ebf 1782 || !h->def_regular))))
d40d037c
AJ
1783 || (ELIMINATE_COPY_RELOCS
1784 && !info->shared
c434dee6
AJ
1785 && (sec->flags & SEC_ALLOC) != 0
1786 && h != NULL
1787 && (h->root.type == bfd_link_hash_defweak
0f88be7a 1788 || !h->def_regular)))
70256ad8 1789 {
e03a8ed8
L
1790 struct elf_dyn_relocs *p;
1791 struct elf_dyn_relocs **head;
c434dee6
AJ
1792
1793 /* We must copy these reloc types into the output file.
1794 Create a reloc section in dynobj and make room for
1795 this reloc. */
70256ad8
AJ
1796 if (sreloc == NULL)
1797 {
c434dee6
AJ
1798 if (htab->elf.dynobj == NULL)
1799 htab->elf.dynobj = abfd;
1800
83bac4b0 1801 sreloc = _bfd_elf_make_dynamic_reloc_section
82e96e07
L
1802 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
1803 abfd, /*rela?*/ TRUE);
70256ad8 1804
70256ad8 1805 if (sreloc == NULL)
c2e61a4e 1806 return FALSE;
70256ad8
AJ
1807 }
1808
c434dee6
AJ
1809 /* If this is a global symbol, we count the number of
1810 relocations we need for this symbol. */
1811 if (h != NULL)
70256ad8 1812 {
351f65ca 1813 head = &((struct elf_x86_64_link_hash_entry *) h)->dyn_relocs;
c434dee6
AJ
1814 }
1815 else
1816 {
1817 /* Track dynamic relocs needed for local syms too.
1818 We really need local syms available to do this
1819 easily. Oh well. */
c434dee6 1820 asection *s;
87d72d41 1821 void **vpp;
87d72d41
AM
1822
1823 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1824 abfd, r_symndx);
1825 if (isym == NULL)
1826 return FALSE;
1827
1828 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
c434dee6 1829 if (s == NULL)
87d72d41 1830 s = sec;
70256ad8 1831
e81d3500
DD
1832 /* Beware of type punned pointers vs strict aliasing
1833 rules. */
1834 vpp = &(elf_section_data (s)->local_dynrel);
e03a8ed8 1835 head = (struct elf_dyn_relocs **)vpp;
c434dee6 1836 }
70256ad8 1837
c434dee6
AJ
1838 p = *head;
1839 if (p == NULL || p->sec != sec)
1840 {
1841 bfd_size_type amt = sizeof *p;
d8045f23 1842
e03a8ed8 1843 p = ((struct elf_dyn_relocs *)
c434dee6 1844 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 1845 if (p == NULL)
c2e61a4e 1846 return FALSE;
c434dee6
AJ
1847 p->next = *head;
1848 *head = p;
1849 p->sec = sec;
1850 p->count = 0;
1851 p->pc_count = 0;
70256ad8 1852 }
c434dee6
AJ
1853
1854 p->count += 1;
06a6a421
L
1855 /* Count size relocation as PC-relative relocation. */
1856 if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
c434dee6 1857 p->pc_count += 1;
70256ad8
AJ
1858 }
1859 break;
fe4770f4
AJ
1860
1861 /* This relocation describes the C++ object vtable hierarchy.
1862 Reconstruct it for later use during GC. */
1863 case R_X86_64_GNU_VTINHERIT:
c152c796 1864 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
c2e61a4e 1865 return FALSE;
fe4770f4
AJ
1866 break;
1867
1868 /* This relocation describes which C++ vtable entries are actually
1869 used. Record for later use during GC. */
1870 case R_X86_64_GNU_VTENTRY:
d17e0c6e
JB
1871 BFD_ASSERT (h != NULL);
1872 if (h != NULL
1873 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
c2e61a4e 1874 return FALSE;
fe4770f4 1875 break;
c434dee6
AJ
1876
1877 default:
1878 break;
70256ad8
AJ
1879 }
1880 }
1881
b34976b6 1882 return TRUE;
70256ad8
AJ
1883}
1884
1885/* Return the section that should be marked against GC for a given
407443a3 1886 relocation. */
70256ad8
AJ
1887
1888static asection *
351f65ca
L
1889elf_x86_64_gc_mark_hook (asection *sec,
1890 struct bfd_link_info *info,
1891 Elf_Internal_Rela *rel,
1892 struct elf_link_hash_entry *h,
1893 Elf_Internal_Sym *sym)
70256ad8
AJ
1894{
1895 if (h != NULL)
351f65ca 1896 switch (ELF32_R_TYPE (rel->r_info))
07adf181
AM
1897 {
1898 case R_X86_64_GNU_VTINHERIT:
1899 case R_X86_64_GNU_VTENTRY:
1900 return NULL;
1901 }
1902
1903 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
70256ad8
AJ
1904}
1905
407443a3 1906/* Update the got entry reference counts for the section being removed. */
70256ad8 1907
b34976b6 1908static bfd_boolean
351f65ca
L
1909elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1910 asection *sec,
1911 const Elf_Internal_Rela *relocs)
70256ad8 1912{
351f65ca 1913 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
1914 Elf_Internal_Shdr *symtab_hdr;
1915 struct elf_link_hash_entry **sym_hashes;
1916 bfd_signed_vma *local_got_refcounts;
1917 const Elf_Internal_Rela *rel, *relend;
c434dee6 1918
7dda2462
TG
1919 if (info->relocatable)
1920 return TRUE;
1921
351f65ca 1922 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
1923 if (htab == NULL)
1924 return FALSE;
1925
c434dee6 1926 elf_section_data (sec)->local_dynrel = NULL;
70256ad8 1927
0ffa91dd 1928 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8
AJ
1929 sym_hashes = elf_sym_hashes (abfd);
1930 local_got_refcounts = elf_local_got_refcounts (abfd);
1931
351f65ca 1932 htab = elf_x86_64_hash_table (info);
70256ad8
AJ
1933 relend = relocs + sec->reloc_count;
1934 for (rel = relocs; rel < relend; rel++)
26e41594
AM
1935 {
1936 unsigned long r_symndx;
1937 unsigned int r_type;
1938 struct elf_link_hash_entry *h = NULL;
70256ad8 1939
351f65ca 1940 r_symndx = htab->r_sym (rel->r_info);
26e41594
AM
1941 if (r_symndx >= symtab_hdr->sh_info)
1942 {
26e41594 1943 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
1944 while (h->root.type == bfd_link_hash_indirect
1945 || h->root.type == bfd_link_hash_warning)
1946 h = (struct elf_link_hash_entry *) h->root.u.i.link;
26e41594 1947 }
bb1cb422
L
1948 else
1949 {
1950 /* A local symbol. */
1951 Elf_Internal_Sym *isym;
1952
1953 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1954 abfd, r_symndx);
1955
1956 /* Check relocation against local STT_GNU_IFUNC symbol. */
1957 if (isym != NULL
82e96e07 1958 && ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
bb1cb422 1959 {
351f65ca 1960 h = elf_x86_64_get_local_sym_hash (htab, abfd, rel, FALSE);
bb1cb422
L
1961 if (h == NULL)
1962 abort ();
1963 }
1964 }
c434dee6 1965
3db2e7dd
L
1966 if (h)
1967 {
1968 struct elf_x86_64_link_hash_entry *eh;
1969 struct elf_dyn_relocs **pp;
1970 struct elf_dyn_relocs *p;
1971
1972 eh = (struct elf_x86_64_link_hash_entry *) h;
1973
1974 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1975 if (p->sec == sec)
1976 {
1977 /* Everything must go for SEC. */
1978 *pp = p->next;
1979 break;
1980 }
1981 }
1982
351f65ca
L
1983 r_type = ELF32_R_TYPE (rel->r_info);
1984 if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
1985 symtab_hdr, sym_hashes,
1986 &r_type, GOT_UNKNOWN,
1987 rel, relend, h, r_symndx))
142411ca
L
1988 return FALSE;
1989
26e41594
AM
1990 switch (r_type)
1991 {
1992 case R_X86_64_TLSLD:
4dfe6ac6
NC
1993 if (htab->tls_ld_got.refcount > 0)
1994 htab->tls_ld_got.refcount -= 1;
26e41594 1995 break;
c434dee6 1996
26e41594 1997 case R_X86_64_TLSGD:
67a4f2b7
AO
1998 case R_X86_64_GOTPC32_TLSDESC:
1999 case R_X86_64_TLSDESC_CALL:
26e41594
AM
2000 case R_X86_64_GOTTPOFF:
2001 case R_X86_64_GOT32:
2002 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
2003 case R_X86_64_GOT64:
2004 case R_X86_64_GOTPCREL64:
2005 case R_X86_64_GOTPLT64:
26e41594
AM
2006 if (h != NULL)
2007 {
7b81dfbb 2008 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
eed180f8 2009 h->plt.refcount -= 1;
26e41594
AM
2010 if (h->got.refcount > 0)
2011 h->got.refcount -= 1;
bb1cb422
L
2012 if (h->type == STT_GNU_IFUNC)
2013 {
2014 if (h->plt.refcount > 0)
2015 h->plt.refcount -= 1;
2016 }
26e41594
AM
2017 }
2018 else if (local_got_refcounts != NULL)
2019 {
2020 if (local_got_refcounts[r_symndx] > 0)
2021 local_got_refcounts[r_symndx] -= 1;
2022 }
2023 break;
c434dee6 2024
26e41594
AM
2025 case R_X86_64_8:
2026 case R_X86_64_16:
2027 case R_X86_64_32:
2028 case R_X86_64_64:
2029 case R_X86_64_32S:
2030 case R_X86_64_PC8:
2031 case R_X86_64_PC16:
2032 case R_X86_64_PC32:
d6ab8113 2033 case R_X86_64_PC64:
1788fc08
L
2034 case R_X86_64_SIZE32:
2035 case R_X86_64_SIZE64:
3db2e7dd
L
2036 if (info->shared
2037 && (h == NULL || h->type != STT_GNU_IFUNC))
26e41594
AM
2038 break;
2039 /* Fall thru */
c434dee6 2040
26e41594 2041 case R_X86_64_PLT32:
7b81dfbb 2042 case R_X86_64_PLTOFF64:
26e41594
AM
2043 if (h != NULL)
2044 {
2045 if (h->plt.refcount > 0)
2046 h->plt.refcount -= 1;
2047 }
2048 break;
70256ad8 2049
26e41594
AM
2050 default:
2051 break;
2052 }
2053 }
70256ad8 2054
b34976b6 2055 return TRUE;
70256ad8
AJ
2056}
2057
2058/* Adjust a symbol defined by a dynamic object and referenced by a
2059 regular object. The current definition is in some section of the
2060 dynamic object, but we're not including those sections. We have to
2061 change the definition to something the rest of the link can
407443a3 2062 understand. */
70256ad8 2063
b34976b6 2064static bfd_boolean
351f65ca
L
2065elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
2066 struct elf_link_hash_entry *h)
70256ad8 2067{
351f65ca 2068 struct elf_x86_64_link_hash_table *htab;
70256ad8 2069 asection *s;
5ca5bb35
L
2070 struct elf_x86_64_link_hash_entry *eh;
2071 struct elf_dyn_relocs *p;
70256ad8 2072
cbe950e9
L
2073 /* STT_GNU_IFUNC symbol must go through PLT. */
2074 if (h->type == STT_GNU_IFUNC)
2075 {
73bcf233
L
2076 /* All local STT_GNU_IFUNC references must be treate as local
2077 calls via local PLT. */
5ca5bb35
L
2078 if (h->ref_regular
2079 && SYMBOL_CALLS_LOCAL (info, h))
2080 {
73bcf233 2081 bfd_size_type pc_count = 0, count = 0;
5ca5bb35
L
2082 struct elf_dyn_relocs **pp;
2083
2084 eh = (struct elf_x86_64_link_hash_entry *) h;
2085 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2086 {
2087 pc_count += p->pc_count;
2088 p->count -= p->pc_count;
2089 p->pc_count = 0;
73bcf233 2090 count += p->count;
5ca5bb35
L
2091 if (p->count == 0)
2092 *pp = p->next;
2093 else
2094 pp = &p->next;
2095 }
2096
73bcf233 2097 if (pc_count || count)
5ca5bb35
L
2098 {
2099 h->needs_plt = 1;
5ca5bb35 2100 h->non_got_ref = 1;
a5479e5f
L
2101 if (h->plt.refcount <= 0)
2102 h->plt.refcount = 1;
2103 else
2104 h->plt.refcount += 1;
5ca5bb35
L
2105 }
2106 }
2107
cbe950e9
L
2108 if (h->plt.refcount <= 0)
2109 {
2110 h->plt.offset = (bfd_vma) -1;
2111 h->needs_plt = 0;
2112 }
2113 return TRUE;
2114 }
2115
70256ad8
AJ
2116 /* If this is a function, put it in the procedure linkage table. We
2117 will fill in the contents of the procedure linkage table later,
2118 when we know the address of the .got section. */
2119 if (h->type == STT_FUNC
f5385ebf 2120 || h->needs_plt)
70256ad8 2121 {
c434dee6 2122 if (h->plt.refcount <= 0
27482721
AJ
2123 || SYMBOL_CALLS_LOCAL (info, h)
2124 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2125 && h->root.type == bfd_link_hash_undefweak))
70256ad8 2126 {
70256ad8
AJ
2127 /* This case can occur if we saw a PLT32 reloc in an input
2128 file, but the symbol was never referred to by a dynamic
2129 object, or if all references were garbage collected. In
2130 such a case, we don't actually need to build a procedure
2131 linkage table, and we can just do a PC32 reloc instead. */
70256ad8 2132 h->plt.offset = (bfd_vma) -1;
f5385ebf 2133 h->needs_plt = 0;
70256ad8
AJ
2134 }
2135
b34976b6 2136 return TRUE;
70256ad8 2137 }
bbd7ec4a 2138 else
c434dee6
AJ
2139 /* It's possible that we incorrectly decided a .plt reloc was
2140 needed for an R_X86_64_PC32 reloc to a non-function sym in
2141 check_relocs. We can't decide accurately between function and
2142 non-function syms in check-relocs; Objects loaded later in
2143 the link may change h->type. So fix it now. */
bbd7ec4a 2144 h->plt.offset = (bfd_vma) -1;
70256ad8
AJ
2145
2146 /* If this is a weak symbol, and there is a real definition, the
2147 processor independent code will have arranged for us to see the
407443a3 2148 real definition first, and we can just use the same value. */
f6e332e6 2149 if (h->u.weakdef != NULL)
70256ad8 2150 {
f6e332e6
AM
2151 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2152 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2153 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2154 h->root.u.def.value = h->u.weakdef->root.u.def.value;
d40d037c 2155 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
f6e332e6 2156 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 2157 return TRUE;
70256ad8
AJ
2158 }
2159
2160 /* This is a reference to a symbol defined by a dynamic object which
407443a3 2161 is not a function. */
70256ad8
AJ
2162
2163 /* If we are creating a shared library, we must presume that the
2164 only references to the symbol are via the global offset table.
2165 For such cases we need not do anything here; the relocations will
407443a3 2166 be handled correctly by relocate_section. */
70256ad8 2167 if (info->shared)
b34976b6 2168 return TRUE;
70256ad8
AJ
2169
2170 /* If there are no references to this symbol that do not use the
2171 GOT, we don't need to generate a copy reloc. */
f5385ebf 2172 if (!h->non_got_ref)
b34976b6 2173 return TRUE;
70256ad8 2174
c434dee6
AJ
2175 /* If -z nocopyreloc was given, we won't generate them either. */
2176 if (info->nocopyreloc)
2177 {
f5385ebf 2178 h->non_got_ref = 0;
b34976b6 2179 return TRUE;
c434dee6
AJ
2180 }
2181
d40d037c 2182 if (ELIMINATE_COPY_RELOCS)
c434dee6 2183 {
351f65ca 2184 eh = (struct elf_x86_64_link_hash_entry *) h;
d40d037c
AJ
2185 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2186 {
2187 s = p->sec->output_section;
2188 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2189 break;
2190 }
2191
2192 /* If we didn't find any dynamic relocs in read-only sections, then
2193 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2194 if (p == NULL)
2195 {
f5385ebf 2196 h->non_got_ref = 0;
d40d037c
AJ
2197 return TRUE;
2198 }
c434dee6
AJ
2199 }
2200
70256ad8 2201 /* We must allocate the symbol in our .dynbss section, which will
407443a3 2202 become part of the .bss section of the executable. There will be
70256ad8
AJ
2203 an entry for this symbol in the .dynsym section. The dynamic
2204 object will contain position independent code, so all references
2205 from the dynamic object to this symbol will go through the global
2206 offset table. The dynamic linker will use the .dynsym entry to
2207 determine the address it must put in the global offset table, so
2208 both the dynamic object and the regular object will refer to the
2209 same memory location for the variable. */
2210
351f65ca 2211 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2212 if (htab == NULL)
2213 return FALSE;
70256ad8
AJ
2214
2215 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
2216 to copy the initial value out of the dynamic object and into the
cedb70c5 2217 runtime process image. */
1d7e9d18 2218 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
70256ad8 2219 {
351f65ca
L
2220 const struct elf_backend_data *bed;
2221 bed = get_elf_backend_data (info->output_bfd);
2222 htab->srelbss->size += bed->s->sizeof_rela;
f5385ebf 2223 h->needs_copy = 1;
70256ad8
AJ
2224 }
2225
c434dee6 2226 s = htab->sdynbss;
70256ad8 2227
027297b7 2228 return _bfd_elf_adjust_dynamic_copy (h, s);
70256ad8
AJ
2229}
2230
c434dee6
AJ
2231/* Allocate space in .plt, .got and associated reloc sections for
2232 dynamic relocs. */
2233
b34976b6 2234static bfd_boolean
351f65ca 2235elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
2236{
2237 struct bfd_link_info *info;
351f65ca
L
2238 struct elf_x86_64_link_hash_table *htab;
2239 struct elf_x86_64_link_hash_entry *eh;
e03a8ed8 2240 struct elf_dyn_relocs *p;
351f65ca 2241 const struct elf_backend_data *bed;
eed180f8 2242 unsigned int plt_entry_size;
c434dee6 2243
e92d460e 2244 if (h->root.type == bfd_link_hash_indirect)
b34976b6 2245 return TRUE;
c434dee6 2246
351f65ca 2247 eh = (struct elf_x86_64_link_hash_entry *) h;
e92d460e 2248
c434dee6 2249 info = (struct bfd_link_info *) inf;
351f65ca 2250 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2251 if (htab == NULL)
2252 return FALSE;
351f65ca 2253 bed = get_elf_backend_data (info->output_bfd);
eed180f8 2254 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
c434dee6 2255
cbe950e9
L
2256 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2257 here if it is defined and referenced in a non-shared object. */
2258 if (h->type == STT_GNU_IFUNC
2259 && h->def_regular)
e03a8ed8
L
2260 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2261 &eh->dyn_relocs,
eed180f8 2262 plt_entry_size,
e03a8ed8 2263 GOT_ENTRY_SIZE);
cbe950e9
L
2264 else if (htab->elf.dynamic_sections_created
2265 && h->plt.refcount > 0)
c434dee6
AJ
2266 {
2267 /* Make sure this symbol is output as a dynamic symbol.
2268 Undefined weak syms won't yet be marked as dynamic. */
2269 if (h->dynindx == -1
f5385ebf 2270 && !h->forced_local)
c434dee6 2271 {
c152c796 2272 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2273 return FALSE;
c434dee6
AJ
2274 }
2275
27482721
AJ
2276 if (info->shared
2277 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
c434dee6 2278 {
6de2ae4a 2279 asection *s = htab->elf.splt;
c434dee6
AJ
2280
2281 /* If this is the first .plt entry, make room for the special
2282 first entry. */
eea6121a 2283 if (s->size == 0)
eed180f8 2284 s->size += plt_entry_size;
c434dee6 2285
eea6121a 2286 h->plt.offset = s->size;
c434dee6
AJ
2287
2288 /* If this symbol is not defined in a regular file, and we are
2289 not generating a shared library, then set the symbol to this
2290 location in the .plt. This is required to make function
2291 pointers compare as equal between the normal executable and
2292 the shared library. */
2293 if (! info->shared
f5385ebf 2294 && !h->def_regular)
c434dee6
AJ
2295 {
2296 h->root.u.def.section = s;
2297 h->root.u.def.value = h->plt.offset;
2298 }
2299
2300 /* Make room for this entry. */
eed180f8 2301 s->size += plt_entry_size;
c434dee6
AJ
2302
2303 /* We also need to make an entry in the .got.plt section, which
2304 will be placed in the .got section by the linker script. */
6de2ae4a 2305 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
c434dee6
AJ
2306
2307 /* We also need to make an entry in the .rela.plt section. */
351f65ca 2308 htab->elf.srelplt->size += bed->s->sizeof_rela;
6de2ae4a 2309 htab->elf.srelplt->reloc_count++;
c434dee6
AJ
2310 }
2311 else
2312 {
2313 h->plt.offset = (bfd_vma) -1;
f5385ebf 2314 h->needs_plt = 0;
c434dee6
AJ
2315 }
2316 }
2317 else
2318 {
2319 h->plt.offset = (bfd_vma) -1;
f5385ebf 2320 h->needs_plt = 0;
c434dee6
AJ
2321 }
2322
67a4f2b7
AO
2323 eh->tlsdesc_got = (bfd_vma) -1;
2324
bffbf940
JJ
2325 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
2326 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
2327 if (h->got.refcount > 0
1d85728f 2328 && info->executable
bffbf940 2329 && h->dynindx == -1
351f65ca 2330 && elf_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
d8045f23
NC
2331 {
2332 h->got.offset = (bfd_vma) -1;
2333 }
bffbf940 2334 else if (h->got.refcount > 0)
c434dee6
AJ
2335 {
2336 asection *s;
b34976b6 2337 bfd_boolean dyn;
351f65ca 2338 int tls_type = elf_x86_64_hash_entry (h)->tls_type;
c434dee6
AJ
2339
2340 /* Make sure this symbol is output as a dynamic symbol.
2341 Undefined weak syms won't yet be marked as dynamic. */
2342 if (h->dynindx == -1
f5385ebf 2343 && !h->forced_local)
c434dee6 2344 {
c152c796 2345 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2346 return FALSE;
c434dee6
AJ
2347 }
2348
67a4f2b7
AO
2349 if (GOT_TLS_GDESC_P (tls_type))
2350 {
6de2ae4a 2351 eh->tlsdesc_got = htab->elf.sgotplt->size
351f65ca 2352 - elf_x86_64_compute_jump_table_size (htab);
6de2ae4a 2353 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
67a4f2b7
AO
2354 h->got.offset = (bfd_vma) -2;
2355 }
2356 if (! GOT_TLS_GDESC_P (tls_type)
2357 || GOT_TLS_GD_P (tls_type))
2358 {
6de2ae4a 2359 s = htab->elf.sgot;
67a4f2b7
AO
2360 h->got.offset = s->size;
2361 s->size += GOT_ENTRY_SIZE;
2362 if (GOT_TLS_GD_P (tls_type))
2363 s->size += GOT_ENTRY_SIZE;
2364 }
c434dee6 2365 dyn = htab->elf.dynamic_sections_created;
bffbf940
JJ
2366 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2367 and two if global.
2368 R_X86_64_GOTTPOFF needs one dynamic relocation. */
67a4f2b7 2369 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
bffbf940 2370 || tls_type == GOT_TLS_IE)
351f65ca 2371 htab->elf.srelgot->size += bed->s->sizeof_rela;
67a4f2b7 2372 else if (GOT_TLS_GD_P (tls_type))
351f65ca 2373 htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
67a4f2b7
AO
2374 else if (! GOT_TLS_GDESC_P (tls_type)
2375 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2376 || h->root.type != bfd_link_hash_undefweak)
27482721
AJ
2377 && (info->shared
2378 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
351f65ca 2379 htab->elf.srelgot->size += bed->s->sizeof_rela;
67a4f2b7
AO
2380 if (GOT_TLS_GDESC_P (tls_type))
2381 {
351f65ca 2382 htab->elf.srelplt->size += bed->s->sizeof_rela;
67a4f2b7
AO
2383 htab->tlsdesc_plt = (bfd_vma) -1;
2384 }
c434dee6
AJ
2385 }
2386 else
2387 h->got.offset = (bfd_vma) -1;
2388
c434dee6 2389 if (eh->dyn_relocs == NULL)
b34976b6 2390 return TRUE;
c434dee6
AJ
2391
2392 /* In the shared -Bsymbolic case, discard space allocated for
2393 dynamic pc-relative relocs against symbols which turn out to be
2394 defined in regular objects. For the normal shared case, discard
2395 space for pc-relative relocs that have become local due to symbol
2396 visibility changes. */
2397
2398 if (info->shared)
2399 {
27482721
AJ
2400 /* Relocs that use pc_count are those that appear on a call
2401 insn, or certain REL relocs that can generated via assembly.
2402 We want calls to protected symbols to resolve directly to the
2403 function rather than going via the plt. If people want
2404 function pointer comparisons to work as expected then they
2405 should avoid writing weird assembly. */
2406 if (SYMBOL_CALLS_LOCAL (info, h))
c434dee6 2407 {
e03a8ed8 2408 struct elf_dyn_relocs **pp;
c434dee6
AJ
2409
2410 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2411 {
2412 p->count -= p->pc_count;
2413 p->pc_count = 0;
2414 if (p->count == 0)
2415 *pp = p->next;
2416 else
2417 pp = &p->next;
2418 }
2419 }
4e795f50
AM
2420
2421 /* Also discard relocs on undefined weak syms with non-default
2422 visibility. */
22d606e9 2423 if (eh->dyn_relocs != NULL
4e795f50 2424 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
2425 {
2426 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2427 eh->dyn_relocs = NULL;
2428
2429 /* Make sure undefined weak symbols are output as a dynamic
2430 symbol in PIEs. */
2431 else if (h->dynindx == -1
d8045f23
NC
2432 && ! h->forced_local
2433 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2434 return FALSE;
22d606e9 2435 }
cbe950e9 2436
d8045f23 2437 }
d40d037c 2438 else if (ELIMINATE_COPY_RELOCS)
c434dee6
AJ
2439 {
2440 /* For the non-shared case, discard space for relocs against
2441 symbols which turn out to need copy relocs or are not
2442 dynamic. */
2443
f5385ebf
AM
2444 if (!h->non_got_ref
2445 && ((h->def_dynamic
2446 && !h->def_regular)
c434dee6
AJ
2447 || (htab->elf.dynamic_sections_created
2448 && (h->root.type == bfd_link_hash_undefweak
2449 || h->root.type == bfd_link_hash_undefined))))
2450 {
2451 /* Make sure this symbol is output as a dynamic symbol.
2452 Undefined weak syms won't yet be marked as dynamic. */
2453 if (h->dynindx == -1
d8045f23
NC
2454 && ! h->forced_local
2455 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2456 return FALSE;
c434dee6
AJ
2457
2458 /* If that succeeded, we know we'll be keeping all the
2459 relocs. */
2460 if (h->dynindx != -1)
2461 goto keep;
2462 }
2463
2464 eh->dyn_relocs = NULL;
2465
2466 keep: ;
2467 }
2468
2469 /* Finally, allocate space. */
2470 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2471 {
e7c33416
NC
2472 asection * sreloc;
2473
cbe950e9 2474 sreloc = elf_section_data (p->sec)->sreloc;
e7c33416
NC
2475
2476 BFD_ASSERT (sreloc != NULL);
2477
351f65ca 2478 sreloc->size += p->count * bed->s->sizeof_rela;
c434dee6
AJ
2479 }
2480
b34976b6 2481 return TRUE;
c434dee6
AJ
2482}
2483
c25bc9fc
L
2484/* Allocate space in .plt, .got and associated reloc sections for
2485 local dynamic relocs. */
2486
2487static bfd_boolean
351f65ca 2488elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
c25bc9fc
L
2489{
2490 struct elf_link_hash_entry *h
2491 = (struct elf_link_hash_entry *) *slot;
2492
2493 if (h->type != STT_GNU_IFUNC
2494 || !h->def_regular
2495 || !h->ref_regular
2496 || !h->forced_local
2497 || h->root.type != bfd_link_hash_defined)
2498 abort ();
2499
351f65ca 2500 return elf_x86_64_allocate_dynrelocs (h, inf);
c25bc9fc
L
2501}
2502
c434dee6
AJ
2503/* Find any dynamic relocs that apply to read-only sections. */
2504
b34976b6 2505static bfd_boolean
351f65ca
L
2506elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h,
2507 void * inf)
c434dee6 2508{
351f65ca 2509 struct elf_x86_64_link_hash_entry *eh;
e03a8ed8 2510 struct elf_dyn_relocs *p;
c434dee6 2511
aa715242
L
2512 /* Skip local IFUNC symbols. */
2513 if (h->forced_local && h->type == STT_GNU_IFUNC)
2514 return TRUE;
2515
351f65ca 2516 eh = (struct elf_x86_64_link_hash_entry *) h;
c434dee6
AJ
2517 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2518 {
2519 asection *s = p->sec->output_section;
2520
2521 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2522 {
2523 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2524
2525 info->flags |= DF_TEXTREL;
2526
b70321a2
L
2527 if (info->warn_shared_textrel && info->shared)
2528 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'.\n"),
2529 p->sec->owner, h->root.root.string,
2530 p->sec);
2531
c434dee6 2532 /* Not an error, just cut short the traversal. */
b34976b6 2533 return FALSE;
c434dee6
AJ
2534 }
2535 }
b34976b6 2536 return TRUE;
c434dee6
AJ
2537}
2538
daa67607
L
2539/* Convert
2540 mov foo@GOTPCREL(%rip), %reg
2541 to
2542 lea foo(%rip), %reg
2543 with the local symbol, foo. */
2544
2545static bfd_boolean
2546elf_x86_64_convert_mov_to_lea (bfd *abfd, asection *sec,
2547 struct bfd_link_info *link_info)
2548{
2549 Elf_Internal_Shdr *symtab_hdr;
2550 Elf_Internal_Rela *internal_relocs;
2551 Elf_Internal_Rela *irel, *irelend;
2552 bfd_byte *contents;
2553 struct elf_x86_64_link_hash_table *htab;
2554 bfd_boolean changed_contents;
2555 bfd_boolean changed_relocs;
2556 bfd_signed_vma *local_got_refcounts;
2557
2558 /* Don't even try to convert non-ELF outputs. */
2559 if (!is_elf_hash_table (link_info->hash))
2560 return FALSE;
2561
fbdc86d9 2562 /* Nothing to do if there are no codes, no relocations or no output. */
daa67607 2563 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
fbdc86d9
L
2564 || sec->reloc_count == 0
2565 || discarded_section (sec))
daa67607
L
2566 return TRUE;
2567
2568 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2569
2570 /* Load the relocations for this section. */
2571 internal_relocs = (_bfd_elf_link_read_relocs
2572 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
2573 link_info->keep_memory));
2574 if (internal_relocs == NULL)
2575 return FALSE;
2576
2577 htab = elf_x86_64_hash_table (link_info);
2578 changed_contents = FALSE;
2579 changed_relocs = FALSE;
2580 local_got_refcounts = elf_local_got_refcounts (abfd);
2581
2582 /* Get the section contents. */
2583 if (elf_section_data (sec)->this_hdr.contents != NULL)
2584 contents = elf_section_data (sec)->this_hdr.contents;
2585 else
2586 {
2587 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2588 goto error_return;
2589 }
2590
2591 irelend = internal_relocs + sec->reloc_count;
2592 for (irel = internal_relocs; irel < irelend; irel++)
2593 {
2594 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
2595 unsigned int r_symndx = htab->r_sym (irel->r_info);
2596 unsigned int indx;
2597 struct elf_link_hash_entry *h;
2598
2599 if (r_type != R_X86_64_GOTPCREL)
2600 continue;
2601
2602 /* Get the symbol referred to by the reloc. */
2603 if (r_symndx < symtab_hdr->sh_info)
2604 {
2605 Elf_Internal_Sym *isym;
2606
2607 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2608 abfd, r_symndx);
2609
2610 /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation. */
2611 if (ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC
2612 && bfd_get_8 (input_bfd,
2613 contents + irel->r_offset - 2) == 0x8b)
2614 {
2615 bfd_put_8 (output_bfd, 0x8d,
2616 contents + irel->r_offset - 2);
2617 irel->r_info = htab->r_info (r_symndx, R_X86_64_PC32);
2618 if (local_got_refcounts != NULL
2619 && local_got_refcounts[r_symndx] > 0)
2620 local_got_refcounts[r_symndx] -= 1;
2621 changed_contents = TRUE;
2622 changed_relocs = TRUE;
2623 }
2624 continue;
2625 }
2626
2627 indx = r_symndx - symtab_hdr->sh_info;
2628 h = elf_sym_hashes (abfd)[indx];
2629 BFD_ASSERT (h != NULL);
2630
2631 while (h->root.type == bfd_link_hash_indirect
2632 || h->root.type == bfd_link_hash_warning)
2633 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2634
3f65f599
L
2635 /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation. We also
2636 avoid optimizing _DYNAMIC since ld.so may use its link-time
2637 address. */
daa67607
L
2638 if (h->def_regular
2639 && h->type != STT_GNU_IFUNC
9637f6ef 2640 && h != htab->elf.hdynamic
daa67607
L
2641 && SYMBOL_REFERENCES_LOCAL (link_info, h)
2642 && bfd_get_8 (input_bfd,
2643 contents + irel->r_offset - 2) == 0x8b)
2644 {
2645 bfd_put_8 (output_bfd, 0x8d,
2646 contents + irel->r_offset - 2);
2647 irel->r_info = htab->r_info (r_symndx, R_X86_64_PC32);
2648 if (h->got.refcount > 0)
2649 h->got.refcount -= 1;
2650 changed_contents = TRUE;
2651 changed_relocs = TRUE;
2652 }
2653 }
2654
2655 if (contents != NULL
2656 && elf_section_data (sec)->this_hdr.contents != contents)
2657 {
2658 if (!changed_contents && !link_info->keep_memory)
2659 free (contents);
2660 else
2661 {
2662 /* Cache the section contents for elf_link_input_bfd. */
2663 elf_section_data (sec)->this_hdr.contents = contents;
2664 }
2665 }
2666
2667 if (elf_section_data (sec)->relocs != internal_relocs)
2668 {
2669 if (!changed_relocs)
2670 free (internal_relocs);
2671 else
2672 elf_section_data (sec)->relocs = internal_relocs;
2673 }
2674
2675 return TRUE;
2676
2677 error_return:
2678 if (contents != NULL
2679 && elf_section_data (sec)->this_hdr.contents != contents)
2680 free (contents);
2681 if (internal_relocs != NULL
2682 && elf_section_data (sec)->relocs != internal_relocs)
2683 free (internal_relocs);
2684 return FALSE;
2685}
2686
70256ad8
AJ
2687/* Set the sizes of the dynamic sections. */
2688
b34976b6 2689static bfd_boolean
351f65ca
L
2690elf_x86_64_size_dynamic_sections (bfd *output_bfd,
2691 struct bfd_link_info *info)
70256ad8 2692{
351f65ca 2693 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
2694 bfd *dynobj;
2695 asection *s;
b34976b6 2696 bfd_boolean relocs;
c434dee6 2697 bfd *ibfd;
351f65ca 2698 const struct elf_backend_data *bed;
70256ad8 2699
351f65ca 2700 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2701 if (htab == NULL)
2702 return FALSE;
351f65ca 2703 bed = get_elf_backend_data (output_bfd);
4dfe6ac6 2704
c434dee6
AJ
2705 dynobj = htab->elf.dynobj;
2706 if (dynobj == NULL)
2707 abort ();
70256ad8 2708
c434dee6 2709 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
2710 {
2711 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 2712 if (info->executable)
70256ad8 2713 {
3d4d4302 2714 s = bfd_get_linker_section (dynobj, ".interp");
c434dee6
AJ
2715 if (s == NULL)
2716 abort ();
351f65ca
L
2717 s->size = htab->dynamic_interpreter_size;
2718 s->contents = (unsigned char *) htab->dynamic_interpreter;
70256ad8
AJ
2719 }
2720 }
70256ad8 2721
c434dee6
AJ
2722 /* Set up .got offsets for local syms, and space for local dynamic
2723 relocs. */
2724 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
70256ad8 2725 {
c434dee6
AJ
2726 bfd_signed_vma *local_got;
2727 bfd_signed_vma *end_local_got;
bffbf940 2728 char *local_tls_type;
67a4f2b7 2729 bfd_vma *local_tlsdesc_gotent;
c434dee6
AJ
2730 bfd_size_type locsymcount;
2731 Elf_Internal_Shdr *symtab_hdr;
2732 asection *srel;
70256ad8 2733
0ffa91dd 2734 if (! is_x86_64_elf (ibfd))
70256ad8
AJ
2735 continue;
2736
c434dee6 2737 for (s = ibfd->sections; s != NULL; s = s->next)
70256ad8 2738 {
e03a8ed8 2739 struct elf_dyn_relocs *p;
c434dee6 2740
daa67607
L
2741 if (!elf_x86_64_convert_mov_to_lea (ibfd, s, info))
2742 return FALSE;
2743
e03a8ed8 2744 for (p = (struct elf_dyn_relocs *)
e81d3500 2745 (elf_section_data (s)->local_dynrel);
c434dee6
AJ
2746 p != NULL;
2747 p = p->next)
70256ad8 2748 {
c434dee6
AJ
2749 if (!bfd_is_abs_section (p->sec)
2750 && bfd_is_abs_section (p->sec->output_section))
2751 {
2752 /* Input section has been discarded, either because
2753 it is a copy of a linkonce section or due to
2754 linker script /DISCARD/, so we'll be discarding
2755 the relocs too. */
2756 }
2757 else if (p->count != 0)
2758 {
2759 srel = elf_section_data (p->sec)->sreloc;
351f65ca 2760 srel->size += p->count * bed->s->sizeof_rela;
4b819e1f
L
2761 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2762 && (info->flags & DF_TEXTREL) == 0)
b70321a2
L
2763 {
2764 info->flags |= DF_TEXTREL;
2765 if (info->warn_shared_textrel && info->shared)
2766 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'.\n"),
2767 p->sec->owner, p->sec);
b70321a2 2768 }
c434dee6 2769 }
70256ad8
AJ
2770 }
2771 }
c434dee6
AJ
2772
2773 local_got = elf_local_got_refcounts (ibfd);
2774 if (!local_got)
2775 continue;
2776
0ffa91dd 2777 symtab_hdr = &elf_symtab_hdr (ibfd);
c434dee6
AJ
2778 locsymcount = symtab_hdr->sh_info;
2779 end_local_got = local_got + locsymcount;
351f65ca
L
2780 local_tls_type = elf_x86_64_local_got_tls_type (ibfd);
2781 local_tlsdesc_gotent = elf_x86_64_local_tlsdesc_gotent (ibfd);
6de2ae4a
L
2782 s = htab->elf.sgot;
2783 srel = htab->elf.srelgot;
67a4f2b7
AO
2784 for (; local_got < end_local_got;
2785 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
70256ad8 2786 {
67a4f2b7 2787 *local_tlsdesc_gotent = (bfd_vma) -1;
c434dee6 2788 if (*local_got > 0)
70256ad8 2789 {
67a4f2b7
AO
2790 if (GOT_TLS_GDESC_P (*local_tls_type))
2791 {
6de2ae4a 2792 *local_tlsdesc_gotent = htab->elf.sgotplt->size
351f65ca 2793 - elf_x86_64_compute_jump_table_size (htab);
6de2ae4a 2794 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
67a4f2b7
AO
2795 *local_got = (bfd_vma) -2;
2796 }
2797 if (! GOT_TLS_GDESC_P (*local_tls_type)
2798 || GOT_TLS_GD_P (*local_tls_type))
2799 {
2800 *local_got = s->size;
2801 s->size += GOT_ENTRY_SIZE;
2802 if (GOT_TLS_GD_P (*local_tls_type))
2803 s->size += GOT_ENTRY_SIZE;
2804 }
bffbf940 2805 if (info->shared
67a4f2b7 2806 || GOT_TLS_GD_ANY_P (*local_tls_type)
bffbf940 2807 || *local_tls_type == GOT_TLS_IE)
67a4f2b7
AO
2808 {
2809 if (GOT_TLS_GDESC_P (*local_tls_type))
2810 {
6de2ae4a 2811 htab->elf.srelplt->size
351f65ca 2812 += bed->s->sizeof_rela;
67a4f2b7
AO
2813 htab->tlsdesc_plt = (bfd_vma) -1;
2814 }
2815 if (! GOT_TLS_GDESC_P (*local_tls_type)
2816 || GOT_TLS_GD_P (*local_tls_type))
351f65ca 2817 srel->size += bed->s->sizeof_rela;
67a4f2b7 2818 }
70256ad8
AJ
2819 }
2820 else
c434dee6
AJ
2821 *local_got = (bfd_vma) -1;
2822 }
2823 }
70256ad8 2824
bffbf940
JJ
2825 if (htab->tls_ld_got.refcount > 0)
2826 {
2827 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2828 relocs. */
6de2ae4a
L
2829 htab->tls_ld_got.offset = htab->elf.sgot->size;
2830 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
351f65ca 2831 htab->elf.srelgot->size += bed->s->sizeof_rela;
bffbf940
JJ
2832 }
2833 else
2834 htab->tls_ld_got.offset = -1;
2835
c434dee6
AJ
2836 /* Allocate global sym .plt and .got entries, and space for global
2837 sym dynamic relocs. */
351f65ca 2838 elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
eb4ff4d6 2839 info);
c434dee6 2840
c25bc9fc
L
2841 /* Allocate .plt and .got entries, and space for local symbols. */
2842 htab_traverse (htab->loc_hash_table,
351f65ca 2843 elf_x86_64_allocate_local_dynrelocs,
c25bc9fc
L
2844 info);
2845
67a4f2b7
AO
2846 /* For every jump slot reserved in the sgotplt, reloc_count is
2847 incremented. However, when we reserve space for TLS descriptors,
2848 it's not incremented, so in order to compute the space reserved
2849 for them, it suffices to multiply the reloc count by the jump
e1f98742
L
2850 slot size.
2851
2852 PR ld/13302: We start next_irelative_index at the end of .rela.plt
2853 so that R_X86_64_IRELATIVE entries come last. */
6de2ae4a 2854 if (htab->elf.srelplt)
e1f98742
L
2855 {
2856 htab->sgotplt_jump_table_size
2857 = elf_x86_64_compute_jump_table_size (htab);
2858 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2859 }
2860 else if (htab->elf.irelplt)
2861 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
67a4f2b7
AO
2862
2863 if (htab->tlsdesc_plt)
2864 {
2865 /* If we're not using lazy TLS relocations, don't generate the
2866 PLT and GOT entries they require. */
2867 if ((info->flags & DF_BIND_NOW))
2868 htab->tlsdesc_plt = 0;
2869 else
2870 {
6de2ae4a
L
2871 htab->tlsdesc_got = htab->elf.sgot->size;
2872 htab->elf.sgot->size += GOT_ENTRY_SIZE;
67a4f2b7
AO
2873 /* Reserve room for the initial entry.
2874 FIXME: we could probably do away with it in this case. */
6de2ae4a 2875 if (htab->elf.splt->size == 0)
eed180f8 2876 htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
6de2ae4a 2877 htab->tlsdesc_plt = htab->elf.splt->size;
eed180f8 2878 htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
67a4f2b7
AO
2879 }
2880 }
2881
a7b16ceb
L
2882 if (htab->elf.sgotplt)
2883 {
2884 /* Don't allocate .got.plt section if there are no GOT nor PLT
eed180f8 2885 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
9d4b2dba
L
2886 if ((htab->elf.hgot == NULL
2887 || !htab->elf.hgot->ref_regular_nonweak)
e28df02b
L
2888 && (htab->elf.sgotplt->size
2889 == get_elf_backend_data (output_bfd)->got_header_size)
a7b16ceb
L
2890 && (htab->elf.splt == NULL
2891 || htab->elf.splt->size == 0)
2892 && (htab->elf.sgot == NULL
2893 || htab->elf.sgot->size == 0)
2894 && (htab->elf.iplt == NULL
2895 || htab->elf.iplt->size == 0)
2896 && (htab->elf.igotplt == NULL
2897 || htab->elf.igotplt->size == 0))
2898 htab->elf.sgotplt->size = 0;
2899 }
2900
9a2a56cc
AM
2901 if (htab->plt_eh_frame != NULL
2902 && htab->elf.splt != NULL
2903 && htab->elf.splt->size != 0
2904 && !bfd_is_abs_section (htab->elf.splt->output_section)
2905 && _bfd_elf_eh_frame_present (info))
2906 {
2907 const struct elf_x86_64_backend_data *arch_data
2908 = (const struct elf_x86_64_backend_data *) bed->arch_data;
2909 htab->plt_eh_frame->size = arch_data->eh_frame_plt_size;
2910 }
2911
c434dee6
AJ
2912 /* We now have determined the sizes of the various dynamic sections.
2913 Allocate memory for them. */
b34976b6 2914 relocs = FALSE;
c434dee6
AJ
2915 for (s = dynobj->sections; s != NULL; s = s->next)
2916 {
2917 if ((s->flags & SEC_LINKER_CREATED) == 0)
2918 continue;
2919
6de2ae4a
L
2920 if (s == htab->elf.splt
2921 || s == htab->elf.sgot
2922 || s == htab->elf.sgotplt
2923 || s == htab->elf.iplt
2924 || s == htab->elf.igotplt
9a2a56cc 2925 || s == htab->plt_eh_frame
75ff4589 2926 || s == htab->sdynbss)
c434dee6
AJ
2927 {
2928 /* Strip this section if we don't need it; see the
2929 comment below. */
2930 }
0112cd26 2931 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
c434dee6 2932 {
6de2ae4a 2933 if (s->size != 0 && s != htab->elf.srelplt)
b34976b6 2934 relocs = TRUE;
c434dee6
AJ
2935
2936 /* We use the reloc_count field as a counter if we need
2937 to copy relocs into the output file. */
6de2ae4a 2938 if (s != htab->elf.srelplt)
67a4f2b7 2939 s->reloc_count = 0;
70256ad8 2940 }
c434dee6 2941 else
70256ad8
AJ
2942 {
2943 /* It's not one of our sections, so don't allocate space. */
2944 continue;
2945 }
2946
eea6121a 2947 if (s->size == 0)
70256ad8 2948 {
c434dee6
AJ
2949 /* If we don't need this section, strip it from the
2950 output file. This is mostly to handle .rela.bss and
2951 .rela.plt. We must create both sections in
2952 create_dynamic_sections, because they must be created
2953 before the linker maps input sections to output
2954 sections. The linker does that before
2955 adjust_dynamic_symbol is called, and it is that
2956 function which decides whether anything needs to go
2957 into these sections. */
2958
8423293d 2959 s->flags |= SEC_EXCLUDE;
70256ad8
AJ
2960 continue;
2961 }
2962
c456f082
AM
2963 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2964 continue;
2965
70256ad8
AJ
2966 /* Allocate memory for the section contents. We use bfd_zalloc
2967 here in case unused entries are not reclaimed before the
2968 section's contents are written out. This should not happen,
2969 but this way if it does, we get a R_X86_64_NONE reloc instead
2970 of garbage. */
eea6121a 2971 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c434dee6 2972 if (s->contents == NULL)
b34976b6 2973 return FALSE;
70256ad8
AJ
2974 }
2975
e41b3a13 2976 if (htab->plt_eh_frame != NULL
9a2a56cc
AM
2977 && htab->plt_eh_frame->contents != NULL)
2978 {
2979 const struct elf_x86_64_backend_data *arch_data
2980 = (const struct elf_x86_64_backend_data *) bed->arch_data;
2981
2982 memcpy (htab->plt_eh_frame->contents,
2983 arch_data->eh_frame_plt, htab->plt_eh_frame->size);
2984 bfd_put_32 (dynobj, htab->elf.splt->size,
2985 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
2986 }
e41b3a13 2987
c434dee6 2988 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
2989 {
2990 /* Add some entries to the .dynamic section. We fill in the
351f65ca 2991 values later, in elf_x86_64_finish_dynamic_sections, but we
70256ad8 2992 must add the entries now so that we get the correct size for
407443a3 2993 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8 2994 dynamic linker and used by the debugger. */
dc810e39 2995#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2996 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 2997
36af4a4e 2998 if (info->executable)
70256ad8 2999 {
dc810e39 3000 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 3001 return FALSE;
70256ad8
AJ
3002 }
3003
6de2ae4a 3004 if (htab->elf.splt->size != 0)
70256ad8 3005 {
dc810e39
AM
3006 if (!add_dynamic_entry (DT_PLTGOT, 0)
3007 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3008 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3009 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 3010 return FALSE;
67a4f2b7
AO
3011
3012 if (htab->tlsdesc_plt
3013 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
3014 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
3015 return FALSE;
70256ad8
AJ
3016 }
3017
3018 if (relocs)
3019 {
dc810e39
AM
3020 if (!add_dynamic_entry (DT_RELA, 0)
3021 || !add_dynamic_entry (DT_RELASZ, 0)
351f65ca 3022 || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
b34976b6 3023 return FALSE;
70256ad8 3024
c434dee6
AJ
3025 /* If any dynamic relocs apply to a read-only section,
3026 then we need a DT_TEXTREL entry. */
3027 if ((info->flags & DF_TEXTREL) == 0)
eed180f8 3028 elf_link_hash_traverse (&htab->elf,
351f65ca 3029 elf_x86_64_readonly_dynrelocs,
eb4ff4d6 3030 info);
c434dee6
AJ
3031
3032 if ((info->flags & DF_TEXTREL) != 0)
3033 {
3034 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 3035 return FALSE;
c434dee6 3036 }
70256ad8
AJ
3037 }
3038 }
dc810e39 3039#undef add_dynamic_entry
70256ad8 3040
b34976b6 3041 return TRUE;
70256ad8
AJ
3042}
3043
67a4f2b7 3044static bfd_boolean
351f65ca
L
3045elf_x86_64_always_size_sections (bfd *output_bfd,
3046 struct bfd_link_info *info)
67a4f2b7
AO
3047{
3048 asection *tls_sec = elf_hash_table (info)->tls_sec;
3049
3050 if (tls_sec)
3051 {
3052 struct elf_link_hash_entry *tlsbase;
3053
3054 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3055 "_TLS_MODULE_BASE_",
3056 FALSE, FALSE, FALSE);
3057
3058 if (tlsbase && tlsbase->type == STT_TLS)
3059 {
351f65ca 3060 struct elf_x86_64_link_hash_table *htab;
67a4f2b7
AO
3061 struct bfd_link_hash_entry *bh = NULL;
3062 const struct elf_backend_data *bed
3063 = get_elf_backend_data (output_bfd);
3064
351f65ca 3065 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3066 if (htab == NULL)
3067 return FALSE;
3068
67a4f2b7
AO
3069 if (!(_bfd_generic_link_add_one_symbol
3070 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3071 tls_sec, 0, NULL, FALSE,
3072 bed->collect, &bh)))
3073 return FALSE;
9f03412a 3074
4dfe6ac6 3075 htab->tls_module_base = bh;
9f03412a 3076
67a4f2b7
AO
3077 tlsbase = (struct elf_link_hash_entry *)bh;
3078 tlsbase->def_regular = 1;
3079 tlsbase->other = STV_HIDDEN;
3080 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
3081 }
3082 }
3083
3084 return TRUE;
3085}
3086
9f03412a
AO
3087/* _TLS_MODULE_BASE_ needs to be treated especially when linking
3088 executables. Rather than setting it to the beginning of the TLS
3089 section, we have to set it to the end. This function may be called
3090 multiple times, it is idempotent. */
3091
3092static void
351f65ca 3093elf_x86_64_set_tls_module_base (struct bfd_link_info *info)
9f03412a 3094{
351f65ca 3095 struct elf_x86_64_link_hash_table *htab;
9f03412a
AO
3096 struct bfd_link_hash_entry *base;
3097
3098 if (!info->executable)
3099 return;
3100
351f65ca 3101 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3102 if (htab == NULL)
3103 return;
9f03412a 3104
4dfe6ac6
NC
3105 base = htab->tls_module_base;
3106 if (base == NULL)
9f03412a
AO
3107 return;
3108
4dfe6ac6 3109 base->u.def.value = htab->elf.tls_size;
9f03412a
AO
3110}
3111
bffbf940
JJ
3112/* Return the base VMA address which should be subtracted from real addresses
3113 when resolving @dtpoff relocation.
3114 This is PT_TLS segment p_vaddr. */
3115
3116static bfd_vma
351f65ca 3117elf_x86_64_dtpoff_base (struct bfd_link_info *info)
bffbf940 3118{
e1918d23
AM
3119 /* If tls_sec is NULL, we should have signalled an error already. */
3120 if (elf_hash_table (info)->tls_sec == NULL)
bffbf940 3121 return 0;
e1918d23 3122 return elf_hash_table (info)->tls_sec->vma;
bffbf940
JJ
3123}
3124
3125/* Return the relocation value for @tpoff relocation
3126 if STT_TLS virtual address is ADDRESS. */
3127
3128static bfd_vma
351f65ca 3129elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 3130{
e1918d23 3131 struct elf_link_hash_table *htab = elf_hash_table (info);
7dc98aea
RO
3132 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3133 bfd_vma static_tls_size;
bffbf940
JJ
3134
3135 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 3136 if (htab->tls_sec == NULL)
bffbf940 3137 return 0;
7dc98aea
RO
3138
3139 /* Consider special static TLS alignment requirements. */
3140 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3141 return address - static_tls_size - htab->tls_sec->vma;
bffbf940
JJ
3142}
3143
90f487df
L
3144/* Is the instruction before OFFSET in CONTENTS a 32bit relative
3145 branch? */
3146
3147static bfd_boolean
3148is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
3149{
3150 /* Opcode Instruction
3151 0xe8 call
3152 0xe9 jump
3153 0x0f 0x8x conditional jump */
3154 return ((offset > 0
3155 && (contents [offset - 1] == 0xe8
3156 || contents [offset - 1] == 0xe9))
3157 || (offset > 1
3158 && contents [offset - 2] == 0x0f
3159 && (contents [offset - 1] & 0xf0) == 0x80));
3160}
3161
8d88c4ca
NC
3162/* Relocate an x86_64 ELF section. */
3163
b34976b6 3164static bfd_boolean
351f65ca
L
3165elf_x86_64_relocate_section (bfd *output_bfd,
3166 struct bfd_link_info *info,
3167 bfd *input_bfd,
3168 asection *input_section,
3169 bfd_byte *contents,
3170 Elf_Internal_Rela *relocs,
3171 Elf_Internal_Sym *local_syms,
3172 asection **local_sections)
8d88c4ca 3173{
351f65ca 3174 struct elf_x86_64_link_hash_table *htab;
8d88c4ca
NC
3175 Elf_Internal_Shdr *symtab_hdr;
3176 struct elf_link_hash_entry **sym_hashes;
3177 bfd_vma *local_got_offsets;
67a4f2b7 3178 bfd_vma *local_tlsdesc_gotents;
c434dee6 3179 Elf_Internal_Rela *rel;
8d88c4ca 3180 Elf_Internal_Rela *relend;
eed180f8 3181 const unsigned int plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
8d88c4ca 3182
0ffa91dd
NC
3183 BFD_ASSERT (is_x86_64_elf (input_bfd));
3184
351f65ca 3185 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3186 if (htab == NULL)
3187 return FALSE;
0ffa91dd 3188 symtab_hdr = &elf_symtab_hdr (input_bfd);
8d88c4ca
NC
3189 sym_hashes = elf_sym_hashes (input_bfd);
3190 local_got_offsets = elf_local_got_offsets (input_bfd);
351f65ca 3191 local_tlsdesc_gotents = elf_x86_64_local_tlsdesc_gotent (input_bfd);
8d88c4ca 3192
351f65ca 3193 elf_x86_64_set_tls_module_base (info);
9f03412a 3194
c434dee6 3195 rel = relocs;
8d88c4ca 3196 relend = relocs + input_section->reloc_count;
c434dee6 3197 for (; rel < relend; rel++)
8d88c4ca 3198 {
bffbf940 3199 unsigned int r_type;
8d88c4ca
NC
3200 reloc_howto_type *howto;
3201 unsigned long r_symndx;
3202 struct elf_link_hash_entry *h;
3203 Elf_Internal_Sym *sym;
3204 asection *sec;
67a4f2b7 3205 bfd_vma off, offplt;
8d88c4ca 3206 bfd_vma relocation;
b34976b6 3207 bfd_boolean unresolved_reloc;
8d88c4ca 3208 bfd_reloc_status_type r;
bffbf940 3209 int tls_type;
cbe950e9 3210 asection *base_got;
1788fc08 3211 bfd_vma st_size;
8d88c4ca 3212
351f65ca 3213 r_type = ELF32_R_TYPE (rel->r_info);
fe4770f4
AJ
3214 if (r_type == (int) R_X86_64_GNU_VTINHERIT
3215 || r_type == (int) R_X86_64_GNU_VTENTRY)
3216 continue;
8d88c4ca 3217
9911c0fc 3218 if (r_type >= (int) R_X86_64_standard)
8da6118f 3219 {
9911c0fc
L
3220 (*_bfd_error_handler)
3221 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3222 input_bfd, input_section, r_type);
8da6118f 3223 bfd_set_error (bfd_error_bad_value);
b34976b6 3224 return FALSE;
8da6118f 3225 }
8d88c4ca 3226
d7921315 3227 if (r_type != (int) R_X86_64_32
eed180f8 3228 || ABI_64_P (output_bfd))
d7921315
L
3229 howto = x86_64_elf_howto_table + r_type;
3230 else
3231 howto = (x86_64_elf_howto_table
3232 + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
351f65ca 3233 r_symndx = htab->r_sym (rel->r_info);
8d88c4ca
NC
3234 h = NULL;
3235 sym = NULL;
3236 sec = NULL;
b34976b6 3237 unresolved_reloc = FALSE;
8d88c4ca 3238 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
3239 {
3240 sym = local_syms + r_symndx;
3241 sec = local_sections[r_symndx];
c434dee6 3242
c25bc9fc
L
3243 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
3244 &sec, rel);
1788fc08 3245 st_size = sym->st_size;
c25bc9fc
L
3246
3247 /* Relocate against local STT_GNU_IFUNC symbol. */
1f85278f 3248 if (!info->relocatable
351f65ca 3249 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc 3250 {
351f65ca
L
3251 h = elf_x86_64_get_local_sym_hash (htab, input_bfd,
3252 rel, FALSE);
c25bc9fc
L
3253 if (h == NULL)
3254 abort ();
3255
eed180f8 3256 /* Set STT_GNU_IFUNC symbol value. */
c25bc9fc
L
3257 h->root.u.def.value = sym->st_value;
3258 h->root.u.def.section = sec;
3259 }
8da6118f 3260 }
8d88c4ca 3261 else
8da6118f 3262 {
c9736ba0 3263 bfd_boolean warned ATTRIBUTE_UNUSED;
c434dee6 3264
b2a8e766
AM
3265 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3266 r_symndx, symtab_hdr, sym_hashes,
3267 h, sec, relocation,
3268 unresolved_reloc, warned);
1788fc08 3269 st_size = h->size;
8da6118f 3270 }
ab96bf03 3271
dbaa2011 3272 if (sec != NULL && discarded_section (sec))
0672748a 3273 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 3274 rel, 1, relend, howto, 0, contents);
ab96bf03
AM
3275
3276 if (info->relocatable)
3277 continue;
3278
1788fc08 3279 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
64d25c44 3280 {
1788fc08
L
3281 if (r_type == R_X86_64_64)
3282 {
3283 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
3284 zero-extend it to 64bit if addend is zero. */
3285 r_type = R_X86_64_32;
3286 memset (contents + rel->r_offset + 4, 0, 4);
3287 }
3288 else if (r_type == R_X86_64_SIZE64)
3289 {
3290 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
3291 zero-extend it to 64bit if addend is zero. */
3292 r_type = R_X86_64_SIZE32;
3293 memset (contents + rel->r_offset + 4, 0, 4);
3294 }
64d25c44
L
3295 }
3296
cbe950e9
L
3297 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3298 it here if it is defined in a non-shared object. */
3299 if (h != NULL
3300 && h->type == STT_GNU_IFUNC
3301 && h->def_regular)
3302 {
3303 asection *plt;
3304 bfd_vma plt_index;
4c544807 3305 const char *name;
cbe950e9
L
3306
3307 if ((input_section->flags & SEC_ALLOC) == 0
3308 || h->plt.offset == (bfd_vma) -1)
3309 abort ();
3310
3311 /* STT_GNU_IFUNC symbol must go through PLT. */
6de2ae4a 3312 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
cbe950e9
L
3313 relocation = (plt->output_section->vma
3314 + plt->output_offset + h->plt.offset);
3315
3316 switch (r_type)
3317 {
3318 default:
4c544807
L
3319 if (h->root.root.string)
3320 name = h->root.root.string;
3321 else
3322 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3323 NULL);
cbe950e9
L
3324 (*_bfd_error_handler)
3325 (_("%B: relocation %s against STT_GNU_IFUNC "
3326 "symbol `%s' isn't handled by %s"), input_bfd,
3327 x86_64_elf_howto_table[r_type].name,
4c544807 3328 name, __FUNCTION__);
cbe950e9
L
3329 bfd_set_error (bfd_error_bad_value);
3330 return FALSE;
3331
3332 case R_X86_64_32S:
710ab287 3333 if (info->shared)
cbe950e9 3334 abort ();
710ab287
L
3335 goto do_relocation;
3336
248775ba
L
3337 case R_X86_64_32:
3338 if (ABI_64_P (output_bfd))
3339 goto do_relocation;
17672001 3340 /* FALLTHROUGH */
eed180f8 3341 case R_X86_64_64:
710ab287
L
3342 if (rel->r_addend != 0)
3343 {
4c544807
L
3344 if (h->root.root.string)
3345 name = h->root.root.string;
3346 else
3347 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3348 sym, NULL);
710ab287
L
3349 (*_bfd_error_handler)
3350 (_("%B: relocation %s against STT_GNU_IFUNC "
3351 "symbol `%s' has non-zero addend: %d"),
3352 input_bfd, x86_64_elf_howto_table[r_type].name,
4c544807 3353 name, rel->r_addend);
710ab287
L
3354 bfd_set_error (bfd_error_bad_value);
3355 return FALSE;
3356 }
3357
3358 /* Generate dynamic relcoation only when there is a
c293fa49 3359 non-GOT reference in a shared object. */
710ab287
L
3360 if (info->shared && h->non_got_ref)
3361 {
3362 Elf_Internal_Rela outrel;
710ab287
L
3363 asection *sreloc;
3364
c25bc9fc
L
3365 /* Need a dynamic relocation to get the real function
3366 address. */
710ab287
L
3367 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3368 info,
3369 input_section,
3370 rel->r_offset);
3371 if (outrel.r_offset == (bfd_vma) -1
3372 || outrel.r_offset == (bfd_vma) -2)
3373 abort ();
3374
3375 outrel.r_offset += (input_section->output_section->vma
3376 + input_section->output_offset);
3377
3378 if (h->dynindx == -1
44c4ea11
L
3379 || h->forced_local
3380 || info->executable)
710ab287
L
3381 {
3382 /* This symbol is resolved locally. */
56b8aada
L
3383 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
3384 outrel.r_addend = (h->root.u.def.value
3385 + h->root.u.def.section->output_section->vma
3386 + h->root.u.def.section->output_offset);
710ab287
L
3387 }
3388 else
3389 {
351f65ca 3390 outrel.r_info = htab->r_info (h->dynindx, r_type);
710ab287
L
3391 outrel.r_addend = 0;
3392 }
3393
6de2ae4a 3394 sreloc = htab->elf.irelifunc;
351f65ca 3395 elf_append_rela (output_bfd, sreloc, &outrel);
710ab287
L
3396
3397 /* If this reloc is against an external symbol, we
3398 do not want to fiddle with the addend. Otherwise,
3399 we need to include the symbol value so that it
3400 becomes an addend for the dynamic reloc. For an
3401 internal symbol, we have updated addend. */
56b8aada 3402 continue;
710ab287 3403 }
17672001 3404 /* FALLTHROUGH */
cbe950e9
L
3405 case R_X86_64_PC32:
3406 case R_X86_64_PC64:
3407 case R_X86_64_PLT32:
3408 goto do_relocation;
3409
3410 case R_X86_64_GOTPCREL:
3411 case R_X86_64_GOTPCREL64:
6de2ae4a 3412 base_got = htab->elf.sgot;
cbe950e9
L
3413 off = h->got.offset;
3414
7afd84dc 3415 if (base_got == NULL)
cbe950e9
L
3416 abort ();
3417
7afd84dc 3418 if (off == (bfd_vma) -1)
cbe950e9 3419 {
7afd84dc
L
3420 /* We can't use h->got.offset here to save state, or
3421 even just remember the offset, as finish_dynamic_symbol
3422 would use that as offset into .got. */
cbe950e9 3423
6de2ae4a 3424 if (htab->elf.splt != NULL)
7afd84dc 3425 {
eed180f8 3426 plt_index = h->plt.offset / plt_entry_size - 1;
7afd84dc 3427 off = (plt_index + 3) * GOT_ENTRY_SIZE;
6de2ae4a 3428 base_got = htab->elf.sgotplt;
7afd84dc 3429 }
cbe950e9
L
3430 else
3431 {
eed180f8 3432 plt_index = h->plt.offset / plt_entry_size;
7afd84dc 3433 off = plt_index * GOT_ENTRY_SIZE;
6de2ae4a 3434 base_got = htab->elf.igotplt;
7afd84dc
L
3435 }
3436
3437 if (h->dynindx == -1
3438 || h->forced_local
3439 || info->symbolic)
3440 {
eed180f8 3441 /* This references the local defitionion. We must
7afd84dc 3442 initialize this entry in the global offset table.
eed180f8 3443 Since the offset must always be a multiple of 8,
7afd84dc
L
3444 we use the least significant bit to record
3445 whether we have initialized it already.
3446
3447 When doing a dynamic link, we create a .rela.got
3448 relocation entry to initialize the value. This
3449 is done in the finish_dynamic_symbol routine. */
3450 if ((off & 1) != 0)
3451 off &= ~1;
3452 else
3453 {
3454 bfd_put_64 (output_bfd, relocation,
3455 base_got->contents + off);
3456 /* Note that this is harmless for the GOTPLT64
3457 case, as -1 | 1 still is -1. */
3458 h->got.offset |= 1;
3459 }
cbe950e9
L
3460 }
3461 }
3462
3463 relocation = (base_got->output_section->vma
3464 + base_got->output_offset + off);
3465
cbe950e9
L
3466 goto do_relocation;
3467 }
3468 }
3469
70256ad8
AJ
3470 /* When generating a shared object, the relocations handled here are
3471 copied into the output file to be resolved at run time. */
3472 switch (r_type)
3473 {
3474 case R_X86_64_GOT32:
7b81dfbb 3475 case R_X86_64_GOT64:
70256ad8
AJ
3476 /* Relocation is to the entry for this symbol in the global
3477 offset table. */
70256ad8 3478 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
3479 case R_X86_64_GOTPCREL64:
3480 /* Use global offset table entry as symbol value. */
3481 case R_X86_64_GOTPLT64:
3482 /* This is the same as GOT64 for relocation purposes, but
3483 indicates the existence of a PLT entry. The difficulty is,
3484 that we must calculate the GOT slot offset from the PLT
3485 offset, if this symbol got a PLT entry (it was global).
3486 Additionally if it's computed from the PLT entry, then that
3487 GOT offset is relative to .got.plt, not to .got. */
6de2ae4a 3488 base_got = htab->elf.sgot;
7b81dfbb 3489
6de2ae4a 3490 if (htab->elf.sgot == NULL)
c434dee6 3491 abort ();
053579d7 3492
51e0a107 3493 if (h != NULL)
70256ad8 3494 {
b34976b6 3495 bfd_boolean dyn;
c434dee6
AJ
3496
3497 off = h->got.offset;
7b81dfbb 3498 if (h->needs_plt
eed180f8 3499 && h->plt.offset != (bfd_vma)-1
7b81dfbb
AJ
3500 && off == (bfd_vma)-1)
3501 {
3502 /* We can't use h->got.offset here to save
3503 state, or even just remember the offset, as
3504 finish_dynamic_symbol would use that as offset into
3505 .got. */
eed180f8 3506 bfd_vma plt_index = h->plt.offset / plt_entry_size - 1;
7b81dfbb 3507 off = (plt_index + 3) * GOT_ENTRY_SIZE;
6de2ae4a 3508 base_got = htab->elf.sgotplt;
7b81dfbb
AJ
3509 }
3510
c434dee6 3511 dyn = htab->elf.dynamic_sections_created;
51e0a107 3512
27482721 3513 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
51e0a107 3514 || (info->shared
27482721 3515 && SYMBOL_REFERENCES_LOCAL (info, h))
4bc6e03a
AJ
3516 || (ELF_ST_VISIBILITY (h->other)
3517 && h->root.type == bfd_link_hash_undefweak))
51e0a107
JH
3518 {
3519 /* This is actually a static link, or it is a -Bsymbolic
3520 link and the symbol is defined locally, or the symbol
407443a3 3521 was forced to be local because of a version file. We
51e0a107
JH
3522 must initialize this entry in the global offset table.
3523 Since the offset must always be a multiple of 8, we
3524 use the least significant bit to record whether we
3525 have initialized it already.
3526
3527 When doing a dynamic link, we create a .rela.got
407443a3
AJ
3528 relocation entry to initialize the value. This is
3529 done in the finish_dynamic_symbol routine. */
51e0a107
JH
3530 if ((off & 1) != 0)
3531 off &= ~1;
3532 else
3533 {
3534 bfd_put_64 (output_bfd, relocation,
7b81dfbb
AJ
3535 base_got->contents + off);
3536 /* Note that this is harmless for the GOTPLT64 case,
eed180f8 3537 as -1 | 1 still is -1. */
51e0a107
JH
3538 h->got.offset |= 1;
3539 }
3540 }
053579d7 3541 else
b34976b6 3542 unresolved_reloc = FALSE;
70256ad8 3543 }
51e0a107
JH
3544 else
3545 {
c434dee6
AJ
3546 if (local_got_offsets == NULL)
3547 abort ();
51e0a107
JH
3548
3549 off = local_got_offsets[r_symndx];
3550
3551 /* The offset must always be a multiple of 8. We use
407443a3
AJ
3552 the least significant bit to record whether we have
3553 already generated the necessary reloc. */
51e0a107
JH
3554 if ((off & 1) != 0)
3555 off &= ~1;
3556 else
3557 {
c434dee6 3558 bfd_put_64 (output_bfd, relocation,
7b81dfbb 3559 base_got->contents + off);
51e0a107
JH
3560
3561 if (info->shared)
3562 {
947216bf 3563 asection *s;
51e0a107 3564 Elf_Internal_Rela outrel;
70256ad8 3565
51e0a107
JH
3566 /* We need to generate a R_X86_64_RELATIVE reloc
3567 for the dynamic linker. */
6de2ae4a 3568 s = htab->elf.srelgot;
947216bf 3569 if (s == NULL)
c434dee6 3570 abort ();
51e0a107 3571
7b81dfbb
AJ
3572 outrel.r_offset = (base_got->output_section->vma
3573 + base_got->output_offset
51e0a107 3574 + off);
351f65ca 3575 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
51e0a107 3576 outrel.r_addend = relocation;
351f65ca 3577 elf_append_rela (output_bfd, s, &outrel);
51e0a107
JH
3578 }
3579
3580 local_got_offsets[r_symndx] |= 1;
3581 }
51e0a107 3582 }
6a2bda3f 3583
c434dee6
AJ
3584 if (off >= (bfd_vma) -2)
3585 abort ();
3586
7b81dfbb
AJ
3587 relocation = base_got->output_section->vma
3588 + base_got->output_offset + off;
3589 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
6de2ae4a
L
3590 relocation -= htab->elf.sgotplt->output_section->vma
3591 - htab->elf.sgotplt->output_offset;
c434dee6 3592
70256ad8
AJ
3593 break;
3594
d6ab8113
JB
3595 case R_X86_64_GOTOFF64:
3596 /* Relocation is relative to the start of the global offset
3597 table. */
3598
3599 /* Check to make sure it isn't a protected function symbol
3600 for shared library since it may not be local when used
3601 as function address. */
bdb892b9 3602 if (!info->executable
d6ab8113 3603 && h
bdb892b9 3604 && !SYMBOLIC_BIND (info, h)
d6ab8113
JB
3605 && h->def_regular
3606 && h->type == STT_FUNC
3607 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3608 {
3609 (*_bfd_error_handler)
3610 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3611 input_bfd, h->root.root.string);
3612 bfd_set_error (bfd_error_bad_value);
3613 return FALSE;
3614 }
3615
3616 /* Note that sgot is not involved in this
3617 calculation. We always want the start of .got.plt. If we
3618 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3619 permitted by the ABI, we might have to change this
3620 calculation. */
6de2ae4a
L
3621 relocation -= htab->elf.sgotplt->output_section->vma
3622 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
3623 break;
3624
3625 case R_X86_64_GOTPC32:
7b81dfbb 3626 case R_X86_64_GOTPC64:
d6ab8113 3627 /* Use global offset table as symbol value. */
6de2ae4a
L
3628 relocation = htab->elf.sgotplt->output_section->vma
3629 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
3630 unresolved_reloc = FALSE;
3631 break;
7b81dfbb
AJ
3632
3633 case R_X86_64_PLTOFF64:
3634 /* Relocation is PLT entry relative to GOT. For local
3635 symbols it's the symbol itself relative to GOT. */
eed180f8 3636 if (h != NULL
7b81dfbb
AJ
3637 /* See PLT32 handling. */
3638 && h->plt.offset != (bfd_vma) -1
6de2ae4a 3639 && htab->elf.splt != NULL)
7b81dfbb 3640 {
6de2ae4a
L
3641 relocation = (htab->elf.splt->output_section->vma
3642 + htab->elf.splt->output_offset
7b81dfbb
AJ
3643 + h->plt.offset);
3644 unresolved_reloc = FALSE;
3645 }
3646
6de2ae4a
L
3647 relocation -= htab->elf.sgotplt->output_section->vma
3648 + htab->elf.sgotplt->output_offset;
7b81dfbb 3649 break;
d6ab8113 3650
70256ad8
AJ
3651 case R_X86_64_PLT32:
3652 /* Relocation is to the entry for this symbol in the
3653 procedure linkage table. */
3654
3655 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 3656 without using the procedure linkage table. */
70256ad8
AJ
3657 if (h == NULL)
3658 break;
3659
c434dee6 3660 if (h->plt.offset == (bfd_vma) -1
6de2ae4a 3661 || htab->elf.splt == NULL)
70256ad8
AJ
3662 {
3663 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
3664 happens when statically linking PIC code, or when
3665 using -Bsymbolic. */
70256ad8
AJ
3666 break;
3667 }
3668
6de2ae4a
L
3669 relocation = (htab->elf.splt->output_section->vma
3670 + htab->elf.splt->output_offset
70256ad8 3671 + h->plt.offset);
b34976b6 3672 unresolved_reloc = FALSE;
70256ad8
AJ
3673 break;
3674
1788fc08
L
3675 case R_X86_64_SIZE32:
3676 case R_X86_64_SIZE64:
1788fc08
L
3677 /* Set to symbol size. */
3678 relocation = st_size;
3679 goto direct;
3680
fd8ab9e5
AJ
3681 case R_X86_64_PC8:
3682 case R_X86_64_PC16:
3683 case R_X86_64_PC32:
6610a52d 3684 if (info->shared
ba3bee0b 3685 && (input_section->flags & SEC_ALLOC) != 0
90f487df 3686 && (input_section->flags & SEC_READONLY) != 0
41bed6dd 3687 && h != NULL)
6610a52d 3688 {
41bed6dd
L
3689 bfd_boolean fail = FALSE;
3690 bfd_boolean branch
3691 = (r_type == R_X86_64_PC32
3692 && is_32bit_relative_branch (contents, rel->r_offset));
3693
3694 if (SYMBOL_REFERENCES_LOCAL (info, h))
3695 {
3696 /* Symbol is referenced locally. Make sure it is
3697 defined locally or for a branch. */
3698 fail = !h->def_regular && !branch;
3699 }
90f487df 3700 else
41bed6dd
L
3701 {
3702 /* Symbol isn't referenced locally. We only allow
3703 branch to symbol with non-default visibility. */
3704 fail = (!branch
3705 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3706 }
3707
3708 if (fail)
3709 {
3710 const char *fmt;
3711 const char *v;
3712 const char *pic = "";
3713
3714 switch (ELF_ST_VISIBILITY (h->other))
3715 {
3716 case STV_HIDDEN:
3717 v = _("hidden symbol");
3718 break;
3719 case STV_INTERNAL:
3720 v = _("internal symbol");
3721 break;
3722 case STV_PROTECTED:
3723 v = _("protected symbol");
3724 break;
3725 default:
3726 v = _("symbol");
3727 pic = _("; recompile with -fPIC");
3728 break;
3729 }
3730
3731 if (h->def_regular)
3732 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3733 else
3734 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3735
3736 (*_bfd_error_handler) (fmt, input_bfd,
3737 x86_64_elf_howto_table[r_type].name,
3738 v, h->root.root.string, pic);
3739 bfd_set_error (bfd_error_bad_value);
3740 return FALSE;
3741 }
6610a52d
L
3742 }
3743 /* Fall through. */
3744
70256ad8
AJ
3745 case R_X86_64_8:
3746 case R_X86_64_16:
3747 case R_X86_64_32:
d6ab8113 3748 case R_X86_64_PC64:
6b3db546 3749 case R_X86_64_64:
80643fbc 3750 /* FIXME: The ABI says the linker should make sure the value is
407443a3 3751 the same when it's zeroextended to 64 bit. */
c434dee6 3752
1788fc08 3753direct:
b1e24c02 3754 if ((input_section->flags & SEC_ALLOC) == 0)
c434dee6
AJ
3755 break;
3756
3757 if ((info->shared
4bc6e03a
AJ
3758 && (h == NULL
3759 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3760 || h->root.type != bfd_link_hash_undefweak)
1788fc08
L
3761 && ((! IS_X86_64_PCREL_TYPE (r_type)
3762 && r_type != R_X86_64_SIZE32
3763 && r_type != R_X86_64_SIZE64)
d8045f23 3764 || ! SYMBOL_CALLS_LOCAL (info, h)))
d40d037c
AJ
3765 || (ELIMINATE_COPY_RELOCS
3766 && !info->shared
c434dee6
AJ
3767 && h != NULL
3768 && h->dynindx != -1
f5385ebf
AM
3769 && !h->non_got_ref
3770 && ((h->def_dynamic
3771 && !h->def_regular)
c434dee6 3772 || h->root.type == bfd_link_hash_undefweak
0f88be7a 3773 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
3774 {
3775 Elf_Internal_Rela outrel;
b34976b6 3776 bfd_boolean skip, relocate;
c434dee6 3777 asection *sreloc;
70256ad8
AJ
3778
3779 /* When generating a shared object, these relocations
3780 are copied into the output file to be resolved at run
407443a3 3781 time. */
b34976b6
AM
3782 skip = FALSE;
3783 relocate = FALSE;
70256ad8 3784
c629eae0
JJ
3785 outrel.r_offset =
3786 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 3787 rel->r_offset);
c629eae0 3788 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 3789 skip = TRUE;
0fb19cbc 3790 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 3791 skip = TRUE, relocate = TRUE;
70256ad8
AJ
3792
3793 outrel.r_offset += (input_section->output_section->vma
3794 + input_section->output_offset);
3795
3796 if (skip)
0bb2d96a 3797 memset (&outrel, 0, sizeof outrel);
c434dee6 3798
fd8ab9e5
AJ
3799 /* h->dynindx may be -1 if this symbol was marked to
3800 become local. */
3801 else if (h != NULL
c434dee6 3802 && h->dynindx != -1
d8045f23
NC
3803 && (IS_X86_64_PCREL_TYPE (r_type)
3804 || ! info->shared
3805 || ! SYMBOLIC_BIND (info, h)
3806 || ! h->def_regular))
70256ad8 3807 {
351f65ca 3808 outrel.r_info = htab->r_info (h->dynindx, r_type);
c434dee6 3809 outrel.r_addend = rel->r_addend;
70256ad8
AJ
3810 }
3811 else
3812 {
c434dee6 3813 /* This symbol is local, or marked to become local. */
248775ba 3814 if (r_type == htab->pointer_r_type)
607c0e09 3815 {
b34976b6 3816 relocate = TRUE;
351f65ca 3817 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
607c0e09
AS
3818 outrel.r_addend = relocation + rel->r_addend;
3819 }
64d25c44
L
3820 else if (r_type == R_X86_64_64
3821 && !ABI_64_P (output_bfd))
3822 {
3823 relocate = TRUE;
3824 outrel.r_info = htab->r_info (0,
3825 R_X86_64_RELATIVE64);
3826 outrel.r_addend = relocation + rel->r_addend;
8cf0d2dd
L
3827 /* Check addend overflow. */
3828 if ((outrel.r_addend & 0x80000000)
3829 != (rel->r_addend & 0x80000000))
3830 {
3831 const char *name;
268a8d3a 3832 int addend = rel->r_addend;
8cf0d2dd
L
3833 if (h && h->root.root.string)
3834 name = h->root.root.string;
3835 else
3836 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3837 sym, NULL);
6f2c9068
L
3838 if (addend < 0)
3839 (*_bfd_error_handler)
268a8d3a 3840 (_("%B: addend -0x%x in relocation %s against "
6f2c9068
L
3841 "symbol `%s' at 0x%lx in section `%A' is "
3842 "out of range"),
3843 input_bfd, input_section, addend,
3844 x86_64_elf_howto_table[r_type].name,
3845 name, (unsigned long) rel->r_offset);
3846 else
3847 (*_bfd_error_handler)
268a8d3a 3848 (_("%B: addend 0x%x in relocation %s against "
6f2c9068
L
3849 "symbol `%s' at 0x%lx in section `%A' is "
3850 "out of range"),
3851 input_bfd, input_section, addend,
3852 x86_64_elf_howto_table[r_type].name,
3853 name, (unsigned long) rel->r_offset);
8cf0d2dd
L
3854 bfd_set_error (bfd_error_bad_value);
3855 return FALSE;
3856 }
64d25c44 3857 }
607c0e09
AS
3858 else
3859 {
3860 long sindx;
3861
8517fae7 3862 if (bfd_is_abs_section (sec))
607c0e09
AS
3863 sindx = 0;
3864 else if (sec == NULL || sec->owner == NULL)
3865 {
3866 bfd_set_error (bfd_error_bad_value);
b34976b6 3867 return FALSE;
607c0e09
AS
3868 }
3869 else
3870 {
3871 asection *osec;
3872
74541ad4
AM
3873 /* We are turning this relocation into one
3874 against a section symbol. It would be
3875 proper to subtract the symbol's value,
3876 osec->vma, from the emitted reloc addend,
3877 but ld.so expects buggy relocs. */
607c0e09
AS
3878 osec = sec->output_section;
3879 sindx = elf_section_data (osec)->dynindx;
74541ad4
AM
3880 if (sindx == 0)
3881 {
3882 asection *oi = htab->elf.text_index_section;
3883 sindx = elf_section_data (oi)->dynindx;
3884 }
3885 BFD_ASSERT (sindx != 0);
607c0e09
AS
3886 }
3887
351f65ca 3888 outrel.r_info = htab->r_info (sindx, r_type);
607c0e09
AS
3889 outrel.r_addend = relocation + rel->r_addend;
3890 }
70256ad8
AJ
3891 }
3892
cbe950e9 3893 sreloc = elf_section_data (input_section)->sreloc;
d8045f23 3894
62d78908
L
3895 if (sreloc == NULL || sreloc->contents == NULL)
3896 {
3897 r = bfd_reloc_notsupported;
3898 goto check_relocation_error;
3899 }
c434dee6 3900
351f65ca 3901 elf_append_rela (output_bfd, sreloc, &outrel);
70256ad8
AJ
3902
3903 /* If this reloc is against an external symbol, we do
3904 not want to fiddle with the addend. Otherwise, we
3905 need to include the symbol value so that it becomes
3906 an addend for the dynamic reloc. */
0f88be7a 3907 if (! relocate)
70256ad8
AJ
3908 continue;
3909 }
3910
3911 break;
3912
bffbf940 3913 case R_X86_64_TLSGD:
67a4f2b7
AO
3914 case R_X86_64_GOTPC32_TLSDESC:
3915 case R_X86_64_TLSDESC_CALL:
bffbf940 3916 case R_X86_64_GOTTPOFF:
bffbf940
JJ
3917 tls_type = GOT_UNKNOWN;
3918 if (h == NULL && local_got_offsets)
351f65ca 3919 tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
bffbf940 3920 else if (h != NULL)
351f65ca 3921 tls_type = elf_x86_64_hash_entry (h)->tls_type;
142411ca 3922
351f65ca
L
3923 if (! elf_x86_64_tls_transition (info, input_bfd,
3924 input_section, contents,
3925 symtab_hdr, sym_hashes,
3926 &r_type, tls_type, rel,
3927 relend, h, r_symndx))
534a31f6 3928 return FALSE;
bffbf940
JJ
3929
3930 if (r_type == R_X86_64_TPOFF32)
3931 {
142411ca
L
3932 bfd_vma roff = rel->r_offset;
3933
bffbf940 3934 BFD_ASSERT (! unresolved_reloc);
142411ca 3935
351f65ca 3936 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
bffbf940 3937 {
52bc799a 3938 /* GD->LE transition. For 64bit, change
abcf1d52 3939 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
a3fadc9a 3940 .word 0x6666; rex64; call __tls_get_addr
52bc799a 3941 into:
bffbf940 3942 movq %fs:0, %rax
52bc799a
L
3943 leaq foo@tpoff(%rax), %rax
3944 For 32bit, change
3945 leaq foo@tlsgd(%rip), %rdi
3946 .word 0x6666; rex64; call __tls_get_addr
3947 into:
3948 movl %fs:0, %eax
bffbf940 3949 leaq foo@tpoff(%rax), %rax */
52bc799a
L
3950 if (ABI_64_P (output_bfd))
3951 memcpy (contents + roff - 4,
3952 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3953 16);
3954 else
3955 memcpy (contents + roff - 3,
3956 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3957 15);
eb4ff4d6 3958 bfd_put_32 (output_bfd,
351f65ca 3959 elf_x86_64_tpoff (info, relocation),
142411ca 3960 contents + roff + 8);
a3fadc9a 3961 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
bffbf940
JJ
3962 rel++;
3963 continue;
3964 }
351f65ca 3965 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
67a4f2b7
AO
3966 {
3967 /* GDesc -> LE transition.
3968 It's originally something like:
3969 leaq x@tlsdesc(%rip), %rax
3970
3971 Change it to:
c9736ba0 3972 movl $x@tpoff, %rax. */
67a4f2b7 3973
c9736ba0 3974 unsigned int val, type;
67a4f2b7 3975
67a4f2b7 3976 type = bfd_get_8 (input_bfd, contents + roff - 3);
67a4f2b7 3977 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
3978 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3979 contents + roff - 3);
3980 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3981 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3982 contents + roff - 1);
eb4ff4d6 3983 bfd_put_32 (output_bfd,
351f65ca 3984 elf_x86_64_tpoff (info, relocation),
67a4f2b7
AO
3985 contents + roff);
3986 continue;
3987 }
351f65ca 3988 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
67a4f2b7
AO
3989 {
3990 /* GDesc -> LE transition.
3991 It's originally:
3992 call *(%rax)
3993 Turn it into:
142411ca 3994 xchg %ax,%ax. */
10efb593 3995 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
3996 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3997 continue;
3998 }
351f65ca 3999 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
bffbf940 4000 {
bffbf940
JJ
4001 /* IE->LE transition:
4002 Originally it can be one of:
4003 movq foo@gottpoff(%rip), %reg
4004 addq foo@gottpoff(%rip), %reg
4005 We change it into:
4006 movq $foo, %reg
4007 leaq foo(%reg), %reg
4008 addq $foo, %reg. */
142411ca
L
4009
4010 unsigned int val, type, reg;
4011
4012 val = bfd_get_8 (input_bfd, contents + roff - 3);
4013 type = bfd_get_8 (input_bfd, contents + roff - 2);
4014 reg = bfd_get_8 (input_bfd, contents + roff - 1);
bffbf940 4015 reg >>= 3;
bffbf940
JJ
4016 if (type == 0x8b)
4017 {
4018 /* movq */
4019 if (val == 0x4c)
4020 bfd_put_8 (output_bfd, 0x49,
142411ca 4021 contents + roff - 3);
4a4c5f25
L
4022 else if (!ABI_64_P (output_bfd) && val == 0x44)
4023 bfd_put_8 (output_bfd, 0x41,
4024 contents + roff - 3);
bffbf940 4025 bfd_put_8 (output_bfd, 0xc7,
142411ca 4026 contents + roff - 2);
bffbf940 4027 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 4028 contents + roff - 1);
bffbf940
JJ
4029 }
4030 else if (reg == 4)
4031 {
4032 /* addq -> addq - addressing with %rsp/%r12 is
4033 special */
4034 if (val == 0x4c)
4035 bfd_put_8 (output_bfd, 0x49,
142411ca 4036 contents + roff - 3);
4a4c5f25
L
4037 else if (!ABI_64_P (output_bfd) && val == 0x44)
4038 bfd_put_8 (output_bfd, 0x41,
4039 contents + roff - 3);
bffbf940 4040 bfd_put_8 (output_bfd, 0x81,
142411ca 4041 contents + roff - 2);
bffbf940 4042 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 4043 contents + roff - 1);
bffbf940
JJ
4044 }
4045 else
4046 {
4047 /* addq -> leaq */
4048 if (val == 0x4c)
4049 bfd_put_8 (output_bfd, 0x4d,
142411ca 4050 contents + roff - 3);
4a4c5f25
L
4051 else if (!ABI_64_P (output_bfd) && val == 0x44)
4052 bfd_put_8 (output_bfd, 0x45,
4053 contents + roff - 3);
bffbf940 4054 bfd_put_8 (output_bfd, 0x8d,
142411ca 4055 contents + roff - 2);
bffbf940 4056 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
142411ca 4057 contents + roff - 1);
bffbf940 4058 }
eb4ff4d6 4059 bfd_put_32 (output_bfd,
351f65ca 4060 elf_x86_64_tpoff (info, relocation),
142411ca 4061 contents + roff);
bffbf940
JJ
4062 continue;
4063 }
142411ca
L
4064 else
4065 BFD_ASSERT (FALSE);
bffbf940
JJ
4066 }
4067
6de2ae4a 4068 if (htab->elf.sgot == NULL)
bffbf940
JJ
4069 abort ();
4070
4071 if (h != NULL)
67a4f2b7
AO
4072 {
4073 off = h->got.offset;
351f65ca 4074 offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
67a4f2b7 4075 }
bffbf940
JJ
4076 else
4077 {
4078 if (local_got_offsets == NULL)
4079 abort ();
4080
4081 off = local_got_offsets[r_symndx];
67a4f2b7 4082 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
4083 }
4084
4085 if ((off & 1) != 0)
4086 off &= ~1;
26e41594 4087 else
bffbf940
JJ
4088 {
4089 Elf_Internal_Rela outrel;
bffbf940 4090 int dr_type, indx;
67a4f2b7 4091 asection *sreloc;
bffbf940 4092
6de2ae4a 4093 if (htab->elf.srelgot == NULL)
bffbf940
JJ
4094 abort ();
4095
67a4f2b7
AO
4096 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4097
4098 if (GOT_TLS_GDESC_P (tls_type))
4099 {
351f65ca 4100 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
67a4f2b7 4101 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
6de2ae4a
L
4102 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
4103 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4104 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
4105 + offplt
4106 + htab->sgotplt_jump_table_size);
6de2ae4a 4107 sreloc = htab->elf.srelplt;
67a4f2b7 4108 if (indx == 0)
351f65ca 4109 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
67a4f2b7
AO
4110 else
4111 outrel.r_addend = 0;
351f65ca 4112 elf_append_rela (output_bfd, sreloc, &outrel);
67a4f2b7
AO
4113 }
4114
6de2ae4a 4115 sreloc = htab->elf.srelgot;
67a4f2b7 4116
6de2ae4a
L
4117 outrel.r_offset = (htab->elf.sgot->output_section->vma
4118 + htab->elf.sgot->output_offset + off);
bffbf940 4119
67a4f2b7 4120 if (GOT_TLS_GD_P (tls_type))
bffbf940 4121 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
4122 else if (GOT_TLS_GDESC_P (tls_type))
4123 goto dr_done;
bffbf940
JJ
4124 else
4125 dr_type = R_X86_64_TPOFF64;
4126
6de2ae4a 4127 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
bffbf940 4128 outrel.r_addend = 0;
67a4f2b7
AO
4129 if ((dr_type == R_X86_64_TPOFF64
4130 || dr_type == R_X86_64_TLSDESC) && indx == 0)
351f65ca
L
4131 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
4132 outrel.r_info = htab->r_info (indx, dr_type);
bffbf940 4133
351f65ca 4134 elf_append_rela (output_bfd, sreloc, &outrel);
bffbf940 4135
67a4f2b7 4136 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
4137 {
4138 if (indx == 0)
4139 {
d40d037c 4140 BFD_ASSERT (! unresolved_reloc);
bffbf940 4141 bfd_put_64 (output_bfd,
351f65ca 4142 relocation - elf_x86_64_dtpoff_base (info),
6de2ae4a 4143 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
bffbf940
JJ
4144 }
4145 else
4146 {
4147 bfd_put_64 (output_bfd, 0,
6de2ae4a 4148 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 4149 outrel.r_info = htab->r_info (indx,
bffbf940
JJ
4150 R_X86_64_DTPOFF64);
4151 outrel.r_offset += GOT_ENTRY_SIZE;
351f65ca 4152 elf_append_rela (output_bfd, sreloc,
464d3bd4 4153 &outrel);
bffbf940
JJ
4154 }
4155 }
4156
67a4f2b7 4157 dr_done:
bffbf940
JJ
4158 if (h != NULL)
4159 h->got.offset |= 1;
4160 else
4161 local_got_offsets[r_symndx] |= 1;
4162 }
4163
67a4f2b7
AO
4164 if (off >= (bfd_vma) -2
4165 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940 4166 abort ();
351f65ca 4167 if (r_type == ELF32_R_TYPE (rel->r_info))
bffbf940 4168 {
67a4f2b7
AO
4169 if (r_type == R_X86_64_GOTPC32_TLSDESC
4170 || r_type == R_X86_64_TLSDESC_CALL)
6de2ae4a
L
4171 relocation = htab->elf.sgotplt->output_section->vma
4172 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
4173 + offplt + htab->sgotplt_jump_table_size;
4174 else
6de2ae4a
L
4175 relocation = htab->elf.sgot->output_section->vma
4176 + htab->elf.sgot->output_offset + off;
b34976b6 4177 unresolved_reloc = FALSE;
bffbf940 4178 }
142411ca 4179 else
67a4f2b7 4180 {
142411ca 4181 bfd_vma roff = rel->r_offset;
67a4f2b7 4182
351f65ca 4183 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
142411ca 4184 {
52bc799a 4185 /* GD->IE transition. For 64bit, change
142411ca
L
4186 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4187 .word 0x6666; rex64; call __tls_get_addr@plt
52bc799a 4188 into:
142411ca 4189 movq %fs:0, %rax
52bc799a
L
4190 addq foo@gottpoff(%rip), %rax
4191 For 32bit, change
4192 leaq foo@tlsgd(%rip), %rdi
4193 .word 0x6666; rex64; call __tls_get_addr@plt
4194 into:
4195 movl %fs:0, %eax
142411ca 4196 addq foo@gottpoff(%rip), %rax */
52bc799a
L
4197 if (ABI_64_P (output_bfd))
4198 memcpy (contents + roff - 4,
4199 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4200 16);
4201 else
4202 memcpy (contents + roff - 3,
4203 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4204 15);
142411ca 4205
6de2ae4a
L
4206 relocation = (htab->elf.sgot->output_section->vma
4207 + htab->elf.sgot->output_offset + off
142411ca
L
4208 - roff
4209 - input_section->output_section->vma
4210 - input_section->output_offset
4211 - 12);
4212 bfd_put_32 (output_bfd, relocation,
4213 contents + roff + 8);
4214 /* Skip R_X86_64_PLT32. */
4215 rel++;
4216 continue;
4217 }
351f65ca 4218 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
142411ca
L
4219 {
4220 /* GDesc -> IE transition.
4221 It's originally something like:
4222 leaq x@tlsdesc(%rip), %rax
67a4f2b7 4223
142411ca 4224 Change it to:
c9736ba0 4225 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
67a4f2b7 4226
142411ca
L
4227 /* Now modify the instruction as appropriate. To
4228 turn a leaq into a movq in the form we use it, it
4229 suffices to change the second byte from 0x8d to
4230 0x8b. */
4231 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
4232
4233 bfd_put_32 (output_bfd,
6de2ae4a
L
4234 htab->elf.sgot->output_section->vma
4235 + htab->elf.sgot->output_offset + off
142411ca
L
4236 - rel->r_offset
4237 - input_section->output_section->vma
4238 - input_section->output_offset
4239 - 4,
4240 contents + roff);
4241 continue;
4242 }
351f65ca 4243 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
142411ca
L
4244 {
4245 /* GDesc -> IE transition.
4246 It's originally:
4247 call *(%rax)
4248
4249 Change it to:
c9736ba0 4250 xchg %ax, %ax. */
142411ca 4251
142411ca
L
4252 bfd_put_8 (output_bfd, 0x66, contents + roff);
4253 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4254 continue;
4255 }
4256 else
4257 BFD_ASSERT (FALSE);
67a4f2b7 4258 }
bffbf940
JJ
4259 break;
4260
4261 case R_X86_64_TLSLD:
351f65ca
L
4262 if (! elf_x86_64_tls_transition (info, input_bfd,
4263 input_section, contents,
4264 symtab_hdr, sym_hashes,
4265 &r_type, GOT_UNKNOWN,
4266 rel, relend, h, r_symndx))
142411ca 4267 return FALSE;
a3fadc9a 4268
142411ca
L
4269 if (r_type != R_X86_64_TLSLD)
4270 {
bffbf940 4271 /* LD->LE transition:
a3fadc9a 4272 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
52bc799a
L
4273 For 64bit, we change it into:
4274 .word 0x6666; .byte 0x66; movq %fs:0, %rax.
4275 For 32bit, we change it into:
4276 nopl 0x0(%rax); movl %fs:0, %eax. */
142411ca
L
4277
4278 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
52bc799a
L
4279 if (ABI_64_P (output_bfd))
4280 memcpy (contents + rel->r_offset - 3,
4281 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
4282 else
4283 memcpy (contents + rel->r_offset - 3,
4284 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
a3fadc9a 4285 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
bffbf940
JJ
4286 rel++;
4287 continue;
4288 }
4289
6de2ae4a 4290 if (htab->elf.sgot == NULL)
bffbf940
JJ
4291 abort ();
4292
4293 off = htab->tls_ld_got.offset;
4294 if (off & 1)
4295 off &= ~1;
4296 else
4297 {
4298 Elf_Internal_Rela outrel;
bffbf940 4299
6de2ae4a 4300 if (htab->elf.srelgot == NULL)
bffbf940
JJ
4301 abort ();
4302
6de2ae4a
L
4303 outrel.r_offset = (htab->elf.sgot->output_section->vma
4304 + htab->elf.sgot->output_offset + off);
bffbf940
JJ
4305
4306 bfd_put_64 (output_bfd, 0,
6de2ae4a 4307 htab->elf.sgot->contents + off);
bffbf940 4308 bfd_put_64 (output_bfd, 0,
6de2ae4a 4309 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 4310 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
bffbf940 4311 outrel.r_addend = 0;
351f65ca 4312 elf_append_rela (output_bfd, htab->elf.srelgot,
464d3bd4 4313 &outrel);
bffbf940
JJ
4314 htab->tls_ld_got.offset |= 1;
4315 }
6de2ae4a
L
4316 relocation = htab->elf.sgot->output_section->vma
4317 + htab->elf.sgot->output_offset + off;
b34976b6 4318 unresolved_reloc = FALSE;
bffbf940
JJ
4319 break;
4320
4321 case R_X86_64_DTPOFF32:
1d85728f 4322 if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
351f65ca 4323 relocation -= elf_x86_64_dtpoff_base (info);
bffbf940 4324 else
351f65ca 4325 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
4326 break;
4327
4328 case R_X86_64_TPOFF32:
6769d501 4329 case R_X86_64_TPOFF64:
9b769489 4330 BFD_ASSERT (info->executable);
351f65ca 4331 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
4332 break;
4333
70256ad8
AJ
4334 default:
4335 break;
4336 }
8d88c4ca 4337
239e1f3a
AM
4338 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4339 because such sections are not SEC_ALLOC and thus ld.so will
4340 not process them. */
c434dee6 4341 if (unresolved_reloc
239e1f3a 4342 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
4343 && h->def_dynamic)
4344 && _bfd_elf_section_offset (output_bfd, info, input_section,
4345 rel->r_offset) != (bfd_vma) -1)
a040981f
L
4346 {
4347 (*_bfd_error_handler)
4348 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4349 input_bfd,
4350 input_section,
4351 (long) rel->r_offset,
4352 howto->name,
4353 h->root.root.string);
4354 return FALSE;
4355 }
c434dee6 4356
cbe950e9 4357do_relocation:
8d88c4ca 4358 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
4359 contents, rel->r_offset,
4360 relocation, rel->r_addend);
8d88c4ca 4361
62d78908 4362check_relocation_error:
8d88c4ca 4363 if (r != bfd_reloc_ok)
8da6118f 4364 {
c434dee6
AJ
4365 const char *name;
4366
4367 if (h != NULL)
4368 name = h->root.root.string;
4369 else
8da6118f 4370 {
c434dee6
AJ
4371 name = bfd_elf_string_from_elf_section (input_bfd,
4372 symtab_hdr->sh_link,
4373 sym->st_name);
4374 if (name == NULL)
b34976b6 4375 return FALSE;
c434dee6
AJ
4376 if (*name == '\0')
4377 name = bfd_section_name (input_bfd, sec);
4378 }
4379
4380 if (r == bfd_reloc_overflow)
4381 {
c434dee6 4382 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
4383 (info, (h ? &h->root : NULL), name, howto->name,
4384 (bfd_vma) 0, input_bfd, input_section,
4385 rel->r_offset)))
b34976b6 4386 return FALSE;
c434dee6
AJ
4387 }
4388 else
4389 {
4390 (*_bfd_error_handler)
bb95161d 4391 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
d003868e 4392 input_bfd, input_section,
c434dee6 4393 (long) rel->r_offset, name, (int) r);
b34976b6 4394 return FALSE;
8da6118f
KH
4395 }
4396 }
8d88c4ca 4397 }
70256ad8 4398
b34976b6 4399 return TRUE;
70256ad8
AJ
4400}
4401
4402/* Finish up dynamic symbol handling. We set the contents of various
4403 dynamic sections here. */
4404
b34976b6 4405static bfd_boolean
351f65ca
L
4406elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
4407 struct bfd_link_info *info,
4408 struct elf_link_hash_entry *h,
220cf809 4409 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
70256ad8 4410{
351f65ca 4411 struct elf_x86_64_link_hash_table *htab;
eed180f8
RM
4412 const struct elf_x86_64_backend_data *const abed
4413 = get_elf_x86_64_backend_data (output_bfd);
70256ad8 4414
351f65ca 4415 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
4416 if (htab == NULL)
4417 return FALSE;
70256ad8
AJ
4418
4419 if (h->plt.offset != (bfd_vma) -1)
4420 {
70256ad8
AJ
4421 bfd_vma plt_index;
4422 bfd_vma got_offset;
4423 Elf_Internal_Rela rela;
947216bf 4424 bfd_byte *loc;
cbe950e9 4425 asection *plt, *gotplt, *relplt;
351f65ca 4426 const struct elf_backend_data *bed;
cbe950e9
L
4427
4428 /* When building a static executable, use .iplt, .igot.plt and
4429 .rela.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 4430 if (htab->elf.splt != NULL)
cbe950e9 4431 {
6de2ae4a
L
4432 plt = htab->elf.splt;
4433 gotplt = htab->elf.sgotplt;
4434 relplt = htab->elf.srelplt;
cbe950e9
L
4435 }
4436 else
4437 {
6de2ae4a
L
4438 plt = htab->elf.iplt;
4439 gotplt = htab->elf.igotplt;
4440 relplt = htab->elf.irelplt;
cbe950e9 4441 }
70256ad8
AJ
4442
4443 /* This symbol has an entry in the procedure linkage table. Set
407443a3 4444 it up. */
cbe950e9
L
4445 if ((h->dynindx == -1
4446 && !((h->forced_local || info->executable)
4447 && h->def_regular
4448 && h->type == STT_GNU_IFUNC))
4449 || plt == NULL
4450 || gotplt == NULL
4451 || relplt == NULL)
cec7f46a 4452 abort ();
70256ad8
AJ
4453
4454 /* Get the index in the procedure linkage table which
4455 corresponds to this symbol. This is the index of this symbol
4456 in all the symbols for which we are making plt entries. The
cbe950e9 4457 first entry in the procedure linkage table is reserved.
6bbec505 4458
cbe950e9 4459 Get the offset into the .got table of the entry that
407443a3 4460 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
cbe950e9
L
4461 bytes. The first three are reserved for the dynamic linker.
4462
4463 For static executables, we don't reserve anything. */
4464
6de2ae4a 4465 if (plt == htab->elf.splt)
cbe950e9 4466 {
eed180f8 4467 got_offset = h->plt.offset / abed->plt_entry_size - 1;
e1f98742 4468 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
cbe950e9
L
4469 }
4470 else
4471 {
eed180f8 4472 got_offset = h->plt.offset / abed->plt_entry_size;
e1f98742 4473 got_offset = got_offset * GOT_ENTRY_SIZE;
cbe950e9 4474 }
70256ad8
AJ
4475
4476 /* Fill in the entry in the procedure linkage table. */
eed180f8
RM
4477 memcpy (plt->contents + h->plt.offset, abed->plt_entry,
4478 abed->plt_entry_size);
4479
4480 /* Insert the relocation positions of the plt section. */
4481
4482 /* Put offset the PC-relative instruction referring to the GOT entry,
4483 subtracting the size of that instruction. */
653165cc 4484 bfd_put_32 (output_bfd,
eed180f8
RM
4485 (gotplt->output_section->vma
4486 + gotplt->output_offset
4487 + got_offset
4488 - plt->output_section->vma
4489 - plt->output_offset
4490 - h->plt.offset
4491 - abed->plt_got_insn_size),
4492 plt->contents + h->plt.offset + abed->plt_got_offset);
cbe950e9 4493
653165cc 4494 /* Fill in the entry in the global offset table, initially this
eed180f8 4495 points to the second part of the PLT entry. */
cbe950e9
L
4496 bfd_put_64 (output_bfd, (plt->output_section->vma
4497 + plt->output_offset
eed180f8 4498 + h->plt.offset + abed->plt_lazy_offset),
cbe950e9 4499 gotplt->contents + got_offset);
70256ad8
AJ
4500
4501 /* Fill in the entry in the .rela.plt section. */
cbe950e9
L
4502 rela.r_offset = (gotplt->output_section->vma
4503 + gotplt->output_offset
70256ad8 4504 + got_offset);
cbe950e9
L
4505 if (h->dynindx == -1
4506 || ((info->executable
4507 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4508 && h->def_regular
4509 && h->type == STT_GNU_IFUNC))
4510 {
4511 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4512 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
351f65ca 4513 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
cbe950e9
L
4514 rela.r_addend = (h->root.u.def.value
4515 + h->root.u.def.section->output_section->vma
4516 + h->root.u.def.section->output_offset);
e1f98742
L
4517 /* R_X86_64_IRELATIVE comes last. */
4518 plt_index = htab->next_irelative_index--;
cbe950e9
L
4519 }
4520 else
4521 {
351f65ca 4522 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
cbe950e9 4523 rela.r_addend = 0;
e1f98742
L
4524 plt_index = htab->next_jump_slot_index++;
4525 }
4526
4527 /* Don't fill PLT entry for static executables. */
4528 if (plt == htab->elf.splt)
4529 {
4530 /* Put relocation index. */
4531 bfd_put_32 (output_bfd, plt_index,
eed180f8 4532 plt->contents + h->plt.offset + abed->plt_reloc_offset);
e1f98742 4533 /* Put offset for jmp .PLT0. */
eed180f8
RM
4534 bfd_put_32 (output_bfd, - (h->plt.offset + abed->plt_plt_insn_end),
4535 plt->contents + h->plt.offset + abed->plt_plt_offset);
cbe950e9 4536 }
351f65ca
L
4537
4538 bed = get_elf_backend_data (output_bfd);
4539 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
82e96e07 4540 bed->s->swap_reloca_out (output_bfd, &rela, loc);
70256ad8 4541
f5385ebf 4542 if (!h->def_regular)
70256ad8
AJ
4543 {
4544 /* Mark the symbol as undefined, rather than as defined in
47a9f7b3
JJ
4545 the .plt section. Leave the value if there were any
4546 relocations where pointer equality matters (this is a clue
c434dee6
AJ
4547 for the dynamic linker, to make function pointer
4548 comparisons work between an application and shared
47a9f7b3
JJ
4549 library), otherwise set it to zero. If a function is only
4550 called from a binary, there is no need to slow down
4551 shared libraries because of that. */
70256ad8 4552 sym->st_shndx = SHN_UNDEF;
f5385ebf 4553 if (!h->pointer_equality_needed)
47a9f7b3 4554 sym->st_value = 0;
70256ad8
AJ
4555 }
4556 }
4557
bffbf940 4558 if (h->got.offset != (bfd_vma) -1
351f65ca
L
4559 && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
4560 && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
053579d7 4561 {
053579d7
AJ
4562 Elf_Internal_Rela rela;
4563
4564 /* This symbol has an entry in the global offset table. Set it
bffbf940 4565 up. */
6de2ae4a 4566 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
c434dee6 4567 abort ();
053579d7 4568
6de2ae4a
L
4569 rela.r_offset = (htab->elf.sgot->output_section->vma
4570 + htab->elf.sgot->output_offset
dc810e39 4571 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
4572
4573 /* If this is a static link, or it is a -Bsymbolic link and the
4574 symbol is defined locally or was forced to be local because
4575 of a version file, we just want to emit a RELATIVE reloc.
4576 The entry in the global offset table will already have been
4577 initialized in the relocate_section function. */
710ab287 4578 if (h->def_regular
0018b0a3
L
4579 && h->type == STT_GNU_IFUNC)
4580 {
710ab287
L
4581 if (info->shared)
4582 {
4583 /* Generate R_X86_64_GLOB_DAT. */
4584 goto do_glob_dat;
4585 }
4586 else
4587 {
90d60710
L
4588 asection *plt;
4589
710ab287
L
4590 if (!h->pointer_equality_needed)
4591 abort ();
4592
4593 /* For non-shared object, we can't use .got.plt, which
4594 contains the real function addres if we need pointer
4595 equality. We load the GOT entry with the PLT entry. */
90d60710 4596 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
710ab287
L
4597 bfd_put_64 (output_bfd, (plt->output_section->vma
4598 + plt->output_offset
4599 + h->plt.offset),
6de2ae4a 4600 htab->elf.sgot->contents + h->got.offset);
710ab287
L
4601 return TRUE;
4602 }
0018b0a3
L
4603 }
4604 else if (info->shared
4605 && SYMBOL_REFERENCES_LOCAL (info, h))
053579d7 4606 {
41bed6dd
L
4607 if (!h->def_regular)
4608 return FALSE;
cc78d0af 4609 BFD_ASSERT((h->got.offset & 1) != 0);
351f65ca 4610 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
053579d7
AJ
4611 rela.r_addend = (h->root.u.def.value
4612 + h->root.u.def.section->output_section->vma
4613 + h->root.u.def.section->output_offset);
4614 }
4615 else
4616 {
4617 BFD_ASSERT((h->got.offset & 1) == 0);
710ab287 4618do_glob_dat:
c434dee6 4619 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 4620 htab->elf.sgot->contents + h->got.offset);
351f65ca 4621 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
053579d7
AJ
4622 rela.r_addend = 0;
4623 }
4624
351f65ca 4625 elf_append_rela (output_bfd, htab->elf.srelgot, &rela);
053579d7
AJ
4626 }
4627
f5385ebf 4628 if (h->needs_copy)
70256ad8 4629 {
70256ad8
AJ
4630 Elf_Internal_Rela rela;
4631
4632 /* This symbol needs a copy reloc. Set it up. */
4633
c434dee6
AJ
4634 if (h->dynindx == -1
4635 || (h->root.type != bfd_link_hash_defined
4636 && h->root.type != bfd_link_hash_defweak)
4637 || htab->srelbss == NULL)
4638 abort ();
70256ad8
AJ
4639
4640 rela.r_offset = (h->root.u.def.value
4641 + h->root.u.def.section->output_section->vma
4642 + h->root.u.def.section->output_offset);
351f65ca 4643 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
70256ad8 4644 rela.r_addend = 0;
351f65ca 4645 elf_append_rela (output_bfd, htab->srelbss, &rela);
70256ad8
AJ
4646 }
4647
b34976b6 4648 return TRUE;
70256ad8
AJ
4649}
4650
c25bc9fc
L
4651/* Finish up local dynamic symbol handling. We set the contents of
4652 various dynamic sections here. */
4653
4654static bfd_boolean
351f65ca 4655elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
c25bc9fc
L
4656{
4657 struct elf_link_hash_entry *h
4658 = (struct elf_link_hash_entry *) *slot;
4659 struct bfd_link_info *info
eed180f8 4660 = (struct bfd_link_info *) inf;
c25bc9fc 4661
351f65ca 4662 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
c25bc9fc
L
4663 info, h, NULL);
4664}
4665
c434dee6
AJ
4666/* Used to decide how to sort relocs in an optimal manner for the
4667 dynamic linker, before writing them out. */
4668
4669static enum elf_reloc_type_class
7e612e98
AM
4670elf_x86_64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4671 const asection *rel_sec ATTRIBUTE_UNUSED,
4672 const Elf_Internal_Rela *rela)
c434dee6 4673{
351f65ca 4674 switch ((int) ELF32_R_TYPE (rela->r_info))
c434dee6
AJ
4675 {
4676 case R_X86_64_RELATIVE:
1da80baa 4677 case R_X86_64_RELATIVE64:
c434dee6
AJ
4678 return reloc_class_relative;
4679 case R_X86_64_JUMP_SLOT:
4680 return reloc_class_plt;
4681 case R_X86_64_COPY:
4682 return reloc_class_copy;
4683 default:
4684 return reloc_class_normal;
4685 }
4686}
4687
70256ad8
AJ
4688/* Finish up the dynamic sections. */
4689
b34976b6 4690static bfd_boolean
351f65ca
L
4691elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4692 struct bfd_link_info *info)
70256ad8 4693{
351f65ca 4694 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
4695 bfd *dynobj;
4696 asection *sdyn;
eed180f8
RM
4697 const struct elf_x86_64_backend_data *const abed
4698 = get_elf_x86_64_backend_data (output_bfd);
70256ad8 4699
351f65ca 4700 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
4701 if (htab == NULL)
4702 return FALSE;
4703
c434dee6 4704 dynobj = htab->elf.dynobj;
3d4d4302 4705 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
70256ad8 4706
c434dee6 4707 if (htab->elf.dynamic_sections_created)
70256ad8 4708 {
82e96e07
L
4709 bfd_byte *dyncon, *dynconend;
4710 const struct elf_backend_data *bed;
4711 bfd_size_type sizeof_dyn;
70256ad8 4712
6de2ae4a 4713 if (sdyn == NULL || htab->elf.sgot == NULL)
c434dee6 4714 abort ();
70256ad8 4715
82e96e07
L
4716 bed = get_elf_backend_data (dynobj);
4717 sizeof_dyn = bed->s->sizeof_dyn;
4718 dyncon = sdyn->contents;
4719 dynconend = sdyn->contents + sdyn->size;
4720 for (; dyncon < dynconend; dyncon += sizeof_dyn)
70256ad8
AJ
4721 {
4722 Elf_Internal_Dyn dyn;
70256ad8
AJ
4723 asection *s;
4724
82e96e07 4725 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
70256ad8
AJ
4726
4727 switch (dyn.d_tag)
4728 {
4729 default:
053579d7 4730 continue;
70256ad8
AJ
4731
4732 case DT_PLTGOT:
6de2ae4a 4733 s = htab->elf.sgotplt;
8c37241b 4734 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
c434dee6 4735 break;
70256ad8
AJ
4736
4737 case DT_JMPREL:
6de2ae4a 4738 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
c434dee6 4739 break;
70256ad8 4740
c434dee6 4741 case DT_PLTRELSZ:
6de2ae4a 4742 s = htab->elf.srelplt->output_section;
eea6121a 4743 dyn.d_un.d_val = s->size;
70256ad8
AJ
4744 break;
4745
4746 case DT_RELASZ:
c434dee6
AJ
4747 /* The procedure linkage table relocs (DT_JMPREL) should
4748 not be included in the overall relocs (DT_RELA).
4749 Therefore, we override the DT_RELASZ entry here to
4750 make it not include the JMPREL relocs. Since the
4751 linker script arranges for .rela.plt to follow all
4752 other relocation sections, we don't have to worry
4753 about changing the DT_RELA entry. */
6de2ae4a 4754 if (htab->elf.srelplt != NULL)
70256ad8 4755 {
6de2ae4a 4756 s = htab->elf.srelplt->output_section;
eea6121a 4757 dyn.d_un.d_val -= s->size;
70256ad8
AJ
4758 }
4759 break;
67a4f2b7
AO
4760
4761 case DT_TLSDESC_PLT:
6de2ae4a 4762 s = htab->elf.splt;
67a4f2b7
AO
4763 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4764 + htab->tlsdesc_plt;
4765 break;
4766
4767 case DT_TLSDESC_GOT:
6de2ae4a 4768 s = htab->elf.sgot;
67a4f2b7
AO
4769 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4770 + htab->tlsdesc_got;
4771 break;
70256ad8 4772 }
c434dee6 4773
82e96e07 4774 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
70256ad8
AJ
4775 }
4776
c434dee6 4777 /* Fill in the special first entry in the procedure linkage table. */
6de2ae4a 4778 if (htab->elf.splt && htab->elf.splt->size > 0)
70256ad8 4779 {
653165cc 4780 /* Fill in the first entry in the procedure linkage table. */
eed180f8
RM
4781 memcpy (htab->elf.splt->contents,
4782 abed->plt0_entry, abed->plt_entry_size);
653165cc
AJ
4783 /* Add offset for pushq GOT+8(%rip), since the instruction
4784 uses 6 bytes subtract this value. */
4785 bfd_put_32 (output_bfd,
6de2ae4a
L
4786 (htab->elf.sgotplt->output_section->vma
4787 + htab->elf.sgotplt->output_offset
653165cc 4788 + 8
6de2ae4a
L
4789 - htab->elf.splt->output_section->vma
4790 - htab->elf.splt->output_offset
653165cc 4791 - 6),
eed180f8
RM
4792 htab->elf.splt->contents + abed->plt0_got1_offset);
4793 /* Add offset for the PC-relative instruction accessing GOT+16,
4794 subtracting the offset to the end of that instruction. */
653165cc 4795 bfd_put_32 (output_bfd,
6de2ae4a
L
4796 (htab->elf.sgotplt->output_section->vma
4797 + htab->elf.sgotplt->output_offset
653165cc 4798 + 16
6de2ae4a
L
4799 - htab->elf.splt->output_section->vma
4800 - htab->elf.splt->output_offset
eed180f8
RM
4801 - abed->plt0_got2_insn_end),
4802 htab->elf.splt->contents + abed->plt0_got2_offset);
653165cc 4803
eed180f8
RM
4804 elf_section_data (htab->elf.splt->output_section)
4805 ->this_hdr.sh_entsize = abed->plt_entry_size;
67a4f2b7
AO
4806
4807 if (htab->tlsdesc_plt)
4808 {
4809 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 4810 htab->elf.sgot->contents + htab->tlsdesc_got);
67a4f2b7 4811
6de2ae4a 4812 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
eed180f8 4813 abed->plt0_entry, abed->plt_entry_size);
67a4f2b7
AO
4814
4815 /* Add offset for pushq GOT+8(%rip), since the
4816 instruction uses 6 bytes subtract this value. */
4817 bfd_put_32 (output_bfd,
6de2ae4a
L
4818 (htab->elf.sgotplt->output_section->vma
4819 + htab->elf.sgotplt->output_offset
67a4f2b7 4820 + 8
6de2ae4a
L
4821 - htab->elf.splt->output_section->vma
4822 - htab->elf.splt->output_offset
67a4f2b7
AO
4823 - htab->tlsdesc_plt
4824 - 6),
eed180f8
RM
4825 htab->elf.splt->contents
4826 + htab->tlsdesc_plt + abed->plt0_got1_offset);
4827 /* Add offset for the PC-relative instruction accessing GOT+TDG,
4828 where TGD stands for htab->tlsdesc_got, subtracting the offset
4829 to the end of that instruction. */
67a4f2b7 4830 bfd_put_32 (output_bfd,
6de2ae4a
L
4831 (htab->elf.sgot->output_section->vma
4832 + htab->elf.sgot->output_offset
67a4f2b7 4833 + htab->tlsdesc_got
6de2ae4a
L
4834 - htab->elf.splt->output_section->vma
4835 - htab->elf.splt->output_offset
67a4f2b7 4836 - htab->tlsdesc_plt
eed180f8
RM
4837 - abed->plt0_got2_insn_end),
4838 htab->elf.splt->contents
4839 + htab->tlsdesc_plt + abed->plt0_got2_offset);
67a4f2b7 4840 }
70256ad8 4841 }
70256ad8
AJ
4842 }
4843
6de2ae4a 4844 if (htab->elf.sgotplt)
70256ad8 4845 {
56d4289c
L
4846 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4847 {
4848 (*_bfd_error_handler)
4849 (_("discarded output section: `%A'"), htab->elf.sgotplt);
4850 return FALSE;
4851 }
4852
c434dee6 4853 /* Fill in the first three entries in the global offset table. */
6de2ae4a 4854 if (htab->elf.sgotplt->size > 0)
c434dee6
AJ
4855 {
4856 /* Set the first entry in the global offset table to the address of
4857 the dynamic section. */
4858 if (sdyn == NULL)
6de2ae4a 4859 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
c434dee6
AJ
4860 else
4861 bfd_put_64 (output_bfd,
4862 sdyn->output_section->vma + sdyn->output_offset,
6de2ae4a 4863 htab->elf.sgotplt->contents);
c434dee6 4864 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
6de2ae4a
L
4865 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4866 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
c434dee6 4867 }
70256ad8 4868
6de2ae4a 4869 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
c434dee6
AJ
4870 GOT_ENTRY_SIZE;
4871 }
70256ad8 4872
e41b3a13 4873 /* Adjust .eh_frame for .plt section. */
9a2a56cc
AM
4874 if (htab->plt_eh_frame != NULL
4875 && htab->plt_eh_frame->contents != NULL)
e41b3a13
JJ
4876 {
4877 if (htab->elf.splt != NULL
4878 && htab->elf.splt->size != 0
4879 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4880 && htab->elf.splt->output_section != NULL
4881 && htab->plt_eh_frame->output_section != NULL)
4882 {
4883 bfd_vma plt_start = htab->elf.splt->output_section->vma;
4884 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4885 + htab->plt_eh_frame->output_offset
4886 + PLT_FDE_START_OFFSET;
4887 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4888 htab->plt_eh_frame->contents
4889 + PLT_FDE_START_OFFSET);
4890 }
dbaa2011 4891 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
e41b3a13
JJ
4892 {
4893 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4894 htab->plt_eh_frame,
4895 htab->plt_eh_frame->contents))
4896 return FALSE;
4897 }
4898 }
4899
6de2ae4a
L
4900 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4901 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
8c37241b
JJ
4902 = GOT_ENTRY_SIZE;
4903
c25bc9fc
L
4904 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4905 htab_traverse (htab->loc_hash_table,
351f65ca 4906 elf_x86_64_finish_local_dynamic_symbol,
c25bc9fc
L
4907 info);
4908
b34976b6 4909 return TRUE;
8d88c4ca
NC
4910}
4911
4c45e5c9
JJ
4912/* Return address for Ith PLT stub in section PLT, for relocation REL
4913 or (bfd_vma) -1 if it should not be included. */
4914
4915static bfd_vma
351f65ca
L
4916elf_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4917 const arelent *rel ATTRIBUTE_UNUSED)
4c45e5c9 4918{
eed180f8 4919 return plt->vma + (i + 1) * GET_PLT_ENTRY_SIZE (plt->owner);
4c45e5c9 4920}
8df9fc9d 4921
d2b2c203
DJ
4922/* Handle an x86-64 specific section when reading an object file. This
4923 is called when elfcode.h finds a section with an unknown type. */
4924
4925static bfd_boolean
351f65ca 4926elf_x86_64_section_from_shdr (bfd *abfd,
6dc132d9
L
4927 Elf_Internal_Shdr *hdr,
4928 const char *name,
4929 int shindex)
d2b2c203
DJ
4930{
4931 if (hdr->sh_type != SHT_X86_64_UNWIND)
4932 return FALSE;
4933
6dc132d9 4934 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
4935 return FALSE;
4936
4937 return TRUE;
4938}
4939
3b22753a
L
4940/* Hook called by the linker routine which adds symbols from an object
4941 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4942 of .bss. */
4943
4944static bfd_boolean
351f65ca
L
4945elf_x86_64_add_symbol_hook (bfd *abfd,
4946 struct bfd_link_info *info,
4947 Elf_Internal_Sym *sym,
4948 const char **namep ATTRIBUTE_UNUSED,
4949 flagword *flagsp ATTRIBUTE_UNUSED,
4950 asection **secp,
4951 bfd_vma *valp)
3b22753a
L
4952{
4953 asection *lcomm;
4954
4955 switch (sym->st_shndx)
4956 {
4957 case SHN_X86_64_LCOMMON:
4958 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4959 if (lcomm == NULL)
4960 {
4961 lcomm = bfd_make_section_with_flags (abfd,
4962 "LARGE_COMMON",
4963 (SEC_ALLOC
4964 | SEC_IS_COMMON
4965 | SEC_LINKER_CREATED));
4966 if (lcomm == NULL)
4967 return FALSE;
4968 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4969 }
4970 *secp = lcomm;
4971 *valp = sym->st_size;
c35bdf6e 4972 return TRUE;
3b22753a 4973 }
d8045f23 4974
c16153ae 4975 if ((abfd->flags & DYNAMIC) == 0
f64b2e8d
NC
4976 && (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
4977 || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE))
4978 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
d8045f23 4979
3b22753a
L
4980 return TRUE;
4981}
4982
4983
4984/* Given a BFD section, try to locate the corresponding ELF section
4985 index. */
4986
4987static bfd_boolean
351f65ca
L
4988elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4989 asection *sec, int *index_return)
3b22753a
L
4990{
4991 if (sec == &_bfd_elf_large_com_section)
4992 {
91d6fa6a 4993 *index_return = SHN_X86_64_LCOMMON;
3b22753a
L
4994 return TRUE;
4995 }
4996 return FALSE;
4997}
4998
4999/* Process a symbol. */
5000
5001static void
351f65ca
L
5002elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5003 asymbol *asym)
3b22753a
L
5004{
5005 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5006
5007 switch (elfsym->internal_elf_sym.st_shndx)
5008 {
5009 case SHN_X86_64_LCOMMON:
5010 asym->section = &_bfd_elf_large_com_section;
5011 asym->value = elfsym->internal_elf_sym.st_size;
5012 /* Common symbol doesn't set BSF_GLOBAL. */
5013 asym->flags &= ~BSF_GLOBAL;
5014 break;
5015 }
5016}
5017
5018static bfd_boolean
351f65ca 5019elf_x86_64_common_definition (Elf_Internal_Sym *sym)
3b22753a
L
5020{
5021 return (sym->st_shndx == SHN_COMMON
5022 || sym->st_shndx == SHN_X86_64_LCOMMON);
5023}
5024
5025static unsigned int
351f65ca 5026elf_x86_64_common_section_index (asection *sec)
3b22753a
L
5027{
5028 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5029 return SHN_COMMON;
5030 else
5031 return SHN_X86_64_LCOMMON;
5032}
5033
5034static asection *
351f65ca 5035elf_x86_64_common_section (asection *sec)
3b22753a
L
5036{
5037 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5038 return bfd_com_section_ptr;
5039 else
5040 return &_bfd_elf_large_com_section;
5041}
5042
5043static bfd_boolean
5d13b3b3
AM
5044elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
5045 const Elf_Internal_Sym *sym,
351f65ca 5046 asection **psec,
5d13b3b3
AM
5047 bfd_boolean newdef,
5048 bfd_boolean olddef,
351f65ca 5049 bfd *oldbfd,
5d13b3b3 5050 const asection *oldsec)
3b22753a
L
5051{
5052 /* A normal common symbol and a large common symbol result in a
00492999
L
5053 normal common symbol. We turn the large common symbol into a
5054 normal one. */
5d13b3b3 5055 if (!olddef
3b22753a 5056 && h->root.type == bfd_link_hash_common
5d13b3b3
AM
5057 && !newdef
5058 && bfd_is_com_section (*psec)
5059 && oldsec != *psec)
3b22753a 5060 {
00492999 5061 if (sym->st_shndx == SHN_COMMON
5d13b3b3 5062 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
00492999
L
5063 {
5064 h->root.u.c.p->section
5065 = bfd_make_section_old_way (oldbfd, "COMMON");
5066 h->root.u.c.p->section->flags = SEC_ALLOC;
5067 }
5068 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5d13b3b3
AM
5069 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
5070 *psec = bfd_com_section_ptr;
3b22753a
L
5071 }
5072
5073 return TRUE;
5074}
5075
5076static int
351f65ca
L
5077elf_x86_64_additional_program_headers (bfd *abfd,
5078 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3b22753a
L
5079{
5080 asection *s;
9a2e389a 5081 int count = 0;
3b22753a
L
5082
5083 /* Check to see if we need a large readonly segment. */
5084 s = bfd_get_section_by_name (abfd, ".lrodata");
5085 if (s && (s->flags & SEC_LOAD))
5086 count++;
5087
5088 /* Check to see if we need a large data segment. Since .lbss sections
5089 is placed right after the .bss section, there should be no need for
5090 a large data segment just because of .lbss. */
5091 s = bfd_get_section_by_name (abfd, ".ldata");
5092 if (s && (s->flags & SEC_LOAD))
5093 count++;
5094
5095 return count;
5096}
5097
fdc90cb4
JJ
5098/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5099
5100static bfd_boolean
351f65ca 5101elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
fdc90cb4
JJ
5102{
5103 if (h->plt.offset != (bfd_vma) -1
5104 && !h->def_regular
5105 && !h->pointer_equality_needed)
5106 return FALSE;
5107
5108 return _bfd_elf_hash_symbol (h);
5109}
5110
c543bf9a
L
5111/* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5112
5113static bfd_boolean
5114elf_x86_64_relocs_compatible (const bfd_target *input,
5115 const bfd_target *output)
5116{
5117 return ((xvec_get_elf_backend_data (input)->s->elfclass
5118 == xvec_get_elf_backend_data (output)->s->elfclass)
5119 && _bfd_elf_relocs_compatible (input, output));
5120}
5121
9a2e389a 5122static const struct bfd_elf_special_section
351f65ca 5123 elf_x86_64_special_sections[]=
3b22753a 5124{
0112cd26
NC
5125 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5126 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5127 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
5128 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5129 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5130 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5131 { NULL, 0, 0, 0, 0 }
3b22753a
L
5132};
5133
70256ad8
AJ
5134#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
5135#define TARGET_LITTLE_NAME "elf64-x86-64"
5136#define ELF_ARCH bfd_arch_i386
ae95ffa6 5137#define ELF_TARGET_ID X86_64_ELF_DATA
70256ad8 5138#define ELF_MACHINE_CODE EM_X86_64
f7661549 5139#define ELF_MAXPAGESIZE 0x200000
2043964e 5140#define ELF_MINPAGESIZE 0x1000
24718e3b 5141#define ELF_COMMONPAGESIZE 0x1000
70256ad8
AJ
5142
5143#define elf_backend_can_gc_sections 1
51b64d56 5144#define elf_backend_can_refcount 1
70256ad8
AJ
5145#define elf_backend_want_got_plt 1
5146#define elf_backend_plt_readonly 1
5147#define elf_backend_want_plt_sym 0
5148#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 5149#define elf_backend_rela_normal 1
e41b3a13 5150#define elf_backend_plt_alignment 4
70256ad8 5151
351f65ca 5152#define elf_info_to_howto elf_x86_64_info_to_howto
70256ad8 5153
70256ad8 5154#define bfd_elf64_bfd_link_hash_table_create \
351f65ca 5155 elf_x86_64_link_hash_table_create
c25bc9fc 5156#define bfd_elf64_bfd_link_hash_table_free \
351f65ca
L
5157 elf_x86_64_link_hash_table_free
5158#define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
157090f7 5159#define bfd_elf64_bfd_reloc_name_lookup \
351f65ca 5160 elf_x86_64_reloc_name_lookup
70256ad8 5161
351f65ca 5162#define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
c543bf9a 5163#define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
351f65ca
L
5164#define elf_backend_check_relocs elf_x86_64_check_relocs
5165#define elf_backend_copy_indirect_symbol elf_x86_64_copy_indirect_symbol
5166#define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
5167#define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5168#define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
5169#define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
5170#define elf_backend_gc_sweep_hook elf_x86_64_gc_sweep_hook
5171#define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5172#define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
8fd79e71
L
5173#ifdef CORE_HEADER
5174#define elf_backend_write_core_note elf_x86_64_write_core_note
5175#endif
351f65ca
L
5176#define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5177#define elf_backend_relocate_section elf_x86_64_relocate_section
5178#define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
5179#define elf_backend_always_size_sections elf_x86_64_always_size_sections
74541ad4 5180#define elf_backend_init_index_section _bfd_elf_init_1_index_section
351f65ca 5181#define elf_backend_plt_sym_val elf_x86_64_plt_sym_val
407443a3 5182#define elf_backend_object_p elf64_x86_64_elf_object_p
351f65ca 5183#define bfd_elf64_mkobject elf_x86_64_mkobject
8d88c4ca 5184
d2b2c203 5185#define elf_backend_section_from_shdr \
351f65ca 5186 elf_x86_64_section_from_shdr
d2b2c203 5187
3b22753a 5188#define elf_backend_section_from_bfd_section \
351f65ca 5189 elf_x86_64_elf_section_from_bfd_section
3b22753a 5190#define elf_backend_add_symbol_hook \
351f65ca 5191 elf_x86_64_add_symbol_hook
3b22753a 5192#define elf_backend_symbol_processing \
351f65ca 5193 elf_x86_64_symbol_processing
3b22753a 5194#define elf_backend_common_section_index \
351f65ca 5195 elf_x86_64_common_section_index
3b22753a 5196#define elf_backend_common_section \
351f65ca 5197 elf_x86_64_common_section
3b22753a 5198#define elf_backend_common_definition \
351f65ca 5199 elf_x86_64_common_definition
3b22753a 5200#define elf_backend_merge_symbol \
351f65ca 5201 elf_x86_64_merge_symbol
3b22753a 5202#define elf_backend_special_sections \
351f65ca 5203 elf_x86_64_special_sections
3b22753a 5204#define elf_backend_additional_program_headers \
351f65ca 5205 elf_x86_64_additional_program_headers
fdc90cb4 5206#define elf_backend_hash_symbol \
351f65ca 5207 elf_x86_64_hash_symbol
3b22753a 5208
d8045f23
NC
5209#define elf_backend_post_process_headers _bfd_elf_set_osabi
5210
8d88c4ca 5211#include "elf64-target.h"
9d7cbccd
NC
5212
5213/* FreeBSD support. */
5214
5215#undef TARGET_LITTLE_SYM
5216#define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
5217#undef TARGET_LITTLE_NAME
5218#define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5219
d1036acb
L
5220#undef ELF_OSABI
5221#define ELF_OSABI ELFOSABI_FREEBSD
9d7cbccd 5222
9d7cbccd
NC
5223#undef elf64_bed
5224#define elf64_bed elf64_x86_64_fbsd_bed
5225
5226#include "elf64-target.h"
8a9036a4 5227
a6cc6b3b
RO
5228/* Solaris 2 support. */
5229
5230#undef TARGET_LITTLE_SYM
5231#define TARGET_LITTLE_SYM bfd_elf64_x86_64_sol2_vec
5232#undef TARGET_LITTLE_NAME
5233#define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5234
5235/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5236 objects won't be recognized. */
5237#undef ELF_OSABI
5238
5239#undef elf64_bed
5240#define elf64_bed elf64_x86_64_sol2_bed
5241
7dc98aea
RO
5242/* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5243 boundary. */
5244#undef elf_backend_static_tls_alignment
5245#define elf_backend_static_tls_alignment 16
5246
a6cc6b3b
RO
5247/* The Solaris 2 ABI requires a plt symbol on all platforms.
5248
5249 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5250 File, p.63. */
5251#undef elf_backend_want_plt_sym
5252#define elf_backend_want_plt_sym 1
5253
5254#include "elf64-target.h"
5255
8059fb19
RM
5256/* Native Client support. */
5257
5258#undef TARGET_LITTLE_SYM
5259#define TARGET_LITTLE_SYM bfd_elf64_x86_64_nacl_vec
5260#undef TARGET_LITTLE_NAME
5261#define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
5262#undef elf64_bed
5263#define elf64_bed elf64_x86_64_nacl_bed
5264
5265#undef ELF_MAXPAGESIZE
5266#undef ELF_MINPAGESIZE
5267#undef ELF_COMMONPAGESIZE
5268#define ELF_MAXPAGESIZE 0x10000
5269#define ELF_MINPAGESIZE 0x10000
5270#define ELF_COMMONPAGESIZE 0x10000
5271
5272/* Restore defaults. */
5273#undef ELF_OSABI
5274#undef elf_backend_static_tls_alignment
5275#undef elf_backend_want_plt_sym
5276#define elf_backend_want_plt_sym 0
5277
5278/* NaCl uses substantially different PLT entries for the same effects. */
5279
5280#undef elf_backend_plt_alignment
5281#define elf_backend_plt_alignment 5
5282#define NACL_PLT_ENTRY_SIZE 64
5283#define NACLMASK 0xe0 /* 32-byte alignment mask. */
5284
5285static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
5286 {
5287 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
5288 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
5289 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5290 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5291 0x41, 0xff, 0xe3, /* jmpq *%r11 */
5292
ea2d813e
RM
5293 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
5294 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopl %cs:0x0(%rax,%rax,1) */
5295
5296 /* 32 bytes of nop to pad out to the standard size. */
8059fb19
RM
5297 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5298 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5299 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5300 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
ea2d813e
RM
5301 0x66, /* excess data32 prefix */
5302 0x90 /* nop */
8059fb19
RM
5303 };
5304
5305static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
5306 {
5307 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
5308 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5309 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5310 0x41, 0xff, 0xe3, /* jmpq *%r11 */
5311
5312 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
5313 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5314 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5315
5316 /* Lazy GOT entries point here (32-byte aligned). */
5317 0x68, /* pushq immediate */
5318 0, 0, 0, 0, /* replaced with index into relocation table. */
5319 0xe9, /* jmp relative */
5320 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
5321
5322 /* 22 bytes of nop to pad out to the standard size. */
5323 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5324 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5325 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
5326 };
5327
5328/* .eh_frame covering the .plt section. */
5329
5330static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
5331 {
5332#if (PLT_CIE_LENGTH != 20 \
5333 || PLT_FDE_LENGTH != 36 \
5334 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
5335 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
5336# error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
5337#endif
5338 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
5339 0, 0, 0, 0, /* CIE ID */
5340 1, /* CIE version */
5341 'z', 'R', 0, /* Augmentation string */
5342 1, /* Code alignment factor */
5343 0x78, /* Data alignment factor */
5344 16, /* Return address column */
5345 1, /* Augmentation size */
5346 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
5347 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
5348 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
5349 DW_CFA_nop, DW_CFA_nop,
5350
5351 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
5352 PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
5353 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
5354 0, 0, 0, 0, /* .plt size goes here */
5355 0, /* Augmentation size */
5356 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
5357 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
5358 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
5359 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5360 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
5361 13, /* Block length */
5362 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
5363 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
5364 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5365 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
5366 DW_CFA_nop, DW_CFA_nop
5367 };
5368
5369static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
5370 {
5371 elf_x86_64_nacl_plt0_entry, /* plt0_entry */
5372 elf_x86_64_nacl_plt_entry, /* plt_entry */
5373 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
5374 2, /* plt0_got1_offset */
5375 9, /* plt0_got2_offset */
5376 13, /* plt0_got2_insn_end */
5377 3, /* plt_got_offset */
5378 33, /* plt_reloc_offset */
5379 38, /* plt_plt_offset */
5380 7, /* plt_got_insn_size */
5381 42, /* plt_plt_insn_end */
5382 32, /* plt_lazy_offset */
5383 elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
5384 sizeof (elf_x86_64_nacl_eh_frame_plt), /* eh_frame_plt_size */
5385 };
5386
5387#undef elf_backend_arch_data
5388#define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
5389
5a68afcf
RM
5390#undef elf_backend_modify_segment_map
5391#define elf_backend_modify_segment_map nacl_modify_segment_map
5392#undef elf_backend_modify_program_headers
5393#define elf_backend_modify_program_headers nacl_modify_program_headers
5394
8059fb19
RM
5395#include "elf64-target.h"
5396
5397/* Native Client x32 support. */
5398
5399#undef TARGET_LITTLE_SYM
5400#define TARGET_LITTLE_SYM bfd_elf32_x86_64_nacl_vec
5401#undef TARGET_LITTLE_NAME
5402#define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
5403#undef elf32_bed
5404#define elf32_bed elf32_x86_64_nacl_bed
5405
5406#define bfd_elf32_bfd_link_hash_table_create \
5407 elf_x86_64_link_hash_table_create
5408#define bfd_elf32_bfd_link_hash_table_free \
5409 elf_x86_64_link_hash_table_free
5410#define bfd_elf32_bfd_reloc_type_lookup \
5411 elf_x86_64_reloc_type_lookup
5412#define bfd_elf32_bfd_reloc_name_lookup \
5413 elf_x86_64_reloc_name_lookup
5414#define bfd_elf32_mkobject \
5415 elf_x86_64_mkobject
5416
5417#undef elf_backend_object_p
5418#define elf_backend_object_p \
5419 elf32_x86_64_elf_object_p
5420
5421#undef elf_backend_bfd_from_remote_memory
5422#define elf_backend_bfd_from_remote_memory \
5423 _bfd_elf32_bfd_from_remote_memory
5424
5425#undef elf_backend_size_info
5426#define elf_backend_size_info \
5427 _bfd_elf32_size_info
5428
5429#include "elf32-target.h"
5430
5431/* Restore defaults. */
5a68afcf 5432#undef elf_backend_object_p
8059fb19 5433#define elf_backend_object_p elf64_x86_64_elf_object_p
5a68afcf
RM
5434#undef elf_backend_bfd_from_remote_memory
5435#undef elf_backend_size_info
5436#undef elf_backend_modify_segment_map
5437#undef elf_backend_modify_program_headers
8059fb19 5438
8a9036a4
L
5439/* Intel L1OM support. */
5440
5441static bfd_boolean
5442elf64_l1om_elf_object_p (bfd *abfd)
5443{
5444 /* Set the right machine number for an L1OM elf64 file. */
5445 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
5446 return TRUE;
5447}
5448
5449#undef TARGET_LITTLE_SYM
5450#define TARGET_LITTLE_SYM bfd_elf64_l1om_vec
5451#undef TARGET_LITTLE_NAME
5452#define TARGET_LITTLE_NAME "elf64-l1om"
5453#undef ELF_ARCH
5454#define ELF_ARCH bfd_arch_l1om
5455
5456#undef ELF_MACHINE_CODE
5457#define ELF_MACHINE_CODE EM_L1OM
5458
5459#undef ELF_OSABI
5460
5461#undef elf64_bed
5462#define elf64_bed elf64_l1om_bed
5463
5464#undef elf_backend_object_p
5465#define elf_backend_object_p elf64_l1om_elf_object_p
5466
8059fb19
RM
5467/* Restore defaults. */
5468#undef ELF_MAXPAGESIZE
5469#undef ELF_MINPAGESIZE
5470#undef ELF_COMMONPAGESIZE
5471#define ELF_MAXPAGESIZE 0x200000
5472#define ELF_MINPAGESIZE 0x1000
5473#define ELF_COMMONPAGESIZE 0x1000
5474#undef elf_backend_plt_alignment
5475#define elf_backend_plt_alignment 4
5476#undef elf_backend_arch_data
5477#define elf_backend_arch_data &elf_x86_64_arch_bed
1a0c107f 5478
8a9036a4
L
5479#include "elf64-target.h"
5480
5481/* FreeBSD L1OM support. */
5482
5483#undef TARGET_LITTLE_SYM
5484#define TARGET_LITTLE_SYM bfd_elf64_l1om_freebsd_vec
5485#undef TARGET_LITTLE_NAME
5486#define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
5487
5488#undef ELF_OSABI
5489#define ELF_OSABI ELFOSABI_FREEBSD
5490
5491#undef elf64_bed
5492#define elf64_bed elf64_l1om_fbsd_bed
5493
8a9036a4 5494#include "elf64-target.h"
351f65ca 5495
7a9068fe
L
5496/* Intel K1OM support. */
5497
5498static bfd_boolean
5499elf64_k1om_elf_object_p (bfd *abfd)
5500{
5501 /* Set the right machine number for an K1OM elf64 file. */
5502 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
5503 return TRUE;
5504}
5505
5506#undef TARGET_LITTLE_SYM
5507#define TARGET_LITTLE_SYM bfd_elf64_k1om_vec
5508#undef TARGET_LITTLE_NAME
5509#define TARGET_LITTLE_NAME "elf64-k1om"
5510#undef ELF_ARCH
5511#define ELF_ARCH bfd_arch_k1om
5512
5513#undef ELF_MACHINE_CODE
5514#define ELF_MACHINE_CODE EM_K1OM
5515
5516#undef ELF_OSABI
5517
5518#undef elf64_bed
5519#define elf64_bed elf64_k1om_bed
5520
5521#undef elf_backend_object_p
5522#define elf_backend_object_p elf64_k1om_elf_object_p
5523
5524#undef elf_backend_static_tls_alignment
5525
5526#undef elf_backend_want_plt_sym
5527#define elf_backend_want_plt_sym 0
5528
5529#include "elf64-target.h"
5530
5531/* FreeBSD K1OM support. */
5532
5533#undef TARGET_LITTLE_SYM
5534#define TARGET_LITTLE_SYM bfd_elf64_k1om_freebsd_vec
5535#undef TARGET_LITTLE_NAME
5536#define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
5537
5538#undef ELF_OSABI
5539#define ELF_OSABI ELFOSABI_FREEBSD
5540
5541#undef elf64_bed
5542#define elf64_bed elf64_k1om_fbsd_bed
5543
5544#include "elf64-target.h"
5545
351f65ca
L
5546/* 32bit x86-64 support. */
5547
351f65ca
L
5548#undef TARGET_LITTLE_SYM
5549#define TARGET_LITTLE_SYM bfd_elf32_x86_64_vec
5550#undef TARGET_LITTLE_NAME
5551#define TARGET_LITTLE_NAME "elf32-x86-64"
8059fb19 5552#undef elf32_bed
351f65ca
L
5553
5554#undef ELF_ARCH
5555#define ELF_ARCH bfd_arch_i386
5556
5557#undef ELF_MACHINE_CODE
5558#define ELF_MACHINE_CODE EM_X86_64
5559
351f65ca
L
5560#undef ELF_OSABI
5561
351f65ca
L
5562#undef elf_backend_object_p
5563#define elf_backend_object_p \
5564 elf32_x86_64_elf_object_p
5565
5566#undef elf_backend_bfd_from_remote_memory
5567#define elf_backend_bfd_from_remote_memory \
5568 _bfd_elf32_bfd_from_remote_memory
5569
5570#undef elf_backend_size_info
5571#define elf_backend_size_info \
5572 _bfd_elf32_size_info
5573
5574#include "elf32-target.h"