1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995 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_check_relocs
33 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
34 const Elf_Internal_Rela
*));
35 static boolean elf_i386_adjust_dynamic_symbol
36 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
37 static boolean elf_i386_size_dynamic_sections
38 PARAMS ((bfd
*, struct bfd_link_info
*));
39 static boolean elf_i386_relocate_section
40 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
41 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
42 static boolean elf_i386_finish_dynamic_symbol
43 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
45 static boolean elf_i386_finish_dynamic_sections
46 PARAMS ((bfd
*, struct bfd_link_info
*));
48 #define USE_REL 1 /* 386 uses REL relocations instead of RELA */
67 static CONST
char *CONST reloc_type_names
[] =
83 static reloc_howto_type elf_howto_table
[]=
85 HOWTO(R_386_NONE
, 0,0, 0,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_NONE", true,0x00000000,0x00000000,false),
86 HOWTO(R_386_32
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_32", true,0xffffffff,0xffffffff,false),
87 HOWTO(R_386_PC32
, 0,2,32,true, 0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_PC32", true,0xffffffff,0xffffffff,true),
88 HOWTO(R_386_GOT32
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_GOT32", true,0xffffffff,0xffffffff,false),
89 HOWTO(R_386_PLT32
, 0,2,32,true,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_PLT32", true,0xffffffff,0xffffffff,true),
90 HOWTO(R_386_COPY
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_COPY", true,0xffffffff,0xffffffff,false),
91 HOWTO(R_386_GLOB_DAT
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_GLOB_DAT", true,0xffffffff,0xffffffff,false),
92 HOWTO(R_386_JUMP_SLOT
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_JUMP_SLOT",true,0xffffffff,0xffffffff,false),
93 HOWTO(R_386_RELATIVE
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false),
94 HOWTO(R_386_GOTOFF
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_GOTOFF", true,0xffffffff,0xffffffff,false),
95 HOWTO(R_386_GOTPC
, 0,2,32,true,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_GOTPC", true,0xffffffff,0xffffffff,true),
98 #ifdef DEBUG_GEN_RELOC
99 #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
104 static reloc_howto_type
*
105 elf_i386_reloc_type_lookup (abfd
, code
)
107 bfd_reloc_code_real_type code
;
112 TRACE ("BFD_RELOC_NONE");
113 return &elf_howto_table
[ (int)R_386_NONE
];
116 TRACE ("BFD_RELOC_32");
117 return &elf_howto_table
[ (int)R_386_32
];
119 case BFD_RELOC_32_PCREL
:
120 TRACE ("BFD_RELOC_PC32");
121 return &elf_howto_table
[ (int)R_386_PC32
];
123 case BFD_RELOC_386_GOT32
:
124 TRACE ("BFD_RELOC_386_GOT32");
125 return &elf_howto_table
[ (int)R_386_GOT32
];
127 case BFD_RELOC_386_PLT32
:
128 TRACE ("BFD_RELOC_386_PLT32");
129 return &elf_howto_table
[ (int)R_386_PLT32
];
131 case BFD_RELOC_386_COPY
:
132 TRACE ("BFD_RELOC_386_COPY");
133 return &elf_howto_table
[ (int)R_386_COPY
];
135 case BFD_RELOC_386_GLOB_DAT
:
136 TRACE ("BFD_RELOC_386_GLOB_DAT");
137 return &elf_howto_table
[ (int)R_386_GLOB_DAT
];
139 case BFD_RELOC_386_JUMP_SLOT
:
140 TRACE ("BFD_RELOC_386_JUMP_SLOT");
141 return &elf_howto_table
[ (int)R_386_JUMP_SLOT
];
143 case BFD_RELOC_386_RELATIVE
:
144 TRACE ("BFD_RELOC_386_RELATIVE");
145 return &elf_howto_table
[ (int)R_386_RELATIVE
];
147 case BFD_RELOC_386_GOTOFF
:
148 TRACE ("BFD_RELOC_386_GOTOFF");
149 return &elf_howto_table
[ (int)R_386_GOTOFF
];
151 case BFD_RELOC_386_GOTPC
:
152 TRACE ("BFD_RELOC_386_GOTPC");
153 return &elf_howto_table
[ (int)R_386_GOTPC
];
164 elf_i386_info_to_howto (abfd
, cache_ptr
, dst
)
167 Elf32_Internal_Rela
*dst
;
169 BFD_ASSERT (ELF32_R_TYPE(dst
->r_info
) < (unsigned int) R_386_max
);
171 cache_ptr
->howto
= &elf_howto_table
[ELF32_R_TYPE(dst
->r_info
)];
175 elf_i386_info_to_howto_rel (abfd
, cache_ptr
, dst
)
178 Elf32_Internal_Rel
*dst
;
180 BFD_ASSERT (ELF32_R_TYPE(dst
->r_info
) < (unsigned int) R_386_max
);
182 cache_ptr
->howto
= &elf_howto_table
[ELF32_R_TYPE(dst
->r_info
)];
185 /* Functions for the i386 ELF linker. */
187 /* The name of the dynamic interpreter. This is put in the .interp
190 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
192 /* The size in bytes of an entry in the procedure linkage table. */
194 #define PLT_ENTRY_SIZE 16
196 /* The first entry in an absolute procedure linkage table looks like
197 this. See the SVR4 ABI i386 supplement to see how this works. */
199 static const bfd_byte elf_i386_plt0_entry
[PLT_ENTRY_SIZE
] =
201 0xff, 0x35, /* pushl contents of address */
202 0, 0, 0, 0, /* replaced with address of .got + 4. */
203 0xff, 0x25, /* jmp indirect */
204 0, 0, 0, 0, /* replaced with address of .got + 8. */
205 0, 0, 0, 0 /* pad out to 16 bytes. */
208 /* Subsequent entries in an absolute procedure linkage table look like
211 static const bfd_byte elf_i386_plt_entry
[PLT_ENTRY_SIZE
] =
213 0xff, 0x25, /* jmp indirect */
214 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
215 0x68, /* pushl immediate */
216 0, 0, 0, 0, /* replaced with offset into relocation table. */
217 0xe9, /* jmp relative */
218 0, 0, 0, 0 /* replaced with offset to start of .plt. */
221 /* The first entry in a PIC procedure linkage table look like this. */
223 static const bfd_byte elf_i386_pic_plt0_entry
[PLT_ENTRY_SIZE
] =
225 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
226 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
227 0, 0, 0, 0 /* pad out to 16 bytes. */
230 /* Subsequent entries in a PIC procedure linkage table look like this. */
232 static const bfd_byte elf_i386_pic_plt_entry
[PLT_ENTRY_SIZE
] =
234 0xff, 0xa3, /* jmp *offset(%ebx) */
235 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
236 0x68, /* pushl immediate */
237 0, 0, 0, 0, /* replaced with offset into relocation table. */
238 0xe9, /* jmp relative */
239 0, 0, 0, 0 /* replaced with offset to start of .plt. */
242 /* Look through the relocs for a section during the first phase, and
243 allocate space in the global offset table or procedure linkage
247 elf_i386_check_relocs (abfd
, info
, sec
, relocs
)
249 struct bfd_link_info
*info
;
251 const Elf_Internal_Rela
*relocs
;
254 Elf_Internal_Shdr
*symtab_hdr
;
255 struct elf_link_hash_entry
**sym_hashes
;
256 bfd_vma
*local_got_offsets
;
257 const Elf_Internal_Rela
*rel
;
258 const Elf_Internal_Rela
*rel_end
;
263 if (info
->relocateable
)
266 dynobj
= elf_hash_table (info
)->dynobj
;
267 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
268 sym_hashes
= elf_sym_hashes (abfd
);
269 local_got_offsets
= elf_local_got_offsets (abfd
);
275 rel_end
= relocs
+ sec
->reloc_count
;
276 for (rel
= relocs
; rel
< rel_end
; rel
++)
278 unsigned long r_symndx
;
279 struct elf_link_hash_entry
*h
;
281 r_symndx
= ELF32_R_SYM (rel
->r_info
);
283 if (r_symndx
< symtab_hdr
->sh_info
)
286 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
288 /* Some relocs require a global offset table. */
291 switch (ELF32_R_TYPE (rel
->r_info
))
296 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
297 if (! _bfd_elf_create_got_section (dynobj
, info
))
306 switch (ELF32_R_TYPE (rel
->r_info
))
309 /* This symbol requires a global offset table entry. */
313 sgot
= bfd_get_section_by_name (dynobj
, ".got");
314 BFD_ASSERT (sgot
!= NULL
);
318 && (h
!= NULL
|| info
->shared
))
320 srelgot
= bfd_get_section_by_name (dynobj
, ".rel.got");
323 srelgot
= bfd_make_section (dynobj
, ".rel.got");
325 || ! bfd_set_section_flags (dynobj
, srelgot
,
331 || ! bfd_set_section_alignment (dynobj
, srelgot
, 2))
338 if (h
->got_offset
!= (bfd_vma
) -1)
340 /* We have already allocated space in the .got. */
343 h
->got_offset
= sgot
->_raw_size
;
345 /* Make sure this symbol is output as a dynamic symbol. */
346 if (h
->dynindx
== -1)
348 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
352 srelgot
->_raw_size
+= sizeof (Elf32_External_Rel
);
356 /* This is a global offset table entry for a local
358 if (local_got_offsets
== NULL
)
361 register unsigned int i
;
363 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
364 local_got_offsets
= (bfd_vma
*) bfd_alloc (abfd
, size
);
365 if (local_got_offsets
== NULL
)
367 elf_local_got_offsets (abfd
) = local_got_offsets
;
368 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
369 local_got_offsets
[i
] = (bfd_vma
) -1;
371 if (local_got_offsets
[r_symndx
] != (bfd_vma
) -1)
373 /* We have already allocated space in the .got. */
376 local_got_offsets
[r_symndx
] = sgot
->_raw_size
;
380 /* If we are generating a shared object, we need to
381 output a R_386_RELATIVE reloc so that the dynamic
382 linker can adjust this GOT entry. */
383 srelgot
->_raw_size
+= sizeof (Elf32_External_Rel
);
387 sgot
->_raw_size
+= 4;
392 /* This symbol requires a procedure linkage table entry. We
393 actually build the entry in adjust_dynamic_symbol,
394 because this might be a case of linking PIC code without
395 linking in any dynamic objects, in which case we don't
396 need to generate a procedure linkage table after all. */
398 /* If this is a local symbol, we resolve it directly without
399 creating a procedure linkage table entry. */
403 /* Make sure this symbol is output as a dynamic symbol. */
404 if (h
->dynindx
== -1)
406 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
410 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
417 && (sec
->flags
& SEC_ALLOC
) != 0
418 && (ELF32_R_TYPE (rel
->r_info
) != R_386_PC32
|| h
!= NULL
))
420 /* When creating a shared object, we must copy these
421 reloc types into the output file. We create a reloc
422 section in dynobj and make room for this reloc. */
427 name
= (bfd_elf_string_from_elf_section
429 elf_elfheader (abfd
)->e_shstrndx
,
430 elf_section_data (sec
)->rel_hdr
.sh_name
));
434 BFD_ASSERT (strncmp (name
, ".rel", 4) == 0
435 && strcmp (bfd_get_section_name (abfd
, sec
),
438 sreloc
= bfd_get_section_by_name (dynobj
, name
);
441 sreloc
= bfd_make_section (dynobj
, name
);
443 || ! bfd_set_section_flags (dynobj
, sreloc
,
449 || ! bfd_set_section_alignment (dynobj
, sreloc
, 2))
454 sreloc
->_raw_size
+= sizeof (Elf32_External_Rel
);
467 /* Adjust a symbol defined by a dynamic object and referenced by a
468 regular object. The current definition is in some section of the
469 dynamic object, but we're not including those sections. We have to
470 change the definition to something the rest of the link can
474 elf_i386_adjust_dynamic_symbol (info
, h
)
475 struct bfd_link_info
*info
;
476 struct elf_link_hash_entry
*h
;
480 unsigned int power_of_two
;
482 dynobj
= elf_hash_table (info
)->dynobj
;
484 /* Make sure we know what is going on here. */
485 BFD_ASSERT (dynobj
!= NULL
486 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
487 || h
->weakdef
!= NULL
488 || ((h
->elf_link_hash_flags
489 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
490 && (h
->elf_link_hash_flags
491 & ELF_LINK_HASH_REF_REGULAR
) != 0
492 && (h
->elf_link_hash_flags
493 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
495 /* If this is a function, put it in the procedure linkage table. We
496 will fill in the contents of the procedure linkage table later,
497 when we know the address of the .got section. */
498 if (h
->type
== STT_FUNC
499 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
501 if (! elf_hash_table (info
)->dynamic_sections_created
)
503 /* This case can occur if we saw a PLT32 reloc in an input
504 file, but none of the input files were dynamic objects.
505 In such a case, we don't actually need to build a
506 procedure linkage table, and we can just do a PC32 reloc
508 BFD_ASSERT ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0);
512 s
= bfd_get_section_by_name (dynobj
, ".plt");
513 BFD_ASSERT (s
!= NULL
);
515 /* If this is the first .plt entry, make room for the special
517 if (s
->_raw_size
== 0)
518 s
->_raw_size
+= PLT_ENTRY_SIZE
;
520 /* If this symbol is not defined in a regular file, and we are
521 not generating a shared library, then set the symbol to this
522 location in the .plt. This is required to make function
523 pointers compare as equal between the normal executable and
524 the shared library. */
526 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
528 h
->root
.u
.def
.section
= s
;
529 h
->root
.u
.def
.value
= s
->_raw_size
;
532 h
->plt_offset
= s
->_raw_size
;
534 /* Make room for this entry. */
535 s
->_raw_size
+= PLT_ENTRY_SIZE
;
537 /* We also need to make an entry in the .got.plt section, which
538 will be placed in the .got section by the linker script. */
540 s
= bfd_get_section_by_name (dynobj
, ".got.plt");
541 BFD_ASSERT (s
!= NULL
);
544 /* We also need to make an entry in the .rel.plt section. */
546 s
= bfd_get_section_by_name (dynobj
, ".rel.plt");
547 BFD_ASSERT (s
!= NULL
);
548 s
->_raw_size
+= sizeof (Elf32_External_Rel
);
553 /* If this is a weak symbol, and there is a real definition, the
554 processor independent code will have arranged for us to see the
555 real definition first, and we can just use the same value. */
556 if (h
->weakdef
!= NULL
)
558 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
559 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
560 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
561 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
565 /* This is a reference to a symbol defined by a dynamic object which
566 is not a function. */
568 /* If we are creating a shared library, we must presume that the
569 only references to the symbol are via the global offset table.
570 For such cases we need not do anything here; the relocations will
571 be handled correctly by relocate_section. */
575 /* We must allocate the symbol in our .dynbss section, which will
576 become part of the .bss section of the executable. There will be
577 an entry for this symbol in the .dynsym section. The dynamic
578 object will contain position independent code, so all references
579 from the dynamic object to this symbol will go through the global
580 offset table. The dynamic linker will use the .dynsym entry to
581 determine the address it must put in the global offset table, so
582 both the dynamic object and the regular object will refer to the
583 same memory location for the variable. */
585 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
586 BFD_ASSERT (s
!= NULL
);
588 /* If the symbol is currently defined in the .bss section of the
589 dynamic object, then it is OK to simply initialize it to zero.
590 If the symbol is in some other section, we must generate a
591 R_386_COPY reloc to tell the dynamic linker to copy the initial
592 value out of the dynamic object and into the runtime process
593 image. We need to remember the offset into the .rel.bss section
594 we are going to use. */
595 if ((h
->root
.u
.def
.section
->flags
& SEC_LOAD
) != 0)
599 srel
= bfd_get_section_by_name (dynobj
, ".rel.bss");
600 BFD_ASSERT (srel
!= NULL
);
601 srel
->_raw_size
+= sizeof (Elf32_External_Rel
);
602 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
605 /* We need to figure out the alignment required for this symbol. I
606 have no idea how ELF linkers handle this. */
607 power_of_two
= bfd_log2 (h
->size
);
608 if (power_of_two
> 3)
611 /* Apply the required alignment. */
612 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
,
613 (bfd_size_type
) (1 << power_of_two
));
614 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
616 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
620 /* Define the symbol as being at this point in the section. */
621 h
->root
.u
.def
.section
= s
;
622 h
->root
.u
.def
.value
= s
->_raw_size
;
624 /* Increment the section size to make room for the symbol. */
625 s
->_raw_size
+= h
->size
;
630 /* Set the sizes of the dynamic sections. */
633 elf_i386_size_dynamic_sections (output_bfd
, info
)
635 struct bfd_link_info
*info
;
643 dynobj
= elf_hash_table (info
)->dynobj
;
644 BFD_ASSERT (dynobj
!= NULL
);
646 if (elf_hash_table (info
)->dynamic_sections_created
)
648 /* Set the contents of the .interp section to the interpreter. */
651 s
= bfd_get_section_by_name (dynobj
, ".interp");
652 BFD_ASSERT (s
!= NULL
);
653 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
654 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
659 /* We may have created entries in the .rel.got section.
660 However, if we are not creating the dynamic sections, we will
661 not actually use these entries. Reset the size of .rel.got,
662 which will cause it to get stripped from the output file
664 s
= bfd_get_section_by_name (dynobj
, ".rel.got");
669 /* The check_relocs and adjust_dynamic_symbol entry points have
670 determined the sizes of the various dynamic sections. Allocate
675 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
680 if ((s
->flags
& SEC_IN_MEMORY
) == 0)
683 /* It's OK to base decisions on the section name, because none
684 of the dynobj section names depend upon the input files. */
685 name
= bfd_get_section_name (dynobj
, s
);
689 if (strcmp (name
, ".plt") == 0)
691 if (s
->_raw_size
== 0)
693 /* Strip this section if we don't need it; see the
699 /* Remember whether there is a PLT. */
703 else if (strncmp (name
, ".rel", 4) == 0)
705 if (s
->_raw_size
== 0)
707 /* If we don't need this section, strip it from the
708 output file. This is mostly to handle .rel.bss and
709 .rel.plt. We must create both sections in
710 create_dynamic_sections, because they must be created
711 before the linker maps input sections to output
712 sections. The linker does that before
713 adjust_dynamic_symbol is called, and it is that
714 function which decides whether anything needs to go
715 into these sections. */
722 /* Remember whether there are any reloc sections other
724 if (strcmp (name
, ".rel.plt") != 0)
728 /* If this relocation section applies to a read only
729 section, then we probably need a DT_TEXTREL
730 entry. The entries in the .rel.plt section
731 really apply to the .got section, which we
732 created ourselves and so know is not readonly. */
733 target
= bfd_get_section_by_name (output_bfd
, name
+ 4);
735 && (target
->flags
& SEC_READONLY
) != 0)
739 /* We use the reloc_count field as a counter if we need
740 to copy relocs into the output file. */
744 else if (strncmp (name
, ".got", 4) != 0)
746 /* It's not one of our sections, so don't allocate space. */
754 for (spp
= &s
->output_section
->owner
->sections
;
755 *spp
!= s
->output_section
;
758 *spp
= s
->output_section
->next
;
759 --s
->output_section
->owner
->section_count
;
764 /* Allocate memory for the section contents. */
765 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
766 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
770 if (elf_hash_table (info
)->dynamic_sections_created
)
772 /* Add some entries to the .dynamic section. We fill in the
773 values later, in elf_i386_finish_dynamic_sections, but we
774 must add the entries now so that we get the correct size for
775 the .dynamic section. The DT_DEBUG entry is filled in by the
776 dynamic linker and used by the debugger. */
779 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
785 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0)
786 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
787 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTREL
, DT_REL
)
788 || ! bfd_elf32_add_dynamic_entry (info
, DT_JMPREL
, 0))
794 if (! bfd_elf32_add_dynamic_entry (info
, DT_REL
, 0)
795 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELSZ
, 0)
796 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELENT
,
797 sizeof (Elf32_External_Rel
)))
803 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
811 /* Relocate an i386 ELF section. */
814 elf_i386_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
815 contents
, relocs
, local_syms
, local_sections
)
817 struct bfd_link_info
*info
;
819 asection
*input_section
;
821 Elf_Internal_Rela
*relocs
;
822 Elf_Internal_Sym
*local_syms
;
823 asection
**local_sections
;
826 Elf_Internal_Shdr
*symtab_hdr
;
827 struct elf_link_hash_entry
**sym_hashes
;
828 bfd_vma
*local_got_offsets
;
832 Elf_Internal_Rela
*rel
;
833 Elf_Internal_Rela
*relend
;
835 dynobj
= elf_hash_table (info
)->dynobj
;
836 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
837 sym_hashes
= elf_sym_hashes (input_bfd
);
838 local_got_offsets
= elf_local_got_offsets (input_bfd
);
845 relend
= relocs
+ input_section
->reloc_count
;
846 for (; rel
< relend
; rel
++)
849 reloc_howto_type
*howto
;
850 unsigned long r_symndx
;
851 struct elf_link_hash_entry
*h
;
852 Elf_Internal_Sym
*sym
;
855 bfd_reloc_status_type r
;
857 r_type
= ELF32_R_TYPE (rel
->r_info
);
858 if (r_type
< 0 || r_type
>= (int) R_386_max
)
860 bfd_set_error (bfd_error_bad_value
);
863 howto
= elf_howto_table
+ r_type
;
865 r_symndx
= ELF32_R_SYM (rel
->r_info
);
867 if (info
->relocateable
)
869 /* This is a relocateable link. We don't have to change
870 anything, unless the reloc is against a section symbol,
871 in which case we have to adjust according to where the
872 section symbol winds up in the output section. */
873 if (r_symndx
< symtab_hdr
->sh_info
)
875 sym
= local_syms
+ r_symndx
;
876 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
880 sec
= local_sections
[r_symndx
];
881 val
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
882 val
+= sec
->output_offset
+ sym
->st_value
;
883 bfd_put_32 (input_bfd
, val
, contents
+ rel
->r_offset
);
890 /* This is a final link. */
894 if (r_symndx
< symtab_hdr
->sh_info
)
896 sym
= local_syms
+ r_symndx
;
897 sec
= local_sections
[r_symndx
];
898 relocation
= (sec
->output_section
->vma
904 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
905 if (h
->root
.type
== bfd_link_hash_defined
906 || h
->root
.type
== bfd_link_hash_defweak
)
908 sec
= h
->root
.u
.def
.section
;
909 if (r_type
== R_386_GOTPC
910 || (r_type
== R_386_PLT32
911 && h
->plt_offset
!= (bfd_vma
) -1)
912 || (r_type
== R_386_GOT32
913 && elf_hash_table (info
)->dynamic_sections_created
916 || (h
->elf_link_hash_flags
917 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
919 && (r_type
== R_386_32
920 || r_type
== R_386_PC32
)
921 && (input_section
->flags
& SEC_ALLOC
) != 0))
923 /* In these cases, we don't need the relocation
924 value. We check specially because in some
925 obscure cases sec->output_section will be NULL. */
929 relocation
= (h
->root
.u
.def
.value
930 + sec
->output_section
->vma
931 + sec
->output_offset
);
933 else if (h
->root
.type
== bfd_link_hash_undefweak
)
935 else if (info
->shared
&& !info
->symbolic
)
939 if (! ((*info
->callbacks
->undefined_symbol
)
940 (info
, h
->root
.root
.string
, input_bfd
,
941 input_section
, rel
->r_offset
)))
950 /* Relocation is to the entry for this symbol in the global
954 sgot
= bfd_get_section_by_name (dynobj
, ".got");
955 BFD_ASSERT (sgot
!= NULL
);
963 BFD_ASSERT (off
!= (bfd_vma
) -1);
965 if (! elf_hash_table (info
)->dynamic_sections_created
968 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
970 /* This is actually a static link, or it is a
971 -Bsymbolic link and the symbol is defined
972 locally. We must initialize this entry in the
973 global offset table. Since the offset must
974 always be a multiple of 4, we use the least
975 significant bit to record whether we have
976 initialized it already.
978 When doing a dynamic link, we create a .rel.got
979 relocation entry to initialize the value. This
980 is done in the finish_dynamic_symbol routine. */
985 bfd_put_32 (output_bfd
, relocation
,
986 sgot
->contents
+ off
);
991 relocation
= sgot
->output_offset
+ off
;
997 BFD_ASSERT (local_got_offsets
!= NULL
998 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1000 off
= local_got_offsets
[r_symndx
];
1002 /* The offset must always be a multiple of 4. We use
1003 the least significant bit to record whether we have
1004 already generated the necessary reloc. */
1009 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
1014 Elf_Internal_Rel outrel
;
1016 srelgot
= bfd_get_section_by_name (dynobj
, ".rel.got");
1017 BFD_ASSERT (srelgot
!= NULL
);
1019 outrel
.r_offset
= (sgot
->output_section
->vma
1020 + sgot
->output_offset
1022 outrel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1023 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
1024 (((Elf32_External_Rel
*)
1026 + srelgot
->reloc_count
));
1027 ++srelgot
->reloc_count
;
1030 local_got_offsets
[r_symndx
] |= 1;
1033 relocation
= sgot
->output_offset
+ off
;
1039 /* Relocation is relative to the start of the global offset
1044 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1045 BFD_ASSERT (sgot
!= NULL
);
1048 /* Note that sgot->output_offset is not involved in this
1049 calculation. We always want the start of .got. If we
1050 defined _GLOBAL_OFFSET_TABLE in a different way, as is
1051 permitted by the ABI, we might have to change this
1053 relocation
-= sgot
->output_section
->vma
;
1058 /* Use global offset table as symbol value. */
1062 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1063 BFD_ASSERT (sgot
!= NULL
);
1066 relocation
= sgot
->output_section
->vma
;
1071 /* Relocation is to the entry for this symbol in the
1072 procedure linkage table. */
1074 /* Resolve a PLT32 reloc again a local symbol directly,
1075 without using the procedure linkage table. */
1079 if (h
->plt_offset
== (bfd_vma
) -1)
1081 /* We didn't make a PLT entry for this symbol. This
1082 happens when statically linking PIC code, or when
1083 using -Bsymbolic. */
1089 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1090 BFD_ASSERT (splt
!= NULL
);
1093 relocation
= (splt
->output_section
->vma
1094 + splt
->output_offset
1102 && (input_section
->flags
& SEC_ALLOC
) != 0
1103 && (r_type
!= R_386_PC32
|| h
!= NULL
))
1105 Elf_Internal_Rel outrel
;
1107 /* When generating a shared object, these relocations
1108 are copied into the output file to be resolved at run
1115 name
= (bfd_elf_string_from_elf_section
1117 elf_elfheader (input_bfd
)->e_shstrndx
,
1118 elf_section_data (input_section
)->rel_hdr
.sh_name
));
1122 BFD_ASSERT (strncmp (name
, ".rel", 4) == 0
1123 && strcmp (bfd_get_section_name (input_bfd
,
1127 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1128 BFD_ASSERT (sreloc
!= NULL
);
1131 outrel
.r_offset
= (rel
->r_offset
1132 + input_section
->output_section
->vma
1133 + input_section
->output_offset
);
1134 if (r_type
== R_386_PC32
)
1136 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
1137 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_PC32
);
1142 outrel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1145 BFD_ASSERT (h
->dynindx
!= -1);
1146 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_32
);
1150 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
1151 (((Elf32_External_Rel
*)
1153 + sreloc
->reloc_count
));
1154 ++sreloc
->reloc_count
;
1156 /* If this reloc is against an external symbol, we do
1157 not want to fiddle with the addend. Otherwise, we
1158 need to include the symbol value so that it becomes
1159 an addend for the dynamic reloc. */
1170 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1171 contents
, rel
->r_offset
,
1172 relocation
, (bfd_vma
) 0);
1174 if (r
!= bfd_reloc_ok
)
1179 case bfd_reloc_outofrange
:
1181 case bfd_reloc_overflow
:
1186 name
= h
->root
.root
.string
;
1189 name
= bfd_elf_string_from_elf_section (input_bfd
,
1190 symtab_hdr
->sh_link
,
1195 name
= bfd_section_name (input_bfd
, sec
);
1197 if (! ((*info
->callbacks
->reloc_overflow
)
1198 (info
, name
, howto
->name
, (bfd_vma
) 0,
1199 input_bfd
, input_section
, rel
->r_offset
)))
1210 /* Finish up dynamic symbol handling. We set the contents of various
1211 dynamic sections here. */
1214 elf_i386_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
1216 struct bfd_link_info
*info
;
1217 struct elf_link_hash_entry
*h
;
1218 Elf_Internal_Sym
*sym
;
1222 dynobj
= elf_hash_table (info
)->dynobj
;
1224 if (h
->plt_offset
!= (bfd_vma
) -1)
1231 Elf_Internal_Rel rel
;
1233 /* This symbol has an entry in the procedure linkage table. Set
1236 BFD_ASSERT (h
->dynindx
!= -1);
1238 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1239 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
1240 srel
= bfd_get_section_by_name (dynobj
, ".rel.plt");
1241 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
1243 /* Get the index in the procedure linkage table which
1244 corresponds to this symbol. This is the index of this symbol
1245 in all the symbols for which we are making plt entries. The
1246 first entry in the procedure linkage table is reserved. */
1247 plt_index
= h
->plt_offset
/ PLT_ENTRY_SIZE
- 1;
1249 /* Get the offset into the .got table of the entry that
1250 corresponds to this function. Each .got entry is 4 bytes.
1251 The first three are reserved. */
1252 got_offset
= (plt_index
+ 3) * 4;
1254 /* Fill in the entry in the procedure linkage table. */
1257 memcpy (splt
->contents
+ h
->plt_offset
, elf_i386_plt_entry
,
1259 bfd_put_32 (output_bfd
,
1260 (sgot
->output_section
->vma
1261 + sgot
->output_offset
1263 splt
->contents
+ h
->plt_offset
+ 2);
1267 memcpy (splt
->contents
+ h
->plt_offset
, elf_i386_pic_plt_entry
,
1269 bfd_put_32 (output_bfd
, got_offset
,
1270 splt
->contents
+ h
->plt_offset
+ 2);
1273 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rel
),
1274 splt
->contents
+ h
->plt_offset
+ 7);
1275 bfd_put_32 (output_bfd
, - (h
->plt_offset
+ PLT_ENTRY_SIZE
),
1276 splt
->contents
+ h
->plt_offset
+ 12);
1278 /* Fill in the entry in the global offset table. */
1279 bfd_put_32 (output_bfd
,
1280 (splt
->output_section
->vma
1281 + splt
->output_offset
1284 sgot
->contents
+ got_offset
);
1286 /* Fill in the entry in the .rel.plt section. */
1287 rel
.r_offset
= (sgot
->output_section
->vma
1288 + sgot
->output_offset
1290 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_JUMP_SLOT
);
1291 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1292 ((Elf32_External_Rel
*) srel
->contents
1295 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1297 /* Mark the symbol as undefined, rather than as defined in
1298 the .plt section. Leave the value alone. */
1299 sym
->st_shndx
= SHN_UNDEF
;
1303 if (h
->got_offset
!= (bfd_vma
) -1)
1307 Elf_Internal_Rel rel
;
1309 /* This symbol has an entry in the global offset table. Set it
1312 BFD_ASSERT (h
->dynindx
!= -1);
1314 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1315 srel
= bfd_get_section_by_name (dynobj
, ".rel.got");
1316 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
1318 rel
.r_offset
= (sgot
->output_section
->vma
1319 + sgot
->output_offset
1320 + (h
->got_offset
&~ 1));
1322 /* If this is a -Bsymbolic link, and the symbol is defined
1323 locally, we just want to emit a RELATIVE reloc. The entry in
1324 the global offset table will already have been initialized in
1325 the relocate_section function. */
1328 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
))
1329 rel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1332 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got_offset
);
1333 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_GLOB_DAT
);
1336 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1337 ((Elf32_External_Rel
*) srel
->contents
1338 + srel
->reloc_count
));
1339 ++srel
->reloc_count
;
1342 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
1345 Elf_Internal_Rel rel
;
1347 /* This symbol needs a copy reloc. Set it up. */
1349 BFD_ASSERT (h
->dynindx
!= -1
1350 && (h
->root
.type
== bfd_link_hash_defined
1351 || h
->root
.type
== bfd_link_hash_defweak
));
1353 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
1355 BFD_ASSERT (s
!= NULL
);
1357 rel
.r_offset
= (h
->root
.u
.def
.value
1358 + h
->root
.u
.def
.section
->output_section
->vma
1359 + h
->root
.u
.def
.section
->output_offset
);
1360 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_COPY
);
1361 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1362 ((Elf32_External_Rel
*) s
->contents
1367 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1368 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
1369 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
1370 sym
->st_shndx
= SHN_ABS
;
1375 /* Finish up the dynamic sections. */
1378 elf_i386_finish_dynamic_sections (output_bfd
, info
)
1380 struct bfd_link_info
*info
;
1386 dynobj
= elf_hash_table (info
)->dynobj
;
1388 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
1389 BFD_ASSERT (sgot
!= NULL
);
1390 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
1392 if (elf_hash_table (info
)->dynamic_sections_created
)
1395 Elf32_External_Dyn
*dyncon
, *dynconend
;
1397 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1398 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
1400 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
1401 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
1402 for (; dyncon
< dynconend
; dyncon
++)
1404 Elf_Internal_Dyn dyn
;
1408 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
1421 s
= bfd_get_section_by_name (output_bfd
, name
);
1422 BFD_ASSERT (s
!= NULL
);
1423 dyn
.d_un
.d_ptr
= s
->vma
;
1424 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1428 s
= bfd_get_section_by_name (output_bfd
, ".rel.plt");
1429 BFD_ASSERT (s
!= NULL
);
1430 if (s
->_cooked_size
!= 0)
1431 dyn
.d_un
.d_val
= s
->_cooked_size
;
1433 dyn
.d_un
.d_val
= s
->_raw_size
;
1434 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1438 /* My reading of the SVR4 ABI indicates that the
1439 procedure linkage table relocs (DT_JMPREL) should be
1440 included in the overall relocs (DT_REL). This is
1441 what Solaris does. However, UnixWare can not handle
1442 that case. Therefore, we override the DT_RELSZ entry
1443 here to make it not include the JMPREL relocs. Since
1444 the linker script arranges for .rel.plt to follow all
1445 other relocation sections, we don't have to worry
1446 about changing the DT_REL entry. */
1447 s
= bfd_get_section_by_name (output_bfd
, ".rel.plt");
1450 if (s
->_cooked_size
!= 0)
1451 dyn
.d_un
.d_val
-= s
->_cooked_size
;
1453 dyn
.d_un
.d_val
-= s
->_raw_size
;
1455 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1460 /* Fill in the first entry in the procedure linkage table. */
1461 if (splt
->_raw_size
> 0)
1464 memcpy (splt
->contents
, elf_i386_pic_plt0_entry
, PLT_ENTRY_SIZE
);
1467 memcpy (splt
->contents
, elf_i386_plt0_entry
, PLT_ENTRY_SIZE
);
1468 bfd_put_32 (output_bfd
,
1469 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
1470 splt
->contents
+ 2);
1471 bfd_put_32 (output_bfd
,
1472 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
1473 splt
->contents
+ 8);
1477 /* UnixWare sets the entsize of .plt to 4, although that doesn't
1478 really seem like the right value. */
1479 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
1482 /* Fill in the first three entries in the global offset table. */
1483 if (sgot
->_raw_size
> 0)
1486 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
1488 bfd_put_32 (output_bfd
,
1489 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
1491 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
1492 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
1495 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
1500 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
1501 #define TARGET_LITTLE_NAME "elf32-i386"
1502 #define ELF_ARCH bfd_arch_i386
1503 #define ELF_MACHINE_CODE EM_386
1504 #define elf_info_to_howto elf_i386_info_to_howto
1505 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
1506 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
1507 #define ELF_MAXPAGESIZE 0x1000
1508 #define elf_backend_create_dynamic_sections \
1509 _bfd_elf_create_dynamic_sections
1510 #define elf_backend_check_relocs elf_i386_check_relocs
1511 #define elf_backend_adjust_dynamic_symbol \
1512 elf_i386_adjust_dynamic_symbol
1513 #define elf_backend_size_dynamic_sections \
1514 elf_i386_size_dynamic_sections
1515 #define elf_backend_relocate_section elf_i386_relocate_section
1516 #define elf_backend_finish_dynamic_symbol \
1517 elf_i386_finish_dynamic_symbol
1518 #define elf_backend_finish_dynamic_sections \
1519 elf_i386_finish_dynamic_sections
1520 #define elf_backend_want_got_plt 1
1521 #define elf_backend_plt_readonly 1
1522 #define elf_backend_want_plt_sym 0
1524 #include "elf32-target.h"