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