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