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