1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright 1993, 94-98, 1999 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 static reloc_howto_type
*elf_i386_reloc_type_lookup
27 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
28 static void elf_i386_info_to_howto
29 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rela
*));
30 static void elf_i386_info_to_howto_rel
31 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rel
*));
32 static boolean elf_i386_is_local_label_name
PARAMS ((bfd
*, const char *));
33 static struct bfd_hash_entry
*elf_i386_link_hash_newfunc
34 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
35 static struct bfd_link_hash_table
*elf_i386_link_hash_table_create
37 static boolean elf_i386_check_relocs
38 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
39 const Elf_Internal_Rela
*));
40 static boolean elf_i386_adjust_dynamic_symbol
41 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
42 static boolean elf_i386_size_dynamic_sections
43 PARAMS ((bfd
*, struct bfd_link_info
*));
44 static boolean elf_i386_relocate_section
45 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
46 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
47 static boolean elf_i386_finish_dynamic_symbol
48 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
50 static boolean elf_i386_finish_dynamic_sections
51 PARAMS ((bfd
*, struct bfd_link_info
*));
53 #define USE_REL 1 /* 386 uses REL relocations instead of RELA */
57 static reloc_howto_type elf_howto_table
[]=
59 HOWTO(R_386_NONE
, 0, 0, 0, false, 0, complain_overflow_bitfield
,
60 bfd_elf_generic_reloc
, "R_386_NONE",
61 true, 0x00000000, 0x00000000, false),
62 HOWTO(R_386_32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
63 bfd_elf_generic_reloc
, "R_386_32",
64 true, 0xffffffff, 0xffffffff, false),
65 HOWTO(R_386_PC32
, 0, 2, 32, true, 0, complain_overflow_bitfield
,
66 bfd_elf_generic_reloc
, "R_386_PC32",
67 true, 0xffffffff, 0xffffffff, true),
68 HOWTO(R_386_GOT32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
69 bfd_elf_generic_reloc
, "R_386_GOT32",
70 true, 0xffffffff, 0xffffffff, false),
71 HOWTO(R_386_PLT32
, 0, 2, 32, true, 0, complain_overflow_bitfield
,
72 bfd_elf_generic_reloc
, "R_386_PLT32",
73 true, 0xffffffff, 0xffffffff, true),
74 HOWTO(R_386_COPY
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
75 bfd_elf_generic_reloc
, "R_386_COPY",
76 true, 0xffffffff, 0xffffffff, false),
77 HOWTO(R_386_GLOB_DAT
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
78 bfd_elf_generic_reloc
, "R_386_GLOB_DAT",
79 true, 0xffffffff, 0xffffffff, false),
80 HOWTO(R_386_JUMP_SLOT
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
81 bfd_elf_generic_reloc
, "R_386_JUMP_SLOT",
82 true, 0xffffffff, 0xffffffff, false),
83 HOWTO(R_386_RELATIVE
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
84 bfd_elf_generic_reloc
, "R_386_RELATIVE",
85 true, 0xffffffff, 0xffffffff, false),
86 HOWTO(R_386_GOTOFF
, 0, 2, 32, false, 0, complain_overflow_bitfield
,
87 bfd_elf_generic_reloc
, "R_386_GOTOFF",
88 true, 0xffffffff, 0xffffffff, false),
89 HOWTO(R_386_GOTPC
, 0, 2, 32, true, 0, complain_overflow_bitfield
,
90 bfd_elf_generic_reloc
, "R_386_GOTPC",
91 true, 0xffffffff, 0xffffffff, true),
93 #define R_386_standard ((unsigned int) R_386_GOTPC + 1)
94 #define R_386_ext_offset ((unsigned int) R_386_16 - R_386_standard)
96 /* The remaining relocs are a GNU extension. */
97 HOWTO(R_386_16
, 0, 1, 16, false, 0, complain_overflow_bitfield
,
98 bfd_elf_generic_reloc
, "R_386_16",
99 true, 0xffff, 0xffff, false),
100 HOWTO(R_386_PC16
, 0, 1, 16, true, 0, complain_overflow_bitfield
,
101 bfd_elf_generic_reloc
, "R_386_PC16",
102 true, 0xffff, 0xffff, true),
103 HOWTO(R_386_8
, 0, 0, 8, false, 0, complain_overflow_bitfield
,
104 bfd_elf_generic_reloc
, "R_386_8",
105 true, 0xff, 0xff, false),
106 HOWTO(R_386_PC8
, 0, 0, 8, true, 0, complain_overflow_signed
,
107 bfd_elf_generic_reloc
, "R_386_PC8",
108 true, 0xff, 0xff, true)
111 /* GNU extension to record C++ vtable hierarchy. */
112 static reloc_howto_type elf32_i386_vtinherit_howto
=
113 HOWTO (R_386_GNU_VTINHERIT
, /* type */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
117 false, /* pc_relative */
119 complain_overflow_dont
, /* complain_on_overflow */
120 NULL
, /* special_function */
121 "R_386_GNU_VTINHERIT", /* name */
122 false, /* partial_inplace */
127 /* GNU extension to record C++ vtable member usage. */
128 static reloc_howto_type elf32_i386_vtentry_howto
=
129 HOWTO (R_386_GNU_VTENTRY
, /* type */
131 2, /* size (0 = byte, 1 = short, 2 = long) */
133 false, /* pc_relative */
135 complain_overflow_dont
, /* complain_on_overflow */
136 _bfd_elf_rel_vtable_reloc_fn
, /* special_function */
137 "R_386_GNU_VTENTRY", /* name */
138 false, /* partial_inplace */
143 #ifdef DEBUG_GEN_RELOC
144 #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
149 static reloc_howto_type
*
150 elf_i386_reloc_type_lookup (abfd
, code
)
151 bfd
*abfd ATTRIBUTE_UNUSED
;
152 bfd_reloc_code_real_type code
;
157 TRACE ("BFD_RELOC_NONE");
158 return &elf_howto_table
[(unsigned int) R_386_NONE
];
161 TRACE ("BFD_RELOC_32");
162 return &elf_howto_table
[(unsigned int) R_386_32
];
165 TRACE ("BFD_RELOC_CTOR");
166 return &elf_howto_table
[(unsigned int) R_386_32
];
168 case BFD_RELOC_32_PCREL
:
169 TRACE ("BFD_RELOC_PC32");
170 return &elf_howto_table
[(unsigned int) R_386_PC32
];
172 case BFD_RELOC_386_GOT32
:
173 TRACE ("BFD_RELOC_386_GOT32");
174 return &elf_howto_table
[(unsigned int) R_386_GOT32
];
176 case BFD_RELOC_386_PLT32
:
177 TRACE ("BFD_RELOC_386_PLT32");
178 return &elf_howto_table
[(unsigned int) R_386_PLT32
];
180 case BFD_RELOC_386_COPY
:
181 TRACE ("BFD_RELOC_386_COPY");
182 return &elf_howto_table
[(unsigned int) R_386_COPY
];
184 case BFD_RELOC_386_GLOB_DAT
:
185 TRACE ("BFD_RELOC_386_GLOB_DAT");
186 return &elf_howto_table
[(unsigned int) R_386_GLOB_DAT
];
188 case BFD_RELOC_386_JUMP_SLOT
:
189 TRACE ("BFD_RELOC_386_JUMP_SLOT");
190 return &elf_howto_table
[(unsigned int) R_386_JUMP_SLOT
];
192 case BFD_RELOC_386_RELATIVE
:
193 TRACE ("BFD_RELOC_386_RELATIVE");
194 return &elf_howto_table
[(unsigned int) R_386_RELATIVE
];
196 case BFD_RELOC_386_GOTOFF
:
197 TRACE ("BFD_RELOC_386_GOTOFF");
198 return &elf_howto_table
[(unsigned int) R_386_GOTOFF
];
200 case BFD_RELOC_386_GOTPC
:
201 TRACE ("BFD_RELOC_386_GOTPC");
202 return &elf_howto_table
[(unsigned int) R_386_GOTPC
];
204 /* The remaining relocs are a GNU extension. */
206 TRACE ("BFD_RELOC_16");
207 return &elf_howto_table
[(unsigned int) R_386_16
- R_386_ext_offset
];
209 case BFD_RELOC_16_PCREL
:
210 TRACE ("BFD_RELOC_16_PCREL");
211 return &elf_howto_table
[(unsigned int) R_386_PC16
- R_386_ext_offset
];
214 TRACE ("BFD_RELOC_8");
215 return &elf_howto_table
[(unsigned int) R_386_8
- R_386_ext_offset
];
217 case BFD_RELOC_8_PCREL
:
218 TRACE ("BFD_RELOC_8_PCREL");
219 return &elf_howto_table
[(unsigned int) R_386_PC8
- R_386_ext_offset
];
221 case BFD_RELOC_VTABLE_INHERIT
:
222 TRACE ("BFD_RELOC_VTABLE_INHERIT");
223 return &elf32_i386_vtinherit_howto
;
225 case BFD_RELOC_VTABLE_ENTRY
:
226 TRACE ("BFD_RELOC_VTABLE_ENTRY");
227 return &elf32_i386_vtentry_howto
;
238 elf_i386_info_to_howto (abfd
, cache_ptr
, dst
)
239 bfd
*abfd ATTRIBUTE_UNUSED
;
240 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
241 Elf32_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
247 elf_i386_info_to_howto_rel (abfd
, cache_ptr
, dst
)
248 bfd
*abfd ATTRIBUTE_UNUSED
;
250 Elf32_Internal_Rel
*dst
;
252 enum elf_i386_reloc_type type
;
254 type
= (enum elf_i386_reloc_type
) ELF32_R_TYPE (dst
->r_info
);
255 if (type
== R_386_GNU_VTINHERIT
)
256 cache_ptr
->howto
= &elf32_i386_vtinherit_howto
;
257 else if (type
== R_386_GNU_VTENTRY
)
258 cache_ptr
->howto
= &elf32_i386_vtentry_howto
;
263 if ((indx
= (unsigned int) type
) >= R_386_standard
264 && ((indx
= (unsigned int) type
- R_386_ext_offset
)
265 >= sizeof (elf_howto_table
) / sizeof (elf_howto_table
[0])))
267 (*_bfd_error_handler
) (_("%s: invalid relocation type %d"),
268 bfd_get_filename (abfd
), (int) type
);
269 indx
= (unsigned int) R_386_NONE
;
271 cache_ptr
->howto
= &elf_howto_table
[indx
];
275 /* Return whether a symbol name implies a local label. The UnixWare
276 2.1 cc generates temporary symbols that start with .X, so we
277 recognize them here. FIXME: do other SVR4 compilers also use .X?.
278 If so, we should move the .X recognition into
279 _bfd_elf_is_local_label_name. */
282 elf_i386_is_local_label_name (abfd
, name
)
286 if (name
[0] == '.' && name
[1] == 'X')
289 return _bfd_elf_is_local_label_name (abfd
, name
);
292 /* Functions for the i386 ELF linker. */
294 /* The name of the dynamic interpreter. This is put in the .interp
297 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
299 /* The size in bytes of an entry in the procedure linkage table. */
301 #define PLT_ENTRY_SIZE 16
303 /* The first entry in an absolute procedure linkage table looks like
304 this. See the SVR4 ABI i386 supplement to see how this works. */
306 static const bfd_byte elf_i386_plt0_entry
[PLT_ENTRY_SIZE
] =
308 0xff, 0x35, /* pushl contents of address */
309 0, 0, 0, 0, /* replaced with address of .got + 4. */
310 0xff, 0x25, /* jmp indirect */
311 0, 0, 0, 0, /* replaced with address of .got + 8. */
312 0, 0, 0, 0 /* pad out to 16 bytes. */
315 /* Subsequent entries in an absolute procedure linkage table look like
318 static const bfd_byte elf_i386_plt_entry
[PLT_ENTRY_SIZE
] =
320 0xff, 0x25, /* jmp indirect */
321 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
322 0x68, /* pushl immediate */
323 0, 0, 0, 0, /* replaced with offset into relocation table. */
324 0xe9, /* jmp relative */
325 0, 0, 0, 0 /* replaced with offset to start of .plt. */
328 /* The first entry in a PIC procedure linkage table look like this. */
330 static const bfd_byte elf_i386_pic_plt0_entry
[PLT_ENTRY_SIZE
] =
332 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
333 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
334 0, 0, 0, 0 /* pad out to 16 bytes. */
337 /* Subsequent entries in a PIC procedure linkage table look like this. */
339 static const bfd_byte elf_i386_pic_plt_entry
[PLT_ENTRY_SIZE
] =
341 0xff, 0xa3, /* jmp *offset(%ebx) */
342 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
343 0x68, /* pushl immediate */
344 0, 0, 0, 0, /* replaced with offset into relocation table. */
345 0xe9, /* jmp relative */
346 0, 0, 0, 0 /* replaced with offset to start of .plt. */
349 /* The i386 linker needs to keep track of the number of relocs that it
350 decides to copy in check_relocs for each symbol. This is so that
351 it can discard PC relative relocs if it doesn't need them when
352 linking with -Bsymbolic. We store the information in a field
353 extending the regular ELF linker hash table. */
355 /* This structure keeps track of the number of PC relative relocs we
356 have copied for a given symbol. */
358 struct elf_i386_pcrel_relocs_copied
361 struct elf_i386_pcrel_relocs_copied
*next
;
362 /* A section in dynobj. */
364 /* Number of relocs copied in this section. */
368 /* i386 ELF linker hash entry. */
370 struct elf_i386_link_hash_entry
372 struct elf_link_hash_entry root
;
374 /* Number of PC relative relocs copied for this symbol. */
375 struct elf_i386_pcrel_relocs_copied
*pcrel_relocs_copied
;
378 /* i386 ELF linker hash table. */
380 struct elf_i386_link_hash_table
382 struct elf_link_hash_table root
;
385 /* Declare this now that the above structures are defined. */
387 static boolean elf_i386_discard_copies
388 PARAMS ((struct elf_i386_link_hash_entry
*, PTR
));
390 /* Traverse an i386 ELF linker hash table. */
392 #define elf_i386_link_hash_traverse(table, func, info) \
393 (elf_link_hash_traverse \
395 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
398 /* Get the i386 ELF linker hash table from a link_info structure. */
400 #define elf_i386_hash_table(p) \
401 ((struct elf_i386_link_hash_table *) ((p)->hash))
403 /* Create an entry in an i386 ELF linker hash table. */
405 static struct bfd_hash_entry
*
406 elf_i386_link_hash_newfunc (entry
, table
, string
)
407 struct bfd_hash_entry
*entry
;
408 struct bfd_hash_table
*table
;
411 struct elf_i386_link_hash_entry
*ret
=
412 (struct elf_i386_link_hash_entry
*) entry
;
414 /* Allocate the structure if it has not already been allocated by a
416 if (ret
== (struct elf_i386_link_hash_entry
*) NULL
)
417 ret
= ((struct elf_i386_link_hash_entry
*)
418 bfd_hash_allocate (table
,
419 sizeof (struct elf_i386_link_hash_entry
)));
420 if (ret
== (struct elf_i386_link_hash_entry
*) NULL
)
421 return (struct bfd_hash_entry
*) ret
;
423 /* Call the allocation method of the superclass. */
424 ret
= ((struct elf_i386_link_hash_entry
*)
425 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
427 if (ret
!= (struct elf_i386_link_hash_entry
*) NULL
)
429 ret
->pcrel_relocs_copied
= NULL
;
432 return (struct bfd_hash_entry
*) ret
;
435 /* Create an i386 ELF linker hash table. */
437 static struct bfd_link_hash_table
*
438 elf_i386_link_hash_table_create (abfd
)
441 struct elf_i386_link_hash_table
*ret
;
443 ret
= ((struct elf_i386_link_hash_table
*)
444 bfd_alloc (abfd
, sizeof (struct elf_i386_link_hash_table
)));
445 if (ret
== (struct elf_i386_link_hash_table
*) NULL
)
448 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
449 elf_i386_link_hash_newfunc
))
451 bfd_release (abfd
, ret
);
455 return &ret
->root
.root
;
458 /* Look through the relocs for a section during the first phase, and
459 allocate space in the global offset table or procedure linkage
463 elf_i386_check_relocs (abfd
, info
, sec
, relocs
)
465 struct bfd_link_info
*info
;
467 const Elf_Internal_Rela
*relocs
;
470 Elf_Internal_Shdr
*symtab_hdr
;
471 struct elf_link_hash_entry
**sym_hashes
;
472 bfd_signed_vma
*local_got_refcounts
;
473 const Elf_Internal_Rela
*rel
;
474 const Elf_Internal_Rela
*rel_end
;
479 if (info
->relocateable
)
482 dynobj
= elf_hash_table (info
)->dynobj
;
483 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
484 sym_hashes
= elf_sym_hashes (abfd
);
485 local_got_refcounts
= elf_local_got_refcounts (abfd
);
491 rel_end
= relocs
+ sec
->reloc_count
;
492 for (rel
= relocs
; rel
< rel_end
; rel
++)
494 unsigned long r_symndx
;
495 struct elf_link_hash_entry
*h
;
497 r_symndx
= ELF32_R_SYM (rel
->r_info
);
499 if (r_symndx
< symtab_hdr
->sh_info
)
502 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
504 /* Some relocs require a global offset table. */
507 switch (ELF32_R_TYPE (rel
->r_info
))
512 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
513 if (! _bfd_elf_create_got_section (dynobj
, info
))
522 switch (ELF32_R_TYPE (rel
->r_info
))
525 /* This symbol requires a global offset table entry. */
529 sgot
= bfd_get_section_by_name (dynobj
, ".got");
530 BFD_ASSERT (sgot
!= NULL
);
534 && (h
!= NULL
|| info
->shared
))
536 srelgot
= bfd_get_section_by_name (dynobj
, ".rel.got");
539 srelgot
= bfd_make_section (dynobj
, ".rel.got");
541 || ! bfd_set_section_flags (dynobj
, srelgot
,
548 || ! bfd_set_section_alignment (dynobj
, srelgot
, 2))
555 if (h
->got
.refcount
== -1)
559 /* Make sure this symbol is output as a dynamic symbol. */
560 if (h
->dynindx
== -1)
562 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
566 sgot
->_raw_size
+= 4;
567 srelgot
->_raw_size
+= sizeof (Elf32_External_Rel
);
570 h
->got
.refcount
+= 1;
574 /* This is a global offset table entry for a local symbol. */
575 if (local_got_refcounts
== NULL
)
579 size
= symtab_hdr
->sh_info
* sizeof (bfd_signed_vma
);
580 local_got_refcounts
= ((bfd_signed_vma
*)
581 bfd_alloc (abfd
, size
));
582 if (local_got_refcounts
== NULL
)
584 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
585 memset (local_got_refcounts
, -1, size
);
587 if (local_got_refcounts
[r_symndx
] == -1)
589 local_got_refcounts
[r_symndx
] = 1;
591 sgot
->_raw_size
+= 4;
594 /* If we are generating a shared object, we need to
595 output a R_386_RELATIVE reloc so that the dynamic
596 linker can adjust this GOT entry. */
597 srelgot
->_raw_size
+= sizeof (Elf32_External_Rel
);
601 local_got_refcounts
[r_symndx
] += 1;
606 /* This symbol requires a procedure linkage table entry. We
607 actually build the entry in adjust_dynamic_symbol,
608 because this might be a case of linking PIC code which is
609 never referenced by a dynamic object, in which case we
610 don't need to generate a procedure linkage table entry
613 /* If this is a local symbol, we resolve it directly without
614 creating a procedure linkage table entry. */
618 if (h
->plt
.refcount
== -1)
621 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
624 h
->plt
.refcount
+= 1;
630 h
->elf_link_hash_flags
|= ELF_LINK_NON_GOT_REF
;
632 /* If we are creating a shared library, and this is a reloc
633 against a global symbol, or a non PC relative reloc
634 against a local symbol, then we need to copy the reloc
635 into the shared library. However, if we are linking with
636 -Bsymbolic, we do not need to copy a reloc against a
637 global symbol which is defined in an object we are
638 including in the link (i.e., DEF_REGULAR is set). At
639 this point we have not seen all the input files, so it is
640 possible that DEF_REGULAR is not set now but will be set
641 later (it is never cleared). We account for that
642 possibility below by storing information in the
643 pcrel_relocs_copied field of the hash table entry. */
645 && (sec
->flags
& SEC_ALLOC
) != 0
646 && (ELF32_R_TYPE (rel
->r_info
) != R_386_PC32
649 || (h
->elf_link_hash_flags
650 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
652 /* When creating a shared object, we must copy these
653 reloc types into the output file. We create a reloc
654 section in dynobj and make room for this reloc. */
659 name
= (bfd_elf_string_from_elf_section
661 elf_elfheader (abfd
)->e_shstrndx
,
662 elf_section_data (sec
)->rel_hdr
.sh_name
));
666 BFD_ASSERT (strncmp (name
, ".rel", 4) == 0
667 && strcmp (bfd_get_section_name (abfd
, sec
),
670 sreloc
= bfd_get_section_by_name (dynobj
, name
);
675 sreloc
= bfd_make_section (dynobj
, name
);
676 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
677 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
678 if ((sec
->flags
& SEC_ALLOC
) != 0)
679 flags
|= SEC_ALLOC
| SEC_LOAD
;
681 || ! bfd_set_section_flags (dynobj
, sreloc
, flags
)
682 || ! bfd_set_section_alignment (dynobj
, sreloc
, 2))
687 sreloc
->_raw_size
+= sizeof (Elf32_External_Rel
);
689 /* If we are linking with -Bsymbolic, and this is a
690 global symbol, we count the number of PC relative
691 relocations we have entered for this symbol, so that
692 we can discard them again if the symbol is later
693 defined by a regular object. Note that this function
694 is only called if we are using an elf_i386 linker
695 hash table, which means that h is really a pointer to
696 an elf_i386_link_hash_entry. */
697 if (h
!= NULL
&& info
->symbolic
698 && ELF32_R_TYPE (rel
->r_info
) == R_386_PC32
)
700 struct elf_i386_link_hash_entry
*eh
;
701 struct elf_i386_pcrel_relocs_copied
*p
;
703 eh
= (struct elf_i386_link_hash_entry
*) h
;
705 for (p
= eh
->pcrel_relocs_copied
; p
!= NULL
; p
= p
->next
)
706 if (p
->section
== sreloc
)
711 p
= ((struct elf_i386_pcrel_relocs_copied
*)
712 bfd_alloc (dynobj
, sizeof *p
));
715 p
->next
= eh
->pcrel_relocs_copied
;
716 eh
->pcrel_relocs_copied
= p
;
727 /* This relocation describes the C++ object vtable hierarchy.
728 Reconstruct it for later use during GC. */
729 case R_386_GNU_VTINHERIT
:
730 if (!_bfd_elf32_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
734 /* This relocation describes which C++ vtable entries are actually
735 used. Record for later use during GC. */
736 case R_386_GNU_VTENTRY
:
737 if (!_bfd_elf32_gc_record_vtentry (abfd
, sec
, h
, rel
->r_offset
))
749 /* Return the section that should be marked against GC for a given
753 elf_i386_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
755 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
756 Elf_Internal_Rela
*rel
;
757 struct elf_link_hash_entry
*h
;
758 Elf_Internal_Sym
*sym
;
762 switch (ELF32_R_TYPE (rel
->r_info
))
764 case R_386_GNU_VTINHERIT
:
765 case R_386_GNU_VTENTRY
:
769 switch (h
->root
.type
)
771 case bfd_link_hash_defined
:
772 case bfd_link_hash_defweak
:
773 return h
->root
.u
.def
.section
;
775 case bfd_link_hash_common
:
776 return h
->root
.u
.c
.p
->section
;
785 if (!(elf_bad_symtab (abfd
)
786 && ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
787 && ! ((sym
->st_shndx
<= 0 || sym
->st_shndx
>= SHN_LORESERVE
)
788 && sym
->st_shndx
!= SHN_COMMON
))
790 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
797 /* Update the got entry reference counts for the section being removed. */
800 elf_i386_gc_sweep_hook (abfd
, info
, sec
, relocs
)
802 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
804 const Elf_Internal_Rela
*relocs
;
806 Elf_Internal_Shdr
*symtab_hdr
;
807 struct elf_link_hash_entry
**sym_hashes
;
808 bfd_signed_vma
*local_got_refcounts
;
809 const Elf_Internal_Rela
*rel
, *relend
;
810 unsigned long r_symndx
;
811 struct elf_link_hash_entry
*h
;
816 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
817 sym_hashes
= elf_sym_hashes (abfd
);
818 local_got_refcounts
= elf_local_got_refcounts (abfd
);
820 dynobj
= elf_hash_table (info
)->dynobj
;
824 sgot
= bfd_get_section_by_name (dynobj
, ".got");
825 srelgot
= bfd_get_section_by_name (dynobj
, ".rel.got");
827 relend
= relocs
+ sec
->reloc_count
;
828 for (rel
= relocs
; rel
< relend
; rel
++)
829 switch (ELF32_R_TYPE (rel
->r_info
))
834 r_symndx
= ELF32_R_SYM (rel
->r_info
);
835 if (r_symndx
>= symtab_hdr
->sh_info
)
837 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
838 if (h
->got
.refcount
> 0)
840 h
->got
.refcount
-= 1;
841 if (h
->got
.refcount
== 0)
843 sgot
->_raw_size
-= 4;
844 srelgot
->_raw_size
-= sizeof (Elf32_External_Rel
);
848 else if (local_got_refcounts
!= NULL
)
850 if (local_got_refcounts
[r_symndx
] > 0)
852 local_got_refcounts
[r_symndx
] -= 1;
853 if (local_got_refcounts
[r_symndx
] == 0)
855 sgot
->_raw_size
-= 4;
857 srelgot
->_raw_size
-= sizeof (Elf32_External_Rel
);
864 r_symndx
= ELF32_R_SYM (rel
->r_info
);
865 if (r_symndx
>= symtab_hdr
->sh_info
)
867 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
868 if (h
->plt
.refcount
> 0)
869 h
->plt
.refcount
-= 1;
880 /* Adjust a symbol defined by a dynamic object and referenced by a
881 regular object. The current definition is in some section of the
882 dynamic object, but we're not including those sections. We have to
883 change the definition to something the rest of the link can
887 elf_i386_adjust_dynamic_symbol (info
, h
)
888 struct bfd_link_info
*info
;
889 struct elf_link_hash_entry
*h
;
893 unsigned int power_of_two
;
895 dynobj
= elf_hash_table (info
)->dynobj
;
897 /* Make sure we know what is going on here. */
898 BFD_ASSERT (dynobj
!= NULL
899 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
900 || h
->weakdef
!= NULL
901 || ((h
->elf_link_hash_flags
902 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
903 && (h
->elf_link_hash_flags
904 & ELF_LINK_HASH_REF_REGULAR
) != 0
905 && (h
->elf_link_hash_flags
906 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
908 /* If this is a function, put it in the procedure linkage table. We
909 will fill in the contents of the procedure linkage table later,
910 when we know the address of the .got section. */
911 if (h
->type
== STT_FUNC
912 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
915 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
916 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
917 || (info
->shared
&& h
->plt
.refcount
<= 0))
919 /* This case can occur if we saw a PLT32 reloc in an input
920 file, but the symbol was never referred to by a dynamic
921 object, or if all references were garbage collected. In
922 such a case, we don't actually need to build a procedure
923 linkage table, and we can just do a PC32 reloc instead. */
924 h
->plt
.offset
= (bfd_vma
) -1;
925 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
929 /* Make sure this symbol is output as a dynamic symbol. */
930 if (h
->dynindx
== -1)
932 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
936 s
= bfd_get_section_by_name (dynobj
, ".plt");
937 BFD_ASSERT (s
!= NULL
);
939 /* If this is the first .plt entry, make room for the special
941 if (s
->_raw_size
== 0)
942 s
->_raw_size
+= PLT_ENTRY_SIZE
;
944 /* If this symbol is not defined in a regular file, and we are
945 not generating a shared library, then set the symbol to this
946 location in the .plt. This is required to make function
947 pointers compare as equal between the normal executable and
948 the shared library. */
950 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
952 h
->root
.u
.def
.section
= s
;
953 h
->root
.u
.def
.value
= s
->_raw_size
;
956 h
->plt
.offset
= s
->_raw_size
;
958 /* Make room for this entry. */
959 s
->_raw_size
+= PLT_ENTRY_SIZE
;
961 /* We also need to make an entry in the .got.plt section, which
962 will be placed in the .got section by the linker script. */
963 s
= bfd_get_section_by_name (dynobj
, ".got.plt");
964 BFD_ASSERT (s
!= NULL
);
967 /* We also need to make an entry in the .rel.plt section. */
968 s
= bfd_get_section_by_name (dynobj
, ".rel.plt");
969 BFD_ASSERT (s
!= NULL
);
970 s
->_raw_size
+= sizeof (Elf32_External_Rel
);
975 /* If this is a weak symbol, and there is a real definition, the
976 processor independent code will have arranged for us to see the
977 real definition first, and we can just use the same value. */
978 if (h
->weakdef
!= NULL
)
980 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
981 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
982 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
983 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
987 /* This is a reference to a symbol defined by a dynamic object which
988 is not a function. */
990 /* If we are creating a shared library, we must presume that the
991 only references to the symbol are via the global offset table.
992 For such cases we need not do anything here; the relocations will
993 be handled correctly by relocate_section. */
997 /* If there are no references to this symbol that do not use the
998 GOT, we don't need to generate a copy reloc. */
999 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0)
1002 /* We must allocate the symbol in our .dynbss section, which will
1003 become part of the .bss section of the executable. There will be
1004 an entry for this symbol in the .dynsym section. The dynamic
1005 object will contain position independent code, so all references
1006 from the dynamic object to this symbol will go through the global
1007 offset table. The dynamic linker will use the .dynsym entry to
1008 determine the address it must put in the global offset table, so
1009 both the dynamic object and the regular object will refer to the
1010 same memory location for the variable. */
1012 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
1013 BFD_ASSERT (s
!= NULL
);
1015 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1016 copy the initial value out of the dynamic object and into the
1017 runtime process image. We need to remember the offset into the
1018 .rel.bss section we are going to use. */
1019 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
1023 srel
= bfd_get_section_by_name (dynobj
, ".rel.bss");
1024 BFD_ASSERT (srel
!= NULL
);
1025 srel
->_raw_size
+= sizeof (Elf32_External_Rel
);
1026 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
1029 /* We need to figure out the alignment required for this symbol. I
1030 have no idea how ELF linkers handle this. */
1031 power_of_two
= bfd_log2 (h
->size
);
1032 if (power_of_two
> 3)
1035 /* Apply the required alignment. */
1036 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
,
1037 (bfd_size_type
) (1 << power_of_two
));
1038 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
1040 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
1044 /* Define the symbol as being at this point in the section. */
1045 h
->root
.u
.def
.section
= s
;
1046 h
->root
.u
.def
.value
= s
->_raw_size
;
1048 /* Increment the section size to make room for the symbol. */
1049 s
->_raw_size
+= h
->size
;
1054 /* Set the sizes of the dynamic sections. */
1057 elf_i386_size_dynamic_sections (output_bfd
, info
)
1059 struct bfd_link_info
*info
;
1067 dynobj
= elf_hash_table (info
)->dynobj
;
1068 BFD_ASSERT (dynobj
!= NULL
);
1070 if (elf_hash_table (info
)->dynamic_sections_created
)
1072 /* Set the contents of the .interp section to the interpreter. */
1075 s
= bfd_get_section_by_name (dynobj
, ".interp");
1076 BFD_ASSERT (s
!= NULL
);
1077 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1078 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1083 /* We may have created entries in the .rel.got section.
1084 However, if we are not creating the dynamic sections, we will
1085 not actually use these entries. Reset the size of .rel.got,
1086 which will cause it to get stripped from the output file
1088 s
= bfd_get_section_by_name (dynobj
, ".rel.got");
1093 /* If this is a -Bsymbolic shared link, then we need to discard all
1094 PC relative relocs against symbols defined in a regular object.
1095 We allocated space for them in the check_relocs routine, but we
1096 will not fill them in in the relocate_section routine. */
1097 if (info
->shared
&& info
->symbolic
)
1098 elf_i386_link_hash_traverse (elf_i386_hash_table (info
),
1099 elf_i386_discard_copies
,
1102 /* The check_relocs and adjust_dynamic_symbol entry points have
1103 determined the sizes of the various dynamic sections. Allocate
1108 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1113 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1116 /* It's OK to base decisions on the section name, because none
1117 of the dynobj section names depend upon the input files. */
1118 name
= bfd_get_section_name (dynobj
, s
);
1122 if (strcmp (name
, ".plt") == 0)
1124 if (s
->_raw_size
== 0)
1126 /* Strip this section if we don't need it; see the
1132 /* Remember whether there is a PLT. */
1136 else if (strncmp (name
, ".rel", 4) == 0)
1138 if (s
->_raw_size
== 0)
1140 /* If we don't need this section, strip it from the
1141 output file. This is mostly to handle .rel.bss and
1142 .rel.plt. We must create both sections in
1143 create_dynamic_sections, because they must be created
1144 before the linker maps input sections to output
1145 sections. The linker does that before
1146 adjust_dynamic_symbol is called, and it is that
1147 function which decides whether anything needs to go
1148 into these sections. */
1155 /* Remember whether there are any reloc sections other
1157 if (strcmp (name
, ".rel.plt") != 0)
1159 const char *outname
;
1163 /* If this relocation section applies to a read only
1164 section, then we probably need a DT_TEXTREL
1165 entry. The entries in the .rel.plt section
1166 really apply to the .got section, which we
1167 created ourselves and so know is not readonly. */
1168 outname
= bfd_get_section_name (output_bfd
,
1170 target
= bfd_get_section_by_name (output_bfd
, outname
+ 4);
1172 && (target
->flags
& SEC_READONLY
) != 0
1173 && (target
->flags
& SEC_ALLOC
) != 0)
1177 /* We use the reloc_count field as a counter if we need
1178 to copy relocs into the output file. */
1182 else if (strncmp (name
, ".got", 4) != 0)
1184 /* It's not one of our sections, so don't allocate space. */
1190 _bfd_strip_section_from_output (info
, s
);
1194 /* Allocate memory for the section contents. */
1195 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
1196 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
1200 if (elf_hash_table (info
)->dynamic_sections_created
)
1202 /* Add some entries to the .dynamic section. We fill in the
1203 values later, in elf_i386_finish_dynamic_sections, but we
1204 must add the entries now so that we get the correct size for
1205 the .dynamic section. The DT_DEBUG entry is filled in by the
1206 dynamic linker and used by the debugger. */
1209 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
1215 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0)
1216 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
1217 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTREL
, DT_REL
)
1218 || ! bfd_elf32_add_dynamic_entry (info
, DT_JMPREL
, 0))
1224 if (! bfd_elf32_add_dynamic_entry (info
, DT_REL
, 0)
1225 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELSZ
, 0)
1226 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELENT
,
1227 sizeof (Elf32_External_Rel
)))
1233 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
1241 /* This function is called via elf_i386_link_hash_traverse if we are
1242 creating a shared object with -Bsymbolic. It discards the space
1243 allocated to copy PC relative relocs against symbols which are
1244 defined in regular objects. We allocated space for them in the
1245 check_relocs routine, but we won't fill them in in the
1246 relocate_section routine. */
1250 elf_i386_discard_copies (h
, ignore
)
1251 struct elf_i386_link_hash_entry
*h
;
1252 PTR ignore ATTRIBUTE_UNUSED
;
1254 struct elf_i386_pcrel_relocs_copied
*s
;
1256 /* We only discard relocs for symbols defined in a regular object. */
1257 if ((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1260 for (s
= h
->pcrel_relocs_copied
; s
!= NULL
; s
= s
->next
)
1261 s
->section
->_raw_size
-= s
->count
* sizeof (Elf32_External_Rel
);
1266 /* Relocate an i386 ELF section. */
1269 elf_i386_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1270 contents
, relocs
, local_syms
, local_sections
)
1272 struct bfd_link_info
*info
;
1274 asection
*input_section
;
1276 Elf_Internal_Rela
*relocs
;
1277 Elf_Internal_Sym
*local_syms
;
1278 asection
**local_sections
;
1281 Elf_Internal_Shdr
*symtab_hdr
;
1282 struct elf_link_hash_entry
**sym_hashes
;
1283 bfd_vma
*local_got_offsets
;
1287 Elf_Internal_Rela
*rel
;
1288 Elf_Internal_Rela
*relend
;
1290 dynobj
= elf_hash_table (info
)->dynobj
;
1291 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1292 sym_hashes
= elf_sym_hashes (input_bfd
);
1293 local_got_offsets
= elf_local_got_offsets (input_bfd
);
1300 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1301 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1305 relend
= relocs
+ input_section
->reloc_count
;
1306 for (; rel
< relend
; rel
++)
1309 reloc_howto_type
*howto
;
1310 unsigned long r_symndx
;
1311 struct elf_link_hash_entry
*h
;
1312 Elf_Internal_Sym
*sym
;
1315 bfd_reloc_status_type r
;
1318 r_type
= ELF32_R_TYPE (rel
->r_info
);
1319 if (r_type
== R_386_GNU_VTINHERIT
1320 || r_type
== R_386_GNU_VTENTRY
)
1322 if ((indx
= (unsigned) r_type
) >= R_386_standard
1323 && ((indx
= (unsigned) r_type
- R_386_ext_offset
)
1324 >= sizeof (elf_howto_table
) / sizeof (elf_howto_table
[0])))
1326 bfd_set_error (bfd_error_bad_value
);
1329 howto
= elf_howto_table
+ indx
;
1331 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1333 if (info
->relocateable
)
1335 /* This is a relocateable link. We don't have to change
1336 anything, unless the reloc is against a section symbol,
1337 in which case we have to adjust according to where the
1338 section symbol winds up in the output section. */
1339 if (r_symndx
< symtab_hdr
->sh_info
)
1341 sym
= local_syms
+ r_symndx
;
1342 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1346 sec
= local_sections
[r_symndx
];
1347 val
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
1348 val
+= sec
->output_offset
+ sym
->st_value
;
1349 bfd_put_32 (input_bfd
, val
, contents
+ rel
->r_offset
);
1356 /* This is a final link. */
1360 if (r_symndx
< symtab_hdr
->sh_info
)
1362 sym
= local_syms
+ r_symndx
;
1363 sec
= local_sections
[r_symndx
];
1364 relocation
= (sec
->output_section
->vma
1365 + sec
->output_offset
1370 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1371 while (h
->root
.type
== bfd_link_hash_indirect
1372 || h
->root
.type
== bfd_link_hash_warning
)
1373 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1374 if (h
->root
.type
== bfd_link_hash_defined
1375 || h
->root
.type
== bfd_link_hash_defweak
)
1377 sec
= h
->root
.u
.def
.section
;
1378 if (r_type
== R_386_GOTPC
1379 || (r_type
== R_386_PLT32
1381 && h
->plt
.offset
!= (bfd_vma
) -1)
1382 || (r_type
== R_386_GOT32
1383 && elf_hash_table (info
)->dynamic_sections_created
1385 || (! info
->symbolic
&& h
->dynindx
!= -1)
1386 || (h
->elf_link_hash_flags
1387 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
1389 && ((! info
->symbolic
&& h
->dynindx
!= -1)
1390 || (h
->elf_link_hash_flags
1391 & ELF_LINK_HASH_DEF_REGULAR
) == 0)
1392 && (r_type
== R_386_32
1393 || r_type
== R_386_PC32
)
1394 && ((input_section
->flags
& SEC_ALLOC
) != 0
1395 /* DWARF will emit R_386_32 relocations in its
1396 sections against symbols defined externally
1397 in shared libraries. We can't do anything
1399 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
1400 && (h
->elf_link_hash_flags
1401 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0))))
1403 /* In these cases, we don't need the relocation
1404 value. We check specially because in some
1405 obscure cases sec->output_section will be NULL. */
1408 else if (sec
->output_section
== NULL
)
1410 (*_bfd_error_handler
)
1411 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1412 bfd_get_filename (input_bfd
), h
->root
.root
.string
,
1413 bfd_get_section_name (input_bfd
, input_section
));
1417 relocation
= (h
->root
.u
.def
.value
1418 + sec
->output_section
->vma
1419 + sec
->output_offset
);
1421 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1423 else if (info
->shared
&& !info
->symbolic
1424 && !info
->no_undefined
1425 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
1429 if (! ((*info
->callbacks
->undefined_symbol
)
1430 (info
, h
->root
.root
.string
, input_bfd
,
1431 input_section
, rel
->r_offset
,
1432 (!info
->shared
|| info
->no_undefined
1433 || ELF_ST_VISIBILITY (h
->other
)))))
1442 /* Relocation is to the entry for this symbol in the global
1444 BFD_ASSERT (sgot
!= NULL
);
1450 off
= h
->got
.offset
;
1451 BFD_ASSERT (off
!= (bfd_vma
) -1);
1453 if (! elf_hash_table (info
)->dynamic_sections_created
1455 && (info
->symbolic
|| h
->dynindx
== -1)
1456 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
1458 /* This is actually a static link, or it is a
1459 -Bsymbolic link and the symbol is defined
1460 locally, or the symbol was forced to be local
1461 because of a version file. We must initialize
1462 this entry in the global offset table. Since the
1463 offset must always be a multiple of 4, we use the
1464 least significant bit to record whether we have
1465 initialized it already.
1467 When doing a dynamic link, we create a .rel.got
1468 relocation entry to initialize the value. This
1469 is done in the finish_dynamic_symbol routine. */
1474 bfd_put_32 (output_bfd
, relocation
,
1475 sgot
->contents
+ off
);
1480 relocation
= sgot
->output_offset
+ off
;
1486 BFD_ASSERT (local_got_offsets
!= NULL
1487 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1489 off
= local_got_offsets
[r_symndx
];
1491 /* The offset must always be a multiple of 4. We use
1492 the least significant bit to record whether we have
1493 already generated the necessary reloc. */
1498 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
1503 Elf_Internal_Rel outrel
;
1505 srelgot
= bfd_get_section_by_name (dynobj
, ".rel.got");
1506 BFD_ASSERT (srelgot
!= NULL
);
1508 outrel
.r_offset
= (sgot
->output_section
->vma
1509 + sgot
->output_offset
1511 outrel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1512 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
1513 (((Elf32_External_Rel
*)
1515 + srelgot
->reloc_count
));
1516 ++srelgot
->reloc_count
;
1519 local_got_offsets
[r_symndx
] |= 1;
1522 relocation
= sgot
->output_offset
+ off
;
1528 /* Relocation is relative to the start of the global offset
1533 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1534 BFD_ASSERT (sgot
!= NULL
);
1537 /* Note that sgot->output_offset is not involved in this
1538 calculation. We always want the start of .got. If we
1539 defined _GLOBAL_OFFSET_TABLE in a different way, as is
1540 permitted by the ABI, we might have to change this
1542 relocation
-= sgot
->output_section
->vma
;
1547 /* Use global offset table as symbol value. */
1551 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1552 BFD_ASSERT (sgot
!= NULL
);
1555 relocation
= sgot
->output_section
->vma
;
1560 /* Relocation is to the entry for this symbol in the
1561 procedure linkage table. */
1563 /* Resolve a PLT32 reloc against a local symbol directly,
1564 without using the procedure linkage table. */
1568 if (h
->plt
.offset
== (bfd_vma
) -1
1571 /* We didn't make a PLT entry for this symbol. This
1572 happens when statically linking PIC code, or when
1573 using -Bsymbolic. */
1577 relocation
= (splt
->output_section
->vma
1578 + splt
->output_offset
1586 && (input_section
->flags
& SEC_ALLOC
) != 0
1587 && (r_type
!= R_386_PC32
1590 && (! info
->symbolic
1591 || (h
->elf_link_hash_flags
1592 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
1594 Elf_Internal_Rel outrel
;
1595 boolean skip
, relocate
;
1597 /* When generating a shared object, these relocations
1598 are copied into the output file to be resolved at run
1605 name
= (bfd_elf_string_from_elf_section
1607 elf_elfheader (input_bfd
)->e_shstrndx
,
1608 elf_section_data (input_section
)->rel_hdr
.sh_name
));
1612 BFD_ASSERT (strncmp (name
, ".rel", 4) == 0
1613 && strcmp (bfd_get_section_name (input_bfd
,
1617 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1618 BFD_ASSERT (sreloc
!= NULL
);
1623 if (elf_section_data (input_section
)->stab_info
== NULL
)
1624 outrel
.r_offset
= rel
->r_offset
;
1629 off
= (_bfd_stab_section_offset
1630 (output_bfd
, &elf_hash_table (info
)->stab_info
,
1632 &elf_section_data (input_section
)->stab_info
,
1634 if (off
== (bfd_vma
) -1)
1636 outrel
.r_offset
= off
;
1639 outrel
.r_offset
+= (input_section
->output_section
->vma
1640 + input_section
->output_offset
);
1644 memset (&outrel
, 0, sizeof outrel
);
1647 else if (r_type
== R_386_PC32
)
1649 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
1651 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_PC32
);
1655 /* h->dynindx may be -1 if this symbol was marked to
1658 || ((info
->symbolic
|| h
->dynindx
== -1)
1659 && (h
->elf_link_hash_flags
1660 & ELF_LINK_HASH_DEF_REGULAR
) != 0))
1663 outrel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1667 BFD_ASSERT (h
->dynindx
!= -1);
1669 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_32
);
1673 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
1674 (((Elf32_External_Rel
*)
1676 + sreloc
->reloc_count
));
1677 ++sreloc
->reloc_count
;
1679 /* If this reloc is against an external symbol, we do
1680 not want to fiddle with the addend. Otherwise, we
1681 need to include the symbol value so that it becomes
1682 an addend for the dynamic reloc. */
1693 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1694 contents
, rel
->r_offset
,
1695 relocation
, (bfd_vma
) 0);
1697 if (r
!= bfd_reloc_ok
)
1702 case bfd_reloc_outofrange
:
1704 case bfd_reloc_overflow
:
1709 name
= h
->root
.root
.string
;
1712 name
= bfd_elf_string_from_elf_section (input_bfd
,
1713 symtab_hdr
->sh_link
,
1718 name
= bfd_section_name (input_bfd
, sec
);
1720 if (! ((*info
->callbacks
->reloc_overflow
)
1721 (info
, name
, howto
->name
, (bfd_vma
) 0,
1722 input_bfd
, input_section
, rel
->r_offset
)))
1733 /* Finish up dynamic symbol handling. We set the contents of various
1734 dynamic sections here. */
1737 elf_i386_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
1739 struct bfd_link_info
*info
;
1740 struct elf_link_hash_entry
*h
;
1741 Elf_Internal_Sym
*sym
;
1745 dynobj
= elf_hash_table (info
)->dynobj
;
1747 if (h
->plt
.offset
!= (bfd_vma
) -1)
1754 Elf_Internal_Rel rel
;
1756 /* This symbol has an entry in the procedure linkage table. Set
1759 BFD_ASSERT (h
->dynindx
!= -1);
1761 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1762 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
1763 srel
= bfd_get_section_by_name (dynobj
, ".rel.plt");
1764 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
1766 /* Get the index in the procedure linkage table which
1767 corresponds to this symbol. This is the index of this symbol
1768 in all the symbols for which we are making plt entries. The
1769 first entry in the procedure linkage table is reserved. */
1770 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
1772 /* Get the offset into the .got table of the entry that
1773 corresponds to this function. Each .got entry is 4 bytes.
1774 The first three are reserved. */
1775 got_offset
= (plt_index
+ 3) * 4;
1777 /* Fill in the entry in the procedure linkage table. */
1780 memcpy (splt
->contents
+ h
->plt
.offset
, elf_i386_plt_entry
,
1782 bfd_put_32 (output_bfd
,
1783 (sgot
->output_section
->vma
1784 + sgot
->output_offset
1786 splt
->contents
+ h
->plt
.offset
+ 2);
1790 memcpy (splt
->contents
+ h
->plt
.offset
, elf_i386_pic_plt_entry
,
1792 bfd_put_32 (output_bfd
, got_offset
,
1793 splt
->contents
+ h
->plt
.offset
+ 2);
1796 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rel
),
1797 splt
->contents
+ h
->plt
.offset
+ 7);
1798 bfd_put_32 (output_bfd
, - (h
->plt
.offset
+ PLT_ENTRY_SIZE
),
1799 splt
->contents
+ h
->plt
.offset
+ 12);
1801 /* Fill in the entry in the global offset table. */
1802 bfd_put_32 (output_bfd
,
1803 (splt
->output_section
->vma
1804 + splt
->output_offset
1807 sgot
->contents
+ got_offset
);
1809 /* Fill in the entry in the .rel.plt section. */
1810 rel
.r_offset
= (sgot
->output_section
->vma
1811 + sgot
->output_offset
1813 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_JUMP_SLOT
);
1814 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1815 ((Elf32_External_Rel
*) srel
->contents
1818 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1820 /* Mark the symbol as undefined, rather than as defined in
1821 the .plt section. Leave the value alone. */
1822 sym
->st_shndx
= SHN_UNDEF
;
1826 if (h
->got
.offset
!= (bfd_vma
) -1)
1830 Elf_Internal_Rel rel
;
1832 /* This symbol has an entry in the global offset table. Set it
1835 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1836 srel
= bfd_get_section_by_name (dynobj
, ".rel.got");
1837 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
1839 rel
.r_offset
= (sgot
->output_section
->vma
1840 + sgot
->output_offset
1841 + (h
->got
.offset
&~ 1));
1843 /* If this is a static link, or it is a -Bsymbolic link and the
1844 symbol is defined locally or was forced to be local because
1845 of a version file, we just want to emit a RELATIVE reloc.
1846 The entry in the global offset table will already have been
1847 initialized in the relocate_section function. */
1848 if (! elf_hash_table (info
)->dynamic_sections_created
1850 && (info
->symbolic
|| h
->dynindx
== -1)
1851 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
1853 rel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1857 BFD_ASSERT((h
->got
.offset
& 1) == 0);
1858 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
1859 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_GLOB_DAT
);
1862 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1863 ((Elf32_External_Rel
*) srel
->contents
1864 + srel
->reloc_count
));
1865 ++srel
->reloc_count
;
1868 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
1871 Elf_Internal_Rel rel
;
1873 /* This symbol needs a copy reloc. Set it up. */
1875 BFD_ASSERT (h
->dynindx
!= -1
1876 && (h
->root
.type
== bfd_link_hash_defined
1877 || h
->root
.type
== bfd_link_hash_defweak
));
1879 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
1881 BFD_ASSERT (s
!= NULL
);
1883 rel
.r_offset
= (h
->root
.u
.def
.value
1884 + h
->root
.u
.def
.section
->output_section
->vma
1885 + h
->root
.u
.def
.section
->output_offset
);
1886 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_COPY
);
1887 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1888 ((Elf32_External_Rel
*) s
->contents
1893 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1894 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
1895 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
1896 sym
->st_shndx
= SHN_ABS
;
1901 /* Finish up the dynamic sections. */
1904 elf_i386_finish_dynamic_sections (output_bfd
, info
)
1906 struct bfd_link_info
*info
;
1912 dynobj
= elf_hash_table (info
)->dynobj
;
1914 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
1915 BFD_ASSERT (sgot
!= NULL
);
1916 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
1918 if (elf_hash_table (info
)->dynamic_sections_created
)
1921 Elf32_External_Dyn
*dyncon
, *dynconend
;
1923 BFD_ASSERT (sdyn
!= NULL
);
1925 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
1926 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
1927 for (; dyncon
< dynconend
; dyncon
++)
1929 Elf_Internal_Dyn dyn
;
1933 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
1946 s
= bfd_get_section_by_name (output_bfd
, name
);
1947 BFD_ASSERT (s
!= NULL
);
1948 dyn
.d_un
.d_ptr
= s
->vma
;
1949 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1953 s
= bfd_get_section_by_name (output_bfd
, ".rel.plt");
1954 BFD_ASSERT (s
!= NULL
);
1955 if (s
->_cooked_size
!= 0)
1956 dyn
.d_un
.d_val
= s
->_cooked_size
;
1958 dyn
.d_un
.d_val
= s
->_raw_size
;
1959 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1963 /* My reading of the SVR4 ABI indicates that the
1964 procedure linkage table relocs (DT_JMPREL) should be
1965 included in the overall relocs (DT_REL). This is
1966 what Solaris does. However, UnixWare can not handle
1967 that case. Therefore, we override the DT_RELSZ entry
1968 here to make it not include the JMPREL relocs. Since
1969 the linker script arranges for .rel.plt to follow all
1970 other relocation sections, we don't have to worry
1971 about changing the DT_REL entry. */
1972 s
= bfd_get_section_by_name (output_bfd
, ".rel.plt");
1975 if (s
->_cooked_size
!= 0)
1976 dyn
.d_un
.d_val
-= s
->_cooked_size
;
1978 dyn
.d_un
.d_val
-= s
->_raw_size
;
1980 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1985 /* Fill in the first entry in the procedure linkage table. */
1986 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1987 if (splt
&& splt
->_raw_size
> 0)
1990 memcpy (splt
->contents
, elf_i386_pic_plt0_entry
, PLT_ENTRY_SIZE
);
1993 memcpy (splt
->contents
, elf_i386_plt0_entry
, PLT_ENTRY_SIZE
);
1994 bfd_put_32 (output_bfd
,
1995 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
1996 splt
->contents
+ 2);
1997 bfd_put_32 (output_bfd
,
1998 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
1999 splt
->contents
+ 8);
2002 /* UnixWare sets the entsize of .plt to 4, although that doesn't
2003 really seem like the right value. */
2004 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
2008 /* Fill in the first three entries in the global offset table. */
2009 if (sgot
->_raw_size
> 0)
2012 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
2014 bfd_put_32 (output_bfd
,
2015 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
2017 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
2018 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
2021 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
2026 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
2027 #define TARGET_LITTLE_NAME "elf32-i386"
2028 #define ELF_ARCH bfd_arch_i386
2029 #define ELF_MACHINE_CODE EM_386
2030 #define ELF_MAXPAGESIZE 0x1000
2032 #define elf_backend_can_gc_sections 1
2033 #define elf_backend_want_got_plt 1
2034 #define elf_backend_plt_readonly 1
2035 #define elf_backend_want_plt_sym 0
2036 #define elf_backend_got_header_size 12
2037 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
2039 #define elf_info_to_howto elf_i386_info_to_howto
2040 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
2042 #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
2043 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
2044 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
2045 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
2047 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
2048 #define elf_backend_check_relocs elf_i386_check_relocs
2049 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
2050 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
2051 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
2052 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
2053 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
2054 #define elf_backend_relocate_section elf_i386_relocate_section
2055 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
2057 #include "elf32-target.h"