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