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