1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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. */
29 #include "elf/x86-64.h"
31 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
32 #define MINUS_ONE (~ (bfd_vma) 0)
34 /* The relocation "howto" table. Order of fields:
35 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
36 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
37 static reloc_howto_type x86_64_elf_howto_table
[] =
39 HOWTO(R_X86_64_NONE
, 0, 0, 0, FALSE
, 0, complain_overflow_dont
,
40 bfd_elf_generic_reloc
, "R_X86_64_NONE", FALSE
, 0x00000000, 0x00000000,
42 HOWTO(R_X86_64_64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
43 bfd_elf_generic_reloc
, "R_X86_64_64", FALSE
, MINUS_ONE
, MINUS_ONE
,
45 HOWTO(R_X86_64_PC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
46 bfd_elf_generic_reloc
, "R_X86_64_PC32", FALSE
, 0xffffffff, 0xffffffff,
48 HOWTO(R_X86_64_GOT32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
49 bfd_elf_generic_reloc
, "R_X86_64_GOT32", FALSE
, 0xffffffff, 0xffffffff,
51 HOWTO(R_X86_64_PLT32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
52 bfd_elf_generic_reloc
, "R_X86_64_PLT32", FALSE
, 0xffffffff, 0xffffffff,
54 HOWTO(R_X86_64_COPY
, 0, 2, 32, FALSE
, 0, complain_overflow_bitfield
,
55 bfd_elf_generic_reloc
, "R_X86_64_COPY", FALSE
, 0xffffffff, 0xffffffff,
57 HOWTO(R_X86_64_GLOB_DAT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
58 bfd_elf_generic_reloc
, "R_X86_64_GLOB_DAT", FALSE
, MINUS_ONE
,
60 HOWTO(R_X86_64_JUMP_SLOT
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
61 bfd_elf_generic_reloc
, "R_X86_64_JUMP_SLOT", FALSE
, MINUS_ONE
,
63 HOWTO(R_X86_64_RELATIVE
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
64 bfd_elf_generic_reloc
, "R_X86_64_RELATIVE", FALSE
, MINUS_ONE
,
66 HOWTO(R_X86_64_GOTPCREL
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
67 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL", FALSE
, 0xffffffff,
69 HOWTO(R_X86_64_32
, 0, 2, 32, FALSE
, 0, complain_overflow_unsigned
,
70 bfd_elf_generic_reloc
, "R_X86_64_32", FALSE
, 0xffffffff, 0xffffffff,
72 HOWTO(R_X86_64_32S
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
73 bfd_elf_generic_reloc
, "R_X86_64_32S", FALSE
, 0xffffffff, 0xffffffff,
75 HOWTO(R_X86_64_16
, 0, 1, 16, FALSE
, 0, complain_overflow_bitfield
,
76 bfd_elf_generic_reloc
, "R_X86_64_16", FALSE
, 0xffff, 0xffff, FALSE
),
77 HOWTO(R_X86_64_PC16
,0, 1, 16, TRUE
, 0, complain_overflow_bitfield
,
78 bfd_elf_generic_reloc
, "R_X86_64_PC16", FALSE
, 0xffff, 0xffff, TRUE
),
79 HOWTO(R_X86_64_8
, 0, 0, 8, FALSE
, 0, complain_overflow_bitfield
,
80 bfd_elf_generic_reloc
, "R_X86_64_8", FALSE
, 0xff, 0xff, FALSE
),
81 HOWTO(R_X86_64_PC8
, 0, 0, 8, TRUE
, 0, complain_overflow_signed
,
82 bfd_elf_generic_reloc
, "R_X86_64_PC8", FALSE
, 0xff, 0xff, TRUE
),
83 HOWTO(R_X86_64_DTPMOD64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
84 bfd_elf_generic_reloc
, "R_X86_64_DTPMOD64", FALSE
, MINUS_ONE
,
86 HOWTO(R_X86_64_DTPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
87 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF64", FALSE
, MINUS_ONE
,
89 HOWTO(R_X86_64_TPOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
90 bfd_elf_generic_reloc
, "R_X86_64_TPOFF64", FALSE
, MINUS_ONE
,
92 HOWTO(R_X86_64_TLSGD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
93 bfd_elf_generic_reloc
, "R_X86_64_TLSGD", FALSE
, 0xffffffff,
95 HOWTO(R_X86_64_TLSLD
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
96 bfd_elf_generic_reloc
, "R_X86_64_TLSLD", FALSE
, 0xffffffff,
98 HOWTO(R_X86_64_DTPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
99 bfd_elf_generic_reloc
, "R_X86_64_DTPOFF32", FALSE
, 0xffffffff,
101 HOWTO(R_X86_64_GOTTPOFF
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
102 bfd_elf_generic_reloc
, "R_X86_64_GOTTPOFF", FALSE
, 0xffffffff,
104 HOWTO(R_X86_64_TPOFF32
, 0, 2, 32, FALSE
, 0, complain_overflow_signed
,
105 bfd_elf_generic_reloc
, "R_X86_64_TPOFF32", FALSE
, 0xffffffff,
107 HOWTO(R_X86_64_PC64
, 0, 4, 64, TRUE
, 0, complain_overflow_bitfield
,
108 bfd_elf_generic_reloc
, "R_X86_64_PC64", FALSE
, MINUS_ONE
, MINUS_ONE
,
110 HOWTO(R_X86_64_GOTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_bitfield
,
111 bfd_elf_generic_reloc
, "R_X86_64_GOTOFF64",
112 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
113 HOWTO(R_X86_64_GOTPC32
, 0, 2, 32, TRUE
, 0, complain_overflow_signed
,
114 bfd_elf_generic_reloc
, "R_X86_64_GOTPC32",
115 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
116 HOWTO(R_X86_64_GOT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
117 bfd_elf_generic_reloc
, "R_X86_64_GOT64", FALSE
, MINUS_ONE
, MINUS_ONE
,
119 HOWTO(R_X86_64_GOTPCREL64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
120 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL64", FALSE
, MINUS_ONE
,
122 HOWTO(R_X86_64_GOTPC64
, 0, 4, 64, TRUE
, 0, complain_overflow_signed
,
123 bfd_elf_generic_reloc
, "R_X86_64_GOTPC64",
124 FALSE
, MINUS_ONE
, MINUS_ONE
, TRUE
),
125 HOWTO(R_X86_64_GOTPLT64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
126 bfd_elf_generic_reloc
, "R_X86_64_GOTPLT64", FALSE
, MINUS_ONE
,
128 HOWTO(R_X86_64_PLTOFF64
, 0, 4, 64, FALSE
, 0, complain_overflow_signed
,
129 bfd_elf_generic_reloc
, "R_X86_64_PLTOFF64", FALSE
, MINUS_ONE
,
133 HOWTO(R_X86_64_GOTPC32_TLSDESC
, 0, 2, 32, TRUE
, 0,
134 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
135 "R_X86_64_GOTPC32_TLSDESC",
136 FALSE
, 0xffffffff, 0xffffffff, TRUE
),
137 HOWTO(R_X86_64_TLSDESC_CALL
, 0, 0, 0, FALSE
, 0,
138 complain_overflow_dont
, bfd_elf_generic_reloc
,
139 "R_X86_64_TLSDESC_CALL",
141 HOWTO(R_X86_64_TLSDESC
, 0, 4, 64, FALSE
, 0,
142 complain_overflow_bitfield
, bfd_elf_generic_reloc
,
144 FALSE
, MINUS_ONE
, MINUS_ONE
, FALSE
),
146 /* We have a gap in the reloc numbers here.
147 R_X86_64_standard counts the number up to this point, and
148 R_X86_64_vt_offset is the value to subtract from a reloc type of
149 R_X86_64_GNU_VT* to form an index into this table. */
150 #define R_X86_64_standard (R_X86_64_TLSDESC + 1)
151 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
153 /* GNU extension to record C++ vtable hierarchy. */
154 HOWTO (R_X86_64_GNU_VTINHERIT
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
155 NULL
, "R_X86_64_GNU_VTINHERIT", FALSE
, 0, 0, FALSE
),
157 /* GNU extension to record C++ vtable member usage. */
158 HOWTO (R_X86_64_GNU_VTENTRY
, 0, 4, 0, FALSE
, 0, complain_overflow_dont
,
159 _bfd_elf_rel_vtable_reloc_fn
, "R_X86_64_GNU_VTENTRY", FALSE
, 0, 0,
163 /* Map BFD relocs to the x86_64 elf relocs. */
166 bfd_reloc_code_real_type bfd_reloc_val
;
167 unsigned char elf_reloc_val
;
170 static const struct elf_reloc_map x86_64_reloc_map
[] =
172 { BFD_RELOC_NONE
, R_X86_64_NONE
, },
173 { BFD_RELOC_64
, R_X86_64_64
, },
174 { BFD_RELOC_32_PCREL
, R_X86_64_PC32
, },
175 { BFD_RELOC_X86_64_GOT32
, R_X86_64_GOT32
,},
176 { BFD_RELOC_X86_64_PLT32
, R_X86_64_PLT32
,},
177 { BFD_RELOC_X86_64_COPY
, R_X86_64_COPY
, },
178 { BFD_RELOC_X86_64_GLOB_DAT
, R_X86_64_GLOB_DAT
, },
179 { BFD_RELOC_X86_64_JUMP_SLOT
, R_X86_64_JUMP_SLOT
, },
180 { BFD_RELOC_X86_64_RELATIVE
, R_X86_64_RELATIVE
, },
181 { BFD_RELOC_X86_64_GOTPCREL
, R_X86_64_GOTPCREL
, },
182 { BFD_RELOC_32
, R_X86_64_32
, },
183 { BFD_RELOC_X86_64_32S
, R_X86_64_32S
, },
184 { BFD_RELOC_16
, R_X86_64_16
, },
185 { BFD_RELOC_16_PCREL
, R_X86_64_PC16
, },
186 { BFD_RELOC_8
, R_X86_64_8
, },
187 { BFD_RELOC_8_PCREL
, R_X86_64_PC8
, },
188 { BFD_RELOC_X86_64_DTPMOD64
, R_X86_64_DTPMOD64
, },
189 { BFD_RELOC_X86_64_DTPOFF64
, R_X86_64_DTPOFF64
, },
190 { BFD_RELOC_X86_64_TPOFF64
, R_X86_64_TPOFF64
, },
191 { BFD_RELOC_X86_64_TLSGD
, R_X86_64_TLSGD
, },
192 { BFD_RELOC_X86_64_TLSLD
, R_X86_64_TLSLD
, },
193 { BFD_RELOC_X86_64_DTPOFF32
, R_X86_64_DTPOFF32
, },
194 { BFD_RELOC_X86_64_GOTTPOFF
, R_X86_64_GOTTPOFF
, },
195 { BFD_RELOC_X86_64_TPOFF32
, R_X86_64_TPOFF32
, },
196 { BFD_RELOC_64_PCREL
, R_X86_64_PC64
, },
197 { BFD_RELOC_X86_64_GOTOFF64
, R_X86_64_GOTOFF64
, },
198 { BFD_RELOC_X86_64_GOTPC32
, R_X86_64_GOTPC32
, },
199 { BFD_RELOC_X86_64_GOT64
, R_X86_64_GOT64
, },
200 { BFD_RELOC_X86_64_GOTPCREL64
,R_X86_64_GOTPCREL64
, },
201 { BFD_RELOC_X86_64_GOTPC64
, R_X86_64_GOTPC64
, },
202 { BFD_RELOC_X86_64_GOTPLT64
, R_X86_64_GOTPLT64
, },
203 { BFD_RELOC_X86_64_PLTOFF64
, R_X86_64_PLTOFF64
, },
204 { BFD_RELOC_X86_64_GOTPC32_TLSDESC
, R_X86_64_GOTPC32_TLSDESC
, },
205 { BFD_RELOC_X86_64_TLSDESC_CALL
, R_X86_64_TLSDESC_CALL
, },
206 { BFD_RELOC_X86_64_TLSDESC
, R_X86_64_TLSDESC
, },
207 { BFD_RELOC_VTABLE_INHERIT
, R_X86_64_GNU_VTINHERIT
, },
208 { BFD_RELOC_VTABLE_ENTRY
, R_X86_64_GNU_VTENTRY
, },
211 static reloc_howto_type
*
212 elf64_x86_64_rtype_to_howto (bfd
*abfd
, unsigned r_type
)
216 if (r_type
< (unsigned int) R_X86_64_GNU_VTINHERIT
217 || r_type
>= (unsigned int) R_X86_64_max
)
219 if (r_type
>= (unsigned int) R_X86_64_standard
)
221 (*_bfd_error_handler
) (_("%B: invalid relocation type %d"),
223 r_type
= R_X86_64_NONE
;
228 i
= r_type
- (unsigned int) R_X86_64_vt_offset
;
229 BFD_ASSERT (x86_64_elf_howto_table
[i
].type
== r_type
);
230 return &x86_64_elf_howto_table
[i
];
233 /* Given a BFD reloc type, return a HOWTO structure. */
234 static reloc_howto_type
*
235 elf64_x86_64_reloc_type_lookup (bfd
*abfd
,
236 bfd_reloc_code_real_type code
)
240 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
243 if (x86_64_reloc_map
[i
].bfd_reloc_val
== code
)
244 return elf64_x86_64_rtype_to_howto (abfd
,
245 x86_64_reloc_map
[i
].elf_reloc_val
);
250 static reloc_howto_type
*
251 elf64_x86_64_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
257 i
< (sizeof (x86_64_elf_howto_table
)
258 / sizeof (x86_64_elf_howto_table
[0]));
260 if (x86_64_elf_howto_table
[i
].name
!= NULL
261 && strcasecmp (x86_64_elf_howto_table
[i
].name
, r_name
) == 0)
262 return &x86_64_elf_howto_table
[i
];
267 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
270 elf64_x86_64_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
271 Elf_Internal_Rela
*dst
)
275 r_type
= ELF64_R_TYPE (dst
->r_info
);
276 cache_ptr
->howto
= elf64_x86_64_rtype_to_howto (abfd
, r_type
);
277 BFD_ASSERT (r_type
== cache_ptr
->howto
->type
);
280 /* Support for core dump NOTE sections. */
282 elf64_x86_64_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
287 switch (note
->descsz
)
292 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
294 elf_tdata (abfd
)->core_signal
295 = bfd_get_16 (abfd
, note
->descdata
+ 12);
298 elf_tdata (abfd
)->core_pid
299 = bfd_get_32 (abfd
, note
->descdata
+ 32);
308 /* Make a ".reg/999" section. */
309 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
310 size
, note
->descpos
+ offset
);
314 elf64_x86_64_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
316 switch (note
->descsz
)
321 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
322 elf_tdata (abfd
)->core_program
323 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 40, 16);
324 elf_tdata (abfd
)->core_command
325 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 56, 80);
328 /* Note that for some reason, a spurious space is tacked
329 onto the end of the args in some (at least one anyway)
330 implementations, so strip it off if it exists. */
333 char *command
= elf_tdata (abfd
)->core_command
;
334 int n
= strlen (command
);
336 if (0 < n
&& command
[n
- 1] == ' ')
337 command
[n
- 1] = '\0';
343 /* Functions for the x86-64 ELF linker. */
345 /* The name of the dynamic interpreter. This is put in the .interp
348 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
350 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
351 copying dynamic variables from a shared lib into an app's dynbss
352 section, and instead use a dynamic relocation to point into the
354 #define ELIMINATE_COPY_RELOCS 1
356 /* The size in bytes of an entry in the global offset table. */
358 #define GOT_ENTRY_SIZE 8
360 /* The size in bytes of an entry in the procedure linkage table. */
362 #define PLT_ENTRY_SIZE 16
364 /* The first entry in a procedure linkage table looks like this. See the
365 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
367 static const bfd_byte elf64_x86_64_plt0_entry
[PLT_ENTRY_SIZE
] =
369 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
370 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
371 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
374 /* Subsequent entries in a procedure linkage table look like this. */
376 static const bfd_byte elf64_x86_64_plt_entry
[PLT_ENTRY_SIZE
] =
378 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
379 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
380 0x68, /* pushq immediate */
381 0, 0, 0, 0, /* replaced with index into relocation table. */
382 0xe9, /* jmp relative */
383 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
386 /* The x86-64 linker needs to keep track of the number of relocs that
387 it decides to copy as dynamic relocs in check_relocs for each symbol.
388 This is so that it can later discard them if they are found to be
389 unnecessary. We store the information in a field extending the
390 regular ELF linker hash table. */
392 struct elf64_x86_64_dyn_relocs
395 struct elf64_x86_64_dyn_relocs
*next
;
397 /* The input section of the reloc. */
400 /* Total number of relocs copied for the input section. */
403 /* Number of pc-relative relocs copied for the input section. */
404 bfd_size_type pc_count
;
407 /* x86-64 ELF linker hash entry. */
409 struct elf64_x86_64_link_hash_entry
411 struct elf_link_hash_entry elf
;
413 /* Track dynamic relocs copied for this symbol. */
414 struct elf64_x86_64_dyn_relocs
*dyn_relocs
;
416 #define GOT_UNKNOWN 0
420 #define GOT_TLS_GDESC 4
421 #define GOT_TLS_GD_BOTH_P(type) \
422 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
423 #define GOT_TLS_GD_P(type) \
424 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
425 #define GOT_TLS_GDESC_P(type) \
426 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
427 #define GOT_TLS_GD_ANY_P(type) \
428 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
429 unsigned char tls_type
;
431 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
432 starting at the end of the jump table. */
436 #define elf64_x86_64_hash_entry(ent) \
437 ((struct elf64_x86_64_link_hash_entry *)(ent))
439 struct elf64_x86_64_obj_tdata
441 struct elf_obj_tdata root
;
443 /* tls_type for each local got entry. */
444 char *local_got_tls_type
;
446 /* GOTPLT entries for TLS descriptors. */
447 bfd_vma
*local_tlsdesc_gotent
;
450 #define elf64_x86_64_tdata(abfd) \
451 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
453 #define elf64_x86_64_local_got_tls_type(abfd) \
454 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
456 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
457 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
459 /* x86-64 ELF linker hash table. */
461 struct elf64_x86_64_link_hash_table
463 struct elf_link_hash_table elf
;
465 /* Short-cuts to get to dynamic linker sections. */
474 /* The offset into splt of the PLT entry for the TLS descriptor
475 resolver. Special values are 0, if not necessary (or not found
476 to be necessary yet), and -1 if needed but not determined
479 /* The offset into sgot of the GOT entry used by the PLT entry
484 bfd_signed_vma refcount
;
488 /* The amount of space used by the jump slots in the GOT. */
489 bfd_vma sgotplt_jump_table_size
;
491 /* Small local sym to section mapping cache. */
492 struct sym_sec_cache sym_sec
;
495 /* Get the x86-64 ELF linker hash table from a link_info structure. */
497 #define elf64_x86_64_hash_table(p) \
498 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
500 #define elf64_x86_64_compute_jump_table_size(htab) \
501 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
503 /* Create an entry in an x86-64 ELF linker hash table. */
505 static struct bfd_hash_entry
*
506 link_hash_newfunc (struct bfd_hash_entry
*entry
, struct bfd_hash_table
*table
,
509 /* Allocate the structure if it has not already been allocated by a
513 entry
= bfd_hash_allocate (table
,
514 sizeof (struct elf64_x86_64_link_hash_entry
));
519 /* Call the allocation method of the superclass. */
520 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
523 struct elf64_x86_64_link_hash_entry
*eh
;
525 eh
= (struct elf64_x86_64_link_hash_entry
*) entry
;
526 eh
->dyn_relocs
= NULL
;
527 eh
->tls_type
= GOT_UNKNOWN
;
528 eh
->tlsdesc_got
= (bfd_vma
) -1;
534 /* Create an X86-64 ELF linker hash table. */
536 static struct bfd_link_hash_table
*
537 elf64_x86_64_link_hash_table_create (bfd
*abfd
)
539 struct elf64_x86_64_link_hash_table
*ret
;
540 bfd_size_type amt
= sizeof (struct elf64_x86_64_link_hash_table
);
542 ret
= (struct elf64_x86_64_link_hash_table
*) bfd_malloc (amt
);
546 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
547 sizeof (struct elf64_x86_64_link_hash_entry
)))
560 ret
->sym_sec
.abfd
= NULL
;
561 ret
->tlsdesc_plt
= 0;
562 ret
->tlsdesc_got
= 0;
563 ret
->tls_ld_got
.refcount
= 0;
564 ret
->sgotplt_jump_table_size
= 0;
566 return &ret
->elf
.root
;
569 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
570 shortcuts to them in our hash table. */
573 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
575 struct elf64_x86_64_link_hash_table
*htab
;
577 if (! _bfd_elf_create_got_section (dynobj
, info
))
580 htab
= elf64_x86_64_hash_table (info
);
581 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
582 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
583 if (!htab
->sgot
|| !htab
->sgotplt
)
586 htab
->srelgot
= bfd_make_section_with_flags (dynobj
, ".rela.got",
587 (SEC_ALLOC
| SEC_LOAD
592 if (htab
->srelgot
== NULL
593 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 3))
598 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
599 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
603 elf64_x86_64_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
605 struct elf64_x86_64_link_hash_table
*htab
;
607 htab
= elf64_x86_64_hash_table (info
);
608 if (!htab
->sgot
&& !create_got_section (dynobj
, info
))
611 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
614 htab
->splt
= bfd_get_section_by_name (dynobj
, ".plt");
615 htab
->srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
616 htab
->sdynbss
= bfd_get_section_by_name (dynobj
, ".dynbss");
618 htab
->srelbss
= bfd_get_section_by_name (dynobj
, ".rela.bss");
620 if (!htab
->splt
|| !htab
->srelplt
|| !htab
->sdynbss
621 || (!info
->shared
&& !htab
->srelbss
))
627 /* Copy the extra info we tack onto an elf_link_hash_entry. */
630 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info
*info
,
631 struct elf_link_hash_entry
*dir
,
632 struct elf_link_hash_entry
*ind
)
634 struct elf64_x86_64_link_hash_entry
*edir
, *eind
;
636 edir
= (struct elf64_x86_64_link_hash_entry
*) dir
;
637 eind
= (struct elf64_x86_64_link_hash_entry
*) ind
;
639 if (eind
->dyn_relocs
!= NULL
)
641 if (edir
->dyn_relocs
!= NULL
)
643 struct elf64_x86_64_dyn_relocs
**pp
;
644 struct elf64_x86_64_dyn_relocs
*p
;
646 /* Add reloc counts against the indirect sym to the direct sym
647 list. Merge any entries against the same section. */
648 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
650 struct elf64_x86_64_dyn_relocs
*q
;
652 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
653 if (q
->sec
== p
->sec
)
655 q
->pc_count
+= p
->pc_count
;
656 q
->count
+= p
->count
;
663 *pp
= edir
->dyn_relocs
;
666 edir
->dyn_relocs
= eind
->dyn_relocs
;
667 eind
->dyn_relocs
= NULL
;
670 if (ind
->root
.type
== bfd_link_hash_indirect
671 && dir
->got
.refcount
<= 0)
673 edir
->tls_type
= eind
->tls_type
;
674 eind
->tls_type
= GOT_UNKNOWN
;
677 if (ELIMINATE_COPY_RELOCS
678 && ind
->root
.type
!= bfd_link_hash_indirect
679 && dir
->dynamic_adjusted
)
681 /* If called to transfer flags for a weakdef during processing
682 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
683 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
684 dir
->ref_dynamic
|= ind
->ref_dynamic
;
685 dir
->ref_regular
|= ind
->ref_regular
;
686 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
687 dir
->needs_plt
|= ind
->needs_plt
;
688 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
691 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
695 elf64_x86_64_mkobject (bfd
*abfd
)
697 if (abfd
->tdata
.any
== NULL
)
699 bfd_size_type amt
= sizeof (struct elf64_x86_64_obj_tdata
);
700 abfd
->tdata
.any
= bfd_zalloc (abfd
, amt
);
701 if (abfd
->tdata
.any
== NULL
)
704 return bfd_elf_mkobject (abfd
);
708 elf64_x86_64_elf_object_p (bfd
*abfd
)
710 /* Set the right machine number for an x86-64 elf64 file. */
711 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x86_64
);
716 elf64_x86_64_tls_transition (struct bfd_link_info
*info
, int r_type
, int is_local
)
724 case R_X86_64_GOTPC32_TLSDESC
:
725 case R_X86_64_TLSDESC_CALL
:
726 case R_X86_64_GOTTPOFF
:
728 return R_X86_64_TPOFF32
;
729 return R_X86_64_GOTTPOFF
;
731 return R_X86_64_TPOFF32
;
737 /* Look through the relocs for a section during the first phase, and
738 calculate needed space in the global offset table, procedure
739 linkage table, and dynamic reloc sections. */
742 elf64_x86_64_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
*sec
,
743 const Elf_Internal_Rela
*relocs
)
745 struct elf64_x86_64_link_hash_table
*htab
;
746 Elf_Internal_Shdr
*symtab_hdr
;
747 struct elf_link_hash_entry
**sym_hashes
;
748 const Elf_Internal_Rela
*rel
;
749 const Elf_Internal_Rela
*rel_end
;
752 if (info
->relocatable
)
755 htab
= elf64_x86_64_hash_table (info
);
756 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
757 sym_hashes
= elf_sym_hashes (abfd
);
761 rel_end
= relocs
+ sec
->reloc_count
;
762 for (rel
= relocs
; rel
< rel_end
; rel
++)
765 unsigned long r_symndx
;
766 struct elf_link_hash_entry
*h
;
768 r_symndx
= ELF64_R_SYM (rel
->r_info
);
769 r_type
= ELF64_R_TYPE (rel
->r_info
);
771 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
773 (*_bfd_error_handler
) (_("%B: bad symbol index: %d"),
778 if (r_symndx
< symtab_hdr
->sh_info
)
782 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
783 while (h
->root
.type
== bfd_link_hash_indirect
784 || h
->root
.type
== bfd_link_hash_warning
)
785 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
788 r_type
= elf64_x86_64_tls_transition (info
, r_type
, h
== NULL
);
792 htab
->tls_ld_got
.refcount
+= 1;
795 case R_X86_64_TPOFF32
:
798 (*_bfd_error_handler
)
799 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
801 x86_64_elf_howto_table
[r_type
].name
,
802 (h
) ? h
->root
.root
.string
: "a local symbol");
803 bfd_set_error (bfd_error_bad_value
);
808 case R_X86_64_GOTTPOFF
:
810 info
->flags
|= DF_STATIC_TLS
;
814 case R_X86_64_GOTPCREL
:
817 case R_X86_64_GOTPCREL64
:
818 case R_X86_64_GOTPLT64
:
819 case R_X86_64_GOTPC32_TLSDESC
:
820 case R_X86_64_TLSDESC_CALL
:
821 /* This symbol requires a global offset table entry. */
823 int tls_type
, old_tls_type
;
827 default: tls_type
= GOT_NORMAL
; break;
828 case R_X86_64_TLSGD
: tls_type
= GOT_TLS_GD
; break;
829 case R_X86_64_GOTTPOFF
: tls_type
= GOT_TLS_IE
; break;
830 case R_X86_64_GOTPC32_TLSDESC
:
831 case R_X86_64_TLSDESC_CALL
:
832 tls_type
= GOT_TLS_GDESC
; break;
837 if (r_type
== R_X86_64_GOTPLT64
)
839 /* This relocation indicates that we also need
840 a PLT entry, as this is a function. We don't need
841 a PLT entry for local symbols. */
843 h
->plt
.refcount
+= 1;
845 h
->got
.refcount
+= 1;
846 old_tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
850 bfd_signed_vma
*local_got_refcounts
;
852 /* This is a global offset table entry for a local symbol. */
853 local_got_refcounts
= elf_local_got_refcounts (abfd
);
854 if (local_got_refcounts
== NULL
)
858 size
= symtab_hdr
->sh_info
;
859 size
*= sizeof (bfd_signed_vma
)
860 + sizeof (bfd_vma
) + sizeof (char);
861 local_got_refcounts
= ((bfd_signed_vma
*)
862 bfd_zalloc (abfd
, size
));
863 if (local_got_refcounts
== NULL
)
865 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
866 elf64_x86_64_local_tlsdesc_gotent (abfd
)
867 = (bfd_vma
*) (local_got_refcounts
+ symtab_hdr
->sh_info
);
868 elf64_x86_64_local_got_tls_type (abfd
)
869 = (char *) (local_got_refcounts
+ 2 * symtab_hdr
->sh_info
);
871 local_got_refcounts
[r_symndx
] += 1;
873 = elf64_x86_64_local_got_tls_type (abfd
) [r_symndx
];
876 /* If a TLS symbol is accessed using IE at least once,
877 there is no point to use dynamic model for it. */
878 if (old_tls_type
!= tls_type
&& old_tls_type
!= GOT_UNKNOWN
879 && (! GOT_TLS_GD_ANY_P (old_tls_type
)
880 || tls_type
!= GOT_TLS_IE
))
882 if (old_tls_type
== GOT_TLS_IE
&& GOT_TLS_GD_ANY_P (tls_type
))
883 tls_type
= old_tls_type
;
884 else if (GOT_TLS_GD_ANY_P (old_tls_type
)
885 && GOT_TLS_GD_ANY_P (tls_type
))
886 tls_type
|= old_tls_type
;
889 (*_bfd_error_handler
)
890 (_("%B: %s' accessed both as normal and thread local symbol"),
891 abfd
, h
? h
->root
.root
.string
: "<local>");
896 if (old_tls_type
!= tls_type
)
899 elf64_x86_64_hash_entry (h
)->tls_type
= tls_type
;
901 elf64_x86_64_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
906 case R_X86_64_GOTOFF64
:
907 case R_X86_64_GOTPC32
:
908 case R_X86_64_GOTPC64
:
910 if (htab
->sgot
== NULL
)
912 if (htab
->elf
.dynobj
== NULL
)
913 htab
->elf
.dynobj
= abfd
;
914 if (!create_got_section (htab
->elf
.dynobj
, info
))
920 /* This symbol requires a procedure linkage table entry. We
921 actually build the entry in adjust_dynamic_symbol,
922 because this might be a case of linking PIC code which is
923 never referenced by a dynamic object, in which case we
924 don't need to generate a procedure linkage table entry
927 /* If this is a local symbol, we resolve it directly without
928 creating a procedure linkage table entry. */
933 h
->plt
.refcount
+= 1;
936 case R_X86_64_PLTOFF64
:
937 /* This tries to form the 'address' of a function relative
938 to GOT. For global symbols we need a PLT entry. */
942 h
->plt
.refcount
+= 1;
950 /* Let's help debug shared library creation. These relocs
951 cannot be used in shared libs. Don't error out for
952 sections we don't care about, such as debug sections or
953 non-constant sections. */
955 && (sec
->flags
& SEC_ALLOC
) != 0
956 && (sec
->flags
& SEC_READONLY
) != 0)
958 (*_bfd_error_handler
)
959 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
961 x86_64_elf_howto_table
[r_type
].name
,
962 (h
) ? h
->root
.root
.string
: "a local symbol");
963 bfd_set_error (bfd_error_bad_value
);
973 if (h
!= NULL
&& !info
->shared
)
975 /* If this reloc is in a read-only section, we might
976 need a copy reloc. We can't check reliably at this
977 stage whether the section is read-only, as input
978 sections have not yet been mapped to output sections.
979 Tentatively set the flag for now, and correct in
980 adjust_dynamic_symbol. */
983 /* We may need a .plt entry if the function this reloc
984 refers to is in a shared lib. */
985 h
->plt
.refcount
+= 1;
986 if (r_type
!= R_X86_64_PC32
&& r_type
!= R_X86_64_PC64
)
987 h
->pointer_equality_needed
= 1;
990 /* If we are creating a shared library, and this is a reloc
991 against a global symbol, or a non PC relative reloc
992 against a local symbol, then we need to copy the reloc
993 into the shared library. However, if we are linking with
994 -Bsymbolic, we do not need to copy a reloc against a
995 global symbol which is defined in an object we are
996 including in the link (i.e., DEF_REGULAR is set). At
997 this point we have not seen all the input files, so it is
998 possible that DEF_REGULAR is not set now but will be set
999 later (it is never cleared). In case of a weak definition,
1000 DEF_REGULAR may be cleared later by a strong definition in
1001 a shared library. We account for that possibility below by
1002 storing information in the relocs_copied field of the hash
1003 table entry. A similar situation occurs when creating
1004 shared libraries and symbol visibility changes render the
1007 If on the other hand, we are creating an executable, we
1008 may need to keep relocations for symbols satisfied by a
1009 dynamic library if we manage to avoid copy relocs for the
1012 && (sec
->flags
& SEC_ALLOC
) != 0
1013 && (((r_type
!= R_X86_64_PC8
)
1014 && (r_type
!= R_X86_64_PC16
)
1015 && (r_type
!= R_X86_64_PC32
)
1016 && (r_type
!= R_X86_64_PC64
))
1018 && (! SYMBOLIC_BIND (info
, h
)
1019 || h
->root
.type
== bfd_link_hash_defweak
1020 || !h
->def_regular
))))
1021 || (ELIMINATE_COPY_RELOCS
1023 && (sec
->flags
& SEC_ALLOC
) != 0
1025 && (h
->root
.type
== bfd_link_hash_defweak
1026 || !h
->def_regular
)))
1028 struct elf64_x86_64_dyn_relocs
*p
;
1029 struct elf64_x86_64_dyn_relocs
**head
;
1031 /* We must copy these reloc types into the output file.
1032 Create a reloc section in dynobj and make room for
1039 name
= (bfd_elf_string_from_elf_section
1041 elf_elfheader (abfd
)->e_shstrndx
,
1042 elf_section_data (sec
)->rel_hdr
.sh_name
));
1046 if (! CONST_STRNEQ (name
, ".rela")
1047 || strcmp (bfd_get_section_name (abfd
, sec
),
1050 (*_bfd_error_handler
)
1051 (_("%B: bad relocation section name `%s\'"),
1055 if (htab
->elf
.dynobj
== NULL
)
1056 htab
->elf
.dynobj
= abfd
;
1058 dynobj
= htab
->elf
.dynobj
;
1060 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1065 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
1066 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
1067 if ((sec
->flags
& SEC_ALLOC
) != 0)
1068 flags
|= SEC_ALLOC
| SEC_LOAD
;
1069 sreloc
= bfd_make_section_with_flags (dynobj
,
1073 || ! bfd_set_section_alignment (dynobj
, sreloc
, 3))
1076 elf_section_data (sec
)->sreloc
= sreloc
;
1079 /* If this is a global symbol, we count the number of
1080 relocations we need for this symbol. */
1083 head
= &((struct elf64_x86_64_link_hash_entry
*) h
)->dyn_relocs
;
1088 /* Track dynamic relocs needed for local syms too.
1089 We really need local syms available to do this
1093 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
1098 /* Beware of type punned pointers vs strict aliasing
1100 vpp
= &(elf_section_data (s
)->local_dynrel
);
1101 head
= (struct elf64_x86_64_dyn_relocs
**)vpp
;
1105 if (p
== NULL
|| p
->sec
!= sec
)
1107 bfd_size_type amt
= sizeof *p
;
1108 p
= ((struct elf64_x86_64_dyn_relocs
*)
1109 bfd_alloc (htab
->elf
.dynobj
, amt
));
1120 if (r_type
== R_X86_64_PC8
1121 || r_type
== R_X86_64_PC16
1122 || r_type
== R_X86_64_PC32
1123 || r_type
== R_X86_64_PC64
)
1128 /* This relocation describes the C++ object vtable hierarchy.
1129 Reconstruct it for later use during GC. */
1130 case R_X86_64_GNU_VTINHERIT
:
1131 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
1135 /* This relocation describes which C++ vtable entries are actually
1136 used. Record for later use during GC. */
1137 case R_X86_64_GNU_VTENTRY
:
1138 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
1150 /* Return the section that should be marked against GC for a given
1154 elf64_x86_64_gc_mark_hook (asection
*sec
,
1155 struct bfd_link_info
*info
,
1156 Elf_Internal_Rela
*rel
,
1157 struct elf_link_hash_entry
*h
,
1158 Elf_Internal_Sym
*sym
)
1161 switch (ELF64_R_TYPE (rel
->r_info
))
1163 case R_X86_64_GNU_VTINHERIT
:
1164 case R_X86_64_GNU_VTENTRY
:
1168 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
1171 /* Update the got entry reference counts for the section being removed. */
1174 elf64_x86_64_gc_sweep_hook (bfd
*abfd
, struct bfd_link_info
*info
,
1175 asection
*sec
, const Elf_Internal_Rela
*relocs
)
1177 Elf_Internal_Shdr
*symtab_hdr
;
1178 struct elf_link_hash_entry
**sym_hashes
;
1179 bfd_signed_vma
*local_got_refcounts
;
1180 const Elf_Internal_Rela
*rel
, *relend
;
1182 elf_section_data (sec
)->local_dynrel
= NULL
;
1184 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1185 sym_hashes
= elf_sym_hashes (abfd
);
1186 local_got_refcounts
= elf_local_got_refcounts (abfd
);
1188 relend
= relocs
+ sec
->reloc_count
;
1189 for (rel
= relocs
; rel
< relend
; rel
++)
1191 unsigned long r_symndx
;
1192 unsigned int r_type
;
1193 struct elf_link_hash_entry
*h
= NULL
;
1195 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1196 if (r_symndx
>= symtab_hdr
->sh_info
)
1198 struct elf64_x86_64_link_hash_entry
*eh
;
1199 struct elf64_x86_64_dyn_relocs
**pp
;
1200 struct elf64_x86_64_dyn_relocs
*p
;
1202 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1203 while (h
->root
.type
== bfd_link_hash_indirect
1204 || h
->root
.type
== bfd_link_hash_warning
)
1205 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1206 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1208 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
1211 /* Everything must go for SEC. */
1217 r_type
= ELF64_R_TYPE (rel
->r_info
);
1218 r_type
= elf64_x86_64_tls_transition (info
, r_type
, h
!= NULL
);
1221 case R_X86_64_TLSLD
:
1222 if (elf64_x86_64_hash_table (info
)->tls_ld_got
.refcount
> 0)
1223 elf64_x86_64_hash_table (info
)->tls_ld_got
.refcount
-= 1;
1226 case R_X86_64_TLSGD
:
1227 case R_X86_64_GOTPC32_TLSDESC
:
1228 case R_X86_64_TLSDESC_CALL
:
1229 case R_X86_64_GOTTPOFF
:
1230 case R_X86_64_GOT32
:
1231 case R_X86_64_GOTPCREL
:
1232 case R_X86_64_GOT64
:
1233 case R_X86_64_GOTPCREL64
:
1234 case R_X86_64_GOTPLT64
:
1237 if (r_type
== R_X86_64_GOTPLT64
&& h
->plt
.refcount
> 0)
1238 h
->plt
.refcount
-= 1;
1239 if (h
->got
.refcount
> 0)
1240 h
->got
.refcount
-= 1;
1242 else if (local_got_refcounts
!= NULL
)
1244 if (local_got_refcounts
[r_symndx
] > 0)
1245 local_got_refcounts
[r_symndx
] -= 1;
1262 case R_X86_64_PLT32
:
1263 case R_X86_64_PLTOFF64
:
1266 if (h
->plt
.refcount
> 0)
1267 h
->plt
.refcount
-= 1;
1279 /* Adjust a symbol defined by a dynamic object and referenced by a
1280 regular object. The current definition is in some section of the
1281 dynamic object, but we're not including those sections. We have to
1282 change the definition to something the rest of the link can
1286 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info
*info
,
1287 struct elf_link_hash_entry
*h
)
1289 struct elf64_x86_64_link_hash_table
*htab
;
1292 /* If this is a function, put it in the procedure linkage table. We
1293 will fill in the contents of the procedure linkage table later,
1294 when we know the address of the .got section. */
1295 if (h
->type
== STT_FUNC
1298 if (h
->plt
.refcount
<= 0
1299 || SYMBOL_CALLS_LOCAL (info
, h
)
1300 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1301 && h
->root
.type
== bfd_link_hash_undefweak
))
1303 /* This case can occur if we saw a PLT32 reloc in an input
1304 file, but the symbol was never referred to by a dynamic
1305 object, or if all references were garbage collected. In
1306 such a case, we don't actually need to build a procedure
1307 linkage table, and we can just do a PC32 reloc instead. */
1308 h
->plt
.offset
= (bfd_vma
) -1;
1315 /* It's possible that we incorrectly decided a .plt reloc was
1316 needed for an R_X86_64_PC32 reloc to a non-function sym in
1317 check_relocs. We can't decide accurately between function and
1318 non-function syms in check-relocs; Objects loaded later in
1319 the link may change h->type. So fix it now. */
1320 h
->plt
.offset
= (bfd_vma
) -1;
1322 /* If this is a weak symbol, and there is a real definition, the
1323 processor independent code will have arranged for us to see the
1324 real definition first, and we can just use the same value. */
1325 if (h
->u
.weakdef
!= NULL
)
1327 BFD_ASSERT (h
->u
.weakdef
->root
.type
== bfd_link_hash_defined
1328 || h
->u
.weakdef
->root
.type
== bfd_link_hash_defweak
);
1329 h
->root
.u
.def
.section
= h
->u
.weakdef
->root
.u
.def
.section
;
1330 h
->root
.u
.def
.value
= h
->u
.weakdef
->root
.u
.def
.value
;
1331 if (ELIMINATE_COPY_RELOCS
|| info
->nocopyreloc
)
1332 h
->non_got_ref
= h
->u
.weakdef
->non_got_ref
;
1336 /* This is a reference to a symbol defined by a dynamic object which
1337 is not a function. */
1339 /* If we are creating a shared library, we must presume that the
1340 only references to the symbol are via the global offset table.
1341 For such cases we need not do anything here; the relocations will
1342 be handled correctly by relocate_section. */
1346 /* If there are no references to this symbol that do not use the
1347 GOT, we don't need to generate a copy reloc. */
1348 if (!h
->non_got_ref
)
1351 /* If -z nocopyreloc was given, we won't generate them either. */
1352 if (info
->nocopyreloc
)
1358 if (ELIMINATE_COPY_RELOCS
)
1360 struct elf64_x86_64_link_hash_entry
* eh
;
1361 struct elf64_x86_64_dyn_relocs
*p
;
1363 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1364 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1366 s
= p
->sec
->output_section
;
1367 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1371 /* If we didn't find any dynamic relocs in read-only sections, then
1372 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1382 (*_bfd_error_handler
) (_("dynamic variable `%s' is zero size"),
1383 h
->root
.root
.string
);
1387 /* We must allocate the symbol in our .dynbss section, which will
1388 become part of the .bss section of the executable. There will be
1389 an entry for this symbol in the .dynsym section. The dynamic
1390 object will contain position independent code, so all references
1391 from the dynamic object to this symbol will go through the global
1392 offset table. The dynamic linker will use the .dynsym entry to
1393 determine the address it must put in the global offset table, so
1394 both the dynamic object and the regular object will refer to the
1395 same memory location for the variable. */
1397 htab
= elf64_x86_64_hash_table (info
);
1399 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1400 to copy the initial value out of the dynamic object and into the
1401 runtime process image. */
1402 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
1404 htab
->srelbss
->size
+= sizeof (Elf64_External_Rela
);
1410 return _bfd_elf_adjust_dynamic_copy (h
, s
);
1413 /* Allocate space in .plt, .got and associated reloc sections for
1417 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
1419 struct bfd_link_info
*info
;
1420 struct elf64_x86_64_link_hash_table
*htab
;
1421 struct elf64_x86_64_link_hash_entry
*eh
;
1422 struct elf64_x86_64_dyn_relocs
*p
;
1424 if (h
->root
.type
== bfd_link_hash_indirect
)
1427 if (h
->root
.type
== bfd_link_hash_warning
)
1428 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1430 info
= (struct bfd_link_info
*) inf
;
1431 htab
= elf64_x86_64_hash_table (info
);
1433 if (htab
->elf
.dynamic_sections_created
1434 && h
->plt
.refcount
> 0)
1436 /* Make sure this symbol is output as a dynamic symbol.
1437 Undefined weak syms won't yet be marked as dynamic. */
1438 if (h
->dynindx
== -1
1439 && !h
->forced_local
)
1441 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1446 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
1448 asection
*s
= htab
->splt
;
1450 /* If this is the first .plt entry, make room for the special
1453 s
->size
+= PLT_ENTRY_SIZE
;
1455 h
->plt
.offset
= s
->size
;
1457 /* If this symbol is not defined in a regular file, and we are
1458 not generating a shared library, then set the symbol to this
1459 location in the .plt. This is required to make function
1460 pointers compare as equal between the normal executable and
1461 the shared library. */
1465 h
->root
.u
.def
.section
= s
;
1466 h
->root
.u
.def
.value
= h
->plt
.offset
;
1469 /* Make room for this entry. */
1470 s
->size
+= PLT_ENTRY_SIZE
;
1472 /* We also need to make an entry in the .got.plt section, which
1473 will be placed in the .got section by the linker script. */
1474 htab
->sgotplt
->size
+= GOT_ENTRY_SIZE
;
1476 /* We also need to make an entry in the .rela.plt section. */
1477 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
1478 htab
->srelplt
->reloc_count
++;
1482 h
->plt
.offset
= (bfd_vma
) -1;
1488 h
->plt
.offset
= (bfd_vma
) -1;
1492 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1493 eh
->tlsdesc_got
= (bfd_vma
) -1;
1495 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1496 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1497 if (h
->got
.refcount
> 0
1500 && elf64_x86_64_hash_entry (h
)->tls_type
== GOT_TLS_IE
)
1501 h
->got
.offset
= (bfd_vma
) -1;
1502 else if (h
->got
.refcount
> 0)
1506 int tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
1508 /* Make sure this symbol is output as a dynamic symbol.
1509 Undefined weak syms won't yet be marked as dynamic. */
1510 if (h
->dynindx
== -1
1511 && !h
->forced_local
)
1513 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1517 if (GOT_TLS_GDESC_P (tls_type
))
1519 eh
->tlsdesc_got
= htab
->sgotplt
->size
1520 - elf64_x86_64_compute_jump_table_size (htab
);
1521 htab
->sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
1522 h
->got
.offset
= (bfd_vma
) -2;
1524 if (! GOT_TLS_GDESC_P (tls_type
)
1525 || GOT_TLS_GD_P (tls_type
))
1528 h
->got
.offset
= s
->size
;
1529 s
->size
+= GOT_ENTRY_SIZE
;
1530 if (GOT_TLS_GD_P (tls_type
))
1531 s
->size
+= GOT_ENTRY_SIZE
;
1533 dyn
= htab
->elf
.dynamic_sections_created
;
1534 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1536 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1537 if ((GOT_TLS_GD_P (tls_type
) && h
->dynindx
== -1)
1538 || tls_type
== GOT_TLS_IE
)
1539 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
1540 else if (GOT_TLS_GD_P (tls_type
))
1541 htab
->srelgot
->size
+= 2 * sizeof (Elf64_External_Rela
);
1542 else if (! GOT_TLS_GDESC_P (tls_type
)
1543 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
1544 || h
->root
.type
!= bfd_link_hash_undefweak
)
1546 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
1547 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
1548 if (GOT_TLS_GDESC_P (tls_type
))
1550 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
1551 htab
->tlsdesc_plt
= (bfd_vma
) -1;
1555 h
->got
.offset
= (bfd_vma
) -1;
1557 if (eh
->dyn_relocs
== NULL
)
1560 /* In the shared -Bsymbolic case, discard space allocated for
1561 dynamic pc-relative relocs against symbols which turn out to be
1562 defined in regular objects. For the normal shared case, discard
1563 space for pc-relative relocs that have become local due to symbol
1564 visibility changes. */
1568 /* Relocs that use pc_count are those that appear on a call
1569 insn, or certain REL relocs that can generated via assembly.
1570 We want calls to protected symbols to resolve directly to the
1571 function rather than going via the plt. If people want
1572 function pointer comparisons to work as expected then they
1573 should avoid writing weird assembly. */
1574 if (SYMBOL_CALLS_LOCAL (info
, h
))
1576 struct elf64_x86_64_dyn_relocs
**pp
;
1578 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
1580 p
->count
-= p
->pc_count
;
1589 /* Also discard relocs on undefined weak syms with non-default
1591 if (eh
->dyn_relocs
!= NULL
1592 && h
->root
.type
== bfd_link_hash_undefweak
)
1594 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
1595 eh
->dyn_relocs
= NULL
;
1597 /* Make sure undefined weak symbols are output as a dynamic
1599 else if (h
->dynindx
== -1
1600 && !h
->forced_local
)
1602 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1607 else if (ELIMINATE_COPY_RELOCS
)
1609 /* For the non-shared case, discard space for relocs against
1610 symbols which turn out to need copy relocs or are not
1616 || (htab
->elf
.dynamic_sections_created
1617 && (h
->root
.type
== bfd_link_hash_undefweak
1618 || h
->root
.type
== bfd_link_hash_undefined
))))
1620 /* Make sure this symbol is output as a dynamic symbol.
1621 Undefined weak syms won't yet be marked as dynamic. */
1622 if (h
->dynindx
== -1
1623 && !h
->forced_local
)
1625 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1629 /* If that succeeded, we know we'll be keeping all the
1631 if (h
->dynindx
!= -1)
1635 eh
->dyn_relocs
= NULL
;
1640 /* Finally, allocate space. */
1641 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1643 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1644 sreloc
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
1650 /* Find any dynamic relocs that apply to read-only sections. */
1653 readonly_dynrelocs (struct elf_link_hash_entry
*h
, void * inf
)
1655 struct elf64_x86_64_link_hash_entry
*eh
;
1656 struct elf64_x86_64_dyn_relocs
*p
;
1658 if (h
->root
.type
== bfd_link_hash_warning
)
1659 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1661 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1662 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1664 asection
*s
= p
->sec
->output_section
;
1666 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1668 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
1670 info
->flags
|= DF_TEXTREL
;
1672 /* Not an error, just cut short the traversal. */
1679 /* Set the sizes of the dynamic sections. */
1682 elf64_x86_64_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1683 struct bfd_link_info
*info
)
1685 struct elf64_x86_64_link_hash_table
*htab
;
1691 htab
= elf64_x86_64_hash_table (info
);
1692 dynobj
= htab
->elf
.dynobj
;
1696 if (htab
->elf
.dynamic_sections_created
)
1698 /* Set the contents of the .interp section to the interpreter. */
1699 if (info
->executable
)
1701 s
= bfd_get_section_by_name (dynobj
, ".interp");
1704 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1705 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1709 /* Set up .got offsets for local syms, and space for local dynamic
1711 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
1713 bfd_signed_vma
*local_got
;
1714 bfd_signed_vma
*end_local_got
;
1715 char *local_tls_type
;
1716 bfd_vma
*local_tlsdesc_gotent
;
1717 bfd_size_type locsymcount
;
1718 Elf_Internal_Shdr
*symtab_hdr
;
1721 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
1724 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1726 struct elf64_x86_64_dyn_relocs
*p
;
1728 for (p
= (struct elf64_x86_64_dyn_relocs
*)
1729 (elf_section_data (s
)->local_dynrel
);
1733 if (!bfd_is_abs_section (p
->sec
)
1734 && bfd_is_abs_section (p
->sec
->output_section
))
1736 /* Input section has been discarded, either because
1737 it is a copy of a linkonce section or due to
1738 linker script /DISCARD/, so we'll be discarding
1741 else if (p
->count
!= 0)
1743 srel
= elf_section_data (p
->sec
)->sreloc
;
1744 srel
->size
+= p
->count
* sizeof (Elf64_External_Rela
);
1745 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1746 info
->flags
|= DF_TEXTREL
;
1752 local_got
= elf_local_got_refcounts (ibfd
);
1756 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
1757 locsymcount
= symtab_hdr
->sh_info
;
1758 end_local_got
= local_got
+ locsymcount
;
1759 local_tls_type
= elf64_x86_64_local_got_tls_type (ibfd
);
1760 local_tlsdesc_gotent
= elf64_x86_64_local_tlsdesc_gotent (ibfd
);
1762 srel
= htab
->srelgot
;
1763 for (; local_got
< end_local_got
;
1764 ++local_got
, ++local_tls_type
, ++local_tlsdesc_gotent
)
1766 *local_tlsdesc_gotent
= (bfd_vma
) -1;
1769 if (GOT_TLS_GDESC_P (*local_tls_type
))
1771 *local_tlsdesc_gotent
= htab
->sgotplt
->size
1772 - elf64_x86_64_compute_jump_table_size (htab
);
1773 htab
->sgotplt
->size
+= 2 * GOT_ENTRY_SIZE
;
1774 *local_got
= (bfd_vma
) -2;
1776 if (! GOT_TLS_GDESC_P (*local_tls_type
)
1777 || GOT_TLS_GD_P (*local_tls_type
))
1779 *local_got
= s
->size
;
1780 s
->size
+= GOT_ENTRY_SIZE
;
1781 if (GOT_TLS_GD_P (*local_tls_type
))
1782 s
->size
+= GOT_ENTRY_SIZE
;
1785 || GOT_TLS_GD_ANY_P (*local_tls_type
)
1786 || *local_tls_type
== GOT_TLS_IE
)
1788 if (GOT_TLS_GDESC_P (*local_tls_type
))
1790 htab
->srelplt
->size
+= sizeof (Elf64_External_Rela
);
1791 htab
->tlsdesc_plt
= (bfd_vma
) -1;
1793 if (! GOT_TLS_GDESC_P (*local_tls_type
)
1794 || GOT_TLS_GD_P (*local_tls_type
))
1795 srel
->size
+= sizeof (Elf64_External_Rela
);
1799 *local_got
= (bfd_vma
) -1;
1803 if (htab
->tls_ld_got
.refcount
> 0)
1805 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1807 htab
->tls_ld_got
.offset
= htab
->sgot
->size
;
1808 htab
->sgot
->size
+= 2 * GOT_ENTRY_SIZE
;
1809 htab
->srelgot
->size
+= sizeof (Elf64_External_Rela
);
1812 htab
->tls_ld_got
.offset
= -1;
1814 /* Allocate global sym .plt and .got entries, and space for global
1815 sym dynamic relocs. */
1816 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, (PTR
) info
);
1818 /* For every jump slot reserved in the sgotplt, reloc_count is
1819 incremented. However, when we reserve space for TLS descriptors,
1820 it's not incremented, so in order to compute the space reserved
1821 for them, it suffices to multiply the reloc count by the jump
1824 htab
->sgotplt_jump_table_size
1825 = elf64_x86_64_compute_jump_table_size (htab
);
1827 if (htab
->tlsdesc_plt
)
1829 /* If we're not using lazy TLS relocations, don't generate the
1830 PLT and GOT entries they require. */
1831 if ((info
->flags
& DF_BIND_NOW
))
1832 htab
->tlsdesc_plt
= 0;
1835 htab
->tlsdesc_got
= htab
->sgot
->size
;
1836 htab
->sgot
->size
+= GOT_ENTRY_SIZE
;
1837 /* Reserve room for the initial entry.
1838 FIXME: we could probably do away with it in this case. */
1839 if (htab
->splt
->size
== 0)
1840 htab
->splt
->size
+= PLT_ENTRY_SIZE
;
1841 htab
->tlsdesc_plt
= htab
->splt
->size
;
1842 htab
->splt
->size
+= PLT_ENTRY_SIZE
;
1846 /* We now have determined the sizes of the various dynamic sections.
1847 Allocate memory for them. */
1849 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1851 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1856 || s
== htab
->sgotplt
1857 || s
== htab
->sdynbss
)
1859 /* Strip this section if we don't need it; see the
1862 else if (CONST_STRNEQ (bfd_get_section_name (dynobj
, s
), ".rela"))
1864 if (s
->size
!= 0 && s
!= htab
->srelplt
)
1867 /* We use the reloc_count field as a counter if we need
1868 to copy relocs into the output file. */
1869 if (s
!= htab
->srelplt
)
1874 /* It's not one of our sections, so don't allocate space. */
1880 /* If we don't need this section, strip it from the
1881 output file. This is mostly to handle .rela.bss and
1882 .rela.plt. We must create both sections in
1883 create_dynamic_sections, because they must be created
1884 before the linker maps input sections to output
1885 sections. The linker does that before
1886 adjust_dynamic_symbol is called, and it is that
1887 function which decides whether anything needs to go
1888 into these sections. */
1890 s
->flags
|= SEC_EXCLUDE
;
1894 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1897 /* Allocate memory for the section contents. We use bfd_zalloc
1898 here in case unused entries are not reclaimed before the
1899 section's contents are written out. This should not happen,
1900 but this way if it does, we get a R_X86_64_NONE reloc instead
1902 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1903 if (s
->contents
== NULL
)
1907 if (htab
->elf
.dynamic_sections_created
)
1909 /* Add some entries to the .dynamic section. We fill in the
1910 values later, in elf64_x86_64_finish_dynamic_sections, but we
1911 must add the entries now so that we get the correct size for
1912 the .dynamic section. The DT_DEBUG entry is filled in by the
1913 dynamic linker and used by the debugger. */
1914 #define add_dynamic_entry(TAG, VAL) \
1915 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1917 if (info
->executable
)
1919 if (!add_dynamic_entry (DT_DEBUG
, 0))
1923 if (htab
->splt
->size
!= 0)
1925 if (!add_dynamic_entry (DT_PLTGOT
, 0)
1926 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
1927 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
1928 || !add_dynamic_entry (DT_JMPREL
, 0))
1931 if (htab
->tlsdesc_plt
1932 && (!add_dynamic_entry (DT_TLSDESC_PLT
, 0)
1933 || !add_dynamic_entry (DT_TLSDESC_GOT
, 0)))
1939 if (!add_dynamic_entry (DT_RELA
, 0)
1940 || !add_dynamic_entry (DT_RELASZ
, 0)
1941 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf64_External_Rela
)))
1944 /* If any dynamic relocs apply to a read-only section,
1945 then we need a DT_TEXTREL entry. */
1946 if ((info
->flags
& DF_TEXTREL
) == 0)
1947 elf_link_hash_traverse (&htab
->elf
, readonly_dynrelocs
,
1950 if ((info
->flags
& DF_TEXTREL
) != 0)
1952 if (!add_dynamic_entry (DT_TEXTREL
, 0))
1957 #undef add_dynamic_entry
1963 elf64_x86_64_always_size_sections (bfd
*output_bfd
,
1964 struct bfd_link_info
*info
)
1966 asection
*tls_sec
= elf_hash_table (info
)->tls_sec
;
1970 struct elf_link_hash_entry
*tlsbase
;
1972 tlsbase
= elf_link_hash_lookup (elf_hash_table (info
),
1973 "_TLS_MODULE_BASE_",
1974 FALSE
, FALSE
, FALSE
);
1976 if (tlsbase
&& tlsbase
->type
== STT_TLS
)
1978 struct bfd_link_hash_entry
*bh
= NULL
;
1979 const struct elf_backend_data
*bed
1980 = get_elf_backend_data (output_bfd
);
1982 if (!(_bfd_generic_link_add_one_symbol
1983 (info
, output_bfd
, "_TLS_MODULE_BASE_", BSF_LOCAL
,
1984 tls_sec
, 0, NULL
, FALSE
,
1985 bed
->collect
, &bh
)))
1987 tlsbase
= (struct elf_link_hash_entry
*)bh
;
1988 tlsbase
->def_regular
= 1;
1989 tlsbase
->other
= STV_HIDDEN
;
1990 (*bed
->elf_backend_hide_symbol
) (info
, tlsbase
, TRUE
);
1997 /* Return the base VMA address which should be subtracted from real addresses
1998 when resolving @dtpoff relocation.
1999 This is PT_TLS segment p_vaddr. */
2002 dtpoff_base (struct bfd_link_info
*info
)
2004 /* If tls_sec is NULL, we should have signalled an error already. */
2005 if (elf_hash_table (info
)->tls_sec
== NULL
)
2007 return elf_hash_table (info
)->tls_sec
->vma
;
2010 /* Return the relocation value for @tpoff relocation
2011 if STT_TLS virtual address is ADDRESS. */
2014 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
2016 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
2018 /* If tls_segment is NULL, we should have signalled an error already. */
2019 if (htab
->tls_sec
== NULL
)
2021 return address
- htab
->tls_size
- htab
->tls_sec
->vma
;
2024 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2028 is_32bit_relative_branch (bfd_byte
*contents
, bfd_vma offset
)
2030 /* Opcode Instruction
2033 0x0f 0x8x conditional jump */
2035 && (contents
[offset
- 1] == 0xe8
2036 || contents
[offset
- 1] == 0xe9))
2038 && contents
[offset
- 2] == 0x0f
2039 && (contents
[offset
- 1] & 0xf0) == 0x80));
2042 /* Relocate an x86_64 ELF section. */
2045 elf64_x86_64_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
2046 bfd
*input_bfd
, asection
*input_section
,
2047 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
2048 Elf_Internal_Sym
*local_syms
,
2049 asection
**local_sections
)
2051 struct elf64_x86_64_link_hash_table
*htab
;
2052 Elf_Internal_Shdr
*symtab_hdr
;
2053 struct elf_link_hash_entry
**sym_hashes
;
2054 bfd_vma
*local_got_offsets
;
2055 bfd_vma
*local_tlsdesc_gotents
;
2056 Elf_Internal_Rela
*rel
;
2057 Elf_Internal_Rela
*relend
;
2059 htab
= elf64_x86_64_hash_table (info
);
2060 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2061 sym_hashes
= elf_sym_hashes (input_bfd
);
2062 local_got_offsets
= elf_local_got_offsets (input_bfd
);
2063 local_tlsdesc_gotents
= elf64_x86_64_local_tlsdesc_gotent (input_bfd
);
2066 relend
= relocs
+ input_section
->reloc_count
;
2067 for (; rel
< relend
; rel
++)
2069 unsigned int r_type
;
2070 reloc_howto_type
*howto
;
2071 unsigned long r_symndx
;
2072 struct elf_link_hash_entry
*h
;
2073 Elf_Internal_Sym
*sym
;
2075 bfd_vma off
, offplt
;
2077 bfd_boolean unresolved_reloc
;
2078 bfd_reloc_status_type r
;
2081 r_type
= ELF64_R_TYPE (rel
->r_info
);
2082 if (r_type
== (int) R_X86_64_GNU_VTINHERIT
2083 || r_type
== (int) R_X86_64_GNU_VTENTRY
)
2086 if (r_type
>= R_X86_64_max
)
2088 bfd_set_error (bfd_error_bad_value
);
2092 howto
= x86_64_elf_howto_table
+ r_type
;
2093 r_symndx
= ELF64_R_SYM (rel
->r_info
);
2097 unresolved_reloc
= FALSE
;
2098 if (r_symndx
< symtab_hdr
->sh_info
)
2100 sym
= local_syms
+ r_symndx
;
2101 sec
= local_sections
[r_symndx
];
2103 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2109 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2110 r_symndx
, symtab_hdr
, sym_hashes
,
2112 unresolved_reloc
, warned
);
2115 if (sec
!= NULL
&& elf_discarded_section (sec
))
2117 /* For relocs against symbols from removed linkonce sections,
2118 or sections discarded by a linker script, we just want the
2119 section contents zeroed. Avoid any special processing. */
2120 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
2126 if (info
->relocatable
)
2129 /* When generating a shared object, the relocations handled here are
2130 copied into the output file to be resolved at run time. */
2134 case R_X86_64_GOT32
:
2135 case R_X86_64_GOT64
:
2136 /* Relocation is to the entry for this symbol in the global
2138 case R_X86_64_GOTPCREL
:
2139 case R_X86_64_GOTPCREL64
:
2140 /* Use global offset table entry as symbol value. */
2141 case R_X86_64_GOTPLT64
:
2142 /* This is the same as GOT64 for relocation purposes, but
2143 indicates the existence of a PLT entry. The difficulty is,
2144 that we must calculate the GOT slot offset from the PLT
2145 offset, if this symbol got a PLT entry (it was global).
2146 Additionally if it's computed from the PLT entry, then that
2147 GOT offset is relative to .got.plt, not to .got. */
2148 base_got
= htab
->sgot
;
2150 if (htab
->sgot
== NULL
)
2157 off
= h
->got
.offset
;
2159 && h
->plt
.offset
!= (bfd_vma
)-1
2160 && off
== (bfd_vma
)-1)
2162 /* We can't use h->got.offset here to save
2163 state, or even just remember the offset, as
2164 finish_dynamic_symbol would use that as offset into
2166 bfd_vma plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
2167 off
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
2168 base_got
= htab
->sgotplt
;
2171 dyn
= htab
->elf
.dynamic_sections_created
;
2173 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
2175 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2176 || (ELF_ST_VISIBILITY (h
->other
)
2177 && h
->root
.type
== bfd_link_hash_undefweak
))
2179 /* This is actually a static link, or it is a -Bsymbolic
2180 link and the symbol is defined locally, or the symbol
2181 was forced to be local because of a version file. We
2182 must initialize this entry in the global offset table.
2183 Since the offset must always be a multiple of 8, we
2184 use the least significant bit to record whether we
2185 have initialized it already.
2187 When doing a dynamic link, we create a .rela.got
2188 relocation entry to initialize the value. This is
2189 done in the finish_dynamic_symbol routine. */
2194 bfd_put_64 (output_bfd
, relocation
,
2195 base_got
->contents
+ off
);
2196 /* Note that this is harmless for the GOTPLT64 case,
2197 as -1 | 1 still is -1. */
2202 unresolved_reloc
= FALSE
;
2206 if (local_got_offsets
== NULL
)
2209 off
= local_got_offsets
[r_symndx
];
2211 /* The offset must always be a multiple of 8. We use
2212 the least significant bit to record whether we have
2213 already generated the necessary reloc. */
2218 bfd_put_64 (output_bfd
, relocation
,
2219 base_got
->contents
+ off
);
2224 Elf_Internal_Rela outrel
;
2227 /* We need to generate a R_X86_64_RELATIVE reloc
2228 for the dynamic linker. */
2233 outrel
.r_offset
= (base_got
->output_section
->vma
2234 + base_got
->output_offset
2236 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
2237 outrel
.r_addend
= relocation
;
2239 loc
+= s
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2240 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2243 local_got_offsets
[r_symndx
] |= 1;
2247 if (off
>= (bfd_vma
) -2)
2250 relocation
= base_got
->output_section
->vma
2251 + base_got
->output_offset
+ off
;
2252 if (r_type
!= R_X86_64_GOTPCREL
&& r_type
!= R_X86_64_GOTPCREL64
)
2253 relocation
-= htab
->sgotplt
->output_section
->vma
2254 - htab
->sgotplt
->output_offset
;
2258 case R_X86_64_GOTOFF64
:
2259 /* Relocation is relative to the start of the global offset
2262 /* Check to make sure it isn't a protected function symbol
2263 for shared library since it may not be local when used
2264 as function address. */
2268 && h
->type
== STT_FUNC
2269 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
2271 (*_bfd_error_handler
)
2272 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2273 input_bfd
, h
->root
.root
.string
);
2274 bfd_set_error (bfd_error_bad_value
);
2278 /* Note that sgot is not involved in this
2279 calculation. We always want the start of .got.plt. If we
2280 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2281 permitted by the ABI, we might have to change this
2283 relocation
-= htab
->sgotplt
->output_section
->vma
2284 + htab
->sgotplt
->output_offset
;
2287 case R_X86_64_GOTPC32
:
2288 case R_X86_64_GOTPC64
:
2289 /* Use global offset table as symbol value. */
2290 relocation
= htab
->sgotplt
->output_section
->vma
2291 + htab
->sgotplt
->output_offset
;
2292 unresolved_reloc
= FALSE
;
2295 case R_X86_64_PLTOFF64
:
2296 /* Relocation is PLT entry relative to GOT. For local
2297 symbols it's the symbol itself relative to GOT. */
2299 /* See PLT32 handling. */
2300 && h
->plt
.offset
!= (bfd_vma
) -1
2301 && htab
->splt
!= NULL
)
2303 relocation
= (htab
->splt
->output_section
->vma
2304 + htab
->splt
->output_offset
2306 unresolved_reloc
= FALSE
;
2309 relocation
-= htab
->sgotplt
->output_section
->vma
2310 + htab
->sgotplt
->output_offset
;
2313 case R_X86_64_PLT32
:
2314 /* Relocation is to the entry for this symbol in the
2315 procedure linkage table. */
2317 /* Resolve a PLT32 reloc against a local symbol directly,
2318 without using the procedure linkage table. */
2322 if (h
->plt
.offset
== (bfd_vma
) -1
2323 || htab
->splt
== NULL
)
2325 /* We didn't make a PLT entry for this symbol. This
2326 happens when statically linking PIC code, or when
2327 using -Bsymbolic. */
2331 relocation
= (htab
->splt
->output_section
->vma
2332 + htab
->splt
->output_offset
2334 unresolved_reloc
= FALSE
;
2341 && !SYMBOL_REFERENCES_LOCAL (info
, h
)
2342 && (input_section
->flags
& SEC_ALLOC
) != 0
2343 && (input_section
->flags
& SEC_READONLY
) != 0
2345 || r_type
!= R_X86_64_PC32
2346 || h
->type
!= STT_FUNC
2347 || ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
2348 || !is_32bit_relative_branch (contents
,
2352 && r_type
== R_X86_64_PC32
2353 && h
->type
== STT_FUNC
2354 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
2355 (*_bfd_error_handler
)
2356 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
2357 input_bfd
, h
->root
.root
.string
);
2359 (*_bfd_error_handler
)
2360 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2361 input_bfd
, x86_64_elf_howto_table
[r_type
].name
,
2362 h
->root
.root
.string
);
2363 bfd_set_error (bfd_error_bad_value
);
2373 /* FIXME: The ABI says the linker should make sure the value is
2374 the same when it's zeroextended to 64 bit. */
2376 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2381 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2382 || h
->root
.type
!= bfd_link_hash_undefweak
)
2383 && ((r_type
!= R_X86_64_PC8
2384 && r_type
!= R_X86_64_PC16
2385 && r_type
!= R_X86_64_PC32
2386 && r_type
!= R_X86_64_PC64
)
2387 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2388 || (ELIMINATE_COPY_RELOCS
2395 || h
->root
.type
== bfd_link_hash_undefweak
2396 || h
->root
.type
== bfd_link_hash_undefined
)))
2398 Elf_Internal_Rela outrel
;
2400 bfd_boolean skip
, relocate
;
2403 /* When generating a shared object, these relocations
2404 are copied into the output file to be resolved at run
2410 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2412 if (outrel
.r_offset
== (bfd_vma
) -1)
2414 else if (outrel
.r_offset
== (bfd_vma
) -2)
2415 skip
= TRUE
, relocate
= TRUE
;
2417 outrel
.r_offset
+= (input_section
->output_section
->vma
2418 + input_section
->output_offset
);
2421 memset (&outrel
, 0, sizeof outrel
);
2423 /* h->dynindx may be -1 if this symbol was marked to
2427 && (r_type
== R_X86_64_PC8
2428 || r_type
== R_X86_64_PC16
2429 || r_type
== R_X86_64_PC32
2430 || r_type
== R_X86_64_PC64
2432 || !SYMBOLIC_BIND (info
, h
)
2433 || !h
->def_regular
))
2435 outrel
.r_info
= ELF64_R_INFO (h
->dynindx
, r_type
);
2436 outrel
.r_addend
= rel
->r_addend
;
2440 /* This symbol is local, or marked to become local. */
2441 if (r_type
== R_X86_64_64
)
2444 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
2445 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2451 if (bfd_is_abs_section (sec
))
2453 else if (sec
== NULL
|| sec
->owner
== NULL
)
2455 bfd_set_error (bfd_error_bad_value
);
2462 /* We are turning this relocation into one
2463 against a section symbol. It would be
2464 proper to subtract the symbol's value,
2465 osec->vma, from the emitted reloc addend,
2466 but ld.so expects buggy relocs. */
2467 osec
= sec
->output_section
;
2468 sindx
= elf_section_data (osec
)->dynindx
;
2471 asection
*oi
= htab
->elf
.text_index_section
;
2472 sindx
= elf_section_data (oi
)->dynindx
;
2474 BFD_ASSERT (sindx
!= 0);
2477 outrel
.r_info
= ELF64_R_INFO (sindx
, r_type
);
2478 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2482 sreloc
= elf_section_data (input_section
)->sreloc
;
2486 loc
= sreloc
->contents
;
2487 loc
+= sreloc
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2488 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2490 /* If this reloc is against an external symbol, we do
2491 not want to fiddle with the addend. Otherwise, we
2492 need to include the symbol value so that it becomes
2493 an addend for the dynamic reloc. */
2500 case R_X86_64_TLSGD
:
2501 case R_X86_64_GOTPC32_TLSDESC
:
2502 case R_X86_64_TLSDESC_CALL
:
2503 case R_X86_64_GOTTPOFF
:
2504 r_type
= elf64_x86_64_tls_transition (info
, r_type
, h
== NULL
);
2505 tls_type
= GOT_UNKNOWN
;
2506 if (h
== NULL
&& local_got_offsets
)
2507 tls_type
= elf64_x86_64_local_got_tls_type (input_bfd
) [r_symndx
];
2510 tls_type
= elf64_x86_64_hash_entry (h
)->tls_type
;
2511 if (!info
->shared
&& h
->dynindx
== -1 && tls_type
== GOT_TLS_IE
)
2512 r_type
= R_X86_64_TPOFF32
;
2514 if (r_type
== R_X86_64_TLSGD
2515 || r_type
== R_X86_64_GOTPC32_TLSDESC
2516 || r_type
== R_X86_64_TLSDESC_CALL
)
2518 if (tls_type
== GOT_TLS_IE
)
2519 r_type
= R_X86_64_GOTTPOFF
;
2522 if (r_type
== R_X86_64_TPOFF32
)
2524 BFD_ASSERT (! unresolved_reloc
);
2525 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
2528 static unsigned char tlsgd
[8]
2529 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2530 unsigned long tls_r_symndx
;
2531 struct elf_link_hash_entry
*tls_h
;
2533 /* GD->LE transition.
2534 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2535 .word 0x6666; rex64; call __tls_get_addr
2538 leaq foo@tpoff(%rax), %rax */
2539 BFD_ASSERT (rel
->r_offset
>= 4);
2540 for (i
= 0; i
< 4; i
++)
2541 BFD_ASSERT (bfd_get_8 (input_bfd
,
2542 contents
+ rel
->r_offset
- 4 + i
)
2544 BFD_ASSERT (rel
->r_offset
+ 12 <= input_section
->size
);
2545 for (i
= 0; i
< 4; i
++)
2546 BFD_ASSERT (bfd_get_8 (input_bfd
,
2547 contents
+ rel
->r_offset
+ 4 + i
)
2549 BFD_ASSERT (rel
+ 1 < relend
);
2550 tls_r_symndx
= ELF64_R_SYM (rel
[1].r_info
);
2551 BFD_ASSERT (tls_r_symndx
>= symtab_hdr
->sh_info
);
2552 tls_h
= sym_hashes
[tls_r_symndx
- symtab_hdr
->sh_info
];
2553 BFD_ASSERT (tls_h
!= NULL
2554 && tls_h
->root
.root
.string
!= NULL
2555 && strcmp (tls_h
->root
.root
.string
,
2556 "__tls_get_addr") == 0);
2557 BFD_ASSERT ((! info
->shared
2558 && ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PC32
)
2559 || ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PLT32
);
2560 memcpy (contents
+ rel
->r_offset
- 4,
2561 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2563 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2564 contents
+ rel
->r_offset
+ 8);
2565 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
2569 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
2571 /* GDesc -> LE transition.
2572 It's originally something like:
2573 leaq x@tlsdesc(%rip), %rax
2578 Registers other than %rax may be set up here. */
2580 unsigned int val
, type
, type2
;
2583 /* First, make sure it's a leaq adding rip to a
2584 32-bit offset into any register, although it's
2585 probably almost always going to be rax. */
2586 roff
= rel
->r_offset
;
2587 BFD_ASSERT (roff
>= 3);
2588 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
2589 BFD_ASSERT ((type
& 0xfb) == 0x48);
2590 type2
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
2591 BFD_ASSERT (type2
== 0x8d);
2592 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
2593 BFD_ASSERT ((val
& 0xc7) == 0x05);
2594 BFD_ASSERT (roff
+ 4 <= input_section
->size
);
2596 /* Now modify the instruction as appropriate. */
2597 bfd_put_8 (output_bfd
, 0x48 | ((type
>> 2) & 1),
2598 contents
+ roff
- 3);
2599 bfd_put_8 (output_bfd
, 0xc7, contents
+ roff
- 2);
2600 bfd_put_8 (output_bfd
, 0xc0 | ((val
>> 3) & 7),
2601 contents
+ roff
- 1);
2602 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2606 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
2608 /* GDesc -> LE transition.
2614 unsigned int val
, type
;
2617 /* First, make sure it's a call *(%rax). */
2618 roff
= rel
->r_offset
;
2619 BFD_ASSERT (roff
+ 2 <= input_section
->size
);
2620 type
= bfd_get_8 (input_bfd
, contents
+ roff
);
2621 BFD_ASSERT (type
== 0xff);
2622 val
= bfd_get_8 (input_bfd
, contents
+ roff
+ 1);
2623 BFD_ASSERT (val
== 0x10);
2625 /* Now modify the instruction as appropriate. Use
2626 xchg %ax,%ax instead of 2 nops. */
2627 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
2628 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
2633 unsigned int val
, type
, reg
;
2635 /* IE->LE transition:
2636 Originally it can be one of:
2637 movq foo@gottpoff(%rip), %reg
2638 addq foo@gottpoff(%rip), %reg
2641 leaq foo(%reg), %reg
2643 BFD_ASSERT (rel
->r_offset
>= 3);
2644 val
= bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 3);
2645 BFD_ASSERT (val
== 0x48 || val
== 0x4c);
2646 type
= bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 2);
2647 BFD_ASSERT (type
== 0x8b || type
== 0x03);
2648 reg
= bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 1);
2649 BFD_ASSERT ((reg
& 0xc7) == 5);
2651 BFD_ASSERT (rel
->r_offset
+ 4 <= input_section
->size
);
2656 bfd_put_8 (output_bfd
, 0x49,
2657 contents
+ rel
->r_offset
- 3);
2658 bfd_put_8 (output_bfd
, 0xc7,
2659 contents
+ rel
->r_offset
- 2);
2660 bfd_put_8 (output_bfd
, 0xc0 | reg
,
2661 contents
+ rel
->r_offset
- 1);
2665 /* addq -> addq - addressing with %rsp/%r12 is
2668 bfd_put_8 (output_bfd
, 0x49,
2669 contents
+ rel
->r_offset
- 3);
2670 bfd_put_8 (output_bfd
, 0x81,
2671 contents
+ rel
->r_offset
- 2);
2672 bfd_put_8 (output_bfd
, 0xc0 | reg
,
2673 contents
+ rel
->r_offset
- 1);
2679 bfd_put_8 (output_bfd
, 0x4d,
2680 contents
+ rel
->r_offset
- 3);
2681 bfd_put_8 (output_bfd
, 0x8d,
2682 contents
+ rel
->r_offset
- 2);
2683 bfd_put_8 (output_bfd
, 0x80 | reg
| (reg
<< 3),
2684 contents
+ rel
->r_offset
- 1);
2686 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2687 contents
+ rel
->r_offset
);
2692 if (htab
->sgot
== NULL
)
2697 off
= h
->got
.offset
;
2698 offplt
= elf64_x86_64_hash_entry (h
)->tlsdesc_got
;
2702 if (local_got_offsets
== NULL
)
2705 off
= local_got_offsets
[r_symndx
];
2706 offplt
= local_tlsdesc_gotents
[r_symndx
];
2713 Elf_Internal_Rela outrel
;
2718 if (htab
->srelgot
== NULL
)
2721 indx
= h
&& h
->dynindx
!= -1 ? h
->dynindx
: 0;
2723 if (GOT_TLS_GDESC_P (tls_type
))
2725 outrel
.r_info
= ELF64_R_INFO (indx
, R_X86_64_TLSDESC
);
2726 BFD_ASSERT (htab
->sgotplt_jump_table_size
+ offplt
2727 + 2 * GOT_ENTRY_SIZE
<= htab
->sgotplt
->size
);
2728 outrel
.r_offset
= (htab
->sgotplt
->output_section
->vma
2729 + htab
->sgotplt
->output_offset
2731 + htab
->sgotplt_jump_table_size
);
2732 sreloc
= htab
->srelplt
;
2733 loc
= sreloc
->contents
;
2734 loc
+= sreloc
->reloc_count
++
2735 * sizeof (Elf64_External_Rela
);
2736 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
2737 <= sreloc
->contents
+ sreloc
->size
);
2739 outrel
.r_addend
= relocation
- dtpoff_base (info
);
2741 outrel
.r_addend
= 0;
2742 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2745 sreloc
= htab
->srelgot
;
2747 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
2748 + htab
->sgot
->output_offset
+ off
);
2750 if (GOT_TLS_GD_P (tls_type
))
2751 dr_type
= R_X86_64_DTPMOD64
;
2752 else if (GOT_TLS_GDESC_P (tls_type
))
2755 dr_type
= R_X86_64_TPOFF64
;
2757 bfd_put_64 (output_bfd
, 0, htab
->sgot
->contents
+ off
);
2758 outrel
.r_addend
= 0;
2759 if ((dr_type
== R_X86_64_TPOFF64
2760 || dr_type
== R_X86_64_TLSDESC
) && indx
== 0)
2761 outrel
.r_addend
= relocation
- dtpoff_base (info
);
2762 outrel
.r_info
= ELF64_R_INFO (indx
, dr_type
);
2764 loc
= sreloc
->contents
;
2765 loc
+= sreloc
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2766 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
2767 <= sreloc
->contents
+ sreloc
->size
);
2768 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2770 if (GOT_TLS_GD_P (tls_type
))
2774 BFD_ASSERT (! unresolved_reloc
);
2775 bfd_put_64 (output_bfd
,
2776 relocation
- dtpoff_base (info
),
2777 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
2781 bfd_put_64 (output_bfd
, 0,
2782 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
2783 outrel
.r_info
= ELF64_R_INFO (indx
,
2785 outrel
.r_offset
+= GOT_ENTRY_SIZE
;
2786 sreloc
->reloc_count
++;
2787 loc
+= sizeof (Elf64_External_Rela
);
2788 BFD_ASSERT (loc
+ sizeof (Elf64_External_Rela
)
2789 <= sreloc
->contents
+ sreloc
->size
);
2790 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2798 local_got_offsets
[r_symndx
] |= 1;
2801 if (off
>= (bfd_vma
) -2
2802 && ! GOT_TLS_GDESC_P (tls_type
))
2804 if (r_type
== ELF64_R_TYPE (rel
->r_info
))
2806 if (r_type
== R_X86_64_GOTPC32_TLSDESC
2807 || r_type
== R_X86_64_TLSDESC_CALL
)
2808 relocation
= htab
->sgotplt
->output_section
->vma
2809 + htab
->sgotplt
->output_offset
2810 + offplt
+ htab
->sgotplt_jump_table_size
;
2812 relocation
= htab
->sgot
->output_section
->vma
2813 + htab
->sgot
->output_offset
+ off
;
2814 unresolved_reloc
= FALSE
;
2816 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSGD
)
2819 static unsigned char tlsgd
[8]
2820 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2822 /* GD->IE transition.
2823 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2824 .word 0x6666; rex64; call __tls_get_addr@plt
2827 addq foo@gottpoff(%rip), %rax */
2828 BFD_ASSERT (rel
->r_offset
>= 4);
2829 for (i
= 0; i
< 4; i
++)
2830 BFD_ASSERT (bfd_get_8 (input_bfd
,
2831 contents
+ rel
->r_offset
- 4 + i
)
2833 BFD_ASSERT (rel
->r_offset
+ 12 <= input_section
->size
);
2834 for (i
= 0; i
< 4; i
++)
2835 BFD_ASSERT (bfd_get_8 (input_bfd
,
2836 contents
+ rel
->r_offset
+ 4 + i
)
2838 BFD_ASSERT (rel
+ 1 < relend
);
2839 BFD_ASSERT (ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PLT32
);
2840 memcpy (contents
+ rel
->r_offset
- 4,
2841 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2844 relocation
= (htab
->sgot
->output_section
->vma
2845 + htab
->sgot
->output_offset
+ off
2847 - input_section
->output_section
->vma
2848 - input_section
->output_offset
2850 bfd_put_32 (output_bfd
, relocation
,
2851 contents
+ rel
->r_offset
+ 8);
2852 /* Skip R_X86_64_PLT32. */
2856 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_GOTPC32_TLSDESC
)
2858 /* GDesc -> IE transition.
2859 It's originally something like:
2860 leaq x@tlsdesc(%rip), %rax
2863 movq x@gottpoff(%rip), %rax # before nop; nop
2865 Registers other than %rax may be set up here. */
2867 unsigned int val
, type
, type2
;
2870 /* First, make sure it's a leaq adding rip to a 32-bit
2871 offset into any register, although it's probably
2872 almost always going to be rax. */
2873 roff
= rel
->r_offset
;
2874 BFD_ASSERT (roff
>= 3);
2875 type
= bfd_get_8 (input_bfd
, contents
+ roff
- 3);
2876 BFD_ASSERT ((type
& 0xfb) == 0x48);
2877 type2
= bfd_get_8 (input_bfd
, contents
+ roff
- 2);
2878 BFD_ASSERT (type2
== 0x8d);
2879 val
= bfd_get_8 (input_bfd
, contents
+ roff
- 1);
2880 BFD_ASSERT ((val
& 0xc7) == 0x05);
2881 BFD_ASSERT (roff
+ 4 <= input_section
->size
);
2883 /* Now modify the instruction as appropriate. */
2884 /* To turn a leaq into a movq in the form we use it, it
2885 suffices to change the second byte from 0x8d to
2887 bfd_put_8 (output_bfd
, 0x8b, contents
+ roff
- 2);
2889 bfd_put_32 (output_bfd
,
2890 htab
->sgot
->output_section
->vma
2891 + htab
->sgot
->output_offset
+ off
2893 - input_section
->output_section
->vma
2894 - input_section
->output_offset
2899 else if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_TLSDESC_CALL
)
2901 /* GDesc -> IE transition.
2908 unsigned int val
, type
;
2911 /* First, make sure it's a call *(%eax). */
2912 roff
= rel
->r_offset
;
2913 BFD_ASSERT (roff
+ 2 <= input_section
->size
);
2914 type
= bfd_get_8 (input_bfd
, contents
+ roff
);
2915 BFD_ASSERT (type
== 0xff);
2916 val
= bfd_get_8 (input_bfd
, contents
+ roff
+ 1);
2917 BFD_ASSERT (val
== 0x10);
2919 /* Now modify the instruction as appropriate. Use
2920 xchg %ax,%ax instead of 2 nops. */
2921 bfd_put_8 (output_bfd
, 0x66, contents
+ roff
);
2922 bfd_put_8 (output_bfd
, 0x90, contents
+ roff
+ 1);
2930 case R_X86_64_TLSLD
:
2933 unsigned long tls_r_symndx
;
2934 struct elf_link_hash_entry
*tls_h
;
2936 /* LD->LE transition:
2938 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
2940 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2941 BFD_ASSERT (rel
->r_offset
>= 3);
2942 BFD_ASSERT (bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 3)
2944 BFD_ASSERT (bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 2)
2946 BFD_ASSERT (bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
- 1)
2948 BFD_ASSERT (rel
->r_offset
+ 9 <= input_section
->size
);
2949 BFD_ASSERT (bfd_get_8 (input_bfd
, contents
+ rel
->r_offset
+ 4)
2951 BFD_ASSERT (rel
+ 1 < relend
);
2952 tls_r_symndx
= ELF64_R_SYM (rel
[1].r_info
);
2953 BFD_ASSERT (tls_r_symndx
>= symtab_hdr
->sh_info
);
2954 tls_h
= sym_hashes
[tls_r_symndx
- symtab_hdr
->sh_info
];
2955 BFD_ASSERT (tls_h
!= NULL
2956 && tls_h
->root
.root
.string
!= NULL
2957 && strcmp (tls_h
->root
.root
.string
,
2958 "__tls_get_addr") == 0);
2959 BFD_ASSERT (ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PC32
2960 || ELF64_R_TYPE (rel
[1].r_info
) == R_X86_64_PLT32
);
2961 memcpy (contents
+ rel
->r_offset
- 3,
2962 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2963 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
2968 if (htab
->sgot
== NULL
)
2971 off
= htab
->tls_ld_got
.offset
;
2976 Elf_Internal_Rela outrel
;
2979 if (htab
->srelgot
== NULL
)
2982 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
2983 + htab
->sgot
->output_offset
+ off
);
2985 bfd_put_64 (output_bfd
, 0,
2986 htab
->sgot
->contents
+ off
);
2987 bfd_put_64 (output_bfd
, 0,
2988 htab
->sgot
->contents
+ off
+ GOT_ENTRY_SIZE
);
2989 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_DTPMOD64
);
2990 outrel
.r_addend
= 0;
2991 loc
= htab
->srelgot
->contents
;
2992 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf64_External_Rela
);
2993 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
2994 htab
->tls_ld_got
.offset
|= 1;
2996 relocation
= htab
->sgot
->output_section
->vma
2997 + htab
->sgot
->output_offset
+ off
;
2998 unresolved_reloc
= FALSE
;
3001 case R_X86_64_DTPOFF32
:
3002 if (info
->shared
|| (input_section
->flags
& SEC_CODE
) == 0)
3003 relocation
-= dtpoff_base (info
);
3005 relocation
= tpoff (info
, relocation
);
3008 case R_X86_64_TPOFF32
:
3009 BFD_ASSERT (! info
->shared
);
3010 relocation
= tpoff (info
, relocation
);
3017 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3018 because such sections are not SEC_ALLOC and thus ld.so will
3019 not process them. */
3020 if (unresolved_reloc
3021 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
3023 (*_bfd_error_handler
)
3024 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3027 (long) rel
->r_offset
,
3029 h
->root
.root
.string
);
3031 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3032 contents
, rel
->r_offset
,
3033 relocation
, rel
->r_addend
);
3035 if (r
!= bfd_reloc_ok
)
3040 name
= h
->root
.root
.string
;
3043 name
= bfd_elf_string_from_elf_section (input_bfd
,
3044 symtab_hdr
->sh_link
,
3049 name
= bfd_section_name (input_bfd
, sec
);
3052 if (r
== bfd_reloc_overflow
)
3054 if (! ((*info
->callbacks
->reloc_overflow
)
3055 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3056 (bfd_vma
) 0, input_bfd
, input_section
,
3062 (*_bfd_error_handler
)
3063 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3064 input_bfd
, input_section
,
3065 (long) rel
->r_offset
, name
, (int) r
);
3074 /* Finish up dynamic symbol handling. We set the contents of various
3075 dynamic sections here. */
3078 elf64_x86_64_finish_dynamic_symbol (bfd
*output_bfd
,
3079 struct bfd_link_info
*info
,
3080 struct elf_link_hash_entry
*h
,
3081 Elf_Internal_Sym
*sym
)
3083 struct elf64_x86_64_link_hash_table
*htab
;
3085 htab
= elf64_x86_64_hash_table (info
);
3087 if (h
->plt
.offset
!= (bfd_vma
) -1)
3091 Elf_Internal_Rela rela
;
3094 /* This symbol has an entry in the procedure linkage table. Set
3096 if (h
->dynindx
== -1
3097 || htab
->splt
== NULL
3098 || htab
->sgotplt
== NULL
3099 || htab
->srelplt
== NULL
)
3102 /* Get the index in the procedure linkage table which
3103 corresponds to this symbol. This is the index of this symbol
3104 in all the symbols for which we are making plt entries. The
3105 first entry in the procedure linkage table is reserved. */
3106 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
3108 /* Get the offset into the .got table of the entry that
3109 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3110 bytes. The first three are reserved for the dynamic linker. */
3111 got_offset
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
3113 /* Fill in the entry in the procedure linkage table. */
3114 memcpy (htab
->splt
->contents
+ h
->plt
.offset
, elf64_x86_64_plt_entry
,
3117 /* Insert the relocation positions of the plt section. The magic
3118 numbers at the end of the statements are the positions of the
3119 relocations in the plt section. */
3120 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3121 instruction uses 6 bytes, subtract this value. */
3122 bfd_put_32 (output_bfd
,
3123 (htab
->sgotplt
->output_section
->vma
3124 + htab
->sgotplt
->output_offset
3126 - htab
->splt
->output_section
->vma
3127 - htab
->splt
->output_offset
3130 htab
->splt
->contents
+ h
->plt
.offset
+ 2);
3131 /* Put relocation index. */
3132 bfd_put_32 (output_bfd
, plt_index
,
3133 htab
->splt
->contents
+ h
->plt
.offset
+ 7);
3134 /* Put offset for jmp .PLT0. */
3135 bfd_put_32 (output_bfd
, - (h
->plt
.offset
+ PLT_ENTRY_SIZE
),
3136 htab
->splt
->contents
+ h
->plt
.offset
+ 12);
3138 /* Fill in the entry in the global offset table, initially this
3139 points to the pushq instruction in the PLT which is at offset 6. */
3140 bfd_put_64 (output_bfd
, (htab
->splt
->output_section
->vma
3141 + htab
->splt
->output_offset
3142 + h
->plt
.offset
+ 6),
3143 htab
->sgotplt
->contents
+ got_offset
);
3145 /* Fill in the entry in the .rela.plt section. */
3146 rela
.r_offset
= (htab
->sgotplt
->output_section
->vma
3147 + htab
->sgotplt
->output_offset
3149 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_JUMP_SLOT
);
3151 loc
= htab
->srelplt
->contents
+ plt_index
* sizeof (Elf64_External_Rela
);
3152 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3154 if (!h
->def_regular
)
3156 /* Mark the symbol as undefined, rather than as defined in
3157 the .plt section. Leave the value if there were any
3158 relocations where pointer equality matters (this is a clue
3159 for the dynamic linker, to make function pointer
3160 comparisons work between an application and shared
3161 library), otherwise set it to zero. If a function is only
3162 called from a binary, there is no need to slow down
3163 shared libraries because of that. */
3164 sym
->st_shndx
= SHN_UNDEF
;
3165 if (!h
->pointer_equality_needed
)
3170 if (h
->got
.offset
!= (bfd_vma
) -1
3171 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h
)->tls_type
)
3172 && elf64_x86_64_hash_entry (h
)->tls_type
!= GOT_TLS_IE
)
3174 Elf_Internal_Rela rela
;
3177 /* This symbol has an entry in the global offset table. Set it
3179 if (htab
->sgot
== NULL
|| htab
->srelgot
== NULL
)
3182 rela
.r_offset
= (htab
->sgot
->output_section
->vma
3183 + htab
->sgot
->output_offset
3184 + (h
->got
.offset
&~ (bfd_vma
) 1));
3186 /* If this is a static link, or it is a -Bsymbolic link and the
3187 symbol is defined locally or was forced to be local because
3188 of a version file, we just want to emit a RELATIVE reloc.
3189 The entry in the global offset table will already have been
3190 initialized in the relocate_section function. */
3192 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3194 BFD_ASSERT((h
->got
.offset
& 1) != 0);
3195 rela
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
3196 rela
.r_addend
= (h
->root
.u
.def
.value
3197 + h
->root
.u
.def
.section
->output_section
->vma
3198 + h
->root
.u
.def
.section
->output_offset
);
3202 BFD_ASSERT((h
->got
.offset
& 1) == 0);
3203 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
3204 htab
->sgot
->contents
+ h
->got
.offset
);
3205 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_GLOB_DAT
);
3209 loc
= htab
->srelgot
->contents
;
3210 loc
+= htab
->srelgot
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3211 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3216 Elf_Internal_Rela rela
;
3219 /* This symbol needs a copy reloc. Set it up. */
3221 if (h
->dynindx
== -1
3222 || (h
->root
.type
!= bfd_link_hash_defined
3223 && h
->root
.type
!= bfd_link_hash_defweak
)
3224 || htab
->srelbss
== NULL
)
3227 rela
.r_offset
= (h
->root
.u
.def
.value
3228 + h
->root
.u
.def
.section
->output_section
->vma
3229 + h
->root
.u
.def
.section
->output_offset
);
3230 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_COPY
);
3232 loc
= htab
->srelbss
->contents
;
3233 loc
+= htab
->srelbss
->reloc_count
++ * sizeof (Elf64_External_Rela
);
3234 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
3237 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3238 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
3239 || h
== htab
->elf
.hgot
)
3240 sym
->st_shndx
= SHN_ABS
;
3245 /* Used to decide how to sort relocs in an optimal manner for the
3246 dynamic linker, before writing them out. */
3248 static enum elf_reloc_type_class
3249 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela
*rela
)
3251 switch ((int) ELF64_R_TYPE (rela
->r_info
))
3253 case R_X86_64_RELATIVE
:
3254 return reloc_class_relative
;
3255 case R_X86_64_JUMP_SLOT
:
3256 return reloc_class_plt
;
3258 return reloc_class_copy
;
3260 return reloc_class_normal
;
3264 /* Finish up the dynamic sections. */
3267 elf64_x86_64_finish_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
3269 struct elf64_x86_64_link_hash_table
*htab
;
3273 htab
= elf64_x86_64_hash_table (info
);
3274 dynobj
= htab
->elf
.dynobj
;
3275 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3277 if (htab
->elf
.dynamic_sections_created
)
3279 Elf64_External_Dyn
*dyncon
, *dynconend
;
3281 if (sdyn
== NULL
|| htab
->sgot
== NULL
)
3284 dyncon
= (Elf64_External_Dyn
*) sdyn
->contents
;
3285 dynconend
= (Elf64_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
3286 for (; dyncon
< dynconend
; dyncon
++)
3288 Elf_Internal_Dyn dyn
;
3291 bfd_elf64_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3300 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3304 dyn
.d_un
.d_ptr
= htab
->srelplt
->output_section
->vma
;
3308 s
= htab
->srelplt
->output_section
;
3309 dyn
.d_un
.d_val
= s
->size
;
3313 /* The procedure linkage table relocs (DT_JMPREL) should
3314 not be included in the overall relocs (DT_RELA).
3315 Therefore, we override the DT_RELASZ entry here to
3316 make it not include the JMPREL relocs. Since the
3317 linker script arranges for .rela.plt to follow all
3318 other relocation sections, we don't have to worry
3319 about changing the DT_RELA entry. */
3320 if (htab
->srelplt
!= NULL
)
3322 s
= htab
->srelplt
->output_section
;
3323 dyn
.d_un
.d_val
-= s
->size
;
3327 case DT_TLSDESC_PLT
:
3329 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
3330 + htab
->tlsdesc_plt
;
3333 case DT_TLSDESC_GOT
:
3335 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
3336 + htab
->tlsdesc_got
;
3340 bfd_elf64_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3343 /* Fill in the special first entry in the procedure linkage table. */
3344 if (htab
->splt
&& htab
->splt
->size
> 0)
3346 /* Fill in the first entry in the procedure linkage table. */
3347 memcpy (htab
->splt
->contents
, elf64_x86_64_plt0_entry
,
3349 /* Add offset for pushq GOT+8(%rip), since the instruction
3350 uses 6 bytes subtract this value. */
3351 bfd_put_32 (output_bfd
,
3352 (htab
->sgotplt
->output_section
->vma
3353 + htab
->sgotplt
->output_offset
3355 - htab
->splt
->output_section
->vma
3356 - htab
->splt
->output_offset
3358 htab
->splt
->contents
+ 2);
3359 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3360 the end of the instruction. */
3361 bfd_put_32 (output_bfd
,
3362 (htab
->sgotplt
->output_section
->vma
3363 + htab
->sgotplt
->output_offset
3365 - htab
->splt
->output_section
->vma
3366 - htab
->splt
->output_offset
3368 htab
->splt
->contents
+ 8);
3370 elf_section_data (htab
->splt
->output_section
)->this_hdr
.sh_entsize
=
3373 if (htab
->tlsdesc_plt
)
3375 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
3376 htab
->sgot
->contents
+ htab
->tlsdesc_got
);
3378 memcpy (htab
->splt
->contents
+ htab
->tlsdesc_plt
,
3379 elf64_x86_64_plt0_entry
,
3382 /* Add offset for pushq GOT+8(%rip), since the
3383 instruction uses 6 bytes subtract this value. */
3384 bfd_put_32 (output_bfd
,
3385 (htab
->sgotplt
->output_section
->vma
3386 + htab
->sgotplt
->output_offset
3388 - htab
->splt
->output_section
->vma
3389 - htab
->splt
->output_offset
3392 htab
->splt
->contents
+ htab
->tlsdesc_plt
+ 2);
3393 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3394 htab->tlsdesc_got. The 12 is the offset to the end of
3396 bfd_put_32 (output_bfd
,
3397 (htab
->sgot
->output_section
->vma
3398 + htab
->sgot
->output_offset
3400 - htab
->splt
->output_section
->vma
3401 - htab
->splt
->output_offset
3404 htab
->splt
->contents
+ htab
->tlsdesc_plt
+ 8);
3411 /* Fill in the first three entries in the global offset table. */
3412 if (htab
->sgotplt
->size
> 0)
3414 /* Set the first entry in the global offset table to the address of
3415 the dynamic section. */
3417 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
);
3419 bfd_put_64 (output_bfd
,
3420 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
3421 htab
->sgotplt
->contents
);
3422 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3423 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3424 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
*2);
3427 elf_section_data (htab
->sgotplt
->output_section
)->this_hdr
.sh_entsize
=
3431 if (htab
->sgot
&& htab
->sgot
->size
> 0)
3432 elf_section_data (htab
->sgot
->output_section
)->this_hdr
.sh_entsize
3438 /* Return address for Ith PLT stub in section PLT, for relocation REL
3439 or (bfd_vma) -1 if it should not be included. */
3442 elf64_x86_64_plt_sym_val (bfd_vma i
, const asection
*plt
,
3443 const arelent
*rel ATTRIBUTE_UNUSED
)
3445 return plt
->vma
+ (i
+ 1) * PLT_ENTRY_SIZE
;
3448 /* Handle an x86-64 specific section when reading an object file. This
3449 is called when elfcode.h finds a section with an unknown type. */
3452 elf64_x86_64_section_from_shdr (bfd
*abfd
,
3453 Elf_Internal_Shdr
*hdr
,
3457 if (hdr
->sh_type
!= SHT_X86_64_UNWIND
)
3460 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
3466 /* Hook called by the linker routine which adds symbols from an object
3467 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3471 elf64_x86_64_add_symbol_hook (bfd
*abfd
,
3472 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3473 Elf_Internal_Sym
*sym
,
3474 const char **namep ATTRIBUTE_UNUSED
,
3475 flagword
*flagsp ATTRIBUTE_UNUSED
,
3476 asection
**secp
, bfd_vma
*valp
)
3480 switch (sym
->st_shndx
)
3482 case SHN_X86_64_LCOMMON
:
3483 lcomm
= bfd_get_section_by_name (abfd
, "LARGE_COMMON");
3486 lcomm
= bfd_make_section_with_flags (abfd
,
3490 | SEC_LINKER_CREATED
));
3493 elf_section_flags (lcomm
) |= SHF_X86_64_LARGE
;
3496 *valp
= sym
->st_size
;
3503 /* Given a BFD section, try to locate the corresponding ELF section
3507 elf64_x86_64_elf_section_from_bfd_section (bfd
*abfd ATTRIBUTE_UNUSED
,
3508 asection
*sec
, int *index
)
3510 if (sec
== &_bfd_elf_large_com_section
)
3512 *index
= SHN_X86_64_LCOMMON
;
3518 /* Process a symbol. */
3521 elf64_x86_64_symbol_processing (bfd
*abfd ATTRIBUTE_UNUSED
,
3524 elf_symbol_type
*elfsym
= (elf_symbol_type
*) asym
;
3526 switch (elfsym
->internal_elf_sym
.st_shndx
)
3528 case SHN_X86_64_LCOMMON
:
3529 asym
->section
= &_bfd_elf_large_com_section
;
3530 asym
->value
= elfsym
->internal_elf_sym
.st_size
;
3531 /* Common symbol doesn't set BSF_GLOBAL. */
3532 asym
->flags
&= ~BSF_GLOBAL
;
3538 elf64_x86_64_common_definition (Elf_Internal_Sym
*sym
)
3540 return (sym
->st_shndx
== SHN_COMMON
3541 || sym
->st_shndx
== SHN_X86_64_LCOMMON
);
3545 elf64_x86_64_common_section_index (asection
*sec
)
3547 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
3550 return SHN_X86_64_LCOMMON
;
3554 elf64_x86_64_common_section (asection
*sec
)
3556 if ((elf_section_flags (sec
) & SHF_X86_64_LARGE
) == 0)
3557 return bfd_com_section_ptr
;
3559 return &_bfd_elf_large_com_section
;
3563 elf64_x86_64_merge_symbol (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3564 struct elf_link_hash_entry
**sym_hash ATTRIBUTE_UNUSED
,
3565 struct elf_link_hash_entry
*h
,
3566 Elf_Internal_Sym
*sym
,
3568 bfd_vma
*pvalue ATTRIBUTE_UNUSED
,
3569 unsigned int *pold_alignment ATTRIBUTE_UNUSED
,
3570 bfd_boolean
*skip ATTRIBUTE_UNUSED
,
3571 bfd_boolean
*override ATTRIBUTE_UNUSED
,
3572 bfd_boolean
*type_change_ok ATTRIBUTE_UNUSED
,
3573 bfd_boolean
*size_change_ok ATTRIBUTE_UNUSED
,
3574 bfd_boolean
*newdef ATTRIBUTE_UNUSED
,
3575 bfd_boolean
*newdyn
,
3576 bfd_boolean
*newdyncommon ATTRIBUTE_UNUSED
,
3577 bfd_boolean
*newweak ATTRIBUTE_UNUSED
,
3578 bfd
*abfd ATTRIBUTE_UNUSED
,
3580 bfd_boolean
*olddef ATTRIBUTE_UNUSED
,
3581 bfd_boolean
*olddyn
,
3582 bfd_boolean
*olddyncommon ATTRIBUTE_UNUSED
,
3583 bfd_boolean
*oldweak ATTRIBUTE_UNUSED
,
3587 /* A normal common symbol and a large common symbol result in a
3588 normal common symbol. We turn the large common symbol into a
3591 && h
->root
.type
== bfd_link_hash_common
3593 && bfd_is_com_section (*sec
)
3596 if (sym
->st_shndx
== SHN_COMMON
3597 && (elf_section_flags (*oldsec
) & SHF_X86_64_LARGE
) != 0)
3599 h
->root
.u
.c
.p
->section
3600 = bfd_make_section_old_way (oldbfd
, "COMMON");
3601 h
->root
.u
.c
.p
->section
->flags
= SEC_ALLOC
;
3603 else if (sym
->st_shndx
== SHN_X86_64_LCOMMON
3604 && (elf_section_flags (*oldsec
) & SHF_X86_64_LARGE
) == 0)
3605 *psec
= *sec
= bfd_com_section_ptr
;
3612 elf64_x86_64_additional_program_headers (bfd
*abfd
,
3613 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
3618 /* Check to see if we need a large readonly segment. */
3619 s
= bfd_get_section_by_name (abfd
, ".lrodata");
3620 if (s
&& (s
->flags
& SEC_LOAD
))
3623 /* Check to see if we need a large data segment. Since .lbss sections
3624 is placed right after the .bss section, there should be no need for
3625 a large data segment just because of .lbss. */
3626 s
= bfd_get_section_by_name (abfd
, ".ldata");
3627 if (s
&& (s
->flags
& SEC_LOAD
))
3633 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3636 elf64_x86_64_hash_symbol (struct elf_link_hash_entry
*h
)
3638 if (h
->plt
.offset
!= (bfd_vma
) -1
3640 && !h
->pointer_equality_needed
)
3643 return _bfd_elf_hash_symbol (h
);
3646 static const struct bfd_elf_special_section
3647 elf64_x86_64_special_sections
[]=
3649 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3650 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
3651 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
+ SHF_X86_64_LARGE
},
3652 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3653 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_X86_64_LARGE
},
3654 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_X86_64_LARGE
},
3655 { NULL
, 0, 0, 0, 0 }
3658 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3659 #define TARGET_LITTLE_NAME "elf64-x86-64"
3660 #define ELF_ARCH bfd_arch_i386
3661 #define ELF_MACHINE_CODE EM_X86_64
3662 #define ELF_MAXPAGESIZE 0x200000
3663 #define ELF_MINPAGESIZE 0x1000
3664 #define ELF_COMMONPAGESIZE 0x1000
3666 #define elf_backend_can_gc_sections 1
3667 #define elf_backend_can_refcount 1
3668 #define elf_backend_want_got_plt 1
3669 #define elf_backend_plt_readonly 1
3670 #define elf_backend_want_plt_sym 0
3671 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
3672 #define elf_backend_rela_normal 1
3674 #define elf_info_to_howto elf64_x86_64_info_to_howto
3676 #define bfd_elf64_bfd_link_hash_table_create \
3677 elf64_x86_64_link_hash_table_create
3678 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
3679 #define bfd_elf64_bfd_reloc_name_lookup \
3680 elf64_x86_64_reloc_name_lookup
3682 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3683 #define elf_backend_check_relocs elf64_x86_64_check_relocs
3684 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3685 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3686 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
3687 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3688 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3689 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3690 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3691 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
3692 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
3693 #define elf_backend_relocate_section elf64_x86_64_relocate_section
3694 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
3695 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
3696 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
3697 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
3698 #define elf_backend_object_p elf64_x86_64_elf_object_p
3699 #define bfd_elf64_mkobject elf64_x86_64_mkobject
3701 #define elf_backend_section_from_shdr \
3702 elf64_x86_64_section_from_shdr
3704 #define elf_backend_section_from_bfd_section \
3705 elf64_x86_64_elf_section_from_bfd_section
3706 #define elf_backend_add_symbol_hook \
3707 elf64_x86_64_add_symbol_hook
3708 #define elf_backend_symbol_processing \
3709 elf64_x86_64_symbol_processing
3710 #define elf_backend_common_section_index \
3711 elf64_x86_64_common_section_index
3712 #define elf_backend_common_section \
3713 elf64_x86_64_common_section
3714 #define elf_backend_common_definition \
3715 elf64_x86_64_common_definition
3716 #define elf_backend_merge_symbol \
3717 elf64_x86_64_merge_symbol
3718 #define elf_backend_special_sections \
3719 elf64_x86_64_special_sections
3720 #define elf_backend_additional_program_headers \
3721 elf64_x86_64_additional_program_headers
3722 #define elf_backend_hash_symbol \
3723 elf64_x86_64_hash_symbol
3725 #include "elf64-target.h"
3727 /* FreeBSD support. */
3729 #undef TARGET_LITTLE_SYM
3730 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
3731 #undef TARGET_LITTLE_NAME
3732 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
3735 #define ELF_OSABI ELFOSABI_FREEBSD
3737 #undef elf_backend_post_process_headers
3738 #define elf_backend_post_process_headers _bfd_elf_set_osabi
3741 #define elf64_bed elf64_x86_64_fbsd_bed
3743 #include "elf64-target.h"