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