Customize the code to make it compile nicely.
[dragonfly.git] / contrib / gdb-6 / bfd / elf32-i386.c
blob495f1230fd5d512b36e9068e1544096485576979
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 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 static bfd_boolean
646 elf_i386_mkobject (bfd *abfd)
648 if (abfd->tdata.any == NULL)
650 bfd_size_type amt = sizeof (struct elf_i386_obj_tdata);
651 abfd->tdata.any = bfd_zalloc (abfd, amt);
652 if (abfd->tdata.any == NULL)
653 return FALSE;
655 return bfd_elf_mkobject (abfd);
658 /* i386 ELF linker hash table. */
660 struct elf_i386_link_hash_table
662 struct elf_link_hash_table elf;
664 /* Short-cuts to get to dynamic linker sections. */
665 asection *sgot;
666 asection *sgotplt;
667 asection *srelgot;
668 asection *splt;
669 asection *srelplt;
670 asection *sdynbss;
671 asection *srelbss;
673 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */
674 asection *srelplt2;
676 /* True if the target system is VxWorks. */
677 int is_vxworks;
679 /* Value used to fill the last word of the first plt entry. */
680 bfd_byte plt0_pad_byte;
682 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */
683 bfd_vma next_tls_desc_index;
685 union {
686 bfd_signed_vma refcount;
687 bfd_vma offset;
688 } tls_ldm_got;
690 /* The amount of space used by the reserved portion of the sgotplt
691 section, plus whatever space is used by the jump slots. */
692 bfd_vma sgotplt_jump_table_size;
694 /* Small local sym to section mapping cache. */
695 struct sym_sec_cache sym_sec;
698 /* Get the i386 ELF linker hash table from a link_info structure. */
700 #define elf_i386_hash_table(p) \
701 ((struct elf_i386_link_hash_table *) ((p)->hash))
703 #define elf_i386_compute_jump_table_size(htab) \
704 ((htab)->next_tls_desc_index * 4)
706 /* Create an entry in an i386 ELF linker hash table. */
708 static struct bfd_hash_entry *
709 link_hash_newfunc (struct bfd_hash_entry *entry,
710 struct bfd_hash_table *table,
711 const char *string)
713 /* Allocate the structure if it has not already been allocated by a
714 subclass. */
715 if (entry == NULL)
717 entry = bfd_hash_allocate (table,
718 sizeof (struct elf_i386_link_hash_entry));
719 if (entry == NULL)
720 return entry;
723 /* Call the allocation method of the superclass. */
724 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
725 if (entry != NULL)
727 struct elf_i386_link_hash_entry *eh;
729 eh = (struct elf_i386_link_hash_entry *) entry;
730 eh->dyn_relocs = NULL;
731 eh->tls_type = GOT_UNKNOWN;
732 eh->tlsdesc_got = (bfd_vma) -1;
735 return entry;
738 /* Create an i386 ELF linker hash table. */
740 static struct bfd_link_hash_table *
741 elf_i386_link_hash_table_create (bfd *abfd)
743 struct elf_i386_link_hash_table *ret;
744 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
746 ret = bfd_malloc (amt);
747 if (ret == NULL)
748 return NULL;
750 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
751 sizeof (struct elf_i386_link_hash_entry)))
753 free (ret);
754 return NULL;
757 ret->sgot = NULL;
758 ret->sgotplt = NULL;
759 ret->srelgot = NULL;
760 ret->splt = NULL;
761 ret->srelplt = NULL;
762 ret->sdynbss = NULL;
763 ret->srelbss = NULL;
764 ret->tls_ldm_got.refcount = 0;
765 ret->next_tls_desc_index = 0;
766 ret->sgotplt_jump_table_size = 0;
767 ret->sym_sec.abfd = NULL;
768 ret->is_vxworks = 0;
769 ret->srelplt2 = NULL;
770 ret->plt0_pad_byte = 0;
772 return &ret->elf.root;
775 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
776 shortcuts to them in our hash table. */
778 static bfd_boolean
779 create_got_section (bfd *dynobj, struct bfd_link_info *info)
781 struct elf_i386_link_hash_table *htab;
783 if (! _bfd_elf_create_got_section (dynobj, info))
784 return FALSE;
786 htab = elf_i386_hash_table (info);
787 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
788 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
789 if (!htab->sgot || !htab->sgotplt)
790 abort ();
792 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rel.got",
793 (SEC_ALLOC | SEC_LOAD
794 | SEC_HAS_CONTENTS
795 | SEC_IN_MEMORY
796 | SEC_LINKER_CREATED
797 | SEC_READONLY));
798 if (htab->srelgot == NULL
799 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
800 return FALSE;
801 return TRUE;
804 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
805 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
806 hash table. */
808 static bfd_boolean
809 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
811 struct elf_i386_link_hash_table *htab;
813 htab = elf_i386_hash_table (info);
814 if (!htab->sgot && !create_got_section (dynobj, info))
815 return FALSE;
817 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
818 return FALSE;
820 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
821 htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
822 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
823 if (!info->shared)
824 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
826 if (!htab->splt || !htab->srelplt || !htab->sdynbss
827 || (!info->shared && !htab->srelbss))
828 abort ();
830 return TRUE;
833 /* Copy the extra info we tack onto an elf_link_hash_entry. */
835 static void
836 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
837 struct elf_link_hash_entry *dir,
838 struct elf_link_hash_entry *ind)
840 struct elf_i386_link_hash_entry *edir, *eind;
842 edir = (struct elf_i386_link_hash_entry *) dir;
843 eind = (struct elf_i386_link_hash_entry *) ind;
845 if (eind->dyn_relocs != NULL)
847 if (edir->dyn_relocs != NULL)
849 struct elf_i386_dyn_relocs **pp;
850 struct elf_i386_dyn_relocs *p;
852 /* Add reloc counts against the indirect sym to the direct sym
853 list. Merge any entries against the same section. */
854 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
856 struct elf_i386_dyn_relocs *q;
858 for (q = edir->dyn_relocs; q != NULL; q = q->next)
859 if (q->sec == p->sec)
861 q->pc_count += p->pc_count;
862 q->count += p->count;
863 *pp = p->next;
864 break;
866 if (q == NULL)
867 pp = &p->next;
869 *pp = edir->dyn_relocs;
872 edir->dyn_relocs = eind->dyn_relocs;
873 eind->dyn_relocs = NULL;
876 if (ind->root.type == bfd_link_hash_indirect
877 && dir->got.refcount <= 0)
879 edir->tls_type = eind->tls_type;
880 eind->tls_type = GOT_UNKNOWN;
883 if (ELIMINATE_COPY_RELOCS
884 && ind->root.type != bfd_link_hash_indirect
885 && dir->dynamic_adjusted)
887 /* If called to transfer flags for a weakdef during processing
888 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
889 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
890 dir->ref_dynamic |= ind->ref_dynamic;
891 dir->ref_regular |= ind->ref_regular;
892 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
893 dir->needs_plt |= ind->needs_plt;
894 dir->pointer_equality_needed |= ind->pointer_equality_needed;
896 else
897 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
900 typedef union
902 unsigned char c[2];
903 uint16_t i;
905 i386_opcode16;
907 /* Return TRUE if the TLS access code sequence support transition
908 from R_TYPE. */
910 static bfd_boolean
911 elf_i386_check_tls_transition (bfd *abfd, asection *sec,
912 bfd_byte *contents,
913 Elf_Internal_Shdr *symtab_hdr,
914 struct elf_link_hash_entry **sym_hashes,
915 unsigned int r_type,
916 const Elf_Internal_Rela *rel,
917 const Elf_Internal_Rela *relend)
919 unsigned int val, type;
920 unsigned long r_symndx;
921 struct elf_link_hash_entry *h;
922 bfd_vma offset;
924 /* Get the section contents. */
925 if (contents == NULL)
927 if (elf_section_data (sec)->this_hdr.contents != NULL)
928 contents = elf_section_data (sec)->this_hdr.contents;
929 else
931 /* FIXME: How to better handle error condition? */
932 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
933 return FALSE;
935 /* Cache the section contents for elf_link_input_bfd. */
936 elf_section_data (sec)->this_hdr.contents = contents;
940 offset = rel->r_offset;
941 switch (r_type)
943 case R_386_TLS_GD:
944 case R_386_TLS_LDM:
945 if (offset < 2 || (rel + 1) >= relend)
946 return FALSE;
948 type = bfd_get_8 (abfd, contents + offset - 2);
949 if (r_type == R_386_TLS_GD)
951 /* Check transition from LD access model. Only
952 leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
953 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
954 can transit to different access model. */
955 if ((offset + 10) > sec->size ||
956 (type != 0x8d && type != 0x04))
957 return FALSE;
959 val = bfd_get_8 (abfd, contents + offset - 1);
960 if (type == 0x04)
962 /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
963 if (offset < 3)
964 return FALSE;
966 if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
967 return FALSE;
969 if ((val & 0xc7) != 0x05 || val == (4 << 3))
970 return FALSE;
972 else
974 /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop */
975 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
976 return FALSE;
978 if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
979 return FALSE;
982 else
984 /* Check transition from LD access model. Only
985 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
986 can transit to different access model. */
987 if (type != 0x8d || (offset + 9) > sec->size)
988 return FALSE;
990 val = bfd_get_8 (abfd, contents + offset - 1);
991 if ((val & 0xf8) != 0x80 || (val & 7) == 4)
992 return FALSE;
995 if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
996 return FALSE;
998 r_symndx = ELF32_R_SYM (rel[1].r_info);
999 if (r_symndx < symtab_hdr->sh_info)
1000 return FALSE;
1002 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1003 return (h != NULL
1004 && h->root.root.string != NULL
1005 && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1006 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
1007 && (strcmp (h->root.root.string, "___tls_get_addr") == 0));
1009 case R_386_TLS_IE:
1010 /* Check transition from IE access model:
1011 movl foo@indntpoff(%rip), %eax
1012 movl foo@indntpoff(%rip), %reg
1013 addl foo@indntpoff(%rip), %reg
1016 if (offset < 1 || (offset + 4) > sec->size)
1017 return FALSE;
1019 /* Check "movl foo@tpoff(%rip), %eax" first. */
1020 val = bfd_get_8 (abfd, contents + offset - 1);
1021 if (val == 0xa1)
1022 return TRUE;
1024 if (offset < 2)
1025 return FALSE;
1027 /* Check movl|addl foo@tpoff(%rip), %reg. */
1028 type = bfd_get_8 (abfd, contents + offset - 2);
1029 return ((type == 0x8b || type == 0x03)
1030 && (val & 0xc7) == 0x05);
1032 case R_386_TLS_GOTIE:
1033 case R_386_TLS_IE_32:
1034 /* Check transition from {IE_32,GOTIE} access model:
1035 subl foo@{tpoff,gontoff}(%reg1), %reg2
1036 movl foo@{tpoff,gontoff}(%reg1), %reg2
1037 addl foo@{tpoff,gontoff}(%reg1), %reg2
1040 if (offset < 2 || (offset + 4) > sec->size)
1041 return FALSE;
1043 val = bfd_get_8 (abfd, contents + offset - 1);
1044 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1045 return FALSE;
1047 type = bfd_get_8 (abfd, contents + offset - 2);
1048 return type == 0x8b || type == 0x2b || type == 0x03;
1050 case R_386_TLS_GOTDESC:
1051 /* Check transition from GDesc access model:
1052 leal x@tlsdesc(%ebx), %eax
1054 Make sure it's a leal adding ebx to a 32-bit offset
1055 into any register, although it's probably almost always
1056 going to be eax. */
1058 if (offset < 2 || (offset + 4) > sec->size)
1059 return FALSE;
1061 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1062 return FALSE;
1064 val = bfd_get_8 (abfd, contents + offset - 1);
1065 return (val & 0xc7) == 0x83;
1067 case R_386_TLS_DESC_CALL:
1068 /* Check transition from GDesc access model:
1069 call *x@tlsdesc(%rax)
1071 if (offset + 2 <= sec->size)
1073 /* Make sure that it's a call *x@tlsdesc(%rax). */
1074 static i386_opcode16 call = { { 0xff, 0x10 } };
1075 return bfd_get_16 (abfd, contents + offset) == call.i;
1078 return FALSE;
1080 default:
1081 abort ();
1085 /* Return TRUE if the TLS access transition is OK or no transition
1086 will be performed. Update R_TYPE if there is a transition. */
1088 static bfd_boolean
1089 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1090 asection *sec, bfd_byte *contents,
1091 Elf_Internal_Shdr *symtab_hdr,
1092 struct elf_link_hash_entry **sym_hashes,
1093 unsigned int *r_type, int tls_type,
1094 const Elf_Internal_Rela *rel,
1095 const Elf_Internal_Rela *relend,
1096 struct elf_link_hash_entry *h)
1098 unsigned int from_type = *r_type;
1099 unsigned int to_type = from_type;
1100 bfd_boolean check = TRUE;
1102 switch (from_type)
1104 case R_386_TLS_GD:
1105 case R_386_TLS_GOTDESC:
1106 case R_386_TLS_DESC_CALL:
1107 case R_386_TLS_IE_32:
1108 case R_386_TLS_IE:
1109 case R_386_TLS_GOTIE:
1110 if (!info->shared)
1112 if (h == NULL)
1113 to_type = R_386_TLS_LE_32;
1114 else if (from_type != R_386_TLS_IE
1115 && from_type != R_386_TLS_GOTIE)
1116 to_type = R_386_TLS_IE_32;
1119 /* When we are called from elf_i386_relocate_section, CONTENTS
1120 isn't NULL and there may be additional transitions based on
1121 TLS_TYPE. */
1122 if (contents != NULL)
1124 unsigned int new_to_type = to_type;
1126 if (!info->shared
1127 && h != NULL
1128 && h->dynindx == -1
1129 && (tls_type & GOT_TLS_IE))
1130 new_to_type = R_386_TLS_LE_32;
1132 if (to_type == R_386_TLS_GD
1133 || to_type == R_386_TLS_GOTDESC
1134 || to_type == R_386_TLS_DESC_CALL)
1136 if (tls_type == GOT_TLS_IE_POS)
1137 new_to_type = R_386_TLS_GOTIE;
1138 else if (tls_type & GOT_TLS_IE)
1139 new_to_type = R_386_TLS_IE_32;
1142 /* We checked the transition before when we were called from
1143 elf_i386_check_relocs. We only want to check the new
1144 transition which hasn't been checked before. */
1145 check = new_to_type != to_type && from_type == to_type;
1146 to_type = new_to_type;
1149 break;
1151 case R_386_TLS_LDM:
1152 if (!info->shared)
1153 to_type = R_386_TLS_LE_32;
1154 break;
1156 default:
1157 return TRUE;
1160 /* Return TRUE if there is no transition. */
1161 if (from_type == to_type)
1162 return TRUE;
1164 /* Check if the transition can be performed. */
1165 if (check
1166 && ! elf_i386_check_tls_transition (abfd, sec, contents,
1167 symtab_hdr, sym_hashes,
1168 from_type, rel, relend))
1170 const reloc_howto_type *from, *to;
1172 from = elf_i386_rtype_to_howto (abfd, from_type);
1173 to = elf_i386_rtype_to_howto (abfd, to_type);
1175 (*_bfd_error_handler)
1176 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1177 "in section `%A' failed"),
1178 abfd, sec, from->name, to->name,
1179 h ? h->root.root.string : "a local symbol",
1180 (unsigned long) rel->r_offset);
1181 bfd_set_error (bfd_error_bad_value);
1182 return FALSE;
1185 *r_type = to_type;
1186 return TRUE;
1189 /* Look through the relocs for a section during the first phase, and
1190 calculate needed space in the global offset table, procedure linkage
1191 table, and dynamic reloc sections. */
1193 static bfd_boolean
1194 elf_i386_check_relocs (bfd *abfd,
1195 struct bfd_link_info *info,
1196 asection *sec,
1197 const Elf_Internal_Rela *relocs)
1199 struct elf_i386_link_hash_table *htab;
1200 Elf_Internal_Shdr *symtab_hdr;
1201 struct elf_link_hash_entry **sym_hashes;
1202 const Elf_Internal_Rela *rel;
1203 const Elf_Internal_Rela *rel_end;
1204 asection *sreloc;
1206 if (info->relocatable)
1207 return TRUE;
1209 htab = elf_i386_hash_table (info);
1210 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1211 sym_hashes = elf_sym_hashes (abfd);
1213 sreloc = NULL;
1215 rel_end = relocs + sec->reloc_count;
1216 for (rel = relocs; rel < rel_end; rel++)
1218 unsigned int r_type;
1219 unsigned long r_symndx;
1220 struct elf_link_hash_entry *h;
1222 r_symndx = ELF32_R_SYM (rel->r_info);
1223 r_type = ELF32_R_TYPE (rel->r_info);
1225 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1227 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1228 abfd,
1229 r_symndx);
1230 return FALSE;
1233 if (r_symndx < symtab_hdr->sh_info)
1234 h = NULL;
1235 else
1237 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1238 while (h->root.type == bfd_link_hash_indirect
1239 || h->root.type == bfd_link_hash_warning)
1240 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1243 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1244 symtab_hdr, sym_hashes,
1245 &r_type, GOT_UNKNOWN,
1246 rel, rel_end, h))
1247 return FALSE;
1249 switch (r_type)
1251 case R_386_TLS_LDM:
1252 htab->tls_ldm_got.refcount += 1;
1253 goto create_got;
1255 case R_386_PLT32:
1256 /* This symbol requires a procedure linkage table entry. We
1257 actually build the entry in adjust_dynamic_symbol,
1258 because this might be a case of linking PIC code which is
1259 never referenced by a dynamic object, in which case we
1260 don't need to generate a procedure linkage table entry
1261 after all. */
1263 /* If this is a local symbol, we resolve it directly without
1264 creating a procedure linkage table entry. */
1265 if (h == NULL)
1266 continue;
1268 h->needs_plt = 1;
1269 h->plt.refcount += 1;
1270 break;
1272 case R_386_TLS_IE_32:
1273 case R_386_TLS_IE:
1274 case R_386_TLS_GOTIE:
1275 if (info->shared)
1276 info->flags |= DF_STATIC_TLS;
1277 /* Fall through */
1279 case R_386_GOT32:
1280 case R_386_TLS_GD:
1281 case R_386_TLS_GOTDESC:
1282 case R_386_TLS_DESC_CALL:
1283 /* This symbol requires a global offset table entry. */
1285 int tls_type, old_tls_type;
1287 switch (r_type)
1289 default:
1290 case R_386_GOT32: tls_type = GOT_NORMAL; break;
1291 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1292 case R_386_TLS_GOTDESC:
1293 case R_386_TLS_DESC_CALL:
1294 tls_type = GOT_TLS_GDESC; break;
1295 case R_386_TLS_IE_32:
1296 if (ELF32_R_TYPE (rel->r_info) == r_type)
1297 tls_type = GOT_TLS_IE_NEG;
1298 else
1299 /* If this is a GD->IE transition, we may use either of
1300 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1301 tls_type = GOT_TLS_IE;
1302 break;
1303 case R_386_TLS_IE:
1304 case R_386_TLS_GOTIE:
1305 tls_type = GOT_TLS_IE_POS; break;
1308 if (h != NULL)
1310 h->got.refcount += 1;
1311 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1313 else
1315 bfd_signed_vma *local_got_refcounts;
1317 /* This is a global offset table entry for a local symbol. */
1318 local_got_refcounts = elf_local_got_refcounts (abfd);
1319 if (local_got_refcounts == NULL)
1321 bfd_size_type size;
1323 size = symtab_hdr->sh_info;
1324 size *= (sizeof (bfd_signed_vma)
1325 + sizeof (bfd_vma) + sizeof(char));
1326 local_got_refcounts = bfd_zalloc (abfd, size);
1327 if (local_got_refcounts == NULL)
1328 return FALSE;
1329 elf_local_got_refcounts (abfd) = local_got_refcounts;
1330 elf_i386_local_tlsdesc_gotent (abfd)
1331 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1332 elf_i386_local_got_tls_type (abfd)
1333 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1335 local_got_refcounts[r_symndx] += 1;
1336 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1339 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1340 tls_type |= old_tls_type;
1341 /* If a TLS symbol is accessed using IE at least once,
1342 there is no point to use dynamic model for it. */
1343 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1344 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1345 || (tls_type & GOT_TLS_IE) == 0))
1347 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1348 tls_type = old_tls_type;
1349 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1350 && GOT_TLS_GD_ANY_P (tls_type))
1351 tls_type |= old_tls_type;
1352 else
1354 (*_bfd_error_handler)
1355 (_("%B: `%s' accessed both as normal and "
1356 "thread local symbol"),
1357 abfd,
1358 h ? h->root.root.string : "<local>");
1359 return FALSE;
1363 if (old_tls_type != tls_type)
1365 if (h != NULL)
1366 elf_i386_hash_entry (h)->tls_type = tls_type;
1367 else
1368 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1371 /* Fall through */
1373 case R_386_GOTOFF:
1374 case R_386_GOTPC:
1375 create_got:
1376 if (htab->sgot == NULL)
1378 if (htab->elf.dynobj == NULL)
1379 htab->elf.dynobj = abfd;
1380 if (!create_got_section (htab->elf.dynobj, info))
1381 return FALSE;
1383 if (r_type != R_386_TLS_IE)
1384 break;
1385 /* Fall through */
1387 case R_386_TLS_LE_32:
1388 case R_386_TLS_LE:
1389 if (!info->shared)
1390 break;
1391 info->flags |= DF_STATIC_TLS;
1392 /* Fall through */
1394 case R_386_32:
1395 case R_386_PC32:
1396 if (h != NULL && !info->shared)
1398 /* If this reloc is in a read-only section, we might
1399 need a copy reloc. We can't check reliably at this
1400 stage whether the section is read-only, as input
1401 sections have not yet been mapped to output sections.
1402 Tentatively set the flag for now, and correct in
1403 adjust_dynamic_symbol. */
1404 h->non_got_ref = 1;
1406 /* We may need a .plt entry if the function this reloc
1407 refers to is in a shared lib. */
1408 h->plt.refcount += 1;
1409 if (r_type != R_386_PC32)
1410 h->pointer_equality_needed = 1;
1413 /* If we are creating a shared library, and this is a reloc
1414 against a global symbol, or a non PC relative reloc
1415 against a local symbol, then we need to copy the reloc
1416 into the shared library. However, if we are linking with
1417 -Bsymbolic, we do not need to copy a reloc against a
1418 global symbol which is defined in an object we are
1419 including in the link (i.e., DEF_REGULAR is set). At
1420 this point we have not seen all the input files, so it is
1421 possible that DEF_REGULAR is not set now but will be set
1422 later (it is never cleared). In case of a weak definition,
1423 DEF_REGULAR may be cleared later by a strong definition in
1424 a shared library. We account for that possibility below by
1425 storing information in the relocs_copied field of the hash
1426 table entry. A similar situation occurs when creating
1427 shared libraries and symbol visibility changes render the
1428 symbol local.
1430 If on the other hand, we are creating an executable, we
1431 may need to keep relocations for symbols satisfied by a
1432 dynamic library if we manage to avoid copy relocs for the
1433 symbol. */
1434 if ((info->shared
1435 && (sec->flags & SEC_ALLOC) != 0
1436 && (r_type != R_386_PC32
1437 || (h != NULL
1438 && (! SYMBOLIC_BIND (info, h)
1439 || h->root.type == bfd_link_hash_defweak
1440 || !h->def_regular))))
1441 || (ELIMINATE_COPY_RELOCS
1442 && !info->shared
1443 && (sec->flags & SEC_ALLOC) != 0
1444 && h != NULL
1445 && (h->root.type == bfd_link_hash_defweak
1446 || !h->def_regular)))
1448 struct elf_i386_dyn_relocs *p;
1449 struct elf_i386_dyn_relocs **head;
1451 /* We must copy these reloc types into the output file.
1452 Create a reloc section in dynobj and make room for
1453 this reloc. */
1454 if (sreloc == NULL)
1456 const char *name;
1457 bfd *dynobj;
1458 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1459 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
1461 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1462 if (name == NULL)
1463 return FALSE;
1465 if (! CONST_STRNEQ (name, ".rel")
1466 || strcmp (bfd_get_section_name (abfd, sec),
1467 name + 4) != 0)
1469 (*_bfd_error_handler)
1470 (_("%B: bad relocation section name `%s\'"),
1471 abfd, name);
1474 if (htab->elf.dynobj == NULL)
1475 htab->elf.dynobj = abfd;
1477 dynobj = htab->elf.dynobj;
1478 sreloc = bfd_get_section_by_name (dynobj, name);
1479 if (sreloc == NULL)
1481 flagword flags;
1483 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1484 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1485 if ((sec->flags & SEC_ALLOC) != 0)
1486 flags |= SEC_ALLOC | SEC_LOAD;
1487 sreloc = bfd_make_section_with_flags (dynobj,
1488 name,
1489 flags);
1490 if (sreloc == NULL
1491 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1492 return FALSE;
1494 elf_section_data (sec)->sreloc = sreloc;
1497 /* If this is a global symbol, we count the number of
1498 relocations we need for this symbol. */
1499 if (h != NULL)
1501 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1503 else
1505 void **vpp;
1506 /* Track dynamic relocs needed for local syms too.
1507 We really need local syms available to do this
1508 easily. Oh well. */
1510 asection *s;
1511 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1512 sec, r_symndx);
1513 if (s == NULL)
1514 return FALSE;
1516 vpp = &elf_section_data (s)->local_dynrel;
1517 head = (struct elf_i386_dyn_relocs **)vpp;
1520 p = *head;
1521 if (p == NULL || p->sec != sec)
1523 bfd_size_type amt = sizeof *p;
1524 p = bfd_alloc (htab->elf.dynobj, amt);
1525 if (p == NULL)
1526 return FALSE;
1527 p->next = *head;
1528 *head = p;
1529 p->sec = sec;
1530 p->count = 0;
1531 p->pc_count = 0;
1534 p->count += 1;
1535 if (r_type == R_386_PC32)
1536 p->pc_count += 1;
1538 break;
1540 /* This relocation describes the C++ object vtable hierarchy.
1541 Reconstruct it for later use during GC. */
1542 case R_386_GNU_VTINHERIT:
1543 BFD_ASSERT (h != NULL);
1544 if (h != NULL
1545 && !bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1546 return FALSE;
1547 break;
1549 /* This relocation describes which C++ vtable entries are actually
1550 used. Record for later use during GC. */
1551 case R_386_GNU_VTENTRY:
1552 BFD_ASSERT (h != NULL);
1553 if (h != NULL
1554 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1555 return FALSE;
1556 break;
1558 default:
1559 break;
1563 return TRUE;
1566 /* Return the section that should be marked against GC for a given
1567 relocation. */
1569 static asection *
1570 elf_i386_gc_mark_hook (asection *sec,
1571 struct bfd_link_info *info,
1572 Elf_Internal_Rela *rel,
1573 struct elf_link_hash_entry *h,
1574 Elf_Internal_Sym *sym)
1576 if (h != NULL)
1577 switch (ELF32_R_TYPE (rel->r_info))
1579 case R_386_GNU_VTINHERIT:
1580 case R_386_GNU_VTENTRY:
1581 return NULL;
1584 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1587 /* Update the got entry reference counts for the section being removed. */
1589 static bfd_boolean
1590 elf_i386_gc_sweep_hook (bfd *abfd,
1591 struct bfd_link_info *info,
1592 asection *sec,
1593 const Elf_Internal_Rela *relocs)
1595 Elf_Internal_Shdr *symtab_hdr;
1596 struct elf_link_hash_entry **sym_hashes;
1597 bfd_signed_vma *local_got_refcounts;
1598 const Elf_Internal_Rela *rel, *relend;
1600 elf_section_data (sec)->local_dynrel = NULL;
1602 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1603 sym_hashes = elf_sym_hashes (abfd);
1604 local_got_refcounts = elf_local_got_refcounts (abfd);
1606 relend = relocs + sec->reloc_count;
1607 for (rel = relocs; rel < relend; rel++)
1609 unsigned long r_symndx;
1610 unsigned int r_type;
1611 struct elf_link_hash_entry *h = NULL;
1613 r_symndx = ELF32_R_SYM (rel->r_info);
1614 if (r_symndx >= symtab_hdr->sh_info)
1616 struct elf_i386_link_hash_entry *eh;
1617 struct elf_i386_dyn_relocs **pp;
1618 struct elf_i386_dyn_relocs *p;
1620 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1621 while (h->root.type == bfd_link_hash_indirect
1622 || h->root.type == bfd_link_hash_warning)
1623 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1624 eh = (struct elf_i386_link_hash_entry *) h;
1626 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1627 if (p->sec == sec)
1629 /* Everything must go for SEC. */
1630 *pp = p->next;
1631 break;
1635 r_type = ELF32_R_TYPE (rel->r_info);
1636 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1637 symtab_hdr, sym_hashes,
1638 &r_type, GOT_UNKNOWN,
1639 rel, relend, h))
1640 return FALSE;
1642 switch (r_type)
1644 case R_386_TLS_LDM:
1645 if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1646 elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1647 break;
1649 case R_386_TLS_GD:
1650 case R_386_TLS_GOTDESC:
1651 case R_386_TLS_DESC_CALL:
1652 case R_386_TLS_IE_32:
1653 case R_386_TLS_IE:
1654 case R_386_TLS_GOTIE:
1655 case R_386_GOT32:
1656 if (h != NULL)
1658 if (h->got.refcount > 0)
1659 h->got.refcount -= 1;
1661 else if (local_got_refcounts != NULL)
1663 if (local_got_refcounts[r_symndx] > 0)
1664 local_got_refcounts[r_symndx] -= 1;
1666 break;
1668 case R_386_32:
1669 case R_386_PC32:
1670 if (info->shared)
1671 break;
1672 /* Fall through */
1674 case R_386_PLT32:
1675 if (h != NULL)
1677 if (h->plt.refcount > 0)
1678 h->plt.refcount -= 1;
1680 break;
1682 default:
1683 break;
1687 return TRUE;
1690 /* Adjust a symbol defined by a dynamic object and referenced by a
1691 regular object. The current definition is in some section of the
1692 dynamic object, but we're not including those sections. We have to
1693 change the definition to something the rest of the link can
1694 understand. */
1696 static bfd_boolean
1697 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1698 struct elf_link_hash_entry *h)
1700 struct elf_i386_link_hash_table *htab;
1701 asection *s;
1703 /* If this is a function, put it in the procedure linkage table. We
1704 will fill in the contents of the procedure linkage table later,
1705 when we know the address of the .got section. */
1706 if (h->type == STT_FUNC
1707 || h->needs_plt)
1709 if (h->plt.refcount <= 0
1710 || SYMBOL_CALLS_LOCAL (info, h)
1711 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1712 && h->root.type == bfd_link_hash_undefweak))
1714 /* This case can occur if we saw a PLT32 reloc in an input
1715 file, but the symbol was never referred to by a dynamic
1716 object, or if all references were garbage collected. In
1717 such a case, we don't actually need to build a procedure
1718 linkage table, and we can just do a PC32 reloc instead. */
1719 h->plt.offset = (bfd_vma) -1;
1720 h->needs_plt = 0;
1723 return TRUE;
1725 else
1726 /* It's possible that we incorrectly decided a .plt reloc was
1727 needed for an R_386_PC32 reloc to a non-function sym in
1728 check_relocs. We can't decide accurately between function and
1729 non-function syms in check-relocs; Objects loaded later in
1730 the link may change h->type. So fix it now. */
1731 h->plt.offset = (bfd_vma) -1;
1733 /* If this is a weak symbol, and there is a real definition, the
1734 processor independent code will have arranged for us to see the
1735 real definition first, and we can just use the same value. */
1736 if (h->u.weakdef != NULL)
1738 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1739 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1740 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1741 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1742 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1743 h->non_got_ref = h->u.weakdef->non_got_ref;
1744 return TRUE;
1747 /* This is a reference to a symbol defined by a dynamic object which
1748 is not a function. */
1750 /* If we are creating a shared library, we must presume that the
1751 only references to the symbol are via the global offset table.
1752 For such cases we need not do anything here; the relocations will
1753 be handled correctly by relocate_section. */
1754 if (info->shared)
1755 return TRUE;
1757 /* If there are no references to this symbol that do not use the
1758 GOT, we don't need to generate a copy reloc. */
1759 if (!h->non_got_ref)
1760 return TRUE;
1762 /* If -z nocopyreloc was given, we won't generate them either. */
1763 if (info->nocopyreloc)
1765 h->non_got_ref = 0;
1766 return TRUE;
1769 htab = elf_i386_hash_table (info);
1771 /* If there aren't any dynamic relocs in read-only sections, then
1772 we can keep the dynamic relocs and avoid the copy reloc. This
1773 doesn't work on VxWorks, where we can not have dynamic relocations
1774 (other than copy and jump slot relocations) in an executable. */
1775 if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
1777 struct elf_i386_link_hash_entry * eh;
1778 struct elf_i386_dyn_relocs *p;
1780 eh = (struct elf_i386_link_hash_entry *) h;
1781 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1783 s = p->sec->output_section;
1784 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1785 break;
1788 if (p == NULL)
1790 h->non_got_ref = 0;
1791 return TRUE;
1795 if (h->size == 0)
1797 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1798 h->root.root.string);
1799 return TRUE;
1802 /* We must allocate the symbol in our .dynbss section, which will
1803 become part of the .bss section of the executable. There will be
1804 an entry for this symbol in the .dynsym section. The dynamic
1805 object will contain position independent code, so all references
1806 from the dynamic object to this symbol will go through the global
1807 offset table. The dynamic linker will use the .dynsym entry to
1808 determine the address it must put in the global offset table, so
1809 both the dynamic object and the regular object will refer to the
1810 same memory location for the variable. */
1812 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1813 copy the initial value out of the dynamic object and into the
1814 runtime process image. */
1815 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1817 htab->srelbss->size += sizeof (Elf32_External_Rel);
1818 h->needs_copy = 1;
1821 s = htab->sdynbss;
1823 return _bfd_elf_adjust_dynamic_copy (h, s);
1826 /* Allocate space in .plt, .got and associated reloc sections for
1827 dynamic relocs. */
1829 static bfd_boolean
1830 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1832 struct bfd_link_info *info;
1833 struct elf_i386_link_hash_table *htab;
1834 struct elf_i386_link_hash_entry *eh;
1835 struct elf_i386_dyn_relocs *p;
1837 if (h->root.type == bfd_link_hash_indirect)
1838 return TRUE;
1840 if (h->root.type == bfd_link_hash_warning)
1841 /* When warning symbols are created, they **replace** the "real"
1842 entry in the hash table, thus we never get to see the real
1843 symbol in a hash traversal. So look at it now. */
1844 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1846 info = (struct bfd_link_info *) inf;
1847 htab = elf_i386_hash_table (info);
1849 if (htab->elf.dynamic_sections_created
1850 && h->plt.refcount > 0)
1852 /* Make sure this symbol is output as a dynamic symbol.
1853 Undefined weak syms won't yet be marked as dynamic. */
1854 if (h->dynindx == -1
1855 && !h->forced_local)
1857 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1858 return FALSE;
1861 if (info->shared
1862 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1864 asection *s = htab->splt;
1866 /* If this is the first .plt entry, make room for the special
1867 first entry. */
1868 if (s->size == 0)
1869 s->size += PLT_ENTRY_SIZE;
1871 h->plt.offset = s->size;
1873 /* If this symbol is not defined in a regular file, and we are
1874 not generating a shared library, then set the symbol to this
1875 location in the .plt. This is required to make function
1876 pointers compare as equal between the normal executable and
1877 the shared library. */
1878 if (! info->shared
1879 && !h->def_regular)
1881 h->root.u.def.section = s;
1882 h->root.u.def.value = h->plt.offset;
1885 /* Make room for this entry. */
1886 s->size += PLT_ENTRY_SIZE;
1888 /* We also need to make an entry in the .got.plt section, which
1889 will be placed in the .got section by the linker script. */
1890 htab->sgotplt->size += 4;
1892 /* We also need to make an entry in the .rel.plt section. */
1893 htab->srelplt->size += sizeof (Elf32_External_Rel);
1894 htab->next_tls_desc_index++;
1896 if (htab->is_vxworks && !info->shared)
1898 /* VxWorks has a second set of relocations for each PLT entry
1899 in executables. They go in a separate relocation section,
1900 which is processed by the kernel loader. */
1902 /* There are two relocations for the initial PLT entry: an
1903 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
1904 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
1906 if (h->plt.offset == PLT_ENTRY_SIZE)
1907 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1909 /* There are two extra relocations for each subsequent PLT entry:
1910 an R_386_32 relocation for the GOT entry, and an R_386_32
1911 relocation for the PLT entry. */
1913 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1916 else
1918 h->plt.offset = (bfd_vma) -1;
1919 h->needs_plt = 0;
1922 else
1924 h->plt.offset = (bfd_vma) -1;
1925 h->needs_plt = 0;
1928 eh = (struct elf_i386_link_hash_entry *) h;
1929 eh->tlsdesc_got = (bfd_vma) -1;
1931 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
1932 make it a R_386_TLS_LE_32 requiring no TLS entry. */
1933 if (h->got.refcount > 0
1934 && !info->shared
1935 && h->dynindx == -1
1936 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
1937 h->got.offset = (bfd_vma) -1;
1938 else if (h->got.refcount > 0)
1940 asection *s;
1941 bfd_boolean dyn;
1942 int tls_type = elf_i386_hash_entry(h)->tls_type;
1944 /* Make sure this symbol is output as a dynamic symbol.
1945 Undefined weak syms won't yet be marked as dynamic. */
1946 if (h->dynindx == -1
1947 && !h->forced_local)
1949 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1950 return FALSE;
1953 s = htab->sgot;
1954 if (GOT_TLS_GDESC_P (tls_type))
1956 eh->tlsdesc_got = htab->sgotplt->size
1957 - elf_i386_compute_jump_table_size (htab);
1958 htab->sgotplt->size += 8;
1959 h->got.offset = (bfd_vma) -2;
1961 if (! GOT_TLS_GDESC_P (tls_type)
1962 || GOT_TLS_GD_P (tls_type))
1964 h->got.offset = s->size;
1965 s->size += 4;
1966 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
1967 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
1968 s->size += 4;
1970 dyn = htab->elf.dynamic_sections_created;
1971 /* R_386_TLS_IE_32 needs one dynamic relocation,
1972 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1973 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1974 need two), R_386_TLS_GD needs one if local symbol and two if
1975 global. */
1976 if (tls_type == GOT_TLS_IE_BOTH)
1977 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1978 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1979 || (tls_type & GOT_TLS_IE))
1980 htab->srelgot->size += sizeof (Elf32_External_Rel);
1981 else if (GOT_TLS_GD_P (tls_type))
1982 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1983 else if (! GOT_TLS_GDESC_P (tls_type)
1984 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1985 || h->root.type != bfd_link_hash_undefweak)
1986 && (info->shared
1987 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1988 htab->srelgot->size += sizeof (Elf32_External_Rel);
1989 if (GOT_TLS_GDESC_P (tls_type))
1990 htab->srelplt->size += sizeof (Elf32_External_Rel);
1992 else
1993 h->got.offset = (bfd_vma) -1;
1995 if (eh->dyn_relocs == NULL)
1996 return TRUE;
1998 /* In the shared -Bsymbolic case, discard space allocated for
1999 dynamic pc-relative relocs against symbols which turn out to be
2000 defined in regular objects. For the normal shared case, discard
2001 space for pc-relative relocs that have become local due to symbol
2002 visibility changes. */
2004 if (info->shared)
2006 /* The only reloc that uses pc_count is R_386_PC32, which will
2007 appear on a call or on something like ".long foo - .". We
2008 want calls to protected symbols to resolve directly to the
2009 function rather than going via the plt. If people want
2010 function pointer comparisons to work as expected then they
2011 should avoid writing assembly like ".long foo - .". */
2012 if (SYMBOL_CALLS_LOCAL (info, h))
2014 struct elf_i386_dyn_relocs **pp;
2016 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2018 p->count -= p->pc_count;
2019 p->pc_count = 0;
2020 if (p->count == 0)
2021 *pp = p->next;
2022 else
2023 pp = &p->next;
2027 /* Also discard relocs on undefined weak syms with non-default
2028 visibility. */
2029 if (eh->dyn_relocs != NULL
2030 && h->root.type == bfd_link_hash_undefweak)
2032 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2033 eh->dyn_relocs = NULL;
2035 /* Make sure undefined weak symbols are output as a dynamic
2036 symbol in PIEs. */
2037 else if (h->dynindx == -1
2038 && !h->forced_local)
2040 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2041 return FALSE;
2045 else if (ELIMINATE_COPY_RELOCS)
2047 /* For the non-shared case, discard space for relocs against
2048 symbols which turn out to need copy relocs or are not
2049 dynamic. */
2051 if (!h->non_got_ref
2052 && ((h->def_dynamic
2053 && !h->def_regular)
2054 || (htab->elf.dynamic_sections_created
2055 && (h->root.type == bfd_link_hash_undefweak
2056 || h->root.type == bfd_link_hash_undefined))))
2058 /* Make sure this symbol is output as a dynamic symbol.
2059 Undefined weak syms won't yet be marked as dynamic. */
2060 if (h->dynindx == -1
2061 && !h->forced_local)
2063 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2064 return FALSE;
2067 /* If that succeeded, we know we'll be keeping all the
2068 relocs. */
2069 if (h->dynindx != -1)
2070 goto keep;
2073 eh->dyn_relocs = NULL;
2075 keep: ;
2078 /* Finally, allocate space. */
2079 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2081 asection *sreloc = elf_section_data (p->sec)->sreloc;
2082 sreloc->size += p->count * sizeof (Elf32_External_Rel);
2085 return TRUE;
2088 /* Find any dynamic relocs that apply to read-only sections. */
2090 static bfd_boolean
2091 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2093 struct elf_i386_link_hash_entry *eh;
2094 struct elf_i386_dyn_relocs *p;
2096 if (h->root.type == bfd_link_hash_warning)
2097 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2099 eh = (struct elf_i386_link_hash_entry *) h;
2100 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2102 asection *s = p->sec->output_section;
2104 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2106 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2108 info->flags |= DF_TEXTREL;
2110 /* Not an error, just cut short the traversal. */
2111 return FALSE;
2114 return TRUE;
2117 /* Set the sizes of the dynamic sections. */
2119 static bfd_boolean
2120 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2121 struct bfd_link_info *info)
2123 struct elf_i386_link_hash_table *htab;
2124 bfd *dynobj;
2125 asection *s;
2126 bfd_boolean relocs;
2127 bfd *ibfd;
2129 htab = elf_i386_hash_table (info);
2130 dynobj = htab->elf.dynobj;
2131 if (dynobj == NULL)
2132 abort ();
2134 if (htab->elf.dynamic_sections_created)
2136 /* Set the contents of the .interp section to the interpreter. */
2137 if (info->executable)
2139 s = bfd_get_section_by_name (dynobj, ".interp");
2140 if (s == NULL)
2141 abort ();
2142 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2143 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2147 /* Set up .got offsets for local syms, and space for local dynamic
2148 relocs. */
2149 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2151 bfd_signed_vma *local_got;
2152 bfd_signed_vma *end_local_got;
2153 char *local_tls_type;
2154 bfd_vma *local_tlsdesc_gotent;
2155 bfd_size_type locsymcount;
2156 Elf_Internal_Shdr *symtab_hdr;
2157 asection *srel;
2159 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2160 continue;
2162 for (s = ibfd->sections; s != NULL; s = s->next)
2164 struct elf_i386_dyn_relocs *p;
2166 for (p = ((struct elf_i386_dyn_relocs *)
2167 elf_section_data (s)->local_dynrel);
2168 p != NULL;
2169 p = p->next)
2171 if (!bfd_is_abs_section (p->sec)
2172 && bfd_is_abs_section (p->sec->output_section))
2174 /* Input section has been discarded, either because
2175 it is a copy of a linkonce section or due to
2176 linker script /DISCARD/, so we'll be discarding
2177 the relocs too. */
2179 else if (p->count != 0)
2181 srel = elf_section_data (p->sec)->sreloc;
2182 srel->size += p->count * sizeof (Elf32_External_Rel);
2183 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2184 info->flags |= DF_TEXTREL;
2189 local_got = elf_local_got_refcounts (ibfd);
2190 if (!local_got)
2191 continue;
2193 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2194 locsymcount = symtab_hdr->sh_info;
2195 end_local_got = local_got + locsymcount;
2196 local_tls_type = elf_i386_local_got_tls_type (ibfd);
2197 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
2198 s = htab->sgot;
2199 srel = htab->srelgot;
2200 for (; local_got < end_local_got;
2201 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2203 *local_tlsdesc_gotent = (bfd_vma) -1;
2204 if (*local_got > 0)
2206 if (GOT_TLS_GDESC_P (*local_tls_type))
2208 *local_tlsdesc_gotent = htab->sgotplt->size
2209 - elf_i386_compute_jump_table_size (htab);
2210 htab->sgotplt->size += 8;
2211 *local_got = (bfd_vma) -2;
2213 if (! GOT_TLS_GDESC_P (*local_tls_type)
2214 || GOT_TLS_GD_P (*local_tls_type))
2216 *local_got = s->size;
2217 s->size += 4;
2218 if (GOT_TLS_GD_P (*local_tls_type)
2219 || *local_tls_type == GOT_TLS_IE_BOTH)
2220 s->size += 4;
2222 if (info->shared
2223 || GOT_TLS_GD_ANY_P (*local_tls_type)
2224 || (*local_tls_type & GOT_TLS_IE))
2226 if (*local_tls_type == GOT_TLS_IE_BOTH)
2227 srel->size += 2 * sizeof (Elf32_External_Rel);
2228 else if (GOT_TLS_GD_P (*local_tls_type)
2229 || ! GOT_TLS_GDESC_P (*local_tls_type))
2230 srel->size += sizeof (Elf32_External_Rel);
2231 if (GOT_TLS_GDESC_P (*local_tls_type))
2232 htab->srelplt->size += sizeof (Elf32_External_Rel);
2235 else
2236 *local_got = (bfd_vma) -1;
2240 if (htab->tls_ldm_got.refcount > 0)
2242 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2243 relocs. */
2244 htab->tls_ldm_got.offset = htab->sgot->size;
2245 htab->sgot->size += 8;
2246 htab->srelgot->size += sizeof (Elf32_External_Rel);
2248 else
2249 htab->tls_ldm_got.offset = -1;
2251 /* Allocate global sym .plt and .got entries, and space for global
2252 sym dynamic relocs. */
2253 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
2255 /* For every jump slot reserved in the sgotplt, reloc_count is
2256 incremented. However, when we reserve space for TLS descriptors,
2257 it's not incremented, so in order to compute the space reserved
2258 for them, it suffices to multiply the reloc count by the jump
2259 slot size. */
2260 if (htab->srelplt)
2261 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
2263 /* We now have determined the sizes of the various dynamic sections.
2264 Allocate memory for them. */
2265 relocs = FALSE;
2266 for (s = dynobj->sections; s != NULL; s = s->next)
2268 bfd_boolean strip_section = TRUE;
2270 if ((s->flags & SEC_LINKER_CREATED) == 0)
2271 continue;
2273 if (s == htab->splt
2274 || s == htab->sgot
2275 || s == htab->sgotplt
2276 || s == htab->sdynbss)
2278 /* Strip this section if we don't need it; see the
2279 comment below. */
2280 /* We'd like to strip these sections if they aren't needed, but if
2281 we've exported dynamic symbols from them we must leave them.
2282 It's too late to tell BFD to get rid of the symbols. */
2284 if (htab->elf.hplt != NULL)
2285 strip_section = FALSE;
2287 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
2289 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
2290 relocs = TRUE;
2292 /* We use the reloc_count field as a counter if we need
2293 to copy relocs into the output file. */
2294 s->reloc_count = 0;
2296 else
2298 /* It's not one of our sections, so don't allocate space. */
2299 continue;
2302 if (s->size == 0)
2304 /* If we don't need this section, strip it from the
2305 output file. This is mostly to handle .rel.bss and
2306 .rel.plt. We must create both sections in
2307 create_dynamic_sections, because they must be created
2308 before the linker maps input sections to output
2309 sections. The linker does that before
2310 adjust_dynamic_symbol is called, and it is that
2311 function which decides whether anything needs to go
2312 into these sections. */
2313 if (strip_section)
2314 s->flags |= SEC_EXCLUDE;
2315 continue;
2318 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2319 continue;
2321 /* Allocate memory for the section contents. We use bfd_zalloc
2322 here in case unused entries are not reclaimed before the
2323 section's contents are written out. This should not happen,
2324 but this way if it does, we get a R_386_NONE reloc instead
2325 of garbage. */
2326 s->contents = bfd_zalloc (dynobj, s->size);
2327 if (s->contents == NULL)
2328 return FALSE;
2331 if (htab->elf.dynamic_sections_created)
2333 /* Add some entries to the .dynamic section. We fill in the
2334 values later, in elf_i386_finish_dynamic_sections, but we
2335 must add the entries now so that we get the correct size for
2336 the .dynamic section. The DT_DEBUG entry is filled in by the
2337 dynamic linker and used by the debugger. */
2338 #define add_dynamic_entry(TAG, VAL) \
2339 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2341 if (info->executable)
2343 if (!add_dynamic_entry (DT_DEBUG, 0))
2344 return FALSE;
2347 if (htab->splt->size != 0)
2349 if (!add_dynamic_entry (DT_PLTGOT, 0)
2350 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2351 || !add_dynamic_entry (DT_PLTREL, DT_REL)
2352 || !add_dynamic_entry (DT_JMPREL, 0))
2353 return FALSE;
2356 if (relocs)
2358 if (!add_dynamic_entry (DT_REL, 0)
2359 || !add_dynamic_entry (DT_RELSZ, 0)
2360 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2361 return FALSE;
2363 /* If any dynamic relocs apply to a read-only section,
2364 then we need a DT_TEXTREL entry. */
2365 if ((info->flags & DF_TEXTREL) == 0)
2366 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2367 (PTR) info);
2369 if ((info->flags & DF_TEXTREL) != 0)
2371 if (!add_dynamic_entry (DT_TEXTREL, 0))
2372 return FALSE;
2376 #undef add_dynamic_entry
2378 return TRUE;
2381 static bfd_boolean
2382 elf_i386_always_size_sections (bfd *output_bfd,
2383 struct bfd_link_info *info)
2385 asection *tls_sec = elf_hash_table (info)->tls_sec;
2387 if (tls_sec)
2389 struct elf_link_hash_entry *tlsbase;
2391 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2392 "_TLS_MODULE_BASE_",
2393 FALSE, FALSE, FALSE);
2395 if (tlsbase && tlsbase->type == STT_TLS)
2397 struct bfd_link_hash_entry *bh = NULL;
2398 const struct elf_backend_data *bed
2399 = get_elf_backend_data (output_bfd);
2401 if (!(_bfd_generic_link_add_one_symbol
2402 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2403 tls_sec, 0, NULL, FALSE,
2404 bed->collect, &bh)))
2405 return FALSE;
2406 tlsbase = (struct elf_link_hash_entry *)bh;
2407 tlsbase->def_regular = 1;
2408 tlsbase->other = STV_HIDDEN;
2409 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2413 return TRUE;
2416 /* Set the correct type for an x86 ELF section. We do this by the
2417 section name, which is a hack, but ought to work. */
2419 static bfd_boolean
2420 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2421 Elf_Internal_Shdr *hdr,
2422 asection *sec)
2424 register const char *name;
2426 name = bfd_get_section_name (abfd, sec);
2428 /* This is an ugly, but unfortunately necessary hack that is
2429 needed when producing EFI binaries on x86. It tells
2430 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2431 containing ELF relocation info. We need this hack in order to
2432 be able to generate ELF binaries that can be translated into
2433 EFI applications (which are essentially COFF objects). Those
2434 files contain a COFF ".reloc" section inside an ELFNN object,
2435 which would normally cause BFD to segfault because it would
2436 attempt to interpret this section as containing relocation
2437 entries for section "oc". With this hack enabled, ".reloc"
2438 will be treated as a normal data section, which will avoid the
2439 segfault. However, you won't be able to create an ELFNN binary
2440 with a section named "oc" that needs relocations, but that's
2441 the kind of ugly side-effects you get when detecting section
2442 types based on their names... In practice, this limitation is
2443 unlikely to bite. */
2444 if (strcmp (name, ".reloc") == 0)
2445 hdr->sh_type = SHT_PROGBITS;
2447 return TRUE;
2450 /* Return the base VMA address which should be subtracted from real addresses
2451 when resolving @dtpoff relocation.
2452 This is PT_TLS segment p_vaddr. */
2454 static bfd_vma
2455 dtpoff_base (struct bfd_link_info *info)
2457 /* If tls_sec is NULL, we should have signalled an error already. */
2458 if (elf_hash_table (info)->tls_sec == NULL)
2459 return 0;
2460 return elf_hash_table (info)->tls_sec->vma;
2463 /* Return the relocation value for @tpoff relocation
2464 if STT_TLS virtual address is ADDRESS. */
2466 static bfd_vma
2467 tpoff (struct bfd_link_info *info, bfd_vma address)
2469 struct elf_link_hash_table *htab = elf_hash_table (info);
2471 /* If tls_sec is NULL, we should have signalled an error already. */
2472 if (htab->tls_sec == NULL)
2473 return 0;
2474 return htab->tls_size + htab->tls_sec->vma - address;
2477 /* Relocate an i386 ELF section. */
2479 static bfd_boolean
2480 elf_i386_relocate_section (bfd *output_bfd,
2481 struct bfd_link_info *info,
2482 bfd *input_bfd,
2483 asection *input_section,
2484 bfd_byte *contents,
2485 Elf_Internal_Rela *relocs,
2486 Elf_Internal_Sym *local_syms,
2487 asection **local_sections)
2489 struct elf_i386_link_hash_table *htab;
2490 Elf_Internal_Shdr *symtab_hdr;
2491 struct elf_link_hash_entry **sym_hashes;
2492 bfd_vma *local_got_offsets;
2493 bfd_vma *local_tlsdesc_gotents;
2494 Elf_Internal_Rela *rel;
2495 Elf_Internal_Rela *relend;
2497 htab = elf_i386_hash_table (info);
2498 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2499 sym_hashes = elf_sym_hashes (input_bfd);
2500 local_got_offsets = elf_local_got_offsets (input_bfd);
2501 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
2503 rel = relocs;
2504 relend = relocs + input_section->reloc_count;
2505 for (; rel < relend; rel++)
2507 unsigned int r_type;
2508 reloc_howto_type *howto;
2509 unsigned long r_symndx;
2510 struct elf_link_hash_entry *h;
2511 Elf_Internal_Sym *sym;
2512 asection *sec;
2513 bfd_vma off, offplt;
2514 bfd_vma relocation;
2515 bfd_boolean unresolved_reloc;
2516 bfd_reloc_status_type r;
2517 unsigned int indx;
2518 int tls_type;
2520 r_type = ELF32_R_TYPE (rel->r_info);
2521 if (r_type == R_386_GNU_VTINHERIT
2522 || r_type == R_386_GNU_VTENTRY)
2523 continue;
2525 if ((indx = r_type) >= R_386_standard
2526 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2527 >= R_386_ext - R_386_standard)
2528 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2529 >= R_386_tls - R_386_ext))
2531 (*_bfd_error_handler)
2532 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2533 input_bfd, input_section, r_type);
2534 bfd_set_error (bfd_error_bad_value);
2535 return FALSE;
2537 howto = elf_howto_table + indx;
2539 r_symndx = ELF32_R_SYM (rel->r_info);
2540 h = NULL;
2541 sym = NULL;
2542 sec = NULL;
2543 unresolved_reloc = FALSE;
2544 if (r_symndx < symtab_hdr->sh_info)
2546 sym = local_syms + r_symndx;
2547 sec = local_sections[r_symndx];
2548 relocation = (sec->output_section->vma
2549 + sec->output_offset
2550 + sym->st_value);
2552 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2553 && ((sec->flags & SEC_MERGE) != 0
2554 || (info->relocatable
2555 && sec->output_offset != 0)))
2557 bfd_vma addend;
2558 bfd_byte *where = contents + rel->r_offset;
2560 switch (howto->size)
2562 case 0:
2563 addend = bfd_get_8 (input_bfd, where);
2564 if (howto->pc_relative)
2566 addend = (addend ^ 0x80) - 0x80;
2567 addend += 1;
2569 break;
2570 case 1:
2571 addend = bfd_get_16 (input_bfd, where);
2572 if (howto->pc_relative)
2574 addend = (addend ^ 0x8000) - 0x8000;
2575 addend += 2;
2577 break;
2578 case 2:
2579 addend = bfd_get_32 (input_bfd, where);
2580 if (howto->pc_relative)
2582 addend = (addend ^ 0x80000000) - 0x80000000;
2583 addend += 4;
2585 break;
2586 default:
2587 abort ();
2590 if (info->relocatable)
2591 addend += sec->output_offset;
2592 else
2594 asection *msec = sec;
2595 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2596 addend);
2597 addend -= relocation;
2598 addend += msec->output_section->vma + msec->output_offset;
2601 switch (howto->size)
2603 case 0:
2604 /* FIXME: overflow checks. */
2605 if (howto->pc_relative)
2606 addend -= 1;
2607 bfd_put_8 (input_bfd, addend, where);
2608 break;
2609 case 1:
2610 if (howto->pc_relative)
2611 addend -= 2;
2612 bfd_put_16 (input_bfd, addend, where);
2613 break;
2614 case 2:
2615 if (howto->pc_relative)
2616 addend -= 4;
2617 bfd_put_32 (input_bfd, addend, where);
2618 break;
2622 else
2624 bfd_boolean warned;
2626 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2627 r_symndx, symtab_hdr, sym_hashes,
2628 h, sec, relocation,
2629 unresolved_reloc, warned);
2632 if (sec != NULL && elf_discarded_section (sec))
2634 /* For relocs against symbols from removed linkonce sections,
2635 or sections discarded by a linker script, we just want the
2636 section contents zeroed. Avoid any special processing. */
2637 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2638 rel->r_info = 0;
2639 rel->r_addend = 0;
2640 continue;
2643 if (info->relocatable)
2644 continue;
2646 switch (r_type)
2648 case R_386_GOT32:
2649 /* Relocation is to the entry for this symbol in the global
2650 offset table. */
2651 if (htab->sgot == NULL)
2652 abort ();
2654 if (h != NULL)
2656 bfd_boolean dyn;
2658 off = h->got.offset;
2659 dyn = htab->elf.dynamic_sections_created;
2660 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2661 || (info->shared
2662 && SYMBOL_REFERENCES_LOCAL (info, h))
2663 || (ELF_ST_VISIBILITY (h->other)
2664 && h->root.type == bfd_link_hash_undefweak))
2666 /* This is actually a static link, or it is a
2667 -Bsymbolic link and the symbol is defined
2668 locally, or the symbol was forced to be local
2669 because of a version file. We must initialize
2670 this entry in the global offset table. Since the
2671 offset must always be a multiple of 4, we use the
2672 least significant bit to record whether we have
2673 initialized it already.
2675 When doing a dynamic link, we create a .rel.got
2676 relocation entry to initialize the value. This
2677 is done in the finish_dynamic_symbol routine. */
2678 if ((off & 1) != 0)
2679 off &= ~1;
2680 else
2682 bfd_put_32 (output_bfd, relocation,
2683 htab->sgot->contents + off);
2684 h->got.offset |= 1;
2687 else
2688 unresolved_reloc = FALSE;
2690 else
2692 if (local_got_offsets == NULL)
2693 abort ();
2695 off = local_got_offsets[r_symndx];
2697 /* The offset must always be a multiple of 4. We use
2698 the least significant bit to record whether we have
2699 already generated the necessary reloc. */
2700 if ((off & 1) != 0)
2701 off &= ~1;
2702 else
2704 bfd_put_32 (output_bfd, relocation,
2705 htab->sgot->contents + off);
2707 if (info->shared)
2709 asection *s;
2710 Elf_Internal_Rela outrel;
2711 bfd_byte *loc;
2713 s = htab->srelgot;
2714 if (s == NULL)
2715 abort ();
2717 outrel.r_offset = (htab->sgot->output_section->vma
2718 + htab->sgot->output_offset
2719 + off);
2720 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2721 loc = s->contents;
2722 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
2723 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2726 local_got_offsets[r_symndx] |= 1;
2730 if (off >= (bfd_vma) -2)
2731 abort ();
2733 relocation = htab->sgot->output_section->vma
2734 + htab->sgot->output_offset + off
2735 - htab->sgotplt->output_section->vma
2736 - htab->sgotplt->output_offset;
2737 break;
2739 case R_386_GOTOFF:
2740 /* Relocation is relative to the start of the global offset
2741 table. */
2743 /* Check to make sure it isn't a protected function symbol
2744 for shared library since it may not be local when used
2745 as function address. */
2746 if (info->shared
2747 && !info->executable
2748 && h
2749 && h->def_regular
2750 && h->type == STT_FUNC
2751 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2753 (*_bfd_error_handler)
2754 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
2755 input_bfd, h->root.root.string);
2756 bfd_set_error (bfd_error_bad_value);
2757 return FALSE;
2760 /* Note that sgot is not involved in this
2761 calculation. We always want the start of .got.plt. If we
2762 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2763 permitted by the ABI, we might have to change this
2764 calculation. */
2765 relocation -= htab->sgotplt->output_section->vma
2766 + htab->sgotplt->output_offset;
2767 break;
2769 case R_386_GOTPC:
2770 /* Use global offset table as symbol value. */
2771 relocation = htab->sgotplt->output_section->vma
2772 + htab->sgotplt->output_offset;
2773 unresolved_reloc = FALSE;
2774 break;
2776 case R_386_PLT32:
2777 /* Relocation is to the entry for this symbol in the
2778 procedure linkage table. */
2780 /* Resolve a PLT32 reloc against a local symbol directly,
2781 without using the procedure linkage table. */
2782 if (h == NULL)
2783 break;
2785 if (h->plt.offset == (bfd_vma) -1
2786 || htab->splt == NULL)
2788 /* We didn't make a PLT entry for this symbol. This
2789 happens when statically linking PIC code, or when
2790 using -Bsymbolic. */
2791 break;
2794 relocation = (htab->splt->output_section->vma
2795 + htab->splt->output_offset
2796 + h->plt.offset);
2797 unresolved_reloc = FALSE;
2798 break;
2800 case R_386_32:
2801 case R_386_PC32:
2802 if ((input_section->flags & SEC_ALLOC) == 0)
2803 break;
2805 if ((info->shared
2806 && (h == NULL
2807 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2808 || h->root.type != bfd_link_hash_undefweak)
2809 && (r_type != R_386_PC32
2810 || !SYMBOL_CALLS_LOCAL (info, h)))
2811 || (ELIMINATE_COPY_RELOCS
2812 && !info->shared
2813 && h != NULL
2814 && h->dynindx != -1
2815 && !h->non_got_ref
2816 && ((h->def_dynamic
2817 && !h->def_regular)
2818 || h->root.type == bfd_link_hash_undefweak
2819 || h->root.type == bfd_link_hash_undefined)))
2821 Elf_Internal_Rela outrel;
2822 bfd_byte *loc;
2823 bfd_boolean skip, relocate;
2824 asection *sreloc;
2826 /* When generating a shared object, these relocations
2827 are copied into the output file to be resolved at run
2828 time. */
2830 skip = FALSE;
2831 relocate = FALSE;
2833 outrel.r_offset =
2834 _bfd_elf_section_offset (output_bfd, info, input_section,
2835 rel->r_offset);
2836 if (outrel.r_offset == (bfd_vma) -1)
2837 skip = TRUE;
2838 else if (outrel.r_offset == (bfd_vma) -2)
2839 skip = TRUE, relocate = TRUE;
2840 outrel.r_offset += (input_section->output_section->vma
2841 + input_section->output_offset);
2843 if (skip)
2844 memset (&outrel, 0, sizeof outrel);
2845 else if (h != NULL
2846 && h->dynindx != -1
2847 && (r_type == R_386_PC32
2848 || !info->shared
2849 || !SYMBOLIC_BIND (info, h)
2850 || !h->def_regular))
2851 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2852 else
2854 /* This symbol is local, or marked to become local. */
2855 relocate = TRUE;
2856 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2859 sreloc = elf_section_data (input_section)->sreloc;
2860 if (sreloc == NULL)
2861 abort ();
2863 loc = sreloc->contents;
2864 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2865 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2867 /* If this reloc is against an external symbol, we do
2868 not want to fiddle with the addend. Otherwise, we
2869 need to include the symbol value so that it becomes
2870 an addend for the dynamic reloc. */
2871 if (! relocate)
2872 continue;
2874 break;
2876 case R_386_TLS_IE:
2877 if (info->shared)
2879 Elf_Internal_Rela outrel;
2880 bfd_byte *loc;
2881 asection *sreloc;
2883 outrel.r_offset = rel->r_offset
2884 + input_section->output_section->vma
2885 + input_section->output_offset;
2886 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2887 sreloc = elf_section_data (input_section)->sreloc;
2888 if (sreloc == NULL)
2889 abort ();
2890 loc = sreloc->contents;
2891 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2892 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2894 /* Fall through */
2896 case R_386_TLS_GD:
2897 case R_386_TLS_GOTDESC:
2898 case R_386_TLS_DESC_CALL:
2899 case R_386_TLS_IE_32:
2900 case R_386_TLS_GOTIE:
2901 tls_type = GOT_UNKNOWN;
2902 if (h == NULL && local_got_offsets)
2903 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2904 else if (h != NULL)
2905 tls_type = elf_i386_hash_entry(h)->tls_type;
2906 if (tls_type == GOT_TLS_IE)
2907 tls_type = GOT_TLS_IE_NEG;
2909 if (! elf_i386_tls_transition (info, input_bfd,
2910 input_section, contents,
2911 symtab_hdr, sym_hashes,
2912 &r_type, tls_type, rel,
2913 relend, h))
2914 return FALSE;
2916 if (r_type == R_386_TLS_LE_32)
2918 BFD_ASSERT (! unresolved_reloc);
2919 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
2921 unsigned int type;
2922 bfd_vma roff;
2924 /* GD->LE transition. */
2925 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2926 if (type == 0x04)
2928 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2929 Change it into:
2930 movl %gs:0, %eax; subl $foo@tpoff, %eax
2931 (6 byte form of subl). */
2932 memcpy (contents + rel->r_offset - 3,
2933 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2934 roff = rel->r_offset + 5;
2936 else
2938 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2939 Change it into:
2940 movl %gs:0, %eax; subl $foo@tpoff, %eax
2941 (6 byte form of subl). */
2942 memcpy (contents + rel->r_offset - 2,
2943 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2944 roff = rel->r_offset + 6;
2946 bfd_put_32 (output_bfd, tpoff (info, relocation),
2947 contents + roff);
2948 /* Skip R_386_PC32/R_386_PLT32. */
2949 rel++;
2950 continue;
2952 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
2954 /* GDesc -> LE transition.
2955 It's originally something like:
2956 leal x@tlsdesc(%ebx), %eax
2958 leal x@ntpoff, %eax
2960 Registers other than %eax may be set up here. */
2962 unsigned int val;
2963 bfd_vma roff;
2965 roff = rel->r_offset;
2966 val = bfd_get_8 (input_bfd, contents + roff - 1);
2968 /* Now modify the instruction as appropriate. */
2969 /* aoliva FIXME: remove the above and xor the byte
2970 below with 0x86. */
2971 bfd_put_8 (output_bfd, val ^ 0x86,
2972 contents + roff - 1);
2973 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2974 contents + roff);
2975 continue;
2977 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
2979 /* GDesc -> LE transition.
2980 It's originally:
2981 call *(%eax)
2982 Turn it into:
2983 xchg %ax,%ax */
2985 bfd_vma roff;
2987 roff = rel->r_offset;
2988 bfd_put_8 (output_bfd, 0x66, contents + roff);
2989 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2990 continue;
2992 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
2994 unsigned int val;
2996 /* IE->LE transition:
2997 Originally it can be one of:
2998 movl foo, %eax
2999 movl foo, %reg
3000 addl foo, %reg
3001 We change it into:
3002 movl $foo, %eax
3003 movl $foo, %reg
3004 addl $foo, %reg. */
3005 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3006 if (val == 0xa1)
3008 /* movl foo, %eax. */
3009 bfd_put_8 (output_bfd, 0xb8,
3010 contents + rel->r_offset - 1);
3012 else
3014 unsigned int type;
3016 type = bfd_get_8 (input_bfd,
3017 contents + rel->r_offset - 2);
3018 switch (type)
3020 case 0x8b:
3021 /* movl */
3022 bfd_put_8 (output_bfd, 0xc7,
3023 contents + rel->r_offset - 2);
3024 bfd_put_8 (output_bfd,
3025 0xc0 | ((val >> 3) & 7),
3026 contents + rel->r_offset - 1);
3027 break;
3028 case 0x03:
3029 /* addl */
3030 bfd_put_8 (output_bfd, 0x81,
3031 contents + rel->r_offset - 2);
3032 bfd_put_8 (output_bfd,
3033 0xc0 | ((val >> 3) & 7),
3034 contents + rel->r_offset - 1);
3035 break;
3036 default:
3037 BFD_FAIL ();
3038 break;
3041 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3042 contents + rel->r_offset);
3043 continue;
3045 else
3047 unsigned int val, type;
3049 /* {IE_32,GOTIE}->LE transition:
3050 Originally it can be one of:
3051 subl foo(%reg1), %reg2
3052 movl foo(%reg1), %reg2
3053 addl foo(%reg1), %reg2
3054 We change it into:
3055 subl $foo, %reg2
3056 movl $foo, %reg2 (6 byte form)
3057 addl $foo, %reg2. */
3058 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3059 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3060 if (type == 0x8b)
3062 /* movl */
3063 bfd_put_8 (output_bfd, 0xc7,
3064 contents + rel->r_offset - 2);
3065 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3066 contents + rel->r_offset - 1);
3068 else if (type == 0x2b)
3070 /* subl */
3071 bfd_put_8 (output_bfd, 0x81,
3072 contents + rel->r_offset - 2);
3073 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3074 contents + rel->r_offset - 1);
3076 else if (type == 0x03)
3078 /* addl */
3079 bfd_put_8 (output_bfd, 0x81,
3080 contents + rel->r_offset - 2);
3081 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3082 contents + rel->r_offset - 1);
3084 else
3085 BFD_FAIL ();
3086 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
3087 bfd_put_32 (output_bfd, -tpoff (info, relocation),
3088 contents + rel->r_offset);
3089 else
3090 bfd_put_32 (output_bfd, tpoff (info, relocation),
3091 contents + rel->r_offset);
3092 continue;
3096 if (htab->sgot == NULL)
3097 abort ();
3099 if (h != NULL)
3101 off = h->got.offset;
3102 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
3104 else
3106 if (local_got_offsets == NULL)
3107 abort ();
3109 off = local_got_offsets[r_symndx];
3110 offplt = local_tlsdesc_gotents[r_symndx];
3113 if ((off & 1) != 0)
3114 off &= ~1;
3115 else
3117 Elf_Internal_Rela outrel;
3118 bfd_byte *loc;
3119 int dr_type, indx;
3120 asection *sreloc;
3122 if (htab->srelgot == NULL)
3123 abort ();
3125 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3127 if (GOT_TLS_GDESC_P (tls_type))
3129 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3130 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3131 <= htab->sgotplt->size);
3132 outrel.r_offset = (htab->sgotplt->output_section->vma
3133 + htab->sgotplt->output_offset
3134 + offplt
3135 + htab->sgotplt_jump_table_size);
3136 sreloc = htab->srelplt;
3137 loc = sreloc->contents;
3138 loc += (htab->next_tls_desc_index++
3139 * sizeof (Elf32_External_Rel));
3140 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3141 <= sreloc->contents + sreloc->size);
3142 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3143 if (indx == 0)
3145 BFD_ASSERT (! unresolved_reloc);
3146 bfd_put_32 (output_bfd,
3147 relocation - dtpoff_base (info),
3148 htab->sgotplt->contents + offplt
3149 + htab->sgotplt_jump_table_size + 4);
3151 else
3153 bfd_put_32 (output_bfd, 0,
3154 htab->sgotplt->contents + offplt
3155 + htab->sgotplt_jump_table_size + 4);
3159 sreloc = htab->srelgot;
3161 outrel.r_offset = (htab->sgot->output_section->vma
3162 + htab->sgot->output_offset + off);
3164 if (GOT_TLS_GD_P (tls_type))
3165 dr_type = R_386_TLS_DTPMOD32;
3166 else if (GOT_TLS_GDESC_P (tls_type))
3167 goto dr_done;
3168 else if (tls_type == GOT_TLS_IE_POS)
3169 dr_type = R_386_TLS_TPOFF;
3170 else
3171 dr_type = R_386_TLS_TPOFF32;
3173 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3174 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3175 htab->sgot->contents + off);
3176 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3177 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
3178 htab->sgot->contents + off);
3179 else if (dr_type != R_386_TLS_DESC)
3180 bfd_put_32 (output_bfd, 0,
3181 htab->sgot->contents + off);
3182 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3184 loc = sreloc->contents;
3185 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3186 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3187 <= sreloc->contents + sreloc->size);
3188 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3190 if (GOT_TLS_GD_P (tls_type))
3192 if (indx == 0)
3194 BFD_ASSERT (! unresolved_reloc);
3195 bfd_put_32 (output_bfd,
3196 relocation - dtpoff_base (info),
3197 htab->sgot->contents + off + 4);
3199 else
3201 bfd_put_32 (output_bfd, 0,
3202 htab->sgot->contents + off + 4);
3203 outrel.r_info = ELF32_R_INFO (indx,
3204 R_386_TLS_DTPOFF32);
3205 outrel.r_offset += 4;
3206 sreloc->reloc_count++;
3207 loc += sizeof (Elf32_External_Rel);
3208 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3209 <= sreloc->contents + sreloc->size);
3210 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3213 else if (tls_type == GOT_TLS_IE_BOTH)
3215 bfd_put_32 (output_bfd,
3216 indx == 0 ? relocation - dtpoff_base (info) : 0,
3217 htab->sgot->contents + off + 4);
3218 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3219 outrel.r_offset += 4;
3220 sreloc->reloc_count++;
3221 loc += sizeof (Elf32_External_Rel);
3222 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3225 dr_done:
3226 if (h != NULL)
3227 h->got.offset |= 1;
3228 else
3229 local_got_offsets[r_symndx] |= 1;
3232 if (off >= (bfd_vma) -2
3233 && ! GOT_TLS_GDESC_P (tls_type))
3234 abort ();
3235 if (r_type == R_386_TLS_GOTDESC
3236 || r_type == R_386_TLS_DESC_CALL)
3238 relocation = htab->sgotplt_jump_table_size + offplt;
3239 unresolved_reloc = FALSE;
3241 else if (r_type == ELF32_R_TYPE (rel->r_info))
3243 bfd_vma g_o_t = htab->sgotplt->output_section->vma
3244 + htab->sgotplt->output_offset;
3245 relocation = htab->sgot->output_section->vma
3246 + htab->sgot->output_offset + off - g_o_t;
3247 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3248 && tls_type == GOT_TLS_IE_BOTH)
3249 relocation += 4;
3250 if (r_type == R_386_TLS_IE)
3251 relocation += g_o_t;
3252 unresolved_reloc = FALSE;
3254 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3256 unsigned int val, type;
3257 bfd_vma roff;
3259 /* GD->IE transition. */
3260 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3261 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3262 if (type == 0x04)
3264 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3265 Change it into:
3266 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3267 val >>= 3;
3268 roff = rel->r_offset - 3;
3270 else
3272 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3273 Change it into:
3274 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3275 roff = rel->r_offset - 2;
3277 memcpy (contents + roff,
3278 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3279 contents[roff + 7] = 0x80 | (val & 7);
3280 /* If foo is used only with foo@gotntpoff(%reg) and
3281 foo@indntpoff, but not with foo@gottpoff(%reg), change
3282 subl $foo@gottpoff(%reg), %eax
3283 into:
3284 addl $foo@gotntpoff(%reg), %eax. */
3285 if (tls_type == GOT_TLS_IE_POS)
3286 contents[roff + 6] = 0x03;
3287 bfd_put_32 (output_bfd,
3288 htab->sgot->output_section->vma
3289 + htab->sgot->output_offset + off
3290 - htab->sgotplt->output_section->vma
3291 - htab->sgotplt->output_offset,
3292 contents + roff + 8);
3293 /* Skip R_386_PLT32. */
3294 rel++;
3295 continue;
3297 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3299 /* GDesc -> IE transition.
3300 It's originally something like:
3301 leal x@tlsdesc(%ebx), %eax
3303 Change it to:
3304 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3306 movl x@gottpoff(%ebx), %eax # before negl %eax
3308 Registers other than %eax may be set up here. */
3310 bfd_vma roff;
3312 /* First, make sure it's a leal adding ebx to a 32-bit
3313 offset into any register, although it's probably
3314 almost always going to be eax. */
3315 roff = rel->r_offset;
3317 /* Now modify the instruction as appropriate. */
3318 /* To turn a leal into a movl in the form we use it, it
3319 suffices to change the first byte from 0x8d to 0x8b.
3320 aoliva FIXME: should we decide to keep the leal, all
3321 we have to do is remove the statement below, and
3322 adjust the relaxation of R_386_TLS_DESC_CALL. */
3323 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3325 if (tls_type == GOT_TLS_IE_BOTH)
3326 off += 4;
3328 bfd_put_32 (output_bfd,
3329 htab->sgot->output_section->vma
3330 + htab->sgot->output_offset + off
3331 - htab->sgotplt->output_section->vma
3332 - htab->sgotplt->output_offset,
3333 contents + roff);
3334 continue;
3336 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3338 /* GDesc -> IE transition.
3339 It's originally:
3340 call *(%eax)
3342 Change it to:
3343 xchg %ax,%ax
3345 negl %eax
3346 depending on how we transformed the TLS_GOTDESC above.
3349 bfd_vma roff;
3351 roff = rel->r_offset;
3353 /* Now modify the instruction as appropriate. */
3354 if (tls_type != GOT_TLS_IE_NEG)
3356 /* xchg %ax,%ax */
3357 bfd_put_8 (output_bfd, 0x66, contents + roff);
3358 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3360 else
3362 /* negl %eax */
3363 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3364 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3367 continue;
3369 else
3370 BFD_ASSERT (FALSE);
3371 break;
3373 case R_386_TLS_LDM:
3374 if (! elf_i386_tls_transition (info, input_bfd,
3375 input_section, contents,
3376 symtab_hdr, sym_hashes,
3377 &r_type, GOT_UNKNOWN, rel,
3378 relend, h))
3379 return FALSE;
3381 if (r_type != R_386_TLS_LDM)
3383 /* LD->LE transition:
3384 leal foo(%reg), %eax; call ___tls_get_addr.
3385 We change it into:
3386 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
3387 BFD_ASSERT (r_type == R_386_TLS_LE_32);
3388 memcpy (contents + rel->r_offset - 2,
3389 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3390 /* Skip R_386_PC32/R_386_PLT32. */
3391 rel++;
3392 continue;
3395 if (htab->sgot == NULL)
3396 abort ();
3398 off = htab->tls_ldm_got.offset;
3399 if (off & 1)
3400 off &= ~1;
3401 else
3403 Elf_Internal_Rela outrel;
3404 bfd_byte *loc;
3406 if (htab->srelgot == NULL)
3407 abort ();
3409 outrel.r_offset = (htab->sgot->output_section->vma
3410 + htab->sgot->output_offset + off);
3412 bfd_put_32 (output_bfd, 0,
3413 htab->sgot->contents + off);
3414 bfd_put_32 (output_bfd, 0,
3415 htab->sgot->contents + off + 4);
3416 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3417 loc = htab->srelgot->contents;
3418 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3419 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3420 htab->tls_ldm_got.offset |= 1;
3422 relocation = htab->sgot->output_section->vma
3423 + htab->sgot->output_offset + off
3424 - htab->sgotplt->output_section->vma
3425 - htab->sgotplt->output_offset;
3426 unresolved_reloc = FALSE;
3427 break;
3429 case R_386_TLS_LDO_32:
3430 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3431 relocation -= dtpoff_base (info);
3432 else
3433 /* When converting LDO to LE, we must negate. */
3434 relocation = -tpoff (info, relocation);
3435 break;
3437 case R_386_TLS_LE_32:
3438 case R_386_TLS_LE:
3439 if (info->shared)
3441 Elf_Internal_Rela outrel;
3442 asection *sreloc;
3443 bfd_byte *loc;
3444 int indx;
3446 outrel.r_offset = rel->r_offset
3447 + input_section->output_section->vma
3448 + input_section->output_offset;
3449 if (h != NULL && h->dynindx != -1)
3450 indx = h->dynindx;
3451 else
3452 indx = 0;
3453 if (r_type == R_386_TLS_LE_32)
3454 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3455 else
3456 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3457 sreloc = elf_section_data (input_section)->sreloc;
3458 if (sreloc == NULL)
3459 abort ();
3460 loc = sreloc->contents;
3461 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3462 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3463 if (indx)
3464 continue;
3465 else if (r_type == R_386_TLS_LE_32)
3466 relocation = dtpoff_base (info) - relocation;
3467 else
3468 relocation -= dtpoff_base (info);
3470 else if (r_type == R_386_TLS_LE_32)
3471 relocation = tpoff (info, relocation);
3472 else
3473 relocation = -tpoff (info, relocation);
3474 break;
3476 default:
3477 break;
3480 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3481 because such sections are not SEC_ALLOC and thus ld.so will
3482 not process them. */
3483 if (unresolved_reloc
3484 && !((input_section->flags & SEC_DEBUGGING) != 0
3485 && h->def_dynamic))
3487 (*_bfd_error_handler)
3488 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3489 input_bfd,
3490 input_section,
3491 (long) rel->r_offset,
3492 howto->name,
3493 h->root.root.string);
3494 return FALSE;
3497 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3498 contents, rel->r_offset,
3499 relocation, 0);
3501 if (r != bfd_reloc_ok)
3503 const char *name;
3505 if (h != NULL)
3506 name = h->root.root.string;
3507 else
3509 name = bfd_elf_string_from_elf_section (input_bfd,
3510 symtab_hdr->sh_link,
3511 sym->st_name);
3512 if (name == NULL)
3513 return FALSE;
3514 if (*name == '\0')
3515 name = bfd_section_name (input_bfd, sec);
3518 if (r == bfd_reloc_overflow)
3520 if (! ((*info->callbacks->reloc_overflow)
3521 (info, (h ? &h->root : NULL), name, howto->name,
3522 (bfd_vma) 0, input_bfd, input_section,
3523 rel->r_offset)))
3524 return FALSE;
3526 else
3528 (*_bfd_error_handler)
3529 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3530 input_bfd, input_section,
3531 (long) rel->r_offset, name, (int) r);
3532 return FALSE;
3537 return TRUE;
3540 /* Finish up dynamic symbol handling. We set the contents of various
3541 dynamic sections here. */
3543 static bfd_boolean
3544 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3545 struct bfd_link_info *info,
3546 struct elf_link_hash_entry *h,
3547 Elf_Internal_Sym *sym)
3549 struct elf_i386_link_hash_table *htab;
3551 htab = elf_i386_hash_table (info);
3553 if (h->plt.offset != (bfd_vma) -1)
3555 bfd_vma plt_index;
3556 bfd_vma got_offset;
3557 Elf_Internal_Rela rel;
3558 bfd_byte *loc;
3560 /* This symbol has an entry in the procedure linkage table. Set
3561 it up. */
3563 if (h->dynindx == -1
3564 || htab->splt == NULL
3565 || htab->sgotplt == NULL
3566 || htab->srelplt == NULL)
3567 abort ();
3569 /* Get the index in the procedure linkage table which
3570 corresponds to this symbol. This is the index of this symbol
3571 in all the symbols for which we are making plt entries. The
3572 first entry in the procedure linkage table is reserved. */
3573 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3575 /* Get the offset into the .got table of the entry that
3576 corresponds to this function. Each .got entry is 4 bytes.
3577 The first three are reserved. */
3578 got_offset = (plt_index + 3) * 4;
3580 /* Fill in the entry in the procedure linkage table. */
3581 if (! info->shared)
3583 memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
3584 PLT_ENTRY_SIZE);
3585 bfd_put_32 (output_bfd,
3586 (htab->sgotplt->output_section->vma
3587 + htab->sgotplt->output_offset
3588 + got_offset),
3589 htab->splt->contents + h->plt.offset + 2);
3591 if (htab->is_vxworks)
3593 int s, k, reloc_index;
3595 /* Create the R_386_32 relocation referencing the GOT
3596 for this PLT entry. */
3598 /* S: Current slot number (zero-based). */
3599 s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3600 /* K: Number of relocations for PLTResolve. */
3601 if (info->shared)
3602 k = PLTRESOLVE_RELOCS_SHLIB;
3603 else
3604 k = PLTRESOLVE_RELOCS;
3605 /* Skip the PLTresolve relocations, and the relocations for
3606 the other PLT slots. */
3607 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3608 loc = (htab->srelplt2->contents + reloc_index
3609 * sizeof (Elf32_External_Rel));
3611 rel.r_offset = (htab->splt->output_section->vma
3612 + htab->splt->output_offset
3613 + h->plt.offset + 2),
3614 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3615 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3617 /* Create the R_386_32 relocation referencing the beginning of
3618 the PLT for this GOT entry. */
3619 rel.r_offset = (htab->sgotplt->output_section->vma
3620 + htab->sgotplt->output_offset
3621 + got_offset);
3622 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3623 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3624 loc + sizeof (Elf32_External_Rel));
3627 else
3629 memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
3630 PLT_ENTRY_SIZE);
3631 bfd_put_32 (output_bfd, got_offset,
3632 htab->splt->contents + h->plt.offset + 2);
3635 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
3636 htab->splt->contents + h->plt.offset + 7);
3637 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3638 htab->splt->contents + h->plt.offset + 12);
3640 /* Fill in the entry in the global offset table. */
3641 bfd_put_32 (output_bfd,
3642 (htab->splt->output_section->vma
3643 + htab->splt->output_offset
3644 + h->plt.offset
3645 + 6),
3646 htab->sgotplt->contents + got_offset);
3648 /* Fill in the entry in the .rel.plt section. */
3649 rel.r_offset = (htab->sgotplt->output_section->vma
3650 + htab->sgotplt->output_offset
3651 + got_offset);
3652 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3653 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
3654 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3656 if (!h->def_regular)
3658 /* Mark the symbol as undefined, rather than as defined in
3659 the .plt section. Leave the value if there were any
3660 relocations where pointer equality matters (this is a clue
3661 for the dynamic linker, to make function pointer
3662 comparisons work between an application and shared
3663 library), otherwise set it to zero. If a function is only
3664 called from a binary, there is no need to slow down
3665 shared libraries because of that. */
3666 sym->st_shndx = SHN_UNDEF;
3667 if (!h->pointer_equality_needed)
3668 sym->st_value = 0;
3672 if (h->got.offset != (bfd_vma) -1
3673 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
3674 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
3676 Elf_Internal_Rela rel;
3677 bfd_byte *loc;
3679 /* This symbol has an entry in the global offset table. Set it
3680 up. */
3682 if (htab->sgot == NULL || htab->srelgot == NULL)
3683 abort ();
3685 rel.r_offset = (htab->sgot->output_section->vma
3686 + htab->sgot->output_offset
3687 + (h->got.offset & ~(bfd_vma) 1));
3689 /* If this is a static link, or it is a -Bsymbolic link and the
3690 symbol is defined locally or was forced to be local because
3691 of a version file, we just want to emit a RELATIVE reloc.
3692 The entry in the global offset table will already have been
3693 initialized in the relocate_section function. */
3694 if (info->shared
3695 && SYMBOL_REFERENCES_LOCAL (info, h))
3697 BFD_ASSERT((h->got.offset & 1) != 0);
3698 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3700 else
3702 BFD_ASSERT((h->got.offset & 1) == 0);
3703 bfd_put_32 (output_bfd, (bfd_vma) 0,
3704 htab->sgot->contents + h->got.offset);
3705 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3708 loc = htab->srelgot->contents;
3709 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3710 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3713 if (h->needs_copy)
3715 Elf_Internal_Rela rel;
3716 bfd_byte *loc;
3718 /* This symbol needs a copy reloc. Set it up. */
3720 if (h->dynindx == -1
3721 || (h->root.type != bfd_link_hash_defined
3722 && h->root.type != bfd_link_hash_defweak)
3723 || htab->srelbss == NULL)
3724 abort ();
3726 rel.r_offset = (h->root.u.def.value
3727 + h->root.u.def.section->output_section->vma
3728 + h->root.u.def.section->output_offset);
3729 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3730 loc = htab->srelbss->contents;
3731 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
3732 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3735 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
3736 On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
3737 is relative to the ".got" section. */
3738 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3739 || (!htab->is_vxworks && h == htab->elf.hgot))
3740 sym->st_shndx = SHN_ABS;
3742 return TRUE;
3745 /* Used to decide how to sort relocs in an optimal manner for the
3746 dynamic linker, before writing them out. */
3748 static enum elf_reloc_type_class
3749 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
3751 switch (ELF32_R_TYPE (rela->r_info))
3753 case R_386_RELATIVE:
3754 return reloc_class_relative;
3755 case R_386_JUMP_SLOT:
3756 return reloc_class_plt;
3757 case R_386_COPY:
3758 return reloc_class_copy;
3759 default:
3760 return reloc_class_normal;
3764 /* Finish up the dynamic sections. */
3766 static bfd_boolean
3767 elf_i386_finish_dynamic_sections (bfd *output_bfd,
3768 struct bfd_link_info *info)
3770 struct elf_i386_link_hash_table *htab;
3771 bfd *dynobj;
3772 asection *sdyn;
3774 htab = elf_i386_hash_table (info);
3775 dynobj = htab->elf.dynobj;
3776 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3778 if (htab->elf.dynamic_sections_created)
3780 Elf32_External_Dyn *dyncon, *dynconend;
3782 if (sdyn == NULL || htab->sgot == NULL)
3783 abort ();
3785 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3786 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3787 for (; dyncon < dynconend; dyncon++)
3789 Elf_Internal_Dyn dyn;
3790 asection *s;
3792 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3794 switch (dyn.d_tag)
3796 default:
3797 continue;
3799 case DT_PLTGOT:
3800 s = htab->sgotplt;
3801 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3802 break;
3804 case DT_JMPREL:
3805 s = htab->srelplt;
3806 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3807 break;
3809 case DT_PLTRELSZ:
3810 s = htab->srelplt;
3811 dyn.d_un.d_val = s->size;
3812 break;
3814 case DT_RELSZ:
3815 /* My reading of the SVR4 ABI indicates that the
3816 procedure linkage table relocs (DT_JMPREL) should be
3817 included in the overall relocs (DT_REL). This is
3818 what Solaris does. However, UnixWare can not handle
3819 that case. Therefore, we override the DT_RELSZ entry
3820 here to make it not include the JMPREL relocs. */
3821 s = htab->srelplt;
3822 if (s == NULL)
3823 continue;
3824 dyn.d_un.d_val -= s->size;
3825 break;
3827 case DT_REL:
3828 /* We may not be using the standard ELF linker script.
3829 If .rel.plt is the first .rel section, we adjust
3830 DT_REL to not include it. */
3831 s = htab->srelplt;
3832 if (s == NULL)
3833 continue;
3834 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
3835 continue;
3836 dyn.d_un.d_ptr += s->size;
3837 break;
3840 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3843 /* Fill in the first entry in the procedure linkage table. */
3844 if (htab->splt && htab->splt->size > 0)
3846 if (info->shared)
3848 memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
3849 sizeof (elf_i386_pic_plt0_entry));
3850 memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
3851 htab->plt0_pad_byte,
3852 PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
3854 else
3856 memcpy (htab->splt->contents, elf_i386_plt0_entry,
3857 sizeof(elf_i386_plt0_entry));
3858 memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
3859 htab->plt0_pad_byte,
3860 PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
3861 bfd_put_32 (output_bfd,
3862 (htab->sgotplt->output_section->vma
3863 + htab->sgotplt->output_offset
3864 + 4),
3865 htab->splt->contents + 2);
3866 bfd_put_32 (output_bfd,
3867 (htab->sgotplt->output_section->vma
3868 + htab->sgotplt->output_offset
3869 + 8),
3870 htab->splt->contents + 8);
3872 if (htab->is_vxworks)
3874 Elf_Internal_Rela rel;
3876 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
3877 On IA32 we use REL relocations so the addend goes in
3878 the PLT directly. */
3879 rel.r_offset = (htab->splt->output_section->vma
3880 + htab->splt->output_offset
3881 + 2);
3882 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3883 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3884 htab->srelplt2->contents);
3885 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
3886 rel.r_offset = (htab->splt->output_section->vma
3887 + htab->splt->output_offset
3888 + 8);
3889 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3890 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3891 htab->srelplt2->contents +
3892 sizeof (Elf32_External_Rel));
3896 /* UnixWare sets the entsize of .plt to 4, although that doesn't
3897 really seem like the right value. */
3898 elf_section_data (htab->splt->output_section)
3899 ->this_hdr.sh_entsize = 4;
3901 /* Correct the .rel.plt.unloaded relocations. */
3902 if (htab->is_vxworks && !info->shared)
3904 int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
3905 unsigned char *p;
3907 p = htab->srelplt2->contents;
3908 if (info->shared)
3909 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
3910 else
3911 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
3913 for (; num_plts; num_plts--)
3915 Elf_Internal_Rela rel;
3916 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
3917 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3918 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3919 p += sizeof (Elf32_External_Rel);
3921 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
3922 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3923 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3924 p += sizeof (Elf32_External_Rel);
3930 if (htab->sgotplt)
3932 /* Fill in the first three entries in the global offset table. */
3933 if (htab->sgotplt->size > 0)
3935 bfd_put_32 (output_bfd,
3936 (sdyn == NULL ? 0
3937 : sdyn->output_section->vma + sdyn->output_offset),
3938 htab->sgotplt->contents);
3939 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
3940 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
3943 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
3946 if (htab->sgot && htab->sgot->size > 0)
3947 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
3949 return TRUE;
3952 /* Return address for Ith PLT stub in section PLT, for relocation REL
3953 or (bfd_vma) -1 if it should not be included. */
3955 static bfd_vma
3956 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
3957 const arelent *rel ATTRIBUTE_UNUSED)
3959 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3962 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3964 static bfd_boolean
3965 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
3967 if (h->plt.offset != (bfd_vma) -1
3968 && !h->def_regular
3969 && !h->pointer_equality_needed)
3970 return FALSE;
3972 return _bfd_elf_hash_symbol (h);
3975 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
3976 #define TARGET_LITTLE_NAME "elf32-i386"
3977 #define ELF_ARCH bfd_arch_i386
3978 #define ELF_MACHINE_CODE EM_386
3979 #define ELF_MAXPAGESIZE 0x1000
3981 #define elf_backend_can_gc_sections 1
3982 #define elf_backend_can_refcount 1
3983 #define elf_backend_want_got_plt 1
3984 #define elf_backend_plt_readonly 1
3985 #define elf_backend_want_plt_sym 0
3986 #define elf_backend_got_header_size 12
3988 /* Support RELA for objdump of prelink objects. */
3989 #define elf_info_to_howto elf_i386_info_to_howto_rel
3990 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
3992 #define bfd_elf32_mkobject elf_i386_mkobject
3994 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
3995 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
3996 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
3997 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
3999 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
4000 #define elf_backend_check_relocs elf_i386_check_relocs
4001 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
4002 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
4003 #define elf_backend_fake_sections elf_i386_fake_sections
4004 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4005 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4006 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
4007 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
4008 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4009 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4010 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4011 #define elf_backend_relocate_section elf_i386_relocate_section
4012 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
4013 #define elf_backend_always_size_sections elf_i386_always_size_sections
4014 #define elf_backend_omit_section_dynsym \
4015 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4016 #define elf_backend_plt_sym_val elf_i386_plt_sym_val
4017 #define elf_backend_hash_symbol elf_i386_hash_symbol
4019 #include "elf32-target.h"
4021 /* FreeBSD support. */
4023 #undef TARGET_LITTLE_SYM
4024 #define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
4025 #undef TARGET_LITTLE_NAME
4026 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4027 #undef ELF_OSABI
4028 #define ELF_OSABI ELFOSABI_FREEBSD
4030 /* The kernel recognizes executables as valid only if they carry a
4031 "FreeBSD" label in the ELF header. So we put this label on all
4032 executables and (for simplicity) also all other object files. */
4034 static void
4035 elf_i386_post_process_headers (bfd *abfd,
4036 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4038 Elf_Internal_Ehdr *i_ehdrp;
4040 i_ehdrp = elf_elfheader (abfd);
4042 /* Put an ABI label supported by FreeBSD >= 4.1. */
4043 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
4044 #ifdef OLD_FREEBSD_ABI_LABEL
4045 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4046 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4047 #endif
4050 #undef elf_backend_post_process_headers
4051 #define elf_backend_post_process_headers elf_i386_post_process_headers
4052 #undef elf32_bed
4053 #define elf32_bed elf32_i386_fbsd_bed
4055 #include "elf32-target.h"