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