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