* config/m68k-parse.y (motorola_operand): Allow (zdireg,EXPR).
[binutils-gdb.git] / bfd / elf32-i386.c
1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "bfdlink.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25
26 static reloc_howto_type *elf_i386_reloc_type_lookup
27 PARAMS ((bfd *, bfd_reloc_code_real_type));
28 static void elf_i386_info_to_howto
29 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
30 static void elf_i386_info_to_howto_rel
31 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
32 static boolean elf_i386_check_relocs
33 PARAMS ((bfd *, struct bfd_link_info *, asection *,
34 const Elf_Internal_Rela *));
35 static boolean elf_i386_adjust_dynamic_symbol
36 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
37 static boolean elf_i386_size_dynamic_sections
38 PARAMS ((bfd *, struct bfd_link_info *));
39 static boolean elf_i386_relocate_section
40 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
41 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
42 static boolean elf_i386_finish_dynamic_symbol
43 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
44 Elf_Internal_Sym *));
45 static boolean elf_i386_finish_dynamic_sections
46 PARAMS ((bfd *, struct bfd_link_info *));
47
48 #define USE_REL 1 /* 386 uses REL relocations instead of RELA */
49
50 enum reloc_type
51 {
52 R_386_NONE = 0,
53 R_386_32,
54 R_386_PC32,
55 R_386_GOT32,
56 R_386_PLT32,
57 R_386_COPY,
58 R_386_GLOB_DAT,
59 R_386_JUMP_SLOT,
60 R_386_RELATIVE,
61 R_386_GOTOFF,
62 R_386_GOTPC,
63 FIRST_INVALID_RELOC,
64 LAST_INVALID_RELOC = 19,
65 /* The remaining relocs are a GNU extension. */
66 R_386_16 = 20,
67 R_386_PC16,
68 R_386_8,
69 R_386_PC8,
70 R_386_max
71 };
72
73 #if 0
74 static CONST char *CONST reloc_type_names[] =
75 {
76 "R_386_NONE",
77 "R_386_32",
78 "R_386_PC32",
79 "R_386_GOT32",
80 "R_386_PLT32",
81 "R_386_COPY",
82 "R_386_GLOB_DAT",
83 "R_386_JUMP_SLOT",
84 "R_386_RELATIVE",
85 "R_386_GOTOFF",
86 "R_386_GOTPC",
87 };
88 #endif
89
90 static reloc_howto_type elf_howto_table[]=
91 {
92 HOWTO(R_386_NONE, 0,0, 0,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_NONE", true,0x00000000,0x00000000,false),
93 HOWTO(R_386_32, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_32", true,0xffffffff,0xffffffff,false),
94 HOWTO(R_386_PC32, 0,2,32,true, 0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PC32", true,0xffffffff,0xffffffff,true),
95 HOWTO(R_386_GOT32, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOT32", true,0xffffffff,0xffffffff,false),
96 HOWTO(R_386_PLT32, 0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PLT32", true,0xffffffff,0xffffffff,true),
97 HOWTO(R_386_COPY, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_COPY", true,0xffffffff,0xffffffff,false),
98 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),
99 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),
100 HOWTO(R_386_RELATIVE, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false),
101 HOWTO(R_386_GOTOFF, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTOFF", true,0xffffffff,0xffffffff,false),
102 HOWTO(R_386_GOTPC, 0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTPC", true,0xffffffff,0xffffffff,true),
103 { 11 },
104 { 12 },
105 { 13 },
106 { 14 },
107 { 15 },
108 { 16 },
109 { 17 },
110 { 18 },
111 { 19 },
112 /* The remaining relocs are a GNU extension. */
113 HOWTO(R_386_16, 0,1,16,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_16", true,0xffff,0xffff,false),
114 HOWTO(R_386_PC16, 0,1,16,true, 0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PC16", true,0xffff,0xffff,true),
115 HOWTO(R_386_8, 0,0,8,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_8", true,0xff,0xff,false),
116 HOWTO(R_386_PC8, 0,0,8,true, 0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PC8", true,0xff,0xff,true),
117 };
118
119 #ifdef DEBUG_GEN_RELOC
120 #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
121 #else
122 #define TRACE(str)
123 #endif
124
125 static reloc_howto_type *
126 elf_i386_reloc_type_lookup (abfd, code)
127 bfd *abfd;
128 bfd_reloc_code_real_type code;
129 {
130 switch (code)
131 {
132 case BFD_RELOC_NONE:
133 TRACE ("BFD_RELOC_NONE");
134 return &elf_howto_table[ (int)R_386_NONE ];
135
136 case BFD_RELOC_32:
137 TRACE ("BFD_RELOC_32");
138 return &elf_howto_table[ (int)R_386_32 ];
139
140 case BFD_RELOC_32_PCREL:
141 TRACE ("BFD_RELOC_PC32");
142 return &elf_howto_table[ (int)R_386_PC32 ];
143
144 case BFD_RELOC_386_GOT32:
145 TRACE ("BFD_RELOC_386_GOT32");
146 return &elf_howto_table[ (int)R_386_GOT32 ];
147
148 case BFD_RELOC_386_PLT32:
149 TRACE ("BFD_RELOC_386_PLT32");
150 return &elf_howto_table[ (int)R_386_PLT32 ];
151
152 case BFD_RELOC_386_COPY:
153 TRACE ("BFD_RELOC_386_COPY");
154 return &elf_howto_table[ (int)R_386_COPY ];
155
156 case BFD_RELOC_386_GLOB_DAT:
157 TRACE ("BFD_RELOC_386_GLOB_DAT");
158 return &elf_howto_table[ (int)R_386_GLOB_DAT ];
159
160 case BFD_RELOC_386_JUMP_SLOT:
161 TRACE ("BFD_RELOC_386_JUMP_SLOT");
162 return &elf_howto_table[ (int)R_386_JUMP_SLOT ];
163
164 case BFD_RELOC_386_RELATIVE:
165 TRACE ("BFD_RELOC_386_RELATIVE");
166 return &elf_howto_table[ (int)R_386_RELATIVE ];
167
168 case BFD_RELOC_386_GOTOFF:
169 TRACE ("BFD_RELOC_386_GOTOFF");
170 return &elf_howto_table[ (int)R_386_GOTOFF ];
171
172 case BFD_RELOC_386_GOTPC:
173 TRACE ("BFD_RELOC_386_GOTPC");
174 return &elf_howto_table[ (int)R_386_GOTPC ];
175
176 /* The remaining relocs are a GNU extension. */
177 case BFD_RELOC_16:
178 TRACE ("BFD_RELOC_16");
179 return &elf_howto_table[(int) R_386_16];
180
181 case BFD_RELOC_16_PCREL:
182 TRACE ("BFD_RELOC_16_PCREL");
183 return &elf_howto_table[(int) R_386_PC16];
184
185 case BFD_RELOC_8:
186 TRACE ("BFD_RELOC_8");
187 return &elf_howto_table[(int) R_386_8];
188
189 case BFD_RELOC_8_PCREL:
190 TRACE ("BFD_RELOC_8_PCREL");
191 return &elf_howto_table[(int) R_386_PC8];
192
193 default:
194 break;
195 }
196
197 TRACE ("Unknown");
198 return 0;
199 }
200
201 static void
202 elf_i386_info_to_howto (abfd, cache_ptr, dst)
203 bfd *abfd;
204 arelent *cache_ptr;
205 Elf32_Internal_Rela *dst;
206 {
207 abort ();
208 }
209
210 static void
211 elf_i386_info_to_howto_rel (abfd, cache_ptr, dst)
212 bfd *abfd;
213 arelent *cache_ptr;
214 Elf32_Internal_Rel *dst;
215 {
216 enum reloc_type type;
217
218 type = (enum reloc_type) ELF32_R_TYPE (dst->r_info);
219 BFD_ASSERT (type < R_386_max);
220 BFD_ASSERT (type < FIRST_INVALID_RELOC || type > LAST_INVALID_RELOC);
221
222 cache_ptr->howto = &elf_howto_table[(int) type];
223 }
224 \f
225 /* Functions for the i386 ELF linker. */
226
227 /* The name of the dynamic interpreter. This is put in the .interp
228 section. */
229
230 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
231
232 /* The size in bytes of an entry in the procedure linkage table. */
233
234 #define PLT_ENTRY_SIZE 16
235
236 /* The first entry in an absolute procedure linkage table looks like
237 this. See the SVR4 ABI i386 supplement to see how this works. */
238
239 static const bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
240 {
241 0xff, 0x35, /* pushl contents of address */
242 0, 0, 0, 0, /* replaced with address of .got + 4. */
243 0xff, 0x25, /* jmp indirect */
244 0, 0, 0, 0, /* replaced with address of .got + 8. */
245 0, 0, 0, 0 /* pad out to 16 bytes. */
246 };
247
248 /* Subsequent entries in an absolute procedure linkage table look like
249 this. */
250
251 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
252 {
253 0xff, 0x25, /* jmp indirect */
254 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
255 0x68, /* pushl immediate */
256 0, 0, 0, 0, /* replaced with offset into relocation table. */
257 0xe9, /* jmp relative */
258 0, 0, 0, 0 /* replaced with offset to start of .plt. */
259 };
260
261 /* The first entry in a PIC procedure linkage table look like this. */
262
263 static const bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
264 {
265 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
266 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
267 0, 0, 0, 0 /* pad out to 16 bytes. */
268 };
269
270 /* Subsequent entries in a PIC procedure linkage table look like this. */
271
272 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
273 {
274 0xff, 0xa3, /* jmp *offset(%ebx) */
275 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
276 0x68, /* pushl immediate */
277 0, 0, 0, 0, /* replaced with offset into relocation table. */
278 0xe9, /* jmp relative */
279 0, 0, 0, 0 /* replaced with offset to start of .plt. */
280 };
281
282 /* Look through the relocs for a section during the first phase, and
283 allocate space in the global offset table or procedure linkage
284 table. */
285
286 static boolean
287 elf_i386_check_relocs (abfd, info, sec, relocs)
288 bfd *abfd;
289 struct bfd_link_info *info;
290 asection *sec;
291 const Elf_Internal_Rela *relocs;
292 {
293 bfd *dynobj;
294 Elf_Internal_Shdr *symtab_hdr;
295 struct elf_link_hash_entry **sym_hashes;
296 bfd_vma *local_got_offsets;
297 const Elf_Internal_Rela *rel;
298 const Elf_Internal_Rela *rel_end;
299 asection *sgot;
300 asection *srelgot;
301 asection *sreloc;
302
303 if (info->relocateable)
304 return true;
305
306 dynobj = elf_hash_table (info)->dynobj;
307 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
308 sym_hashes = elf_sym_hashes (abfd);
309 local_got_offsets = elf_local_got_offsets (abfd);
310
311 sgot = NULL;
312 srelgot = NULL;
313 sreloc = NULL;
314
315 rel_end = relocs + sec->reloc_count;
316 for (rel = relocs; rel < rel_end; rel++)
317 {
318 unsigned long r_symndx;
319 struct elf_link_hash_entry *h;
320
321 r_symndx = ELF32_R_SYM (rel->r_info);
322
323 if (r_symndx < symtab_hdr->sh_info)
324 h = NULL;
325 else
326 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
327
328 /* Some relocs require a global offset table. */
329 if (dynobj == NULL)
330 {
331 switch (ELF32_R_TYPE (rel->r_info))
332 {
333 case R_386_GOT32:
334 case R_386_GOTOFF:
335 case R_386_GOTPC:
336 elf_hash_table (info)->dynobj = dynobj = abfd;
337 if (! _bfd_elf_create_got_section (dynobj, info))
338 return false;
339 break;
340
341 default:
342 break;
343 }
344 }
345
346 switch (ELF32_R_TYPE (rel->r_info))
347 {
348 case R_386_GOT32:
349 /* This symbol requires a global offset table entry. */
350
351 if (sgot == NULL)
352 {
353 sgot = bfd_get_section_by_name (dynobj, ".got");
354 BFD_ASSERT (sgot != NULL);
355 }
356
357 if (srelgot == NULL
358 && (h != NULL || info->shared))
359 {
360 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
361 if (srelgot == NULL)
362 {
363 srelgot = bfd_make_section (dynobj, ".rel.got");
364 if (srelgot == NULL
365 || ! bfd_set_section_flags (dynobj, srelgot,
366 (SEC_ALLOC
367 | SEC_LOAD
368 | SEC_HAS_CONTENTS
369 | SEC_IN_MEMORY
370 | SEC_LINKER_CREATED
371 | SEC_READONLY))
372 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
373 return false;
374 }
375 }
376
377 if (h != NULL)
378 {
379 if (h->got_offset != (bfd_vma) -1)
380 {
381 /* We have already allocated space in the .got. */
382 break;
383 }
384 h->got_offset = sgot->_raw_size;
385
386 /* Make sure this symbol is output as a dynamic symbol. */
387 if (h->dynindx == -1)
388 {
389 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
390 return false;
391 }
392
393 srelgot->_raw_size += sizeof (Elf32_External_Rel);
394 }
395 else
396 {
397 /* This is a global offset table entry for a local
398 symbol. */
399 if (local_got_offsets == NULL)
400 {
401 size_t size;
402 register unsigned int i;
403
404 size = symtab_hdr->sh_info * sizeof (bfd_vma);
405 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
406 if (local_got_offsets == NULL)
407 return false;
408 elf_local_got_offsets (abfd) = local_got_offsets;
409 for (i = 0; i < symtab_hdr->sh_info; i++)
410 local_got_offsets[i] = (bfd_vma) -1;
411 }
412 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
413 {
414 /* We have already allocated space in the .got. */
415 break;
416 }
417 local_got_offsets[r_symndx] = sgot->_raw_size;
418
419 if (info->shared)
420 {
421 /* If we are generating a shared object, we need to
422 output a R_386_RELATIVE reloc so that the dynamic
423 linker can adjust this GOT entry. */
424 srelgot->_raw_size += sizeof (Elf32_External_Rel);
425 }
426 }
427
428 sgot->_raw_size += 4;
429
430 break;
431
432 case R_386_PLT32:
433 /* This symbol requires a procedure linkage table entry. We
434 actually build the entry in adjust_dynamic_symbol,
435 because this might be a case of linking PIC code which is
436 never referenced by a dynamic object, in which case we
437 don't need to generate a procedure linkage table entry
438 after all. */
439
440 /* If this is a local symbol, we resolve it directly without
441 creating a procedure linkage table entry. */
442 if (h == NULL)
443 continue;
444
445 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
446
447 break;
448
449 case R_386_32:
450 case R_386_PC32:
451 if (info->shared
452 && (ELF32_R_TYPE (rel->r_info) != R_386_PC32 || h != NULL))
453 {
454 /* When creating a shared object, we must copy these
455 reloc types into the output file. We create a reloc
456 section in dynobj and make room for this reloc. */
457 if (sreloc == NULL)
458 {
459 const char *name;
460
461 name = (bfd_elf_string_from_elf_section
462 (abfd,
463 elf_elfheader (abfd)->e_shstrndx,
464 elf_section_data (sec)->rel_hdr.sh_name));
465 if (name == NULL)
466 return false;
467
468 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
469 && strcmp (bfd_get_section_name (abfd, sec),
470 name + 4) == 0);
471
472 sreloc = bfd_get_section_by_name (dynobj, name);
473 if (sreloc == NULL)
474 {
475 flagword flags;
476
477 sreloc = bfd_make_section (dynobj, name);
478 flags = (SEC_HAS_CONTENTS | SEC_READONLY
479 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
480 if ((sec->flags & SEC_ALLOC) != 0)
481 flags |= SEC_ALLOC | SEC_LOAD;
482 if (sreloc == NULL
483 || ! bfd_set_section_flags (dynobj, sreloc, flags)
484 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
485 return false;
486 }
487 }
488
489 sreloc->_raw_size += sizeof (Elf32_External_Rel);
490 }
491
492 break;
493
494 default:
495 break;
496 }
497 }
498
499 return true;
500 }
501
502 /* Adjust a symbol defined by a dynamic object and referenced by a
503 regular object. The current definition is in some section of the
504 dynamic object, but we're not including those sections. We have to
505 change the definition to something the rest of the link can
506 understand. */
507
508 static boolean
509 elf_i386_adjust_dynamic_symbol (info, h)
510 struct bfd_link_info *info;
511 struct elf_link_hash_entry *h;
512 {
513 bfd *dynobj;
514 asection *s;
515 unsigned int power_of_two;
516
517 dynobj = elf_hash_table (info)->dynobj;
518
519 /* Make sure we know what is going on here. */
520 BFD_ASSERT (dynobj != NULL
521 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
522 || h->weakdef != NULL
523 || ((h->elf_link_hash_flags
524 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
525 && (h->elf_link_hash_flags
526 & ELF_LINK_HASH_REF_REGULAR) != 0
527 && (h->elf_link_hash_flags
528 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
529
530 /* If this is a function, put it in the procedure linkage table. We
531 will fill in the contents of the procedure linkage table later,
532 when we know the address of the .got section. */
533 if (h->type == STT_FUNC
534 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
535 {
536 if (! info->shared
537 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
538 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
539 {
540 /* This case can occur if we saw a PLT32 reloc in an input
541 file, but the symbol was never referred to by a dynamic
542 object. In such a case, we don't actually need to build
543 a procedure linkage table, and we can just do a PC32
544 reloc instead. */
545 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
546 return true;
547 }
548
549 /* Make sure this symbol is output as a dynamic symbol. */
550 if (h->dynindx == -1)
551 {
552 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
553 return false;
554 }
555
556 s = bfd_get_section_by_name (dynobj, ".plt");
557 BFD_ASSERT (s != NULL);
558
559 /* If this is the first .plt entry, make room for the special
560 first entry. */
561 if (s->_raw_size == 0)
562 s->_raw_size += PLT_ENTRY_SIZE;
563
564 /* If this symbol is not defined in a regular file, and we are
565 not generating a shared library, then set the symbol to this
566 location in the .plt. This is required to make function
567 pointers compare as equal between the normal executable and
568 the shared library. */
569 if (! info->shared
570 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
571 {
572 h->root.u.def.section = s;
573 h->root.u.def.value = s->_raw_size;
574 }
575
576 h->plt_offset = s->_raw_size;
577
578 /* Make room for this entry. */
579 s->_raw_size += PLT_ENTRY_SIZE;
580
581 /* We also need to make an entry in the .got.plt section, which
582 will be placed in the .got section by the linker script. */
583
584 s = bfd_get_section_by_name (dynobj, ".got.plt");
585 BFD_ASSERT (s != NULL);
586 s->_raw_size += 4;
587
588 /* We also need to make an entry in the .rel.plt section. */
589
590 s = bfd_get_section_by_name (dynobj, ".rel.plt");
591 BFD_ASSERT (s != NULL);
592 s->_raw_size += sizeof (Elf32_External_Rel);
593
594 return true;
595 }
596
597 /* If this is a weak symbol, and there is a real definition, the
598 processor independent code will have arranged for us to see the
599 real definition first, and we can just use the same value. */
600 if (h->weakdef != NULL)
601 {
602 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
603 || h->weakdef->root.type == bfd_link_hash_defweak);
604 h->root.u.def.section = h->weakdef->root.u.def.section;
605 h->root.u.def.value = h->weakdef->root.u.def.value;
606 return true;
607 }
608
609 /* This is a reference to a symbol defined by a dynamic object which
610 is not a function. */
611
612 /* If we are creating a shared library, we must presume that the
613 only references to the symbol are via the global offset table.
614 For such cases we need not do anything here; the relocations will
615 be handled correctly by relocate_section. */
616 if (info->shared)
617 return true;
618
619 /* We must allocate the symbol in our .dynbss section, which will
620 become part of the .bss section of the executable. There will be
621 an entry for this symbol in the .dynsym section. The dynamic
622 object will contain position independent code, so all references
623 from the dynamic object to this symbol will go through the global
624 offset table. The dynamic linker will use the .dynsym entry to
625 determine the address it must put in the global offset table, so
626 both the dynamic object and the regular object will refer to the
627 same memory location for the variable. */
628
629 s = bfd_get_section_by_name (dynobj, ".dynbss");
630 BFD_ASSERT (s != NULL);
631
632 /* If the symbol is currently defined in the .bss section of the
633 dynamic object, then it is OK to simply initialize it to zero.
634 If the symbol is in some other section, we must generate a
635 R_386_COPY reloc to tell the dynamic linker to copy the initial
636 value out of the dynamic object and into the runtime process
637 image. We need to remember the offset into the .rel.bss section
638 we are going to use. */
639 if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
640 {
641 asection *srel;
642
643 srel = bfd_get_section_by_name (dynobj, ".rel.bss");
644 BFD_ASSERT (srel != NULL);
645 srel->_raw_size += sizeof (Elf32_External_Rel);
646 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
647 }
648
649 /* We need to figure out the alignment required for this symbol. I
650 have no idea how ELF linkers handle this. */
651 power_of_two = bfd_log2 (h->size);
652 if (power_of_two > 3)
653 power_of_two = 3;
654
655 /* Apply the required alignment. */
656 s->_raw_size = BFD_ALIGN (s->_raw_size,
657 (bfd_size_type) (1 << power_of_two));
658 if (power_of_two > bfd_get_section_alignment (dynobj, s))
659 {
660 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
661 return false;
662 }
663
664 /* Define the symbol as being at this point in the section. */
665 h->root.u.def.section = s;
666 h->root.u.def.value = s->_raw_size;
667
668 /* Increment the section size to make room for the symbol. */
669 s->_raw_size += h->size;
670
671 return true;
672 }
673
674 /* Set the sizes of the dynamic sections. */
675
676 static boolean
677 elf_i386_size_dynamic_sections (output_bfd, info)
678 bfd *output_bfd;
679 struct bfd_link_info *info;
680 {
681 bfd *dynobj;
682 asection *s;
683 boolean plt;
684 boolean relocs;
685 boolean reltext;
686
687 dynobj = elf_hash_table (info)->dynobj;
688 BFD_ASSERT (dynobj != NULL);
689
690 if (elf_hash_table (info)->dynamic_sections_created)
691 {
692 /* Set the contents of the .interp section to the interpreter. */
693 if (! info->shared)
694 {
695 s = bfd_get_section_by_name (dynobj, ".interp");
696 BFD_ASSERT (s != NULL);
697 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
698 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
699 }
700 }
701 else
702 {
703 /* We may have created entries in the .rel.got section.
704 However, if we are not creating the dynamic sections, we will
705 not actually use these entries. Reset the size of .rel.got,
706 which will cause it to get stripped from the output file
707 below. */
708 s = bfd_get_section_by_name (dynobj, ".rel.got");
709 if (s != NULL)
710 s->_raw_size = 0;
711 }
712
713 /* The check_relocs and adjust_dynamic_symbol entry points have
714 determined the sizes of the various dynamic sections. Allocate
715 memory for them. */
716 plt = false;
717 relocs = false;
718 reltext = false;
719 for (s = dynobj->sections; s != NULL; s = s->next)
720 {
721 const char *name;
722 boolean strip;
723
724 if ((s->flags & SEC_LINKER_CREATED) == 0)
725 continue;
726
727 /* It's OK to base decisions on the section name, because none
728 of the dynobj section names depend upon the input files. */
729 name = bfd_get_section_name (dynobj, s);
730
731 strip = false;
732
733 if (strcmp (name, ".plt") == 0)
734 {
735 if (s->_raw_size == 0)
736 {
737 /* Strip this section if we don't need it; see the
738 comment below. */
739 strip = true;
740 }
741 else
742 {
743 /* Remember whether there is a PLT. */
744 plt = true;
745 }
746 }
747 else if (strncmp (name, ".rel", 4) == 0)
748 {
749 if (s->_raw_size == 0)
750 {
751 /* If we don't need this section, strip it from the
752 output file. This is mostly to handle .rel.bss and
753 .rel.plt. We must create both sections in
754 create_dynamic_sections, because they must be created
755 before the linker maps input sections to output
756 sections. The linker does that before
757 adjust_dynamic_symbol is called, and it is that
758 function which decides whether anything needs to go
759 into these sections. */
760 strip = true;
761 }
762 else
763 {
764 asection *target;
765
766 /* Remember whether there are any reloc sections other
767 than .rel.plt. */
768 if (strcmp (name, ".rel.plt") != 0)
769 {
770 relocs = true;
771
772 /* If this relocation section applies to a read only
773 section, then we probably need a DT_TEXTREL
774 entry. The entries in the .rel.plt section
775 really apply to the .got section, which we
776 created ourselves and so know is not readonly. */
777 target = bfd_get_section_by_name (output_bfd, name + 4);
778 if (target != NULL
779 && (target->flags & SEC_READONLY) != 0)
780 reltext = true;
781 }
782
783 /* We use the reloc_count field as a counter if we need
784 to copy relocs into the output file. */
785 s->reloc_count = 0;
786 }
787 }
788 else if (strncmp (name, ".got", 4) != 0)
789 {
790 /* It's not one of our sections, so don't allocate space. */
791 continue;
792 }
793
794 if (strip)
795 {
796 asection **spp;
797
798 for (spp = &s->output_section->owner->sections;
799 *spp != s->output_section;
800 spp = &(*spp)->next)
801 ;
802 *spp = s->output_section->next;
803 --s->output_section->owner->section_count;
804
805 continue;
806 }
807
808 /* Allocate memory for the section contents. */
809 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
810 if (s->contents == NULL && s->_raw_size != 0)
811 return false;
812 }
813
814 if (elf_hash_table (info)->dynamic_sections_created)
815 {
816 /* Add some entries to the .dynamic section. We fill in the
817 values later, in elf_i386_finish_dynamic_sections, but we
818 must add the entries now so that we get the correct size for
819 the .dynamic section. The DT_DEBUG entry is filled in by the
820 dynamic linker and used by the debugger. */
821 if (! info->shared)
822 {
823 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
824 return false;
825 }
826
827 if (plt)
828 {
829 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
830 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
831 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
832 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
833 return false;
834 }
835
836 if (relocs)
837 {
838 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0)
839 || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0)
840 || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT,
841 sizeof (Elf32_External_Rel)))
842 return false;
843 }
844
845 if (reltext)
846 {
847 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
848 return false;
849 }
850 }
851
852 return true;
853 }
854
855 /* Relocate an i386 ELF section. */
856
857 static boolean
858 elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
859 contents, relocs, local_syms, local_sections)
860 bfd *output_bfd;
861 struct bfd_link_info *info;
862 bfd *input_bfd;
863 asection *input_section;
864 bfd_byte *contents;
865 Elf_Internal_Rela *relocs;
866 Elf_Internal_Sym *local_syms;
867 asection **local_sections;
868 {
869 bfd *dynobj;
870 Elf_Internal_Shdr *symtab_hdr;
871 struct elf_link_hash_entry **sym_hashes;
872 bfd_vma *local_got_offsets;
873 asection *sgot;
874 asection *splt;
875 asection *sreloc;
876 Elf_Internal_Rela *rel;
877 Elf_Internal_Rela *relend;
878
879 dynobj = elf_hash_table (info)->dynobj;
880 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
881 sym_hashes = elf_sym_hashes (input_bfd);
882 local_got_offsets = elf_local_got_offsets (input_bfd);
883
884 sgot = NULL;
885 splt = NULL;
886 sreloc = NULL;
887
888 rel = relocs;
889 relend = relocs + input_section->reloc_count;
890 for (; rel < relend; rel++)
891 {
892 int r_type;
893 reloc_howto_type *howto;
894 unsigned long r_symndx;
895 struct elf_link_hash_entry *h;
896 Elf_Internal_Sym *sym;
897 asection *sec;
898 bfd_vma relocation;
899 bfd_reloc_status_type r;
900
901 r_type = ELF32_R_TYPE (rel->r_info);
902 if (r_type < 0
903 || r_type >= (int) R_386_max
904 || (r_type >= (int) FIRST_INVALID_RELOC
905 && r_type <= (int) LAST_INVALID_RELOC))
906 {
907 bfd_set_error (bfd_error_bad_value);
908 return false;
909 }
910 howto = elf_howto_table + r_type;
911
912 r_symndx = ELF32_R_SYM (rel->r_info);
913
914 if (info->relocateable)
915 {
916 /* This is a relocateable link. We don't have to change
917 anything, unless the reloc is against a section symbol,
918 in which case we have to adjust according to where the
919 section symbol winds up in the output section. */
920 if (r_symndx < symtab_hdr->sh_info)
921 {
922 sym = local_syms + r_symndx;
923 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
924 {
925 bfd_vma val;
926
927 sec = local_sections[r_symndx];
928 val = bfd_get_32 (input_bfd, contents + rel->r_offset);
929 val += sec->output_offset + sym->st_value;
930 bfd_put_32 (input_bfd, val, contents + rel->r_offset);
931 }
932 }
933
934 continue;
935 }
936
937 /* This is a final link. */
938 h = NULL;
939 sym = NULL;
940 sec = NULL;
941 if (r_symndx < symtab_hdr->sh_info)
942 {
943 sym = local_syms + r_symndx;
944 sec = local_sections[r_symndx];
945 relocation = (sec->output_section->vma
946 + sec->output_offset
947 + sym->st_value);
948 }
949 else
950 {
951 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
952 while (h->root.type == bfd_link_hash_indirect
953 || h->root.type == bfd_link_hash_warning)
954 h = (struct elf_link_hash_entry *) h->root.u.i.link;
955 if (h->root.type == bfd_link_hash_defined
956 || h->root.type == bfd_link_hash_defweak)
957 {
958 sec = h->root.u.def.section;
959 if (r_type == R_386_GOTPC
960 || (r_type == R_386_PLT32
961 && h->plt_offset != (bfd_vma) -1)
962 || (r_type == R_386_GOT32
963 && elf_hash_table (info)->dynamic_sections_created
964 && (! info->shared
965 || ! info->symbolic
966 || (h->elf_link_hash_flags
967 & ELF_LINK_HASH_DEF_REGULAR) == 0))
968 || (info->shared
969 && (! info->symbolic
970 || (h->elf_link_hash_flags
971 & ELF_LINK_HASH_DEF_REGULAR) == 0)
972 && (r_type == R_386_32
973 || r_type == R_386_PC32)))
974 {
975 /* In these cases, we don't need the relocation
976 value. We check specially because in some
977 obscure cases sec->output_section will be NULL. */
978 relocation = 0;
979 }
980 else if (sec->output_section == NULL)
981 {
982 (*_bfd_error_handler)
983 ("%s: warning: unresolvable relocation against symbol `%s' from %s section",
984 bfd_get_filename (input_bfd), h->root.root.string,
985 bfd_get_section_name (input_bfd, input_section));
986 relocation = 0;
987 }
988 else
989 relocation = (h->root.u.def.value
990 + sec->output_section->vma
991 + sec->output_offset);
992 }
993 else if (h->root.type == bfd_link_hash_undefweak)
994 relocation = 0;
995 else if (info->shared && !info->symbolic)
996 relocation = 0;
997 else
998 {
999 if (! ((*info->callbacks->undefined_symbol)
1000 (info, h->root.root.string, input_bfd,
1001 input_section, rel->r_offset)))
1002 return false;
1003 relocation = 0;
1004 }
1005 }
1006
1007 switch (r_type)
1008 {
1009 case R_386_GOT32:
1010 /* Relocation is to the entry for this symbol in the global
1011 offset table. */
1012 if (sgot == NULL)
1013 {
1014 sgot = bfd_get_section_by_name (dynobj, ".got");
1015 BFD_ASSERT (sgot != NULL);
1016 }
1017
1018 if (h != NULL)
1019 {
1020 bfd_vma off;
1021
1022 off = h->got_offset;
1023 BFD_ASSERT (off != (bfd_vma) -1);
1024
1025 if (! elf_hash_table (info)->dynamic_sections_created
1026 || (info->shared
1027 && info->symbolic
1028 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1029 {
1030 /* This is actually a static link, or it is a
1031 -Bsymbolic link and the symbol is defined
1032 locally. We must initialize this entry in the
1033 global offset table. Since the offset must
1034 always be a multiple of 4, we use the least
1035 significant bit to record whether we have
1036 initialized it already.
1037
1038 When doing a dynamic link, we create a .rel.got
1039 relocation entry to initialize the value. This
1040 is done in the finish_dynamic_symbol routine. */
1041 if ((off & 1) != 0)
1042 off &= ~1;
1043 else
1044 {
1045 bfd_put_32 (output_bfd, relocation,
1046 sgot->contents + off);
1047 h->got_offset |= 1;
1048 }
1049 }
1050
1051 relocation = sgot->output_offset + off;
1052 }
1053 else
1054 {
1055 bfd_vma off;
1056
1057 BFD_ASSERT (local_got_offsets != NULL
1058 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1059
1060 off = local_got_offsets[r_symndx];
1061
1062 /* The offset must always be a multiple of 4. We use
1063 the least significant bit to record whether we have
1064 already generated the necessary reloc. */
1065 if ((off & 1) != 0)
1066 off &= ~1;
1067 else
1068 {
1069 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1070
1071 if (info->shared)
1072 {
1073 asection *srelgot;
1074 Elf_Internal_Rel outrel;
1075
1076 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
1077 BFD_ASSERT (srelgot != NULL);
1078
1079 outrel.r_offset = (sgot->output_section->vma
1080 + sgot->output_offset
1081 + off);
1082 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1083 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1084 (((Elf32_External_Rel *)
1085 srelgot->contents)
1086 + srelgot->reloc_count));
1087 ++srelgot->reloc_count;
1088 }
1089
1090 local_got_offsets[r_symndx] |= 1;
1091 }
1092
1093 relocation = sgot->output_offset + off;
1094 }
1095
1096 break;
1097
1098 case R_386_GOTOFF:
1099 /* Relocation is relative to the start of the global offset
1100 table. */
1101
1102 if (sgot == NULL)
1103 {
1104 sgot = bfd_get_section_by_name (dynobj, ".got");
1105 BFD_ASSERT (sgot != NULL);
1106 }
1107
1108 /* Note that sgot->output_offset is not involved in this
1109 calculation. We always want the start of .got. If we
1110 defined _GLOBAL_OFFSET_TABLE in a different way, as is
1111 permitted by the ABI, we might have to change this
1112 calculation. */
1113 relocation -= sgot->output_section->vma;
1114
1115 break;
1116
1117 case R_386_GOTPC:
1118 /* Use global offset table as symbol value. */
1119
1120 if (sgot == NULL)
1121 {
1122 sgot = bfd_get_section_by_name (dynobj, ".got");
1123 BFD_ASSERT (sgot != NULL);
1124 }
1125
1126 relocation = sgot->output_section->vma;
1127
1128 break;
1129
1130 case R_386_PLT32:
1131 /* Relocation is to the entry for this symbol in the
1132 procedure linkage table. */
1133
1134 /* Resolve a PLT32 reloc again a local symbol directly,
1135 without using the procedure linkage table. */
1136 if (h == NULL)
1137 break;
1138
1139 if (h->plt_offset == (bfd_vma) -1)
1140 {
1141 /* We didn't make a PLT entry for this symbol. This
1142 happens when statically linking PIC code, or when
1143 using -Bsymbolic. */
1144 break;
1145 }
1146
1147 if (splt == NULL)
1148 {
1149 splt = bfd_get_section_by_name (dynobj, ".plt");
1150 BFD_ASSERT (splt != NULL);
1151 }
1152
1153 relocation = (splt->output_section->vma
1154 + splt->output_offset
1155 + h->plt_offset);
1156
1157 break;
1158
1159 case R_386_32:
1160 case R_386_PC32:
1161 if (info->shared
1162 && (r_type != R_386_PC32
1163 || (h != NULL
1164 && (! info->symbolic
1165 || (h->elf_link_hash_flags
1166 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1167 {
1168 Elf_Internal_Rel outrel;
1169 boolean relocate;
1170
1171 /* When generating a shared object, these relocations
1172 are copied into the output file to be resolved at run
1173 time. */
1174
1175 if (sreloc == NULL)
1176 {
1177 const char *name;
1178
1179 name = (bfd_elf_string_from_elf_section
1180 (input_bfd,
1181 elf_elfheader (input_bfd)->e_shstrndx,
1182 elf_section_data (input_section)->rel_hdr.sh_name));
1183 if (name == NULL)
1184 return false;
1185
1186 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
1187 && strcmp (bfd_get_section_name (input_bfd,
1188 input_section),
1189 name + 4) == 0);
1190
1191 sreloc = bfd_get_section_by_name (dynobj, name);
1192 BFD_ASSERT (sreloc != NULL);
1193 }
1194
1195 outrel.r_offset = (rel->r_offset
1196 + input_section->output_section->vma
1197 + input_section->output_offset);
1198 if (r_type == R_386_PC32)
1199 {
1200 BFD_ASSERT (h != NULL && h->dynindx != -1);
1201 relocate = false;
1202 outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_PC32);
1203 }
1204 else
1205 {
1206 if (h == NULL
1207 || (info->symbolic
1208 && (h->elf_link_hash_flags
1209 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1210 {
1211 relocate = true;
1212 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1213 }
1214 else
1215 {
1216 BFD_ASSERT (h->dynindx != -1);
1217 relocate = false;
1218 outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_32);
1219 }
1220 }
1221
1222 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1223 (((Elf32_External_Rel *)
1224 sreloc->contents)
1225 + sreloc->reloc_count));
1226 ++sreloc->reloc_count;
1227
1228 /* If this reloc is against an external symbol, we do
1229 not want to fiddle with the addend. Otherwise, we
1230 need to include the symbol value so that it becomes
1231 an addend for the dynamic reloc. */
1232 if (! relocate)
1233 continue;
1234 }
1235
1236 break;
1237
1238 default:
1239 break;
1240 }
1241
1242 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1243 contents, rel->r_offset,
1244 relocation, (bfd_vma) 0);
1245
1246 if (r != bfd_reloc_ok)
1247 {
1248 switch (r)
1249 {
1250 default:
1251 case bfd_reloc_outofrange:
1252 abort ();
1253 case bfd_reloc_overflow:
1254 {
1255 const char *name;
1256
1257 if (h != NULL)
1258 name = h->root.root.string;
1259 else
1260 {
1261 name = bfd_elf_string_from_elf_section (input_bfd,
1262 symtab_hdr->sh_link,
1263 sym->st_name);
1264 if (name == NULL)
1265 return false;
1266 if (*name == '\0')
1267 name = bfd_section_name (input_bfd, sec);
1268 }
1269 if (! ((*info->callbacks->reloc_overflow)
1270 (info, name, howto->name, (bfd_vma) 0,
1271 input_bfd, input_section, rel->r_offset)))
1272 return false;
1273 }
1274 break;
1275 }
1276 }
1277 }
1278
1279 return true;
1280 }
1281
1282 /* Finish up dynamic symbol handling. We set the contents of various
1283 dynamic sections here. */
1284
1285 static boolean
1286 elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
1287 bfd *output_bfd;
1288 struct bfd_link_info *info;
1289 struct elf_link_hash_entry *h;
1290 Elf_Internal_Sym *sym;
1291 {
1292 bfd *dynobj;
1293
1294 dynobj = elf_hash_table (info)->dynobj;
1295
1296 if (h->plt_offset != (bfd_vma) -1)
1297 {
1298 asection *splt;
1299 asection *sgot;
1300 asection *srel;
1301 bfd_vma plt_index;
1302 bfd_vma got_offset;
1303 Elf_Internal_Rel rel;
1304
1305 /* This symbol has an entry in the procedure linkage table. Set
1306 it up. */
1307
1308 BFD_ASSERT (h->dynindx != -1);
1309
1310 splt = bfd_get_section_by_name (dynobj, ".plt");
1311 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1312 srel = bfd_get_section_by_name (dynobj, ".rel.plt");
1313 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1314
1315 /* Get the index in the procedure linkage table which
1316 corresponds to this symbol. This is the index of this symbol
1317 in all the symbols for which we are making plt entries. The
1318 first entry in the procedure linkage table is reserved. */
1319 plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1;
1320
1321 /* Get the offset into the .got table of the entry that
1322 corresponds to this function. Each .got entry is 4 bytes.
1323 The first three are reserved. */
1324 got_offset = (plt_index + 3) * 4;
1325
1326 /* Fill in the entry in the procedure linkage table. */
1327 if (! info->shared)
1328 {
1329 memcpy (splt->contents + h->plt_offset, elf_i386_plt_entry,
1330 PLT_ENTRY_SIZE);
1331 bfd_put_32 (output_bfd,
1332 (sgot->output_section->vma
1333 + sgot->output_offset
1334 + got_offset),
1335 splt->contents + h->plt_offset + 2);
1336 }
1337 else
1338 {
1339 memcpy (splt->contents + h->plt_offset, elf_i386_pic_plt_entry,
1340 PLT_ENTRY_SIZE);
1341 bfd_put_32 (output_bfd, got_offset,
1342 splt->contents + h->plt_offset + 2);
1343 }
1344
1345 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
1346 splt->contents + h->plt_offset + 7);
1347 bfd_put_32 (output_bfd, - (h->plt_offset + PLT_ENTRY_SIZE),
1348 splt->contents + h->plt_offset + 12);
1349
1350 /* Fill in the entry in the global offset table. */
1351 bfd_put_32 (output_bfd,
1352 (splt->output_section->vma
1353 + splt->output_offset
1354 + h->plt_offset
1355 + 6),
1356 sgot->contents + got_offset);
1357
1358 /* Fill in the entry in the .rel.plt section. */
1359 rel.r_offset = (sgot->output_section->vma
1360 + sgot->output_offset
1361 + got_offset);
1362 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
1363 bfd_elf32_swap_reloc_out (output_bfd, &rel,
1364 ((Elf32_External_Rel *) srel->contents
1365 + plt_index));
1366
1367 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1368 {
1369 /* Mark the symbol as undefined, rather than as defined in
1370 the .plt section. Leave the value alone. */
1371 sym->st_shndx = SHN_UNDEF;
1372 }
1373 }
1374
1375 if (h->got_offset != (bfd_vma) -1)
1376 {
1377 asection *sgot;
1378 asection *srel;
1379 Elf_Internal_Rel rel;
1380
1381 /* This symbol has an entry in the global offset table. Set it
1382 up. */
1383
1384 BFD_ASSERT (h->dynindx != -1);
1385
1386 sgot = bfd_get_section_by_name (dynobj, ".got");
1387 srel = bfd_get_section_by_name (dynobj, ".rel.got");
1388 BFD_ASSERT (sgot != NULL && srel != NULL);
1389
1390 rel.r_offset = (sgot->output_section->vma
1391 + sgot->output_offset
1392 + (h->got_offset &~ 1));
1393
1394 /* If this is a -Bsymbolic link, and the symbol is defined
1395 locally, we just want to emit a RELATIVE reloc. The entry in
1396 the global offset table will already have been initialized in
1397 the relocate_section function. */
1398 if (info->shared
1399 && info->symbolic
1400 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1401 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1402 else
1403 {
1404 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
1405 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
1406 }
1407
1408 bfd_elf32_swap_reloc_out (output_bfd, &rel,
1409 ((Elf32_External_Rel *) srel->contents
1410 + srel->reloc_count));
1411 ++srel->reloc_count;
1412 }
1413
1414 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1415 {
1416 asection *s;
1417 Elf_Internal_Rel rel;
1418
1419 /* This symbol needs a copy reloc. Set it up. */
1420
1421 BFD_ASSERT (h->dynindx != -1
1422 && (h->root.type == bfd_link_hash_defined
1423 || h->root.type == bfd_link_hash_defweak));
1424
1425 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1426 ".rel.bss");
1427 BFD_ASSERT (s != NULL);
1428
1429 rel.r_offset = (h->root.u.def.value
1430 + h->root.u.def.section->output_section->vma
1431 + h->root.u.def.section->output_offset);
1432 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
1433 bfd_elf32_swap_reloc_out (output_bfd, &rel,
1434 ((Elf32_External_Rel *) s->contents
1435 + s->reloc_count));
1436 ++s->reloc_count;
1437 }
1438
1439 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1440 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1441 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1442 sym->st_shndx = SHN_ABS;
1443
1444 return true;
1445 }
1446
1447 /* Finish up the dynamic sections. */
1448
1449 static boolean
1450 elf_i386_finish_dynamic_sections (output_bfd, info)
1451 bfd *output_bfd;
1452 struct bfd_link_info *info;
1453 {
1454 bfd *dynobj;
1455 asection *sgot;
1456 asection *sdyn;
1457
1458 dynobj = elf_hash_table (info)->dynobj;
1459
1460 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1461 BFD_ASSERT (sgot != NULL);
1462 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1463
1464 if (elf_hash_table (info)->dynamic_sections_created)
1465 {
1466 asection *splt;
1467 Elf32_External_Dyn *dyncon, *dynconend;
1468
1469 splt = bfd_get_section_by_name (dynobj, ".plt");
1470 BFD_ASSERT (splt != NULL && sdyn != NULL);
1471
1472 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1473 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1474 for (; dyncon < dynconend; dyncon++)
1475 {
1476 Elf_Internal_Dyn dyn;
1477 const char *name;
1478 asection *s;
1479
1480 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1481
1482 switch (dyn.d_tag)
1483 {
1484 default:
1485 break;
1486
1487 case DT_PLTGOT:
1488 name = ".got";
1489 goto get_vma;
1490 case DT_JMPREL:
1491 name = ".rel.plt";
1492 get_vma:
1493 s = bfd_get_section_by_name (output_bfd, name);
1494 BFD_ASSERT (s != NULL);
1495 dyn.d_un.d_ptr = s->vma;
1496 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1497 break;
1498
1499 case DT_PLTRELSZ:
1500 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
1501 BFD_ASSERT (s != NULL);
1502 if (s->_cooked_size != 0)
1503 dyn.d_un.d_val = s->_cooked_size;
1504 else
1505 dyn.d_un.d_val = s->_raw_size;
1506 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1507 break;
1508
1509 case DT_RELSZ:
1510 /* My reading of the SVR4 ABI indicates that the
1511 procedure linkage table relocs (DT_JMPREL) should be
1512 included in the overall relocs (DT_REL). This is
1513 what Solaris does. However, UnixWare can not handle
1514 that case. Therefore, we override the DT_RELSZ entry
1515 here to make it not include the JMPREL relocs. Since
1516 the linker script arranges for .rel.plt to follow all
1517 other relocation sections, we don't have to worry
1518 about changing the DT_REL entry. */
1519 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
1520 if (s != NULL)
1521 {
1522 if (s->_cooked_size != 0)
1523 dyn.d_un.d_val -= s->_cooked_size;
1524 else
1525 dyn.d_un.d_val -= s->_raw_size;
1526 }
1527 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1528 break;
1529 }
1530 }
1531
1532 /* Fill in the first entry in the procedure linkage table. */
1533 if (splt->_raw_size > 0)
1534 {
1535 if (info->shared)
1536 memcpy (splt->contents, elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
1537 else
1538 {
1539 memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE);
1540 bfd_put_32 (output_bfd,
1541 sgot->output_section->vma + sgot->output_offset + 4,
1542 splt->contents + 2);
1543 bfd_put_32 (output_bfd,
1544 sgot->output_section->vma + sgot->output_offset + 8,
1545 splt->contents + 8);
1546 }
1547 }
1548
1549 /* UnixWare sets the entsize of .plt to 4, although that doesn't
1550 really seem like the right value. */
1551 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
1552 }
1553
1554 /* Fill in the first three entries in the global offset table. */
1555 if (sgot->_raw_size > 0)
1556 {
1557 if (sdyn == NULL)
1558 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1559 else
1560 bfd_put_32 (output_bfd,
1561 sdyn->output_section->vma + sdyn->output_offset,
1562 sgot->contents);
1563 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1564 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1565 }
1566
1567 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1568
1569 return true;
1570 }
1571
1572 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
1573 #define TARGET_LITTLE_NAME "elf32-i386"
1574 #define ELF_ARCH bfd_arch_i386
1575 #define ELF_MACHINE_CODE EM_386
1576 #define elf_info_to_howto elf_i386_info_to_howto
1577 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
1578 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
1579 #define ELF_MAXPAGESIZE 0x1000
1580 #define elf_backend_create_dynamic_sections \
1581 _bfd_elf_create_dynamic_sections
1582 #define elf_backend_check_relocs elf_i386_check_relocs
1583 #define elf_backend_adjust_dynamic_symbol \
1584 elf_i386_adjust_dynamic_symbol
1585 #define elf_backend_size_dynamic_sections \
1586 elf_i386_size_dynamic_sections
1587 #define elf_backend_relocate_section elf_i386_relocate_section
1588 #define elf_backend_finish_dynamic_symbol \
1589 elf_i386_finish_dynamic_symbol
1590 #define elf_backend_finish_dynamic_sections \
1591 elf_i386_finish_dynamic_sections
1592 #define elf_backend_want_got_plt 1
1593 #define elf_backend_plt_readonly 1
1594 #define elf_backend_want_plt_sym 0
1595
1596 #include "elf32-target.h"