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