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