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