* scripttempl/alpha.sc: Define _ftext, _etext and _fdata insted of
[binutils-gdb.git] / bfd / elf32-i386.c
1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright 1993 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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. */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "bfdlink.h"
23 #include "libbfd.h"
24 #include "libelf.h"
25
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 **));
43 static boolean elf_i386_finish_dynamic_symbol
44 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
45 Elf_Internal_Sym *));
46 static boolean elf_i386_finish_dynamic_sections
47 PARAMS ((bfd *, struct bfd_link_info *));
48
49 #define USE_REL 1 /* 386 uses REL relocations instead of RELA */
50
51 enum reloc_type
52 {
53 R_386_NONE = 0,
54 R_386_32,
55 R_386_PC32,
56 R_386_GOT32,
57 R_386_PLT32,
58 R_386_COPY,
59 R_386_GLOB_DAT,
60 R_386_JUMP_SLOT,
61 R_386_RELATIVE,
62 R_386_GOTOFF,
63 R_386_GOTPC,
64 R_386_max
65 };
66
67 #if 0
68 static CONST char *CONST reloc_type_names[] =
69 {
70 "R_386_NONE",
71 "R_386_32",
72 "R_386_PC32",
73 "R_386_GOT32",
74 "R_386_PLT32",
75 "R_386_COPY",
76 "R_386_GLOB_DAT",
77 "R_386_JUMP_SLOT",
78 "R_386_RELATIVE",
79 "R_386_GOTOFF",
80 "R_386_GOTPC",
81 };
82 #endif
83
84 static reloc_howto_type elf_howto_table[]=
85 {
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),
97 };
98
99 #ifdef DEBUG_GEN_RELOC
100 #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
101 #else
102 #define TRACE(str)
103 #endif
104
105 static CONST struct reloc_howto_struct *
106 elf_i386_reloc_type_lookup (abfd, code)
107 bfd *abfd;
108 bfd_reloc_code_real_type code;
109 {
110 switch (code)
111 {
112 case BFD_RELOC_NONE:
113 TRACE ("BFD_RELOC_NONE");
114 return &elf_howto_table[ (int)R_386_NONE ];
115
116 case BFD_RELOC_32:
117 TRACE ("BFD_RELOC_32");
118 return &elf_howto_table[ (int)R_386_32 ];
119
120 case BFD_RELOC_32_PCREL:
121 TRACE ("BFD_RELOC_PC32");
122 return &elf_howto_table[ (int)R_386_PC32 ];
123
124 case BFD_RELOC_386_GOT32:
125 TRACE ("BFD_RELOC_386_GOT32");
126 return &elf_howto_table[ (int)R_386_GOT32 ];
127
128 case BFD_RELOC_386_PLT32:
129 TRACE ("BFD_RELOC_386_PLT32");
130 return &elf_howto_table[ (int)R_386_PLT32 ];
131
132 case BFD_RELOC_386_COPY:
133 TRACE ("BFD_RELOC_386_COPY");
134 return &elf_howto_table[ (int)R_386_COPY ];
135
136 case BFD_RELOC_386_GLOB_DAT:
137 TRACE ("BFD_RELOC_386_GLOB_DAT");
138 return &elf_howto_table[ (int)R_386_GLOB_DAT ];
139
140 case BFD_RELOC_386_JUMP_SLOT:
141 TRACE ("BFD_RELOC_386_JUMP_SLOT");
142 return &elf_howto_table[ (int)R_386_JUMP_SLOT ];
143
144 case BFD_RELOC_386_RELATIVE:
145 TRACE ("BFD_RELOC_386_RELATIVE");
146 return &elf_howto_table[ (int)R_386_RELATIVE ];
147
148 case BFD_RELOC_386_GOTOFF:
149 TRACE ("BFD_RELOC_386_GOTOFF");
150 return &elf_howto_table[ (int)R_386_GOTOFF ];
151
152 case BFD_RELOC_386_GOTPC:
153 TRACE ("BFD_RELOC_386_GOTPC");
154 return &elf_howto_table[ (int)R_386_GOTPC ];
155
156 default:
157 break;
158 }
159
160 TRACE ("Unknown");
161 return 0;
162 }
163
164 static void
165 elf_i386_info_to_howto (abfd, cache_ptr, dst)
166 bfd *abfd;
167 arelent *cache_ptr;
168 Elf32_Internal_Rela *dst;
169 {
170 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
171
172 cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
173 }
174
175 static void
176 elf_i386_info_to_howto_rel (abfd, cache_ptr, dst)
177 bfd *abfd;
178 arelent *cache_ptr;
179 Elf32_Internal_Rel *dst;
180 {
181 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
182
183 cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
184 }
185 \f
186 /* Functions for the i386 ELF linker. */
187
188 /* The name of the dynamic interpreter. This is put in the .interp
189 section. */
190
191 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
192
193 /* The size in bytes of an entry in the procedure linkage table. */
194
195 #define PLT_ENTRY_SIZE 16
196
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. */
199
200 static bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
201 {
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. */
207 };
208
209 /* Subsequent entries in an absolute procedure linkage table look like
210 this. */
211
212 static bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
213 {
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. */
220 };
221
222 /* Create dynamic sections when linking against a dynamic object. */
223
224 static boolean
225 elf_i386_create_dynamic_sections (abfd, info)
226 bfd *abfd;
227 struct bfd_link_info *info;
228 {
229 flagword flags;
230 register asection *s;
231 struct elf_link_hash_entry *h;
232
233 /* We need to create .plt, .rel.plt, .got, .dynbss, and .rel.bss
234 sections. */
235
236 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
237
238 s = bfd_make_section (abfd, ".plt");
239 if (s == NULL
240 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY | SEC_CODE)
241 || ! bfd_set_section_alignment (abfd, s, 2))
242 return false;
243
244 s = bfd_make_section (abfd, ".rel.plt");
245 if (s == NULL
246 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
247 || ! bfd_set_section_alignment (abfd, s, 2))
248 return false;
249
250 s = bfd_make_section (abfd, ".got");
251 if (s == NULL
252 || ! bfd_set_section_flags (abfd, s, flags)
253 || ! bfd_set_section_alignment (abfd, s, 2))
254 return false;
255
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
259 table. */
260 h = NULL;
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)))
265 return false;
266 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
267
268 /* The first three global offset table entries are reserved. */
269 s->_raw_size += 3 * 4;
270
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");
278 if (s == NULL
279 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
280 return false;
281
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");
287 if (s == NULL
288 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
289 || ! bfd_set_section_alignment (abfd, s, 2))
290 return false;
291
292 return true;
293 }
294
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
299 understand. */
300
301 static boolean
302 elf_i386_adjust_dynamic_symbol (info, h)
303 struct bfd_link_info *info;
304 struct elf_link_hash_entry *h;
305 {
306 bfd *dynobj;
307 asection *s;
308 unsigned int power_of_two;
309 size_t align;
310
311 dynobj = elf_hash_table (info)->dynobj;
312
313 /* Make sure we know what is going on here. */
314 BFD_ASSERT (dynobj != NULL
315 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
316 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
317 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
318 && h->root.type == bfd_link_hash_defined
319 && (bfd_get_flavour (h->root.u.def.section->owner)
320 == bfd_target_elf_flavour)
321 && (elf_elfheader (h->root.u.def.section->owner)->e_type
322 == ET_DYN)
323 && h->root.u.def.section->output_section == NULL);
324
325 /* If this is a function, put it in the procedure linkage table. We
326 will fill in the contents of the procedure linkage table later,
327 when we know the address of the .got section. */
328 if (h->type == STT_FUNC)
329 {
330 s = bfd_get_section_by_name (dynobj, ".plt");
331 BFD_ASSERT (s != NULL);
332
333 /* If this is the first .plt entry, make room for the special
334 first entry. */
335 if (s->_raw_size == 0)
336 s->_raw_size += PLT_ENTRY_SIZE;
337
338 /* Set the symbol to this location in the .plt. */
339 h->root.u.def.section = s;
340 h->root.u.def.value = s->_raw_size;
341
342 /* Make room for this entry. */
343 s->_raw_size += PLT_ENTRY_SIZE;
344
345 /* We also need to make an entry in the .got section. */
346
347 s = bfd_get_section_by_name (dynobj, ".got");
348 BFD_ASSERT (s != NULL);
349 s->_raw_size += 4;
350
351 /* We also need to make an entry in the .rel.plt section. */
352
353 s = bfd_get_section_by_name (dynobj, ".rel.plt");
354 BFD_ASSERT (s != NULL);
355 s->_raw_size += sizeof (Elf32_External_Rel);
356
357 return true;
358 }
359
360 /* If this is a weak symbol, and there is a real definition, the
361 processor independent code will have arranged for us to see the
362 real definition first, and we can just use the same value. */
363 if (h->weakdef != NULL)
364 {
365 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined);
366 h->root.u.def.section = h->weakdef->root.u.def.section;
367 h->root.u.def.value = h->weakdef->root.u.def.value;
368 h->align = (bfd_size_type) -1;
369 return true;
370 }
371
372 /* This is a reference to a symbol defined by a dynamic object which
373 is not a function. We must allocate it in our .dynbss section,
374 which will become part of the .bss section of the executable.
375 There will be an entry for this symbol in the .dynsym section.
376 The dynamic object will contain position independent code, so all
377 references from the dynamic object to this symbol will go through
378 the global offset table. The dynamic linker will use the .dynsym
379 entry to determine the address it must put in the global offset
380 table, so both the dynamic object and the regular object will
381 refer to the same memory location for the variable. */
382
383 s = bfd_get_section_by_name (dynobj, ".dynbss");
384 BFD_ASSERT (s != NULL);
385
386 /* If the symbol is currently defined in the .bss section of the
387 dynamic object, then it is OK to simply initialize it to zero.
388 If the symbol is in some other section, we must generate a
389 R_386_COPY reloc to tell the dynamic linker to copy the initial
390 value out of the dynamic object and into the runtime process
391 image. We need to remember the offset into the .rel.bss section
392 we are going to use, and we coopt the align field for this
393 purpose (the align field is only used for common symbols, and
394 these symbols are always defined). It would be cleaner to use a
395 new field, but that would waste memory. */
396 if ((h->root.u.def.section->flags & SEC_LOAD) == 0)
397 h->align = (bfd_size_type) -1;
398 else
399 {
400 asection *srel;
401
402 srel = bfd_get_section_by_name (dynobj, ".rel.bss");
403 BFD_ASSERT (srel != NULL);
404 h->align = srel->_raw_size;
405 srel->_raw_size += sizeof (Elf32_External_Rel);
406 }
407
408 /* We need to figure out the alignment required for this symbol. I
409 have no idea how ELF linkers handle this. */
410 switch (h->size)
411 {
412 case 0:
413 case 1:
414 power_of_two = 0;
415 align = 1;
416 break;
417 case 2:
418 power_of_two = 1;
419 align = 2;
420 break;
421 case 3:
422 case 4:
423 power_of_two = 2;
424 align = 4;
425 break;
426 case 5:
427 case 6:
428 case 7:
429 case 8:
430 power_of_two = 3;
431 align = 8;
432 break;
433 default:
434 power_of_two = 4;
435 align = 16;
436 break;
437 }
438
439 /* Apply the required alignment. */
440 s->_raw_size = BFD_ALIGN (s->_raw_size, align);
441 if (power_of_two > bfd_get_section_alignment (dynobj, s))
442 {
443 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
444 return false;
445 }
446
447 /* Define the symbol as being at this point in the section. */
448 h->root.u.def.section = s;
449 h->root.u.def.value = s->_raw_size;
450
451 /* Increment the section size to make room for the symbol. */
452 s->_raw_size += h->size;
453
454 return true;
455 }
456
457 /* Allocate contents for a section. */
458
459 static INLINE boolean
460 elf_i386_allocate_dynamic_section (dynobj, name)
461 bfd *dynobj;
462 const char *name;
463 {
464 register asection *s;
465
466 s = bfd_get_section_by_name (dynobj, name);
467 BFD_ASSERT (s != NULL);
468 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
469 if (s->contents == NULL && s->_raw_size != 0)
470 {
471 bfd_set_error (bfd_error_no_memory);
472 return false;
473 }
474 return true;
475 }
476
477 /* Set the sizes of the dynamic sections. */
478
479 static boolean
480 elf_i386_size_dynamic_sections (output_bfd, info)
481 bfd *output_bfd;
482 struct bfd_link_info *info;
483 {
484 bfd *dynobj;
485 asection *s;
486
487 dynobj = elf_hash_table (info)->dynobj;
488 BFD_ASSERT (dynobj != NULL);
489
490 /* Set the contents of the .interp section to the interpreter. */
491 s = bfd_get_section_by_name (dynobj, ".interp");
492 BFD_ASSERT (s != NULL);
493 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
494 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
495
496 /* The adjust_dynamic_symbol entry point has determined the sizes of
497 the various dynamic sections. Allocate some memory for them to
498 hold contents. */
499 if (! elf_i386_allocate_dynamic_section (dynobj, ".plt")
500 || ! elf_i386_allocate_dynamic_section (dynobj, ".rel.plt")
501 || ! elf_i386_allocate_dynamic_section (dynobj, ".got")
502 || ! elf_i386_allocate_dynamic_section (dynobj, ".rel.bss"))
503 return false;
504
505 /* Add some entries to the .dynamic section. We fill in the values
506 later, in elf_i386_finish_dynamic_sections, but we must add the
507 entries now so that we get the correct size for the .dynamic
508 section. */
509 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
510 return false;
511
512 s = bfd_get_section_by_name (dynobj, ".plt");
513 BFD_ASSERT (s != NULL);
514 if (s->_raw_size != 0)
515 {
516 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
517 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
518 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
519 return false;
520 }
521
522 /* If we didn't need the .rel.bss section, then discard it from the
523 output file. This is a hack. We don't bother to do it for the
524 other sections because they normally are needed. */
525 s = bfd_get_section_by_name (dynobj, ".rel.bss");
526 BFD_ASSERT (s != NULL);
527 if (s->_raw_size == 0)
528 {
529 asection **spp;
530
531 for (spp = &s->output_section->owner->sections;
532 *spp != s->output_section;
533 spp = &(*spp)->next)
534 ;
535 *spp = s->output_section->next;
536 --s->output_section->owner->section_count;
537 }
538 else
539 {
540 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0)
541 || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0)
542 || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT,
543 sizeof (Elf32_External_Rel)))
544 return false;
545 }
546
547 return true;
548 }
549
550 /* Relocate an i386 ELF section. */
551
552 static boolean
553 elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
554 contents, relocs, local_syms, local_sections)
555 bfd *output_bfd;
556 struct bfd_link_info *info;
557 bfd *input_bfd;
558 asection *input_section;
559 bfd_byte *contents;
560 Elf_Internal_Rela *relocs;
561 Elf_Internal_Sym *local_syms;
562 asection **local_sections;
563 {
564 Elf_Internal_Shdr *symtab_hdr;
565 Elf_Internal_Rela *rel;
566 Elf_Internal_Rela *relend;
567
568 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
569
570 rel = relocs;
571 relend = relocs + input_section->reloc_count;
572 for (; rel < relend; rel++)
573 {
574 int r_type;
575 const reloc_howto_type *howto;
576 long r_symndx;
577 struct elf_link_hash_entry *h;
578 Elf_Internal_Sym *sym;
579 asection *sec;
580 bfd_vma relocation;
581 bfd_reloc_status_type r;
582
583 r_type = ELF32_R_TYPE (rel->r_info);
584 if (r_type < 0 || r_type >= (int) R_386_max)
585 {
586 bfd_set_error (bfd_error_bad_value);
587 return false;
588 }
589 howto = elf_howto_table + r_type;
590
591 r_symndx = ELF32_R_SYM (rel->r_info);
592
593 if (info->relocateable)
594 {
595 /* This is a relocateable link. We don't have to change
596 anything, unless the reloc is against a section symbol,
597 in which case we have to adjust according to where the
598 section symbol winds up in the output section. */
599 if (r_symndx < symtab_hdr->sh_info)
600 {
601 sym = local_syms + r_symndx;
602 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
603 {
604 bfd_vma val;
605
606 sec = local_sections[r_symndx];
607 val = bfd_get_32 (input_bfd, contents + rel->r_offset);
608 val += sec->output_offset + sym->st_value;
609 bfd_put_32 (input_bfd, val, contents + rel->r_offset);
610 }
611 }
612
613 continue;
614 }
615
616 /* This is a final link. */
617 h = NULL;
618 sym = NULL;
619 sec = NULL;
620 if (r_symndx < symtab_hdr->sh_info)
621 {
622 sym = local_syms + r_symndx;
623 sec = local_sections[r_symndx];
624 relocation = (sec->output_section->vma
625 + sec->output_offset
626 + sym->st_value);
627 }
628 else
629 {
630 long indx;
631
632 indx = r_symndx - symtab_hdr->sh_info;
633 h = elf_sym_hashes (input_bfd)[indx];
634 if (h->root.type == bfd_link_hash_defined)
635 {
636 sec = h->root.u.def.section;
637 relocation = (h->root.u.def.value
638 + sec->output_section->vma
639 + sec->output_offset);
640 }
641 else if (h->root.type == bfd_link_hash_weak)
642 relocation = 0;
643 else
644 {
645 if (! ((*info->callbacks->undefined_symbol)
646 (info, h->root.root.string, input_bfd,
647 input_section, rel->r_offset)))
648 return false;
649 relocation = 0;
650 }
651 }
652
653 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
654 contents, rel->r_offset,
655 relocation, (bfd_vma) 0);
656
657 if (r != bfd_reloc_ok)
658 {
659 switch (r)
660 {
661 default:
662 case bfd_reloc_outofrange:
663 abort ();
664 case bfd_reloc_overflow:
665 {
666 const char *name;
667
668 if (h != NULL)
669 name = h->root.root.string;
670 else
671 {
672 name = elf_string_from_elf_section (input_bfd,
673 symtab_hdr->sh_link,
674 sym->st_name);
675 if (name == NULL)
676 return false;
677 if (*name == '\0')
678 name = bfd_section_name (input_bfd, sec);
679 }
680 if (! ((*info->callbacks->reloc_overflow)
681 (info, name, howto->name, (bfd_vma) 0,
682 input_bfd, input_section, rel->r_offset)))
683 return false;
684 }
685 break;
686 }
687 }
688 }
689
690 return true;
691 }
692
693 /* Finish up dynamic symbol handling. We set the contents of various
694 dynamic sections here. */
695
696 static boolean
697 elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
698 bfd *output_bfd;
699 struct bfd_link_info *info;
700 struct elf_link_hash_entry *h;
701 Elf_Internal_Sym *sym;
702 {
703 /* If this symbol is not defined by a dynamic object, or is not
704 referenced by a regular object, ignore it. */
705 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
706 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
707 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
708 {
709 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
710 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
711 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
712 sym->st_shndx = SHN_ABS;
713 return true;
714 }
715
716 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
717 BFD_ASSERT (h->dynindx != -1);
718
719 if (h->type == STT_FUNC)
720 {
721 asection *splt;
722 asection *sgot;
723 asection *srel;
724 bfd_vma plt_index;
725 bfd_vma got_offset;
726 Elf_Internal_Rel rel;
727
728 splt = h->root.u.def.section;
729 BFD_ASSERT (strcmp (bfd_get_section_name (splt->owner, splt), ".plt")
730 == 0);
731 sgot = bfd_get_section_by_name (splt->owner, ".got");
732 BFD_ASSERT (sgot != NULL);
733 srel = bfd_get_section_by_name (splt->owner, ".rel.plt");
734 BFD_ASSERT (srel != NULL);
735
736 /* FIXME: This only handles an absolute procedure linkage table.
737 When producing a dynamic object, we need to generate a
738 position independent procedure linkage table. */
739
740 /* Get the index in the procedure linkage table which
741 corresponds to this symbol. This is the index of this symbol
742 in all the symbols for which we are making plt entries. The
743 first entry in the procedure linkage table is reserved. */
744 plt_index = h->root.u.def.value / PLT_ENTRY_SIZE - 1;
745
746 /* Get the offset into the .got table of the entry that
747 corresponds to this function. Each .got entry is 4 bytes.
748 The first three are reserved. */
749 got_offset = (plt_index + 3) * 4;
750
751 /* Fill in the entry in the procedure linkage table. */
752 memcpy (splt->contents + h->root.u.def.value, elf_i386_plt_entry,
753 PLT_ENTRY_SIZE);
754 bfd_put_32 (output_bfd,
755 (sgot->output_section->vma
756 + sgot->output_offset
757 + got_offset),
758 splt->contents + h->root.u.def.value + 2);
759 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
760 splt->contents + h->root.u.def.value + 7);
761 bfd_put_32 (output_bfd, - (h->root.u.def.value + PLT_ENTRY_SIZE),
762 splt->contents + h->root.u.def.value + 12);
763
764 /* Fill in the entry in the global offset table. */
765 bfd_put_32 (output_bfd,
766 (splt->output_section->vma
767 + splt->output_offset
768 + h->root.u.def.value
769 + 6),
770 sgot->contents + got_offset);
771
772 /* Fill in the entry in the .rel.plt section. */
773 rel.r_offset = (sgot->output_section->vma
774 + sgot->output_offset
775 + got_offset);
776 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
777 bfd_elf32_swap_reloc_out (output_bfd, &rel,
778 ((Elf32_External_Rel *) srel->contents
779 + plt_index));
780
781 /* Mark the symbol as undefined, rather than as defined in the
782 .plt section. Leave the value alone. */
783 sym->st_shndx = SHN_UNDEF;
784 }
785 else
786 {
787 /* This is not a function. We have already allocated memory for
788 it in the .bss section (via .dynbss). All we have to do here
789 is create a COPY reloc if required. */
790 if (h->align != (bfd_size_type) -1)
791 {
792 asection *s;
793 Elf_Internal_Rel rel;
794
795 s = bfd_get_section_by_name (h->root.u.def.section->owner,
796 ".rel.bss");
797 BFD_ASSERT (s != NULL);
798
799 rel.r_offset = (h->root.u.def.value
800 + h->root.u.def.section->output_section->vma
801 + h->root.u.def.section->output_offset);
802 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
803 bfd_elf32_swap_reloc_out (output_bfd, &rel,
804 ((Elf32_External_Rel *)
805 (s->contents + h->align)));
806 }
807 }
808
809 return true;
810 }
811
812 /* Finish up the dynamic sections. */
813
814 static boolean
815 elf_i386_finish_dynamic_sections (output_bfd, info)
816 bfd *output_bfd;
817 struct bfd_link_info *info;
818 {
819 asection *splt;
820 asection *sgot;
821 asection *sdyn;
822 Elf32_External_Dyn *dyncon, *dynconend;
823
824 splt = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".plt");
825 sgot = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".got");
826 sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".dynamic");
827 BFD_ASSERT (splt != NULL && sgot != NULL && sdyn != NULL);
828
829 dyncon = (Elf32_External_Dyn *) sdyn->contents;
830 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
831 for (; dyncon < dynconend; dyncon++)
832 {
833 Elf_Internal_Dyn dyn;
834 const char *name;
835 boolean size;
836
837 bfd_elf32_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon, &dyn);
838
839 /* My reading of the SVR4 ABI indicates that the procedure
840 linkage table relocs (DT_JMPREL) should be included in the
841 overall relocs (DT_REL). This is what Solaris does.
842 However, UnixWare can not handle that case. Therefore, we
843 override the DT_REL and DT_RELSZ entries here to make them
844 not include the JMPREL relocs. */
845
846 switch (dyn.d_tag)
847 {
848 case DT_PLTGOT: name = ".got"; size = false; break;
849 case DT_PLTRELSZ: name = ".rel.plt"; size = true; break;
850 case DT_JMPREL: name = ".rel.plt"; size = false; break;
851 case DT_REL: name = ".rel.bss"; size = false; break;
852 case DT_RELSZ: name = ".rel.bss"; size = true; break;
853 default: name = NULL; size = false; break;
854 }
855
856 if (name != NULL)
857 {
858 asection *s;
859
860 s = bfd_get_section_by_name (output_bfd, name);
861 BFD_ASSERT (s != NULL);
862 if (! size)
863 dyn.d_un.d_ptr = s->vma;
864 else
865 {
866 if (s->_cooked_size != 0)
867 dyn.d_un.d_val = s->_cooked_size;
868 else
869 dyn.d_un.d_val = s->_raw_size;
870 }
871 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
872 }
873 }
874
875 /* Fill in the first entry in the procedure linkage table. */
876 if (splt->_raw_size > 0)
877 {
878 memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE);
879 bfd_put_32 (output_bfd,
880 sgot->output_section->vma + sgot->output_offset + 4,
881 splt->contents + 2);
882 bfd_put_32 (output_bfd,
883 sgot->output_section->vma + sgot->output_offset + 8,
884 splt->contents + 8);
885 }
886
887 /* Fill in the first three entries in the global offset table. */
888 if (sgot->_raw_size > 0)
889 {
890 bfd_put_32 (output_bfd,
891 sdyn->output_section->vma + sdyn->output_offset,
892 sgot->contents);
893 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
894 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
895 }
896
897 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
898
899 /* UnixWare sets the entsize of .plt to 4, although that doesn't
900 really seem like the right value. */
901 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
902
903 return true;
904 }
905
906 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
907 #define TARGET_LITTLE_NAME "elf32-i386"
908 #define ELF_ARCH bfd_arch_i386
909 #define ELF_MACHINE_CODE EM_386
910 #define elf_info_to_howto elf_i386_info_to_howto
911 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
912 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
913 #define ELF_MAXPAGESIZE 0x1000
914 #define elf_backend_create_dynamic_sections \
915 elf_i386_create_dynamic_sections
916 #define elf_backend_adjust_dynamic_symbol \
917 elf_i386_adjust_dynamic_symbol
918 #define elf_backend_size_dynamic_sections \
919 elf_i386_size_dynamic_sections
920 #define elf_backend_relocate_section elf_i386_relocate_section
921 #define elf_backend_finish_dynamic_symbol \
922 elf_i386_finish_dynamic_symbol
923 #define elf_backend_finish_dynamic_sections \
924 elf_i386_finish_dynamic_sections
925
926 #include "elf32-target.h"