source: trunk/src/binutils/bfd/elf32-i386.c@ 10

Last change on this file since 10 was 10, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 64.1 KB
Line 
1/* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "bfdlink.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26
27static reloc_howto_type *elf_i386_reloc_type_lookup
28 PARAMS ((bfd *, bfd_reloc_code_real_type));
29static void elf_i386_info_to_howto
30 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
31static void elf_i386_info_to_howto_rel
32 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
33static boolean elf_i386_is_local_label_name PARAMS ((bfd *, const char *));
34static struct bfd_hash_entry *elf_i386_link_hash_newfunc
35 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
36static struct bfd_link_hash_table *elf_i386_link_hash_table_create
37 PARAMS ((bfd *));
38static boolean elf_i386_check_relocs
39 PARAMS ((bfd *, struct bfd_link_info *, asection *,
40 const Elf_Internal_Rela *));
41static boolean elf_i386_adjust_dynamic_symbol
42 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
43static boolean elf_i386_size_dynamic_sections
44 PARAMS ((bfd *, struct bfd_link_info *));
45static boolean elf_i386_relocate_section
46 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
47 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
48static boolean elf_i386_finish_dynamic_symbol
49 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
50 Elf_Internal_Sym *));
51static boolean elf_i386_finish_dynamic_sections
52 PARAMS ((bfd *, struct bfd_link_info *));
53
54#define USE_REL 1 /* 386 uses REL relocations instead of RELA */
55
56#include "elf/i386.h"
57
58static reloc_howto_type elf_howto_table[]=
59{
60 HOWTO(R_386_NONE, 0, 0, 0, false, 0, complain_overflow_bitfield,
61 bfd_elf_generic_reloc, "R_386_NONE",
62 true, 0x00000000, 0x00000000, false),
63 HOWTO(R_386_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
64 bfd_elf_generic_reloc, "R_386_32",
65 true, 0xffffffff, 0xffffffff, false),
66 HOWTO(R_386_PC32, 0, 2, 32, true, 0, complain_overflow_bitfield,
67 bfd_elf_generic_reloc, "R_386_PC32",
68 true, 0xffffffff, 0xffffffff, true),
69 HOWTO(R_386_GOT32, 0, 2, 32, false, 0, complain_overflow_bitfield,
70 bfd_elf_generic_reloc, "R_386_GOT32",
71 true, 0xffffffff, 0xffffffff, false),
72 HOWTO(R_386_PLT32, 0, 2, 32, true, 0, complain_overflow_bitfield,
73 bfd_elf_generic_reloc, "R_386_PLT32",
74 true, 0xffffffff, 0xffffffff, true),
75 HOWTO(R_386_COPY, 0, 2, 32, false, 0, complain_overflow_bitfield,
76 bfd_elf_generic_reloc, "R_386_COPY",
77 true, 0xffffffff, 0xffffffff, false),
78 HOWTO(R_386_GLOB_DAT, 0, 2, 32, false, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
80 true, 0xffffffff, 0xffffffff, false),
81 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, false, 0, complain_overflow_bitfield,
82 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
83 true, 0xffffffff, 0xffffffff, false),
84 HOWTO(R_386_RELATIVE, 0, 2, 32, false, 0, complain_overflow_bitfield,
85 bfd_elf_generic_reloc, "R_386_RELATIVE",
86 true, 0xffffffff, 0xffffffff, false),
87 HOWTO(R_386_GOTOFF, 0, 2, 32, false, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_386_GOTOFF",
89 true, 0xffffffff, 0xffffffff, false),
90 HOWTO(R_386_GOTPC, 0, 2, 32, true, 0, complain_overflow_bitfield,
91 bfd_elf_generic_reloc, "R_386_GOTPC",
92 true, 0xffffffff, 0xffffffff, true),
93
94 /* We have a gap in the reloc numbers here.
95 R_386_standard counts the number up to this point, and
96 R_386_ext_offset is the value to subtract from a reloc type of
97 R_386_16 thru R_386_PC8 to form an index into this table. */
98#define R_386_standard ((unsigned int) R_386_GOTPC + 1)
99#define R_386_ext_offset ((unsigned int) R_386_16 - R_386_standard)
100
101 /* The remaining relocs are a GNU extension. */
102 HOWTO(R_386_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
103 bfd_elf_generic_reloc, "R_386_16",
104 true, 0xffff, 0xffff, false),
105 HOWTO(R_386_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield,
106 bfd_elf_generic_reloc, "R_386_PC16",
107 true, 0xffff, 0xffff, true),
108 HOWTO(R_386_8, 0, 0, 8, false, 0, complain_overflow_bitfield,
109 bfd_elf_generic_reloc, "R_386_8",
110 true, 0xff, 0xff, false),
111 HOWTO(R_386_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
112 bfd_elf_generic_reloc, "R_386_PC8",
113 true, 0xff, 0xff, true),
114
115 /* Another gap. */
116#define R_386_ext ((unsigned int) R_386_PC8 + 1 - R_386_ext_offset)
117#define R_386_vt_offset ((unsigned int) R_386_GNU_VTINHERIT - R_386_ext)
118
119/* GNU extension to record C++ vtable hierarchy. */
120 HOWTO (R_386_GNU_VTINHERIT, /* type */
121 0, /* rightshift */
122 2, /* size (0 = byte, 1 = short, 2 = long) */
123 0, /* bitsize */
124 false, /* pc_relative */
125 0, /* bitpos */
126 complain_overflow_dont, /* complain_on_overflow */
127 NULL, /* special_function */
128 "R_386_GNU_VTINHERIT", /* name */
129 false, /* partial_inplace */
130 0, /* src_mask */
131 0, /* dst_mask */
132 false),
133
134/* GNU extension to record C++ vtable member usage. */
135 HOWTO (R_386_GNU_VTENTRY, /* type */
136 0, /* rightshift */
137 2, /* size (0 = byte, 1 = short, 2 = long) */
138 0, /* bitsize */
139 false, /* pc_relative */
140 0, /* bitpos */
141 complain_overflow_dont, /* complain_on_overflow */
142 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
143 "R_386_GNU_VTENTRY", /* name */
144 false, /* partial_inplace */
145 0, /* src_mask */
146 0, /* dst_mask */
147 false)
148
149#define R_386_vt ((unsigned int) R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
150
151};
152
153#ifdef DEBUG_GEN_RELOC
154#define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
155#else
156#define TRACE(str)
157#endif
158
159static reloc_howto_type *
160elf_i386_reloc_type_lookup (abfd, code)
161 bfd *abfd ATTRIBUTE_UNUSED;
162 bfd_reloc_code_real_type code;
163{
164 switch (code)
165 {
166 case BFD_RELOC_NONE:
167 TRACE ("BFD_RELOC_NONE");
168 return &elf_howto_table[(unsigned int) R_386_NONE ];
169
170 case BFD_RELOC_32:
171 TRACE ("BFD_RELOC_32");
172 return &elf_howto_table[(unsigned int) R_386_32 ];
173
174 case BFD_RELOC_CTOR:
175 TRACE ("BFD_RELOC_CTOR");
176 return &elf_howto_table[(unsigned int) R_386_32 ];
177
178 case BFD_RELOC_32_PCREL:
179 TRACE ("BFD_RELOC_PC32");
180 return &elf_howto_table[(unsigned int) R_386_PC32 ];
181
182 case BFD_RELOC_386_GOT32:
183 TRACE ("BFD_RELOC_386_GOT32");
184 return &elf_howto_table[(unsigned int) R_386_GOT32 ];
185
186 case BFD_RELOC_386_PLT32:
187 TRACE ("BFD_RELOC_386_PLT32");
188 return &elf_howto_table[(unsigned int) R_386_PLT32 ];
189
190 case BFD_RELOC_386_COPY:
191 TRACE ("BFD_RELOC_386_COPY");
192 return &elf_howto_table[(unsigned int) R_386_COPY ];
193
194 case BFD_RELOC_386_GLOB_DAT:
195 TRACE ("BFD_RELOC_386_GLOB_DAT");
196 return &elf_howto_table[(unsigned int) R_386_GLOB_DAT ];
197
198 case BFD_RELOC_386_JUMP_SLOT:
199 TRACE ("BFD_RELOC_386_JUMP_SLOT");
200 return &elf_howto_table[(unsigned int) R_386_JUMP_SLOT ];
201
202 case BFD_RELOC_386_RELATIVE:
203 TRACE ("BFD_RELOC_386_RELATIVE");
204 return &elf_howto_table[(unsigned int) R_386_RELATIVE ];
205
206 case BFD_RELOC_386_GOTOFF:
207 TRACE ("BFD_RELOC_386_GOTOFF");
208 return &elf_howto_table[(unsigned int) R_386_GOTOFF ];
209
210 case BFD_RELOC_386_GOTPC:
211 TRACE ("BFD_RELOC_386_GOTPC");
212 return &elf_howto_table[(unsigned int) R_386_GOTPC ];
213
214 /* The remaining relocs are a GNU extension. */
215 case BFD_RELOC_16:
216 TRACE ("BFD_RELOC_16");
217 return &elf_howto_table[(unsigned int) R_386_16 - R_386_ext_offset];
218
219 case BFD_RELOC_16_PCREL:
220 TRACE ("BFD_RELOC_16_PCREL");
221 return &elf_howto_table[(unsigned int) R_386_PC16 - R_386_ext_offset];
222
223 case BFD_RELOC_8:
224 TRACE ("BFD_RELOC_8");
225 return &elf_howto_table[(unsigned int) R_386_8 - R_386_ext_offset];
226
227 case BFD_RELOC_8_PCREL:
228 TRACE ("BFD_RELOC_8_PCREL");
229 return &elf_howto_table[(unsigned int) R_386_PC8 - R_386_ext_offset];
230
231 case BFD_RELOC_VTABLE_INHERIT:
232 TRACE ("BFD_RELOC_VTABLE_INHERIT");
233 return &elf_howto_table[(unsigned int) R_386_GNU_VTINHERIT
234 - R_386_vt_offset];
235
236 case BFD_RELOC_VTABLE_ENTRY:
237 TRACE ("BFD_RELOC_VTABLE_ENTRY");
238 return &elf_howto_table[(unsigned int) R_386_GNU_VTENTRY
239 - R_386_vt_offset];
240
241 default:
242 break;
243 }
244
245 TRACE ("Unknown");
246 return 0;
247}
248
249static void
250elf_i386_info_to_howto (abfd, cache_ptr, dst)
251 bfd *abfd ATTRIBUTE_UNUSED;
252 arelent *cache_ptr ATTRIBUTE_UNUSED;
253 Elf32_Internal_Rela *dst ATTRIBUTE_UNUSED;
254{
255 abort ();
256}
257
258static void
259elf_i386_info_to_howto_rel (abfd, cache_ptr, dst)
260 bfd *abfd ATTRIBUTE_UNUSED;
261 arelent *cache_ptr;
262 Elf32_Internal_Rel *dst;
263{
264 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
265 unsigned int indx;
266
267 if ((indx = r_type) >= R_386_standard
268 && ((indx = r_type - R_386_ext_offset) - R_386_standard
269 >= R_386_ext - R_386_standard)
270 && ((indx = r_type - R_386_vt_offset) - R_386_ext
271 >= R_386_vt - R_386_ext))
272 {
273 (*_bfd_error_handler) (_("%s: invalid relocation type %d"),
274 bfd_get_filename (abfd), (int) r_type);
275 indx = (unsigned int) R_386_NONE;
276 }
277 cache_ptr->howto = &elf_howto_table[indx];
278}
279
280/* Return whether a symbol name implies a local label. The UnixWare
281 2.1 cc generates temporary symbols that start with .X, so we
282 recognize them here. FIXME: do other SVR4 compilers also use .X?.
283 If so, we should move the .X recognition into
284 _bfd_elf_is_local_label_name. */
285
286static boolean
287elf_i386_is_local_label_name (abfd, name)
288 bfd *abfd;
289 const char *name;
290{
291 if (name[0] == '.' && name[1] == 'X')
292 return true;
293
294 return _bfd_elf_is_local_label_name (abfd, name);
295}
296
297
298/* Functions for the i386 ELF linker. */
299
300/* The name of the dynamic interpreter. This is put in the .interp
301 section. */
302
303#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
304
305/* The size in bytes of an entry in the procedure linkage table. */
306
307#define PLT_ENTRY_SIZE 16
308
309/* The first entry in an absolute procedure linkage table looks like
310 this. See the SVR4 ABI i386 supplement to see how this works. */
311
312static const bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
313{
314 0xff, 0x35, /* pushl contents of address */
315 0, 0, 0, 0, /* replaced with address of .got + 4. */
316 0xff, 0x25, /* jmp indirect */
317 0, 0, 0, 0, /* replaced with address of .got + 8. */
318 0, 0, 0, 0 /* pad out to 16 bytes. */
319};
320
321/* Subsequent entries in an absolute procedure linkage table look like
322 this. */
323
324static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
325{
326 0xff, 0x25, /* jmp indirect */
327 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
328 0x68, /* pushl immediate */
329 0, 0, 0, 0, /* replaced with offset into relocation table. */
330 0xe9, /* jmp relative */
331 0, 0, 0, 0 /* replaced with offset to start of .plt. */
332};
333
334/* The first entry in a PIC procedure linkage table look like this. */
335
336static const bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
337{
338 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
339 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
340 0, 0, 0, 0 /* pad out to 16 bytes. */
341};
342
343/* Subsequent entries in a PIC procedure linkage table look like this. */
344
345static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
346{
347 0xff, 0xa3, /* jmp *offset(%ebx) */
348 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
349 0x68, /* pushl immediate */
350 0, 0, 0, 0, /* replaced with offset into relocation table. */
351 0xe9, /* jmp relative */
352 0, 0, 0, 0 /* replaced with offset to start of .plt. */
353};
354
355/* The i386 linker needs to keep track of the number of relocs that it
356 decides to copy in check_relocs for each symbol. This is so that
357 it can discard PC relative relocs if it doesn't need them when
358 linking with -Bsymbolic. We store the information in a field
359 extending the regular ELF linker hash table. */
360
361/* This structure keeps track of the number of PC relative relocs we
362 have copied for a given symbol. */
363
364struct elf_i386_pcrel_relocs_copied
365{
366 /* Next section. */
367 struct elf_i386_pcrel_relocs_copied *next;
368 /* A section in dynobj. */
369 asection *section;
370 /* Number of relocs copied in this section. */
371 bfd_size_type count;
372};
373
374/* i386 ELF linker hash entry. */
375
376struct elf_i386_link_hash_entry
377{
378 struct elf_link_hash_entry root;
379
380 /* Number of PC relative relocs copied for this symbol. */
381 struct elf_i386_pcrel_relocs_copied *pcrel_relocs_copied;
382};
383
384/* i386 ELF linker hash table. */
385
386struct elf_i386_link_hash_table
387{
388 struct elf_link_hash_table root;
389};
390
391/* Declare this now that the above structures are defined. */
392
393static boolean elf_i386_discard_copies
394 PARAMS ((struct elf_i386_link_hash_entry *, PTR));
395
396/* Traverse an i386 ELF linker hash table. */
397
398#define elf_i386_link_hash_traverse(table, func, info) \
399 (elf_link_hash_traverse \
400 (&(table)->root, \
401 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
402 (info)))
403
404/* Get the i386 ELF linker hash table from a link_info structure. */
405
406#define elf_i386_hash_table(p) \
407 ((struct elf_i386_link_hash_table *) ((p)->hash))
408
409/* Create an entry in an i386 ELF linker hash table. */
410
411static struct bfd_hash_entry *
412elf_i386_link_hash_newfunc (entry, table, string)
413 struct bfd_hash_entry *entry;
414 struct bfd_hash_table *table;
415 const char *string;
416{
417 struct elf_i386_link_hash_entry *ret =
418 (struct elf_i386_link_hash_entry *) entry;
419
420 /* Allocate the structure if it has not already been allocated by a
421 subclass. */
422 if (ret == (struct elf_i386_link_hash_entry *) NULL)
423 ret = ((struct elf_i386_link_hash_entry *)
424 bfd_hash_allocate (table,
425 sizeof (struct elf_i386_link_hash_entry)));
426 if (ret == (struct elf_i386_link_hash_entry *) NULL)
427 return (struct bfd_hash_entry *) ret;
428
429 /* Call the allocation method of the superclass. */
430 ret = ((struct elf_i386_link_hash_entry *)
431 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
432 table, string));
433 if (ret != (struct elf_i386_link_hash_entry *) NULL)
434 {
435 ret->pcrel_relocs_copied = NULL;
436 }
437
438 return (struct bfd_hash_entry *) ret;
439}
440
441/* Create an i386 ELF linker hash table. */
442
443static struct bfd_link_hash_table *
444elf_i386_link_hash_table_create (abfd)
445 bfd *abfd;
446{
447 struct elf_i386_link_hash_table *ret;
448
449 ret = ((struct elf_i386_link_hash_table *)
450 bfd_alloc (abfd, sizeof (struct elf_i386_link_hash_table)));
451 if (ret == (struct elf_i386_link_hash_table *) NULL)
452 return NULL;
453
454 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
455 elf_i386_link_hash_newfunc))
456 {
457 bfd_release (abfd, ret);
458 return NULL;
459 }
460
461 return &ret->root.root;
462}
463
464/* Look through the relocs for a section during the first phase, and
465 allocate space in the global offset table or procedure linkage
466 table. */
467
468static boolean
469elf_i386_check_relocs (abfd, info, sec, relocs)
470 bfd *abfd;
471 struct bfd_link_info *info;
472 asection *sec;
473 const Elf_Internal_Rela *relocs;
474{
475 bfd *dynobj;
476 Elf_Internal_Shdr *symtab_hdr;
477 struct elf_link_hash_entry **sym_hashes;
478 bfd_signed_vma *local_got_refcounts;
479 const Elf_Internal_Rela *rel;
480 const Elf_Internal_Rela *rel_end;
481 asection *sgot;
482 asection *srelgot;
483 asection *sreloc;
484
485 if (info->relocateable)
486 return true;
487
488 dynobj = elf_hash_table (info)->dynobj;
489 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
490 sym_hashes = elf_sym_hashes (abfd);
491 local_got_refcounts = elf_local_got_refcounts (abfd);
492
493 sgot = NULL;
494 srelgot = NULL;
495 sreloc = NULL;
496
497 rel_end = relocs + sec->reloc_count;
498 for (rel = relocs; rel < rel_end; rel++)
499 {
500 unsigned long r_symndx;
501 struct elf_link_hash_entry *h;
502
503 r_symndx = ELF32_R_SYM (rel->r_info);
504
505 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
506 {
507 if (abfd->my_archive)
508 (*_bfd_error_handler) (_("%s(%s): bad symbol index: %d"),
509 bfd_get_filename (abfd->my_archive),
510 bfd_get_filename (abfd),
511 r_symndx);
512 else
513 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
514 bfd_get_filename (abfd),
515 r_symndx);
516 return false;
517 }
518
519 if (r_symndx < symtab_hdr->sh_info)
520 h = NULL;
521 else
522 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
523
524 /* Some relocs require a global offset table. */
525 if (dynobj == NULL)
526 {
527 switch (ELF32_R_TYPE (rel->r_info))
528 {
529 case R_386_GOT32:
530 case R_386_GOTOFF:
531 case R_386_GOTPC:
532 elf_hash_table (info)->dynobj = dynobj = abfd;
533 if (! _bfd_elf_create_got_section (dynobj, info))
534 return false;
535 break;
536
537 default:
538 break;
539 }
540 }
541
542 switch (ELF32_R_TYPE (rel->r_info))
543 {
544 case R_386_GOT32:
545 /* This symbol requires a global offset table entry. */
546
547 if (sgot == NULL)
548 {
549 sgot = bfd_get_section_by_name (dynobj, ".got");
550 BFD_ASSERT (sgot != NULL);
551 }
552
553 if (srelgot == NULL
554 && (h != NULL || info->shared))
555 {
556 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
557 if (srelgot == NULL)
558 {
559 srelgot = bfd_make_section (dynobj, ".rel.got");
560 if (srelgot == NULL
561 || ! bfd_set_section_flags (dynobj, srelgot,
562 (SEC_ALLOC
563 | SEC_LOAD
564 | SEC_HAS_CONTENTS
565 | SEC_IN_MEMORY
566 | SEC_LINKER_CREATED
567 | SEC_READONLY))
568 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
569 return false;
570 }
571 }
572
573 if (h != NULL)
574 {
575 if (h->got.refcount == -1)
576 {
577 h->got.refcount = 1;
578
579 /* Make sure this symbol is output as a dynamic symbol. */
580 if (h->dynindx == -1)
581 {
582 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
583 return false;
584 }
585
586 sgot->_raw_size += 4;
587 srelgot->_raw_size += sizeof (Elf32_External_Rel);
588 }
589 else
590 h->got.refcount += 1;
591 }
592 else
593 {
594 /* This is a global offset table entry for a local symbol. */
595 if (local_got_refcounts == NULL)
596 {
597 size_t size;
598
599 size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
600 local_got_refcounts = ((bfd_signed_vma *)
601 bfd_alloc (abfd, size));
602 if (local_got_refcounts == NULL)
603 return false;
604 elf_local_got_refcounts (abfd) = local_got_refcounts;
605 memset (local_got_refcounts, -1, size);
606 }
607 if (local_got_refcounts[r_symndx] == -1)
608 {
609 local_got_refcounts[r_symndx] = 1;
610
611 sgot->_raw_size += 4;
612 if (info->shared)
613 {
614 /* If we are generating a shared object, we need to
615 output a R_386_RELATIVE reloc so that the dynamic
616 linker can adjust this GOT entry. */
617 srelgot->_raw_size += sizeof (Elf32_External_Rel);
618 }
619 }
620 else
621 local_got_refcounts[r_symndx] += 1;
622 }
623 break;
624
625 case R_386_PLT32:
626 /* This symbol requires a procedure linkage table entry. We
627 actually build the entry in adjust_dynamic_symbol,
628 because this might be a case of linking PIC code which is
629 never referenced by a dynamic object, in which case we
630 don't need to generate a procedure linkage table entry
631 after all. */
632
633 /* If this is a local symbol, we resolve it directly without
634 creating a procedure linkage table entry. */
635 if (h == NULL)
636 continue;
637
638 if (h->plt.refcount == -1)
639 {
640 h->plt.refcount = 1;
641 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
642 }
643 else
644 h->plt.refcount += 1;
645 break;
646
647 case R_386_32:
648 case R_386_PC32:
649 if (h != NULL)
650 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
651
652 /* If we are creating a shared library, and this is a reloc
653 against a global symbol, or a non PC relative reloc
654 against a local symbol, then we need to copy the reloc
655 into the shared library. However, if we are linking with
656 -Bsymbolic, we do not need to copy a reloc against a
657 global symbol which is defined in an object we are
658 including in the link (i.e., DEF_REGULAR is set). At
659 this point we have not seen all the input files, so it is
660 possible that DEF_REGULAR is not set now but will be set
661 later (it is never cleared). In case of a weak definition,
662 DEF_REGULAR may be cleared later by a strong definition in
663 a shared library. We account for that possibility below by
664 storing information in the relocs_copied field of the hash
665 table entry. A similar situation occurs when creating
666 shared libraries and symbol visibility changes render the
667 symbol local. */
668 if (info->shared
669 && (sec->flags & SEC_ALLOC) != 0
670 && (ELF32_R_TYPE (rel->r_info) != R_386_PC32
671 || (h != NULL
672 && (! info->symbolic
673 || h->root.type == bfd_link_hash_defweak
674 || (h->elf_link_hash_flags
675 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
676 {
677 /* When creating a shared object, we must copy these
678 reloc types into the output file. We create a reloc
679 section in dynobj and make room for this reloc. */
680 if (sreloc == NULL)
681 {
682 const char *name;
683
684 name = (bfd_elf_string_from_elf_section
685 (abfd,
686 elf_elfheader (abfd)->e_shstrndx,
687 elf_section_data (sec)->rel_hdr.sh_name));
688 if (name == NULL)
689 return false;
690
691 if (strncmp (name, ".rel", 4) != 0
692 || strcmp (bfd_get_section_name (abfd, sec),
693 name + 4) != 0)
694 {
695 if (abfd->my_archive)
696 (*_bfd_error_handler) (_("%s(%s): bad relocation section name `%s\'"),
697 bfd_get_filename (abfd->my_archive),
698 bfd_get_filename (abfd),
699 name);
700 else
701 (*_bfd_error_handler) (_("%s: bad relocation section name `%s\'"),
702 bfd_get_filename (abfd),
703 name);
704 }
705
706 sreloc = bfd_get_section_by_name (dynobj, name);
707 if (sreloc == NULL)
708 {
709 flagword flags;
710
711 sreloc = bfd_make_section (dynobj, name);
712 flags = (SEC_HAS_CONTENTS | SEC_READONLY
713 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
714 if ((sec->flags & SEC_ALLOC) != 0)
715 flags |= SEC_ALLOC | SEC_LOAD;
716 if (sreloc == NULL
717 || ! bfd_set_section_flags (dynobj, sreloc, flags)
718 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
719 return false;
720 }
721 }
722
723 sreloc->_raw_size += sizeof (Elf32_External_Rel);
724
725 /* If this is a global symbol, we count the number of PC
726 relative relocations we have entered for this symbol,
727 so that we can discard them later as necessary. Note
728 that this function is only called if we are using an
729 elf_i386 linker hash table, which means that h is
730 really a pointer to an elf_i386_link_hash_entry. */
731 if (h != NULL
732 && ELF32_R_TYPE (rel->r_info) == R_386_PC32)
733 {
734 struct elf_i386_link_hash_entry *eh;
735 struct elf_i386_pcrel_relocs_copied *p;
736
737 eh = (struct elf_i386_link_hash_entry *) h;
738
739 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
740 if (p->section == sreloc)
741 break;
742
743 if (p == NULL)
744 {
745 p = ((struct elf_i386_pcrel_relocs_copied *)
746 bfd_alloc (dynobj, sizeof *p));
747 if (p == NULL)
748 return false;
749 p->next = eh->pcrel_relocs_copied;
750 eh->pcrel_relocs_copied = p;
751 p->section = sreloc;
752 p->count = 0;
753 }
754
755 ++p->count;
756 }
757 }
758
759 break;
760
761 /* This relocation describes the C++ object vtable hierarchy.
762 Reconstruct it for later use during GC. */
763 case R_386_GNU_VTINHERIT:
764 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
765 return false;
766 break;
767
768 /* This relocation describes which C++ vtable entries are actually
769 used. Record for later use during GC. */
770 case R_386_GNU_VTENTRY:
771 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
772 return false;
773 break;
774
775 default:
776 break;
777 }
778 }
779
780 return true;
781}
782
783/* Return the section that should be marked against GC for a given
784 relocation. */
785
786static asection *
787elf_i386_gc_mark_hook (abfd, info, rel, h, sym)
788 bfd *abfd;
789 struct bfd_link_info *info ATTRIBUTE_UNUSED;
790 Elf_Internal_Rela *rel;
791 struct elf_link_hash_entry *h;
792 Elf_Internal_Sym *sym;
793{
794 if (h != NULL)
795 {
796 switch (ELF32_R_TYPE (rel->r_info))
797 {
798 case R_386_GNU_VTINHERIT:
799 case R_386_GNU_VTENTRY:
800 break;
801
802 default:
803 switch (h->root.type)
804 {
805 case bfd_link_hash_defined:
806 case bfd_link_hash_defweak:
807 return h->root.u.def.section;
808
809 case bfd_link_hash_common:
810 return h->root.u.c.p->section;
811
812 default:
813 break;
814 }
815 }
816 }
817 else
818 {
819 if (!(elf_bad_symtab (abfd)
820 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
821 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
822 && sym->st_shndx != SHN_COMMON))
823 {
824 return bfd_section_from_elf_index (abfd, sym->st_shndx);
825 }
826 }
827
828 return NULL;
829}
830
831/* Update the got entry reference counts for the section being removed. */
832
833static boolean
834elf_i386_gc_sweep_hook (abfd, info, sec, relocs)
835 bfd *abfd;
836 struct bfd_link_info *info;
837 asection *sec;
838 const Elf_Internal_Rela *relocs;
839{
840 Elf_Internal_Shdr *symtab_hdr;
841 struct elf_link_hash_entry **sym_hashes;
842 bfd_signed_vma *local_got_refcounts;
843 const Elf_Internal_Rela *rel, *relend;
844 unsigned long r_symndx;
845 struct elf_link_hash_entry *h;
846 bfd *dynobj;
847 asection *sgot;
848 asection *srelgot;
849
850 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
851 sym_hashes = elf_sym_hashes (abfd);
852 local_got_refcounts = elf_local_got_refcounts (abfd);
853
854 dynobj = elf_hash_table (info)->dynobj;
855 if (dynobj == NULL)
856 return true;
857
858 sgot = bfd_get_section_by_name (dynobj, ".got");
859 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
860
861 relend = relocs + sec->reloc_count;
862 for (rel = relocs; rel < relend; rel++)
863 switch (ELF32_R_TYPE (rel->r_info))
864 {
865 case R_386_GOT32:
866 case R_386_GOTOFF:
867 case R_386_GOTPC:
868 r_symndx = ELF32_R_SYM (rel->r_info);
869 if (r_symndx >= symtab_hdr->sh_info)
870 {
871 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
872 if (h->got.refcount > 0)
873 {
874 h->got.refcount -= 1;
875 if (h->got.refcount == 0)
876 {
877 sgot->_raw_size -= 4;
878 srelgot->_raw_size -= sizeof (Elf32_External_Rel);
879 }
880 }
881 }
882 else if (local_got_refcounts != NULL)
883 {
884 if (local_got_refcounts[r_symndx] > 0)
885 {
886 local_got_refcounts[r_symndx] -= 1;
887 if (local_got_refcounts[r_symndx] == 0)
888 {
889 sgot->_raw_size -= 4;
890 if (info->shared)
891 srelgot->_raw_size -= sizeof (Elf32_External_Rel);
892 }
893 }
894 }
895 break;
896
897 case R_386_PLT32:
898 r_symndx = ELF32_R_SYM (rel->r_info);
899 if (r_symndx >= symtab_hdr->sh_info)
900 {
901 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
902 if (h->plt.refcount > 0)
903 h->plt.refcount -= 1;
904 }
905 break;
906
907 default:
908 break;
909 }
910
911 return true;
912}
913
914/* Adjust a symbol defined by a dynamic object and referenced by a
915 regular object. The current definition is in some section of the
916 dynamic object, but we're not including those sections. We have to
917 change the definition to something the rest of the link can
918 understand. */
919
920static boolean
921elf_i386_adjust_dynamic_symbol (info, h)
922 struct bfd_link_info *info;
923 struct elf_link_hash_entry *h;
924{
925 bfd *dynobj;
926 asection *s;
927 unsigned int power_of_two;
928
929 dynobj = elf_hash_table (info)->dynobj;
930
931 /* Make sure we know what is going on here. */
932 BFD_ASSERT (dynobj != NULL
933 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
934 || h->weakdef != NULL
935 || ((h->elf_link_hash_flags
936 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
937 && (h->elf_link_hash_flags
938 & ELF_LINK_HASH_REF_REGULAR) != 0
939 && (h->elf_link_hash_flags
940 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
941
942 /* If this is a function, put it in the procedure linkage table. We
943 will fill in the contents of the procedure linkage table later,
944 when we know the address of the .got section. */
945 if (h->type == STT_FUNC
946 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
947 {
948 if ((! info->shared
949 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
950 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
951 || (info->shared && h->plt.refcount <= 0))
952 {
953 /* This case can occur if we saw a PLT32 reloc in an input
954 file, but the symbol was never referred to by a dynamic
955 object, or if all references were garbage collected. In
956 such a case, we don't actually need to build a procedure
957 linkage table, and we can just do a PC32 reloc instead. */
958 h->plt.offset = (bfd_vma) -1;
959 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
960 return true;
961 }
962
963 /* Make sure this symbol is output as a dynamic symbol. */
964 if (h->dynindx == -1)
965 {
966 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
967 return false;
968 }
969
970 s = bfd_get_section_by_name (dynobj, ".plt");
971 BFD_ASSERT (s != NULL);
972
973 /* If this is the first .plt entry, make room for the special
974 first entry. */
975 if (s->_raw_size == 0)
976 s->_raw_size += PLT_ENTRY_SIZE;
977
978 /* If this symbol is not defined in a regular file, and we are
979 not generating a shared library, then set the symbol to this
980 location in the .plt. This is required to make function
981 pointers compare as equal between the normal executable and
982 the shared library. */
983 if (! info->shared
984 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
985 {
986 h->root.u.def.section = s;
987 h->root.u.def.value = s->_raw_size;
988 }
989
990 h->plt.offset = s->_raw_size;
991
992 /* Make room for this entry. */
993 s->_raw_size += PLT_ENTRY_SIZE;
994
995 /* We also need to make an entry in the .got.plt section, which
996 will be placed in the .got section by the linker script. */
997 s = bfd_get_section_by_name (dynobj, ".got.plt");
998 BFD_ASSERT (s != NULL);
999 s->_raw_size += 4;
1000
1001 /* We also need to make an entry in the .rel.plt section. */
1002 s = bfd_get_section_by_name (dynobj, ".rel.plt");
1003 BFD_ASSERT (s != NULL);
1004 s->_raw_size += sizeof (Elf32_External_Rel);
1005
1006 return true;
1007 }
1008
1009 /* If this is a weak symbol, and there is a real definition, the
1010 processor independent code will have arranged for us to see the
1011 real definition first, and we can just use the same value. */
1012 if (h->weakdef != NULL)
1013 {
1014 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1015 || h->weakdef->root.type == bfd_link_hash_defweak);
1016 h->root.u.def.section = h->weakdef->root.u.def.section;
1017 h->root.u.def.value = h->weakdef->root.u.def.value;
1018 return true;
1019 }
1020
1021 /* This is a reference to a symbol defined by a dynamic object which
1022 is not a function. */
1023
1024 /* If we are creating a shared library, we must presume that the
1025 only references to the symbol are via the global offset table.
1026 For such cases we need not do anything here; the relocations will
1027 be handled correctly by relocate_section. */
1028 if (info->shared)
1029 return true;
1030
1031 /* If there are no references to this symbol that do not use the
1032 GOT, we don't need to generate a copy reloc. */
1033 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1034 return true;
1035
1036 /* We must allocate the symbol in our .dynbss section, which will
1037 become part of the .bss section of the executable. There will be
1038 an entry for this symbol in the .dynsym section. The dynamic
1039 object will contain position independent code, so all references
1040 from the dynamic object to this symbol will go through the global
1041 offset table. The dynamic linker will use the .dynsym entry to
1042 determine the address it must put in the global offset table, so
1043 both the dynamic object and the regular object will refer to the
1044 same memory location for the variable. */
1045
1046 s = bfd_get_section_by_name (dynobj, ".dynbss");
1047 BFD_ASSERT (s != NULL);
1048
1049 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1050 copy the initial value out of the dynamic object and into the
1051 runtime process image. We need to remember the offset into the
1052 .rel.bss section we are going to use. */
1053 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1054 {
1055 asection *srel;
1056
1057 srel = bfd_get_section_by_name (dynobj, ".rel.bss");
1058 BFD_ASSERT (srel != NULL);
1059 srel->_raw_size += sizeof (Elf32_External_Rel);
1060 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1061 }
1062
1063 /* We need to figure out the alignment required for this symbol. I
1064 have no idea how ELF linkers handle this. */
1065 power_of_two = bfd_log2 (h->size);
1066 if (power_of_two > 3)
1067 power_of_two = 3;
1068
1069 /* Apply the required alignment. */
1070 s->_raw_size = BFD_ALIGN (s->_raw_size,
1071 (bfd_size_type) (1 << power_of_two));
1072 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1073 {
1074 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1075 return false;
1076 }
1077
1078 /* Define the symbol as being at this point in the section. */
1079 h->root.u.def.section = s;
1080 h->root.u.def.value = s->_raw_size;
1081
1082 /* Increment the section size to make room for the symbol. */
1083 s->_raw_size += h->size;
1084
1085 return true;
1086}
1087
1088/* Set the sizes of the dynamic sections. */
1089
1090static boolean
1091elf_i386_size_dynamic_sections (output_bfd, info)
1092 bfd *output_bfd;
1093 struct bfd_link_info *info;
1094{
1095 bfd *dynobj;
1096 asection *s;
1097 boolean plt;
1098 boolean relocs;
1099 boolean reltext;
1100
1101 dynobj = elf_hash_table (info)->dynobj;
1102 BFD_ASSERT (dynobj != NULL);
1103
1104 if (elf_hash_table (info)->dynamic_sections_created)
1105 {
1106 /* Set the contents of the .interp section to the interpreter. */
1107 if (! info->shared)
1108 {
1109 s = bfd_get_section_by_name (dynobj, ".interp");
1110 BFD_ASSERT (s != NULL);
1111 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1112 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1113 }
1114 }
1115 else
1116 {
1117 /* We may have created entries in the .rel.got section.
1118 However, if we are not creating the dynamic sections, we will
1119 not actually use these entries. Reset the size of .rel.got,
1120 which will cause it to get stripped from the output file
1121 below. */
1122 s = bfd_get_section_by_name (dynobj, ".rel.got");
1123 if (s != NULL)
1124 s->_raw_size = 0;
1125 }
1126
1127 /* If this is a -Bsymbolic shared link, then we need to discard all
1128 PC relative relocs against symbols defined in a regular object.
1129 We allocated space for them in the check_relocs routine, but we
1130 will not fill them in in the relocate_section routine. */
1131 if (info->shared)
1132 elf_i386_link_hash_traverse (elf_i386_hash_table (info),
1133 elf_i386_discard_copies,
1134 (PTR) info);
1135
1136 /* The check_relocs and adjust_dynamic_symbol entry points have
1137 determined the sizes of the various dynamic sections. Allocate
1138 memory for them. */
1139 plt = false;
1140 relocs = false;
1141 reltext = false;
1142 for (s = dynobj->sections; s != NULL; s = s->next)
1143 {
1144 const char *name;
1145 boolean strip;
1146
1147 if ((s->flags & SEC_LINKER_CREATED) == 0)
1148 continue;
1149
1150 /* It's OK to base decisions on the section name, because none
1151 of the dynobj section names depend upon the input files. */
1152 name = bfd_get_section_name (dynobj, s);
1153
1154 strip = false;
1155
1156 if (strcmp (name, ".plt") == 0)
1157 {
1158 if (s->_raw_size == 0)
1159 {
1160 /* Strip this section if we don't need it; see the
1161 comment below. */
1162 strip = true;
1163 }
1164 else
1165 {
1166 /* Remember whether there is a PLT. */
1167 plt = true;
1168 }
1169 }
1170 else if (strncmp (name, ".rel", 4) == 0)
1171 {
1172 if (s->_raw_size == 0)
1173 {
1174 /* If we don't need this section, strip it from the
1175 output file. This is mostly to handle .rel.bss and
1176 .rel.plt. We must create both sections in
1177 create_dynamic_sections, because they must be created
1178 before the linker maps input sections to output
1179 sections. The linker does that before
1180 adjust_dynamic_symbol is called, and it is that
1181 function which decides whether anything needs to go
1182 into these sections. */
1183 strip = true;
1184 }
1185 else
1186 {
1187 asection *target;
1188
1189 /* Remember whether there are any reloc sections other
1190 than .rel.plt. */
1191 if (strcmp (name, ".rel.plt") != 0)
1192 {
1193 const char *outname;
1194
1195 relocs = true;
1196
1197 /* If this relocation section applies to a read only
1198 section, then we probably need a DT_TEXTREL
1199 entry. The entries in the .rel.plt section
1200 really apply to the .got section, which we
1201 created ourselves and so know is not readonly. */
1202 outname = bfd_get_section_name (output_bfd,
1203 s->output_section);
1204 target = bfd_get_section_by_name (output_bfd, outname + 4);
1205 if (target != NULL
1206 && (target->flags & SEC_READONLY) != 0
1207 && (target->flags & SEC_ALLOC) != 0)
1208 reltext = true;
1209 }
1210
1211 /* We use the reloc_count field as a counter if we need
1212 to copy relocs into the output file. */
1213 s->reloc_count = 0;
1214 }
1215 }
1216 else if (strncmp (name, ".got", 4) != 0)
1217 {
1218 /* It's not one of our sections, so don't allocate space. */
1219 continue;
1220 }
1221
1222 if (strip)
1223 {
1224 _bfd_strip_section_from_output (info, s);
1225 continue;
1226 }
1227
1228 /* Allocate memory for the section contents. We use bfd_zalloc
1229 here in case unused entries are not reclaimed before the
1230 section's contents are written out. This should not happen,
1231 but this way if it does, we get a R_386_NONE reloc instead
1232 of garbage. */
1233 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1234 if (s->contents == NULL && s->_raw_size != 0)
1235 return false;
1236 }
1237
1238 if (elf_hash_table (info)->dynamic_sections_created)
1239 {
1240 /* Add some entries to the .dynamic section. We fill in the
1241 values later, in elf_i386_finish_dynamic_sections, but we
1242 must add the entries now so that we get the correct size for
1243 the .dynamic section. The DT_DEBUG entry is filled in by the
1244 dynamic linker and used by the debugger. */
1245 if (! info->shared)
1246 {
1247 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1248 return false;
1249 }
1250
1251 if (plt)
1252 {
1253 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
1254 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1255 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
1256 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1257 return false;
1258 }
1259
1260 if (relocs)
1261 {
1262 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0)
1263 || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0)
1264 || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT,
1265 sizeof (Elf32_External_Rel)))
1266 return false;
1267 }
1268
1269 if (reltext)
1270 {
1271 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1272 return false;
1273 info->flags |= DF_TEXTREL;
1274 }
1275 }
1276
1277 return true;
1278}
1279
1280/* This function is called via elf_i386_link_hash_traverse if we are
1281 creating a shared object. In the -Bsymbolic case, it discards the
1282 space allocated to copy PC relative relocs against symbols which
1283 are defined in regular objects. For the normal non-symbolic case,
1284 we also discard space for relocs that have become local due to
1285 symbol visibility changes. We allocated space for them in the
1286 check_relocs routine, but we won't fill them in in the
1287 relocate_section routine. */
1288
1289static boolean
1290elf_i386_discard_copies (h, inf)
1291 struct elf_i386_link_hash_entry *h;
1292 PTR inf;
1293{
1294 struct elf_i386_pcrel_relocs_copied *s;
1295 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1296
1297 /* If a symbol has been forced local or we have found a regular
1298 definition for the symbolic link case, then we won't be needing
1299 any relocs. */
1300 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1301 && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1302 || info->symbolic))
1303 {
1304 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1305 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rel);
1306 }
1307
1308 return true;
1309}
1310
1311/* Relocate an i386 ELF section. */
1312
1313static boolean
1314elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
1315 contents, relocs, local_syms, local_sections)
1316 bfd *output_bfd;
1317 struct bfd_link_info *info;
1318 bfd *input_bfd;
1319 asection *input_section;
1320 bfd_byte *contents;
1321 Elf_Internal_Rela *relocs;
1322 Elf_Internal_Sym *local_syms;
1323 asection **local_sections;
1324{
1325 bfd *dynobj;
1326 Elf_Internal_Shdr *symtab_hdr;
1327 struct elf_link_hash_entry **sym_hashes;
1328 bfd_vma *local_got_offsets;
1329 asection *sgot;
1330 asection *splt;
1331 asection *sreloc;
1332 Elf_Internal_Rela *rel;
1333 Elf_Internal_Rela *relend;
1334
1335 dynobj = elf_hash_table (info)->dynobj;
1336 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1337 sym_hashes = elf_sym_hashes (input_bfd);
1338 local_got_offsets = elf_local_got_offsets (input_bfd);
1339
1340 sreloc = NULL;
1341 splt = NULL;
1342 sgot = NULL;
1343 if (dynobj != NULL)
1344 {
1345 splt = bfd_get_section_by_name (dynobj, ".plt");
1346 sgot = bfd_get_section_by_name (dynobj, ".got");
1347 }
1348
1349 rel = relocs;
1350 relend = relocs + input_section->reloc_count;
1351 for (; rel < relend; rel++)
1352 {
1353 int r_type;
1354 reloc_howto_type *howto;
1355 unsigned long r_symndx;
1356 struct elf_link_hash_entry *h;
1357 Elf_Internal_Sym *sym;
1358 asection *sec;
1359 bfd_vma relocation;
1360 bfd_reloc_status_type r;
1361 unsigned int indx;
1362
1363 r_type = ELF32_R_TYPE (rel->r_info);
1364 if (r_type == (int) R_386_GNU_VTINHERIT
1365 || r_type == (int) R_386_GNU_VTENTRY)
1366 continue;
1367
1368 if ((indx = (unsigned) r_type) >= R_386_standard
1369 && ((indx = (unsigned) r_type - R_386_ext_offset) - R_386_standard
1370 >= R_386_ext - R_386_standard))
1371 {
1372 bfd_set_error (bfd_error_bad_value);
1373 return false;
1374 }
1375 howto = elf_howto_table + indx;
1376
1377 r_symndx = ELF32_R_SYM (rel->r_info);
1378
1379 if (info->relocateable)
1380 {
1381 /* This is a relocateable link. We don't have to change
1382 anything, unless the reloc is against a section symbol,
1383 in which case we have to adjust according to where the
1384 section symbol winds up in the output section. */
1385 if (r_symndx < symtab_hdr->sh_info)
1386 {
1387 sym = local_syms + r_symndx;
1388 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1389 {
1390 bfd_vma val;
1391
1392 sec = local_sections[r_symndx];
1393 val = bfd_get_32 (input_bfd, contents + rel->r_offset);
1394 val += sec->output_offset + sym->st_value;
1395 bfd_put_32 (input_bfd, val, contents + rel->r_offset);
1396 }
1397 }
1398
1399 continue;
1400 }
1401
1402 /* This is a final link. */
1403 h = NULL;
1404 sym = NULL;
1405 sec = NULL;
1406 if (r_symndx < symtab_hdr->sh_info)
1407 {
1408 sym = local_syms + r_symndx;
1409 sec = local_sections[r_symndx];
1410 relocation = (sec->output_section->vma
1411 + sec->output_offset
1412 + sym->st_value);
1413 }
1414 else
1415 {
1416 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1417 while (h->root.type == bfd_link_hash_indirect
1418 || h->root.type == bfd_link_hash_warning)
1419 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1420 if (h->root.type == bfd_link_hash_defined
1421 || h->root.type == bfd_link_hash_defweak)
1422 {
1423 sec = h->root.u.def.section;
1424 if (r_type == R_386_GOTPC
1425 || (r_type == R_386_PLT32
1426 && splt != NULL
1427 && h->plt.offset != (bfd_vma) -1)
1428 || (r_type == R_386_GOT32
1429 && elf_hash_table (info)->dynamic_sections_created
1430 && (! info->shared
1431 || (! info->symbolic && h->dynindx != -1)
1432 || (h->elf_link_hash_flags
1433 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1434 || (info->shared
1435 && ((! info->symbolic && h->dynindx != -1)
1436 || (h->elf_link_hash_flags
1437 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1438 && (r_type == R_386_32
1439 || r_type == R_386_PC32)
1440 && ((input_section->flags & SEC_ALLOC) != 0
1441 /* DWARF will emit R_386_32 relocations in its
1442 sections against symbols defined externally
1443 in shared libraries. We can't do anything
1444 with them here. */
1445 || ((input_section->flags & SEC_DEBUGGING) != 0
1446 && (h->elf_link_hash_flags
1447 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
1448 {
1449 /* In these cases, we don't need the relocation
1450 value. We check specially because in some
1451 obscure cases sec->output_section will be NULL. */
1452 relocation = 0;
1453 }
1454 else if (sec->output_section == NULL)
1455 {
1456 (*_bfd_error_handler)
1457 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1458 bfd_get_filename (input_bfd), h->root.root.string,
1459 bfd_get_section_name (input_bfd, input_section));
1460 relocation = 0;
1461 }
1462 else
1463 relocation = (h->root.u.def.value
1464 + sec->output_section->vma
1465 + sec->output_offset);
1466 }
1467 else if (h->root.type == bfd_link_hash_undefweak)
1468 relocation = 0;
1469 else if (info->shared && !info->symbolic
1470 && !info->no_undefined
1471 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1472 relocation = 0;
1473 else
1474 {
1475 if (! ((*info->callbacks->undefined_symbol)
1476 (info, h->root.root.string, input_bfd,
1477 input_section, rel->r_offset,
1478 (!info->shared || info->no_undefined
1479 || ELF_ST_VISIBILITY (h->other)))))
1480 return false;
1481 relocation = 0;
1482 }
1483 }
1484
1485 switch (r_type)
1486 {
1487 case R_386_GOT32:
1488 /* Relocation is to the entry for this symbol in the global
1489 offset table. */
1490 BFD_ASSERT (sgot != NULL);
1491
1492 if (h != NULL)
1493 {
1494 bfd_vma off;
1495
1496 off = h->got.offset;
1497 BFD_ASSERT (off != (bfd_vma) -1);
1498
1499 if (! elf_hash_table (info)->dynamic_sections_created
1500 || (info->shared
1501 && (info->symbolic || h->dynindx == -1)
1502 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1503 {
1504 /* This is actually a static link, or it is a
1505 -Bsymbolic link and the symbol is defined
1506 locally, or the symbol was forced to be local
1507 because of a version file. We must initialize
1508 this entry in the global offset table. Since the
1509 offset must always be a multiple of 4, we use the
1510 least significant bit to record whether we have
1511 initialized it already.
1512
1513 When doing a dynamic link, we create a .rel.got
1514 relocation entry to initialize the value. This
1515 is done in the finish_dynamic_symbol routine. */
1516 if ((off & 1) != 0)
1517 off &= ~1;
1518 else
1519 {
1520 bfd_put_32 (output_bfd, relocation,
1521 sgot->contents + off);
1522 h->got.offset |= 1;
1523 }
1524 }
1525
1526 relocation = sgot->output_offset + off;
1527 }
1528 else
1529 {
1530 bfd_vma off;
1531
1532 BFD_ASSERT (local_got_offsets != NULL
1533 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1534
1535 off = local_got_offsets[r_symndx];
1536
1537 /* The offset must always be a multiple of 4. We use
1538 the least significant bit to record whether we have
1539 already generated the necessary reloc. */
1540 if ((off & 1) != 0)
1541 off &= ~1;
1542 else
1543 {
1544 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1545
1546 if (info->shared)
1547 {
1548 asection *srelgot;
1549 Elf_Internal_Rel outrel;
1550
1551 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
1552 BFD_ASSERT (srelgot != NULL);
1553
1554 outrel.r_offset = (sgot->output_section->vma
1555 + sgot->output_offset
1556 + off);
1557 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1558 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1559 (((Elf32_External_Rel *)
1560 srelgot->contents)
1561 + srelgot->reloc_count));
1562 ++srelgot->reloc_count;
1563 }
1564
1565 local_got_offsets[r_symndx] |= 1;
1566 }
1567
1568 relocation = sgot->output_offset + off;
1569 }
1570
1571 break;
1572
1573 case R_386_GOTOFF:
1574 /* Relocation is relative to the start of the global offset
1575 table. */
1576
1577 if (sgot == NULL)
1578 {
1579 sgot = bfd_get_section_by_name (dynobj, ".got");
1580 BFD_ASSERT (sgot != NULL);
1581 }
1582
1583 /* Note that sgot->output_offset is not involved in this
1584 calculation. We always want the start of .got. If we
1585 defined _GLOBAL_OFFSET_TABLE in a different way, as is
1586 permitted by the ABI, we might have to change this
1587 calculation. */
1588 relocation -= sgot->output_section->vma;
1589
1590 break;
1591
1592 case R_386_GOTPC:
1593 /* Use global offset table as symbol value. */
1594
1595 if (sgot == NULL)
1596 {
1597 sgot = bfd_get_section_by_name (dynobj, ".got");
1598 BFD_ASSERT (sgot != NULL);
1599 }
1600
1601 relocation = sgot->output_section->vma;
1602
1603 break;
1604
1605 case R_386_PLT32:
1606 /* Relocation is to the entry for this symbol in the
1607 procedure linkage table. */
1608
1609 /* Resolve a PLT32 reloc against a local symbol directly,
1610 without using the procedure linkage table. */
1611 if (h == NULL)
1612 break;
1613
1614 if (h->plt.offset == (bfd_vma) -1
1615 || splt == NULL)
1616 {
1617 /* We didn't make a PLT entry for this symbol. This
1618 happens when statically linking PIC code, or when
1619 using -Bsymbolic. */
1620 break;
1621 }
1622
1623 relocation = (splt->output_section->vma
1624 + splt->output_offset
1625 + h->plt.offset);
1626
1627 break;
1628
1629 case R_386_32:
1630 case R_386_PC32:
1631 if (info->shared
1632 && (input_section->flags & SEC_ALLOC) != 0
1633 && (r_type != R_386_PC32
1634 || (h != NULL
1635 && h->dynindx != -1
1636 && (! info->symbolic
1637 || (h->elf_link_hash_flags
1638 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1639 {
1640 Elf_Internal_Rel outrel;
1641 boolean skip, relocate;
1642
1643 /* When generating a shared object, these relocations
1644 are copied into the output file to be resolved at run
1645 time. */
1646
1647 if (sreloc == NULL)
1648 {
1649 const char *name;
1650
1651 name = (bfd_elf_string_from_elf_section
1652 (input_bfd,
1653 elf_elfheader (input_bfd)->e_shstrndx,
1654 elf_section_data (input_section)->rel_hdr.sh_name));
1655 if (name == NULL)
1656 return false;
1657
1658 if (strncmp (name, ".rel", 4) != 0
1659 || strcmp (bfd_get_section_name (input_bfd,
1660 input_section),
1661 name + 4) != 0)
1662 {
1663 if (input_bfd->my_archive)
1664 (*_bfd_error_handler) (_("%s(%s): bad relocation section name `%s\'"),
1665 bfd_get_filename (input_bfd->my_archive),
1666 bfd_get_filename (input_bfd),
1667 name);
1668 else
1669 (*_bfd_error_handler) (_("%s: bad relocation section name `%s\'"),
1670 bfd_get_filename (input_bfd),
1671 name);
1672 return false;
1673 }
1674
1675 sreloc = bfd_get_section_by_name (dynobj, name);
1676 BFD_ASSERT (sreloc != NULL);
1677 }
1678
1679 skip = false;
1680
1681 if (elf_section_data (input_section)->stab_info == NULL)
1682 outrel.r_offset = rel->r_offset;
1683 else
1684 {
1685 bfd_vma off;
1686
1687 off = (_bfd_stab_section_offset
1688 (output_bfd, &elf_hash_table (info)->stab_info,
1689 input_section,
1690 &elf_section_data (input_section)->stab_info,
1691 rel->r_offset));
1692 if (off == (bfd_vma) -1)
1693 skip = true;
1694 outrel.r_offset = off;
1695 }
1696
1697 outrel.r_offset += (input_section->output_section->vma
1698 + input_section->output_offset);
1699
1700 if (skip)
1701 {
1702 memset (&outrel, 0, sizeof outrel);
1703 relocate = false;
1704 }
1705 else if (r_type == R_386_PC32)
1706 {
1707 BFD_ASSERT (h != NULL && h->dynindx != -1);
1708 relocate = false;
1709 outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_PC32);
1710 }
1711 else
1712 {
1713 /* h->dynindx may be -1 if this symbol was marked to
1714 become local. */
1715 if (h == NULL
1716 || ((info->symbolic || h->dynindx == -1)
1717 && (h->elf_link_hash_flags
1718 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1719 {
1720 relocate = true;
1721 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1722 }
1723 else
1724 {
1725 BFD_ASSERT (h->dynindx != -1);
1726 relocate = false;
1727 outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_32);
1728 }
1729 }
1730
1731 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1732 (((Elf32_External_Rel *)
1733 sreloc->contents)
1734 + sreloc->reloc_count));
1735 ++sreloc->reloc_count;
1736
1737 /* If this reloc is against an external symbol, we do
1738 not want to fiddle with the addend. Otherwise, we
1739 need to include the symbol value so that it becomes
1740 an addend for the dynamic reloc. */
1741 if (! relocate)
1742 continue;
1743 }
1744
1745 break;
1746
1747 default:
1748 break;
1749 }
1750
1751 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1752 contents, rel->r_offset,
1753 relocation, (bfd_vma) 0);
1754
1755 if (r != bfd_reloc_ok)
1756 {
1757 switch (r)
1758 {
1759 default:
1760 case bfd_reloc_outofrange:
1761 abort ();
1762 case bfd_reloc_overflow:
1763 {
1764 const char *name;
1765
1766 if (h != NULL)
1767 name = h->root.root.string;
1768 else
1769 {
1770 name = bfd_elf_string_from_elf_section (input_bfd,
1771 symtab_hdr->sh_link,
1772 sym->st_name);
1773 if (name == NULL)
1774 return false;
1775 if (*name == '\0')
1776 name = bfd_section_name (input_bfd, sec);
1777 }
1778 if (! ((*info->callbacks->reloc_overflow)
1779 (info, name, howto->name, (bfd_vma) 0,
1780 input_bfd, input_section, rel->r_offset)))
1781 return false;
1782 }
1783 break;
1784 }
1785 }
1786 }
1787
1788 return true;
1789}
1790
1791/* Finish up dynamic symbol handling. We set the contents of various
1792 dynamic sections here. */
1793
1794static boolean
1795elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
1796 bfd *output_bfd;
1797 struct bfd_link_info *info;
1798 struct elf_link_hash_entry *h;
1799 Elf_Internal_Sym *sym;
1800{
1801 bfd *dynobj;
1802
1803 dynobj = elf_hash_table (info)->dynobj;
1804
1805 if (h->plt.offset != (bfd_vma) -1)
1806 {
1807 asection *splt;
1808 asection *sgot;
1809 asection *srel;
1810 bfd_vma plt_index;
1811 bfd_vma got_offset;
1812 Elf_Internal_Rel rel;
1813
1814 /* This symbol has an entry in the procedure linkage table. Set
1815 it up. */
1816
1817 BFD_ASSERT (h->dynindx != -1);
1818
1819 splt = bfd_get_section_by_name (dynobj, ".plt");
1820 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1821 srel = bfd_get_section_by_name (dynobj, ".rel.plt");
1822 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1823
1824 /* Get the index in the procedure linkage table which
1825 corresponds to this symbol. This is the index of this symbol
1826 in all the symbols for which we are making plt entries. The
1827 first entry in the procedure linkage table is reserved. */
1828 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1829
1830 /* Get the offset into the .got table of the entry that
1831 corresponds to this function. Each .got entry is 4 bytes.
1832 The first three are reserved. */
1833 got_offset = (plt_index + 3) * 4;
1834
1835 /* Fill in the entry in the procedure linkage table. */
1836 if (! info->shared)
1837 {
1838 memcpy (splt->contents + h->plt.offset, elf_i386_plt_entry,
1839 PLT_ENTRY_SIZE);
1840 bfd_put_32 (output_bfd,
1841 (sgot->output_section->vma
1842 + sgot->output_offset
1843 + got_offset),
1844 splt->contents + h->plt.offset + 2);
1845 }
1846 else
1847 {
1848 memcpy (splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
1849 PLT_ENTRY_SIZE);
1850 bfd_put_32 (output_bfd, got_offset,
1851 splt->contents + h->plt.offset + 2);
1852 }
1853
1854 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
1855 splt->contents + h->plt.offset + 7);
1856 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
1857 splt->contents + h->plt.offset + 12);
1858
1859 /* Fill in the entry in the global offset table. */
1860 bfd_put_32 (output_bfd,
1861 (splt->output_section->vma
1862 + splt->output_offset
1863 + h->plt.offset
1864 + 6),
1865 sgot->contents + got_offset);
1866
1867 /* Fill in the entry in the .rel.plt section. */
1868 rel.r_offset = (sgot->output_section->vma
1869 + sgot->output_offset
1870 + got_offset);
1871 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
1872 bfd_elf32_swap_reloc_out (output_bfd, &rel,
1873 ((Elf32_External_Rel *) srel->contents
1874 + plt_index));
1875
1876 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1877 {
1878 /* Mark the symbol as undefined, rather than as defined in
1879 the .plt section. Leave the value alone. */
1880 sym->st_shndx = SHN_UNDEF;
1881 }
1882 }
1883
1884 if (h->got.offset != (bfd_vma) -1)
1885 {
1886 asection *sgot;
1887 asection *srel;
1888 Elf_Internal_Rel rel;
1889
1890 /* This symbol has an entry in the global offset table. Set it
1891 up. */
1892
1893 sgot = bfd_get_section_by_name (dynobj, ".got");
1894 srel = bfd_get_section_by_name (dynobj, ".rel.got");
1895 BFD_ASSERT (sgot != NULL && srel != NULL);
1896
1897 rel.r_offset = (sgot->output_section->vma
1898 + sgot->output_offset
1899 + (h->got.offset &~ 1));
1900
1901 /* If this is a static link, or it is a -Bsymbolic link and the
1902 symbol is defined locally or was forced to be local because
1903 of a version file, we just want to emit a RELATIVE reloc.
1904 The entry in the global offset table will already have been
1905 initialized in the relocate_section function. */
1906 if (! elf_hash_table (info)->dynamic_sections_created
1907 || (info->shared
1908 && (info->symbolic || h->dynindx == -1)
1909 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1910 {
1911 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
1912 }
1913 else
1914 {
1915 BFD_ASSERT((h->got.offset & 1) == 0);
1916 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1917 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
1918 }
1919
1920 bfd_elf32_swap_reloc_out (output_bfd, &rel,
1921 ((Elf32_External_Rel *) srel->contents
1922 + srel->reloc_count));
1923 ++srel->reloc_count;
1924 }
1925
1926 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1927 {
1928 asection *s;
1929 Elf_Internal_Rel rel;
1930
1931 /* This symbol needs a copy reloc. Set it up. */
1932
1933 BFD_ASSERT (h->dynindx != -1
1934 && (h->root.type == bfd_link_hash_defined
1935 || h->root.type == bfd_link_hash_defweak));
1936
1937 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1938 ".rel.bss");
1939 BFD_ASSERT (s != NULL);
1940
1941 rel.r_offset = (h->root.u.def.value
1942 + h->root.u.def.section->output_section->vma
1943 + h->root.u.def.section->output_offset);
1944 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
1945 bfd_elf32_swap_reloc_out (output_bfd, &rel,
1946 ((Elf32_External_Rel *) s->contents
1947 + s->reloc_count));
1948 ++s->reloc_count;
1949 }
1950
1951 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1952 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1953 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1954 sym->st_shndx = SHN_ABS;
1955
1956 return true;
1957}
1958
1959/* Finish up the dynamic sections. */
1960
1961static boolean
1962elf_i386_finish_dynamic_sections (output_bfd, info)
1963 bfd *output_bfd;
1964 struct bfd_link_info *info;
1965{
1966 bfd *dynobj;
1967 asection *sgot;
1968 asection *sdyn;
1969
1970 dynobj = elf_hash_table (info)->dynobj;
1971
1972 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1973 BFD_ASSERT (sgot != NULL);
1974 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1975
1976 if (elf_hash_table (info)->dynamic_sections_created)
1977 {
1978 asection *splt;
1979 Elf32_External_Dyn *dyncon, *dynconend;
1980
1981 BFD_ASSERT (sdyn != NULL);
1982
1983 dyncon = (Elf32_External_Dyn *) sdyn->contents;
1984 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1985 for (; dyncon < dynconend; dyncon++)
1986 {
1987 Elf_Internal_Dyn dyn;
1988 const char *name;
1989 asection *s;
1990
1991 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1992
1993 switch (dyn.d_tag)
1994 {
1995 default:
1996 break;
1997
1998 case DT_PLTGOT:
1999 name = ".got";
2000 goto get_vma;
2001 case DT_JMPREL:
2002 name = ".rel.plt";
2003 get_vma:
2004 s = bfd_get_section_by_name (output_bfd, name);
2005 BFD_ASSERT (s != NULL);
2006 dyn.d_un.d_ptr = s->vma;
2007 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2008 break;
2009
2010 case DT_PLTRELSZ:
2011 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
2012 BFD_ASSERT (s != NULL);
2013 if (s->_cooked_size != 0)
2014 dyn.d_un.d_val = s->_cooked_size;
2015 else
2016 dyn.d_un.d_val = s->_raw_size;
2017 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2018 break;
2019
2020 case DT_RELSZ:
2021 /* My reading of the SVR4 ABI indicates that the
2022 procedure linkage table relocs (DT_JMPREL) should be
2023 included in the overall relocs (DT_REL). This is
2024 what Solaris does. However, UnixWare can not handle
2025 that case. Therefore, we override the DT_RELSZ entry
2026 here to make it not include the JMPREL relocs. Since
2027 the linker script arranges for .rel.plt to follow all
2028 other relocation sections, we don't have to worry
2029 about changing the DT_REL entry. */
2030 s = bfd_get_section_by_name (output_bfd, ".rel.plt");
2031 if (s != NULL)
2032 {
2033 if (s->_cooked_size != 0)
2034 dyn.d_un.d_val -= s->_cooked_size;
2035 else
2036 dyn.d_un.d_val -= s->_raw_size;
2037 }
2038 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2039 break;
2040 }
2041 }
2042
2043 /* Fill in the first entry in the procedure linkage table. */
2044 splt = bfd_get_section_by_name (dynobj, ".plt");
2045 if (splt && splt->_raw_size > 0)
2046 {
2047 if (info->shared)
2048 memcpy (splt->contents, elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
2049 else
2050 {
2051 memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE);
2052 bfd_put_32 (output_bfd,
2053 sgot->output_section->vma + sgot->output_offset + 4,
2054 splt->contents + 2);
2055 bfd_put_32 (output_bfd,
2056 sgot->output_section->vma + sgot->output_offset + 8,
2057 splt->contents + 8);
2058 }
2059
2060 /* UnixWare sets the entsize of .plt to 4, although that doesn't
2061 really seem like the right value. */
2062 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
2063 }
2064 }
2065
2066 /* Fill in the first three entries in the global offset table. */
2067 if (sgot->_raw_size > 0)
2068 {
2069 if (sdyn == NULL)
2070 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2071 else
2072 bfd_put_32 (output_bfd,
2073 sdyn->output_section->vma + sdyn->output_offset,
2074 sgot->contents);
2075 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2076 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2077 }
2078
2079 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2080
2081 return true;
2082}
2083
2084/* Set the correct type for an x86 ELF section. We do this by the
2085 section name, which is a hack, but ought to work. */
2086
2087static boolean
2088elf_i386_fake_sections (abfd, hdr, sec)
2089 bfd *abfd ATTRIBUTE_UNUSED;
2090 Elf32_Internal_Shdr *hdr;
2091 asection *sec;
2092{
2093 register const char *name;
2094
2095 name = bfd_get_section_name (abfd, sec);
2096
2097 if (strcmp (name, ".reloc") == 0)
2098 /*
2099 * This is an ugly, but unfortunately necessary hack that is
2100 * needed when producing EFI binaries on x86. It tells
2101 * elf.c:elf_fake_sections() not to consider ".reloc" as a section
2102 * containing ELF relocation info. We need this hack in order to
2103 * be able to generate ELF binaries that can be translated into
2104 * EFI applications (which are essentially COFF objects). Those
2105 * files contain a COFF ".reloc" section inside an ELFNN object,
2106 * which would normally cause BFD to segfault because it would
2107 * attempt to interpret this section as containing relocation
2108 * entries for section "oc". With this hack enabled, ".reloc"
2109 * will be treated as a normal data section, which will avoid the
2110 * segfault. However, you won't be able to create an ELFNN binary
2111 * with a section named "oc" that needs relocations, but that's
2112 * the kind of ugly side-effects you get when detecting section
2113 * types based on their names... In practice, this limitation is
2114 * unlikely to bite.
2115 */
2116 hdr->sh_type = SHT_PROGBITS;
2117
2118 return true;
2119}
2120
2121
2122#define TARGET_LITTLE_SYM bfd_elf32_i386_vec
2123#define TARGET_LITTLE_NAME "elf32-i386"
2124#define ELF_ARCH bfd_arch_i386
2125#define ELF_MACHINE_CODE EM_386
2126#define ELF_MAXPAGESIZE 0x1000
2127
2128#define elf_backend_can_gc_sections 1
2129#define elf_backend_want_got_plt 1
2130#define elf_backend_plt_readonly 1
2131#define elf_backend_want_plt_sym 0
2132#define elf_backend_got_header_size 12
2133#define elf_backend_plt_header_size PLT_ENTRY_SIZE
2134
2135#define elf_info_to_howto elf_i386_info_to_howto
2136#define elf_info_to_howto_rel elf_i386_info_to_howto_rel
2137
2138#define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
2139#define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
2140#define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
2141#define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
2142
2143#define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
2144#define elf_backend_check_relocs elf_i386_check_relocs
2145#define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
2146#define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
2147#define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
2148#define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
2149#define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
2150#define elf_backend_relocate_section elf_i386_relocate_section
2151#define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
2152#define elf_backend_fake_sections elf_i386_fake_sections
2153
2154#include "elf32-target.h"
Note: See TracBrowser for help on using the repository browser.