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