Commit | Line | Data |
---|---|---|
8d88c4ca | 1 | /* X86-64 specific support for 64-bit ELF |
3eb128b2 AM |
2 | Copyright 2000, 2001, 2002, 2003, 2004, 2005 |
3 | Free Software Foundation, Inc. | |
8d88c4ca NC |
4 | Contributed by Jan Hubicka <jh@suse.cz>. |
5 | ||
ae9a127f | 6 | This file is part of BFD, the Binary File Descriptor library. |
8d88c4ca | 7 | |
ae9a127f NC |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
8d88c4ca | 12 | |
ae9a127f NC |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
8d88c4ca | 17 | |
ae9a127f NC |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
3e110533 | 20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
8d88c4ca NC |
21 | |
22 | #include "bfd.h" | |
23 | #include "sysdep.h" | |
c434dee6 | 24 | #include "bfdlink.h" |
8d88c4ca NC |
25 | #include "libbfd.h" |
26 | #include "elf-bfd.h" | |
27 | ||
28 | #include "elf/x86-64.h" | |
29 | ||
8d88c4ca NC |
30 | /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */ |
31 | #define MINUS_ONE (~ (bfd_vma) 0) | |
32 | ||
33 | /* The relocation "howto" table. Order of fields: | |
407443a3 AJ |
34 | type, size, bitsize, pc_relative, complain_on_overflow, |
35 | special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */ | |
70256ad8 AJ |
36 | static reloc_howto_type x86_64_elf_howto_table[] = |
37 | { | |
b34976b6 AM |
38 | HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont, |
39 | bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000, | |
40 | FALSE), | |
41 | HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
42 | bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE, | |
43 | FALSE), | |
44 | HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed, | |
45 | bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff, | |
46 | TRUE), | |
47 | HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed, | |
48 | bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff, | |
49 | FALSE), | |
50 | HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed, | |
51 | bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff, | |
52 | TRUE), | |
53 | HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
54 | bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff, | |
55 | FALSE), | |
56 | HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
57 | bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE, | |
58 | MINUS_ONE, FALSE), | |
59 | HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
60 | bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE, | |
61 | MINUS_ONE, FALSE), | |
62 | HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
63 | bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE, | |
64 | MINUS_ONE, FALSE), | |
65 | HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed, | |
66 | bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff, | |
67 | 0xffffffff, TRUE), | |
68 | HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned, | |
69 | bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff, | |
70 | FALSE), | |
71 | HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed, | |
72 | bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff, | |
73 | FALSE), | |
74 | HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, | |
75 | bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE), | |
76 | HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield, | |
77 | bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE), | |
78 | HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_signed, | |
79 | bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE), | |
80 | HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, | |
81 | bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE), | |
82 | HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
83 | bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE, | |
84 | MINUS_ONE, FALSE), | |
85 | HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
86 | bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE, | |
87 | MINUS_ONE, FALSE), | |
88 | HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
89 | bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE, | |
90 | MINUS_ONE, FALSE), | |
91 | HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed, | |
92 | bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff, | |
93 | 0xffffffff, TRUE), | |
94 | HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed, | |
95 | bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff, | |
96 | 0xffffffff, TRUE), | |
97 | HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
98 | bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff, | |
99 | 0xffffffff, FALSE), | |
100 | HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed, | |
101 | bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff, | |
102 | 0xffffffff, TRUE), | |
103 | HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed, | |
104 | bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff, | |
105 | 0xffffffff, FALSE), | |
d6ab8113 JB |
106 | HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield, |
107 | bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE, | |
108 | TRUE), | |
109 | HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield, | |
110 | bfd_elf_generic_reloc, "R_X86_64_GOTOFF64", | |
111 | FALSE, MINUS_ONE, MINUS_ONE, FALSE), | |
112 | HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed, | |
113 | bfd_elf_generic_reloc, "R_X86_64_GOTPC32", | |
114 | FALSE, 0xffffffff, 0xffffffff, TRUE), | |
fe4770f4 AJ |
115 | |
116 | /* GNU extension to record C++ vtable hierarchy. */ | |
b34976b6 AM |
117 | HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont, |
118 | NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE), | |
fe4770f4 AJ |
119 | |
120 | /* GNU extension to record C++ vtable member usage. */ | |
b34976b6 AM |
121 | HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont, |
122 | _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0, | |
123 | FALSE) | |
8d88c4ca NC |
124 | }; |
125 | ||
126 | /* Map BFD relocs to the x86_64 elf relocs. */ | |
70256ad8 AJ |
127 | struct elf_reloc_map |
128 | { | |
8d88c4ca NC |
129 | bfd_reloc_code_real_type bfd_reloc_val; |
130 | unsigned char elf_reloc_val; | |
131 | }; | |
132 | ||
dc810e39 | 133 | static const struct elf_reloc_map x86_64_reloc_map[] = |
8d88c4ca | 134 | { |
70256ad8 AJ |
135 | { BFD_RELOC_NONE, R_X86_64_NONE, }, |
136 | { BFD_RELOC_64, R_X86_64_64, }, | |
137 | { BFD_RELOC_32_PCREL, R_X86_64_PC32, }, | |
138 | { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,}, | |
139 | { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,}, | |
140 | { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, }, | |
141 | { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, }, | |
142 | { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, }, | |
143 | { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, }, | |
144 | { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, }, | |
145 | { BFD_RELOC_32, R_X86_64_32, }, | |
146 | { BFD_RELOC_X86_64_32S, R_X86_64_32S, }, | |
147 | { BFD_RELOC_16, R_X86_64_16, }, | |
148 | { BFD_RELOC_16_PCREL, R_X86_64_PC16, }, | |
149 | { BFD_RELOC_8, R_X86_64_8, }, | |
150 | { BFD_RELOC_8_PCREL, R_X86_64_PC8, }, | |
bffbf940 JJ |
151 | { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, }, |
152 | { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, }, | |
153 | { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, }, | |
154 | { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, }, | |
155 | { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, }, | |
156 | { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, }, | |
157 | { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, }, | |
158 | { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, }, | |
d6ab8113 JB |
159 | { BFD_RELOC_64_PCREL, R_X86_64_PC64, }, |
160 | { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, }, | |
161 | { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, }, | |
fe4770f4 AJ |
162 | { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, }, |
163 | { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, }, | |
8d88c4ca NC |
164 | }; |
165 | ||
8d88c4ca NC |
166 | |
167 | /* Given a BFD reloc type, return a HOWTO structure. */ | |
168 | static reloc_howto_type * | |
27482721 AJ |
169 | elf64_x86_64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
170 | bfd_reloc_code_real_type code) | |
8d88c4ca NC |
171 | { |
172 | unsigned int i; | |
27482721 | 173 | |
8d88c4ca NC |
174 | for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map); |
175 | i++) | |
176 | { | |
177 | if (x86_64_reloc_map[i].bfd_reloc_val == code) | |
ffaef159 | 178 | return &x86_64_elf_howto_table[i]; |
8d88c4ca NC |
179 | } |
180 | return 0; | |
181 | } | |
182 | ||
8d88c4ca | 183 | /* Given an x86_64 ELF reloc type, fill in an arelent structure. */ |
8da6118f | 184 | |
8d88c4ca | 185 | static void |
27482721 AJ |
186 | elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr, |
187 | Elf_Internal_Rela *dst) | |
8d88c4ca | 188 | { |
fe4770f4 | 189 | unsigned r_type, i; |
8d88c4ca NC |
190 | |
191 | r_type = ELF64_R_TYPE (dst->r_info); | |
d0fb9a8d JJ |
192 | if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT |
193 | || r_type >= (unsigned int) R_X86_64_max) | |
fe4770f4 | 194 | { |
d0fb9a8d JJ |
195 | if (r_type > (unsigned int) R_X86_64_GOTPC32) |
196 | { | |
197 | (*_bfd_error_handler) (_("%B: invalid relocation type %d"), | |
198 | abfd, (int) r_type); | |
199 | r_type = R_X86_64_NONE; | |
200 | } | |
fe4770f4 AJ |
201 | i = r_type; |
202 | } | |
203 | else | |
d0fb9a8d | 204 | i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_GOTPC32 - 1); |
fe4770f4 | 205 | cache_ptr->howto = &x86_64_elf_howto_table[i]; |
8d88c4ca NC |
206 | BFD_ASSERT (r_type == cache_ptr->howto->type); |
207 | } | |
70256ad8 | 208 | \f |
3bab7989 | 209 | /* Support for core dump NOTE sections. */ |
b34976b6 | 210 | static bfd_boolean |
27482721 | 211 | elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
3bab7989 ML |
212 | { |
213 | int offset; | |
eea6121a | 214 | size_t size; |
3bab7989 ML |
215 | |
216 | switch (note->descsz) | |
217 | { | |
218 | default: | |
b34976b6 | 219 | return FALSE; |
3bab7989 ML |
220 | |
221 | case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */ | |
222 | /* pr_cursig */ | |
cedb70c5 | 223 | elf_tdata (abfd)->core_signal |
3bab7989 ML |
224 | = bfd_get_16 (abfd, note->descdata + 12); |
225 | ||
226 | /* pr_pid */ | |
cedb70c5 | 227 | elf_tdata (abfd)->core_pid |
3bab7989 ML |
228 | = bfd_get_32 (abfd, note->descdata + 32); |
229 | ||
230 | /* pr_reg */ | |
231 | offset = 112; | |
eea6121a | 232 | size = 216; |
3bab7989 ML |
233 | |
234 | break; | |
235 | } | |
236 | ||
237 | /* Make a ".reg/999" section. */ | |
238 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
eea6121a | 239 | size, note->descpos + offset); |
3bab7989 ML |
240 | } |
241 | ||
b34976b6 | 242 | static bfd_boolean |
27482721 | 243 | elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
3bab7989 ML |
244 | { |
245 | switch (note->descsz) | |
246 | { | |
247 | default: | |
b34976b6 | 248 | return FALSE; |
3bab7989 ML |
249 | |
250 | case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */ | |
251 | elf_tdata (abfd)->core_program | |
252 | = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16); | |
253 | elf_tdata (abfd)->core_command | |
254 | = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80); | |
255 | } | |
256 | ||
257 | /* Note that for some reason, a spurious space is tacked | |
258 | onto the end of the args in some (at least one anyway) | |
259 | implementations, so strip it off if it exists. */ | |
260 | ||
261 | { | |
262 | char *command = elf_tdata (abfd)->core_command; | |
263 | int n = strlen (command); | |
264 | ||
265 | if (0 < n && command[n - 1] == ' ') | |
266 | command[n - 1] = '\0'; | |
267 | } | |
268 | ||
b34976b6 | 269 | return TRUE; |
3bab7989 ML |
270 | } |
271 | \f | |
407443a3 | 272 | /* Functions for the x86-64 ELF linker. */ |
70256ad8 | 273 | |
407443a3 | 274 | /* The name of the dynamic interpreter. This is put in the .interp |
70256ad8 AJ |
275 | section. */ |
276 | ||
407443a3 | 277 | #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1" |
70256ad8 | 278 | |
d40d037c AJ |
279 | /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid |
280 | copying dynamic variables from a shared lib into an app's dynbss | |
281 | section, and instead use a dynamic relocation to point into the | |
282 | shared lib. */ | |
283 | #define ELIMINATE_COPY_RELOCS 1 | |
284 | ||
70256ad8 AJ |
285 | /* The size in bytes of an entry in the global offset table. */ |
286 | ||
287 | #define GOT_ENTRY_SIZE 8 | |
8d88c4ca | 288 | |
70256ad8 | 289 | /* The size in bytes of an entry in the procedure linkage table. */ |
8d88c4ca | 290 | |
70256ad8 AJ |
291 | #define PLT_ENTRY_SIZE 16 |
292 | ||
293 | /* The first entry in a procedure linkage table looks like this. See the | |
294 | SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */ | |
295 | ||
296 | static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] = | |
297 | { | |
653165cc AJ |
298 | 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */ |
299 | 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */ | |
300 | 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */ | |
70256ad8 AJ |
301 | }; |
302 | ||
303 | /* Subsequent entries in a procedure linkage table look like this. */ | |
304 | ||
305 | static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] = | |
306 | { | |
653165cc | 307 | 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */ |
407443a3 | 308 | 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */ |
653165cc | 309 | 0x68, /* pushq immediate */ |
70256ad8 AJ |
310 | 0, 0, 0, 0, /* replaced with index into relocation table. */ |
311 | 0xe9, /* jmp relative */ | |
312 | 0, 0, 0, 0 /* replaced with offset to start of .plt0. */ | |
313 | }; | |
314 | ||
315 | /* The x86-64 linker needs to keep track of the number of relocs that | |
985142a4 | 316 | it decides to copy as dynamic relocs in check_relocs for each symbol. |
c434dee6 AJ |
317 | This is so that it can later discard them if they are found to be |
318 | unnecessary. We store the information in a field extending the | |
319 | regular ELF linker hash table. */ | |
70256ad8 | 320 | |
c434dee6 | 321 | struct elf64_x86_64_dyn_relocs |
70256ad8 AJ |
322 | { |
323 | /* Next section. */ | |
c434dee6 AJ |
324 | struct elf64_x86_64_dyn_relocs *next; |
325 | ||
326 | /* The input section of the reloc. */ | |
327 | asection *sec; | |
328 | ||
329 | /* Total number of relocs copied for the input section. */ | |
70256ad8 | 330 | bfd_size_type count; |
c434dee6 AJ |
331 | |
332 | /* Number of pc-relative relocs copied for the input section. */ | |
333 | bfd_size_type pc_count; | |
70256ad8 AJ |
334 | }; |
335 | ||
336 | /* x86-64 ELF linker hash entry. */ | |
337 | ||
338 | struct elf64_x86_64_link_hash_entry | |
339 | { | |
c434dee6 | 340 | struct elf_link_hash_entry elf; |
70256ad8 | 341 | |
c434dee6 AJ |
342 | /* Track dynamic relocs copied for this symbol. */ |
343 | struct elf64_x86_64_dyn_relocs *dyn_relocs; | |
bffbf940 JJ |
344 | |
345 | #define GOT_UNKNOWN 0 | |
346 | #define GOT_NORMAL 1 | |
347 | #define GOT_TLS_GD 2 | |
348 | #define GOT_TLS_IE 3 | |
349 | unsigned char tls_type; | |
350 | }; | |
351 | ||
352 | #define elf64_x86_64_hash_entry(ent) \ | |
353 | ((struct elf64_x86_64_link_hash_entry *)(ent)) | |
354 | ||
355 | struct elf64_x86_64_obj_tdata | |
356 | { | |
357 | struct elf_obj_tdata root; | |
358 | ||
359 | /* tls_type for each local got entry. */ | |
360 | char *local_got_tls_type; | |
70256ad8 AJ |
361 | }; |
362 | ||
bffbf940 JJ |
363 | #define elf64_x86_64_tdata(abfd) \ |
364 | ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any) | |
365 | ||
366 | #define elf64_x86_64_local_got_tls_type(abfd) \ | |
367 | (elf64_x86_64_tdata (abfd)->local_got_tls_type) | |
368 | ||
369 | ||
c434dee6 | 370 | /* x86-64 ELF linker hash table. */ |
8d88c4ca | 371 | |
407443a3 AJ |
372 | struct elf64_x86_64_link_hash_table |
373 | { | |
c434dee6 | 374 | struct elf_link_hash_table elf; |
70256ad8 | 375 | |
c434dee6 AJ |
376 | /* Short-cuts to get to dynamic linker sections. */ |
377 | asection *sgot; | |
378 | asection *sgotplt; | |
379 | asection *srelgot; | |
380 | asection *splt; | |
381 | asection *srelplt; | |
382 | asection *sdynbss; | |
383 | asection *srelbss; | |
70256ad8 | 384 | |
bffbf940 JJ |
385 | union { |
386 | bfd_signed_vma refcount; | |
387 | bfd_vma offset; | |
388 | } tls_ld_got; | |
389 | ||
c434dee6 AJ |
390 | /* Small local sym to section mapping cache. */ |
391 | struct sym_sec_cache sym_sec; | |
392 | }; | |
70256ad8 AJ |
393 | |
394 | /* Get the x86-64 ELF linker hash table from a link_info structure. */ | |
8d88c4ca NC |
395 | |
396 | #define elf64_x86_64_hash_table(p) \ | |
397 | ((struct elf64_x86_64_link_hash_table *) ((p)->hash)) | |
398 | ||
407443a3 | 399 | /* Create an entry in an x86-64 ELF linker hash table. */ |
70256ad8 AJ |
400 | |
401 | static struct bfd_hash_entry * | |
27482721 AJ |
402 | link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table, |
403 | const char *string) | |
70256ad8 | 404 | { |
70256ad8 | 405 | /* Allocate the structure if it has not already been allocated by a |
c434dee6 AJ |
406 | subclass. */ |
407 | if (entry == NULL) | |
408 | { | |
409 | entry = bfd_hash_allocate (table, | |
410 | sizeof (struct elf64_x86_64_link_hash_entry)); | |
411 | if (entry == NULL) | |
412 | return entry; | |
413 | } | |
70256ad8 AJ |
414 | |
415 | /* Call the allocation method of the superclass. */ | |
c434dee6 AJ |
416 | entry = _bfd_elf_link_hash_newfunc (entry, table, string); |
417 | if (entry != NULL) | |
70256ad8 | 418 | { |
c434dee6 AJ |
419 | struct elf64_x86_64_link_hash_entry *eh; |
420 | ||
421 | eh = (struct elf64_x86_64_link_hash_entry *) entry; | |
422 | eh->dyn_relocs = NULL; | |
bffbf940 | 423 | eh->tls_type = GOT_UNKNOWN; |
70256ad8 AJ |
424 | } |
425 | ||
c434dee6 | 426 | return entry; |
70256ad8 AJ |
427 | } |
428 | ||
8d88c4ca NC |
429 | /* Create an X86-64 ELF linker hash table. */ |
430 | ||
431 | static struct bfd_link_hash_table * | |
27482721 | 432 | elf64_x86_64_link_hash_table_create (bfd *abfd) |
8d88c4ca NC |
433 | { |
434 | struct elf64_x86_64_link_hash_table *ret; | |
dc810e39 | 435 | bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table); |
8d88c4ca | 436 | |
e2d34d7d | 437 | ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt); |
c434dee6 | 438 | if (ret == NULL) |
8d88c4ca NC |
439 | return NULL; |
440 | ||
c434dee6 | 441 | if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc)) |
8d88c4ca | 442 | { |
e2d34d7d | 443 | free (ret); |
8d88c4ca NC |
444 | return NULL; |
445 | } | |
446 | ||
c434dee6 AJ |
447 | ret->sgot = NULL; |
448 | ret->sgotplt = NULL; | |
449 | ret->srelgot = NULL; | |
450 | ret->splt = NULL; | |
451 | ret->srelplt = NULL; | |
452 | ret->sdynbss = NULL; | |
453 | ret->srelbss = NULL; | |
454 | ret->sym_sec.abfd = NULL; | |
bffbf940 | 455 | ret->tls_ld_got.refcount = 0; |
c434dee6 AJ |
456 | |
457 | return &ret->elf.root; | |
458 | } | |
459 | ||
460 | /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up | |
461 | shortcuts to them in our hash table. */ | |
462 | ||
b34976b6 | 463 | static bfd_boolean |
27482721 | 464 | create_got_section (bfd *dynobj, struct bfd_link_info *info) |
c434dee6 AJ |
465 | { |
466 | struct elf64_x86_64_link_hash_table *htab; | |
467 | ||
468 | if (! _bfd_elf_create_got_section (dynobj, info)) | |
b34976b6 | 469 | return FALSE; |
c434dee6 AJ |
470 | |
471 | htab = elf64_x86_64_hash_table (info); | |
472 | htab->sgot = bfd_get_section_by_name (dynobj, ".got"); | |
473 | htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); | |
474 | if (!htab->sgot || !htab->sgotplt) | |
475 | abort (); | |
476 | ||
3496cb2a L |
477 | htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got", |
478 | (SEC_ALLOC | SEC_LOAD | |
479 | | SEC_HAS_CONTENTS | |
480 | | SEC_IN_MEMORY | |
481 | | SEC_LINKER_CREATED | |
482 | | SEC_READONLY)); | |
c434dee6 | 483 | if (htab->srelgot == NULL |
c434dee6 | 484 | || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3)) |
b34976b6 AM |
485 | return FALSE; |
486 | return TRUE; | |
c434dee6 AJ |
487 | } |
488 | ||
489 | /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and | |
490 | .rela.bss sections in DYNOBJ, and set up shortcuts to them in our | |
491 | hash table. */ | |
492 | ||
b34976b6 | 493 | static bfd_boolean |
27482721 | 494 | elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info) |
c434dee6 AJ |
495 | { |
496 | struct elf64_x86_64_link_hash_table *htab; | |
497 | ||
498 | htab = elf64_x86_64_hash_table (info); | |
499 | if (!htab->sgot && !create_got_section (dynobj, info)) | |
b34976b6 | 500 | return FALSE; |
c434dee6 AJ |
501 | |
502 | if (!_bfd_elf_create_dynamic_sections (dynobj, info)) | |
b34976b6 | 503 | return FALSE; |
c434dee6 AJ |
504 | |
505 | htab->splt = bfd_get_section_by_name (dynobj, ".plt"); | |
506 | htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt"); | |
507 | htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss"); | |
508 | if (!info->shared) | |
509 | htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss"); | |
510 | ||
511 | if (!htab->splt || !htab->srelplt || !htab->sdynbss | |
512 | || (!info->shared && !htab->srelbss)) | |
513 | abort (); | |
514 | ||
b34976b6 | 515 | return TRUE; |
c434dee6 AJ |
516 | } |
517 | ||
518 | /* Copy the extra info we tack onto an elf_link_hash_entry. */ | |
519 | ||
520 | static void | |
9c5bfbb7 | 521 | elf64_x86_64_copy_indirect_symbol (const struct elf_backend_data *bed, |
27482721 AJ |
522 | struct elf_link_hash_entry *dir, |
523 | struct elf_link_hash_entry *ind) | |
c434dee6 AJ |
524 | { |
525 | struct elf64_x86_64_link_hash_entry *edir, *eind; | |
526 | ||
527 | edir = (struct elf64_x86_64_link_hash_entry *) dir; | |
528 | eind = (struct elf64_x86_64_link_hash_entry *) ind; | |
529 | ||
530 | if (eind->dyn_relocs != NULL) | |
531 | { | |
532 | if (edir->dyn_relocs != NULL) | |
533 | { | |
534 | struct elf64_x86_64_dyn_relocs **pp; | |
535 | struct elf64_x86_64_dyn_relocs *p; | |
536 | ||
537 | if (ind->root.type == bfd_link_hash_indirect) | |
538 | abort (); | |
539 | ||
540 | /* Add reloc counts against the weak sym to the strong sym | |
541 | list. Merge any entries against the same section. */ | |
542 | for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) | |
543 | { | |
544 | struct elf64_x86_64_dyn_relocs *q; | |
545 | ||
546 | for (q = edir->dyn_relocs; q != NULL; q = q->next) | |
547 | if (q->sec == p->sec) | |
548 | { | |
549 | q->pc_count += p->pc_count; | |
550 | q->count += p->count; | |
551 | *pp = p->next; | |
552 | break; | |
553 | } | |
554 | if (q == NULL) | |
555 | pp = &p->next; | |
556 | } | |
557 | *pp = edir->dyn_relocs; | |
558 | } | |
559 | ||
560 | edir->dyn_relocs = eind->dyn_relocs; | |
561 | eind->dyn_relocs = NULL; | |
562 | } | |
563 | ||
bffbf940 JJ |
564 | if (ind->root.type == bfd_link_hash_indirect |
565 | && dir->got.refcount <= 0) | |
566 | { | |
567 | edir->tls_type = eind->tls_type; | |
568 | eind->tls_type = GOT_UNKNOWN; | |
569 | } | |
570 | ||
d40d037c AJ |
571 | if (ELIMINATE_COPY_RELOCS |
572 | && ind->root.type != bfd_link_hash_indirect | |
f5385ebf AM |
573 | && dir->dynamic_adjusted) |
574 | { | |
575 | /* If called to transfer flags for a weakdef during processing | |
576 | of elf_adjust_dynamic_symbol, don't copy non_got_ref. | |
577 | We clear it ourselves for ELIMINATE_COPY_RELOCS. */ | |
578 | dir->ref_dynamic |= ind->ref_dynamic; | |
579 | dir->ref_regular |= ind->ref_regular; | |
580 | dir->ref_regular_nonweak |= ind->ref_regular_nonweak; | |
581 | dir->needs_plt |= ind->needs_plt; | |
582 | dir->pointer_equality_needed |= ind->pointer_equality_needed; | |
583 | } | |
d40d037c AJ |
584 | else |
585 | _bfd_elf_link_hash_copy_indirect (bed, dir, ind); | |
8d88c4ca NC |
586 | } |
587 | ||
b34976b6 | 588 | static bfd_boolean |
27482721 | 589 | elf64_x86_64_mkobject (bfd *abfd) |
8d88c4ca | 590 | { |
bffbf940 JJ |
591 | bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata); |
592 | abfd->tdata.any = bfd_zalloc (abfd, amt); | |
593 | if (abfd->tdata.any == NULL) | |
b34976b6 AM |
594 | return FALSE; |
595 | return TRUE; | |
bffbf940 JJ |
596 | } |
597 | ||
b34976b6 | 598 | static bfd_boolean |
27482721 | 599 | elf64_x86_64_elf_object_p (bfd *abfd) |
bffbf940 | 600 | { |
8d88c4ca NC |
601 | /* Set the right machine number for an x86-64 elf64 file. */ |
602 | bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64); | |
b34976b6 | 603 | return TRUE; |
8d88c4ca NC |
604 | } |
605 | ||
bffbf940 | 606 | static int |
27482721 | 607 | elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local) |
bffbf940 JJ |
608 | { |
609 | if (info->shared) | |
610 | return r_type; | |
611 | ||
612 | switch (r_type) | |
613 | { | |
614 | case R_X86_64_TLSGD: | |
615 | case R_X86_64_GOTTPOFF: | |
616 | if (is_local) | |
617 | return R_X86_64_TPOFF32; | |
618 | return R_X86_64_GOTTPOFF; | |
619 | case R_X86_64_TLSLD: | |
620 | return R_X86_64_TPOFF32; | |
621 | } | |
622 | ||
623 | return r_type; | |
624 | } | |
625 | ||
70256ad8 | 626 | /* Look through the relocs for a section during the first phase, and |
c434dee6 AJ |
627 | calculate needed space in the global offset table, procedure |
628 | linkage table, and dynamic reloc sections. */ | |
70256ad8 | 629 | |
b34976b6 | 630 | static bfd_boolean |
27482721 AJ |
631 | elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec, |
632 | const Elf_Internal_Rela *relocs) | |
70256ad8 | 633 | { |
c434dee6 | 634 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 AJ |
635 | Elf_Internal_Shdr *symtab_hdr; |
636 | struct elf_link_hash_entry **sym_hashes; | |
70256ad8 AJ |
637 | const Elf_Internal_Rela *rel; |
638 | const Elf_Internal_Rela *rel_end; | |
70256ad8 AJ |
639 | asection *sreloc; |
640 | ||
1049f94e | 641 | if (info->relocatable) |
b34976b6 | 642 | return TRUE; |
70256ad8 | 643 | |
c434dee6 | 644 | htab = elf64_x86_64_hash_table (info); |
70256ad8 AJ |
645 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
646 | sym_hashes = elf_sym_hashes (abfd); | |
70256ad8 | 647 | |
c434dee6 AJ |
648 | sreloc = NULL; |
649 | ||
70256ad8 AJ |
650 | rel_end = relocs + sec->reloc_count; |
651 | for (rel = relocs; rel < rel_end; rel++) | |
652 | { | |
bffbf940 | 653 | unsigned int r_type; |
70256ad8 AJ |
654 | unsigned long r_symndx; |
655 | struct elf_link_hash_entry *h; | |
656 | ||
657 | r_symndx = ELF64_R_SYM (rel->r_info); | |
bffbf940 | 658 | r_type = ELF64_R_TYPE (rel->r_info); |
c434dee6 AJ |
659 | |
660 | if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) | |
661 | { | |
d003868e AM |
662 | (*_bfd_error_handler) (_("%B: bad symbol index: %d"), |
663 | abfd, r_symndx); | |
b34976b6 | 664 | return FALSE; |
c434dee6 AJ |
665 | } |
666 | ||
70256ad8 AJ |
667 | if (r_symndx < symtab_hdr->sh_info) |
668 | h = NULL; | |
669 | else | |
71cb9464 L |
670 | { |
671 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
672 | while (h->root.type == bfd_link_hash_indirect | |
673 | || h->root.type == bfd_link_hash_warning) | |
674 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
675 | } | |
70256ad8 | 676 | |
bffbf940 JJ |
677 | r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL); |
678 | switch (r_type) | |
70256ad8 | 679 | { |
bffbf940 JJ |
680 | case R_X86_64_TLSLD: |
681 | htab->tls_ld_got.refcount += 1; | |
682 | goto create_got; | |
683 | ||
684 | case R_X86_64_TPOFF32: | |
685 | if (info->shared) | |
70256ad8 | 686 | { |
bffbf940 | 687 | (*_bfd_error_handler) |
d003868e AM |
688 | (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), |
689 | abfd, | |
6610a52d L |
690 | x86_64_elf_howto_table[r_type].name, |
691 | (h) ? h->root.root.string : "a local symbol"); | |
bffbf940 | 692 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 693 | return FALSE; |
70256ad8 | 694 | } |
bffbf940 | 695 | break; |
c434dee6 | 696 | |
bffbf940 JJ |
697 | case R_X86_64_GOTTPOFF: |
698 | if (info->shared) | |
699 | info->flags |= DF_STATIC_TLS; | |
700 | /* Fall through */ | |
70256ad8 | 701 | |
bffbf940 JJ |
702 | case R_X86_64_GOT32: |
703 | case R_X86_64_GOTPCREL: | |
704 | case R_X86_64_TLSGD: | |
705 | /* This symbol requires a global offset table entry. */ | |
706 | { | |
707 | int tls_type, old_tls_type; | |
708 | ||
709 | switch (r_type) | |
710 | { | |
711 | default: tls_type = GOT_NORMAL; break; | |
712 | case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break; | |
713 | case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break; | |
714 | } | |
715 | ||
716 | if (h != NULL) | |
717 | { | |
718 | h->got.refcount += 1; | |
719 | old_tls_type = elf64_x86_64_hash_entry (h)->tls_type; | |
720 | } | |
721 | else | |
722 | { | |
723 | bfd_signed_vma *local_got_refcounts; | |
724 | ||
725 | /* This is a global offset table entry for a local symbol. */ | |
726 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
727 | if (local_got_refcounts == NULL) | |
728 | { | |
729 | bfd_size_type size; | |
730 | ||
731 | size = symtab_hdr->sh_info; | |
732 | size *= sizeof (bfd_signed_vma) + sizeof (char); | |
733 | local_got_refcounts = ((bfd_signed_vma *) | |
734 | bfd_zalloc (abfd, size)); | |
735 | if (local_got_refcounts == NULL) | |
b34976b6 | 736 | return FALSE; |
bffbf940 JJ |
737 | elf_local_got_refcounts (abfd) = local_got_refcounts; |
738 | elf64_x86_64_local_got_tls_type (abfd) | |
739 | = (char *) (local_got_refcounts + symtab_hdr->sh_info); | |
740 | } | |
741 | local_got_refcounts[r_symndx] += 1; | |
742 | old_tls_type | |
743 | = elf64_x86_64_local_got_tls_type (abfd) [r_symndx]; | |
744 | } | |
745 | ||
746 | /* If a TLS symbol is accessed using IE at least once, | |
747 | there is no point to use dynamic model for it. */ | |
748 | if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN | |
749 | && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE)) | |
750 | { | |
751 | if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD) | |
752 | tls_type = old_tls_type; | |
753 | else | |
754 | { | |
755 | (*_bfd_error_handler) | |
d003868e AM |
756 | (_("%B: %s' accessed both as normal and thread local symbol"), |
757 | abfd, h ? h->root.root.string : "<local>"); | |
b34976b6 | 758 | return FALSE; |
bffbf940 JJ |
759 | } |
760 | } | |
761 | ||
762 | if (old_tls_type != tls_type) | |
763 | { | |
764 | if (h != NULL) | |
765 | elf64_x86_64_hash_entry (h)->tls_type = tls_type; | |
766 | else | |
767 | elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type; | |
768 | } | |
769 | } | |
c434dee6 AJ |
770 | /* Fall through */ |
771 | ||
d6ab8113 JB |
772 | case R_X86_64_GOTOFF64: |
773 | case R_X86_64_GOTPC32: | |
bffbf940 | 774 | create_got: |
c434dee6 AJ |
775 | if (htab->sgot == NULL) |
776 | { | |
777 | if (htab->elf.dynobj == NULL) | |
778 | htab->elf.dynobj = abfd; | |
779 | if (!create_got_section (htab->elf.dynobj, info)) | |
b34976b6 | 780 | return FALSE; |
c434dee6 | 781 | } |
70256ad8 AJ |
782 | break; |
783 | ||
784 | case R_X86_64_PLT32: | |
785 | /* This symbol requires a procedure linkage table entry. We | |
407443a3 AJ |
786 | actually build the entry in adjust_dynamic_symbol, |
787 | because this might be a case of linking PIC code which is | |
788 | never referenced by a dynamic object, in which case we | |
789 | don't need to generate a procedure linkage table entry | |
790 | after all. */ | |
70256ad8 AJ |
791 | |
792 | /* If this is a local symbol, we resolve it directly without | |
407443a3 | 793 | creating a procedure linkage table entry. */ |
70256ad8 AJ |
794 | if (h == NULL) |
795 | continue; | |
796 | ||
f5385ebf | 797 | h->needs_plt = 1; |
51b64d56 | 798 | h->plt.refcount += 1; |
70256ad8 AJ |
799 | break; |
800 | ||
cc78d0af AJ |
801 | case R_X86_64_8: |
802 | case R_X86_64_16: | |
70256ad8 AJ |
803 | case R_X86_64_32: |
804 | case R_X86_64_32S: | |
1b71fb54 AJ |
805 | /* Let's help debug shared library creation. These relocs |
806 | cannot be used in shared libs. Don't error out for | |
807 | sections we don't care about, such as debug sections or | |
808 | non-constant sections. */ | |
809 | if (info->shared | |
810 | && (sec->flags & SEC_ALLOC) != 0 | |
811 | && (sec->flags & SEC_READONLY) != 0) | |
812 | { | |
813 | (*_bfd_error_handler) | |
d003868e AM |
814 | (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), |
815 | abfd, | |
6610a52d L |
816 | x86_64_elf_howto_table[r_type].name, |
817 | (h) ? h->root.root.string : "a local symbol"); | |
1b71fb54 | 818 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 819 | return FALSE; |
1b71fb54 AJ |
820 | } |
821 | /* Fall through. */ | |
822 | ||
c434dee6 AJ |
823 | case R_X86_64_PC8: |
824 | case R_X86_64_PC16: | |
70256ad8 | 825 | case R_X86_64_PC32: |
d6ab8113 | 826 | case R_X86_64_PC64: |
1b71fb54 | 827 | case R_X86_64_64: |
c434dee6 AJ |
828 | if (h != NULL && !info->shared) |
829 | { | |
830 | /* If this reloc is in a read-only section, we might | |
831 | need a copy reloc. We can't check reliably at this | |
832 | stage whether the section is read-only, as input | |
833 | sections have not yet been mapped to output sections. | |
834 | Tentatively set the flag for now, and correct in | |
835 | adjust_dynamic_symbol. */ | |
f5385ebf | 836 | h->non_got_ref = 1; |
c434dee6 AJ |
837 | |
838 | /* We may need a .plt entry if the function this reloc | |
839 | refers to is in a shared lib. */ | |
840 | h->plt.refcount += 1; | |
d6ab8113 | 841 | if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64) |
f5385ebf | 842 | h->pointer_equality_needed = 1; |
c434dee6 | 843 | } |
70256ad8 AJ |
844 | |
845 | /* If we are creating a shared library, and this is a reloc | |
846 | against a global symbol, or a non PC relative reloc | |
847 | against a local symbol, then we need to copy the reloc | |
848 | into the shared library. However, if we are linking with | |
849 | -Bsymbolic, we do not need to copy a reloc against a | |
850 | global symbol which is defined in an object we are | |
407443a3 | 851 | including in the link (i.e., DEF_REGULAR is set). At |
70256ad8 AJ |
852 | this point we have not seen all the input files, so it is |
853 | possible that DEF_REGULAR is not set now but will be set | |
c434dee6 AJ |
854 | later (it is never cleared). In case of a weak definition, |
855 | DEF_REGULAR may be cleared later by a strong definition in | |
856 | a shared library. We account for that possibility below by | |
857 | storing information in the relocs_copied field of the hash | |
858 | table entry. A similar situation occurs when creating | |
859 | shared libraries and symbol visibility changes render the | |
860 | symbol local. | |
861 | ||
862 | If on the other hand, we are creating an executable, we | |
863 | may need to keep relocations for symbols satisfied by a | |
864 | dynamic library if we manage to avoid copy relocs for the | |
865 | symbol. */ | |
866 | if ((info->shared | |
867 | && (sec->flags & SEC_ALLOC) != 0 | |
bffbf940 JJ |
868 | && (((r_type != R_X86_64_PC8) |
869 | && (r_type != R_X86_64_PC16) | |
d6ab8113 JB |
870 | && (r_type != R_X86_64_PC32) |
871 | && (r_type != R_X86_64_PC64)) | |
c434dee6 AJ |
872 | || (h != NULL |
873 | && (! info->symbolic | |
874 | || h->root.type == bfd_link_hash_defweak | |
f5385ebf | 875 | || !h->def_regular)))) |
d40d037c AJ |
876 | || (ELIMINATE_COPY_RELOCS |
877 | && !info->shared | |
c434dee6 AJ |
878 | && (sec->flags & SEC_ALLOC) != 0 |
879 | && h != NULL | |
880 | && (h->root.type == bfd_link_hash_defweak | |
f5385ebf | 881 | || !h->def_regular))) |
70256ad8 | 882 | { |
c434dee6 AJ |
883 | struct elf64_x86_64_dyn_relocs *p; |
884 | struct elf64_x86_64_dyn_relocs **head; | |
885 | ||
886 | /* We must copy these reloc types into the output file. | |
887 | Create a reloc section in dynobj and make room for | |
888 | this reloc. */ | |
70256ad8 AJ |
889 | if (sreloc == NULL) |
890 | { | |
891 | const char *name; | |
c434dee6 | 892 | bfd *dynobj; |
70256ad8 AJ |
893 | |
894 | name = (bfd_elf_string_from_elf_section | |
895 | (abfd, | |
896 | elf_elfheader (abfd)->e_shstrndx, | |
897 | elf_section_data (sec)->rel_hdr.sh_name)); | |
898 | if (name == NULL) | |
b34976b6 | 899 | return FALSE; |
70256ad8 | 900 | |
c434dee6 AJ |
901 | if (strncmp (name, ".rela", 5) != 0 |
902 | || strcmp (bfd_get_section_name (abfd, sec), | |
903 | name + 5) != 0) | |
904 | { | |
905 | (*_bfd_error_handler) | |
d003868e AM |
906 | (_("%B: bad relocation section name `%s\'"), |
907 | abfd, name); | |
c434dee6 AJ |
908 | } |
909 | ||
910 | if (htab->elf.dynobj == NULL) | |
911 | htab->elf.dynobj = abfd; | |
912 | ||
913 | dynobj = htab->elf.dynobj; | |
70256ad8 AJ |
914 | |
915 | sreloc = bfd_get_section_by_name (dynobj, name); | |
916 | if (sreloc == NULL) | |
917 | { | |
918 | flagword flags; | |
919 | ||
70256ad8 AJ |
920 | flags = (SEC_HAS_CONTENTS | SEC_READONLY |
921 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
922 | if ((sec->flags & SEC_ALLOC) != 0) | |
923 | flags |= SEC_ALLOC | SEC_LOAD; | |
3496cb2a L |
924 | sreloc = bfd_make_section_with_flags (dynobj, |
925 | name, | |
926 | flags); | |
70256ad8 | 927 | if (sreloc == NULL |
cc78d0af | 928 | || ! bfd_set_section_alignment (dynobj, sreloc, 3)) |
b34976b6 | 929 | return FALSE; |
70256ad8 | 930 | } |
c434dee6 | 931 | elf_section_data (sec)->sreloc = sreloc; |
70256ad8 AJ |
932 | } |
933 | ||
c434dee6 AJ |
934 | /* If this is a global symbol, we count the number of |
935 | relocations we need for this symbol. */ | |
936 | if (h != NULL) | |
70256ad8 | 937 | { |
c434dee6 AJ |
938 | head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs; |
939 | } | |
940 | else | |
941 | { | |
942 | /* Track dynamic relocs needed for local syms too. | |
943 | We really need local syms available to do this | |
944 | easily. Oh well. */ | |
945 | ||
946 | asection *s; | |
947 | s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, | |
948 | sec, r_symndx); | |
949 | if (s == NULL) | |
b34976b6 | 950 | return FALSE; |
70256ad8 | 951 | |
c434dee6 AJ |
952 | head = ((struct elf64_x86_64_dyn_relocs **) |
953 | &elf_section_data (s)->local_dynrel); | |
954 | } | |
70256ad8 | 955 | |
c434dee6 AJ |
956 | p = *head; |
957 | if (p == NULL || p->sec != sec) | |
958 | { | |
959 | bfd_size_type amt = sizeof *p; | |
960 | p = ((struct elf64_x86_64_dyn_relocs *) | |
961 | bfd_alloc (htab->elf.dynobj, amt)); | |
70256ad8 | 962 | if (p == NULL) |
b34976b6 | 963 | return FALSE; |
c434dee6 AJ |
964 | p->next = *head; |
965 | *head = p; | |
966 | p->sec = sec; | |
967 | p->count = 0; | |
968 | p->pc_count = 0; | |
70256ad8 | 969 | } |
c434dee6 AJ |
970 | |
971 | p->count += 1; | |
bffbf940 JJ |
972 | if (r_type == R_X86_64_PC8 |
973 | || r_type == R_X86_64_PC16 | |
d6ab8113 JB |
974 | || r_type == R_X86_64_PC32 |
975 | || r_type == R_X86_64_PC64) | |
c434dee6 | 976 | p->pc_count += 1; |
70256ad8 AJ |
977 | } |
978 | break; | |
fe4770f4 AJ |
979 | |
980 | /* This relocation describes the C++ object vtable hierarchy. | |
981 | Reconstruct it for later use during GC. */ | |
982 | case R_X86_64_GNU_VTINHERIT: | |
c152c796 | 983 | if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) |
b34976b6 | 984 | return FALSE; |
fe4770f4 AJ |
985 | break; |
986 | ||
987 | /* This relocation describes which C++ vtable entries are actually | |
988 | used. Record for later use during GC. */ | |
989 | case R_X86_64_GNU_VTENTRY: | |
c152c796 | 990 | if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) |
b34976b6 | 991 | return FALSE; |
fe4770f4 | 992 | break; |
c434dee6 AJ |
993 | |
994 | default: | |
995 | break; | |
70256ad8 AJ |
996 | } |
997 | } | |
998 | ||
b34976b6 | 999 | return TRUE; |
70256ad8 AJ |
1000 | } |
1001 | ||
1002 | /* Return the section that should be marked against GC for a given | |
407443a3 | 1003 | relocation. */ |
70256ad8 AJ |
1004 | |
1005 | static asection * | |
27482721 AJ |
1006 | elf64_x86_64_gc_mark_hook (asection *sec, |
1007 | struct bfd_link_info *info ATTRIBUTE_UNUSED, | |
1008 | Elf_Internal_Rela *rel, | |
1009 | struct elf_link_hash_entry *h, | |
1010 | Elf_Internal_Sym *sym) | |
70256ad8 AJ |
1011 | { |
1012 | if (h != NULL) | |
1013 | { | |
fe4770f4 | 1014 | switch (ELF64_R_TYPE (rel->r_info)) |
70256ad8 | 1015 | { |
fe4770f4 AJ |
1016 | case R_X86_64_GNU_VTINHERIT: |
1017 | case R_X86_64_GNU_VTENTRY: | |
1018 | break; | |
70256ad8 AJ |
1019 | |
1020 | default: | |
fe4770f4 AJ |
1021 | switch (h->root.type) |
1022 | { | |
1023 | case bfd_link_hash_defined: | |
1024 | case bfd_link_hash_defweak: | |
1025 | return h->root.u.def.section; | |
1026 | ||
1027 | case bfd_link_hash_common: | |
1028 | return h->root.u.c.p->section; | |
1029 | ||
1030 | default: | |
1031 | break; | |
1032 | } | |
70256ad8 AJ |
1033 | } |
1034 | } | |
1035 | else | |
1e2f5b6e | 1036 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); |
70256ad8 AJ |
1037 | |
1038 | return NULL; | |
1039 | } | |
1040 | ||
407443a3 | 1041 | /* Update the got entry reference counts for the section being removed. */ |
70256ad8 | 1042 | |
b34976b6 | 1043 | static bfd_boolean |
27482721 AJ |
1044 | elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, |
1045 | asection *sec, const Elf_Internal_Rela *relocs) | |
70256ad8 AJ |
1046 | { |
1047 | Elf_Internal_Shdr *symtab_hdr; | |
1048 | struct elf_link_hash_entry **sym_hashes; | |
1049 | bfd_signed_vma *local_got_refcounts; | |
1050 | const Elf_Internal_Rela *rel, *relend; | |
c434dee6 AJ |
1051 | |
1052 | elf_section_data (sec)->local_dynrel = NULL; | |
70256ad8 AJ |
1053 | |
1054 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1055 | sym_hashes = elf_sym_hashes (abfd); | |
1056 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
1057 | ||
70256ad8 AJ |
1058 | relend = relocs + sec->reloc_count; |
1059 | for (rel = relocs; rel < relend; rel++) | |
26e41594 AM |
1060 | { |
1061 | unsigned long r_symndx; | |
1062 | unsigned int r_type; | |
1063 | struct elf_link_hash_entry *h = NULL; | |
70256ad8 | 1064 | |
26e41594 AM |
1065 | r_symndx = ELF64_R_SYM (rel->r_info); |
1066 | if (r_symndx >= symtab_hdr->sh_info) | |
1067 | { | |
1068 | struct elf64_x86_64_link_hash_entry *eh; | |
1069 | struct elf64_x86_64_dyn_relocs **pp; | |
1070 | struct elf64_x86_64_dyn_relocs *p; | |
c434dee6 | 1071 | |
26e41594 | 1072 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
3eb128b2 AM |
1073 | while (h->root.type == bfd_link_hash_indirect |
1074 | || h->root.type == bfd_link_hash_warning) | |
1075 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
26e41594 | 1076 | eh = (struct elf64_x86_64_link_hash_entry *) h; |
c434dee6 | 1077 | |
26e41594 AM |
1078 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next) |
1079 | if (p->sec == sec) | |
1080 | { | |
1081 | /* Everything must go for SEC. */ | |
1082 | *pp = p->next; | |
1083 | break; | |
1084 | } | |
1085 | } | |
c434dee6 | 1086 | |
26e41594 AM |
1087 | r_type = ELF64_R_TYPE (rel->r_info); |
1088 | r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL); | |
1089 | switch (r_type) | |
1090 | { | |
1091 | case R_X86_64_TLSLD: | |
1092 | if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0) | |
1093 | elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1; | |
1094 | break; | |
c434dee6 | 1095 | |
26e41594 AM |
1096 | case R_X86_64_TLSGD: |
1097 | case R_X86_64_GOTTPOFF: | |
1098 | case R_X86_64_GOT32: | |
1099 | case R_X86_64_GOTPCREL: | |
1100 | if (h != NULL) | |
1101 | { | |
1102 | if (h->got.refcount > 0) | |
1103 | h->got.refcount -= 1; | |
1104 | } | |
1105 | else if (local_got_refcounts != NULL) | |
1106 | { | |
1107 | if (local_got_refcounts[r_symndx] > 0) | |
1108 | local_got_refcounts[r_symndx] -= 1; | |
1109 | } | |
1110 | break; | |
c434dee6 | 1111 | |
26e41594 AM |
1112 | case R_X86_64_8: |
1113 | case R_X86_64_16: | |
1114 | case R_X86_64_32: | |
1115 | case R_X86_64_64: | |
1116 | case R_X86_64_32S: | |
1117 | case R_X86_64_PC8: | |
1118 | case R_X86_64_PC16: | |
1119 | case R_X86_64_PC32: | |
d6ab8113 | 1120 | case R_X86_64_PC64: |
26e41594 AM |
1121 | if (info->shared) |
1122 | break; | |
1123 | /* Fall thru */ | |
c434dee6 | 1124 | |
26e41594 AM |
1125 | case R_X86_64_PLT32: |
1126 | if (h != NULL) | |
1127 | { | |
1128 | if (h->plt.refcount > 0) | |
1129 | h->plt.refcount -= 1; | |
1130 | } | |
1131 | break; | |
70256ad8 | 1132 | |
26e41594 AM |
1133 | default: |
1134 | break; | |
1135 | } | |
1136 | } | |
70256ad8 | 1137 | |
b34976b6 | 1138 | return TRUE; |
70256ad8 AJ |
1139 | } |
1140 | ||
1141 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
1142 | regular object. The current definition is in some section of the | |
1143 | dynamic object, but we're not including those sections. We have to | |
1144 | change the definition to something the rest of the link can | |
407443a3 | 1145 | understand. */ |
70256ad8 | 1146 | |
b34976b6 | 1147 | static bfd_boolean |
27482721 AJ |
1148 | elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info, |
1149 | struct elf_link_hash_entry *h) | |
70256ad8 | 1150 | { |
c434dee6 | 1151 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 AJ |
1152 | asection *s; |
1153 | unsigned int power_of_two; | |
1154 | ||
70256ad8 AJ |
1155 | /* If this is a function, put it in the procedure linkage table. We |
1156 | will fill in the contents of the procedure linkage table later, | |
1157 | when we know the address of the .got section. */ | |
1158 | if (h->type == STT_FUNC | |
f5385ebf | 1159 | || h->needs_plt) |
70256ad8 | 1160 | { |
c434dee6 | 1161 | if (h->plt.refcount <= 0 |
27482721 AJ |
1162 | || SYMBOL_CALLS_LOCAL (info, h) |
1163 | || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
1164 | && h->root.type == bfd_link_hash_undefweak)) | |
70256ad8 | 1165 | { |
70256ad8 AJ |
1166 | /* This case can occur if we saw a PLT32 reloc in an input |
1167 | file, but the symbol was never referred to by a dynamic | |
1168 | object, or if all references were garbage collected. In | |
1169 | such a case, we don't actually need to build a procedure | |
1170 | linkage table, and we can just do a PC32 reloc instead. */ | |
70256ad8 | 1171 | h->plt.offset = (bfd_vma) -1; |
f5385ebf | 1172 | h->needs_plt = 0; |
70256ad8 AJ |
1173 | } |
1174 | ||
b34976b6 | 1175 | return TRUE; |
70256ad8 | 1176 | } |
bbd7ec4a | 1177 | else |
c434dee6 AJ |
1178 | /* It's possible that we incorrectly decided a .plt reloc was |
1179 | needed for an R_X86_64_PC32 reloc to a non-function sym in | |
1180 | check_relocs. We can't decide accurately between function and | |
1181 | non-function syms in check-relocs; Objects loaded later in | |
1182 | the link may change h->type. So fix it now. */ | |
bbd7ec4a | 1183 | h->plt.offset = (bfd_vma) -1; |
70256ad8 AJ |
1184 | |
1185 | /* If this is a weak symbol, and there is a real definition, the | |
1186 | processor independent code will have arranged for us to see the | |
407443a3 | 1187 | real definition first, and we can just use the same value. */ |
f6e332e6 | 1188 | if (h->u.weakdef != NULL) |
70256ad8 | 1189 | { |
f6e332e6 AM |
1190 | BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined |
1191 | || h->u.weakdef->root.type == bfd_link_hash_defweak); | |
1192 | h->root.u.def.section = h->u.weakdef->root.u.def.section; | |
1193 | h->root.u.def.value = h->u.weakdef->root.u.def.value; | |
d40d037c | 1194 | if (ELIMINATE_COPY_RELOCS || info->nocopyreloc) |
f6e332e6 | 1195 | h->non_got_ref = h->u.weakdef->non_got_ref; |
b34976b6 | 1196 | return TRUE; |
70256ad8 AJ |
1197 | } |
1198 | ||
1199 | /* This is a reference to a symbol defined by a dynamic object which | |
407443a3 | 1200 | is not a function. */ |
70256ad8 AJ |
1201 | |
1202 | /* If we are creating a shared library, we must presume that the | |
1203 | only references to the symbol are via the global offset table. | |
1204 | For such cases we need not do anything here; the relocations will | |
407443a3 | 1205 | be handled correctly by relocate_section. */ |
70256ad8 | 1206 | if (info->shared) |
b34976b6 | 1207 | return TRUE; |
70256ad8 AJ |
1208 | |
1209 | /* If there are no references to this symbol that do not use the | |
1210 | GOT, we don't need to generate a copy reloc. */ | |
f5385ebf | 1211 | if (!h->non_got_ref) |
b34976b6 | 1212 | return TRUE; |
70256ad8 | 1213 | |
c434dee6 AJ |
1214 | /* If -z nocopyreloc was given, we won't generate them either. */ |
1215 | if (info->nocopyreloc) | |
1216 | { | |
f5385ebf | 1217 | h->non_got_ref = 0; |
b34976b6 | 1218 | return TRUE; |
c434dee6 AJ |
1219 | } |
1220 | ||
d40d037c | 1221 | if (ELIMINATE_COPY_RELOCS) |
c434dee6 | 1222 | { |
d40d037c AJ |
1223 | struct elf64_x86_64_link_hash_entry * eh; |
1224 | struct elf64_x86_64_dyn_relocs *p; | |
c434dee6 | 1225 | |
d40d037c AJ |
1226 | eh = (struct elf64_x86_64_link_hash_entry *) h; |
1227 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1228 | { | |
1229 | s = p->sec->output_section; | |
1230 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
1231 | break; | |
1232 | } | |
1233 | ||
1234 | /* If we didn't find any dynamic relocs in read-only sections, then | |
1235 | we'll be keeping the dynamic relocs and avoiding the copy reloc. */ | |
1236 | if (p == NULL) | |
1237 | { | |
f5385ebf | 1238 | h->non_got_ref = 0; |
d40d037c AJ |
1239 | return TRUE; |
1240 | } | |
c434dee6 AJ |
1241 | } |
1242 | ||
70256ad8 | 1243 | /* We must allocate the symbol in our .dynbss section, which will |
407443a3 | 1244 | become part of the .bss section of the executable. There will be |
70256ad8 AJ |
1245 | an entry for this symbol in the .dynsym section. The dynamic |
1246 | object will contain position independent code, so all references | |
1247 | from the dynamic object to this symbol will go through the global | |
1248 | offset table. The dynamic linker will use the .dynsym entry to | |
1249 | determine the address it must put in the global offset table, so | |
1250 | both the dynamic object and the regular object will refer to the | |
1251 | same memory location for the variable. */ | |
1252 | ||
c434dee6 | 1253 | htab = elf64_x86_64_hash_table (info); |
70256ad8 AJ |
1254 | |
1255 | /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker | |
1256 | to copy the initial value out of the dynamic object and into the | |
cedb70c5 | 1257 | runtime process image. */ |
70256ad8 AJ |
1258 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) |
1259 | { | |
eea6121a | 1260 | htab->srelbss->size += sizeof (Elf64_External_Rela); |
f5385ebf | 1261 | h->needs_copy = 1; |
70256ad8 AJ |
1262 | } |
1263 | ||
1264 | /* We need to figure out the alignment required for this symbol. I | |
407443a3 | 1265 | have no idea how ELF linkers handle this. 16-bytes is the size |
70256ad8 AJ |
1266 | of the largest type that requires hard alignment -- long double. */ |
1267 | /* FIXME: This is VERY ugly. Should be fixed for all architectures using | |
1268 | this construct. */ | |
1269 | power_of_two = bfd_log2 (h->size); | |
1270 | if (power_of_two > 4) | |
1271 | power_of_two = 4; | |
1272 | ||
1273 | /* Apply the required alignment. */ | |
c434dee6 | 1274 | s = htab->sdynbss; |
eea6121a | 1275 | s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two)); |
c434dee6 | 1276 | if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s)) |
70256ad8 | 1277 | { |
c434dee6 | 1278 | if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two)) |
b34976b6 | 1279 | return FALSE; |
70256ad8 AJ |
1280 | } |
1281 | ||
1282 | /* Define the symbol as being at this point in the section. */ | |
1283 | h->root.u.def.section = s; | |
eea6121a | 1284 | h->root.u.def.value = s->size; |
70256ad8 AJ |
1285 | |
1286 | /* Increment the section size to make room for the symbol. */ | |
eea6121a | 1287 | s->size += h->size; |
70256ad8 | 1288 | |
b34976b6 | 1289 | return TRUE; |
70256ad8 AJ |
1290 | } |
1291 | ||
c434dee6 AJ |
1292 | /* Allocate space in .plt, .got and associated reloc sections for |
1293 | dynamic relocs. */ | |
1294 | ||
b34976b6 | 1295 | static bfd_boolean |
27482721 | 1296 | allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf) |
c434dee6 AJ |
1297 | { |
1298 | struct bfd_link_info *info; | |
1299 | struct elf64_x86_64_link_hash_table *htab; | |
1300 | struct elf64_x86_64_link_hash_entry *eh; | |
1301 | struct elf64_x86_64_dyn_relocs *p; | |
1302 | ||
e92d460e | 1303 | if (h->root.type == bfd_link_hash_indirect) |
b34976b6 | 1304 | return TRUE; |
c434dee6 | 1305 | |
e92d460e AM |
1306 | if (h->root.type == bfd_link_hash_warning) |
1307 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1308 | ||
c434dee6 AJ |
1309 | info = (struct bfd_link_info *) inf; |
1310 | htab = elf64_x86_64_hash_table (info); | |
1311 | ||
1312 | if (htab->elf.dynamic_sections_created | |
27482721 | 1313 | && h->plt.refcount > 0) |
c434dee6 AJ |
1314 | { |
1315 | /* Make sure this symbol is output as a dynamic symbol. | |
1316 | Undefined weak syms won't yet be marked as dynamic. */ | |
1317 | if (h->dynindx == -1 | |
f5385ebf | 1318 | && !h->forced_local) |
c434dee6 | 1319 | { |
c152c796 | 1320 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 1321 | return FALSE; |
c434dee6 AJ |
1322 | } |
1323 | ||
27482721 AJ |
1324 | if (info->shared |
1325 | || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h)) | |
c434dee6 AJ |
1326 | { |
1327 | asection *s = htab->splt; | |
1328 | ||
1329 | /* If this is the first .plt entry, make room for the special | |
1330 | first entry. */ | |
eea6121a AM |
1331 | if (s->size == 0) |
1332 | s->size += PLT_ENTRY_SIZE; | |
c434dee6 | 1333 | |
eea6121a | 1334 | h->plt.offset = s->size; |
c434dee6 AJ |
1335 | |
1336 | /* If this symbol is not defined in a regular file, and we are | |
1337 | not generating a shared library, then set the symbol to this | |
1338 | location in the .plt. This is required to make function | |
1339 | pointers compare as equal between the normal executable and | |
1340 | the shared library. */ | |
1341 | if (! info->shared | |
f5385ebf | 1342 | && !h->def_regular) |
c434dee6 AJ |
1343 | { |
1344 | h->root.u.def.section = s; | |
1345 | h->root.u.def.value = h->plt.offset; | |
1346 | } | |
1347 | ||
1348 | /* Make room for this entry. */ | |
eea6121a | 1349 | s->size += PLT_ENTRY_SIZE; |
c434dee6 AJ |
1350 | |
1351 | /* We also need to make an entry in the .got.plt section, which | |
1352 | will be placed in the .got section by the linker script. */ | |
eea6121a | 1353 | htab->sgotplt->size += GOT_ENTRY_SIZE; |
c434dee6 AJ |
1354 | |
1355 | /* We also need to make an entry in the .rela.plt section. */ | |
eea6121a | 1356 | htab->srelplt->size += sizeof (Elf64_External_Rela); |
c434dee6 AJ |
1357 | } |
1358 | else | |
1359 | { | |
1360 | h->plt.offset = (bfd_vma) -1; | |
f5385ebf | 1361 | h->needs_plt = 0; |
c434dee6 AJ |
1362 | } |
1363 | } | |
1364 | else | |
1365 | { | |
1366 | h->plt.offset = (bfd_vma) -1; | |
f5385ebf | 1367 | h->needs_plt = 0; |
c434dee6 AJ |
1368 | } |
1369 | ||
bffbf940 JJ |
1370 | /* If R_X86_64_GOTTPOFF symbol is now local to the binary, |
1371 | make it a R_X86_64_TPOFF32 requiring no GOT entry. */ | |
1372 | if (h->got.refcount > 0 | |
1373 | && !info->shared | |
1374 | && h->dynindx == -1 | |
1375 | && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE) | |
1376 | h->got.offset = (bfd_vma) -1; | |
1377 | else if (h->got.refcount > 0) | |
c434dee6 AJ |
1378 | { |
1379 | asection *s; | |
b34976b6 | 1380 | bfd_boolean dyn; |
bffbf940 | 1381 | int tls_type = elf64_x86_64_hash_entry (h)->tls_type; |
c434dee6 AJ |
1382 | |
1383 | /* Make sure this symbol is output as a dynamic symbol. | |
1384 | Undefined weak syms won't yet be marked as dynamic. */ | |
1385 | if (h->dynindx == -1 | |
f5385ebf | 1386 | && !h->forced_local) |
c434dee6 | 1387 | { |
c152c796 | 1388 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 1389 | return FALSE; |
c434dee6 AJ |
1390 | } |
1391 | ||
1392 | s = htab->sgot; | |
eea6121a AM |
1393 | h->got.offset = s->size; |
1394 | s->size += GOT_ENTRY_SIZE; | |
bffbf940 JJ |
1395 | /* R_X86_64_TLSGD needs 2 consecutive GOT slots. */ |
1396 | if (tls_type == GOT_TLS_GD) | |
eea6121a | 1397 | s->size += GOT_ENTRY_SIZE; |
c434dee6 | 1398 | dyn = htab->elf.dynamic_sections_created; |
bffbf940 JJ |
1399 | /* R_X86_64_TLSGD needs one dynamic relocation if local symbol |
1400 | and two if global. | |
1401 | R_X86_64_GOTTPOFF needs one dynamic relocation. */ | |
1402 | if ((tls_type == GOT_TLS_GD && h->dynindx == -1) | |
1403 | || tls_type == GOT_TLS_IE) | |
eea6121a | 1404 | htab->srelgot->size += sizeof (Elf64_External_Rela); |
bffbf940 | 1405 | else if (tls_type == GOT_TLS_GD) |
eea6121a | 1406 | htab->srelgot->size += 2 * sizeof (Elf64_External_Rela); |
4bc6e03a AJ |
1407 | else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
1408 | || h->root.type != bfd_link_hash_undefweak) | |
27482721 AJ |
1409 | && (info->shared |
1410 | || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))) | |
eea6121a | 1411 | htab->srelgot->size += sizeof (Elf64_External_Rela); |
c434dee6 AJ |
1412 | } |
1413 | else | |
1414 | h->got.offset = (bfd_vma) -1; | |
1415 | ||
1416 | eh = (struct elf64_x86_64_link_hash_entry *) h; | |
1417 | if (eh->dyn_relocs == NULL) | |
b34976b6 | 1418 | return TRUE; |
c434dee6 AJ |
1419 | |
1420 | /* In the shared -Bsymbolic case, discard space allocated for | |
1421 | dynamic pc-relative relocs against symbols which turn out to be | |
1422 | defined in regular objects. For the normal shared case, discard | |
1423 | space for pc-relative relocs that have become local due to symbol | |
1424 | visibility changes. */ | |
1425 | ||
1426 | if (info->shared) | |
1427 | { | |
27482721 AJ |
1428 | /* Relocs that use pc_count are those that appear on a call |
1429 | insn, or certain REL relocs that can generated via assembly. | |
1430 | We want calls to protected symbols to resolve directly to the | |
1431 | function rather than going via the plt. If people want | |
1432 | function pointer comparisons to work as expected then they | |
1433 | should avoid writing weird assembly. */ | |
1434 | if (SYMBOL_CALLS_LOCAL (info, h)) | |
c434dee6 AJ |
1435 | { |
1436 | struct elf64_x86_64_dyn_relocs **pp; | |
1437 | ||
1438 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) | |
1439 | { | |
1440 | p->count -= p->pc_count; | |
1441 | p->pc_count = 0; | |
1442 | if (p->count == 0) | |
1443 | *pp = p->next; | |
1444 | else | |
1445 | pp = &p->next; | |
1446 | } | |
1447 | } | |
4e795f50 AM |
1448 | |
1449 | /* Also discard relocs on undefined weak syms with non-default | |
1450 | visibility. */ | |
1451 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
1452 | && h->root.type == bfd_link_hash_undefweak) | |
1453 | eh->dyn_relocs = NULL; | |
c434dee6 | 1454 | } |
d40d037c | 1455 | else if (ELIMINATE_COPY_RELOCS) |
c434dee6 AJ |
1456 | { |
1457 | /* For the non-shared case, discard space for relocs against | |
1458 | symbols which turn out to need copy relocs or are not | |
1459 | dynamic. */ | |
1460 | ||
f5385ebf AM |
1461 | if (!h->non_got_ref |
1462 | && ((h->def_dynamic | |
1463 | && !h->def_regular) | |
c434dee6 AJ |
1464 | || (htab->elf.dynamic_sections_created |
1465 | && (h->root.type == bfd_link_hash_undefweak | |
1466 | || h->root.type == bfd_link_hash_undefined)))) | |
1467 | { | |
1468 | /* Make sure this symbol is output as a dynamic symbol. | |
1469 | Undefined weak syms won't yet be marked as dynamic. */ | |
1470 | if (h->dynindx == -1 | |
f5385ebf | 1471 | && !h->forced_local) |
c434dee6 | 1472 | { |
c152c796 | 1473 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 1474 | return FALSE; |
c434dee6 AJ |
1475 | } |
1476 | ||
1477 | /* If that succeeded, we know we'll be keeping all the | |
1478 | relocs. */ | |
1479 | if (h->dynindx != -1) | |
1480 | goto keep; | |
1481 | } | |
1482 | ||
1483 | eh->dyn_relocs = NULL; | |
1484 | ||
1485 | keep: ; | |
1486 | } | |
1487 | ||
1488 | /* Finally, allocate space. */ | |
1489 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1490 | { | |
1491 | asection *sreloc = elf_section_data (p->sec)->sreloc; | |
eea6121a | 1492 | sreloc->size += p->count * sizeof (Elf64_External_Rela); |
c434dee6 AJ |
1493 | } |
1494 | ||
b34976b6 | 1495 | return TRUE; |
c434dee6 AJ |
1496 | } |
1497 | ||
1498 | /* Find any dynamic relocs that apply to read-only sections. */ | |
1499 | ||
b34976b6 | 1500 | static bfd_boolean |
27482721 | 1501 | readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf) |
c434dee6 AJ |
1502 | { |
1503 | struct elf64_x86_64_link_hash_entry *eh; | |
1504 | struct elf64_x86_64_dyn_relocs *p; | |
1505 | ||
e92d460e AM |
1506 | if (h->root.type == bfd_link_hash_warning) |
1507 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1508 | ||
c434dee6 AJ |
1509 | eh = (struct elf64_x86_64_link_hash_entry *) h; |
1510 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
1511 | { | |
1512 | asection *s = p->sec->output_section; | |
1513 | ||
1514 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
1515 | { | |
1516 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
1517 | ||
1518 | info->flags |= DF_TEXTREL; | |
1519 | ||
1520 | /* Not an error, just cut short the traversal. */ | |
b34976b6 | 1521 | return FALSE; |
c434dee6 AJ |
1522 | } |
1523 | } | |
b34976b6 | 1524 | return TRUE; |
c434dee6 AJ |
1525 | } |
1526 | ||
70256ad8 AJ |
1527 | /* Set the sizes of the dynamic sections. */ |
1528 | ||
b34976b6 | 1529 | static bfd_boolean |
27482721 AJ |
1530 | elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED, |
1531 | struct bfd_link_info *info) | |
70256ad8 | 1532 | { |
c434dee6 | 1533 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 AJ |
1534 | bfd *dynobj; |
1535 | asection *s; | |
b34976b6 | 1536 | bfd_boolean relocs; |
c434dee6 | 1537 | bfd *ibfd; |
70256ad8 | 1538 | |
c434dee6 AJ |
1539 | htab = elf64_x86_64_hash_table (info); |
1540 | dynobj = htab->elf.dynobj; | |
1541 | if (dynobj == NULL) | |
1542 | abort (); | |
70256ad8 | 1543 | |
c434dee6 | 1544 | if (htab->elf.dynamic_sections_created) |
70256ad8 AJ |
1545 | { |
1546 | /* Set the contents of the .interp section to the interpreter. */ | |
36af4a4e | 1547 | if (info->executable) |
70256ad8 AJ |
1548 | { |
1549 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
c434dee6 AJ |
1550 | if (s == NULL) |
1551 | abort (); | |
eea6121a | 1552 | s->size = sizeof ELF_DYNAMIC_INTERPRETER; |
70256ad8 AJ |
1553 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; |
1554 | } | |
1555 | } | |
70256ad8 | 1556 | |
c434dee6 AJ |
1557 | /* Set up .got offsets for local syms, and space for local dynamic |
1558 | relocs. */ | |
1559 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) | |
70256ad8 | 1560 | { |
c434dee6 AJ |
1561 | bfd_signed_vma *local_got; |
1562 | bfd_signed_vma *end_local_got; | |
bffbf940 | 1563 | char *local_tls_type; |
c434dee6 AJ |
1564 | bfd_size_type locsymcount; |
1565 | Elf_Internal_Shdr *symtab_hdr; | |
1566 | asection *srel; | |
70256ad8 | 1567 | |
c434dee6 | 1568 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
70256ad8 AJ |
1569 | continue; |
1570 | ||
c434dee6 | 1571 | for (s = ibfd->sections; s != NULL; s = s->next) |
70256ad8 | 1572 | { |
c434dee6 AJ |
1573 | struct elf64_x86_64_dyn_relocs *p; |
1574 | ||
1575 | for (p = *((struct elf64_x86_64_dyn_relocs **) | |
1576 | &elf_section_data (s)->local_dynrel); | |
1577 | p != NULL; | |
1578 | p = p->next) | |
70256ad8 | 1579 | { |
c434dee6 AJ |
1580 | if (!bfd_is_abs_section (p->sec) |
1581 | && bfd_is_abs_section (p->sec->output_section)) | |
1582 | { | |
1583 | /* Input section has been discarded, either because | |
1584 | it is a copy of a linkonce section or due to | |
1585 | linker script /DISCARD/, so we'll be discarding | |
1586 | the relocs too. */ | |
1587 | } | |
1588 | else if (p->count != 0) | |
1589 | { | |
1590 | srel = elf_section_data (p->sec)->sreloc; | |
eea6121a | 1591 | srel->size += p->count * sizeof (Elf64_External_Rela); |
c434dee6 AJ |
1592 | if ((p->sec->output_section->flags & SEC_READONLY) != 0) |
1593 | info->flags |= DF_TEXTREL; | |
1594 | ||
1595 | } | |
70256ad8 AJ |
1596 | } |
1597 | } | |
c434dee6 AJ |
1598 | |
1599 | local_got = elf_local_got_refcounts (ibfd); | |
1600 | if (!local_got) | |
1601 | continue; | |
1602 | ||
1603 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
1604 | locsymcount = symtab_hdr->sh_info; | |
1605 | end_local_got = local_got + locsymcount; | |
bffbf940 | 1606 | local_tls_type = elf64_x86_64_local_got_tls_type (ibfd); |
c434dee6 AJ |
1607 | s = htab->sgot; |
1608 | srel = htab->srelgot; | |
bffbf940 | 1609 | for (; local_got < end_local_got; ++local_got, ++local_tls_type) |
70256ad8 | 1610 | { |
c434dee6 | 1611 | if (*local_got > 0) |
70256ad8 | 1612 | { |
eea6121a AM |
1613 | *local_got = s->size; |
1614 | s->size += GOT_ENTRY_SIZE; | |
bffbf940 | 1615 | if (*local_tls_type == GOT_TLS_GD) |
eea6121a | 1616 | s->size += GOT_ENTRY_SIZE; |
bffbf940 JJ |
1617 | if (info->shared |
1618 | || *local_tls_type == GOT_TLS_GD | |
1619 | || *local_tls_type == GOT_TLS_IE) | |
eea6121a | 1620 | srel->size += sizeof (Elf64_External_Rela); |
70256ad8 AJ |
1621 | } |
1622 | else | |
c434dee6 AJ |
1623 | *local_got = (bfd_vma) -1; |
1624 | } | |
1625 | } | |
70256ad8 | 1626 | |
bffbf940 JJ |
1627 | if (htab->tls_ld_got.refcount > 0) |
1628 | { | |
1629 | /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD | |
1630 | relocs. */ | |
eea6121a AM |
1631 | htab->tls_ld_got.offset = htab->sgot->size; |
1632 | htab->sgot->size += 2 * GOT_ENTRY_SIZE; | |
1633 | htab->srelgot->size += sizeof (Elf64_External_Rela); | |
bffbf940 JJ |
1634 | } |
1635 | else | |
1636 | htab->tls_ld_got.offset = -1; | |
1637 | ||
c434dee6 AJ |
1638 | /* Allocate global sym .plt and .got entries, and space for global |
1639 | sym dynamic relocs. */ | |
1640 | elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info); | |
1641 | ||
1642 | /* We now have determined the sizes of the various dynamic sections. | |
1643 | Allocate memory for them. */ | |
b34976b6 | 1644 | relocs = FALSE; |
c434dee6 AJ |
1645 | for (s = dynobj->sections; s != NULL; s = s->next) |
1646 | { | |
1647 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
1648 | continue; | |
1649 | ||
1650 | if (s == htab->splt | |
1651 | || s == htab->sgot | |
75ff4589 L |
1652 | || s == htab->sgotplt |
1653 | || s == htab->sdynbss) | |
c434dee6 AJ |
1654 | { |
1655 | /* Strip this section if we don't need it; see the | |
1656 | comment below. */ | |
1657 | } | |
1658 | else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0) | |
1659 | { | |
eea6121a | 1660 | if (s->size != 0 && s != htab->srelplt) |
b34976b6 | 1661 | relocs = TRUE; |
c434dee6 AJ |
1662 | |
1663 | /* We use the reloc_count field as a counter if we need | |
1664 | to copy relocs into the output file. */ | |
1665 | s->reloc_count = 0; | |
70256ad8 | 1666 | } |
c434dee6 | 1667 | else |
70256ad8 AJ |
1668 | { |
1669 | /* It's not one of our sections, so don't allocate space. */ | |
1670 | continue; | |
1671 | } | |
1672 | ||
eea6121a | 1673 | if (s->size == 0) |
70256ad8 | 1674 | { |
c434dee6 AJ |
1675 | /* If we don't need this section, strip it from the |
1676 | output file. This is mostly to handle .rela.bss and | |
1677 | .rela.plt. We must create both sections in | |
1678 | create_dynamic_sections, because they must be created | |
1679 | before the linker maps input sections to output | |
1680 | sections. The linker does that before | |
1681 | adjust_dynamic_symbol is called, and it is that | |
1682 | function which decides whether anything needs to go | |
1683 | into these sections. */ | |
1684 | ||
8423293d | 1685 | s->flags |= SEC_EXCLUDE; |
70256ad8 AJ |
1686 | continue; |
1687 | } | |
1688 | ||
c456f082 AM |
1689 | if ((s->flags & SEC_HAS_CONTENTS) == 0) |
1690 | continue; | |
1691 | ||
70256ad8 AJ |
1692 | /* Allocate memory for the section contents. We use bfd_zalloc |
1693 | here in case unused entries are not reclaimed before the | |
1694 | section's contents are written out. This should not happen, | |
1695 | but this way if it does, we get a R_X86_64_NONE reloc instead | |
1696 | of garbage. */ | |
eea6121a | 1697 | s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size); |
c434dee6 | 1698 | if (s->contents == NULL) |
b34976b6 | 1699 | return FALSE; |
70256ad8 AJ |
1700 | } |
1701 | ||
c434dee6 | 1702 | if (htab->elf.dynamic_sections_created) |
70256ad8 AJ |
1703 | { |
1704 | /* Add some entries to the .dynamic section. We fill in the | |
1705 | values later, in elf64_x86_64_finish_dynamic_sections, but we | |
1706 | must add the entries now so that we get the correct size for | |
407443a3 | 1707 | the .dynamic section. The DT_DEBUG entry is filled in by the |
70256ad8 | 1708 | dynamic linker and used by the debugger. */ |
dc810e39 | 1709 | #define add_dynamic_entry(TAG, VAL) \ |
5a580b3a | 1710 | _bfd_elf_add_dynamic_entry (info, TAG, VAL) |
dc810e39 | 1711 | |
36af4a4e | 1712 | if (info->executable) |
70256ad8 | 1713 | { |
dc810e39 | 1714 | if (!add_dynamic_entry (DT_DEBUG, 0)) |
b34976b6 | 1715 | return FALSE; |
70256ad8 AJ |
1716 | } |
1717 | ||
eea6121a | 1718 | if (htab->splt->size != 0) |
70256ad8 | 1719 | { |
dc810e39 AM |
1720 | if (!add_dynamic_entry (DT_PLTGOT, 0) |
1721 | || !add_dynamic_entry (DT_PLTRELSZ, 0) | |
1722 | || !add_dynamic_entry (DT_PLTREL, DT_RELA) | |
1723 | || !add_dynamic_entry (DT_JMPREL, 0)) | |
b34976b6 | 1724 | return FALSE; |
70256ad8 AJ |
1725 | } |
1726 | ||
1727 | if (relocs) | |
1728 | { | |
dc810e39 AM |
1729 | if (!add_dynamic_entry (DT_RELA, 0) |
1730 | || !add_dynamic_entry (DT_RELASZ, 0) | |
1731 | || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela))) | |
b34976b6 | 1732 | return FALSE; |
70256ad8 | 1733 | |
c434dee6 AJ |
1734 | /* If any dynamic relocs apply to a read-only section, |
1735 | then we need a DT_TEXTREL entry. */ | |
1736 | if ((info->flags & DF_TEXTREL) == 0) | |
1737 | elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, | |
1738 | (PTR) info); | |
1739 | ||
1740 | if ((info->flags & DF_TEXTREL) != 0) | |
1741 | { | |
1742 | if (!add_dynamic_entry (DT_TEXTREL, 0)) | |
b34976b6 | 1743 | return FALSE; |
c434dee6 | 1744 | } |
70256ad8 AJ |
1745 | } |
1746 | } | |
dc810e39 | 1747 | #undef add_dynamic_entry |
70256ad8 | 1748 | |
b34976b6 | 1749 | return TRUE; |
70256ad8 AJ |
1750 | } |
1751 | ||
bffbf940 JJ |
1752 | /* Return the base VMA address which should be subtracted from real addresses |
1753 | when resolving @dtpoff relocation. | |
1754 | This is PT_TLS segment p_vaddr. */ | |
1755 | ||
1756 | static bfd_vma | |
27482721 | 1757 | dtpoff_base (struct bfd_link_info *info) |
bffbf940 | 1758 | { |
e1918d23 AM |
1759 | /* If tls_sec is NULL, we should have signalled an error already. */ |
1760 | if (elf_hash_table (info)->tls_sec == NULL) | |
bffbf940 | 1761 | return 0; |
e1918d23 | 1762 | return elf_hash_table (info)->tls_sec->vma; |
bffbf940 JJ |
1763 | } |
1764 | ||
1765 | /* Return the relocation value for @tpoff relocation | |
1766 | if STT_TLS virtual address is ADDRESS. */ | |
1767 | ||
1768 | static bfd_vma | |
27482721 | 1769 | tpoff (struct bfd_link_info *info, bfd_vma address) |
bffbf940 | 1770 | { |
e1918d23 | 1771 | struct elf_link_hash_table *htab = elf_hash_table (info); |
bffbf940 JJ |
1772 | |
1773 | /* If tls_segment is NULL, we should have signalled an error already. */ | |
e1918d23 | 1774 | if (htab->tls_sec == NULL) |
bffbf940 | 1775 | return 0; |
e1918d23 | 1776 | return address - htab->tls_size - htab->tls_sec->vma; |
bffbf940 JJ |
1777 | } |
1778 | ||
90f487df L |
1779 | /* Is the instruction before OFFSET in CONTENTS a 32bit relative |
1780 | branch? */ | |
1781 | ||
1782 | static bfd_boolean | |
1783 | is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset) | |
1784 | { | |
1785 | /* Opcode Instruction | |
1786 | 0xe8 call | |
1787 | 0xe9 jump | |
1788 | 0x0f 0x8x conditional jump */ | |
1789 | return ((offset > 0 | |
1790 | && (contents [offset - 1] == 0xe8 | |
1791 | || contents [offset - 1] == 0xe9)) | |
1792 | || (offset > 1 | |
1793 | && contents [offset - 2] == 0x0f | |
1794 | && (contents [offset - 1] & 0xf0) == 0x80)); | |
1795 | } | |
1796 | ||
8d88c4ca NC |
1797 | /* Relocate an x86_64 ELF section. */ |
1798 | ||
b34976b6 | 1799 | static bfd_boolean |
27482721 AJ |
1800 | elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info, |
1801 | bfd *input_bfd, asection *input_section, | |
1802 | bfd_byte *contents, Elf_Internal_Rela *relocs, | |
1803 | Elf_Internal_Sym *local_syms, | |
1804 | asection **local_sections) | |
8d88c4ca | 1805 | { |
c434dee6 | 1806 | struct elf64_x86_64_link_hash_table *htab; |
8d88c4ca NC |
1807 | Elf_Internal_Shdr *symtab_hdr; |
1808 | struct elf_link_hash_entry **sym_hashes; | |
1809 | bfd_vma *local_got_offsets; | |
c434dee6 | 1810 | Elf_Internal_Rela *rel; |
8d88c4ca NC |
1811 | Elf_Internal_Rela *relend; |
1812 | ||
1049f94e | 1813 | if (info->relocatable) |
b34976b6 | 1814 | return TRUE; |
b491616a | 1815 | |
c434dee6 | 1816 | htab = elf64_x86_64_hash_table (info); |
8d88c4ca NC |
1817 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
1818 | sym_hashes = elf_sym_hashes (input_bfd); | |
1819 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
1820 | ||
c434dee6 | 1821 | rel = relocs; |
8d88c4ca | 1822 | relend = relocs + input_section->reloc_count; |
c434dee6 | 1823 | for (; rel < relend; rel++) |
8d88c4ca | 1824 | { |
bffbf940 | 1825 | unsigned int r_type; |
8d88c4ca NC |
1826 | reloc_howto_type *howto; |
1827 | unsigned long r_symndx; | |
1828 | struct elf_link_hash_entry *h; | |
1829 | Elf_Internal_Sym *sym; | |
1830 | asection *sec; | |
c434dee6 | 1831 | bfd_vma off; |
8d88c4ca | 1832 | bfd_vma relocation; |
b34976b6 | 1833 | bfd_boolean unresolved_reloc; |
8d88c4ca | 1834 | bfd_reloc_status_type r; |
bffbf940 | 1835 | int tls_type; |
8d88c4ca | 1836 | |
c434dee6 | 1837 | r_type = ELF64_R_TYPE (rel->r_info); |
fe4770f4 AJ |
1838 | if (r_type == (int) R_X86_64_GNU_VTINHERIT |
1839 | || r_type == (int) R_X86_64_GNU_VTENTRY) | |
1840 | continue; | |
8d88c4ca | 1841 | |
bffbf940 | 1842 | if (r_type >= R_X86_64_max) |
8da6118f KH |
1843 | { |
1844 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 1845 | return FALSE; |
8da6118f | 1846 | } |
8d88c4ca | 1847 | |
b491616a | 1848 | howto = x86_64_elf_howto_table + r_type; |
c434dee6 | 1849 | r_symndx = ELF64_R_SYM (rel->r_info); |
8d88c4ca NC |
1850 | h = NULL; |
1851 | sym = NULL; | |
1852 | sec = NULL; | |
b34976b6 | 1853 | unresolved_reloc = FALSE; |
8d88c4ca | 1854 | if (r_symndx < symtab_hdr->sh_info) |
8da6118f KH |
1855 | { |
1856 | sym = local_syms + r_symndx; | |
1857 | sec = local_sections[r_symndx]; | |
c434dee6 | 1858 | |
8517fae7 | 1859 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); |
8da6118f | 1860 | } |
8d88c4ca | 1861 | else |
8da6118f | 1862 | { |
560e09e9 | 1863 | bfd_boolean warned; |
c434dee6 | 1864 | |
b2a8e766 AM |
1865 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, |
1866 | r_symndx, symtab_hdr, sym_hashes, | |
1867 | h, sec, relocation, | |
1868 | unresolved_reloc, warned); | |
8da6118f | 1869 | } |
70256ad8 AJ |
1870 | /* When generating a shared object, the relocations handled here are |
1871 | copied into the output file to be resolved at run time. */ | |
1872 | switch (r_type) | |
1873 | { | |
1874 | case R_X86_64_GOT32: | |
1875 | /* Relocation is to the entry for this symbol in the global | |
1876 | offset table. */ | |
70256ad8 AJ |
1877 | case R_X86_64_GOTPCREL: |
1878 | /* Use global offset table as symbol value. */ | |
c434dee6 AJ |
1879 | if (htab->sgot == NULL) |
1880 | abort (); | |
053579d7 | 1881 | |
51e0a107 | 1882 | if (h != NULL) |
70256ad8 | 1883 | { |
b34976b6 | 1884 | bfd_boolean dyn; |
c434dee6 AJ |
1885 | |
1886 | off = h->got.offset; | |
1887 | dyn = htab->elf.dynamic_sections_created; | |
51e0a107 | 1888 | |
27482721 | 1889 | if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) |
51e0a107 | 1890 | || (info->shared |
27482721 | 1891 | && SYMBOL_REFERENCES_LOCAL (info, h)) |
4bc6e03a AJ |
1892 | || (ELF_ST_VISIBILITY (h->other) |
1893 | && h->root.type == bfd_link_hash_undefweak)) | |
51e0a107 JH |
1894 | { |
1895 | /* This is actually a static link, or it is a -Bsymbolic | |
1896 | link and the symbol is defined locally, or the symbol | |
407443a3 | 1897 | was forced to be local because of a version file. We |
51e0a107 JH |
1898 | must initialize this entry in the global offset table. |
1899 | Since the offset must always be a multiple of 8, we | |
1900 | use the least significant bit to record whether we | |
1901 | have initialized it already. | |
1902 | ||
1903 | When doing a dynamic link, we create a .rela.got | |
407443a3 AJ |
1904 | relocation entry to initialize the value. This is |
1905 | done in the finish_dynamic_symbol routine. */ | |
51e0a107 JH |
1906 | if ((off & 1) != 0) |
1907 | off &= ~1; | |
1908 | else | |
1909 | { | |
1910 | bfd_put_64 (output_bfd, relocation, | |
c434dee6 | 1911 | htab->sgot->contents + off); |
51e0a107 JH |
1912 | h->got.offset |= 1; |
1913 | } | |
1914 | } | |
053579d7 | 1915 | else |
b34976b6 | 1916 | unresolved_reloc = FALSE; |
70256ad8 | 1917 | } |
51e0a107 JH |
1918 | else |
1919 | { | |
c434dee6 AJ |
1920 | if (local_got_offsets == NULL) |
1921 | abort (); | |
51e0a107 JH |
1922 | |
1923 | off = local_got_offsets[r_symndx]; | |
1924 | ||
1925 | /* The offset must always be a multiple of 8. We use | |
407443a3 AJ |
1926 | the least significant bit to record whether we have |
1927 | already generated the necessary reloc. */ | |
51e0a107 JH |
1928 | if ((off & 1) != 0) |
1929 | off &= ~1; | |
1930 | else | |
1931 | { | |
c434dee6 AJ |
1932 | bfd_put_64 (output_bfd, relocation, |
1933 | htab->sgot->contents + off); | |
51e0a107 JH |
1934 | |
1935 | if (info->shared) | |
1936 | { | |
947216bf | 1937 | asection *s; |
51e0a107 | 1938 | Elf_Internal_Rela outrel; |
947216bf | 1939 | bfd_byte *loc; |
70256ad8 | 1940 | |
51e0a107 JH |
1941 | /* We need to generate a R_X86_64_RELATIVE reloc |
1942 | for the dynamic linker. */ | |
947216bf AM |
1943 | s = htab->srelgot; |
1944 | if (s == NULL) | |
c434dee6 | 1945 | abort (); |
51e0a107 | 1946 | |
c434dee6 AJ |
1947 | outrel.r_offset = (htab->sgot->output_section->vma |
1948 | + htab->sgot->output_offset | |
51e0a107 JH |
1949 | + off); |
1950 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); | |
1951 | outrel.r_addend = relocation; | |
947216bf AM |
1952 | loc = s->contents; |
1953 | loc += s->reloc_count++ * sizeof (Elf64_External_Rela); | |
c434dee6 | 1954 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); |
51e0a107 JH |
1955 | } |
1956 | ||
1957 | local_got_offsets[r_symndx] |= 1; | |
1958 | } | |
51e0a107 | 1959 | } |
6a2bda3f | 1960 | |
c434dee6 AJ |
1961 | if (off >= (bfd_vma) -2) |
1962 | abort (); | |
1963 | ||
8c37241b JJ |
1964 | relocation = htab->sgot->output_section->vma |
1965 | + htab->sgot->output_offset + off; | |
1966 | if (r_type != R_X86_64_GOTPCREL) | |
1967 | relocation -= htab->sgotplt->output_section->vma | |
1968 | - htab->sgotplt->output_offset; | |
c434dee6 | 1969 | |
70256ad8 AJ |
1970 | break; |
1971 | ||
d6ab8113 JB |
1972 | case R_X86_64_GOTOFF64: |
1973 | /* Relocation is relative to the start of the global offset | |
1974 | table. */ | |
1975 | ||
1976 | /* Check to make sure it isn't a protected function symbol | |
1977 | for shared library since it may not be local when used | |
1978 | as function address. */ | |
1979 | if (info->shared | |
1980 | && h | |
1981 | && h->def_regular | |
1982 | && h->type == STT_FUNC | |
1983 | && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
1984 | { | |
1985 | (*_bfd_error_handler) | |
1986 | (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"), | |
1987 | input_bfd, h->root.root.string); | |
1988 | bfd_set_error (bfd_error_bad_value); | |
1989 | return FALSE; | |
1990 | } | |
1991 | ||
1992 | /* Note that sgot is not involved in this | |
1993 | calculation. We always want the start of .got.plt. If we | |
1994 | defined _GLOBAL_OFFSET_TABLE_ in a different way, as is | |
1995 | permitted by the ABI, we might have to change this | |
1996 | calculation. */ | |
1997 | relocation -= htab->sgotplt->output_section->vma | |
1998 | + htab->sgotplt->output_offset; | |
1999 | break; | |
2000 | ||
2001 | case R_X86_64_GOTPC32: | |
2002 | /* Use global offset table as symbol value. */ | |
2003 | relocation = htab->sgotplt->output_section->vma | |
2004 | + htab->sgotplt->output_offset; | |
2005 | unresolved_reloc = FALSE; | |
2006 | break; | |
2007 | ||
70256ad8 AJ |
2008 | case R_X86_64_PLT32: |
2009 | /* Relocation is to the entry for this symbol in the | |
2010 | procedure linkage table. */ | |
2011 | ||
2012 | /* Resolve a PLT32 reloc against a local symbol directly, | |
407443a3 | 2013 | without using the procedure linkage table. */ |
70256ad8 AJ |
2014 | if (h == NULL) |
2015 | break; | |
2016 | ||
c434dee6 AJ |
2017 | if (h->plt.offset == (bfd_vma) -1 |
2018 | || htab->splt == NULL) | |
70256ad8 AJ |
2019 | { |
2020 | /* We didn't make a PLT entry for this symbol. This | |
407443a3 AJ |
2021 | happens when statically linking PIC code, or when |
2022 | using -Bsymbolic. */ | |
70256ad8 AJ |
2023 | break; |
2024 | } | |
2025 | ||
c434dee6 AJ |
2026 | relocation = (htab->splt->output_section->vma |
2027 | + htab->splt->output_offset | |
70256ad8 | 2028 | + h->plt.offset); |
b34976b6 | 2029 | unresolved_reloc = FALSE; |
70256ad8 AJ |
2030 | break; |
2031 | ||
fd8ab9e5 AJ |
2032 | case R_X86_64_PC8: |
2033 | case R_X86_64_PC16: | |
2034 | case R_X86_64_PC32: | |
6610a52d L |
2035 | if (info->shared |
2036 | && !SYMBOL_REFERENCES_LOCAL (info, h) | |
ba3bee0b | 2037 | && (input_section->flags & SEC_ALLOC) != 0 |
90f487df L |
2038 | && (input_section->flags & SEC_READONLY) != 0 |
2039 | && (!h->def_regular | |
2040 | || r_type != R_X86_64_PC32 | |
2041 | || h->type != STT_FUNC | |
2042 | || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED | |
2043 | || !is_32bit_relative_branch (contents, | |
2044 | rel->r_offset))) | |
6610a52d | 2045 | { |
90f487df L |
2046 | if (h->def_regular |
2047 | && r_type == R_X86_64_PC32 | |
2048 | && h->type == STT_FUNC | |
2049 | && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) | |
2050 | (*_bfd_error_handler) | |
2051 | (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"), | |
2052 | input_bfd, h->root.root.string); | |
2053 | else | |
2054 | (*_bfd_error_handler) | |
2055 | (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), | |
2056 | input_bfd, x86_64_elf_howto_table[r_type].name, | |
2057 | h->root.root.string); | |
6610a52d L |
2058 | bfd_set_error (bfd_error_bad_value); |
2059 | return FALSE; | |
2060 | } | |
2061 | /* Fall through. */ | |
2062 | ||
70256ad8 AJ |
2063 | case R_X86_64_8: |
2064 | case R_X86_64_16: | |
2065 | case R_X86_64_32: | |
d6ab8113 | 2066 | case R_X86_64_PC64: |
6b3db546 | 2067 | case R_X86_64_64: |
80643fbc | 2068 | /* FIXME: The ABI says the linker should make sure the value is |
407443a3 | 2069 | the same when it's zeroextended to 64 bit. */ |
c434dee6 AJ |
2070 | |
2071 | /* r_symndx will be zero only for relocs against symbols | |
2072 | from removed linkonce sections, or sections discarded by | |
2073 | a linker script. */ | |
2074 | if (r_symndx == 0 | |
2075 | || (input_section->flags & SEC_ALLOC) == 0) | |
2076 | break; | |
2077 | ||
2078 | if ((info->shared | |
4bc6e03a AJ |
2079 | && (h == NULL |
2080 | || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
2081 | || h->root.type != bfd_link_hash_undefweak) | |
c434dee6 AJ |
2082 | && ((r_type != R_X86_64_PC8 |
2083 | && r_type != R_X86_64_PC16 | |
d6ab8113 JB |
2084 | && r_type != R_X86_64_PC32 |
2085 | && r_type != R_X86_64_PC64) | |
f6c52c13 | 2086 | || !SYMBOL_CALLS_LOCAL (info, h))) |
d40d037c AJ |
2087 | || (ELIMINATE_COPY_RELOCS |
2088 | && !info->shared | |
c434dee6 AJ |
2089 | && h != NULL |
2090 | && h->dynindx != -1 | |
f5385ebf AM |
2091 | && !h->non_got_ref |
2092 | && ((h->def_dynamic | |
2093 | && !h->def_regular) | |
c434dee6 AJ |
2094 | || h->root.type == bfd_link_hash_undefweak |
2095 | || h->root.type == bfd_link_hash_undefined))) | |
70256ad8 AJ |
2096 | { |
2097 | Elf_Internal_Rela outrel; | |
947216bf | 2098 | bfd_byte *loc; |
b34976b6 | 2099 | bfd_boolean skip, relocate; |
c434dee6 | 2100 | asection *sreloc; |
70256ad8 AJ |
2101 | |
2102 | /* When generating a shared object, these relocations | |
2103 | are copied into the output file to be resolved at run | |
407443a3 | 2104 | time. */ |
b34976b6 AM |
2105 | skip = FALSE; |
2106 | relocate = FALSE; | |
70256ad8 | 2107 | |
c629eae0 JJ |
2108 | outrel.r_offset = |
2109 | _bfd_elf_section_offset (output_bfd, info, input_section, | |
c434dee6 | 2110 | rel->r_offset); |
c629eae0 | 2111 | if (outrel.r_offset == (bfd_vma) -1) |
b34976b6 | 2112 | skip = TRUE; |
0fb19cbc | 2113 | else if (outrel.r_offset == (bfd_vma) -2) |
b34976b6 | 2114 | skip = TRUE, relocate = TRUE; |
70256ad8 AJ |
2115 | |
2116 | outrel.r_offset += (input_section->output_section->vma | |
2117 | + input_section->output_offset); | |
2118 | ||
2119 | if (skip) | |
0bb2d96a | 2120 | memset (&outrel, 0, sizeof outrel); |
c434dee6 | 2121 | |
fd8ab9e5 AJ |
2122 | /* h->dynindx may be -1 if this symbol was marked to |
2123 | become local. */ | |
2124 | else if (h != NULL | |
c434dee6 AJ |
2125 | && h->dynindx != -1 |
2126 | && (r_type == R_X86_64_PC8 | |
2127 | || r_type == R_X86_64_PC16 | |
2128 | || r_type == R_X86_64_PC32 | |
d6ab8113 | 2129 | || r_type == R_X86_64_PC64 |
c434dee6 AJ |
2130 | || !info->shared |
2131 | || !info->symbolic | |
f5385ebf | 2132 | || !h->def_regular)) |
70256ad8 | 2133 | { |
70256ad8 | 2134 | outrel.r_info = ELF64_R_INFO (h->dynindx, r_type); |
c434dee6 | 2135 | outrel.r_addend = rel->r_addend; |
70256ad8 AJ |
2136 | } |
2137 | else | |
2138 | { | |
c434dee6 | 2139 | /* This symbol is local, or marked to become local. */ |
607c0e09 AS |
2140 | if (r_type == R_X86_64_64) |
2141 | { | |
b34976b6 | 2142 | relocate = TRUE; |
607c0e09 AS |
2143 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); |
2144 | outrel.r_addend = relocation + rel->r_addend; | |
2145 | } | |
2146 | else | |
2147 | { | |
2148 | long sindx; | |
2149 | ||
8517fae7 | 2150 | if (bfd_is_abs_section (sec)) |
607c0e09 AS |
2151 | sindx = 0; |
2152 | else if (sec == NULL || sec->owner == NULL) | |
2153 | { | |
2154 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 2155 | return FALSE; |
607c0e09 AS |
2156 | } |
2157 | else | |
2158 | { | |
2159 | asection *osec; | |
2160 | ||
2161 | osec = sec->output_section; | |
2162 | sindx = elf_section_data (osec)->dynindx; | |
2163 | BFD_ASSERT (sindx > 0); | |
2164 | } | |
2165 | ||
2166 | outrel.r_info = ELF64_R_INFO (sindx, r_type); | |
2167 | outrel.r_addend = relocation + rel->r_addend; | |
2168 | } | |
70256ad8 AJ |
2169 | } |
2170 | ||
c434dee6 AJ |
2171 | sreloc = elf_section_data (input_section)->sreloc; |
2172 | if (sreloc == NULL) | |
2173 | abort (); | |
2174 | ||
947216bf AM |
2175 | loc = sreloc->contents; |
2176 | loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela); | |
c434dee6 | 2177 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); |
70256ad8 AJ |
2178 | |
2179 | /* If this reloc is against an external symbol, we do | |
2180 | not want to fiddle with the addend. Otherwise, we | |
2181 | need to include the symbol value so that it becomes | |
2182 | an addend for the dynamic reloc. */ | |
2183 | if (! relocate) | |
2184 | continue; | |
2185 | } | |
2186 | ||
2187 | break; | |
2188 | ||
bffbf940 JJ |
2189 | case R_X86_64_TLSGD: |
2190 | case R_X86_64_GOTTPOFF: | |
2191 | r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL); | |
2192 | tls_type = GOT_UNKNOWN; | |
2193 | if (h == NULL && local_got_offsets) | |
2194 | tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx]; | |
2195 | else if (h != NULL) | |
2196 | { | |
2197 | tls_type = elf64_x86_64_hash_entry (h)->tls_type; | |
2198 | if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE) | |
2199 | r_type = R_X86_64_TPOFF32; | |
2200 | } | |
2201 | if (r_type == R_X86_64_TLSGD) | |
2202 | { | |
2203 | if (tls_type == GOT_TLS_IE) | |
2204 | r_type = R_X86_64_GOTTPOFF; | |
2205 | } | |
2206 | ||
2207 | if (r_type == R_X86_64_TPOFF32) | |
2208 | { | |
2209 | BFD_ASSERT (! unresolved_reloc); | |
2210 | if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD) | |
2211 | { | |
2212 | unsigned int i; | |
abcf1d52 JJ |
2213 | static unsigned char tlsgd[8] |
2214 | = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 }; | |
bffbf940 JJ |
2215 | |
2216 | /* GD->LE transition. | |
abcf1d52 JJ |
2217 | .byte 0x66; leaq foo@tlsgd(%rip), %rdi |
2218 | .word 0x6666; rex64; call __tls_get_addr@plt | |
bffbf940 JJ |
2219 | Change it into: |
2220 | movq %fs:0, %rax | |
2221 | leaq foo@tpoff(%rax), %rax */ | |
abcf1d52 JJ |
2222 | BFD_ASSERT (rel->r_offset >= 4); |
2223 | for (i = 0; i < 4; i++) | |
bffbf940 | 2224 | BFD_ASSERT (bfd_get_8 (input_bfd, |
abcf1d52 | 2225 | contents + rel->r_offset - 4 + i) |
bffbf940 | 2226 | == tlsgd[i]); |
eea6121a | 2227 | BFD_ASSERT (rel->r_offset + 12 <= input_section->size); |
abcf1d52 JJ |
2228 | for (i = 0; i < 4; i++) |
2229 | BFD_ASSERT (bfd_get_8 (input_bfd, | |
2230 | contents + rel->r_offset + 4 + i) | |
2231 | == tlsgd[i+4]); | |
bffbf940 JJ |
2232 | BFD_ASSERT (rel + 1 < relend); |
2233 | BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32); | |
abcf1d52 | 2234 | memcpy (contents + rel->r_offset - 4, |
bffbf940 JJ |
2235 | "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0", |
2236 | 16); | |
2237 | bfd_put_32 (output_bfd, tpoff (info, relocation), | |
abcf1d52 | 2238 | contents + rel->r_offset + 8); |
bffbf940 JJ |
2239 | /* Skip R_X86_64_PLT32. */ |
2240 | rel++; | |
2241 | continue; | |
2242 | } | |
2243 | else | |
2244 | { | |
2245 | unsigned int val, type, reg; | |
2246 | ||
2247 | /* IE->LE transition: | |
2248 | Originally it can be one of: | |
2249 | movq foo@gottpoff(%rip), %reg | |
2250 | addq foo@gottpoff(%rip), %reg | |
2251 | We change it into: | |
2252 | movq $foo, %reg | |
2253 | leaq foo(%reg), %reg | |
2254 | addq $foo, %reg. */ | |
2255 | BFD_ASSERT (rel->r_offset >= 3); | |
2256 | val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3); | |
2257 | BFD_ASSERT (val == 0x48 || val == 0x4c); | |
2258 | type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2); | |
2259 | BFD_ASSERT (type == 0x8b || type == 0x03); | |
2260 | reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1); | |
2261 | BFD_ASSERT ((reg & 0xc7) == 5); | |
2262 | reg >>= 3; | |
eea6121a | 2263 | BFD_ASSERT (rel->r_offset + 4 <= input_section->size); |
bffbf940 JJ |
2264 | if (type == 0x8b) |
2265 | { | |
2266 | /* movq */ | |
2267 | if (val == 0x4c) | |
2268 | bfd_put_8 (output_bfd, 0x49, | |
2269 | contents + rel->r_offset - 3); | |
2270 | bfd_put_8 (output_bfd, 0xc7, | |
2271 | contents + rel->r_offset - 2); | |
2272 | bfd_put_8 (output_bfd, 0xc0 | reg, | |
2273 | contents + rel->r_offset - 1); | |
2274 | } | |
2275 | else if (reg == 4) | |
2276 | { | |
2277 | /* addq -> addq - addressing with %rsp/%r12 is | |
2278 | special */ | |
2279 | if (val == 0x4c) | |
2280 | bfd_put_8 (output_bfd, 0x49, | |
2281 | contents + rel->r_offset - 3); | |
2282 | bfd_put_8 (output_bfd, 0x81, | |
2283 | contents + rel->r_offset - 2); | |
2284 | bfd_put_8 (output_bfd, 0xc0 | reg, | |
2285 | contents + rel->r_offset - 1); | |
2286 | } | |
2287 | else | |
2288 | { | |
2289 | /* addq -> leaq */ | |
2290 | if (val == 0x4c) | |
2291 | bfd_put_8 (output_bfd, 0x4d, | |
2292 | contents + rel->r_offset - 3); | |
2293 | bfd_put_8 (output_bfd, 0x8d, | |
2294 | contents + rel->r_offset - 2); | |
2295 | bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3), | |
2296 | contents + rel->r_offset - 1); | |
2297 | } | |
2298 | bfd_put_32 (output_bfd, tpoff (info, relocation), | |
2299 | contents + rel->r_offset); | |
2300 | continue; | |
2301 | } | |
2302 | } | |
2303 | ||
2304 | if (htab->sgot == NULL) | |
2305 | abort (); | |
2306 | ||
2307 | if (h != NULL) | |
2308 | off = h->got.offset; | |
2309 | else | |
2310 | { | |
2311 | if (local_got_offsets == NULL) | |
2312 | abort (); | |
2313 | ||
2314 | off = local_got_offsets[r_symndx]; | |
2315 | } | |
2316 | ||
2317 | if ((off & 1) != 0) | |
2318 | off &= ~1; | |
26e41594 | 2319 | else |
bffbf940 JJ |
2320 | { |
2321 | Elf_Internal_Rela outrel; | |
947216bf | 2322 | bfd_byte *loc; |
bffbf940 JJ |
2323 | int dr_type, indx; |
2324 | ||
2325 | if (htab->srelgot == NULL) | |
2326 | abort (); | |
2327 | ||
2328 | outrel.r_offset = (htab->sgot->output_section->vma | |
2329 | + htab->sgot->output_offset + off); | |
2330 | ||
2331 | indx = h && h->dynindx != -1 ? h->dynindx : 0; | |
2332 | if (r_type == R_X86_64_TLSGD) | |
2333 | dr_type = R_X86_64_DTPMOD64; | |
2334 | else | |
2335 | dr_type = R_X86_64_TPOFF64; | |
2336 | ||
2337 | bfd_put_64 (output_bfd, 0, htab->sgot->contents + off); | |
2338 | outrel.r_addend = 0; | |
2339 | if (dr_type == R_X86_64_TPOFF64 && indx == 0) | |
2340 | outrel.r_addend = relocation - dtpoff_base (info); | |
2341 | outrel.r_info = ELF64_R_INFO (indx, dr_type); | |
2342 | ||
947216bf AM |
2343 | loc = htab->srelgot->contents; |
2344 | loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela); | |
bffbf940 JJ |
2345 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); |
2346 | ||
2347 | if (r_type == R_X86_64_TLSGD) | |
2348 | { | |
2349 | if (indx == 0) | |
2350 | { | |
d40d037c | 2351 | BFD_ASSERT (! unresolved_reloc); |
bffbf940 JJ |
2352 | bfd_put_64 (output_bfd, |
2353 | relocation - dtpoff_base (info), | |
2354 | htab->sgot->contents + off + GOT_ENTRY_SIZE); | |
2355 | } | |
2356 | else | |
2357 | { | |
2358 | bfd_put_64 (output_bfd, 0, | |
2359 | htab->sgot->contents + off + GOT_ENTRY_SIZE); | |
2360 | outrel.r_info = ELF64_R_INFO (indx, | |
2361 | R_X86_64_DTPOFF64); | |
2362 | outrel.r_offset += GOT_ENTRY_SIZE; | |
2363 | htab->srelgot->reloc_count++; | |
947216bf AM |
2364 | loc += sizeof (Elf64_External_Rela); |
2365 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); | |
bffbf940 JJ |
2366 | } |
2367 | } | |
2368 | ||
2369 | if (h != NULL) | |
2370 | h->got.offset |= 1; | |
2371 | else | |
2372 | local_got_offsets[r_symndx] |= 1; | |
2373 | } | |
2374 | ||
2375 | if (off >= (bfd_vma) -2) | |
2376 | abort (); | |
2377 | if (r_type == ELF64_R_TYPE (rel->r_info)) | |
2378 | { | |
2379 | relocation = htab->sgot->output_section->vma | |
2380 | + htab->sgot->output_offset + off; | |
b34976b6 | 2381 | unresolved_reloc = FALSE; |
bffbf940 JJ |
2382 | } |
2383 | else | |
2384 | { | |
2385 | unsigned int i; | |
abcf1d52 JJ |
2386 | static unsigned char tlsgd[8] |
2387 | = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 }; | |
bffbf940 JJ |
2388 | |
2389 | /* GD->IE transition. | |
abcf1d52 JJ |
2390 | .byte 0x66; leaq foo@tlsgd(%rip), %rdi |
2391 | .word 0x6666; rex64; call __tls_get_addr@plt | |
bffbf940 JJ |
2392 | Change it into: |
2393 | movq %fs:0, %rax | |
2394 | addq foo@gottpoff(%rip), %rax */ | |
abcf1d52 JJ |
2395 | BFD_ASSERT (rel->r_offset >= 4); |
2396 | for (i = 0; i < 4; i++) | |
26e41594 | 2397 | BFD_ASSERT (bfd_get_8 (input_bfd, |
abcf1d52 | 2398 | contents + rel->r_offset - 4 + i) |
bffbf940 | 2399 | == tlsgd[i]); |
eea6121a | 2400 | BFD_ASSERT (rel->r_offset + 12 <= input_section->size); |
abcf1d52 | 2401 | for (i = 0; i < 4; i++) |
26e41594 | 2402 | BFD_ASSERT (bfd_get_8 (input_bfd, |
abcf1d52 JJ |
2403 | contents + rel->r_offset + 4 + i) |
2404 | == tlsgd[i+4]); | |
bffbf940 JJ |
2405 | BFD_ASSERT (rel + 1 < relend); |
2406 | BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32); | |
abcf1d52 | 2407 | memcpy (contents + rel->r_offset - 4, |
bffbf940 JJ |
2408 | "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0", |
2409 | 16); | |
2410 | ||
2411 | relocation = (htab->sgot->output_section->vma | |
2412 | + htab->sgot->output_offset + off | |
2413 | - rel->r_offset | |
2414 | - input_section->output_section->vma | |
2415 | - input_section->output_offset | |
abcf1d52 | 2416 | - 12); |
bffbf940 | 2417 | bfd_put_32 (output_bfd, relocation, |
abcf1d52 | 2418 | contents + rel->r_offset + 8); |
bffbf940 JJ |
2419 | /* Skip R_X86_64_PLT32. */ |
2420 | rel++; | |
2421 | continue; | |
2422 | } | |
2423 | break; | |
2424 | ||
2425 | case R_X86_64_TLSLD: | |
2426 | if (! info->shared) | |
2427 | { | |
2428 | /* LD->LE transition: | |
2429 | Ensure it is: | |
2430 | leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt. | |
2431 | We change it into: | |
2432 | .word 0x6666; .byte 0x66; movl %fs:0, %rax. */ | |
2433 | BFD_ASSERT (rel->r_offset >= 3); | |
2434 | BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3) | |
2435 | == 0x48); | |
2436 | BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2) | |
2437 | == 0x8d); | |
2438 | BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1) | |
2439 | == 0x3d); | |
eea6121a | 2440 | BFD_ASSERT (rel->r_offset + 9 <= input_section->size); |
bffbf940 JJ |
2441 | BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4) |
2442 | == 0xe8); | |
2443 | BFD_ASSERT (rel + 1 < relend); | |
2444 | BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32); | |
2445 | memcpy (contents + rel->r_offset - 3, | |
2446 | "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12); | |
2447 | /* Skip R_X86_64_PLT32. */ | |
2448 | rel++; | |
2449 | continue; | |
2450 | } | |
2451 | ||
2452 | if (htab->sgot == NULL) | |
2453 | abort (); | |
2454 | ||
2455 | off = htab->tls_ld_got.offset; | |
2456 | if (off & 1) | |
2457 | off &= ~1; | |
2458 | else | |
2459 | { | |
2460 | Elf_Internal_Rela outrel; | |
947216bf | 2461 | bfd_byte *loc; |
bffbf940 JJ |
2462 | |
2463 | if (htab->srelgot == NULL) | |
2464 | abort (); | |
2465 | ||
2466 | outrel.r_offset = (htab->sgot->output_section->vma | |
2467 | + htab->sgot->output_offset + off); | |
2468 | ||
2469 | bfd_put_64 (output_bfd, 0, | |
2470 | htab->sgot->contents + off); | |
2471 | bfd_put_64 (output_bfd, 0, | |
2472 | htab->sgot->contents + off + GOT_ENTRY_SIZE); | |
2473 | outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64); | |
2474 | outrel.r_addend = 0; | |
947216bf AM |
2475 | loc = htab->srelgot->contents; |
2476 | loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela); | |
bffbf940 JJ |
2477 | bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc); |
2478 | htab->tls_ld_got.offset |= 1; | |
2479 | } | |
2480 | relocation = htab->sgot->output_section->vma | |
2481 | + htab->sgot->output_offset + off; | |
b34976b6 | 2482 | unresolved_reloc = FALSE; |
bffbf940 JJ |
2483 | break; |
2484 | ||
2485 | case R_X86_64_DTPOFF32: | |
a45bb67d | 2486 | if (info->shared || (input_section->flags & SEC_CODE) == 0) |
bffbf940 JJ |
2487 | relocation -= dtpoff_base (info); |
2488 | else | |
2489 | relocation = tpoff (info, relocation); | |
2490 | break; | |
2491 | ||
2492 | case R_X86_64_TPOFF32: | |
2493 | BFD_ASSERT (! info->shared); | |
2494 | relocation = tpoff (info, relocation); | |
2495 | break; | |
2496 | ||
70256ad8 AJ |
2497 | default: |
2498 | break; | |
2499 | } | |
8d88c4ca | 2500 | |
239e1f3a AM |
2501 | /* Dynamic relocs are not propagated for SEC_DEBUGGING sections |
2502 | because such sections are not SEC_ALLOC and thus ld.so will | |
2503 | not process them. */ | |
c434dee6 | 2504 | if (unresolved_reloc |
239e1f3a | 2505 | && !((input_section->flags & SEC_DEBUGGING) != 0 |
f5385ebf | 2506 | && h->def_dynamic)) |
c434dee6 | 2507 | (*_bfd_error_handler) |
d003868e AM |
2508 | (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"), |
2509 | input_bfd, | |
2510 | input_section, | |
c434dee6 AJ |
2511 | (long) rel->r_offset, |
2512 | h->root.root.string); | |
2513 | ||
8d88c4ca | 2514 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
c434dee6 AJ |
2515 | contents, rel->r_offset, |
2516 | relocation, rel->r_addend); | |
8d88c4ca NC |
2517 | |
2518 | if (r != bfd_reloc_ok) | |
8da6118f | 2519 | { |
c434dee6 AJ |
2520 | const char *name; |
2521 | ||
2522 | if (h != NULL) | |
2523 | name = h->root.root.string; | |
2524 | else | |
8da6118f | 2525 | { |
c434dee6 AJ |
2526 | name = bfd_elf_string_from_elf_section (input_bfd, |
2527 | symtab_hdr->sh_link, | |
2528 | sym->st_name); | |
2529 | if (name == NULL) | |
b34976b6 | 2530 | return FALSE; |
c434dee6 AJ |
2531 | if (*name == '\0') |
2532 | name = bfd_section_name (input_bfd, sec); | |
2533 | } | |
2534 | ||
2535 | if (r == bfd_reloc_overflow) | |
2536 | { | |
3ffa5234 AM |
2537 | if (h != NULL |
2538 | && h->root.type == bfd_link_hash_undefweak | |
2539 | && howto->pc_relative) | |
2540 | /* Ignore reloc overflow on branches to undefweak syms. */ | |
2541 | continue; | |
c434dee6 AJ |
2542 | |
2543 | if (! ((*info->callbacks->reloc_overflow) | |
dfeffb9f L |
2544 | (info, (h ? &h->root : NULL), name, howto->name, |
2545 | (bfd_vma) 0, input_bfd, input_section, | |
2546 | rel->r_offset))) | |
b34976b6 | 2547 | return FALSE; |
c434dee6 AJ |
2548 | } |
2549 | else | |
2550 | { | |
2551 | (*_bfd_error_handler) | |
d003868e AM |
2552 | (_("%B(%A+0x%lx): reloc against `%s': error %d"), |
2553 | input_bfd, input_section, | |
c434dee6 | 2554 | (long) rel->r_offset, name, (int) r); |
b34976b6 | 2555 | return FALSE; |
8da6118f KH |
2556 | } |
2557 | } | |
8d88c4ca | 2558 | } |
70256ad8 | 2559 | |
b34976b6 | 2560 | return TRUE; |
70256ad8 AJ |
2561 | } |
2562 | ||
2563 | /* Finish up dynamic symbol handling. We set the contents of various | |
2564 | dynamic sections here. */ | |
2565 | ||
b34976b6 | 2566 | static bfd_boolean |
27482721 AJ |
2567 | elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd, |
2568 | struct bfd_link_info *info, | |
2569 | struct elf_link_hash_entry *h, | |
2570 | Elf_Internal_Sym *sym) | |
70256ad8 | 2571 | { |
c434dee6 | 2572 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 | 2573 | |
c434dee6 | 2574 | htab = elf64_x86_64_hash_table (info); |
70256ad8 AJ |
2575 | |
2576 | if (h->plt.offset != (bfd_vma) -1) | |
2577 | { | |
70256ad8 AJ |
2578 | bfd_vma plt_index; |
2579 | bfd_vma got_offset; | |
2580 | Elf_Internal_Rela rela; | |
947216bf | 2581 | bfd_byte *loc; |
70256ad8 AJ |
2582 | |
2583 | /* This symbol has an entry in the procedure linkage table. Set | |
407443a3 | 2584 | it up. */ |
c434dee6 AJ |
2585 | if (h->dynindx == -1 |
2586 | || htab->splt == NULL | |
2587 | || htab->sgotplt == NULL | |
2588 | || htab->srelplt == NULL) | |
2589 | abort (); | |
70256ad8 AJ |
2590 | |
2591 | /* Get the index in the procedure linkage table which | |
2592 | corresponds to this symbol. This is the index of this symbol | |
2593 | in all the symbols for which we are making plt entries. The | |
2594 | first entry in the procedure linkage table is reserved. */ | |
2595 | plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1; | |
2596 | ||
2597 | /* Get the offset into the .got table of the entry that | |
407443a3 | 2598 | corresponds to this function. Each .got entry is GOT_ENTRY_SIZE |
fe4770f4 | 2599 | bytes. The first three are reserved for the dynamic linker. */ |
70256ad8 AJ |
2600 | got_offset = (plt_index + 3) * GOT_ENTRY_SIZE; |
2601 | ||
2602 | /* Fill in the entry in the procedure linkage table. */ | |
c434dee6 | 2603 | memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry, |
70256ad8 AJ |
2604 | PLT_ENTRY_SIZE); |
2605 | ||
2606 | /* Insert the relocation positions of the plt section. The magic | |
2607 | numbers at the end of the statements are the positions of the | |
2608 | relocations in the plt section. */ | |
653165cc AJ |
2609 | /* Put offset for jmp *name@GOTPCREL(%rip), since the |
2610 | instruction uses 6 bytes, subtract this value. */ | |
2611 | bfd_put_32 (output_bfd, | |
c434dee6 AJ |
2612 | (htab->sgotplt->output_section->vma |
2613 | + htab->sgotplt->output_offset | |
653165cc | 2614 | + got_offset |
c434dee6 AJ |
2615 | - htab->splt->output_section->vma |
2616 | - htab->splt->output_offset | |
653165cc AJ |
2617 | - h->plt.offset |
2618 | - 6), | |
c434dee6 | 2619 | htab->splt->contents + h->plt.offset + 2); |
653165cc AJ |
2620 | /* Put relocation index. */ |
2621 | bfd_put_32 (output_bfd, plt_index, | |
c434dee6 | 2622 | htab->splt->contents + h->plt.offset + 7); |
653165cc AJ |
2623 | /* Put offset for jmp .PLT0. */ |
2624 | bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE), | |
c434dee6 | 2625 | htab->splt->contents + h->plt.offset + 12); |
70256ad8 | 2626 | |
653165cc AJ |
2627 | /* Fill in the entry in the global offset table, initially this |
2628 | points to the pushq instruction in the PLT which is at offset 6. */ | |
c434dee6 AJ |
2629 | bfd_put_64 (output_bfd, (htab->splt->output_section->vma |
2630 | + htab->splt->output_offset | |
70256ad8 | 2631 | + h->plt.offset + 6), |
c434dee6 | 2632 | htab->sgotplt->contents + got_offset); |
70256ad8 AJ |
2633 | |
2634 | /* Fill in the entry in the .rela.plt section. */ | |
c434dee6 AJ |
2635 | rela.r_offset = (htab->sgotplt->output_section->vma |
2636 | + htab->sgotplt->output_offset | |
70256ad8 AJ |
2637 | + got_offset); |
2638 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT); | |
2639 | rela.r_addend = 0; | |
947216bf | 2640 | loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela); |
c434dee6 | 2641 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); |
70256ad8 | 2642 | |
f5385ebf | 2643 | if (!h->def_regular) |
70256ad8 AJ |
2644 | { |
2645 | /* Mark the symbol as undefined, rather than as defined in | |
47a9f7b3 JJ |
2646 | the .plt section. Leave the value if there were any |
2647 | relocations where pointer equality matters (this is a clue | |
c434dee6 AJ |
2648 | for the dynamic linker, to make function pointer |
2649 | comparisons work between an application and shared | |
47a9f7b3 JJ |
2650 | library), otherwise set it to zero. If a function is only |
2651 | called from a binary, there is no need to slow down | |
2652 | shared libraries because of that. */ | |
70256ad8 | 2653 | sym->st_shndx = SHN_UNDEF; |
f5385ebf | 2654 | if (!h->pointer_equality_needed) |
47a9f7b3 | 2655 | sym->st_value = 0; |
70256ad8 AJ |
2656 | } |
2657 | } | |
2658 | ||
bffbf940 JJ |
2659 | if (h->got.offset != (bfd_vma) -1 |
2660 | && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD | |
2661 | && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE) | |
053579d7 | 2662 | { |
053579d7 | 2663 | Elf_Internal_Rela rela; |
947216bf | 2664 | bfd_byte *loc; |
053579d7 AJ |
2665 | |
2666 | /* This symbol has an entry in the global offset table. Set it | |
bffbf940 | 2667 | up. */ |
c434dee6 AJ |
2668 | if (htab->sgot == NULL || htab->srelgot == NULL) |
2669 | abort (); | |
053579d7 | 2670 | |
c434dee6 AJ |
2671 | rela.r_offset = (htab->sgot->output_section->vma |
2672 | + htab->sgot->output_offset | |
dc810e39 | 2673 | + (h->got.offset &~ (bfd_vma) 1)); |
053579d7 AJ |
2674 | |
2675 | /* If this is a static link, or it is a -Bsymbolic link and the | |
2676 | symbol is defined locally or was forced to be local because | |
2677 | of a version file, we just want to emit a RELATIVE reloc. | |
2678 | The entry in the global offset table will already have been | |
2679 | initialized in the relocate_section function. */ | |
c434dee6 | 2680 | if (info->shared |
27482721 | 2681 | && SYMBOL_REFERENCES_LOCAL (info, h)) |
053579d7 | 2682 | { |
cc78d0af | 2683 | BFD_ASSERT((h->got.offset & 1) != 0); |
053579d7 AJ |
2684 | rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE); |
2685 | rela.r_addend = (h->root.u.def.value | |
2686 | + h->root.u.def.section->output_section->vma | |
2687 | + h->root.u.def.section->output_offset); | |
2688 | } | |
2689 | else | |
2690 | { | |
2691 | BFD_ASSERT((h->got.offset & 1) == 0); | |
c434dee6 AJ |
2692 | bfd_put_64 (output_bfd, (bfd_vma) 0, |
2693 | htab->sgot->contents + h->got.offset); | |
053579d7 AJ |
2694 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT); |
2695 | rela.r_addend = 0; | |
2696 | } | |
2697 | ||
947216bf AM |
2698 | loc = htab->srelgot->contents; |
2699 | loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela); | |
c434dee6 | 2700 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); |
053579d7 AJ |
2701 | } |
2702 | ||
f5385ebf | 2703 | if (h->needs_copy) |
70256ad8 | 2704 | { |
70256ad8 | 2705 | Elf_Internal_Rela rela; |
947216bf | 2706 | bfd_byte *loc; |
70256ad8 AJ |
2707 | |
2708 | /* This symbol needs a copy reloc. Set it up. */ | |
2709 | ||
c434dee6 AJ |
2710 | if (h->dynindx == -1 |
2711 | || (h->root.type != bfd_link_hash_defined | |
2712 | && h->root.type != bfd_link_hash_defweak) | |
2713 | || htab->srelbss == NULL) | |
2714 | abort (); | |
70256ad8 AJ |
2715 | |
2716 | rela.r_offset = (h->root.u.def.value | |
2717 | + h->root.u.def.section->output_section->vma | |
2718 | + h->root.u.def.section->output_offset); | |
2719 | rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY); | |
2720 | rela.r_addend = 0; | |
947216bf AM |
2721 | loc = htab->srelbss->contents; |
2722 | loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela); | |
c434dee6 | 2723 | bfd_elf64_swap_reloca_out (output_bfd, &rela, loc); |
70256ad8 AJ |
2724 | } |
2725 | ||
2726 | /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ | |
2727 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
2728 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
2729 | sym->st_shndx = SHN_ABS; | |
2730 | ||
b34976b6 | 2731 | return TRUE; |
70256ad8 AJ |
2732 | } |
2733 | ||
c434dee6 AJ |
2734 | /* Used to decide how to sort relocs in an optimal manner for the |
2735 | dynamic linker, before writing them out. */ | |
2736 | ||
2737 | static enum elf_reloc_type_class | |
27482721 | 2738 | elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela) |
c434dee6 AJ |
2739 | { |
2740 | switch ((int) ELF64_R_TYPE (rela->r_info)) | |
2741 | { | |
2742 | case R_X86_64_RELATIVE: | |
2743 | return reloc_class_relative; | |
2744 | case R_X86_64_JUMP_SLOT: | |
2745 | return reloc_class_plt; | |
2746 | case R_X86_64_COPY: | |
2747 | return reloc_class_copy; | |
2748 | default: | |
2749 | return reloc_class_normal; | |
2750 | } | |
2751 | } | |
2752 | ||
70256ad8 AJ |
2753 | /* Finish up the dynamic sections. */ |
2754 | ||
b34976b6 | 2755 | static bfd_boolean |
27482721 | 2756 | elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) |
70256ad8 | 2757 | { |
c434dee6 | 2758 | struct elf64_x86_64_link_hash_table *htab; |
70256ad8 AJ |
2759 | bfd *dynobj; |
2760 | asection *sdyn; | |
70256ad8 | 2761 | |
c434dee6 AJ |
2762 | htab = elf64_x86_64_hash_table (info); |
2763 | dynobj = htab->elf.dynobj; | |
70256ad8 AJ |
2764 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); |
2765 | ||
c434dee6 | 2766 | if (htab->elf.dynamic_sections_created) |
70256ad8 | 2767 | { |
70256ad8 AJ |
2768 | Elf64_External_Dyn *dyncon, *dynconend; |
2769 | ||
c434dee6 AJ |
2770 | if (sdyn == NULL || htab->sgot == NULL) |
2771 | abort (); | |
70256ad8 AJ |
2772 | |
2773 | dyncon = (Elf64_External_Dyn *) sdyn->contents; | |
eea6121a | 2774 | dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size); |
70256ad8 AJ |
2775 | for (; dyncon < dynconend; dyncon++) |
2776 | { | |
2777 | Elf_Internal_Dyn dyn; | |
70256ad8 AJ |
2778 | asection *s; |
2779 | ||
2780 | bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn); | |
2781 | ||
2782 | switch (dyn.d_tag) | |
2783 | { | |
2784 | default: | |
053579d7 | 2785 | continue; |
70256ad8 AJ |
2786 | |
2787 | case DT_PLTGOT: | |
8c37241b JJ |
2788 | s = htab->sgotplt; |
2789 | dyn.d_un.d_ptr = s->output_section->vma + s->output_offset; | |
c434dee6 | 2790 | break; |
70256ad8 AJ |
2791 | |
2792 | case DT_JMPREL: | |
c434dee6 AJ |
2793 | dyn.d_un.d_ptr = htab->srelplt->output_section->vma; |
2794 | break; | |
70256ad8 | 2795 | |
c434dee6 AJ |
2796 | case DT_PLTRELSZ: |
2797 | s = htab->srelplt->output_section; | |
eea6121a | 2798 | dyn.d_un.d_val = s->size; |
70256ad8 AJ |
2799 | break; |
2800 | ||
2801 | case DT_RELASZ: | |
c434dee6 AJ |
2802 | /* The procedure linkage table relocs (DT_JMPREL) should |
2803 | not be included in the overall relocs (DT_RELA). | |
2804 | Therefore, we override the DT_RELASZ entry here to | |
2805 | make it not include the JMPREL relocs. Since the | |
2806 | linker script arranges for .rela.plt to follow all | |
2807 | other relocation sections, we don't have to worry | |
2808 | about changing the DT_RELA entry. */ | |
2809 | if (htab->srelplt != NULL) | |
70256ad8 | 2810 | { |
c434dee6 | 2811 | s = htab->srelplt->output_section; |
eea6121a | 2812 | dyn.d_un.d_val -= s->size; |
70256ad8 AJ |
2813 | } |
2814 | break; | |
70256ad8 | 2815 | } |
c434dee6 | 2816 | |
70256ad8 AJ |
2817 | bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon); |
2818 | } | |
2819 | ||
c434dee6 | 2820 | /* Fill in the special first entry in the procedure linkage table. */ |
eea6121a | 2821 | if (htab->splt && htab->splt->size > 0) |
70256ad8 | 2822 | { |
653165cc | 2823 | /* Fill in the first entry in the procedure linkage table. */ |
c434dee6 AJ |
2824 | memcpy (htab->splt->contents, elf64_x86_64_plt0_entry, |
2825 | PLT_ENTRY_SIZE); | |
653165cc AJ |
2826 | /* Add offset for pushq GOT+8(%rip), since the instruction |
2827 | uses 6 bytes subtract this value. */ | |
2828 | bfd_put_32 (output_bfd, | |
c434dee6 AJ |
2829 | (htab->sgotplt->output_section->vma |
2830 | + htab->sgotplt->output_offset | |
653165cc | 2831 | + 8 |
c434dee6 AJ |
2832 | - htab->splt->output_section->vma |
2833 | - htab->splt->output_offset | |
653165cc | 2834 | - 6), |
c434dee6 | 2835 | htab->splt->contents + 2); |
653165cc AJ |
2836 | /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to |
2837 | the end of the instruction. */ | |
2838 | bfd_put_32 (output_bfd, | |
c434dee6 AJ |
2839 | (htab->sgotplt->output_section->vma |
2840 | + htab->sgotplt->output_offset | |
653165cc | 2841 | + 16 |
c434dee6 AJ |
2842 | - htab->splt->output_section->vma |
2843 | - htab->splt->output_offset | |
653165cc | 2844 | - 12), |
c434dee6 | 2845 | htab->splt->contents + 8); |
653165cc | 2846 | |
c434dee6 AJ |
2847 | elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize = |
2848 | PLT_ENTRY_SIZE; | |
70256ad8 | 2849 | } |
70256ad8 AJ |
2850 | } |
2851 | ||
c434dee6 | 2852 | if (htab->sgotplt) |
70256ad8 | 2853 | { |
c434dee6 | 2854 | /* Fill in the first three entries in the global offset table. */ |
eea6121a | 2855 | if (htab->sgotplt->size > 0) |
c434dee6 AJ |
2856 | { |
2857 | /* Set the first entry in the global offset table to the address of | |
2858 | the dynamic section. */ | |
2859 | if (sdyn == NULL) | |
2860 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents); | |
2861 | else | |
2862 | bfd_put_64 (output_bfd, | |
2863 | sdyn->output_section->vma + sdyn->output_offset, | |
2864 | htab->sgotplt->contents); | |
2865 | /* Write GOT[1] and GOT[2], needed for the dynamic linker. */ | |
2866 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE); | |
2867 | bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2); | |
2868 | } | |
70256ad8 | 2869 | |
c434dee6 AJ |
2870 | elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = |
2871 | GOT_ENTRY_SIZE; | |
2872 | } | |
70256ad8 | 2873 | |
eea6121a | 2874 | if (htab->sgot && htab->sgot->size > 0) |
8c37241b JJ |
2875 | elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize |
2876 | = GOT_ENTRY_SIZE; | |
2877 | ||
b34976b6 | 2878 | return TRUE; |
8d88c4ca NC |
2879 | } |
2880 | ||
4c45e5c9 JJ |
2881 | /* Return address for Ith PLT stub in section PLT, for relocation REL |
2882 | or (bfd_vma) -1 if it should not be included. */ | |
2883 | ||
2884 | static bfd_vma | |
2885 | elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt, | |
2886 | const arelent *rel ATTRIBUTE_UNUSED) | |
2887 | { | |
2888 | return plt->vma + (i + 1) * PLT_ENTRY_SIZE; | |
2889 | } | |
8df9fc9d | 2890 | |
d2b2c203 DJ |
2891 | /* Handle an x86-64 specific section when reading an object file. This |
2892 | is called when elfcode.h finds a section with an unknown type. */ | |
2893 | ||
2894 | static bfd_boolean | |
6dc132d9 L |
2895 | elf64_x86_64_section_from_shdr (bfd *abfd, |
2896 | Elf_Internal_Shdr *hdr, | |
2897 | const char *name, | |
2898 | int shindex) | |
d2b2c203 DJ |
2899 | { |
2900 | if (hdr->sh_type != SHT_X86_64_UNWIND) | |
2901 | return FALSE; | |
2902 | ||
6dc132d9 | 2903 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
d2b2c203 DJ |
2904 | return FALSE; |
2905 | ||
2906 | return TRUE; | |
2907 | } | |
2908 | ||
70256ad8 AJ |
2909 | #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec |
2910 | #define TARGET_LITTLE_NAME "elf64-x86-64" | |
2911 | #define ELF_ARCH bfd_arch_i386 | |
2912 | #define ELF_MACHINE_CODE EM_X86_64 | |
2913 | #define ELF_MAXPAGESIZE 0x100000 | |
2914 | ||
2915 | #define elf_backend_can_gc_sections 1 | |
51b64d56 | 2916 | #define elf_backend_can_refcount 1 |
70256ad8 AJ |
2917 | #define elf_backend_want_got_plt 1 |
2918 | #define elf_backend_plt_readonly 1 | |
2919 | #define elf_backend_want_plt_sym 0 | |
2920 | #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3) | |
b491616a | 2921 | #define elf_backend_rela_normal 1 |
70256ad8 AJ |
2922 | |
2923 | #define elf_info_to_howto elf64_x86_64_info_to_howto | |
70256ad8 | 2924 | |
70256ad8 AJ |
2925 | #define bfd_elf64_bfd_link_hash_table_create \ |
2926 | elf64_x86_64_link_hash_table_create | |
407443a3 | 2927 | #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup |
70256ad8 AJ |
2928 | |
2929 | #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol | |
2930 | #define elf_backend_check_relocs elf64_x86_64_check_relocs | |
c434dee6 AJ |
2931 | #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol |
2932 | #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections | |
2933 | #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections | |
70256ad8 AJ |
2934 | #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol |
2935 | #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook | |
2936 | #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook | |
3bab7989 ML |
2937 | #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus |
2938 | #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo | |
c434dee6 | 2939 | #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class |
70256ad8 AJ |
2940 | #define elf_backend_relocate_section elf64_x86_64_relocate_section |
2941 | #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections | |
4c45e5c9 | 2942 | #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val |
407443a3 | 2943 | #define elf_backend_object_p elf64_x86_64_elf_object_p |
bffbf940 | 2944 | #define bfd_elf64_mkobject elf64_x86_64_mkobject |
8d88c4ca | 2945 | |
d2b2c203 DJ |
2946 | #define elf_backend_section_from_shdr \ |
2947 | elf64_x86_64_section_from_shdr | |
2948 | ||
8d88c4ca | 2949 | #include "elf64-target.h" |