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