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