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