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