PR 9934
[binutils.git] / bfd / elf32-i386.c
blob3c56685f1330ecfe64fb30a5c0e689c1e55029ab
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"
30 /* 386 uses REL relocations instead of RELA. */
31 #define USE_REL 1
33 #include "elf/i386.h"
35 static reloc_howto_type elf_howto_table[]=
37 HOWTO(R_386_NONE, 0, 0, 0, FALSE, 0, complain_overflow_bitfield,
38 bfd_elf_generic_reloc, "R_386_NONE",
39 TRUE, 0x00000000, 0x00000000, FALSE),
40 HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
41 bfd_elf_generic_reloc, "R_386_32",
42 TRUE, 0xffffffff, 0xffffffff, FALSE),
43 HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
44 bfd_elf_generic_reloc, "R_386_PC32",
45 TRUE, 0xffffffff, 0xffffffff, TRUE),
46 HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
47 bfd_elf_generic_reloc, "R_386_GOT32",
48 TRUE, 0xffffffff, 0xffffffff, FALSE),
49 HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
50 bfd_elf_generic_reloc, "R_386_PLT32",
51 TRUE, 0xffffffff, 0xffffffff, TRUE),
52 HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
53 bfd_elf_generic_reloc, "R_386_COPY",
54 TRUE, 0xffffffff, 0xffffffff, FALSE),
55 HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
56 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
57 TRUE, 0xffffffff, 0xffffffff, FALSE),
58 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
59 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
60 TRUE, 0xffffffff, 0xffffffff, FALSE),
61 HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_386_RELATIVE",
63 TRUE, 0xffffffff, 0xffffffff, FALSE),
64 HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
65 bfd_elf_generic_reloc, "R_386_GOTOFF",
66 TRUE, 0xffffffff, 0xffffffff, FALSE),
67 HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
68 bfd_elf_generic_reloc, "R_386_GOTPC",
69 TRUE, 0xffffffff, 0xffffffff, TRUE),
71 /* We have a gap in the reloc numbers here.
72 R_386_standard counts the number up to this point, and
73 R_386_ext_offset is the value to subtract from a reloc type of
74 R_386_16 thru R_386_PC8 to form an index into this table. */
75 #define R_386_standard (R_386_GOTPC + 1)
76 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
78 /* These relocs are a GNU extension. */
79 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
80 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
81 TRUE, 0xffffffff, 0xffffffff, FALSE),
82 HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_386_TLS_IE",
84 TRUE, 0xffffffff, 0xffffffff, FALSE),
85 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
86 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
87 TRUE, 0xffffffff, 0xffffffff, FALSE),
88 HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
89 bfd_elf_generic_reloc, "R_386_TLS_LE",
90 TRUE, 0xffffffff, 0xffffffff, FALSE),
91 HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
92 bfd_elf_generic_reloc, "R_386_TLS_GD",
93 TRUE, 0xffffffff, 0xffffffff, FALSE),
94 HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
95 bfd_elf_generic_reloc, "R_386_TLS_LDM",
96 TRUE, 0xffffffff, 0xffffffff, FALSE),
97 HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
98 bfd_elf_generic_reloc, "R_386_16",
99 TRUE, 0xffff, 0xffff, FALSE),
100 HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
101 bfd_elf_generic_reloc, "R_386_PC16",
102 TRUE, 0xffff, 0xffff, TRUE),
103 HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
104 bfd_elf_generic_reloc, "R_386_8",
105 TRUE, 0xff, 0xff, FALSE),
106 HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
107 bfd_elf_generic_reloc, "R_386_PC8",
108 TRUE, 0xff, 0xff, TRUE),
110 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
111 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
112 /* These are common with Solaris TLS implementation. */
113 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
114 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
115 TRUE, 0xffffffff, 0xffffffff, FALSE),
116 HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
117 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
118 TRUE, 0xffffffff, 0xffffffff, FALSE),
119 HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
120 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
121 TRUE, 0xffffffff, 0xffffffff, FALSE),
122 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
123 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
124 TRUE, 0xffffffff, 0xffffffff, FALSE),
125 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
126 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
127 TRUE, 0xffffffff, 0xffffffff, FALSE),
128 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
129 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
130 TRUE, 0xffffffff, 0xffffffff, FALSE),
131 EMPTY_HOWTO (38),
132 HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
133 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
134 TRUE, 0xffffffff, 0xffffffff, FALSE),
135 HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
136 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
137 FALSE, 0, 0, FALSE),
138 HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
139 bfd_elf_generic_reloc, "R_386_TLS_DESC",
140 TRUE, 0xffffffff, 0xffffffff, FALSE),
142 /* Another gap. */
143 #define R_386_tls (R_386_TLS_DESC + 1 - R_386_tls_offset)
144 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_tls)
146 /* GNU extension to record C++ vtable hierarchy. */
147 HOWTO (R_386_GNU_VTINHERIT, /* type */
148 0, /* rightshift */
149 2, /* size (0 = byte, 1 = short, 2 = long) */
150 0, /* bitsize */
151 FALSE, /* pc_relative */
152 0, /* bitpos */
153 complain_overflow_dont, /* complain_on_overflow */
154 NULL, /* special_function */
155 "R_386_GNU_VTINHERIT", /* name */
156 FALSE, /* partial_inplace */
157 0, /* src_mask */
158 0, /* dst_mask */
159 FALSE), /* pcrel_offset */
161 /* GNU extension to record C++ vtable member usage. */
162 HOWTO (R_386_GNU_VTENTRY, /* type */
163 0, /* rightshift */
164 2, /* size (0 = byte, 1 = short, 2 = long) */
165 0, /* bitsize */
166 FALSE, /* pc_relative */
167 0, /* bitpos */
168 complain_overflow_dont, /* complain_on_overflow */
169 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
170 "R_386_GNU_VTENTRY", /* name */
171 FALSE, /* partial_inplace */
172 0, /* src_mask */
173 0, /* dst_mask */
174 FALSE) /* pcrel_offset */
176 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
180 #ifdef DEBUG_GEN_RELOC
181 #define TRACE(str) \
182 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
183 #else
184 #define TRACE(str)
185 #endif
187 static reloc_howto_type *
188 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
189 bfd_reloc_code_real_type code)
191 switch (code)
193 case BFD_RELOC_NONE:
194 TRACE ("BFD_RELOC_NONE");
195 return &elf_howto_table[R_386_NONE];
197 case BFD_RELOC_32:
198 TRACE ("BFD_RELOC_32");
199 return &elf_howto_table[R_386_32];
201 case BFD_RELOC_CTOR:
202 TRACE ("BFD_RELOC_CTOR");
203 return &elf_howto_table[R_386_32];
205 case BFD_RELOC_32_PCREL:
206 TRACE ("BFD_RELOC_PC32");
207 return &elf_howto_table[R_386_PC32];
209 case BFD_RELOC_386_GOT32:
210 TRACE ("BFD_RELOC_386_GOT32");
211 return &elf_howto_table[R_386_GOT32];
213 case BFD_RELOC_386_PLT32:
214 TRACE ("BFD_RELOC_386_PLT32");
215 return &elf_howto_table[R_386_PLT32];
217 case BFD_RELOC_386_COPY:
218 TRACE ("BFD_RELOC_386_COPY");
219 return &elf_howto_table[R_386_COPY];
221 case BFD_RELOC_386_GLOB_DAT:
222 TRACE ("BFD_RELOC_386_GLOB_DAT");
223 return &elf_howto_table[R_386_GLOB_DAT];
225 case BFD_RELOC_386_JUMP_SLOT:
226 TRACE ("BFD_RELOC_386_JUMP_SLOT");
227 return &elf_howto_table[R_386_JUMP_SLOT];
229 case BFD_RELOC_386_RELATIVE:
230 TRACE ("BFD_RELOC_386_RELATIVE");
231 return &elf_howto_table[R_386_RELATIVE];
233 case BFD_RELOC_386_GOTOFF:
234 TRACE ("BFD_RELOC_386_GOTOFF");
235 return &elf_howto_table[R_386_GOTOFF];
237 case BFD_RELOC_386_GOTPC:
238 TRACE ("BFD_RELOC_386_GOTPC");
239 return &elf_howto_table[R_386_GOTPC];
241 /* These relocs are a GNU extension. */
242 case BFD_RELOC_386_TLS_TPOFF:
243 TRACE ("BFD_RELOC_386_TLS_TPOFF");
244 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
246 case BFD_RELOC_386_TLS_IE:
247 TRACE ("BFD_RELOC_386_TLS_IE");
248 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
250 case BFD_RELOC_386_TLS_GOTIE:
251 TRACE ("BFD_RELOC_386_TLS_GOTIE");
252 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
254 case BFD_RELOC_386_TLS_LE:
255 TRACE ("BFD_RELOC_386_TLS_LE");
256 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
258 case BFD_RELOC_386_TLS_GD:
259 TRACE ("BFD_RELOC_386_TLS_GD");
260 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
262 case BFD_RELOC_386_TLS_LDM:
263 TRACE ("BFD_RELOC_386_TLS_LDM");
264 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
266 case BFD_RELOC_16:
267 TRACE ("BFD_RELOC_16");
268 return &elf_howto_table[R_386_16 - R_386_ext_offset];
270 case BFD_RELOC_16_PCREL:
271 TRACE ("BFD_RELOC_16_PCREL");
272 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
274 case BFD_RELOC_8:
275 TRACE ("BFD_RELOC_8");
276 return &elf_howto_table[R_386_8 - R_386_ext_offset];
278 case BFD_RELOC_8_PCREL:
279 TRACE ("BFD_RELOC_8_PCREL");
280 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
282 /* Common with Sun TLS implementation. */
283 case BFD_RELOC_386_TLS_LDO_32:
284 TRACE ("BFD_RELOC_386_TLS_LDO_32");
285 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
287 case BFD_RELOC_386_TLS_IE_32:
288 TRACE ("BFD_RELOC_386_TLS_IE_32");
289 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
291 case BFD_RELOC_386_TLS_LE_32:
292 TRACE ("BFD_RELOC_386_TLS_LE_32");
293 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
295 case BFD_RELOC_386_TLS_DTPMOD32:
296 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
297 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
299 case BFD_RELOC_386_TLS_DTPOFF32:
300 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
301 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
303 case BFD_RELOC_386_TLS_TPOFF32:
304 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
305 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
307 case BFD_RELOC_386_TLS_GOTDESC:
308 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
309 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
311 case BFD_RELOC_386_TLS_DESC_CALL:
312 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
313 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
315 case BFD_RELOC_386_TLS_DESC:
316 TRACE ("BFD_RELOC_386_TLS_DESC");
317 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
319 case BFD_RELOC_VTABLE_INHERIT:
320 TRACE ("BFD_RELOC_VTABLE_INHERIT");
321 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
323 case BFD_RELOC_VTABLE_ENTRY:
324 TRACE ("BFD_RELOC_VTABLE_ENTRY");
325 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
327 default:
328 break;
331 TRACE ("Unknown");
332 return 0;
335 static reloc_howto_type *
336 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
337 const char *r_name)
339 unsigned int i;
341 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
342 if (elf_howto_table[i].name != NULL
343 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
344 return &elf_howto_table[i];
346 return NULL;
349 static reloc_howto_type *
350 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
352 unsigned int indx;
354 if ((indx = r_type) >= R_386_standard
355 && ((indx = r_type - R_386_ext_offset) - R_386_standard
356 >= R_386_ext - R_386_standard)
357 && ((indx = r_type - R_386_tls_offset) - R_386_ext
358 >= R_386_tls - R_386_ext)
359 && ((indx = r_type - R_386_vt_offset) - R_386_tls
360 >= R_386_vt - R_386_tls))
362 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
363 abfd, (int) r_type);
364 indx = R_386_NONE;
366 BFD_ASSERT (elf_howto_table [indx].type == r_type);
367 return &elf_howto_table[indx];
370 static void
371 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
372 arelent *cache_ptr,
373 Elf_Internal_Rela *dst)
375 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
376 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
379 /* Return whether a symbol name implies a local label. The UnixWare
380 2.1 cc generates temporary symbols that start with .X, so we
381 recognize them here. FIXME: do other SVR4 compilers also use .X?.
382 If so, we should move the .X recognition into
383 _bfd_elf_is_local_label_name. */
385 static bfd_boolean
386 elf_i386_is_local_label_name (bfd *abfd, const char *name)
388 if (name[0] == '.' && name[1] == 'X')
389 return TRUE;
391 return _bfd_elf_is_local_label_name (abfd, name);
394 /* Support for core dump NOTE sections. */
396 static bfd_boolean
397 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
399 int offset;
400 size_t size;
402 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
404 int pr_version = bfd_get_32 (abfd, note->descdata);
406 if (pr_version != 1)
407 return FALSE;
409 /* pr_cursig */
410 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 20);
412 /* pr_pid */
413 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
415 /* pr_reg */
416 offset = 28;
417 size = bfd_get_32 (abfd, note->descdata + 8);
419 else
421 switch (note->descsz)
423 default:
424 return FALSE;
426 case 144: /* Linux/i386 */
427 /* pr_cursig */
428 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
430 /* pr_pid */
431 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
433 /* pr_reg */
434 offset = 72;
435 size = 68;
437 break;
441 /* Make a ".reg/999" section. */
442 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
443 size, note->descpos + offset);
446 static bfd_boolean
447 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
449 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
451 int pr_version = bfd_get_32 (abfd, note->descdata);
453 if (pr_version != 1)
454 return FALSE;
456 elf_tdata (abfd)->core_program
457 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
458 elf_tdata (abfd)->core_command
459 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
461 else
463 switch (note->descsz)
465 default:
466 return FALSE;
468 case 124: /* Linux/i386 elf_prpsinfo. */
469 elf_tdata (abfd)->core_program
470 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
471 elf_tdata (abfd)->core_command
472 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
476 /* Note that for some reason, a spurious space is tacked
477 onto the end of the args in some (at least one anyway)
478 implementations, so strip it off if it exists. */
480 char *command = elf_tdata (abfd)->core_command;
481 int n = strlen (command);
483 if (0 < n && command[n - 1] == ' ')
484 command[n - 1] = '\0';
487 return TRUE;
490 /* Functions for the i386 ELF linker.
492 In order to gain some understanding of code in this file without
493 knowing all the intricate details of the linker, note the
494 following:
496 Functions named elf_i386_* are called by external routines, other
497 functions are only called locally. elf_i386_* functions appear
498 in this file more or less in the order in which they are called
499 from external routines. eg. elf_i386_check_relocs is called
500 early in the link process, elf_i386_finish_dynamic_sections is
501 one of the last functions. */
504 /* The name of the dynamic interpreter. This is put in the .interp
505 section. */
507 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
509 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
510 copying dynamic variables from a shared lib into an app's dynbss
511 section, and instead use a dynamic relocation to point into the
512 shared lib. */
513 #define ELIMINATE_COPY_RELOCS 1
515 /* The size in bytes of an entry in the procedure linkage table. */
517 #define PLT_ENTRY_SIZE 16
519 /* The first entry in an absolute procedure linkage table looks like
520 this. See the SVR4 ABI i386 supplement to see how this works.
521 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
523 static const bfd_byte elf_i386_plt0_entry[12] =
525 0xff, 0x35, /* pushl contents of address */
526 0, 0, 0, 0, /* replaced with address of .got + 4. */
527 0xff, 0x25, /* jmp indirect */
528 0, 0, 0, 0 /* replaced with address of .got + 8. */
531 /* Subsequent entries in an absolute procedure linkage table look like
532 this. */
534 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
536 0xff, 0x25, /* jmp indirect */
537 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
538 0x68, /* pushl immediate */
539 0, 0, 0, 0, /* replaced with offset into relocation table. */
540 0xe9, /* jmp relative */
541 0, 0, 0, 0 /* replaced with offset to start of .plt. */
544 /* The first entry in a PIC procedure linkage table look like this.
545 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */
547 static const bfd_byte elf_i386_pic_plt0_entry[12] =
549 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
550 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
553 /* Subsequent entries in a PIC procedure linkage table look like this. */
555 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
557 0xff, 0xa3, /* jmp *offset(%ebx) */
558 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
559 0x68, /* pushl immediate */
560 0, 0, 0, 0, /* replaced with offset into relocation table. */
561 0xe9, /* jmp relative */
562 0, 0, 0, 0 /* replaced with offset to start of .plt. */
565 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
566 for the PLTResolve stub and then for each PLT entry. */
567 #define PLTRESOLVE_RELOCS_SHLIB 0
568 #define PLTRESOLVE_RELOCS 2
569 #define PLT_NON_JUMP_SLOT_RELOCS 2
571 /* The i386 linker needs to keep track of the number of relocs that it
572 decides to copy as dynamic relocs in check_relocs for each symbol.
573 This is so that it can later discard them if they are found to be
574 unnecessary. We store the information in a field extending the
575 regular ELF linker hash table. */
577 struct elf_i386_dyn_relocs
579 struct elf_i386_dyn_relocs *next;
581 /* The input section of the reloc. */
582 asection *sec;
584 /* Total number of relocs copied for the input section. */
585 bfd_size_type count;
587 /* Number of pc-relative relocs copied for the input section. */
588 bfd_size_type pc_count;
591 /* i386 ELF linker hash entry. */
593 struct elf_i386_link_hash_entry
595 struct elf_link_hash_entry elf;
597 /* Track dynamic relocs copied for this symbol. */
598 struct elf_i386_dyn_relocs *dyn_relocs;
600 #define GOT_UNKNOWN 0
601 #define GOT_NORMAL 1
602 #define GOT_TLS_GD 2
603 #define GOT_TLS_IE 4
604 #define GOT_TLS_IE_POS 5
605 #define GOT_TLS_IE_NEG 6
606 #define GOT_TLS_IE_BOTH 7
607 #define GOT_TLS_GDESC 8
608 #define GOT_TLS_GD_BOTH_P(type) \
609 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
610 #define GOT_TLS_GD_P(type) \
611 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
612 #define GOT_TLS_GDESC_P(type) \
613 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
614 #define GOT_TLS_GD_ANY_P(type) \
615 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
616 unsigned char tls_type;
618 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
619 starting at the end of the jump table. */
620 bfd_vma tlsdesc_got;
623 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
625 struct elf_i386_obj_tdata
627 struct elf_obj_tdata root;
629 /* tls_type for each local got entry. */
630 char *local_got_tls_type;
632 /* GOTPLT entries for TLS descriptors. */
633 bfd_vma *local_tlsdesc_gotent;
636 #define elf_i386_tdata(abfd) \
637 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
639 #define elf_i386_local_got_tls_type(abfd) \
640 (elf_i386_tdata (abfd)->local_got_tls_type)
642 #define elf_i386_local_tlsdesc_gotent(abfd) \
643 (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
645 #define is_i386_elf(bfd) \
646 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
647 && elf_tdata (bfd) != NULL \
648 && elf_object_id (bfd) == I386_ELF_TDATA)
650 static bfd_boolean
651 elf_i386_mkobject (bfd *abfd)
653 return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
654 I386_ELF_TDATA);
657 /* i386 ELF linker hash table. */
659 struct elf_i386_link_hash_table
661 struct elf_link_hash_table elf;
663 /* Short-cuts to get to dynamic linker sections. */
664 asection *sgot;
665 asection *sgotplt;
666 asection *srelgot;
667 asection *splt;
668 asection *srelplt;
669 asection *sdynbss;
670 asection *srelbss;
672 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
673 asection *srelplt2;
675 /* True if the target system is VxWorks. */
676 int is_vxworks;
678 /* Value used to fill the last word of the first plt entry. */
679 bfd_byte plt0_pad_byte;
681 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
682 bfd_vma next_tls_desc_index;
684 union {
685 bfd_signed_vma refcount;
686 bfd_vma offset;
687 } tls_ldm_got;
689 /* The amount of space used by the reserved portion of the sgotplt
690 section, plus whatever space is used by the jump slots. */
691 bfd_vma sgotplt_jump_table_size;
693 /* Small local sym to section mapping cache. */
694 struct sym_sec_cache sym_sec;
696 /* _TLS_MODULE_BASE_ symbol. */
697 struct bfd_link_hash_entry *tls_module_base;
700 /* Get the i386 ELF linker hash table from a link_info structure. */
702 #define elf_i386_hash_table(p) \
703 ((struct elf_i386_link_hash_table *) ((p)->hash))
705 #define elf_i386_compute_jump_table_size(htab) \
706 ((htab)->next_tls_desc_index * 4)
708 /* Create an entry in an i386 ELF linker hash table. */
710 static struct bfd_hash_entry *
711 link_hash_newfunc (struct bfd_hash_entry *entry,
712 struct bfd_hash_table *table,
713 const char *string)
715 /* Allocate the structure if it has not already been allocated by a
716 subclass. */
717 if (entry == NULL)
719 entry = bfd_hash_allocate (table,
720 sizeof (struct elf_i386_link_hash_entry));
721 if (entry == NULL)
722 return entry;
725 /* Call the allocation method of the superclass. */
726 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
727 if (entry != NULL)
729 struct elf_i386_link_hash_entry *eh;
731 eh = (struct elf_i386_link_hash_entry *) entry;
732 eh->dyn_relocs = NULL;
733 eh->tls_type = GOT_UNKNOWN;
734 eh->tlsdesc_got = (bfd_vma) -1;
737 return entry;
740 /* Create an i386 ELF linker hash table. */
742 static struct bfd_link_hash_table *
743 elf_i386_link_hash_table_create (bfd *abfd)
745 struct elf_i386_link_hash_table *ret;
746 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
748 ret = bfd_malloc (amt);
749 if (ret == NULL)
750 return NULL;
752 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
753 sizeof (struct elf_i386_link_hash_entry)))
755 free (ret);
756 return NULL;
759 ret->sgot = NULL;
760 ret->sgotplt = NULL;
761 ret->srelgot = NULL;
762 ret->splt = NULL;
763 ret->srelplt = NULL;
764 ret->sdynbss = NULL;
765 ret->srelbss = NULL;
766 ret->tls_ldm_got.refcount = 0;
767 ret->next_tls_desc_index = 0;
768 ret->sgotplt_jump_table_size = 0;
769 ret->sym_sec.abfd = NULL;
770 ret->is_vxworks = 0;
771 ret->srelplt2 = NULL;
772 ret->plt0_pad_byte = 0;
773 ret->tls_module_base = NULL;
775 return &ret->elf.root;
778 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
779 shortcuts to them in our hash table. */
781 static bfd_boolean
782 create_got_section (bfd *dynobj, struct bfd_link_info *info)
784 struct elf_i386_link_hash_table *htab;
786 if (! _bfd_elf_create_got_section (dynobj, info))
787 return FALSE;
789 htab = elf_i386_hash_table (info);
790 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
791 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
792 if (!htab->sgot || !htab->sgotplt)
793 abort ();
795 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rel.got",
796 (SEC_ALLOC | SEC_LOAD
797 | SEC_HAS_CONTENTS
798 | SEC_IN_MEMORY
799 | SEC_LINKER_CREATED
800 | SEC_READONLY));
801 if (htab->srelgot == NULL
802 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
803 return FALSE;
804 return TRUE;
807 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
808 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
809 hash table. */
811 static bfd_boolean
812 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
814 struct elf_i386_link_hash_table *htab;
816 htab = elf_i386_hash_table (info);
817 if (!htab->sgot && !create_got_section (dynobj, info))
818 return FALSE;
820 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
821 return FALSE;
823 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
824 htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
825 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
826 if (!info->shared)
827 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
829 if (!htab->splt || !htab->srelplt || !htab->sdynbss
830 || (!info->shared && !htab->srelbss))
831 abort ();
833 if (htab->is_vxworks
834 && !elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
835 return FALSE;
837 return TRUE;
840 /* Copy the extra info we tack onto an elf_link_hash_entry. */
842 static void
843 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
844 struct elf_link_hash_entry *dir,
845 struct elf_link_hash_entry *ind)
847 struct elf_i386_link_hash_entry *edir, *eind;
849 edir = (struct elf_i386_link_hash_entry *) dir;
850 eind = (struct elf_i386_link_hash_entry *) ind;
852 if (eind->dyn_relocs != NULL)
854 if (edir->dyn_relocs != NULL)
856 struct elf_i386_dyn_relocs **pp;
857 struct elf_i386_dyn_relocs *p;
859 /* Add reloc counts against the indirect sym to the direct sym
860 list. Merge any entries against the same section. */
861 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
863 struct elf_i386_dyn_relocs *q;
865 for (q = edir->dyn_relocs; q != NULL; q = q->next)
866 if (q->sec == p->sec)
868 q->pc_count += p->pc_count;
869 q->count += p->count;
870 *pp = p->next;
871 break;
873 if (q == NULL)
874 pp = &p->next;
876 *pp = edir->dyn_relocs;
879 edir->dyn_relocs = eind->dyn_relocs;
880 eind->dyn_relocs = NULL;
883 if (ind->root.type == bfd_link_hash_indirect
884 && dir->got.refcount <= 0)
886 edir->tls_type = eind->tls_type;
887 eind->tls_type = GOT_UNKNOWN;
890 if (ELIMINATE_COPY_RELOCS
891 && ind->root.type != bfd_link_hash_indirect
892 && dir->dynamic_adjusted)
894 /* If called to transfer flags for a weakdef during processing
895 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
896 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
897 dir->ref_dynamic |= ind->ref_dynamic;
898 dir->ref_regular |= ind->ref_regular;
899 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
900 dir->needs_plt |= ind->needs_plt;
901 dir->pointer_equality_needed |= ind->pointer_equality_needed;
903 else
904 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
907 typedef union
909 unsigned char c[2];
910 uint16_t i;
912 i386_opcode16;
914 /* Return TRUE if the TLS access code sequence support transition
915 from R_TYPE. */
917 static bfd_boolean
918 elf_i386_check_tls_transition (bfd *abfd, asection *sec,
919 bfd_byte *contents,
920 Elf_Internal_Shdr *symtab_hdr,
921 struct elf_link_hash_entry **sym_hashes,
922 unsigned int r_type,
923 const Elf_Internal_Rela *rel,
924 const Elf_Internal_Rela *relend)
926 unsigned int val, type;
927 unsigned long r_symndx;
928 struct elf_link_hash_entry *h;
929 bfd_vma offset;
931 /* Get the section contents. */
932 if (contents == NULL)
934 if (elf_section_data (sec)->this_hdr.contents != NULL)
935 contents = elf_section_data (sec)->this_hdr.contents;
936 else
938 /* FIXME: How to better handle error condition? */
939 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
940 return FALSE;
942 /* Cache the section contents for elf_link_input_bfd. */
943 elf_section_data (sec)->this_hdr.contents = contents;
947 offset = rel->r_offset;
948 switch (r_type)
950 case R_386_TLS_GD:
951 case R_386_TLS_LDM:
952 if (offset < 2 || (rel + 1) >= relend)
953 return FALSE;
955 type = bfd_get_8 (abfd, contents + offset - 2);
956 if (r_type == R_386_TLS_GD)
958 /* Check transition from GD access model. Only
959 leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
960 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
961 can transit to different access model. */
962 if ((offset + 10) > sec->size ||
963 (type != 0x8d && type != 0x04))
964 return FALSE;
966 val = bfd_get_8 (abfd, contents + offset - 1);
967 if (type == 0x04)
969 /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
970 if (offset < 3)
971 return FALSE;
973 if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
974 return FALSE;
976 if ((val & 0xc7) != 0x05 || val == (4 << 3))
977 return FALSE;
979 else
981 /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop */
982 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
983 return FALSE;
985 if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
986 return FALSE;
989 else
991 /* Check transition from LD access model. Only
992 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
993 can transit to different access model. */
994 if (type != 0x8d || (offset + 9) > sec->size)
995 return FALSE;
997 val = bfd_get_8 (abfd, contents + offset - 1);
998 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
999 return FALSE;
1002 if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
1003 return FALSE;
1005 r_symndx = ELF32_R_SYM (rel[1].r_info);
1006 if (r_symndx < symtab_hdr->sh_info)
1007 return FALSE;
1009 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1010 /* Use strncmp to check ___tls_get_addr since ___tls_get_addr
1011 may be versioned. */
1012 return (h != NULL
1013 && h->root.root.string != NULL
1014 && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1015 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
1016 && (strncmp (h->root.root.string, "___tls_get_addr",
1017 15) == 0));
1019 case R_386_TLS_IE:
1020 /* Check transition from IE access model:
1021 movl foo@indntpoff(%rip), %eax
1022 movl foo@indntpoff(%rip), %reg
1023 addl foo@indntpoff(%rip), %reg
1026 if (offset < 1 || (offset + 4) > sec->size)
1027 return FALSE;
1029 /* Check "movl foo@tpoff(%rip), %eax" first. */
1030 val = bfd_get_8 (abfd, contents + offset - 1);
1031 if (val == 0xa1)
1032 return TRUE;
1034 if (offset < 2)
1035 return FALSE;
1037 /* Check movl|addl foo@tpoff(%rip), %reg. */
1038 type = bfd_get_8 (abfd, contents + offset - 2);
1039 return ((type == 0x8b || type == 0x03)
1040 && (val & 0xc7) == 0x05);
1042 case R_386_TLS_GOTIE:
1043 case R_386_TLS_IE_32:
1044 /* Check transition from {IE_32,GOTIE} access model:
1045 subl foo@{tpoff,gontoff}(%reg1), %reg2
1046 movl foo@{tpoff,gontoff}(%reg1), %reg2
1047 addl foo@{tpoff,gontoff}(%reg1), %reg2
1050 if (offset < 2 || (offset + 4) > sec->size)
1051 return FALSE;
1053 val = bfd_get_8 (abfd, contents + offset - 1);
1054 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1055 return FALSE;
1057 type = bfd_get_8 (abfd, contents + offset - 2);
1058 return type == 0x8b || type == 0x2b || type == 0x03;
1060 case R_386_TLS_GOTDESC:
1061 /* Check transition from GDesc access model:
1062 leal x@tlsdesc(%ebx), %eax
1064 Make sure it's a leal adding ebx to a 32-bit offset
1065 into any register, although it's probably almost always
1066 going to be eax. */
1068 if (offset < 2 || (offset + 4) > sec->size)
1069 return FALSE;
1071 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1072 return FALSE;
1074 val = bfd_get_8 (abfd, contents + offset - 1);
1075 return (val & 0xc7) == 0x83;
1077 case R_386_TLS_DESC_CALL:
1078 /* Check transition from GDesc access model:
1079 call *x@tlsdesc(%rax)
1081 if (offset + 2 <= sec->size)
1083 /* Make sure that it's a call *x@tlsdesc(%rax). */
1084 static i386_opcode16 call = { { 0xff, 0x10 } };
1085 return bfd_get_16 (abfd, contents + offset) == call.i;
1088 return FALSE;
1090 default:
1091 abort ();
1095 /* Return TRUE if the TLS access transition is OK or no transition
1096 will be performed. Update R_TYPE if there is a transition. */
1098 static bfd_boolean
1099 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1100 asection *sec, bfd_byte *contents,
1101 Elf_Internal_Shdr *symtab_hdr,
1102 struct elf_link_hash_entry **sym_hashes,
1103 unsigned int *r_type, int tls_type,
1104 const Elf_Internal_Rela *rel,
1105 const Elf_Internal_Rela *relend,
1106 struct elf_link_hash_entry *h)
1108 unsigned int from_type = *r_type;
1109 unsigned int to_type = from_type;
1110 bfd_boolean check = TRUE;
1112 switch (from_type)
1114 case R_386_TLS_GD:
1115 case R_386_TLS_GOTDESC:
1116 case R_386_TLS_DESC_CALL:
1117 case R_386_TLS_IE_32:
1118 case R_386_TLS_IE:
1119 case R_386_TLS_GOTIE:
1120 if (!info->shared)
1122 if (h == NULL)
1123 to_type = R_386_TLS_LE_32;
1124 else if (from_type != R_386_TLS_IE
1125 && from_type != R_386_TLS_GOTIE)
1126 to_type = R_386_TLS_IE_32;
1129 /* When we are called from elf_i386_relocate_section, CONTENTS
1130 isn't NULL and there may be additional transitions based on
1131 TLS_TYPE. */
1132 if (contents != NULL)
1134 unsigned int new_to_type = to_type;
1136 if (!info->shared
1137 && h != NULL
1138 && h->dynindx == -1
1139 && (tls_type & GOT_TLS_IE))
1140 new_to_type = R_386_TLS_LE_32;
1142 if (to_type == R_386_TLS_GD
1143 || to_type == R_386_TLS_GOTDESC
1144 || to_type == R_386_TLS_DESC_CALL)
1146 if (tls_type == GOT_TLS_IE_POS)
1147 new_to_type = R_386_TLS_GOTIE;
1148 else if (tls_type & GOT_TLS_IE)
1149 new_to_type = R_386_TLS_IE_32;
1152 /* We checked the transition before when we were called from
1153 elf_i386_check_relocs. We only want to check the new
1154 transition which hasn't been checked before. */
1155 check = new_to_type != to_type && from_type == to_type;
1156 to_type = new_to_type;
1159 break;
1161 case R_386_TLS_LDM:
1162 if (!info->shared)
1163 to_type = R_386_TLS_LE_32;
1164 break;
1166 default:
1167 return TRUE;
1170 /* Return TRUE if there is no transition. */
1171 if (from_type == to_type)
1172 return TRUE;
1174 /* Check if the transition can be performed. */
1175 if (check
1176 && ! elf_i386_check_tls_transition (abfd, sec, contents,
1177 symtab_hdr, sym_hashes,
1178 from_type, rel, relend))
1180 reloc_howto_type *from, *to;
1182 from = elf_i386_rtype_to_howto (abfd, from_type);
1183 to = elf_i386_rtype_to_howto (abfd, to_type);
1185 (*_bfd_error_handler)
1186 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1187 "in section `%A' failed"),
1188 abfd, sec, from->name, to->name,
1189 h ? h->root.root.string : "a local symbol",
1190 (unsigned long) rel->r_offset);
1191 bfd_set_error (bfd_error_bad_value);
1192 return FALSE;
1195 *r_type = to_type;
1196 return TRUE;
1199 /* Look through the relocs for a section during the first phase, and
1200 calculate needed space in the global offset table, procedure linkage
1201 table, and dynamic reloc sections. */
1203 static bfd_boolean
1204 elf_i386_check_relocs (bfd *abfd,
1205 struct bfd_link_info *info,
1206 asection *sec,
1207 const Elf_Internal_Rela *relocs)
1209 struct elf_i386_link_hash_table *htab;
1210 Elf_Internal_Shdr *symtab_hdr;
1211 struct elf_link_hash_entry **sym_hashes;
1212 const Elf_Internal_Rela *rel;
1213 const Elf_Internal_Rela *rel_end;
1214 asection *sreloc;
1216 if (info->relocatable)
1217 return TRUE;
1219 BFD_ASSERT (is_i386_elf (abfd));
1221 htab = elf_i386_hash_table (info);
1222 symtab_hdr = &elf_symtab_hdr (abfd);
1223 sym_hashes = elf_sym_hashes (abfd);
1225 sreloc = NULL;
1227 rel_end = relocs + sec->reloc_count;
1228 for (rel = relocs; rel < rel_end; rel++)
1230 unsigned int r_type;
1231 unsigned long r_symndx;
1232 struct elf_link_hash_entry *h;
1234 r_symndx = ELF32_R_SYM (rel->r_info);
1235 r_type = ELF32_R_TYPE (rel->r_info);
1237 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1239 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1240 abfd,
1241 r_symndx);
1242 return FALSE;
1245 if (r_symndx < symtab_hdr->sh_info)
1246 h = NULL;
1247 else
1249 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1250 while (h->root.type == bfd_link_hash_indirect
1251 || h->root.type == bfd_link_hash_warning)
1252 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1255 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1256 symtab_hdr, sym_hashes,
1257 &r_type, GOT_UNKNOWN,
1258 rel, rel_end, h))
1259 return FALSE;
1261 switch (r_type)
1263 case R_386_TLS_LDM:
1264 htab->tls_ldm_got.refcount += 1;
1265 goto create_got;
1267 case R_386_PLT32:
1268 /* This symbol requires a procedure linkage table entry. We
1269 actually build the entry in adjust_dynamic_symbol,
1270 because this might be a case of linking PIC code which is
1271 never referenced by a dynamic object, in which case we
1272 don't need to generate a procedure linkage table entry
1273 after all. */
1275 /* If this is a local symbol, we resolve it directly without
1276 creating a procedure linkage table entry. */
1277 if (h == NULL)
1278 continue;
1280 h->needs_plt = 1;
1281 h->plt.refcount += 1;
1282 break;
1284 case R_386_TLS_IE_32:
1285 case R_386_TLS_IE:
1286 case R_386_TLS_GOTIE:
1287 if (info->shared)
1288 info->flags |= DF_STATIC_TLS;
1289 /* Fall through */
1291 case R_386_GOT32:
1292 case R_386_TLS_GD:
1293 case R_386_TLS_GOTDESC:
1294 case R_386_TLS_DESC_CALL:
1295 /* This symbol requires a global offset table entry. */
1297 int tls_type, old_tls_type;
1299 switch (r_type)
1301 default:
1302 case R_386_GOT32: tls_type = GOT_NORMAL; break;
1303 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1304 case R_386_TLS_GOTDESC:
1305 case R_386_TLS_DESC_CALL:
1306 tls_type = GOT_TLS_GDESC; break;
1307 case R_386_TLS_IE_32:
1308 if (ELF32_R_TYPE (rel->r_info) == r_type)
1309 tls_type = GOT_TLS_IE_NEG;
1310 else
1311 /* If this is a GD->IE transition, we may use either of
1312 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1313 tls_type = GOT_TLS_IE;
1314 break;
1315 case R_386_TLS_IE:
1316 case R_386_TLS_GOTIE:
1317 tls_type = GOT_TLS_IE_POS; break;
1320 if (h != NULL)
1322 h->got.refcount += 1;
1323 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1325 else
1327 bfd_signed_vma *local_got_refcounts;
1329 /* This is a global offset table entry for a local symbol. */
1330 local_got_refcounts = elf_local_got_refcounts (abfd);
1331 if (local_got_refcounts == NULL)
1333 bfd_size_type size;
1335 size = symtab_hdr->sh_info;
1336 size *= (sizeof (bfd_signed_vma)
1337 + sizeof (bfd_vma) + sizeof(char));
1338 local_got_refcounts = bfd_zalloc (abfd, size);
1339 if (local_got_refcounts == NULL)
1340 return FALSE;
1341 elf_local_got_refcounts (abfd) = local_got_refcounts;
1342 elf_i386_local_tlsdesc_gotent (abfd)
1343 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1344 elf_i386_local_got_tls_type (abfd)
1345 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1347 local_got_refcounts[r_symndx] += 1;
1348 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1351 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1352 tls_type |= old_tls_type;
1353 /* If a TLS symbol is accessed using IE at least once,
1354 there is no point to use dynamic model for it. */
1355 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1356 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1357 || (tls_type & GOT_TLS_IE) == 0))
1359 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1360 tls_type = old_tls_type;
1361 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1362 && GOT_TLS_GD_ANY_P (tls_type))
1363 tls_type |= old_tls_type;
1364 else
1366 (*_bfd_error_handler)
1367 (_("%B: `%s' accessed both as normal and "
1368 "thread local symbol"),
1369 abfd,
1370 h ? h->root.root.string : "<local>");
1371 return FALSE;
1375 if (old_tls_type != tls_type)
1377 if (h != NULL)
1378 elf_i386_hash_entry (h)->tls_type = tls_type;
1379 else
1380 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1383 /* Fall through */
1385 case R_386_GOTOFF:
1386 case R_386_GOTPC:
1387 create_got:
1388 if (htab->sgot == NULL)
1390 if (htab->elf.dynobj == NULL)
1391 htab->elf.dynobj = abfd;
1392 if (!create_got_section (htab->elf.dynobj, info))
1393 return FALSE;
1395 if (r_type != R_386_TLS_IE)
1396 break;
1397 /* Fall through */
1399 case R_386_TLS_LE_32:
1400 case R_386_TLS_LE:
1401 if (!info->shared)
1402 break;
1403 info->flags |= DF_STATIC_TLS;
1404 /* Fall through */
1406 case R_386_32:
1407 case R_386_PC32:
1408 if (h != NULL && !info->shared)
1410 /* If this reloc is in a read-only section, we might
1411 need a copy reloc. We can't check reliably at this
1412 stage whether the section is read-only, as input
1413 sections have not yet been mapped to output sections.
1414 Tentatively set the flag for now, and correct in
1415 adjust_dynamic_symbol. */
1416 h->non_got_ref = 1;
1418 /* We may need a .plt entry if the function this reloc
1419 refers to is in a shared lib. */
1420 h->plt.refcount += 1;
1421 if (r_type != R_386_PC32)
1422 h->pointer_equality_needed = 1;
1425 /* If we are creating a shared library, and this is a reloc
1426 against a global symbol, or a non PC relative reloc
1427 against a local symbol, then we need to copy the reloc
1428 into the shared library. However, if we are linking with
1429 -Bsymbolic, we do not need to copy a reloc against a
1430 global symbol which is defined in an object we are
1431 including in the link (i.e., DEF_REGULAR is set). At
1432 this point we have not seen all the input files, so it is
1433 possible that DEF_REGULAR is not set now but will be set
1434 later (it is never cleared). In case of a weak definition,
1435 DEF_REGULAR may be cleared later by a strong definition in
1436 a shared library. We account for that possibility below by
1437 storing information in the relocs_copied field of the hash
1438 table entry. A similar situation occurs when creating
1439 shared libraries and symbol visibility changes render the
1440 symbol local.
1442 If on the other hand, we are creating an executable, we
1443 may need to keep relocations for symbols satisfied by a
1444 dynamic library if we manage to avoid copy relocs for the
1445 symbol. */
1446 if ((info->shared
1447 && (sec->flags & SEC_ALLOC) != 0
1448 && (r_type != R_386_PC32
1449 || (h != NULL
1450 && (! SYMBOLIC_BIND (info, h)
1451 || h->root.type == bfd_link_hash_defweak
1452 || !h->def_regular))))
1453 || (ELIMINATE_COPY_RELOCS
1454 && !info->shared
1455 && (sec->flags & SEC_ALLOC) != 0
1456 && h != NULL
1457 && (h->root.type == bfd_link_hash_defweak
1458 || !h->def_regular)))
1460 struct elf_i386_dyn_relocs *p;
1461 struct elf_i386_dyn_relocs **head;
1463 /* We must copy these reloc types into the output file.
1464 Create a reloc section in dynobj and make room for
1465 this reloc. */
1466 if (sreloc == NULL)
1468 if (htab->elf.dynobj == NULL)
1469 htab->elf.dynobj = abfd;
1471 sreloc = _bfd_elf_make_dynamic_reloc_section
1472 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1474 if (sreloc == NULL)
1475 return FALSE;
1478 /* If this is a global symbol, we count the number of
1479 relocations we need for this symbol. */
1480 if (h != NULL)
1482 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1484 else
1486 void **vpp;
1487 /* Track dynamic relocs needed for local syms too.
1488 We really need local syms available to do this
1489 easily. Oh well. */
1491 asection *s;
1492 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1493 sec, r_symndx);
1494 if (s == NULL)
1495 return FALSE;
1497 vpp = &elf_section_data (s)->local_dynrel;
1498 head = (struct elf_i386_dyn_relocs **)vpp;
1501 p = *head;
1502 if (p == NULL || p->sec != sec)
1504 bfd_size_type amt = sizeof *p;
1505 p = bfd_alloc (htab->elf.dynobj, amt);
1506 if (p == NULL)
1507 return FALSE;
1508 p->next = *head;
1509 *head = p;
1510 p->sec = sec;
1511 p->count = 0;
1512 p->pc_count = 0;
1515 p->count += 1;
1516 if (r_type == R_386_PC32)
1517 p->pc_count += 1;
1519 break;
1521 /* This relocation describes the C++ object vtable hierarchy.
1522 Reconstruct it for later use during GC. */
1523 case R_386_GNU_VTINHERIT:
1524 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1525 return FALSE;
1526 break;
1528 /* This relocation describes which C++ vtable entries are actually
1529 used. Record for later use during GC. */
1530 case R_386_GNU_VTENTRY:
1531 BFD_ASSERT (h != NULL);
1532 if (h != NULL
1533 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1534 return FALSE;
1535 break;
1537 default:
1538 break;
1542 return TRUE;
1545 /* Return the section that should be marked against GC for a given
1546 relocation. */
1548 static asection *
1549 elf_i386_gc_mark_hook (asection *sec,
1550 struct bfd_link_info *info,
1551 Elf_Internal_Rela *rel,
1552 struct elf_link_hash_entry *h,
1553 Elf_Internal_Sym *sym)
1555 if (h != NULL)
1556 switch (ELF32_R_TYPE (rel->r_info))
1558 case R_386_GNU_VTINHERIT:
1559 case R_386_GNU_VTENTRY:
1560 return NULL;
1563 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1566 /* Update the got entry reference counts for the section being removed. */
1568 static bfd_boolean
1569 elf_i386_gc_sweep_hook (bfd *abfd,
1570 struct bfd_link_info *info,
1571 asection *sec,
1572 const Elf_Internal_Rela *relocs)
1574 Elf_Internal_Shdr *symtab_hdr;
1575 struct elf_link_hash_entry **sym_hashes;
1576 bfd_signed_vma *local_got_refcounts;
1577 const Elf_Internal_Rela *rel, *relend;
1579 if (info->relocatable)
1580 return TRUE;
1582 elf_section_data (sec)->local_dynrel = NULL;
1584 symtab_hdr = &elf_symtab_hdr (abfd);
1585 sym_hashes = elf_sym_hashes (abfd);
1586 local_got_refcounts = elf_local_got_refcounts (abfd);
1588 relend = relocs + sec->reloc_count;
1589 for (rel = relocs; rel < relend; rel++)
1591 unsigned long r_symndx;
1592 unsigned int r_type;
1593 struct elf_link_hash_entry *h = NULL;
1595 r_symndx = ELF32_R_SYM (rel->r_info);
1596 if (r_symndx >= symtab_hdr->sh_info)
1598 struct elf_i386_link_hash_entry *eh;
1599 struct elf_i386_dyn_relocs **pp;
1600 struct elf_i386_dyn_relocs *p;
1602 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1603 while (h->root.type == bfd_link_hash_indirect
1604 || h->root.type == bfd_link_hash_warning)
1605 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1606 eh = (struct elf_i386_link_hash_entry *) h;
1608 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1609 if (p->sec == sec)
1611 /* Everything must go for SEC. */
1612 *pp = p->next;
1613 break;
1617 r_type = ELF32_R_TYPE (rel->r_info);
1618 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1619 symtab_hdr, sym_hashes,
1620 &r_type, GOT_UNKNOWN,
1621 rel, relend, h))
1622 return FALSE;
1624 switch (r_type)
1626 case R_386_TLS_LDM:
1627 if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1628 elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1629 break;
1631 case R_386_TLS_GD:
1632 case R_386_TLS_GOTDESC:
1633 case R_386_TLS_DESC_CALL:
1634 case R_386_TLS_IE_32:
1635 case R_386_TLS_IE:
1636 case R_386_TLS_GOTIE:
1637 case R_386_GOT32:
1638 if (h != NULL)
1640 if (h->got.refcount > 0)
1641 h->got.refcount -= 1;
1643 else if (local_got_refcounts != NULL)
1645 if (local_got_refcounts[r_symndx] > 0)
1646 local_got_refcounts[r_symndx] -= 1;
1648 break;
1650 case R_386_32:
1651 case R_386_PC32:
1652 if (info->shared)
1653 break;
1654 /* Fall through */
1656 case R_386_PLT32:
1657 if (h != NULL)
1659 if (h->plt.refcount > 0)
1660 h->plt.refcount -= 1;
1662 break;
1664 default:
1665 break;
1669 return TRUE;
1672 /* Adjust a symbol defined by a dynamic object and referenced by a
1673 regular object. The current definition is in some section of the
1674 dynamic object, but we're not including those sections. We have to
1675 change the definition to something the rest of the link can
1676 understand. */
1678 static bfd_boolean
1679 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1680 struct elf_link_hash_entry *h)
1682 struct elf_i386_link_hash_table *htab;
1683 asection *s;
1685 /* If this is a function, put it in the procedure linkage table. We
1686 will fill in the contents of the procedure linkage table later,
1687 when we know the address of the .got section. */
1688 if (h->type == STT_FUNC
1689 || h->needs_plt)
1691 if (h->plt.refcount <= 0
1692 || SYMBOL_CALLS_LOCAL (info, h)
1693 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1694 && h->root.type == bfd_link_hash_undefweak))
1696 /* This case can occur if we saw a PLT32 reloc in an input
1697 file, but the symbol was never referred to by a dynamic
1698 object, or if all references were garbage collected. In
1699 such a case, we don't actually need to build a procedure
1700 linkage table, and we can just do a PC32 reloc instead. */
1701 h->plt.offset = (bfd_vma) -1;
1702 h->needs_plt = 0;
1705 return TRUE;
1707 else
1708 /* It's possible that we incorrectly decided a .plt reloc was
1709 needed for an R_386_PC32 reloc to a non-function sym in
1710 check_relocs. We can't decide accurately between function and
1711 non-function syms in check-relocs; Objects loaded later in
1712 the link may change h->type. So fix it now. */
1713 h->plt.offset = (bfd_vma) -1;
1715 /* If this is a weak symbol, and there is a real definition, the
1716 processor independent code will have arranged for us to see the
1717 real definition first, and we can just use the same value. */
1718 if (h->u.weakdef != NULL)
1720 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1721 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1722 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1723 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1724 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1725 h->non_got_ref = h->u.weakdef->non_got_ref;
1726 return TRUE;
1729 /* This is a reference to a symbol defined by a dynamic object which
1730 is not a function. */
1732 /* If we are creating a shared library, we must presume that the
1733 only references to the symbol are via the global offset table.
1734 For such cases we need not do anything here; the relocations will
1735 be handled correctly by relocate_section. */
1736 if (info->shared)
1737 return TRUE;
1739 /* If there are no references to this symbol that do not use the
1740 GOT, we don't need to generate a copy reloc. */
1741 if (!h->non_got_ref)
1742 return TRUE;
1744 /* If -z nocopyreloc was given, we won't generate them either. */
1745 if (info->nocopyreloc)
1747 h->non_got_ref = 0;
1748 return TRUE;
1751 htab = elf_i386_hash_table (info);
1753 /* If there aren't any dynamic relocs in read-only sections, then
1754 we can keep the dynamic relocs and avoid the copy reloc. This
1755 doesn't work on VxWorks, where we can not have dynamic relocations
1756 (other than copy and jump slot relocations) in an executable. */
1757 if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
1759 struct elf_i386_link_hash_entry * eh;
1760 struct elf_i386_dyn_relocs *p;
1762 eh = (struct elf_i386_link_hash_entry *) h;
1763 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1765 s = p->sec->output_section;
1766 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1767 break;
1770 if (p == NULL)
1772 h->non_got_ref = 0;
1773 return TRUE;
1777 if (h->size == 0)
1779 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1780 h->root.root.string);
1781 return TRUE;
1784 /* We must allocate the symbol in our .dynbss section, which will
1785 become part of the .bss section of the executable. There will be
1786 an entry for this symbol in the .dynsym section. The dynamic
1787 object will contain position independent code, so all references
1788 from the dynamic object to this symbol will go through the global
1789 offset table. The dynamic linker will use the .dynsym entry to
1790 determine the address it must put in the global offset table, so
1791 both the dynamic object and the regular object will refer to the
1792 same memory location for the variable. */
1794 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1795 copy the initial value out of the dynamic object and into the
1796 runtime process image. */
1797 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1799 htab->srelbss->size += sizeof (Elf32_External_Rel);
1800 h->needs_copy = 1;
1803 s = htab->sdynbss;
1805 return _bfd_elf_adjust_dynamic_copy (h, s);
1808 /* Allocate space in .plt, .got and associated reloc sections for
1809 dynamic relocs. */
1811 static bfd_boolean
1812 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1814 struct bfd_link_info *info;
1815 struct elf_i386_link_hash_table *htab;
1816 struct elf_i386_link_hash_entry *eh;
1817 struct elf_i386_dyn_relocs *p;
1819 if (h->root.type == bfd_link_hash_indirect)
1820 return TRUE;
1822 if (h->root.type == bfd_link_hash_warning)
1823 /* When warning symbols are created, they **replace** the "real"
1824 entry in the hash table, thus we never get to see the real
1825 symbol in a hash traversal. So look at it now. */
1826 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1828 info = (struct bfd_link_info *) inf;
1829 htab = elf_i386_hash_table (info);
1831 if (htab->elf.dynamic_sections_created
1832 && h->plt.refcount > 0)
1834 /* Make sure this symbol is output as a dynamic symbol.
1835 Undefined weak syms won't yet be marked as dynamic. */
1836 if (h->dynindx == -1
1837 && !h->forced_local)
1839 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1840 return FALSE;
1843 if (info->shared
1844 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1846 asection *s = htab->splt;
1848 /* If this is the first .plt entry, make room for the special
1849 first entry. */
1850 if (s->size == 0)
1851 s->size += PLT_ENTRY_SIZE;
1853 h->plt.offset = s->size;
1855 /* If this symbol is not defined in a regular file, and we are
1856 not generating a shared library, then set the symbol to this
1857 location in the .plt. This is required to make function
1858 pointers compare as equal between the normal executable and
1859 the shared library. */
1860 if (! info->shared
1861 && !h->def_regular)
1863 h->root.u.def.section = s;
1864 h->root.u.def.value = h->plt.offset;
1867 /* Make room for this entry. */
1868 s->size += PLT_ENTRY_SIZE;
1870 /* We also need to make an entry in the .got.plt section, which
1871 will be placed in the .got section by the linker script. */
1872 htab->sgotplt->size += 4;
1874 /* We also need to make an entry in the .rel.plt section. */
1875 htab->srelplt->size += sizeof (Elf32_External_Rel);
1876 htab->next_tls_desc_index++;
1878 if (htab->is_vxworks && !info->shared)
1880 /* VxWorks has a second set of relocations for each PLT entry
1881 in executables. They go in a separate relocation section,
1882 which is processed by the kernel loader. */
1884 /* There are two relocations for the initial PLT entry: an
1885 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
1886 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
1888 if (h->plt.offset == PLT_ENTRY_SIZE)
1889 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1891 /* There are two extra relocations for each subsequent PLT entry:
1892 an R_386_32 relocation for the GOT entry, and an R_386_32
1893 relocation for the PLT entry. */
1895 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1898 else
1900 h->plt.offset = (bfd_vma) -1;
1901 h->needs_plt = 0;
1904 else
1906 h->plt.offset = (bfd_vma) -1;
1907 h->needs_plt = 0;
1910 eh = (struct elf_i386_link_hash_entry *) h;
1911 eh->tlsdesc_got = (bfd_vma) -1;
1913 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
1914 make it a R_386_TLS_LE_32 requiring no TLS entry. */
1915 if (h->got.refcount > 0
1916 && !info->shared
1917 && h->dynindx == -1
1918 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
1919 h->got.offset = (bfd_vma) -1;
1920 else if (h->got.refcount > 0)
1922 asection *s;
1923 bfd_boolean dyn;
1924 int tls_type = elf_i386_hash_entry(h)->tls_type;
1926 /* Make sure this symbol is output as a dynamic symbol.
1927 Undefined weak syms won't yet be marked as dynamic. */
1928 if (h->dynindx == -1
1929 && !h->forced_local)
1931 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1932 return FALSE;
1935 s = htab->sgot;
1936 if (GOT_TLS_GDESC_P (tls_type))
1938 eh->tlsdesc_got = htab->sgotplt->size
1939 - elf_i386_compute_jump_table_size (htab);
1940 htab->sgotplt->size += 8;
1941 h->got.offset = (bfd_vma) -2;
1943 if (! GOT_TLS_GDESC_P (tls_type)
1944 || GOT_TLS_GD_P (tls_type))
1946 h->got.offset = s->size;
1947 s->size += 4;
1948 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
1949 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
1950 s->size += 4;
1952 dyn = htab->elf.dynamic_sections_created;
1953 /* R_386_TLS_IE_32 needs one dynamic relocation,
1954 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1955 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1956 need two), R_386_TLS_GD needs one if local symbol and two if
1957 global. */
1958 if (tls_type == GOT_TLS_IE_BOTH)
1959 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1960 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1961 || (tls_type & GOT_TLS_IE))
1962 htab->srelgot->size += sizeof (Elf32_External_Rel);
1963 else if (GOT_TLS_GD_P (tls_type))
1964 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1965 else if (! GOT_TLS_GDESC_P (tls_type)
1966 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1967 || h->root.type != bfd_link_hash_undefweak)
1968 && (info->shared
1969 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1970 htab->srelgot->size += sizeof (Elf32_External_Rel);
1971 if (GOT_TLS_GDESC_P (tls_type))
1972 htab->srelplt->size += sizeof (Elf32_External_Rel);
1974 else
1975 h->got.offset = (bfd_vma) -1;
1977 if (eh->dyn_relocs == NULL)
1978 return TRUE;
1980 /* In the shared -Bsymbolic case, discard space allocated for
1981 dynamic pc-relative relocs against symbols which turn out to be
1982 defined in regular objects. For the normal shared case, discard
1983 space for pc-relative relocs that have become local due to symbol
1984 visibility changes. */
1986 if (info->shared)
1988 /* The only reloc that uses pc_count is R_386_PC32, which will
1989 appear on a call or on something like ".long foo - .". We
1990 want calls to protected symbols to resolve directly to the
1991 function rather than going via the plt. If people want
1992 function pointer comparisons to work as expected then they
1993 should avoid writing assembly like ".long foo - .". */
1994 if (SYMBOL_CALLS_LOCAL (info, h))
1996 struct elf_i386_dyn_relocs **pp;
1998 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2000 p->count -= p->pc_count;
2001 p->pc_count = 0;
2002 if (p->count == 0)
2003 *pp = p->next;
2004 else
2005 pp = &p->next;
2009 if (htab->is_vxworks)
2011 struct elf_i386_dyn_relocs **pp;
2012 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2014 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2015 *pp = p->next;
2016 else
2017 pp = &p->next;
2021 /* Also discard relocs on undefined weak syms with non-default
2022 visibility. */
2023 if (eh->dyn_relocs != NULL
2024 && h->root.type == bfd_link_hash_undefweak)
2026 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2027 eh->dyn_relocs = NULL;
2029 /* Make sure undefined weak symbols are output as a dynamic
2030 symbol in PIEs. */
2031 else if (h->dynindx == -1
2032 && !h->forced_local)
2034 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2035 return FALSE;
2039 else if (ELIMINATE_COPY_RELOCS)
2041 /* For the non-shared case, discard space for relocs against
2042 symbols which turn out to need copy relocs or are not
2043 dynamic. */
2045 if (!h->non_got_ref
2046 && ((h->def_dynamic
2047 && !h->def_regular)
2048 || (htab->elf.dynamic_sections_created
2049 && (h->root.type == bfd_link_hash_undefweak
2050 || h->root.type == bfd_link_hash_undefined))))
2052 /* Make sure this symbol is output as a dynamic symbol.
2053 Undefined weak syms won't yet be marked as dynamic. */
2054 if (h->dynindx == -1
2055 && !h->forced_local)
2057 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2058 return FALSE;
2061 /* If that succeeded, we know we'll be keeping all the
2062 relocs. */
2063 if (h->dynindx != -1)
2064 goto keep;
2067 eh->dyn_relocs = NULL;
2069 keep: ;
2072 /* Finally, allocate space. */
2073 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2075 asection *sreloc;
2077 sreloc = elf_section_data (p->sec)->sreloc;
2079 BFD_ASSERT (sreloc != NULL);
2080 sreloc->size += p->count * sizeof (Elf32_External_Rel);
2083 return TRUE;
2086 /* Find any dynamic relocs that apply to read-only sections. */
2088 static bfd_boolean
2089 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2091 struct elf_i386_link_hash_entry *eh;
2092 struct elf_i386_dyn_relocs *p;
2094 if (h->root.type == bfd_link_hash_warning)
2095 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2097 eh = (struct elf_i386_link_hash_entry *) h;
2098 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2100 asection *s = p->sec->output_section;
2102 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2104 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2106 info->flags |= DF_TEXTREL;
2108 /* Not an error, just cut short the traversal. */
2109 return FALSE;
2112 return TRUE;
2115 /* Set the sizes of the dynamic sections. */
2117 static bfd_boolean
2118 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2119 struct bfd_link_info *info)
2121 struct elf_i386_link_hash_table *htab;
2122 bfd *dynobj;
2123 asection *s;
2124 bfd_boolean relocs;
2125 bfd *ibfd;
2127 htab = elf_i386_hash_table (info);
2128 dynobj = htab->elf.dynobj;
2129 if (dynobj == NULL)
2130 abort ();
2132 if (htab->elf.dynamic_sections_created)
2134 /* Set the contents of the .interp section to the interpreter. */
2135 if (info->executable)
2137 s = bfd_get_section_by_name (dynobj, ".interp");
2138 if (s == NULL)
2139 abort ();
2140 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2141 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2145 /* Set up .got offsets for local syms, and space for local dynamic
2146 relocs. */
2147 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2149 bfd_signed_vma *local_got;
2150 bfd_signed_vma *end_local_got;
2151 char *local_tls_type;
2152 bfd_vma *local_tlsdesc_gotent;
2153 bfd_size_type locsymcount;
2154 Elf_Internal_Shdr *symtab_hdr;
2155 asection *srel;
2157 if (! is_i386_elf (ibfd))
2158 continue;
2160 for (s = ibfd->sections; s != NULL; s = s->next)
2162 struct elf_i386_dyn_relocs *p;
2164 for (p = ((struct elf_i386_dyn_relocs *)
2165 elf_section_data (s)->local_dynrel);
2166 p != NULL;
2167 p = p->next)
2169 if (!bfd_is_abs_section (p->sec)
2170 && bfd_is_abs_section (p->sec->output_section))
2172 /* Input section has been discarded, either because
2173 it is a copy of a linkonce section or due to
2174 linker script /DISCARD/, so we'll be discarding
2175 the relocs too. */
2177 else if (htab->is_vxworks
2178 && strcmp (p->sec->output_section->name,
2179 ".tls_vars") == 0)
2181 /* Relocations in vxworks .tls_vars sections are
2182 handled specially by the loader. */
2184 else if (p->count != 0)
2186 srel = elf_section_data (p->sec)->sreloc;
2187 srel->size += p->count * sizeof (Elf32_External_Rel);
2188 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2189 info->flags |= DF_TEXTREL;
2194 local_got = elf_local_got_refcounts (ibfd);
2195 if (!local_got)
2196 continue;
2198 symtab_hdr = &elf_symtab_hdr (ibfd);
2199 locsymcount = symtab_hdr->sh_info;
2200 end_local_got = local_got + locsymcount;
2201 local_tls_type = elf_i386_local_got_tls_type (ibfd);
2202 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
2203 s = htab->sgot;
2204 srel = htab->srelgot;
2205 for (; local_got < end_local_got;
2206 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2208 *local_tlsdesc_gotent = (bfd_vma) -1;
2209 if (*local_got > 0)
2211 if (GOT_TLS_GDESC_P (*local_tls_type))
2213 *local_tlsdesc_gotent = htab->sgotplt->size
2214 - elf_i386_compute_jump_table_size (htab);
2215 htab->sgotplt->size += 8;
2216 *local_got = (bfd_vma) -2;
2218 if (! GOT_TLS_GDESC_P (*local_tls_type)
2219 || GOT_TLS_GD_P (*local_tls_type))
2221 *local_got = s->size;
2222 s->size += 4;
2223 if (GOT_TLS_GD_P (*local_tls_type)
2224 || *local_tls_type == GOT_TLS_IE_BOTH)
2225 s->size += 4;
2227 if (info->shared
2228 || GOT_TLS_GD_ANY_P (*local_tls_type)
2229 || (*local_tls_type & GOT_TLS_IE))
2231 if (*local_tls_type == GOT_TLS_IE_BOTH)
2232 srel->size += 2 * sizeof (Elf32_External_Rel);
2233 else if (GOT_TLS_GD_P (*local_tls_type)
2234 || ! GOT_TLS_GDESC_P (*local_tls_type))
2235 srel->size += sizeof (Elf32_External_Rel);
2236 if (GOT_TLS_GDESC_P (*local_tls_type))
2237 htab->srelplt->size += sizeof (Elf32_External_Rel);
2240 else
2241 *local_got = (bfd_vma) -1;
2245 if (htab->tls_ldm_got.refcount > 0)
2247 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2248 relocs. */
2249 htab->tls_ldm_got.offset = htab->sgot->size;
2250 htab->sgot->size += 8;
2251 htab->srelgot->size += sizeof (Elf32_External_Rel);
2253 else
2254 htab->tls_ldm_got.offset = -1;
2256 /* Allocate global sym .plt and .got entries, and space for global
2257 sym dynamic relocs. */
2258 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2260 /* For every jump slot reserved in the sgotplt, reloc_count is
2261 incremented. However, when we reserve space for TLS descriptors,
2262 it's not incremented, so in order to compute the space reserved
2263 for them, it suffices to multiply the reloc count by the jump
2264 slot size. */
2265 if (htab->srelplt)
2266 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
2268 /* We now have determined the sizes of the various dynamic sections.
2269 Allocate memory for them. */
2270 relocs = FALSE;
2271 for (s = dynobj->sections; s != NULL; s = s->next)
2273 bfd_boolean strip_section = TRUE;
2275 if ((s->flags & SEC_LINKER_CREATED) == 0)
2276 continue;
2278 if (s == htab->splt
2279 || s == htab->sgot
2280 || s == htab->sgotplt
2281 || s == htab->sdynbss)
2283 /* Strip this section if we don't need it; see the
2284 comment below. */
2285 /* We'd like to strip these sections if they aren't needed, but if
2286 we've exported dynamic symbols from them we must leave them.
2287 It's too late to tell BFD to get rid of the symbols. */
2289 if (htab->elf.hplt != NULL)
2290 strip_section = FALSE;
2292 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
2294 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
2295 relocs = TRUE;
2297 /* We use the reloc_count field as a counter if we need
2298 to copy relocs into the output file. */
2299 s->reloc_count = 0;
2301 else
2303 /* It's not one of our sections, so don't allocate space. */
2304 continue;
2307 if (s->size == 0)
2309 /* If we don't need this section, strip it from the
2310 output file. This is mostly to handle .rel.bss and
2311 .rel.plt. We must create both sections in
2312 create_dynamic_sections, because they must be created
2313 before the linker maps input sections to output
2314 sections. The linker does that before
2315 adjust_dynamic_symbol is called, and it is that
2316 function which decides whether anything needs to go
2317 into these sections. */
2318 if (strip_section)
2319 s->flags |= SEC_EXCLUDE;
2320 continue;
2323 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2324 continue;
2326 /* Allocate memory for the section contents. We use bfd_zalloc
2327 here in case unused entries are not reclaimed before the
2328 section's contents are written out. This should not happen,
2329 but this way if it does, we get a R_386_NONE reloc instead
2330 of garbage. */
2331 s->contents = bfd_zalloc (dynobj, s->size);
2332 if (s->contents == NULL)
2333 return FALSE;
2336 if (htab->elf.dynamic_sections_created)
2338 /* Add some entries to the .dynamic section. We fill in the
2339 values later, in elf_i386_finish_dynamic_sections, but we
2340 must add the entries now so that we get the correct size for
2341 the .dynamic section. The DT_DEBUG entry is filled in by the
2342 dynamic linker and used by the debugger. */
2343 #define add_dynamic_entry(TAG, VAL) \
2344 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2346 if (info->executable)
2348 if (!add_dynamic_entry (DT_DEBUG, 0))
2349 return FALSE;
2352 if (htab->splt->size != 0)
2354 if (!add_dynamic_entry (DT_PLTGOT, 0)
2355 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2356 || !add_dynamic_entry (DT_PLTREL, DT_REL)
2357 || !add_dynamic_entry (DT_JMPREL, 0))
2358 return FALSE;
2361 if (relocs)
2363 if (!add_dynamic_entry (DT_REL, 0)
2364 || !add_dynamic_entry (DT_RELSZ, 0)
2365 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2366 return FALSE;
2368 /* If any dynamic relocs apply to a read-only section,
2369 then we need a DT_TEXTREL entry. */
2370 if ((info->flags & DF_TEXTREL) == 0)
2371 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
2373 if ((info->flags & DF_TEXTREL) != 0)
2375 if (!add_dynamic_entry (DT_TEXTREL, 0))
2376 return FALSE;
2379 if (htab->is_vxworks
2380 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
2381 return FALSE;
2383 #undef add_dynamic_entry
2385 return TRUE;
2388 static bfd_boolean
2389 elf_i386_always_size_sections (bfd *output_bfd,
2390 struct bfd_link_info *info)
2392 asection *tls_sec = elf_hash_table (info)->tls_sec;
2394 if (tls_sec)
2396 struct elf_link_hash_entry *tlsbase;
2398 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2399 "_TLS_MODULE_BASE_",
2400 FALSE, FALSE, FALSE);
2402 if (tlsbase && tlsbase->type == STT_TLS)
2404 struct bfd_link_hash_entry *bh = NULL;
2405 const struct elf_backend_data *bed
2406 = get_elf_backend_data (output_bfd);
2408 if (!(_bfd_generic_link_add_one_symbol
2409 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2410 tls_sec, 0, NULL, FALSE,
2411 bed->collect, &bh)))
2412 return FALSE;
2414 elf_i386_hash_table (info)->tls_module_base = bh;
2416 tlsbase = (struct elf_link_hash_entry *)bh;
2417 tlsbase->def_regular = 1;
2418 tlsbase->other = STV_HIDDEN;
2419 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2423 return TRUE;
2426 /* Set the correct type for an x86 ELF section. We do this by the
2427 section name, which is a hack, but ought to work. */
2429 static bfd_boolean
2430 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2431 Elf_Internal_Shdr *hdr,
2432 asection *sec)
2434 register const char *name;
2436 name = bfd_get_section_name (abfd, sec);
2438 /* This is an ugly, but unfortunately necessary hack that is
2439 needed when producing EFI binaries on x86. It tells
2440 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2441 containing ELF relocation info. We need this hack in order to
2442 be able to generate ELF binaries that can be translated into
2443 EFI applications (which are essentially COFF objects). Those
2444 files contain a COFF ".reloc" section inside an ELFNN object,
2445 which would normally cause BFD to segfault because it would
2446 attempt to interpret this section as containing relocation
2447 entries for section "oc". With this hack enabled, ".reloc"
2448 will be treated as a normal data section, which will avoid the
2449 segfault. However, you won't be able to create an ELFNN binary
2450 with a section named "oc" that needs relocations, but that's
2451 the kind of ugly side-effects you get when detecting section
2452 types based on their names... In practice, this limitation is
2453 unlikely to bite. */
2454 if (strcmp (name, ".reloc") == 0)
2455 hdr->sh_type = SHT_PROGBITS;
2457 return TRUE;
2460 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2461 executables. Rather than setting it to the beginning of the TLS
2462 section, we have to set it to the end. This function may be called
2463 multiple times, it is idempotent. */
2465 static void
2466 set_tls_module_base (struct bfd_link_info *info)
2468 struct bfd_link_hash_entry *base;
2470 if (!info->executable)
2471 return;
2473 base = elf_i386_hash_table (info)->tls_module_base;
2475 if (!base)
2476 return;
2478 base->u.def.value = elf_hash_table (info)->tls_size;
2481 /* Return the base VMA address which should be subtracted from real addresses
2482 when resolving @dtpoff relocation.
2483 This is PT_TLS segment p_vaddr. */
2485 static bfd_vma
2486 dtpoff_base (struct bfd_link_info *info)
2488 /* If tls_sec is NULL, we should have signalled an error already. */
2489 if (elf_hash_table (info)->tls_sec == NULL)
2490 return 0;
2491 return elf_hash_table (info)->tls_sec->vma;
2494 /* Return the relocation value for @tpoff relocation
2495 if STT_TLS virtual address is ADDRESS. */
2497 static bfd_vma
2498 tpoff (struct bfd_link_info *info, bfd_vma address)
2500 struct elf_link_hash_table *htab = elf_hash_table (info);
2502 /* If tls_sec is NULL, we should have signalled an error already. */
2503 if (htab->tls_sec == NULL)
2504 return 0;
2505 return htab->tls_size + htab->tls_sec->vma - address;
2508 /* Relocate an i386 ELF section. */
2510 static bfd_boolean
2511 elf_i386_relocate_section (bfd *output_bfd,
2512 struct bfd_link_info *info,
2513 bfd *input_bfd,
2514 asection *input_section,
2515 bfd_byte *contents,
2516 Elf_Internal_Rela *relocs,
2517 Elf_Internal_Sym *local_syms,
2518 asection **local_sections)
2520 struct elf_i386_link_hash_table *htab;
2521 Elf_Internal_Shdr *symtab_hdr;
2522 struct elf_link_hash_entry **sym_hashes;
2523 bfd_vma *local_got_offsets;
2524 bfd_vma *local_tlsdesc_gotents;
2525 Elf_Internal_Rela *rel;
2526 Elf_Internal_Rela *relend;
2527 bfd_boolean is_vxworks_tls;
2529 BFD_ASSERT (is_i386_elf (input_bfd));
2531 htab = elf_i386_hash_table (info);
2532 symtab_hdr = &elf_symtab_hdr (input_bfd);
2533 sym_hashes = elf_sym_hashes (input_bfd);
2534 local_got_offsets = elf_local_got_offsets (input_bfd);
2535 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
2536 /* We have to handle relocations in vxworks .tls_vars sections
2537 specially, because the dynamic loader is 'weird'. */
2538 is_vxworks_tls = (htab->is_vxworks && info->shared
2539 && !strcmp (input_section->output_section->name,
2540 ".tls_vars"));
2542 set_tls_module_base (info);
2544 rel = relocs;
2545 relend = relocs + input_section->reloc_count;
2546 for (; rel < relend; rel++)
2548 unsigned int r_type;
2549 reloc_howto_type *howto;
2550 unsigned long r_symndx;
2551 struct elf_link_hash_entry *h;
2552 Elf_Internal_Sym *sym;
2553 asection *sec;
2554 bfd_vma off, offplt;
2555 bfd_vma relocation;
2556 bfd_boolean unresolved_reloc;
2557 bfd_reloc_status_type r;
2558 unsigned int indx;
2559 int tls_type;
2561 r_type = ELF32_R_TYPE (rel->r_info);
2562 if (r_type == R_386_GNU_VTINHERIT
2563 || r_type == R_386_GNU_VTENTRY)
2564 continue;
2566 if ((indx = r_type) >= R_386_standard
2567 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2568 >= R_386_ext - R_386_standard)
2569 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2570 >= R_386_tls - R_386_ext))
2572 (*_bfd_error_handler)
2573 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2574 input_bfd, input_section, r_type);
2575 bfd_set_error (bfd_error_bad_value);
2576 return FALSE;
2578 howto = elf_howto_table + indx;
2580 r_symndx = ELF32_R_SYM (rel->r_info);
2581 h = NULL;
2582 sym = NULL;
2583 sec = NULL;
2584 unresolved_reloc = FALSE;
2585 if (r_symndx < symtab_hdr->sh_info)
2587 sym = local_syms + r_symndx;
2588 sec = local_sections[r_symndx];
2589 relocation = (sec->output_section->vma
2590 + sec->output_offset
2591 + sym->st_value);
2593 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2594 && ((sec->flags & SEC_MERGE) != 0
2595 || (info->relocatable
2596 && sec->output_offset != 0)))
2598 bfd_vma addend;
2599 bfd_byte *where = contents + rel->r_offset;
2601 switch (howto->size)
2603 case 0:
2604 addend = bfd_get_8 (input_bfd, where);
2605 if (howto->pc_relative)
2607 addend = (addend ^ 0x80) - 0x80;
2608 addend += 1;
2610 break;
2611 case 1:
2612 addend = bfd_get_16 (input_bfd, where);
2613 if (howto->pc_relative)
2615 addend = (addend ^ 0x8000) - 0x8000;
2616 addend += 2;
2618 break;
2619 case 2:
2620 addend = bfd_get_32 (input_bfd, where);
2621 if (howto->pc_relative)
2623 addend = (addend ^ 0x80000000) - 0x80000000;
2624 addend += 4;
2626 break;
2627 default:
2628 abort ();
2631 if (info->relocatable)
2632 addend += sec->output_offset;
2633 else
2635 asection *msec = sec;
2636 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2637 addend);
2638 addend -= relocation;
2639 addend += msec->output_section->vma + msec->output_offset;
2642 switch (howto->size)
2644 case 0:
2645 /* FIXME: overflow checks. */
2646 if (howto->pc_relative)
2647 addend -= 1;
2648 bfd_put_8 (input_bfd, addend, where);
2649 break;
2650 case 1:
2651 if (howto->pc_relative)
2652 addend -= 2;
2653 bfd_put_16 (input_bfd, addend, where);
2654 break;
2655 case 2:
2656 if (howto->pc_relative)
2657 addend -= 4;
2658 bfd_put_32 (input_bfd, addend, where);
2659 break;
2663 else
2665 bfd_boolean warned;
2667 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2668 r_symndx, symtab_hdr, sym_hashes,
2669 h, sec, relocation,
2670 unresolved_reloc, warned);
2673 if (sec != NULL && elf_discarded_section (sec))
2675 /* For relocs against symbols from removed linkonce sections,
2676 or sections discarded by a linker script, we just want the
2677 section contents zeroed. Avoid any special processing. */
2678 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2679 rel->r_info = 0;
2680 rel->r_addend = 0;
2681 continue;
2684 if (info->relocatable)
2685 continue;
2687 switch (r_type)
2689 case R_386_GOT32:
2690 /* Relocation is to the entry for this symbol in the global
2691 offset table. */
2692 if (htab->sgot == NULL)
2693 abort ();
2695 if (h != NULL)
2697 bfd_boolean dyn;
2699 off = h->got.offset;
2700 dyn = htab->elf.dynamic_sections_created;
2701 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2702 || (info->shared
2703 && SYMBOL_REFERENCES_LOCAL (info, h))
2704 || (ELF_ST_VISIBILITY (h->other)
2705 && h->root.type == bfd_link_hash_undefweak))
2707 /* This is actually a static link, or it is a
2708 -Bsymbolic link and the symbol is defined
2709 locally, or the symbol was forced to be local
2710 because of a version file. We must initialize
2711 this entry in the global offset table. Since the
2712 offset must always be a multiple of 4, we use the
2713 least significant bit to record whether we have
2714 initialized it already.
2716 When doing a dynamic link, we create a .rel.got
2717 relocation entry to initialize the value. This
2718 is done in the finish_dynamic_symbol routine. */
2719 if ((off & 1) != 0)
2720 off &= ~1;
2721 else
2723 bfd_put_32 (output_bfd, relocation,
2724 htab->sgot->contents + off);
2725 h->got.offset |= 1;
2728 else
2729 unresolved_reloc = FALSE;
2731 else
2733 if (local_got_offsets == NULL)
2734 abort ();
2736 off = local_got_offsets[r_symndx];
2738 /* The offset must always be a multiple of 4. We use
2739 the least significant bit to record whether we have
2740 already generated the necessary reloc. */
2741 if ((off & 1) != 0)
2742 off &= ~1;
2743 else
2745 bfd_put_32 (output_bfd, relocation,
2746 htab->sgot->contents + off);
2748 if (info->shared)
2750 asection *s;
2751 Elf_Internal_Rela outrel;
2752 bfd_byte *loc;
2754 s = htab->srelgot;
2755 if (s == NULL)
2756 abort ();
2758 outrel.r_offset = (htab->sgot->output_section->vma
2759 + htab->sgot->output_offset
2760 + off);
2761 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2762 loc = s->contents;
2763 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
2764 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2767 local_got_offsets[r_symndx] |= 1;
2771 if (off >= (bfd_vma) -2)
2772 abort ();
2774 relocation = htab->sgot->output_section->vma
2775 + htab->sgot->output_offset + off
2776 - htab->sgotplt->output_section->vma
2777 - htab->sgotplt->output_offset;
2778 break;
2780 case R_386_GOTOFF:
2781 /* Relocation is relative to the start of the global offset
2782 table. */
2784 /* Check to make sure it isn't a protected function symbol
2785 for shared library since it may not be local when used
2786 as function address. We also need to make sure that a
2787 symbol is defined locally. */
2788 if (info->shared && h)
2790 if (!h->def_regular)
2792 const char *v;
2794 switch (ELF_ST_VISIBILITY (h->other))
2796 case STV_HIDDEN:
2797 v = _("hidden symbol");
2798 break;
2799 case STV_INTERNAL:
2800 v = _("internal symbol");
2801 break;
2802 case STV_PROTECTED:
2803 v = _("protected symbol");
2804 break;
2805 default:
2806 v = _("symbol");
2807 break;
2810 (*_bfd_error_handler)
2811 (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
2812 input_bfd, v, h->root.root.string);
2813 bfd_set_error (bfd_error_bad_value);
2814 return FALSE;
2816 else if (!info->executable
2817 && h->type == STT_FUNC
2818 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2820 (*_bfd_error_handler)
2821 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
2822 input_bfd, h->root.root.string);
2823 bfd_set_error (bfd_error_bad_value);
2824 return FALSE;
2828 /* Note that sgot is not involved in this
2829 calculation. We always want the start of .got.plt. If we
2830 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2831 permitted by the ABI, we might have to change this
2832 calculation. */
2833 relocation -= htab->sgotplt->output_section->vma
2834 + htab->sgotplt->output_offset;
2835 break;
2837 case R_386_GOTPC:
2838 /* Use global offset table as symbol value. */
2839 relocation = htab->sgotplt->output_section->vma
2840 + htab->sgotplt->output_offset;
2841 unresolved_reloc = FALSE;
2842 break;
2844 case R_386_PLT32:
2845 /* Relocation is to the entry for this symbol in the
2846 procedure linkage table. */
2848 /* Resolve a PLT32 reloc against a local symbol directly,
2849 without using the procedure linkage table. */
2850 if (h == NULL)
2851 break;
2853 if (h->plt.offset == (bfd_vma) -1
2854 || htab->splt == NULL)
2856 /* We didn't make a PLT entry for this symbol. This
2857 happens when statically linking PIC code, or when
2858 using -Bsymbolic. */
2859 break;
2862 relocation = (htab->splt->output_section->vma
2863 + htab->splt->output_offset
2864 + h->plt.offset);
2865 unresolved_reloc = FALSE;
2866 break;
2868 case R_386_32:
2869 case R_386_PC32:
2870 if ((input_section->flags & SEC_ALLOC) == 0
2871 || is_vxworks_tls)
2872 break;
2874 if ((info->shared
2875 && (h == NULL
2876 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2877 || h->root.type != bfd_link_hash_undefweak)
2878 && (r_type != R_386_PC32
2879 || !SYMBOL_CALLS_LOCAL (info, h)))
2880 || (ELIMINATE_COPY_RELOCS
2881 && !info->shared
2882 && h != NULL
2883 && h->dynindx != -1
2884 && !h->non_got_ref
2885 && ((h->def_dynamic
2886 && !h->def_regular)
2887 || h->root.type == bfd_link_hash_undefweak
2888 || h->root.type == bfd_link_hash_undefined)))
2890 Elf_Internal_Rela outrel;
2891 bfd_byte *loc;
2892 bfd_boolean skip, relocate;
2893 asection *sreloc;
2895 /* When generating a shared object, these relocations
2896 are copied into the output file to be resolved at run
2897 time. */
2899 skip = FALSE;
2900 relocate = FALSE;
2902 outrel.r_offset =
2903 _bfd_elf_section_offset (output_bfd, info, input_section,
2904 rel->r_offset);
2905 if (outrel.r_offset == (bfd_vma) -1)
2906 skip = TRUE;
2907 else if (outrel.r_offset == (bfd_vma) -2)
2908 skip = TRUE, relocate = TRUE;
2909 outrel.r_offset += (input_section->output_section->vma
2910 + input_section->output_offset);
2912 if (skip)
2913 memset (&outrel, 0, sizeof outrel);
2914 else if (h != NULL
2915 && h->dynindx != -1
2916 && (r_type == R_386_PC32
2917 || !info->shared
2918 || !SYMBOLIC_BIND (info, h)
2919 || !h->def_regular))
2920 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2921 else
2923 /* This symbol is local, or marked to become local. */
2924 relocate = TRUE;
2925 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2928 sreloc = elf_section_data (input_section)->sreloc;
2930 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
2932 loc = sreloc->contents;
2933 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2935 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2937 /* If this reloc is against an external symbol, we do
2938 not want to fiddle with the addend. Otherwise, we
2939 need to include the symbol value so that it becomes
2940 an addend for the dynamic reloc. */
2941 if (! relocate)
2942 continue;
2944 break;
2946 case R_386_TLS_IE:
2947 if (info->shared)
2949 Elf_Internal_Rela outrel;
2950 bfd_byte *loc;
2951 asection *sreloc;
2953 outrel.r_offset = rel->r_offset
2954 + input_section->output_section->vma
2955 + input_section->output_offset;
2956 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2957 sreloc = elf_section_data (input_section)->sreloc;
2958 if (sreloc == NULL)
2959 abort ();
2960 loc = sreloc->contents;
2961 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2962 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2964 /* Fall through */
2966 case R_386_TLS_GD:
2967 case R_386_TLS_GOTDESC:
2968 case R_386_TLS_DESC_CALL:
2969 case R_386_TLS_IE_32:
2970 case R_386_TLS_GOTIE:
2971 tls_type = GOT_UNKNOWN;
2972 if (h == NULL && local_got_offsets)
2973 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2974 else if (h != NULL)
2975 tls_type = elf_i386_hash_entry(h)->tls_type;
2976 if (tls_type == GOT_TLS_IE)
2977 tls_type = GOT_TLS_IE_NEG;
2979 if (! elf_i386_tls_transition (info, input_bfd,
2980 input_section, contents,
2981 symtab_hdr, sym_hashes,
2982 &r_type, tls_type, rel,
2983 relend, h))
2984 return FALSE;
2986 if (r_type == R_386_TLS_LE_32)
2988 BFD_ASSERT (! unresolved_reloc);
2989 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
2991 unsigned int type;
2992 bfd_vma roff;
2994 /* GD->LE transition. */
2995 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2996 if (type == 0x04)
2998 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2999 Change it into:
3000 movl %gs:0, %eax; subl $foo@tpoff, %eax
3001 (6 byte form of subl). */
3002 memcpy (contents + rel->r_offset - 3,
3003 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3004 roff = rel->r_offset + 5;
3006 else
3008 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3009 Change it into:
3010 movl %gs:0, %eax; subl $foo@tpoff, %eax
3011 (6 byte form of subl). */
3012 memcpy (contents + rel->r_offset - 2,
3013 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3014 roff = rel->r_offset + 6;
3016 bfd_put_32 (output_bfd, tpoff (info, relocation),
3017 contents + roff);
3018 /* Skip R_386_PC32/R_386_PLT32. */
3019 rel++;
3020 continue;
3022 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3024 /* GDesc -> LE transition.
3025 It's originally something like:
3026 leal x@tlsdesc(%ebx), %eax
3028 leal x@ntpoff, %eax
3030 Registers other than %eax may be set up here. */
3032 unsigned int val;
3033 bfd_vma roff;
3035 roff = rel->r_offset;
3036 val = bfd_get_8 (input_bfd, contents + roff - 1);
3038 /* Now modify the instruction as appropriate. */
3039 /* aoliva FIXME: remove the above and xor the byte
3040 below with 0x86. */
3041 bfd_put_8 (output_bfd, val ^ 0x86,
3042 contents + roff - 1);
3043 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3044 contents + roff);
3045 continue;
3047 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3049 /* GDesc -> LE transition.
3050 It's originally:
3051 call *(%eax)
3052 Turn it into:
3053 xchg %ax,%ax */
3055 bfd_vma roff;
3057 roff = rel->r_offset;
3058 bfd_put_8 (output_bfd, 0x66, contents + roff);
3059 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3060 continue;
3062 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
3064 unsigned int val;
3066 /* IE->LE transition:
3067 Originally it can be one of:
3068 movl foo, %eax
3069 movl foo, %reg
3070 addl foo, %reg
3071 We change it into:
3072 movl $foo, %eax
3073 movl $foo, %reg
3074 addl $foo, %reg. */
3075 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3076 if (val == 0xa1)
3078 /* movl foo, %eax. */
3079 bfd_put_8 (output_bfd, 0xb8,
3080 contents + rel->r_offset - 1);
3082 else
3084 unsigned int type;
3086 type = bfd_get_8 (input_bfd,
3087 contents + rel->r_offset - 2);
3088 switch (type)
3090 case 0x8b:
3091 /* movl */
3092 bfd_put_8 (output_bfd, 0xc7,
3093 contents + rel->r_offset - 2);
3094 bfd_put_8 (output_bfd,
3095 0xc0 | ((val >> 3) & 7),
3096 contents + rel->r_offset - 1);
3097 break;
3098 case 0x03:
3099 /* addl */
3100 bfd_put_8 (output_bfd, 0x81,
3101 contents + rel->r_offset - 2);
3102 bfd_put_8 (output_bfd,
3103 0xc0 | ((val >> 3) & 7),
3104 contents + rel->r_offset - 1);
3105 break;
3106 default:
3107 BFD_FAIL ();
3108 break;
3111 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3112 contents + rel->r_offset);
3113 continue;
3115 else
3117 unsigned int val, type;
3119 /* {IE_32,GOTIE}->LE transition:
3120 Originally it can be one of:
3121 subl foo(%reg1), %reg2
3122 movl foo(%reg1), %reg2
3123 addl foo(%reg1), %reg2
3124 We change it into:
3125 subl $foo, %reg2
3126 movl $foo, %reg2 (6 byte form)
3127 addl $foo, %reg2. */
3128 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3129 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3130 if (type == 0x8b)
3132 /* movl */
3133 bfd_put_8 (output_bfd, 0xc7,
3134 contents + rel->r_offset - 2);
3135 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3136 contents + rel->r_offset - 1);
3138 else if (type == 0x2b)
3140 /* subl */
3141 bfd_put_8 (output_bfd, 0x81,
3142 contents + rel->r_offset - 2);
3143 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3144 contents + rel->r_offset - 1);
3146 else if (type == 0x03)
3148 /* addl */
3149 bfd_put_8 (output_bfd, 0x81,
3150 contents + rel->r_offset - 2);
3151 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3152 contents + rel->r_offset - 1);
3154 else
3155 BFD_FAIL ();
3156 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
3157 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3158 contents + rel->r_offset);
3159 else
3160 bfd_put_32 (output_bfd, tpoff (info, relocation),
3161 contents + rel->r_offset);
3162 continue;
3166 if (htab->sgot == NULL)
3167 abort ();
3169 if (h != NULL)
3171 off = h->got.offset;
3172 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
3174 else
3176 if (local_got_offsets == NULL)
3177 abort ();
3179 off = local_got_offsets[r_symndx];
3180 offplt = local_tlsdesc_gotents[r_symndx];
3183 if ((off & 1) != 0)
3184 off &= ~1;
3185 else
3187 Elf_Internal_Rela outrel;
3188 bfd_byte *loc;
3189 int dr_type, indx;
3190 asection *sreloc;
3192 if (htab->srelgot == NULL)
3193 abort ();
3195 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3197 if (GOT_TLS_GDESC_P (tls_type))
3199 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3200 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3201 <= htab->sgotplt->size);
3202 outrel.r_offset = (htab->sgotplt->output_section->vma
3203 + htab->sgotplt->output_offset
3204 + offplt
3205 + htab->sgotplt_jump_table_size);
3206 sreloc = htab->srelplt;
3207 loc = sreloc->contents;
3208 loc += (htab->next_tls_desc_index++
3209 * sizeof (Elf32_External_Rel));
3210 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3211 <= sreloc->contents + sreloc->size);
3212 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3213 if (indx == 0)
3215 BFD_ASSERT (! unresolved_reloc);
3216 bfd_put_32 (output_bfd,
3217 relocation - dtpoff_base (info),
3218 htab->sgotplt->contents + offplt
3219 + htab->sgotplt_jump_table_size + 4);
3221 else
3223 bfd_put_32 (output_bfd, 0,
3224 htab->sgotplt->contents + offplt
3225 + htab->sgotplt_jump_table_size + 4);
3229 sreloc = htab->srelgot;
3231 outrel.r_offset = (htab->sgot->output_section->vma
3232 + htab->sgot->output_offset + off);
3234 if (GOT_TLS_GD_P (tls_type))
3235 dr_type = R_386_TLS_DTPMOD32;
3236 else if (GOT_TLS_GDESC_P (tls_type))
3237 goto dr_done;
3238 else if (tls_type == GOT_TLS_IE_POS)
3239 dr_type = R_386_TLS_TPOFF;
3240 else
3241 dr_type = R_386_TLS_TPOFF32;
3243 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3244 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3245 htab->sgot->contents + off);
3246 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3247 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
3248 htab->sgot->contents + off);
3249 else if (dr_type != R_386_TLS_DESC)
3250 bfd_put_32 (output_bfd, 0,
3251 htab->sgot->contents + off);
3252 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3254 loc = sreloc->contents;
3255 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3256 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3257 <= sreloc->contents + sreloc->size);
3258 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3260 if (GOT_TLS_GD_P (tls_type))
3262 if (indx == 0)
3264 BFD_ASSERT (! unresolved_reloc);
3265 bfd_put_32 (output_bfd,
3266 relocation - dtpoff_base (info),
3267 htab->sgot->contents + off + 4);
3269 else
3271 bfd_put_32 (output_bfd, 0,
3272 htab->sgot->contents + off + 4);
3273 outrel.r_info = ELF32_R_INFO (indx,
3274 R_386_TLS_DTPOFF32);
3275 outrel.r_offset += 4;
3276 sreloc->reloc_count++;
3277 loc += sizeof (Elf32_External_Rel);
3278 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3279 <= sreloc->contents + sreloc->size);
3280 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3283 else if (tls_type == GOT_TLS_IE_BOTH)
3285 bfd_put_32 (output_bfd,
3286 indx == 0 ? relocation - dtpoff_base (info) : 0,
3287 htab->sgot->contents + off + 4);
3288 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3289 outrel.r_offset += 4;
3290 sreloc->reloc_count++;
3291 loc += sizeof (Elf32_External_Rel);
3292 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3295 dr_done:
3296 if (h != NULL)
3297 h->got.offset |= 1;
3298 else
3299 local_got_offsets[r_symndx] |= 1;
3302 if (off >= (bfd_vma) -2
3303 && ! GOT_TLS_GDESC_P (tls_type))
3304 abort ();
3305 if (r_type == R_386_TLS_GOTDESC
3306 || r_type == R_386_TLS_DESC_CALL)
3308 relocation = htab->sgotplt_jump_table_size + offplt;
3309 unresolved_reloc = FALSE;
3311 else if (r_type == ELF32_R_TYPE (rel->r_info))
3313 bfd_vma g_o_t = htab->sgotplt->output_section->vma
3314 + htab->sgotplt->output_offset;
3315 relocation = htab->sgot->output_section->vma
3316 + htab->sgot->output_offset + off - g_o_t;
3317 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3318 && tls_type == GOT_TLS_IE_BOTH)
3319 relocation += 4;
3320 if (r_type == R_386_TLS_IE)
3321 relocation += g_o_t;
3322 unresolved_reloc = FALSE;
3324 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3326 unsigned int val, type;
3327 bfd_vma roff;
3329 /* GD->IE transition. */
3330 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3331 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3332 if (type == 0x04)
3334 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3335 Change it into:
3336 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3337 val >>= 3;
3338 roff = rel->r_offset - 3;
3340 else
3342 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3343 Change it into:
3344 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3345 roff = rel->r_offset - 2;
3347 memcpy (contents + roff,
3348 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3349 contents[roff + 7] = 0x80 | (val & 7);
3350 /* If foo is used only with foo@gotntpoff(%reg) and
3351 foo@indntpoff, but not with foo@gottpoff(%reg), change
3352 subl $foo@gottpoff(%reg), %eax
3353 into:
3354 addl $foo@gotntpoff(%reg), %eax. */
3355 if (tls_type == GOT_TLS_IE_POS)
3356 contents[roff + 6] = 0x03;
3357 bfd_put_32 (output_bfd,
3358 htab->sgot->output_section->vma
3359 + htab->sgot->output_offset + off
3360 - htab->sgotplt->output_section->vma
3361 - htab->sgotplt->output_offset,
3362 contents + roff + 8);
3363 /* Skip R_386_PLT32. */
3364 rel++;
3365 continue;
3367 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3369 /* GDesc -> IE transition.
3370 It's originally something like:
3371 leal x@tlsdesc(%ebx), %eax
3373 Change it to:
3374 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3376 movl x@gottpoff(%ebx), %eax # before negl %eax
3378 Registers other than %eax may be set up here. */
3380 bfd_vma roff;
3382 /* First, make sure it's a leal adding ebx to a 32-bit
3383 offset into any register, although it's probably
3384 almost always going to be eax. */
3385 roff = rel->r_offset;
3387 /* Now modify the instruction as appropriate. */
3388 /* To turn a leal into a movl in the form we use it, it
3389 suffices to change the first byte from 0x8d to 0x8b.
3390 aoliva FIXME: should we decide to keep the leal, all
3391 we have to do is remove the statement below, and
3392 adjust the relaxation of R_386_TLS_DESC_CALL. */
3393 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3395 if (tls_type == GOT_TLS_IE_BOTH)
3396 off += 4;
3398 bfd_put_32 (output_bfd,
3399 htab->sgot->output_section->vma
3400 + htab->sgot->output_offset + off
3401 - htab->sgotplt->output_section->vma
3402 - htab->sgotplt->output_offset,
3403 contents + roff);
3404 continue;
3406 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3408 /* GDesc -> IE transition.
3409 It's originally:
3410 call *(%eax)
3412 Change it to:
3413 xchg %ax,%ax
3415 negl %eax
3416 depending on how we transformed the TLS_GOTDESC above.
3419 bfd_vma roff;
3421 roff = rel->r_offset;
3423 /* Now modify the instruction as appropriate. */
3424 if (tls_type != GOT_TLS_IE_NEG)
3426 /* xchg %ax,%ax */
3427 bfd_put_8 (output_bfd, 0x66, contents + roff);
3428 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3430 else
3432 /* negl %eax */
3433 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3434 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3437 continue;
3439 else
3440 BFD_ASSERT (FALSE);
3441 break;
3443 case R_386_TLS_LDM:
3444 if (! elf_i386_tls_transition (info, input_bfd,
3445 input_section, contents,
3446 symtab_hdr, sym_hashes,
3447 &r_type, GOT_UNKNOWN, rel,
3448 relend, h))
3449 return FALSE;
3451 if (r_type != R_386_TLS_LDM)
3453 /* LD->LE transition:
3454 leal foo(%reg), %eax; call ___tls_get_addr.
3455 We change it into:
3456 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
3457 BFD_ASSERT (r_type == R_386_TLS_LE_32);
3458 memcpy (contents + rel->r_offset - 2,
3459 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3460 /* Skip R_386_PC32/R_386_PLT32. */
3461 rel++;
3462 continue;
3465 if (htab->sgot == NULL)
3466 abort ();
3468 off = htab->tls_ldm_got.offset;
3469 if (off & 1)
3470 off &= ~1;
3471 else
3473 Elf_Internal_Rela outrel;
3474 bfd_byte *loc;
3476 if (htab->srelgot == NULL)
3477 abort ();
3479 outrel.r_offset = (htab->sgot->output_section->vma
3480 + htab->sgot->output_offset + off);
3482 bfd_put_32 (output_bfd, 0,
3483 htab->sgot->contents + off);
3484 bfd_put_32 (output_bfd, 0,
3485 htab->sgot->contents + off + 4);
3486 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3487 loc = htab->srelgot->contents;
3488 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3489 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3490 htab->tls_ldm_got.offset |= 1;
3492 relocation = htab->sgot->output_section->vma
3493 + htab->sgot->output_offset + off
3494 - htab->sgotplt->output_section->vma
3495 - htab->sgotplt->output_offset;
3496 unresolved_reloc = FALSE;
3497 break;
3499 case R_386_TLS_LDO_32:
3500 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3501 relocation -= dtpoff_base (info);
3502 else
3503 /* When converting LDO to LE, we must negate. */
3504 relocation = -tpoff (info, relocation);
3505 break;
3507 case R_386_TLS_LE_32:
3508 case R_386_TLS_LE:
3509 if (info->shared)
3511 Elf_Internal_Rela outrel;
3512 asection *sreloc;
3513 bfd_byte *loc;
3514 int indx;
3516 outrel.r_offset = rel->r_offset
3517 + input_section->output_section->vma
3518 + input_section->output_offset;
3519 if (h != NULL && h->dynindx != -1)
3520 indx = h->dynindx;
3521 else
3522 indx = 0;
3523 if (r_type == R_386_TLS_LE_32)
3524 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3525 else
3526 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3527 sreloc = elf_section_data (input_section)->sreloc;
3528 if (sreloc == NULL)
3529 abort ();
3530 loc = sreloc->contents;
3531 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3532 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3533 if (indx)
3534 continue;
3535 else if (r_type == R_386_TLS_LE_32)
3536 relocation = dtpoff_base (info) - relocation;
3537 else
3538 relocation -= dtpoff_base (info);
3540 else if (r_type == R_386_TLS_LE_32)
3541 relocation = tpoff (info, relocation);
3542 else
3543 relocation = -tpoff (info, relocation);
3544 break;
3546 default:
3547 break;
3550 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3551 because such sections are not SEC_ALLOC and thus ld.so will
3552 not process them. */
3553 if (unresolved_reloc
3554 && !((input_section->flags & SEC_DEBUGGING) != 0
3555 && h->def_dynamic))
3557 (*_bfd_error_handler)
3558 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3559 input_bfd,
3560 input_section,
3561 (long) rel->r_offset,
3562 howto->name,
3563 h->root.root.string);
3564 return FALSE;
3567 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3568 contents, rel->r_offset,
3569 relocation, 0);
3571 if (r != bfd_reloc_ok)
3573 const char *name;
3575 if (h != NULL)
3576 name = h->root.root.string;
3577 else
3579 name = bfd_elf_string_from_elf_section (input_bfd,
3580 symtab_hdr->sh_link,
3581 sym->st_name);
3582 if (name == NULL)
3583 return FALSE;
3584 if (*name == '\0')
3585 name = bfd_section_name (input_bfd, sec);
3588 if (r == bfd_reloc_overflow)
3590 if (! ((*info->callbacks->reloc_overflow)
3591 (info, (h ? &h->root : NULL), name, howto->name,
3592 (bfd_vma) 0, input_bfd, input_section,
3593 rel->r_offset)))
3594 return FALSE;
3596 else
3598 (*_bfd_error_handler)
3599 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3600 input_bfd, input_section,
3601 (long) rel->r_offset, name, (int) r);
3602 return FALSE;
3607 return TRUE;
3610 /* Finish up dynamic symbol handling. We set the contents of various
3611 dynamic sections here. */
3613 static bfd_boolean
3614 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3615 struct bfd_link_info *info,
3616 struct elf_link_hash_entry *h,
3617 Elf_Internal_Sym *sym)
3619 struct elf_i386_link_hash_table *htab;
3621 htab = elf_i386_hash_table (info);
3623 if (h->plt.offset != (bfd_vma) -1)
3625 bfd_vma plt_index;
3626 bfd_vma got_offset;
3627 Elf_Internal_Rela rel;
3628 bfd_byte *loc;
3630 /* This symbol has an entry in the procedure linkage table. Set
3631 it up. */
3633 if (h->dynindx == -1
3634 || htab->splt == NULL
3635 || htab->sgotplt == NULL
3636 || htab->srelplt == NULL)
3637 abort ();
3639 /* Get the index in the procedure linkage table which
3640 corresponds to this symbol. This is the index of this symbol
3641 in all the symbols for which we are making plt entries. The
3642 first entry in the procedure linkage table is reserved. */
3643 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3645 /* Get the offset into the .got table of the entry that
3646 corresponds to this function. Each .got entry is 4 bytes.
3647 The first three are reserved. */
3648 got_offset = (plt_index + 3) * 4;
3650 /* Fill in the entry in the procedure linkage table. */
3651 if (! info->shared)
3653 memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
3654 PLT_ENTRY_SIZE);
3655 bfd_put_32 (output_bfd,
3656 (htab->sgotplt->output_section->vma
3657 + htab->sgotplt->output_offset
3658 + got_offset),
3659 htab->splt->contents + h->plt.offset + 2);
3661 if (htab->is_vxworks)
3663 int s, k, reloc_index;
3665 /* Create the R_386_32 relocation referencing the GOT
3666 for this PLT entry. */
3668 /* S: Current slot number (zero-based). */
3669 s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3670 /* K: Number of relocations for PLTResolve. */
3671 if (info->shared)
3672 k = PLTRESOLVE_RELOCS_SHLIB;
3673 else
3674 k = PLTRESOLVE_RELOCS;
3675 /* Skip the PLTresolve relocations, and the relocations for
3676 the other PLT slots. */
3677 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3678 loc = (htab->srelplt2->contents + reloc_index
3679 * sizeof (Elf32_External_Rel));
3681 rel.r_offset = (htab->splt->output_section->vma
3682 + htab->splt->output_offset
3683 + h->plt.offset + 2),
3684 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3685 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3687 /* Create the R_386_32 relocation referencing the beginning of
3688 the PLT for this GOT entry. */
3689 rel.r_offset = (htab->sgotplt->output_section->vma
3690 + htab->sgotplt->output_offset
3691 + got_offset);
3692 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3693 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3694 loc + sizeof (Elf32_External_Rel));
3697 else
3699 memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
3700 PLT_ENTRY_SIZE);
3701 bfd_put_32 (output_bfd, got_offset,
3702 htab->splt->contents + h->plt.offset + 2);
3705 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
3706 htab->splt->contents + h->plt.offset + 7);
3707 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3708 htab->splt->contents + h->plt.offset + 12);
3710 /* Fill in the entry in the global offset table. */
3711 bfd_put_32 (output_bfd,
3712 (htab->splt->output_section->vma
3713 + htab->splt->output_offset
3714 + h->plt.offset
3715 + 6),
3716 htab->sgotplt->contents + got_offset);
3718 /* Fill in the entry in the .rel.plt section. */
3719 rel.r_offset = (htab->sgotplt->output_section->vma
3720 + htab->sgotplt->output_offset
3721 + got_offset);
3722 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3723 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
3724 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3726 if (!h->def_regular)
3728 /* Mark the symbol as undefined, rather than as defined in
3729 the .plt section. Leave the value if there were any
3730 relocations where pointer equality matters (this is a clue
3731 for the dynamic linker, to make function pointer
3732 comparisons work between an application and shared
3733 library), otherwise set it to zero. If a function is only
3734 called from a binary, there is no need to slow down
3735 shared libraries because of that. */
3736 sym->st_shndx = SHN_UNDEF;
3737 if (!h->pointer_equality_needed)
3738 sym->st_value = 0;
3742 if (h->got.offset != (bfd_vma) -1
3743 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
3744 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
3746 Elf_Internal_Rela rel;
3747 bfd_byte *loc;
3749 /* This symbol has an entry in the global offset table. Set it
3750 up. */
3752 if (htab->sgot == NULL || htab->srelgot == NULL)
3753 abort ();
3755 rel.r_offset = (htab->sgot->output_section->vma
3756 + htab->sgot->output_offset
3757 + (h->got.offset & ~(bfd_vma) 1));
3759 /* If this is a static link, or it is a -Bsymbolic link and the
3760 symbol is defined locally or was forced to be local because
3761 of a version file, we just want to emit a RELATIVE reloc.
3762 The entry in the global offset table will already have been
3763 initialized in the relocate_section function. */
3764 if (info->shared
3765 && SYMBOL_REFERENCES_LOCAL (info, h))
3767 BFD_ASSERT((h->got.offset & 1) != 0);
3768 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3770 else
3772 BFD_ASSERT((h->got.offset & 1) == 0);
3773 bfd_put_32 (output_bfd, (bfd_vma) 0,
3774 htab->sgot->contents + h->got.offset);
3775 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3778 loc = htab->srelgot->contents;
3779 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3780 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3783 if (h->needs_copy)
3785 Elf_Internal_Rela rel;
3786 bfd_byte *loc;
3788 /* This symbol needs a copy reloc. Set it up. */
3790 if (h->dynindx == -1
3791 || (h->root.type != bfd_link_hash_defined
3792 && h->root.type != bfd_link_hash_defweak)
3793 || htab->srelbss == NULL)
3794 abort ();
3796 rel.r_offset = (h->root.u.def.value
3797 + h->root.u.def.section->output_section->vma
3798 + h->root.u.def.section->output_offset);
3799 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3800 loc = htab->srelbss->contents;
3801 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
3802 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3805 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
3806 On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
3807 is relative to the ".got" section. */
3808 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3809 || (!htab->is_vxworks && h == htab->elf.hgot))
3810 sym->st_shndx = SHN_ABS;
3812 return TRUE;
3815 /* Used to decide how to sort relocs in an optimal manner for the
3816 dynamic linker, before writing them out. */
3818 static enum elf_reloc_type_class
3819 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
3821 switch (ELF32_R_TYPE (rela->r_info))
3823 case R_386_RELATIVE:
3824 return reloc_class_relative;
3825 case R_386_JUMP_SLOT:
3826 return reloc_class_plt;
3827 case R_386_COPY:
3828 return reloc_class_copy;
3829 default:
3830 return reloc_class_normal;
3834 /* Finish up the dynamic sections. */
3836 static bfd_boolean
3837 elf_i386_finish_dynamic_sections (bfd *output_bfd,
3838 struct bfd_link_info *info)
3840 struct elf_i386_link_hash_table *htab;
3841 bfd *dynobj;
3842 asection *sdyn;
3844 htab = elf_i386_hash_table (info);
3845 dynobj = htab->elf.dynobj;
3846 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3848 if (htab->elf.dynamic_sections_created)
3850 Elf32_External_Dyn *dyncon, *dynconend;
3852 if (sdyn == NULL || htab->sgot == NULL)
3853 abort ();
3855 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3856 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3857 for (; dyncon < dynconend; dyncon++)
3859 Elf_Internal_Dyn dyn;
3860 asection *s;
3862 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3864 switch (dyn.d_tag)
3866 default:
3867 if (htab->is_vxworks
3868 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
3869 break;
3870 continue;
3872 case DT_PLTGOT:
3873 s = htab->sgotplt;
3874 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3875 break;
3877 case DT_JMPREL:
3878 s = htab->srelplt;
3879 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3880 break;
3882 case DT_PLTRELSZ:
3883 s = htab->srelplt;
3884 dyn.d_un.d_val = s->size;
3885 break;
3887 case DT_RELSZ:
3888 /* My reading of the SVR4 ABI indicates that the
3889 procedure linkage table relocs (DT_JMPREL) should be
3890 included in the overall relocs (DT_REL). This is
3891 what Solaris does. However, UnixWare can not handle
3892 that case. Therefore, we override the DT_RELSZ entry
3893 here to make it not include the JMPREL relocs. */
3894 s = htab->srelplt;
3895 if (s == NULL)
3896 continue;
3897 dyn.d_un.d_val -= s->size;
3898 break;
3900 case DT_REL:
3901 /* We may not be using the standard ELF linker script.
3902 If .rel.plt is the first .rel section, we adjust
3903 DT_REL to not include it. */
3904 s = htab->srelplt;
3905 if (s == NULL)
3906 continue;
3907 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
3908 continue;
3909 dyn.d_un.d_ptr += s->size;
3910 break;
3913 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3916 /* Fill in the first entry in the procedure linkage table. */
3917 if (htab->splt && htab->splt->size > 0)
3919 if (info->shared)
3921 memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
3922 sizeof (elf_i386_pic_plt0_entry));
3923 memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
3924 htab->plt0_pad_byte,
3925 PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
3927 else
3929 memcpy (htab->splt->contents, elf_i386_plt0_entry,
3930 sizeof(elf_i386_plt0_entry));
3931 memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
3932 htab->plt0_pad_byte,
3933 PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
3934 bfd_put_32 (output_bfd,
3935 (htab->sgotplt->output_section->vma
3936 + htab->sgotplt->output_offset
3937 + 4),
3938 htab->splt->contents + 2);
3939 bfd_put_32 (output_bfd,
3940 (htab->sgotplt->output_section->vma
3941 + htab->sgotplt->output_offset
3942 + 8),
3943 htab->splt->contents + 8);
3945 if (htab->is_vxworks)
3947 Elf_Internal_Rela rel;
3949 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
3950 On IA32 we use REL relocations so the addend goes in
3951 the PLT directly. */
3952 rel.r_offset = (htab->splt->output_section->vma
3953 + htab->splt->output_offset
3954 + 2);
3955 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3956 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3957 htab->srelplt2->contents);
3958 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
3959 rel.r_offset = (htab->splt->output_section->vma
3960 + htab->splt->output_offset
3961 + 8);
3962 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3963 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3964 htab->srelplt2->contents +
3965 sizeof (Elf32_External_Rel));
3969 /* UnixWare sets the entsize of .plt to 4, although that doesn't
3970 really seem like the right value. */
3971 elf_section_data (htab->splt->output_section)
3972 ->this_hdr.sh_entsize = 4;
3974 /* Correct the .rel.plt.unloaded relocations. */
3975 if (htab->is_vxworks && !info->shared)
3977 int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
3978 unsigned char *p;
3980 p = htab->srelplt2->contents;
3981 if (info->shared)
3982 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
3983 else
3984 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
3986 for (; num_plts; num_plts--)
3988 Elf_Internal_Rela rel;
3989 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
3990 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3991 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3992 p += sizeof (Elf32_External_Rel);
3994 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
3995 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3996 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3997 p += sizeof (Elf32_External_Rel);
4003 if (htab->sgotplt)
4005 /* Fill in the first three entries in the global offset table. */
4006 if (htab->sgotplt->size > 0)
4008 bfd_put_32 (output_bfd,
4009 (sdyn == NULL ? 0
4010 : sdyn->output_section->vma + sdyn->output_offset),
4011 htab->sgotplt->contents);
4012 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
4013 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
4016 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
4019 if (htab->sgot && htab->sgot->size > 0)
4020 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
4022 return TRUE;
4025 /* Return address for Ith PLT stub in section PLT, for relocation REL
4026 or (bfd_vma) -1 if it should not be included. */
4028 static bfd_vma
4029 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
4030 const arelent *rel ATTRIBUTE_UNUSED)
4032 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4035 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4037 static bfd_boolean
4038 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
4040 if (h->plt.offset != (bfd_vma) -1
4041 && !h->def_regular
4042 && !h->pointer_equality_needed)
4043 return FALSE;
4045 return _bfd_elf_hash_symbol (h);
4048 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
4049 #define TARGET_LITTLE_NAME "elf32-i386"
4050 #define ELF_ARCH bfd_arch_i386
4051 #define ELF_MACHINE_CODE EM_386
4052 #define ELF_MAXPAGESIZE 0x1000
4054 #define elf_backend_can_gc_sections 1
4055 #define elf_backend_can_refcount 1
4056 #define elf_backend_want_got_plt 1
4057 #define elf_backend_plt_readonly 1
4058 #define elf_backend_want_plt_sym 0
4059 #define elf_backend_got_header_size 12
4061 /* Support RELA for objdump of prelink objects. */
4062 #define elf_info_to_howto elf_i386_info_to_howto_rel
4063 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4065 #define bfd_elf32_mkobject elf_i386_mkobject
4067 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4068 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
4069 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
4070 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
4072 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
4073 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4074 #define elf_backend_check_relocs elf_i386_check_relocs
4075 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
4076 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
4077 #define elf_backend_fake_sections elf_i386_fake_sections
4078 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4079 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4080 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
4081 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
4082 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4083 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4084 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4085 #define elf_backend_relocate_section elf_i386_relocate_section
4086 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
4087 #define elf_backend_always_size_sections elf_i386_always_size_sections
4088 #define elf_backend_omit_section_dynsym \
4089 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4090 #define elf_backend_plt_sym_val elf_i386_plt_sym_val
4091 #define elf_backend_hash_symbol elf_i386_hash_symbol
4093 #include "elf32-target.h"
4095 /* FreeBSD support. */
4097 #undef TARGET_LITTLE_SYM
4098 #define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
4099 #undef TARGET_LITTLE_NAME
4100 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4101 #undef ELF_OSABI
4102 #define ELF_OSABI ELFOSABI_FREEBSD
4104 /* The kernel recognizes executables as valid only if they carry a
4105 "FreeBSD" label in the ELF header. So we put this label on all
4106 executables and (for simplicity) also all other object files. */
4108 static void
4109 elf_i386_post_process_headers (bfd *abfd,
4110 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4112 Elf_Internal_Ehdr *i_ehdrp;
4114 i_ehdrp = elf_elfheader (abfd);
4116 /* Put an ABI label supported by FreeBSD >= 4.1. */
4117 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
4118 #ifdef OLD_FREEBSD_ABI_LABEL
4119 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4120 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4121 #endif
4124 #undef elf_backend_post_process_headers
4125 #define elf_backend_post_process_headers elf_i386_post_process_headers
4126 #undef elf32_bed
4127 #define elf32_bed elf32_i386_fbsd_bed
4129 #include "elf32-target.h"
4131 /* VxWorks support. */
4133 #undef TARGET_LITTLE_SYM
4134 #define TARGET_LITTLE_SYM bfd_elf32_i386_vxworks_vec
4135 #undef TARGET_LITTLE_NAME
4136 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
4137 #undef ELF_OSABI
4139 /* Like elf_i386_link_hash_table_create but with tweaks for VxWorks. */
4141 static struct bfd_link_hash_table *
4142 elf_i386_vxworks_link_hash_table_create (bfd *abfd)
4144 struct bfd_link_hash_table *ret;
4145 struct elf_i386_link_hash_table *htab;
4147 ret = elf_i386_link_hash_table_create (abfd);
4148 if (ret)
4150 htab = (struct elf_i386_link_hash_table *) ret;
4151 htab->is_vxworks = 1;
4152 htab->plt0_pad_byte = 0x90;
4155 return ret;
4159 #undef elf_backend_relocs_compatible
4160 #undef elf_backend_post_process_headers
4161 #undef bfd_elf32_bfd_link_hash_table_create
4162 #define bfd_elf32_bfd_link_hash_table_create \
4163 elf_i386_vxworks_link_hash_table_create
4164 #undef elf_backend_add_symbol_hook
4165 #define elf_backend_add_symbol_hook \
4166 elf_vxworks_add_symbol_hook
4167 #undef elf_backend_link_output_symbol_hook
4168 #define elf_backend_link_output_symbol_hook \
4169 elf_vxworks_link_output_symbol_hook
4170 #undef elf_backend_emit_relocs
4171 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
4172 #undef elf_backend_final_write_processing
4173 #define elf_backend_final_write_processing \
4174 elf_vxworks_final_write_processing
4176 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4177 define it. */
4178 #undef elf_backend_want_plt_sym
4179 #define elf_backend_want_plt_sym 1
4181 #undef elf32_bed
4182 #define elf32_bed elf32_i386_vxworks_bed
4184 #include "elf32-target.h"