Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Intel 80386/80486-specific support for 32-bit ELF |
2571583a | 2 | Copyright (C) 1993-2017 Free Software Foundation, Inc. |
252b5132 | 3 | |
571fe01f | 4 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 5 | |
571fe01f NC |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 8 | the Free Software Foundation; either version 3 of the License, or |
571fe01f | 9 | (at your option) any later version. |
252b5132 | 10 | |
571fe01f NC |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
252b5132 | 15 | |
571fe01f NC |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | MA 02110-1301, USA. */ | |
252b5132 | 20 | |
0afcef53 | 21 | #include "elfxx-x86.h" |
5a68afcf | 22 | #include "elf-nacl.h" |
eac338cf | 23 | #include "elf-vxworks.h" |
e41b3a13 | 24 | #include "dwarf2.h" |
02a86693 | 25 | #include "opcode/i386.h" |
252b5132 | 26 | |
55fd94b0 AM |
27 | /* 386 uses REL relocations instead of RELA. */ |
28 | #define USE_REL 1 | |
252b5132 RH |
29 | |
30 | #include "elf/i386.h" | |
31 | ||
32 | static reloc_howto_type elf_howto_table[]= | |
33 | { | |
6346d5ca | 34 | HOWTO(R_386_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont, |
1b452ec6 | 35 | bfd_elf_generic_reloc, "R_386_NONE", |
b34976b6 AM |
36 | TRUE, 0x00000000, 0x00000000, FALSE), |
37 | HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
1b452ec6 | 38 | bfd_elf_generic_reloc, "R_386_32", |
b34976b6 AM |
39 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
40 | HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield, | |
1b452ec6 | 41 | bfd_elf_generic_reloc, "R_386_PC32", |
b34976b6 AM |
42 | TRUE, 0xffffffff, 0xffffffff, TRUE), |
43 | HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
1b452ec6 | 44 | bfd_elf_generic_reloc, "R_386_GOT32", |
b34976b6 AM |
45 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
46 | HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield, | |
1b452ec6 | 47 | bfd_elf_generic_reloc, "R_386_PLT32", |
b34976b6 AM |
48 | TRUE, 0xffffffff, 0xffffffff, TRUE), |
49 | HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
1b452ec6 | 50 | bfd_elf_generic_reloc, "R_386_COPY", |
b34976b6 AM |
51 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
52 | HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
1b452ec6 | 53 | bfd_elf_generic_reloc, "R_386_GLOB_DAT", |
b34976b6 AM |
54 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
55 | HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
1b452ec6 | 56 | bfd_elf_generic_reloc, "R_386_JUMP_SLOT", |
b34976b6 AM |
57 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
58 | HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
1b452ec6 | 59 | bfd_elf_generic_reloc, "R_386_RELATIVE", |
b34976b6 AM |
60 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
61 | HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
1b452ec6 | 62 | bfd_elf_generic_reloc, "R_386_GOTOFF", |
b34976b6 AM |
63 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
64 | HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield, | |
1b452ec6 | 65 | bfd_elf_generic_reloc, "R_386_GOTPC", |
b34976b6 | 66 | TRUE, 0xffffffff, 0xffffffff, TRUE), |
1b452ec6 | 67 | |
dc47f327 AM |
68 | /* We have a gap in the reloc numbers here. |
69 | R_386_standard counts the number up to this point, and | |
70 | R_386_ext_offset is the value to subtract from a reloc type of | |
71 | R_386_16 thru R_386_PC8 to form an index into this table. */ | |
55fd94b0 AM |
72 | #define R_386_standard (R_386_GOTPC + 1) |
73 | #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard) | |
1b452ec6 | 74 | |
37e55690 | 75 | /* These relocs are a GNU extension. */ |
b34976b6 | 76 | HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, |
37e55690 | 77 | bfd_elf_generic_reloc, "R_386_TLS_TPOFF", |
b34976b6 AM |
78 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
79 | HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
37e55690 | 80 | bfd_elf_generic_reloc, "R_386_TLS_IE", |
b34976b6 AM |
81 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
82 | HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
37e55690 | 83 | bfd_elf_generic_reloc, "R_386_TLS_GOTIE", |
b34976b6 AM |
84 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
85 | HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
13ae64f3 | 86 | bfd_elf_generic_reloc, "R_386_TLS_LE", |
b34976b6 AM |
87 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
88 | HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
13ae64f3 | 89 | bfd_elf_generic_reloc, "R_386_TLS_GD", |
b34976b6 AM |
90 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
91 | HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
13ae64f3 | 92 | bfd_elf_generic_reloc, "R_386_TLS_LDM", |
b34976b6 AM |
93 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
94 | HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, | |
1b452ec6 | 95 | bfd_elf_generic_reloc, "R_386_16", |
b34976b6 | 96 | TRUE, 0xffff, 0xffff, FALSE), |
b0360d8c | 97 | HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield, |
1b452ec6 | 98 | bfd_elf_generic_reloc, "R_386_PC16", |
b34976b6 AM |
99 | TRUE, 0xffff, 0xffff, TRUE), |
100 | HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, | |
1b452ec6 | 101 | bfd_elf_generic_reloc, "R_386_8", |
b34976b6 AM |
102 | TRUE, 0xff, 0xff, FALSE), |
103 | HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, | |
1b452ec6 | 104 | bfd_elf_generic_reloc, "R_386_PC8", |
b34976b6 | 105 | TRUE, 0xff, 0xff, TRUE), |
dc47f327 | 106 | |
55fd94b0 AM |
107 | #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset) |
108 | #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext) | |
13ae64f3 | 109 | /* These are common with Solaris TLS implementation. */ |
b34976b6 | 110 | HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, |
13ae64f3 | 111 | bfd_elf_generic_reloc, "R_386_TLS_LDO_32", |
b34976b6 AM |
112 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
113 | HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
13ae64f3 | 114 | bfd_elf_generic_reloc, "R_386_TLS_IE_32", |
b34976b6 AM |
115 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
116 | HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
13ae64f3 | 117 | bfd_elf_generic_reloc, "R_386_TLS_LE_32", |
b34976b6 AM |
118 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
119 | HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
13ae64f3 | 120 | bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32", |
b34976b6 AM |
121 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
122 | HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
13ae64f3 | 123 | bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32", |
b34976b6 AM |
124 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
125 | HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
13ae64f3 | 126 | bfd_elf_generic_reloc, "R_386_TLS_TPOFF32", |
b34976b6 | 127 | TRUE, 0xffffffff, 0xffffffff, FALSE), |
1788fc08 L |
128 | HOWTO(R_386_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned, |
129 | bfd_elf_generic_reloc, "R_386_SIZE32", | |
130 | TRUE, 0xffffffff, 0xffffffff, FALSE), | |
67a4f2b7 AO |
131 | HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, |
132 | bfd_elf_generic_reloc, "R_386_TLS_GOTDESC", | |
133 | TRUE, 0xffffffff, 0xffffffff, FALSE), | |
134 | HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont, | |
135 | bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL", | |
136 | FALSE, 0, 0, FALSE), | |
137 | HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, | |
138 | bfd_elf_generic_reloc, "R_386_TLS_DESC", | |
139 | TRUE, 0xffffffff, 0xffffffff, FALSE), | |
cbe950e9 L |
140 | HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, |
141 | bfd_elf_generic_reloc, "R_386_IRELATIVE", | |
142 | TRUE, 0xffffffff, 0xffffffff, FALSE), | |
02a86693 L |
143 | HOWTO(R_386_GOT32X, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, |
144 | bfd_elf_generic_reloc, "R_386_GOT32X", | |
145 | TRUE, 0xffffffff, 0xffffffff, FALSE), | |
13ae64f3 JJ |
146 | |
147 | /* Another gap. */ | |
02a86693 | 148 | #define R_386_ext2 (R_386_GOT32X + 1 - R_386_tls_offset) |
c74be520 | 149 | #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_ext2) |
252b5132 RH |
150 | |
151 | /* GNU extension to record C++ vtable hierarchy. */ | |
252b5132 RH |
152 | HOWTO (R_386_GNU_VTINHERIT, /* type */ |
153 | 0, /* rightshift */ | |
154 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
155 | 0, /* bitsize */ | |
b34976b6 | 156 | FALSE, /* pc_relative */ |
252b5132 RH |
157 | 0, /* bitpos */ |
158 | complain_overflow_dont, /* complain_on_overflow */ | |
159 | NULL, /* special_function */ | |
160 | "R_386_GNU_VTINHERIT", /* name */ | |
b34976b6 | 161 | FALSE, /* partial_inplace */ |
252b5132 RH |
162 | 0, /* src_mask */ |
163 | 0, /* dst_mask */ | |
b34976b6 | 164 | FALSE), /* pcrel_offset */ |
252b5132 RH |
165 | |
166 | /* GNU extension to record C++ vtable member usage. */ | |
252b5132 RH |
167 | HOWTO (R_386_GNU_VTENTRY, /* type */ |
168 | 0, /* rightshift */ | |
169 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
170 | 0, /* bitsize */ | |
b34976b6 | 171 | FALSE, /* pc_relative */ |
252b5132 RH |
172 | 0, /* bitpos */ |
173 | complain_overflow_dont, /* complain_on_overflow */ | |
174 | _bfd_elf_rel_vtable_reloc_fn, /* special_function */ | |
175 | "R_386_GNU_VTENTRY", /* name */ | |
b34976b6 | 176 | FALSE, /* partial_inplace */ |
252b5132 RH |
177 | 0, /* src_mask */ |
178 | 0, /* dst_mask */ | |
b34976b6 | 179 | FALSE) /* pcrel_offset */ |
dc47f327 | 180 | |
55fd94b0 | 181 | #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset) |
dc47f327 AM |
182 | |
183 | }; | |
184 | ||
252b5132 | 185 | #ifdef DEBUG_GEN_RELOC |
55fd94b0 AM |
186 | #define TRACE(str) \ |
187 | fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str) | |
252b5132 RH |
188 | #else |
189 | #define TRACE(str) | |
190 | #endif | |
191 | ||
192 | static reloc_howto_type * | |
55fd94b0 AM |
193 | elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
194 | bfd_reloc_code_real_type code) | |
252b5132 RH |
195 | { |
196 | switch (code) | |
197 | { | |
198 | case BFD_RELOC_NONE: | |
199 | TRACE ("BFD_RELOC_NONE"); | |
55fd94b0 | 200 | return &elf_howto_table[R_386_NONE]; |
252b5132 RH |
201 | |
202 | case BFD_RELOC_32: | |
203 | TRACE ("BFD_RELOC_32"); | |
55fd94b0 | 204 | return &elf_howto_table[R_386_32]; |
252b5132 RH |
205 | |
206 | case BFD_RELOC_CTOR: | |
207 | TRACE ("BFD_RELOC_CTOR"); | |
55fd94b0 | 208 | return &elf_howto_table[R_386_32]; |
252b5132 RH |
209 | |
210 | case BFD_RELOC_32_PCREL: | |
211 | TRACE ("BFD_RELOC_PC32"); | |
55fd94b0 | 212 | return &elf_howto_table[R_386_PC32]; |
252b5132 RH |
213 | |
214 | case BFD_RELOC_386_GOT32: | |
215 | TRACE ("BFD_RELOC_386_GOT32"); | |
55fd94b0 | 216 | return &elf_howto_table[R_386_GOT32]; |
252b5132 RH |
217 | |
218 | case BFD_RELOC_386_PLT32: | |
219 | TRACE ("BFD_RELOC_386_PLT32"); | |
55fd94b0 | 220 | return &elf_howto_table[R_386_PLT32]; |
252b5132 RH |
221 | |
222 | case BFD_RELOC_386_COPY: | |
223 | TRACE ("BFD_RELOC_386_COPY"); | |
55fd94b0 | 224 | return &elf_howto_table[R_386_COPY]; |
252b5132 RH |
225 | |
226 | case BFD_RELOC_386_GLOB_DAT: | |
227 | TRACE ("BFD_RELOC_386_GLOB_DAT"); | |
55fd94b0 | 228 | return &elf_howto_table[R_386_GLOB_DAT]; |
252b5132 RH |
229 | |
230 | case BFD_RELOC_386_JUMP_SLOT: | |
231 | TRACE ("BFD_RELOC_386_JUMP_SLOT"); | |
55fd94b0 | 232 | return &elf_howto_table[R_386_JUMP_SLOT]; |
252b5132 RH |
233 | |
234 | case BFD_RELOC_386_RELATIVE: | |
235 | TRACE ("BFD_RELOC_386_RELATIVE"); | |
55fd94b0 | 236 | return &elf_howto_table[R_386_RELATIVE]; |
252b5132 RH |
237 | |
238 | case BFD_RELOC_386_GOTOFF: | |
239 | TRACE ("BFD_RELOC_386_GOTOFF"); | |
55fd94b0 | 240 | return &elf_howto_table[R_386_GOTOFF]; |
252b5132 RH |
241 | |
242 | case BFD_RELOC_386_GOTPC: | |
243 | TRACE ("BFD_RELOC_386_GOTPC"); | |
55fd94b0 | 244 | return &elf_howto_table[R_386_GOTPC]; |
252b5132 | 245 | |
37e55690 JJ |
246 | /* These relocs are a GNU extension. */ |
247 | case BFD_RELOC_386_TLS_TPOFF: | |
248 | TRACE ("BFD_RELOC_386_TLS_TPOFF"); | |
55fd94b0 | 249 | return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset]; |
37e55690 JJ |
250 | |
251 | case BFD_RELOC_386_TLS_IE: | |
252 | TRACE ("BFD_RELOC_386_TLS_IE"); | |
55fd94b0 | 253 | return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset]; |
37e55690 JJ |
254 | |
255 | case BFD_RELOC_386_TLS_GOTIE: | |
256 | TRACE ("BFD_RELOC_386_TLS_GOTIE"); | |
55fd94b0 | 257 | return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset]; |
37e55690 | 258 | |
13ae64f3 JJ |
259 | case BFD_RELOC_386_TLS_LE: |
260 | TRACE ("BFD_RELOC_386_TLS_LE"); | |
55fd94b0 | 261 | return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset]; |
13ae64f3 JJ |
262 | |
263 | case BFD_RELOC_386_TLS_GD: | |
264 | TRACE ("BFD_RELOC_386_TLS_GD"); | |
55fd94b0 | 265 | return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset]; |
13ae64f3 JJ |
266 | |
267 | case BFD_RELOC_386_TLS_LDM: | |
268 | TRACE ("BFD_RELOC_386_TLS_LDM"); | |
55fd94b0 | 269 | return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset]; |
13ae64f3 | 270 | |
252b5132 RH |
271 | case BFD_RELOC_16: |
272 | TRACE ("BFD_RELOC_16"); | |
55fd94b0 | 273 | return &elf_howto_table[R_386_16 - R_386_ext_offset]; |
252b5132 RH |
274 | |
275 | case BFD_RELOC_16_PCREL: | |
276 | TRACE ("BFD_RELOC_16_PCREL"); | |
55fd94b0 | 277 | return &elf_howto_table[R_386_PC16 - R_386_ext_offset]; |
252b5132 RH |
278 | |
279 | case BFD_RELOC_8: | |
280 | TRACE ("BFD_RELOC_8"); | |
55fd94b0 | 281 | return &elf_howto_table[R_386_8 - R_386_ext_offset]; |
252b5132 RH |
282 | |
283 | case BFD_RELOC_8_PCREL: | |
284 | TRACE ("BFD_RELOC_8_PCREL"); | |
55fd94b0 | 285 | return &elf_howto_table[R_386_PC8 - R_386_ext_offset]; |
252b5132 | 286 | |
13ae64f3 JJ |
287 | /* Common with Sun TLS implementation. */ |
288 | case BFD_RELOC_386_TLS_LDO_32: | |
289 | TRACE ("BFD_RELOC_386_TLS_LDO_32"); | |
55fd94b0 | 290 | return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset]; |
13ae64f3 JJ |
291 | |
292 | case BFD_RELOC_386_TLS_IE_32: | |
293 | TRACE ("BFD_RELOC_386_TLS_IE_32"); | |
55fd94b0 | 294 | return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset]; |
13ae64f3 JJ |
295 | |
296 | case BFD_RELOC_386_TLS_LE_32: | |
297 | TRACE ("BFD_RELOC_386_TLS_LE_32"); | |
55fd94b0 | 298 | return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset]; |
13ae64f3 JJ |
299 | |
300 | case BFD_RELOC_386_TLS_DTPMOD32: | |
301 | TRACE ("BFD_RELOC_386_TLS_DTPMOD32"); | |
55fd94b0 | 302 | return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset]; |
13ae64f3 JJ |
303 | |
304 | case BFD_RELOC_386_TLS_DTPOFF32: | |
305 | TRACE ("BFD_RELOC_386_TLS_DTPOFF32"); | |
55fd94b0 | 306 | return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset]; |
13ae64f3 JJ |
307 | |
308 | case BFD_RELOC_386_TLS_TPOFF32: | |
309 | TRACE ("BFD_RELOC_386_TLS_TPOFF32"); | |
55fd94b0 | 310 | return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset]; |
13ae64f3 | 311 | |
1788fc08 L |
312 | case BFD_RELOC_SIZE32: |
313 | TRACE ("BFD_RELOC_SIZE32"); | |
314 | return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset]; | |
315 | ||
67a4f2b7 AO |
316 | case BFD_RELOC_386_TLS_GOTDESC: |
317 | TRACE ("BFD_RELOC_386_TLS_GOTDESC"); | |
318 | return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset]; | |
319 | ||
320 | case BFD_RELOC_386_TLS_DESC_CALL: | |
321 | TRACE ("BFD_RELOC_386_TLS_DESC_CALL"); | |
322 | return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset]; | |
323 | ||
324 | case BFD_RELOC_386_TLS_DESC: | |
325 | TRACE ("BFD_RELOC_386_TLS_DESC"); | |
326 | return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset]; | |
327 | ||
cbe950e9 L |
328 | case BFD_RELOC_386_IRELATIVE: |
329 | TRACE ("BFD_RELOC_386_IRELATIVE"); | |
2a750708 | 330 | return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset]; |
cbe950e9 | 331 | |
02a86693 L |
332 | case BFD_RELOC_386_GOT32X: |
333 | TRACE ("BFD_RELOC_386_GOT32X"); | |
334 | return &elf_howto_table[R_386_GOT32X - R_386_tls_offset]; | |
335 | ||
252b5132 RH |
336 | case BFD_RELOC_VTABLE_INHERIT: |
337 | TRACE ("BFD_RELOC_VTABLE_INHERIT"); | |
55fd94b0 | 338 | return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset]; |
252b5132 RH |
339 | |
340 | case BFD_RELOC_VTABLE_ENTRY: | |
341 | TRACE ("BFD_RELOC_VTABLE_ENTRY"); | |
55fd94b0 | 342 | return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset]; |
252b5132 RH |
343 | |
344 | default: | |
345 | break; | |
346 | } | |
347 | ||
348 | TRACE ("Unknown"); | |
349 | return 0; | |
350 | } | |
351 | ||
157090f7 AM |
352 | static reloc_howto_type * |
353 | elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
354 | const char *r_name) | |
355 | { | |
356 | unsigned int i; | |
357 | ||
358 | for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++) | |
359 | if (elf_howto_table[i].name != NULL | |
360 | && strcasecmp (elf_howto_table[i].name, r_name) == 0) | |
361 | return &elf_howto_table[i]; | |
362 | ||
363 | return NULL; | |
364 | } | |
365 | ||
142411ca L |
366 | static reloc_howto_type * |
367 | elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type) | |
252b5132 | 368 | { |
dc47f327 AM |
369 | unsigned int indx; |
370 | ||
371 | if ((indx = r_type) >= R_386_standard | |
372 | && ((indx = r_type - R_386_ext_offset) - R_386_standard | |
373 | >= R_386_ext - R_386_standard) | |
13ae64f3 | 374 | && ((indx = r_type - R_386_tls_offset) - R_386_ext |
c74be520 L |
375 | >= R_386_ext2 - R_386_ext) |
376 | && ((indx = r_type - R_386_vt_offset) - R_386_ext2 | |
377 | >= R_386_vt - R_386_ext2)) | |
252b5132 | 378 | { |
695344c0 | 379 | /* xgettext:c-format */ |
4eca0228 AM |
380 | _bfd_error_handler (_("%B: invalid relocation type %d"), |
381 | abfd, (int) r_type); | |
55fd94b0 | 382 | indx = R_386_NONE; |
252b5132 | 383 | } |
06614111 NC |
384 | /* PR 17512: file: 0f67f69d. */ |
385 | if (elf_howto_table [indx].type != r_type) | |
386 | return NULL; | |
142411ca L |
387 | return &elf_howto_table[indx]; |
388 | } | |
389 | ||
390 | static void | |
391 | elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, | |
392 | arelent *cache_ptr, | |
393 | Elf_Internal_Rela *dst) | |
394 | { | |
395 | unsigned int r_type = ELF32_R_TYPE (dst->r_info); | |
396 | cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type); | |
252b5132 RH |
397 | } |
398 | ||
399 | /* Return whether a symbol name implies a local label. The UnixWare | |
400 | 2.1 cc generates temporary symbols that start with .X, so we | |
401 | recognize them here. FIXME: do other SVR4 compilers also use .X?. | |
402 | If so, we should move the .X recognition into | |
403 | _bfd_elf_is_local_label_name. */ | |
404 | ||
b34976b6 | 405 | static bfd_boolean |
55fd94b0 | 406 | elf_i386_is_local_label_name (bfd *abfd, const char *name) |
252b5132 RH |
407 | { |
408 | if (name[0] == '.' && name[1] == 'X') | |
b34976b6 | 409 | return TRUE; |
252b5132 RH |
410 | |
411 | return _bfd_elf_is_local_label_name (abfd, name); | |
412 | } | |
413 | \f | |
38701953 | 414 | /* Support for core dump NOTE sections. */ |
61adc1a4 | 415 | |
b34976b6 | 416 | static bfd_boolean |
55fd94b0 | 417 | elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
38701953 AM |
418 | { |
419 | int offset; | |
eea6121a | 420 | size_t size; |
38701953 | 421 | |
61adc1a4 | 422 | if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0) |
38701953 | 423 | { |
61adc1a4 NC |
424 | int pr_version = bfd_get_32 (abfd, note->descdata); |
425 | ||
426 | if (pr_version != 1) | |
427 | return FALSE; | |
428 | ||
429 | /* pr_cursig */ | |
228e534f | 430 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20); |
61adc1a4 NC |
431 | |
432 | /* pr_pid */ | |
228e534f | 433 | elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24); |
61adc1a4 NC |
434 | |
435 | /* pr_reg */ | |
436 | offset = 28; | |
eea6121a | 437 | size = bfd_get_32 (abfd, note->descdata + 8); |
61adc1a4 NC |
438 | } |
439 | else | |
440 | { | |
441 | switch (note->descsz) | |
442 | { | |
443 | default: | |
444 | return FALSE; | |
38701953 | 445 | |
61adc1a4 NC |
446 | case 144: /* Linux/i386 */ |
447 | /* pr_cursig */ | |
228e534f | 448 | elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12); |
38701953 | 449 | |
61adc1a4 | 450 | /* pr_pid */ |
228e534f | 451 | elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24); |
38701953 | 452 | |
61adc1a4 NC |
453 | /* pr_reg */ |
454 | offset = 72; | |
eea6121a | 455 | size = 68; |
38701953 | 456 | |
61adc1a4 NC |
457 | break; |
458 | } | |
38701953 AM |
459 | } |
460 | ||
461 | /* Make a ".reg/999" section. */ | |
462 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
eea6121a | 463 | size, note->descpos + offset); |
38701953 AM |
464 | } |
465 | ||
b34976b6 | 466 | static bfd_boolean |
55fd94b0 | 467 | elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
38701953 | 468 | { |
61adc1a4 | 469 | if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0) |
38701953 | 470 | { |
61adc1a4 NC |
471 | int pr_version = bfd_get_32 (abfd, note->descdata); |
472 | ||
473 | if (pr_version != 1) | |
b34976b6 | 474 | return FALSE; |
38701953 | 475 | |
228e534f | 476 | elf_tdata (abfd)->core->program |
61adc1a4 | 477 | = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17); |
228e534f | 478 | elf_tdata (abfd)->core->command |
61adc1a4 NC |
479 | = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81); |
480 | } | |
481 | else | |
482 | { | |
483 | switch (note->descsz) | |
484 | { | |
485 | default: | |
486 | return FALSE; | |
487 | ||
488 | case 124: /* Linux/i386 elf_prpsinfo. */ | |
228e534f | 489 | elf_tdata (abfd)->core->pid |
261b8d08 | 490 | = bfd_get_32 (abfd, note->descdata + 12); |
228e534f | 491 | elf_tdata (abfd)->core->program |
61adc1a4 | 492 | = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16); |
228e534f | 493 | elf_tdata (abfd)->core->command |
61adc1a4 NC |
494 | = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80); |
495 | } | |
38701953 AM |
496 | } |
497 | ||
498 | /* Note that for some reason, a spurious space is tacked | |
499 | onto the end of the args in some (at least one anyway) | |
500 | implementations, so strip it off if it exists. */ | |
38701953 | 501 | { |
228e534f | 502 | char *command = elf_tdata (abfd)->core->command; |
38701953 AM |
503 | int n = strlen (command); |
504 | ||
505 | if (0 < n && command[n - 1] == ' ') | |
506 | command[n - 1] = '\0'; | |
507 | } | |
508 | ||
b34976b6 | 509 | return TRUE; |
38701953 AM |
510 | } |
511 | \f | |
512 | /* Functions for the i386 ELF linker. | |
513 | ||
514 | In order to gain some understanding of code in this file without | |
515 | knowing all the intricate details of the linker, note the | |
516 | following: | |
517 | ||
518 | Functions named elf_i386_* are called by external routines, other | |
519 | functions are only called locally. elf_i386_* functions appear | |
520 | in this file more or less in the order in which they are called | |
521 | from external routines. eg. elf_i386_check_relocs is called | |
522 | early in the link process, elf_i386_finish_dynamic_sections is | |
523 | one of the last functions. */ | |
524 | ||
252b5132 RH |
525 | |
526 | /* The name of the dynamic interpreter. This is put in the .interp | |
527 | section. */ | |
528 | ||
529 | #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1" | |
530 | ||
f604c2a2 | 531 | /* The size in bytes of an entry in the lazy procedure linkage table. */ |
252b5132 | 532 | |
f604c2a2 | 533 | #define LAZY_PLT_ENTRY_SIZE 16 |
252b5132 | 534 | |
f604c2a2 L |
535 | /* The size in bytes of an entry in the non-lazy procedure linkage |
536 | table. */ | |
252b5132 | 537 | |
f604c2a2 L |
538 | #define NON_LAZY_PLT_ENTRY_SIZE 8 |
539 | ||
540 | /* The first entry in an absolute lazy procedure linkage table looks | |
541 | like this. See the SVR4 ABI i386 supplement to see how this works. | |
542 | Will be padded to LAZY_PLT_ENTRY_SIZE with lazy_plt->plt0_pad_byte. */ | |
543 | ||
544 | static const bfd_byte elf_i386_lazy_plt0_entry[12] = | |
252b5132 RH |
545 | { |
546 | 0xff, 0x35, /* pushl contents of address */ | |
547 | 0, 0, 0, 0, /* replaced with address of .got + 4. */ | |
548 | 0xff, 0x25, /* jmp indirect */ | |
eac338cf | 549 | 0, 0, 0, 0 /* replaced with address of .got + 8. */ |
252b5132 RH |
550 | }; |
551 | ||
f604c2a2 L |
552 | /* Subsequent entries in an absolute lazy procedure linkage table look |
553 | like this. */ | |
252b5132 | 554 | |
f604c2a2 | 555 | static const bfd_byte elf_i386_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] = |
252b5132 RH |
556 | { |
557 | 0xff, 0x25, /* jmp indirect */ | |
558 | 0, 0, 0, 0, /* replaced with address of this symbol in .got. */ | |
559 | 0x68, /* pushl immediate */ | |
560 | 0, 0, 0, 0, /* replaced with offset into relocation table. */ | |
561 | 0xe9, /* jmp relative */ | |
562 | 0, 0, 0, 0 /* replaced with offset to start of .plt. */ | |
563 | }; | |
564 | ||
f604c2a2 L |
565 | /* The first entry in a PIC lazy procedure linkage table look like |
566 | this. Will be padded to LAZY_PLT_ENTRY_SIZE with | |
567 | lazy_plt->plt0_pad_byte. */ | |
252b5132 | 568 | |
f604c2a2 | 569 | static const bfd_byte elf_i386_pic_lazy_plt0_entry[12] = |
252b5132 RH |
570 | { |
571 | 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */ | |
eac338cf | 572 | 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */ |
252b5132 RH |
573 | }; |
574 | ||
f604c2a2 L |
575 | /* Subsequent entries in a PIC lazy procedure linkage table look like |
576 | this. */ | |
252b5132 | 577 | |
f604c2a2 | 578 | static const bfd_byte elf_i386_pic_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] = |
252b5132 RH |
579 | { |
580 | 0xff, 0xa3, /* jmp *offset(%ebx) */ | |
581 | 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */ | |
582 | 0x68, /* pushl immediate */ | |
583 | 0, 0, 0, 0, /* replaced with offset into relocation table. */ | |
584 | 0xe9, /* jmp relative */ | |
585 | 0, 0, 0, 0 /* replaced with offset to start of .plt. */ | |
586 | }; | |
587 | ||
f604c2a2 | 588 | /* Entries in the non-lazy procedure linkage table look like this. */ |
dd7e64d4 | 589 | |
f604c2a2 | 590 | static const bfd_byte elf_i386_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] = |
dd7e64d4 L |
591 | { |
592 | 0xff, 0x25, /* jmp indirect */ | |
593 | 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */ | |
594 | 0x66, 0x90 /* xchg %ax,%ax */ | |
595 | }; | |
596 | ||
f604c2a2 L |
597 | /* Entries in the PIC non-lazy procedure linkage table look like |
598 | this. */ | |
dd7e64d4 | 599 | |
f604c2a2 | 600 | static const bfd_byte elf_i386_pic_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] = |
dd7e64d4 L |
601 | { |
602 | 0xff, 0xa3, /* jmp *offset(%ebx) */ | |
603 | 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */ | |
604 | 0x66, 0x90 /* xchg %ax,%ax */ | |
605 | }; | |
606 | ||
ee2fdd6f L |
607 | /* The first entry in an absolute IBT-enabled lazy procedure linkage |
608 | table looks like this. */ | |
609 | ||
610 | static const bfd_byte elf_i386_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] = | |
611 | { | |
612 | 0xff, 0x35, 0, 0, 0, 0, /* pushl GOT[1] */ | |
613 | 0xff, 0x25, 0, 0, 0, 0, /* jmp *GOT[2] */ | |
614 | 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */ | |
615 | }; | |
616 | ||
617 | /* Subsequent entries for an absolute IBT-enabled lazy procedure linkage | |
618 | table look like this. Subsequent entries for a PIC IBT-enabled lazy | |
619 | procedure linkage table are the same. */ | |
620 | ||
621 | static const bfd_byte elf_i386_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] = | |
622 | { | |
623 | 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */ | |
624 | 0x68, 0, 0, 0, 0, /* pushl immediate */ | |
625 | 0xe9, 0, 0, 0, 0, /* jmp relative */ | |
626 | 0x66, 0x90 /* xchg %ax,%ax */ | |
627 | }; | |
628 | ||
629 | /* The first entry in a PIC IBT-enabled lazy procedure linkage table | |
630 | look like. */ | |
631 | ||
632 | static const bfd_byte elf_i386_pic_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] = | |
633 | { | |
634 | 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */ | |
635 | 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */ | |
636 | 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */ | |
637 | }; | |
638 | ||
639 | /* Entries for branches with IBT-enabled in the absolute non-lazey | |
640 | procedure linkage table look like this. They have the same size | |
641 | as the lazy PLT entry. */ | |
642 | ||
643 | static const bfd_byte elf_i386_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] = | |
644 | { | |
645 | 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */ | |
646 | 0xff, 0x25, 0, 0, 0, 0, /* jmp *name@GOT */ | |
647 | 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */ | |
648 | }; | |
649 | ||
650 | /* Entries for branches with IBT-enabled in the PIC non-lazey procedure | |
651 | linkage table look like this. They have the same size as the lazy | |
652 | PLT entry. */ | |
653 | ||
654 | static const bfd_byte elf_i386_pic_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] = | |
655 | { | |
656 | 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */ | |
657 | 0xff, 0xa3, 0, 0, 0, 0, /* jmp *name@GOT(%ebx) */ | |
658 | 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */ | |
659 | }; | |
660 | ||
f604c2a2 | 661 | /* .eh_frame covering the lazy .plt section. */ |
e41b3a13 | 662 | |
f604c2a2 | 663 | static const bfd_byte elf_i386_eh_frame_lazy_plt[] = |
e41b3a13 JJ |
664 | { |
665 | #define PLT_CIE_LENGTH 20 | |
666 | #define PLT_FDE_LENGTH 36 | |
667 | #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8 | |
668 | #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12 | |
669 | PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */ | |
670 | 0, 0, 0, 0, /* CIE ID */ | |
671 | 1, /* CIE version */ | |
672 | 'z', 'R', 0, /* Augmentation string */ | |
673 | 1, /* Code alignment factor */ | |
674 | 0x7c, /* Data alignment factor */ | |
675 | 8, /* Return address column */ | |
676 | 1, /* Augmentation size */ | |
677 | DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */ | |
678 | DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */ | |
679 | DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */ | |
680 | DW_CFA_nop, DW_CFA_nop, | |
681 | ||
682 | PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */ | |
683 | PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */ | |
684 | 0, 0, 0, 0, /* R_386_PC32 .plt goes here */ | |
685 | 0, 0, 0, 0, /* .plt size goes here */ | |
686 | 0, /* Augmentation size */ | |
687 | DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */ | |
688 | DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */ | |
689 | DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */ | |
690 | DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */ | |
691 | DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */ | |
692 | 11, /* Block length */ | |
693 | DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */ | |
694 | DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */ | |
695 | DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge, | |
28ede8be | 696 | DW_OP_lit2, DW_OP_shl, DW_OP_plus, |
e41b3a13 JJ |
697 | DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop |
698 | }; | |
699 | ||
ee2fdd6f L |
700 | /* .eh_frame covering the lazy .plt section with IBT-enabled. */ |
701 | ||
702 | static const bfd_byte elf_i386_eh_frame_lazy_ibt_plt[] = | |
703 | { | |
704 | PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */ | |
705 | 0, 0, 0, 0, /* CIE ID */ | |
706 | 1, /* CIE version */ | |
707 | 'z', 'R', 0, /* Augmentation string */ | |
708 | 1, /* Code alignment factor */ | |
709 | 0x7c, /* Data alignment factor */ | |
710 | 8, /* Return address column */ | |
711 | 1, /* Augmentation size */ | |
712 | DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */ | |
713 | DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */ | |
714 | DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */ | |
715 | DW_CFA_nop, DW_CFA_nop, | |
716 | ||
717 | PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */ | |
718 | PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */ | |
719 | 0, 0, 0, 0, /* R_386_PC32 .plt goes here */ | |
720 | 0, 0, 0, 0, /* .plt size goes here */ | |
721 | 0, /* Augmentation size */ | |
722 | DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */ | |
723 | DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */ | |
724 | DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */ | |
725 | DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */ | |
726 | DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */ | |
727 | 11, /* Block length */ | |
728 | DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */ | |
729 | DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */ | |
730 | DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge, | |
731 | DW_OP_lit2, DW_OP_shl, DW_OP_plus, | |
732 | DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop | |
733 | }; | |
734 | ||
f604c2a2 | 735 | /* .eh_frame covering the non-lazy .plt section. */ |
fff53dae | 736 | |
f604c2a2 | 737 | static const bfd_byte elf_i386_eh_frame_non_lazy_plt[] = |
fff53dae L |
738 | { |
739 | #define PLT_GOT_FDE_LENGTH 16 | |
740 | PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */ | |
741 | 0, 0, 0, 0, /* CIE ID */ | |
742 | 1, /* CIE version */ | |
743 | 'z', 'R', 0, /* Augmentation string */ | |
744 | 1, /* Code alignment factor */ | |
745 | 0x7c, /* Data alignment factor */ | |
746 | 8, /* Return address column */ | |
747 | 1, /* Augmentation size */ | |
748 | DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */ | |
749 | DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */ | |
750 | DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */ | |
751 | DW_CFA_nop, DW_CFA_nop, | |
752 | ||
753 | PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */ | |
754 | PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */ | |
f604c2a2 L |
755 | 0, 0, 0, 0, /* the start of non-lazy .plt goes here */ |
756 | 0, 0, 0, 0, /* non-lazy .plt size goes here */ | |
fff53dae L |
757 | 0, /* Augmentation size */ |
758 | DW_CFA_nop, DW_CFA_nop, DW_CFA_nop | |
759 | }; | |
760 | ||
f604c2a2 | 761 | struct elf_i386_lazy_plt_layout |
25e762b9 | 762 | { |
f604c2a2 L |
763 | /* The first entry in an absolute lazy procedure linkage table looks |
764 | like this. */ | |
25e762b9 RM |
765 | const bfd_byte *plt0_entry; |
766 | unsigned int plt0_entry_size; | |
767 | ||
f604c2a2 L |
768 | /* Offsets into plt0_entry that are to be replaced with GOT[1] and |
769 | GOT[2]. */ | |
25e762b9 RM |
770 | unsigned int plt0_got1_offset; |
771 | unsigned int plt0_got2_offset; | |
772 | ||
f604c2a2 L |
773 | /* Later entries in an absolute lazy procedure linkage table look |
774 | like this. */ | |
25e762b9 RM |
775 | const bfd_byte *plt_entry; |
776 | unsigned int plt_entry_size; | |
777 | ||
778 | /* Offsets into plt_entry that are to be replaced with... */ | |
779 | unsigned int plt_got_offset; /* ... address of this symbol in .got. */ | |
780 | unsigned int plt_reloc_offset; /* ... offset into relocation table. */ | |
781 | unsigned int plt_plt_offset; /* ... offset to start of .plt. */ | |
782 | ||
f604c2a2 L |
783 | /* Offset into plt_entry where the initial value of the GOT entry |
784 | points. */ | |
25e762b9 RM |
785 | unsigned int plt_lazy_offset; |
786 | ||
f604c2a2 L |
787 | /* The first entry in a PIC lazy procedure linkage table looks like |
788 | this. */ | |
25e762b9 RM |
789 | const bfd_byte *pic_plt0_entry; |
790 | ||
f604c2a2 L |
791 | /* Subsequent entries in a PIC lazy procedure linkage table look |
792 | like this. */ | |
25e762b9 RM |
793 | const bfd_byte *pic_plt_entry; |
794 | ||
f604c2a2 | 795 | /* .eh_frame covering the lazy .plt section. */ |
25e762b9 RM |
796 | const bfd_byte *eh_frame_plt; |
797 | unsigned int eh_frame_plt_size; | |
f604c2a2 | 798 | }; |
fff53dae | 799 | |
f604c2a2 L |
800 | struct elf_i386_non_lazy_plt_layout |
801 | { | |
802 | /* Entries in an absolute non-lazy procedure linkage table look like | |
803 | this. */ | |
804 | const bfd_byte *plt_entry; | |
805 | /* Entries in a PIC non-lazy procedure linkage table look like this. */ | |
806 | const bfd_byte *pic_plt_entry; | |
807 | ||
808 | unsigned int plt_entry_size; | |
809 | ||
810 | /* Offsets into plt_entry that are to be replaced with... */ | |
811 | unsigned int plt_got_offset; /* ... address of this symbol in .got. */ | |
812 | ||
813 | /* .eh_frame covering the non-lazy .plt section. */ | |
814 | const bfd_byte *eh_frame_plt; | |
815 | unsigned int eh_frame_plt_size; | |
25e762b9 RM |
816 | }; |
817 | ||
f604c2a2 L |
818 | struct elf_i386_plt_layout |
819 | { | |
820 | /* The first entry in a lazy procedure linkage table looks like this. */ | |
821 | const bfd_byte *plt0_entry; | |
822 | /* Entries in a procedure linkage table look like this. */ | |
823 | const bfd_byte *plt_entry; | |
824 | unsigned int plt_entry_size; | |
825 | ||
826 | /* 1 has PLT0. */ | |
827 | unsigned int has_plt0; | |
828 | ||
829 | /* Offsets into plt_entry that are to be replaced with... */ | |
830 | unsigned int plt_got_offset; /* ... address of this symbol in .got. */ | |
831 | ||
832 | /* .eh_frame covering the .plt section. */ | |
833 | const bfd_byte *eh_frame_plt; | |
834 | unsigned int eh_frame_plt_size; | |
835 | }; | |
25e762b9 RM |
836 | |
837 | /* These are the standard parameters. */ | |
f604c2a2 | 838 | static const struct elf_i386_lazy_plt_layout elf_i386_lazy_plt = |
25e762b9 | 839 | { |
f604c2a2 L |
840 | elf_i386_lazy_plt0_entry, /* plt0_entry */ |
841 | sizeof (elf_i386_lazy_plt0_entry), /* plt0_entry_size */ | |
25e762b9 RM |
842 | 2, /* plt0_got1_offset */ |
843 | 8, /* plt0_got2_offset */ | |
f604c2a2 L |
844 | elf_i386_lazy_plt_entry, /* plt_entry */ |
845 | LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */ | |
25e762b9 RM |
846 | 2, /* plt_got_offset */ |
847 | 7, /* plt_reloc_offset */ | |
848 | 12, /* plt_plt_offset */ | |
849 | 6, /* plt_lazy_offset */ | |
f604c2a2 L |
850 | elf_i386_pic_lazy_plt0_entry, /* pic_plt0_entry */ |
851 | elf_i386_pic_lazy_plt_entry, /* pic_plt_entry */ | |
852 | elf_i386_eh_frame_lazy_plt, /* eh_frame_plt */ | |
853 | sizeof (elf_i386_eh_frame_lazy_plt) /* eh_frame_plt_size */ | |
854 | }; | |
855 | ||
856 | static const struct elf_i386_non_lazy_plt_layout elf_i386_non_lazy_plt = | |
857 | { | |
858 | elf_i386_non_lazy_plt_entry, /* plt_entry */ | |
859 | elf_i386_pic_non_lazy_plt_entry, /* pic_plt_entry */ | |
860 | NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */ | |
861 | 2, /* plt_got_offset */ | |
862 | elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */ | |
863 | sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */ | |
25e762b9 | 864 | }; |
ee2fdd6f L |
865 | |
866 | static const struct elf_i386_lazy_plt_layout elf_i386_lazy_ibt_plt = | |
867 | { | |
868 | elf_i386_lazy_ibt_plt0_entry, /* plt0_entry */ | |
869 | sizeof (elf_i386_lazy_ibt_plt0_entry), /* plt0_entry_size */ | |
870 | 2, /* plt0_got1_offset */ | |
871 | 8, /* plt0_got2_offset */ | |
872 | elf_i386_lazy_ibt_plt_entry, /* plt_entry */ | |
873 | LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */ | |
874 | 4+2, /* plt_got_offset */ | |
875 | 4+1, /* plt_reloc_offset */ | |
876 | 4+6, /* plt_plt_offset */ | |
877 | 0, /* plt_lazy_offset */ | |
878 | elf_i386_pic_lazy_ibt_plt0_entry, /* pic_plt0_entry */ | |
879 | elf_i386_lazy_ibt_plt_entry, /* pic_plt_entry */ | |
880 | elf_i386_eh_frame_lazy_ibt_plt, /* eh_frame_plt */ | |
881 | sizeof (elf_i386_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */ | |
882 | }; | |
883 | ||
884 | static const struct elf_i386_non_lazy_plt_layout elf_i386_non_lazy_ibt_plt = | |
885 | { | |
886 | elf_i386_non_lazy_ibt_plt_entry, /* plt_entry */ | |
887 | elf_i386_pic_non_lazy_ibt_plt_entry,/* pic_plt_entry */ | |
888 | LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */ | |
889 | 4+2, /* plt_got_offset */ | |
890 | elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */ | |
891 | sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */ | |
892 | }; | |
25e762b9 RM |
893 | \f |
894 | ||
eac338cf PB |
895 | /* On VxWorks, the .rel.plt.unloaded section has absolute relocations |
896 | for the PLTResolve stub and then for each PLT entry. */ | |
897 | #define PLTRESOLVE_RELOCS_SHLIB 0 | |
898 | #define PLTRESOLVE_RELOCS 2 | |
899 | #define PLT_NON_JUMP_SLOT_RELOCS 2 | |
900 | ||
23209a78 RM |
901 | /* Architecture-specific backend data for i386. */ |
902 | ||
903 | struct elf_i386_backend_data | |
904 | { | |
905 | /* Value used to fill the unused bytes of the first PLT entry. */ | |
906 | bfd_byte plt0_pad_byte; | |
907 | ||
f604c2a2 L |
908 | /* Target system. */ |
909 | enum | |
910 | { | |
911 | is_normal, | |
912 | is_vxworks, | |
913 | is_nacl | |
914 | } os; | |
23209a78 RM |
915 | }; |
916 | ||
917 | #define get_elf_i386_backend_data(abfd) \ | |
918 | ((const struct elf_i386_backend_data *) \ | |
919 | get_elf_backend_data (abfd)->arch_data) | |
920 | ||
921 | /* These are the standard parameters. */ | |
922 | static const struct elf_i386_backend_data elf_i386_arch_bed = | |
923 | { | |
924 | 0, /* plt0_pad_byte */ | |
f604c2a2 | 925 | is_normal /* os */ |
23209a78 RM |
926 | }; |
927 | ||
928 | #define elf_backend_arch_data &elf_i386_arch_bed | |
929 | ||
0afcef53 | 930 | /* Values in tls_type of x86 ELF linker hash entry. */ |
37e55690 JJ |
931 | #define GOT_TLS_IE 4 |
932 | #define GOT_TLS_IE_POS 5 | |
933 | #define GOT_TLS_IE_NEG 6 | |
934 | #define GOT_TLS_IE_BOTH 7 | |
67a4f2b7 AO |
935 | #define GOT_TLS_GDESC 8 |
936 | #define GOT_TLS_GD_BOTH_P(type) \ | |
937 | ((type) == (GOT_TLS_GD | GOT_TLS_GDESC)) | |
938 | #define GOT_TLS_GD_P(type) \ | |
939 | ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type)) | |
940 | #define GOT_TLS_GDESC_P(type) \ | |
941 | ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type)) | |
942 | #define GOT_TLS_GD_ANY_P(type) \ | |
943 | (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type)) | |
67a4f2b7 | 944 | |
0ffa91dd NC |
945 | #define is_i386_elf(bfd) \ |
946 | (bfd_get_flavour (bfd) == bfd_target_elf_flavour \ | |
947 | && elf_tdata (bfd) != NULL \ | |
4dfe6ac6 | 948 | && elf_object_id (bfd) == I386_ELF_DATA) |
0ffa91dd | 949 | |
b34976b6 | 950 | static bfd_boolean |
55fd94b0 | 951 | elf_i386_mkobject (bfd *abfd) |
13ae64f3 | 952 | { |
0afcef53 L |
953 | return bfd_elf_allocate_object (abfd, |
954 | sizeof (struct elf_x86_obj_tdata), | |
4dfe6ac6 | 955 | I386_ELF_DATA); |
13ae64f3 | 956 | } |
cedb70c5 | 957 | |
252b5132 RH |
958 | /* i386 ELF linker hash table. */ |
959 | ||
960 | struct elf_i386_link_hash_table | |
961 | { | |
0afcef53 | 962 | struct elf_x86_link_hash_table x86; |
9635fe29 | 963 | |
0afcef53 | 964 | /* Parameters describing PLT generation, lazy or non-lazy. */ |
f604c2a2 L |
965 | struct elf_i386_plt_layout plt; |
966 | ||
967 | /* Parameters describing lazy PLT generation. */ | |
968 | const struct elf_i386_lazy_plt_layout *lazy_plt; | |
969 | ||
970 | /* Parameters describing non-lazy PLT generation. */ | |
971 | const struct elf_i386_non_lazy_plt_layout *non_lazy_plt; | |
972 | ||
4dfe6ac6 NC |
973 | /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */ |
974 | asection *srelplt2; | |
975 | ||
4dfe6ac6 NC |
976 | /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */ |
977 | bfd_vma next_tls_desc_index; | |
0afcef53 | 978 | }; |
e1f98742 | 979 | |
0afcef53 L |
980 | #define elf_i386_next_tls_desc_index(htab) \ |
981 | ((struct elf_i386_link_hash_table *) (htab))->next_tls_desc_index | |
e1f98742 | 982 | |
0afcef53 L |
983 | #define elf_i386_srelplt2(htab) \ |
984 | ((struct elf_i386_link_hash_table *) (htab))->srelplt2 | |
2df3368d | 985 | |
0afcef53 L |
986 | #define elf_i386_plt(htab) \ |
987 | ((struct elf_i386_link_hash_table *) (htab))->plt | |
252b5132 | 988 | |
0afcef53 L |
989 | #define elf_i386_lazy_plt(htab) \ |
990 | ((struct elf_i386_link_hash_table *) (htab))->lazy_plt | |
252b5132 | 991 | |
0afcef53 L |
992 | #define elf_i386_non_lazy_plt(htab) \ |
993 | ((struct elf_i386_link_hash_table *) (htab))->non_lazy_plt | |
252b5132 | 994 | |
67a4f2b7 | 995 | #define elf_i386_compute_jump_table_size(htab) \ |
998d811a | 996 | ((htab)->elf.srelplt->reloc_count * 4) |
67a4f2b7 | 997 | |
252b5132 RH |
998 | /* Create an i386 ELF linker hash table. */ |
999 | ||
1000 | static struct bfd_link_hash_table * | |
55fd94b0 | 1001 | elf_i386_link_hash_table_create (bfd *abfd) |
252b5132 | 1002 | { |
0afcef53 | 1003 | struct elf_x86_link_hash_table *ret; |
dc810e39 | 1004 | bfd_size_type amt = sizeof (struct elf_i386_link_hash_table); |
252b5132 | 1005 | |
0afcef53 | 1006 | ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt); |
ebe50bae | 1007 | if (ret == NULL) |
252b5132 RH |
1008 | return NULL; |
1009 | ||
eb4ff4d6 | 1010 | if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, |
0afcef53 L |
1011 | _bfd_x86_elf_link_hash_newfunc, |
1012 | sizeof (struct elf_x86_link_hash_entry), | |
4dfe6ac6 | 1013 | I386_ELF_DATA)) |
252b5132 | 1014 | { |
e2d34d7d | 1015 | free (ret); |
252b5132 RH |
1016 | return NULL; |
1017 | } | |
1018 | ||
0afcef53 L |
1019 | ret->r_info = elf32_r_info; |
1020 | ret->r_sym = elf32_r_sym; | |
1021 | ret->pointer_r_type = R_386_32; | |
1022 | ret->dynamic_interpreter_size = sizeof ELF_DYNAMIC_INTERPRETER; | |
1023 | ret->dynamic_interpreter = ELF_DYNAMIC_INTERPRETER; | |
1024 | ret->tls_get_addr = "___tls_get_addr"; | |
c25bc9fc | 1025 | ret->loc_hash_table = htab_try_create (1024, |
0afcef53 L |
1026 | _bfd_x86_elf_local_htab_hash, |
1027 | _bfd_x86_elf_local_htab_eq, | |
c25bc9fc L |
1028 | NULL); |
1029 | ret->loc_hash_memory = objalloc_create (); | |
1030 | if (!ret->loc_hash_table || !ret->loc_hash_memory) | |
1031 | { | |
0afcef53 | 1032 | _bfd_x86_elf_link_hash_table_free (abfd); |
c25bc9fc L |
1033 | return NULL; |
1034 | } | |
0afcef53 | 1035 | ret->elf.root.hash_table_free = _bfd_x86_elf_link_hash_table_free; |
c25bc9fc | 1036 | |
ebe50bae | 1037 | return &ret->elf.root; |
252b5132 RH |
1038 | } |
1039 | ||
142411ca L |
1040 | /* Return TRUE if the TLS access code sequence support transition |
1041 | from R_TYPE. */ | |
1042 | ||
1043 | static bfd_boolean | |
bedfd056 | 1044 | elf_i386_check_tls_transition (asection *sec, |
142411ca L |
1045 | bfd_byte *contents, |
1046 | Elf_Internal_Shdr *symtab_hdr, | |
1047 | struct elf_link_hash_entry **sym_hashes, | |
1048 | unsigned int r_type, | |
1049 | const Elf_Internal_Rela *rel, | |
1050 | const Elf_Internal_Rela *relend) | |
13ae64f3 | 1051 | { |
6eaa7fb5 | 1052 | unsigned int val, type, reg; |
142411ca L |
1053 | unsigned long r_symndx; |
1054 | struct elf_link_hash_entry *h; | |
1055 | bfd_vma offset; | |
6eaa7fb5 | 1056 | bfd_byte *call; |
1d4af308 | 1057 | bfd_boolean indirect_call; |
142411ca | 1058 | |
142411ca | 1059 | offset = rel->r_offset; |
13ae64f3 | 1060 | switch (r_type) |
142411ca L |
1061 | { |
1062 | case R_386_TLS_GD: | |
1063 | case R_386_TLS_LDM: | |
1064 | if (offset < 2 || (rel + 1) >= relend) | |
1065 | return FALSE; | |
1066 | ||
6eaa7fb5 L |
1067 | indirect_call = FALSE; |
1068 | call = contents + offset + 4; | |
1069 | val = *(call - 5); | |
1070 | type = *(call - 6); | |
142411ca L |
1071 | if (r_type == R_386_TLS_GD) |
1072 | { | |
09e8c3bf | 1073 | /* Check transition from GD access model. Only |
6eaa7fb5 L |
1074 | leal foo@tlsgd(,%ebx,1), %eax |
1075 | call ___tls_get_addr@PLT | |
1076 | or | |
1077 | leal foo@tlsgd(%ebx) %eax | |
1078 | call ___tls_get_addr@PLT | |
1079 | nop | |
1080 | or | |
1081 | leal foo@tlsgd(%reg), %eax | |
1082 | call *___tls_get_addr@GOT(%reg) | |
1083 | which may be converted to | |
1084 | addr32 call ___tls_get_addr | |
142411ca | 1085 | can transit to different access model. */ |
6eaa7fb5 L |
1086 | if ((offset + 10) > sec->size |
1087 | || (type != 0x8d && type != 0x04)) | |
142411ca L |
1088 | return FALSE; |
1089 | ||
142411ca L |
1090 | if (type == 0x04) |
1091 | { | |
6eaa7fb5 L |
1092 | /* leal foo@tlsgd(,%ebx,1), %eax |
1093 | call ___tls_get_addr@PLT */ | |
142411ca L |
1094 | if (offset < 3) |
1095 | return FALSE; | |
1096 | ||
6eaa7fb5 L |
1097 | if (*(call - 7) != 0x8d |
1098 | || val != 0x1d | |
1099 | || call[0] != 0xe8) | |
142411ca L |
1100 | return FALSE; |
1101 | } | |
1102 | else | |
1103 | { | |
6eaa7fb5 L |
1104 | /* This must be |
1105 | leal foo@tlsgd(%ebx), %eax | |
1106 | call ___tls_get_addr@PLT | |
1107 | nop | |
1108 | or | |
1109 | leal foo@tlsgd(%reg), %eax | |
1110 | call *___tls_get_addr@GOT(%reg) | |
1111 | which may be converted to | |
1112 | addr32 call ___tls_get_addr | |
1113 | ||
1114 | %eax can't be used as the GOT base register since it | |
1115 | is used to pass parameter to ___tls_get_addr. */ | |
1116 | reg = val & 7; | |
1117 | if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0) | |
142411ca L |
1118 | return FALSE; |
1119 | ||
6eaa7fb5 L |
1120 | indirect_call = call[0] == 0xff; |
1121 | if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90) | |
1122 | && !(call[0] == 0x67 && call[1] == 0xe8) | |
1123 | && !(indirect_call | |
1124 | && (call[1] & 0xf8) == 0x90 | |
1125 | && (call[1] & 0x7) == reg)) | |
142411ca L |
1126 | return FALSE; |
1127 | } | |
1128 | } | |
1129 | else | |
1130 | { | |
1131 | /* Check transition from LD access model. Only | |
6eaa7fb5 L |
1132 | leal foo@tlsldm(%ebx), %eax |
1133 | call ___tls_get_addr@PLT | |
1134 | or | |
1135 | leal foo@tlsldm(%reg), %eax | |
1136 | call *___tls_get_addr@GOT(%reg) | |
1137 | which may be converted to | |
1138 | addr32 call ___tls_get_addr | |
142411ca L |
1139 | can transit to different access model. */ |
1140 | if (type != 0x8d || (offset + 9) > sec->size) | |
1141 | return FALSE; | |
1142 | ||
6eaa7fb5 L |
1143 | /* %eax can't be used as the GOT base register since it is |
1144 | used to pass parameter to ___tls_get_addr. */ | |
1145 | reg = val & 7; | |
1146 | if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0) | |
142411ca | 1147 | return FALSE; |
142411ca | 1148 | |
6eaa7fb5 L |
1149 | indirect_call = call[0] == 0xff; |
1150 | if (!(reg == 3 && call[0] == 0xe8) | |
1151 | && !(call[0] == 0x67 && call[1] == 0xe8) | |
1152 | && !(indirect_call | |
1153 | && (call[1] & 0xf8) == 0x90 | |
1154 | && (call[1] & 0x7) == reg)) | |
1155 | return FALSE; | |
1156 | } | |
142411ca L |
1157 | |
1158 | r_symndx = ELF32_R_SYM (rel[1].r_info); | |
1159 | if (r_symndx < symtab_hdr->sh_info) | |
1160 | return FALSE; | |
1161 | ||
1162 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1d4af308 | 1163 | if (h == NULL |
0afcef53 | 1164 | || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr) |
6eaa7fb5 L |
1165 | return FALSE; |
1166 | else if (indirect_call) | |
1167 | return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X); | |
1168 | else | |
1169 | return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32 | |
1170 | || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32); | |
142411ca L |
1171 | |
1172 | case R_386_TLS_IE: | |
1173 | /* Check transition from IE access model: | |
1174 | movl foo@indntpoff(%rip), %eax | |
1175 | movl foo@indntpoff(%rip), %reg | |
1176 | addl foo@indntpoff(%rip), %reg | |
1177 | */ | |
1178 | ||
1179 | if (offset < 1 || (offset + 4) > sec->size) | |
1180 | return FALSE; | |
1181 | ||
1182 | /* Check "movl foo@tpoff(%rip), %eax" first. */ | |
1183 | val = bfd_get_8 (abfd, contents + offset - 1); | |
1184 | if (val == 0xa1) | |
1185 | return TRUE; | |
1186 | ||
1187 | if (offset < 2) | |
1188 | return FALSE; | |
1189 | ||
1190 | /* Check movl|addl foo@tpoff(%rip), %reg. */ | |
1191 | type = bfd_get_8 (abfd, contents + offset - 2); | |
1192 | return ((type == 0x8b || type == 0x03) | |
1193 | && (val & 0xc7) == 0x05); | |
1194 | ||
1195 | case R_386_TLS_GOTIE: | |
1196 | case R_386_TLS_IE_32: | |
1197 | /* Check transition from {IE_32,GOTIE} access model: | |
1198 | subl foo@{tpoff,gontoff}(%reg1), %reg2 | |
1199 | movl foo@{tpoff,gontoff}(%reg1), %reg2 | |
1200 | addl foo@{tpoff,gontoff}(%reg1), %reg2 | |
1201 | */ | |
1202 | ||
1203 | if (offset < 2 || (offset + 4) > sec->size) | |
1204 | return FALSE; | |
1205 | ||
1206 | val = bfd_get_8 (abfd, contents + offset - 1); | |
1207 | if ((val & 0xc0) != 0x80 || (val & 7) == 4) | |
1208 | return FALSE; | |
1209 | ||
1210 | type = bfd_get_8 (abfd, contents + offset - 2); | |
1211 | return type == 0x8b || type == 0x2b || type == 0x03; | |
1212 | ||
1213 | case R_386_TLS_GOTDESC: | |
1214 | /* Check transition from GDesc access model: | |
1215 | leal x@tlsdesc(%ebx), %eax | |
1216 | ||
1217 | Make sure it's a leal adding ebx to a 32-bit offset | |
1218 | into any register, although it's probably almost always | |
1219 | going to be eax. */ | |
1220 | ||
1221 | if (offset < 2 || (offset + 4) > sec->size) | |
1222 | return FALSE; | |
1223 | ||
1224 | if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d) | |
1225 | return FALSE; | |
1226 | ||
1227 | val = bfd_get_8 (abfd, contents + offset - 1); | |
1228 | return (val & 0xc7) == 0x83; | |
1229 | ||
1230 | case R_386_TLS_DESC_CALL: | |
1231 | /* Check transition from GDesc access model: | |
6eaa7fb5 | 1232 | call *x@tlsdesc(%eax) |
142411ca L |
1233 | */ |
1234 | if (offset + 2 <= sec->size) | |
1235 | { | |
6eaa7fb5 L |
1236 | /* Make sure that it's a call *x@tlsdesc(%eax). */ |
1237 | call = contents + offset; | |
1238 | return call[0] == 0xff && call[1] == 0x10; | |
142411ca L |
1239 | } |
1240 | ||
1241 | return FALSE; | |
1242 | ||
1243 | default: | |
1244 | abort (); | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | /* Return TRUE if the TLS access transition is OK or no transition | |
1249 | will be performed. Update R_TYPE if there is a transition. */ | |
1250 | ||
1251 | static bfd_boolean | |
1252 | elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd, | |
1253 | asection *sec, bfd_byte *contents, | |
1254 | Elf_Internal_Shdr *symtab_hdr, | |
1255 | struct elf_link_hash_entry **sym_hashes, | |
1256 | unsigned int *r_type, int tls_type, | |
1257 | const Elf_Internal_Rela *rel, | |
1258 | const Elf_Internal_Rela *relend, | |
4c544807 | 1259 | struct elf_link_hash_entry *h, |
bedfd056 L |
1260 | unsigned long r_symndx, |
1261 | bfd_boolean from_relocate_section) | |
142411ca L |
1262 | { |
1263 | unsigned int from_type = *r_type; | |
1264 | unsigned int to_type = from_type; | |
1265 | bfd_boolean check = TRUE; | |
1266 | ||
bb1cb422 L |
1267 | /* Skip TLS transition for functions. */ |
1268 | if (h != NULL | |
1269 | && (h->type == STT_FUNC | |
1270 | || h->type == STT_GNU_IFUNC)) | |
1271 | return TRUE; | |
1272 | ||
142411ca | 1273 | switch (from_type) |
13ae64f3 JJ |
1274 | { |
1275 | case R_386_TLS_GD: | |
67a4f2b7 AO |
1276 | case R_386_TLS_GOTDESC: |
1277 | case R_386_TLS_DESC_CALL: | |
13ae64f3 | 1278 | case R_386_TLS_IE_32: |
37e55690 JJ |
1279 | case R_386_TLS_IE: |
1280 | case R_386_TLS_GOTIE: | |
0e1862bb | 1281 | if (bfd_link_executable (info)) |
142411ca L |
1282 | { |
1283 | if (h == NULL) | |
1284 | to_type = R_386_TLS_LE_32; | |
1285 | else if (from_type != R_386_TLS_IE | |
1286 | && from_type != R_386_TLS_GOTIE) | |
1287 | to_type = R_386_TLS_IE_32; | |
1288 | } | |
1289 | ||
bedfd056 L |
1290 | /* When we are called from elf_i386_relocate_section, there may |
1291 | be additional transitions based on TLS_TYPE. */ | |
1292 | if (from_relocate_section) | |
142411ca L |
1293 | { |
1294 | unsigned int new_to_type = to_type; | |
1295 | ||
0e1862bb | 1296 | if (bfd_link_executable (info) |
142411ca L |
1297 | && h != NULL |
1298 | && h->dynindx == -1 | |
1299 | && (tls_type & GOT_TLS_IE)) | |
1300 | new_to_type = R_386_TLS_LE_32; | |
1301 | ||
1302 | if (to_type == R_386_TLS_GD | |
1303 | || to_type == R_386_TLS_GOTDESC | |
1304 | || to_type == R_386_TLS_DESC_CALL) | |
1305 | { | |
1306 | if (tls_type == GOT_TLS_IE_POS) | |
1307 | new_to_type = R_386_TLS_GOTIE; | |
1308 | else if (tls_type & GOT_TLS_IE) | |
1309 | new_to_type = R_386_TLS_IE_32; | |
1310 | } | |
1311 | ||
1312 | /* We checked the transition before when we were called from | |
1313 | elf_i386_check_relocs. We only want to check the new | |
1314 | transition which hasn't been checked before. */ | |
1315 | check = new_to_type != to_type && from_type == to_type; | |
1316 | to_type = new_to_type; | |
1317 | } | |
1318 | ||
1319 | break; | |
1320 | ||
13ae64f3 | 1321 | case R_386_TLS_LDM: |
0e1862bb | 1322 | if (bfd_link_executable (info)) |
142411ca L |
1323 | to_type = R_386_TLS_LE_32; |
1324 | break; | |
1325 | ||
1326 | default: | |
1327 | return TRUE; | |
1328 | } | |
1329 | ||
1330 | /* Return TRUE if there is no transition. */ | |
1331 | if (from_type == to_type) | |
1332 | return TRUE; | |
1333 | ||
1334 | /* Check if the transition can be performed. */ | |
1335 | if (check | |
bedfd056 | 1336 | && ! elf_i386_check_tls_transition (sec, contents, |
142411ca L |
1337 | symtab_hdr, sym_hashes, |
1338 | from_type, rel, relend)) | |
1339 | { | |
2f629d23 | 1340 | reloc_howto_type *from, *to; |
4c544807 | 1341 | const char *name; |
142411ca L |
1342 | |
1343 | from = elf_i386_rtype_to_howto (abfd, from_type); | |
1344 | to = elf_i386_rtype_to_howto (abfd, to_type); | |
1345 | ||
4c544807 L |
1346 | if (h) |
1347 | name = h->root.root.string; | |
1348 | else | |
1349 | { | |
0afcef53 | 1350 | struct elf_x86_link_hash_table *htab; |
4dfe6ac6 | 1351 | |
0afcef53 | 1352 | htab = elf_x86_hash_table (info, I386_ELF_DATA); |
4dfe6ac6 NC |
1353 | if (htab == NULL) |
1354 | name = "*unknown*"; | |
1355 | else | |
1356 | { | |
1357 | Elf_Internal_Sym *isym; | |
1358 | ||
1359 | isym = bfd_sym_from_r_symndx (&htab->sym_cache, | |
1360 | abfd, r_symndx); | |
1361 | name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); | |
1362 | } | |
4c544807 L |
1363 | } |
1364 | ||
4eca0228 | 1365 | _bfd_error_handler |
695344c0 | 1366 | /* xgettext:c-format */ |
d42c267e | 1367 | (_("%B: TLS transition from %s to %s against `%s' at %#Lx " |
142411ca | 1368 | "in section `%A' failed"), |
c08bb8dd | 1369 | abfd, from->name, to->name, name, |
d42c267e | 1370 | rel->r_offset, sec); |
142411ca L |
1371 | bfd_set_error (bfd_error_bad_value); |
1372 | return FALSE; | |
13ae64f3 JJ |
1373 | } |
1374 | ||
142411ca L |
1375 | *r_type = to_type; |
1376 | return TRUE; | |
13ae64f3 JJ |
1377 | } |
1378 | ||
c175a657 L |
1379 | /* With the local symbol, foo, we convert |
1380 | mov foo@GOT[(%reg1)], %reg2 | |
1381 | to | |
1382 | lea foo[@GOTOFF(%reg1)], %reg2 | |
1383 | and convert | |
1384 | call/jmp *foo@GOT[(%reg)] | |
1385 | to | |
1386 | nop call foo/jmp foo nop | |
1387 | When PIC is false, convert | |
1388 | test %reg1, foo@GOT[(%reg2)] | |
1389 | to | |
1390 | test $foo, %reg1 | |
1391 | and convert | |
1392 | binop foo@GOT[(%reg1)], %reg2 | |
1393 | to | |
1394 | binop $foo, %reg2 | |
1395 | where binop is one of adc, add, and, cmp, or, sbb, sub, xor | |
1396 | instructions. */ | |
1397 | ||
1398 | static | |
1399 | bfd_boolean | |
1400 | elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr, | |
1401 | bfd_byte *contents, | |
1402 | Elf_Internal_Rela *irel, | |
1403 | struct elf_link_hash_entry *h, | |
1404 | bfd_boolean *converted, | |
1405 | struct bfd_link_info *link_info) | |
1406 | { | |
0afcef53 | 1407 | struct elf_x86_link_hash_table *htab; |
c175a657 L |
1408 | unsigned int opcode; |
1409 | unsigned int modrm; | |
1410 | bfd_boolean baseless; | |
1411 | Elf_Internal_Sym *isym; | |
1412 | unsigned int addend; | |
1413 | unsigned int nop; | |
1414 | bfd_vma nop_offset; | |
1415 | bfd_boolean is_pic; | |
1416 | bfd_boolean to_reloc_32; | |
1417 | unsigned int r_type; | |
1418 | unsigned int r_symndx; | |
1419 | bfd_vma roff = irel->r_offset; | |
1420 | ||
1421 | if (roff < 2) | |
1422 | return TRUE; | |
1423 | ||
7d4d9709 | 1424 | /* Addend for R_386_GOT32X relocations must be 0. */ |
c175a657 L |
1425 | addend = bfd_get_32 (abfd, contents + roff); |
1426 | if (addend != 0) | |
1427 | return TRUE; | |
1428 | ||
0afcef53 | 1429 | htab = elf_x86_hash_table (link_info, I386_ELF_DATA); |
c175a657 L |
1430 | is_pic = bfd_link_pic (link_info); |
1431 | ||
1432 | r_type = ELF32_R_TYPE (irel->r_info); | |
1433 | r_symndx = ELF32_R_SYM (irel->r_info); | |
1434 | ||
1435 | modrm = bfd_get_8 (abfd, contents + roff - 1); | |
1436 | baseless = (modrm & 0xc7) == 0x5; | |
1437 | ||
7d4d9709 | 1438 | if (baseless && is_pic) |
c175a657 L |
1439 | { |
1440 | /* For PIC, disallow R_386_GOT32X without a base register | |
7d4d9709 | 1441 | since we don't know what the GOT base is. */ |
c175a657 L |
1442 | const char *name; |
1443 | ||
1444 | if (h == NULL) | |
1445 | { | |
1446 | isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, | |
1447 | r_symndx); | |
1448 | name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); | |
1449 | } | |
1450 | else | |
1451 | name = h->root.root.string; | |
1452 | ||
4eca0228 | 1453 | _bfd_error_handler |
695344c0 | 1454 | /* xgettext:c-format */ |
63a5468a AM |
1455 | (_("%B: direct GOT relocation R_386_GOT32X against `%s' without base" |
1456 | " register can not be used when making a shared object"), | |
c175a657 L |
1457 | abfd, name); |
1458 | return FALSE; | |
1459 | } | |
1460 | ||
1461 | opcode = bfd_get_8 (abfd, contents + roff - 2); | |
1462 | ||
c175a657 L |
1463 | /* Convert to R_386_32 if PIC is false or there is no base |
1464 | register. */ | |
1465 | to_reloc_32 = !is_pic || baseless; | |
1466 | ||
7d4d9709 L |
1467 | /* Try to convert R_386_GOT32X. Get the symbol referred to by the |
1468 | reloc. */ | |
c175a657 L |
1469 | if (h == NULL) |
1470 | { | |
1471 | if (opcode == 0x0ff) | |
1472 | /* Convert "call/jmp *foo@GOT[(%reg)]". */ | |
1473 | goto convert_branch; | |
1474 | else | |
1475 | /* Convert "mov foo@GOT[(%reg1)], %reg2", | |
1476 | "test %reg1, foo@GOT(%reg2)" and | |
1477 | "binop foo@GOT[(%reg1)], %reg2". */ | |
1478 | goto convert_load; | |
1479 | } | |
1480 | ||
1481 | /* Undefined weak symbol is only bound locally in executable | |
1482 | and its reference is resolved as 0. */ | |
0afcef53 L |
1483 | if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info, I386_ELF_DATA, TRUE, |
1484 | elf_x86_hash_entry (h))) | |
c175a657 L |
1485 | { |
1486 | if (opcode == 0xff) | |
1487 | { | |
1488 | /* No direct branch to 0 for PIC. */ | |
1489 | if (is_pic) | |
1490 | return TRUE; | |
1491 | else | |
1492 | goto convert_branch; | |
1493 | } | |
1494 | else | |
1495 | { | |
1496 | /* We can convert load of address 0 to R_386_32. */ | |
1497 | to_reloc_32 = TRUE; | |
1498 | goto convert_load; | |
1499 | } | |
1500 | } | |
1501 | ||
1502 | if (opcode == 0xff) | |
1503 | { | |
1504 | /* We have "call/jmp *foo@GOT[(%reg)]". */ | |
1505 | if ((h->root.type == bfd_link_hash_defined | |
1506 | || h->root.type == bfd_link_hash_defweak) | |
1507 | && SYMBOL_REFERENCES_LOCAL (link_info, h)) | |
1508 | { | |
1509 | /* The function is locally defined. */ | |
1510 | convert_branch: | |
1511 | /* Convert R_386_GOT32X to R_386_PC32. */ | |
1512 | if (modrm == 0x15 || (modrm & 0xf8) == 0x90) | |
1513 | { | |
0afcef53 L |
1514 | struct elf_x86_link_hash_entry *eh |
1515 | = (struct elf_x86_link_hash_entry *) h; | |
6eaa7fb5 | 1516 | |
c175a657 L |
1517 | /* Convert to "nop call foo". ADDR_PREFIX_OPCODE |
1518 | is a nop prefix. */ | |
1519 | modrm = 0xe8; | |
6eaa7fb5 L |
1520 | /* To support TLS optimization, always use addr32 prefix |
1521 | for "call *___tls_get_addr@GOT(%reg)". */ | |
1d4af308 | 1522 | if (eh && eh->tls_get_addr) |
c175a657 | 1523 | { |
6eaa7fb5 L |
1524 | nop = 0x67; |
1525 | nop_offset = irel->r_offset - 2; | |
c175a657 L |
1526 | } |
1527 | else | |
6eaa7fb5 L |
1528 | { |
1529 | nop = link_info->call_nop_byte; | |
1530 | if (link_info->call_nop_as_suffix) | |
1531 | { | |
1532 | nop_offset = roff + 3; | |
1533 | irel->r_offset -= 1; | |
1534 | } | |
1535 | else | |
1536 | nop_offset = roff - 2; | |
1537 | } | |
c175a657 L |
1538 | } |
1539 | else | |
1540 | { | |
1541 | /* Convert to "jmp foo nop". */ | |
1542 | modrm = 0xe9; | |
1543 | nop = NOP_OPCODE; | |
1544 | nop_offset = roff + 3; | |
1545 | irel->r_offset -= 1; | |
1546 | } | |
1547 | ||
1548 | bfd_put_8 (abfd, nop, contents + nop_offset); | |
1549 | bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1); | |
1550 | /* When converting to PC-relative relocation, we | |
1551 | need to adjust addend by -4. */ | |
1552 | bfd_put_32 (abfd, -4, contents + irel->r_offset); | |
1553 | irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32); | |
1554 | ||
1555 | *converted = TRUE; | |
1556 | } | |
1557 | } | |
1558 | else | |
1559 | { | |
1560 | /* We have "mov foo@GOT[(%re1g)], %reg2", | |
1561 | "test %reg1, foo@GOT(%reg2)" and | |
1562 | "binop foo@GOT[(%reg1)], %reg2". | |
1563 | ||
1564 | Avoid optimizing _DYNAMIC since ld.so may use its | |
1565 | link-time address. */ | |
1566 | if (h == htab->elf.hdynamic) | |
1567 | return TRUE; | |
1568 | ||
1569 | /* def_regular is set by an assignment in a linker script in | |
cbd0eecf L |
1570 | bfd_elf_record_link_assignment. start_stop is set on |
1571 | __start_SECNAME/__stop_SECNAME which mark section SECNAME. */ | |
1572 | if (h->start_stop | |
1573 | || ((h->def_regular | |
1574 | || h->root.type == bfd_link_hash_defined | |
1575 | || h->root.type == bfd_link_hash_defweak) | |
1576 | && SYMBOL_REFERENCES_LOCAL (link_info, h))) | |
c175a657 L |
1577 | { |
1578 | convert_load: | |
1579 | if (opcode == 0x8b) | |
1580 | { | |
1581 | if (to_reloc_32) | |
1582 | { | |
1583 | /* Convert "mov foo@GOT[(%reg1)], %reg2" to | |
1584 | "mov $foo, %reg2" with R_386_32. */ | |
1585 | r_type = R_386_32; | |
1586 | modrm = 0xc0 | (modrm & 0x38) >> 3; | |
1587 | bfd_put_8 (abfd, modrm, contents + roff - 1); | |
1588 | opcode = 0xc7; | |
1589 | } | |
1590 | else | |
1591 | { | |
1592 | /* Convert "mov foo@GOT(%reg1), %reg2" to | |
1593 | "lea foo@GOTOFF(%reg1), %reg2". */ | |
1594 | r_type = R_386_GOTOFF; | |
1595 | opcode = 0x8d; | |
1596 | } | |
1597 | } | |
1598 | else | |
1599 | { | |
1600 | /* Only R_386_32 is supported. */ | |
1601 | if (!to_reloc_32) | |
1602 | return TRUE; | |
1603 | ||
1604 | if (opcode == 0x85) | |
1605 | { | |
1606 | /* Convert "test %reg1, foo@GOT(%reg2)" to | |
1607 | "test $foo, %reg1". */ | |
1608 | modrm = 0xc0 | (modrm & 0x38) >> 3; | |
1609 | opcode = 0xf7; | |
1610 | } | |
1611 | else | |
1612 | { | |
1613 | /* Convert "binop foo@GOT(%reg1), %reg2" to | |
1614 | "binop $foo, %reg2". */ | |
1615 | modrm = (0xc0 | |
1616 | | (modrm & 0x38) >> 3 | |
1617 | | (opcode & 0x3c)); | |
1618 | opcode = 0x81; | |
1619 | } | |
1620 | bfd_put_8 (abfd, modrm, contents + roff - 1); | |
1621 | r_type = R_386_32; | |
1622 | } | |
1623 | ||
1624 | bfd_put_8 (abfd, opcode, contents + roff - 2); | |
1625 | irel->r_info = ELF32_R_INFO (r_symndx, r_type); | |
1626 | ||
1627 | *converted = TRUE; | |
1628 | } | |
1629 | } | |
1630 | ||
1631 | return TRUE; | |
1632 | } | |
1633 | ||
c1d11331 L |
1634 | /* Rename some of the generic section flags to better document how they |
1635 | are used here. */ | |
c337a162 L |
1636 | #define need_convert_load sec_flg0 |
1637 | #define check_relocs_failed sec_flg1 | |
c1d11331 | 1638 | |
252b5132 | 1639 | /* Look through the relocs for a section during the first phase, and |
0ac8d2ca AM |
1640 | calculate needed space in the global offset table, procedure linkage |
1641 | table, and dynamic reloc sections. */ | |
252b5132 | 1642 | |
b34976b6 | 1643 | static bfd_boolean |
55fd94b0 AM |
1644 | elf_i386_check_relocs (bfd *abfd, |
1645 | struct bfd_link_info *info, | |
1646 | asection *sec, | |
1647 | const Elf_Internal_Rela *relocs) | |
252b5132 | 1648 | { |
0afcef53 | 1649 | struct elf_x86_link_hash_table *htab; |
252b5132 RH |
1650 | Elf_Internal_Shdr *symtab_hdr; |
1651 | struct elf_link_hash_entry **sym_hashes; | |
252b5132 RH |
1652 | const Elf_Internal_Rela *rel; |
1653 | const Elf_Internal_Rela *rel_end; | |
252b5132 | 1654 | asection *sreloc; |
bedfd056 | 1655 | bfd_byte *contents; |
252b5132 | 1656 | |
0e1862bb | 1657 | if (bfd_link_relocatable (info)) |
b34976b6 | 1658 | return TRUE; |
252b5132 | 1659 | |
081b1afe L |
1660 | /* Don't do anything special with non-loaded, non-alloced sections. |
1661 | In particular, any relocs in such sections should not affect GOT | |
1662 | and PLT reference counting (ie. we don't allow them to create GOT | |
1663 | or PLT entries), there's no possibility or desire to optimize TLS | |
1664 | relocs, and there's not much point in propagating relocs to shared | |
1665 | libs that the dynamic linker won't relocate. */ | |
1666 | if ((sec->flags & SEC_ALLOC) == 0) | |
1667 | return TRUE; | |
1668 | ||
0ffa91dd NC |
1669 | BFD_ASSERT (is_i386_elf (abfd)); |
1670 | ||
0afcef53 | 1671 | htab = elf_x86_hash_table (info, I386_ELF_DATA); |
4dfe6ac6 | 1672 | if (htab == NULL) |
c337a162 L |
1673 | { |
1674 | sec->check_relocs_failed = 1; | |
1675 | return FALSE; | |
1676 | } | |
4dfe6ac6 | 1677 | |
bedfd056 L |
1678 | /* Get the section contents. */ |
1679 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
1680 | contents = elf_section_data (sec)->this_hdr.contents; | |
1681 | else if (!bfd_malloc_and_get_section (abfd, sec, &contents)) | |
1682 | { | |
1683 | sec->check_relocs_failed = 1; | |
1684 | return FALSE; | |
1685 | } | |
1686 | ||
0ffa91dd | 1687 | symtab_hdr = &elf_symtab_hdr (abfd); |
252b5132 | 1688 | sym_hashes = elf_sym_hashes (abfd); |
252b5132 | 1689 | |
252b5132 RH |
1690 | sreloc = NULL; |
1691 | ||
1692 | rel_end = relocs + sec->reloc_count; | |
1693 | for (rel = relocs; rel < rel_end; rel++) | |
1694 | { | |
13ae64f3 | 1695 | unsigned int r_type; |
d42c267e | 1696 | unsigned int r_symndx; |
252b5132 | 1697 | struct elf_link_hash_entry *h; |
0afcef53 | 1698 | struct elf_x86_link_hash_entry *eh; |
4c544807 L |
1699 | Elf_Internal_Sym *isym; |
1700 | const char *name; | |
06a6a421 | 1701 | bfd_boolean size_reloc; |
252b5132 RH |
1702 | |
1703 | r_symndx = ELF32_R_SYM (rel->r_info); | |
13ae64f3 | 1704 | r_type = ELF32_R_TYPE (rel->r_info); |
252b5132 | 1705 | |
d9bc7a44 | 1706 | if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) |
f5f31454 | 1707 | { |
695344c0 | 1708 | /* xgettext:c-format */ |
4eca0228 AM |
1709 | _bfd_error_handler (_("%B: bad symbol index: %d"), |
1710 | abfd, r_symndx); | |
c337a162 | 1711 | goto error_return; |
f5f31454 L |
1712 | } |
1713 | ||
252b5132 | 1714 | if (r_symndx < symtab_hdr->sh_info) |
c25bc9fc L |
1715 | { |
1716 | /* A local symbol. */ | |
c2e61a4e L |
1717 | isym = bfd_sym_from_r_symndx (&htab->sym_cache, |
1718 | abfd, r_symndx); | |
1719 | if (isym == NULL) | |
c337a162 | 1720 | goto error_return; |
c25bc9fc L |
1721 | |
1722 | /* Check relocation against local STT_GNU_IFUNC symbol. */ | |
c25bc9fc L |
1723 | if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC) |
1724 | { | |
0afcef53 | 1725 | h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, TRUE); |
c25bc9fc | 1726 | if (h == NULL) |
c337a162 | 1727 | goto error_return; |
6bbec505 | 1728 | |
c25bc9fc | 1729 | /* Fake a STT_GNU_IFUNC symbol. */ |
6322e5c5 L |
1730 | h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr, |
1731 | isym, NULL); | |
c25bc9fc L |
1732 | h->type = STT_GNU_IFUNC; |
1733 | h->def_regular = 1; | |
1734 | h->ref_regular = 1; | |
1735 | h->forced_local = 1; | |
1736 | h->root.type = bfd_link_hash_defined; | |
1737 | } | |
1738 | else | |
1739 | h = NULL; | |
1740 | } | |
252b5132 | 1741 | else |
71cb9464 | 1742 | { |
4c544807 | 1743 | isym = NULL; |
71cb9464 L |
1744 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
1745 | while (h->root.type == bfd_link_hash_indirect | |
1746 | || h->root.type == bfd_link_hash_warning) | |
1747 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
c25bc9fc | 1748 | } |
cbe950e9 | 1749 | |
0afcef53 | 1750 | eh = (struct elf_x86_link_hash_entry *) h; |
c25bc9fc L |
1751 | if (h != NULL) |
1752 | { | |
f604c2a2 L |
1753 | if (r_type == R_386_GOTOFF) |
1754 | eh->gotoff_ref = 1; | |
cbe950e9 | 1755 | |
ad1e85de L |
1756 | /* It is referenced by a non-shared object. */ |
1757 | h->ref_regular = 1; | |
bc4e12de | 1758 | h->root.non_ir_ref_regular = 1; |
13a2df29 L |
1759 | |
1760 | if (h->type == STT_GNU_IFUNC) | |
1761 | elf_tdata (info->output_bfd)->has_gnu_symbols | |
1762 | |= elf_gnu_symbol_ifunc; | |
71cb9464 | 1763 | } |
252b5132 | 1764 | |
bedfd056 | 1765 | if (! elf_i386_tls_transition (info, abfd, sec, contents, |
142411ca L |
1766 | symtab_hdr, sym_hashes, |
1767 | &r_type, GOT_UNKNOWN, | |
bedfd056 | 1768 | rel, rel_end, h, r_symndx, FALSE)) |
c337a162 | 1769 | goto error_return; |
13ae64f3 JJ |
1770 | |
1771 | switch (r_type) | |
252b5132 | 1772 | { |
37e55690 | 1773 | case R_386_TLS_LDM: |
0afcef53 | 1774 | htab->tls_ld_or_ldm_got.refcount += 1; |
37e55690 JJ |
1775 | goto create_got; |
1776 | ||
1777 | case R_386_PLT32: | |
1778 | /* This symbol requires a procedure linkage table entry. We | |
1779 | actually build the entry in adjust_dynamic_symbol, | |
1780 | because this might be a case of linking PIC code which is | |
1781 | never referenced by a dynamic object, in which case we | |
1782 | don't need to generate a procedure linkage table entry | |
1783 | after all. */ | |
1784 | ||
1785 | /* If this is a local symbol, we resolve it directly without | |
1786 | creating a procedure linkage table entry. */ | |
1787 | if (h == NULL) | |
1788 | continue; | |
1789 | ||
aec6b87e | 1790 | eh->has_got_reloc = 1; |
f5385ebf | 1791 | h->needs_plt = 1; |
37e55690 JJ |
1792 | h->plt.refcount += 1; |
1793 | break; | |
1794 | ||
6a3e1bae | 1795 | case R_386_SIZE32: |
06a6a421 | 1796 | size_reloc = TRUE; |
6a3e1bae L |
1797 | goto do_size; |
1798 | ||
13ae64f3 | 1799 | case R_386_TLS_IE_32: |
37e55690 JJ |
1800 | case R_386_TLS_IE: |
1801 | case R_386_TLS_GOTIE: | |
0e1862bb | 1802 | if (!bfd_link_executable (info)) |
13ae64f3 | 1803 | info->flags |= DF_STATIC_TLS; |
37e55690 JJ |
1804 | /* Fall through */ |
1805 | ||
252b5132 | 1806 | case R_386_GOT32: |
02a86693 | 1807 | case R_386_GOT32X: |
13ae64f3 | 1808 | case R_386_TLS_GD: |
67a4f2b7 AO |
1809 | case R_386_TLS_GOTDESC: |
1810 | case R_386_TLS_DESC_CALL: | |
252b5132 | 1811 | /* This symbol requires a global offset table entry. */ |
13ae64f3 JJ |
1812 | { |
1813 | int tls_type, old_tls_type; | |
1814 | ||
1815 | switch (r_type) | |
1816 | { | |
1817 | default: | |
02a86693 L |
1818 | case R_386_GOT32: |
1819 | case R_386_GOT32X: | |
1820 | tls_type = GOT_NORMAL; | |
1821 | break; | |
13ae64f3 | 1822 | case R_386_TLS_GD: tls_type = GOT_TLS_GD; break; |
67a4f2b7 AO |
1823 | case R_386_TLS_GOTDESC: |
1824 | case R_386_TLS_DESC_CALL: | |
1825 | tls_type = GOT_TLS_GDESC; break; | |
37e55690 JJ |
1826 | case R_386_TLS_IE_32: |
1827 | if (ELF32_R_TYPE (rel->r_info) == r_type) | |
1828 | tls_type = GOT_TLS_IE_NEG; | |
1829 | else | |
ebcfb3c0 JJ |
1830 | /* If this is a GD->IE transition, we may use either of |
1831 | R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */ | |
37e55690 JJ |
1832 | tls_type = GOT_TLS_IE; |
1833 | break; | |
1834 | case R_386_TLS_IE: | |
1835 | case R_386_TLS_GOTIE: | |
1836 | tls_type = GOT_TLS_IE_POS; break; | |
13ae64f3 JJ |
1837 | } |
1838 | ||
1839 | if (h != NULL) | |
1840 | { | |
1841 | h->got.refcount += 1; | |
0afcef53 | 1842 | old_tls_type = elf_x86_hash_entry (h)->tls_type; |
13ae64f3 JJ |
1843 | } |
1844 | else | |
1845 | { | |
1846 | bfd_signed_vma *local_got_refcounts; | |
1847 | ||
1848 | /* This is a global offset table entry for a local symbol. */ | |
1849 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
1850 | if (local_got_refcounts == NULL) | |
1851 | { | |
1852 | bfd_size_type size; | |
1853 | ||
1854 | size = symtab_hdr->sh_info; | |
67a4f2b7 AO |
1855 | size *= (sizeof (bfd_signed_vma) |
1856 | + sizeof (bfd_vma) + sizeof(char)); | |
a50b1753 NC |
1857 | local_got_refcounts = (bfd_signed_vma *) |
1858 | bfd_zalloc (abfd, size); | |
13ae64f3 | 1859 | if (local_got_refcounts == NULL) |
c337a162 | 1860 | goto error_return; |
13ae64f3 | 1861 | elf_local_got_refcounts (abfd) = local_got_refcounts; |
0afcef53 | 1862 | elf_x86_local_tlsdesc_gotent (abfd) |
67a4f2b7 | 1863 | = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info); |
0afcef53 | 1864 | elf_x86_local_got_tls_type (abfd) |
67a4f2b7 | 1865 | = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info); |
13ae64f3 JJ |
1866 | } |
1867 | local_got_refcounts[r_symndx] += 1; | |
0afcef53 | 1868 | old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx]; |
13ae64f3 JJ |
1869 | } |
1870 | ||
37e55690 JJ |
1871 | if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE)) |
1872 | tls_type |= old_tls_type; | |
13ae64f3 JJ |
1873 | /* If a TLS symbol is accessed using IE at least once, |
1874 | there is no point to use dynamic model for it. */ | |
ebcfb3c0 | 1875 | else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN |
67a4f2b7 | 1876 | && (! GOT_TLS_GD_ANY_P (old_tls_type) |
37e55690 | 1877 | || (tls_type & GOT_TLS_IE) == 0)) |
13ae64f3 | 1878 | { |
67a4f2b7 | 1879 | if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type)) |
37e55690 | 1880 | tls_type = old_tls_type; |
67a4f2b7 AO |
1881 | else if (GOT_TLS_GD_ANY_P (old_tls_type) |
1882 | && GOT_TLS_GD_ANY_P (tls_type)) | |
1883 | tls_type |= old_tls_type; | |
13ae64f3 JJ |
1884 | else |
1885 | { | |
09a24cbf | 1886 | if (h) |
4c544807 L |
1887 | name = h->root.root.string; |
1888 | else | |
1889 | name = bfd_elf_sym_name (abfd, symtab_hdr, isym, | |
1890 | NULL); | |
4eca0228 | 1891 | _bfd_error_handler |
695344c0 | 1892 | /* xgettext:c-format */ |
d003868e | 1893 | (_("%B: `%s' accessed both as normal and " |
55fd94b0 | 1894 | "thread local symbol"), |
4c544807 | 1895 | abfd, name); |
68c4a57e | 1896 | bfd_set_error (bfd_error_bad_value); |
c337a162 | 1897 | goto error_return; |
13ae64f3 JJ |
1898 | } |
1899 | } | |
1900 | ||
1901 | if (old_tls_type != tls_type) | |
1902 | { | |
1903 | if (h != NULL) | |
0afcef53 | 1904 | elf_x86_hash_entry (h)->tls_type = tls_type; |
13ae64f3 | 1905 | else |
0afcef53 | 1906 | elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type; |
13ae64f3 JJ |
1907 | } |
1908 | } | |
0ac8d2ca AM |
1909 | /* Fall through */ |
1910 | ||
1911 | case R_386_GOTOFF: | |
1912 | case R_386_GOTPC: | |
13ae64f3 | 1913 | create_got: |
37e55690 | 1914 | if (r_type != R_386_TLS_IE) |
aec6b87e L |
1915 | { |
1916 | if (eh != NULL) | |
1917 | eh->has_got_reloc = 1; | |
1918 | break; | |
1919 | } | |
37e55690 | 1920 | /* Fall through */ |
252b5132 | 1921 | |
37e55690 JJ |
1922 | case R_386_TLS_LE_32: |
1923 | case R_386_TLS_LE: | |
aec6b87e L |
1924 | if (eh != NULL) |
1925 | eh->has_got_reloc = 1; | |
0e1862bb | 1926 | if (bfd_link_executable (info)) |
37e55690 | 1927 | break; |
bffbf940 | 1928 | info->flags |= DF_STATIC_TLS; |
aec6b87e | 1929 | goto do_relocation; |
252b5132 RH |
1930 | |
1931 | case R_386_32: | |
1932 | case R_386_PC32: | |
aec6b87e L |
1933 | if (eh != NULL && (sec->flags & SEC_CODE) != 0) |
1934 | eh->has_non_got_reloc = 1; | |
1935 | do_relocation: | |
bf52d7c7 L |
1936 | /* We are called after all symbols have been resolved. Only |
1937 | relocation against STT_GNU_IFUNC symbol must go through | |
1938 | PLT. */ | |
d1ed1c7d L |
1939 | if (h != NULL |
1940 | && (bfd_link_executable (info) | |
bf52d7c7 | 1941 | || h->type == STT_GNU_IFUNC)) |
6725bdbf | 1942 | { |
12d0ee4a | 1943 | /* If this reloc is in a read-only section, we might |
ebe50bae AM |
1944 | need a copy reloc. We can't check reliably at this |
1945 | stage whether the section is read-only, as input | |
1946 | sections have not yet been mapped to output sections. | |
1947 | Tentatively set the flag for now, and correct in | |
1948 | adjust_dynamic_symbol. */ | |
f5385ebf | 1949 | h->non_got_ref = 1; |
12d0ee4a | 1950 | |
233cc9c1 L |
1951 | /* We may need a .plt entry if the symbol is a function |
1952 | defined in a shared lib or is a STT_GNU_IFUNC function | |
1953 | referenced from the code or read-only section. */ | |
34bb2571 L |
1954 | if (!h->def_regular |
1955 | || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0) | |
233cc9c1 L |
1956 | h->plt.refcount += 1; |
1957 | ||
4b627c18 L |
1958 | if (r_type == R_386_PC32) |
1959 | { | |
1960 | /* Since something like ".long foo - ." may be used | |
1961 | as pointer, make sure that PLT is used if foo is | |
1962 | a function defined in a shared library. */ | |
1963 | if ((sec->flags & SEC_CODE) == 0) | |
1964 | h->pointer_equality_needed = 1; | |
74437ea2 L |
1965 | else if (h->type == STT_GNU_IFUNC |
1966 | && bfd_link_pic (info)) | |
1967 | { | |
6322e5c5 L |
1968 | _bfd_error_handler |
1969 | /* xgettext:c-format */ | |
1970 | (_("%B: unsupported non-PIC call to IFUNC `%s'"), | |
1971 | abfd, h->root.root.string); | |
74437ea2 L |
1972 | bfd_set_error (bfd_error_bad_value); |
1973 | goto error_return; | |
1974 | } | |
4b627c18 L |
1975 | } |
1976 | else | |
04ebc307 L |
1977 | { |
1978 | h->pointer_equality_needed = 1; | |
1979 | /* R_386_32 can be resolved at run-time. */ | |
1980 | if (r_type == R_386_32 | |
1981 | && (sec->flags & SEC_READONLY) == 0) | |
1982 | eh->func_pointer_refcount += 1; | |
1983 | } | |
6725bdbf | 1984 | } |
7843f00e | 1985 | |
06a6a421 | 1986 | size_reloc = FALSE; |
6a3e1bae | 1987 | do_size: |
252b5132 | 1988 | /* If we are creating a shared library, and this is a reloc |
f69da49f AM |
1989 | against a global symbol, or a non PC relative reloc |
1990 | against a local symbol, then we need to copy the reloc | |
1991 | into the shared library. However, if we are linking with | |
1992 | -Bsymbolic, we do not need to copy a reloc against a | |
1993 | global symbol which is defined in an object we are | |
1994 | including in the link (i.e., DEF_REGULAR is set). At | |
1995 | this point we have not seen all the input files, so it is | |
1996 | possible that DEF_REGULAR is not set now but will be set | |
1f655a09 L |
1997 | later (it is never cleared). In case of a weak definition, |
1998 | DEF_REGULAR may be cleared later by a strong definition in | |
ebe50bae | 1999 | a shared library. We account for that possibility below by |
1f655a09 L |
2000 | storing information in the relocs_copied field of the hash |
2001 | table entry. A similar situation occurs when creating | |
2002 | shared libraries and symbol visibility changes render the | |
12d0ee4a | 2003 | symbol local. |
56882138 | 2004 | |
12d0ee4a AM |
2005 | If on the other hand, we are creating an executable, we |
2006 | may need to keep relocations for symbols satisfied by a | |
2007 | dynamic library if we manage to avoid copy relocs for the | |
233cc9c1 L |
2008 | symbol. |
2009 | ||
2010 | Generate dynamic pointer relocation against STT_GNU_IFUNC | |
2011 | symbol in the non-code section. */ | |
081b1afe L |
2012 | if ((bfd_link_pic (info) |
2013 | && (r_type != R_386_PC32 | |
2014 | || (h != NULL | |
2015 | && (! (bfd_link_pie (info) | |
2016 | || SYMBOLIC_BIND (info, h)) | |
2017 | || h->root.type == bfd_link_hash_defweak | |
2018 | || !h->def_regular)))) | |
233cc9c1 L |
2019 | || (h != NULL |
2020 | && h->type == STT_GNU_IFUNC | |
2021 | && r_type == R_386_32 | |
2022 | && (sec->flags & SEC_CODE) == 0) | |
081b1afe L |
2023 | || (ELIMINATE_COPY_RELOCS |
2024 | && !bfd_link_pic (info) | |
2025 | && h != NULL | |
2026 | && (h->root.type == bfd_link_hash_defweak | |
2027 | || !h->def_regular))) | |
252b5132 | 2028 | { |
e03a8ed8 L |
2029 | struct elf_dyn_relocs *p; |
2030 | struct elf_dyn_relocs **head; | |
ec338859 | 2031 | |
12d0ee4a AM |
2032 | /* We must copy these reloc types into the output file. |
2033 | Create a reloc section in dynobj and make room for | |
2034 | this reloc. */ | |
252b5132 RH |
2035 | if (sreloc == NULL) |
2036 | { | |
83bac4b0 NC |
2037 | sreloc = _bfd_elf_make_dynamic_reloc_section |
2038 | (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE); | |
2039 | ||
252b5132 | 2040 | if (sreloc == NULL) |
c337a162 | 2041 | goto error_return; |
252b5132 RH |
2042 | } |
2043 | ||
0c715baa AM |
2044 | /* If this is a global symbol, we count the number of |
2045 | relocations we need for this symbol. */ | |
2046 | if (h != NULL) | |
252b5132 | 2047 | { |
d5597ebc | 2048 | head = &eh->dyn_relocs; |
0c715baa AM |
2049 | } |
2050 | else | |
2051 | { | |
ec338859 AM |
2052 | /* Track dynamic relocs needed for local syms too. |
2053 | We really need local syms available to do this | |
2054 | easily. Oh well. */ | |
87d72d41 | 2055 | void **vpp; |
ec338859 | 2056 | asection *s; |
87d72d41 AM |
2057 | |
2058 | isym = bfd_sym_from_r_symndx (&htab->sym_cache, | |
2059 | abfd, r_symndx); | |
2060 | if (isym == NULL) | |
c337a162 | 2061 | goto error_return; |
87d72d41 AM |
2062 | |
2063 | s = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
ec338859 | 2064 | if (s == NULL) |
87d72d41 | 2065 | s = sec; |
ec338859 | 2066 | |
e81d3500 | 2067 | vpp = &elf_section_data (s)->local_dynrel; |
e03a8ed8 | 2068 | head = (struct elf_dyn_relocs **)vpp; |
ec338859 AM |
2069 | } |
2070 | ||
2071 | p = *head; | |
2072 | if (p == NULL || p->sec != sec) | |
2073 | { | |
2074 | bfd_size_type amt = sizeof *p; | |
a50b1753 NC |
2075 | p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj, |
2076 | amt); | |
ec338859 | 2077 | if (p == NULL) |
c337a162 | 2078 | goto error_return; |
ec338859 AM |
2079 | p->next = *head; |
2080 | *head = p; | |
2081 | p->sec = sec; | |
2082 | p->count = 0; | |
2083 | p->pc_count = 0; | |
252b5132 | 2084 | } |
ec338859 AM |
2085 | |
2086 | p->count += 1; | |
06a6a421 L |
2087 | /* Count size relocation as PC-relative relocation. */ |
2088 | if (r_type == R_386_PC32 || size_reloc) | |
ec338859 | 2089 | p->pc_count += 1; |
252b5132 | 2090 | } |
252b5132 RH |
2091 | break; |
2092 | ||
2093 | /* This relocation describes the C++ object vtable hierarchy. | |
2094 | Reconstruct it for later use during GC. */ | |
2095 | case R_386_GNU_VTINHERIT: | |
d17e0c6e | 2096 | if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) |
c337a162 | 2097 | goto error_return; |
252b5132 RH |
2098 | break; |
2099 | ||
2100 | /* This relocation describes which C++ vtable entries are actually | |
2101 | used. Record for later use during GC. */ | |
2102 | case R_386_GNU_VTENTRY: | |
c6aa130f MS |
2103 | BFD_ASSERT (h != NULL); |
2104 | if (h != NULL | |
2105 | && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset)) | |
c337a162 | 2106 | goto error_return; |
252b5132 RH |
2107 | break; |
2108 | ||
2109 | default: | |
2110 | break; | |
2111 | } | |
dd7e64d4 | 2112 | |
52bf37dd | 2113 | if (r_type == R_386_GOT32X |
c1d11331 | 2114 | && (h == NULL || h->type != STT_GNU_IFUNC)) |
02a86693 | 2115 | sec->need_convert_load = 1; |
252b5132 RH |
2116 | } |
2117 | ||
bedfd056 L |
2118 | if (elf_section_data (sec)->this_hdr.contents != contents) |
2119 | { | |
2120 | if (!info->keep_memory) | |
2121 | free (contents); | |
2122 | else | |
2123 | { | |
2124 | /* Cache the section contents for elf_link_input_bfd. */ | |
2125 | elf_section_data (sec)->this_hdr.contents = contents; | |
2126 | } | |
2127 | } | |
2128 | ||
b34976b6 | 2129 | return TRUE; |
c337a162 L |
2130 | |
2131 | error_return: | |
bedfd056 L |
2132 | if (elf_section_data (sec)->this_hdr.contents != contents) |
2133 | free (contents); | |
c337a162 L |
2134 | sec->check_relocs_failed = 1; |
2135 | return FALSE; | |
252b5132 RH |
2136 | } |
2137 | ||
2138 | /* Return the section that should be marked against GC for a given | |
2139 | relocation. */ | |
2140 | ||
2141 | static asection * | |
55fd94b0 | 2142 | elf_i386_gc_mark_hook (asection *sec, |
07adf181 | 2143 | struct bfd_link_info *info, |
55fd94b0 AM |
2144 | Elf_Internal_Rela *rel, |
2145 | struct elf_link_hash_entry *h, | |
2146 | Elf_Internal_Sym *sym) | |
252b5132 RH |
2147 | { |
2148 | if (h != NULL) | |
07adf181 AM |
2149 | switch (ELF32_R_TYPE (rel->r_info)) |
2150 | { | |
2151 | case R_386_GNU_VTINHERIT: | |
2152 | case R_386_GNU_VTENTRY: | |
2153 | return NULL; | |
2154 | } | |
2155 | ||
2156 | return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym); | |
252b5132 RH |
2157 | } |
2158 | ||
252b5132 RH |
2159 | /* Adjust a symbol defined by a dynamic object and referenced by a |
2160 | regular object. The current definition is in some section of the | |
2161 | dynamic object, but we're not including those sections. We have to | |
2162 | change the definition to something the rest of the link can | |
2163 | understand. */ | |
2164 | ||
b34976b6 | 2165 | static bfd_boolean |
55fd94b0 AM |
2166 | elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info, |
2167 | struct elf_link_hash_entry *h) | |
252b5132 | 2168 | { |
0afcef53 | 2169 | struct elf_x86_link_hash_table *htab; |
5474d94f | 2170 | asection *s, *srel; |
0afcef53 | 2171 | struct elf_x86_link_hash_entry *eh; |
5ca5bb35 | 2172 | struct elf_dyn_relocs *p; |
252b5132 | 2173 | |
cbe950e9 L |
2174 | /* STT_GNU_IFUNC symbol must go through PLT. */ |
2175 | if (h->type == STT_GNU_IFUNC) | |
2176 | { | |
73bcf233 L |
2177 | /* All local STT_GNU_IFUNC references must be treate as local |
2178 | calls via local PLT. */ | |
5ca5bb35 L |
2179 | if (h->ref_regular |
2180 | && SYMBOL_CALLS_LOCAL (info, h)) | |
2181 | { | |
73bcf233 | 2182 | bfd_size_type pc_count = 0, count = 0; |
5ca5bb35 L |
2183 | struct elf_dyn_relocs **pp; |
2184 | ||
0afcef53 | 2185 | eh = (struct elf_x86_link_hash_entry *) h; |
5ca5bb35 L |
2186 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) |
2187 | { | |
2188 | pc_count += p->pc_count; | |
2189 | p->count -= p->pc_count; | |
2190 | p->pc_count = 0; | |
73bcf233 | 2191 | count += p->count; |
5ca5bb35 L |
2192 | if (p->count == 0) |
2193 | *pp = p->next; | |
2194 | else | |
2195 | pp = &p->next; | |
2196 | } | |
2197 | ||
73bcf233 | 2198 | if (pc_count || count) |
5ca5bb35 | 2199 | { |
5ca5bb35 | 2200 | h->non_got_ref = 1; |
233cc9c1 L |
2201 | if (pc_count) |
2202 | { | |
2203 | /* Increment PLT reference count only for PC-relative | |
2204 | references. */ | |
2205 | h->needs_plt = 1; | |
2206 | if (h->plt.refcount <= 0) | |
2207 | h->plt.refcount = 1; | |
2208 | else | |
2209 | h->plt.refcount += 1; | |
2210 | } | |
5ca5bb35 L |
2211 | } |
2212 | } | |
2213 | ||
cbe950e9 L |
2214 | if (h->plt.refcount <= 0) |
2215 | { | |
2216 | h->plt.offset = (bfd_vma) -1; | |
2217 | h->needs_plt = 0; | |
2218 | } | |
2219 | return TRUE; | |
2220 | } | |
2221 | ||
252b5132 RH |
2222 | /* If this is a function, put it in the procedure linkage table. We |
2223 | will fill in the contents of the procedure linkage table later, | |
2224 | when we know the address of the .got section. */ | |
2225 | if (h->type == STT_FUNC | |
f5385ebf | 2226 | || h->needs_plt) |
252b5132 | 2227 | { |
6725bdbf | 2228 | if (h->plt.refcount <= 0 |
9c7a29a3 AM |
2229 | || SYMBOL_CALLS_LOCAL (info, h) |
2230 | || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
2231 | && h->root.type == bfd_link_hash_undefweak)) | |
252b5132 RH |
2232 | { |
2233 | /* This case can occur if we saw a PLT32 reloc in an input | |
dd5724d5 AM |
2234 | file, but the symbol was never referred to by a dynamic |
2235 | object, or if all references were garbage collected. In | |
2236 | such a case, we don't actually need to build a procedure | |
2237 | linkage table, and we can just do a PC32 reloc instead. */ | |
bbd7ec4a | 2238 | h->plt.offset = (bfd_vma) -1; |
f5385ebf | 2239 | h->needs_plt = 0; |
252b5132 RH |
2240 | } |
2241 | ||
b34976b6 | 2242 | return TRUE; |
252b5132 | 2243 | } |
6725bdbf AM |
2244 | else |
2245 | /* It's possible that we incorrectly decided a .plt reloc was | |
2246 | needed for an R_386_PC32 reloc to a non-function sym in | |
2247 | check_relocs. We can't decide accurately between function and | |
2248 | non-function syms in check-relocs; Objects loaded later in | |
2249 | the link may change h->type. So fix it now. */ | |
bbd7ec4a | 2250 | h->plt.offset = (bfd_vma) -1; |
252b5132 | 2251 | |
0afcef53 | 2252 | eh = (struct elf_x86_link_hash_entry *) h; |
a5b4ee94 | 2253 | |
252b5132 RH |
2254 | /* If this is a weak symbol, and there is a real definition, the |
2255 | processor independent code will have arranged for us to see the | |
2256 | real definition first, and we can just use the same value. */ | |
f6e332e6 | 2257 | if (h->u.weakdef != NULL) |
252b5132 | 2258 | { |
f6e332e6 AM |
2259 | BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined |
2260 | || h->u.weakdef->root.type == bfd_link_hash_defweak); | |
2261 | h->root.u.def.section = h->u.weakdef->root.u.def.section; | |
2262 | h->root.u.def.value = h->u.weakdef->root.u.def.value; | |
a5b4ee94 L |
2263 | if (ELIMINATE_COPY_RELOCS |
2264 | || info->nocopyreloc | |
2265 | || SYMBOL_NO_COPYRELOC (info, eh)) | |
f6e332e6 | 2266 | h->non_got_ref = h->u.weakdef->non_got_ref; |
b34976b6 | 2267 | return TRUE; |
252b5132 RH |
2268 | } |
2269 | ||
2270 | /* This is a reference to a symbol defined by a dynamic object which | |
2271 | is not a function. */ | |
2272 | ||
2273 | /* If we are creating a shared library, we must presume that the | |
2274 | only references to the symbol are via the global offset table. | |
2275 | For such cases we need not do anything here; the relocations will | |
2276 | be handled correctly by relocate_section. */ | |
0e1862bb | 2277 | if (!bfd_link_executable (info)) |
b34976b6 | 2278 | return TRUE; |
252b5132 | 2279 | |
7843f00e | 2280 | /* If there are no references to this symbol that do not use the |
d5597ebc L |
2281 | GOT nor R_386_GOTOFF relocation, we don't need to generate a copy |
2282 | reloc. */ | |
d5597ebc | 2283 | if (!h->non_got_ref && !eh->gotoff_ref) |
b34976b6 | 2284 | return TRUE; |
7843f00e | 2285 | |
8bd621d8 | 2286 | /* If -z nocopyreloc was given, we won't generate them either. */ |
a5b4ee94 | 2287 | if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh)) |
8bd621d8 | 2288 | { |
f5385ebf | 2289 | h->non_got_ref = 0; |
b34976b6 | 2290 | return TRUE; |
8bd621d8 AM |
2291 | } |
2292 | ||
0afcef53 | 2293 | htab = elf_x86_hash_table (info, I386_ELF_DATA); |
4dfe6ac6 NC |
2294 | if (htab == NULL) |
2295 | return FALSE; | |
643796e3 | 2296 | |
d5597ebc L |
2297 | /* If there aren't any dynamic relocs in read-only sections nor |
2298 | R_386_GOTOFF relocation, then we can keep the dynamic relocs and | |
2299 | avoid the copy reloc. This doesn't work on VxWorks, where we can | |
2300 | not have dynamic relocations (other than copy and jump slot | |
2301 | relocations) in an executable. */ | |
23209a78 | 2302 | if (ELIMINATE_COPY_RELOCS |
d5597ebc | 2303 | && !eh->gotoff_ref |
f604c2a2 | 2304 | && get_elf_i386_backend_data (info->output_bfd)->os != is_vxworks) |
ebe50bae | 2305 | { |
a23b6845 AM |
2306 | for (p = eh->dyn_relocs; p != NULL; p = p->next) |
2307 | { | |
2308 | s = p->sec->output_section; | |
2309 | if (s != NULL && (s->flags & SEC_READONLY) != 0) | |
2310 | break; | |
2311 | } | |
2312 | ||
a23b6845 AM |
2313 | if (p == NULL) |
2314 | { | |
f5385ebf | 2315 | h->non_got_ref = 0; |
a23b6845 AM |
2316 | return TRUE; |
2317 | } | |
ebe50bae AM |
2318 | } |
2319 | ||
252b5132 RH |
2320 | /* We must allocate the symbol in our .dynbss section, which will |
2321 | become part of the .bss section of the executable. There will be | |
2322 | an entry for this symbol in the .dynsym section. The dynamic | |
2323 | object will contain position independent code, so all references | |
2324 | from the dynamic object to this symbol will go through the global | |
2325 | offset table. The dynamic linker will use the .dynsym entry to | |
2326 | determine the address it must put in the global offset table, so | |
2327 | both the dynamic object and the regular object will refer to the | |
2328 | same memory location for the variable. */ | |
2329 | ||
252b5132 RH |
2330 | /* We must generate a R_386_COPY reloc to tell the dynamic linker to |
2331 | copy the initial value out of the dynamic object and into the | |
0ac8d2ca | 2332 | runtime process image. */ |
5474d94f AM |
2333 | if ((h->root.u.def.section->flags & SEC_READONLY) != 0) |
2334 | { | |
2335 | s = htab->elf.sdynrelro; | |
2336 | srel = htab->elf.sreldynrelro; | |
2337 | } | |
2338 | else | |
2339 | { | |
2340 | s = htab->elf.sdynbss; | |
2341 | srel = htab->elf.srelbss; | |
2342 | } | |
1d7e9d18 | 2343 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0) |
252b5132 | 2344 | { |
5474d94f | 2345 | srel->size += sizeof (Elf32_External_Rel); |
f5385ebf | 2346 | h->needs_copy = 1; |
252b5132 RH |
2347 | } |
2348 | ||
6cabe1ea | 2349 | return _bfd_elf_adjust_dynamic_copy (info, h, s); |
252b5132 RH |
2350 | } |
2351 | ||
6725bdbf | 2352 | /* Allocate space in .plt, .got and associated reloc sections for |
0c715baa | 2353 | dynamic relocs. */ |
6725bdbf | 2354 | |
b34976b6 | 2355 | static bfd_boolean |
eb4ff4d6 | 2356 | elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) |
6725bdbf AM |
2357 | { |
2358 | struct bfd_link_info *info; | |
0afcef53 L |
2359 | struct elf_x86_link_hash_table *htab; |
2360 | struct elf_x86_link_hash_entry *eh; | |
e03a8ed8 | 2361 | struct elf_dyn_relocs *p; |
25e762b9 | 2362 | unsigned plt_entry_size; |
aec6b87e | 2363 | bfd_boolean resolved_to_zero; |
f604c2a2 | 2364 | const struct elf_i386_backend_data *bed; |
0afcef53 L |
2365 | const struct elf_i386_plt_layout *plt_layout; |
2366 | const struct elf_i386_non_lazy_plt_layout *non_lazy_plt_layout; | |
6725bdbf | 2367 | |
e92d460e | 2368 | if (h->root.type == bfd_link_hash_indirect) |
b34976b6 | 2369 | return TRUE; |
6725bdbf | 2370 | |
0afcef53 | 2371 | eh = (struct elf_x86_link_hash_entry *) h; |
e92d460e | 2372 | |
6725bdbf | 2373 | info = (struct bfd_link_info *) inf; |
0afcef53 | 2374 | htab = elf_x86_hash_table (info, I386_ELF_DATA); |
4dfe6ac6 NC |
2375 | if (htab == NULL) |
2376 | return FALSE; | |
6725bdbf | 2377 | |
f604c2a2 L |
2378 | bed = get_elf_i386_backend_data (info->output_bfd); |
2379 | ||
0afcef53 L |
2380 | plt_layout = &elf_i386_plt (htab); |
2381 | non_lazy_plt_layout = elf_i386_non_lazy_plt (htab); | |
2382 | plt_entry_size = plt_layout->plt_entry_size; | |
25e762b9 | 2383 | |
e62b9723 | 2384 | resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, |
0afcef53 | 2385 | I386_ELF_DATA, |
e62b9723 L |
2386 | eh->has_got_reloc, |
2387 | eh); | |
aec6b87e | 2388 | |
04ebc307 L |
2389 | /* Clear the reference count of function pointer relocations if |
2390 | symbol isn't a normal function. */ | |
2391 | if (h->type != STT_FUNC) | |
2392 | eh->func_pointer_refcount = 0; | |
2393 | ||
dd7e64d4 L |
2394 | /* We can't use the GOT PLT if pointer equality is needed since |
2395 | finish_dynamic_symbol won't clear symbol value and the dynamic | |
2396 | linker won't update the GOT slot. We will get into an infinite | |
2397 | loop at run-time. */ | |
2398 | if (htab->plt_got != NULL | |
2399 | && h->type != STT_GNU_IFUNC | |
2400 | && !h->pointer_equality_needed | |
2401 | && h->plt.refcount > 0 | |
2402 | && h->got.refcount > 0) | |
2403 | { | |
2404 | /* Don't use the regular PLT if there are both GOT and GOTPLT | |
2405 | reloctions. */ | |
2406 | h->plt.offset = (bfd_vma) -1; | |
2407 | ||
2408 | /* Use the GOT PLT. */ | |
2409 | eh->plt_got.refcount = 1; | |
2410 | } | |
2411 | ||
cbe950e9 L |
2412 | /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it |
2413 | here if it is defined and referenced in a non-shared object. */ | |
2414 | if (h->type == STT_GNU_IFUNC | |
2415 | && h->def_regular) | |
ee2fdd6f L |
2416 | { |
2417 | if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs, | |
2418 | &htab->readonly_dynrelocs_against_ifunc, | |
2419 | plt_entry_size, | |
0afcef53 L |
2420 | (plt_layout->has_plt0 |
2421 | * plt_entry_size), | |
ee2fdd6f L |
2422 | 4, TRUE)) |
2423 | { | |
2424 | asection *s = htab->plt_second; | |
2425 | if (h->plt.offset != (bfd_vma) -1 && s != NULL) | |
2426 | { | |
2427 | /* Use the second PLT section if it is created. */ | |
2428 | eh->plt_second.offset = s->size; | |
2429 | ||
2430 | /* Make room for this entry in the second PLT section. */ | |
0afcef53 | 2431 | s->size += non_lazy_plt_layout->plt_entry_size; |
ee2fdd6f L |
2432 | } |
2433 | ||
2434 | return TRUE; | |
2435 | } | |
2436 | else | |
2437 | return FALSE; | |
2438 | } | |
04ebc307 L |
2439 | /* Don't create the PLT entry if there are only function pointer |
2440 | relocations which can be resolved at run-time. */ | |
cbe950e9 | 2441 | else if (htab->elf.dynamic_sections_created |
04ebc307 L |
2442 | && (h->plt.refcount > eh->func_pointer_refcount |
2443 | || eh->plt_got.refcount > 0)) | |
6725bdbf | 2444 | { |
dc2be3d2 | 2445 | bfd_boolean use_plt_got = eh->plt_got.refcount > 0; |
25070364 | 2446 | |
04ebc307 L |
2447 | /* Clear the reference count of function pointer relocations |
2448 | if PLT is used. */ | |
2449 | eh->func_pointer_refcount = 0; | |
2450 | ||
5a15f56f AM |
2451 | /* Make sure this symbol is output as a dynamic symbol. |
2452 | Undefined weak syms won't yet be marked as dynamic. */ | |
2453 | if (h->dynindx == -1 | |
aec6b87e | 2454 | && !h->forced_local |
0dc9a308 L |
2455 | && !resolved_to_zero |
2456 | && h->root.type == bfd_link_hash_undefweak) | |
5a15f56f | 2457 | { |
c152c796 | 2458 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 2459 | return FALSE; |
5a15f56f AM |
2460 | } |
2461 | ||
0e1862bb | 2462 | if (bfd_link_pic (info) |
4e795f50 | 2463 | || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h)) |
ced53ee5 | 2464 | { |
6de2ae4a | 2465 | asection *s = htab->elf.splt; |
ee2fdd6f | 2466 | asection *second_s = htab->plt_second; |
dd7e64d4 | 2467 | asection *got_s = htab->plt_got; |
6725bdbf | 2468 | |
7c1e8d3e L |
2469 | /* If this is the first .plt entry, make room for the special |
2470 | first entry. The .plt section is used by prelink to undo | |
2471 | prelinking for dynamic relocations. */ | |
2472 | if (s->size == 0) | |
0afcef53 | 2473 | s->size = plt_layout->has_plt0 * plt_entry_size; |
7c1e8d3e | 2474 | |
dd7e64d4 L |
2475 | if (use_plt_got) |
2476 | eh->plt_got.offset = got_s->size; | |
2477 | else | |
ee2fdd6f L |
2478 | { |
2479 | h->plt.offset = s->size; | |
2480 | if (second_s) | |
2481 | eh->plt_second.offset = second_s->size; | |
2482 | } | |
6725bdbf | 2483 | |
ced53ee5 AM |
2484 | /* If this symbol is not defined in a regular file, and we are |
2485 | not generating a shared library, then set the symbol to this | |
2486 | location in the .plt. This is required to make function | |
2487 | pointers compare as equal between the normal executable and | |
2488 | the shared library. */ | |
0e1862bb | 2489 | if (! bfd_link_pic (info) |
f5385ebf | 2490 | && !h->def_regular) |
ced53ee5 | 2491 | { |
dd7e64d4 L |
2492 | if (use_plt_got) |
2493 | { | |
2494 | /* We need to make a call to the entry of the GOT PLT | |
2495 | instead of regular PLT entry. */ | |
2496 | h->root.u.def.section = got_s; | |
2497 | h->root.u.def.value = eh->plt_got.offset; | |
2498 | } | |
2499 | else | |
2500 | { | |
ee2fdd6f L |
2501 | if (second_s) |
2502 | { | |
2503 | /* We need to make a call to the entry of the | |
2504 | second PLT instead of regular PLT entry. */ | |
2505 | h->root.u.def.section = second_s; | |
2506 | h->root.u.def.value = eh->plt_second.offset; | |
2507 | } | |
2508 | else | |
2509 | { | |
2510 | h->root.u.def.section = s; | |
2511 | h->root.u.def.value = h->plt.offset; | |
2512 | } | |
dd7e64d4 | 2513 | } |
ced53ee5 | 2514 | } |
6725bdbf | 2515 | |
ced53ee5 | 2516 | /* Make room for this entry. */ |
dd7e64d4 | 2517 | if (use_plt_got) |
0afcef53 | 2518 | got_s->size += non_lazy_plt_layout->plt_entry_size; |
dd7e64d4 L |
2519 | else |
2520 | { | |
2521 | s->size += plt_entry_size; | |
ee2fdd6f | 2522 | if (second_s) |
0afcef53 | 2523 | second_s->size += non_lazy_plt_layout->plt_entry_size; |
6725bdbf | 2524 | |
dd7e64d4 L |
2525 | /* We also need to make an entry in the .got.plt section, |
2526 | which will be placed in the .got section by the linker | |
2527 | script. */ | |
2528 | htab->elf.sgotplt->size += 4; | |
6725bdbf | 2529 | |
aec6b87e L |
2530 | /* There should be no PLT relocation against resolved |
2531 | undefined weak symbol in executable. */ | |
2532 | if (!resolved_to_zero) | |
2533 | { | |
2534 | /* We also need to make an entry in the .rel.plt | |
2535 | section. */ | |
2536 | htab->elf.srelplt->size += sizeof (Elf32_External_Rel); | |
2537 | htab->elf.srelplt->reloc_count++; | |
2538 | } | |
dd7e64d4 | 2539 | } |
eac338cf | 2540 | |
f604c2a2 | 2541 | if (bed->os == is_vxworks && !bfd_link_pic (info)) |
eac338cf PB |
2542 | { |
2543 | /* VxWorks has a second set of relocations for each PLT entry | |
2544 | in executables. They go in a separate relocation section, | |
2545 | which is processed by the kernel loader. */ | |
2546 | ||
2547 | /* There are two relocations for the initial PLT entry: an | |
2548 | R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an | |
2549 | R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */ | |
2550 | ||
0afcef53 | 2551 | asection *srelplt2 = elf_i386_srelplt2 (htab); |
25e762b9 | 2552 | if (h->plt.offset == plt_entry_size) |
0afcef53 | 2553 | srelplt2->size += (sizeof (Elf32_External_Rel) * 2); |
eac338cf PB |
2554 | |
2555 | /* There are two extra relocations for each subsequent PLT entry: | |
2556 | an R_386_32 relocation for the GOT entry, and an R_386_32 | |
2557 | relocation for the PLT entry. */ | |
2558 | ||
0afcef53 | 2559 | srelplt2->size += (sizeof (Elf32_External_Rel) * 2); |
eac338cf | 2560 | } |
6725bdbf | 2561 | } |
ced53ee5 AM |
2562 | else |
2563 | { | |
13f42277 | 2564 | eh->plt_got.offset = (bfd_vma) -1; |
51b64d56 | 2565 | h->plt.offset = (bfd_vma) -1; |
f5385ebf | 2566 | h->needs_plt = 0; |
ced53ee5 | 2567 | } |
6725bdbf AM |
2568 | } |
2569 | else | |
2570 | { | |
13f42277 | 2571 | eh->plt_got.offset = (bfd_vma) -1; |
51b64d56 | 2572 | h->plt.offset = (bfd_vma) -1; |
f5385ebf | 2573 | h->needs_plt = 0; |
6725bdbf AM |
2574 | } |
2575 | ||
67a4f2b7 AO |
2576 | eh->tlsdesc_got = (bfd_vma) -1; |
2577 | ||
37e55690 | 2578 | /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary, |
13ae64f3 JJ |
2579 | make it a R_386_TLS_LE_32 requiring no TLS entry. */ |
2580 | if (h->got.refcount > 0 | |
0e1862bb | 2581 | && bfd_link_executable (info) |
13ae64f3 | 2582 | && h->dynindx == -1 |
0afcef53 | 2583 | && (elf_x86_hash_entry (h)->tls_type & GOT_TLS_IE)) |
cedb70c5 | 2584 | h->got.offset = (bfd_vma) -1; |
13ae64f3 | 2585 | else if (h->got.refcount > 0) |
6725bdbf | 2586 | { |
0ac8d2ca | 2587 | asection *s; |
b34976b6 | 2588 | bfd_boolean dyn; |
0afcef53 | 2589 | int tls_type = elf_x86_hash_entry (h)->tls_type; |
6725bdbf | 2590 | |
5a15f56f | 2591 | /* Make sure this symbol is output as a dynamic symbol. |
0dc9a308 | 2592 | Undefined weak syms won't yet be marked as dynamic. */ |
5a15f56f | 2593 | if (h->dynindx == -1 |
aec6b87e | 2594 | && !h->forced_local |
e133d005 | 2595 | && !resolved_to_zero |
0dc9a308 | 2596 | && h->root.type == bfd_link_hash_undefweak) |
5a15f56f | 2597 | { |
c152c796 | 2598 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 2599 | return FALSE; |
5a15f56f AM |
2600 | } |
2601 | ||
6de2ae4a | 2602 | s = htab->elf.sgot; |
67a4f2b7 AO |
2603 | if (GOT_TLS_GDESC_P (tls_type)) |
2604 | { | |
6de2ae4a | 2605 | eh->tlsdesc_got = htab->elf.sgotplt->size |
67a4f2b7 | 2606 | - elf_i386_compute_jump_table_size (htab); |
6de2ae4a | 2607 | htab->elf.sgotplt->size += 8; |
67a4f2b7 AO |
2608 | h->got.offset = (bfd_vma) -2; |
2609 | } | |
2610 | if (! GOT_TLS_GDESC_P (tls_type) | |
2611 | || GOT_TLS_GD_P (tls_type)) | |
2612 | { | |
2613 | h->got.offset = s->size; | |
2614 | s->size += 4; | |
2615 | /* R_386_TLS_GD needs 2 consecutive GOT slots. */ | |
2616 | if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH) | |
2617 | s->size += 4; | |
2618 | } | |
ebe50bae | 2619 | dyn = htab->elf.dynamic_sections_created; |
13ae64f3 | 2620 | /* R_386_TLS_IE_32 needs one dynamic relocation, |
37e55690 JJ |
2621 | R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation, |
2622 | (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we | |
2623 | need two), R_386_TLS_GD needs one if local symbol and two if | |
aec6b87e L |
2624 | global. No dynamic relocation against resolved undefined weak |
2625 | symbol in executable. */ | |
37e55690 | 2626 | if (tls_type == GOT_TLS_IE_BOTH) |
6de2ae4a | 2627 | htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel); |
67a4f2b7 | 2628 | else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1) |
37e55690 | 2629 | || (tls_type & GOT_TLS_IE)) |
6de2ae4a | 2630 | htab->elf.srelgot->size += sizeof (Elf32_External_Rel); |
67a4f2b7 | 2631 | else if (GOT_TLS_GD_P (tls_type)) |
6de2ae4a | 2632 | htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel); |
67a4f2b7 | 2633 | else if (! GOT_TLS_GDESC_P (tls_type) |
aec6b87e L |
2634 | && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
2635 | && !resolved_to_zero) | |
67a4f2b7 | 2636 | || h->root.type != bfd_link_hash_undefweak) |
0e1862bb | 2637 | && (bfd_link_pic (info) |
ef5aade5 | 2638 | || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))) |
6de2ae4a | 2639 | htab->elf.srelgot->size += sizeof (Elf32_External_Rel); |
67a4f2b7 | 2640 | if (GOT_TLS_GDESC_P (tls_type)) |
6de2ae4a | 2641 | htab->elf.srelplt->size += sizeof (Elf32_External_Rel); |
6725bdbf AM |
2642 | } |
2643 | else | |
51b64d56 | 2644 | h->got.offset = (bfd_vma) -1; |
6725bdbf | 2645 | |
5a15f56f | 2646 | if (eh->dyn_relocs == NULL) |
b34976b6 | 2647 | return TRUE; |
5a15f56f | 2648 | |
0c715baa AM |
2649 | /* In the shared -Bsymbolic case, discard space allocated for |
2650 | dynamic pc-relative relocs against symbols which turn out to be | |
2651 | defined in regular objects. For the normal shared case, discard | |
0ac8d2ca AM |
2652 | space for pc-relative relocs that have become local due to symbol |
2653 | visibility changes. */ | |
0c715baa | 2654 | |
0e1862bb | 2655 | if (bfd_link_pic (info)) |
5a15f56f | 2656 | { |
09695f56 AM |
2657 | /* The only reloc that uses pc_count is R_386_PC32, which will |
2658 | appear on a call or on something like ".long foo - .". We | |
2659 | want calls to protected symbols to resolve directly to the | |
2660 | function rather than going via the plt. If people want | |
2661 | function pointer comparisons to work as expected then they | |
2662 | should avoid writing assembly like ".long foo - .". */ | |
2663 | if (SYMBOL_CALLS_LOCAL (info, h)) | |
5a15f56f | 2664 | { |
e03a8ed8 | 2665 | struct elf_dyn_relocs **pp; |
0c715baa AM |
2666 | |
2667 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) | |
2668 | { | |
c3ce498c L |
2669 | p->count -= p->pc_count; |
2670 | p->pc_count = 0; | |
0c715baa AM |
2671 | if (p->count == 0) |
2672 | *pp = p->next; | |
2673 | else | |
2674 | pp = &p->next; | |
2675 | } | |
5a15f56f | 2676 | } |
4e795f50 | 2677 | |
f604c2a2 | 2678 | if (bed->os == is_vxworks) |
3348747a | 2679 | { |
e03a8ed8 | 2680 | struct elf_dyn_relocs **pp; |
3348747a NS |
2681 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) |
2682 | { | |
2683 | if (strcmp (p->sec->output_section->name, ".tls_vars") == 0) | |
2684 | *pp = p->next; | |
2685 | else | |
2686 | pp = &p->next; | |
2687 | } | |
2688 | } | |
2689 | ||
4e795f50 | 2690 | /* Also discard relocs on undefined weak syms with non-default |
aec6b87e | 2691 | visibility or in PIE. */ |
22d606e9 | 2692 | if (eh->dyn_relocs != NULL |
c353e543 | 2693 | && h->root.type == bfd_link_hash_undefweak) |
22d606e9 | 2694 | { |
aec6b87e L |
2695 | /* Undefined weak symbol is never bound locally in shared |
2696 | library. */ | |
2697 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
2698 | || resolved_to_zero) | |
2699 | { | |
2700 | if (h->non_got_ref) | |
2701 | { | |
2702 | /* Keep dynamic non-GOT/non-PLT relocation so that we | |
2703 | can branch to 0 without PLT. */ | |
2704 | struct elf_dyn_relocs **pp; | |
2705 | ||
2706 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) | |
2707 | if (p->pc_count == 0) | |
2708 | *pp = p->next; | |
2709 | else | |
2710 | { | |
2711 | /* Remove non-R_386_PC32 relocation. */ | |
2712 | p->count = p->pc_count; | |
2713 | pp = &p->next; | |
2714 | } | |
22d606e9 | 2715 | |
aec6b87e L |
2716 | if (eh->dyn_relocs != NULL) |
2717 | { | |
2718 | /* Make sure undefined weak symbols are output | |
2719 | as dynamic symbols in PIEs for dynamic non-GOT | |
2720 | non-PLT reloations. */ | |
2721 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
2722 | return FALSE; | |
2723 | } | |
2724 | } | |
2725 | else | |
2726 | eh->dyn_relocs = NULL; | |
2727 | } | |
22d606e9 AM |
2728 | else if (h->dynindx == -1 |
2729 | && !h->forced_local) | |
2730 | { | |
2731 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) | |
2732 | return FALSE; | |
2733 | } | |
2734 | } | |
0c715baa | 2735 | } |
a23b6845 | 2736 | else if (ELIMINATE_COPY_RELOCS) |
0c715baa AM |
2737 | { |
2738 | /* For the non-shared case, discard space for relocs against | |
2739 | symbols which turn out to need copy relocs or are not | |
04ebc307 L |
2740 | dynamic. Keep dynamic relocations for run-time function |
2741 | pointer initialization. */ | |
0c715baa | 2742 | |
aec6b87e L |
2743 | if ((!h->non_got_ref |
2744 | || eh->func_pointer_refcount > 0 | |
2745 | || (h->root.type == bfd_link_hash_undefweak | |
2746 | && !resolved_to_zero)) | |
f5385ebf AM |
2747 | && ((h->def_dynamic |
2748 | && !h->def_regular) | |
ebe50bae | 2749 | || (htab->elf.dynamic_sections_created |
0c715baa AM |
2750 | && (h->root.type == bfd_link_hash_undefweak |
2751 | || h->root.type == bfd_link_hash_undefined)))) | |
2752 | { | |
2753 | /* Make sure this symbol is output as a dynamic symbol. | |
2754 | Undefined weak syms won't yet be marked as dynamic. */ | |
2755 | if (h->dynindx == -1 | |
aec6b87e | 2756 | && !h->forced_local |
0dc9a308 L |
2757 | && !resolved_to_zero |
2758 | && h->root.type == bfd_link_hash_undefweak) | |
0c715baa | 2759 | { |
c152c796 | 2760 | if (! bfd_elf_link_record_dynamic_symbol (info, h)) |
b34976b6 | 2761 | return FALSE; |
0c715baa | 2762 | } |
5a15f56f | 2763 | |
0c715baa AM |
2764 | /* If that succeeded, we know we'll be keeping all the |
2765 | relocs. */ | |
2766 | if (h->dynindx != -1) | |
2767 | goto keep; | |
2768 | } | |
2769 | ||
2770 | eh->dyn_relocs = NULL; | |
04ebc307 | 2771 | eh->func_pointer_refcount = 0; |
0c715baa | 2772 | |
ec338859 | 2773 | keep: ; |
5a15f56f AM |
2774 | } |
2775 | ||
0c715baa AM |
2776 | /* Finally, allocate space. */ |
2777 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
12d0ee4a | 2778 | { |
e7c33416 NC |
2779 | asection *sreloc; |
2780 | ||
cbe950e9 | 2781 | sreloc = elf_section_data (p->sec)->sreloc; |
e7c33416 NC |
2782 | |
2783 | BFD_ASSERT (sreloc != NULL); | |
eea6121a | 2784 | sreloc->size += p->count * sizeof (Elf32_External_Rel); |
12d0ee4a AM |
2785 | } |
2786 | ||
b34976b6 | 2787 | return TRUE; |
6725bdbf AM |
2788 | } |
2789 | ||
c25bc9fc L |
2790 | /* Allocate space in .plt, .got and associated reloc sections for |
2791 | local dynamic relocs. */ | |
2792 | ||
2793 | static bfd_boolean | |
2794 | elf_i386_allocate_local_dynrelocs (void **slot, void *inf) | |
2795 | { | |
2796 | struct elf_link_hash_entry *h | |
2797 | = (struct elf_link_hash_entry *) *slot; | |
2798 | ||
2799 | if (h->type != STT_GNU_IFUNC | |
2800 | || !h->def_regular | |
2801 | || !h->ref_regular | |
2802 | || !h->forced_local | |
2803 | || h->root.type != bfd_link_hash_defined) | |
2804 | abort (); | |
2805 | ||
2806 | return elf_i386_allocate_dynrelocs (h, inf); | |
2807 | } | |
2808 | ||
c175a657 | 2809 | /* Convert load via the GOT slot to load immediate. */ |
daa67607 L |
2810 | |
2811 | static bfd_boolean | |
02a86693 | 2812 | elf_i386_convert_load (bfd *abfd, asection *sec, |
a6af384b | 2813 | struct bfd_link_info *link_info) |
daa67607 | 2814 | { |
0afcef53 | 2815 | struct elf_x86_link_hash_table *htab; |
daa67607 L |
2816 | Elf_Internal_Shdr *symtab_hdr; |
2817 | Elf_Internal_Rela *internal_relocs; | |
2818 | Elf_Internal_Rela *irel, *irelend; | |
2819 | bfd_byte *contents; | |
c175a657 | 2820 | bfd_boolean changed; |
daa67607 L |
2821 | bfd_signed_vma *local_got_refcounts; |
2822 | ||
2823 | /* Don't even try to convert non-ELF outputs. */ | |
2824 | if (!is_elf_hash_table (link_info->hash)) | |
2825 | return FALSE; | |
2826 | ||
c1d11331 | 2827 | /* Nothing to do if there is no need or no output. */ |
daa67607 | 2828 | if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC) |
02a86693 | 2829 | || sec->need_convert_load == 0 |
c8831961 | 2830 | || bfd_is_abs_section (sec->output_section)) |
daa67607 L |
2831 | return TRUE; |
2832 | ||
2833 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2834 | ||
2835 | /* Load the relocations for this section. */ | |
2836 | internal_relocs = (_bfd_elf_link_read_relocs | |
2837 | (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, | |
2838 | link_info->keep_memory)); | |
2839 | if (internal_relocs == NULL) | |
2840 | return FALSE; | |
2841 | ||
c175a657 | 2842 | changed = FALSE; |
0afcef53 | 2843 | htab = elf_x86_hash_table (link_info, I386_ELF_DATA); |
daa67607 L |
2844 | local_got_refcounts = elf_local_got_refcounts (abfd); |
2845 | ||
2846 | /* Get the section contents. */ | |
2847 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
2848 | contents = elf_section_data (sec)->this_hdr.contents; | |
2849 | else | |
2850 | { | |
2851 | if (!bfd_malloc_and_get_section (abfd, sec, &contents)) | |
2852 | goto error_return; | |
2853 | } | |
2854 | ||
2855 | irelend = internal_relocs + sec->reloc_count; | |
2856 | for (irel = internal_relocs; irel < irelend; irel++) | |
2857 | { | |
2858 | unsigned int r_type = ELF32_R_TYPE (irel->r_info); | |
c175a657 | 2859 | unsigned int r_symndx; |
daa67607 | 2860 | struct elf_link_hash_entry *h; |
c175a657 | 2861 | bfd_boolean converted; |
daa67607 | 2862 | |
7d4d9709 L |
2863 | /* Don't convert R_386_GOT32 since we can't tell if it is applied |
2864 | to "mov $foo@GOT, %reg" which isn't a load via GOT. */ | |
2865 | if (r_type != R_386_GOT32X) | |
daa67607 L |
2866 | continue; |
2867 | ||
c175a657 | 2868 | r_symndx = ELF32_R_SYM (irel->r_info); |
02a86693 | 2869 | if (r_symndx < symtab_hdr->sh_info) |
0afcef53 L |
2870 | h = _bfd_elf_x86_get_local_sym_hash (htab, sec->owner, |
2871 | (const Elf_Internal_Rela *) irel, | |
2872 | FALSE); | |
c175a657 | 2873 | else |
02a86693 | 2874 | { |
c175a657 L |
2875 | h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info]; |
2876 | while (h->root.type == bfd_link_hash_indirect | |
2877 | || h->root.type == bfd_link_hash_warning) | |
2878 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
daa67607 L |
2879 | } |
2880 | ||
02a86693 | 2881 | /* STT_GNU_IFUNC must keep GOT32 relocations. */ |
c175a657 | 2882 | if (h != NULL && h->type == STT_GNU_IFUNC) |
02a86693 L |
2883 | continue; |
2884 | ||
c175a657 L |
2885 | converted = FALSE; |
2886 | if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents, | |
2887 | irel, h, &converted, link_info)) | |
2888 | goto error_return; | |
bae420ef | 2889 | |
c175a657 | 2890 | if (converted) |
02a86693 | 2891 | { |
c175a657 L |
2892 | changed = converted; |
2893 | if (h) | |
02a86693 | 2894 | { |
c175a657 L |
2895 | if (h->got.refcount > 0) |
2896 | h->got.refcount -= 1; | |
02a86693 | 2897 | } |
c175a657 | 2898 | else |
02a86693 | 2899 | { |
c175a657 L |
2900 | if (local_got_refcounts != NULL |
2901 | && local_got_refcounts[r_symndx] > 0) | |
2902 | local_got_refcounts[r_symndx] -= 1; | |
02a86693 | 2903 | } |
daa67607 L |
2904 | } |
2905 | } | |
2906 | ||
2907 | if (contents != NULL | |
2908 | && elf_section_data (sec)->this_hdr.contents != contents) | |
2909 | { | |
c175a657 | 2910 | if (!changed && !link_info->keep_memory) |
daa67607 L |
2911 | free (contents); |
2912 | else | |
2913 | { | |
2914 | /* Cache the section contents for elf_link_input_bfd. */ | |
2915 | elf_section_data (sec)->this_hdr.contents = contents; | |
2916 | } | |
2917 | } | |
2918 | ||
2919 | if (elf_section_data (sec)->relocs != internal_relocs) | |
2920 | { | |
c175a657 | 2921 | if (!changed) |
daa67607 L |
2922 | free (internal_relocs); |
2923 | else | |
2924 | elf_section_data (sec)->relocs = internal_relocs; | |
2925 | } | |
2926 | ||
2927 | return TRUE; | |
2928 | ||
2929 | error_return: | |
2930 | if (contents != NULL | |
2931 | && elf_section_data (sec)->this_hdr.contents != contents) | |
2932 | free (contents); | |
2933 | if (internal_relocs != NULL | |
2934 | && elf_section_data (sec)->relocs != internal_relocs) | |
2935 | free (internal_relocs); | |
2936 | return FALSE; | |
2937 | } | |
2938 | ||
252b5132 RH |
2939 | /* Set the sizes of the dynamic sections. */ |
2940 | ||
b34976b6 | 2941 | static bfd_boolean |
23209a78 | 2942 | elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) |
252b5132 | 2943 | { |
0afcef53 | 2944 | struct elf_x86_link_hash_table *htab; |
252b5132 RH |
2945 | bfd *dynobj; |
2946 | asection *s; | |
b34976b6 | 2947 | bfd_boolean relocs; |
0c715baa | 2948 | bfd *ibfd; |
252b5132 | 2949 | |
0afcef53 | 2950 | htab = elf_x86_hash_table (info, I386_ELF_DATA); |
4dfe6ac6 NC |
2951 | if (htab == NULL) |
2952 | return FALSE; | |
ebe50bae | 2953 | dynobj = htab->elf.dynobj; |
ffb2e45b AM |
2954 | if (dynobj == NULL) |
2955 | abort (); | |
252b5132 | 2956 | |
0c715baa AM |
2957 | /* Set up .got offsets for local syms, and space for local dynamic |
2958 | relocs. */ | |
c72f2fb2 | 2959 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
161d71a6 L |
2960 | { |
2961 | bfd_signed_vma *local_got; | |
2962 | bfd_signed_vma *end_local_got; | |
13ae64f3 | 2963 | char *local_tls_type; |
67a4f2b7 | 2964 | bfd_vma *local_tlsdesc_gotent; |
161d71a6 L |
2965 | bfd_size_type locsymcount; |
2966 | Elf_Internal_Shdr *symtab_hdr; | |
2967 | asection *srel; | |
6725bdbf | 2968 | |
0ffa91dd | 2969 | if (! is_i386_elf (ibfd)) |
161d71a6 | 2970 | continue; |
6725bdbf | 2971 | |
0c715baa AM |
2972 | for (s = ibfd->sections; s != NULL; s = s->next) |
2973 | { | |
e03a8ed8 | 2974 | struct elf_dyn_relocs *p; |
0c715baa | 2975 | |
02a86693 | 2976 | if (!elf_i386_convert_load (ibfd, s, info)) |
daa67607 L |
2977 | return FALSE; |
2978 | ||
e03a8ed8 | 2979 | for (p = ((struct elf_dyn_relocs *) |
e81d3500 | 2980 | elf_section_data (s)->local_dynrel); |
ec338859 AM |
2981 | p != NULL; |
2982 | p = p->next) | |
0c715baa | 2983 | { |
ec338859 AM |
2984 | if (!bfd_is_abs_section (p->sec) |
2985 | && bfd_is_abs_section (p->sec->output_section)) | |
2986 | { | |
2987 | /* Input section has been discarded, either because | |
2988 | it is a copy of a linkonce section or due to | |
2989 | linker script /DISCARD/, so we'll be discarding | |
2990 | the relocs too. */ | |
2991 | } | |
f604c2a2 L |
2992 | else if ((get_elf_i386_backend_data (output_bfd)->os |
2993 | == is_vxworks) | |
3348747a NS |
2994 | && strcmp (p->sec->output_section->name, |
2995 | ".tls_vars") == 0) | |
2996 | { | |
2997 | /* Relocations in vxworks .tls_vars sections are | |
2998 | handled specially by the loader. */ | |
2999 | } | |
248866a8 | 3000 | else if (p->count != 0) |
ec338859 AM |
3001 | { |
3002 | srel = elf_section_data (p->sec)->sreloc; | |
eea6121a | 3003 | srel->size += p->count * sizeof (Elf32_External_Rel); |
4b819e1f L |
3004 | if ((p->sec->output_section->flags & SEC_READONLY) != 0 |
3005 | && (info->flags & DF_TEXTREL) == 0) | |
b70321a2 L |
3006 | { |
3007 | info->flags |= DF_TEXTREL; | |
0e1862bb | 3008 | if ((info->warn_shared_textrel && bfd_link_pic (info)) |
1952c5cd | 3009 | || info->error_textrel) |
695344c0 | 3010 | /* xgettext:c-format */ |
1952c5cd | 3011 | info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"), |
b70321a2 | 3012 | p->sec->owner, p->sec); |
b70321a2 | 3013 | } |
ec338859 | 3014 | } |
0c715baa AM |
3015 | } |
3016 | } | |
3017 | ||
3018 | local_got = elf_local_got_refcounts (ibfd); | |
161d71a6 L |
3019 | if (!local_got) |
3020 | continue; | |
6725bdbf | 3021 | |
0ffa91dd | 3022 | symtab_hdr = &elf_symtab_hdr (ibfd); |
161d71a6 L |
3023 | locsymcount = symtab_hdr->sh_info; |
3024 | end_local_got = local_got + locsymcount; | |
0afcef53 L |
3025 | local_tls_type = elf_x86_local_got_tls_type (ibfd); |
3026 | local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd); | |
6de2ae4a L |
3027 | s = htab->elf.sgot; |
3028 | srel = htab->elf.srelgot; | |
67a4f2b7 AO |
3029 | for (; local_got < end_local_got; |
3030 | ++local_got, ++local_tls_type, ++local_tlsdesc_gotent) | |
161d71a6 | 3031 | { |
67a4f2b7 | 3032 | *local_tlsdesc_gotent = (bfd_vma) -1; |
161d71a6 | 3033 | if (*local_got > 0) |
6725bdbf | 3034 | { |
67a4f2b7 AO |
3035 | if (GOT_TLS_GDESC_P (*local_tls_type)) |
3036 | { | |
6de2ae4a | 3037 | *local_tlsdesc_gotent = htab->elf.sgotplt->size |
67a4f2b7 | 3038 | - elf_i386_compute_jump_table_size (htab); |
6de2ae4a | 3039 | htab->elf.sgotplt->size += 8; |
67a4f2b7 AO |
3040 | *local_got = (bfd_vma) -2; |
3041 | } | |
3042 | if (! GOT_TLS_GDESC_P (*local_tls_type) | |
3043 | || GOT_TLS_GD_P (*local_tls_type)) | |
3044 | { | |
3045 | *local_got = s->size; | |
3046 | s->size += 4; | |
3047 | if (GOT_TLS_GD_P (*local_tls_type) | |
3048 | || *local_tls_type == GOT_TLS_IE_BOTH) | |
3049 | s->size += 4; | |
3050 | } | |
0e1862bb | 3051 | if (bfd_link_pic (info) |
67a4f2b7 | 3052 | || GOT_TLS_GD_ANY_P (*local_tls_type) |
37e55690 JJ |
3053 | || (*local_tls_type & GOT_TLS_IE)) |
3054 | { | |
3055 | if (*local_tls_type == GOT_TLS_IE_BOTH) | |
eea6121a | 3056 | srel->size += 2 * sizeof (Elf32_External_Rel); |
67a4f2b7 AO |
3057 | else if (GOT_TLS_GD_P (*local_tls_type) |
3058 | || ! GOT_TLS_GDESC_P (*local_tls_type)) | |
eea6121a | 3059 | srel->size += sizeof (Elf32_External_Rel); |
67a4f2b7 | 3060 | if (GOT_TLS_GDESC_P (*local_tls_type)) |
6de2ae4a | 3061 | htab->elf.srelplt->size += sizeof (Elf32_External_Rel); |
37e55690 | 3062 | } |
6725bdbf | 3063 | } |
161d71a6 L |
3064 | else |
3065 | *local_got = (bfd_vma) -1; | |
6725bdbf | 3066 | } |
252b5132 | 3067 | } |
6725bdbf | 3068 | |
0afcef53 | 3069 | if (htab->tls_ld_or_ldm_got.refcount > 0) |
13ae64f3 JJ |
3070 | { |
3071 | /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM | |
3072 | relocs. */ | |
0afcef53 | 3073 | htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size; |
6de2ae4a L |
3074 | htab->elf.sgot->size += 8; |
3075 | htab->elf.srelgot->size += sizeof (Elf32_External_Rel); | |
13ae64f3 JJ |
3076 | } |
3077 | else | |
0afcef53 | 3078 | htab->tls_ld_or_ldm_got.offset = -1; |
13ae64f3 | 3079 | |
0c715baa AM |
3080 | /* Allocate global sym .plt and .got entries, and space for global |
3081 | sym dynamic relocs. */ | |
eb4ff4d6 | 3082 | elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info); |
252b5132 | 3083 | |
c25bc9fc L |
3084 | /* Allocate .plt and .got entries, and space for local symbols. */ |
3085 | htab_traverse (htab->loc_hash_table, | |
3086 | elf_i386_allocate_local_dynrelocs, | |
3087 | info); | |
3088 | ||
67a4f2b7 AO |
3089 | /* For every jump slot reserved in the sgotplt, reloc_count is |
3090 | incremented. However, when we reserve space for TLS descriptors, | |
3091 | it's not incremented, so in order to compute the space reserved | |
3092 | for them, it suffices to multiply the reloc count by the jump | |
e1f98742 | 3093 | slot size. |
caa0075c | 3094 | |
e1f98742 L |
3095 | PR ld/13302: We start next_irelative_index at the end of .rela.plt |
3096 | so that R_386_IRELATIVE entries come last. */ | |
6de2ae4a | 3097 | if (htab->elf.srelplt) |
e1f98742 | 3098 | { |
0afcef53 L |
3099 | elf_i386_next_tls_desc_index(htab) |
3100 | = htab->elf.srelplt->reloc_count; | |
3101 | htab->sgotplt_jump_table_size | |
3102 | = elf_i386_next_tls_desc_index(htab) * 4; | |
e1f98742 L |
3103 | htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1; |
3104 | } | |
3105 | else if (htab->elf.irelplt) | |
3106 | htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1; | |
3107 | ||
67a4f2b7 | 3108 | |
a7b16ceb L |
3109 | if (htab->elf.sgotplt) |
3110 | { | |
3111 | /* Don't allocate .got.plt section if there are no GOT nor PLT | |
9a2a56cc | 3112 | entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */ |
9d4b2dba L |
3113 | if ((htab->elf.hgot == NULL |
3114 | || !htab->elf.hgot->ref_regular_nonweak) | |
e28df02b L |
3115 | && (htab->elf.sgotplt->size |
3116 | == get_elf_backend_data (output_bfd)->got_header_size) | |
a7b16ceb L |
3117 | && (htab->elf.splt == NULL |
3118 | || htab->elf.splt->size == 0) | |
3119 | && (htab->elf.sgot == NULL | |
3120 | || htab->elf.sgot->size == 0) | |
3121 | && (htab->elf.iplt == NULL | |
3122 | || htab->elf.iplt->size == 0) | |
3123 | && (htab->elf.igotplt == NULL | |
3124 | || htab->elf.igotplt->size == 0)) | |
3125 | htab->elf.sgotplt->size = 0; | |
3126 | } | |
3127 | ||
fff53dae L |
3128 | if (_bfd_elf_eh_frame_present (info)) |
3129 | { | |
3130 | if (htab->plt_eh_frame != NULL | |
3131 | && htab->elf.splt != NULL | |
3132 | && htab->elf.splt->size != 0 | |
3133 | && !bfd_is_abs_section (htab->elf.splt->output_section)) | |
0afcef53 | 3134 | htab->plt_eh_frame->size = elf_i386_plt (htab).eh_frame_plt_size; |
fff53dae L |
3135 | |
3136 | if (htab->plt_got_eh_frame != NULL | |
3137 | && htab->plt_got != NULL | |
3138 | && htab->plt_got->size != 0 | |
3139 | && !bfd_is_abs_section (htab->plt_got->output_section)) | |
3140 | htab->plt_got_eh_frame->size | |
0afcef53 | 3141 | = elf_i386_non_lazy_plt (htab)->eh_frame_plt_size; |
ee2fdd6f L |
3142 | |
3143 | /* Unwind info for the second PLT and .plt.got sections are | |
3144 | identical. */ | |
3145 | if (htab->plt_second_eh_frame != NULL | |
3146 | && htab->plt_second != NULL | |
3147 | && htab->plt_second->size != 0 | |
3148 | && !bfd_is_abs_section (htab->plt_second->output_section)) | |
3149 | htab->plt_second_eh_frame->size | |
0afcef53 | 3150 | = elf_i386_non_lazy_plt (htab)->eh_frame_plt_size; |
fff53dae | 3151 | } |
9a2a56cc | 3152 | |
5a15f56f AM |
3153 | /* We now have determined the sizes of the various dynamic sections. |
3154 | Allocate memory for them. */ | |
b34976b6 | 3155 | relocs = FALSE; |
252b5132 RH |
3156 | for (s = dynobj->sections; s != NULL; s = s->next) |
3157 | { | |
eac338cf PB |
3158 | bfd_boolean strip_section = TRUE; |
3159 | ||
252b5132 RH |
3160 | if ((s->flags & SEC_LINKER_CREATED) == 0) |
3161 | continue; | |
3162 | ||
6de2ae4a | 3163 | if (s == htab->elf.splt |
9a2a56cc | 3164 | || s == htab->elf.sgot) |
252b5132 | 3165 | { |
6725bdbf AM |
3166 | /* Strip this section if we don't need it; see the |
3167 | comment below. */ | |
eac338cf PB |
3168 | /* We'd like to strip these sections if they aren't needed, but if |
3169 | we've exported dynamic symbols from them we must leave them. | |
3170 | It's too late to tell BFD to get rid of the symbols. */ | |
3171 | ||
7325306f | 3172 | if (htab->elf.hplt != NULL) |
eac338cf | 3173 | strip_section = FALSE; |
252b5132 | 3174 | } |
9a2a56cc AM |
3175 | else if (s == htab->elf.sgotplt |
3176 | || s == htab->elf.iplt | |
3177 | || s == htab->elf.igotplt | |
ee2fdd6f | 3178 | || s == htab->plt_second |
dd7e64d4 | 3179 | || s == htab->plt_got |
9a2a56cc | 3180 | || s == htab->plt_eh_frame |
fff53dae | 3181 | || s == htab->plt_got_eh_frame |
ee2fdd6f | 3182 | || s == htab->plt_second_eh_frame |
5474d94f AM |
3183 | || s == htab->elf.sdynbss |
3184 | || s == htab->elf.sdynrelro) | |
9a2a56cc AM |
3185 | { |
3186 | /* Strip these too. */ | |
3187 | } | |
0112cd26 | 3188 | else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel")) |
252b5132 | 3189 | { |
6de2ae4a L |
3190 | if (s->size != 0 |
3191 | && s != htab->elf.srelplt | |
0afcef53 | 3192 | && s != elf_i386_srelplt2 (htab)) |
b34976b6 | 3193 | relocs = TRUE; |
252b5132 | 3194 | |
0ac8d2ca AM |
3195 | /* We use the reloc_count field as a counter if we need |
3196 | to copy relocs into the output file. */ | |
5ae0bfb6 | 3197 | s->reloc_count = 0; |
252b5132 | 3198 | } |
6725bdbf | 3199 | else |
252b5132 RH |
3200 | { |
3201 | /* It's not one of our sections, so don't allocate space. */ | |
3202 | continue; | |
3203 | } | |
3204 | ||
c456f082 | 3205 | if (s->size == 0) |
252b5132 | 3206 | { |
0ac8d2ca AM |
3207 | /* If we don't need this section, strip it from the |
3208 | output file. This is mostly to handle .rel.bss and | |
3209 | .rel.plt. We must create both sections in | |
3210 | create_dynamic_sections, because they must be created | |
3211 | before the linker maps input sections to output | |
3212 | sections. The linker does that before | |
3213 | adjust_dynamic_symbol is called, and it is that | |
3214 | function which decides whether anything needs to go | |
3215 | into these sections. */ | |
c456f082 AM |
3216 | if (strip_section) |
3217 | s->flags |= SEC_EXCLUDE; | |
252b5132 RH |
3218 | continue; |
3219 | } | |
3220 | ||
c456f082 AM |
3221 | if ((s->flags & SEC_HAS_CONTENTS) == 0) |
3222 | continue; | |
3223 | ||
f69da49f AM |
3224 | /* Allocate memory for the section contents. We use bfd_zalloc |
3225 | here in case unused entries are not reclaimed before the | |
3226 | section's contents are written out. This should not happen, | |
3227 | but this way if it does, we get a R_386_NONE reloc instead | |
3228 | of garbage. */ | |
a50b1753 | 3229 | s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size); |
6725bdbf | 3230 | if (s->contents == NULL) |
b34976b6 | 3231 | return FALSE; |
252b5132 RH |
3232 | } |
3233 | ||
e41b3a13 | 3234 | if (htab->plt_eh_frame != NULL |
9a2a56cc AM |
3235 | && htab->plt_eh_frame->contents != NULL) |
3236 | { | |
f129e49f | 3237 | memcpy (htab->plt_eh_frame->contents, |
0afcef53 | 3238 | elf_i386_plt (htab).eh_frame_plt, |
f129e49f | 3239 | htab->plt_eh_frame->size); |
9a2a56cc AM |
3240 | bfd_put_32 (dynobj, htab->elf.splt->size, |
3241 | htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET); | |
3242 | } | |
e41b3a13 | 3243 | |
fff53dae L |
3244 | if (htab->plt_got_eh_frame != NULL |
3245 | && htab->plt_got_eh_frame->contents != NULL) | |
3246 | { | |
3247 | memcpy (htab->plt_got_eh_frame->contents, | |
0afcef53 | 3248 | elf_i386_non_lazy_plt (htab)->eh_frame_plt, |
fff53dae L |
3249 | htab->plt_got_eh_frame->size); |
3250 | bfd_put_32 (dynobj, htab->plt_got->size, | |
3251 | (htab->plt_got_eh_frame->contents | |
3252 | + PLT_FDE_LEN_OFFSET)); | |
3253 | } | |
3254 | ||
ee2fdd6f L |
3255 | if (htab->plt_second_eh_frame != NULL |
3256 | && htab->plt_second_eh_frame->contents != NULL) | |
3257 | { | |
3258 | memcpy (htab->plt_second_eh_frame->contents, | |
0afcef53 | 3259 | elf_i386_non_lazy_plt (htab)->eh_frame_plt, |
ee2fdd6f L |
3260 | htab->plt_second_eh_frame->size); |
3261 | bfd_put_32 (dynobj, htab->plt_second->size, | |
3262 | (htab->plt_second_eh_frame->contents | |
3263 | + PLT_FDE_LEN_OFFSET)); | |
3264 | } | |
3265 | ||
ebe50bae | 3266 | if (htab->elf.dynamic_sections_created) |
252b5132 RH |
3267 | { |
3268 | /* Add some entries to the .dynamic section. We fill in the | |
3269 | values later, in elf_i386_finish_dynamic_sections, but we | |
3270 | must add the entries now so that we get the correct size for | |
3271 | the .dynamic section. The DT_DEBUG entry is filled in by the | |
3272 | dynamic linker and used by the debugger. */ | |
dc810e39 | 3273 | #define add_dynamic_entry(TAG, VAL) \ |
5a580b3a | 3274 | _bfd_elf_add_dynamic_entry (info, TAG, VAL) |
dc810e39 | 3275 | |
0e1862bb | 3276 | if (bfd_link_executable (info)) |
252b5132 | 3277 | { |
dc810e39 | 3278 | if (!add_dynamic_entry (DT_DEBUG, 0)) |
b34976b6 | 3279 | return FALSE; |
252b5132 RH |
3280 | } |
3281 | ||
6de2ae4a | 3282 | if (htab->elf.splt->size != 0) |
252b5132 | 3283 | { |
7c1e8d3e L |
3284 | /* DT_PLTGOT is used by prelink even if there is no PLT |
3285 | relocation. */ | |
3286 | if (!add_dynamic_entry (DT_PLTGOT, 0)) | |
b34976b6 | 3287 | return FALSE; |
97d343d4 | 3288 | } |
7c1e8d3e | 3289 | |
97d343d4 L |
3290 | if (htab->elf.srelplt->size != 0) |
3291 | { | |
3292 | if (!add_dynamic_entry (DT_PLTRELSZ, 0) | |
3293 | || !add_dynamic_entry (DT_PLTREL, DT_REL) | |
3294 | || !add_dynamic_entry (DT_JMPREL, 0)) | |
3295 | return FALSE; | |
252b5132 RH |
3296 | } |
3297 | ||
3298 | if (relocs) | |
3299 | { | |
dc810e39 AM |
3300 | if (!add_dynamic_entry (DT_REL, 0) |
3301 | || !add_dynamic_entry (DT_RELSZ, 0) | |
3302 | || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel))) | |
b34976b6 | 3303 | return FALSE; |
252b5132 | 3304 | |
0c715baa AM |
3305 | /* If any dynamic relocs apply to a read-only section, |
3306 | then we need a DT_TEXTREL entry. */ | |
248866a8 | 3307 | if ((info->flags & DF_TEXTREL) == 0) |
eb4ff4d6 | 3308 | elf_link_hash_traverse (&htab->elf, |
0afcef53 L |
3309 | _bfd_x86_elf_readonly_dynrelocs, |
3310 | info); | |
0c715baa AM |
3311 | |
3312 | if ((info->flags & DF_TEXTREL) != 0) | |
3313 | { | |
2df3368d | 3314 | if (htab->readonly_dynrelocs_against_ifunc) |
8efa2874 L |
3315 | { |
3316 | info->callbacks->einfo | |
3317 | (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n")); | |
3318 | bfd_set_error (bfd_error_bad_value); | |
3319 | return FALSE; | |
3320 | } | |
3321 | ||
0c715baa | 3322 | if (!add_dynamic_entry (DT_TEXTREL, 0)) |
b34976b6 | 3323 | return FALSE; |
0c715baa | 3324 | } |
252b5132 | 3325 | } |
f604c2a2 | 3326 | if (get_elf_i386_backend_data (output_bfd)->os == is_vxworks |
7a2b07ff NS |
3327 | && !elf_vxworks_add_dynamic_entries (output_bfd, info)) |
3328 | return FALSE; | |
252b5132 | 3329 | } |
dc810e39 | 3330 | #undef add_dynamic_entry |
252b5132 | 3331 | |
b34976b6 | 3332 | return TRUE; |
252b5132 RH |
3333 | } |
3334 | ||
38701953 AM |
3335 | /* Set the correct type for an x86 ELF section. We do this by the |
3336 | section name, which is a hack, but ought to work. */ | |
3337 | ||
b34976b6 | 3338 | static bfd_boolean |
55fd94b0 AM |
3339 | elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED, |
3340 | Elf_Internal_Shdr *hdr, | |
3341 | asection *sec) | |
38701953 | 3342 | { |
91d6fa6a | 3343 | const char *name; |
38701953 AM |
3344 | |
3345 | name = bfd_get_section_name (abfd, sec); | |
3346 | ||
3347 | /* This is an ugly, but unfortunately necessary hack that is | |
3348 | needed when producing EFI binaries on x86. It tells | |
3349 | elf.c:elf_fake_sections() not to consider ".reloc" as a section | |
3350 | containing ELF relocation info. We need this hack in order to | |
3351 | be able to generate ELF binaries that can be translated into | |
3352 | EFI applications (which are essentially COFF objects). Those | |
3353 | files contain a COFF ".reloc" section inside an ELFNN object, | |
3354 | which would normally cause BFD to segfault because it would | |
3355 | attempt to interpret this section as containing relocation | |
3356 | entries for section "oc". With this hack enabled, ".reloc" | |
3357 | will be treated as a normal data section, which will avoid the | |
3358 | segfault. However, you won't be able to create an ELFNN binary | |
3359 | with a section named "oc" that needs relocations, but that's | |
3360 | the kind of ugly side-effects you get when detecting section | |
3361 | types based on their names... In practice, this limitation is | |
3362 | unlikely to bite. */ | |
3363 | if (strcmp (name, ".reloc") == 0) | |
3364 | hdr->sh_type = SHT_PROGBITS; | |
3365 | ||
b34976b6 | 3366 | return TRUE; |
38701953 AM |
3367 | } |
3368 | ||
13ae64f3 JJ |
3369 | /* Return the relocation value for @tpoff relocation |
3370 | if STT_TLS virtual address is ADDRESS. */ | |
3371 | ||
3372 | static bfd_vma | |
eb4ff4d6 | 3373 | elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address) |
13ae64f3 | 3374 | { |
e1918d23 | 3375 | struct elf_link_hash_table *htab = elf_hash_table (info); |
7dc98aea RO |
3376 | const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd); |
3377 | bfd_vma static_tls_size; | |
13ae64f3 | 3378 | |
e1918d23 AM |
3379 | /* If tls_sec is NULL, we should have signalled an error already. */ |
3380 | if (htab->tls_sec == NULL) | |
6a30718d | 3381 | return 0; |
7dc98aea RO |
3382 | |
3383 | /* Consider special static TLS alignment requirements. */ | |
3384 | static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment); | |
3385 | return static_tls_size + htab->tls_sec->vma - address; | |
13ae64f3 JJ |
3386 | } |
3387 | ||
252b5132 RH |
3388 | /* Relocate an i386 ELF section. */ |
3389 | ||
b34976b6 | 3390 | static bfd_boolean |
55fd94b0 AM |
3391 | elf_i386_relocate_section (bfd *output_bfd, |
3392 | struct bfd_link_info *info, | |
3393 | bfd *input_bfd, | |
3394 | asection *input_section, | |
3395 | bfd_byte *contents, | |
3396 | Elf_Internal_Rela *relocs, | |
3397 | Elf_Internal_Sym *local_syms, | |
3398 | asection **local_sections) | |
252b5132 | 3399 | { |
0afcef53 | 3400 | struct elf_x86_link_hash_table *htab; |
252b5132 RH |
3401 | Elf_Internal_Shdr *symtab_hdr; |
3402 | struct elf_link_hash_entry **sym_hashes; | |
3403 | bfd_vma *local_got_offsets; | |
67a4f2b7 | 3404 | bfd_vma *local_tlsdesc_gotents; |
252b5132 | 3405 | Elf_Internal_Rela *rel; |
60f2e42e | 3406 | Elf_Internal_Rela *wrel; |
252b5132 | 3407 | Elf_Internal_Rela *relend; |
3348747a | 3408 | bfd_boolean is_vxworks_tls; |
25e762b9 | 3409 | unsigned plt_entry_size; |
252b5132 | 3410 | |
0ffa91dd | 3411 | BFD_ASSERT (is_i386_elf (input_bfd)); |
6bbec505 | 3412 | |
c337a162 L |
3413 | /* Skip if check_relocs failed. */ |
3414 | if (input_section->check_relocs_failed) | |
3415 | return FALSE; | |
3416 | ||
0afcef53 | 3417 | htab = elf_x86_hash_table (info, I386_ELF_DATA); |
4dfe6ac6 NC |
3418 | if (htab == NULL) |
3419 | return FALSE; | |
0ffa91dd | 3420 | symtab_hdr = &elf_symtab_hdr (input_bfd); |
252b5132 RH |
3421 | sym_hashes = elf_sym_hashes (input_bfd); |
3422 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
0afcef53 | 3423 | local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd); |
3348747a NS |
3424 | /* We have to handle relocations in vxworks .tls_vars sections |
3425 | specially, because the dynamic loader is 'weird'. */ | |
f604c2a2 L |
3426 | is_vxworks_tls = ((get_elf_i386_backend_data (output_bfd)->os |
3427 | == is_vxworks) | |
0e1862bb | 3428 | && bfd_link_pic (info) |
3348747a NS |
3429 | && !strcmp (input_section->output_section->name, |
3430 | ".tls_vars")); | |
252b5132 | 3431 | |
0afcef53 | 3432 | _bfd_x86_elf_set_tls_module_base (info); |
9f03412a | 3433 | |
0afcef53 | 3434 | plt_entry_size = elf_i386_plt (htab).plt_entry_size; |
25e762b9 | 3435 | |
60f2e42e | 3436 | rel = wrel = relocs; |
252b5132 | 3437 | relend = relocs + input_section->reloc_count; |
60f2e42e | 3438 | for (; rel < relend; wrel++, rel++) |
252b5132 | 3439 | { |
13ae64f3 | 3440 | unsigned int r_type; |
252b5132 RH |
3441 | reloc_howto_type *howto; |
3442 | unsigned long r_symndx; | |
3443 | struct elf_link_hash_entry *h; | |
0afcef53 | 3444 | struct elf_x86_link_hash_entry *eh; |
252b5132 RH |
3445 | Elf_Internal_Sym *sym; |
3446 | asection *sec; | |
dd7e64d4 | 3447 | bfd_vma off, offplt, plt_offset; |
252b5132 | 3448 | bfd_vma relocation; |
b34976b6 | 3449 | bfd_boolean unresolved_reloc; |
252b5132 | 3450 | bfd_reloc_status_type r; |
1b452ec6 | 3451 | unsigned int indx; |
13ae64f3 | 3452 | int tls_type; |
1788fc08 | 3453 | bfd_vma st_size; |
dd7e64d4 | 3454 | asection *resolved_plt; |
aec6b87e | 3455 | bfd_boolean resolved_to_zero; |
e133d005 | 3456 | bfd_boolean relative_reloc; |
252b5132 RH |
3457 | |
3458 | r_type = ELF32_R_TYPE (rel->r_info); | |
55fd94b0 AM |
3459 | if (r_type == R_386_GNU_VTINHERIT |
3460 | || r_type == R_386_GNU_VTENTRY) | |
18954b29 L |
3461 | { |
3462 | if (wrel != rel) | |
3463 | *wrel = *rel; | |
3464 | continue; | |
3465 | } | |
dc47f327 | 3466 | |
55fd94b0 | 3467 | if ((indx = r_type) >= R_386_standard |
13ae64f3 JJ |
3468 | && ((indx = r_type - R_386_ext_offset) - R_386_standard |
3469 | >= R_386_ext - R_386_standard) | |
3470 | && ((indx = r_type - R_386_tls_offset) - R_386_ext | |
c74be520 | 3471 | >= R_386_ext2 - R_386_ext)) |
47aeb64c NC |
3472 | return _bfd_unrecognized_reloc (input_bfd, input_section, r_type); |
3473 | ||
1b452ec6 | 3474 | howto = elf_howto_table + indx; |
252b5132 RH |
3475 | |
3476 | r_symndx = ELF32_R_SYM (rel->r_info); | |
252b5132 RH |
3477 | h = NULL; |
3478 | sym = NULL; | |
3479 | sec = NULL; | |
b34976b6 | 3480 | unresolved_reloc = FALSE; |
252b5132 RH |
3481 | if (r_symndx < symtab_hdr->sh_info) |
3482 | { | |
3483 | sym = local_syms + r_symndx; | |
3484 | sec = local_sections[r_symndx]; | |
3485 | relocation = (sec->output_section->vma | |
3486 | + sec->output_offset | |
3487 | + sym->st_value); | |
1788fc08 | 3488 | st_size = sym->st_size; |
ab96bf03 AM |
3489 | |
3490 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION | |
3491 | && ((sec->flags & SEC_MERGE) != 0 | |
0e1862bb | 3492 | || (bfd_link_relocatable (info) |
ab96bf03 | 3493 | && sec->output_offset != 0))) |
f8df10f4 | 3494 | { |
f8df10f4 | 3495 | bfd_vma addend; |
4a335f3d | 3496 | bfd_byte *where = contents + rel->r_offset; |
f8df10f4 | 3497 | |
4a335f3d | 3498 | switch (howto->size) |
f8df10f4 | 3499 | { |
4a335f3d AM |
3500 | case 0: |
3501 | addend = bfd_get_8 (input_bfd, where); | |
3502 | if (howto->pc_relative) | |
3503 | { | |
3504 | addend = (addend ^ 0x80) - 0x80; | |
3505 | addend += 1; | |
3506 | } | |
3507 | break; | |
3508 | case 1: | |
3509 | addend = bfd_get_16 (input_bfd, where); | |
3510 | if (howto->pc_relative) | |
3511 | { | |
3512 | addend = (addend ^ 0x8000) - 0x8000; | |
3513 | addend += 2; | |
3514 | } | |
3515 | break; | |
3516 | case 2: | |
3517 | addend = bfd_get_32 (input_bfd, where); | |
3518 | if (howto->pc_relative) | |
3519 | { | |
3520 | addend = (addend ^ 0x80000000) - 0x80000000; | |
3521 | addend += 4; | |
3522 | } | |
3523 | break; | |
3524 | default: | |
3525 | abort (); | |
f8df10f4 JJ |
3526 | } |
3527 | ||
0e1862bb | 3528 | if (bfd_link_relocatable (info)) |
ab96bf03 AM |
3529 | addend += sec->output_offset; |
3530 | else | |
3531 | { | |
3532 | asection *msec = sec; | |
3533 | addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, | |
3534 | addend); | |
3535 | addend -= relocation; | |
3536 | addend += msec->output_section->vma + msec->output_offset; | |
3537 | } | |
4a335f3d AM |
3538 | |
3539 | switch (howto->size) | |
3540 | { | |
3541 | case 0: | |
16a10388 | 3542 | /* FIXME: overflow checks. */ |
4a335f3d AM |
3543 | if (howto->pc_relative) |
3544 | addend -= 1; | |
3545 | bfd_put_8 (input_bfd, addend, where); | |
4a335f3d AM |
3546 | break; |
3547 | case 1: | |
3548 | if (howto->pc_relative) | |
3549 | addend -= 2; | |
3550 | bfd_put_16 (input_bfd, addend, where); | |
4a335f3d AM |
3551 | break; |
3552 | case 2: | |
3553 | if (howto->pc_relative) | |
3554 | addend -= 4; | |
3555 | bfd_put_32 (input_bfd, addend, where); | |
3556 | break; | |
3557 | } | |
f8df10f4 | 3558 | } |
0e1862bb | 3559 | else if (!bfd_link_relocatable (info) |
1f85278f | 3560 | && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC) |
c25bc9fc L |
3561 | { |
3562 | /* Relocate against local STT_GNU_IFUNC symbol. */ | |
0afcef53 L |
3563 | h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel, |
3564 | FALSE); | |
c25bc9fc L |
3565 | if (h == NULL) |
3566 | abort (); | |
3567 | ||
23209a78 | 3568 | /* Set STT_GNU_IFUNC symbol value. */ |
c25bc9fc L |
3569 | h->root.u.def.value = sym->st_value; |
3570 | h->root.u.def.section = sec; | |
3571 | } | |
252b5132 RH |
3572 | } |
3573 | else | |
3574 | { | |
3d540e93 | 3575 | bfd_boolean warned ATTRIBUTE_UNUSED; |
62d887d4 | 3576 | bfd_boolean ignored ATTRIBUTE_UNUSED; |
ffb2e45b | 3577 | |
b2a8e766 AM |
3578 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, |
3579 | r_symndx, symtab_hdr, sym_hashes, | |
3580 | h, sec, relocation, | |
62d887d4 | 3581 | unresolved_reloc, warned, ignored); |
1788fc08 | 3582 | st_size = h->size; |
252b5132 RH |
3583 | } |
3584 | ||
dbaa2011 | 3585 | if (sec != NULL && discarded_section (sec)) |
60f2e42e L |
3586 | { |
3587 | _bfd_clear_contents (howto, input_bfd, input_section, | |
3588 | contents + rel->r_offset); | |
3589 | wrel->r_offset = rel->r_offset; | |
3590 | wrel->r_info = 0; | |
3591 | wrel->r_addend = 0; | |
3592 | ||
3593 | /* For ld -r, remove relocations in debug sections against | |
3594 | sections defined in discarded sections. Not done for | |
3595 | eh_frame editing code expects to be present. */ | |
3596 | if (bfd_link_relocatable (info) | |
3597 | && (input_section->flags & SEC_DEBUGGING)) | |
3598 | wrel--; | |
3599 | ||
3600 | continue; | |
3601 | } | |
9635fe29 | 3602 | |
0e1862bb | 3603 | if (bfd_link_relocatable (info)) |
2d5da473 L |
3604 | { |
3605 | if (wrel != rel) | |
3606 | *wrel = *rel; | |
3607 | continue; | |
3608 | } | |
ab96bf03 | 3609 | |
0afcef53 | 3610 | eh = (struct elf_x86_link_hash_entry *) h; |
ee2fdd6f | 3611 | |
cbe950e9 L |
3612 | /* Since STT_GNU_IFUNC symbol must go through PLT, we handle |
3613 | it here if it is defined in a non-shared object. */ | |
3614 | if (h != NULL | |
3615 | && h->type == STT_GNU_IFUNC | |
3616 | && h->def_regular) | |
3617 | { | |
ee2fdd6f | 3618 | asection *gotplt, *base_got; |
cbe950e9 | 3619 | bfd_vma plt_index; |
4c544807 | 3620 | const char *name; |
cbe950e9 | 3621 | |
97dc35c8 L |
3622 | if ((input_section->flags & SEC_ALLOC) == 0) |
3623 | { | |
3624 | /* Dynamic relocs are not propagated for SEC_DEBUGGING | |
3625 | sections because such sections are not SEC_ALLOC and | |
3626 | thus ld.so will not process them. */ | |
3627 | if ((input_section->flags & SEC_DEBUGGING) != 0) | |
0eace210 | 3628 | continue; |
97dc35c8 L |
3629 | abort (); |
3630 | } | |
cbe950e9 L |
3631 | |
3632 | /* STT_GNU_IFUNC symbol must go through PLT. */ | |
6de2ae4a | 3633 | if (htab->elf.splt != NULL) |
cbe950e9 | 3634 | { |
ee2fdd6f L |
3635 | if (htab->plt_second != NULL) |
3636 | { | |
3637 | resolved_plt = htab->plt_second; | |
3638 | plt_offset = eh->plt_second.offset; | |
3639 | } | |
3640 | else | |
3641 | { | |
3642 | resolved_plt = htab->elf.splt; | |
3643 | plt_offset = h->plt.offset; | |
3644 | } | |
6de2ae4a | 3645 | gotplt = htab->elf.sgotplt; |
cbe950e9 L |
3646 | } |
3647 | else | |
3648 | { | |
ee2fdd6f L |
3649 | resolved_plt = htab->elf.iplt; |
3650 | plt_offset = h->plt.offset; | |
6de2ae4a | 3651 | gotplt = htab->elf.igotplt; |
cbe950e9 L |
3652 | } |
3653 | ||
cbe950e9 L |
3654 | switch (r_type) |
3655 | { | |
3656 | default: | |
233cc9c1 | 3657 | break; |
cbe950e9 L |
3658 | |
3659 | case R_386_GOT32: | |
02a86693 | 3660 | case R_386_GOT32X: |
6de2ae4a | 3661 | base_got = htab->elf.sgot; |
cbe950e9 L |
3662 | off = h->got.offset; |
3663 | ||
7afd84dc | 3664 | if (base_got == NULL) |
cbe950e9 L |
3665 | abort (); |
3666 | ||
7afd84dc | 3667 | if (off == (bfd_vma) -1) |
cbe950e9 | 3668 | { |
7afd84dc L |
3669 | /* We can't use h->got.offset here to save state, or |
3670 | even just remember the offset, as finish_dynamic_symbol | |
3671 | would use that as offset into .got. */ | |
6bbec505 | 3672 | |
233cc9c1 L |
3673 | if (h->plt.offset == (bfd_vma) -1) |
3674 | abort (); | |
3675 | ||
6de2ae4a | 3676 | if (htab->elf.splt != NULL) |
7afd84dc | 3677 | { |
f604c2a2 | 3678 | plt_index = (h->plt.offset / plt_entry_size |
0afcef53 | 3679 | - elf_i386_plt (htab).has_plt0); |
7afd84dc | 3680 | off = (plt_index + 3) * 4; |
6de2ae4a | 3681 | base_got = htab->elf.sgotplt; |
7afd84dc | 3682 | } |
cbe950e9 L |
3683 | else |
3684 | { | |
25e762b9 | 3685 | plt_index = h->plt.offset / plt_entry_size; |
7afd84dc | 3686 | off = plt_index * 4; |
6de2ae4a | 3687 | base_got = htab->elf.igotplt; |
7afd84dc L |
3688 | } |
3689 | ||
3690 | if (h->dynindx == -1 | |
3691 | || h->forced_local | |
3692 | || info->symbolic) | |
3693 | { | |
3694 | /* This references the local defitionion. We must | |
3695 | initialize this entry in the global offset table. | |
3696 | Since the offset must always be a multiple of 8, | |
3697 | we use the least significant bit to record | |
3698 | whether we have initialized it already. | |
3699 | ||
3700 | When doing a dynamic link, we create a .rela.got | |
3701 | relocation entry to initialize the value. This | |
3702 | is done in the finish_dynamic_symbol routine. */ | |
3703 | if ((off & 1) != 0) | |
3704 | off &= ~1; | |
3705 | else | |
3706 | { | |
3707 | bfd_put_32 (output_bfd, relocation, | |
3708 | base_got->contents + off); | |
3709 | h->got.offset |= 1; | |
3710 | } | |
cbe950e9 | 3711 | } |
cbe950e9 | 3712 | |
7afd84dc | 3713 | relocation = off; |
7afd84dc L |
3714 | } |
3715 | else | |
ca8c86ef L |
3716 | relocation = (base_got->output_section->vma |
3717 | + base_got->output_offset + off | |
3718 | - gotplt->output_section->vma | |
3719 | - gotplt->output_offset); | |
3720 | ||
2a568401 L |
3721 | if (rel->r_offset > 1 |
3722 | && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5 | |
3723 | && *(contents + rel->r_offset - 2) != 0x8d) | |
712ec279 L |
3724 | { |
3725 | if (bfd_link_pic (info)) | |
3726 | goto disallow_got32; | |
3727 | ||
3728 | /* Add the GOT base if there is no base register. */ | |
3729 | relocation += (gotplt->output_section->vma | |
3730 | + gotplt->output_offset); | |
3731 | } | |
3732 | else if (htab->elf.splt == NULL) | |
3733 | { | |
3734 | /* Adjust for static executables. */ | |
3735 | relocation += gotplt->output_offset; | |
3736 | } | |
0018b0a3 | 3737 | |
cbe950e9 | 3738 | goto do_relocation; |
233cc9c1 L |
3739 | } |
3740 | ||
3741 | if (h->plt.offset == (bfd_vma) -1) | |
3742 | { | |
3743 | /* Handle static pointers of STT_GNU_IFUNC symbols. */ | |
3744 | if (r_type == R_386_32 | |
3745 | && (input_section->flags & SEC_CODE) == 0) | |
3746 | goto do_ifunc_pointer; | |
3747 | goto bad_ifunc_reloc; | |
3748 | } | |
3749 | ||
ee2fdd6f L |
3750 | relocation = (resolved_plt->output_section->vma |
3751 | + resolved_plt->output_offset + plt_offset); | |
233cc9c1 L |
3752 | |
3753 | switch (r_type) | |
3754 | { | |
3755 | default: | |
3756 | bad_ifunc_reloc: | |
3757 | if (h->root.root.string) | |
3758 | name = h->root.root.string; | |
3759 | else | |
3760 | name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, | |
3761 | NULL); | |
4eca0228 | 3762 | _bfd_error_handler |
695344c0 | 3763 | /* xgettext:c-format */ |
233cc9c1 L |
3764 | (_("%B: relocation %s against STT_GNU_IFUNC " |
3765 | "symbol `%s' isn't supported"), input_bfd, | |
3766 | howto->name, name); | |
3767 | bfd_set_error (bfd_error_bad_value); | |
3768 | return FALSE; | |
3769 | ||
3770 | case R_386_32: | |
3771 | /* Generate dynamic relcoation only when there is a | |
3772 | non-GOT reference in a shared object. */ | |
3773 | if ((bfd_link_pic (info) && h->non_got_ref) | |
3774 | || h->plt.offset == (bfd_vma) -1) | |
3775 | { | |
3776 | Elf_Internal_Rela outrel; | |
3777 | asection *sreloc; | |
3778 | bfd_vma offset; | |
3779 | ||
3780 | do_ifunc_pointer: | |
3781 | /* Need a dynamic relocation to get the real function | |
3782 | adddress. */ | |
3783 | offset = _bfd_elf_section_offset (output_bfd, | |
3784 | info, | |
3785 | input_section, | |
3786 | rel->r_offset); | |
3787 | if (offset == (bfd_vma) -1 | |
3788 | || offset == (bfd_vma) -2) | |
3789 | abort (); | |
3790 | ||
3791 | outrel.r_offset = (input_section->output_section->vma | |
3792 | + input_section->output_offset | |
3793 | + offset); | |
3794 | ||
3795 | if (h->dynindx == -1 | |
3796 | || h->forced_local | |
3797 | || bfd_link_executable (info)) | |
3798 | { | |
6322e5c5 L |
3799 | info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"), |
3800 | h->root.root.string, | |
3801 | h->root.u.def.section->owner); | |
3802 | ||
233cc9c1 L |
3803 | /* This symbol is resolved locally. */ |
3804 | outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE); | |
3805 | bfd_put_32 (output_bfd, | |
3806 | (h->root.u.def.value | |
3807 | + h->root.u.def.section->output_section->vma | |
3808 | + h->root.u.def.section->output_offset), | |
3809 | contents + offset); | |
3810 | } | |
3811 | else | |
3812 | outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); | |
3813 | ||
3814 | /* Dynamic relocations are stored in | |
3815 | 1. .rel.ifunc section in PIC object. | |
3816 | 2. .rel.got section in dynamic executable. | |
3817 | 3. .rel.iplt section in static executable. */ | |
3818 | if (bfd_link_pic (info)) | |
3819 | sreloc = htab->elf.irelifunc; | |
3820 | else if (htab->elf.splt != NULL) | |
3821 | sreloc = htab->elf.srelgot; | |
3822 | else | |
3823 | sreloc = htab->elf.irelplt; | |
3824 | elf_append_rel (output_bfd, sreloc, &outrel); | |
3825 | ||
3826 | /* If this reloc is against an external symbol, we | |
3827 | do not want to fiddle with the addend. Otherwise, | |
3828 | we need to include the symbol value so that it | |
3829 | becomes an addend for the dynamic reloc. For an | |
3830 | internal symbol, we have updated addend. */ | |
3831 | continue; | |
3832 | } | |
3833 | /* FALLTHROUGH */ | |
3834 | case R_386_PC32: | |
3835 | case R_386_PLT32: | |
3836 | goto do_relocation; | |
cbe950e9 L |
3837 | |
3838 | case R_386_GOTOFF: | |
3839 | relocation -= (gotplt->output_section->vma | |
3840 | + gotplt->output_offset); | |
3841 | goto do_relocation; | |
3842 | } | |
3843 | } | |
3844 | ||
aec6b87e | 3845 | resolved_to_zero = (eh != NULL |
e62b9723 | 3846 | && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, |
0afcef53 | 3847 | I386_ELF_DATA, |
e62b9723 L |
3848 | eh->has_got_reloc, |
3849 | eh)); | |
aec6b87e | 3850 | |
252b5132 RH |
3851 | switch (r_type) |
3852 | { | |
02a86693 L |
3853 | case R_386_GOT32X: |
3854 | /* Avoid optimizing _DYNAMIC since ld.so may use its | |
3855 | link-time address. */ | |
3856 | if (h == htab->elf.hdynamic) | |
3857 | goto r_386_got32; | |
3858 | ||
3859 | if (bfd_link_pic (info)) | |
3860 | { | |
3861 | /* It is OK to convert mov to lea and convert indirect | |
3862 | branch to direct branch. It is OK to convert adc, | |
3863 | add, and, cmp, or, sbb, sub, test, xor only when PIC | |
3864 | is false. */ | |
02e2aef8 L |
3865 | unsigned int opcode, addend; |
3866 | addend = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
3867 | if (addend != 0) | |
3868 | goto r_386_got32; | |
3869 | opcode = bfd_get_8 (input_bfd, contents + rel->r_offset - 2); | |
02a86693 L |
3870 | if (opcode != 0x8b && opcode != 0xff) |
3871 | goto r_386_got32; | |
3872 | } | |
3873 | ||
3874 | /* Resolve "mov GOT[(%reg)], %reg", | |
3875 | "call/jmp *GOT[(%reg)]", "test %reg, foo@GOT[(%reg)]" | |
3876 | and "binop foo@GOT[(%reg)], %reg". */ | |
3877 | if (h == NULL | |
3878 | || (h->plt.offset == (bfd_vma) -1 | |
3879 | && h->got.offset == (bfd_vma) -1) | |
3880 | || htab->elf.sgotplt == NULL) | |
3881 | abort (); | |
3882 | ||
3883 | offplt = (htab->elf.sgotplt->output_section->vma | |
3884 | + htab->elf.sgotplt->output_offset); | |
3885 | ||
3886 | /* It is relative to .got.plt section. */ | |
3887 | if (h->got.offset != (bfd_vma) -1) | |
32875eb1 L |
3888 | /* Use GOT entry. Mask off the least significant bit in |
3889 | GOT offset which may be set by R_386_GOT32 processing | |
3890 | below. */ | |
02a86693 L |
3891 | relocation = (htab->elf.sgot->output_section->vma |
3892 | + htab->elf.sgot->output_offset | |
32875eb1 | 3893 | + (h->got.offset & ~1) - offplt); |
02a86693 L |
3894 | else |
3895 | /* Use GOTPLT entry. */ | |
f604c2a2 | 3896 | relocation = (h->plt.offset / plt_entry_size |
0afcef53 | 3897 | - elf_i386_plt (htab).has_plt0 + 3) * 4; |
02a86693 L |
3898 | |
3899 | if (!bfd_link_pic (info)) | |
3900 | { | |
3901 | /* If not PIC, add the .got.plt section address for | |
a3718e9e | 3902 | baseless addressing. */ |
02a86693 | 3903 | unsigned int modrm; |
02e2aef8 | 3904 | modrm = bfd_get_8 (input_bfd, contents + rel->r_offset - 1); |
02a86693 L |
3905 | if ((modrm & 0xc7) == 0x5) |
3906 | relocation += offplt; | |
3907 | } | |
3908 | ||
3909 | unresolved_reloc = FALSE; | |
3910 | break; | |
3911 | ||
252b5132 | 3912 | case R_386_GOT32: |
02a86693 | 3913 | r_386_got32: |
252b5132 RH |
3914 | /* Relocation is to the entry for this symbol in the global |
3915 | offset table. */ | |
6de2ae4a | 3916 | if (htab->elf.sgot == NULL) |
ffb2e45b | 3917 | abort (); |
252b5132 | 3918 | |
e133d005 | 3919 | relative_reloc = FALSE; |
252b5132 RH |
3920 | if (h != NULL) |
3921 | { | |
b34976b6 | 3922 | bfd_boolean dyn; |
252b5132 RH |
3923 | |
3924 | off = h->got.offset; | |
ebe50bae | 3925 | dyn = htab->elf.dynamic_sections_created; |
0e1862bb L |
3926 | if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, |
3927 | bfd_link_pic (info), | |
3928 | h) | |
3929 | || (bfd_link_pic (info) | |
586119b3 | 3930 | && SYMBOL_REFERENCES_LOCAL (info, h)) |
ef5aade5 L |
3931 | || (ELF_ST_VISIBILITY (h->other) |
3932 | && h->root.type == bfd_link_hash_undefweak)) | |
252b5132 RH |
3933 | { |
3934 | /* This is actually a static link, or it is a | |
3935 | -Bsymbolic link and the symbol is defined | |
3936 | locally, or the symbol was forced to be local | |
3937 | because of a version file. We must initialize | |
3938 | this entry in the global offset table. Since the | |
3939 | offset must always be a multiple of 4, we use the | |
3940 | least significant bit to record whether we have | |
3941 | initialized it already. | |
3942 | ||
3943 | When doing a dynamic link, we create a .rel.got | |
3944 | relocation entry to initialize the value. This | |
3945 | is done in the finish_dynamic_symbol routine. */ | |
3946 | if ((off & 1) != 0) | |
3947 | off &= ~1; | |
3948 | else | |
3949 | { | |
3950 | bfd_put_32 (output_bfd, relocation, | |
6de2ae4a | 3951 | htab->elf.sgot->contents + off); |
252b5132 | 3952 | h->got.offset |= 1; |
e133d005 L |
3953 | |
3954 | if (h->dynindx == -1 | |
3955 | && !h->forced_local | |
3956 | && h->root.type != bfd_link_hash_undefweak | |
0dc9a308 | 3957 | && bfd_link_pic (info)) |
e133d005 L |
3958 | { |
3959 | /* PR ld/21402: If this symbol isn't dynamic | |
0dc9a308 | 3960 | in PIC, generate R_386_RELATIVE here. */ |
e133d005 L |
3961 | eh->no_finish_dynamic_symbol = 1; |
3962 | relative_reloc = TRUE; | |
3963 | } | |
252b5132 RH |
3964 | } |
3965 | } | |
8c694914 | 3966 | else |
b34976b6 | 3967 | unresolved_reloc = FALSE; |
252b5132 RH |
3968 | } |
3969 | else | |
3970 | { | |
ffb2e45b AM |
3971 | if (local_got_offsets == NULL) |
3972 | abort (); | |
252b5132 RH |
3973 | |
3974 | off = local_got_offsets[r_symndx]; | |
3975 | ||
3976 | /* The offset must always be a multiple of 4. We use | |
83be169b AM |
3977 | the least significant bit to record whether we have |
3978 | already generated the necessary reloc. */ | |
252b5132 RH |
3979 | if ((off & 1) != 0) |
3980 | off &= ~1; | |
3981 | else | |
3982 | { | |
6725bdbf | 3983 | bfd_put_32 (output_bfd, relocation, |
6de2ae4a | 3984 | htab->elf.sgot->contents + off); |
e133d005 | 3985 | local_got_offsets[r_symndx] |= 1; |
252b5132 | 3986 | |
0e1862bb | 3987 | if (bfd_link_pic (info)) |
e133d005 | 3988 | relative_reloc = TRUE; |
252b5132 | 3989 | } |
252b5132 RH |
3990 | } |
3991 | ||
e133d005 L |
3992 | if (relative_reloc) |
3993 | { | |
3994 | asection *s; | |
3995 | Elf_Internal_Rela outrel; | |
3996 | ||
3997 | s = htab->elf.srelgot; | |
3998 | if (s == NULL) | |
3999 | abort (); | |
4000 | ||
4001 | outrel.r_offset = (htab->elf.sgot->output_section->vma | |
4002 | + htab->elf.sgot->output_offset | |
4003 | + off); | |
4004 | outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); | |
4005 | elf_append_rel (output_bfd, s, &outrel); | |
4006 | } | |
4007 | ||
ffb2e45b AM |
4008 | if (off >= (bfd_vma) -2) |
4009 | abort (); | |
4010 | ||
74d7f0aa L |
4011 | relocation = (htab->elf.sgot->output_section->vma |
4012 | + htab->elf.sgot->output_offset + off); | |
2a568401 L |
4013 | if (rel->r_offset > 1 |
4014 | && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5 | |
4015 | && *(contents + rel->r_offset - 2) != 0x8d) | |
74d7f0aa L |
4016 | { |
4017 | if (bfd_link_pic (info)) | |
4018 | { | |
4019 | /* For PIC, disallow R_386_GOT32 without a base | |
2a568401 L |
4020 | register, except for "lea foo@GOT, %reg", since |
4021 | we don't know what the GOT base is. */ | |
74d7f0aa L |
4022 | const char *name; |
4023 | ||
712ec279 | 4024 | disallow_got32: |
a5def14f | 4025 | if (h == NULL || h->root.root.string == NULL) |
74d7f0aa L |
4026 | name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, |
4027 | NULL); | |
4028 | else | |
4029 | name = h->root.root.string; | |
4030 | ||
4eca0228 | 4031 | _bfd_error_handler |
695344c0 | 4032 | /* xgettext:c-format */ |
63a5468a AM |
4033 | (_("%B: direct GOT relocation %s against `%s'" |
4034 | " without base register can not be used" | |
4035 | " when making a shared object"), | |
712ec279 | 4036 | input_bfd, howto->name, name); |
74d7f0aa L |
4037 | bfd_set_error (bfd_error_bad_value); |
4038 | return FALSE; | |
4039 | } | |
4040 | } | |
4041 | else | |
4042 | { | |
4043 | /* Subtract the .got.plt section address only with a base | |
4044 | register. */ | |
4045 | relocation -= (htab->elf.sgotplt->output_section->vma | |
4046 | + htab->elf.sgotplt->output_offset); | |
4047 | } | |
4048 | ||
252b5132 RH |
4049 | break; |
4050 | ||
4051 | case R_386_GOTOFF: | |
4052 | /* Relocation is relative to the start of the global offset | |
4053 | table. */ | |
4054 | ||
3d949995 L |
4055 | /* Check to make sure it isn't a protected function or data |
4056 | symbol for shared library since it may not be local when | |
4057 | used as function address or with copy relocation. We also | |
e3c0e327 | 4058 | need to make sure that a symbol is referenced locally. */ |
0e1862bb | 4059 | if (!bfd_link_executable (info) && h) |
90f487df | 4060 | { |
41bed6dd L |
4061 | if (!h->def_regular) |
4062 | { | |
4063 | const char *v; | |
4064 | ||
4065 | switch (ELF_ST_VISIBILITY (h->other)) | |
4066 | { | |
4067 | case STV_HIDDEN: | |
4068 | v = _("hidden symbol"); | |
4069 | break; | |
4070 | case STV_INTERNAL: | |
4071 | v = _("internal symbol"); | |
4072 | break; | |
4073 | case STV_PROTECTED: | |
4074 | v = _("protected symbol"); | |
4075 | break; | |
4076 | default: | |
4077 | v = _("symbol"); | |
4078 | break; | |
4079 | } | |
4080 | ||
4eca0228 | 4081 | _bfd_error_handler |
695344c0 | 4082 | /* xgettext:c-format */ |
63a5468a AM |
4083 | (_("%B: relocation R_386_GOTOFF against undefined %s" |
4084 | " `%s' can not be used when making a shared object"), | |
41bed6dd L |
4085 | input_bfd, v, h->root.root.string); |
4086 | bfd_set_error (bfd_error_bad_value); | |
4087 | return FALSE; | |
4088 | } | |
d5597ebc | 4089 | else if (!SYMBOL_REFERENCES_LOCAL (info, h) |
3d949995 L |
4090 | && (h->type == STT_FUNC |
4091 | || h->type == STT_OBJECT) | |
41bed6dd L |
4092 | && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) |
4093 | { | |
4eca0228 | 4094 | _bfd_error_handler |
695344c0 | 4095 | /* xgettext:c-format */ |
63a5468a AM |
4096 | (_("%B: relocation R_386_GOTOFF against protected %s" |
4097 | " `%s' can not be used when making a shared object"), | |
3d949995 L |
4098 | input_bfd, |
4099 | h->type == STT_FUNC ? "function" : "data", | |
4100 | h->root.root.string); | |
41bed6dd L |
4101 | bfd_set_error (bfd_error_bad_value); |
4102 | return FALSE; | |
4103 | } | |
90f487df L |
4104 | } |
4105 | ||
8c37241b JJ |
4106 | /* Note that sgot is not involved in this |
4107 | calculation. We always want the start of .got.plt. If we | |
4108 | defined _GLOBAL_OFFSET_TABLE_ in a different way, as is | |
252b5132 RH |
4109 | permitted by the ABI, we might have to change this |
4110 | calculation. */ | |
6de2ae4a L |
4111 | relocation -= htab->elf.sgotplt->output_section->vma |
4112 | + htab->elf.sgotplt->output_offset; | |
252b5132 RH |
4113 | break; |
4114 | ||
4115 | case R_386_GOTPC: | |
4116 | /* Use global offset table as symbol value. */ | |
6de2ae4a L |
4117 | relocation = htab->elf.sgotplt->output_section->vma |
4118 | + htab->elf.sgotplt->output_offset; | |
b34976b6 | 4119 | unresolved_reloc = FALSE; |
252b5132 RH |
4120 | break; |
4121 | ||
4122 | case R_386_PLT32: | |
4123 | /* Relocation is to the entry for this symbol in the | |
4124 | procedure linkage table. */ | |
4125 | ||
dd5724d5 | 4126 | /* Resolve a PLT32 reloc against a local symbol directly, |
83be169b | 4127 | without using the procedure linkage table. */ |
252b5132 RH |
4128 | if (h == NULL) |
4129 | break; | |
4130 | ||
dd7e64d4 L |
4131 | if ((h->plt.offset == (bfd_vma) -1 |
4132 | && eh->plt_got.offset == (bfd_vma) -1) | |
6de2ae4a | 4133 | || htab->elf.splt == NULL) |
252b5132 RH |
4134 | { |
4135 | /* We didn't make a PLT entry for this symbol. This | |
83be169b AM |
4136 | happens when statically linking PIC code, or when |
4137 | using -Bsymbolic. */ | |
252b5132 RH |
4138 | break; |
4139 | } | |
4140 | ||
dd7e64d4 L |
4141 | if (h->plt.offset != (bfd_vma) -1) |
4142 | { | |
ee2fdd6f L |
4143 | if (htab->plt_second != NULL) |
4144 | { | |
4145 | resolved_plt = htab->plt_second; | |
4146 | plt_offset = eh->plt_second.offset; | |
4147 | } | |
4148 | else | |
4149 | { | |
4150 | resolved_plt = htab->elf.splt; | |
4151 | plt_offset = h->plt.offset; | |
4152 | } | |
dd7e64d4 L |
4153 | } |
4154 | else | |
4155 | { | |
4156 | resolved_plt = htab->plt_got; | |
4157 | plt_offset = eh->plt_got.offset; | |
4158 | } | |
4159 | ||
4160 | relocation = (resolved_plt->output_section->vma | |
4161 | + resolved_plt->output_offset | |
4162 | + plt_offset); | |
b34976b6 | 4163 | unresolved_reloc = FALSE; |
252b5132 RH |
4164 | break; |
4165 | ||
1788fc08 | 4166 | case R_386_SIZE32: |
1788fc08 L |
4167 | /* Set to symbol size. */ |
4168 | relocation = st_size; | |
4169 | /* Fall through. */ | |
4170 | ||
252b5132 RH |
4171 | case R_386_32: |
4172 | case R_386_PC32: | |
3348747a NS |
4173 | if ((input_section->flags & SEC_ALLOC) == 0 |
4174 | || is_vxworks_tls) | |
ec338859 AM |
4175 | break; |
4176 | ||
aec6b87e L |
4177 | /* Copy dynamic function pointer relocations. Don't generate |
4178 | dynamic relocations against resolved undefined weak symbols | |
4179 | in PIE, except for R_386_PC32. */ | |
0e1862bb | 4180 | if ((bfd_link_pic (info) |
ef5aade5 | 4181 | && (h == NULL |
aec6b87e L |
4182 | || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT |
4183 | && (!resolved_to_zero | |
4184 | || r_type == R_386_PC32)) | |
4185 | || h->root.type != bfd_link_hash_undefweak)) | |
1788fc08 | 4186 | && ((r_type != R_386_PC32 && r_type != R_386_SIZE32) |
f6c52c13 | 4187 | || !SYMBOL_CALLS_LOCAL (info, h))) |
a23b6845 | 4188 | || (ELIMINATE_COPY_RELOCS |
0e1862bb | 4189 | && !bfd_link_pic (info) |
12d0ee4a AM |
4190 | && h != NULL |
4191 | && h->dynindx != -1 | |
aec6b87e L |
4192 | && (!h->non_got_ref |
4193 | || eh->func_pointer_refcount > 0 | |
4194 | || (h->root.type == bfd_link_hash_undefweak | |
4195 | && !resolved_to_zero)) | |
bae420ef L |
4196 | && ((h->def_dynamic && !h->def_regular) |
4197 | /* Undefined weak symbol is bound locally when | |
4198 | PIC is false. */ | |
4199 | || h->root.type == bfd_link_hash_undefweak))) | |
252b5132 | 4200 | { |
947216bf | 4201 | Elf_Internal_Rela outrel; |
b34976b6 | 4202 | bfd_boolean skip, relocate; |
0c715baa | 4203 | asection *sreloc; |
252b5132 RH |
4204 | |
4205 | /* When generating a shared object, these relocations | |
4206 | are copied into the output file to be resolved at run | |
4207 | time. */ | |
4208 | ||
b34976b6 AM |
4209 | skip = FALSE; |
4210 | relocate = FALSE; | |
252b5132 | 4211 | |
c629eae0 JJ |
4212 | outrel.r_offset = |
4213 | _bfd_elf_section_offset (output_bfd, info, input_section, | |
4214 | rel->r_offset); | |
4215 | if (outrel.r_offset == (bfd_vma) -1) | |
b34976b6 | 4216 | skip = TRUE; |
0bb2d96a | 4217 | else if (outrel.r_offset == (bfd_vma) -2) |
b34976b6 | 4218 | skip = TRUE, relocate = TRUE; |
252b5132 RH |
4219 | outrel.r_offset += (input_section->output_section->vma |
4220 | + input_section->output_offset); | |
4221 | ||
4222 | if (skip) | |
0bb2d96a | 4223 | memset (&outrel, 0, sizeof outrel); |
5a15f56f AM |
4224 | else if (h != NULL |
4225 | && h->dynindx != -1 | |
4226 | && (r_type == R_386_PC32 | |
4e0c91e4 L |
4227 | || !(bfd_link_executable (info) |
4228 | || SYMBOLIC_BIND (info, h)) | |
f5385ebf | 4229 | || !h->def_regular)) |
0bb2d96a | 4230 | outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); |
252b5132 RH |
4231 | else |
4232 | { | |
5a15f56f | 4233 | /* This symbol is local, or marked to become local. */ |
b34976b6 | 4234 | relocate = TRUE; |
5a15f56f | 4235 | outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); |
252b5132 RH |
4236 | } |
4237 | ||
cbe950e9 | 4238 | sreloc = elf_section_data (input_section)->sreloc; |
e7c33416 | 4239 | |
62d78908 L |
4240 | if (sreloc == NULL || sreloc->contents == NULL) |
4241 | { | |
4242 | r = bfd_reloc_notsupported; | |
4243 | goto check_relocation_error; | |
4244 | } | |
0c715baa | 4245 | |
59d6ffb2 | 4246 | elf_append_rel (output_bfd, sreloc, &outrel); |
252b5132 RH |
4247 | |
4248 | /* If this reloc is against an external symbol, we do | |
4249 | not want to fiddle with the addend. Otherwise, we | |
4250 | need to include the symbol value so that it becomes | |
4251 | an addend for the dynamic reloc. */ | |
0f88be7a | 4252 | if (! relocate) |
252b5132 RH |
4253 | continue; |
4254 | } | |
252b5132 RH |
4255 | break; |
4256 | ||
37e55690 | 4257 | case R_386_TLS_IE: |
0e1862bb | 4258 | if (!bfd_link_executable (info)) |
37e55690 | 4259 | { |
947216bf | 4260 | Elf_Internal_Rela outrel; |
37e55690 | 4261 | asection *sreloc; |
37e55690 JJ |
4262 | |
4263 | outrel.r_offset = rel->r_offset | |
4264 | + input_section->output_section->vma | |
4265 | + input_section->output_offset; | |
4266 | outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); | |
4267 | sreloc = elf_section_data (input_section)->sreloc; | |
4268 | if (sreloc == NULL) | |
4269 | abort (); | |
59d6ffb2 | 4270 | elf_append_rel (output_bfd, sreloc, &outrel); |
37e55690 JJ |
4271 | } |
4272 | /* Fall through */ | |
4273 | ||
13ae64f3 | 4274 | case R_386_TLS_GD: |
67a4f2b7 AO |
4275 | case R_386_TLS_GOTDESC: |
4276 | case R_386_TLS_DESC_CALL: | |
13ae64f3 | 4277 | case R_386_TLS_IE_32: |
37e55690 | 4278 | case R_386_TLS_GOTIE: |
13ae64f3 JJ |
4279 | tls_type = GOT_UNKNOWN; |
4280 | if (h == NULL && local_got_offsets) | |
0afcef53 | 4281 | tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx]; |
13ae64f3 | 4282 | else if (h != NULL) |
0afcef53 | 4283 | tls_type = elf_x86_hash_entry(h)->tls_type; |
37e55690 JJ |
4284 | if (tls_type == GOT_TLS_IE) |
4285 | tls_type = GOT_TLS_IE_NEG; | |
142411ca L |
4286 | |
4287 | if (! elf_i386_tls_transition (info, input_bfd, | |
4288 | input_section, contents, | |
4289 | symtab_hdr, sym_hashes, | |
4290 | &r_type, tls_type, rel, | |
bedfd056 | 4291 | relend, h, r_symndx, TRUE)) |
142411ca | 4292 | return FALSE; |
13ae64f3 JJ |
4293 | |
4294 | if (r_type == R_386_TLS_LE_32) | |
4295 | { | |
82e51918 | 4296 | BFD_ASSERT (! unresolved_reloc); |
13ae64f3 JJ |
4297 | if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD) |
4298 | { | |
142411ca | 4299 | unsigned int type; |
13ae64f3 JJ |
4300 | bfd_vma roff; |
4301 | ||
4302 | /* GD->LE transition. */ | |
6eaa7fb5 | 4303 | type = *(contents + rel->r_offset - 2); |
13ae64f3 JJ |
4304 | if (type == 0x04) |
4305 | { | |
6eaa7fb5 L |
4306 | /* Change |
4307 | leal foo@tlsgd(,%ebx,1), %eax | |
4308 | call ___tls_get_addr@PLT | |
4309 | into: | |
4310 | movl %gs:0, %eax | |
4311 | subl $foo@tpoff, %eax | |
13ae64f3 | 4312 | (6 byte form of subl). */ |
142411ca | 4313 | roff = rel->r_offset + 5; |
13ae64f3 JJ |
4314 | } |
4315 | else | |
4316 | { | |
6eaa7fb5 L |
4317 | /* Change |
4318 | leal foo@tlsgd(%ebx), %eax | |
4319 | call ___tls_get_addr@PLT | |
4320 | nop | |
4321 | or | |
4322 | leal foo@tlsgd(%reg), %eax | |
4323 | call *___tls_get_addr@GOT(%reg) | |
4324 | which may be converted to | |
4325 | addr32 call ___tls_get_addr | |
4326 | into: | |
4327 | movl %gs:0, %eax; subl $foo@tpoff, %eax | |
142411ca | 4328 | (6 byte form of subl). */ |
142411ca | 4329 | roff = rel->r_offset + 6; |
13ae64f3 | 4330 | } |
6eaa7fb5 L |
4331 | memcpy (contents + roff - 8, |
4332 | "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12); | |
eb4ff4d6 | 4333 | bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation), |
13ae64f3 | 4334 | contents + roff); |
6eaa7fb5 | 4335 | /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */ |
13ae64f3 | 4336 | rel++; |
60f2e42e | 4337 | wrel++; |
13ae64f3 JJ |
4338 | continue; |
4339 | } | |
67a4f2b7 AO |
4340 | else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC) |
4341 | { | |
4342 | /* GDesc -> LE transition. | |
4343 | It's originally something like: | |
4344 | leal x@tlsdesc(%ebx), %eax | |
4345 | ||
4346 | leal x@ntpoff, %eax | |
4347 | ||
4348 | Registers other than %eax may be set up here. */ | |
4349 | ||
142411ca | 4350 | unsigned int val; |
67a4f2b7 AO |
4351 | bfd_vma roff; |
4352 | ||
67a4f2b7 | 4353 | roff = rel->r_offset; |
67a4f2b7 | 4354 | val = bfd_get_8 (input_bfd, contents + roff - 1); |
67a4f2b7 AO |
4355 | |
4356 | /* Now modify the instruction as appropriate. */ | |
4357 | /* aoliva FIXME: remove the above and xor the byte | |
4358 | below with 0x86. */ | |
4359 | bfd_put_8 (output_bfd, val ^ 0x86, | |
4360 | contents + roff - 1); | |
eb4ff4d6 | 4361 | bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation), |
67a4f2b7 AO |
4362 | contents + roff); |
4363 | continue; | |
4364 | } | |
4365 | else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL) | |
4366 | { | |
4367 | /* GDesc -> LE transition. | |
4368 | It's originally: | |
4369 | call *(%eax) | |
4370 | Turn it into: | |
142411ca | 4371 | xchg %ax,%ax */ |
67a4f2b7 | 4372 | |
67a4f2b7 | 4373 | bfd_vma roff; |
6bbec505 | 4374 | |
67a4f2b7 | 4375 | roff = rel->r_offset; |
10efb593 | 4376 | bfd_put_8 (output_bfd, 0x66, contents + roff); |
67a4f2b7 AO |
4377 | bfd_put_8 (output_bfd, 0x90, contents + roff + 1); |
4378 | continue; | |
4379 | } | |
37e55690 | 4380 | else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE) |
13ae64f3 | 4381 | { |
142411ca | 4382 | unsigned int val; |
13ae64f3 JJ |
4383 | |
4384 | /* IE->LE transition: | |
37e55690 JJ |
4385 | Originally it can be one of: |
4386 | movl foo, %eax | |
4387 | movl foo, %reg | |
4388 | addl foo, %reg | |
4389 | We change it into: | |
4390 | movl $foo, %eax | |
4391 | movl $foo, %reg | |
4392 | addl $foo, %reg. */ | |
37e55690 | 4393 | val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1); |
37e55690 JJ |
4394 | if (val == 0xa1) |
4395 | { | |
4396 | /* movl foo, %eax. */ | |
55fd94b0 AM |
4397 | bfd_put_8 (output_bfd, 0xb8, |
4398 | contents + rel->r_offset - 1); | |
37e55690 | 4399 | } |
299bf759 | 4400 | else |
37e55690 | 4401 | { |
142411ca L |
4402 | unsigned int type; |
4403 | ||
55fd94b0 AM |
4404 | type = bfd_get_8 (input_bfd, |
4405 | contents + rel->r_offset - 2); | |
299bf759 | 4406 | switch (type) |
26e41594 | 4407 | { |
299bf759 L |
4408 | case 0x8b: |
4409 | /* movl */ | |
299bf759 L |
4410 | bfd_put_8 (output_bfd, 0xc7, |
4411 | contents + rel->r_offset - 2); | |
4412 | bfd_put_8 (output_bfd, | |
4413 | 0xc0 | ((val >> 3) & 7), | |
4414 | contents + rel->r_offset - 1); | |
4415 | break; | |
4416 | case 0x03: | |
4417 | /* addl */ | |
299bf759 L |
4418 | bfd_put_8 (output_bfd, 0x81, |
4419 | contents + rel->r_offset - 2); | |
4420 | bfd_put_8 (output_bfd, | |
4421 | 0xc0 | ((val >> 3) & 7), | |
4422 | contents + rel->r_offset - 1); | |
4423 | break; | |
4424 | default: | |
4425 | BFD_FAIL (); | |
4426 | break; | |
26e41594 | 4427 | } |
37e55690 | 4428 | } |
eb4ff4d6 | 4429 | bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation), |
37e55690 JJ |
4430 | contents + rel->r_offset); |
4431 | continue; | |
4432 | } | |
4433 | else | |
4434 | { | |
4435 | unsigned int val, type; | |
4436 | ||
4437 | /* {IE_32,GOTIE}->LE transition: | |
4438 | Originally it can be one of: | |
13ae64f3 | 4439 | subl foo(%reg1), %reg2 |
13ae64f3 | 4440 | movl foo(%reg1), %reg2 |
37e55690 | 4441 | addl foo(%reg1), %reg2 |
13ae64f3 JJ |
4442 | We change it into: |
4443 | subl $foo, %reg2 | |
37e55690 JJ |
4444 | movl $foo, %reg2 (6 byte form) |
4445 | addl $foo, %reg2. */ | |
13ae64f3 JJ |
4446 | type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2); |
4447 | val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1); | |
13ae64f3 JJ |
4448 | if (type == 0x8b) |
4449 | { | |
4450 | /* movl */ | |
13ae64f3 JJ |
4451 | bfd_put_8 (output_bfd, 0xc7, |
4452 | contents + rel->r_offset - 2); | |
4453 | bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7), | |
4454 | contents + rel->r_offset - 1); | |
4455 | } | |
4456 | else if (type == 0x2b) | |
4457 | { | |
4458 | /* subl */ | |
13ae64f3 JJ |
4459 | bfd_put_8 (output_bfd, 0x81, |
4460 | contents + rel->r_offset - 2); | |
4461 | bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7), | |
4462 | contents + rel->r_offset - 1); | |
4463 | } | |
37e55690 JJ |
4464 | else if (type == 0x03) |
4465 | { | |
4466 | /* addl */ | |
4467 | bfd_put_8 (output_bfd, 0x81, | |
4468 | contents + rel->r_offset - 2); | |
4469 | bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7), | |
4470 | contents + rel->r_offset - 1); | |
4471 | } | |
13ae64f3 JJ |
4472 | else |
4473 | BFD_FAIL (); | |
37e55690 | 4474 | if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE) |
eb4ff4d6 | 4475 | bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation), |
37e55690 JJ |
4476 | contents + rel->r_offset); |
4477 | else | |
eb4ff4d6 | 4478 | bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation), |
37e55690 | 4479 | contents + rel->r_offset); |
13ae64f3 JJ |
4480 | continue; |
4481 | } | |
4482 | } | |
4483 | ||
6de2ae4a | 4484 | if (htab->elf.sgot == NULL) |
13ae64f3 JJ |
4485 | abort (); |
4486 | ||
4487 | if (h != NULL) | |
67a4f2b7 AO |
4488 | { |
4489 | off = h->got.offset; | |
0afcef53 | 4490 | offplt = elf_x86_hash_entry (h)->tlsdesc_got; |
67a4f2b7 | 4491 | } |
13ae64f3 JJ |
4492 | else |
4493 | { | |
4494 | if (local_got_offsets == NULL) | |
4495 | abort (); | |
4496 | ||
4497 | off = local_got_offsets[r_symndx]; | |
67a4f2b7 | 4498 | offplt = local_tlsdesc_gotents[r_symndx]; |
13ae64f3 JJ |
4499 | } |
4500 | ||
4501 | if ((off & 1) != 0) | |
4502 | off &= ~1; | |
26e41594 | 4503 | else |
13ae64f3 | 4504 | { |
947216bf | 4505 | Elf_Internal_Rela outrel; |
91d6fa6a | 4506 | int dr_type; |
67a4f2b7 | 4507 | asection *sreloc; |
13ae64f3 | 4508 | |
6de2ae4a | 4509 | if (htab->elf.srelgot == NULL) |
13ae64f3 JJ |
4510 | abort (); |
4511 | ||
67a4f2b7 AO |
4512 | indx = h && h->dynindx != -1 ? h->dynindx : 0; |
4513 | ||
4514 | if (GOT_TLS_GDESC_P (tls_type)) | |
4515 | { | |
59d6ffb2 | 4516 | bfd_byte *loc; |
67a4f2b7 AO |
4517 | outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC); |
4518 | BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8 | |
6de2ae4a L |
4519 | <= htab->elf.sgotplt->size); |
4520 | outrel.r_offset = (htab->elf.sgotplt->output_section->vma | |
4521 | + htab->elf.sgotplt->output_offset | |
67a4f2b7 AO |
4522 | + offplt |
4523 | + htab->sgotplt_jump_table_size); | |
6de2ae4a | 4524 | sreloc = htab->elf.srelplt; |
67a4f2b7 | 4525 | loc = sreloc->contents; |
0afcef53 | 4526 | loc += (elf_i386_next_tls_desc_index (htab)++ |
5ae0bfb6 | 4527 | * sizeof (Elf32_External_Rel)); |
67a4f2b7 | 4528 | BFD_ASSERT (loc + sizeof (Elf32_External_Rel) |
c3b02097 | 4529 | <= sreloc->contents + sreloc->size); |
67a4f2b7 AO |
4530 | bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc); |
4531 | if (indx == 0) | |
4532 | { | |
4533 | BFD_ASSERT (! unresolved_reloc); | |
4534 | bfd_put_32 (output_bfd, | |
0afcef53 | 4535 | relocation - _bfd_x86_elf_dtpoff_base (info), |
6de2ae4a | 4536 | htab->elf.sgotplt->contents + offplt |
67a4f2b7 AO |
4537 | + htab->sgotplt_jump_table_size + 4); |
4538 | } | |
4539 | else | |
4540 | { | |
4541 | bfd_put_32 (output_bfd, 0, | |
6de2ae4a | 4542 | htab->elf.sgotplt->contents + offplt |
67a4f2b7 AO |
4543 | + htab->sgotplt_jump_table_size + 4); |
4544 | } | |
4545 | } | |
4546 | ||
6de2ae4a | 4547 | sreloc = htab->elf.srelgot; |
67a4f2b7 | 4548 | |
6de2ae4a L |
4549 | outrel.r_offset = (htab->elf.sgot->output_section->vma |
4550 | + htab->elf.sgot->output_offset + off); | |
13ae64f3 | 4551 | |
67a4f2b7 | 4552 | if (GOT_TLS_GD_P (tls_type)) |
13ae64f3 | 4553 | dr_type = R_386_TLS_DTPMOD32; |
67a4f2b7 AO |
4554 | else if (GOT_TLS_GDESC_P (tls_type)) |
4555 | goto dr_done; | |
37e55690 JJ |
4556 | else if (tls_type == GOT_TLS_IE_POS) |
4557 | dr_type = R_386_TLS_TPOFF; | |
13ae64f3 JJ |
4558 | else |
4559 | dr_type = R_386_TLS_TPOFF32; | |
67a4f2b7 | 4560 | |
37e55690 | 4561 | if (dr_type == R_386_TLS_TPOFF && indx == 0) |
eb4ff4d6 | 4562 | bfd_put_32 (output_bfd, |
0afcef53 | 4563 | relocation - _bfd_x86_elf_dtpoff_base (info), |
6de2ae4a | 4564 | htab->elf.sgot->contents + off); |
37e55690 | 4565 | else if (dr_type == R_386_TLS_TPOFF32 && indx == 0) |
23209a78 | 4566 | bfd_put_32 (output_bfd, |
0afcef53 | 4567 | _bfd_x86_elf_dtpoff_base (info) - relocation, |
6de2ae4a | 4568 | htab->elf.sgot->contents + off); |
67a4f2b7 | 4569 | else if (dr_type != R_386_TLS_DESC) |
c366c25e | 4570 | bfd_put_32 (output_bfd, 0, |
6de2ae4a | 4571 | htab->elf.sgot->contents + off); |
13ae64f3 | 4572 | outrel.r_info = ELF32_R_INFO (indx, dr_type); |
67a4f2b7 | 4573 | |
59d6ffb2 | 4574 | elf_append_rel (output_bfd, sreloc, &outrel); |
13ae64f3 | 4575 | |
67a4f2b7 | 4576 | if (GOT_TLS_GD_P (tls_type)) |
13ae64f3 JJ |
4577 | { |
4578 | if (indx == 0) | |
4579 | { | |
82e51918 | 4580 | BFD_ASSERT (! unresolved_reloc); |
13ae64f3 | 4581 | bfd_put_32 (output_bfd, |
0afcef53 | 4582 | relocation - _bfd_x86_elf_dtpoff_base (info), |
6de2ae4a | 4583 | htab->elf.sgot->contents + off + 4); |
13ae64f3 JJ |
4584 | } |
4585 | else | |
4586 | { | |
4587 | bfd_put_32 (output_bfd, 0, | |
6de2ae4a | 4588 | htab->elf.sgot->contents + off + 4); |
13ae64f3 JJ |
4589 | outrel.r_info = ELF32_R_INFO (indx, |
4590 | R_386_TLS_DTPOFF32); | |
4591 | outrel.r_offset += 4; | |
59d6ffb2 | 4592 | elf_append_rel (output_bfd, sreloc, &outrel); |
13ae64f3 JJ |
4593 | } |
4594 | } | |
37e55690 JJ |
4595 | else if (tls_type == GOT_TLS_IE_BOTH) |
4596 | { | |
4597 | bfd_put_32 (output_bfd, | |
eb4ff4d6 | 4598 | (indx == 0 |
0afcef53 | 4599 | ? relocation - _bfd_x86_elf_dtpoff_base (info) |
eb4ff4d6 | 4600 | : 0), |
6de2ae4a | 4601 | htab->elf.sgot->contents + off + 4); |
37e55690 JJ |
4602 | outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF); |
4603 | outrel.r_offset += 4; | |
59d6ffb2 | 4604 | elf_append_rel (output_bfd, sreloc, &outrel); |
37e55690 | 4605 | } |
13ae64f3 | 4606 | |
67a4f2b7 | 4607 | dr_done: |
13ae64f3 JJ |
4608 | if (h != NULL) |
4609 | h->got.offset |= 1; | |
4610 | else | |
4611 | local_got_offsets[r_symndx] |= 1; | |
4612 | } | |
4613 | ||
67a4f2b7 AO |
4614 | if (off >= (bfd_vma) -2 |
4615 | && ! GOT_TLS_GDESC_P (tls_type)) | |
13ae64f3 | 4616 | abort (); |
67a4f2b7 AO |
4617 | if (r_type == R_386_TLS_GOTDESC |
4618 | || r_type == R_386_TLS_DESC_CALL) | |
4619 | { | |
4620 | relocation = htab->sgotplt_jump_table_size + offplt; | |
4621 | unresolved_reloc = FALSE; | |
4622 | } | |
4623 | else if (r_type == ELF32_R_TYPE (rel->r_info)) | |
13ae64f3 | 4624 | { |
6de2ae4a L |
4625 | bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma |
4626 | + htab->elf.sgotplt->output_offset; | |
4627 | relocation = htab->elf.sgot->output_section->vma | |
4628 | + htab->elf.sgot->output_offset + off - g_o_t; | |
37e55690 JJ |
4629 | if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE) |
4630 | && tls_type == GOT_TLS_IE_BOTH) | |
4631 | relocation += 4; | |
4632 | if (r_type == R_386_TLS_IE) | |
8c37241b | 4633 | relocation += g_o_t; |
b34976b6 | 4634 | unresolved_reloc = FALSE; |
13ae64f3 | 4635 | } |
67a4f2b7 | 4636 | else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD) |
13ae64f3 JJ |
4637 | { |
4638 | unsigned int val, type; | |
4639 | bfd_vma roff; | |
4640 | ||
4641 | /* GD->IE transition. */ | |
6eaa7fb5 L |
4642 | type = *(contents + rel->r_offset - 2); |
4643 | val = *(contents + rel->r_offset - 1); | |
13ae64f3 JJ |
4644 | if (type == 0x04) |
4645 | { | |
6eaa7fb5 L |
4646 | /* Change |
4647 | leal foo@tlsgd(,%ebx,1), %eax | |
4648 | call ___tls_get_addr@PLT | |
4649 | into: | |
4650 | movl %gs:0, %eax | |
4651 | subl $foo@gottpoff(%ebx), %eax. */ | |
13ae64f3 | 4652 | val >>= 3; |
142411ca | 4653 | roff = rel->r_offset - 3; |
13ae64f3 JJ |
4654 | } |
4655 | else | |
4656 | { | |
6eaa7fb5 L |
4657 | /* Change |
4658 | leal foo@tlsgd(%ebx), %eax | |
4659 | call ___tls_get_addr@PLT | |
4660 | nop | |
4661 | or | |
4662 | leal foo@tlsgd(%reg), %eax | |
4663 | call *___tls_get_addr@GOT(%reg) | |
4664 | which may be converted to | |
4665 | addr32 call ___tls_get_addr | |
4666 | into: | |
4667 | movl %gs:0, %eax; | |
4668 | subl $foo@gottpoff(%reg), %eax. */ | |
13ae64f3 JJ |
4669 | roff = rel->r_offset - 2; |
4670 | } | |
4671 | memcpy (contents + roff, | |
4672 | "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12); | |
4673 | contents[roff + 7] = 0x80 | (val & 7); | |
37e55690 JJ |
4674 | /* If foo is used only with foo@gotntpoff(%reg) and |
4675 | foo@indntpoff, but not with foo@gottpoff(%reg), change | |
4676 | subl $foo@gottpoff(%reg), %eax | |
4677 | into: | |
4678 | addl $foo@gotntpoff(%reg), %eax. */ | |
ebcfb3c0 JJ |
4679 | if (tls_type == GOT_TLS_IE_POS) |
4680 | contents[roff + 6] = 0x03; | |
8c37241b | 4681 | bfd_put_32 (output_bfd, |
6de2ae4a L |
4682 | htab->elf.sgot->output_section->vma |
4683 | + htab->elf.sgot->output_offset + off | |
4684 | - htab->elf.sgotplt->output_section->vma | |
4685 | - htab->elf.sgotplt->output_offset, | |
13ae64f3 | 4686 | contents + roff + 8); |
6eaa7fb5 | 4687 | /* Skip R_386_PLT32 and R_386_GOT32X. */ |
13ae64f3 | 4688 | rel++; |
60f2e42e | 4689 | wrel++; |
13ae64f3 JJ |
4690 | continue; |
4691 | } | |
67a4f2b7 AO |
4692 | else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC) |
4693 | { | |
4694 | /* GDesc -> IE transition. | |
4695 | It's originally something like: | |
4696 | leal x@tlsdesc(%ebx), %eax | |
4697 | ||
4698 | Change it to: | |
142411ca | 4699 | movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax |
67a4f2b7 AO |
4700 | or: |
4701 | movl x@gottpoff(%ebx), %eax # before negl %eax | |
4702 | ||
4703 | Registers other than %eax may be set up here. */ | |
4704 | ||
67a4f2b7 AO |
4705 | bfd_vma roff; |
4706 | ||
4707 | /* First, make sure it's a leal adding ebx to a 32-bit | |
4708 | offset into any register, although it's probably | |
4709 | almost always going to be eax. */ | |
4710 | roff = rel->r_offset; | |
67a4f2b7 AO |
4711 | |
4712 | /* Now modify the instruction as appropriate. */ | |
4713 | /* To turn a leal into a movl in the form we use it, it | |
4714 | suffices to change the first byte from 0x8d to 0x8b. | |
4715 | aoliva FIXME: should we decide to keep the leal, all | |
4716 | we have to do is remove the statement below, and | |
4717 | adjust the relaxation of R_386_TLS_DESC_CALL. */ | |
4718 | bfd_put_8 (output_bfd, 0x8b, contents + roff - 2); | |
4719 | ||
4720 | if (tls_type == GOT_TLS_IE_BOTH) | |
4721 | off += 4; | |
4722 | ||
4723 | bfd_put_32 (output_bfd, | |
6de2ae4a L |
4724 | htab->elf.sgot->output_section->vma |
4725 | + htab->elf.sgot->output_offset + off | |
4726 | - htab->elf.sgotplt->output_section->vma | |
4727 | - htab->elf.sgotplt->output_offset, | |
67a4f2b7 AO |
4728 | contents + roff); |
4729 | continue; | |
4730 | } | |
4731 | else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL) | |
4732 | { | |
4733 | /* GDesc -> IE transition. | |
4734 | It's originally: | |
4735 | call *(%eax) | |
4736 | ||
4737 | Change it to: | |
142411ca | 4738 | xchg %ax,%ax |
67a4f2b7 AO |
4739 | or |
4740 | negl %eax | |
4741 | depending on how we transformed the TLS_GOTDESC above. | |
4742 | */ | |
4743 | ||
67a4f2b7 AO |
4744 | bfd_vma roff; |
4745 | ||
67a4f2b7 | 4746 | roff = rel->r_offset; |
67a4f2b7 AO |
4747 | |
4748 | /* Now modify the instruction as appropriate. */ | |
4749 | if (tls_type != GOT_TLS_IE_NEG) | |
4750 | { | |
10efb593 L |
4751 | /* xchg %ax,%ax */ |
4752 | bfd_put_8 (output_bfd, 0x66, contents + roff); | |
67a4f2b7 AO |
4753 | bfd_put_8 (output_bfd, 0x90, contents + roff + 1); |
4754 | } | |
4755 | else | |
4756 | { | |
4757 | /* negl %eax */ | |
4758 | bfd_put_8 (output_bfd, 0xf7, contents + roff); | |
4759 | bfd_put_8 (output_bfd, 0xd8, contents + roff + 1); | |
4760 | } | |
4761 | ||
4762 | continue; | |
4763 | } | |
4764 | else | |
4765 | BFD_ASSERT (FALSE); | |
13ae64f3 JJ |
4766 | break; |
4767 | ||
4768 | case R_386_TLS_LDM: | |
142411ca L |
4769 | if (! elf_i386_tls_transition (info, input_bfd, |
4770 | input_section, contents, | |
4771 | symtab_hdr, sym_hashes, | |
4772 | &r_type, GOT_UNKNOWN, rel, | |
bedfd056 | 4773 | relend, h, r_symndx, TRUE)) |
142411ca | 4774 | return FALSE; |
13ae64f3 | 4775 | |
142411ca L |
4776 | if (r_type != R_386_TLS_LDM) |
4777 | { | |
6eaa7fb5 L |
4778 | /* LD->LE transition. Change |
4779 | leal foo@tlsldm(%ebx) %eax | |
4780 | call ___tls_get_addr@PLT | |
4781 | into: | |
4782 | movl %gs:0, %eax | |
4783 | nop | |
4784 | leal 0(%esi,1), %esi | |
4785 | or change | |
4786 | leal foo@tlsldm(%reg) %eax | |
4787 | call *___tls_get_addr@GOT(%reg) | |
4788 | which may be converted to | |
4789 | addr32 call ___tls_get_addr | |
4790 | into: | |
4791 | movl %gs:0, %eax | |
4792 | leal 0(%esi), %esi */ | |
142411ca | 4793 | BFD_ASSERT (r_type == R_386_TLS_LE_32); |
6eaa7fb5 L |
4794 | if (*(contents + rel->r_offset + 4) == 0xff |
4795 | || *(contents + rel->r_offset + 4) == 0x67) | |
4796 | memcpy (contents + rel->r_offset - 2, | |
4797 | "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12); | |
4798 | else | |
4799 | memcpy (contents + rel->r_offset - 2, | |
4800 | "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11); | |
a3fadc9a | 4801 | /* Skip R_386_PC32/R_386_PLT32. */ |
13ae64f3 | 4802 | rel++; |
60f2e42e | 4803 | wrel++; |
13ae64f3 JJ |
4804 | continue; |
4805 | } | |
4806 | ||
6de2ae4a | 4807 | if (htab->elf.sgot == NULL) |
13ae64f3 JJ |
4808 | abort (); |
4809 | ||
0afcef53 | 4810 | off = htab->tls_ld_or_ldm_got.offset; |
13ae64f3 JJ |
4811 | if (off & 1) |
4812 | off &= ~1; | |
4813 | else | |
4814 | { | |
947216bf | 4815 | Elf_Internal_Rela outrel; |
13ae64f3 | 4816 | |
6de2ae4a | 4817 | if (htab->elf.srelgot == NULL) |
13ae64f3 JJ |
4818 | abort (); |
4819 | ||
6de2ae4a L |
4820 | outrel.r_offset = (htab->elf.sgot->output_section->vma |
4821 | + htab->elf.sgot->output_offset + off); | |
13ae64f3 JJ |
4822 | |
4823 | bfd_put_32 (output_bfd, 0, | |
6de2ae4a | 4824 | htab->elf.sgot->contents + off); |
13ae64f3 | 4825 | bfd_put_32 (output_bfd, 0, |
6de2ae4a | 4826 | htab->elf.sgot->contents + off + 4); |
13ae64f3 | 4827 | outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32); |
59d6ffb2 | 4828 | elf_append_rel (output_bfd, htab->elf.srelgot, &outrel); |
0afcef53 | 4829 | htab->tls_ld_or_ldm_got.offset |= 1; |
13ae64f3 | 4830 | } |
6de2ae4a L |
4831 | relocation = htab->elf.sgot->output_section->vma |
4832 | + htab->elf.sgot->output_offset + off | |
4833 | - htab->elf.sgotplt->output_section->vma | |
4834 | - htab->elf.sgotplt->output_offset; | |
b34976b6 | 4835 | unresolved_reloc = FALSE; |
13ae64f3 JJ |
4836 | break; |
4837 | ||
4838 | case R_386_TLS_LDO_32: | |
0e1862bb L |
4839 | if (!bfd_link_executable (info) |
4840 | || (input_section->flags & SEC_CODE) == 0) | |
0afcef53 | 4841 | relocation -= _bfd_x86_elf_dtpoff_base (info); |
13ae64f3 JJ |
4842 | else |
4843 | /* When converting LDO to LE, we must negate. */ | |
eb4ff4d6 | 4844 | relocation = -elf_i386_tpoff (info, relocation); |
13ae64f3 JJ |
4845 | break; |
4846 | ||
4847 | case R_386_TLS_LE_32: | |
13ae64f3 | 4848 | case R_386_TLS_LE: |
0e1862bb | 4849 | if (!bfd_link_executable (info)) |
37e55690 | 4850 | { |
947216bf | 4851 | Elf_Internal_Rela outrel; |
37e55690 | 4852 | asection *sreloc; |
37e55690 JJ |
4853 | |
4854 | outrel.r_offset = rel->r_offset | |
4855 | + input_section->output_section->vma | |
4856 | + input_section->output_offset; | |
4857 | if (h != NULL && h->dynindx != -1) | |
4858 | indx = h->dynindx; | |
4859 | else | |
4860 | indx = 0; | |
4861 | if (r_type == R_386_TLS_LE_32) | |
4862 | outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32); | |
4863 | else | |
4864 | outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF); | |
4865 | sreloc = elf_section_data (input_section)->sreloc; | |
4866 | if (sreloc == NULL) | |
4867 | abort (); | |
59d6ffb2 | 4868 | elf_append_rel (output_bfd, sreloc, &outrel); |
37e55690 JJ |
4869 | if (indx) |
4870 | continue; | |
4871 | else if (r_type == R_386_TLS_LE_32) | |
0afcef53 | 4872 | relocation = _bfd_x86_elf_dtpoff_base (info) - relocation; |
37e55690 | 4873 | else |
0afcef53 | 4874 | relocation -= _bfd_x86_elf_dtpoff_base (info); |
37e55690 JJ |
4875 | } |
4876 | else if (r_type == R_386_TLS_LE_32) | |
eb4ff4d6 | 4877 | relocation = elf_i386_tpoff (info, relocation); |
37e55690 | 4878 | else |
eb4ff4d6 | 4879 | relocation = -elf_i386_tpoff (info, relocation); |
13ae64f3 JJ |
4880 | break; |
4881 | ||
252b5132 RH |
4882 | default: |
4883 | break; | |
4884 | } | |
4885 | ||
239e1f3a AM |
4886 | /* Dynamic relocs are not propagated for SEC_DEBUGGING sections |
4887 | because such sections are not SEC_ALLOC and thus ld.so will | |
4888 | not process them. */ | |
8c694914 | 4889 | if (unresolved_reloc |
239e1f3a | 4890 | && !((input_section->flags & SEC_DEBUGGING) != 0 |
1d5316ab AM |
4891 | && h->def_dynamic) |
4892 | && _bfd_elf_section_offset (output_bfd, info, input_section, | |
4893 | rel->r_offset) != (bfd_vma) -1) | |
6a30718d | 4894 | { |
4eca0228 | 4895 | _bfd_error_handler |
695344c0 | 4896 | /* xgettext:c-format */ |
d42c267e | 4897 | (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"), |
d003868e AM |
4898 | input_bfd, |
4899 | input_section, | |
d42c267e | 4900 | rel->r_offset, |
843fe662 | 4901 | howto->name, |
6a30718d | 4902 | h->root.root.string); |
b34976b6 | 4903 | return FALSE; |
6a30718d | 4904 | } |
83be169b | 4905 | |
cbe950e9 | 4906 | do_relocation: |
252b5132 RH |
4907 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
4908 | contents, rel->r_offset, | |
55fd94b0 | 4909 | relocation, 0); |
252b5132 | 4910 | |
62d78908 | 4911 | check_relocation_error: |
cf5c0c5b | 4912 | if (r != bfd_reloc_ok) |
252b5132 | 4913 | { |
cf5c0c5b | 4914 | const char *name; |
ffb2e45b | 4915 | |
cf5c0c5b AM |
4916 | if (h != NULL) |
4917 | name = h->root.root.string; | |
4918 | else | |
4919 | { | |
4920 | name = bfd_elf_string_from_elf_section (input_bfd, | |
4921 | symtab_hdr->sh_link, | |
4922 | sym->st_name); | |
4923 | if (name == NULL) | |
b34976b6 | 4924 | return FALSE; |
cf5c0c5b AM |
4925 | if (*name == '\0') |
4926 | name = bfd_section_name (input_bfd, sec); | |
4927 | } | |
ffb2e45b | 4928 | |
cf5c0c5b | 4929 | if (r == bfd_reloc_overflow) |
1a72702b AM |
4930 | (*info->callbacks->reloc_overflow) |
4931 | (info, (h ? &h->root : NULL), name, howto->name, | |
4932 | (bfd_vma) 0, input_bfd, input_section, rel->r_offset); | |
cf5c0c5b AM |
4933 | else |
4934 | { | |
4eca0228 | 4935 | _bfd_error_handler |
695344c0 | 4936 | /* xgettext:c-format */ |
d42c267e | 4937 | (_("%B(%A+%#Lx): reloc against `%s': error %d"), |
d003868e | 4938 | input_bfd, input_section, |
d42c267e | 4939 | rel->r_offset, name, (int) r); |
b34976b6 | 4940 | return FALSE; |
cf5c0c5b | 4941 | } |
252b5132 | 4942 | } |
60f2e42e L |
4943 | |
4944 | if (wrel != rel) | |
4945 | *wrel = *rel; | |
4946 | } | |
4947 | ||
4948 | if (wrel != rel) | |
4949 | { | |
4950 | Elf_Internal_Shdr *rel_hdr; | |
4951 | size_t deleted = rel - wrel; | |
4952 | ||
4953 | rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section); | |
4954 | rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted; | |
4955 | if (rel_hdr->sh_size == 0) | |
4956 | { | |
4957 | /* It is too late to remove an empty reloc section. Leave | |
4958 | one NONE reloc. | |
4959 | ??? What is wrong with an empty section??? */ | |
4960 | rel_hdr->sh_size = rel_hdr->sh_entsize; | |
4961 | deleted -= 1; | |
4962 | } | |
4963 | rel_hdr = _bfd_elf_single_rel_hdr (input_section); | |
4964 | rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted; | |
4965 | input_section->reloc_count -= deleted; | |
252b5132 RH |
4966 | } |
4967 | ||
b34976b6 | 4968 | return TRUE; |
252b5132 RH |
4969 | } |
4970 | ||
4971 | /* Finish up dynamic symbol handling. We set the contents of various | |
4972 | dynamic sections here. */ | |
4973 | ||
b34976b6 | 4974 | static bfd_boolean |
55fd94b0 AM |
4975 | elf_i386_finish_dynamic_symbol (bfd *output_bfd, |
4976 | struct bfd_link_info *info, | |
4977 | struct elf_link_hash_entry *h, | |
4978 | Elf_Internal_Sym *sym) | |
252b5132 | 4979 | { |
0afcef53 | 4980 | struct elf_x86_link_hash_table *htab; |
25e762b9 RM |
4981 | unsigned plt_entry_size; |
4982 | const struct elf_i386_backend_data *abed; | |
0afcef53 | 4983 | struct elf_x86_link_hash_entry *eh; |
aec6b87e | 4984 | bfd_boolean local_undefweak; |
ee2fdd6f | 4985 | bfd_boolean use_plt_second; |
0afcef53 L |
4986 | const struct elf_i386_plt_layout *plt_layout; |
4987 | const struct elf_i386_lazy_plt_layout *lazy_plt_layout; | |
4988 | const struct elf_i386_non_lazy_plt_layout *non_lazy_plt_layout; | |
252b5132 | 4989 | |
0afcef53 | 4990 | htab = elf_x86_hash_table (info, I386_ELF_DATA); |
4dfe6ac6 NC |
4991 | if (htab == NULL) |
4992 | return FALSE; | |
252b5132 | 4993 | |
25e762b9 | 4994 | abed = get_elf_i386_backend_data (output_bfd); |
0afcef53 L |
4995 | plt_layout = &elf_i386_plt (htab); |
4996 | lazy_plt_layout = elf_i386_lazy_plt (htab); | |
4997 | non_lazy_plt_layout = elf_i386_non_lazy_plt (htab); | |
4998 | plt_entry_size = plt_layout->plt_entry_size; | |
25e762b9 | 4999 | |
ee2fdd6f L |
5000 | /* Use the second PLT section only if there is .plt section. */ |
5001 | use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL; | |
5002 | ||
0afcef53 | 5003 | eh = (struct elf_x86_link_hash_entry *) h; |
e133d005 L |
5004 | if (eh->no_finish_dynamic_symbol) |
5005 | abort (); | |
dd7e64d4 | 5006 | |
aec6b87e L |
5007 | /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for |
5008 | resolved undefined weak symbols in executable so that their | |
5009 | references have value 0 at run-time. */ | |
0afcef53 | 5010 | local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, I386_ELF_DATA, |
e62b9723 L |
5011 | eh->has_got_reloc, |
5012 | eh); | |
aec6b87e | 5013 | |
252b5132 RH |
5014 | if (h->plt.offset != (bfd_vma) -1) |
5015 | { | |
ee2fdd6f | 5016 | bfd_vma plt_index, plt_offset; |
252b5132 | 5017 | bfd_vma got_offset; |
947216bf AM |
5018 | Elf_Internal_Rela rel; |
5019 | bfd_byte *loc; | |
ee2fdd6f | 5020 | asection *plt, *resolved_plt, *gotplt, *relplt; |
cbe950e9 L |
5021 | |
5022 | /* When building a static executable, use .iplt, .igot.plt and | |
5023 | .rel.iplt sections for STT_GNU_IFUNC symbols. */ | |
6de2ae4a | 5024 | if (htab->elf.splt != NULL) |
cbe950e9 | 5025 | { |
6de2ae4a L |
5026 | plt = htab->elf.splt; |
5027 | gotplt = htab->elf.sgotplt; | |
5028 | relplt = htab->elf.srelplt; | |
cbe950e9 L |
5029 | } |
5030 | else | |
5031 | { | |
6de2ae4a L |
5032 | plt = htab->elf.iplt; |
5033 | gotplt = htab->elf.igotplt; | |
5034 | relplt = htab->elf.irelplt; | |
cbe950e9 | 5035 | } |
252b5132 RH |
5036 | |
5037 | /* This symbol has an entry in the procedure linkage table. Set | |
5038 | it up. */ | |
5039 | ||
cbe950e9 | 5040 | if ((h->dynindx == -1 |
aec6b87e | 5041 | && !local_undefweak |
0e1862bb | 5042 | && !((h->forced_local || bfd_link_executable (info)) |
cbe950e9 L |
5043 | && h->def_regular |
5044 | && h->type == STT_GNU_IFUNC)) | |
5045 | || plt == NULL | |
5046 | || gotplt == NULL | |
5047 | || relplt == NULL) | |
cec7f46a | 5048 | abort (); |
252b5132 RH |
5049 | |
5050 | /* Get the index in the procedure linkage table which | |
5051 | corresponds to this symbol. This is the index of this symbol | |
5052 | in all the symbols for which we are making plt entries. The | |
cbe950e9 | 5053 | first entry in the procedure linkage table is reserved. |
252b5132 | 5054 | |
cbe950e9 | 5055 | Get the offset into the .got table of the entry that |
252b5132 | 5056 | corresponds to this function. Each .got entry is 4 bytes. |
cbe950e9 | 5057 | The first three are reserved. |
6bbec505 | 5058 | |
cbe950e9 L |
5059 | For static executables, we don't reserve anything. */ |
5060 | ||
6de2ae4a | 5061 | if (plt == htab->elf.splt) |
cbe950e9 | 5062 | { |
f604c2a2 | 5063 | got_offset = (h->plt.offset / plt_entry_size |
0afcef53 | 5064 | - plt_layout->has_plt0); |
e1f98742 | 5065 | got_offset = (got_offset + 3) * 4; |
cbe950e9 L |
5066 | } |
5067 | else | |
5068 | { | |
e1f98742 L |
5069 | got_offset = h->plt.offset / plt_entry_size; |
5070 | got_offset = got_offset * 4; | |
cbe950e9 | 5071 | } |
252b5132 | 5072 | |
f604c2a2 L |
5073 | /* Fill in the entry in the procedure linkage table and update |
5074 | the first slot. */ | |
0afcef53 | 5075 | memcpy (plt->contents + h->plt.offset, plt_layout->plt_entry, |
f604c2a2 | 5076 | plt_entry_size); |
ee2fdd6f L |
5077 | |
5078 | if (use_plt_second) | |
5079 | { | |
5080 | const bfd_byte *plt_entry; | |
5081 | if (bfd_link_pic (info)) | |
0afcef53 | 5082 | plt_entry = non_lazy_plt_layout->pic_plt_entry; |
ee2fdd6f | 5083 | else |
0afcef53 | 5084 | plt_entry = non_lazy_plt_layout->plt_entry; |
ee2fdd6f | 5085 | memcpy (htab->plt_second->contents + eh->plt_second.offset, |
0afcef53 | 5086 | plt_entry, non_lazy_plt_layout->plt_entry_size); |
ee2fdd6f L |
5087 | |
5088 | resolved_plt = htab->plt_second; | |
5089 | plt_offset = eh->plt_second.offset; | |
5090 | } | |
5091 | else | |
5092 | { | |
5093 | resolved_plt = plt; | |
5094 | plt_offset = h->plt.offset; | |
5095 | } | |
5096 | ||
0e1862bb | 5097 | if (! bfd_link_pic (info)) |
252b5132 | 5098 | { |
252b5132 | 5099 | bfd_put_32 (output_bfd, |
cbe950e9 L |
5100 | (gotplt->output_section->vma |
5101 | + gotplt->output_offset | |
252b5132 | 5102 | + got_offset), |
ee2fdd6f | 5103 | resolved_plt->contents + plt_offset |
0afcef53 | 5104 | + plt_layout->plt_got_offset); |
eac338cf | 5105 | |
f604c2a2 | 5106 | if (abed->os == is_vxworks) |
eac338cf PB |
5107 | { |
5108 | int s, k, reloc_index; | |
5109 | ||
5110 | /* Create the R_386_32 relocation referencing the GOT | |
5111 | for this PLT entry. */ | |
5112 | ||
5113 | /* S: Current slot number (zero-based). */ | |
0afcef53 L |
5114 | s = ((h->plt.offset - plt_layout->plt_entry_size) |
5115 | / plt_layout->plt_entry_size); | |
eac338cf | 5116 | /* K: Number of relocations for PLTResolve. */ |
0e1862bb | 5117 | if (bfd_link_pic (info)) |
eac338cf PB |
5118 | k = PLTRESOLVE_RELOCS_SHLIB; |
5119 | else | |
5120 | k = PLTRESOLVE_RELOCS; | |
5121 | /* Skip the PLTresolve relocations, and the relocations for | |
5122 | the other PLT slots. */ | |
5123 | reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS; | |
0afcef53 | 5124 | loc = (elf_i386_srelplt2 (htab)->contents + reloc_index |
eac338cf PB |
5125 | * sizeof (Elf32_External_Rel)); |
5126 | ||
ee2fdd6f L |
5127 | rel.r_offset = (plt->output_section->vma |
5128 | + plt->output_offset | |
eac338cf | 5129 | + h->plt.offset + 2), |
7325306f | 5130 | rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32); |
eac338cf PB |
5131 | bfd_elf32_swap_reloc_out (output_bfd, &rel, loc); |
5132 | ||
5133 | /* Create the R_386_32 relocation referencing the beginning of | |
5134 | the PLT for this GOT entry. */ | |
6de2ae4a L |
5135 | rel.r_offset = (htab->elf.sgotplt->output_section->vma |
5136 | + htab->elf.sgotplt->output_offset | |
eac338cf | 5137 | + got_offset); |
7325306f | 5138 | rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32); |
eac338cf | 5139 | bfd_elf32_swap_reloc_out (output_bfd, &rel, |
13ca3149 | 5140 | loc + sizeof (Elf32_External_Rel)); |
eac338cf | 5141 | } |
252b5132 RH |
5142 | } |
5143 | else | |
5144 | { | |
252b5132 | 5145 | bfd_put_32 (output_bfd, got_offset, |
ee2fdd6f | 5146 | resolved_plt->contents + plt_offset |
0afcef53 | 5147 | + plt_layout->plt_got_offset); |
252b5132 RH |
5148 | } |
5149 | ||
aec6b87e L |
5150 | /* Fill in the entry in the global offset table. Leave the entry |
5151 | as zero for undefined weak symbol in PIE. No PLT relocation | |
5152 | against undefined weak symbol in PIE. */ | |
5153 | if (!local_undefweak) | |
cbe950e9 | 5154 | { |
0afcef53 | 5155 | if (plt_layout->has_plt0) |
f604c2a2 L |
5156 | bfd_put_32 (output_bfd, |
5157 | (plt->output_section->vma | |
5158 | + plt->output_offset | |
5159 | + h->plt.offset | |
0afcef53 | 5160 | + lazy_plt_layout->plt_lazy_offset), |
f604c2a2 | 5161 | gotplt->contents + got_offset); |
252b5132 | 5162 | |
aec6b87e L |
5163 | /* Fill in the entry in the .rel.plt section. */ |
5164 | rel.r_offset = (gotplt->output_section->vma | |
5165 | + gotplt->output_offset | |
5166 | + got_offset); | |
5167 | if (h->dynindx == -1 | |
5168 | || ((bfd_link_executable (info) | |
5169 | || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) | |
5170 | && h->def_regular | |
5171 | && h->type == STT_GNU_IFUNC)) | |
5172 | { | |
6322e5c5 L |
5173 | info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"), |
5174 | h->root.root.string, | |
5175 | h->root.u.def.section->owner); | |
5176 | ||
aec6b87e L |
5177 | /* If an STT_GNU_IFUNC symbol is locally defined, generate |
5178 | R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend | |
5179 | in the .got.plt section. */ | |
5180 | bfd_put_32 (output_bfd, | |
5181 | (h->root.u.def.value | |
5182 | + h->root.u.def.section->output_section->vma | |
5183 | + h->root.u.def.section->output_offset), | |
5184 | gotplt->contents + got_offset); | |
5185 | rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE); | |
5186 | /* R_386_IRELATIVE comes last. */ | |
5187 | plt_index = htab->next_irelative_index--; | |
5188 | } | |
5189 | else | |
5190 | { | |
5191 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT); | |
5192 | plt_index = htab->next_jump_slot_index++; | |
5193 | } | |
5194 | ||
5195 | loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel); | |
5196 | bfd_elf32_swap_reloc_out (output_bfd, &rel, loc); | |
5197 | ||
f604c2a2 L |
5198 | /* Don't fill the second and third slots in PLT entry for |
5199 | static executables nor without PLT0. */ | |
0afcef53 | 5200 | if (plt == htab->elf.splt && plt_layout->has_plt0) |
aec6b87e L |
5201 | { |
5202 | bfd_put_32 (output_bfd, | |
5203 | plt_index * sizeof (Elf32_External_Rel), | |
5204 | plt->contents + h->plt.offset | |
0afcef53 | 5205 | + lazy_plt_layout->plt_reloc_offset); |
f604c2a2 L |
5206 | bfd_put_32 (output_bfd, |
5207 | - (h->plt.offset | |
0afcef53 | 5208 | + lazy_plt_layout->plt_plt_offset + 4), |
f604c2a2 | 5209 | (plt->contents + h->plt.offset |
0afcef53 | 5210 | + lazy_plt_layout->plt_plt_offset)); |
aec6b87e | 5211 | } |
e1f98742 | 5212 | } |
dd7e64d4 L |
5213 | } |
5214 | else if (eh->plt_got.offset != (bfd_vma) -1) | |
5215 | { | |
5216 | bfd_vma got_offset, plt_offset; | |
5217 | asection *plt, *got, *gotplt; | |
5218 | const bfd_byte *got_plt_entry; | |
5219 | ||
dd7e64d4 L |
5220 | /* Set the entry in the GOT procedure linkage table. */ |
5221 | plt = htab->plt_got; | |
5222 | got = htab->elf.sgot; | |
5223 | gotplt = htab->elf.sgotplt; | |
5224 | got_offset = h->got.offset; | |
5225 | ||
5226 | if (got_offset == (bfd_vma) -1 | |
5227 | || plt == NULL | |
5228 | || got == NULL | |
5229 | || gotplt == NULL) | |
5230 | abort (); | |
5231 | ||
5232 | /* Fill in the entry in the GOT procedure linkage table. */ | |
0e1862bb | 5233 | if (! bfd_link_pic (info)) |
252b5132 | 5234 | { |
0afcef53 | 5235 | got_plt_entry = non_lazy_plt_layout->plt_entry; |
dd7e64d4 | 5236 | got_offset += got->output_section->vma + got->output_offset; |
252b5132 | 5237 | } |
dd7e64d4 L |
5238 | else |
5239 | { | |
0afcef53 | 5240 | got_plt_entry = non_lazy_plt_layout->pic_plt_entry; |
dd7e64d4 L |
5241 | got_offset += (got->output_section->vma |
5242 | + got->output_offset | |
5243 | - gotplt->output_section->vma | |
5244 | - gotplt->output_offset); | |
5245 | } | |
5246 | ||
5247 | plt_offset = eh->plt_got.offset; | |
5248 | memcpy (plt->contents + plt_offset, got_plt_entry, | |
0afcef53 | 5249 | non_lazy_plt_layout->plt_entry_size); |
dd7e64d4 | 5250 | bfd_put_32 (output_bfd, got_offset, |
f604c2a2 | 5251 | (plt->contents + plt_offset |
0afcef53 | 5252 | + non_lazy_plt_layout->plt_got_offset)); |
dd7e64d4 L |
5253 | } |
5254 | ||
aec6b87e L |
5255 | if (!local_undefweak |
5256 | && !h->def_regular | |
dd7e64d4 L |
5257 | && (h->plt.offset != (bfd_vma) -1 |
5258 | || eh->plt_got.offset != (bfd_vma) -1)) | |
5259 | { | |
5260 | /* Mark the symbol as undefined, rather than as defined in | |
5261 | the .plt section. Leave the value if there were any | |
5262 | relocations where pointer equality matters (this is a clue | |
5263 | for the dynamic linker, to make function pointer | |
5264 | comparisons work between an application and shared | |
5265 | library), otherwise set it to zero. If a function is only | |
5266 | called from a binary, there is no need to slow down | |
5267 | shared libraries because of that. */ | |
5268 | sym->st_shndx = SHN_UNDEF; | |
5269 | if (!h->pointer_equality_needed) | |
5270 | sym->st_value = 0; | |
252b5132 RH |
5271 | } |
5272 | ||
aec6b87e L |
5273 | /* Don't generate dynamic GOT relocation against undefined weak |
5274 | symbol in executable. */ | |
13ae64f3 | 5275 | if (h->got.offset != (bfd_vma) -1 |
0afcef53 L |
5276 | && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type) |
5277 | && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0 | |
aec6b87e | 5278 | && !local_undefweak) |
252b5132 | 5279 | { |
947216bf | 5280 | Elf_Internal_Rela rel; |
233cc9c1 | 5281 | asection *relgot = htab->elf.srelgot; |
252b5132 RH |
5282 | |
5283 | /* This symbol has an entry in the global offset table. Set it | |
5284 | up. */ | |
5285 | ||
6de2ae4a | 5286 | if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL) |
ffb2e45b | 5287 | abort (); |
252b5132 | 5288 | |
6de2ae4a L |
5289 | rel.r_offset = (htab->elf.sgot->output_section->vma |
5290 | + htab->elf.sgot->output_offset | |
dc810e39 | 5291 | + (h->got.offset & ~(bfd_vma) 1)); |
252b5132 | 5292 | |
dd5724d5 AM |
5293 | /* If this is a static link, or it is a -Bsymbolic link and the |
5294 | symbol is defined locally or was forced to be local because | |
5295 | of a version file, we just want to emit a RELATIVE reloc. | |
252b5132 RH |
5296 | The entry in the global offset table will already have been |
5297 | initialized in the relocate_section function. */ | |
710ab287 | 5298 | if (h->def_regular |
0018b0a3 L |
5299 | && h->type == STT_GNU_IFUNC) |
5300 | { | |
233cc9c1 L |
5301 | if (h->plt.offset == (bfd_vma) -1) |
5302 | { | |
5303 | /* STT_GNU_IFUNC is referenced without PLT. */ | |
5304 | if (htab->elf.splt == NULL) | |
5305 | { | |
5306 | /* use .rel[a].iplt section to store .got relocations | |
5307 | in static executable. */ | |
5308 | relgot = htab->elf.irelplt; | |
5309 | } | |
5310 | if (SYMBOL_REFERENCES_LOCAL (info, h)) | |
5311 | { | |
6322e5c5 L |
5312 | info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"), |
5313 | h->root.root.string, | |
5314 | h->root.u.def.section->owner); | |
5315 | ||
233cc9c1 L |
5316 | bfd_put_32 (output_bfd, |
5317 | (h->root.u.def.value | |
5318 | + h->root.u.def.section->output_section->vma | |
5319 | + h->root.u.def.section->output_offset), | |
5320 | htab->elf.sgot->contents + h->got.offset); | |
5321 | rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE); | |
5322 | } | |
5323 | else | |
5324 | goto do_glob_dat; | |
5325 | } | |
5326 | else if (bfd_link_pic (info)) | |
710ab287 L |
5327 | { |
5328 | /* Generate R_386_GLOB_DAT. */ | |
5329 | goto do_glob_dat; | |
5330 | } | |
5331 | else | |
5332 | { | |
cd2b2c10 | 5333 | asection *plt; |
ee2fdd6f | 5334 | bfd_vma plt_offset; |
cd2b2c10 | 5335 | |
710ab287 L |
5336 | if (!h->pointer_equality_needed) |
5337 | abort (); | |
5338 | ||
5339 | /* For non-shared object, we can't use .got.plt, which | |
5340 | contains the real function addres if we need pointer | |
5341 | equality. We load the GOT entry with the PLT entry. */ | |
ee2fdd6f L |
5342 | if (htab->plt_second != NULL) |
5343 | { | |
5344 | plt = htab->plt_second; | |
5345 | plt_offset = eh->plt_second.offset; | |
5346 | } | |
5347 | else | |
5348 | { | |
5349 | plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt; | |
5350 | plt_offset = h->plt.offset; | |
5351 | } | |
710ab287 L |
5352 | bfd_put_32 (output_bfd, |
5353 | (plt->output_section->vma | |
ee2fdd6f | 5354 | + plt->output_offset + plt_offset), |
6de2ae4a | 5355 | htab->elf.sgot->contents + h->got.offset); |
710ab287 L |
5356 | return TRUE; |
5357 | } | |
0018b0a3 | 5358 | } |
0e1862bb | 5359 | else if (bfd_link_pic (info) |
0018b0a3 | 5360 | && SYMBOL_REFERENCES_LOCAL (info, h)) |
dd5724d5 | 5361 | { |
6725bdbf | 5362 | BFD_ASSERT((h->got.offset & 1) != 0); |
dd5724d5 AM |
5363 | rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); |
5364 | } | |
252b5132 RH |
5365 | else |
5366 | { | |
dd5724d5 | 5367 | BFD_ASSERT((h->got.offset & 1) == 0); |
710ab287 | 5368 | do_glob_dat: |
6725bdbf | 5369 | bfd_put_32 (output_bfd, (bfd_vma) 0, |
6de2ae4a | 5370 | htab->elf.sgot->contents + h->got.offset); |
252b5132 RH |
5371 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT); |
5372 | } | |
5373 | ||
233cc9c1 | 5374 | elf_append_rel (output_bfd, relgot, &rel); |
252b5132 RH |
5375 | } |
5376 | ||
f5385ebf | 5377 | if (h->needs_copy) |
252b5132 | 5378 | { |
947216bf | 5379 | Elf_Internal_Rela rel; |
5474d94f | 5380 | asection *s; |
252b5132 RH |
5381 | |
5382 | /* This symbol needs a copy reloc. Set it up. */ | |
5383 | ||
ffb2e45b AM |
5384 | if (h->dynindx == -1 |
5385 | || (h->root.type != bfd_link_hash_defined | |
5386 | && h->root.type != bfd_link_hash_defweak) | |
5474d94f AM |
5387 | || htab->elf.srelbss == NULL |
5388 | || htab->elf.sreldynrelro == NULL) | |
ffb2e45b | 5389 | abort (); |
252b5132 RH |
5390 | |
5391 | rel.r_offset = (h->root.u.def.value | |
5392 | + h->root.u.def.section->output_section->vma | |
5393 | + h->root.u.def.section->output_offset); | |
5394 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY); | |
afbf7e8e | 5395 | if (h->root.u.def.section == htab->elf.sdynrelro) |
5474d94f AM |
5396 | s = htab->elf.sreldynrelro; |
5397 | else | |
5398 | s = htab->elf.srelbss; | |
5399 | elf_append_rel (output_bfd, s, &rel); | |
252b5132 RH |
5400 | } |
5401 | ||
b34976b6 | 5402 | return TRUE; |
252b5132 RH |
5403 | } |
5404 | ||
c25bc9fc L |
5405 | /* Finish up local dynamic symbol handling. We set the contents of |
5406 | various dynamic sections here. */ | |
5407 | ||
5408 | static bfd_boolean | |
5409 | elf_i386_finish_local_dynamic_symbol (void **slot, void *inf) | |
5410 | { | |
5411 | struct elf_link_hash_entry *h | |
5412 | = (struct elf_link_hash_entry *) *slot; | |
5413 | struct bfd_link_info *info | |
23209a78 | 5414 | = (struct bfd_link_info *) inf; |
c25bc9fc L |
5415 | |
5416 | return elf_i386_finish_dynamic_symbol (info->output_bfd, info, | |
5417 | h, NULL); | |
5418 | } | |
5419 | ||
aec6b87e L |
5420 | /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry |
5421 | here since undefined weak symbol may not be dynamic and may not be | |
5422 | called for elf_i386_finish_dynamic_symbol. */ | |
5423 | ||
5424 | static bfd_boolean | |
5425 | elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh, | |
5426 | void *inf) | |
5427 | { | |
5428 | struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; | |
5429 | struct bfd_link_info *info = (struct bfd_link_info *) inf; | |
5430 | ||
5431 | if (h->root.type != bfd_link_hash_undefweak | |
5432 | || h->dynindx != -1) | |
5433 | return TRUE; | |
5434 | ||
5435 | return elf_i386_finish_dynamic_symbol (info->output_bfd, | |
5436 | info, h, NULL); | |
5437 | } | |
5438 | ||
38701953 AM |
5439 | /* Used to decide how to sort relocs in an optimal manner for the |
5440 | dynamic linker, before writing them out. */ | |
5441 | ||
5442 | static enum elf_reloc_type_class | |
cae1fbbb | 5443 | elf_i386_reloc_type_class (const struct bfd_link_info *info, |
7e612e98 AM |
5444 | const asection *rel_sec ATTRIBUTE_UNUSED, |
5445 | const Elf_Internal_Rela *rela) | |
38701953 | 5446 | { |
cae1fbbb L |
5447 | bfd *abfd = info->output_bfd; |
5448 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5449 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
cae1fbbb | 5450 | |
d9e3b590 L |
5451 | if (htab->dynsym != NULL |
5452 | && htab->dynsym->contents != NULL) | |
5453 | { | |
5454 | /* Check relocation against STT_GNU_IFUNC symbol if there are | |
5455 | dynamic symbols. */ | |
5456 | unsigned long r_symndx = ELF32_R_SYM (rela->r_info); | |
c428ce9d L |
5457 | if (r_symndx != STN_UNDEF) |
5458 | { | |
5459 | Elf_Internal_Sym sym; | |
5460 | if (!bed->s->swap_symbol_in (abfd, | |
5461 | (htab->dynsym->contents | |
5462 | + r_symndx * sizeof (Elf32_External_Sym)), | |
5463 | 0, &sym)) | |
5464 | abort (); | |
d9e3b590 | 5465 | |
c428ce9d L |
5466 | if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC) |
5467 | return reloc_class_ifunc; | |
5468 | } | |
d9e3b590 | 5469 | } |
cae1fbbb | 5470 | |
55fd94b0 | 5471 | switch (ELF32_R_TYPE (rela->r_info)) |
38701953 | 5472 | { |
c428ce9d L |
5473 | case R_386_IRELATIVE: |
5474 | return reloc_class_ifunc; | |
38701953 AM |
5475 | case R_386_RELATIVE: |
5476 | return reloc_class_relative; | |
5477 | case R_386_JUMP_SLOT: | |
5478 | return reloc_class_plt; | |
5479 | case R_386_COPY: | |
5480 | return reloc_class_copy; | |
5481 | default: | |
5482 | return reloc_class_normal; | |
5483 | } | |
5484 | } | |
5485 | ||
252b5132 RH |
5486 | /* Finish up the dynamic sections. */ |
5487 | ||
b34976b6 | 5488 | static bfd_boolean |
55fd94b0 AM |
5489 | elf_i386_finish_dynamic_sections (bfd *output_bfd, |
5490 | struct bfd_link_info *info) | |
252b5132 | 5491 | { |
0afcef53 | 5492 | struct elf_x86_link_hash_table *htab; |
252b5132 | 5493 | bfd *dynobj; |
252b5132 | 5494 | asection *sdyn; |
25e762b9 | 5495 | const struct elf_i386_backend_data *abed; |
252b5132 | 5496 | |
0afcef53 | 5497 | htab = elf_x86_hash_table (info, I386_ELF_DATA); |
4dfe6ac6 NC |
5498 | if (htab == NULL) |
5499 | return FALSE; | |
5500 | ||
ebe50bae | 5501 | dynobj = htab->elf.dynobj; |
3d4d4302 | 5502 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); |
25e762b9 | 5503 | abed = get_elf_i386_backend_data (output_bfd); |
252b5132 | 5504 | |
ebe50bae | 5505 | if (htab->elf.dynamic_sections_created) |
252b5132 | 5506 | { |
252b5132 | 5507 | Elf32_External_Dyn *dyncon, *dynconend; |
0afcef53 L |
5508 | const struct elf_i386_plt_layout *plt_layout; |
5509 | const struct elf_i386_lazy_plt_layout *lazy_plt_layout; | |
5510 | const struct elf_i386_non_lazy_plt_layout *non_lazy_plt_layout; | |
252b5132 | 5511 | |
6de2ae4a | 5512 | if (sdyn == NULL || htab->elf.sgot == NULL) |
ffb2e45b | 5513 | abort (); |
252b5132 RH |
5514 | |
5515 | dyncon = (Elf32_External_Dyn *) sdyn->contents; | |
eea6121a | 5516 | dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size); |
252b5132 RH |
5517 | for (; dyncon < dynconend; dyncon++) |
5518 | { | |
5519 | Elf_Internal_Dyn dyn; | |
51b64d56 | 5520 | asection *s; |
252b5132 RH |
5521 | |
5522 | bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); | |
5523 | ||
5524 | switch (dyn.d_tag) | |
5525 | { | |
5526 | default: | |
f604c2a2 | 5527 | if (abed->os == is_vxworks |
25e762b9 | 5528 | && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn)) |
7a2b07ff | 5529 | break; |
0ac8d2ca | 5530 | continue; |
252b5132 RH |
5531 | |
5532 | case DT_PLTGOT: | |
6de2ae4a | 5533 | s = htab->elf.sgotplt; |
8c37241b | 5534 | dyn.d_un.d_ptr = s->output_section->vma + s->output_offset; |
6725bdbf AM |
5535 | break; |
5536 | ||
252b5132 | 5537 | case DT_JMPREL: |
6de2ae4a | 5538 | s = htab->elf.srelplt; |
6348e046 | 5539 | dyn.d_un.d_ptr = s->output_section->vma + s->output_offset; |
252b5132 RH |
5540 | break; |
5541 | ||
5542 | case DT_PLTRELSZ: | |
6de2ae4a | 5543 | s = htab->elf.srelplt; |
eea6121a | 5544 | dyn.d_un.d_val = s->size; |
252b5132 | 5545 | break; |
252b5132 | 5546 | } |
0ac8d2ca AM |
5547 | |
5548 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
252b5132 RH |
5549 | } |
5550 | ||
0afcef53 L |
5551 | plt_layout = &elf_i386_plt (htab); |
5552 | lazy_plt_layout = elf_i386_lazy_plt (htab); | |
5553 | non_lazy_plt_layout = elf_i386_non_lazy_plt (htab); | |
5554 | ||
6de2ae4a | 5555 | if (htab->elf.splt && htab->elf.splt->size > 0) |
252b5132 | 5556 | { |
1f78f649 L |
5557 | /* UnixWare sets the entsize of .plt to 4, although that doesn't |
5558 | really seem like the right value. */ | |
5559 | elf_section_data (htab->elf.splt->output_section) | |
5560 | ->this_hdr.sh_entsize = 4; | |
5561 | ||
0afcef53 | 5562 | if (plt_layout->has_plt0) |
252b5132 | 5563 | { |
f604c2a2 L |
5564 | /* Fill in the special first entry in the procedure linkage |
5565 | table. */ | |
0afcef53 L |
5566 | memcpy (htab->elf.splt->contents, plt_layout->plt0_entry, |
5567 | lazy_plt_layout->plt0_entry_size); | |
5568 | memset (htab->elf.splt->contents + lazy_plt_layout->plt0_entry_size, | |
25e762b9 | 5569 | abed->plt0_pad_byte, |
0afcef53 | 5570 | plt_layout->plt_entry_size - lazy_plt_layout->plt0_entry_size); |
f604c2a2 | 5571 | if (!bfd_link_pic (info)) |
eac338cf | 5572 | { |
f604c2a2 L |
5573 | bfd_put_32 (output_bfd, |
5574 | (htab->elf.sgotplt->output_section->vma | |
5575 | + htab->elf.sgotplt->output_offset | |
5576 | + 4), | |
5577 | htab->elf.splt->contents | |
0afcef53 | 5578 | + lazy_plt_layout->plt0_got1_offset); |
f604c2a2 L |
5579 | bfd_put_32 (output_bfd, |
5580 | (htab->elf.sgotplt->output_section->vma | |
5581 | + htab->elf.sgotplt->output_offset | |
5582 | + 8), | |
5583 | htab->elf.splt->contents | |
0afcef53 | 5584 | + lazy_plt_layout->plt0_got2_offset); |
eac338cf | 5585 | |
f604c2a2 | 5586 | if (abed->os == is_vxworks) |
1f78f649 | 5587 | { |
f604c2a2 L |
5588 | Elf_Internal_Rela rel; |
5589 | int num_plts = (htab->elf.splt->size | |
0afcef53 | 5590 | / plt_layout->plt_entry_size) - 1; |
f604c2a2 | 5591 | unsigned char *p; |
0afcef53 | 5592 | asection *srelplt2 = elf_i386_srelplt2 (htab); |
f604c2a2 L |
5593 | |
5594 | /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ | |
5595 | + 4. On IA32 we use REL relocations so the | |
5596 | addend goes in the PLT directly. */ | |
5597 | rel.r_offset = (htab->elf.splt->output_section->vma | |
5598 | + htab->elf.splt->output_offset | |
0afcef53 | 5599 | + lazy_plt_layout->plt0_got1_offset); |
1f78f649 L |
5600 | rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, |
5601 | R_386_32); | |
f604c2a2 | 5602 | bfd_elf32_swap_reloc_out (output_bfd, &rel, |
0afcef53 | 5603 | srelplt2->contents); |
f604c2a2 L |
5604 | /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ |
5605 | + 8. */ | |
5606 | rel.r_offset = (htab->elf.splt->output_section->vma | |
5607 | + htab->elf.splt->output_offset | |
0afcef53 | 5608 | + lazy_plt_layout->plt0_got2_offset); |
f604c2a2 | 5609 | rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, |
1f78f649 | 5610 | R_386_32); |
f604c2a2 | 5611 | bfd_elf32_swap_reloc_out (output_bfd, &rel, |
0afcef53 | 5612 | srelplt2->contents + |
f604c2a2 L |
5613 | sizeof (Elf32_External_Rel)); |
5614 | /* Correct the .rel.plt.unloaded relocations. */ | |
0afcef53 | 5615 | p = srelplt2->contents; |
f604c2a2 L |
5616 | if (bfd_link_pic (info)) |
5617 | p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel); | |
5618 | else | |
5619 | p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel); | |
5620 | ||
5621 | for (; num_plts; num_plts--) | |
5622 | { | |
5623 | bfd_elf32_swap_reloc_in (output_bfd, p, &rel); | |
5624 | rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, | |
5625 | R_386_32); | |
5626 | bfd_elf32_swap_reloc_out (output_bfd, &rel, p); | |
5627 | p += sizeof (Elf32_External_Rel); | |
5628 | ||
5629 | bfd_elf32_swap_reloc_in (output_bfd, p, &rel); | |
5630 | rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, | |
5631 | R_386_32); | |
5632 | bfd_elf32_swap_reloc_out (output_bfd, &rel, p); | |
5633 | p += sizeof (Elf32_External_Rel); | |
5634 | } | |
1f78f649 | 5635 | } |
eac338cf PB |
5636 | } |
5637 | } | |
252b5132 | 5638 | } |
f604c2a2 L |
5639 | |
5640 | if (htab->plt_got != NULL && htab->plt_got->size > 0) | |
5641 | elf_section_data (htab->plt_got->output_section) | |
0afcef53 | 5642 | ->this_hdr.sh_entsize = non_lazy_plt_layout->plt_entry_size; |
ee2fdd6f L |
5643 | |
5644 | if (htab->plt_second != NULL && htab->plt_second->size > 0) | |
5645 | elf_section_data (htab->plt_second->output_section) | |
0afcef53 | 5646 | ->this_hdr.sh_entsize = non_lazy_plt_layout->plt_entry_size; |
252b5132 RH |
5647 | } |
5648 | ||
f604c2a2 L |
5649 | /* Fill in the first three entries in the global offset table. */ |
5650 | if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0) | |
252b5132 | 5651 | { |
56d4289c L |
5652 | if (bfd_is_abs_section (htab->elf.sgotplt->output_section)) |
5653 | { | |
4eca0228 | 5654 | _bfd_error_handler |
56d4289c L |
5655 | (_("discarded output section: `%A'"), htab->elf.sgotplt); |
5656 | return FALSE; | |
5657 | } | |
5658 | ||
f604c2a2 L |
5659 | bfd_put_32 (output_bfd, |
5660 | (sdyn == NULL ? 0 | |
5661 | : sdyn->output_section->vma + sdyn->output_offset), | |
5662 | htab->elf.sgotplt->contents); | |
5663 | bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4); | |
5664 | bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8); | |
252b5132 | 5665 | |
6de2ae4a | 5666 | elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4; |
12d0ee4a | 5667 | } |
8c37241b | 5668 | |
e41b3a13 | 5669 | /* Adjust .eh_frame for .plt section. */ |
9a2a56cc AM |
5670 | if (htab->plt_eh_frame != NULL |
5671 | && htab->plt_eh_frame->contents != NULL) | |
e41b3a13 JJ |
5672 | { |
5673 | if (htab->elf.splt != NULL | |
5674 | && htab->elf.splt->size != 0 | |
5675 | && (htab->elf.splt->flags & SEC_EXCLUDE) == 0 | |
5676 | && htab->elf.splt->output_section != NULL | |
5677 | && htab->plt_eh_frame->output_section != NULL) | |
5678 | { | |
5679 | bfd_vma plt_start = htab->elf.splt->output_section->vma; | |
5680 | bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma | |
5681 | + htab->plt_eh_frame->output_offset | |
5682 | + PLT_FDE_START_OFFSET; | |
5683 | bfd_put_signed_32 (dynobj, plt_start - eh_frame_start, | |
5684 | htab->plt_eh_frame->contents | |
5685 | + PLT_FDE_START_OFFSET); | |
5686 | } | |
5687 | if (htab->plt_eh_frame->sec_info_type | |
dbaa2011 | 5688 | == SEC_INFO_TYPE_EH_FRAME) |
e41b3a13 JJ |
5689 | { |
5690 | if (! _bfd_elf_write_section_eh_frame (output_bfd, info, | |
5691 | htab->plt_eh_frame, | |
5692 | htab->plt_eh_frame->contents)) | |
5693 | return FALSE; | |
5694 | } | |
5695 | } | |
5696 | ||
fff53dae L |
5697 | /* Adjust .eh_frame for .plt.got section. */ |
5698 | if (htab->plt_got_eh_frame != NULL | |
5699 | && htab->plt_got_eh_frame->contents != NULL) | |
5700 | { | |
5701 | if (htab->plt_got != NULL | |
5702 | && htab->plt_got->size != 0 | |
5703 | && (htab->plt_got->flags & SEC_EXCLUDE) == 0 | |
5704 | && htab->plt_got->output_section != NULL | |
5705 | && htab->plt_got_eh_frame->output_section != NULL) | |
5706 | { | |
5707 | bfd_vma plt_start = htab->plt_got->output_section->vma; | |
5708 | bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma | |
5709 | + htab->plt_got_eh_frame->output_offset | |
5710 | + PLT_FDE_START_OFFSET; | |
5711 | bfd_put_signed_32 (dynobj, plt_start - eh_frame_start, | |
5712 | htab->plt_got_eh_frame->contents | |
5713 | + PLT_FDE_START_OFFSET); | |
5714 | } | |
5715 | if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME) | |
5716 | { | |
5717 | if (! _bfd_elf_write_section_eh_frame (output_bfd, info, | |
5718 | htab->plt_got_eh_frame, | |
5719 | htab->plt_got_eh_frame->contents)) | |
5720 | return FALSE; | |
5721 | } | |
5722 | } | |
5723 | ||
ee2fdd6f L |
5724 | /* Adjust .eh_frame for the second PLT section. */ |
5725 | if (htab->plt_second_eh_frame != NULL | |
5726 | && htab->plt_second_eh_frame->contents != NULL) | |
5727 | { | |
5728 | if (htab->plt_second != NULL | |
5729 | && htab->plt_second->size != 0 | |
5730 | && (htab->plt_second->flags & SEC_EXCLUDE) == 0 | |
5731 | && htab->plt_second->output_section != NULL | |
5732 | && htab->plt_second_eh_frame->output_section != NULL) | |
5733 | { | |
5734 | bfd_vma plt_start = htab->plt_second->output_section->vma; | |
5735 | bfd_vma eh_frame_start | |
5736 | = (htab->plt_second_eh_frame->output_section->vma | |
5737 | + htab->plt_second_eh_frame->output_offset | |
5738 | + PLT_FDE_START_OFFSET); | |
5739 | bfd_put_signed_32 (dynobj, plt_start - eh_frame_start, | |
5740 | htab->plt_second_eh_frame->contents | |
5741 | + PLT_FDE_START_OFFSET); | |
5742 | } | |
5743 | if (htab->plt_second_eh_frame->sec_info_type | |
5744 | == SEC_INFO_TYPE_EH_FRAME) | |
5745 | { | |
5746 | if (! _bfd_elf_write_section_eh_frame (output_bfd, info, | |
5747 | htab->plt_second_eh_frame, | |
5748 | htab->plt_second_eh_frame->contents)) | |
5749 | return FALSE; | |
5750 | } | |
5751 | } | |
5752 | ||
6de2ae4a L |
5753 | if (htab->elf.sgot && htab->elf.sgot->size > 0) |
5754 | elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4; | |
8c37241b | 5755 | |
aec6b87e L |
5756 | /* Fill PLT entries for undefined weak symbols in PIE. */ |
5757 | if (bfd_link_pie (info)) | |
5758 | bfd_hash_traverse (&info->hash->table, | |
5759 | elf_i386_pie_finish_undefweak_symbol, | |
5760 | info); | |
5761 | ||
b34976b6 | 5762 | return TRUE; |
252b5132 RH |
5763 | } |
5764 | ||
233cc9c1 L |
5765 | /* Fill PLT/GOT entries and allocate dynamic relocations for local |
5766 | STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table. | |
5767 | It has to be done before elf_link_sort_relocs is called so that | |
5768 | dynamic relocations are properly sorted. */ | |
5769 | ||
5770 | static bfd_boolean | |
5771 | elf_i386_output_arch_local_syms | |
5772 | (bfd *output_bfd ATTRIBUTE_UNUSED, | |
5773 | struct bfd_link_info *info, | |
5774 | void *flaginfo ATTRIBUTE_UNUSED, | |
5775 | int (*func) (void *, const char *, | |
5776 | Elf_Internal_Sym *, | |
5777 | asection *, | |
5778 | struct elf_link_hash_entry *) ATTRIBUTE_UNUSED) | |
5779 | { | |
0afcef53 L |
5780 | struct elf_x86_link_hash_table *htab |
5781 | = elf_x86_hash_table (info, I386_ELF_DATA); | |
233cc9c1 L |
5782 | if (htab == NULL) |
5783 | return FALSE; | |
5784 | ||
5785 | /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */ | |
5786 | htab_traverse (htab->loc_hash_table, | |
5787 | elf_i386_finish_local_dynamic_symbol, | |
5788 | info); | |
5789 | ||
5790 | return TRUE; | |
5791 | } | |
5792 | ||
f604c2a2 L |
5793 | enum elf_i386_plt_type |
5794 | { | |
5795 | plt_non_lazy = 0, | |
5796 | plt_lazy = 1 << 0, | |
5797 | plt_pic = 1 << 1, | |
ee2fdd6f | 5798 | plt_second = 1 << 2, |
f604c2a2 L |
5799 | plt_unknown = -1 |
5800 | }; | |
5801 | ||
5802 | struct elf_i386_plt | |
5803 | { | |
5804 | const char *name; | |
5805 | asection *sec; | |
5806 | bfd_byte *contents; | |
5807 | enum elf_i386_plt_type type; | |
5808 | unsigned int plt_got_offset; | |
5809 | unsigned int plt_entry_size; | |
5810 | long count; | |
5811 | }; | |
5812 | ||
5813 | /* Forward declaration. */ | |
5814 | static const struct elf_i386_lazy_plt_layout elf_i386_nacl_plt; | |
5815 | ||
5816 | /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all | |
5817 | dynamic relocations. */ | |
5818 | ||
5819 | static long | |
5820 | elf_i386_get_synthetic_symtab (bfd *abfd, | |
5821 | long symcount ATTRIBUTE_UNUSED, | |
5822 | asymbol **syms ATTRIBUTE_UNUSED, | |
5823 | long dynsymcount, | |
5824 | asymbol **dynsyms, | |
5825 | asymbol **ret) | |
5826 | { | |
98c5dfc9 | 5827 | long size, count, i, n, len; |
f604c2a2 L |
5828 | int j; |
5829 | unsigned int plt_got_offset, plt_entry_size; | |
5830 | asymbol *s; | |
5831 | bfd_byte *plt_contents; | |
5832 | long dynrelcount, relsize; | |
98c5dfc9 | 5833 | arelent **dynrelbuf, *p; |
f604c2a2 L |
5834 | const struct elf_i386_lazy_plt_layout *lazy_plt; |
5835 | const struct elf_i386_non_lazy_plt_layout *non_lazy_plt; | |
ee2fdd6f L |
5836 | const struct elf_i386_lazy_plt_layout *lazy_ibt_plt; |
5837 | const struct elf_i386_non_lazy_plt_layout *non_lazy_ibt_plt; | |
f604c2a2 L |
5838 | asection *plt; |
5839 | bfd_vma got_addr; | |
5840 | char *names; | |
5841 | enum elf_i386_plt_type plt_type; | |
5842 | struct elf_i386_plt plts[] = | |
3972882e | 5843 | { |
f604c2a2 L |
5844 | { ".plt", NULL, NULL, plt_unknown, 0, 0, 0 }, |
5845 | { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0 }, | |
ee2fdd6f | 5846 | { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0 }, |
dd9e66ee | 5847 | { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0 } |
f604c2a2 | 5848 | }; |
144bed8d | 5849 | |
f604c2a2 | 5850 | *ret = NULL; |
144bed8d | 5851 | |
f604c2a2 L |
5852 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
5853 | return 0; | |
3972882e | 5854 | |
f604c2a2 L |
5855 | if (dynsymcount <= 0) |
5856 | return 0; | |
cca5b8b6 | 5857 | |
f604c2a2 L |
5858 | relsize = bfd_get_dynamic_reloc_upper_bound (abfd); |
5859 | if (relsize <= 0) | |
5860 | return -1; | |
cca5b8b6 | 5861 | |
f604c2a2 L |
5862 | non_lazy_plt = NULL; |
5863 | /* Silence GCC 6. */ | |
5864 | lazy_plt = NULL; | |
ee2fdd6f L |
5865 | non_lazy_ibt_plt = NULL; |
5866 | lazy_ibt_plt = NULL; | |
f604c2a2 | 5867 | switch (get_elf_i386_backend_data (abfd)->os) |
144bed8d | 5868 | { |
f604c2a2 L |
5869 | case is_normal: |
5870 | non_lazy_plt = &elf_i386_non_lazy_plt; | |
ee2fdd6f L |
5871 | lazy_ibt_plt = &elf_i386_lazy_ibt_plt; |
5872 | non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt; | |
f604c2a2 L |
5873 | /* Fall through */ |
5874 | case is_vxworks: | |
5875 | lazy_plt = &elf_i386_lazy_plt; | |
5876 | break; | |
5877 | case is_nacl: | |
5878 | lazy_plt = &elf_i386_nacl_plt; | |
5879 | break; | |
5880 | } | |
5881 | ||
5882 | got_addr = 0; | |
3972882e | 5883 | |
f604c2a2 L |
5884 | count = 0; |
5885 | for (j = 0; plts[j].name != NULL; j++) | |
5886 | { | |
5887 | plt = bfd_get_section_by_name (abfd, plts[j].name); | |
90efb642 | 5888 | if (plt == NULL || plt->size == 0) |
6f25f223 | 5889 | continue; |
533d0af0 | 5890 | |
f604c2a2 L |
5891 | /* Get the PLT section contents. */ |
5892 | plt_contents = (bfd_byte *) bfd_malloc (plt->size); | |
5893 | if (plt_contents == NULL) | |
5894 | break; | |
5895 | if (!bfd_get_section_contents (abfd, (asection *) plt, | |
5896 | plt_contents, 0, plt->size)) | |
5897 | { | |
5898 | free (plt_contents); | |
5899 | break; | |
5900 | } | |
5901 | ||
5902 | /* Check what kind of PLT it is. */ | |
5903 | plt_type = plt_unknown; | |
90efb642 L |
5904 | if (plts[j].type == plt_unknown |
5905 | && (plt->size >= (lazy_plt->plt0_entry_size | |
5906 | + lazy_plt->plt_entry_size))) | |
f604c2a2 L |
5907 | { |
5908 | /* Match lazy PLT first. */ | |
5909 | if (memcmp (plt_contents, lazy_plt->plt0_entry, | |
5910 | lazy_plt->plt0_got1_offset) == 0) | |
ee2fdd6f L |
5911 | { |
5912 | /* The fist entry in the lazy IBT PLT is the same as the | |
5913 | normal lazy PLT. */ | |
5914 | if (lazy_ibt_plt != NULL | |
90efb642 | 5915 | && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size, |
ee2fdd6f L |
5916 | lazy_ibt_plt->plt_entry, |
5917 | lazy_ibt_plt->plt_got_offset) == 0)) | |
5918 | plt_type = plt_lazy | plt_second; | |
5919 | else | |
5920 | plt_type = plt_lazy; | |
5921 | } | |
f604c2a2 L |
5922 | else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry, |
5923 | lazy_plt->plt0_got1_offset) == 0) | |
ee2fdd6f L |
5924 | { |
5925 | /* The fist entry in the PIC lazy IBT PLT is the same as | |
5926 | the normal PIC lazy PLT. */ | |
5927 | if (lazy_ibt_plt != NULL | |
90efb642 | 5928 | && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size, |
ee2fdd6f L |
5929 | lazy_ibt_plt->pic_plt_entry, |
5930 | lazy_ibt_plt->plt_got_offset) == 0)) | |
5931 | plt_type = plt_lazy | plt_pic | plt_second; | |
5932 | else | |
5933 | plt_type = plt_lazy | plt_pic; | |
5934 | } | |
f604c2a2 L |
5935 | } |
5936 | ||
5937 | if (non_lazy_plt != NULL | |
90efb642 L |
5938 | && (plt_type == plt_unknown || plt_type == plt_non_lazy) |
5939 | && plt->size >= non_lazy_plt->plt_entry_size) | |
f604c2a2 L |
5940 | { |
5941 | /* Match non-lazy PLT. */ | |
5942 | if (memcmp (plt_contents, non_lazy_plt->plt_entry, | |
5943 | non_lazy_plt->plt_got_offset) == 0) | |
5944 | plt_type = plt_non_lazy; | |
5945 | else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry, | |
5946 | non_lazy_plt->plt_got_offset) == 0) | |
5947 | plt_type = plt_pic; | |
5948 | } | |
5949 | ||
ee2fdd6f | 5950 | if ((non_lazy_ibt_plt != NULL) |
90efb642 L |
5951 | && (plt_type == plt_unknown || plt_type == plt_second) |
5952 | && plt->size >= non_lazy_ibt_plt->plt_entry_size) | |
ee2fdd6f L |
5953 | { |
5954 | if (memcmp (plt_contents, | |
5955 | non_lazy_ibt_plt->plt_entry, | |
5956 | non_lazy_ibt_plt->plt_got_offset) == 0) | |
5957 | { | |
5958 | /* Match IBT PLT. */ | |
5959 | plt_type = plt_second; | |
5960 | non_lazy_plt = non_lazy_ibt_plt; | |
5961 | } | |
5962 | else if (memcmp (plt_contents, | |
5963 | non_lazy_ibt_plt->pic_plt_entry, | |
5964 | non_lazy_ibt_plt->plt_got_offset) == 0) | |
5965 | { | |
5966 | /* Match PIC IBT PLT. */ | |
5967 | plt_type = plt_second | plt_pic; | |
5968 | non_lazy_plt = non_lazy_ibt_plt; | |
5969 | } | |
5970 | } | |
5971 | ||
f604c2a2 | 5972 | if (plt_type == plt_unknown) |
37c0b6ee L |
5973 | { |
5974 | free (plt_contents); | |
5975 | continue; | |
5976 | } | |
3972882e | 5977 | |
f604c2a2 L |
5978 | plts[j].sec = plt; |
5979 | plts[j].type = plt_type; | |
ec1f73bb | 5980 | |
f604c2a2 L |
5981 | if ((plt_type & plt_lazy)) |
5982 | { | |
5983 | plts[j].plt_got_offset = lazy_plt->plt_got_offset; | |
5984 | plts[j].plt_entry_size = lazy_plt->plt_entry_size; | |
5985 | /* Skip PLT0 in lazy PLT. */ | |
5986 | i = 1; | |
5987 | } | |
5988 | else | |
5989 | { | |
5990 | plts[j].plt_got_offset = non_lazy_plt->plt_got_offset; | |
5991 | plts[j].plt_entry_size = non_lazy_plt->plt_entry_size; | |
5992 | i = 0; | |
5993 | } | |
fca6ae69 | 5994 | |
ee2fdd6f L |
5995 | /* Skip lazy PLT when the second PLT is used. */ |
5996 | if ((plt_type & (plt_lazy | plt_second)) | |
5997 | == (plt_lazy | plt_second)) | |
5998 | plts[j].count = 0; | |
5999 | else | |
6000 | { | |
6001 | n = plt->size / plts[j].plt_entry_size; | |
6002 | plts[j].count = n; | |
6003 | count += n - i; | |
6004 | } | |
f604c2a2 L |
6005 | |
6006 | plts[j].contents = plt_contents; | |
6007 | ||
6008 | /* The _GLOBAL_OFFSET_TABLE_ address is needed. */ | |
6009 | if ((plt_type & plt_pic)) | |
6010 | got_addr = (bfd_vma) -1; | |
144bed8d L |
6011 | } |
6012 | ||
90efb642 L |
6013 | if (count == 0) |
6014 | return -1; | |
6015 | ||
37c0b6ee L |
6016 | dynrelbuf = (arelent **) bfd_malloc (relsize); |
6017 | if (dynrelbuf == NULL) | |
6018 | return -1; | |
6019 | ||
6020 | dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf, | |
6021 | dynsyms); | |
6022 | ||
6023 | /* Sort the relocs by address. */ | |
0afcef53 L |
6024 | qsort (dynrelbuf, dynrelcount, sizeof (arelent *), |
6025 | _bfd_x86_elf_compare_relocs); | |
37c0b6ee | 6026 | |
f604c2a2 | 6027 | size = count * sizeof (asymbol); |
98c5dfc9 L |
6028 | |
6029 | /* Allocate space for @plt suffixes. */ | |
6030 | n = 0; | |
6031 | for (i = 0; i < dynrelcount; i++) | |
6032 | { | |
6033 | p = dynrelbuf[i]; | |
6034 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
6035 | if (p->addend != 0) | |
6036 | size += sizeof ("+0x") - 1 + 8; | |
6037 | } | |
6038 | ||
f604c2a2 L |
6039 | s = *ret = (asymbol *) bfd_zmalloc (size); |
6040 | if (s == NULL) | |
823cb067 | 6041 | goto bad_return; |
3972882e | 6042 | |
f604c2a2 L |
6043 | if (got_addr) |
6044 | { | |
6045 | /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_ | |
6046 | address. */ | |
6047 | asection *sec = bfd_get_section_by_name (abfd, ".got.plt"); | |
6048 | if (sec != NULL) | |
6049 | got_addr = sec->vma; | |
6050 | else | |
6051 | { | |
6052 | sec = bfd_get_section_by_name (abfd, ".got"); | |
6053 | if (sec != NULL) | |
6054 | got_addr = sec->vma; | |
6055 | } | |
3972882e | 6056 | |
f604c2a2 L |
6057 | if (got_addr == (bfd_vma) -1) |
6058 | goto bad_return; | |
6059 | } | |
3972882e | 6060 | |
f604c2a2 | 6061 | /* Check for each PLT section. */ |
98c5dfc9 | 6062 | names = (char *) (s + count); |
f604c2a2 L |
6063 | size = 0; |
6064 | n = 0; | |
6065 | for (j = 0; plts[j].name != NULL; j++) | |
6066 | if ((plt_contents = plts[j].contents) != NULL) | |
6067 | { | |
6068 | long k; | |
6069 | bfd_vma offset; | |
6070 | ||
6071 | plt_got_offset = plts[j].plt_got_offset; | |
6072 | plt_entry_size = plts[j].plt_entry_size; | |
6073 | ||
6074 | plt = plts[j].sec; | |
6075 | ||
6076 | if ((plts[j].type & plt_lazy)) | |
6077 | { | |
6078 | /* Skip PLT0 in lazy PLT. */ | |
6079 | k = 1; | |
6080 | offset = plt_entry_size; | |
6081 | } | |
6082 | else | |
6083 | { | |
6084 | k = 0; | |
6085 | offset = 0; | |
6086 | } | |
6087 | ||
6088 | /* Check each PLT entry against dynamic relocations. */ | |
6089 | for (; k < plts[j].count; k++) | |
6090 | { | |
6091 | int off; | |
6092 | bfd_vma got_vma; | |
6093 | long min, max, mid; | |
f604c2a2 L |
6094 | |
6095 | /* Get the GOT offset, a signed 32-bit integer. */ | |
6096 | off = H_GET_32 (abfd, (plt_contents + offset | |
6097 | + plt_got_offset)); | |
6098 | got_vma = got_addr + off; | |
6099 | ||
6100 | /* Binary search. */ | |
6101 | p = dynrelbuf[0]; | |
6102 | min = 0; | |
6103 | max = dynrelcount; | |
6104 | while ((min + 1) < max) | |
6105 | { | |
6106 | arelent *r; | |
6107 | ||
6108 | mid = (min + max) / 2; | |
6109 | r = dynrelbuf[mid]; | |
6110 | if (got_vma > r->address) | |
6111 | min = mid; | |
6112 | else if (got_vma < r->address) | |
6113 | max = mid; | |
6114 | else | |
6115 | { | |
6116 | p = r; | |
6117 | break; | |
6118 | } | |
6119 | } | |
6120 | ||
6121 | /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */ | |
6122 | if (got_vma == p->address | |
6123 | && p->howto != NULL | |
6124 | && (p->howto->type == R_386_JUMP_SLOT | |
6125 | || p->howto->type == R_386_GLOB_DAT | |
6126 | || p->howto->type == R_386_IRELATIVE)) | |
6127 | { | |
6128 | *s = **p->sym_ptr_ptr; | |
6129 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL | |
6130 | set. Since we are defining a symbol, ensure one | |
6131 | of them is set. */ | |
6132 | if ((s->flags & BSF_LOCAL) == 0) | |
6133 | s->flags |= BSF_GLOBAL; | |
6134 | s->flags |= BSF_SYNTHETIC; | |
6135 | /* This is no longer a section symbol. */ | |
6136 | s->flags &= ~BSF_SECTION_SYM; | |
6137 | s->section = plt; | |
6138 | s->the_bfd = plt->owner; | |
6139 | s->value = offset; | |
98c5dfc9 L |
6140 | s->udata.p = NULL; |
6141 | s->name = names; | |
6142 | len = strlen ((*p->sym_ptr_ptr)->name); | |
6143 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
6144 | names += len; | |
f604c2a2 | 6145 | if (p->addend != 0) |
98c5dfc9 L |
6146 | { |
6147 | char buf[30], *a; | |
6148 | ||
6149 | memcpy (names, "+0x", sizeof ("+0x") - 1); | |
6150 | names += sizeof ("+0x") - 1; | |
6151 | bfd_sprintf_vma (abfd, buf, p->addend); | |
6152 | for (a = buf; *a == '0'; ++a) | |
6153 | ; | |
6154 | size = strlen (a); | |
6155 | memcpy (names, a, size); | |
6156 | names += size; | |
6157 | } | |
6158 | memcpy (names, "@plt", sizeof ("@plt")); | |
6159 | names += sizeof ("@plt"); | |
f604c2a2 L |
6160 | n++; |
6161 | s++; | |
6162 | } | |
6163 | offset += plt_entry_size; | |
6164 | } | |
6165 | } | |
6166 | ||
6167 | /* PLT entries with R_386_TLS_DESC relocations are skipped. */ | |
6168 | if (n == 0) | |
823cb067 L |
6169 | { |
6170 | bad_return: | |
6171 | count = -1; | |
6172 | } | |
6173 | else | |
6174 | count = n; | |
f604c2a2 | 6175 | |
f604c2a2 L |
6176 | for (j = 0; plts[j].name != NULL; j++) |
6177 | if (plts[j].contents != NULL) | |
6178 | free (plts[j].contents); | |
6179 | ||
6180 | free (dynrelbuf); | |
6181 | ||
6182 | return count; | |
4c45e5c9 JJ |
6183 | } |
6184 | ||
f604c2a2 L |
6185 | /* Set up i386 GNU properties. Return the first relocatable ELF input |
6186 | with GNU properties if found. Otherwise, return NULL. */ | |
6187 | ||
6188 | static bfd * | |
6189 | elf_i386_link_setup_gnu_properties (struct bfd_link_info *info) | |
6190 | { | |
6191 | bfd_boolean normal_target; | |
ee2fdd6f | 6192 | bfd_boolean lazy_plt; |
f604c2a2 L |
6193 | asection *sec, *pltsec; |
6194 | bfd *dynobj; | |
ee2fdd6f L |
6195 | bfd_boolean use_ibt_plt; |
6196 | unsigned int plt_alignment, features; | |
0afcef53 | 6197 | struct elf_x86_link_hash_table *htab; |
ee2fdd6f | 6198 | bfd *pbfd; |
73784fa5 L |
6199 | bfd *ebfd = NULL; |
6200 | elf_property *prop; | |
0afcef53 L |
6201 | struct elf_i386_plt_layout *plt_layout; |
6202 | const struct elf_i386_lazy_plt_layout *lazy_plt_layout; | |
6203 | const struct elf_i386_non_lazy_plt_layout *non_lazy_plt_layout; | |
ee2fdd6f L |
6204 | |
6205 | features = 0; | |
6206 | if (info->ibt) | |
6207 | features = GNU_PROPERTY_X86_FEATURE_1_IBT; | |
48580982 L |
6208 | if (info->shstk) |
6209 | features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; | |
ee2fdd6f | 6210 | |
73784fa5 L |
6211 | /* Find a normal input file with GNU property note. */ |
6212 | for (pbfd = info->input_bfds; | |
6213 | pbfd != NULL; | |
6214 | pbfd = pbfd->link.next) | |
6215 | if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour | |
6216 | && bfd_count_sections (pbfd) != 0) | |
6217 | { | |
6218 | ebfd = pbfd; | |
ee2fdd6f | 6219 | |
73784fa5 L |
6220 | if (elf_properties (pbfd) != NULL) |
6221 | break; | |
6222 | } | |
6223 | ||
a5b4ee94 | 6224 | if (ebfd != NULL && features) |
73784fa5 | 6225 | { |
a5b4ee94 L |
6226 | /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and |
6227 | GNU_PROPERTY_X86_FEATURE_1_SHSTK. */ | |
6228 | prop = _bfd_elf_get_property (ebfd, | |
6229 | GNU_PROPERTY_X86_FEATURE_1_AND, | |
6230 | 4); | |
6231 | prop->u.number |= features; | |
6232 | prop->pr_kind = property_number; | |
6233 | ||
6234 | /* Create the GNU property note section if needed. */ | |
6235 | if (pbfd == NULL) | |
ee2fdd6f | 6236 | { |
a5b4ee94 L |
6237 | sec = bfd_make_section_with_flags (ebfd, |
6238 | NOTE_GNU_PROPERTY_SECTION_NAME, | |
6239 | (SEC_ALLOC | |
6240 | | SEC_LOAD | |
6241 | | SEC_IN_MEMORY | |
6242 | | SEC_READONLY | |
6243 | | SEC_HAS_CONTENTS | |
6244 | | SEC_DATA)); | |
6245 | if (sec == NULL) | |
6246 | info->callbacks->einfo (_("%F: failed to create GNU property section\n")); | |
ee2fdd6f | 6247 | |
a5b4ee94 L |
6248 | if (!bfd_set_section_alignment (ebfd, sec, 2)) |
6249 | { | |
73784fa5 | 6250 | error_alignment: |
a5b4ee94 L |
6251 | info->callbacks->einfo (_("%F%A: failed to align section\n"), |
6252 | sec); | |
73784fa5 | 6253 | } |
73784fa5 | 6254 | |
a5b4ee94 L |
6255 | elf_section_type (sec) = SHT_NOTE; |
6256 | } | |
ee2fdd6f L |
6257 | } |
6258 | ||
6259 | pbfd = _bfd_elf_link_setup_gnu_properties (info); | |
f604c2a2 L |
6260 | |
6261 | if (bfd_link_relocatable (info)) | |
6262 | return pbfd; | |
6263 | ||
0afcef53 | 6264 | htab = elf_x86_hash_table (info, I386_ELF_DATA); |
f604c2a2 L |
6265 | if (htab == NULL) |
6266 | return pbfd; | |
6267 | ||
ee2fdd6f L |
6268 | use_ibt_plt = info->ibtplt || info->ibt; |
6269 | if (!use_ibt_plt && pbfd != NULL) | |
6270 | { | |
6271 | /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */ | |
6272 | elf_property_list *p; | |
6273 | ||
6274 | /* The property list is sorted in order of type. */ | |
6275 | for (p = elf_properties (pbfd); p; p = p->next) | |
6276 | { | |
6277 | if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type) | |
6278 | { | |
6279 | use_ibt_plt = !!(p->property.u.number | |
6280 | & GNU_PROPERTY_X86_FEATURE_1_IBT); | |
6281 | break; | |
6282 | } | |
6283 | else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type) | |
6284 | break; | |
6285 | } | |
6286 | } | |
6287 | ||
f604c2a2 L |
6288 | dynobj = htab->elf.dynobj; |
6289 | ||
6290 | /* Set htab->elf.dynobj here so that there is no need to check and | |
6291 | set it in check_relocs. */ | |
6292 | if (dynobj == NULL) | |
6293 | { | |
b7a18930 NC |
6294 | if (pbfd != NULL) |
6295 | { | |
6296 | htab->elf.dynobj = pbfd; | |
6297 | dynobj = pbfd; | |
6298 | } | |
6299 | else | |
6300 | { | |
6301 | bfd *abfd; | |
6302 | ||
6303 | /* Find a normal input file to hold linker created | |
6304 | sections. */ | |
6305 | for (abfd = info->input_bfds; | |
6306 | abfd != NULL; | |
6307 | abfd = abfd->link.next) | |
6308 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
6309 | && (abfd->flags | |
6310 | & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0) | |
6311 | { | |
6312 | htab->elf.dynobj = abfd; | |
6313 | dynobj = abfd; | |
6314 | break; | |
6315 | } | |
6316 | } | |
f604c2a2 L |
6317 | } |
6318 | ||
0afcef53 L |
6319 | plt_layout = &elf_i386_plt (htab); |
6320 | ||
f604c2a2 L |
6321 | /* Even when lazy binding is disabled by "-z now", the PLT0 entry may |
6322 | still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for | |
6323 | canonical function address. */ | |
0afcef53 | 6324 | plt_layout->has_plt0 = 1; |
f604c2a2 L |
6325 | normal_target = FALSE; |
6326 | ||
6327 | switch (get_elf_i386_backend_data (info->output_bfd)->os) | |
6328 | { | |
6329 | case is_normal: | |
ee2fdd6f L |
6330 | if (use_ibt_plt) |
6331 | { | |
0afcef53 L |
6332 | elf_i386_lazy_plt (htab) = &elf_i386_lazy_ibt_plt; |
6333 | elf_i386_non_lazy_plt (htab) = &elf_i386_non_lazy_ibt_plt; | |
ee2fdd6f L |
6334 | } |
6335 | else | |
6336 | { | |
0afcef53 L |
6337 | elf_i386_lazy_plt (htab) = &elf_i386_lazy_plt; |
6338 | elf_i386_non_lazy_plt (htab) = &elf_i386_non_lazy_plt; | |
ee2fdd6f | 6339 | } |
f604c2a2 L |
6340 | normal_target = TRUE; |
6341 | break; | |
6342 | case is_vxworks: | |
0afcef53 L |
6343 | elf_i386_lazy_plt (htab) = &elf_i386_lazy_plt; |
6344 | elf_i386_non_lazy_plt (htab) = NULL; | |
f604c2a2 | 6345 | if (!elf_vxworks_create_dynamic_sections (dynobj, info, |
0afcef53 | 6346 | &elf_i386_srelplt2 (htab))) |
f604c2a2 L |
6347 | info->callbacks->einfo (_("%F: failed to create VxWorks dynamic sections\n")); |
6348 | break; | |
6349 | case is_nacl: | |
0afcef53 L |
6350 | elf_i386_lazy_plt (htab) = &elf_i386_nacl_plt; |
6351 | elf_i386_non_lazy_plt (htab) = NULL; | |
f604c2a2 L |
6352 | break; |
6353 | } | |
6354 | ||
0afcef53 L |
6355 | lazy_plt_layout = elf_i386_lazy_plt (htab); |
6356 | non_lazy_plt_layout = elf_i386_non_lazy_plt (htab); | |
6357 | ||
f604c2a2 L |
6358 | pltsec = htab->elf.splt; |
6359 | ||
6360 | /* If the non-lazy PLT is available, use it for all PLT entries if | |
6361 | there are no PLT0 or no .plt section. */ | |
0afcef53 L |
6362 | if (non_lazy_plt_layout != NULL |
6363 | && (!plt_layout->has_plt0 || pltsec == NULL)) | |
f604c2a2 | 6364 | { |
ee2fdd6f | 6365 | lazy_plt = FALSE; |
f604c2a2 | 6366 | if (bfd_link_pic (info)) |
0afcef53 L |
6367 | plt_layout->plt_entry |
6368 | = non_lazy_plt_layout->pic_plt_entry; | |
f604c2a2 | 6369 | else |
0afcef53 L |
6370 | plt_layout->plt_entry |
6371 | = non_lazy_plt_layout->plt_entry; | |
6372 | plt_layout->plt_entry_size | |
6373 | = non_lazy_plt_layout->plt_entry_size; | |
6374 | plt_layout->plt_got_offset | |
6375 | = non_lazy_plt_layout->plt_got_offset; | |
6376 | plt_layout->eh_frame_plt_size | |
6377 | = non_lazy_plt_layout->eh_frame_plt_size; | |
6378 | plt_layout->eh_frame_plt | |
6379 | = non_lazy_plt_layout->eh_frame_plt; | |
f604c2a2 L |
6380 | } |
6381 | else | |
6382 | { | |
ee2fdd6f | 6383 | lazy_plt = TRUE; |
f604c2a2 L |
6384 | if (bfd_link_pic (info)) |
6385 | { | |
0afcef53 L |
6386 | plt_layout->plt0_entry |
6387 | = lazy_plt_layout->pic_plt0_entry; | |
6388 | plt_layout->plt_entry | |
6389 | = lazy_plt_layout->pic_plt_entry; | |
f604c2a2 L |
6390 | } |
6391 | else | |
6392 | { | |
0afcef53 L |
6393 | plt_layout->plt0_entry |
6394 | = lazy_plt_layout->plt0_entry; | |
6395 | plt_layout->plt_entry | |
6396 | = lazy_plt_layout->plt_entry; | |
f604c2a2 | 6397 | } |
0afcef53 L |
6398 | plt_layout->plt_entry_size |
6399 | = lazy_plt_layout->plt_entry_size; | |
6400 | plt_layout->plt_got_offset | |
6401 | = lazy_plt_layout->plt_got_offset; | |
6402 | plt_layout->eh_frame_plt_size | |
6403 | = lazy_plt_layout->eh_frame_plt_size; | |
6404 | plt_layout->eh_frame_plt | |
6405 | = lazy_plt_layout->eh_frame_plt; | |
f604c2a2 L |
6406 | } |
6407 | ||
6408 | /* Return if there are no normal input files. */ | |
6409 | if (dynobj == NULL) | |
6410 | return pbfd; | |
6411 | ||
6412 | /* Since create_dynamic_sections isn't always called, but GOT | |
b7a18930 | 6413 | relocations need GOT sections, create them here so that we |
f604c2a2 L |
6414 | don't need to do it in check_relocs. */ |
6415 | if (htab->elf.sgot == NULL | |
6416 | && !_bfd_elf_create_got_section (dynobj, info)) | |
6417 | info->callbacks->einfo (_("%F: failed to create GOT sections\n")); | |
6418 | ||
6419 | /* Create the ifunc sections here so that check_relocs can be | |
6420 | simplified. */ | |
6421 | if (!_bfd_elf_create_ifunc_sections (dynobj, info)) | |
6422 | info->callbacks->einfo (_("%F: failed to create ifunc sections\n")); | |
6423 | ||
0afcef53 | 6424 | plt_alignment = bfd_log2 (plt_layout->plt_entry_size); |
f604c2a2 L |
6425 | |
6426 | if (pltsec != NULL) | |
6427 | { | |
6428 | /* Whe creating executable, set the contents of the .interp | |
6429 | section to the interpreter. */ | |
6430 | if (bfd_link_executable (info) && !info->nointerp) | |
6431 | { | |
6432 | asection *s = bfd_get_linker_section (dynobj, ".interp"); | |
6433 | if (s == NULL) | |
6434 | abort (); | |
0afcef53 L |
6435 | s->size = htab->dynamic_interpreter_size; |
6436 | s->contents = (unsigned char *) htab->dynamic_interpreter; | |
f604c2a2 L |
6437 | htab->interp = s; |
6438 | } | |
6439 | ||
6440 | /* Don't change PLT section alignment for NaCl since it uses | |
6441 | 64-byte PLT entry and sets PLT section alignment to 32 | |
6442 | bytes. */ | |
6443 | if (normal_target) | |
6444 | { | |
6445 | const struct elf_backend_data *bed | |
6446 | = get_elf_backend_data (dynobj); | |
6447 | flagword pltflags = (bed->dynamic_sec_flags | |
6448 | | SEC_ALLOC | |
6449 | | SEC_CODE | |
6450 | | SEC_LOAD | |
6451 | | SEC_READONLY); | |
6452 | unsigned int non_lazy_plt_alignment | |
0afcef53 | 6453 | = bfd_log2 (non_lazy_plt_layout->plt_entry_size); |
f604c2a2 L |
6454 | |
6455 | sec = pltsec; | |
6456 | if (!bfd_set_section_alignment (sec->owner, sec, | |
6457 | plt_alignment)) | |
6458 | goto error_alignment; | |
6459 | ||
6460 | /* Create the GOT procedure linkage table. */ | |
6461 | sec = bfd_make_section_anyway_with_flags (dynobj, | |
6462 | ".plt.got", | |
6463 | pltflags); | |
6464 | if (sec == NULL) | |
6465 | info->callbacks->einfo (_("%F: failed to create GOT PLT section\n")); | |
6466 | ||
6467 | if (!bfd_set_section_alignment (dynobj, sec, | |
6468 | non_lazy_plt_alignment)) | |
6469 | goto error_alignment; | |
6470 | ||
6471 | htab->plt_got = sec; | |
ee2fdd6f L |
6472 | |
6473 | if (lazy_plt) | |
6474 | { | |
6475 | sec = NULL; | |
6476 | ||
6477 | if (use_ibt_plt) | |
6478 | { | |
6479 | /* Create the second PLT for Intel IBT support. IBT | |
6480 | PLT is supported only for non-NaCl target and is | |
6481 | is needed only for lazy binding. */ | |
6482 | sec = bfd_make_section_anyway_with_flags (dynobj, | |
6483 | ".plt.sec", | |
6484 | pltflags); | |
6485 | if (sec == NULL) | |
6486 | info->callbacks->einfo (_("%F: failed to create IBT-enabled PLT section\n")); | |
6487 | ||
6488 | if (!bfd_set_section_alignment (dynobj, sec, | |
6489 | plt_alignment)) | |
6490 | goto error_alignment; | |
6491 | } | |
6492 | ||
6493 | htab->plt_second = sec; | |
6494 | } | |
f604c2a2 L |
6495 | } |
6496 | ||
6497 | if (!info->no_ld_generated_unwind_info) | |
6498 | { | |
6499 | flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | |
6500 | | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
6501 | | SEC_LINKER_CREATED); | |
6502 | ||
6503 | sec = bfd_make_section_anyway_with_flags (dynobj, | |
6504 | ".eh_frame", | |
6505 | flags); | |
6506 | if (sec == NULL) | |
6507 | info->callbacks->einfo (_("%F: failed to create PLT .eh_frame section\n")); | |
6508 | ||
6509 | if (!bfd_set_section_alignment (dynobj, sec, 2)) | |
6510 | goto error_alignment; | |
6511 | ||
6512 | htab->plt_eh_frame = sec; | |
6513 | ||
6514 | if (htab->plt_got != NULL) | |
6515 | { | |
6516 | sec = bfd_make_section_anyway_with_flags (dynobj, | |
6517 | ".eh_frame", | |
6518 | flags); | |
6519 | if (sec == NULL) | |
6520 | info->callbacks->einfo (_("%F: failed to create GOT PLT .eh_frame section\n")); | |
6521 | ||
6522 | if (!bfd_set_section_alignment (dynobj, sec, 2)) | |
6523 | goto error_alignment; | |
6524 | ||
6525 | htab->plt_got_eh_frame = sec; | |
6526 | } | |
6527 | } | |
6528 | } | |
6529 | ||
6530 | if (normal_target) | |
6531 | { | |
6532 | /* The .iplt section is used for IFUNC symbols in static | |
6533 | executables. */ | |
6534 | sec = htab->elf.iplt; | |
6535 | if (sec != NULL | |
6536 | && !bfd_set_section_alignment (sec->owner, sec, | |
6537 | plt_alignment)) | |
73784fa5 | 6538 | goto error_alignment; |
f604c2a2 L |
6539 | } |
6540 | ||
6541 | return pbfd; | |
6542 | } | |
6543 | ||
6d00b590 | 6544 | #define TARGET_LITTLE_SYM i386_elf32_vec |
252b5132 RH |
6545 | #define TARGET_LITTLE_NAME "elf32-i386" |
6546 | #define ELF_ARCH bfd_arch_i386 | |
ae95ffa6 | 6547 | #define ELF_TARGET_ID I386_ELF_DATA |
252b5132 RH |
6548 | #define ELF_MACHINE_CODE EM_386 |
6549 | #define ELF_MAXPAGESIZE 0x1000 | |
252b5132 RH |
6550 | |
6551 | #define elf_backend_can_gc_sections 1 | |
51b64d56 | 6552 | #define elf_backend_can_refcount 1 |
252b5132 RH |
6553 | #define elf_backend_want_got_plt 1 |
6554 | #define elf_backend_plt_readonly 1 | |
6555 | #define elf_backend_want_plt_sym 0 | |
6556 | #define elf_backend_got_header_size 12 | |
e41b3a13 | 6557 | #define elf_backend_plt_alignment 4 |
64f52338 | 6558 | #define elf_backend_dtrel_excludes_plt 1 |
f7483970 | 6559 | #define elf_backend_extern_protected_data 1 |
bedfd056 | 6560 | #define elf_backend_caches_rawsize 1 |
5474d94f | 6561 | #define elf_backend_want_dynrelro 1 |
252b5132 | 6562 | |
8c29f035 AM |
6563 | /* Support RELA for objdump of prelink objects. */ |
6564 | #define elf_info_to_howto elf_i386_info_to_howto_rel | |
dd5724d5 AM |
6565 | #define elf_info_to_howto_rel elf_i386_info_to_howto_rel |
6566 | ||
13ae64f3 | 6567 | #define bfd_elf32_mkobject elf_i386_mkobject |
13ae64f3 | 6568 | |
dd5724d5 AM |
6569 | #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name |
6570 | #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create | |
6571 | #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup | |
13285a1b | 6572 | #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup |
3972882e | 6573 | #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab |
dd5724d5 AM |
6574 | |
6575 | #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol | |
13285a1b | 6576 | #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible |
dd5724d5 | 6577 | #define elf_backend_check_relocs elf_i386_check_relocs |
f604c2a2 | 6578 | #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections |
0ac8d2ca | 6579 | #define elf_backend_fake_sections elf_i386_fake_sections |
dd5724d5 AM |
6580 | #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections |
6581 | #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol | |
233cc9c1 | 6582 | #define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms |
dd5724d5 | 6583 | #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook |
c5fccbec DJ |
6584 | #define elf_backend_grok_prstatus elf_i386_grok_prstatus |
6585 | #define elf_backend_grok_psinfo elf_i386_grok_psinfo | |
db6751f2 | 6586 | #define elf_backend_reloc_type_class elf_i386_reloc_type_class |
0ac8d2ca AM |
6587 | #define elf_backend_relocate_section elf_i386_relocate_section |
6588 | #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections | |
f604c2a2 | 6589 | #define elf_backend_setup_gnu_properties elf_i386_link_setup_gnu_properties |
dd5724d5 | 6590 | |
252b5132 | 6591 | #include "elf32-target.h" |
2bc3c89a AM |
6592 | |
6593 | /* FreeBSD support. */ | |
6594 | ||
6595 | #undef TARGET_LITTLE_SYM | |
6d00b590 | 6596 | #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec |
2bc3c89a AM |
6597 | #undef TARGET_LITTLE_NAME |
6598 | #define TARGET_LITTLE_NAME "elf32-i386-freebsd" | |
d1036acb L |
6599 | #undef ELF_OSABI |
6600 | #define ELF_OSABI ELFOSABI_FREEBSD | |
2bc3c89a AM |
6601 | |
6602 | /* The kernel recognizes executables as valid only if they carry a | |
6603 | "FreeBSD" label in the ELF header. So we put this label on all | |
6604 | executables and (for simplicity) also all other object files. */ | |
6605 | ||
2bc3c89a | 6606 | static void |
d8045f23 | 6607 | elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info) |
2bc3c89a | 6608 | { |
78245035 | 6609 | _bfd_elf_post_process_headers (abfd, info); |
2bc3c89a | 6610 | |
2bc3c89a | 6611 | #ifdef OLD_FREEBSD_ABI_LABEL |
cf7363b4 L |
6612 | { |
6613 | /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */ | |
6614 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
6615 | memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8); | |
6616 | } | |
caf47ea6 | 6617 | #endif |
2bc3c89a AM |
6618 | } |
6619 | ||
6620 | #undef elf_backend_post_process_headers | |
d8045f23 | 6621 | #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers |
571fe01f NC |
6622 | #undef elf32_bed |
6623 | #define elf32_bed elf32_i386_fbsd_bed | |
2bc3c89a | 6624 | |
d8045f23 NC |
6625 | #undef elf_backend_add_symbol_hook |
6626 | ||
2bc3c89a | 6627 | #include "elf32-target.h" |
eac338cf | 6628 | |
a6cc6b3b RO |
6629 | /* Solaris 2. */ |
6630 | ||
6631 | #undef TARGET_LITTLE_SYM | |
6d00b590 | 6632 | #define TARGET_LITTLE_SYM i386_elf32_sol2_vec |
a6cc6b3b RO |
6633 | #undef TARGET_LITTLE_NAME |
6634 | #define TARGET_LITTLE_NAME "elf32-i386-sol2" | |
6635 | ||
914082d1 L |
6636 | #undef elf_backend_post_process_headers |
6637 | ||
a6cc6b3b RO |
6638 | /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE |
6639 | objects won't be recognized. */ | |
6640 | #undef ELF_OSABI | |
6641 | ||
6642 | #undef elf32_bed | |
6643 | #define elf32_bed elf32_i386_sol2_bed | |
6644 | ||
7dc98aea RO |
6645 | /* The 32-bit static TLS arena size is rounded to the nearest 8-byte |
6646 | boundary. */ | |
84865015 | 6647 | #undef elf_backend_static_tls_alignment |
7dc98aea RO |
6648 | #define elf_backend_static_tls_alignment 8 |
6649 | ||
a6cc6b3b RO |
6650 | /* The Solaris 2 ABI requires a plt symbol on all platforms. |
6651 | ||
6652 | Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output | |
6653 | File, p.63. */ | |
84865015 | 6654 | #undef elf_backend_want_plt_sym |
a6cc6b3b RO |
6655 | #define elf_backend_want_plt_sym 1 |
6656 | ||
84865015 NC |
6657 | #undef elf_backend_strtab_flags |
6658 | #define elf_backend_strtab_flags SHF_STRINGS | |
6659 | ||
5522f910 NC |
6660 | /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which |
6661 | has a type >= SHT_LOOS. Returns TRUE if these fields were initialised | |
6662 | FALSE otherwise. ISECTION is the best guess matching section from the | |
6663 | input bfd IBFD, but it might be NULL. */ | |
6664 | ||
84865015 | 6665 | static bfd_boolean |
5522f910 NC |
6666 | elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED, |
6667 | bfd *obfd ATTRIBUTE_UNUSED, | |
6668 | const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED, | |
6669 | Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED) | |
84865015 NC |
6670 | { |
6671 | /* PR 19938: FIXME: Need to add code for setting the sh_info | |
5522f910 NC |
6672 | and sh_link fields of Solaris specific section types. */ |
6673 | return FALSE; | |
84865015 | 6674 | |
5522f910 | 6675 | /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13, |
84865015 NC |
6676 | Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation: |
6677 | ||
6678 | http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc | |
6679 | ||
6680 | The following values should be set: | |
6681 | ||
6682 | Type Link Info | |
6683 | ----------------------------------------------------------------------------- | |
6684 | SHT_SUNW_ancillary The section header index of 0 | |
6685 | [0x6fffffee] the associated string table. | |
6686 | ||
6687 | SHT_SUNW_capinfo The section header index of For a dynamic object, the | |
6688 | [0x6ffffff0] the associated symbol table. section header index of | |
6689 | the associated | |
6690 | SHT_SUNW_capchain table, | |
6691 | otherwise 0. | |
6692 | ||
6693 | SHT_SUNW_symsort The section header index of 0 | |
6694 | [0x6ffffff1] the associated symbol table. | |
6695 | ||
6696 | SHT_SUNW_tlssort The section header index of 0 | |
6697 | [0x6ffffff2] the associated symbol table. | |
6698 | ||
6699 | SHT_SUNW_LDYNSYM The section header index of One greater than the | |
6700 | [0x6ffffff3] the associated string table. symbol table index of the | |
6701 | This index is the same string last local symbol, | |
6702 | table used by the SHT_DYNSYM STB_LOCAL. Since | |
6703 | section. SHT_SUNW_LDYNSYM only | |
6704 | contains local symbols, | |
6705 | sh_info is equivalent to | |
6706 | the number of symbols in | |
6707 | the table. | |
6708 | ||
6709 | SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer | |
6710 | [0x6ffffff5] the section header index of to named strings, the | |
6711 | the associated section header index of | |
6712 | SHT_SUNW_capinfo table, the associated string | |
6713 | otherwise 0. table, otherwise 0. | |
6714 | ||
6715 | SHT_SUNW_move The section header index of 0 | |
6716 | [0x6ffffffa] the associated symbol table. | |
6717 | ||
6718 | SHT_SUNW_COMDAT 0 0 | |
6719 | [0x6ffffffb] | |
6720 | ||
6721 | SHT_SUNW_syminfo The section header index of The section header index | |
6722 | [0x6ffffffc] the associated symbol table. of the associated | |
6723 | .dynamic section. | |
6724 | ||
6725 | SHT_SUNW_verdef The section header index of The number of version | |
6726 | [0x6ffffffd] the associated string table. definitions within the | |
6727 | section. | |
6728 | ||
6729 | SHT_SUNW_verneed The section header index of The number of version | |
6730 | [0x6ffffffe] the associated string table. dependencies within the | |
6731 | section. | |
6732 | ||
6733 | SHT_SUNW_versym The section header index of 0 | |
6734 | [0x6fffffff] the associated symbol table. */ | |
84865015 NC |
6735 | } |
6736 | ||
5522f910 NC |
6737 | #undef elf_backend_copy_special_section_fields |
6738 | #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields | |
84865015 | 6739 | |
a6cc6b3b RO |
6740 | #include "elf32-target.h" |
6741 | ||
bf64a951 L |
6742 | /* Intel MCU support. */ |
6743 | ||
6744 | static bfd_boolean | |
6745 | elf32_iamcu_elf_object_p (bfd *abfd) | |
6746 | { | |
6747 | /* Set the right machine number for an IAMCU elf32 file. */ | |
6748 | bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu); | |
6749 | return TRUE; | |
6750 | } | |
6751 | ||
6752 | #undef TARGET_LITTLE_SYM | |
6753 | #define TARGET_LITTLE_SYM iamcu_elf32_vec | |
6754 | #undef TARGET_LITTLE_NAME | |
6755 | #define TARGET_LITTLE_NAME "elf32-iamcu" | |
84865015 | 6756 | #undef ELF_ARCH |
bf64a951 L |
6757 | #define ELF_ARCH bfd_arch_iamcu |
6758 | ||
6759 | #undef ELF_MACHINE_CODE | |
6760 | #define ELF_MACHINE_CODE EM_IAMCU | |
6761 | ||
6762 | #undef ELF_OSABI | |
6763 | ||
6764 | #undef elf32_bed | |
6765 | #define elf32_bed elf32_iamcu_bed | |
6766 | ||
6767 | #undef elf_backend_object_p | |
6768 | #define elf_backend_object_p elf32_iamcu_elf_object_p | |
6769 | ||
6770 | #undef elf_backend_static_tls_alignment | |
6771 | ||
6772 | #undef elf_backend_want_plt_sym | |
6773 | #define elf_backend_want_plt_sym 0 | |
6774 | ||
84865015 | 6775 | #undef elf_backend_strtab_flags |
5522f910 | 6776 | #undef elf_backend_copy_special_section_fields |
84865015 | 6777 | |
bf64a951 L |
6778 | #include "elf32-target.h" |
6779 | ||
6780 | /* Restore defaults. */ | |
6781 | #undef ELF_ARCH | |
6782 | #define ELF_ARCH bfd_arch_i386 | |
6783 | #undef ELF_MACHINE_CODE | |
6784 | #define ELF_MACHINE_CODE EM_386 | |
6785 | ||
a27e4371 RM |
6786 | /* Native Client support. */ |
6787 | ||
6788 | #undef TARGET_LITTLE_SYM | |
6d00b590 | 6789 | #define TARGET_LITTLE_SYM i386_elf32_nacl_vec |
a27e4371 RM |
6790 | #undef TARGET_LITTLE_NAME |
6791 | #define TARGET_LITTLE_NAME "elf32-i386-nacl" | |
6792 | #undef elf32_bed | |
6793 | #define elf32_bed elf32_i386_nacl_bed | |
6794 | ||
6795 | #undef ELF_MAXPAGESIZE | |
6796 | #define ELF_MAXPAGESIZE 0x10000 | |
6797 | ||
6798 | /* Restore defaults. */ | |
6799 | #undef ELF_OSABI | |
6800 | #undef elf_backend_want_plt_sym | |
6801 | #define elf_backend_want_plt_sym 0 | |
6802 | #undef elf_backend_post_process_headers | |
a27e4371 RM |
6803 | #undef elf_backend_static_tls_alignment |
6804 | ||
6805 | /* NaCl uses substantially different PLT entries for the same effects. */ | |
6806 | ||
6807 | #undef elf_backend_plt_alignment | |
6808 | #define elf_backend_plt_alignment 5 | |
6809 | #define NACL_PLT_ENTRY_SIZE 64 | |
6810 | #define NACLMASK 0xe0 /* 32-byte alignment mask. */ | |
6811 | ||
6812 | static const bfd_byte elf_i386_nacl_plt0_entry[] = | |
6813 | { | |
6814 | 0xff, 0x35, /* pushl contents of address */ | |
6815 | 0, 0, 0, 0, /* replaced with address of .got + 4. */ | |
6816 | 0x8b, 0x0d, /* movl contents of address, %ecx */ | |
6817 | 0, 0, 0, 0, /* replaced with address of .got + 8. */ | |
6818 | 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */ | |
6819 | 0xff, 0xe1 /* jmp *%ecx */ | |
6820 | }; | |
6821 | ||
6822 | static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] = | |
6823 | { | |
6824 | 0x8b, 0x0d, /* movl contents of address, %ecx */ | |
6825 | 0, 0, 0, 0, /* replaced with GOT slot address. */ | |
6826 | 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */ | |
6827 | 0xff, 0xe1, /* jmp *%ecx */ | |
6828 | ||
6829 | /* Pad to the next 32-byte boundary with nop instructions. */ | |
6830 | 0x90, | |
6831 | 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, | |
6832 | 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, | |
6833 | ||
6834 | /* Lazy GOT entries point here (32-byte aligned). */ | |
6835 | 0x68, /* pushl immediate */ | |
6836 | 0, 0, 0, 0, /* replaced with reloc offset. */ | |
6837 | 0xe9, /* jmp relative */ | |
6838 | 0, 0, 0, 0, /* replaced with offset to .plt. */ | |
6839 | ||
6840 | /* Pad to the next 32-byte boundary with nop instructions. */ | |
6841 | 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, | |
6842 | 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, | |
6843 | 0x90, 0x90 | |
6844 | }; | |
6845 | ||
6846 | static const bfd_byte | |
6847 | elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] = | |
6848 | { | |
6849 | 0xff, 0x73, 0x04, /* pushl 4(%ebx) */ | |
6850 | 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */ | |
6851 | 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */ | |
6852 | 0xff, 0xe1, /* jmp *%ecx */ | |
caa0075c RM |
6853 | |
6854 | /* This is expected to be the same size as elf_i386_nacl_plt0_entry, | |
6855 | so pad to that size with nop instructions. */ | |
6856 | 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 | |
a27e4371 RM |
6857 | }; |
6858 | ||
6859 | static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] = | |
6860 | { | |
6861 | 0x8b, 0x8b, /* movl offset(%ebx), %ecx */ | |
6862 | 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */ | |
6863 | 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */ | |
6864 | 0xff, 0xe1, /* jmp *%ecx */ | |
6865 | ||
6866 | /* Pad to the next 32-byte boundary with nop instructions. */ | |
6867 | 0x90, | |
6868 | 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, | |
6869 | 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, | |
6870 | ||
6871 | /* Lazy GOT entries point here (32-byte aligned). */ | |
6872 | 0x68, /* pushl immediate */ | |
6873 | 0, 0, 0, 0, /* replaced with offset into relocation table. */ | |
6874 | 0xe9, /* jmp relative */ | |
6875 | 0, 0, 0, 0, /* replaced with offset to start of .plt. */ | |
6876 | ||
6877 | /* Pad to the next 32-byte boundary with nop instructions. */ | |
6878 | 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, | |
6879 | 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, | |
6880 | 0x90, 0x90 | |
6881 | }; | |
6882 | ||
6883 | static const bfd_byte elf_i386_nacl_eh_frame_plt[] = | |
6884 | { | |
6885 | #if (PLT_CIE_LENGTH != 20 \ | |
6886 | || PLT_FDE_LENGTH != 36 \ | |
6887 | || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \ | |
6888 | || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12) | |
6889 | # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!" | |
6890 | #endif | |
6891 | PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */ | |
6892 | 0, 0, 0, 0, /* CIE ID */ | |
6893 | 1, /* CIE version */ | |
6894 | 'z', 'R', 0, /* Augmentation string */ | |
6895 | 1, /* Code alignment factor */ | |
6896 | 0x7c, /* Data alignment factor: -4 */ | |
6897 | 8, /* Return address column */ | |
6898 | 1, /* Augmentation size */ | |
6899 | DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */ | |
6900 | DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */ | |
6901 | DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */ | |
6902 | DW_CFA_nop, DW_CFA_nop, | |
6903 | ||
6904 | PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */ | |
6905 | PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */ | |
6906 | 0, 0, 0, 0, /* R_386_PC32 .plt goes here */ | |
6907 | 0, 0, 0, 0, /* .plt size goes here */ | |
6908 | 0, /* Augmentation size */ | |
6909 | DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */ | |
6910 | DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */ | |
6911 | DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */ | |
6912 | DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */ | |
6913 | DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */ | |
6914 | 13, /* Block length */ | |
6915 | DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */ | |
6916 | DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */ | |
6917 | DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge, | |
6918 | DW_OP_lit2, DW_OP_shl, DW_OP_plus, | |
6919 | DW_CFA_nop, DW_CFA_nop | |
6920 | }; | |
6921 | ||
f604c2a2 | 6922 | static const struct elf_i386_lazy_plt_layout elf_i386_nacl_plt = |
a27e4371 RM |
6923 | { |
6924 | elf_i386_nacl_plt0_entry, /* plt0_entry */ | |
6925 | sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */ | |
6926 | 2, /* plt0_got1_offset */ | |
6927 | 8, /* plt0_got2_offset */ | |
6928 | elf_i386_nacl_plt_entry, /* plt_entry */ | |
6929 | NACL_PLT_ENTRY_SIZE, /* plt_entry_size */ | |
6930 | 2, /* plt_got_offset */ | |
6931 | 33, /* plt_reloc_offset */ | |
6932 | 38, /* plt_plt_offset */ | |
6933 | 32, /* plt_lazy_offset */ | |
6934 | elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */ | |
6935 | elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */ | |
6936 | elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */ | |
f604c2a2 | 6937 | sizeof (elf_i386_nacl_eh_frame_plt) /* eh_frame_plt_size */ |
a27e4371 RM |
6938 | }; |
6939 | ||
6940 | static const struct elf_i386_backend_data elf_i386_nacl_arch_bed = | |
6941 | { | |
f604c2a2 L |
6942 | 0x90, /* plt0_pad_byte: nop insn */ |
6943 | is_nacl /* os */ | |
a27e4371 RM |
6944 | }; |
6945 | ||
64b384e1 RM |
6946 | static bfd_boolean |
6947 | elf32_i386_nacl_elf_object_p (bfd *abfd) | |
6948 | { | |
6949 | /* Set the right machine number for a NaCl i386 ELF32 file. */ | |
6950 | bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl); | |
6951 | return TRUE; | |
6952 | } | |
6953 | ||
a27e4371 RM |
6954 | #undef elf_backend_arch_data |
6955 | #define elf_backend_arch_data &elf_i386_nacl_arch_bed | |
6956 | ||
64b384e1 RM |
6957 | #undef elf_backend_object_p |
6958 | #define elf_backend_object_p elf32_i386_nacl_elf_object_p | |
5a68afcf RM |
6959 | #undef elf_backend_modify_segment_map |
6960 | #define elf_backend_modify_segment_map nacl_modify_segment_map | |
6961 | #undef elf_backend_modify_program_headers | |
6962 | #define elf_backend_modify_program_headers nacl_modify_program_headers | |
887badb3 RM |
6963 | #undef elf_backend_final_write_processing |
6964 | #define elf_backend_final_write_processing nacl_final_write_processing | |
5a68afcf | 6965 | |
a27e4371 RM |
6966 | #include "elf32-target.h" |
6967 | ||
5a68afcf | 6968 | /* Restore defaults. */ |
64b384e1 | 6969 | #undef elf_backend_object_p |
5a68afcf RM |
6970 | #undef elf_backend_modify_segment_map |
6971 | #undef elf_backend_modify_program_headers | |
887badb3 | 6972 | #undef elf_backend_final_write_processing |
5a68afcf | 6973 | |
eac338cf PB |
6974 | /* VxWorks support. */ |
6975 | ||
6976 | #undef TARGET_LITTLE_SYM | |
6d00b590 | 6977 | #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec |
eac338cf PB |
6978 | #undef TARGET_LITTLE_NAME |
6979 | #define TARGET_LITTLE_NAME "elf32-i386-vxworks" | |
d1036acb | 6980 | #undef ELF_OSABI |
1587442d L |
6981 | #undef ELF_MAXPAGESIZE |
6982 | #define ELF_MAXPAGESIZE 0x1000 | |
a27e4371 RM |
6983 | #undef elf_backend_plt_alignment |
6984 | #define elf_backend_plt_alignment 4 | |
eac338cf | 6985 | |
23209a78 RM |
6986 | static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed = |
6987 | { | |
6988 | 0x90, /* plt0_pad_byte */ | |
f604c2a2 | 6989 | is_vxworks /* os */ |
23209a78 | 6990 | }; |
eac338cf | 6991 | |
23209a78 RM |
6992 | #undef elf_backend_arch_data |
6993 | #define elf_backend_arch_data &elf_i386_vxworks_arch_bed | |
eac338cf | 6994 | |
13285a1b | 6995 | #undef elf_backend_relocs_compatible |
eac338cf PB |
6996 | #undef elf_backend_add_symbol_hook |
6997 | #define elf_backend_add_symbol_hook \ | |
6998 | elf_vxworks_add_symbol_hook | |
6999 | #undef elf_backend_link_output_symbol_hook | |
7000 | #define elf_backend_link_output_symbol_hook \ | |
9c72ff84 | 7001 | elf_vxworks_link_output_symbol_hook |
eac338cf PB |
7002 | #undef elf_backend_emit_relocs |
7003 | #define elf_backend_emit_relocs elf_vxworks_emit_relocs | |
7004 | #undef elf_backend_final_write_processing | |
7005 | #define elf_backend_final_write_processing \ | |
7006 | elf_vxworks_final_write_processing | |
7dc98aea | 7007 | #undef elf_backend_static_tls_alignment |
eac338cf PB |
7008 | |
7009 | /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so | |
7010 | define it. */ | |
7011 | #undef elf_backend_want_plt_sym | |
7012 | #define elf_backend_want_plt_sym 1 | |
7013 | ||
7014 | #undef elf32_bed | |
7015 | #define elf32_bed elf32_i386_vxworks_bed | |
7016 | ||
7017 | #include "elf32-target.h" |