1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>.
6 This file is part of BFD, the Binary File Descriptor library.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
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
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 #include "bfd_stdint.h"
30 #include "elf/x86-64.h"
32 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
33 #define MINUS_ONE (~ (bfd_vma) 0)
35 /* The relocation "howto" table. Order of fields:
36 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
37 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
38 static reloc_howto_type x86_64_elf_howto_table
[] =
40 HOWTO(R_X86_64_NONE
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
41 bfd_elf_generic_reloc
, "R_X86_64_NONE", FALSE
, 0x00000000, 0x00000000,
43 HOWTO(R_X86_64_64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
44 bfd_elf_generic_reloc
, "R_X86_64_64", FALSE
, MINUS_ONE
, MINUS_ONE
,
46 HOWTO(R_X86_64_PC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
47 bfd_elf_generic_reloc
, "R_X86_64_PC32", FALSE
, 0xffffffff, 0xffffffff,
49 HOWTO(R_X86_64_GOT32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
50 bfd_elf_generic_reloc
, "R_X86_64_GOT32", FALSE
, 0xffffffff, 0xffffffff,
52 HOWTO(R_X86_64_PLT32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
53 bfd_elf_generic_reloc
, "R_X86_64_PLT32", FALSE
, 0xffffffff, 0xffffffff,
55 HOWTO(R_X86_64_COPY
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
56 bfd_elf_generic_reloc
, "R_X86_64_COPY", FALSE
, 0xffffffff, 0xffffffff,
58 HOWTO(R_X86_64_GLOB_DAT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
59 bfd_elf_generic_reloc
, "R_X86_64_GLOB_DAT", FALSE
, MINUS_ONE
,
61 HOWTO(R_X86_64_JUMP_SLOT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
62 bfd_elf_generic_reloc
, "R_X86_64_JUMP_SLOT", FALSE
, MINUS_ONE
,
64 HOWTO(R_X86_64_RELATIVE
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
65 bfd_elf_generic_reloc
, "R_X86_64_RELATIVE", FALSE
, MINUS_ONE
,
67 HOWTO(R_X86_64_GOTPCREL
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
68 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL", FALSE
, 0xffffffff,
70 HOWTO(R_X86_64_32
, 0, 2, 32, FALSE
, 0, complain_overflow_unsigned
,
71 bfd_elf_generic_reloc
, "R_X86_64_32", FALSE
, 0xffffffff, 0xffffffff,
73 HOWTO(R_X86_64_32S
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
74 bfd_elf_generic_reloc
, "R_X86_64_32S", FALSE
, 0xffffffff, 0xffffffff,
76 HOWTO(R_X86_64_16
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
,
77 bfd_elf_generic_reloc
, "R_X86_64_16", FALSE
, 0xffff, 0xffff, FALSE
),
78 HOWTO(R_X86_64_PC16
,0, 1, 16, TRUE
, 0, complain_overflow_bitfield
,
79 bfd_elf_generic_reloc
, "R_X86_64_PC16", FALSE
, 0xffff, 0xffff, TRUE
),
80 HOWTO(R_X86_64_8
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
,
81 bfd_elf_generic_reloc
, "R_X86_64_8", FALSE
, 0xff, 0xff, FALSE
),
82 HOWTO(R_X86_64_PC8
, 0, 0, 8, TRUE
, 0, complain_overflow_signed
,
83 bfd_elf_generic_reloc
, "R_X86_64_PC8", FALSE
, 0xff, 0xff, TRUE
),
84 HOWTO(R_X86_64_DTPMOD64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
85 bfd_elf_generic_reloc
, "R_X86_64_DTPMOD64", FALSE
, MINUS_ONE
,
87 HOWTO(R_X86_64_DTPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
88 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF64", FALSE
, MINUS_ONE
,
90 HOWTO(R_X86_64_TPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
91 bfd_elf_generic_reloc
, "R_X86_64_TPOFF64", FALSE
, MINUS_ONE
,
93 HOWTO(R_X86_64_TLSGD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
94 bfd_elf_generic_reloc
, "R_X86_64_TLSGD", FALSE
, 0xffffffff,
96 HOWTO(R_X86_64_TLSLD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
97 bfd_elf_generic_reloc
, "R_X86_64_TLSLD", FALSE
, 0xffffffff,
99 HOWTO(R_X86_64_DTPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
100 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF32", FALSE
, 0xffffffff,
102 HOWTO(R_X86_64_GOTTPOFF
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
103 bfd_elf_generic_reloc
, "R_X86_64_GOTTPOFF", FALSE
, 0xffffffff,
105 HOWTO(R_X86_64_TPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
106 bfd_elf_generic_reloc
, "R_X86_64_TPOFF32", FALSE
, 0xffffffff,
108 HOWTO(R_X86_64_PC64
, 0, 4, 64, TRUE
, 0, complain_overflow_bitfield
,
109 bfd_elf_generic_reloc
, "R_X86_64_PC64", FALSE
, MINUS_ONE
, MINUS_ONE
,
111 HOWTO(R_X86_64_GOTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
112 bfd_elf_generic_reloc
, "R_X86_64_GOTOFF64",
113 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
114 HOWTO(R_X86_64_GOTPC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
115 bfd_elf_generic_reloc
, "R_X86_64_GOTPC32",
116 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
117 HOWTO(R_X86_64_GOT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
118 bfd_elf_generic_reloc
, "R_X86_64_GOT64", FALSE
, MINUS_ONE
, MINUS_ONE
,
120 HOWTO(R_X86_64_GOTPCREL64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
121 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL64", FALSE
, MINUS_ONE
,
123 HOWTO(R_X86_64_GOTPC64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
124 bfd_elf_generic_reloc
, "R_X86_64_GOTPC64",
125 FALSE
, MINUS_ONE
, MINUS_ONE
, TRUE
),
126 HOWTO(R_X86_64_GOTPLT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
127 bfd_elf_generic_reloc
, "R_X86_64_GOTPLT64", FALSE
, MINUS_ONE
,
129 HOWTO(R_X86_64_PLTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
130 bfd_elf_generic_reloc
, "R_X86_64_PLTOFF64", FALSE
, MINUS_ONE
,
134 HOWTO(R_X86_64_GOTPC32_TLSDESC
, 0, 2, 32, TRUE
, 0,
135 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
136 "R_X86_64_GOTPC32_TLSDESC",
137 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
138 HOWTO(R_X86_64_TLSDESC_CALL
, 0, 0, 0, FALSE
, 0,
139 complain_overflow_dont
, bfd_elf_generic_reloc
,
140 "R_X86_64_TLSDESC_CALL",
142 HOWTO(R_X86_64_TLSDESC
, 0, 4, 64, FALSE
, 0,
143 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
145 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
147 /* We have a gap in the reloc numbers here.
148 R_X86_64_standard counts the number up to this point, and
149 R_X86_64_vt_offset is the value to subtract from a reloc type of
150 R_X86_64_GNU_VT* to form an index into this table. */
151 #define R_X86_64_standard (R_X86_64_TLSDESC + 1)
152 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
154 /* GNU extension to record C++ vtable hierarchy. */
155 HOWTO (R_X86_64_GNU_VTINHERIT
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
156 NULL
, "R_X86_64_GNU_VTINHERIT", FALSE
, 0, 0, FALSE
),
158 /* GNU extension to record C++ vtable member usage. */
159 HOWTO (R_X86_64_GNU_VTENTRY
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
160 _bfd_elf_rel_vtable_reloc_fn
, "R_X86_64_GNU_VTENTRY", FALSE
, 0, 0,
164 #define IS_X86_64_PCREL_TYPE(TYPE) \
165 ( ((TYPE) == R_X86_64_PC8) \
166 || ((TYPE) == R_X86_64_PC16) \
167 || ((TYPE) == R_X86_64_PC32) \
168 || ((TYPE) == R_X86_64_PC64))
170 /* Map BFD relocs to the x86_64 elf relocs. */
173 bfd_reloc_code_real_type bfd_reloc_val
;
174 unsigned char elf_reloc_val
;
177 static const struct elf_reloc_map x86_64_reloc_map
[] =
179 { BFD_RELOC_NONE
, R_X86_64_NONE
, },
180 { BFD_RELOC_64
, R_X86_64_64
, },
181 { BFD_RELOC_32_PCREL
, R_X86_64_PC32
, },
182 { BFD_RELOC_X86_64_GOT32
, R_X86_64_GOT32
,},
183 { BFD_RELOC_X86_64_PLT32
, R_X86_64_PLT32
,},
184 { BFD_RELOC_X86_64_COPY
, R_X86_64_COPY
, },
185 { BFD_RELOC_X86_64_GLOB_DAT
, R_X86_64_GLOB_DAT
, },
186 { BFD_RELOC_X86_64_JUMP_SLOT
, R_X86_64_JUMP_SLOT
, },
187 { BFD_RELOC_X86_64_RELATIVE
, R_X86_64_RELATIVE
, },
188 { BFD_RELOC_X86_64_GOTPCREL
, R_X86_64_GOTPCREL
, },
189 { BFD_RELOC_32
, R_X86_64_32
, },
190 { BFD_RELOC_X86_64_32S
, R_X86_64_32S
, },
191 { BFD_RELOC_16
, R_X86_64_16
, },
192 { BFD_RELOC_16_PCREL
, R_X86_64_PC16
, },
193 { BFD_RELOC_8
, R_X86_64_8
, },
194 { BFD_RELOC_8_PCREL
, R_X86_64_PC8
, },
195 { BFD_RELOC_X86_64_DTPMOD64
, R_X86_64_DTPMOD64
, },
196 { BFD_RELOC_X86_64_DTPOFF64
, R_X86_64_DTPOFF64
, },
197 { BFD_RELOC_X86_64_TPOFF64
, R_X86_64_TPOFF64
, },
198 { BFD_RELOC_X86_64_TLSGD
, R_X86_64_TLSGD
, },
199 { BFD_RELOC_X86_64_TLSLD
, R_X86_64_TLSLD
, },
200 { BFD_RELOC_X86_64_DTPOFF32
, R_X86_64_DTPOFF32
, },
201 { BFD_RELOC_X86_64_GOTTPOFF
, R_X86_64_GOTTPOFF
, },
202 { BFD_RELOC_X86_64_TPOFF32
, R_X86_64_TPOFF32
, },
203 { BFD_RELOC_64_PCREL
, R_X86_64_PC64
, },
204 { BFD_RELOC_X86_64_GOTOFF64
, R_X86_64_GOTOFF64
, },
205 { BFD_RELOC_X86_64_GOTPC32
, R_X86_64_GOTPC32
, },
206 { BFD_RELOC_X86_64_GOT64
, R_X86_64_GOT64
, },
207 { BFD_RELOC_X86_64_GOTPCREL64
,R_X86_64_GOTPCREL64
, },
208 { BFD_RELOC_X86_64_GOTPC64
, R_X86_64_GOTPC64
, },
209 { BFD_RELOC_X86_64_GOTPLT64
, R_X86_64_GOTPLT64
, },
210 { BFD_RELOC_X86_64_PLTOFF64
, R_X86_64_PLTOFF64
, },
211 { BFD_RELOC_X86_64_GOTPC32_TLSDESC
, R_X86_64_GOTPC32_TLSDESC
, },
212 { BFD_RELOC_X86_64_TLSDESC_CALL
, R_X86_64_TLSDESC_CALL
, },
213 { BFD_RELOC_X86_64_TLSDESC
, R_X86_64_TLSDESC
, },
214 { BFD_RELOC_VTABLE_INHERIT
, R_X86_64_GNU_VTINHERIT
, },
215 { BFD_RELOC_VTABLE_ENTRY
, R_X86_64_GNU_VTENTRY
, },
218 static reloc_howto_type
*
219 elf64_x86_64_rtype_to_howto (bfd
*abfd
, unsigned r_type
)
223 if (r_type
< (unsigned int) R_X86_64_GNU_VTINHERIT
224 || r_type
>= (unsigned int) R_X86_64_max
)
226 if (r_type
>= (unsigned int) R_X86_64_standard
)
228 (*_bfd_error_handler
) (_("%B: invalid relocation type %d"),
230 r_type
= R_X86_64_NONE
;
235 i
= r_type
- (unsigned int) R_X86_64_vt_offset
;
236 BFD_ASSERT (x86_64_elf_howto_table
[i
].type
== r_type
);
237 return &x86_64_elf_howto_table
[i
];
240 /* Given a BFD reloc type, return a HOWTO structure. */
241 static reloc_howto_type
*
242 elf64_x86_64_reloc_type_lookup (bfd
*abfd
,
243 bfd_reloc_code_real_type code
)
247 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
250 if (x86_64_reloc_map
[i
].bfd_reloc_val
== code
)
251 return elf64_x86_64_rtype_to_howto (abfd
,
252 x86_64_reloc_map
[i
].elf_reloc_val
);
257 static reloc_howto_type
*
258 elf64_x86_64_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
264 i
< (sizeof (x86_64_elf_howto_table
)
265 / sizeof (x86_64_elf_howto_table
[0]));
267 if (x86_64_elf_howto_table
[i
].name
!= NULL
268 && strcasecmp (x86_64_elf_howto_table
[i
].name
, r_name
) == 0)
269 return &x86_64_elf_howto_table
[i
];
274 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
277 elf64_x86_64_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
278 Elf_Internal_Rela
*dst
)
282 r_type
= ELF64_R_TYPE (dst
->r_info
);
283 cache_ptr
->howto
= elf64_x86_64_rtype_to_howto (abfd
, r_type
);
284 BFD_ASSERT (r_type
== cache_ptr
->howto
->type
);
287 /* Support for core dump NOTE sections. */
289 elf64_x86_64_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
294 switch (note
->descsz
)
299 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
301 elf_tdata (abfd
)->core_signal
302 = bfd_get_16 (abfd
, note
->descdata
+ 12);
305 elf_tdata (abfd
)->core_pid
306 = bfd_get_32 (abfd
, note
->descdata
+ 32);
315 /* Make a ".reg/999" section. */
316 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
317 size
, note
->descpos
+ offset
);
321 elf64_x86_64_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
323 switch (note
->descsz
)
328 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
329 elf_tdata (abfd
)->core_program
330 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 40, 16);
331 elf_tdata (abfd
)->core_command
332 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 56, 80);
335 /* Note that for some reason, a spurious space is tacked
336 onto the end of the args in some (at least one anyway)
337 implementations, so strip it off if it exists. */
340 char *command
= elf_tdata (abfd
)->core_command
;
341 int n
= strlen (command
);
343 if (0 < n
&& command
[n
- 1] == ' ')
344 command
[n
- 1] = '\0';
350 /* Functions for the x86-64 ELF linker. */
352 /* The name of the dynamic interpreter. This is put in the .interp
355 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
357 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
358 copying dynamic variables from a shared lib into an app's dynbss
359 section, and instead use a dynamic relocation to point into the
361 #define ELIMINATE_COPY_RELOCS 1
363 /* The size in bytes of an entry in the global offset table. */
365 #define GOT_ENTRY_SIZE 8
367 /* The size in bytes of an entry in the procedure linkage table. */
369 #define PLT_ENTRY_SIZE 16
371 /* The first entry in a procedure linkage table looks like this. See the
372 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
374 static const bfd_byte elf64_x86_64_plt0_entry
[PLT_ENTRY_SIZE
] =
376 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
377 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
378 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
381 /* Subsequent entries in a procedure linkage table look like this. */
383 static const bfd_byte elf64_x86_64_plt_entry
[PLT_ENTRY_SIZE
] =
385 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
386 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
387 0x68, /* pushq immediate */
388 0, 0, 0, 0, /* replaced with index into relocation table. */
389 0xe9, /* jmp relative */
390 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
393 /* The x86-64 linker needs to keep track of the number of relocs that
394 it decides to copy as dynamic relocs in check_relocs for each symbol.
395 This is so that it can later discard them if they are found to be
396 unnecessary. We store the information in a field extending the
397 regular ELF linker hash table. */
399 struct elf64_x86_64_dyn_relocs
402 struct elf64_x86_64_dyn_relocs
*next
;
404 /* The input section of the reloc. */
407 /* Total number of relocs copied for the input section. */
410 /* Number of pc-relative relocs copied for the input section. */
411 bfd_size_type pc_count
;
414 /* x86-64 ELF linker hash entry. */
416 struct elf64_x86_64_link_hash_entry
418 struct elf_link_hash_entry elf
;
420 /* Track dynamic relocs copied for this symbol. */
421 struct elf64_x86_64_dyn_relocs
*dyn_relocs
;
423 #define GOT_UNKNOWN 0
427 #define GOT_TLS_GDESC 4
428 #define GOT_TLS_GD_BOTH_P(type) \
429 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
430 #define GOT_TLS_GD_P(type) \
431 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
432 #define GOT_TLS_GDESC_P(type) \
433 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
434 #define GOT_TLS_GD_ANY_P(type) \
435 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
436 unsigned char tls_type
;
438 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
439 starting at the end of the jump table. */
443 #define elf64_x86_64_hash_entry(ent) \
444 ((struct elf64_x86_64_link_hash_entry *)(ent))
446 struct elf64_x86_64_obj_tdata
448 struct elf_obj_tdata root
;
450 /* tls_type for each local got entry. */
451 char *local_got_tls_type
;
453 /* GOTPLT entries for TLS descriptors. */
454 bfd_vma
*local_tlsdesc_gotent
;
457 #define elf64_x86_64_tdata(abfd) \
458 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
460 #define elf64_x86_64_local_got_tls_type(abfd) \
461 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
463 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
464 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
466 #define is_x86_64_elf(bfd) \
467 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
468 && elf_tdata (bfd) != NULL \
469 && elf_object_id (bfd) == X86_64_ELF_TDATA)
472 elf64_x86_64_mkobject (bfd
*abfd
)
474 return bfd_elf_allocate_object (abfd
, sizeof (struct elf64_x86_64_obj_tdata
),
478 /* x86-64 ELF linker hash table. */
480 struct elf64_x86_64_link_hash_table
482 struct elf_link_hash_table elf
;
484 /* Short-cuts to get to dynamic linker sections. */
493 /* The offset into splt of the PLT entry for the TLS descriptor
494 resolver. Special values are 0, if not necessary (or not found
495 to be necessary yet), and -1 if needed but not determined
498 /* The offset into sgot of the GOT entry used by the PLT entry
503 bfd_signed_vma refcount
;
507 /* The amount of space used by the jump slots in the GOT. */
508 bfd_vma sgotplt_jump_table_size
;
510 /* Small local sym to section mapping cache. */
511 struct sym_sec_cache sym_sec
;
513 /* _TLS_MODULE_BASE_ symbol. */
514 struct bfd_link_hash_entry
*tls_module_base
;
517 /* Get the x86-64 ELF linker hash table from a link_info structure. */
519 #define elf64_x86_64_hash_table(p) \
520 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
522 #define elf64_x86_64_compute_jump_table_size(htab) \
523 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
525 /* Create an entry in an x86-64 ELF linker hash table. */
527 static struct bfd_hash_entry
*
528 link_hash_newfunc (struct bfd_hash_entry
*entry
, struct bfd_hash_table
*table
,
531 /* Allocate the structure if it has not already been allocated by a
535 entry
= bfd_hash_allocate (table
,
536 sizeof (struct elf64_x86_64_link_hash_entry
));
541 /* Call the allocation method of the superclass. */
542 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
545 struct elf64_x86_64_link_hash_entry
*eh
;
547 eh
= (struct elf64_x86_64_link_hash_entry
*) entry
;
548 eh
->dyn_relocs
= NULL
;
549 eh
->tls_type
= GOT_UNKNOWN
;
550 eh
->tlsdesc_got
= (bfd_vma
) -1;
556 /* Create an X86-64 ELF linker hash table. */
558 static struct bfd_link_hash_table
*
559 elf64_x86_64_link_hash_table_create (bfd
*abfd
)
561 struct elf64_x86_64_link_hash_table
*ret
;
562 bfd_size_type amt
= sizeof (struct elf64_x86_64_link_hash_table
);
564 ret
= (struct elf64_x86_64_link_hash_table
*) bfd_malloc (amt
);
568 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
569 sizeof (struct elf64_x86_64_link_hash_entry
)))
582 ret
->sym_sec
.abfd
= NULL
;
583 ret
->tlsdesc_plt
= 0;
584 ret
->tlsdesc_got
= 0;
585 ret
->tls_ld_got
.refcount
= 0;
586 ret
->sgotplt_jump_table_size
= 0;
587 ret
->tls_module_base
= NULL
;
589 return &ret
->elf
.root
;
592 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
593 shortcuts to them in our hash table. */
596 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
598 struct elf64_x86_64_link_hash_table
*htab
;
600 if (! _bfd_elf_create_got_section (dynobj
, info
))
603 htab
= elf64_x86_64_hash_table (info
);
604 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
605 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
606 if (!htab
->sgot
|| !htab
->sgotplt
)
609 htab
->srelgot
= bfd_make_section_with_flags (dynobj
, ".rela.got",
610 (SEC_ALLOC
| SEC_LOAD
615 if (htab
->srelgot
== NULL
616 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 3))
621 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
622 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
626 elf64_x86_64_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
628 struct elf64_x86_64_link_hash_table
*htab
;
630 htab
= elf64_x86_64_hash_table (info
);
631 if (!htab
->sgot
&& !create_got_section (dynobj
, info
))
634 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
637 htab
->splt
= bfd_get_section_by_name (dynobj
, ".plt");
638 htab
->srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
639 htab
->sdynbss
= bfd_get_section_by_name (dynobj
, ".dynbss");
641 htab
->srelbss
= bfd_get_section_by_name (dynobj
, ".rela.bss");
643 if (!htab
->splt
|| !htab
->srelplt
|| !htab
->sdynbss
644 || (!info
->shared
&& !htab
->srelbss
))
650 /* Copy the extra info we tack onto an elf_link_hash_entry. */
653 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info
*info
,
654 struct elf_link_hash_entry
*dir
,
655 struct elf_link_hash_entry
*ind
)
657 struct elf64_x86_64_link_hash_entry
*edir
, *eind
;
659 edir
= (struct elf64_x86_64_link_hash_entry
*) dir
;
660 eind
= (struct elf64_x86_64_link_hash_entry
*) ind
;
662 if (eind
->dyn_relocs
!= NULL
)
664 if (edir
->dyn_relocs
!= NULL
)
666 struct elf64_x86_64_dyn_relocs
**pp
;
667 struct elf64_x86_64_dyn_relocs
*p
;
669 /* Add reloc counts against the indirect sym to the direct sym
670 list. Merge any entries against the same section. */
671 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
673 struct elf64_x86_64_dyn_relocs
*q
;
675 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
676 if (q
->sec
== p
->sec
)
678 q
->pc_count
+= p
->pc_count
;
679 q
->count
+= p
->count
;
686 *pp
= edir
->dyn_relocs
;
689 edir
->dyn_relocs
= eind
->dyn_relocs
;
690 eind
->dyn_relocs
= NULL
;
693 if (ind
->root
.type
== bfd_link_hash_indirect
694 && dir
->got
.refcount
<= 0)
696 edir
->tls_type
= eind
->tls_type
;
697 eind
->tls_type
= GOT_UNKNOWN
;
700 if (ELIMINATE_COPY_RELOCS
701 && ind
->root
.type
!= bfd_link_hash_indirect
702 && dir
->dynamic_adjusted
)
704 /* If called to transfer flags for a weakdef during processing
705 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
706 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
707 dir
->ref_dynamic
|= ind
->ref_dynamic
;
708 dir
->ref_regular
|= ind
->ref_regular
;
709 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
710 dir
->needs_plt
|= ind
->needs_plt
;
711 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
714 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
718 elf64_x86_64_elf_object_p (bfd
*abfd
)
720 /* Set the right machine number for an x86-64 elf64 file. */
721 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x86_64
);
739 /* Return TRUE if the TLS access code sequence support transition
743 elf64_x86_64_check_tls_transition (bfd
*abfd
, asection
*sec
,
745 Elf_Internal_Shdr
*symtab_hdr
,
746 struct elf_link_hash_entry
**sym_hashes
,
748 const Elf_Internal_Rela
*rel
,
749 const Elf_Internal_Rela
*relend
)
752 unsigned long r_symndx
;
753 struct elf_link_hash_entry
*h
;
756 /* Get the section contents. */
757 if (contents
== NULL
)
759 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
760 contents
= elf_section_data (sec
)->this_hdr
.contents
;
763 /* FIXME: How to better handle error condition? */
764 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
767 /* Cache the section contents for elf_link_input_bfd. */
768 elf_section_data (sec
)->this_hdr
.contents
= contents
;
772 offset
= rel
->r_offset
;
777 if ((rel
+ 1) >= relend
)
780 if (r_type
== R_X86_64_TLSGD
)
782 /* Check transition from GD access model. Only
783 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
784 .word 0x6666; rex64; call __tls_get_addr
785 can transit to different access model. */
787 static x86_64_opcode32 leaq
= { { 0x66, 0x48, 0x8d, 0x3d } },
788 call
= { { 0x66, 0x66, 0x48, 0xe8 } };
790 || (offset
+ 12) > sec
->size
791 || bfd_get_32 (abfd
, contents
+ offset
- 4) != leaq
.i
792 || bfd_get_32 (abfd
, contents
+ offset
+ 4) != call
.i
)
797 /* Check transition from LD access model. Only
798 leaq foo@tlsld(%rip), %rdi;
800 can transit to different access model. */
802 static x86_64_opcode32 ld
= { { 0x48, 0x8d, 0x3d, 0xe8 } };
805 if (offset
< 3 || (offset
+ 9) > sec
->size
)
808 op
.i
= bfd_get_32 (abfd
, contents
+ offset
- 3);
809 op
.c
[3] = bfd_get_8 (abfd
, contents
+ offset
+ 4);
814 r_symndx
= ELF64_R_SYM (rel
[1].r_info
);
815 if (r_symndx
< symtab_hdr
->sh_info
)
818 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
819 /* Use strncmp to check __tls_get_addr since __tls_get_addr
822 && h
->root
.root
.string
!= NULL
823 && (ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PC32
824 || ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PLT32
)
825 && (strncmp (h
->root
.root
.string
,
826 "__tls_get_addr", 14) == 0));
828 case R_X86_64_GOTTPOFF
:
829 /* Check transition from IE access model:
830 movq foo@gottpoff(%rip), %reg
831 addq foo@gottpoff(%rip), %reg
834 if (offset
< 3 || (offset
+ 4) > sec
->size
)
837 val
= bfd_get_8 (abfd
, contents
+ offset
- 3);
838 if (val
!= 0x48 && val
!= 0x4c)
841 val
= bfd_get_8 (abfd
, contents
+ offset
- 2);
842 if (val
!= 0x8b && val
!= 0x03)
845 val
= bfd_get_8 (abfd
, contents
+ offset
- 1);
846 return (val
& 0xc7) == 5;
848 case R_X86_64_GOTPC32_TLSDESC
:
849 /* Check transition from GDesc access model:
850 leaq x@tlsdesc(%rip), %rax
852 Make sure it's a leaq adding rip to a 32-bit offset
853 into any register, although it's probably almost always
856 if (offset
< 3 || (offset
+ 4) > sec
->size
)
859 val
= bfd_get_8 (abfd
, contents
+ offset
- 3);
860 if ((val
& 0xfb) != 0x48)
863 if (bfd_get_8 (abfd
, contents
+ offset
- 2) != 0x8d)
866 val
= bfd_get_8 (abfd
, contents
+ offset
- 1);
867 return (val
& 0xc7) == 0x05;
869 case R_X86_64_TLSDESC_CALL
:
870 /* Check transition from GDesc access model:
871 call *x@tlsdesc(%rax)
873 if (offset
+ 2 <= sec
->size
)
875 /* Make sure that it's a call *x@tlsdesc(%rax). */
876 static x86_64_opcode16 call
= { { 0xff, 0x10 } };
877 return bfd_get_16 (abfd
, contents
+ offset
) == call
.i
;
887 /* Return TRUE if the TLS access transition is OK or no transition
888 will be performed. Update R_TYPE if there is a transition. */
891 elf64_x86_64_tls_transition (struct bfd_link_info
*info
, bfd
*abfd
,
892 asection
*sec
, bfd_byte
*contents
,
893 Elf_Internal_Shdr
*symtab_hdr
,
894 struct elf_link_hash_entry
**sym_hashes
,
895 unsigned int *r_type
, int tls_type
,
896 const Elf_Internal_Rela
*rel
,
897 const Elf_Internal_Rela
*relend
,
898 struct elf_link_hash_entry
*h
)
900 unsigned int from_type
= *r_type
;
901 unsigned int to_type
= from_type
;
902 bfd_boolean check
= TRUE
;
907 case R_X86_64_GOTPC32_TLSDESC
:
908 case R_X86_64_TLSDESC_CALL
:
909 case R_X86_64_GOTTPOFF
:
913 to_type
= R_X86_64_TPOFF32
;
915 to_type
= R_X86_64_GOTTPOFF
;
918 /* When we are called from elf64_x86_64_relocate_section,
919 CONTENTS isn't NULL and there may be additional transitions
920 based on TLS_TYPE. */
921 if (contents
!= NULL
)
923 unsigned int new_to_type
= to_type
;
928 && tls_type
== GOT_TLS_IE
)
929 new_to_type
= R_X86_64_TPOFF32
;
931 if (to_type
== R_X86_64_TLSGD
932 || to_type
== R_X86_64_GOTPC32_TLSDESC
933 || to_type
== R_X86_64_TLSDESC_CALL
)
935 if (tls_type
== GOT_TLS_IE
)
936 new_to_type
= R_X86_64_GOTTPOFF
;
939 /* We checked the transition before when we were called from
940 elf64_x86_64_check_relocs. We only want to check the new
941 transition which hasn't been checked before. */
942 check
= new_to_type
!= to_type
&& from_type
== to_type
;
943 to_type
= new_to_type
;
950 to_type
= R_X86_64_TPOFF32
;
957 /* Return TRUE if there is no transition. */
958 if (from_type
== to_type
)
961 /* Check if the transition can be performed. */
963 && ! elf64_x86_64_check_tls_transition (abfd
, sec
, contents
,
964 symtab_hdr
, sym_hashes
,
965 from_type
, rel
, relend
))
967 reloc_howto_type
*from
, *to
;
969 from
= elf64_x86_64_rtype_to_howto (abfd
, from_type
);
970 to
= elf64_x86_64_rtype_to_howto (abfd
, to_type
);
972 (*_bfd_error_handler
)
973 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
974 "in section `%A' failed"),
975 abfd
, sec
, from
->name
, to
->name
,
976 h
? h
->root
.root
.string
: "a local symbol",
977 (unsigned long) rel
->r_offset
);
978 bfd_set_error (bfd_error_bad_value
);
986 /* Look through the relocs for a section during the first phase, and
987 calculate needed space in the global offset table, procedure
988 linkage table, and dynamic reloc sections. */
991 elf64_x86_64_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
993 const Elf_Internal_Rela
*relocs
)
995 struct elf64_x86_64_link_hash_table
*htab
;
996 Elf_Internal_Shdr
*symtab_hdr
;
997 struct elf_link_hash_entry
**sym_hashes
;
998 const Elf_Internal_Rela
*rel
;
999 const Elf_Internal_Rela
*rel_end
;
1002 if (info
->relocatable
)
1005 BFD_ASSERT (is_x86_64_elf (abfd
));
1007 htab
= elf64_x86_64_hash_table (info
);
1008 symtab_hdr
= &elf_symtab_hdr (abfd
);
1009 sym_hashes
= elf_sym_hashes (abfd
);
1013 rel_end
= relocs
+ sec
->reloc_count
;
1014 for (rel
= relocs
; rel
< rel_end
; rel
++)
1016 unsigned int r_type
;
1017 unsigned long r_symndx
;
1018 struct elf_link_hash_entry
*h
;
1020 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1021 r_type
= ELF64_R_TYPE (rel
->r_info
);
1023 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
1025 (*_bfd_error_handler
) (_("%B: bad symbol index: %d"),
1030 if (r_symndx
< symtab_hdr
->sh_info
)
1034 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1035 while (h
->root
.type
== bfd_link_hash_indirect
1036 || h
->root
.type
== bfd_link_hash_warning
)
1037 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1040 if (! elf64_x86_64_tls_transition (info
, abfd
, sec
, NULL
,
1041 symtab_hdr
, sym_hashes
,
1042 &r_type
, GOT_UNKNOWN
,
1048 case R_X86_64_TLSLD
:
1049 htab
->tls_ld_got
.refcount
+= 1;
1052 case R_X86_64_TPOFF32
:
1055 (*_bfd_error_handler
)
1056 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1058 x86_64_elf_howto_table
[r_type
].name
,
1059 (h
) ? h
->root
.root
.string
: "a local symbol");
1060 bfd_set_error (bfd_error_bad_value
);
1065 case R_X86_64_GOTTPOFF
:
1067 info
->flags
|= DF_STATIC_TLS
;
1070 case R_X86_64_GOT32
:
1071 case R_X86_64_GOTPCREL
:
1072 case R_X86_64_TLSGD
:
1073 case R_X86_64_GOT64
:
1074 case R_X86_64_GOTPCREL64
:
1075 case R_X86_64_GOTPLT64
:
1076 case R_X86_64_GOTPC32_TLSDESC
:
1077 case R_X86_64_TLSDESC_CALL
:
1078 /* This symbol requires a global offset table entry. */
1080 int tls_type
, old_tls_type
;
1084 default: tls_type
= GOT_NORMAL
; break;
1085 case R_X86_64_TLSGD
: tls_type
= GOT_TLS_GD
; break;
1086 case R_X86_64_GOTTPOFF
: tls_type
= GOT_TLS_IE
; break;
1087 case R_X86_64_GOTPC32_TLSDESC
:
1088 case R_X86_64_TLSDESC_CALL
:
1089 tls_type
= GOT_TLS_GDESC
; break;
1094 if (r_type
== R_X86_64_GOTPLT64
)
1096 /* This relocation indicates that we also need
1097 a PLT entry, as this is a function. We don't need
1098 a PLT entry for local symbols. */
1100 h
->plt
.refcount
+= 1;
1102 h
->got
.refcount
+= 1;
1103 old_tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
1107 bfd_signed_vma
*local_got_refcounts
;
1109 /* This is a global offset table entry for a local symbol. */
1110 local_got_refcounts
= elf_local_got_refcounts (abfd
);
1111 if (local_got_refcounts
== NULL
)
1115 size
= symtab_hdr
->sh_info
;
1116 size
*= sizeof (bfd_signed_vma
)
1117 + sizeof (bfd_vma
) + sizeof (char);
1118 local_got_refcounts
= ((bfd_signed_vma
*)
1119 bfd_zalloc (abfd
, size
));
1120 if (local_got_refcounts
== NULL
)
1122 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
1123 elf64_x86_64_local_tlsdesc_gotent (abfd
)
1124 = (bfd_vma
*) (local_got_refcounts
+ symtab_hdr
->sh_info
);
1125 elf64_x86_64_local_got_tls_type (abfd
)
1126 = (char *) (local_got_refcounts
+ 2 * symtab_hdr
->sh_info
);
1128 local_got_refcounts
[r_symndx
] += 1;
1130 = elf64_x86_64_local_got_tls_type (abfd
) [r_symndx
];
1133 /* If a TLS symbol is accessed using IE at least once,
1134 there is no point to use dynamic model for it. */
1135 if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
1136 && (! GOT_TLS_GD_ANY_P (old_tls_type
)
1137 || tls_type
!= GOT_TLS_IE
))
1139 if (old_tls_type
== GOT_TLS_IE
&& GOT_TLS_GD_ANY_P (tls_type
))
1140 tls_type
= old_tls_type
;
1141 else if (GOT_TLS_GD_ANY_P (old_tls_type
)
1142 && GOT_TLS_GD_ANY_P (tls_type
))
1143 tls_type
|= old_tls_type
;
1146 (*_bfd_error_handler
)
1147 (_("%B: '%s' accessed both as normal and thread local symbol"),
1148 abfd
, h
? h
->root
.root
.string
: "<local>");
1153 if (old_tls_type
!= tls_type
)
1156 elf64_x86_64_hash_entry (h
)->tls_type
= tls_type
;
1158 elf64_x86_64_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
1163 case R_X86_64_GOTOFF64
:
1164 case R_X86_64_GOTPC32
:
1165 case R_X86_64_GOTPC64
:
1167 if (htab
->sgot
== NULL
)
1169 if (htab
->elf
.dynobj
== NULL
)
1170 htab
->elf
.dynobj
= abfd
;
1171 if (!create_got_section (htab
->elf
.dynobj
, info
))
1176 case R_X86_64_PLT32
:
1177 /* This symbol requires a procedure linkage table entry. We
1178 actually build the entry in adjust_dynamic_symbol,
1179 because this might be a case of linking PIC code which is
1180 never referenced by a dynamic object, in which case we
1181 don't need to generate a procedure linkage table entry
1184 /* If this is a local symbol, we resolve it directly without
1185 creating a procedure linkage table entry. */
1190 h
->plt
.refcount
+= 1;
1193 case R_X86_64_PLTOFF64
:
1194 /* This tries to form the 'address' of a function relative
1195 to GOT. For global symbols we need a PLT entry. */
1199 h
->plt
.refcount
+= 1;
1207 /* Let's help debug shared library creation. These relocs
1208 cannot be used in shared libs. Don't error out for
1209 sections we don't care about, such as debug sections or
1210 non-constant sections. */
1212 && (sec
->flags
& SEC_ALLOC
) != 0
1213 && (sec
->flags
& SEC_READONLY
) != 0)
1215 (*_bfd_error_handler
)
1216 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1218 x86_64_elf_howto_table
[r_type
].name
,
1219 (h
) ? h
->root
.root
.string
: "a local symbol");
1220 bfd_set_error (bfd_error_bad_value
);
1230 if (h
!= NULL
&& !info
->shared
)
1232 /* If this reloc is in a read-only section, we might
1233 need a copy reloc. We can't check reliably at this
1234 stage whether the section is read-only, as input
1235 sections have not yet been mapped to output sections.
1236 Tentatively set the flag for now, and correct in
1237 adjust_dynamic_symbol. */
1240 /* We may need a .plt entry if the function this reloc
1241 refers to is in a shared lib. */
1242 h
->plt
.refcount
+= 1;
1243 if (r_type
!= R_X86_64_PC32
&& r_type
!= R_X86_64_PC64
)
1244 h
->pointer_equality_needed
= 1;
1247 /* If we are creating a shared library, and this is a reloc
1248 against a global symbol, or a non PC relative reloc
1249 against a local symbol, then we need to copy the reloc
1250 into the shared library. However, if we are linking with
1251 -Bsymbolic, we do not need to copy a reloc against a
1252 global symbol which is defined in an object we are
1253 including in the link (i.e., DEF_REGULAR is set). At
1254 this point we have not seen all the input files, so it is
1255 possible that DEF_REGULAR is not set now but will be set
1256 later (it is never cleared). In case of a weak definition,
1257 DEF_REGULAR may be cleared later by a strong definition in
1258 a shared library. We account for that possibility below by
1259 storing information in the relocs_copied field of the hash
1260 table entry. A similar situation occurs when creating
1261 shared libraries and symbol visibility changes render the
1264 If on the other hand, we are creating an executable, we
1265 may need to keep relocations for symbols satisfied by a
1266 dynamic library if we manage to avoid copy relocs for the
1269 && (sec
->flags
& SEC_ALLOC
) != 0
1270 && (! IS_X86_64_PCREL_TYPE (r_type
)
1272 && (! SYMBOLIC_BIND (info
, h
)
1273 || h
->root
.type
== bfd_link_hash_defweak
1274 || !h
->def_regular
))))
1275 || (ELIMINATE_COPY_RELOCS
1277 && (sec
->flags
& SEC_ALLOC
) != 0
1279 && (h
->root
.type
== bfd_link_hash_defweak
1280 || !h
->def_regular
)))
1282 struct elf64_x86_64_dyn_relocs
*p
;
1283 struct elf64_x86_64_dyn_relocs
**head
;
1285 /* We must copy these reloc types into the output file.
1286 Create a reloc section in dynobj and make room for
1290 if (htab
->elf
.dynobj
== NULL
)
1291 htab
->elf
.dynobj
= abfd
;
1293 sreloc
= _bfd_elf_make_dynamic_reloc_section
1294 (sec
, htab
->elf
.dynobj
, 3, abfd
, /*rela?*/ TRUE
);
1299 /* Create the ifunc section, even if we will not encounter an
1300 indirect function symbol. We may not even see one in the input
1301 object file, but we can still encounter them in libraries. */
1302 (void) _bfd_elf_make_ifunc_reloc_section
1303 (abfd
, sec
, htab
->elf
.dynobj
, 2);
1306 /* If this is a global symbol, we count the number of
1307 relocations we need for this symbol. */
1310 head
= &((struct elf64_x86_64_link_hash_entry
*) h
)->dyn_relocs
;
1315 /* Track dynamic relocs needed for local syms too.
1316 We really need local syms available to do this
1320 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
1325 /* Beware of type punned pointers vs strict aliasing
1327 vpp
= &(elf_section_data (s
)->local_dynrel
);
1328 head
= (struct elf64_x86_64_dyn_relocs
**)vpp
;
1332 if (p
== NULL
|| p
->sec
!= sec
)
1334 bfd_size_type amt
= sizeof *p
;
1336 p
= ((struct elf64_x86_64_dyn_relocs
*)
1337 bfd_alloc (htab
->elf
.dynobj
, amt
));
1348 if (IS_X86_64_PCREL_TYPE (r_type
))
1353 /* This relocation describes the C++ object vtable hierarchy.
1354 Reconstruct it for later use during GC. */
1355 case R_X86_64_GNU_VTINHERIT
:
1356 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
1360 /* This relocation describes which C++ vtable entries are actually
1361 used. Record for later use during GC. */
1362 case R_X86_64_GNU_VTENTRY
:
1363 BFD_ASSERT (h
!= NULL
);
1365 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
1377 /* Return the section that should be marked against GC for a given
1381 elf64_x86_64_gc_mark_hook (asection
*sec
,
1382 struct bfd_link_info
*info
,
1383 Elf_Internal_Rela
*rel
,
1384 struct elf_link_hash_entry
*h
,
1385 Elf_Internal_Sym
*sym
)
1388 switch (ELF64_R_TYPE (rel
->r_info
))
1390 case R_X86_64_GNU_VTINHERIT
:
1391 case R_X86_64_GNU_VTENTRY
:
1395 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
1398 /* Update the got entry reference counts for the section being removed. */
1401 elf64_x86_64_gc_sweep_hook (bfd
*abfd
, struct bfd_link_info
*info
,
1403 const Elf_Internal_Rela
*relocs
)
1405 Elf_Internal_Shdr
*symtab_hdr
;
1406 struct elf_link_hash_entry
**sym_hashes
;
1407 bfd_signed_vma
*local_got_refcounts
;
1408 const Elf_Internal_Rela
*rel
, *relend
;
1410 if (info
->relocatable
)
1413 elf_section_data (sec
)->local_dynrel
= NULL
;
1415 symtab_hdr
= &elf_symtab_hdr (abfd
);
1416 sym_hashes
= elf_sym_hashes (abfd
);
1417 local_got_refcounts
= elf_local_got_refcounts (abfd
);
1419 relend
= relocs
+ sec
->reloc_count
;
1420 for (rel
= relocs
; rel
< relend
; rel
++)
1422 unsigned long r_symndx
;
1423 unsigned int r_type
;
1424 struct elf_link_hash_entry
*h
= NULL
;
1426 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1427 if (r_symndx
>= symtab_hdr
->sh_info
)
1429 struct elf64_x86_64_link_hash_entry
*eh
;
1430 struct elf64_x86_64_dyn_relocs
**pp
;
1431 struct elf64_x86_64_dyn_relocs
*p
;
1433 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1434 while (h
->root
.type
== bfd_link_hash_indirect
1435 || h
->root
.type
== bfd_link_hash_warning
)
1436 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1437 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1439 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
1442 /* Everything must go for SEC. */
1448 r_type
= ELF64_R_TYPE (rel
->r_info
);
1449 if (! elf64_x86_64_tls_transition (info
, abfd
, sec
, NULL
,
1450 symtab_hdr
, sym_hashes
,
1451 &r_type
, GOT_UNKNOWN
,
1457 case R_X86_64_TLSLD
:
1458 if (elf64_x86_64_hash_table (info
)->tls_ld_got
.refcount
> 0)
1459 elf64_x86_64_hash_table (info
)->tls_ld_got
.refcount
-= 1;
1462 case R_X86_64_TLSGD
:
1463 case R_X86_64_GOTPC32_TLSDESC
:
1464 case R_X86_64_TLSDESC_CALL
:
1465 case R_X86_64_GOTTPOFF
:
1466 case R_X86_64_GOT32
:
1467 case R_X86_64_GOTPCREL
:
1468 case R_X86_64_GOT64
:
1469 case R_X86_64_GOTPCREL64
:
1470 case R_X86_64_GOTPLT64
:
1473 if (r_type
== R_X86_64_GOTPLT64
&& h
->plt
.refcount
> 0)
1474 h
->plt
.refcount
-= 1;
1475 if (h
->got
.refcount
> 0)
1476 h
->got
.refcount
-= 1;
1478 else if (local_got_refcounts
!= NULL
)
1480 if (local_got_refcounts
[r_symndx
] > 0)
1481 local_got_refcounts
[r_symndx
] -= 1;
1498 case R_X86_64_PLT32
:
1499 case R_X86_64_PLTOFF64
:
1502 if (h
->plt
.refcount
> 0)
1503 h
->plt
.refcount
-= 1;
1515 /* Adjust a symbol defined by a dynamic object and referenced by a
1516 regular object. The current definition is in some section of the
1517 dynamic object, but we're not including those sections. We have to
1518 change the definition to something the rest of the link can
1522 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info
*info
,
1523 struct elf_link_hash_entry
*h
)
1525 struct elf64_x86_64_link_hash_table
*htab
;
1528 /* If this is a function, put it in the procedure linkage table. We
1529 will fill in the contents of the procedure linkage table later,
1530 when we know the address of the .got section. */
1531 if (h
->type
== STT_FUNC
1534 if (h
->plt
.refcount
<= 0
1535 || SYMBOL_CALLS_LOCAL (info
, h
)
1536 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1537 && h
->root
.type
== bfd_link_hash_undefweak
))
1539 /* This case can occur if we saw a PLT32 reloc in an input
1540 file, but the symbol was never referred to by a dynamic
1541 object, or if all references were garbage collected. In
1542 such a case, we don't actually need to build a procedure
1543 linkage table, and we can just do a PC32 reloc instead. */
1544 h
->plt
.offset
= (bfd_vma
) -1;
1551 /* It's possible that we incorrectly decided a .plt reloc was
1552 needed for an R_X86_64_PC32 reloc to a non-function sym in
1553 check_relocs. We can't decide accurately between function and
1554 non-function syms in check-relocs; Objects loaded later in
1555 the link may change h->type. So fix it now. */
1556 h
->plt
.offset
= (bfd_vma
) -1;
1558 /* If this is a weak symbol, and there is a real definition, the
1559 processor independent code will have arranged for us to see the
1560 real definition first, and we can just use the same value. */
1561 if (h
->u
.weakdef
!= NULL
)
1563 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
1564 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
1565 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
1566 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
1567 if (ELIMINATE_COPY_RELOCS
|| info
->nocopyreloc
)
1568 h
->non_got_ref
= h
->u
.weakdef
->non_got_ref
;
1572 /* This is a reference to a symbol defined by a dynamic object which
1573 is not a function. */
1575 /* If we are creating a shared library, we must presume that the
1576 only references to the symbol are via the global offset table.
1577 For such cases we need not do anything here; the relocations will
1578 be handled correctly by relocate_section. */
1582 /* If there are no references to this symbol that do not use the
1583 GOT, we don't need to generate a copy reloc. */
1584 if (!h
->non_got_ref
)
1587 /* If -z nocopyreloc was given, we won't generate them either. */
1588 if (info
->nocopyreloc
)
1594 if (ELIMINATE_COPY_RELOCS
)
1596 struct elf64_x86_64_link_hash_entry
* eh
;
1597 struct elf64_x86_64_dyn_relocs
*p
;
1599 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1600 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1602 s
= p
->sec
->output_section
;
1603 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1607 /* If we didn't find any dynamic relocs in read-only sections, then
1608 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1618 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
1619 h
->root
.root
.string
);
1623 /* We must allocate the symbol in our .dynbss section, which will
1624 become part of the .bss section of the executable. There will be
1625 an entry for this symbol in the .dynsym section. The dynamic
1626 object will contain position independent code, so all references
1627 from the dynamic object to this symbol will go through the global
1628 offset table. The dynamic linker will use the .dynsym entry to
1629 determine the address it must put in the global offset table, so
1630 both the dynamic object and the regular object will refer to the
1631 same memory location for the variable. */
1633 htab
= elf64_x86_64_hash_table (info
);
1635 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1636 to copy the initial value out of the dynamic object and into the
1637 runtime process image. */
1638 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
1640 htab
->srelbss
->size
+= sizeof (Elf64_External_Rela
);
1646 return _bfd_elf_adjust_dynamic_copy (h
, s
);
1649 /* Allocate space in .plt, .got and associated reloc sections for
1653 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
1655 struct bfd_link_info
*info
;
1656 struct elf64_x86_64_link_hash_table
*htab
;
1657 struct elf64_x86_64_link_hash_entry
*eh
;
1658 struct elf64_x86_64_dyn_relocs
*p
;
1659 bfd_boolean use_indirect_section
= FALSE
;
1661 if (h
->root
.type
== bfd_link_hash_indirect
)
1664 if (h
->root
.type
== bfd_link_hash_warning
)
1665 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1667 info
= (struct bfd_link_info
*) inf
;
1668 htab
= elf64_x86_64_hash_table (info
);
1670 if (htab
->elf
.dynamic_sections_created
1671 && h
->plt
.refcount
> 0)
1673 /* Make sure this symbol is output as a dynamic symbol.
1674 Undefined weak syms won't yet be marked as dynamic. */
1675 if (h
->dynindx
== -1
1676 && !h
->forced_local
)
1678 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1683 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
1685 asection
*s
= htab
->splt
;
1687 /* If this is the first .plt entry, make room for the special
1690 s
->size
+= PLT_ENTRY_SIZE
;
1692 h
->plt
.offset
= s
->size
;
1694 /* If this symbol is not defined in a regular file, and we are
1695 not generating a shared library, then set the symbol to this
1696 location in the .plt. This is required to make function
1697 pointers compare as equal between the normal executable and
1698 the shared library. */
1702 h
->root
.u
.def
.section
= s
;
1703 h
->root
.u
.def
.value
= h
->plt
.offset
;
1706 /* Make room for this entry. */
1707 s
->size
+= PLT_ENTRY_SIZE
;
1709 /* We also need to make an entry in the .got.plt section, which
1710 will be placed in the .got section by the linker script. */
1711 htab
->sgotplt
->size
+= GOT_ENTRY_SIZE
;
1713 /* We also need to make an entry in the .rela.plt section. */
1714 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
1715 htab
->srelplt
->reloc_count
++;
1719 h
->plt
.offset
= (bfd_vma
) -1;
1725 h
->plt
.offset
= (bfd_vma
) -1;
1729 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1730 eh
->tlsdesc_got
= (bfd_vma
) -1;
1732 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1733 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1734 if (h
->got
.refcount
> 0
1737 && elf64_x86_64_hash_entry (h
)->tls_type
== GOT_TLS_IE
)
1739 h
->got
.offset
= (bfd_vma
) -1;
1741 else if (h
->got
.refcount
> 0)
1745 int tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
1747 /* Make sure this symbol is output as a dynamic symbol.
1748 Undefined weak syms won't yet be marked as dynamic. */
1749 if (h
->dynindx
== -1
1750 && !h
->forced_local
)
1752 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1756 if (GOT_TLS_GDESC_P (tls_type
))
1758 eh
->tlsdesc_got
= htab
->sgotplt
->size
1759 - elf64_x86_64_compute_jump_table_size (htab
);
1760 htab
->sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
1761 h
->got
.offset
= (bfd_vma
) -2;
1763 if (! GOT_TLS_GDESC_P (tls_type
)
1764 || GOT_TLS_GD_P (tls_type
))
1767 h
->got
.offset
= s
->size
;
1768 s
->size
+= GOT_ENTRY_SIZE
;
1769 if (GOT_TLS_GD_P (tls_type
))
1770 s
->size
+= GOT_ENTRY_SIZE
;
1772 dyn
= htab
->elf
.dynamic_sections_created
;
1773 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1775 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1776 if ((GOT_TLS_GD_P (tls_type
) && h
->dynindx
== -1)
1777 || tls_type
== GOT_TLS_IE
)
1778 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
1779 else if (GOT_TLS_GD_P (tls_type
))
1780 htab
->srelgot
->size
+= 2 * sizeof (Elf64_External_Rela
);
1781 else if (! GOT_TLS_GDESC_P (tls_type
)
1782 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
1783 || h
->root
.type
!= bfd_link_hash_undefweak
)
1785 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
1786 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
1787 if (GOT_TLS_GDESC_P (tls_type
))
1789 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
1790 htab
->tlsdesc_plt
= (bfd_vma
) -1;
1794 h
->got
.offset
= (bfd_vma
) -1;
1796 if (eh
->dyn_relocs
== NULL
)
1799 /* In the shared -Bsymbolic case, discard space allocated for
1800 dynamic pc-relative relocs against symbols which turn out to be
1801 defined in regular objects. For the normal shared case, discard
1802 space for pc-relative relocs that have become local due to symbol
1803 visibility changes. */
1807 /* Relocs that use pc_count are those that appear on a call
1808 insn, or certain REL relocs that can generated via assembly.
1809 We want calls to protected symbols to resolve directly to the
1810 function rather than going via the plt. If people want
1811 function pointer comparisons to work as expected then they
1812 should avoid writing weird assembly. */
1813 if (SYMBOL_CALLS_LOCAL (info
, h
))
1815 struct elf64_x86_64_dyn_relocs
**pp
;
1817 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
1819 p
->count
-= p
->pc_count
;
1828 /* Also discard relocs on undefined weak syms with non-default
1830 if (eh
->dyn_relocs
!= NULL
1831 && h
->root
.type
== bfd_link_hash_undefweak
)
1833 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
1834 eh
->dyn_relocs
= NULL
;
1836 /* Make sure undefined weak symbols are output as a dynamic
1838 else if (h
->dynindx
== -1
1839 && ! h
->forced_local
1840 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
1844 else if (_bfd_elf_is_ifunc_symbol (info
->output_bfd
, h
)
1846 && ! h
->forced_local
)
1848 if (bfd_elf_link_record_dynamic_symbol (info
, h
)
1849 && h
->dynindx
!= -1)
1850 use_indirect_section
= TRUE
;
1854 else if (ELIMINATE_COPY_RELOCS
)
1856 /* For the non-shared case, discard space for relocs against
1857 symbols which turn out to need copy relocs or are not
1863 || (htab
->elf
.dynamic_sections_created
1864 && (h
->root
.type
== bfd_link_hash_undefweak
1865 || h
->root
.type
== bfd_link_hash_undefined
))))
1867 /* Make sure this symbol is output as a dynamic symbol.
1868 Undefined weak syms won't yet be marked as dynamic. */
1869 if (h
->dynindx
== -1
1870 && ! h
->forced_local
1871 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
1874 /* If that succeeded, we know we'll be keeping all the
1876 if (h
->dynindx
!= -1)
1880 eh
->dyn_relocs
= NULL
;
1885 /* Finally, allocate space. */
1886 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1890 if (use_indirect_section
)
1891 sreloc
= elf_section_data (p
->sec
)->indirect_relocs
;
1893 sreloc
= elf_section_data (p
->sec
)->sreloc
;
1895 BFD_ASSERT (sreloc
!= NULL
);
1897 sreloc
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
1903 /* Find any dynamic relocs that apply to read-only sections. */
1906 readonly_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
1908 struct elf64_x86_64_link_hash_entry
*eh
;
1909 struct elf64_x86_64_dyn_relocs
*p
;
1911 if (h
->root
.type
== bfd_link_hash_warning
)
1912 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1914 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1915 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1917 asection
*s
= p
->sec
->output_section
;
1919 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1921 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
1923 info
->flags
|= DF_TEXTREL
;
1925 /* Not an error, just cut short the traversal. */
1932 /* Set the sizes of the dynamic sections. */
1935 elf64_x86_64_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1936 struct bfd_link_info
*info
)
1938 struct elf64_x86_64_link_hash_table
*htab
;
1944 htab
= elf64_x86_64_hash_table (info
);
1945 dynobj
= htab
->elf
.dynobj
;
1949 if (htab
->elf
.dynamic_sections_created
)
1951 /* Set the contents of the .interp section to the interpreter. */
1952 if (info
->executable
)
1954 s
= bfd_get_section_by_name (dynobj
, ".interp");
1957 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1958 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1962 /* Set up .got offsets for local syms, and space for local dynamic
1964 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
1966 bfd_signed_vma
*local_got
;
1967 bfd_signed_vma
*end_local_got
;
1968 char *local_tls_type
;
1969 bfd_vma
*local_tlsdesc_gotent
;
1970 bfd_size_type locsymcount
;
1971 Elf_Internal_Shdr
*symtab_hdr
;
1974 if (! is_x86_64_elf (ibfd
))
1977 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1979 struct elf64_x86_64_dyn_relocs
*p
;
1981 for (p
= (struct elf64_x86_64_dyn_relocs
*)
1982 (elf_section_data (s
)->local_dynrel
);
1986 if (!bfd_is_abs_section (p
->sec
)
1987 && bfd_is_abs_section (p
->sec
->output_section
))
1989 /* Input section has been discarded, either because
1990 it is a copy of a linkonce section or due to
1991 linker script /DISCARD/, so we'll be discarding
1994 else if (p
->count
!= 0)
1996 srel
= elf_section_data (p
->sec
)->sreloc
;
1997 srel
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
1998 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1999 info
->flags
|= DF_TEXTREL
;
2004 local_got
= elf_local_got_refcounts (ibfd
);
2008 symtab_hdr
= &elf_symtab_hdr (ibfd
);
2009 locsymcount
= symtab_hdr
->sh_info
;
2010 end_local_got
= local_got
+ locsymcount
;
2011 local_tls_type
= elf64_x86_64_local_got_tls_type (ibfd
);
2012 local_tlsdesc_gotent
= elf64_x86_64_local_tlsdesc_gotent (ibfd
);
2014 srel
= htab
->srelgot
;
2015 for (; local_got
< end_local_got
;
2016 ++local_got
, ++local_tls_type
, ++local_tlsdesc_gotent
)
2018 *local_tlsdesc_gotent
= (bfd_vma
) -1;
2021 if (GOT_TLS_GDESC_P (*local_tls_type
))
2023 *local_tlsdesc_gotent
= htab
->sgotplt
->size
2024 - elf64_x86_64_compute_jump_table_size (htab
);
2025 htab
->sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
2026 *local_got
= (bfd_vma
) -2;
2028 if (! GOT_TLS_GDESC_P (*local_tls_type
)
2029 || GOT_TLS_GD_P (*local_tls_type
))
2031 *local_got
= s
->size
;
2032 s
->size
+= GOT_ENTRY_SIZE
;
2033 if (GOT_TLS_GD_P (*local_tls_type
))
2034 s
->size
+= GOT_ENTRY_SIZE
;
2037 || GOT_TLS_GD_ANY_P (*local_tls_type
)
2038 || *local_tls_type
== GOT_TLS_IE
)
2040 if (GOT_TLS_GDESC_P (*local_tls_type
))
2042 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
2043 htab
->tlsdesc_plt
= (bfd_vma
) -1;
2045 if (! GOT_TLS_GDESC_P (*local_tls_type
)
2046 || GOT_TLS_GD_P (*local_tls_type
))
2047 srel
->size
+= sizeof (Elf64_External_Rela
);
2051 *local_got
= (bfd_vma
) -1;
2055 if (htab
->tls_ld_got
.refcount
> 0)
2057 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2059 htab
->tls_ld_got
.offset
= htab
->sgot
->size
;
2060 htab
->sgot
->size
+= 2 * GOT_ENTRY_SIZE
;
2061 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
2064 htab
->tls_ld_got
.offset
= -1;
2066 /* Allocate global sym .plt and .got entries, and space for global
2067 sym dynamic relocs. */
2068 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
2070 /* For every jump slot reserved in the sgotplt, reloc_count is
2071 incremented. However, when we reserve space for TLS descriptors,
2072 it's not incremented, so in order to compute the space reserved
2073 for them, it suffices to multiply the reloc count by the jump
2076 htab
->sgotplt_jump_table_size
2077 = elf64_x86_64_compute_jump_table_size (htab
);
2079 if (htab
->tlsdesc_plt
)
2081 /* If we're not using lazy TLS relocations, don't generate the
2082 PLT and GOT entries they require. */
2083 if ((info
->flags
& DF_BIND_NOW
))
2084 htab
->tlsdesc_plt
= 0;
2087 htab
->tlsdesc_got
= htab
->sgot
->size
;
2088 htab
->sgot
->size
+= GOT_ENTRY_SIZE
;
2089 /* Reserve room for the initial entry.
2090 FIXME: we could probably do away with it in this case. */
2091 if (htab
->splt
->size
== 0)
2092 htab
->splt
->size
+= PLT_ENTRY_SIZE
;
2093 htab
->tlsdesc_plt
= htab
->splt
->size
;
2094 htab
->splt
->size
+= PLT_ENTRY_SIZE
;
2098 /* We now have determined the sizes of the various dynamic sections.
2099 Allocate memory for them. */
2101 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
2103 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
2108 || s
== htab
->sgotplt
2109 || s
== htab
->sdynbss
)
2111 /* Strip this section if we don't need it; see the
2114 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
2116 if (s
->size
!= 0 && s
!= htab
->srelplt
)
2119 /* We use the reloc_count field as a counter if we need
2120 to copy relocs into the output file. */
2121 if (s
!= htab
->srelplt
)
2126 /* It's not one of our sections, so don't allocate space. */
2132 /* If we don't need this section, strip it from the
2133 output file. This is mostly to handle .rela.bss and
2134 .rela.plt. We must create both sections in
2135 create_dynamic_sections, because they must be created
2136 before the linker maps input sections to output
2137 sections. The linker does that before
2138 adjust_dynamic_symbol is called, and it is that
2139 function which decides whether anything needs to go
2140 into these sections. */
2142 s
->flags
|= SEC_EXCLUDE
;
2146 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
2149 /* Allocate memory for the section contents. We use bfd_zalloc
2150 here in case unused entries are not reclaimed before the
2151 section's contents are written out. This should not happen,
2152 but this way if it does, we get a R_X86_64_NONE reloc instead
2154 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
2155 if (s
->contents
== NULL
)
2159 if (htab
->elf
.dynamic_sections_created
)
2161 /* Add some entries to the .dynamic section. We fill in the
2162 values later, in elf64_x86_64_finish_dynamic_sections, but we
2163 must add the entries now so that we get the correct size for
2164 the .dynamic section. The DT_DEBUG entry is filled in by the
2165 dynamic linker and used by the debugger. */
2166 #define add_dynamic_entry(TAG, VAL) \
2167 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2169 if (info
->executable
)
2171 if (!add_dynamic_entry (DT_DEBUG
, 0))
2175 if (htab
->splt
->size
!= 0)
2177 if (!add_dynamic_entry (DT_PLTGOT
, 0)
2178 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
2179 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
2180 || !add_dynamic_entry (DT_JMPREL
, 0))
2183 if (htab
->tlsdesc_plt
2184 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
2185 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
2191 if (!add_dynamic_entry (DT_RELA
, 0)
2192 || !add_dynamic_entry (DT_RELASZ
, 0)
2193 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf64_External_Rela
)))
2196 /* If any dynamic relocs apply to a read-only section,
2197 then we need a DT_TEXTREL entry. */
2198 if ((info
->flags
& DF_TEXTREL
) == 0)
2199 elf_link_hash_traverse (&htab
->elf
, readonly_dynrelocs
, info
);
2201 if ((info
->flags
& DF_TEXTREL
) != 0)
2203 if (!add_dynamic_entry (DT_TEXTREL
, 0))
2208 #undef add_dynamic_entry
2214 elf64_x86_64_always_size_sections (bfd
*output_bfd
,
2215 struct bfd_link_info
*info
)
2217 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
2221 struct elf_link_hash_entry
*tlsbase
;
2223 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
2224 "_TLS_MODULE_BASE_",
2225 FALSE
, FALSE
, FALSE
);
2227 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
2229 struct bfd_link_hash_entry
*bh
= NULL
;
2230 const struct elf_backend_data
*bed
2231 = get_elf_backend_data (output_bfd
);
2233 if (!(_bfd_generic_link_add_one_symbol
2234 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
2235 tls_sec
, 0, NULL
, FALSE
,
2236 bed
->collect
, &bh
)))
2239 elf64_x86_64_hash_table (info
)->tls_module_base
= bh
;
2241 tlsbase
= (struct elf_link_hash_entry
*)bh
;
2242 tlsbase
->def_regular
= 1;
2243 tlsbase
->other
= STV_HIDDEN
;
2244 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, TRUE
);
2251 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2252 executables. Rather than setting it to the beginning of the TLS
2253 section, we have to set it to the end. This function may be called
2254 multiple times, it is idempotent. */
2257 set_tls_module_base (struct bfd_link_info
*info
)
2259 struct bfd_link_hash_entry
*base
;
2261 if (!info
->executable
)
2264 base
= elf64_x86_64_hash_table (info
)->tls_module_base
;
2269 base
->u
.def
.value
= elf_hash_table (info
)->tls_size
;
2272 /* Return the base VMA address which should be subtracted from real addresses
2273 when resolving @dtpoff relocation.
2274 This is PT_TLS segment p_vaddr. */
2277 dtpoff_base (struct bfd_link_info
*info
)
2279 /* If tls_sec is NULL, we should have signalled an error already. */
2280 if (elf_hash_table (info
)->tls_sec
== NULL
)
2282 return elf_hash_table (info
)->tls_sec
->vma
;
2285 /* Return the relocation value for @tpoff relocation
2286 if STT_TLS virtual address is ADDRESS. */
2289 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
2291 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2293 /* If tls_segment is NULL, we should have signalled an error already. */
2294 if (htab
->tls_sec
== NULL
)
2296 return address
- htab
->tls_size
- htab
->tls_sec
->vma
;
2299 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2303 is_32bit_relative_branch (bfd_byte
*contents
, bfd_vma offset
)
2305 /* Opcode Instruction
2308 0x0f 0x8x conditional jump */
2310 && (contents
[offset
- 1] == 0xe8
2311 || contents
[offset
- 1] == 0xe9))
2313 && contents
[offset
- 2] == 0x0f
2314 && (contents
[offset
- 1] & 0xf0) == 0x80));
2317 /* Relocate an x86_64 ELF section. */
2320 elf64_x86_64_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
2321 bfd
*input_bfd
, asection
*input_section
,
2322 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
2323 Elf_Internal_Sym
*local_syms
,
2324 asection
**local_sections
)
2326 struct elf64_x86_64_link_hash_table
*htab
;
2327 Elf_Internal_Shdr
*symtab_hdr
;
2328 struct elf_link_hash_entry
**sym_hashes
;
2329 bfd_vma
*local_got_offsets
;
2330 bfd_vma
*local_tlsdesc_gotents
;
2331 Elf_Internal_Rela
*rel
;
2332 Elf_Internal_Rela
*relend
;
2334 BFD_ASSERT (is_x86_64_elf (input_bfd
));
2336 htab
= elf64_x86_64_hash_table (info
);
2337 symtab_hdr
= &elf_symtab_hdr (input_bfd
);
2338 sym_hashes
= elf_sym_hashes (input_bfd
);
2339 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2340 local_tlsdesc_gotents
= elf64_x86_64_local_tlsdesc_gotent (input_bfd
);
2342 set_tls_module_base (info
);
2345 relend
= relocs
+ input_section
->reloc_count
;
2346 for (; rel
< relend
; rel
++)
2348 unsigned int r_type
;
2349 reloc_howto_type
*howto
;
2350 unsigned long r_symndx
;
2351 struct elf_link_hash_entry
*h
;
2352 Elf_Internal_Sym
*sym
;
2354 bfd_vma off
, offplt
;
2356 bfd_boolean unresolved_reloc
;
2357 bfd_reloc_status_type r
;
2360 r_type
= ELF64_R_TYPE (rel
->r_info
);
2361 if (r_type
== (int) R_X86_64_GNU_VTINHERIT
2362 || r_type
== (int) R_X86_64_GNU_VTENTRY
)
2365 if (r_type
>= R_X86_64_max
)
2367 bfd_set_error (bfd_error_bad_value
);
2371 howto
= x86_64_elf_howto_table
+ r_type
;
2372 r_symndx
= ELF64_R_SYM (rel
->r_info
);
2376 unresolved_reloc
= FALSE
;
2377 if (r_symndx
< symtab_hdr
->sh_info
)
2379 sym
= local_syms
+ r_symndx
;
2380 sec
= local_sections
[r_symndx
];
2382 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2388 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2389 r_symndx
, symtab_hdr
, sym_hashes
,
2391 unresolved_reloc
, warned
);
2394 if (sec
!= NULL
&& elf_discarded_section (sec
))
2396 /* For relocs against symbols from removed linkonce sections,
2397 or sections discarded by a linker script, we just want the
2398 section contents zeroed. Avoid any special processing. */
2399 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
2405 if (info
->relocatable
)
2408 /* When generating a shared object, the relocations handled here are
2409 copied into the output file to be resolved at run time. */
2413 case R_X86_64_GOT32
:
2414 case R_X86_64_GOT64
:
2415 /* Relocation is to the entry for this symbol in the global
2417 case R_X86_64_GOTPCREL
:
2418 case R_X86_64_GOTPCREL64
:
2419 /* Use global offset table entry as symbol value. */
2420 case R_X86_64_GOTPLT64
:
2421 /* This is the same as GOT64 for relocation purposes, but
2422 indicates the existence of a PLT entry. The difficulty is,
2423 that we must calculate the GOT slot offset from the PLT
2424 offset, if this symbol got a PLT entry (it was global).
2425 Additionally if it's computed from the PLT entry, then that
2426 GOT offset is relative to .got.plt, not to .got. */
2427 base_got
= htab
->sgot
;
2429 if (htab
->sgot
== NULL
)
2436 off
= h
->got
.offset
;
2438 && h
->plt
.offset
!= (bfd_vma
)-1
2439 && off
== (bfd_vma
)-1)
2441 /* We can't use h->got.offset here to save
2442 state, or even just remember the offset, as
2443 finish_dynamic_symbol would use that as offset into
2445 bfd_vma plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
2446 off
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
2447 base_got
= htab
->sgotplt
;
2450 dyn
= htab
->elf
.dynamic_sections_created
;
2452 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
2454 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2455 || (ELF_ST_VISIBILITY (h
->other
)
2456 && h
->root
.type
== bfd_link_hash_undefweak
))
2458 /* This is actually a static link, or it is a -Bsymbolic
2459 link and the symbol is defined locally, or the symbol
2460 was forced to be local because of a version file. We
2461 must initialize this entry in the global offset table.
2462 Since the offset must always be a multiple of 8, we
2463 use the least significant bit to record whether we
2464 have initialized it already.
2466 When doing a dynamic link, we create a .rela.got
2467 relocation entry to initialize the value. This is
2468 done in the finish_dynamic_symbol routine. */
2473 bfd_put_64 (output_bfd
, relocation
,
2474 base_got
->contents
+ off
);
2475 /* Note that this is harmless for the GOTPLT64 case,
2476 as -1 | 1 still is -1. */
2481 unresolved_reloc
= FALSE
;
2485 if (local_got_offsets
== NULL
)
2488 off
= local_got_offsets
[r_symndx
];
2490 /* The offset must always be a multiple of 8. We use
2491 the least significant bit to record whether we have
2492 already generated the necessary reloc. */
2497 bfd_put_64 (output_bfd
, relocation
,
2498 base_got
->contents
+ off
);
2503 Elf_Internal_Rela outrel
;
2506 /* We need to generate a R_X86_64_RELATIVE reloc
2507 for the dynamic linker. */
2512 outrel
.r_offset
= (base_got
->output_section
->vma
2513 + base_got
->output_offset
2515 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
2516 outrel
.r_addend
= relocation
;
2518 loc
+= s
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2519 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2522 local_got_offsets
[r_symndx
] |= 1;
2526 if (off
>= (bfd_vma
) -2)
2529 relocation
= base_got
->output_section
->vma
2530 + base_got
->output_offset
+ off
;
2531 if (r_type
!= R_X86_64_GOTPCREL
&& r_type
!= R_X86_64_GOTPCREL64
)
2532 relocation
-= htab
->sgotplt
->output_section
->vma
2533 - htab
->sgotplt
->output_offset
;
2537 case R_X86_64_GOTOFF64
:
2538 /* Relocation is relative to the start of the global offset
2541 /* Check to make sure it isn't a protected function symbol
2542 for shared library since it may not be local when used
2543 as function address. */
2547 && h
->type
== STT_FUNC
2548 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
2550 (*_bfd_error_handler
)
2551 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2552 input_bfd
, h
->root
.root
.string
);
2553 bfd_set_error (bfd_error_bad_value
);
2557 /* Note that sgot is not involved in this
2558 calculation. We always want the start of .got.plt. If we
2559 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2560 permitted by the ABI, we might have to change this
2562 relocation
-= htab
->sgotplt
->output_section
->vma
2563 + htab
->sgotplt
->output_offset
;
2566 case R_X86_64_GOTPC32
:
2567 case R_X86_64_GOTPC64
:
2568 /* Use global offset table as symbol value. */
2569 relocation
= htab
->sgotplt
->output_section
->vma
2570 + htab
->sgotplt
->output_offset
;
2571 unresolved_reloc
= FALSE
;
2574 case R_X86_64_PLTOFF64
:
2575 /* Relocation is PLT entry relative to GOT. For local
2576 symbols it's the symbol itself relative to GOT. */
2578 /* See PLT32 handling. */
2579 && h
->plt
.offset
!= (bfd_vma
) -1
2580 && htab
->splt
!= NULL
)
2582 relocation
= (htab
->splt
->output_section
->vma
2583 + htab
->splt
->output_offset
2585 unresolved_reloc
= FALSE
;
2588 relocation
-= htab
->sgotplt
->output_section
->vma
2589 + htab
->sgotplt
->output_offset
;
2592 case R_X86_64_PLT32
:
2593 /* Relocation is to the entry for this symbol in the
2594 procedure linkage table. */
2596 /* Resolve a PLT32 reloc against a local symbol directly,
2597 without using the procedure linkage table. */
2601 if (h
->plt
.offset
== (bfd_vma
) -1
2602 || htab
->splt
== NULL
)
2604 /* We didn't make a PLT entry for this symbol. This
2605 happens when statically linking PIC code, or when
2606 using -Bsymbolic. */
2610 relocation
= (htab
->splt
->output_section
->vma
2611 + htab
->splt
->output_offset
2613 unresolved_reloc
= FALSE
;
2620 && (input_section
->flags
& SEC_ALLOC
) != 0
2621 && (input_section
->flags
& SEC_READONLY
) != 0
2624 bfd_boolean fail
= FALSE
;
2626 = (r_type
== R_X86_64_PC32
2627 && is_32bit_relative_branch (contents
, rel
->r_offset
));
2629 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
2631 /* Symbol is referenced locally. Make sure it is
2632 defined locally or for a branch. */
2633 fail
= !h
->def_regular
&& !branch
;
2637 /* Symbol isn't referenced locally. We only allow
2638 branch to symbol with non-default visibility. */
2640 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
);
2647 const char *pic
= "";
2649 switch (ELF_ST_VISIBILITY (h
->other
))
2652 v
= _("hidden symbol");
2655 v
= _("internal symbol");
2658 v
= _("protected symbol");
2662 pic
= _("; recompile with -fPIC");
2667 fmt
= _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
2669 fmt
= _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
2671 (*_bfd_error_handler
) (fmt
, input_bfd
,
2672 x86_64_elf_howto_table
[r_type
].name
,
2673 v
, h
->root
.root
.string
, pic
);
2674 bfd_set_error (bfd_error_bad_value
);
2685 /* FIXME: The ABI says the linker should make sure the value is
2686 the same when it's zeroextended to 64 bit. */
2688 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2693 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2694 || h
->root
.type
!= bfd_link_hash_undefweak
)
2695 && (! IS_X86_64_PCREL_TYPE (r_type
)
2696 || ! SYMBOL_CALLS_LOCAL (info
, h
)))
2700 && ! h
->forced_local
2701 && ((struct elf64_x86_64_link_hash_entry
*) h
)->dyn_relocs
!= NULL
2702 && _bfd_elf_is_ifunc_symbol (output_bfd
, h
))
2703 || (ELIMINATE_COPY_RELOCS
2710 || h
->root
.type
== bfd_link_hash_undefweak
2711 || h
->root
.type
== bfd_link_hash_undefined
)))
2713 Elf_Internal_Rela outrel
;
2715 bfd_boolean skip
, relocate
;
2718 /* When generating a shared object, these relocations
2719 are copied into the output file to be resolved at run
2725 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2727 if (outrel
.r_offset
== (bfd_vma
) -1)
2729 else if (outrel
.r_offset
== (bfd_vma
) -2)
2730 skip
= TRUE
, relocate
= TRUE
;
2732 outrel
.r_offset
+= (input_section
->output_section
->vma
2733 + input_section
->output_offset
);
2736 memset (&outrel
, 0, sizeof outrel
);
2738 /* h->dynindx may be -1 if this symbol was marked to
2742 && (IS_X86_64_PCREL_TYPE (r_type
)
2744 || ! SYMBOLIC_BIND (info
, h
)
2745 || ! h
->def_regular
))
2747 outrel
.r_info
= ELF64_R_INFO (h
->dynindx
, r_type
);
2748 outrel
.r_addend
= rel
->r_addend
;
2752 /* This symbol is local, or marked to become local. */
2753 if (r_type
== R_X86_64_64
)
2756 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
2757 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2763 if (bfd_is_abs_section (sec
))
2765 else if (sec
== NULL
|| sec
->owner
== NULL
)
2767 bfd_set_error (bfd_error_bad_value
);
2774 /* We are turning this relocation into one
2775 against a section symbol. It would be
2776 proper to subtract the symbol's value,
2777 osec->vma, from the emitted reloc addend,
2778 but ld.so expects buggy relocs. */
2779 osec
= sec
->output_section
;
2780 sindx
= elf_section_data (osec
)->dynindx
;
2783 asection
*oi
= htab
->elf
.text_index_section
;
2784 sindx
= elf_section_data (oi
)->dynindx
;
2786 BFD_ASSERT (sindx
!= 0);
2789 outrel
.r_info
= ELF64_R_INFO (sindx
, r_type
);
2790 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2797 && ! h
->forced_local
2798 && _bfd_elf_is_ifunc_symbol (output_bfd
, h
)
2799 && elf_section_data (input_section
)->indirect_relocs
!= NULL
2800 && elf_section_data (input_section
)->indirect_relocs
->contents
!= NULL
)
2801 sreloc
= elf_section_data (input_section
)->indirect_relocs
;
2803 sreloc
= elf_section_data (input_section
)->sreloc
;
2805 BFD_ASSERT (sreloc
!= NULL
&& sreloc
->contents
!= NULL
);
2807 loc
= sreloc
->contents
;
2808 loc
+= sreloc
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2809 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2811 /* If this reloc is against an external symbol, we do
2812 not want to fiddle with the addend. Otherwise, we
2813 need to include the symbol value so that it becomes
2814 an addend for the dynamic reloc. */
2821 case R_X86_64_TLSGD
:
2822 case R_X86_64_GOTPC32_TLSDESC
:
2823 case R_X86_64_TLSDESC_CALL
:
2824 case R_X86_64_GOTTPOFF
:
2825 tls_type
= GOT_UNKNOWN
;
2826 if (h
== NULL
&& local_got_offsets
)
2827 tls_type
= elf64_x86_64_local_got_tls_type (input_bfd
) [r_symndx
];
2829 tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
2831 if (! elf64_x86_64_tls_transition (info
, input_bfd
,
2832 input_section
, contents
,
2833 symtab_hdr
, sym_hashes
,
2834 &r_type
, tls_type
, rel
,
2838 if (r_type
== R_X86_64_TPOFF32
)
2840 bfd_vma roff
= rel
->r_offset
;
2842 BFD_ASSERT (! unresolved_reloc
);
2844 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
2846 /* GD->LE transition.
2847 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2848 .word 0x6666; rex64; call __tls_get_addr
2851 leaq foo@tpoff(%rax), %rax */
2852 memcpy (contents
+ roff
- 4,
2853 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2855 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2856 contents
+ roff
+ 8);
2857 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
2861 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
2863 /* GDesc -> LE transition.
2864 It's originally something like:
2865 leaq x@tlsdesc(%rip), %rax
2871 unsigned int val
, type
, type2
;
2873 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
2874 type2
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
2875 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
2876 bfd_put_8 (output_bfd
, 0x48 | ((type
>> 2) & 1),
2877 contents
+ roff
- 3);
2878 bfd_put_8 (output_bfd
, 0xc7, contents
+ roff
- 2);
2879 bfd_put_8 (output_bfd
, 0xc0 | ((val
>> 3) & 7),
2880 contents
+ roff
- 1);
2881 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2885 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
2887 /* GDesc -> LE transition.
2892 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
2893 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
2896 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTTPOFF
)
2898 /* IE->LE transition:
2899 Originally it can be one of:
2900 movq foo@gottpoff(%rip), %reg
2901 addq foo@gottpoff(%rip), %reg
2904 leaq foo(%reg), %reg
2907 unsigned int val
, type
, reg
;
2909 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
2910 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
2911 reg
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
2917 bfd_put_8 (output_bfd
, 0x49,
2918 contents
+ roff
- 3);
2919 bfd_put_8 (output_bfd
, 0xc7,
2920 contents
+ roff
- 2);
2921 bfd_put_8 (output_bfd
, 0xc0 | reg
,
2922 contents
+ roff
- 1);
2926 /* addq -> addq - addressing with %rsp/%r12 is
2929 bfd_put_8 (output_bfd
, 0x49,
2930 contents
+ roff
- 3);
2931 bfd_put_8 (output_bfd
, 0x81,
2932 contents
+ roff
- 2);
2933 bfd_put_8 (output_bfd
, 0xc0 | reg
,
2934 contents
+ roff
- 1);
2940 bfd_put_8 (output_bfd
, 0x4d,
2941 contents
+ roff
- 3);
2942 bfd_put_8 (output_bfd
, 0x8d,
2943 contents
+ roff
- 2);
2944 bfd_put_8 (output_bfd
, 0x80 | reg
| (reg
<< 3),
2945 contents
+ roff
- 1);
2947 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2955 if (htab
->sgot
== NULL
)
2960 off
= h
->got
.offset
;
2961 offplt
= elf64_x86_64_hash_entry (h
)->tlsdesc_got
;
2965 if (local_got_offsets
== NULL
)
2968 off
= local_got_offsets
[r_symndx
];
2969 offplt
= local_tlsdesc_gotents
[r_symndx
];
2976 Elf_Internal_Rela outrel
;
2981 if (htab
->srelgot
== NULL
)
2984 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
2986 if (GOT_TLS_GDESC_P (tls_type
))
2988 outrel
.r_info
= ELF64_R_INFO (indx
, R_X86_64_TLSDESC
);
2989 BFD_ASSERT (htab
->sgotplt_jump_table_size
+ offplt
2990 + 2 * GOT_ENTRY_SIZE
<= htab
->sgotplt
->size
);
2991 outrel
.r_offset
= (htab
->sgotplt
->output_section
->vma
2992 + htab
->sgotplt
->output_offset
2994 + htab
->sgotplt_jump_table_size
);
2995 sreloc
= htab
->srelplt
;
2996 loc
= sreloc
->contents
;
2997 loc
+= sreloc
->reloc_count
++
2998 * sizeof (Elf64_External_Rela
);
2999 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
3000 <= sreloc
->contents
+ sreloc
->size
);
3002 outrel
.r_addend
= relocation
- dtpoff_base (info
);
3004 outrel
.r_addend
= 0;
3005 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3008 sreloc
= htab
->srelgot
;
3010 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
3011 + htab
->sgot
->output_offset
+ off
);
3013 if (GOT_TLS_GD_P (tls_type
))
3014 dr_type
= R_X86_64_DTPMOD64
;
3015 else if (GOT_TLS_GDESC_P (tls_type
))
3018 dr_type
= R_X86_64_TPOFF64
;
3020 bfd_put_64 (output_bfd
, 0, htab
->sgot
->contents
+ off
);
3021 outrel
.r_addend
= 0;
3022 if ((dr_type
== R_X86_64_TPOFF64
3023 || dr_type
== R_X86_64_TLSDESC
) && indx
== 0)
3024 outrel
.r_addend
= relocation
- dtpoff_base (info
);
3025 outrel
.r_info
= ELF64_R_INFO (indx
, dr_type
);
3027 loc
= sreloc
->contents
;
3028 loc
+= sreloc
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3029 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
3030 <= sreloc
->contents
+ sreloc
->size
);
3031 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3033 if (GOT_TLS_GD_P (tls_type
))
3037 BFD_ASSERT (! unresolved_reloc
);
3038 bfd_put_64 (output_bfd
,
3039 relocation
- dtpoff_base (info
),
3040 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3044 bfd_put_64 (output_bfd
, 0,
3045 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3046 outrel
.r_info
= ELF64_R_INFO (indx
,
3048 outrel
.r_offset
+= GOT_ENTRY_SIZE
;
3049 sreloc
->reloc_count
++;
3050 loc
+= sizeof (Elf64_External_Rela
);
3051 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
3052 <= sreloc
->contents
+ sreloc
->size
);
3053 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3061 local_got_offsets
[r_symndx
] |= 1;
3064 if (off
>= (bfd_vma
) -2
3065 && ! GOT_TLS_GDESC_P (tls_type
))
3067 if (r_type
== ELF64_R_TYPE (rel
->r_info
))
3069 if (r_type
== R_X86_64_GOTPC32_TLSDESC
3070 || r_type
== R_X86_64_TLSDESC_CALL
)
3071 relocation
= htab
->sgotplt
->output_section
->vma
3072 + htab
->sgotplt
->output_offset
3073 + offplt
+ htab
->sgotplt_jump_table_size
;
3075 relocation
= htab
->sgot
->output_section
->vma
3076 + htab
->sgot
->output_offset
+ off
;
3077 unresolved_reloc
= FALSE
;
3081 bfd_vma roff
= rel
->r_offset
;
3083 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
3085 /* GD->IE transition.
3086 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3087 .word 0x6666; rex64; call __tls_get_addr@plt
3090 addq foo@gottpoff(%rip), %rax */
3091 memcpy (contents
+ roff
- 4,
3092 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3095 relocation
= (htab
->sgot
->output_section
->vma
3096 + htab
->sgot
->output_offset
+ off
3098 - input_section
->output_section
->vma
3099 - input_section
->output_offset
3101 bfd_put_32 (output_bfd
, relocation
,
3102 contents
+ roff
+ 8);
3103 /* Skip R_X86_64_PLT32. */
3107 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
3109 /* GDesc -> IE transition.
3110 It's originally something like:
3111 leaq x@tlsdesc(%rip), %rax
3114 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3117 unsigned int val
, type
, type2
;
3119 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
3120 type2
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
3121 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
3123 /* Now modify the instruction as appropriate. To
3124 turn a leaq into a movq in the form we use it, it
3125 suffices to change the second byte from 0x8d to
3127 bfd_put_8 (output_bfd
, 0x8b, contents
+ roff
- 2);
3129 bfd_put_32 (output_bfd
,
3130 htab
->sgot
->output_section
->vma
3131 + htab
->sgot
->output_offset
+ off
3133 - input_section
->output_section
->vma
3134 - input_section
->output_offset
3139 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
3141 /* GDesc -> IE transition.
3148 unsigned int val
, type
;
3150 type
= bfd_get_8 (input_bfd
, contents
+ roff
);
3151 val
= bfd_get_8 (input_bfd
, contents
+ roff
+ 1);
3152 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
3153 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
3161 case R_X86_64_TLSLD
:
3162 if (! elf64_x86_64_tls_transition (info
, input_bfd
,
3163 input_section
, contents
,
3164 symtab_hdr
, sym_hashes
,
3165 &r_type
, GOT_UNKNOWN
,
3169 if (r_type
!= R_X86_64_TLSLD
)
3171 /* LD->LE transition:
3172 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3174 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3176 BFD_ASSERT (r_type
== R_X86_64_TPOFF32
);
3177 memcpy (contents
+ rel
->r_offset
- 3,
3178 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3179 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3184 if (htab
->sgot
== NULL
)
3187 off
= htab
->tls_ld_got
.offset
;
3192 Elf_Internal_Rela outrel
;
3195 if (htab
->srelgot
== NULL
)
3198 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
3199 + htab
->sgot
->output_offset
+ off
);
3201 bfd_put_64 (output_bfd
, 0,
3202 htab
->sgot
->contents
+ off
);
3203 bfd_put_64 (output_bfd
, 0,
3204 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
3205 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_DTPMOD64
);
3206 outrel
.r_addend
= 0;
3207 loc
= htab
->srelgot
->contents
;
3208 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3209 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
3210 htab
->tls_ld_got
.offset
|= 1;
3212 relocation
= htab
->sgot
->output_section
->vma
3213 + htab
->sgot
->output_offset
+ off
;
3214 unresolved_reloc
= FALSE
;
3217 case R_X86_64_DTPOFF32
:
3218 if (info
->shared
|| (input_section
->flags
& SEC_CODE
) == 0)
3219 relocation
-= dtpoff_base (info
);
3221 relocation
= tpoff (info
, relocation
);
3224 case R_X86_64_TPOFF32
:
3225 BFD_ASSERT (! info
->shared
);
3226 relocation
= tpoff (info
, relocation
);
3233 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3234 because such sections are not SEC_ALLOC and thus ld.so will
3235 not process them. */
3236 if (unresolved_reloc
3237 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
3239 (*_bfd_error_handler
)
3240 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3243 (long) rel
->r_offset
,
3245 h
->root
.root
.string
);
3247 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3248 contents
, rel
->r_offset
,
3249 relocation
, rel
->r_addend
);
3251 if (r
!= bfd_reloc_ok
)
3256 name
= h
->root
.root
.string
;
3259 name
= bfd_elf_string_from_elf_section (input_bfd
,
3260 symtab_hdr
->sh_link
,
3265 name
= bfd_section_name (input_bfd
, sec
);
3268 if (r
== bfd_reloc_overflow
)
3270 if (! ((*info
->callbacks
->reloc_overflow
)
3271 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3272 (bfd_vma
) 0, input_bfd
, input_section
,
3278 (*_bfd_error_handler
)
3279 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3280 input_bfd
, input_section
,
3281 (long) rel
->r_offset
, name
, (int) r
);
3290 /* Finish up dynamic symbol handling. We set the contents of various
3291 dynamic sections here. */
3294 elf64_x86_64_finish_dynamic_symbol (bfd
*output_bfd
,
3295 struct bfd_link_info
*info
,
3296 struct elf_link_hash_entry
*h
,
3297 Elf_Internal_Sym
*sym
)
3299 struct elf64_x86_64_link_hash_table
*htab
;
3301 htab
= elf64_x86_64_hash_table (info
);
3303 if (h
->plt
.offset
!= (bfd_vma
) -1)
3307 Elf_Internal_Rela rela
;
3310 /* This symbol has an entry in the procedure linkage table. Set
3312 if (h
->dynindx
== -1
3313 || htab
->splt
== NULL
3314 || htab
->sgotplt
== NULL
3315 || htab
->srelplt
== NULL
)
3318 /* Get the index in the procedure linkage table which
3319 corresponds to this symbol. This is the index of this symbol
3320 in all the symbols for which we are making plt entries. The
3321 first entry in the procedure linkage table is reserved. */
3322 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
3324 /* Get the offset into the .got table of the entry that
3325 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3326 bytes. The first three are reserved for the dynamic linker. */
3327 got_offset
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
3329 /* Fill in the entry in the procedure linkage table. */
3330 memcpy (htab
->splt
->contents
+ h
->plt
.offset
, elf64_x86_64_plt_entry
,
3333 /* Insert the relocation positions of the plt section. The magic
3334 numbers at the end of the statements are the positions of the
3335 relocations in the plt section. */
3336 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3337 instruction uses 6 bytes, subtract this value. */
3338 bfd_put_32 (output_bfd
,
3339 (htab
->sgotplt
->output_section
->vma
3340 + htab
->sgotplt
->output_offset
3342 - htab
->splt
->output_section
->vma
3343 - htab
->splt
->output_offset
3346 htab
->splt
->contents
+ h
->plt
.offset
+ 2);
3347 /* Put relocation index. */
3348 bfd_put_32 (output_bfd
, plt_index
,
3349 htab
->splt
->contents
+ h
->plt
.offset
+ 7);
3350 /* Put offset for jmp .PLT0. */
3351 bfd_put_32 (output_bfd
, - (h
->plt
.offset
+ PLT_ENTRY_SIZE
),
3352 htab
->splt
->contents
+ h
->plt
.offset
+ 12);
3354 /* Fill in the entry in the global offset table, initially this
3355 points to the pushq instruction in the PLT which is at offset 6. */
3356 bfd_put_64 (output_bfd
, (htab
->splt
->output_section
->vma
3357 + htab
->splt
->output_offset
3358 + h
->plt
.offset
+ 6),
3359 htab
->sgotplt
->contents
+ got_offset
);
3361 /* Fill in the entry in the .rela.plt section. */
3362 rela
.r_offset
= (htab
->sgotplt
->output_section
->vma
3363 + htab
->sgotplt
->output_offset
3365 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_JUMP_SLOT
);
3367 loc
= htab
->srelplt
->contents
+ plt_index
* sizeof (Elf64_External_Rela
);
3368 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3370 if (!h
->def_regular
)
3372 /* Mark the symbol as undefined, rather than as defined in
3373 the .plt section. Leave the value if there were any
3374 relocations where pointer equality matters (this is a clue
3375 for the dynamic linker, to make function pointer
3376 comparisons work between an application and shared
3377 library), otherwise set it to zero. If a function is only
3378 called from a binary, there is no need to slow down
3379 shared libraries because of that. */
3380 sym
->st_shndx
= SHN_UNDEF
;
3381 if (!h
->pointer_equality_needed
)
3386 if (h
->got
.offset
!= (bfd_vma
) -1
3387 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h
)->tls_type
)
3388 && elf64_x86_64_hash_entry (h
)->tls_type
!= GOT_TLS_IE
)
3390 Elf_Internal_Rela rela
;
3393 /* This symbol has an entry in the global offset table. Set it
3395 if (htab
->sgot
== NULL
|| htab
->srelgot
== NULL
)
3398 rela
.r_offset
= (htab
->sgot
->output_section
->vma
3399 + htab
->sgot
->output_offset
3400 + (h
->got
.offset
&~ (bfd_vma
) 1));
3402 /* If this is a static link, or it is a -Bsymbolic link and the
3403 symbol is defined locally or was forced to be local because
3404 of a version file, we just want to emit a RELATIVE reloc.
3405 The entry in the global offset table will already have been
3406 initialized in the relocate_section function. */
3408 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3410 if (!h
->def_regular
)
3412 BFD_ASSERT((h
->got
.offset
& 1) != 0);
3413 rela
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
3414 rela
.r_addend
= (h
->root
.u
.def
.value
3415 + h
->root
.u
.def
.section
->output_section
->vma
3416 + h
->root
.u
.def
.section
->output_offset
);
3420 BFD_ASSERT((h
->got
.offset
& 1) == 0);
3421 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
3422 htab
->sgot
->contents
+ h
->got
.offset
);
3423 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_GLOB_DAT
);
3427 loc
= htab
->srelgot
->contents
;
3428 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3429 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3434 Elf_Internal_Rela rela
;
3437 /* This symbol needs a copy reloc. Set it up. */
3439 if (h
->dynindx
== -1
3440 || (h
->root
.type
!= bfd_link_hash_defined
3441 && h
->root
.type
!= bfd_link_hash_defweak
)
3442 || htab
->srelbss
== NULL
)
3445 rela
.r_offset
= (h
->root
.u
.def
.value
3446 + h
->root
.u
.def
.section
->output_section
->vma
3447 + h
->root
.u
.def
.section
->output_offset
);
3448 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_COPY
);
3450 loc
= htab
->srelbss
->contents
;
3451 loc
+= htab
->srelbss
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3452 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3455 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3456 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
3457 || h
== htab
->elf
.hgot
)
3458 sym
->st_shndx
= SHN_ABS
;
3463 /* Used to decide how to sort relocs in an optimal manner for the
3464 dynamic linker, before writing them out. */
3466 static enum elf_reloc_type_class
3467 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela
*rela
)
3469 switch ((int) ELF64_R_TYPE (rela
->r_info
))
3471 case R_X86_64_RELATIVE
:
3472 return reloc_class_relative
;
3473 case R_X86_64_JUMP_SLOT
:
3474 return reloc_class_plt
;
3476 return reloc_class_copy
;
3478 return reloc_class_normal
;
3482 /* Finish up the dynamic sections. */
3485 elf64_x86_64_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
3487 struct elf64_x86_64_link_hash_table
*htab
;
3491 htab
= elf64_x86_64_hash_table (info
);
3492 dynobj
= htab
->elf
.dynobj
;
3493 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3495 if (htab
->elf
.dynamic_sections_created
)
3497 Elf64_External_Dyn
*dyncon
, *dynconend
;
3499 if (sdyn
== NULL
|| htab
->sgot
== NULL
)
3502 dyncon
= (Elf64_External_Dyn
*) sdyn
->contents
;
3503 dynconend
= (Elf64_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
3504 for (; dyncon
< dynconend
; dyncon
++)
3506 Elf_Internal_Dyn dyn
;
3509 bfd_elf64_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3518 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3522 dyn
.d_un
.d_ptr
= htab
->srelplt
->output_section
->vma
;
3526 s
= htab
->srelplt
->output_section
;
3527 dyn
.d_un
.d_val
= s
->size
;
3531 /* The procedure linkage table relocs (DT_JMPREL) should
3532 not be included in the overall relocs (DT_RELA).
3533 Therefore, we override the DT_RELASZ entry here to
3534 make it not include the JMPREL relocs. Since the
3535 linker script arranges for .rela.plt to follow all
3536 other relocation sections, we don't have to worry
3537 about changing the DT_RELA entry. */
3538 if (htab
->srelplt
!= NULL
)
3540 s
= htab
->srelplt
->output_section
;
3541 dyn
.d_un
.d_val
-= s
->size
;
3545 case DT_TLSDESC_PLT
:
3547 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
3548 + htab
->tlsdesc_plt
;
3551 case DT_TLSDESC_GOT
:
3553 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
3554 + htab
->tlsdesc_got
;
3558 bfd_elf64_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3561 /* Fill in the special first entry in the procedure linkage table. */
3562 if (htab
->splt
&& htab
->splt
->size
> 0)
3564 /* Fill in the first entry in the procedure linkage table. */
3565 memcpy (htab
->splt
->contents
, elf64_x86_64_plt0_entry
,
3567 /* Add offset for pushq GOT+8(%rip), since the instruction
3568 uses 6 bytes subtract this value. */
3569 bfd_put_32 (output_bfd
,
3570 (htab
->sgotplt
->output_section
->vma
3571 + htab
->sgotplt
->output_offset
3573 - htab
->splt
->output_section
->vma
3574 - htab
->splt
->output_offset
3576 htab
->splt
->contents
+ 2);
3577 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3578 the end of the instruction. */
3579 bfd_put_32 (output_bfd
,
3580 (htab
->sgotplt
->output_section
->vma
3581 + htab
->sgotplt
->output_offset
3583 - htab
->splt
->output_section
->vma
3584 - htab
->splt
->output_offset
3586 htab
->splt
->contents
+ 8);
3588 elf_section_data (htab
->splt
->output_section
)->this_hdr
.sh_entsize
=
3591 if (htab
->tlsdesc_plt
)
3593 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
3594 htab
->sgot
->contents
+ htab
->tlsdesc_got
);
3596 memcpy (htab
->splt
->contents
+ htab
->tlsdesc_plt
,
3597 elf64_x86_64_plt0_entry
,
3600 /* Add offset for pushq GOT+8(%rip), since the
3601 instruction uses 6 bytes subtract this value. */
3602 bfd_put_32 (output_bfd
,
3603 (htab
->sgotplt
->output_section
->vma
3604 + htab
->sgotplt
->output_offset
3606 - htab
->splt
->output_section
->vma
3607 - htab
->splt
->output_offset
3610 htab
->splt
->contents
+ htab
->tlsdesc_plt
+ 2);
3611 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3612 htab->tlsdesc_got. The 12 is the offset to the end of
3614 bfd_put_32 (output_bfd
,
3615 (htab
->sgot
->output_section
->vma
3616 + htab
->sgot
->output_offset
3618 - htab
->splt
->output_section
->vma
3619 - htab
->splt
->output_offset
3622 htab
->splt
->contents
+ htab
->tlsdesc_plt
+ 8);
3629 /* Fill in the first three entries in the global offset table. */
3630 if (htab
->sgotplt
->size
> 0)
3632 /* Set the first entry in the global offset table to the address of
3633 the dynamic section. */
3635 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
);
3637 bfd_put_64 (output_bfd
,
3638 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
3639 htab
->sgotplt
->contents
);
3640 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3641 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3642 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
*2);
3645 elf_section_data (htab
->sgotplt
->output_section
)->this_hdr
.sh_entsize
=
3649 if (htab
->sgot
&& htab
->sgot
->size
> 0)
3650 elf_section_data (htab
->sgot
->output_section
)->this_hdr
.sh_entsize
3656 /* Return address for Ith PLT stub in section PLT, for relocation REL
3657 or (bfd_vma) -1 if it should not be included. */
3660 elf64_x86_64_plt_sym_val (bfd_vma i
, const asection
*plt
,
3661 const arelent
*rel ATTRIBUTE_UNUSED
)
3663 return plt
->vma
+ (i
+ 1) * PLT_ENTRY_SIZE
;
3666 /* Handle an x86-64 specific section when reading an object file. This
3667 is called when elfcode.h finds a section with an unknown type. */
3670 elf64_x86_64_section_from_shdr (bfd
*abfd
,
3671 Elf_Internal_Shdr
*hdr
,
3675 if (hdr
->sh_type
!= SHT_X86_64_UNWIND
)
3678 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
3684 /* Hook called by the linker routine which adds symbols from an object
3685 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3689 elf64_x86_64_add_symbol_hook (bfd
*abfd
,
3690 struct bfd_link_info
*info
,
3691 Elf_Internal_Sym
*sym
,
3692 const char **namep ATTRIBUTE_UNUSED
,
3693 flagword
*flagsp ATTRIBUTE_UNUSED
,
3699 switch (sym
->st_shndx
)
3701 case SHN_X86_64_LCOMMON
:
3702 lcomm
= bfd_get_section_by_name (abfd
, "LARGE_COMMON");
3705 lcomm
= bfd_make_section_with_flags (abfd
,
3709 | SEC_LINKER_CREATED
));
3712 elf_section_flags (lcomm
) |= SHF_X86_64_LARGE
;
3715 *valp
= sym
->st_size
;
3719 if (ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
3720 elf_tdata (info
->output_bfd
)->has_ifunc_symbols
= TRUE
;
3726 /* Given a BFD section, try to locate the corresponding ELF section
3730 elf64_x86_64_elf_section_from_bfd_section (bfd
*abfd ATTRIBUTE_UNUSED
,
3731 asection
*sec
, int *index
)
3733 if (sec
== &_bfd_elf_large_com_section
)
3735 *index
= SHN_X86_64_LCOMMON
;
3741 /* Process a symbol. */
3744 elf64_x86_64_symbol_processing (bfd
*abfd ATTRIBUTE_UNUSED
,
3747 elf_symbol_type
*elfsym
= (elf_symbol_type
*) asym
;
3749 switch (elfsym
->internal_elf_sym
.st_shndx
)
3751 case SHN_X86_64_LCOMMON
:
3752 asym
->section
= &_bfd_elf_large_com_section
;
3753 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
3754 /* Common symbol doesn't set BSF_GLOBAL. */
3755 asym
->flags
&= ~BSF_GLOBAL
;
3761 elf64_x86_64_common_definition (Elf_Internal_Sym
*sym
)
3763 return (sym
->st_shndx
== SHN_COMMON
3764 || sym
->st_shndx
== SHN_X86_64_LCOMMON
);
3768 elf64_x86_64_common_section_index (asection
*sec
)
3770 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
3773 return SHN_X86_64_LCOMMON
;
3777 elf64_x86_64_common_section (asection
*sec
)
3779 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
3780 return bfd_com_section_ptr
;
3782 return &_bfd_elf_large_com_section
;
3786 elf64_x86_64_merge_symbol (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3787 struct elf_link_hash_entry
**sym_hash ATTRIBUTE_UNUSED
,
3788 struct elf_link_hash_entry
*h
,
3789 Elf_Internal_Sym
*sym
,
3791 bfd_vma
*pvalue ATTRIBUTE_UNUSED
,
3792 unsigned int *pold_alignment ATTRIBUTE_UNUSED
,
3793 bfd_boolean
*skip ATTRIBUTE_UNUSED
,
3794 bfd_boolean
*override ATTRIBUTE_UNUSED
,
3795 bfd_boolean
*type_change_ok ATTRIBUTE_UNUSED
,
3796 bfd_boolean
*size_change_ok ATTRIBUTE_UNUSED
,
3797 bfd_boolean
*newdef ATTRIBUTE_UNUSED
,
3798 bfd_boolean
*newdyn
,
3799 bfd_boolean
*newdyncommon ATTRIBUTE_UNUSED
,
3800 bfd_boolean
*newweak ATTRIBUTE_UNUSED
,
3801 bfd
*abfd ATTRIBUTE_UNUSED
,
3803 bfd_boolean
*olddef ATTRIBUTE_UNUSED
,
3804 bfd_boolean
*olddyn
,
3805 bfd_boolean
*olddyncommon ATTRIBUTE_UNUSED
,
3806 bfd_boolean
*oldweak ATTRIBUTE_UNUSED
,
3810 /* A normal common symbol and a large common symbol result in a
3811 normal common symbol. We turn the large common symbol into a
3814 && h
->root
.type
== bfd_link_hash_common
3816 && bfd_is_com_section (*sec
)
3819 if (sym
->st_shndx
== SHN_COMMON
3820 && (elf_section_flags (*oldsec
) & SHF_X86_64_LARGE
) != 0)
3822 h
->root
.u
.c
.p
->section
3823 = bfd_make_section_old_way (oldbfd
, "COMMON");
3824 h
->root
.u
.c
.p
->section
->flags
= SEC_ALLOC
;
3826 else if (sym
->st_shndx
== SHN_X86_64_LCOMMON
3827 && (elf_section_flags (*oldsec
) & SHF_X86_64_LARGE
) == 0)
3828 *psec
= *sec
= bfd_com_section_ptr
;
3835 elf64_x86_64_additional_program_headers (bfd
*abfd
,
3836 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
3841 /* Check to see if we need a large readonly segment. */
3842 s
= bfd_get_section_by_name (abfd
, ".lrodata");
3843 if (s
&& (s
->flags
& SEC_LOAD
))
3846 /* Check to see if we need a large data segment. Since .lbss sections
3847 is placed right after the .bss section, there should be no need for
3848 a large data segment just because of .lbss. */
3849 s
= bfd_get_section_by_name (abfd
, ".ldata");
3850 if (s
&& (s
->flags
& SEC_LOAD
))
3856 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3859 elf64_x86_64_hash_symbol (struct elf_link_hash_entry
*h
)
3861 if (h
->plt
.offset
!= (bfd_vma
) -1
3863 && !h
->pointer_equality_needed
)
3866 return _bfd_elf_hash_symbol (h
);
3869 static const struct bfd_elf_special_section
3870 elf64_x86_64_special_sections
[]=
3872 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3873 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
3874 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
+ SHF_X86_64_LARGE
},
3875 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3876 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3877 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
3878 { NULL
, 0, 0, 0, 0 }
3881 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3882 #define TARGET_LITTLE_NAME "elf64-x86-64"
3883 #define ELF_ARCH bfd_arch_i386
3884 #define ELF_MACHINE_CODE EM_X86_64
3885 #define ELF_MAXPAGESIZE 0x200000
3886 #define ELF_MINPAGESIZE 0x1000
3887 #define ELF_COMMONPAGESIZE 0x1000
3889 #define elf_backend_can_gc_sections 1
3890 #define elf_backend_can_refcount 1
3891 #define elf_backend_want_got_plt 1
3892 #define elf_backend_plt_readonly 1
3893 #define elf_backend_want_plt_sym 0
3894 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
3895 #define elf_backend_rela_normal 1
3897 #define elf_info_to_howto elf64_x86_64_info_to_howto
3899 #define bfd_elf64_bfd_link_hash_table_create \
3900 elf64_x86_64_link_hash_table_create
3901 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
3902 #define bfd_elf64_bfd_reloc_name_lookup \
3903 elf64_x86_64_reloc_name_lookup
3905 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3906 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
3907 #define elf_backend_check_relocs elf64_x86_64_check_relocs
3908 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3909 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3910 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
3911 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3912 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3913 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3914 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3915 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
3916 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
3917 #define elf_backend_relocate_section elf64_x86_64_relocate_section
3918 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
3919 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
3920 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
3921 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
3922 #define elf_backend_object_p elf64_x86_64_elf_object_p
3923 #define bfd_elf64_mkobject elf64_x86_64_mkobject
3925 #define elf_backend_section_from_shdr \
3926 elf64_x86_64_section_from_shdr
3928 #define elf_backend_section_from_bfd_section \
3929 elf64_x86_64_elf_section_from_bfd_section
3930 #define elf_backend_add_symbol_hook \
3931 elf64_x86_64_add_symbol_hook
3932 #define elf_backend_symbol_processing \
3933 elf64_x86_64_symbol_processing
3934 #define elf_backend_common_section_index \
3935 elf64_x86_64_common_section_index
3936 #define elf_backend_common_section \
3937 elf64_x86_64_common_section
3938 #define elf_backend_common_definition \
3939 elf64_x86_64_common_definition
3940 #define elf_backend_merge_symbol \
3941 elf64_x86_64_merge_symbol
3942 #define elf_backend_special_sections \
3943 elf64_x86_64_special_sections
3944 #define elf_backend_additional_program_headers \
3945 elf64_x86_64_additional_program_headers
3946 #define elf_backend_hash_symbol \
3947 elf64_x86_64_hash_symbol
3949 #undef elf_backend_post_process_headers
3950 #define elf_backend_post_process_headers _bfd_elf_set_osabi
3952 #include "elf64-target.h"
3954 /* FreeBSD support. */
3956 #undef TARGET_LITTLE_SYM
3957 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
3958 #undef TARGET_LITTLE_NAME
3959 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
3962 #define ELF_OSABI ELFOSABI_FREEBSD
3965 #define elf64_bed elf64_x86_64_fbsd_bed
3967 #include "elf64-target.h"