]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-x86-64.c
bfd/
[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,
3ce231f8 3 2010, 2011, 2012
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 */
351 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
352
353 /* pr_pid */
354 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 24);
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 */
cedb70c5 364 elf_tdata (abfd)->core_signal
3bab7989
ML
365 = bfd_get_16 (abfd, note->descdata + 12);
366
367 /* pr_pid */
261b8d08 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
L
391 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
392 elf_tdata (abfd)->core_pid
393 = bfd_get_32 (abfd, note->descdata + 12);
394 elf_tdata (abfd)->core_program
395 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
396 elf_tdata (abfd)->core_command
397 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
398 break;
399
3bab7989 400 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
261b8d08
PA
401 elf_tdata (abfd)->core_pid
402 = bfd_get_32 (abfd, note->descdata + 24);
3bab7989
ML
403 elf_tdata (abfd)->core_program
404 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
405 elf_tdata (abfd)->core_command
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 {
414 char *command = elf_tdata (abfd)->core_command;
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;
71cb9464 1522 }
70256ad8 1523
351f65ca
L
1524 if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
1525 symtab_hdr, sym_hashes,
1526 &r_type, GOT_UNKNOWN,
1527 rel, rel_end, h, r_symndx))
c2e61a4e 1528 return FALSE;
142411ca 1529
bffbf940 1530 switch (r_type)
70256ad8 1531 {
bffbf940
JJ
1532 case R_X86_64_TLSLD:
1533 htab->tls_ld_got.refcount += 1;
1534 goto create_got;
1535
1536 case R_X86_64_TPOFF32:
351f65ca 1537 if (!info->executable && ABI_64_P (abfd))
70256ad8 1538 {
09a24cbf 1539 if (h)
4c544807
L
1540 name = h->root.root.string;
1541 else
1542 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1543 NULL);
bffbf940 1544 (*_bfd_error_handler)
d003868e
AM
1545 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1546 abfd,
4c544807 1547 x86_64_elf_howto_table[r_type].name, name);
bffbf940 1548 bfd_set_error (bfd_error_bad_value);
c2e61a4e 1549 return FALSE;
70256ad8 1550 }
bffbf940 1551 break;
c434dee6 1552
bffbf940 1553 case R_X86_64_GOTTPOFF:
1d85728f 1554 if (!info->executable)
bffbf940
JJ
1555 info->flags |= DF_STATIC_TLS;
1556 /* Fall through */
70256ad8 1557
bffbf940
JJ
1558 case R_X86_64_GOT32:
1559 case R_X86_64_GOTPCREL:
1560 case R_X86_64_TLSGD:
7b81dfbb
AJ
1561 case R_X86_64_GOT64:
1562 case R_X86_64_GOTPCREL64:
1563 case R_X86_64_GOTPLT64:
67a4f2b7
AO
1564 case R_X86_64_GOTPC32_TLSDESC:
1565 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
1566 /* This symbol requires a global offset table entry. */
1567 {
1568 int tls_type, old_tls_type;
1569
1570 switch (r_type)
1571 {
1572 default: tls_type = GOT_NORMAL; break;
1573 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1574 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
67a4f2b7
AO
1575 case R_X86_64_GOTPC32_TLSDESC:
1576 case R_X86_64_TLSDESC_CALL:
1577 tls_type = GOT_TLS_GDESC; break;
bffbf940
JJ
1578 }
1579
1580 if (h != NULL)
1581 {
7b81dfbb
AJ
1582 if (r_type == R_X86_64_GOTPLT64)
1583 {
1584 /* This relocation indicates that we also need
1585 a PLT entry, as this is a function. We don't need
1586 a PLT entry for local symbols. */
1587 h->needs_plt = 1;
1588 h->plt.refcount += 1;
1589 }
bffbf940 1590 h->got.refcount += 1;
351f65ca 1591 old_tls_type = elf_x86_64_hash_entry (h)->tls_type;
bffbf940
JJ
1592 }
1593 else
1594 {
1595 bfd_signed_vma *local_got_refcounts;
1596
1597 /* This is a global offset table entry for a local symbol. */
1598 local_got_refcounts = elf_local_got_refcounts (abfd);
1599 if (local_got_refcounts == NULL)
1600 {
1601 bfd_size_type size;
1602
1603 size = symtab_hdr->sh_info;
67a4f2b7
AO
1604 size *= sizeof (bfd_signed_vma)
1605 + sizeof (bfd_vma) + sizeof (char);
bffbf940
JJ
1606 local_got_refcounts = ((bfd_signed_vma *)
1607 bfd_zalloc (abfd, size));
1608 if (local_got_refcounts == NULL)
c2e61a4e 1609 return FALSE;
bffbf940 1610 elf_local_got_refcounts (abfd) = local_got_refcounts;
351f65ca 1611 elf_x86_64_local_tlsdesc_gotent (abfd)
67a4f2b7 1612 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
351f65ca 1613 elf_x86_64_local_got_tls_type (abfd)
67a4f2b7 1614 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
bffbf940
JJ
1615 }
1616 local_got_refcounts[r_symndx] += 1;
1617 old_tls_type
351f65ca 1618 = elf_x86_64_local_got_tls_type (abfd) [r_symndx];
bffbf940
JJ
1619 }
1620
1621 /* If a TLS symbol is accessed using IE at least once,
1622 there is no point to use dynamic model for it. */
1623 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7
AO
1624 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1625 || tls_type != GOT_TLS_IE))
bffbf940 1626 {
67a4f2b7 1627 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
bffbf940 1628 tls_type = old_tls_type;
67a4f2b7
AO
1629 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1630 && GOT_TLS_GD_ANY_P (tls_type))
1631 tls_type |= old_tls_type;
bffbf940
JJ
1632 else
1633 {
09a24cbf 1634 if (h)
4c544807
L
1635 name = h->root.root.string;
1636 else
1637 name = bfd_elf_sym_name (abfd, symtab_hdr,
1638 isym, NULL);
bffbf940 1639 (*_bfd_error_handler)
1f7a4e42 1640 (_("%B: '%s' accessed both as normal and thread local symbol"),
4c544807 1641 abfd, name);
68c4a57e 1642 bfd_set_error (bfd_error_bad_value);
c2e61a4e 1643 return FALSE;
bffbf940
JJ
1644 }
1645 }
1646
1647 if (old_tls_type != tls_type)
1648 {
1649 if (h != NULL)
351f65ca 1650 elf_x86_64_hash_entry (h)->tls_type = tls_type;
bffbf940 1651 else
351f65ca 1652 elf_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
bffbf940
JJ
1653 }
1654 }
c434dee6
AJ
1655 /* Fall through */
1656
d6ab8113
JB
1657 case R_X86_64_GOTOFF64:
1658 case R_X86_64_GOTPC32:
7b81dfbb 1659 case R_X86_64_GOTPC64:
bffbf940 1660 create_got:
6de2ae4a 1661 if (htab->elf.sgot == NULL)
c434dee6
AJ
1662 {
1663 if (htab->elf.dynobj == NULL)
1664 htab->elf.dynobj = abfd;
6de2ae4a
L
1665 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1666 info))
c2e61a4e 1667 return FALSE;
c434dee6 1668 }
70256ad8
AJ
1669 break;
1670
1671 case R_X86_64_PLT32:
1672 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
1673 actually build the entry in adjust_dynamic_symbol,
1674 because this might be a case of linking PIC code which is
1675 never referenced by a dynamic object, in which case we
1676 don't need to generate a procedure linkage table entry
1677 after all. */
70256ad8
AJ
1678
1679 /* If this is a local symbol, we resolve it directly without
407443a3 1680 creating a procedure linkage table entry. */
70256ad8
AJ
1681 if (h == NULL)
1682 continue;
1683
f5385ebf 1684 h->needs_plt = 1;
51b64d56 1685 h->plt.refcount += 1;
70256ad8
AJ
1686 break;
1687
7b81dfbb
AJ
1688 case R_X86_64_PLTOFF64:
1689 /* This tries to form the 'address' of a function relative
1690 to GOT. For global symbols we need a PLT entry. */
1691 if (h != NULL)
1692 {
1693 h->needs_plt = 1;
1694 h->plt.refcount += 1;
1695 }
1696 goto create_got;
1697
6a3e1bae
L
1698 case R_X86_64_SIZE32:
1699 case R_X86_64_SIZE64:
06a6a421 1700 size_reloc = TRUE;
6a3e1bae
L
1701 goto do_size;
1702
248775ba
L
1703 case R_X86_64_32:
1704 if (!ABI_64_P (abfd))
1705 goto pointer;
cc78d0af
AJ
1706 case R_X86_64_8:
1707 case R_X86_64_16:
70256ad8 1708 case R_X86_64_32S:
1b71fb54
AJ
1709 /* Let's help debug shared library creation. These relocs
1710 cannot be used in shared libs. Don't error out for
1711 sections we don't care about, such as debug sections or
1712 non-constant sections. */
1713 if (info->shared
1714 && (sec->flags & SEC_ALLOC) != 0
1715 && (sec->flags & SEC_READONLY) != 0)
1716 {
09a24cbf 1717 if (h)
4c544807
L
1718 name = h->root.root.string;
1719 else
1720 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1b71fb54 1721 (*_bfd_error_handler)
d003868e 1722 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
4c544807 1723 abfd, x86_64_elf_howto_table[r_type].name, name);
1b71fb54 1724 bfd_set_error (bfd_error_bad_value);
c2e61a4e 1725 return FALSE;
1b71fb54
AJ
1726 }
1727 /* Fall through. */
1728
c434dee6
AJ
1729 case R_X86_64_PC8:
1730 case R_X86_64_PC16:
70256ad8 1731 case R_X86_64_PC32:
d6ab8113 1732 case R_X86_64_PC64:
1b71fb54 1733 case R_X86_64_64:
248775ba 1734pointer:
710ab287 1735 if (h != NULL && info->executable)
c434dee6
AJ
1736 {
1737 /* If this reloc is in a read-only section, we might
1738 need a copy reloc. We can't check reliably at this
1739 stage whether the section is read-only, as input
1740 sections have not yet been mapped to output sections.
1741 Tentatively set the flag for now, and correct in
1742 adjust_dynamic_symbol. */
f5385ebf 1743 h->non_got_ref = 1;
c434dee6
AJ
1744
1745 /* We may need a .plt entry if the function this reloc
1746 refers to is in a shared lib. */
1747 h->plt.refcount += 1;
d6ab8113 1748 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
f5385ebf 1749 h->pointer_equality_needed = 1;
c434dee6 1750 }
70256ad8 1751
06a6a421 1752 size_reloc = FALSE;
6a3e1bae 1753do_size:
70256ad8
AJ
1754 /* If we are creating a shared library, and this is a reloc
1755 against a global symbol, or a non PC relative reloc
1756 against a local symbol, then we need to copy the reloc
1757 into the shared library. However, if we are linking with
1758 -Bsymbolic, we do not need to copy a reloc against a
1759 global symbol which is defined in an object we are
407443a3 1760 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
1761 this point we have not seen all the input files, so it is
1762 possible that DEF_REGULAR is not set now but will be set
c434dee6
AJ
1763 later (it is never cleared). In case of a weak definition,
1764 DEF_REGULAR may be cleared later by a strong definition in
1765 a shared library. We account for that possibility below by
1766 storing information in the relocs_copied field of the hash
1767 table entry. A similar situation occurs when creating
1768 shared libraries and symbol visibility changes render the
1769 symbol local.
1770
1771 If on the other hand, we are creating an executable, we
1772 may need to keep relocations for symbols satisfied by a
1773 dynamic library if we manage to avoid copy relocs for the
0f88be7a 1774 symbol. */
c434dee6
AJ
1775 if ((info->shared
1776 && (sec->flags & SEC_ALLOC) != 0
d8045f23 1777 && (! IS_X86_64_PCREL_TYPE (r_type)
c434dee6 1778 || (h != NULL
55255dae 1779 && (! SYMBOLIC_BIND (info, h)
c434dee6 1780 || h->root.type == bfd_link_hash_defweak
f5385ebf 1781 || !h->def_regular))))
d40d037c
AJ
1782 || (ELIMINATE_COPY_RELOCS
1783 && !info->shared
c434dee6
AJ
1784 && (sec->flags & SEC_ALLOC) != 0
1785 && h != NULL
1786 && (h->root.type == bfd_link_hash_defweak
0f88be7a 1787 || !h->def_regular)))
70256ad8 1788 {
e03a8ed8
L
1789 struct elf_dyn_relocs *p;
1790 struct elf_dyn_relocs **head;
c434dee6
AJ
1791
1792 /* We must copy these reloc types into the output file.
1793 Create a reloc section in dynobj and make room for
1794 this reloc. */
70256ad8
AJ
1795 if (sreloc == NULL)
1796 {
c434dee6
AJ
1797 if (htab->elf.dynobj == NULL)
1798 htab->elf.dynobj = abfd;
1799
83bac4b0 1800 sreloc = _bfd_elf_make_dynamic_reloc_section
82e96e07
L
1801 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
1802 abfd, /*rela?*/ TRUE);
70256ad8 1803
70256ad8 1804 if (sreloc == NULL)
c2e61a4e 1805 return FALSE;
70256ad8
AJ
1806 }
1807
c434dee6
AJ
1808 /* If this is a global symbol, we count the number of
1809 relocations we need for this symbol. */
1810 if (h != NULL)
70256ad8 1811 {
351f65ca 1812 head = &((struct elf_x86_64_link_hash_entry *) h)->dyn_relocs;
c434dee6
AJ
1813 }
1814 else
1815 {
1816 /* Track dynamic relocs needed for local syms too.
1817 We really need local syms available to do this
1818 easily. Oh well. */
c434dee6 1819 asection *s;
87d72d41 1820 void **vpp;
87d72d41
AM
1821
1822 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1823 abfd, r_symndx);
1824 if (isym == NULL)
1825 return FALSE;
1826
1827 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
c434dee6 1828 if (s == NULL)
87d72d41 1829 s = sec;
70256ad8 1830
e81d3500
DD
1831 /* Beware of type punned pointers vs strict aliasing
1832 rules. */
1833 vpp = &(elf_section_data (s)->local_dynrel);
e03a8ed8 1834 head = (struct elf_dyn_relocs **)vpp;
c434dee6 1835 }
70256ad8 1836
c434dee6
AJ
1837 p = *head;
1838 if (p == NULL || p->sec != sec)
1839 {
1840 bfd_size_type amt = sizeof *p;
d8045f23 1841
e03a8ed8 1842 p = ((struct elf_dyn_relocs *)
c434dee6 1843 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 1844 if (p == NULL)
c2e61a4e 1845 return FALSE;
c434dee6
AJ
1846 p->next = *head;
1847 *head = p;
1848 p->sec = sec;
1849 p->count = 0;
1850 p->pc_count = 0;
70256ad8 1851 }
c434dee6
AJ
1852
1853 p->count += 1;
06a6a421
L
1854 /* Count size relocation as PC-relative relocation. */
1855 if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
c434dee6 1856 p->pc_count += 1;
70256ad8
AJ
1857 }
1858 break;
fe4770f4
AJ
1859
1860 /* This relocation describes the C++ object vtable hierarchy.
1861 Reconstruct it for later use during GC. */
1862 case R_X86_64_GNU_VTINHERIT:
c152c796 1863 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
c2e61a4e 1864 return FALSE;
fe4770f4
AJ
1865 break;
1866
1867 /* This relocation describes which C++ vtable entries are actually
1868 used. Record for later use during GC. */
1869 case R_X86_64_GNU_VTENTRY:
d17e0c6e
JB
1870 BFD_ASSERT (h != NULL);
1871 if (h != NULL
1872 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
c2e61a4e 1873 return FALSE;
fe4770f4 1874 break;
c434dee6
AJ
1875
1876 default:
1877 break;
70256ad8
AJ
1878 }
1879 }
1880
b34976b6 1881 return TRUE;
70256ad8
AJ
1882}
1883
1884/* Return the section that should be marked against GC for a given
407443a3 1885 relocation. */
70256ad8
AJ
1886
1887static asection *
351f65ca
L
1888elf_x86_64_gc_mark_hook (asection *sec,
1889 struct bfd_link_info *info,
1890 Elf_Internal_Rela *rel,
1891 struct elf_link_hash_entry *h,
1892 Elf_Internal_Sym *sym)
70256ad8
AJ
1893{
1894 if (h != NULL)
351f65ca 1895 switch (ELF32_R_TYPE (rel->r_info))
07adf181
AM
1896 {
1897 case R_X86_64_GNU_VTINHERIT:
1898 case R_X86_64_GNU_VTENTRY:
1899 return NULL;
1900 }
1901
1902 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
70256ad8
AJ
1903}
1904
407443a3 1905/* Update the got entry reference counts for the section being removed. */
70256ad8 1906
b34976b6 1907static bfd_boolean
351f65ca
L
1908elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1909 asection *sec,
1910 const Elf_Internal_Rela *relocs)
70256ad8 1911{
351f65ca 1912 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
1913 Elf_Internal_Shdr *symtab_hdr;
1914 struct elf_link_hash_entry **sym_hashes;
1915 bfd_signed_vma *local_got_refcounts;
1916 const Elf_Internal_Rela *rel, *relend;
c434dee6 1917
7dda2462
TG
1918 if (info->relocatable)
1919 return TRUE;
1920
351f65ca 1921 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
1922 if (htab == NULL)
1923 return FALSE;
1924
c434dee6 1925 elf_section_data (sec)->local_dynrel = NULL;
70256ad8 1926
0ffa91dd 1927 symtab_hdr = &elf_symtab_hdr (abfd);
70256ad8
AJ
1928 sym_hashes = elf_sym_hashes (abfd);
1929 local_got_refcounts = elf_local_got_refcounts (abfd);
1930
351f65ca 1931 htab = elf_x86_64_hash_table (info);
70256ad8
AJ
1932 relend = relocs + sec->reloc_count;
1933 for (rel = relocs; rel < relend; rel++)
26e41594
AM
1934 {
1935 unsigned long r_symndx;
1936 unsigned int r_type;
1937 struct elf_link_hash_entry *h = NULL;
70256ad8 1938
351f65ca 1939 r_symndx = htab->r_sym (rel->r_info);
26e41594
AM
1940 if (r_symndx >= symtab_hdr->sh_info)
1941 {
26e41594 1942 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
1943 while (h->root.type == bfd_link_hash_indirect
1944 || h->root.type == bfd_link_hash_warning)
1945 h = (struct elf_link_hash_entry *) h->root.u.i.link;
26e41594 1946 }
bb1cb422
L
1947 else
1948 {
1949 /* A local symbol. */
1950 Elf_Internal_Sym *isym;
1951
1952 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1953 abfd, r_symndx);
1954
1955 /* Check relocation against local STT_GNU_IFUNC symbol. */
1956 if (isym != NULL
82e96e07 1957 && ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
bb1cb422 1958 {
351f65ca 1959 h = elf_x86_64_get_local_sym_hash (htab, abfd, rel, FALSE);
bb1cb422
L
1960 if (h == NULL)
1961 abort ();
1962 }
1963 }
c434dee6 1964
3db2e7dd
L
1965 if (h)
1966 {
1967 struct elf_x86_64_link_hash_entry *eh;
1968 struct elf_dyn_relocs **pp;
1969 struct elf_dyn_relocs *p;
1970
1971 eh = (struct elf_x86_64_link_hash_entry *) h;
1972
1973 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1974 if (p->sec == sec)
1975 {
1976 /* Everything must go for SEC. */
1977 *pp = p->next;
1978 break;
1979 }
1980 }
1981
351f65ca
L
1982 r_type = ELF32_R_TYPE (rel->r_info);
1983 if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
1984 symtab_hdr, sym_hashes,
1985 &r_type, GOT_UNKNOWN,
1986 rel, relend, h, r_symndx))
142411ca
L
1987 return FALSE;
1988
26e41594
AM
1989 switch (r_type)
1990 {
1991 case R_X86_64_TLSLD:
4dfe6ac6
NC
1992 if (htab->tls_ld_got.refcount > 0)
1993 htab->tls_ld_got.refcount -= 1;
26e41594 1994 break;
c434dee6 1995
26e41594 1996 case R_X86_64_TLSGD:
67a4f2b7
AO
1997 case R_X86_64_GOTPC32_TLSDESC:
1998 case R_X86_64_TLSDESC_CALL:
26e41594
AM
1999 case R_X86_64_GOTTPOFF:
2000 case R_X86_64_GOT32:
2001 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
2002 case R_X86_64_GOT64:
2003 case R_X86_64_GOTPCREL64:
2004 case R_X86_64_GOTPLT64:
26e41594
AM
2005 if (h != NULL)
2006 {
7b81dfbb 2007 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
eed180f8 2008 h->plt.refcount -= 1;
26e41594
AM
2009 if (h->got.refcount > 0)
2010 h->got.refcount -= 1;
bb1cb422
L
2011 if (h->type == STT_GNU_IFUNC)
2012 {
2013 if (h->plt.refcount > 0)
2014 h->plt.refcount -= 1;
2015 }
26e41594
AM
2016 }
2017 else if (local_got_refcounts != NULL)
2018 {
2019 if (local_got_refcounts[r_symndx] > 0)
2020 local_got_refcounts[r_symndx] -= 1;
2021 }
2022 break;
c434dee6 2023
26e41594
AM
2024 case R_X86_64_8:
2025 case R_X86_64_16:
2026 case R_X86_64_32:
2027 case R_X86_64_64:
2028 case R_X86_64_32S:
2029 case R_X86_64_PC8:
2030 case R_X86_64_PC16:
2031 case R_X86_64_PC32:
d6ab8113 2032 case R_X86_64_PC64:
1788fc08
L
2033 case R_X86_64_SIZE32:
2034 case R_X86_64_SIZE64:
3db2e7dd
L
2035 if (info->shared
2036 && (h == NULL || h->type != STT_GNU_IFUNC))
26e41594
AM
2037 break;
2038 /* Fall thru */
c434dee6 2039
26e41594 2040 case R_X86_64_PLT32:
7b81dfbb 2041 case R_X86_64_PLTOFF64:
26e41594
AM
2042 if (h != NULL)
2043 {
2044 if (h->plt.refcount > 0)
2045 h->plt.refcount -= 1;
2046 }
2047 break;
70256ad8 2048
26e41594
AM
2049 default:
2050 break;
2051 }
2052 }
70256ad8 2053
b34976b6 2054 return TRUE;
70256ad8
AJ
2055}
2056
2057/* Adjust a symbol defined by a dynamic object and referenced by a
2058 regular object. The current definition is in some section of the
2059 dynamic object, but we're not including those sections. We have to
2060 change the definition to something the rest of the link can
407443a3 2061 understand. */
70256ad8 2062
b34976b6 2063static bfd_boolean
351f65ca
L
2064elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
2065 struct elf_link_hash_entry *h)
70256ad8 2066{
351f65ca 2067 struct elf_x86_64_link_hash_table *htab;
70256ad8 2068 asection *s;
5ca5bb35
L
2069 struct elf_x86_64_link_hash_entry *eh;
2070 struct elf_dyn_relocs *p;
70256ad8 2071
cbe950e9
L
2072 /* STT_GNU_IFUNC symbol must go through PLT. */
2073 if (h->type == STT_GNU_IFUNC)
2074 {
73bcf233
L
2075 /* All local STT_GNU_IFUNC references must be treate as local
2076 calls via local PLT. */
5ca5bb35
L
2077 if (h->ref_regular
2078 && SYMBOL_CALLS_LOCAL (info, h))
2079 {
73bcf233 2080 bfd_size_type pc_count = 0, count = 0;
5ca5bb35
L
2081 struct elf_dyn_relocs **pp;
2082
2083 eh = (struct elf_x86_64_link_hash_entry *) h;
2084 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2085 {
2086 pc_count += p->pc_count;
2087 p->count -= p->pc_count;
2088 p->pc_count = 0;
73bcf233 2089 count += p->count;
5ca5bb35
L
2090 if (p->count == 0)
2091 *pp = p->next;
2092 else
2093 pp = &p->next;
2094 }
2095
73bcf233 2096 if (pc_count || count)
5ca5bb35
L
2097 {
2098 h->needs_plt = 1;
5ca5bb35 2099 h->non_got_ref = 1;
a5479e5f
L
2100 if (h->plt.refcount <= 0)
2101 h->plt.refcount = 1;
2102 else
2103 h->plt.refcount += 1;
5ca5bb35
L
2104 }
2105 }
2106
cbe950e9
L
2107 if (h->plt.refcount <= 0)
2108 {
2109 h->plt.offset = (bfd_vma) -1;
2110 h->needs_plt = 0;
2111 }
2112 return TRUE;
2113 }
2114
70256ad8
AJ
2115 /* If this is a function, put it in the procedure linkage table. We
2116 will fill in the contents of the procedure linkage table later,
2117 when we know the address of the .got section. */
2118 if (h->type == STT_FUNC
f5385ebf 2119 || h->needs_plt)
70256ad8 2120 {
c434dee6 2121 if (h->plt.refcount <= 0
27482721
AJ
2122 || SYMBOL_CALLS_LOCAL (info, h)
2123 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2124 && h->root.type == bfd_link_hash_undefweak))
70256ad8 2125 {
70256ad8
AJ
2126 /* This case can occur if we saw a PLT32 reloc in an input
2127 file, but the symbol was never referred to by a dynamic
2128 object, or if all references were garbage collected. In
2129 such a case, we don't actually need to build a procedure
2130 linkage table, and we can just do a PC32 reloc instead. */
70256ad8 2131 h->plt.offset = (bfd_vma) -1;
f5385ebf 2132 h->needs_plt = 0;
70256ad8
AJ
2133 }
2134
b34976b6 2135 return TRUE;
70256ad8 2136 }
bbd7ec4a 2137 else
c434dee6
AJ
2138 /* It's possible that we incorrectly decided a .plt reloc was
2139 needed for an R_X86_64_PC32 reloc to a non-function sym in
2140 check_relocs. We can't decide accurately between function and
2141 non-function syms in check-relocs; Objects loaded later in
2142 the link may change h->type. So fix it now. */
bbd7ec4a 2143 h->plt.offset = (bfd_vma) -1;
70256ad8
AJ
2144
2145 /* If this is a weak symbol, and there is a real definition, the
2146 processor independent code will have arranged for us to see the
407443a3 2147 real definition first, and we can just use the same value. */
f6e332e6 2148 if (h->u.weakdef != NULL)
70256ad8 2149 {
f6e332e6
AM
2150 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2151 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2152 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2153 h->root.u.def.value = h->u.weakdef->root.u.def.value;
d40d037c 2154 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
f6e332e6 2155 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 2156 return TRUE;
70256ad8
AJ
2157 }
2158
2159 /* This is a reference to a symbol defined by a dynamic object which
407443a3 2160 is not a function. */
70256ad8
AJ
2161
2162 /* If we are creating a shared library, we must presume that the
2163 only references to the symbol are via the global offset table.
2164 For such cases we need not do anything here; the relocations will
407443a3 2165 be handled correctly by relocate_section. */
70256ad8 2166 if (info->shared)
b34976b6 2167 return TRUE;
70256ad8
AJ
2168
2169 /* If there are no references to this symbol that do not use the
2170 GOT, we don't need to generate a copy reloc. */
f5385ebf 2171 if (!h->non_got_ref)
b34976b6 2172 return TRUE;
70256ad8 2173
c434dee6
AJ
2174 /* If -z nocopyreloc was given, we won't generate them either. */
2175 if (info->nocopyreloc)
2176 {
f5385ebf 2177 h->non_got_ref = 0;
b34976b6 2178 return TRUE;
c434dee6
AJ
2179 }
2180
d40d037c 2181 if (ELIMINATE_COPY_RELOCS)
c434dee6 2182 {
351f65ca 2183 eh = (struct elf_x86_64_link_hash_entry *) h;
d40d037c
AJ
2184 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2185 {
2186 s = p->sec->output_section;
2187 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2188 break;
2189 }
2190
2191 /* If we didn't find any dynamic relocs in read-only sections, then
2192 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2193 if (p == NULL)
2194 {
f5385ebf 2195 h->non_got_ref = 0;
d40d037c
AJ
2196 return TRUE;
2197 }
c434dee6
AJ
2198 }
2199
70256ad8 2200 /* We must allocate the symbol in our .dynbss section, which will
407443a3 2201 become part of the .bss section of the executable. There will be
70256ad8
AJ
2202 an entry for this symbol in the .dynsym section. The dynamic
2203 object will contain position independent code, so all references
2204 from the dynamic object to this symbol will go through the global
2205 offset table. The dynamic linker will use the .dynsym entry to
2206 determine the address it must put in the global offset table, so
2207 both the dynamic object and the regular object will refer to the
2208 same memory location for the variable. */
2209
351f65ca 2210 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2211 if (htab == NULL)
2212 return FALSE;
70256ad8
AJ
2213
2214 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
2215 to copy the initial value out of the dynamic object and into the
cedb70c5 2216 runtime process image. */
1d7e9d18 2217 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
70256ad8 2218 {
351f65ca
L
2219 const struct elf_backend_data *bed;
2220 bed = get_elf_backend_data (info->output_bfd);
2221 htab->srelbss->size += bed->s->sizeof_rela;
f5385ebf 2222 h->needs_copy = 1;
70256ad8
AJ
2223 }
2224
c434dee6 2225 s = htab->sdynbss;
70256ad8 2226
027297b7 2227 return _bfd_elf_adjust_dynamic_copy (h, s);
70256ad8
AJ
2228}
2229
c434dee6
AJ
2230/* Allocate space in .plt, .got and associated reloc sections for
2231 dynamic relocs. */
2232
b34976b6 2233static bfd_boolean
351f65ca 2234elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
2235{
2236 struct bfd_link_info *info;
351f65ca
L
2237 struct elf_x86_64_link_hash_table *htab;
2238 struct elf_x86_64_link_hash_entry *eh;
e03a8ed8 2239 struct elf_dyn_relocs *p;
351f65ca 2240 const struct elf_backend_data *bed;
eed180f8 2241 unsigned int plt_entry_size;
c434dee6 2242
e92d460e 2243 if (h->root.type == bfd_link_hash_indirect)
b34976b6 2244 return TRUE;
c434dee6 2245
351f65ca 2246 eh = (struct elf_x86_64_link_hash_entry *) h;
e92d460e 2247
c434dee6 2248 info = (struct bfd_link_info *) inf;
351f65ca 2249 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2250 if (htab == NULL)
2251 return FALSE;
351f65ca 2252 bed = get_elf_backend_data (info->output_bfd);
eed180f8 2253 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
c434dee6 2254
cbe950e9
L
2255 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2256 here if it is defined and referenced in a non-shared object. */
2257 if (h->type == STT_GNU_IFUNC
2258 && h->def_regular)
e03a8ed8
L
2259 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2260 &eh->dyn_relocs,
eed180f8 2261 plt_entry_size,
e03a8ed8 2262 GOT_ENTRY_SIZE);
cbe950e9
L
2263 else if (htab->elf.dynamic_sections_created
2264 && h->plt.refcount > 0)
c434dee6
AJ
2265 {
2266 /* Make sure this symbol is output as a dynamic symbol.
2267 Undefined weak syms won't yet be marked as dynamic. */
2268 if (h->dynindx == -1
f5385ebf 2269 && !h->forced_local)
c434dee6 2270 {
c152c796 2271 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2272 return FALSE;
c434dee6
AJ
2273 }
2274
27482721
AJ
2275 if (info->shared
2276 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
c434dee6 2277 {
6de2ae4a 2278 asection *s = htab->elf.splt;
c434dee6
AJ
2279
2280 /* If this is the first .plt entry, make room for the special
2281 first entry. */
eea6121a 2282 if (s->size == 0)
eed180f8 2283 s->size += plt_entry_size;
c434dee6 2284
eea6121a 2285 h->plt.offset = s->size;
c434dee6
AJ
2286
2287 /* If this symbol is not defined in a regular file, and we are
2288 not generating a shared library, then set the symbol to this
2289 location in the .plt. This is required to make function
2290 pointers compare as equal between the normal executable and
2291 the shared library. */
2292 if (! info->shared
f5385ebf 2293 && !h->def_regular)
c434dee6
AJ
2294 {
2295 h->root.u.def.section = s;
2296 h->root.u.def.value = h->plt.offset;
2297 }
2298
2299 /* Make room for this entry. */
eed180f8 2300 s->size += plt_entry_size;
c434dee6
AJ
2301
2302 /* We also need to make an entry in the .got.plt section, which
2303 will be placed in the .got section by the linker script. */
6de2ae4a 2304 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
c434dee6
AJ
2305
2306 /* We also need to make an entry in the .rela.plt section. */
351f65ca 2307 htab->elf.srelplt->size += bed->s->sizeof_rela;
6de2ae4a 2308 htab->elf.srelplt->reloc_count++;
c434dee6
AJ
2309 }
2310 else
2311 {
2312 h->plt.offset = (bfd_vma) -1;
f5385ebf 2313 h->needs_plt = 0;
c434dee6
AJ
2314 }
2315 }
2316 else
2317 {
2318 h->plt.offset = (bfd_vma) -1;
f5385ebf 2319 h->needs_plt = 0;
c434dee6
AJ
2320 }
2321
67a4f2b7
AO
2322 eh->tlsdesc_got = (bfd_vma) -1;
2323
bffbf940
JJ
2324 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
2325 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
2326 if (h->got.refcount > 0
1d85728f 2327 && info->executable
bffbf940 2328 && h->dynindx == -1
351f65ca 2329 && elf_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
d8045f23
NC
2330 {
2331 h->got.offset = (bfd_vma) -1;
2332 }
bffbf940 2333 else if (h->got.refcount > 0)
c434dee6
AJ
2334 {
2335 asection *s;
b34976b6 2336 bfd_boolean dyn;
351f65ca 2337 int tls_type = elf_x86_64_hash_entry (h)->tls_type;
c434dee6
AJ
2338
2339 /* Make sure this symbol is output as a dynamic symbol.
2340 Undefined weak syms won't yet be marked as dynamic. */
2341 if (h->dynindx == -1
f5385ebf 2342 && !h->forced_local)
c434dee6 2343 {
c152c796 2344 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 2345 return FALSE;
c434dee6
AJ
2346 }
2347
67a4f2b7
AO
2348 if (GOT_TLS_GDESC_P (tls_type))
2349 {
6de2ae4a 2350 eh->tlsdesc_got = htab->elf.sgotplt->size
351f65ca 2351 - elf_x86_64_compute_jump_table_size (htab);
6de2ae4a 2352 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
67a4f2b7
AO
2353 h->got.offset = (bfd_vma) -2;
2354 }
2355 if (! GOT_TLS_GDESC_P (tls_type)
2356 || GOT_TLS_GD_P (tls_type))
2357 {
6de2ae4a 2358 s = htab->elf.sgot;
67a4f2b7
AO
2359 h->got.offset = s->size;
2360 s->size += GOT_ENTRY_SIZE;
2361 if (GOT_TLS_GD_P (tls_type))
2362 s->size += GOT_ENTRY_SIZE;
2363 }
c434dee6 2364 dyn = htab->elf.dynamic_sections_created;
bffbf940
JJ
2365 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2366 and two if global.
2367 R_X86_64_GOTTPOFF needs one dynamic relocation. */
67a4f2b7 2368 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
bffbf940 2369 || tls_type == GOT_TLS_IE)
351f65ca 2370 htab->elf.srelgot->size += bed->s->sizeof_rela;
67a4f2b7 2371 else if (GOT_TLS_GD_P (tls_type))
351f65ca 2372 htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
67a4f2b7
AO
2373 else if (! GOT_TLS_GDESC_P (tls_type)
2374 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2375 || h->root.type != bfd_link_hash_undefweak)
27482721
AJ
2376 && (info->shared
2377 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
351f65ca 2378 htab->elf.srelgot->size += bed->s->sizeof_rela;
67a4f2b7
AO
2379 if (GOT_TLS_GDESC_P (tls_type))
2380 {
351f65ca 2381 htab->elf.srelplt->size += bed->s->sizeof_rela;
67a4f2b7
AO
2382 htab->tlsdesc_plt = (bfd_vma) -1;
2383 }
c434dee6
AJ
2384 }
2385 else
2386 h->got.offset = (bfd_vma) -1;
2387
c434dee6 2388 if (eh->dyn_relocs == NULL)
b34976b6 2389 return TRUE;
c434dee6
AJ
2390
2391 /* In the shared -Bsymbolic case, discard space allocated for
2392 dynamic pc-relative relocs against symbols which turn out to be
2393 defined in regular objects. For the normal shared case, discard
2394 space for pc-relative relocs that have become local due to symbol
2395 visibility changes. */
2396
2397 if (info->shared)
2398 {
27482721
AJ
2399 /* Relocs that use pc_count are those that appear on a call
2400 insn, or certain REL relocs that can generated via assembly.
2401 We want calls to protected symbols to resolve directly to the
2402 function rather than going via the plt. If people want
2403 function pointer comparisons to work as expected then they
2404 should avoid writing weird assembly. */
2405 if (SYMBOL_CALLS_LOCAL (info, h))
c434dee6 2406 {
e03a8ed8 2407 struct elf_dyn_relocs **pp;
c434dee6
AJ
2408
2409 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2410 {
2411 p->count -= p->pc_count;
2412 p->pc_count = 0;
2413 if (p->count == 0)
2414 *pp = p->next;
2415 else
2416 pp = &p->next;
2417 }
2418 }
4e795f50
AM
2419
2420 /* Also discard relocs on undefined weak syms with non-default
2421 visibility. */
22d606e9 2422 if (eh->dyn_relocs != NULL
4e795f50 2423 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
2424 {
2425 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2426 eh->dyn_relocs = NULL;
2427
2428 /* Make sure undefined weak symbols are output as a dynamic
2429 symbol in PIEs. */
2430 else if (h->dynindx == -1
d8045f23
NC
2431 && ! h->forced_local
2432 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2433 return FALSE;
22d606e9 2434 }
cbe950e9 2435
d8045f23 2436 }
d40d037c 2437 else if (ELIMINATE_COPY_RELOCS)
c434dee6
AJ
2438 {
2439 /* For the non-shared case, discard space for relocs against
2440 symbols which turn out to need copy relocs or are not
2441 dynamic. */
2442
f5385ebf
AM
2443 if (!h->non_got_ref
2444 && ((h->def_dynamic
2445 && !h->def_regular)
c434dee6
AJ
2446 || (htab->elf.dynamic_sections_created
2447 && (h->root.type == bfd_link_hash_undefweak
2448 || h->root.type == bfd_link_hash_undefined))))
2449 {
2450 /* Make sure this symbol is output as a dynamic symbol.
2451 Undefined weak syms won't yet be marked as dynamic. */
2452 if (h->dynindx == -1
d8045f23
NC
2453 && ! h->forced_local
2454 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2455 return FALSE;
c434dee6
AJ
2456
2457 /* If that succeeded, we know we'll be keeping all the
2458 relocs. */
2459 if (h->dynindx != -1)
2460 goto keep;
2461 }
2462
2463 eh->dyn_relocs = NULL;
2464
2465 keep: ;
2466 }
2467
2468 /* Finally, allocate space. */
2469 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2470 {
e7c33416
NC
2471 asection * sreloc;
2472
cbe950e9 2473 sreloc = elf_section_data (p->sec)->sreloc;
e7c33416
NC
2474
2475 BFD_ASSERT (sreloc != NULL);
2476
351f65ca 2477 sreloc->size += p->count * bed->s->sizeof_rela;
c434dee6
AJ
2478 }
2479
b34976b6 2480 return TRUE;
c434dee6
AJ
2481}
2482
c25bc9fc
L
2483/* Allocate space in .plt, .got and associated reloc sections for
2484 local dynamic relocs. */
2485
2486static bfd_boolean
351f65ca 2487elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
c25bc9fc
L
2488{
2489 struct elf_link_hash_entry *h
2490 = (struct elf_link_hash_entry *) *slot;
2491
2492 if (h->type != STT_GNU_IFUNC
2493 || !h->def_regular
2494 || !h->ref_regular
2495 || !h->forced_local
2496 || h->root.type != bfd_link_hash_defined)
2497 abort ();
2498
351f65ca 2499 return elf_x86_64_allocate_dynrelocs (h, inf);
c25bc9fc
L
2500}
2501
c434dee6
AJ
2502/* Find any dynamic relocs that apply to read-only sections. */
2503
b34976b6 2504static bfd_boolean
351f65ca
L
2505elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h,
2506 void * inf)
c434dee6 2507{
351f65ca 2508 struct elf_x86_64_link_hash_entry *eh;
e03a8ed8 2509 struct elf_dyn_relocs *p;
c434dee6 2510
aa715242
L
2511 /* Skip local IFUNC symbols. */
2512 if (h->forced_local && h->type == STT_GNU_IFUNC)
2513 return TRUE;
2514
351f65ca 2515 eh = (struct elf_x86_64_link_hash_entry *) h;
c434dee6
AJ
2516 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2517 {
2518 asection *s = p->sec->output_section;
2519
2520 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2521 {
2522 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2523
2524 info->flags |= DF_TEXTREL;
2525
b70321a2
L
2526 if (info->warn_shared_textrel && info->shared)
2527 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'.\n"),
2528 p->sec->owner, h->root.root.string,
2529 p->sec);
2530
c434dee6 2531 /* Not an error, just cut short the traversal. */
b34976b6 2532 return FALSE;
c434dee6
AJ
2533 }
2534 }
b34976b6 2535 return TRUE;
c434dee6
AJ
2536}
2537
daa67607
L
2538/* Convert
2539 mov foo@GOTPCREL(%rip), %reg
2540 to
2541 lea foo(%rip), %reg
2542 with the local symbol, foo. */
2543
2544static bfd_boolean
2545elf_x86_64_convert_mov_to_lea (bfd *abfd, asection *sec,
2546 struct bfd_link_info *link_info)
2547{
2548 Elf_Internal_Shdr *symtab_hdr;
2549 Elf_Internal_Rela *internal_relocs;
2550 Elf_Internal_Rela *irel, *irelend;
2551 bfd_byte *contents;
2552 struct elf_x86_64_link_hash_table *htab;
2553 bfd_boolean changed_contents;
2554 bfd_boolean changed_relocs;
2555 bfd_signed_vma *local_got_refcounts;
2556
2557 /* Don't even try to convert non-ELF outputs. */
2558 if (!is_elf_hash_table (link_info->hash))
2559 return FALSE;
2560
fbdc86d9 2561 /* Nothing to do if there are no codes, no relocations or no output. */
daa67607 2562 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
fbdc86d9
L
2563 || sec->reloc_count == 0
2564 || discarded_section (sec))
daa67607
L
2565 return TRUE;
2566
2567 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2568
2569 /* Load the relocations for this section. */
2570 internal_relocs = (_bfd_elf_link_read_relocs
2571 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
2572 link_info->keep_memory));
2573 if (internal_relocs == NULL)
2574 return FALSE;
2575
2576 htab = elf_x86_64_hash_table (link_info);
2577 changed_contents = FALSE;
2578 changed_relocs = FALSE;
2579 local_got_refcounts = elf_local_got_refcounts (abfd);
2580
2581 /* Get the section contents. */
2582 if (elf_section_data (sec)->this_hdr.contents != NULL)
2583 contents = elf_section_data (sec)->this_hdr.contents;
2584 else
2585 {
2586 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2587 goto error_return;
2588 }
2589
2590 irelend = internal_relocs + sec->reloc_count;
2591 for (irel = internal_relocs; irel < irelend; irel++)
2592 {
2593 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
2594 unsigned int r_symndx = htab->r_sym (irel->r_info);
2595 unsigned int indx;
2596 struct elf_link_hash_entry *h;
2597
2598 if (r_type != R_X86_64_GOTPCREL)
2599 continue;
2600
2601 /* Get the symbol referred to by the reloc. */
2602 if (r_symndx < symtab_hdr->sh_info)
2603 {
2604 Elf_Internal_Sym *isym;
2605
2606 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2607 abfd, r_symndx);
2608
2609 /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation. */
2610 if (ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC
2611 && bfd_get_8 (input_bfd,
2612 contents + irel->r_offset - 2) == 0x8b)
2613 {
2614 bfd_put_8 (output_bfd, 0x8d,
2615 contents + irel->r_offset - 2);
2616 irel->r_info = htab->r_info (r_symndx, R_X86_64_PC32);
2617 if (local_got_refcounts != NULL
2618 && local_got_refcounts[r_symndx] > 0)
2619 local_got_refcounts[r_symndx] -= 1;
2620 changed_contents = TRUE;
2621 changed_relocs = TRUE;
2622 }
2623 continue;
2624 }
2625
2626 indx = r_symndx - symtab_hdr->sh_info;
2627 h = elf_sym_hashes (abfd)[indx];
2628 BFD_ASSERT (h != NULL);
2629
2630 while (h->root.type == bfd_link_hash_indirect
2631 || h->root.type == bfd_link_hash_warning)
2632 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2633
3f65f599
L
2634 /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation. We also
2635 avoid optimizing _DYNAMIC since ld.so may use its link-time
2636 address. */
daa67607
L
2637 if (h->def_regular
2638 && h->type != STT_GNU_IFUNC
9637f6ef 2639 && h != htab->elf.hdynamic
daa67607
L
2640 && SYMBOL_REFERENCES_LOCAL (link_info, h)
2641 && bfd_get_8 (input_bfd,
2642 contents + irel->r_offset - 2) == 0x8b)
2643 {
2644 bfd_put_8 (output_bfd, 0x8d,
2645 contents + irel->r_offset - 2);
2646 irel->r_info = htab->r_info (r_symndx, R_X86_64_PC32);
2647 if (h->got.refcount > 0)
2648 h->got.refcount -= 1;
2649 changed_contents = TRUE;
2650 changed_relocs = TRUE;
2651 }
2652 }
2653
2654 if (contents != NULL
2655 && elf_section_data (sec)->this_hdr.contents != contents)
2656 {
2657 if (!changed_contents && !link_info->keep_memory)
2658 free (contents);
2659 else
2660 {
2661 /* Cache the section contents for elf_link_input_bfd. */
2662 elf_section_data (sec)->this_hdr.contents = contents;
2663 }
2664 }
2665
2666 if (elf_section_data (sec)->relocs != internal_relocs)
2667 {
2668 if (!changed_relocs)
2669 free (internal_relocs);
2670 else
2671 elf_section_data (sec)->relocs = internal_relocs;
2672 }
2673
2674 return TRUE;
2675
2676 error_return:
2677 if (contents != NULL
2678 && elf_section_data (sec)->this_hdr.contents != contents)
2679 free (contents);
2680 if (internal_relocs != NULL
2681 && elf_section_data (sec)->relocs != internal_relocs)
2682 free (internal_relocs);
2683 return FALSE;
2684}
2685
70256ad8
AJ
2686/* Set the sizes of the dynamic sections. */
2687
b34976b6 2688static bfd_boolean
351f65ca
L
2689elf_x86_64_size_dynamic_sections (bfd *output_bfd,
2690 struct bfd_link_info *info)
70256ad8 2691{
351f65ca 2692 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
2693 bfd *dynobj;
2694 asection *s;
b34976b6 2695 bfd_boolean relocs;
c434dee6 2696 bfd *ibfd;
351f65ca 2697 const struct elf_backend_data *bed;
70256ad8 2698
351f65ca 2699 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
2700 if (htab == NULL)
2701 return FALSE;
351f65ca 2702 bed = get_elf_backend_data (output_bfd);
4dfe6ac6 2703
c434dee6
AJ
2704 dynobj = htab->elf.dynobj;
2705 if (dynobj == NULL)
2706 abort ();
70256ad8 2707
c434dee6 2708 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
2709 {
2710 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 2711 if (info->executable)
70256ad8 2712 {
3d4d4302 2713 s = bfd_get_linker_section (dynobj, ".interp");
c434dee6
AJ
2714 if (s == NULL)
2715 abort ();
351f65ca
L
2716 s->size = htab->dynamic_interpreter_size;
2717 s->contents = (unsigned char *) htab->dynamic_interpreter;
70256ad8
AJ
2718 }
2719 }
70256ad8 2720
c434dee6
AJ
2721 /* Set up .got offsets for local syms, and space for local dynamic
2722 relocs. */
2723 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
70256ad8 2724 {
c434dee6
AJ
2725 bfd_signed_vma *local_got;
2726 bfd_signed_vma *end_local_got;
bffbf940 2727 char *local_tls_type;
67a4f2b7 2728 bfd_vma *local_tlsdesc_gotent;
c434dee6
AJ
2729 bfd_size_type locsymcount;
2730 Elf_Internal_Shdr *symtab_hdr;
2731 asection *srel;
70256ad8 2732
0ffa91dd 2733 if (! is_x86_64_elf (ibfd))
70256ad8
AJ
2734 continue;
2735
c434dee6 2736 for (s = ibfd->sections; s != NULL; s = s->next)
70256ad8 2737 {
e03a8ed8 2738 struct elf_dyn_relocs *p;
c434dee6 2739
daa67607
L
2740 if (!elf_x86_64_convert_mov_to_lea (ibfd, s, info))
2741 return FALSE;
2742
e03a8ed8 2743 for (p = (struct elf_dyn_relocs *)
e81d3500 2744 (elf_section_data (s)->local_dynrel);
c434dee6
AJ
2745 p != NULL;
2746 p = p->next)
70256ad8 2747 {
c434dee6
AJ
2748 if (!bfd_is_abs_section (p->sec)
2749 && bfd_is_abs_section (p->sec->output_section))
2750 {
2751 /* Input section has been discarded, either because
2752 it is a copy of a linkonce section or due to
2753 linker script /DISCARD/, so we'll be discarding
2754 the relocs too. */
2755 }
2756 else if (p->count != 0)
2757 {
2758 srel = elf_section_data (p->sec)->sreloc;
351f65ca 2759 srel->size += p->count * bed->s->sizeof_rela;
4b819e1f
L
2760 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2761 && (info->flags & DF_TEXTREL) == 0)
b70321a2
L
2762 {
2763 info->flags |= DF_TEXTREL;
2764 if (info->warn_shared_textrel && info->shared)
2765 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'.\n"),
2766 p->sec->owner, p->sec);
b70321a2 2767 }
c434dee6 2768 }
70256ad8
AJ
2769 }
2770 }
c434dee6
AJ
2771
2772 local_got = elf_local_got_refcounts (ibfd);
2773 if (!local_got)
2774 continue;
2775
0ffa91dd 2776 symtab_hdr = &elf_symtab_hdr (ibfd);
c434dee6
AJ
2777 locsymcount = symtab_hdr->sh_info;
2778 end_local_got = local_got + locsymcount;
351f65ca
L
2779 local_tls_type = elf_x86_64_local_got_tls_type (ibfd);
2780 local_tlsdesc_gotent = elf_x86_64_local_tlsdesc_gotent (ibfd);
6de2ae4a
L
2781 s = htab->elf.sgot;
2782 srel = htab->elf.srelgot;
67a4f2b7
AO
2783 for (; local_got < end_local_got;
2784 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
70256ad8 2785 {
67a4f2b7 2786 *local_tlsdesc_gotent = (bfd_vma) -1;
c434dee6 2787 if (*local_got > 0)
70256ad8 2788 {
67a4f2b7
AO
2789 if (GOT_TLS_GDESC_P (*local_tls_type))
2790 {
6de2ae4a 2791 *local_tlsdesc_gotent = htab->elf.sgotplt->size
351f65ca 2792 - elf_x86_64_compute_jump_table_size (htab);
6de2ae4a 2793 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
67a4f2b7
AO
2794 *local_got = (bfd_vma) -2;
2795 }
2796 if (! GOT_TLS_GDESC_P (*local_tls_type)
2797 || GOT_TLS_GD_P (*local_tls_type))
2798 {
2799 *local_got = s->size;
2800 s->size += GOT_ENTRY_SIZE;
2801 if (GOT_TLS_GD_P (*local_tls_type))
2802 s->size += GOT_ENTRY_SIZE;
2803 }
bffbf940 2804 if (info->shared
67a4f2b7 2805 || GOT_TLS_GD_ANY_P (*local_tls_type)
bffbf940 2806 || *local_tls_type == GOT_TLS_IE)
67a4f2b7
AO
2807 {
2808 if (GOT_TLS_GDESC_P (*local_tls_type))
2809 {
6de2ae4a 2810 htab->elf.srelplt->size
351f65ca 2811 += bed->s->sizeof_rela;
67a4f2b7
AO
2812 htab->tlsdesc_plt = (bfd_vma) -1;
2813 }
2814 if (! GOT_TLS_GDESC_P (*local_tls_type)
2815 || GOT_TLS_GD_P (*local_tls_type))
351f65ca 2816 srel->size += bed->s->sizeof_rela;
67a4f2b7 2817 }
70256ad8
AJ
2818 }
2819 else
c434dee6
AJ
2820 *local_got = (bfd_vma) -1;
2821 }
2822 }
70256ad8 2823
bffbf940
JJ
2824 if (htab->tls_ld_got.refcount > 0)
2825 {
2826 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2827 relocs. */
6de2ae4a
L
2828 htab->tls_ld_got.offset = htab->elf.sgot->size;
2829 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
351f65ca 2830 htab->elf.srelgot->size += bed->s->sizeof_rela;
bffbf940
JJ
2831 }
2832 else
2833 htab->tls_ld_got.offset = -1;
2834
c434dee6
AJ
2835 /* Allocate global sym .plt and .got entries, and space for global
2836 sym dynamic relocs. */
351f65ca 2837 elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
eb4ff4d6 2838 info);
c434dee6 2839
c25bc9fc
L
2840 /* Allocate .plt and .got entries, and space for local symbols. */
2841 htab_traverse (htab->loc_hash_table,
351f65ca 2842 elf_x86_64_allocate_local_dynrelocs,
c25bc9fc
L
2843 info);
2844
67a4f2b7
AO
2845 /* For every jump slot reserved in the sgotplt, reloc_count is
2846 incremented. However, when we reserve space for TLS descriptors,
2847 it's not incremented, so in order to compute the space reserved
2848 for them, it suffices to multiply the reloc count by the jump
e1f98742
L
2849 slot size.
2850
2851 PR ld/13302: We start next_irelative_index at the end of .rela.plt
2852 so that R_X86_64_IRELATIVE entries come last. */
6de2ae4a 2853 if (htab->elf.srelplt)
e1f98742
L
2854 {
2855 htab->sgotplt_jump_table_size
2856 = elf_x86_64_compute_jump_table_size (htab);
2857 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2858 }
2859 else if (htab->elf.irelplt)
2860 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
67a4f2b7
AO
2861
2862 if (htab->tlsdesc_plt)
2863 {
2864 /* If we're not using lazy TLS relocations, don't generate the
2865 PLT and GOT entries they require. */
2866 if ((info->flags & DF_BIND_NOW))
2867 htab->tlsdesc_plt = 0;
2868 else
2869 {
6de2ae4a
L
2870 htab->tlsdesc_got = htab->elf.sgot->size;
2871 htab->elf.sgot->size += GOT_ENTRY_SIZE;
67a4f2b7
AO
2872 /* Reserve room for the initial entry.
2873 FIXME: we could probably do away with it in this case. */
6de2ae4a 2874 if (htab->elf.splt->size == 0)
eed180f8 2875 htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
6de2ae4a 2876 htab->tlsdesc_plt = htab->elf.splt->size;
eed180f8 2877 htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
67a4f2b7
AO
2878 }
2879 }
2880
a7b16ceb
L
2881 if (htab->elf.sgotplt)
2882 {
2883 /* Don't allocate .got.plt section if there are no GOT nor PLT
eed180f8 2884 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
9d4b2dba
L
2885 if ((htab->elf.hgot == NULL
2886 || !htab->elf.hgot->ref_regular_nonweak)
e28df02b
L
2887 && (htab->elf.sgotplt->size
2888 == get_elf_backend_data (output_bfd)->got_header_size)
a7b16ceb
L
2889 && (htab->elf.splt == NULL
2890 || htab->elf.splt->size == 0)
2891 && (htab->elf.sgot == NULL
2892 || htab->elf.sgot->size == 0)
2893 && (htab->elf.iplt == NULL
2894 || htab->elf.iplt->size == 0)
2895 && (htab->elf.igotplt == NULL
2896 || htab->elf.igotplt->size == 0))
2897 htab->elf.sgotplt->size = 0;
2898 }
2899
9a2a56cc
AM
2900 if (htab->plt_eh_frame != NULL
2901 && htab->elf.splt != NULL
2902 && htab->elf.splt->size != 0
2903 && !bfd_is_abs_section (htab->elf.splt->output_section)
2904 && _bfd_elf_eh_frame_present (info))
2905 {
2906 const struct elf_x86_64_backend_data *arch_data
2907 = (const struct elf_x86_64_backend_data *) bed->arch_data;
2908 htab->plt_eh_frame->size = arch_data->eh_frame_plt_size;
2909 }
2910
c434dee6
AJ
2911 /* We now have determined the sizes of the various dynamic sections.
2912 Allocate memory for them. */
b34976b6 2913 relocs = FALSE;
c434dee6
AJ
2914 for (s = dynobj->sections; s != NULL; s = s->next)
2915 {
2916 if ((s->flags & SEC_LINKER_CREATED) == 0)
2917 continue;
2918
6de2ae4a
L
2919 if (s == htab->elf.splt
2920 || s == htab->elf.sgot
2921 || s == htab->elf.sgotplt
2922 || s == htab->elf.iplt
2923 || s == htab->elf.igotplt
9a2a56cc 2924 || s == htab->plt_eh_frame
75ff4589 2925 || s == htab->sdynbss)
c434dee6
AJ
2926 {
2927 /* Strip this section if we don't need it; see the
2928 comment below. */
2929 }
0112cd26 2930 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
c434dee6 2931 {
6de2ae4a 2932 if (s->size != 0 && s != htab->elf.srelplt)
b34976b6 2933 relocs = TRUE;
c434dee6
AJ
2934
2935 /* We use the reloc_count field as a counter if we need
2936 to copy relocs into the output file. */
6de2ae4a 2937 if (s != htab->elf.srelplt)
67a4f2b7 2938 s->reloc_count = 0;
70256ad8 2939 }
c434dee6 2940 else
70256ad8
AJ
2941 {
2942 /* It's not one of our sections, so don't allocate space. */
2943 continue;
2944 }
2945
eea6121a 2946 if (s->size == 0)
70256ad8 2947 {
c434dee6
AJ
2948 /* If we don't need this section, strip it from the
2949 output file. This is mostly to handle .rela.bss and
2950 .rela.plt. We must create both sections in
2951 create_dynamic_sections, because they must be created
2952 before the linker maps input sections to output
2953 sections. The linker does that before
2954 adjust_dynamic_symbol is called, and it is that
2955 function which decides whether anything needs to go
2956 into these sections. */
2957
8423293d 2958 s->flags |= SEC_EXCLUDE;
70256ad8
AJ
2959 continue;
2960 }
2961
c456f082
AM
2962 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2963 continue;
2964
70256ad8
AJ
2965 /* Allocate memory for the section contents. We use bfd_zalloc
2966 here in case unused entries are not reclaimed before the
2967 section's contents are written out. This should not happen,
2968 but this way if it does, we get a R_X86_64_NONE reloc instead
2969 of garbage. */
eea6121a 2970 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c434dee6 2971 if (s->contents == NULL)
b34976b6 2972 return FALSE;
70256ad8
AJ
2973 }
2974
e41b3a13 2975 if (htab->plt_eh_frame != NULL
9a2a56cc
AM
2976 && htab->plt_eh_frame->contents != NULL)
2977 {
2978 const struct elf_x86_64_backend_data *arch_data
2979 = (const struct elf_x86_64_backend_data *) bed->arch_data;
2980
2981 memcpy (htab->plt_eh_frame->contents,
2982 arch_data->eh_frame_plt, htab->plt_eh_frame->size);
2983 bfd_put_32 (dynobj, htab->elf.splt->size,
2984 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
2985 }
e41b3a13 2986
c434dee6 2987 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
2988 {
2989 /* Add some entries to the .dynamic section. We fill in the
351f65ca 2990 values later, in elf_x86_64_finish_dynamic_sections, but we
70256ad8 2991 must add the entries now so that we get the correct size for
407443a3 2992 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8 2993 dynamic linker and used by the debugger. */
dc810e39 2994#define add_dynamic_entry(TAG, VAL) \
5a580b3a 2995 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 2996
36af4a4e 2997 if (info->executable)
70256ad8 2998 {
dc810e39 2999 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 3000 return FALSE;
70256ad8
AJ
3001 }
3002
6de2ae4a 3003 if (htab->elf.splt->size != 0)
70256ad8 3004 {
dc810e39
AM
3005 if (!add_dynamic_entry (DT_PLTGOT, 0)
3006 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3007 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3008 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 3009 return FALSE;
67a4f2b7
AO
3010
3011 if (htab->tlsdesc_plt
3012 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
3013 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
3014 return FALSE;
70256ad8
AJ
3015 }
3016
3017 if (relocs)
3018 {
dc810e39
AM
3019 if (!add_dynamic_entry (DT_RELA, 0)
3020 || !add_dynamic_entry (DT_RELASZ, 0)
351f65ca 3021 || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
b34976b6 3022 return FALSE;
70256ad8 3023
c434dee6
AJ
3024 /* If any dynamic relocs apply to a read-only section,
3025 then we need a DT_TEXTREL entry. */
3026 if ((info->flags & DF_TEXTREL) == 0)
eed180f8 3027 elf_link_hash_traverse (&htab->elf,
351f65ca 3028 elf_x86_64_readonly_dynrelocs,
eb4ff4d6 3029 info);
c434dee6
AJ
3030
3031 if ((info->flags & DF_TEXTREL) != 0)
3032 {
3033 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 3034 return FALSE;
c434dee6 3035 }
70256ad8
AJ
3036 }
3037 }
dc810e39 3038#undef add_dynamic_entry
70256ad8 3039
b34976b6 3040 return TRUE;
70256ad8
AJ
3041}
3042
67a4f2b7 3043static bfd_boolean
351f65ca
L
3044elf_x86_64_always_size_sections (bfd *output_bfd,
3045 struct bfd_link_info *info)
67a4f2b7
AO
3046{
3047 asection *tls_sec = elf_hash_table (info)->tls_sec;
3048
3049 if (tls_sec)
3050 {
3051 struct elf_link_hash_entry *tlsbase;
3052
3053 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3054 "_TLS_MODULE_BASE_",
3055 FALSE, FALSE, FALSE);
3056
3057 if (tlsbase && tlsbase->type == STT_TLS)
3058 {
351f65ca 3059 struct elf_x86_64_link_hash_table *htab;
67a4f2b7
AO
3060 struct bfd_link_hash_entry *bh = NULL;
3061 const struct elf_backend_data *bed
3062 = get_elf_backend_data (output_bfd);
3063
351f65ca 3064 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3065 if (htab == NULL)
3066 return FALSE;
3067
67a4f2b7
AO
3068 if (!(_bfd_generic_link_add_one_symbol
3069 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3070 tls_sec, 0, NULL, FALSE,
3071 bed->collect, &bh)))
3072 return FALSE;
9f03412a 3073
4dfe6ac6 3074 htab->tls_module_base = bh;
9f03412a 3075
67a4f2b7
AO
3076 tlsbase = (struct elf_link_hash_entry *)bh;
3077 tlsbase->def_regular = 1;
3078 tlsbase->other = STV_HIDDEN;
3079 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
3080 }
3081 }
3082
3083 return TRUE;
3084}
3085
9f03412a
AO
3086/* _TLS_MODULE_BASE_ needs to be treated especially when linking
3087 executables. Rather than setting it to the beginning of the TLS
3088 section, we have to set it to the end. This function may be called
3089 multiple times, it is idempotent. */
3090
3091static void
351f65ca 3092elf_x86_64_set_tls_module_base (struct bfd_link_info *info)
9f03412a 3093{
351f65ca 3094 struct elf_x86_64_link_hash_table *htab;
9f03412a
AO
3095 struct bfd_link_hash_entry *base;
3096
3097 if (!info->executable)
3098 return;
3099
351f65ca 3100 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3101 if (htab == NULL)
3102 return;
9f03412a 3103
4dfe6ac6
NC
3104 base = htab->tls_module_base;
3105 if (base == NULL)
9f03412a
AO
3106 return;
3107
4dfe6ac6 3108 base->u.def.value = htab->elf.tls_size;
9f03412a
AO
3109}
3110
bffbf940
JJ
3111/* Return the base VMA address which should be subtracted from real addresses
3112 when resolving @dtpoff relocation.
3113 This is PT_TLS segment p_vaddr. */
3114
3115static bfd_vma
351f65ca 3116elf_x86_64_dtpoff_base (struct bfd_link_info *info)
bffbf940 3117{
e1918d23
AM
3118 /* If tls_sec is NULL, we should have signalled an error already. */
3119 if (elf_hash_table (info)->tls_sec == NULL)
bffbf940 3120 return 0;
e1918d23 3121 return elf_hash_table (info)->tls_sec->vma;
bffbf940
JJ
3122}
3123
3124/* Return the relocation value for @tpoff relocation
3125 if STT_TLS virtual address is ADDRESS. */
3126
3127static bfd_vma
351f65ca 3128elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 3129{
e1918d23 3130 struct elf_link_hash_table *htab = elf_hash_table (info);
7dc98aea
RO
3131 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3132 bfd_vma static_tls_size;
bffbf940
JJ
3133
3134 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 3135 if (htab->tls_sec == NULL)
bffbf940 3136 return 0;
7dc98aea
RO
3137
3138 /* Consider special static TLS alignment requirements. */
3139 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3140 return address - static_tls_size - htab->tls_sec->vma;
bffbf940
JJ
3141}
3142
90f487df
L
3143/* Is the instruction before OFFSET in CONTENTS a 32bit relative
3144 branch? */
3145
3146static bfd_boolean
3147is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
3148{
3149 /* Opcode Instruction
3150 0xe8 call
3151 0xe9 jump
3152 0x0f 0x8x conditional jump */
3153 return ((offset > 0
3154 && (contents [offset - 1] == 0xe8
3155 || contents [offset - 1] == 0xe9))
3156 || (offset > 1
3157 && contents [offset - 2] == 0x0f
3158 && (contents [offset - 1] & 0xf0) == 0x80));
3159}
3160
8d88c4ca
NC
3161/* Relocate an x86_64 ELF section. */
3162
b34976b6 3163static bfd_boolean
351f65ca
L
3164elf_x86_64_relocate_section (bfd *output_bfd,
3165 struct bfd_link_info *info,
3166 bfd *input_bfd,
3167 asection *input_section,
3168 bfd_byte *contents,
3169 Elf_Internal_Rela *relocs,
3170 Elf_Internal_Sym *local_syms,
3171 asection **local_sections)
8d88c4ca 3172{
351f65ca 3173 struct elf_x86_64_link_hash_table *htab;
8d88c4ca
NC
3174 Elf_Internal_Shdr *symtab_hdr;
3175 struct elf_link_hash_entry **sym_hashes;
3176 bfd_vma *local_got_offsets;
67a4f2b7 3177 bfd_vma *local_tlsdesc_gotents;
c434dee6 3178 Elf_Internal_Rela *rel;
8d88c4ca 3179 Elf_Internal_Rela *relend;
eed180f8 3180 const unsigned int plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
8d88c4ca 3181
0ffa91dd
NC
3182 BFD_ASSERT (is_x86_64_elf (input_bfd));
3183
351f65ca 3184 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
3185 if (htab == NULL)
3186 return FALSE;
0ffa91dd 3187 symtab_hdr = &elf_symtab_hdr (input_bfd);
8d88c4ca
NC
3188 sym_hashes = elf_sym_hashes (input_bfd);
3189 local_got_offsets = elf_local_got_offsets (input_bfd);
351f65ca 3190 local_tlsdesc_gotents = elf_x86_64_local_tlsdesc_gotent (input_bfd);
8d88c4ca 3191
351f65ca 3192 elf_x86_64_set_tls_module_base (info);
9f03412a 3193
c434dee6 3194 rel = relocs;
8d88c4ca 3195 relend = relocs + input_section->reloc_count;
c434dee6 3196 for (; rel < relend; rel++)
8d88c4ca 3197 {
bffbf940 3198 unsigned int r_type;
8d88c4ca
NC
3199 reloc_howto_type *howto;
3200 unsigned long r_symndx;
3201 struct elf_link_hash_entry *h;
3202 Elf_Internal_Sym *sym;
3203 asection *sec;
67a4f2b7 3204 bfd_vma off, offplt;
8d88c4ca 3205 bfd_vma relocation;
b34976b6 3206 bfd_boolean unresolved_reloc;
8d88c4ca 3207 bfd_reloc_status_type r;
bffbf940 3208 int tls_type;
cbe950e9 3209 asection *base_got;
1788fc08 3210 bfd_vma st_size;
8d88c4ca 3211
351f65ca 3212 r_type = ELF32_R_TYPE (rel->r_info);
fe4770f4
AJ
3213 if (r_type == (int) R_X86_64_GNU_VTINHERIT
3214 || r_type == (int) R_X86_64_GNU_VTENTRY)
3215 continue;
8d88c4ca 3216
9911c0fc 3217 if (r_type >= (int) R_X86_64_standard)
8da6118f 3218 {
9911c0fc
L
3219 (*_bfd_error_handler)
3220 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3221 input_bfd, input_section, r_type);
8da6118f 3222 bfd_set_error (bfd_error_bad_value);
b34976b6 3223 return FALSE;
8da6118f 3224 }
8d88c4ca 3225
d7921315 3226 if (r_type != (int) R_X86_64_32
eed180f8 3227 || ABI_64_P (output_bfd))
d7921315
L
3228 howto = x86_64_elf_howto_table + r_type;
3229 else
3230 howto = (x86_64_elf_howto_table
3231 + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
351f65ca 3232 r_symndx = htab->r_sym (rel->r_info);
8d88c4ca
NC
3233 h = NULL;
3234 sym = NULL;
3235 sec = NULL;
b34976b6 3236 unresolved_reloc = FALSE;
8d88c4ca 3237 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
3238 {
3239 sym = local_syms + r_symndx;
3240 sec = local_sections[r_symndx];
c434dee6 3241
c25bc9fc
L
3242 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
3243 &sec, rel);
1788fc08 3244 st_size = sym->st_size;
c25bc9fc
L
3245
3246 /* Relocate against local STT_GNU_IFUNC symbol. */
1f85278f 3247 if (!info->relocatable
351f65ca 3248 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
c25bc9fc 3249 {
351f65ca
L
3250 h = elf_x86_64_get_local_sym_hash (htab, input_bfd,
3251 rel, FALSE);
c25bc9fc
L
3252 if (h == NULL)
3253 abort ();
3254
eed180f8 3255 /* Set STT_GNU_IFUNC symbol value. */
c25bc9fc
L
3256 h->root.u.def.value = sym->st_value;
3257 h->root.u.def.section = sec;
3258 }
8da6118f 3259 }
8d88c4ca 3260 else
8da6118f 3261 {
c9736ba0 3262 bfd_boolean warned ATTRIBUTE_UNUSED;
c434dee6 3263
b2a8e766
AM
3264 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3265 r_symndx, symtab_hdr, sym_hashes,
3266 h, sec, relocation,
3267 unresolved_reloc, warned);
1788fc08 3268 st_size = h->size;
8da6118f 3269 }
ab96bf03 3270
dbaa2011 3271 if (sec != NULL && discarded_section (sec))
0672748a 3272 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 3273 rel, 1, relend, howto, 0, contents);
ab96bf03
AM
3274
3275 if (info->relocatable)
3276 continue;
3277
1788fc08 3278 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
64d25c44 3279 {
1788fc08
L
3280 if (r_type == R_X86_64_64)
3281 {
3282 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
3283 zero-extend it to 64bit if addend is zero. */
3284 r_type = R_X86_64_32;
3285 memset (contents + rel->r_offset + 4, 0, 4);
3286 }
3287 else if (r_type == R_X86_64_SIZE64)
3288 {
3289 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
3290 zero-extend it to 64bit if addend is zero. */
3291 r_type = R_X86_64_SIZE32;
3292 memset (contents + rel->r_offset + 4, 0, 4);
3293 }
64d25c44
L
3294 }
3295
cbe950e9
L
3296 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3297 it here if it is defined in a non-shared object. */
3298 if (h != NULL
3299 && h->type == STT_GNU_IFUNC
3300 && h->def_regular)
3301 {
3302 asection *plt;
3303 bfd_vma plt_index;
4c544807 3304 const char *name;
cbe950e9
L
3305
3306 if ((input_section->flags & SEC_ALLOC) == 0
3307 || h->plt.offset == (bfd_vma) -1)
3308 abort ();
3309
3310 /* STT_GNU_IFUNC symbol must go through PLT. */
6de2ae4a 3311 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
cbe950e9
L
3312 relocation = (plt->output_section->vma
3313 + plt->output_offset + h->plt.offset);
3314
3315 switch (r_type)
3316 {
3317 default:
4c544807
L
3318 if (h->root.root.string)
3319 name = h->root.root.string;
3320 else
3321 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3322 NULL);
cbe950e9
L
3323 (*_bfd_error_handler)
3324 (_("%B: relocation %s against STT_GNU_IFUNC "
3325 "symbol `%s' isn't handled by %s"), input_bfd,
3326 x86_64_elf_howto_table[r_type].name,
4c544807 3327 name, __FUNCTION__);
cbe950e9
L
3328 bfd_set_error (bfd_error_bad_value);
3329 return FALSE;
3330
3331 case R_X86_64_32S:
710ab287 3332 if (info->shared)
cbe950e9 3333 abort ();
710ab287
L
3334 goto do_relocation;
3335
248775ba
L
3336 case R_X86_64_32:
3337 if (ABI_64_P (output_bfd))
3338 goto do_relocation;
17672001 3339 /* FALLTHROUGH */
eed180f8 3340 case R_X86_64_64:
710ab287
L
3341 if (rel->r_addend != 0)
3342 {
4c544807
L
3343 if (h->root.root.string)
3344 name = h->root.root.string;
3345 else
3346 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3347 sym, NULL);
710ab287
L
3348 (*_bfd_error_handler)
3349 (_("%B: relocation %s against STT_GNU_IFUNC "
3350 "symbol `%s' has non-zero addend: %d"),
3351 input_bfd, x86_64_elf_howto_table[r_type].name,
4c544807 3352 name, rel->r_addend);
710ab287
L
3353 bfd_set_error (bfd_error_bad_value);
3354 return FALSE;
3355 }
3356
3357 /* Generate dynamic relcoation only when there is a
c293fa49 3358 non-GOT reference in a shared object. */
710ab287
L
3359 if (info->shared && h->non_got_ref)
3360 {
3361 Elf_Internal_Rela outrel;
710ab287
L
3362 asection *sreloc;
3363
c25bc9fc
L
3364 /* Need a dynamic relocation to get the real function
3365 address. */
710ab287
L
3366 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3367 info,
3368 input_section,
3369 rel->r_offset);
3370 if (outrel.r_offset == (bfd_vma) -1
3371 || outrel.r_offset == (bfd_vma) -2)
3372 abort ();
3373
3374 outrel.r_offset += (input_section->output_section->vma
3375 + input_section->output_offset);
3376
3377 if (h->dynindx == -1
44c4ea11
L
3378 || h->forced_local
3379 || info->executable)
710ab287
L
3380 {
3381 /* This symbol is resolved locally. */
56b8aada
L
3382 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
3383 outrel.r_addend = (h->root.u.def.value
3384 + h->root.u.def.section->output_section->vma
3385 + h->root.u.def.section->output_offset);
710ab287
L
3386 }
3387 else
3388 {
351f65ca 3389 outrel.r_info = htab->r_info (h->dynindx, r_type);
710ab287
L
3390 outrel.r_addend = 0;
3391 }
3392
6de2ae4a 3393 sreloc = htab->elf.irelifunc;
351f65ca 3394 elf_append_rela (output_bfd, sreloc, &outrel);
710ab287
L
3395
3396 /* If this reloc is against an external symbol, we
3397 do not want to fiddle with the addend. Otherwise,
3398 we need to include the symbol value so that it
3399 becomes an addend for the dynamic reloc. For an
3400 internal symbol, we have updated addend. */
56b8aada 3401 continue;
710ab287 3402 }
17672001 3403 /* FALLTHROUGH */
cbe950e9
L
3404 case R_X86_64_PC32:
3405 case R_X86_64_PC64:
3406 case R_X86_64_PLT32:
3407 goto do_relocation;
3408
3409 case R_X86_64_GOTPCREL:
3410 case R_X86_64_GOTPCREL64:
6de2ae4a 3411 base_got = htab->elf.sgot;
cbe950e9
L
3412 off = h->got.offset;
3413
7afd84dc 3414 if (base_got == NULL)
cbe950e9
L
3415 abort ();
3416
7afd84dc 3417 if (off == (bfd_vma) -1)
cbe950e9 3418 {
7afd84dc
L
3419 /* We can't use h->got.offset here to save state, or
3420 even just remember the offset, as finish_dynamic_symbol
3421 would use that as offset into .got. */
cbe950e9 3422
6de2ae4a 3423 if (htab->elf.splt != NULL)
7afd84dc 3424 {
eed180f8 3425 plt_index = h->plt.offset / plt_entry_size - 1;
7afd84dc 3426 off = (plt_index + 3) * GOT_ENTRY_SIZE;
6de2ae4a 3427 base_got = htab->elf.sgotplt;
7afd84dc 3428 }
cbe950e9
L
3429 else
3430 {
eed180f8 3431 plt_index = h->plt.offset / plt_entry_size;
7afd84dc 3432 off = plt_index * GOT_ENTRY_SIZE;
6de2ae4a 3433 base_got = htab->elf.igotplt;
7afd84dc
L
3434 }
3435
3436 if (h->dynindx == -1
3437 || h->forced_local
3438 || info->symbolic)
3439 {
eed180f8 3440 /* This references the local defitionion. We must
7afd84dc 3441 initialize this entry in the global offset table.
eed180f8 3442 Since the offset must always be a multiple of 8,
7afd84dc
L
3443 we use the least significant bit to record
3444 whether we have initialized it already.
3445
3446 When doing a dynamic link, we create a .rela.got
3447 relocation entry to initialize the value. This
3448 is done in the finish_dynamic_symbol routine. */
3449 if ((off & 1) != 0)
3450 off &= ~1;
3451 else
3452 {
3453 bfd_put_64 (output_bfd, relocation,
3454 base_got->contents + off);
3455 /* Note that this is harmless for the GOTPLT64
3456 case, as -1 | 1 still is -1. */
3457 h->got.offset |= 1;
3458 }
cbe950e9
L
3459 }
3460 }
3461
3462 relocation = (base_got->output_section->vma
3463 + base_got->output_offset + off);
3464
cbe950e9
L
3465 goto do_relocation;
3466 }
3467 }
3468
70256ad8
AJ
3469 /* When generating a shared object, the relocations handled here are
3470 copied into the output file to be resolved at run time. */
3471 switch (r_type)
3472 {
3473 case R_X86_64_GOT32:
7b81dfbb 3474 case R_X86_64_GOT64:
70256ad8
AJ
3475 /* Relocation is to the entry for this symbol in the global
3476 offset table. */
70256ad8 3477 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
3478 case R_X86_64_GOTPCREL64:
3479 /* Use global offset table entry as symbol value. */
3480 case R_X86_64_GOTPLT64:
3481 /* This is the same as GOT64 for relocation purposes, but
3482 indicates the existence of a PLT entry. The difficulty is,
3483 that we must calculate the GOT slot offset from the PLT
3484 offset, if this symbol got a PLT entry (it was global).
3485 Additionally if it's computed from the PLT entry, then that
3486 GOT offset is relative to .got.plt, not to .got. */
6de2ae4a 3487 base_got = htab->elf.sgot;
7b81dfbb 3488
6de2ae4a 3489 if (htab->elf.sgot == NULL)
c434dee6 3490 abort ();
053579d7 3491
51e0a107 3492 if (h != NULL)
70256ad8 3493 {
b34976b6 3494 bfd_boolean dyn;
c434dee6
AJ
3495
3496 off = h->got.offset;
7b81dfbb 3497 if (h->needs_plt
eed180f8 3498 && h->plt.offset != (bfd_vma)-1
7b81dfbb
AJ
3499 && off == (bfd_vma)-1)
3500 {
3501 /* We can't use h->got.offset here to save
3502 state, or even just remember the offset, as
3503 finish_dynamic_symbol would use that as offset into
3504 .got. */
eed180f8 3505 bfd_vma plt_index = h->plt.offset / plt_entry_size - 1;
7b81dfbb 3506 off = (plt_index + 3) * GOT_ENTRY_SIZE;
6de2ae4a 3507 base_got = htab->elf.sgotplt;
7b81dfbb
AJ
3508 }
3509
c434dee6 3510 dyn = htab->elf.dynamic_sections_created;
51e0a107 3511
27482721 3512 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
51e0a107 3513 || (info->shared
27482721 3514 && SYMBOL_REFERENCES_LOCAL (info, h))
4bc6e03a
AJ
3515 || (ELF_ST_VISIBILITY (h->other)
3516 && h->root.type == bfd_link_hash_undefweak))
51e0a107
JH
3517 {
3518 /* This is actually a static link, or it is a -Bsymbolic
3519 link and the symbol is defined locally, or the symbol
407443a3 3520 was forced to be local because of a version file. We
51e0a107
JH
3521 must initialize this entry in the global offset table.
3522 Since the offset must always be a multiple of 8, we
3523 use the least significant bit to record whether we
3524 have initialized it already.
3525
3526 When doing a dynamic link, we create a .rela.got
407443a3
AJ
3527 relocation entry to initialize the value. This is
3528 done in the finish_dynamic_symbol routine. */
51e0a107
JH
3529 if ((off & 1) != 0)
3530 off &= ~1;
3531 else
3532 {
3533 bfd_put_64 (output_bfd, relocation,
7b81dfbb
AJ
3534 base_got->contents + off);
3535 /* Note that this is harmless for the GOTPLT64 case,
eed180f8 3536 as -1 | 1 still is -1. */
51e0a107
JH
3537 h->got.offset |= 1;
3538 }
3539 }
053579d7 3540 else
b34976b6 3541 unresolved_reloc = FALSE;
70256ad8 3542 }
51e0a107
JH
3543 else
3544 {
c434dee6
AJ
3545 if (local_got_offsets == NULL)
3546 abort ();
51e0a107
JH
3547
3548 off = local_got_offsets[r_symndx];
3549
3550 /* The offset must always be a multiple of 8. We use
407443a3
AJ
3551 the least significant bit to record whether we have
3552 already generated the necessary reloc. */
51e0a107
JH
3553 if ((off & 1) != 0)
3554 off &= ~1;
3555 else
3556 {
c434dee6 3557 bfd_put_64 (output_bfd, relocation,
7b81dfbb 3558 base_got->contents + off);
51e0a107
JH
3559
3560 if (info->shared)
3561 {
947216bf 3562 asection *s;
51e0a107 3563 Elf_Internal_Rela outrel;
70256ad8 3564
51e0a107
JH
3565 /* We need to generate a R_X86_64_RELATIVE reloc
3566 for the dynamic linker. */
6de2ae4a 3567 s = htab->elf.srelgot;
947216bf 3568 if (s == NULL)
c434dee6 3569 abort ();
51e0a107 3570
7b81dfbb
AJ
3571 outrel.r_offset = (base_got->output_section->vma
3572 + base_got->output_offset
51e0a107 3573 + off);
351f65ca 3574 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
51e0a107 3575 outrel.r_addend = relocation;
351f65ca 3576 elf_append_rela (output_bfd, s, &outrel);
51e0a107
JH
3577 }
3578
3579 local_got_offsets[r_symndx] |= 1;
3580 }
51e0a107 3581 }
6a2bda3f 3582
c434dee6
AJ
3583 if (off >= (bfd_vma) -2)
3584 abort ();
3585
7b81dfbb
AJ
3586 relocation = base_got->output_section->vma
3587 + base_got->output_offset + off;
3588 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
6de2ae4a
L
3589 relocation -= htab->elf.sgotplt->output_section->vma
3590 - htab->elf.sgotplt->output_offset;
c434dee6 3591
70256ad8
AJ
3592 break;
3593
d6ab8113
JB
3594 case R_X86_64_GOTOFF64:
3595 /* Relocation is relative to the start of the global offset
3596 table. */
3597
3598 /* Check to make sure it isn't a protected function symbol
3599 for shared library since it may not be local when used
3600 as function address. */
bdb892b9 3601 if (!info->executable
d6ab8113 3602 && h
bdb892b9 3603 && !SYMBOLIC_BIND (info, h)
d6ab8113
JB
3604 && h->def_regular
3605 && h->type == STT_FUNC
3606 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3607 {
3608 (*_bfd_error_handler)
3609 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3610 input_bfd, h->root.root.string);
3611 bfd_set_error (bfd_error_bad_value);
3612 return FALSE;
3613 }
3614
3615 /* Note that sgot is not involved in this
3616 calculation. We always want the start of .got.plt. If we
3617 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3618 permitted by the ABI, we might have to change this
3619 calculation. */
6de2ae4a
L
3620 relocation -= htab->elf.sgotplt->output_section->vma
3621 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
3622 break;
3623
3624 case R_X86_64_GOTPC32:
7b81dfbb 3625 case R_X86_64_GOTPC64:
d6ab8113 3626 /* Use global offset table as symbol value. */
6de2ae4a
L
3627 relocation = htab->elf.sgotplt->output_section->vma
3628 + htab->elf.sgotplt->output_offset;
d6ab8113
JB
3629 unresolved_reloc = FALSE;
3630 break;
7b81dfbb
AJ
3631
3632 case R_X86_64_PLTOFF64:
3633 /* Relocation is PLT entry relative to GOT. For local
3634 symbols it's the symbol itself relative to GOT. */
eed180f8 3635 if (h != NULL
7b81dfbb
AJ
3636 /* See PLT32 handling. */
3637 && h->plt.offset != (bfd_vma) -1
6de2ae4a 3638 && htab->elf.splt != NULL)
7b81dfbb 3639 {
6de2ae4a
L
3640 relocation = (htab->elf.splt->output_section->vma
3641 + htab->elf.splt->output_offset
7b81dfbb
AJ
3642 + h->plt.offset);
3643 unresolved_reloc = FALSE;
3644 }
3645
6de2ae4a
L
3646 relocation -= htab->elf.sgotplt->output_section->vma
3647 + htab->elf.sgotplt->output_offset;
7b81dfbb 3648 break;
d6ab8113 3649
70256ad8
AJ
3650 case R_X86_64_PLT32:
3651 /* Relocation is to the entry for this symbol in the
3652 procedure linkage table. */
3653
3654 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 3655 without using the procedure linkage table. */
70256ad8
AJ
3656 if (h == NULL)
3657 break;
3658
c434dee6 3659 if (h->plt.offset == (bfd_vma) -1
6de2ae4a 3660 || htab->elf.splt == NULL)
70256ad8
AJ
3661 {
3662 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
3663 happens when statically linking PIC code, or when
3664 using -Bsymbolic. */
70256ad8
AJ
3665 break;
3666 }
3667
6de2ae4a
L
3668 relocation = (htab->elf.splt->output_section->vma
3669 + htab->elf.splt->output_offset
70256ad8 3670 + h->plt.offset);
b34976b6 3671 unresolved_reloc = FALSE;
70256ad8
AJ
3672 break;
3673
1788fc08
L
3674 case R_X86_64_SIZE32:
3675 case R_X86_64_SIZE64:
1788fc08
L
3676 /* Set to symbol size. */
3677 relocation = st_size;
3678 goto direct;
3679
fd8ab9e5
AJ
3680 case R_X86_64_PC8:
3681 case R_X86_64_PC16:
3682 case R_X86_64_PC32:
6610a52d 3683 if (info->shared
ba3bee0b 3684 && (input_section->flags & SEC_ALLOC) != 0
90f487df 3685 && (input_section->flags & SEC_READONLY) != 0
41bed6dd 3686 && h != NULL)
6610a52d 3687 {
41bed6dd
L
3688 bfd_boolean fail = FALSE;
3689 bfd_boolean branch
3690 = (r_type == R_X86_64_PC32
3691 && is_32bit_relative_branch (contents, rel->r_offset));
3692
3693 if (SYMBOL_REFERENCES_LOCAL (info, h))
3694 {
3695 /* Symbol is referenced locally. Make sure it is
3696 defined locally or for a branch. */
3697 fail = !h->def_regular && !branch;
3698 }
90f487df 3699 else
41bed6dd
L
3700 {
3701 /* Symbol isn't referenced locally. We only allow
3702 branch to symbol with non-default visibility. */
3703 fail = (!branch
3704 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3705 }
3706
3707 if (fail)
3708 {
3709 const char *fmt;
3710 const char *v;
3711 const char *pic = "";
3712
3713 switch (ELF_ST_VISIBILITY (h->other))
3714 {
3715 case STV_HIDDEN:
3716 v = _("hidden symbol");
3717 break;
3718 case STV_INTERNAL:
3719 v = _("internal symbol");
3720 break;
3721 case STV_PROTECTED:
3722 v = _("protected symbol");
3723 break;
3724 default:
3725 v = _("symbol");
3726 pic = _("; recompile with -fPIC");
3727 break;
3728 }
3729
3730 if (h->def_regular)
3731 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3732 else
3733 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3734
3735 (*_bfd_error_handler) (fmt, input_bfd,
3736 x86_64_elf_howto_table[r_type].name,
3737 v, h->root.root.string, pic);
3738 bfd_set_error (bfd_error_bad_value);
3739 return FALSE;
3740 }
6610a52d
L
3741 }
3742 /* Fall through. */
3743
70256ad8
AJ
3744 case R_X86_64_8:
3745 case R_X86_64_16:
3746 case R_X86_64_32:
d6ab8113 3747 case R_X86_64_PC64:
6b3db546 3748 case R_X86_64_64:
80643fbc 3749 /* FIXME: The ABI says the linker should make sure the value is
407443a3 3750 the same when it's zeroextended to 64 bit. */
c434dee6 3751
1788fc08 3752direct:
b1e24c02 3753 if ((input_section->flags & SEC_ALLOC) == 0)
c434dee6
AJ
3754 break;
3755
3756 if ((info->shared
4bc6e03a
AJ
3757 && (h == NULL
3758 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3759 || h->root.type != bfd_link_hash_undefweak)
1788fc08
L
3760 && ((! IS_X86_64_PCREL_TYPE (r_type)
3761 && r_type != R_X86_64_SIZE32
3762 && r_type != R_X86_64_SIZE64)
d8045f23 3763 || ! SYMBOL_CALLS_LOCAL (info, h)))
d40d037c
AJ
3764 || (ELIMINATE_COPY_RELOCS
3765 && !info->shared
c434dee6
AJ
3766 && h != NULL
3767 && h->dynindx != -1
f5385ebf
AM
3768 && !h->non_got_ref
3769 && ((h->def_dynamic
3770 && !h->def_regular)
c434dee6 3771 || h->root.type == bfd_link_hash_undefweak
0f88be7a 3772 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
3773 {
3774 Elf_Internal_Rela outrel;
b34976b6 3775 bfd_boolean skip, relocate;
c434dee6 3776 asection *sreloc;
70256ad8
AJ
3777
3778 /* When generating a shared object, these relocations
3779 are copied into the output file to be resolved at run
407443a3 3780 time. */
b34976b6
AM
3781 skip = FALSE;
3782 relocate = FALSE;
70256ad8 3783
c629eae0
JJ
3784 outrel.r_offset =
3785 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 3786 rel->r_offset);
c629eae0 3787 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 3788 skip = TRUE;
0fb19cbc 3789 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 3790 skip = TRUE, relocate = TRUE;
70256ad8
AJ
3791
3792 outrel.r_offset += (input_section->output_section->vma
3793 + input_section->output_offset);
3794
3795 if (skip)
0bb2d96a 3796 memset (&outrel, 0, sizeof outrel);
c434dee6 3797
fd8ab9e5
AJ
3798 /* h->dynindx may be -1 if this symbol was marked to
3799 become local. */
3800 else if (h != NULL
c434dee6 3801 && h->dynindx != -1
d8045f23
NC
3802 && (IS_X86_64_PCREL_TYPE (r_type)
3803 || ! info->shared
3804 || ! SYMBOLIC_BIND (info, h)
3805 || ! h->def_regular))
70256ad8 3806 {
351f65ca 3807 outrel.r_info = htab->r_info (h->dynindx, r_type);
c434dee6 3808 outrel.r_addend = rel->r_addend;
70256ad8
AJ
3809 }
3810 else
3811 {
c434dee6 3812 /* This symbol is local, or marked to become local. */
248775ba 3813 if (r_type == htab->pointer_r_type)
607c0e09 3814 {
b34976b6 3815 relocate = TRUE;
351f65ca 3816 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
607c0e09
AS
3817 outrel.r_addend = relocation + rel->r_addend;
3818 }
64d25c44
L
3819 else if (r_type == R_X86_64_64
3820 && !ABI_64_P (output_bfd))
3821 {
3822 relocate = TRUE;
3823 outrel.r_info = htab->r_info (0,
3824 R_X86_64_RELATIVE64);
3825 outrel.r_addend = relocation + rel->r_addend;
8cf0d2dd
L
3826 /* Check addend overflow. */
3827 if ((outrel.r_addend & 0x80000000)
3828 != (rel->r_addend & 0x80000000))
3829 {
3830 const char *name;
268a8d3a 3831 int addend = rel->r_addend;
8cf0d2dd
L
3832 if (h && h->root.root.string)
3833 name = h->root.root.string;
3834 else
3835 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3836 sym, NULL);
6f2c9068
L
3837 if (addend < 0)
3838 (*_bfd_error_handler)
268a8d3a 3839 (_("%B: addend -0x%x in relocation %s against "
6f2c9068
L
3840 "symbol `%s' at 0x%lx in section `%A' is "
3841 "out of range"),
3842 input_bfd, input_section, addend,
3843 x86_64_elf_howto_table[r_type].name,
3844 name, (unsigned long) rel->r_offset);
3845 else
3846 (*_bfd_error_handler)
268a8d3a 3847 (_("%B: addend 0x%x in relocation %s against "
6f2c9068
L
3848 "symbol `%s' at 0x%lx in section `%A' is "
3849 "out of range"),
3850 input_bfd, input_section, addend,
3851 x86_64_elf_howto_table[r_type].name,
3852 name, (unsigned long) rel->r_offset);
8cf0d2dd
L
3853 bfd_set_error (bfd_error_bad_value);
3854 return FALSE;
3855 }
64d25c44 3856 }
607c0e09
AS
3857 else
3858 {
3859 long sindx;
3860
8517fae7 3861 if (bfd_is_abs_section (sec))
607c0e09
AS
3862 sindx = 0;
3863 else if (sec == NULL || sec->owner == NULL)
3864 {
3865 bfd_set_error (bfd_error_bad_value);
b34976b6 3866 return FALSE;
607c0e09
AS
3867 }
3868 else
3869 {
3870 asection *osec;
3871
74541ad4
AM
3872 /* We are turning this relocation into one
3873 against a section symbol. It would be
3874 proper to subtract the symbol's value,
3875 osec->vma, from the emitted reloc addend,
3876 but ld.so expects buggy relocs. */
607c0e09
AS
3877 osec = sec->output_section;
3878 sindx = elf_section_data (osec)->dynindx;
74541ad4
AM
3879 if (sindx == 0)
3880 {
3881 asection *oi = htab->elf.text_index_section;
3882 sindx = elf_section_data (oi)->dynindx;
3883 }
3884 BFD_ASSERT (sindx != 0);
607c0e09
AS
3885 }
3886
351f65ca 3887 outrel.r_info = htab->r_info (sindx, r_type);
607c0e09
AS
3888 outrel.r_addend = relocation + rel->r_addend;
3889 }
70256ad8
AJ
3890 }
3891
cbe950e9 3892 sreloc = elf_section_data (input_section)->sreloc;
d8045f23 3893
62d78908
L
3894 if (sreloc == NULL || sreloc->contents == NULL)
3895 {
3896 r = bfd_reloc_notsupported;
3897 goto check_relocation_error;
3898 }
c434dee6 3899
351f65ca 3900 elf_append_rela (output_bfd, sreloc, &outrel);
70256ad8
AJ
3901
3902 /* If this reloc is against an external symbol, we do
3903 not want to fiddle with the addend. Otherwise, we
3904 need to include the symbol value so that it becomes
3905 an addend for the dynamic reloc. */
0f88be7a 3906 if (! relocate)
70256ad8
AJ
3907 continue;
3908 }
3909
3910 break;
3911
bffbf940 3912 case R_X86_64_TLSGD:
67a4f2b7
AO
3913 case R_X86_64_GOTPC32_TLSDESC:
3914 case R_X86_64_TLSDESC_CALL:
bffbf940 3915 case R_X86_64_GOTTPOFF:
bffbf940
JJ
3916 tls_type = GOT_UNKNOWN;
3917 if (h == NULL && local_got_offsets)
351f65ca 3918 tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
bffbf940 3919 else if (h != NULL)
351f65ca 3920 tls_type = elf_x86_64_hash_entry (h)->tls_type;
142411ca 3921
351f65ca
L
3922 if (! elf_x86_64_tls_transition (info, input_bfd,
3923 input_section, contents,
3924 symtab_hdr, sym_hashes,
3925 &r_type, tls_type, rel,
3926 relend, h, r_symndx))
534a31f6 3927 return FALSE;
bffbf940
JJ
3928
3929 if (r_type == R_X86_64_TPOFF32)
3930 {
142411ca
L
3931 bfd_vma roff = rel->r_offset;
3932
bffbf940 3933 BFD_ASSERT (! unresolved_reloc);
142411ca 3934
351f65ca 3935 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
bffbf940 3936 {
52bc799a 3937 /* GD->LE transition. For 64bit, change
abcf1d52 3938 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
a3fadc9a 3939 .word 0x6666; rex64; call __tls_get_addr
52bc799a 3940 into:
bffbf940 3941 movq %fs:0, %rax
52bc799a
L
3942 leaq foo@tpoff(%rax), %rax
3943 For 32bit, change
3944 leaq foo@tlsgd(%rip), %rdi
3945 .word 0x6666; rex64; call __tls_get_addr
3946 into:
3947 movl %fs:0, %eax
bffbf940 3948 leaq foo@tpoff(%rax), %rax */
52bc799a
L
3949 if (ABI_64_P (output_bfd))
3950 memcpy (contents + roff - 4,
3951 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3952 16);
3953 else
3954 memcpy (contents + roff - 3,
3955 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3956 15);
eb4ff4d6 3957 bfd_put_32 (output_bfd,
351f65ca 3958 elf_x86_64_tpoff (info, relocation),
142411ca 3959 contents + roff + 8);
a3fadc9a 3960 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
bffbf940
JJ
3961 rel++;
3962 continue;
3963 }
351f65ca 3964 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
67a4f2b7
AO
3965 {
3966 /* GDesc -> LE transition.
3967 It's originally something like:
3968 leaq x@tlsdesc(%rip), %rax
3969
3970 Change it to:
c9736ba0 3971 movl $x@tpoff, %rax. */
67a4f2b7 3972
c9736ba0 3973 unsigned int val, type;
67a4f2b7 3974
67a4f2b7 3975 type = bfd_get_8 (input_bfd, contents + roff - 3);
67a4f2b7 3976 val = bfd_get_8 (input_bfd, contents + roff - 1);
67a4f2b7
AO
3977 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3978 contents + roff - 3);
3979 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3980 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3981 contents + roff - 1);
eb4ff4d6 3982 bfd_put_32 (output_bfd,
351f65ca 3983 elf_x86_64_tpoff (info, relocation),
67a4f2b7
AO
3984 contents + roff);
3985 continue;
3986 }
351f65ca 3987 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
67a4f2b7
AO
3988 {
3989 /* GDesc -> LE transition.
3990 It's originally:
3991 call *(%rax)
3992 Turn it into:
142411ca 3993 xchg %ax,%ax. */
10efb593 3994 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
3995 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3996 continue;
3997 }
351f65ca 3998 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
bffbf940 3999 {
bffbf940
JJ
4000 /* IE->LE transition:
4001 Originally it can be one of:
4002 movq foo@gottpoff(%rip), %reg
4003 addq foo@gottpoff(%rip), %reg
4004 We change it into:
4005 movq $foo, %reg
4006 leaq foo(%reg), %reg
4007 addq $foo, %reg. */
142411ca
L
4008
4009 unsigned int val, type, reg;
4010
4011 val = bfd_get_8 (input_bfd, contents + roff - 3);
4012 type = bfd_get_8 (input_bfd, contents + roff - 2);
4013 reg = bfd_get_8 (input_bfd, contents + roff - 1);
bffbf940 4014 reg >>= 3;
bffbf940
JJ
4015 if (type == 0x8b)
4016 {
4017 /* movq */
4018 if (val == 0x4c)
4019 bfd_put_8 (output_bfd, 0x49,
142411ca 4020 contents + roff - 3);
4a4c5f25
L
4021 else if (!ABI_64_P (output_bfd) && val == 0x44)
4022 bfd_put_8 (output_bfd, 0x41,
4023 contents + roff - 3);
bffbf940 4024 bfd_put_8 (output_bfd, 0xc7,
142411ca 4025 contents + roff - 2);
bffbf940 4026 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 4027 contents + roff - 1);
bffbf940
JJ
4028 }
4029 else if (reg == 4)
4030 {
4031 /* addq -> addq - addressing with %rsp/%r12 is
4032 special */
4033 if (val == 0x4c)
4034 bfd_put_8 (output_bfd, 0x49,
142411ca 4035 contents + roff - 3);
4a4c5f25
L
4036 else if (!ABI_64_P (output_bfd) && val == 0x44)
4037 bfd_put_8 (output_bfd, 0x41,
4038 contents + roff - 3);
bffbf940 4039 bfd_put_8 (output_bfd, 0x81,
142411ca 4040 contents + roff - 2);
bffbf940 4041 bfd_put_8 (output_bfd, 0xc0 | reg,
142411ca 4042 contents + roff - 1);
bffbf940
JJ
4043 }
4044 else
4045 {
4046 /* addq -> leaq */
4047 if (val == 0x4c)
4048 bfd_put_8 (output_bfd, 0x4d,
142411ca 4049 contents + roff - 3);
4a4c5f25
L
4050 else if (!ABI_64_P (output_bfd) && val == 0x44)
4051 bfd_put_8 (output_bfd, 0x45,
4052 contents + roff - 3);
bffbf940 4053 bfd_put_8 (output_bfd, 0x8d,
142411ca 4054 contents + roff - 2);
bffbf940 4055 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
142411ca 4056 contents + roff - 1);
bffbf940 4057 }
eb4ff4d6 4058 bfd_put_32 (output_bfd,
351f65ca 4059 elf_x86_64_tpoff (info, relocation),
142411ca 4060 contents + roff);
bffbf940
JJ
4061 continue;
4062 }
142411ca
L
4063 else
4064 BFD_ASSERT (FALSE);
bffbf940
JJ
4065 }
4066
6de2ae4a 4067 if (htab->elf.sgot == NULL)
bffbf940
JJ
4068 abort ();
4069
4070 if (h != NULL)
67a4f2b7
AO
4071 {
4072 off = h->got.offset;
351f65ca 4073 offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
67a4f2b7 4074 }
bffbf940
JJ
4075 else
4076 {
4077 if (local_got_offsets == NULL)
4078 abort ();
4079
4080 off = local_got_offsets[r_symndx];
67a4f2b7 4081 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
4082 }
4083
4084 if ((off & 1) != 0)
4085 off &= ~1;
26e41594 4086 else
bffbf940
JJ
4087 {
4088 Elf_Internal_Rela outrel;
bffbf940 4089 int dr_type, indx;
67a4f2b7 4090 asection *sreloc;
bffbf940 4091
6de2ae4a 4092 if (htab->elf.srelgot == NULL)
bffbf940
JJ
4093 abort ();
4094
67a4f2b7
AO
4095 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4096
4097 if (GOT_TLS_GDESC_P (tls_type))
4098 {
351f65ca 4099 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
67a4f2b7 4100 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
6de2ae4a
L
4101 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
4102 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4103 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
4104 + offplt
4105 + htab->sgotplt_jump_table_size);
6de2ae4a 4106 sreloc = htab->elf.srelplt;
67a4f2b7 4107 if (indx == 0)
351f65ca 4108 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
67a4f2b7
AO
4109 else
4110 outrel.r_addend = 0;
351f65ca 4111 elf_append_rela (output_bfd, sreloc, &outrel);
67a4f2b7
AO
4112 }
4113
6de2ae4a 4114 sreloc = htab->elf.srelgot;
67a4f2b7 4115
6de2ae4a
L
4116 outrel.r_offset = (htab->elf.sgot->output_section->vma
4117 + htab->elf.sgot->output_offset + off);
bffbf940 4118
67a4f2b7 4119 if (GOT_TLS_GD_P (tls_type))
bffbf940 4120 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
4121 else if (GOT_TLS_GDESC_P (tls_type))
4122 goto dr_done;
bffbf940
JJ
4123 else
4124 dr_type = R_X86_64_TPOFF64;
4125
6de2ae4a 4126 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
bffbf940 4127 outrel.r_addend = 0;
67a4f2b7
AO
4128 if ((dr_type == R_X86_64_TPOFF64
4129 || dr_type == R_X86_64_TLSDESC) && indx == 0)
351f65ca
L
4130 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
4131 outrel.r_info = htab->r_info (indx, dr_type);
bffbf940 4132
351f65ca 4133 elf_append_rela (output_bfd, sreloc, &outrel);
bffbf940 4134
67a4f2b7 4135 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
4136 {
4137 if (indx == 0)
4138 {
d40d037c 4139 BFD_ASSERT (! unresolved_reloc);
bffbf940 4140 bfd_put_64 (output_bfd,
351f65ca 4141 relocation - elf_x86_64_dtpoff_base (info),
6de2ae4a 4142 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
bffbf940
JJ
4143 }
4144 else
4145 {
4146 bfd_put_64 (output_bfd, 0,
6de2ae4a 4147 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 4148 outrel.r_info = htab->r_info (indx,
bffbf940
JJ
4149 R_X86_64_DTPOFF64);
4150 outrel.r_offset += GOT_ENTRY_SIZE;
351f65ca 4151 elf_append_rela (output_bfd, sreloc,
464d3bd4 4152 &outrel);
bffbf940
JJ
4153 }
4154 }
4155
67a4f2b7 4156 dr_done:
bffbf940
JJ
4157 if (h != NULL)
4158 h->got.offset |= 1;
4159 else
4160 local_got_offsets[r_symndx] |= 1;
4161 }
4162
67a4f2b7
AO
4163 if (off >= (bfd_vma) -2
4164 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940 4165 abort ();
351f65ca 4166 if (r_type == ELF32_R_TYPE (rel->r_info))
bffbf940 4167 {
67a4f2b7
AO
4168 if (r_type == R_X86_64_GOTPC32_TLSDESC
4169 || r_type == R_X86_64_TLSDESC_CALL)
6de2ae4a
L
4170 relocation = htab->elf.sgotplt->output_section->vma
4171 + htab->elf.sgotplt->output_offset
67a4f2b7
AO
4172 + offplt + htab->sgotplt_jump_table_size;
4173 else
6de2ae4a
L
4174 relocation = htab->elf.sgot->output_section->vma
4175 + htab->elf.sgot->output_offset + off;
b34976b6 4176 unresolved_reloc = FALSE;
bffbf940 4177 }
142411ca 4178 else
67a4f2b7 4179 {
142411ca 4180 bfd_vma roff = rel->r_offset;
67a4f2b7 4181
351f65ca 4182 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
142411ca 4183 {
52bc799a 4184 /* GD->IE transition. For 64bit, change
142411ca
L
4185 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4186 .word 0x6666; rex64; call __tls_get_addr@plt
52bc799a 4187 into:
142411ca 4188 movq %fs:0, %rax
52bc799a
L
4189 addq foo@gottpoff(%rip), %rax
4190 For 32bit, change
4191 leaq foo@tlsgd(%rip), %rdi
4192 .word 0x6666; rex64; call __tls_get_addr@plt
4193 into:
4194 movl %fs:0, %eax
142411ca 4195 addq foo@gottpoff(%rip), %rax */
52bc799a
L
4196 if (ABI_64_P (output_bfd))
4197 memcpy (contents + roff - 4,
4198 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4199 16);
4200 else
4201 memcpy (contents + roff - 3,
4202 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4203 15);
142411ca 4204
6de2ae4a
L
4205 relocation = (htab->elf.sgot->output_section->vma
4206 + htab->elf.sgot->output_offset + off
142411ca
L
4207 - roff
4208 - input_section->output_section->vma
4209 - input_section->output_offset
4210 - 12);
4211 bfd_put_32 (output_bfd, relocation,
4212 contents + roff + 8);
4213 /* Skip R_X86_64_PLT32. */
4214 rel++;
4215 continue;
4216 }
351f65ca 4217 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
142411ca
L
4218 {
4219 /* GDesc -> IE transition.
4220 It's originally something like:
4221 leaq x@tlsdesc(%rip), %rax
67a4f2b7 4222
142411ca 4223 Change it to:
c9736ba0 4224 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
67a4f2b7 4225
142411ca
L
4226 /* Now modify the instruction as appropriate. To
4227 turn a leaq into a movq in the form we use it, it
4228 suffices to change the second byte from 0x8d to
4229 0x8b. */
4230 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
4231
4232 bfd_put_32 (output_bfd,
6de2ae4a
L
4233 htab->elf.sgot->output_section->vma
4234 + htab->elf.sgot->output_offset + off
142411ca
L
4235 - rel->r_offset
4236 - input_section->output_section->vma
4237 - input_section->output_offset
4238 - 4,
4239 contents + roff);
4240 continue;
4241 }
351f65ca 4242 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
142411ca
L
4243 {
4244 /* GDesc -> IE transition.
4245 It's originally:
4246 call *(%rax)
4247
4248 Change it to:
c9736ba0 4249 xchg %ax, %ax. */
142411ca 4250
142411ca
L
4251 bfd_put_8 (output_bfd, 0x66, contents + roff);
4252 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4253 continue;
4254 }
4255 else
4256 BFD_ASSERT (FALSE);
67a4f2b7 4257 }
bffbf940
JJ
4258 break;
4259
4260 case R_X86_64_TLSLD:
351f65ca
L
4261 if (! elf_x86_64_tls_transition (info, input_bfd,
4262 input_section, contents,
4263 symtab_hdr, sym_hashes,
4264 &r_type, GOT_UNKNOWN,
4265 rel, relend, h, r_symndx))
142411ca 4266 return FALSE;
a3fadc9a 4267
142411ca
L
4268 if (r_type != R_X86_64_TLSLD)
4269 {
bffbf940 4270 /* LD->LE transition:
a3fadc9a 4271 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
52bc799a
L
4272 For 64bit, we change it into:
4273 .word 0x6666; .byte 0x66; movq %fs:0, %rax.
4274 For 32bit, we change it into:
4275 nopl 0x0(%rax); movl %fs:0, %eax. */
142411ca
L
4276
4277 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
52bc799a
L
4278 if (ABI_64_P (output_bfd))
4279 memcpy (contents + rel->r_offset - 3,
4280 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
4281 else
4282 memcpy (contents + rel->r_offset - 3,
4283 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
a3fadc9a 4284 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
bffbf940
JJ
4285 rel++;
4286 continue;
4287 }
4288
6de2ae4a 4289 if (htab->elf.sgot == NULL)
bffbf940
JJ
4290 abort ();
4291
4292 off = htab->tls_ld_got.offset;
4293 if (off & 1)
4294 off &= ~1;
4295 else
4296 {
4297 Elf_Internal_Rela outrel;
bffbf940 4298
6de2ae4a 4299 if (htab->elf.srelgot == NULL)
bffbf940
JJ
4300 abort ();
4301
6de2ae4a
L
4302 outrel.r_offset = (htab->elf.sgot->output_section->vma
4303 + htab->elf.sgot->output_offset + off);
bffbf940
JJ
4304
4305 bfd_put_64 (output_bfd, 0,
6de2ae4a 4306 htab->elf.sgot->contents + off);
bffbf940 4307 bfd_put_64 (output_bfd, 0,
6de2ae4a 4308 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
351f65ca 4309 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
bffbf940 4310 outrel.r_addend = 0;
351f65ca 4311 elf_append_rela (output_bfd, htab->elf.srelgot,
464d3bd4 4312 &outrel);
bffbf940
JJ
4313 htab->tls_ld_got.offset |= 1;
4314 }
6de2ae4a
L
4315 relocation = htab->elf.sgot->output_section->vma
4316 + htab->elf.sgot->output_offset + off;
b34976b6 4317 unresolved_reloc = FALSE;
bffbf940
JJ
4318 break;
4319
4320 case R_X86_64_DTPOFF32:
1d85728f 4321 if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
351f65ca 4322 relocation -= elf_x86_64_dtpoff_base (info);
bffbf940 4323 else
351f65ca 4324 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
4325 break;
4326
4327 case R_X86_64_TPOFF32:
6769d501 4328 case R_X86_64_TPOFF64:
9b769489 4329 BFD_ASSERT (info->executable);
351f65ca 4330 relocation = elf_x86_64_tpoff (info, relocation);
bffbf940
JJ
4331 break;
4332
70256ad8
AJ
4333 default:
4334 break;
4335 }
8d88c4ca 4336
239e1f3a
AM
4337 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4338 because such sections are not SEC_ALLOC and thus ld.so will
4339 not process them. */
c434dee6 4340 if (unresolved_reloc
239e1f3a 4341 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
4342 && h->def_dynamic)
4343 && _bfd_elf_section_offset (output_bfd, info, input_section,
4344 rel->r_offset) != (bfd_vma) -1)
a040981f
L
4345 {
4346 (*_bfd_error_handler)
4347 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4348 input_bfd,
4349 input_section,
4350 (long) rel->r_offset,
4351 howto->name,
4352 h->root.root.string);
4353 return FALSE;
4354 }
c434dee6 4355
cbe950e9 4356do_relocation:
8d88c4ca 4357 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
4358 contents, rel->r_offset,
4359 relocation, rel->r_addend);
8d88c4ca 4360
62d78908 4361check_relocation_error:
8d88c4ca 4362 if (r != bfd_reloc_ok)
8da6118f 4363 {
c434dee6
AJ
4364 const char *name;
4365
4366 if (h != NULL)
4367 name = h->root.root.string;
4368 else
8da6118f 4369 {
c434dee6
AJ
4370 name = bfd_elf_string_from_elf_section (input_bfd,
4371 symtab_hdr->sh_link,
4372 sym->st_name);
4373 if (name == NULL)
b34976b6 4374 return FALSE;
c434dee6
AJ
4375 if (*name == '\0')
4376 name = bfd_section_name (input_bfd, sec);
4377 }
4378
4379 if (r == bfd_reloc_overflow)
4380 {
c434dee6 4381 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
4382 (info, (h ? &h->root : NULL), name, howto->name,
4383 (bfd_vma) 0, input_bfd, input_section,
4384 rel->r_offset)))
b34976b6 4385 return FALSE;
c434dee6
AJ
4386 }
4387 else
4388 {
4389 (*_bfd_error_handler)
bb95161d 4390 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
d003868e 4391 input_bfd, input_section,
c434dee6 4392 (long) rel->r_offset, name, (int) r);
b34976b6 4393 return FALSE;
8da6118f
KH
4394 }
4395 }
8d88c4ca 4396 }
70256ad8 4397
b34976b6 4398 return TRUE;
70256ad8
AJ
4399}
4400
4401/* Finish up dynamic symbol handling. We set the contents of various
4402 dynamic sections here. */
4403
b34976b6 4404static bfd_boolean
351f65ca
L
4405elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
4406 struct bfd_link_info *info,
4407 struct elf_link_hash_entry *h,
220cf809 4408 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
70256ad8 4409{
351f65ca 4410 struct elf_x86_64_link_hash_table *htab;
eed180f8
RM
4411 const struct elf_x86_64_backend_data *const abed
4412 = get_elf_x86_64_backend_data (output_bfd);
70256ad8 4413
351f65ca 4414 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
4415 if (htab == NULL)
4416 return FALSE;
70256ad8
AJ
4417
4418 if (h->plt.offset != (bfd_vma) -1)
4419 {
70256ad8
AJ
4420 bfd_vma plt_index;
4421 bfd_vma got_offset;
4422 Elf_Internal_Rela rela;
947216bf 4423 bfd_byte *loc;
cbe950e9 4424 asection *plt, *gotplt, *relplt;
351f65ca 4425 const struct elf_backend_data *bed;
cbe950e9
L
4426
4427 /* When building a static executable, use .iplt, .igot.plt and
4428 .rela.iplt sections for STT_GNU_IFUNC symbols. */
6de2ae4a 4429 if (htab->elf.splt != NULL)
cbe950e9 4430 {
6de2ae4a
L
4431 plt = htab->elf.splt;
4432 gotplt = htab->elf.sgotplt;
4433 relplt = htab->elf.srelplt;
cbe950e9
L
4434 }
4435 else
4436 {
6de2ae4a
L
4437 plt = htab->elf.iplt;
4438 gotplt = htab->elf.igotplt;
4439 relplt = htab->elf.irelplt;
cbe950e9 4440 }
70256ad8
AJ
4441
4442 /* This symbol has an entry in the procedure linkage table. Set
407443a3 4443 it up. */
cbe950e9
L
4444 if ((h->dynindx == -1
4445 && !((h->forced_local || info->executable)
4446 && h->def_regular
4447 && h->type == STT_GNU_IFUNC))
4448 || plt == NULL
4449 || gotplt == NULL
4450 || relplt == NULL)
cec7f46a 4451 abort ();
70256ad8
AJ
4452
4453 /* Get the index in the procedure linkage table which
4454 corresponds to this symbol. This is the index of this symbol
4455 in all the symbols for which we are making plt entries. The
cbe950e9 4456 first entry in the procedure linkage table is reserved.
6bbec505 4457
cbe950e9 4458 Get the offset into the .got table of the entry that
407443a3 4459 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
cbe950e9
L
4460 bytes. The first three are reserved for the dynamic linker.
4461
4462 For static executables, we don't reserve anything. */
4463
6de2ae4a 4464 if (plt == htab->elf.splt)
cbe950e9 4465 {
eed180f8 4466 got_offset = h->plt.offset / abed->plt_entry_size - 1;
e1f98742 4467 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
cbe950e9
L
4468 }
4469 else
4470 {
eed180f8 4471 got_offset = h->plt.offset / abed->plt_entry_size;
e1f98742 4472 got_offset = got_offset * GOT_ENTRY_SIZE;
cbe950e9 4473 }
70256ad8
AJ
4474
4475 /* Fill in the entry in the procedure linkage table. */
eed180f8
RM
4476 memcpy (plt->contents + h->plt.offset, abed->plt_entry,
4477 abed->plt_entry_size);
4478
4479 /* Insert the relocation positions of the plt section. */
4480
4481 /* Put offset the PC-relative instruction referring to the GOT entry,
4482 subtracting the size of that instruction. */
653165cc 4483 bfd_put_32 (output_bfd,
eed180f8
RM
4484 (gotplt->output_section->vma
4485 + gotplt->output_offset
4486 + got_offset
4487 - plt->output_section->vma
4488 - plt->output_offset
4489 - h->plt.offset
4490 - abed->plt_got_insn_size),
4491 plt->contents + h->plt.offset + abed->plt_got_offset);
cbe950e9 4492
653165cc 4493 /* Fill in the entry in the global offset table, initially this
eed180f8 4494 points to the second part of the PLT entry. */
cbe950e9
L
4495 bfd_put_64 (output_bfd, (plt->output_section->vma
4496 + plt->output_offset
eed180f8 4497 + h->plt.offset + abed->plt_lazy_offset),
cbe950e9 4498 gotplt->contents + got_offset);
70256ad8
AJ
4499
4500 /* Fill in the entry in the .rela.plt section. */
cbe950e9
L
4501 rela.r_offset = (gotplt->output_section->vma
4502 + gotplt->output_offset
70256ad8 4503 + got_offset);
cbe950e9
L
4504 if (h->dynindx == -1
4505 || ((info->executable
4506 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4507 && h->def_regular
4508 && h->type == STT_GNU_IFUNC))
4509 {
4510 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4511 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
351f65ca 4512 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
cbe950e9
L
4513 rela.r_addend = (h->root.u.def.value
4514 + h->root.u.def.section->output_section->vma
4515 + h->root.u.def.section->output_offset);
e1f98742
L
4516 /* R_X86_64_IRELATIVE comes last. */
4517 plt_index = htab->next_irelative_index--;
cbe950e9
L
4518 }
4519 else
4520 {
351f65ca 4521 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
cbe950e9 4522 rela.r_addend = 0;
e1f98742
L
4523 plt_index = htab->next_jump_slot_index++;
4524 }
4525
4526 /* Don't fill PLT entry for static executables. */
4527 if (plt == htab->elf.splt)
4528 {
4529 /* Put relocation index. */
4530 bfd_put_32 (output_bfd, plt_index,
eed180f8 4531 plt->contents + h->plt.offset + abed->plt_reloc_offset);
e1f98742 4532 /* Put offset for jmp .PLT0. */
eed180f8
RM
4533 bfd_put_32 (output_bfd, - (h->plt.offset + abed->plt_plt_insn_end),
4534 plt->contents + h->plt.offset + abed->plt_plt_offset);
cbe950e9 4535 }
351f65ca
L
4536
4537 bed = get_elf_backend_data (output_bfd);
4538 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
82e96e07 4539 bed->s->swap_reloca_out (output_bfd, &rela, loc);
70256ad8 4540
f5385ebf 4541 if (!h->def_regular)
70256ad8
AJ
4542 {
4543 /* Mark the symbol as undefined, rather than as defined in
47a9f7b3
JJ
4544 the .plt section. Leave the value if there were any
4545 relocations where pointer equality matters (this is a clue
c434dee6
AJ
4546 for the dynamic linker, to make function pointer
4547 comparisons work between an application and shared
47a9f7b3
JJ
4548 library), otherwise set it to zero. If a function is only
4549 called from a binary, there is no need to slow down
4550 shared libraries because of that. */
70256ad8 4551 sym->st_shndx = SHN_UNDEF;
f5385ebf 4552 if (!h->pointer_equality_needed)
47a9f7b3 4553 sym->st_value = 0;
70256ad8
AJ
4554 }
4555 }
4556
bffbf940 4557 if (h->got.offset != (bfd_vma) -1
351f65ca
L
4558 && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
4559 && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
053579d7 4560 {
053579d7
AJ
4561 Elf_Internal_Rela rela;
4562
4563 /* This symbol has an entry in the global offset table. Set it
bffbf940 4564 up. */
6de2ae4a 4565 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
c434dee6 4566 abort ();
053579d7 4567
6de2ae4a
L
4568 rela.r_offset = (htab->elf.sgot->output_section->vma
4569 + htab->elf.sgot->output_offset
dc810e39 4570 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
4571
4572 /* If this is a static link, or it is a -Bsymbolic link and the
4573 symbol is defined locally or was forced to be local because
4574 of a version file, we just want to emit a RELATIVE reloc.
4575 The entry in the global offset table will already have been
4576 initialized in the relocate_section function. */
710ab287 4577 if (h->def_regular
0018b0a3
L
4578 && h->type == STT_GNU_IFUNC)
4579 {
710ab287
L
4580 if (info->shared)
4581 {
4582 /* Generate R_X86_64_GLOB_DAT. */
4583 goto do_glob_dat;
4584 }
4585 else
4586 {
90d60710
L
4587 asection *plt;
4588
710ab287
L
4589 if (!h->pointer_equality_needed)
4590 abort ();
4591
4592 /* For non-shared object, we can't use .got.plt, which
4593 contains the real function addres if we need pointer
4594 equality. We load the GOT entry with the PLT entry. */
90d60710 4595 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
710ab287
L
4596 bfd_put_64 (output_bfd, (plt->output_section->vma
4597 + plt->output_offset
4598 + h->plt.offset),
6de2ae4a 4599 htab->elf.sgot->contents + h->got.offset);
710ab287
L
4600 return TRUE;
4601 }
0018b0a3
L
4602 }
4603 else if (info->shared
4604 && SYMBOL_REFERENCES_LOCAL (info, h))
053579d7 4605 {
41bed6dd
L
4606 if (!h->def_regular)
4607 return FALSE;
cc78d0af 4608 BFD_ASSERT((h->got.offset & 1) != 0);
351f65ca 4609 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
053579d7
AJ
4610 rela.r_addend = (h->root.u.def.value
4611 + h->root.u.def.section->output_section->vma
4612 + h->root.u.def.section->output_offset);
4613 }
4614 else
4615 {
4616 BFD_ASSERT((h->got.offset & 1) == 0);
710ab287 4617do_glob_dat:
c434dee6 4618 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 4619 htab->elf.sgot->contents + h->got.offset);
351f65ca 4620 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
053579d7
AJ
4621 rela.r_addend = 0;
4622 }
4623
351f65ca 4624 elf_append_rela (output_bfd, htab->elf.srelgot, &rela);
053579d7
AJ
4625 }
4626
f5385ebf 4627 if (h->needs_copy)
70256ad8 4628 {
70256ad8
AJ
4629 Elf_Internal_Rela rela;
4630
4631 /* This symbol needs a copy reloc. Set it up. */
4632
c434dee6
AJ
4633 if (h->dynindx == -1
4634 || (h->root.type != bfd_link_hash_defined
4635 && h->root.type != bfd_link_hash_defweak)
4636 || htab->srelbss == NULL)
4637 abort ();
70256ad8
AJ
4638
4639 rela.r_offset = (h->root.u.def.value
4640 + h->root.u.def.section->output_section->vma
4641 + h->root.u.def.section->output_offset);
351f65ca 4642 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
70256ad8 4643 rela.r_addend = 0;
351f65ca 4644 elf_append_rela (output_bfd, htab->srelbss, &rela);
70256ad8
AJ
4645 }
4646
b34976b6 4647 return TRUE;
70256ad8
AJ
4648}
4649
c25bc9fc
L
4650/* Finish up local dynamic symbol handling. We set the contents of
4651 various dynamic sections here. */
4652
4653static bfd_boolean
351f65ca 4654elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
c25bc9fc
L
4655{
4656 struct elf_link_hash_entry *h
4657 = (struct elf_link_hash_entry *) *slot;
4658 struct bfd_link_info *info
eed180f8 4659 = (struct bfd_link_info *) inf;
c25bc9fc 4660
351f65ca 4661 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
c25bc9fc
L
4662 info, h, NULL);
4663}
4664
c434dee6
AJ
4665/* Used to decide how to sort relocs in an optimal manner for the
4666 dynamic linker, before writing them out. */
4667
4668static enum elf_reloc_type_class
351f65ca 4669elf_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
c434dee6 4670{
351f65ca 4671 switch ((int) ELF32_R_TYPE (rela->r_info))
c434dee6
AJ
4672 {
4673 case R_X86_64_RELATIVE:
1da80baa 4674 case R_X86_64_RELATIVE64:
c434dee6
AJ
4675 return reloc_class_relative;
4676 case R_X86_64_JUMP_SLOT:
4677 return reloc_class_plt;
4678 case R_X86_64_COPY:
4679 return reloc_class_copy;
4680 default:
4681 return reloc_class_normal;
4682 }
4683}
4684
70256ad8
AJ
4685/* Finish up the dynamic sections. */
4686
b34976b6 4687static bfd_boolean
351f65ca
L
4688elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4689 struct bfd_link_info *info)
70256ad8 4690{
351f65ca 4691 struct elf_x86_64_link_hash_table *htab;
70256ad8
AJ
4692 bfd *dynobj;
4693 asection *sdyn;
eed180f8
RM
4694 const struct elf_x86_64_backend_data *const abed
4695 = get_elf_x86_64_backend_data (output_bfd);
70256ad8 4696
351f65ca 4697 htab = elf_x86_64_hash_table (info);
4dfe6ac6
NC
4698 if (htab == NULL)
4699 return FALSE;
4700
c434dee6 4701 dynobj = htab->elf.dynobj;
3d4d4302 4702 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
70256ad8 4703
c434dee6 4704 if (htab->elf.dynamic_sections_created)
70256ad8 4705 {
82e96e07
L
4706 bfd_byte *dyncon, *dynconend;
4707 const struct elf_backend_data *bed;
4708 bfd_size_type sizeof_dyn;
70256ad8 4709
6de2ae4a 4710 if (sdyn == NULL || htab->elf.sgot == NULL)
c434dee6 4711 abort ();
70256ad8 4712
82e96e07
L
4713 bed = get_elf_backend_data (dynobj);
4714 sizeof_dyn = bed->s->sizeof_dyn;
4715 dyncon = sdyn->contents;
4716 dynconend = sdyn->contents + sdyn->size;
4717 for (; dyncon < dynconend; dyncon += sizeof_dyn)
70256ad8
AJ
4718 {
4719 Elf_Internal_Dyn dyn;
70256ad8
AJ
4720 asection *s;
4721
82e96e07 4722 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
70256ad8
AJ
4723
4724 switch (dyn.d_tag)
4725 {
4726 default:
053579d7 4727 continue;
70256ad8
AJ
4728
4729 case DT_PLTGOT:
6de2ae4a 4730 s = htab->elf.sgotplt;
8c37241b 4731 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
c434dee6 4732 break;
70256ad8
AJ
4733
4734 case DT_JMPREL:
6de2ae4a 4735 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
c434dee6 4736 break;
70256ad8 4737
c434dee6 4738 case DT_PLTRELSZ:
6de2ae4a 4739 s = htab->elf.srelplt->output_section;
eea6121a 4740 dyn.d_un.d_val = s->size;
70256ad8
AJ
4741 break;
4742
4743 case DT_RELASZ:
c434dee6
AJ
4744 /* The procedure linkage table relocs (DT_JMPREL) should
4745 not be included in the overall relocs (DT_RELA).
4746 Therefore, we override the DT_RELASZ entry here to
4747 make it not include the JMPREL relocs. Since the
4748 linker script arranges for .rela.plt to follow all
4749 other relocation sections, we don't have to worry
4750 about changing the DT_RELA entry. */
6de2ae4a 4751 if (htab->elf.srelplt != NULL)
70256ad8 4752 {
6de2ae4a 4753 s = htab->elf.srelplt->output_section;
eea6121a 4754 dyn.d_un.d_val -= s->size;
70256ad8
AJ
4755 }
4756 break;
67a4f2b7
AO
4757
4758 case DT_TLSDESC_PLT:
6de2ae4a 4759 s = htab->elf.splt;
67a4f2b7
AO
4760 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4761 + htab->tlsdesc_plt;
4762 break;
4763
4764 case DT_TLSDESC_GOT:
6de2ae4a 4765 s = htab->elf.sgot;
67a4f2b7
AO
4766 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4767 + htab->tlsdesc_got;
4768 break;
70256ad8 4769 }
c434dee6 4770
82e96e07 4771 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
70256ad8
AJ
4772 }
4773
c434dee6 4774 /* Fill in the special first entry in the procedure linkage table. */
6de2ae4a 4775 if (htab->elf.splt && htab->elf.splt->size > 0)
70256ad8 4776 {
653165cc 4777 /* Fill in the first entry in the procedure linkage table. */
eed180f8
RM
4778 memcpy (htab->elf.splt->contents,
4779 abed->plt0_entry, abed->plt_entry_size);
653165cc
AJ
4780 /* Add offset for pushq GOT+8(%rip), since the instruction
4781 uses 6 bytes subtract this value. */
4782 bfd_put_32 (output_bfd,
6de2ae4a
L
4783 (htab->elf.sgotplt->output_section->vma
4784 + htab->elf.sgotplt->output_offset
653165cc 4785 + 8
6de2ae4a
L
4786 - htab->elf.splt->output_section->vma
4787 - htab->elf.splt->output_offset
653165cc 4788 - 6),
eed180f8
RM
4789 htab->elf.splt->contents + abed->plt0_got1_offset);
4790 /* Add offset for the PC-relative instruction accessing GOT+16,
4791 subtracting the offset to the end of that instruction. */
653165cc 4792 bfd_put_32 (output_bfd,
6de2ae4a
L
4793 (htab->elf.sgotplt->output_section->vma
4794 + htab->elf.sgotplt->output_offset
653165cc 4795 + 16
6de2ae4a
L
4796 - htab->elf.splt->output_section->vma
4797 - htab->elf.splt->output_offset
eed180f8
RM
4798 - abed->plt0_got2_insn_end),
4799 htab->elf.splt->contents + abed->plt0_got2_offset);
653165cc 4800
eed180f8
RM
4801 elf_section_data (htab->elf.splt->output_section)
4802 ->this_hdr.sh_entsize = abed->plt_entry_size;
67a4f2b7
AO
4803
4804 if (htab->tlsdesc_plt)
4805 {
4806 bfd_put_64 (output_bfd, (bfd_vma) 0,
6de2ae4a 4807 htab->elf.sgot->contents + htab->tlsdesc_got);
67a4f2b7 4808
6de2ae4a 4809 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
eed180f8 4810 abed->plt0_entry, abed->plt_entry_size);
67a4f2b7
AO
4811
4812 /* Add offset for pushq GOT+8(%rip), since the
4813 instruction uses 6 bytes subtract this value. */
4814 bfd_put_32 (output_bfd,
6de2ae4a
L
4815 (htab->elf.sgotplt->output_section->vma
4816 + htab->elf.sgotplt->output_offset
67a4f2b7 4817 + 8
6de2ae4a
L
4818 - htab->elf.splt->output_section->vma
4819 - htab->elf.splt->output_offset
67a4f2b7
AO
4820 - htab->tlsdesc_plt
4821 - 6),
eed180f8
RM
4822 htab->elf.splt->contents
4823 + htab->tlsdesc_plt + abed->plt0_got1_offset);
4824 /* Add offset for the PC-relative instruction accessing GOT+TDG,
4825 where TGD stands for htab->tlsdesc_got, subtracting the offset
4826 to the end of that instruction. */
67a4f2b7 4827 bfd_put_32 (output_bfd,
6de2ae4a
L
4828 (htab->elf.sgot->output_section->vma
4829 + htab->elf.sgot->output_offset
67a4f2b7 4830 + htab->tlsdesc_got
6de2ae4a
L
4831 - htab->elf.splt->output_section->vma
4832 - htab->elf.splt->output_offset
67a4f2b7 4833 - htab->tlsdesc_plt
eed180f8
RM
4834 - abed->plt0_got2_insn_end),
4835 htab->elf.splt->contents
4836 + htab->tlsdesc_plt + abed->plt0_got2_offset);
67a4f2b7 4837 }
70256ad8 4838 }
70256ad8
AJ
4839 }
4840
6de2ae4a 4841 if (htab->elf.sgotplt)
70256ad8 4842 {
56d4289c
L
4843 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4844 {
4845 (*_bfd_error_handler)
4846 (_("discarded output section: `%A'"), htab->elf.sgotplt);
4847 return FALSE;
4848 }
4849
c434dee6 4850 /* Fill in the first three entries in the global offset table. */
6de2ae4a 4851 if (htab->elf.sgotplt->size > 0)
c434dee6
AJ
4852 {
4853 /* Set the first entry in the global offset table to the address of
4854 the dynamic section. */
4855 if (sdyn == NULL)
6de2ae4a 4856 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
c434dee6
AJ
4857 else
4858 bfd_put_64 (output_bfd,
4859 sdyn->output_section->vma + sdyn->output_offset,
6de2ae4a 4860 htab->elf.sgotplt->contents);
c434dee6 4861 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
6de2ae4a
L
4862 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4863 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
c434dee6 4864 }
70256ad8 4865
6de2ae4a 4866 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
c434dee6
AJ
4867 GOT_ENTRY_SIZE;
4868 }
70256ad8 4869
e41b3a13 4870 /* Adjust .eh_frame for .plt section. */
9a2a56cc
AM
4871 if (htab->plt_eh_frame != NULL
4872 && htab->plt_eh_frame->contents != NULL)
e41b3a13
JJ
4873 {
4874 if (htab->elf.splt != NULL
4875 && htab->elf.splt->size != 0
4876 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4877 && htab->elf.splt->output_section != NULL
4878 && htab->plt_eh_frame->output_section != NULL)
4879 {
4880 bfd_vma plt_start = htab->elf.splt->output_section->vma;
4881 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4882 + htab->plt_eh_frame->output_offset
4883 + PLT_FDE_START_OFFSET;
4884 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4885 htab->plt_eh_frame->contents
4886 + PLT_FDE_START_OFFSET);
4887 }
dbaa2011 4888 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
e41b3a13
JJ
4889 {
4890 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4891 htab->plt_eh_frame,
4892 htab->plt_eh_frame->contents))
4893 return FALSE;
4894 }
4895 }
4896
6de2ae4a
L
4897 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4898 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
8c37241b
JJ
4899 = GOT_ENTRY_SIZE;
4900
c25bc9fc
L
4901 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4902 htab_traverse (htab->loc_hash_table,
351f65ca 4903 elf_x86_64_finish_local_dynamic_symbol,
c25bc9fc
L
4904 info);
4905
b34976b6 4906 return TRUE;
8d88c4ca
NC
4907}
4908
4c45e5c9
JJ
4909/* Return address for Ith PLT stub in section PLT, for relocation REL
4910 or (bfd_vma) -1 if it should not be included. */
4911
4912static bfd_vma
351f65ca
L
4913elf_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4914 const arelent *rel ATTRIBUTE_UNUSED)
4c45e5c9 4915{
eed180f8 4916 return plt->vma + (i + 1) * GET_PLT_ENTRY_SIZE (plt->owner);
4c45e5c9 4917}
8df9fc9d 4918
d2b2c203
DJ
4919/* Handle an x86-64 specific section when reading an object file. This
4920 is called when elfcode.h finds a section with an unknown type. */
4921
4922static bfd_boolean
351f65ca 4923elf_x86_64_section_from_shdr (bfd *abfd,
6dc132d9
L
4924 Elf_Internal_Shdr *hdr,
4925 const char *name,
4926 int shindex)
d2b2c203
DJ
4927{
4928 if (hdr->sh_type != SHT_X86_64_UNWIND)
4929 return FALSE;
4930
6dc132d9 4931 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
4932 return FALSE;
4933
4934 return TRUE;
4935}
4936
3b22753a
L
4937/* Hook called by the linker routine which adds symbols from an object
4938 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4939 of .bss. */
4940
4941static bfd_boolean
351f65ca
L
4942elf_x86_64_add_symbol_hook (bfd *abfd,
4943 struct bfd_link_info *info,
4944 Elf_Internal_Sym *sym,
4945 const char **namep ATTRIBUTE_UNUSED,
4946 flagword *flagsp ATTRIBUTE_UNUSED,
4947 asection **secp,
4948 bfd_vma *valp)
3b22753a
L
4949{
4950 asection *lcomm;
4951
4952 switch (sym->st_shndx)
4953 {
4954 case SHN_X86_64_LCOMMON:
4955 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4956 if (lcomm == NULL)
4957 {
4958 lcomm = bfd_make_section_with_flags (abfd,
4959 "LARGE_COMMON",
4960 (SEC_ALLOC
4961 | SEC_IS_COMMON
4962 | SEC_LINKER_CREATED));
4963 if (lcomm == NULL)
4964 return FALSE;
4965 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4966 }
4967 *secp = lcomm;
4968 *valp = sym->st_size;
c35bdf6e 4969 return TRUE;
3b22753a 4970 }
d8045f23 4971
c16153ae 4972 if ((abfd->flags & DYNAMIC) == 0
f64b2e8d
NC
4973 && (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
4974 || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE))
4975 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
d8045f23 4976
3b22753a
L
4977 return TRUE;
4978}
4979
4980
4981/* Given a BFD section, try to locate the corresponding ELF section
4982 index. */
4983
4984static bfd_boolean
351f65ca
L
4985elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4986 asection *sec, int *index_return)
3b22753a
L
4987{
4988 if (sec == &_bfd_elf_large_com_section)
4989 {
91d6fa6a 4990 *index_return = SHN_X86_64_LCOMMON;
3b22753a
L
4991 return TRUE;
4992 }
4993 return FALSE;
4994}
4995
4996/* Process a symbol. */
4997
4998static void
351f65ca
L
4999elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5000 asymbol *asym)
3b22753a
L
5001{
5002 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5003
5004 switch (elfsym->internal_elf_sym.st_shndx)
5005 {
5006 case SHN_X86_64_LCOMMON:
5007 asym->section = &_bfd_elf_large_com_section;
5008 asym->value = elfsym->internal_elf_sym.st_size;
5009 /* Common symbol doesn't set BSF_GLOBAL. */
5010 asym->flags &= ~BSF_GLOBAL;
5011 break;
5012 }
5013}
5014
5015static bfd_boolean
351f65ca 5016elf_x86_64_common_definition (Elf_Internal_Sym *sym)
3b22753a
L
5017{
5018 return (sym->st_shndx == SHN_COMMON
5019 || sym->st_shndx == SHN_X86_64_LCOMMON);
5020}
5021
5022static unsigned int
351f65ca 5023elf_x86_64_common_section_index (asection *sec)
3b22753a
L
5024{
5025 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5026 return SHN_COMMON;
5027 else
5028 return SHN_X86_64_LCOMMON;
5029}
5030
5031static asection *
351f65ca 5032elf_x86_64_common_section (asection *sec)
3b22753a
L
5033{
5034 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5035 return bfd_com_section_ptr;
5036 else
5037 return &_bfd_elf_large_com_section;
5038}
5039
5040static bfd_boolean
351f65ca
L
5041elf_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5042 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
5043 struct elf_link_hash_entry *h,
5044 Elf_Internal_Sym *sym,
5045 asection **psec,
5046 bfd_vma *pvalue ATTRIBUTE_UNUSED,
5047 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
5048 bfd_boolean *skip ATTRIBUTE_UNUSED,
5049 bfd_boolean *override ATTRIBUTE_UNUSED,
5050 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
5051 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
71e7aa7d
L
5052 bfd_boolean *newdyn ATTRIBUTE_UNUSED,
5053 bfd_boolean *newdef,
351f65ca
L
5054 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
5055 bfd_boolean *newweak ATTRIBUTE_UNUSED,
5056 bfd *abfd ATTRIBUTE_UNUSED,
5057 asection **sec,
71e7aa7d
L
5058 bfd_boolean *olddyn ATTRIBUTE_UNUSED,
5059 bfd_boolean *olddef,
351f65ca
L
5060 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
5061 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
5062 bfd *oldbfd,
5063 asection **oldsec)
3b22753a
L
5064{
5065 /* A normal common symbol and a large common symbol result in a
00492999
L
5066 normal common symbol. We turn the large common symbol into a
5067 normal one. */
71e7aa7d 5068 if (!*olddef
3b22753a 5069 && h->root.type == bfd_link_hash_common
71e7aa7d 5070 && !*newdef
3b22753a 5071 && bfd_is_com_section (*sec)
00492999 5072 && *oldsec != *sec)
3b22753a 5073 {
00492999
L
5074 if (sym->st_shndx == SHN_COMMON
5075 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
5076 {
5077 h->root.u.c.p->section
5078 = bfd_make_section_old_way (oldbfd, "COMMON");
5079 h->root.u.c.p->section->flags = SEC_ALLOC;
5080 }
5081 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5082 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
9a2e389a 5083 *psec = *sec = bfd_com_section_ptr;
3b22753a
L
5084 }
5085
5086 return TRUE;
5087}
5088
5089static int
351f65ca
L
5090elf_x86_64_additional_program_headers (bfd *abfd,
5091 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3b22753a
L
5092{
5093 asection *s;
9a2e389a 5094 int count = 0;
3b22753a
L
5095
5096 /* Check to see if we need a large readonly segment. */
5097 s = bfd_get_section_by_name (abfd, ".lrodata");
5098 if (s && (s->flags & SEC_LOAD))
5099 count++;
5100
5101 /* Check to see if we need a large data segment. Since .lbss sections
5102 is placed right after the .bss section, there should be no need for
5103 a large data segment just because of .lbss. */
5104 s = bfd_get_section_by_name (abfd, ".ldata");
5105 if (s && (s->flags & SEC_LOAD))
5106 count++;
5107
5108 return count;
5109}
5110
fdc90cb4
JJ
5111/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5112
5113static bfd_boolean
351f65ca 5114elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
fdc90cb4
JJ
5115{
5116 if (h->plt.offset != (bfd_vma) -1
5117 && !h->def_regular
5118 && !h->pointer_equality_needed)
5119 return FALSE;
5120
5121 return _bfd_elf_hash_symbol (h);
5122}
5123
c543bf9a
L
5124/* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5125
5126static bfd_boolean
5127elf_x86_64_relocs_compatible (const bfd_target *input,
5128 const bfd_target *output)
5129{
5130 return ((xvec_get_elf_backend_data (input)->s->elfclass
5131 == xvec_get_elf_backend_data (output)->s->elfclass)
5132 && _bfd_elf_relocs_compatible (input, output));
5133}
5134
9a2e389a 5135static const struct bfd_elf_special_section
351f65ca 5136 elf_x86_64_special_sections[]=
3b22753a 5137{
0112cd26
NC
5138 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5139 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5140 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
5141 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5142 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5143 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5144 { NULL, 0, 0, 0, 0 }
3b22753a
L
5145};
5146
70256ad8
AJ
5147#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
5148#define TARGET_LITTLE_NAME "elf64-x86-64"
5149#define ELF_ARCH bfd_arch_i386
ae95ffa6 5150#define ELF_TARGET_ID X86_64_ELF_DATA
70256ad8 5151#define ELF_MACHINE_CODE EM_X86_64
f7661549 5152#define ELF_MAXPAGESIZE 0x200000
2043964e 5153#define ELF_MINPAGESIZE 0x1000
24718e3b 5154#define ELF_COMMONPAGESIZE 0x1000
70256ad8
AJ
5155
5156#define elf_backend_can_gc_sections 1
51b64d56 5157#define elf_backend_can_refcount 1
70256ad8
AJ
5158#define elf_backend_want_got_plt 1
5159#define elf_backend_plt_readonly 1
5160#define elf_backend_want_plt_sym 0
5161#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 5162#define elf_backend_rela_normal 1
e41b3a13 5163#define elf_backend_plt_alignment 4
70256ad8 5164
351f65ca 5165#define elf_info_to_howto elf_x86_64_info_to_howto
70256ad8 5166
70256ad8 5167#define bfd_elf64_bfd_link_hash_table_create \
351f65ca 5168 elf_x86_64_link_hash_table_create
c25bc9fc 5169#define bfd_elf64_bfd_link_hash_table_free \
351f65ca
L
5170 elf_x86_64_link_hash_table_free
5171#define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
157090f7 5172#define bfd_elf64_bfd_reloc_name_lookup \
351f65ca 5173 elf_x86_64_reloc_name_lookup
70256ad8 5174
351f65ca 5175#define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
c543bf9a 5176#define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
351f65ca
L
5177#define elf_backend_check_relocs elf_x86_64_check_relocs
5178#define elf_backend_copy_indirect_symbol elf_x86_64_copy_indirect_symbol
5179#define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
5180#define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5181#define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
5182#define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
5183#define elf_backend_gc_sweep_hook elf_x86_64_gc_sweep_hook
5184#define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5185#define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
8fd79e71
L
5186#ifdef CORE_HEADER
5187#define elf_backend_write_core_note elf_x86_64_write_core_note
5188#endif
351f65ca
L
5189#define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5190#define elf_backend_relocate_section elf_x86_64_relocate_section
5191#define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
5192#define elf_backend_always_size_sections elf_x86_64_always_size_sections
74541ad4 5193#define elf_backend_init_index_section _bfd_elf_init_1_index_section
351f65ca 5194#define elf_backend_plt_sym_val elf_x86_64_plt_sym_val
407443a3 5195#define elf_backend_object_p elf64_x86_64_elf_object_p
351f65ca 5196#define bfd_elf64_mkobject elf_x86_64_mkobject
8d88c4ca 5197
d2b2c203 5198#define elf_backend_section_from_shdr \
351f65ca 5199 elf_x86_64_section_from_shdr
d2b2c203 5200
3b22753a 5201#define elf_backend_section_from_bfd_section \
351f65ca 5202 elf_x86_64_elf_section_from_bfd_section
3b22753a 5203#define elf_backend_add_symbol_hook \
351f65ca 5204 elf_x86_64_add_symbol_hook
3b22753a 5205#define elf_backend_symbol_processing \
351f65ca 5206 elf_x86_64_symbol_processing
3b22753a 5207#define elf_backend_common_section_index \
351f65ca 5208 elf_x86_64_common_section_index
3b22753a 5209#define elf_backend_common_section \
351f65ca 5210 elf_x86_64_common_section
3b22753a 5211#define elf_backend_common_definition \
351f65ca 5212 elf_x86_64_common_definition
3b22753a 5213#define elf_backend_merge_symbol \
351f65ca 5214 elf_x86_64_merge_symbol
3b22753a 5215#define elf_backend_special_sections \
351f65ca 5216 elf_x86_64_special_sections
3b22753a 5217#define elf_backend_additional_program_headers \
351f65ca 5218 elf_x86_64_additional_program_headers
fdc90cb4 5219#define elf_backend_hash_symbol \
351f65ca 5220 elf_x86_64_hash_symbol
3b22753a 5221
d8045f23
NC
5222#define elf_backend_post_process_headers _bfd_elf_set_osabi
5223
8d88c4ca 5224#include "elf64-target.h"
9d7cbccd
NC
5225
5226/* FreeBSD support. */
5227
5228#undef TARGET_LITTLE_SYM
5229#define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
5230#undef TARGET_LITTLE_NAME
5231#define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5232
d1036acb
L
5233#undef ELF_OSABI
5234#define ELF_OSABI ELFOSABI_FREEBSD
9d7cbccd 5235
9d7cbccd
NC
5236#undef elf64_bed
5237#define elf64_bed elf64_x86_64_fbsd_bed
5238
5239#include "elf64-target.h"
8a9036a4 5240
a6cc6b3b
RO
5241/* Solaris 2 support. */
5242
5243#undef TARGET_LITTLE_SYM
5244#define TARGET_LITTLE_SYM bfd_elf64_x86_64_sol2_vec
5245#undef TARGET_LITTLE_NAME
5246#define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5247
5248/* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5249 objects won't be recognized. */
5250#undef ELF_OSABI
5251
5252#undef elf64_bed
5253#define elf64_bed elf64_x86_64_sol2_bed
5254
7dc98aea
RO
5255/* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5256 boundary. */
5257#undef elf_backend_static_tls_alignment
5258#define elf_backend_static_tls_alignment 16
5259
a6cc6b3b
RO
5260/* The Solaris 2 ABI requires a plt symbol on all platforms.
5261
5262 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5263 File, p.63. */
5264#undef elf_backend_want_plt_sym
5265#define elf_backend_want_plt_sym 1
5266
5267#include "elf64-target.h"
5268
8059fb19
RM
5269/* Native Client support. */
5270
5271#undef TARGET_LITTLE_SYM
5272#define TARGET_LITTLE_SYM bfd_elf64_x86_64_nacl_vec
5273#undef TARGET_LITTLE_NAME
5274#define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
5275#undef elf64_bed
5276#define elf64_bed elf64_x86_64_nacl_bed
5277
5278#undef ELF_MAXPAGESIZE
5279#undef ELF_MINPAGESIZE
5280#undef ELF_COMMONPAGESIZE
5281#define ELF_MAXPAGESIZE 0x10000
5282#define ELF_MINPAGESIZE 0x10000
5283#define ELF_COMMONPAGESIZE 0x10000
5284
5285/* Restore defaults. */
5286#undef ELF_OSABI
5287#undef elf_backend_static_tls_alignment
5288#undef elf_backend_want_plt_sym
5289#define elf_backend_want_plt_sym 0
5290
5291/* NaCl uses substantially different PLT entries for the same effects. */
5292
5293#undef elf_backend_plt_alignment
5294#define elf_backend_plt_alignment 5
5295#define NACL_PLT_ENTRY_SIZE 64
5296#define NACLMASK 0xe0 /* 32-byte alignment mask. */
5297
5298static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
5299 {
5300 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
5301 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
5302 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5303 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5304 0x41, 0xff, 0xe3, /* jmpq *%r11 */
5305
ea2d813e
RM
5306 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
5307 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopl %cs:0x0(%rax,%rax,1) */
5308
5309 /* 32 bytes of nop to pad out to the standard size. */
8059fb19
RM
5310 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5311 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5312 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5313 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
ea2d813e
RM
5314 0x66, /* excess data32 prefix */
5315 0x90 /* nop */
8059fb19
RM
5316 };
5317
5318static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
5319 {
5320 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
5321 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5322 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5323 0x41, 0xff, 0xe3, /* jmpq *%r11 */
5324
5325 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
5326 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5327 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5328
5329 /* Lazy GOT entries point here (32-byte aligned). */
5330 0x68, /* pushq immediate */
5331 0, 0, 0, 0, /* replaced with index into relocation table. */
5332 0xe9, /* jmp relative */
5333 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
5334
5335 /* 22 bytes of nop to pad out to the standard size. */
5336 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5337 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5338 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
5339 };
5340
5341/* .eh_frame covering the .plt section. */
5342
5343static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
5344 {
5345#if (PLT_CIE_LENGTH != 20 \
5346 || PLT_FDE_LENGTH != 36 \
5347 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
5348 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
5349# error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
5350#endif
5351 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
5352 0, 0, 0, 0, /* CIE ID */
5353 1, /* CIE version */
5354 'z', 'R', 0, /* Augmentation string */
5355 1, /* Code alignment factor */
5356 0x78, /* Data alignment factor */
5357 16, /* Return address column */
5358 1, /* Augmentation size */
5359 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
5360 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
5361 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
5362 DW_CFA_nop, DW_CFA_nop,
5363
5364 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
5365 PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
5366 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
5367 0, 0, 0, 0, /* .plt size goes here */
5368 0, /* Augmentation size */
5369 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
5370 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
5371 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
5372 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5373 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
5374 13, /* Block length */
5375 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
5376 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
5377 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5378 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
5379 DW_CFA_nop, DW_CFA_nop
5380 };
5381
5382static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
5383 {
5384 elf_x86_64_nacl_plt0_entry, /* plt0_entry */
5385 elf_x86_64_nacl_plt_entry, /* plt_entry */
5386 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
5387 2, /* plt0_got1_offset */
5388 9, /* plt0_got2_offset */
5389 13, /* plt0_got2_insn_end */
5390 3, /* plt_got_offset */
5391 33, /* plt_reloc_offset */
5392 38, /* plt_plt_offset */
5393 7, /* plt_got_insn_size */
5394 42, /* plt_plt_insn_end */
5395 32, /* plt_lazy_offset */
5396 elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
5397 sizeof (elf_x86_64_nacl_eh_frame_plt), /* eh_frame_plt_size */
5398 };
5399
5400#undef elf_backend_arch_data
5401#define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
5402
5a68afcf
RM
5403#undef elf_backend_modify_segment_map
5404#define elf_backend_modify_segment_map nacl_modify_segment_map
5405#undef elf_backend_modify_program_headers
5406#define elf_backend_modify_program_headers nacl_modify_program_headers
5407
8059fb19
RM
5408#include "elf64-target.h"
5409
5410/* Native Client x32 support. */
5411
5412#undef TARGET_LITTLE_SYM
5413#define TARGET_LITTLE_SYM bfd_elf32_x86_64_nacl_vec
5414#undef TARGET_LITTLE_NAME
5415#define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
5416#undef elf32_bed
5417#define elf32_bed elf32_x86_64_nacl_bed
5418
5419#define bfd_elf32_bfd_link_hash_table_create \
5420 elf_x86_64_link_hash_table_create
5421#define bfd_elf32_bfd_link_hash_table_free \
5422 elf_x86_64_link_hash_table_free
5423#define bfd_elf32_bfd_reloc_type_lookup \
5424 elf_x86_64_reloc_type_lookup
5425#define bfd_elf32_bfd_reloc_name_lookup \
5426 elf_x86_64_reloc_name_lookup
5427#define bfd_elf32_mkobject \
5428 elf_x86_64_mkobject
5429
5430#undef elf_backend_object_p
5431#define elf_backend_object_p \
5432 elf32_x86_64_elf_object_p
5433
5434#undef elf_backend_bfd_from_remote_memory
5435#define elf_backend_bfd_from_remote_memory \
5436 _bfd_elf32_bfd_from_remote_memory
5437
5438#undef elf_backend_size_info
5439#define elf_backend_size_info \
5440 _bfd_elf32_size_info
5441
5442#include "elf32-target.h"
5443
5444/* Restore defaults. */
5a68afcf 5445#undef elf_backend_object_p
8059fb19 5446#define elf_backend_object_p elf64_x86_64_elf_object_p
5a68afcf
RM
5447#undef elf_backend_bfd_from_remote_memory
5448#undef elf_backend_size_info
5449#undef elf_backend_modify_segment_map
5450#undef elf_backend_modify_program_headers
8059fb19 5451
8a9036a4
L
5452/* Intel L1OM support. */
5453
5454static bfd_boolean
5455elf64_l1om_elf_object_p (bfd *abfd)
5456{
5457 /* Set the right machine number for an L1OM elf64 file. */
5458 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
5459 return TRUE;
5460}
5461
5462#undef TARGET_LITTLE_SYM
5463#define TARGET_LITTLE_SYM bfd_elf64_l1om_vec
5464#undef TARGET_LITTLE_NAME
5465#define TARGET_LITTLE_NAME "elf64-l1om"
5466#undef ELF_ARCH
5467#define ELF_ARCH bfd_arch_l1om
5468
5469#undef ELF_MACHINE_CODE
5470#define ELF_MACHINE_CODE EM_L1OM
5471
5472#undef ELF_OSABI
5473
5474#undef elf64_bed
5475#define elf64_bed elf64_l1om_bed
5476
5477#undef elf_backend_object_p
5478#define elf_backend_object_p elf64_l1om_elf_object_p
5479
8059fb19
RM
5480/* Restore defaults. */
5481#undef ELF_MAXPAGESIZE
5482#undef ELF_MINPAGESIZE
5483#undef ELF_COMMONPAGESIZE
5484#define ELF_MAXPAGESIZE 0x200000
5485#define ELF_MINPAGESIZE 0x1000
5486#define ELF_COMMONPAGESIZE 0x1000
5487#undef elf_backend_plt_alignment
5488#define elf_backend_plt_alignment 4
5489#undef elf_backend_arch_data
5490#define elf_backend_arch_data &elf_x86_64_arch_bed
1a0c107f 5491
8a9036a4
L
5492#include "elf64-target.h"
5493
5494/* FreeBSD L1OM support. */
5495
5496#undef TARGET_LITTLE_SYM
5497#define TARGET_LITTLE_SYM bfd_elf64_l1om_freebsd_vec
5498#undef TARGET_LITTLE_NAME
5499#define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
5500
5501#undef ELF_OSABI
5502#define ELF_OSABI ELFOSABI_FREEBSD
5503
5504#undef elf64_bed
5505#define elf64_bed elf64_l1om_fbsd_bed
5506
8a9036a4 5507#include "elf64-target.h"
351f65ca 5508
7a9068fe
L
5509/* Intel K1OM support. */
5510
5511static bfd_boolean
5512elf64_k1om_elf_object_p (bfd *abfd)
5513{
5514 /* Set the right machine number for an K1OM elf64 file. */
5515 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
5516 return TRUE;
5517}
5518
5519#undef TARGET_LITTLE_SYM
5520#define TARGET_LITTLE_SYM bfd_elf64_k1om_vec
5521#undef TARGET_LITTLE_NAME
5522#define TARGET_LITTLE_NAME "elf64-k1om"
5523#undef ELF_ARCH
5524#define ELF_ARCH bfd_arch_k1om
5525
5526#undef ELF_MACHINE_CODE
5527#define ELF_MACHINE_CODE EM_K1OM
5528
5529#undef ELF_OSABI
5530
5531#undef elf64_bed
5532#define elf64_bed elf64_k1om_bed
5533
5534#undef elf_backend_object_p
5535#define elf_backend_object_p elf64_k1om_elf_object_p
5536
5537#undef elf_backend_static_tls_alignment
5538
5539#undef elf_backend_want_plt_sym
5540#define elf_backend_want_plt_sym 0
5541
5542#include "elf64-target.h"
5543
5544/* FreeBSD K1OM support. */
5545
5546#undef TARGET_LITTLE_SYM
5547#define TARGET_LITTLE_SYM bfd_elf64_k1om_freebsd_vec
5548#undef TARGET_LITTLE_NAME
5549#define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
5550
5551#undef ELF_OSABI
5552#define ELF_OSABI ELFOSABI_FREEBSD
5553
5554#undef elf64_bed
5555#define elf64_bed elf64_k1om_fbsd_bed
5556
5557#include "elf64-target.h"
5558
351f65ca
L
5559/* 32bit x86-64 support. */
5560
351f65ca
L
5561#undef TARGET_LITTLE_SYM
5562#define TARGET_LITTLE_SYM bfd_elf32_x86_64_vec
5563#undef TARGET_LITTLE_NAME
5564#define TARGET_LITTLE_NAME "elf32-x86-64"
8059fb19 5565#undef elf32_bed
351f65ca
L
5566
5567#undef ELF_ARCH
5568#define ELF_ARCH bfd_arch_i386
5569
5570#undef ELF_MACHINE_CODE
5571#define ELF_MACHINE_CODE EM_X86_64
5572
351f65ca
L
5573#undef ELF_OSABI
5574
351f65ca
L
5575#undef elf_backend_object_p
5576#define elf_backend_object_p \
5577 elf32_x86_64_elf_object_p
5578
5579#undef elf_backend_bfd_from_remote_memory
5580#define elf_backend_bfd_from_remote_memory \
5581 _bfd_elf32_bfd_from_remote_memory
5582
5583#undef elf_backend_size_info
5584#define elf_backend_size_info \
5585 _bfd_elf32_size_info
5586
5587#include "elf32-target.h"