1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright (C) 1993-2015 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"
33 /* 386 uses REL relocations instead of RELA. */
38 static reloc_howto_type elf_howto_table[]=
40 HOWTO(R_386_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
41 bfd_elf_generic_reloc, "R_386_NONE",
42 TRUE, 0x00000000, 0x00000000, FALSE),
43 HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
44 bfd_elf_generic_reloc, "R_386_32",
45 TRUE, 0xffffffff, 0xffffffff, FALSE),
46 HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
47 bfd_elf_generic_reloc, "R_386_PC32",
48 TRUE, 0xffffffff, 0xffffffff, TRUE),
49 HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
50 bfd_elf_generic_reloc, "R_386_GOT32",
51 TRUE, 0xffffffff, 0xffffffff, FALSE),
52 HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
53 bfd_elf_generic_reloc, "R_386_PLT32",
54 TRUE, 0xffffffff, 0xffffffff, TRUE),
55 HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
56 bfd_elf_generic_reloc, "R_386_COPY",
57 TRUE, 0xffffffff, 0xffffffff, FALSE),
58 HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
59 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
60 TRUE, 0xffffffff, 0xffffffff, FALSE),
61 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
63 TRUE, 0xffffffff, 0xffffffff, FALSE),
64 HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
65 bfd_elf_generic_reloc, "R_386_RELATIVE",
66 TRUE, 0xffffffff, 0xffffffff, FALSE),
67 HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
68 bfd_elf_generic_reloc, "R_386_GOTOFF",
69 TRUE, 0xffffffff, 0xffffffff, FALSE),
70 HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
71 bfd_elf_generic_reloc, "R_386_GOTPC",
72 TRUE, 0xffffffff, 0xffffffff, TRUE),
74 /* We have a gap in the reloc numbers here.
75 R_386_standard counts the number up to this point, and
76 R_386_ext_offset is the value to subtract from a reloc type of
77 R_386_16 thru R_386_PC8 to form an index into this table. */
78 #define R_386_standard (R_386_GOTPC + 1)
79 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
81 /* These relocs are a GNU extension. */
82 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
84 TRUE, 0xffffffff, 0xffffffff, FALSE),
85 HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
86 bfd_elf_generic_reloc, "R_386_TLS_IE",
87 TRUE, 0xffffffff, 0xffffffff, FALSE),
88 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
89 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
90 TRUE, 0xffffffff, 0xffffffff, FALSE),
91 HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
92 bfd_elf_generic_reloc, "R_386_TLS_LE",
93 TRUE, 0xffffffff, 0xffffffff, FALSE),
94 HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
95 bfd_elf_generic_reloc, "R_386_TLS_GD",
96 TRUE, 0xffffffff, 0xffffffff, FALSE),
97 HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
98 bfd_elf_generic_reloc, "R_386_TLS_LDM",
99 TRUE, 0xffffffff, 0xffffffff, FALSE),
100 HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
101 bfd_elf_generic_reloc, "R_386_16",
102 TRUE, 0xffff, 0xffff, FALSE),
103 HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
104 bfd_elf_generic_reloc, "R_386_PC16",
105 TRUE, 0xffff, 0xffff, TRUE),
106 HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
107 bfd_elf_generic_reloc, "R_386_8",
108 TRUE, 0xff, 0xff, FALSE),
109 HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
110 bfd_elf_generic_reloc, "R_386_PC8",
111 TRUE, 0xff, 0xff, TRUE),
113 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
114 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
115 /* These are common with Solaris TLS implementation. */
116 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
117 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
118 TRUE, 0xffffffff, 0xffffffff, FALSE),
119 HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
120 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
121 TRUE, 0xffffffff, 0xffffffff, FALSE),
122 HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
123 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
124 TRUE, 0xffffffff, 0xffffffff, FALSE),
125 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
126 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
127 TRUE, 0xffffffff, 0xffffffff, FALSE),
128 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
129 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
130 TRUE, 0xffffffff, 0xffffffff, FALSE),
131 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
132 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
133 TRUE, 0xffffffff, 0xffffffff, FALSE),
134 HOWTO(R_386_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
135 bfd_elf_generic_reloc, "R_386_SIZE32",
136 TRUE, 0xffffffff, 0xffffffff, FALSE),
137 HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
138 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
139 TRUE, 0xffffffff, 0xffffffff, FALSE),
140 HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
141 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
143 HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
144 bfd_elf_generic_reloc, "R_386_TLS_DESC",
145 TRUE, 0xffffffff, 0xffffffff, FALSE),
146 HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
147 bfd_elf_generic_reloc, "R_386_IRELATIVE",
148 TRUE, 0xffffffff, 0xffffffff, FALSE),
151 #define R_386_irelative (R_386_IRELATIVE + 1 - R_386_tls_offset)
152 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_irelative)
154 /* GNU extension to record C++ vtable hierarchy. */
155 HOWTO (R_386_GNU_VTINHERIT, /* type */
157 2, /* size (0 = byte, 1 = short, 2 = long) */
159 FALSE, /* pc_relative */
161 complain_overflow_dont, /* complain_on_overflow */
162 NULL, /* special_function */
163 "R_386_GNU_VTINHERIT", /* name */
164 FALSE, /* partial_inplace */
167 FALSE), /* pcrel_offset */
169 /* GNU extension to record C++ vtable member usage. */
170 HOWTO (R_386_GNU_VTENTRY, /* type */
172 2, /* size (0 = byte, 1 = short, 2 = long) */
174 FALSE, /* pc_relative */
176 complain_overflow_dont, /* complain_on_overflow */
177 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
178 "R_386_GNU_VTENTRY", /* name */
179 FALSE, /* partial_inplace */
182 FALSE) /* pcrel_offset */
184 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
188 #ifdef DEBUG_GEN_RELOC
190 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
195 static reloc_howto_type *
196 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
197 bfd_reloc_code_real_type code)
202 TRACE ("BFD_RELOC_NONE");
203 return &elf_howto_table[R_386_NONE];
206 TRACE ("BFD_RELOC_32");
207 return &elf_howto_table[R_386_32];
210 TRACE ("BFD_RELOC_CTOR");
211 return &elf_howto_table[R_386_32];
213 case BFD_RELOC_32_PCREL:
214 TRACE ("BFD_RELOC_PC32");
215 return &elf_howto_table[R_386_PC32];
217 case BFD_RELOC_386_GOT32:
218 TRACE ("BFD_RELOC_386_GOT32");
219 return &elf_howto_table[R_386_GOT32];
221 case BFD_RELOC_386_PLT32:
222 TRACE ("BFD_RELOC_386_PLT32");
223 return &elf_howto_table[R_386_PLT32];
225 case BFD_RELOC_386_COPY:
226 TRACE ("BFD_RELOC_386_COPY");
227 return &elf_howto_table[R_386_COPY];
229 case BFD_RELOC_386_GLOB_DAT:
230 TRACE ("BFD_RELOC_386_GLOB_DAT");
231 return &elf_howto_table[R_386_GLOB_DAT];
233 case BFD_RELOC_386_JUMP_SLOT:
234 TRACE ("BFD_RELOC_386_JUMP_SLOT");
235 return &elf_howto_table[R_386_JUMP_SLOT];
237 case BFD_RELOC_386_RELATIVE:
238 TRACE ("BFD_RELOC_386_RELATIVE");
239 return &elf_howto_table[R_386_RELATIVE];
241 case BFD_RELOC_386_GOTOFF:
242 TRACE ("BFD_RELOC_386_GOTOFF");
243 return &elf_howto_table[R_386_GOTOFF];
245 case BFD_RELOC_386_GOTPC:
246 TRACE ("BFD_RELOC_386_GOTPC");
247 return &elf_howto_table[R_386_GOTPC];
249 /* These relocs are a GNU extension. */
250 case BFD_RELOC_386_TLS_TPOFF:
251 TRACE ("BFD_RELOC_386_TLS_TPOFF");
252 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
254 case BFD_RELOC_386_TLS_IE:
255 TRACE ("BFD_RELOC_386_TLS_IE");
256 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
258 case BFD_RELOC_386_TLS_GOTIE:
259 TRACE ("BFD_RELOC_386_TLS_GOTIE");
260 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
262 case BFD_RELOC_386_TLS_LE:
263 TRACE ("BFD_RELOC_386_TLS_LE");
264 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
266 case BFD_RELOC_386_TLS_GD:
267 TRACE ("BFD_RELOC_386_TLS_GD");
268 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
270 case BFD_RELOC_386_TLS_LDM:
271 TRACE ("BFD_RELOC_386_TLS_LDM");
272 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
275 TRACE ("BFD_RELOC_16");
276 return &elf_howto_table[R_386_16 - R_386_ext_offset];
278 case BFD_RELOC_16_PCREL:
279 TRACE ("BFD_RELOC_16_PCREL");
280 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
283 TRACE ("BFD_RELOC_8");
284 return &elf_howto_table[R_386_8 - R_386_ext_offset];
286 case BFD_RELOC_8_PCREL:
287 TRACE ("BFD_RELOC_8_PCREL");
288 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
290 /* Common with Sun TLS implementation. */
291 case BFD_RELOC_386_TLS_LDO_32:
292 TRACE ("BFD_RELOC_386_TLS_LDO_32");
293 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
295 case BFD_RELOC_386_TLS_IE_32:
296 TRACE ("BFD_RELOC_386_TLS_IE_32");
297 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
299 case BFD_RELOC_386_TLS_LE_32:
300 TRACE ("BFD_RELOC_386_TLS_LE_32");
301 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
303 case BFD_RELOC_386_TLS_DTPMOD32:
304 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
305 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
307 case BFD_RELOC_386_TLS_DTPOFF32:
308 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
309 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
311 case BFD_RELOC_386_TLS_TPOFF32:
312 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
313 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
315 case BFD_RELOC_SIZE32:
316 TRACE ("BFD_RELOC_SIZE32");
317 return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
319 case BFD_RELOC_386_TLS_GOTDESC:
320 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
321 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
323 case BFD_RELOC_386_TLS_DESC_CALL:
324 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
325 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
327 case BFD_RELOC_386_TLS_DESC:
328 TRACE ("BFD_RELOC_386_TLS_DESC");
329 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
331 case BFD_RELOC_386_IRELATIVE:
332 TRACE ("BFD_RELOC_386_IRELATIVE");
333 return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
335 case BFD_RELOC_VTABLE_INHERIT:
336 TRACE ("BFD_RELOC_VTABLE_INHERIT");
337 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
339 case BFD_RELOC_VTABLE_ENTRY:
340 TRACE ("BFD_RELOC_VTABLE_ENTRY");
341 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
351 static reloc_howto_type *
352 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
357 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
358 if (elf_howto_table[i].name != NULL
359 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
360 return &elf_howto_table[i];
365 static reloc_howto_type *
366 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
370 if ((indx = r_type) >= R_386_standard
371 && ((indx = r_type - R_386_ext_offset) - R_386_standard
372 >= R_386_ext - R_386_standard)
373 && ((indx = r_type - R_386_tls_offset) - R_386_ext
374 >= R_386_irelative - R_386_ext)
375 && ((indx = r_type - R_386_vt_offset) - R_386_irelative
376 >= R_386_vt - R_386_irelative))
378 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
382 /* PR 17512: file: 0f67f69d. */
383 if (elf_howto_table [indx].type != r_type)
385 return &elf_howto_table[indx];
389 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
391 Elf_Internal_Rela *dst)
393 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
394 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
397 /* Return whether a symbol name implies a local label. The UnixWare
398 2.1 cc generates temporary symbols that start with .X, so we
399 recognize them here. FIXME: do other SVR4 compilers also use .X?.
400 If so, we should move the .X recognition into
401 _bfd_elf_is_local_label_name. */
404 elf_i386_is_local_label_name (bfd *abfd, const char *name)
406 if (name[0] == '.' && name[1] == 'X')
409 return _bfd_elf_is_local_label_name (abfd, name);
412 /* Support for core dump NOTE sections. */
415 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
420 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
422 int pr_version = bfd_get_32 (abfd, note->descdata);
428 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
431 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
435 size = bfd_get_32 (abfd, note->descdata + 8);
439 switch (note->descsz)
444 case 144: /* Linux/i386 */
446 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
449 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
459 /* Make a ".reg/999" section. */
460 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
461 size, note->descpos + offset);
465 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
467 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
469 int pr_version = bfd_get_32 (abfd, note->descdata);
474 elf_tdata (abfd)->core->program
475 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
476 elf_tdata (abfd)->core->command
477 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
481 switch (note->descsz)
486 case 124: /* Linux/i386 elf_prpsinfo. */
487 elf_tdata (abfd)->core->pid
488 = bfd_get_32 (abfd, note->descdata + 12);
489 elf_tdata (abfd)->core->program
490 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
491 elf_tdata (abfd)->core->command
492 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
496 /* Note that for some reason, a spurious space is tacked
497 onto the end of the args in some (at least one anyway)
498 implementations, so strip it off if it exists. */
500 char *command = elf_tdata (abfd)->core->command;
501 int n = strlen (command);
503 if (0 < n && command[n - 1] == ' ')
504 command[n - 1] = '\0';
510 /* Functions for the i386 ELF linker.
512 In order to gain some understanding of code in this file without
513 knowing all the intricate details of the linker, note the
516 Functions named elf_i386_* are called by external routines, other
517 functions are only called locally. elf_i386_* functions appear
518 in this file more or less in the order in which they are called
519 from external routines. eg. elf_i386_check_relocs is called
520 early in the link process, elf_i386_finish_dynamic_sections is
521 one of the last functions. */
524 /* The name of the dynamic interpreter. This is put in the .interp
527 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
529 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
530 copying dynamic variables from a shared lib into an app's dynbss
531 section, and instead use a dynamic relocation to point into the
533 #define ELIMINATE_COPY_RELOCS 1
535 /* The size in bytes of an entry in the procedure linkage table. */
537 #define PLT_ENTRY_SIZE 16
539 /* The first entry in an absolute procedure linkage table looks like
540 this. See the SVR4 ABI i386 supplement to see how this works.
541 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
543 static const bfd_byte elf_i386_plt0_entry[12] =
545 0xff, 0x35, /* pushl contents of address */
546 0, 0, 0, 0, /* replaced with address of .got + 4. */
547 0xff, 0x25, /* jmp indirect */
548 0, 0, 0, 0 /* replaced with address of .got + 8. */
551 /* Subsequent entries in an absolute procedure linkage table look like
554 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
556 0xff, 0x25, /* jmp indirect */
557 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
558 0x68, /* pushl immediate */
559 0, 0, 0, 0, /* replaced with offset into relocation table. */
560 0xe9, /* jmp relative */
561 0, 0, 0, 0 /* replaced with offset to start of .plt. */
564 /* The first entry in a PIC procedure linkage table look like this.
565 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
567 static const bfd_byte elf_i386_pic_plt0_entry[12] =
569 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
570 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
573 /* Subsequent entries in a PIC procedure linkage table look like this. */
575 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
577 0xff, 0xa3, /* jmp *offset(%ebx) */
578 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
579 0x68, /* pushl immediate */
580 0, 0, 0, 0, /* replaced with offset into relocation table. */
581 0xe9, /* jmp relative */
582 0, 0, 0, 0 /* replaced with offset to start of .plt. */
585 /* Entries in the GOT procedure linkage table look like this. */
587 static const bfd_byte elf_i386_got_plt_entry[8] =
589 0xff, 0x25, /* jmp indirect */
590 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
591 0x66, 0x90 /* xchg %ax,%ax */
594 /* Entries in the PIC GOT procedure linkage table look like this. */
596 static const bfd_byte elf_i386_pic_got_plt_entry[8] =
598 0xff, 0xa3, /* jmp *offset(%ebx) */
599 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
600 0x66, 0x90 /* xchg %ax,%ax */
603 /* .eh_frame covering the .plt section. */
605 static const bfd_byte elf_i386_eh_frame_plt[] =
607 #define PLT_CIE_LENGTH 20
608 #define PLT_FDE_LENGTH 36
609 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
610 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
611 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
612 0, 0, 0, 0, /* CIE ID */
614 'z', 'R', 0, /* Augmentation string */
615 1, /* Code alignment factor */
616 0x7c, /* Data alignment factor */
617 8, /* Return address column */
618 1, /* Augmentation size */
619 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
620 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
621 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
622 DW_CFA_nop, DW_CFA_nop,
624 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
625 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
626 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
627 0, 0, 0, 0, /* .plt size goes here */
628 0, /* Augmentation size */
629 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
630 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
631 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
632 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
633 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
634 11, /* Block length */
635 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
636 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
637 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
638 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
639 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
642 struct elf_i386_plt_layout
644 /* The first entry in an absolute procedure linkage table looks like this. */
645 const bfd_byte *plt0_entry;
646 unsigned int plt0_entry_size;
648 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
649 unsigned int plt0_got1_offset;
650 unsigned int plt0_got2_offset;
652 /* Later entries in an absolute procedure linkage table look like this. */
653 const bfd_byte *plt_entry;
654 unsigned int plt_entry_size;
656 /* Offsets into plt_entry that are to be replaced with... */
657 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
658 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
659 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
661 /* Offset into plt_entry where the initial value of the GOT entry points. */
662 unsigned int plt_lazy_offset;
664 /* The first entry in a PIC procedure linkage table looks like this. */
665 const bfd_byte *pic_plt0_entry;
667 /* Subsequent entries in a PIC procedure linkage table look like this. */
668 const bfd_byte *pic_plt_entry;
670 /* .eh_frame covering the .plt section. */
671 const bfd_byte *eh_frame_plt;
672 unsigned int eh_frame_plt_size;
675 #define GET_PLT_ENTRY_SIZE(abfd) \
676 get_elf_i386_backend_data (abfd)->plt->plt_entry_size
678 /* These are the standard parameters. */
679 static const struct elf_i386_plt_layout elf_i386_plt =
681 elf_i386_plt0_entry, /* plt0_entry */
682 sizeof (elf_i386_plt0_entry), /* plt0_entry_size */
683 2, /* plt0_got1_offset */
684 8, /* plt0_got2_offset */
685 elf_i386_plt_entry, /* plt_entry */
686 PLT_ENTRY_SIZE, /* plt_entry_size */
687 2, /* plt_got_offset */
688 7, /* plt_reloc_offset */
689 12, /* plt_plt_offset */
690 6, /* plt_lazy_offset */
691 elf_i386_pic_plt0_entry, /* pic_plt0_entry */
692 elf_i386_pic_plt_entry, /* pic_plt_entry */
693 elf_i386_eh_frame_plt, /* eh_frame_plt */
694 sizeof (elf_i386_eh_frame_plt), /* eh_frame_plt_size */
698 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
699 for the PLTResolve stub and then for each PLT entry. */
700 #define PLTRESOLVE_RELOCS_SHLIB 0
701 #define PLTRESOLVE_RELOCS 2
702 #define PLT_NON_JUMP_SLOT_RELOCS 2
704 /* Architecture-specific backend data for i386. */
706 struct elf_i386_backend_data
708 /* Parameters describing PLT generation. */
709 const struct elf_i386_plt_layout *plt;
711 /* Value used to fill the unused bytes of the first PLT entry. */
712 bfd_byte plt0_pad_byte;
714 /* True if the target system is VxWorks. */
718 #define get_elf_i386_backend_data(abfd) \
719 ((const struct elf_i386_backend_data *) \
720 get_elf_backend_data (abfd)->arch_data)
722 /* These are the standard parameters. */
723 static const struct elf_i386_backend_data elf_i386_arch_bed =
725 &elf_i386_plt, /* plt */
726 0, /* plt0_pad_byte */
730 #define elf_backend_arch_data &elf_i386_arch_bed
732 /* i386 ELF linker hash entry. */
734 struct elf_i386_link_hash_entry
736 struct elf_link_hash_entry elf;
738 /* Track dynamic relocs copied for this symbol. */
739 struct elf_dyn_relocs *dyn_relocs;
741 #define GOT_UNKNOWN 0
745 #define GOT_TLS_IE_POS 5
746 #define GOT_TLS_IE_NEG 6
747 #define GOT_TLS_IE_BOTH 7
748 #define GOT_TLS_GDESC 8
749 #define GOT_TLS_GD_BOTH_P(type) \
750 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
751 #define GOT_TLS_GD_P(type) \
752 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
753 #define GOT_TLS_GDESC_P(type) \
754 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
755 #define GOT_TLS_GD_ANY_P(type) \
756 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
757 unsigned char tls_type;
759 /* Information about the GOT PLT entry. Filled when there are both
760 GOT and PLT relocations against the same function. */
761 union gotplt_union plt_got;
763 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
764 starting at the end of the jump table. */
768 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
770 struct elf_i386_obj_tdata
772 struct elf_obj_tdata root;
774 /* tls_type for each local got entry. */
775 char *local_got_tls_type;
777 /* GOTPLT entries for TLS descriptors. */
778 bfd_vma *local_tlsdesc_gotent;
781 #define elf_i386_tdata(abfd) \
782 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
784 #define elf_i386_local_got_tls_type(abfd) \
785 (elf_i386_tdata (abfd)->local_got_tls_type)
787 #define elf_i386_local_tlsdesc_gotent(abfd) \
788 (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
790 #define is_i386_elf(bfd) \
791 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
792 && elf_tdata (bfd) != NULL \
793 && elf_object_id (bfd) == I386_ELF_DATA)
796 elf_i386_mkobject (bfd *abfd)
798 return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
802 /* i386 ELF linker hash table. */
804 struct elf_i386_link_hash_table
806 struct elf_link_hash_table elf;
808 /* Short-cuts to get to dynamic linker sections. */
811 asection *plt_eh_frame;
816 bfd_signed_vma refcount;
820 /* The amount of space used by the reserved portion of the sgotplt
821 section, plus whatever space is used by the jump slots. */
822 bfd_vma sgotplt_jump_table_size;
824 /* Small local sym cache. */
825 struct sym_cache sym_cache;
827 /* _TLS_MODULE_BASE_ symbol. */
828 struct bfd_link_hash_entry *tls_module_base;
830 /* Used by local STT_GNU_IFUNC symbols. */
831 htab_t loc_hash_table;
832 void * loc_hash_memory;
834 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
837 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
838 bfd_vma next_tls_desc_index;
840 /* The index of the next unused R_386_JUMP_SLOT slot in .rel.plt. */
841 bfd_vma next_jump_slot_index;
843 /* The index of the next unused R_386_IRELATIVE slot in .rel.plt. */
844 bfd_vma next_irelative_index;
847 /* Get the i386 ELF linker hash table from a link_info structure. */
849 #define elf_i386_hash_table(p) \
850 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
851 == I386_ELF_DATA ? ((struct elf_i386_link_hash_table *) ((p)->hash)) : NULL)
853 #define elf_i386_compute_jump_table_size(htab) \
854 ((htab)->elf.srelplt->reloc_count * 4)
856 /* Create an entry in an i386 ELF linker hash table. */
858 static struct bfd_hash_entry *
859 elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
860 struct bfd_hash_table *table,
863 /* Allocate the structure if it has not already been allocated by a
867 entry = (struct bfd_hash_entry *)
868 bfd_hash_allocate (table, sizeof (struct elf_i386_link_hash_entry));
873 /* Call the allocation method of the superclass. */
874 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
877 struct elf_i386_link_hash_entry *eh;
879 eh = (struct elf_i386_link_hash_entry *) entry;
880 eh->dyn_relocs = NULL;
881 eh->tls_type = GOT_UNKNOWN;
882 eh->plt_got.offset = (bfd_vma) -1;
883 eh->tlsdesc_got = (bfd_vma) -1;
889 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
890 for local symbol so that we can handle local STT_GNU_IFUNC symbols
891 as global symbol. We reuse indx and dynstr_index for local symbol
892 hash since they aren't used by global symbols in this backend. */
895 elf_i386_local_htab_hash (const void *ptr)
897 struct elf_link_hash_entry *h
898 = (struct elf_link_hash_entry *) ptr;
899 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
902 /* Compare local hash entries. */
905 elf_i386_local_htab_eq (const void *ptr1, const void *ptr2)
907 struct elf_link_hash_entry *h1
908 = (struct elf_link_hash_entry *) ptr1;
909 struct elf_link_hash_entry *h2
910 = (struct elf_link_hash_entry *) ptr2;
912 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
915 /* Find and/or create a hash entry for local symbol. */
917 static struct elf_link_hash_entry *
918 elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab,
919 bfd *abfd, const Elf_Internal_Rela *rel,
922 struct elf_i386_link_hash_entry e, *ret;
923 asection *sec = abfd->sections;
924 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
925 ELF32_R_SYM (rel->r_info));
928 e.elf.indx = sec->id;
929 e.elf.dynstr_index = ELF32_R_SYM (rel->r_info);
930 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
931 create ? INSERT : NO_INSERT);
938 ret = (struct elf_i386_link_hash_entry *) *slot;
942 ret = (struct elf_i386_link_hash_entry *)
943 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
944 sizeof (struct elf_i386_link_hash_entry));
947 memset (ret, 0, sizeof (*ret));
948 ret->elf.indx = sec->id;
949 ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
950 ret->elf.dynindx = -1;
951 ret->plt_got.offset = (bfd_vma) -1;
957 /* Destroy an i386 ELF linker hash table. */
960 elf_i386_link_hash_table_free (bfd *obfd)
962 struct elf_i386_link_hash_table *htab
963 = (struct elf_i386_link_hash_table *) obfd->link.hash;
965 if (htab->loc_hash_table)
966 htab_delete (htab->loc_hash_table);
967 if (htab->loc_hash_memory)
968 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
969 _bfd_elf_link_hash_table_free (obfd);
972 /* Create an i386 ELF linker hash table. */
974 static struct bfd_link_hash_table *
975 elf_i386_link_hash_table_create (bfd *abfd)
977 struct elf_i386_link_hash_table *ret;
978 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
980 ret = (struct elf_i386_link_hash_table *) bfd_zmalloc (amt);
984 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
985 elf_i386_link_hash_newfunc,
986 sizeof (struct elf_i386_link_hash_entry),
993 ret->loc_hash_table = htab_try_create (1024,
994 elf_i386_local_htab_hash,
995 elf_i386_local_htab_eq,
997 ret->loc_hash_memory = objalloc_create ();
998 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1000 elf_i386_link_hash_table_free (abfd);
1003 ret->elf.root.hash_table_free = elf_i386_link_hash_table_free;
1005 return &ret->elf.root;
1008 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
1009 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
1013 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
1015 struct elf_i386_link_hash_table *htab;
1017 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
1020 htab = elf_i386_hash_table (info);
1024 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
1026 htab->srelbss = bfd_get_linker_section (dynobj, ".rel.bss");
1029 || (!info->shared && !htab->srelbss))
1032 if (get_elf_i386_backend_data (dynobj)->is_vxworks
1033 && !elf_vxworks_create_dynamic_sections (dynobj, info,
1037 if (!info->no_ld_generated_unwind_info
1038 && htab->plt_eh_frame == NULL
1039 && htab->elf.splt != NULL)
1041 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
1042 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1043 | SEC_LINKER_CREATED);
1045 = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
1046 if (htab->plt_eh_frame == NULL
1047 || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 2))
1054 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1057 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
1058 struct elf_link_hash_entry *dir,
1059 struct elf_link_hash_entry *ind)
1061 struct elf_i386_link_hash_entry *edir, *eind;
1063 edir = (struct elf_i386_link_hash_entry *) dir;
1064 eind = (struct elf_i386_link_hash_entry *) ind;
1066 if (eind->dyn_relocs != NULL)
1068 if (edir->dyn_relocs != NULL)
1070 struct elf_dyn_relocs **pp;
1071 struct elf_dyn_relocs *p;
1073 /* Add reloc counts against the indirect sym to the direct sym
1074 list. Merge any entries against the same section. */
1075 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1077 struct elf_dyn_relocs *q;
1079 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1080 if (q->sec == p->sec)
1082 q->pc_count += p->pc_count;
1083 q->count += p->count;
1090 *pp = edir->dyn_relocs;
1093 edir->dyn_relocs = eind->dyn_relocs;
1094 eind->dyn_relocs = NULL;
1097 if (ind->root.type == bfd_link_hash_indirect
1098 && dir->got.refcount <= 0)
1100 edir->tls_type = eind->tls_type;
1101 eind->tls_type = GOT_UNKNOWN;
1104 if (ELIMINATE_COPY_RELOCS
1105 && ind->root.type != bfd_link_hash_indirect
1106 && dir->dynamic_adjusted)
1108 /* If called to transfer flags for a weakdef during processing
1109 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1110 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1111 dir->ref_dynamic |= ind->ref_dynamic;
1112 dir->ref_regular |= ind->ref_regular;
1113 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1114 dir->needs_plt |= ind->needs_plt;
1115 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1118 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1121 /* Return TRUE if the TLS access code sequence support transition
1125 elf_i386_check_tls_transition (bfd *abfd, asection *sec,
1127 Elf_Internal_Shdr *symtab_hdr,
1128 struct elf_link_hash_entry **sym_hashes,
1129 unsigned int r_type,
1130 const Elf_Internal_Rela *rel,
1131 const Elf_Internal_Rela *relend)
1133 unsigned int val, type;
1134 unsigned long r_symndx;
1135 struct elf_link_hash_entry *h;
1138 /* Get the section contents. */
1139 if (contents == NULL)
1141 if (elf_section_data (sec)->this_hdr.contents != NULL)
1142 contents = elf_section_data (sec)->this_hdr.contents;
1145 /* FIXME: How to better handle error condition? */
1146 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1149 /* Cache the section contents for elf_link_input_bfd. */
1150 elf_section_data (sec)->this_hdr.contents = contents;
1154 offset = rel->r_offset;
1159 if (offset < 2 || (rel + 1) >= relend)
1162 type = bfd_get_8 (abfd, contents + offset - 2);
1163 if (r_type == R_386_TLS_GD)
1165 /* Check transition from GD access model. Only
1166 leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
1167 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
1168 can transit to different access model. */
1169 if ((offset + 10) > sec->size ||
1170 (type != 0x8d && type != 0x04))
1173 val = bfd_get_8 (abfd, contents + offset - 1);
1176 /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
1180 if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
1183 if ((val & 0xc7) != 0x05 || val == (4 << 3))
1188 /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop */
1189 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
1192 if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
1198 /* Check transition from LD access model. Only
1199 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
1200 can transit to different access model. */
1201 if (type != 0x8d || (offset + 9) > sec->size)
1204 val = bfd_get_8 (abfd, contents + offset - 1);
1205 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
1209 if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
1212 r_symndx = ELF32_R_SYM (rel[1].r_info);
1213 if (r_symndx < symtab_hdr->sh_info)
1216 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1217 /* Use strncmp to check ___tls_get_addr since ___tls_get_addr
1218 may be versioned. */
1220 && h->root.root.string != NULL
1221 && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1222 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
1223 && (strncmp (h->root.root.string, "___tls_get_addr",
1227 /* Check transition from IE access model:
1228 movl foo@indntpoff(%rip), %eax
1229 movl foo@indntpoff(%rip), %reg
1230 addl foo@indntpoff(%rip), %reg
1233 if (offset < 1 || (offset + 4) > sec->size)
1236 /* Check "movl foo@tpoff(%rip), %eax" first. */
1237 val = bfd_get_8 (abfd, contents + offset - 1);
1244 /* Check movl|addl foo@tpoff(%rip), %reg. */
1245 type = bfd_get_8 (abfd, contents + offset - 2);
1246 return ((type == 0x8b || type == 0x03)
1247 && (val & 0xc7) == 0x05);
1249 case R_386_TLS_GOTIE:
1250 case R_386_TLS_IE_32:
1251 /* Check transition from {IE_32,GOTIE} access model:
1252 subl foo@{tpoff,gontoff}(%reg1), %reg2
1253 movl foo@{tpoff,gontoff}(%reg1), %reg2
1254 addl foo@{tpoff,gontoff}(%reg1), %reg2
1257 if (offset < 2 || (offset + 4) > sec->size)
1260 val = bfd_get_8 (abfd, contents + offset - 1);
1261 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1264 type = bfd_get_8 (abfd, contents + offset - 2);
1265 return type == 0x8b || type == 0x2b || type == 0x03;
1267 case R_386_TLS_GOTDESC:
1268 /* Check transition from GDesc access model:
1269 leal x@tlsdesc(%ebx), %eax
1271 Make sure it's a leal adding ebx to a 32-bit offset
1272 into any register, although it's probably almost always
1275 if (offset < 2 || (offset + 4) > sec->size)
1278 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1281 val = bfd_get_8 (abfd, contents + offset - 1);
1282 return (val & 0xc7) == 0x83;
1284 case R_386_TLS_DESC_CALL:
1285 /* Check transition from GDesc access model:
1286 call *x@tlsdesc(%rax)
1288 if (offset + 2 <= sec->size)
1290 /* Make sure that it's a call *x@tlsdesc(%rax). */
1291 static const unsigned char call[] = { 0xff, 0x10 };
1292 return memcmp (contents + offset, call, 2) == 0;
1302 /* Return TRUE if the TLS access transition is OK or no transition
1303 will be performed. Update R_TYPE if there is a transition. */
1306 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1307 asection *sec, bfd_byte *contents,
1308 Elf_Internal_Shdr *symtab_hdr,
1309 struct elf_link_hash_entry **sym_hashes,
1310 unsigned int *r_type, int tls_type,
1311 const Elf_Internal_Rela *rel,
1312 const Elf_Internal_Rela *relend,
1313 struct elf_link_hash_entry *h,
1314 unsigned long r_symndx)
1316 unsigned int from_type = *r_type;
1317 unsigned int to_type = from_type;
1318 bfd_boolean check = TRUE;
1320 /* Skip TLS transition for functions. */
1322 && (h->type == STT_FUNC
1323 || h->type == STT_GNU_IFUNC))
1329 case R_386_TLS_GOTDESC:
1330 case R_386_TLS_DESC_CALL:
1331 case R_386_TLS_IE_32:
1333 case R_386_TLS_GOTIE:
1334 if (info->executable)
1337 to_type = R_386_TLS_LE_32;
1338 else if (from_type != R_386_TLS_IE
1339 && from_type != R_386_TLS_GOTIE)
1340 to_type = R_386_TLS_IE_32;
1343 /* When we are called from elf_i386_relocate_section, CONTENTS
1344 isn't NULL and there may be additional transitions based on
1346 if (contents != NULL)
1348 unsigned int new_to_type = to_type;
1350 if (info->executable
1353 && (tls_type & GOT_TLS_IE))
1354 new_to_type = R_386_TLS_LE_32;
1356 if (to_type == R_386_TLS_GD
1357 || to_type == R_386_TLS_GOTDESC
1358 || to_type == R_386_TLS_DESC_CALL)
1360 if (tls_type == GOT_TLS_IE_POS)
1361 new_to_type = R_386_TLS_GOTIE;
1362 else if (tls_type & GOT_TLS_IE)
1363 new_to_type = R_386_TLS_IE_32;
1366 /* We checked the transition before when we were called from
1367 elf_i386_check_relocs. We only want to check the new
1368 transition which hasn't been checked before. */
1369 check = new_to_type != to_type && from_type == to_type;
1370 to_type = new_to_type;
1376 if (info->executable)
1377 to_type = R_386_TLS_LE_32;
1384 /* Return TRUE if there is no transition. */
1385 if (from_type == to_type)
1388 /* Check if the transition can be performed. */
1390 && ! elf_i386_check_tls_transition (abfd, sec, contents,
1391 symtab_hdr, sym_hashes,
1392 from_type, rel, relend))
1394 reloc_howto_type *from, *to;
1397 from = elf_i386_rtype_to_howto (abfd, from_type);
1398 to = elf_i386_rtype_to_howto (abfd, to_type);
1401 name = h->root.root.string;
1404 struct elf_i386_link_hash_table *htab;
1406 htab = elf_i386_hash_table (info);
1411 Elf_Internal_Sym *isym;
1413 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1415 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1419 (*_bfd_error_handler)
1420 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1421 "in section `%A' failed"),
1422 abfd, sec, from->name, to->name, name,
1423 (unsigned long) rel->r_offset);
1424 bfd_set_error (bfd_error_bad_value);
1432 /* Look through the relocs for a section during the first phase, and
1433 calculate needed space in the global offset table, procedure linkage
1434 table, and dynamic reloc sections. */
1437 elf_i386_check_relocs (bfd *abfd,
1438 struct bfd_link_info *info,
1440 const Elf_Internal_Rela *relocs)
1442 struct elf_i386_link_hash_table *htab;
1443 Elf_Internal_Shdr *symtab_hdr;
1444 struct elf_link_hash_entry **sym_hashes;
1445 const Elf_Internal_Rela *rel;
1446 const Elf_Internal_Rela *rel_end;
1448 bfd_boolean use_plt_got;
1450 if (info->relocatable)
1453 BFD_ASSERT (is_i386_elf (abfd));
1455 htab = elf_i386_hash_table (info);
1459 use_plt_got = (!get_elf_i386_backend_data (abfd)->is_vxworks
1460 && (get_elf_i386_backend_data (abfd)
1461 == &elf_i386_arch_bed));
1463 symtab_hdr = &elf_symtab_hdr (abfd);
1464 sym_hashes = elf_sym_hashes (abfd);
1468 rel_end = relocs + sec->reloc_count;
1469 for (rel = relocs; rel < rel_end; rel++)
1471 unsigned int r_type;
1472 unsigned long r_symndx;
1473 struct elf_link_hash_entry *h;
1474 Elf_Internal_Sym *isym;
1476 bfd_boolean size_reloc;
1478 r_symndx = ELF32_R_SYM (rel->r_info);
1479 r_type = ELF32_R_TYPE (rel->r_info);
1481 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1483 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1489 if (r_symndx < symtab_hdr->sh_info)
1491 /* A local symbol. */
1492 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1497 /* Check relocation against local STT_GNU_IFUNC symbol. */
1498 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1500 h = elf_i386_get_local_sym_hash (htab, abfd, rel, TRUE);
1504 /* Fake a STT_GNU_IFUNC symbol. */
1505 h->type = STT_GNU_IFUNC;
1508 h->forced_local = 1;
1509 h->root.type = bfd_link_hash_defined;
1517 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1518 while (h->root.type == bfd_link_hash_indirect
1519 || h->root.type == bfd_link_hash_warning)
1520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1525 /* Create the ifunc sections for static executables. If we
1526 never see an indirect function symbol nor we are building
1527 a static executable, those sections will be empty and
1528 won't appear in output. */
1539 if (htab->elf.dynobj == NULL)
1540 htab->elf.dynobj = abfd;
1541 if (!_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
1546 /* It is referenced by a non-shared object. */
1548 h->root.non_ir_ref = 1;
1551 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1552 symtab_hdr, sym_hashes,
1553 &r_type, GOT_UNKNOWN,
1554 rel, rel_end, h, r_symndx))
1560 htab->tls_ldm_got.refcount += 1;
1564 /* This symbol requires a procedure linkage table entry. We
1565 actually build the entry in adjust_dynamic_symbol,
1566 because this might be a case of linking PIC code which is
1567 never referenced by a dynamic object, in which case we
1568 don't need to generate a procedure linkage table entry
1571 /* If this is a local symbol, we resolve it directly without
1572 creating a procedure linkage table entry. */
1577 h->plt.refcount += 1;
1584 case R_386_TLS_IE_32:
1586 case R_386_TLS_GOTIE:
1587 if (!info->executable)
1588 info->flags |= DF_STATIC_TLS;
1593 case R_386_TLS_GOTDESC:
1594 case R_386_TLS_DESC_CALL:
1595 /* This symbol requires a global offset table entry. */
1597 int tls_type, old_tls_type;
1602 case R_386_GOT32: tls_type = GOT_NORMAL; break;
1603 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1604 case R_386_TLS_GOTDESC:
1605 case R_386_TLS_DESC_CALL:
1606 tls_type = GOT_TLS_GDESC; break;
1607 case R_386_TLS_IE_32:
1608 if (ELF32_R_TYPE (rel->r_info) == r_type)
1609 tls_type = GOT_TLS_IE_NEG;
1611 /* If this is a GD->IE transition, we may use either of
1612 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1613 tls_type = GOT_TLS_IE;
1616 case R_386_TLS_GOTIE:
1617 tls_type = GOT_TLS_IE_POS; break;
1622 h->got.refcount += 1;
1623 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1627 bfd_signed_vma *local_got_refcounts;
1629 /* This is a global offset table entry for a local symbol. */
1630 local_got_refcounts = elf_local_got_refcounts (abfd);
1631 if (local_got_refcounts == NULL)
1635 size = symtab_hdr->sh_info;
1636 size *= (sizeof (bfd_signed_vma)
1637 + sizeof (bfd_vma) + sizeof(char));
1638 local_got_refcounts = (bfd_signed_vma *)
1639 bfd_zalloc (abfd, size);
1640 if (local_got_refcounts == NULL)
1642 elf_local_got_refcounts (abfd) = local_got_refcounts;
1643 elf_i386_local_tlsdesc_gotent (abfd)
1644 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1645 elf_i386_local_got_tls_type (abfd)
1646 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1648 local_got_refcounts[r_symndx] += 1;
1649 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1652 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1653 tls_type |= old_tls_type;
1654 /* If a TLS symbol is accessed using IE at least once,
1655 there is no point to use dynamic model for it. */
1656 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1657 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1658 || (tls_type & GOT_TLS_IE) == 0))
1660 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1661 tls_type = old_tls_type;
1662 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1663 && GOT_TLS_GD_ANY_P (tls_type))
1664 tls_type |= old_tls_type;
1668 name = h->root.root.string;
1670 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1672 (*_bfd_error_handler)
1673 (_("%B: `%s' accessed both as normal and "
1674 "thread local symbol"),
1676 bfd_set_error (bfd_error_bad_value);
1681 if (old_tls_type != tls_type)
1684 elf_i386_hash_entry (h)->tls_type = tls_type;
1686 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1694 if (htab->elf.sgot == NULL)
1696 if (htab->elf.dynobj == NULL)
1697 htab->elf.dynobj = abfd;
1698 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
1701 if (r_type != R_386_TLS_IE)
1705 case R_386_TLS_LE_32:
1707 if (info->executable)
1709 info->flags |= DF_STATIC_TLS;
1714 if (h != NULL && info->executable)
1716 /* If this reloc is in a read-only section, we might
1717 need a copy reloc. We can't check reliably at this
1718 stage whether the section is read-only, as input
1719 sections have not yet been mapped to output sections.
1720 Tentatively set the flag for now, and correct in
1721 adjust_dynamic_symbol. */
1724 /* We may need a .plt entry if the function this reloc
1725 refers to is in a shared lib. */
1726 h->plt.refcount += 1;
1727 if (r_type != R_386_PC32)
1728 h->pointer_equality_needed = 1;
1733 /* If we are creating a shared library, and this is a reloc
1734 against a global symbol, or a non PC relative reloc
1735 against a local symbol, then we need to copy the reloc
1736 into the shared library. However, if we are linking with
1737 -Bsymbolic, we do not need to copy a reloc against a
1738 global symbol which is defined in an object we are
1739 including in the link (i.e., DEF_REGULAR is set). At
1740 this point we have not seen all the input files, so it is
1741 possible that DEF_REGULAR is not set now but will be set
1742 later (it is never cleared). In case of a weak definition,
1743 DEF_REGULAR may be cleared later by a strong definition in
1744 a shared library. We account for that possibility below by
1745 storing information in the relocs_copied field of the hash
1746 table entry. A similar situation occurs when creating
1747 shared libraries and symbol visibility changes render the
1750 If on the other hand, we are creating an executable, we
1751 may need to keep relocations for symbols satisfied by a
1752 dynamic library if we manage to avoid copy relocs for the
1755 && (sec->flags & SEC_ALLOC) != 0
1756 && (r_type != R_386_PC32
1758 && (! SYMBOLIC_BIND (info, h)
1759 || h->root.type == bfd_link_hash_defweak
1760 || !h->def_regular))))
1761 || (ELIMINATE_COPY_RELOCS
1763 && (sec->flags & SEC_ALLOC) != 0
1765 && (h->root.type == bfd_link_hash_defweak
1766 || !h->def_regular)))
1768 struct elf_dyn_relocs *p;
1769 struct elf_dyn_relocs **head;
1771 /* We must copy these reloc types into the output file.
1772 Create a reloc section in dynobj and make room for
1776 if (htab->elf.dynobj == NULL)
1777 htab->elf.dynobj = abfd;
1779 sreloc = _bfd_elf_make_dynamic_reloc_section
1780 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1786 /* If this is a global symbol, we count the number of
1787 relocations we need for this symbol. */
1790 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1794 /* Track dynamic relocs needed for local syms too.
1795 We really need local syms available to do this
1800 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1805 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1809 vpp = &elf_section_data (s)->local_dynrel;
1810 head = (struct elf_dyn_relocs **)vpp;
1814 if (p == NULL || p->sec != sec)
1816 bfd_size_type amt = sizeof *p;
1817 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1829 /* Count size relocation as PC-relative relocation. */
1830 if (r_type == R_386_PC32 || size_reloc)
1835 /* This relocation describes the C++ object vtable hierarchy.
1836 Reconstruct it for later use during GC. */
1837 case R_386_GNU_VTINHERIT:
1838 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1842 /* This relocation describes which C++ vtable entries are actually
1843 used. Record for later use during GC. */
1844 case R_386_GNU_VTENTRY:
1845 BFD_ASSERT (h != NULL);
1847 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1857 && h->plt.refcount > 0
1858 && h->got.refcount > 0
1859 && htab->plt_got == NULL)
1861 /* Create the GOT procedure linkage table. */
1862 unsigned int plt_got_align;
1863 const struct elf_backend_data *bed;
1865 bed = get_elf_backend_data (info->output_bfd);
1866 BFD_ASSERT (sizeof (elf_i386_got_plt_entry) == 8
1867 && (sizeof (elf_i386_got_plt_entry)
1868 == sizeof (elf_i386_pic_got_plt_entry)));
1871 if (htab->elf.dynobj == NULL)
1872 htab->elf.dynobj = abfd;
1874 = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
1876 (bed->dynamic_sec_flags
1881 if (htab->plt_got == NULL
1882 || !bfd_set_section_alignment (htab->elf.dynobj,
1892 /* Return the section that should be marked against GC for a given
1896 elf_i386_gc_mark_hook (asection *sec,
1897 struct bfd_link_info *info,
1898 Elf_Internal_Rela *rel,
1899 struct elf_link_hash_entry *h,
1900 Elf_Internal_Sym *sym)
1903 switch (ELF32_R_TYPE (rel->r_info))
1905 case R_386_GNU_VTINHERIT:
1906 case R_386_GNU_VTENTRY:
1910 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1913 /* Update the got entry reference counts for the section being removed. */
1916 elf_i386_gc_sweep_hook (bfd *abfd,
1917 struct bfd_link_info *info,
1919 const Elf_Internal_Rela *relocs)
1921 struct elf_i386_link_hash_table *htab;
1922 Elf_Internal_Shdr *symtab_hdr;
1923 struct elf_link_hash_entry **sym_hashes;
1924 bfd_signed_vma *local_got_refcounts;
1925 const Elf_Internal_Rela *rel, *relend;
1927 if (info->relocatable)
1930 htab = elf_i386_hash_table (info);
1934 elf_section_data (sec)->local_dynrel = NULL;
1936 symtab_hdr = &elf_symtab_hdr (abfd);
1937 sym_hashes = elf_sym_hashes (abfd);
1938 local_got_refcounts = elf_local_got_refcounts (abfd);
1940 relend = relocs + sec->reloc_count;
1941 for (rel = relocs; rel < relend; rel++)
1943 unsigned long r_symndx;
1944 unsigned int r_type;
1945 struct elf_link_hash_entry *h = NULL;
1947 r_symndx = ELF32_R_SYM (rel->r_info);
1948 if (r_symndx >= symtab_hdr->sh_info)
1950 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1951 while (h->root.type == bfd_link_hash_indirect
1952 || h->root.type == bfd_link_hash_warning)
1953 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1957 /* A local symbol. */
1958 Elf_Internal_Sym *isym;
1960 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1963 /* Check relocation against local STT_GNU_IFUNC symbol. */
1965 && ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1967 h = elf_i386_get_local_sym_hash (htab, abfd, rel, FALSE);
1975 struct elf_i386_link_hash_entry *eh;
1976 struct elf_dyn_relocs **pp;
1977 struct elf_dyn_relocs *p;
1979 eh = (struct elf_i386_link_hash_entry *) h;
1980 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1983 /* Everything must go for SEC. */
1989 r_type = ELF32_R_TYPE (rel->r_info);
1990 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1991 symtab_hdr, sym_hashes,
1992 &r_type, GOT_UNKNOWN,
1993 rel, relend, h, r_symndx))
1999 if (htab->tls_ldm_got.refcount > 0)
2000 htab->tls_ldm_got.refcount -= 1;
2004 case R_386_TLS_GOTDESC:
2005 case R_386_TLS_DESC_CALL:
2006 case R_386_TLS_IE_32:
2008 case R_386_TLS_GOTIE:
2012 if (h->got.refcount > 0)
2013 h->got.refcount -= 1;
2014 if (h->type == STT_GNU_IFUNC)
2016 if (h->plt.refcount > 0)
2017 h->plt.refcount -= 1;
2020 else if (local_got_refcounts != NULL)
2022 if (local_got_refcounts[r_symndx] > 0)
2023 local_got_refcounts[r_symndx] -= 1;
2031 && (h == NULL || h->type != STT_GNU_IFUNC))
2038 if (h->plt.refcount > 0)
2039 h->plt.refcount -= 1;
2044 if (h != NULL && h->type == STT_GNU_IFUNC)
2046 if (h->got.refcount > 0)
2047 h->got.refcount -= 1;
2048 if (h->plt.refcount > 0)
2049 h->plt.refcount -= 1;
2061 /* Adjust a symbol defined by a dynamic object and referenced by a
2062 regular object. The current definition is in some section of the
2063 dynamic object, but we're not including those sections. We have to
2064 change the definition to something the rest of the link can
2068 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
2069 struct elf_link_hash_entry *h)
2071 struct elf_i386_link_hash_table *htab;
2073 struct elf_i386_link_hash_entry *eh;
2074 struct elf_dyn_relocs *p;
2076 /* STT_GNU_IFUNC symbol must go through PLT. */
2077 if (h->type == STT_GNU_IFUNC)
2079 /* All local STT_GNU_IFUNC references must be treate as local
2080 calls via local PLT. */
2082 && SYMBOL_CALLS_LOCAL (info, h))
2084 bfd_size_type pc_count = 0, count = 0;
2085 struct elf_dyn_relocs **pp;
2087 eh = (struct elf_i386_link_hash_entry *) h;
2088 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2090 pc_count += p->pc_count;
2091 p->count -= p->pc_count;
2100 if (pc_count || count)
2104 if (h->plt.refcount <= 0)
2105 h->plt.refcount = 1;
2107 h->plt.refcount += 1;
2111 if (h->plt.refcount <= 0)
2113 h->plt.offset = (bfd_vma) -1;
2119 /* If this is a function, put it in the procedure linkage table. We
2120 will fill in the contents of the procedure linkage table later,
2121 when we know the address of the .got section. */
2122 if (h->type == STT_FUNC
2125 if (h->plt.refcount <= 0
2126 || SYMBOL_CALLS_LOCAL (info, h)
2127 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2128 && h->root.type == bfd_link_hash_undefweak))
2130 /* This case can occur if we saw a PLT32 reloc in an input
2131 file, but the symbol was never referred to by a dynamic
2132 object, or if all references were garbage collected. In
2133 such a case, we don't actually need to build a procedure
2134 linkage table, and we can just do a PC32 reloc instead. */
2135 h->plt.offset = (bfd_vma) -1;
2142 /* It's possible that we incorrectly decided a .plt reloc was
2143 needed for an R_386_PC32 reloc to a non-function sym in
2144 check_relocs. We can't decide accurately between function and
2145 non-function syms in check-relocs; Objects loaded later in
2146 the link may change h->type. So fix it now. */
2147 h->plt.offset = (bfd_vma) -1;
2149 /* If this is a weak symbol, and there is a real definition, the
2150 processor independent code will have arranged for us to see the
2151 real definition first, and we can just use the same value. */
2152 if (h->u.weakdef != NULL)
2154 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2155 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2156 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2157 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2158 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
2159 h->non_got_ref = h->u.weakdef->non_got_ref;
2163 /* This is a reference to a symbol defined by a dynamic object which
2164 is not a function. */
2166 /* If we are creating a shared library, we must presume that the
2167 only references to the symbol are via the global offset table.
2168 For such cases we need not do anything here; the relocations will
2169 be handled correctly by relocate_section. */
2173 /* If there are no references to this symbol that do not use the
2174 GOT, we don't need to generate a copy reloc. */
2175 if (!h->non_got_ref)
2178 /* If -z nocopyreloc was given, we won't generate them either. */
2179 if (info->nocopyreloc)
2185 htab = elf_i386_hash_table (info);
2189 /* If there aren't any dynamic relocs in read-only sections, then
2190 we can keep the dynamic relocs and avoid the copy reloc. This
2191 doesn't work on VxWorks, where we can not have dynamic relocations
2192 (other than copy and jump slot relocations) in an executable. */
2193 if (ELIMINATE_COPY_RELOCS
2194 && !get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
2196 eh = (struct elf_i386_link_hash_entry *) h;
2197 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2199 s = p->sec->output_section;
2200 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2211 /* We must allocate the symbol in our .dynbss section, which will
2212 become part of the .bss section of the executable. There will be
2213 an entry for this symbol in the .dynsym section. The dynamic
2214 object will contain position independent code, so all references
2215 from the dynamic object to this symbol will go through the global
2216 offset table. The dynamic linker will use the .dynsym entry to
2217 determine the address it must put in the global offset table, so
2218 both the dynamic object and the regular object will refer to the
2219 same memory location for the variable. */
2221 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
2222 copy the initial value out of the dynamic object and into the
2223 runtime process image. */
2224 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2226 htab->srelbss->size += sizeof (Elf32_External_Rel);
2232 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2235 /* Allocate space in .plt, .got and associated reloc sections for
2239 elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2241 struct bfd_link_info *info;
2242 struct elf_i386_link_hash_table *htab;
2243 struct elf_i386_link_hash_entry *eh;
2244 struct elf_dyn_relocs *p;
2245 unsigned plt_entry_size;
2247 if (h->root.type == bfd_link_hash_indirect)
2250 eh = (struct elf_i386_link_hash_entry *) h;
2252 info = (struct bfd_link_info *) inf;
2253 htab = elf_i386_hash_table (info);
2257 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
2259 /* We can't use the GOT PLT if pointer equality is needed since
2260 finish_dynamic_symbol won't clear symbol value and the dynamic
2261 linker won't update the GOT slot. We will get into an infinite
2262 loop at run-time. */
2263 if (htab->plt_got != NULL
2264 && h->type != STT_GNU_IFUNC
2265 && !h->pointer_equality_needed
2266 && h->plt.refcount > 0
2267 && h->got.refcount > 0)
2269 /* Don't use the regular PLT if there are both GOT and GOTPLT
2271 h->plt.offset = (bfd_vma) -1;
2273 /* Use the GOT PLT. */
2274 eh->plt_got.refcount = 1;
2277 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2278 here if it is defined and referenced in a non-shared object. */
2279 if (h->type == STT_GNU_IFUNC
2281 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
2284 else if (htab->elf.dynamic_sections_created
2285 && (h->plt.refcount > 0 || eh->plt_got.refcount > 0))
2287 bfd_boolean use_plt_got = eh->plt_got.refcount > 0;
2289 /* Make sure this symbol is output as a dynamic symbol.
2290 Undefined weak syms won't yet be marked as dynamic. */
2291 if (h->dynindx == -1
2292 && !h->forced_local)
2294 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2299 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2301 asection *s = htab->elf.splt;
2302 asection *got_s = htab->plt_got;
2304 /* If this is the first .plt entry, make room for the special
2307 s->size = plt_entry_size;
2310 eh->plt_got.offset = got_s->size;
2312 h->plt.offset = s->size;
2314 /* If this symbol is not defined in a regular file, and we are
2315 not generating a shared library, then set the symbol to this
2316 location in the .plt. This is required to make function
2317 pointers compare as equal between the normal executable and
2318 the shared library. */
2324 /* We need to make a call to the entry of the GOT PLT
2325 instead of regular PLT entry. */
2326 h->root.u.def.section = got_s;
2327 h->root.u.def.value = eh->plt_got.offset;
2331 h->root.u.def.section = s;
2332 h->root.u.def.value = h->plt.offset;
2336 /* Make room for this entry. */
2338 got_s->size += sizeof (elf_i386_got_plt_entry);
2341 s->size += plt_entry_size;
2343 /* We also need to make an entry in the .got.plt section,
2344 which will be placed in the .got section by the linker
2346 htab->elf.sgotplt->size += 4;
2348 /* We also need to make an entry in the .rel.plt section. */
2349 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2350 htab->elf.srelplt->reloc_count++;
2353 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks
2356 /* VxWorks has a second set of relocations for each PLT entry
2357 in executables. They go in a separate relocation section,
2358 which is processed by the kernel loader. */
2360 /* There are two relocations for the initial PLT entry: an
2361 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
2362 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
2364 if (h->plt.offset == plt_entry_size)
2365 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2367 /* There are two extra relocations for each subsequent PLT entry:
2368 an R_386_32 relocation for the GOT entry, and an R_386_32
2369 relocation for the PLT entry. */
2371 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2376 h->plt.offset = (bfd_vma) -1;
2382 h->plt.offset = (bfd_vma) -1;
2386 eh->tlsdesc_got = (bfd_vma) -1;
2388 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
2389 make it a R_386_TLS_LE_32 requiring no TLS entry. */
2390 if (h->got.refcount > 0
2393 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
2394 h->got.offset = (bfd_vma) -1;
2395 else if (h->got.refcount > 0)
2399 int tls_type = elf_i386_hash_entry(h)->tls_type;
2401 /* Make sure this symbol is output as a dynamic symbol.
2402 Undefined weak syms won't yet be marked as dynamic. */
2403 if (h->dynindx == -1
2404 && !h->forced_local)
2406 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2411 if (GOT_TLS_GDESC_P (tls_type))
2413 eh->tlsdesc_got = htab->elf.sgotplt->size
2414 - elf_i386_compute_jump_table_size (htab);
2415 htab->elf.sgotplt->size += 8;
2416 h->got.offset = (bfd_vma) -2;
2418 if (! GOT_TLS_GDESC_P (tls_type)
2419 || GOT_TLS_GD_P (tls_type))
2421 h->got.offset = s->size;
2423 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
2424 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
2427 dyn = htab->elf.dynamic_sections_created;
2428 /* R_386_TLS_IE_32 needs one dynamic relocation,
2429 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
2430 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
2431 need two), R_386_TLS_GD needs one if local symbol and two if
2433 if (tls_type == GOT_TLS_IE_BOTH)
2434 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2435 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2436 || (tls_type & GOT_TLS_IE))
2437 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2438 else if (GOT_TLS_GD_P (tls_type))
2439 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2440 else if (! GOT_TLS_GDESC_P (tls_type)
2441 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2442 || h->root.type != bfd_link_hash_undefweak)
2444 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2445 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2446 if (GOT_TLS_GDESC_P (tls_type))
2447 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2450 h->got.offset = (bfd_vma) -1;
2452 if (eh->dyn_relocs == NULL)
2455 /* In the shared -Bsymbolic case, discard space allocated for
2456 dynamic pc-relative relocs against symbols which turn out to be
2457 defined in regular objects. For the normal shared case, discard
2458 space for pc-relative relocs that have become local due to symbol
2459 visibility changes. */
2463 /* The only reloc that uses pc_count is R_386_PC32, which will
2464 appear on a call or on something like ".long foo - .". We
2465 want calls to protected symbols to resolve directly to the
2466 function rather than going via the plt. If people want
2467 function pointer comparisons to work as expected then they
2468 should avoid writing assembly like ".long foo - .". */
2469 if (SYMBOL_CALLS_LOCAL (info, h))
2471 struct elf_dyn_relocs **pp;
2473 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2475 p->count -= p->pc_count;
2484 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
2486 struct elf_dyn_relocs **pp;
2487 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2489 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2496 /* Also discard relocs on undefined weak syms with non-default
2498 if (eh->dyn_relocs != NULL
2499 && h->root.type == bfd_link_hash_undefweak)
2501 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2502 eh->dyn_relocs = NULL;
2504 /* Make sure undefined weak symbols are output as a dynamic
2506 else if (h->dynindx == -1
2507 && !h->forced_local)
2509 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2514 else if (ELIMINATE_COPY_RELOCS)
2516 /* For the non-shared case, discard space for relocs against
2517 symbols which turn out to need copy relocs or are not
2523 || (htab->elf.dynamic_sections_created
2524 && (h->root.type == bfd_link_hash_undefweak
2525 || h->root.type == bfd_link_hash_undefined))))
2527 /* Make sure this symbol is output as a dynamic symbol.
2528 Undefined weak syms won't yet be marked as dynamic. */
2529 if (h->dynindx == -1
2530 && !h->forced_local)
2532 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2536 /* If that succeeded, we know we'll be keeping all the
2538 if (h->dynindx != -1)
2542 eh->dyn_relocs = NULL;
2547 /* Finally, allocate space. */
2548 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2552 sreloc = elf_section_data (p->sec)->sreloc;
2554 BFD_ASSERT (sreloc != NULL);
2555 sreloc->size += p->count * sizeof (Elf32_External_Rel);
2561 /* Allocate space in .plt, .got and associated reloc sections for
2562 local dynamic relocs. */
2565 elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
2567 struct elf_link_hash_entry *h
2568 = (struct elf_link_hash_entry *) *slot;
2570 if (h->type != STT_GNU_IFUNC
2574 || h->root.type != bfd_link_hash_defined)
2577 return elf_i386_allocate_dynrelocs (h, inf);
2580 /* Find any dynamic relocs that apply to read-only sections. */
2583 elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2585 struct elf_i386_link_hash_entry *eh;
2586 struct elf_dyn_relocs *p;
2588 /* Skip local IFUNC symbols. */
2589 if (h->forced_local && h->type == STT_GNU_IFUNC)
2592 eh = (struct elf_i386_link_hash_entry *) h;
2593 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2595 asection *s = p->sec->output_section;
2597 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2599 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2601 info->flags |= DF_TEXTREL;
2603 if ((info->warn_shared_textrel && info->shared)
2604 || info->error_textrel)
2605 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
2606 p->sec->owner, h->root.root.string,
2609 /* Not an error, just cut short the traversal. */
2617 mov foo@GOT(%reg), %reg
2619 lea foo@GOTOFF(%reg), %reg
2620 with the local symbol, foo. */
2623 elf_i386_convert_mov_to_lea (bfd *abfd, asection *sec,
2624 struct bfd_link_info *link_info)
2626 Elf_Internal_Shdr *symtab_hdr;
2627 Elf_Internal_Rela *internal_relocs;
2628 Elf_Internal_Rela *irel, *irelend;
2630 struct elf_i386_link_hash_table *htab;
2631 bfd_boolean changed_contents;
2632 bfd_boolean changed_relocs;
2633 bfd_signed_vma *local_got_refcounts;
2635 /* Don't even try to convert non-ELF outputs. */
2636 if (!is_elf_hash_table (link_info->hash))
2639 /* Nothing to do if there are no codes, no relocations or no output. */
2640 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
2641 || sec->reloc_count == 0
2642 || bfd_is_abs_section (sec->output_section))
2645 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2647 /* Load the relocations for this section. */
2648 internal_relocs = (_bfd_elf_link_read_relocs
2649 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
2650 link_info->keep_memory));
2651 if (internal_relocs == NULL)
2654 htab = elf_i386_hash_table (link_info);
2655 changed_contents = FALSE;
2656 changed_relocs = FALSE;
2657 local_got_refcounts = elf_local_got_refcounts (abfd);
2659 /* Get the section contents. */
2660 if (elf_section_data (sec)->this_hdr.contents != NULL)
2661 contents = elf_section_data (sec)->this_hdr.contents;
2664 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2668 irelend = internal_relocs + sec->reloc_count;
2669 for (irel = internal_relocs; irel < irelend; irel++)
2671 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
2672 unsigned int r_symndx = ELF32_R_SYM (irel->r_info);
2674 struct elf_link_hash_entry *h;
2676 if (r_type != R_386_GOT32)
2679 /* Get the symbol referred to by the reloc. */
2680 if (r_symndx < symtab_hdr->sh_info)
2682 Elf_Internal_Sym *isym;
2684 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2687 /* STT_GNU_IFUNC must keep R_386_GOT32 relocation. */
2688 if (ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC
2689 && irel->r_offset >= 2
2690 && bfd_get_8 (input_bfd,
2691 contents + irel->r_offset - 2) == 0x8b)
2693 bfd_put_8 (output_bfd, 0x8d,
2694 contents + irel->r_offset - 2);
2695 irel->r_info = ELF32_R_INFO (r_symndx, R_386_GOTOFF);
2696 if (local_got_refcounts != NULL
2697 && local_got_refcounts[r_symndx] > 0)
2698 local_got_refcounts[r_symndx] -= 1;
2699 changed_contents = TRUE;
2700 changed_relocs = TRUE;
2705 indx = r_symndx - symtab_hdr->sh_info;
2706 h = elf_sym_hashes (abfd)[indx];
2707 BFD_ASSERT (h != NULL);
2709 while (h->root.type == bfd_link_hash_indirect
2710 || h->root.type == bfd_link_hash_warning)
2711 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2713 /* STT_GNU_IFUNC must keep R_386_GOT32 relocation. We also avoid
2714 optimizing _DYNAMIC since ld.so may use its link-time address. */
2716 && h->type != STT_GNU_IFUNC
2717 && h != htab->elf.hdynamic
2718 && SYMBOL_REFERENCES_LOCAL (link_info, h)
2719 && irel->r_offset >= 2
2720 && bfd_get_8 (input_bfd,
2721 contents + irel->r_offset - 2) == 0x8b)
2723 bfd_put_8 (output_bfd, 0x8d,
2724 contents + irel->r_offset - 2);
2725 irel->r_info = ELF32_R_INFO (r_symndx, R_386_GOTOFF);
2726 if (h->got.refcount > 0)
2727 h->got.refcount -= 1;
2728 changed_contents = TRUE;
2729 changed_relocs = TRUE;
2733 if (contents != NULL
2734 && elf_section_data (sec)->this_hdr.contents != contents)
2736 if (!changed_contents && !link_info->keep_memory)
2740 /* Cache the section contents for elf_link_input_bfd. */
2741 elf_section_data (sec)->this_hdr.contents = contents;
2745 if (elf_section_data (sec)->relocs != internal_relocs)
2747 if (!changed_relocs)
2748 free (internal_relocs);
2750 elf_section_data (sec)->relocs = internal_relocs;
2756 if (contents != NULL
2757 && elf_section_data (sec)->this_hdr.contents != contents)
2759 if (internal_relocs != NULL
2760 && elf_section_data (sec)->relocs != internal_relocs)
2761 free (internal_relocs);
2765 /* Set the sizes of the dynamic sections. */
2768 elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
2770 struct elf_i386_link_hash_table *htab;
2776 htab = elf_i386_hash_table (info);
2779 dynobj = htab->elf.dynobj;
2783 if (htab->elf.dynamic_sections_created)
2785 /* Set the contents of the .interp section to the interpreter. */
2786 if (info->executable)
2788 s = bfd_get_linker_section (dynobj, ".interp");
2791 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2792 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2796 /* Set up .got offsets for local syms, and space for local dynamic
2798 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2800 bfd_signed_vma *local_got;
2801 bfd_signed_vma *end_local_got;
2802 char *local_tls_type;
2803 bfd_vma *local_tlsdesc_gotent;
2804 bfd_size_type locsymcount;
2805 Elf_Internal_Shdr *symtab_hdr;
2808 if (! is_i386_elf (ibfd))
2811 for (s = ibfd->sections; s != NULL; s = s->next)
2813 struct elf_dyn_relocs *p;
2815 if (!elf_i386_convert_mov_to_lea (ibfd, s, info))
2818 for (p = ((struct elf_dyn_relocs *)
2819 elf_section_data (s)->local_dynrel);
2823 if (!bfd_is_abs_section (p->sec)
2824 && bfd_is_abs_section (p->sec->output_section))
2826 /* Input section has been discarded, either because
2827 it is a copy of a linkonce section or due to
2828 linker script /DISCARD/, so we'll be discarding
2831 else if (get_elf_i386_backend_data (output_bfd)->is_vxworks
2832 && strcmp (p->sec->output_section->name,
2835 /* Relocations in vxworks .tls_vars sections are
2836 handled specially by the loader. */
2838 else if (p->count != 0)
2840 srel = elf_section_data (p->sec)->sreloc;
2841 srel->size += p->count * sizeof (Elf32_External_Rel);
2842 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2843 && (info->flags & DF_TEXTREL) == 0)
2845 info->flags |= DF_TEXTREL;
2846 if ((info->warn_shared_textrel && info->shared)
2847 || info->error_textrel)
2848 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
2849 p->sec->owner, p->sec);
2855 local_got = elf_local_got_refcounts (ibfd);
2859 symtab_hdr = &elf_symtab_hdr (ibfd);
2860 locsymcount = symtab_hdr->sh_info;
2861 end_local_got = local_got + locsymcount;
2862 local_tls_type = elf_i386_local_got_tls_type (ibfd);
2863 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
2865 srel = htab->elf.srelgot;
2866 for (; local_got < end_local_got;
2867 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2869 *local_tlsdesc_gotent = (bfd_vma) -1;
2872 if (GOT_TLS_GDESC_P (*local_tls_type))
2874 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2875 - elf_i386_compute_jump_table_size (htab);
2876 htab->elf.sgotplt->size += 8;
2877 *local_got = (bfd_vma) -2;
2879 if (! GOT_TLS_GDESC_P (*local_tls_type)
2880 || GOT_TLS_GD_P (*local_tls_type))
2882 *local_got = s->size;
2884 if (GOT_TLS_GD_P (*local_tls_type)
2885 || *local_tls_type == GOT_TLS_IE_BOTH)
2889 || GOT_TLS_GD_ANY_P (*local_tls_type)
2890 || (*local_tls_type & GOT_TLS_IE))
2892 if (*local_tls_type == GOT_TLS_IE_BOTH)
2893 srel->size += 2 * sizeof (Elf32_External_Rel);
2894 else if (GOT_TLS_GD_P (*local_tls_type)
2895 || ! GOT_TLS_GDESC_P (*local_tls_type))
2896 srel->size += sizeof (Elf32_External_Rel);
2897 if (GOT_TLS_GDESC_P (*local_tls_type))
2898 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2902 *local_got = (bfd_vma) -1;
2906 if (htab->tls_ldm_got.refcount > 0)
2908 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2910 htab->tls_ldm_got.offset = htab->elf.sgot->size;
2911 htab->elf.sgot->size += 8;
2912 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2915 htab->tls_ldm_got.offset = -1;
2917 /* Allocate global sym .plt and .got entries, and space for global
2918 sym dynamic relocs. */
2919 elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
2921 /* Allocate .plt and .got entries, and space for local symbols. */
2922 htab_traverse (htab->loc_hash_table,
2923 elf_i386_allocate_local_dynrelocs,
2926 /* For every jump slot reserved in the sgotplt, reloc_count is
2927 incremented. However, when we reserve space for TLS descriptors,
2928 it's not incremented, so in order to compute the space reserved
2929 for them, it suffices to multiply the reloc count by the jump
2932 PR ld/13302: We start next_irelative_index at the end of .rela.plt
2933 so that R_386_IRELATIVE entries come last. */
2934 if (htab->elf.srelplt)
2936 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
2937 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
2938 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2940 else if (htab->elf.irelplt)
2941 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
2944 if (htab->elf.sgotplt)
2946 /* Don't allocate .got.plt section if there are no GOT nor PLT
2947 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
2948 if ((htab->elf.hgot == NULL
2949 || !htab->elf.hgot->ref_regular_nonweak)
2950 && (htab->elf.sgotplt->size
2951 == get_elf_backend_data (output_bfd)->got_header_size)
2952 && (htab->elf.splt == NULL
2953 || htab->elf.splt->size == 0)
2954 && (htab->elf.sgot == NULL
2955 || htab->elf.sgot->size == 0)
2956 && (htab->elf.iplt == NULL
2957 || htab->elf.iplt->size == 0)
2958 && (htab->elf.igotplt == NULL
2959 || htab->elf.igotplt->size == 0))
2960 htab->elf.sgotplt->size = 0;
2964 if (htab->plt_eh_frame != NULL
2965 && htab->elf.splt != NULL
2966 && htab->elf.splt->size != 0
2967 && !bfd_is_abs_section (htab->elf.splt->output_section)
2968 && _bfd_elf_eh_frame_present (info))
2969 htab->plt_eh_frame->size = sizeof (elf_i386_eh_frame_plt);
2971 /* We now have determined the sizes of the various dynamic sections.
2972 Allocate memory for them. */
2974 for (s = dynobj->sections; s != NULL; s = s->next)
2976 bfd_boolean strip_section = TRUE;
2978 if ((s->flags & SEC_LINKER_CREATED) == 0)
2981 if (s == htab->elf.splt
2982 || s == htab->elf.sgot)
2984 /* Strip this section if we don't need it; see the
2986 /* We'd like to strip these sections if they aren't needed, but if
2987 we've exported dynamic symbols from them we must leave them.
2988 It's too late to tell BFD to get rid of the symbols. */
2990 if (htab->elf.hplt != NULL)
2991 strip_section = FALSE;
2993 else if (s == htab->elf.sgotplt
2994 || s == htab->elf.iplt
2995 || s == htab->elf.igotplt
2996 || s == htab->plt_got
2997 || s == htab->plt_eh_frame
2998 || s == htab->sdynbss)
3000 /* Strip these too. */
3002 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
3005 && s != htab->elf.srelplt
3006 && s != htab->srelplt2)
3009 /* We use the reloc_count field as a counter if we need
3010 to copy relocs into the output file. */
3015 /* It's not one of our sections, so don't allocate space. */
3021 /* If we don't need this section, strip it from the
3022 output file. This is mostly to handle .rel.bss and
3023 .rel.plt. We must create both sections in
3024 create_dynamic_sections, because they must be created
3025 before the linker maps input sections to output
3026 sections. The linker does that before
3027 adjust_dynamic_symbol is called, and it is that
3028 function which decides whether anything needs to go
3029 into these sections. */
3031 s->flags |= SEC_EXCLUDE;
3035 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3038 /* Allocate memory for the section contents. We use bfd_zalloc
3039 here in case unused entries are not reclaimed before the
3040 section's contents are written out. This should not happen,
3041 but this way if it does, we get a R_386_NONE reloc instead
3043 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
3044 if (s->contents == NULL)
3048 if (htab->plt_eh_frame != NULL
3049 && htab->plt_eh_frame->contents != NULL)
3051 memcpy (htab->plt_eh_frame->contents, elf_i386_eh_frame_plt,
3052 sizeof (elf_i386_eh_frame_plt));
3053 bfd_put_32 (dynobj, htab->elf.splt->size,
3054 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3057 if (htab->elf.dynamic_sections_created)
3059 /* Add some entries to the .dynamic section. We fill in the
3060 values later, in elf_i386_finish_dynamic_sections, but we
3061 must add the entries now so that we get the correct size for
3062 the .dynamic section. The DT_DEBUG entry is filled in by the
3063 dynamic linker and used by the debugger. */
3064 #define add_dynamic_entry(TAG, VAL) \
3065 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3067 if (info->executable)
3069 if (!add_dynamic_entry (DT_DEBUG, 0))
3073 if (htab->elf.splt->size != 0)
3075 if (!add_dynamic_entry (DT_PLTGOT, 0)
3076 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3077 || !add_dynamic_entry (DT_PLTREL, DT_REL)
3078 || !add_dynamic_entry (DT_JMPREL, 0))
3084 if (!add_dynamic_entry (DT_REL, 0)
3085 || !add_dynamic_entry (DT_RELSZ, 0)
3086 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
3089 /* If any dynamic relocs apply to a read-only section,
3090 then we need a DT_TEXTREL entry. */
3091 if ((info->flags & DF_TEXTREL) == 0)
3092 elf_link_hash_traverse (&htab->elf,
3093 elf_i386_readonly_dynrelocs, info);
3095 if ((info->flags & DF_TEXTREL) != 0)
3097 if (!add_dynamic_entry (DT_TEXTREL, 0))
3101 if (get_elf_i386_backend_data (output_bfd)->is_vxworks
3102 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3105 #undef add_dynamic_entry
3111 elf_i386_always_size_sections (bfd *output_bfd,
3112 struct bfd_link_info *info)
3114 asection *tls_sec = elf_hash_table (info)->tls_sec;
3118 struct elf_link_hash_entry *tlsbase;
3120 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3121 "_TLS_MODULE_BASE_",
3122 FALSE, FALSE, FALSE);
3124 if (tlsbase && tlsbase->type == STT_TLS)
3126 struct elf_i386_link_hash_table *htab;
3127 struct bfd_link_hash_entry *bh = NULL;
3128 const struct elf_backend_data *bed
3129 = get_elf_backend_data (output_bfd);
3131 htab = elf_i386_hash_table (info);
3135 if (!(_bfd_generic_link_add_one_symbol
3136 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3137 tls_sec, 0, NULL, FALSE,
3138 bed->collect, &bh)))
3141 htab->tls_module_base = bh;
3143 tlsbase = (struct elf_link_hash_entry *)bh;
3144 tlsbase->def_regular = 1;
3145 tlsbase->other = STV_HIDDEN;
3146 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
3153 /* Set the correct type for an x86 ELF section. We do this by the
3154 section name, which is a hack, but ought to work. */
3157 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
3158 Elf_Internal_Shdr *hdr,
3163 name = bfd_get_section_name (abfd, sec);
3165 /* This is an ugly, but unfortunately necessary hack that is
3166 needed when producing EFI binaries on x86. It tells
3167 elf.c:elf_fake_sections() not to consider ".reloc" as a section
3168 containing ELF relocation info. We need this hack in order to
3169 be able to generate ELF binaries that can be translated into
3170 EFI applications (which are essentially COFF objects). Those
3171 files contain a COFF ".reloc" section inside an ELFNN object,
3172 which would normally cause BFD to segfault because it would
3173 attempt to interpret this section as containing relocation
3174 entries for section "oc". With this hack enabled, ".reloc"
3175 will be treated as a normal data section, which will avoid the
3176 segfault. However, you won't be able to create an ELFNN binary
3177 with a section named "oc" that needs relocations, but that's
3178 the kind of ugly side-effects you get when detecting section
3179 types based on their names... In practice, this limitation is
3180 unlikely to bite. */
3181 if (strcmp (name, ".reloc") == 0)
3182 hdr->sh_type = SHT_PROGBITS;
3187 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
3188 executables. Rather than setting it to the beginning of the TLS
3189 section, we have to set it to the end. This function may be called
3190 multiple times, it is idempotent. */
3193 elf_i386_set_tls_module_base (struct bfd_link_info *info)
3195 struct elf_i386_link_hash_table *htab;
3196 struct bfd_link_hash_entry *base;
3198 if (!info->executable)
3201 htab = elf_i386_hash_table (info);
3205 base = htab->tls_module_base;
3209 base->u.def.value = htab->elf.tls_size;
3212 /* Return the base VMA address which should be subtracted from real addresses
3213 when resolving @dtpoff relocation.
3214 This is PT_TLS segment p_vaddr. */
3217 elf_i386_dtpoff_base (struct bfd_link_info *info)
3219 /* If tls_sec is NULL, we should have signalled an error already. */
3220 if (elf_hash_table (info)->tls_sec == NULL)
3222 return elf_hash_table (info)->tls_sec->vma;
3225 /* Return the relocation value for @tpoff relocation
3226 if STT_TLS virtual address is ADDRESS. */
3229 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
3231 struct elf_link_hash_table *htab = elf_hash_table (info);
3232 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3233 bfd_vma static_tls_size;
3235 /* If tls_sec is NULL, we should have signalled an error already. */
3236 if (htab->tls_sec == NULL)
3239 /* Consider special static TLS alignment requirements. */
3240 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3241 return static_tls_size + htab->tls_sec->vma - address;
3244 /* Relocate an i386 ELF section. */
3247 elf_i386_relocate_section (bfd *output_bfd,
3248 struct bfd_link_info *info,
3250 asection *input_section,
3252 Elf_Internal_Rela *relocs,
3253 Elf_Internal_Sym *local_syms,
3254 asection **local_sections)
3256 struct elf_i386_link_hash_table *htab;
3257 Elf_Internal_Shdr *symtab_hdr;
3258 struct elf_link_hash_entry **sym_hashes;
3259 bfd_vma *local_got_offsets;
3260 bfd_vma *local_tlsdesc_gotents;
3261 Elf_Internal_Rela *rel;
3262 Elf_Internal_Rela *relend;
3263 bfd_boolean is_vxworks_tls;
3264 unsigned plt_entry_size;
3266 BFD_ASSERT (is_i386_elf (input_bfd));
3268 htab = elf_i386_hash_table (info);
3271 symtab_hdr = &elf_symtab_hdr (input_bfd);
3272 sym_hashes = elf_sym_hashes (input_bfd);
3273 local_got_offsets = elf_local_got_offsets (input_bfd);
3274 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
3275 /* We have to handle relocations in vxworks .tls_vars sections
3276 specially, because the dynamic loader is 'weird'. */
3277 is_vxworks_tls = (get_elf_i386_backend_data (output_bfd)->is_vxworks
3279 && !strcmp (input_section->output_section->name,
3282 elf_i386_set_tls_module_base (info);
3284 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
3287 relend = relocs + input_section->reloc_count;
3288 for (; rel < relend; rel++)
3290 unsigned int r_type;
3291 reloc_howto_type *howto;
3292 unsigned long r_symndx;
3293 struct elf_link_hash_entry *h;
3294 struct elf_i386_link_hash_entry *eh;
3295 Elf_Internal_Sym *sym;
3297 bfd_vma off, offplt, plt_offset;
3299 bfd_boolean unresolved_reloc;
3300 bfd_reloc_status_type r;
3304 asection *resolved_plt;
3306 r_type = ELF32_R_TYPE (rel->r_info);
3307 if (r_type == R_386_GNU_VTINHERIT
3308 || r_type == R_386_GNU_VTENTRY)
3311 if ((indx = r_type) >= R_386_standard
3312 && ((indx = r_type - R_386_ext_offset) - R_386_standard
3313 >= R_386_ext - R_386_standard)
3314 && ((indx = r_type - R_386_tls_offset) - R_386_ext
3315 >= R_386_irelative - R_386_ext))
3317 (*_bfd_error_handler)
3318 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3319 input_bfd, input_section, r_type);
3320 bfd_set_error (bfd_error_bad_value);
3323 howto = elf_howto_table + indx;
3325 r_symndx = ELF32_R_SYM (rel->r_info);
3329 unresolved_reloc = FALSE;
3330 if (r_symndx < symtab_hdr->sh_info)
3332 sym = local_syms + r_symndx;
3333 sec = local_sections[r_symndx];
3334 relocation = (sec->output_section->vma
3335 + sec->output_offset
3337 st_size = sym->st_size;
3339 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
3340 && ((sec->flags & SEC_MERGE) != 0
3341 || (info->relocatable
3342 && sec->output_offset != 0)))
3345 bfd_byte *where = contents + rel->r_offset;
3347 switch (howto->size)
3350 addend = bfd_get_8 (input_bfd, where);
3351 if (howto->pc_relative)
3353 addend = (addend ^ 0x80) - 0x80;
3358 addend = bfd_get_16 (input_bfd, where);
3359 if (howto->pc_relative)
3361 addend = (addend ^ 0x8000) - 0x8000;
3366 addend = bfd_get_32 (input_bfd, where);
3367 if (howto->pc_relative)
3369 addend = (addend ^ 0x80000000) - 0x80000000;
3377 if (info->relocatable)
3378 addend += sec->output_offset;
3381 asection *msec = sec;
3382 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
3384 addend -= relocation;
3385 addend += msec->output_section->vma + msec->output_offset;
3388 switch (howto->size)
3391 /* FIXME: overflow checks. */
3392 if (howto->pc_relative)
3394 bfd_put_8 (input_bfd, addend, where);
3397 if (howto->pc_relative)
3399 bfd_put_16 (input_bfd, addend, where);
3402 if (howto->pc_relative)
3404 bfd_put_32 (input_bfd, addend, where);
3408 else if (!info->relocatable
3409 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3411 /* Relocate against local STT_GNU_IFUNC symbol. */
3412 h = elf_i386_get_local_sym_hash (htab, input_bfd, rel,
3417 /* Set STT_GNU_IFUNC symbol value. */
3418 h->root.u.def.value = sym->st_value;
3419 h->root.u.def.section = sec;
3424 bfd_boolean warned ATTRIBUTE_UNUSED;
3425 bfd_boolean ignored ATTRIBUTE_UNUSED;
3427 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3428 r_symndx, symtab_hdr, sym_hashes,
3430 unresolved_reloc, warned, ignored);
3434 if (sec != NULL && discarded_section (sec))
3435 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3436 rel, 1, relend, howto, 0, contents);
3438 if (info->relocatable)
3441 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3442 it here if it is defined in a non-shared object. */
3444 && h->type == STT_GNU_IFUNC
3447 asection *plt, *gotplt, *base_got;
3451 if ((input_section->flags & SEC_ALLOC) == 0
3452 || h->plt.offset == (bfd_vma) -1)
3455 /* STT_GNU_IFUNC symbol must go through PLT. */
3456 if (htab->elf.splt != NULL)
3458 plt = htab->elf.splt;
3459 gotplt = htab->elf.sgotplt;
3463 plt = htab->elf.iplt;
3464 gotplt = htab->elf.igotplt;
3467 relocation = (plt->output_section->vma
3468 + plt->output_offset + h->plt.offset);
3473 if (h->root.root.string)
3474 name = h->root.root.string;
3476 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3478 (*_bfd_error_handler)
3479 (_("%B: relocation %s against STT_GNU_IFUNC "
3480 "symbol `%s' isn't handled by %s"), input_bfd,
3481 elf_howto_table[r_type].name,
3482 name, __FUNCTION__);
3483 bfd_set_error (bfd_error_bad_value);
3487 /* Generate dynamic relcoation only when there is a
3488 non-GOT reference in a shared object. */
3489 if (info->shared && h->non_got_ref)
3491 Elf_Internal_Rela outrel;
3495 /* Need a dynamic relocation to get the real function
3497 offset = _bfd_elf_section_offset (output_bfd,
3501 if (offset == (bfd_vma) -1
3502 || offset == (bfd_vma) -2)
3505 outrel.r_offset = (input_section->output_section->vma
3506 + input_section->output_offset
3509 if (h->dynindx == -1
3511 || info->executable)
3513 /* This symbol is resolved locally. */
3514 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3515 bfd_put_32 (output_bfd,
3516 (h->root.u.def.value
3517 + h->root.u.def.section->output_section->vma
3518 + h->root.u.def.section->output_offset),
3522 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3524 sreloc = htab->elf.irelifunc;
3525 elf_append_rel (output_bfd, sreloc, &outrel);
3527 /* If this reloc is against an external symbol, we
3528 do not want to fiddle with the addend. Otherwise,
3529 we need to include the symbol value so that it
3530 becomes an addend for the dynamic reloc. For an
3531 internal symbol, we have updated addend. */
3540 base_got = htab->elf.sgot;
3541 off = h->got.offset;
3543 if (base_got == NULL)
3546 if (off == (bfd_vma) -1)
3548 /* We can't use h->got.offset here to save state, or
3549 even just remember the offset, as finish_dynamic_symbol
3550 would use that as offset into .got. */
3552 if (htab->elf.splt != NULL)
3554 plt_index = h->plt.offset / plt_entry_size - 1;
3555 off = (plt_index + 3) * 4;
3556 base_got = htab->elf.sgotplt;
3560 plt_index = h->plt.offset / plt_entry_size;
3561 off = plt_index * 4;
3562 base_got = htab->elf.igotplt;
3565 if (h->dynindx == -1
3569 /* This references the local defitionion. We must
3570 initialize this entry in the global offset table.
3571 Since the offset must always be a multiple of 8,
3572 we use the least significant bit to record
3573 whether we have initialized it already.
3575 When doing a dynamic link, we create a .rela.got
3576 relocation entry to initialize the value. This
3577 is done in the finish_dynamic_symbol routine. */
3582 bfd_put_32 (output_bfd, relocation,
3583 base_got->contents + off);
3590 /* Adjust for static executables. */
3591 if (htab->elf.splt == NULL)
3592 relocation += gotplt->output_offset;
3596 relocation = (base_got->output_section->vma
3597 + base_got->output_offset + off
3598 - gotplt->output_section->vma
3599 - gotplt->output_offset);
3600 /* Adjust for static executables. */
3601 if (htab->elf.splt == NULL)
3602 relocation += gotplt->output_offset;
3608 relocation -= (gotplt->output_section->vma
3609 + gotplt->output_offset);
3617 /* Relocation is to the entry for this symbol in the global
3619 if (htab->elf.sgot == NULL)
3626 off = h->got.offset;
3627 dyn = htab->elf.dynamic_sections_created;
3628 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3630 && SYMBOL_REFERENCES_LOCAL (info, h))
3631 || (ELF_ST_VISIBILITY (h->other)
3632 && h->root.type == bfd_link_hash_undefweak))
3634 /* This is actually a static link, or it is a
3635 -Bsymbolic link and the symbol is defined
3636 locally, or the symbol was forced to be local
3637 because of a version file. We must initialize
3638 this entry in the global offset table. Since the
3639 offset must always be a multiple of 4, we use the
3640 least significant bit to record whether we have
3641 initialized it already.
3643 When doing a dynamic link, we create a .rel.got
3644 relocation entry to initialize the value. This
3645 is done in the finish_dynamic_symbol routine. */
3650 bfd_put_32 (output_bfd, relocation,
3651 htab->elf.sgot->contents + off);
3656 unresolved_reloc = FALSE;
3660 if (local_got_offsets == NULL)
3663 off = local_got_offsets[r_symndx];
3665 /* The offset must always be a multiple of 4. We use
3666 the least significant bit to record whether we have
3667 already generated the necessary reloc. */
3672 bfd_put_32 (output_bfd, relocation,
3673 htab->elf.sgot->contents + off);
3678 Elf_Internal_Rela outrel;
3680 s = htab->elf.srelgot;
3684 outrel.r_offset = (htab->elf.sgot->output_section->vma
3685 + htab->elf.sgot->output_offset
3687 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3688 elf_append_rel (output_bfd, s, &outrel);
3691 local_got_offsets[r_symndx] |= 1;
3695 if (off >= (bfd_vma) -2)
3698 relocation = htab->elf.sgot->output_section->vma
3699 + htab->elf.sgot->output_offset + off
3700 - htab->elf.sgotplt->output_section->vma
3701 - htab->elf.sgotplt->output_offset;
3705 /* Relocation is relative to the start of the global offset
3708 /* Check to make sure it isn't a protected function symbol
3709 for shared library since it may not be local when used
3710 as function address. We also need to make sure that a
3711 symbol is defined locally. */
3712 if (info->shared && h)
3714 if (!h->def_regular)
3718 switch (ELF_ST_VISIBILITY (h->other))
3721 v = _("hidden symbol");
3724 v = _("internal symbol");
3727 v = _("protected symbol");
3734 (*_bfd_error_handler)
3735 (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
3736 input_bfd, v, h->root.root.string);
3737 bfd_set_error (bfd_error_bad_value);
3740 else if (!info->executable
3741 && !SYMBOLIC_BIND (info, h)
3742 && h->type == STT_FUNC
3743 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3745 (*_bfd_error_handler)
3746 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
3747 input_bfd, h->root.root.string);
3748 bfd_set_error (bfd_error_bad_value);
3753 /* Note that sgot is not involved in this
3754 calculation. We always want the start of .got.plt. If we
3755 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3756 permitted by the ABI, we might have to change this
3758 relocation -= htab->elf.sgotplt->output_section->vma
3759 + htab->elf.sgotplt->output_offset;
3763 /* Use global offset table as symbol value. */
3764 relocation = htab->elf.sgotplt->output_section->vma
3765 + htab->elf.sgotplt->output_offset;
3766 unresolved_reloc = FALSE;
3770 /* Relocation is to the entry for this symbol in the
3771 procedure linkage table. */
3773 /* Resolve a PLT32 reloc against a local symbol directly,
3774 without using the procedure linkage table. */
3778 eh = (struct elf_i386_link_hash_entry *) h;
3779 if ((h->plt.offset == (bfd_vma) -1
3780 && eh->plt_got.offset == (bfd_vma) -1)
3781 || htab->elf.splt == NULL)
3783 /* We didn't make a PLT entry for this symbol. This
3784 happens when statically linking PIC code, or when
3785 using -Bsymbolic. */
3789 if (h->plt.offset != (bfd_vma) -1)
3791 resolved_plt = htab->elf.splt;
3792 plt_offset = h->plt.offset;
3796 resolved_plt = htab->plt_got;
3797 plt_offset = eh->plt_got.offset;
3800 relocation = (resolved_plt->output_section->vma
3801 + resolved_plt->output_offset
3803 unresolved_reloc = FALSE;
3807 /* Set to symbol size. */
3808 relocation = st_size;
3813 if ((input_section->flags & SEC_ALLOC) == 0
3819 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3820 || h->root.type != bfd_link_hash_undefweak)
3821 && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
3822 || !SYMBOL_CALLS_LOCAL (info, h)))
3823 || (ELIMINATE_COPY_RELOCS
3830 || h->root.type == bfd_link_hash_undefweak
3831 || h->root.type == bfd_link_hash_undefined)))
3833 Elf_Internal_Rela outrel;
3834 bfd_boolean skip, relocate;
3837 /* When generating a shared object, these relocations
3838 are copied into the output file to be resolved at run
3845 _bfd_elf_section_offset (output_bfd, info, input_section,
3847 if (outrel.r_offset == (bfd_vma) -1)
3849 else if (outrel.r_offset == (bfd_vma) -2)
3850 skip = TRUE, relocate = TRUE;
3851 outrel.r_offset += (input_section->output_section->vma
3852 + input_section->output_offset);
3855 memset (&outrel, 0, sizeof outrel);
3858 && (r_type == R_386_PC32
3860 || !SYMBOLIC_BIND (info, h)
3861 || !h->def_regular))
3862 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3865 /* This symbol is local, or marked to become local. */
3867 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3870 sreloc = elf_section_data (input_section)->sreloc;
3872 if (sreloc == NULL || sreloc->contents == NULL)
3874 r = bfd_reloc_notsupported;
3875 goto check_relocation_error;
3878 elf_append_rel (output_bfd, sreloc, &outrel);
3880 /* If this reloc is against an external symbol, we do
3881 not want to fiddle with the addend. Otherwise, we
3882 need to include the symbol value so that it becomes
3883 an addend for the dynamic reloc. */
3890 if (!info->executable)
3892 Elf_Internal_Rela outrel;
3895 outrel.r_offset = rel->r_offset
3896 + input_section->output_section->vma
3897 + input_section->output_offset;
3898 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3899 sreloc = elf_section_data (input_section)->sreloc;
3902 elf_append_rel (output_bfd, sreloc, &outrel);
3907 case R_386_TLS_GOTDESC:
3908 case R_386_TLS_DESC_CALL:
3909 case R_386_TLS_IE_32:
3910 case R_386_TLS_GOTIE:
3911 tls_type = GOT_UNKNOWN;
3912 if (h == NULL && local_got_offsets)
3913 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
3915 tls_type = elf_i386_hash_entry(h)->tls_type;
3916 if (tls_type == GOT_TLS_IE)
3917 tls_type = GOT_TLS_IE_NEG;
3919 if (! elf_i386_tls_transition (info, input_bfd,
3920 input_section, contents,
3921 symtab_hdr, sym_hashes,
3922 &r_type, tls_type, rel,
3923 relend, h, r_symndx))
3926 if (r_type == R_386_TLS_LE_32)
3928 BFD_ASSERT (! unresolved_reloc);
3929 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3934 /* GD->LE transition. */
3935 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3938 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3940 movl %gs:0, %eax; subl $foo@tpoff, %eax
3941 (6 byte form of subl). */
3942 memcpy (contents + rel->r_offset - 3,
3943 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3944 roff = rel->r_offset + 5;
3948 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3950 movl %gs:0, %eax; subl $foo@tpoff, %eax
3951 (6 byte form of subl). */
3952 memcpy (contents + rel->r_offset - 2,
3953 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3954 roff = rel->r_offset + 6;
3956 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3958 /* Skip R_386_PC32/R_386_PLT32. */
3962 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3964 /* GDesc -> LE transition.
3965 It's originally something like:
3966 leal x@tlsdesc(%ebx), %eax
3970 Registers other than %eax may be set up here. */
3975 roff = rel->r_offset;
3976 val = bfd_get_8 (input_bfd, contents + roff - 1);
3978 /* Now modify the instruction as appropriate. */
3979 /* aoliva FIXME: remove the above and xor the byte
3981 bfd_put_8 (output_bfd, val ^ 0x86,
3982 contents + roff - 1);
3983 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3987 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3989 /* GDesc -> LE transition.
3997 roff = rel->r_offset;
3998 bfd_put_8 (output_bfd, 0x66, contents + roff);
3999 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4002 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
4006 /* IE->LE transition:
4007 Originally it can be one of:
4015 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4018 /* movl foo, %eax. */
4019 bfd_put_8 (output_bfd, 0xb8,
4020 contents + rel->r_offset - 1);
4026 type = bfd_get_8 (input_bfd,
4027 contents + rel->r_offset - 2);
4032 bfd_put_8 (output_bfd, 0xc7,
4033 contents + rel->r_offset - 2);
4034 bfd_put_8 (output_bfd,
4035 0xc0 | ((val >> 3) & 7),
4036 contents + rel->r_offset - 1);
4040 bfd_put_8 (output_bfd, 0x81,
4041 contents + rel->r_offset - 2);
4042 bfd_put_8 (output_bfd,
4043 0xc0 | ((val >> 3) & 7),
4044 contents + rel->r_offset - 1);
4051 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4052 contents + rel->r_offset);
4057 unsigned int val, type;
4059 /* {IE_32,GOTIE}->LE transition:
4060 Originally it can be one of:
4061 subl foo(%reg1), %reg2
4062 movl foo(%reg1), %reg2
4063 addl foo(%reg1), %reg2
4066 movl $foo, %reg2 (6 byte form)
4067 addl $foo, %reg2. */
4068 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4069 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4073 bfd_put_8 (output_bfd, 0xc7,
4074 contents + rel->r_offset - 2);
4075 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4076 contents + rel->r_offset - 1);
4078 else if (type == 0x2b)
4081 bfd_put_8 (output_bfd, 0x81,
4082 contents + rel->r_offset - 2);
4083 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
4084 contents + rel->r_offset - 1);
4086 else if (type == 0x03)
4089 bfd_put_8 (output_bfd, 0x81,
4090 contents + rel->r_offset - 2);
4091 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4092 contents + rel->r_offset - 1);
4096 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
4097 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4098 contents + rel->r_offset);
4100 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
4101 contents + rel->r_offset);
4106 if (htab->elf.sgot == NULL)
4111 off = h->got.offset;
4112 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
4116 if (local_got_offsets == NULL)
4119 off = local_got_offsets[r_symndx];
4120 offplt = local_tlsdesc_gotents[r_symndx];
4127 Elf_Internal_Rela outrel;
4131 if (htab->elf.srelgot == NULL)
4134 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4136 if (GOT_TLS_GDESC_P (tls_type))
4139 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
4140 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
4141 <= htab->elf.sgotplt->size);
4142 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4143 + htab->elf.sgotplt->output_offset
4145 + htab->sgotplt_jump_table_size);
4146 sreloc = htab->elf.srelplt;
4147 loc = sreloc->contents;
4148 loc += (htab->next_tls_desc_index++
4149 * sizeof (Elf32_External_Rel));
4150 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
4151 <= sreloc->contents + sreloc->size);
4152 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4155 BFD_ASSERT (! unresolved_reloc);
4156 bfd_put_32 (output_bfd,
4157 relocation - elf_i386_dtpoff_base (info),
4158 htab->elf.sgotplt->contents + offplt
4159 + htab->sgotplt_jump_table_size + 4);
4163 bfd_put_32 (output_bfd, 0,
4164 htab->elf.sgotplt->contents + offplt
4165 + htab->sgotplt_jump_table_size + 4);
4169 sreloc = htab->elf.srelgot;
4171 outrel.r_offset = (htab->elf.sgot->output_section->vma
4172 + htab->elf.sgot->output_offset + off);
4174 if (GOT_TLS_GD_P (tls_type))
4175 dr_type = R_386_TLS_DTPMOD32;
4176 else if (GOT_TLS_GDESC_P (tls_type))
4178 else if (tls_type == GOT_TLS_IE_POS)
4179 dr_type = R_386_TLS_TPOFF;
4181 dr_type = R_386_TLS_TPOFF32;
4183 if (dr_type == R_386_TLS_TPOFF && indx == 0)
4184 bfd_put_32 (output_bfd,
4185 relocation - elf_i386_dtpoff_base (info),
4186 htab->elf.sgot->contents + off);
4187 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
4188 bfd_put_32 (output_bfd,
4189 elf_i386_dtpoff_base (info) - relocation,
4190 htab->elf.sgot->contents + off);
4191 else if (dr_type != R_386_TLS_DESC)
4192 bfd_put_32 (output_bfd, 0,
4193 htab->elf.sgot->contents + off);
4194 outrel.r_info = ELF32_R_INFO (indx, dr_type);
4196 elf_append_rel (output_bfd, sreloc, &outrel);
4198 if (GOT_TLS_GD_P (tls_type))
4202 BFD_ASSERT (! unresolved_reloc);
4203 bfd_put_32 (output_bfd,
4204 relocation - elf_i386_dtpoff_base (info),
4205 htab->elf.sgot->contents + off + 4);
4209 bfd_put_32 (output_bfd, 0,
4210 htab->elf.sgot->contents + off + 4);
4211 outrel.r_info = ELF32_R_INFO (indx,
4212 R_386_TLS_DTPOFF32);
4213 outrel.r_offset += 4;
4214 elf_append_rel (output_bfd, sreloc, &outrel);
4217 else if (tls_type == GOT_TLS_IE_BOTH)
4219 bfd_put_32 (output_bfd,
4221 ? relocation - elf_i386_dtpoff_base (info)
4223 htab->elf.sgot->contents + off + 4);
4224 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
4225 outrel.r_offset += 4;
4226 elf_append_rel (output_bfd, sreloc, &outrel);
4233 local_got_offsets[r_symndx] |= 1;
4236 if (off >= (bfd_vma) -2
4237 && ! GOT_TLS_GDESC_P (tls_type))
4239 if (r_type == R_386_TLS_GOTDESC
4240 || r_type == R_386_TLS_DESC_CALL)
4242 relocation = htab->sgotplt_jump_table_size + offplt;
4243 unresolved_reloc = FALSE;
4245 else if (r_type == ELF32_R_TYPE (rel->r_info))
4247 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
4248 + htab->elf.sgotplt->output_offset;
4249 relocation = htab->elf.sgot->output_section->vma
4250 + htab->elf.sgot->output_offset + off - g_o_t;
4251 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
4252 && tls_type == GOT_TLS_IE_BOTH)
4254 if (r_type == R_386_TLS_IE)
4255 relocation += g_o_t;
4256 unresolved_reloc = FALSE;
4258 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
4260 unsigned int val, type;
4263 /* GD->IE transition. */
4264 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4265 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4268 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
4270 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
4272 roff = rel->r_offset - 3;
4276 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
4278 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
4279 roff = rel->r_offset - 2;
4281 memcpy (contents + roff,
4282 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
4283 contents[roff + 7] = 0x80 | (val & 7);
4284 /* If foo is used only with foo@gotntpoff(%reg) and
4285 foo@indntpoff, but not with foo@gottpoff(%reg), change
4286 subl $foo@gottpoff(%reg), %eax
4288 addl $foo@gotntpoff(%reg), %eax. */
4289 if (tls_type == GOT_TLS_IE_POS)
4290 contents[roff + 6] = 0x03;
4291 bfd_put_32 (output_bfd,
4292 htab->elf.sgot->output_section->vma
4293 + htab->elf.sgot->output_offset + off
4294 - htab->elf.sgotplt->output_section->vma
4295 - htab->elf.sgotplt->output_offset,
4296 contents + roff + 8);
4297 /* Skip R_386_PLT32. */
4301 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
4303 /* GDesc -> IE transition.
4304 It's originally something like:
4305 leal x@tlsdesc(%ebx), %eax
4308 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
4310 movl x@gottpoff(%ebx), %eax # before negl %eax
4312 Registers other than %eax may be set up here. */
4316 /* First, make sure it's a leal adding ebx to a 32-bit
4317 offset into any register, although it's probably
4318 almost always going to be eax. */
4319 roff = rel->r_offset;
4321 /* Now modify the instruction as appropriate. */
4322 /* To turn a leal into a movl in the form we use it, it
4323 suffices to change the first byte from 0x8d to 0x8b.
4324 aoliva FIXME: should we decide to keep the leal, all
4325 we have to do is remove the statement below, and
4326 adjust the relaxation of R_386_TLS_DESC_CALL. */
4327 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
4329 if (tls_type == GOT_TLS_IE_BOTH)
4332 bfd_put_32 (output_bfd,
4333 htab->elf.sgot->output_section->vma
4334 + htab->elf.sgot->output_offset + off
4335 - htab->elf.sgotplt->output_section->vma
4336 - htab->elf.sgotplt->output_offset,
4340 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
4342 /* GDesc -> IE transition.
4350 depending on how we transformed the TLS_GOTDESC above.
4355 roff = rel->r_offset;
4357 /* Now modify the instruction as appropriate. */
4358 if (tls_type != GOT_TLS_IE_NEG)
4361 bfd_put_8 (output_bfd, 0x66, contents + roff);
4362 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4367 bfd_put_8 (output_bfd, 0xf7, contents + roff);
4368 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
4378 if (! elf_i386_tls_transition (info, input_bfd,
4379 input_section, contents,
4380 symtab_hdr, sym_hashes,
4381 &r_type, GOT_UNKNOWN, rel,
4382 relend, h, r_symndx))
4385 if (r_type != R_386_TLS_LDM)
4387 /* LD->LE transition:
4388 leal foo(%reg), %eax; call ___tls_get_addr.
4390 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
4391 BFD_ASSERT (r_type == R_386_TLS_LE_32);
4392 memcpy (contents + rel->r_offset - 2,
4393 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
4394 /* Skip R_386_PC32/R_386_PLT32. */
4399 if (htab->elf.sgot == NULL)
4402 off = htab->tls_ldm_got.offset;
4407 Elf_Internal_Rela outrel;
4409 if (htab->elf.srelgot == NULL)
4412 outrel.r_offset = (htab->elf.sgot->output_section->vma
4413 + htab->elf.sgot->output_offset + off);
4415 bfd_put_32 (output_bfd, 0,
4416 htab->elf.sgot->contents + off);
4417 bfd_put_32 (output_bfd, 0,
4418 htab->elf.sgot->contents + off + 4);
4419 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
4420 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
4421 htab->tls_ldm_got.offset |= 1;
4423 relocation = htab->elf.sgot->output_section->vma
4424 + htab->elf.sgot->output_offset + off
4425 - htab->elf.sgotplt->output_section->vma
4426 - htab->elf.sgotplt->output_offset;
4427 unresolved_reloc = FALSE;
4430 case R_386_TLS_LDO_32:
4431 if (!info->executable || (input_section->flags & SEC_CODE) == 0)
4432 relocation -= elf_i386_dtpoff_base (info);
4434 /* When converting LDO to LE, we must negate. */
4435 relocation = -elf_i386_tpoff (info, relocation);
4438 case R_386_TLS_LE_32:
4440 if (!info->executable)
4442 Elf_Internal_Rela outrel;
4445 outrel.r_offset = rel->r_offset
4446 + input_section->output_section->vma
4447 + input_section->output_offset;
4448 if (h != NULL && h->dynindx != -1)
4452 if (r_type == R_386_TLS_LE_32)
4453 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
4455 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
4456 sreloc = elf_section_data (input_section)->sreloc;
4459 elf_append_rel (output_bfd, sreloc, &outrel);
4462 else if (r_type == R_386_TLS_LE_32)
4463 relocation = elf_i386_dtpoff_base (info) - relocation;
4465 relocation -= elf_i386_dtpoff_base (info);
4467 else if (r_type == R_386_TLS_LE_32)
4468 relocation = elf_i386_tpoff (info, relocation);
4470 relocation = -elf_i386_tpoff (info, relocation);
4477 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4478 because such sections are not SEC_ALLOC and thus ld.so will
4479 not process them. */
4480 if (unresolved_reloc
4481 && !((input_section->flags & SEC_DEBUGGING) != 0
4483 && _bfd_elf_section_offset (output_bfd, info, input_section,
4484 rel->r_offset) != (bfd_vma) -1)
4486 (*_bfd_error_handler)
4487 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4490 (long) rel->r_offset,
4492 h->root.root.string);
4497 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4498 contents, rel->r_offset,
4501 check_relocation_error:
4502 if (r != bfd_reloc_ok)
4507 name = h->root.root.string;
4510 name = bfd_elf_string_from_elf_section (input_bfd,
4511 symtab_hdr->sh_link,
4516 name = bfd_section_name (input_bfd, sec);
4519 if (r == bfd_reloc_overflow)
4521 if (! ((*info->callbacks->reloc_overflow)
4522 (info, (h ? &h->root : NULL), name, howto->name,
4523 (bfd_vma) 0, input_bfd, input_section,
4529 (*_bfd_error_handler)
4530 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
4531 input_bfd, input_section,
4532 (long) rel->r_offset, name, (int) r);
4541 /* Finish up dynamic symbol handling. We set the contents of various
4542 dynamic sections here. */
4545 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
4546 struct bfd_link_info *info,
4547 struct elf_link_hash_entry *h,
4548 Elf_Internal_Sym *sym)
4550 struct elf_i386_link_hash_table *htab;
4551 unsigned plt_entry_size;
4552 const struct elf_i386_backend_data *abed;
4553 struct elf_i386_link_hash_entry *eh;
4555 htab = elf_i386_hash_table (info);
4559 abed = get_elf_i386_backend_data (output_bfd);
4560 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
4562 eh = (struct elf_i386_link_hash_entry *) h;
4564 if (h->plt.offset != (bfd_vma) -1)
4568 Elf_Internal_Rela rel;
4570 asection *plt, *gotplt, *relplt;
4572 /* When building a static executable, use .iplt, .igot.plt and
4573 .rel.iplt sections for STT_GNU_IFUNC symbols. */
4574 if (htab->elf.splt != NULL)
4576 plt = htab->elf.splt;
4577 gotplt = htab->elf.sgotplt;
4578 relplt = htab->elf.srelplt;
4582 plt = htab->elf.iplt;
4583 gotplt = htab->elf.igotplt;
4584 relplt = htab->elf.irelplt;
4587 /* This symbol has an entry in the procedure linkage table. Set
4590 if ((h->dynindx == -1
4591 && !((h->forced_local || info->executable)
4593 && h->type == STT_GNU_IFUNC))
4599 /* Get the index in the procedure linkage table which
4600 corresponds to this symbol. This is the index of this symbol
4601 in all the symbols for which we are making plt entries. The
4602 first entry in the procedure linkage table is reserved.
4604 Get the offset into the .got table of the entry that
4605 corresponds to this function. Each .got entry is 4 bytes.
4606 The first three are reserved.
4608 For static executables, we don't reserve anything. */
4610 if (plt == htab->elf.splt)
4612 got_offset = h->plt.offset / plt_entry_size - 1;
4613 got_offset = (got_offset + 3) * 4;
4617 got_offset = h->plt.offset / plt_entry_size;
4618 got_offset = got_offset * 4;
4621 /* Fill in the entry in the procedure linkage table. */
4624 memcpy (plt->contents + h->plt.offset, abed->plt->plt_entry,
4625 abed->plt->plt_entry_size);
4626 bfd_put_32 (output_bfd,
4627 (gotplt->output_section->vma
4628 + gotplt->output_offset
4630 plt->contents + h->plt.offset
4631 + abed->plt->plt_got_offset);
4633 if (abed->is_vxworks)
4635 int s, k, reloc_index;
4637 /* Create the R_386_32 relocation referencing the GOT
4638 for this PLT entry. */
4640 /* S: Current slot number (zero-based). */
4641 s = ((h->plt.offset - abed->plt->plt_entry_size)
4642 / abed->plt->plt_entry_size);
4643 /* K: Number of relocations for PLTResolve. */
4645 k = PLTRESOLVE_RELOCS_SHLIB;
4647 k = PLTRESOLVE_RELOCS;
4648 /* Skip the PLTresolve relocations, and the relocations for
4649 the other PLT slots. */
4650 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
4651 loc = (htab->srelplt2->contents + reloc_index
4652 * sizeof (Elf32_External_Rel));
4654 rel.r_offset = (htab->elf.splt->output_section->vma
4655 + htab->elf.splt->output_offset
4656 + h->plt.offset + 2),
4657 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4658 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4660 /* Create the R_386_32 relocation referencing the beginning of
4661 the PLT for this GOT entry. */
4662 rel.r_offset = (htab->elf.sgotplt->output_section->vma
4663 + htab->elf.sgotplt->output_offset
4665 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4666 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4667 loc + sizeof (Elf32_External_Rel));
4672 memcpy (plt->contents + h->plt.offset, abed->plt->pic_plt_entry,
4673 abed->plt->plt_entry_size);
4674 bfd_put_32 (output_bfd, got_offset,
4675 plt->contents + h->plt.offset
4676 + abed->plt->plt_got_offset);
4679 /* Fill in the entry in the global offset table. */
4680 bfd_put_32 (output_bfd,
4681 (plt->output_section->vma
4682 + plt->output_offset
4684 + abed->plt->plt_lazy_offset),
4685 gotplt->contents + got_offset);
4687 /* Fill in the entry in the .rel.plt section. */
4688 rel.r_offset = (gotplt->output_section->vma
4689 + gotplt->output_offset
4691 if (h->dynindx == -1
4692 || ((info->executable
4693 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4695 && h->type == STT_GNU_IFUNC))
4697 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4698 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
4699 in the .got.plt section. */
4700 bfd_put_32 (output_bfd,
4701 (h->root.u.def.value
4702 + h->root.u.def.section->output_section->vma
4703 + h->root.u.def.section->output_offset),
4704 gotplt->contents + got_offset);
4705 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4706 /* R_386_IRELATIVE comes last. */
4707 plt_index = htab->next_irelative_index--;
4711 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
4712 plt_index = htab->next_jump_slot_index++;
4714 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
4715 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4717 /* Don't fill PLT entry for static executables. */
4718 if (plt == htab->elf.splt)
4720 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
4721 plt->contents + h->plt.offset
4722 + abed->plt->plt_reloc_offset);
4723 bfd_put_32 (output_bfd, - (h->plt.offset
4724 + abed->plt->plt_plt_offset + 4),
4725 plt->contents + h->plt.offset
4726 + abed->plt->plt_plt_offset);
4729 else if (eh->plt_got.offset != (bfd_vma) -1)
4731 bfd_vma got_offset, plt_offset;
4732 asection *plt, *got, *gotplt;
4733 const bfd_byte *got_plt_entry;
4735 /* Offset of displacement of the indirect jump. */
4736 bfd_vma plt_got_offset = 2;
4738 /* Set the entry in the GOT procedure linkage table. */
4739 plt = htab->plt_got;
4740 got = htab->elf.sgot;
4741 gotplt = htab->elf.sgotplt;
4742 got_offset = h->got.offset;
4744 if (got_offset == (bfd_vma) -1
4750 /* Fill in the entry in the GOT procedure linkage table. */
4753 got_plt_entry = elf_i386_got_plt_entry;
4754 got_offset += got->output_section->vma + got->output_offset;
4758 got_plt_entry = elf_i386_pic_got_plt_entry;
4759 got_offset += (got->output_section->vma
4760 + got->output_offset
4761 - gotplt->output_section->vma
4762 - gotplt->output_offset);
4765 plt_offset = eh->plt_got.offset;
4766 memcpy (plt->contents + plt_offset, got_plt_entry,
4767 sizeof (elf_i386_got_plt_entry));
4768 bfd_put_32 (output_bfd, got_offset,
4769 plt->contents + plt_offset + plt_got_offset);
4773 && (h->plt.offset != (bfd_vma) -1
4774 || eh->plt_got.offset != (bfd_vma) -1))
4776 /* Mark the symbol as undefined, rather than as defined in
4777 the .plt section. Leave the value if there were any
4778 relocations where pointer equality matters (this is a clue
4779 for the dynamic linker, to make function pointer
4780 comparisons work between an application and shared
4781 library), otherwise set it to zero. If a function is only
4782 called from a binary, there is no need to slow down
4783 shared libraries because of that. */
4784 sym->st_shndx = SHN_UNDEF;
4785 if (!h->pointer_equality_needed)
4789 if (h->got.offset != (bfd_vma) -1
4790 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
4791 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
4793 Elf_Internal_Rela rel;
4795 /* This symbol has an entry in the global offset table. Set it
4798 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4801 rel.r_offset = (htab->elf.sgot->output_section->vma
4802 + htab->elf.sgot->output_offset
4803 + (h->got.offset & ~(bfd_vma) 1));
4805 /* If this is a static link, or it is a -Bsymbolic link and the
4806 symbol is defined locally or was forced to be local because
4807 of a version file, we just want to emit a RELATIVE reloc.
4808 The entry in the global offset table will already have been
4809 initialized in the relocate_section function. */
4811 && h->type == STT_GNU_IFUNC)
4815 /* Generate R_386_GLOB_DAT. */
4822 if (!h->pointer_equality_needed)
4825 /* For non-shared object, we can't use .got.plt, which
4826 contains the real function addres if we need pointer
4827 equality. We load the GOT entry with the PLT entry. */
4828 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4829 bfd_put_32 (output_bfd,
4830 (plt->output_section->vma
4831 + plt->output_offset + h->plt.offset),
4832 htab->elf.sgot->contents + h->got.offset);
4836 else if (info->shared
4837 && SYMBOL_REFERENCES_LOCAL (info, h))
4839 BFD_ASSERT((h->got.offset & 1) != 0);
4840 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4844 BFD_ASSERT((h->got.offset & 1) == 0);
4846 bfd_put_32 (output_bfd, (bfd_vma) 0,
4847 htab->elf.sgot->contents + h->got.offset);
4848 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
4851 elf_append_rel (output_bfd, htab->elf.srelgot, &rel);
4856 Elf_Internal_Rela rel;
4858 /* This symbol needs a copy reloc. Set it up. */
4860 if (h->dynindx == -1
4861 || (h->root.type != bfd_link_hash_defined
4862 && h->root.type != bfd_link_hash_defweak)
4863 || htab->srelbss == NULL)
4866 rel.r_offset = (h->root.u.def.value
4867 + h->root.u.def.section->output_section->vma
4868 + h->root.u.def.section->output_offset);
4869 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
4870 elf_append_rel (output_bfd, htab->srelbss, &rel);
4876 /* Finish up local dynamic symbol handling. We set the contents of
4877 various dynamic sections here. */
4880 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4882 struct elf_link_hash_entry *h
4883 = (struct elf_link_hash_entry *) *slot;
4884 struct bfd_link_info *info
4885 = (struct bfd_link_info *) inf;
4887 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
4891 /* Used to decide how to sort relocs in an optimal manner for the
4892 dynamic linker, before writing them out. */
4894 static enum elf_reloc_type_class
4895 elf_i386_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4896 const asection *rel_sec ATTRIBUTE_UNUSED,
4897 const Elf_Internal_Rela *rela)
4899 switch (ELF32_R_TYPE (rela->r_info))
4901 case R_386_RELATIVE:
4902 return reloc_class_relative;
4903 case R_386_JUMP_SLOT:
4904 return reloc_class_plt;
4906 return reloc_class_copy;
4908 return reloc_class_normal;
4912 /* Finish up the dynamic sections. */
4915 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4916 struct bfd_link_info *info)
4918 struct elf_i386_link_hash_table *htab;
4921 const struct elf_i386_backend_data *abed;
4923 htab = elf_i386_hash_table (info);
4927 dynobj = htab->elf.dynobj;
4928 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4929 abed = get_elf_i386_backend_data (output_bfd);
4931 if (htab->elf.dynamic_sections_created)
4933 Elf32_External_Dyn *dyncon, *dynconend;
4935 if (sdyn == NULL || htab->elf.sgot == NULL)
4938 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4939 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4940 for (; dyncon < dynconend; dyncon++)
4942 Elf_Internal_Dyn dyn;
4945 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4950 if (abed->is_vxworks
4951 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4956 s = htab->elf.sgotplt;
4957 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4961 s = htab->elf.srelplt;
4962 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4966 s = htab->elf.srelplt;
4967 dyn.d_un.d_val = s->size;
4971 /* My reading of the SVR4 ABI indicates that the
4972 procedure linkage table relocs (DT_JMPREL) should be
4973 included in the overall relocs (DT_REL). This is
4974 what Solaris does. However, UnixWare can not handle
4975 that case. Therefore, we override the DT_RELSZ entry
4976 here to make it not include the JMPREL relocs. */
4977 s = htab->elf.srelplt;
4980 dyn.d_un.d_val -= s->size;
4984 /* We may not be using the standard ELF linker script.
4985 If .rel.plt is the first .rel section, we adjust
4986 DT_REL to not include it. */
4987 s = htab->elf.srelplt;
4990 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4992 dyn.d_un.d_ptr += s->size;
4996 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4999 /* Fill in the first entry in the procedure linkage table. */
5000 if (htab->elf.splt && htab->elf.splt->size > 0)
5004 memcpy (htab->elf.splt->contents, abed->plt->pic_plt0_entry,
5005 abed->plt->plt0_entry_size);
5006 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
5007 abed->plt0_pad_byte,
5008 abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
5012 memcpy (htab->elf.splt->contents, abed->plt->plt0_entry,
5013 abed->plt->plt0_entry_size);
5014 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
5015 abed->plt0_pad_byte,
5016 abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
5017 bfd_put_32 (output_bfd,
5018 (htab->elf.sgotplt->output_section->vma
5019 + htab->elf.sgotplt->output_offset
5021 htab->elf.splt->contents
5022 + abed->plt->plt0_got1_offset);
5023 bfd_put_32 (output_bfd,
5024 (htab->elf.sgotplt->output_section->vma
5025 + htab->elf.sgotplt->output_offset
5027 htab->elf.splt->contents
5028 + abed->plt->plt0_got2_offset);
5030 if (abed->is_vxworks)
5032 Elf_Internal_Rela rel;
5034 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
5035 On IA32 we use REL relocations so the addend goes in
5036 the PLT directly. */
5037 rel.r_offset = (htab->elf.splt->output_section->vma
5038 + htab->elf.splt->output_offset
5039 + abed->plt->plt0_got1_offset);
5040 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5041 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5042 htab->srelplt2->contents);
5043 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
5044 rel.r_offset = (htab->elf.splt->output_section->vma
5045 + htab->elf.splt->output_offset
5046 + abed->plt->plt0_got2_offset);
5047 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5048 bfd_elf32_swap_reloc_out (output_bfd, &rel,
5049 htab->srelplt2->contents +
5050 sizeof (Elf32_External_Rel));
5054 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5055 really seem like the right value. */
5056 elf_section_data (htab->elf.splt->output_section)
5057 ->this_hdr.sh_entsize = 4;
5059 /* Correct the .rel.plt.unloaded relocations. */
5060 if (abed->is_vxworks && !info->shared)
5062 int num_plts = (htab->elf.splt->size
5063 / abed->plt->plt_entry_size) - 1;
5066 p = htab->srelplt2->contents;
5068 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
5070 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
5072 for (; num_plts; num_plts--)
5074 Elf_Internal_Rela rel;
5075 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
5076 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5077 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
5078 p += sizeof (Elf32_External_Rel);
5080 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
5081 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
5082 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
5083 p += sizeof (Elf32_External_Rel);
5089 if (htab->elf.sgotplt)
5091 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
5093 (*_bfd_error_handler)
5094 (_("discarded output section: `%A'"), htab->elf.sgotplt);
5098 /* Fill in the first three entries in the global offset table. */
5099 if (htab->elf.sgotplt->size > 0)
5101 bfd_put_32 (output_bfd,
5103 : sdyn->output_section->vma + sdyn->output_offset),
5104 htab->elf.sgotplt->contents);
5105 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
5106 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
5109 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
5112 /* Adjust .eh_frame for .plt section. */
5113 if (htab->plt_eh_frame != NULL
5114 && htab->plt_eh_frame->contents != NULL)
5116 if (htab->elf.splt != NULL
5117 && htab->elf.splt->size != 0
5118 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
5119 && htab->elf.splt->output_section != NULL
5120 && htab->plt_eh_frame->output_section != NULL)
5122 bfd_vma plt_start = htab->elf.splt->output_section->vma;
5123 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
5124 + htab->plt_eh_frame->output_offset
5125 + PLT_FDE_START_OFFSET;
5126 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5127 htab->plt_eh_frame->contents
5128 + PLT_FDE_START_OFFSET);
5130 if (htab->plt_eh_frame->sec_info_type
5131 == SEC_INFO_TYPE_EH_FRAME)
5133 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5135 htab->plt_eh_frame->contents))
5140 if (htab->elf.sgot && htab->elf.sgot->size > 0)
5141 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
5143 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
5144 htab_traverse (htab->loc_hash_table,
5145 elf_i386_finish_local_dynamic_symbol,
5151 /* Return an array of PLT entry symbol values. */
5154 elf_i386_get_plt_sym_val (bfd *abfd, asymbol **dynsyms, asection *plt,
5157 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
5160 bfd_vma *plt_sym_val;
5162 bfd_byte *plt_contents;
5163 const struct elf_i386_backend_data *bed
5164 = get_elf_i386_backend_data (abfd);
5165 Elf_Internal_Shdr *hdr;
5167 /* Get the .plt section contents. */
5168 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
5169 if (plt_contents == NULL)
5171 if (!bfd_get_section_contents (abfd, (asection *) plt,
5172 plt_contents, 0, plt->size))
5175 free (plt_contents);
5179 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5180 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
5183 hdr = &elf_section_data (relplt)->this_hdr;
5184 count = relplt->size / hdr->sh_entsize;
5186 plt_sym_val = (bfd_vma *) bfd_malloc (sizeof (bfd_vma) * count);
5187 if (plt_sym_val == NULL)
5190 for (i = 0; i < count; i++, p++)
5191 plt_sym_val[i] = -1;
5193 plt_offset = bed->plt->plt_entry_size;
5194 p = relplt->relocation;
5195 for (i = 0; i < count; i++, p++)
5199 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
5200 if (p->howto == NULL)
5203 if (p->howto->type != R_386_JUMP_SLOT
5204 && p->howto->type != R_386_IRELATIVE)
5207 reloc_index = H_GET_32 (abfd, (plt_contents + plt_offset
5208 + bed->plt->plt_reloc_offset));
5209 reloc_index /= sizeof (Elf32_External_Rel);
5210 if (reloc_index >= count)
5212 plt_sym_val[reloc_index] = plt->vma + plt_offset;
5213 plt_offset += bed->plt->plt_entry_size;
5216 free (plt_contents);
5221 /* Similar to _bfd_elf_get_synthetic_symtab. */
5224 elf_i386_get_synthetic_symtab (bfd *abfd,
5231 asection *plt = bfd_get_section_by_name (abfd, ".plt");
5232 return _bfd_elf_ifunc_get_synthetic_symtab (abfd, symcount, syms,
5233 dynsymcount, dynsyms, ret,
5235 elf_i386_get_plt_sym_val);
5238 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5241 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
5243 if (h->plt.offset != (bfd_vma) -1
5245 && !h->pointer_equality_needed)
5248 return _bfd_elf_hash_symbol (h);
5251 /* Hook called by the linker routine which adds symbols from an object
5255 elf_i386_add_symbol_hook (bfd * abfd,
5256 struct bfd_link_info * info,
5257 Elf_Internal_Sym * sym,
5258 const char ** namep ATTRIBUTE_UNUSED,
5259 flagword * flagsp ATTRIBUTE_UNUSED,
5260 asection ** secp ATTRIBUTE_UNUSED,
5261 bfd_vma * valp ATTRIBUTE_UNUSED)
5263 if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
5264 || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
5265 && (abfd->flags & DYNAMIC) == 0
5266 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
5267 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
5272 #define TARGET_LITTLE_SYM i386_elf32_vec
5273 #define TARGET_LITTLE_NAME "elf32-i386"
5274 #define ELF_ARCH bfd_arch_i386
5275 #define ELF_TARGET_ID I386_ELF_DATA
5276 #define ELF_MACHINE_CODE EM_386
5277 #define ELF_MAXPAGESIZE 0x1000
5279 #define elf_backend_can_gc_sections 1
5280 #define elf_backend_can_refcount 1
5281 #define elf_backend_want_got_plt 1
5282 #define elf_backend_plt_readonly 1
5283 #define elf_backend_want_plt_sym 0
5284 #define elf_backend_got_header_size 12
5285 #define elf_backend_plt_alignment 4
5287 /* Support RELA for objdump of prelink objects. */
5288 #define elf_info_to_howto elf_i386_info_to_howto_rel
5289 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
5291 #define bfd_elf32_mkobject elf_i386_mkobject
5293 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
5294 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
5295 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
5296 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
5297 #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
5299 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
5300 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
5301 #define elf_backend_check_relocs elf_i386_check_relocs
5302 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
5303 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
5304 #define elf_backend_fake_sections elf_i386_fake_sections
5305 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
5306 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
5307 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
5308 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
5309 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
5310 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
5311 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
5312 #define elf_backend_relocate_section elf_i386_relocate_section
5313 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
5314 #define elf_backend_always_size_sections elf_i386_always_size_sections
5315 #define elf_backend_omit_section_dynsym \
5316 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
5317 #define elf_backend_hash_symbol elf_i386_hash_symbol
5318 #define elf_backend_add_symbol_hook elf_i386_add_symbol_hook
5320 #include "elf32-target.h"
5322 /* FreeBSD support. */
5324 #undef TARGET_LITTLE_SYM
5325 #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
5326 #undef TARGET_LITTLE_NAME
5327 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
5329 #define ELF_OSABI ELFOSABI_FREEBSD
5331 /* The kernel recognizes executables as valid only if they carry a
5332 "FreeBSD" label in the ELF header. So we put this label on all
5333 executables and (for simplicity) also all other object files. */
5336 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
5338 _bfd_elf_post_process_headers (abfd, info);
5340 #ifdef OLD_FREEBSD_ABI_LABEL
5342 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
5343 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5344 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
5349 #undef elf_backend_post_process_headers
5350 #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
5352 #define elf32_bed elf32_i386_fbsd_bed
5354 #undef elf_backend_add_symbol_hook
5356 #include "elf32-target.h"
5360 #undef TARGET_LITTLE_SYM
5361 #define TARGET_LITTLE_SYM i386_elf32_sol2_vec
5362 #undef TARGET_LITTLE_NAME
5363 #define TARGET_LITTLE_NAME "elf32-i386-sol2"
5365 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5366 objects won't be recognized. */
5370 #define elf32_bed elf32_i386_sol2_bed
5372 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
5374 #undef elf_backend_static_tls_alignment
5375 #define elf_backend_static_tls_alignment 8
5377 /* The Solaris 2 ABI requires a plt symbol on all platforms.
5379 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5381 #undef elf_backend_want_plt_sym
5382 #define elf_backend_want_plt_sym 1
5384 #include "elf32-target.h"
5386 /* Native Client support. */
5388 #undef TARGET_LITTLE_SYM
5389 #define TARGET_LITTLE_SYM i386_elf32_nacl_vec
5390 #undef TARGET_LITTLE_NAME
5391 #define TARGET_LITTLE_NAME "elf32-i386-nacl"
5393 #define elf32_bed elf32_i386_nacl_bed
5395 #undef ELF_MAXPAGESIZE
5396 #define ELF_MAXPAGESIZE 0x10000
5398 /* Restore defaults. */
5400 #undef elf_backend_want_plt_sym
5401 #define elf_backend_want_plt_sym 0
5402 #undef elf_backend_post_process_headers
5403 #undef elf_backend_static_tls_alignment
5405 /* NaCl uses substantially different PLT entries for the same effects. */
5407 #undef elf_backend_plt_alignment
5408 #define elf_backend_plt_alignment 5
5409 #define NACL_PLT_ENTRY_SIZE 64
5410 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
5412 static const bfd_byte elf_i386_nacl_plt0_entry[] =
5414 0xff, 0x35, /* pushl contents of address */
5415 0, 0, 0, 0, /* replaced with address of .got + 4. */
5416 0x8b, 0x0d, /* movl contents of address, %ecx */
5417 0, 0, 0, 0, /* replaced with address of .got + 8. */
5418 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
5419 0xff, 0xe1 /* jmp *%ecx */
5422 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
5424 0x8b, 0x0d, /* movl contents of address, %ecx */
5425 0, 0, 0, 0, /* replaced with GOT slot address. */
5426 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
5427 0xff, 0xe1, /* jmp *%ecx */
5429 /* Pad to the next 32-byte boundary with nop instructions. */
5431 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5432 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5434 /* Lazy GOT entries point here (32-byte aligned). */
5435 0x68, /* pushl immediate */
5436 0, 0, 0, 0, /* replaced with reloc offset. */
5437 0xe9, /* jmp relative */
5438 0, 0, 0, 0, /* replaced with offset to .plt. */
5440 /* Pad to the next 32-byte boundary with nop instructions. */
5441 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5442 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5446 static const bfd_byte
5447 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
5449 0xff, 0x73, 0x04, /* pushl 4(%ebx) */
5450 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */
5451 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */
5452 0xff, 0xe1, /* jmp *%ecx */
5454 /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
5455 so pad to that size with nop instructions. */
5456 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
5459 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
5461 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
5462 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
5463 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */
5464 0xff, 0xe1, /* jmp *%ecx */
5466 /* Pad to the next 32-byte boundary with nop instructions. */
5468 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5469 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5471 /* Lazy GOT entries point here (32-byte aligned). */
5472 0x68, /* pushl immediate */
5473 0, 0, 0, 0, /* replaced with offset into relocation table. */
5474 0xe9, /* jmp relative */
5475 0, 0, 0, 0, /* replaced with offset to start of .plt. */
5477 /* Pad to the next 32-byte boundary with nop instructions. */
5478 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5479 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
5483 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
5485 #if (PLT_CIE_LENGTH != 20 \
5486 || PLT_FDE_LENGTH != 36 \
5487 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
5488 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
5489 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
5491 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
5492 0, 0, 0, 0, /* CIE ID */
5493 1, /* CIE version */
5494 'z', 'R', 0, /* Augmentation string */
5495 1, /* Code alignment factor */
5496 0x7c, /* Data alignment factor: -4 */
5497 8, /* Return address column */
5498 1, /* Augmentation size */
5499 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
5500 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
5501 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
5502 DW_CFA_nop, DW_CFA_nop,
5504 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
5505 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
5506 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
5507 0, 0, 0, 0, /* .plt size goes here */
5508 0, /* Augmentation size */
5509 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
5510 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
5511 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
5512 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5513 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
5514 13, /* Block length */
5515 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
5516 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
5517 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5518 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
5519 DW_CFA_nop, DW_CFA_nop
5522 static const struct elf_i386_plt_layout elf_i386_nacl_plt =
5524 elf_i386_nacl_plt0_entry, /* plt0_entry */
5525 sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */
5526 2, /* plt0_got1_offset */
5527 8, /* plt0_got2_offset */
5528 elf_i386_nacl_plt_entry, /* plt_entry */
5529 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
5530 2, /* plt_got_offset */
5531 33, /* plt_reloc_offset */
5532 38, /* plt_plt_offset */
5533 32, /* plt_lazy_offset */
5534 elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */
5535 elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */
5536 elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */
5537 sizeof (elf_i386_nacl_eh_frame_plt),/* eh_frame_plt_size */
5540 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
5542 &elf_i386_nacl_plt, /* plt */
5543 0x90, /* plt0_pad_byte: nop insn */
5548 elf32_i386_nacl_elf_object_p (bfd *abfd)
5550 /* Set the right machine number for a NaCl i386 ELF32 file. */
5551 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
5555 #undef elf_backend_arch_data
5556 #define elf_backend_arch_data &elf_i386_nacl_arch_bed
5558 #undef elf_backend_object_p
5559 #define elf_backend_object_p elf32_i386_nacl_elf_object_p
5560 #undef elf_backend_modify_segment_map
5561 #define elf_backend_modify_segment_map nacl_modify_segment_map
5562 #undef elf_backend_modify_program_headers
5563 #define elf_backend_modify_program_headers nacl_modify_program_headers
5564 #undef elf_backend_final_write_processing
5565 #define elf_backend_final_write_processing nacl_final_write_processing
5567 #include "elf32-target.h"
5569 /* Restore defaults. */
5570 #undef elf_backend_object_p
5571 #undef elf_backend_modify_segment_map
5572 #undef elf_backend_modify_program_headers
5573 #undef elf_backend_final_write_processing
5575 /* VxWorks support. */
5577 #undef TARGET_LITTLE_SYM
5578 #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
5579 #undef TARGET_LITTLE_NAME
5580 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
5582 #undef elf_backend_plt_alignment
5583 #define elf_backend_plt_alignment 4
5585 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
5587 &elf_i386_plt, /* plt */
5588 0x90, /* plt0_pad_byte */
5592 #undef elf_backend_arch_data
5593 #define elf_backend_arch_data &elf_i386_vxworks_arch_bed
5595 #undef elf_backend_relocs_compatible
5596 #undef elf_backend_add_symbol_hook
5597 #define elf_backend_add_symbol_hook \
5598 elf_vxworks_add_symbol_hook
5599 #undef elf_backend_link_output_symbol_hook
5600 #define elf_backend_link_output_symbol_hook \
5601 elf_vxworks_link_output_symbol_hook
5602 #undef elf_backend_emit_relocs
5603 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
5604 #undef elf_backend_final_write_processing
5605 #define elf_backend_final_write_processing \
5606 elf_vxworks_final_write_processing
5607 #undef elf_backend_static_tls_alignment
5609 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
5611 #undef elf_backend_want_plt_sym
5612 #define elf_backend_want_plt_sym 1
5615 #define elf32_bed elf32_i386_vxworks_bed
5617 #include "elf32-target.h"