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