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