include/
[binutils.git] / bfd / elf32-i386.c
blob9e96e215fb90e59fe6b184dff7133c208b529134
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 LD 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 return (h != NULL
1011 && h->root.root.string != NULL
1012 && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1013 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
1014 && (strcmp (h->root.root.string, "___tls_get_addr") == 0));
1016 case R_386_TLS_IE:
1017 /* Check transition from IE access model:
1018 movl foo@indntpoff(%rip), %eax
1019 movl foo@indntpoff(%rip), %reg
1020 addl foo@indntpoff(%rip), %reg
1023 if (offset < 1 || (offset + 4) > sec->size)
1024 return FALSE;
1026 /* Check "movl foo@tpoff(%rip), %eax" first. */
1027 val = bfd_get_8 (abfd, contents + offset - 1);
1028 if (val == 0xa1)
1029 return TRUE;
1031 if (offset < 2)
1032 return FALSE;
1034 /* Check movl|addl foo@tpoff(%rip), %reg. */
1035 type = bfd_get_8 (abfd, contents + offset - 2);
1036 return ((type == 0x8b || type == 0x03)
1037 && (val & 0xc7) == 0x05);
1039 case R_386_TLS_GOTIE:
1040 case R_386_TLS_IE_32:
1041 /* Check transition from {IE_32,GOTIE} access model:
1042 subl foo@{tpoff,gontoff}(%reg1), %reg2
1043 movl foo@{tpoff,gontoff}(%reg1), %reg2
1044 addl foo@{tpoff,gontoff}(%reg1), %reg2
1047 if (offset < 2 || (offset + 4) > sec->size)
1048 return FALSE;
1050 val = bfd_get_8 (abfd, contents + offset - 1);
1051 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1052 return FALSE;
1054 type = bfd_get_8 (abfd, contents + offset - 2);
1055 return type == 0x8b || type == 0x2b || type == 0x03;
1057 case R_386_TLS_GOTDESC:
1058 /* Check transition from GDesc access model:
1059 leal x@tlsdesc(%ebx), %eax
1061 Make sure it's a leal adding ebx to a 32-bit offset
1062 into any register, although it's probably almost always
1063 going to be eax. */
1065 if (offset < 2 || (offset + 4) > sec->size)
1066 return FALSE;
1068 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1069 return FALSE;
1071 val = bfd_get_8 (abfd, contents + offset - 1);
1072 return (val & 0xc7) == 0x83;
1074 case R_386_TLS_DESC_CALL:
1075 /* Check transition from GDesc access model:
1076 call *x@tlsdesc(%rax)
1078 if (offset + 2 <= sec->size)
1080 /* Make sure that it's a call *x@tlsdesc(%rax). */
1081 static i386_opcode16 call = { { 0xff, 0x10 } };
1082 return bfd_get_16 (abfd, contents + offset) == call.i;
1085 return FALSE;
1087 default:
1088 abort ();
1092 /* Return TRUE if the TLS access transition is OK or no transition
1093 will be performed. Update R_TYPE if there is a transition. */
1095 static bfd_boolean
1096 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1097 asection *sec, bfd_byte *contents,
1098 Elf_Internal_Shdr *symtab_hdr,
1099 struct elf_link_hash_entry **sym_hashes,
1100 unsigned int *r_type, int tls_type,
1101 const Elf_Internal_Rela *rel,
1102 const Elf_Internal_Rela *relend,
1103 struct elf_link_hash_entry *h)
1105 unsigned int from_type = *r_type;
1106 unsigned int to_type = from_type;
1107 bfd_boolean check = TRUE;
1109 switch (from_type)
1111 case R_386_TLS_GD:
1112 case R_386_TLS_GOTDESC:
1113 case R_386_TLS_DESC_CALL:
1114 case R_386_TLS_IE_32:
1115 case R_386_TLS_IE:
1116 case R_386_TLS_GOTIE:
1117 if (!info->shared)
1119 if (h == NULL)
1120 to_type = R_386_TLS_LE_32;
1121 else if (from_type != R_386_TLS_IE
1122 && from_type != R_386_TLS_GOTIE)
1123 to_type = R_386_TLS_IE_32;
1126 /* When we are called from elf_i386_relocate_section, CONTENTS
1127 isn't NULL and there may be additional transitions based on
1128 TLS_TYPE. */
1129 if (contents != NULL)
1131 unsigned int new_to_type = to_type;
1133 if (!info->shared
1134 && h != NULL
1135 && h->dynindx == -1
1136 && (tls_type & GOT_TLS_IE))
1137 new_to_type = R_386_TLS_LE_32;
1139 if (to_type == R_386_TLS_GD
1140 || to_type == R_386_TLS_GOTDESC
1141 || to_type == R_386_TLS_DESC_CALL)
1143 if (tls_type == GOT_TLS_IE_POS)
1144 new_to_type = R_386_TLS_GOTIE;
1145 else if (tls_type & GOT_TLS_IE)
1146 new_to_type = R_386_TLS_IE_32;
1149 /* We checked the transition before when we were called from
1150 elf_i386_check_relocs. We only want to check the new
1151 transition which hasn't been checked before. */
1152 check = new_to_type != to_type && from_type == to_type;
1153 to_type = new_to_type;
1156 break;
1158 case R_386_TLS_LDM:
1159 if (!info->shared)
1160 to_type = R_386_TLS_LE_32;
1161 break;
1163 default:
1164 return TRUE;
1167 /* Return TRUE if there is no transition. */
1168 if (from_type == to_type)
1169 return TRUE;
1171 /* Check if the transition can be performed. */
1172 if (check
1173 && ! elf_i386_check_tls_transition (abfd, sec, contents,
1174 symtab_hdr, sym_hashes,
1175 from_type, rel, relend))
1177 reloc_howto_type *from, *to;
1179 from = elf_i386_rtype_to_howto (abfd, from_type);
1180 to = elf_i386_rtype_to_howto (abfd, to_type);
1182 (*_bfd_error_handler)
1183 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1184 "in section `%A' failed"),
1185 abfd, sec, from->name, to->name,
1186 h ? h->root.root.string : "a local symbol",
1187 (unsigned long) rel->r_offset);
1188 bfd_set_error (bfd_error_bad_value);
1189 return FALSE;
1192 *r_type = to_type;
1193 return TRUE;
1196 /* Look through the relocs for a section during the first phase, and
1197 calculate needed space in the global offset table, procedure linkage
1198 table, and dynamic reloc sections. */
1200 static bfd_boolean
1201 elf_i386_check_relocs (bfd *abfd,
1202 struct bfd_link_info *info,
1203 asection *sec,
1204 const Elf_Internal_Rela *relocs)
1206 struct elf_i386_link_hash_table *htab;
1207 Elf_Internal_Shdr *symtab_hdr;
1208 struct elf_link_hash_entry **sym_hashes;
1209 const Elf_Internal_Rela *rel;
1210 const Elf_Internal_Rela *rel_end;
1211 asection *sreloc;
1213 if (info->relocatable)
1214 return TRUE;
1216 BFD_ASSERT (is_i386_elf (abfd));
1218 htab = elf_i386_hash_table (info);
1219 symtab_hdr = &elf_symtab_hdr (abfd);
1220 sym_hashes = elf_sym_hashes (abfd);
1222 sreloc = NULL;
1224 rel_end = relocs + sec->reloc_count;
1225 for (rel = relocs; rel < rel_end; rel++)
1227 unsigned int r_type;
1228 unsigned long r_symndx;
1229 struct elf_link_hash_entry *h;
1231 r_symndx = ELF32_R_SYM (rel->r_info);
1232 r_type = ELF32_R_TYPE (rel->r_info);
1234 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1236 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1237 abfd,
1238 r_symndx);
1239 return FALSE;
1242 if (r_symndx < symtab_hdr->sh_info)
1243 h = NULL;
1244 else
1246 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1247 while (h->root.type == bfd_link_hash_indirect
1248 || h->root.type == bfd_link_hash_warning)
1249 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1252 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1253 symtab_hdr, sym_hashes,
1254 &r_type, GOT_UNKNOWN,
1255 rel, rel_end, h))
1256 return FALSE;
1258 switch (r_type)
1260 case R_386_TLS_LDM:
1261 htab->tls_ldm_got.refcount += 1;
1262 goto create_got;
1264 case R_386_PLT32:
1265 /* This symbol requires a procedure linkage table entry. We
1266 actually build the entry in adjust_dynamic_symbol,
1267 because this might be a case of linking PIC code which is
1268 never referenced by a dynamic object, in which case we
1269 don't need to generate a procedure linkage table entry
1270 after all. */
1272 /* If this is a local symbol, we resolve it directly without
1273 creating a procedure linkage table entry. */
1274 if (h == NULL)
1275 continue;
1277 h->needs_plt = 1;
1278 h->plt.refcount += 1;
1279 break;
1281 case R_386_TLS_IE_32:
1282 case R_386_TLS_IE:
1283 case R_386_TLS_GOTIE:
1284 if (info->shared)
1285 info->flags |= DF_STATIC_TLS;
1286 /* Fall through */
1288 case R_386_GOT32:
1289 case R_386_TLS_GD:
1290 case R_386_TLS_GOTDESC:
1291 case R_386_TLS_DESC_CALL:
1292 /* This symbol requires a global offset table entry. */
1294 int tls_type, old_tls_type;
1296 switch (r_type)
1298 default:
1299 case R_386_GOT32: tls_type = GOT_NORMAL; break;
1300 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1301 case R_386_TLS_GOTDESC:
1302 case R_386_TLS_DESC_CALL:
1303 tls_type = GOT_TLS_GDESC; break;
1304 case R_386_TLS_IE_32:
1305 if (ELF32_R_TYPE (rel->r_info) == r_type)
1306 tls_type = GOT_TLS_IE_NEG;
1307 else
1308 /* If this is a GD->IE transition, we may use either of
1309 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1310 tls_type = GOT_TLS_IE;
1311 break;
1312 case R_386_TLS_IE:
1313 case R_386_TLS_GOTIE:
1314 tls_type = GOT_TLS_IE_POS; break;
1317 if (h != NULL)
1319 h->got.refcount += 1;
1320 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1322 else
1324 bfd_signed_vma *local_got_refcounts;
1326 /* This is a global offset table entry for a local symbol. */
1327 local_got_refcounts = elf_local_got_refcounts (abfd);
1328 if (local_got_refcounts == NULL)
1330 bfd_size_type size;
1332 size = symtab_hdr->sh_info;
1333 size *= (sizeof (bfd_signed_vma)
1334 + sizeof (bfd_vma) + sizeof(char));
1335 local_got_refcounts = bfd_zalloc (abfd, size);
1336 if (local_got_refcounts == NULL)
1337 return FALSE;
1338 elf_local_got_refcounts (abfd) = local_got_refcounts;
1339 elf_i386_local_tlsdesc_gotent (abfd)
1340 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1341 elf_i386_local_got_tls_type (abfd)
1342 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1344 local_got_refcounts[r_symndx] += 1;
1345 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1348 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1349 tls_type |= old_tls_type;
1350 /* If a TLS symbol is accessed using IE at least once,
1351 there is no point to use dynamic model for it. */
1352 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1353 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1354 || (tls_type & GOT_TLS_IE) == 0))
1356 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1357 tls_type = old_tls_type;
1358 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1359 && GOT_TLS_GD_ANY_P (tls_type))
1360 tls_type |= old_tls_type;
1361 else
1363 (*_bfd_error_handler)
1364 (_("%B: `%s' accessed both as normal and "
1365 "thread local symbol"),
1366 abfd,
1367 h ? h->root.root.string : "<local>");
1368 return FALSE;
1372 if (old_tls_type != tls_type)
1374 if (h != NULL)
1375 elf_i386_hash_entry (h)->tls_type = tls_type;
1376 else
1377 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1380 /* Fall through */
1382 case R_386_GOTOFF:
1383 case R_386_GOTPC:
1384 create_got:
1385 if (htab->sgot == NULL)
1387 if (htab->elf.dynobj == NULL)
1388 htab->elf.dynobj = abfd;
1389 if (!create_got_section (htab->elf.dynobj, info))
1390 return FALSE;
1392 if (r_type != R_386_TLS_IE)
1393 break;
1394 /* Fall through */
1396 case R_386_TLS_LE_32:
1397 case R_386_TLS_LE:
1398 if (!info->shared)
1399 break;
1400 info->flags |= DF_STATIC_TLS;
1401 /* Fall through */
1403 case R_386_32:
1404 case R_386_PC32:
1405 if (h != NULL && !info->shared)
1407 /* If this reloc is in a read-only section, we might
1408 need a copy reloc. We can't check reliably at this
1409 stage whether the section is read-only, as input
1410 sections have not yet been mapped to output sections.
1411 Tentatively set the flag for now, and correct in
1412 adjust_dynamic_symbol. */
1413 h->non_got_ref = 1;
1415 /* We may need a .plt entry if the function this reloc
1416 refers to is in a shared lib. */
1417 h->plt.refcount += 1;
1418 if (r_type != R_386_PC32)
1419 h->pointer_equality_needed = 1;
1422 /* If we are creating a shared library, and this is a reloc
1423 against a global symbol, or a non PC relative reloc
1424 against a local symbol, then we need to copy the reloc
1425 into the shared library. However, if we are linking with
1426 -Bsymbolic, we do not need to copy a reloc against a
1427 global symbol which is defined in an object we are
1428 including in the link (i.e., DEF_REGULAR is set). At
1429 this point we have not seen all the input files, so it is
1430 possible that DEF_REGULAR is not set now but will be set
1431 later (it is never cleared). In case of a weak definition,
1432 DEF_REGULAR may be cleared later by a strong definition in
1433 a shared library. We account for that possibility below by
1434 storing information in the relocs_copied field of the hash
1435 table entry. A similar situation occurs when creating
1436 shared libraries and symbol visibility changes render the
1437 symbol local.
1439 If on the other hand, we are creating an executable, we
1440 may need to keep relocations for symbols satisfied by a
1441 dynamic library if we manage to avoid copy relocs for the
1442 symbol. */
1443 if ((info->shared
1444 && (sec->flags & SEC_ALLOC) != 0
1445 && (r_type != R_386_PC32
1446 || (h != NULL
1447 && (! SYMBOLIC_BIND (info, h)
1448 || h->root.type == bfd_link_hash_defweak
1449 || !h->def_regular))))
1450 || (ELIMINATE_COPY_RELOCS
1451 && !info->shared
1452 && (sec->flags & SEC_ALLOC) != 0
1453 && h != NULL
1454 && (h->root.type == bfd_link_hash_defweak
1455 || !h->def_regular)))
1457 struct elf_i386_dyn_relocs *p;
1458 struct elf_i386_dyn_relocs **head;
1460 /* We must copy these reloc types into the output file.
1461 Create a reloc section in dynobj and make room for
1462 this reloc. */
1463 if (sreloc == NULL)
1465 const char *name;
1466 bfd *dynobj;
1467 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1468 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
1470 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1471 if (name == NULL)
1472 return FALSE;
1474 if (! CONST_STRNEQ (name, ".rel")
1475 || strcmp (bfd_get_section_name (abfd, sec),
1476 name + 4) != 0)
1478 (*_bfd_error_handler)
1479 (_("%B: bad relocation section name `%s\'"),
1480 abfd, name);
1483 if (htab->elf.dynobj == NULL)
1484 htab->elf.dynobj = abfd;
1486 dynobj = htab->elf.dynobj;
1487 sreloc = bfd_get_section_by_name (dynobj, name);
1488 if (sreloc == NULL)
1490 flagword flags;
1492 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1493 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1494 if ((sec->flags & SEC_ALLOC) != 0)
1495 flags |= SEC_ALLOC | SEC_LOAD;
1496 sreloc = bfd_make_section_with_flags (dynobj,
1497 name,
1498 flags);
1499 if (sreloc == NULL
1500 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1501 return FALSE;
1503 elf_section_data (sec)->sreloc = sreloc;
1506 /* If this is a global symbol, we count the number of
1507 relocations we need for this symbol. */
1508 if (h != NULL)
1510 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1512 else
1514 void **vpp;
1515 /* Track dynamic relocs needed for local syms too.
1516 We really need local syms available to do this
1517 easily. Oh well. */
1519 asection *s;
1520 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1521 sec, r_symndx);
1522 if (s == NULL)
1523 return FALSE;
1525 vpp = &elf_section_data (s)->local_dynrel;
1526 head = (struct elf_i386_dyn_relocs **)vpp;
1529 p = *head;
1530 if (p == NULL || p->sec != sec)
1532 bfd_size_type amt = sizeof *p;
1533 p = bfd_alloc (htab->elf.dynobj, amt);
1534 if (p == NULL)
1535 return FALSE;
1536 p->next = *head;
1537 *head = p;
1538 p->sec = sec;
1539 p->count = 0;
1540 p->pc_count = 0;
1543 p->count += 1;
1544 if (r_type == R_386_PC32)
1545 p->pc_count += 1;
1547 break;
1549 /* This relocation describes the C++ object vtable hierarchy.
1550 Reconstruct it for later use during GC. */
1551 case R_386_GNU_VTINHERIT:
1552 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1553 return FALSE;
1554 break;
1556 /* This relocation describes which C++ vtable entries are actually
1557 used. Record for later use during GC. */
1558 case R_386_GNU_VTENTRY:
1559 BFD_ASSERT (h != NULL);
1560 if (h != NULL
1561 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1562 return FALSE;
1563 break;
1565 default:
1566 break;
1570 return TRUE;
1573 /* Return the section that should be marked against GC for a given
1574 relocation. */
1576 static asection *
1577 elf_i386_gc_mark_hook (asection *sec,
1578 struct bfd_link_info *info,
1579 Elf_Internal_Rela *rel,
1580 struct elf_link_hash_entry *h,
1581 Elf_Internal_Sym *sym)
1583 if (h != NULL)
1584 switch (ELF32_R_TYPE (rel->r_info))
1586 case R_386_GNU_VTINHERIT:
1587 case R_386_GNU_VTENTRY:
1588 return NULL;
1591 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1594 /* Update the got entry reference counts for the section being removed. */
1596 static bfd_boolean
1597 elf_i386_gc_sweep_hook (bfd *abfd,
1598 struct bfd_link_info *info,
1599 asection *sec,
1600 const Elf_Internal_Rela *relocs)
1602 Elf_Internal_Shdr *symtab_hdr;
1603 struct elf_link_hash_entry **sym_hashes;
1604 bfd_signed_vma *local_got_refcounts;
1605 const Elf_Internal_Rela *rel, *relend;
1607 if (info->relocatable)
1608 return TRUE;
1610 elf_section_data (sec)->local_dynrel = NULL;
1612 symtab_hdr = &elf_symtab_hdr (abfd);
1613 sym_hashes = elf_sym_hashes (abfd);
1614 local_got_refcounts = elf_local_got_refcounts (abfd);
1616 relend = relocs + sec->reloc_count;
1617 for (rel = relocs; rel < relend; rel++)
1619 unsigned long r_symndx;
1620 unsigned int r_type;
1621 struct elf_link_hash_entry *h = NULL;
1623 r_symndx = ELF32_R_SYM (rel->r_info);
1624 if (r_symndx >= symtab_hdr->sh_info)
1626 struct elf_i386_link_hash_entry *eh;
1627 struct elf_i386_dyn_relocs **pp;
1628 struct elf_i386_dyn_relocs *p;
1630 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1631 while (h->root.type == bfd_link_hash_indirect
1632 || h->root.type == bfd_link_hash_warning)
1633 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1634 eh = (struct elf_i386_link_hash_entry *) h;
1636 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1637 if (p->sec == sec)
1639 /* Everything must go for SEC. */
1640 *pp = p->next;
1641 break;
1645 r_type = ELF32_R_TYPE (rel->r_info);
1646 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1647 symtab_hdr, sym_hashes,
1648 &r_type, GOT_UNKNOWN,
1649 rel, relend, h))
1650 return FALSE;
1652 switch (r_type)
1654 case R_386_TLS_LDM:
1655 if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1656 elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1657 break;
1659 case R_386_TLS_GD:
1660 case R_386_TLS_GOTDESC:
1661 case R_386_TLS_DESC_CALL:
1662 case R_386_TLS_IE_32:
1663 case R_386_TLS_IE:
1664 case R_386_TLS_GOTIE:
1665 case R_386_GOT32:
1666 if (h != NULL)
1668 if (h->got.refcount > 0)
1669 h->got.refcount -= 1;
1671 else if (local_got_refcounts != NULL)
1673 if (local_got_refcounts[r_symndx] > 0)
1674 local_got_refcounts[r_symndx] -= 1;
1676 break;
1678 case R_386_32:
1679 case R_386_PC32:
1680 if (info->shared)
1681 break;
1682 /* Fall through */
1684 case R_386_PLT32:
1685 if (h != NULL)
1687 if (h->plt.refcount > 0)
1688 h->plt.refcount -= 1;
1690 break;
1692 default:
1693 break;
1697 return TRUE;
1700 /* Adjust a symbol defined by a dynamic object and referenced by a
1701 regular object. The current definition is in some section of the
1702 dynamic object, but we're not including those sections. We have to
1703 change the definition to something the rest of the link can
1704 understand. */
1706 static bfd_boolean
1707 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1708 struct elf_link_hash_entry *h)
1710 struct elf_i386_link_hash_table *htab;
1711 asection *s;
1713 /* If this is a function, put it in the procedure linkage table. We
1714 will fill in the contents of the procedure linkage table later,
1715 when we know the address of the .got section. */
1716 if (h->type == STT_FUNC
1717 || h->needs_plt)
1719 if (h->plt.refcount <= 0
1720 || SYMBOL_CALLS_LOCAL (info, h)
1721 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1722 && h->root.type == bfd_link_hash_undefweak))
1724 /* This case can occur if we saw a PLT32 reloc in an input
1725 file, but the symbol was never referred to by a dynamic
1726 object, or if all references were garbage collected. In
1727 such a case, we don't actually need to build a procedure
1728 linkage table, and we can just do a PC32 reloc instead. */
1729 h->plt.offset = (bfd_vma) -1;
1730 h->needs_plt = 0;
1733 return TRUE;
1735 else
1736 /* It's possible that we incorrectly decided a .plt reloc was
1737 needed for an R_386_PC32 reloc to a non-function sym in
1738 check_relocs. We can't decide accurately between function and
1739 non-function syms in check-relocs; Objects loaded later in
1740 the link may change h->type. So fix it now. */
1741 h->plt.offset = (bfd_vma) -1;
1743 /* If this is a weak symbol, and there is a real definition, the
1744 processor independent code will have arranged for us to see the
1745 real definition first, and we can just use the same value. */
1746 if (h->u.weakdef != NULL)
1748 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1749 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1750 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1751 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1752 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1753 h->non_got_ref = h->u.weakdef->non_got_ref;
1754 return TRUE;
1757 /* This is a reference to a symbol defined by a dynamic object which
1758 is not a function. */
1760 /* If we are creating a shared library, we must presume that the
1761 only references to the symbol are via the global offset table.
1762 For such cases we need not do anything here; the relocations will
1763 be handled correctly by relocate_section. */
1764 if (info->shared)
1765 return TRUE;
1767 /* If there are no references to this symbol that do not use the
1768 GOT, we don't need to generate a copy reloc. */
1769 if (!h->non_got_ref)
1770 return TRUE;
1772 /* If -z nocopyreloc was given, we won't generate them either. */
1773 if (info->nocopyreloc)
1775 h->non_got_ref = 0;
1776 return TRUE;
1779 htab = elf_i386_hash_table (info);
1781 /* If there aren't any dynamic relocs in read-only sections, then
1782 we can keep the dynamic relocs and avoid the copy reloc. This
1783 doesn't work on VxWorks, where we can not have dynamic relocations
1784 (other than copy and jump slot relocations) in an executable. */
1785 if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
1787 struct elf_i386_link_hash_entry * eh;
1788 struct elf_i386_dyn_relocs *p;
1790 eh = (struct elf_i386_link_hash_entry *) h;
1791 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1793 s = p->sec->output_section;
1794 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1795 break;
1798 if (p == NULL)
1800 h->non_got_ref = 0;
1801 return TRUE;
1805 if (h->size == 0)
1807 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1808 h->root.root.string);
1809 return TRUE;
1812 /* We must allocate the symbol in our .dynbss section, which will
1813 become part of the .bss section of the executable. There will be
1814 an entry for this symbol in the .dynsym section. The dynamic
1815 object will contain position independent code, so all references
1816 from the dynamic object to this symbol will go through the global
1817 offset table. The dynamic linker will use the .dynsym entry to
1818 determine the address it must put in the global offset table, so
1819 both the dynamic object and the regular object will refer to the
1820 same memory location for the variable. */
1822 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1823 copy the initial value out of the dynamic object and into the
1824 runtime process image. */
1825 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1827 htab->srelbss->size += sizeof (Elf32_External_Rel);
1828 h->needs_copy = 1;
1831 s = htab->sdynbss;
1833 return _bfd_elf_adjust_dynamic_copy (h, s);
1836 /* Allocate space in .plt, .got and associated reloc sections for
1837 dynamic relocs. */
1839 static bfd_boolean
1840 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1842 struct bfd_link_info *info;
1843 struct elf_i386_link_hash_table *htab;
1844 struct elf_i386_link_hash_entry *eh;
1845 struct elf_i386_dyn_relocs *p;
1847 if (h->root.type == bfd_link_hash_indirect)
1848 return TRUE;
1850 if (h->root.type == bfd_link_hash_warning)
1851 /* When warning symbols are created, they **replace** the "real"
1852 entry in the hash table, thus we never get to see the real
1853 symbol in a hash traversal. So look at it now. */
1854 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1856 info = (struct bfd_link_info *) inf;
1857 htab = elf_i386_hash_table (info);
1859 if (htab->elf.dynamic_sections_created
1860 && h->plt.refcount > 0)
1862 /* Make sure this symbol is output as a dynamic symbol.
1863 Undefined weak syms won't yet be marked as dynamic. */
1864 if (h->dynindx == -1
1865 && !h->forced_local)
1867 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1868 return FALSE;
1871 if (info->shared
1872 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1874 asection *s = htab->splt;
1876 /* If this is the first .plt entry, make room for the special
1877 first entry. */
1878 if (s->size == 0)
1879 s->size += PLT_ENTRY_SIZE;
1881 h->plt.offset = s->size;
1883 /* If this symbol is not defined in a regular file, and we are
1884 not generating a shared library, then set the symbol to this
1885 location in the .plt. This is required to make function
1886 pointers compare as equal between the normal executable and
1887 the shared library. */
1888 if (! info->shared
1889 && !h->def_regular)
1891 h->root.u.def.section = s;
1892 h->root.u.def.value = h->plt.offset;
1895 /* Make room for this entry. */
1896 s->size += PLT_ENTRY_SIZE;
1898 /* We also need to make an entry in the .got.plt section, which
1899 will be placed in the .got section by the linker script. */
1900 htab->sgotplt->size += 4;
1902 /* We also need to make an entry in the .rel.plt section. */
1903 htab->srelplt->size += sizeof (Elf32_External_Rel);
1904 htab->next_tls_desc_index++;
1906 if (htab->is_vxworks && !info->shared)
1908 /* VxWorks has a second set of relocations for each PLT entry
1909 in executables. They go in a separate relocation section,
1910 which is processed by the kernel loader. */
1912 /* There are two relocations for the initial PLT entry: an
1913 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
1914 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
1916 if (h->plt.offset == PLT_ENTRY_SIZE)
1917 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1919 /* There are two extra relocations for each subsequent PLT entry:
1920 an R_386_32 relocation for the GOT entry, and an R_386_32
1921 relocation for the PLT entry. */
1923 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1926 else
1928 h->plt.offset = (bfd_vma) -1;
1929 h->needs_plt = 0;
1932 else
1934 h->plt.offset = (bfd_vma) -1;
1935 h->needs_plt = 0;
1938 eh = (struct elf_i386_link_hash_entry *) h;
1939 eh->tlsdesc_got = (bfd_vma) -1;
1941 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
1942 make it a R_386_TLS_LE_32 requiring no TLS entry. */
1943 if (h->got.refcount > 0
1944 && !info->shared
1945 && h->dynindx == -1
1946 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
1947 h->got.offset = (bfd_vma) -1;
1948 else if (h->got.refcount > 0)
1950 asection *s;
1951 bfd_boolean dyn;
1952 int tls_type = elf_i386_hash_entry(h)->tls_type;
1954 /* Make sure this symbol is output as a dynamic symbol.
1955 Undefined weak syms won't yet be marked as dynamic. */
1956 if (h->dynindx == -1
1957 && !h->forced_local)
1959 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1960 return FALSE;
1963 s = htab->sgot;
1964 if (GOT_TLS_GDESC_P (tls_type))
1966 eh->tlsdesc_got = htab->sgotplt->size
1967 - elf_i386_compute_jump_table_size (htab);
1968 htab->sgotplt->size += 8;
1969 h->got.offset = (bfd_vma) -2;
1971 if (! GOT_TLS_GDESC_P (tls_type)
1972 || GOT_TLS_GD_P (tls_type))
1974 h->got.offset = s->size;
1975 s->size += 4;
1976 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
1977 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
1978 s->size += 4;
1980 dyn = htab->elf.dynamic_sections_created;
1981 /* R_386_TLS_IE_32 needs one dynamic relocation,
1982 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1983 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1984 need two), R_386_TLS_GD needs one if local symbol and two if
1985 global. */
1986 if (tls_type == GOT_TLS_IE_BOTH)
1987 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1988 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1989 || (tls_type & GOT_TLS_IE))
1990 htab->srelgot->size += sizeof (Elf32_External_Rel);
1991 else if (GOT_TLS_GD_P (tls_type))
1992 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1993 else if (! GOT_TLS_GDESC_P (tls_type)
1994 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1995 || h->root.type != bfd_link_hash_undefweak)
1996 && (info->shared
1997 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1998 htab->srelgot->size += sizeof (Elf32_External_Rel);
1999 if (GOT_TLS_GDESC_P (tls_type))
2000 htab->srelplt->size += sizeof (Elf32_External_Rel);
2002 else
2003 h->got.offset = (bfd_vma) -1;
2005 if (eh->dyn_relocs == NULL)
2006 return TRUE;
2008 /* In the shared -Bsymbolic case, discard space allocated for
2009 dynamic pc-relative relocs against symbols which turn out to be
2010 defined in regular objects. For the normal shared case, discard
2011 space for pc-relative relocs that have become local due to symbol
2012 visibility changes. */
2014 if (info->shared)
2016 /* The only reloc that uses pc_count is R_386_PC32, which will
2017 appear on a call or on something like ".long foo - .". We
2018 want calls to protected symbols to resolve directly to the
2019 function rather than going via the plt. If people want
2020 function pointer comparisons to work as expected then they
2021 should avoid writing assembly like ".long foo - .". */
2022 if (SYMBOL_CALLS_LOCAL (info, h))
2024 struct elf_i386_dyn_relocs **pp;
2026 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2028 p->count -= p->pc_count;
2029 p->pc_count = 0;
2030 if (p->count == 0)
2031 *pp = p->next;
2032 else
2033 pp = &p->next;
2037 if (htab->is_vxworks)
2039 struct elf_i386_dyn_relocs **pp;
2040 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2042 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2043 *pp = p->next;
2044 else
2045 pp = &p->next;
2049 /* Also discard relocs on undefined weak syms with non-default
2050 visibility. */
2051 if (eh->dyn_relocs != NULL
2052 && h->root.type == bfd_link_hash_undefweak)
2054 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2055 eh->dyn_relocs = NULL;
2057 /* Make sure undefined weak symbols are output as a dynamic
2058 symbol in PIEs. */
2059 else if (h->dynindx == -1
2060 && !h->forced_local)
2062 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2063 return FALSE;
2067 else if (ELIMINATE_COPY_RELOCS)
2069 /* For the non-shared case, discard space for relocs against
2070 symbols which turn out to need copy relocs or are not
2071 dynamic. */
2073 if (!h->non_got_ref
2074 && ((h->def_dynamic
2075 && !h->def_regular)
2076 || (htab->elf.dynamic_sections_created
2077 && (h->root.type == bfd_link_hash_undefweak
2078 || h->root.type == bfd_link_hash_undefined))))
2080 /* Make sure this symbol is output as a dynamic symbol.
2081 Undefined weak syms won't yet be marked as dynamic. */
2082 if (h->dynindx == -1
2083 && !h->forced_local)
2085 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2086 return FALSE;
2089 /* If that succeeded, we know we'll be keeping all the
2090 relocs. */
2091 if (h->dynindx != -1)
2092 goto keep;
2095 eh->dyn_relocs = NULL;
2097 keep: ;
2100 /* Finally, allocate space. */
2101 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2103 asection *sreloc = elf_section_data (p->sec)->sreloc;
2104 sreloc->size += p->count * sizeof (Elf32_External_Rel);
2107 return TRUE;
2110 /* Find any dynamic relocs that apply to read-only sections. */
2112 static bfd_boolean
2113 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2115 struct elf_i386_link_hash_entry *eh;
2116 struct elf_i386_dyn_relocs *p;
2118 if (h->root.type == bfd_link_hash_warning)
2119 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2121 eh = (struct elf_i386_link_hash_entry *) h;
2122 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2124 asection *s = p->sec->output_section;
2126 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2128 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2130 info->flags |= DF_TEXTREL;
2132 /* Not an error, just cut short the traversal. */
2133 return FALSE;
2136 return TRUE;
2139 /* Set the sizes of the dynamic sections. */
2141 static bfd_boolean
2142 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2143 struct bfd_link_info *info)
2145 struct elf_i386_link_hash_table *htab;
2146 bfd *dynobj;
2147 asection *s;
2148 bfd_boolean relocs;
2149 bfd *ibfd;
2151 htab = elf_i386_hash_table (info);
2152 dynobj = htab->elf.dynobj;
2153 if (dynobj == NULL)
2154 abort ();
2156 if (htab->elf.dynamic_sections_created)
2158 /* Set the contents of the .interp section to the interpreter. */
2159 if (info->executable)
2161 s = bfd_get_section_by_name (dynobj, ".interp");
2162 if (s == NULL)
2163 abort ();
2164 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2165 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2169 /* Set up .got offsets for local syms, and space for local dynamic
2170 relocs. */
2171 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2173 bfd_signed_vma *local_got;
2174 bfd_signed_vma *end_local_got;
2175 char *local_tls_type;
2176 bfd_vma *local_tlsdesc_gotent;
2177 bfd_size_type locsymcount;
2178 Elf_Internal_Shdr *symtab_hdr;
2179 asection *srel;
2181 if (! is_i386_elf (ibfd))
2182 continue;
2184 for (s = ibfd->sections; s != NULL; s = s->next)
2186 struct elf_i386_dyn_relocs *p;
2188 for (p = ((struct elf_i386_dyn_relocs *)
2189 elf_section_data (s)->local_dynrel);
2190 p != NULL;
2191 p = p->next)
2193 if (!bfd_is_abs_section (p->sec)
2194 && bfd_is_abs_section (p->sec->output_section))
2196 /* Input section has been discarded, either because
2197 it is a copy of a linkonce section or due to
2198 linker script /DISCARD/, so we'll be discarding
2199 the relocs too. */
2201 else if (htab->is_vxworks
2202 && strcmp (p->sec->output_section->name,
2203 ".tls_vars") == 0)
2205 /* Relocations in vxworks .tls_vars sections are
2206 handled specially by the loader. */
2208 else if (p->count != 0)
2210 srel = elf_section_data (p->sec)->sreloc;
2211 srel->size += p->count * sizeof (Elf32_External_Rel);
2212 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2213 info->flags |= DF_TEXTREL;
2218 local_got = elf_local_got_refcounts (ibfd);
2219 if (!local_got)
2220 continue;
2222 symtab_hdr = &elf_symtab_hdr (ibfd);
2223 locsymcount = symtab_hdr->sh_info;
2224 end_local_got = local_got + locsymcount;
2225 local_tls_type = elf_i386_local_got_tls_type (ibfd);
2226 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
2227 s = htab->sgot;
2228 srel = htab->srelgot;
2229 for (; local_got < end_local_got;
2230 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2232 *local_tlsdesc_gotent = (bfd_vma) -1;
2233 if (*local_got > 0)
2235 if (GOT_TLS_GDESC_P (*local_tls_type))
2237 *local_tlsdesc_gotent = htab->sgotplt->size
2238 - elf_i386_compute_jump_table_size (htab);
2239 htab->sgotplt->size += 8;
2240 *local_got = (bfd_vma) -2;
2242 if (! GOT_TLS_GDESC_P (*local_tls_type)
2243 || GOT_TLS_GD_P (*local_tls_type))
2245 *local_got = s->size;
2246 s->size += 4;
2247 if (GOT_TLS_GD_P (*local_tls_type)
2248 || *local_tls_type == GOT_TLS_IE_BOTH)
2249 s->size += 4;
2251 if (info->shared
2252 || GOT_TLS_GD_ANY_P (*local_tls_type)
2253 || (*local_tls_type & GOT_TLS_IE))
2255 if (*local_tls_type == GOT_TLS_IE_BOTH)
2256 srel->size += 2 * sizeof (Elf32_External_Rel);
2257 else if (GOT_TLS_GD_P (*local_tls_type)
2258 || ! GOT_TLS_GDESC_P (*local_tls_type))
2259 srel->size += sizeof (Elf32_External_Rel);
2260 if (GOT_TLS_GDESC_P (*local_tls_type))
2261 htab->srelplt->size += sizeof (Elf32_External_Rel);
2264 else
2265 *local_got = (bfd_vma) -1;
2269 if (htab->tls_ldm_got.refcount > 0)
2271 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2272 relocs. */
2273 htab->tls_ldm_got.offset = htab->sgot->size;
2274 htab->sgot->size += 8;
2275 htab->srelgot->size += sizeof (Elf32_External_Rel);
2277 else
2278 htab->tls_ldm_got.offset = -1;
2280 /* Allocate global sym .plt and .got entries, and space for global
2281 sym dynamic relocs. */
2282 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
2284 /* For every jump slot reserved in the sgotplt, reloc_count is
2285 incremented. However, when we reserve space for TLS descriptors,
2286 it's not incremented, so in order to compute the space reserved
2287 for them, it suffices to multiply the reloc count by the jump
2288 slot size. */
2289 if (htab->srelplt)
2290 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
2292 /* We now have determined the sizes of the various dynamic sections.
2293 Allocate memory for them. */
2294 relocs = FALSE;
2295 for (s = dynobj->sections; s != NULL; s = s->next)
2297 bfd_boolean strip_section = TRUE;
2299 if ((s->flags & SEC_LINKER_CREATED) == 0)
2300 continue;
2302 if (s == htab->splt
2303 || s == htab->sgot
2304 || s == htab->sgotplt
2305 || s == htab->sdynbss)
2307 /* Strip this section if we don't need it; see the
2308 comment below. */
2309 /* We'd like to strip these sections if they aren't needed, but if
2310 we've exported dynamic symbols from them we must leave them.
2311 It's too late to tell BFD to get rid of the symbols. */
2313 if (htab->elf.hplt != NULL)
2314 strip_section = FALSE;
2316 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
2318 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
2319 relocs = TRUE;
2321 /* We use the reloc_count field as a counter if we need
2322 to copy relocs into the output file. */
2323 s->reloc_count = 0;
2325 else
2327 /* It's not one of our sections, so don't allocate space. */
2328 continue;
2331 if (s->size == 0)
2333 /* If we don't need this section, strip it from the
2334 output file. This is mostly to handle .rel.bss and
2335 .rel.plt. We must create both sections in
2336 create_dynamic_sections, because they must be created
2337 before the linker maps input sections to output
2338 sections. The linker does that before
2339 adjust_dynamic_symbol is called, and it is that
2340 function which decides whether anything needs to go
2341 into these sections. */
2342 if (strip_section)
2343 s->flags |= SEC_EXCLUDE;
2344 continue;
2347 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2348 continue;
2350 /* Allocate memory for the section contents. We use bfd_zalloc
2351 here in case unused entries are not reclaimed before the
2352 section's contents are written out. This should not happen,
2353 but this way if it does, we get a R_386_NONE reloc instead
2354 of garbage. */
2355 s->contents = bfd_zalloc (dynobj, s->size);
2356 if (s->contents == NULL)
2357 return FALSE;
2360 if (htab->elf.dynamic_sections_created)
2362 /* Add some entries to the .dynamic section. We fill in the
2363 values later, in elf_i386_finish_dynamic_sections, but we
2364 must add the entries now so that we get the correct size for
2365 the .dynamic section. The DT_DEBUG entry is filled in by the
2366 dynamic linker and used by the debugger. */
2367 #define add_dynamic_entry(TAG, VAL) \
2368 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2370 if (info->executable)
2372 if (!add_dynamic_entry (DT_DEBUG, 0))
2373 return FALSE;
2376 if (htab->splt->size != 0)
2378 if (!add_dynamic_entry (DT_PLTGOT, 0)
2379 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2380 || !add_dynamic_entry (DT_PLTREL, DT_REL)
2381 || !add_dynamic_entry (DT_JMPREL, 0))
2382 return FALSE;
2385 if (relocs)
2387 if (!add_dynamic_entry (DT_REL, 0)
2388 || !add_dynamic_entry (DT_RELSZ, 0)
2389 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2390 return FALSE;
2392 /* If any dynamic relocs apply to a read-only section,
2393 then we need a DT_TEXTREL entry. */
2394 if ((info->flags & DF_TEXTREL) == 0)
2395 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2396 (PTR) info);
2398 if ((info->flags & DF_TEXTREL) != 0)
2400 if (!add_dynamic_entry (DT_TEXTREL, 0))
2401 return FALSE;
2404 if (htab->is_vxworks
2405 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
2406 return FALSE;
2408 #undef add_dynamic_entry
2410 return TRUE;
2413 static bfd_boolean
2414 elf_i386_always_size_sections (bfd *output_bfd,
2415 struct bfd_link_info *info)
2417 asection *tls_sec = elf_hash_table (info)->tls_sec;
2419 if (tls_sec)
2421 struct elf_link_hash_entry *tlsbase;
2423 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2424 "_TLS_MODULE_BASE_",
2425 FALSE, FALSE, FALSE);
2427 if (tlsbase && tlsbase->type == STT_TLS)
2429 struct bfd_link_hash_entry *bh = NULL;
2430 const struct elf_backend_data *bed
2431 = get_elf_backend_data (output_bfd);
2433 if (!(_bfd_generic_link_add_one_symbol
2434 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2435 tls_sec, 0, NULL, FALSE,
2436 bed->collect, &bh)))
2437 return FALSE;
2439 elf_i386_hash_table (info)->tls_module_base = bh;
2441 tlsbase = (struct elf_link_hash_entry *)bh;
2442 tlsbase->def_regular = 1;
2443 tlsbase->other = STV_HIDDEN;
2444 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2448 return TRUE;
2451 /* Set the correct type for an x86 ELF section. We do this by the
2452 section name, which is a hack, but ought to work. */
2454 static bfd_boolean
2455 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2456 Elf_Internal_Shdr *hdr,
2457 asection *sec)
2459 register const char *name;
2461 name = bfd_get_section_name (abfd, sec);
2463 /* This is an ugly, but unfortunately necessary hack that is
2464 needed when producing EFI binaries on x86. It tells
2465 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2466 containing ELF relocation info. We need this hack in order to
2467 be able to generate ELF binaries that can be translated into
2468 EFI applications (which are essentially COFF objects). Those
2469 files contain a COFF ".reloc" section inside an ELFNN object,
2470 which would normally cause BFD to segfault because it would
2471 attempt to interpret this section as containing relocation
2472 entries for section "oc". With this hack enabled, ".reloc"
2473 will be treated as a normal data section, which will avoid the
2474 segfault. However, you won't be able to create an ELFNN binary
2475 with a section named "oc" that needs relocations, but that's
2476 the kind of ugly side-effects you get when detecting section
2477 types based on their names... In practice, this limitation is
2478 unlikely to bite. */
2479 if (strcmp (name, ".reloc") == 0)
2480 hdr->sh_type = SHT_PROGBITS;
2482 return TRUE;
2485 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2486 executables. Rather than setting it to the beginning of the TLS
2487 section, we have to set it to the end. This function may be called
2488 multiple times, it is idempotent. */
2490 static void
2491 set_tls_module_base (struct bfd_link_info *info)
2493 struct bfd_link_hash_entry *base;
2495 if (!info->executable)
2496 return;
2498 base = elf_i386_hash_table (info)->tls_module_base;
2500 if (!base)
2501 return;
2503 base->u.def.value = elf_hash_table (info)->tls_size;
2506 /* Return the base VMA address which should be subtracted from real addresses
2507 when resolving @dtpoff relocation.
2508 This is PT_TLS segment p_vaddr. */
2510 static bfd_vma
2511 dtpoff_base (struct bfd_link_info *info)
2513 /* If tls_sec is NULL, we should have signalled an error already. */
2514 if (elf_hash_table (info)->tls_sec == NULL)
2515 return 0;
2516 return elf_hash_table (info)->tls_sec->vma;
2519 /* Return the relocation value for @tpoff relocation
2520 if STT_TLS virtual address is ADDRESS. */
2522 static bfd_vma
2523 tpoff (struct bfd_link_info *info, bfd_vma address)
2525 struct elf_link_hash_table *htab = elf_hash_table (info);
2527 /* If tls_sec is NULL, we should have signalled an error already. */
2528 if (htab->tls_sec == NULL)
2529 return 0;
2530 return htab->tls_size + htab->tls_sec->vma - address;
2533 /* Relocate an i386 ELF section. */
2535 static bfd_boolean
2536 elf_i386_relocate_section (bfd *output_bfd,
2537 struct bfd_link_info *info,
2538 bfd *input_bfd,
2539 asection *input_section,
2540 bfd_byte *contents,
2541 Elf_Internal_Rela *relocs,
2542 Elf_Internal_Sym *local_syms,
2543 asection **local_sections)
2545 struct elf_i386_link_hash_table *htab;
2546 Elf_Internal_Shdr *symtab_hdr;
2547 struct elf_link_hash_entry **sym_hashes;
2548 bfd_vma *local_got_offsets;
2549 bfd_vma *local_tlsdesc_gotents;
2550 Elf_Internal_Rela *rel;
2551 Elf_Internal_Rela *relend;
2552 bfd_boolean is_vxworks_tls;
2554 BFD_ASSERT (is_i386_elf (input_bfd));
2556 htab = elf_i386_hash_table (info);
2557 symtab_hdr = &elf_symtab_hdr (input_bfd);
2558 sym_hashes = elf_sym_hashes (input_bfd);
2559 local_got_offsets = elf_local_got_offsets (input_bfd);
2560 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
2561 /* We have to handle relocations in vxworks .tls_vars sections
2562 specially, because the dynamic loader is 'weird'. */
2563 is_vxworks_tls = (htab->is_vxworks && info->shared
2564 && !strcmp (input_section->output_section->name,
2565 ".tls_vars"));
2567 set_tls_module_base (info);
2569 rel = relocs;
2570 relend = relocs + input_section->reloc_count;
2571 for (; rel < relend; rel++)
2573 unsigned int r_type;
2574 reloc_howto_type *howto;
2575 unsigned long r_symndx;
2576 struct elf_link_hash_entry *h;
2577 Elf_Internal_Sym *sym;
2578 asection *sec;
2579 bfd_vma off, offplt;
2580 bfd_vma relocation;
2581 bfd_boolean unresolved_reloc;
2582 bfd_reloc_status_type r;
2583 unsigned int indx;
2584 int tls_type;
2586 r_type = ELF32_R_TYPE (rel->r_info);
2587 if (r_type == R_386_GNU_VTINHERIT
2588 || r_type == R_386_GNU_VTENTRY)
2589 continue;
2591 if ((indx = r_type) >= R_386_standard
2592 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2593 >= R_386_ext - R_386_standard)
2594 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2595 >= R_386_tls - R_386_ext))
2597 (*_bfd_error_handler)
2598 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2599 input_bfd, input_section, r_type);
2600 bfd_set_error (bfd_error_bad_value);
2601 return FALSE;
2603 howto = elf_howto_table + indx;
2605 r_symndx = ELF32_R_SYM (rel->r_info);
2606 h = NULL;
2607 sym = NULL;
2608 sec = NULL;
2609 unresolved_reloc = FALSE;
2610 if (r_symndx < symtab_hdr->sh_info)
2612 sym = local_syms + r_symndx;
2613 sec = local_sections[r_symndx];
2614 relocation = (sec->output_section->vma
2615 + sec->output_offset
2616 + sym->st_value);
2618 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2619 && ((sec->flags & SEC_MERGE) != 0
2620 || (info->relocatable
2621 && sec->output_offset != 0)))
2623 bfd_vma addend;
2624 bfd_byte *where = contents + rel->r_offset;
2626 switch (howto->size)
2628 case 0:
2629 addend = bfd_get_8 (input_bfd, where);
2630 if (howto->pc_relative)
2632 addend = (addend ^ 0x80) - 0x80;
2633 addend += 1;
2635 break;
2636 case 1:
2637 addend = bfd_get_16 (input_bfd, where);
2638 if (howto->pc_relative)
2640 addend = (addend ^ 0x8000) - 0x8000;
2641 addend += 2;
2643 break;
2644 case 2:
2645 addend = bfd_get_32 (input_bfd, where);
2646 if (howto->pc_relative)
2648 addend = (addend ^ 0x80000000) - 0x80000000;
2649 addend += 4;
2651 break;
2652 default:
2653 abort ();
2656 if (info->relocatable)
2657 addend += sec->output_offset;
2658 else
2660 asection *msec = sec;
2661 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2662 addend);
2663 addend -= relocation;
2664 addend += msec->output_section->vma + msec->output_offset;
2667 switch (howto->size)
2669 case 0:
2670 /* FIXME: overflow checks. */
2671 if (howto->pc_relative)
2672 addend -= 1;
2673 bfd_put_8 (input_bfd, addend, where);
2674 break;
2675 case 1:
2676 if (howto->pc_relative)
2677 addend -= 2;
2678 bfd_put_16 (input_bfd, addend, where);
2679 break;
2680 case 2:
2681 if (howto->pc_relative)
2682 addend -= 4;
2683 bfd_put_32 (input_bfd, addend, where);
2684 break;
2688 else
2690 bfd_boolean warned;
2692 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2693 r_symndx, symtab_hdr, sym_hashes,
2694 h, sec, relocation,
2695 unresolved_reloc, warned);
2698 if (sec != NULL && elf_discarded_section (sec))
2700 /* For relocs against symbols from removed linkonce sections,
2701 or sections discarded by a linker script, we just want the
2702 section contents zeroed. Avoid any special processing. */
2703 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2704 rel->r_info = 0;
2705 rel->r_addend = 0;
2706 continue;
2709 if (info->relocatable)
2710 continue;
2712 switch (r_type)
2714 case R_386_GOT32:
2715 /* Relocation is to the entry for this symbol in the global
2716 offset table. */
2717 if (htab->sgot == NULL)
2718 abort ();
2720 if (h != NULL)
2722 bfd_boolean dyn;
2724 off = h->got.offset;
2725 dyn = htab->elf.dynamic_sections_created;
2726 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2727 || (info->shared
2728 && SYMBOL_REFERENCES_LOCAL (info, h))
2729 || (ELF_ST_VISIBILITY (h->other)
2730 && h->root.type == bfd_link_hash_undefweak))
2732 /* This is actually a static link, or it is a
2733 -Bsymbolic link and the symbol is defined
2734 locally, or the symbol was forced to be local
2735 because of a version file. We must initialize
2736 this entry in the global offset table. Since the
2737 offset must always be a multiple of 4, we use the
2738 least significant bit to record whether we have
2739 initialized it already.
2741 When doing a dynamic link, we create a .rel.got
2742 relocation entry to initialize the value. This
2743 is done in the finish_dynamic_symbol routine. */
2744 if ((off & 1) != 0)
2745 off &= ~1;
2746 else
2748 bfd_put_32 (output_bfd, relocation,
2749 htab->sgot->contents + off);
2750 h->got.offset |= 1;
2753 else
2754 unresolved_reloc = FALSE;
2756 else
2758 if (local_got_offsets == NULL)
2759 abort ();
2761 off = local_got_offsets[r_symndx];
2763 /* The offset must always be a multiple of 4. We use
2764 the least significant bit to record whether we have
2765 already generated the necessary reloc. */
2766 if ((off & 1) != 0)
2767 off &= ~1;
2768 else
2770 bfd_put_32 (output_bfd, relocation,
2771 htab->sgot->contents + off);
2773 if (info->shared)
2775 asection *s;
2776 Elf_Internal_Rela outrel;
2777 bfd_byte *loc;
2779 s = htab->srelgot;
2780 if (s == NULL)
2781 abort ();
2783 outrel.r_offset = (htab->sgot->output_section->vma
2784 + htab->sgot->output_offset
2785 + off);
2786 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2787 loc = s->contents;
2788 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
2789 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2792 local_got_offsets[r_symndx] |= 1;
2796 if (off >= (bfd_vma) -2)
2797 abort ();
2799 relocation = htab->sgot->output_section->vma
2800 + htab->sgot->output_offset + off
2801 - htab->sgotplt->output_section->vma
2802 - htab->sgotplt->output_offset;
2803 break;
2805 case R_386_GOTOFF:
2806 /* Relocation is relative to the start of the global offset
2807 table. */
2809 /* Check to make sure it isn't a protected function symbol
2810 for shared library since it may not be local when used
2811 as function address. We also need to make sure that a
2812 symbol is defined locally. */
2813 if (info->shared && h)
2815 if (!h->def_regular)
2817 const char *v;
2819 switch (ELF_ST_VISIBILITY (h->other))
2821 case STV_HIDDEN:
2822 v = _("hidden symbol");
2823 break;
2824 case STV_INTERNAL:
2825 v = _("internal symbol");
2826 break;
2827 case STV_PROTECTED:
2828 v = _("protected symbol");
2829 break;
2830 default:
2831 v = _("symbol");
2832 break;
2835 (*_bfd_error_handler)
2836 (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
2837 input_bfd, v, h->root.root.string);
2838 bfd_set_error (bfd_error_bad_value);
2839 return FALSE;
2841 else if (!info->executable
2842 && h->type == STT_FUNC
2843 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2845 (*_bfd_error_handler)
2846 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
2847 input_bfd, h->root.root.string);
2848 bfd_set_error (bfd_error_bad_value);
2849 return FALSE;
2853 /* Note that sgot is not involved in this
2854 calculation. We always want the start of .got.plt. If we
2855 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2856 permitted by the ABI, we might have to change this
2857 calculation. */
2858 relocation -= htab->sgotplt->output_section->vma
2859 + htab->sgotplt->output_offset;
2860 break;
2862 case R_386_GOTPC:
2863 /* Use global offset table as symbol value. */
2864 relocation = htab->sgotplt->output_section->vma
2865 + htab->sgotplt->output_offset;
2866 unresolved_reloc = FALSE;
2867 break;
2869 case R_386_PLT32:
2870 /* Relocation is to the entry for this symbol in the
2871 procedure linkage table. */
2873 /* Resolve a PLT32 reloc against a local symbol directly,
2874 without using the procedure linkage table. */
2875 if (h == NULL)
2876 break;
2878 if (h->plt.offset == (bfd_vma) -1
2879 || htab->splt == NULL)
2881 /* We didn't make a PLT entry for this symbol. This
2882 happens when statically linking PIC code, or when
2883 using -Bsymbolic. */
2884 break;
2887 relocation = (htab->splt->output_section->vma
2888 + htab->splt->output_offset
2889 + h->plt.offset);
2890 unresolved_reloc = FALSE;
2891 break;
2893 case R_386_32:
2894 case R_386_PC32:
2895 if ((input_section->flags & SEC_ALLOC) == 0
2896 || is_vxworks_tls)
2897 break;
2899 if ((info->shared
2900 && (h == NULL
2901 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2902 || h->root.type != bfd_link_hash_undefweak)
2903 && (r_type != R_386_PC32
2904 || !SYMBOL_CALLS_LOCAL (info, h)))
2905 || (ELIMINATE_COPY_RELOCS
2906 && !info->shared
2907 && h != NULL
2908 && h->dynindx != -1
2909 && !h->non_got_ref
2910 && ((h->def_dynamic
2911 && !h->def_regular)
2912 || h->root.type == bfd_link_hash_undefweak
2913 || h->root.type == bfd_link_hash_undefined)))
2915 Elf_Internal_Rela outrel;
2916 bfd_byte *loc;
2917 bfd_boolean skip, relocate;
2918 asection *sreloc;
2920 /* When generating a shared object, these relocations
2921 are copied into the output file to be resolved at run
2922 time. */
2924 skip = FALSE;
2925 relocate = FALSE;
2927 outrel.r_offset =
2928 _bfd_elf_section_offset (output_bfd, info, input_section,
2929 rel->r_offset);
2930 if (outrel.r_offset == (bfd_vma) -1)
2931 skip = TRUE;
2932 else if (outrel.r_offset == (bfd_vma) -2)
2933 skip = TRUE, relocate = TRUE;
2934 outrel.r_offset += (input_section->output_section->vma
2935 + input_section->output_offset);
2937 if (skip)
2938 memset (&outrel, 0, sizeof outrel);
2939 else if (h != NULL
2940 && h->dynindx != -1
2941 && (r_type == R_386_PC32
2942 || !info->shared
2943 || !SYMBOLIC_BIND (info, h)
2944 || !h->def_regular))
2945 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2946 else
2948 /* This symbol is local, or marked to become local. */
2949 relocate = TRUE;
2950 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2953 sreloc = elf_section_data (input_section)->sreloc;
2954 if (sreloc == NULL)
2955 abort ();
2957 loc = sreloc->contents;
2958 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2959 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2961 /* If this reloc is against an external symbol, we do
2962 not want to fiddle with the addend. Otherwise, we
2963 need to include the symbol value so that it becomes
2964 an addend for the dynamic reloc. */
2965 if (! relocate)
2966 continue;
2968 break;
2970 case R_386_TLS_IE:
2971 if (info->shared)
2973 Elf_Internal_Rela outrel;
2974 bfd_byte *loc;
2975 asection *sreloc;
2977 outrel.r_offset = rel->r_offset
2978 + input_section->output_section->vma
2979 + input_section->output_offset;
2980 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2981 sreloc = elf_section_data (input_section)->sreloc;
2982 if (sreloc == NULL)
2983 abort ();
2984 loc = sreloc->contents;
2985 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2986 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2988 /* Fall through */
2990 case R_386_TLS_GD:
2991 case R_386_TLS_GOTDESC:
2992 case R_386_TLS_DESC_CALL:
2993 case R_386_TLS_IE_32:
2994 case R_386_TLS_GOTIE:
2995 tls_type = GOT_UNKNOWN;
2996 if (h == NULL && local_got_offsets)
2997 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2998 else if (h != NULL)
2999 tls_type = elf_i386_hash_entry(h)->tls_type;
3000 if (tls_type == GOT_TLS_IE)
3001 tls_type = GOT_TLS_IE_NEG;
3003 if (! elf_i386_tls_transition (info, input_bfd,
3004 input_section, contents,
3005 symtab_hdr, sym_hashes,
3006 &r_type, tls_type, rel,
3007 relend, h))
3008 return FALSE;
3010 if (r_type == R_386_TLS_LE_32)
3012 BFD_ASSERT (! unresolved_reloc);
3013 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3015 unsigned int type;
3016 bfd_vma roff;
3018 /* GD->LE transition. */
3019 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3020 if (type == 0x04)
3022 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3023 Change it into:
3024 movl %gs:0, %eax; subl $foo@tpoff, %eax
3025 (6 byte form of subl). */
3026 memcpy (contents + rel->r_offset - 3,
3027 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3028 roff = rel->r_offset + 5;
3030 else
3032 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3033 Change it into:
3034 movl %gs:0, %eax; subl $foo@tpoff, %eax
3035 (6 byte form of subl). */
3036 memcpy (contents + rel->r_offset - 2,
3037 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3038 roff = rel->r_offset + 6;
3040 bfd_put_32 (output_bfd, tpoff (info, relocation),
3041 contents + roff);
3042 /* Skip R_386_PC32/R_386_PLT32. */
3043 rel++;
3044 continue;
3046 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3048 /* GDesc -> LE transition.
3049 It's originally something like:
3050 leal x@tlsdesc(%ebx), %eax
3052 leal x@ntpoff, %eax
3054 Registers other than %eax may be set up here. */
3056 unsigned int val;
3057 bfd_vma roff;
3059 roff = rel->r_offset;
3060 val = bfd_get_8 (input_bfd, contents + roff - 1);
3062 /* Now modify the instruction as appropriate. */
3063 /* aoliva FIXME: remove the above and xor the byte
3064 below with 0x86. */
3065 bfd_put_8 (output_bfd, val ^ 0x86,
3066 contents + roff - 1);
3067 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3068 contents + roff);
3069 continue;
3071 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3073 /* GDesc -> LE transition.
3074 It's originally:
3075 call *(%eax)
3076 Turn it into:
3077 xchg %ax,%ax */
3079 bfd_vma roff;
3081 roff = rel->r_offset;
3082 bfd_put_8 (output_bfd, 0x66, contents + roff);
3083 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3084 continue;
3086 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
3088 unsigned int val;
3090 /* IE->LE transition:
3091 Originally it can be one of:
3092 movl foo, %eax
3093 movl foo, %reg
3094 addl foo, %reg
3095 We change it into:
3096 movl $foo, %eax
3097 movl $foo, %reg
3098 addl $foo, %reg. */
3099 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3100 if (val == 0xa1)
3102 /* movl foo, %eax. */
3103 bfd_put_8 (output_bfd, 0xb8,
3104 contents + rel->r_offset - 1);
3106 else
3108 unsigned int type;
3110 type = bfd_get_8 (input_bfd,
3111 contents + rel->r_offset - 2);
3112 switch (type)
3114 case 0x8b:
3115 /* movl */
3116 bfd_put_8 (output_bfd, 0xc7,
3117 contents + rel->r_offset - 2);
3118 bfd_put_8 (output_bfd,
3119 0xc0 | ((val >> 3) & 7),
3120 contents + rel->r_offset - 1);
3121 break;
3122 case 0x03:
3123 /* addl */
3124 bfd_put_8 (output_bfd, 0x81,
3125 contents + rel->r_offset - 2);
3126 bfd_put_8 (output_bfd,
3127 0xc0 | ((val >> 3) & 7),
3128 contents + rel->r_offset - 1);
3129 break;
3130 default:
3131 BFD_FAIL ();
3132 break;
3135 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3136 contents + rel->r_offset);
3137 continue;
3139 else
3141 unsigned int val, type;
3143 /* {IE_32,GOTIE}->LE transition:
3144 Originally it can be one of:
3145 subl foo(%reg1), %reg2
3146 movl foo(%reg1), %reg2
3147 addl foo(%reg1), %reg2
3148 We change it into:
3149 subl $foo, %reg2
3150 movl $foo, %reg2 (6 byte form)
3151 addl $foo, %reg2. */
3152 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3153 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3154 if (type == 0x8b)
3156 /* movl */
3157 bfd_put_8 (output_bfd, 0xc7,
3158 contents + rel->r_offset - 2);
3159 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3160 contents + rel->r_offset - 1);
3162 else if (type == 0x2b)
3164 /* subl */
3165 bfd_put_8 (output_bfd, 0x81,
3166 contents + rel->r_offset - 2);
3167 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3168 contents + rel->r_offset - 1);
3170 else if (type == 0x03)
3172 /* addl */
3173 bfd_put_8 (output_bfd, 0x81,
3174 contents + rel->r_offset - 2);
3175 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3176 contents + rel->r_offset - 1);
3178 else
3179 BFD_FAIL ();
3180 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
3181 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3182 contents + rel->r_offset);
3183 else
3184 bfd_put_32 (output_bfd, tpoff (info, relocation),
3185 contents + rel->r_offset);
3186 continue;
3190 if (htab->sgot == NULL)
3191 abort ();
3193 if (h != NULL)
3195 off = h->got.offset;
3196 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
3198 else
3200 if (local_got_offsets == NULL)
3201 abort ();
3203 off = local_got_offsets[r_symndx];
3204 offplt = local_tlsdesc_gotents[r_symndx];
3207 if ((off & 1) != 0)
3208 off &= ~1;
3209 else
3211 Elf_Internal_Rela outrel;
3212 bfd_byte *loc;
3213 int dr_type, indx;
3214 asection *sreloc;
3216 if (htab->srelgot == NULL)
3217 abort ();
3219 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3221 if (GOT_TLS_GDESC_P (tls_type))
3223 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3224 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3225 <= htab->sgotplt->size);
3226 outrel.r_offset = (htab->sgotplt->output_section->vma
3227 + htab->sgotplt->output_offset
3228 + offplt
3229 + htab->sgotplt_jump_table_size);
3230 sreloc = htab->srelplt;
3231 loc = sreloc->contents;
3232 loc += (htab->next_tls_desc_index++
3233 * sizeof (Elf32_External_Rel));
3234 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3235 <= sreloc->contents + sreloc->size);
3236 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3237 if (indx == 0)
3239 BFD_ASSERT (! unresolved_reloc);
3240 bfd_put_32 (output_bfd,
3241 relocation - dtpoff_base (info),
3242 htab->sgotplt->contents + offplt
3243 + htab->sgotplt_jump_table_size + 4);
3245 else
3247 bfd_put_32 (output_bfd, 0,
3248 htab->sgotplt->contents + offplt
3249 + htab->sgotplt_jump_table_size + 4);
3253 sreloc = htab->srelgot;
3255 outrel.r_offset = (htab->sgot->output_section->vma
3256 + htab->sgot->output_offset + off);
3258 if (GOT_TLS_GD_P (tls_type))
3259 dr_type = R_386_TLS_DTPMOD32;
3260 else if (GOT_TLS_GDESC_P (tls_type))
3261 goto dr_done;
3262 else if (tls_type == GOT_TLS_IE_POS)
3263 dr_type = R_386_TLS_TPOFF;
3264 else
3265 dr_type = R_386_TLS_TPOFF32;
3267 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3268 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3269 htab->sgot->contents + off);
3270 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3271 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
3272 htab->sgot->contents + off);
3273 else if (dr_type != R_386_TLS_DESC)
3274 bfd_put_32 (output_bfd, 0,
3275 htab->sgot->contents + off);
3276 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3278 loc = sreloc->contents;
3279 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3280 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3281 <= sreloc->contents + sreloc->size);
3282 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3284 if (GOT_TLS_GD_P (tls_type))
3286 if (indx == 0)
3288 BFD_ASSERT (! unresolved_reloc);
3289 bfd_put_32 (output_bfd,
3290 relocation - dtpoff_base (info),
3291 htab->sgot->contents + off + 4);
3293 else
3295 bfd_put_32 (output_bfd, 0,
3296 htab->sgot->contents + off + 4);
3297 outrel.r_info = ELF32_R_INFO (indx,
3298 R_386_TLS_DTPOFF32);
3299 outrel.r_offset += 4;
3300 sreloc->reloc_count++;
3301 loc += sizeof (Elf32_External_Rel);
3302 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3303 <= sreloc->contents + sreloc->size);
3304 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3307 else if (tls_type == GOT_TLS_IE_BOTH)
3309 bfd_put_32 (output_bfd,
3310 indx == 0 ? relocation - dtpoff_base (info) : 0,
3311 htab->sgot->contents + off + 4);
3312 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3313 outrel.r_offset += 4;
3314 sreloc->reloc_count++;
3315 loc += sizeof (Elf32_External_Rel);
3316 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3319 dr_done:
3320 if (h != NULL)
3321 h->got.offset |= 1;
3322 else
3323 local_got_offsets[r_symndx] |= 1;
3326 if (off >= (bfd_vma) -2
3327 && ! GOT_TLS_GDESC_P (tls_type))
3328 abort ();
3329 if (r_type == R_386_TLS_GOTDESC
3330 || r_type == R_386_TLS_DESC_CALL)
3332 relocation = htab->sgotplt_jump_table_size + offplt;
3333 unresolved_reloc = FALSE;
3335 else if (r_type == ELF32_R_TYPE (rel->r_info))
3337 bfd_vma g_o_t = htab->sgotplt->output_section->vma
3338 + htab->sgotplt->output_offset;
3339 relocation = htab->sgot->output_section->vma
3340 + htab->sgot->output_offset + off - g_o_t;
3341 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3342 && tls_type == GOT_TLS_IE_BOTH)
3343 relocation += 4;
3344 if (r_type == R_386_TLS_IE)
3345 relocation += g_o_t;
3346 unresolved_reloc = FALSE;
3348 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3350 unsigned int val, type;
3351 bfd_vma roff;
3353 /* GD->IE transition. */
3354 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3355 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3356 if (type == 0x04)
3358 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3359 Change it into:
3360 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3361 val >>= 3;
3362 roff = rel->r_offset - 3;
3364 else
3366 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3367 Change it into:
3368 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3369 roff = rel->r_offset - 2;
3371 memcpy (contents + roff,
3372 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3373 contents[roff + 7] = 0x80 | (val & 7);
3374 /* If foo is used only with foo@gotntpoff(%reg) and
3375 foo@indntpoff, but not with foo@gottpoff(%reg), change
3376 subl $foo@gottpoff(%reg), %eax
3377 into:
3378 addl $foo@gotntpoff(%reg), %eax. */
3379 if (tls_type == GOT_TLS_IE_POS)
3380 contents[roff + 6] = 0x03;
3381 bfd_put_32 (output_bfd,
3382 htab->sgot->output_section->vma
3383 + htab->sgot->output_offset + off
3384 - htab->sgotplt->output_section->vma
3385 - htab->sgotplt->output_offset,
3386 contents + roff + 8);
3387 /* Skip R_386_PLT32. */
3388 rel++;
3389 continue;
3391 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3393 /* GDesc -> IE transition.
3394 It's originally something like:
3395 leal x@tlsdesc(%ebx), %eax
3397 Change it to:
3398 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3400 movl x@gottpoff(%ebx), %eax # before negl %eax
3402 Registers other than %eax may be set up here. */
3404 bfd_vma roff;
3406 /* First, make sure it's a leal adding ebx to a 32-bit
3407 offset into any register, although it's probably
3408 almost always going to be eax. */
3409 roff = rel->r_offset;
3411 /* Now modify the instruction as appropriate. */
3412 /* To turn a leal into a movl in the form we use it, it
3413 suffices to change the first byte from 0x8d to 0x8b.
3414 aoliva FIXME: should we decide to keep the leal, all
3415 we have to do is remove the statement below, and
3416 adjust the relaxation of R_386_TLS_DESC_CALL. */
3417 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3419 if (tls_type == GOT_TLS_IE_BOTH)
3420 off += 4;
3422 bfd_put_32 (output_bfd,
3423 htab->sgot->output_section->vma
3424 + htab->sgot->output_offset + off
3425 - htab->sgotplt->output_section->vma
3426 - htab->sgotplt->output_offset,
3427 contents + roff);
3428 continue;
3430 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3432 /* GDesc -> IE transition.
3433 It's originally:
3434 call *(%eax)
3436 Change it to:
3437 xchg %ax,%ax
3439 negl %eax
3440 depending on how we transformed the TLS_GOTDESC above.
3443 bfd_vma roff;
3445 roff = rel->r_offset;
3447 /* Now modify the instruction as appropriate. */
3448 if (tls_type != GOT_TLS_IE_NEG)
3450 /* xchg %ax,%ax */
3451 bfd_put_8 (output_bfd, 0x66, contents + roff);
3452 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3454 else
3456 /* negl %eax */
3457 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3458 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3461 continue;
3463 else
3464 BFD_ASSERT (FALSE);
3465 break;
3467 case R_386_TLS_LDM:
3468 if (! elf_i386_tls_transition (info, input_bfd,
3469 input_section, contents,
3470 symtab_hdr, sym_hashes,
3471 &r_type, GOT_UNKNOWN, rel,
3472 relend, h))
3473 return FALSE;
3475 if (r_type != R_386_TLS_LDM)
3477 /* LD->LE transition:
3478 leal foo(%reg), %eax; call ___tls_get_addr.
3479 We change it into:
3480 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
3481 BFD_ASSERT (r_type == R_386_TLS_LE_32);
3482 memcpy (contents + rel->r_offset - 2,
3483 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3484 /* Skip R_386_PC32/R_386_PLT32. */
3485 rel++;
3486 continue;
3489 if (htab->sgot == NULL)
3490 abort ();
3492 off = htab->tls_ldm_got.offset;
3493 if (off & 1)
3494 off &= ~1;
3495 else
3497 Elf_Internal_Rela outrel;
3498 bfd_byte *loc;
3500 if (htab->srelgot == NULL)
3501 abort ();
3503 outrel.r_offset = (htab->sgot->output_section->vma
3504 + htab->sgot->output_offset + off);
3506 bfd_put_32 (output_bfd, 0,
3507 htab->sgot->contents + off);
3508 bfd_put_32 (output_bfd, 0,
3509 htab->sgot->contents + off + 4);
3510 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3511 loc = htab->srelgot->contents;
3512 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3513 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3514 htab->tls_ldm_got.offset |= 1;
3516 relocation = htab->sgot->output_section->vma
3517 + htab->sgot->output_offset + off
3518 - htab->sgotplt->output_section->vma
3519 - htab->sgotplt->output_offset;
3520 unresolved_reloc = FALSE;
3521 break;
3523 case R_386_TLS_LDO_32:
3524 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3525 relocation -= dtpoff_base (info);
3526 else
3527 /* When converting LDO to LE, we must negate. */
3528 relocation = -tpoff (info, relocation);
3529 break;
3531 case R_386_TLS_LE_32:
3532 case R_386_TLS_LE:
3533 if (info->shared)
3535 Elf_Internal_Rela outrel;
3536 asection *sreloc;
3537 bfd_byte *loc;
3538 int indx;
3540 outrel.r_offset = rel->r_offset
3541 + input_section->output_section->vma
3542 + input_section->output_offset;
3543 if (h != NULL && h->dynindx != -1)
3544 indx = h->dynindx;
3545 else
3546 indx = 0;
3547 if (r_type == R_386_TLS_LE_32)
3548 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3549 else
3550 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3551 sreloc = elf_section_data (input_section)->sreloc;
3552 if (sreloc == NULL)
3553 abort ();
3554 loc = sreloc->contents;
3555 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3556 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3557 if (indx)
3558 continue;
3559 else if (r_type == R_386_TLS_LE_32)
3560 relocation = dtpoff_base (info) - relocation;
3561 else
3562 relocation -= dtpoff_base (info);
3564 else if (r_type == R_386_TLS_LE_32)
3565 relocation = tpoff (info, relocation);
3566 else
3567 relocation = -tpoff (info, relocation);
3568 break;
3570 default:
3571 break;
3574 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3575 because such sections are not SEC_ALLOC and thus ld.so will
3576 not process them. */
3577 if (unresolved_reloc
3578 && !((input_section->flags & SEC_DEBUGGING) != 0
3579 && h->def_dynamic))
3581 (*_bfd_error_handler)
3582 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3583 input_bfd,
3584 input_section,
3585 (long) rel->r_offset,
3586 howto->name,
3587 h->root.root.string);
3588 return FALSE;
3591 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3592 contents, rel->r_offset,
3593 relocation, 0);
3595 if (r != bfd_reloc_ok)
3597 const char *name;
3599 if (h != NULL)
3600 name = h->root.root.string;
3601 else
3603 name = bfd_elf_string_from_elf_section (input_bfd,
3604 symtab_hdr->sh_link,
3605 sym->st_name);
3606 if (name == NULL)
3607 return FALSE;
3608 if (*name == '\0')
3609 name = bfd_section_name (input_bfd, sec);
3612 if (r == bfd_reloc_overflow)
3614 if (! ((*info->callbacks->reloc_overflow)
3615 (info, (h ? &h->root : NULL), name, howto->name,
3616 (bfd_vma) 0, input_bfd, input_section,
3617 rel->r_offset)))
3618 return FALSE;
3620 else
3622 (*_bfd_error_handler)
3623 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3624 input_bfd, input_section,
3625 (long) rel->r_offset, name, (int) r);
3626 return FALSE;
3631 return TRUE;
3634 /* Finish up dynamic symbol handling. We set the contents of various
3635 dynamic sections here. */
3637 static bfd_boolean
3638 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3639 struct bfd_link_info *info,
3640 struct elf_link_hash_entry *h,
3641 Elf_Internal_Sym *sym)
3643 struct elf_i386_link_hash_table *htab;
3645 htab = elf_i386_hash_table (info);
3647 if (h->plt.offset != (bfd_vma) -1)
3649 bfd_vma plt_index;
3650 bfd_vma got_offset;
3651 Elf_Internal_Rela rel;
3652 bfd_byte *loc;
3654 /* This symbol has an entry in the procedure linkage table. Set
3655 it up. */
3657 if (h->dynindx == -1
3658 || htab->splt == NULL
3659 || htab->sgotplt == NULL
3660 || htab->srelplt == NULL)
3661 abort ();
3663 /* Get the index in the procedure linkage table which
3664 corresponds to this symbol. This is the index of this symbol
3665 in all the symbols for which we are making plt entries. The
3666 first entry in the procedure linkage table is reserved. */
3667 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3669 /* Get the offset into the .got table of the entry that
3670 corresponds to this function. Each .got entry is 4 bytes.
3671 The first three are reserved. */
3672 got_offset = (plt_index + 3) * 4;
3674 /* Fill in the entry in the procedure linkage table. */
3675 if (! info->shared)
3677 memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
3678 PLT_ENTRY_SIZE);
3679 bfd_put_32 (output_bfd,
3680 (htab->sgotplt->output_section->vma
3681 + htab->sgotplt->output_offset
3682 + got_offset),
3683 htab->splt->contents + h->plt.offset + 2);
3685 if (htab->is_vxworks)
3687 int s, k, reloc_index;
3689 /* Create the R_386_32 relocation referencing the GOT
3690 for this PLT entry. */
3692 /* S: Current slot number (zero-based). */
3693 s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3694 /* K: Number of relocations for PLTResolve. */
3695 if (info->shared)
3696 k = PLTRESOLVE_RELOCS_SHLIB;
3697 else
3698 k = PLTRESOLVE_RELOCS;
3699 /* Skip the PLTresolve relocations, and the relocations for
3700 the other PLT slots. */
3701 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3702 loc = (htab->srelplt2->contents + reloc_index
3703 * sizeof (Elf32_External_Rel));
3705 rel.r_offset = (htab->splt->output_section->vma
3706 + htab->splt->output_offset
3707 + h->plt.offset + 2),
3708 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3709 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3711 /* Create the R_386_32 relocation referencing the beginning of
3712 the PLT for this GOT entry. */
3713 rel.r_offset = (htab->sgotplt->output_section->vma
3714 + htab->sgotplt->output_offset
3715 + got_offset);
3716 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3717 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3718 loc + sizeof (Elf32_External_Rel));
3721 else
3723 memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
3724 PLT_ENTRY_SIZE);
3725 bfd_put_32 (output_bfd, got_offset,
3726 htab->splt->contents + h->plt.offset + 2);
3729 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
3730 htab->splt->contents + h->plt.offset + 7);
3731 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3732 htab->splt->contents + h->plt.offset + 12);
3734 /* Fill in the entry in the global offset table. */
3735 bfd_put_32 (output_bfd,
3736 (htab->splt->output_section->vma
3737 + htab->splt->output_offset
3738 + h->plt.offset
3739 + 6),
3740 htab->sgotplt->contents + got_offset);
3742 /* Fill in the entry in the .rel.plt section. */
3743 rel.r_offset = (htab->sgotplt->output_section->vma
3744 + htab->sgotplt->output_offset
3745 + got_offset);
3746 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3747 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
3748 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3750 if (!h->def_regular)
3752 /* Mark the symbol as undefined, rather than as defined in
3753 the .plt section. Leave the value if there were any
3754 relocations where pointer equality matters (this is a clue
3755 for the dynamic linker, to make function pointer
3756 comparisons work between an application and shared
3757 library), otherwise set it to zero. If a function is only
3758 called from a binary, there is no need to slow down
3759 shared libraries because of that. */
3760 sym->st_shndx = SHN_UNDEF;
3761 if (!h->pointer_equality_needed)
3762 sym->st_value = 0;
3766 if (h->got.offset != (bfd_vma) -1
3767 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
3768 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
3770 Elf_Internal_Rela rel;
3771 bfd_byte *loc;
3773 /* This symbol has an entry in the global offset table. Set it
3774 up. */
3776 if (htab->sgot == NULL || htab->srelgot == NULL)
3777 abort ();
3779 rel.r_offset = (htab->sgot->output_section->vma
3780 + htab->sgot->output_offset
3781 + (h->got.offset & ~(bfd_vma) 1));
3783 /* If this is a static link, or it is a -Bsymbolic link and the
3784 symbol is defined locally or was forced to be local because
3785 of a version file, we just want to emit a RELATIVE reloc.
3786 The entry in the global offset table will already have been
3787 initialized in the relocate_section function. */
3788 if (info->shared
3789 && SYMBOL_REFERENCES_LOCAL (info, h))
3791 BFD_ASSERT((h->got.offset & 1) != 0);
3792 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3794 else
3796 BFD_ASSERT((h->got.offset & 1) == 0);
3797 bfd_put_32 (output_bfd, (bfd_vma) 0,
3798 htab->sgot->contents + h->got.offset);
3799 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3802 loc = htab->srelgot->contents;
3803 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3804 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3807 if (h->needs_copy)
3809 Elf_Internal_Rela rel;
3810 bfd_byte *loc;
3812 /* This symbol needs a copy reloc. Set it up. */
3814 if (h->dynindx == -1
3815 || (h->root.type != bfd_link_hash_defined
3816 && h->root.type != bfd_link_hash_defweak)
3817 || htab->srelbss == NULL)
3818 abort ();
3820 rel.r_offset = (h->root.u.def.value
3821 + h->root.u.def.section->output_section->vma
3822 + h->root.u.def.section->output_offset);
3823 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3824 loc = htab->srelbss->contents;
3825 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
3826 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3829 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
3830 On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
3831 is relative to the ".got" section. */
3832 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3833 || (!htab->is_vxworks && h == htab->elf.hgot))
3834 sym->st_shndx = SHN_ABS;
3836 return TRUE;
3839 /* Used to decide how to sort relocs in an optimal manner for the
3840 dynamic linker, before writing them out. */
3842 static enum elf_reloc_type_class
3843 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
3845 switch (ELF32_R_TYPE (rela->r_info))
3847 case R_386_RELATIVE:
3848 return reloc_class_relative;
3849 case R_386_JUMP_SLOT:
3850 return reloc_class_plt;
3851 case R_386_COPY:
3852 return reloc_class_copy;
3853 default:
3854 return reloc_class_normal;
3858 /* Finish up the dynamic sections. */
3860 static bfd_boolean
3861 elf_i386_finish_dynamic_sections (bfd *output_bfd,
3862 struct bfd_link_info *info)
3864 struct elf_i386_link_hash_table *htab;
3865 bfd *dynobj;
3866 asection *sdyn;
3868 htab = elf_i386_hash_table (info);
3869 dynobj = htab->elf.dynobj;
3870 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3872 if (htab->elf.dynamic_sections_created)
3874 Elf32_External_Dyn *dyncon, *dynconend;
3876 if (sdyn == NULL || htab->sgot == NULL)
3877 abort ();
3879 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3880 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3881 for (; dyncon < dynconend; dyncon++)
3883 Elf_Internal_Dyn dyn;
3884 asection *s;
3886 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3888 switch (dyn.d_tag)
3890 default:
3891 if (htab->is_vxworks
3892 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
3893 break;
3894 continue;
3896 case DT_PLTGOT:
3897 s = htab->sgotplt;
3898 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3899 break;
3901 case DT_JMPREL:
3902 s = htab->srelplt;
3903 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3904 break;
3906 case DT_PLTRELSZ:
3907 s = htab->srelplt;
3908 dyn.d_un.d_val = s->size;
3909 break;
3911 case DT_RELSZ:
3912 /* My reading of the SVR4 ABI indicates that the
3913 procedure linkage table relocs (DT_JMPREL) should be
3914 included in the overall relocs (DT_REL). This is
3915 what Solaris does. However, UnixWare can not handle
3916 that case. Therefore, we override the DT_RELSZ entry
3917 here to make it not include the JMPREL relocs. */
3918 s = htab->srelplt;
3919 if (s == NULL)
3920 continue;
3921 dyn.d_un.d_val -= s->size;
3922 break;
3924 case DT_REL:
3925 /* We may not be using the standard ELF linker script.
3926 If .rel.plt is the first .rel section, we adjust
3927 DT_REL to not include it. */
3928 s = htab->srelplt;
3929 if (s == NULL)
3930 continue;
3931 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
3932 continue;
3933 dyn.d_un.d_ptr += s->size;
3934 break;
3937 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3940 /* Fill in the first entry in the procedure linkage table. */
3941 if (htab->splt && htab->splt->size > 0)
3943 if (info->shared)
3945 memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
3946 sizeof (elf_i386_pic_plt0_entry));
3947 memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
3948 htab->plt0_pad_byte,
3949 PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
3951 else
3953 memcpy (htab->splt->contents, elf_i386_plt0_entry,
3954 sizeof(elf_i386_plt0_entry));
3955 memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
3956 htab->plt0_pad_byte,
3957 PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
3958 bfd_put_32 (output_bfd,
3959 (htab->sgotplt->output_section->vma
3960 + htab->sgotplt->output_offset
3961 + 4),
3962 htab->splt->contents + 2);
3963 bfd_put_32 (output_bfd,
3964 (htab->sgotplt->output_section->vma
3965 + htab->sgotplt->output_offset
3966 + 8),
3967 htab->splt->contents + 8);
3969 if (htab->is_vxworks)
3971 Elf_Internal_Rela rel;
3973 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
3974 On IA32 we use REL relocations so the addend goes in
3975 the PLT directly. */
3976 rel.r_offset = (htab->splt->output_section->vma
3977 + htab->splt->output_offset
3978 + 2);
3979 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3980 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3981 htab->srelplt2->contents);
3982 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
3983 rel.r_offset = (htab->splt->output_section->vma
3984 + htab->splt->output_offset
3985 + 8);
3986 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3987 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3988 htab->srelplt2->contents +
3989 sizeof (Elf32_External_Rel));
3993 /* UnixWare sets the entsize of .plt to 4, although that doesn't
3994 really seem like the right value. */
3995 elf_section_data (htab->splt->output_section)
3996 ->this_hdr.sh_entsize = 4;
3998 /* Correct the .rel.plt.unloaded relocations. */
3999 if (htab->is_vxworks && !info->shared)
4001 int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
4002 unsigned char *p;
4004 p = htab->srelplt2->contents;
4005 if (info->shared)
4006 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4007 else
4008 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4010 for (; num_plts; num_plts--)
4012 Elf_Internal_Rela rel;
4013 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4014 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4015 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4016 p += sizeof (Elf32_External_Rel);
4018 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4019 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4020 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4021 p += sizeof (Elf32_External_Rel);
4027 if (htab->sgotplt)
4029 /* Fill in the first three entries in the global offset table. */
4030 if (htab->sgotplt->size > 0)
4032 bfd_put_32 (output_bfd,
4033 (sdyn == NULL ? 0
4034 : sdyn->output_section->vma + sdyn->output_offset),
4035 htab->sgotplt->contents);
4036 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
4037 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
4040 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
4043 if (htab->sgot && htab->sgot->size > 0)
4044 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
4046 return TRUE;
4049 /* Return address for Ith PLT stub in section PLT, for relocation REL
4050 or (bfd_vma) -1 if it should not be included. */
4052 static bfd_vma
4053 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
4054 const arelent *rel ATTRIBUTE_UNUSED)
4056 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4059 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4061 static bfd_boolean
4062 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
4064 if (h->plt.offset != (bfd_vma) -1
4065 && !h->def_regular
4066 && !h->pointer_equality_needed)
4067 return FALSE;
4069 return _bfd_elf_hash_symbol (h);
4072 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
4073 #define TARGET_LITTLE_NAME "elf32-i386"
4074 #define ELF_ARCH bfd_arch_i386
4075 #define ELF_MACHINE_CODE EM_386
4076 #define ELF_MAXPAGESIZE 0x1000
4078 #define elf_backend_can_gc_sections 1
4079 #define elf_backend_can_refcount 1
4080 #define elf_backend_want_got_plt 1
4081 #define elf_backend_plt_readonly 1
4082 #define elf_backend_want_plt_sym 0
4083 #define elf_backend_got_header_size 12
4085 /* Support RELA for objdump of prelink objects. */
4086 #define elf_info_to_howto elf_i386_info_to_howto_rel
4087 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4089 #define bfd_elf32_mkobject elf_i386_mkobject
4091 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4092 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
4093 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
4094 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
4096 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
4097 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4098 #define elf_backend_check_relocs elf_i386_check_relocs
4099 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
4100 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
4101 #define elf_backend_fake_sections elf_i386_fake_sections
4102 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4103 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4104 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
4105 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
4106 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4107 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4108 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4109 #define elf_backend_relocate_section elf_i386_relocate_section
4110 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
4111 #define elf_backend_always_size_sections elf_i386_always_size_sections
4112 #define elf_backend_omit_section_dynsym \
4113 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4114 #define elf_backend_plt_sym_val elf_i386_plt_sym_val
4115 #define elf_backend_hash_symbol elf_i386_hash_symbol
4117 #include "elf32-target.h"
4119 /* FreeBSD support. */
4121 #undef TARGET_LITTLE_SYM
4122 #define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
4123 #undef TARGET_LITTLE_NAME
4124 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4125 #undef ELF_OSABI
4126 #define ELF_OSABI ELFOSABI_FREEBSD
4128 /* The kernel recognizes executables as valid only if they carry a
4129 "FreeBSD" label in the ELF header. So we put this label on all
4130 executables and (for simplicity) also all other object files. */
4132 static void
4133 elf_i386_post_process_headers (bfd *abfd,
4134 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4136 Elf_Internal_Ehdr *i_ehdrp;
4138 i_ehdrp = elf_elfheader (abfd);
4140 /* Put an ABI label supported by FreeBSD >= 4.1. */
4141 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
4142 #ifdef OLD_FREEBSD_ABI_LABEL
4143 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4144 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4145 #endif
4148 #undef elf_backend_post_process_headers
4149 #define elf_backend_post_process_headers elf_i386_post_process_headers
4150 #undef elf32_bed
4151 #define elf32_bed elf32_i386_fbsd_bed
4153 #include "elf32-target.h"
4155 /* VxWorks support. */
4157 #undef TARGET_LITTLE_SYM
4158 #define TARGET_LITTLE_SYM bfd_elf32_i386_vxworks_vec
4159 #undef TARGET_LITTLE_NAME
4160 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
4161 #undef ELF_OSABI
4163 /* Like elf_i386_link_hash_table_create but with tweaks for VxWorks. */
4165 static struct bfd_link_hash_table *
4166 elf_i386_vxworks_link_hash_table_create (bfd *abfd)
4168 struct bfd_link_hash_table *ret;
4169 struct elf_i386_link_hash_table *htab;
4171 ret = elf_i386_link_hash_table_create (abfd);
4172 if (ret)
4174 htab = (struct elf_i386_link_hash_table *) ret;
4175 htab->is_vxworks = 1;
4176 htab->plt0_pad_byte = 0x90;
4179 return ret;
4183 #undef elf_backend_relocs_compatible
4184 #undef elf_backend_post_process_headers
4185 #undef bfd_elf32_bfd_link_hash_table_create
4186 #define bfd_elf32_bfd_link_hash_table_create \
4187 elf_i386_vxworks_link_hash_table_create
4188 #undef elf_backend_add_symbol_hook
4189 #define elf_backend_add_symbol_hook \
4190 elf_vxworks_add_symbol_hook
4191 #undef elf_backend_link_output_symbol_hook
4192 #define elf_backend_link_output_symbol_hook \
4193 elf_vxworks_link_output_symbol_hook
4194 #undef elf_backend_emit_relocs
4195 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
4196 #undef elf_backend_final_write_processing
4197 #define elf_backend_final_write_processing \
4198 elf_vxworks_final_write_processing
4200 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4201 define it. */
4202 #undef elf_backend_want_plt_sym
4203 #define elf_backend_want_plt_sym 1
4205 #undef elf32_bed
4206 #define elf32_bed elf32_i386_vxworks_bed
4208 #include "elf32-target.h"