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