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