2009-06-18 H.J. Lu <hongjiu.lu@intel.com>
[platform/upstream/binutils.git] / bfd / elf32-i386.c
1 /* Intel 80386/80486-specific support for 32-bit ELF
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf-vxworks.h"
28 #include "bfd_stdint.h"
29 #include "objalloc.h"
30 #include "hashtab.h"
31
32 /* 386 uses REL relocations instead of RELA.  */
33 #define USE_REL 1
34
35 #include "elf/i386.h"
36
37 static reloc_howto_type elf_howto_table[]=
38 {
39   HOWTO(R_386_NONE, 0, 0, 0, FALSE, 0, complain_overflow_bitfield,
40         bfd_elf_generic_reloc, "R_386_NONE",
41         TRUE, 0x00000000, 0x00000000, FALSE),
42   HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
43         bfd_elf_generic_reloc, "R_386_32",
44         TRUE, 0xffffffff, 0xffffffff, FALSE),
45   HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
46         bfd_elf_generic_reloc, "R_386_PC32",
47         TRUE, 0xffffffff, 0xffffffff, TRUE),
48   HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
49         bfd_elf_generic_reloc, "R_386_GOT32",
50         TRUE, 0xffffffff, 0xffffffff, FALSE),
51   HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
52         bfd_elf_generic_reloc, "R_386_PLT32",
53         TRUE, 0xffffffff, 0xffffffff, TRUE),
54   HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
55         bfd_elf_generic_reloc, "R_386_COPY",
56         TRUE, 0xffffffff, 0xffffffff, FALSE),
57   HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
58         bfd_elf_generic_reloc, "R_386_GLOB_DAT",
59         TRUE, 0xffffffff, 0xffffffff, FALSE),
60   HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
61         bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
62         TRUE, 0xffffffff, 0xffffffff, FALSE),
63   HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
64         bfd_elf_generic_reloc, "R_386_RELATIVE",
65         TRUE, 0xffffffff, 0xffffffff, FALSE),
66   HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
67         bfd_elf_generic_reloc, "R_386_GOTOFF",
68         TRUE, 0xffffffff, 0xffffffff, FALSE),
69   HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
70         bfd_elf_generic_reloc, "R_386_GOTPC",
71         TRUE, 0xffffffff, 0xffffffff, TRUE),
72
73   /* We have a gap in the reloc numbers here.
74      R_386_standard counts the number up to this point, and
75      R_386_ext_offset is the value to subtract from a reloc type of
76      R_386_16 thru R_386_PC8 to form an index into this table.  */
77 #define R_386_standard (R_386_GOTPC + 1)
78 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
79
80   /* These relocs are a GNU extension.  */
81   HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
82         bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
83         TRUE, 0xffffffff, 0xffffffff, FALSE),
84   HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
85         bfd_elf_generic_reloc, "R_386_TLS_IE",
86         TRUE, 0xffffffff, 0xffffffff, FALSE),
87   HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
88         bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
89         TRUE, 0xffffffff, 0xffffffff, FALSE),
90   HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
91         bfd_elf_generic_reloc, "R_386_TLS_LE",
92         TRUE, 0xffffffff, 0xffffffff, FALSE),
93   HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
94         bfd_elf_generic_reloc, "R_386_TLS_GD",
95         TRUE, 0xffffffff, 0xffffffff, FALSE),
96   HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
97         bfd_elf_generic_reloc, "R_386_TLS_LDM",
98         TRUE, 0xffffffff, 0xffffffff, FALSE),
99   HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
100         bfd_elf_generic_reloc, "R_386_16",
101         TRUE, 0xffff, 0xffff, FALSE),
102   HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
103         bfd_elf_generic_reloc, "R_386_PC16",
104         TRUE, 0xffff, 0xffff, TRUE),
105   HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
106         bfd_elf_generic_reloc, "R_386_8",
107         TRUE, 0xff, 0xff, FALSE),
108   HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
109         bfd_elf_generic_reloc, "R_386_PC8",
110         TRUE, 0xff, 0xff, TRUE),
111
112 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
113 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
114   /* These are common with Solaris TLS implementation.  */
115   HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
116         bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
117         TRUE, 0xffffffff, 0xffffffff, FALSE),
118   HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
119         bfd_elf_generic_reloc, "R_386_TLS_IE_32",
120         TRUE, 0xffffffff, 0xffffffff, FALSE),
121   HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
122         bfd_elf_generic_reloc, "R_386_TLS_LE_32",
123         TRUE, 0xffffffff, 0xffffffff, FALSE),
124   HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
125         bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
126         TRUE, 0xffffffff, 0xffffffff, FALSE),
127   HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
128         bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
129         TRUE, 0xffffffff, 0xffffffff, FALSE),
130   HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
131         bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
132         TRUE, 0xffffffff, 0xffffffff, FALSE),
133   EMPTY_HOWTO (38),
134   HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
135         bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
136         TRUE, 0xffffffff, 0xffffffff, FALSE),
137   HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
138         bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
139         FALSE, 0, 0, FALSE),
140   HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
141         bfd_elf_generic_reloc, "R_386_TLS_DESC",
142         TRUE, 0xffffffff, 0xffffffff, FALSE),
143   HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
144         bfd_elf_generic_reloc, "R_386_IRELATIVE",
145         TRUE, 0xffffffff, 0xffffffff, FALSE),
146
147   /* Another gap.  */
148 #define R_386_irelative (R_386_IRELATIVE + 1 - R_386_tls_offset)
149 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_irelative)
150
151 /* GNU extension to record C++ vtable hierarchy.  */
152   HOWTO (R_386_GNU_VTINHERIT,   /* type */
153          0,                     /* rightshift */
154          2,                     /* size (0 = byte, 1 = short, 2 = long) */
155          0,                     /* bitsize */
156          FALSE,                 /* pc_relative */
157          0,                     /* bitpos */
158          complain_overflow_dont, /* complain_on_overflow */
159          NULL,                  /* special_function */
160          "R_386_GNU_VTINHERIT", /* name */
161          FALSE,                 /* partial_inplace */
162          0,                     /* src_mask */
163          0,                     /* dst_mask */
164          FALSE),                /* pcrel_offset */
165
166 /* GNU extension to record C++ vtable member usage.  */
167   HOWTO (R_386_GNU_VTENTRY,     /* type */
168          0,                     /* rightshift */
169          2,                     /* size (0 = byte, 1 = short, 2 = long) */
170          0,                     /* bitsize */
171          FALSE,                 /* pc_relative */
172          0,                     /* bitpos */
173          complain_overflow_dont, /* complain_on_overflow */
174          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
175          "R_386_GNU_VTENTRY",   /* name */
176          FALSE,                 /* partial_inplace */
177          0,                     /* src_mask */
178          0,                     /* dst_mask */
179          FALSE)                 /* pcrel_offset */
180
181 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
182
183 };
184
185 #ifdef DEBUG_GEN_RELOC
186 #define TRACE(str) \
187   fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
188 #else
189 #define TRACE(str)
190 #endif
191
192 static reloc_howto_type *
193 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
194                             bfd_reloc_code_real_type code)
195 {
196   switch (code)
197     {
198     case BFD_RELOC_NONE:
199       TRACE ("BFD_RELOC_NONE");
200       return &elf_howto_table[R_386_NONE];
201
202     case BFD_RELOC_32:
203       TRACE ("BFD_RELOC_32");
204       return &elf_howto_table[R_386_32];
205
206     case BFD_RELOC_CTOR:
207       TRACE ("BFD_RELOC_CTOR");
208       return &elf_howto_table[R_386_32];
209
210     case BFD_RELOC_32_PCREL:
211       TRACE ("BFD_RELOC_PC32");
212       return &elf_howto_table[R_386_PC32];
213
214     case BFD_RELOC_386_GOT32:
215       TRACE ("BFD_RELOC_386_GOT32");
216       return &elf_howto_table[R_386_GOT32];
217
218     case BFD_RELOC_386_PLT32:
219       TRACE ("BFD_RELOC_386_PLT32");
220       return &elf_howto_table[R_386_PLT32];
221
222     case BFD_RELOC_386_COPY:
223       TRACE ("BFD_RELOC_386_COPY");
224       return &elf_howto_table[R_386_COPY];
225
226     case BFD_RELOC_386_GLOB_DAT:
227       TRACE ("BFD_RELOC_386_GLOB_DAT");
228       return &elf_howto_table[R_386_GLOB_DAT];
229
230     case BFD_RELOC_386_JUMP_SLOT:
231       TRACE ("BFD_RELOC_386_JUMP_SLOT");
232       return &elf_howto_table[R_386_JUMP_SLOT];
233
234     case BFD_RELOC_386_RELATIVE:
235       TRACE ("BFD_RELOC_386_RELATIVE");
236       return &elf_howto_table[R_386_RELATIVE];
237
238     case BFD_RELOC_386_GOTOFF:
239       TRACE ("BFD_RELOC_386_GOTOFF");
240       return &elf_howto_table[R_386_GOTOFF];
241
242     case BFD_RELOC_386_GOTPC:
243       TRACE ("BFD_RELOC_386_GOTPC");
244       return &elf_howto_table[R_386_GOTPC];
245
246       /* These relocs are a GNU extension.  */
247     case BFD_RELOC_386_TLS_TPOFF:
248       TRACE ("BFD_RELOC_386_TLS_TPOFF");
249       return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
250
251     case BFD_RELOC_386_TLS_IE:
252       TRACE ("BFD_RELOC_386_TLS_IE");
253       return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
254
255     case BFD_RELOC_386_TLS_GOTIE:
256       TRACE ("BFD_RELOC_386_TLS_GOTIE");
257       return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
258
259     case BFD_RELOC_386_TLS_LE:
260       TRACE ("BFD_RELOC_386_TLS_LE");
261       return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
262
263     case BFD_RELOC_386_TLS_GD:
264       TRACE ("BFD_RELOC_386_TLS_GD");
265       return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
266
267     case BFD_RELOC_386_TLS_LDM:
268       TRACE ("BFD_RELOC_386_TLS_LDM");
269       return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
270
271     case BFD_RELOC_16:
272       TRACE ("BFD_RELOC_16");
273       return &elf_howto_table[R_386_16 - R_386_ext_offset];
274
275     case BFD_RELOC_16_PCREL:
276       TRACE ("BFD_RELOC_16_PCREL");
277       return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
278
279     case BFD_RELOC_8:
280       TRACE ("BFD_RELOC_8");
281       return &elf_howto_table[R_386_8 - R_386_ext_offset];
282
283     case BFD_RELOC_8_PCREL:
284       TRACE ("BFD_RELOC_8_PCREL");
285       return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
286
287     /* Common with Sun TLS implementation.  */
288     case BFD_RELOC_386_TLS_LDO_32:
289       TRACE ("BFD_RELOC_386_TLS_LDO_32");
290       return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
291
292     case BFD_RELOC_386_TLS_IE_32:
293       TRACE ("BFD_RELOC_386_TLS_IE_32");
294       return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
295
296     case BFD_RELOC_386_TLS_LE_32:
297       TRACE ("BFD_RELOC_386_TLS_LE_32");
298       return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
299
300     case BFD_RELOC_386_TLS_DTPMOD32:
301       TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
302       return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
303
304     case BFD_RELOC_386_TLS_DTPOFF32:
305       TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
306       return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
307
308     case BFD_RELOC_386_TLS_TPOFF32:
309       TRACE ("BFD_RELOC_386_TLS_TPOFF32");
310       return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
311
312     case BFD_RELOC_386_TLS_GOTDESC:
313       TRACE ("BFD_RELOC_386_TLS_GOTDESC");
314       return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
315
316     case BFD_RELOC_386_TLS_DESC_CALL:
317       TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
318       return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
319
320     case BFD_RELOC_386_TLS_DESC:
321       TRACE ("BFD_RELOC_386_TLS_DESC");
322       return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
323
324     case BFD_RELOC_386_IRELATIVE:
325       TRACE ("BFD_RELOC_386_IRELATIVE");
326       return &elf_howto_table[R_386_IRELATIVE];
327
328     case BFD_RELOC_VTABLE_INHERIT:
329       TRACE ("BFD_RELOC_VTABLE_INHERIT");
330       return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
331
332     case BFD_RELOC_VTABLE_ENTRY:
333       TRACE ("BFD_RELOC_VTABLE_ENTRY");
334       return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
335
336     default:
337       break;
338     }
339
340   TRACE ("Unknown");
341   return 0;
342 }
343
344 static reloc_howto_type *
345 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
346                             const char *r_name)
347 {
348   unsigned int i;
349
350   for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
351     if (elf_howto_table[i].name != NULL
352         && strcasecmp (elf_howto_table[i].name, r_name) == 0)
353       return &elf_howto_table[i];
354
355   return NULL;
356 }
357
358 static reloc_howto_type *
359 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
360 {
361   unsigned int indx;
362
363   if ((indx = r_type) >= R_386_standard
364       && ((indx = r_type - R_386_ext_offset) - R_386_standard
365           >= R_386_ext - R_386_standard)
366       && ((indx = r_type - R_386_tls_offset) - R_386_ext
367           >= R_386_irelative - R_386_ext)
368       && ((indx = r_type - R_386_vt_offset) - R_386_irelative
369           >= R_386_vt - R_386_irelative))
370     {
371       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
372                              abfd, (int) r_type);
373       indx = R_386_NONE;
374     }
375   BFD_ASSERT (elf_howto_table [indx].type == r_type);
376   return &elf_howto_table[indx];
377 }
378
379 static void
380 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
381                             arelent *cache_ptr,
382                             Elf_Internal_Rela *dst)
383 {
384   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
385   cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
386 }
387
388 /* Return whether a symbol name implies a local label.  The UnixWare
389    2.1 cc generates temporary symbols that start with .X, so we
390    recognize them here.  FIXME: do other SVR4 compilers also use .X?.
391    If so, we should move the .X recognition into
392    _bfd_elf_is_local_label_name.  */
393
394 static bfd_boolean
395 elf_i386_is_local_label_name (bfd *abfd, const char *name)
396 {
397   if (name[0] == '.' && name[1] == 'X')
398     return TRUE;
399
400   return _bfd_elf_is_local_label_name (abfd, name);
401 }
402 \f
403 /* Support for core dump NOTE sections.  */
404
405 static bfd_boolean
406 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
407 {
408   int offset;
409   size_t size;
410
411   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
412     {
413       int pr_version = bfd_get_32 (abfd, note->descdata);
414
415       if (pr_version != 1)
416         return FALSE;
417
418       /* pr_cursig */
419       elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 20);
420
421       /* pr_pid */
422       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
423
424       /* pr_reg */
425       offset = 28;
426       size = bfd_get_32 (abfd, note->descdata + 8);
427     }
428   else
429     {
430       switch (note->descsz)
431         {
432         default:
433           return FALSE;
434
435         case 144:               /* Linux/i386 */
436           /* pr_cursig */
437           elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
438
439           /* pr_pid */
440           elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
441
442           /* pr_reg */
443           offset = 72;
444           size = 68;
445
446           break;
447         }
448     }
449
450   /* Make a ".reg/999" section.  */
451   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
452                                           size, note->descpos + offset);
453 }
454
455 static bfd_boolean
456 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
457 {
458   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
459     {
460       int pr_version = bfd_get_32 (abfd, note->descdata);
461
462       if (pr_version != 1)
463         return FALSE;
464
465       elf_tdata (abfd)->core_program
466         = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
467       elf_tdata (abfd)->core_command
468         = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
469     }
470   else
471     {
472       switch (note->descsz)
473         {
474         default:
475           return FALSE;
476
477         case 124:               /* Linux/i386 elf_prpsinfo.  */
478           elf_tdata (abfd)->core_program
479             = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
480           elf_tdata (abfd)->core_command
481             = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
482         }
483     }
484
485   /* Note that for some reason, a spurious space is tacked
486      onto the end of the args in some (at least one anyway)
487      implementations, so strip it off if it exists.  */
488   {
489     char *command = elf_tdata (abfd)->core_command;
490     int n = strlen (command);
491
492     if (0 < n && command[n - 1] == ' ')
493       command[n - 1] = '\0';
494   }
495
496   return TRUE;
497 }
498 \f
499 /* Functions for the i386 ELF linker.
500
501    In order to gain some understanding of code in this file without
502    knowing all the intricate details of the linker, note the
503    following:
504
505    Functions named elf_i386_* are called by external routines, other
506    functions are only called locally.  elf_i386_* functions appear
507    in this file more or less in the order in which they are called
508    from external routines.  eg. elf_i386_check_relocs is called
509    early in the link process, elf_i386_finish_dynamic_sections is
510    one of the last functions.  */
511
512
513 /* The name of the dynamic interpreter.  This is put in the .interp
514    section.  */
515
516 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
517
518 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
519    copying dynamic variables from a shared lib into an app's dynbss
520    section, and instead use a dynamic relocation to point into the
521    shared lib.  */
522 #define ELIMINATE_COPY_RELOCS 1
523
524 /* The size in bytes of an entry in the procedure linkage table.  */
525
526 #define PLT_ENTRY_SIZE 16
527
528 /* The first entry in an absolute procedure linkage table looks like
529    this.  See the SVR4 ABI i386 supplement to see how this works.
530    Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte.  */
531
532 static const bfd_byte elf_i386_plt0_entry[12] =
533 {
534   0xff, 0x35,   /* pushl contents of address */
535   0, 0, 0, 0,   /* replaced with address of .got + 4.  */
536   0xff, 0x25,   /* jmp indirect */
537   0, 0, 0, 0    /* replaced with address of .got + 8.  */
538 };
539
540 /* Subsequent entries in an absolute procedure linkage table look like
541    this.  */
542
543 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
544 {
545   0xff, 0x25,   /* jmp indirect */
546   0, 0, 0, 0,   /* replaced with address of this symbol in .got.  */
547   0x68,         /* pushl immediate */
548   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
549   0xe9,         /* jmp relative */
550   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
551 };
552
553 /* The first entry in a PIC procedure linkage table look like this.
554    Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte.  */
555
556 static const bfd_byte elf_i386_pic_plt0_entry[12] =
557 {
558   0xff, 0xb3, 4, 0, 0, 0,       /* pushl 4(%ebx) */
559   0xff, 0xa3, 8, 0, 0, 0        /* jmp *8(%ebx) */
560 };
561
562 /* Subsequent entries in a PIC procedure linkage table look like this.  */
563
564 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
565 {
566   0xff, 0xa3,   /* jmp *offset(%ebx) */
567   0, 0, 0, 0,   /* replaced with offset of this symbol in .got.  */
568   0x68,         /* pushl immediate */
569   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
570   0xe9,         /* jmp relative */
571   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
572 };
573
574 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
575    for the PLTResolve stub and then for each PLT entry.  */
576 #define PLTRESOLVE_RELOCS_SHLIB 0
577 #define PLTRESOLVE_RELOCS 2
578 #define PLT_NON_JUMP_SLOT_RELOCS 2
579
580 /* i386 ELF linker hash entry.  */
581
582 struct elf_i386_link_hash_entry
583 {
584   struct elf_link_hash_entry elf;
585
586   /* Track dynamic relocs copied for this symbol.  */
587   struct elf_dyn_relocs *dyn_relocs;
588
589 #define GOT_UNKNOWN     0
590 #define GOT_NORMAL      1
591 #define GOT_TLS_GD      2
592 #define GOT_TLS_IE      4
593 #define GOT_TLS_IE_POS  5
594 #define GOT_TLS_IE_NEG  6
595 #define GOT_TLS_IE_BOTH 7
596 #define GOT_TLS_GDESC   8
597 #define GOT_TLS_GD_BOTH_P(type)                                         \
598   ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
599 #define GOT_TLS_GD_P(type)                                              \
600   ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
601 #define GOT_TLS_GDESC_P(type)                                           \
602   ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
603 #define GOT_TLS_GD_ANY_P(type)                                          \
604   (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
605   unsigned char tls_type;
606
607   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
608      starting at the end of the jump table.  */
609   bfd_vma tlsdesc_got;
610 };
611
612 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
613
614 struct elf_i386_obj_tdata
615 {
616   struct elf_obj_tdata root;
617
618   /* tls_type for each local got entry.  */
619   char *local_got_tls_type;
620
621   /* GOTPLT entries for TLS descriptors.  */
622   bfd_vma *local_tlsdesc_gotent;
623 };
624
625 #define elf_i386_tdata(abfd) \
626   ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
627
628 #define elf_i386_local_got_tls_type(abfd) \
629   (elf_i386_tdata (abfd)->local_got_tls_type)
630
631 #define elf_i386_local_tlsdesc_gotent(abfd) \
632   (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
633
634 #define is_i386_elf(bfd)                                \
635   (bfd_get_flavour (bfd) == bfd_target_elf_flavour      \
636    && elf_tdata (bfd) != NULL                           \
637    && elf_object_id (bfd) == I386_ELF_TDATA)
638
639 static bfd_boolean
640 elf_i386_mkobject (bfd *abfd)
641 {
642   return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
643                                   I386_ELF_TDATA);
644 }
645
646 /* i386 ELF linker hash table.  */
647
648 struct elf_i386_link_hash_table
649 {
650   struct elf_link_hash_table elf;
651
652   /* Short-cuts to get to dynamic linker sections.  */
653   asection *sdynbss;
654   asection *srelbss;
655
656   /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.  */
657   asection *srelplt2;
658
659   /* True if the target system is VxWorks.  */
660   int is_vxworks;
661
662   /* Value used to fill the last word of the first plt entry.  */
663   bfd_byte plt0_pad_byte;
664
665   /* The index of the next unused R_386_TLS_DESC slot in .rel.plt.  */
666   bfd_vma next_tls_desc_index;
667
668   union {
669     bfd_signed_vma refcount;
670     bfd_vma offset;
671   } tls_ldm_got;
672
673   /* The amount of space used by the reserved portion of the sgotplt
674      section, plus whatever space is used by the jump slots.  */
675   bfd_vma sgotplt_jump_table_size;
676
677   /* Small local sym to section mapping cache.  */
678   struct sym_sec_cache sym_sec;
679
680   /* _TLS_MODULE_BASE_ symbol.  */
681   struct bfd_link_hash_entry *tls_module_base;
682
683   /* Used by local STT_GNU_IFUNC symbols.  */
684   htab_t loc_hash_table;
685   void *loc_hash_memory;
686 };
687
688 /* Get the i386 ELF linker hash table from a link_info structure.  */
689
690 #define elf_i386_hash_table(p) \
691   ((struct elf_i386_link_hash_table *) ((p)->hash))
692
693 #define elf_i386_compute_jump_table_size(htab) \
694   ((htab)->next_tls_desc_index * 4)
695
696 /* Create an entry in an i386 ELF linker hash table.  */
697
698 static struct bfd_hash_entry *
699 elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
700                             struct bfd_hash_table *table,
701                             const char *string)
702 {
703   /* Allocate the structure if it has not already been allocated by a
704      subclass.  */
705   if (entry == NULL)
706     {
707       entry = bfd_hash_allocate (table,
708                                  sizeof (struct elf_i386_link_hash_entry));
709       if (entry == NULL)
710         return entry;
711     }
712
713   /* Call the allocation method of the superclass.  */
714   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
715   if (entry != NULL)
716     {
717       struct elf_i386_link_hash_entry *eh;
718
719       eh = (struct elf_i386_link_hash_entry *) entry;
720       eh->dyn_relocs = NULL;
721       eh->tls_type = GOT_UNKNOWN;
722       eh->tlsdesc_got = (bfd_vma) -1;
723     }
724
725   return entry;
726 }
727
728 /* Compute a hash of a local hash entry.  We use elf_link_hash_entry
729   for local symbol so that we can handle local STT_GNU_IFUNC symbols
730   as global symbol.  We reuse indx and dynstr_index for local symbol
731   hash since they aren't used by global symbols in this backend.  */
732
733 static hashval_t
734 elf_i386_local_htab_hash (const void *ptr)
735 {
736   struct elf_link_hash_entry *h
737     = (struct elf_link_hash_entry *) ptr;
738   return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
739 }
740
741 /* Compare local hash entries.  */
742
743 static int
744 elf_i386_local_htab_eq (const void *ptr1, const void *ptr2)
745 {
746   struct elf_link_hash_entry *h1
747      = (struct elf_link_hash_entry *) ptr1;
748   struct elf_link_hash_entry *h2
749     = (struct elf_link_hash_entry *) ptr2;
750
751   return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
752 }
753
754 /* Find and/or create a hash entry for local symbol.  */
755
756 static struct elf_link_hash_entry *
757 elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab,
758                              bfd *abfd, const Elf_Internal_Rela *rel,
759                              bfd_boolean create)
760 {
761   struct elf_i386_link_hash_entry e, *ret;
762   asection *sec = abfd->sections;
763   hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
764                                        ELF32_R_SYM (rel->r_info));
765   void **slot;
766
767   e.elf.indx = sec->id;
768   e.elf.dynstr_index = ELF32_R_SYM (rel->r_info);
769   slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
770                                    create ? INSERT : NO_INSERT);
771
772   if (!slot)
773     return NULL;
774
775   if (*slot)
776     {
777       ret = (struct elf_i386_link_hash_entry *) *slot;
778       return &ret->elf;
779     }
780
781   ret = (struct elf_i386_link_hash_entry *)
782         objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
783                         sizeof (struct elf_i386_link_hash_entry));
784   if (ret)
785     {
786       memset (ret, 0, sizeof (*ret));
787       ret->elf.indx = sec->id;
788       ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
789       ret->elf.dynindx = -1;
790       ret->elf.plt.offset = (bfd_vma) -1;
791       ret->elf.got.offset = (bfd_vma) -1;
792       *slot = ret;
793     }
794   return &ret->elf;
795 }
796
797 /* Create an i386 ELF linker hash table.  */
798
799 static struct bfd_link_hash_table *
800 elf_i386_link_hash_table_create (bfd *abfd)
801 {
802   struct elf_i386_link_hash_table *ret;
803   bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
804
805   ret = bfd_malloc (amt);
806   if (ret == NULL)
807     return NULL;
808
809   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
810                                       elf_i386_link_hash_newfunc,
811                                       sizeof (struct elf_i386_link_hash_entry)))
812     {
813       free (ret);
814       return NULL;
815     }
816
817   ret->sdynbss = NULL;
818   ret->srelbss = NULL;
819   ret->tls_ldm_got.refcount = 0;
820   ret->next_tls_desc_index = 0;
821   ret->sgotplt_jump_table_size = 0;
822   ret->sym_sec.abfd = NULL;
823   ret->is_vxworks = 0;
824   ret->srelplt2 = NULL;
825   ret->plt0_pad_byte = 0;
826   ret->tls_module_base = NULL;
827
828   ret->loc_hash_table = htab_try_create (1024,
829                                          elf_i386_local_htab_hash,
830                                          elf_i386_local_htab_eq,
831                                          NULL);
832   ret->loc_hash_memory = objalloc_create ();
833   if (!ret->loc_hash_table || !ret->loc_hash_memory)
834     {
835       free (ret);
836       return NULL;
837     }
838
839   return &ret->elf.root;
840 }
841
842 /* Destroy an i386 ELF linker hash table.  */
843
844 static void
845 elf_i386_link_hash_table_free (struct bfd_link_hash_table *hash)
846 {
847   struct elf_i386_link_hash_table *htab
848     = (struct elf_i386_link_hash_table *) hash;
849
850   if (htab->loc_hash_table)
851     htab_delete (htab->loc_hash_table);
852   if (htab->loc_hash_memory)
853     objalloc_free ((struct objalloc *) htab->loc_hash_memory);
854   _bfd_generic_link_hash_table_free (hash);
855 }
856
857 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
858    .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
859    hash table.  */
860
861 static bfd_boolean
862 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
863 {
864   struct elf_i386_link_hash_table *htab;
865
866   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
867     return FALSE;
868
869   htab = elf_i386_hash_table (info);
870   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
871   if (!info->shared)
872     htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
873
874   if (!htab->sdynbss
875       || (!info->shared && !htab->srelbss))
876     abort ();
877
878   if (htab->is_vxworks
879       && !elf_vxworks_create_dynamic_sections (dynobj, info,
880                                                &htab->srelplt2))
881     return FALSE;
882
883   return TRUE;
884 }
885
886 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
887
888 static void
889 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
890                                struct elf_link_hash_entry *dir,
891                                struct elf_link_hash_entry *ind)
892 {
893   struct elf_i386_link_hash_entry *edir, *eind;
894
895   edir = (struct elf_i386_link_hash_entry *) dir;
896   eind = (struct elf_i386_link_hash_entry *) ind;
897
898   if (eind->dyn_relocs != NULL)
899     {
900       if (edir->dyn_relocs != NULL)
901         {
902           struct elf_dyn_relocs **pp;
903           struct elf_dyn_relocs *p;
904
905           /* Add reloc counts against the indirect sym to the direct sym
906              list.  Merge any entries against the same section.  */
907           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
908             {
909               struct elf_dyn_relocs *q;
910
911               for (q = edir->dyn_relocs; q != NULL; q = q->next)
912                 if (q->sec == p->sec)
913                   {
914                     q->pc_count += p->pc_count;
915                     q->count += p->count;
916                     *pp = p->next;
917                     break;
918                   }
919               if (q == NULL)
920                 pp = &p->next;
921             }
922           *pp = edir->dyn_relocs;
923         }
924
925       edir->dyn_relocs = eind->dyn_relocs;
926       eind->dyn_relocs = NULL;
927     }
928
929   if (ind->root.type == bfd_link_hash_indirect
930       && dir->got.refcount <= 0)
931     {
932       edir->tls_type = eind->tls_type;
933       eind->tls_type = GOT_UNKNOWN;
934     }
935
936   if (ELIMINATE_COPY_RELOCS
937       && ind->root.type != bfd_link_hash_indirect
938       && dir->dynamic_adjusted)
939     {
940       /* If called to transfer flags for a weakdef during processing
941          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
942          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
943       dir->ref_dynamic |= ind->ref_dynamic;
944       dir->ref_regular |= ind->ref_regular;
945       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
946       dir->needs_plt |= ind->needs_plt;
947       dir->pointer_equality_needed |= ind->pointer_equality_needed;
948     }
949   else
950     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
951 }
952
953 typedef union 
954   {
955     unsigned char c[2];
956     uint16_t i;
957   }
958 i386_opcode16;
959
960 /* Return TRUE if the TLS access code sequence support transition
961    from R_TYPE.  */
962
963 static bfd_boolean
964 elf_i386_check_tls_transition (bfd *abfd, asection *sec,
965                                bfd_byte *contents,
966                                Elf_Internal_Shdr *symtab_hdr,
967                                struct elf_link_hash_entry **sym_hashes,
968                                unsigned int r_type,
969                                const Elf_Internal_Rela *rel,
970                                const Elf_Internal_Rela *relend)
971 {
972   unsigned int val, type;
973   unsigned long r_symndx;
974   struct elf_link_hash_entry *h;
975   bfd_vma offset;
976
977   /* Get the section contents.  */
978   if (contents == NULL)
979     {
980       if (elf_section_data (sec)->this_hdr.contents != NULL)
981         contents = elf_section_data (sec)->this_hdr.contents;
982       else
983         {
984           /* FIXME: How to better handle error condition?  */
985           if (!bfd_malloc_and_get_section (abfd, sec, &contents))
986             return FALSE;
987
988           /* Cache the section contents for elf_link_input_bfd.  */
989           elf_section_data (sec)->this_hdr.contents = contents;
990         }
991     }
992
993   offset = rel->r_offset;
994   switch (r_type)
995     {
996     case R_386_TLS_GD:
997     case R_386_TLS_LDM:
998       if (offset < 2 || (rel + 1) >= relend)
999         return FALSE;
1000
1001       type = bfd_get_8 (abfd, contents + offset - 2);
1002       if (r_type == R_386_TLS_GD)
1003         {
1004           /* Check transition from GD access model.  Only
1005                 leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
1006                 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
1007              can transit to different access model.  */
1008           if ((offset + 10) > sec->size ||
1009               (type != 0x8d && type != 0x04))
1010             return FALSE;
1011
1012           val = bfd_get_8 (abfd, contents + offset - 1);
1013           if (type == 0x04)
1014             {
1015               /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
1016               if (offset < 3)
1017                 return FALSE;
1018
1019               if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
1020                 return FALSE;
1021
1022               if ((val & 0xc7) != 0x05 || val == (4 << 3))
1023                 return FALSE;
1024             }
1025           else
1026             {
1027               /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop  */
1028               if ((val & 0xf8) != 0x80 || (val & 7) == 4)
1029                 return FALSE;
1030
1031               if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
1032                 return FALSE;
1033             }
1034         }
1035       else
1036         {
1037           /* Check transition from LD access model.  Only
1038                 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
1039              can transit to different access model.  */
1040           if (type != 0x8d || (offset + 9) > sec->size)
1041             return FALSE;
1042
1043           val = bfd_get_8 (abfd, contents + offset - 1);
1044           if ((val & 0xf8) != 0x80 || (val & 7) == 4)
1045             return FALSE;
1046         }
1047
1048       if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
1049         return FALSE;
1050
1051       r_symndx = ELF32_R_SYM (rel[1].r_info);
1052       if (r_symndx < symtab_hdr->sh_info)
1053         return FALSE;
1054
1055       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1056       /* Use strncmp to check ___tls_get_addr since ___tls_get_addr
1057          may be versioned.  */
1058       return (h != NULL
1059               && h->root.root.string != NULL
1060               && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1061                   || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
1062               && (strncmp (h->root.root.string, "___tls_get_addr",
1063                            15) == 0));
1064
1065     case R_386_TLS_IE:
1066       /* Check transition from IE access model:
1067                 movl foo@indntpoff(%rip), %eax
1068                 movl foo@indntpoff(%rip), %reg
1069                 addl foo@indntpoff(%rip), %reg
1070        */
1071
1072       if (offset < 1 || (offset + 4) > sec->size)
1073         return FALSE;
1074
1075       /* Check "movl foo@tpoff(%rip), %eax" first.  */
1076       val = bfd_get_8 (abfd, contents + offset - 1);
1077       if (val == 0xa1)
1078         return TRUE;
1079
1080       if (offset < 2)
1081         return FALSE;
1082
1083       /* Check movl|addl foo@tpoff(%rip), %reg.   */
1084       type = bfd_get_8 (abfd, contents + offset - 2);
1085       return ((type == 0x8b || type == 0x03)
1086               && (val & 0xc7) == 0x05);
1087
1088     case R_386_TLS_GOTIE:
1089     case R_386_TLS_IE_32:
1090       /* Check transition from {IE_32,GOTIE} access model:
1091                 subl foo@{tpoff,gontoff}(%reg1), %reg2
1092                 movl foo@{tpoff,gontoff}(%reg1), %reg2
1093                 addl foo@{tpoff,gontoff}(%reg1), %reg2
1094        */
1095
1096       if (offset < 2 || (offset + 4) > sec->size)
1097         return FALSE;
1098
1099       val = bfd_get_8 (abfd, contents + offset - 1);
1100       if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1101         return FALSE;
1102
1103       type = bfd_get_8 (abfd, contents + offset - 2);
1104       return type == 0x8b || type == 0x2b || type == 0x03;
1105
1106     case R_386_TLS_GOTDESC:
1107       /* Check transition from GDesc access model:
1108                 leal x@tlsdesc(%ebx), %eax
1109
1110          Make sure it's a leal adding ebx to a 32-bit offset
1111          into any register, although it's probably almost always
1112          going to be eax.  */
1113
1114       if (offset < 2 || (offset + 4) > sec->size)
1115         return FALSE;
1116
1117       if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1118         return FALSE;
1119
1120       val = bfd_get_8 (abfd, contents + offset - 1);
1121       return (val & 0xc7) == 0x83;
1122
1123     case R_386_TLS_DESC_CALL:
1124       /* Check transition from GDesc access model:
1125                 call *x@tlsdesc(%rax)
1126        */
1127       if (offset + 2 <= sec->size)
1128         {
1129           /* Make sure that it's a call *x@tlsdesc(%rax).  */
1130           static i386_opcode16 call = { { 0xff, 0x10 } };
1131           return bfd_get_16 (abfd, contents + offset) == call.i;
1132         }
1133
1134       return FALSE;
1135
1136     default:
1137       abort ();
1138     }
1139 }
1140
1141 /* Return TRUE if the TLS access transition is OK or no transition
1142    will be performed.  Update R_TYPE if there is a transition.  */
1143
1144 static bfd_boolean
1145 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1146                          asection *sec, bfd_byte *contents,
1147                          Elf_Internal_Shdr *symtab_hdr,
1148                          struct elf_link_hash_entry **sym_hashes,
1149                          unsigned int *r_type, int tls_type,
1150                          const Elf_Internal_Rela *rel,
1151                          const Elf_Internal_Rela *relend,
1152                          struct elf_link_hash_entry *h)
1153 {
1154   unsigned int from_type = *r_type;
1155   unsigned int to_type = from_type;
1156   bfd_boolean check = TRUE;
1157
1158   switch (from_type)
1159     {
1160     case R_386_TLS_GD:
1161     case R_386_TLS_GOTDESC:
1162     case R_386_TLS_DESC_CALL:
1163     case R_386_TLS_IE_32:
1164     case R_386_TLS_IE:
1165     case R_386_TLS_GOTIE:
1166       if (!info->shared)
1167         {
1168           if (h == NULL)
1169             to_type = R_386_TLS_LE_32;
1170           else if (from_type != R_386_TLS_IE
1171                    && from_type != R_386_TLS_GOTIE)
1172             to_type = R_386_TLS_IE_32;
1173         }
1174
1175       /* When we are called from elf_i386_relocate_section, CONTENTS
1176          isn't NULL and there may be additional transitions based on
1177          TLS_TYPE.  */
1178       if (contents != NULL)
1179         {
1180           unsigned int new_to_type = to_type;
1181
1182           if (!info->shared
1183               && h != NULL
1184               && h->dynindx == -1
1185               && (tls_type & GOT_TLS_IE))
1186             new_to_type = R_386_TLS_LE_32;
1187
1188           if (to_type == R_386_TLS_GD
1189               || to_type == R_386_TLS_GOTDESC
1190               || to_type == R_386_TLS_DESC_CALL)
1191             {
1192               if (tls_type == GOT_TLS_IE_POS)
1193                 new_to_type = R_386_TLS_GOTIE;
1194               else if (tls_type & GOT_TLS_IE)
1195                 new_to_type = R_386_TLS_IE_32;
1196             }
1197
1198           /* We checked the transition before when we were called from
1199              elf_i386_check_relocs.  We only want to check the new
1200              transition which hasn't been checked before.  */
1201           check = new_to_type != to_type && from_type == to_type;
1202           to_type = new_to_type;
1203         }
1204
1205       break;
1206
1207     case R_386_TLS_LDM:
1208       if (!info->shared)
1209         to_type = R_386_TLS_LE_32;
1210       break;
1211
1212     default:
1213       return TRUE;
1214     }
1215
1216   /* Return TRUE if there is no transition.  */
1217   if (from_type == to_type)
1218     return TRUE;
1219
1220   /* Check if the transition can be performed.  */
1221   if (check
1222       && ! elf_i386_check_tls_transition (abfd, sec, contents,
1223                                           symtab_hdr, sym_hashes,
1224                                           from_type, rel, relend))
1225     {
1226       reloc_howto_type *from, *to;
1227
1228       from = elf_i386_rtype_to_howto (abfd, from_type);
1229       to = elf_i386_rtype_to_howto (abfd, to_type);
1230
1231       (*_bfd_error_handler)
1232         (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1233            "in section `%A' failed"),
1234          abfd, sec, from->name, to->name,
1235          h ? h->root.root.string : "a local symbol",
1236          (unsigned long) rel->r_offset);
1237       bfd_set_error (bfd_error_bad_value);
1238       return FALSE;
1239     }
1240
1241   *r_type = to_type;
1242   return TRUE;
1243 }
1244
1245 /* Look through the relocs for a section during the first phase, and
1246    calculate needed space in the global offset table, procedure linkage
1247    table, and dynamic reloc sections.  */
1248
1249 static bfd_boolean
1250 elf_i386_check_relocs (bfd *abfd,
1251                        struct bfd_link_info *info,
1252                        asection *sec,
1253                        const Elf_Internal_Rela *relocs)
1254 {
1255   struct elf_i386_link_hash_table *htab;
1256   Elf_Internal_Shdr *symtab_hdr;
1257   struct elf_link_hash_entry **sym_hashes;
1258   const Elf_Internal_Rela *rel;
1259   const Elf_Internal_Rela *rel_end;
1260   asection *sreloc;
1261   Elf_Internal_Sym *isymbuf;
1262
1263   if (info->relocatable)
1264     return TRUE;
1265
1266   BFD_ASSERT (is_i386_elf (abfd));
1267
1268   htab = elf_i386_hash_table (info);
1269   symtab_hdr = &elf_symtab_hdr (abfd);
1270   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1271   sym_hashes = elf_sym_hashes (abfd);
1272
1273   sreloc = NULL;
1274
1275   rel_end = relocs + sec->reloc_count;
1276   for (rel = relocs; rel < rel_end; rel++)
1277     {
1278       unsigned int r_type;
1279       unsigned long r_symndx;
1280       struct elf_link_hash_entry *h;
1281
1282       r_symndx = ELF32_R_SYM (rel->r_info);
1283       r_type = ELF32_R_TYPE (rel->r_info);
1284
1285       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1286         {
1287           (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1288                                  abfd,
1289                                  r_symndx);
1290           return FALSE;
1291         }
1292
1293       if (r_symndx < symtab_hdr->sh_info)
1294         {
1295           /* A local symbol.  */
1296           Elf_Internal_Sym *isym;
1297
1298           /* Read this BFD's local symbols.  */
1299           if (isymbuf == NULL)
1300             {
1301               if (isymbuf == NULL)
1302                 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1303                                                 symtab_hdr->sh_info, 0,
1304                                                 NULL, NULL, NULL);
1305               if (isymbuf == NULL)
1306                   return FALSE;
1307             }
1308
1309           /* Check relocation against local STT_GNU_IFUNC symbol.  */
1310           isym = isymbuf + r_symndx;
1311           if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1312             {
1313               h = elf_i386_get_local_sym_hash (htab, abfd, rel,
1314                                                    TRUE);
1315               if (h == NULL)
1316                 goto error_return;
1317               
1318               /* Fake a STT_GNU_IFUNC symbol.  */
1319               h->type = STT_GNU_IFUNC;
1320               h->def_regular = 1;
1321               h->ref_regular = 1;
1322               h->forced_local = 1;
1323               h->root.type = bfd_link_hash_defined;
1324             }
1325           else
1326             h = NULL;
1327         }
1328       else
1329         {
1330           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1331           while (h->root.type == bfd_link_hash_indirect
1332                  || h->root.type == bfd_link_hash_warning)
1333             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1334         }
1335
1336       if (h != NULL)
1337         {
1338           /* Create the ifunc sections for static executables.  If we
1339              never see an indirect function symbol nor we are building
1340              a static executable, those sections will be empty and
1341              won't appear in output.  */
1342           switch (r_type)
1343             {
1344             default:
1345               break;
1346
1347             case R_386_32:
1348             case R_386_PC32:
1349             case R_386_PLT32:
1350             case R_386_GOT32:
1351             case R_386_GOTOFF:
1352               if (!_bfd_elf_create_ifunc_sections (abfd, info))
1353                 goto error_return;
1354               break;
1355             }
1356
1357           /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1358              it here if it is defined in a non-shared object.  */
1359           if (h->type == STT_GNU_IFUNC
1360               && h->def_regular)
1361             {
1362               /* It is referenced by a non-shared object. */
1363               h->ref_regular = 1;
1364  
1365               /* STT_GNU_IFUNC symbol must go through PLT.  */
1366               h->plt.refcount += 1;
1367
1368               /* STT_GNU_IFUNC needs dynamic sections.  */
1369               if (htab->elf.dynobj == NULL)
1370                 htab->elf.dynobj = abfd;
1371
1372               switch (r_type)
1373                 {
1374                 default:
1375                   (*_bfd_error_handler)
1376                     (_("%B: relocation %s against STT_GNU_IFUNC "
1377                        "symbol `%s' isn't handled by %s"), abfd,
1378                      elf_howto_table[r_type].name,
1379                      (h->root.root.string
1380                       ? h->root.root.string : "a local symbol"),
1381                      __FUNCTION__);
1382                   bfd_set_error (bfd_error_bad_value);
1383                   goto error_return;
1384
1385                 case R_386_32:
1386                   h->non_got_ref = 1;
1387                   h->pointer_equality_needed = 1;
1388                   if (info->shared)
1389                     {
1390                       /* We must copy these reloc types into the
1391                          output file.  Create a reloc section in
1392                          dynobj and make room for this reloc.  */
1393                       sreloc = _bfd_elf_create_ifunc_dyn_reloc
1394                         (abfd, info, sec, sreloc,
1395                          &((struct elf_i386_link_hash_entry *) h)->dyn_relocs);
1396                       if (sreloc == NULL)
1397                         goto error_return;
1398                     }
1399                   break;
1400
1401                 case R_386_PC32:
1402                   h->non_got_ref = 1;
1403                   break;
1404
1405                 case R_386_PLT32:
1406                   break;
1407
1408                 case R_386_GOT32:
1409                 case R_386_GOTOFF:
1410                   h->got.refcount += 1;
1411                   if (htab->elf.sgot == NULL
1412                       && !_bfd_elf_create_got_section (htab->elf.dynobj,
1413                                                        info))
1414                     goto error_return;
1415                   break;
1416                 }
1417
1418               continue;
1419             }
1420         }
1421
1422       if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1423                                      symtab_hdr, sym_hashes,
1424                                      &r_type, GOT_UNKNOWN,
1425                                      rel, rel_end, h)) 
1426         goto error_return;
1427
1428       switch (r_type)
1429         {
1430         case R_386_TLS_LDM:
1431           htab->tls_ldm_got.refcount += 1;
1432           goto create_got;
1433
1434         case R_386_PLT32:
1435           /* This symbol requires a procedure linkage table entry.  We
1436              actually build the entry in adjust_dynamic_symbol,
1437              because this might be a case of linking PIC code which is
1438              never referenced by a dynamic object, in which case we
1439              don't need to generate a procedure linkage table entry
1440              after all.  */
1441
1442           /* If this is a local symbol, we resolve it directly without
1443              creating a procedure linkage table entry.  */
1444           if (h == NULL)
1445             continue;
1446
1447           h->needs_plt = 1;
1448           h->plt.refcount += 1;
1449           break;
1450
1451         case R_386_TLS_IE_32:
1452         case R_386_TLS_IE:
1453         case R_386_TLS_GOTIE:
1454           if (info->shared)
1455             info->flags |= DF_STATIC_TLS;
1456           /* Fall through */
1457
1458         case R_386_GOT32:
1459         case R_386_TLS_GD:
1460         case R_386_TLS_GOTDESC:
1461         case R_386_TLS_DESC_CALL:
1462           /* This symbol requires a global offset table entry.  */
1463           {
1464             int tls_type, old_tls_type;
1465
1466             switch (r_type)
1467               {
1468               default:
1469               case R_386_GOT32: tls_type = GOT_NORMAL; break;
1470               case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1471               case R_386_TLS_GOTDESC:
1472               case R_386_TLS_DESC_CALL:
1473                 tls_type = GOT_TLS_GDESC; break;
1474               case R_386_TLS_IE_32:
1475                 if (ELF32_R_TYPE (rel->r_info) == r_type)
1476                   tls_type = GOT_TLS_IE_NEG;
1477                 else
1478                   /* If this is a GD->IE transition, we may use either of
1479                      R_386_TLS_TPOFF and R_386_TLS_TPOFF32.  */
1480                   tls_type = GOT_TLS_IE;
1481                 break;
1482               case R_386_TLS_IE:
1483               case R_386_TLS_GOTIE:
1484                 tls_type = GOT_TLS_IE_POS; break;
1485               }
1486
1487             if (h != NULL)
1488               {
1489                 h->got.refcount += 1;
1490                 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1491               }
1492             else
1493               {
1494                 bfd_signed_vma *local_got_refcounts;
1495
1496                 /* This is a global offset table entry for a local symbol.  */
1497                 local_got_refcounts = elf_local_got_refcounts (abfd);
1498                 if (local_got_refcounts == NULL)
1499                   {
1500                     bfd_size_type size;
1501
1502                     size = symtab_hdr->sh_info;
1503                     size *= (sizeof (bfd_signed_vma)
1504                              + sizeof (bfd_vma) + sizeof(char));
1505                     local_got_refcounts = bfd_zalloc (abfd, size);
1506                     if (local_got_refcounts == NULL)
1507                       goto error_return;
1508                     elf_local_got_refcounts (abfd) = local_got_refcounts;
1509                     elf_i386_local_tlsdesc_gotent (abfd)
1510                       = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1511                     elf_i386_local_got_tls_type (abfd)
1512                       = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1513                   }
1514                 local_got_refcounts[r_symndx] += 1;
1515                 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1516               }
1517
1518             if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1519               tls_type |= old_tls_type;
1520             /* If a TLS symbol is accessed using IE at least once,
1521                there is no point to use dynamic model for it.  */
1522             else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1523                      && (! GOT_TLS_GD_ANY_P (old_tls_type)
1524                          || (tls_type & GOT_TLS_IE) == 0))
1525               {
1526                 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1527                   tls_type = old_tls_type;
1528                 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1529                          && GOT_TLS_GD_ANY_P (tls_type))
1530                   tls_type |= old_tls_type;
1531                 else
1532                   {
1533                     (*_bfd_error_handler)
1534                       (_("%B: `%s' accessed both as normal and "
1535                          "thread local symbol"),
1536                        abfd,
1537                        h ? h->root.root.string : "<local>");
1538                     goto error_return;
1539                   }
1540               }
1541
1542             if (old_tls_type != tls_type)
1543               {
1544                 if (h != NULL)
1545                   elf_i386_hash_entry (h)->tls_type = tls_type;
1546                 else
1547                   elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1548               }
1549           }
1550           /* Fall through */
1551
1552         case R_386_GOTOFF:
1553         case R_386_GOTPC:
1554         create_got:
1555           if (htab->elf.sgot == NULL)
1556             {
1557               if (htab->elf.dynobj == NULL)
1558                 htab->elf.dynobj = abfd;
1559               if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
1560                 goto error_return;
1561             }
1562           if (r_type != R_386_TLS_IE)
1563             break;
1564           /* Fall through */
1565
1566         case R_386_TLS_LE_32:
1567         case R_386_TLS_LE:
1568           if (!info->shared)
1569             break;
1570           info->flags |= DF_STATIC_TLS;
1571           /* Fall through */
1572
1573         case R_386_32:
1574         case R_386_PC32:
1575           if (h != NULL && info->executable)
1576             {
1577               /* If this reloc is in a read-only section, we might
1578                  need a copy reloc.  We can't check reliably at this
1579                  stage whether the section is read-only, as input
1580                  sections have not yet been mapped to output sections.
1581                  Tentatively set the flag for now, and correct in
1582                  adjust_dynamic_symbol.  */
1583               h->non_got_ref = 1;
1584
1585               /* We may need a .plt entry if the function this reloc
1586                  refers to is in a shared lib.  */
1587               h->plt.refcount += 1;
1588               if (r_type != R_386_PC32)
1589                 h->pointer_equality_needed = 1;
1590             }
1591
1592           /* If we are creating a shared library, and this is a reloc
1593              against a global symbol, or a non PC relative reloc
1594              against a local symbol, then we need to copy the reloc
1595              into the shared library.  However, if we are linking with
1596              -Bsymbolic, we do not need to copy a reloc against a
1597              global symbol which is defined in an object we are
1598              including in the link (i.e., DEF_REGULAR is set).  At
1599              this point we have not seen all the input files, so it is
1600              possible that DEF_REGULAR is not set now but will be set
1601              later (it is never cleared).  In case of a weak definition,
1602              DEF_REGULAR may be cleared later by a strong definition in
1603              a shared library.  We account for that possibility below by
1604              storing information in the relocs_copied field of the hash
1605              table entry.  A similar situation occurs when creating
1606              shared libraries and symbol visibility changes render the
1607              symbol local.
1608
1609              If on the other hand, we are creating an executable, we
1610              may need to keep relocations for symbols satisfied by a
1611              dynamic library if we manage to avoid copy relocs for the
1612              symbol.  */
1613           if ((info->shared
1614                && (sec->flags & SEC_ALLOC) != 0
1615                && (r_type != R_386_PC32
1616                    || (h != NULL
1617                        && (! SYMBOLIC_BIND (info, h)
1618                            || h->root.type == bfd_link_hash_defweak
1619                            || !h->def_regular))))
1620               || (ELIMINATE_COPY_RELOCS
1621                   && !info->shared
1622                   && (sec->flags & SEC_ALLOC) != 0
1623                   && h != NULL
1624                   && (h->root.type == bfd_link_hash_defweak
1625                       || !h->def_regular)))
1626             {
1627               struct elf_dyn_relocs *p;
1628               struct elf_dyn_relocs **head;
1629
1630               /* We must copy these reloc types into the output file.
1631                  Create a reloc section in dynobj and make room for
1632                  this reloc.  */
1633               if (sreloc == NULL)
1634                 {
1635                   if (htab->elf.dynobj == NULL)
1636                     htab->elf.dynobj = abfd;
1637
1638                   sreloc = _bfd_elf_make_dynamic_reloc_section
1639                     (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1640
1641                   if (sreloc == NULL)
1642                     goto error_return;
1643                 }
1644
1645               /* If this is a global symbol, we count the number of
1646                  relocations we need for this symbol.  */
1647               if (h != NULL)
1648                 {
1649                   head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1650                 }
1651               else
1652                 {
1653                   void **vpp;
1654                   /* Track dynamic relocs needed for local syms too.
1655                      We really need local syms available to do this
1656                      easily.  Oh well.  */
1657
1658                   asection *s;
1659                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1660                                                  sec, r_symndx);
1661                   if (s == NULL)
1662                     goto error_return;
1663
1664                   vpp = &elf_section_data (s)->local_dynrel;
1665                   head = (struct elf_dyn_relocs **)vpp;
1666                 }
1667
1668               p = *head;
1669               if (p == NULL || p->sec != sec)
1670                 {
1671                   bfd_size_type amt = sizeof *p;
1672                   p = bfd_alloc (htab->elf.dynobj, amt);
1673                   if (p == NULL)
1674                     goto error_return;
1675                   p->next = *head;
1676                   *head = p;
1677                   p->sec = sec;
1678                   p->count = 0;
1679                   p->pc_count = 0;
1680                 }
1681
1682               p->count += 1;
1683               if (r_type == R_386_PC32)
1684                 p->pc_count += 1;
1685             }
1686           break;
1687
1688           /* This relocation describes the C++ object vtable hierarchy.
1689              Reconstruct it for later use during GC.  */
1690         case R_386_GNU_VTINHERIT:
1691           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1692             goto error_return;
1693           break;
1694
1695           /* This relocation describes which C++ vtable entries are actually
1696              used.  Record for later use during GC.  */
1697         case R_386_GNU_VTENTRY:
1698           BFD_ASSERT (h != NULL);
1699           if (h != NULL
1700               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1701             goto error_return;
1702           break;
1703
1704         default:
1705           break;
1706         }
1707     }
1708
1709    if (isymbuf != NULL
1710        && (unsigned char *) isymbuf != symtab_hdr->contents)
1711     {
1712       if (!info->keep_memory)
1713         free (isymbuf);
1714       else
1715         {
1716           /* Cache the symbols for elf_link_input_bfd.  */
1717           symtab_hdr->contents = (unsigned char *) isymbuf;
1718         }
1719     }
1720
1721   return TRUE;
1722
1723 error_return:
1724    if (isymbuf != NULL
1725        && (unsigned char *) isymbuf != symtab_hdr->contents)
1726      free (isymbuf);
1727    return FALSE;
1728 }
1729
1730 /* Return the section that should be marked against GC for a given
1731    relocation.  */
1732
1733 static asection *
1734 elf_i386_gc_mark_hook (asection *sec,
1735                        struct bfd_link_info *info,
1736                        Elf_Internal_Rela *rel,
1737                        struct elf_link_hash_entry *h,
1738                        Elf_Internal_Sym *sym)
1739 {
1740   if (h != NULL)
1741     switch (ELF32_R_TYPE (rel->r_info))
1742       {
1743       case R_386_GNU_VTINHERIT:
1744       case R_386_GNU_VTENTRY:
1745         return NULL;
1746       }
1747
1748   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1749 }
1750
1751 /* Update the got entry reference counts for the section being removed.  */
1752
1753 static bfd_boolean
1754 elf_i386_gc_sweep_hook (bfd *abfd,
1755                         struct bfd_link_info *info,
1756                         asection *sec,
1757                         const Elf_Internal_Rela *relocs)
1758 {
1759   Elf_Internal_Shdr *symtab_hdr;
1760   struct elf_link_hash_entry **sym_hashes;
1761   bfd_signed_vma *local_got_refcounts;
1762   const Elf_Internal_Rela *rel, *relend;
1763
1764   if (info->relocatable)
1765     return TRUE;
1766
1767   elf_section_data (sec)->local_dynrel = NULL;
1768
1769   symtab_hdr = &elf_symtab_hdr (abfd);
1770   sym_hashes = elf_sym_hashes (abfd);
1771   local_got_refcounts = elf_local_got_refcounts (abfd);
1772
1773   relend = relocs + sec->reloc_count;
1774   for (rel = relocs; rel < relend; rel++)
1775     {
1776       unsigned long r_symndx;
1777       unsigned int r_type;
1778       struct elf_link_hash_entry *h = NULL;
1779
1780       r_symndx = ELF32_R_SYM (rel->r_info);
1781       if (r_symndx >= symtab_hdr->sh_info)
1782         {
1783           struct elf_i386_link_hash_entry *eh;
1784           struct elf_dyn_relocs **pp;
1785           struct elf_dyn_relocs *p;
1786
1787           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1788           while (h->root.type == bfd_link_hash_indirect
1789                  || h->root.type == bfd_link_hash_warning)
1790             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1791           eh = (struct elf_i386_link_hash_entry *) h;
1792
1793           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1794             if (p->sec == sec)
1795               {
1796                 /* Everything must go for SEC.  */
1797                 *pp = p->next;
1798                 break;
1799               }
1800         }
1801
1802       r_type = ELF32_R_TYPE (rel->r_info);
1803       if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1804                                      symtab_hdr, sym_hashes,
1805                                      &r_type, GOT_UNKNOWN,
1806                                      rel, relend, h)) 
1807         return FALSE;
1808
1809       switch (r_type)
1810         {
1811         case R_386_TLS_LDM:
1812           if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1813             elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1814           break;
1815
1816         case R_386_TLS_GD:
1817         case R_386_TLS_GOTDESC:
1818         case R_386_TLS_DESC_CALL:
1819         case R_386_TLS_IE_32:
1820         case R_386_TLS_IE:
1821         case R_386_TLS_GOTIE:
1822         case R_386_GOT32:
1823           if (h != NULL)
1824             {
1825               if (h->got.refcount > 0)
1826                 h->got.refcount -= 1;
1827             }
1828           else if (local_got_refcounts != NULL)
1829             {
1830               if (local_got_refcounts[r_symndx] > 0)
1831                 local_got_refcounts[r_symndx] -= 1;
1832             }
1833           break;
1834
1835         case R_386_32:
1836         case R_386_PC32:
1837           if (info->shared)
1838             break;
1839           /* Fall through */
1840
1841         case R_386_PLT32:
1842           if (h != NULL)
1843             {
1844               if (h->plt.refcount > 0)
1845                 h->plt.refcount -= 1;
1846             }
1847           break;
1848
1849         default:
1850           break;
1851         }
1852     }
1853
1854   return TRUE;
1855 }
1856
1857 /* Adjust a symbol defined by a dynamic object and referenced by a
1858    regular object.  The current definition is in some section of the
1859    dynamic object, but we're not including those sections.  We have to
1860    change the definition to something the rest of the link can
1861    understand.  */
1862
1863 static bfd_boolean
1864 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1865                                 struct elf_link_hash_entry *h)
1866 {
1867   struct elf_i386_link_hash_table *htab;
1868   asection *s;
1869
1870   /* STT_GNU_IFUNC symbol must go through PLT. */
1871   if (h->type == STT_GNU_IFUNC)
1872     {
1873       if (h->plt.refcount <= 0)
1874         {
1875           h->plt.offset = (bfd_vma) -1;
1876           h->needs_plt = 0;
1877         }
1878       return TRUE;
1879     }
1880
1881   /* If this is a function, put it in the procedure linkage table.  We
1882      will fill in the contents of the procedure linkage table later,
1883      when we know the address of the .got section.  */
1884   if (h->type == STT_FUNC
1885       || h->needs_plt)
1886     {
1887       if (h->plt.refcount <= 0
1888           || SYMBOL_CALLS_LOCAL (info, h)
1889           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1890               && h->root.type == bfd_link_hash_undefweak))
1891         {
1892           /* This case can occur if we saw a PLT32 reloc in an input
1893              file, but the symbol was never referred to by a dynamic
1894              object, or if all references were garbage collected.  In
1895              such a case, we don't actually need to build a procedure
1896              linkage table, and we can just do a PC32 reloc instead.  */
1897           h->plt.offset = (bfd_vma) -1;
1898           h->needs_plt = 0;
1899         }
1900
1901       return TRUE;
1902     }
1903   else
1904     /* It's possible that we incorrectly decided a .plt reloc was
1905        needed for an R_386_PC32 reloc to a non-function sym in
1906        check_relocs.  We can't decide accurately between function and
1907        non-function syms in check-relocs;  Objects loaded later in
1908        the link may change h->type.  So fix it now.  */
1909     h->plt.offset = (bfd_vma) -1;
1910
1911   /* If this is a weak symbol, and there is a real definition, the
1912      processor independent code will have arranged for us to see the
1913      real definition first, and we can just use the same value.  */
1914   if (h->u.weakdef != NULL)
1915     {
1916       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1917                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
1918       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1919       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1920       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1921         h->non_got_ref = h->u.weakdef->non_got_ref;
1922       return TRUE;
1923     }
1924
1925   /* This is a reference to a symbol defined by a dynamic object which
1926      is not a function.  */
1927
1928   /* If we are creating a shared library, we must presume that the
1929      only references to the symbol are via the global offset table.
1930      For such cases we need not do anything here; the relocations will
1931      be handled correctly by relocate_section.  */
1932   if (info->shared)
1933     return TRUE;
1934
1935   /* If there are no references to this symbol that do not use the
1936      GOT, we don't need to generate a copy reloc.  */
1937   if (!h->non_got_ref)
1938     return TRUE;
1939
1940   /* If -z nocopyreloc was given, we won't generate them either.  */
1941   if (info->nocopyreloc)
1942     {
1943       h->non_got_ref = 0;
1944       return TRUE;
1945     }
1946
1947   htab = elf_i386_hash_table (info);
1948
1949   /* If there aren't any dynamic relocs in read-only sections, then
1950      we can keep the dynamic relocs and avoid the copy reloc.  This
1951      doesn't work on VxWorks, where we can not have dynamic relocations
1952      (other than copy and jump slot relocations) in an executable.  */
1953   if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
1954     {
1955       struct elf_i386_link_hash_entry * eh;
1956       struct elf_dyn_relocs *p;
1957
1958       eh = (struct elf_i386_link_hash_entry *) h;
1959       for (p = eh->dyn_relocs; p != NULL; p = p->next)
1960         {
1961           s = p->sec->output_section;
1962           if (s != NULL && (s->flags & SEC_READONLY) != 0)
1963             break;
1964         }
1965
1966       if (p == NULL)
1967         {
1968           h->non_got_ref = 0;
1969           return TRUE;
1970         }
1971     }
1972
1973   if (h->size == 0)
1974     {
1975       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1976                              h->root.root.string);
1977       return TRUE;
1978     }
1979
1980   /* We must allocate the symbol in our .dynbss section, which will
1981      become part of the .bss section of the executable.  There will be
1982      an entry for this symbol in the .dynsym section.  The dynamic
1983      object will contain position independent code, so all references
1984      from the dynamic object to this symbol will go through the global
1985      offset table.  The dynamic linker will use the .dynsym entry to
1986      determine the address it must put in the global offset table, so
1987      both the dynamic object and the regular object will refer to the
1988      same memory location for the variable.  */
1989
1990   /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1991      copy the initial value out of the dynamic object and into the
1992      runtime process image.  */
1993   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1994     {
1995       htab->srelbss->size += sizeof (Elf32_External_Rel);
1996       h->needs_copy = 1;
1997     }
1998
1999   s = htab->sdynbss;
2000
2001   return _bfd_elf_adjust_dynamic_copy (h, s);
2002 }
2003
2004 /* Allocate space in .plt, .got and associated reloc sections for
2005    dynamic relocs.  */
2006
2007 static bfd_boolean
2008 elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2009 {
2010   struct bfd_link_info *info;
2011   struct elf_i386_link_hash_table *htab;
2012   struct elf_i386_link_hash_entry *eh;
2013   struct elf_dyn_relocs *p;
2014
2015   if (h->root.type == bfd_link_hash_indirect)
2016     return TRUE;
2017
2018   if (h->root.type == bfd_link_hash_warning)
2019     /* When warning symbols are created, they **replace** the "real"
2020        entry in the hash table, thus we never get to see the real
2021        symbol in a hash traversal.  So look at it now.  */
2022     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2023   eh = (struct elf_i386_link_hash_entry *) h;
2024
2025   info = (struct bfd_link_info *) inf;
2026   htab = elf_i386_hash_table (info);
2027
2028   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2029      here if it is defined and referenced in a non-shared object.  */
2030   if (h->type == STT_GNU_IFUNC
2031       && h->def_regular)
2032     return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2033                                                &eh->dyn_relocs,
2034                                                PLT_ENTRY_SIZE, 4);
2035   else if (htab->elf.dynamic_sections_created
2036            && h->plt.refcount > 0)
2037     {
2038       /* Make sure this symbol is output as a dynamic symbol.
2039          Undefined weak syms won't yet be marked as dynamic.  */
2040       if (h->dynindx == -1
2041           && !h->forced_local)
2042         {
2043           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2044             return FALSE;
2045         }
2046
2047       if (info->shared
2048           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2049         {
2050           asection *s = htab->elf.splt;
2051
2052           /* If this is the first .plt entry, make room for the special
2053              first entry.  */
2054           if (s->size == 0)
2055             s->size += PLT_ENTRY_SIZE;
2056
2057           h->plt.offset = s->size;
2058
2059           /* If this symbol is not defined in a regular file, and we are
2060              not generating a shared library, then set the symbol to this
2061              location in the .plt.  This is required to make function
2062              pointers compare as equal between the normal executable and
2063              the shared library.  */
2064           if (! info->shared
2065               && !h->def_regular)
2066             {
2067               h->root.u.def.section = s;
2068               h->root.u.def.value = h->plt.offset;
2069             }
2070
2071           /* Make room for this entry.  */
2072           s->size += PLT_ENTRY_SIZE;
2073
2074           /* We also need to make an entry in the .got.plt section, which
2075              will be placed in the .got section by the linker script.  */
2076           htab->elf.sgotplt->size += 4;
2077
2078           /* We also need to make an entry in the .rel.plt section.  */
2079           htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2080           htab->next_tls_desc_index++;
2081
2082           if (htab->is_vxworks && !info->shared)
2083             {
2084               /* VxWorks has a second set of relocations for each PLT entry
2085                  in executables.  They go in a separate relocation section,
2086                  which is processed by the kernel loader.  */
2087
2088               /* There are two relocations for the initial PLT entry: an
2089                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
2090                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
2091
2092               if (h->plt.offset == PLT_ENTRY_SIZE)
2093                 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2094
2095               /* There are two extra relocations for each subsequent PLT entry:
2096                  an R_386_32 relocation for the GOT entry, and an R_386_32
2097                  relocation for the PLT entry.  */
2098
2099               htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2100             }
2101         }
2102       else
2103         {
2104           h->plt.offset = (bfd_vma) -1;
2105           h->needs_plt = 0;
2106         }
2107     }
2108   else
2109     {
2110       h->plt.offset = (bfd_vma) -1;
2111       h->needs_plt = 0;
2112     }
2113
2114   eh->tlsdesc_got = (bfd_vma) -1;
2115
2116   /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
2117      make it a R_386_TLS_LE_32 requiring no TLS entry.  */
2118   if (h->got.refcount > 0
2119       && !info->shared
2120       && h->dynindx == -1
2121       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
2122     h->got.offset = (bfd_vma) -1;
2123   else if (h->got.refcount > 0)
2124     {
2125       asection *s;
2126       bfd_boolean dyn;
2127       int tls_type = elf_i386_hash_entry(h)->tls_type;
2128
2129       /* Make sure this symbol is output as a dynamic symbol.
2130          Undefined weak syms won't yet be marked as dynamic.  */
2131       if (h->dynindx == -1
2132           && !h->forced_local)
2133         {
2134           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2135             return FALSE;
2136         }
2137
2138       s = htab->elf.sgot;
2139       if (GOT_TLS_GDESC_P (tls_type))
2140         {
2141           eh->tlsdesc_got = htab->elf.sgotplt->size
2142             - elf_i386_compute_jump_table_size (htab);
2143           htab->elf.sgotplt->size += 8;
2144           h->got.offset = (bfd_vma) -2;
2145         }
2146       if (! GOT_TLS_GDESC_P (tls_type)
2147           || GOT_TLS_GD_P (tls_type))
2148         {
2149           h->got.offset = s->size;
2150           s->size += 4;
2151           /* R_386_TLS_GD needs 2 consecutive GOT slots.  */
2152           if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
2153             s->size += 4;
2154         }
2155       dyn = htab->elf.dynamic_sections_created;
2156       /* R_386_TLS_IE_32 needs one dynamic relocation,
2157          R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
2158          (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
2159          need two), R_386_TLS_GD needs one if local symbol and two if
2160          global.  */
2161       if (tls_type == GOT_TLS_IE_BOTH)
2162         htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2163       else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2164                || (tls_type & GOT_TLS_IE))
2165         htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2166       else if (GOT_TLS_GD_P (tls_type))
2167         htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2168       else if (! GOT_TLS_GDESC_P (tls_type)
2169                && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2170                    || h->root.type != bfd_link_hash_undefweak)
2171                && (info->shared
2172                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2173         htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2174       if (GOT_TLS_GDESC_P (tls_type))
2175         htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2176     }
2177   else
2178     h->got.offset = (bfd_vma) -1;
2179
2180   if (eh->dyn_relocs == NULL)
2181     return TRUE;
2182
2183   /* In the shared -Bsymbolic case, discard space allocated for
2184      dynamic pc-relative relocs against symbols which turn out to be
2185      defined in regular objects.  For the normal shared case, discard
2186      space for pc-relative relocs that have become local due to symbol
2187      visibility changes.  */
2188
2189   if (info->shared)
2190     {
2191       /* The only reloc that uses pc_count is R_386_PC32, which will
2192          appear on a call or on something like ".long foo - .".  We
2193          want calls to protected symbols to resolve directly to the
2194          function rather than going via the plt.  If people want
2195          function pointer comparisons to work as expected then they
2196          should avoid writing assembly like ".long foo - .".  */
2197       if (SYMBOL_CALLS_LOCAL (info, h))
2198         {
2199           struct elf_dyn_relocs **pp;
2200
2201           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2202             {
2203               p->count -= p->pc_count;
2204               p->pc_count = 0;
2205               if (p->count == 0)
2206                 *pp = p->next;
2207               else
2208                 pp = &p->next;
2209             }
2210         }
2211
2212       if (htab->is_vxworks)
2213         {
2214           struct elf_dyn_relocs **pp;
2215           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2216             {
2217               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2218                 *pp = p->next;
2219               else
2220                 pp = &p->next;
2221             }
2222         }
2223
2224       /* Also discard relocs on undefined weak syms with non-default
2225          visibility.  */
2226       if (eh->dyn_relocs != NULL
2227           && h->root.type == bfd_link_hash_undefweak)
2228         {
2229           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2230             eh->dyn_relocs = NULL;
2231
2232           /* Make sure undefined weak symbols are output as a dynamic
2233              symbol in PIEs.  */
2234           else if (h->dynindx == -1
2235                    && !h->forced_local)
2236             {
2237               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2238                 return FALSE;
2239             }
2240         }
2241     }
2242   else if (ELIMINATE_COPY_RELOCS)
2243     {
2244       /* For the non-shared case, discard space for relocs against
2245          symbols which turn out to need copy relocs or are not
2246          dynamic.  */
2247
2248       if (!h->non_got_ref
2249           && ((h->def_dynamic
2250                && !h->def_regular)
2251               || (htab->elf.dynamic_sections_created
2252                   && (h->root.type == bfd_link_hash_undefweak
2253                       || h->root.type == bfd_link_hash_undefined))))
2254         {
2255           /* Make sure this symbol is output as a dynamic symbol.
2256              Undefined weak syms won't yet be marked as dynamic.  */
2257           if (h->dynindx == -1
2258               && !h->forced_local)
2259             {
2260               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2261                 return FALSE;
2262             }
2263
2264           /* If that succeeded, we know we'll be keeping all the
2265              relocs.  */
2266           if (h->dynindx != -1)
2267             goto keep;
2268         }
2269
2270       eh->dyn_relocs = NULL;
2271
2272     keep: ;
2273     }
2274
2275   /* Finally, allocate space.  */
2276   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2277     {
2278       asection *sreloc;
2279
2280       sreloc = elf_section_data (p->sec)->sreloc;
2281
2282       BFD_ASSERT (sreloc != NULL);
2283       sreloc->size += p->count * sizeof (Elf32_External_Rel);
2284     }
2285
2286   return TRUE;
2287 }
2288
2289 /* Allocate space in .plt, .got and associated reloc sections for
2290    local dynamic relocs.  */
2291
2292 static bfd_boolean
2293 elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
2294 {
2295   struct elf_link_hash_entry *h
2296     = (struct elf_link_hash_entry *) *slot;
2297
2298   if (h->type != STT_GNU_IFUNC
2299       || !h->def_regular
2300       || !h->ref_regular
2301       || !h->forced_local
2302       || h->root.type != bfd_link_hash_defined)
2303     abort ();
2304
2305   return elf_i386_allocate_dynrelocs (h, inf);
2306 }
2307
2308 /* Find any dynamic relocs that apply to read-only sections.  */
2309
2310 static bfd_boolean
2311 elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2312 {
2313   struct elf_i386_link_hash_entry *eh;
2314   struct elf_dyn_relocs *p;
2315
2316   if (h->root.type == bfd_link_hash_warning)
2317     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2318
2319   eh = (struct elf_i386_link_hash_entry *) h;
2320   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2321     {
2322       asection *s = p->sec->output_section;
2323
2324       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2325         {
2326           struct bfd_link_info *info = (struct bfd_link_info *) inf;
2327
2328           info->flags |= DF_TEXTREL;
2329
2330           /* Not an error, just cut short the traversal.  */
2331           return FALSE;
2332         }
2333     }
2334   return TRUE;
2335 }
2336
2337 /* Set the sizes of the dynamic sections.  */
2338
2339 static bfd_boolean
2340 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2341                                 struct bfd_link_info *info)
2342 {
2343   struct elf_i386_link_hash_table *htab;
2344   bfd *dynobj;
2345   asection *s;
2346   bfd_boolean relocs;
2347   bfd *ibfd;
2348
2349   htab = elf_i386_hash_table (info);
2350   dynobj = htab->elf.dynobj;
2351   if (dynobj == NULL)
2352     abort ();
2353
2354   if (htab->elf.dynamic_sections_created)
2355     {
2356       /* Set the contents of the .interp section to the interpreter.  */
2357       if (info->executable)
2358         {
2359           s = bfd_get_section_by_name (dynobj, ".interp");
2360           if (s == NULL)
2361             abort ();
2362           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2363           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2364         }
2365     }
2366
2367   /* Set up .got offsets for local syms, and space for local dynamic
2368      relocs.  */
2369   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2370     {
2371       bfd_signed_vma *local_got;
2372       bfd_signed_vma *end_local_got;
2373       char *local_tls_type;
2374       bfd_vma *local_tlsdesc_gotent;
2375       bfd_size_type locsymcount;
2376       Elf_Internal_Shdr *symtab_hdr;
2377       asection *srel;
2378
2379       if (! is_i386_elf (ibfd))
2380         continue;
2381
2382       for (s = ibfd->sections; s != NULL; s = s->next)
2383         {
2384           struct elf_dyn_relocs *p;
2385
2386           for (p = ((struct elf_dyn_relocs *)
2387                      elf_section_data (s)->local_dynrel);
2388                p != NULL;
2389                p = p->next)
2390             {
2391               if (!bfd_is_abs_section (p->sec)
2392                   && bfd_is_abs_section (p->sec->output_section))
2393                 {
2394                   /* Input section has been discarded, either because
2395                      it is a copy of a linkonce section or due to
2396                      linker script /DISCARD/, so we'll be discarding
2397                      the relocs too.  */
2398                 }
2399               else if (htab->is_vxworks
2400                        && strcmp (p->sec->output_section->name,
2401                                   ".tls_vars") == 0)
2402                 {
2403                   /* Relocations in vxworks .tls_vars sections are
2404                      handled specially by the loader.  */
2405                 }
2406               else if (p->count != 0)
2407                 {
2408                   srel = elf_section_data (p->sec)->sreloc;
2409                   srel->size += p->count * sizeof (Elf32_External_Rel);
2410                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2411                     info->flags |= DF_TEXTREL;
2412                 }
2413             }
2414         }
2415
2416       local_got = elf_local_got_refcounts (ibfd);
2417       if (!local_got)
2418         continue;
2419
2420       symtab_hdr = &elf_symtab_hdr (ibfd);
2421       locsymcount = symtab_hdr->sh_info;
2422       end_local_got = local_got + locsymcount;
2423       local_tls_type = elf_i386_local_got_tls_type (ibfd);
2424       local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
2425       s = htab->elf.sgot;
2426       srel = htab->elf.srelgot;
2427       for (; local_got < end_local_got;
2428            ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2429         {
2430           *local_tlsdesc_gotent = (bfd_vma) -1;
2431           if (*local_got > 0)
2432             {
2433               if (GOT_TLS_GDESC_P (*local_tls_type))
2434                 {
2435                   *local_tlsdesc_gotent = htab->elf.sgotplt->size
2436                     - elf_i386_compute_jump_table_size (htab);
2437                   htab->elf.sgotplt->size += 8;
2438                   *local_got = (bfd_vma) -2;
2439                 }
2440               if (! GOT_TLS_GDESC_P (*local_tls_type)
2441                   || GOT_TLS_GD_P (*local_tls_type))
2442                 {
2443                   *local_got = s->size;
2444                   s->size += 4;
2445                   if (GOT_TLS_GD_P (*local_tls_type)
2446                       || *local_tls_type == GOT_TLS_IE_BOTH)
2447                     s->size += 4;
2448                 }
2449               if (info->shared
2450                   || GOT_TLS_GD_ANY_P (*local_tls_type)
2451                   || (*local_tls_type & GOT_TLS_IE))
2452                 {
2453                   if (*local_tls_type == GOT_TLS_IE_BOTH)
2454                     srel->size += 2 * sizeof (Elf32_External_Rel);
2455                   else if (GOT_TLS_GD_P (*local_tls_type)
2456                            || ! GOT_TLS_GDESC_P (*local_tls_type))
2457                     srel->size += sizeof (Elf32_External_Rel);
2458                   if (GOT_TLS_GDESC_P (*local_tls_type))
2459                     htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2460                 }
2461             }
2462           else
2463             *local_got = (bfd_vma) -1;
2464         }
2465     }
2466
2467   if (htab->tls_ldm_got.refcount > 0)
2468     {
2469       /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2470          relocs.  */
2471       htab->tls_ldm_got.offset = htab->elf.sgot->size;
2472       htab->elf.sgot->size += 8;
2473       htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2474     }
2475   else
2476     htab->tls_ldm_got.offset = -1;
2477
2478   /* Allocate global sym .plt and .got entries, and space for global
2479      sym dynamic relocs.  */
2480   elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
2481
2482   /* Allocate .plt and .got entries, and space for local symbols.  */
2483   htab_traverse (htab->loc_hash_table,
2484                  elf_i386_allocate_local_dynrelocs,
2485                  info);
2486
2487   /* For every jump slot reserved in the sgotplt, reloc_count is
2488      incremented.  However, when we reserve space for TLS descriptors,
2489      it's not incremented, so in order to compute the space reserved
2490      for them, it suffices to multiply the reloc count by the jump
2491      slot size.  */
2492   if (htab->elf.srelplt)
2493     htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
2494
2495   /* We now have determined the sizes of the various dynamic sections.
2496      Allocate memory for them.  */
2497   relocs = FALSE;
2498   for (s = dynobj->sections; s != NULL; s = s->next)
2499     {
2500       bfd_boolean strip_section = TRUE;
2501
2502       if ((s->flags & SEC_LINKER_CREATED) == 0)
2503         continue;
2504
2505       if (s == htab->elf.splt
2506           || s == htab->elf.sgot
2507           || s == htab->elf.sgotplt
2508           || s == htab->elf.iplt
2509           || s == htab->elf.igotplt
2510           || s == htab->sdynbss)
2511         {
2512           /* Strip this section if we don't need it; see the
2513              comment below.  */
2514           /* We'd like to strip these sections if they aren't needed, but if
2515              we've exported dynamic symbols from them we must leave them.
2516              It's too late to tell BFD to get rid of the symbols.  */
2517
2518           if (htab->elf.hplt != NULL)
2519             strip_section = FALSE;
2520         }
2521       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
2522         {
2523           if (s->size != 0
2524               && s != htab->elf.srelplt
2525               && s != htab->srelplt2)
2526             relocs = TRUE;
2527
2528           /* We use the reloc_count field as a counter if we need
2529              to copy relocs into the output file.  */
2530           s->reloc_count = 0;
2531         }
2532       else
2533         {
2534           /* It's not one of our sections, so don't allocate space.  */
2535           continue;
2536         }
2537
2538       if (s->size == 0)
2539         {
2540           /* If we don't need this section, strip it from the
2541              output file.  This is mostly to handle .rel.bss and
2542              .rel.plt.  We must create both sections in
2543              create_dynamic_sections, because they must be created
2544              before the linker maps input sections to output
2545              sections.  The linker does that before
2546              adjust_dynamic_symbol is called, and it is that
2547              function which decides whether anything needs to go
2548              into these sections.  */
2549           if (strip_section)
2550             s->flags |= SEC_EXCLUDE;
2551           continue;
2552         }
2553
2554       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2555         continue;
2556
2557       /* Allocate memory for the section contents.  We use bfd_zalloc
2558          here in case unused entries are not reclaimed before the
2559          section's contents are written out.  This should not happen,
2560          but this way if it does, we get a R_386_NONE reloc instead
2561          of garbage.  */
2562       s->contents = bfd_zalloc (dynobj, s->size);
2563       if (s->contents == NULL)
2564         return FALSE;
2565     }
2566
2567   if (htab->elf.dynamic_sections_created)
2568     {
2569       /* Add some entries to the .dynamic section.  We fill in the
2570          values later, in elf_i386_finish_dynamic_sections, but we
2571          must add the entries now so that we get the correct size for
2572          the .dynamic section.  The DT_DEBUG entry is filled in by the
2573          dynamic linker and used by the debugger.  */
2574 #define add_dynamic_entry(TAG, VAL) \
2575   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2576
2577       if (info->executable)
2578         {
2579           if (!add_dynamic_entry (DT_DEBUG, 0))
2580             return FALSE;
2581         }
2582
2583       if (htab->elf.splt->size != 0)
2584         {
2585           if (!add_dynamic_entry (DT_PLTGOT, 0)
2586               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2587               || !add_dynamic_entry (DT_PLTREL, DT_REL)
2588               || !add_dynamic_entry (DT_JMPREL, 0))
2589             return FALSE;
2590         }
2591
2592       if (relocs)
2593         {
2594           if (!add_dynamic_entry (DT_REL, 0)
2595               || !add_dynamic_entry (DT_RELSZ, 0)
2596               || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2597             return FALSE;
2598
2599           /* If any dynamic relocs apply to a read-only section,
2600              then we need a DT_TEXTREL entry.  */
2601           if ((info->flags & DF_TEXTREL) == 0)
2602             elf_link_hash_traverse (&htab->elf,
2603                                     elf_i386_readonly_dynrelocs, info);
2604
2605           if ((info->flags & DF_TEXTREL) != 0)
2606             {
2607               if (!add_dynamic_entry (DT_TEXTREL, 0))
2608                 return FALSE;
2609             }
2610         }
2611       if (htab->is_vxworks
2612           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
2613         return FALSE;
2614     }
2615 #undef add_dynamic_entry
2616
2617   return TRUE;
2618 }
2619
2620 static bfd_boolean
2621 elf_i386_always_size_sections (bfd *output_bfd,
2622                                struct bfd_link_info *info)
2623 {
2624   asection *tls_sec = elf_hash_table (info)->tls_sec;
2625
2626   if (tls_sec)
2627     {
2628       struct elf_link_hash_entry *tlsbase;
2629
2630       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2631                                       "_TLS_MODULE_BASE_",
2632                                       FALSE, FALSE, FALSE);
2633
2634       if (tlsbase && tlsbase->type == STT_TLS)
2635         {
2636           struct bfd_link_hash_entry *bh = NULL;
2637           const struct elf_backend_data *bed
2638             = get_elf_backend_data (output_bfd);
2639
2640           if (!(_bfd_generic_link_add_one_symbol
2641                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2642                  tls_sec, 0, NULL, FALSE,
2643                  bed->collect, &bh)))
2644             return FALSE;
2645
2646           elf_i386_hash_table (info)->tls_module_base = bh;
2647
2648           tlsbase = (struct elf_link_hash_entry *)bh;
2649           tlsbase->def_regular = 1;
2650           tlsbase->other = STV_HIDDEN;
2651           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2652         }
2653     }
2654
2655   return TRUE;
2656 }
2657
2658 /* Set the correct type for an x86 ELF section.  We do this by the
2659    section name, which is a hack, but ought to work.  */
2660
2661 static bfd_boolean
2662 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2663                         Elf_Internal_Shdr *hdr,
2664                         asection *sec)
2665 {
2666   register const char *name;
2667
2668   name = bfd_get_section_name (abfd, sec);
2669
2670   /* This is an ugly, but unfortunately necessary hack that is
2671      needed when producing EFI binaries on x86. It tells
2672      elf.c:elf_fake_sections() not to consider ".reloc" as a section
2673      containing ELF relocation info.  We need this hack in order to
2674      be able to generate ELF binaries that can be translated into
2675      EFI applications (which are essentially COFF objects).  Those
2676      files contain a COFF ".reloc" section inside an ELFNN object,
2677      which would normally cause BFD to segfault because it would
2678      attempt to interpret this section as containing relocation
2679      entries for section "oc".  With this hack enabled, ".reloc"
2680      will be treated as a normal data section, which will avoid the
2681      segfault.  However, you won't be able to create an ELFNN binary
2682      with a section named "oc" that needs relocations, but that's
2683      the kind of ugly side-effects you get when detecting section
2684      types based on their names...  In practice, this limitation is
2685      unlikely to bite.  */
2686   if (strcmp (name, ".reloc") == 0)
2687     hdr->sh_type = SHT_PROGBITS;
2688
2689   return TRUE;
2690 }
2691
2692 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2693    executables.  Rather than setting it to the beginning of the TLS
2694    section, we have to set it to the end.    This function may be called
2695    multiple times, it is idempotent.  */
2696
2697 static void
2698 elf_i386_set_tls_module_base (struct bfd_link_info *info)
2699 {
2700   struct bfd_link_hash_entry *base;
2701
2702   if (!info->executable)
2703     return;
2704
2705   base = elf_i386_hash_table (info)->tls_module_base;
2706
2707   if (!base)
2708     return;
2709
2710   base->u.def.value = elf_hash_table (info)->tls_size;
2711 }
2712
2713 /* Return the base VMA address which should be subtracted from real addresses
2714    when resolving @dtpoff relocation.
2715    This is PT_TLS segment p_vaddr.  */
2716
2717 static bfd_vma
2718 elf_i386_dtpoff_base (struct bfd_link_info *info)
2719 {
2720   /* If tls_sec is NULL, we should have signalled an error already.  */
2721   if (elf_hash_table (info)->tls_sec == NULL)
2722     return 0;
2723   return elf_hash_table (info)->tls_sec->vma;
2724 }
2725
2726 /* Return the relocation value for @tpoff relocation
2727    if STT_TLS virtual address is ADDRESS.  */
2728
2729 static bfd_vma
2730 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
2731 {
2732   struct elf_link_hash_table *htab = elf_hash_table (info);
2733
2734   /* If tls_sec is NULL, we should have signalled an error already.  */
2735   if (htab->tls_sec == NULL)
2736     return 0;
2737   return htab->tls_size + htab->tls_sec->vma - address;
2738 }
2739
2740 /* Relocate an i386 ELF section.  */
2741
2742 static bfd_boolean
2743 elf_i386_relocate_section (bfd *output_bfd,
2744                            struct bfd_link_info *info,
2745                            bfd *input_bfd,
2746                            asection *input_section,
2747                            bfd_byte *contents,
2748                            Elf_Internal_Rela *relocs,
2749                            Elf_Internal_Sym *local_syms,
2750                            asection **local_sections)
2751 {
2752   struct elf_i386_link_hash_table *htab;
2753   Elf_Internal_Shdr *symtab_hdr;
2754   struct elf_link_hash_entry **sym_hashes;
2755   bfd_vma *local_got_offsets;
2756   bfd_vma *local_tlsdesc_gotents;
2757   Elf_Internal_Rela *rel;
2758   Elf_Internal_Rela *relend;
2759   bfd_boolean is_vxworks_tls;
2760
2761   BFD_ASSERT (is_i386_elf (input_bfd));
2762   
2763   htab = elf_i386_hash_table (info);
2764   symtab_hdr = &elf_symtab_hdr (input_bfd);
2765   sym_hashes = elf_sym_hashes (input_bfd);
2766   local_got_offsets = elf_local_got_offsets (input_bfd);
2767   local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
2768   /* We have to handle relocations in vxworks .tls_vars sections
2769      specially, because the dynamic loader is 'weird'.  */
2770   is_vxworks_tls = (htab->is_vxworks && info->shared
2771                     && !strcmp (input_section->output_section->name,
2772                                 ".tls_vars"));
2773
2774   elf_i386_set_tls_module_base (info);
2775
2776   rel = relocs;
2777   relend = relocs + input_section->reloc_count;
2778   for (; rel < relend; rel++)
2779     {
2780       unsigned int r_type;
2781       reloc_howto_type *howto;
2782       unsigned long r_symndx;
2783       struct elf_link_hash_entry *h;
2784       Elf_Internal_Sym *sym;
2785       asection *sec;
2786       bfd_vma off, offplt;
2787       bfd_vma relocation;
2788       bfd_boolean unresolved_reloc;
2789       bfd_reloc_status_type r;
2790       unsigned int indx;
2791       int tls_type;
2792
2793       r_type = ELF32_R_TYPE (rel->r_info);
2794       if (r_type == R_386_GNU_VTINHERIT
2795           || r_type == R_386_GNU_VTENTRY)
2796         continue;
2797
2798       if ((indx = r_type) >= R_386_standard
2799           && ((indx = r_type - R_386_ext_offset) - R_386_standard
2800               >= R_386_ext - R_386_standard)
2801           && ((indx = r_type - R_386_tls_offset) - R_386_ext
2802               >= R_386_irelative - R_386_ext))
2803         {
2804           (*_bfd_error_handler)
2805             (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2806              input_bfd, input_section, r_type);
2807           bfd_set_error (bfd_error_bad_value);
2808           return FALSE;
2809         }
2810       howto = elf_howto_table + indx;
2811
2812       r_symndx = ELF32_R_SYM (rel->r_info);
2813       h = NULL;
2814       sym = NULL;
2815       sec = NULL;
2816       unresolved_reloc = FALSE;
2817       if (r_symndx < symtab_hdr->sh_info)
2818         {
2819           sym = local_syms + r_symndx;
2820           sec = local_sections[r_symndx];
2821           relocation = (sec->output_section->vma
2822                         + sec->output_offset
2823                         + sym->st_value);
2824
2825           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2826               && ((sec->flags & SEC_MERGE) != 0
2827                   || (info->relocatable
2828                       && sec->output_offset != 0)))
2829             {
2830               bfd_vma addend;
2831               bfd_byte *where = contents + rel->r_offset;
2832
2833               switch (howto->size)
2834                 {
2835                 case 0:
2836                   addend = bfd_get_8 (input_bfd, where);
2837                   if (howto->pc_relative)
2838                     {
2839                       addend = (addend ^ 0x80) - 0x80;
2840                       addend += 1;
2841                     }
2842                   break;
2843                 case 1:
2844                   addend = bfd_get_16 (input_bfd, where);
2845                   if (howto->pc_relative)
2846                     {
2847                       addend = (addend ^ 0x8000) - 0x8000;
2848                       addend += 2;
2849                     }
2850                   break;
2851                 case 2:
2852                   addend = bfd_get_32 (input_bfd, where);
2853                   if (howto->pc_relative)
2854                     {
2855                       addend = (addend ^ 0x80000000) - 0x80000000;
2856                       addend += 4;
2857                     }
2858                   break;
2859                 default:
2860                   abort ();
2861                 }
2862
2863               if (info->relocatable)
2864                 addend += sec->output_offset;
2865               else
2866                 {
2867                   asection *msec = sec;
2868                   addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2869                                                    addend);
2870                   addend -= relocation;
2871                   addend += msec->output_section->vma + msec->output_offset;
2872                 }
2873
2874               switch (howto->size)
2875                 {
2876                 case 0:
2877                   /* FIXME: overflow checks.  */
2878                   if (howto->pc_relative)
2879                     addend -= 1;
2880                   bfd_put_8 (input_bfd, addend, where);
2881                   break;
2882                 case 1:
2883                   if (howto->pc_relative)
2884                     addend -= 2;
2885                   bfd_put_16 (input_bfd, addend, where);
2886                   break;
2887                 case 2:
2888                   if (howto->pc_relative)
2889                     addend -= 4;
2890                   bfd_put_32 (input_bfd, addend, where);
2891                   break;
2892                 }
2893             }
2894           else if (ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2895             {
2896               /* Relocate against local STT_GNU_IFUNC symbol.  */
2897               h = elf_i386_get_local_sym_hash (htab, input_bfd,
2898                                                    rel, FALSE);
2899               if (h == NULL)
2900                 abort ();
2901
2902               /* Set STT_GNU_IFUNC symbol value.  */ 
2903               h->root.u.def.value = sym->st_value;
2904               h->root.u.def.section = sec;
2905             }
2906         }
2907       else
2908         {
2909           bfd_boolean warned;
2910
2911           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2912                                    r_symndx, symtab_hdr, sym_hashes,
2913                                    h, sec, relocation,
2914                                    unresolved_reloc, warned);
2915         }
2916
2917       if (sec != NULL && elf_discarded_section (sec))
2918         {
2919           /* For relocs against symbols from removed linkonce sections,
2920              or sections discarded by a linker script, we just want the
2921              section contents zeroed.  Avoid any special processing.  */
2922           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2923           rel->r_info = 0;
2924           rel->r_addend = 0;
2925           continue;
2926         }
2927
2928       if (info->relocatable)
2929         continue;
2930
2931       /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2932          it here if it is defined in a non-shared object.  */
2933       if (h != NULL
2934           && h->type == STT_GNU_IFUNC
2935           && h->def_regular)
2936         {
2937           asection *plt, *gotplt, *base_got;
2938           bfd_vma plt_index;
2939
2940           if ((input_section->flags & SEC_ALLOC) == 0
2941               || h->plt.offset == (bfd_vma) -1)
2942             abort ();
2943
2944           /* STT_GNU_IFUNC symbol must go through PLT.  */
2945           if (htab->elf.splt != NULL)
2946             {
2947               plt = htab->elf.splt;
2948               gotplt = htab->elf.sgotplt;
2949             }
2950           else
2951             {
2952               plt = htab->elf.iplt;
2953               gotplt = htab->elf.igotplt;
2954             }
2955
2956           relocation = (plt->output_section->vma
2957                         + plt->output_offset + h->plt.offset);
2958
2959           switch (r_type)
2960             {
2961             default:
2962               (*_bfd_error_handler)
2963                 (_("%B: relocation %s against STT_GNU_IFUNC "
2964                    "symbol `%s' isn't handled by %s"), input_bfd,
2965                  elf_howto_table[r_type].name,
2966                  (h->root.root.string
2967                   ? h->root.root.string : "a local symbol"),
2968                  __FUNCTION__);
2969               bfd_set_error (bfd_error_bad_value);
2970               return FALSE;
2971
2972             case R_386_32:
2973               /* Generate dynamic relcoation only when there is a
2974                  non-GOF reference in a shared object.  */
2975               if (info->shared && h->non_got_ref)
2976                 {
2977                   Elf_Internal_Rela outrel;
2978                   bfd_byte *loc;
2979                   asection *sreloc;
2980                   bfd_vma offset;
2981
2982                   /* Need a dynamic relocation to get the real function
2983                      adddress.  */
2984                   offset = _bfd_elf_section_offset (output_bfd,
2985                                                     info,
2986                                                     input_section,
2987                                                     rel->r_offset);
2988                   if (offset == (bfd_vma) -1
2989                       || offset == (bfd_vma) -2)
2990                     abort ();
2991
2992                   outrel.r_offset = (input_section->output_section->vma
2993                                      + input_section->output_offset
2994                                      + offset);
2995
2996                   if (h->dynindx == -1
2997                       || h->forced_local
2998                       || info->executable)
2999                     {
3000                       /* This symbol is resolved locally.  */
3001                       outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3002                       bfd_put_32 (output_bfd,
3003                                   (h->root.u.def.value 
3004                                    + h->root.u.def.section->output_section->vma
3005                                    + h->root.u.def.section->output_offset),
3006                                   contents + offset);
3007                     }
3008                   else
3009                     outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3010
3011                   sreloc = htab->elf.irelifunc;
3012                   loc = sreloc->contents;
3013                   loc += (sreloc->reloc_count++
3014                           * sizeof (Elf32_External_Rel));
3015                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3016
3017                   /* If this reloc is against an external symbol, we
3018                      do not want to fiddle with the addend.  Otherwise,
3019                      we need to include the symbol value so that it
3020                      becomes an addend for the dynamic reloc.  For an
3021                      internal symbol, we have updated addend.  */
3022                   continue;
3023                 }
3024
3025             case R_386_PC32:
3026             case R_386_PLT32:
3027               goto do_relocation;
3028
3029             case R_386_GOT32:
3030               base_got = htab->elf.sgot;
3031               off = h->got.offset;
3032
3033               if (base_got == NULL)
3034                 abort ();
3035
3036               if (off == (bfd_vma) -1)
3037                 {
3038                   /* We can't use h->got.offset here to save state, or
3039                      even just remember the offset, as finish_dynamic_symbol
3040                      would use that as offset into .got.  */
3041                   
3042                   if (htab->elf.splt != NULL)
3043                     {
3044                       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3045                       off = (plt_index + 3) * 4;
3046                       base_got = htab->elf.sgotplt;
3047                     }
3048                   else
3049                     {
3050                       plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3051                       off = plt_index * 4;
3052                       base_got = htab->elf.igotplt;
3053                     }
3054
3055                   if (h->dynindx == -1
3056                       || h->forced_local
3057                       || info->symbolic)
3058                     {
3059                       /* This references the local defitionion.  We must
3060                          initialize this entry in the global offset table.
3061                          Since the offset must always be a multiple of 8,
3062                          we use the least significant bit to record
3063                          whether we have initialized it already.
3064
3065                          When doing a dynamic link, we create a .rela.got
3066                          relocation entry to initialize the value.  This
3067                          is done in the finish_dynamic_symbol routine.   */
3068                       if ((off & 1) != 0)
3069                         off &= ~1;
3070                       else
3071                         {
3072                           bfd_put_32 (output_bfd, relocation,
3073                                       base_got->contents + off);
3074                           h->got.offset |= 1;
3075                         }
3076                     }
3077
3078                   relocation = off;
3079
3080                   /* Adjust for static executables.  */
3081                   if (htab->elf.splt == NULL)
3082                     relocation += gotplt->output_offset;
3083                 }
3084               else
3085                 {
3086                   relocation = (base_got->output_section->vma
3087                                 + base_got->output_offset + off
3088                                 - gotplt->output_section->vma
3089                                 - gotplt->output_offset);
3090                   /* Adjust for static executables.  */
3091                   if (htab->elf.splt == NULL)
3092                     relocation += gotplt->output_offset;
3093                 }
3094
3095               goto do_relocation;
3096
3097             case R_386_GOTOFF:
3098               relocation -= (gotplt->output_section->vma
3099                              + gotplt->output_offset);
3100               goto do_relocation;
3101             }
3102         }
3103
3104       switch (r_type)
3105         {
3106         case R_386_GOT32:
3107           /* Relocation is to the entry for this symbol in the global
3108              offset table.  */
3109           if (htab->elf.sgot == NULL)
3110             abort ();
3111
3112           if (h != NULL)
3113             {
3114               bfd_boolean dyn;
3115
3116               off = h->got.offset;
3117               dyn = htab->elf.dynamic_sections_created;
3118               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3119                   || (info->shared
3120                       && SYMBOL_REFERENCES_LOCAL (info, h))
3121                   || (ELF_ST_VISIBILITY (h->other)
3122                       && h->root.type == bfd_link_hash_undefweak))
3123                 {
3124                   /* This is actually a static link, or it is a
3125                      -Bsymbolic link and the symbol is defined
3126                      locally, or the symbol was forced to be local
3127                      because of a version file.  We must initialize
3128                      this entry in the global offset table.  Since the
3129                      offset must always be a multiple of 4, we use the
3130                      least significant bit to record whether we have
3131                      initialized it already.
3132
3133                      When doing a dynamic link, we create a .rel.got
3134                      relocation entry to initialize the value.  This
3135                      is done in the finish_dynamic_symbol routine.  */
3136                   if ((off & 1) != 0)
3137                     off &= ~1;
3138                   else
3139                     {
3140                       bfd_put_32 (output_bfd, relocation,
3141                                   htab->elf.sgot->contents + off);
3142                       h->got.offset |= 1;
3143                     }
3144                 }
3145               else
3146                 unresolved_reloc = FALSE;
3147             }
3148           else
3149             {
3150               if (local_got_offsets == NULL)
3151                 abort ();
3152
3153               off = local_got_offsets[r_symndx];
3154
3155               /* The offset must always be a multiple of 4.  We use
3156                  the least significant bit to record whether we have
3157                  already generated the necessary reloc.  */
3158               if ((off & 1) != 0)
3159                 off &= ~1;
3160               else
3161                 {
3162                   bfd_put_32 (output_bfd, relocation,
3163                               htab->elf.sgot->contents + off);
3164
3165                   if (info->shared)
3166                     {
3167                       asection *s;
3168                       Elf_Internal_Rela outrel;
3169                       bfd_byte *loc;
3170
3171                       s = htab->elf.srelgot;
3172                       if (s == NULL)
3173                         abort ();
3174
3175                       outrel.r_offset = (htab->elf.sgot->output_section->vma
3176                                          + htab->elf.sgot->output_offset
3177                                          + off);
3178                       outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3179                       loc = s->contents;
3180                       loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
3181                       bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3182                     }
3183
3184                   local_got_offsets[r_symndx] |= 1;
3185                 }
3186             }
3187
3188           if (off >= (bfd_vma) -2)
3189             abort ();
3190
3191           relocation = htab->elf.sgot->output_section->vma
3192                        + htab->elf.sgot->output_offset + off
3193                        - htab->elf.sgotplt->output_section->vma
3194                        - htab->elf.sgotplt->output_offset;
3195           break;
3196
3197         case R_386_GOTOFF:
3198           /* Relocation is relative to the start of the global offset
3199              table.  */
3200
3201           /* Check to make sure it isn't a protected function symbol
3202              for shared library since it may not be local when used
3203              as function address.  We also need to make sure that a
3204              symbol is defined locally.  */
3205           if (info->shared && h)
3206             {
3207               if (!h->def_regular)
3208                 {
3209                   const char *v;
3210
3211                   switch (ELF_ST_VISIBILITY (h->other))
3212                     {
3213                     case STV_HIDDEN:
3214                       v = _("hidden symbol");
3215                       break;
3216                     case STV_INTERNAL:
3217                       v = _("internal symbol");
3218                       break;
3219                     case STV_PROTECTED:
3220                       v = _("protected symbol");
3221                       break;
3222                     default:
3223                       v = _("symbol");
3224                       break;
3225                     }
3226
3227                   (*_bfd_error_handler)
3228                     (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
3229                      input_bfd, v, h->root.root.string);
3230                   bfd_set_error (bfd_error_bad_value);
3231                   return FALSE;
3232                 }
3233               else if (!info->executable
3234                        && h->type == STT_FUNC
3235                        && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3236                 {
3237                   (*_bfd_error_handler)
3238                     (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
3239                      input_bfd, h->root.root.string);
3240                   bfd_set_error (bfd_error_bad_value);
3241                   return FALSE;
3242                 }
3243             }
3244
3245           /* Note that sgot is not involved in this
3246              calculation.  We always want the start of .got.plt.  If we
3247              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3248              permitted by the ABI, we might have to change this
3249              calculation.  */
3250           relocation -= htab->elf.sgotplt->output_section->vma
3251                         + htab->elf.sgotplt->output_offset;
3252           break;
3253
3254         case R_386_GOTPC:
3255           /* Use global offset table as symbol value.  */
3256           relocation = htab->elf.sgotplt->output_section->vma
3257                        + htab->elf.sgotplt->output_offset;
3258           unresolved_reloc = FALSE;
3259           break;
3260
3261         case R_386_PLT32:
3262           /* Relocation is to the entry for this symbol in the
3263              procedure linkage table.  */
3264
3265           /* Resolve a PLT32 reloc against a local symbol directly,
3266              without using the procedure linkage table.  */
3267           if (h == NULL)
3268             break;
3269
3270           if (h->plt.offset == (bfd_vma) -1
3271               || htab->elf.splt == NULL)
3272             {
3273               /* We didn't make a PLT entry for this symbol.  This
3274                  happens when statically linking PIC code, or when
3275                  using -Bsymbolic.  */
3276               break;
3277             }
3278
3279           relocation = (htab->elf.splt->output_section->vma
3280                         + htab->elf.splt->output_offset
3281                         + h->plt.offset);
3282           unresolved_reloc = FALSE;
3283           break;
3284
3285         case R_386_32:
3286         case R_386_PC32:
3287           if ((input_section->flags & SEC_ALLOC) == 0
3288               || is_vxworks_tls)
3289             break;
3290
3291           if ((info->shared
3292                && (h == NULL
3293                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3294                    || h->root.type != bfd_link_hash_undefweak)
3295                && (r_type != R_386_PC32
3296                    || !SYMBOL_CALLS_LOCAL (info, h)))
3297               || (ELIMINATE_COPY_RELOCS
3298                   && !info->shared
3299                   && h != NULL
3300                   && h->dynindx != -1
3301                   && !h->non_got_ref
3302                   && ((h->def_dynamic
3303                        && !h->def_regular)
3304                       || h->root.type == bfd_link_hash_undefweak
3305                       || h->root.type == bfd_link_hash_undefined)))
3306             {
3307               Elf_Internal_Rela outrel;
3308               bfd_byte *loc;
3309               bfd_boolean skip, relocate;
3310               asection *sreloc;
3311
3312               /* When generating a shared object, these relocations
3313                  are copied into the output file to be resolved at run
3314                  time.  */
3315
3316               skip = FALSE;
3317               relocate = FALSE;
3318
3319               outrel.r_offset =
3320                 _bfd_elf_section_offset (output_bfd, info, input_section,
3321                                          rel->r_offset);
3322               if (outrel.r_offset == (bfd_vma) -1)
3323                 skip = TRUE;
3324               else if (outrel.r_offset == (bfd_vma) -2)
3325                 skip = TRUE, relocate = TRUE;
3326               outrel.r_offset += (input_section->output_section->vma
3327                                   + input_section->output_offset);
3328
3329               if (skip)
3330                 memset (&outrel, 0, sizeof outrel);
3331               else if (h != NULL
3332                        && h->dynindx != -1
3333                        && (r_type == R_386_PC32
3334                            || !info->shared
3335                            || !SYMBOLIC_BIND (info, h)
3336                            || !h->def_regular))
3337                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3338               else
3339                 {
3340                   /* This symbol is local, or marked to become local.  */
3341                   relocate = TRUE;
3342                   outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3343                 }
3344
3345               sreloc = elf_section_data (input_section)->sreloc;
3346
3347               BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3348
3349               loc = sreloc->contents;
3350               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3351
3352               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3353
3354               /* If this reloc is against an external symbol, we do
3355                  not want to fiddle with the addend.  Otherwise, we
3356                  need to include the symbol value so that it becomes
3357                  an addend for the dynamic reloc.  */
3358               if (! relocate)
3359                 continue;
3360             }
3361           break;
3362
3363         case R_386_TLS_IE:
3364           if (info->shared)
3365             {
3366               Elf_Internal_Rela outrel;
3367               bfd_byte *loc;
3368               asection *sreloc;
3369
3370               outrel.r_offset = rel->r_offset
3371                                 + input_section->output_section->vma
3372                                 + input_section->output_offset;
3373               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3374               sreloc = elf_section_data (input_section)->sreloc;
3375               if (sreloc == NULL)
3376                 abort ();
3377               loc = sreloc->contents;
3378               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3379               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3380             }
3381           /* Fall through */
3382
3383         case R_386_TLS_GD:
3384         case R_386_TLS_GOTDESC:
3385         case R_386_TLS_DESC_CALL:
3386         case R_386_TLS_IE_32:
3387         case R_386_TLS_GOTIE:
3388           tls_type = GOT_UNKNOWN;
3389           if (h == NULL && local_got_offsets)
3390             tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
3391           else if (h != NULL)
3392             tls_type = elf_i386_hash_entry(h)->tls_type;
3393           if (tls_type == GOT_TLS_IE)
3394             tls_type = GOT_TLS_IE_NEG;
3395
3396           if (! elf_i386_tls_transition (info, input_bfd,
3397                                          input_section, contents,
3398                                          symtab_hdr, sym_hashes,
3399                                          &r_type, tls_type, rel,
3400                                          relend, h))
3401             return FALSE;
3402
3403           if (r_type == R_386_TLS_LE_32)
3404             {
3405               BFD_ASSERT (! unresolved_reloc);
3406               if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3407                 {
3408                   unsigned int type;
3409                   bfd_vma roff;
3410
3411                   /* GD->LE transition.  */
3412                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3413                   if (type == 0x04)
3414                     {
3415                       /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3416                          Change it into:
3417                          movl %gs:0, %eax; subl $foo@tpoff, %eax
3418                          (6 byte form of subl).  */
3419                       memcpy (contents + rel->r_offset - 3,
3420                               "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3421                       roff = rel->r_offset + 5;
3422                     }
3423                   else
3424                     {
3425                       /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3426                          Change it into:
3427                          movl %gs:0, %eax; subl $foo@tpoff, %eax
3428                          (6 byte form of subl).  */
3429                       memcpy (contents + rel->r_offset - 2,
3430                               "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3431                       roff = rel->r_offset + 6;
3432                     }
3433                   bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3434                               contents + roff);
3435                   /* Skip R_386_PC32/R_386_PLT32.  */
3436                   rel++;
3437                   continue;
3438                 }
3439               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3440                 {
3441                   /* GDesc -> LE transition.
3442                      It's originally something like:
3443                      leal x@tlsdesc(%ebx), %eax
3444
3445                      leal x@ntpoff, %eax
3446
3447                      Registers other than %eax may be set up here.  */
3448
3449                   unsigned int val;
3450                   bfd_vma roff;
3451
3452                   roff = rel->r_offset;
3453                   val = bfd_get_8 (input_bfd, contents + roff - 1);
3454
3455                   /* Now modify the instruction as appropriate.  */
3456                   /* aoliva FIXME: remove the above and xor the byte
3457                      below with 0x86.  */
3458                   bfd_put_8 (output_bfd, val ^ 0x86,
3459                              contents + roff - 1);
3460                   bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3461                               contents + roff);
3462                   continue;
3463                 }
3464               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3465                 {
3466                   /* GDesc -> LE transition.
3467                      It's originally:
3468                      call *(%eax)
3469                      Turn it into:
3470                      xchg %ax,%ax  */
3471
3472                   bfd_vma roff;
3473                   
3474                   roff = rel->r_offset;
3475                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3476                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3477                   continue;
3478                 }
3479               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
3480                 {
3481                   unsigned int val;
3482
3483                   /* IE->LE transition:
3484                      Originally it can be one of:
3485                      movl foo, %eax
3486                      movl foo, %reg
3487                      addl foo, %reg
3488                      We change it into:
3489                      movl $foo, %eax
3490                      movl $foo, %reg
3491                      addl $foo, %reg.  */
3492                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3493                   if (val == 0xa1)
3494                     {
3495                       /* movl foo, %eax.  */
3496                       bfd_put_8 (output_bfd, 0xb8,
3497                                  contents + rel->r_offset - 1);
3498                     }
3499                   else
3500                     {
3501                       unsigned int type;
3502
3503                       type = bfd_get_8 (input_bfd,
3504                                         contents + rel->r_offset - 2);
3505                       switch (type)
3506                         {
3507                         case 0x8b:
3508                           /* movl */
3509                           bfd_put_8 (output_bfd, 0xc7,
3510                                      contents + rel->r_offset - 2);
3511                           bfd_put_8 (output_bfd,
3512                                      0xc0 | ((val >> 3) & 7),
3513                                      contents + rel->r_offset - 1);
3514                           break;
3515                         case 0x03:
3516                           /* addl */
3517                           bfd_put_8 (output_bfd, 0x81,
3518                                      contents + rel->r_offset - 2);
3519                           bfd_put_8 (output_bfd,
3520                                      0xc0 | ((val >> 3) & 7),
3521                                      contents + rel->r_offset - 1);
3522                           break;
3523                         default:
3524                           BFD_FAIL ();
3525                           break;
3526                         }
3527                     }
3528                   bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3529                               contents + rel->r_offset);
3530                   continue;
3531                 }
3532               else
3533                 {
3534                   unsigned int val, type;
3535
3536                   /* {IE_32,GOTIE}->LE transition:
3537                      Originally it can be one of:
3538                      subl foo(%reg1), %reg2
3539                      movl foo(%reg1), %reg2
3540                      addl foo(%reg1), %reg2
3541                      We change it into:
3542                      subl $foo, %reg2
3543                      movl $foo, %reg2 (6 byte form)
3544                      addl $foo, %reg2.  */
3545                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3546                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3547                   if (type == 0x8b)
3548                     {
3549                       /* movl */
3550                       bfd_put_8 (output_bfd, 0xc7,
3551                                  contents + rel->r_offset - 2);
3552                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3553                                  contents + rel->r_offset - 1);
3554                     }
3555                   else if (type == 0x2b)
3556                     {
3557                       /* subl */
3558                       bfd_put_8 (output_bfd, 0x81,
3559                                  contents + rel->r_offset - 2);
3560                       bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3561                                  contents + rel->r_offset - 1);
3562                     }
3563                   else if (type == 0x03)
3564                     {
3565                       /* addl */
3566                       bfd_put_8 (output_bfd, 0x81,
3567                                  contents + rel->r_offset - 2);
3568                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3569                                  contents + rel->r_offset - 1);
3570                     }
3571                   else
3572                     BFD_FAIL ();
3573                   if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
3574                     bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3575                                 contents + rel->r_offset);
3576                   else
3577                     bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3578                                 contents + rel->r_offset);
3579                   continue;
3580                 }
3581             }
3582
3583           if (htab->elf.sgot == NULL)
3584             abort ();
3585
3586           if (h != NULL)
3587             {
3588               off = h->got.offset;
3589               offplt = elf_i386_hash_entry (h)->tlsdesc_got;
3590             }
3591           else
3592             {
3593               if (local_got_offsets == NULL)
3594                 abort ();
3595
3596               off = local_got_offsets[r_symndx];
3597               offplt = local_tlsdesc_gotents[r_symndx];
3598             }
3599
3600           if ((off & 1) != 0)
3601             off &= ~1;
3602           else
3603             {
3604               Elf_Internal_Rela outrel;
3605               bfd_byte *loc;
3606               int dr_type, indx;
3607               asection *sreloc;
3608
3609               if (htab->elf.srelgot == NULL)
3610                 abort ();
3611
3612               indx = h && h->dynindx != -1 ? h->dynindx : 0;
3613
3614               if (GOT_TLS_GDESC_P (tls_type))
3615                 {
3616                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3617                   BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3618                               <= htab->elf.sgotplt->size);
3619                   outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3620                                      + htab->elf.sgotplt->output_offset
3621                                      + offplt
3622                                      + htab->sgotplt_jump_table_size);
3623                   sreloc = htab->elf.srelplt;
3624                   loc = sreloc->contents;
3625                   loc += (htab->next_tls_desc_index++
3626                           * sizeof (Elf32_External_Rel));
3627                   BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3628                               <= sreloc->contents + sreloc->size);
3629                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3630                   if (indx == 0)
3631                     {
3632                       BFD_ASSERT (! unresolved_reloc);
3633                       bfd_put_32 (output_bfd,
3634                                   relocation - elf_i386_dtpoff_base (info),
3635                                   htab->elf.sgotplt->contents + offplt
3636                                   + htab->sgotplt_jump_table_size + 4);
3637                     }
3638                   else
3639                     {
3640                       bfd_put_32 (output_bfd, 0,
3641                                   htab->elf.sgotplt->contents + offplt
3642                                   + htab->sgotplt_jump_table_size + 4);
3643                     }
3644                 }
3645
3646               sreloc = htab->elf.srelgot;
3647
3648               outrel.r_offset = (htab->elf.sgot->output_section->vma
3649                                  + htab->elf.sgot->output_offset + off);
3650
3651               if (GOT_TLS_GD_P (tls_type))
3652                 dr_type = R_386_TLS_DTPMOD32;
3653               else if (GOT_TLS_GDESC_P (tls_type))
3654                 goto dr_done;
3655               else if (tls_type == GOT_TLS_IE_POS)
3656                 dr_type = R_386_TLS_TPOFF;
3657               else
3658                 dr_type = R_386_TLS_TPOFF32;
3659
3660               if (dr_type == R_386_TLS_TPOFF && indx == 0)
3661                 bfd_put_32 (output_bfd,
3662                             relocation - elf_i386_dtpoff_base (info),
3663                             htab->elf.sgot->contents + off);
3664               else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3665                 bfd_put_32 (output_bfd, 
3666                             elf_i386_dtpoff_base (info) - relocation,
3667                             htab->elf.sgot->contents + off);
3668               else if (dr_type != R_386_TLS_DESC)
3669                 bfd_put_32 (output_bfd, 0,
3670                             htab->elf.sgot->contents + off);
3671               outrel.r_info = ELF32_R_INFO (indx, dr_type);
3672
3673               loc = sreloc->contents;
3674               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3675               BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3676                           <= sreloc->contents + sreloc->size);
3677               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3678
3679               if (GOT_TLS_GD_P (tls_type))
3680                 {
3681                   if (indx == 0)
3682                     {
3683                       BFD_ASSERT (! unresolved_reloc);
3684                       bfd_put_32 (output_bfd,
3685                                   relocation - elf_i386_dtpoff_base (info),
3686                                   htab->elf.sgot->contents + off + 4);
3687                     }
3688                   else
3689                     {
3690                       bfd_put_32 (output_bfd, 0,
3691                                   htab->elf.sgot->contents + off + 4);
3692                       outrel.r_info = ELF32_R_INFO (indx,
3693                                                     R_386_TLS_DTPOFF32);
3694                       outrel.r_offset += 4;
3695                       sreloc->reloc_count++;
3696                       loc += sizeof (Elf32_External_Rel);
3697                       BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3698                                   <= sreloc->contents + sreloc->size);
3699                       bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3700                     }
3701                 }
3702               else if (tls_type == GOT_TLS_IE_BOTH)
3703                 {
3704                   bfd_put_32 (output_bfd,
3705                               (indx == 0
3706                                ? relocation - elf_i386_dtpoff_base (info)
3707                                : 0),
3708                               htab->elf.sgot->contents + off + 4);
3709                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3710                   outrel.r_offset += 4;
3711                   sreloc->reloc_count++;
3712                   loc += sizeof (Elf32_External_Rel);
3713                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3714                 }
3715
3716             dr_done:
3717               if (h != NULL)
3718                 h->got.offset |= 1;
3719               else
3720                 local_got_offsets[r_symndx] |= 1;
3721             }
3722
3723           if (off >= (bfd_vma) -2
3724               && ! GOT_TLS_GDESC_P (tls_type))
3725             abort ();
3726           if (r_type == R_386_TLS_GOTDESC
3727               || r_type == R_386_TLS_DESC_CALL)
3728             {
3729               relocation = htab->sgotplt_jump_table_size + offplt;
3730               unresolved_reloc = FALSE;
3731             }
3732           else if (r_type == ELF32_R_TYPE (rel->r_info))
3733             {
3734               bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3735                               + htab->elf.sgotplt->output_offset;
3736               relocation = htab->elf.sgot->output_section->vma
3737                 + htab->elf.sgot->output_offset + off - g_o_t;
3738               if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3739                   && tls_type == GOT_TLS_IE_BOTH)
3740                 relocation += 4;
3741               if (r_type == R_386_TLS_IE)
3742                 relocation += g_o_t;
3743               unresolved_reloc = FALSE;
3744             }
3745           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3746             {
3747               unsigned int val, type;
3748               bfd_vma roff;
3749
3750               /* GD->IE transition.  */
3751               type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3752               val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3753               if (type == 0x04)
3754                 {
3755                   /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3756                      Change it into:
3757                      movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
3758                   val >>= 3;
3759                   roff = rel->r_offset - 3;
3760                 }
3761               else
3762                 {
3763                   /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3764                      Change it into:
3765                      movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
3766                   roff = rel->r_offset - 2;
3767                 }
3768               memcpy (contents + roff,
3769                       "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3770               contents[roff + 7] = 0x80 | (val & 7);
3771               /* If foo is used only with foo@gotntpoff(%reg) and
3772                  foo@indntpoff, but not with foo@gottpoff(%reg), change
3773                  subl $foo@gottpoff(%reg), %eax
3774                  into:
3775                  addl $foo@gotntpoff(%reg), %eax.  */
3776               if (tls_type == GOT_TLS_IE_POS)
3777                 contents[roff + 6] = 0x03;
3778               bfd_put_32 (output_bfd,
3779                           htab->elf.sgot->output_section->vma
3780                           + htab->elf.sgot->output_offset + off
3781                           - htab->elf.sgotplt->output_section->vma
3782                           - htab->elf.sgotplt->output_offset,
3783                           contents + roff + 8);
3784               /* Skip R_386_PLT32.  */
3785               rel++;
3786               continue;
3787             }
3788           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3789             {
3790               /* GDesc -> IE transition.
3791                  It's originally something like:
3792                  leal x@tlsdesc(%ebx), %eax
3793
3794                  Change it to:
3795                  movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3796                  or:
3797                  movl x@gottpoff(%ebx), %eax # before negl %eax
3798
3799                  Registers other than %eax may be set up here.  */
3800
3801               bfd_vma roff;
3802
3803               /* First, make sure it's a leal adding ebx to a 32-bit
3804                  offset into any register, although it's probably
3805                  almost always going to be eax.  */
3806               roff = rel->r_offset;
3807
3808               /* Now modify the instruction as appropriate.  */
3809               /* To turn a leal into a movl in the form we use it, it
3810                  suffices to change the first byte from 0x8d to 0x8b.
3811                  aoliva FIXME: should we decide to keep the leal, all
3812                  we have to do is remove the statement below, and
3813                  adjust the relaxation of R_386_TLS_DESC_CALL.  */
3814               bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3815
3816               if (tls_type == GOT_TLS_IE_BOTH)
3817                 off += 4;
3818
3819               bfd_put_32 (output_bfd,
3820                           htab->elf.sgot->output_section->vma
3821                           + htab->elf.sgot->output_offset + off
3822                           - htab->elf.sgotplt->output_section->vma
3823                           - htab->elf.sgotplt->output_offset,
3824                           contents + roff);
3825               continue;
3826             }
3827           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3828             {
3829               /* GDesc -> IE transition.
3830                  It's originally:
3831                  call *(%eax)
3832
3833                  Change it to:
3834                  xchg %ax,%ax
3835                  or
3836                  negl %eax
3837                  depending on how we transformed the TLS_GOTDESC above.
3838               */
3839
3840               bfd_vma roff;
3841
3842               roff = rel->r_offset;
3843
3844               /* Now modify the instruction as appropriate.  */
3845               if (tls_type != GOT_TLS_IE_NEG)
3846                 {
3847                   /* xchg %ax,%ax */
3848                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3849                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3850                 }
3851               else
3852                 {
3853                   /* negl %eax */
3854                   bfd_put_8 (output_bfd, 0xf7, contents + roff);
3855                   bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3856                 }
3857
3858               continue;
3859             }
3860           else
3861             BFD_ASSERT (FALSE);
3862           break;
3863
3864         case R_386_TLS_LDM:
3865           if (! elf_i386_tls_transition (info, input_bfd,
3866                                          input_section, contents,
3867                                          symtab_hdr, sym_hashes,
3868                                          &r_type, GOT_UNKNOWN, rel,
3869                                          relend, h))
3870             return FALSE;
3871
3872           if (r_type != R_386_TLS_LDM)
3873             {
3874               /* LD->LE transition:
3875                  leal foo(%reg), %eax; call ___tls_get_addr.
3876                  We change it into:
3877                  movl %gs:0, %eax; nop; leal 0(%esi,1), %esi.  */
3878               BFD_ASSERT (r_type == R_386_TLS_LE_32);
3879               memcpy (contents + rel->r_offset - 2,
3880                       "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3881               /* Skip R_386_PC32/R_386_PLT32.  */
3882               rel++;
3883               continue;
3884             }
3885
3886           if (htab->elf.sgot == NULL)
3887             abort ();
3888
3889           off = htab->tls_ldm_got.offset;
3890           if (off & 1)
3891             off &= ~1;
3892           else
3893             {
3894               Elf_Internal_Rela outrel;
3895               bfd_byte *loc;
3896
3897               if (htab->elf.srelgot == NULL)
3898                 abort ();
3899
3900               outrel.r_offset = (htab->elf.sgot->output_section->vma
3901                                  + htab->elf.sgot->output_offset + off);
3902
3903               bfd_put_32 (output_bfd, 0,
3904                           htab->elf.sgot->contents + off);
3905               bfd_put_32 (output_bfd, 0,
3906                           htab->elf.sgot->contents + off + 4);
3907               outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3908               loc = htab->elf.srelgot->contents;
3909               loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3910               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3911               htab->tls_ldm_got.offset |= 1;
3912             }
3913           relocation = htab->elf.sgot->output_section->vma
3914                        + htab->elf.sgot->output_offset + off
3915                        - htab->elf.sgotplt->output_section->vma
3916                        - htab->elf.sgotplt->output_offset;
3917           unresolved_reloc = FALSE;
3918           break;
3919
3920         case R_386_TLS_LDO_32:
3921           if (info->shared || (input_section->flags & SEC_CODE) == 0)
3922             relocation -= elf_i386_dtpoff_base (info);
3923           else
3924             /* When converting LDO to LE, we must negate.  */
3925             relocation = -elf_i386_tpoff (info, relocation);
3926           break;
3927
3928         case R_386_TLS_LE_32:
3929         case R_386_TLS_LE:
3930           if (info->shared)
3931             {
3932               Elf_Internal_Rela outrel;
3933               asection *sreloc;
3934               bfd_byte *loc;
3935               int indx;
3936
3937               outrel.r_offset = rel->r_offset
3938                                 + input_section->output_section->vma
3939                                 + input_section->output_offset;
3940               if (h != NULL && h->dynindx != -1)
3941                 indx = h->dynindx;
3942               else
3943                 indx = 0;
3944               if (r_type == R_386_TLS_LE_32)
3945                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3946               else
3947                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3948               sreloc = elf_section_data (input_section)->sreloc;
3949               if (sreloc == NULL)
3950                 abort ();
3951               loc = sreloc->contents;
3952               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3953               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3954               if (indx)
3955                 continue;
3956               else if (r_type == R_386_TLS_LE_32)
3957                 relocation = elf_i386_dtpoff_base (info) - relocation;
3958               else
3959                 relocation -= elf_i386_dtpoff_base (info);
3960             }
3961           else if (r_type == R_386_TLS_LE_32)
3962             relocation = elf_i386_tpoff (info, relocation);
3963           else
3964             relocation = -elf_i386_tpoff (info, relocation);
3965           break;
3966
3967         default:
3968           break;
3969         }
3970
3971       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3972          because such sections are not SEC_ALLOC and thus ld.so will
3973          not process them.  */
3974       if (unresolved_reloc
3975           && !((input_section->flags & SEC_DEBUGGING) != 0
3976                && h->def_dynamic))
3977         {
3978           (*_bfd_error_handler)
3979             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3980              input_bfd,
3981              input_section,
3982              (long) rel->r_offset,
3983              howto->name,
3984              h->root.root.string);
3985           return FALSE;
3986         }
3987
3988 do_relocation:
3989       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3990                                     contents, rel->r_offset,
3991                                     relocation, 0);
3992
3993       if (r != bfd_reloc_ok)
3994         {
3995           const char *name;
3996
3997           if (h != NULL)
3998             name = h->root.root.string;
3999           else
4000             {
4001               name = bfd_elf_string_from_elf_section (input_bfd,
4002                                                       symtab_hdr->sh_link,
4003                                                       sym->st_name);
4004               if (name == NULL)
4005                 return FALSE;
4006               if (*name == '\0')
4007                 name = bfd_section_name (input_bfd, sec);
4008             }
4009
4010           if (r == bfd_reloc_overflow)
4011             {
4012               if (! ((*info->callbacks->reloc_overflow)
4013                      (info, (h ? &h->root : NULL), name, howto->name,
4014                       (bfd_vma) 0, input_bfd, input_section,
4015                       rel->r_offset)))
4016                 return FALSE;
4017             }
4018           else
4019             {
4020               (*_bfd_error_handler)
4021                 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
4022                  input_bfd, input_section,
4023                  (long) rel->r_offset, name, (int) r);
4024               return FALSE;
4025             }
4026         }
4027     }
4028
4029   return TRUE;
4030 }
4031
4032 /* Finish up dynamic symbol handling.  We set the contents of various
4033    dynamic sections here.  */
4034
4035 static bfd_boolean
4036 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
4037                                 struct bfd_link_info *info,
4038                                 struct elf_link_hash_entry *h,
4039                                 Elf_Internal_Sym *sym)
4040 {
4041   struct elf_i386_link_hash_table *htab;
4042
4043   htab = elf_i386_hash_table (info);
4044
4045   if (h->plt.offset != (bfd_vma) -1)
4046     {
4047       bfd_vma plt_index;
4048       bfd_vma got_offset;
4049       Elf_Internal_Rela rel;
4050       bfd_byte *loc;
4051       asection *plt, *gotplt, *relplt;
4052
4053       /* When building a static executable, use .iplt, .igot.plt and
4054          .rel.iplt sections for STT_GNU_IFUNC symbols.  */
4055       if (htab->elf.splt != NULL)
4056         {
4057           plt = htab->elf.splt;
4058           gotplt = htab->elf.sgotplt;
4059           relplt = htab->elf.srelplt;
4060         }
4061       else
4062         {
4063           plt = htab->elf.iplt;
4064           gotplt = htab->elf.igotplt;
4065           relplt = htab->elf.irelplt;
4066         }
4067
4068       /* This symbol has an entry in the procedure linkage table.  Set
4069          it up.  */
4070
4071       if ((h->dynindx == -1
4072            && !((h->forced_local || info->executable)
4073                 && h->def_regular
4074                 && h->type == STT_GNU_IFUNC))
4075           || plt == NULL
4076           || gotplt == NULL
4077           || relplt == NULL)
4078         abort ();
4079
4080       /* Get the index in the procedure linkage table which
4081          corresponds to this symbol.  This is the index of this symbol
4082          in all the symbols for which we are making plt entries.  The
4083          first entry in the procedure linkage table is reserved.
4084
4085          Get the offset into the .got table of the entry that
4086          corresponds to this function.  Each .got entry is 4 bytes.
4087          The first three are reserved.
4088          
4089          For static executables, we don't reserve anything.  */
4090
4091       if (plt == htab->elf.splt)
4092         {
4093           plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
4094           got_offset = (plt_index + 3) * 4;
4095         }
4096       else
4097         {
4098           plt_index = h->plt.offset / PLT_ENTRY_SIZE;
4099           got_offset = plt_index * 4;
4100         }
4101
4102       /* Fill in the entry in the procedure linkage table.  */
4103       if (! info->shared)
4104         {
4105           memcpy (plt->contents + h->plt.offset, elf_i386_plt_entry,
4106                   PLT_ENTRY_SIZE);
4107           bfd_put_32 (output_bfd,
4108                       (gotplt->output_section->vma
4109                        + gotplt->output_offset
4110                        + got_offset),
4111                       plt->contents + h->plt.offset + 2);
4112
4113           if (htab->is_vxworks)
4114             {
4115               int s, k, reloc_index;
4116
4117               /* Create the R_386_32 relocation referencing the GOT
4118                  for this PLT entry.  */
4119
4120               /* S: Current slot number (zero-based).  */
4121               s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
4122               /* K: Number of relocations for PLTResolve. */
4123               if (info->shared)
4124                 k = PLTRESOLVE_RELOCS_SHLIB;
4125               else
4126                 k = PLTRESOLVE_RELOCS;
4127               /* Skip the PLTresolve relocations, and the relocations for
4128                  the other PLT slots. */
4129               reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
4130               loc = (htab->srelplt2->contents + reloc_index
4131                      * sizeof (Elf32_External_Rel));
4132
4133               rel.r_offset = (htab->elf.splt->output_section->vma
4134                               + htab->elf.splt->output_offset
4135                               + h->plt.offset + 2),
4136               rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4137               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4138
4139               /* Create the R_386_32 relocation referencing the beginning of
4140                  the PLT for this GOT entry.  */
4141               rel.r_offset = (htab->elf.sgotplt->output_section->vma
4142                               + htab->elf.sgotplt->output_offset
4143                               + got_offset);
4144               rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4145               bfd_elf32_swap_reloc_out (output_bfd, &rel,
4146               loc + sizeof (Elf32_External_Rel));
4147             }
4148         }
4149       else
4150         {
4151           memcpy (plt->contents + h->plt.offset, elf_i386_pic_plt_entry,
4152                   PLT_ENTRY_SIZE);
4153           bfd_put_32 (output_bfd, got_offset,
4154                       plt->contents + h->plt.offset + 2);
4155         }
4156
4157       /* Don't fill PLT entry for static executables.  */
4158       if (plt == htab->elf.splt)
4159         {
4160           bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
4161                       plt->contents + h->plt.offset + 7);
4162           bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
4163                       plt->contents + h->plt.offset + 12);
4164         }
4165
4166       /* Fill in the entry in the global offset table.  */
4167       bfd_put_32 (output_bfd,
4168                   (plt->output_section->vma
4169                    + plt->output_offset
4170                    + h->plt.offset
4171                    + 6),
4172                   gotplt->contents + got_offset);
4173
4174       /* Fill in the entry in the .rel.plt section.  */
4175       rel.r_offset = (gotplt->output_section->vma
4176                       + gotplt->output_offset
4177                       + got_offset);
4178       if (h->dynindx == -1
4179           || ((info->executable
4180                || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4181               && h->def_regular
4182                && h->type == STT_GNU_IFUNC))
4183         {
4184           /* If an STT_GNU_IFUNC symbol is locally defined, generate
4185              R_386_IRELATIVE instead of R_386_JUMP_SLOT.  Store addend
4186              in the .got.plt section.  */
4187           bfd_put_32 (output_bfd,
4188                       (h->root.u.def.value 
4189                        + h->root.u.def.section->output_section->vma
4190                        + h->root.u.def.section->output_offset),
4191                       gotplt->contents + got_offset);
4192           rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4193         }
4194       else
4195         rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
4196       loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
4197       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4198
4199       if (!h->def_regular)
4200         {
4201           /* Mark the symbol as undefined, rather than as defined in
4202              the .plt section.  Leave the value if there were any
4203              relocations where pointer equality matters (this is a clue
4204              for the dynamic linker, to make function pointer
4205              comparisons work between an application and shared
4206              library), otherwise set it to zero.  If a function is only
4207              called from a binary, there is no need to slow down
4208              shared libraries because of that.  */
4209           sym->st_shndx = SHN_UNDEF;
4210           if (!h->pointer_equality_needed)
4211             sym->st_value = 0;
4212         }
4213     }
4214
4215   if (h->got.offset != (bfd_vma) -1
4216       && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
4217       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
4218     {
4219       Elf_Internal_Rela rel;
4220       bfd_byte *loc;
4221
4222       /* This symbol has an entry in the global offset table.  Set it
4223          up.  */
4224
4225       if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4226         abort ();
4227
4228       rel.r_offset = (htab->elf.sgot->output_section->vma
4229                       + htab->elf.sgot->output_offset
4230                       + (h->got.offset & ~(bfd_vma) 1));
4231
4232       /* If this is a static link, or it is a -Bsymbolic link and the
4233          symbol is defined locally or was forced to be local because
4234          of a version file, we just want to emit a RELATIVE reloc.
4235          The entry in the global offset table will already have been
4236          initialized in the relocate_section function.  */
4237       if (h->def_regular
4238           && h->type == STT_GNU_IFUNC)
4239         {
4240           if (info->shared)
4241             {
4242               /* Generate R_386_GLOB_DAT.  */
4243               goto do_glob_dat;
4244             }
4245           else
4246             {
4247               if (!h->pointer_equality_needed)
4248                 abort ();
4249
4250               /* For non-shared object, we can't use .got.plt, which
4251                  contains the real function addres if we need pointer
4252                  equality.  We load the GOT entry with the PLT entry.  */
4253               asection *plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4254               bfd_put_32 (output_bfd,
4255                           (plt->output_section->vma
4256                            + plt->output_offset + h->plt.offset),
4257                           htab->elf.sgot->contents + h->got.offset);
4258               return TRUE;
4259             }
4260         }
4261       else if (info->shared
4262                && SYMBOL_REFERENCES_LOCAL (info, h))
4263         {
4264           BFD_ASSERT((h->got.offset & 1) != 0);
4265           rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4266         }
4267       else
4268         {
4269           BFD_ASSERT((h->got.offset & 1) == 0);
4270 do_glob_dat:
4271           bfd_put_32 (output_bfd, (bfd_vma) 0,
4272                       htab->elf.sgot->contents + h->got.offset);
4273           rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
4274         }
4275
4276       loc = htab->elf.srelgot->contents;
4277       loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
4278       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4279     }
4280
4281   if (h->needs_copy)
4282     {
4283       Elf_Internal_Rela rel;
4284       bfd_byte *loc;
4285
4286       /* This symbol needs a copy reloc.  Set it up.  */
4287
4288       if (h->dynindx == -1
4289           || (h->root.type != bfd_link_hash_defined
4290               && h->root.type != bfd_link_hash_defweak)
4291           || htab->srelbss == NULL)
4292         abort ();
4293
4294       rel.r_offset = (h->root.u.def.value
4295                       + h->root.u.def.section->output_section->vma
4296                       + h->root.u.def.section->output_offset);
4297       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
4298       loc = htab->srelbss->contents;
4299       loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
4300       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4301     }
4302
4303   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
4304      be NULL for local symbols.
4305
4306      On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
4307      is relative to the ".got" section.  */
4308   if (sym != NULL
4309       && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4310           || (!htab->is_vxworks && h == htab->elf.hgot)))
4311     sym->st_shndx = SHN_ABS;
4312
4313   return TRUE;
4314 }
4315
4316 /* Finish up local dynamic symbol handling.  We set the contents of
4317    various dynamic sections here.  */
4318
4319 static bfd_boolean
4320 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4321 {
4322   struct elf_link_hash_entry *h
4323     = (struct elf_link_hash_entry *) *slot;
4324   struct bfd_link_info *info
4325     = (struct bfd_link_info *) inf; 
4326
4327   return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
4328                                          h, NULL);
4329 }
4330
4331 /* Used to decide how to sort relocs in an optimal manner for the
4332    dynamic linker, before writing them out.  */
4333
4334 static enum elf_reloc_type_class
4335 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
4336 {
4337   switch (ELF32_R_TYPE (rela->r_info))
4338     {
4339     case R_386_RELATIVE:
4340       return reloc_class_relative;
4341     case R_386_JUMP_SLOT:
4342       return reloc_class_plt;
4343     case R_386_COPY:
4344       return reloc_class_copy;
4345     default:
4346       return reloc_class_normal;
4347     }
4348 }
4349
4350 /* Finish up the dynamic sections.  */
4351
4352 static bfd_boolean
4353 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4354                                   struct bfd_link_info *info)
4355 {
4356   struct elf_i386_link_hash_table *htab;
4357   bfd *dynobj;
4358   asection *sdyn;
4359
4360   htab = elf_i386_hash_table (info);
4361   dynobj = htab->elf.dynobj;
4362   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4363
4364   if (htab->elf.dynamic_sections_created)
4365     {
4366       Elf32_External_Dyn *dyncon, *dynconend;
4367
4368       if (sdyn == NULL || htab->elf.sgot == NULL)
4369         abort ();
4370
4371       dyncon = (Elf32_External_Dyn *) sdyn->contents;
4372       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4373       for (; dyncon < dynconend; dyncon++)
4374         {
4375           Elf_Internal_Dyn dyn;
4376           asection *s;
4377
4378           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4379
4380           switch (dyn.d_tag)
4381             {
4382             default:
4383               if (htab->is_vxworks
4384                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4385                 break;
4386               continue;
4387
4388             case DT_PLTGOT:
4389               s = htab->elf.sgotplt;
4390               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4391               break;
4392
4393             case DT_JMPREL:
4394               s = htab->elf.srelplt;
4395               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4396               break;
4397
4398             case DT_PLTRELSZ:
4399               s = htab->elf.srelplt;
4400               dyn.d_un.d_val = s->size;
4401               break;
4402
4403             case DT_RELSZ:
4404               /* My reading of the SVR4 ABI indicates that the
4405                  procedure linkage table relocs (DT_JMPREL) should be
4406                  included in the overall relocs (DT_REL).  This is
4407                  what Solaris does.  However, UnixWare can not handle
4408                  that case.  Therefore, we override the DT_RELSZ entry
4409                  here to make it not include the JMPREL relocs.  */
4410               s = htab->elf.srelplt;
4411               if (s == NULL)
4412                 continue;
4413               dyn.d_un.d_val -= s->size;
4414               break;
4415
4416             case DT_REL:
4417               /* We may not be using the standard ELF linker script.
4418                  If .rel.plt is the first .rel section, we adjust
4419                  DT_REL to not include it.  */
4420               s = htab->elf.srelplt;
4421               if (s == NULL)
4422                 continue;
4423               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4424                 continue;
4425               dyn.d_un.d_ptr += s->size;
4426               break;
4427             }
4428
4429           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4430         }
4431
4432       /* Fill in the first entry in the procedure linkage table.  */
4433       if (htab->elf.splt && htab->elf.splt->size > 0)
4434         {
4435           if (info->shared)
4436             {
4437               memcpy (htab->elf.splt->contents, elf_i386_pic_plt0_entry,
4438                       sizeof (elf_i386_pic_plt0_entry));
4439               memset (htab->elf.splt->contents + sizeof (elf_i386_pic_plt0_entry),
4440                       htab->plt0_pad_byte,
4441                       PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
4442             }
4443           else
4444             {
4445               memcpy (htab->elf.splt->contents, elf_i386_plt0_entry,
4446                       sizeof(elf_i386_plt0_entry));
4447               memset (htab->elf.splt->contents + sizeof (elf_i386_plt0_entry),
4448                       htab->plt0_pad_byte,
4449                       PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
4450               bfd_put_32 (output_bfd,
4451                           (htab->elf.sgotplt->output_section->vma
4452                            + htab->elf.sgotplt->output_offset
4453                            + 4),
4454                           htab->elf.splt->contents + 2);
4455               bfd_put_32 (output_bfd,
4456                           (htab->elf.sgotplt->output_section->vma
4457                            + htab->elf.sgotplt->output_offset
4458                            + 8),
4459                           htab->elf.splt->contents + 8);
4460
4461               if (htab->is_vxworks)
4462                 {
4463                   Elf_Internal_Rela rel;
4464
4465                   /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
4466                      On IA32 we use REL relocations so the addend goes in
4467                      the PLT directly.  */
4468                   rel.r_offset = (htab->elf.splt->output_section->vma
4469                                   + htab->elf.splt->output_offset
4470                                   + 2);
4471                   rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4472                   bfd_elf32_swap_reloc_out (output_bfd, &rel,
4473                                             htab->srelplt2->contents);
4474                   /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
4475                   rel.r_offset = (htab->elf.splt->output_section->vma
4476                                   + htab->elf.splt->output_offset
4477                                   + 8);
4478                   rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4479                   bfd_elf32_swap_reloc_out (output_bfd, &rel,
4480                                             htab->srelplt2->contents +
4481                                             sizeof (Elf32_External_Rel));
4482                 }
4483             }
4484
4485           /* UnixWare sets the entsize of .plt to 4, although that doesn't
4486              really seem like the right value.  */
4487           elf_section_data (htab->elf.splt->output_section)
4488             ->this_hdr.sh_entsize = 4;
4489
4490           /* Correct the .rel.plt.unloaded relocations.  */
4491           if (htab->is_vxworks && !info->shared)
4492             {
4493               int num_plts = (htab->elf.splt->size / PLT_ENTRY_SIZE) - 1;
4494               unsigned char *p;
4495
4496               p = htab->srelplt2->contents;
4497               if (info->shared)
4498                 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4499               else
4500                 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4501
4502               for (; num_plts; num_plts--)
4503                 {
4504                   Elf_Internal_Rela rel;
4505                   bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4506                   rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4507                   bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4508                   p += sizeof (Elf32_External_Rel);
4509
4510                   bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4511                   rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4512                   bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4513                   p += sizeof (Elf32_External_Rel);
4514                 }
4515             }
4516         }
4517     }
4518
4519   if (htab->elf.sgotplt)
4520     {
4521       /* Fill in the first three entries in the global offset table.  */
4522       if (htab->elf.sgotplt->size > 0)
4523         {
4524           bfd_put_32 (output_bfd,
4525                       (sdyn == NULL ? 0
4526                        : sdyn->output_section->vma + sdyn->output_offset),
4527                       htab->elf.sgotplt->contents);
4528           bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
4529           bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
4530         }
4531
4532       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
4533     }
4534
4535   if (htab->elf.sgot && htab->elf.sgot->size > 0)
4536     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
4537
4538   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
4539   htab_traverse (htab->loc_hash_table,
4540                  elf_i386_finish_local_dynamic_symbol,
4541                  info);
4542
4543   return TRUE;
4544 }
4545
4546 /* Return address for Ith PLT stub in section PLT, for relocation REL
4547    or (bfd_vma) -1 if it should not be included.  */
4548
4549 static bfd_vma
4550 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
4551                       const arelent *rel ATTRIBUTE_UNUSED)
4552 {
4553   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4554 }
4555
4556 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
4557
4558 static bfd_boolean
4559 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
4560 {
4561   if (h->plt.offset != (bfd_vma) -1
4562       && !h->def_regular
4563       && !h->pointer_equality_needed)
4564     return FALSE;
4565
4566   return _bfd_elf_hash_symbol (h);
4567 }
4568
4569 /* Hook called by the linker routine which adds symbols from an object
4570    file.  */
4571
4572 static bfd_boolean
4573 elf_i386_add_symbol_hook (bfd * abfd ATTRIBUTE_UNUSED,
4574                           struct bfd_link_info * info ATTRIBUTE_UNUSED,
4575                           Elf_Internal_Sym * sym,
4576                           const char ** namep ATTRIBUTE_UNUSED,
4577                           flagword * flagsp ATTRIBUTE_UNUSED,
4578                           asection ** secp ATTRIBUTE_UNUSED,
4579                           bfd_vma * valp ATTRIBUTE_UNUSED)
4580 {
4581   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4582     elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4583
4584   return TRUE;
4585 }
4586
4587 #define TARGET_LITTLE_SYM               bfd_elf32_i386_vec
4588 #define TARGET_LITTLE_NAME              "elf32-i386"
4589 #define ELF_ARCH                        bfd_arch_i386
4590 #define ELF_MACHINE_CODE                EM_386
4591 #define ELF_MAXPAGESIZE                 0x1000
4592
4593 #define elf_backend_can_gc_sections     1
4594 #define elf_backend_can_refcount        1
4595 #define elf_backend_want_got_plt        1
4596 #define elf_backend_plt_readonly        1
4597 #define elf_backend_want_plt_sym        0
4598 #define elf_backend_got_header_size     12
4599
4600 /* Support RELA for objdump of prelink objects.  */
4601 #define elf_info_to_howto                     elf_i386_info_to_howto_rel
4602 #define elf_info_to_howto_rel                 elf_i386_info_to_howto_rel
4603
4604 #define bfd_elf32_mkobject                    elf_i386_mkobject
4605
4606 #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
4607 #define bfd_elf32_bfd_link_hash_table_create  elf_i386_link_hash_table_create
4608 #define bfd_elf32_bfd_link_hash_table_free    elf_i386_link_hash_table_free
4609 #define bfd_elf32_bfd_reloc_type_lookup       elf_i386_reloc_type_lookup
4610 #define bfd_elf32_bfd_reloc_name_lookup       elf_i386_reloc_name_lookup
4611
4612 #define elf_backend_adjust_dynamic_symbol     elf_i386_adjust_dynamic_symbol
4613 #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
4614 #define elf_backend_check_relocs              elf_i386_check_relocs
4615 #define elf_backend_copy_indirect_symbol      elf_i386_copy_indirect_symbol
4616 #define elf_backend_create_dynamic_sections   elf_i386_create_dynamic_sections
4617 #define elf_backend_fake_sections             elf_i386_fake_sections
4618 #define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
4619 #define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
4620 #define elf_backend_gc_mark_hook              elf_i386_gc_mark_hook
4621 #define elf_backend_gc_sweep_hook             elf_i386_gc_sweep_hook
4622 #define elf_backend_grok_prstatus             elf_i386_grok_prstatus
4623 #define elf_backend_grok_psinfo               elf_i386_grok_psinfo
4624 #define elf_backend_reloc_type_class          elf_i386_reloc_type_class
4625 #define elf_backend_relocate_section          elf_i386_relocate_section
4626 #define elf_backend_size_dynamic_sections     elf_i386_size_dynamic_sections
4627 #define elf_backend_always_size_sections      elf_i386_always_size_sections
4628 #define elf_backend_omit_section_dynsym \
4629   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4630 #define elf_backend_plt_sym_val               elf_i386_plt_sym_val
4631 #define elf_backend_hash_symbol               elf_i386_hash_symbol
4632 #define elf_backend_add_symbol_hook           elf_i386_add_symbol_hook
4633 #undef  elf_backend_post_process_headers
4634 #define elf_backend_post_process_headers        _bfd_elf_set_osabi
4635
4636 #include "elf32-target.h"
4637
4638 /* FreeBSD support.  */
4639
4640 #undef  TARGET_LITTLE_SYM
4641 #define TARGET_LITTLE_SYM               bfd_elf32_i386_freebsd_vec
4642 #undef  TARGET_LITTLE_NAME
4643 #define TARGET_LITTLE_NAME              "elf32-i386-freebsd"
4644 #undef  ELF_OSABI
4645 #define ELF_OSABI                       ELFOSABI_FREEBSD
4646
4647 /* The kernel recognizes executables as valid only if they carry a
4648    "FreeBSD" label in the ELF header.  So we put this label on all
4649    executables and (for simplicity) also all other object files.  */
4650
4651 static void
4652 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
4653 {
4654   _bfd_elf_set_osabi (abfd, info);
4655
4656 #ifdef OLD_FREEBSD_ABI_LABEL
4657   /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
4658   memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4659 #endif
4660 }
4661
4662 #undef  elf_backend_post_process_headers
4663 #define elf_backend_post_process_headers        elf_i386_fbsd_post_process_headers
4664 #undef  elf32_bed
4665 #define elf32_bed                               elf32_i386_fbsd_bed
4666
4667 #undef elf_backend_add_symbol_hook
4668
4669 #include "elf32-target.h"
4670
4671 /* VxWorks support.  */
4672
4673 #undef  TARGET_LITTLE_SYM
4674 #define TARGET_LITTLE_SYM               bfd_elf32_i386_vxworks_vec
4675 #undef  TARGET_LITTLE_NAME
4676 #define TARGET_LITTLE_NAME              "elf32-i386-vxworks"
4677 #undef  ELF_OSABI
4678
4679 /* Like elf_i386_link_hash_table_create but with tweaks for VxWorks.  */
4680
4681 static struct bfd_link_hash_table *
4682 elf_i386_vxworks_link_hash_table_create (bfd *abfd)
4683 {
4684   struct bfd_link_hash_table *ret;
4685   struct elf_i386_link_hash_table *htab;
4686
4687   ret = elf_i386_link_hash_table_create (abfd);
4688   if (ret)
4689     {
4690       htab = (struct elf_i386_link_hash_table *) ret;
4691       htab->is_vxworks = 1;
4692       htab->plt0_pad_byte = 0x90;
4693     }
4694
4695   return ret;
4696 }
4697
4698
4699 #undef elf_backend_relocs_compatible
4700 #undef elf_backend_post_process_headers
4701 #undef bfd_elf32_bfd_link_hash_table_create
4702 #define bfd_elf32_bfd_link_hash_table_create \
4703   elf_i386_vxworks_link_hash_table_create
4704 #undef elf_backend_add_symbol_hook
4705 #define elf_backend_add_symbol_hook \
4706   elf_vxworks_add_symbol_hook
4707 #undef elf_backend_link_output_symbol_hook
4708 #define elf_backend_link_output_symbol_hook \
4709   elf_vxworks_link_output_symbol_hook
4710 #undef elf_backend_emit_relocs
4711 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
4712 #undef elf_backend_final_write_processing
4713 #define elf_backend_final_write_processing \
4714   elf_vxworks_final_write_processing
4715
4716 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4717    define it.  */
4718 #undef elf_backend_want_plt_sym
4719 #define elf_backend_want_plt_sym        1
4720
4721 #undef  elf32_bed
4722 #define elf32_bed                               elf32_i386_vxworks_bed
4723
4724 #include "elf32-target.h"