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_adjust_dynamic_symbol
35 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
36 static boolean elf_i386_allocate_dynamic_section
37 PARAMS ((bfd
*, const char *));
38 static boolean elf_i386_size_dynamic_sections
39 PARAMS ((bfd
*, struct bfd_link_info
*));
40 static boolean elf_i386_relocate_section
41 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
42 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**, char *));
43 static boolean elf_i386_finish_dynamic_symbol
44 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
46 static boolean elf_i386_finish_dynamic_sections
47 PARAMS ((bfd
*, struct bfd_link_info
*));
49 #define USE_REL 1 /* 386 uses REL relocations instead of RELA */
68 static CONST
char *CONST reloc_type_names
[] =
84 static reloc_howto_type elf_howto_table
[]=
86 HOWTO(R_386_NONE
, 0,0, 0,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_NONE", true,0x00000000,0x00000000,false),
87 HOWTO(R_386_32
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_32", true,0xffffffff,0xffffffff,false),
88 HOWTO(R_386_PC32
, 0,2,32,true, 0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_PC32", true,0xffffffff,0xffffffff,true),
89 HOWTO(R_386_GOT32
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_GOT32", true,0xffffffff,0xffffffff,false),
90 HOWTO(R_386_PLT32
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_PLT32", true,0xffffffff,0xffffffff,false),
91 HOWTO(R_386_COPY
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_COPY", true,0xffffffff,0xffffffff,false),
92 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),
93 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),
94 HOWTO(R_386_RELATIVE
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false),
95 HOWTO(R_386_GOTOFF
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_GOTOFF", true,0xffffffff,0xffffffff,false),
96 HOWTO(R_386_GOTPC
, 0,2,32,false,0,complain_overflow_bitfield
, bfd_elf_generic_reloc
,"R_386_GOTPC", true,0xffffffff,0xffffffff,false),
99 #ifdef DEBUG_GEN_RELOC
100 #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
105 static CONST
struct reloc_howto_struct
*
106 elf_i386_reloc_type_lookup (abfd
, code
)
108 bfd_reloc_code_real_type code
;
113 TRACE ("BFD_RELOC_NONE");
114 return &elf_howto_table
[ (int)R_386_NONE
];
117 TRACE ("BFD_RELOC_32");
118 return &elf_howto_table
[ (int)R_386_32
];
120 case BFD_RELOC_32_PCREL
:
121 TRACE ("BFD_RELOC_PC32");
122 return &elf_howto_table
[ (int)R_386_PC32
];
124 case BFD_RELOC_386_GOT32
:
125 TRACE ("BFD_RELOC_386_GOT32");
126 return &elf_howto_table
[ (int)R_386_GOT32
];
128 case BFD_RELOC_386_PLT32
:
129 TRACE ("BFD_RELOC_386_PLT32");
130 return &elf_howto_table
[ (int)R_386_PLT32
];
132 case BFD_RELOC_386_COPY
:
133 TRACE ("BFD_RELOC_386_COPY");
134 return &elf_howto_table
[ (int)R_386_COPY
];
136 case BFD_RELOC_386_GLOB_DAT
:
137 TRACE ("BFD_RELOC_386_GLOB_DAT");
138 return &elf_howto_table
[ (int)R_386_GLOB_DAT
];
140 case BFD_RELOC_386_JUMP_SLOT
:
141 TRACE ("BFD_RELOC_386_JUMP_SLOT");
142 return &elf_howto_table
[ (int)R_386_JUMP_SLOT
];
144 case BFD_RELOC_386_RELATIVE
:
145 TRACE ("BFD_RELOC_386_RELATIVE");
146 return &elf_howto_table
[ (int)R_386_RELATIVE
];
148 case BFD_RELOC_386_GOTOFF
:
149 TRACE ("BFD_RELOC_386_GOTOFF");
150 return &elf_howto_table
[ (int)R_386_GOTOFF
];
152 case BFD_RELOC_386_GOTPC
:
153 TRACE ("BFD_RELOC_386_GOTPC");
154 return &elf_howto_table
[ (int)R_386_GOTPC
];
165 elf_i386_info_to_howto (abfd
, cache_ptr
, dst
)
168 Elf32_Internal_Rela
*dst
;
170 BFD_ASSERT (ELF32_R_TYPE(dst
->r_info
) < (unsigned int) R_386_max
);
172 cache_ptr
->howto
= &elf_howto_table
[ELF32_R_TYPE(dst
->r_info
)];
176 elf_i386_info_to_howto_rel (abfd
, cache_ptr
, dst
)
179 Elf32_Internal_Rel
*dst
;
181 BFD_ASSERT (ELF32_R_TYPE(dst
->r_info
) < (unsigned int) R_386_max
);
183 cache_ptr
->howto
= &elf_howto_table
[ELF32_R_TYPE(dst
->r_info
)];
186 /* Functions for the i386 ELF linker. */
188 /* The name of the dynamic interpreter. This is put in the .interp
191 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
193 /* The size in bytes of an entry in the procedure linkage table. */
195 #define PLT_ENTRY_SIZE 16
197 /* The first entry in an absolute procedure linkage table looks like
198 this. See the SVR4 ABI i386 supplement to see how this works. */
200 static bfd_byte elf_i386_plt0_entry
[PLT_ENTRY_SIZE
] =
202 0xff, 0x35, /* pushl contents of address */
203 0, 0, 0, 0, /* replaced with address of .got + 4. */
204 0xff, 0x25, /* jmp indirect */
205 0, 0, 0, 0, /* replaced with address of .got + 8. */
206 0, 0, 0, 0 /* pad out to 16 bytes. */
209 /* Subsequent entries in an absolute procedure linkage table look like
212 static bfd_byte elf_i386_plt_entry
[PLT_ENTRY_SIZE
] =
214 0xff, 0x25, /* jmp indirect */
215 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
216 0x68, /* pushl immediate */
217 0, 0, 0, 0, /* replaced with offset into relocation table. */
218 0xe9, /* jmp relative */
219 0, 0, 0, 0 /* replaced with offset to start of .plt. */
222 /* Create dynamic sections when linking against a dynamic object. */
225 elf_i386_create_dynamic_sections (abfd
, info
)
227 struct bfd_link_info
*info
;
230 register asection
*s
;
231 struct elf_link_hash_entry
*h
;
233 /* We need to create .plt, .rel.plt, .got, .dynbss, and .rel.bss
236 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
238 s
= bfd_make_section (abfd
, ".plt");
240 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
| SEC_CODE
)
241 || ! bfd_set_section_alignment (abfd
, s
, 2))
244 s
= bfd_make_section (abfd
, ".rel.plt");
246 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
247 || ! bfd_set_section_alignment (abfd
, s
, 2))
250 s
= bfd_make_section (abfd
, ".got");
252 || ! bfd_set_section_flags (abfd
, s
, flags
)
253 || ! bfd_set_section_alignment (abfd
, s
, 2))
256 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
257 section. We don't do this in the linker script because we don't
258 want to define the symbol if we are not creating a global offset
261 if (! (_bfd_generic_link_add_one_symbol
262 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
, (bfd_vma
) 0,
263 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
264 (struct bfd_link_hash_entry
**) &h
)))
266 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
268 /* The first three global offset table entries are reserved. */
269 s
->_raw_size
+= 3 * 4;
271 /* The .dynbss section is a place to put symbols which are defined
272 by dynamic objects, are referenced by regular objects, and are
273 not functions. We must allocate space for them in the process
274 image and use a R_386_COPY reloc to tell the dynamic linker to
275 initialize them at run time. The linker script puts the .dynbss
276 section into the .bss section of the final image. */
277 s
= bfd_make_section (abfd
, ".dynbss");
279 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
282 /* The .rel.bss section holds copy relocs. This section is not
283 normally needed. We need to create it here, though, so that the
284 linker will map it to an output section. If it turns out not to
285 be needed, we can discard it later. */
286 s
= bfd_make_section (abfd
, ".rel.bss");
288 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
289 || ! bfd_set_section_alignment (abfd
, s
, 2))
295 /* Adjust a symbol defined by a dynamic object and referenced by a
296 regular object. The current definition is in some section of the
297 dynamic object, but we're not including those sections. We have to
298 change the definition to something the rest of the link can
302 elf_i386_adjust_dynamic_symbol (info
, h
)
303 struct bfd_link_info
*info
;
304 struct elf_link_hash_entry
*h
;
308 unsigned int power_of_two
;
310 dynobj
= elf_hash_table (info
)->dynobj
;
312 /* Make sure we know what is going on here. */
313 BFD_ASSERT (dynobj
!= NULL
314 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
315 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) != 0
316 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0
317 && h
->root
.type
== bfd_link_hash_defined
318 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
319 == bfd_target_elf_flavour
)
320 && (elf_elfheader (h
->root
.u
.def
.section
->owner
)->e_type
322 && h
->root
.u
.def
.section
->output_section
== NULL
);
324 /* If this is a function, put it in the procedure linkage table. We
325 will fill in the contents of the procedure linkage table later,
326 when we know the address of the .got section. */
327 if (h
->type
== STT_FUNC
)
329 s
= bfd_get_section_by_name (dynobj
, ".plt");
330 BFD_ASSERT (s
!= NULL
);
332 /* If this is the first .plt entry, make room for the special
334 if (s
->_raw_size
== 0)
335 s
->_raw_size
+= PLT_ENTRY_SIZE
;
337 /* Set the symbol to this location in the .plt. */
338 h
->root
.u
.def
.section
= s
;
339 h
->root
.u
.def
.value
= s
->_raw_size
;
341 /* Make room for this entry. */
342 s
->_raw_size
+= PLT_ENTRY_SIZE
;
344 /* We also need to make an entry in the .got section. */
346 s
= bfd_get_section_by_name (dynobj
, ".got");
347 BFD_ASSERT (s
!= NULL
);
350 /* We also need to make an entry in the .rel.plt section. */
352 s
= bfd_get_section_by_name (dynobj
, ".rel.plt");
353 BFD_ASSERT (s
!= NULL
);
354 s
->_raw_size
+= sizeof (Elf32_External_Rel
);
359 /* If this is a weak symbol, and there is a real definition, the
360 processor independent code will have arranged for us to see the
361 real definition first, and we can just use the same value. */
362 if (h
->weakdef
!= NULL
)
364 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
);
365 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
366 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
367 h
->copy_offset
= (bfd_vma
) -1;
371 /* This is a reference to a symbol defined by a dynamic object which
372 is not a function. We must allocate it in our .dynbss section,
373 which will become part of the .bss section of the executable.
374 There will be an entry for this symbol in the .dynsym section.
375 The dynamic object will contain position independent code, so all
376 references from the dynamic object to this symbol will go through
377 the global offset table. The dynamic linker will use the .dynsym
378 entry to determine the address it must put in the global offset
379 table, so both the dynamic object and the regular object will
380 refer to the same memory location for the variable. */
382 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
383 BFD_ASSERT (s
!= NULL
);
385 /* If the symbol is currently defined in the .bss section of the
386 dynamic object, then it is OK to simply initialize it to zero.
387 If the symbol is in some other section, we must generate a
388 R_386_COPY reloc to tell the dynamic linker to copy the initial
389 value out of the dynamic object and into the runtime process
390 image. We need to remember the offset into the .rel.bss section
391 we are going to use. */
392 if ((h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0)
393 h
->copy_offset
= (bfd_vma
) -1;
398 srel
= bfd_get_section_by_name (dynobj
, ".rel.bss");
399 BFD_ASSERT (srel
!= NULL
);
400 h
->copy_offset
= srel
->_raw_size
;
401 srel
->_raw_size
+= sizeof (Elf32_External_Rel
);
404 /* We need to figure out the alignment required for this symbol. I
405 have no idea how ELF linkers handle this. */
406 power_of_two
= bfd_log2 (h
->size
);
407 if (power_of_two
> 3)
410 /* Apply the required alignment. */
411 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
,
412 (bfd_size_type
) (1 << power_of_two
));
413 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
415 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
419 /* Define the symbol as being at this point in the section. */
420 h
->root
.u
.def
.section
= s
;
421 h
->root
.u
.def
.value
= s
->_raw_size
;
423 /* Increment the section size to make room for the symbol. */
424 s
->_raw_size
+= h
->size
;
429 /* Allocate contents for a section. */
431 static INLINE boolean
432 elf_i386_allocate_dynamic_section (dynobj
, name
)
436 register asection
*s
;
438 s
= bfd_get_section_by_name (dynobj
, name
);
439 BFD_ASSERT (s
!= NULL
);
440 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
441 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
443 bfd_set_error (bfd_error_no_memory
);
449 /* Set the sizes of the dynamic sections. */
452 elf_i386_size_dynamic_sections (output_bfd
, info
)
454 struct bfd_link_info
*info
;
459 dynobj
= elf_hash_table (info
)->dynobj
;
460 BFD_ASSERT (dynobj
!= NULL
);
462 /* Set the contents of the .interp section to the interpreter. */
465 s
= bfd_get_section_by_name (dynobj
, ".interp");
466 BFD_ASSERT (s
!= NULL
);
467 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
468 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
471 /* The adjust_dynamic_symbol entry point has determined the sizes of
472 the various dynamic sections. Allocate some memory for them to
474 if (! elf_i386_allocate_dynamic_section (dynobj
, ".plt")
475 || ! elf_i386_allocate_dynamic_section (dynobj
, ".rel.plt")
476 || ! elf_i386_allocate_dynamic_section (dynobj
, ".got")
477 || ! elf_i386_allocate_dynamic_section (dynobj
, ".rel.bss"))
480 /* Add some entries to the .dynamic section. We fill in the values
481 later, in elf_i386_finish_dynamic_sections, but we must add the
482 entries now so that we get the correct size for the .dynamic
483 section. The DT_DEBUG entry is filled in by the dynamic linker
484 and used by the debugger. */
485 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0)
486 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0))
489 s
= bfd_get_section_by_name (dynobj
, ".plt");
490 BFD_ASSERT (s
!= NULL
);
491 if (s
->_raw_size
!= 0)
493 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
494 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTREL
, DT_REL
)
495 || ! bfd_elf32_add_dynamic_entry (info
, DT_JMPREL
, 0))
499 /* If we didn't need the .rel.bss section, then discard it from the
500 output file. This is a hack. We don't bother to do it for the
501 other sections because they normally are needed. */
502 s
= bfd_get_section_by_name (dynobj
, ".rel.bss");
503 BFD_ASSERT (s
!= NULL
);
504 if (s
->_raw_size
== 0)
508 for (spp
= &s
->output_section
->owner
->sections
;
509 *spp
!= s
->output_section
;
512 *spp
= s
->output_section
->next
;
513 --s
->output_section
->owner
->section_count
;
517 if (! bfd_elf32_add_dynamic_entry (info
, DT_REL
, 0)
518 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELSZ
, 0)
519 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELENT
,
520 sizeof (Elf32_External_Rel
)))
527 /* Relocate an i386 ELF section. */
530 elf_i386_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
531 contents
, relocs
, local_syms
, local_sections
,
534 struct bfd_link_info
*info
;
536 asection
*input_section
;
538 Elf_Internal_Rela
*relocs
;
539 Elf_Internal_Sym
*local_syms
;
540 asection
**local_sections
;
543 Elf_Internal_Shdr
*symtab_hdr
;
544 Elf_Internal_Rela
*rel
;
545 Elf_Internal_Rela
*relend
;
547 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
550 relend
= relocs
+ input_section
->reloc_count
;
551 for (; rel
< relend
; rel
++)
554 const reloc_howto_type
*howto
;
556 struct elf_link_hash_entry
*h
;
557 Elf_Internal_Sym
*sym
;
560 bfd_reloc_status_type r
;
562 r_type
= ELF32_R_TYPE (rel
->r_info
);
563 if (r_type
< 0 || r_type
>= (int) R_386_max
)
565 bfd_set_error (bfd_error_bad_value
);
568 howto
= elf_howto_table
+ r_type
;
570 r_symndx
= ELF32_R_SYM (rel
->r_info
);
572 if (info
->relocateable
)
574 /* This is a relocateable link. We don't have to change
575 anything, unless the reloc is against a section symbol,
576 in which case we have to adjust according to where the
577 section symbol winds up in the output section. */
578 if (r_symndx
< symtab_hdr
->sh_info
)
580 sym
= local_syms
+ r_symndx
;
581 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
585 sec
= local_sections
[r_symndx
];
586 val
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
587 val
+= sec
->output_offset
+ sym
->st_value
;
588 bfd_put_32 (input_bfd
, val
, contents
+ rel
->r_offset
);
595 /* This is a final link. */
599 if (r_symndx
< symtab_hdr
->sh_info
)
601 sym
= local_syms
+ r_symndx
;
602 sec
= local_sections
[r_symndx
];
603 relocation
= (sec
->output_section
->vma
611 indx
= r_symndx
- symtab_hdr
->sh_info
;
612 h
= elf_sym_hashes (input_bfd
)[indx
];
613 if (h
->root
.type
== bfd_link_hash_defined
)
615 sec
= h
->root
.u
.def
.section
;
616 relocation
= (h
->root
.u
.def
.value
617 + sec
->output_section
->vma
618 + sec
->output_offset
);
620 else if (h
->root
.type
== bfd_link_hash_weak
)
624 if (! ((*info
->callbacks
->undefined_symbol
)
625 (info
, h
->root
.root
.string
, input_bfd
,
626 input_section
, rel
->r_offset
)))
632 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
633 contents
, rel
->r_offset
,
634 relocation
, (bfd_vma
) 0);
636 if (r
!= bfd_reloc_ok
)
641 case bfd_reloc_outofrange
:
643 case bfd_reloc_overflow
:
648 name
= h
->root
.root
.string
;
651 name
= output_names
+ sym
->st_name
;
655 name
= bfd_section_name (input_bfd
, sec
);
657 if (! ((*info
->callbacks
->reloc_overflow
)
658 (info
, name
, howto
->name
, (bfd_vma
) 0,
659 input_bfd
, input_section
, rel
->r_offset
)))
670 /* Finish up dynamic symbol handling. We set the contents of various
671 dynamic sections here. */
674 elf_i386_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
676 struct bfd_link_info
*info
;
677 struct elf_link_hash_entry
*h
;
678 Elf_Internal_Sym
*sym
;
680 /* If this symbol is not defined by a dynamic object, or is not
681 referenced by a regular object, ignore it. */
682 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
683 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
684 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR
) == 0)
686 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
687 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
688 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
689 sym
->st_shndx
= SHN_ABS
;
693 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
);
694 BFD_ASSERT (h
->dynindx
!= -1);
696 if (h
->type
== STT_FUNC
)
703 Elf_Internal_Rel rel
;
705 splt
= h
->root
.u
.def
.section
;
706 BFD_ASSERT (strcmp (bfd_get_section_name (splt
->owner
, splt
), ".plt")
708 sgot
= bfd_get_section_by_name (splt
->owner
, ".got");
709 BFD_ASSERT (sgot
!= NULL
);
710 srel
= bfd_get_section_by_name (splt
->owner
, ".rel.plt");
711 BFD_ASSERT (srel
!= NULL
);
713 /* FIXME: This only handles an absolute procedure linkage table.
714 When producing a dynamic object, we need to generate a
715 position independent procedure linkage table. */
717 /* Get the index in the procedure linkage table which
718 corresponds to this symbol. This is the index of this symbol
719 in all the symbols for which we are making plt entries. The
720 first entry in the procedure linkage table is reserved. */
721 plt_index
= h
->root
.u
.def
.value
/ PLT_ENTRY_SIZE
- 1;
723 /* Get the offset into the .got table of the entry that
724 corresponds to this function. Each .got entry is 4 bytes.
725 The first three are reserved. */
726 got_offset
= (plt_index
+ 3) * 4;
728 /* Fill in the entry in the procedure linkage table. */
729 memcpy (splt
->contents
+ h
->root
.u
.def
.value
, elf_i386_plt_entry
,
731 bfd_put_32 (output_bfd
,
732 (sgot
->output_section
->vma
733 + sgot
->output_offset
735 splt
->contents
+ h
->root
.u
.def
.value
+ 2);
736 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rel
),
737 splt
->contents
+ h
->root
.u
.def
.value
+ 7);
738 bfd_put_32 (output_bfd
, - (h
->root
.u
.def
.value
+ PLT_ENTRY_SIZE
),
739 splt
->contents
+ h
->root
.u
.def
.value
+ 12);
741 /* Fill in the entry in the global offset table. */
742 bfd_put_32 (output_bfd
,
743 (splt
->output_section
->vma
744 + splt
->output_offset
745 + h
->root
.u
.def
.value
747 sgot
->contents
+ got_offset
);
749 /* Fill in the entry in the .rel.plt section. */
750 rel
.r_offset
= (sgot
->output_section
->vma
751 + sgot
->output_offset
753 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_JUMP_SLOT
);
754 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
755 ((Elf32_External_Rel
*) srel
->contents
758 /* Mark the symbol as undefined, rather than as defined in the
759 .plt section. Leave the value alone. */
760 sym
->st_shndx
= SHN_UNDEF
;
764 /* This is not a function. We have already allocated memory for
765 it in the .bss section (via .dynbss). All we have to do here
766 is create a COPY reloc if required. */
767 if (h
->copy_offset
!= (bfd_vma
) -1)
770 Elf_Internal_Rel rel
;
772 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
774 BFD_ASSERT (s
!= NULL
);
776 rel
.r_offset
= (h
->root
.u
.def
.value
777 + h
->root
.u
.def
.section
->output_section
->vma
778 + h
->root
.u
.def
.section
->output_offset
);
779 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_386_COPY
);
780 bfd_elf32_swap_reloc_out (output_bfd
, &rel
,
781 ((Elf32_External_Rel
*)
782 (s
->contents
+ h
->copy_offset
)));
789 /* Finish up the dynamic sections. */
792 elf_i386_finish_dynamic_sections (output_bfd
, info
)
794 struct bfd_link_info
*info
;
799 Elf32_External_Dyn
*dyncon
, *dynconend
;
801 splt
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
, ".plt");
802 sgot
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
, ".got");
803 sdyn
= bfd_get_section_by_name (elf_hash_table (info
)->dynobj
, ".dynamic");
804 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& sdyn
!= NULL
);
806 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
807 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
808 for (; dyncon
< dynconend
; dyncon
++)
810 Elf_Internal_Dyn dyn
;
814 bfd_elf32_swap_dyn_in (elf_hash_table (info
)->dynobj
, dyncon
, &dyn
);
816 /* My reading of the SVR4 ABI indicates that the procedure
817 linkage table relocs (DT_JMPREL) should be included in the
818 overall relocs (DT_REL). This is what Solaris does.
819 However, UnixWare can not handle that case. Therefore, we
820 override the DT_REL and DT_RELSZ entries here to make them
821 not include the JMPREL relocs. */
825 case DT_PLTGOT
: name
= ".got"; size
= false; break;
826 case DT_PLTRELSZ
: name
= ".rel.plt"; size
= true; break;
827 case DT_JMPREL
: name
= ".rel.plt"; size
= false; break;
828 case DT_REL
: name
= ".rel.bss"; size
= false; break;
829 case DT_RELSZ
: name
= ".rel.bss"; size
= true; break;
830 default: name
= NULL
; size
= false; break;
837 s
= bfd_get_section_by_name (output_bfd
, name
);
838 BFD_ASSERT (s
!= NULL
);
840 dyn
.d_un
.d_ptr
= s
->vma
;
843 if (s
->_cooked_size
!= 0)
844 dyn
.d_un
.d_val
= s
->_cooked_size
;
846 dyn
.d_un
.d_val
= s
->_raw_size
;
848 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
852 /* Fill in the first entry in the procedure linkage table. */
853 if (splt
->_raw_size
> 0)
855 memcpy (splt
->contents
, elf_i386_plt0_entry
, PLT_ENTRY_SIZE
);
856 bfd_put_32 (output_bfd
,
857 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
859 bfd_put_32 (output_bfd
,
860 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
864 /* Fill in the first three entries in the global offset table. */
865 if (sgot
->_raw_size
> 0)
867 bfd_put_32 (output_bfd
,
868 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
870 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
871 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
874 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
876 /* UnixWare sets the entsize of .plt to 4, although that doesn't
877 really seem like the right value. */
878 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
883 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
884 #define TARGET_LITTLE_NAME "elf32-i386"
885 #define ELF_ARCH bfd_arch_i386
886 #define ELF_MACHINE_CODE EM_386
887 #define elf_info_to_howto elf_i386_info_to_howto
888 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
889 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
890 #define ELF_MAXPAGESIZE 0x1000
891 #define elf_backend_create_dynamic_sections \
892 elf_i386_create_dynamic_sections
893 #define elf_backend_adjust_dynamic_symbol \
894 elf_i386_adjust_dynamic_symbol
895 #define elf_backend_size_dynamic_sections \
896 elf_i386_size_dynamic_sections
897 #define elf_backend_relocate_section elf_i386_relocate_section
898 #define elf_backend_finish_dynamic_symbol \
899 elf_i386_finish_dynamic_symbol
900 #define elf_backend_finish_dynamic_sections \
901 elf_i386_finish_dynamic_sections
903 #include "elf32-target.h"