1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 static CONST
struct reloc_howto_struct
*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_create_dynamic_sections
33 PARAMS ((bfd
*, struct bfd_link_info
*));
34 static boolean elf_i386_check_relocs
35 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
36 const Elf_Internal_Rela
*));
37 static boolean elf_i386_adjust_dynamic_symbol
38 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
39 static boolean elf_i386_size_dynamic_sections
40 PARAMS ((bfd
*, struct bfd_link_info
*));
41 static boolean elf_i386_relocate_section
42 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
43 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
44 static boolean elf_i386_finish_dynamic_symbol
45 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
47 static boolean elf_i386_finish_dynamic_sections
48 PARAMS ((bfd
*, struct bfd_link_info
*));
50 #define USE_REL 1 /* 386 uses REL relocations instead of RELA */
69 static CONST
char *CONST reloc_type_names
[] =
85 static reloc_howto_type elf_howto_table
[]=
87 HOWTO(R_386_NONE
, 0,0, 0,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_NONE", true,0x00000000,0x00000000,false),
88 HOWTO(R_386_32
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_32", true,0xffffffff,0xffffffff,false),
89 HOWTO(R_386_PC32
, 0,2,32,true, 0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_PC32", true,0xffffffff,0xffffffff,true),
90 HOWTO(R_386_GOT32
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_GOT32", true,0xffffffff,0xffffffff,false),
91 HOWTO(R_386_PLT32
, 0,2,32,true,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_PLT32", true,0xffffffff,0xffffffff,true),
92 HOWTO(R_386_COPY
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_COPY", true,0xffffffff,0xffffffff,false),
93 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),
94 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),
95 HOWTO(R_386_RELATIVE
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false),
96 HOWTO(R_386_GOTOFF
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_GOTOFF", true,0xffffffff,0xffffffff,false),
97 HOWTO(R_386_GOTPC
, 0,2,32,true,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_GOTPC", true,0xffffffff,0xffffffff,true),
100 #ifdef DEBUG_GEN_RELOC
101 #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
106 static CONST
struct reloc_howto_struct
*
107 elf_i386_reloc_type_lookup (abfd
, code
)
109 bfd_reloc_code_real_type code
;
114 TRACE ("BFD_RELOC_NONE");
115 return &elf_howto_table
[ (int)R_386_NONE
];
118 TRACE ("BFD_RELOC_32");
119 return &elf_howto_table
[ (int)R_386_32
];
121 case BFD_RELOC_32_PCREL
:
122 TRACE ("BFD_RELOC_PC32");
123 return &elf_howto_table
[ (int)R_386_PC32
];
125 case BFD_RELOC_386_GOT32
:
126 TRACE ("BFD_RELOC_386_GOT32");
127 return &elf_howto_table
[ (int)R_386_GOT32
];
129 case BFD_RELOC_386_PLT32
:
130 TRACE ("BFD_RELOC_386_PLT32");
131 return &elf_howto_table
[ (int)R_386_PLT32
];
133 case BFD_RELOC_386_COPY
:
134 TRACE ("BFD_RELOC_386_COPY");
135 return &elf_howto_table
[ (int)R_386_COPY
];
137 case BFD_RELOC_386_GLOB_DAT
:
138 TRACE ("BFD_RELOC_386_GLOB_DAT");
139 return &elf_howto_table
[ (int)R_386_GLOB_DAT
];
141 case BFD_RELOC_386_JUMP_SLOT
:
142 TRACE ("BFD_RELOC_386_JUMP_SLOT");
143 return &elf_howto_table
[ (int)R_386_JUMP_SLOT
];
145 case BFD_RELOC_386_RELATIVE
:
146 TRACE ("BFD_RELOC_386_RELATIVE");
147 return &elf_howto_table
[ (int)R_386_RELATIVE
];
149 case BFD_RELOC_386_GOTOFF
:
150 TRACE ("BFD_RELOC_386_GOTOFF");
151 return &elf_howto_table
[ (int)R_386_GOTOFF
];
153 case BFD_RELOC_386_GOTPC
:
154 TRACE ("BFD_RELOC_386_GOTPC");
155 return &elf_howto_table
[ (int)R_386_GOTPC
];
166 elf_i386_info_to_howto (abfd
, cache_ptr
, dst
)
169 Elf32_Internal_Rela
*dst
;
171 BFD_ASSERT (ELF32_R_TYPE(dst
->r_info
) < (unsigned int) R_386_max
);
173 cache_ptr
->howto
= &elf_howto_table
[ELF32_R_TYPE(dst
->r_info
)];
177 elf_i386_info_to_howto_rel (abfd
, cache_ptr
, dst
)
180 Elf32_Internal_Rel
*dst
;
182 BFD_ASSERT (ELF32_R_TYPE(dst
->r_info
) < (unsigned int) R_386_max
);
184 cache_ptr
->howto
= &elf_howto_table
[ELF32_R_TYPE(dst
->r_info
)];
187 /* Functions for the i386 ELF linker. */
189 /* The name of the dynamic interpreter. This is put in the .interp
192 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
194 /* The size in bytes of an entry in the procedure linkage table. */
196 #define PLT_ENTRY_SIZE 16
198 /* The first entry in an absolute procedure linkage table looks like
199 this. See the SVR4 ABI i386 supplement to see how this works. */
201 static bfd_byte elf_i386_plt0_entry
[PLT_ENTRY_SIZE
] =
203 0xff, 0x35, /* pushl contents of address */
204 0, 0, 0, 0, /* replaced with address of .got + 4. */
205 0xff, 0x25, /* jmp indirect */
206 0, 0, 0, 0, /* replaced with address of .got + 8. */
207 0, 0, 0, 0 /* pad out to 16 bytes. */
210 /* Subsequent entries in an absolute procedure linkage table look like
213 static bfd_byte elf_i386_plt_entry
[PLT_ENTRY_SIZE
] =
215 0xff, 0x25, /* jmp indirect */
216 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
217 0x68, /* pushl immediate */
218 0, 0, 0, 0, /* replaced with offset into relocation table. */
219 0xe9, /* jmp relative */
220 0, 0, 0, 0 /* replaced with offset to start of .plt. */
223 /* The first entry in a PIC procedure linkage table look like this. */
225 static bfd_byte elf_i386_pic_plt0_entry
[PLT_ENTRY_SIZE
] =
227 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
228 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
229 0, 0, 0, 0 /* pad out to 16 bytes. */
232 /* Subsequent entries in a PIC procedure linkage table look like this. */
234 static bfd_byte elf_i386_pic_plt_entry
[PLT_ENTRY_SIZE
] =
236 0xff, 0xa3, /* jmp *offset(%ebx) */
237 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
238 0x68, /* pushl immediate */
239 0, 0, 0, 0, /* replaced with offset into relocation table. */
240 0xe9, /* jmp relative */
241 0, 0, 0, 0 /* replaced with offset to start of .plt. */
244 /* Create dynamic sections when linking against a dynamic object. */
247 elf_i386_create_dynamic_sections (abfd
, info
)
249 struct bfd_link_info
*info
;
252 register asection
*s
;
253 struct elf_link_hash_entry
*h
;
255 /* We need to create .plt, .rel.plt, .got, .got.plt, .dynbss, and
256 .rel.bss sections. */
258 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
260 s
= bfd_make_section (abfd
, ".plt");
262 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
| SEC_CODE
)
263 || ! bfd_set_section_alignment (abfd
, s
, 2))
266 s
= bfd_make_section (abfd
, ".rel.plt");
268 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
269 || ! bfd_set_section_alignment (abfd
, s
, 2))
272 s
= bfd_make_section (abfd
, ".got");
274 || ! bfd_set_section_flags (abfd
, s
, flags
)
275 || ! bfd_set_section_alignment (abfd
, s
, 2))
278 s
= bfd_make_section (abfd
, ".got.plt");
280 || ! bfd_set_section_flags (abfd
, s
, flags
)
281 || ! bfd_set_section_alignment (abfd
, s
, 2))
284 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
285 .got.plt section, which will be placed at the start of the output
286 .got section. We don't do this in the linker script because we
287 don't want to define the symbol if we are not creating a global
290 if (! (_bfd_generic_link_add_one_symbol
291 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
, (bfd_vma
) 0,
292 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
293 (struct bfd_link_hash_entry
**) &h
)))
295 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
296 h
->type
= STT_OBJECT
;
299 && ! bfd_elf32_link_record_dynamic_symbol (info
, h
))
302 /* The first three global offset table entries are reserved. */
303 s
->_raw_size
+= 3 * 4;
305 /* The .dynbss section is a place to put symbols which are defined
306 by dynamic objects, are referenced by regular objects, and are
307 not functions. We must allocate space for them in the process
308 image and use a R_386_COPY reloc to tell the dynamic linker to
309 initialize them at run time. The linker script puts the .dynbss
310 section into the .bss section of the final image. */
311 s
= bfd_make_section (abfd
, ".dynbss");
313 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
316 /* The .rel.bss section holds copy relocs. This section is not
317 normally needed. We need to create it here, though, so that the
318 linker will map it to an output section. We can't just create it
319 only if we need it, because we will not know whether we need it
320 until we have seen all the input files, and the first time the
321 main linker code calls BFD after examining all the input files
322 (size_dynamic_sections) the input sections have already been
323 mapped to the output sections. If the section turns out not to
324 be needed, we can discard it later. We will never need this
325 section when generating a shared object, since they do not use
329 s
= bfd_make_section (abfd
, ".rel.bss");
331 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
332 || ! bfd_set_section_alignment (abfd
, s
, 2))
339 /* Look through the relocs for a section during the first phase, and
340 allocate space in the global offset table or procedure linkage
344 elf_i386_check_relocs (abfd
, info
, sec
, relocs
)
346 struct bfd_link_info
*info
;
348 const Elf_Internal_Rela
*relocs
;
351 Elf_Internal_Shdr
*symtab_hdr
;
352 struct elf_link_hash_entry
**sym_hashes
;
353 bfd_vma
*local_got_offsets
;
354 const Elf_Internal_Rela
*rel
;
355 const Elf_Internal_Rela
*rel_end
;
363 if (info
->relocateable
)
366 dynobj
= elf_hash_table (info
)->dynobj
;
367 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
368 sym_hashes
= elf_sym_hashes (abfd
);
369 local_got_offsets
= elf_local_got_offsets (abfd
);
378 rel_end
= relocs
+ sec
->reloc_count
;
379 for (rel
= relocs
; rel
< rel_end
; rel
++)
382 struct elf_link_hash_entry
*h
;
384 r_symndx
= ELF32_R_SYM (rel
->r_info
);
386 if (r_symndx
< symtab_hdr
->sh_info
)
389 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
391 /* Some relocs require a global offset table. FIXME: If this is
392 a static link of PIC code, we need a global offset table but
393 we don't really need to create the full dynamic linking
397 switch (ELF32_R_TYPE (rel
->r_info
))
403 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
404 if (! bfd_elf32_link_create_dynamic_sections (dynobj
, info
))
413 switch (ELF32_R_TYPE (rel
->r_info
))
416 /* This symbol requires a global offset table entry. */
420 sgot
= bfd_get_section_by_name (dynobj
, ".got");
421 srelgot
= bfd_get_section_by_name (dynobj
, ".rel.got");
424 srelgot
= bfd_make_section (dynobj
, ".rel.got");
426 || ! bfd_set_section_flags (dynobj
, srelgot
,
432 || ! bfd_set_section_alignment (dynobj
, srelgot
, 2))
435 BFD_ASSERT (sgot
!= NULL
&& srelgot
!= NULL
);
440 if (h
->got_offset
!= (bfd_vma
) -1)
442 /* We have already allocated space in the .got. */
445 h
->got_offset
= sgot
->_raw_size
;
447 /* Make sure this symbol is output as a dynamic symbol. */
448 if (h
->dynindx
== -1)
450 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
456 /* This is a global offset table entry for a local
458 if (local_got_offsets
== NULL
)
463 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
464 local_got_offsets
= (bfd_vma
*) bfd_alloc (abfd
, size
);
465 if (local_got_offsets
== NULL
)
467 bfd_set_error (bfd_error_no_memory
);
470 elf_local_got_offsets (abfd
) = local_got_offsets
;
471 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
472 local_got_offsets
[i
] = (bfd_vma
) -1;
474 if (local_got_offsets
[r_symndx
] != (bfd_vma
) -1)
476 /* We have already allocated space in the .got. */
479 local_got_offsets
[r_symndx
] = sgot
->_raw_size
;
482 sgot
->_raw_size
+= 4;
483 srelgot
->_raw_size
+= sizeof (Elf32_External_Rel
);
488 /* This symbol requires a procedure linkage table entry. */
490 /* If this is a local symbol, we resolve it directly without
491 creating a procedure linkage table entry. */
495 if (h
->plt_offset
!= (bfd_vma
) -1)
497 /* There is already an entry for this symbol in the
498 procedure linkage table. */
504 splt
= bfd_get_section_by_name (dynobj
, ".plt");
505 sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
506 srelplt
= bfd_get_section_by_name (dynobj
, ".rel.plt");
507 BFD_ASSERT (splt
!= NULL
&& sgotplt
!= NULL
&& srelplt
!= NULL
);
510 /* If this is the first .plt entry, make room for the
511 special first entry. */
512 if (splt
->_raw_size
== 0)
513 splt
->_raw_size
+= PLT_ENTRY_SIZE
;
515 /* Make sure this symbol is output as a dynamic symbol. */
516 if (h
->dynindx
== -1)
518 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
522 h
->plt_offset
= splt
->_raw_size
;
524 /* Make room for this entry. We need a procedure linkage
525 table entry in .plt, a global offset table entry in
526 .got.plt (which is placed in .got by the linker script),
527 and a relocation in .rel.plt. */
528 splt
->_raw_size
+= PLT_ENTRY_SIZE
;
529 sgotplt
->_raw_size
+= 4;
530 srelplt
->_raw_size
+= sizeof (Elf32_External_Rel
);
537 && (sec
->flags
& SEC_ALLOC
) != 0)
539 /* When creating a shared object, we must output a
540 R_386_RELATIVE reloc for this location. We create a
541 reloc section in dynobj and make room for this reloc. */
546 name
= (elf_string_from_elf_section
548 elf_elfheader (abfd
)->e_shstrndx
,
549 elf_section_data (sec
)->rel_hdr
.sh_name
));
553 BFD_ASSERT (strncmp (name
, ".rel", 4) == 0
554 && strcmp (bfd_get_section_name (abfd
, sec
),
557 sreloc
= bfd_get_section_by_name (dynobj
, name
);
560 sreloc
= bfd_make_section (dynobj
, name
);
562 || ! bfd_set_section_flags (dynobj
, sreloc
,
568 || ! bfd_set_section_alignment (dynobj
, sreloc
, 2))
573 sreloc
->_raw_size
+= sizeof (Elf32_External_Rel
);
586 /* Adjust a symbol defined by a dynamic object and referenced by a
587 regular object. The current definition is in some section of the
588 dynamic object, but we're not including those sections. We have to
589 change the definition to something the rest of the link can
593 elf_i386_adjust_dynamic_symbol (info
, h
)
594 struct bfd_link_info
*info
;
595 struct elf_link_hash_entry
*h
;
599 unsigned int power_of_two
;
601 dynobj
= elf_hash_table (info
)->dynobj
;
603 /* Make sure we know what is going on here. */
604 BFD_ASSERT (dynobj
!= NULL
605 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
606 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
607 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
608 && h
->root
.type
== bfd_link_hash_defined
609 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
610 == bfd_target_elf_flavour
)
611 && (elf_elfheader (h
->root
.u
.def
.section
->owner
)->e_type
613 && h
->root
.u
.def
.section
->output_section
== NULL
);
615 /* If this is a function, put it in the procedure linkage table. We
616 will fill in the contents of the procedure linkage table later,
617 when we know the address of the .got section. */
618 if (h
->type
== STT_FUNC
)
620 s
= bfd_get_section_by_name (dynobj
, ".plt");
621 BFD_ASSERT (s
!= NULL
);
623 if (h
->plt_offset
!= (bfd_vma
) -1)
625 h
->root
.u
.def
.section
= s
;
626 h
->root
.u
.def
.value
= h
->plt_offset
;
630 /* If this is the first .plt entry, make room for the
631 special first entry. */
632 if (s
->_raw_size
== 0)
633 s
->_raw_size
+= PLT_ENTRY_SIZE
;
635 /* Set the symbol to this location in the .plt. */
636 h
->root
.u
.def
.section
= s
;
637 h
->root
.u
.def
.value
= s
->_raw_size
;
639 h
->plt_offset
= s
->_raw_size
;
641 /* Make room for this entry. */
642 s
->_raw_size
+= PLT_ENTRY_SIZE
;
644 /* We also need to make an entry in the .got.plt section,
645 which will be placed in the .got section by the linker
648 s
= bfd_get_section_by_name (dynobj
, ".got.plt");
649 BFD_ASSERT (s
!= NULL
);
652 /* We also need to make an entry in the .rel.plt section. */
654 s
= bfd_get_section_by_name (dynobj
, ".rel.plt");
655 BFD_ASSERT (s
!= NULL
);
656 s
->_raw_size
+= sizeof (Elf32_External_Rel
);
662 /* If this is a weak symbol, and there is a real definition, the
663 processor independent code will have arranged for us to see the
664 real definition first, and we can just use the same value. */
665 if (h
->weakdef
!= NULL
)
667 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
);
668 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
669 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
673 /* This is a reference to a symbol defined by a dynamic object which
674 is not a function. */
676 /* If we are creating a shared library, we must presume that the
677 only references to the symbol are via the global offset table.
678 For such cases we need not do anything here; the relocations will
679 be handled correctly by relocate_section. */
683 /* We must allocate the symbol in our .dynbss section, which will
684 become part of the .bss section of the executable. There will be
685 an entry for this symbol in the .dynsym section. The dynamic
686 object will contain position independent code, so all references
687 from the dynamic object to this symbol will go through the global
688 offset table. The dynamic linker will use the .dynsym entry to
689 determine the address it must put in the global offset table, so
690 both the dynamic object and the regular object will refer to the
691 same memory location for the variable. */
693 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
694 BFD_ASSERT (s
!= NULL
);
696 /* If the symbol is currently defined in the .bss section of the
697 dynamic object, then it is OK to simply initialize it to zero.
698 If the symbol is in some other section, we must generate a
699 R_386_COPY reloc to tell the dynamic linker to copy the initial
700 value out of the dynamic object and into the runtime process
701 image. We need to remember the offset into the .rel.bss section
702 we are going to use. */
703 if ((h
->root
.u
.def
.section
->flags
& SEC_LOAD
) != 0)
707 srel
= bfd_get_section_by_name (dynobj
, ".rel.bss");
708 BFD_ASSERT (srel
!= NULL
);
709 srel
->_raw_size
+= sizeof (Elf32_External_Rel
);
710 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
713 /* We need to figure out the alignment required for this symbol. I
714 have no idea how ELF linkers handle this. */
715 power_of_two
= bfd_log2 (h
->size
);
716 if (power_of_two
> 3)
719 /* Apply the required alignment. */
720 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
,
721 (bfd_size_type
) (1 << power_of_two
));
722 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
724 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
728 /* Define the symbol as being at this point in the section. */
729 h
->root
.u
.def
.section
= s
;
730 h
->root
.u
.def
.value
= s
->_raw_size
;
732 /* Increment the section size to make room for the symbol. */
733 s
->_raw_size
+= h
->size
;
738 /* Set the sizes of the dynamic sections. */
741 elf_i386_size_dynamic_sections (output_bfd
, info
)
743 struct bfd_link_info
*info
;
751 dynobj
= elf_hash_table (info
)->dynobj
;
752 BFD_ASSERT (dynobj
!= NULL
);
754 /* Set the contents of the .interp section to the interpreter. */
757 s
= bfd_get_section_by_name (dynobj
, ".interp");
758 BFD_ASSERT (s
!= NULL
);
759 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
760 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
763 /* The check_relocs and adjust_dynamic_symbol entry points have
764 determined the sizes of the various dynamic sections. Allocate
769 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
774 if ((s
->flags
& SEC_IN_MEMORY
) == 0)
777 /* It's OK to base decisions on the section name, because none
778 of the dynobj section names depend upon the input files. */
779 name
= bfd_get_section_name (dynobj
, s
);
783 if (strcmp (name
, ".plt") == 0)
785 if (s
->_raw_size
== 0)
787 /* Strip this section if we don't need it; see the
793 /* Remember whether there is a PLT. */
797 else if (strncmp (name
, ".rel", 4) == 0)
799 if (s
->_raw_size
== 0)
801 /* If we don't need this section, strip it from the
802 output file. This is mostly to handle .rel.bss and
803 .rel.plt. We must create both sections in
804 create_dynamic_sections, because they must be created
805 before the linker maps input sections to output
806 sections. The linker does that before
807 adjust_dynamic_symbol is called, and it is that
808 function which decides whether anything needs to go
809 into these sections. */
816 /* Remember whether there are any reloc sections other
818 if (strcmp (name
, ".rel.plt") != 0)
821 /* If this relocation section applies to a read only
822 section, then we probably need a DT_TEXTREL entry. */
823 target
= bfd_get_section_by_name (output_bfd
, name
+ 4);
825 && (target
->flags
& SEC_READONLY
) != 0)
828 /* We use the reloc_count field as a counter if we need
829 to copy relocs into the output file. */
833 else if (strncmp (name
, ".got", 4) != 0)
835 /* It's not one of our sections, so don't allocate space. */
843 for (spp
= &s
->output_section
->owner
->sections
;
844 *spp
!= s
->output_section
;
847 *spp
= s
->output_section
->next
;
848 --s
->output_section
->owner
->section_count
;
853 /* Allocate memory for the section contents. */
854 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
855 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
857 bfd_set_error (bfd_error_no_memory
);
862 /* Add some entries to the .dynamic section. We fill in the values
863 later, in elf_i386_finish_dynamic_sections, but we must add the
864 entries now so that we get the correct size for the .dynamic
865 section. The DT_DEBUG entry is filled in by the dynamic linker
866 and used by the debugger. */
869 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
875 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0)
876 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
877 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTREL
, DT_REL
)
878 || ! bfd_elf32_add_dynamic_entry (info
, DT_JMPREL
, 0))
884 if (! bfd_elf32_add_dynamic_entry (info
, DT_REL
, 0)
885 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELSZ
, 0)
886 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELENT
,
887 sizeof (Elf32_External_Rel
)))
893 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
900 /* Relocate an i386 ELF section. */
903 elf_i386_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
904 contents
, relocs
, local_syms
, local_sections
)
906 struct bfd_link_info
*info
;
908 asection
*input_section
;
910 Elf_Internal_Rela
*relocs
;
911 Elf_Internal_Sym
*local_syms
;
912 asection
**local_sections
;
916 Elf_Internal_Shdr
*symtab_hdr
;
917 struct elf_link_hash_entry
**sym_hashes
;
918 bfd_vma
*local_got_offsets
;
922 Elf_Internal_Rela
*rel
;
923 Elf_Internal_Rela
*relend
;
925 dynobj
= elf_hash_table (info
)->dynobj
;
926 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
927 sym_hashes
= elf_sym_hashes (input_bfd
);
928 local_got_offsets
= elf_local_got_offsets (input_bfd
);
935 relend
= relocs
+ input_section
->reloc_count
;
936 for (; rel
< relend
; rel
++)
939 const reloc_howto_type
*howto
;
941 struct elf_link_hash_entry
*h
;
942 Elf_Internal_Sym
*sym
;
945 bfd_reloc_status_type r
;
947 r_type
= ELF32_R_TYPE (rel
->r_info
);
948 if (r_type
< 0 || r_type
>= (int) R_386_max
)
950 bfd_set_error (bfd_error_bad_value
);
953 howto
= elf_howto_table
+ r_type
;
955 r_symndx
= ELF32_R_SYM (rel
->r_info
);
957 if (info
->relocateable
)
959 /* This is a relocateable link. We don't have to change
960 anything, unless the reloc is against a section symbol,
961 in which case we have to adjust according to where the
962 section symbol winds up in the output section. */
963 if (r_symndx
< symtab_hdr
->sh_info
)
965 sym
= local_syms
+ r_symndx
;
966 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
970 sec
= local_sections
[r_symndx
];
971 val
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
972 val
+= sec
->output_offset
+ sym
->st_value
;
973 bfd_put_32 (input_bfd
, val
, contents
+ rel
->r_offset
);
980 /* This is a final link. */
984 if (r_symndx
< symtab_hdr
->sh_info
)
986 sym
= local_syms
+ r_symndx
;
987 sec
= local_sections
[r_symndx
];
988 relocation
= (sec
->output_section
->vma
994 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
995 if (h
->root
.type
== bfd_link_hash_defined
)
997 sec
= h
->root
.u
.def
.section
;
998 relocation
= (h
->root
.u
.def
.value
999 + sec
->output_section
->vma
1000 + sec
->output_offset
);
1002 else if (h
->root
.type
== bfd_link_hash_weak
)
1004 else if (info
->shared
)
1008 if (! ((*info
->callbacks
->undefined_symbol
)
1009 (info
, h
->root
.root
.string
, input_bfd
,
1010 input_section
, rel
->r_offset
)))
1019 /* Relocation is to the entry for this symbol in the global
1023 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1024 BFD_ASSERT (sgot
!= NULL
);
1029 BFD_ASSERT (h
->got_offset
!= (bfd_vma
) -1);
1030 relocation
= sgot
->output_offset
+ h
->got_offset
;
1036 BFD_ASSERT (local_got_offsets
!= NULL
1037 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1039 off
= local_got_offsets
[r_symndx
];
1041 /* The offset must always be a multiple of 4. We use
1042 the least significant bit to record whether we have
1043 already generated the necessary reloc. */
1049 Elf_Internal_Rel outrel
;
1051 bfd_put_32 (output_bfd
, relocation
, sgot
->contents
+ off
);
1053 srelgot
= bfd_get_section_by_name (dynobj
, ".rel.got");
1054 BFD_ASSERT (srelgot
!= NULL
);
1056 outrel
.r_offset
= (sgot
->output_section
->vma
1057 + sgot
->output_offset
1059 outrel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1060 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
1061 (((Elf32_External_Rel
*)
1063 + srelgot
->reloc_count
));
1064 ++srelgot
->reloc_count
;
1066 local_got_offsets
[r_symndx
] |= 1;
1069 relocation
= sgot
->output_offset
+ off
;
1075 /* Relocation is relative to the start of the global offset
1080 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1081 BFD_ASSERT (sgot
!= NULL
);
1084 /* Note that sgot->output_offset is not involved in this
1085 calculation. We always want the start of .got. If we
1086 defined _GLOBAL_OFFSET_TABLE in a different way, as is
1087 permitted by the ABI, we might have to change this
1089 relocation
-= sgot
->output_section
->vma
;
1094 /* Use global offset table as symbol value. */
1098 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1099 BFD_ASSERT (sgot
!= NULL
);
1102 relocation
= sgot
->output_section
->vma
;
1107 /* Relocation is to the entry for this symbol in the
1108 procedure linkage table. */
1110 /* Resolve a PLT32 reloc again a local symbol directly,
1111 without using the procedure linkage table. */
1117 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1118 BFD_ASSERT (splt
!= NULL
);
1121 BFD_ASSERT (h
!= NULL
&& h
->plt_offset
!= (bfd_vma
) -1);
1122 relocation
= (splt
->output_section
->vma
1123 + splt
->output_offset
1131 && (input_section
->flags
& SEC_ALLOC
) != 0)
1133 Elf_Internal_Rel outrel
;
1135 /* When generating a shared object, these relocations
1136 are copied into the output file to be resolved at run
1143 name
= (elf_string_from_elf_section
1145 elf_elfheader (input_bfd
)->e_shstrndx
,
1146 elf_section_data (input_section
)->rel_hdr
.sh_name
));
1150 BFD_ASSERT (strncmp (name
, ".rel", 4) == 0
1151 && strcmp (bfd_get_section_name (input_bfd
,
1155 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1156 BFD_ASSERT (sreloc
!= NULL
);
1159 outrel
.r_offset
= (rel
->r_offset
1160 + input_section
->output_section
->vma
1161 + input_section
->output_offset
);
1162 if (r_type
== R_386_PC32
)
1164 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= (bfd_vma
) -1);
1165 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_PC32
);
1170 outrel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
1173 BFD_ASSERT (h
->dynindx
!= (bfd_vma
) -1);
1174 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_32
);
1178 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
,
1179 (((Elf32_External_Rel
*)
1181 + sreloc
->reloc_count
));
1182 ++sreloc
->reloc_count
;
1184 /* If this reloc is against an external symbol, we do
1185 not want to fiddle with the addend. Otherwise, we
1186 need to include the symbol value so that it becomes
1187 an addend for the dynamic reloc. */
1198 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1199 contents
, rel
->r_offset
,
1200 relocation
, (bfd_vma
) 0);
1202 if (r
!= bfd_reloc_ok
)
1207 case bfd_reloc_outofrange
:
1209 case bfd_reloc_overflow
:
1214 name
= h
->root
.root
.string
;
1217 name
= elf_string_from_elf_section (input_bfd
,
1218 symtab_hdr
->sh_link
,
1223 name
= bfd_section_name (input_bfd
, sec
);
1225 if (! ((*info
->callbacks
->reloc_overflow
)
1226 (info
, name
, howto
->name
, (bfd_vma
) 0,
1227 input_bfd
, input_section
, rel
->r_offset
)))
1238 /* Finish up dynamic symbol handling. We set the contents of various
1239 dynamic sections here. */
1242 elf_i386_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
1244 struct bfd_link_info
*info
;
1245 struct elf_link_hash_entry
*h
;
1246 Elf_Internal_Sym
*sym
;
1250 dynobj
= elf_hash_table (info
)->dynobj
;
1252 if (h
->plt_offset
!= (bfd_vma
) -1)
1259 Elf_Internal_Rel rel
;
1261 /* This symbol has an entry in the procedure linkage table. Set
1264 BFD_ASSERT (h
->dynindx
!= -1);
1266 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1267 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
1268 srel
= bfd_get_section_by_name (dynobj
, ".rel.plt");
1269 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
1271 /* Get the index in the procedure linkage table which
1272 corresponds to this symbol. This is the index of this symbol
1273 in all the symbols for which we are making plt entries. The
1274 first entry in the procedure linkage table is reserved. */
1275 plt_index
= h
->plt_offset
/ PLT_ENTRY_SIZE
- 1;
1277 /* Get the offset into the .got table of the entry that
1278 corresponds to this function. Each .got entry is 4 bytes.
1279 The first three are reserved. */
1280 got_offset
= (plt_index
+ 3) * 4;
1282 /* Fill in the entry in the procedure linkage table. */
1285 memcpy (splt
->contents
+ h
->plt_offset
, elf_i386_plt_entry
,
1287 bfd_put_32 (output_bfd
,
1288 (sgot
->output_section
->vma
1289 + sgot
->output_offset
1291 splt
->contents
+ h
->plt_offset
+ 2);
1295 memcpy (splt
->contents
+ h
->plt_offset
, elf_i386_pic_plt_entry
,
1297 bfd_put_32 (output_bfd
, got_offset
,
1298 splt
->contents
+ h
->plt_offset
+ 2);
1301 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rel
),
1302 splt
->contents
+ h
->plt_offset
+ 7);
1303 bfd_put_32 (output_bfd
, - (h
->plt_offset
+ PLT_ENTRY_SIZE
),
1304 splt
->contents
+ h
->plt_offset
+ 12);
1306 /* Fill in the entry in the global offset table. */
1307 bfd_put_32 (output_bfd
,
1308 (splt
->output_section
->vma
1309 + splt
->output_offset
1312 sgot
->contents
+ got_offset
);
1314 /* Fill in the entry in the .rel.plt section. */
1315 rel
.r_offset
= (sgot
->output_section
->vma
1316 + sgot
->output_offset
1318 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_JUMP_SLOT
);
1319 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1320 ((Elf32_External_Rel
*) srel
->contents
1323 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1325 /* Mark the symbol as undefined, rather than as defined in
1326 the .plt section. Leave the value alone. */
1327 sym
->st_shndx
= SHN_UNDEF
;
1331 if (h
->got_offset
!= (bfd_vma
) -1)
1335 Elf_Internal_Rel rel
;
1337 /* This symbol has an entry in the global offset table. Set it
1340 BFD_ASSERT (h
->dynindx
!= -1);
1342 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1343 srel
= bfd_get_section_by_name (dynobj
, ".rel.got");
1344 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
1346 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got_offset
);
1348 rel
.r_offset
= (sgot
->output_section
->vma
1349 + sgot
->output_offset
1351 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_GLOB_DAT
);
1352 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1353 ((Elf32_External_Rel
*) srel
->contents
1354 + srel
->reloc_count
));
1355 ++srel
->reloc_count
;
1358 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
1361 Elf_Internal_Rel rel
;
1363 /* This symbol needs a copy reloc. Set it up. */
1365 BFD_ASSERT (h
->dynindx
!= -1
1366 && h
->root
.type
== bfd_link_hash_defined
);
1368 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
1370 BFD_ASSERT (s
!= NULL
);
1372 rel
.r_offset
= (h
->root
.u
.def
.value
1373 + h
->root
.u
.def
.section
->output_section
->vma
1374 + h
->root
.u
.def
.section
->output_offset
);
1375 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_COPY
);
1376 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
1377 ((Elf32_External_Rel
*) s
->contents
1382 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1383 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
1384 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
1385 sym
->st_shndx
= SHN_ABS
;
1390 /* Finish up the dynamic sections. */
1393 elf_i386_finish_dynamic_sections (output_bfd
, info
)
1395 struct bfd_link_info
*info
;
1401 Elf32_External_Dyn
*dyncon
, *dynconend
;
1403 dynobj
= elf_hash_table (info
)->dynobj
;
1405 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1406 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
1407 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
1408 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& sdyn
!= NULL
);
1410 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
1411 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
1412 for (; dyncon
< dynconend
; dyncon
++)
1414 Elf_Internal_Dyn dyn
;
1418 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
1431 s
= bfd_get_section_by_name (output_bfd
, name
);
1432 BFD_ASSERT (s
!= NULL
);
1433 dyn
.d_un
.d_ptr
= s
->vma
;
1434 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1438 s
= bfd_get_section_by_name (output_bfd
, ".rel.plt");
1439 BFD_ASSERT (s
!= NULL
);
1440 if (s
->_cooked_size
!= 0)
1441 dyn
.d_un
.d_val
= s
->_cooked_size
;
1443 dyn
.d_un
.d_val
= s
->_raw_size
;
1444 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1448 /* My reading of the SVR4 ABI indicates that the procedure
1449 linkage table relocs (DT_JMPREL) should be included in
1450 the overall relocs (DT_REL). This is what Solaris does.
1451 However, UnixWare can not handle that case. Therefore,
1452 we override the DT_RELSZ entry here to make it not
1453 include the JMPREL relocs. Since the linker script
1454 arranges for .rel.plt to follow all other relocation
1455 sections, we don't have to worry about changing the
1457 s
= bfd_get_section_by_name (output_bfd
, ".rel.plt");
1460 if (s
->_cooked_size
!= 0)
1461 dyn
.d_un
.d_val
-= s
->_cooked_size
;
1463 dyn
.d_un
.d_val
-= s
->_raw_size
;
1465 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1470 /* Fill in the first entry in the procedure linkage table. */
1471 if (splt
->_raw_size
> 0)
1474 memcpy (splt
->contents
, elf_i386_pic_plt0_entry
, PLT_ENTRY_SIZE
);
1477 memcpy (splt
->contents
, elf_i386_plt0_entry
, PLT_ENTRY_SIZE
);
1478 bfd_put_32 (output_bfd
,
1479 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
1480 splt
->contents
+ 2);
1481 bfd_put_32 (output_bfd
,
1482 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
1483 splt
->contents
+ 8);
1487 /* Fill in the first three entries in the global offset table. */
1488 if (sgot
->_raw_size
> 0)
1490 bfd_put_32 (output_bfd
,
1491 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
1493 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
1494 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
1497 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
1499 /* UnixWare sets the entsize of .plt to 4, although that doesn't
1500 really seem like the right value. */
1501 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
1506 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
1507 #define TARGET_LITTLE_NAME "elf32-i386"
1508 #define ELF_ARCH bfd_arch_i386
1509 #define ELF_MACHINE_CODE EM_386
1510 #define elf_info_to_howto elf_i386_info_to_howto
1511 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
1512 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
1513 #define ELF_MAXPAGESIZE 0x1000
1514 #define elf_backend_create_dynamic_sections \
1515 elf_i386_create_dynamic_sections
1516 #define elf_backend_check_relocs elf_i386_check_relocs
1517 #define elf_backend_adjust_dynamic_symbol \
1518 elf_i386_adjust_dynamic_symbol
1519 #define elf_backend_size_dynamic_sections \
1520 elf_i386_size_dynamic_sections
1521 #define elf_backend_relocate_section elf_i386_relocate_section
1522 #define elf_backend_finish_dynamic_symbol \
1523 elf_i386_finish_dynamic_symbol
1524 #define elf_backend_finish_dynamic_sections \
1525 elf_i386_finish_dynamic_sections
1527 #include "elf32-target.h"