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