1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright (C) 1993-2017 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 #include "elf-vxworks.h"
28 #include "bfd_stdint.h"
32 #include "opcode/i386.h"
34 /* 386 uses REL relocations instead of RELA. */
39 static reloc_howto_type elf_howto_table[]=
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),
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)
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),
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",
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),
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)
158 /* GNU extension to record C++ vtable hierarchy. */
159 HOWTO (R_386_GNU_VTINHERIT, /* type */
161 2, /* size (0 = byte, 1 = short, 2 = long) */
163 FALSE, /* pc_relative */
165 complain_overflow_dont, /* complain_on_overflow */
166 NULL, /* special_function */
167 "R_386_GNU_VTINHERIT", /* name */
168 FALSE, /* partial_inplace */
171 FALSE), /* pcrel_offset */
173 /* GNU extension to record C++ vtable member usage. */
174 HOWTO (R_386_GNU_VTENTRY, /* type */
176 2, /* size (0 = byte, 1 = short, 2 = long) */
178 FALSE, /* pc_relative */
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 */
186 FALSE) /* pcrel_offset */
188 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
192 #ifdef DEBUG_GEN_RELOC
194 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
199 static reloc_howto_type *
200 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
201 bfd_reloc_code_real_type code)
206 TRACE ("BFD_RELOC_NONE");
207 return &elf_howto_table[R_386_NONE];
210 TRACE ("BFD_RELOC_32");
211 return &elf_howto_table[R_386_32];
214 TRACE ("BFD_RELOC_CTOR");
215 return &elf_howto_table[R_386_32];
217 case BFD_RELOC_32_PCREL:
218 TRACE ("BFD_RELOC_PC32");
219 return &elf_howto_table[R_386_PC32];
221 case BFD_RELOC_386_GOT32:
222 TRACE ("BFD_RELOC_386_GOT32");
223 return &elf_howto_table[R_386_GOT32];
225 case BFD_RELOC_386_PLT32:
226 TRACE ("BFD_RELOC_386_PLT32");
227 return &elf_howto_table[R_386_PLT32];
229 case BFD_RELOC_386_COPY:
230 TRACE ("BFD_RELOC_386_COPY");
231 return &elf_howto_table[R_386_COPY];
233 case BFD_RELOC_386_GLOB_DAT:
234 TRACE ("BFD_RELOC_386_GLOB_DAT");
235 return &elf_howto_table[R_386_GLOB_DAT];
237 case BFD_RELOC_386_JUMP_SLOT:
238 TRACE ("BFD_RELOC_386_JUMP_SLOT");
239 return &elf_howto_table[R_386_JUMP_SLOT];
241 case BFD_RELOC_386_RELATIVE:
242 TRACE ("BFD_RELOC_386_RELATIVE");
243 return &elf_howto_table[R_386_RELATIVE];
245 case BFD_RELOC_386_GOTOFF:
246 TRACE ("BFD_RELOC_386_GOTOFF");
247 return &elf_howto_table[R_386_GOTOFF];
249 case BFD_RELOC_386_GOTPC:
250 TRACE ("BFD_RELOC_386_GOTPC");
251 return &elf_howto_table[R_386_GOTPC];
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];
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];
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];
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];
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];
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];
279 TRACE ("BFD_RELOC_16");
280 return &elf_howto_table[R_386_16 - R_386_ext_offset];
282 case BFD_RELOC_16_PCREL:
283 TRACE ("BFD_RELOC_16_PCREL");
284 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
287 TRACE ("BFD_RELOC_8");
288 return &elf_howto_table[R_386_8 - R_386_ext_offset];
290 case BFD_RELOC_8_PCREL:
291 TRACE ("BFD_RELOC_8_PCREL");
292 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
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];
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];
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];
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];
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];
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];
319 case BFD_RELOC_SIZE32:
320 TRACE ("BFD_RELOC_SIZE32");
321 return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
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];
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];
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];
335 case BFD_RELOC_386_IRELATIVE:
336 TRACE ("BFD_RELOC_386_IRELATIVE");
337 return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
339 case BFD_RELOC_386_GOT32X:
340 TRACE ("BFD_RELOC_386_GOT32X");
341 return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
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];
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];
359 static reloc_howto_type *
360 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
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];
373 static reloc_howto_type *
374 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
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))
386 /* xgettext:c-format */
387 _bfd_error_handler (_("%B: invalid relocation type %d"),
391 /* PR 17512: file: 0f67f69d. */
392 if (elf_howto_table [indx].type != r_type)
394 return &elf_howto_table[indx];
398 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
400 Elf_Internal_Rela *dst)
402 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
403 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
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. */
413 elf_i386_is_local_label_name (bfd *abfd, const char *name)
415 if (name[0] == '.' && name[1] == 'X')
418 return _bfd_elf_is_local_label_name (abfd, name);
421 /* Support for core dump NOTE sections. */
424 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
429 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
431 int pr_version = bfd_get_32 (abfd, note->descdata);
437 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
440 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
444 size = bfd_get_32 (abfd, note->descdata + 8);
448 switch (note->descsz)
453 case 144: /* Linux/i386 */
455 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
458 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
468 /* Make a ".reg/999" section. */
469 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
470 size, note->descpos + offset);
474 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
476 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
478 int pr_version = bfd_get_32 (abfd, note->descdata);
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);
490 switch (note->descsz)
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);
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. */
509 char *command = elf_tdata (abfd)->core->command;
510 int n = strlen (command);
512 if (0 < n && command[n - 1] == ' ')
513 command[n - 1] = '\0';
519 /* Functions for the i386 ELF linker.
521 In order to gain some understanding of code in this file without
522 knowing all the intricate details of the linker, note the
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. */
533 /* The name of the dynamic interpreter. This is put in the .interp
536 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
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
542 #define ELIMINATE_COPY_RELOCS 1
544 /* The size in bytes of an entry in the procedure linkage table. */
546 #define PLT_ENTRY_SIZE 16
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. */
552 static const bfd_byte elf_i386_plt0_entry[12] =
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. */
560 /* Subsequent entries in an absolute procedure linkage table look like
563 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
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. */
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. */
576 static const bfd_byte elf_i386_pic_plt0_entry[12] =
578 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
579 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
582 /* Subsequent entries in a PIC procedure linkage table look like this. */
584 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
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. */
594 /* Entries in the GOT procedure linkage table look like this. */
596 static const bfd_byte elf_i386_got_plt_entry[8] =
598 0xff, 0x25, /* jmp indirect */
599 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
600 0x66, 0x90 /* xchg %ax,%ax */
603 /* Entries in the PIC GOT procedure linkage table look like this. */
605 static const bfd_byte elf_i386_pic_got_plt_entry[8] =
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 */
612 /* .eh_frame covering the .plt section. */
614 static const bfd_byte elf_i386_eh_frame_plt[] =
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 */
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,
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
651 /* .eh_frame covering the .plt.got section. */
653 static const bfd_byte elf_i386_eh_frame_plt_got[] =
655 #define PLT_GOT_FDE_LENGTH 16
656 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
657 0, 0, 0, 0, /* CIE ID */
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,
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
677 struct elf_i386_plt_layout
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;
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;
687 /* Later entries in an absolute procedure linkage table look like this. */
688 const bfd_byte *plt_entry;
689 unsigned int plt_entry_size;
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. */
696 /* Offset into plt_entry where the initial value of the GOT entry points. */
697 unsigned int plt_lazy_offset;
699 /* The first entry in a PIC procedure linkage table looks like this. */
700 const bfd_byte *pic_plt0_entry;
702 /* Subsequent entries in a PIC procedure linkage table look like this. */
703 const bfd_byte *pic_plt_entry;
705 /* .eh_frame covering the .plt section. */
706 const bfd_byte *eh_frame_plt;
707 unsigned int eh_frame_plt_size;
709 /* .eh_frame covering the .plt.got section. */
710 const bfd_byte *eh_frame_plt_got;
711 unsigned int eh_frame_plt_got_size;
714 #define GET_PLT_ENTRY_SIZE(abfd) \
715 get_elf_i386_backend_data (abfd)->plt->plt_entry_size
717 /* These are the standard parameters. */
718 static const struct elf_i386_plt_layout elf_i386_plt =
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 */
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
745 /* Architecture-specific backend data for i386. */
747 struct elf_i386_backend_data
749 /* Parameters describing PLT generation. */
750 const struct elf_i386_plt_layout *plt;
752 /* Value used to fill the unused bytes of the first PLT entry. */
753 bfd_byte plt0_pad_byte;
755 /* True if the target system is VxWorks. */
759 #define get_elf_i386_backend_data(abfd) \
760 ((const struct elf_i386_backend_data *) \
761 get_elf_backend_data (abfd)->arch_data)
763 /* These are the standard parameters. */
764 static const struct elf_i386_backend_data elf_i386_arch_bed =
766 &elf_i386_plt, /* plt */
767 0, /* plt0_pad_byte */
771 #define elf_backend_arch_data &elf_i386_arch_bed
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
776 1. Has non-GOT/non-PLT relocations in text section. Or
777 2. Has no GOT/PLT relocation.
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 \
784 || (EH)->has_non_got_reloc \
785 || !(INFO)->dynamic_undefined_weak))
787 /* i386 ELF linker hash entry. */
789 struct elf_i386_link_hash_entry
791 struct elf_link_hash_entry elf;
793 /* Track dynamic relocs copied for this symbol. */
794 struct elf_dyn_relocs *dyn_relocs;
796 #define GOT_UNKNOWN 0
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;
814 /* Symbol is referenced by R_386_GOTOFF relocation. */
815 unsigned int gotoff_ref : 1;
817 /* Symbol has GOT or PLT relocations. */
818 unsigned int has_got_reloc : 1;
820 /* Symbol has non-GOT/non-PLT relocations in text sections. */
821 unsigned int has_non_got_reloc : 1;
823 /* Don't call finish_dynamic_symbol on this symbol. */
824 unsigned int no_finish_dynamic_symbol : 1;
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;
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;
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;
839 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
840 starting at the end of the jump table. */
844 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
846 struct elf_i386_obj_tdata
848 struct elf_obj_tdata root;
850 /* tls_type for each local got entry. */
851 char *local_got_tls_type;
853 /* GOTPLT entries for TLS descriptors. */
854 bfd_vma *local_tlsdesc_gotent;
857 #define elf_i386_tdata(abfd) \
858 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
860 #define elf_i386_local_got_tls_type(abfd) \
861 (elf_i386_tdata (abfd)->local_got_tls_type)
863 #define elf_i386_local_tlsdesc_gotent(abfd) \
864 (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
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)
872 elf_i386_mkobject (bfd *abfd)
874 return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
878 /* i386 ELF linker hash table. */
880 struct elf_i386_link_hash_table
882 struct elf_link_hash_table elf;
884 /* Short-cuts to get to dynamic linker sections. */
886 asection *plt_eh_frame;
888 asection *plt_got_eh_frame;
892 bfd_signed_vma refcount;
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;
900 /* Small local sym cache. */
901 struct sym_cache sym_cache;
903 /* _TLS_MODULE_BASE_ symbol. */
904 struct bfd_link_hash_entry *tls_module_base;
906 /* Used by local STT_GNU_IFUNC symbols. */
907 htab_t loc_hash_table;
908 void * loc_hash_memory;
910 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
913 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
914 bfd_vma next_tls_desc_index;
916 /* The index of the next unused R_386_JUMP_SLOT slot in .rel.plt. */
917 bfd_vma next_jump_slot_index;
919 /* The index of the next unused R_386_IRELATIVE slot in .rel.plt. */
920 bfd_vma next_irelative_index;
922 /* TRUE if there are dynamic relocs against IFUNC symbols that apply
923 to read-only sections. */
924 bfd_boolean readonly_dynrelocs_against_ifunc;
927 /* Get the i386 ELF linker hash table from a link_info structure. */
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)
933 #define elf_i386_compute_jump_table_size(htab) \
934 ((htab)->elf.srelplt->reloc_count * 4)
936 /* Create an entry in an i386 ELF linker hash table. */
938 static struct bfd_hash_entry *
939 elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
940 struct bfd_hash_table *table,
943 /* Allocate the structure if it has not already been allocated by a
947 entry = (struct bfd_hash_entry *)
948 bfd_hash_allocate (table, sizeof (struct elf_i386_link_hash_entry));
953 /* Call the allocation method of the superclass. */
954 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
957 struct elf_i386_link_hash_entry *eh;
959 eh = (struct elf_i386_link_hash_entry *) entry;
960 eh->dyn_relocs = NULL;
961 eh->tls_type = GOT_UNKNOWN;
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;
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. */
981 elf_i386_local_htab_hash (const void *ptr)
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);
988 /* Compare local hash entries. */
991 elf_i386_local_htab_eq (const void *ptr1, const void *ptr2)
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;
998 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
1001 /* Find and/or create a hash entry for local symbol. */
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,
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));
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);
1024 ret = (struct elf_i386_link_hash_entry *) *slot;
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));
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;
1044 /* Destroy an i386 ELF linker hash table. */
1047 elf_i386_link_hash_table_free (bfd *obfd)
1049 struct elf_i386_link_hash_table *htab
1050 = (struct elf_i386_link_hash_table *) obfd->link.hash;
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);
1059 /* Create an i386 ELF linker hash table. */
1061 static struct bfd_link_hash_table *
1062 elf_i386_link_hash_table_create (bfd *abfd)
1064 struct elf_i386_link_hash_table *ret;
1065 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
1067 ret = (struct elf_i386_link_hash_table *) bfd_zmalloc (amt);
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),
1080 ret->loc_hash_table = htab_try_create (1024,
1081 elf_i386_local_htab_hash,
1082 elf_i386_local_htab_eq,
1084 ret->loc_hash_memory = objalloc_create ();
1085 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1087 elf_i386_link_hash_table_free (abfd);
1090 ret->elf.root.hash_table_free = elf_i386_link_hash_table_free;
1092 return &ret->elf.root;
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
1100 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1102 struct elf_i386_link_hash_table *htab;
1104 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1107 htab = elf_i386_hash_table (info);
1111 /* Set the contents of the .interp section to the interpreter. */
1112 if (bfd_link_executable (info) && !info->nointerp)
1114 asection *s = bfd_get_linker_section (dynobj, ".interp");
1117 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1118 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1122 if (get_elf_i386_backend_data (dynobj)->is_vxworks
1123 && !elf_vxworks_create_dynamic_sections (dynobj, info,
1127 if (!info->no_ld_generated_unwind_info
1128 && htab->plt_eh_frame == NULL
1129 && htab->elf.splt != NULL)
1131 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
1132 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1133 | SEC_LINKER_CREATED);
1135 = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
1136 if (htab->plt_eh_frame == NULL
1137 || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 2))
1144 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1147 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
1148 struct elf_link_hash_entry *dir,
1149 struct elf_link_hash_entry *ind)
1151 struct elf_i386_link_hash_entry *edir, *eind;
1153 edir = (struct elf_i386_link_hash_entry *) dir;
1154 eind = (struct elf_i386_link_hash_entry *) ind;
1156 if (eind->dyn_relocs != NULL)
1158 if (edir->dyn_relocs != NULL)
1160 struct elf_dyn_relocs **pp;
1161 struct elf_dyn_relocs *p;
1163 /* Add reloc counts against the indirect sym to the direct sym
1164 list. Merge any entries against the same section. */
1165 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1167 struct elf_dyn_relocs *q;
1169 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1170 if (q->sec == p->sec)
1172 q->pc_count += p->pc_count;
1173 q->count += p->count;
1180 *pp = edir->dyn_relocs;
1183 edir->dyn_relocs = eind->dyn_relocs;
1184 eind->dyn_relocs = NULL;
1187 if (ind->root.type == bfd_link_hash_indirect
1188 && dir->got.refcount <= 0)
1190 edir->tls_type = eind->tls_type;
1191 eind->tls_type = GOT_UNKNOWN;
1194 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
1195 generate a R_386_COPY reloc. */
1196 edir->gotoff_ref |= eind->gotoff_ref;
1198 edir->has_got_reloc |= eind->has_got_reloc;
1199 edir->has_non_got_reloc |= eind->has_non_got_reloc;
1201 if (ELIMINATE_COPY_RELOCS
1202 && ind->root.type != bfd_link_hash_indirect
1203 && dir->dynamic_adjusted)
1205 /* If called to transfer flags for a weakdef during processing
1206 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1207 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1208 if (dir->versioned != versioned_hidden)
1209 dir->ref_dynamic |= ind->ref_dynamic;
1210 dir->ref_regular |= ind->ref_regular;
1211 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1212 dir->needs_plt |= ind->needs_plt;
1213 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1217 if (eind->func_pointer_refcount > 0)
1219 edir->func_pointer_refcount += eind->func_pointer_refcount;
1220 eind->func_pointer_refcount = 0;
1223 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1227 /* Return TRUE if the TLS access code sequence support transition
1231 elf_i386_check_tls_transition (asection *sec,
1233 Elf_Internal_Shdr *symtab_hdr,
1234 struct elf_link_hash_entry **sym_hashes,
1235 unsigned int r_type,
1236 const Elf_Internal_Rela *rel,
1237 const Elf_Internal_Rela *relend)
1239 unsigned int val, type, reg;
1240 unsigned long r_symndx;
1241 struct elf_link_hash_entry *h;
1244 bfd_boolean indirect_call, tls_get_addr;
1246 offset = rel->r_offset;
1251 if (offset < 2 || (rel + 1) >= relend)
1254 indirect_call = FALSE;
1255 call = contents + offset + 4;
1258 if (r_type == R_386_TLS_GD)
1260 /* Check transition from GD access model. Only
1261 leal foo@tlsgd(,%ebx,1), %eax
1262 call ___tls_get_addr@PLT
1264 leal foo@tlsgd(%ebx) %eax
1265 call ___tls_get_addr@PLT
1268 leal foo@tlsgd(%reg), %eax
1269 call *___tls_get_addr@GOT(%reg)
1270 which may be converted to
1271 addr32 call ___tls_get_addr
1272 can transit to different access model. */
1273 if ((offset + 10) > sec->size
1274 || (type != 0x8d && type != 0x04))
1279 /* leal foo@tlsgd(,%ebx,1), %eax
1280 call ___tls_get_addr@PLT */
1284 if (*(call - 7) != 0x8d
1292 leal foo@tlsgd(%ebx), %eax
1293 call ___tls_get_addr@PLT
1296 leal foo@tlsgd(%reg), %eax
1297 call *___tls_get_addr@GOT(%reg)
1298 which may be converted to
1299 addr32 call ___tls_get_addr
1301 %eax can't be used as the GOT base register since it
1302 is used to pass parameter to ___tls_get_addr. */
1304 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
1307 indirect_call = call[0] == 0xff;
1308 if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
1309 && !(call[0] == 0x67 && call[1] == 0xe8)
1311 && (call[1] & 0xf8) == 0x90
1312 && (call[1] & 0x7) == reg))
1318 /* Check transition from LD access model. Only
1319 leal foo@tlsldm(%ebx), %eax
1320 call ___tls_get_addr@PLT
1322 leal foo@tlsldm(%reg), %eax
1323 call *___tls_get_addr@GOT(%reg)
1324 which may be converted to
1325 addr32 call ___tls_get_addr
1326 can transit to different access model. */
1327 if (type != 0x8d || (offset + 9) > sec->size)
1330 /* %eax can't be used as the GOT base register since it is
1331 used to pass parameter to ___tls_get_addr. */
1333 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
1336 indirect_call = call[0] == 0xff;
1337 if (!(reg == 3 && call[0] == 0xe8)
1338 && !(call[0] == 0x67 && call[1] == 0xe8)
1340 && (call[1] & 0xf8) == 0x90
1341 && (call[1] & 0x7) == reg))
1345 r_symndx = ELF32_R_SYM (rel[1].r_info);
1346 if (r_symndx < symtab_hdr->sh_info)
1349 tls_get_addr = FALSE;
1350 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1351 if (h != NULL && h->root.root.string != NULL)
1353 struct elf_i386_link_hash_entry *eh
1354 = (struct elf_i386_link_hash_entry *) h;
1355 tls_get_addr = eh->tls_get_addr == 1;
1356 if (eh->tls_get_addr > 1)
1358 /* Use strncmp to check ___tls_get_addr since
1359 ___tls_get_addr may be versioned. */
1360 if (strncmp (h->root.root.string, "___tls_get_addr", 15)
1363 eh->tls_get_addr = 1;
1364 tls_get_addr = TRUE;
1367 eh->tls_get_addr = 0;
1373 else if (indirect_call)
1374 return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X);
1376 return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1377 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
1380 /* Check transition from IE access model:
1381 movl foo@indntpoff(%rip), %eax
1382 movl foo@indntpoff(%rip), %reg
1383 addl foo@indntpoff(%rip), %reg
1386 if (offset < 1 || (offset + 4) > sec->size)
1389 /* Check "movl foo@tpoff(%rip), %eax" first. */
1390 val = bfd_get_8 (abfd, contents + offset - 1);
1397 /* Check movl|addl foo@tpoff(%rip), %reg. */
1398 type = bfd_get_8 (abfd, contents + offset - 2);
1399 return ((type == 0x8b || type == 0x03)
1400 && (val & 0xc7) == 0x05);
1402 case R_386_TLS_GOTIE:
1403 case R_386_TLS_IE_32:
1404 /* Check transition from {IE_32,GOTIE} access model:
1405 subl foo@{tpoff,gontoff}(%reg1), %reg2
1406 movl foo@{tpoff,gontoff}(%reg1), %reg2
1407 addl foo@{tpoff,gontoff}(%reg1), %reg2
1410 if (offset < 2 || (offset + 4) > sec->size)
1413 val = bfd_get_8 (abfd, contents + offset - 1);
1414 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1417 type = bfd_get_8 (abfd, contents + offset - 2);
1418 return type == 0x8b || type == 0x2b || type == 0x03;
1420 case R_386_TLS_GOTDESC:
1421 /* Check transition from GDesc access model:
1422 leal x@tlsdesc(%ebx), %eax
1424 Make sure it's a leal adding ebx to a 32-bit offset
1425 into any register, although it's probably almost always
1428 if (offset < 2 || (offset + 4) > sec->size)
1431 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1434 val = bfd_get_8 (abfd, contents + offset - 1);
1435 return (val & 0xc7) == 0x83;
1437 case R_386_TLS_DESC_CALL:
1438 /* Check transition from GDesc access model:
1439 call *x@tlsdesc(%eax)
1441 if (offset + 2 <= sec->size)
1443 /* Make sure that it's a call *x@tlsdesc(%eax). */
1444 call = contents + offset;
1445 return call[0] == 0xff && call[1] == 0x10;
1455 /* Return TRUE if the TLS access transition is OK or no transition
1456 will be performed. Update R_TYPE if there is a transition. */
1459 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1460 asection *sec, bfd_byte *contents,
1461 Elf_Internal_Shdr *symtab_hdr,
1462 struct elf_link_hash_entry **sym_hashes,
1463 unsigned int *r_type, int tls_type,
1464 const Elf_Internal_Rela *rel,
1465 const Elf_Internal_Rela *relend,
1466 struct elf_link_hash_entry *h,
1467 unsigned long r_symndx,
1468 bfd_boolean from_relocate_section)
1470 unsigned int from_type = *r_type;
1471 unsigned int to_type = from_type;
1472 bfd_boolean check = TRUE;
1474 /* Skip TLS transition for functions. */
1476 && (h->type == STT_FUNC
1477 || h->type == STT_GNU_IFUNC))
1483 case R_386_TLS_GOTDESC:
1484 case R_386_TLS_DESC_CALL:
1485 case R_386_TLS_IE_32:
1487 case R_386_TLS_GOTIE:
1488 if (bfd_link_executable (info))
1491 to_type = R_386_TLS_LE_32;
1492 else if (from_type != R_386_TLS_IE
1493 && from_type != R_386_TLS_GOTIE)
1494 to_type = R_386_TLS_IE_32;
1497 /* When we are called from elf_i386_relocate_section, there may
1498 be additional transitions based on TLS_TYPE. */
1499 if (from_relocate_section)
1501 unsigned int new_to_type = to_type;
1503 if (bfd_link_executable (info)
1506 && (tls_type & GOT_TLS_IE))
1507 new_to_type = R_386_TLS_LE_32;
1509 if (to_type == R_386_TLS_GD
1510 || to_type == R_386_TLS_GOTDESC
1511 || to_type == R_386_TLS_DESC_CALL)
1513 if (tls_type == GOT_TLS_IE_POS)
1514 new_to_type = R_386_TLS_GOTIE;
1515 else if (tls_type & GOT_TLS_IE)
1516 new_to_type = R_386_TLS_IE_32;
1519 /* We checked the transition before when we were called from
1520 elf_i386_check_relocs. We only want to check the new
1521 transition which hasn't been checked before. */
1522 check = new_to_type != to_type && from_type == to_type;
1523 to_type = new_to_type;
1529 if (bfd_link_executable (info))
1530 to_type = R_386_TLS_LE_32;
1537 /* Return TRUE if there is no transition. */
1538 if (from_type == to_type)
1541 /* Check if the transition can be performed. */
1543 && ! elf_i386_check_tls_transition (sec, contents,
1544 symtab_hdr, sym_hashes,
1545 from_type, rel, relend))
1547 reloc_howto_type *from, *to;
1550 from = elf_i386_rtype_to_howto (abfd, from_type);
1551 to = elf_i386_rtype_to_howto (abfd, to_type);
1554 name = h->root.root.string;
1557 struct elf_i386_link_hash_table *htab;
1559 htab = elf_i386_hash_table (info);
1564 Elf_Internal_Sym *isym;
1566 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1568 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1573 /* xgettext:c-format */
1574 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1575 "in section `%A' failed"),
1576 abfd, from->name, to->name, name,
1577 (unsigned long) rel->r_offset, sec);
1578 bfd_set_error (bfd_error_bad_value);
1586 /* With the local symbol, foo, we convert
1587 mov foo@GOT[(%reg1)], %reg2
1589 lea foo[@GOTOFF(%reg1)], %reg2
1591 call/jmp *foo@GOT[(%reg)]
1593 nop call foo/jmp foo nop
1594 When PIC is false, convert
1595 test %reg1, foo@GOT[(%reg2)]
1599 binop foo@GOT[(%reg1)], %reg2
1602 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1607 elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1609 Elf_Internal_Rela *irel,
1610 struct elf_link_hash_entry *h,
1611 bfd_boolean *converted,
1612 struct bfd_link_info *link_info)
1614 struct elf_i386_link_hash_table *htab;
1615 unsigned int opcode;
1617 bfd_boolean baseless;
1618 Elf_Internal_Sym *isym;
1619 unsigned int addend;
1623 bfd_boolean to_reloc_32;
1624 unsigned int r_type;
1625 unsigned int r_symndx;
1626 bfd_vma roff = irel->r_offset;
1631 /* Addend for R_386_GOT32X relocations must be 0. */
1632 addend = bfd_get_32 (abfd, contents + roff);
1636 htab = elf_i386_hash_table (link_info);
1637 is_pic = bfd_link_pic (link_info);
1639 r_type = ELF32_R_TYPE (irel->r_info);
1640 r_symndx = ELF32_R_SYM (irel->r_info);
1642 modrm = bfd_get_8 (abfd, contents + roff - 1);
1643 baseless = (modrm & 0xc7) == 0x5;
1645 if (baseless && is_pic)
1647 /* For PIC, disallow R_386_GOT32X without a base register
1648 since we don't know what the GOT base is. */
1653 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd,
1655 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1658 name = h->root.root.string;
1661 /* xgettext:c-format */
1662 (_("%B: direct GOT relocation R_386_GOT32X against `%s' without base"
1663 " register can not be used when making a shared object"),
1668 opcode = bfd_get_8 (abfd, contents + roff - 2);
1670 /* Convert to R_386_32 if PIC is false or there is no base
1672 to_reloc_32 = !is_pic || baseless;
1674 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1678 if (opcode == 0x0ff)
1679 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1680 goto convert_branch;
1682 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1683 "test %reg1, foo@GOT(%reg2)" and
1684 "binop foo@GOT[(%reg1)], %reg2". */
1688 /* Undefined weak symbol is only bound locally in executable
1689 and its reference is resolved as 0. */
1690 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info, TRUE,
1691 elf_i386_hash_entry (h)))
1695 /* No direct branch to 0 for PIC. */
1699 goto convert_branch;
1703 /* We can convert load of address 0 to R_386_32. */
1711 /* We have "call/jmp *foo@GOT[(%reg)]". */
1712 if ((h->root.type == bfd_link_hash_defined
1713 || h->root.type == bfd_link_hash_defweak)
1714 && SYMBOL_REFERENCES_LOCAL (link_info, h))
1716 /* The function is locally defined. */
1718 /* Convert R_386_GOT32X to R_386_PC32. */
1719 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1721 struct elf_i386_link_hash_entry *eh
1722 = (struct elf_i386_link_hash_entry *) h;
1724 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1727 /* To support TLS optimization, always use addr32 prefix
1728 for "call *___tls_get_addr@GOT(%reg)". */
1729 if (eh && eh->tls_get_addr == 1)
1732 nop_offset = irel->r_offset - 2;
1736 nop = link_info->call_nop_byte;
1737 if (link_info->call_nop_as_suffix)
1739 nop_offset = roff + 3;
1740 irel->r_offset -= 1;
1743 nop_offset = roff - 2;
1748 /* Convert to "jmp foo nop". */
1751 nop_offset = roff + 3;
1752 irel->r_offset -= 1;
1755 bfd_put_8 (abfd, nop, contents + nop_offset);
1756 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1757 /* When converting to PC-relative relocation, we
1758 need to adjust addend by -4. */
1759 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1760 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1767 /* We have "mov foo@GOT[(%re1g)], %reg2",
1768 "test %reg1, foo@GOT(%reg2)" and
1769 "binop foo@GOT[(%reg1)], %reg2".
1771 Avoid optimizing _DYNAMIC since ld.so may use its
1772 link-time address. */
1773 if (h == htab->elf.hdynamic)
1776 /* def_regular is set by an assignment in a linker script in
1777 bfd_elf_record_link_assignment. */
1779 || h->root.type == bfd_link_hash_defined
1780 || h->root.type == bfd_link_hash_defweak)
1781 && SYMBOL_REFERENCES_LOCAL (link_info, h))
1788 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1789 "mov $foo, %reg2" with R_386_32. */
1791 modrm = 0xc0 | (modrm & 0x38) >> 3;
1792 bfd_put_8 (abfd, modrm, contents + roff - 1);
1797 /* Convert "mov foo@GOT(%reg1), %reg2" to
1798 "lea foo@GOTOFF(%reg1), %reg2". */
1799 r_type = R_386_GOTOFF;
1805 /* Only R_386_32 is supported. */
1811 /* Convert "test %reg1, foo@GOT(%reg2)" to
1812 "test $foo, %reg1". */
1813 modrm = 0xc0 | (modrm & 0x38) >> 3;
1818 /* Convert "binop foo@GOT(%reg1), %reg2" to
1819 "binop $foo, %reg2". */
1821 | (modrm & 0x38) >> 3
1825 bfd_put_8 (abfd, modrm, contents + roff - 1);
1829 bfd_put_8 (abfd, opcode, contents + roff - 2);
1830 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1839 /* Rename some of the generic section flags to better document how they
1841 #define need_convert_load sec_flg0
1842 #define check_relocs_failed sec_flg1
1844 /* Look through the relocs for a section during the first phase, and
1845 calculate needed space in the global offset table, procedure linkage
1846 table, and dynamic reloc sections. */
1849 elf_i386_check_relocs (bfd *abfd,
1850 struct bfd_link_info *info,
1852 const Elf_Internal_Rela *relocs)
1854 struct elf_i386_link_hash_table *htab;
1855 Elf_Internal_Shdr *symtab_hdr;
1856 struct elf_link_hash_entry **sym_hashes;
1857 const Elf_Internal_Rela *rel;
1858 const Elf_Internal_Rela *rel_end;
1861 bfd_boolean use_plt_got;
1863 if (bfd_link_relocatable (info))
1866 /* Don't do anything special with non-loaded, non-alloced sections.
1867 In particular, any relocs in such sections should not affect GOT
1868 and PLT reference counting (ie. we don't allow them to create GOT
1869 or PLT entries), there's no possibility or desire to optimize TLS
1870 relocs, and there's not much point in propagating relocs to shared
1871 libs that the dynamic linker won't relocate. */
1872 if ((sec->flags & SEC_ALLOC) == 0)
1875 BFD_ASSERT (is_i386_elf (abfd));
1877 htab = elf_i386_hash_table (info);
1880 sec->check_relocs_failed = 1;
1884 /* Get the section contents. */
1885 if (elf_section_data (sec)->this_hdr.contents != NULL)
1886 contents = elf_section_data (sec)->this_hdr.contents;
1887 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1889 sec->check_relocs_failed = 1;
1893 use_plt_got = (!get_elf_i386_backend_data (abfd)->is_vxworks
1894 && (get_elf_i386_backend_data (abfd)
1895 == &elf_i386_arch_bed));
1897 symtab_hdr = &elf_symtab_hdr (abfd);
1898 sym_hashes = elf_sym_hashes (abfd);
1902 rel_end = relocs + sec->reloc_count;
1903 for (rel = relocs; rel < rel_end; rel++)
1905 unsigned int r_type;
1906 unsigned long r_symndx;
1907 struct elf_link_hash_entry *h;
1908 struct elf_i386_link_hash_entry *eh;
1909 Elf_Internal_Sym *isym;
1911 bfd_boolean size_reloc;
1913 r_symndx = ELF32_R_SYM (rel->r_info);
1914 r_type = ELF32_R_TYPE (rel->r_info);
1916 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1918 /* xgettext:c-format */
1919 _bfd_error_handler (_("%B: bad symbol index: %d"),
1924 if (r_symndx < symtab_hdr->sh_info)
1926 /* A local symbol. */
1927 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1932 /* Check relocation against local STT_GNU_IFUNC symbol. */
1933 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1935 h = elf_i386_get_local_sym_hash (htab, abfd, rel, TRUE);
1939 /* Fake a STT_GNU_IFUNC symbol. */
1940 h->type = STT_GNU_IFUNC;
1943 h->forced_local = 1;
1944 h->root.type = bfd_link_hash_defined;
1952 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1953 while (h->root.type == bfd_link_hash_indirect
1954 || h->root.type == bfd_link_hash_warning)
1955 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1958 eh = (struct elf_i386_link_hash_entry *) h;
1974 if (htab->elf.dynobj == NULL)
1975 htab->elf.dynobj = abfd;
1976 /* Create the ifunc sections for static executables. */
1977 if (h->type == STT_GNU_IFUNC
1978 && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj,
1984 /* It is referenced by a non-shared object. */
1986 h->root.non_ir_ref = 1;
1988 if (h->type == STT_GNU_IFUNC)
1989 elf_tdata (info->output_bfd)->has_gnu_symbols
1990 |= elf_gnu_symbol_ifunc;
1993 if (! elf_i386_tls_transition (info, abfd, sec, contents,
1994 symtab_hdr, sym_hashes,
1995 &r_type, GOT_UNKNOWN,
1996 rel, rel_end, h, r_symndx, FALSE))
2002 htab->tls_ldm_got.refcount += 1;
2006 /* This symbol requires a procedure linkage table entry. We
2007 actually build the entry in adjust_dynamic_symbol,
2008 because this might be a case of linking PIC code which is
2009 never referenced by a dynamic object, in which case we
2010 don't need to generate a procedure linkage table entry
2013 /* If this is a local symbol, we resolve it directly without
2014 creating a procedure linkage table entry. */
2018 eh->has_got_reloc = 1;
2020 h->plt.refcount += 1;
2027 case R_386_TLS_IE_32:
2029 case R_386_TLS_GOTIE:
2030 if (!bfd_link_executable (info))
2031 info->flags |= DF_STATIC_TLS;
2037 case R_386_TLS_GOTDESC:
2038 case R_386_TLS_DESC_CALL:
2039 /* This symbol requires a global offset table entry. */
2041 int tls_type, old_tls_type;
2048 tls_type = GOT_NORMAL;
2050 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
2051 case R_386_TLS_GOTDESC:
2052 case R_386_TLS_DESC_CALL:
2053 tls_type = GOT_TLS_GDESC; break;
2054 case R_386_TLS_IE_32:
2055 if (ELF32_R_TYPE (rel->r_info) == r_type)
2056 tls_type = GOT_TLS_IE_NEG;
2058 /* If this is a GD->IE transition, we may use either of
2059 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
2060 tls_type = GOT_TLS_IE;
2063 case R_386_TLS_GOTIE:
2064 tls_type = GOT_TLS_IE_POS; break;
2069 h->got.refcount += 1;
2070 old_tls_type = elf_i386_hash_entry(h)->tls_type;
2074 bfd_signed_vma *local_got_refcounts;
2076 /* This is a global offset table entry for a local symbol. */
2077 local_got_refcounts = elf_local_got_refcounts (abfd);
2078 if (local_got_refcounts == NULL)
2082 size = symtab_hdr->sh_info;
2083 size *= (sizeof (bfd_signed_vma)
2084 + sizeof (bfd_vma) + sizeof(char));
2085 local_got_refcounts = (bfd_signed_vma *)
2086 bfd_zalloc (abfd, size);
2087 if (local_got_refcounts == NULL)
2089 elf_local_got_refcounts (abfd) = local_got_refcounts;
2090 elf_i386_local_tlsdesc_gotent (abfd)
2091 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
2092 elf_i386_local_got_tls_type (abfd)
2093 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
2095 local_got_refcounts[r_symndx] += 1;
2096 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
2099 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
2100 tls_type |= old_tls_type;
2101 /* If a TLS symbol is accessed using IE at least once,
2102 there is no point to use dynamic model for it. */
2103 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2104 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2105 || (tls_type & GOT_TLS_IE) == 0))
2107 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
2108 tls_type = old_tls_type;
2109 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2110 && GOT_TLS_GD_ANY_P (tls_type))
2111 tls_type |= old_tls_type;
2115 name = h->root.root.string;
2117 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2120 /* xgettext:c-format */
2121 (_("%B: `%s' accessed both as normal and "
2122 "thread local symbol"),
2124 bfd_set_error (bfd_error_bad_value);
2129 if (old_tls_type != tls_type)
2132 elf_i386_hash_entry (h)->tls_type = tls_type;
2134 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
2142 if (htab->elf.sgot == NULL)
2144 if (htab->elf.dynobj == NULL)
2145 htab->elf.dynobj = abfd;
2146 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
2149 if (r_type != R_386_TLS_IE)
2152 eh->has_got_reloc = 1;
2157 case R_386_TLS_LE_32:
2160 eh->has_got_reloc = 1;
2161 if (bfd_link_executable (info))
2163 info->flags |= DF_STATIC_TLS;
2168 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2169 eh->has_non_got_reloc = 1;
2171 /* We are called after all symbols have been resolved. Only
2172 relocation against STT_GNU_IFUNC symbol must go through
2175 && (bfd_link_executable (info)
2176 || h->type == STT_GNU_IFUNC))
2178 /* If this reloc is in a read-only section, we might
2179 need a copy reloc. We can't check reliably at this
2180 stage whether the section is read-only, as input
2181 sections have not yet been mapped to output sections.
2182 Tentatively set the flag for now, and correct in
2183 adjust_dynamic_symbol. */
2186 /* We may need a .plt entry if the symbol is a function
2187 defined in a shared lib or is a STT_GNU_IFUNC function
2188 referenced from the code or read-only section. */
2190 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2191 h->plt.refcount += 1;
2193 if (r_type == R_386_PC32)
2195 /* Since something like ".long foo - ." may be used
2196 as pointer, make sure that PLT is used if foo is
2197 a function defined in a shared library. */
2198 if ((sec->flags & SEC_CODE) == 0)
2199 h->pointer_equality_needed = 1;
2200 else if (h->type == STT_GNU_IFUNC
2201 && bfd_link_pic (info))
2204 name = h->root.root.string;
2206 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2209 /* xgettext:c-format */
2210 (_("%B: unsupported non-PIC call to IFUNC `%s'"),
2212 bfd_set_error (bfd_error_bad_value);
2218 h->pointer_equality_needed = 1;
2219 /* R_386_32 can be resolved at run-time. */
2220 if (r_type == R_386_32
2221 && (sec->flags & SEC_READONLY) == 0)
2222 eh->func_pointer_refcount += 1;
2228 /* If we are creating a shared library, and this is a reloc
2229 against a global symbol, or a non PC relative reloc
2230 against a local symbol, then we need to copy the reloc
2231 into the shared library. However, if we are linking with
2232 -Bsymbolic, we do not need to copy a reloc against a
2233 global symbol which is defined in an object we are
2234 including in the link (i.e., DEF_REGULAR is set). At
2235 this point we have not seen all the input files, so it is
2236 possible that DEF_REGULAR is not set now but will be set
2237 later (it is never cleared). In case of a weak definition,
2238 DEF_REGULAR may be cleared later by a strong definition in
2239 a shared library. We account for that possibility below by
2240 storing information in the relocs_copied field of the hash
2241 table entry. A similar situation occurs when creating
2242 shared libraries and symbol visibility changes render the
2245 If on the other hand, we are creating an executable, we
2246 may need to keep relocations for symbols satisfied by a
2247 dynamic library if we manage to avoid copy relocs for the
2250 Generate dynamic pointer relocation against STT_GNU_IFUNC
2251 symbol in the non-code section. */
2252 if ((bfd_link_pic (info)
2253 && (r_type != R_386_PC32
2255 && (! (bfd_link_pie (info)
2256 || SYMBOLIC_BIND (info, h))
2257 || h->root.type == bfd_link_hash_defweak
2258 || !h->def_regular))))
2260 && h->type == STT_GNU_IFUNC
2261 && r_type == R_386_32
2262 && (sec->flags & SEC_CODE) == 0)
2263 || (ELIMINATE_COPY_RELOCS
2264 && !bfd_link_pic (info)
2266 && (h->root.type == bfd_link_hash_defweak
2267 || !h->def_regular)))
2269 struct elf_dyn_relocs *p;
2270 struct elf_dyn_relocs **head;
2272 /* We must copy these reloc types into the output file.
2273 Create a reloc section in dynobj and make room for
2277 if (htab->elf.dynobj == NULL)
2278 htab->elf.dynobj = abfd;
2280 sreloc = _bfd_elf_make_dynamic_reloc_section
2281 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
2287 /* If this is a global symbol, we count the number of
2288 relocations we need for this symbol. */
2291 head = &eh->dyn_relocs;
2295 /* Track dynamic relocs needed for local syms too.
2296 We really need local syms available to do this
2301 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2306 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2310 vpp = &elf_section_data (s)->local_dynrel;
2311 head = (struct elf_dyn_relocs **)vpp;
2315 if (p == NULL || p->sec != sec)
2317 bfd_size_type amt = sizeof *p;
2318 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
2330 /* Count size relocation as PC-relative relocation. */
2331 if (r_type == R_386_PC32 || size_reloc)
2336 /* This relocation describes the C++ object vtable hierarchy.
2337 Reconstruct it for later use during GC. */
2338 case R_386_GNU_VTINHERIT:
2339 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2343 /* This relocation describes which C++ vtable entries are actually
2344 used. Record for later use during GC. */
2345 case R_386_GNU_VTENTRY:
2346 BFD_ASSERT (h != NULL);
2348 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
2358 && h->plt.refcount > 0
2359 && (((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
2360 || h->got.refcount > 0)
2361 && htab->plt_got == NULL)
2363 /* Create the GOT procedure linkage table. */
2364 unsigned int plt_got_align;
2365 const struct elf_backend_data *bed;
2367 bed = get_elf_backend_data (info->output_bfd);
2368 BFD_ASSERT (sizeof (elf_i386_got_plt_entry) == 8
2369 && (sizeof (elf_i386_got_plt_entry)
2370 == sizeof (elf_i386_pic_got_plt_entry)));
2373 if (htab->elf.dynobj == NULL)
2374 htab->elf.dynobj = abfd;
2376 = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2378 (bed->dynamic_sec_flags
2383 if (htab->plt_got == NULL
2384 || !bfd_set_section_alignment (htab->elf.dynobj,
2389 if (!info->no_ld_generated_unwind_info
2390 && htab->plt_got_eh_frame == NULL
2391 && get_elf_i386_backend_data (abfd)->plt->eh_frame_plt_got != NULL)
2393 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2394 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2395 | SEC_LINKER_CREATED);
2396 htab->plt_got_eh_frame
2397 = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2400 if (htab->plt_got_eh_frame == NULL
2401 || !bfd_set_section_alignment (htab->elf.dynobj,
2402 htab->plt_got_eh_frame,
2408 if (r_type == R_386_GOT32X
2409 && (h == NULL || h->type != STT_GNU_IFUNC))
2410 sec->need_convert_load = 1;
2413 if (elf_section_data (sec)->this_hdr.contents != contents)
2415 if (!info->keep_memory)
2419 /* Cache the section contents for elf_link_input_bfd. */
2420 elf_section_data (sec)->this_hdr.contents = contents;
2427 if (elf_section_data (sec)->this_hdr.contents != contents)
2429 sec->check_relocs_failed = 1;
2433 /* Return the section that should be marked against GC for a given
2437 elf_i386_gc_mark_hook (asection *sec,
2438 struct bfd_link_info *info,
2439 Elf_Internal_Rela *rel,
2440 struct elf_link_hash_entry *h,
2441 Elf_Internal_Sym *sym)
2444 switch (ELF32_R_TYPE (rel->r_info))
2446 case R_386_GNU_VTINHERIT:
2447 case R_386_GNU_VTENTRY:
2451 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2454 /* Remove undefined weak symbol from the dynamic symbol table if it
2455 is resolved to 0. */
2458 elf_i386_fixup_symbol (struct bfd_link_info *info,
2459 struct elf_link_hash_entry *h)
2461 if (h->dynindx != -1
2462 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2463 elf_i386_hash_entry (h)->has_got_reloc,
2464 elf_i386_hash_entry (h)))
2467 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2473 /* Adjust a symbol defined by a dynamic object and referenced by a
2474 regular object. The current definition is in some section of the
2475 dynamic object, but we're not including those sections. We have to
2476 change the definition to something the rest of the link can
2480 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
2481 struct elf_link_hash_entry *h)
2483 struct elf_i386_link_hash_table *htab;
2485 struct elf_i386_link_hash_entry *eh;
2486 struct elf_dyn_relocs *p;
2488 /* STT_GNU_IFUNC symbol must go through PLT. */
2489 if (h->type == STT_GNU_IFUNC)
2491 /* All local STT_GNU_IFUNC references must be treate as local
2492 calls via local PLT. */
2494 && SYMBOL_CALLS_LOCAL (info, h))
2496 bfd_size_type pc_count = 0, count = 0;
2497 struct elf_dyn_relocs **pp;
2499 eh = (struct elf_i386_link_hash_entry *) h;
2500 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2502 pc_count += p->pc_count;
2503 p->count -= p->pc_count;
2512 if (pc_count || count)
2517 /* Increment PLT reference count only for PC-relative
2520 if (h->plt.refcount <= 0)
2521 h->plt.refcount = 1;
2523 h->plt.refcount += 1;
2528 if (h->plt.refcount <= 0)
2530 h->plt.offset = (bfd_vma) -1;
2536 /* If this is a function, put it in the procedure linkage table. We
2537 will fill in the contents of the procedure linkage table later,
2538 when we know the address of the .got section. */
2539 if (h->type == STT_FUNC
2542 if (h->plt.refcount <= 0
2543 || SYMBOL_CALLS_LOCAL (info, h)
2544 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2545 && h->root.type == bfd_link_hash_undefweak))
2547 /* This case can occur if we saw a PLT32 reloc in an input
2548 file, but the symbol was never referred to by a dynamic
2549 object, or if all references were garbage collected. In
2550 such a case, we don't actually need to build a procedure
2551 linkage table, and we can just do a PC32 reloc instead. */
2552 h->plt.offset = (bfd_vma) -1;
2559 /* It's possible that we incorrectly decided a .plt reloc was
2560 needed for an R_386_PC32 reloc to a non-function sym in
2561 check_relocs. We can't decide accurately between function and
2562 non-function syms in check-relocs; Objects loaded later in
2563 the link may change h->type. So fix it now. */
2564 h->plt.offset = (bfd_vma) -1;
2566 /* If this is a weak symbol, and there is a real definition, the
2567 processor independent code will have arranged for us to see the
2568 real definition first, and we can just use the same value. */
2569 if (h->u.weakdef != NULL)
2571 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2572 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2573 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2574 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2575 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
2576 h->non_got_ref = h->u.weakdef->non_got_ref;
2580 /* This is a reference to a symbol defined by a dynamic object which
2581 is not a function. */
2583 /* If we are creating a shared library, we must presume that the
2584 only references to the symbol are via the global offset table.
2585 For such cases we need not do anything here; the relocations will
2586 be handled correctly by relocate_section. */
2587 if (!bfd_link_executable (info))
2590 /* If there are no references to this symbol that do not use the
2591 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
2593 eh = (struct elf_i386_link_hash_entry *) h;
2594 if (!h->non_got_ref && !eh->gotoff_ref)
2597 /* If -z nocopyreloc was given, we won't generate them either. */
2598 if (info->nocopyreloc)
2604 htab = elf_i386_hash_table (info);
2608 /* If there aren't any dynamic relocs in read-only sections nor
2609 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
2610 avoid the copy reloc. This doesn't work on VxWorks, where we can
2611 not have dynamic relocations (other than copy and jump slot
2612 relocations) in an executable. */
2613 if (ELIMINATE_COPY_RELOCS
2615 && !get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
2617 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2619 s = p->sec->output_section;
2620 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2631 /* We must allocate the symbol in our .dynbss section, which will
2632 become part of the .bss section of the executable. There will be
2633 an entry for this symbol in the .dynsym section. The dynamic
2634 object will contain position independent code, so all references
2635 from the dynamic object to this symbol will go through the global
2636 offset table. The dynamic linker will use the .dynsym entry to
2637 determine the address it must put in the global offset table, so
2638 both the dynamic object and the regular object will refer to the
2639 same memory location for the variable. */
2641 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
2642 copy the initial value out of the dynamic object and into the
2643 runtime process image. */
2644 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2646 s = htab->elf.sdynrelro;
2647 srel = htab->elf.sreldynrelro;
2651 s = htab->elf.sdynbss;
2652 srel = htab->elf.srelbss;
2654 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2656 srel->size += sizeof (Elf32_External_Rel);
2660 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2663 /* Allocate space in .plt, .got and associated reloc sections for
2667 elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2669 struct bfd_link_info *info;
2670 struct elf_i386_link_hash_table *htab;
2671 struct elf_i386_link_hash_entry *eh;
2672 struct elf_dyn_relocs *p;
2673 unsigned plt_entry_size;
2674 bfd_boolean resolved_to_zero;
2676 if (h->root.type == bfd_link_hash_indirect)
2679 eh = (struct elf_i386_link_hash_entry *) h;
2681 info = (struct bfd_link_info *) inf;
2682 htab = elf_i386_hash_table (info);
2686 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
2688 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2692 /* Clear the reference count of function pointer relocations if
2693 symbol isn't a normal function. */
2694 if (h->type != STT_FUNC)
2695 eh->func_pointer_refcount = 0;
2697 /* We can't use the GOT PLT if pointer equality is needed since
2698 finish_dynamic_symbol won't clear symbol value and the dynamic
2699 linker won't update the GOT slot. We will get into an infinite
2700 loop at run-time. */
2701 if (htab->plt_got != NULL
2702 && h->type != STT_GNU_IFUNC
2703 && !h->pointer_equality_needed
2704 && h->plt.refcount > 0
2705 && h->got.refcount > 0)
2707 /* Don't use the regular PLT if there are both GOT and GOTPLT
2709 h->plt.offset = (bfd_vma) -1;
2711 /* Use the GOT PLT. */
2712 eh->plt_got.refcount = 1;
2715 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2716 here if it is defined and referenced in a non-shared object. */
2717 if (h->type == STT_GNU_IFUNC
2719 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
2720 &htab->readonly_dynrelocs_against_ifunc,
2722 plt_entry_size, 4, TRUE);
2723 /* Don't create the PLT entry if there are only function pointer
2724 relocations which can be resolved at run-time. */
2725 else if (htab->elf.dynamic_sections_created
2726 && (h->plt.refcount > eh->func_pointer_refcount
2727 || eh->plt_got.refcount > 0))
2729 bfd_boolean use_plt_got;
2731 /* Clear the reference count of function pointer relocations
2733 eh->func_pointer_refcount = 0;
2735 if ((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
2737 /* Don't use the regular PLT for DF_BIND_NOW. */
2738 h->plt.offset = (bfd_vma) -1;
2740 /* Use the GOT PLT. */
2741 h->got.refcount = 1;
2742 eh->plt_got.refcount = 1;
2745 use_plt_got = eh->plt_got.refcount > 0;
2747 /* Make sure this symbol is output as a dynamic symbol.
2748 Undefined weak syms won't yet be marked as dynamic. */
2749 if (h->dynindx == -1
2751 && !resolved_to_zero
2752 && h->root.type == bfd_link_hash_undefweak)
2754 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2758 if (bfd_link_pic (info)
2759 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2761 asection *s = htab->elf.splt;
2762 asection *got_s = htab->plt_got;
2764 /* If this is the first .plt entry, make room for the special
2765 first entry. The .plt section is used by prelink to undo
2766 prelinking for dynamic relocations. */
2768 s->size = plt_entry_size;
2771 eh->plt_got.offset = got_s->size;
2773 h->plt.offset = s->size;
2775 /* If this symbol is not defined in a regular file, and we are
2776 not generating a shared library, then set the symbol to this
2777 location in the .plt. This is required to make function
2778 pointers compare as equal between the normal executable and
2779 the shared library. */
2780 if (! bfd_link_pic (info)
2785 /* We need to make a call to the entry of the GOT PLT
2786 instead of regular PLT entry. */
2787 h->root.u.def.section = got_s;
2788 h->root.u.def.value = eh->plt_got.offset;
2792 h->root.u.def.section = s;
2793 h->root.u.def.value = h->plt.offset;
2797 /* Make room for this entry. */
2799 got_s->size += sizeof (elf_i386_got_plt_entry);
2802 s->size += plt_entry_size;
2804 /* We also need to make an entry in the .got.plt section,
2805 which will be placed in the .got section by the linker
2807 htab->elf.sgotplt->size += 4;
2809 /* There should be no PLT relocation against resolved
2810 undefined weak symbol in executable. */
2811 if (!resolved_to_zero)
2813 /* We also need to make an entry in the .rel.plt
2815 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2816 htab->elf.srelplt->reloc_count++;
2820 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks
2821 && !bfd_link_pic (info))
2823 /* VxWorks has a second set of relocations for each PLT entry
2824 in executables. They go in a separate relocation section,
2825 which is processed by the kernel loader. */
2827 /* There are two relocations for the initial PLT entry: an
2828 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
2829 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
2831 if (h->plt.offset == plt_entry_size)
2832 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2834 /* There are two extra relocations for each subsequent PLT entry:
2835 an R_386_32 relocation for the GOT entry, and an R_386_32
2836 relocation for the PLT entry. */
2838 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2843 eh->plt_got.offset = (bfd_vma) -1;
2844 h->plt.offset = (bfd_vma) -1;
2850 eh->plt_got.offset = (bfd_vma) -1;
2851 h->plt.offset = (bfd_vma) -1;
2855 eh->tlsdesc_got = (bfd_vma) -1;
2857 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
2858 make it a R_386_TLS_LE_32 requiring no TLS entry. */
2859 if (h->got.refcount > 0
2860 && bfd_link_executable (info)
2862 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
2863 h->got.offset = (bfd_vma) -1;
2864 else if (h->got.refcount > 0)
2868 int tls_type = elf_i386_hash_entry(h)->tls_type;
2870 /* Make sure this symbol is output as a dynamic symbol.
2871 Undefined weak syms won't yet be marked as dynamic. */
2872 if (h->dynindx == -1
2874 && !resolved_to_zero
2875 && h->root.type == bfd_link_hash_undefweak)
2877 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2882 if (GOT_TLS_GDESC_P (tls_type))
2884 eh->tlsdesc_got = htab->elf.sgotplt->size
2885 - elf_i386_compute_jump_table_size (htab);
2886 htab->elf.sgotplt->size += 8;
2887 h->got.offset = (bfd_vma) -2;
2889 if (! GOT_TLS_GDESC_P (tls_type)
2890 || GOT_TLS_GD_P (tls_type))
2892 h->got.offset = s->size;
2894 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
2895 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
2898 dyn = htab->elf.dynamic_sections_created;
2899 /* R_386_TLS_IE_32 needs one dynamic relocation,
2900 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
2901 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
2902 need two), R_386_TLS_GD needs one if local symbol and two if
2903 global. No dynamic relocation against resolved undefined weak
2904 symbol in executable. */
2905 if (tls_type == GOT_TLS_IE_BOTH)
2906 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2907 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2908 || (tls_type & GOT_TLS_IE))
2909 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2910 else if (GOT_TLS_GD_P (tls_type))
2911 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2912 else if (! GOT_TLS_GDESC_P (tls_type)
2913 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2914 && !resolved_to_zero)
2915 || h->root.type != bfd_link_hash_undefweak)
2916 && (bfd_link_pic (info)
2917 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2918 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2919 if (GOT_TLS_GDESC_P (tls_type))
2920 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2923 h->got.offset = (bfd_vma) -1;
2925 if (eh->dyn_relocs == NULL)
2928 /* In the shared -Bsymbolic case, discard space allocated for
2929 dynamic pc-relative relocs against symbols which turn out to be
2930 defined in regular objects. For the normal shared case, discard
2931 space for pc-relative relocs that have become local due to symbol
2932 visibility changes. */
2934 if (bfd_link_pic (info))
2936 /* The only reloc that uses pc_count is R_386_PC32, which will
2937 appear on a call or on something like ".long foo - .". We
2938 want calls to protected symbols to resolve directly to the
2939 function rather than going via the plt. If people want
2940 function pointer comparisons to work as expected then they
2941 should avoid writing assembly like ".long foo - .". */
2942 if (SYMBOL_CALLS_LOCAL (info, h))
2944 struct elf_dyn_relocs **pp;
2946 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2948 p->count -= p->pc_count;
2957 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
2959 struct elf_dyn_relocs **pp;
2960 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2962 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2969 /* Also discard relocs on undefined weak syms with non-default
2970 visibility or in PIE. */
2971 if (eh->dyn_relocs != NULL
2972 && h->root.type == bfd_link_hash_undefweak)
2974 /* Undefined weak symbol is never bound locally in shared
2976 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2977 || resolved_to_zero)
2981 /* Keep dynamic non-GOT/non-PLT relocation so that we
2982 can branch to 0 without PLT. */
2983 struct elf_dyn_relocs **pp;
2985 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2986 if (p->pc_count == 0)
2990 /* Remove non-R_386_PC32 relocation. */
2991 p->count = p->pc_count;
2995 if (eh->dyn_relocs != NULL)
2997 /* Make sure undefined weak symbols are output
2998 as dynamic symbols in PIEs for dynamic non-GOT
2999 non-PLT reloations. */
3000 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3005 eh->dyn_relocs = NULL;
3007 else if (h->dynindx == -1
3008 && !h->forced_local)
3010 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3015 else if (ELIMINATE_COPY_RELOCS)
3017 /* For the non-shared case, discard space for relocs against
3018 symbols which turn out to need copy relocs or are not
3019 dynamic. Keep dynamic relocations for run-time function
3020 pointer initialization. */
3022 if ((!h->non_got_ref
3023 || eh->func_pointer_refcount > 0
3024 || (h->root.type == bfd_link_hash_undefweak
3025 && !resolved_to_zero))
3028 || (htab->elf.dynamic_sections_created
3029 && (h->root.type == bfd_link_hash_undefweak
3030 || h->root.type == bfd_link_hash_undefined))))
3032 /* Make sure this symbol is output as a dynamic symbol.
3033 Undefined weak syms won't yet be marked as dynamic. */
3034 if (h->dynindx == -1
3036 && !resolved_to_zero
3037 && h->root.type == bfd_link_hash_undefweak)
3039 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3043 /* If that succeeded, we know we'll be keeping all the
3045 if (h->dynindx != -1)
3049 eh->dyn_relocs = NULL;
3050 eh->func_pointer_refcount = 0;
3055 /* Finally, allocate space. */
3056 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3060 sreloc = elf_section_data (p->sec)->sreloc;
3062 BFD_ASSERT (sreloc != NULL);
3063 sreloc->size += p->count * sizeof (Elf32_External_Rel);
3069 /* Allocate space in .plt, .got and associated reloc sections for
3070 local dynamic relocs. */
3073 elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
3075 struct elf_link_hash_entry *h
3076 = (struct elf_link_hash_entry *) *slot;
3078 if (h->type != STT_GNU_IFUNC
3082 || h->root.type != bfd_link_hash_defined)
3085 return elf_i386_allocate_dynrelocs (h, inf);
3088 /* Find any dynamic relocs that apply to read-only sections. */
3091 elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3093 struct elf_i386_link_hash_entry *eh;
3094 struct elf_dyn_relocs *p;
3096 /* Skip local IFUNC symbols. */
3097 if (h->forced_local && h->type == STT_GNU_IFUNC)
3100 eh = (struct elf_i386_link_hash_entry *) h;
3101 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3103 asection *s = p->sec->output_section;
3105 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3107 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3109 info->flags |= DF_TEXTREL;
3111 if ((info->warn_shared_textrel && bfd_link_pic (info))
3112 || info->error_textrel)
3113 /* xgettext:c-format */
3114 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
3115 p->sec->owner, h->root.root.string,
3118 /* Not an error, just cut short the traversal. */
3125 /* Convert load via the GOT slot to load immediate. */
3128 elf_i386_convert_load (bfd *abfd, asection *sec,
3129 struct bfd_link_info *link_info)
3131 struct elf_i386_link_hash_table *htab;
3132 Elf_Internal_Shdr *symtab_hdr;
3133 Elf_Internal_Rela *internal_relocs;
3134 Elf_Internal_Rela *irel, *irelend;
3136 bfd_boolean changed;
3137 bfd_signed_vma *local_got_refcounts;
3139 /* Don't even try to convert non-ELF outputs. */
3140 if (!is_elf_hash_table (link_info->hash))
3143 /* Nothing to do if there is no need or no output. */
3144 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
3145 || sec->need_convert_load == 0
3146 || bfd_is_abs_section (sec->output_section))
3149 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3151 /* Load the relocations for this section. */
3152 internal_relocs = (_bfd_elf_link_read_relocs
3153 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
3154 link_info->keep_memory));
3155 if (internal_relocs == NULL)
3159 htab = elf_i386_hash_table (link_info);
3160 local_got_refcounts = elf_local_got_refcounts (abfd);
3162 /* Get the section contents. */
3163 if (elf_section_data (sec)->this_hdr.contents != NULL)
3164 contents = elf_section_data (sec)->this_hdr.contents;
3167 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3171 irelend = internal_relocs + sec->reloc_count;
3172 for (irel = internal_relocs; irel < irelend; irel++)
3174 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
3175 unsigned int r_symndx;
3176 struct elf_link_hash_entry *h;
3177 bfd_boolean converted;
3179 /* Don't convert R_386_GOT32 since we can't tell if it is applied
3180 to "mov $foo@GOT, %reg" which isn't a load via GOT. */
3181 if (r_type != R_386_GOT32X)
3184 r_symndx = ELF32_R_SYM (irel->r_info);
3185 if (r_symndx < symtab_hdr->sh_info)
3186 h = elf_i386_get_local_sym_hash (htab, sec->owner,
3187 (const Elf_Internal_Rela *) irel,
3191 h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
3192 while (h->root.type == bfd_link_hash_indirect
3193 || h->root.type == bfd_link_hash_warning)
3194 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3197 /* STT_GNU_IFUNC must keep GOT32 relocations. */
3198 if (h != NULL && h->type == STT_GNU_IFUNC)
3202 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
3203 irel, h, &converted, link_info))
3208 changed = converted;
3211 if (h->got.refcount > 0)
3212 h->got.refcount -= 1;
3216 if (local_got_refcounts != NULL
3217 && local_got_refcounts[r_symndx] > 0)
3218 local_got_refcounts[r_symndx] -= 1;
3223 if (contents != NULL
3224 && elf_section_data (sec)->this_hdr.contents != contents)
3226 if (!changed && !link_info->keep_memory)
3230 /* Cache the section contents for elf_link_input_bfd. */
3231 elf_section_data (sec)->this_hdr.contents = contents;
3235 if (elf_section_data (sec)->relocs != internal_relocs)
3238 free (internal_relocs);
3240 elf_section_data (sec)->relocs = internal_relocs;
3246 if (contents != NULL
3247 && elf_section_data (sec)->this_hdr.contents != contents)
3249 if (internal_relocs != NULL
3250 && elf_section_data (sec)->relocs != internal_relocs)
3251 free (internal_relocs);
3255 /* Set the sizes of the dynamic sections. */
3258 elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3260 struct elf_i386_link_hash_table *htab;
3266 htab = elf_i386_hash_table (info);
3269 dynobj = htab->elf.dynobj;
3273 /* Set up .got offsets for local syms, and space for local dynamic
3275 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3277 bfd_signed_vma *local_got;
3278 bfd_signed_vma *end_local_got;
3279 char *local_tls_type;
3280 bfd_vma *local_tlsdesc_gotent;
3281 bfd_size_type locsymcount;
3282 Elf_Internal_Shdr *symtab_hdr;
3285 if (! is_i386_elf (ibfd))
3288 for (s = ibfd->sections; s != NULL; s = s->next)
3290 struct elf_dyn_relocs *p;
3292 if (!elf_i386_convert_load (ibfd, s, info))
3295 for (p = ((struct elf_dyn_relocs *)
3296 elf_section_data (s)->local_dynrel);
3300 if (!bfd_is_abs_section (p->sec)
3301 && bfd_is_abs_section (p->sec->output_section))
3303 /* Input section has been discarded, either because
3304 it is a copy of a linkonce section or due to
3305 linker script /DISCARD/, so we'll be discarding
3308 else if (get_elf_i386_backend_data (output_bfd)->is_vxworks
3309 && strcmp (p->sec->output_section->name,
3312 /* Relocations in vxworks .tls_vars sections are
3313 handled specially by the loader. */
3315 else if (p->count != 0)
3317 srel = elf_section_data (p->sec)->sreloc;
3318 srel->size += p->count * sizeof (Elf32_External_Rel);
3319 if ((p->sec->output_section->flags & SEC_READONLY) != 0
3320 && (info->flags & DF_TEXTREL) == 0)
3322 info->flags |= DF_TEXTREL;
3323 if ((info->warn_shared_textrel && bfd_link_pic (info))
3324 || info->error_textrel)
3325 /* xgettext:c-format */
3326 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
3327 p->sec->owner, p->sec);
3333 local_got = elf_local_got_refcounts (ibfd);
3337 symtab_hdr = &elf_symtab_hdr (ibfd);
3338 locsymcount = symtab_hdr->sh_info;
3339 end_local_got = local_got + locsymcount;
3340 local_tls_type = elf_i386_local_got_tls_type (ibfd);
3341 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
3343 srel = htab->elf.srelgot;
3344 for (; local_got < end_local_got;
3345 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
3347 *local_tlsdesc_gotent = (bfd_vma) -1;
3350 if (GOT_TLS_GDESC_P (*local_tls_type))
3352 *local_tlsdesc_gotent = htab->elf.sgotplt->size
3353 - elf_i386_compute_jump_table_size (htab);
3354 htab->elf.sgotplt->size += 8;
3355 *local_got = (bfd_vma) -2;
3357 if (! GOT_TLS_GDESC_P (*local_tls_type)
3358 || GOT_TLS_GD_P (*local_tls_type))
3360 *local_got = s->size;
3362 if (GOT_TLS_GD_P (*local_tls_type)
3363 || *local_tls_type == GOT_TLS_IE_BOTH)
3366 if (bfd_link_pic (info)
3367 || GOT_TLS_GD_ANY_P (*local_tls_type)
3368 || (*local_tls_type & GOT_TLS_IE))
3370 if (*local_tls_type == GOT_TLS_IE_BOTH)
3371 srel->size += 2 * sizeof (Elf32_External_Rel);
3372 else if (GOT_TLS_GD_P (*local_tls_type)
3373 || ! GOT_TLS_GDESC_P (*local_tls_type))
3374 srel->size += sizeof (Elf32_External_Rel);
3375 if (GOT_TLS_GDESC_P (*local_tls_type))
3376 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
3380 *local_got = (bfd_vma) -1;
3384 if (htab->tls_ldm_got.refcount > 0)
3386 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
3388 htab->tls_ldm_got.offset = htab->elf.sgot->size;
3389 htab->elf.sgot->size += 8;
3390 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
3393 htab->tls_ldm_got.offset = -1;
3395 /* Allocate global sym .plt and .got entries, and space for global
3396 sym dynamic relocs. */
3397 elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
3399 /* Allocate .plt and .got entries, and space for local symbols. */
3400 htab_traverse (htab->loc_hash_table,
3401 elf_i386_allocate_local_dynrelocs,
3404 /* For every jump slot reserved in the sgotplt, reloc_count is
3405 incremented. However, when we reserve space for TLS descriptors,
3406 it's not incremented, so in order to compute the space reserved
3407 for them, it suffices to multiply the reloc count by the jump
3410 PR ld/13302: We start next_irelative_index at the end of .rela.plt
3411 so that R_386_IRELATIVE entries come last. */
3412 if (htab->elf.srelplt)
3414 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
3415 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
3416 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
3418 else if (htab->elf.irelplt)
3419 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
3422 if (htab->elf.sgotplt)
3424 /* Don't allocate .got.plt section if there are no GOT nor PLT
3425 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
3426 if ((htab->elf.hgot == NULL
3427 || !htab->elf.hgot->ref_regular_nonweak)
3428 && (htab->elf.sgotplt->size
3429 == get_elf_backend_data (output_bfd)->got_header_size)
3430 && (htab->elf.splt == NULL
3431 || htab->elf.splt->size == 0)
3432 && (htab->elf.sgot == NULL
3433 || htab->elf.sgot->size == 0)
3434 && (htab->elf.iplt == NULL
3435 || htab->elf.iplt->size == 0)
3436 && (htab->elf.igotplt == NULL
3437 || htab->elf.igotplt->size == 0))
3438 htab->elf.sgotplt->size = 0;
3441 if (_bfd_elf_eh_frame_present (info))
3443 if (htab->plt_eh_frame != NULL
3444 && htab->elf.splt != NULL
3445 && htab->elf.splt->size != 0
3446 && !bfd_is_abs_section (htab->elf.splt->output_section))
3447 htab->plt_eh_frame->size
3448 = get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt_size;
3450 if (htab->plt_got_eh_frame != NULL
3451 && htab->plt_got != NULL
3452 && htab->plt_got->size != 0
3453 && !bfd_is_abs_section (htab->plt_got->output_section))
3454 htab->plt_got_eh_frame->size
3455 = get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt_got_size;
3458 /* We now have determined the sizes of the various dynamic sections.
3459 Allocate memory for them. */
3461 for (s = dynobj->sections; s != NULL; s = s->next)
3463 bfd_boolean strip_section = TRUE;
3465 if ((s->flags & SEC_LINKER_CREATED) == 0)
3468 if (s == htab->elf.splt
3469 || s == htab->elf.sgot)
3471 /* Strip this section if we don't need it; see the
3473 /* We'd like to strip these sections if they aren't needed, but if
3474 we've exported dynamic symbols from them we must leave them.
3475 It's too late to tell BFD to get rid of the symbols. */
3477 if (htab->elf.hplt != NULL)
3478 strip_section = FALSE;
3480 else if (s == htab->elf.sgotplt
3481 || s == htab->elf.iplt
3482 || s == htab->elf.igotplt
3483 || s == htab->plt_got
3484 || s == htab->plt_eh_frame
3485 || s == htab->plt_got_eh_frame
3486 || s == htab->elf.sdynbss
3487 || s == htab->elf.sdynrelro)
3489 /* Strip these too. */
3491 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
3494 && s != htab->elf.srelplt
3495 && s != htab->srelplt2)
3498 /* We use the reloc_count field as a counter if we need
3499 to copy relocs into the output file. */
3504 /* It's not one of our sections, so don't allocate space. */
3510 /* If we don't need this section, strip it from the
3511 output file. This is mostly to handle .rel.bss and
3512 .rel.plt. We must create both sections in
3513 create_dynamic_sections, because they must be created
3514 before the linker maps input sections to output
3515 sections. The linker does that before
3516 adjust_dynamic_symbol is called, and it is that
3517 function which decides whether anything needs to go
3518 into these sections. */
3520 s->flags |= SEC_EXCLUDE;
3524 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3527 /* Allocate memory for the section contents. We use bfd_zalloc
3528 here in case unused entries are not reclaimed before the
3529 section's contents are written out. This should not happen,
3530 but this way if it does, we get a R_386_NONE reloc instead
3532 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
3533 if (s->contents == NULL)
3537 if (htab->plt_eh_frame != NULL
3538 && htab->plt_eh_frame->contents != NULL)
3540 memcpy (htab->plt_eh_frame->contents,
3541 get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt,
3542 htab->plt_eh_frame->size);
3543 bfd_put_32 (dynobj, htab->elf.splt->size,
3544 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3547 if (htab->plt_got_eh_frame != NULL
3548 && htab->plt_got_eh_frame->contents != NULL)
3550 memcpy (htab->plt_got_eh_frame->contents,
3551 get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt_got,
3552 htab->plt_got_eh_frame->size);
3553 bfd_put_32 (dynobj, htab->plt_got->size,
3554 (htab->plt_got_eh_frame->contents
3555 + PLT_FDE_LEN_OFFSET));
3558 if (htab->elf.dynamic_sections_created)
3560 /* Add some entries to the .dynamic section. We fill in the
3561 values later, in elf_i386_finish_dynamic_sections, but we
3562 must add the entries now so that we get the correct size for
3563 the .dynamic section. The DT_DEBUG entry is filled in by the
3564 dynamic linker and used by the debugger. */
3565 #define add_dynamic_entry(TAG, VAL) \
3566 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3568 if (bfd_link_executable (info))
3570 if (!add_dynamic_entry (DT_DEBUG, 0))
3574 if (htab->elf.splt->size != 0)
3576 /* DT_PLTGOT is used by prelink even if there is no PLT
3578 if (!add_dynamic_entry (DT_PLTGOT, 0))
3582 if (htab->elf.srelplt->size != 0)
3584 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3585 || !add_dynamic_entry (DT_PLTREL, DT_REL)
3586 || !add_dynamic_entry (DT_JMPREL, 0))
3592 if (!add_dynamic_entry (DT_REL, 0)
3593 || !add_dynamic_entry (DT_RELSZ, 0)
3594 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
3597 /* If any dynamic relocs apply to a read-only section,
3598 then we need a DT_TEXTREL entry. */
3599 if ((info->flags & DF_TEXTREL) == 0)
3600 elf_link_hash_traverse (&htab->elf,
3601 elf_i386_readonly_dynrelocs, info);
3603 if ((info->flags & DF_TEXTREL) != 0)
3605 if (htab->readonly_dynrelocs_against_ifunc)
3607 info->callbacks->einfo
3608 (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
3609 bfd_set_error (bfd_error_bad_value);
3613 if (!add_dynamic_entry (DT_TEXTREL, 0))
3617 if (get_elf_i386_backend_data (output_bfd)->is_vxworks
3618 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3621 #undef add_dynamic_entry
3627 elf_i386_always_size_sections (bfd *output_bfd,
3628 struct bfd_link_info *info)
3630 asection *tls_sec = elf_hash_table (info)->tls_sec;
3634 struct elf_link_hash_entry *tlsbase;
3636 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3637 "_TLS_MODULE_BASE_",
3638 FALSE, FALSE, FALSE);
3640 if (tlsbase && tlsbase->type == STT_TLS)
3642 struct elf_i386_link_hash_table *htab;
3643 struct bfd_link_hash_entry *bh = NULL;
3644 const struct elf_backend_data *bed
3645 = get_elf_backend_data (output_bfd);
3647 htab = elf_i386_hash_table (info);
3651 if (!(_bfd_generic_link_add_one_symbol
3652 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3653 tls_sec, 0, NULL, FALSE,
3654 bed->collect, &bh)))
3657 htab->tls_module_base = bh;
3659 tlsbase = (struct elf_link_hash_entry *)bh;
3660 tlsbase->def_regular = 1;
3661 tlsbase->other = STV_HIDDEN;
3662 tlsbase->root.linker_def = 1;
3663 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
3670 /* Set the correct type for an x86 ELF section. We do this by the
3671 section name, which is a hack, but ought to work. */
3674 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
3675 Elf_Internal_Shdr *hdr,
3680 name = bfd_get_section_name (abfd, sec);
3682 /* This is an ugly, but unfortunately necessary hack that is
3683 needed when producing EFI binaries on x86. It tells
3684 elf.c:elf_fake_sections() not to consider ".reloc" as a section
3685 containing ELF relocation info. We need this hack in order to
3686 be able to generate ELF binaries that can be translated into
3687 EFI applications (which are essentially COFF objects). Those
3688 files contain a COFF ".reloc" section inside an ELFNN object,
3689 which would normally cause BFD to segfault because it would
3690 attempt to interpret this section as containing relocation
3691 entries for section "oc". With this hack enabled, ".reloc"
3692 will be treated as a normal data section, which will avoid the
3693 segfault. However, you won't be able to create an ELFNN binary
3694 with a section named "oc" that needs relocations, but that's
3695 the kind of ugly side-effects you get when detecting section
3696 types based on their names... In practice, this limitation is
3697 unlikely to bite. */
3698 if (strcmp (name, ".reloc") == 0)
3699 hdr->sh_type = SHT_PROGBITS;
3704 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
3705 executables. Rather than setting it to the beginning of the TLS
3706 section, we have to set it to the end. This function may be called
3707 multiple times, it is idempotent. */
3710 elf_i386_set_tls_module_base (struct bfd_link_info *info)
3712 struct elf_i386_link_hash_table *htab;
3713 struct bfd_link_hash_entry *base;
3715 if (!bfd_link_executable (info))
3718 htab = elf_i386_hash_table (info);
3722 base = htab->tls_module_base;
3726 base->u.def.value = htab->elf.tls_size;
3729 /* Return the base VMA address which should be subtracted from real addresses
3730 when resolving @dtpoff relocation.
3731 This is PT_TLS segment p_vaddr. */
3734 elf_i386_dtpoff_base (struct bfd_link_info *info)
3736 /* If tls_sec is NULL, we should have signalled an error already. */
3737 if (elf_hash_table (info)->tls_sec == NULL)
3739 return elf_hash_table (info)->tls_sec->vma;
3742 /* Return the relocation value for @tpoff relocation
3743 if STT_TLS virtual address is ADDRESS. */
3746 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
3748 struct elf_link_hash_table *htab = elf_hash_table (info);
3749 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3750 bfd_vma static_tls_size;
3752 /* If tls_sec is NULL, we should have signalled an error already. */
3753 if (htab->tls_sec == NULL)
3756 /* Consider special static TLS alignment requirements. */
3757 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3758 return static_tls_size + htab->tls_sec->vma - address;
3761 /* Relocate an i386 ELF section. */
3764 elf_i386_relocate_section (bfd *output_bfd,
3765 struct bfd_link_info *info,
3767 asection *input_section,
3769 Elf_Internal_Rela *relocs,
3770 Elf_Internal_Sym *local_syms,
3771 asection **local_sections)
3773 struct elf_i386_link_hash_table *htab;
3774 Elf_Internal_Shdr *symtab_hdr;
3775 struct elf_link_hash_entry **sym_hashes;
3776 bfd_vma *local_got_offsets;
3777 bfd_vma *local_tlsdesc_gotents;
3778 Elf_Internal_Rela *rel;
3779 Elf_Internal_Rela *wrel;
3780 Elf_Internal_Rela *relend;
3781 bfd_boolean is_vxworks_tls;
3782 unsigned plt_entry_size;
3784 BFD_ASSERT (is_i386_elf (input_bfd));
3786 /* Skip if check_relocs failed. */
3787 if (input_section->check_relocs_failed)
3790 htab = elf_i386_hash_table (info);
3793 symtab_hdr = &elf_symtab_hdr (input_bfd);
3794 sym_hashes = elf_sym_hashes (input_bfd);
3795 local_got_offsets = elf_local_got_offsets (input_bfd);
3796 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
3797 /* We have to handle relocations in vxworks .tls_vars sections
3798 specially, because the dynamic loader is 'weird'. */
3799 is_vxworks_tls = (get_elf_i386_backend_data (output_bfd)->is_vxworks
3800 && bfd_link_pic (info)
3801 && !strcmp (input_section->output_section->name,
3804 elf_i386_set_tls_module_base (info);
3806 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
3808 rel = wrel = relocs;
3809 relend = relocs + input_section->reloc_count;
3810 for (; rel < relend; wrel++, rel++)
3812 unsigned int r_type;
3813 reloc_howto_type *howto;
3814 unsigned long r_symndx;
3815 struct elf_link_hash_entry *h;
3816 struct elf_i386_link_hash_entry *eh;
3817 Elf_Internal_Sym *sym;
3819 bfd_vma off, offplt, plt_offset;
3821 bfd_boolean unresolved_reloc;
3822 bfd_reloc_status_type r;
3826 asection *resolved_plt;
3827 bfd_boolean resolved_to_zero;
3828 bfd_boolean relative_reloc;
3830 r_type = ELF32_R_TYPE (rel->r_info);
3831 if (r_type == R_386_GNU_VTINHERIT
3832 || r_type == R_386_GNU_VTENTRY)
3839 if ((indx = r_type) >= R_386_standard
3840 && ((indx = r_type - R_386_ext_offset) - R_386_standard
3841 >= R_386_ext - R_386_standard)
3842 && ((indx = r_type - R_386_tls_offset) - R_386_ext
3843 >= R_386_ext2 - R_386_ext))
3846 /* xgettext:c-format */
3847 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3848 input_bfd, r_type, input_section);
3849 bfd_set_error (bfd_error_bad_value);
3852 howto = elf_howto_table + indx;
3854 r_symndx = ELF32_R_SYM (rel->r_info);
3858 unresolved_reloc = FALSE;
3859 if (r_symndx < symtab_hdr->sh_info)
3861 sym = local_syms + r_symndx;
3862 sec = local_sections[r_symndx];
3863 relocation = (sec->output_section->vma
3864 + sec->output_offset
3866 st_size = sym->st_size;
3868 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
3869 && ((sec->flags & SEC_MERGE) != 0
3870 || (bfd_link_relocatable (info)
3871 && sec->output_offset != 0)))
3874 bfd_byte *where = contents + rel->r_offset;
3876 switch (howto->size)
3879 addend = bfd_get_8 (input_bfd, where);
3880 if (howto->pc_relative)
3882 addend = (addend ^ 0x80) - 0x80;
3887 addend = bfd_get_16 (input_bfd, where);
3888 if (howto->pc_relative)
3890 addend = (addend ^ 0x8000) - 0x8000;
3895 addend = bfd_get_32 (input_bfd, where);
3896 if (howto->pc_relative)
3898 addend = (addend ^ 0x80000000) - 0x80000000;
3906 if (bfd_link_relocatable (info))
3907 addend += sec->output_offset;
3910 asection *msec = sec;
3911 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
3913 addend -= relocation;
3914 addend += msec->output_section->vma + msec->output_offset;
3917 switch (howto->size)
3920 /* FIXME: overflow checks. */
3921 if (howto->pc_relative)
3923 bfd_put_8 (input_bfd, addend, where);
3926 if (howto->pc_relative)
3928 bfd_put_16 (input_bfd, addend, where);
3931 if (howto->pc_relative)
3933 bfd_put_32 (input_bfd, addend, where);
3937 else if (!bfd_link_relocatable (info)
3938 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3940 /* Relocate against local STT_GNU_IFUNC symbol. */
3941 h = elf_i386_get_local_sym_hash (htab, input_bfd, rel,
3946 /* Set STT_GNU_IFUNC symbol value. */
3947 h->root.u.def.value = sym->st_value;
3948 h->root.u.def.section = sec;
3953 bfd_boolean warned ATTRIBUTE_UNUSED;
3954 bfd_boolean ignored ATTRIBUTE_UNUSED;
3956 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3957 r_symndx, symtab_hdr, sym_hashes,
3959 unresolved_reloc, warned, ignored);
3963 if (sec != NULL && discarded_section (sec))
3965 _bfd_clear_contents (howto, input_bfd, input_section,
3966 contents + rel->r_offset);
3967 wrel->r_offset = rel->r_offset;
3971 /* For ld -r, remove relocations in debug sections against
3972 sections defined in discarded sections. Not done for
3973 eh_frame editing code expects to be present. */
3974 if (bfd_link_relocatable (info)
3975 && (input_section->flags & SEC_DEBUGGING))
3981 if (bfd_link_relocatable (info))
3988 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3989 it here if it is defined in a non-shared object. */
3991 && h->type == STT_GNU_IFUNC
3994 asection *plt, *gotplt, *base_got;
3998 if ((input_section->flags & SEC_ALLOC) == 0)
4000 /* Dynamic relocs are not propagated for SEC_DEBUGGING
4001 sections because such sections are not SEC_ALLOC and
4002 thus ld.so will not process them. */
4003 if ((input_section->flags & SEC_DEBUGGING) != 0)
4008 /* STT_GNU_IFUNC symbol must go through PLT. */
4009 if (htab->elf.splt != NULL)
4011 plt = htab->elf.splt;
4012 gotplt = htab->elf.sgotplt;
4016 plt = htab->elf.iplt;
4017 gotplt = htab->elf.igotplt;
4027 base_got = htab->elf.sgot;
4028 off = h->got.offset;
4030 if (base_got == NULL)
4033 if (off == (bfd_vma) -1)
4035 /* We can't use h->got.offset here to save state, or
4036 even just remember the offset, as finish_dynamic_symbol
4037 would use that as offset into .got. */
4039 if (h->plt.offset == (bfd_vma) -1)
4042 if (htab->elf.splt != NULL)
4044 plt_index = h->plt.offset / plt_entry_size - 1;
4045 off = (plt_index + 3) * 4;
4046 base_got = htab->elf.sgotplt;
4050 plt_index = h->plt.offset / plt_entry_size;
4051 off = plt_index * 4;
4052 base_got = htab->elf.igotplt;
4055 if (h->dynindx == -1
4059 /* This references the local defitionion. We must
4060 initialize this entry in the global offset table.
4061 Since the offset must always be a multiple of 8,
4062 we use the least significant bit to record
4063 whether we have initialized it already.
4065 When doing a dynamic link, we create a .rela.got
4066 relocation entry to initialize the value. This
4067 is done in the finish_dynamic_symbol routine. */
4072 bfd_put_32 (output_bfd, relocation,
4073 base_got->contents + off);
4081 relocation = (base_got->output_section->vma
4082 + base_got->output_offset + off
4083 - gotplt->output_section->vma
4084 - gotplt->output_offset);
4086 if (rel->r_offset > 1
4087 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
4088 && *(contents + rel->r_offset - 2) != 0x8d)
4090 if (bfd_link_pic (info))
4091 goto disallow_got32;
4093 /* Add the GOT base if there is no base register. */
4094 relocation += (gotplt->output_section->vma
4095 + gotplt->output_offset);
4097 else if (htab->elf.splt == NULL)
4099 /* Adjust for static executables. */
4100 relocation += gotplt->output_offset;
4106 if (h->plt.offset == (bfd_vma) -1)
4108 /* Handle static pointers of STT_GNU_IFUNC symbols. */
4109 if (r_type == R_386_32
4110 && (input_section->flags & SEC_CODE) == 0)
4111 goto do_ifunc_pointer;
4112 goto bad_ifunc_reloc;
4115 relocation = (plt->output_section->vma
4116 + plt->output_offset + h->plt.offset);
4122 if (h->root.root.string)
4123 name = h->root.root.string;
4125 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4128 /* xgettext:c-format */
4129 (_("%B: relocation %s against STT_GNU_IFUNC "
4130 "symbol `%s' isn't supported"), input_bfd,
4132 bfd_set_error (bfd_error_bad_value);
4136 /* Generate dynamic relcoation only when there is a
4137 non-GOT reference in a shared object. */
4138 if ((bfd_link_pic (info) && h->non_got_ref)
4139 || h->plt.offset == (bfd_vma) -1)
4141 Elf_Internal_Rela outrel;
4146 /* Need a dynamic relocation to get the real function
4148 offset = _bfd_elf_section_offset (output_bfd,
4152 if (offset == (bfd_vma) -1
4153 || offset == (bfd_vma) -2)
4156 outrel.r_offset = (input_section->output_section->vma
4157 + input_section->output_offset
4160 if (h->dynindx == -1
4162 || bfd_link_executable (info))
4164 /* This symbol is resolved locally. */
4165 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4166 bfd_put_32 (output_bfd,
4167 (h->root.u.def.value
4168 + h->root.u.def.section->output_section->vma
4169 + h->root.u.def.section->output_offset),
4173 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4175 /* Dynamic relocations are stored in
4176 1. .rel.ifunc section in PIC object.
4177 2. .rel.got section in dynamic executable.
4178 3. .rel.iplt section in static executable. */
4179 if (bfd_link_pic (info))
4180 sreloc = htab->elf.irelifunc;
4181 else if (htab->elf.splt != NULL)
4182 sreloc = htab->elf.srelgot;
4184 sreloc = htab->elf.irelplt;
4185 elf_append_rel (output_bfd, sreloc, &outrel);
4187 /* If this reloc is against an external symbol, we
4188 do not want to fiddle with the addend. Otherwise,
4189 we need to include the symbol value so that it
4190 becomes an addend for the dynamic reloc. For an
4191 internal symbol, we have updated addend. */
4200 relocation -= (gotplt->output_section->vma
4201 + gotplt->output_offset);
4206 eh = (struct elf_i386_link_hash_entry *) h;
4207 resolved_to_zero = (eh != NULL
4208 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
4215 /* Avoid optimizing _DYNAMIC since ld.so may use its
4216 link-time address. */
4217 if (h == htab->elf.hdynamic)
4220 if (bfd_link_pic (info))
4222 /* It is OK to convert mov to lea and convert indirect
4223 branch to direct branch. It is OK to convert adc,
4224 add, and, cmp, or, sbb, sub, test, xor only when PIC
4226 unsigned int opcode, addend;
4227 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4230 opcode = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4231 if (opcode != 0x8b && opcode != 0xff)
4235 /* Resolve "mov GOT[(%reg)], %reg",
4236 "call/jmp *GOT[(%reg)]", "test %reg, foo@GOT[(%reg)]"
4237 and "binop foo@GOT[(%reg)], %reg". */
4239 || (h->plt.offset == (bfd_vma) -1
4240 && h->got.offset == (bfd_vma) -1)
4241 || htab->elf.sgotplt == NULL)
4244 offplt = (htab->elf.sgotplt->output_section->vma
4245 + htab->elf.sgotplt->output_offset);
4247 /* It is relative to .got.plt section. */
4248 if (h->got.offset != (bfd_vma) -1)
4249 /* Use GOT entry. Mask off the least significant bit in
4250 GOT offset which may be set by R_386_GOT32 processing
4252 relocation = (htab->elf.sgot->output_section->vma
4253 + htab->elf.sgot->output_offset
4254 + (h->got.offset & ~1) - offplt);
4256 /* Use GOTPLT entry. */
4257 relocation = (h->plt.offset / plt_entry_size - 1 + 3) * 4;
4259 if (!bfd_link_pic (info))
4261 /* If not PIC, add the .got.plt section address for
4262 baseless addressing. */
4264 modrm = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4265 if ((modrm & 0xc7) == 0x5)
4266 relocation += offplt;
4269 unresolved_reloc = FALSE;
4274 /* Relocation is to the entry for this symbol in the global
4276 if (htab->elf.sgot == NULL)
4279 relative_reloc = FALSE;
4284 off = h->got.offset;
4285 dyn = htab->elf.dynamic_sections_created;
4286 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4287 bfd_link_pic (info),
4289 || (bfd_link_pic (info)
4290 && SYMBOL_REFERENCES_LOCAL (info, h))
4291 || (ELF_ST_VISIBILITY (h->other)
4292 && h->root.type == bfd_link_hash_undefweak))
4294 /* This is actually a static link, or it is a
4295 -Bsymbolic link and the symbol is defined
4296 locally, or the symbol was forced to be local
4297 because of a version file. We must initialize
4298 this entry in the global offset table. Since the
4299 offset must always be a multiple of 4, we use the
4300 least significant bit to record whether we have
4301 initialized it already.
4303 When doing a dynamic link, we create a .rel.got
4304 relocation entry to initialize the value. This
4305 is done in the finish_dynamic_symbol routine. */
4310 bfd_put_32 (output_bfd, relocation,
4311 htab->elf.sgot->contents + off);
4314 if (h->dynindx == -1
4316 && h->root.type != bfd_link_hash_undefweak
4317 && bfd_link_pic (info))
4319 /* PR ld/21402: If this symbol isn't dynamic
4320 in PIC, generate R_386_RELATIVE here. */
4321 eh->no_finish_dynamic_symbol = 1;
4322 relative_reloc = TRUE;
4327 unresolved_reloc = FALSE;
4331 if (local_got_offsets == NULL)
4334 off = local_got_offsets[r_symndx];
4336 /* The offset must always be a multiple of 4. We use
4337 the least significant bit to record whether we have
4338 already generated the necessary reloc. */
4343 bfd_put_32 (output_bfd, relocation,
4344 htab->elf.sgot->contents + off);
4345 local_got_offsets[r_symndx] |= 1;
4347 if (bfd_link_pic (info))
4348 relative_reloc = TRUE;
4355 Elf_Internal_Rela outrel;
4357 s = htab->elf.srelgot;
4361 outrel.r_offset = (htab->elf.sgot->output_section->vma
4362 + htab->elf.sgot->output_offset
4364 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4365 elf_append_rel (output_bfd, s, &outrel);
4368 if (off >= (bfd_vma) -2)
4371 relocation = (htab->elf.sgot->output_section->vma
4372 + htab->elf.sgot->output_offset + off);
4373 if (rel->r_offset > 1
4374 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
4375 && *(contents + rel->r_offset - 2) != 0x8d)
4377 if (bfd_link_pic (info))
4379 /* For PIC, disallow R_386_GOT32 without a base
4380 register, except for "lea foo@GOT, %reg", since
4381 we don't know what the GOT base is. */
4385 if (h == NULL || h->root.root.string == NULL)
4386 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4389 name = h->root.root.string;
4392 /* xgettext:c-format */
4393 (_("%B: direct GOT relocation %s against `%s'"
4394 " without base register can not be used"
4395 " when making a shared object"),
4396 input_bfd, howto->name, name);
4397 bfd_set_error (bfd_error_bad_value);
4403 /* Subtract the .got.plt section address only with a base
4405 relocation -= (htab->elf.sgotplt->output_section->vma
4406 + htab->elf.sgotplt->output_offset);
4412 /* Relocation is relative to the start of the global offset
4415 /* Check to make sure it isn't a protected function or data
4416 symbol for shared library since it may not be local when
4417 used as function address or with copy relocation. We also
4418 need to make sure that a symbol is referenced locally. */
4419 if (!bfd_link_executable (info) && h)
4421 if (!h->def_regular)
4425 switch (ELF_ST_VISIBILITY (h->other))
4428 v = _("hidden symbol");
4431 v = _("internal symbol");
4434 v = _("protected symbol");
4442 /* xgettext:c-format */
4443 (_("%B: relocation R_386_GOTOFF against undefined %s"
4444 " `%s' can not be used when making a shared object"),
4445 input_bfd, v, h->root.root.string);
4446 bfd_set_error (bfd_error_bad_value);
4449 else if (!SYMBOL_REFERENCES_LOCAL (info, h)
4450 && (h->type == STT_FUNC
4451 || h->type == STT_OBJECT)
4452 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
4455 /* xgettext:c-format */
4456 (_("%B: relocation R_386_GOTOFF against protected %s"
4457 " `%s' can not be used when making a shared object"),
4459 h->type == STT_FUNC ? "function" : "data",
4460 h->root.root.string);
4461 bfd_set_error (bfd_error_bad_value);
4466 /* Note that sgot is not involved in this
4467 calculation. We always want the start of .got.plt. If we
4468 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
4469 permitted by the ABI, we might have to change this
4471 relocation -= htab->elf.sgotplt->output_section->vma
4472 + htab->elf.sgotplt->output_offset;
4476 /* Use global offset table as symbol value. */
4477 relocation = htab->elf.sgotplt->output_section->vma
4478 + htab->elf.sgotplt->output_offset;
4479 unresolved_reloc = FALSE;
4483 /* Relocation is to the entry for this symbol in the
4484 procedure linkage table. */
4486 /* Resolve a PLT32 reloc against a local symbol directly,
4487 without using the procedure linkage table. */
4491 if ((h->plt.offset == (bfd_vma) -1
4492 && eh->plt_got.offset == (bfd_vma) -1)
4493 || htab->elf.splt == NULL)
4495 /* We didn't make a PLT entry for this symbol. This
4496 happens when statically linking PIC code, or when
4497 using -Bsymbolic. */
4501 if (h->plt.offset != (bfd_vma) -1)
4503 resolved_plt = htab->elf.splt;
4504 plt_offset = h->plt.offset;
4508 resolved_plt = htab->plt_got;
4509 plt_offset = eh->plt_got.offset;
4512 relocation = (resolved_plt->output_section->vma
4513 + resolved_plt->output_offset
4515 unresolved_reloc = FALSE;
4519 /* Set to symbol size. */
4520 relocation = st_size;
4525 if ((input_section->flags & SEC_ALLOC) == 0
4529 /* Copy dynamic function pointer relocations. Don't generate
4530 dynamic relocations against resolved undefined weak symbols
4531 in PIE, except for R_386_PC32. */
4532 if ((bfd_link_pic (info)
4534 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4535 && (!resolved_to_zero
4536 || r_type == R_386_PC32))
4537 || h->root.type != bfd_link_hash_undefweak))
4538 && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
4539 || !SYMBOL_CALLS_LOCAL (info, h)))
4540 || (ELIMINATE_COPY_RELOCS
4541 && !bfd_link_pic (info)
4545 || eh->func_pointer_refcount > 0
4546 || (h->root.type == bfd_link_hash_undefweak
4547 && !resolved_to_zero))
4548 && ((h->def_dynamic && !h->def_regular)
4549 /* Undefined weak symbol is bound locally when
4551 || h->root.type == bfd_link_hash_undefweak)))
4553 Elf_Internal_Rela outrel;
4554 bfd_boolean skip, relocate;
4557 /* When generating a shared object, these relocations
4558 are copied into the output file to be resolved at run
4565 _bfd_elf_section_offset (output_bfd, info, input_section,
4567 if (outrel.r_offset == (bfd_vma) -1)
4569 else if (outrel.r_offset == (bfd_vma) -2)
4570 skip = TRUE, relocate = TRUE;
4571 outrel.r_offset += (input_section->output_section->vma
4572 + input_section->output_offset);
4575 memset (&outrel, 0, sizeof outrel);
4578 && (r_type == R_386_PC32
4579 || !(bfd_link_executable (info)
4580 || SYMBOLIC_BIND (info, h))
4581 || !h->def_regular))
4582 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4585 /* This symbol is local, or marked to become local. */
4587 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4590 sreloc = elf_section_data (input_section)->sreloc;
4592 if (sreloc == NULL || sreloc->contents == NULL)
4594 r = bfd_reloc_notsupported;
4595 goto check_relocation_error;
4598 elf_append_rel (output_bfd, sreloc, &outrel);
4600 /* If this reloc is against an external symbol, we do
4601 not want to fiddle with the addend. Otherwise, we
4602 need to include the symbol value so that it becomes
4603 an addend for the dynamic reloc. */
4610 if (!bfd_link_executable (info))
4612 Elf_Internal_Rela outrel;
4615 outrel.r_offset = rel->r_offset
4616 + input_section->output_section->vma
4617 + input_section->output_offset;
4618 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4619 sreloc = elf_section_data (input_section)->sreloc;
4622 elf_append_rel (output_bfd, sreloc, &outrel);
4627 case R_386_TLS_GOTDESC:
4628 case R_386_TLS_DESC_CALL:
4629 case R_386_TLS_IE_32:
4630 case R_386_TLS_GOTIE:
4631 tls_type = GOT_UNKNOWN;
4632 if (h == NULL && local_got_offsets)
4633 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
4635 tls_type = elf_i386_hash_entry(h)->tls_type;
4636 if (tls_type == GOT_TLS_IE)
4637 tls_type = GOT_TLS_IE_NEG;
4639 if (! elf_i386_tls_transition (info, input_bfd,
4640 input_section, contents,
4641 symtab_hdr, sym_hashes,
4642 &r_type, tls_type, rel,
4643 relend, h, r_symndx, TRUE))
4646 if (r_type == R_386_TLS_LE_32)
4648 BFD_ASSERT (! unresolved_reloc);
4649 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
4654 /* GD->LE transition. */
4655 type = *(contents + rel->r_offset - 2);
4659 leal foo@tlsgd(,%ebx,1), %eax
4660 call ___tls_get_addr@PLT
4663 subl $foo@tpoff, %eax
4664 (6 byte form of subl). */
4665 roff = rel->r_offset + 5;
4670 leal foo@tlsgd(%ebx), %eax
4671 call ___tls_get_addr@PLT
4674 leal foo@tlsgd(%reg), %eax
4675 call *___tls_get_addr@GOT(%reg)
4676 which may be converted to
4677 addr32 call ___tls_get_addr
4679 movl %gs:0, %eax; subl $foo@tpoff, %eax
4680 (6 byte form of subl). */
4681 roff = rel->r_offset + 6;
4683 memcpy (contents + roff - 8,
4684 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
4685 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
4687 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
4692 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
4694 /* GDesc -> LE transition.
4695 It's originally something like:
4696 leal x@tlsdesc(%ebx), %eax
4700 Registers other than %eax may be set up here. */
4705 roff = rel->r_offset;
4706 val = bfd_get_8 (input_bfd, contents + roff - 1);
4708 /* Now modify the instruction as appropriate. */
4709 /* aoliva FIXME: remove the above and xor the byte
4711 bfd_put_8 (output_bfd, val ^ 0x86,
4712 contents + roff - 1);
4713 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4717 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
4719 /* GDesc -> LE transition.
4727 roff = rel->r_offset;
4728 bfd_put_8 (output_bfd, 0x66, contents + roff);
4729 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4732 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
4736 /* IE->LE transition:
4737 Originally it can be one of:
4745 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4748 /* movl foo, %eax. */
4749 bfd_put_8 (output_bfd, 0xb8,
4750 contents + rel->r_offset - 1);
4756 type = bfd_get_8 (input_bfd,
4757 contents + rel->r_offset - 2);
4762 bfd_put_8 (output_bfd, 0xc7,
4763 contents + rel->r_offset - 2);
4764 bfd_put_8 (output_bfd,
4765 0xc0 | ((val >> 3) & 7),
4766 contents + rel->r_offset - 1);
4770 bfd_put_8 (output_bfd, 0x81,
4771 contents + rel->r_offset - 2);
4772 bfd_put_8 (output_bfd,
4773 0xc0 | ((val >> 3) & 7),
4774 contents + rel->r_offset - 1);
4781 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4782 contents + rel->r_offset);
4787 unsigned int val, type;
4789 /* {IE_32,GOTIE}->LE transition:
4790 Originally it can be one of:
4791 subl foo(%reg1), %reg2
4792 movl foo(%reg1), %reg2
4793 addl foo(%reg1), %reg2
4796 movl $foo, %reg2 (6 byte form)
4797 addl $foo, %reg2. */
4798 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4799 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4803 bfd_put_8 (output_bfd, 0xc7,
4804 contents + rel->r_offset - 2);
4805 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4806 contents + rel->r_offset - 1);
4808 else if (type == 0x2b)
4811 bfd_put_8 (output_bfd, 0x81,
4812 contents + rel->r_offset - 2);
4813 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
4814 contents + rel->r_offset - 1);
4816 else if (type == 0x03)
4819 bfd_put_8 (output_bfd, 0x81,
4820 contents + rel->r_offset - 2);
4821 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4822 contents + rel->r_offset - 1);
4826 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
4827 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4828 contents + rel->r_offset);
4830 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
4831 contents + rel->r_offset);
4836 if (htab->elf.sgot == NULL)
4841 off = h->got.offset;
4842 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
4846 if (local_got_offsets == NULL)
4849 off = local_got_offsets[r_symndx];
4850 offplt = local_tlsdesc_gotents[r_symndx];
4857 Elf_Internal_Rela outrel;
4861 if (htab->elf.srelgot == NULL)
4864 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4866 if (GOT_TLS_GDESC_P (tls_type))
4869 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
4870 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
4871 <= htab->elf.sgotplt->size);
4872 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4873 + htab->elf.sgotplt->output_offset
4875 + htab->sgotplt_jump_table_size);
4876 sreloc = htab->elf.srelplt;
4877 loc = sreloc->contents;
4878 loc += (htab->next_tls_desc_index++
4879 * sizeof (Elf32_External_Rel));
4880 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
4881 <= sreloc->contents + sreloc->size);
4882 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4885 BFD_ASSERT (! unresolved_reloc);
4886 bfd_put_32 (output_bfd,
4887 relocation - elf_i386_dtpoff_base (info),
4888 htab->elf.sgotplt->contents + offplt
4889 + htab->sgotplt_jump_table_size + 4);
4893 bfd_put_32 (output_bfd, 0,
4894 htab->elf.sgotplt->contents + offplt
4895 + htab->sgotplt_jump_table_size + 4);
4899 sreloc = htab->elf.srelgot;
4901 outrel.r_offset = (htab->elf.sgot->output_section->vma
4902 + htab->elf.sgot->output_offset + off);
4904 if (GOT_TLS_GD_P (tls_type))
4905 dr_type = R_386_TLS_DTPMOD32;
4906 else if (GOT_TLS_GDESC_P (tls_type))
4908 else if (tls_type == GOT_TLS_IE_POS)
4909 dr_type = R_386_TLS_TPOFF;
4911 dr_type = R_386_TLS_TPOFF32;
4913 if (dr_type == R_386_TLS_TPOFF && indx == 0)
4914 bfd_put_32 (output_bfd,
4915 relocation - elf_i386_dtpoff_base (info),
4916 htab->elf.sgot->contents + off);
4917 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
4918 bfd_put_32 (output_bfd,
4919 elf_i386_dtpoff_base (info) - relocation,
4920 htab->elf.sgot->contents + off);
4921 else if (dr_type != R_386_TLS_DESC)
4922 bfd_put_32 (output_bfd, 0,
4923 htab->elf.sgot->contents + off);
4924 outrel.r_info = ELF32_R_INFO (indx, dr_type);
4926 elf_append_rel (output_bfd, sreloc, &outrel);
4928 if (GOT_TLS_GD_P (tls_type))
4932 BFD_ASSERT (! unresolved_reloc);
4933 bfd_put_32 (output_bfd,
4934 relocation - elf_i386_dtpoff_base (info),
4935 htab->elf.sgot->contents + off + 4);
4939 bfd_put_32 (output_bfd, 0,
4940 htab->elf.sgot->contents + off + 4);
4941 outrel.r_info = ELF32_R_INFO (indx,
4942 R_386_TLS_DTPOFF32);
4943 outrel.r_offset += 4;
4944 elf_append_rel (output_bfd, sreloc, &outrel);
4947 else if (tls_type == GOT_TLS_IE_BOTH)
4949 bfd_put_32 (output_bfd,
4951 ? relocation - elf_i386_dtpoff_base (info)
4953 htab->elf.sgot->contents + off + 4);
4954 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
4955 outrel.r_offset += 4;
4956 elf_append_rel (output_bfd, sreloc, &outrel);
4963 local_got_offsets[r_symndx] |= 1;
4966 if (off >= (bfd_vma) -2
4967 && ! GOT_TLS_GDESC_P (tls_type))
4969 if (r_type == R_386_TLS_GOTDESC
4970 || r_type == R_386_TLS_DESC_CALL)
4972 relocation = htab->sgotplt_jump_table_size + offplt;
4973 unresolved_reloc = FALSE;
4975 else if (r_type == ELF32_R_TYPE (rel->r_info))
4977 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
4978 + htab->elf.sgotplt->output_offset;
4979 relocation = htab->elf.sgot->output_section->vma
4980 + htab->elf.sgot->output_offset + off - g_o_t;
4981 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
4982 && tls_type == GOT_TLS_IE_BOTH)
4984 if (r_type == R_386_TLS_IE)
4985 relocation += g_o_t;
4986 unresolved_reloc = FALSE;
4988 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
4990 unsigned int val, type;
4993 /* GD->IE transition. */
4994 type = *(contents + rel->r_offset - 2);
4995 val = *(contents + rel->r_offset - 1);
4999 leal foo@tlsgd(,%ebx,1), %eax
5000 call ___tls_get_addr@PLT
5003 subl $foo@gottpoff(%ebx), %eax. */
5005 roff = rel->r_offset - 3;
5010 leal foo@tlsgd(%ebx), %eax
5011 call ___tls_get_addr@PLT
5014 leal foo@tlsgd(%reg), %eax
5015 call *___tls_get_addr@GOT(%reg)
5016 which may be converted to
5017 addr32 call ___tls_get_addr
5020 subl $foo@gottpoff(%reg), %eax. */
5021 roff = rel->r_offset - 2;
5023 memcpy (contents + roff,
5024 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
5025 contents[roff + 7] = 0x80 | (val & 7);
5026 /* If foo is used only with foo@gotntpoff(%reg) and
5027 foo@indntpoff, but not with foo@gottpoff(%reg), change
5028 subl $foo@gottpoff(%reg), %eax
5030 addl $foo@gotntpoff(%reg), %eax. */
5031 if (tls_type == GOT_TLS_IE_POS)
5032 contents[roff + 6] = 0x03;
5033 bfd_put_32 (output_bfd,
5034 htab->elf.sgot->output_section->vma
5035 + htab->elf.sgot->output_offset + off
5036 - htab->elf.sgotplt->output_section->vma
5037 - htab->elf.sgotplt->output_offset,
5038 contents + roff + 8);
5039 /* Skip R_386_PLT32 and R_386_GOT32X. */
5044 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
5046 /* GDesc -> IE transition.
5047 It's originally something like:
5048 leal x@tlsdesc(%ebx), %eax
5051 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
5053 movl x@gottpoff(%ebx), %eax # before negl %eax
5055 Registers other than %eax may be set up here. */
5059 /* First, make sure it's a leal adding ebx to a 32-bit
5060 offset into any register, although it's probably
5061 almost always going to be eax. */
5062 roff = rel->r_offset;
5064 /* Now modify the instruction as appropriate. */
5065 /* To turn a leal into a movl in the form we use it, it
5066 suffices to change the first byte from 0x8d to 0x8b.
5067 aoliva FIXME: should we decide to keep the leal, all
5068 we have to do is remove the statement below, and
5069 adjust the relaxation of R_386_TLS_DESC_CALL. */
5070 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
5072 if (tls_type == GOT_TLS_IE_BOTH)
5075 bfd_put_32 (output_bfd,
5076 htab->elf.sgot->output_section->vma
5077 + htab->elf.sgot->output_offset + off
5078 - htab->elf.sgotplt->output_section->vma
5079 - htab->elf.sgotplt->output_offset,
5083 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
5085 /* GDesc -> IE transition.
5093 depending on how we transformed the TLS_GOTDESC above.
5098 roff = rel->r_offset;
5100 /* Now modify the instruction as appropriate. */
5101 if (tls_type != GOT_TLS_IE_NEG)
5104 bfd_put_8 (output_bfd, 0x66, contents + roff);
5105 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5110 bfd_put_8 (output_bfd, 0xf7, contents + roff);
5111 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
5121 if (! elf_i386_tls_transition (info, input_bfd,
5122 input_section, contents,
5123 symtab_hdr, sym_hashes,
5124 &r_type, GOT_UNKNOWN, rel,
5125 relend, h, r_symndx, TRUE))
5128 if (r_type != R_386_TLS_LDM)
5130 /* LD->LE transition. Change
5131 leal foo@tlsldm(%ebx) %eax
5132 call ___tls_get_addr@PLT
5136 leal 0(%esi,1), %esi
5138 leal foo@tlsldm(%reg) %eax
5139 call *___tls_get_addr@GOT(%reg)
5140 which may be converted to
5141 addr32 call ___tls_get_addr
5144 leal 0(%esi), %esi */
5145 BFD_ASSERT (r_type == R_386_TLS_LE_32);
5146 if (*(contents + rel->r_offset + 4) == 0xff
5147 || *(contents + rel->r_offset + 4) == 0x67)
5148 memcpy (contents + rel->r_offset - 2,
5149 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
5151 memcpy (contents + rel->r_offset - 2,
5152 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
5153 /* Skip R_386_PC32/R_386_PLT32. */
5159 if (htab->elf.sgot == NULL)
5162 off = htab->tls_ldm_got.offset;
5167 Elf_Internal_Rela outrel;
5169 if (htab->elf.srelgot == NULL)
5172 outrel.r_offset = (htab->elf.sgot->output_section->vma
5173 + htab->elf.sgot->output_offset + off);
5175 bfd_put_32 (output_bfd, 0,
5176 htab->elf.sgot->contents + off);
5177 bfd_put_32 (output_bfd, 0,
5178 htab->elf.sgot->contents + off + 4);
5179 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
5180 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
5181 htab->tls_ldm_got.offset |= 1;
5183 relocation = htab->elf.sgot->output_section->vma
5184 + htab->elf.sgot->output_offset + off
5185 - htab->elf.sgotplt->output_section->vma
5186 - htab->elf.sgotplt->output_offset;
5187 unresolved_reloc = FALSE;
5190 case R_386_TLS_LDO_32:
5191 if (!bfd_link_executable (info)
5192 || (input_section->flags & SEC_CODE) == 0)
5193 relocation -= elf_i386_dtpoff_base (info);
5195 /* When converting LDO to LE, we must negate. */
5196 relocation = -elf_i386_tpoff (info, relocation);
5199 case R_386_TLS_LE_32:
5201 if (!bfd_link_executable (info))
5203 Elf_Internal_Rela outrel;
5206 outrel.r_offset = rel->r_offset
5207 + input_section->output_section->vma
5208 + input_section->output_offset;
5209 if (h != NULL && h->dynindx != -1)
5213 if (r_type == R_386_TLS_LE_32)
5214 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
5216 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
5217 sreloc = elf_section_data (input_section)->sreloc;
5220 elf_append_rel (output_bfd, sreloc, &outrel);
5223 else if (r_type == R_386_TLS_LE_32)
5224 relocation = elf_i386_dtpoff_base (info) - relocation;
5226 relocation -= elf_i386_dtpoff_base (info);
5228 else if (r_type == R_386_TLS_LE_32)
5229 relocation = elf_i386_tpoff (info, relocation);
5231 relocation = -elf_i386_tpoff (info, relocation);
5238 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5239 because such sections are not SEC_ALLOC and thus ld.so will
5240 not process them. */
5241 if (unresolved_reloc
5242 && !((input_section->flags & SEC_DEBUGGING) != 0
5244 && _bfd_elf_section_offset (output_bfd, info, input_section,
5245 rel->r_offset) != (bfd_vma) -1)
5248 /* xgettext:c-format */
5249 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5252 (long) rel->r_offset,
5254 h->root.root.string);
5259 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5260 contents, rel->r_offset,
5263 check_relocation_error:
5264 if (r != bfd_reloc_ok)
5269 name = h->root.root.string;
5272 name = bfd_elf_string_from_elf_section (input_bfd,
5273 symtab_hdr->sh_link,
5278 name = bfd_section_name (input_bfd, sec);
5281 if (r == bfd_reloc_overflow)
5282 (*info->callbacks->reloc_overflow)
5283 (info, (h ? &h->root : NULL), name, howto->name,
5284 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5288 /* xgettext:c-format */
5289 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
5290 input_bfd, input_section,
5291 (long) rel->r_offset, name, (int) r);
5302 Elf_Internal_Shdr *rel_hdr;
5303 size_t deleted = rel - wrel;
5305 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5306 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5307 if (rel_hdr->sh_size == 0)
5309 /* It is too late to remove an empty reloc section. Leave
5311 ??? What is wrong with an empty section??? */
5312 rel_hdr->sh_size = rel_hdr->sh_entsize;
5315 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5316 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5317 input_section->reloc_count -= deleted;
5323 /* Finish up dynamic symbol handling. We set the contents of various
5324 dynamic sections here. */
5327 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
5328 struct bfd_link_info *info,
5329 struct elf_link_hash_entry *h,
5330 Elf_Internal_Sym *sym)
5332 struct elf_i386_link_hash_table *htab;
5333 unsigned plt_entry_size;
5334 const struct elf_i386_backend_data *abed;
5335 struct elf_i386_link_hash_entry *eh;
5336 bfd_boolean local_undefweak;
5338 htab = elf_i386_hash_table (info);
5342 abed = get_elf_i386_backend_data (output_bfd);
5343 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
5345 eh = (struct elf_i386_link_hash_entry *) h;
5346 if (eh->no_finish_dynamic_symbol)
5349 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5350 resolved undefined weak symbols in executable so that their
5351 references have value 0 at run-time. */
5352 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
5356 if (h->plt.offset != (bfd_vma) -1)
5360 Elf_Internal_Rela rel;
5362 asection *plt, *gotplt, *relplt;
5364 /* When building a static executable, use .iplt, .igot.plt and
5365 .rel.iplt sections for STT_GNU_IFUNC symbols. */
5366 if (htab->elf.splt != NULL)
5368 plt = htab->elf.splt;
5369 gotplt = htab->elf.sgotplt;
5370 relplt = htab->elf.srelplt;
5374 plt = htab->elf.iplt;
5375 gotplt = htab->elf.igotplt;
5376 relplt = htab->elf.irelplt;
5379 /* This symbol has an entry in the procedure linkage table. Set
5382 if ((h->dynindx == -1
5384 && !((h->forced_local || bfd_link_executable (info))
5386 && h->type == STT_GNU_IFUNC))
5392 /* Get the index in the procedure linkage table which
5393 corresponds to this symbol. This is the index of this symbol
5394 in all the symbols for which we are making plt entries. The
5395 first entry in the procedure linkage table is reserved.
5397 Get the offset into the .got table of the entry that
5398 corresponds to this function. Each .got entry is 4 bytes.
5399 The first three are reserved.
5401 For static executables, we don't reserve anything. */
5403 if (plt == htab->elf.splt)
5405 got_offset = h->plt.offset / plt_entry_size - 1;
5406 got_offset = (got_offset + 3) * 4;
5410 got_offset = h->plt.offset / plt_entry_size;
5411 got_offset = got_offset * 4;
5414 /* Fill in the entry in the procedure linkage table. */
5415 if (! bfd_link_pic (info))
5417 memcpy (plt->contents + h->plt.offset, abed->plt->plt_entry,
5418 abed->plt->plt_entry_size);
5419 bfd_put_32 (output_bfd,
5420 (gotplt->output_section->vma
5421 + gotplt->output_offset
5423 plt->contents + h->plt.offset
5424 + abed->plt->plt_got_offset);
5426 if (abed->is_vxworks)
5428 int s, k, reloc_index;
5430 /* Create the R_386_32 relocation referencing the GOT
5431 for this PLT entry. */
5433 /* S: Current slot number (zero-based). */
5434 s = ((h->plt.offset - abed->plt->plt_entry_size)
5435 / abed->plt->plt_entry_size);
5436 /* K: Number of relocations for PLTResolve. */
5437 if (bfd_link_pic (info))
5438 k = PLTRESOLVE_RELOCS_SHLIB;
5440 k = PLTRESOLVE_RELOCS;
5441 /* Skip the PLTresolve relocations, and the relocations for
5442 the other PLT slots. */
5443 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
5444 loc = (htab->srelplt2->contents + reloc_index
5445 * sizeof (Elf32_External_Rel));
5447 rel.r_offset = (htab->elf.splt->output_section->vma
5448 + htab->elf.splt->output_offset
5449 + h->plt.offset + 2),
5450 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5451 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5453 /* Create the R_386_32 relocation referencing the beginning of
5454 the PLT for this GOT entry. */
5455 rel.r_offset = (htab->elf.sgotplt->output_section->vma
5456 + htab->elf.sgotplt->output_offset
5458 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
5459 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5460 loc + sizeof (Elf32_External_Rel));
5465 memcpy (plt->contents + h->plt.offset, abed->plt->pic_plt_entry,
5466 abed->plt->plt_entry_size);
5467 bfd_put_32 (output_bfd, got_offset,
5468 plt->contents + h->plt.offset
5469 + abed->plt->plt_got_offset);
5472 /* Fill in the entry in the global offset table. Leave the entry
5473 as zero for undefined weak symbol in PIE. No PLT relocation
5474 against undefined weak symbol in PIE. */
5475 if (!local_undefweak)
5477 bfd_put_32 (output_bfd,
5478 (plt->output_section->vma
5479 + plt->output_offset
5481 + abed->plt->plt_lazy_offset),
5482 gotplt->contents + got_offset);
5484 /* Fill in the entry in the .rel.plt section. */
5485 rel.r_offset = (gotplt->output_section->vma
5486 + gotplt->output_offset
5488 if (h->dynindx == -1
5489 || ((bfd_link_executable (info)
5490 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5492 && h->type == STT_GNU_IFUNC))
5494 /* If an STT_GNU_IFUNC symbol is locally defined, generate
5495 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
5496 in the .got.plt section. */
5497 bfd_put_32 (output_bfd,
5498 (h->root.u.def.value
5499 + h->root.u.def.section->output_section->vma
5500 + h->root.u.def.section->output_offset),
5501 gotplt->contents + got_offset);
5502 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
5503 /* R_386_IRELATIVE comes last. */
5504 plt_index = htab->next_irelative_index--;
5508 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
5509 plt_index = htab->next_jump_slot_index++;
5512 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
5513 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5515 /* Don't fill PLT entry for static executables. */
5516 if (plt == htab->elf.splt)
5518 bfd_put_32 (output_bfd,
5519 plt_index * sizeof (Elf32_External_Rel),
5520 plt->contents + h->plt.offset
5521 + abed->plt->plt_reloc_offset);
5522 bfd_put_32 (output_bfd, - (h->plt.offset
5523 + abed->plt->plt_plt_offset + 4),
5524 plt->contents + h->plt.offset
5525 + abed->plt->plt_plt_offset);
5529 else if (eh->plt_got.offset != (bfd_vma) -1)
5531 bfd_vma got_offset, plt_offset;
5532 asection *plt, *got, *gotplt;
5533 const bfd_byte *got_plt_entry;
5535 /* Offset of displacement of the indirect jump. */
5536 bfd_vma plt_got_offset = 2;
5538 /* Set the entry in the GOT procedure linkage table. */
5539 plt = htab->plt_got;
5540 got = htab->elf.sgot;
5541 gotplt = htab->elf.sgotplt;
5542 got_offset = h->got.offset;
5544 if (got_offset == (bfd_vma) -1
5550 /* Fill in the entry in the GOT procedure linkage table. */
5551 if (! bfd_link_pic (info))
5553 got_plt_entry = elf_i386_got_plt_entry;
5554 got_offset += got->output_section->vma + got->output_offset;
5558 got_plt_entry = elf_i386_pic_got_plt_entry;
5559 got_offset += (got->output_section->vma
5560 + got->output_offset
5561 - gotplt->output_section->vma
5562 - gotplt->output_offset);
5565 plt_offset = eh->plt_got.offset;
5566 memcpy (plt->contents + plt_offset, got_plt_entry,
5567 sizeof (elf_i386_got_plt_entry));
5568 bfd_put_32 (output_bfd, got_offset,
5569 plt->contents + plt_offset + plt_got_offset);
5572 if (!local_undefweak
5574 && (h->plt.offset != (bfd_vma) -1
5575 || eh->plt_got.offset != (bfd_vma) -1))
5577 /* Mark the symbol as undefined, rather than as defined in
5578 the .plt section. Leave the value if there were any
5579 relocations where pointer equality matters (this is a clue
5580 for the dynamic linker, to make function pointer
5581 comparisons work between an application and shared
5582 library), otherwise set it to zero. If a function is only
5583 called from a binary, there is no need to slow down
5584 shared libraries because of that. */
5585 sym->st_shndx = SHN_UNDEF;
5586 if (!h->pointer_equality_needed)
5590 /* Don't generate dynamic GOT relocation against undefined weak
5591 symbol in executable. */
5592 if (h->got.offset != (bfd_vma) -1
5593 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
5594 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
5595 && !local_undefweak)
5597 Elf_Internal_Rela rel;
5598 asection *relgot = htab->elf.srelgot;
5600 /* This symbol has an entry in the global offset table. Set it
5603 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
5606 rel.r_offset = (htab->elf.sgot->output_section->vma
5607 + htab->elf.sgot->output_offset
5608 + (h->got.offset & ~(bfd_vma) 1));
5610 /* If this is a static link, or it is a -Bsymbolic link and the
5611 symbol is defined locally or was forced to be local because
5612 of a version file, we just want to emit a RELATIVE reloc.
5613 The entry in the global offset table will already have been
5614 initialized in the relocate_section function. */
5616 && h->type == STT_GNU_IFUNC)
5618 if (h->plt.offset == (bfd_vma) -1)
5620 /* STT_GNU_IFUNC is referenced without PLT. */
5621 if (htab->elf.splt == NULL)
5623 /* use .rel[a].iplt section to store .got relocations
5624 in static executable. */
5625 relgot = htab->elf.irelplt;
5627 if (SYMBOL_REFERENCES_LOCAL (info, h))
5629 bfd_put_32 (output_bfd,
5630 (h->root.u.def.value
5631 + h->root.u.def.section->output_section->vma
5632 + h->root.u.def.section->output_offset),
5633 htab->elf.sgot->contents + h->got.offset);
5634 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
5639 else if (bfd_link_pic (info))
5641 /* Generate R_386_GLOB_DAT. */
5648 if (!h->pointer_equality_needed)
5651 /* For non-shared object, we can't use .got.plt, which
5652 contains the real function addres if we need pointer
5653 equality. We load the GOT entry with the PLT entry. */
5654 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
5655 bfd_put_32 (output_bfd,
5656 (plt->output_section->vma
5657 + plt->output_offset + h->plt.offset),
5658 htab->elf.sgot->contents + h->got.offset);
5662 else if (bfd_link_pic (info)
5663 && SYMBOL_REFERENCES_LOCAL (info, h))
5665 BFD_ASSERT((h->got.offset & 1) != 0);
5666 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
5670 BFD_ASSERT((h->got.offset & 1) == 0);
5672 bfd_put_32 (output_bfd, (bfd_vma) 0,
5673 htab->elf.sgot->contents + h->got.offset);
5674 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
5677 elf_append_rel (output_bfd, relgot, &rel);
5682 Elf_Internal_Rela rel;
5685 /* This symbol needs a copy reloc. Set it up. */
5687 if (h->dynindx == -1
5688 || (h->root.type != bfd_link_hash_defined
5689 && h->root.type != bfd_link_hash_defweak)
5690 || htab->elf.srelbss == NULL
5691 || htab->elf.sreldynrelro == NULL)
5694 rel.r_offset = (h->root.u.def.value
5695 + h->root.u.def.section->output_section->vma
5696 + h->root.u.def.section->output_offset);
5697 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
5698 if (h->root.u.def.section == htab->elf.sdynrelro)
5699 s = htab->elf.sreldynrelro;
5701 s = htab->elf.srelbss;
5702 elf_append_rel (output_bfd, s, &rel);
5708 /* Finish up local dynamic symbol handling. We set the contents of
5709 various dynamic sections here. */
5712 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
5714 struct elf_link_hash_entry *h
5715 = (struct elf_link_hash_entry *) *slot;
5716 struct bfd_link_info *info
5717 = (struct bfd_link_info *) inf;
5719 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
5723 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
5724 here since undefined weak symbol may not be dynamic and may not be
5725 called for elf_i386_finish_dynamic_symbol. */
5728 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
5731 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
5732 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5734 if (h->root.type != bfd_link_hash_undefweak
5735 || h->dynindx != -1)
5738 return elf_i386_finish_dynamic_symbol (info->output_bfd,
5742 /* Used to decide how to sort relocs in an optimal manner for the
5743 dynamic linker, before writing them out. */
5745 static enum elf_reloc_type_class
5746 elf_i386_reloc_type_class (const struct bfd_link_info *info,
5747 const asection *rel_sec ATTRIBUTE_UNUSED,
5748 const Elf_Internal_Rela *rela)
5750 bfd *abfd = info->output_bfd;
5751 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5752 struct elf_link_hash_table *htab = elf_hash_table (info);
5754 if (htab->dynsym != NULL
5755 && htab->dynsym->contents != NULL)
5757 /* Check relocation against STT_GNU_IFUNC symbol if there are
5759 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
5760 if (r_symndx != STN_UNDEF)
5762 Elf_Internal_Sym sym;
5763 if (!bed->s->swap_symbol_in (abfd,
5764 (htab->dynsym->contents
5765 + r_symndx * sizeof (Elf32_External_Sym)),
5769 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
5770 return reloc_class_ifunc;
5774 switch (ELF32_R_TYPE (rela->r_info))
5776 case R_386_IRELATIVE:
5777 return reloc_class_ifunc;
5778 case R_386_RELATIVE:
5779 return reloc_class_relative;
5780 case R_386_JUMP_SLOT:
5781 return reloc_class_plt;
5783 return reloc_class_copy;
5785 return reloc_class_normal;
5789 /* Finish up the dynamic sections. */
5792 elf_i386_finish_dynamic_sections (bfd *output_bfd,
5793 struct bfd_link_info *info)
5795 struct elf_i386_link_hash_table *htab;
5798 const struct elf_i386_backend_data *abed;
5800 htab = elf_i386_hash_table (info);
5804 dynobj = htab->elf.dynobj;
5805 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5806 abed = get_elf_i386_backend_data (output_bfd);
5808 if (htab->elf.dynamic_sections_created)
5810 Elf32_External_Dyn *dyncon, *dynconend;
5812 if (sdyn == NULL || htab->elf.sgot == NULL)
5815 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5816 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5817 for (; dyncon < dynconend; dyncon++)
5819 Elf_Internal_Dyn dyn;
5822 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5827 if (abed->is_vxworks
5828 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
5833 s = htab->elf.sgotplt;
5834 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5838 s = htab->elf.srelplt;
5839 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5843 s = htab->elf.srelplt;
5844 dyn.d_un.d_val = s->size;
5848 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5851 /* Fill in the first entry in the procedure linkage table. */
5852 if (htab->elf.splt && htab->elf.splt->size > 0)
5854 if (bfd_link_pic (info))
5856 memcpy (htab->elf.splt->contents, abed->plt->pic_plt0_entry,
5857 abed->plt->plt0_entry_size);
5858 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
5859 abed->plt0_pad_byte,
5860 abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
5864 memcpy (htab->elf.splt->contents, abed->plt->plt0_entry,
5865 abed->plt->plt0_entry_size);
5866 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
5867 abed->plt0_pad_byte,
5868 abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
5869 bfd_put_32 (output_bfd,
5870 (htab->elf.sgotplt->output_section->vma
5871 + htab->elf.sgotplt->output_offset
5873 htab->elf.splt->contents
5874 + abed->plt->plt0_got1_offset);
5875 bfd_put_32 (output_bfd,
5876 (htab->elf.sgotplt->output_section->vma
5877 + htab->elf.sgotplt->output_offset
5879 htab->elf.splt->contents
5880 + abed->plt->plt0_got2_offset);
5882 if (abed->is_vxworks)
5884 Elf_Internal_Rela rel;
5886 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
5887 On IA32 we use REL relocations so the addend goes in
5888 the PLT directly. */
5889 rel.r_offset = (htab->elf.splt->output_section->vma
5890 + htab->elf.splt->output_offset
5891 + abed->plt->plt0_got1_offset);
5892 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5893 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5894 htab->srelplt2->contents);
5895 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
5896 rel.r_offset = (htab->elf.splt->output_section->vma
5897 + htab->elf.splt->output_offset
5898 + abed->plt->plt0_got2_offset);
5899 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5900 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5901 htab->srelplt2->contents +
5902 sizeof (Elf32_External_Rel));
5906 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5907 really seem like the right value. */
5908 elf_section_data (htab->elf.splt->output_section)
5909 ->this_hdr.sh_entsize = 4;
5911 /* Correct the .rel.plt.unloaded relocations. */
5912 if (abed->is_vxworks && !bfd_link_pic (info))
5914 int num_plts = (htab->elf.splt->size
5915 / abed->plt->plt_entry_size) - 1;
5918 p = htab->srelplt2->contents;
5919 if (bfd_link_pic (info))
5920 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
5922 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
5924 for (; num_plts; num_plts--)
5926 Elf_Internal_Rela rel;
5927 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
5928 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5929 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
5930 p += sizeof (Elf32_External_Rel);
5932 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
5933 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
5934 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
5935 p += sizeof (Elf32_External_Rel);
5941 if (htab->elf.sgotplt)
5943 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
5946 (_("discarded output section: `%A'"), htab->elf.sgotplt);
5950 /* Fill in the first three entries in the global offset table. */
5951 if (htab->elf.sgotplt->size > 0)
5953 bfd_put_32 (output_bfd,
5955 : sdyn->output_section->vma + sdyn->output_offset),
5956 htab->elf.sgotplt->contents);
5957 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
5958 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
5961 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
5964 /* Adjust .eh_frame for .plt section. */
5965 if (htab->plt_eh_frame != NULL
5966 && htab->plt_eh_frame->contents != NULL)
5968 if (htab->elf.splt != NULL
5969 && htab->elf.splt->size != 0
5970 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
5971 && htab->elf.splt->output_section != NULL
5972 && htab->plt_eh_frame->output_section != NULL)
5974 bfd_vma plt_start = htab->elf.splt->output_section->vma;
5975 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
5976 + htab->plt_eh_frame->output_offset
5977 + PLT_FDE_START_OFFSET;
5978 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5979 htab->plt_eh_frame->contents
5980 + PLT_FDE_START_OFFSET);
5982 if (htab->plt_eh_frame->sec_info_type
5983 == SEC_INFO_TYPE_EH_FRAME)
5985 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5987 htab->plt_eh_frame->contents))
5992 /* Adjust .eh_frame for .plt.got section. */
5993 if (htab->plt_got_eh_frame != NULL
5994 && htab->plt_got_eh_frame->contents != NULL)
5996 if (htab->plt_got != NULL
5997 && htab->plt_got->size != 0
5998 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
5999 && htab->plt_got->output_section != NULL
6000 && htab->plt_got_eh_frame->output_section != NULL)
6002 bfd_vma plt_start = htab->plt_got->output_section->vma;
6003 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
6004 + htab->plt_got_eh_frame->output_offset
6005 + PLT_FDE_START_OFFSET;
6006 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6007 htab->plt_got_eh_frame->contents
6008 + PLT_FDE_START_OFFSET);
6010 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6012 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6013 htab->plt_got_eh_frame,
6014 htab->plt_got_eh_frame->contents))
6019 if (htab->elf.sgot && htab->elf.sgot->size > 0)
6020 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
6022 /* Fill PLT entries for undefined weak symbols in PIE. */
6023 if (bfd_link_pie (info))
6024 bfd_hash_traverse (&info->hash->table,
6025 elf_i386_pie_finish_undefweak_symbol,
6031 /* Fill PLT/GOT entries and allocate dynamic relocations for local
6032 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
6033 It has to be done before elf_link_sort_relocs is called so that
6034 dynamic relocations are properly sorted. */
6037 elf_i386_output_arch_local_syms
6038 (bfd *output_bfd ATTRIBUTE_UNUSED,
6039 struct bfd_link_info *info,
6040 void *flaginfo ATTRIBUTE_UNUSED,
6041 int (*func) (void *, const char *,
6044 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
6046 struct elf_i386_link_hash_table *htab = elf_i386_hash_table (info);
6050 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
6051 htab_traverse (htab->loc_hash_table,
6052 elf_i386_finish_local_dynamic_symbol,
6058 /* Return an array of PLT entry symbol values. */
6061 elf_i386_get_plt_sym_val (bfd *abfd, asymbol **dynsyms, asection *plt,
6064 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6067 bfd_vma *plt_sym_val;
6069 bfd_byte *plt_contents;
6070 const struct elf_i386_backend_data *bed
6071 = get_elf_i386_backend_data (abfd);
6072 Elf_Internal_Shdr *hdr;
6074 /* Get the .plt section contents. */
6075 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
6076 if (plt_contents == NULL)
6078 if (!bfd_get_section_contents (abfd, (asection *) plt,
6079 plt_contents, 0, plt->size))
6082 free (plt_contents);
6086 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6087 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
6090 hdr = &elf_section_data (relplt)->this_hdr;
6091 count = relplt->size / hdr->sh_entsize;
6093 plt_sym_val = (bfd_vma *) bfd_malloc (sizeof (bfd_vma) * count);
6094 if (plt_sym_val == NULL)
6097 for (i = 0; i < count; i++)
6098 plt_sym_val[i] = -1;
6100 plt_offset = bed->plt->plt_entry_size;
6101 p = relplt->relocation;
6102 for (i = 0; i < count; i++, p++)
6106 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
6107 if (p->howto == NULL)
6110 if (p->howto->type != R_386_JUMP_SLOT
6111 && p->howto->type != R_386_IRELATIVE)
6114 reloc_index = H_GET_32 (abfd, (plt_contents + plt_offset
6115 + bed->plt->plt_reloc_offset));
6116 reloc_index /= sizeof (Elf32_External_Rel);
6117 if (reloc_index < count)
6118 plt_sym_val[reloc_index] = plt->vma + plt_offset;
6120 plt_offset += bed->plt->plt_entry_size;
6122 /* PR binutils/18437: Skip extra relocations in the .rel.plt
6124 if (plt_offset >= plt->size)
6128 free (plt_contents);
6133 /* Similar to _bfd_elf_get_synthetic_symtab. */
6136 elf_i386_get_synthetic_symtab (bfd *abfd,
6143 asection *plt = bfd_get_section_by_name (abfd, ".plt");
6144 return _bfd_elf_ifunc_get_synthetic_symtab (abfd, symcount, syms,
6145 dynsymcount, dynsyms, ret,
6147 elf_i386_get_plt_sym_val);
6150 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6153 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
6155 if (h->plt.offset != (bfd_vma) -1
6157 && !h->pointer_equality_needed)
6160 return _bfd_elf_hash_symbol (h);
6163 /* Parse i386 GNU properties. */
6165 static enum elf_property_kind
6166 elf_i386_parse_gnu_properties (bfd *abfd, unsigned int type,
6167 bfd_byte *ptr, unsigned int datasz)
6173 case GNU_PROPERTY_X86_ISA_1_USED:
6174 case GNU_PROPERTY_X86_ISA_1_NEEDED:
6178 ((type == GNU_PROPERTY_X86_ISA_1_USED
6179 ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
6180 : _("error: %B: <corrupt x86 ISA needed size: 0x%x>")),
6182 return property_corrupt;
6184 prop = _bfd_elf_get_property (abfd, type, datasz);
6185 prop->u.number = bfd_h_get_32 (abfd, ptr);
6186 prop->pr_kind = property_number;
6190 return property_ignored;
6193 return property_number;
6196 /* Merge i386 GNU property BPROP with APROP. If APROP isn't NULL,
6197 return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
6198 should be merged with ABFD. */
6201 elf_i386_merge_gnu_properties (bfd *abfd ATTRIBUTE_UNUSED,
6202 elf_property *aprop,
6203 elf_property *bprop)
6205 unsigned int number;
6206 bfd_boolean updated = FALSE;
6207 unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
6211 case GNU_PROPERTY_X86_ISA_1_USED:
6212 case GNU_PROPERTY_X86_ISA_1_NEEDED:
6213 if (aprop != NULL && bprop != NULL)
6215 number = aprop->u.number;
6216 aprop->u.number = number | bprop->u.number;
6217 updated = number != (unsigned int) aprop->u.number;
6221 /* Return TRUE if APROP is NULL to indicate that BPROP should
6222 be added to ABFD. */
6223 updated = aprop == NULL;
6228 /* Never should happen. */
6235 #define TARGET_LITTLE_SYM i386_elf32_vec
6236 #define TARGET_LITTLE_NAME "elf32-i386"
6237 #define ELF_ARCH bfd_arch_i386
6238 #define ELF_TARGET_ID I386_ELF_DATA
6239 #define ELF_MACHINE_CODE EM_386
6240 #define ELF_MAXPAGESIZE 0x1000
6242 #define elf_backend_can_gc_sections 1
6243 #define elf_backend_can_refcount 1
6244 #define elf_backend_want_got_plt 1
6245 #define elf_backend_plt_readonly 1
6246 #define elf_backend_want_plt_sym 0
6247 #define elf_backend_got_header_size 12
6248 #define elf_backend_plt_alignment 4
6249 #define elf_backend_dtrel_excludes_plt 1
6250 #define elf_backend_extern_protected_data 1
6251 #define elf_backend_caches_rawsize 1
6252 #define elf_backend_want_dynrelro 1
6254 /* Support RELA for objdump of prelink objects. */
6255 #define elf_info_to_howto elf_i386_info_to_howto_rel
6256 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
6258 #define bfd_elf32_mkobject elf_i386_mkobject
6260 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
6261 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
6262 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
6263 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
6264 #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
6266 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
6267 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
6268 #define elf_backend_check_relocs elf_i386_check_relocs
6269 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
6270 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
6271 #define elf_backend_fake_sections elf_i386_fake_sections
6272 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
6273 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
6274 #define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
6275 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
6276 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
6277 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
6278 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
6279 #define elf_backend_relocate_section elf_i386_relocate_section
6280 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
6281 #define elf_backend_always_size_sections elf_i386_always_size_sections
6282 #define elf_backend_omit_section_dynsym \
6283 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
6284 #define elf_backend_hash_symbol elf_i386_hash_symbol
6285 #define elf_backend_fixup_symbol elf_i386_fixup_symbol
6286 #define elf_backend_parse_gnu_properties elf_i386_parse_gnu_properties
6287 #define elf_backend_merge_gnu_properties elf_i386_merge_gnu_properties
6289 #include "elf32-target.h"
6291 /* FreeBSD support. */
6293 #undef TARGET_LITTLE_SYM
6294 #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
6295 #undef TARGET_LITTLE_NAME
6296 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
6298 #define ELF_OSABI ELFOSABI_FREEBSD
6300 /* The kernel recognizes executables as valid only if they carry a
6301 "FreeBSD" label in the ELF header. So we put this label on all
6302 executables and (for simplicity) also all other object files. */
6305 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
6307 _bfd_elf_post_process_headers (abfd, info);
6309 #ifdef OLD_FREEBSD_ABI_LABEL
6311 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
6312 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6313 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
6318 #undef elf_backend_post_process_headers
6319 #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
6321 #define elf32_bed elf32_i386_fbsd_bed
6323 #undef elf_backend_add_symbol_hook
6325 #include "elf32-target.h"
6329 #undef TARGET_LITTLE_SYM
6330 #define TARGET_LITTLE_SYM i386_elf32_sol2_vec
6331 #undef TARGET_LITTLE_NAME
6332 #define TARGET_LITTLE_NAME "elf32-i386-sol2"
6334 #undef elf_backend_post_process_headers
6336 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
6337 objects won't be recognized. */
6341 #define elf32_bed elf32_i386_sol2_bed
6343 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
6345 #undef elf_backend_static_tls_alignment
6346 #define elf_backend_static_tls_alignment 8
6348 /* The Solaris 2 ABI requires a plt symbol on all platforms.
6350 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
6352 #undef elf_backend_want_plt_sym
6353 #define elf_backend_want_plt_sym 1
6355 #undef elf_backend_strtab_flags
6356 #define elf_backend_strtab_flags SHF_STRINGS
6358 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
6359 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
6360 FALSE otherwise. ISECTION is the best guess matching section from the
6361 input bfd IBFD, but it might be NULL. */
6364 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
6365 bfd *obfd ATTRIBUTE_UNUSED,
6366 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
6367 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
6369 /* PR 19938: FIXME: Need to add code for setting the sh_info
6370 and sh_link fields of Solaris specific section types. */
6373 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
6374 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
6376 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
6378 The following values should be set:
6381 -----------------------------------------------------------------------------
6382 SHT_SUNW_ancillary The section header index of 0
6383 [0x6fffffee] the associated string table.
6385 SHT_SUNW_capinfo The section header index of For a dynamic object, the
6386 [0x6ffffff0] the associated symbol table. section header index of
6388 SHT_SUNW_capchain table,
6391 SHT_SUNW_symsort The section header index of 0
6392 [0x6ffffff1] the associated symbol table.
6394 SHT_SUNW_tlssort The section header index of 0
6395 [0x6ffffff2] the associated symbol table.
6397 SHT_SUNW_LDYNSYM The section header index of One greater than the
6398 [0x6ffffff3] the associated string table. symbol table index of the
6399 This index is the same string last local symbol,
6400 table used by the SHT_DYNSYM STB_LOCAL. Since
6401 section. SHT_SUNW_LDYNSYM only
6402 contains local symbols,
6403 sh_info is equivalent to
6404 the number of symbols in
6407 SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
6408 [0x6ffffff5] the section header index of to named strings, the
6409 the associated section header index of
6410 SHT_SUNW_capinfo table, the associated string
6411 otherwise 0. table, otherwise 0.
6413 SHT_SUNW_move The section header index of 0
6414 [0x6ffffffa] the associated symbol table.
6419 SHT_SUNW_syminfo The section header index of The section header index
6420 [0x6ffffffc] the associated symbol table. of the associated
6423 SHT_SUNW_verdef The section header index of The number of version
6424 [0x6ffffffd] the associated string table. definitions within the
6427 SHT_SUNW_verneed The section header index of The number of version
6428 [0x6ffffffe] the associated string table. dependencies within the
6431 SHT_SUNW_versym The section header index of 0
6432 [0x6fffffff] the associated symbol table. */
6435 #undef elf_backend_copy_special_section_fields
6436 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
6438 #include "elf32-target.h"
6440 /* Intel MCU support. */
6443 elf32_iamcu_elf_object_p (bfd *abfd)
6445 /* Set the right machine number for an IAMCU elf32 file. */
6446 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
6450 #undef TARGET_LITTLE_SYM
6451 #define TARGET_LITTLE_SYM iamcu_elf32_vec
6452 #undef TARGET_LITTLE_NAME
6453 #define TARGET_LITTLE_NAME "elf32-iamcu"
6455 #define ELF_ARCH bfd_arch_iamcu
6457 #undef ELF_MACHINE_CODE
6458 #define ELF_MACHINE_CODE EM_IAMCU
6463 #define elf32_bed elf32_iamcu_bed
6465 #undef elf_backend_object_p
6466 #define elf_backend_object_p elf32_iamcu_elf_object_p
6468 #undef elf_backend_static_tls_alignment
6470 #undef elf_backend_want_plt_sym
6471 #define elf_backend_want_plt_sym 0
6473 #undef elf_backend_strtab_flags
6474 #undef elf_backend_copy_special_section_fields
6476 #include "elf32-target.h"
6478 /* Restore defaults. */
6480 #define ELF_ARCH bfd_arch_i386
6481 #undef ELF_MACHINE_CODE
6482 #define ELF_MACHINE_CODE EM_386
6484 /* Native Client support. */
6486 #undef TARGET_LITTLE_SYM
6487 #define TARGET_LITTLE_SYM i386_elf32_nacl_vec
6488 #undef TARGET_LITTLE_NAME
6489 #define TARGET_LITTLE_NAME "elf32-i386-nacl"
6491 #define elf32_bed elf32_i386_nacl_bed
6493 #undef ELF_MAXPAGESIZE
6494 #define ELF_MAXPAGESIZE 0x10000
6496 /* Restore defaults. */
6498 #undef elf_backend_want_plt_sym
6499 #define elf_backend_want_plt_sym 0
6500 #undef elf_backend_post_process_headers
6501 #undef elf_backend_static_tls_alignment
6503 /* NaCl uses substantially different PLT entries for the same effects. */
6505 #undef elf_backend_plt_alignment
6506 #define elf_backend_plt_alignment 5
6507 #define NACL_PLT_ENTRY_SIZE 64
6508 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
6510 static const bfd_byte elf_i386_nacl_plt0_entry[] =
6512 0xff, 0x35, /* pushl contents of address */
6513 0, 0, 0, 0, /* replaced with address of .got + 4. */
6514 0x8b, 0x0d, /* movl contents of address, %ecx */
6515 0, 0, 0, 0, /* replaced with address of .got + 8. */
6516 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
6517 0xff, 0xe1 /* jmp *%ecx */
6520 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
6522 0x8b, 0x0d, /* movl contents of address, %ecx */
6523 0, 0, 0, 0, /* replaced with GOT slot address. */
6524 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
6525 0xff, 0xe1, /* jmp *%ecx */
6527 /* Pad to the next 32-byte boundary with nop instructions. */
6529 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6530 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6532 /* Lazy GOT entries point here (32-byte aligned). */
6533 0x68, /* pushl immediate */
6534 0, 0, 0, 0, /* replaced with reloc offset. */
6535 0xe9, /* jmp relative */
6536 0, 0, 0, 0, /* replaced with offset to .plt. */
6538 /* Pad to the next 32-byte boundary with nop instructions. */
6539 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6540 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6544 static const bfd_byte
6545 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
6547 0xff, 0x73, 0x04, /* pushl 4(%ebx) */
6548 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */
6549 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */
6550 0xff, 0xe1, /* jmp *%ecx */
6552 /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
6553 so pad to that size with nop instructions. */
6554 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
6557 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
6559 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
6560 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
6561 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */
6562 0xff, 0xe1, /* jmp *%ecx */
6564 /* Pad to the next 32-byte boundary with nop instructions. */
6566 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6567 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6569 /* Lazy GOT entries point here (32-byte aligned). */
6570 0x68, /* pushl immediate */
6571 0, 0, 0, 0, /* replaced with offset into relocation table. */
6572 0xe9, /* jmp relative */
6573 0, 0, 0, 0, /* replaced with offset to start of .plt. */
6575 /* Pad to the next 32-byte boundary with nop instructions. */
6576 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6577 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6581 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
6583 #if (PLT_CIE_LENGTH != 20 \
6584 || PLT_FDE_LENGTH != 36 \
6585 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
6586 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
6587 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
6589 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
6590 0, 0, 0, 0, /* CIE ID */
6591 1, /* CIE version */
6592 'z', 'R', 0, /* Augmentation string */
6593 1, /* Code alignment factor */
6594 0x7c, /* Data alignment factor: -4 */
6595 8, /* Return address column */
6596 1, /* Augmentation size */
6597 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
6598 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
6599 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
6600 DW_CFA_nop, DW_CFA_nop,
6602 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
6603 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
6604 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
6605 0, 0, 0, 0, /* .plt size goes here */
6606 0, /* Augmentation size */
6607 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
6608 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
6609 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
6610 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
6611 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
6612 13, /* Block length */
6613 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
6614 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
6615 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
6616 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
6617 DW_CFA_nop, DW_CFA_nop
6620 static const struct elf_i386_plt_layout elf_i386_nacl_plt =
6622 elf_i386_nacl_plt0_entry, /* plt0_entry */
6623 sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */
6624 2, /* plt0_got1_offset */
6625 8, /* plt0_got2_offset */
6626 elf_i386_nacl_plt_entry, /* plt_entry */
6627 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
6628 2, /* plt_got_offset */
6629 33, /* plt_reloc_offset */
6630 38, /* plt_plt_offset */
6631 32, /* plt_lazy_offset */
6632 elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */
6633 elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */
6634 elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */
6635 sizeof (elf_i386_nacl_eh_frame_plt),/* eh_frame_plt_size */
6636 NULL, /* eh_frame_plt_got */
6637 0, /* eh_frame_plt_got_size */
6640 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
6642 &elf_i386_nacl_plt, /* plt */
6643 0x90, /* plt0_pad_byte: nop insn */
6648 elf32_i386_nacl_elf_object_p (bfd *abfd)
6650 /* Set the right machine number for a NaCl i386 ELF32 file. */
6651 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
6655 #undef elf_backend_arch_data
6656 #define elf_backend_arch_data &elf_i386_nacl_arch_bed
6658 #undef elf_backend_object_p
6659 #define elf_backend_object_p elf32_i386_nacl_elf_object_p
6660 #undef elf_backend_modify_segment_map
6661 #define elf_backend_modify_segment_map nacl_modify_segment_map
6662 #undef elf_backend_modify_program_headers
6663 #define elf_backend_modify_program_headers nacl_modify_program_headers
6664 #undef elf_backend_final_write_processing
6665 #define elf_backend_final_write_processing nacl_final_write_processing
6667 #include "elf32-target.h"
6669 /* Restore defaults. */
6670 #undef elf_backend_object_p
6671 #undef elf_backend_modify_segment_map
6672 #undef elf_backend_modify_program_headers
6673 #undef elf_backend_final_write_processing
6675 /* VxWorks support. */
6677 #undef TARGET_LITTLE_SYM
6678 #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
6679 #undef TARGET_LITTLE_NAME
6680 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
6682 #undef ELF_MAXPAGESIZE
6683 #define ELF_MAXPAGESIZE 0x1000
6684 #undef elf_backend_plt_alignment
6685 #define elf_backend_plt_alignment 4
6687 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
6689 &elf_i386_plt, /* plt */
6690 0x90, /* plt0_pad_byte */
6694 #undef elf_backend_arch_data
6695 #define elf_backend_arch_data &elf_i386_vxworks_arch_bed
6697 #undef elf_backend_relocs_compatible
6698 #undef elf_backend_add_symbol_hook
6699 #define elf_backend_add_symbol_hook \
6700 elf_vxworks_add_symbol_hook
6701 #undef elf_backend_link_output_symbol_hook
6702 #define elf_backend_link_output_symbol_hook \
6703 elf_vxworks_link_output_symbol_hook
6704 #undef elf_backend_emit_relocs
6705 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
6706 #undef elf_backend_final_write_processing
6707 #define elf_backend_final_write_processing \
6708 elf_vxworks_final_write_processing
6709 #undef elf_backend_static_tls_alignment
6711 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
6713 #undef elf_backend_want_plt_sym
6714 #define elf_backend_want_plt_sym 1
6717 #define elf32_bed elf32_i386_vxworks_bed
6719 #include "elf32-target.h"