2009-06-16 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / bfd / elf32-i386.c
blob610c709959f21f78bc0ec11cab6b62333ad65e0e
1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf-vxworks.h"
28 #include "bfd_stdint.h"
29 #include "objalloc.h"
30 #include "hashtab.h"
32 /* 386 uses REL relocations instead of RELA. */
33 #define USE_REL 1
35 #include "elf/i386.h"
37 static reloc_howto_type elf_howto_table[]=
39 HOWTO(R_386_NONE, 0, 0, 0, FALSE, 0, complain_overflow_bitfield,
40 bfd_elf_generic_reloc, "R_386_NONE",
41 TRUE, 0x00000000, 0x00000000, FALSE),
42 HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
43 bfd_elf_generic_reloc, "R_386_32",
44 TRUE, 0xffffffff, 0xffffffff, FALSE),
45 HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
46 bfd_elf_generic_reloc, "R_386_PC32",
47 TRUE, 0xffffffff, 0xffffffff, TRUE),
48 HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
49 bfd_elf_generic_reloc, "R_386_GOT32",
50 TRUE, 0xffffffff, 0xffffffff, FALSE),
51 HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
52 bfd_elf_generic_reloc, "R_386_PLT32",
53 TRUE, 0xffffffff, 0xffffffff, TRUE),
54 HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
55 bfd_elf_generic_reloc, "R_386_COPY",
56 TRUE, 0xffffffff, 0xffffffff, FALSE),
57 HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
58 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
59 TRUE, 0xffffffff, 0xffffffff, FALSE),
60 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
61 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
62 TRUE, 0xffffffff, 0xffffffff, FALSE),
63 HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
64 bfd_elf_generic_reloc, "R_386_RELATIVE",
65 TRUE, 0xffffffff, 0xffffffff, FALSE),
66 HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
67 bfd_elf_generic_reloc, "R_386_GOTOFF",
68 TRUE, 0xffffffff, 0xffffffff, FALSE),
69 HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
70 bfd_elf_generic_reloc, "R_386_GOTPC",
71 TRUE, 0xffffffff, 0xffffffff, TRUE),
73 /* We have a gap in the reloc numbers here.
74 R_386_standard counts the number up to this point, and
75 R_386_ext_offset is the value to subtract from a reloc type of
76 R_386_16 thru R_386_PC8 to form an index into this table. */
77 #define R_386_standard (R_386_GOTPC + 1)
78 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
80 /* These relocs are a GNU extension. */
81 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
82 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
83 TRUE, 0xffffffff, 0xffffffff, FALSE),
84 HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
85 bfd_elf_generic_reloc, "R_386_TLS_IE",
86 TRUE, 0xffffffff, 0xffffffff, FALSE),
87 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
89 TRUE, 0xffffffff, 0xffffffff, FALSE),
90 HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
91 bfd_elf_generic_reloc, "R_386_TLS_LE",
92 TRUE, 0xffffffff, 0xffffffff, FALSE),
93 HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
94 bfd_elf_generic_reloc, "R_386_TLS_GD",
95 TRUE, 0xffffffff, 0xffffffff, FALSE),
96 HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
97 bfd_elf_generic_reloc, "R_386_TLS_LDM",
98 TRUE, 0xffffffff, 0xffffffff, FALSE),
99 HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
100 bfd_elf_generic_reloc, "R_386_16",
101 TRUE, 0xffff, 0xffff, FALSE),
102 HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
103 bfd_elf_generic_reloc, "R_386_PC16",
104 TRUE, 0xffff, 0xffff, TRUE),
105 HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
106 bfd_elf_generic_reloc, "R_386_8",
107 TRUE, 0xff, 0xff, FALSE),
108 HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
109 bfd_elf_generic_reloc, "R_386_PC8",
110 TRUE, 0xff, 0xff, TRUE),
112 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
113 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
114 /* These are common with Solaris TLS implementation. */
115 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
116 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
117 TRUE, 0xffffffff, 0xffffffff, FALSE),
118 HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
119 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
120 TRUE, 0xffffffff, 0xffffffff, FALSE),
121 HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
122 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
123 TRUE, 0xffffffff, 0xffffffff, FALSE),
124 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
125 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
126 TRUE, 0xffffffff, 0xffffffff, FALSE),
127 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
128 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
129 TRUE, 0xffffffff, 0xffffffff, FALSE),
130 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
131 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
132 TRUE, 0xffffffff, 0xffffffff, FALSE),
133 EMPTY_HOWTO (38),
134 HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
135 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
136 TRUE, 0xffffffff, 0xffffffff, FALSE),
137 HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
138 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
139 FALSE, 0, 0, FALSE),
140 HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
141 bfd_elf_generic_reloc, "R_386_TLS_DESC",
142 TRUE, 0xffffffff, 0xffffffff, FALSE),
143 HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
144 bfd_elf_generic_reloc, "R_386_IRELATIVE",
145 TRUE, 0xffffffff, 0xffffffff, FALSE),
147 /* Another gap. */
148 #define R_386_irelative (R_386_IRELATIVE + 1 - R_386_tls_offset)
149 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_irelative)
151 /* GNU extension to record C++ vtable hierarchy. */
152 HOWTO (R_386_GNU_VTINHERIT, /* type */
153 0, /* rightshift */
154 2, /* size (0 = byte, 1 = short, 2 = long) */
155 0, /* bitsize */
156 FALSE, /* pc_relative */
157 0, /* bitpos */
158 complain_overflow_dont, /* complain_on_overflow */
159 NULL, /* special_function */
160 "R_386_GNU_VTINHERIT", /* name */
161 FALSE, /* partial_inplace */
162 0, /* src_mask */
163 0, /* dst_mask */
164 FALSE), /* pcrel_offset */
166 /* GNU extension to record C++ vtable member usage. */
167 HOWTO (R_386_GNU_VTENTRY, /* type */
168 0, /* rightshift */
169 2, /* size (0 = byte, 1 = short, 2 = long) */
170 0, /* bitsize */
171 FALSE, /* pc_relative */
172 0, /* bitpos */
173 complain_overflow_dont, /* complain_on_overflow */
174 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
175 "R_386_GNU_VTENTRY", /* name */
176 FALSE, /* partial_inplace */
177 0, /* src_mask */
178 0, /* dst_mask */
179 FALSE) /* pcrel_offset */
181 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
185 #ifdef DEBUG_GEN_RELOC
186 #define TRACE(str) \
187 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
188 #else
189 #define TRACE(str)
190 #endif
192 static reloc_howto_type *
193 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
194 bfd_reloc_code_real_type code)
196 switch (code)
198 case BFD_RELOC_NONE:
199 TRACE ("BFD_RELOC_NONE");
200 return &elf_howto_table[R_386_NONE];
202 case BFD_RELOC_32:
203 TRACE ("BFD_RELOC_32");
204 return &elf_howto_table[R_386_32];
206 case BFD_RELOC_CTOR:
207 TRACE ("BFD_RELOC_CTOR");
208 return &elf_howto_table[R_386_32];
210 case BFD_RELOC_32_PCREL:
211 TRACE ("BFD_RELOC_PC32");
212 return &elf_howto_table[R_386_PC32];
214 case BFD_RELOC_386_GOT32:
215 TRACE ("BFD_RELOC_386_GOT32");
216 return &elf_howto_table[R_386_GOT32];
218 case BFD_RELOC_386_PLT32:
219 TRACE ("BFD_RELOC_386_PLT32");
220 return &elf_howto_table[R_386_PLT32];
222 case BFD_RELOC_386_COPY:
223 TRACE ("BFD_RELOC_386_COPY");
224 return &elf_howto_table[R_386_COPY];
226 case BFD_RELOC_386_GLOB_DAT:
227 TRACE ("BFD_RELOC_386_GLOB_DAT");
228 return &elf_howto_table[R_386_GLOB_DAT];
230 case BFD_RELOC_386_JUMP_SLOT:
231 TRACE ("BFD_RELOC_386_JUMP_SLOT");
232 return &elf_howto_table[R_386_JUMP_SLOT];
234 case BFD_RELOC_386_RELATIVE:
235 TRACE ("BFD_RELOC_386_RELATIVE");
236 return &elf_howto_table[R_386_RELATIVE];
238 case BFD_RELOC_386_GOTOFF:
239 TRACE ("BFD_RELOC_386_GOTOFF");
240 return &elf_howto_table[R_386_GOTOFF];
242 case BFD_RELOC_386_GOTPC:
243 TRACE ("BFD_RELOC_386_GOTPC");
244 return &elf_howto_table[R_386_GOTPC];
246 /* These relocs are a GNU extension. */
247 case BFD_RELOC_386_TLS_TPOFF:
248 TRACE ("BFD_RELOC_386_TLS_TPOFF");
249 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
251 case BFD_RELOC_386_TLS_IE:
252 TRACE ("BFD_RELOC_386_TLS_IE");
253 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
255 case BFD_RELOC_386_TLS_GOTIE:
256 TRACE ("BFD_RELOC_386_TLS_GOTIE");
257 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
259 case BFD_RELOC_386_TLS_LE:
260 TRACE ("BFD_RELOC_386_TLS_LE");
261 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
263 case BFD_RELOC_386_TLS_GD:
264 TRACE ("BFD_RELOC_386_TLS_GD");
265 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
267 case BFD_RELOC_386_TLS_LDM:
268 TRACE ("BFD_RELOC_386_TLS_LDM");
269 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
271 case BFD_RELOC_16:
272 TRACE ("BFD_RELOC_16");
273 return &elf_howto_table[R_386_16 - R_386_ext_offset];
275 case BFD_RELOC_16_PCREL:
276 TRACE ("BFD_RELOC_16_PCREL");
277 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
279 case BFD_RELOC_8:
280 TRACE ("BFD_RELOC_8");
281 return &elf_howto_table[R_386_8 - R_386_ext_offset];
283 case BFD_RELOC_8_PCREL:
284 TRACE ("BFD_RELOC_8_PCREL");
285 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
287 /* Common with Sun TLS implementation. */
288 case BFD_RELOC_386_TLS_LDO_32:
289 TRACE ("BFD_RELOC_386_TLS_LDO_32");
290 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
292 case BFD_RELOC_386_TLS_IE_32:
293 TRACE ("BFD_RELOC_386_TLS_IE_32");
294 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
296 case BFD_RELOC_386_TLS_LE_32:
297 TRACE ("BFD_RELOC_386_TLS_LE_32");
298 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
300 case BFD_RELOC_386_TLS_DTPMOD32:
301 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
302 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
304 case BFD_RELOC_386_TLS_DTPOFF32:
305 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
306 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
308 case BFD_RELOC_386_TLS_TPOFF32:
309 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
310 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
312 case BFD_RELOC_386_TLS_GOTDESC:
313 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
314 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
316 case BFD_RELOC_386_TLS_DESC_CALL:
317 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
318 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
320 case BFD_RELOC_386_TLS_DESC:
321 TRACE ("BFD_RELOC_386_TLS_DESC");
322 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
324 case BFD_RELOC_386_IRELATIVE:
325 TRACE ("BFD_RELOC_386_IRELATIVE");
326 return &elf_howto_table[R_386_IRELATIVE];
328 case BFD_RELOC_VTABLE_INHERIT:
329 TRACE ("BFD_RELOC_VTABLE_INHERIT");
330 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
332 case BFD_RELOC_VTABLE_ENTRY:
333 TRACE ("BFD_RELOC_VTABLE_ENTRY");
334 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
336 default:
337 break;
340 TRACE ("Unknown");
341 return 0;
344 static reloc_howto_type *
345 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
346 const char *r_name)
348 unsigned int i;
350 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
351 if (elf_howto_table[i].name != NULL
352 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
353 return &elf_howto_table[i];
355 return NULL;
358 static reloc_howto_type *
359 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
361 unsigned int indx;
363 if ((indx = r_type) >= R_386_standard
364 && ((indx = r_type - R_386_ext_offset) - R_386_standard
365 >= R_386_ext - R_386_standard)
366 && ((indx = r_type - R_386_tls_offset) - R_386_ext
367 >= R_386_irelative - R_386_ext)
368 && ((indx = r_type - R_386_vt_offset) - R_386_irelative
369 >= R_386_vt - R_386_irelative))
371 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
372 abfd, (int) r_type);
373 indx = R_386_NONE;
375 BFD_ASSERT (elf_howto_table [indx].type == r_type);
376 return &elf_howto_table[indx];
379 static void
380 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
381 arelent *cache_ptr,
382 Elf_Internal_Rela *dst)
384 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
385 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
388 /* Return whether a symbol name implies a local label. The UnixWare
389 2.1 cc generates temporary symbols that start with .X, so we
390 recognize them here. FIXME: do other SVR4 compilers also use .X?.
391 If so, we should move the .X recognition into
392 _bfd_elf_is_local_label_name. */
394 static bfd_boolean
395 elf_i386_is_local_label_name (bfd *abfd, const char *name)
397 if (name[0] == '.' && name[1] == 'X')
398 return TRUE;
400 return _bfd_elf_is_local_label_name (abfd, name);
403 /* Support for core dump NOTE sections. */
405 static bfd_boolean
406 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
408 int offset;
409 size_t size;
411 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
413 int pr_version = bfd_get_32 (abfd, note->descdata);
415 if (pr_version != 1)
416 return FALSE;
418 /* pr_cursig */
419 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 20);
421 /* pr_pid */
422 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
424 /* pr_reg */
425 offset = 28;
426 size = bfd_get_32 (abfd, note->descdata + 8);
428 else
430 switch (note->descsz)
432 default:
433 return FALSE;
435 case 144: /* Linux/i386 */
436 /* pr_cursig */
437 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
439 /* pr_pid */
440 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
442 /* pr_reg */
443 offset = 72;
444 size = 68;
446 break;
450 /* Make a ".reg/999" section. */
451 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
452 size, note->descpos + offset);
455 static bfd_boolean
456 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
458 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
460 int pr_version = bfd_get_32 (abfd, note->descdata);
462 if (pr_version != 1)
463 return FALSE;
465 elf_tdata (abfd)->core_program
466 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
467 elf_tdata (abfd)->core_command
468 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
470 else
472 switch (note->descsz)
474 default:
475 return FALSE;
477 case 124: /* Linux/i386 elf_prpsinfo. */
478 elf_tdata (abfd)->core_program
479 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
480 elf_tdata (abfd)->core_command
481 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
485 /* Note that for some reason, a spurious space is tacked
486 onto the end of the args in some (at least one anyway)
487 implementations, so strip it off if it exists. */
489 char *command = elf_tdata (abfd)->core_command;
490 int n = strlen (command);
492 if (0 < n && command[n - 1] == ' ')
493 command[n - 1] = '\0';
496 return TRUE;
499 /* Functions for the i386 ELF linker.
501 In order to gain some understanding of code in this file without
502 knowing all the intricate details of the linker, note the
503 following:
505 Functions named elf_i386_* are called by external routines, other
506 functions are only called locally. elf_i386_* functions appear
507 in this file more or less in the order in which they are called
508 from external routines. eg. elf_i386_check_relocs is called
509 early in the link process, elf_i386_finish_dynamic_sections is
510 one of the last functions. */
513 /* The name of the dynamic interpreter. This is put in the .interp
514 section. */
516 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
518 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
519 copying dynamic variables from a shared lib into an app's dynbss
520 section, and instead use a dynamic relocation to point into the
521 shared lib. */
522 #define ELIMINATE_COPY_RELOCS 1
524 /* The size in bytes of an entry in the procedure linkage table. */
526 #define PLT_ENTRY_SIZE 16
528 /* The first entry in an absolute procedure linkage table looks like
529 this. See the SVR4 ABI i386 supplement to see how this works.
530 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
532 static const bfd_byte elf_i386_plt0_entry[12] =
534 0xff, 0x35, /* pushl contents of address */
535 0, 0, 0, 0, /* replaced with address of .got + 4. */
536 0xff, 0x25, /* jmp indirect */
537 0, 0, 0, 0 /* replaced with address of .got + 8. */
540 /* Subsequent entries in an absolute procedure linkage table look like
541 this. */
543 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
545 0xff, 0x25, /* jmp indirect */
546 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
547 0x68, /* pushl immediate */
548 0, 0, 0, 0, /* replaced with offset into relocation table. */
549 0xe9, /* jmp relative */
550 0, 0, 0, 0 /* replaced with offset to start of .plt. */
553 /* The first entry in a PIC procedure linkage table look like this.
554 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
556 static const bfd_byte elf_i386_pic_plt0_entry[12] =
558 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
559 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
562 /* Subsequent entries in a PIC procedure linkage table look like this. */
564 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
566 0xff, 0xa3, /* jmp *offset(%ebx) */
567 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
568 0x68, /* pushl immediate */
569 0, 0, 0, 0, /* replaced with offset into relocation table. */
570 0xe9, /* jmp relative */
571 0, 0, 0, 0 /* replaced with offset to start of .plt. */
574 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
575 for the PLTResolve stub and then for each PLT entry. */
576 #define PLTRESOLVE_RELOCS_SHLIB 0
577 #define PLTRESOLVE_RELOCS 2
578 #define PLT_NON_JUMP_SLOT_RELOCS 2
580 /* The i386 linker needs to keep track of the number of relocs that it
581 decides to copy as dynamic relocs in check_relocs for each symbol.
582 This is so that it can later discard them if they are found to be
583 unnecessary. We store the information in a field extending the
584 regular ELF linker hash table. */
586 struct elf_i386_dyn_relocs
588 struct elf_i386_dyn_relocs *next;
590 /* The input section of the reloc. */
591 asection *sec;
593 /* Total number of relocs copied for the input section. */
594 bfd_size_type count;
596 /* Number of pc-relative relocs copied for the input section. */
597 bfd_size_type pc_count;
600 /* i386 ELF linker hash entry. */
602 struct elf_i386_link_hash_entry
604 struct elf_link_hash_entry elf;
606 /* Track dynamic relocs copied for this symbol. */
607 struct elf_i386_dyn_relocs *dyn_relocs;
609 #define GOT_UNKNOWN 0
610 #define GOT_NORMAL 1
611 #define GOT_TLS_GD 2
612 #define GOT_TLS_IE 4
613 #define GOT_TLS_IE_POS 5
614 #define GOT_TLS_IE_NEG 6
615 #define GOT_TLS_IE_BOTH 7
616 #define GOT_TLS_GDESC 8
617 #define GOT_TLS_GD_BOTH_P(type) \
618 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
619 #define GOT_TLS_GD_P(type) \
620 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
621 #define GOT_TLS_GDESC_P(type) \
622 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
623 #define GOT_TLS_GD_ANY_P(type) \
624 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
625 unsigned char tls_type;
627 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
628 starting at the end of the jump table. */
629 bfd_vma tlsdesc_got;
632 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
634 struct elf_i386_obj_tdata
636 struct elf_obj_tdata root;
638 /* tls_type for each local got entry. */
639 char *local_got_tls_type;
641 /* GOTPLT entries for TLS descriptors. */
642 bfd_vma *local_tlsdesc_gotent;
645 #define elf_i386_tdata(abfd) \
646 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
648 #define elf_i386_local_got_tls_type(abfd) \
649 (elf_i386_tdata (abfd)->local_got_tls_type)
651 #define elf_i386_local_tlsdesc_gotent(abfd) \
652 (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
654 #define is_i386_elf(bfd) \
655 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
656 && elf_tdata (bfd) != NULL \
657 && elf_object_id (bfd) == I386_ELF_TDATA)
659 static bfd_boolean
660 elf_i386_mkobject (bfd *abfd)
662 return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
663 I386_ELF_TDATA);
666 /* i386 ELF linker hash table. */
668 struct elf_i386_link_hash_table
670 struct elf_link_hash_table elf;
672 /* Short-cuts to get to dynamic linker sections. */
673 asection *sdynbss;
674 asection *srelbss;
676 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
677 asection *srelplt2;
679 /* True if the target system is VxWorks. */
680 int is_vxworks;
682 /* Value used to fill the last word of the first plt entry. */
683 bfd_byte plt0_pad_byte;
685 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
686 bfd_vma next_tls_desc_index;
688 union {
689 bfd_signed_vma refcount;
690 bfd_vma offset;
691 } tls_ldm_got;
693 /* The amount of space used by the reserved portion of the sgotplt
694 section, plus whatever space is used by the jump slots. */
695 bfd_vma sgotplt_jump_table_size;
697 /* Small local sym to section mapping cache. */
698 struct sym_sec_cache sym_sec;
700 /* _TLS_MODULE_BASE_ symbol. */
701 struct bfd_link_hash_entry *tls_module_base;
703 /* Used by local STT_GNU_IFUNC symbols. */
704 htab_t loc_hash_table;
705 void *loc_hash_memory;
708 /* Get the i386 ELF linker hash table from a link_info structure. */
710 #define elf_i386_hash_table(p) \
711 ((struct elf_i386_link_hash_table *) ((p)->hash))
713 #define elf_i386_compute_jump_table_size(htab) \
714 ((htab)->next_tls_desc_index * 4)
716 /* Create an entry in an i386 ELF linker hash table. */
718 static struct bfd_hash_entry *
719 elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
720 struct bfd_hash_table *table,
721 const char *string)
723 /* Allocate the structure if it has not already been allocated by a
724 subclass. */
725 if (entry == NULL)
727 entry = bfd_hash_allocate (table,
728 sizeof (struct elf_i386_link_hash_entry));
729 if (entry == NULL)
730 return entry;
733 /* Call the allocation method of the superclass. */
734 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
735 if (entry != NULL)
737 struct elf_i386_link_hash_entry *eh;
739 eh = (struct elf_i386_link_hash_entry *) entry;
740 eh->dyn_relocs = NULL;
741 eh->tls_type = GOT_UNKNOWN;
742 eh->tlsdesc_got = (bfd_vma) -1;
745 return entry;
748 static hashval_t
749 elf_i386_local_hash (int id, int r_sym)
751 return ((((id & 0xff) << 24) | ((id & 0xff00) << 8))
752 ^ r_sym ^ (id >> 16));
755 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
756 for local symbol so that we can handle local STT_GNU_IFUNC symbols
757 as global symbol. We reuse indx and dynstr_index for local symbol
758 hash since they aren't used by global symbols in this backend. */
760 static hashval_t
761 elf_i386_local_htab_hash (const void *ptr)
763 struct elf_link_hash_entry *h
764 = (struct elf_link_hash_entry *) ptr;
765 return elf_i386_local_hash (h->indx, h->dynstr_index);
768 /* Compare local hash entries. */
770 static int
771 elf_i386_local_htab_eq (const void *ptr1, const void *ptr2)
773 struct elf_link_hash_entry *h1
774 = (struct elf_link_hash_entry *) ptr1;
775 struct elf_link_hash_entry *h2
776 = (struct elf_link_hash_entry *) ptr2;
778 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
781 /* Find and/or create a hash entry for local symbol. */
783 static struct elf_link_hash_entry *
784 elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab,
785 bfd *abfd, const Elf_Internal_Rela *rel,
786 bfd_boolean create)
788 struct elf_i386_link_hash_entry e, *ret;
789 asection *sec = abfd->sections;
790 hashval_t h = elf_i386_local_hash (sec->id,
791 ELF32_R_SYM (rel->r_info));
792 void **slot;
794 e.elf.indx = sec->id;
795 e.elf.dynstr_index = ELF32_R_SYM (rel->r_info);
796 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
797 create ? INSERT : NO_INSERT);
799 if (!slot)
800 return NULL;
802 if (*slot)
804 ret = (struct elf_i386_link_hash_entry *) *slot;
805 return &ret->elf;
808 ret = (struct elf_i386_link_hash_entry *)
809 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
810 sizeof (struct elf_i386_link_hash_entry));
811 if (ret)
813 memset (ret, 0, sizeof (*ret));
814 ret->elf.indx = sec->id;
815 ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
816 ret->elf.dynindx = -1;
817 ret->elf.plt.offset = (bfd_vma) -1;
818 ret->elf.got.offset = (bfd_vma) -1;
819 *slot = ret;
821 return &ret->elf;
824 /* Create an i386 ELF linker hash table. */
826 static struct bfd_link_hash_table *
827 elf_i386_link_hash_table_create (bfd *abfd)
829 struct elf_i386_link_hash_table *ret;
830 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
832 ret = bfd_malloc (amt);
833 if (ret == NULL)
834 return NULL;
836 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
837 elf_i386_link_hash_newfunc,
838 sizeof (struct elf_i386_link_hash_entry)))
840 free (ret);
841 return NULL;
844 ret->sdynbss = NULL;
845 ret->srelbss = NULL;
846 ret->tls_ldm_got.refcount = 0;
847 ret->next_tls_desc_index = 0;
848 ret->sgotplt_jump_table_size = 0;
849 ret->sym_sec.abfd = NULL;
850 ret->is_vxworks = 0;
851 ret->srelplt2 = NULL;
852 ret->plt0_pad_byte = 0;
853 ret->tls_module_base = NULL;
855 ret->loc_hash_table = htab_try_create (1024,
856 elf_i386_local_htab_hash,
857 elf_i386_local_htab_eq,
858 NULL);
859 ret->loc_hash_memory = objalloc_create ();
860 if (!ret->loc_hash_table || !ret->loc_hash_memory)
862 free (ret);
863 return NULL;
866 return &ret->elf.root;
869 /* Destroy an i386 ELF linker hash table. */
871 static void
872 elf_i386_link_hash_table_free (struct bfd_link_hash_table *hash)
874 struct elf_i386_link_hash_table *htab
875 = (struct elf_i386_link_hash_table *) hash;
877 if (htab->loc_hash_table)
878 htab_delete (htab->loc_hash_table);
879 if (htab->loc_hash_memory)
880 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
881 _bfd_generic_link_hash_table_free (hash);
884 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
885 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
886 hash table. */
888 static bfd_boolean
889 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
891 struct elf_i386_link_hash_table *htab;
893 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
894 return FALSE;
896 htab = elf_i386_hash_table (info);
897 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
898 if (!info->shared)
899 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
901 if (!htab->sdynbss
902 || (!info->shared && !htab->srelbss))
903 abort ();
905 if (htab->is_vxworks
906 && !elf_vxworks_create_dynamic_sections (dynobj, info,
907 &htab->srelplt2))
908 return FALSE;
910 return TRUE;
913 /* Copy the extra info we tack onto an elf_link_hash_entry. */
915 static void
916 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
917 struct elf_link_hash_entry *dir,
918 struct elf_link_hash_entry *ind)
920 struct elf_i386_link_hash_entry *edir, *eind;
922 edir = (struct elf_i386_link_hash_entry *) dir;
923 eind = (struct elf_i386_link_hash_entry *) ind;
925 if (eind->dyn_relocs != NULL)
927 if (edir->dyn_relocs != NULL)
929 struct elf_i386_dyn_relocs **pp;
930 struct elf_i386_dyn_relocs *p;
932 /* Add reloc counts against the indirect sym to the direct sym
933 list. Merge any entries against the same section. */
934 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
936 struct elf_i386_dyn_relocs *q;
938 for (q = edir->dyn_relocs; q != NULL; q = q->next)
939 if (q->sec == p->sec)
941 q->pc_count += p->pc_count;
942 q->count += p->count;
943 *pp = p->next;
944 break;
946 if (q == NULL)
947 pp = &p->next;
949 *pp = edir->dyn_relocs;
952 edir->dyn_relocs = eind->dyn_relocs;
953 eind->dyn_relocs = NULL;
956 if (ind->root.type == bfd_link_hash_indirect
957 && dir->got.refcount <= 0)
959 edir->tls_type = eind->tls_type;
960 eind->tls_type = GOT_UNKNOWN;
963 if (ELIMINATE_COPY_RELOCS
964 && ind->root.type != bfd_link_hash_indirect
965 && dir->dynamic_adjusted)
967 /* If called to transfer flags for a weakdef during processing
968 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
969 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
970 dir->ref_dynamic |= ind->ref_dynamic;
971 dir->ref_regular |= ind->ref_regular;
972 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
973 dir->needs_plt |= ind->needs_plt;
974 dir->pointer_equality_needed |= ind->pointer_equality_needed;
976 else
977 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
980 typedef union
982 unsigned char c[2];
983 uint16_t i;
985 i386_opcode16;
987 /* Return TRUE if the TLS access code sequence support transition
988 from R_TYPE. */
990 static bfd_boolean
991 elf_i386_check_tls_transition (bfd *abfd, asection *sec,
992 bfd_byte *contents,
993 Elf_Internal_Shdr *symtab_hdr,
994 struct elf_link_hash_entry **sym_hashes,
995 unsigned int r_type,
996 const Elf_Internal_Rela *rel,
997 const Elf_Internal_Rela *relend)
999 unsigned int val, type;
1000 unsigned long r_symndx;
1001 struct elf_link_hash_entry *h;
1002 bfd_vma offset;
1004 /* Get the section contents. */
1005 if (contents == NULL)
1007 if (elf_section_data (sec)->this_hdr.contents != NULL)
1008 contents = elf_section_data (sec)->this_hdr.contents;
1009 else
1011 /* FIXME: How to better handle error condition? */
1012 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1013 return FALSE;
1015 /* Cache the section contents for elf_link_input_bfd. */
1016 elf_section_data (sec)->this_hdr.contents = contents;
1020 offset = rel->r_offset;
1021 switch (r_type)
1023 case R_386_TLS_GD:
1024 case R_386_TLS_LDM:
1025 if (offset < 2 || (rel + 1) >= relend)
1026 return FALSE;
1028 type = bfd_get_8 (abfd, contents + offset - 2);
1029 if (r_type == R_386_TLS_GD)
1031 /* Check transition from GD access model. Only
1032 leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
1033 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
1034 can transit to different access model. */
1035 if ((offset + 10) > sec->size ||
1036 (type != 0x8d && type != 0x04))
1037 return FALSE;
1039 val = bfd_get_8 (abfd, contents + offset - 1);
1040 if (type == 0x04)
1042 /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
1043 if (offset < 3)
1044 return FALSE;
1046 if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
1047 return FALSE;
1049 if ((val & 0xc7) != 0x05 || val == (4 << 3))
1050 return FALSE;
1052 else
1054 /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop */
1055 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
1056 return FALSE;
1058 if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
1059 return FALSE;
1062 else
1064 /* Check transition from LD access model. Only
1065 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
1066 can transit to different access model. */
1067 if (type != 0x8d || (offset + 9) > sec->size)
1068 return FALSE;
1070 val = bfd_get_8 (abfd, contents + offset - 1);
1071 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
1072 return FALSE;
1075 if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
1076 return FALSE;
1078 r_symndx = ELF32_R_SYM (rel[1].r_info);
1079 if (r_symndx < symtab_hdr->sh_info)
1080 return FALSE;
1082 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1083 /* Use strncmp to check ___tls_get_addr since ___tls_get_addr
1084 may be versioned. */
1085 return (h != NULL
1086 && h->root.root.string != NULL
1087 && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1088 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
1089 && (strncmp (h->root.root.string, "___tls_get_addr",
1090 15) == 0));
1092 case R_386_TLS_IE:
1093 /* Check transition from IE access model:
1094 movl foo@indntpoff(%rip), %eax
1095 movl foo@indntpoff(%rip), %reg
1096 addl foo@indntpoff(%rip), %reg
1099 if (offset < 1 || (offset + 4) > sec->size)
1100 return FALSE;
1102 /* Check "movl foo@tpoff(%rip), %eax" first. */
1103 val = bfd_get_8 (abfd, contents + offset - 1);
1104 if (val == 0xa1)
1105 return TRUE;
1107 if (offset < 2)
1108 return FALSE;
1110 /* Check movl|addl foo@tpoff(%rip), %reg. */
1111 type = bfd_get_8 (abfd, contents + offset - 2);
1112 return ((type == 0x8b || type == 0x03)
1113 && (val & 0xc7) == 0x05);
1115 case R_386_TLS_GOTIE:
1116 case R_386_TLS_IE_32:
1117 /* Check transition from {IE_32,GOTIE} access model:
1118 subl foo@{tpoff,gontoff}(%reg1), %reg2
1119 movl foo@{tpoff,gontoff}(%reg1), %reg2
1120 addl foo@{tpoff,gontoff}(%reg1), %reg2
1123 if (offset < 2 || (offset + 4) > sec->size)
1124 return FALSE;
1126 val = bfd_get_8 (abfd, contents + offset - 1);
1127 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1128 return FALSE;
1130 type = bfd_get_8 (abfd, contents + offset - 2);
1131 return type == 0x8b || type == 0x2b || type == 0x03;
1133 case R_386_TLS_GOTDESC:
1134 /* Check transition from GDesc access model:
1135 leal x@tlsdesc(%ebx), %eax
1137 Make sure it's a leal adding ebx to a 32-bit offset
1138 into any register, although it's probably almost always
1139 going to be eax. */
1141 if (offset < 2 || (offset + 4) > sec->size)
1142 return FALSE;
1144 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1145 return FALSE;
1147 val = bfd_get_8 (abfd, contents + offset - 1);
1148 return (val & 0xc7) == 0x83;
1150 case R_386_TLS_DESC_CALL:
1151 /* Check transition from GDesc access model:
1152 call *x@tlsdesc(%rax)
1154 if (offset + 2 <= sec->size)
1156 /* Make sure that it's a call *x@tlsdesc(%rax). */
1157 static i386_opcode16 call = { { 0xff, 0x10 } };
1158 return bfd_get_16 (abfd, contents + offset) == call.i;
1161 return FALSE;
1163 default:
1164 abort ();
1168 /* Return TRUE if the TLS access transition is OK or no transition
1169 will be performed. Update R_TYPE if there is a transition. */
1171 static bfd_boolean
1172 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1173 asection *sec, bfd_byte *contents,
1174 Elf_Internal_Shdr *symtab_hdr,
1175 struct elf_link_hash_entry **sym_hashes,
1176 unsigned int *r_type, int tls_type,
1177 const Elf_Internal_Rela *rel,
1178 const Elf_Internal_Rela *relend,
1179 struct elf_link_hash_entry *h)
1181 unsigned int from_type = *r_type;
1182 unsigned int to_type = from_type;
1183 bfd_boolean check = TRUE;
1185 switch (from_type)
1187 case R_386_TLS_GD:
1188 case R_386_TLS_GOTDESC:
1189 case R_386_TLS_DESC_CALL:
1190 case R_386_TLS_IE_32:
1191 case R_386_TLS_IE:
1192 case R_386_TLS_GOTIE:
1193 if (!info->shared)
1195 if (h == NULL)
1196 to_type = R_386_TLS_LE_32;
1197 else if (from_type != R_386_TLS_IE
1198 && from_type != R_386_TLS_GOTIE)
1199 to_type = R_386_TLS_IE_32;
1202 /* When we are called from elf_i386_relocate_section, CONTENTS
1203 isn't NULL and there may be additional transitions based on
1204 TLS_TYPE. */
1205 if (contents != NULL)
1207 unsigned int new_to_type = to_type;
1209 if (!info->shared
1210 && h != NULL
1211 && h->dynindx == -1
1212 && (tls_type & GOT_TLS_IE))
1213 new_to_type = R_386_TLS_LE_32;
1215 if (to_type == R_386_TLS_GD
1216 || to_type == R_386_TLS_GOTDESC
1217 || to_type == R_386_TLS_DESC_CALL)
1219 if (tls_type == GOT_TLS_IE_POS)
1220 new_to_type = R_386_TLS_GOTIE;
1221 else if (tls_type & GOT_TLS_IE)
1222 new_to_type = R_386_TLS_IE_32;
1225 /* We checked the transition before when we were called from
1226 elf_i386_check_relocs. We only want to check the new
1227 transition which hasn't been checked before. */
1228 check = new_to_type != to_type && from_type == to_type;
1229 to_type = new_to_type;
1232 break;
1234 case R_386_TLS_LDM:
1235 if (!info->shared)
1236 to_type = R_386_TLS_LE_32;
1237 break;
1239 default:
1240 return TRUE;
1243 /* Return TRUE if there is no transition. */
1244 if (from_type == to_type)
1245 return TRUE;
1247 /* Check if the transition can be performed. */
1248 if (check
1249 && ! elf_i386_check_tls_transition (abfd, sec, contents,
1250 symtab_hdr, sym_hashes,
1251 from_type, rel, relend))
1253 reloc_howto_type *from, *to;
1255 from = elf_i386_rtype_to_howto (abfd, from_type);
1256 to = elf_i386_rtype_to_howto (abfd, to_type);
1258 (*_bfd_error_handler)
1259 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1260 "in section `%A' failed"),
1261 abfd, sec, from->name, to->name,
1262 h ? h->root.root.string : "a local symbol",
1263 (unsigned long) rel->r_offset);
1264 bfd_set_error (bfd_error_bad_value);
1265 return FALSE;
1268 *r_type = to_type;
1269 return TRUE;
1272 /* Look through the relocs for a section during the first phase, and
1273 calculate needed space in the global offset table, procedure linkage
1274 table, and dynamic reloc sections. */
1276 static bfd_boolean
1277 elf_i386_check_relocs (bfd *abfd,
1278 struct bfd_link_info *info,
1279 asection *sec,
1280 const Elf_Internal_Rela *relocs)
1282 struct elf_i386_link_hash_table *htab;
1283 Elf_Internal_Shdr *symtab_hdr;
1284 struct elf_link_hash_entry **sym_hashes;
1285 const Elf_Internal_Rela *rel;
1286 const Elf_Internal_Rela *rel_end;
1287 asection *sreloc;
1288 Elf_Internal_Sym *isymbuf;
1290 if (info->relocatable)
1291 return TRUE;
1293 BFD_ASSERT (is_i386_elf (abfd));
1295 htab = elf_i386_hash_table (info);
1296 symtab_hdr = &elf_symtab_hdr (abfd);
1297 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1298 sym_hashes = elf_sym_hashes (abfd);
1300 sreloc = NULL;
1302 rel_end = relocs + sec->reloc_count;
1303 for (rel = relocs; rel < rel_end; rel++)
1305 unsigned int r_type;
1306 unsigned long r_symndx;
1307 struct elf_link_hash_entry *h;
1309 r_symndx = ELF32_R_SYM (rel->r_info);
1310 r_type = ELF32_R_TYPE (rel->r_info);
1312 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1314 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1315 abfd,
1316 r_symndx);
1317 return FALSE;
1320 if (r_symndx < symtab_hdr->sh_info)
1322 /* A local symbol. */
1323 Elf_Internal_Sym *isym;
1325 /* Read this BFD's local symbols. */
1326 if (isymbuf == NULL)
1328 if (isymbuf == NULL)
1329 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1330 symtab_hdr->sh_info, 0,
1331 NULL, NULL, NULL);
1332 if (isymbuf == NULL)
1333 return FALSE;
1336 /* Check relocation against local STT_GNU_IFUNC symbol. */
1337 isym = isymbuf + r_symndx;
1338 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1340 h = elf_i386_get_local_sym_hash (htab, abfd, rel,
1341 TRUE);
1342 if (h == NULL)
1343 return FALSE;
1345 /* Fake a STT_GNU_IFUNC symbol. */
1346 h->type = STT_GNU_IFUNC;
1347 h->def_regular = 1;
1348 h->ref_regular = 1;
1349 h->forced_local = 1;
1350 h->root.type = bfd_link_hash_defined;
1352 else
1353 h = NULL;
1355 else
1357 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1358 while (h->root.type == bfd_link_hash_indirect
1359 || h->root.type == bfd_link_hash_warning)
1360 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1363 if (h != NULL)
1365 /* Create the ifunc sections for static executables. If we
1366 never see an indirect function symbol nor we are building
1367 a static executable, those sections will be empty and
1368 won't appear in output. */
1369 switch (r_type)
1371 default:
1372 break;
1374 case R_386_32:
1375 case R_386_PC32:
1376 case R_386_PLT32:
1377 case R_386_GOT32:
1378 case R_386_GOTOFF:
1379 if (!_bfd_elf_create_ifunc_sections (abfd, info))
1380 return FALSE;
1381 break;
1384 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1385 it here if it is defined in a non-shared object. */
1386 if (h->type == STT_GNU_IFUNC
1387 && h->def_regular)
1389 /* It is referenced by a non-shared object. */
1390 h->ref_regular = 1;
1392 /* STT_GNU_IFUNC symbol must go through PLT. */
1393 h->plt.refcount += 1;
1395 /* STT_GNU_IFUNC needs dynamic sections. */
1396 if (htab->elf.dynobj == NULL)
1397 htab->elf.dynobj = abfd;
1399 switch (r_type)
1401 default:
1402 (*_bfd_error_handler)
1403 (_("%B: relocation %s against STT_GNU_IFUNC "
1404 "symbol `%s' isn't handled by %s"), abfd,
1405 elf_howto_table[r_type].name,
1406 (h->root.root.string
1407 ? h->root.root.string : "a local symbol"),
1408 __FUNCTION__);
1409 bfd_set_error (bfd_error_bad_value);
1410 return FALSE;
1412 case R_386_32:
1413 h->non_got_ref = 1;
1414 h->pointer_equality_needed = 1;
1415 if (info->shared)
1417 struct elf_i386_dyn_relocs *p;
1418 struct elf_i386_dyn_relocs **head;
1420 /* We must copy these reloc types into the
1421 output file. Create a reloc section in
1422 dynobj and make room for this reloc. */
1423 if (sreloc == NULL)
1425 if (htab->elf.dynobj == NULL)
1426 htab->elf.dynobj = abfd;
1428 sreloc = _bfd_elf_make_dynamic_reloc_section
1429 (sec, htab->elf.dynobj, 2, abfd, FALSE);
1431 if (sreloc == NULL)
1432 return FALSE;
1435 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1436 p = *head;
1437 if (p == NULL || p->sec != sec)
1439 bfd_size_type amt = sizeof *p;
1440 p = bfd_alloc (htab->elf.dynobj, amt);
1441 if (p == NULL)
1442 return FALSE;
1443 p->next = *head;
1444 *head = p;
1445 p->sec = sec;
1446 p->count = 0;
1447 p->pc_count = 0;
1449 p->count += 1;
1451 break;
1453 case R_386_PC32:
1454 h->non_got_ref = 1;
1455 break;
1457 case R_386_PLT32:
1458 break;
1460 case R_386_GOT32:
1461 case R_386_GOTOFF:
1462 h->got.refcount += 1;
1463 if (htab->elf.sgot == NULL
1464 && !_bfd_elf_create_got_section (htab->elf.dynobj,
1465 info))
1466 return FALSE;
1467 break;
1470 continue;
1474 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1475 symtab_hdr, sym_hashes,
1476 &r_type, GOT_UNKNOWN,
1477 rel, rel_end, h))
1478 return FALSE;
1480 switch (r_type)
1482 case R_386_TLS_LDM:
1483 htab->tls_ldm_got.refcount += 1;
1484 goto create_got;
1486 case R_386_PLT32:
1487 /* This symbol requires a procedure linkage table entry. We
1488 actually build the entry in adjust_dynamic_symbol,
1489 because this might be a case of linking PIC code which is
1490 never referenced by a dynamic object, in which case we
1491 don't need to generate a procedure linkage table entry
1492 after all. */
1494 /* If this is a local symbol, we resolve it directly without
1495 creating a procedure linkage table entry. */
1496 if (h == NULL)
1497 continue;
1499 h->needs_plt = 1;
1500 h->plt.refcount += 1;
1501 break;
1503 case R_386_TLS_IE_32:
1504 case R_386_TLS_IE:
1505 case R_386_TLS_GOTIE:
1506 if (info->shared)
1507 info->flags |= DF_STATIC_TLS;
1508 /* Fall through */
1510 case R_386_GOT32:
1511 case R_386_TLS_GD:
1512 case R_386_TLS_GOTDESC:
1513 case R_386_TLS_DESC_CALL:
1514 /* This symbol requires a global offset table entry. */
1516 int tls_type, old_tls_type;
1518 switch (r_type)
1520 default:
1521 case R_386_GOT32: tls_type = GOT_NORMAL; break;
1522 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1523 case R_386_TLS_GOTDESC:
1524 case R_386_TLS_DESC_CALL:
1525 tls_type = GOT_TLS_GDESC; break;
1526 case R_386_TLS_IE_32:
1527 if (ELF32_R_TYPE (rel->r_info) == r_type)
1528 tls_type = GOT_TLS_IE_NEG;
1529 else
1530 /* If this is a GD->IE transition, we may use either of
1531 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1532 tls_type = GOT_TLS_IE;
1533 break;
1534 case R_386_TLS_IE:
1535 case R_386_TLS_GOTIE:
1536 tls_type = GOT_TLS_IE_POS; break;
1539 if (h != NULL)
1541 h->got.refcount += 1;
1542 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1544 else
1546 bfd_signed_vma *local_got_refcounts;
1548 /* This is a global offset table entry for a local symbol. */
1549 local_got_refcounts = elf_local_got_refcounts (abfd);
1550 if (local_got_refcounts == NULL)
1552 bfd_size_type size;
1554 size = symtab_hdr->sh_info;
1555 size *= (sizeof (bfd_signed_vma)
1556 + sizeof (bfd_vma) + sizeof(char));
1557 local_got_refcounts = bfd_zalloc (abfd, size);
1558 if (local_got_refcounts == NULL)
1559 return FALSE;
1560 elf_local_got_refcounts (abfd) = local_got_refcounts;
1561 elf_i386_local_tlsdesc_gotent (abfd)
1562 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1563 elf_i386_local_got_tls_type (abfd)
1564 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1566 local_got_refcounts[r_symndx] += 1;
1567 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1570 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1571 tls_type |= old_tls_type;
1572 /* If a TLS symbol is accessed using IE at least once,
1573 there is no point to use dynamic model for it. */
1574 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1575 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1576 || (tls_type & GOT_TLS_IE) == 0))
1578 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1579 tls_type = old_tls_type;
1580 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1581 && GOT_TLS_GD_ANY_P (tls_type))
1582 tls_type |= old_tls_type;
1583 else
1585 (*_bfd_error_handler)
1586 (_("%B: `%s' accessed both as normal and "
1587 "thread local symbol"),
1588 abfd,
1589 h ? h->root.root.string : "<local>");
1590 return FALSE;
1594 if (old_tls_type != tls_type)
1596 if (h != NULL)
1597 elf_i386_hash_entry (h)->tls_type = tls_type;
1598 else
1599 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1602 /* Fall through */
1604 case R_386_GOTOFF:
1605 case R_386_GOTPC:
1606 create_got:
1607 if (htab->elf.sgot == NULL)
1609 if (htab->elf.dynobj == NULL)
1610 htab->elf.dynobj = abfd;
1611 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
1612 return FALSE;
1614 if (r_type != R_386_TLS_IE)
1615 break;
1616 /* Fall through */
1618 case R_386_TLS_LE_32:
1619 case R_386_TLS_LE:
1620 if (!info->shared)
1621 break;
1622 info->flags |= DF_STATIC_TLS;
1623 /* Fall through */
1625 case R_386_32:
1626 case R_386_PC32:
1627 if (h != NULL && info->executable)
1629 /* If this reloc is in a read-only section, we might
1630 need a copy reloc. We can't check reliably at this
1631 stage whether the section is read-only, as input
1632 sections have not yet been mapped to output sections.
1633 Tentatively set the flag for now, and correct in
1634 adjust_dynamic_symbol. */
1635 h->non_got_ref = 1;
1637 /* We may need a .plt entry if the function this reloc
1638 refers to is in a shared lib. */
1639 h->plt.refcount += 1;
1640 if (r_type != R_386_PC32)
1641 h->pointer_equality_needed = 1;
1644 /* If we are creating a shared library, and this is a reloc
1645 against a global symbol, or a non PC relative reloc
1646 against a local symbol, then we need to copy the reloc
1647 into the shared library. However, if we are linking with
1648 -Bsymbolic, we do not need to copy a reloc against a
1649 global symbol which is defined in an object we are
1650 including in the link (i.e., DEF_REGULAR is set). At
1651 this point we have not seen all the input files, so it is
1652 possible that DEF_REGULAR is not set now but will be set
1653 later (it is never cleared). In case of a weak definition,
1654 DEF_REGULAR may be cleared later by a strong definition in
1655 a shared library. We account for that possibility below by
1656 storing information in the relocs_copied field of the hash
1657 table entry. A similar situation occurs when creating
1658 shared libraries and symbol visibility changes render the
1659 symbol local.
1661 If on the other hand, we are creating an executable, we
1662 may need to keep relocations for symbols satisfied by a
1663 dynamic library if we manage to avoid copy relocs for the
1664 symbol. */
1665 if ((info->shared
1666 && (sec->flags & SEC_ALLOC) != 0
1667 && (r_type != R_386_PC32
1668 || (h != NULL
1669 && (! SYMBOLIC_BIND (info, h)
1670 || h->root.type == bfd_link_hash_defweak
1671 || !h->def_regular))))
1672 || (ELIMINATE_COPY_RELOCS
1673 && !info->shared
1674 && (sec->flags & SEC_ALLOC) != 0
1675 && h != NULL
1676 && (h->root.type == bfd_link_hash_defweak
1677 || !h->def_regular)))
1679 struct elf_i386_dyn_relocs *p;
1680 struct elf_i386_dyn_relocs **head;
1682 /* We must copy these reloc types into the output file.
1683 Create a reloc section in dynobj and make room for
1684 this reloc. */
1685 if (sreloc == NULL)
1687 if (htab->elf.dynobj == NULL)
1688 htab->elf.dynobj = abfd;
1690 sreloc = _bfd_elf_make_dynamic_reloc_section
1691 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1693 if (sreloc == NULL)
1694 return FALSE;
1697 /* If this is a global symbol, we count the number of
1698 relocations we need for this symbol. */
1699 if (h != NULL)
1701 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1703 else
1705 void **vpp;
1706 /* Track dynamic relocs needed for local syms too.
1707 We really need local syms available to do this
1708 easily. Oh well. */
1710 asection *s;
1711 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1712 sec, r_symndx);
1713 if (s == NULL)
1714 return FALSE;
1716 vpp = &elf_section_data (s)->local_dynrel;
1717 head = (struct elf_i386_dyn_relocs **)vpp;
1720 p = *head;
1721 if (p == NULL || p->sec != sec)
1723 bfd_size_type amt = sizeof *p;
1724 p = bfd_alloc (htab->elf.dynobj, amt);
1725 if (p == NULL)
1726 return FALSE;
1727 p->next = *head;
1728 *head = p;
1729 p->sec = sec;
1730 p->count = 0;
1731 p->pc_count = 0;
1734 p->count += 1;
1735 if (r_type == R_386_PC32)
1736 p->pc_count += 1;
1738 break;
1740 /* This relocation describes the C++ object vtable hierarchy.
1741 Reconstruct it for later use during GC. */
1742 case R_386_GNU_VTINHERIT:
1743 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1744 return FALSE;
1745 break;
1747 /* This relocation describes which C++ vtable entries are actually
1748 used. Record for later use during GC. */
1749 case R_386_GNU_VTENTRY:
1750 BFD_ASSERT (h != NULL);
1751 if (h != NULL
1752 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1753 return FALSE;
1754 break;
1756 default:
1757 break;
1761 return TRUE;
1764 /* Return the section that should be marked against GC for a given
1765 relocation. */
1767 static asection *
1768 elf_i386_gc_mark_hook (asection *sec,
1769 struct bfd_link_info *info,
1770 Elf_Internal_Rela *rel,
1771 struct elf_link_hash_entry *h,
1772 Elf_Internal_Sym *sym)
1774 if (h != NULL)
1775 switch (ELF32_R_TYPE (rel->r_info))
1777 case R_386_GNU_VTINHERIT:
1778 case R_386_GNU_VTENTRY:
1779 return NULL;
1782 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1785 /* Update the got entry reference counts for the section being removed. */
1787 static bfd_boolean
1788 elf_i386_gc_sweep_hook (bfd *abfd,
1789 struct bfd_link_info *info,
1790 asection *sec,
1791 const Elf_Internal_Rela *relocs)
1793 Elf_Internal_Shdr *symtab_hdr;
1794 struct elf_link_hash_entry **sym_hashes;
1795 bfd_signed_vma *local_got_refcounts;
1796 const Elf_Internal_Rela *rel, *relend;
1798 if (info->relocatable)
1799 return TRUE;
1801 elf_section_data (sec)->local_dynrel = NULL;
1803 symtab_hdr = &elf_symtab_hdr (abfd);
1804 sym_hashes = elf_sym_hashes (abfd);
1805 local_got_refcounts = elf_local_got_refcounts (abfd);
1807 relend = relocs + sec->reloc_count;
1808 for (rel = relocs; rel < relend; rel++)
1810 unsigned long r_symndx;
1811 unsigned int r_type;
1812 struct elf_link_hash_entry *h = NULL;
1814 r_symndx = ELF32_R_SYM (rel->r_info);
1815 if (r_symndx >= symtab_hdr->sh_info)
1817 struct elf_i386_link_hash_entry *eh;
1818 struct elf_i386_dyn_relocs **pp;
1819 struct elf_i386_dyn_relocs *p;
1821 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1822 while (h->root.type == bfd_link_hash_indirect
1823 || h->root.type == bfd_link_hash_warning)
1824 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1825 eh = (struct elf_i386_link_hash_entry *) h;
1827 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1828 if (p->sec == sec)
1830 /* Everything must go for SEC. */
1831 *pp = p->next;
1832 break;
1836 r_type = ELF32_R_TYPE (rel->r_info);
1837 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1838 symtab_hdr, sym_hashes,
1839 &r_type, GOT_UNKNOWN,
1840 rel, relend, h))
1841 return FALSE;
1843 switch (r_type)
1845 case R_386_TLS_LDM:
1846 if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1847 elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1848 break;
1850 case R_386_TLS_GD:
1851 case R_386_TLS_GOTDESC:
1852 case R_386_TLS_DESC_CALL:
1853 case R_386_TLS_IE_32:
1854 case R_386_TLS_IE:
1855 case R_386_TLS_GOTIE:
1856 case R_386_GOT32:
1857 if (h != NULL)
1859 if (h->got.refcount > 0)
1860 h->got.refcount -= 1;
1862 else if (local_got_refcounts != NULL)
1864 if (local_got_refcounts[r_symndx] > 0)
1865 local_got_refcounts[r_symndx] -= 1;
1867 break;
1869 case R_386_32:
1870 case R_386_PC32:
1871 if (info->shared)
1872 break;
1873 /* Fall through */
1875 case R_386_PLT32:
1876 if (h != NULL)
1878 if (h->plt.refcount > 0)
1879 h->plt.refcount -= 1;
1881 break;
1883 default:
1884 break;
1888 return TRUE;
1891 /* Adjust a symbol defined by a dynamic object and referenced by a
1892 regular object. The current definition is in some section of the
1893 dynamic object, but we're not including those sections. We have to
1894 change the definition to something the rest of the link can
1895 understand. */
1897 static bfd_boolean
1898 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1899 struct elf_link_hash_entry *h)
1901 struct elf_i386_link_hash_table *htab;
1902 asection *s;
1904 /* STT_GNU_IFUNC symbol must go through PLT. */
1905 if (h->type == STT_GNU_IFUNC)
1907 if (h->plt.refcount <= 0)
1909 h->plt.offset = (bfd_vma) -1;
1910 h->needs_plt = 0;
1912 return TRUE;
1915 /* If this is a function, put it in the procedure linkage table. We
1916 will fill in the contents of the procedure linkage table later,
1917 when we know the address of the .got section. */
1918 if (h->type == STT_FUNC
1919 || h->needs_plt)
1921 if (h->plt.refcount <= 0
1922 || SYMBOL_CALLS_LOCAL (info, h)
1923 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1924 && h->root.type == bfd_link_hash_undefweak))
1926 /* This case can occur if we saw a PLT32 reloc in an input
1927 file, but the symbol was never referred to by a dynamic
1928 object, or if all references were garbage collected. In
1929 such a case, we don't actually need to build a procedure
1930 linkage table, and we can just do a PC32 reloc instead. */
1931 h->plt.offset = (bfd_vma) -1;
1932 h->needs_plt = 0;
1935 return TRUE;
1937 else
1938 /* It's possible that we incorrectly decided a .plt reloc was
1939 needed for an R_386_PC32 reloc to a non-function sym in
1940 check_relocs. We can't decide accurately between function and
1941 non-function syms in check-relocs; Objects loaded later in
1942 the link may change h->type. So fix it now. */
1943 h->plt.offset = (bfd_vma) -1;
1945 /* If this is a weak symbol, and there is a real definition, the
1946 processor independent code will have arranged for us to see the
1947 real definition first, and we can just use the same value. */
1948 if (h->u.weakdef != NULL)
1950 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1951 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1952 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1953 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1954 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1955 h->non_got_ref = h->u.weakdef->non_got_ref;
1956 return TRUE;
1959 /* This is a reference to a symbol defined by a dynamic object which
1960 is not a function. */
1962 /* If we are creating a shared library, we must presume that the
1963 only references to the symbol are via the global offset table.
1964 For such cases we need not do anything here; the relocations will
1965 be handled correctly by relocate_section. */
1966 if (info->shared)
1967 return TRUE;
1969 /* If there are no references to this symbol that do not use the
1970 GOT, we don't need to generate a copy reloc. */
1971 if (!h->non_got_ref)
1972 return TRUE;
1974 /* If -z nocopyreloc was given, we won't generate them either. */
1975 if (info->nocopyreloc)
1977 h->non_got_ref = 0;
1978 return TRUE;
1981 htab = elf_i386_hash_table (info);
1983 /* If there aren't any dynamic relocs in read-only sections, then
1984 we can keep the dynamic relocs and avoid the copy reloc. This
1985 doesn't work on VxWorks, where we can not have dynamic relocations
1986 (other than copy and jump slot relocations) in an executable. */
1987 if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
1989 struct elf_i386_link_hash_entry * eh;
1990 struct elf_i386_dyn_relocs *p;
1992 eh = (struct elf_i386_link_hash_entry *) h;
1993 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1995 s = p->sec->output_section;
1996 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1997 break;
2000 if (p == NULL)
2002 h->non_got_ref = 0;
2003 return TRUE;
2007 if (h->size == 0)
2009 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
2010 h->root.root.string);
2011 return TRUE;
2014 /* We must allocate the symbol in our .dynbss section, which will
2015 become part of the .bss section of the executable. There will be
2016 an entry for this symbol in the .dynsym section. The dynamic
2017 object will contain position independent code, so all references
2018 from the dynamic object to this symbol will go through the global
2019 offset table. The dynamic linker will use the .dynsym entry to
2020 determine the address it must put in the global offset table, so
2021 both the dynamic object and the regular object will refer to the
2022 same memory location for the variable. */
2024 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
2025 copy the initial value out of the dynamic object and into the
2026 runtime process image. */
2027 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2029 htab->srelbss->size += sizeof (Elf32_External_Rel);
2030 h->needs_copy = 1;
2033 s = htab->sdynbss;
2035 return _bfd_elf_adjust_dynamic_copy (h, s);
2038 /* Allocate space in .plt, .got and associated reloc sections for
2039 dynamic relocs. */
2041 static bfd_boolean
2042 elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2044 struct bfd_link_info *info;
2045 struct elf_i386_link_hash_table *htab;
2046 struct elf_i386_link_hash_entry *eh;
2047 struct elf_i386_dyn_relocs *p;
2049 if (h->root.type == bfd_link_hash_indirect)
2050 return TRUE;
2052 if (h->root.type == bfd_link_hash_warning)
2053 /* When warning symbols are created, they **replace** the "real"
2054 entry in the hash table, thus we never get to see the real
2055 symbol in a hash traversal. So look at it now. */
2056 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2057 eh = (struct elf_i386_link_hash_entry *) h;
2059 info = (struct bfd_link_info *) inf;
2060 htab = elf_i386_hash_table (info);
2062 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2063 here if it is defined and referenced in a non-shared object. */
2064 if (h->type == STT_GNU_IFUNC
2065 && h->def_regular)
2067 asection *plt, *gotplt, *relplt;
2069 /* When a shared library references a STT_GNU_IFUNC symbol
2070 defined in executable, the address of the resolved function
2071 may be used. But in non-shared executable, the address of
2072 its .plt slot may be used. Pointer equality may not work
2073 correctly. PIE should be used if pointer equality is
2074 required here. */
2075 if (!info->shared
2076 && (h->dynindx != -1
2077 || info->export_dynamic)
2078 && h->pointer_equality_needed)
2080 info->callbacks->einfo
2081 (_("%F%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer "
2082 "equality in `%B' can not be used when making an "
2083 "executable; recompile with -fPIE and relink with -pie\n"),
2084 h->root.root.string,
2085 h->root.u.def.section->owner);
2086 bfd_set_error (bfd_error_bad_value);
2087 return FALSE;
2090 /* Return and discard space for dynamic relocations against it if
2091 it is never referenced in a non-shared object. */
2092 if (!h->ref_regular)
2094 if (h->plt.refcount > 0
2095 || h->got.refcount > 0)
2096 abort ();
2097 h->got.offset = (bfd_vma) -1;
2098 eh->dyn_relocs = NULL;
2099 return TRUE;
2102 /* When building a static executable, use .iplt, .igot.plt and
2103 .rel.iplt sections for STT_GNU_IFUNC symbols. */
2104 if (htab->elf.splt != NULL)
2106 plt = htab->elf.splt;
2107 gotplt = htab->elf.sgotplt;
2108 relplt = htab->elf.srelplt;
2110 /* If this is the first .plt entry, make room for the special
2111 first entry. */
2112 if (plt->size == 0)
2113 plt->size += PLT_ENTRY_SIZE;
2115 else
2117 plt = htab->elf.iplt;
2118 gotplt = htab->elf.igotplt;
2119 relplt = htab->elf.irelplt;
2122 /* Don't update value of STT_GNU_IFUNC symbol to PLT. We need
2123 the original value for R_386_IRELATIVE. */
2124 h->plt.offset = plt->size;
2126 /* Make room for this entry in the .plt/.iplt section. */
2127 plt->size += PLT_ENTRY_SIZE;
2129 /* We also need to make an entry in the .got.plt/.got.iplt
2130 section, which will be placed in the .got section by the
2131 linker script. */
2132 gotplt->size += 4;
2134 /* We also need to make an entry in the .rela.plt/.rela.iplt
2135 section. */
2136 relplt->size += sizeof (Elf32_External_Rel);
2137 relplt->reloc_count++;
2139 /* We need dynamic relocation for STT_GNU_IFUNC symbol only
2140 when there is a non-GOT reference in a shared object. */
2141 if (!info->shared
2142 || !h->non_got_ref)
2143 eh->dyn_relocs = NULL;
2145 /* Finally, allocate space. */
2146 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2147 htab->elf.irelifunc->size += p->count * sizeof (Elf32_External_Rel);
2149 /* For STT_GNU_IFUNC symbol, .got.plt has the real function
2150 addres and .got has the PLT entry adddress. We will load
2151 the GOT entry with the PLT entry in finish_dynamic_symbol if
2152 it is used. For branch, it uses .got.plt. For symbol value,
2153 1. Use .got.plt in a shared object if it is forced local or
2154 not dynamic.
2155 2. Use .got.plt in a non-shared object if pointer equality
2156 isn't needed.
2157 3. Use .got.plt in PIE.
2158 4. Use .got.plt if .got isn't used.
2159 5. Otherwise use .got so that it can be shared among different
2160 objects at run-time.
2161 We only need to relocate .got entry in shared object. */
2162 if ((info->shared
2163 && (h->dynindx == -1
2164 || h->forced_local))
2165 || (!info->shared
2166 && !h->pointer_equality_needed)
2167 || (info->executable && info->shared)
2168 || htab->elf.sgot == NULL)
2170 /* Use .got.plt. */
2171 h->got.offset = (bfd_vma) -1;
2173 else
2175 h->got.offset = htab->elf.sgot->size;
2176 htab->elf.sgot->size += 4;
2177 if (info->shared)
2178 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2181 return TRUE;
2183 else if (htab->elf.dynamic_sections_created
2184 && h->plt.refcount > 0)
2186 /* Make sure this symbol is output as a dynamic symbol.
2187 Undefined weak syms won't yet be marked as dynamic. */
2188 if (h->dynindx == -1
2189 && !h->forced_local)
2191 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2192 return FALSE;
2195 if (info->shared
2196 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2198 asection *s = htab->elf.splt;
2200 /* If this is the first .plt entry, make room for the special
2201 first entry. */
2202 if (s->size == 0)
2203 s->size += PLT_ENTRY_SIZE;
2205 h->plt.offset = s->size;
2207 /* If this symbol is not defined in a regular file, and we are
2208 not generating a shared library, then set the symbol to this
2209 location in the .plt. This is required to make function
2210 pointers compare as equal between the normal executable and
2211 the shared library. */
2212 if (! info->shared
2213 && !h->def_regular)
2215 h->root.u.def.section = s;
2216 h->root.u.def.value = h->plt.offset;
2219 /* Make room for this entry. */
2220 s->size += PLT_ENTRY_SIZE;
2222 /* We also need to make an entry in the .got.plt section, which
2223 will be placed in the .got section by the linker script. */
2224 htab->elf.sgotplt->size += 4;
2226 /* We also need to make an entry in the .rel.plt section. */
2227 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2228 htab->next_tls_desc_index++;
2230 if (htab->is_vxworks && !info->shared)
2232 /* VxWorks has a second set of relocations for each PLT entry
2233 in executables. They go in a separate relocation section,
2234 which is processed by the kernel loader. */
2236 /* There are two relocations for the initial PLT entry: an
2237 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
2238 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
2240 if (h->plt.offset == PLT_ENTRY_SIZE)
2241 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2243 /* There are two extra relocations for each subsequent PLT entry:
2244 an R_386_32 relocation for the GOT entry, and an R_386_32
2245 relocation for the PLT entry. */
2247 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2250 else
2252 h->plt.offset = (bfd_vma) -1;
2253 h->needs_plt = 0;
2256 else
2258 h->plt.offset = (bfd_vma) -1;
2259 h->needs_plt = 0;
2262 eh->tlsdesc_got = (bfd_vma) -1;
2264 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
2265 make it a R_386_TLS_LE_32 requiring no TLS entry. */
2266 if (h->got.refcount > 0
2267 && !info->shared
2268 && h->dynindx == -1
2269 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
2270 h->got.offset = (bfd_vma) -1;
2271 else if (h->got.refcount > 0)
2273 asection *s;
2274 bfd_boolean dyn;
2275 int tls_type = elf_i386_hash_entry(h)->tls_type;
2277 /* Make sure this symbol is output as a dynamic symbol.
2278 Undefined weak syms won't yet be marked as dynamic. */
2279 if (h->dynindx == -1
2280 && !h->forced_local)
2282 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2283 return FALSE;
2286 s = htab->elf.sgot;
2287 if (GOT_TLS_GDESC_P (tls_type))
2289 eh->tlsdesc_got = htab->elf.sgotplt->size
2290 - elf_i386_compute_jump_table_size (htab);
2291 htab->elf.sgotplt->size += 8;
2292 h->got.offset = (bfd_vma) -2;
2294 if (! GOT_TLS_GDESC_P (tls_type)
2295 || GOT_TLS_GD_P (tls_type))
2297 h->got.offset = s->size;
2298 s->size += 4;
2299 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
2300 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
2301 s->size += 4;
2303 dyn = htab->elf.dynamic_sections_created;
2304 /* R_386_TLS_IE_32 needs one dynamic relocation,
2305 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
2306 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
2307 need two), R_386_TLS_GD needs one if local symbol and two if
2308 global. */
2309 if (tls_type == GOT_TLS_IE_BOTH)
2310 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2311 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2312 || (tls_type & GOT_TLS_IE))
2313 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2314 else if (GOT_TLS_GD_P (tls_type))
2315 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2316 else if (! GOT_TLS_GDESC_P (tls_type)
2317 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2318 || h->root.type != bfd_link_hash_undefweak)
2319 && (info->shared
2320 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2321 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2322 if (GOT_TLS_GDESC_P (tls_type))
2323 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2325 else
2326 h->got.offset = (bfd_vma) -1;
2328 if (eh->dyn_relocs == NULL)
2329 return TRUE;
2331 /* In the shared -Bsymbolic case, discard space allocated for
2332 dynamic pc-relative relocs against symbols which turn out to be
2333 defined in regular objects. For the normal shared case, discard
2334 space for pc-relative relocs that have become local due to symbol
2335 visibility changes. */
2337 if (info->shared)
2339 /* The only reloc that uses pc_count is R_386_PC32, which will
2340 appear on a call or on something like ".long foo - .". We
2341 want calls to protected symbols to resolve directly to the
2342 function rather than going via the plt. If people want
2343 function pointer comparisons to work as expected then they
2344 should avoid writing assembly like ".long foo - .". */
2345 if (SYMBOL_CALLS_LOCAL (info, h))
2347 struct elf_i386_dyn_relocs **pp;
2349 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2351 p->count -= p->pc_count;
2352 p->pc_count = 0;
2353 if (p->count == 0)
2354 *pp = p->next;
2355 else
2356 pp = &p->next;
2360 if (htab->is_vxworks)
2362 struct elf_i386_dyn_relocs **pp;
2363 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2365 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2366 *pp = p->next;
2367 else
2368 pp = &p->next;
2372 /* Also discard relocs on undefined weak syms with non-default
2373 visibility. */
2374 if (eh->dyn_relocs != NULL
2375 && h->root.type == bfd_link_hash_undefweak)
2377 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2378 eh->dyn_relocs = NULL;
2380 /* Make sure undefined weak symbols are output as a dynamic
2381 symbol in PIEs. */
2382 else if (h->dynindx == -1
2383 && !h->forced_local)
2385 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2386 return FALSE;
2390 else if (ELIMINATE_COPY_RELOCS)
2392 /* For the non-shared case, discard space for relocs against
2393 symbols which turn out to need copy relocs or are not
2394 dynamic. */
2396 if (!h->non_got_ref
2397 && ((h->def_dynamic
2398 && !h->def_regular)
2399 || (htab->elf.dynamic_sections_created
2400 && (h->root.type == bfd_link_hash_undefweak
2401 || h->root.type == bfd_link_hash_undefined))))
2403 /* Make sure this symbol is output as a dynamic symbol.
2404 Undefined weak syms won't yet be marked as dynamic. */
2405 if (h->dynindx == -1
2406 && !h->forced_local)
2408 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2409 return FALSE;
2412 /* If that succeeded, we know we'll be keeping all the
2413 relocs. */
2414 if (h->dynindx != -1)
2415 goto keep;
2418 eh->dyn_relocs = NULL;
2420 keep: ;
2423 /* Finally, allocate space. */
2424 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2426 asection *sreloc;
2428 sreloc = elf_section_data (p->sec)->sreloc;
2430 BFD_ASSERT (sreloc != NULL);
2431 sreloc->size += p->count * sizeof (Elf32_External_Rel);
2434 return TRUE;
2437 /* Allocate space in .plt, .got and associated reloc sections for
2438 local dynamic relocs. */
2440 static bfd_boolean
2441 elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
2443 struct elf_link_hash_entry *h
2444 = (struct elf_link_hash_entry *) *slot;
2446 if (h->type != STT_GNU_IFUNC
2447 || !h->def_regular
2448 || !h->ref_regular
2449 || !h->forced_local
2450 || h->root.type != bfd_link_hash_defined)
2451 abort ();
2453 return elf_i386_allocate_dynrelocs (h, inf);
2456 /* Find any dynamic relocs that apply to read-only sections. */
2458 static bfd_boolean
2459 elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2461 struct elf_i386_link_hash_entry *eh;
2462 struct elf_i386_dyn_relocs *p;
2464 if (h->root.type == bfd_link_hash_warning)
2465 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2467 eh = (struct elf_i386_link_hash_entry *) h;
2468 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2470 asection *s = p->sec->output_section;
2472 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2474 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2476 info->flags |= DF_TEXTREL;
2478 /* Not an error, just cut short the traversal. */
2479 return FALSE;
2482 return TRUE;
2485 /* Set the sizes of the dynamic sections. */
2487 static bfd_boolean
2488 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2489 struct bfd_link_info *info)
2491 struct elf_i386_link_hash_table *htab;
2492 bfd *dynobj;
2493 asection *s;
2494 bfd_boolean relocs;
2495 bfd *ibfd;
2497 htab = elf_i386_hash_table (info);
2498 dynobj = htab->elf.dynobj;
2499 if (dynobj == NULL)
2500 abort ();
2502 if (htab->elf.dynamic_sections_created)
2504 /* Set the contents of the .interp section to the interpreter. */
2505 if (info->executable)
2507 s = bfd_get_section_by_name (dynobj, ".interp");
2508 if (s == NULL)
2509 abort ();
2510 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2511 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2515 /* Set up .got offsets for local syms, and space for local dynamic
2516 relocs. */
2517 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2519 bfd_signed_vma *local_got;
2520 bfd_signed_vma *end_local_got;
2521 char *local_tls_type;
2522 bfd_vma *local_tlsdesc_gotent;
2523 bfd_size_type locsymcount;
2524 Elf_Internal_Shdr *symtab_hdr;
2525 asection *srel;
2527 if (! is_i386_elf (ibfd))
2528 continue;
2530 for (s = ibfd->sections; s != NULL; s = s->next)
2532 struct elf_i386_dyn_relocs *p;
2534 for (p = ((struct elf_i386_dyn_relocs *)
2535 elf_section_data (s)->local_dynrel);
2536 p != NULL;
2537 p = p->next)
2539 if (!bfd_is_abs_section (p->sec)
2540 && bfd_is_abs_section (p->sec->output_section))
2542 /* Input section has been discarded, either because
2543 it is a copy of a linkonce section or due to
2544 linker script /DISCARD/, so we'll be discarding
2545 the relocs too. */
2547 else if (htab->is_vxworks
2548 && strcmp (p->sec->output_section->name,
2549 ".tls_vars") == 0)
2551 /* Relocations in vxworks .tls_vars sections are
2552 handled specially by the loader. */
2554 else if (p->count != 0)
2556 srel = elf_section_data (p->sec)->sreloc;
2557 srel->size += p->count * sizeof (Elf32_External_Rel);
2558 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2559 info->flags |= DF_TEXTREL;
2564 local_got = elf_local_got_refcounts (ibfd);
2565 if (!local_got)
2566 continue;
2568 symtab_hdr = &elf_symtab_hdr (ibfd);
2569 locsymcount = symtab_hdr->sh_info;
2570 end_local_got = local_got + locsymcount;
2571 local_tls_type = elf_i386_local_got_tls_type (ibfd);
2572 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
2573 s = htab->elf.sgot;
2574 srel = htab->elf.srelgot;
2575 for (; local_got < end_local_got;
2576 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2578 *local_tlsdesc_gotent = (bfd_vma) -1;
2579 if (*local_got > 0)
2581 if (GOT_TLS_GDESC_P (*local_tls_type))
2583 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2584 - elf_i386_compute_jump_table_size (htab);
2585 htab->elf.sgotplt->size += 8;
2586 *local_got = (bfd_vma) -2;
2588 if (! GOT_TLS_GDESC_P (*local_tls_type)
2589 || GOT_TLS_GD_P (*local_tls_type))
2591 *local_got = s->size;
2592 s->size += 4;
2593 if (GOT_TLS_GD_P (*local_tls_type)
2594 || *local_tls_type == GOT_TLS_IE_BOTH)
2595 s->size += 4;
2597 if (info->shared
2598 || GOT_TLS_GD_ANY_P (*local_tls_type)
2599 || (*local_tls_type & GOT_TLS_IE))
2601 if (*local_tls_type == GOT_TLS_IE_BOTH)
2602 srel->size += 2 * sizeof (Elf32_External_Rel);
2603 else if (GOT_TLS_GD_P (*local_tls_type)
2604 || ! GOT_TLS_GDESC_P (*local_tls_type))
2605 srel->size += sizeof (Elf32_External_Rel);
2606 if (GOT_TLS_GDESC_P (*local_tls_type))
2607 htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2610 else
2611 *local_got = (bfd_vma) -1;
2615 if (htab->tls_ldm_got.refcount > 0)
2617 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2618 relocs. */
2619 htab->tls_ldm_got.offset = htab->elf.sgot->size;
2620 htab->elf.sgot->size += 8;
2621 htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2623 else
2624 htab->tls_ldm_got.offset = -1;
2626 /* Allocate global sym .plt and .got entries, and space for global
2627 sym dynamic relocs. */
2628 elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
2630 /* Allocate .plt and .got entries, and space for local symbols. */
2631 htab_traverse (htab->loc_hash_table,
2632 elf_i386_allocate_local_dynrelocs,
2633 info);
2635 /* For every jump slot reserved in the sgotplt, reloc_count is
2636 incremented. However, when we reserve space for TLS descriptors,
2637 it's not incremented, so in order to compute the space reserved
2638 for them, it suffices to multiply the reloc count by the jump
2639 slot size. */
2640 if (htab->elf.srelplt)
2641 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
2643 /* We now have determined the sizes of the various dynamic sections.
2644 Allocate memory for them. */
2645 relocs = FALSE;
2646 for (s = dynobj->sections; s != NULL; s = s->next)
2648 bfd_boolean strip_section = TRUE;
2650 if ((s->flags & SEC_LINKER_CREATED) == 0)
2651 continue;
2653 if (s == htab->elf.splt
2654 || s == htab->elf.sgot
2655 || s == htab->elf.sgotplt
2656 || s == htab->elf.iplt
2657 || s == htab->elf.igotplt
2658 || s == htab->sdynbss)
2660 /* Strip this section if we don't need it; see the
2661 comment below. */
2662 /* We'd like to strip these sections if they aren't needed, but if
2663 we've exported dynamic symbols from them we must leave them.
2664 It's too late to tell BFD to get rid of the symbols. */
2666 if (htab->elf.hplt != NULL)
2667 strip_section = FALSE;
2669 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
2671 if (s->size != 0
2672 && s != htab->elf.srelplt
2673 && s != htab->srelplt2)
2674 relocs = TRUE;
2676 /* We use the reloc_count field as a counter if we need
2677 to copy relocs into the output file. */
2678 s->reloc_count = 0;
2680 else
2682 /* It's not one of our sections, so don't allocate space. */
2683 continue;
2686 if (s->size == 0)
2688 /* If we don't need this section, strip it from the
2689 output file. This is mostly to handle .rel.bss and
2690 .rel.plt. We must create both sections in
2691 create_dynamic_sections, because they must be created
2692 before the linker maps input sections to output
2693 sections. The linker does that before
2694 adjust_dynamic_symbol is called, and it is that
2695 function which decides whether anything needs to go
2696 into these sections. */
2697 if (strip_section)
2698 s->flags |= SEC_EXCLUDE;
2699 continue;
2702 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2703 continue;
2705 /* Allocate memory for the section contents. We use bfd_zalloc
2706 here in case unused entries are not reclaimed before the
2707 section's contents are written out. This should not happen,
2708 but this way if it does, we get a R_386_NONE reloc instead
2709 of garbage. */
2710 s->contents = bfd_zalloc (dynobj, s->size);
2711 if (s->contents == NULL)
2712 return FALSE;
2715 if (htab->elf.dynamic_sections_created)
2717 /* Add some entries to the .dynamic section. We fill in the
2718 values later, in elf_i386_finish_dynamic_sections, but we
2719 must add the entries now so that we get the correct size for
2720 the .dynamic section. The DT_DEBUG entry is filled in by the
2721 dynamic linker and used by the debugger. */
2722 #define add_dynamic_entry(TAG, VAL) \
2723 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2725 if (info->executable)
2727 if (!add_dynamic_entry (DT_DEBUG, 0))
2728 return FALSE;
2731 if (htab->elf.splt->size != 0)
2733 if (!add_dynamic_entry (DT_PLTGOT, 0)
2734 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2735 || !add_dynamic_entry (DT_PLTREL, DT_REL)
2736 || !add_dynamic_entry (DT_JMPREL, 0))
2737 return FALSE;
2740 if (relocs)
2742 if (!add_dynamic_entry (DT_REL, 0)
2743 || !add_dynamic_entry (DT_RELSZ, 0)
2744 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2745 return FALSE;
2747 /* If any dynamic relocs apply to a read-only section,
2748 then we need a DT_TEXTREL entry. */
2749 if ((info->flags & DF_TEXTREL) == 0)
2750 elf_link_hash_traverse (&htab->elf,
2751 elf_i386_readonly_dynrelocs, info);
2753 if ((info->flags & DF_TEXTREL) != 0)
2755 if (!add_dynamic_entry (DT_TEXTREL, 0))
2756 return FALSE;
2759 if (htab->is_vxworks
2760 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
2761 return FALSE;
2763 #undef add_dynamic_entry
2765 return TRUE;
2768 static bfd_boolean
2769 elf_i386_always_size_sections (bfd *output_bfd,
2770 struct bfd_link_info *info)
2772 asection *tls_sec = elf_hash_table (info)->tls_sec;
2774 if (tls_sec)
2776 struct elf_link_hash_entry *tlsbase;
2778 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2779 "_TLS_MODULE_BASE_",
2780 FALSE, FALSE, FALSE);
2782 if (tlsbase && tlsbase->type == STT_TLS)
2784 struct bfd_link_hash_entry *bh = NULL;
2785 const struct elf_backend_data *bed
2786 = get_elf_backend_data (output_bfd);
2788 if (!(_bfd_generic_link_add_one_symbol
2789 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2790 tls_sec, 0, NULL, FALSE,
2791 bed->collect, &bh)))
2792 return FALSE;
2794 elf_i386_hash_table (info)->tls_module_base = bh;
2796 tlsbase = (struct elf_link_hash_entry *)bh;
2797 tlsbase->def_regular = 1;
2798 tlsbase->other = STV_HIDDEN;
2799 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2803 return TRUE;
2806 /* Set the correct type for an x86 ELF section. We do this by the
2807 section name, which is a hack, but ought to work. */
2809 static bfd_boolean
2810 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2811 Elf_Internal_Shdr *hdr,
2812 asection *sec)
2814 register const char *name;
2816 name = bfd_get_section_name (abfd, sec);
2818 /* This is an ugly, but unfortunately necessary hack that is
2819 needed when producing EFI binaries on x86. It tells
2820 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2821 containing ELF relocation info. We need this hack in order to
2822 be able to generate ELF binaries that can be translated into
2823 EFI applications (which are essentially COFF objects). Those
2824 files contain a COFF ".reloc" section inside an ELFNN object,
2825 which would normally cause BFD to segfault because it would
2826 attempt to interpret this section as containing relocation
2827 entries for section "oc". With this hack enabled, ".reloc"
2828 will be treated as a normal data section, which will avoid the
2829 segfault. However, you won't be able to create an ELFNN binary
2830 with a section named "oc" that needs relocations, but that's
2831 the kind of ugly side-effects you get when detecting section
2832 types based on their names... In practice, this limitation is
2833 unlikely to bite. */
2834 if (strcmp (name, ".reloc") == 0)
2835 hdr->sh_type = SHT_PROGBITS;
2837 return TRUE;
2840 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2841 executables. Rather than setting it to the beginning of the TLS
2842 section, we have to set it to the end. This function may be called
2843 multiple times, it is idempotent. */
2845 static void
2846 elf_i386_set_tls_module_base (struct bfd_link_info *info)
2848 struct bfd_link_hash_entry *base;
2850 if (!info->executable)
2851 return;
2853 base = elf_i386_hash_table (info)->tls_module_base;
2855 if (!base)
2856 return;
2858 base->u.def.value = elf_hash_table (info)->tls_size;
2861 /* Return the base VMA address which should be subtracted from real addresses
2862 when resolving @dtpoff relocation.
2863 This is PT_TLS segment p_vaddr. */
2865 static bfd_vma
2866 elf_i386_dtpoff_base (struct bfd_link_info *info)
2868 /* If tls_sec is NULL, we should have signalled an error already. */
2869 if (elf_hash_table (info)->tls_sec == NULL)
2870 return 0;
2871 return elf_hash_table (info)->tls_sec->vma;
2874 /* Return the relocation value for @tpoff relocation
2875 if STT_TLS virtual address is ADDRESS. */
2877 static bfd_vma
2878 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
2880 struct elf_link_hash_table *htab = elf_hash_table (info);
2882 /* If tls_sec is NULL, we should have signalled an error already. */
2883 if (htab->tls_sec == NULL)
2884 return 0;
2885 return htab->tls_size + htab->tls_sec->vma - address;
2888 /* Relocate an i386 ELF section. */
2890 static bfd_boolean
2891 elf_i386_relocate_section (bfd *output_bfd,
2892 struct bfd_link_info *info,
2893 bfd *input_bfd,
2894 asection *input_section,
2895 bfd_byte *contents,
2896 Elf_Internal_Rela *relocs,
2897 Elf_Internal_Sym *local_syms,
2898 asection **local_sections)
2900 struct elf_i386_link_hash_table *htab;
2901 Elf_Internal_Shdr *symtab_hdr;
2902 struct elf_link_hash_entry **sym_hashes;
2903 bfd_vma *local_got_offsets;
2904 bfd_vma *local_tlsdesc_gotents;
2905 Elf_Internal_Rela *rel;
2906 Elf_Internal_Rela *relend;
2907 bfd_boolean is_vxworks_tls;
2909 BFD_ASSERT (is_i386_elf (input_bfd));
2911 htab = elf_i386_hash_table (info);
2912 symtab_hdr = &elf_symtab_hdr (input_bfd);
2913 sym_hashes = elf_sym_hashes (input_bfd);
2914 local_got_offsets = elf_local_got_offsets (input_bfd);
2915 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
2916 /* We have to handle relocations in vxworks .tls_vars sections
2917 specially, because the dynamic loader is 'weird'. */
2918 is_vxworks_tls = (htab->is_vxworks && info->shared
2919 && !strcmp (input_section->output_section->name,
2920 ".tls_vars"));
2922 elf_i386_set_tls_module_base (info);
2924 rel = relocs;
2925 relend = relocs + input_section->reloc_count;
2926 for (; rel < relend; rel++)
2928 unsigned int r_type;
2929 reloc_howto_type *howto;
2930 unsigned long r_symndx;
2931 struct elf_link_hash_entry *h;
2932 Elf_Internal_Sym *sym;
2933 asection *sec;
2934 bfd_vma off, offplt;
2935 bfd_vma relocation;
2936 bfd_boolean unresolved_reloc;
2937 bfd_reloc_status_type r;
2938 unsigned int indx;
2939 int tls_type;
2941 r_type = ELF32_R_TYPE (rel->r_info);
2942 if (r_type == R_386_GNU_VTINHERIT
2943 || r_type == R_386_GNU_VTENTRY)
2944 continue;
2946 if ((indx = r_type) >= R_386_standard
2947 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2948 >= R_386_ext - R_386_standard)
2949 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2950 >= R_386_irelative - R_386_ext))
2952 (*_bfd_error_handler)
2953 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2954 input_bfd, input_section, r_type);
2955 bfd_set_error (bfd_error_bad_value);
2956 return FALSE;
2958 howto = elf_howto_table + indx;
2960 r_symndx = ELF32_R_SYM (rel->r_info);
2961 h = NULL;
2962 sym = NULL;
2963 sec = NULL;
2964 unresolved_reloc = FALSE;
2965 if (r_symndx < symtab_hdr->sh_info)
2967 sym = local_syms + r_symndx;
2968 sec = local_sections[r_symndx];
2969 relocation = (sec->output_section->vma
2970 + sec->output_offset
2971 + sym->st_value);
2973 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2974 && ((sec->flags & SEC_MERGE) != 0
2975 || (info->relocatable
2976 && sec->output_offset != 0)))
2978 bfd_vma addend;
2979 bfd_byte *where = contents + rel->r_offset;
2981 switch (howto->size)
2983 case 0:
2984 addend = bfd_get_8 (input_bfd, where);
2985 if (howto->pc_relative)
2987 addend = (addend ^ 0x80) - 0x80;
2988 addend += 1;
2990 break;
2991 case 1:
2992 addend = bfd_get_16 (input_bfd, where);
2993 if (howto->pc_relative)
2995 addend = (addend ^ 0x8000) - 0x8000;
2996 addend += 2;
2998 break;
2999 case 2:
3000 addend = bfd_get_32 (input_bfd, where);
3001 if (howto->pc_relative)
3003 addend = (addend ^ 0x80000000) - 0x80000000;
3004 addend += 4;
3006 break;
3007 default:
3008 abort ();
3011 if (info->relocatable)
3012 addend += sec->output_offset;
3013 else
3015 asection *msec = sec;
3016 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
3017 addend);
3018 addend -= relocation;
3019 addend += msec->output_section->vma + msec->output_offset;
3022 switch (howto->size)
3024 case 0:
3025 /* FIXME: overflow checks. */
3026 if (howto->pc_relative)
3027 addend -= 1;
3028 bfd_put_8 (input_bfd, addend, where);
3029 break;
3030 case 1:
3031 if (howto->pc_relative)
3032 addend -= 2;
3033 bfd_put_16 (input_bfd, addend, where);
3034 break;
3035 case 2:
3036 if (howto->pc_relative)
3037 addend -= 4;
3038 bfd_put_32 (input_bfd, addend, where);
3039 break;
3042 else if (ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3044 /* Relocate against local STT_GNU_IFUNC symbol. */
3045 h = elf_i386_get_local_sym_hash (htab, input_bfd,
3046 rel, FALSE);
3047 if (h == NULL)
3048 abort ();
3050 /* Set STT_GNU_IFUNC symbol value. */
3051 h->root.u.def.value = sym->st_value;
3052 h->root.u.def.section = sec;
3055 else
3057 bfd_boolean warned;
3059 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3060 r_symndx, symtab_hdr, sym_hashes,
3061 h, sec, relocation,
3062 unresolved_reloc, warned);
3065 if (sec != NULL && elf_discarded_section (sec))
3067 /* For relocs against symbols from removed linkonce sections,
3068 or sections discarded by a linker script, we just want the
3069 section contents zeroed. Avoid any special processing. */
3070 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
3071 rel->r_info = 0;
3072 rel->r_addend = 0;
3073 continue;
3076 if (info->relocatable)
3077 continue;
3079 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3080 it here if it is defined in a non-shared object. */
3081 if (h != NULL
3082 && h->type == STT_GNU_IFUNC
3083 && h->def_regular)
3085 asection *plt, *gotplt, *base_got;
3086 bfd_vma plt_index;
3088 if ((input_section->flags & SEC_ALLOC) == 0
3089 || h->plt.offset == (bfd_vma) -1)
3090 abort ();
3092 /* STT_GNU_IFUNC symbol must go through PLT. */
3093 if (htab->elf.splt != NULL)
3095 plt = htab->elf.splt;
3096 gotplt = htab->elf.sgotplt;
3098 else
3100 plt = htab->elf.iplt;
3101 gotplt = htab->elf.igotplt;
3104 relocation = (plt->output_section->vma
3105 + plt->output_offset + h->plt.offset);
3107 switch (r_type)
3109 default:
3110 (*_bfd_error_handler)
3111 (_("%B: relocation %s against STT_GNU_IFUNC "
3112 "symbol `%s' isn't handled by %s"), input_bfd,
3113 elf_howto_table[r_type].name,
3114 (h->root.root.string
3115 ? h->root.root.string : "a local symbol"),
3116 __FUNCTION__);
3117 bfd_set_error (bfd_error_bad_value);
3118 return FALSE;
3120 case R_386_32:
3121 /* Generate dynamic relcoation only when there is a
3122 non-GOF reference in a shared object. */
3123 if (info->shared && h->non_got_ref)
3125 Elf_Internal_Rela outrel;
3126 bfd_byte *loc;
3127 asection *sreloc;
3128 bfd_vma offset;
3130 /* Need a dynamic relocation to get the real function
3131 adddress. */
3132 offset = _bfd_elf_section_offset (output_bfd,
3133 info,
3134 input_section,
3135 rel->r_offset);
3136 if (offset == (bfd_vma) -1
3137 || offset == (bfd_vma) -2)
3138 abort ();
3140 outrel.r_offset = (input_section->output_section->vma
3141 + input_section->output_offset
3142 + offset);
3144 if (h->dynindx == -1
3145 || h->forced_local
3146 || info->executable)
3148 /* This symbol is resolved locally. */
3149 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3150 bfd_put_32 (output_bfd,
3151 (h->root.u.def.value
3152 + h->root.u.def.section->output_section->vma
3153 + h->root.u.def.section->output_offset),
3154 contents + offset);
3156 else
3157 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3159 sreloc = htab->elf.irelifunc;
3160 loc = sreloc->contents;
3161 loc += (sreloc->reloc_count++
3162 * sizeof (Elf32_External_Rel));
3163 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3165 /* If this reloc is against an external symbol, we
3166 do not want to fiddle with the addend. Otherwise,
3167 we need to include the symbol value so that it
3168 becomes an addend for the dynamic reloc. For an
3169 internal symbol, we have updated addend. */
3170 continue;
3173 case R_386_PC32:
3174 case R_386_PLT32:
3175 goto do_relocation;
3177 case R_386_GOT32:
3178 base_got = htab->elf.sgot;
3179 off = h->got.offset;
3181 if (base_got == NULL)
3182 abort ();
3184 if (off == (bfd_vma) -1)
3186 /* We can't use h->got.offset here to save state, or
3187 even just remember the offset, as finish_dynamic_symbol
3188 would use that as offset into .got. */
3190 if (htab->elf.splt != NULL)
3192 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3193 off = (plt_index + 3) * 4;
3194 base_got = htab->elf.sgotplt;
3196 else
3198 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3199 off = plt_index * 4;
3200 base_got = htab->elf.igotplt;
3203 if (h->dynindx == -1
3204 || h->forced_local
3205 || info->symbolic)
3207 /* This references the local defitionion. We must
3208 initialize this entry in the global offset table.
3209 Since the offset must always be a multiple of 8,
3210 we use the least significant bit to record
3211 whether we have initialized it already.
3213 When doing a dynamic link, we create a .rela.got
3214 relocation entry to initialize the value. This
3215 is done in the finish_dynamic_symbol routine. */
3216 if ((off & 1) != 0)
3217 off &= ~1;
3218 else
3220 bfd_put_32 (output_bfd, relocation,
3221 base_got->contents + off);
3222 h->got.offset |= 1;
3226 relocation = off;
3228 /* Adjust for static executables. */
3229 if (htab->elf.splt == NULL)
3230 relocation += gotplt->output_offset;
3232 else
3234 relocation = (base_got->output_section->vma
3235 + base_got->output_offset + off
3236 - gotplt->output_section->vma
3237 - gotplt->output_offset);
3238 /* Adjust for static executables. */
3239 if (htab->elf.splt == NULL)
3240 relocation += gotplt->output_offset;
3243 goto do_relocation;
3245 case R_386_GOTOFF:
3246 relocation -= (gotplt->output_section->vma
3247 + gotplt->output_offset);
3248 goto do_relocation;
3252 switch (r_type)
3254 case R_386_GOT32:
3255 /* Relocation is to the entry for this symbol in the global
3256 offset table. */
3257 if (htab->elf.sgot == NULL)
3258 abort ();
3260 if (h != NULL)
3262 bfd_boolean dyn;
3264 off = h->got.offset;
3265 dyn = htab->elf.dynamic_sections_created;
3266 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3267 || (info->shared
3268 && SYMBOL_REFERENCES_LOCAL (info, h))
3269 || (ELF_ST_VISIBILITY (h->other)
3270 && h->root.type == bfd_link_hash_undefweak))
3272 /* This is actually a static link, or it is a
3273 -Bsymbolic link and the symbol is defined
3274 locally, or the symbol was forced to be local
3275 because of a version file. We must initialize
3276 this entry in the global offset table. Since the
3277 offset must always be a multiple of 4, we use the
3278 least significant bit to record whether we have
3279 initialized it already.
3281 When doing a dynamic link, we create a .rel.got
3282 relocation entry to initialize the value. This
3283 is done in the finish_dynamic_symbol routine. */
3284 if ((off & 1) != 0)
3285 off &= ~1;
3286 else
3288 bfd_put_32 (output_bfd, relocation,
3289 htab->elf.sgot->contents + off);
3290 h->got.offset |= 1;
3293 else
3294 unresolved_reloc = FALSE;
3296 else
3298 if (local_got_offsets == NULL)
3299 abort ();
3301 off = local_got_offsets[r_symndx];
3303 /* The offset must always be a multiple of 4. We use
3304 the least significant bit to record whether we have
3305 already generated the necessary reloc. */
3306 if ((off & 1) != 0)
3307 off &= ~1;
3308 else
3310 bfd_put_32 (output_bfd, relocation,
3311 htab->elf.sgot->contents + off);
3313 if (info->shared)
3315 asection *s;
3316 Elf_Internal_Rela outrel;
3317 bfd_byte *loc;
3319 s = htab->elf.srelgot;
3320 if (s == NULL)
3321 abort ();
3323 outrel.r_offset = (htab->elf.sgot->output_section->vma
3324 + htab->elf.sgot->output_offset
3325 + off);
3326 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3327 loc = s->contents;
3328 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
3329 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3332 local_got_offsets[r_symndx] |= 1;
3336 if (off >= (bfd_vma) -2)
3337 abort ();
3339 relocation = htab->elf.sgot->output_section->vma
3340 + htab->elf.sgot->output_offset + off
3341 - htab->elf.sgotplt->output_section->vma
3342 - htab->elf.sgotplt->output_offset;
3343 break;
3345 case R_386_GOTOFF:
3346 /* Relocation is relative to the start of the global offset
3347 table. */
3349 /* Check to make sure it isn't a protected function symbol
3350 for shared library since it may not be local when used
3351 as function address. We also need to make sure that a
3352 symbol is defined locally. */
3353 if (info->shared && h)
3355 if (!h->def_regular)
3357 const char *v;
3359 switch (ELF_ST_VISIBILITY (h->other))
3361 case STV_HIDDEN:
3362 v = _("hidden symbol");
3363 break;
3364 case STV_INTERNAL:
3365 v = _("internal symbol");
3366 break;
3367 case STV_PROTECTED:
3368 v = _("protected symbol");
3369 break;
3370 default:
3371 v = _("symbol");
3372 break;
3375 (*_bfd_error_handler)
3376 (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
3377 input_bfd, v, h->root.root.string);
3378 bfd_set_error (bfd_error_bad_value);
3379 return FALSE;
3381 else if (!info->executable
3382 && h->type == STT_FUNC
3383 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3385 (*_bfd_error_handler)
3386 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
3387 input_bfd, h->root.root.string);
3388 bfd_set_error (bfd_error_bad_value);
3389 return FALSE;
3393 /* Note that sgot is not involved in this
3394 calculation. We always want the start of .got.plt. If we
3395 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3396 permitted by the ABI, we might have to change this
3397 calculation. */
3398 relocation -= htab->elf.sgotplt->output_section->vma
3399 + htab->elf.sgotplt->output_offset;
3400 break;
3402 case R_386_GOTPC:
3403 /* Use global offset table as symbol value. */
3404 relocation = htab->elf.sgotplt->output_section->vma
3405 + htab->elf.sgotplt->output_offset;
3406 unresolved_reloc = FALSE;
3407 break;
3409 case R_386_PLT32:
3410 /* Relocation is to the entry for this symbol in the
3411 procedure linkage table. */
3413 /* Resolve a PLT32 reloc against a local symbol directly,
3414 without using the procedure linkage table. */
3415 if (h == NULL)
3416 break;
3418 if (h->plt.offset == (bfd_vma) -1
3419 || htab->elf.splt == NULL)
3421 /* We didn't make a PLT entry for this symbol. This
3422 happens when statically linking PIC code, or when
3423 using -Bsymbolic. */
3424 break;
3427 relocation = (htab->elf.splt->output_section->vma
3428 + htab->elf.splt->output_offset
3429 + h->plt.offset);
3430 unresolved_reloc = FALSE;
3431 break;
3433 case R_386_32:
3434 case R_386_PC32:
3435 if ((input_section->flags & SEC_ALLOC) == 0
3436 || is_vxworks_tls)
3437 break;
3439 if ((info->shared
3440 && (h == NULL
3441 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3442 || h->root.type != bfd_link_hash_undefweak)
3443 && (r_type != R_386_PC32
3444 || !SYMBOL_CALLS_LOCAL (info, h)))
3445 || (ELIMINATE_COPY_RELOCS
3446 && !info->shared
3447 && h != NULL
3448 && h->dynindx != -1
3449 && !h->non_got_ref
3450 && ((h->def_dynamic
3451 && !h->def_regular)
3452 || h->root.type == bfd_link_hash_undefweak
3453 || h->root.type == bfd_link_hash_undefined)))
3455 Elf_Internal_Rela outrel;
3456 bfd_byte *loc;
3457 bfd_boolean skip, relocate;
3458 asection *sreloc;
3460 /* When generating a shared object, these relocations
3461 are copied into the output file to be resolved at run
3462 time. */
3464 skip = FALSE;
3465 relocate = FALSE;
3467 outrel.r_offset =
3468 _bfd_elf_section_offset (output_bfd, info, input_section,
3469 rel->r_offset);
3470 if (outrel.r_offset == (bfd_vma) -1)
3471 skip = TRUE;
3472 else if (outrel.r_offset == (bfd_vma) -2)
3473 skip = TRUE, relocate = TRUE;
3474 outrel.r_offset += (input_section->output_section->vma
3475 + input_section->output_offset);
3477 if (skip)
3478 memset (&outrel, 0, sizeof outrel);
3479 else if (h != NULL
3480 && h->dynindx != -1
3481 && (r_type == R_386_PC32
3482 || !info->shared
3483 || !SYMBOLIC_BIND (info, h)
3484 || !h->def_regular))
3485 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3486 else
3488 /* This symbol is local, or marked to become local. */
3489 relocate = TRUE;
3490 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3493 sreloc = elf_section_data (input_section)->sreloc;
3495 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3497 loc = sreloc->contents;
3498 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3500 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3502 /* If this reloc is against an external symbol, we do
3503 not want to fiddle with the addend. Otherwise, we
3504 need to include the symbol value so that it becomes
3505 an addend for the dynamic reloc. */
3506 if (! relocate)
3507 continue;
3509 break;
3511 case R_386_TLS_IE:
3512 if (info->shared)
3514 Elf_Internal_Rela outrel;
3515 bfd_byte *loc;
3516 asection *sreloc;
3518 outrel.r_offset = rel->r_offset
3519 + input_section->output_section->vma
3520 + input_section->output_offset;
3521 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3522 sreloc = elf_section_data (input_section)->sreloc;
3523 if (sreloc == NULL)
3524 abort ();
3525 loc = sreloc->contents;
3526 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3527 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3529 /* Fall through */
3531 case R_386_TLS_GD:
3532 case R_386_TLS_GOTDESC:
3533 case R_386_TLS_DESC_CALL:
3534 case R_386_TLS_IE_32:
3535 case R_386_TLS_GOTIE:
3536 tls_type = GOT_UNKNOWN;
3537 if (h == NULL && local_got_offsets)
3538 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
3539 else if (h != NULL)
3540 tls_type = elf_i386_hash_entry(h)->tls_type;
3541 if (tls_type == GOT_TLS_IE)
3542 tls_type = GOT_TLS_IE_NEG;
3544 if (! elf_i386_tls_transition (info, input_bfd,
3545 input_section, contents,
3546 symtab_hdr, sym_hashes,
3547 &r_type, tls_type, rel,
3548 relend, h))
3549 return FALSE;
3551 if (r_type == R_386_TLS_LE_32)
3553 BFD_ASSERT (! unresolved_reloc);
3554 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3556 unsigned int type;
3557 bfd_vma roff;
3559 /* GD->LE transition. */
3560 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3561 if (type == 0x04)
3563 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3564 Change it into:
3565 movl %gs:0, %eax; subl $foo@tpoff, %eax
3566 (6 byte form of subl). */
3567 memcpy (contents + rel->r_offset - 3,
3568 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3569 roff = rel->r_offset + 5;
3571 else
3573 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3574 Change it into:
3575 movl %gs:0, %eax; subl $foo@tpoff, %eax
3576 (6 byte form of subl). */
3577 memcpy (contents + rel->r_offset - 2,
3578 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3579 roff = rel->r_offset + 6;
3581 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3582 contents + roff);
3583 /* Skip R_386_PC32/R_386_PLT32. */
3584 rel++;
3585 continue;
3587 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3589 /* GDesc -> LE transition.
3590 It's originally something like:
3591 leal x@tlsdesc(%ebx), %eax
3593 leal x@ntpoff, %eax
3595 Registers other than %eax may be set up here. */
3597 unsigned int val;
3598 bfd_vma roff;
3600 roff = rel->r_offset;
3601 val = bfd_get_8 (input_bfd, contents + roff - 1);
3603 /* Now modify the instruction as appropriate. */
3604 /* aoliva FIXME: remove the above and xor the byte
3605 below with 0x86. */
3606 bfd_put_8 (output_bfd, val ^ 0x86,
3607 contents + roff - 1);
3608 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3609 contents + roff);
3610 continue;
3612 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3614 /* GDesc -> LE transition.
3615 It's originally:
3616 call *(%eax)
3617 Turn it into:
3618 xchg %ax,%ax */
3620 bfd_vma roff;
3622 roff = rel->r_offset;
3623 bfd_put_8 (output_bfd, 0x66, contents + roff);
3624 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3625 continue;
3627 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
3629 unsigned int val;
3631 /* IE->LE transition:
3632 Originally it can be one of:
3633 movl foo, %eax
3634 movl foo, %reg
3635 addl foo, %reg
3636 We change it into:
3637 movl $foo, %eax
3638 movl $foo, %reg
3639 addl $foo, %reg. */
3640 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3641 if (val == 0xa1)
3643 /* movl foo, %eax. */
3644 bfd_put_8 (output_bfd, 0xb8,
3645 contents + rel->r_offset - 1);
3647 else
3649 unsigned int type;
3651 type = bfd_get_8 (input_bfd,
3652 contents + rel->r_offset - 2);
3653 switch (type)
3655 case 0x8b:
3656 /* movl */
3657 bfd_put_8 (output_bfd, 0xc7,
3658 contents + rel->r_offset - 2);
3659 bfd_put_8 (output_bfd,
3660 0xc0 | ((val >> 3) & 7),
3661 contents + rel->r_offset - 1);
3662 break;
3663 case 0x03:
3664 /* addl */
3665 bfd_put_8 (output_bfd, 0x81,
3666 contents + rel->r_offset - 2);
3667 bfd_put_8 (output_bfd,
3668 0xc0 | ((val >> 3) & 7),
3669 contents + rel->r_offset - 1);
3670 break;
3671 default:
3672 BFD_FAIL ();
3673 break;
3676 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3677 contents + rel->r_offset);
3678 continue;
3680 else
3682 unsigned int val, type;
3684 /* {IE_32,GOTIE}->LE transition:
3685 Originally it can be one of:
3686 subl foo(%reg1), %reg2
3687 movl foo(%reg1), %reg2
3688 addl foo(%reg1), %reg2
3689 We change it into:
3690 subl $foo, %reg2
3691 movl $foo, %reg2 (6 byte form)
3692 addl $foo, %reg2. */
3693 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3694 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3695 if (type == 0x8b)
3697 /* movl */
3698 bfd_put_8 (output_bfd, 0xc7,
3699 contents + rel->r_offset - 2);
3700 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3701 contents + rel->r_offset - 1);
3703 else if (type == 0x2b)
3705 /* subl */
3706 bfd_put_8 (output_bfd, 0x81,
3707 contents + rel->r_offset - 2);
3708 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3709 contents + rel->r_offset - 1);
3711 else if (type == 0x03)
3713 /* addl */
3714 bfd_put_8 (output_bfd, 0x81,
3715 contents + rel->r_offset - 2);
3716 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3717 contents + rel->r_offset - 1);
3719 else
3720 BFD_FAIL ();
3721 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
3722 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3723 contents + rel->r_offset);
3724 else
3725 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3726 contents + rel->r_offset);
3727 continue;
3731 if (htab->elf.sgot == NULL)
3732 abort ();
3734 if (h != NULL)
3736 off = h->got.offset;
3737 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
3739 else
3741 if (local_got_offsets == NULL)
3742 abort ();
3744 off = local_got_offsets[r_symndx];
3745 offplt = local_tlsdesc_gotents[r_symndx];
3748 if ((off & 1) != 0)
3749 off &= ~1;
3750 else
3752 Elf_Internal_Rela outrel;
3753 bfd_byte *loc;
3754 int dr_type, indx;
3755 asection *sreloc;
3757 if (htab->elf.srelgot == NULL)
3758 abort ();
3760 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3762 if (GOT_TLS_GDESC_P (tls_type))
3764 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3765 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3766 <= htab->elf.sgotplt->size);
3767 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3768 + htab->elf.sgotplt->output_offset
3769 + offplt
3770 + htab->sgotplt_jump_table_size);
3771 sreloc = htab->elf.srelplt;
3772 loc = sreloc->contents;
3773 loc += (htab->next_tls_desc_index++
3774 * sizeof (Elf32_External_Rel));
3775 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3776 <= sreloc->contents + sreloc->size);
3777 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3778 if (indx == 0)
3780 BFD_ASSERT (! unresolved_reloc);
3781 bfd_put_32 (output_bfd,
3782 relocation - elf_i386_dtpoff_base (info),
3783 htab->elf.sgotplt->contents + offplt
3784 + htab->sgotplt_jump_table_size + 4);
3786 else
3788 bfd_put_32 (output_bfd, 0,
3789 htab->elf.sgotplt->contents + offplt
3790 + htab->sgotplt_jump_table_size + 4);
3794 sreloc = htab->elf.srelgot;
3796 outrel.r_offset = (htab->elf.sgot->output_section->vma
3797 + htab->elf.sgot->output_offset + off);
3799 if (GOT_TLS_GD_P (tls_type))
3800 dr_type = R_386_TLS_DTPMOD32;
3801 else if (GOT_TLS_GDESC_P (tls_type))
3802 goto dr_done;
3803 else if (tls_type == GOT_TLS_IE_POS)
3804 dr_type = R_386_TLS_TPOFF;
3805 else
3806 dr_type = R_386_TLS_TPOFF32;
3808 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3809 bfd_put_32 (output_bfd,
3810 relocation - elf_i386_dtpoff_base (info),
3811 htab->elf.sgot->contents + off);
3812 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3813 bfd_put_32 (output_bfd,
3814 elf_i386_dtpoff_base (info) - relocation,
3815 htab->elf.sgot->contents + off);
3816 else if (dr_type != R_386_TLS_DESC)
3817 bfd_put_32 (output_bfd, 0,
3818 htab->elf.sgot->contents + off);
3819 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3821 loc = sreloc->contents;
3822 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3823 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3824 <= sreloc->contents + sreloc->size);
3825 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3827 if (GOT_TLS_GD_P (tls_type))
3829 if (indx == 0)
3831 BFD_ASSERT (! unresolved_reloc);
3832 bfd_put_32 (output_bfd,
3833 relocation - elf_i386_dtpoff_base (info),
3834 htab->elf.sgot->contents + off + 4);
3836 else
3838 bfd_put_32 (output_bfd, 0,
3839 htab->elf.sgot->contents + off + 4);
3840 outrel.r_info = ELF32_R_INFO (indx,
3841 R_386_TLS_DTPOFF32);
3842 outrel.r_offset += 4;
3843 sreloc->reloc_count++;
3844 loc += sizeof (Elf32_External_Rel);
3845 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3846 <= sreloc->contents + sreloc->size);
3847 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3850 else if (tls_type == GOT_TLS_IE_BOTH)
3852 bfd_put_32 (output_bfd,
3853 (indx == 0
3854 ? relocation - elf_i386_dtpoff_base (info)
3855 : 0),
3856 htab->elf.sgot->contents + off + 4);
3857 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3858 outrel.r_offset += 4;
3859 sreloc->reloc_count++;
3860 loc += sizeof (Elf32_External_Rel);
3861 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3864 dr_done:
3865 if (h != NULL)
3866 h->got.offset |= 1;
3867 else
3868 local_got_offsets[r_symndx] |= 1;
3871 if (off >= (bfd_vma) -2
3872 && ! GOT_TLS_GDESC_P (tls_type))
3873 abort ();
3874 if (r_type == R_386_TLS_GOTDESC
3875 || r_type == R_386_TLS_DESC_CALL)
3877 relocation = htab->sgotplt_jump_table_size + offplt;
3878 unresolved_reloc = FALSE;
3880 else if (r_type == ELF32_R_TYPE (rel->r_info))
3882 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3883 + htab->elf.sgotplt->output_offset;
3884 relocation = htab->elf.sgot->output_section->vma
3885 + htab->elf.sgot->output_offset + off - g_o_t;
3886 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3887 && tls_type == GOT_TLS_IE_BOTH)
3888 relocation += 4;
3889 if (r_type == R_386_TLS_IE)
3890 relocation += g_o_t;
3891 unresolved_reloc = FALSE;
3893 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3895 unsigned int val, type;
3896 bfd_vma roff;
3898 /* GD->IE transition. */
3899 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3900 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3901 if (type == 0x04)
3903 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3904 Change it into:
3905 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3906 val >>= 3;
3907 roff = rel->r_offset - 3;
3909 else
3911 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3912 Change it into:
3913 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3914 roff = rel->r_offset - 2;
3916 memcpy (contents + roff,
3917 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3918 contents[roff + 7] = 0x80 | (val & 7);
3919 /* If foo is used only with foo@gotntpoff(%reg) and
3920 foo@indntpoff, but not with foo@gottpoff(%reg), change
3921 subl $foo@gottpoff(%reg), %eax
3922 into:
3923 addl $foo@gotntpoff(%reg), %eax. */
3924 if (tls_type == GOT_TLS_IE_POS)
3925 contents[roff + 6] = 0x03;
3926 bfd_put_32 (output_bfd,
3927 htab->elf.sgot->output_section->vma
3928 + htab->elf.sgot->output_offset + off
3929 - htab->elf.sgotplt->output_section->vma
3930 - htab->elf.sgotplt->output_offset,
3931 contents + roff + 8);
3932 /* Skip R_386_PLT32. */
3933 rel++;
3934 continue;
3936 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3938 /* GDesc -> IE transition.
3939 It's originally something like:
3940 leal x@tlsdesc(%ebx), %eax
3942 Change it to:
3943 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3945 movl x@gottpoff(%ebx), %eax # before negl %eax
3947 Registers other than %eax may be set up here. */
3949 bfd_vma roff;
3951 /* First, make sure it's a leal adding ebx to a 32-bit
3952 offset into any register, although it's probably
3953 almost always going to be eax. */
3954 roff = rel->r_offset;
3956 /* Now modify the instruction as appropriate. */
3957 /* To turn a leal into a movl in the form we use it, it
3958 suffices to change the first byte from 0x8d to 0x8b.
3959 aoliva FIXME: should we decide to keep the leal, all
3960 we have to do is remove the statement below, and
3961 adjust the relaxation of R_386_TLS_DESC_CALL. */
3962 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3964 if (tls_type == GOT_TLS_IE_BOTH)
3965 off += 4;
3967 bfd_put_32 (output_bfd,
3968 htab->elf.sgot->output_section->vma
3969 + htab->elf.sgot->output_offset + off
3970 - htab->elf.sgotplt->output_section->vma
3971 - htab->elf.sgotplt->output_offset,
3972 contents + roff);
3973 continue;
3975 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3977 /* GDesc -> IE transition.
3978 It's originally:
3979 call *(%eax)
3981 Change it to:
3982 xchg %ax,%ax
3984 negl %eax
3985 depending on how we transformed the TLS_GOTDESC above.
3988 bfd_vma roff;
3990 roff = rel->r_offset;
3992 /* Now modify the instruction as appropriate. */
3993 if (tls_type != GOT_TLS_IE_NEG)
3995 /* xchg %ax,%ax */
3996 bfd_put_8 (output_bfd, 0x66, contents + roff);
3997 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3999 else
4001 /* negl %eax */
4002 bfd_put_8 (output_bfd, 0xf7, contents + roff);
4003 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
4006 continue;
4008 else
4009 BFD_ASSERT (FALSE);
4010 break;
4012 case R_386_TLS_LDM:
4013 if (! elf_i386_tls_transition (info, input_bfd,
4014 input_section, contents,
4015 symtab_hdr, sym_hashes,
4016 &r_type, GOT_UNKNOWN, rel,
4017 relend, h))
4018 return FALSE;
4020 if (r_type != R_386_TLS_LDM)
4022 /* LD->LE transition:
4023 leal foo(%reg), %eax; call ___tls_get_addr.
4024 We change it into:
4025 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
4026 BFD_ASSERT (r_type == R_386_TLS_LE_32);
4027 memcpy (contents + rel->r_offset - 2,
4028 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
4029 /* Skip R_386_PC32/R_386_PLT32. */
4030 rel++;
4031 continue;
4034 if (htab->elf.sgot == NULL)
4035 abort ();
4037 off = htab->tls_ldm_got.offset;
4038 if (off & 1)
4039 off &= ~1;
4040 else
4042 Elf_Internal_Rela outrel;
4043 bfd_byte *loc;
4045 if (htab->elf.srelgot == NULL)
4046 abort ();
4048 outrel.r_offset = (htab->elf.sgot->output_section->vma
4049 + htab->elf.sgot->output_offset + off);
4051 bfd_put_32 (output_bfd, 0,
4052 htab->elf.sgot->contents + off);
4053 bfd_put_32 (output_bfd, 0,
4054 htab->elf.sgot->contents + off + 4);
4055 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
4056 loc = htab->elf.srelgot->contents;
4057 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
4058 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4059 htab->tls_ldm_got.offset |= 1;
4061 relocation = htab->elf.sgot->output_section->vma
4062 + htab->elf.sgot->output_offset + off
4063 - htab->elf.sgotplt->output_section->vma
4064 - htab->elf.sgotplt->output_offset;
4065 unresolved_reloc = FALSE;
4066 break;
4068 case R_386_TLS_LDO_32:
4069 if (info->shared || (input_section->flags & SEC_CODE) == 0)
4070 relocation -= elf_i386_dtpoff_base (info);
4071 else
4072 /* When converting LDO to LE, we must negate. */
4073 relocation = -elf_i386_tpoff (info, relocation);
4074 break;
4076 case R_386_TLS_LE_32:
4077 case R_386_TLS_LE:
4078 if (info->shared)
4080 Elf_Internal_Rela outrel;
4081 asection *sreloc;
4082 bfd_byte *loc;
4083 int indx;
4085 outrel.r_offset = rel->r_offset
4086 + input_section->output_section->vma
4087 + input_section->output_offset;
4088 if (h != NULL && h->dynindx != -1)
4089 indx = h->dynindx;
4090 else
4091 indx = 0;
4092 if (r_type == R_386_TLS_LE_32)
4093 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
4094 else
4095 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
4096 sreloc = elf_section_data (input_section)->sreloc;
4097 if (sreloc == NULL)
4098 abort ();
4099 loc = sreloc->contents;
4100 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
4101 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
4102 if (indx)
4103 continue;
4104 else if (r_type == R_386_TLS_LE_32)
4105 relocation = elf_i386_dtpoff_base (info) - relocation;
4106 else
4107 relocation -= elf_i386_dtpoff_base (info);
4109 else if (r_type == R_386_TLS_LE_32)
4110 relocation = elf_i386_tpoff (info, relocation);
4111 else
4112 relocation = -elf_i386_tpoff (info, relocation);
4113 break;
4115 default:
4116 break;
4119 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4120 because such sections are not SEC_ALLOC and thus ld.so will
4121 not process them. */
4122 if (unresolved_reloc
4123 && !((input_section->flags & SEC_DEBUGGING) != 0
4124 && h->def_dynamic))
4126 (*_bfd_error_handler)
4127 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4128 input_bfd,
4129 input_section,
4130 (long) rel->r_offset,
4131 howto->name,
4132 h->root.root.string);
4133 return FALSE;
4136 do_relocation:
4137 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4138 contents, rel->r_offset,
4139 relocation, 0);
4141 if (r != bfd_reloc_ok)
4143 const char *name;
4145 if (h != NULL)
4146 name = h->root.root.string;
4147 else
4149 name = bfd_elf_string_from_elf_section (input_bfd,
4150 symtab_hdr->sh_link,
4151 sym->st_name);
4152 if (name == NULL)
4153 return FALSE;
4154 if (*name == '\0')
4155 name = bfd_section_name (input_bfd, sec);
4158 if (r == bfd_reloc_overflow)
4160 if (! ((*info->callbacks->reloc_overflow)
4161 (info, (h ? &h->root : NULL), name, howto->name,
4162 (bfd_vma) 0, input_bfd, input_section,
4163 rel->r_offset)))
4164 return FALSE;
4166 else
4168 (*_bfd_error_handler)
4169 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
4170 input_bfd, input_section,
4171 (long) rel->r_offset, name, (int) r);
4172 return FALSE;
4177 return TRUE;
4180 /* Finish up dynamic symbol handling. We set the contents of various
4181 dynamic sections here. */
4183 static bfd_boolean
4184 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
4185 struct bfd_link_info *info,
4186 struct elf_link_hash_entry *h,
4187 Elf_Internal_Sym *sym)
4189 struct elf_i386_link_hash_table *htab;
4191 htab = elf_i386_hash_table (info);
4193 if (h->plt.offset != (bfd_vma) -1)
4195 bfd_vma plt_index;
4196 bfd_vma got_offset;
4197 Elf_Internal_Rela rel;
4198 bfd_byte *loc;
4199 asection *plt, *gotplt, *relplt;
4201 /* When building a static executable, use .iplt, .igot.plt and
4202 .rel.iplt sections for STT_GNU_IFUNC symbols. */
4203 if (htab->elf.splt != NULL)
4205 plt = htab->elf.splt;
4206 gotplt = htab->elf.sgotplt;
4207 relplt = htab->elf.srelplt;
4209 else
4211 plt = htab->elf.iplt;
4212 gotplt = htab->elf.igotplt;
4213 relplt = htab->elf.irelplt;
4216 /* This symbol has an entry in the procedure linkage table. Set
4217 it up. */
4219 if ((h->dynindx == -1
4220 && !((h->forced_local || info->executable)
4221 && h->def_regular
4222 && h->type == STT_GNU_IFUNC))
4223 || plt == NULL
4224 || gotplt == NULL
4225 || relplt == NULL)
4226 abort ();
4228 /* Get the index in the procedure linkage table which
4229 corresponds to this symbol. This is the index of this symbol
4230 in all the symbols for which we are making plt entries. The
4231 first entry in the procedure linkage table is reserved.
4233 Get the offset into the .got table of the entry that
4234 corresponds to this function. Each .got entry is 4 bytes.
4235 The first three are reserved.
4237 For static executables, we don't reserve anything. */
4239 if (plt == htab->elf.splt)
4241 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
4242 got_offset = (plt_index + 3) * 4;
4244 else
4246 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
4247 got_offset = plt_index * 4;
4250 /* Fill in the entry in the procedure linkage table. */
4251 if (! info->shared)
4253 memcpy (plt->contents + h->plt.offset, elf_i386_plt_entry,
4254 PLT_ENTRY_SIZE);
4255 bfd_put_32 (output_bfd,
4256 (gotplt->output_section->vma
4257 + gotplt->output_offset
4258 + got_offset),
4259 plt->contents + h->plt.offset + 2);
4261 if (htab->is_vxworks)
4263 int s, k, reloc_index;
4265 /* Create the R_386_32 relocation referencing the GOT
4266 for this PLT entry. */
4268 /* S: Current slot number (zero-based). */
4269 s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
4270 /* K: Number of relocations for PLTResolve. */
4271 if (info->shared)
4272 k = PLTRESOLVE_RELOCS_SHLIB;
4273 else
4274 k = PLTRESOLVE_RELOCS;
4275 /* Skip the PLTresolve relocations, and the relocations for
4276 the other PLT slots. */
4277 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
4278 loc = (htab->srelplt2->contents + reloc_index
4279 * sizeof (Elf32_External_Rel));
4281 rel.r_offset = (htab->elf.splt->output_section->vma
4282 + htab->elf.splt->output_offset
4283 + h->plt.offset + 2),
4284 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4285 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4287 /* Create the R_386_32 relocation referencing the beginning of
4288 the PLT for this GOT entry. */
4289 rel.r_offset = (htab->elf.sgotplt->output_section->vma
4290 + htab->elf.sgotplt->output_offset
4291 + got_offset);
4292 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4293 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4294 loc + sizeof (Elf32_External_Rel));
4297 else
4299 memcpy (plt->contents + h->plt.offset, elf_i386_pic_plt_entry,
4300 PLT_ENTRY_SIZE);
4301 bfd_put_32 (output_bfd, got_offset,
4302 plt->contents + h->plt.offset + 2);
4305 /* Don't fill PLT entry for static executables. */
4306 if (plt == htab->elf.splt)
4308 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
4309 plt->contents + h->plt.offset + 7);
4310 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
4311 plt->contents + h->plt.offset + 12);
4314 /* Fill in the entry in the global offset table. */
4315 bfd_put_32 (output_bfd,
4316 (plt->output_section->vma
4317 + plt->output_offset
4318 + h->plt.offset
4319 + 6),
4320 gotplt->contents + got_offset);
4322 /* Fill in the entry in the .rel.plt section. */
4323 rel.r_offset = (gotplt->output_section->vma
4324 + gotplt->output_offset
4325 + got_offset);
4326 if (h->dynindx == -1
4327 || ((info->executable
4328 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4329 && h->def_regular
4330 && h->type == STT_GNU_IFUNC))
4332 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4333 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
4334 in the .got.plt section. */
4335 bfd_put_32 (output_bfd,
4336 (h->root.u.def.value
4337 + h->root.u.def.section->output_section->vma
4338 + h->root.u.def.section->output_offset),
4339 gotplt->contents + got_offset);
4340 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4342 else
4343 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
4344 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
4345 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4347 if (!h->def_regular)
4349 /* Mark the symbol as undefined, rather than as defined in
4350 the .plt section. Leave the value if there were any
4351 relocations where pointer equality matters (this is a clue
4352 for the dynamic linker, to make function pointer
4353 comparisons work between an application and shared
4354 library), otherwise set it to zero. If a function is only
4355 called from a binary, there is no need to slow down
4356 shared libraries because of that. */
4357 sym->st_shndx = SHN_UNDEF;
4358 if (!h->pointer_equality_needed)
4359 sym->st_value = 0;
4363 if (h->got.offset != (bfd_vma) -1
4364 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
4365 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
4367 Elf_Internal_Rela rel;
4368 bfd_byte *loc;
4370 /* This symbol has an entry in the global offset table. Set it
4371 up. */
4373 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4374 abort ();
4376 rel.r_offset = (htab->elf.sgot->output_section->vma
4377 + htab->elf.sgot->output_offset
4378 + (h->got.offset & ~(bfd_vma) 1));
4380 /* If this is a static link, or it is a -Bsymbolic link and the
4381 symbol is defined locally or was forced to be local because
4382 of a version file, we just want to emit a RELATIVE reloc.
4383 The entry in the global offset table will already have been
4384 initialized in the relocate_section function. */
4385 if (h->def_regular
4386 && h->type == STT_GNU_IFUNC)
4388 if (info->shared)
4390 /* Generate R_386_GLOB_DAT. */
4391 goto do_glob_dat;
4393 else
4395 if (!h->pointer_equality_needed)
4396 abort ();
4398 /* For non-shared object, we can't use .got.plt, which
4399 contains the real function addres if we need pointer
4400 equality. We load the GOT entry with the PLT entry. */
4401 asection *plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4402 bfd_put_32 (output_bfd,
4403 (plt->output_section->vma
4404 + plt->output_offset + h->plt.offset),
4405 htab->elf.sgot->contents + h->got.offset);
4406 return TRUE;
4409 else if (info->shared
4410 && SYMBOL_REFERENCES_LOCAL (info, h))
4412 BFD_ASSERT((h->got.offset & 1) != 0);
4413 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4415 else
4417 BFD_ASSERT((h->got.offset & 1) == 0);
4418 do_glob_dat:
4419 bfd_put_32 (output_bfd, (bfd_vma) 0,
4420 htab->elf.sgot->contents + h->got.offset);
4421 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
4424 loc = htab->elf.srelgot->contents;
4425 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
4426 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4429 if (h->needs_copy)
4431 Elf_Internal_Rela rel;
4432 bfd_byte *loc;
4434 /* This symbol needs a copy reloc. Set it up. */
4436 if (h->dynindx == -1
4437 || (h->root.type != bfd_link_hash_defined
4438 && h->root.type != bfd_link_hash_defweak)
4439 || htab->srelbss == NULL)
4440 abort ();
4442 rel.r_offset = (h->root.u.def.value
4443 + h->root.u.def.section->output_section->vma
4444 + h->root.u.def.section->output_offset);
4445 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
4446 loc = htab->srelbss->contents;
4447 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
4448 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4451 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
4452 be NULL for local symbols.
4454 On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
4455 is relative to the ".got" section. */
4456 if (sym != NULL
4457 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4458 || (!htab->is_vxworks && h == htab->elf.hgot)))
4459 sym->st_shndx = SHN_ABS;
4461 return TRUE;
4464 /* Finish up local dynamic symbol handling. We set the contents of
4465 various dynamic sections here. */
4467 static bfd_boolean
4468 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4470 struct elf_link_hash_entry *h
4471 = (struct elf_link_hash_entry *) *slot;
4472 struct bfd_link_info *info
4473 = (struct bfd_link_info *) inf;
4475 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
4476 h, NULL);
4479 /* Used to decide how to sort relocs in an optimal manner for the
4480 dynamic linker, before writing them out. */
4482 static enum elf_reloc_type_class
4483 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
4485 switch (ELF32_R_TYPE (rela->r_info))
4487 case R_386_RELATIVE:
4488 return reloc_class_relative;
4489 case R_386_JUMP_SLOT:
4490 return reloc_class_plt;
4491 case R_386_COPY:
4492 return reloc_class_copy;
4493 default:
4494 return reloc_class_normal;
4498 /* Finish up the dynamic sections. */
4500 static bfd_boolean
4501 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4502 struct bfd_link_info *info)
4504 struct elf_i386_link_hash_table *htab;
4505 bfd *dynobj;
4506 asection *sdyn;
4508 htab = elf_i386_hash_table (info);
4509 dynobj = htab->elf.dynobj;
4510 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4512 if (htab->elf.dynamic_sections_created)
4514 Elf32_External_Dyn *dyncon, *dynconend;
4516 if (sdyn == NULL || htab->elf.sgot == NULL)
4517 abort ();
4519 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4520 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4521 for (; dyncon < dynconend; dyncon++)
4523 Elf_Internal_Dyn dyn;
4524 asection *s;
4526 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4528 switch (dyn.d_tag)
4530 default:
4531 if (htab->is_vxworks
4532 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4533 break;
4534 continue;
4536 case DT_PLTGOT:
4537 s = htab->elf.sgotplt;
4538 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4539 break;
4541 case DT_JMPREL:
4542 s = htab->elf.srelplt;
4543 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4544 break;
4546 case DT_PLTRELSZ:
4547 s = htab->elf.srelplt;
4548 dyn.d_un.d_val = s->size;
4549 break;
4551 case DT_RELSZ:
4552 /* My reading of the SVR4 ABI indicates that the
4553 procedure linkage table relocs (DT_JMPREL) should be
4554 included in the overall relocs (DT_REL). This is
4555 what Solaris does. However, UnixWare can not handle
4556 that case. Therefore, we override the DT_RELSZ entry
4557 here to make it not include the JMPREL relocs. */
4558 s = htab->elf.srelplt;
4559 if (s == NULL)
4560 continue;
4561 dyn.d_un.d_val -= s->size;
4562 break;
4564 case DT_REL:
4565 /* We may not be using the standard ELF linker script.
4566 If .rel.plt is the first .rel section, we adjust
4567 DT_REL to not include it. */
4568 s = htab->elf.srelplt;
4569 if (s == NULL)
4570 continue;
4571 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4572 continue;
4573 dyn.d_un.d_ptr += s->size;
4574 break;
4577 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4580 /* Fill in the first entry in the procedure linkage table. */
4581 if (htab->elf.splt && htab->elf.splt->size > 0)
4583 if (info->shared)
4585 memcpy (htab->elf.splt->contents, elf_i386_pic_plt0_entry,
4586 sizeof (elf_i386_pic_plt0_entry));
4587 memset (htab->elf.splt->contents + sizeof (elf_i386_pic_plt0_entry),
4588 htab->plt0_pad_byte,
4589 PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
4591 else
4593 memcpy (htab->elf.splt->contents, elf_i386_plt0_entry,
4594 sizeof(elf_i386_plt0_entry));
4595 memset (htab->elf.splt->contents + sizeof (elf_i386_plt0_entry),
4596 htab->plt0_pad_byte,
4597 PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
4598 bfd_put_32 (output_bfd,
4599 (htab->elf.sgotplt->output_section->vma
4600 + htab->elf.sgotplt->output_offset
4601 + 4),
4602 htab->elf.splt->contents + 2);
4603 bfd_put_32 (output_bfd,
4604 (htab->elf.sgotplt->output_section->vma
4605 + htab->elf.sgotplt->output_offset
4606 + 8),
4607 htab->elf.splt->contents + 8);
4609 if (htab->is_vxworks)
4611 Elf_Internal_Rela rel;
4613 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
4614 On IA32 we use REL relocations so the addend goes in
4615 the PLT directly. */
4616 rel.r_offset = (htab->elf.splt->output_section->vma
4617 + htab->elf.splt->output_offset
4618 + 2);
4619 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4620 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4621 htab->srelplt2->contents);
4622 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
4623 rel.r_offset = (htab->elf.splt->output_section->vma
4624 + htab->elf.splt->output_offset
4625 + 8);
4626 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4627 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4628 htab->srelplt2->contents +
4629 sizeof (Elf32_External_Rel));
4633 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4634 really seem like the right value. */
4635 elf_section_data (htab->elf.splt->output_section)
4636 ->this_hdr.sh_entsize = 4;
4638 /* Correct the .rel.plt.unloaded relocations. */
4639 if (htab->is_vxworks && !info->shared)
4641 int num_plts = (htab->elf.splt->size / PLT_ENTRY_SIZE) - 1;
4642 unsigned char *p;
4644 p = htab->srelplt2->contents;
4645 if (info->shared)
4646 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4647 else
4648 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4650 for (; num_plts; num_plts--)
4652 Elf_Internal_Rela rel;
4653 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4654 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4655 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4656 p += sizeof (Elf32_External_Rel);
4658 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4659 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4660 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4661 p += sizeof (Elf32_External_Rel);
4667 if (htab->elf.sgotplt)
4669 /* Fill in the first three entries in the global offset table. */
4670 if (htab->elf.sgotplt->size > 0)
4672 bfd_put_32 (output_bfd,
4673 (sdyn == NULL ? 0
4674 : sdyn->output_section->vma + sdyn->output_offset),
4675 htab->elf.sgotplt->contents);
4676 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
4677 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
4680 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
4683 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4684 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
4686 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4687 htab_traverse (htab->loc_hash_table,
4688 elf_i386_finish_local_dynamic_symbol,
4689 info);
4691 return TRUE;
4694 /* Return address for Ith PLT stub in section PLT, for relocation REL
4695 or (bfd_vma) -1 if it should not be included. */
4697 static bfd_vma
4698 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
4699 const arelent *rel ATTRIBUTE_UNUSED)
4701 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4704 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4706 static bfd_boolean
4707 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
4709 if (h->plt.offset != (bfd_vma) -1
4710 && !h->def_regular
4711 && !h->pointer_equality_needed)
4712 return FALSE;
4714 return _bfd_elf_hash_symbol (h);
4717 /* Hook called by the linker routine which adds symbols from an object
4718 file. */
4720 static bfd_boolean
4721 elf_i386_add_symbol_hook (bfd * abfd ATTRIBUTE_UNUSED,
4722 struct bfd_link_info * info ATTRIBUTE_UNUSED,
4723 Elf_Internal_Sym * sym,
4724 const char ** namep ATTRIBUTE_UNUSED,
4725 flagword * flagsp ATTRIBUTE_UNUSED,
4726 asection ** secp ATTRIBUTE_UNUSED,
4727 bfd_vma * valp ATTRIBUTE_UNUSED)
4729 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4730 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4732 return TRUE;
4735 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
4736 #define TARGET_LITTLE_NAME "elf32-i386"
4737 #define ELF_ARCH bfd_arch_i386
4738 #define ELF_MACHINE_CODE EM_386
4739 #define ELF_MAXPAGESIZE 0x1000
4741 #define elf_backend_can_gc_sections 1
4742 #define elf_backend_can_refcount 1
4743 #define elf_backend_want_got_plt 1
4744 #define elf_backend_plt_readonly 1
4745 #define elf_backend_want_plt_sym 0
4746 #define elf_backend_got_header_size 12
4748 /* Support RELA for objdump of prelink objects. */
4749 #define elf_info_to_howto elf_i386_info_to_howto_rel
4750 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4752 #define bfd_elf32_mkobject elf_i386_mkobject
4754 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4755 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
4756 #define bfd_elf32_bfd_link_hash_table_free elf_i386_link_hash_table_free
4757 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
4758 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
4760 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
4761 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4762 #define elf_backend_check_relocs elf_i386_check_relocs
4763 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
4764 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
4765 #define elf_backend_fake_sections elf_i386_fake_sections
4766 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4767 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4768 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
4769 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
4770 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4771 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4772 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4773 #define elf_backend_relocate_section elf_i386_relocate_section
4774 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
4775 #define elf_backend_always_size_sections elf_i386_always_size_sections
4776 #define elf_backend_omit_section_dynsym \
4777 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4778 #define elf_backend_plt_sym_val elf_i386_plt_sym_val
4779 #define elf_backend_hash_symbol elf_i386_hash_symbol
4780 #define elf_backend_add_symbol_hook elf_i386_add_symbol_hook
4781 #undef elf_backend_post_process_headers
4782 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4784 #include "elf32-target.h"
4786 /* FreeBSD support. */
4788 #undef TARGET_LITTLE_SYM
4789 #define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
4790 #undef TARGET_LITTLE_NAME
4791 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4792 #undef ELF_OSABI
4793 #define ELF_OSABI ELFOSABI_FREEBSD
4795 /* The kernel recognizes executables as valid only if they carry a
4796 "FreeBSD" label in the ELF header. So we put this label on all
4797 executables and (for simplicity) also all other object files. */
4799 static void
4800 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
4802 _bfd_elf_set_osabi (abfd, info);
4804 #ifdef OLD_FREEBSD_ABI_LABEL
4805 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4806 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4807 #endif
4810 #undef elf_backend_post_process_headers
4811 #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
4812 #undef elf32_bed
4813 #define elf32_bed elf32_i386_fbsd_bed
4815 #undef elf_backend_add_symbol_hook
4817 #include "elf32-target.h"
4819 /* VxWorks support. */
4821 #undef TARGET_LITTLE_SYM
4822 #define TARGET_LITTLE_SYM bfd_elf32_i386_vxworks_vec
4823 #undef TARGET_LITTLE_NAME
4824 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
4825 #undef ELF_OSABI
4827 /* Like elf_i386_link_hash_table_create but with tweaks for VxWorks. */
4829 static struct bfd_link_hash_table *
4830 elf_i386_vxworks_link_hash_table_create (bfd *abfd)
4832 struct bfd_link_hash_table *ret;
4833 struct elf_i386_link_hash_table *htab;
4835 ret = elf_i386_link_hash_table_create (abfd);
4836 if (ret)
4838 htab = (struct elf_i386_link_hash_table *) ret;
4839 htab->is_vxworks = 1;
4840 htab->plt0_pad_byte = 0x90;
4843 return ret;
4847 #undef elf_backend_relocs_compatible
4848 #undef elf_backend_post_process_headers
4849 #undef bfd_elf32_bfd_link_hash_table_create
4850 #define bfd_elf32_bfd_link_hash_table_create \
4851 elf_i386_vxworks_link_hash_table_create
4852 #undef elf_backend_add_symbol_hook
4853 #define elf_backend_add_symbol_hook \
4854 elf_vxworks_add_symbol_hook
4855 #undef elf_backend_link_output_symbol_hook
4856 #define elf_backend_link_output_symbol_hook \
4857 elf_vxworks_link_output_symbol_hook
4858 #undef elf_backend_emit_relocs
4859 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
4860 #undef elf_backend_final_write_processing
4861 #define elf_backend_final_write_processing \
4862 elf_vxworks_final_write_processing
4864 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4865 define it. */
4866 #undef elf_backend_want_plt_sym
4867 #define elf_backend_want_plt_sym 1
4869 #undef elf32_bed
4870 #define elf32_bed elf32_i386_vxworks_bed
4872 #include "elf32-target.h"