1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright (C) 1993-2016 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 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
390 /* PR 17512: file: 0f67f69d. */
391 if (elf_howto_table [indx].type != r_type)
393 return &elf_howto_table[indx];
397 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
399 Elf_Internal_Rela *dst)
401 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
402 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
405 /* Return whether a symbol name implies a local label. The UnixWare
406 2.1 cc generates temporary symbols that start with .X, so we
407 recognize them here. FIXME: do other SVR4 compilers also use .X?.
408 If so, we should move the .X recognition into
409 _bfd_elf_is_local_label_name. */
412 elf_i386_is_local_label_name (bfd *abfd, const char *name)
414 if (name[0] == '.' && name[1] == 'X')
417 return _bfd_elf_is_local_label_name (abfd, name);
420 /* Support for core dump NOTE sections. */
423 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
428 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
430 int pr_version = bfd_get_32 (abfd, note->descdata);
436 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
439 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
443 size = bfd_get_32 (abfd, note->descdata + 8);
447 switch (note->descsz)
452 case 144: /* Linux/i386 */
454 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
457 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
467 /* Make a ".reg/999" section. */
468 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
469 size, note->descpos + offset);
473 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
475 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
477 int pr_version = bfd_get_32 (abfd, note->descdata);
482 elf_tdata (abfd)->core->program
483 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
484 elf_tdata (abfd)->core->command
485 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
489 switch (note->descsz)
494 case 124: /* Linux/i386 elf_prpsinfo. */
495 elf_tdata (abfd)->core->pid
496 = bfd_get_32 (abfd, note->descdata + 12);
497 elf_tdata (abfd)->core->program
498 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
499 elf_tdata (abfd)->core->command
500 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
504 /* Note that for some reason, a spurious space is tacked
505 onto the end of the args in some (at least one anyway)
506 implementations, so strip it off if it exists. */
508 char *command = elf_tdata (abfd)->core->command;
509 int n = strlen (command);
511 if (0 < n && command[n - 1] == ' ')
512 command[n - 1] = '\0';
518 /* Functions for the i386 ELF linker.
520 In order to gain some understanding of code in this file without
521 knowing all the intricate details of the linker, note the
524 Functions named elf_i386_* are called by external routines, other
525 functions are only called locally. elf_i386_* functions appear
526 in this file more or less in the order in which they are called
527 from external routines. eg. elf_i386_check_relocs is called
528 early in the link process, elf_i386_finish_dynamic_sections is
529 one of the last functions. */
532 /* The name of the dynamic interpreter. This is put in the .interp
535 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
537 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
538 copying dynamic variables from a shared lib into an app's dynbss
539 section, and instead use a dynamic relocation to point into the
541 #define ELIMINATE_COPY_RELOCS 1
543 /* The size in bytes of an entry in the procedure linkage table. */
545 #define PLT_ENTRY_SIZE 16
547 /* The first entry in an absolute procedure linkage table looks like
548 this. See the SVR4 ABI i386 supplement to see how this works.
549 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
551 static const bfd_byte elf_i386_plt0_entry[12] =
553 0xff, 0x35, /* pushl contents of address */
554 0, 0, 0, 0, /* replaced with address of .got + 4. */
555 0xff, 0x25, /* jmp indirect */
556 0, 0, 0, 0 /* replaced with address of .got + 8. */
559 /* Subsequent entries in an absolute procedure linkage table look like
562 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
564 0xff, 0x25, /* jmp indirect */
565 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
566 0x68, /* pushl immediate */
567 0, 0, 0, 0, /* replaced with offset into relocation table. */
568 0xe9, /* jmp relative */
569 0, 0, 0, 0 /* replaced with offset to start of .plt. */
572 /* The first entry in a PIC procedure linkage table look like this.
573 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
575 static const bfd_byte elf_i386_pic_plt0_entry[12] =
577 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
578 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
581 /* Subsequent entries in a PIC procedure linkage table look like this. */
583 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
585 0xff, 0xa3, /* jmp *offset(%ebx) */
586 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
587 0x68, /* pushl immediate */
588 0, 0, 0, 0, /* replaced with offset into relocation table. */
589 0xe9, /* jmp relative */
590 0, 0, 0, 0 /* replaced with offset to start of .plt. */
593 /* Entries in the GOT procedure linkage table look like this. */
595 static const bfd_byte elf_i386_got_plt_entry[8] =
597 0xff, 0x25, /* jmp indirect */
598 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
599 0x66, 0x90 /* xchg %ax,%ax */
602 /* Entries in the PIC GOT procedure linkage table look like this. */
604 static const bfd_byte elf_i386_pic_got_plt_entry[8] =
606 0xff, 0xa3, /* jmp *offset(%ebx) */
607 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
608 0x66, 0x90 /* xchg %ax,%ax */
611 /* .eh_frame covering the .plt section. */
613 static const bfd_byte elf_i386_eh_frame_plt[] =
615 #define PLT_CIE_LENGTH 20
616 #define PLT_FDE_LENGTH 36
617 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
618 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
619 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
620 0, 0, 0, 0, /* CIE ID */
622 'z', 'R', 0, /* Augmentation string */
623 1, /* Code alignment factor */
624 0x7c, /* Data alignment factor */
625 8, /* Return address column */
626 1, /* Augmentation size */
627 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
628 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
629 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
630 DW_CFA_nop, DW_CFA_nop,
632 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
633 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
634 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
635 0, 0, 0, 0, /* .plt size goes here */
636 0, /* Augmentation size */
637 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
638 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
639 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
640 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
641 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
642 11, /* Block length */
643 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
644 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
645 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
646 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
647 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
650 /* .eh_frame covering the .plt.got section. */
652 static const bfd_byte elf_i386_eh_frame_plt_got[] =
654 #define PLT_GOT_FDE_LENGTH 16
655 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
656 0, 0, 0, 0, /* CIE ID */
658 'z', 'R', 0, /* Augmentation string */
659 1, /* Code alignment factor */
660 0x7c, /* Data alignment factor */
661 8, /* Return address column */
662 1, /* Augmentation size */
663 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
664 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
665 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
666 DW_CFA_nop, DW_CFA_nop,
668 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
669 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
670 0, 0, 0, 0, /* the start of .plt.got goes here */
671 0, 0, 0, 0, /* .plt.got size goes here */
672 0, /* Augmentation size */
673 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
676 struct elf_i386_plt_layout
678 /* The first entry in an absolute procedure linkage table looks like this. */
679 const bfd_byte *plt0_entry;
680 unsigned int plt0_entry_size;
682 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
683 unsigned int plt0_got1_offset;
684 unsigned int plt0_got2_offset;
686 /* Later entries in an absolute procedure linkage table look like this. */
687 const bfd_byte *plt_entry;
688 unsigned int plt_entry_size;
690 /* Offsets into plt_entry that are to be replaced with... */
691 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
692 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
693 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
695 /* Offset into plt_entry where the initial value of the GOT entry points. */
696 unsigned int plt_lazy_offset;
698 /* The first entry in a PIC procedure linkage table looks like this. */
699 const bfd_byte *pic_plt0_entry;
701 /* Subsequent entries in a PIC procedure linkage table look like this. */
702 const bfd_byte *pic_plt_entry;
704 /* .eh_frame covering the .plt section. */
705 const bfd_byte *eh_frame_plt;
706 unsigned int eh_frame_plt_size;
708 /* .eh_frame covering the .plt.got section. */
709 const bfd_byte *eh_frame_plt_got;
710 unsigned int eh_frame_plt_got_size;
713 #define GET_PLT_ENTRY_SIZE(abfd) \
714 get_elf_i386_backend_data (abfd)->plt->plt_entry_size
716 /* These are the standard parameters. */
717 static const struct elf_i386_plt_layout elf_i386_plt =
719 elf_i386_plt0_entry, /* plt0_entry */
720 sizeof (elf_i386_plt0_entry), /* plt0_entry_size */
721 2, /* plt0_got1_offset */
722 8, /* plt0_got2_offset */
723 elf_i386_plt_entry, /* plt_entry */
724 PLT_ENTRY_SIZE, /* plt_entry_size */
725 2, /* plt_got_offset */
726 7, /* plt_reloc_offset */
727 12, /* plt_plt_offset */
728 6, /* plt_lazy_offset */
729 elf_i386_pic_plt0_entry, /* pic_plt0_entry */
730 elf_i386_pic_plt_entry, /* pic_plt_entry */
731 elf_i386_eh_frame_plt, /* eh_frame_plt */
732 sizeof (elf_i386_eh_frame_plt), /* eh_frame_plt_size */
733 elf_i386_eh_frame_plt_got, /* eh_frame_plt_got */
734 sizeof (elf_i386_eh_frame_plt_got), /* eh_frame_plt_got_size */
738 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
739 for the PLTResolve stub and then for each PLT entry. */
740 #define PLTRESOLVE_RELOCS_SHLIB 0
741 #define PLTRESOLVE_RELOCS 2
742 #define PLT_NON_JUMP_SLOT_RELOCS 2
744 /* Architecture-specific backend data for i386. */
746 struct elf_i386_backend_data
748 /* Parameters describing PLT generation. */
749 const struct elf_i386_plt_layout *plt;
751 /* Value used to fill the unused bytes of the first PLT entry. */
752 bfd_byte plt0_pad_byte;
754 /* True if the target system is VxWorks. */
758 #define get_elf_i386_backend_data(abfd) \
759 ((const struct elf_i386_backend_data *) \
760 get_elf_backend_data (abfd)->arch_data)
762 /* These are the standard parameters. */
763 static const struct elf_i386_backend_data elf_i386_arch_bed =
765 &elf_i386_plt, /* plt */
766 0, /* plt0_pad_byte */
770 #define elf_backend_arch_data &elf_i386_arch_bed
772 /* Is a undefined weak symbol which is resolved to 0. Reference to an
773 undefined weak symbol is resolved to 0 when building executable if
775 1. Has non-GOT/non-PLT relocations in text section. Or
776 2. Has no GOT/PLT relocation.
778 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, GOT_RELOC, EH) \
779 ((EH)->elf.root.type == bfd_link_hash_undefweak \
780 && bfd_link_executable (INFO) \
781 && (elf_i386_hash_table (INFO)->interp == NULL \
783 || (EH)->has_non_got_reloc \
784 || !(INFO)->dynamic_undefined_weak))
786 /* i386 ELF linker hash entry. */
788 struct elf_i386_link_hash_entry
790 struct elf_link_hash_entry elf;
792 /* Track dynamic relocs copied for this symbol. */
793 struct elf_dyn_relocs *dyn_relocs;
795 #define GOT_UNKNOWN 0
799 #define GOT_TLS_IE_POS 5
800 #define GOT_TLS_IE_NEG 6
801 #define GOT_TLS_IE_BOTH 7
802 #define GOT_TLS_GDESC 8
803 #define GOT_TLS_GD_BOTH_P(type) \
804 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
805 #define GOT_TLS_GD_P(type) \
806 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
807 #define GOT_TLS_GDESC_P(type) \
808 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
809 #define GOT_TLS_GD_ANY_P(type) \
810 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
811 unsigned char tls_type;
813 /* Symbol is referenced by R_386_GOTOFF relocation. */
814 unsigned int gotoff_ref : 1;
816 /* Symbol has GOT or PLT relocations. */
817 unsigned int has_got_reloc : 1;
819 /* Symbol has non-GOT/non-PLT relocations in text sections. */
820 unsigned int has_non_got_reloc : 1;
822 /* 0: symbol isn't ___tls_get_addr.
823 1: symbol is ___tls_get_addr.
824 2: symbol is unknown. */
825 unsigned int tls_get_addr : 2;
827 /* Reference count of C/C++ function pointer relocations in read-write
828 section which can be resolved at run-time. */
829 bfd_signed_vma func_pointer_refcount;
831 /* Information about the GOT PLT entry. Filled when there are both
832 GOT and PLT relocations against the same function. */
833 union gotplt_union plt_got;
835 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
836 starting at the end of the jump table. */
840 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
842 struct elf_i386_obj_tdata
844 struct elf_obj_tdata root;
846 /* tls_type for each local got entry. */
847 char *local_got_tls_type;
849 /* GOTPLT entries for TLS descriptors. */
850 bfd_vma *local_tlsdesc_gotent;
853 #define elf_i386_tdata(abfd) \
854 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
856 #define elf_i386_local_got_tls_type(abfd) \
857 (elf_i386_tdata (abfd)->local_got_tls_type)
859 #define elf_i386_local_tlsdesc_gotent(abfd) \
860 (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
862 #define is_i386_elf(bfd) \
863 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
864 && elf_tdata (bfd) != NULL \
865 && elf_object_id (bfd) == I386_ELF_DATA)
868 elf_i386_mkobject (bfd *abfd)
870 return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
874 /* i386 ELF linker hash table. */
876 struct elf_i386_link_hash_table
878 struct elf_link_hash_table elf;
880 /* Short-cuts to get to dynamic linker sections. */
884 asection *plt_eh_frame;
886 asection *plt_got_eh_frame;
890 bfd_signed_vma refcount;
894 /* The amount of space used by the reserved portion of the sgotplt
895 section, plus whatever space is used by the jump slots. */
896 bfd_vma sgotplt_jump_table_size;
898 /* Small local sym cache. */
899 struct sym_cache sym_cache;
901 /* _TLS_MODULE_BASE_ symbol. */
902 struct bfd_link_hash_entry *tls_module_base;
904 /* Used by local STT_GNU_IFUNC symbols. */
905 htab_t loc_hash_table;
906 void * loc_hash_memory;
908 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
911 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
912 bfd_vma next_tls_desc_index;
914 /* The index of the next unused R_386_JUMP_SLOT slot in .rel.plt. */
915 bfd_vma next_jump_slot_index;
917 /* The index of the next unused R_386_IRELATIVE slot in .rel.plt. */
918 bfd_vma next_irelative_index;
920 /* TRUE if there are dynamic relocs against IFUNC symbols that apply
921 to read-only sections. */
922 bfd_boolean readonly_dynrelocs_against_ifunc;
925 /* Get the i386 ELF linker hash table from a link_info structure. */
927 #define elf_i386_hash_table(p) \
928 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
929 == I386_ELF_DATA ? ((struct elf_i386_link_hash_table *) ((p)->hash)) : NULL)
931 #define elf_i386_compute_jump_table_size(htab) \
932 ((htab)->elf.srelplt->reloc_count * 4)
934 /* Create an entry in an i386 ELF linker hash table. */
936 static struct bfd_hash_entry *
937 elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
938 struct bfd_hash_table *table,
941 /* Allocate the structure if it has not already been allocated by a
945 entry = (struct bfd_hash_entry *)
946 bfd_hash_allocate (table, sizeof (struct elf_i386_link_hash_entry));
951 /* Call the allocation method of the superclass. */
952 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
955 struct elf_i386_link_hash_entry *eh;
957 eh = (struct elf_i386_link_hash_entry *) entry;
958 eh->dyn_relocs = NULL;
959 eh->tls_type = GOT_UNKNOWN;
961 eh->has_got_reloc = 0;
962 eh->has_non_got_reloc = 0;
963 eh->tls_get_addr = 2;
964 eh->func_pointer_refcount = 0;
965 eh->plt_got.offset = (bfd_vma) -1;
966 eh->tlsdesc_got = (bfd_vma) -1;
972 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
973 for local symbol so that we can handle local STT_GNU_IFUNC symbols
974 as global symbol. We reuse indx and dynstr_index for local symbol
975 hash since they aren't used by global symbols in this backend. */
978 elf_i386_local_htab_hash (const void *ptr)
980 struct elf_link_hash_entry *h
981 = (struct elf_link_hash_entry *) ptr;
982 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
985 /* Compare local hash entries. */
988 elf_i386_local_htab_eq (const void *ptr1, const void *ptr2)
990 struct elf_link_hash_entry *h1
991 = (struct elf_link_hash_entry *) ptr1;
992 struct elf_link_hash_entry *h2
993 = (struct elf_link_hash_entry *) ptr2;
995 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
998 /* Find and/or create a hash entry for local symbol. */
1000 static struct elf_link_hash_entry *
1001 elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab,
1002 bfd *abfd, const Elf_Internal_Rela *rel,
1005 struct elf_i386_link_hash_entry e, *ret;
1006 asection *sec = abfd->sections;
1007 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
1008 ELF32_R_SYM (rel->r_info));
1011 e.elf.indx = sec->id;
1012 e.elf.dynstr_index = ELF32_R_SYM (rel->r_info);
1013 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
1014 create ? INSERT : NO_INSERT);
1021 ret = (struct elf_i386_link_hash_entry *) *slot;
1025 ret = (struct elf_i386_link_hash_entry *)
1026 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
1027 sizeof (struct elf_i386_link_hash_entry));
1030 memset (ret, 0, sizeof (*ret));
1031 ret->elf.indx = sec->id;
1032 ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
1033 ret->elf.dynindx = -1;
1034 ret->func_pointer_refcount = 0;
1035 ret->plt_got.offset = (bfd_vma) -1;
1041 /* Destroy an i386 ELF linker hash table. */
1044 elf_i386_link_hash_table_free (bfd *obfd)
1046 struct elf_i386_link_hash_table *htab
1047 = (struct elf_i386_link_hash_table *) obfd->link.hash;
1049 if (htab->loc_hash_table)
1050 htab_delete (htab->loc_hash_table);
1051 if (htab->loc_hash_memory)
1052 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
1053 _bfd_elf_link_hash_table_free (obfd);
1056 /* Create an i386 ELF linker hash table. */
1058 static struct bfd_link_hash_table *
1059 elf_i386_link_hash_table_create (bfd *abfd)
1061 struct elf_i386_link_hash_table *ret;
1062 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
1064 ret = (struct elf_i386_link_hash_table *) bfd_zmalloc (amt);
1068 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1069 elf_i386_link_hash_newfunc,
1070 sizeof (struct elf_i386_link_hash_entry),
1077 ret->loc_hash_table = htab_try_create (1024,
1078 elf_i386_local_htab_hash,
1079 elf_i386_local_htab_eq,
1081 ret->loc_hash_memory = objalloc_create ();
1082 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1084 elf_i386_link_hash_table_free (abfd);
1087 ret->elf.root.hash_table_free = elf_i386_link_hash_table_free;
1089 return &ret->elf.root;
1092 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
1093 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
1097 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1099 struct elf_i386_link_hash_table *htab;
1101 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1104 htab = elf_i386_hash_table (info);
1108 /* Set the contents of the .interp section to the interpreter. */
1109 if (bfd_link_executable (info) && !info->nointerp)
1111 asection *s = bfd_get_linker_section (dynobj, ".interp");
1114 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1115 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1119 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
1123 if (bfd_link_executable (info))
1125 /* Always allow copy relocs for building executables. */
1126 asection *s = bfd_get_linker_section (dynobj, ".rel.bss");
1129 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
1130 s = bfd_make_section_anyway_with_flags (dynobj,
1132 (bed->dynamic_sec_flags
1135 || ! bfd_set_section_alignment (dynobj, s,
1136 bed->s->log_file_align))
1142 if (get_elf_i386_backend_data (dynobj)->is_vxworks
1143 && !elf_vxworks_create_dynamic_sections (dynobj, info,
1147 if (!info->no_ld_generated_unwind_info
1148 && htab->plt_eh_frame == NULL
1149 && htab->elf.splt != NULL)
1151 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
1152 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1153 | SEC_LINKER_CREATED);
1155 = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
1156 if (htab->plt_eh_frame == NULL
1157 || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 2))
1164 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1167 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
1168 struct elf_link_hash_entry *dir,
1169 struct elf_link_hash_entry *ind)
1171 struct elf_i386_link_hash_entry *edir, *eind;
1173 edir = (struct elf_i386_link_hash_entry *) dir;
1174 eind = (struct elf_i386_link_hash_entry *) ind;
1176 if (eind->dyn_relocs != NULL)
1178 if (edir->dyn_relocs != NULL)
1180 struct elf_dyn_relocs **pp;
1181 struct elf_dyn_relocs *p;
1183 /* Add reloc counts against the indirect sym to the direct sym
1184 list. Merge any entries against the same section. */
1185 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1187 struct elf_dyn_relocs *q;
1189 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1190 if (q->sec == p->sec)
1192 q->pc_count += p->pc_count;
1193 q->count += p->count;
1200 *pp = edir->dyn_relocs;
1203 edir->dyn_relocs = eind->dyn_relocs;
1204 eind->dyn_relocs = NULL;
1207 if (ind->root.type == bfd_link_hash_indirect
1208 && dir->got.refcount <= 0)
1210 edir->tls_type = eind->tls_type;
1211 eind->tls_type = GOT_UNKNOWN;
1214 /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
1215 generate a R_386_COPY reloc. */
1216 edir->gotoff_ref |= eind->gotoff_ref;
1218 edir->has_got_reloc |= eind->has_got_reloc;
1219 edir->has_non_got_reloc |= eind->has_non_got_reloc;
1221 if (ELIMINATE_COPY_RELOCS
1222 && ind->root.type != bfd_link_hash_indirect
1223 && dir->dynamic_adjusted)
1225 /* If called to transfer flags for a weakdef during processing
1226 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1227 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1228 dir->ref_dynamic |= ind->ref_dynamic;
1229 dir->ref_regular |= ind->ref_regular;
1230 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1231 dir->needs_plt |= ind->needs_plt;
1232 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1236 if (eind->func_pointer_refcount > 0)
1238 edir->func_pointer_refcount += eind->func_pointer_refcount;
1239 eind->func_pointer_refcount = 0;
1242 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1246 /* Return TRUE if the TLS access code sequence support transition
1250 elf_i386_check_tls_transition (asection *sec,
1252 Elf_Internal_Shdr *symtab_hdr,
1253 struct elf_link_hash_entry **sym_hashes,
1254 unsigned int r_type,
1255 const Elf_Internal_Rela *rel,
1256 const Elf_Internal_Rela *relend)
1258 unsigned int val, type, reg;
1259 unsigned long r_symndx;
1260 struct elf_link_hash_entry *h;
1263 bfd_boolean indirect_call, tls_get_addr;
1265 offset = rel->r_offset;
1270 if (offset < 2 || (rel + 1) >= relend)
1273 indirect_call = FALSE;
1274 call = contents + offset + 4;
1277 if (r_type == R_386_TLS_GD)
1279 /* Check transition from GD access model. Only
1280 leal foo@tlsgd(,%ebx,1), %eax
1281 call ___tls_get_addr@PLT
1283 leal foo@tlsgd(%ebx) %eax
1284 call ___tls_get_addr@PLT
1287 leal foo@tlsgd(%reg), %eax
1288 call *___tls_get_addr@GOT(%reg)
1289 which may be converted to
1290 addr32 call ___tls_get_addr
1291 can transit to different access model. */
1292 if ((offset + 10) > sec->size
1293 || (type != 0x8d && type != 0x04))
1298 /* leal foo@tlsgd(,%ebx,1), %eax
1299 call ___tls_get_addr@PLT */
1303 if (*(call - 7) != 0x8d
1311 leal foo@tlsgd(%ebx), %eax
1312 call ___tls_get_addr@PLT
1315 leal foo@tlsgd(%reg), %eax
1316 call *___tls_get_addr@GOT(%reg)
1317 which may be converted to
1318 addr32 call ___tls_get_addr
1320 %eax can't be used as the GOT base register since it
1321 is used to pass parameter to ___tls_get_addr. */
1323 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
1326 indirect_call = call[0] == 0xff;
1327 if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
1328 && !(call[0] == 0x67 && call[1] == 0xe8)
1330 && (call[1] & 0xf8) == 0x90
1331 && (call[1] & 0x7) == reg))
1337 /* Check transition from LD access model. Only
1338 leal foo@tlsldm(%ebx), %eax
1339 call ___tls_get_addr@PLT
1341 leal foo@tlsldm(%reg), %eax
1342 call *___tls_get_addr@GOT(%reg)
1343 which may be converted to
1344 addr32 call ___tls_get_addr
1345 can transit to different access model. */
1346 if (type != 0x8d || (offset + 9) > sec->size)
1349 /* %eax can't be used as the GOT base register since it is
1350 used to pass parameter to ___tls_get_addr. */
1352 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
1355 indirect_call = call[0] == 0xff;
1356 if (!(reg == 3 && call[0] == 0xe8)
1357 && !(call[0] == 0x67 && call[1] == 0xe8)
1359 && (call[1] & 0xf8) == 0x90
1360 && (call[1] & 0x7) == reg))
1364 r_symndx = ELF32_R_SYM (rel[1].r_info);
1365 if (r_symndx < symtab_hdr->sh_info)
1368 tls_get_addr = FALSE;
1369 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1370 if (h != NULL && h->root.root.string != NULL)
1372 struct elf_i386_link_hash_entry *eh
1373 = (struct elf_i386_link_hash_entry *) h;
1374 tls_get_addr = eh->tls_get_addr == 1;
1375 if (eh->tls_get_addr > 1)
1377 /* Use strncmp to check ___tls_get_addr since
1378 ___tls_get_addr may be versioned. */
1379 if (strncmp (h->root.root.string, "___tls_get_addr", 15)
1382 eh->tls_get_addr = 1;
1383 tls_get_addr = TRUE;
1386 eh->tls_get_addr = 0;
1392 else if (indirect_call)
1393 return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X);
1395 return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1396 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
1399 /* Check transition from IE access model:
1400 movl foo@indntpoff(%rip), %eax
1401 movl foo@indntpoff(%rip), %reg
1402 addl foo@indntpoff(%rip), %reg
1405 if (offset < 1 || (offset + 4) > sec->size)
1408 /* Check "movl foo@tpoff(%rip), %eax" first. */
1409 val = bfd_get_8 (abfd, contents + offset - 1);
1416 /* Check movl|addl foo@tpoff(%rip), %reg. */
1417 type = bfd_get_8 (abfd, contents + offset - 2);
1418 return ((type == 0x8b || type == 0x03)
1419 && (val & 0xc7) == 0x05);
1421 case R_386_TLS_GOTIE:
1422 case R_386_TLS_IE_32:
1423 /* Check transition from {IE_32,GOTIE} access model:
1424 subl foo@{tpoff,gontoff}(%reg1), %reg2
1425 movl foo@{tpoff,gontoff}(%reg1), %reg2
1426 addl foo@{tpoff,gontoff}(%reg1), %reg2
1429 if (offset < 2 || (offset + 4) > sec->size)
1432 val = bfd_get_8 (abfd, contents + offset - 1);
1433 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1436 type = bfd_get_8 (abfd, contents + offset - 2);
1437 return type == 0x8b || type == 0x2b || type == 0x03;
1439 case R_386_TLS_GOTDESC:
1440 /* Check transition from GDesc access model:
1441 leal x@tlsdesc(%ebx), %eax
1443 Make sure it's a leal adding ebx to a 32-bit offset
1444 into any register, although it's probably almost always
1447 if (offset < 2 || (offset + 4) > sec->size)
1450 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1453 val = bfd_get_8 (abfd, contents + offset - 1);
1454 return (val & 0xc7) == 0x83;
1456 case R_386_TLS_DESC_CALL:
1457 /* Check transition from GDesc access model:
1458 call *x@tlsdesc(%eax)
1460 if (offset + 2 <= sec->size)
1462 /* Make sure that it's a call *x@tlsdesc(%eax). */
1463 call = contents + offset;
1464 return call[0] == 0xff && call[1] == 0x10;
1474 /* Return TRUE if the TLS access transition is OK or no transition
1475 will be performed. Update R_TYPE if there is a transition. */
1478 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1479 asection *sec, bfd_byte *contents,
1480 Elf_Internal_Shdr *symtab_hdr,
1481 struct elf_link_hash_entry **sym_hashes,
1482 unsigned int *r_type, int tls_type,
1483 const Elf_Internal_Rela *rel,
1484 const Elf_Internal_Rela *relend,
1485 struct elf_link_hash_entry *h,
1486 unsigned long r_symndx,
1487 bfd_boolean from_relocate_section)
1489 unsigned int from_type = *r_type;
1490 unsigned int to_type = from_type;
1491 bfd_boolean check = TRUE;
1493 /* Skip TLS transition for functions. */
1495 && (h->type == STT_FUNC
1496 || h->type == STT_GNU_IFUNC))
1502 case R_386_TLS_GOTDESC:
1503 case R_386_TLS_DESC_CALL:
1504 case R_386_TLS_IE_32:
1506 case R_386_TLS_GOTIE:
1507 if (bfd_link_executable (info))
1510 to_type = R_386_TLS_LE_32;
1511 else if (from_type != R_386_TLS_IE
1512 && from_type != R_386_TLS_GOTIE)
1513 to_type = R_386_TLS_IE_32;
1516 /* When we are called from elf_i386_relocate_section, there may
1517 be additional transitions based on TLS_TYPE. */
1518 if (from_relocate_section)
1520 unsigned int new_to_type = to_type;
1522 if (bfd_link_executable (info)
1525 && (tls_type & GOT_TLS_IE))
1526 new_to_type = R_386_TLS_LE_32;
1528 if (to_type == R_386_TLS_GD
1529 || to_type == R_386_TLS_GOTDESC
1530 || to_type == R_386_TLS_DESC_CALL)
1532 if (tls_type == GOT_TLS_IE_POS)
1533 new_to_type = R_386_TLS_GOTIE;
1534 else if (tls_type & GOT_TLS_IE)
1535 new_to_type = R_386_TLS_IE_32;
1538 /* We checked the transition before when we were called from
1539 elf_i386_check_relocs. We only want to check the new
1540 transition which hasn't been checked before. */
1541 check = new_to_type != to_type && from_type == to_type;
1542 to_type = new_to_type;
1548 if (bfd_link_executable (info))
1549 to_type = R_386_TLS_LE_32;
1556 /* Return TRUE if there is no transition. */
1557 if (from_type == to_type)
1560 /* Check if the transition can be performed. */
1562 && ! elf_i386_check_tls_transition (sec, contents,
1563 symtab_hdr, sym_hashes,
1564 from_type, rel, relend))
1566 reloc_howto_type *from, *to;
1569 from = elf_i386_rtype_to_howto (abfd, from_type);
1570 to = elf_i386_rtype_to_howto (abfd, to_type);
1573 name = h->root.root.string;
1576 struct elf_i386_link_hash_table *htab;
1578 htab = elf_i386_hash_table (info);
1583 Elf_Internal_Sym *isym;
1585 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1587 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1591 (*_bfd_error_handler)
1592 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1593 "in section `%A' failed"),
1594 abfd, sec, from->name, to->name, name,
1595 (unsigned long) rel->r_offset);
1596 bfd_set_error (bfd_error_bad_value);
1604 /* With the local symbol, foo, we convert
1605 mov foo@GOT[(%reg1)], %reg2
1607 lea foo[@GOTOFF(%reg1)], %reg2
1609 call/jmp *foo@GOT[(%reg)]
1611 nop call foo/jmp foo nop
1612 When PIC is false, convert
1613 test %reg1, foo@GOT[(%reg2)]
1617 binop foo@GOT[(%reg1)], %reg2
1620 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1625 elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1627 Elf_Internal_Rela *irel,
1628 struct elf_link_hash_entry *h,
1629 bfd_boolean *converted,
1630 struct bfd_link_info *link_info)
1632 struct elf_i386_link_hash_table *htab;
1633 unsigned int opcode;
1635 bfd_boolean baseless;
1636 Elf_Internal_Sym *isym;
1637 unsigned int addend;
1641 bfd_boolean to_reloc_32;
1642 unsigned int r_type;
1643 unsigned int r_symndx;
1644 bfd_vma roff = irel->r_offset;
1649 /* Addend for R_386_GOT32X relocations must be 0. */
1650 addend = bfd_get_32 (abfd, contents + roff);
1654 htab = elf_i386_hash_table (link_info);
1655 is_pic = bfd_link_pic (link_info);
1657 r_type = ELF32_R_TYPE (irel->r_info);
1658 r_symndx = ELF32_R_SYM (irel->r_info);
1660 modrm = bfd_get_8 (abfd, contents + roff - 1);
1661 baseless = (modrm & 0xc7) == 0x5;
1663 if (baseless && is_pic)
1665 /* For PIC, disallow R_386_GOT32X without a base register
1666 since we don't know what the GOT base is. */
1671 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd,
1673 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1676 name = h->root.root.string;
1678 (*_bfd_error_handler)
1679 (_("%B: direct GOT relocation R_386_GOT32X against `%s' without base register can not be used when making a shared object"),
1684 opcode = bfd_get_8 (abfd, contents + roff - 2);
1686 /* Convert to R_386_32 if PIC is false or there is no base
1688 to_reloc_32 = !is_pic || baseless;
1690 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1694 if (opcode == 0x0ff)
1695 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1696 goto convert_branch;
1698 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1699 "test %reg1, foo@GOT(%reg2)" and
1700 "binop foo@GOT[(%reg1)], %reg2". */
1704 /* Undefined weak symbol is only bound locally in executable
1705 and its reference is resolved as 0. */
1706 if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info, TRUE,
1707 elf_i386_hash_entry (h)))
1711 /* No direct branch to 0 for PIC. */
1715 goto convert_branch;
1719 /* We can convert load of address 0 to R_386_32. */
1727 /* We have "call/jmp *foo@GOT[(%reg)]". */
1728 if ((h->root.type == bfd_link_hash_defined
1729 || h->root.type == bfd_link_hash_defweak)
1730 && SYMBOL_REFERENCES_LOCAL (link_info, h))
1732 /* The function is locally defined. */
1734 /* Convert R_386_GOT32X to R_386_PC32. */
1735 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1737 struct elf_i386_link_hash_entry *eh
1738 = (struct elf_i386_link_hash_entry *) h;
1740 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1743 /* To support TLS optimization, always use addr32 prefix
1744 for "call *___tls_get_addr@GOT(%reg)". */
1745 if (eh && eh->tls_get_addr == 1)
1748 nop_offset = irel->r_offset - 2;
1752 nop = link_info->call_nop_byte;
1753 if (link_info->call_nop_as_suffix)
1755 nop_offset = roff + 3;
1756 irel->r_offset -= 1;
1759 nop_offset = roff - 2;
1764 /* Convert to "jmp foo nop". */
1767 nop_offset = roff + 3;
1768 irel->r_offset -= 1;
1771 bfd_put_8 (abfd, nop, contents + nop_offset);
1772 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1773 /* When converting to PC-relative relocation, we
1774 need to adjust addend by -4. */
1775 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1776 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1783 /* We have "mov foo@GOT[(%re1g)], %reg2",
1784 "test %reg1, foo@GOT(%reg2)" and
1785 "binop foo@GOT[(%reg1)], %reg2".
1787 Avoid optimizing _DYNAMIC since ld.so may use its
1788 link-time address. */
1789 if (h == htab->elf.hdynamic)
1792 /* def_regular is set by an assignment in a linker script in
1793 bfd_elf_record_link_assignment. */
1795 || h->root.type == bfd_link_hash_defined
1796 || h->root.type == bfd_link_hash_defweak)
1797 && SYMBOL_REFERENCES_LOCAL (link_info, h))
1804 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1805 "mov $foo, %reg2" with R_386_32. */
1807 modrm = 0xc0 | (modrm & 0x38) >> 3;
1808 bfd_put_8 (abfd, modrm, contents + roff - 1);
1813 /* Convert "mov foo@GOT(%reg1), %reg2" to
1814 "lea foo@GOTOFF(%reg1), %reg2". */
1815 r_type = R_386_GOTOFF;
1821 /* Only R_386_32 is supported. */
1827 /* Convert "test %reg1, foo@GOT(%reg2)" to
1828 "test $foo, %reg1". */
1829 modrm = 0xc0 | (modrm & 0x38) >> 3;
1834 /* Convert "binop foo@GOT(%reg1), %reg2" to
1835 "binop $foo, %reg2". */
1837 | (modrm & 0x38) >> 3
1841 bfd_put_8 (abfd, modrm, contents + roff - 1);
1845 bfd_put_8 (abfd, opcode, contents + roff - 2);
1846 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1855 /* Rename some of the generic section flags to better document how they
1857 #define need_convert_load sec_flg0
1858 #define check_relocs_failed sec_flg1
1860 /* Look through the relocs for a section during the first phase, and
1861 calculate needed space in the global offset table, procedure linkage
1862 table, and dynamic reloc sections. */
1865 elf_i386_check_relocs (bfd *abfd,
1866 struct bfd_link_info *info,
1868 const Elf_Internal_Rela *relocs)
1870 struct elf_i386_link_hash_table *htab;
1871 Elf_Internal_Shdr *symtab_hdr;
1872 struct elf_link_hash_entry **sym_hashes;
1873 const Elf_Internal_Rela *rel;
1874 const Elf_Internal_Rela *rel_end;
1877 bfd_boolean use_plt_got;
1879 if (bfd_link_relocatable (info))
1882 /* Don't do anything special with non-loaded, non-alloced sections.
1883 In particular, any relocs in such sections should not affect GOT
1884 and PLT reference counting (ie. we don't allow them to create GOT
1885 or PLT entries), there's no possibility or desire to optimize TLS
1886 relocs, and there's not much point in propagating relocs to shared
1887 libs that the dynamic linker won't relocate. */
1888 if ((sec->flags & SEC_ALLOC) == 0)
1891 BFD_ASSERT (is_i386_elf (abfd));
1893 htab = elf_i386_hash_table (info);
1896 sec->check_relocs_failed = 1;
1900 /* Get the section contents. */
1901 if (elf_section_data (sec)->this_hdr.contents != NULL)
1902 contents = elf_section_data (sec)->this_hdr.contents;
1903 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1905 sec->check_relocs_failed = 1;
1909 use_plt_got = (!get_elf_i386_backend_data (abfd)->is_vxworks
1910 && (get_elf_i386_backend_data (abfd)
1911 == &elf_i386_arch_bed));
1913 symtab_hdr = &elf_symtab_hdr (abfd);
1914 sym_hashes = elf_sym_hashes (abfd);
1918 rel_end = relocs + sec->reloc_count;
1919 for (rel = relocs; rel < rel_end; rel++)
1921 unsigned int r_type;
1922 unsigned long r_symndx;
1923 struct elf_link_hash_entry *h;
1924 struct elf_i386_link_hash_entry *eh;
1925 Elf_Internal_Sym *isym;
1927 bfd_boolean size_reloc;
1929 r_symndx = ELF32_R_SYM (rel->r_info);
1930 r_type = ELF32_R_TYPE (rel->r_info);
1932 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1934 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1940 if (r_symndx < symtab_hdr->sh_info)
1942 /* A local symbol. */
1943 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1948 /* Check relocation against local STT_GNU_IFUNC symbol. */
1949 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1951 h = elf_i386_get_local_sym_hash (htab, abfd, rel, TRUE);
1955 /* Fake a STT_GNU_IFUNC symbol. */
1956 h->type = STT_GNU_IFUNC;
1959 h->forced_local = 1;
1960 h->root.type = bfd_link_hash_defined;
1968 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1969 while (h->root.type == bfd_link_hash_indirect
1970 || h->root.type == bfd_link_hash_warning)
1971 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1974 eh = (struct elf_i386_link_hash_entry *) h;
1989 if (htab->elf.dynobj == NULL)
1990 htab->elf.dynobj = abfd;
1991 /* Create the ifunc sections for static executables. */
1992 if (h->type == STT_GNU_IFUNC
1993 && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj,
1999 /* It is referenced by a non-shared object. */
2001 h->root.non_ir_ref = 1;
2003 if (h->type == STT_GNU_IFUNC)
2004 elf_tdata (info->output_bfd)->has_gnu_symbols
2005 |= elf_gnu_symbol_ifunc;
2008 if (! elf_i386_tls_transition (info, abfd, sec, contents,
2009 symtab_hdr, sym_hashes,
2010 &r_type, GOT_UNKNOWN,
2011 rel, rel_end, h, r_symndx, FALSE))
2017 htab->tls_ldm_got.refcount += 1;
2021 /* This symbol requires a procedure linkage table entry. We
2022 actually build the entry in adjust_dynamic_symbol,
2023 because this might be a case of linking PIC code which is
2024 never referenced by a dynamic object, in which case we
2025 don't need to generate a procedure linkage table entry
2028 /* If this is a local symbol, we resolve it directly without
2029 creating a procedure linkage table entry. */
2033 eh->has_got_reloc = 1;
2035 h->plt.refcount += 1;
2042 case R_386_TLS_IE_32:
2044 case R_386_TLS_GOTIE:
2045 if (!bfd_link_executable (info))
2046 info->flags |= DF_STATIC_TLS;
2052 case R_386_TLS_GOTDESC:
2053 case R_386_TLS_DESC_CALL:
2054 /* This symbol requires a global offset table entry. */
2056 int tls_type, old_tls_type;
2063 tls_type = GOT_NORMAL;
2065 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
2066 case R_386_TLS_GOTDESC:
2067 case R_386_TLS_DESC_CALL:
2068 tls_type = GOT_TLS_GDESC; break;
2069 case R_386_TLS_IE_32:
2070 if (ELF32_R_TYPE (rel->r_info) == r_type)
2071 tls_type = GOT_TLS_IE_NEG;
2073 /* If this is a GD->IE transition, we may use either of
2074 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
2075 tls_type = GOT_TLS_IE;
2078 case R_386_TLS_GOTIE:
2079 tls_type = GOT_TLS_IE_POS; break;
2084 h->got.refcount += 1;
2085 old_tls_type = elf_i386_hash_entry(h)->tls_type;
2089 bfd_signed_vma *local_got_refcounts;
2091 /* This is a global offset table entry for a local symbol. */
2092 local_got_refcounts = elf_local_got_refcounts (abfd);
2093 if (local_got_refcounts == NULL)
2097 size = symtab_hdr->sh_info;
2098 size *= (sizeof (bfd_signed_vma)
2099 + sizeof (bfd_vma) + sizeof(char));
2100 local_got_refcounts = (bfd_signed_vma *)
2101 bfd_zalloc (abfd, size);
2102 if (local_got_refcounts == NULL)
2104 elf_local_got_refcounts (abfd) = local_got_refcounts;
2105 elf_i386_local_tlsdesc_gotent (abfd)
2106 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
2107 elf_i386_local_got_tls_type (abfd)
2108 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
2110 local_got_refcounts[r_symndx] += 1;
2111 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
2114 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
2115 tls_type |= old_tls_type;
2116 /* If a TLS symbol is accessed using IE at least once,
2117 there is no point to use dynamic model for it. */
2118 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2119 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2120 || (tls_type & GOT_TLS_IE) == 0))
2122 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
2123 tls_type = old_tls_type;
2124 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2125 && GOT_TLS_GD_ANY_P (tls_type))
2126 tls_type |= old_tls_type;
2130 name = h->root.root.string;
2132 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2134 (*_bfd_error_handler)
2135 (_("%B: `%s' accessed both as normal and "
2136 "thread local symbol"),
2138 bfd_set_error (bfd_error_bad_value);
2143 if (old_tls_type != tls_type)
2146 elf_i386_hash_entry (h)->tls_type = tls_type;
2148 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
2156 if (htab->elf.sgot == NULL)
2158 if (htab->elf.dynobj == NULL)
2159 htab->elf.dynobj = abfd;
2160 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
2163 if (r_type != R_386_TLS_IE)
2166 eh->has_got_reloc = 1;
2171 case R_386_TLS_LE_32:
2174 eh->has_got_reloc = 1;
2175 if (bfd_link_executable (info))
2177 info->flags |= DF_STATIC_TLS;
2182 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2183 eh->has_non_got_reloc = 1;
2185 /* We are called after all symbols have been resolved. Only
2186 relocation against STT_GNU_IFUNC symbol must go through
2189 && (bfd_link_executable (info)
2190 || h->type == STT_GNU_IFUNC))
2192 /* If this reloc is in a read-only section, we might
2193 need a copy reloc. We can't check reliably at this
2194 stage whether the section is read-only, as input
2195 sections have not yet been mapped to output sections.
2196 Tentatively set the flag for now, and correct in
2197 adjust_dynamic_symbol. */
2200 /* We may need a .plt entry if the symbol is a function
2201 defined in a shared lib or is a STT_GNU_IFUNC function
2202 referenced from the code or read-only section. */
2204 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2205 h->plt.refcount += 1;
2207 if (r_type == R_386_PC32)
2209 /* Since something like ".long foo - ." may be used
2210 as pointer, make sure that PLT is used if foo is
2211 a function defined in a shared library. */
2212 if ((sec->flags & SEC_CODE) == 0)
2213 h->pointer_equality_needed = 1;
2217 h->pointer_equality_needed = 1;
2218 /* R_386_32 can be resolved at run-time. */
2219 if (r_type == R_386_32
2220 && (sec->flags & SEC_READONLY) == 0)
2221 eh->func_pointer_refcount += 1;
2227 /* If we are creating a shared library, and this is a reloc
2228 against a global symbol, or a non PC relative reloc
2229 against a local symbol, then we need to copy the reloc
2230 into the shared library. However, if we are linking with
2231 -Bsymbolic, we do not need to copy a reloc against a
2232 global symbol which is defined in an object we are
2233 including in the link (i.e., DEF_REGULAR is set). At
2234 this point we have not seen all the input files, so it is
2235 possible that DEF_REGULAR is not set now but will be set
2236 later (it is never cleared). In case of a weak definition,
2237 DEF_REGULAR may be cleared later by a strong definition in
2238 a shared library. We account for that possibility below by
2239 storing information in the relocs_copied field of the hash
2240 table entry. A similar situation occurs when creating
2241 shared libraries and symbol visibility changes render the
2244 If on the other hand, we are creating an executable, we
2245 may need to keep relocations for symbols satisfied by a
2246 dynamic library if we manage to avoid copy relocs for the
2249 Generate dynamic pointer relocation against STT_GNU_IFUNC
2250 symbol in the non-code section. */
2251 if ((bfd_link_pic (info)
2252 && (r_type != R_386_PC32
2254 && (! (bfd_link_pie (info)
2255 || SYMBOLIC_BIND (info, h))
2256 || h->root.type == bfd_link_hash_defweak
2257 || !h->def_regular))))
2259 && h->type == STT_GNU_IFUNC
2260 && r_type == R_386_32
2261 && (sec->flags & SEC_CODE) == 0)
2262 || (ELIMINATE_COPY_RELOCS
2263 && !bfd_link_pic (info)
2265 && (h->root.type == bfd_link_hash_defweak
2266 || !h->def_regular)))
2268 struct elf_dyn_relocs *p;
2269 struct elf_dyn_relocs **head;
2271 /* We must copy these reloc types into the output file.
2272 Create a reloc section in dynobj and make room for
2276 if (htab->elf.dynobj == NULL)
2277 htab->elf.dynobj = abfd;
2279 sreloc = _bfd_elf_make_dynamic_reloc_section
2280 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
2286 /* If this is a global symbol, we count the number of
2287 relocations we need for this symbol. */
2290 head = &eh->dyn_relocs;
2294 /* Track dynamic relocs needed for local syms too.
2295 We really need local syms available to do this
2300 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2305 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2309 vpp = &elf_section_data (s)->local_dynrel;
2310 head = (struct elf_dyn_relocs **)vpp;
2314 if (p == NULL || p->sec != sec)
2316 bfd_size_type amt = sizeof *p;
2317 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
2329 /* Count size relocation as PC-relative relocation. */
2330 if (r_type == R_386_PC32 || size_reloc)
2335 /* This relocation describes the C++ object vtable hierarchy.
2336 Reconstruct it for later use during GC. */
2337 case R_386_GNU_VTINHERIT:
2338 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2342 /* This relocation describes which C++ vtable entries are actually
2343 used. Record for later use during GC. */
2344 case R_386_GNU_VTENTRY:
2345 BFD_ASSERT (h != NULL);
2347 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
2357 && h->plt.refcount > 0
2358 && (((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
2359 || h->got.refcount > 0)
2360 && htab->plt_got == NULL)
2362 /* Create the GOT procedure linkage table. */
2363 unsigned int plt_got_align;
2364 const struct elf_backend_data *bed;
2366 bed = get_elf_backend_data (info->output_bfd);
2367 BFD_ASSERT (sizeof (elf_i386_got_plt_entry) == 8
2368 && (sizeof (elf_i386_got_plt_entry)
2369 == sizeof (elf_i386_pic_got_plt_entry)));
2372 if (htab->elf.dynobj == NULL)
2373 htab->elf.dynobj = abfd;
2375 = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2377 (bed->dynamic_sec_flags
2382 if (htab->plt_got == NULL
2383 || !bfd_set_section_alignment (htab->elf.dynobj,
2388 if (!info->no_ld_generated_unwind_info
2389 && htab->plt_got_eh_frame == NULL
2390 && get_elf_i386_backend_data (abfd)->plt->eh_frame_plt_got != NULL)
2392 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2393 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2394 | SEC_LINKER_CREATED);
2395 htab->plt_got_eh_frame
2396 = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2399 if (htab->plt_got_eh_frame == NULL
2400 || !bfd_set_section_alignment (htab->elf.dynobj,
2401 htab->plt_got_eh_frame,
2407 if (r_type == R_386_GOT32X
2408 && (h == NULL || h->type != STT_GNU_IFUNC))
2409 sec->need_convert_load = 1;
2412 if (elf_section_data (sec)->this_hdr.contents != contents)
2414 if (!info->keep_memory)
2418 /* Cache the section contents for elf_link_input_bfd. */
2419 elf_section_data (sec)->this_hdr.contents = contents;
2426 if (elf_section_data (sec)->this_hdr.contents != contents)
2428 sec->check_relocs_failed = 1;
2432 /* Return the section that should be marked against GC for a given
2436 elf_i386_gc_mark_hook (asection *sec,
2437 struct bfd_link_info *info,
2438 Elf_Internal_Rela *rel,
2439 struct elf_link_hash_entry *h,
2440 Elf_Internal_Sym *sym)
2443 switch (ELF32_R_TYPE (rel->r_info))
2445 case R_386_GNU_VTINHERIT:
2446 case R_386_GNU_VTENTRY:
2450 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2453 /* Remove undefined weak symbol from the dynamic symbol table if it
2454 is resolved to 0. */
2457 elf_i386_fixup_symbol (struct bfd_link_info *info,
2458 struct elf_link_hash_entry *h)
2460 if (h->dynindx != -1
2461 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2462 elf_i386_hash_entry (h)->has_got_reloc,
2463 elf_i386_hash_entry (h)))
2466 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2472 /* Adjust a symbol defined by a dynamic object and referenced by a
2473 regular object. The current definition is in some section of the
2474 dynamic object, but we're not including those sections. We have to
2475 change the definition to something the rest of the link can
2479 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
2480 struct elf_link_hash_entry *h)
2482 struct elf_i386_link_hash_table *htab;
2484 struct elf_i386_link_hash_entry *eh;
2485 struct elf_dyn_relocs *p;
2487 /* STT_GNU_IFUNC symbol must go through PLT. */
2488 if (h->type == STT_GNU_IFUNC)
2490 /* All local STT_GNU_IFUNC references must be treate as local
2491 calls via local PLT. */
2493 && SYMBOL_CALLS_LOCAL (info, h))
2495 bfd_size_type pc_count = 0, count = 0;
2496 struct elf_dyn_relocs **pp;
2498 eh = (struct elf_i386_link_hash_entry *) h;
2499 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2501 pc_count += p->pc_count;
2502 p->count -= p->pc_count;
2511 if (pc_count || count)
2516 /* Increment PLT reference count only for PC-relative
2519 if (h->plt.refcount <= 0)
2520 h->plt.refcount = 1;
2522 h->plt.refcount += 1;
2527 if (h->plt.refcount <= 0)
2529 h->plt.offset = (bfd_vma) -1;
2535 /* If this is a function, put it in the procedure linkage table. We
2536 will fill in the contents of the procedure linkage table later,
2537 when we know the address of the .got section. */
2538 if (h->type == STT_FUNC
2541 if (h->plt.refcount <= 0
2542 || SYMBOL_CALLS_LOCAL (info, h)
2543 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2544 && h->root.type == bfd_link_hash_undefweak))
2546 /* This case can occur if we saw a PLT32 reloc in an input
2547 file, but the symbol was never referred to by a dynamic
2548 object, or if all references were garbage collected. In
2549 such a case, we don't actually need to build a procedure
2550 linkage table, and we can just do a PC32 reloc instead. */
2551 h->plt.offset = (bfd_vma) -1;
2558 /* It's possible that we incorrectly decided a .plt reloc was
2559 needed for an R_386_PC32 reloc to a non-function sym in
2560 check_relocs. We can't decide accurately between function and
2561 non-function syms in check-relocs; Objects loaded later in
2562 the link may change h->type. So fix it now. */
2563 h->plt.offset = (bfd_vma) -1;
2565 /* If this is a weak symbol, and there is a real definition, the
2566 processor independent code will have arranged for us to see the
2567 real definition first, and we can just use the same value. */
2568 if (h->u.weakdef != NULL)
2570 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2571 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2572 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2573 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2574 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
2575 h->non_got_ref = h->u.weakdef->non_got_ref;
2579 /* This is a reference to a symbol defined by a dynamic object which
2580 is not a function. */
2582 /* If we are creating a shared library, we must presume that the
2583 only references to the symbol are via the global offset table.
2584 For such cases we need not do anything here; the relocations will
2585 be handled correctly by relocate_section. */
2586 if (!bfd_link_executable (info))
2589 /* If there are no references to this symbol that do not use the
2590 GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
2592 eh = (struct elf_i386_link_hash_entry *) h;
2593 if (!h->non_got_ref && !eh->gotoff_ref)
2596 /* If -z nocopyreloc was given, we won't generate them either. */
2597 if (info->nocopyreloc)
2603 htab = elf_i386_hash_table (info);
2607 /* If there aren't any dynamic relocs in read-only sections nor
2608 R_386_GOTOFF relocation, then we can keep the dynamic relocs and
2609 avoid the copy reloc. This doesn't work on VxWorks, where we can
2610 not have dynamic relocations (other than copy and jump slot
2611 relocations) in an executable. */
2612 if (ELIMINATE_COPY_RELOCS
2614 && !get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
2616 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2618 s = p->sec->output_section;
2619 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2630 /* We must allocate the symbol in our .dynbss section, which will
2631 become part of the .bss section of the executable. There will be
2632 an entry for this symbol in the .dynsym section. The dynamic
2633 object will contain position independent code, so all references
2634 from the dynamic object to this symbol will go through the global
2635 offset table. The dynamic linker will use the .dynsym entry to
2636 determine the address it must put in the global offset table, so
2637 both the dynamic object and the regular object will refer to the
2638 same memory location for the variable. */
2640 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
2641 copy the initial value out of the dynamic object and into the
2642 runtime process image. */
2643 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2645 htab->srelbss->size += sizeof (Elf32_External_Rel);
2651 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2654 /* Allocate space in .plt, .got and associated reloc sections for
2658 elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2660 struct bfd_link_info *info;
2661 struct elf_i386_link_hash_table *htab;
2662 struct elf_i386_link_hash_entry *eh;
2663 struct elf_dyn_relocs *p;
2664 unsigned plt_entry_size;
2665 bfd_boolean resolved_to_zero;
2667 if (h->root.type == bfd_link_hash_indirect)
2670 eh = (struct elf_i386_link_hash_entry *) h;
2672 info = (struct bfd_link_info *) inf;
2673 htab = elf_i386_hash_table (info);
2677 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
2679 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2683 /* Clear the reference count of function pointer relocations if
2684 symbol isn't a normal function. */
2685 if (h->type != STT_FUNC)
2686 eh->func_pointer_refcount = 0;
2688 /* We can't use the GOT PLT if pointer equality is needed since
2689 finish_dynamic_symbol won't clear symbol value and the dynamic
2690 linker won't update the GOT slot. We will get into an infinite
2691 loop at run-time. */
2692 if (htab->plt_got != NULL
2693 && h->type != STT_GNU_IFUNC
2694 && !h->pointer_equality_needed
2695 && h->plt.refcount > 0
2696 && h->got.refcount > 0)
2698 /* Don't use the regular PLT if there are both GOT and GOTPLT
2700 h->plt.offset = (bfd_vma) -1;
2702 /* Use the GOT PLT. */
2703 eh->plt_got.refcount = 1;
2706 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2707 here if it is defined and referenced in a non-shared object. */
2708 if (h->type == STT_GNU_IFUNC
2710 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
2711 &htab->readonly_dynrelocs_against_ifunc,
2713 plt_entry_size, 4, TRUE);
2714 /* Don't create the PLT entry if there are only function pointer
2715 relocations which can be resolved at run-time. */
2716 else if (htab->elf.dynamic_sections_created
2717 && (h->plt.refcount > eh->func_pointer_refcount
2718 || eh->plt_got.refcount > 0))
2720 bfd_boolean use_plt_got;
2722 /* Clear the reference count of function pointer relocations
2724 eh->func_pointer_refcount = 0;
2726 if ((info->flags & DF_BIND_NOW) && !h->pointer_equality_needed)
2728 /* Don't use the regular PLT for DF_BIND_NOW. */
2729 h->plt.offset = (bfd_vma) -1;
2731 /* Use the GOT PLT. */
2732 h->got.refcount = 1;
2733 eh->plt_got.refcount = 1;
2736 use_plt_got = eh->plt_got.refcount > 0;
2738 /* Make sure this symbol is output as a dynamic symbol.
2739 Undefined weak syms won't yet be marked as dynamic. */
2740 if (h->dynindx == -1
2742 && !resolved_to_zero)
2744 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2748 if (bfd_link_pic (info)
2749 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2751 asection *s = htab->elf.splt;
2752 asection *got_s = htab->plt_got;
2754 /* If this is the first .plt entry, make room for the special
2755 first entry. The .plt section is used by prelink to undo
2756 prelinking for dynamic relocations. */
2758 s->size = plt_entry_size;
2761 eh->plt_got.offset = got_s->size;
2763 h->plt.offset = s->size;
2765 /* If this symbol is not defined in a regular file, and we are
2766 not generating a shared library, then set the symbol to this
2767 location in the .plt. This is required to make function
2768 pointers compare as equal between the normal executable and
2769 the shared library. */
2770 if (! bfd_link_pic (info)
2775 /* We need to make a call to the entry of the GOT PLT
2776 instead of regular PLT entry. */
2777 h->root.u.def.section = got_s;
2778 h->root.u.def.value = eh->plt_got.offset;
2782 h->root.u.def.section = s;
2783 h->root.u.def.value = h->plt.offset;
2787 /* Make room for this entry. */
2789 got_s->size += sizeof (elf_i386_got_plt_entry);
2792 s->size += plt_entry_size;
2794 /* We also need to make an entry in the .got.plt section,
2795 which will be placed in the .got section by the linker
2797 htab->elf.sgotplt->size += 4;
2799 /* There should be no PLT relocation against resolved
2800 undefined weak symbol in executable. */
2801 if (!resolved_to_zero)
2803 /* We also need to make an entry in the .rel.plt
2805 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2806 htab->elf.srelplt->reloc_count++;
2810 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks
2811 && !bfd_link_pic (info))
2813 /* VxWorks has a second set of relocations for each PLT entry
2814 in executables. They go in a separate relocation section,
2815 which is processed by the kernel loader. */
2817 /* There are two relocations for the initial PLT entry: an
2818 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
2819 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
2821 if (h->plt.offset == plt_entry_size)
2822 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2824 /* There are two extra relocations for each subsequent PLT entry:
2825 an R_386_32 relocation for the GOT entry, and an R_386_32
2826 relocation for the PLT entry. */
2828 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2833 eh->plt_got.offset = (bfd_vma) -1;
2834 h->plt.offset = (bfd_vma) -1;
2840 eh->plt_got.offset = (bfd_vma) -1;
2841 h->plt.offset = (bfd_vma) -1;
2845 eh->tlsdesc_got = (bfd_vma) -1;
2847 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
2848 make it a R_386_TLS_LE_32 requiring no TLS entry. */
2849 if (h->got.refcount > 0
2850 && bfd_link_executable (info)
2852 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
2853 h->got.offset = (bfd_vma) -1;
2854 else if (h->got.refcount > 0)
2858 int tls_type = elf_i386_hash_entry(h)->tls_type;
2860 /* Make sure this symbol is output as a dynamic symbol.
2861 Undefined weak syms won't yet be marked as dynamic. */
2862 if (h->dynindx == -1
2864 && !resolved_to_zero)
2866 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2871 if (GOT_TLS_GDESC_P (tls_type))
2873 eh->tlsdesc_got = htab->elf.sgotplt->size
2874 - elf_i386_compute_jump_table_size (htab);
2875 htab->elf.sgotplt->size += 8;
2876 h->got.offset = (bfd_vma) -2;
2878 if (! GOT_TLS_GDESC_P (tls_type)
2879 || GOT_TLS_GD_P (tls_type))
2881 h->got.offset = s->size;
2883 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
2884 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
2887 dyn = htab->elf.dynamic_sections_created;
2888 /* R_386_TLS_IE_32 needs one dynamic relocation,
2889 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
2890 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
2891 need two), R_386_TLS_GD needs one if local symbol and two if
2892 global. No dynamic relocation against resolved undefined weak
2893 symbol in executable. */
2894 if (tls_type == GOT_TLS_IE_BOTH)
2895 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2896 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2897 || (tls_type & GOT_TLS_IE))
2898 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2899 else if (GOT_TLS_GD_P (tls_type))
2900 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2901 else if (! GOT_TLS_GDESC_P (tls_type)
2902 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2903 && !resolved_to_zero)
2904 || h->root.type != bfd_link_hash_undefweak)
2905 && (bfd_link_pic (info)
2906 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2907 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2908 if (GOT_TLS_GDESC_P (tls_type))
2909 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2912 h->got.offset = (bfd_vma) -1;
2914 if (eh->dyn_relocs == NULL)
2917 /* In the shared -Bsymbolic case, discard space allocated for
2918 dynamic pc-relative relocs against symbols which turn out to be
2919 defined in regular objects. For the normal shared case, discard
2920 space for pc-relative relocs that have become local due to symbol
2921 visibility changes. */
2923 if (bfd_link_pic (info))
2925 /* The only reloc that uses pc_count is R_386_PC32, which will
2926 appear on a call or on something like ".long foo - .". We
2927 want calls to protected symbols to resolve directly to the
2928 function rather than going via the plt. If people want
2929 function pointer comparisons to work as expected then they
2930 should avoid writing assembly like ".long foo - .". */
2931 if (SYMBOL_CALLS_LOCAL (info, h))
2933 struct elf_dyn_relocs **pp;
2935 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2937 p->count -= p->pc_count;
2946 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
2948 struct elf_dyn_relocs **pp;
2949 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2951 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2958 /* Also discard relocs on undefined weak syms with non-default
2959 visibility or in PIE. */
2960 if (eh->dyn_relocs != NULL
2961 && h->root.type == bfd_link_hash_undefweak)
2963 /* Undefined weak symbol is never bound locally in shared
2965 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2966 || resolved_to_zero)
2970 /* Keep dynamic non-GOT/non-PLT relocation so that we
2971 can branch to 0 without PLT. */
2972 struct elf_dyn_relocs **pp;
2974 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2975 if (p->pc_count == 0)
2979 /* Remove non-R_386_PC32 relocation. */
2980 p->count = p->pc_count;
2984 if (eh->dyn_relocs != NULL)
2986 /* Make sure undefined weak symbols are output
2987 as dynamic symbols in PIEs for dynamic non-GOT
2988 non-PLT reloations. */
2989 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2994 eh->dyn_relocs = NULL;
2996 else if (h->dynindx == -1
2997 && !h->forced_local)
2999 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3004 else if (ELIMINATE_COPY_RELOCS)
3006 /* For the non-shared case, discard space for relocs against
3007 symbols which turn out to need copy relocs or are not
3008 dynamic. Keep dynamic relocations for run-time function
3009 pointer initialization. */
3011 if ((!h->non_got_ref
3012 || eh->func_pointer_refcount > 0
3013 || (h->root.type == bfd_link_hash_undefweak
3014 && !resolved_to_zero))
3017 || (htab->elf.dynamic_sections_created
3018 && (h->root.type == bfd_link_hash_undefweak
3019 || h->root.type == bfd_link_hash_undefined))))
3021 /* Make sure this symbol is output as a dynamic symbol.
3022 Undefined weak syms won't yet be marked as dynamic. */
3023 if (h->dynindx == -1
3025 && !resolved_to_zero)
3027 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3031 /* If that succeeded, we know we'll be keeping all the
3033 if (h->dynindx != -1)
3037 eh->dyn_relocs = NULL;
3038 eh->func_pointer_refcount = 0;
3043 /* Finally, allocate space. */
3044 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3048 sreloc = elf_section_data (p->sec)->sreloc;
3050 BFD_ASSERT (sreloc != NULL);
3051 sreloc->size += p->count * sizeof (Elf32_External_Rel);
3057 /* Allocate space in .plt, .got and associated reloc sections for
3058 local dynamic relocs. */
3061 elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
3063 struct elf_link_hash_entry *h
3064 = (struct elf_link_hash_entry *) *slot;
3066 if (h->type != STT_GNU_IFUNC
3070 || h->root.type != bfd_link_hash_defined)
3073 return elf_i386_allocate_dynrelocs (h, inf);
3076 /* Find any dynamic relocs that apply to read-only sections. */
3079 elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3081 struct elf_i386_link_hash_entry *eh;
3082 struct elf_dyn_relocs *p;
3084 /* Skip local IFUNC symbols. */
3085 if (h->forced_local && h->type == STT_GNU_IFUNC)
3088 eh = (struct elf_i386_link_hash_entry *) h;
3089 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3091 asection *s = p->sec->output_section;
3093 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3095 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3097 info->flags |= DF_TEXTREL;
3099 if ((info->warn_shared_textrel && bfd_link_pic (info))
3100 || info->error_textrel)
3101 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
3102 p->sec->owner, h->root.root.string,
3105 /* Not an error, just cut short the traversal. */
3112 /* Convert load via the GOT slot to load immediate. */
3115 elf_i386_convert_load (bfd *abfd, asection *sec,
3116 struct bfd_link_info *link_info)
3118 struct elf_i386_link_hash_table *htab;
3119 Elf_Internal_Shdr *symtab_hdr;
3120 Elf_Internal_Rela *internal_relocs;
3121 Elf_Internal_Rela *irel, *irelend;
3123 bfd_boolean changed;
3124 bfd_signed_vma *local_got_refcounts;
3126 /* Don't even try to convert non-ELF outputs. */
3127 if (!is_elf_hash_table (link_info->hash))
3130 /* Nothing to do if there is no need or no output. */
3131 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
3132 || sec->need_convert_load == 0
3133 || bfd_is_abs_section (sec->output_section))
3136 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3138 /* Load the relocations for this section. */
3139 internal_relocs = (_bfd_elf_link_read_relocs
3140 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
3141 link_info->keep_memory));
3142 if (internal_relocs == NULL)
3146 htab = elf_i386_hash_table (link_info);
3147 local_got_refcounts = elf_local_got_refcounts (abfd);
3149 /* Get the section contents. */
3150 if (elf_section_data (sec)->this_hdr.contents != NULL)
3151 contents = elf_section_data (sec)->this_hdr.contents;
3154 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3158 irelend = internal_relocs + sec->reloc_count;
3159 for (irel = internal_relocs; irel < irelend; irel++)
3161 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
3162 unsigned int r_symndx;
3163 struct elf_link_hash_entry *h;
3164 bfd_boolean converted;
3166 /* Don't convert R_386_GOT32 since we can't tell if it is applied
3167 to "mov $foo@GOT, %reg" which isn't a load via GOT. */
3168 if (r_type != R_386_GOT32X)
3171 r_symndx = ELF32_R_SYM (irel->r_info);
3172 if (r_symndx < symtab_hdr->sh_info)
3173 h = elf_i386_get_local_sym_hash (htab, sec->owner,
3174 (const Elf_Internal_Rela *) irel,
3178 h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
3179 while (h->root.type == bfd_link_hash_indirect
3180 || h->root.type == bfd_link_hash_warning)
3181 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3184 /* STT_GNU_IFUNC must keep GOT32 relocations. */
3185 if (h != NULL && h->type == STT_GNU_IFUNC)
3189 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
3190 irel, h, &converted, link_info))
3195 changed = converted;
3198 if (h->got.refcount > 0)
3199 h->got.refcount -= 1;
3203 if (local_got_refcounts != NULL
3204 && local_got_refcounts[r_symndx] > 0)
3205 local_got_refcounts[r_symndx] -= 1;
3210 if (contents != NULL
3211 && elf_section_data (sec)->this_hdr.contents != contents)
3213 if (!changed && !link_info->keep_memory)
3217 /* Cache the section contents for elf_link_input_bfd. */
3218 elf_section_data (sec)->this_hdr.contents = contents;
3222 if (elf_section_data (sec)->relocs != internal_relocs)
3225 free (internal_relocs);
3227 elf_section_data (sec)->relocs = internal_relocs;
3233 if (contents != NULL
3234 && elf_section_data (sec)->this_hdr.contents != contents)
3236 if (internal_relocs != NULL
3237 && elf_section_data (sec)->relocs != internal_relocs)
3238 free (internal_relocs);
3242 /* Set the sizes of the dynamic sections. */
3245 elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3247 struct elf_i386_link_hash_table *htab;
3253 htab = elf_i386_hash_table (info);
3256 dynobj = htab->elf.dynobj;
3260 /* Set up .got offsets for local syms, and space for local dynamic
3262 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3264 bfd_signed_vma *local_got;
3265 bfd_signed_vma *end_local_got;
3266 char *local_tls_type;
3267 bfd_vma *local_tlsdesc_gotent;
3268 bfd_size_type locsymcount;
3269 Elf_Internal_Shdr *symtab_hdr;
3272 if (! is_i386_elf (ibfd))
3275 for (s = ibfd->sections; s != NULL; s = s->next)
3277 struct elf_dyn_relocs *p;
3279 if (!elf_i386_convert_load (ibfd, s, info))
3282 for (p = ((struct elf_dyn_relocs *)
3283 elf_section_data (s)->local_dynrel);
3287 if (!bfd_is_abs_section (p->sec)
3288 && bfd_is_abs_section (p->sec->output_section))
3290 /* Input section has been discarded, either because
3291 it is a copy of a linkonce section or due to
3292 linker script /DISCARD/, so we'll be discarding
3295 else if (get_elf_i386_backend_data (output_bfd)->is_vxworks
3296 && strcmp (p->sec->output_section->name,
3299 /* Relocations in vxworks .tls_vars sections are
3300 handled specially by the loader. */
3302 else if (p->count != 0)
3304 srel = elf_section_data (p->sec)->sreloc;
3305 srel->size += p->count * sizeof (Elf32_External_Rel);
3306 if ((p->sec->output_section->flags & SEC_READONLY) != 0
3307 && (info->flags & DF_TEXTREL) == 0)
3309 info->flags |= DF_TEXTREL;
3310 if ((info->warn_shared_textrel && bfd_link_pic (info))
3311 || info->error_textrel)
3312 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
3313 p->sec->owner, p->sec);
3319 local_got = elf_local_got_refcounts (ibfd);
3323 symtab_hdr = &elf_symtab_hdr (ibfd);
3324 locsymcount = symtab_hdr->sh_info;
3325 end_local_got = local_got + locsymcount;
3326 local_tls_type = elf_i386_local_got_tls_type (ibfd);
3327 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
3329 srel = htab->elf.srelgot;
3330 for (; local_got < end_local_got;
3331 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
3333 *local_tlsdesc_gotent = (bfd_vma) -1;
3336 if (GOT_TLS_GDESC_P (*local_tls_type))
3338 *local_tlsdesc_gotent = htab->elf.sgotplt->size
3339 - elf_i386_compute_jump_table_size (htab);
3340 htab->elf.sgotplt->size += 8;
3341 *local_got = (bfd_vma) -2;
3343 if (! GOT_TLS_GDESC_P (*local_tls_type)
3344 || GOT_TLS_GD_P (*local_tls_type))
3346 *local_got = s->size;
3348 if (GOT_TLS_GD_P (*local_tls_type)
3349 || *local_tls_type == GOT_TLS_IE_BOTH)
3352 if (bfd_link_pic (info)
3353 || GOT_TLS_GD_ANY_P (*local_tls_type)
3354 || (*local_tls_type & GOT_TLS_IE))
3356 if (*local_tls_type == GOT_TLS_IE_BOTH)
3357 srel->size += 2 * sizeof (Elf32_External_Rel);
3358 else if (GOT_TLS_GD_P (*local_tls_type)
3359 || ! GOT_TLS_GDESC_P (*local_tls_type))
3360 srel->size += sizeof (Elf32_External_Rel);
3361 if (GOT_TLS_GDESC_P (*local_tls_type))
3362 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
3366 *local_got = (bfd_vma) -1;
3370 if (htab->tls_ldm_got.refcount > 0)
3372 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
3374 htab->tls_ldm_got.offset = htab->elf.sgot->size;
3375 htab->elf.sgot->size += 8;
3376 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
3379 htab->tls_ldm_got.offset = -1;
3381 /* Allocate global sym .plt and .got entries, and space for global
3382 sym dynamic relocs. */
3383 elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
3385 /* Allocate .plt and .got entries, and space for local symbols. */
3386 htab_traverse (htab->loc_hash_table,
3387 elf_i386_allocate_local_dynrelocs,
3390 /* For every jump slot reserved in the sgotplt, reloc_count is
3391 incremented. However, when we reserve space for TLS descriptors,
3392 it's not incremented, so in order to compute the space reserved
3393 for them, it suffices to multiply the reloc count by the jump
3396 PR ld/13302: We start next_irelative_index at the end of .rela.plt
3397 so that R_386_IRELATIVE entries come last. */
3398 if (htab->elf.srelplt)
3400 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
3401 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
3402 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
3404 else if (htab->elf.irelplt)
3405 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
3408 if (htab->elf.sgotplt)
3410 /* Don't allocate .got.plt section if there are no GOT nor PLT
3411 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
3412 if ((htab->elf.hgot == NULL
3413 || !htab->elf.hgot->ref_regular_nonweak)
3414 && (htab->elf.sgotplt->size
3415 == get_elf_backend_data (output_bfd)->got_header_size)
3416 && (htab->elf.splt == NULL
3417 || htab->elf.splt->size == 0)
3418 && (htab->elf.sgot == NULL
3419 || htab->elf.sgot->size == 0)
3420 && (htab->elf.iplt == NULL
3421 || htab->elf.iplt->size == 0)
3422 && (htab->elf.igotplt == NULL
3423 || htab->elf.igotplt->size == 0))
3424 htab->elf.sgotplt->size = 0;
3427 if (_bfd_elf_eh_frame_present (info))
3429 if (htab->plt_eh_frame != NULL
3430 && htab->elf.splt != NULL
3431 && htab->elf.splt->size != 0
3432 && !bfd_is_abs_section (htab->elf.splt->output_section))
3433 htab->plt_eh_frame->size =
3434 get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt_size;
3436 if (htab->plt_got_eh_frame != NULL
3437 && htab->plt_got != NULL
3438 && htab->plt_got->size != 0
3439 && !bfd_is_abs_section (htab->plt_got->output_section))
3440 htab->plt_got_eh_frame->size =
3441 get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt_got_size;
3444 /* We now have determined the sizes of the various dynamic sections.
3445 Allocate memory for them. */
3447 for (s = dynobj->sections; s != NULL; s = s->next)
3449 bfd_boolean strip_section = TRUE;
3451 if ((s->flags & SEC_LINKER_CREATED) == 0)
3454 if (s == htab->elf.splt
3455 || s == htab->elf.sgot)
3457 /* Strip this section if we don't need it; see the
3459 /* We'd like to strip these sections if they aren't needed, but if
3460 we've exported dynamic symbols from them we must leave them.
3461 It's too late to tell BFD to get rid of the symbols. */
3463 if (htab->elf.hplt != NULL)
3464 strip_section = FALSE;
3466 else if (s == htab->elf.sgotplt
3467 || s == htab->elf.iplt
3468 || s == htab->elf.igotplt
3469 || s == htab->plt_got
3470 || s == htab->plt_eh_frame
3471 || s == htab->plt_got_eh_frame
3472 || s == htab->sdynbss)
3474 /* Strip these too. */
3476 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
3479 && s != htab->elf.srelplt
3480 && s != htab->srelplt2)
3483 /* We use the reloc_count field as a counter if we need
3484 to copy relocs into the output file. */
3489 /* It's not one of our sections, so don't allocate space. */
3495 /* If we don't need this section, strip it from the
3496 output file. This is mostly to handle .rel.bss and
3497 .rel.plt. We must create both sections in
3498 create_dynamic_sections, because they must be created
3499 before the linker maps input sections to output
3500 sections. The linker does that before
3501 adjust_dynamic_symbol is called, and it is that
3502 function which decides whether anything needs to go
3503 into these sections. */
3505 s->flags |= SEC_EXCLUDE;
3509 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3512 /* Allocate memory for the section contents. We use bfd_zalloc
3513 here in case unused entries are not reclaimed before the
3514 section's contents are written out. This should not happen,
3515 but this way if it does, we get a R_386_NONE reloc instead
3517 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
3518 if (s->contents == NULL)
3522 if (htab->plt_eh_frame != NULL
3523 && htab->plt_eh_frame->contents != NULL)
3525 memcpy (htab->plt_eh_frame->contents, elf_i386_eh_frame_plt,
3526 sizeof (elf_i386_eh_frame_plt));
3527 bfd_put_32 (dynobj, htab->elf.splt->size,
3528 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3531 if (htab->plt_got_eh_frame != NULL
3532 && htab->plt_got_eh_frame->contents != NULL)
3534 memcpy (htab->plt_got_eh_frame->contents,
3535 get_elf_i386_backend_data (output_bfd)->plt->eh_frame_plt_got,
3536 htab->plt_got_eh_frame->size);
3537 bfd_put_32 (dynobj, htab->plt_got->size,
3538 (htab->plt_got_eh_frame->contents
3539 + PLT_FDE_LEN_OFFSET));
3542 if (htab->elf.dynamic_sections_created)
3544 /* Add some entries to the .dynamic section. We fill in the
3545 values later, in elf_i386_finish_dynamic_sections, but we
3546 must add the entries now so that we get the correct size for
3547 the .dynamic section. The DT_DEBUG entry is filled in by the
3548 dynamic linker and used by the debugger. */
3549 #define add_dynamic_entry(TAG, VAL) \
3550 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3552 if (bfd_link_executable (info))
3554 if (!add_dynamic_entry (DT_DEBUG, 0))
3558 if (htab->elf.splt->size != 0)
3560 /* DT_PLTGOT is used by prelink even if there is no PLT
3562 if (!add_dynamic_entry (DT_PLTGOT, 0))
3565 if (htab->elf.srelplt->size != 0)
3567 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3568 || !add_dynamic_entry (DT_PLTREL, DT_REL)
3569 || !add_dynamic_entry (DT_JMPREL, 0))
3576 if (!add_dynamic_entry (DT_REL, 0)
3577 || !add_dynamic_entry (DT_RELSZ, 0)
3578 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
3581 /* If any dynamic relocs apply to a read-only section,
3582 then we need a DT_TEXTREL entry. */
3583 if ((info->flags & DF_TEXTREL) == 0)
3584 elf_link_hash_traverse (&htab->elf,
3585 elf_i386_readonly_dynrelocs, info);
3587 if ((info->flags & DF_TEXTREL) != 0)
3589 if (htab->readonly_dynrelocs_against_ifunc)
3591 info->callbacks->einfo
3592 (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
3593 bfd_set_error (bfd_error_bad_value);
3597 if (!add_dynamic_entry (DT_TEXTREL, 0))
3601 if (get_elf_i386_backend_data (output_bfd)->is_vxworks
3602 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3605 #undef add_dynamic_entry
3611 elf_i386_always_size_sections (bfd *output_bfd,
3612 struct bfd_link_info *info)
3614 asection *tls_sec = elf_hash_table (info)->tls_sec;
3618 struct elf_link_hash_entry *tlsbase;
3620 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3621 "_TLS_MODULE_BASE_",
3622 FALSE, FALSE, FALSE);
3624 if (tlsbase && tlsbase->type == STT_TLS)
3626 struct elf_i386_link_hash_table *htab;
3627 struct bfd_link_hash_entry *bh = NULL;
3628 const struct elf_backend_data *bed
3629 = get_elf_backend_data (output_bfd);
3631 htab = elf_i386_hash_table (info);
3635 if (!(_bfd_generic_link_add_one_symbol
3636 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3637 tls_sec, 0, NULL, FALSE,
3638 bed->collect, &bh)))
3641 htab->tls_module_base = bh;
3643 tlsbase = (struct elf_link_hash_entry *)bh;
3644 tlsbase->def_regular = 1;
3645 tlsbase->other = STV_HIDDEN;
3646 tlsbase->root.linker_def = 1;
3647 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
3654 /* Set the correct type for an x86 ELF section. We do this by the
3655 section name, which is a hack, but ought to work. */
3658 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
3659 Elf_Internal_Shdr *hdr,
3664 name = bfd_get_section_name (abfd, sec);
3666 /* This is an ugly, but unfortunately necessary hack that is
3667 needed when producing EFI binaries on x86. It tells
3668 elf.c:elf_fake_sections() not to consider ".reloc" as a section
3669 containing ELF relocation info. We need this hack in order to
3670 be able to generate ELF binaries that can be translated into
3671 EFI applications (which are essentially COFF objects). Those
3672 files contain a COFF ".reloc" section inside an ELFNN object,
3673 which would normally cause BFD to segfault because it would
3674 attempt to interpret this section as containing relocation
3675 entries for section "oc". With this hack enabled, ".reloc"
3676 will be treated as a normal data section, which will avoid the
3677 segfault. However, you won't be able to create an ELFNN binary
3678 with a section named "oc" that needs relocations, but that's
3679 the kind of ugly side-effects you get when detecting section
3680 types based on their names... In practice, this limitation is
3681 unlikely to bite. */
3682 if (strcmp (name, ".reloc") == 0)
3683 hdr->sh_type = SHT_PROGBITS;
3688 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
3689 executables. Rather than setting it to the beginning of the TLS
3690 section, we have to set it to the end. This function may be called
3691 multiple times, it is idempotent. */
3694 elf_i386_set_tls_module_base (struct bfd_link_info *info)
3696 struct elf_i386_link_hash_table *htab;
3697 struct bfd_link_hash_entry *base;
3699 if (!bfd_link_executable (info))
3702 htab = elf_i386_hash_table (info);
3706 base = htab->tls_module_base;
3710 base->u.def.value = htab->elf.tls_size;
3713 /* Return the base VMA address which should be subtracted from real addresses
3714 when resolving @dtpoff relocation.
3715 This is PT_TLS segment p_vaddr. */
3718 elf_i386_dtpoff_base (struct bfd_link_info *info)
3720 /* If tls_sec is NULL, we should have signalled an error already. */
3721 if (elf_hash_table (info)->tls_sec == NULL)
3723 return elf_hash_table (info)->tls_sec->vma;
3726 /* Return the relocation value for @tpoff relocation
3727 if STT_TLS virtual address is ADDRESS. */
3730 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
3732 struct elf_link_hash_table *htab = elf_hash_table (info);
3733 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3734 bfd_vma static_tls_size;
3736 /* If tls_sec is NULL, we should have signalled an error already. */
3737 if (htab->tls_sec == NULL)
3740 /* Consider special static TLS alignment requirements. */
3741 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3742 return static_tls_size + htab->tls_sec->vma - address;
3745 /* Relocate an i386 ELF section. */
3748 elf_i386_relocate_section (bfd *output_bfd,
3749 struct bfd_link_info *info,
3751 asection *input_section,
3753 Elf_Internal_Rela *relocs,
3754 Elf_Internal_Sym *local_syms,
3755 asection **local_sections)
3757 struct elf_i386_link_hash_table *htab;
3758 Elf_Internal_Shdr *symtab_hdr;
3759 struct elf_link_hash_entry **sym_hashes;
3760 bfd_vma *local_got_offsets;
3761 bfd_vma *local_tlsdesc_gotents;
3762 Elf_Internal_Rela *rel;
3763 Elf_Internal_Rela *wrel;
3764 Elf_Internal_Rela *relend;
3765 bfd_boolean is_vxworks_tls;
3766 unsigned plt_entry_size;
3768 BFD_ASSERT (is_i386_elf (input_bfd));
3770 /* Skip if check_relocs failed. */
3771 if (input_section->check_relocs_failed)
3774 htab = elf_i386_hash_table (info);
3777 symtab_hdr = &elf_symtab_hdr (input_bfd);
3778 sym_hashes = elf_sym_hashes (input_bfd);
3779 local_got_offsets = elf_local_got_offsets (input_bfd);
3780 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
3781 /* We have to handle relocations in vxworks .tls_vars sections
3782 specially, because the dynamic loader is 'weird'. */
3783 is_vxworks_tls = (get_elf_i386_backend_data (output_bfd)->is_vxworks
3784 && bfd_link_pic (info)
3785 && !strcmp (input_section->output_section->name,
3788 elf_i386_set_tls_module_base (info);
3790 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
3792 rel = wrel = relocs;
3793 relend = relocs + input_section->reloc_count;
3794 for (; rel < relend; wrel++, rel++)
3796 unsigned int r_type;
3797 reloc_howto_type *howto;
3798 unsigned long r_symndx;
3799 struct elf_link_hash_entry *h;
3800 struct elf_i386_link_hash_entry *eh;
3801 Elf_Internal_Sym *sym;
3803 bfd_vma off, offplt, plt_offset;
3805 bfd_boolean unresolved_reloc;
3806 bfd_reloc_status_type r;
3810 asection *resolved_plt;
3811 bfd_boolean resolved_to_zero;
3813 r_type = ELF32_R_TYPE (rel->r_info);
3814 if (r_type == R_386_GNU_VTINHERIT
3815 || r_type == R_386_GNU_VTENTRY)
3822 if ((indx = r_type) >= R_386_standard
3823 && ((indx = r_type - R_386_ext_offset) - R_386_standard
3824 >= R_386_ext - R_386_standard)
3825 && ((indx = r_type - R_386_tls_offset) - R_386_ext
3826 >= R_386_ext2 - R_386_ext))
3828 (*_bfd_error_handler)
3829 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3830 input_bfd, input_section, r_type);
3831 bfd_set_error (bfd_error_bad_value);
3834 howto = elf_howto_table + indx;
3836 r_symndx = ELF32_R_SYM (rel->r_info);
3840 unresolved_reloc = FALSE;
3841 if (r_symndx < symtab_hdr->sh_info)
3843 sym = local_syms + r_symndx;
3844 sec = local_sections[r_symndx];
3845 relocation = (sec->output_section->vma
3846 + sec->output_offset
3848 st_size = sym->st_size;
3850 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
3851 && ((sec->flags & SEC_MERGE) != 0
3852 || (bfd_link_relocatable (info)
3853 && sec->output_offset != 0)))
3856 bfd_byte *where = contents + rel->r_offset;
3858 switch (howto->size)
3861 addend = bfd_get_8 (input_bfd, where);
3862 if (howto->pc_relative)
3864 addend = (addend ^ 0x80) - 0x80;
3869 addend = bfd_get_16 (input_bfd, where);
3870 if (howto->pc_relative)
3872 addend = (addend ^ 0x8000) - 0x8000;
3877 addend = bfd_get_32 (input_bfd, where);
3878 if (howto->pc_relative)
3880 addend = (addend ^ 0x80000000) - 0x80000000;
3888 if (bfd_link_relocatable (info))
3889 addend += sec->output_offset;
3892 asection *msec = sec;
3893 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
3895 addend -= relocation;
3896 addend += msec->output_section->vma + msec->output_offset;
3899 switch (howto->size)
3902 /* FIXME: overflow checks. */
3903 if (howto->pc_relative)
3905 bfd_put_8 (input_bfd, addend, where);
3908 if (howto->pc_relative)
3910 bfd_put_16 (input_bfd, addend, where);
3913 if (howto->pc_relative)
3915 bfd_put_32 (input_bfd, addend, where);
3919 else if (!bfd_link_relocatable (info)
3920 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3922 /* Relocate against local STT_GNU_IFUNC symbol. */
3923 h = elf_i386_get_local_sym_hash (htab, input_bfd, rel,
3928 /* Set STT_GNU_IFUNC symbol value. */
3929 h->root.u.def.value = sym->st_value;
3930 h->root.u.def.section = sec;
3935 bfd_boolean warned ATTRIBUTE_UNUSED;
3936 bfd_boolean ignored ATTRIBUTE_UNUSED;
3938 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3939 r_symndx, symtab_hdr, sym_hashes,
3941 unresolved_reloc, warned, ignored);
3945 if (sec != NULL && discarded_section (sec))
3947 _bfd_clear_contents (howto, input_bfd, input_section,
3948 contents + rel->r_offset);
3949 wrel->r_offset = rel->r_offset;
3953 /* For ld -r, remove relocations in debug sections against
3954 sections defined in discarded sections. Not done for
3955 eh_frame editing code expects to be present. */
3956 if (bfd_link_relocatable (info)
3957 && (input_section->flags & SEC_DEBUGGING))
3963 if (bfd_link_relocatable (info))
3970 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3971 it here if it is defined in a non-shared object. */
3973 && h->type == STT_GNU_IFUNC
3976 asection *plt, *gotplt, *base_got;
3980 if ((input_section->flags & SEC_ALLOC) == 0)
3982 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3983 sections because such sections are not SEC_ALLOC and
3984 thus ld.so will not process them. */
3985 if ((input_section->flags & SEC_DEBUGGING) != 0)
3990 /* STT_GNU_IFUNC symbol must go through PLT. */
3991 if (htab->elf.splt != NULL)
3993 plt = htab->elf.splt;
3994 gotplt = htab->elf.sgotplt;
3998 plt = htab->elf.iplt;
3999 gotplt = htab->elf.igotplt;
4009 base_got = htab->elf.sgot;
4010 off = h->got.offset;
4012 if (base_got == NULL)
4015 if (off == (bfd_vma) -1)
4017 /* We can't use h->got.offset here to save state, or
4018 even just remember the offset, as finish_dynamic_symbol
4019 would use that as offset into .got. */
4021 if (h->plt.offset == (bfd_vma) -1)
4024 if (htab->elf.splt != NULL)
4026 plt_index = h->plt.offset / plt_entry_size - 1;
4027 off = (plt_index + 3) * 4;
4028 base_got = htab->elf.sgotplt;
4032 plt_index = h->plt.offset / plt_entry_size;
4033 off = plt_index * 4;
4034 base_got = htab->elf.igotplt;
4037 if (h->dynindx == -1
4041 /* This references the local defitionion. We must
4042 initialize this entry in the global offset table.
4043 Since the offset must always be a multiple of 8,
4044 we use the least significant bit to record
4045 whether we have initialized it already.
4047 When doing a dynamic link, we create a .rela.got
4048 relocation entry to initialize the value. This
4049 is done in the finish_dynamic_symbol routine. */
4054 bfd_put_32 (output_bfd, relocation,
4055 base_got->contents + off);
4063 relocation = (base_got->output_section->vma
4064 + base_got->output_offset + off
4065 - gotplt->output_section->vma
4066 - gotplt->output_offset);
4068 if ((*(contents + rel->r_offset - 1) & 0xc7) == 0x5)
4070 if (bfd_link_pic (info))
4071 goto disallow_got32;
4073 /* Add the GOT base if there is no base register. */
4074 relocation += (gotplt->output_section->vma
4075 + gotplt->output_offset);
4077 else if (htab->elf.splt == NULL)
4079 /* Adjust for static executables. */
4080 relocation += gotplt->output_offset;
4086 if (h->plt.offset == (bfd_vma) -1)
4088 /* Handle static pointers of STT_GNU_IFUNC symbols. */
4089 if (r_type == R_386_32
4090 && (input_section->flags & SEC_CODE) == 0)
4091 goto do_ifunc_pointer;
4092 goto bad_ifunc_reloc;
4095 relocation = (plt->output_section->vma
4096 + plt->output_offset + h->plt.offset);
4102 if (h->root.root.string)
4103 name = h->root.root.string;
4105 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4107 (*_bfd_error_handler)
4108 (_("%B: relocation %s against STT_GNU_IFUNC "
4109 "symbol `%s' isn't supported"), input_bfd,
4111 bfd_set_error (bfd_error_bad_value);
4115 /* Generate dynamic relcoation only when there is a
4116 non-GOT reference in a shared object. */
4117 if ((bfd_link_pic (info) && h->non_got_ref)
4118 || h->plt.offset == (bfd_vma) -1)
4120 Elf_Internal_Rela outrel;
4125 /* Need a dynamic relocation to get the real function
4127 offset = _bfd_elf_section_offset (output_bfd,
4131 if (offset == (bfd_vma) -1
4132 || offset == (bfd_vma) -2)
4135 outrel.r_offset = (input_section->output_section->vma
4136 + input_section->output_offset
4139 if (h->dynindx == -1
4141 || bfd_link_executable (info))
4143 /* This symbol is resolved locally. */
4144 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4145 bfd_put_32 (output_bfd,
4146 (h->root.u.def.value
4147 + h->root.u.def.section->output_section->vma
4148 + h->root.u.def.section->output_offset),
4152 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4154 /* Dynamic relocations are stored in
4155 1. .rel.ifunc section in PIC object.
4156 2. .rel.got section in dynamic executable.
4157 3. .rel.iplt section in static executable. */
4158 if (bfd_link_pic (info))
4159 sreloc = htab->elf.irelifunc;
4160 else if (htab->elf.splt != NULL)
4161 sreloc = htab->elf.srelgot;
4163 sreloc = htab->elf.irelplt;
4164 elf_append_rel (output_bfd, sreloc, &outrel);
4166 /* If this reloc is against an external symbol, we
4167 do not want to fiddle with the addend. Otherwise,
4168 we need to include the symbol value so that it
4169 becomes an addend for the dynamic reloc. For an
4170 internal symbol, we have updated addend. */
4179 relocation -= (gotplt->output_section->vma
4180 + gotplt->output_offset);
4185 eh = (struct elf_i386_link_hash_entry *) h;
4186 resolved_to_zero = (eh != NULL
4187 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
4194 /* Avoid optimizing _DYNAMIC since ld.so may use its
4195 link-time address. */
4196 if (h == htab->elf.hdynamic)
4199 if (bfd_link_pic (info))
4201 /* It is OK to convert mov to lea and convert indirect
4202 branch to direct branch. It is OK to convert adc,
4203 add, and, cmp, or, sbb, sub, test, xor only when PIC
4205 unsigned int opcode, addend;
4206 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4209 opcode = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4210 if (opcode != 0x8b && opcode != 0xff)
4214 /* Resolve "mov GOT[(%reg)], %reg",
4215 "call/jmp *GOT[(%reg)]", "test %reg, foo@GOT[(%reg)]"
4216 and "binop foo@GOT[(%reg)], %reg". */
4218 || (h->plt.offset == (bfd_vma) -1
4219 && h->got.offset == (bfd_vma) -1)
4220 || htab->elf.sgotplt == NULL)
4223 offplt = (htab->elf.sgotplt->output_section->vma
4224 + htab->elf.sgotplt->output_offset);
4226 /* It is relative to .got.plt section. */
4227 if (h->got.offset != (bfd_vma) -1)
4228 /* Use GOT entry. Mask off the least significant bit in
4229 GOT offset which may be set by R_386_GOT32 processing
4231 relocation = (htab->elf.sgot->output_section->vma
4232 + htab->elf.sgot->output_offset
4233 + (h->got.offset & ~1) - offplt);
4235 /* Use GOTPLT entry. */
4236 relocation = (h->plt.offset / plt_entry_size - 1 + 3) * 4;
4238 if (!bfd_link_pic (info))
4240 /* If not PIC, add the .got.plt section address for
4241 baseless addressing. */
4243 modrm = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4244 if ((modrm & 0xc7) == 0x5)
4245 relocation += offplt;
4248 unresolved_reloc = FALSE;
4253 /* Relocation is to the entry for this symbol in the global
4255 if (htab->elf.sgot == NULL)
4262 off = h->got.offset;
4263 dyn = htab->elf.dynamic_sections_created;
4264 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4265 bfd_link_pic (info),
4267 || (bfd_link_pic (info)
4268 && SYMBOL_REFERENCES_LOCAL (info, h))
4269 || (ELF_ST_VISIBILITY (h->other)
4270 && h->root.type == bfd_link_hash_undefweak))
4272 /* This is actually a static link, or it is a
4273 -Bsymbolic link and the symbol is defined
4274 locally, or the symbol was forced to be local
4275 because of a version file. We must initialize
4276 this entry in the global offset table. Since the
4277 offset must always be a multiple of 4, we use the
4278 least significant bit to record whether we have
4279 initialized it already.
4281 When doing a dynamic link, we create a .rel.got
4282 relocation entry to initialize the value. This
4283 is done in the finish_dynamic_symbol routine. */
4288 bfd_put_32 (output_bfd, relocation,
4289 htab->elf.sgot->contents + off);
4294 unresolved_reloc = FALSE;
4298 if (local_got_offsets == NULL)
4301 off = local_got_offsets[r_symndx];
4303 /* The offset must always be a multiple of 4. We use
4304 the least significant bit to record whether we have
4305 already generated the necessary reloc. */
4310 bfd_put_32 (output_bfd, relocation,
4311 htab->elf.sgot->contents + off);
4313 if (bfd_link_pic (info))
4316 Elf_Internal_Rela outrel;
4318 s = htab->elf.srelgot;
4322 outrel.r_offset = (htab->elf.sgot->output_section->vma
4323 + htab->elf.sgot->output_offset
4325 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4326 elf_append_rel (output_bfd, s, &outrel);
4329 local_got_offsets[r_symndx] |= 1;
4333 if (off >= (bfd_vma) -2)
4336 relocation = (htab->elf.sgot->output_section->vma
4337 + htab->elf.sgot->output_offset + off);
4338 if ((*(contents + rel->r_offset - 1) & 0xc7) == 0x5)
4340 if (bfd_link_pic (info))
4342 /* For PIC, disallow R_386_GOT32 without a base
4343 register since we don't know what the GOT base
4349 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4352 name = h->root.root.string;
4354 (*_bfd_error_handler)
4355 (_("%B: direct GOT relocation %s against `%s' without base register can not be used when making a shared object"),
4356 input_bfd, howto->name, name);
4357 bfd_set_error (bfd_error_bad_value);
4363 /* Subtract the .got.plt section address only with a base
4365 relocation -= (htab->elf.sgotplt->output_section->vma
4366 + htab->elf.sgotplt->output_offset);
4372 /* Relocation is relative to the start of the global offset
4375 /* Check to make sure it isn't a protected function or data
4376 symbol for shared library since it may not be local when
4377 used as function address or with copy relocation. We also
4378 need to make sure that a symbol is referenced locally. */
4379 if (!bfd_link_executable (info) && h)
4381 if (!h->def_regular)
4385 switch (ELF_ST_VISIBILITY (h->other))
4388 v = _("hidden symbol");
4391 v = _("internal symbol");
4394 v = _("protected symbol");
4401 (*_bfd_error_handler)
4402 (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
4403 input_bfd, v, h->root.root.string);
4404 bfd_set_error (bfd_error_bad_value);
4407 else if (!SYMBOL_REFERENCES_LOCAL (info, h)
4408 && (h->type == STT_FUNC
4409 || h->type == STT_OBJECT)
4410 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
4412 (*_bfd_error_handler)
4413 (_("%B: relocation R_386_GOTOFF against protected %s `%s' can not be used when making a shared object"),
4415 h->type == STT_FUNC ? "function" : "data",
4416 h->root.root.string);
4417 bfd_set_error (bfd_error_bad_value);
4422 /* Note that sgot is not involved in this
4423 calculation. We always want the start of .got.plt. If we
4424 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
4425 permitted by the ABI, we might have to change this
4427 relocation -= htab->elf.sgotplt->output_section->vma
4428 + htab->elf.sgotplt->output_offset;
4432 /* Use global offset table as symbol value. */
4433 relocation = htab->elf.sgotplt->output_section->vma
4434 + htab->elf.sgotplt->output_offset;
4435 unresolved_reloc = FALSE;
4439 /* Relocation is to the entry for this symbol in the
4440 procedure linkage table. */
4442 /* Resolve a PLT32 reloc against a local symbol directly,
4443 without using the procedure linkage table. */
4447 if ((h->plt.offset == (bfd_vma) -1
4448 && eh->plt_got.offset == (bfd_vma) -1)
4449 || htab->elf.splt == NULL)
4451 /* We didn't make a PLT entry for this symbol. This
4452 happens when statically linking PIC code, or when
4453 using -Bsymbolic. */
4457 if (h->plt.offset != (bfd_vma) -1)
4459 resolved_plt = htab->elf.splt;
4460 plt_offset = h->plt.offset;
4464 resolved_plt = htab->plt_got;
4465 plt_offset = eh->plt_got.offset;
4468 relocation = (resolved_plt->output_section->vma
4469 + resolved_plt->output_offset
4471 unresolved_reloc = FALSE;
4475 /* Set to symbol size. */
4476 relocation = st_size;
4481 if ((input_section->flags & SEC_ALLOC) == 0
4485 /* Copy dynamic function pointer relocations. Don't generate
4486 dynamic relocations against resolved undefined weak symbols
4487 in PIE, except for R_386_PC32. */
4488 if ((bfd_link_pic (info)
4490 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4491 && (!resolved_to_zero
4492 || r_type == R_386_PC32))
4493 || h->root.type != bfd_link_hash_undefweak))
4494 && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
4495 || !SYMBOL_CALLS_LOCAL (info, h)))
4496 || (ELIMINATE_COPY_RELOCS
4497 && !bfd_link_pic (info)
4501 || eh->func_pointer_refcount > 0
4502 || (h->root.type == bfd_link_hash_undefweak
4503 && !resolved_to_zero))
4504 && ((h->def_dynamic && !h->def_regular)
4505 /* Undefined weak symbol is bound locally when
4507 || h->root.type == bfd_link_hash_undefweak)))
4509 Elf_Internal_Rela outrel;
4510 bfd_boolean skip, relocate;
4513 /* When generating a shared object, these relocations
4514 are copied into the output file to be resolved at run
4521 _bfd_elf_section_offset (output_bfd, info, input_section,
4523 if (outrel.r_offset == (bfd_vma) -1)
4525 else if (outrel.r_offset == (bfd_vma) -2)
4526 skip = TRUE, relocate = TRUE;
4527 outrel.r_offset += (input_section->output_section->vma
4528 + input_section->output_offset);
4531 memset (&outrel, 0, sizeof outrel);
4534 && (r_type == R_386_PC32
4535 || !(bfd_link_executable (info)
4536 || SYMBOLIC_BIND (info, h))
4537 || !h->def_regular))
4538 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4541 /* This symbol is local, or marked to become local. */
4543 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4546 sreloc = elf_section_data (input_section)->sreloc;
4548 if (sreloc == NULL || sreloc->contents == NULL)
4550 r = bfd_reloc_notsupported;
4551 goto check_relocation_error;
4554 elf_append_rel (output_bfd, sreloc, &outrel);
4556 /* If this reloc is against an external symbol, we do
4557 not want to fiddle with the addend. Otherwise, we
4558 need to include the symbol value so that it becomes
4559 an addend for the dynamic reloc. */
4566 if (!bfd_link_executable (info))
4568 Elf_Internal_Rela outrel;
4571 outrel.r_offset = rel->r_offset
4572 + input_section->output_section->vma
4573 + input_section->output_offset;
4574 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4575 sreloc = elf_section_data (input_section)->sreloc;
4578 elf_append_rel (output_bfd, sreloc, &outrel);
4583 case R_386_TLS_GOTDESC:
4584 case R_386_TLS_DESC_CALL:
4585 case R_386_TLS_IE_32:
4586 case R_386_TLS_GOTIE:
4587 tls_type = GOT_UNKNOWN;
4588 if (h == NULL && local_got_offsets)
4589 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
4591 tls_type = elf_i386_hash_entry(h)->tls_type;
4592 if (tls_type == GOT_TLS_IE)
4593 tls_type = GOT_TLS_IE_NEG;
4595 if (! elf_i386_tls_transition (info, input_bfd,
4596 input_section, contents,
4597 symtab_hdr, sym_hashes,
4598 &r_type, tls_type, rel,
4599 relend, h, r_symndx, TRUE))
4602 if (r_type == R_386_TLS_LE_32)
4604 BFD_ASSERT (! unresolved_reloc);
4605 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
4610 /* GD->LE transition. */
4611 type = *(contents + rel->r_offset - 2);
4615 leal foo@tlsgd(,%ebx,1), %eax
4616 call ___tls_get_addr@PLT
4619 subl $foo@tpoff, %eax
4620 (6 byte form of subl). */
4621 roff = rel->r_offset + 5;
4626 leal foo@tlsgd(%ebx), %eax
4627 call ___tls_get_addr@PLT
4630 leal foo@tlsgd(%reg), %eax
4631 call *___tls_get_addr@GOT(%reg)
4632 which may be converted to
4633 addr32 call ___tls_get_addr
4635 movl %gs:0, %eax; subl $foo@tpoff, %eax
4636 (6 byte form of subl). */
4637 roff = rel->r_offset + 6;
4639 memcpy (contents + roff - 8,
4640 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
4641 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
4643 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
4648 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
4650 /* GDesc -> LE transition.
4651 It's originally something like:
4652 leal x@tlsdesc(%ebx), %eax
4656 Registers other than %eax may be set up here. */
4661 roff = rel->r_offset;
4662 val = bfd_get_8 (input_bfd, contents + roff - 1);
4664 /* Now modify the instruction as appropriate. */
4665 /* aoliva FIXME: remove the above and xor the byte
4667 bfd_put_8 (output_bfd, val ^ 0x86,
4668 contents + roff - 1);
4669 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4673 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
4675 /* GDesc -> LE transition.
4683 roff = rel->r_offset;
4684 bfd_put_8 (output_bfd, 0x66, contents + roff);
4685 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4688 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
4692 /* IE->LE transition:
4693 Originally it can be one of:
4701 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4704 /* movl foo, %eax. */
4705 bfd_put_8 (output_bfd, 0xb8,
4706 contents + rel->r_offset - 1);
4712 type = bfd_get_8 (input_bfd,
4713 contents + rel->r_offset - 2);
4718 bfd_put_8 (output_bfd, 0xc7,
4719 contents + rel->r_offset - 2);
4720 bfd_put_8 (output_bfd,
4721 0xc0 | ((val >> 3) & 7),
4722 contents + rel->r_offset - 1);
4726 bfd_put_8 (output_bfd, 0x81,
4727 contents + rel->r_offset - 2);
4728 bfd_put_8 (output_bfd,
4729 0xc0 | ((val >> 3) & 7),
4730 contents + rel->r_offset - 1);
4737 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4738 contents + rel->r_offset);
4743 unsigned int val, type;
4745 /* {IE_32,GOTIE}->LE transition:
4746 Originally it can be one of:
4747 subl foo(%reg1), %reg2
4748 movl foo(%reg1), %reg2
4749 addl foo(%reg1), %reg2
4752 movl $foo, %reg2 (6 byte form)
4753 addl $foo, %reg2. */
4754 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4755 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4759 bfd_put_8 (output_bfd, 0xc7,
4760 contents + rel->r_offset - 2);
4761 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4762 contents + rel->r_offset - 1);
4764 else if (type == 0x2b)
4767 bfd_put_8 (output_bfd, 0x81,
4768 contents + rel->r_offset - 2);
4769 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
4770 contents + rel->r_offset - 1);
4772 else if (type == 0x03)
4775 bfd_put_8 (output_bfd, 0x81,
4776 contents + rel->r_offset - 2);
4777 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4778 contents + rel->r_offset - 1);
4782 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
4783 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4784 contents + rel->r_offset);
4786 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
4787 contents + rel->r_offset);
4792 if (htab->elf.sgot == NULL)
4797 off = h->got.offset;
4798 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
4802 if (local_got_offsets == NULL)
4805 off = local_got_offsets[r_symndx];
4806 offplt = local_tlsdesc_gotents[r_symndx];
4813 Elf_Internal_Rela outrel;
4817 if (htab->elf.srelgot == NULL)
4820 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4822 if (GOT_TLS_GDESC_P (tls_type))
4825 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
4826 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
4827 <= htab->elf.sgotplt->size);
4828 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4829 + htab->elf.sgotplt->output_offset
4831 + htab->sgotplt_jump_table_size);
4832 sreloc = htab->elf.srelplt;
4833 loc = sreloc->contents;
4834 loc += (htab->next_tls_desc_index++
4835 * sizeof (Elf32_External_Rel));
4836 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
4837 <= sreloc->contents + sreloc->size);
4838 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4841 BFD_ASSERT (! unresolved_reloc);
4842 bfd_put_32 (output_bfd,
4843 relocation - elf_i386_dtpoff_base (info),
4844 htab->elf.sgotplt->contents + offplt
4845 + htab->sgotplt_jump_table_size + 4);
4849 bfd_put_32 (output_bfd, 0,
4850 htab->elf.sgotplt->contents + offplt
4851 + htab->sgotplt_jump_table_size + 4);
4855 sreloc = htab->elf.srelgot;
4857 outrel.r_offset = (htab->elf.sgot->output_section->vma
4858 + htab->elf.sgot->output_offset + off);
4860 if (GOT_TLS_GD_P (tls_type))
4861 dr_type = R_386_TLS_DTPMOD32;
4862 else if (GOT_TLS_GDESC_P (tls_type))
4864 else if (tls_type == GOT_TLS_IE_POS)
4865 dr_type = R_386_TLS_TPOFF;
4867 dr_type = R_386_TLS_TPOFF32;
4869 if (dr_type == R_386_TLS_TPOFF && indx == 0)
4870 bfd_put_32 (output_bfd,
4871 relocation - elf_i386_dtpoff_base (info),
4872 htab->elf.sgot->contents + off);
4873 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
4874 bfd_put_32 (output_bfd,
4875 elf_i386_dtpoff_base (info) - relocation,
4876 htab->elf.sgot->contents + off);
4877 else if (dr_type != R_386_TLS_DESC)
4878 bfd_put_32 (output_bfd, 0,
4879 htab->elf.sgot->contents + off);
4880 outrel.r_info = ELF32_R_INFO (indx, dr_type);
4882 elf_append_rel (output_bfd, sreloc, &outrel);
4884 if (GOT_TLS_GD_P (tls_type))
4888 BFD_ASSERT (! unresolved_reloc);
4889 bfd_put_32 (output_bfd,
4890 relocation - elf_i386_dtpoff_base (info),
4891 htab->elf.sgot->contents + off + 4);
4895 bfd_put_32 (output_bfd, 0,
4896 htab->elf.sgot->contents + off + 4);
4897 outrel.r_info = ELF32_R_INFO (indx,
4898 R_386_TLS_DTPOFF32);
4899 outrel.r_offset += 4;
4900 elf_append_rel (output_bfd, sreloc, &outrel);
4903 else if (tls_type == GOT_TLS_IE_BOTH)
4905 bfd_put_32 (output_bfd,
4907 ? relocation - elf_i386_dtpoff_base (info)
4909 htab->elf.sgot->contents + off + 4);
4910 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
4911 outrel.r_offset += 4;
4912 elf_append_rel (output_bfd, sreloc, &outrel);
4919 local_got_offsets[r_symndx] |= 1;
4922 if (off >= (bfd_vma) -2
4923 && ! GOT_TLS_GDESC_P (tls_type))
4925 if (r_type == R_386_TLS_GOTDESC
4926 || r_type == R_386_TLS_DESC_CALL)
4928 relocation = htab->sgotplt_jump_table_size + offplt;
4929 unresolved_reloc = FALSE;
4931 else if (r_type == ELF32_R_TYPE (rel->r_info))
4933 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
4934 + htab->elf.sgotplt->output_offset;
4935 relocation = htab->elf.sgot->output_section->vma
4936 + htab->elf.sgot->output_offset + off - g_o_t;
4937 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
4938 && tls_type == GOT_TLS_IE_BOTH)
4940 if (r_type == R_386_TLS_IE)
4941 relocation += g_o_t;
4942 unresolved_reloc = FALSE;
4944 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
4946 unsigned int val, type;
4949 /* GD->IE transition. */
4950 type = *(contents + rel->r_offset - 2);
4951 val = *(contents + rel->r_offset - 1);
4955 leal foo@tlsgd(,%ebx,1), %eax
4956 call ___tls_get_addr@PLT
4959 subl $foo@gottpoff(%ebx), %eax. */
4961 roff = rel->r_offset - 3;
4966 leal foo@tlsgd(%ebx), %eax
4967 call ___tls_get_addr@PLT
4970 leal foo@tlsgd(%reg), %eax
4971 call *___tls_get_addr@GOT(%reg)
4972 which may be converted to
4973 addr32 call ___tls_get_addr
4976 subl $foo@gottpoff(%reg), %eax. */
4977 roff = rel->r_offset - 2;
4979 memcpy (contents + roff,
4980 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
4981 contents[roff + 7] = 0x80 | (val & 7);
4982 /* If foo is used only with foo@gotntpoff(%reg) and
4983 foo@indntpoff, but not with foo@gottpoff(%reg), change
4984 subl $foo@gottpoff(%reg), %eax
4986 addl $foo@gotntpoff(%reg), %eax. */
4987 if (tls_type == GOT_TLS_IE_POS)
4988 contents[roff + 6] = 0x03;
4989 bfd_put_32 (output_bfd,
4990 htab->elf.sgot->output_section->vma
4991 + htab->elf.sgot->output_offset + off
4992 - htab->elf.sgotplt->output_section->vma
4993 - htab->elf.sgotplt->output_offset,
4994 contents + roff + 8);
4995 /* Skip R_386_PLT32 and R_386_GOT32X. */
5000 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
5002 /* GDesc -> IE transition.
5003 It's originally something like:
5004 leal x@tlsdesc(%ebx), %eax
5007 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
5009 movl x@gottpoff(%ebx), %eax # before negl %eax
5011 Registers other than %eax may be set up here. */
5015 /* First, make sure it's a leal adding ebx to a 32-bit
5016 offset into any register, although it's probably
5017 almost always going to be eax. */
5018 roff = rel->r_offset;
5020 /* Now modify the instruction as appropriate. */
5021 /* To turn a leal into a movl in the form we use it, it
5022 suffices to change the first byte from 0x8d to 0x8b.
5023 aoliva FIXME: should we decide to keep the leal, all
5024 we have to do is remove the statement below, and
5025 adjust the relaxation of R_386_TLS_DESC_CALL. */
5026 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
5028 if (tls_type == GOT_TLS_IE_BOTH)
5031 bfd_put_32 (output_bfd,
5032 htab->elf.sgot->output_section->vma
5033 + htab->elf.sgot->output_offset + off
5034 - htab->elf.sgotplt->output_section->vma
5035 - htab->elf.sgotplt->output_offset,
5039 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
5041 /* GDesc -> IE transition.
5049 depending on how we transformed the TLS_GOTDESC above.
5054 roff = rel->r_offset;
5056 /* Now modify the instruction as appropriate. */
5057 if (tls_type != GOT_TLS_IE_NEG)
5060 bfd_put_8 (output_bfd, 0x66, contents + roff);
5061 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5066 bfd_put_8 (output_bfd, 0xf7, contents + roff);
5067 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
5077 if (! elf_i386_tls_transition (info, input_bfd,
5078 input_section, contents,
5079 symtab_hdr, sym_hashes,
5080 &r_type, GOT_UNKNOWN, rel,
5081 relend, h, r_symndx, TRUE))
5084 if (r_type != R_386_TLS_LDM)
5086 /* LD->LE transition. Change
5087 leal foo@tlsldm(%ebx) %eax
5088 call ___tls_get_addr@PLT
5092 leal 0(%esi,1), %esi
5094 leal foo@tlsldm(%reg) %eax
5095 call *___tls_get_addr@GOT(%reg)
5096 which may be converted to
5097 addr32 call ___tls_get_addr
5100 leal 0(%esi), %esi */
5101 BFD_ASSERT (r_type == R_386_TLS_LE_32);
5102 if (*(contents + rel->r_offset + 4) == 0xff
5103 || *(contents + rel->r_offset + 4) == 0x67)
5104 memcpy (contents + rel->r_offset - 2,
5105 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
5107 memcpy (contents + rel->r_offset - 2,
5108 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
5109 /* Skip R_386_PC32/R_386_PLT32. */
5115 if (htab->elf.sgot == NULL)
5118 off = htab->tls_ldm_got.offset;
5123 Elf_Internal_Rela outrel;
5125 if (htab->elf.srelgot == NULL)
5128 outrel.r_offset = (htab->elf.sgot->output_section->vma
5129 + htab->elf.sgot->output_offset + off);
5131 bfd_put_32 (output_bfd, 0,
5132 htab->elf.sgot->contents + off);
5133 bfd_put_32 (output_bfd, 0,
5134 htab->elf.sgot->contents + off + 4);
5135 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
5136 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
5137 htab->tls_ldm_got.offset |= 1;
5139 relocation = htab->elf.sgot->output_section->vma
5140 + htab->elf.sgot->output_offset + off
5141 - htab->elf.sgotplt->output_section->vma
5142 - htab->elf.sgotplt->output_offset;
5143 unresolved_reloc = FALSE;
5146 case R_386_TLS_LDO_32:
5147 if (!bfd_link_executable (info)
5148 || (input_section->flags & SEC_CODE) == 0)
5149 relocation -= elf_i386_dtpoff_base (info);
5151 /* When converting LDO to LE, we must negate. */
5152 relocation = -elf_i386_tpoff (info, relocation);
5155 case R_386_TLS_LE_32:
5157 if (!bfd_link_executable (info))
5159 Elf_Internal_Rela outrel;
5162 outrel.r_offset = rel->r_offset
5163 + input_section->output_section->vma
5164 + input_section->output_offset;
5165 if (h != NULL && h->dynindx != -1)
5169 if (r_type == R_386_TLS_LE_32)
5170 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
5172 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
5173 sreloc = elf_section_data (input_section)->sreloc;
5176 elf_append_rel (output_bfd, sreloc, &outrel);
5179 else if (r_type == R_386_TLS_LE_32)
5180 relocation = elf_i386_dtpoff_base (info) - relocation;
5182 relocation -= elf_i386_dtpoff_base (info);
5184 else if (r_type == R_386_TLS_LE_32)
5185 relocation = elf_i386_tpoff (info, relocation);
5187 relocation = -elf_i386_tpoff (info, relocation);
5194 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5195 because such sections are not SEC_ALLOC and thus ld.so will
5196 not process them. */
5197 if (unresolved_reloc
5198 && !((input_section->flags & SEC_DEBUGGING) != 0
5200 && _bfd_elf_section_offset (output_bfd, info, input_section,
5201 rel->r_offset) != (bfd_vma) -1)
5203 (*_bfd_error_handler)
5204 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5207 (long) rel->r_offset,
5209 h->root.root.string);
5214 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5215 contents, rel->r_offset,
5218 check_relocation_error:
5219 if (r != bfd_reloc_ok)
5224 name = h->root.root.string;
5227 name = bfd_elf_string_from_elf_section (input_bfd,
5228 symtab_hdr->sh_link,
5233 name = bfd_section_name (input_bfd, sec);
5236 if (r == bfd_reloc_overflow)
5237 (*info->callbacks->reloc_overflow)
5238 (info, (h ? &h->root : NULL), name, howto->name,
5239 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5242 (*_bfd_error_handler)
5243 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
5244 input_bfd, input_section,
5245 (long) rel->r_offset, name, (int) r);
5256 Elf_Internal_Shdr *rel_hdr;
5257 size_t deleted = rel - wrel;
5259 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5260 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5261 if (rel_hdr->sh_size == 0)
5263 /* It is too late to remove an empty reloc section. Leave
5265 ??? What is wrong with an empty section??? */
5266 rel_hdr->sh_size = rel_hdr->sh_entsize;
5269 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5270 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5271 input_section->reloc_count -= deleted;
5277 /* Finish up dynamic symbol handling. We set the contents of various
5278 dynamic sections here. */
5281 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
5282 struct bfd_link_info *info,
5283 struct elf_link_hash_entry *h,
5284 Elf_Internal_Sym *sym)
5286 struct elf_i386_link_hash_table *htab;
5287 unsigned plt_entry_size;
5288 const struct elf_i386_backend_data *abed;
5289 struct elf_i386_link_hash_entry *eh;
5290 bfd_boolean local_undefweak;
5292 htab = elf_i386_hash_table (info);
5296 abed = get_elf_i386_backend_data (output_bfd);
5297 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
5299 eh = (struct elf_i386_link_hash_entry *) h;
5301 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5302 resolved undefined weak symbols in executable so that their
5303 references have value 0 at run-time. */
5304 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
5308 if (h->plt.offset != (bfd_vma) -1)
5312 Elf_Internal_Rela rel;
5314 asection *plt, *gotplt, *relplt;
5316 /* When building a static executable, use .iplt, .igot.plt and
5317 .rel.iplt sections for STT_GNU_IFUNC symbols. */
5318 if (htab->elf.splt != NULL)
5320 plt = htab->elf.splt;
5321 gotplt = htab->elf.sgotplt;
5322 relplt = htab->elf.srelplt;
5326 plt = htab->elf.iplt;
5327 gotplt = htab->elf.igotplt;
5328 relplt = htab->elf.irelplt;
5331 /* This symbol has an entry in the procedure linkage table. Set
5334 if ((h->dynindx == -1
5336 && !((h->forced_local || bfd_link_executable (info))
5338 && h->type == STT_GNU_IFUNC))
5344 /* Get the index in the procedure linkage table which
5345 corresponds to this symbol. This is the index of this symbol
5346 in all the symbols for which we are making plt entries. The
5347 first entry in the procedure linkage table is reserved.
5349 Get the offset into the .got table of the entry that
5350 corresponds to this function. Each .got entry is 4 bytes.
5351 The first three are reserved.
5353 For static executables, we don't reserve anything. */
5355 if (plt == htab->elf.splt)
5357 got_offset = h->plt.offset / plt_entry_size - 1;
5358 got_offset = (got_offset + 3) * 4;
5362 got_offset = h->plt.offset / plt_entry_size;
5363 got_offset = got_offset * 4;
5366 /* Fill in the entry in the procedure linkage table. */
5367 if (! bfd_link_pic (info))
5369 memcpy (plt->contents + h->plt.offset, abed->plt->plt_entry,
5370 abed->plt->plt_entry_size);
5371 bfd_put_32 (output_bfd,
5372 (gotplt->output_section->vma
5373 + gotplt->output_offset
5375 plt->contents + h->plt.offset
5376 + abed->plt->plt_got_offset);
5378 if (abed->is_vxworks)
5380 int s, k, reloc_index;
5382 /* Create the R_386_32 relocation referencing the GOT
5383 for this PLT entry. */
5385 /* S: Current slot number (zero-based). */
5386 s = ((h->plt.offset - abed->plt->plt_entry_size)
5387 / abed->plt->plt_entry_size);
5388 /* K: Number of relocations for PLTResolve. */
5389 if (bfd_link_pic (info))
5390 k = PLTRESOLVE_RELOCS_SHLIB;
5392 k = PLTRESOLVE_RELOCS;
5393 /* Skip the PLTresolve relocations, and the relocations for
5394 the other PLT slots. */
5395 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
5396 loc = (htab->srelplt2->contents + reloc_index
5397 * sizeof (Elf32_External_Rel));
5399 rel.r_offset = (htab->elf.splt->output_section->vma
5400 + htab->elf.splt->output_offset
5401 + h->plt.offset + 2),
5402 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5403 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5405 /* Create the R_386_32 relocation referencing the beginning of
5406 the PLT for this GOT entry. */
5407 rel.r_offset = (htab->elf.sgotplt->output_section->vma
5408 + htab->elf.sgotplt->output_offset
5410 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
5411 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5412 loc + sizeof (Elf32_External_Rel));
5417 memcpy (plt->contents + h->plt.offset, abed->plt->pic_plt_entry,
5418 abed->plt->plt_entry_size);
5419 bfd_put_32 (output_bfd, got_offset,
5420 plt->contents + h->plt.offset
5421 + abed->plt->plt_got_offset);
5424 /* Fill in the entry in the global offset table. Leave the entry
5425 as zero for undefined weak symbol in PIE. No PLT relocation
5426 against undefined weak symbol in PIE. */
5427 if (!local_undefweak)
5429 bfd_put_32 (output_bfd,
5430 (plt->output_section->vma
5431 + plt->output_offset
5433 + abed->plt->plt_lazy_offset),
5434 gotplt->contents + got_offset);
5436 /* Fill in the entry in the .rel.plt section. */
5437 rel.r_offset = (gotplt->output_section->vma
5438 + gotplt->output_offset
5440 if (h->dynindx == -1
5441 || ((bfd_link_executable (info)
5442 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5444 && h->type == STT_GNU_IFUNC))
5446 /* If an STT_GNU_IFUNC symbol is locally defined, generate
5447 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
5448 in the .got.plt section. */
5449 bfd_put_32 (output_bfd,
5450 (h->root.u.def.value
5451 + h->root.u.def.section->output_section->vma
5452 + h->root.u.def.section->output_offset),
5453 gotplt->contents + got_offset);
5454 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
5455 /* R_386_IRELATIVE comes last. */
5456 plt_index = htab->next_irelative_index--;
5460 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
5461 plt_index = htab->next_jump_slot_index++;
5464 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
5465 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5467 /* Don't fill PLT entry for static executables. */
5468 if (plt == htab->elf.splt)
5470 bfd_put_32 (output_bfd,
5471 plt_index * sizeof (Elf32_External_Rel),
5472 plt->contents + h->plt.offset
5473 + abed->plt->plt_reloc_offset);
5474 bfd_put_32 (output_bfd, - (h->plt.offset
5475 + abed->plt->plt_plt_offset + 4),
5476 plt->contents + h->plt.offset
5477 + abed->plt->plt_plt_offset);
5481 else if (eh->plt_got.offset != (bfd_vma) -1)
5483 bfd_vma got_offset, plt_offset;
5484 asection *plt, *got, *gotplt;
5485 const bfd_byte *got_plt_entry;
5487 /* Offset of displacement of the indirect jump. */
5488 bfd_vma plt_got_offset = 2;
5490 /* Set the entry in the GOT procedure linkage table. */
5491 plt = htab->plt_got;
5492 got = htab->elf.sgot;
5493 gotplt = htab->elf.sgotplt;
5494 got_offset = h->got.offset;
5496 if (got_offset == (bfd_vma) -1
5502 /* Fill in the entry in the GOT procedure linkage table. */
5503 if (! bfd_link_pic (info))
5505 got_plt_entry = elf_i386_got_plt_entry;
5506 got_offset += got->output_section->vma + got->output_offset;
5510 got_plt_entry = elf_i386_pic_got_plt_entry;
5511 got_offset += (got->output_section->vma
5512 + got->output_offset
5513 - gotplt->output_section->vma
5514 - gotplt->output_offset);
5517 plt_offset = eh->plt_got.offset;
5518 memcpy (plt->contents + plt_offset, got_plt_entry,
5519 sizeof (elf_i386_got_plt_entry));
5520 bfd_put_32 (output_bfd, got_offset,
5521 plt->contents + plt_offset + plt_got_offset);
5524 if (!local_undefweak
5526 && (h->plt.offset != (bfd_vma) -1
5527 || eh->plt_got.offset != (bfd_vma) -1))
5529 /* Mark the symbol as undefined, rather than as defined in
5530 the .plt section. Leave the value if there were any
5531 relocations where pointer equality matters (this is a clue
5532 for the dynamic linker, to make function pointer
5533 comparisons work between an application and shared
5534 library), otherwise set it to zero. If a function is only
5535 called from a binary, there is no need to slow down
5536 shared libraries because of that. */
5537 sym->st_shndx = SHN_UNDEF;
5538 if (!h->pointer_equality_needed)
5542 /* Don't generate dynamic GOT relocation against undefined weak
5543 symbol in executable. */
5544 if (h->got.offset != (bfd_vma) -1
5545 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
5546 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
5547 && !local_undefweak)
5549 Elf_Internal_Rela rel;
5550 asection *relgot = htab->elf.srelgot;
5552 /* This symbol has an entry in the global offset table. Set it
5555 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
5558 rel.r_offset = (htab->elf.sgot->output_section->vma
5559 + htab->elf.sgot->output_offset
5560 + (h->got.offset & ~(bfd_vma) 1));
5562 /* If this is a static link, or it is a -Bsymbolic link and the
5563 symbol is defined locally or was forced to be local because
5564 of a version file, we just want to emit a RELATIVE reloc.
5565 The entry in the global offset table will already have been
5566 initialized in the relocate_section function. */
5568 && h->type == STT_GNU_IFUNC)
5570 if (h->plt.offset == (bfd_vma) -1)
5572 /* STT_GNU_IFUNC is referenced without PLT. */
5573 if (htab->elf.splt == NULL)
5575 /* use .rel[a].iplt section to store .got relocations
5576 in static executable. */
5577 relgot = htab->elf.irelplt;
5579 if (SYMBOL_REFERENCES_LOCAL (info, h))
5581 bfd_put_32 (output_bfd,
5582 (h->root.u.def.value
5583 + h->root.u.def.section->output_section->vma
5584 + h->root.u.def.section->output_offset),
5585 htab->elf.sgot->contents + h->got.offset);
5586 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
5591 else if (bfd_link_pic (info))
5593 /* Generate R_386_GLOB_DAT. */
5600 if (!h->pointer_equality_needed)
5603 /* For non-shared object, we can't use .got.plt, which
5604 contains the real function addres if we need pointer
5605 equality. We load the GOT entry with the PLT entry. */
5606 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
5607 bfd_put_32 (output_bfd,
5608 (plt->output_section->vma
5609 + plt->output_offset + h->plt.offset),
5610 htab->elf.sgot->contents + h->got.offset);
5614 else if (bfd_link_pic (info)
5615 && SYMBOL_REFERENCES_LOCAL (info, h))
5617 BFD_ASSERT((h->got.offset & 1) != 0);
5618 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
5622 BFD_ASSERT((h->got.offset & 1) == 0);
5624 bfd_put_32 (output_bfd, (bfd_vma) 0,
5625 htab->elf.sgot->contents + h->got.offset);
5626 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
5629 elf_append_rel (output_bfd, relgot, &rel);
5634 Elf_Internal_Rela rel;
5636 /* This symbol needs a copy reloc. Set it up. */
5638 if (h->dynindx == -1
5639 || (h->root.type != bfd_link_hash_defined
5640 && h->root.type != bfd_link_hash_defweak)
5641 || htab->srelbss == NULL)
5644 rel.r_offset = (h->root.u.def.value
5645 + h->root.u.def.section->output_section->vma
5646 + h->root.u.def.section->output_offset);
5647 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
5648 elf_append_rel (output_bfd, htab->srelbss, &rel);
5654 /* Finish up local dynamic symbol handling. We set the contents of
5655 various dynamic sections here. */
5658 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
5660 struct elf_link_hash_entry *h
5661 = (struct elf_link_hash_entry *) *slot;
5662 struct bfd_link_info *info
5663 = (struct bfd_link_info *) inf;
5665 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
5669 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
5670 here since undefined weak symbol may not be dynamic and may not be
5671 called for elf_i386_finish_dynamic_symbol. */
5674 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
5677 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
5678 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5680 if (h->root.type != bfd_link_hash_undefweak
5681 || h->dynindx != -1)
5684 return elf_i386_finish_dynamic_symbol (info->output_bfd,
5688 /* Used to decide how to sort relocs in an optimal manner for the
5689 dynamic linker, before writing them out. */
5691 static enum elf_reloc_type_class
5692 elf_i386_reloc_type_class (const struct bfd_link_info *info,
5693 const asection *rel_sec ATTRIBUTE_UNUSED,
5694 const Elf_Internal_Rela *rela)
5696 bfd *abfd = info->output_bfd;
5697 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5698 struct elf_link_hash_table *htab = elf_hash_table (info);
5700 if (htab->dynsym != NULL
5701 && htab->dynsym->contents != NULL)
5703 /* Check relocation against STT_GNU_IFUNC symbol if there are
5705 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
5706 if (r_symndx != STN_UNDEF)
5708 Elf_Internal_Sym sym;
5709 if (!bed->s->swap_symbol_in (abfd,
5710 (htab->dynsym->contents
5711 + r_symndx * sizeof (Elf32_External_Sym)),
5715 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
5716 return reloc_class_ifunc;
5720 switch (ELF32_R_TYPE (rela->r_info))
5722 case R_386_IRELATIVE:
5723 return reloc_class_ifunc;
5724 case R_386_RELATIVE:
5725 return reloc_class_relative;
5726 case R_386_JUMP_SLOT:
5727 return reloc_class_plt;
5729 return reloc_class_copy;
5731 return reloc_class_normal;
5735 /* Finish up the dynamic sections. */
5738 elf_i386_finish_dynamic_sections (bfd *output_bfd,
5739 struct bfd_link_info *info)
5741 struct elf_i386_link_hash_table *htab;
5744 const struct elf_i386_backend_data *abed;
5746 htab = elf_i386_hash_table (info);
5750 dynobj = htab->elf.dynobj;
5751 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5752 abed = get_elf_i386_backend_data (output_bfd);
5754 if (htab->elf.dynamic_sections_created)
5756 Elf32_External_Dyn *dyncon, *dynconend;
5758 if (sdyn == NULL || htab->elf.sgot == NULL)
5761 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5762 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5763 for (; dyncon < dynconend; dyncon++)
5765 Elf_Internal_Dyn dyn;
5768 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5773 if (abed->is_vxworks
5774 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
5779 s = htab->elf.sgotplt;
5780 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5784 s = htab->elf.srelplt;
5785 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5789 s = htab->elf.srelplt;
5790 dyn.d_un.d_val = s->size;
5794 /* My reading of the SVR4 ABI indicates that the
5795 procedure linkage table relocs (DT_JMPREL) should be
5796 included in the overall relocs (DT_REL). This is
5797 what Solaris does. However, UnixWare can not handle
5798 that case. Therefore, we override the DT_RELSZ entry
5799 here to make it not include the JMPREL relocs. */
5800 s = htab->elf.srelplt;
5803 dyn.d_un.d_val -= s->size;
5807 /* We may not be using the standard ELF linker script.
5808 If .rel.plt is the first .rel section, we adjust
5809 DT_REL to not include it. */
5810 s = htab->elf.srelplt;
5813 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
5815 dyn.d_un.d_ptr += s->size;
5819 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5822 /* Fill in the first entry in the procedure linkage table. */
5823 if (htab->elf.splt && htab->elf.splt->size > 0)
5825 if (bfd_link_pic (info))
5827 memcpy (htab->elf.splt->contents, abed->plt->pic_plt0_entry,
5828 abed->plt->plt0_entry_size);
5829 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
5830 abed->plt0_pad_byte,
5831 abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
5835 memcpy (htab->elf.splt->contents, abed->plt->plt0_entry,
5836 abed->plt->plt0_entry_size);
5837 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
5838 abed->plt0_pad_byte,
5839 abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
5840 bfd_put_32 (output_bfd,
5841 (htab->elf.sgotplt->output_section->vma
5842 + htab->elf.sgotplt->output_offset
5844 htab->elf.splt->contents
5845 + abed->plt->plt0_got1_offset);
5846 bfd_put_32 (output_bfd,
5847 (htab->elf.sgotplt->output_section->vma
5848 + htab->elf.sgotplt->output_offset
5850 htab->elf.splt->contents
5851 + abed->plt->plt0_got2_offset);
5853 if (abed->is_vxworks)
5855 Elf_Internal_Rela rel;
5857 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
5858 On IA32 we use REL relocations so the addend goes in
5859 the PLT directly. */
5860 rel.r_offset = (htab->elf.splt->output_section->vma
5861 + htab->elf.splt->output_offset
5862 + abed->plt->plt0_got1_offset);
5863 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5864 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5865 htab->srelplt2->contents);
5866 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
5867 rel.r_offset = (htab->elf.splt->output_section->vma
5868 + htab->elf.splt->output_offset
5869 + abed->plt->plt0_got2_offset);
5870 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5871 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5872 htab->srelplt2->contents +
5873 sizeof (Elf32_External_Rel));
5877 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5878 really seem like the right value. */
5879 elf_section_data (htab->elf.splt->output_section)
5880 ->this_hdr.sh_entsize = 4;
5882 /* Correct the .rel.plt.unloaded relocations. */
5883 if (abed->is_vxworks && !bfd_link_pic (info))
5885 int num_plts = (htab->elf.splt->size
5886 / abed->plt->plt_entry_size) - 1;
5889 p = htab->srelplt2->contents;
5890 if (bfd_link_pic (info))
5891 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
5893 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
5895 for (; num_plts; num_plts--)
5897 Elf_Internal_Rela rel;
5898 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
5899 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5900 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
5901 p += sizeof (Elf32_External_Rel);
5903 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
5904 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
5905 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
5906 p += sizeof (Elf32_External_Rel);
5912 if (htab->elf.sgotplt)
5914 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
5916 (*_bfd_error_handler)
5917 (_("discarded output section: `%A'"), htab->elf.sgotplt);
5921 /* Fill in the first three entries in the global offset table. */
5922 if (htab->elf.sgotplt->size > 0)
5924 bfd_put_32 (output_bfd,
5926 : sdyn->output_section->vma + sdyn->output_offset),
5927 htab->elf.sgotplt->contents);
5928 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
5929 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
5932 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
5935 /* Adjust .eh_frame for .plt section. */
5936 if (htab->plt_eh_frame != NULL
5937 && htab->plt_eh_frame->contents != NULL)
5939 if (htab->elf.splt != NULL
5940 && htab->elf.splt->size != 0
5941 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
5942 && htab->elf.splt->output_section != NULL
5943 && htab->plt_eh_frame->output_section != NULL)
5945 bfd_vma plt_start = htab->elf.splt->output_section->vma;
5946 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
5947 + htab->plt_eh_frame->output_offset
5948 + PLT_FDE_START_OFFSET;
5949 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5950 htab->plt_eh_frame->contents
5951 + PLT_FDE_START_OFFSET);
5953 if (htab->plt_eh_frame->sec_info_type
5954 == SEC_INFO_TYPE_EH_FRAME)
5956 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5958 htab->plt_eh_frame->contents))
5963 /* Adjust .eh_frame for .plt.got section. */
5964 if (htab->plt_got_eh_frame != NULL
5965 && htab->plt_got_eh_frame->contents != NULL)
5967 if (htab->plt_got != NULL
5968 && htab->plt_got->size != 0
5969 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
5970 && htab->plt_got->output_section != NULL
5971 && htab->plt_got_eh_frame->output_section != NULL)
5973 bfd_vma plt_start = htab->plt_got->output_section->vma;
5974 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
5975 + htab->plt_got_eh_frame->output_offset
5976 + PLT_FDE_START_OFFSET;
5977 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5978 htab->plt_got_eh_frame->contents
5979 + PLT_FDE_START_OFFSET);
5981 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
5983 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5984 htab->plt_got_eh_frame,
5985 htab->plt_got_eh_frame->contents))
5990 if (htab->elf.sgot && htab->elf.sgot->size > 0)
5991 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
5993 /* Fill PLT entries for undefined weak symbols in PIE. */
5994 if (bfd_link_pie (info))
5995 bfd_hash_traverse (&info->hash->table,
5996 elf_i386_pie_finish_undefweak_symbol,
6002 /* Fill PLT/GOT entries and allocate dynamic relocations for local
6003 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
6004 It has to be done before elf_link_sort_relocs is called so that
6005 dynamic relocations are properly sorted. */
6008 elf_i386_output_arch_local_syms
6009 (bfd *output_bfd ATTRIBUTE_UNUSED,
6010 struct bfd_link_info *info,
6011 void *flaginfo ATTRIBUTE_UNUSED,
6012 int (*func) (void *, const char *,
6015 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
6017 struct elf_i386_link_hash_table *htab = elf_i386_hash_table (info);
6021 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
6022 htab_traverse (htab->loc_hash_table,
6023 elf_i386_finish_local_dynamic_symbol,
6029 /* Return an array of PLT entry symbol values. */
6032 elf_i386_get_plt_sym_val (bfd *abfd, asymbol **dynsyms, asection *plt,
6035 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6038 bfd_vma *plt_sym_val;
6040 bfd_byte *plt_contents;
6041 const struct elf_i386_backend_data *bed
6042 = get_elf_i386_backend_data (abfd);
6043 Elf_Internal_Shdr *hdr;
6045 /* Get the .plt section contents. */
6046 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
6047 if (plt_contents == NULL)
6049 if (!bfd_get_section_contents (abfd, (asection *) plt,
6050 plt_contents, 0, plt->size))
6053 free (plt_contents);
6057 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6058 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
6061 hdr = &elf_section_data (relplt)->this_hdr;
6062 count = relplt->size / hdr->sh_entsize;
6064 plt_sym_val = (bfd_vma *) bfd_malloc (sizeof (bfd_vma) * count);
6065 if (plt_sym_val == NULL)
6068 for (i = 0; i < count; i++)
6069 plt_sym_val[i] = -1;
6071 plt_offset = bed->plt->plt_entry_size;
6072 p = relplt->relocation;
6073 for (i = 0; i < count; i++, p++)
6077 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
6078 if (p->howto == NULL)
6081 if (p->howto->type != R_386_JUMP_SLOT
6082 && p->howto->type != R_386_IRELATIVE)
6085 reloc_index = H_GET_32 (abfd, (plt_contents + plt_offset
6086 + bed->plt->plt_reloc_offset));
6087 reloc_index /= sizeof (Elf32_External_Rel);
6088 if (reloc_index < count)
6089 plt_sym_val[reloc_index] = plt->vma + plt_offset;
6091 plt_offset += bed->plt->plt_entry_size;
6093 /* PR binutils/18437: Skip extra relocations in the .rel.plt
6095 if (plt_offset >= plt->size)
6099 free (plt_contents);
6104 /* Similar to _bfd_elf_get_synthetic_symtab. */
6107 elf_i386_get_synthetic_symtab (bfd *abfd,
6114 asection *plt = bfd_get_section_by_name (abfd, ".plt");
6115 return _bfd_elf_ifunc_get_synthetic_symtab (abfd, symcount, syms,
6116 dynsymcount, dynsyms, ret,
6118 elf_i386_get_plt_sym_val);
6121 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6124 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
6126 if (h->plt.offset != (bfd_vma) -1
6128 && !h->pointer_equality_needed)
6131 return _bfd_elf_hash_symbol (h);
6134 #define TARGET_LITTLE_SYM i386_elf32_vec
6135 #define TARGET_LITTLE_NAME "elf32-i386"
6136 #define ELF_ARCH bfd_arch_i386
6137 #define ELF_TARGET_ID I386_ELF_DATA
6138 #define ELF_MACHINE_CODE EM_386
6139 #define ELF_MAXPAGESIZE 0x1000
6141 #define elf_backend_can_gc_sections 1
6142 #define elf_backend_can_refcount 1
6143 #define elf_backend_want_got_plt 1
6144 #define elf_backend_plt_readonly 1
6145 #define elf_backend_want_plt_sym 0
6146 #define elf_backend_got_header_size 12
6147 #define elf_backend_plt_alignment 4
6148 #define elf_backend_extern_protected_data 1
6149 #define elf_backend_caches_rawsize 1
6151 /* Support RELA for objdump of prelink objects. */
6152 #define elf_info_to_howto elf_i386_info_to_howto_rel
6153 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
6155 #define bfd_elf32_mkobject elf_i386_mkobject
6157 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
6158 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
6159 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
6160 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
6161 #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
6163 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
6164 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
6165 #define elf_backend_check_relocs elf_i386_check_relocs
6166 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
6167 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
6168 #define elf_backend_fake_sections elf_i386_fake_sections
6169 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
6170 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
6171 #define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
6172 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
6173 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
6174 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
6175 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
6176 #define elf_backend_relocate_section elf_i386_relocate_section
6177 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
6178 #define elf_backend_always_size_sections elf_i386_always_size_sections
6179 #define elf_backend_omit_section_dynsym \
6180 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
6181 #define elf_backend_hash_symbol elf_i386_hash_symbol
6182 #define elf_backend_fixup_symbol elf_i386_fixup_symbol
6184 #include "elf32-target.h"
6186 /* FreeBSD support. */
6188 #undef TARGET_LITTLE_SYM
6189 #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
6190 #undef TARGET_LITTLE_NAME
6191 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
6193 #define ELF_OSABI ELFOSABI_FREEBSD
6195 /* The kernel recognizes executables as valid only if they carry a
6196 "FreeBSD" label in the ELF header. So we put this label on all
6197 executables and (for simplicity) also all other object files. */
6200 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
6202 _bfd_elf_post_process_headers (abfd, info);
6204 #ifdef OLD_FREEBSD_ABI_LABEL
6206 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
6207 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6208 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
6213 #undef elf_backend_post_process_headers
6214 #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
6216 #define elf32_bed elf32_i386_fbsd_bed
6218 #undef elf_backend_add_symbol_hook
6220 #include "elf32-target.h"
6224 #undef TARGET_LITTLE_SYM
6225 #define TARGET_LITTLE_SYM i386_elf32_sol2_vec
6226 #undef TARGET_LITTLE_NAME
6227 #define TARGET_LITTLE_NAME "elf32-i386-sol2"
6229 #undef elf_backend_post_process_headers
6231 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
6232 objects won't be recognized. */
6236 #define elf32_bed elf32_i386_sol2_bed
6238 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
6240 #undef elf_backend_static_tls_alignment
6241 #define elf_backend_static_tls_alignment 8
6243 /* The Solaris 2 ABI requires a plt symbol on all platforms.
6245 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
6247 #undef elf_backend_want_plt_sym
6248 #define elf_backend_want_plt_sym 1
6250 #undef elf_backend_strtab_flags
6251 #define elf_backend_strtab_flags SHF_STRINGS
6253 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
6254 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
6255 FALSE otherwise. ISECTION is the best guess matching section from the
6256 input bfd IBFD, but it might be NULL. */
6259 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
6260 bfd *obfd ATTRIBUTE_UNUSED,
6261 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
6262 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
6264 /* PR 19938: FIXME: Need to add code for setting the sh_info
6265 and sh_link fields of Solaris specific section types. */
6268 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
6269 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
6271 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
6273 The following values should be set:
6276 -----------------------------------------------------------------------------
6277 SHT_SUNW_ancillary The section header index of 0
6278 [0x6fffffee] the associated string table.
6280 SHT_SUNW_capinfo The section header index of For a dynamic object, the
6281 [0x6ffffff0] the associated symbol table. section header index of
6283 SHT_SUNW_capchain table,
6286 SHT_SUNW_symsort The section header index of 0
6287 [0x6ffffff1] the associated symbol table.
6289 SHT_SUNW_tlssort The section header index of 0
6290 [0x6ffffff2] the associated symbol table.
6292 SHT_SUNW_LDYNSYM The section header index of One greater than the
6293 [0x6ffffff3] the associated string table. symbol table index of the
6294 This index is the same string last local symbol,
6295 table used by the SHT_DYNSYM STB_LOCAL. Since
6296 section. SHT_SUNW_LDYNSYM only
6297 contains local symbols,
6298 sh_info is equivalent to
6299 the number of symbols in
6302 SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
6303 [0x6ffffff5] the section header index of to named strings, the
6304 the associated section header index of
6305 SHT_SUNW_capinfo table, the associated string
6306 otherwise 0. table, otherwise 0.
6308 SHT_SUNW_move The section header index of 0
6309 [0x6ffffffa] the associated symbol table.
6314 SHT_SUNW_syminfo The section header index of The section header index
6315 [0x6ffffffc] the associated symbol table. of the associated
6318 SHT_SUNW_verdef The section header index of The number of version
6319 [0x6ffffffd] the associated string table. definitions within the
6322 SHT_SUNW_verneed The section header index of The number of version
6323 [0x6ffffffe] the associated string table. dependencies within the
6326 SHT_SUNW_versym The section header index of 0
6327 [0x6fffffff] the associated symbol table. */
6330 #undef elf_backend_copy_special_section_fields
6331 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
6333 #include "elf32-target.h"
6335 /* Intel MCU support. */
6338 elf32_iamcu_elf_object_p (bfd *abfd)
6340 /* Set the right machine number for an IAMCU elf32 file. */
6341 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
6345 #undef TARGET_LITTLE_SYM
6346 #define TARGET_LITTLE_SYM iamcu_elf32_vec
6347 #undef TARGET_LITTLE_NAME
6348 #define TARGET_LITTLE_NAME "elf32-iamcu"
6350 #define ELF_ARCH bfd_arch_iamcu
6352 #undef ELF_MACHINE_CODE
6353 #define ELF_MACHINE_CODE EM_IAMCU
6358 #define elf32_bed elf32_iamcu_bed
6360 #undef elf_backend_object_p
6361 #define elf_backend_object_p elf32_iamcu_elf_object_p
6363 #undef elf_backend_static_tls_alignment
6365 #undef elf_backend_want_plt_sym
6366 #define elf_backend_want_plt_sym 0
6368 #undef elf_backend_strtab_flags
6369 #undef elf_backend_copy_special_section_fields
6371 #include "elf32-target.h"
6373 /* Restore defaults. */
6375 #define ELF_ARCH bfd_arch_i386
6376 #undef ELF_MACHINE_CODE
6377 #define ELF_MACHINE_CODE EM_386
6379 /* Native Client support. */
6381 #undef TARGET_LITTLE_SYM
6382 #define TARGET_LITTLE_SYM i386_elf32_nacl_vec
6383 #undef TARGET_LITTLE_NAME
6384 #define TARGET_LITTLE_NAME "elf32-i386-nacl"
6386 #define elf32_bed elf32_i386_nacl_bed
6388 #undef ELF_MAXPAGESIZE
6389 #define ELF_MAXPAGESIZE 0x10000
6391 /* Restore defaults. */
6393 #undef elf_backend_want_plt_sym
6394 #define elf_backend_want_plt_sym 0
6395 #undef elf_backend_post_process_headers
6396 #undef elf_backend_static_tls_alignment
6398 /* NaCl uses substantially different PLT entries for the same effects. */
6400 #undef elf_backend_plt_alignment
6401 #define elf_backend_plt_alignment 5
6402 #define NACL_PLT_ENTRY_SIZE 64
6403 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
6405 static const bfd_byte elf_i386_nacl_plt0_entry[] =
6407 0xff, 0x35, /* pushl contents of address */
6408 0, 0, 0, 0, /* replaced with address of .got + 4. */
6409 0x8b, 0x0d, /* movl contents of address, %ecx */
6410 0, 0, 0, 0, /* replaced with address of .got + 8. */
6411 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
6412 0xff, 0xe1 /* jmp *%ecx */
6415 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
6417 0x8b, 0x0d, /* movl contents of address, %ecx */
6418 0, 0, 0, 0, /* replaced with GOT slot address. */
6419 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
6420 0xff, 0xe1, /* jmp *%ecx */
6422 /* Pad to the next 32-byte boundary with nop instructions. */
6424 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6425 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6427 /* Lazy GOT entries point here (32-byte aligned). */
6428 0x68, /* pushl immediate */
6429 0, 0, 0, 0, /* replaced with reloc offset. */
6430 0xe9, /* jmp relative */
6431 0, 0, 0, 0, /* replaced with offset to .plt. */
6433 /* Pad to the next 32-byte boundary with nop instructions. */
6434 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6435 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6439 static const bfd_byte
6440 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
6442 0xff, 0x73, 0x04, /* pushl 4(%ebx) */
6443 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */
6444 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */
6445 0xff, 0xe1, /* jmp *%ecx */
6447 /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
6448 so pad to that size with nop instructions. */
6449 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
6452 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
6454 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
6455 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
6456 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */
6457 0xff, 0xe1, /* jmp *%ecx */
6459 /* Pad to the next 32-byte boundary with nop instructions. */
6461 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6462 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6464 /* Lazy GOT entries point here (32-byte aligned). */
6465 0x68, /* pushl immediate */
6466 0, 0, 0, 0, /* replaced with offset into relocation table. */
6467 0xe9, /* jmp relative */
6468 0, 0, 0, 0, /* replaced with offset to start of .plt. */
6470 /* Pad to the next 32-byte boundary with nop instructions. */
6471 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6472 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
6476 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
6478 #if (PLT_CIE_LENGTH != 20 \
6479 || PLT_FDE_LENGTH != 36 \
6480 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
6481 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
6482 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
6484 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
6485 0, 0, 0, 0, /* CIE ID */
6486 1, /* CIE version */
6487 'z', 'R', 0, /* Augmentation string */
6488 1, /* Code alignment factor */
6489 0x7c, /* Data alignment factor: -4 */
6490 8, /* Return address column */
6491 1, /* Augmentation size */
6492 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
6493 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
6494 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
6495 DW_CFA_nop, DW_CFA_nop,
6497 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
6498 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
6499 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
6500 0, 0, 0, 0, /* .plt size goes here */
6501 0, /* Augmentation size */
6502 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
6503 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
6504 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
6505 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
6506 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
6507 13, /* Block length */
6508 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
6509 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
6510 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
6511 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
6512 DW_CFA_nop, DW_CFA_nop
6515 static const struct elf_i386_plt_layout elf_i386_nacl_plt =
6517 elf_i386_nacl_plt0_entry, /* plt0_entry */
6518 sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */
6519 2, /* plt0_got1_offset */
6520 8, /* plt0_got2_offset */
6521 elf_i386_nacl_plt_entry, /* plt_entry */
6522 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
6523 2, /* plt_got_offset */
6524 33, /* plt_reloc_offset */
6525 38, /* plt_plt_offset */
6526 32, /* plt_lazy_offset */
6527 elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */
6528 elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */
6529 elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */
6530 sizeof (elf_i386_nacl_eh_frame_plt),/* eh_frame_plt_size */
6531 NULL, /* eh_frame_plt_got */
6532 0, /* eh_frame_plt_got_size */
6535 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
6537 &elf_i386_nacl_plt, /* plt */
6538 0x90, /* plt0_pad_byte: nop insn */
6543 elf32_i386_nacl_elf_object_p (bfd *abfd)
6545 /* Set the right machine number for a NaCl i386 ELF32 file. */
6546 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
6550 #undef elf_backend_arch_data
6551 #define elf_backend_arch_data &elf_i386_nacl_arch_bed
6553 #undef elf_backend_object_p
6554 #define elf_backend_object_p elf32_i386_nacl_elf_object_p
6555 #undef elf_backend_modify_segment_map
6556 #define elf_backend_modify_segment_map nacl_modify_segment_map
6557 #undef elf_backend_modify_program_headers
6558 #define elf_backend_modify_program_headers nacl_modify_program_headers
6559 #undef elf_backend_final_write_processing
6560 #define elf_backend_final_write_processing nacl_final_write_processing
6562 #include "elf32-target.h"
6564 /* Restore defaults. */
6565 #undef elf_backend_object_p
6566 #undef elf_backend_modify_segment_map
6567 #undef elf_backend_modify_program_headers
6568 #undef elf_backend_final_write_processing
6570 /* VxWorks support. */
6572 #undef TARGET_LITTLE_SYM
6573 #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
6574 #undef TARGET_LITTLE_NAME
6575 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
6577 #undef elf_backend_plt_alignment
6578 #define elf_backend_plt_alignment 4
6580 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
6582 &elf_i386_plt, /* plt */
6583 0x90, /* plt0_pad_byte */
6587 #undef elf_backend_arch_data
6588 #define elf_backend_arch_data &elf_i386_vxworks_arch_bed
6590 #undef elf_backend_relocs_compatible
6591 #undef elf_backend_add_symbol_hook
6592 #define elf_backend_add_symbol_hook \
6593 elf_vxworks_add_symbol_hook
6594 #undef elf_backend_link_output_symbol_hook
6595 #define elf_backend_link_output_symbol_hook \
6596 elf_vxworks_link_output_symbol_hook
6597 #undef elf_backend_emit_relocs
6598 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
6599 #undef elf_backend_final_write_processing
6600 #define elf_backend_final_write_processing \
6601 elf_vxworks_final_write_processing
6602 #undef elf_backend_static_tls_alignment
6604 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
6606 #undef elf_backend_want_plt_sym
6607 #define elf_backend_want_plt_sym 1
6610 #define elf32_bed elf32_i386_vxworks_bed
6612 #include "elf32-target.h"