1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 static reloc_howto_type *elf_i386_reloc_type_lookup
30 PARAMS ((bfd *, bfd_reloc_code_real_type));
31 static void elf_i386_info_to_howto
32 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
33 static void elf_i386_info_to_howto_rel
34 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
35 static boolean elf_i386_is_local_label_name
36 PARAMS ((bfd *, const char *));
37 static boolean elf_i386_grok_prstatus
38 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
39 static boolean elf_i386_grok_psinfo
40 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
41 static struct bfd_hash_entry *link_hash_newfunc
42 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
43 static struct bfd_link_hash_table *elf_i386_link_hash_table_create
45 static boolean create_got_section
46 PARAMS((bfd *, struct bfd_link_info *));
47 static boolean elf_i386_create_dynamic_sections
48 PARAMS((bfd *, struct bfd_link_info *));
49 static void elf_i386_copy_indirect_symbol
50 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
51 struct elf_link_hash_entry *));
52 static int elf_i386_tls_transition
53 PARAMS ((struct bfd_link_info *, int, int));
55 static boolean elf_i386_mkobject
57 static boolean elf_i386_object_p
59 static boolean elf_i386_check_relocs
60 PARAMS ((bfd *, struct bfd_link_info *, asection *,
61 const Elf_Internal_Rela *));
62 static asection *elf_i386_gc_mark_hook
63 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
64 struct elf_link_hash_entry *, Elf_Internal_Sym *));
65 static boolean elf_i386_gc_sweep_hook
66 PARAMS ((bfd *, struct bfd_link_info *, asection *,
67 const Elf_Internal_Rela *));
68 static boolean elf_i386_adjust_dynamic_symbol
69 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
70 static boolean allocate_dynrelocs
71 PARAMS ((struct elf_link_hash_entry *, PTR));
72 static boolean readonly_dynrelocs
73 PARAMS ((struct elf_link_hash_entry *, PTR));
74 static boolean elf_i386_fake_sections
75 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
76 static boolean elf_i386_size_dynamic_sections
77 PARAMS ((bfd *, struct bfd_link_info *));
78 static bfd_vma dtpoff_base
79 PARAMS ((struct bfd_link_info *));
81 PARAMS ((struct bfd_link_info *, bfd_vma));
82 static boolean elf_i386_relocate_section
83 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
84 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
85 static boolean elf_i386_finish_dynamic_symbol
86 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
88 static enum elf_reloc_type_class elf_i386_reloc_type_class
89 PARAMS ((const Elf_Internal_Rela *));
90 static boolean elf_i386_finish_dynamic_sections
91 PARAMS ((bfd *, struct bfd_link_info *));
93 #define USE_REL 1 /* 386 uses REL relocations instead of RELA */
97 static reloc_howto_type elf_howto_table[]=
99 HOWTO(R_386_NONE, 0, 0, 0, false, 0, complain_overflow_bitfield,
100 bfd_elf_generic_reloc, "R_386_NONE",
101 true, 0x00000000, 0x00000000, false),
102 HOWTO(R_386_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
103 bfd_elf_generic_reloc, "R_386_32",
104 true, 0xffffffff, 0xffffffff, false),
105 HOWTO(R_386_PC32, 0, 2, 32, true, 0, complain_overflow_bitfield,
106 bfd_elf_generic_reloc, "R_386_PC32",
107 true, 0xffffffff, 0xffffffff, true),
108 HOWTO(R_386_GOT32, 0, 2, 32, false, 0, complain_overflow_bitfield,
109 bfd_elf_generic_reloc, "R_386_GOT32",
110 true, 0xffffffff, 0xffffffff, false),
111 HOWTO(R_386_PLT32, 0, 2, 32, true, 0, complain_overflow_bitfield,
112 bfd_elf_generic_reloc, "R_386_PLT32",
113 true, 0xffffffff, 0xffffffff, true),
114 HOWTO(R_386_COPY, 0, 2, 32, false, 0, complain_overflow_bitfield,
115 bfd_elf_generic_reloc, "R_386_COPY",
116 true, 0xffffffff, 0xffffffff, false),
117 HOWTO(R_386_GLOB_DAT, 0, 2, 32, false, 0, complain_overflow_bitfield,
118 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
119 true, 0xffffffff, 0xffffffff, false),
120 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, false, 0, complain_overflow_bitfield,
121 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
122 true, 0xffffffff, 0xffffffff, false),
123 HOWTO(R_386_RELATIVE, 0, 2, 32, false, 0, complain_overflow_bitfield,
124 bfd_elf_generic_reloc, "R_386_RELATIVE",
125 true, 0xffffffff, 0xffffffff, false),
126 HOWTO(R_386_GOTOFF, 0, 2, 32, false, 0, complain_overflow_bitfield,
127 bfd_elf_generic_reloc, "R_386_GOTOFF",
128 true, 0xffffffff, 0xffffffff, false),
129 HOWTO(R_386_GOTPC, 0, 2, 32, true, 0, complain_overflow_bitfield,
130 bfd_elf_generic_reloc, "R_386_GOTPC",
131 true, 0xffffffff, 0xffffffff, true),
133 /* We have a gap in the reloc numbers here.
134 R_386_standard counts the number up to this point, and
135 R_386_ext_offset is the value to subtract from a reloc type of
136 R_386_16 thru R_386_PC8 to form an index into this table. */
137 #define R_386_standard ((unsigned int) R_386_GOTPC + 1)
138 #define R_386_ext_offset ((unsigned int) R_386_TLS_TPOFF - R_386_standard)
140 /* These relocs are a GNU extension. */
141 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, false, 0, complain_overflow_bitfield,
142 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
143 true, 0xffffffff, 0xffffffff, false),
144 HOWTO(R_386_TLS_IE, 0, 2, 32, false, 0, complain_overflow_bitfield,
145 bfd_elf_generic_reloc, "R_386_TLS_IE",
146 true, 0xffffffff, 0xffffffff, false),
147 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, false, 0, complain_overflow_bitfield,
148 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
149 true, 0xffffffff, 0xffffffff, false),
150 HOWTO(R_386_TLS_LE, 0, 2, 32, false, 0, complain_overflow_bitfield,
151 bfd_elf_generic_reloc, "R_386_TLS_LE",
152 true, 0xffffffff, 0xffffffff, false),
153 HOWTO(R_386_TLS_GD, 0, 2, 32, false, 0, complain_overflow_bitfield,
154 bfd_elf_generic_reloc, "R_386_TLS_GD",
155 true, 0xffffffff, 0xffffffff, false),
156 HOWTO(R_386_TLS_LDM, 0, 2, 32, false, 0, complain_overflow_bitfield,
157 bfd_elf_generic_reloc, "R_386_TLS_LDM",
158 true, 0xffffffff, 0xffffffff, false),
159 HOWTO(R_386_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
160 bfd_elf_generic_reloc, "R_386_16",
161 true, 0xffff, 0xffff, false),
162 HOWTO(R_386_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield,
163 bfd_elf_generic_reloc, "R_386_PC16",
164 true, 0xffff, 0xffff, true),
165 HOWTO(R_386_8, 0, 0, 8, false, 0, complain_overflow_bitfield,
166 bfd_elf_generic_reloc, "R_386_8",
167 true, 0xff, 0xff, false),
168 HOWTO(R_386_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
169 bfd_elf_generic_reloc, "R_386_PC8",
170 true, 0xff, 0xff, true),
172 #define R_386_ext ((unsigned int) R_386_PC8 + 1 - R_386_ext_offset)
173 #define R_386_tls_offset ((unsigned int) R_386_TLS_LDO_32 - R_386_ext)
174 /* These are common with Solaris TLS implementation. */
175 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
176 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
177 true, 0xffffffff, 0xffffffff, false),
178 HOWTO(R_386_TLS_IE_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
179 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
180 true, 0xffffffff, 0xffffffff, false),
181 HOWTO(R_386_TLS_LE_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
182 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
183 true, 0xffffffff, 0xffffffff, false),
184 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, false, 0, complain_overflow_bitfield,
185 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
186 true, 0xffffffff, 0xffffffff, false),
187 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, false, 0, complain_overflow_bitfield,
188 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
189 true, 0xffffffff, 0xffffffff, false),
190 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, false, 0, complain_overflow_bitfield,
191 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
192 true, 0xffffffff, 0xffffffff, false),
195 #define R_386_tls ((unsigned int) R_386_TLS_TPOFF32 + 1 - R_386_tls_offset)
196 #define R_386_vt_offset ((unsigned int) R_386_GNU_VTINHERIT - R_386_tls)
198 /* GNU extension to record C++ vtable hierarchy. */
199 HOWTO (R_386_GNU_VTINHERIT, /* type */
201 2, /* size (0 = byte, 1 = short, 2 = long) */
203 false, /* pc_relative */
205 complain_overflow_dont, /* complain_on_overflow */
206 NULL, /* special_function */
207 "R_386_GNU_VTINHERIT", /* name */
208 false, /* partial_inplace */
211 false), /* pcrel_offset */
213 /* GNU extension to record C++ vtable member usage. */
214 HOWTO (R_386_GNU_VTENTRY, /* type */
216 2, /* size (0 = byte, 1 = short, 2 = long) */
218 false, /* pc_relative */
220 complain_overflow_dont, /* complain_on_overflow */
221 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
222 "R_386_GNU_VTENTRY", /* name */
223 false, /* partial_inplace */
226 false) /* pcrel_offset */
228 #define R_386_vt ((unsigned int) R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
232 #ifdef DEBUG_GEN_RELOC
233 #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
238 static reloc_howto_type *
239 elf_i386_reloc_type_lookup (abfd, code)
240 bfd *abfd ATTRIBUTE_UNUSED;
241 bfd_reloc_code_real_type code;
246 TRACE ("BFD_RELOC_NONE");
247 return &elf_howto_table[(unsigned int) R_386_NONE ];
250 TRACE ("BFD_RELOC_32");
251 return &elf_howto_table[(unsigned int) R_386_32 ];
254 TRACE ("BFD_RELOC_CTOR");
255 return &elf_howto_table[(unsigned int) R_386_32 ];
257 case BFD_RELOC_32_PCREL:
258 TRACE ("BFD_RELOC_PC32");
259 return &elf_howto_table[(unsigned int) R_386_PC32 ];
261 case BFD_RELOC_386_GOT32:
262 TRACE ("BFD_RELOC_386_GOT32");
263 return &elf_howto_table[(unsigned int) R_386_GOT32 ];
265 case BFD_RELOC_386_PLT32:
266 TRACE ("BFD_RELOC_386_PLT32");
267 return &elf_howto_table[(unsigned int) R_386_PLT32 ];
269 case BFD_RELOC_386_COPY:
270 TRACE ("BFD_RELOC_386_COPY");
271 return &elf_howto_table[(unsigned int) R_386_COPY ];
273 case BFD_RELOC_386_GLOB_DAT:
274 TRACE ("BFD_RELOC_386_GLOB_DAT");
275 return &elf_howto_table[(unsigned int) R_386_GLOB_DAT ];
277 case BFD_RELOC_386_JUMP_SLOT:
278 TRACE ("BFD_RELOC_386_JUMP_SLOT");
279 return &elf_howto_table[(unsigned int) R_386_JUMP_SLOT ];
281 case BFD_RELOC_386_RELATIVE:
282 TRACE ("BFD_RELOC_386_RELATIVE");
283 return &elf_howto_table[(unsigned int) R_386_RELATIVE ];
285 case BFD_RELOC_386_GOTOFF:
286 TRACE ("BFD_RELOC_386_GOTOFF");
287 return &elf_howto_table[(unsigned int) R_386_GOTOFF ];
289 case BFD_RELOC_386_GOTPC:
290 TRACE ("BFD_RELOC_386_GOTPC");
291 return &elf_howto_table[(unsigned int) R_386_GOTPC ];
293 /* These relocs are a GNU extension. */
294 case BFD_RELOC_386_TLS_TPOFF:
295 TRACE ("BFD_RELOC_386_TLS_TPOFF");
296 return &elf_howto_table[(unsigned int) R_386_TLS_TPOFF - R_386_ext_offset];
298 case BFD_RELOC_386_TLS_IE:
299 TRACE ("BFD_RELOC_386_TLS_IE");
300 return &elf_howto_table[(unsigned int) R_386_TLS_IE - R_386_ext_offset];
302 case BFD_RELOC_386_TLS_GOTIE:
303 TRACE ("BFD_RELOC_386_TLS_GOTIE");
304 return &elf_howto_table[(unsigned int) R_386_TLS_GOTIE - R_386_ext_offset];
306 case BFD_RELOC_386_TLS_LE:
307 TRACE ("BFD_RELOC_386_TLS_LE");
308 return &elf_howto_table[(unsigned int) R_386_TLS_LE - R_386_ext_offset];
310 case BFD_RELOC_386_TLS_GD:
311 TRACE ("BFD_RELOC_386_TLS_GD");
312 return &elf_howto_table[(unsigned int) R_386_TLS_GD - R_386_ext_offset];
314 case BFD_RELOC_386_TLS_LDM:
315 TRACE ("BFD_RELOC_386_TLS_LDM");
316 return &elf_howto_table[(unsigned int) R_386_TLS_LDM - R_386_ext_offset];
319 TRACE ("BFD_RELOC_16");
320 return &elf_howto_table[(unsigned int) R_386_16 - R_386_ext_offset];
322 case BFD_RELOC_16_PCREL:
323 TRACE ("BFD_RELOC_16_PCREL");
324 return &elf_howto_table[(unsigned int) R_386_PC16 - R_386_ext_offset];
327 TRACE ("BFD_RELOC_8");
328 return &elf_howto_table[(unsigned int) R_386_8 - R_386_ext_offset];
330 case BFD_RELOC_8_PCREL:
331 TRACE ("BFD_RELOC_8_PCREL");
332 return &elf_howto_table[(unsigned int) R_386_PC8 - R_386_ext_offset];
334 /* Common with Sun TLS implementation. */
335 case BFD_RELOC_386_TLS_LDO_32:
336 TRACE ("BFD_RELOC_386_TLS_LDO_32");
337 return &elf_howto_table[(unsigned int) R_386_TLS_LDO_32 - R_386_tls_offset];
339 case BFD_RELOC_386_TLS_IE_32:
340 TRACE ("BFD_RELOC_386_TLS_IE_32");
341 return &elf_howto_table[(unsigned int) R_386_TLS_IE_32 - R_386_tls_offset];
343 case BFD_RELOC_386_TLS_LE_32:
344 TRACE ("BFD_RELOC_386_TLS_LE_32");
345 return &elf_howto_table[(unsigned int) R_386_TLS_LE_32 - R_386_tls_offset];
347 case BFD_RELOC_386_TLS_DTPMOD32:
348 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
349 return &elf_howto_table[(unsigned int) R_386_TLS_DTPMOD32 - R_386_tls_offset];
351 case BFD_RELOC_386_TLS_DTPOFF32:
352 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
353 return &elf_howto_table[(unsigned int) R_386_TLS_DTPOFF32 - R_386_tls_offset];
355 case BFD_RELOC_386_TLS_TPOFF32:
356 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
357 return &elf_howto_table[(unsigned int) R_386_TLS_TPOFF32 - R_386_tls_offset];
359 case BFD_RELOC_VTABLE_INHERIT:
360 TRACE ("BFD_RELOC_VTABLE_INHERIT");
361 return &elf_howto_table[(unsigned int) R_386_GNU_VTINHERIT
364 case BFD_RELOC_VTABLE_ENTRY:
365 TRACE ("BFD_RELOC_VTABLE_ENTRY");
366 return &elf_howto_table[(unsigned int) R_386_GNU_VTENTRY
378 elf_i386_info_to_howto (abfd, cache_ptr, dst)
379 bfd *abfd ATTRIBUTE_UNUSED;
380 arelent *cache_ptr ATTRIBUTE_UNUSED;
381 Elf32_Internal_Rela *dst ATTRIBUTE_UNUSED;
387 elf_i386_info_to_howto_rel (abfd, cache_ptr, dst)
388 bfd *abfd ATTRIBUTE_UNUSED;
390 Elf32_Internal_Rel *dst;
392 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
395 if ((indx = r_type) >= R_386_standard
396 && ((indx = r_type - R_386_ext_offset) - R_386_standard
397 >= R_386_ext - R_386_standard)
398 && ((indx = r_type - R_386_tls_offset) - R_386_ext
399 >= R_386_tls - R_386_ext)
400 && ((indx = r_type - R_386_vt_offset) - R_386_tls
401 >= R_386_vt - R_386_tls))
403 (*_bfd_error_handler) (_("%s: invalid relocation type %d"),
404 bfd_archive_filename (abfd), (int) r_type);
405 indx = (unsigned int) R_386_NONE;
407 cache_ptr->howto = &elf_howto_table[indx];
410 /* Return whether a symbol name implies a local label. The UnixWare
411 2.1 cc generates temporary symbols that start with .X, so we
412 recognize them here. FIXME: do other SVR4 compilers also use .X?.
413 If so, we should move the .X recognition into
414 _bfd_elf_is_local_label_name. */
417 elf_i386_is_local_label_name (abfd, name)
421 if (name[0] == '.' && name[1] == 'X')
424 return _bfd_elf_is_local_label_name (abfd, name);
427 /* Support for core dump NOTE sections. */
429 elf_i386_grok_prstatus (abfd, note)
431 Elf_Internal_Note *note;
436 switch (note->descsz)
441 case 144: /* Linux/i386 */
443 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
446 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
455 /* Make a ".reg/999" section. */
456 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
457 raw_size, note->descpos + offset);
461 elf_i386_grok_psinfo (abfd, note)
463 Elf_Internal_Note *note;
465 switch (note->descsz)
470 case 124: /* Linux/i386 elf_prpsinfo */
471 elf_tdata (abfd)->core_program
472 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
473 elf_tdata (abfd)->core_command
474 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
477 /* Note that for some reason, a spurious space is tacked
478 onto the end of the args in some (at least one anyway)
479 implementations, so strip it off if it exists. */
482 char *command = elf_tdata (abfd)->core_command;
483 int n = strlen (command);
485 if (0 < n && command[n - 1] == ' ')
486 command[n - 1] = '\0';
492 /* Functions for the i386 ELF linker.
494 In order to gain some understanding of code in this file without
495 knowing all the intricate details of the linker, note the
498 Functions named elf_i386_* are called by external routines, other
499 functions are only called locally. elf_i386_* functions appear
500 in this file more or less in the order in which they are called
501 from external routines. eg. elf_i386_check_relocs is called
502 early in the link process, elf_i386_finish_dynamic_sections is
503 one of the last functions. */
506 /* The name of the dynamic interpreter. This is put in the .interp
509 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
511 /* The size in bytes of an entry in the procedure linkage table. */
513 #define PLT_ENTRY_SIZE 16
515 /* The first entry in an absolute procedure linkage table looks like
516 this. See the SVR4 ABI i386 supplement to see how this works. */
518 static const bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
520 0xff, 0x35, /* pushl contents of address */
521 0, 0, 0, 0, /* replaced with address of .got + 4. */
522 0xff, 0x25, /* jmp indirect */
523 0, 0, 0, 0, /* replaced with address of .got + 8. */
524 0, 0, 0, 0 /* pad out to 16 bytes. */
527 /* Subsequent entries in an absolute procedure linkage table look like
530 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
532 0xff, 0x25, /* jmp indirect */
533 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
534 0x68, /* pushl immediate */
535 0, 0, 0, 0, /* replaced with offset into relocation table. */
536 0xe9, /* jmp relative */
537 0, 0, 0, 0 /* replaced with offset to start of .plt. */
540 /* The first entry in a PIC procedure linkage table look like this. */
542 static const bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
544 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
545 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
546 0, 0, 0, 0 /* pad out to 16 bytes. */
549 /* Subsequent entries in a PIC procedure linkage table look like this. */
551 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
553 0xff, 0xa3, /* jmp *offset(%ebx) */
554 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
555 0x68, /* pushl immediate */
556 0, 0, 0, 0, /* replaced with offset into relocation table. */
557 0xe9, /* jmp relative */
558 0, 0, 0, 0 /* replaced with offset to start of .plt. */
561 /* The i386 linker needs to keep track of the number of relocs that it
562 decides to copy as dynamic relocs in check_relocs for each symbol.
563 This is so that it can later discard them if they are found to be
564 unnecessary. We store the information in a field extending the
565 regular ELF linker hash table. */
567 struct elf_i386_dyn_relocs
569 struct elf_i386_dyn_relocs *next;
571 /* The input section of the reloc. */
574 /* Total number of relocs copied for the input section. */
577 /* Number of pc-relative relocs copied for the input section. */
578 bfd_size_type pc_count;
581 /* i386 ELF linker hash entry. */
583 struct elf_i386_link_hash_entry
585 struct elf_link_hash_entry elf;
587 /* Track dynamic relocs copied for this symbol. */
588 struct elf_i386_dyn_relocs *dyn_relocs;
590 #define GOT_UNKNOWN 0
594 #define GOT_TLS_IE_POS 5
595 #define GOT_TLS_IE_NEG 6
596 #define GOT_TLS_IE_BOTH 7
597 unsigned char tls_type;
600 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
602 struct elf_i386_obj_tdata
604 struct elf_obj_tdata root;
606 /* tls_type for each local got entry. */
607 char *local_got_tls_type;
610 #define elf_i386_tdata(abfd) \
611 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
613 #define elf_i386_local_got_tls_type(abfd) \
614 (elf_i386_tdata (abfd)->local_got_tls_type)
617 elf_i386_mkobject (abfd)
620 bfd_size_type amt = sizeof (struct elf_i386_obj_tdata);
621 abfd->tdata.any = bfd_zalloc (abfd, amt);
622 if (abfd->tdata.any == NULL)
628 elf_i386_object_p (abfd)
631 /* Allocate our special target data. */
632 struct elf_i386_obj_tdata *new_tdata;
633 bfd_size_type amt = sizeof (struct elf_i386_obj_tdata);
634 new_tdata = bfd_zalloc (abfd, amt);
635 if (new_tdata == NULL)
637 new_tdata->root = *abfd->tdata.elf_obj_data;
638 abfd->tdata.any = new_tdata;
642 /* i386 ELF linker hash table. */
644 struct elf_i386_link_hash_table
646 struct elf_link_hash_table elf;
648 /* Short-cuts to get to dynamic linker sections. */
658 bfd_signed_vma refcount;
662 /* Small local sym to section mapping cache. */
663 struct sym_sec_cache sym_sec;
666 /* Get the i386 ELF linker hash table from a link_info structure. */
668 #define elf_i386_hash_table(p) \
669 ((struct elf_i386_link_hash_table *) ((p)->hash))
671 /* Create an entry in an i386 ELF linker hash table. */
673 static struct bfd_hash_entry *
674 link_hash_newfunc (entry, table, string)
675 struct bfd_hash_entry *entry;
676 struct bfd_hash_table *table;
679 /* Allocate the structure if it has not already been allocated by a
683 entry = bfd_hash_allocate (table,
684 sizeof (struct elf_i386_link_hash_entry));
689 /* Call the allocation method of the superclass. */
690 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
693 struct elf_i386_link_hash_entry *eh;
695 eh = (struct elf_i386_link_hash_entry *) entry;
696 eh->dyn_relocs = NULL;
697 eh->tls_type = GOT_UNKNOWN;
703 /* Create an i386 ELF linker hash table. */
705 static struct bfd_link_hash_table *
706 elf_i386_link_hash_table_create (abfd)
709 struct elf_i386_link_hash_table *ret;
710 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
712 ret = (struct elf_i386_link_hash_table *) bfd_malloc (amt);
716 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
729 ret->tls_ldm_got.refcount = 0;
730 ret->sym_sec.abfd = NULL;
732 return &ret->elf.root;
735 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
736 shortcuts to them in our hash table. */
739 create_got_section (dynobj, info)
741 struct bfd_link_info *info;
743 struct elf_i386_link_hash_table *htab;
745 if (! _bfd_elf_create_got_section (dynobj, info))
748 htab = elf_i386_hash_table (info);
749 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
750 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
751 if (!htab->sgot || !htab->sgotplt)
754 htab->srelgot = bfd_make_section (dynobj, ".rel.got");
755 if (htab->srelgot == NULL
756 || ! bfd_set_section_flags (dynobj, htab->srelgot,
757 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
758 | SEC_IN_MEMORY | SEC_LINKER_CREATED
760 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
765 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
766 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
770 elf_i386_create_dynamic_sections (dynobj, info)
772 struct bfd_link_info *info;
774 struct elf_i386_link_hash_table *htab;
776 htab = elf_i386_hash_table (info);
777 if (!htab->sgot && !create_got_section (dynobj, info))
780 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
783 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
784 htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
785 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
787 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
789 if (!htab->splt || !htab->srelplt || !htab->sdynbss
790 || (!info->shared && !htab->srelbss))
796 /* Copy the extra info we tack onto an elf_link_hash_entry. */
799 elf_i386_copy_indirect_symbol (bed, dir, ind)
800 struct elf_backend_data *bed;
801 struct elf_link_hash_entry *dir, *ind;
803 struct elf_i386_link_hash_entry *edir, *eind;
805 edir = (struct elf_i386_link_hash_entry *) dir;
806 eind = (struct elf_i386_link_hash_entry *) ind;
808 if (eind->dyn_relocs != NULL)
810 if (edir->dyn_relocs != NULL)
812 struct elf_i386_dyn_relocs **pp;
813 struct elf_i386_dyn_relocs *p;
815 if (ind->root.type == bfd_link_hash_indirect)
818 /* Add reloc counts against the weak sym to the strong sym
819 list. Merge any entries against the same section. */
820 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
822 struct elf_i386_dyn_relocs *q;
824 for (q = edir->dyn_relocs; q != NULL; q = q->next)
825 if (q->sec == p->sec)
827 q->pc_count += p->pc_count;
828 q->count += p->count;
835 *pp = edir->dyn_relocs;
838 edir->dyn_relocs = eind->dyn_relocs;
839 eind->dyn_relocs = NULL;
842 if (ind->root.type == bfd_link_hash_indirect
843 && dir->got.refcount <= 0)
845 edir->tls_type = eind->tls_type;
846 eind->tls_type = GOT_UNKNOWN;
848 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
852 elf_i386_tls_transition (info, r_type, is_local)
853 struct bfd_link_info *info;
863 case R_386_TLS_IE_32:
865 return R_386_TLS_LE_32;
866 return R_386_TLS_IE_32;
868 case R_386_TLS_GOTIE:
870 return R_386_TLS_LE_32;
873 return R_386_TLS_LE_32;
879 /* Look through the relocs for a section during the first phase, and
880 calculate needed space in the global offset table, procedure linkage
881 table, and dynamic reloc sections. */
884 elf_i386_check_relocs (abfd, info, sec, relocs)
886 struct bfd_link_info *info;
888 const Elf_Internal_Rela *relocs;
890 struct elf_i386_link_hash_table *htab;
891 Elf_Internal_Shdr *symtab_hdr;
892 struct elf_link_hash_entry **sym_hashes;
893 const Elf_Internal_Rela *rel;
894 const Elf_Internal_Rela *rel_end;
897 if (info->relocateable)
900 htab = elf_i386_hash_table (info);
901 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
902 sym_hashes = elf_sym_hashes (abfd);
906 rel_end = relocs + sec->reloc_count;
907 for (rel = relocs; rel < rel_end; rel++)
910 unsigned long r_symndx;
911 struct elf_link_hash_entry *h;
913 r_symndx = ELF32_R_SYM (rel->r_info);
914 r_type = ELF32_R_TYPE (rel->r_info);
916 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
918 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
919 bfd_archive_filename (abfd),
924 if (r_symndx < symtab_hdr->sh_info)
927 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
929 r_type = elf_i386_tls_transition (info, r_type, h == NULL);
934 htab->tls_ldm_got.refcount += 1;
938 /* This symbol requires a procedure linkage table entry. We
939 actually build the entry in adjust_dynamic_symbol,
940 because this might be a case of linking PIC code which is
941 never referenced by a dynamic object, in which case we
942 don't need to generate a procedure linkage table entry
945 /* If this is a local symbol, we resolve it directly without
946 creating a procedure linkage table entry. */
950 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
951 h->plt.refcount += 1;
954 case R_386_TLS_IE_32:
956 case R_386_TLS_GOTIE:
958 info->flags |= DF_STATIC_TLS;
963 /* This symbol requires a global offset table entry. */
965 int tls_type, old_tls_type;
970 case R_386_GOT32: tls_type = GOT_NORMAL; break;
971 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
972 case R_386_TLS_IE_32:
973 if (ELF32_R_TYPE (rel->r_info) == r_type)
974 tls_type = GOT_TLS_IE_NEG;
976 /* If this is a GD->IE transition, we may use either of
977 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
978 tls_type = GOT_TLS_IE;
981 case R_386_TLS_GOTIE:
982 tls_type = GOT_TLS_IE_POS; break;
987 h->got.refcount += 1;
988 old_tls_type = elf_i386_hash_entry(h)->tls_type;
992 bfd_signed_vma *local_got_refcounts;
994 /* This is a global offset table entry for a local symbol. */
995 local_got_refcounts = elf_local_got_refcounts (abfd);
996 if (local_got_refcounts == NULL)
1000 size = symtab_hdr->sh_info;
1001 size *= (sizeof (bfd_signed_vma) + sizeof(char));
1002 local_got_refcounts = ((bfd_signed_vma *)
1003 bfd_zalloc (abfd, size));
1004 if (local_got_refcounts == NULL)
1006 elf_local_got_refcounts (abfd) = local_got_refcounts;
1007 elf_i386_local_got_tls_type (abfd)
1008 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
1010 local_got_refcounts[r_symndx] += 1;
1011 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1014 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1015 tls_type |= old_tls_type;
1016 /* If a TLS symbol is accessed using IE at least once,
1017 there is no point to use dynamic model for it. */
1018 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1019 && (old_tls_type != GOT_TLS_GD
1020 || (tls_type & GOT_TLS_IE) == 0))
1022 if ((old_tls_type & GOT_TLS_IE) && tls_type == GOT_TLS_GD)
1023 tls_type = old_tls_type;
1026 (*_bfd_error_handler)
1027 (_("%s: `%s' accessed both as normal and thread local symbol"),
1028 bfd_archive_filename (abfd),
1029 h ? h->root.root.string : "<local>");
1034 if (old_tls_type != tls_type)
1037 elf_i386_hash_entry (h)->tls_type = tls_type;
1039 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1047 if (htab->sgot == NULL)
1049 if (htab->elf.dynobj == NULL)
1050 htab->elf.dynobj = abfd;
1051 if (!create_got_section (htab->elf.dynobj, info))
1054 if (r_type != R_386_TLS_IE)
1058 case R_386_TLS_LE_32:
1066 if (h != NULL && !info->shared)
1068 /* If this reloc is in a read-only section, we might
1069 need a copy reloc. We can't check reliably at this
1070 stage whether the section is read-only, as input
1071 sections have not yet been mapped to output sections.
1072 Tentatively set the flag for now, and correct in
1073 adjust_dynamic_symbol. */
1074 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1076 /* We may need a .plt entry if the function this reloc
1077 refers to is in a shared lib. */
1078 h->plt.refcount += 1;
1081 /* If we are creating a shared library, and this is a reloc
1082 against a global symbol, or a non PC relative reloc
1083 against a local symbol, then we need to copy the reloc
1084 into the shared library. However, if we are linking with
1085 -Bsymbolic, we do not need to copy a reloc against a
1086 global symbol which is defined in an object we are
1087 including in the link (i.e., DEF_REGULAR is set). At
1088 this point we have not seen all the input files, so it is
1089 possible that DEF_REGULAR is not set now but will be set
1090 later (it is never cleared). In case of a weak definition,
1091 DEF_REGULAR may be cleared later by a strong definition in
1092 a shared library. We account for that possibility below by
1093 storing information in the relocs_copied field of the hash
1094 table entry. A similar situation occurs when creating
1095 shared libraries and symbol visibility changes render the
1098 If on the other hand, we are creating an executable, we
1099 may need to keep relocations for symbols satisfied by a
1100 dynamic library if we manage to avoid copy relocs for the
1103 && (sec->flags & SEC_ALLOC) != 0
1104 && (r_type != R_386_PC32
1106 && (! info->symbolic
1107 || h->root.type == bfd_link_hash_defweak
1108 || (h->elf_link_hash_flags
1109 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1111 && (sec->flags & SEC_ALLOC) != 0
1113 && (h->root.type == bfd_link_hash_defweak
1114 || (h->elf_link_hash_flags
1115 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1117 struct elf_i386_dyn_relocs *p;
1118 struct elf_i386_dyn_relocs **head;
1120 /* We must copy these reloc types into the output file.
1121 Create a reloc section in dynobj and make room for
1127 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1128 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
1130 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1134 if (strncmp (name, ".rel", 4) != 0
1135 || strcmp (bfd_get_section_name (abfd, sec),
1138 (*_bfd_error_handler)
1139 (_("%s: bad relocation section name `%s\'"),
1140 bfd_archive_filename (abfd), name);
1143 if (htab->elf.dynobj == NULL)
1144 htab->elf.dynobj = abfd;
1146 dynobj = htab->elf.dynobj;
1147 sreloc = bfd_get_section_by_name (dynobj, name);
1152 sreloc = bfd_make_section (dynobj, name);
1153 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1154 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1155 if ((sec->flags & SEC_ALLOC) != 0)
1156 flags |= SEC_ALLOC | SEC_LOAD;
1158 || ! bfd_set_section_flags (dynobj, sreloc, flags)
1159 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1162 elf_section_data (sec)->sreloc = sreloc;
1165 /* If this is a global symbol, we count the number of
1166 relocations we need for this symbol. */
1169 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1173 /* Track dynamic relocs needed for local syms too.
1174 We really need local syms available to do this
1178 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1183 head = ((struct elf_i386_dyn_relocs **)
1184 &elf_section_data (s)->local_dynrel);
1188 if (p == NULL || p->sec != sec)
1190 bfd_size_type amt = sizeof *p;
1191 p = ((struct elf_i386_dyn_relocs *)
1192 bfd_alloc (htab->elf.dynobj, amt));
1203 if (r_type == R_386_PC32)
1208 /* This relocation describes the C++ object vtable hierarchy.
1209 Reconstruct it for later use during GC. */
1210 case R_386_GNU_VTINHERIT:
1211 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1215 /* This relocation describes which C++ vtable entries are actually
1216 used. Record for later use during GC. */
1217 case R_386_GNU_VTENTRY:
1218 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1230 /* Return the section that should be marked against GC for a given
1234 elf_i386_gc_mark_hook (sec, info, rel, h, sym)
1236 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1237 Elf_Internal_Rela *rel;
1238 struct elf_link_hash_entry *h;
1239 Elf_Internal_Sym *sym;
1243 switch (ELF32_R_TYPE (rel->r_info))
1245 case R_386_GNU_VTINHERIT:
1246 case R_386_GNU_VTENTRY:
1250 switch (h->root.type)
1252 case bfd_link_hash_defined:
1253 case bfd_link_hash_defweak:
1254 return h->root.u.def.section;
1256 case bfd_link_hash_common:
1257 return h->root.u.c.p->section;
1265 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1270 /* Update the got entry reference counts for the section being removed. */
1273 elf_i386_gc_sweep_hook (abfd, info, sec, relocs)
1275 struct bfd_link_info *info;
1277 const Elf_Internal_Rela *relocs;
1279 Elf_Internal_Shdr *symtab_hdr;
1280 struct elf_link_hash_entry **sym_hashes;
1281 bfd_signed_vma *local_got_refcounts;
1282 const Elf_Internal_Rela *rel, *relend;
1283 unsigned long r_symndx;
1285 struct elf_link_hash_entry *h;
1287 elf_section_data (sec)->local_dynrel = NULL;
1289 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1290 sym_hashes = elf_sym_hashes (abfd);
1291 local_got_refcounts = elf_local_got_refcounts (abfd);
1293 relend = relocs + sec->reloc_count;
1294 for (rel = relocs; rel < relend; rel++)
1295 switch ((r_type = elf_i386_tls_transition (info,
1296 ELF32_R_TYPE (rel->r_info),
1297 ELF32_R_SYM (rel->r_info)
1298 >= symtab_hdr->sh_info)))
1301 if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1302 elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1306 case R_386_TLS_IE_32:
1308 case R_386_TLS_GOTIE:
1310 r_symndx = ELF32_R_SYM (rel->r_info);
1311 if (r_symndx >= symtab_hdr->sh_info)
1313 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1314 if (h->got.refcount > 0)
1315 h->got.refcount -= 1;
1317 else if (local_got_refcounts != NULL)
1319 if (local_got_refcounts[r_symndx] > 0)
1320 local_got_refcounts[r_symndx] -= 1;
1322 if (r_type != R_386_TLS_IE)
1326 case R_386_TLS_LE_32:
1334 r_symndx = ELF32_R_SYM (rel->r_info);
1335 if (r_symndx >= symtab_hdr->sh_info)
1337 struct elf_i386_link_hash_entry *eh;
1338 struct elf_i386_dyn_relocs **pp;
1339 struct elf_i386_dyn_relocs *p;
1341 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1343 if (!info->shared && h->plt.refcount > 0)
1344 h->plt.refcount -= 1;
1346 eh = (struct elf_i386_link_hash_entry *) h;
1348 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1351 if (ELF32_R_TYPE (rel->r_info) == R_386_PC32)
1362 r_symndx = ELF32_R_SYM (rel->r_info);
1363 if (r_symndx >= symtab_hdr->sh_info)
1365 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1366 if (h->plt.refcount > 0)
1367 h->plt.refcount -= 1;
1378 /* Adjust a symbol defined by a dynamic object and referenced by a
1379 regular object. The current definition is in some section of the
1380 dynamic object, but we're not including those sections. We have to
1381 change the definition to something the rest of the link can
1385 elf_i386_adjust_dynamic_symbol (info, h)
1386 struct bfd_link_info *info;
1387 struct elf_link_hash_entry *h;
1389 struct elf_i386_link_hash_table *htab;
1390 struct elf_i386_link_hash_entry * eh;
1391 struct elf_i386_dyn_relocs *p;
1393 unsigned int power_of_two;
1395 /* If this is a function, put it in the procedure linkage table. We
1396 will fill in the contents of the procedure linkage table later,
1397 when we know the address of the .got section. */
1398 if (h->type == STT_FUNC
1399 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1401 if (h->plt.refcount <= 0
1403 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1404 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1405 && h->root.type != bfd_link_hash_undefweak
1406 && h->root.type != bfd_link_hash_undefined))
1408 /* This case can occur if we saw a PLT32 reloc in an input
1409 file, but the symbol was never referred to by a dynamic
1410 object, or if all references were garbage collected. In
1411 such a case, we don't actually need to build a procedure
1412 linkage table, and we can just do a PC32 reloc instead. */
1413 h->plt.offset = (bfd_vma) -1;
1414 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1420 /* It's possible that we incorrectly decided a .plt reloc was
1421 needed for an R_386_PC32 reloc to a non-function sym in
1422 check_relocs. We can't decide accurately between function and
1423 non-function syms in check-relocs; Objects loaded later in
1424 the link may change h->type. So fix it now. */
1425 h->plt.offset = (bfd_vma) -1;
1427 /* If this is a weak symbol, and there is a real definition, the
1428 processor independent code will have arranged for us to see the
1429 real definition first, and we can just use the same value. */
1430 if (h->weakdef != NULL)
1432 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1433 || h->weakdef->root.type == bfd_link_hash_defweak);
1434 h->root.u.def.section = h->weakdef->root.u.def.section;
1435 h->root.u.def.value = h->weakdef->root.u.def.value;
1439 /* This is a reference to a symbol defined by a dynamic object which
1440 is not a function. */
1442 /* If we are creating a shared library, we must presume that the
1443 only references to the symbol are via the global offset table.
1444 For such cases we need not do anything here; the relocations will
1445 be handled correctly by relocate_section. */
1449 /* If there are no references to this symbol that do not use the
1450 GOT, we don't need to generate a copy reloc. */
1451 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1454 /* If -z nocopyreloc was given, we won't generate them either. */
1455 if (info->nocopyreloc)
1457 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1461 eh = (struct elf_i386_link_hash_entry *) h;
1462 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1464 s = p->sec->output_section;
1465 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1469 /* If we didn't find any dynamic relocs in read-only sections, then
1470 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1473 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1477 /* We must allocate the symbol in our .dynbss section, which will
1478 become part of the .bss section of the executable. There will be
1479 an entry for this symbol in the .dynsym section. The dynamic
1480 object will contain position independent code, so all references
1481 from the dynamic object to this symbol will go through the global
1482 offset table. The dynamic linker will use the .dynsym entry to
1483 determine the address it must put in the global offset table, so
1484 both the dynamic object and the regular object will refer to the
1485 same memory location for the variable. */
1487 htab = elf_i386_hash_table (info);
1489 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1490 copy the initial value out of the dynamic object and into the
1491 runtime process image. */
1492 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1494 htab->srelbss->_raw_size += sizeof (Elf32_External_Rel);
1495 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1498 /* We need to figure out the alignment required for this symbol. I
1499 have no idea how ELF linkers handle this. */
1500 power_of_two = bfd_log2 (h->size);
1501 if (power_of_two > 3)
1504 /* Apply the required alignment. */
1506 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
1507 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1509 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1513 /* Define the symbol as being at this point in the section. */
1514 h->root.u.def.section = s;
1515 h->root.u.def.value = s->_raw_size;
1517 /* Increment the section size to make room for the symbol. */
1518 s->_raw_size += h->size;
1523 /* This is the condition under which elf_i386_finish_dynamic_symbol
1524 will be called from elflink.h. If elflink.h doesn't call our
1525 finish_dynamic_symbol routine, we'll need to do something about
1526 initializing any .plt and .got entries in elf_i386_relocate_section. */
1527 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1529 && ((INFO)->shared \
1530 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1531 && ((H)->dynindx != -1 \
1532 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1534 /* Allocate space in .plt, .got and associated reloc sections for
1538 allocate_dynrelocs (h, inf)
1539 struct elf_link_hash_entry *h;
1542 struct bfd_link_info *info;
1543 struct elf_i386_link_hash_table *htab;
1544 struct elf_i386_link_hash_entry *eh;
1545 struct elf_i386_dyn_relocs *p;
1547 if (h->root.type == bfd_link_hash_indirect)
1550 if (h->root.type == bfd_link_hash_warning)
1551 /* When warning symbols are created, they **replace** the "real"
1552 entry in the hash table, thus we never get to see the real
1553 symbol in a hash traversal. So look at it now. */
1554 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1556 info = (struct bfd_link_info *) inf;
1557 htab = elf_i386_hash_table (info);
1559 if (htab->elf.dynamic_sections_created
1560 && h->plt.refcount > 0)
1562 /* Make sure this symbol is output as a dynamic symbol.
1563 Undefined weak syms won't yet be marked as dynamic. */
1564 if (h->dynindx == -1
1565 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1567 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1571 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1573 asection *s = htab->splt;
1575 /* If this is the first .plt entry, make room for the special
1577 if (s->_raw_size == 0)
1578 s->_raw_size += PLT_ENTRY_SIZE;
1580 h->plt.offset = s->_raw_size;
1582 /* If this symbol is not defined in a regular file, and we are
1583 not generating a shared library, then set the symbol to this
1584 location in the .plt. This is required to make function
1585 pointers compare as equal between the normal executable and
1586 the shared library. */
1588 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1590 h->root.u.def.section = s;
1591 h->root.u.def.value = h->plt.offset;
1594 /* Make room for this entry. */
1595 s->_raw_size += PLT_ENTRY_SIZE;
1597 /* We also need to make an entry in the .got.plt section, which
1598 will be placed in the .got section by the linker script. */
1599 htab->sgotplt->_raw_size += 4;
1601 /* We also need to make an entry in the .rel.plt section. */
1602 htab->srelplt->_raw_size += sizeof (Elf32_External_Rel);
1606 h->plt.offset = (bfd_vma) -1;
1607 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1612 h->plt.offset = (bfd_vma) -1;
1613 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1616 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
1617 make it a R_386_TLS_LE_32 requiring no TLS entry. */
1618 if (h->got.refcount > 0
1621 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
1622 h->got.offset = (bfd_vma) -1;
1623 else if (h->got.refcount > 0)
1627 int tls_type = elf_i386_hash_entry(h)->tls_type;
1629 /* Make sure this symbol is output as a dynamic symbol.
1630 Undefined weak syms won't yet be marked as dynamic. */
1631 if (h->dynindx == -1
1632 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1634 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1639 h->got.offset = s->_raw_size;
1641 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
1642 if (tls_type == GOT_TLS_GD || tls_type == GOT_TLS_IE_BOTH)
1644 dyn = htab->elf.dynamic_sections_created;
1645 /* R_386_TLS_IE_32 needs one dynamic relocation,
1646 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1647 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1648 need two), R_386_TLS_GD needs one if local symbol and two if
1650 if (tls_type == GOT_TLS_IE_BOTH)
1651 htab->srelgot->_raw_size += 2 * sizeof (Elf32_External_Rel);
1652 else if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1653 || (tls_type & GOT_TLS_IE))
1654 htab->srelgot->_raw_size += sizeof (Elf32_External_Rel);
1655 else if (tls_type == GOT_TLS_GD)
1656 htab->srelgot->_raw_size += 2 * sizeof (Elf32_External_Rel);
1657 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
1658 htab->srelgot->_raw_size += sizeof (Elf32_External_Rel);
1661 h->got.offset = (bfd_vma) -1;
1663 eh = (struct elf_i386_link_hash_entry *) h;
1664 if (eh->dyn_relocs == NULL)
1667 /* In the shared -Bsymbolic case, discard space allocated for
1668 dynamic pc-relative relocs against symbols which turn out to be
1669 defined in regular objects. For the normal shared case, discard
1670 space for pc-relative relocs that have become local due to symbol
1671 visibility changes. */
1675 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1676 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1679 struct elf_i386_dyn_relocs **pp;
1681 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1683 p->count -= p->pc_count;
1694 /* For the non-shared case, discard space for relocs against
1695 symbols which turn out to need copy relocs or are not
1698 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1699 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1700 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1701 || (htab->elf.dynamic_sections_created
1702 && (h->root.type == bfd_link_hash_undefweak
1703 || h->root.type == bfd_link_hash_undefined))))
1705 /* Make sure this symbol is output as a dynamic symbol.
1706 Undefined weak syms won't yet be marked as dynamic. */
1707 if (h->dynindx == -1
1708 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1710 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1714 /* If that succeeded, we know we'll be keeping all the
1716 if (h->dynindx != -1)
1720 eh->dyn_relocs = NULL;
1725 /* Finally, allocate space. */
1726 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1728 asection *sreloc = elf_section_data (p->sec)->sreloc;
1729 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rel);
1735 /* Find any dynamic relocs that apply to read-only sections. */
1738 readonly_dynrelocs (h, inf)
1739 struct elf_link_hash_entry *h;
1742 struct elf_i386_link_hash_entry *eh;
1743 struct elf_i386_dyn_relocs *p;
1745 if (h->root.type == bfd_link_hash_warning)
1746 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1748 eh = (struct elf_i386_link_hash_entry *) h;
1749 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1751 asection *s = p->sec->output_section;
1753 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1755 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1757 info->flags |= DF_TEXTREL;
1759 /* Not an error, just cut short the traversal. */
1766 /* Set the sizes of the dynamic sections. */
1769 elf_i386_size_dynamic_sections (output_bfd, info)
1770 bfd *output_bfd ATTRIBUTE_UNUSED;
1771 struct bfd_link_info *info;
1773 struct elf_i386_link_hash_table *htab;
1779 htab = elf_i386_hash_table (info);
1780 dynobj = htab->elf.dynobj;
1784 if (htab->elf.dynamic_sections_created)
1786 /* Set the contents of the .interp section to the interpreter. */
1789 s = bfd_get_section_by_name (dynobj, ".interp");
1792 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1793 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1797 /* Set up .got offsets for local syms, and space for local dynamic
1799 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1801 bfd_signed_vma *local_got;
1802 bfd_signed_vma *end_local_got;
1803 char *local_tls_type;
1804 bfd_size_type locsymcount;
1805 Elf_Internal_Shdr *symtab_hdr;
1808 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1811 for (s = ibfd->sections; s != NULL; s = s->next)
1813 struct elf_i386_dyn_relocs *p;
1815 for (p = *((struct elf_i386_dyn_relocs **)
1816 &elf_section_data (s)->local_dynrel);
1820 if (!bfd_is_abs_section (p->sec)
1821 && bfd_is_abs_section (p->sec->output_section))
1823 /* Input section has been discarded, either because
1824 it is a copy of a linkonce section or due to
1825 linker script /DISCARD/, so we'll be discarding
1828 else if (p->count != 0)
1830 srel = elf_section_data (p->sec)->sreloc;
1831 srel->_raw_size += p->count * sizeof (Elf32_External_Rel);
1832 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1833 info->flags |= DF_TEXTREL;
1838 local_got = elf_local_got_refcounts (ibfd);
1842 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1843 locsymcount = symtab_hdr->sh_info;
1844 end_local_got = local_got + locsymcount;
1845 local_tls_type = elf_i386_local_got_tls_type (ibfd);
1847 srel = htab->srelgot;
1848 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1852 *local_got = s->_raw_size;
1854 if (*local_tls_type == GOT_TLS_GD
1855 || *local_tls_type == GOT_TLS_IE_BOTH)
1858 || *local_tls_type == GOT_TLS_GD
1859 || (*local_tls_type & GOT_TLS_IE))
1861 if (*local_tls_type == GOT_TLS_IE_BOTH)
1862 srel->_raw_size += 2 * sizeof (Elf32_External_Rel);
1864 srel->_raw_size += sizeof (Elf32_External_Rel);
1868 *local_got = (bfd_vma) -1;
1872 if (htab->tls_ldm_got.refcount > 0)
1874 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
1876 htab->tls_ldm_got.offset = htab->sgot->_raw_size;
1877 htab->sgot->_raw_size += 8;
1878 htab->srelgot->_raw_size += sizeof (Elf32_External_Rel);
1881 htab->tls_ldm_got.offset = -1;
1883 /* Allocate global sym .plt and .got entries, and space for global
1884 sym dynamic relocs. */
1885 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1887 /* We now have determined the sizes of the various dynamic sections.
1888 Allocate memory for them. */
1890 for (s = dynobj->sections; s != NULL; s = s->next)
1892 if ((s->flags & SEC_LINKER_CREATED) == 0)
1897 || s == htab->sgotplt)
1899 /* Strip this section if we don't need it; see the
1902 else if (strncmp (bfd_get_section_name (dynobj, s), ".rel", 4) == 0)
1904 if (s->_raw_size != 0 && s != htab->srelplt)
1907 /* We use the reloc_count field as a counter if we need
1908 to copy relocs into the output file. */
1913 /* It's not one of our sections, so don't allocate space. */
1917 if (s->_raw_size == 0)
1919 /* If we don't need this section, strip it from the
1920 output file. This is mostly to handle .rel.bss and
1921 .rel.plt. We must create both sections in
1922 create_dynamic_sections, because they must be created
1923 before the linker maps input sections to output
1924 sections. The linker does that before
1925 adjust_dynamic_symbol is called, and it is that
1926 function which decides whether anything needs to go
1927 into these sections. */
1929 _bfd_strip_section_from_output (info, s);
1933 /* Allocate memory for the section contents. We use bfd_zalloc
1934 here in case unused entries are not reclaimed before the
1935 section's contents are written out. This should not happen,
1936 but this way if it does, we get a R_386_NONE reloc instead
1938 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1939 if (s->contents == NULL)
1943 if (htab->elf.dynamic_sections_created)
1945 /* Add some entries to the .dynamic section. We fill in the
1946 values later, in elf_i386_finish_dynamic_sections, but we
1947 must add the entries now so that we get the correct size for
1948 the .dynamic section. The DT_DEBUG entry is filled in by the
1949 dynamic linker and used by the debugger. */
1950 #define add_dynamic_entry(TAG, VAL) \
1951 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1955 if (!add_dynamic_entry (DT_DEBUG, 0))
1959 if (htab->splt->_raw_size != 0)
1961 if (!add_dynamic_entry (DT_PLTGOT, 0)
1962 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1963 || !add_dynamic_entry (DT_PLTREL, DT_REL)
1964 || !add_dynamic_entry (DT_JMPREL, 0))
1970 if (!add_dynamic_entry (DT_REL, 0)
1971 || !add_dynamic_entry (DT_RELSZ, 0)
1972 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
1975 /* If any dynamic relocs apply to a read-only section,
1976 then we need a DT_TEXTREL entry. */
1977 if ((info->flags & DF_TEXTREL) == 0)
1978 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1981 if ((info->flags & DF_TEXTREL) != 0)
1983 if (!add_dynamic_entry (DT_TEXTREL, 0))
1988 #undef add_dynamic_entry
1993 /* Set the correct type for an x86 ELF section. We do this by the
1994 section name, which is a hack, but ought to work. */
1997 elf_i386_fake_sections (abfd, hdr, sec)
1998 bfd *abfd ATTRIBUTE_UNUSED;
1999 Elf32_Internal_Shdr *hdr;
2002 register const char *name;
2004 name = bfd_get_section_name (abfd, sec);
2006 /* This is an ugly, but unfortunately necessary hack that is
2007 needed when producing EFI binaries on x86. It tells
2008 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2009 containing ELF relocation info. We need this hack in order to
2010 be able to generate ELF binaries that can be translated into
2011 EFI applications (which are essentially COFF objects). Those
2012 files contain a COFF ".reloc" section inside an ELFNN object,
2013 which would normally cause BFD to segfault because it would
2014 attempt to interpret this section as containing relocation
2015 entries for section "oc". With this hack enabled, ".reloc"
2016 will be treated as a normal data section, which will avoid the
2017 segfault. However, you won't be able to create an ELFNN binary
2018 with a section named "oc" that needs relocations, but that's
2019 the kind of ugly side-effects you get when detecting section
2020 types based on their names... In practice, this limitation is
2021 unlikely to bite. */
2022 if (strcmp (name, ".reloc") == 0)
2023 hdr->sh_type = SHT_PROGBITS;
2028 /* Return the base VMA address which should be subtracted from real addresses
2029 when resolving @dtpoff relocation.
2030 This is PT_TLS segment p_vaddr. */
2034 struct bfd_link_info *info;
2036 /* If tls_segment is NULL, we should have signalled an error already. */
2037 if (elf_hash_table (info)->tls_segment == NULL)
2039 return elf_hash_table (info)->tls_segment->start;
2042 /* Return the relocation value for @tpoff relocation
2043 if STT_TLS virtual address is ADDRESS. */
2046 tpoff (info, address)
2047 struct bfd_link_info *info;
2050 struct elf_link_tls_segment *tls_segment
2051 = elf_hash_table (info)->tls_segment;
2053 /* If tls_segment is NULL, we should have signalled an error already. */
2054 if (tls_segment == NULL)
2056 return (align_power (tls_segment->size, tls_segment->align)
2057 + tls_segment->start - address);
2060 /* Relocate an i386 ELF section. */
2063 elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
2064 contents, relocs, local_syms, local_sections)
2066 struct bfd_link_info *info;
2068 asection *input_section;
2070 Elf_Internal_Rela *relocs;
2071 Elf_Internal_Sym *local_syms;
2072 asection **local_sections;
2074 struct elf_i386_link_hash_table *htab;
2075 Elf_Internal_Shdr *symtab_hdr;
2076 struct elf_link_hash_entry **sym_hashes;
2077 bfd_vma *local_got_offsets;
2078 Elf_Internal_Rela *rel;
2079 Elf_Internal_Rela *relend;
2081 htab = elf_i386_hash_table (info);
2082 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2083 sym_hashes = elf_sym_hashes (input_bfd);
2084 local_got_offsets = elf_local_got_offsets (input_bfd);
2087 relend = relocs + input_section->reloc_count;
2088 for (; rel < relend; rel++)
2090 unsigned int r_type;
2091 reloc_howto_type *howto;
2092 unsigned long r_symndx;
2093 struct elf_link_hash_entry *h;
2094 Elf_Internal_Sym *sym;
2098 boolean unresolved_reloc;
2099 bfd_reloc_status_type r;
2103 r_type = ELF32_R_TYPE (rel->r_info);
2104 if (r_type == (int) R_386_GNU_VTINHERIT
2105 || r_type == (int) R_386_GNU_VTENTRY)
2108 if ((indx = (unsigned) r_type) >= R_386_standard
2109 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2110 >= R_386_ext - R_386_standard)
2111 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2112 >= R_386_tls - R_386_ext))
2114 bfd_set_error (bfd_error_bad_value);
2117 howto = elf_howto_table + indx;
2119 r_symndx = ELF32_R_SYM (rel->r_info);
2121 if (info->relocateable)
2126 /* This is a relocatable link. We don't have to change
2127 anything, unless the reloc is against a section symbol,
2128 in which case we have to adjust according to where the
2129 section symbol winds up in the output section. */
2130 if (r_symndx >= symtab_hdr->sh_info)
2133 sym = local_syms + r_symndx;
2134 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2137 sec = local_sections[r_symndx];
2138 val = sec->output_offset;
2142 where = contents + rel->r_offset;
2143 switch (howto->size)
2145 /* FIXME: overflow checks. */
2147 val += bfd_get_8 (input_bfd, where);
2148 bfd_put_8 (input_bfd, val, where);
2151 val += bfd_get_16 (input_bfd, where);
2152 bfd_put_16 (input_bfd, val, where);
2155 val += bfd_get_32 (input_bfd, where);
2156 bfd_put_32 (input_bfd, val, where);
2164 /* This is a final link. */
2168 unresolved_reloc = false;
2169 if (r_symndx < symtab_hdr->sh_info)
2171 sym = local_syms + r_symndx;
2172 sec = local_sections[r_symndx];
2173 relocation = (sec->output_section->vma
2174 + sec->output_offset
2176 if ((sec->flags & SEC_MERGE)
2177 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2181 bfd_byte *where = contents + rel->r_offset;
2183 switch (howto->size)
2186 addend = bfd_get_8 (input_bfd, where);
2187 if (howto->pc_relative)
2189 addend = (addend ^ 0x80) - 0x80;
2194 addend = bfd_get_16 (input_bfd, where);
2195 if (howto->pc_relative)
2197 addend = (addend ^ 0x8000) - 0x8000;
2202 addend = bfd_get_32 (input_bfd, where);
2203 if (howto->pc_relative)
2205 addend = (addend ^ 0x80000000) - 0x80000000;
2214 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend);
2215 addend -= relocation;
2216 addend += msec->output_section->vma + msec->output_offset;
2218 switch (howto->size)
2221 /* FIXME: overflow checks. */
2222 if (howto->pc_relative)
2224 bfd_put_8 (input_bfd, addend, where);
2227 if (howto->pc_relative)
2229 bfd_put_16 (input_bfd, addend, where);
2232 if (howto->pc_relative)
2234 bfd_put_32 (input_bfd, addend, where);
2241 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2242 while (h->root.type == bfd_link_hash_indirect
2243 || h->root.type == bfd_link_hash_warning)
2244 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2247 if (h->root.type == bfd_link_hash_defined
2248 || h->root.type == bfd_link_hash_defweak)
2250 sec = h->root.u.def.section;
2251 if (sec->output_section == NULL)
2252 /* Set a flag that will be cleared later if we find a
2253 relocation value for this symbol. output_section
2254 is typically NULL for symbols satisfied by a shared
2256 unresolved_reloc = true;
2258 relocation = (h->root.u.def.value
2259 + sec->output_section->vma
2260 + sec->output_offset);
2262 else if (h->root.type == bfd_link_hash_undefweak)
2264 else if (info->shared
2265 && (!info->symbolic || info->allow_shlib_undefined)
2266 && !info->no_undefined
2267 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2271 if (! ((*info->callbacks->undefined_symbol)
2272 (info, h->root.root.string, input_bfd,
2273 input_section, rel->r_offset,
2274 (!info->shared || info->no_undefined
2275 || ELF_ST_VISIBILITY (h->other)))))
2283 /* Relocation is to the entry for this symbol in the global
2285 if (htab->sgot == NULL)
2292 off = h->got.offset;
2293 dyn = htab->elf.dynamic_sections_created;
2294 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
2298 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2299 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2301 /* This is actually a static link, or it is a
2302 -Bsymbolic link and the symbol is defined
2303 locally, or the symbol was forced to be local
2304 because of a version file. We must initialize
2305 this entry in the global offset table. Since the
2306 offset must always be a multiple of 4, we use the
2307 least significant bit to record whether we have
2308 initialized it already.
2310 When doing a dynamic link, we create a .rel.got
2311 relocation entry to initialize the value. This
2312 is done in the finish_dynamic_symbol routine. */
2317 bfd_put_32 (output_bfd, relocation,
2318 htab->sgot->contents + off);
2323 unresolved_reloc = false;
2327 if (local_got_offsets == NULL)
2330 off = local_got_offsets[r_symndx];
2332 /* The offset must always be a multiple of 4. We use
2333 the least significant bit to record whether we have
2334 already generated the necessary reloc. */
2339 bfd_put_32 (output_bfd, relocation,
2340 htab->sgot->contents + off);
2345 Elf_Internal_Rel outrel;
2346 Elf32_External_Rel *loc;
2348 srelgot = htab->srelgot;
2349 if (srelgot == NULL)
2352 outrel.r_offset = (htab->sgot->output_section->vma
2353 + htab->sgot->output_offset
2355 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2356 loc = (Elf32_External_Rel *) srelgot->contents;
2357 loc += srelgot->reloc_count++;
2358 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2361 local_got_offsets[r_symndx] |= 1;
2365 if (off >= (bfd_vma) -2)
2368 relocation = htab->sgot->output_offset + off;
2372 /* Relocation is relative to the start of the global offset
2375 /* Note that sgot->output_offset is not involved in this
2376 calculation. We always want the start of .got. If we
2377 defined _GLOBAL_OFFSET_TABLE in a different way, as is
2378 permitted by the ABI, we might have to change this
2380 relocation -= htab->sgot->output_section->vma;
2384 /* Use global offset table as symbol value. */
2385 relocation = htab->sgot->output_section->vma;
2386 unresolved_reloc = false;
2390 /* Relocation is to the entry for this symbol in the
2391 procedure linkage table. */
2393 /* Resolve a PLT32 reloc against a local symbol directly,
2394 without using the procedure linkage table. */
2398 if (h->plt.offset == (bfd_vma) -1
2399 || htab->splt == NULL)
2401 /* We didn't make a PLT entry for this symbol. This
2402 happens when statically linking PIC code, or when
2403 using -Bsymbolic. */
2407 relocation = (htab->splt->output_section->vma
2408 + htab->splt->output_offset
2410 unresolved_reloc = false;
2415 /* r_symndx will be zero only for relocs against symbols
2416 from removed linkonce sections, or sections discarded by
2419 || (input_section->flags & SEC_ALLOC) == 0)
2423 && (r_type != R_386_PC32
2426 && (! info->symbolic
2427 || (h->elf_link_hash_flags
2428 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2432 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2433 && (((h->elf_link_hash_flags
2434 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2435 && (h->elf_link_hash_flags
2436 & ELF_LINK_HASH_DEF_REGULAR) == 0)
2437 || h->root.type == bfd_link_hash_undefweak
2438 || h->root.type == bfd_link_hash_undefined)))
2440 Elf_Internal_Rel outrel;
2441 boolean skip, relocate;
2443 Elf32_External_Rel *loc;
2445 /* When generating a shared object, these relocations
2446 are copied into the output file to be resolved at run
2453 _bfd_elf_section_offset (output_bfd, info, input_section,
2455 if (outrel.r_offset == (bfd_vma) -1)
2457 else if (outrel.r_offset == (bfd_vma) -2)
2458 skip = true, relocate = true;
2459 outrel.r_offset += (input_section->output_section->vma
2460 + input_section->output_offset);
2463 memset (&outrel, 0, sizeof outrel);
2466 && (r_type == R_386_PC32
2469 || (h->elf_link_hash_flags
2470 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2471 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2474 /* This symbol is local, or marked to become local. */
2476 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2479 sreloc = elf_section_data (input_section)->sreloc;
2483 loc = (Elf32_External_Rel *) sreloc->contents;
2484 loc += sreloc->reloc_count++;
2485 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2487 /* If this reloc is against an external symbol, we do
2488 not want to fiddle with the addend. Otherwise, we
2489 need to include the symbol value so that it becomes
2490 an addend for the dynamic reloc. */
2499 Elf_Internal_Rel outrel;
2501 Elf32_External_Rel *loc;
2503 outrel.r_offset = rel->r_offset
2504 + input_section->output_section->vma
2505 + input_section->output_offset;
2506 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2507 sreloc = elf_section_data (input_section)->sreloc;
2510 loc = (Elf32_External_Rel *) sreloc->contents;
2511 loc += sreloc->reloc_count++;
2512 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2517 case R_386_TLS_IE_32:
2518 case R_386_TLS_GOTIE:
2519 r_type = elf_i386_tls_transition (info, r_type, h == NULL);
2520 tls_type = GOT_UNKNOWN;
2521 if (h == NULL && local_got_offsets)
2522 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2525 tls_type = elf_i386_hash_entry(h)->tls_type;
2526 if (!info->shared && h->dynindx == -1 && (tls_type & GOT_TLS_IE))
2527 r_type = R_386_TLS_LE_32;
2529 if (tls_type == GOT_TLS_IE)
2530 tls_type = GOT_TLS_IE_NEG;
2531 if (r_type == R_386_TLS_GD)
2533 if (tls_type == GOT_TLS_IE_POS)
2534 r_type = R_386_TLS_GOTIE;
2535 else if (tls_type & GOT_TLS_IE)
2536 r_type = R_386_TLS_IE_32;
2539 if (r_type == R_386_TLS_LE_32)
2541 BFD_ASSERT (! unresolved_reloc);
2542 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
2544 unsigned int val, type;
2547 /* GD->LE transition. */
2548 BFD_ASSERT (rel->r_offset >= 2);
2549 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2550 BFD_ASSERT (type == 0x8d || type == 0x04);
2551 BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size);
2552 BFD_ASSERT (bfd_get_8 (input_bfd,
2553 contents + rel->r_offset + 4)
2555 BFD_ASSERT (rel + 1 < relend);
2556 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2557 roff = rel->r_offset + 5;
2558 val = bfd_get_8 (input_bfd,
2559 contents + rel->r_offset - 1);
2562 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2564 movl %gs:0, %eax; subl $foo@tpoff, %eax
2565 (6 byte form of subl). */
2566 BFD_ASSERT (rel->r_offset >= 3);
2567 BFD_ASSERT (bfd_get_8 (input_bfd,
2568 contents + rel->r_offset - 3)
2570 BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2571 memcpy (contents + rel->r_offset - 3,
2572 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2576 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2577 if (rel->r_offset + 10 <= input_section->_raw_size
2578 && bfd_get_8 (input_bfd,
2579 contents + rel->r_offset + 9) == 0x90)
2581 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2583 movl %gs:0, %eax; subl $foo@tpoff, %eax
2584 (6 byte form of subl). */
2585 memcpy (contents + rel->r_offset - 2,
2586 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2587 roff = rel->r_offset + 6;
2591 /* leal foo(%reg), %eax; call ___tls_get_addr
2593 movl %gs:0, %eax; subl $foo@tpoff, %eax
2594 (5 byte form of subl). */
2595 memcpy (contents + rel->r_offset - 2,
2596 "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2599 bfd_put_32 (output_bfd, tpoff (info, relocation),
2601 /* Skip R_386_PLT32. */
2605 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
2607 unsigned int val, type;
2609 /* IE->LE transition:
2610 Originally it can be one of:
2618 BFD_ASSERT (rel->r_offset >= 1);
2619 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2620 BFD_ASSERT (rel->r_offset + 4 <= input_section->_raw_size);
2623 /* movl foo, %eax. */
2624 bfd_put_8 (output_bfd, 0xb8, contents + rel->r_offset - 1);
2628 BFD_ASSERT (rel->r_offset >= 2);
2629 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2634 BFD_ASSERT ((val & 0xc7) == 0x05);
2635 bfd_put_8 (output_bfd, 0xc7,
2636 contents + rel->r_offset - 2);
2637 bfd_put_8 (output_bfd,
2638 0xc0 | ((val >> 3) & 7),
2639 contents + rel->r_offset - 1);
2643 BFD_ASSERT ((val & 0xc7) == 0x05);
2644 bfd_put_8 (output_bfd, 0x81,
2645 contents + rel->r_offset - 2);
2646 bfd_put_8 (output_bfd,
2647 0xc0 | ((val >> 3) & 7),
2648 contents + rel->r_offset - 1);
2655 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2656 contents + rel->r_offset);
2661 unsigned int val, type;
2663 /* {IE_32,GOTIE}->LE transition:
2664 Originally it can be one of:
2665 subl foo(%reg1), %reg2
2666 movl foo(%reg1), %reg2
2667 addl foo(%reg1), %reg2
2670 movl $foo, %reg2 (6 byte form)
2671 addl $foo, %reg2. */
2672 BFD_ASSERT (rel->r_offset >= 2);
2673 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2674 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2675 BFD_ASSERT (rel->r_offset + 4 <= input_section->_raw_size);
2676 BFD_ASSERT ((val & 0xc0) == 0x80 && (val & 7) != 4);
2680 bfd_put_8 (output_bfd, 0xc7,
2681 contents + rel->r_offset - 2);
2682 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2683 contents + rel->r_offset - 1);
2685 else if (type == 0x2b)
2688 bfd_put_8 (output_bfd, 0x81,
2689 contents + rel->r_offset - 2);
2690 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
2691 contents + rel->r_offset - 1);
2693 else if (type == 0x03)
2696 bfd_put_8 (output_bfd, 0x81,
2697 contents + rel->r_offset - 2);
2698 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2699 contents + rel->r_offset - 1);
2703 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
2704 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2705 contents + rel->r_offset);
2707 bfd_put_32 (output_bfd, tpoff (info, relocation),
2708 contents + rel->r_offset);
2713 if (htab->sgot == NULL)
2717 off = h->got.offset;
2720 if (local_got_offsets == NULL)
2723 off = local_got_offsets[r_symndx];
2730 Elf_Internal_Rel outrel;
2731 Elf32_External_Rel *loc;
2734 if (htab->srelgot == NULL)
2737 outrel.r_offset = (htab->sgot->output_section->vma
2738 + htab->sgot->output_offset + off);
2740 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2741 if (r_type == R_386_TLS_GD)
2742 dr_type = R_386_TLS_DTPMOD32;
2743 else if (tls_type == GOT_TLS_IE_POS)
2744 dr_type = R_386_TLS_TPOFF;
2746 dr_type = R_386_TLS_TPOFF32;
2747 if (dr_type == R_386_TLS_TPOFF && indx == 0)
2748 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
2749 htab->sgot->contents + off);
2750 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
2751 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
2752 htab->sgot->contents + off);
2754 bfd_put_32 (output_bfd, 0,
2755 htab->sgot->contents + off);
2756 outrel.r_info = ELF32_R_INFO (indx, dr_type);
2757 loc = (Elf32_External_Rel *) htab->srelgot->contents;
2758 loc += htab->srelgot->reloc_count++;
2759 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2761 if (r_type == R_386_TLS_GD)
2765 BFD_ASSERT (! unresolved_reloc);
2766 bfd_put_32 (output_bfd,
2767 relocation - dtpoff_base (info),
2768 htab->sgot->contents + off + 4);
2772 bfd_put_32 (output_bfd, 0,
2773 htab->sgot->contents + off + 4);
2774 outrel.r_info = ELF32_R_INFO (indx,
2775 R_386_TLS_DTPOFF32);
2776 outrel.r_offset += 4;
2777 htab->srelgot->reloc_count++;
2779 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
2783 else if (tls_type == GOT_TLS_IE_BOTH)
2785 bfd_put_32 (output_bfd,
2786 indx == 0 ? relocation - dtpoff_base (info) : 0,
2787 htab->sgot->contents + off + 4);
2788 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
2789 outrel.r_offset += 4;
2790 htab->srelgot->reloc_count++;
2792 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2798 local_got_offsets[r_symndx] |= 1;
2801 if (off >= (bfd_vma) -2)
2803 if (r_type == ELF32_R_TYPE (rel->r_info))
2805 relocation = htab->sgot->output_offset + off;
2806 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
2807 && tls_type == GOT_TLS_IE_BOTH)
2809 if (r_type == R_386_TLS_IE)
2810 relocation += htab->sgot->output_section->vma;
2811 unresolved_reloc = false;
2815 unsigned int val, type;
2818 /* GD->IE transition. */
2819 BFD_ASSERT (rel->r_offset >= 2);
2820 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2821 BFD_ASSERT (type == 0x8d || type == 0x04);
2822 BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size);
2823 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2825 BFD_ASSERT (rel + 1 < relend);
2826 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2827 roff = rel->r_offset - 3;
2828 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2831 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2833 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
2834 BFD_ASSERT (rel->r_offset >= 3);
2835 BFD_ASSERT (bfd_get_8 (input_bfd,
2836 contents + rel->r_offset - 3)
2838 BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2843 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2845 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
2846 BFD_ASSERT (rel->r_offset + 10 <= input_section->_raw_size);
2847 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2848 BFD_ASSERT (bfd_get_8 (input_bfd,
2849 contents + rel->r_offset + 9)
2851 roff = rel->r_offset - 2;
2853 memcpy (contents + roff,
2854 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
2855 contents[roff + 7] = 0x80 | (val & 7);
2856 /* If foo is used only with foo@gotntpoff(%reg) and
2857 foo@indntpoff, but not with foo@gottpoff(%reg), change
2858 subl $foo@gottpoff(%reg), %eax
2860 addl $foo@gotntpoff(%reg), %eax. */
2861 if (r_type == R_386_TLS_GOTIE)
2863 contents[roff + 6] = 0x03;
2864 if (tls_type == GOT_TLS_IE_BOTH)
2867 bfd_put_32 (output_bfd, htab->sgot->output_offset + off,
2868 contents + roff + 8);
2869 /* Skip R_386_PLT32. */
2880 /* LD->LE transition:
2882 leal foo(%reg), %eax; call ___tls_get_addr.
2884 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
2885 BFD_ASSERT (rel->r_offset >= 2);
2886 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2888 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2889 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2890 BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size);
2891 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2893 BFD_ASSERT (rel + 1 < relend);
2894 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2895 memcpy (contents + rel->r_offset - 2,
2896 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
2897 /* Skip R_386_PLT32. */
2902 if (htab->sgot == NULL)
2905 off = htab->tls_ldm_got.offset;
2910 Elf_Internal_Rel outrel;
2911 Elf32_External_Rel *loc;
2913 if (htab->srelgot == NULL)
2916 outrel.r_offset = (htab->sgot->output_section->vma
2917 + htab->sgot->output_offset + off);
2919 bfd_put_32 (output_bfd, 0,
2920 htab->sgot->contents + off);
2921 bfd_put_32 (output_bfd, 0,
2922 htab->sgot->contents + off + 4);
2923 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
2924 loc = (Elf32_External_Rel *) htab->srelgot->contents;
2925 loc += htab->srelgot->reloc_count++;
2926 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2927 htab->tls_ldm_got.offset |= 1;
2929 relocation = htab->sgot->output_offset + off;
2930 unresolved_reloc = false;
2933 case R_386_TLS_LDO_32:
2935 relocation -= dtpoff_base (info);
2937 /* When converting LDO to LE, we must negate. */
2938 relocation = -tpoff (info, relocation);
2941 case R_386_TLS_LE_32:
2945 Elf_Internal_Rel outrel;
2947 Elf32_External_Rel *loc;
2950 outrel.r_offset = rel->r_offset
2951 + input_section->output_section->vma
2952 + input_section->output_offset;
2953 if (h != NULL && h->dynindx != -1)
2957 if (r_type == R_386_TLS_LE_32)
2958 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
2960 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
2961 sreloc = elf_section_data (input_section)->sreloc;
2964 loc = (Elf32_External_Rel *) sreloc->contents;
2965 loc += sreloc->reloc_count++;
2966 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2969 else if (r_type == R_386_TLS_LE_32)
2970 relocation = dtpoff_base (info) - relocation;
2972 relocation -= dtpoff_base (info);
2974 else if (r_type == R_386_TLS_LE_32)
2975 relocation = tpoff (info, relocation);
2977 relocation = -tpoff (info, relocation);
2984 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2985 because such sections are not SEC_ALLOC and thus ld.so will
2986 not process them. */
2987 if (unresolved_reloc
2988 && !((input_section->flags & SEC_DEBUGGING) != 0
2989 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2991 (*_bfd_error_handler)
2992 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2993 bfd_archive_filename (input_bfd),
2994 bfd_get_section_name (input_bfd, input_section),
2995 (long) rel->r_offset,
2996 h->root.root.string);
3000 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3001 contents, rel->r_offset,
3002 relocation, (bfd_vma) 0);
3004 if (r != bfd_reloc_ok)
3009 name = h->root.root.string;
3012 name = bfd_elf_string_from_elf_section (input_bfd,
3013 symtab_hdr->sh_link,
3018 name = bfd_section_name (input_bfd, sec);
3021 if (r == bfd_reloc_overflow)
3023 if (! ((*info->callbacks->reloc_overflow)
3024 (info, name, howto->name, (bfd_vma) 0,
3025 input_bfd, input_section, rel->r_offset)))
3030 (*_bfd_error_handler)
3031 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
3032 bfd_archive_filename (input_bfd),
3033 bfd_get_section_name (input_bfd, input_section),
3034 (long) rel->r_offset, name, (int) r);
3043 /* Finish up dynamic symbol handling. We set the contents of various
3044 dynamic sections here. */
3047 elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
3049 struct bfd_link_info *info;
3050 struct elf_link_hash_entry *h;
3051 Elf_Internal_Sym *sym;
3053 struct elf_i386_link_hash_table *htab;
3055 htab = elf_i386_hash_table (info);
3057 if (h->plt.offset != (bfd_vma) -1)
3061 Elf_Internal_Rel rel;
3062 Elf32_External_Rel *loc;
3064 /* This symbol has an entry in the procedure linkage table. Set
3067 if (h->dynindx == -1
3068 || htab->splt == NULL
3069 || htab->sgotplt == NULL
3070 || htab->srelplt == NULL)
3073 /* Get the index in the procedure linkage table which
3074 corresponds to this symbol. This is the index of this symbol
3075 in all the symbols for which we are making plt entries. The
3076 first entry in the procedure linkage table is reserved. */
3077 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3079 /* Get the offset into the .got table of the entry that
3080 corresponds to this function. Each .got entry is 4 bytes.
3081 The first three are reserved. */
3082 got_offset = (plt_index + 3) * 4;
3084 /* Fill in the entry in the procedure linkage table. */
3087 memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
3089 bfd_put_32 (output_bfd,
3090 (htab->sgotplt->output_section->vma
3091 + htab->sgotplt->output_offset
3093 htab->splt->contents + h->plt.offset + 2);
3097 memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
3099 bfd_put_32 (output_bfd, got_offset,
3100 htab->splt->contents + h->plt.offset + 2);
3103 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
3104 htab->splt->contents + h->plt.offset + 7);
3105 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3106 htab->splt->contents + h->plt.offset + 12);
3108 /* Fill in the entry in the global offset table. */
3109 bfd_put_32 (output_bfd,
3110 (htab->splt->output_section->vma
3111 + htab->splt->output_offset
3114 htab->sgotplt->contents + got_offset);
3116 /* Fill in the entry in the .rel.plt section. */
3117 rel.r_offset = (htab->sgotplt->output_section->vma
3118 + htab->sgotplt->output_offset
3120 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3121 loc = (Elf32_External_Rel *) htab->srelplt->contents + plt_index;
3122 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3124 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3126 /* Mark the symbol as undefined, rather than as defined in
3127 the .plt section. Leave the value alone. This is a clue
3128 for the dynamic linker, to make function pointer
3129 comparisons work between an application and shared
3131 sym->st_shndx = SHN_UNDEF;
3135 if (h->got.offset != (bfd_vma) -1
3136 && elf_i386_hash_entry(h)->tls_type != GOT_TLS_GD
3137 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
3139 Elf_Internal_Rel rel;
3140 Elf32_External_Rel *loc;
3142 /* This symbol has an entry in the global offset table. Set it
3145 if (htab->sgot == NULL || htab->srelgot == NULL)
3148 rel.r_offset = (htab->sgot->output_section->vma
3149 + htab->sgot->output_offset
3150 + (h->got.offset & ~(bfd_vma) 1));
3152 /* If this is a static link, or it is a -Bsymbolic link and the
3153 symbol is defined locally or was forced to be local because
3154 of a version file, we just want to emit a RELATIVE reloc.
3155 The entry in the global offset table will already have been
3156 initialized in the relocate_section function. */
3160 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3161 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3163 BFD_ASSERT((h->got.offset & 1) != 0);
3164 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3168 BFD_ASSERT((h->got.offset & 1) == 0);
3169 bfd_put_32 (output_bfd, (bfd_vma) 0,
3170 htab->sgot->contents + h->got.offset);
3171 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3174 loc = (Elf32_External_Rel *) htab->srelgot->contents;
3175 loc += htab->srelgot->reloc_count++;
3176 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3179 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3181 Elf_Internal_Rel rel;
3182 Elf32_External_Rel *loc;
3184 /* This symbol needs a copy reloc. Set it up. */
3186 if (h->dynindx == -1
3187 || (h->root.type != bfd_link_hash_defined
3188 && h->root.type != bfd_link_hash_defweak)
3189 || htab->srelbss == NULL)
3192 rel.r_offset = (h->root.u.def.value
3193 + h->root.u.def.section->output_section->vma
3194 + h->root.u.def.section->output_offset);
3195 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3196 loc = (Elf32_External_Rel *) htab->srelbss->contents;
3197 loc += htab->srelbss->reloc_count++;
3198 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3201 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3202 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3203 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3204 sym->st_shndx = SHN_ABS;
3209 /* Used to decide how to sort relocs in an optimal manner for the
3210 dynamic linker, before writing them out. */
3212 static enum elf_reloc_type_class
3213 elf_i386_reloc_type_class (rela)
3214 const Elf_Internal_Rela *rela;
3216 switch ((int) ELF32_R_TYPE (rela->r_info))
3218 case R_386_RELATIVE:
3219 return reloc_class_relative;
3220 case R_386_JUMP_SLOT:
3221 return reloc_class_plt;
3223 return reloc_class_copy;
3225 return reloc_class_normal;
3229 /* Finish up the dynamic sections. */
3232 elf_i386_finish_dynamic_sections (output_bfd, info)
3234 struct bfd_link_info *info;
3236 struct elf_i386_link_hash_table *htab;
3240 htab = elf_i386_hash_table (info);
3241 dynobj = htab->elf.dynobj;
3242 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3244 if (htab->elf.dynamic_sections_created)
3246 Elf32_External_Dyn *dyncon, *dynconend;
3248 if (sdyn == NULL || htab->sgot == NULL)
3251 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3252 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3253 for (; dyncon < dynconend; dyncon++)
3255 Elf_Internal_Dyn dyn;
3258 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3266 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
3270 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3274 s = htab->srelplt->output_section;
3275 if (s->_cooked_size != 0)
3276 dyn.d_un.d_val = s->_cooked_size;
3278 dyn.d_un.d_val = s->_raw_size;
3282 /* My reading of the SVR4 ABI indicates that the
3283 procedure linkage table relocs (DT_JMPREL) should be
3284 included in the overall relocs (DT_REL). This is
3285 what Solaris does. However, UnixWare can not handle
3286 that case. Therefore, we override the DT_RELSZ entry
3287 here to make it not include the JMPREL relocs. Since
3288 the linker script arranges for .rel.plt to follow all
3289 other relocation sections, we don't have to worry
3290 about changing the DT_REL entry. */
3291 if (htab->srelplt != NULL)
3293 s = htab->srelplt->output_section;
3294 if (s->_cooked_size != 0)
3295 dyn.d_un.d_val -= s->_cooked_size;
3297 dyn.d_un.d_val -= s->_raw_size;
3302 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3305 /* Fill in the first entry in the procedure linkage table. */
3306 if (htab->splt && htab->splt->_raw_size > 0)
3309 memcpy (htab->splt->contents,
3310 elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
3313 memcpy (htab->splt->contents,
3314 elf_i386_plt0_entry, PLT_ENTRY_SIZE);
3315 bfd_put_32 (output_bfd,
3316 (htab->sgotplt->output_section->vma
3317 + htab->sgotplt->output_offset
3319 htab->splt->contents + 2);
3320 bfd_put_32 (output_bfd,
3321 (htab->sgotplt->output_section->vma
3322 + htab->sgotplt->output_offset
3324 htab->splt->contents + 8);
3327 /* UnixWare sets the entsize of .plt to 4, although that doesn't
3328 really seem like the right value. */
3329 elf_section_data (htab->splt->output_section)
3330 ->this_hdr.sh_entsize = 4;
3336 /* Fill in the first three entries in the global offset table. */
3337 if (htab->sgotplt->_raw_size > 0)
3339 bfd_put_32 (output_bfd,
3340 (sdyn == NULL ? (bfd_vma) 0
3341 : sdyn->output_section->vma + sdyn->output_offset),
3342 htab->sgotplt->contents);
3343 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 4);
3344 bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8);
3347 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
3353 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
3354 #define TARGET_LITTLE_NAME "elf32-i386"
3355 #define ELF_ARCH bfd_arch_i386
3356 #define ELF_MACHINE_CODE EM_386
3357 #define ELF_MAXPAGESIZE 0x1000
3358 #endif /* ELF_ARCH */
3360 #define elf_backend_can_gc_sections 1
3361 #define elf_backend_can_refcount 1
3362 #define elf_backend_want_got_plt 1
3363 #define elf_backend_plt_readonly 1
3364 #define elf_backend_want_plt_sym 0
3365 #define elf_backend_got_header_size 12
3366 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
3368 #define elf_info_to_howto elf_i386_info_to_howto
3369 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
3371 #define bfd_elf32_mkobject elf_i386_mkobject
3372 #define elf_backend_object_p elf_i386_object_p
3374 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
3375 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
3376 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
3378 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
3379 #define elf_backend_check_relocs elf_i386_check_relocs
3380 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
3381 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
3382 #define elf_backend_fake_sections elf_i386_fake_sections
3383 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
3384 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
3385 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
3386 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
3387 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
3388 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
3389 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
3390 #define elf_backend_relocate_section elf_i386_relocate_section
3391 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
3393 #include "elf32-target.h"