1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright 1993, 94, 95, 96, 97, 98, 99, 2000
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. */
27 static reloc_howto_type
*elf_i386_reloc_type_lookup
28 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
29 static void elf_i386_info_to_howto
30 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rela
*));
31 static void elf_i386_info_to_howto_rel
32 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rel
*));
33 static boolean elf_i386_is_local_label_name
PARAMS ((bfd
*, const char *));
34 static struct bfd_hash_entry
*elf_i386_link_hash_newfunc
35 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
36 static struct bfd_link_hash_table
*elf_i386_link_hash_table_create
38 static boolean elf_i386_check_relocs
39 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
40 const Elf_Internal_Rela
*));
41 static boolean elf_i386_adjust_dynamic_symbol
42 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
43 static boolean elf_i386_size_dynamic_sections
44 PARAMS ((bfd
*, struct bfd_link_info
*));
45 static boolean elf_i386_relocate_section
46 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
47 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
48 static boolean elf_i386_finish_dynamic_symbol
49 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
51 static boolean elf_i386_finish_dynamic_sections
52 PARAMS ((bfd
*, struct bfd_link_info
*));
54 #define USE_REL 1 /* 386 uses REL relocations instead of RELA */
58 static reloc_howto_type elf_howto_table
[]=
60 HOWTO(R_386_NONE
, 0, 0, 0, false, 0, complain_overflow_bitfield
,
61 bfd_elf_generic_reloc
, "R_386_NONE",
62 true, 0x00000000, 0x00000000, false),
63 HOWTO(R_386_32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
64 bfd_elf_generic_reloc
, "R_386_32",
65 true, 0xffffffff, 0xffffffff, false),
66 HOWTO(R_386_PC32
, 0, 2, 32, true, 0, complain_overflow_bitfield
,
67 bfd_elf_generic_reloc
, "R_386_PC32",
68 true, 0xffffffff, 0xffffffff, true),
69 HOWTO(R_386_GOT32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
70 bfd_elf_generic_reloc
, "R_386_GOT32",
71 true, 0xffffffff, 0xffffffff, false),
72 HOWTO(R_386_PLT32
, 0, 2, 32, true, 0, complain_overflow_bitfield
,
73 bfd_elf_generic_reloc
, "R_386_PLT32",
74 true, 0xffffffff, 0xffffffff, true),
75 HOWTO(R_386_COPY
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
76 bfd_elf_generic_reloc
, "R_386_COPY",
77 true, 0xffffffff, 0xffffffff, false),
78 HOWTO(R_386_GLOB_DAT
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
79 bfd_elf_generic_reloc
, "R_386_GLOB_DAT",
80 true, 0xffffffff, 0xffffffff, false),
81 HOWTO(R_386_JUMP_SLOT
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
82 bfd_elf_generic_reloc
, "R_386_JUMP_SLOT",
83 true, 0xffffffff, 0xffffffff, false),
84 HOWTO(R_386_RELATIVE
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
85 bfd_elf_generic_reloc
, "R_386_RELATIVE",
86 true, 0xffffffff, 0xffffffff, false),
87 HOWTO(R_386_GOTOFF
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
88 bfd_elf_generic_reloc
, "R_386_GOTOFF",
89 true, 0xffffffff, 0xffffffff, false),
90 HOWTO(R_386_GOTPC
, 0, 2, 32, true, 0, complain_overflow_bitfield
,
91 bfd_elf_generic_reloc
, "R_386_GOTPC",
92 true, 0xffffffff, 0xffffffff, true),
94 #define R_386_standard ((unsigned int) R_386_GOTPC + 1)
95 #define R_386_ext_offset ((unsigned int) R_386_16 - R_386_standard)
97 /* The remaining relocs are a GNU extension. */
98 HOWTO(R_386_16
, 0, 1, 16, false, 0, complain_overflow_bitfield
,
99 bfd_elf_generic_reloc
, "R_386_16",
100 true, 0xffff, 0xffff, false),
101 HOWTO(R_386_PC16
, 0, 1, 16, true, 0, complain_overflow_bitfield
,
102 bfd_elf_generic_reloc
, "R_386_PC16",
103 true, 0xffff, 0xffff, true),
104 HOWTO(R_386_8
, 0, 0, 8, false, 0, complain_overflow_bitfield
,
105 bfd_elf_generic_reloc
, "R_386_8",
106 true, 0xff, 0xff, false),
107 HOWTO(R_386_PC8
, 0, 0, 8, true, 0, complain_overflow_signed
,
108 bfd_elf_generic_reloc
, "R_386_PC8",
109 true, 0xff, 0xff, true)
112 /* GNU extension to record C++ vtable hierarchy. */
113 static reloc_howto_type elf32_i386_vtinherit_howto
=
114 HOWTO (R_386_GNU_VTINHERIT
, /* type */
116 2, /* size (0 = byte, 1 = short, 2 = long) */
118 false, /* pc_relative */
120 complain_overflow_dont
, /* complain_on_overflow */
121 NULL
, /* special_function */
122 "R_386_GNU_VTINHERIT", /* name */
123 false, /* partial_inplace */
128 /* GNU extension to record C++ vtable member usage. */
129 static reloc_howto_type elf32_i386_vtentry_howto
=
130 HOWTO (R_386_GNU_VTENTRY
, /* type */
132 2, /* size (0 = byte, 1 = short, 2 = long) */
134 false, /* pc_relative */
136 complain_overflow_dont
, /* complain_on_overflow */
137 _bfd_elf_rel_vtable_reloc_fn
, /* special_function */
138 "R_386_GNU_VTENTRY", /* name */
139 false, /* partial_inplace */
144 #ifdef DEBUG_GEN_RELOC
145 #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
150 static reloc_howto_type
*
151 elf_i386_reloc_type_lookup (abfd
, code
)
152 bfd
*abfd ATTRIBUTE_UNUSED
;
153 bfd_reloc_code_real_type code
;
158 TRACE ("BFD_RELOC_NONE");
159 return &elf_howto_table
[(unsigned int) R_386_NONE
];
162 TRACE ("BFD_RELOC_32");
163 return &elf_howto_table
[(unsigned int) R_386_32
];
166 TRACE ("BFD_RELOC_CTOR");
167 return &elf_howto_table
[(unsigned int) R_386_32
];
169 case BFD_RELOC_32_PCREL
:
170 TRACE ("BFD_RELOC_PC32");
171 return &elf_howto_table
[(unsigned int) R_386_PC32
];
173 case BFD_RELOC_386_GOT32
:
174 TRACE ("BFD_RELOC_386_GOT32");
175 return &elf_howto_table
[(unsigned int) R_386_GOT32
];
177 case BFD_RELOC_386_PLT32
:
178 TRACE ("BFD_RELOC_386_PLT32");
179 return &elf_howto_table
[(unsigned int) R_386_PLT32
];
181 case BFD_RELOC_386_COPY
:
182 TRACE ("BFD_RELOC_386_COPY");
183 return &elf_howto_table
[(unsigned int) R_386_COPY
];
185 case BFD_RELOC_386_GLOB_DAT
:
186 TRACE ("BFD_RELOC_386_GLOB_DAT");
187 return &elf_howto_table
[(unsigned int) R_386_GLOB_DAT
];
189 case BFD_RELOC_386_JUMP_SLOT
:
190 TRACE ("BFD_RELOC_386_JUMP_SLOT");
191 return &elf_howto_table
[(unsigned int) R_386_JUMP_SLOT
];
193 case BFD_RELOC_386_RELATIVE
:
194 TRACE ("BFD_RELOC_386_RELATIVE");
195 return &elf_howto_table
[(unsigned int) R_386_RELATIVE
];
197 case BFD_RELOC_386_GOTOFF
:
198 TRACE ("BFD_RELOC_386_GOTOFF");
199 return &elf_howto_table
[(unsigned int) R_386_GOTOFF
];
201 case BFD_RELOC_386_GOTPC
:
202 TRACE ("BFD_RELOC_386_GOTPC");
203 return &elf_howto_table
[(unsigned int) R_386_GOTPC
];
205 /* The remaining relocs are a GNU extension. */
207 TRACE ("BFD_RELOC_16");
208 return &elf_howto_table
[(unsigned int) R_386_16
- R_386_ext_offset
];
210 case BFD_RELOC_16_PCREL
:
211 TRACE ("BFD_RELOC_16_PCREL");
212 return &elf_howto_table
[(unsigned int) R_386_PC16
- R_386_ext_offset
];
215 TRACE ("BFD_RELOC_8");
216 return &elf_howto_table
[(unsigned int) R_386_8
- R_386_ext_offset
];
218 case BFD_RELOC_8_PCREL
:
219 TRACE ("BFD_RELOC_8_PCREL");
220 return &elf_howto_table
[(unsigned int) R_386_PC8
- R_386_ext_offset
];
222 case BFD_RELOC_VTABLE_INHERIT
:
223 TRACE ("BFD_RELOC_VTABLE_INHERIT");
224 return &elf32_i386_vtinherit_howto
;
226 case BFD_RELOC_VTABLE_ENTRY
:
227 TRACE ("BFD_RELOC_VTABLE_ENTRY");
228 return &elf32_i386_vtentry_howto
;
239 elf_i386_info_to_howto (abfd
, cache_ptr
, dst
)
240 bfd
*abfd ATTRIBUTE_UNUSED
;
241 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
242 Elf32_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
248 elf_i386_info_to_howto_rel (abfd
, cache_ptr
, dst
)
249 bfd
*abfd ATTRIBUTE_UNUSED
;
251 Elf32_Internal_Rel
*dst
;
253 enum elf_i386_reloc_type type
;
255 type
= (enum elf_i386_reloc_type
) ELF32_R_TYPE (dst
->r_info
);
256 if (type
== R_386_GNU_VTINHERIT
)
257 cache_ptr
->howto
= &elf32_i386_vtinherit_howto
;
258 else if (type
== R_386_GNU_VTENTRY
)
259 cache_ptr
->howto
= &elf32_i386_vtentry_howto
;
264 if ((indx
= (unsigned int) type
) >= R_386_standard
265 && ((indx
= (unsigned int) type
- R_386_ext_offset
)
266 >= sizeof (elf_howto_table
) / sizeof (elf_howto_table
[0])))
268 (*_bfd_error_handler
) (_("%s: invalid relocation type %d"),
269 bfd_get_filename (abfd
), (int) type
);
270 indx
= (unsigned int) R_386_NONE
;
272 cache_ptr
->howto
= &elf_howto_table
[indx
];
276 /* Return whether a symbol name implies a local label. The UnixWare
277 2.1 cc generates temporary symbols that start with .X, so we
278 recognize them here. FIXME: do other SVR4 compilers also use .X?.
279 If so, we should move the .X recognition into
280 _bfd_elf_is_local_label_name. */
283 elf_i386_is_local_label_name (abfd
, name
)
287 if (name
[0] == '.' && name
[1] == 'X')
290 return _bfd_elf_is_local_label_name (abfd
, name
);
293 /* Functions for the i386 ELF linker. */
295 /* The name of the dynamic interpreter. This is put in the .interp
298 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
300 /* The size in bytes of an entry in the procedure linkage table. */
302 #define PLT_ENTRY_SIZE 16
304 /* The first entry in an absolute procedure linkage table looks like
305 this. See the SVR4 ABI i386 supplement to see how this works. */
307 static const bfd_byte elf_i386_plt0_entry
[PLT_ENTRY_SIZE
] =
309 0xff, 0x35, /* pushl contents of address */
310 0, 0, 0, 0, /* replaced with address of .got + 4. */
311 0xff, 0x25, /* jmp indirect */
312 0, 0, 0, 0, /* replaced with address of .got + 8. */
313 0, 0, 0, 0 /* pad out to 16 bytes. */
316 /* Subsequent entries in an absolute procedure linkage table look like
319 static const bfd_byte elf_i386_plt_entry
[PLT_ENTRY_SIZE
] =
321 0xff, 0x25, /* jmp indirect */
322 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
323 0x68, /* pushl immediate */
324 0, 0, 0, 0, /* replaced with offset into relocation table. */
325 0xe9, /* jmp relative */
326 0, 0, 0, 0 /* replaced with offset to start of .plt. */
329 /* The first entry in a PIC procedure linkage table look like this. */
331 static const bfd_byte elf_i386_pic_plt0_entry
[PLT_ENTRY_SIZE
] =
333 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
334 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
335 0, 0, 0, 0 /* pad out to 16 bytes. */
338 /* Subsequent entries in a PIC procedure linkage table look like this. */
340 static const bfd_byte elf_i386_pic_plt_entry
[PLT_ENTRY_SIZE
] =
342 0xff, 0xa3, /* jmp *offset(%ebx) */
343 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
344 0x68, /* pushl immediate */
345 0, 0, 0, 0, /* replaced with offset into relocation table. */
346 0xe9, /* jmp relative */
347 0, 0, 0, 0 /* replaced with offset to start of .plt. */
350 /* The i386 linker needs to keep track of the number of relocs that it
351 decides to copy in check_relocs for each symbol. This is so that
352 it can discard PC relative relocs if it doesn't need them when
353 linking with -Bsymbolic. We store the information in a field
354 extending the regular ELF linker hash table. */
356 /* This structure keeps track of the number of PC relative relocs we
357 have copied for a given symbol. */
359 struct elf_i386_pcrel_relocs_copied
362 struct elf_i386_pcrel_relocs_copied
*next
;
363 /* A section in dynobj. */
365 /* Number of relocs copied in this section. */
369 /* i386 ELF linker hash entry. */
371 struct elf_i386_link_hash_entry
373 struct elf_link_hash_entry root
;
375 /* Number of PC relative relocs copied for this symbol. */
376 struct elf_i386_pcrel_relocs_copied
*pcrel_relocs_copied
;
379 /* i386 ELF linker hash table. */
381 struct elf_i386_link_hash_table
383 struct elf_link_hash_table root
;
386 /* Declare this now that the above structures are defined. */
388 static boolean elf_i386_discard_copies
389 PARAMS ((struct elf_i386_link_hash_entry
*, PTR
));
391 /* Traverse an i386 ELF linker hash table. */
393 #define elf_i386_link_hash_traverse(table, func, info) \
394 (elf_link_hash_traverse \
396 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
399 /* Get the i386 ELF linker hash table from a link_info structure. */
401 #define elf_i386_hash_table(p) \
402 ((struct elf_i386_link_hash_table *) ((p)->hash))
404 /* Create an entry in an i386 ELF linker hash table. */
406 static struct bfd_hash_entry
*
407 elf_i386_link_hash_newfunc (entry
, table
, string
)
408 struct bfd_hash_entry
*entry
;
409 struct bfd_hash_table
*table
;
412 struct elf_i386_link_hash_entry
*ret
=
413 (struct elf_i386_link_hash_entry
*) entry
;
415 /* Allocate the structure if it has not already been allocated by a
417 if (ret
== (struct elf_i386_link_hash_entry
*) NULL
)
418 ret
= ((struct elf_i386_link_hash_entry
*)
419 bfd_hash_allocate (table
,
420 sizeof (struct elf_i386_link_hash_entry
)));
421 if (ret
== (struct elf_i386_link_hash_entry
*) NULL
)
422 return (struct bfd_hash_entry
*) ret
;
424 /* Call the allocation method of the superclass. */
425 ret
= ((struct elf_i386_link_hash_entry
*)
426 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
428 if (ret
!= (struct elf_i386_link_hash_entry
*) NULL
)
430 ret
->pcrel_relocs_copied
= NULL
;
433 return (struct bfd_hash_entry
*) ret
;
436 /* Create an i386 ELF linker hash table. */
438 static struct bfd_link_hash_table
*
439 elf_i386_link_hash_table_create (abfd
)
442 struct elf_i386_link_hash_table
*ret
;
444 ret
= ((struct elf_i386_link_hash_table
*)
445 bfd_alloc (abfd
, sizeof (struct elf_i386_link_hash_table
)));
446 if (ret
== (struct elf_i386_link_hash_table
*) NULL
)
449 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
450 elf_i386_link_hash_newfunc
))
452 bfd_release (abfd
, ret
);
456 return &ret
->root
.root
;
459 /* Look through the relocs for a section during the first phase, and
460 allocate space in the global offset table or procedure linkage
464 elf_i386_check_relocs (abfd
, info
, sec
, relocs
)
466 struct bfd_link_info
*info
;
468 const Elf_Internal_Rela
*relocs
;
471 Elf_Internal_Shdr
*symtab_hdr
;
472 struct elf_link_hash_entry
**sym_hashes
;
473 bfd_signed_vma
*local_got_refcounts
;
474 const Elf_Internal_Rela
*rel
;
475 const Elf_Internal_Rela
*rel_end
;
480 if (info
->relocateable
)
483 dynobj
= elf_hash_table (info
)->dynobj
;
484 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
485 sym_hashes
= elf_sym_hashes (abfd
);
486 local_got_refcounts
= elf_local_got_refcounts (abfd
);
492 rel_end
= relocs
+ sec
->reloc_count
;
493 for (rel
= relocs
; rel
< rel_end
; rel
++)
495 unsigned long r_symndx
;
496 struct elf_link_hash_entry
*h
;
498 r_symndx
= ELF32_R_SYM (rel
->r_info
);
500 if (r_symndx
< symtab_hdr
->sh_info
)
503 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
505 /* Some relocs require a global offset table. */
508 switch (ELF32_R_TYPE (rel
->r_info
))
513 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
514 if (! _bfd_elf_create_got_section (dynobj
, info
))
523 switch (ELF32_R_TYPE (rel
->r_info
))
526 /* This symbol requires a global offset table entry. */
530 sgot
= bfd_get_section_by_name (dynobj
, ".got");
531 BFD_ASSERT (sgot
!= NULL
);
535 && (h
!= NULL
|| info
->shared
))
537 srelgot
= bfd_get_section_by_name (dynobj
, ".rel.got");
540 srelgot
= bfd_make_section (dynobj
, ".rel.got");
542 || ! bfd_set_section_flags (dynobj
, srelgot
,
549 || ! bfd_set_section_alignment (dynobj
, srelgot
, 2))
556 if (h
->got
.refcount
== -1)
560 /* Make sure this symbol is output as a dynamic symbol. */
561 if (h
->dynindx
== -1)
563 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
567 sgot
->_raw_size
+= 4;
568 srelgot
->_raw_size
+= sizeof (Elf32_External_Rel
);
571 h
->got
.refcount
+= 1;
575 /* This is a global offset table entry for a local symbol. */
576 if (local_got_refcounts
== NULL
)
580 size
= symtab_hdr
->sh_info
* sizeof (bfd_signed_vma
);
581 local_got_refcounts
= ((bfd_signed_vma
*)
582 bfd_alloc (abfd
, size
));
583 if (local_got_refcounts
== NULL
)
585 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
586 memset (local_got_refcounts
, -1, size
);
588 if (local_got_refcounts
[r_symndx
] == -1)
590 local_got_refcounts
[r_symndx
] = 1;
592 sgot
->_raw_size
+= 4;
595 /* If we are generating a shared object, we need to
596 output a R_386_RELATIVE reloc so that the dynamic
597 linker can adjust this GOT entry. */
598 srelgot
->_raw_size
+= sizeof (Elf32_External_Rel
);
602 local_got_refcounts
[r_symndx
] += 1;
607 /* This symbol requires a procedure linkage table entry. We
608 actually build the entry in adjust_dynamic_symbol,
609 because this might be a case of linking PIC code which is
610 never referenced by a dynamic object, in which case we
611 don't need to generate a procedure linkage table entry
614 /* If this is a local symbol, we resolve it directly without
615 creating a procedure linkage table entry. */
619 if (h
->plt
.refcount
== -1)
622 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
625 h
->plt
.refcount
+= 1;
631 h
->elf_link_hash_flags
|= ELF_LINK_NON_GOT_REF
;
633 /* If we are creating a shared library, and this is a reloc
634 against a global symbol, or a non PC relative reloc
635 against a local symbol, then we need to copy the reloc
636 into the shared library. However, if we are linking with
637 -Bsymbolic, we do not need to copy a reloc against a
638 global symbol which is defined in an object we are
639 including in the link (i.e., DEF_REGULAR is set). At
640 this point we have not seen all the input files, so it is
641 possible that DEF_REGULAR is not set now but will be set
642 later (it is never cleared). We account for that
643 possibility below by storing information in the
644 pcrel_relocs_copied field of the hash table entry. */
646 && (sec
->flags
& SEC_ALLOC
) != 0
647 && (ELF32_R_TYPE (rel
->r_info
) != R_386_PC32
650 || (h
->elf_link_hash_flags
651 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
653 /* When creating a shared object, we must copy these
654 reloc types into the output file. We create a reloc
655 section in dynobj and make room for this reloc. */
660 name
= (bfd_elf_string_from_elf_section
662 elf_elfheader (abfd
)->e_shstrndx
,
663 elf_section_data (sec
)->rel_hdr
.sh_name
));
667 BFD_ASSERT (strncmp (name
, ".rel", 4) == 0
668 && strcmp (bfd_get_section_name (abfd
, sec
),
671 sreloc
= bfd_get_section_by_name (dynobj
, name
);
676 sreloc
= bfd_make_section (dynobj
, name
);
677 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
678 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
679 if ((sec
->flags
& SEC_ALLOC
) != 0)
680 flags
|= SEC_ALLOC
| SEC_LOAD
;
682 || ! bfd_set_section_flags (dynobj
, sreloc
, flags
)
683 || ! bfd_set_section_alignment (dynobj
, sreloc
, 2))
688 sreloc
->_raw_size
+= sizeof (Elf32_External_Rel
);
690 /* If we are linking with -Bsymbolic, and this is a
691 global symbol, we count the number of PC relative
692 relocations we have entered for this symbol, so that
693 we can discard them again if the symbol is later
694 defined by a regular object. Note that this function
695 is only called if we are using an elf_i386 linker
696 hash table, which means that h is really a pointer to
697 an elf_i386_link_hash_entry. */
698 if (h
!= NULL
&& info
->symbolic
699 && ELF32_R_TYPE (rel
->r_info
) == R_386_PC32
)
701 struct elf_i386_link_hash_entry
*eh
;
702 struct elf_i386_pcrel_relocs_copied
*p
;
704 eh
= (struct elf_i386_link_hash_entry
*) h
;
706 for (p
= eh
->pcrel_relocs_copied
; p
!= NULL
; p
= p
->next
)
707 if (p
->section
== sreloc
)
712 p
= ((struct elf_i386_pcrel_relocs_copied
*)
713 bfd_alloc (dynobj
, sizeof *p
));
716 p
->next
= eh
->pcrel_relocs_copied
;
717 eh
->pcrel_relocs_copied
= p
;
728 /* This relocation describes the C++ object vtable hierarchy.
729 Reconstruct it for later use during GC. */
730 case R_386_GNU_VTINHERIT
:
731 if (!_bfd_elf32_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
735 /* This relocation describes which C++ vtable entries are actually
736 used. Record for later use during GC. */
737 case R_386_GNU_VTENTRY
:
738 if (!_bfd_elf32_gc_record_vtentry (abfd
, sec
, h
, rel
->r_offset
))
750 /* Return the section that should be marked against GC for a given
754 elf_i386_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
756 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
757 Elf_Internal_Rela
*rel
;
758 struct elf_link_hash_entry
*h
;
759 Elf_Internal_Sym
*sym
;
763 switch (ELF32_R_TYPE (rel
->r_info
))
765 case R_386_GNU_VTINHERIT
:
766 case R_386_GNU_VTENTRY
:
770 switch (h
->root
.type
)
772 case bfd_link_hash_defined
:
773 case bfd_link_hash_defweak
:
774 return h
->root
.u
.def
.section
;
776 case bfd_link_hash_common
:
777 return h
->root
.u
.c
.p
->section
;
786 if (!(elf_bad_symtab (abfd
)
787 && ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
788 && ! ((sym
->st_shndx
<= 0 || sym
->st_shndx
>= SHN_LORESERVE
)
789 && sym
->st_shndx
!= SHN_COMMON
))
791 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
798 /* Update the got entry reference counts for the section being removed. */
801 elf_i386_gc_sweep_hook (abfd
, info
, sec
, relocs
)
803 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
805 const Elf_Internal_Rela
*relocs
;
807 Elf_Internal_Shdr
*symtab_hdr
;
808 struct elf_link_hash_entry
**sym_hashes
;
809 bfd_signed_vma
*local_got_refcounts
;
810 const Elf_Internal_Rela
*rel
, *relend
;
811 unsigned long r_symndx
;
812 struct elf_link_hash_entry
*h
;
817 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
818 sym_hashes
= elf_sym_hashes (abfd
);
819 local_got_refcounts
= elf_local_got_refcounts (abfd
);
821 dynobj
= elf_hash_table (info
)->dynobj
;
825 sgot
= bfd_get_section_by_name (dynobj
, ".got");
826 srelgot
= bfd_get_section_by_name (dynobj
, ".rel.got");
828 relend
= relocs
+ sec
->reloc_count
;
829 for (rel
= relocs
; rel
< relend
; rel
++)
830 switch (ELF32_R_TYPE (rel
->r_info
))
835 r_symndx
= ELF32_R_SYM (rel
->r_info
);
836 if (r_symndx
>= symtab_hdr
->sh_info
)
838 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
839 if (h
->got
.refcount
> 0)
841 h
->got
.refcount
-= 1;
842 if (h
->got
.refcount
== 0)
844 sgot
->_raw_size
-= 4;
845 srelgot
->_raw_size
-= sizeof (Elf32_External_Rel
);
849 else if (local_got_refcounts
!= NULL
)
851 if (local_got_refcounts
[r_symndx
] > 0)
853 local_got_refcounts
[r_symndx
] -= 1;
854 if (local_got_refcounts
[r_symndx
] == 0)
856 sgot
->_raw_size
-= 4;
858 srelgot
->_raw_size
-= sizeof (Elf32_External_Rel
);
865 r_symndx
= ELF32_R_SYM (rel
->r_info
);
866 if (r_symndx
>= symtab_hdr
->sh_info
)
868 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
869 if (h
->plt
.refcount
> 0)
870 h
->plt
.refcount
-= 1;
881 /* Adjust a symbol defined by a dynamic object and referenced by a
882 regular object. The current definition is in some section of the
883 dynamic object, but we're not including those sections. We have to
884 change the definition to something the rest of the link can
888 elf_i386_adjust_dynamic_symbol (info
, h
)
889 struct bfd_link_info
*info
;
890 struct elf_link_hash_entry
*h
;
894 unsigned int power_of_two
;
896 dynobj
= elf_hash_table (info
)->dynobj
;
898 /* Make sure we know what is going on here. */
899 BFD_ASSERT (dynobj
!= NULL
900 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
901 || h
->weakdef
!= NULL
902 || ((h
->elf_link_hash_flags
903 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
904 && (h
->elf_link_hash_flags
905 & ELF_LINK_HASH_REF_REGULAR
) != 0
906 && (h
->elf_link_hash_flags
907 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
909 /* If this is a function, put it in the procedure linkage table. We
910 will fill in the contents of the procedure linkage table later,
911 when we know the address of the .got section. */
912 if (h
->type
== STT_FUNC
913 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
916 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
917 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
918 || (info
->shared
&& h
->plt
.refcount
<= 0))
920 /* This case can occur if we saw a PLT32 reloc in an input
921 file, but the symbol was never referred to by a dynamic
922 object, or if all references were garbage collected. In
923 such a case, we don't actually need to build a procedure
924 linkage table, and we can just do a PC32 reloc instead. */
925 h
->plt
.offset
= (bfd_vma
) -1;
926 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
930 /* Make sure this symbol is output as a dynamic symbol. */
931 if (h
->dynindx
== -1)
933 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
937 s
= bfd_get_section_by_name (dynobj
, ".plt");
938 BFD_ASSERT (s
!= NULL
);
940 /* If this is the first .plt entry, make room for the special
942 if (s
->_raw_size
== 0)
943 s
->_raw_size
+= PLT_ENTRY_SIZE
;
945 /* If this symbol is not defined in a regular file, and we are
946 not generating a shared library, then set the symbol to this
947 location in the .plt. This is required to make function
948 pointers compare as equal between the normal executable and
949 the shared library. */
951 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
953 h
->root
.u
.def
.section
= s
;
954 h
->root
.u
.def
.value
= s
->_raw_size
;
957 h
->plt
.offset
= s
->_raw_size
;
959 /* Make room for this entry. */
960 s
->_raw_size
+= PLT_ENTRY_SIZE
;
962 /* We also need to make an entry in the .got.plt section, which
963 will be placed in the .got section by the linker script. */
964 s
= bfd_get_section_by_name (dynobj
, ".got.plt");
965 BFD_ASSERT (s
!= NULL
);
968 /* We also need to make an entry in the .rel.plt section. */
969 s
= bfd_get_section_by_name (dynobj
, ".rel.plt");
970 BFD_ASSERT (s
!= NULL
);
971 s
->_raw_size
+= sizeof (Elf32_External_Rel
);
976 /* If this is a weak symbol, and there is a real definition, the
977 processor independent code will have arranged for us to see the
978 real definition first, and we can just use the same value. */
979 if (h
->weakdef
!= NULL
)
981 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
982 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
983 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
984 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
988 /* This is a reference to a symbol defined by a dynamic object which
989 is not a function. */
991 /* If we are creating a shared library, we must presume that the
992 only references to the symbol are via the global offset table.
993 For such cases we need not do anything here; the relocations will
994 be handled correctly by relocate_section. */
998 /* If there are no references to this symbol that do not use the
999 GOT, we don't need to generate a copy reloc. */
1000 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0)
1003 /* We must allocate the symbol in our .dynbss section, which will
1004 become part of the .bss section of the executable. There will be
1005 an entry for this symbol in the .dynsym section. The dynamic
1006 object will contain position independent code, so all references
1007 from the dynamic object to this symbol will go through the global
1008 offset table. The dynamic linker will use the .dynsym entry to
1009 determine the address it must put in the global offset table, so
1010 both the dynamic object and the regular object will refer to the
1011 same memory location for the variable. */
1013 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
1014 BFD_ASSERT (s
!= NULL
);
1016 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1017 copy the initial value out of the dynamic object and into the
1018 runtime process image. We need to remember the offset into the
1019 .rel.bss section we are going to use. */
1020 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
1024 srel
= bfd_get_section_by_name (dynobj
, ".rel.bss");
1025 BFD_ASSERT (srel
!= NULL
);
1026 srel
->_raw_size
+= sizeof (Elf32_External_Rel
);
1027 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
1030 /* We need to figure out the alignment required for this symbol. I
1031 have no idea how ELF linkers handle this. */
1032 power_of_two
= bfd_log2 (h
->size
);
1033 if (power_of_two
> 3)
1036 /* Apply the required alignment. */
1037 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
,
1038 (bfd_size_type
) (1 << power_of_two
));
1039 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
1041 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
1045 /* Define the symbol as being at this point in the section. */
1046 h
->root
.u
.def
.section
= s
;
1047 h
->root
.u
.def
.value
= s
->_raw_size
;
1049 /* Increment the section size to make room for the symbol. */
1050 s
->_raw_size
+= h
->size
;
1055 /* Set the sizes of the dynamic sections. */
1058 elf_i386_size_dynamic_sections (output_bfd
, info
)
1060 struct bfd_link_info
*info
;
1068 dynobj
= elf_hash_table (info
)->dynobj
;
1069 BFD_ASSERT (dynobj
!= NULL
);
1071 if (elf_hash_table (info
)->dynamic_sections_created
)
1073 /* Set the contents of the .interp section to the interpreter. */
1076 s
= bfd_get_section_by_name (dynobj
, ".interp");
1077 BFD_ASSERT (s
!= NULL
);
1078 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1079 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1084 /* We may have created entries in the .rel.got section.
1085 However, if we are not creating the dynamic sections, we will
1086 not actually use these entries. Reset the size of .rel.got,
1087 which will cause it to get stripped from the output file
1089 s
= bfd_get_section_by_name (dynobj
, ".rel.got");
1094 /* If this is a -Bsymbolic shared link, then we need to discard all
1095 PC relative relocs against symbols defined in a regular object.
1096 We allocated space for them in the check_relocs routine, but we
1097 will not fill them in in the relocate_section routine. */
1098 if (info
->shared
&& info
->symbolic
)
1099 elf_i386_link_hash_traverse (elf_i386_hash_table (info
),
1100 elf_i386_discard_copies
,
1103 /* The check_relocs and adjust_dynamic_symbol entry points have
1104 determined the sizes of the various dynamic sections. Allocate
1109 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1114 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1117 /* It's OK to base decisions on the section name, because none
1118 of the dynobj section names depend upon the input files. */
1119 name
= bfd_get_section_name (dynobj
, s
);
1123 if (strcmp (name
, ".plt") == 0)
1125 if (s
->_raw_size
== 0)
1127 /* Strip this section if we don't need it; see the
1133 /* Remember whether there is a PLT. */
1137 else if (strncmp (name
, ".rel", 4) == 0)
1139 if (s
->_raw_size
== 0)
1141 /* If we don't need this section, strip it from the
1142 output file. This is mostly to handle .rel.bss and
1143 .rel.plt. We must create both sections in
1144 create_dynamic_sections, because they must be created
1145 before the linker maps input sections to output
1146 sections. The linker does that before
1147 adjust_dynamic_symbol is called, and it is that
1148 function which decides whether anything needs to go
1149 into these sections. */
1156 /* Remember whether there are any reloc sections other
1158 if (strcmp (name
, ".rel.plt") != 0)
1160 const char *outname
;
1164 /* If this relocation section applies to a read only
1165 section, then we probably need a DT_TEXTREL
1166 entry. The entries in the .rel.plt section
1167 really apply to the .got section, which we
1168 created ourselves and so know is not readonly. */
1169 outname
= bfd_get_section_name (output_bfd
,
1171 target
= bfd_get_section_by_name (output_bfd
, outname
+ 4);
1173 && (target
->flags
& SEC_READONLY
) != 0
1174 && (target
->flags
& SEC_ALLOC
) != 0)
1178 /* We use the reloc_count field as a counter if we need
1179 to copy relocs into the output file. */
1183 else if (strncmp (name
, ".got", 4) != 0)
1185 /* It's not one of our sections, so don't allocate space. */
1191 _bfd_strip_section_from_output (info
, s
);
1195 /* Allocate memory for the section contents. */
1196 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
1197 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
1201 if (elf_hash_table (info
)->dynamic_sections_created
)
1203 /* Add some entries to the .dynamic section. We fill in the
1204 values later, in elf_i386_finish_dynamic_sections, but we
1205 must add the entries now so that we get the correct size for
1206 the .dynamic section. The DT_DEBUG entry is filled in by the
1207 dynamic linker and used by the debugger. */
1210 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
1216 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0)
1217 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
1218 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTREL
, DT_REL
)
1219 || ! bfd_elf32_add_dynamic_entry (info
, DT_JMPREL
, 0))
1225 if (! bfd_elf32_add_dynamic_entry (info
, DT_REL
, 0)
1226 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELSZ
, 0)
1227 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELENT
,
1228 sizeof (Elf32_External_Rel
)))
1234 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
1242 /* This function is called via elf_i386_link_hash_traverse if we are
1243 creating a shared object with -Bsymbolic. It discards the space
1244 allocated to copy PC relative relocs against symbols which are
1245 defined in regular objects. We allocated space for them in the
1246 check_relocs routine, but we won't fill them in in the
1247 relocate_section routine. */
1251 elf_i386_discard_copies (h
, ignore
)
1252 struct elf_i386_link_hash_entry
*h
;
1253 PTR ignore ATTRIBUTE_UNUSED
;
1255 struct elf_i386_pcrel_relocs_copied
*s
;
1257 /* We only discard relocs for symbols defined in a regular object. */
1258 if ((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1261 for (s
= h
->pcrel_relocs_copied
; s
!= NULL
; s
= s
->next
)
1262 s
->section
->_raw_size
-= s
->count
* sizeof (Elf32_External_Rel
);
1267 /* Relocate an i386 ELF section. */
1270 elf_i386_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1271 contents
, relocs
, local_syms
, local_sections
)
1273 struct bfd_link_info
*info
;
1275 asection
*input_section
;
1277 Elf_Internal_Rela
*relocs
;
1278 Elf_Internal_Sym
*local_syms
;
1279 asection
**local_sections
;
1282 Elf_Internal_Shdr
*symtab_hdr
;
1283 struct elf_link_hash_entry
**sym_hashes
;
1284 bfd_vma
*local_got_offsets
;
1288 Elf_Internal_Rela
*rel
;
1289 Elf_Internal_Rela
*relend
;
1291 dynobj
= elf_hash_table (info
)->dynobj
;
1292 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1293 sym_hashes
= elf_sym_hashes (input_bfd
);
1294 local_got_offsets
= elf_local_got_offsets (input_bfd
);
1301 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1302 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1306 relend
= relocs
+ input_section
->reloc_count
;
1307 for (; rel
< relend
; rel
++)
1310 reloc_howto_type
*howto
;
1311 unsigned long r_symndx
;
1312 struct elf_link_hash_entry
*h
;
1313 Elf_Internal_Sym
*sym
;
1316 bfd_reloc_status_type r
;
1319 r_type
= ELF32_R_TYPE (rel
->r_info
);
1320 if (r_type
== R_386_GNU_VTINHERIT
1321 || r_type
== R_386_GNU_VTENTRY
)
1323 if ((indx
= (unsigned) r_type
) >= R_386_standard
1324 && ((indx
= (unsigned) r_type
- R_386_ext_offset
)
1325 >= sizeof (elf_howto_table
) / sizeof (elf_howto_table
[0])))
1327 bfd_set_error (bfd_error_bad_value
);
1330 howto
= elf_howto_table
+ indx
;
1332 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1334 if (info
->relocateable
)
1336 /* This is a relocateable link. We don't have to change
1337 anything, unless the reloc is against a section symbol,
1338 in which case we have to adjust according to where the
1339 section symbol winds up in the output section. */
1340 if (r_symndx
< symtab_hdr
->sh_info
)
1342 sym
= local_syms
+ r_symndx
;
1343 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1347 sec
= local_sections
[r_symndx
];
1348 val
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
1349 val
+= sec
->output_offset
+ sym
->st_value
;
1350 bfd_put_32 (input_bfd
, val
, contents
+ rel
->r_offset
);
1357 /* This is a final link. */
1361 if (r_symndx
< symtab_hdr
->sh_info
)
1363 sym
= local_syms
+ r_symndx
;
1364 sec
= local_sections
[r_symndx
];
1365 relocation
= (sec
->output_section
->vma
1366 + sec
->output_offset
1371 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1372 while (h
->root
.type
== bfd_link_hash_indirect
1373 || h
->root
.type
== bfd_link_hash_warning
)
1374 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1375 if (h
->root
.type
== bfd_link_hash_defined
1376 || h
->root
.type
== bfd_link_hash_defweak
)
1378 sec
= h
->root
.u
.def
.section
;
1379 if (r_type
== R_386_GOTPC
1380 || (r_type
== R_386_PLT32
1382 && h
->plt
.offset
!= (bfd_vma
) -1)
1383 || (r_type
== R_386_GOT32
1384 && elf_hash_table (info
)->dynamic_sections_created
1386 || (! info
->symbolic
&& h
->dynindx
!= -1)
1387 || (h
->elf_link_hash_flags
1388 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
1390 && ((! info
->symbolic
&& h
->dynindx
!= -1)
1391 || (h
->elf_link_hash_flags
1392 & ELF_LINK_HASH_DEF_REGULAR
) == 0)
1393 && (r_type
== R_386_32
1394 || r_type
== R_386_PC32
)
1395 && ((input_section
->flags
& SEC_ALLOC
) != 0
1396 /* DWARF will emit R_386_32 relocations in its
1397 sections against symbols defined externally
1398 in shared libraries. We can't do anything
1400 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
1401 && (h
->elf_link_hash_flags
1402 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0))))
1404 /* In these cases, we don't need the relocation
1405 value. We check specially because in some
1406 obscure cases sec->output_section will be NULL. */
1409 else if (sec
->output_section
== NULL
)
1411 (*_bfd_error_handler
)
1412 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1413 bfd_get_filename (input_bfd
), h
->root
.root
.string
,
1414 bfd_get_section_name (input_bfd
, input_section
));
1418 relocation
= (h
->root
.u
.def
.value
1419 + sec
->output_section
->vma
1420 + sec
->output_offset
);
1422 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1424 else if (info
->shared
&& !info
->symbolic
1425 && !info
->no_undefined
1426 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
1430 if (! ((*info
->callbacks
->undefined_symbol
)
1431 (info
, h
->root
.root
.string
, input_bfd
,
1432 input_section
, rel
->r_offset
,
1433 (!info
->shared
|| info
->no_undefined
1434 || ELF_ST_VISIBILITY (h
->other
)))))
1443 /* Relocation is to the entry for this symbol in the global
1445 BFD_ASSERT (sgot
!= NULL
);
1451 off
= h
->got
.offset
;
1452 BFD_ASSERT (off
!= (bfd_vma
) -1);
1454 if (! elf_hash_table (info
)->dynamic_sections_created
1456 && (info
->symbolic
|| h
->dynindx
== -1)
1457 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
1459 /* This is actually a static link, or it is a
1460 -Bsymbolic link and the symbol is defined
1461 locally, or the symbol was forced to be local
1462 because of a version file. We must initialize
1463 this entry in the global offset table. Since the
1464 offset must always be a multiple of 4, we use the
1465 least significant bit to record whether we have
1466 initialized it already.
1468 When doing a dynamic link, we create a .rel.got
1469 relocation entry to initialize the value. This
1470 is done in the finish_dynamic_symbol routine. */
1475 bfd_put_32 (output_bfd
, relocation
,
1476 sgot
->contents
+ off
);
1481 relocation
= sgot
->output_offset
+ off
;
1487 BFD_ASSERT (local_got_offsets
!= NULL
1488 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1490 off
= local_got_offsets
[r_symndx
];
1492 /* The offset must always be a multiple of 4. We use
1493 the least significant bit to record whether we have
1494 already generated the necessary reloc. */
1499 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
1504 Elf_Internal_Rel outrel
;
1506 srelgot
= bfd_get_section_by_name (dynobj
, ".rel.got");
1507 BFD_ASSERT (srelgot
!= NULL
);
1509 outrel
.r_offset
= (sgot
->output_section
->vma
1510 + sgot
->output_offset
1512 outrel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1513 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
1514 (((Elf32_External_Rel
*)
1516 + srelgot
->reloc_count
));
1517 ++srelgot
->reloc_count
;
1520 local_got_offsets
[r_symndx
] |= 1;
1523 relocation
= sgot
->output_offset
+ off
;
1529 /* Relocation is relative to the start of the global offset
1534 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1535 BFD_ASSERT (sgot
!= NULL
);
1538 /* Note that sgot->output_offset is not involved in this
1539 calculation. We always want the start of .got. If we
1540 defined _GLOBAL_OFFSET_TABLE in a different way, as is
1541 permitted by the ABI, we might have to change this
1543 relocation
-= sgot
->output_section
->vma
;
1548 /* Use global offset table as symbol value. */
1552 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1553 BFD_ASSERT (sgot
!= NULL
);
1556 relocation
= sgot
->output_section
->vma
;
1561 /* Relocation is to the entry for this symbol in the
1562 procedure linkage table. */
1564 /* Resolve a PLT32 reloc against a local symbol directly,
1565 without using the procedure linkage table. */
1569 if (h
->plt
.offset
== (bfd_vma
) -1
1572 /* We didn't make a PLT entry for this symbol. This
1573 happens when statically linking PIC code, or when
1574 using -Bsymbolic. */
1578 relocation
= (splt
->output_section
->vma
1579 + splt
->output_offset
1587 && (input_section
->flags
& SEC_ALLOC
) != 0
1588 && (r_type
!= R_386_PC32
1591 && (! info
->symbolic
1592 || (h
->elf_link_hash_flags
1593 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
1595 Elf_Internal_Rel outrel
;
1596 boolean skip
, relocate
;
1598 /* When generating a shared object, these relocations
1599 are copied into the output file to be resolved at run
1606 name
= (bfd_elf_string_from_elf_section
1608 elf_elfheader (input_bfd
)->e_shstrndx
,
1609 elf_section_data (input_section
)->rel_hdr
.sh_name
));
1613 BFD_ASSERT (strncmp (name
, ".rel", 4) == 0
1614 && strcmp (bfd_get_section_name (input_bfd
,
1618 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1619 BFD_ASSERT (sreloc
!= NULL
);
1624 if (elf_section_data (input_section
)->stab_info
== NULL
)
1625 outrel
.r_offset
= rel
->r_offset
;
1630 off
= (_bfd_stab_section_offset
1631 (output_bfd
, &elf_hash_table (info
)->stab_info
,
1633 &elf_section_data (input_section
)->stab_info
,
1635 if (off
== (bfd_vma
) -1)
1637 outrel
.r_offset
= off
;
1640 outrel
.r_offset
+= (input_section
->output_section
->vma
1641 + input_section
->output_offset
);
1645 memset (&outrel
, 0, sizeof outrel
);
1648 else if (r_type
== R_386_PC32
)
1650 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
1652 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_PC32
);
1656 /* h->dynindx may be -1 if this symbol was marked to
1659 || ((info
->symbolic
|| h
->dynindx
== -1)
1660 && (h
->elf_link_hash_flags
1661 & ELF_LINK_HASH_DEF_REGULAR
) != 0))
1664 outrel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1668 BFD_ASSERT (h
->dynindx
!= -1);
1670 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_32
);
1674 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
1675 (((Elf32_External_Rel
*)
1677 + sreloc
->reloc_count
));
1678 ++sreloc
->reloc_count
;
1680 /* If this reloc is against an external symbol, we do
1681 not want to fiddle with the addend. Otherwise, we
1682 need to include the symbol value so that it becomes
1683 an addend for the dynamic reloc. */
1694 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1695 contents
, rel
->r_offset
,
1696 relocation
, (bfd_vma
) 0);
1698 if (r
!= bfd_reloc_ok
)
1703 case bfd_reloc_outofrange
:
1705 case bfd_reloc_overflow
:
1710 name
= h
->root
.root
.string
;
1713 name
= bfd_elf_string_from_elf_section (input_bfd
,
1714 symtab_hdr
->sh_link
,
1719 name
= bfd_section_name (input_bfd
, sec
);
1721 if (! ((*info
->callbacks
->reloc_overflow
)
1722 (info
, name
, howto
->name
, (bfd_vma
) 0,
1723 input_bfd
, input_section
, rel
->r_offset
)))
1734 /* Finish up dynamic symbol handling. We set the contents of various
1735 dynamic sections here. */
1738 elf_i386_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
1740 struct bfd_link_info
*info
;
1741 struct elf_link_hash_entry
*h
;
1742 Elf_Internal_Sym
*sym
;
1746 dynobj
= elf_hash_table (info
)->dynobj
;
1748 if (h
->plt
.offset
!= (bfd_vma
) -1)
1755 Elf_Internal_Rel rel
;
1757 /* This symbol has an entry in the procedure linkage table. Set
1760 BFD_ASSERT (h
->dynindx
!= -1);
1762 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1763 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
1764 srel
= bfd_get_section_by_name (dynobj
, ".rel.plt");
1765 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
1767 /* Get the index in the procedure linkage table which
1768 corresponds to this symbol. This is the index of this symbol
1769 in all the symbols for which we are making plt entries. The
1770 first entry in the procedure linkage table is reserved. */
1771 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
1773 /* Get the offset into the .got table of the entry that
1774 corresponds to this function. Each .got entry is 4 bytes.
1775 The first three are reserved. */
1776 got_offset
= (plt_index
+ 3) * 4;
1778 /* Fill in the entry in the procedure linkage table. */
1781 memcpy (splt
->contents
+ h
->plt
.offset
, elf_i386_plt_entry
,
1783 bfd_put_32 (output_bfd
,
1784 (sgot
->output_section
->vma
1785 + sgot
->output_offset
1787 splt
->contents
+ h
->plt
.offset
+ 2);
1791 memcpy (splt
->contents
+ h
->plt
.offset
, elf_i386_pic_plt_entry
,
1793 bfd_put_32 (output_bfd
, got_offset
,
1794 splt
->contents
+ h
->plt
.offset
+ 2);
1797 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rel
),
1798 splt
->contents
+ h
->plt
.offset
+ 7);
1799 bfd_put_32 (output_bfd
, - (h
->plt
.offset
+ PLT_ENTRY_SIZE
),
1800 splt
->contents
+ h
->plt
.offset
+ 12);
1802 /* Fill in the entry in the global offset table. */
1803 bfd_put_32 (output_bfd
,
1804 (splt
->output_section
->vma
1805 + splt
->output_offset
1808 sgot
->contents
+ got_offset
);
1810 /* Fill in the entry in the .rel.plt section. */
1811 rel
.r_offset
= (sgot
->output_section
->vma
1812 + sgot
->output_offset
1814 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_JUMP_SLOT
);
1815 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1816 ((Elf32_External_Rel
*) srel
->contents
1819 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1821 /* Mark the symbol as undefined, rather than as defined in
1822 the .plt section. Leave the value alone. */
1823 sym
->st_shndx
= SHN_UNDEF
;
1827 if (h
->got
.offset
!= (bfd_vma
) -1)
1831 Elf_Internal_Rel rel
;
1833 /* This symbol has an entry in the global offset table. Set it
1836 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1837 srel
= bfd_get_section_by_name (dynobj
, ".rel.got");
1838 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
1840 rel
.r_offset
= (sgot
->output_section
->vma
1841 + sgot
->output_offset
1842 + (h
->got
.offset
&~ 1));
1844 /* If this is a static link, or it is a -Bsymbolic link and the
1845 symbol is defined locally or was forced to be local because
1846 of a version file, we just want to emit a RELATIVE reloc.
1847 The entry in the global offset table will already have been
1848 initialized in the relocate_section function. */
1849 if (! elf_hash_table (info
)->dynamic_sections_created
1851 && (info
->symbolic
|| h
->dynindx
== -1)
1852 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
1854 rel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1858 BFD_ASSERT((h
->got
.offset
& 1) == 0);
1859 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
1860 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_GLOB_DAT
);
1863 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1864 ((Elf32_External_Rel
*) srel
->contents
1865 + srel
->reloc_count
));
1866 ++srel
->reloc_count
;
1869 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
1872 Elf_Internal_Rel rel
;
1874 /* This symbol needs a copy reloc. Set it up. */
1876 BFD_ASSERT (h
->dynindx
!= -1
1877 && (h
->root
.type
== bfd_link_hash_defined
1878 || h
->root
.type
== bfd_link_hash_defweak
));
1880 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
1882 BFD_ASSERT (s
!= NULL
);
1884 rel
.r_offset
= (h
->root
.u
.def
.value
1885 + h
->root
.u
.def
.section
->output_section
->vma
1886 + h
->root
.u
.def
.section
->output_offset
);
1887 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_COPY
);
1888 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1889 ((Elf32_External_Rel
*) s
->contents
1894 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1895 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
1896 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
1897 sym
->st_shndx
= SHN_ABS
;
1902 /* Finish up the dynamic sections. */
1905 elf_i386_finish_dynamic_sections (output_bfd
, info
)
1907 struct bfd_link_info
*info
;
1913 dynobj
= elf_hash_table (info
)->dynobj
;
1915 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
1916 BFD_ASSERT (sgot
!= NULL
);
1917 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
1919 if (elf_hash_table (info
)->dynamic_sections_created
)
1922 Elf32_External_Dyn
*dyncon
, *dynconend
;
1924 BFD_ASSERT (sdyn
!= NULL
);
1926 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
1927 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
1928 for (; dyncon
< dynconend
; dyncon
++)
1930 Elf_Internal_Dyn dyn
;
1934 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
1947 s
= bfd_get_section_by_name (output_bfd
, name
);
1948 BFD_ASSERT (s
!= NULL
);
1949 dyn
.d_un
.d_ptr
= s
->vma
;
1950 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1954 s
= bfd_get_section_by_name (output_bfd
, ".rel.plt");
1955 BFD_ASSERT (s
!= NULL
);
1956 if (s
->_cooked_size
!= 0)
1957 dyn
.d_un
.d_val
= s
->_cooked_size
;
1959 dyn
.d_un
.d_val
= s
->_raw_size
;
1960 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1964 /* My reading of the SVR4 ABI indicates that the
1965 procedure linkage table relocs (DT_JMPREL) should be
1966 included in the overall relocs (DT_REL). This is
1967 what Solaris does. However, UnixWare can not handle
1968 that case. Therefore, we override the DT_RELSZ entry
1969 here to make it not include the JMPREL relocs. Since
1970 the linker script arranges for .rel.plt to follow all
1971 other relocation sections, we don't have to worry
1972 about changing the DT_REL entry. */
1973 s
= bfd_get_section_by_name (output_bfd
, ".rel.plt");
1976 if (s
->_cooked_size
!= 0)
1977 dyn
.d_un
.d_val
-= s
->_cooked_size
;
1979 dyn
.d_un
.d_val
-= s
->_raw_size
;
1981 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1986 /* Fill in the first entry in the procedure linkage table. */
1987 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1988 if (splt
&& splt
->_raw_size
> 0)
1991 memcpy (splt
->contents
, elf_i386_pic_plt0_entry
, PLT_ENTRY_SIZE
);
1994 memcpy (splt
->contents
, elf_i386_plt0_entry
, PLT_ENTRY_SIZE
);
1995 bfd_put_32 (output_bfd
,
1996 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
1997 splt
->contents
+ 2);
1998 bfd_put_32 (output_bfd
,
1999 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
2000 splt
->contents
+ 8);
2003 /* UnixWare sets the entsize of .plt to 4, although that doesn't
2004 really seem like the right value. */
2005 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
2009 /* Fill in the first three entries in the global offset table. */
2010 if (sgot
->_raw_size
> 0)
2013 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
2015 bfd_put_32 (output_bfd
,
2016 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
2018 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
2019 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
2022 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
2027 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
2028 #define TARGET_LITTLE_NAME "elf32-i386"
2029 #define ELF_ARCH bfd_arch_i386
2030 #define ELF_MACHINE_CODE EM_386
2031 #define ELF_MAXPAGESIZE 0x1000
2033 #define elf_backend_can_gc_sections 1
2034 #define elf_backend_want_got_plt 1
2035 #define elf_backend_plt_readonly 1
2036 #define elf_backend_want_plt_sym 0
2037 #define elf_backend_got_header_size 12
2038 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
2040 #define elf_info_to_howto elf_i386_info_to_howto
2041 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
2043 #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
2044 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
2045 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
2046 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
2048 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
2049 #define elf_backend_check_relocs elf_i386_check_relocs
2050 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
2051 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
2052 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
2053 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
2054 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
2055 #define elf_backend_relocate_section elf_i386_relocate_section
2056 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
2058 #include "elf32-target.h"