* doc/c-arm.texi (ARM Directives): Move brackets out of @vars.
[deliverable/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
8d88c4ca 1/* X86-64 specific support for 64-bit ELF
ab96bf03 2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3eb128b2 3 Free Software Foundation, Inc.
8d88c4ca
NC
4 Contributed by Jan Hubicka <jh@suse.cz>.
5
ae9a127f 6 This file is part of BFD, the Binary File Descriptor library.
8d88c4ca 7
ae9a127f
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
cd123cb7 10 the Free Software Foundation; either version 3 of the License, or
ae9a127f 11 (at your option) any later version.
8d88c4ca 12
ae9a127f
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
8d88c4ca 17
ae9a127f
NC
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
cd123cb7
NC
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
8d88c4ca 22
8d88c4ca 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
c434dee6 25#include "bfdlink.h"
8d88c4ca
NC
26#include "libbfd.h"
27#include "elf-bfd.h"
28
29#include "elf/x86-64.h"
30
8d88c4ca
NC
31/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
32#define MINUS_ONE (~ (bfd_vma) 0)
33
34/* The relocation "howto" table. Order of fields:
7b81dfbb
AJ
35 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
36 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
70256ad8
AJ
37static reloc_howto_type x86_64_elf_howto_table[] =
38{
b34976b6
AM
39 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
40 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
41 FALSE),
42 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
43 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
44 FALSE),
45 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
46 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
47 TRUE),
48 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
49 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
50 FALSE),
51 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
52 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
53 TRUE),
54 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
55 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
56 FALSE),
57 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
58 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
59 MINUS_ONE, FALSE),
60 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
61 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
62 MINUS_ONE, FALSE),
63 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
64 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
65 MINUS_ONE, FALSE),
66 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
67 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
68 0xffffffff, TRUE),
69 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
70 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
71 FALSE),
72 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
73 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
74 FALSE),
75 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
76 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
b0360d8c 77 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
b34976b6 78 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
ac2aa337 79 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
b34976b6
AM
80 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
81 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
82 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
83 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
84 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
85 MINUS_ONE, FALSE),
86 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
87 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
88 MINUS_ONE, FALSE),
89 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
90 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
91 MINUS_ONE, FALSE),
92 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
93 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
94 0xffffffff, TRUE),
95 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
96 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
97 0xffffffff, TRUE),
ac2aa337 98 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
b34976b6
AM
99 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
100 0xffffffff, FALSE),
101 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
102 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
103 0xffffffff, TRUE),
104 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
105 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
106 0xffffffff, FALSE),
d6ab8113
JB
107 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
108 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
109 TRUE),
110 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
111 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
112 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
113 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
114 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
115 FALSE, 0xffffffff, 0xffffffff, TRUE),
7b81dfbb
AJ
116 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
117 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
118 FALSE),
119 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
120 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
121 MINUS_ONE, TRUE),
122 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
123 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
124 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
125 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
126 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
127 MINUS_ONE, FALSE),
128 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
129 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
130 MINUS_ONE, FALSE),
67a4f2b7
AO
131 EMPTY_HOWTO (32),
132 EMPTY_HOWTO (33),
133 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
134 complain_overflow_bitfield, bfd_elf_generic_reloc,
135 "R_X86_64_GOTPC32_TLSDESC",
136 FALSE, 0xffffffff, 0xffffffff, TRUE),
137 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
138 complain_overflow_dont, bfd_elf_generic_reloc,
139 "R_X86_64_TLSDESC_CALL",
140 FALSE, 0, 0, FALSE),
141 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
142 complain_overflow_bitfield, bfd_elf_generic_reloc,
143 "R_X86_64_TLSDESC",
144 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
fe4770f4 145
a33d77bc
JB
146 /* We have a gap in the reloc numbers here.
147 R_X86_64_standard counts the number up to this point, and
148 R_X86_64_vt_offset is the value to subtract from a reloc type of
149 R_X86_64_GNU_VT* to form an index into this table. */
67a4f2b7 150#define R_X86_64_standard (R_X86_64_TLSDESC + 1)
a33d77bc
JB
151#define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
152
fe4770f4 153/* GNU extension to record C++ vtable hierarchy. */
b34976b6
AM
154 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
155 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
fe4770f4
AJ
156
157/* GNU extension to record C++ vtable member usage. */
b34976b6
AM
158 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
159 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
160 FALSE)
8d88c4ca
NC
161};
162
163/* Map BFD relocs to the x86_64 elf relocs. */
70256ad8
AJ
164struct elf_reloc_map
165{
8d88c4ca
NC
166 bfd_reloc_code_real_type bfd_reloc_val;
167 unsigned char elf_reloc_val;
168};
169
dc810e39 170static const struct elf_reloc_map x86_64_reloc_map[] =
8d88c4ca 171{
70256ad8
AJ
172 { BFD_RELOC_NONE, R_X86_64_NONE, },
173 { BFD_RELOC_64, R_X86_64_64, },
174 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
175 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
176 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
177 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
178 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
179 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
180 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
181 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
182 { BFD_RELOC_32, R_X86_64_32, },
183 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
184 { BFD_RELOC_16, R_X86_64_16, },
185 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
186 { BFD_RELOC_8, R_X86_64_8, },
187 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
bffbf940
JJ
188 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
189 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
190 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
191 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
192 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
193 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
194 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
195 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
d6ab8113
JB
196 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
197 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
198 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
7b81dfbb
AJ
199 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
200 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
201 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
202 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
203 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
67a4f2b7
AO
204 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
205 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
206 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
fe4770f4
AJ
207 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
208 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
8d88c4ca
NC
209};
210
67a4f2b7
AO
211static reloc_howto_type *
212elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
213{
214 unsigned i;
215
216 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
217 || r_type >= (unsigned int) R_X86_64_max)
218 {
219 if (r_type >= (unsigned int) R_X86_64_standard)
220 {
221 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
222 abfd, (int) r_type);
223 r_type = R_X86_64_NONE;
224 }
225 i = r_type;
226 }
227 else
228 i = r_type - (unsigned int) R_X86_64_vt_offset;
229 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
230 return &x86_64_elf_howto_table[i];
231}
8d88c4ca
NC
232
233/* Given a BFD reloc type, return a HOWTO structure. */
234static reloc_howto_type *
67a4f2b7 235elf64_x86_64_reloc_type_lookup (bfd *abfd,
27482721 236 bfd_reloc_code_real_type code)
8d88c4ca
NC
237{
238 unsigned int i;
27482721 239
8d88c4ca
NC
240 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
241 i++)
242 {
243 if (x86_64_reloc_map[i].bfd_reloc_val == code)
67a4f2b7
AO
244 return elf64_x86_64_rtype_to_howto (abfd,
245 x86_64_reloc_map[i].elf_reloc_val);
8d88c4ca
NC
246 }
247 return 0;
248}
249
157090f7
AM
250static reloc_howto_type *
251elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
252 const char *r_name)
253{
254 unsigned int i;
255
256 for (i = 0;
257 i < (sizeof (x86_64_elf_howto_table)
258 / sizeof (x86_64_elf_howto_table[0]));
259 i++)
260 if (x86_64_elf_howto_table[i].name != NULL
261 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
262 return &x86_64_elf_howto_table[i];
263
264 return NULL;
265}
266
8d88c4ca 267/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
8da6118f 268
8d88c4ca 269static void
27482721
AJ
270elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
271 Elf_Internal_Rela *dst)
8d88c4ca 272{
67a4f2b7 273 unsigned r_type;
8d88c4ca
NC
274
275 r_type = ELF64_R_TYPE (dst->r_info);
67a4f2b7 276 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
8d88c4ca
NC
277 BFD_ASSERT (r_type == cache_ptr->howto->type);
278}
70256ad8 279\f
3bab7989 280/* Support for core dump NOTE sections. */
b34976b6 281static bfd_boolean
27482721 282elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
283{
284 int offset;
eea6121a 285 size_t size;
3bab7989
ML
286
287 switch (note->descsz)
288 {
289 default:
b34976b6 290 return FALSE;
3bab7989
ML
291
292 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
293 /* pr_cursig */
cedb70c5 294 elf_tdata (abfd)->core_signal
3bab7989
ML
295 = bfd_get_16 (abfd, note->descdata + 12);
296
297 /* pr_pid */
cedb70c5 298 elf_tdata (abfd)->core_pid
3bab7989
ML
299 = bfd_get_32 (abfd, note->descdata + 32);
300
301 /* pr_reg */
302 offset = 112;
eea6121a 303 size = 216;
3bab7989
ML
304
305 break;
306 }
307
308 /* Make a ".reg/999" section. */
309 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 310 size, note->descpos + offset);
3bab7989
ML
311}
312
b34976b6 313static bfd_boolean
27482721 314elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3bab7989
ML
315{
316 switch (note->descsz)
317 {
318 default:
b34976b6 319 return FALSE;
3bab7989
ML
320
321 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
322 elf_tdata (abfd)->core_program
323 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
324 elf_tdata (abfd)->core_command
325 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
326 }
327
328 /* Note that for some reason, a spurious space is tacked
329 onto the end of the args in some (at least one anyway)
330 implementations, so strip it off if it exists. */
331
332 {
333 char *command = elf_tdata (abfd)->core_command;
334 int n = strlen (command);
335
336 if (0 < n && command[n - 1] == ' ')
337 command[n - 1] = '\0';
338 }
339
b34976b6 340 return TRUE;
3bab7989
ML
341}
342\f
407443a3 343/* Functions for the x86-64 ELF linker. */
70256ad8 344
407443a3 345/* The name of the dynamic interpreter. This is put in the .interp
70256ad8
AJ
346 section. */
347
407443a3 348#define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
70256ad8 349
d40d037c
AJ
350/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
351 copying dynamic variables from a shared lib into an app's dynbss
352 section, and instead use a dynamic relocation to point into the
353 shared lib. */
354#define ELIMINATE_COPY_RELOCS 1
355
70256ad8
AJ
356/* The size in bytes of an entry in the global offset table. */
357
358#define GOT_ENTRY_SIZE 8
8d88c4ca 359
70256ad8 360/* The size in bytes of an entry in the procedure linkage table. */
8d88c4ca 361
70256ad8
AJ
362#define PLT_ENTRY_SIZE 16
363
364/* The first entry in a procedure linkage table looks like this. See the
365 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
366
367static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
368{
653165cc
AJ
369 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
370 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
10efb593 371 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
70256ad8
AJ
372};
373
374/* Subsequent entries in a procedure linkage table look like this. */
375
376static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
377{
653165cc 378 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
407443a3 379 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
653165cc 380 0x68, /* pushq immediate */
70256ad8
AJ
381 0, 0, 0, 0, /* replaced with index into relocation table. */
382 0xe9, /* jmp relative */
383 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
384};
385
386/* The x86-64 linker needs to keep track of the number of relocs that
985142a4 387 it decides to copy as dynamic relocs in check_relocs for each symbol.
c434dee6
AJ
388 This is so that it can later discard them if they are found to be
389 unnecessary. We store the information in a field extending the
390 regular ELF linker hash table. */
70256ad8 391
c434dee6 392struct elf64_x86_64_dyn_relocs
70256ad8
AJ
393{
394 /* Next section. */
c434dee6
AJ
395 struct elf64_x86_64_dyn_relocs *next;
396
397 /* The input section of the reloc. */
398 asection *sec;
399
400 /* Total number of relocs copied for the input section. */
70256ad8 401 bfd_size_type count;
c434dee6
AJ
402
403 /* Number of pc-relative relocs copied for the input section. */
404 bfd_size_type pc_count;
70256ad8
AJ
405};
406
407/* x86-64 ELF linker hash entry. */
408
409struct elf64_x86_64_link_hash_entry
410{
c434dee6 411 struct elf_link_hash_entry elf;
70256ad8 412
c434dee6
AJ
413 /* Track dynamic relocs copied for this symbol. */
414 struct elf64_x86_64_dyn_relocs *dyn_relocs;
bffbf940
JJ
415
416#define GOT_UNKNOWN 0
417#define GOT_NORMAL 1
418#define GOT_TLS_GD 2
419#define GOT_TLS_IE 3
67a4f2b7
AO
420#define GOT_TLS_GDESC 4
421#define GOT_TLS_GD_BOTH_P(type) \
422 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
423#define GOT_TLS_GD_P(type) \
424 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
425#define GOT_TLS_GDESC_P(type) \
426 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
427#define GOT_TLS_GD_ANY_P(type) \
428 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
bffbf940 429 unsigned char tls_type;
67a4f2b7
AO
430
431 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
432 starting at the end of the jump table. */
433 bfd_vma tlsdesc_got;
bffbf940
JJ
434};
435
436#define elf64_x86_64_hash_entry(ent) \
437 ((struct elf64_x86_64_link_hash_entry *)(ent))
438
439struct elf64_x86_64_obj_tdata
440{
441 struct elf_obj_tdata root;
442
443 /* tls_type for each local got entry. */
444 char *local_got_tls_type;
67a4f2b7
AO
445
446 /* GOTPLT entries for TLS descriptors. */
447 bfd_vma *local_tlsdesc_gotent;
70256ad8
AJ
448};
449
bffbf940
JJ
450#define elf64_x86_64_tdata(abfd) \
451 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
452
453#define elf64_x86_64_local_got_tls_type(abfd) \
454 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
455
67a4f2b7
AO
456#define elf64_x86_64_local_tlsdesc_gotent(abfd) \
457 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
bffbf940 458
c434dee6 459/* x86-64 ELF linker hash table. */
8d88c4ca 460
407443a3
AJ
461struct elf64_x86_64_link_hash_table
462{
c434dee6 463 struct elf_link_hash_table elf;
70256ad8 464
c434dee6
AJ
465 /* Short-cuts to get to dynamic linker sections. */
466 asection *sgot;
467 asection *sgotplt;
468 asection *srelgot;
469 asection *splt;
470 asection *srelplt;
471 asection *sdynbss;
472 asection *srelbss;
70256ad8 473
67a4f2b7
AO
474 /* The offset into splt of the PLT entry for the TLS descriptor
475 resolver. Special values are 0, if not necessary (or not found
476 to be necessary yet), and -1 if needed but not determined
477 yet. */
478 bfd_vma tlsdesc_plt;
479 /* The offset into sgot of the GOT entry used by the PLT entry
480 above. */
481 bfd_vma tlsdesc_got;
482
bffbf940
JJ
483 union {
484 bfd_signed_vma refcount;
485 bfd_vma offset;
486 } tls_ld_got;
487
67a4f2b7
AO
488 /* The amount of space used by the jump slots in the GOT. */
489 bfd_vma sgotplt_jump_table_size;
490
c434dee6
AJ
491 /* Small local sym to section mapping cache. */
492 struct sym_sec_cache sym_sec;
493};
70256ad8
AJ
494
495/* Get the x86-64 ELF linker hash table from a link_info structure. */
8d88c4ca
NC
496
497#define elf64_x86_64_hash_table(p) \
498 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
499
67a4f2b7
AO
500#define elf64_x86_64_compute_jump_table_size(htab) \
501 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
502
407443a3 503/* Create an entry in an x86-64 ELF linker hash table. */
70256ad8
AJ
504
505static struct bfd_hash_entry *
27482721
AJ
506link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
507 const char *string)
70256ad8 508{
70256ad8 509 /* Allocate the structure if it has not already been allocated by a
c434dee6
AJ
510 subclass. */
511 if (entry == NULL)
512 {
513 entry = bfd_hash_allocate (table,
514 sizeof (struct elf64_x86_64_link_hash_entry));
515 if (entry == NULL)
516 return entry;
517 }
70256ad8
AJ
518
519 /* Call the allocation method of the superclass. */
c434dee6
AJ
520 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
521 if (entry != NULL)
70256ad8 522 {
c434dee6
AJ
523 struct elf64_x86_64_link_hash_entry *eh;
524
525 eh = (struct elf64_x86_64_link_hash_entry *) entry;
526 eh->dyn_relocs = NULL;
bffbf940 527 eh->tls_type = GOT_UNKNOWN;
67a4f2b7 528 eh->tlsdesc_got = (bfd_vma) -1;
70256ad8
AJ
529 }
530
c434dee6 531 return entry;
70256ad8
AJ
532}
533
8d88c4ca
NC
534/* Create an X86-64 ELF linker hash table. */
535
536static struct bfd_link_hash_table *
27482721 537elf64_x86_64_link_hash_table_create (bfd *abfd)
8d88c4ca
NC
538{
539 struct elf64_x86_64_link_hash_table *ret;
dc810e39 540 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
8d88c4ca 541
e2d34d7d 542 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
c434dee6 543 if (ret == NULL)
8d88c4ca
NC
544 return NULL;
545
66eb6687
AM
546 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
547 sizeof (struct elf64_x86_64_link_hash_entry)))
8d88c4ca 548 {
e2d34d7d 549 free (ret);
8d88c4ca
NC
550 return NULL;
551 }
552
c434dee6
AJ
553 ret->sgot = NULL;
554 ret->sgotplt = NULL;
555 ret->srelgot = NULL;
556 ret->splt = NULL;
557 ret->srelplt = NULL;
558 ret->sdynbss = NULL;
559 ret->srelbss = NULL;
560 ret->sym_sec.abfd = NULL;
67a4f2b7
AO
561 ret->tlsdesc_plt = 0;
562 ret->tlsdesc_got = 0;
bffbf940 563 ret->tls_ld_got.refcount = 0;
67a4f2b7 564 ret->sgotplt_jump_table_size = 0;
c434dee6
AJ
565
566 return &ret->elf.root;
567}
568
569/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
570 shortcuts to them in our hash table. */
571
b34976b6 572static bfd_boolean
27482721 573create_got_section (bfd *dynobj, struct bfd_link_info *info)
c434dee6
AJ
574{
575 struct elf64_x86_64_link_hash_table *htab;
576
577 if (! _bfd_elf_create_got_section (dynobj, info))
b34976b6 578 return FALSE;
c434dee6
AJ
579
580 htab = elf64_x86_64_hash_table (info);
581 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
582 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
583 if (!htab->sgot || !htab->sgotplt)
584 abort ();
585
3496cb2a
L
586 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
587 (SEC_ALLOC | SEC_LOAD
588 | SEC_HAS_CONTENTS
589 | SEC_IN_MEMORY
590 | SEC_LINKER_CREATED
591 | SEC_READONLY));
c434dee6 592 if (htab->srelgot == NULL
c434dee6 593 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
b34976b6
AM
594 return FALSE;
595 return TRUE;
c434dee6
AJ
596}
597
598/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
599 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
600 hash table. */
601
b34976b6 602static bfd_boolean
27482721 603elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
c434dee6
AJ
604{
605 struct elf64_x86_64_link_hash_table *htab;
606
607 htab = elf64_x86_64_hash_table (info);
608 if (!htab->sgot && !create_got_section (dynobj, info))
b34976b6 609 return FALSE;
c434dee6
AJ
610
611 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 612 return FALSE;
c434dee6
AJ
613
614 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
615 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
616 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
617 if (!info->shared)
618 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
619
620 if (!htab->splt || !htab->srelplt || !htab->sdynbss
621 || (!info->shared && !htab->srelbss))
622 abort ();
623
b34976b6 624 return TRUE;
c434dee6
AJ
625}
626
627/* Copy the extra info we tack onto an elf_link_hash_entry. */
628
629static void
fcfa13d2 630elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
27482721
AJ
631 struct elf_link_hash_entry *dir,
632 struct elf_link_hash_entry *ind)
c434dee6
AJ
633{
634 struct elf64_x86_64_link_hash_entry *edir, *eind;
635
636 edir = (struct elf64_x86_64_link_hash_entry *) dir;
637 eind = (struct elf64_x86_64_link_hash_entry *) ind;
638
639 if (eind->dyn_relocs != NULL)
640 {
641 if (edir->dyn_relocs != NULL)
642 {
643 struct elf64_x86_64_dyn_relocs **pp;
644 struct elf64_x86_64_dyn_relocs *p;
645
fcfa13d2 646 /* Add reloc counts against the indirect sym to the direct sym
c434dee6
AJ
647 list. Merge any entries against the same section. */
648 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
649 {
650 struct elf64_x86_64_dyn_relocs *q;
651
652 for (q = edir->dyn_relocs; q != NULL; q = q->next)
653 if (q->sec == p->sec)
654 {
655 q->pc_count += p->pc_count;
656 q->count += p->count;
657 *pp = p->next;
658 break;
659 }
660 if (q == NULL)
661 pp = &p->next;
662 }
663 *pp = edir->dyn_relocs;
664 }
665
666 edir->dyn_relocs = eind->dyn_relocs;
667 eind->dyn_relocs = NULL;
668 }
669
bffbf940
JJ
670 if (ind->root.type == bfd_link_hash_indirect
671 && dir->got.refcount <= 0)
672 {
673 edir->tls_type = eind->tls_type;
674 eind->tls_type = GOT_UNKNOWN;
675 }
676
d40d037c
AJ
677 if (ELIMINATE_COPY_RELOCS
678 && ind->root.type != bfd_link_hash_indirect
f5385ebf
AM
679 && dir->dynamic_adjusted)
680 {
681 /* If called to transfer flags for a weakdef during processing
682 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
683 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
684 dir->ref_dynamic |= ind->ref_dynamic;
685 dir->ref_regular |= ind->ref_regular;
686 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
687 dir->needs_plt |= ind->needs_plt;
688 dir->pointer_equality_needed |= ind->pointer_equality_needed;
689 }
d40d037c 690 else
fcfa13d2 691 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
8d88c4ca
NC
692}
693
b34976b6 694static bfd_boolean
27482721 695elf64_x86_64_mkobject (bfd *abfd)
8d88c4ca 696{
bffbf940 697 if (abfd->tdata.any == NULL)
62d7a5f6
AM
698 {
699 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
700 abfd->tdata.any = bfd_zalloc (abfd, amt);
701 if (abfd->tdata.any == NULL)
702 return FALSE;
703 }
704 return bfd_elf_mkobject (abfd);
bffbf940
JJ
705}
706
b34976b6 707static bfd_boolean
27482721 708elf64_x86_64_elf_object_p (bfd *abfd)
bffbf940 709{
8d88c4ca
NC
710 /* Set the right machine number for an x86-64 elf64 file. */
711 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
b34976b6 712 return TRUE;
8d88c4ca
NC
713}
714
bffbf940 715static int
3bfa9139 716elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type,
2f3bf80a 717 struct elf_link_hash_entry *h)
bffbf940
JJ
718{
719 if (info->shared)
720 return r_type;
721
722 switch (r_type)
723 {
724 case R_X86_64_TLSGD:
67a4f2b7
AO
725 case R_X86_64_GOTPC32_TLSDESC:
726 case R_X86_64_TLSDESC_CALL:
bffbf940 727 case R_X86_64_GOTTPOFF:
2f3bf80a 728 if (h == NULL)
bffbf940
JJ
729 return R_X86_64_TPOFF32;
730 return R_X86_64_GOTTPOFF;
731 case R_X86_64_TLSLD:
732 return R_X86_64_TPOFF32;
733 }
734
735 return r_type;
736}
737
70256ad8 738/* Look through the relocs for a section during the first phase, and
c434dee6
AJ
739 calculate needed space in the global offset table, procedure
740 linkage table, and dynamic reloc sections. */
70256ad8 741
b34976b6 742static bfd_boolean
27482721
AJ
743elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
744 const Elf_Internal_Rela *relocs)
70256ad8 745{
c434dee6 746 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
747 Elf_Internal_Shdr *symtab_hdr;
748 struct elf_link_hash_entry **sym_hashes;
70256ad8
AJ
749 const Elf_Internal_Rela *rel;
750 const Elf_Internal_Rela *rel_end;
70256ad8
AJ
751 asection *sreloc;
752
1049f94e 753 if (info->relocatable)
b34976b6 754 return TRUE;
70256ad8 755
c434dee6 756 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
757 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
758 sym_hashes = elf_sym_hashes (abfd);
70256ad8 759
c434dee6
AJ
760 sreloc = NULL;
761
70256ad8
AJ
762 rel_end = relocs + sec->reloc_count;
763 for (rel = relocs; rel < rel_end; rel++)
764 {
bffbf940 765 unsigned int r_type;
70256ad8
AJ
766 unsigned long r_symndx;
767 struct elf_link_hash_entry *h;
768
769 r_symndx = ELF64_R_SYM (rel->r_info);
bffbf940 770 r_type = ELF64_R_TYPE (rel->r_info);
c434dee6
AJ
771
772 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
773 {
d003868e
AM
774 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
775 abfd, r_symndx);
b34976b6 776 return FALSE;
c434dee6
AJ
777 }
778
70256ad8
AJ
779 if (r_symndx < symtab_hdr->sh_info)
780 h = NULL;
781 else
71cb9464
L
782 {
783 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
784 while (h->root.type == bfd_link_hash_indirect
785 || h->root.type == bfd_link_hash_warning)
786 h = (struct elf_link_hash_entry *) h->root.u.i.link;
787 }
70256ad8 788
2f3bf80a 789 r_type = elf64_x86_64_tls_transition (info, r_type, h);
bffbf940 790 switch (r_type)
70256ad8 791 {
bffbf940
JJ
792 case R_X86_64_TLSLD:
793 htab->tls_ld_got.refcount += 1;
794 goto create_got;
795
796 case R_X86_64_TPOFF32:
797 if (info->shared)
70256ad8 798 {
bffbf940 799 (*_bfd_error_handler)
d003868e
AM
800 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
801 abfd,
6610a52d
L
802 x86_64_elf_howto_table[r_type].name,
803 (h) ? h->root.root.string : "a local symbol");
bffbf940 804 bfd_set_error (bfd_error_bad_value);
b34976b6 805 return FALSE;
70256ad8 806 }
bffbf940 807 break;
c434dee6 808
bffbf940
JJ
809 case R_X86_64_GOTTPOFF:
810 if (info->shared)
811 info->flags |= DF_STATIC_TLS;
812 /* Fall through */
70256ad8 813
bffbf940
JJ
814 case R_X86_64_GOT32:
815 case R_X86_64_GOTPCREL:
816 case R_X86_64_TLSGD:
7b81dfbb
AJ
817 case R_X86_64_GOT64:
818 case R_X86_64_GOTPCREL64:
819 case R_X86_64_GOTPLT64:
67a4f2b7
AO
820 case R_X86_64_GOTPC32_TLSDESC:
821 case R_X86_64_TLSDESC_CALL:
bffbf940
JJ
822 /* This symbol requires a global offset table entry. */
823 {
824 int tls_type, old_tls_type;
825
826 switch (r_type)
827 {
828 default: tls_type = GOT_NORMAL; break;
829 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
830 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
67a4f2b7
AO
831 case R_X86_64_GOTPC32_TLSDESC:
832 case R_X86_64_TLSDESC_CALL:
833 tls_type = GOT_TLS_GDESC; break;
bffbf940
JJ
834 }
835
836 if (h != NULL)
837 {
7b81dfbb
AJ
838 if (r_type == R_X86_64_GOTPLT64)
839 {
840 /* This relocation indicates that we also need
841 a PLT entry, as this is a function. We don't need
842 a PLT entry for local symbols. */
843 h->needs_plt = 1;
844 h->plt.refcount += 1;
845 }
bffbf940
JJ
846 h->got.refcount += 1;
847 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
848 }
849 else
850 {
851 bfd_signed_vma *local_got_refcounts;
852
853 /* This is a global offset table entry for a local symbol. */
854 local_got_refcounts = elf_local_got_refcounts (abfd);
855 if (local_got_refcounts == NULL)
856 {
857 bfd_size_type size;
858
859 size = symtab_hdr->sh_info;
67a4f2b7
AO
860 size *= sizeof (bfd_signed_vma)
861 + sizeof (bfd_vma) + sizeof (char);
bffbf940
JJ
862 local_got_refcounts = ((bfd_signed_vma *)
863 bfd_zalloc (abfd, size));
864 if (local_got_refcounts == NULL)
b34976b6 865 return FALSE;
bffbf940 866 elf_local_got_refcounts (abfd) = local_got_refcounts;
67a4f2b7
AO
867 elf64_x86_64_local_tlsdesc_gotent (abfd)
868 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
bffbf940 869 elf64_x86_64_local_got_tls_type (abfd)
67a4f2b7 870 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
bffbf940
JJ
871 }
872 local_got_refcounts[r_symndx] += 1;
873 old_tls_type
874 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
875 }
876
877 /* If a TLS symbol is accessed using IE at least once,
878 there is no point to use dynamic model for it. */
879 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
67a4f2b7
AO
880 && (! GOT_TLS_GD_ANY_P (old_tls_type)
881 || tls_type != GOT_TLS_IE))
bffbf940 882 {
67a4f2b7 883 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
bffbf940 884 tls_type = old_tls_type;
67a4f2b7
AO
885 else if (GOT_TLS_GD_ANY_P (old_tls_type)
886 && GOT_TLS_GD_ANY_P (tls_type))
887 tls_type |= old_tls_type;
bffbf940
JJ
888 else
889 {
890 (*_bfd_error_handler)
d003868e
AM
891 (_("%B: %s' accessed both as normal and thread local symbol"),
892 abfd, h ? h->root.root.string : "<local>");
b34976b6 893 return FALSE;
bffbf940
JJ
894 }
895 }
896
897 if (old_tls_type != tls_type)
898 {
899 if (h != NULL)
900 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
901 else
902 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
903 }
904 }
c434dee6
AJ
905 /* Fall through */
906
d6ab8113
JB
907 case R_X86_64_GOTOFF64:
908 case R_X86_64_GOTPC32:
7b81dfbb 909 case R_X86_64_GOTPC64:
bffbf940 910 create_got:
c434dee6
AJ
911 if (htab->sgot == NULL)
912 {
913 if (htab->elf.dynobj == NULL)
914 htab->elf.dynobj = abfd;
915 if (!create_got_section (htab->elf.dynobj, info))
b34976b6 916 return FALSE;
c434dee6 917 }
70256ad8
AJ
918 break;
919
920 case R_X86_64_PLT32:
921 /* This symbol requires a procedure linkage table entry. We
407443a3
AJ
922 actually build the entry in adjust_dynamic_symbol,
923 because this might be a case of linking PIC code which is
924 never referenced by a dynamic object, in which case we
925 don't need to generate a procedure linkage table entry
926 after all. */
70256ad8
AJ
927
928 /* If this is a local symbol, we resolve it directly without
407443a3 929 creating a procedure linkage table entry. */
70256ad8
AJ
930 if (h == NULL)
931 continue;
932
f5385ebf 933 h->needs_plt = 1;
51b64d56 934 h->plt.refcount += 1;
70256ad8
AJ
935 break;
936
7b81dfbb
AJ
937 case R_X86_64_PLTOFF64:
938 /* This tries to form the 'address' of a function relative
939 to GOT. For global symbols we need a PLT entry. */
940 if (h != NULL)
941 {
942 h->needs_plt = 1;
943 h->plt.refcount += 1;
944 }
945 goto create_got;
946
cc78d0af
AJ
947 case R_X86_64_8:
948 case R_X86_64_16:
70256ad8
AJ
949 case R_X86_64_32:
950 case R_X86_64_32S:
1b71fb54
AJ
951 /* Let's help debug shared library creation. These relocs
952 cannot be used in shared libs. Don't error out for
953 sections we don't care about, such as debug sections or
954 non-constant sections. */
955 if (info->shared
956 && (sec->flags & SEC_ALLOC) != 0
957 && (sec->flags & SEC_READONLY) != 0)
958 {
959 (*_bfd_error_handler)
d003868e
AM
960 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
961 abfd,
6610a52d
L
962 x86_64_elf_howto_table[r_type].name,
963 (h) ? h->root.root.string : "a local symbol");
1b71fb54 964 bfd_set_error (bfd_error_bad_value);
b34976b6 965 return FALSE;
1b71fb54
AJ
966 }
967 /* Fall through. */
968
c434dee6
AJ
969 case R_X86_64_PC8:
970 case R_X86_64_PC16:
70256ad8 971 case R_X86_64_PC32:
d6ab8113 972 case R_X86_64_PC64:
1b71fb54 973 case R_X86_64_64:
c434dee6
AJ
974 if (h != NULL && !info->shared)
975 {
976 /* If this reloc is in a read-only section, we might
977 need a copy reloc. We can't check reliably at this
978 stage whether the section is read-only, as input
979 sections have not yet been mapped to output sections.
980 Tentatively set the flag for now, and correct in
981 adjust_dynamic_symbol. */
f5385ebf 982 h->non_got_ref = 1;
c434dee6
AJ
983
984 /* We may need a .plt entry if the function this reloc
985 refers to is in a shared lib. */
986 h->plt.refcount += 1;
d6ab8113 987 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
f5385ebf 988 h->pointer_equality_needed = 1;
c434dee6 989 }
70256ad8
AJ
990
991 /* If we are creating a shared library, and this is a reloc
992 against a global symbol, or a non PC relative reloc
993 against a local symbol, then we need to copy the reloc
994 into the shared library. However, if we are linking with
995 -Bsymbolic, we do not need to copy a reloc against a
996 global symbol which is defined in an object we are
407443a3 997 including in the link (i.e., DEF_REGULAR is set). At
70256ad8
AJ
998 this point we have not seen all the input files, so it is
999 possible that DEF_REGULAR is not set now but will be set
c434dee6
AJ
1000 later (it is never cleared). In case of a weak definition,
1001 DEF_REGULAR may be cleared later by a strong definition in
1002 a shared library. We account for that possibility below by
1003 storing information in the relocs_copied field of the hash
1004 table entry. A similar situation occurs when creating
1005 shared libraries and symbol visibility changes render the
1006 symbol local.
1007
1008 If on the other hand, we are creating an executable, we
1009 may need to keep relocations for symbols satisfied by a
1010 dynamic library if we manage to avoid copy relocs for the
1011 symbol. */
1012 if ((info->shared
1013 && (sec->flags & SEC_ALLOC) != 0
bffbf940
JJ
1014 && (((r_type != R_X86_64_PC8)
1015 && (r_type != R_X86_64_PC16)
d6ab8113
JB
1016 && (r_type != R_X86_64_PC32)
1017 && (r_type != R_X86_64_PC64))
c434dee6 1018 || (h != NULL
55255dae 1019 && (! SYMBOLIC_BIND (info, h)
c434dee6 1020 || h->root.type == bfd_link_hash_defweak
f5385ebf 1021 || !h->def_regular))))
d40d037c
AJ
1022 || (ELIMINATE_COPY_RELOCS
1023 && !info->shared
c434dee6
AJ
1024 && (sec->flags & SEC_ALLOC) != 0
1025 && h != NULL
1026 && (h->root.type == bfd_link_hash_defweak
f5385ebf 1027 || !h->def_regular)))
70256ad8 1028 {
c434dee6
AJ
1029 struct elf64_x86_64_dyn_relocs *p;
1030 struct elf64_x86_64_dyn_relocs **head;
1031
1032 /* We must copy these reloc types into the output file.
1033 Create a reloc section in dynobj and make room for
1034 this reloc. */
70256ad8
AJ
1035 if (sreloc == NULL)
1036 {
1037 const char *name;
c434dee6 1038 bfd *dynobj;
70256ad8
AJ
1039
1040 name = (bfd_elf_string_from_elf_section
1041 (abfd,
1042 elf_elfheader (abfd)->e_shstrndx,
1043 elf_section_data (sec)->rel_hdr.sh_name));
1044 if (name == NULL)
b34976b6 1045 return FALSE;
70256ad8 1046
0112cd26 1047 if (! CONST_STRNEQ (name, ".rela")
c434dee6
AJ
1048 || strcmp (bfd_get_section_name (abfd, sec),
1049 name + 5) != 0)
1050 {
1051 (*_bfd_error_handler)
d003868e
AM
1052 (_("%B: bad relocation section name `%s\'"),
1053 abfd, name);
c434dee6
AJ
1054 }
1055
1056 if (htab->elf.dynobj == NULL)
1057 htab->elf.dynobj = abfd;
1058
1059 dynobj = htab->elf.dynobj;
70256ad8
AJ
1060
1061 sreloc = bfd_get_section_by_name (dynobj, name);
1062 if (sreloc == NULL)
1063 {
1064 flagword flags;
1065
70256ad8
AJ
1066 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1067 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1068 if ((sec->flags & SEC_ALLOC) != 0)
1069 flags |= SEC_ALLOC | SEC_LOAD;
3496cb2a
L
1070 sreloc = bfd_make_section_with_flags (dynobj,
1071 name,
1072 flags);
70256ad8 1073 if (sreloc == NULL
cc78d0af 1074 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
b34976b6 1075 return FALSE;
70256ad8 1076 }
c434dee6 1077 elf_section_data (sec)->sreloc = sreloc;
70256ad8
AJ
1078 }
1079
c434dee6
AJ
1080 /* If this is a global symbol, we count the number of
1081 relocations we need for this symbol. */
1082 if (h != NULL)
70256ad8 1083 {
c434dee6
AJ
1084 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1085 }
1086 else
1087 {
e81d3500 1088 void **vpp;
c434dee6
AJ
1089 /* Track dynamic relocs needed for local syms too.
1090 We really need local syms available to do this
1091 easily. Oh well. */
1092
1093 asection *s;
1094 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1095 sec, r_symndx);
1096 if (s == NULL)
b34976b6 1097 return FALSE;
70256ad8 1098
e81d3500
DD
1099 /* Beware of type punned pointers vs strict aliasing
1100 rules. */
1101 vpp = &(elf_section_data (s)->local_dynrel);
1102 head = (struct elf64_x86_64_dyn_relocs **)vpp;
c434dee6 1103 }
70256ad8 1104
c434dee6
AJ
1105 p = *head;
1106 if (p == NULL || p->sec != sec)
1107 {
1108 bfd_size_type amt = sizeof *p;
1109 p = ((struct elf64_x86_64_dyn_relocs *)
1110 bfd_alloc (htab->elf.dynobj, amt));
70256ad8 1111 if (p == NULL)
b34976b6 1112 return FALSE;
c434dee6
AJ
1113 p->next = *head;
1114 *head = p;
1115 p->sec = sec;
1116 p->count = 0;
1117 p->pc_count = 0;
70256ad8 1118 }
c434dee6
AJ
1119
1120 p->count += 1;
bffbf940
JJ
1121 if (r_type == R_X86_64_PC8
1122 || r_type == R_X86_64_PC16
d6ab8113
JB
1123 || r_type == R_X86_64_PC32
1124 || r_type == R_X86_64_PC64)
c434dee6 1125 p->pc_count += 1;
70256ad8
AJ
1126 }
1127 break;
fe4770f4
AJ
1128
1129 /* This relocation describes the C++ object vtable hierarchy.
1130 Reconstruct it for later use during GC. */
1131 case R_X86_64_GNU_VTINHERIT:
c152c796 1132 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 1133 return FALSE;
fe4770f4
AJ
1134 break;
1135
1136 /* This relocation describes which C++ vtable entries are actually
1137 used. Record for later use during GC. */
1138 case R_X86_64_GNU_VTENTRY:
c152c796 1139 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 1140 return FALSE;
fe4770f4 1141 break;
c434dee6
AJ
1142
1143 default:
1144 break;
70256ad8
AJ
1145 }
1146 }
1147
b34976b6 1148 return TRUE;
70256ad8
AJ
1149}
1150
1151/* Return the section that should be marked against GC for a given
407443a3 1152 relocation. */
70256ad8
AJ
1153
1154static asection *
27482721 1155elf64_x86_64_gc_mark_hook (asection *sec,
07adf181 1156 struct bfd_link_info *info,
27482721
AJ
1157 Elf_Internal_Rela *rel,
1158 struct elf_link_hash_entry *h,
1159 Elf_Internal_Sym *sym)
70256ad8
AJ
1160{
1161 if (h != NULL)
07adf181
AM
1162 switch (ELF64_R_TYPE (rel->r_info))
1163 {
1164 case R_X86_64_GNU_VTINHERIT:
1165 case R_X86_64_GNU_VTENTRY:
1166 return NULL;
1167 }
1168
1169 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
70256ad8
AJ
1170}
1171
407443a3 1172/* Update the got entry reference counts for the section being removed. */
70256ad8 1173
b34976b6 1174static bfd_boolean
27482721
AJ
1175elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1176 asection *sec, const Elf_Internal_Rela *relocs)
70256ad8
AJ
1177{
1178 Elf_Internal_Shdr *symtab_hdr;
1179 struct elf_link_hash_entry **sym_hashes;
1180 bfd_signed_vma *local_got_refcounts;
1181 const Elf_Internal_Rela *rel, *relend;
c434dee6
AJ
1182
1183 elf_section_data (sec)->local_dynrel = NULL;
70256ad8
AJ
1184
1185 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1186 sym_hashes = elf_sym_hashes (abfd);
1187 local_got_refcounts = elf_local_got_refcounts (abfd);
1188
70256ad8
AJ
1189 relend = relocs + sec->reloc_count;
1190 for (rel = relocs; rel < relend; rel++)
26e41594
AM
1191 {
1192 unsigned long r_symndx;
1193 unsigned int r_type;
1194 struct elf_link_hash_entry *h = NULL;
70256ad8 1195
26e41594
AM
1196 r_symndx = ELF64_R_SYM (rel->r_info);
1197 if (r_symndx >= symtab_hdr->sh_info)
1198 {
1199 struct elf64_x86_64_link_hash_entry *eh;
1200 struct elf64_x86_64_dyn_relocs **pp;
1201 struct elf64_x86_64_dyn_relocs *p;
c434dee6 1202
26e41594 1203 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
1204 while (h->root.type == bfd_link_hash_indirect
1205 || h->root.type == bfd_link_hash_warning)
1206 h = (struct elf_link_hash_entry *) h->root.u.i.link;
26e41594 1207 eh = (struct elf64_x86_64_link_hash_entry *) h;
c434dee6 1208
26e41594
AM
1209 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1210 if (p->sec == sec)
1211 {
1212 /* Everything must go for SEC. */
1213 *pp = p->next;
1214 break;
1215 }
1216 }
c434dee6 1217
26e41594 1218 r_type = ELF64_R_TYPE (rel->r_info);
2f3bf80a 1219 r_type = elf64_x86_64_tls_transition (info, r_type, h);
26e41594
AM
1220 switch (r_type)
1221 {
1222 case R_X86_64_TLSLD:
1223 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1224 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1225 break;
c434dee6 1226
26e41594 1227 case R_X86_64_TLSGD:
67a4f2b7
AO
1228 case R_X86_64_GOTPC32_TLSDESC:
1229 case R_X86_64_TLSDESC_CALL:
26e41594
AM
1230 case R_X86_64_GOTTPOFF:
1231 case R_X86_64_GOT32:
1232 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
1233 case R_X86_64_GOT64:
1234 case R_X86_64_GOTPCREL64:
1235 case R_X86_64_GOTPLT64:
26e41594
AM
1236 if (h != NULL)
1237 {
7b81dfbb
AJ
1238 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1239 h->plt.refcount -= 1;
26e41594
AM
1240 if (h->got.refcount > 0)
1241 h->got.refcount -= 1;
1242 }
1243 else if (local_got_refcounts != NULL)
1244 {
1245 if (local_got_refcounts[r_symndx] > 0)
1246 local_got_refcounts[r_symndx] -= 1;
1247 }
1248 break;
c434dee6 1249
26e41594
AM
1250 case R_X86_64_8:
1251 case R_X86_64_16:
1252 case R_X86_64_32:
1253 case R_X86_64_64:
1254 case R_X86_64_32S:
1255 case R_X86_64_PC8:
1256 case R_X86_64_PC16:
1257 case R_X86_64_PC32:
d6ab8113 1258 case R_X86_64_PC64:
26e41594
AM
1259 if (info->shared)
1260 break;
1261 /* Fall thru */
c434dee6 1262
26e41594 1263 case R_X86_64_PLT32:
7b81dfbb 1264 case R_X86_64_PLTOFF64:
26e41594
AM
1265 if (h != NULL)
1266 {
1267 if (h->plt.refcount > 0)
1268 h->plt.refcount -= 1;
1269 }
1270 break;
70256ad8 1271
26e41594
AM
1272 default:
1273 break;
1274 }
1275 }
70256ad8 1276
b34976b6 1277 return TRUE;
70256ad8
AJ
1278}
1279
1280/* Adjust a symbol defined by a dynamic object and referenced by a
1281 regular object. The current definition is in some section of the
1282 dynamic object, but we're not including those sections. We have to
1283 change the definition to something the rest of the link can
407443a3 1284 understand. */
70256ad8 1285
b34976b6 1286static bfd_boolean
27482721
AJ
1287elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1288 struct elf_link_hash_entry *h)
70256ad8 1289{
c434dee6 1290 struct elf64_x86_64_link_hash_table *htab;
70256ad8 1291 asection *s;
70256ad8 1292
70256ad8
AJ
1293 /* If this is a function, put it in the procedure linkage table. We
1294 will fill in the contents of the procedure linkage table later,
1295 when we know the address of the .got section. */
1296 if (h->type == STT_FUNC
f5385ebf 1297 || h->needs_plt)
70256ad8 1298 {
c434dee6 1299 if (h->plt.refcount <= 0
27482721
AJ
1300 || SYMBOL_CALLS_LOCAL (info, h)
1301 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1302 && h->root.type == bfd_link_hash_undefweak))
70256ad8 1303 {
70256ad8
AJ
1304 /* This case can occur if we saw a PLT32 reloc in an input
1305 file, but the symbol was never referred to by a dynamic
1306 object, or if all references were garbage collected. In
1307 such a case, we don't actually need to build a procedure
1308 linkage table, and we can just do a PC32 reloc instead. */
70256ad8 1309 h->plt.offset = (bfd_vma) -1;
f5385ebf 1310 h->needs_plt = 0;
70256ad8
AJ
1311 }
1312
b34976b6 1313 return TRUE;
70256ad8 1314 }
bbd7ec4a 1315 else
c434dee6
AJ
1316 /* It's possible that we incorrectly decided a .plt reloc was
1317 needed for an R_X86_64_PC32 reloc to a non-function sym in
1318 check_relocs. We can't decide accurately between function and
1319 non-function syms in check-relocs; Objects loaded later in
1320 the link may change h->type. So fix it now. */
bbd7ec4a 1321 h->plt.offset = (bfd_vma) -1;
70256ad8
AJ
1322
1323 /* If this is a weak symbol, and there is a real definition, the
1324 processor independent code will have arranged for us to see the
407443a3 1325 real definition first, and we can just use the same value. */
f6e332e6 1326 if (h->u.weakdef != NULL)
70256ad8 1327 {
f6e332e6
AM
1328 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1329 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1330 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1331 h->root.u.def.value = h->u.weakdef->root.u.def.value;
d40d037c 1332 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
f6e332e6 1333 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 1334 return TRUE;
70256ad8
AJ
1335 }
1336
1337 /* This is a reference to a symbol defined by a dynamic object which
407443a3 1338 is not a function. */
70256ad8
AJ
1339
1340 /* If we are creating a shared library, we must presume that the
1341 only references to the symbol are via the global offset table.
1342 For such cases we need not do anything here; the relocations will
407443a3 1343 be handled correctly by relocate_section. */
70256ad8 1344 if (info->shared)
b34976b6 1345 return TRUE;
70256ad8
AJ
1346
1347 /* If there are no references to this symbol that do not use the
1348 GOT, we don't need to generate a copy reloc. */
f5385ebf 1349 if (!h->non_got_ref)
b34976b6 1350 return TRUE;
70256ad8 1351
c434dee6
AJ
1352 /* If -z nocopyreloc was given, we won't generate them either. */
1353 if (info->nocopyreloc)
1354 {
f5385ebf 1355 h->non_got_ref = 0;
b34976b6 1356 return TRUE;
c434dee6
AJ
1357 }
1358
d40d037c 1359 if (ELIMINATE_COPY_RELOCS)
c434dee6 1360 {
d40d037c
AJ
1361 struct elf64_x86_64_link_hash_entry * eh;
1362 struct elf64_x86_64_dyn_relocs *p;
c434dee6 1363
d40d037c
AJ
1364 eh = (struct elf64_x86_64_link_hash_entry *) h;
1365 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1366 {
1367 s = p->sec->output_section;
1368 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1369 break;
1370 }
1371
1372 /* If we didn't find any dynamic relocs in read-only sections, then
1373 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1374 if (p == NULL)
1375 {
f5385ebf 1376 h->non_got_ref = 0;
d40d037c
AJ
1377 return TRUE;
1378 }
c434dee6
AJ
1379 }
1380
909272ee
AM
1381 if (h->size == 0)
1382 {
1383 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1384 h->root.root.string);
1385 return TRUE;
1386 }
1387
70256ad8 1388 /* We must allocate the symbol in our .dynbss section, which will
407443a3 1389 become part of the .bss section of the executable. There will be
70256ad8
AJ
1390 an entry for this symbol in the .dynsym section. The dynamic
1391 object will contain position independent code, so all references
1392 from the dynamic object to this symbol will go through the global
1393 offset table. The dynamic linker will use the .dynsym entry to
1394 determine the address it must put in the global offset table, so
1395 both the dynamic object and the regular object will refer to the
1396 same memory location for the variable. */
1397
c434dee6 1398 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
1399
1400 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1401 to copy the initial value out of the dynamic object and into the
cedb70c5 1402 runtime process image. */
70256ad8
AJ
1403 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1404 {
eea6121a 1405 htab->srelbss->size += sizeof (Elf64_External_Rela);
f5385ebf 1406 h->needs_copy = 1;
70256ad8
AJ
1407 }
1408
c434dee6 1409 s = htab->sdynbss;
70256ad8 1410
027297b7 1411 return _bfd_elf_adjust_dynamic_copy (h, s);
70256ad8
AJ
1412}
1413
c434dee6
AJ
1414/* Allocate space in .plt, .got and associated reloc sections for
1415 dynamic relocs. */
1416
b34976b6 1417static bfd_boolean
27482721 1418allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
1419{
1420 struct bfd_link_info *info;
1421 struct elf64_x86_64_link_hash_table *htab;
1422 struct elf64_x86_64_link_hash_entry *eh;
1423 struct elf64_x86_64_dyn_relocs *p;
1424
e92d460e 1425 if (h->root.type == bfd_link_hash_indirect)
b34976b6 1426 return TRUE;
c434dee6 1427
e92d460e
AM
1428 if (h->root.type == bfd_link_hash_warning)
1429 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1430
c434dee6
AJ
1431 info = (struct bfd_link_info *) inf;
1432 htab = elf64_x86_64_hash_table (info);
1433
1434 if (htab->elf.dynamic_sections_created
27482721 1435 && h->plt.refcount > 0)
c434dee6
AJ
1436 {
1437 /* Make sure this symbol is output as a dynamic symbol.
1438 Undefined weak syms won't yet be marked as dynamic. */
1439 if (h->dynindx == -1
f5385ebf 1440 && !h->forced_local)
c434dee6 1441 {
c152c796 1442 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1443 return FALSE;
c434dee6
AJ
1444 }
1445
27482721
AJ
1446 if (info->shared
1447 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
c434dee6
AJ
1448 {
1449 asection *s = htab->splt;
1450
1451 /* If this is the first .plt entry, make room for the special
1452 first entry. */
eea6121a
AM
1453 if (s->size == 0)
1454 s->size += PLT_ENTRY_SIZE;
c434dee6 1455
eea6121a 1456 h->plt.offset = s->size;
c434dee6
AJ
1457
1458 /* If this symbol is not defined in a regular file, and we are
1459 not generating a shared library, then set the symbol to this
1460 location in the .plt. This is required to make function
1461 pointers compare as equal between the normal executable and
1462 the shared library. */
1463 if (! info->shared
f5385ebf 1464 && !h->def_regular)
c434dee6
AJ
1465 {
1466 h->root.u.def.section = s;
1467 h->root.u.def.value = h->plt.offset;
1468 }
1469
1470 /* Make room for this entry. */
eea6121a 1471 s->size += PLT_ENTRY_SIZE;
c434dee6
AJ
1472
1473 /* We also need to make an entry in the .got.plt section, which
1474 will be placed in the .got section by the linker script. */
eea6121a 1475 htab->sgotplt->size += GOT_ENTRY_SIZE;
c434dee6
AJ
1476
1477 /* We also need to make an entry in the .rela.plt section. */
eea6121a 1478 htab->srelplt->size += sizeof (Elf64_External_Rela);
67a4f2b7 1479 htab->srelplt->reloc_count++;
c434dee6
AJ
1480 }
1481 else
1482 {
1483 h->plt.offset = (bfd_vma) -1;
f5385ebf 1484 h->needs_plt = 0;
c434dee6
AJ
1485 }
1486 }
1487 else
1488 {
1489 h->plt.offset = (bfd_vma) -1;
f5385ebf 1490 h->needs_plt = 0;
c434dee6
AJ
1491 }
1492
67a4f2b7
AO
1493 eh = (struct elf64_x86_64_link_hash_entry *) h;
1494 eh->tlsdesc_got = (bfd_vma) -1;
1495
bffbf940
JJ
1496 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1497 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1498 if (h->got.refcount > 0
1499 && !info->shared
1500 && h->dynindx == -1
1501 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1502 h->got.offset = (bfd_vma) -1;
1503 else if (h->got.refcount > 0)
c434dee6
AJ
1504 {
1505 asection *s;
b34976b6 1506 bfd_boolean dyn;
bffbf940 1507 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
c434dee6
AJ
1508
1509 /* Make sure this symbol is output as a dynamic symbol.
1510 Undefined weak syms won't yet be marked as dynamic. */
1511 if (h->dynindx == -1
f5385ebf 1512 && !h->forced_local)
c434dee6 1513 {
c152c796 1514 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1515 return FALSE;
c434dee6
AJ
1516 }
1517
67a4f2b7
AO
1518 if (GOT_TLS_GDESC_P (tls_type))
1519 {
1520 eh->tlsdesc_got = htab->sgotplt->size
1521 - elf64_x86_64_compute_jump_table_size (htab);
1522 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1523 h->got.offset = (bfd_vma) -2;
1524 }
1525 if (! GOT_TLS_GDESC_P (tls_type)
1526 || GOT_TLS_GD_P (tls_type))
1527 {
1528 s = htab->sgot;
1529 h->got.offset = s->size;
1530 s->size += GOT_ENTRY_SIZE;
1531 if (GOT_TLS_GD_P (tls_type))
1532 s->size += GOT_ENTRY_SIZE;
1533 }
c434dee6 1534 dyn = htab->elf.dynamic_sections_created;
bffbf940
JJ
1535 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1536 and two if global.
1537 R_X86_64_GOTTPOFF needs one dynamic relocation. */
67a4f2b7 1538 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
bffbf940 1539 || tls_type == GOT_TLS_IE)
eea6121a 1540 htab->srelgot->size += sizeof (Elf64_External_Rela);
67a4f2b7 1541 else if (GOT_TLS_GD_P (tls_type))
eea6121a 1542 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
67a4f2b7
AO
1543 else if (! GOT_TLS_GDESC_P (tls_type)
1544 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1545 || h->root.type != bfd_link_hash_undefweak)
27482721
AJ
1546 && (info->shared
1547 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
eea6121a 1548 htab->srelgot->size += sizeof (Elf64_External_Rela);
67a4f2b7
AO
1549 if (GOT_TLS_GDESC_P (tls_type))
1550 {
1551 htab->srelplt->size += sizeof (Elf64_External_Rela);
1552 htab->tlsdesc_plt = (bfd_vma) -1;
1553 }
c434dee6
AJ
1554 }
1555 else
1556 h->got.offset = (bfd_vma) -1;
1557
c434dee6 1558 if (eh->dyn_relocs == NULL)
b34976b6 1559 return TRUE;
c434dee6
AJ
1560
1561 /* In the shared -Bsymbolic case, discard space allocated for
1562 dynamic pc-relative relocs against symbols which turn out to be
1563 defined in regular objects. For the normal shared case, discard
1564 space for pc-relative relocs that have become local due to symbol
1565 visibility changes. */
1566
1567 if (info->shared)
1568 {
27482721
AJ
1569 /* Relocs that use pc_count are those that appear on a call
1570 insn, or certain REL relocs that can generated via assembly.
1571 We want calls to protected symbols to resolve directly to the
1572 function rather than going via the plt. If people want
1573 function pointer comparisons to work as expected then they
1574 should avoid writing weird assembly. */
1575 if (SYMBOL_CALLS_LOCAL (info, h))
c434dee6
AJ
1576 {
1577 struct elf64_x86_64_dyn_relocs **pp;
1578
1579 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1580 {
1581 p->count -= p->pc_count;
1582 p->pc_count = 0;
1583 if (p->count == 0)
1584 *pp = p->next;
1585 else
1586 pp = &p->next;
1587 }
1588 }
4e795f50
AM
1589
1590 /* Also discard relocs on undefined weak syms with non-default
1591 visibility. */
22d606e9 1592 if (eh->dyn_relocs != NULL
4e795f50 1593 && h->root.type == bfd_link_hash_undefweak)
22d606e9
AM
1594 {
1595 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1596 eh->dyn_relocs = NULL;
1597
1598 /* Make sure undefined weak symbols are output as a dynamic
1599 symbol in PIEs. */
1600 else if (h->dynindx == -1
1601 && !h->forced_local)
1602 {
1603 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1604 return FALSE;
1605 }
1606 }
c434dee6 1607 }
d40d037c 1608 else if (ELIMINATE_COPY_RELOCS)
c434dee6
AJ
1609 {
1610 /* For the non-shared case, discard space for relocs against
1611 symbols which turn out to need copy relocs or are not
1612 dynamic. */
1613
f5385ebf
AM
1614 if (!h->non_got_ref
1615 && ((h->def_dynamic
1616 && !h->def_regular)
c434dee6
AJ
1617 || (htab->elf.dynamic_sections_created
1618 && (h->root.type == bfd_link_hash_undefweak
1619 || h->root.type == bfd_link_hash_undefined))))
1620 {
1621 /* Make sure this symbol is output as a dynamic symbol.
1622 Undefined weak syms won't yet be marked as dynamic. */
1623 if (h->dynindx == -1
f5385ebf 1624 && !h->forced_local)
c434dee6 1625 {
c152c796 1626 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 1627 return FALSE;
c434dee6
AJ
1628 }
1629
1630 /* If that succeeded, we know we'll be keeping all the
1631 relocs. */
1632 if (h->dynindx != -1)
1633 goto keep;
1634 }
1635
1636 eh->dyn_relocs = NULL;
1637
1638 keep: ;
1639 }
1640
1641 /* Finally, allocate space. */
1642 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1643 {
1644 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 1645 sreloc->size += p->count * sizeof (Elf64_External_Rela);
c434dee6
AJ
1646 }
1647
b34976b6 1648 return TRUE;
c434dee6
AJ
1649}
1650
1651/* Find any dynamic relocs that apply to read-only sections. */
1652
b34976b6 1653static bfd_boolean
27482721 1654readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
c434dee6
AJ
1655{
1656 struct elf64_x86_64_link_hash_entry *eh;
1657 struct elf64_x86_64_dyn_relocs *p;
1658
e92d460e
AM
1659 if (h->root.type == bfd_link_hash_warning)
1660 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1661
c434dee6
AJ
1662 eh = (struct elf64_x86_64_link_hash_entry *) h;
1663 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1664 {
1665 asection *s = p->sec->output_section;
1666
1667 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1668 {
1669 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1670
1671 info->flags |= DF_TEXTREL;
1672
1673 /* Not an error, just cut short the traversal. */
b34976b6 1674 return FALSE;
c434dee6
AJ
1675 }
1676 }
b34976b6 1677 return TRUE;
c434dee6
AJ
1678}
1679
70256ad8
AJ
1680/* Set the sizes of the dynamic sections. */
1681
b34976b6 1682static bfd_boolean
27482721
AJ
1683elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1684 struct bfd_link_info *info)
70256ad8 1685{
c434dee6 1686 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
1687 bfd *dynobj;
1688 asection *s;
b34976b6 1689 bfd_boolean relocs;
c434dee6 1690 bfd *ibfd;
70256ad8 1691
c434dee6
AJ
1692 htab = elf64_x86_64_hash_table (info);
1693 dynobj = htab->elf.dynobj;
1694 if (dynobj == NULL)
1695 abort ();
70256ad8 1696
c434dee6 1697 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1698 {
1699 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 1700 if (info->executable)
70256ad8
AJ
1701 {
1702 s = bfd_get_section_by_name (dynobj, ".interp");
c434dee6
AJ
1703 if (s == NULL)
1704 abort ();
eea6121a 1705 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
70256ad8
AJ
1706 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1707 }
1708 }
70256ad8 1709
c434dee6
AJ
1710 /* Set up .got offsets for local syms, and space for local dynamic
1711 relocs. */
1712 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
70256ad8 1713 {
c434dee6
AJ
1714 bfd_signed_vma *local_got;
1715 bfd_signed_vma *end_local_got;
bffbf940 1716 char *local_tls_type;
67a4f2b7 1717 bfd_vma *local_tlsdesc_gotent;
c434dee6
AJ
1718 bfd_size_type locsymcount;
1719 Elf_Internal_Shdr *symtab_hdr;
1720 asection *srel;
70256ad8 1721
c434dee6 1722 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
70256ad8
AJ
1723 continue;
1724
c434dee6 1725 for (s = ibfd->sections; s != NULL; s = s->next)
70256ad8 1726 {
c434dee6
AJ
1727 struct elf64_x86_64_dyn_relocs *p;
1728
e81d3500
DD
1729 for (p = (struct elf64_x86_64_dyn_relocs *)
1730 (elf_section_data (s)->local_dynrel);
c434dee6
AJ
1731 p != NULL;
1732 p = p->next)
70256ad8 1733 {
c434dee6
AJ
1734 if (!bfd_is_abs_section (p->sec)
1735 && bfd_is_abs_section (p->sec->output_section))
1736 {
1737 /* Input section has been discarded, either because
1738 it is a copy of a linkonce section or due to
1739 linker script /DISCARD/, so we'll be discarding
1740 the relocs too. */
1741 }
1742 else if (p->count != 0)
1743 {
1744 srel = elf_section_data (p->sec)->sreloc;
eea6121a 1745 srel->size += p->count * sizeof (Elf64_External_Rela);
c434dee6
AJ
1746 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1747 info->flags |= DF_TEXTREL;
1748
1749 }
70256ad8
AJ
1750 }
1751 }
c434dee6
AJ
1752
1753 local_got = elf_local_got_refcounts (ibfd);
1754 if (!local_got)
1755 continue;
1756
1757 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1758 locsymcount = symtab_hdr->sh_info;
1759 end_local_got = local_got + locsymcount;
bffbf940 1760 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
67a4f2b7 1761 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
c434dee6
AJ
1762 s = htab->sgot;
1763 srel = htab->srelgot;
67a4f2b7
AO
1764 for (; local_got < end_local_got;
1765 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
70256ad8 1766 {
67a4f2b7 1767 *local_tlsdesc_gotent = (bfd_vma) -1;
c434dee6 1768 if (*local_got > 0)
70256ad8 1769 {
67a4f2b7
AO
1770 if (GOT_TLS_GDESC_P (*local_tls_type))
1771 {
1772 *local_tlsdesc_gotent = htab->sgotplt->size
1773 - elf64_x86_64_compute_jump_table_size (htab);
1774 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1775 *local_got = (bfd_vma) -2;
1776 }
1777 if (! GOT_TLS_GDESC_P (*local_tls_type)
1778 || GOT_TLS_GD_P (*local_tls_type))
1779 {
1780 *local_got = s->size;
1781 s->size += GOT_ENTRY_SIZE;
1782 if (GOT_TLS_GD_P (*local_tls_type))
1783 s->size += GOT_ENTRY_SIZE;
1784 }
bffbf940 1785 if (info->shared
67a4f2b7 1786 || GOT_TLS_GD_ANY_P (*local_tls_type)
bffbf940 1787 || *local_tls_type == GOT_TLS_IE)
67a4f2b7
AO
1788 {
1789 if (GOT_TLS_GDESC_P (*local_tls_type))
1790 {
1791 htab->srelplt->size += sizeof (Elf64_External_Rela);
1792 htab->tlsdesc_plt = (bfd_vma) -1;
1793 }
1794 if (! GOT_TLS_GDESC_P (*local_tls_type)
1795 || GOT_TLS_GD_P (*local_tls_type))
1796 srel->size += sizeof (Elf64_External_Rela);
1797 }
70256ad8
AJ
1798 }
1799 else
c434dee6
AJ
1800 *local_got = (bfd_vma) -1;
1801 }
1802 }
70256ad8 1803
bffbf940
JJ
1804 if (htab->tls_ld_got.refcount > 0)
1805 {
1806 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1807 relocs. */
eea6121a
AM
1808 htab->tls_ld_got.offset = htab->sgot->size;
1809 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
1810 htab->srelgot->size += sizeof (Elf64_External_Rela);
bffbf940
JJ
1811 }
1812 else
1813 htab->tls_ld_got.offset = -1;
1814
c434dee6
AJ
1815 /* Allocate global sym .plt and .got entries, and space for global
1816 sym dynamic relocs. */
1817 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1818
67a4f2b7
AO
1819 /* For every jump slot reserved in the sgotplt, reloc_count is
1820 incremented. However, when we reserve space for TLS descriptors,
1821 it's not incremented, so in order to compute the space reserved
1822 for them, it suffices to multiply the reloc count by the jump
1823 slot size. */
1824 if (htab->srelplt)
1825 htab->sgotplt_jump_table_size
1826 = elf64_x86_64_compute_jump_table_size (htab);
1827
1828 if (htab->tlsdesc_plt)
1829 {
1830 /* If we're not using lazy TLS relocations, don't generate the
1831 PLT and GOT entries they require. */
1832 if ((info->flags & DF_BIND_NOW))
1833 htab->tlsdesc_plt = 0;
1834 else
1835 {
1836 htab->tlsdesc_got = htab->sgot->size;
1837 htab->sgot->size += GOT_ENTRY_SIZE;
1838 /* Reserve room for the initial entry.
1839 FIXME: we could probably do away with it in this case. */
1840 if (htab->splt->size == 0)
1841 htab->splt->size += PLT_ENTRY_SIZE;
1842 htab->tlsdesc_plt = htab->splt->size;
1843 htab->splt->size += PLT_ENTRY_SIZE;
1844 }
1845 }
1846
c434dee6
AJ
1847 /* We now have determined the sizes of the various dynamic sections.
1848 Allocate memory for them. */
b34976b6 1849 relocs = FALSE;
c434dee6
AJ
1850 for (s = dynobj->sections; s != NULL; s = s->next)
1851 {
1852 if ((s->flags & SEC_LINKER_CREATED) == 0)
1853 continue;
1854
1855 if (s == htab->splt
1856 || s == htab->sgot
75ff4589
L
1857 || s == htab->sgotplt
1858 || s == htab->sdynbss)
c434dee6
AJ
1859 {
1860 /* Strip this section if we don't need it; see the
1861 comment below. */
1862 }
0112cd26 1863 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
c434dee6 1864 {
eea6121a 1865 if (s->size != 0 && s != htab->srelplt)
b34976b6 1866 relocs = TRUE;
c434dee6
AJ
1867
1868 /* We use the reloc_count field as a counter if we need
1869 to copy relocs into the output file. */
67a4f2b7
AO
1870 if (s != htab->srelplt)
1871 s->reloc_count = 0;
70256ad8 1872 }
c434dee6 1873 else
70256ad8
AJ
1874 {
1875 /* It's not one of our sections, so don't allocate space. */
1876 continue;
1877 }
1878
eea6121a 1879 if (s->size == 0)
70256ad8 1880 {
c434dee6
AJ
1881 /* If we don't need this section, strip it from the
1882 output file. This is mostly to handle .rela.bss and
1883 .rela.plt. We must create both sections in
1884 create_dynamic_sections, because they must be created
1885 before the linker maps input sections to output
1886 sections. The linker does that before
1887 adjust_dynamic_symbol is called, and it is that
1888 function which decides whether anything needs to go
1889 into these sections. */
1890
8423293d 1891 s->flags |= SEC_EXCLUDE;
70256ad8
AJ
1892 continue;
1893 }
1894
c456f082
AM
1895 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1896 continue;
1897
70256ad8
AJ
1898 /* Allocate memory for the section contents. We use bfd_zalloc
1899 here in case unused entries are not reclaimed before the
1900 section's contents are written out. This should not happen,
1901 but this way if it does, we get a R_X86_64_NONE reloc instead
1902 of garbage. */
eea6121a 1903 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
c434dee6 1904 if (s->contents == NULL)
b34976b6 1905 return FALSE;
70256ad8
AJ
1906 }
1907
c434dee6 1908 if (htab->elf.dynamic_sections_created)
70256ad8
AJ
1909 {
1910 /* Add some entries to the .dynamic section. We fill in the
1911 values later, in elf64_x86_64_finish_dynamic_sections, but we
1912 must add the entries now so that we get the correct size for
407443a3 1913 the .dynamic section. The DT_DEBUG entry is filled in by the
70256ad8 1914 dynamic linker and used by the debugger. */
dc810e39 1915#define add_dynamic_entry(TAG, VAL) \
5a580b3a 1916 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 1917
36af4a4e 1918 if (info->executable)
70256ad8 1919 {
dc810e39 1920 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 1921 return FALSE;
70256ad8
AJ
1922 }
1923
eea6121a 1924 if (htab->splt->size != 0)
70256ad8 1925 {
dc810e39
AM
1926 if (!add_dynamic_entry (DT_PLTGOT, 0)
1927 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1928 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1929 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 1930 return FALSE;
67a4f2b7
AO
1931
1932 if (htab->tlsdesc_plt
1933 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
1934 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
1935 return FALSE;
70256ad8
AJ
1936 }
1937
1938 if (relocs)
1939 {
dc810e39
AM
1940 if (!add_dynamic_entry (DT_RELA, 0)
1941 || !add_dynamic_entry (DT_RELASZ, 0)
1942 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 1943 return FALSE;
70256ad8 1944
c434dee6
AJ
1945 /* If any dynamic relocs apply to a read-only section,
1946 then we need a DT_TEXTREL entry. */
1947 if ((info->flags & DF_TEXTREL) == 0)
1948 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1949 (PTR) info);
1950
1951 if ((info->flags & DF_TEXTREL) != 0)
1952 {
1953 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 1954 return FALSE;
c434dee6 1955 }
70256ad8
AJ
1956 }
1957 }
dc810e39 1958#undef add_dynamic_entry
70256ad8 1959
b34976b6 1960 return TRUE;
70256ad8
AJ
1961}
1962
67a4f2b7
AO
1963static bfd_boolean
1964elf64_x86_64_always_size_sections (bfd *output_bfd,
1965 struct bfd_link_info *info)
1966{
1967 asection *tls_sec = elf_hash_table (info)->tls_sec;
1968
1969 if (tls_sec)
1970 {
1971 struct elf_link_hash_entry *tlsbase;
1972
1973 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
1974 "_TLS_MODULE_BASE_",
1975 FALSE, FALSE, FALSE);
1976
1977 if (tlsbase && tlsbase->type == STT_TLS)
1978 {
1979 struct bfd_link_hash_entry *bh = NULL;
1980 const struct elf_backend_data *bed
1981 = get_elf_backend_data (output_bfd);
1982
1983 if (!(_bfd_generic_link_add_one_symbol
1984 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1985 tls_sec, 0, NULL, FALSE,
1986 bed->collect, &bh)))
1987 return FALSE;
1988 tlsbase = (struct elf_link_hash_entry *)bh;
1989 tlsbase->def_regular = 1;
1990 tlsbase->other = STV_HIDDEN;
1991 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
1992 }
1993 }
1994
1995 return TRUE;
1996}
1997
bffbf940
JJ
1998/* Return the base VMA address which should be subtracted from real addresses
1999 when resolving @dtpoff relocation.
2000 This is PT_TLS segment p_vaddr. */
2001
2002static bfd_vma
27482721 2003dtpoff_base (struct bfd_link_info *info)
bffbf940 2004{
e1918d23
AM
2005 /* If tls_sec is NULL, we should have signalled an error already. */
2006 if (elf_hash_table (info)->tls_sec == NULL)
bffbf940 2007 return 0;
e1918d23 2008 return elf_hash_table (info)->tls_sec->vma;
bffbf940
JJ
2009}
2010
2011/* Return the relocation value for @tpoff relocation
2012 if STT_TLS virtual address is ADDRESS. */
2013
2014static bfd_vma
27482721 2015tpoff (struct bfd_link_info *info, bfd_vma address)
bffbf940 2016{
e1918d23 2017 struct elf_link_hash_table *htab = elf_hash_table (info);
bffbf940
JJ
2018
2019 /* If tls_segment is NULL, we should have signalled an error already. */
e1918d23 2020 if (htab->tls_sec == NULL)
bffbf940 2021 return 0;
e1918d23 2022 return address - htab->tls_size - htab->tls_sec->vma;
bffbf940
JJ
2023}
2024
90f487df
L
2025/* Is the instruction before OFFSET in CONTENTS a 32bit relative
2026 branch? */
2027
2028static bfd_boolean
2029is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2030{
2031 /* Opcode Instruction
2032 0xe8 call
2033 0xe9 jump
2034 0x0f 0x8x conditional jump */
2035 return ((offset > 0
2036 && (contents [offset - 1] == 0xe8
2037 || contents [offset - 1] == 0xe9))
2038 || (offset > 1
2039 && contents [offset - 2] == 0x0f
2040 && (contents [offset - 1] & 0xf0) == 0x80));
2041}
2042
8d88c4ca
NC
2043/* Relocate an x86_64 ELF section. */
2044
b34976b6 2045static bfd_boolean
27482721
AJ
2046elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2047 bfd *input_bfd, asection *input_section,
2048 bfd_byte *contents, Elf_Internal_Rela *relocs,
2049 Elf_Internal_Sym *local_syms,
2050 asection **local_sections)
8d88c4ca 2051{
c434dee6 2052 struct elf64_x86_64_link_hash_table *htab;
8d88c4ca
NC
2053 Elf_Internal_Shdr *symtab_hdr;
2054 struct elf_link_hash_entry **sym_hashes;
2055 bfd_vma *local_got_offsets;
67a4f2b7 2056 bfd_vma *local_tlsdesc_gotents;
c434dee6 2057 Elf_Internal_Rela *rel;
8d88c4ca
NC
2058 Elf_Internal_Rela *relend;
2059
c434dee6 2060 htab = elf64_x86_64_hash_table (info);
8d88c4ca
NC
2061 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2062 sym_hashes = elf_sym_hashes (input_bfd);
2063 local_got_offsets = elf_local_got_offsets (input_bfd);
67a4f2b7 2064 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
8d88c4ca 2065
c434dee6 2066 rel = relocs;
8d88c4ca 2067 relend = relocs + input_section->reloc_count;
c434dee6 2068 for (; rel < relend; rel++)
8d88c4ca 2069 {
bffbf940 2070 unsigned int r_type;
8d88c4ca
NC
2071 reloc_howto_type *howto;
2072 unsigned long r_symndx;
2073 struct elf_link_hash_entry *h;
2074 Elf_Internal_Sym *sym;
2075 asection *sec;
67a4f2b7 2076 bfd_vma off, offplt;
8d88c4ca 2077 bfd_vma relocation;
b34976b6 2078 bfd_boolean unresolved_reloc;
8d88c4ca 2079 bfd_reloc_status_type r;
bffbf940 2080 int tls_type;
8d88c4ca 2081
c434dee6 2082 r_type = ELF64_R_TYPE (rel->r_info);
fe4770f4
AJ
2083 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2084 || r_type == (int) R_X86_64_GNU_VTENTRY)
2085 continue;
8d88c4ca 2086
bffbf940 2087 if (r_type >= R_X86_64_max)
8da6118f
KH
2088 {
2089 bfd_set_error (bfd_error_bad_value);
b34976b6 2090 return FALSE;
8da6118f 2091 }
8d88c4ca 2092
b491616a 2093 howto = x86_64_elf_howto_table + r_type;
c434dee6 2094 r_symndx = ELF64_R_SYM (rel->r_info);
8d88c4ca
NC
2095 h = NULL;
2096 sym = NULL;
2097 sec = NULL;
b34976b6 2098 unresolved_reloc = FALSE;
8d88c4ca 2099 if (r_symndx < symtab_hdr->sh_info)
8da6118f
KH
2100 {
2101 sym = local_syms + r_symndx;
2102 sec = local_sections[r_symndx];
c434dee6 2103
8517fae7 2104 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8da6118f 2105 }
8d88c4ca 2106 else
8da6118f 2107 {
560e09e9 2108 bfd_boolean warned;
c434dee6 2109
b2a8e766
AM
2110 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2111 r_symndx, symtab_hdr, sym_hashes,
2112 h, sec, relocation,
2113 unresolved_reloc, warned);
8da6118f 2114 }
ab96bf03
AM
2115
2116 if (sec != NULL && elf_discarded_section (sec))
2117 {
2118 /* For relocs against symbols from removed linkonce sections,
2119 or sections discarded by a linker script, we just want the
2120 section contents zeroed. Avoid any special processing. */
2121 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2122 rel->r_info = 0;
2123 rel->r_addend = 0;
2124 continue;
2125 }
2126
2127 if (info->relocatable)
2128 continue;
2129
70256ad8
AJ
2130 /* When generating a shared object, the relocations handled here are
2131 copied into the output file to be resolved at run time. */
2132 switch (r_type)
2133 {
7b81dfbb 2134 asection *base_got;
70256ad8 2135 case R_X86_64_GOT32:
7b81dfbb 2136 case R_X86_64_GOT64:
70256ad8
AJ
2137 /* Relocation is to the entry for this symbol in the global
2138 offset table. */
70256ad8 2139 case R_X86_64_GOTPCREL:
7b81dfbb
AJ
2140 case R_X86_64_GOTPCREL64:
2141 /* Use global offset table entry as symbol value. */
2142 case R_X86_64_GOTPLT64:
2143 /* This is the same as GOT64 for relocation purposes, but
2144 indicates the existence of a PLT entry. The difficulty is,
2145 that we must calculate the GOT slot offset from the PLT
2146 offset, if this symbol got a PLT entry (it was global).
2147 Additionally if it's computed from the PLT entry, then that
2148 GOT offset is relative to .got.plt, not to .got. */
2149 base_got = htab->sgot;
2150
c434dee6
AJ
2151 if (htab->sgot == NULL)
2152 abort ();
053579d7 2153
51e0a107 2154 if (h != NULL)
70256ad8 2155 {
b34976b6 2156 bfd_boolean dyn;
c434dee6
AJ
2157
2158 off = h->got.offset;
7b81dfbb
AJ
2159 if (h->needs_plt
2160 && h->plt.offset != (bfd_vma)-1
2161 && off == (bfd_vma)-1)
2162 {
2163 /* We can't use h->got.offset here to save
2164 state, or even just remember the offset, as
2165 finish_dynamic_symbol would use that as offset into
2166 .got. */
2167 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2168 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2169 base_got = htab->sgotplt;
2170 }
2171
c434dee6 2172 dyn = htab->elf.dynamic_sections_created;
51e0a107 2173
27482721 2174 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
51e0a107 2175 || (info->shared
27482721 2176 && SYMBOL_REFERENCES_LOCAL (info, h))
4bc6e03a
AJ
2177 || (ELF_ST_VISIBILITY (h->other)
2178 && h->root.type == bfd_link_hash_undefweak))
51e0a107
JH
2179 {
2180 /* This is actually a static link, or it is a -Bsymbolic
2181 link and the symbol is defined locally, or the symbol
407443a3 2182 was forced to be local because of a version file. We
51e0a107
JH
2183 must initialize this entry in the global offset table.
2184 Since the offset must always be a multiple of 8, we
2185 use the least significant bit to record whether we
2186 have initialized it already.
2187
2188 When doing a dynamic link, we create a .rela.got
407443a3
AJ
2189 relocation entry to initialize the value. This is
2190 done in the finish_dynamic_symbol routine. */
51e0a107
JH
2191 if ((off & 1) != 0)
2192 off &= ~1;
2193 else
2194 {
2195 bfd_put_64 (output_bfd, relocation,
7b81dfbb
AJ
2196 base_got->contents + off);
2197 /* Note that this is harmless for the GOTPLT64 case,
2198 as -1 | 1 still is -1. */
51e0a107
JH
2199 h->got.offset |= 1;
2200 }
2201 }
053579d7 2202 else
b34976b6 2203 unresolved_reloc = FALSE;
70256ad8 2204 }
51e0a107
JH
2205 else
2206 {
c434dee6
AJ
2207 if (local_got_offsets == NULL)
2208 abort ();
51e0a107
JH
2209
2210 off = local_got_offsets[r_symndx];
2211
2212 /* The offset must always be a multiple of 8. We use
407443a3
AJ
2213 the least significant bit to record whether we have
2214 already generated the necessary reloc. */
51e0a107
JH
2215 if ((off & 1) != 0)
2216 off &= ~1;
2217 else
2218 {
c434dee6 2219 bfd_put_64 (output_bfd, relocation,
7b81dfbb 2220 base_got->contents + off);
51e0a107
JH
2221
2222 if (info->shared)
2223 {
947216bf 2224 asection *s;
51e0a107 2225 Elf_Internal_Rela outrel;
947216bf 2226 bfd_byte *loc;
70256ad8 2227
51e0a107
JH
2228 /* We need to generate a R_X86_64_RELATIVE reloc
2229 for the dynamic linker. */
947216bf
AM
2230 s = htab->srelgot;
2231 if (s == NULL)
c434dee6 2232 abort ();
51e0a107 2233
7b81dfbb
AJ
2234 outrel.r_offset = (base_got->output_section->vma
2235 + base_got->output_offset
51e0a107
JH
2236 + off);
2237 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2238 outrel.r_addend = relocation;
947216bf
AM
2239 loc = s->contents;
2240 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2241 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
51e0a107
JH
2242 }
2243
2244 local_got_offsets[r_symndx] |= 1;
2245 }
51e0a107 2246 }
6a2bda3f 2247
c434dee6
AJ
2248 if (off >= (bfd_vma) -2)
2249 abort ();
2250
7b81dfbb
AJ
2251 relocation = base_got->output_section->vma
2252 + base_got->output_offset + off;
2253 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
8c37241b
JJ
2254 relocation -= htab->sgotplt->output_section->vma
2255 - htab->sgotplt->output_offset;
c434dee6 2256
70256ad8
AJ
2257 break;
2258
d6ab8113
JB
2259 case R_X86_64_GOTOFF64:
2260 /* Relocation is relative to the start of the global offset
2261 table. */
2262
2263 /* Check to make sure it isn't a protected function symbol
2264 for shared library since it may not be local when used
2265 as function address. */
2266 if (info->shared
2267 && h
2268 && h->def_regular
2269 && h->type == STT_FUNC
2270 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2271 {
2272 (*_bfd_error_handler)
2273 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2274 input_bfd, h->root.root.string);
2275 bfd_set_error (bfd_error_bad_value);
2276 return FALSE;
2277 }
2278
2279 /* Note that sgot is not involved in this
2280 calculation. We always want the start of .got.plt. If we
2281 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2282 permitted by the ABI, we might have to change this
2283 calculation. */
2284 relocation -= htab->sgotplt->output_section->vma
2285 + htab->sgotplt->output_offset;
2286 break;
2287
2288 case R_X86_64_GOTPC32:
7b81dfbb 2289 case R_X86_64_GOTPC64:
d6ab8113
JB
2290 /* Use global offset table as symbol value. */
2291 relocation = htab->sgotplt->output_section->vma
2292 + htab->sgotplt->output_offset;
2293 unresolved_reloc = FALSE;
2294 break;
7b81dfbb
AJ
2295
2296 case R_X86_64_PLTOFF64:
2297 /* Relocation is PLT entry relative to GOT. For local
2298 symbols it's the symbol itself relative to GOT. */
2299 if (h != NULL
2300 /* See PLT32 handling. */
2301 && h->plt.offset != (bfd_vma) -1
2302 && htab->splt != NULL)
2303 {
2304 relocation = (htab->splt->output_section->vma
2305 + htab->splt->output_offset
2306 + h->plt.offset);
2307 unresolved_reloc = FALSE;
2308 }
2309
2310 relocation -= htab->sgotplt->output_section->vma
2311 + htab->sgotplt->output_offset;
2312 break;
d6ab8113 2313
70256ad8
AJ
2314 case R_X86_64_PLT32:
2315 /* Relocation is to the entry for this symbol in the
2316 procedure linkage table. */
2317
2318 /* Resolve a PLT32 reloc against a local symbol directly,
407443a3 2319 without using the procedure linkage table. */
70256ad8
AJ
2320 if (h == NULL)
2321 break;
2322
c434dee6
AJ
2323 if (h->plt.offset == (bfd_vma) -1
2324 || htab->splt == NULL)
70256ad8
AJ
2325 {
2326 /* We didn't make a PLT entry for this symbol. This
407443a3
AJ
2327 happens when statically linking PIC code, or when
2328 using -Bsymbolic. */
70256ad8
AJ
2329 break;
2330 }
2331
c434dee6
AJ
2332 relocation = (htab->splt->output_section->vma
2333 + htab->splt->output_offset
70256ad8 2334 + h->plt.offset);
b34976b6 2335 unresolved_reloc = FALSE;
70256ad8
AJ
2336 break;
2337
fd8ab9e5
AJ
2338 case R_X86_64_PC8:
2339 case R_X86_64_PC16:
2340 case R_X86_64_PC32:
6610a52d
L
2341 if (info->shared
2342 && !SYMBOL_REFERENCES_LOCAL (info, h)
ba3bee0b 2343 && (input_section->flags & SEC_ALLOC) != 0
90f487df
L
2344 && (input_section->flags & SEC_READONLY) != 0
2345 && (!h->def_regular
2346 || r_type != R_X86_64_PC32
2347 || h->type != STT_FUNC
2348 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
2349 || !is_32bit_relative_branch (contents,
2350 rel->r_offset)))
6610a52d 2351 {
90f487df
L
2352 if (h->def_regular
2353 && r_type == R_X86_64_PC32
2354 && h->type == STT_FUNC
2355 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2356 (*_bfd_error_handler)
2357 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
2358 input_bfd, h->root.root.string);
2359 else
2360 (*_bfd_error_handler)
2361 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2362 input_bfd, x86_64_elf_howto_table[r_type].name,
2363 h->root.root.string);
6610a52d
L
2364 bfd_set_error (bfd_error_bad_value);
2365 return FALSE;
2366 }
2367 /* Fall through. */
2368
70256ad8
AJ
2369 case R_X86_64_8:
2370 case R_X86_64_16:
2371 case R_X86_64_32:
d6ab8113 2372 case R_X86_64_PC64:
6b3db546 2373 case R_X86_64_64:
80643fbc 2374 /* FIXME: The ABI says the linker should make sure the value is
407443a3 2375 the same when it's zeroextended to 64 bit. */
c434dee6 2376
b1e24c02 2377 if ((input_section->flags & SEC_ALLOC) == 0)
c434dee6
AJ
2378 break;
2379
2380 if ((info->shared
4bc6e03a
AJ
2381 && (h == NULL
2382 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2383 || h->root.type != bfd_link_hash_undefweak)
c434dee6
AJ
2384 && ((r_type != R_X86_64_PC8
2385 && r_type != R_X86_64_PC16
d6ab8113
JB
2386 && r_type != R_X86_64_PC32
2387 && r_type != R_X86_64_PC64)
f6c52c13 2388 || !SYMBOL_CALLS_LOCAL (info, h)))
d40d037c
AJ
2389 || (ELIMINATE_COPY_RELOCS
2390 && !info->shared
c434dee6
AJ
2391 && h != NULL
2392 && h->dynindx != -1
f5385ebf
AM
2393 && !h->non_got_ref
2394 && ((h->def_dynamic
2395 && !h->def_regular)
c434dee6
AJ
2396 || h->root.type == bfd_link_hash_undefweak
2397 || h->root.type == bfd_link_hash_undefined)))
70256ad8
AJ
2398 {
2399 Elf_Internal_Rela outrel;
947216bf 2400 bfd_byte *loc;
b34976b6 2401 bfd_boolean skip, relocate;
c434dee6 2402 asection *sreloc;
70256ad8
AJ
2403
2404 /* When generating a shared object, these relocations
2405 are copied into the output file to be resolved at run
407443a3 2406 time. */
b34976b6
AM
2407 skip = FALSE;
2408 relocate = FALSE;
70256ad8 2409
c629eae0
JJ
2410 outrel.r_offset =
2411 _bfd_elf_section_offset (output_bfd, info, input_section,
c434dee6 2412 rel->r_offset);
c629eae0 2413 if (outrel.r_offset == (bfd_vma) -1)
b34976b6 2414 skip = TRUE;
0fb19cbc 2415 else if (outrel.r_offset == (bfd_vma) -2)
b34976b6 2416 skip = TRUE, relocate = TRUE;
70256ad8
AJ
2417
2418 outrel.r_offset += (input_section->output_section->vma
2419 + input_section->output_offset);
2420
2421 if (skip)
0bb2d96a 2422 memset (&outrel, 0, sizeof outrel);
c434dee6 2423
fd8ab9e5
AJ
2424 /* h->dynindx may be -1 if this symbol was marked to
2425 become local. */
2426 else if (h != NULL
c434dee6
AJ
2427 && h->dynindx != -1
2428 && (r_type == R_X86_64_PC8
2429 || r_type == R_X86_64_PC16
2430 || r_type == R_X86_64_PC32
d6ab8113 2431 || r_type == R_X86_64_PC64
c434dee6 2432 || !info->shared
55255dae 2433 || !SYMBOLIC_BIND (info, h)
f5385ebf 2434 || !h->def_regular))
70256ad8 2435 {
70256ad8 2436 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
c434dee6 2437 outrel.r_addend = rel->r_addend;
70256ad8
AJ
2438 }
2439 else
2440 {
c434dee6 2441 /* This symbol is local, or marked to become local. */
607c0e09
AS
2442 if (r_type == R_X86_64_64)
2443 {
b34976b6 2444 relocate = TRUE;
607c0e09
AS
2445 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2446 outrel.r_addend = relocation + rel->r_addend;
2447 }
2448 else
2449 {
2450 long sindx;
2451
8517fae7 2452 if (bfd_is_abs_section (sec))
607c0e09
AS
2453 sindx = 0;
2454 else if (sec == NULL || sec->owner == NULL)
2455 {
2456 bfd_set_error (bfd_error_bad_value);
b34976b6 2457 return FALSE;
607c0e09
AS
2458 }
2459 else
2460 {
2461 asection *osec;
2462
74541ad4
AM
2463 /* We are turning this relocation into one
2464 against a section symbol. It would be
2465 proper to subtract the symbol's value,
2466 osec->vma, from the emitted reloc addend,
2467 but ld.so expects buggy relocs. */
607c0e09
AS
2468 osec = sec->output_section;
2469 sindx = elf_section_data (osec)->dynindx;
74541ad4
AM
2470 if (sindx == 0)
2471 {
2472 asection *oi = htab->elf.text_index_section;
2473 sindx = elf_section_data (oi)->dynindx;
2474 }
2475 BFD_ASSERT (sindx != 0);
607c0e09
AS
2476 }
2477
2478 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2479 outrel.r_addend = relocation + rel->r_addend;
2480 }
70256ad8
AJ
2481 }
2482
c434dee6
AJ
2483 sreloc = elf_section_data (input_section)->sreloc;
2484 if (sreloc == NULL)
2485 abort ();
2486
947216bf
AM
2487 loc = sreloc->contents;
2488 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 2489 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
70256ad8
AJ
2490
2491 /* If this reloc is against an external symbol, we do
2492 not want to fiddle with the addend. Otherwise, we
2493 need to include the symbol value so that it becomes
2494 an addend for the dynamic reloc. */
2495 if (! relocate)
2496 continue;
2497 }
2498
2499 break;
2500
bffbf940 2501 case R_X86_64_TLSGD:
67a4f2b7
AO
2502 case R_X86_64_GOTPC32_TLSDESC:
2503 case R_X86_64_TLSDESC_CALL:
bffbf940 2504 case R_X86_64_GOTTPOFF:
2f3bf80a 2505 r_type = elf64_x86_64_tls_transition (info, r_type, h);
bffbf940
JJ
2506 tls_type = GOT_UNKNOWN;
2507 if (h == NULL && local_got_offsets)
2508 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2509 else if (h != NULL)
2510 {
2511 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2512 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2513 r_type = R_X86_64_TPOFF32;
2514 }
67a4f2b7
AO
2515 if (r_type == R_X86_64_TLSGD
2516 || r_type == R_X86_64_GOTPC32_TLSDESC
2517 || r_type == R_X86_64_TLSDESC_CALL)
bffbf940
JJ
2518 {
2519 if (tls_type == GOT_TLS_IE)
2520 r_type = R_X86_64_GOTTPOFF;
2521 }
2522
2523 if (r_type == R_X86_64_TPOFF32)
2524 {
2525 BFD_ASSERT (! unresolved_reloc);
2526 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2527 {
2528 unsigned int i;
abcf1d52
JJ
2529 static unsigned char tlsgd[8]
2530 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
a3fadc9a
L
2531 unsigned long tls_r_symndx;
2532 struct elf_link_hash_entry *tls_h;
bffbf940
JJ
2533
2534 /* GD->LE transition.
abcf1d52 2535 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
a3fadc9a 2536 .word 0x6666; rex64; call __tls_get_addr
bffbf940
JJ
2537 Change it into:
2538 movq %fs:0, %rax
2539 leaq foo@tpoff(%rax), %rax */
abcf1d52
JJ
2540 BFD_ASSERT (rel->r_offset >= 4);
2541 for (i = 0; i < 4; i++)
bffbf940 2542 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52 2543 contents + rel->r_offset - 4 + i)
bffbf940 2544 == tlsgd[i]);
eea6121a 2545 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
abcf1d52
JJ
2546 for (i = 0; i < 4; i++)
2547 BFD_ASSERT (bfd_get_8 (input_bfd,
2548 contents + rel->r_offset + 4 + i)
2549 == tlsgd[i+4]);
bffbf940 2550 BFD_ASSERT (rel + 1 < relend);
a3fadc9a
L
2551 tls_r_symndx = ELF64_R_SYM (rel[1].r_info);
2552 BFD_ASSERT (tls_r_symndx >= symtab_hdr->sh_info);
2553 tls_h = sym_hashes[tls_r_symndx - symtab_hdr->sh_info];
2554 BFD_ASSERT (tls_h != NULL
2555 && tls_h->root.root.string != NULL
2556 && strcmp (tls_h->root.root.string,
2557 "__tls_get_addr") == 0);
2558 BFD_ASSERT ((! info->shared
2559 && ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32)
2560 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
abcf1d52 2561 memcpy (contents + rel->r_offset - 4,
bffbf940
JJ
2562 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2563 16);
2564 bfd_put_32 (output_bfd, tpoff (info, relocation),
abcf1d52 2565 contents + rel->r_offset + 8);
a3fadc9a 2566 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
bffbf940
JJ
2567 rel++;
2568 continue;
2569 }
67a4f2b7
AO
2570 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2571 {
2572 /* GDesc -> LE transition.
2573 It's originally something like:
2574 leaq x@tlsdesc(%rip), %rax
2575
2576 Change it to:
2577 movl $x@tpoff, %rax
2578
2579 Registers other than %rax may be set up here. */
2580
2581 unsigned int val, type, type2;
2582 bfd_vma roff;
2583
2584 /* First, make sure it's a leaq adding rip to a
2585 32-bit offset into any register, although it's
2586 probably almost always going to be rax. */
2587 roff = rel->r_offset;
2588 BFD_ASSERT (roff >= 3);
2589 type = bfd_get_8 (input_bfd, contents + roff - 3);
2590 BFD_ASSERT ((type & 0xfb) == 0x48);
2591 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2592 BFD_ASSERT (type2 == 0x8d);
2593 val = bfd_get_8 (input_bfd, contents + roff - 1);
2594 BFD_ASSERT ((val & 0xc7) == 0x05);
2595 BFD_ASSERT (roff + 4 <= input_section->size);
2596
2597 /* Now modify the instruction as appropriate. */
2598 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
2599 contents + roff - 3);
2600 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
2601 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2602 contents + roff - 1);
2603 bfd_put_32 (output_bfd, tpoff (info, relocation),
2604 contents + roff);
2605 continue;
2606 }
2607 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2608 {
2609 /* GDesc -> LE transition.
2610 It's originally:
2611 call *(%rax)
2612 Turn it into:
2613 nop; nop. */
2614
2615 unsigned int val, type;
2616 bfd_vma roff;
2617
2618 /* First, make sure it's a call *(%rax). */
2619 roff = rel->r_offset;
2620 BFD_ASSERT (roff + 2 <= input_section->size);
2621 type = bfd_get_8 (input_bfd, contents + roff);
2622 BFD_ASSERT (type == 0xff);
2623 val = bfd_get_8 (input_bfd, contents + roff + 1);
2624 BFD_ASSERT (val == 0x10);
2625
10efb593
L
2626 /* Now modify the instruction as appropriate. Use
2627 xchg %ax,%ax instead of 2 nops. */
2628 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
2629 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2630 continue;
2631 }
bffbf940
JJ
2632 else
2633 {
2634 unsigned int val, type, reg;
2635
2636 /* IE->LE transition:
2637 Originally it can be one of:
2638 movq foo@gottpoff(%rip), %reg
2639 addq foo@gottpoff(%rip), %reg
2640 We change it into:
2641 movq $foo, %reg
2642 leaq foo(%reg), %reg
2643 addq $foo, %reg. */
2644 BFD_ASSERT (rel->r_offset >= 3);
2645 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2646 BFD_ASSERT (val == 0x48 || val == 0x4c);
2647 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2648 BFD_ASSERT (type == 0x8b || type == 0x03);
2649 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2650 BFD_ASSERT ((reg & 0xc7) == 5);
2651 reg >>= 3;
eea6121a 2652 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
bffbf940
JJ
2653 if (type == 0x8b)
2654 {
2655 /* movq */
2656 if (val == 0x4c)
2657 bfd_put_8 (output_bfd, 0x49,
2658 contents + rel->r_offset - 3);
2659 bfd_put_8 (output_bfd, 0xc7,
2660 contents + rel->r_offset - 2);
2661 bfd_put_8 (output_bfd, 0xc0 | reg,
2662 contents + rel->r_offset - 1);
2663 }
2664 else if (reg == 4)
2665 {
2666 /* addq -> addq - addressing with %rsp/%r12 is
2667 special */
2668 if (val == 0x4c)
2669 bfd_put_8 (output_bfd, 0x49,
2670 contents + rel->r_offset - 3);
2671 bfd_put_8 (output_bfd, 0x81,
2672 contents + rel->r_offset - 2);
2673 bfd_put_8 (output_bfd, 0xc0 | reg,
2674 contents + rel->r_offset - 1);
2675 }
2676 else
2677 {
2678 /* addq -> leaq */
2679 if (val == 0x4c)
2680 bfd_put_8 (output_bfd, 0x4d,
2681 contents + rel->r_offset - 3);
2682 bfd_put_8 (output_bfd, 0x8d,
2683 contents + rel->r_offset - 2);
2684 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2685 contents + rel->r_offset - 1);
2686 }
2687 bfd_put_32 (output_bfd, tpoff (info, relocation),
2688 contents + rel->r_offset);
2689 continue;
2690 }
2691 }
2692
2693 if (htab->sgot == NULL)
2694 abort ();
2695
2696 if (h != NULL)
67a4f2b7
AO
2697 {
2698 off = h->got.offset;
2699 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
2700 }
bffbf940
JJ
2701 else
2702 {
2703 if (local_got_offsets == NULL)
2704 abort ();
2705
2706 off = local_got_offsets[r_symndx];
67a4f2b7 2707 offplt = local_tlsdesc_gotents[r_symndx];
bffbf940
JJ
2708 }
2709
2710 if ((off & 1) != 0)
2711 off &= ~1;
26e41594 2712 else
bffbf940
JJ
2713 {
2714 Elf_Internal_Rela outrel;
947216bf 2715 bfd_byte *loc;
bffbf940 2716 int dr_type, indx;
67a4f2b7 2717 asection *sreloc;
bffbf940
JJ
2718
2719 if (htab->srelgot == NULL)
2720 abort ();
2721
67a4f2b7
AO
2722 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2723
2724 if (GOT_TLS_GDESC_P (tls_type))
2725 {
2726 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
2727 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
2728 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
2729 outrel.r_offset = (htab->sgotplt->output_section->vma
2730 + htab->sgotplt->output_offset
2731 + offplt
2732 + htab->sgotplt_jump_table_size);
2733 sreloc = htab->srelplt;
2734 loc = sreloc->contents;
2735 loc += sreloc->reloc_count++
2736 * sizeof (Elf64_External_Rela);
2737 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2738 <= sreloc->contents + sreloc->size);
2739 if (indx == 0)
2740 outrel.r_addend = relocation - dtpoff_base (info);
2741 else
2742 outrel.r_addend = 0;
2743 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2744 }
2745
2746 sreloc = htab->srelgot;
2747
bffbf940
JJ
2748 outrel.r_offset = (htab->sgot->output_section->vma
2749 + htab->sgot->output_offset + off);
2750
67a4f2b7 2751 if (GOT_TLS_GD_P (tls_type))
bffbf940 2752 dr_type = R_X86_64_DTPMOD64;
67a4f2b7
AO
2753 else if (GOT_TLS_GDESC_P (tls_type))
2754 goto dr_done;
bffbf940
JJ
2755 else
2756 dr_type = R_X86_64_TPOFF64;
2757
2758 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2759 outrel.r_addend = 0;
67a4f2b7
AO
2760 if ((dr_type == R_X86_64_TPOFF64
2761 || dr_type == R_X86_64_TLSDESC) && indx == 0)
bffbf940
JJ
2762 outrel.r_addend = relocation - dtpoff_base (info);
2763 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2764
67a4f2b7
AO
2765 loc = sreloc->contents;
2766 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2767 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2768 <= sreloc->contents + sreloc->size);
bffbf940
JJ
2769 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2770
67a4f2b7 2771 if (GOT_TLS_GD_P (tls_type))
bffbf940
JJ
2772 {
2773 if (indx == 0)
2774 {
d40d037c 2775 BFD_ASSERT (! unresolved_reloc);
bffbf940
JJ
2776 bfd_put_64 (output_bfd,
2777 relocation - dtpoff_base (info),
2778 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2779 }
2780 else
2781 {
2782 bfd_put_64 (output_bfd, 0,
2783 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2784 outrel.r_info = ELF64_R_INFO (indx,
2785 R_X86_64_DTPOFF64);
2786 outrel.r_offset += GOT_ENTRY_SIZE;
67a4f2b7 2787 sreloc->reloc_count++;
947216bf 2788 loc += sizeof (Elf64_External_Rela);
67a4f2b7
AO
2789 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2790 <= sreloc->contents + sreloc->size);
947216bf 2791 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
bffbf940
JJ
2792 }
2793 }
2794
67a4f2b7 2795 dr_done:
bffbf940
JJ
2796 if (h != NULL)
2797 h->got.offset |= 1;
2798 else
2799 local_got_offsets[r_symndx] |= 1;
2800 }
2801
67a4f2b7
AO
2802 if (off >= (bfd_vma) -2
2803 && ! GOT_TLS_GDESC_P (tls_type))
bffbf940
JJ
2804 abort ();
2805 if (r_type == ELF64_R_TYPE (rel->r_info))
2806 {
67a4f2b7
AO
2807 if (r_type == R_X86_64_GOTPC32_TLSDESC
2808 || r_type == R_X86_64_TLSDESC_CALL)
2809 relocation = htab->sgotplt->output_section->vma
2810 + htab->sgotplt->output_offset
2811 + offplt + htab->sgotplt_jump_table_size;
2812 else
2813 relocation = htab->sgot->output_section->vma
2814 + htab->sgot->output_offset + off;
b34976b6 2815 unresolved_reloc = FALSE;
bffbf940 2816 }
67a4f2b7 2817 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
bffbf940
JJ
2818 {
2819 unsigned int i;
abcf1d52
JJ
2820 static unsigned char tlsgd[8]
2821 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
bffbf940
JJ
2822
2823 /* GD->IE transition.
abcf1d52
JJ
2824 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2825 .word 0x6666; rex64; call __tls_get_addr@plt
bffbf940
JJ
2826 Change it into:
2827 movq %fs:0, %rax
2828 addq foo@gottpoff(%rip), %rax */
abcf1d52
JJ
2829 BFD_ASSERT (rel->r_offset >= 4);
2830 for (i = 0; i < 4; i++)
26e41594 2831 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52 2832 contents + rel->r_offset - 4 + i)
bffbf940 2833 == tlsgd[i]);
eea6121a 2834 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
abcf1d52 2835 for (i = 0; i < 4; i++)
26e41594 2836 BFD_ASSERT (bfd_get_8 (input_bfd,
abcf1d52
JJ
2837 contents + rel->r_offset + 4 + i)
2838 == tlsgd[i+4]);
bffbf940
JJ
2839 BFD_ASSERT (rel + 1 < relend);
2840 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
abcf1d52 2841 memcpy (contents + rel->r_offset - 4,
bffbf940
JJ
2842 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2843 16);
2844
2845 relocation = (htab->sgot->output_section->vma
2846 + htab->sgot->output_offset + off
2847 - rel->r_offset
2848 - input_section->output_section->vma
2849 - input_section->output_offset
abcf1d52 2850 - 12);
bffbf940 2851 bfd_put_32 (output_bfd, relocation,
abcf1d52 2852 contents + rel->r_offset + 8);
bffbf940
JJ
2853 /* Skip R_X86_64_PLT32. */
2854 rel++;
2855 continue;
2856 }
67a4f2b7
AO
2857 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2858 {
2859 /* GDesc -> IE transition.
2860 It's originally something like:
2861 leaq x@tlsdesc(%rip), %rax
2862
2863 Change it to:
2864 movq x@gottpoff(%rip), %rax # before nop; nop
2865
2866 Registers other than %rax may be set up here. */
2867
2868 unsigned int val, type, type2;
2869 bfd_vma roff;
2870
2871 /* First, make sure it's a leaq adding rip to a 32-bit
2872 offset into any register, although it's probably
2873 almost always going to be rax. */
2874 roff = rel->r_offset;
2875 BFD_ASSERT (roff >= 3);
2876 type = bfd_get_8 (input_bfd, contents + roff - 3);
2877 BFD_ASSERT ((type & 0xfb) == 0x48);
2878 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2879 BFD_ASSERT (type2 == 0x8d);
2880 val = bfd_get_8 (input_bfd, contents + roff - 1);
2881 BFD_ASSERT ((val & 0xc7) == 0x05);
2882 BFD_ASSERT (roff + 4 <= input_section->size);
2883
2884 /* Now modify the instruction as appropriate. */
2885 /* To turn a leaq into a movq in the form we use it, it
2886 suffices to change the second byte from 0x8d to
2887 0x8b. */
2888 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
2889
2890 bfd_put_32 (output_bfd,
2891 htab->sgot->output_section->vma
2892 + htab->sgot->output_offset + off
2893 - rel->r_offset
2894 - input_section->output_section->vma
2895 - input_section->output_offset
2896 - 4,
2897 contents + roff);
2898 continue;
2899 }
2900 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2901 {
2902 /* GDesc -> IE transition.
2903 It's originally:
2904 call *(%rax)
2905
2906 Change it to:
2907 nop; nop. */
2908
2909 unsigned int val, type;
2910 bfd_vma roff;
2911
2912 /* First, make sure it's a call *(%eax). */
2913 roff = rel->r_offset;
2914 BFD_ASSERT (roff + 2 <= input_section->size);
2915 type = bfd_get_8 (input_bfd, contents + roff);
2916 BFD_ASSERT (type == 0xff);
2917 val = bfd_get_8 (input_bfd, contents + roff + 1);
2918 BFD_ASSERT (val == 0x10);
2919
10efb593
L
2920 /* Now modify the instruction as appropriate. Use
2921 xchg %ax,%ax instead of 2 nops. */
2922 bfd_put_8 (output_bfd, 0x66, contents + roff);
67a4f2b7
AO
2923 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2924
2925 continue;
2926 }
2927 else
2928 BFD_ASSERT (FALSE);
bffbf940
JJ
2929 break;
2930
2931 case R_X86_64_TLSLD:
2932 if (! info->shared)
2933 {
a3fadc9a
L
2934 unsigned long tls_r_symndx;
2935 struct elf_link_hash_entry *tls_h;
2936
bffbf940
JJ
2937 /* LD->LE transition:
2938 Ensure it is:
a3fadc9a 2939 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
bffbf940
JJ
2940 We change it into:
2941 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2942 BFD_ASSERT (rel->r_offset >= 3);
2943 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2944 == 0x48);
2945 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2946 == 0x8d);
2947 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2948 == 0x3d);
eea6121a 2949 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
bffbf940
JJ
2950 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2951 == 0xe8);
2952 BFD_ASSERT (rel + 1 < relend);
a3fadc9a
L
2953 tls_r_symndx = ELF64_R_SYM (rel[1].r_info);
2954 BFD_ASSERT (tls_r_symndx >= symtab_hdr->sh_info);
2955 tls_h = sym_hashes[tls_r_symndx - symtab_hdr->sh_info];
2956 BFD_ASSERT (tls_h != NULL
2957 && tls_h->root.root.string != NULL
2958 && strcmp (tls_h->root.root.string,
2959 "__tls_get_addr") == 0);
2960 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
2961 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
bffbf940
JJ
2962 memcpy (contents + rel->r_offset - 3,
2963 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
a3fadc9a 2964 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
bffbf940
JJ
2965 rel++;
2966 continue;
2967 }
2968
2969 if (htab->sgot == NULL)
2970 abort ();
2971
2972 off = htab->tls_ld_got.offset;
2973 if (off & 1)
2974 off &= ~1;
2975 else
2976 {
2977 Elf_Internal_Rela outrel;
947216bf 2978 bfd_byte *loc;
bffbf940
JJ
2979
2980 if (htab->srelgot == NULL)
2981 abort ();
2982
2983 outrel.r_offset = (htab->sgot->output_section->vma
2984 + htab->sgot->output_offset + off);
2985
2986 bfd_put_64 (output_bfd, 0,
2987 htab->sgot->contents + off);
2988 bfd_put_64 (output_bfd, 0,
2989 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2990 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2991 outrel.r_addend = 0;
947216bf
AM
2992 loc = htab->srelgot->contents;
2993 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
bffbf940
JJ
2994 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2995 htab->tls_ld_got.offset |= 1;
2996 }
2997 relocation = htab->sgot->output_section->vma
2998 + htab->sgot->output_offset + off;
b34976b6 2999 unresolved_reloc = FALSE;
bffbf940
JJ
3000 break;
3001
3002 case R_X86_64_DTPOFF32:
a45bb67d 3003 if (info->shared || (input_section->flags & SEC_CODE) == 0)
bffbf940
JJ
3004 relocation -= dtpoff_base (info);
3005 else
3006 relocation = tpoff (info, relocation);
3007 break;
3008
3009 case R_X86_64_TPOFF32:
3010 BFD_ASSERT (! info->shared);
3011 relocation = tpoff (info, relocation);
3012 break;
3013
70256ad8
AJ
3014 default:
3015 break;
3016 }
8d88c4ca 3017
239e1f3a
AM
3018 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3019 because such sections are not SEC_ALLOC and thus ld.so will
3020 not process them. */
c434dee6 3021 if (unresolved_reloc
239e1f3a 3022 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 3023 && h->def_dynamic))
c434dee6 3024 (*_bfd_error_handler)
843fe662 3025 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
d003868e
AM
3026 input_bfd,
3027 input_section,
c434dee6 3028 (long) rel->r_offset,
843fe662 3029 howto->name,
c434dee6
AJ
3030 h->root.root.string);
3031
8d88c4ca 3032 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
c434dee6
AJ
3033 contents, rel->r_offset,
3034 relocation, rel->r_addend);
8d88c4ca
NC
3035
3036 if (r != bfd_reloc_ok)
8da6118f 3037 {
c434dee6
AJ
3038 const char *name;
3039
3040 if (h != NULL)
3041 name = h->root.root.string;
3042 else
8da6118f 3043 {
c434dee6
AJ
3044 name = bfd_elf_string_from_elf_section (input_bfd,
3045 symtab_hdr->sh_link,
3046 sym->st_name);
3047 if (name == NULL)
b34976b6 3048 return FALSE;
c434dee6
AJ
3049 if (*name == '\0')
3050 name = bfd_section_name (input_bfd, sec);
3051 }
3052
3053 if (r == bfd_reloc_overflow)
3054 {
c434dee6 3055 if (! ((*info->callbacks->reloc_overflow)
dfeffb9f
L
3056 (info, (h ? &h->root : NULL), name, howto->name,
3057 (bfd_vma) 0, input_bfd, input_section,
3058 rel->r_offset)))
b34976b6 3059 return FALSE;
c434dee6
AJ
3060 }
3061 else
3062 {
3063 (*_bfd_error_handler)
d003868e
AM
3064 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3065 input_bfd, input_section,
c434dee6 3066 (long) rel->r_offset, name, (int) r);
b34976b6 3067 return FALSE;
8da6118f
KH
3068 }
3069 }
8d88c4ca 3070 }
70256ad8 3071
b34976b6 3072 return TRUE;
70256ad8
AJ
3073}
3074
3075/* Finish up dynamic symbol handling. We set the contents of various
3076 dynamic sections here. */
3077
b34976b6 3078static bfd_boolean
27482721
AJ
3079elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3080 struct bfd_link_info *info,
3081 struct elf_link_hash_entry *h,
3082 Elf_Internal_Sym *sym)
70256ad8 3083{
c434dee6 3084 struct elf64_x86_64_link_hash_table *htab;
70256ad8 3085
c434dee6 3086 htab = elf64_x86_64_hash_table (info);
70256ad8
AJ
3087
3088 if (h->plt.offset != (bfd_vma) -1)
3089 {
70256ad8
AJ
3090 bfd_vma plt_index;
3091 bfd_vma got_offset;
3092 Elf_Internal_Rela rela;
947216bf 3093 bfd_byte *loc;
70256ad8
AJ
3094
3095 /* This symbol has an entry in the procedure linkage table. Set
407443a3 3096 it up. */
c434dee6
AJ
3097 if (h->dynindx == -1
3098 || htab->splt == NULL
3099 || htab->sgotplt == NULL
3100 || htab->srelplt == NULL)
3101 abort ();
70256ad8
AJ
3102
3103 /* Get the index in the procedure linkage table which
3104 corresponds to this symbol. This is the index of this symbol
3105 in all the symbols for which we are making plt entries. The
3106 first entry in the procedure linkage table is reserved. */
3107 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3108
3109 /* Get the offset into the .got table of the entry that
407443a3 3110 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
fe4770f4 3111 bytes. The first three are reserved for the dynamic linker. */
70256ad8
AJ
3112 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3113
3114 /* Fill in the entry in the procedure linkage table. */
c434dee6 3115 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
70256ad8
AJ
3116 PLT_ENTRY_SIZE);
3117
3118 /* Insert the relocation positions of the plt section. The magic
3119 numbers at the end of the statements are the positions of the
3120 relocations in the plt section. */
653165cc
AJ
3121 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3122 instruction uses 6 bytes, subtract this value. */
3123 bfd_put_32 (output_bfd,
c434dee6
AJ
3124 (htab->sgotplt->output_section->vma
3125 + htab->sgotplt->output_offset
653165cc 3126 + got_offset
c434dee6
AJ
3127 - htab->splt->output_section->vma
3128 - htab->splt->output_offset
653165cc
AJ
3129 - h->plt.offset
3130 - 6),
c434dee6 3131 htab->splt->contents + h->plt.offset + 2);
653165cc
AJ
3132 /* Put relocation index. */
3133 bfd_put_32 (output_bfd, plt_index,
c434dee6 3134 htab->splt->contents + h->plt.offset + 7);
653165cc
AJ
3135 /* Put offset for jmp .PLT0. */
3136 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
c434dee6 3137 htab->splt->contents + h->plt.offset + 12);
70256ad8 3138
653165cc
AJ
3139 /* Fill in the entry in the global offset table, initially this
3140 points to the pushq instruction in the PLT which is at offset 6. */
c434dee6
AJ
3141 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
3142 + htab->splt->output_offset
70256ad8 3143 + h->plt.offset + 6),
c434dee6 3144 htab->sgotplt->contents + got_offset);
70256ad8
AJ
3145
3146 /* Fill in the entry in the .rela.plt section. */
c434dee6
AJ
3147 rela.r_offset = (htab->sgotplt->output_section->vma
3148 + htab->sgotplt->output_offset
70256ad8
AJ
3149 + got_offset);
3150 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3151 rela.r_addend = 0;
947216bf 3152 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
c434dee6 3153 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8 3154
f5385ebf 3155 if (!h->def_regular)
70256ad8
AJ
3156 {
3157 /* Mark the symbol as undefined, rather than as defined in
47a9f7b3
JJ
3158 the .plt section. Leave the value if there were any
3159 relocations where pointer equality matters (this is a clue
c434dee6
AJ
3160 for the dynamic linker, to make function pointer
3161 comparisons work between an application and shared
47a9f7b3
JJ
3162 library), otherwise set it to zero. If a function is only
3163 called from a binary, there is no need to slow down
3164 shared libraries because of that. */
70256ad8 3165 sym->st_shndx = SHN_UNDEF;
f5385ebf 3166 if (!h->pointer_equality_needed)
47a9f7b3 3167 sym->st_value = 0;
70256ad8
AJ
3168 }
3169 }
3170
bffbf940 3171 if (h->got.offset != (bfd_vma) -1
67a4f2b7 3172 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
bffbf940 3173 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
053579d7 3174 {
053579d7 3175 Elf_Internal_Rela rela;
947216bf 3176 bfd_byte *loc;
053579d7
AJ
3177
3178 /* This symbol has an entry in the global offset table. Set it
bffbf940 3179 up. */
c434dee6
AJ
3180 if (htab->sgot == NULL || htab->srelgot == NULL)
3181 abort ();
053579d7 3182
c434dee6
AJ
3183 rela.r_offset = (htab->sgot->output_section->vma
3184 + htab->sgot->output_offset
dc810e39 3185 + (h->got.offset &~ (bfd_vma) 1));
053579d7
AJ
3186
3187 /* If this is a static link, or it is a -Bsymbolic link and the
3188 symbol is defined locally or was forced to be local because
3189 of a version file, we just want to emit a RELATIVE reloc.
3190 The entry in the global offset table will already have been
3191 initialized in the relocate_section function. */
c434dee6 3192 if (info->shared
27482721 3193 && SYMBOL_REFERENCES_LOCAL (info, h))
053579d7 3194 {
cc78d0af 3195 BFD_ASSERT((h->got.offset & 1) != 0);
053579d7
AJ
3196 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3197 rela.r_addend = (h->root.u.def.value
3198 + h->root.u.def.section->output_section->vma
3199 + h->root.u.def.section->output_offset);
3200 }
3201 else
3202 {
3203 BFD_ASSERT((h->got.offset & 1) == 0);
c434dee6
AJ
3204 bfd_put_64 (output_bfd, (bfd_vma) 0,
3205 htab->sgot->contents + h->got.offset);
053579d7
AJ
3206 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3207 rela.r_addend = 0;
3208 }
3209
947216bf
AM
3210 loc = htab->srelgot->contents;
3211 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 3212 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
053579d7
AJ
3213 }
3214
f5385ebf 3215 if (h->needs_copy)
70256ad8 3216 {
70256ad8 3217 Elf_Internal_Rela rela;
947216bf 3218 bfd_byte *loc;
70256ad8
AJ
3219
3220 /* This symbol needs a copy reloc. Set it up. */
3221
c434dee6
AJ
3222 if (h->dynindx == -1
3223 || (h->root.type != bfd_link_hash_defined
3224 && h->root.type != bfd_link_hash_defweak)
3225 || htab->srelbss == NULL)
3226 abort ();
70256ad8
AJ
3227
3228 rela.r_offset = (h->root.u.def.value
3229 + h->root.u.def.section->output_section->vma
3230 + h->root.u.def.section->output_offset);
3231 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3232 rela.r_addend = 0;
947216bf
AM
3233 loc = htab->srelbss->contents;
3234 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
c434dee6 3235 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
70256ad8
AJ
3236 }
3237
3238 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3239 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
22edb2f1 3240 || h == htab->elf.hgot)
70256ad8
AJ
3241 sym->st_shndx = SHN_ABS;
3242
b34976b6 3243 return TRUE;
70256ad8
AJ
3244}
3245
c434dee6
AJ
3246/* Used to decide how to sort relocs in an optimal manner for the
3247 dynamic linker, before writing them out. */
3248
3249static enum elf_reloc_type_class
27482721 3250elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
c434dee6
AJ
3251{
3252 switch ((int) ELF64_R_TYPE (rela->r_info))
3253 {
3254 case R_X86_64_RELATIVE:
3255 return reloc_class_relative;
3256 case R_X86_64_JUMP_SLOT:
3257 return reloc_class_plt;
3258 case R_X86_64_COPY:
3259 return reloc_class_copy;
3260 default:
3261 return reloc_class_normal;
3262 }
3263}
3264
70256ad8
AJ
3265/* Finish up the dynamic sections. */
3266
b34976b6 3267static bfd_boolean
27482721 3268elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
70256ad8 3269{
c434dee6 3270 struct elf64_x86_64_link_hash_table *htab;
70256ad8
AJ
3271 bfd *dynobj;
3272 asection *sdyn;
70256ad8 3273
c434dee6
AJ
3274 htab = elf64_x86_64_hash_table (info);
3275 dynobj = htab->elf.dynobj;
70256ad8
AJ
3276 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3277
c434dee6 3278 if (htab->elf.dynamic_sections_created)
70256ad8 3279 {
70256ad8
AJ
3280 Elf64_External_Dyn *dyncon, *dynconend;
3281
c434dee6
AJ
3282 if (sdyn == NULL || htab->sgot == NULL)
3283 abort ();
70256ad8
AJ
3284
3285 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 3286 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
70256ad8
AJ
3287 for (; dyncon < dynconend; dyncon++)
3288 {
3289 Elf_Internal_Dyn dyn;
70256ad8
AJ
3290 asection *s;
3291
3292 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3293
3294 switch (dyn.d_tag)
3295 {
3296 default:
053579d7 3297 continue;
70256ad8
AJ
3298
3299 case DT_PLTGOT:
8c37241b
JJ
3300 s = htab->sgotplt;
3301 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
c434dee6 3302 break;
70256ad8
AJ
3303
3304 case DT_JMPREL:
c434dee6
AJ
3305 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3306 break;
70256ad8 3307
c434dee6
AJ
3308 case DT_PLTRELSZ:
3309 s = htab->srelplt->output_section;
eea6121a 3310 dyn.d_un.d_val = s->size;
70256ad8
AJ
3311 break;
3312
3313 case DT_RELASZ:
c434dee6
AJ
3314 /* The procedure linkage table relocs (DT_JMPREL) should
3315 not be included in the overall relocs (DT_RELA).
3316 Therefore, we override the DT_RELASZ entry here to
3317 make it not include the JMPREL relocs. Since the
3318 linker script arranges for .rela.plt to follow all
3319 other relocation sections, we don't have to worry
3320 about changing the DT_RELA entry. */
3321 if (htab->srelplt != NULL)
70256ad8 3322 {
c434dee6 3323 s = htab->srelplt->output_section;
eea6121a 3324 dyn.d_un.d_val -= s->size;
70256ad8
AJ
3325 }
3326 break;
67a4f2b7
AO
3327
3328 case DT_TLSDESC_PLT:
3329 s = htab->splt;
3330 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3331 + htab->tlsdesc_plt;
3332 break;
3333
3334 case DT_TLSDESC_GOT:
3335 s = htab->sgot;
3336 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3337 + htab->tlsdesc_got;
3338 break;
70256ad8 3339 }
c434dee6 3340
70256ad8
AJ
3341 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3342 }
3343
c434dee6 3344 /* Fill in the special first entry in the procedure linkage table. */
eea6121a 3345 if (htab->splt && htab->splt->size > 0)
70256ad8 3346 {
653165cc 3347 /* Fill in the first entry in the procedure linkage table. */
c434dee6
AJ
3348 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
3349 PLT_ENTRY_SIZE);
653165cc
AJ
3350 /* Add offset for pushq GOT+8(%rip), since the instruction
3351 uses 6 bytes subtract this value. */
3352 bfd_put_32 (output_bfd,
c434dee6
AJ
3353 (htab->sgotplt->output_section->vma
3354 + htab->sgotplt->output_offset
653165cc 3355 + 8
c434dee6
AJ
3356 - htab->splt->output_section->vma
3357 - htab->splt->output_offset
653165cc 3358 - 6),
c434dee6 3359 htab->splt->contents + 2);
653165cc
AJ
3360 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3361 the end of the instruction. */
3362 bfd_put_32 (output_bfd,
c434dee6
AJ
3363 (htab->sgotplt->output_section->vma
3364 + htab->sgotplt->output_offset
653165cc 3365 + 16
c434dee6
AJ
3366 - htab->splt->output_section->vma
3367 - htab->splt->output_offset
653165cc 3368 - 12),
c434dee6 3369 htab->splt->contents + 8);
653165cc 3370
c434dee6
AJ
3371 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
3372 PLT_ENTRY_SIZE;
67a4f2b7
AO
3373
3374 if (htab->tlsdesc_plt)
3375 {
3376 bfd_put_64 (output_bfd, (bfd_vma) 0,
3377 htab->sgot->contents + htab->tlsdesc_got);
3378
3379 memcpy (htab->splt->contents + htab->tlsdesc_plt,
3380 elf64_x86_64_plt0_entry,
3381 PLT_ENTRY_SIZE);
3382
3383 /* Add offset for pushq GOT+8(%rip), since the
3384 instruction uses 6 bytes subtract this value. */
3385 bfd_put_32 (output_bfd,
3386 (htab->sgotplt->output_section->vma
3387 + htab->sgotplt->output_offset
3388 + 8
3389 - htab->splt->output_section->vma
3390 - htab->splt->output_offset
3391 - htab->tlsdesc_plt
3392 - 6),
3393 htab->splt->contents + htab->tlsdesc_plt + 2);
3394 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3395 htab->tlsdesc_got. The 12 is the offset to the end of
3396 the instruction. */
3397 bfd_put_32 (output_bfd,
3398 (htab->sgot->output_section->vma
3399 + htab->sgot->output_offset
3400 + htab->tlsdesc_got
3401 - htab->splt->output_section->vma
3402 - htab->splt->output_offset
3403 - htab->tlsdesc_plt
3404 - 12),
3405 htab->splt->contents + htab->tlsdesc_plt + 8);
3406 }
70256ad8 3407 }
70256ad8
AJ
3408 }
3409
c434dee6 3410 if (htab->sgotplt)
70256ad8 3411 {
c434dee6 3412 /* Fill in the first three entries in the global offset table. */
eea6121a 3413 if (htab->sgotplt->size > 0)
c434dee6
AJ
3414 {
3415 /* Set the first entry in the global offset table to the address of
3416 the dynamic section. */
3417 if (sdyn == NULL)
3418 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
3419 else
3420 bfd_put_64 (output_bfd,
3421 sdyn->output_section->vma + sdyn->output_offset,
3422 htab->sgotplt->contents);
3423 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3424 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
3425 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
3426 }
70256ad8 3427
c434dee6
AJ
3428 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
3429 GOT_ENTRY_SIZE;
3430 }
70256ad8 3431
eea6121a 3432 if (htab->sgot && htab->sgot->size > 0)
8c37241b
JJ
3433 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
3434 = GOT_ENTRY_SIZE;
3435
b34976b6 3436 return TRUE;
8d88c4ca
NC
3437}
3438
4c45e5c9
JJ
3439/* Return address for Ith PLT stub in section PLT, for relocation REL
3440 or (bfd_vma) -1 if it should not be included. */
3441
3442static bfd_vma
3443elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
3444 const arelent *rel ATTRIBUTE_UNUSED)
3445{
3446 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3447}
8df9fc9d 3448
d2b2c203
DJ
3449/* Handle an x86-64 specific section when reading an object file. This
3450 is called when elfcode.h finds a section with an unknown type. */
3451
3452static bfd_boolean
6dc132d9
L
3453elf64_x86_64_section_from_shdr (bfd *abfd,
3454 Elf_Internal_Shdr *hdr,
3455 const char *name,
3456 int shindex)
d2b2c203
DJ
3457{
3458 if (hdr->sh_type != SHT_X86_64_UNWIND)
3459 return FALSE;
3460
6dc132d9 3461 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
d2b2c203
DJ
3462 return FALSE;
3463
3464 return TRUE;
3465}
3466
3b22753a
L
3467/* Hook called by the linker routine which adds symbols from an object
3468 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3469 of .bss. */
3470
3471static bfd_boolean
3472elf64_x86_64_add_symbol_hook (bfd *abfd,
3473 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3474 Elf_Internal_Sym *sym,
3475 const char **namep ATTRIBUTE_UNUSED,
3476 flagword *flagsp ATTRIBUTE_UNUSED,
3477 asection **secp, bfd_vma *valp)
3478{
3479 asection *lcomm;
3480
3481 switch (sym->st_shndx)
3482 {
3483 case SHN_X86_64_LCOMMON:
3484 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
3485 if (lcomm == NULL)
3486 {
3487 lcomm = bfd_make_section_with_flags (abfd,
3488 "LARGE_COMMON",
3489 (SEC_ALLOC
3490 | SEC_IS_COMMON
3491 | SEC_LINKER_CREATED));
3492 if (lcomm == NULL)
3493 return FALSE;
3494 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
3495 }
3496 *secp = lcomm;
3497 *valp = sym->st_size;
3498 break;
3499 }
3500 return TRUE;
3501}
3502
3503
3504/* Given a BFD section, try to locate the corresponding ELF section
3505 index. */
3506
3507static bfd_boolean
3508elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
3509 asection *sec, int *index)
3510{
3511 if (sec == &_bfd_elf_large_com_section)
3512 {
3513 *index = SHN_X86_64_LCOMMON;
3514 return TRUE;
3515 }
3516 return FALSE;
3517}
3518
3519/* Process a symbol. */
3520
3521static void
3522elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
3523 asymbol *asym)
3524{
3525 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
3526
3527 switch (elfsym->internal_elf_sym.st_shndx)
3528 {
3529 case SHN_X86_64_LCOMMON:
3530 asym->section = &_bfd_elf_large_com_section;
3531 asym->value = elfsym->internal_elf_sym.st_size;
3532 /* Common symbol doesn't set BSF_GLOBAL. */
3533 asym->flags &= ~BSF_GLOBAL;
3534 break;
3535 }
3536}
3537
3538static bfd_boolean
3539elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
3540{
3541 return (sym->st_shndx == SHN_COMMON
3542 || sym->st_shndx == SHN_X86_64_LCOMMON);
3543}
3544
3545static unsigned int
3546elf64_x86_64_common_section_index (asection *sec)
3547{
3548 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3549 return SHN_COMMON;
3550 else
3551 return SHN_X86_64_LCOMMON;
3552}
3553
3554static asection *
3555elf64_x86_64_common_section (asection *sec)
3556{
3557 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3558 return bfd_com_section_ptr;
3559 else
3560 return &_bfd_elf_large_com_section;
3561}
3562
3563static bfd_boolean
3564elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3565 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
3566 struct elf_link_hash_entry *h,
3567 Elf_Internal_Sym *sym,
00492999 3568 asection **psec,
3b22753a
L
3569 bfd_vma *pvalue ATTRIBUTE_UNUSED,
3570 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
3571 bfd_boolean *skip ATTRIBUTE_UNUSED,
3572 bfd_boolean *override ATTRIBUTE_UNUSED,
3573 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
3574 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
3575 bfd_boolean *newdef ATTRIBUTE_UNUSED,
3576 bfd_boolean *newdyn,
3577 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
3578 bfd_boolean *newweak ATTRIBUTE_UNUSED,
3579 bfd *abfd ATTRIBUTE_UNUSED,
3580 asection **sec,
3581 bfd_boolean *olddef ATTRIBUTE_UNUSED,
3582 bfd_boolean *olddyn,
3583 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
3584 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
00492999 3585 bfd *oldbfd,
3b22753a
L
3586 asection **oldsec)
3587{
3588 /* A normal common symbol and a large common symbol result in a
00492999
L
3589 normal common symbol. We turn the large common symbol into a
3590 normal one. */
3b22753a
L
3591 if (!*olddyn
3592 && h->root.type == bfd_link_hash_common
3593 && !*newdyn
3594 && bfd_is_com_section (*sec)
00492999 3595 && *oldsec != *sec)
3b22753a 3596 {
00492999
L
3597 if (sym->st_shndx == SHN_COMMON
3598 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
3599 {
3600 h->root.u.c.p->section
3601 = bfd_make_section_old_way (oldbfd, "COMMON");
3602 h->root.u.c.p->section->flags = SEC_ALLOC;
3603 }
3604 else if (sym->st_shndx == SHN_X86_64_LCOMMON
3605 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
9a2e389a 3606 *psec = *sec = bfd_com_section_ptr;
3b22753a
L
3607 }
3608
3609 return TRUE;
3610}
3611
3612static int
a6b96beb
AM
3613elf64_x86_64_additional_program_headers (bfd *abfd,
3614 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3b22753a
L
3615{
3616 asection *s;
9a2e389a 3617 int count = 0;
3b22753a
L
3618
3619 /* Check to see if we need a large readonly segment. */
3620 s = bfd_get_section_by_name (abfd, ".lrodata");
3621 if (s && (s->flags & SEC_LOAD))
3622 count++;
3623
3624 /* Check to see if we need a large data segment. Since .lbss sections
3625 is placed right after the .bss section, there should be no need for
3626 a large data segment just because of .lbss. */
3627 s = bfd_get_section_by_name (abfd, ".ldata");
3628 if (s && (s->flags & SEC_LOAD))
3629 count++;
3630
3631 return count;
3632}
3633
fdc90cb4
JJ
3634/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3635
3636static bfd_boolean
3637elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
3638{
3639 if (h->plt.offset != (bfd_vma) -1
3640 && !h->def_regular
3641 && !h->pointer_equality_needed)
3642 return FALSE;
3643
3644 return _bfd_elf_hash_symbol (h);
3645}
3646
9a2e389a 3647static const struct bfd_elf_special_section
3b22753a
L
3648 elf64_x86_64_special_sections[]=
3649{
0112cd26
NC
3650 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3651 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3652 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
3653 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3654 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3655 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3656 { NULL, 0, 0, 0, 0 }
3b22753a
L
3657};
3658
70256ad8
AJ
3659#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3660#define TARGET_LITTLE_NAME "elf64-x86-64"
3661#define ELF_ARCH bfd_arch_i386
3662#define ELF_MACHINE_CODE EM_X86_64
f7661549 3663#define ELF_MAXPAGESIZE 0x200000
2043964e 3664#define ELF_MINPAGESIZE 0x1000
24718e3b 3665#define ELF_COMMONPAGESIZE 0x1000
70256ad8
AJ
3666
3667#define elf_backend_can_gc_sections 1
51b64d56 3668#define elf_backend_can_refcount 1
70256ad8
AJ
3669#define elf_backend_want_got_plt 1
3670#define elf_backend_plt_readonly 1
3671#define elf_backend_want_plt_sym 0
3672#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
b491616a 3673#define elf_backend_rela_normal 1
70256ad8
AJ
3674
3675#define elf_info_to_howto elf64_x86_64_info_to_howto
70256ad8 3676
70256ad8
AJ
3677#define bfd_elf64_bfd_link_hash_table_create \
3678 elf64_x86_64_link_hash_table_create
407443a3 3679#define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
157090f7
AM
3680#define bfd_elf64_bfd_reloc_name_lookup \
3681 elf64_x86_64_reloc_name_lookup
70256ad8
AJ
3682
3683#define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3684#define elf_backend_check_relocs elf64_x86_64_check_relocs
c434dee6
AJ
3685#define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3686#define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3687#define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
70256ad8
AJ
3688#define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3689#define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3690#define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3bab7989
ML
3691#define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3692#define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
c434dee6 3693#define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
70256ad8
AJ
3694#define elf_backend_relocate_section elf64_x86_64_relocate_section
3695#define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
67a4f2b7 3696#define elf_backend_always_size_sections elf64_x86_64_always_size_sections
74541ad4 3697#define elf_backend_init_index_section _bfd_elf_init_1_index_section
4c45e5c9 3698#define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
407443a3 3699#define elf_backend_object_p elf64_x86_64_elf_object_p
bffbf940 3700#define bfd_elf64_mkobject elf64_x86_64_mkobject
8d88c4ca 3701
d2b2c203
DJ
3702#define elf_backend_section_from_shdr \
3703 elf64_x86_64_section_from_shdr
3704
3b22753a
L
3705#define elf_backend_section_from_bfd_section \
3706 elf64_x86_64_elf_section_from_bfd_section
3707#define elf_backend_add_symbol_hook \
3708 elf64_x86_64_add_symbol_hook
3709#define elf_backend_symbol_processing \
3710 elf64_x86_64_symbol_processing
3711#define elf_backend_common_section_index \
3712 elf64_x86_64_common_section_index
3713#define elf_backend_common_section \
3714 elf64_x86_64_common_section
3715#define elf_backend_common_definition \
3716 elf64_x86_64_common_definition
3717#define elf_backend_merge_symbol \
3718 elf64_x86_64_merge_symbol
3719#define elf_backend_special_sections \
3720 elf64_x86_64_special_sections
3721#define elf_backend_additional_program_headers \
3722 elf64_x86_64_additional_program_headers
fdc90cb4
JJ
3723#define elf_backend_hash_symbol \
3724 elf64_x86_64_hash_symbol
3b22753a 3725
8d88c4ca 3726#include "elf64-target.h"
9d7cbccd
NC
3727
3728/* FreeBSD support. */
3729
3730#undef TARGET_LITTLE_SYM
3731#define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
3732#undef TARGET_LITTLE_NAME
3733#define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
3734
d1036acb
L
3735#undef ELF_OSABI
3736#define ELF_OSABI ELFOSABI_FREEBSD
9d7cbccd
NC
3737
3738#undef elf_backend_post_process_headers
d1036acb 3739#define elf_backend_post_process_headers _bfd_elf_set_osabi
9d7cbccd
NC
3740
3741#undef elf64_bed
3742#define elf64_bed elf64_x86_64_fbsd_bed
3743
3744#include "elf64-target.h"
This page took 0.565862 seconds and 4 git commands to generate.