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