bfd/
[external/binutils.git] / 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 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26
27 static reloc_howto_type *elf_i386_reloc_type_lookup
28   PARAMS ((bfd *, bfd_reloc_code_real_type));
29 static void elf_i386_info_to_howto
30   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
31 static void elf_i386_info_to_howto_rel
32   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
33 static boolean elf_i386_is_local_label_name
34   PARAMS ((bfd *, const char *));
35 static boolean elf_i386_grok_prstatus
36   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
37 static boolean elf_i386_grok_psinfo
38   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
39 static struct bfd_hash_entry *link_hash_newfunc
40   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
41 static struct bfd_link_hash_table *elf_i386_link_hash_table_create
42   PARAMS ((bfd *));
43 static boolean create_got_section
44   PARAMS((bfd *, struct bfd_link_info *));
45 static boolean elf_i386_create_dynamic_sections
46   PARAMS((bfd *, struct bfd_link_info *));
47 static void elf_i386_copy_indirect_symbol
48   PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
49            struct elf_link_hash_entry *));
50 static int elf_i386_tls_transition
51   PARAMS ((struct bfd_link_info *, int, int));
52
53 static boolean elf_i386_mkobject
54   PARAMS((bfd *));
55 static boolean elf_i386_object_p
56   PARAMS((bfd *));
57 static boolean elf_i386_check_relocs
58   PARAMS ((bfd *, struct bfd_link_info *, asection *,
59            const Elf_Internal_Rela *));
60 static asection *elf_i386_gc_mark_hook
61   PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
62            struct elf_link_hash_entry *, Elf_Internal_Sym *));
63 static boolean elf_i386_gc_sweep_hook
64   PARAMS ((bfd *, struct bfd_link_info *, asection *,
65            const Elf_Internal_Rela *));
66 static boolean elf_i386_adjust_dynamic_symbol
67   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
68 static boolean allocate_dynrelocs
69   PARAMS ((struct elf_link_hash_entry *, PTR));
70 static boolean readonly_dynrelocs
71   PARAMS ((struct elf_link_hash_entry *, PTR));
72 static boolean elf_i386_fake_sections
73   PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
74 static boolean elf_i386_size_dynamic_sections
75   PARAMS ((bfd *, struct bfd_link_info *));
76 static bfd_vma dtpoff_base
77   PARAMS ((struct bfd_link_info *));
78 static bfd_vma tpoff
79   PARAMS ((struct bfd_link_info *, bfd_vma));
80 static boolean elf_i386_relocate_section
81   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
82            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
83 static boolean elf_i386_finish_dynamic_symbol
84   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
85            Elf_Internal_Sym *));
86 static enum elf_reloc_type_class elf_i386_reloc_type_class
87   PARAMS ((const Elf_Internal_Rela *));
88 static boolean elf_i386_finish_dynamic_sections
89   PARAMS ((bfd *, struct bfd_link_info *));
90
91 #define USE_REL 1               /* 386 uses REL relocations instead of RELA */
92
93 #include "elf/i386.h"
94
95 static reloc_howto_type elf_howto_table[]=
96 {
97   HOWTO(R_386_NONE, 0, 0, 0, false, 0, complain_overflow_bitfield,
98         bfd_elf_generic_reloc, "R_386_NONE",
99         true, 0x00000000, 0x00000000, false),
100   HOWTO(R_386_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
101         bfd_elf_generic_reloc, "R_386_32",
102         true, 0xffffffff, 0xffffffff, false),
103   HOWTO(R_386_PC32, 0, 2, 32, true, 0, complain_overflow_bitfield,
104         bfd_elf_generic_reloc, "R_386_PC32",
105         true, 0xffffffff, 0xffffffff, true),
106   HOWTO(R_386_GOT32, 0, 2, 32, false, 0, complain_overflow_bitfield,
107         bfd_elf_generic_reloc, "R_386_GOT32",
108         true, 0xffffffff, 0xffffffff, false),
109   HOWTO(R_386_PLT32, 0, 2, 32, true, 0, complain_overflow_bitfield,
110         bfd_elf_generic_reloc, "R_386_PLT32",
111         true, 0xffffffff, 0xffffffff, true),
112   HOWTO(R_386_COPY, 0, 2, 32, false, 0, complain_overflow_bitfield,
113         bfd_elf_generic_reloc, "R_386_COPY",
114         true, 0xffffffff, 0xffffffff, false),
115   HOWTO(R_386_GLOB_DAT, 0, 2, 32, false, 0, complain_overflow_bitfield,
116         bfd_elf_generic_reloc, "R_386_GLOB_DAT",
117         true, 0xffffffff, 0xffffffff, false),
118   HOWTO(R_386_JUMP_SLOT, 0, 2, 32, false, 0, complain_overflow_bitfield,
119         bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
120         true, 0xffffffff, 0xffffffff, false),
121   HOWTO(R_386_RELATIVE, 0, 2, 32, false, 0, complain_overflow_bitfield,
122         bfd_elf_generic_reloc, "R_386_RELATIVE",
123         true, 0xffffffff, 0xffffffff, false),
124   HOWTO(R_386_GOTOFF, 0, 2, 32, false, 0, complain_overflow_bitfield,
125         bfd_elf_generic_reloc, "R_386_GOTOFF",
126         true, 0xffffffff, 0xffffffff, false),
127   HOWTO(R_386_GOTPC, 0, 2, 32, true, 0, complain_overflow_bitfield,
128         bfd_elf_generic_reloc, "R_386_GOTPC",
129         true, 0xffffffff, 0xffffffff, true),
130
131   /* We have a gap in the reloc numbers here.
132      R_386_standard counts the number up to this point, and
133      R_386_ext_offset is the value to subtract from a reloc type of
134      R_386_16 thru R_386_PC8 to form an index into this table.  */
135 #define R_386_standard ((unsigned int) R_386_GOTPC + 1)
136 #define R_386_ext_offset ((unsigned int) R_386_TLS_TPOFF - R_386_standard)
137
138   /* These relocs are a GNU extension.  */
139   HOWTO(R_386_TLS_TPOFF, 0, 2, 32, false, 0, complain_overflow_bitfield,
140         bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
141         true, 0xffffffff, 0xffffffff, false),
142   HOWTO(R_386_TLS_IE, 0, 2, 32, false, 0, complain_overflow_bitfield,
143         bfd_elf_generic_reloc, "R_386_TLS_IE",
144         true, 0xffffffff, 0xffffffff, false),
145   HOWTO(R_386_TLS_GOTIE, 0, 2, 32, false, 0, complain_overflow_bitfield,
146         bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
147         true, 0xffffffff, 0xffffffff, false),
148   HOWTO(R_386_TLS_LE, 0, 2, 32, false, 0, complain_overflow_bitfield,
149         bfd_elf_generic_reloc, "R_386_TLS_LE",
150         true, 0xffffffff, 0xffffffff, false),
151   HOWTO(R_386_TLS_GD, 0, 2, 32, false, 0, complain_overflow_bitfield,
152         bfd_elf_generic_reloc, "R_386_TLS_GD",
153         true, 0xffffffff, 0xffffffff, false),
154   HOWTO(R_386_TLS_LDM, 0, 2, 32, false, 0, complain_overflow_bitfield,
155         bfd_elf_generic_reloc, "R_386_TLS_LDM",
156         true, 0xffffffff, 0xffffffff, false),
157   HOWTO(R_386_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
158         bfd_elf_generic_reloc, "R_386_16",
159         true, 0xffff, 0xffff, false),
160   HOWTO(R_386_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield,
161         bfd_elf_generic_reloc, "R_386_PC16",
162         true, 0xffff, 0xffff, true),
163   HOWTO(R_386_8, 0, 0, 8, false, 0, complain_overflow_bitfield,
164         bfd_elf_generic_reloc, "R_386_8",
165         true, 0xff, 0xff, false),
166   HOWTO(R_386_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
167         bfd_elf_generic_reloc, "R_386_PC8",
168         true, 0xff, 0xff, true),
169
170 #define R_386_ext ((unsigned int) R_386_PC8 + 1 - R_386_ext_offset)
171 #define R_386_tls_offset ((unsigned int) R_386_TLS_LDO_32 - R_386_ext)
172   /* These are common with Solaris TLS implementation.  */
173   HOWTO(R_386_TLS_LDO_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
174         bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
175         true, 0xffffffff, 0xffffffff, false),
176   HOWTO(R_386_TLS_IE_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
177         bfd_elf_generic_reloc, "R_386_TLS_IE_32",
178         true, 0xffffffff, 0xffffffff, false),
179   HOWTO(R_386_TLS_LE_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
180         bfd_elf_generic_reloc, "R_386_TLS_LE_32",
181         true, 0xffffffff, 0xffffffff, false),
182   HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, false, 0, complain_overflow_bitfield,
183         bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
184         true, 0xffffffff, 0xffffffff, false),
185   HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, false, 0, complain_overflow_bitfield,
186         bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
187         true, 0xffffffff, 0xffffffff, false),
188   HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, false, 0, complain_overflow_bitfield,
189         bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
190         true, 0xffffffff, 0xffffffff, false),
191
192   /* Another gap.  */
193 #define R_386_tls ((unsigned int) R_386_TLS_TPOFF32 + 1 - R_386_tls_offset)
194 #define R_386_vt_offset ((unsigned int) R_386_GNU_VTINHERIT - R_386_tls)
195
196 /* GNU extension to record C++ vtable hierarchy.  */
197   HOWTO (R_386_GNU_VTINHERIT,   /* type */
198          0,                     /* rightshift */
199          2,                     /* size (0 = byte, 1 = short, 2 = long) */
200          0,                     /* bitsize */
201          false,                 /* pc_relative */
202          0,                     /* bitpos */
203          complain_overflow_dont, /* complain_on_overflow */
204          NULL,                  /* special_function */
205          "R_386_GNU_VTINHERIT", /* name */
206          false,                 /* partial_inplace */
207          0,                     /* src_mask */
208          0,                     /* dst_mask */
209          false),                /* pcrel_offset */
210
211 /* GNU extension to record C++ vtable member usage.  */
212   HOWTO (R_386_GNU_VTENTRY,     /* type */
213          0,                     /* rightshift */
214          2,                     /* size (0 = byte, 1 = short, 2 = long) */
215          0,                     /* bitsize */
216          false,                 /* pc_relative */
217          0,                     /* bitpos */
218          complain_overflow_dont, /* complain_on_overflow */
219          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
220          "R_386_GNU_VTENTRY",   /* name */
221          false,                 /* partial_inplace */
222          0,                     /* src_mask */
223          0,                     /* dst_mask */
224          false)                 /* pcrel_offset */
225
226 #define R_386_vt ((unsigned int) R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
227
228 };
229
230 #ifdef DEBUG_GEN_RELOC
231 #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
232 #else
233 #define TRACE(str)
234 #endif
235
236 static reloc_howto_type *
237 elf_i386_reloc_type_lookup (abfd, code)
238      bfd *abfd ATTRIBUTE_UNUSED;
239      bfd_reloc_code_real_type code;
240 {
241   switch (code)
242     {
243     case BFD_RELOC_NONE:
244       TRACE ("BFD_RELOC_NONE");
245       return &elf_howto_table[(unsigned int) R_386_NONE ];
246
247     case BFD_RELOC_32:
248       TRACE ("BFD_RELOC_32");
249       return &elf_howto_table[(unsigned int) R_386_32 ];
250
251     case BFD_RELOC_CTOR:
252       TRACE ("BFD_RELOC_CTOR");
253       return &elf_howto_table[(unsigned int) R_386_32 ];
254
255     case BFD_RELOC_32_PCREL:
256       TRACE ("BFD_RELOC_PC32");
257       return &elf_howto_table[(unsigned int) R_386_PC32 ];
258
259     case BFD_RELOC_386_GOT32:
260       TRACE ("BFD_RELOC_386_GOT32");
261       return &elf_howto_table[(unsigned int) R_386_GOT32 ];
262
263     case BFD_RELOC_386_PLT32:
264       TRACE ("BFD_RELOC_386_PLT32");
265       return &elf_howto_table[(unsigned int) R_386_PLT32 ];
266
267     case BFD_RELOC_386_COPY:
268       TRACE ("BFD_RELOC_386_COPY");
269       return &elf_howto_table[(unsigned int) R_386_COPY ];
270
271     case BFD_RELOC_386_GLOB_DAT:
272       TRACE ("BFD_RELOC_386_GLOB_DAT");
273       return &elf_howto_table[(unsigned int) R_386_GLOB_DAT ];
274
275     case BFD_RELOC_386_JUMP_SLOT:
276       TRACE ("BFD_RELOC_386_JUMP_SLOT");
277       return &elf_howto_table[(unsigned int) R_386_JUMP_SLOT ];
278
279     case BFD_RELOC_386_RELATIVE:
280       TRACE ("BFD_RELOC_386_RELATIVE");
281       return &elf_howto_table[(unsigned int) R_386_RELATIVE ];
282
283     case BFD_RELOC_386_GOTOFF:
284       TRACE ("BFD_RELOC_386_GOTOFF");
285       return &elf_howto_table[(unsigned int) R_386_GOTOFF ];
286
287     case BFD_RELOC_386_GOTPC:
288       TRACE ("BFD_RELOC_386_GOTPC");
289       return &elf_howto_table[(unsigned int) R_386_GOTPC ];
290
291       /* These relocs are a GNU extension.  */
292     case BFD_RELOC_386_TLS_TPOFF:
293       TRACE ("BFD_RELOC_386_TLS_TPOFF");
294       return &elf_howto_table[(unsigned int) R_386_TLS_TPOFF - R_386_ext_offset];
295
296     case BFD_RELOC_386_TLS_IE:
297       TRACE ("BFD_RELOC_386_TLS_IE");
298       return &elf_howto_table[(unsigned int) R_386_TLS_IE - R_386_ext_offset];
299
300     case BFD_RELOC_386_TLS_GOTIE:
301       TRACE ("BFD_RELOC_386_TLS_GOTIE");
302       return &elf_howto_table[(unsigned int) R_386_TLS_GOTIE - R_386_ext_offset];
303
304     case BFD_RELOC_386_TLS_LE:
305       TRACE ("BFD_RELOC_386_TLS_LE");
306       return &elf_howto_table[(unsigned int) R_386_TLS_LE - R_386_ext_offset];
307
308     case BFD_RELOC_386_TLS_GD:
309       TRACE ("BFD_RELOC_386_TLS_GD");
310       return &elf_howto_table[(unsigned int) R_386_TLS_GD - R_386_ext_offset];
311
312     case BFD_RELOC_386_TLS_LDM:
313       TRACE ("BFD_RELOC_386_TLS_LDM");
314       return &elf_howto_table[(unsigned int) R_386_TLS_LDM - R_386_ext_offset];
315
316     case BFD_RELOC_16:
317       TRACE ("BFD_RELOC_16");
318       return &elf_howto_table[(unsigned int) R_386_16 - R_386_ext_offset];
319
320     case BFD_RELOC_16_PCREL:
321       TRACE ("BFD_RELOC_16_PCREL");
322       return &elf_howto_table[(unsigned int) R_386_PC16 - R_386_ext_offset];
323
324     case BFD_RELOC_8:
325       TRACE ("BFD_RELOC_8");
326       return &elf_howto_table[(unsigned int) R_386_8 - R_386_ext_offset];
327
328     case BFD_RELOC_8_PCREL:
329       TRACE ("BFD_RELOC_8_PCREL");
330       return &elf_howto_table[(unsigned int) R_386_PC8 - R_386_ext_offset];
331
332     /* Common with Sun TLS implementation.  */
333     case BFD_RELOC_386_TLS_LDO_32:
334       TRACE ("BFD_RELOC_386_TLS_LDO_32");
335       return &elf_howto_table[(unsigned int) R_386_TLS_LDO_32 - R_386_tls_offset];
336
337     case BFD_RELOC_386_TLS_IE_32:
338       TRACE ("BFD_RELOC_386_TLS_IE_32");
339       return &elf_howto_table[(unsigned int) R_386_TLS_IE_32 - R_386_tls_offset];
340
341     case BFD_RELOC_386_TLS_LE_32:
342       TRACE ("BFD_RELOC_386_TLS_LE_32");
343       return &elf_howto_table[(unsigned int) R_386_TLS_LE_32 - R_386_tls_offset];
344
345     case BFD_RELOC_386_TLS_DTPMOD32:
346       TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
347       return &elf_howto_table[(unsigned int) R_386_TLS_DTPMOD32 - R_386_tls_offset];
348
349     case BFD_RELOC_386_TLS_DTPOFF32:
350       TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
351       return &elf_howto_table[(unsigned int) R_386_TLS_DTPOFF32 - R_386_tls_offset];
352
353     case BFD_RELOC_386_TLS_TPOFF32:
354       TRACE ("BFD_RELOC_386_TLS_TPOFF32");
355       return &elf_howto_table[(unsigned int) R_386_TLS_TPOFF32 - R_386_tls_offset];
356
357     case BFD_RELOC_VTABLE_INHERIT:
358       TRACE ("BFD_RELOC_VTABLE_INHERIT");
359       return &elf_howto_table[(unsigned int) R_386_GNU_VTINHERIT
360                              - R_386_vt_offset];
361
362     case BFD_RELOC_VTABLE_ENTRY:
363       TRACE ("BFD_RELOC_VTABLE_ENTRY");
364       return &elf_howto_table[(unsigned int) R_386_GNU_VTENTRY
365                              - R_386_vt_offset];
366
367     default:
368       break;
369     }
370
371   TRACE ("Unknown");
372   return 0;
373 }
374
375 static void
376 elf_i386_info_to_howto (abfd, cache_ptr, dst)
377      bfd                *abfd ATTRIBUTE_UNUSED;
378      arelent            *cache_ptr ATTRIBUTE_UNUSED;
379      Elf32_Internal_Rela *dst ATTRIBUTE_UNUSED;
380 {
381   abort ();
382 }
383
384 static void
385 elf_i386_info_to_howto_rel (abfd, cache_ptr, dst)
386      bfd *abfd ATTRIBUTE_UNUSED;
387      arelent *cache_ptr;
388      Elf32_Internal_Rel *dst;
389 {
390   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
391   unsigned int indx;
392
393   if ((indx = r_type) >= R_386_standard
394       && ((indx = r_type - R_386_ext_offset) - R_386_standard
395           >= R_386_ext - R_386_standard)
396       && ((indx = r_type - R_386_tls_offset) - R_386_ext
397           >= R_386_tls - R_386_ext)
398       && ((indx = r_type - R_386_vt_offset) - R_386_tls
399           >= R_386_vt - R_386_tls))
400     {
401       (*_bfd_error_handler) (_("%s: invalid relocation type %d"),
402                              bfd_archive_filename (abfd), (int) r_type);
403       indx = (unsigned int) R_386_NONE;
404     }
405   cache_ptr->howto = &elf_howto_table[indx];
406 }
407
408 /* Return whether a symbol name implies a local label.  The UnixWare
409    2.1 cc generates temporary symbols that start with .X, so we
410    recognize them here.  FIXME: do other SVR4 compilers also use .X?.
411    If so, we should move the .X recognition into
412    _bfd_elf_is_local_label_name.  */
413
414 static boolean
415 elf_i386_is_local_label_name (abfd, name)
416      bfd *abfd;
417      const char *name;
418 {
419   if (name[0] == '.' && name[1] == 'X')
420     return true;
421
422   return _bfd_elf_is_local_label_name (abfd, name);
423 }
424 \f
425 /* Support for core dump NOTE sections.  */
426 static boolean
427 elf_i386_grok_prstatus (abfd, note)
428      bfd *abfd;
429      Elf_Internal_Note *note;
430 {
431   int offset;
432   size_t raw_size;
433
434   switch (note->descsz)
435     {
436       default:
437         return false;
438
439       case 144:         /* Linux/i386 */
440         /* pr_cursig */
441         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
442
443         /* pr_pid */
444         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
445
446         /* pr_reg */
447         offset = 72;
448         raw_size = 68;
449
450         break;
451     }
452
453   /* Make a ".reg/999" section.  */
454   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
455                                           raw_size, note->descpos + offset);
456 }
457
458 static boolean
459 elf_i386_grok_psinfo (abfd, note)
460      bfd *abfd;
461      Elf_Internal_Note *note;
462 {
463   switch (note->descsz)
464     {
465       default:
466         return false;
467
468       case 124:         /* Linux/i386 elf_prpsinfo */
469         elf_tdata (abfd)->core_program
470          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
471         elf_tdata (abfd)->core_command
472          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
473     }
474
475   /* Note that for some reason, a spurious space is tacked
476      onto the end of the args in some (at least one anyway)
477      implementations, so strip it off if it exists.  */
478
479   {
480     char *command = elf_tdata (abfd)->core_command;
481     int n = strlen (command);
482
483     if (0 < n && command[n - 1] == ' ')
484       command[n - 1] = '\0';
485   }
486
487   return true;
488 }
489 \f
490 /* Functions for the i386 ELF linker.
491
492    In order to gain some understanding of code in this file without
493    knowing all the intricate details of the linker, note the
494    following:
495
496    Functions named elf_i386_* are called by external routines, other
497    functions are only called locally.  elf_i386_* functions appear
498    in this file more or less in the order in which they are called
499    from external routines.  eg. elf_i386_check_relocs is called
500    early in the link process, elf_i386_finish_dynamic_sections is
501    one of the last functions.  */
502
503
504 /* The name of the dynamic interpreter.  This is put in the .interp
505    section.  */
506
507 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
508
509 /* The size in bytes of an entry in the procedure linkage table.  */
510
511 #define PLT_ENTRY_SIZE 16
512
513 /* The first entry in an absolute procedure linkage table looks like
514    this.  See the SVR4 ABI i386 supplement to see how this works.  */
515
516 static const bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
517 {
518   0xff, 0x35,   /* pushl contents of address */
519   0, 0, 0, 0,   /* replaced with address of .got + 4.  */
520   0xff, 0x25,   /* jmp indirect */
521   0, 0, 0, 0,   /* replaced with address of .got + 8.  */
522   0, 0, 0, 0    /* pad out to 16 bytes.  */
523 };
524
525 /* Subsequent entries in an absolute procedure linkage table look like
526    this.  */
527
528 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
529 {
530   0xff, 0x25,   /* jmp indirect */
531   0, 0, 0, 0,   /* replaced with address of this symbol in .got.  */
532   0x68,         /* pushl immediate */
533   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
534   0xe9,         /* jmp relative */
535   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
536 };
537
538 /* The first entry in a PIC procedure linkage table look like this.  */
539
540 static const bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
541 {
542   0xff, 0xb3, 4, 0, 0, 0,       /* pushl 4(%ebx) */
543   0xff, 0xa3, 8, 0, 0, 0,       /* jmp *8(%ebx) */
544   0, 0, 0, 0                    /* pad out to 16 bytes.  */
545 };
546
547 /* Subsequent entries in a PIC procedure linkage table look like this.  */
548
549 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
550 {
551   0xff, 0xa3,   /* jmp *offset(%ebx) */
552   0, 0, 0, 0,   /* replaced with offset of this symbol in .got.  */
553   0x68,         /* pushl immediate */
554   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
555   0xe9,         /* jmp relative */
556   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
557 };
558
559 /* The i386 linker needs to keep track of the number of relocs that it
560    decides to copy as dynamic relocs in check_relocs for each symbol.
561    This is so that it can later discard them if they are found to be
562    unnecessary.  We store the information in a field extending the
563    regular ELF linker hash table.  */
564
565 struct elf_i386_dyn_relocs
566 {
567   struct elf_i386_dyn_relocs *next;
568
569   /* The input section of the reloc.  */
570   asection *sec;
571
572   /* Total number of relocs copied for the input section.  */
573   bfd_size_type count;
574
575   /* Number of pc-relative relocs copied for the input section.  */
576   bfd_size_type pc_count;
577 };
578
579 /* i386 ELF linker hash entry.  */
580
581 struct elf_i386_link_hash_entry
582 {
583   struct elf_link_hash_entry elf;
584
585   /* Track dynamic relocs copied for this symbol.  */
586   struct elf_i386_dyn_relocs *dyn_relocs;
587
588 #define GOT_UNKNOWN     0
589 #define GOT_NORMAL      1
590 #define GOT_TLS_GD      2
591 #define GOT_TLS_IE      4
592 #define GOT_TLS_IE_POS  5
593 #define GOT_TLS_IE_NEG  6
594 #define GOT_TLS_IE_BOTH 7
595   unsigned char tls_type;
596 };
597
598 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
599
600 struct elf_i386_obj_tdata
601 {
602   struct elf_obj_tdata root;
603
604   /* tls_type for each local got entry.  */
605   char *local_got_tls_type;
606 };
607
608 #define elf_i386_tdata(abfd) \
609   ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
610
611 #define elf_i386_local_got_tls_type(abfd) \
612   (elf_i386_tdata (abfd)->local_got_tls_type)
613
614 static boolean
615 elf_i386_mkobject (abfd)
616      bfd *abfd;
617 {
618   bfd_size_type amt = sizeof (struct elf_i386_obj_tdata);
619   abfd->tdata.any = bfd_zalloc (abfd, amt);
620   if (abfd->tdata.any == NULL)
621     return false;
622   return true;
623 }
624
625 static boolean
626 elf_i386_object_p (abfd)
627   bfd *abfd;
628 {
629   /* Allocate our special target data.  */
630   struct elf_i386_obj_tdata *new_tdata;
631   bfd_size_type amt = sizeof (struct elf_i386_obj_tdata);
632   new_tdata = bfd_zalloc (abfd, amt);
633   if (new_tdata == NULL)
634     return false;
635   new_tdata->root = *abfd->tdata.elf_obj_data;
636   abfd->tdata.any = new_tdata;
637   return true;
638 }
639
640 /* i386 ELF linker hash table.  */
641
642 struct elf_i386_link_hash_table
643 {
644   struct elf_link_hash_table elf;
645
646   /* Short-cuts to get to dynamic linker sections.  */
647   asection *sgot;
648   asection *sgotplt;
649   asection *srelgot;
650   asection *splt;
651   asection *srelplt;
652   asection *sdynbss;
653   asection *srelbss;
654
655   union {
656     bfd_signed_vma refcount;
657     bfd_vma offset;
658   } tls_ldm_got;
659
660   /* Small local sym to section mapping cache.  */
661   struct sym_sec_cache sym_sec;
662 };
663
664 /* Get the i386 ELF linker hash table from a link_info structure.  */
665
666 #define elf_i386_hash_table(p) \
667   ((struct elf_i386_link_hash_table *) ((p)->hash))
668
669 /* Create an entry in an i386 ELF linker hash table.  */
670
671 static struct bfd_hash_entry *
672 link_hash_newfunc (entry, table, string)
673      struct bfd_hash_entry *entry;
674      struct bfd_hash_table *table;
675      const char *string;
676 {
677   /* Allocate the structure if it has not already been allocated by a
678      subclass.  */
679   if (entry == NULL)
680     {
681       entry = bfd_hash_allocate (table,
682                                  sizeof (struct elf_i386_link_hash_entry));
683       if (entry == NULL)
684         return entry;
685     }
686
687   /* Call the allocation method of the superclass.  */
688   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
689   if (entry != NULL)
690     {
691       struct elf_i386_link_hash_entry *eh;
692
693       eh = (struct elf_i386_link_hash_entry *) entry;
694       eh->dyn_relocs = NULL;
695       eh->tls_type = GOT_UNKNOWN;
696     }
697
698   return entry;
699 }
700
701 /* Create an i386 ELF linker hash table.  */
702
703 static struct bfd_link_hash_table *
704 elf_i386_link_hash_table_create (abfd)
705      bfd *abfd;
706 {
707   struct elf_i386_link_hash_table *ret;
708   bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
709
710   ret = (struct elf_i386_link_hash_table *) bfd_malloc (amt);
711   if (ret == NULL)
712     return NULL;
713
714   if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
715     {
716       free (ret);
717       return NULL;
718     }
719
720   ret->sgot = NULL;
721   ret->sgotplt = NULL;
722   ret->srelgot = NULL;
723   ret->splt = NULL;
724   ret->srelplt = NULL;
725   ret->sdynbss = NULL;
726   ret->srelbss = NULL;
727   ret->tls_ldm_got.refcount = 0;
728   ret->sym_sec.abfd = NULL;
729
730   return &ret->elf.root;
731 }
732
733 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
734    shortcuts to them in our hash table.  */
735
736 static boolean
737 create_got_section (dynobj, info)
738      bfd *dynobj;
739      struct bfd_link_info *info;
740 {
741   struct elf_i386_link_hash_table *htab;
742
743   if (! _bfd_elf_create_got_section (dynobj, info))
744     return false;
745
746   htab = elf_i386_hash_table (info);
747   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
748   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
749   if (!htab->sgot || !htab->sgotplt)
750     abort ();
751
752   htab->srelgot = bfd_make_section (dynobj, ".rel.got");
753   if (htab->srelgot == NULL
754       || ! bfd_set_section_flags (dynobj, htab->srelgot,
755                                   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
756                                    | SEC_IN_MEMORY | SEC_LINKER_CREATED
757                                    | SEC_READONLY))
758       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
759     return false;
760   return true;
761 }
762
763 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
764    .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
765    hash table.  */
766
767 static boolean
768 elf_i386_create_dynamic_sections (dynobj, info)
769      bfd *dynobj;
770      struct bfd_link_info *info;
771 {
772   struct elf_i386_link_hash_table *htab;
773
774   htab = elf_i386_hash_table (info);
775   if (!htab->sgot && !create_got_section (dynobj, info))
776     return false;
777
778   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
779     return false;
780
781   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
782   htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
783   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
784   if (!info->shared)
785     htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
786
787   if (!htab->splt || !htab->srelplt || !htab->sdynbss
788       || (!info->shared && !htab->srelbss))
789     abort ();
790
791   return true;
792 }
793
794 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
795
796 static void
797 elf_i386_copy_indirect_symbol (bed, dir, ind)
798      struct elf_backend_data *bed;
799      struct elf_link_hash_entry *dir, *ind;
800 {
801   struct elf_i386_link_hash_entry *edir, *eind;
802
803   edir = (struct elf_i386_link_hash_entry *) dir;
804   eind = (struct elf_i386_link_hash_entry *) ind;
805
806   if (eind->dyn_relocs != NULL)
807     {
808       if (edir->dyn_relocs != NULL)
809         {
810           struct elf_i386_dyn_relocs **pp;
811           struct elf_i386_dyn_relocs *p;
812
813           if (ind->root.type == bfd_link_hash_indirect)
814             abort ();
815
816           /* Add reloc counts against the weak sym to the strong sym
817              list.  Merge any entries against the same section.  */
818           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
819             {
820               struct elf_i386_dyn_relocs *q;
821
822               for (q = edir->dyn_relocs; q != NULL; q = q->next)
823                 if (q->sec == p->sec)
824                   {
825                     q->pc_count += p->pc_count;
826                     q->count += p->count;
827                     *pp = p->next;
828                     break;
829                   }
830               if (q == NULL)
831                 pp = &p->next;
832             }
833           *pp = edir->dyn_relocs;
834         }
835
836       edir->dyn_relocs = eind->dyn_relocs;
837       eind->dyn_relocs = NULL;
838     }
839
840   if (ind->root.type == bfd_link_hash_indirect
841       && dir->got.refcount <= 0)
842     {
843       edir->tls_type = eind->tls_type;
844       eind->tls_type = GOT_UNKNOWN;
845     }
846   _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
847 }
848
849 static int
850 elf_i386_tls_transition (info, r_type, is_local)
851      struct bfd_link_info *info;
852      int r_type;
853      int is_local;
854 {
855   if (info->shared)
856     return r_type;
857
858   switch (r_type)
859     {
860     case R_386_TLS_GD:
861     case R_386_TLS_IE_32:
862       if (is_local)
863         return R_386_TLS_LE_32;
864       return R_386_TLS_IE_32;
865     case R_386_TLS_IE:
866     case R_386_TLS_GOTIE:
867       if (is_local)
868         return R_386_TLS_LE_32;
869       return r_type;
870     case R_386_TLS_LDM:
871       return R_386_TLS_LE_32;
872     }
873
874   return r_type;
875 }
876
877 /* Look through the relocs for a section during the first phase, and
878    calculate needed space in the global offset table, procedure linkage
879    table, and dynamic reloc sections.  */
880
881 static boolean
882 elf_i386_check_relocs (abfd, info, sec, relocs)
883      bfd *abfd;
884      struct bfd_link_info *info;
885      asection *sec;
886      const Elf_Internal_Rela *relocs;
887 {
888   struct elf_i386_link_hash_table *htab;
889   Elf_Internal_Shdr *symtab_hdr;
890   struct elf_link_hash_entry **sym_hashes;
891   const Elf_Internal_Rela *rel;
892   const Elf_Internal_Rela *rel_end;
893   asection *sreloc;
894
895   if (info->relocateable)
896     return true;
897
898   htab = elf_i386_hash_table (info);
899   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
900   sym_hashes = elf_sym_hashes (abfd);
901
902   sreloc = NULL;
903
904   rel_end = relocs + sec->reloc_count;
905   for (rel = relocs; rel < rel_end; rel++)
906     {
907       unsigned int r_type;
908       unsigned long r_symndx;
909       struct elf_link_hash_entry *h;
910
911       r_symndx = ELF32_R_SYM (rel->r_info);
912       r_type = ELF32_R_TYPE (rel->r_info);
913
914       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
915         {
916           (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
917                                  bfd_archive_filename (abfd),
918                                  r_symndx);
919           return false;
920         }
921
922       if (r_symndx < symtab_hdr->sh_info)
923         h = NULL;
924       else
925         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
926
927       r_type = elf_i386_tls_transition (info, r_type, h == NULL);
928
929       switch (r_type)
930         {
931         case R_386_TLS_LDM:
932           htab->tls_ldm_got.refcount += 1;
933           goto create_got;
934
935         case R_386_PLT32:
936           /* This symbol requires a procedure linkage table entry.  We
937              actually build the entry in adjust_dynamic_symbol,
938              because this might be a case of linking PIC code which is
939              never referenced by a dynamic object, in which case we
940              don't need to generate a procedure linkage table entry
941              after all.  */
942
943           /* If this is a local symbol, we resolve it directly without
944              creating a procedure linkage table entry.  */
945           if (h == NULL)
946             continue;
947
948           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
949           h->plt.refcount += 1;
950           break;
951
952         case R_386_TLS_IE_32:
953         case R_386_TLS_IE:
954         case R_386_TLS_GOTIE:
955           if (info->shared)
956             info->flags |= DF_STATIC_TLS;
957           /* Fall through */
958
959         case R_386_GOT32:
960         case R_386_TLS_GD:
961           /* This symbol requires a global offset table entry.  */
962           {
963             int tls_type, old_tls_type;
964
965             switch (r_type)
966               {
967               default:
968               case R_386_GOT32: tls_type = GOT_NORMAL; break;
969               case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
970               case R_386_TLS_IE_32:
971                 if (ELF32_R_TYPE (rel->r_info) == r_type)
972                   tls_type = GOT_TLS_IE_NEG;
973                 else
974                   /* If this is a GD->IE transition, we may use either of
975                      R_386_TLS_TPOFF and R_386_TLS_TPOFF32.  */
976                   tls_type = GOT_TLS_IE;
977                 break;
978               case R_386_TLS_IE:
979               case R_386_TLS_GOTIE:
980                 tls_type = GOT_TLS_IE_POS; break;
981               }
982
983             if (h != NULL)
984               {
985                 h->got.refcount += 1;
986                 old_tls_type = elf_i386_hash_entry(h)->tls_type;
987               }
988             else
989               {
990                 bfd_signed_vma *local_got_refcounts;
991
992                 /* This is a global offset table entry for a local symbol.  */
993                 local_got_refcounts = elf_local_got_refcounts (abfd);
994                 if (local_got_refcounts == NULL)
995                   {
996                     bfd_size_type size;
997
998                     size = symtab_hdr->sh_info;
999                     size *= (sizeof (bfd_signed_vma) + sizeof(char));
1000                     local_got_refcounts = ((bfd_signed_vma *)
1001                                            bfd_zalloc (abfd, size));
1002                     if (local_got_refcounts == NULL)
1003                       return false;
1004                     elf_local_got_refcounts (abfd) = local_got_refcounts;
1005                     elf_i386_local_got_tls_type (abfd)
1006                       = (char *) (local_got_refcounts + symtab_hdr->sh_info);
1007                   }
1008                 local_got_refcounts[r_symndx] += 1;
1009                 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1010               }
1011
1012             if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1013               tls_type |= old_tls_type;
1014             /* If a TLS symbol is accessed using IE at least once,
1015                there is no point to use dynamic model for it.  */
1016             else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1017                      && (old_tls_type != GOT_TLS_GD
1018                          || (tls_type & GOT_TLS_IE) == 0))
1019               {
1020                 if ((old_tls_type & GOT_TLS_IE) && tls_type == GOT_TLS_GD)
1021                   tls_type = old_tls_type;
1022                 else
1023                   {
1024                     (*_bfd_error_handler)
1025                       (_("%s: `%s' accessed both as normal and thread local symbol"),
1026                        bfd_archive_filename (abfd),
1027                        h ? h->root.root.string : "<local>");
1028                     return false;
1029                   }
1030               }
1031
1032             if (old_tls_type != tls_type)
1033               {
1034                 if (h != NULL)
1035                   elf_i386_hash_entry (h)->tls_type = tls_type;
1036                 else
1037                   elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1038               }
1039           }
1040           /* Fall through */
1041
1042         case R_386_GOTOFF:
1043         case R_386_GOTPC:
1044         create_got:
1045           if (htab->sgot == NULL)
1046             {
1047               if (htab->elf.dynobj == NULL)
1048                 htab->elf.dynobj = abfd;
1049               if (!create_got_section (htab->elf.dynobj, info))
1050                 return false;
1051             }
1052           if (r_type != R_386_TLS_IE)
1053             break;
1054           /* Fall through */
1055
1056         case R_386_TLS_LE_32:
1057         case R_386_TLS_LE:
1058           if (!info->shared)
1059             break;
1060           /* Fall through */        
1061
1062         case R_386_32:
1063         case R_386_PC32:
1064           if (h != NULL && !info->shared)
1065             {
1066               /* If this reloc is in a read-only section, we might
1067                  need a copy reloc.  We can't check reliably at this
1068                  stage whether the section is read-only, as input
1069                  sections have not yet been mapped to output sections.
1070                  Tentatively set the flag for now, and correct in
1071                  adjust_dynamic_symbol.  */
1072               h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
1073
1074               /* We may need a .plt entry if the function this reloc
1075                  refers to is in a shared lib.  */
1076               h->plt.refcount += 1;
1077             }
1078
1079           /* If we are creating a shared library, and this is a reloc
1080              against a global symbol, or a non PC relative reloc
1081              against a local symbol, then we need to copy the reloc
1082              into the shared library.  However, if we are linking with
1083              -Bsymbolic, we do not need to copy a reloc against a
1084              global symbol which is defined in an object we are
1085              including in the link (i.e., DEF_REGULAR is set).  At
1086              this point we have not seen all the input files, so it is
1087              possible that DEF_REGULAR is not set now but will be set
1088              later (it is never cleared).  In case of a weak definition,
1089              DEF_REGULAR may be cleared later by a strong definition in
1090              a shared library.  We account for that possibility below by
1091              storing information in the relocs_copied field of the hash
1092              table entry.  A similar situation occurs when creating
1093              shared libraries and symbol visibility changes render the
1094              symbol local.
1095
1096              If on the other hand, we are creating an executable, we
1097              may need to keep relocations for symbols satisfied by a
1098              dynamic library if we manage to avoid copy relocs for the
1099              symbol.  */
1100           if ((info->shared
1101                && (sec->flags & SEC_ALLOC) != 0
1102                && (r_type != R_386_PC32
1103                    || (h != NULL
1104                        && (! info->symbolic
1105                            || h->root.type == bfd_link_hash_defweak
1106                            || (h->elf_link_hash_flags
1107                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1108               || (!info->shared
1109                   && (sec->flags & SEC_ALLOC) != 0
1110                   && h != NULL
1111                   && (h->root.type == bfd_link_hash_defweak
1112                       || (h->elf_link_hash_flags
1113                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1114             {
1115               struct elf_i386_dyn_relocs *p;
1116               struct elf_i386_dyn_relocs **head;
1117
1118               /* We must copy these reloc types into the output file.
1119                  Create a reloc section in dynobj and make room for
1120                  this reloc.  */
1121               if (sreloc == NULL)
1122                 {
1123                   const char *name;
1124                   bfd *dynobj;
1125                   unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1126                   unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
1127
1128                   name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1129                   if (name == NULL)
1130                     return false;
1131
1132                   if (strncmp (name, ".rel", 4) != 0
1133                       || strcmp (bfd_get_section_name (abfd, sec),
1134                                  name + 4) != 0)
1135                     {
1136                       (*_bfd_error_handler)
1137                         (_("%s: bad relocation section name `%s\'"),
1138                          bfd_archive_filename (abfd), name);
1139                     }
1140
1141                   if (htab->elf.dynobj == NULL)
1142                     htab->elf.dynobj = abfd;
1143
1144                   dynobj = htab->elf.dynobj;
1145                   sreloc = bfd_get_section_by_name (dynobj, name);
1146                   if (sreloc == NULL)
1147                     {
1148                       flagword flags;
1149
1150                       sreloc = bfd_make_section (dynobj, name);
1151                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
1152                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1153                       if ((sec->flags & SEC_ALLOC) != 0)
1154                         flags |= SEC_ALLOC | SEC_LOAD;
1155                       if (sreloc == NULL
1156                           || ! bfd_set_section_flags (dynobj, sreloc, flags)
1157                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1158                         return false;
1159                     }
1160                   elf_section_data (sec)->sreloc = sreloc;
1161                 }
1162
1163               /* If this is a global symbol, we count the number of
1164                  relocations we need for this symbol.  */
1165               if (h != NULL)
1166                 {
1167                   head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1168                 }
1169               else
1170                 {
1171                   /* Track dynamic relocs needed for local syms too.
1172                      We really need local syms available to do this
1173                      easily.  Oh well.  */
1174
1175                   asection *s;
1176                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1177                                                  sec, r_symndx);
1178                   if (s == NULL)
1179                     return false;
1180
1181                   head = ((struct elf_i386_dyn_relocs **)
1182                           &elf_section_data (s)->local_dynrel);
1183                 }
1184
1185               p = *head;
1186               if (p == NULL || p->sec != sec)
1187                 {
1188                   bfd_size_type amt = sizeof *p;
1189                   p = ((struct elf_i386_dyn_relocs *)
1190                        bfd_alloc (htab->elf.dynobj, amt));
1191                   if (p == NULL)
1192                     return false;
1193                   p->next = *head;
1194                   *head = p;
1195                   p->sec = sec;
1196                   p->count = 0;
1197                   p->pc_count = 0;
1198                 }
1199
1200               p->count += 1;
1201               if (r_type == R_386_PC32)
1202                 p->pc_count += 1;
1203             }
1204           break;
1205
1206           /* This relocation describes the C++ object vtable hierarchy.
1207              Reconstruct it for later use during GC.  */
1208         case R_386_GNU_VTINHERIT:
1209           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1210             return false;
1211           break;
1212
1213           /* This relocation describes which C++ vtable entries are actually
1214              used.  Record for later use during GC.  */
1215         case R_386_GNU_VTENTRY:
1216           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1217             return false;
1218           break;
1219
1220         default:
1221           break;
1222         }
1223     }
1224
1225   return true;
1226 }
1227
1228 /* Return the section that should be marked against GC for a given
1229    relocation.  */
1230
1231 static asection *
1232 elf_i386_gc_mark_hook (sec, info, rel, h, sym)
1233      asection *sec;
1234      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1235      Elf_Internal_Rela *rel;
1236      struct elf_link_hash_entry *h;
1237      Elf_Internal_Sym *sym;
1238 {
1239   if (h != NULL)
1240     {
1241       switch (ELF32_R_TYPE (rel->r_info))
1242         {
1243         case R_386_GNU_VTINHERIT:
1244         case R_386_GNU_VTENTRY:
1245           break;
1246
1247         default:
1248           switch (h->root.type)
1249             {
1250             case bfd_link_hash_defined:
1251             case bfd_link_hash_defweak:
1252               return h->root.u.def.section;
1253
1254             case bfd_link_hash_common:
1255               return h->root.u.c.p->section;
1256
1257             default:
1258               break;
1259             }
1260         }
1261     }
1262   else
1263     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1264
1265   return NULL;
1266 }
1267
1268 /* Update the got entry reference counts for the section being removed.  */
1269
1270 static boolean
1271 elf_i386_gc_sweep_hook (abfd, info, sec, relocs)
1272      bfd *abfd;
1273      struct bfd_link_info *info;
1274      asection *sec;
1275      const Elf_Internal_Rela *relocs;
1276 {
1277   Elf_Internal_Shdr *symtab_hdr;
1278   struct elf_link_hash_entry **sym_hashes;
1279   bfd_signed_vma *local_got_refcounts;
1280   const Elf_Internal_Rela *rel, *relend;
1281   unsigned long r_symndx;
1282   int r_type;
1283   struct elf_link_hash_entry *h;
1284
1285   elf_section_data (sec)->local_dynrel = NULL;
1286
1287   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1288   sym_hashes = elf_sym_hashes (abfd);
1289   local_got_refcounts = elf_local_got_refcounts (abfd);
1290
1291   relend = relocs + sec->reloc_count;
1292   for (rel = relocs; rel < relend; rel++)
1293     switch ((r_type = elf_i386_tls_transition (info,
1294                                                ELF32_R_TYPE (rel->r_info),
1295                                                ELF32_R_SYM (rel->r_info)
1296                                                >= symtab_hdr->sh_info)))
1297       {
1298       case R_386_TLS_LDM:
1299         if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1300           elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1301         break;
1302
1303       case R_386_TLS_GD:
1304       case R_386_TLS_IE_32:
1305       case R_386_TLS_IE:
1306       case R_386_TLS_GOTIE:
1307       case R_386_GOT32:
1308         r_symndx = ELF32_R_SYM (rel->r_info);
1309         if (r_symndx >= symtab_hdr->sh_info)
1310           {
1311             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1312             if (h->got.refcount > 0)
1313               h->got.refcount -= 1;
1314           }
1315         else if (local_got_refcounts != NULL)
1316           {
1317             if (local_got_refcounts[r_symndx] > 0)
1318               local_got_refcounts[r_symndx] -= 1;
1319           }
1320         if (r_type != R_386_TLS_IE)
1321           break;
1322         /* Fall through */
1323
1324       case R_386_TLS_LE_32:
1325       case R_386_TLS_LE:
1326         if (!info->shared)
1327           break;
1328         /* Fall through */
1329
1330       case R_386_32:
1331       case R_386_PC32:
1332         r_symndx = ELF32_R_SYM (rel->r_info);
1333         if (r_symndx >= symtab_hdr->sh_info)
1334           {
1335             struct elf_i386_link_hash_entry *eh;
1336             struct elf_i386_dyn_relocs **pp;
1337             struct elf_i386_dyn_relocs *p;
1338
1339             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1340
1341             if (!info->shared && h->plt.refcount > 0)
1342               h->plt.refcount -= 1;
1343
1344             eh = (struct elf_i386_link_hash_entry *) h;
1345
1346             for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1347               if (p->sec == sec)
1348                 {
1349                   if (ELF32_R_TYPE (rel->r_info) == R_386_PC32)
1350                     p->pc_count -= 1;
1351                   p->count -= 1;
1352                   if (p->count == 0)
1353                     *pp = p->next;
1354                   break;
1355                 }
1356           }
1357         break;
1358
1359       case R_386_PLT32:
1360         r_symndx = ELF32_R_SYM (rel->r_info);
1361         if (r_symndx >= symtab_hdr->sh_info)
1362           {
1363             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1364             if (h->plt.refcount > 0)
1365               h->plt.refcount -= 1;
1366           }
1367         break;
1368
1369       default:
1370         break;
1371       }
1372
1373   return true;
1374 }
1375
1376 /* Adjust a symbol defined by a dynamic object and referenced by a
1377    regular object.  The current definition is in some section of the
1378    dynamic object, but we're not including those sections.  We have to
1379    change the definition to something the rest of the link can
1380    understand.  */
1381
1382 static boolean
1383 elf_i386_adjust_dynamic_symbol (info, h)
1384      struct bfd_link_info *info;
1385      struct elf_link_hash_entry *h;
1386 {
1387   struct elf_i386_link_hash_table *htab;
1388   struct elf_i386_link_hash_entry * eh;
1389   struct elf_i386_dyn_relocs *p;
1390   asection *s;
1391   unsigned int power_of_two;
1392
1393   /* If this is a function, put it in the procedure linkage table.  We
1394      will fill in the contents of the procedure linkage table later,
1395      when we know the address of the .got section.  */
1396   if (h->type == STT_FUNC
1397       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1398     {
1399       if (h->plt.refcount <= 0
1400           || (! info->shared
1401               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1402               && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1403               && h->root.type != bfd_link_hash_undefweak
1404               && h->root.type != bfd_link_hash_undefined))
1405         {
1406           /* This case can occur if we saw a PLT32 reloc in an input
1407              file, but the symbol was never referred to by a dynamic
1408              object, or if all references were garbage collected.  In
1409              such a case, we don't actually need to build a procedure
1410              linkage table, and we can just do a PC32 reloc instead.  */
1411           h->plt.offset = (bfd_vma) -1;
1412           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1413         }
1414
1415       return true;
1416     }
1417   else
1418     /* It's possible that we incorrectly decided a .plt reloc was
1419        needed for an R_386_PC32 reloc to a non-function sym in
1420        check_relocs.  We can't decide accurately between function and
1421        non-function syms in check-relocs;  Objects loaded later in
1422        the link may change h->type.  So fix it now.  */
1423     h->plt.offset = (bfd_vma) -1;
1424
1425   /* If this is a weak symbol, and there is a real definition, the
1426      processor independent code will have arranged for us to see the
1427      real definition first, and we can just use the same value.  */
1428   if (h->weakdef != NULL)
1429     {
1430       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1431                   || h->weakdef->root.type == bfd_link_hash_defweak);
1432       h->root.u.def.section = h->weakdef->root.u.def.section;
1433       h->root.u.def.value = h->weakdef->root.u.def.value;
1434       return true;
1435     }
1436
1437   /* This is a reference to a symbol defined by a dynamic object which
1438      is not a function.  */
1439
1440   /* If we are creating a shared library, we must presume that the
1441      only references to the symbol are via the global offset table.
1442      For such cases we need not do anything here; the relocations will
1443      be handled correctly by relocate_section.  */
1444   if (info->shared)
1445     return true;
1446
1447   /* If there are no references to this symbol that do not use the
1448      GOT, we don't need to generate a copy reloc.  */
1449   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1450     return true;
1451
1452   /* If -z nocopyreloc was given, we won't generate them either.  */
1453   if (info->nocopyreloc)
1454     {
1455       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1456       return true;
1457     }
1458
1459   eh = (struct elf_i386_link_hash_entry *) h;
1460   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1461     {
1462       s = p->sec->output_section;
1463       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1464         break;
1465     }
1466
1467   /* If we didn't find any dynamic relocs in read-only sections, then
1468      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1469   if (p == NULL)
1470     {
1471       h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1472       return true;
1473     }
1474
1475   /* We must allocate the symbol in our .dynbss section, which will
1476      become part of the .bss section of the executable.  There will be
1477      an entry for this symbol in the .dynsym section.  The dynamic
1478      object will contain position independent code, so all references
1479      from the dynamic object to this symbol will go through the global
1480      offset table.  The dynamic linker will use the .dynsym entry to
1481      determine the address it must put in the global offset table, so
1482      both the dynamic object and the regular object will refer to the
1483      same memory location for the variable.  */
1484
1485   htab = elf_i386_hash_table (info);
1486
1487   /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1488      copy the initial value out of the dynamic object and into the
1489      runtime process image.  */
1490   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1491     {
1492       htab->srelbss->_raw_size += sizeof (Elf32_External_Rel);
1493       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1494     }
1495
1496   /* We need to figure out the alignment required for this symbol.  I
1497      have no idea how ELF linkers handle this.  */
1498   power_of_two = bfd_log2 (h->size);
1499   if (power_of_two > 3)
1500     power_of_two = 3;
1501
1502   /* Apply the required alignment.  */
1503   s = htab->sdynbss;
1504   s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
1505   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1506     {
1507       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1508         return false;
1509     }
1510
1511   /* Define the symbol as being at this point in the section.  */
1512   h->root.u.def.section = s;
1513   h->root.u.def.value = s->_raw_size;
1514
1515   /* Increment the section size to make room for the symbol.  */
1516   s->_raw_size += h->size;
1517
1518   return true;
1519 }
1520
1521 /* This is the condition under which elf_i386_finish_dynamic_symbol
1522    will be called from elflink.h.  If elflink.h doesn't call our
1523    finish_dynamic_symbol routine, we'll need to do something about
1524    initializing any .plt and .got entries in elf_i386_relocate_section.  */
1525 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1526   ((DYN)                                                                \
1527    && ((INFO)->shared                                                   \
1528        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)      \
1529    && ((H)->dynindx != -1                                               \
1530        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1531
1532 /* Allocate space in .plt, .got and associated reloc sections for
1533    dynamic relocs.  */
1534
1535 static boolean
1536 allocate_dynrelocs (h, inf)
1537      struct elf_link_hash_entry *h;
1538      PTR inf;
1539 {
1540   struct bfd_link_info *info;
1541   struct elf_i386_link_hash_table *htab;
1542   struct elf_i386_link_hash_entry *eh;
1543   struct elf_i386_dyn_relocs *p;
1544
1545   if (h->root.type == bfd_link_hash_indirect)
1546     return true;
1547
1548   if (h->root.type == bfd_link_hash_warning)
1549     /* When warning symbols are created, they **replace** the "real"
1550        entry in the hash table, thus we never get to see the real
1551        symbol in a hash traversal.  So look at it now.  */
1552     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1553
1554   info = (struct bfd_link_info *) inf;
1555   htab = elf_i386_hash_table (info);
1556
1557   if (htab->elf.dynamic_sections_created
1558       && h->plt.refcount > 0)
1559     {
1560       /* Make sure this symbol is output as a dynamic symbol.
1561          Undefined weak syms won't yet be marked as dynamic.  */
1562       if (h->dynindx == -1
1563           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1564         {
1565           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1566             return false;
1567         }
1568
1569       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1570         {
1571           asection *s = htab->splt;
1572
1573           /* If this is the first .plt entry, make room for the special
1574              first entry.  */
1575           if (s->_raw_size == 0)
1576             s->_raw_size += PLT_ENTRY_SIZE;
1577
1578           h->plt.offset = s->_raw_size;
1579
1580           /* If this symbol is not defined in a regular file, and we are
1581              not generating a shared library, then set the symbol to this
1582              location in the .plt.  This is required to make function
1583              pointers compare as equal between the normal executable and
1584              the shared library.  */
1585           if (! info->shared
1586               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1587             {
1588               h->root.u.def.section = s;
1589               h->root.u.def.value = h->plt.offset;
1590             }
1591
1592           /* Make room for this entry.  */
1593           s->_raw_size += PLT_ENTRY_SIZE;
1594
1595           /* We also need to make an entry in the .got.plt section, which
1596              will be placed in the .got section by the linker script.  */
1597           htab->sgotplt->_raw_size += 4;
1598
1599           /* We also need to make an entry in the .rel.plt section.  */
1600           htab->srelplt->_raw_size += sizeof (Elf32_External_Rel);
1601         }
1602       else
1603         {
1604           h->plt.offset = (bfd_vma) -1;
1605           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1606         }
1607     }
1608   else
1609     {
1610       h->plt.offset = (bfd_vma) -1;
1611       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1612     }
1613
1614   /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
1615      make it a R_386_TLS_LE_32 requiring no TLS entry.  */
1616   if (h->got.refcount > 0
1617       && !info->shared
1618       && h->dynindx == -1
1619       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
1620     h->got.offset = (bfd_vma) -1;
1621   else if (h->got.refcount > 0)
1622     {
1623       asection *s;
1624       boolean dyn;
1625       int tls_type = elf_i386_hash_entry(h)->tls_type;
1626
1627       /* Make sure this symbol is output as a dynamic symbol.
1628          Undefined weak syms won't yet be marked as dynamic.  */
1629       if (h->dynindx == -1
1630           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1631         {
1632           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1633             return false;
1634         }
1635
1636       s = htab->sgot;
1637       h->got.offset = s->_raw_size;
1638       s->_raw_size += 4;
1639       /* R_386_TLS_GD needs 2 consecutive GOT slots.  */
1640       if (tls_type == GOT_TLS_GD || tls_type == GOT_TLS_IE_BOTH)
1641         s->_raw_size += 4;
1642       dyn = htab->elf.dynamic_sections_created;
1643       /* R_386_TLS_IE_32 needs one dynamic relocation,
1644          R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1645          (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1646          need two), R_386_TLS_GD needs one if local symbol and two if
1647          global.  */
1648       if (tls_type == GOT_TLS_IE_BOTH)
1649         htab->srelgot->_raw_size += 2 * sizeof (Elf32_External_Rel);
1650       else if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1651                || (tls_type & GOT_TLS_IE))
1652         htab->srelgot->_raw_size += sizeof (Elf32_External_Rel);
1653       else if (tls_type == GOT_TLS_GD)
1654         htab->srelgot->_raw_size += 2 * sizeof (Elf32_External_Rel);
1655       else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
1656         htab->srelgot->_raw_size += sizeof (Elf32_External_Rel);
1657     }
1658   else
1659     h->got.offset = (bfd_vma) -1;
1660
1661   eh = (struct elf_i386_link_hash_entry *) h;
1662   if (eh->dyn_relocs == NULL)
1663     return true;
1664
1665   /* In the shared -Bsymbolic case, discard space allocated for
1666      dynamic pc-relative relocs against symbols which turn out to be
1667      defined in regular objects.  For the normal shared case, discard
1668      space for pc-relative relocs that have become local due to symbol
1669      visibility changes.  */
1670
1671   if (info->shared)
1672     {
1673       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1674           && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1675               || info->symbolic))
1676         {
1677           struct elf_i386_dyn_relocs **pp;
1678
1679           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1680             {
1681               p->count -= p->pc_count;
1682               p->pc_count = 0;
1683               if (p->count == 0)
1684                 *pp = p->next;
1685               else
1686                 pp = &p->next;
1687             }
1688         }
1689     }
1690   else
1691     {
1692       /* For the non-shared case, discard space for relocs against
1693          symbols which turn out to need copy relocs or are not
1694          dynamic.  */
1695
1696       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1697           && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1698                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1699               || (htab->elf.dynamic_sections_created
1700                   && (h->root.type == bfd_link_hash_undefweak
1701                       || h->root.type == bfd_link_hash_undefined))))
1702         {
1703           /* Make sure this symbol is output as a dynamic symbol.
1704              Undefined weak syms won't yet be marked as dynamic.  */
1705           if (h->dynindx == -1
1706               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1707             {
1708               if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1709                 return false;
1710             }
1711
1712           /* If that succeeded, we know we'll be keeping all the
1713              relocs.  */
1714           if (h->dynindx != -1)
1715             goto keep;
1716         }
1717
1718       eh->dyn_relocs = NULL;
1719
1720     keep: ;
1721     }
1722
1723   /* Finally, allocate space.  */
1724   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1725     {
1726       asection *sreloc = elf_section_data (p->sec)->sreloc;
1727       sreloc->_raw_size += p->count * sizeof (Elf32_External_Rel);
1728     }
1729
1730   return true;
1731 }
1732
1733 /* Find any dynamic relocs that apply to read-only sections.  */
1734
1735 static boolean
1736 readonly_dynrelocs (h, inf)
1737      struct elf_link_hash_entry *h;
1738      PTR inf;
1739 {
1740   struct elf_i386_link_hash_entry *eh;
1741   struct elf_i386_dyn_relocs *p;
1742
1743   if (h->root.type == bfd_link_hash_warning)
1744     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1745
1746   eh = (struct elf_i386_link_hash_entry *) h;
1747   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1748     {
1749       asection *s = p->sec->output_section;
1750
1751       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1752         {
1753           struct bfd_link_info *info = (struct bfd_link_info *) inf;
1754
1755           info->flags |= DF_TEXTREL;
1756
1757           /* Not an error, just cut short the traversal.  */
1758           return false;
1759         }
1760     }
1761   return true;
1762 }
1763
1764 /* Set the sizes of the dynamic sections.  */
1765
1766 static boolean
1767 elf_i386_size_dynamic_sections (output_bfd, info)
1768      bfd *output_bfd ATTRIBUTE_UNUSED;
1769      struct bfd_link_info *info;
1770 {
1771   struct elf_i386_link_hash_table *htab;
1772   bfd *dynobj;
1773   asection *s;
1774   boolean relocs;
1775   bfd *ibfd;
1776
1777   htab = elf_i386_hash_table (info);
1778   dynobj = htab->elf.dynobj;
1779   if (dynobj == NULL)
1780     abort ();
1781
1782   if (htab->elf.dynamic_sections_created)
1783     {
1784       /* Set the contents of the .interp section to the interpreter.  */
1785       if (! info->shared)
1786         {
1787           s = bfd_get_section_by_name (dynobj, ".interp");
1788           if (s == NULL)
1789             abort ();
1790           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1791           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1792         }
1793     }
1794
1795   /* Set up .got offsets for local syms, and space for local dynamic
1796      relocs.  */
1797   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1798     {
1799       bfd_signed_vma *local_got;
1800       bfd_signed_vma *end_local_got;
1801       char *local_tls_type;
1802       bfd_size_type locsymcount;
1803       Elf_Internal_Shdr *symtab_hdr;
1804       asection *srel;
1805
1806       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1807         continue;
1808
1809       for (s = ibfd->sections; s != NULL; s = s->next)
1810         {
1811           struct elf_i386_dyn_relocs *p;
1812
1813           for (p = *((struct elf_i386_dyn_relocs **)
1814                      &elf_section_data (s)->local_dynrel);
1815                p != NULL;
1816                p = p->next)
1817             {
1818               if (!bfd_is_abs_section (p->sec)
1819                   && bfd_is_abs_section (p->sec->output_section))
1820                 {
1821                   /* Input section has been discarded, either because
1822                      it is a copy of a linkonce section or due to
1823                      linker script /DISCARD/, so we'll be discarding
1824                      the relocs too.  */
1825                 }
1826               else if (p->count != 0)
1827                 {
1828                   srel = elf_section_data (p->sec)->sreloc;
1829                   srel->_raw_size += p->count * sizeof (Elf32_External_Rel);
1830                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1831                     info->flags |= DF_TEXTREL;
1832                 }
1833             }
1834         }
1835
1836       local_got = elf_local_got_refcounts (ibfd);
1837       if (!local_got)
1838         continue;
1839
1840       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1841       locsymcount = symtab_hdr->sh_info;
1842       end_local_got = local_got + locsymcount;
1843       local_tls_type = elf_i386_local_got_tls_type (ibfd);
1844       s = htab->sgot;
1845       srel = htab->srelgot;
1846       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1847         {
1848           if (*local_got > 0)
1849             {
1850               *local_got = s->_raw_size;
1851               s->_raw_size += 4;
1852               if (*local_tls_type == GOT_TLS_GD
1853                   || *local_tls_type == GOT_TLS_IE_BOTH)
1854                 s->_raw_size += 4;
1855               if (info->shared
1856                   || *local_tls_type == GOT_TLS_GD
1857                   || (*local_tls_type & GOT_TLS_IE))
1858                 {
1859                   if (*local_tls_type == GOT_TLS_IE_BOTH)
1860                     srel->_raw_size += 2 * sizeof (Elf32_External_Rel);
1861                   else
1862                     srel->_raw_size += sizeof (Elf32_External_Rel);
1863                 }
1864             }
1865           else
1866             *local_got = (bfd_vma) -1;
1867         }
1868     }
1869
1870   if (htab->tls_ldm_got.refcount > 0)
1871     {
1872       /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
1873          relocs.  */
1874       htab->tls_ldm_got.offset = htab->sgot->_raw_size;
1875       htab->sgot->_raw_size += 8;
1876       htab->srelgot->_raw_size += sizeof (Elf32_External_Rel);
1877     }
1878   else
1879     htab->tls_ldm_got.offset = -1;
1880
1881   /* Allocate global sym .plt and .got entries, and space for global
1882      sym dynamic relocs.  */
1883   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1884
1885   /* We now have determined the sizes of the various dynamic sections.
1886      Allocate memory for them.  */
1887   relocs = false;
1888   for (s = dynobj->sections; s != NULL; s = s->next)
1889     {
1890       if ((s->flags & SEC_LINKER_CREATED) == 0)
1891         continue;
1892
1893       if (s == htab->splt
1894           || s == htab->sgot
1895           || s == htab->sgotplt)
1896         {
1897           /* Strip this section if we don't need it; see the
1898              comment below.  */
1899         }
1900       else if (strncmp (bfd_get_section_name (dynobj, s), ".rel", 4) == 0)
1901         {
1902           if (s->_raw_size != 0 && s != htab->srelplt)
1903             relocs = true;
1904
1905           /* We use the reloc_count field as a counter if we need
1906              to copy relocs into the output file.  */
1907           s->reloc_count = 0;
1908         }
1909       else
1910         {
1911           /* It's not one of our sections, so don't allocate space.  */
1912           continue;
1913         }
1914
1915       if (s->_raw_size == 0)
1916         {
1917           /* If we don't need this section, strip it from the
1918              output file.  This is mostly to handle .rel.bss and
1919              .rel.plt.  We must create both sections in
1920              create_dynamic_sections, because they must be created
1921              before the linker maps input sections to output
1922              sections.  The linker does that before
1923              adjust_dynamic_symbol is called, and it is that
1924              function which decides whether anything needs to go
1925              into these sections.  */
1926
1927           _bfd_strip_section_from_output (info, s);
1928           continue;
1929         }
1930
1931       /* Allocate memory for the section contents.  We use bfd_zalloc
1932          here in case unused entries are not reclaimed before the
1933          section's contents are written out.  This should not happen,
1934          but this way if it does, we get a R_386_NONE reloc instead
1935          of garbage.  */
1936       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1937       if (s->contents == NULL)
1938         return false;
1939     }
1940
1941   if (htab->elf.dynamic_sections_created)
1942     {
1943       /* Add some entries to the .dynamic section.  We fill in the
1944          values later, in elf_i386_finish_dynamic_sections, but we
1945          must add the entries now so that we get the correct size for
1946          the .dynamic section.  The DT_DEBUG entry is filled in by the
1947          dynamic linker and used by the debugger.  */
1948 #define add_dynamic_entry(TAG, VAL) \
1949   bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1950
1951       if (! info->shared)
1952         {
1953           if (!add_dynamic_entry (DT_DEBUG, 0))
1954             return false;
1955         }
1956
1957       if (htab->splt->_raw_size != 0)
1958         {
1959           if (!add_dynamic_entry (DT_PLTGOT, 0)
1960               || !add_dynamic_entry (DT_PLTRELSZ, 0)
1961               || !add_dynamic_entry (DT_PLTREL, DT_REL)
1962               || !add_dynamic_entry (DT_JMPREL, 0))
1963             return false;
1964         }
1965
1966       if (relocs)
1967         {
1968           if (!add_dynamic_entry (DT_REL, 0)
1969               || !add_dynamic_entry (DT_RELSZ, 0)
1970               || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
1971             return false;
1972
1973           /* If any dynamic relocs apply to a read-only section,
1974              then we need a DT_TEXTREL entry.  */
1975           if ((info->flags & DF_TEXTREL) == 0)
1976             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1977                                     (PTR) info);
1978
1979           if ((info->flags & DF_TEXTREL) != 0)
1980             {
1981               if (!add_dynamic_entry (DT_TEXTREL, 0))
1982                 return false;
1983             }
1984         }
1985     }
1986 #undef add_dynamic_entry
1987
1988   return true;
1989 }
1990
1991 /* Set the correct type for an x86 ELF section.  We do this by the
1992    section name, which is a hack, but ought to work.  */
1993
1994 static boolean
1995 elf_i386_fake_sections (abfd, hdr, sec)
1996      bfd *abfd ATTRIBUTE_UNUSED;
1997      Elf32_Internal_Shdr *hdr;
1998      asection *sec;
1999 {
2000   register const char *name;
2001
2002   name = bfd_get_section_name (abfd, sec);
2003
2004   /* This is an ugly, but unfortunately necessary hack that is
2005      needed when producing EFI binaries on x86. It tells
2006      elf.c:elf_fake_sections() not to consider ".reloc" as a section
2007      containing ELF relocation info.  We need this hack in order to
2008      be able to generate ELF binaries that can be translated into
2009      EFI applications (which are essentially COFF objects).  Those
2010      files contain a COFF ".reloc" section inside an ELFNN object,
2011      which would normally cause BFD to segfault because it would
2012      attempt to interpret this section as containing relocation
2013      entries for section "oc".  With this hack enabled, ".reloc"
2014      will be treated as a normal data section, which will avoid the
2015      segfault.  However, you won't be able to create an ELFNN binary
2016      with a section named "oc" that needs relocations, but that's
2017      the kind of ugly side-effects you get when detecting section
2018      types based on their names...  In practice, this limitation is
2019      unlikely to bite.  */
2020   if (strcmp (name, ".reloc") == 0)
2021     hdr->sh_type = SHT_PROGBITS;
2022
2023   return true;
2024 }
2025
2026 /* Return the base VMA address which should be subtracted from real addresses
2027    when resolving @dtpoff relocation.
2028    This is PT_TLS segment p_vaddr.  */
2029
2030 static bfd_vma
2031 dtpoff_base (info)
2032      struct bfd_link_info *info;
2033 {
2034   /* If tls_segment is NULL, we should have signalled an error already.  */
2035   if (elf_hash_table (info)->tls_segment == NULL)
2036     return 0;
2037   return elf_hash_table (info)->tls_segment->start;
2038 }
2039
2040 /* Return the relocation value for @tpoff relocation
2041    if STT_TLS virtual address is ADDRESS.  */
2042
2043 static bfd_vma
2044 tpoff (info, address)
2045      struct bfd_link_info *info;
2046      bfd_vma address;
2047 {
2048   struct elf_link_tls_segment *tls_segment
2049     = elf_hash_table (info)->tls_segment;
2050
2051   /* If tls_segment is NULL, we should have signalled an error already.  */
2052   if (tls_segment == NULL)
2053     return 0;
2054   return (align_power (tls_segment->size, tls_segment->align)
2055           + tls_segment->start - address);
2056 }
2057
2058 /* Relocate an i386 ELF section.  */
2059
2060 static boolean
2061 elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
2062                            contents, relocs, local_syms, local_sections)
2063      bfd *output_bfd;
2064      struct bfd_link_info *info;
2065      bfd *input_bfd;
2066      asection *input_section;
2067      bfd_byte *contents;
2068      Elf_Internal_Rela *relocs;
2069      Elf_Internal_Sym *local_syms;
2070      asection **local_sections;
2071 {
2072   struct elf_i386_link_hash_table *htab;
2073   Elf_Internal_Shdr *symtab_hdr;
2074   struct elf_link_hash_entry **sym_hashes;
2075   bfd_vma *local_got_offsets;
2076   Elf_Internal_Rela *rel;
2077   Elf_Internal_Rela *relend;
2078
2079   htab = elf_i386_hash_table (info);
2080   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2081   sym_hashes = elf_sym_hashes (input_bfd);
2082   local_got_offsets = elf_local_got_offsets (input_bfd);
2083
2084   rel = relocs;
2085   relend = relocs + input_section->reloc_count;
2086   for (; rel < relend; rel++)
2087     {
2088       unsigned int r_type;
2089       reloc_howto_type *howto;
2090       unsigned long r_symndx;
2091       struct elf_link_hash_entry *h;
2092       Elf_Internal_Sym *sym;
2093       asection *sec;
2094       bfd_vma off;
2095       bfd_vma relocation;
2096       boolean unresolved_reloc;
2097       bfd_reloc_status_type r;
2098       unsigned int indx;
2099       int tls_type;
2100
2101       r_type = ELF32_R_TYPE (rel->r_info);
2102       if (r_type == (int) R_386_GNU_VTINHERIT
2103           || r_type == (int) R_386_GNU_VTENTRY)
2104         continue;
2105
2106       if ((indx = (unsigned) r_type) >= R_386_standard
2107           && ((indx = r_type - R_386_ext_offset) - R_386_standard
2108               >= R_386_ext - R_386_standard)
2109           && ((indx = r_type - R_386_tls_offset) - R_386_ext
2110               >= R_386_tls - R_386_ext))
2111         {
2112           bfd_set_error (bfd_error_bad_value);
2113           return false;
2114         }
2115       howto = elf_howto_table + indx;
2116
2117       r_symndx = ELF32_R_SYM (rel->r_info);
2118
2119       if (info->relocateable)
2120         {
2121           bfd_vma val;
2122           bfd_byte *where;
2123
2124           /* This is a relocatable link.  We don't have to change
2125              anything, unless the reloc is against a section symbol,
2126              in which case we have to adjust according to where the
2127              section symbol winds up in the output section.  */
2128           if (r_symndx >= symtab_hdr->sh_info)
2129             continue;
2130
2131           sym = local_syms + r_symndx;
2132           if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2133             continue;
2134
2135           sec = local_sections[r_symndx];
2136           val = sec->output_offset;
2137           if (val == 0)
2138             continue;
2139
2140           where = contents + rel->r_offset;
2141           switch (howto->size)
2142             {
2143               /* FIXME: overflow checks.  */
2144             case 0:
2145               val += bfd_get_8 (input_bfd, where);
2146               bfd_put_8 (input_bfd, val, where);
2147               break;
2148             case 1:
2149               val += bfd_get_16 (input_bfd, where);
2150               bfd_put_16 (input_bfd, val, where);
2151               break;
2152             case 2:
2153               val += bfd_get_32 (input_bfd, where);
2154               bfd_put_32 (input_bfd, val, where);
2155               break;
2156             default:
2157               abort ();
2158             }
2159           continue;
2160         }
2161
2162       /* This is a final link.  */
2163       h = NULL;
2164       sym = NULL;
2165       sec = NULL;
2166       unresolved_reloc = false;
2167       if (r_symndx < symtab_hdr->sh_info)
2168         {
2169           sym = local_syms + r_symndx;
2170           sec = local_sections[r_symndx];
2171           relocation = (sec->output_section->vma
2172                         + sec->output_offset
2173                         + sym->st_value);
2174           if ((sec->flags & SEC_MERGE)
2175               && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2176             {
2177               asection *msec;
2178               bfd_vma addend;
2179               bfd_byte *where = contents + rel->r_offset;
2180
2181               switch (howto->size)
2182                 {
2183                 case 0:
2184                   addend = bfd_get_8 (input_bfd, where);
2185                   if (howto->pc_relative)
2186                     {
2187                       addend = (addend ^ 0x80) - 0x80;
2188                       addend += 1;
2189                     }
2190                   break;
2191                 case 1:
2192                   addend = bfd_get_16 (input_bfd, where);
2193                   if (howto->pc_relative)
2194                     {
2195                       addend = (addend ^ 0x8000) - 0x8000;
2196                       addend += 2;
2197                     }
2198                   break;
2199                 case 2:
2200                   addend = bfd_get_32 (input_bfd, where);
2201                   if (howto->pc_relative)
2202                     {
2203                       addend = (addend ^ 0x80000000) - 0x80000000;
2204                       addend += 4;
2205                     }
2206                   break;
2207                 default:
2208                   abort ();
2209                 }
2210
2211               msec = sec;
2212               addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend);
2213               addend -= relocation;
2214               addend += msec->output_section->vma + msec->output_offset;
2215
2216               switch (howto->size)
2217                 {
2218                 case 0:
2219                   /* FIXME: overflow checks.  */
2220                   if (howto->pc_relative)
2221                     addend -= 1;
2222                   bfd_put_8 (input_bfd, addend, where);
2223                   break;
2224                 case 1:
2225                   if (howto->pc_relative)
2226                     addend -= 2;
2227                   bfd_put_16 (input_bfd, addend, where);
2228                   break;
2229                 case 2:
2230                   if (howto->pc_relative)
2231                     addend -= 4;
2232                   bfd_put_32 (input_bfd, addend, where);
2233                   break;
2234                 }
2235             }
2236         }
2237       else
2238         {
2239           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2240           while (h->root.type == bfd_link_hash_indirect
2241                  || h->root.type == bfd_link_hash_warning)
2242             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2243
2244           relocation = 0;
2245           if (h->root.type == bfd_link_hash_defined
2246               || h->root.type == bfd_link_hash_defweak)
2247             {
2248               sec = h->root.u.def.section;
2249               if (sec->output_section == NULL)
2250                 /* Set a flag that will be cleared later if we find a
2251                    relocation value for this symbol.  output_section
2252                    is typically NULL for symbols satisfied by a shared
2253                    library.  */
2254                 unresolved_reloc = true;
2255               else
2256                 relocation = (h->root.u.def.value
2257                               + sec->output_section->vma
2258                               + sec->output_offset);
2259             }
2260           else if (h->root.type == bfd_link_hash_undefweak)
2261             ;
2262           else if (info->shared
2263                    && (!info->symbolic || info->allow_shlib_undefined)
2264                    && !info->no_undefined
2265                    && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2266             ;
2267           else
2268             {
2269               if (! ((*info->callbacks->undefined_symbol)
2270                      (info, h->root.root.string, input_bfd,
2271                       input_section, rel->r_offset,
2272                       (!info->shared || info->no_undefined
2273                        || ELF_ST_VISIBILITY (h->other)))))
2274                 return false;
2275             }
2276         }
2277
2278       switch (r_type)
2279         {
2280         case R_386_GOT32:
2281           /* Relocation is to the entry for this symbol in the global
2282              offset table.  */
2283           if (htab->sgot == NULL)
2284             abort ();
2285
2286           if (h != NULL)
2287             {
2288               boolean dyn;
2289
2290               off = h->got.offset;
2291               dyn = htab->elf.dynamic_sections_created;
2292               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
2293                   || (info->shared
2294                       && (info->symbolic
2295                           || h->dynindx == -1
2296                           || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2297                       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2298                 {
2299                   /* This is actually a static link, or it is a
2300                      -Bsymbolic link and the symbol is defined
2301                      locally, or the symbol was forced to be local
2302                      because of a version file.  We must initialize
2303                      this entry in the global offset table.  Since the
2304                      offset must always be a multiple of 4, we use the
2305                      least significant bit to record whether we have
2306                      initialized it already.
2307
2308                      When doing a dynamic link, we create a .rel.got
2309                      relocation entry to initialize the value.  This
2310                      is done in the finish_dynamic_symbol routine.  */
2311                   if ((off & 1) != 0)
2312                     off &= ~1;
2313                   else
2314                     {
2315                       bfd_put_32 (output_bfd, relocation,
2316                                   htab->sgot->contents + off);
2317                       h->got.offset |= 1;
2318                     }
2319                 }
2320               else
2321                 unresolved_reloc = false;
2322             }
2323           else
2324             {
2325               if (local_got_offsets == NULL)
2326                 abort ();
2327
2328               off = local_got_offsets[r_symndx];
2329
2330               /* The offset must always be a multiple of 4.  We use
2331                  the least significant bit to record whether we have
2332                  already generated the necessary reloc.  */
2333               if ((off & 1) != 0)
2334                 off &= ~1;
2335               else
2336                 {
2337                   bfd_put_32 (output_bfd, relocation,
2338                               htab->sgot->contents + off);
2339
2340                   if (info->shared)
2341                     {
2342                       asection *srelgot;
2343                       Elf_Internal_Rel outrel;
2344                       Elf32_External_Rel *loc;
2345
2346                       srelgot = htab->srelgot;
2347                       if (srelgot == NULL)
2348                         abort ();
2349
2350                       outrel.r_offset = (htab->sgot->output_section->vma
2351                                          + htab->sgot->output_offset
2352                                          + off);
2353                       outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2354                       loc = (Elf32_External_Rel *) srelgot->contents;
2355                       loc += srelgot->reloc_count++;
2356                       bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2357                     }
2358
2359                   local_got_offsets[r_symndx] |= 1;
2360                 }
2361             }
2362
2363           if (off >= (bfd_vma) -2)
2364             abort ();
2365
2366           relocation = htab->sgot->output_offset + off;
2367           break;
2368
2369         case R_386_GOTOFF:
2370           /* Relocation is relative to the start of the global offset
2371              table.  */
2372
2373           /* Note that sgot->output_offset is not involved in this
2374              calculation.  We always want the start of .got.  If we
2375              defined _GLOBAL_OFFSET_TABLE in a different way, as is
2376              permitted by the ABI, we might have to change this
2377              calculation.  */
2378           relocation -= htab->sgot->output_section->vma;
2379           break;
2380
2381         case R_386_GOTPC:
2382           /* Use global offset table as symbol value.  */
2383           relocation = htab->sgot->output_section->vma;
2384           unresolved_reloc = false;
2385           break;
2386
2387         case R_386_PLT32:
2388           /* Relocation is to the entry for this symbol in the
2389              procedure linkage table.  */
2390
2391           /* Resolve a PLT32 reloc against a local symbol directly,
2392              without using the procedure linkage table.  */
2393           if (h == NULL)
2394             break;
2395
2396           if (h->plt.offset == (bfd_vma) -1
2397               || htab->splt == NULL)
2398             {
2399               /* We didn't make a PLT entry for this symbol.  This
2400                  happens when statically linking PIC code, or when
2401                  using -Bsymbolic.  */
2402               break;
2403             }
2404
2405           relocation = (htab->splt->output_section->vma
2406                         + htab->splt->output_offset
2407                         + h->plt.offset);
2408           unresolved_reloc = false;
2409           break;
2410
2411         case R_386_32:
2412         case R_386_PC32:
2413           /* r_symndx will be zero only for relocs against symbols
2414              from removed linkonce sections, or sections discarded by
2415              a linker script.  */
2416           if (r_symndx == 0
2417               || (input_section->flags & SEC_ALLOC) == 0)
2418             break;
2419
2420           if ((info->shared
2421                && (r_type != R_386_PC32
2422                    || (h != NULL
2423                        && h->dynindx != -1
2424                        && (! info->symbolic
2425                            || (h->elf_link_hash_flags
2426                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2427               || (!info->shared
2428                   && h != NULL
2429                   && h->dynindx != -1
2430                   && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2431                   && (((h->elf_link_hash_flags
2432                         & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2433                        && (h->elf_link_hash_flags
2434                            & ELF_LINK_HASH_DEF_REGULAR) == 0)
2435                       || h->root.type == bfd_link_hash_undefweak
2436                       || h->root.type == bfd_link_hash_undefined)))
2437             {
2438               Elf_Internal_Rel outrel;
2439               boolean skip, relocate;
2440               asection *sreloc;
2441               Elf32_External_Rel *loc;
2442
2443               /* When generating a shared object, these relocations
2444                  are copied into the output file to be resolved at run
2445                  time.  */
2446
2447               skip = false;
2448               relocate = false;
2449
2450               outrel.r_offset =
2451                 _bfd_elf_section_offset (output_bfd, info, input_section,
2452                                          rel->r_offset);
2453               if (outrel.r_offset == (bfd_vma) -1)
2454                 skip = true;
2455               else if (outrel.r_offset == (bfd_vma) -2)
2456                 skip = true, relocate = true;
2457               outrel.r_offset += (input_section->output_section->vma
2458                                   + input_section->output_offset);
2459
2460               if (skip)
2461                 memset (&outrel, 0, sizeof outrel);
2462               else if (h != NULL
2463                        && h->dynindx != -1
2464                        && (r_type == R_386_PC32
2465                            || !info->shared
2466                            || !info->symbolic
2467                            || (h->elf_link_hash_flags
2468                                & ELF_LINK_HASH_DEF_REGULAR) == 0))
2469                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2470               else
2471                 {
2472                   /* This symbol is local, or marked to become local.  */
2473                   relocate = true;
2474                   outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2475                 }
2476
2477               sreloc = elf_section_data (input_section)->sreloc;
2478               if (sreloc == NULL)
2479                 abort ();
2480
2481               loc = (Elf32_External_Rel *) sreloc->contents;
2482               loc += sreloc->reloc_count++;
2483               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2484
2485               /* If this reloc is against an external symbol, we do
2486                  not want to fiddle with the addend.  Otherwise, we
2487                  need to include the symbol value so that it becomes
2488                  an addend for the dynamic reloc.  */
2489               if (! relocate)
2490                 continue;
2491             }
2492           break;
2493
2494         case R_386_TLS_IE:
2495           if (info->shared)
2496             {
2497               Elf_Internal_Rel outrel;
2498               asection *sreloc;
2499               Elf32_External_Rel *loc;
2500
2501               outrel.r_offset = rel->r_offset
2502                                 + input_section->output_section->vma
2503                                 + input_section->output_offset;
2504               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2505               sreloc = elf_section_data (input_section)->sreloc;
2506               if (sreloc == NULL)
2507                 abort ();
2508               loc = (Elf32_External_Rel *) sreloc->contents;
2509               loc += sreloc->reloc_count++;
2510               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2511             }
2512           /* Fall through */
2513
2514         case R_386_TLS_GD:
2515         case R_386_TLS_IE_32:
2516         case R_386_TLS_GOTIE:
2517           r_type = elf_i386_tls_transition (info, r_type, h == NULL);
2518           tls_type = GOT_UNKNOWN;
2519           if (h == NULL && local_got_offsets)
2520             tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2521           else if (h != NULL)
2522             {
2523               tls_type = elf_i386_hash_entry(h)->tls_type;
2524               if (!info->shared && h->dynindx == -1 && (tls_type & GOT_TLS_IE))
2525                 r_type = R_386_TLS_LE_32;
2526             }
2527           if (tls_type == GOT_TLS_IE)
2528             tls_type = GOT_TLS_IE_NEG;
2529           if (r_type == R_386_TLS_GD)
2530             {
2531               if (tls_type == GOT_TLS_IE_POS)
2532                 r_type = R_386_TLS_GOTIE;
2533               else if (tls_type & GOT_TLS_IE)
2534                 r_type = R_386_TLS_IE_32;
2535             }
2536
2537           if (r_type == R_386_TLS_LE_32)
2538             {
2539               BFD_ASSERT (! unresolved_reloc);
2540               if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
2541                 {
2542                   unsigned int val, type;
2543                   bfd_vma roff;
2544
2545                   /* GD->LE transition.  */
2546                   BFD_ASSERT (rel->r_offset >= 2);
2547                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2548                   BFD_ASSERT (type == 0x8d || type == 0x04);
2549                   BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size);
2550                   BFD_ASSERT (bfd_get_8 (input_bfd,
2551                                          contents + rel->r_offset + 4)
2552                               == 0xe8);
2553                   BFD_ASSERT (rel + 1 < relend);
2554                   BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2555                   roff = rel->r_offset + 5;
2556                   val = bfd_get_8 (input_bfd,
2557                                    contents + rel->r_offset - 1);
2558                   if (type == 0x04)
2559                     {
2560                       /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2561                          Change it into:
2562                          movl %gs:0, %eax; subl $foo@tpoff, %eax
2563                          (6 byte form of subl).  */
2564                       BFD_ASSERT (rel->r_offset >= 3);
2565                       BFD_ASSERT (bfd_get_8 (input_bfd,
2566                                              contents + rel->r_offset - 3)
2567                                   == 0x8d);
2568                       BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2569                       memcpy (contents + rel->r_offset - 3,
2570                               "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2571                     }
2572                   else
2573                     {
2574                       BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2575                       if (rel->r_offset + 10 <= input_section->_raw_size
2576                           && bfd_get_8 (input_bfd,
2577                                         contents + rel->r_offset + 9) == 0x90)
2578                         {
2579                           /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2580                              Change it into:
2581                              movl %gs:0, %eax; subl $foo@tpoff, %eax
2582                              (6 byte form of subl).  */
2583                           memcpy (contents + rel->r_offset - 2,
2584                                   "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2585                           roff = rel->r_offset + 6;
2586                         }
2587                       else
2588                         {
2589                           /* leal foo(%reg), %eax; call ___tls_get_addr
2590                              Change it into:
2591                              movl %gs:0, %eax; subl $foo@tpoff, %eax
2592                              (5 byte form of subl).  */
2593                           memcpy (contents + rel->r_offset - 2,
2594                                   "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2595                         }
2596                     }
2597                   bfd_put_32 (output_bfd, tpoff (info, relocation),
2598                               contents + roff);
2599                   /* Skip R_386_PLT32.  */
2600                   rel++;
2601                   continue;
2602                 }
2603               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
2604                 {
2605                   unsigned int val, type;
2606
2607                   /* IE->LE transition:
2608                      Originally it can be one of:
2609                      movl foo, %eax
2610                      movl foo, %reg
2611                      addl foo, %reg
2612                      We change it into:
2613                      movl $foo, %eax
2614                      movl $foo, %reg
2615                      addl $foo, %reg.  */
2616                   BFD_ASSERT (rel->r_offset >= 1);
2617                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2618                   BFD_ASSERT (rel->r_offset + 4 <= input_section->_raw_size);
2619                   if (val != 0xa1)
2620                     {
2621                       BFD_ASSERT (rel->r_offset >= 2);
2622                       type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2623                     }
2624                   if (val == 0xa1)
2625                     {
2626                       /* movl foo, %eax.  */
2627                       bfd_put_8 (output_bfd, 0xb8, contents + rel->r_offset - 2);
2628                     }
2629                   else if (type == 0x8b)
2630                     {
2631                       /* movl */
2632                       BFD_ASSERT ((val & 0xc7) == 0x05);
2633                       bfd_put_8 (output_bfd, 0xc7,
2634                                  contents + rel->r_offset - 2);
2635                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2636                                  contents + rel->r_offset - 1);
2637                     }
2638                   else if (type == 0x03)
2639                     {
2640                       /* addl */
2641                       BFD_ASSERT ((val & 0xc7) == 0x05);
2642                       bfd_put_8 (output_bfd, 0x81,
2643                                  contents + rel->r_offset - 2);
2644                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2645                                  contents + rel->r_offset - 1);
2646                     }
2647                   else
2648                     BFD_FAIL ();
2649                   bfd_put_32 (output_bfd, -tpoff (info, relocation),
2650                               contents + rel->r_offset);
2651                   continue;
2652                 }
2653               else
2654                 {
2655                   unsigned int val, type;
2656
2657                   /* {IE_32,GOTIE}->LE transition:
2658                      Originally it can be one of:
2659                      subl foo(%reg1), %reg2
2660                      movl foo(%reg1), %reg2
2661                      addl foo(%reg1), %reg2
2662                      We change it into:
2663                      subl $foo, %reg2
2664                      movl $foo, %reg2 (6 byte form)
2665                      addl $foo, %reg2.  */
2666                   BFD_ASSERT (rel->r_offset >= 2);
2667                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2668                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2669                   BFD_ASSERT (rel->r_offset + 4 <= input_section->_raw_size);
2670                   BFD_ASSERT ((val & 0xc0) == 0x80 && (val & 7) != 4);
2671                   if (type == 0x8b)
2672                     {
2673                       /* movl */
2674                       bfd_put_8 (output_bfd, 0xc7,
2675                                  contents + rel->r_offset - 2);
2676                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2677                                  contents + rel->r_offset - 1);
2678                     }
2679                   else if (type == 0x2b)
2680                     {
2681                       /* subl */
2682                       bfd_put_8 (output_bfd, 0x81,
2683                                  contents + rel->r_offset - 2);
2684                       bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
2685                                  contents + rel->r_offset - 1);
2686                     }
2687                   else if (type == 0x03)
2688                     {
2689                       /* addl */
2690                       bfd_put_8 (output_bfd, 0x81,
2691                                  contents + rel->r_offset - 2);
2692                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2693                                  contents + rel->r_offset - 1);
2694                     }
2695                   else
2696                     BFD_FAIL ();
2697                   if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
2698                     bfd_put_32 (output_bfd, -tpoff (info, relocation),
2699                                 contents + rel->r_offset);
2700                   else
2701                     bfd_put_32 (output_bfd, tpoff (info, relocation),
2702                                 contents + rel->r_offset);
2703                   continue;
2704                 }
2705             }
2706
2707           if (htab->sgot == NULL)
2708             abort ();
2709
2710           if (h != NULL)
2711             off = h->got.offset;
2712           else
2713             {
2714               if (local_got_offsets == NULL)
2715                 abort ();
2716
2717               off = local_got_offsets[r_symndx];
2718             }
2719
2720           if ((off & 1) != 0)
2721             off &= ~1;
2722           else
2723             {
2724               Elf_Internal_Rel outrel;
2725               Elf32_External_Rel *loc;
2726               int dr_type, indx;
2727
2728               if (htab->srelgot == NULL)
2729                 abort ();
2730
2731               outrel.r_offset = (htab->sgot->output_section->vma
2732                                  + htab->sgot->output_offset + off);
2733
2734               indx = h && h->dynindx != -1 ? h->dynindx : 0;
2735               if (r_type == R_386_TLS_GD)
2736                 dr_type = R_386_TLS_DTPMOD32;
2737               else if (tls_type == GOT_TLS_IE_POS)
2738                 dr_type = R_386_TLS_TPOFF;
2739               else
2740                 dr_type = R_386_TLS_TPOFF32;
2741               if (dr_type == R_386_TLS_TPOFF && indx == 0)
2742                 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
2743                             htab->sgot->contents + off);
2744               else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
2745                 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
2746                             htab->sgot->contents + off);
2747               else
2748                 bfd_put_32 (output_bfd, 0,
2749                             htab->sgot->contents + off);
2750               outrel.r_info = ELF32_R_INFO (indx, dr_type);
2751               loc = (Elf32_External_Rel *) htab->srelgot->contents;
2752               loc += htab->srelgot->reloc_count++;
2753               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2754
2755               if (r_type == R_386_TLS_GD)
2756                 {
2757                   if (indx == 0)
2758                     {
2759                       BFD_ASSERT (! unresolved_reloc);
2760                       bfd_put_32 (output_bfd,
2761                                   relocation - dtpoff_base (info),
2762                                   htab->sgot->contents + off + 4);
2763                     }
2764                   else
2765                     {
2766                       bfd_put_32 (output_bfd, 0,
2767                                   htab->sgot->contents + off + 4);
2768                       outrel.r_info = ELF32_R_INFO (indx,
2769                                                     R_386_TLS_DTPOFF32);
2770                       outrel.r_offset += 4;
2771                       htab->srelgot->reloc_count++;
2772                       loc++;
2773                       bfd_elf32_swap_reloc_out (output_bfd, &outrel,
2774                                                 loc);
2775                     }
2776                 }
2777               else if (tls_type == GOT_TLS_IE_BOTH)
2778                 {
2779                   bfd_put_32 (output_bfd,
2780                               indx == 0 ? relocation - dtpoff_base (info) : 0,
2781                               htab->sgot->contents + off + 4);
2782                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
2783                   outrel.r_offset += 4;
2784                   htab->srelgot->reloc_count++;
2785                   loc++;
2786                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2787                 }
2788
2789               if (h != NULL)
2790                 h->got.offset |= 1;
2791               else
2792                 local_got_offsets[r_symndx] |= 1;
2793             }
2794
2795           if (off >= (bfd_vma) -2)
2796             abort ();
2797           if (r_type == ELF32_R_TYPE (rel->r_info))
2798             {
2799               relocation = htab->sgot->output_offset + off;
2800               if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
2801                   && tls_type == GOT_TLS_IE_BOTH)
2802                 relocation += 4;
2803               if (r_type == R_386_TLS_IE)
2804                 relocation += htab->sgot->output_section->vma;
2805               unresolved_reloc = false;
2806             }
2807           else
2808             {
2809               unsigned int val, type;
2810               bfd_vma roff;
2811
2812               /* GD->IE transition.  */
2813               BFD_ASSERT (rel->r_offset >= 2);
2814               type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2815               BFD_ASSERT (type == 0x8d || type == 0x04);
2816               BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size);
2817               BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2818                           == 0xe8);
2819               BFD_ASSERT (rel + 1 < relend);
2820               BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2821               roff = rel->r_offset - 3;
2822               val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2823               if (type == 0x04)
2824                 {
2825                   /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2826                      Change it into:
2827                      movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
2828                   BFD_ASSERT (rel->r_offset >= 3);
2829                   BFD_ASSERT (bfd_get_8 (input_bfd,
2830                                          contents + rel->r_offset - 3)
2831                               == 0x8d);
2832                   BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2833                   val >>= 3;
2834                 }
2835               else
2836                 {
2837                   /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2838                      Change it into:
2839                      movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
2840                   BFD_ASSERT (rel->r_offset + 10 <= input_section->_raw_size);
2841                   BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2842                   BFD_ASSERT (bfd_get_8 (input_bfd,
2843                                          contents + rel->r_offset + 9)
2844                               == 0x90);
2845                   roff = rel->r_offset - 2;
2846                 }
2847               memcpy (contents + roff,
2848                       "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
2849               contents[roff + 7] = 0x80 | (val & 7);
2850               /* If foo is used only with foo@gotntpoff(%reg) and
2851                  foo@indntpoff, but not with foo@gottpoff(%reg), change
2852                  subl $foo@gottpoff(%reg), %eax
2853                  into:
2854                  addl $foo@gotntpoff(%reg), %eax.  */
2855               if (r_type == R_386_TLS_GOTIE)
2856                 {
2857                   contents[roff + 6] = 0x03;
2858                   if (tls_type == GOT_TLS_IE_BOTH)
2859                     off += 4;
2860                 }
2861               bfd_put_32 (output_bfd, htab->sgot->output_offset + off,
2862                           contents + roff + 8);
2863               /* Skip R_386_PLT32.  */
2864               rel++;
2865               continue;
2866             }
2867           break;
2868
2869         case R_386_TLS_LDM:
2870           if (! info->shared)
2871             {
2872               unsigned int val;
2873
2874               /* LD->LE transition:
2875                  Ensure it is:
2876                  leal foo(%reg), %eax; call ___tls_get_addr.
2877                  We change it into:
2878                  movl %gs:0, %eax; nop; leal 0(%esi,1), %esi.  */
2879               BFD_ASSERT (rel->r_offset >= 2);
2880               BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2881                           == 0x8d);
2882               val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2883               BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2884               BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size);
2885               BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2886                           == 0xe8);
2887               BFD_ASSERT (rel + 1 < relend);
2888               BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2889               memcpy (contents + rel->r_offset - 2,
2890                       "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
2891               /* Skip R_386_PLT32.  */
2892               rel++;
2893               continue;
2894             }
2895
2896           if (htab->sgot == NULL)
2897             abort ();
2898
2899           off = htab->tls_ldm_got.offset;
2900           if (off & 1)
2901             off &= ~1;
2902           else
2903             {
2904               Elf_Internal_Rel outrel;
2905               Elf32_External_Rel *loc;
2906
2907               if (htab->srelgot == NULL)
2908                 abort ();
2909
2910               outrel.r_offset = (htab->sgot->output_section->vma
2911                                  + htab->sgot->output_offset + off);
2912
2913               bfd_put_32 (output_bfd, 0,
2914                           htab->sgot->contents + off);
2915               bfd_put_32 (output_bfd, 0,
2916                           htab->sgot->contents + off + 4);
2917               outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
2918               loc = (Elf32_External_Rel *) htab->srelgot->contents;
2919               loc += htab->srelgot->reloc_count++;
2920               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2921               htab->tls_ldm_got.offset |= 1;
2922             }
2923           relocation = htab->sgot->output_offset + off;
2924           unresolved_reloc = false;
2925           break;
2926
2927         case R_386_TLS_LDO_32:
2928           if (info->shared)
2929             relocation -= dtpoff_base (info);
2930           else
2931             /* When converting LDO to LE, we must negate.  */
2932             relocation = -tpoff (info, relocation);
2933           break;
2934
2935         case R_386_TLS_LE_32:
2936         case R_386_TLS_LE:
2937           if (info->shared)
2938             {
2939               Elf_Internal_Rel outrel;
2940               asection *sreloc;
2941               Elf32_External_Rel *loc;
2942               int indx;
2943
2944               outrel.r_offset = rel->r_offset
2945                                 + input_section->output_section->vma
2946                                 + input_section->output_offset;
2947               if (h != NULL && h->dynindx != -1)
2948                 indx = h->dynindx;
2949               else
2950                 indx = 0;
2951               if (r_type == R_386_TLS_LE_32)
2952                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
2953               else
2954                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
2955               sreloc = elf_section_data (input_section)->sreloc;
2956               if (sreloc == NULL)
2957                 abort ();
2958               loc = (Elf32_External_Rel *) sreloc->contents;
2959               loc += sreloc->reloc_count++;
2960               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2961               if (indx)
2962                 continue;
2963               else if (r_type == R_386_TLS_LE_32)
2964                 relocation = dtpoff_base (info) - relocation;
2965               else
2966                 relocation -= dtpoff_base (info);
2967             }
2968           else if (r_type == R_386_TLS_LE_32)
2969             relocation = tpoff (info, relocation);
2970           else
2971             relocation = -tpoff (info, relocation);
2972           break;
2973
2974         default:
2975           break;
2976         }
2977
2978       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2979          because such sections are not SEC_ALLOC and thus ld.so will
2980          not process them.  */
2981       if (unresolved_reloc
2982           && !((input_section->flags & SEC_DEBUGGING) != 0
2983                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2984         {
2985           (*_bfd_error_handler)
2986             (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2987              bfd_archive_filename (input_bfd),
2988              bfd_get_section_name (input_bfd, input_section),
2989              (long) rel->r_offset,
2990              h->root.root.string);
2991           return false;
2992         }
2993
2994       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2995                                     contents, rel->r_offset,
2996                                     relocation, (bfd_vma) 0);
2997
2998       if (r != bfd_reloc_ok)
2999         {
3000           const char *name;
3001
3002           if (h != NULL)
3003             name = h->root.root.string;
3004           else
3005             {
3006               name = bfd_elf_string_from_elf_section (input_bfd,
3007                                                       symtab_hdr->sh_link,
3008                                                       sym->st_name);
3009               if (name == NULL)
3010                 return false;
3011               if (*name == '\0')
3012                 name = bfd_section_name (input_bfd, sec);
3013             }
3014
3015           if (r == bfd_reloc_overflow)
3016             {
3017               if (! ((*info->callbacks->reloc_overflow)
3018                      (info, name, howto->name, (bfd_vma) 0,
3019                       input_bfd, input_section, rel->r_offset)))
3020                 return false;
3021             }
3022           else
3023             {
3024               (*_bfd_error_handler)
3025                 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
3026                  bfd_archive_filename (input_bfd),
3027                  bfd_get_section_name (input_bfd, input_section),
3028                  (long) rel->r_offset, name, (int) r);
3029               return false;
3030             }
3031         }
3032     }
3033
3034   return true;
3035 }
3036
3037 /* Finish up dynamic symbol handling.  We set the contents of various
3038    dynamic sections here.  */
3039
3040 static boolean
3041 elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
3042      bfd *output_bfd;
3043      struct bfd_link_info *info;
3044      struct elf_link_hash_entry *h;
3045      Elf_Internal_Sym *sym;
3046 {
3047   struct elf_i386_link_hash_table *htab;
3048
3049   htab = elf_i386_hash_table (info);
3050
3051   if (h->plt.offset != (bfd_vma) -1)
3052     {
3053       bfd_vma plt_index;
3054       bfd_vma got_offset;
3055       Elf_Internal_Rel rel;
3056       Elf32_External_Rel *loc;
3057
3058       /* This symbol has an entry in the procedure linkage table.  Set
3059          it up.  */
3060
3061       if (h->dynindx == -1
3062           || htab->splt == NULL
3063           || htab->sgotplt == NULL
3064           || htab->srelplt == NULL)
3065         abort ();
3066
3067       /* Get the index in the procedure linkage table which
3068          corresponds to this symbol.  This is the index of this symbol
3069          in all the symbols for which we are making plt entries.  The
3070          first entry in the procedure linkage table is reserved.  */
3071       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3072
3073       /* Get the offset into the .got table of the entry that
3074          corresponds to this function.  Each .got entry is 4 bytes.
3075          The first three are reserved.  */
3076       got_offset = (plt_index + 3) * 4;
3077
3078       /* Fill in the entry in the procedure linkage table.  */
3079       if (! info->shared)
3080         {
3081           memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
3082                   PLT_ENTRY_SIZE);
3083           bfd_put_32 (output_bfd,
3084                       (htab->sgotplt->output_section->vma
3085                        + htab->sgotplt->output_offset
3086                        + got_offset),
3087                       htab->splt->contents + h->plt.offset + 2);
3088         }
3089       else
3090         {
3091           memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
3092                   PLT_ENTRY_SIZE);
3093           bfd_put_32 (output_bfd, got_offset,
3094                       htab->splt->contents + h->plt.offset + 2);
3095         }
3096
3097       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
3098                   htab->splt->contents + h->plt.offset + 7);
3099       bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3100                   htab->splt->contents + h->plt.offset + 12);
3101
3102       /* Fill in the entry in the global offset table.  */
3103       bfd_put_32 (output_bfd,
3104                   (htab->splt->output_section->vma
3105                    + htab->splt->output_offset
3106                    + h->plt.offset
3107                    + 6),
3108                   htab->sgotplt->contents + got_offset);
3109
3110       /* Fill in the entry in the .rel.plt section.  */
3111       rel.r_offset = (htab->sgotplt->output_section->vma
3112                       + htab->sgotplt->output_offset
3113                       + got_offset);
3114       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3115       loc = (Elf32_External_Rel *) htab->srelplt->contents + plt_index;
3116       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3117
3118       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3119         {
3120           /* Mark the symbol as undefined, rather than as defined in
3121              the .plt section.  Leave the value alone.  This is a clue
3122              for the dynamic linker, to make function pointer
3123              comparisons work between an application and shared
3124              library.  */
3125           sym->st_shndx = SHN_UNDEF;
3126         }
3127     }
3128
3129   if (h->got.offset != (bfd_vma) -1
3130       && elf_i386_hash_entry(h)->tls_type != GOT_TLS_GD
3131       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
3132     {
3133       Elf_Internal_Rel rel;
3134       Elf32_External_Rel *loc;
3135
3136       /* This symbol has an entry in the global offset table.  Set it
3137          up.  */
3138
3139       if (htab->sgot == NULL || htab->srelgot == NULL)
3140         abort ();
3141
3142       rel.r_offset = (htab->sgot->output_section->vma
3143                       + htab->sgot->output_offset
3144                       + (h->got.offset & ~(bfd_vma) 1));
3145
3146       /* If this is a static link, or it is a -Bsymbolic link and the
3147          symbol is defined locally or was forced to be local because
3148          of a version file, we just want to emit a RELATIVE reloc.
3149          The entry in the global offset table will already have been
3150          initialized in the relocate_section function.  */
3151       if (info->shared
3152           && (info->symbolic
3153               || h->dynindx == -1
3154               || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
3155           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
3156         {
3157           BFD_ASSERT((h->got.offset & 1) != 0);
3158           rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3159         }
3160       else
3161         {
3162           BFD_ASSERT((h->got.offset & 1) == 0);
3163           bfd_put_32 (output_bfd, (bfd_vma) 0,
3164                       htab->sgot->contents + h->got.offset);
3165           rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3166         }
3167
3168       loc = (Elf32_External_Rel *) htab->srelgot->contents;
3169       loc += htab->srelgot->reloc_count++;
3170       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3171     }
3172
3173   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3174     {
3175       Elf_Internal_Rel rel;
3176       Elf32_External_Rel *loc;
3177
3178       /* This symbol needs a copy reloc.  Set it up.  */
3179
3180       if (h->dynindx == -1
3181           || (h->root.type != bfd_link_hash_defined
3182               && h->root.type != bfd_link_hash_defweak)
3183           || htab->srelbss == NULL)
3184         abort ();
3185
3186       rel.r_offset = (h->root.u.def.value
3187                       + h->root.u.def.section->output_section->vma
3188                       + h->root.u.def.section->output_offset);
3189       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3190       loc = (Elf32_External_Rel *) htab->srelbss->contents;
3191       loc += htab->srelbss->reloc_count++;
3192       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3193     }
3194
3195   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
3196   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3197       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3198     sym->st_shndx = SHN_ABS;
3199
3200   return true;
3201 }
3202
3203 /* Used to decide how to sort relocs in an optimal manner for the
3204    dynamic linker, before writing them out.  */
3205
3206 static enum elf_reloc_type_class
3207 elf_i386_reloc_type_class (rela)
3208      const Elf_Internal_Rela *rela;
3209 {
3210   switch ((int) ELF32_R_TYPE (rela->r_info))
3211     {
3212     case R_386_RELATIVE:
3213       return reloc_class_relative;
3214     case R_386_JUMP_SLOT:
3215       return reloc_class_plt;
3216     case R_386_COPY:
3217       return reloc_class_copy;
3218     default:
3219       return reloc_class_normal;
3220     }
3221 }
3222
3223 /* Finish up the dynamic sections.  */
3224
3225 static boolean
3226 elf_i386_finish_dynamic_sections (output_bfd, info)
3227      bfd *output_bfd;
3228      struct bfd_link_info *info;
3229 {
3230   struct elf_i386_link_hash_table *htab;
3231   bfd *dynobj;
3232   asection *sdyn;
3233
3234   htab = elf_i386_hash_table (info);
3235   dynobj = htab->elf.dynobj;
3236   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3237
3238   if (htab->elf.dynamic_sections_created)
3239     {
3240       Elf32_External_Dyn *dyncon, *dynconend;
3241
3242       if (sdyn == NULL || htab->sgot == NULL)
3243         abort ();
3244
3245       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3246       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3247       for (; dyncon < dynconend; dyncon++)
3248         {
3249           Elf_Internal_Dyn dyn;
3250           asection *s;
3251
3252           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3253
3254           switch (dyn.d_tag)
3255             {
3256             default:
3257               continue;
3258
3259             case DT_PLTGOT:
3260               dyn.d_un.d_ptr = htab->sgot->output_section->vma;
3261               break;
3262
3263             case DT_JMPREL:
3264               dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3265               break;
3266
3267             case DT_PLTRELSZ:
3268               s = htab->srelplt->output_section;
3269               if (s->_cooked_size != 0)
3270                 dyn.d_un.d_val = s->_cooked_size;
3271               else
3272                 dyn.d_un.d_val = s->_raw_size;
3273               break;
3274
3275             case DT_RELSZ:
3276               /* My reading of the SVR4 ABI indicates that the
3277                  procedure linkage table relocs (DT_JMPREL) should be
3278                  included in the overall relocs (DT_REL).  This is
3279                  what Solaris does.  However, UnixWare can not handle
3280                  that case.  Therefore, we override the DT_RELSZ entry
3281                  here to make it not include the JMPREL relocs.  Since
3282                  the linker script arranges for .rel.plt to follow all
3283                  other relocation sections, we don't have to worry
3284                  about changing the DT_REL entry.  */
3285               if (htab->srelplt != NULL)
3286                 {
3287                   s = htab->srelplt->output_section;
3288                   if (s->_cooked_size != 0)
3289                     dyn.d_un.d_val -= s->_cooked_size;
3290                   else
3291                     dyn.d_un.d_val -= s->_raw_size;
3292                 }
3293               break;
3294             }
3295
3296           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3297         }
3298
3299       /* Fill in the first entry in the procedure linkage table.  */
3300       if (htab->splt && htab->splt->_raw_size > 0)
3301         {
3302           if (info->shared)
3303             memcpy (htab->splt->contents,
3304                     elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
3305           else
3306             {
3307               memcpy (htab->splt->contents,
3308                       elf_i386_plt0_entry, PLT_ENTRY_SIZE);
3309               bfd_put_32 (output_bfd,
3310                           (htab->sgotplt->output_section->vma
3311                            + htab->sgotplt->output_offset
3312                            + 4),
3313                           htab->splt->contents + 2);
3314               bfd_put_32 (output_bfd,
3315                           (htab->sgotplt->output_section->vma
3316                            + htab->sgotplt->output_offset
3317                            + 8),
3318                           htab->splt->contents + 8);
3319             }
3320
3321           /* UnixWare sets the entsize of .plt to 4, although that doesn't
3322              really seem like the right value.  */
3323           elf_section_data (htab->splt->output_section)
3324             ->this_hdr.sh_entsize = 4;
3325         }
3326     }
3327
3328   if (htab->sgotplt)
3329     {
3330       /* Fill in the first three entries in the global offset table.  */
3331       if (htab->sgotplt->_raw_size > 0)
3332         {
3333           bfd_put_32 (output_bfd,
3334                       (sdyn == NULL ? (bfd_vma) 0
3335                        : sdyn->output_section->vma + sdyn->output_offset),
3336                       htab->sgotplt->contents);
3337           bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 4);
3338           bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8);
3339         }
3340
3341       elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
3342     }
3343   return true;
3344 }
3345
3346 #ifndef ELF_ARCH
3347 #define TARGET_LITTLE_SYM               bfd_elf32_i386_vec
3348 #define TARGET_LITTLE_NAME              "elf32-i386"
3349 #define ELF_ARCH                        bfd_arch_i386
3350 #define ELF_MACHINE_CODE                EM_386
3351 #define ELF_MAXPAGESIZE                 0x1000
3352 #endif /* ELF_ARCH */
3353
3354 #define elf_backend_can_gc_sections     1
3355 #define elf_backend_can_refcount        1
3356 #define elf_backend_want_got_plt        1
3357 #define elf_backend_plt_readonly        1
3358 #define elf_backend_want_plt_sym        0
3359 #define elf_backend_got_header_size     12
3360 #define elf_backend_plt_header_size     PLT_ENTRY_SIZE
3361
3362 #define elf_info_to_howto                     elf_i386_info_to_howto
3363 #define elf_info_to_howto_rel                 elf_i386_info_to_howto_rel
3364
3365 #define bfd_elf32_mkobject                    elf_i386_mkobject
3366 #define elf_backend_object_p                  elf_i386_object_p
3367
3368 #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
3369 #define bfd_elf32_bfd_link_hash_table_create  elf_i386_link_hash_table_create
3370 #define bfd_elf32_bfd_reloc_type_lookup       elf_i386_reloc_type_lookup
3371
3372 #define elf_backend_adjust_dynamic_symbol     elf_i386_adjust_dynamic_symbol
3373 #define elf_backend_check_relocs              elf_i386_check_relocs
3374 #define elf_backend_copy_indirect_symbol      elf_i386_copy_indirect_symbol
3375 #define elf_backend_create_dynamic_sections   elf_i386_create_dynamic_sections
3376 #define elf_backend_fake_sections             elf_i386_fake_sections
3377 #define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
3378 #define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
3379 #define elf_backend_gc_mark_hook              elf_i386_gc_mark_hook
3380 #define elf_backend_gc_sweep_hook             elf_i386_gc_sweep_hook
3381 #define elf_backend_grok_prstatus             elf_i386_grok_prstatus
3382 #define elf_backend_grok_psinfo               elf_i386_grok_psinfo
3383 #define elf_backend_reloc_type_class          elf_i386_reloc_type_class
3384 #define elf_backend_relocate_section          elf_i386_relocate_section
3385 #define elf_backend_size_dynamic_sections     elf_i386_size_dynamic_sections
3386
3387 #ifndef ELF32_I386_C_INCLUDED
3388 #include "elf32-target.h"
3389 #endif