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